Compiler-Inlining.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. Smalltalk createPackage: 'Compiler-Inlining'!
  2. IRAssignment subclass: #IRInlinedAssignment
  3. instanceVariableNames: ''
  4. package: 'Compiler-Inlining'!
  5. !IRInlinedAssignment commentStamp!
  6. I represent an inlined assignment instruction.!
  7. !IRInlinedAssignment methodsFor: 'testing'!
  8. isInlined
  9. ^ true
  10. ! !
  11. !IRInlinedAssignment methodsFor: 'visiting'!
  12. accept: aVisitor
  13. ^ aVisitor visitIRInlinedAssignment: self
  14. ! !
  15. IRClosure subclass: #IRInlinedClosure
  16. instanceVariableNames: ''
  17. package: 'Compiler-Inlining'!
  18. !IRInlinedClosure commentStamp!
  19. I represent an inlined closure instruction.!
  20. !IRInlinedClosure methodsFor: 'testing'!
  21. isInlined
  22. ^ true
  23. ! !
  24. !IRInlinedClosure methodsFor: 'visiting'!
  25. accept: aVisitor
  26. aVisitor visitIRInlinedClosure: self
  27. ! !
  28. IRReturn subclass: #IRInlinedReturn
  29. instanceVariableNames: ''
  30. package: 'Compiler-Inlining'!
  31. !IRInlinedReturn commentStamp!
  32. I represent an inlined local return instruction.!
  33. !IRInlinedReturn methodsFor: 'testing'!
  34. isInlined
  35. ^ true
  36. ! !
  37. !IRInlinedReturn methodsFor: 'visiting'!
  38. accept: aVisitor
  39. ^ aVisitor visitIRInlinedReturn: self
  40. ! !
  41. IRSend subclass: #IRInlinedSend
  42. instanceVariableNames: ''
  43. package: 'Compiler-Inlining'!
  44. !IRInlinedSend commentStamp!
  45. I am the abstract super class of inlined message send instructions.!
  46. !IRInlinedSend methodsFor: 'accessing'!
  47. internalVariables
  48. "Answer a collection of internal variables required
  49. to perform the inlining"
  50. ^ #()
  51. ! !
  52. !IRInlinedSend methodsFor: 'testing'!
  53. isInlined
  54. ^ true
  55. ! !
  56. !IRInlinedSend methodsFor: 'visiting'!
  57. accept: aVisitor
  58. aVisitor visitInlinedSend: self
  59. ! !
  60. IRInlinedSend subclass: #IRInlinedIfFalse
  61. instanceVariableNames: ''
  62. package: 'Compiler-Inlining'!
  63. !IRInlinedIfFalse commentStamp!
  64. I represent an inlined `#ifFalse:` message send instruction.!
  65. !IRInlinedIfFalse methodsFor: 'visiting'!
  66. accept: aVisitor
  67. aVisitor visitIRInlinedIfFalse: self
  68. ! !
  69. IRInlinedSend subclass: #IRInlinedIfNilIfNotNil
  70. instanceVariableNames: ''
  71. package: 'Compiler-Inlining'!
  72. !IRInlinedIfNilIfNotNil commentStamp!
  73. I represent an inlined `#ifNil:ifNotNil:` message send instruction.!
  74. !IRInlinedIfNilIfNotNil methodsFor: 'accessing'!
  75. internalVariables
  76. ^ Array with: self receiverInternalVariable
  77. !
  78. receiverInternalVariable
  79. ^ IRVariable new
  80. variable: (AliasVar new name: self receiverInternalVariableName);
  81. yourself.
  82. !
  83. receiverInternalVariableName
  84. ^ '$receiver'
  85. ! !
  86. !IRInlinedIfNilIfNotNil methodsFor: 'visiting'!
  87. accept: aVisitor
  88. aVisitor visitIRInlinedIfNilIfNotNil: self
  89. ! !
  90. IRInlinedSend subclass: #IRInlinedIfTrue
  91. instanceVariableNames: ''
  92. package: 'Compiler-Inlining'!
  93. !IRInlinedIfTrue commentStamp!
  94. I represent an inlined `#ifTrue:` message send instruction.!
  95. !IRInlinedIfTrue methodsFor: 'visiting'!
  96. accept: aVisitor
  97. aVisitor visitIRInlinedIfTrue: self
  98. ! !
  99. IRInlinedSend subclass: #IRInlinedIfTrueIfFalse
  100. instanceVariableNames: ''
  101. package: 'Compiler-Inlining'!
  102. !IRInlinedIfTrueIfFalse commentStamp!
  103. I represent an inlined `#ifTrue:ifFalse:` message send instruction.!
  104. !IRInlinedIfTrueIfFalse methodsFor: 'visiting'!
  105. accept: aVisitor
  106. aVisitor visitIRInlinedIfTrueIfFalse: self
  107. ! !
  108. IRBlockSequence subclass: #IRInlinedSequence
  109. instanceVariableNames: ''
  110. package: 'Compiler-Inlining'!
  111. !IRInlinedSequence commentStamp!
  112. I represent a (block) sequence inside an inlined closure instruction (instance of `IRInlinedClosure`).!
  113. !IRInlinedSequence methodsFor: 'testing'!
  114. isInlined
  115. ^ true
  116. ! !
  117. !IRInlinedSequence methodsFor: 'visiting'!
  118. accept: aVisitor
  119. aVisitor visitIRInlinedSequence: self
  120. ! !
  121. IRVisitor subclass: #IRInliner
  122. instanceVariableNames: ''
  123. package: 'Compiler-Inlining'!
  124. !IRInliner commentStamp!
  125. I visit an IR tree, inlining message sends and block closures.
  126. Message selectors that can be inlined are answered by `IRSendInliner >> #inlinedSelectors`!
  127. !IRInliner methodsFor: 'factory'!
  128. assignmentInliner
  129. ^ IRAssignmentInliner new
  130. translator: self;
  131. yourself
  132. !
  133. returnInliner
  134. ^ IRReturnInliner new
  135. translator: self;
  136. yourself
  137. !
  138. sendInliner
  139. ^ IRSendInliner new
  140. translator: self;
  141. yourself
  142. ! !
  143. !IRInliner methodsFor: 'testing'!
  144. shouldInlineAssignment: anIRAssignment
  145. ^ anIRAssignment isInlined not and: [
  146. anIRAssignment right isSend and: [
  147. self shouldInlineSend: anIRAssignment right ]]
  148. !
  149. shouldInlineReturn: anIRReturn
  150. ^ anIRReturn isInlined not and: [
  151. anIRReturn expression isSend and: [
  152. self shouldInlineSend: anIRReturn expression ]]
  153. !
  154. shouldInlineSend: anIRSend
  155. ^ anIRSend isInlined not and: [
  156. IRSendInliner shouldInline: anIRSend ]
  157. ! !
  158. !IRInliner methodsFor: 'visiting'!
  159. visitIRAssignment: anIRAssignment
  160. ^ (self shouldInlineAssignment: anIRAssignment)
  161. ifTrue: [ self assignmentInliner inlineAssignment: anIRAssignment ]
  162. ifFalse: [ super visitIRAssignment: anIRAssignment ]
  163. !
  164. visitIRNonLocalReturn: anIRNonLocalReturn
  165. | localReturn |
  166. anIRNonLocalReturn scope canInlineNonLocalReturns ifTrue: [
  167. anIRNonLocalReturn scope methodScope removeNonLocalReturn: anIRNonLocalReturn scope.
  168. localReturn := IRReturn new
  169. scope: anIRNonLocalReturn scope;
  170. yourself.
  171. anIRNonLocalReturn instructions do: [ :each |
  172. localReturn add: each ].
  173. anIRNonLocalReturn replaceWith: localReturn.
  174. ^ localReturn ].
  175. ^ super visitIRNonLocalReturn: anIRNonLocalReturn
  176. !
  177. visitIRReturn: anIRReturn
  178. ^ (self shouldInlineReturn: anIRReturn)
  179. ifTrue: [ self returnInliner inlineReturn: anIRReturn ]
  180. ifFalse: [ super visitIRReturn: anIRReturn ]
  181. !
  182. visitIRSend: anIRSend
  183. ^ (self shouldInlineSend: anIRSend)
  184. ifTrue: [ self sendInliner inlineSend: anIRSend ]
  185. ifFalse: [ super visitIRSend: anIRSend ]
  186. ! !
  187. IRJSTranslator subclass: #IRInliningJSTranslator
  188. instanceVariableNames: ''
  189. package: 'Compiler-Inlining'!
  190. !IRInliningJSTranslator commentStamp!
  191. I am a specialized JavaScript translator able to write inlined IR instructions to JavaScript stream (`JSStream` instance).!
  192. !IRInliningJSTranslator methodsFor: 'visiting'!
  193. visitIRInlinedAssignment: anIRInlinedAssignment
  194. self visit: anIRInlinedAssignment right
  195. !
  196. visitIRInlinedClosure: anIRInlinedClosure
  197. self stream nextPutVars: (anIRInlinedClosure tempDeclarations collect: [ :each |
  198. each name asVariableName ]).
  199. anIRInlinedClosure instructions do: [ :each |
  200. self visit: each ]
  201. !
  202. visitIRInlinedIfFalse: anIRInlinedIfFalse
  203. self stream nextPutIf: [
  204. self stream nextPutAll: '!!$core.assert('.
  205. self visit: anIRInlinedIfFalse instructions first.
  206. self stream nextPutAll: ')' ]
  207. then: [ self visit: anIRInlinedIfFalse instructions last ]
  208. !
  209. visitIRInlinedIfNilIfNotNil: anIRInlinedIfNilIfNotNil
  210. self stream
  211. nextPutIf: [
  212. | recvVarName |
  213. recvVarName := anIRInlinedIfNilIfNotNil receiverInternalVariableName.
  214. self stream nextPutAll: '(', recvVarName, ' = '.
  215. self visit: anIRInlinedIfNilIfNotNil instructions first.
  216. self stream nextPutAll: ') == null || ', recvVarName, '.isNil' ]
  217. then: [ self visit: anIRInlinedIfNilIfNotNil instructions second ]
  218. else: [ self visit: anIRInlinedIfNilIfNotNil instructions third ]
  219. !
  220. visitIRInlinedIfTrue: anIRInlinedIfTrue
  221. self stream nextPutIf: [
  222. self stream nextPutAll: '$core.assert('.
  223. self visit: anIRInlinedIfTrue instructions first.
  224. self stream nextPutAll: ')' ]
  225. then: [ self visit: anIRInlinedIfTrue instructions last ]
  226. !
  227. visitIRInlinedIfTrueIfFalse: anIRInlinedIfTrueIfFalse
  228. self stream
  229. nextPutIf: [
  230. self stream nextPutAll: '$core.assert('.
  231. self visit: anIRInlinedIfTrueIfFalse instructions first.
  232. self stream nextPutAll: ')' ]
  233. then: [ self visit: anIRInlinedIfTrueIfFalse instructions second ]
  234. else: [ self visit: anIRInlinedIfTrueIfFalse instructions third ]
  235. !
  236. visitIRInlinedReturn: anIRInlinedReturn
  237. self visit: anIRInlinedReturn expression
  238. !
  239. visitIRInlinedSequence: anIRInlinedSequence
  240. anIRInlinedSequence instructions do: [ :each |
  241. self stream nextPutStatementWith: [ self visit: each ]]
  242. ! !
  243. Object subclass: #IRSendInliner
  244. instanceVariableNames: 'send translator'
  245. package: 'Compiler-Inlining'!
  246. !IRSendInliner commentStamp!
  247. I inline some message sends and block closure arguments. I heavily rely on #perform: to dispatch inlining methods.!
  248. !IRSendInliner methodsFor: 'accessing'!
  249. send
  250. ^ send
  251. !
  252. send: anIRSend
  253. send := anIRSend
  254. !
  255. translator
  256. ^ translator
  257. !
  258. translator: anASTTranslator
  259. translator := anASTTranslator
  260. ! !
  261. !IRSendInliner methodsFor: 'error handling'!
  262. inliningError: aString
  263. InliningError signal: aString
  264. ! !
  265. !IRSendInliner methodsFor: 'factory'!
  266. inlinedClosure
  267. ^ IRInlinedClosure new
  268. !
  269. inlinedSequence
  270. ^ IRInlinedSequence new
  271. ! !
  272. !IRSendInliner methodsFor: 'inlining'!
  273. ifFalse: anIRInstruction
  274. ^ self inlinedSend: IRInlinedIfFalse new withBlock: anIRInstruction
  275. !
  276. ifFalse: anIRInstruction ifTrue: anotherIRInstruction
  277. ^ self perform: #ifTrue:ifFalse: withArguments: { anotherIRInstruction. anIRInstruction }
  278. !
  279. ifNil: anIRInstruction
  280. ^ self
  281. inlinedSend: IRInlinedIfNilIfNotNil new
  282. withBlock: anIRInstruction
  283. withBlock: (IRClosure new
  284. scope: anIRInstruction scope copy;
  285. add: (IRBlockSequence new
  286. add: self send receiver;
  287. yourself);
  288. yourself)
  289. !
  290. ifNil: anIRInstruction ifNotNil: anotherIRInstruction
  291. ^ self inlinedSend: IRInlinedIfNilIfNotNil new withBlock: anIRInstruction withBlock: anotherIRInstruction
  292. !
  293. ifNotNil: anIRInstruction
  294. ^ self
  295. inlinedSend: IRInlinedIfNilIfNotNil new
  296. withBlock: (IRClosure new
  297. scope: anIRInstruction scope copy;
  298. add: (IRBlockSequence new
  299. add: self send receiver;
  300. yourself);
  301. yourself)
  302. withBlock: anIRInstruction
  303. !
  304. ifNotNil: anIRInstruction ifNil: anotherIRInstruction
  305. ^ self inlinedSend: IRInlinedIfNilIfNotNil new withBlock: anotherIRInstruction withBlock: anIRInstruction
  306. !
  307. ifTrue: anIRInstruction
  308. ^ self inlinedSend: IRInlinedIfTrue new withBlock: anIRInstruction
  309. !
  310. ifTrue: anIRInstruction ifFalse: anotherIRInstruction
  311. ^ self inlinedSend: IRInlinedIfTrueIfFalse new withBlock: anIRInstruction withBlock: anotherIRInstruction
  312. !
  313. inlineClosure: anIRClosure
  314. | inlinedClosure sequence statements |
  315. inlinedClosure := self inlinedClosure.
  316. inlinedClosure
  317. scope: anIRClosure scope;
  318. parent: anIRClosure parent.
  319. "Add the possible temp declarations"
  320. anIRClosure tempDeclarations do: [ :each |
  321. inlinedClosure add: each ].
  322. "Add a block sequence"
  323. sequence := self inlinedSequence.
  324. "Map the closure arguments to the receiver of the message send"
  325. anIRClosure arguments do: [ :each |
  326. inlinedClosure add: (IRTempDeclaration new name: each; yourself).
  327. sequence add: (IRAssignment new
  328. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: each; yourself));
  329. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: '$receiver'; yourself));
  330. yourself) ].
  331. "To ensure the correct order of the closure instructions: first the temps then the sequence"
  332. inlinedClosure add: sequence.
  333. "Get all the statements"
  334. statements := anIRClosure sequence instructions.
  335. statements ifNotEmpty: [
  336. statements allButLast do: [ :each | sequence add: each ].
  337. "Inlined closures don't have implicit local returns"
  338. (statements last isReturn and: [ statements last isBlockReturn ])
  339. ifTrue: [ sequence add: statements last expression ]
  340. ifFalse: [ sequence add: statements last ] ].
  341. ^ inlinedClosure
  342. !
  343. inlineSend: anIRSend
  344. self send: anIRSend.
  345. ^ self
  346. perform: self send selector
  347. withArguments: self send arguments
  348. !
  349. inlinedSend: inlinedSend withBlock: anIRInstruction
  350. | inlinedClosure |
  351. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  352. anIRInstruction arguments size = 0 ifFalse: [ self inliningError: 'Inlined block should have zero argument' ].
  353. inlinedClosure := self translator visit: (self inlineClosure: anIRInstruction).
  354. inlinedSend
  355. add: self send receiver;
  356. add: inlinedClosure.
  357. self send replaceWith: inlinedSend.
  358. inlinedSend method internalVariables
  359. addAll: inlinedSend internalVariables.
  360. ^ inlinedSend
  361. !
  362. inlinedSend: inlinedSend withBlock: anIRInstruction withBlock: anotherIRInstruction
  363. | inlinedClosure1 inlinedClosure2 |
  364. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  365. anotherIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  366. inlinedClosure1 := self translator visit: (self inlineClosure: anIRInstruction).
  367. inlinedClosure2 := self translator visit: (self inlineClosure: anotherIRInstruction).
  368. inlinedSend
  369. add: self send receiver;
  370. add: inlinedClosure1;
  371. add: inlinedClosure2.
  372. self send replaceWith: inlinedSend.
  373. inlinedSend method internalVariables
  374. addAll: inlinedSend internalVariables.
  375. ^ inlinedSend
  376. ! !
  377. !IRSendInliner class methodsFor: 'accessing'!
  378. inlinedSelectors
  379. ^ #('ifTrue:' 'ifFalse:' 'ifTrue:ifFalse:' 'ifFalse:ifTrue:' 'ifNil:' 'ifNotNil:' 'ifNil:ifNotNil:' 'ifNotNil:ifNil:')
  380. !
  381. shouldInline: anIRSend
  382. (self inlinedSelectors includes: anIRSend selector) ifFalse: [ ^ false ].
  383. ^ anIRSend arguments allSatisfy: [ :each | each isClosure ]
  384. ! !
  385. IRSendInliner subclass: #IRAssignmentInliner
  386. instanceVariableNames: 'assignment'
  387. package: 'Compiler-Inlining'!
  388. !IRAssignmentInliner commentStamp!
  389. I inline message sends together with assignments by moving them around into the inline closure instructions.
  390. ##Example
  391. foo
  392. | a |
  393. a := true ifTrue: [ 1 ]
  394. Will produce:
  395. if($core.assert(true) {
  396. a = 1;
  397. };!
  398. !IRAssignmentInliner methodsFor: 'accessing'!
  399. assignment
  400. ^ assignment
  401. !
  402. assignment: aNode
  403. assignment := aNode
  404. ! !
  405. !IRAssignmentInliner methodsFor: 'inlining'!
  406. inlineAssignment: anIRAssignment
  407. | inlinedAssignment |
  408. self assignment: anIRAssignment.
  409. inlinedAssignment := IRInlinedAssignment new.
  410. anIRAssignment instructions do: [ :each |
  411. inlinedAssignment add: each ].
  412. anIRAssignment replaceWith: inlinedAssignment.
  413. self inlineSend: inlinedAssignment right.
  414. ^ inlinedAssignment
  415. !
  416. inlineClosure: anIRClosure
  417. | inlinedClosure statements |
  418. inlinedClosure := super inlineClosure: anIRClosure.
  419. statements := inlinedClosure sequence instructions.
  420. statements ifNotEmpty: [
  421. statements last canBeAssigned ifTrue: [
  422. statements last replaceWith: (IRAssignment new
  423. add: self assignment left;
  424. add: statements last copy;
  425. yourself) ] ].
  426. ^ inlinedClosure
  427. ! !
  428. IRSendInliner subclass: #IRReturnInliner
  429. instanceVariableNames: ''
  430. package: 'Compiler-Inlining'!
  431. !IRReturnInliner commentStamp!
  432. I inline message sends with inlined closure together with a return instruction.!
  433. !IRReturnInliner methodsFor: 'factory'!
  434. inlinedReturn
  435. ^ IRInlinedReturn new
  436. ! !
  437. !IRReturnInliner methodsFor: 'inlining'!
  438. inlineClosure: anIRClosure
  439. | closure statements |
  440. closure := super inlineClosure: anIRClosure.
  441. statements := closure sequence instructions.
  442. statements ifNotEmpty: [
  443. statements last isReturn
  444. ifFalse: [ statements last replaceWith: (IRReturn new
  445. add: statements last copy;
  446. yourself)] ].
  447. ^ closure
  448. !
  449. inlineReturn: anIRReturn
  450. | return |
  451. return := self inlinedReturn.
  452. anIRReturn instructions do: [ :each |
  453. return add: each ].
  454. anIRReturn replaceWith: return.
  455. self inlineSend: return expression.
  456. ^ return
  457. ! !
  458. CodeGenerator subclass: #InliningCodeGenerator
  459. instanceVariableNames: ''
  460. package: 'Compiler-Inlining'!
  461. !InliningCodeGenerator commentStamp!
  462. I am a specialized code generator that uses inlining to produce more optimized JavaScript output!
  463. !InliningCodeGenerator methodsFor: 'compiling'!
  464. compileNode: aNode
  465. | ir stream |
  466. self semanticAnalyzer visit: aNode.
  467. ir := self translator visit: aNode.
  468. self inliner visit: ir.
  469. ^ self irTranslator
  470. currentClass: self currentClass;
  471. visit: ir;
  472. contents
  473. !
  474. inliner
  475. ^ IRInliner new
  476. !
  477. irTranslator
  478. ^ IRInliningJSTranslator new
  479. ! !
  480. SemanticError subclass: #InliningError
  481. instanceVariableNames: ''
  482. package: 'Compiler-Inlining'!
  483. !InliningError commentStamp!
  484. Instances of InliningError are signaled when using an `InliningCodeGenerator`in a `Compiler`.!