2
0

Compiler-Inlining.st 16 KB

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