Compiler-Inlining.st 15 KB

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