1
0

Compiler-Inlining.st 17 KB

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