Compiler-Inlining.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 change local returns into result value itself"
  338. sequence add: statements last asInlinedBlockResult ].
  339. ^ inlinedClosure
  340. !
  341. inlineSend: anIRSend
  342. self send: anIRSend.
  343. ^ self
  344. perform: self send selector
  345. withArguments: self send arguments
  346. !
  347. inlinedSend: inlinedSend withBlock: anIRInstruction
  348. | inlinedClosure |
  349. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  350. anIRInstruction arguments size = 0 ifFalse: [ self inliningError: 'Inlined block should have zero argument' ].
  351. inlinedClosure := self translator visit: (self inlineClosure: anIRInstruction).
  352. inlinedSend
  353. add: self send receiver;
  354. add: inlinedClosure.
  355. self send replaceWith: inlinedSend.
  356. inlinedSend method internalVariables
  357. addAll: inlinedSend internalVariables.
  358. ^ inlinedSend
  359. !
  360. inlinedSend: inlinedSend withBlock: anIRInstruction withBlock: anotherIRInstruction
  361. | inlinedClosure1 inlinedClosure2 |
  362. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  363. anotherIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  364. inlinedClosure1 := self translator visit: (self inlineClosure: anIRInstruction).
  365. inlinedClosure2 := self translator visit: (self inlineClosure: anotherIRInstruction).
  366. inlinedSend
  367. add: self send receiver;
  368. add: inlinedClosure1;
  369. add: inlinedClosure2.
  370. self send replaceWith: inlinedSend.
  371. inlinedSend method internalVariables
  372. addAll: inlinedSend internalVariables.
  373. ^ inlinedSend
  374. ! !
  375. !IRSendInliner class methodsFor: 'accessing'!
  376. inlinedSelectors
  377. ^ #('ifTrue:' 'ifFalse:' 'ifTrue:ifFalse:' 'ifFalse:ifTrue:' 'ifNil:' 'ifNotNil:' 'ifNil:ifNotNil:' 'ifNotNil:ifNil:')
  378. !
  379. shouldInline: anIRSend
  380. (self inlinedSelectors includes: anIRSend selector) ifFalse: [ ^ false ].
  381. ^ anIRSend arguments allSatisfy: [ :each | each isClosure ]
  382. ! !
  383. IRSendInliner subclass: #IRAssignmentInliner
  384. instanceVariableNames: 'assignment'
  385. package: 'Compiler-Inlining'!
  386. !IRAssignmentInliner commentStamp!
  387. I inline message sends together with assignments by moving them around into the inline closure instructions.
  388. ##Example
  389. foo
  390. | a |
  391. a := true ifTrue: [ 1 ]
  392. Will produce:
  393. if($core.assert(true) {
  394. a = 1;
  395. };!
  396. !IRAssignmentInliner methodsFor: 'accessing'!
  397. assignment
  398. ^ assignment
  399. !
  400. assignment: aNode
  401. assignment := aNode
  402. ! !
  403. !IRAssignmentInliner methodsFor: 'inlining'!
  404. inlineAssignment: anIRAssignment
  405. | inlinedAssignment |
  406. self assignment: anIRAssignment.
  407. inlinedAssignment := IRInlinedAssignment new.
  408. anIRAssignment instructions do: [ :each |
  409. inlinedAssignment add: each ].
  410. anIRAssignment replaceWith: inlinedAssignment.
  411. self inlineSend: inlinedAssignment right.
  412. ^ inlinedAssignment
  413. !
  414. inlineClosure: anIRClosure
  415. | closure statements |
  416. closure := super inlineClosure: anIRClosure.
  417. statements := closure sequence instructions.
  418. statements ifNotEmpty: [
  419. statements last yieldsValue ifTrue: [
  420. statements last replaceWith: (IRAssignment new
  421. add: self assignment left;
  422. add: statements last copy;
  423. yourself) ] ].
  424. ^ closure
  425. ! !
  426. IRSendInliner subclass: #IRReturnInliner
  427. instanceVariableNames: ''
  428. package: 'Compiler-Inlining'!
  429. !IRReturnInliner commentStamp!
  430. I inline message sends with inlined closure together with a return instruction.!
  431. !IRReturnInliner methodsFor: 'factory'!
  432. inlinedReturn
  433. ^ IRInlinedReturn new
  434. ! !
  435. !IRReturnInliner methodsFor: 'inlining'!
  436. inlineClosure: anIRClosure
  437. | closure statements |
  438. closure := super inlineClosure: anIRClosure.
  439. statements := closure sequence instructions.
  440. statements ifNotEmpty: [
  441. statements last yieldsValue ifTrue: [
  442. statements last replaceWith: (IRReturn new
  443. add: statements last copy;
  444. yourself)] ].
  445. ^ closure
  446. !
  447. inlineReturn: anIRReturn
  448. | return |
  449. return := self inlinedReturn.
  450. anIRReturn instructions do: [ :each |
  451. return add: each ].
  452. anIRReturn replaceWith: return.
  453. self inlineSend: return expression.
  454. ^ return
  455. ! !
  456. CodeGenerator subclass: #InliningCodeGenerator
  457. instanceVariableNames: ''
  458. package: 'Compiler-Inlining'!
  459. !InliningCodeGenerator commentStamp!
  460. I am a specialized code generator that uses inlining to produce more optimized JavaScript output!
  461. !InliningCodeGenerator methodsFor: 'compiling'!
  462. compileNode: aNode
  463. | ir stream |
  464. self semanticAnalyzer visit: aNode.
  465. ir := self translator visit: aNode.
  466. self inliner visit: ir.
  467. ^ self irTranslator
  468. currentClass: self currentClass;
  469. visit: ir;
  470. contents
  471. !
  472. inliner
  473. ^ IRInliner new
  474. !
  475. irTranslator
  476. ^ IRInliningJSTranslator new
  477. ! !
  478. SemanticError subclass: #InliningError
  479. instanceVariableNames: ''
  480. package: 'Compiler-Inlining'!
  481. !InliningError commentStamp!
  482. Instances of InliningError are signaled when using an `InliningCodeGenerator`in a `Compiler`.!
  483. !IRBlockReturn methodsFor: '*Compiler-Inlining'!
  484. asInlinedBlockResult
  485. ^ self expression
  486. ! !
  487. !IRInstruction methodsFor: '*Compiler-Inlining'!
  488. asInlinedBlockResult
  489. ^ self
  490. ! !