1
0

Compiler-Inlining.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 instructions last isSend and: [
  147. self shouldInlineSend: (anIRAssignment instructions last) ]]
  148. !
  149. shouldInlineReturn: anIRReturn
  150. ^ anIRReturn isInlined not and: [
  151. anIRReturn instructions first isSend and: [
  152. self shouldInlineSend: (anIRReturn instructions first) ]]
  153. !
  154. shouldInlineSend: anIRSend
  155. ^ anIRSend isInlined not and: [
  156. IRSendInliner shouldInline: anIRSend ]
  157. ! !
  158. !IRInliner methodsFor: 'visiting'!
  159. transformNonLocalReturn: anIRNonLocalReturn
  160. "Replace a non local return into a local return"
  161. | localReturn |
  162. anIRNonLocalReturn scope canInlineNonLocalReturns ifTrue: [
  163. anIRNonLocalReturn scope methodScope removeNonLocalReturn: anIRNonLocalReturn scope.
  164. localReturn := IRReturn new
  165. scope: anIRNonLocalReturn scope;
  166. yourself.
  167. anIRNonLocalReturn instructions do: [ :each |
  168. localReturn add: each ].
  169. anIRNonLocalReturn replaceWith: localReturn.
  170. ^ localReturn ].
  171. ^ super visitIRNonLocalReturn: anIRNonLocalReturn
  172. !
  173. visitIRAssignment: anIRAssignment
  174. ^ (self shouldInlineAssignment: anIRAssignment)
  175. ifTrue: [ self assignmentInliner inlineAssignment: anIRAssignment ]
  176. ifFalse: [ super visitIRAssignment: anIRAssignment ]
  177. !
  178. visitIRNonLocalReturn: anIRNonLocalReturn
  179. ^ self transformNonLocalReturn: anIRNonLocalReturn
  180. !
  181. visitIRReturn: anIRReturn
  182. ^ (self shouldInlineReturn: anIRReturn)
  183. ifTrue: [ self returnInliner inlineReturn: anIRReturn ]
  184. ifFalse: [ super visitIRReturn: anIRReturn ]
  185. !
  186. visitIRSend: anIRSend
  187. ^ (self shouldInlineSend: anIRSend)
  188. ifTrue: [ self sendInliner inlineSend: anIRSend ]
  189. ifFalse: [ super visitIRSend: anIRSend ]
  190. ! !
  191. IRJSTranslator subclass: #IRInliningJSTranslator
  192. instanceVariableNames: ''
  193. package: 'Compiler-Inlining'!
  194. !IRInliningJSTranslator commentStamp!
  195. I am a specialized JavaScript translator able to write inlined IR instructions to JavaScript stream (`JSStream` instance).!
  196. !IRInliningJSTranslator methodsFor: 'visiting'!
  197. visitIRInlinedAssignment: anIRInlinedAssignment
  198. self visit: anIRInlinedAssignment instructions last
  199. !
  200. visitIRInlinedClosure: anIRInlinedClosure
  201. self stream nextPutVars: (anIRInlinedClosure tempDeclarations collect: [ :each |
  202. each name asVariableName ]).
  203. anIRInlinedClosure instructions do: [ :each |
  204. self visit: each ]
  205. !
  206. visitIRInlinedIfFalse: anIRInlinedIfFalse
  207. self stream nextPutIf: [
  208. self stream nextPutAll: '!!$core.assert('.
  209. self visit: anIRInlinedIfFalse instructions first.
  210. self stream nextPutAll: ')' ]
  211. then: [ self visit: anIRInlinedIfFalse instructions last ]
  212. !
  213. visitIRInlinedIfNilIfNotNil: anIRInlinedIfNilIfNotNil
  214. self stream
  215. nextPutIf: [
  216. | recvVarName |
  217. recvVarName := anIRInlinedIfNilIfNotNil receiverInternalVariableName.
  218. self stream nextPutAll: '(', recvVarName, ' = '.
  219. self visit: anIRInlinedIfNilIfNotNil instructions first.
  220. self stream nextPutAll: ') == null || ', recvVarName, '.isNil' ]
  221. then: [ self visit: anIRInlinedIfNilIfNotNil instructions second ]
  222. else: [ self visit: anIRInlinedIfNilIfNotNil instructions third ]
  223. !
  224. visitIRInlinedIfTrue: anIRInlinedIfTrue
  225. self stream nextPutIf: [
  226. self stream nextPutAll: '$core.assert('.
  227. self visit: anIRInlinedIfTrue instructions first.
  228. self stream nextPutAll: ')' ]
  229. then: [ self visit: anIRInlinedIfTrue instructions last ]
  230. !
  231. visitIRInlinedIfTrueIfFalse: anIRInlinedIfTrueIfFalse
  232. self stream
  233. nextPutIf: [
  234. self stream nextPutAll: '$core.assert('.
  235. self visit: anIRInlinedIfTrueIfFalse instructions first.
  236. self stream nextPutAll: ')' ]
  237. then: [ self visit: anIRInlinedIfTrueIfFalse instructions second ]
  238. else: [ self visit: anIRInlinedIfTrueIfFalse instructions third ]
  239. !
  240. visitIRInlinedNonLocalReturn: anIRInlinedReturn
  241. self stream nextPutStatementWith: [
  242. self visit: anIRInlinedReturn instructions last ].
  243. self stream nextPutNonLocalReturnWith: [ ]
  244. !
  245. visitIRInlinedReturn: anIRInlinedReturn
  246. self visit: anIRInlinedReturn instructions last
  247. !
  248. visitIRInlinedSequence: anIRInlinedSequence
  249. anIRInlinedSequence instructions do: [ :each |
  250. self stream nextPutStatementWith: [ self visit: each ]]
  251. ! !
  252. Object subclass: #IRSendInliner
  253. instanceVariableNames: 'send translator'
  254. package: 'Compiler-Inlining'!
  255. !IRSendInliner commentStamp!
  256. I inline some message sends and block closure arguments. I heavily rely on #perform: to dispatch inlining methods.!
  257. !IRSendInliner methodsFor: 'accessing'!
  258. send
  259. ^ send
  260. !
  261. send: anIRSend
  262. send := anIRSend
  263. !
  264. translator
  265. ^ translator
  266. !
  267. translator: anASTTranslator
  268. translator := anASTTranslator
  269. ! !
  270. !IRSendInliner methodsFor: 'error handling'!
  271. inliningError: aString
  272. InliningError signal: aString
  273. ! !
  274. !IRSendInliner methodsFor: 'factory'!
  275. inlinedClosure
  276. ^ IRInlinedClosure new
  277. !
  278. inlinedSequence
  279. ^ IRInlinedSequence new
  280. ! !
  281. !IRSendInliner methodsFor: 'inlining'!
  282. ifFalse: anIRInstruction
  283. ^ self inlinedSend: IRInlinedIfFalse new withBlock: anIRInstruction
  284. !
  285. ifFalse: anIRInstruction ifTrue: anotherIRInstruction
  286. ^ self perform: #ifTrue:ifFalse: withArguments: { anotherIRInstruction. anIRInstruction }
  287. !
  288. ifNil: anIRInstruction
  289. ^ self
  290. inlinedSend: IRInlinedIfNilIfNotNil new
  291. withBlock: anIRInstruction
  292. withBlock: (IRClosure new
  293. scope: anIRInstruction scope copy;
  294. add: (IRBlockSequence new
  295. add: self send instructions first;
  296. yourself);
  297. yourself)
  298. !
  299. ifNil: anIRInstruction ifNotNil: anotherIRInstruction
  300. ^ self inlinedSend: IRInlinedIfNilIfNotNil new withBlock: anIRInstruction withBlock: anotherIRInstruction
  301. !
  302. ifNotNil: anIRInstruction
  303. ^ self
  304. inlinedSend: IRInlinedIfNilIfNotNil new
  305. withBlock: (IRClosure new
  306. scope: anIRInstruction scope copy;
  307. add: (IRBlockSequence new
  308. add: self send instructions first;
  309. yourself);
  310. yourself)
  311. withBlock: anIRInstruction
  312. !
  313. ifNotNil: anIRInstruction ifNil: anotherIRInstruction
  314. ^ self inlinedSend: IRInlinedIfNilIfNotNil new withBlock: anotherIRInstruction withBlock: anIRInstruction
  315. !
  316. ifTrue: anIRInstruction
  317. ^ self inlinedSend: IRInlinedIfTrue new withBlock: anIRInstruction
  318. !
  319. ifTrue: anIRInstruction ifFalse: anotherIRInstruction
  320. ^ self inlinedSend: IRInlinedIfTrueIfFalse new withBlock: anIRInstruction withBlock: anotherIRInstruction
  321. !
  322. inlineClosure: anIRClosure
  323. | inlinedClosure sequence statements |
  324. inlinedClosure := self inlinedClosure.
  325. inlinedClosure
  326. scope: anIRClosure scope;
  327. parent: anIRClosure parent.
  328. "Add the possible temp declarations"
  329. anIRClosure tempDeclarations do: [ :each |
  330. inlinedClosure add: each ].
  331. "Add a block sequence"
  332. sequence := self inlinedSequence.
  333. "Map the closure arguments to the receiver of the message send"
  334. anIRClosure arguments do: [ :each |
  335. inlinedClosure add: (IRTempDeclaration new name: each; yourself).
  336. sequence add: (IRAssignment new
  337. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: each; yourself));
  338. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: '$receiver'; yourself));
  339. yourself) ].
  340. "To ensure the correct order of the closure instructions: first the temps then the sequence"
  341. inlinedClosure add: sequence.
  342. "Get all the statements"
  343. statements := anIRClosure instructions last instructions.
  344. statements ifNotEmpty: [
  345. statements allButLast do: [ :each | sequence add: each ].
  346. "Inlined closures don't have implicit local returns"
  347. (statements last isReturn and: [ statements last isBlockReturn ])
  348. ifTrue: [ sequence add: statements last instructions first ]
  349. ifFalse: [ sequence add: statements last ] ].
  350. ^ inlinedClosure
  351. !
  352. inlineSend: anIRSend
  353. self send: anIRSend.
  354. ^ self
  355. perform: self send selector
  356. withArguments: self send instructions allButFirst
  357. !
  358. inlinedSend: inlinedSend withBlock: anIRInstruction
  359. | inlinedClosure |
  360. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  361. anIRInstruction arguments size = 0 ifFalse: [ self inliningError: 'Inlined block should have zero argument' ].
  362. inlinedClosure := self translator visit: (self inlineClosure: anIRInstruction).
  363. inlinedSend
  364. add: self send instructions first;
  365. add: inlinedClosure.
  366. self send replaceWith: inlinedSend.
  367. inlinedSend method internalVariables
  368. addAll: inlinedSend internalVariables.
  369. ^ inlinedSend
  370. !
  371. inlinedSend: inlinedSend withBlock: anIRInstruction withBlock: anotherIRInstruction
  372. | inlinedClosure1 inlinedClosure2 |
  373. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  374. anotherIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  375. inlinedClosure1 := self translator visit: (self inlineClosure: anIRInstruction).
  376. inlinedClosure2 := self translator visit: (self inlineClosure: anotherIRInstruction).
  377. inlinedSend
  378. add: self send instructions first;
  379. add: inlinedClosure1;
  380. add: inlinedClosure2.
  381. self send replaceWith: inlinedSend.
  382. inlinedSend method internalVariables
  383. addAll: inlinedSend internalVariables.
  384. ^ inlinedSend
  385. ! !
  386. !IRSendInliner class methodsFor: 'accessing'!
  387. inlinedSelectors
  388. ^ #('ifTrue:' 'ifFalse:' 'ifTrue:ifFalse:' 'ifFalse:ifTrue:' 'ifNil:' 'ifNotNil:' 'ifNil:ifNotNil:' 'ifNotNil:ifNil:')
  389. !
  390. shouldInline: anIRInstruction
  391. (self inlinedSelectors includes: anIRInstruction selector) ifFalse: [ ^ false ].
  392. ^ anIRInstruction instructions allButFirst allSatisfy: [ :each | each isClosure]
  393. ! !
  394. IRSendInliner subclass: #IRAssignmentInliner
  395. instanceVariableNames: 'assignment'
  396. package: 'Compiler-Inlining'!
  397. !IRAssignmentInliner commentStamp!
  398. I inline message sends together with assignments by moving them around into the inline closure instructions.
  399. ##Example
  400. foo
  401. | a |
  402. a := true ifTrue: [ 1 ]
  403. Will produce:
  404. if($core.assert(true) {
  405. a = 1;
  406. };!
  407. !IRAssignmentInliner methodsFor: 'accessing'!
  408. assignment
  409. ^ assignment
  410. !
  411. assignment: aNode
  412. assignment := aNode
  413. ! !
  414. !IRAssignmentInliner methodsFor: 'inlining'!
  415. inlineAssignment: anIRAssignment
  416. | inlinedAssignment |
  417. self assignment: anIRAssignment.
  418. inlinedAssignment := IRInlinedAssignment new.
  419. anIRAssignment instructions do: [ :each |
  420. inlinedAssignment add: each ].
  421. anIRAssignment replaceWith: inlinedAssignment.
  422. self inlineSend: inlinedAssignment instructions last.
  423. ^ inlinedAssignment
  424. !
  425. inlineClosure: anIRClosure
  426. | inlinedClosure statements |
  427. inlinedClosure := super inlineClosure: anIRClosure.
  428. statements := inlinedClosure instructions last instructions.
  429. statements ifNotEmpty: [
  430. statements last canBeAssigned ifTrue: [
  431. statements last replaceWith: (IRAssignment new
  432. add: self assignment instructions first;
  433. add: statements last copy;
  434. yourself) ] ].
  435. ^ inlinedClosure
  436. ! !
  437. IRSendInliner subclass: #IRReturnInliner
  438. instanceVariableNames: ''
  439. package: 'Compiler-Inlining'!
  440. !IRReturnInliner commentStamp!
  441. I inline message sends with inlined closure together with a return instruction.!
  442. !IRReturnInliner methodsFor: 'factory'!
  443. inlinedReturn
  444. ^ IRInlinedReturn new
  445. ! !
  446. !IRReturnInliner methodsFor: 'inlining'!
  447. inlineClosure: anIRClosure
  448. | closure statements |
  449. closure := super inlineClosure: anIRClosure.
  450. statements := closure instructions last instructions.
  451. statements ifNotEmpty: [
  452. statements last isReturn
  453. ifFalse: [ statements last replaceWith: (IRReturn new
  454. add: statements last copy;
  455. yourself)] ].
  456. ^ closure
  457. !
  458. inlineReturn: anIRReturn
  459. | return |
  460. return := self inlinedReturn.
  461. anIRReturn instructions do: [ :each |
  462. return add: each ].
  463. anIRReturn replaceWith: return.
  464. self inlineSend: return instructions last.
  465. ^ return
  466. ! !
  467. CodeGenerator subclass: #InliningCodeGenerator
  468. instanceVariableNames: ''
  469. package: 'Compiler-Inlining'!
  470. !InliningCodeGenerator commentStamp!
  471. I am a specialized code generator that uses inlining to produce more optimized JavaScript output!
  472. !InliningCodeGenerator methodsFor: 'compiling'!
  473. compileNode: aNode
  474. | ir stream |
  475. self semanticAnalyzer visit: aNode.
  476. ir := self translator visit: aNode.
  477. self inliner visit: ir.
  478. ^ self irTranslator
  479. currentClass: self currentClass;
  480. visit: ir;
  481. contents
  482. !
  483. inliner
  484. ^ IRInliner new
  485. !
  486. irTranslator
  487. ^ IRInliningJSTranslator new
  488. ! !
  489. SemanticError subclass: #InliningError
  490. instanceVariableNames: ''
  491. package: 'Compiler-Inlining'!
  492. !InliningError commentStamp!
  493. Instances of InliningError are signaled when using an `InliningCodeGenerator`in a `Compiler`.!