1
0

Compiler-IR.st 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. Smalltalk createPackage: 'Compiler-IR'!
  2. NodeVisitor subclass: #IRASTTranslator
  3. instanceVariableNames: 'source theClass method sequence nextAlias'
  4. package: 'Compiler-IR'!
  5. !IRASTTranslator commentStamp!
  6. I am the AST (abstract syntax tree) visitor responsible for building the intermediate representation graph.!
  7. !IRASTTranslator methodsFor: 'accessing'!
  8. method
  9. ^ method
  10. !
  11. method: anIRMethod
  12. method := anIRMethod
  13. !
  14. nextAlias
  15. nextAlias ifNil: [ nextAlias := 0 ].
  16. nextAlias := nextAlias + 1.
  17. ^ nextAlias asString
  18. !
  19. sequence
  20. ^ sequence
  21. !
  22. sequence: anIRSequence
  23. sequence := anIRSequence
  24. !
  25. source
  26. ^ source
  27. !
  28. source: aString
  29. source := aString
  30. !
  31. theClass
  32. ^ theClass
  33. !
  34. theClass: aClass
  35. theClass := aClass
  36. !
  37. withSequence: aSequence do: aBlock
  38. | outerSequence |
  39. outerSequence := self sequence.
  40. self sequence: aSequence.
  41. aBlock value.
  42. self sequence: outerSequence.
  43. ^ aSequence
  44. ! !
  45. !IRASTTranslator methodsFor: 'visiting'!
  46. alias: aNode
  47. | variable |
  48. aNode isImmutable ifTrue: [ ^ self visit: aNode ].
  49. variable := IRVariable new
  50. variable: (AliasVar new name: '$', self nextAlias);
  51. yourself.
  52. self sequence add: (IRAssignment new
  53. add: variable;
  54. add: (self visit: aNode);
  55. yourself).
  56. self method internalVariables add: variable.
  57. ^ variable
  58. !
  59. aliasTemporally: aCollection
  60. "https://github.com/NicolasPetton/amber/issues/296
  61. If a node is aliased, all preceding ones are aliased as well.
  62. The tree is iterated twice. First we get the aliasing dependency,
  63. then the aliasing itself is done"
  64. | threshold result |
  65. threshold := 0.
  66. aCollection withIndexDo: [ :each :i |
  67. each subtreeNeedsAliasing
  68. ifTrue: [ threshold := i ] ].
  69. result := OrderedCollection new.
  70. aCollection withIndexDo: [ :each :i |
  71. result add: (i <= threshold
  72. ifTrue: [ self alias: each ]
  73. ifFalse: [ self visit: each ]) ].
  74. ^ result
  75. !
  76. visitAssignmentNode: aNode
  77. | left right assignment |
  78. right := self visit: aNode right.
  79. left := self visit: aNode left.
  80. self sequence add: (IRAssignment new
  81. add: left;
  82. add: right;
  83. yourself).
  84. ^ left
  85. !
  86. visitBlockNode: aNode
  87. | closure |
  88. closure := IRClosure new
  89. arguments: aNode parameters;
  90. scope: aNode scope;
  91. yourself.
  92. aNode scope temps do: [ :each |
  93. closure add: (IRTempDeclaration new
  94. name: each name;
  95. scope: aNode scope;
  96. yourself) ].
  97. aNode nodes do: [ :each | closure add: (self visit: each) ].
  98. ^ closure
  99. !
  100. visitBlockSequenceNode: aNode
  101. ^ self
  102. withSequence: IRBlockSequence new
  103. do: [
  104. aNode nodes ifNotEmpty: [
  105. aNode nodes allButLast do: [ :each |
  106. self sequence add: (self visitOrAlias: each) ].
  107. aNode nodes last isReturnNode
  108. ifFalse: [ self sequence add: (IRBlockReturn new add: (self visitOrAlias: aNode nodes last); yourself) ]
  109. ifTrue: [ self sequence add: (self visitOrAlias: aNode nodes last) ] ]]
  110. !
  111. visitCascadeNode: aNode
  112. | alias receiver |
  113. aNode receiver isImmutable
  114. ifTrue: [ receiver := aNode receiver ]
  115. ifFalse: [
  116. alias := self alias: aNode receiver.
  117. receiver := VariableNode new binding: alias variable ].
  118. aNode nodes do: [ :each |
  119. each receiver: receiver ].
  120. aNode nodes allButLast do: [ :each |
  121. self sequence add: (self visit: each) ].
  122. ^ self alias: aNode nodes last
  123. !
  124. visitDynamicArrayNode: aNode
  125. | array |
  126. array := IRDynamicArray new.
  127. (self aliasTemporally: aNode nodes) do: [ :each | array add: each ].
  128. ^ array
  129. !
  130. visitDynamicDictionaryNode: aNode
  131. | dictionary |
  132. dictionary := IRDynamicDictionary new.
  133. (self aliasTemporally: aNode nodes) do: [ :each | dictionary add: each ].
  134. ^ dictionary
  135. !
  136. visitJSStatementNode: aNode
  137. ^ IRVerbatim new
  138. source: aNode source crlfSanitized;
  139. yourself
  140. !
  141. visitMethodNode: aNode
  142. self method: (IRMethod new
  143. source: self source crlfSanitized;
  144. theClass: self theClass;
  145. arguments: aNode arguments;
  146. selector: aNode selector;
  147. sendIndexes: aNode sendIndexes;
  148. superSends: aNode superSends;
  149. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  150. classReferences: aNode classReferences;
  151. scope: aNode scope;
  152. yourself).
  153. aNode scope temps do: [ :each |
  154. self method add: (IRTempDeclaration new
  155. name: each name;
  156. scope: aNode scope;
  157. yourself) ].
  158. aNode nodes do: [ :each | self method add: (self visit: each) ].
  159. aNode scope hasLocalReturn ifFalse: [
  160. (self method add: IRReturn new) add: (IRVariable new
  161. variable: (aNode scope pseudoVars at: 'self');
  162. yourself) ].
  163. ^ self method
  164. !
  165. visitOrAlias: aNode
  166. ^ aNode shouldBeAliased
  167. ifTrue: [ self alias: aNode ]
  168. ifFalse: [ self visit: aNode ]
  169. !
  170. visitReturnNode: aNode
  171. | return |
  172. return := aNode nonLocalReturn
  173. ifTrue: [ IRNonLocalReturn new ]
  174. ifFalse: [ IRReturn new ].
  175. return scope: aNode scope.
  176. aNode nodes do: [ :each |
  177. return add: (self alias: each) ].
  178. ^ return
  179. !
  180. visitSendNode: aNode
  181. | send all receiver arguments |
  182. send := IRSend new.
  183. send
  184. selector: aNode selector;
  185. index: aNode index.
  186. aNode superSend ifTrue: [ send classSend: self theClass superclass ].
  187. all := self aliasTemporally: { aNode receiver }, aNode arguments.
  188. receiver := all first.
  189. arguments := all allButFirst.
  190. send add: receiver.
  191. arguments do: [ :each | send add: each ].
  192. ^ send
  193. !
  194. visitSequenceNode: aNode
  195. ^ self
  196. withSequence: IRSequence new
  197. do: [
  198. aNode nodes do: [ :each | | instruction |
  199. instruction := self visitOrAlias: each.
  200. instruction isVariable ifFalse: [
  201. self sequence add: instruction ] ]]
  202. !
  203. visitValueNode: aNode
  204. ^ IRValue new
  205. value: aNode value;
  206. yourself
  207. !
  208. visitVariableNode: aNode
  209. ^ IRVariable new
  210. variable: aNode binding;
  211. yourself
  212. ! !
  213. Object subclass: #IRInstruction
  214. instanceVariableNames: 'parent instructions'
  215. package: 'Compiler-IR'!
  216. !IRInstruction commentStamp!
  217. I am the abstract root class of the IR (intermediate representation) instructions class hierarchy.
  218. The IR graph is used to emit JavaScript code using a JSStream.!
  219. !IRInstruction methodsFor: 'accessing'!
  220. instructions
  221. ^ instructions ifNil: [ instructions := OrderedCollection new ]
  222. !
  223. method
  224. ^ self parent method
  225. !
  226. parent
  227. ^ parent
  228. !
  229. parent: anIRInstruction
  230. parent := anIRInstruction
  231. !
  232. scope
  233. ^ self parent ifNotNil: [ :node |
  234. node scope ]
  235. ! !
  236. !IRInstruction methodsFor: 'building'!
  237. add: anObject
  238. anObject parent: self.
  239. ^ self instructions add: anObject
  240. !
  241. remove
  242. self parent remove: self
  243. !
  244. remove: anIRInstruction
  245. self instructions remove: anIRInstruction
  246. !
  247. replace: anIRInstruction with: anotherIRInstruction
  248. anotherIRInstruction parent: self.
  249. self instructions
  250. at: (self instructions indexOf: anIRInstruction)
  251. put: anotherIRInstruction
  252. !
  253. replaceWith: anIRInstruction
  254. self parent replace: self with: anIRInstruction
  255. ! !
  256. !IRInstruction methodsFor: 'testing'!
  257. canBeAssigned
  258. ^ true
  259. !
  260. isClosure
  261. ^ false
  262. !
  263. isInlined
  264. ^ false
  265. !
  266. isLocalReturn
  267. ^ false
  268. !
  269. isMethod
  270. ^ false
  271. !
  272. isReturn
  273. ^ false
  274. !
  275. isSend
  276. ^ false
  277. !
  278. isSequence
  279. ^ false
  280. !
  281. isTempDeclaration
  282. ^ false
  283. !
  284. isVariable
  285. ^ false
  286. !
  287. needsBoxingAsReceiver
  288. ^ true
  289. ! !
  290. !IRInstruction methodsFor: 'visiting'!
  291. accept: aVisitor
  292. ^ aVisitor visitIRInstruction: self
  293. ! !
  294. !IRInstruction class methodsFor: 'instance creation'!
  295. on: aBuilder
  296. ^ self new
  297. builder: aBuilder;
  298. yourself
  299. ! !
  300. IRInstruction subclass: #IRAssignment
  301. instanceVariableNames: ''
  302. package: 'Compiler-IR'!
  303. !IRAssignment methodsFor: 'visiting'!
  304. accept: aVisitor
  305. ^ aVisitor visitIRAssignment: self
  306. ! !
  307. IRInstruction subclass: #IRDynamicArray
  308. instanceVariableNames: ''
  309. package: 'Compiler-IR'!
  310. !IRDynamicArray methodsFor: 'visiting'!
  311. accept: aVisitor
  312. ^ aVisitor visitIRDynamicArray: self
  313. ! !
  314. IRInstruction subclass: #IRDynamicDictionary
  315. instanceVariableNames: ''
  316. package: 'Compiler-IR'!
  317. !IRDynamicDictionary methodsFor: 'visiting'!
  318. accept: aVisitor
  319. ^ aVisitor visitIRDynamicDictionary: self
  320. ! !
  321. IRInstruction subclass: #IRScopedInstruction
  322. instanceVariableNames: 'scope'
  323. package: 'Compiler-IR'!
  324. !IRScopedInstruction methodsFor: 'accessing'!
  325. scope
  326. ^ scope
  327. !
  328. scope: aScope
  329. scope := aScope
  330. ! !
  331. IRScopedInstruction subclass: #IRClosureInstruction
  332. instanceVariableNames: 'arguments'
  333. package: 'Compiler-IR'!
  334. !IRClosureInstruction methodsFor: 'accessing'!
  335. arguments
  336. ^ arguments ifNil: [ #() ]
  337. !
  338. arguments: aCollection
  339. arguments := aCollection
  340. !
  341. locals
  342. ^ self arguments copy
  343. addAll: (self tempDeclarations collect: [ :each | each name ]);
  344. yourself
  345. !
  346. scope: aScope
  347. super scope: aScope.
  348. aScope instruction: self
  349. !
  350. tempDeclarations
  351. ^ self instructions select: [ :each |
  352. each isTempDeclaration ]
  353. ! !
  354. IRClosureInstruction subclass: #IRClosure
  355. instanceVariableNames: ''
  356. package: 'Compiler-IR'!
  357. !IRClosure methodsFor: 'accessing'!
  358. sequence
  359. ^ self instructions last
  360. ! !
  361. !IRClosure methodsFor: 'testing'!
  362. isClosure
  363. ^ true
  364. ! !
  365. !IRClosure methodsFor: 'visiting'!
  366. accept: aVisitor
  367. ^ aVisitor visitIRClosure: self
  368. ! !
  369. IRClosureInstruction subclass: #IRMethod
  370. instanceVariableNames: 'theClass source selector classReferences sendIndexes superSends requiresSmalltalkContext internalVariables'
  371. package: 'Compiler-IR'!
  372. !IRMethod commentStamp!
  373. I am a method instruction!
  374. !IRMethod methodsFor: 'accessing'!
  375. classReferences
  376. ^ classReferences
  377. !
  378. classReferences: aCollection
  379. classReferences := aCollection
  380. !
  381. internalVariables
  382. ^ internalVariables ifNil: [ internalVariables := Set new ]
  383. !
  384. isMethod
  385. ^ true
  386. !
  387. messageSends
  388. ^ self sendIndexes keys
  389. !
  390. method
  391. ^ self
  392. !
  393. requiresSmalltalkContext
  394. ^ requiresSmalltalkContext ifNil: [ false ]
  395. !
  396. requiresSmalltalkContext: anObject
  397. requiresSmalltalkContext := anObject
  398. !
  399. selector
  400. ^ selector
  401. !
  402. selector: aString
  403. selector := aString
  404. !
  405. sendIndexes
  406. ^ sendIndexes
  407. !
  408. sendIndexes: aDictionary
  409. sendIndexes := aDictionary
  410. !
  411. source
  412. ^ source
  413. !
  414. source: aString
  415. source := aString
  416. !
  417. superSends
  418. ^ superSends
  419. !
  420. superSends: aCollection
  421. superSends := aCollection
  422. !
  423. theClass
  424. ^ theClass
  425. !
  426. theClass: aClass
  427. theClass := aClass
  428. ! !
  429. !IRMethod methodsFor: 'visiting'!
  430. accept: aVisitor
  431. ^ aVisitor visitIRMethod: self
  432. ! !
  433. IRScopedInstruction subclass: #IRReturn
  434. instanceVariableNames: ''
  435. package: 'Compiler-IR'!
  436. !IRReturn commentStamp!
  437. I am a local return instruction.!
  438. !IRReturn methodsFor: 'accessing'!
  439. scope
  440. ^ scope ifNil: [ self parent scope ]
  441. ! !
  442. !IRReturn methodsFor: 'testing'!
  443. canBeAssigned
  444. ^ false
  445. !
  446. isBlockReturn
  447. ^ false
  448. !
  449. isLocalReturn
  450. ^ true
  451. !
  452. isNonLocalReturn
  453. ^ self isLocalReturn not
  454. !
  455. isReturn
  456. ^ true
  457. ! !
  458. !IRReturn methodsFor: 'visiting'!
  459. accept: aVisitor
  460. ^ aVisitor visitIRReturn: self
  461. ! !
  462. IRReturn subclass: #IRBlockReturn
  463. instanceVariableNames: ''
  464. package: 'Compiler-IR'!
  465. !IRBlockReturn commentStamp!
  466. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  467. !IRBlockReturn methodsFor: 'testing'!
  468. isBlockReturn
  469. ^ true
  470. ! !
  471. !IRBlockReturn methodsFor: 'visiting'!
  472. accept: aVisitor
  473. ^ aVisitor visitIRBlockReturn: self
  474. ! !
  475. IRReturn subclass: #IRNonLocalReturn
  476. instanceVariableNames: ''
  477. package: 'Compiler-IR'!
  478. !IRNonLocalReturn commentStamp!
  479. I am a non local return instruction.
  480. Non local returns are handled using a try/catch JavaScript statement.
  481. See `IRNonLocalReturnHandling` class.!
  482. !IRNonLocalReturn methodsFor: 'testing'!
  483. isLocalReturn
  484. ^ false
  485. ! !
  486. !IRNonLocalReturn methodsFor: 'visiting'!
  487. accept: aVisitor
  488. ^ aVisitor visitIRNonLocalReturn: self
  489. ! !
  490. IRScopedInstruction subclass: #IRTempDeclaration
  491. instanceVariableNames: 'name'
  492. package: 'Compiler-IR'!
  493. !IRTempDeclaration methodsFor: 'accessing'!
  494. name
  495. ^ name
  496. !
  497. name: aString
  498. name := aString
  499. ! !
  500. !IRTempDeclaration methodsFor: 'testing'!
  501. isTempDeclaration
  502. ^ true
  503. ! !
  504. !IRTempDeclaration methodsFor: 'visiting'!
  505. accept: aVisitor
  506. ^ aVisitor visitIRTempDeclaration: self
  507. ! !
  508. IRInstruction subclass: #IRSend
  509. instanceVariableNames: 'selector classSend index'
  510. package: 'Compiler-IR'!
  511. !IRSend commentStamp!
  512. I am a message send instruction.!
  513. !IRSend methodsFor: 'accessing'!
  514. classSend
  515. ^ classSend
  516. !
  517. classSend: aClass
  518. classSend := aClass
  519. !
  520. index
  521. ^ index
  522. !
  523. index: anInteger
  524. index := anInteger
  525. !
  526. selector
  527. ^ selector
  528. !
  529. selector: aString
  530. selector := aString
  531. ! !
  532. !IRSend methodsFor: 'testing'!
  533. isSend
  534. ^ true
  535. ! !
  536. !IRSend methodsFor: 'visiting'!
  537. accept: aVisitor
  538. ^ aVisitor visitIRSend: self
  539. ! !
  540. IRInstruction subclass: #IRSequence
  541. instanceVariableNames: ''
  542. package: 'Compiler-IR'!
  543. !IRSequence methodsFor: 'testing'!
  544. isSequence
  545. ^ true
  546. ! !
  547. !IRSequence methodsFor: 'visiting'!
  548. accept: aVisitor
  549. ^ aVisitor visitIRSequence: self
  550. ! !
  551. IRSequence subclass: #IRBlockSequence
  552. instanceVariableNames: ''
  553. package: 'Compiler-IR'!
  554. !IRBlockSequence methodsFor: 'visiting'!
  555. accept: aVisitor
  556. ^ aVisitor visitIRBlockSequence: self
  557. ! !
  558. IRInstruction subclass: #IRValue
  559. instanceVariableNames: 'value'
  560. package: 'Compiler-IR'!
  561. !IRValue commentStamp!
  562. I am the simplest possible instruction. I represent a value.!
  563. !IRValue methodsFor: 'accessing'!
  564. value
  565. ^ value
  566. !
  567. value: aString
  568. value := aString
  569. ! !
  570. !IRValue methodsFor: 'testing'!
  571. needsBoxingAsReceiver
  572. ^ false
  573. ! !
  574. !IRValue methodsFor: 'visiting'!
  575. accept: aVisitor
  576. ^ aVisitor visitIRValue: self
  577. ! !
  578. IRInstruction subclass: #IRVariable
  579. instanceVariableNames: 'variable'
  580. package: 'Compiler-IR'!
  581. !IRVariable commentStamp!
  582. I am a variable instruction.!
  583. !IRVariable methodsFor: 'accessing'!
  584. variable
  585. ^ variable
  586. !
  587. variable: aScopeVariable
  588. variable := aScopeVariable
  589. ! !
  590. !IRVariable methodsFor: 'testing'!
  591. isVariable
  592. ^ true
  593. !
  594. needsBoxingAsReceiver
  595. ^ self variable isPseudoVar not
  596. ! !
  597. !IRVariable methodsFor: 'visiting'!
  598. accept: aVisitor
  599. ^ aVisitor visitIRVariable: self
  600. ! !
  601. IRInstruction subclass: #IRVerbatim
  602. instanceVariableNames: 'source'
  603. package: 'Compiler-IR'!
  604. !IRVerbatim methodsFor: 'accessing'!
  605. source
  606. ^ source
  607. !
  608. source: aString
  609. source := aString
  610. ! !
  611. !IRVerbatim methodsFor: 'visiting'!
  612. accept: aVisitor
  613. ^ aVisitor visitIRVerbatim: self
  614. ! !
  615. Object subclass: #IRVisitor
  616. instanceVariableNames: ''
  617. package: 'Compiler-IR'!
  618. !IRVisitor methodsFor: 'visiting'!
  619. visit: anIRInstruction
  620. ^ anIRInstruction accept: self
  621. !
  622. visitIRAssignment: anIRAssignment
  623. ^ self visitIRInstruction: anIRAssignment
  624. !
  625. visitIRBlockReturn: anIRBlockReturn
  626. ^ self visitIRReturn: anIRBlockReturn
  627. !
  628. visitIRBlockSequence: anIRBlockSequence
  629. ^ self visitIRSequence: anIRBlockSequence
  630. !
  631. visitIRClosure: anIRClosure
  632. ^ self visitIRInstruction: anIRClosure
  633. !
  634. visitIRDynamicArray: anIRDynamicArray
  635. ^ self visitIRInstruction: anIRDynamicArray
  636. !
  637. visitIRDynamicDictionary: anIRDynamicDictionary
  638. ^ self visitIRInstruction: anIRDynamicDictionary
  639. !
  640. visitIRInlinedClosure: anIRInlinedClosure
  641. ^ self visitIRClosure: anIRInlinedClosure
  642. !
  643. visitIRInlinedSequence: anIRInlinedSequence
  644. ^ self visitIRSequence: anIRInlinedSequence
  645. !
  646. visitIRInstruction: anIRInstruction
  647. anIRInstruction instructions do: [ :each | self visit: each ].
  648. ^ anIRInstruction
  649. !
  650. visitIRMethod: anIRMethod
  651. ^ self visitIRInstruction: anIRMethod
  652. !
  653. visitIRNonLocalReturn: anIRNonLocalReturn
  654. ^ self visitIRInstruction: anIRNonLocalReturn
  655. !
  656. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  657. ^ self visitIRInstruction: anIRNonLocalReturnHandling
  658. !
  659. visitIRReturn: anIRReturn
  660. ^ self visitIRInstruction: anIRReturn
  661. !
  662. visitIRSend: anIRSend
  663. ^ self visitIRInstruction: anIRSend
  664. !
  665. visitIRSequence: anIRSequence
  666. ^ self visitIRInstruction: anIRSequence
  667. !
  668. visitIRTempDeclaration: anIRTempDeclaration
  669. ^ self visitIRInstruction: anIRTempDeclaration
  670. !
  671. visitIRValue: anIRValue
  672. ^ self visitIRInstruction: anIRValue
  673. !
  674. visitIRVariable: anIRVariable
  675. ^ self visitIRInstruction: anIRVariable
  676. !
  677. visitIRVerbatim: anIRVerbatim
  678. ^ self visitIRInstruction: anIRVerbatim
  679. ! !
  680. IRVisitor subclass: #IRJSTranslator
  681. instanceVariableNames: 'stream currentClass'
  682. package: 'Compiler-IR'!
  683. !IRJSTranslator methodsFor: 'accessing'!
  684. contents
  685. ^ self stream contents
  686. !
  687. currentClass
  688. ^ currentClass
  689. !
  690. currentClass: aClass
  691. currentClass := aClass
  692. !
  693. stream
  694. ^ stream
  695. !
  696. stream: aStream
  697. stream := aStream
  698. ! !
  699. !IRJSTranslator methodsFor: 'initialization'!
  700. initialize
  701. super initialize.
  702. stream := JSStream new.
  703. ! !
  704. !IRJSTranslator methodsFor: 'visiting'!
  705. visitIRAssignment: anIRAssignment
  706. self visit: anIRAssignment instructions first.
  707. self stream nextPutAssignment.
  708. self visit: anIRAssignment instructions last.
  709. !
  710. visitIRClosure: anIRClosure
  711. self stream
  712. nextPutClosureWith: [
  713. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  714. each name asVariableName ]).
  715. self stream
  716. nextPutBlockContextFor: anIRClosure
  717. during: [ super visitIRClosure: anIRClosure ] ]
  718. arguments: anIRClosure arguments
  719. !
  720. visitIRDynamicArray: anIRDynamicArray
  721. self stream nextPutAll: '['.
  722. anIRDynamicArray instructions
  723. do: [ :each | self visit: each ]
  724. separatedBy: [ self stream nextPutAll: ',' ].
  725. stream nextPutAll: ']'
  726. !
  727. visitIRDynamicDictionary: anIRDynamicDictionary
  728. self stream nextPutAll: 'globals.HashedCollection._from_(['.
  729. anIRDynamicDictionary instructions
  730. do: [ :each | self visit: each ]
  731. separatedBy: [ self stream nextPutAll: ',' ].
  732. self stream nextPutAll: '])'
  733. !
  734. visitIRMethod: anIRMethod
  735. self stream
  736. nextPutMethodDeclaration: anIRMethod
  737. with: [ self stream
  738. nextPutFunctionWith: [
  739. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  740. each name asVariableName ]).
  741. anIRMethod classReferences do: [ :each | self stream nextPutClassRefFunction: each ].
  742. self stream nextPutContextFor: anIRMethod during: [
  743. anIRMethod internalVariables notEmpty ifTrue: [
  744. self stream nextPutVars: (anIRMethod internalVariables asArray collect: [ :each |
  745. each variable alias ]) ].
  746. anIRMethod scope hasNonLocalReturn
  747. ifTrue: [
  748. self stream nextPutNonLocalReturnHandlingWith: [
  749. super visitIRMethod: anIRMethod ] ]
  750. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  751. arguments: anIRMethod arguments ]
  752. !
  753. visitIRNonLocalReturn: anIRNonLocalReturn
  754. self stream nextPutNonLocalReturnWith: [
  755. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  756. !
  757. visitIRReturn: anIRReturn
  758. self stream nextPutReturnWith: [
  759. super visitIRReturn: anIRReturn ]
  760. !
  761. visitIRSend: anIRSend
  762. | sends |
  763. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  764. anIRSend classSend
  765. ifNil: [ self visitSend: anIRSend ]
  766. ifNotNil: [ self visitSuperSend: anIRSend ].
  767. (sends > 1 and: [ anIRSend index < sends ])
  768. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  769. !
  770. visitIRSequence: anIRSequence
  771. self stream nextPutSequenceWith: [
  772. anIRSequence instructions do: [ :each |
  773. self stream nextPutStatementWith: (self visit: each) ] ]
  774. !
  775. visitIRTempDeclaration: anIRTempDeclaration
  776. "self stream
  777. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  778. lf"
  779. !
  780. visitIRValue: anIRValue
  781. self stream nextPutAll: anIRValue value asJavascript
  782. !
  783. visitIRVariable: anIRVariable
  784. anIRVariable variable name = 'thisContext'
  785. ifTrue: [ self stream nextPutAll: 'smalltalk.getThisContext()' ]
  786. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  787. !
  788. visitIRVerbatim: anIRVerbatim
  789. self stream nextPutStatementWith: [
  790. self stream nextPutAll: anIRVerbatim source ]
  791. !
  792. visitReceiver: anIRInstruction
  793. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  794. self stream nextPutAll: '_st('.
  795. self visit: anIRInstruction.
  796. self stream nextPutAll: ')'
  797. !
  798. visitSend: anIRSend
  799. self visitReceiver: anIRSend instructions first.
  800. self stream nextPutAll: '.', anIRSend selector asSelector, '('.
  801. anIRSend instructions allButFirst
  802. do: [ :each | self visit: each ]
  803. separatedBy: [ self stream nextPutAll: ',' ].
  804. self stream nextPutAll: ')'
  805. !
  806. visitSuperSend: anIRSend
  807. self stream
  808. nextPutAll: self currentClass asJavascript;
  809. nextPutAll: '.superclass.fn.prototype.';
  810. nextPutAll: anIRSend selector asSelector, '.apply(';
  811. nextPutAll: '_st('.
  812. self visit: anIRSend instructions first.
  813. self stream nextPutAll: '), ['.
  814. anIRSend instructions allButFirst
  815. do: [ :each | self visit: each ]
  816. separatedBy: [ self stream nextPutAll: ',' ].
  817. self stream nextPutAll: '])'
  818. ! !
  819. Object subclass: #JSStream
  820. instanceVariableNames: 'stream'
  821. package: 'Compiler-IR'!
  822. !JSStream methodsFor: 'accessing'!
  823. contents
  824. ^ stream contents
  825. ! !
  826. !JSStream methodsFor: 'initialization'!
  827. initialize
  828. super initialize.
  829. stream := '' writeStream.
  830. ! !
  831. !JSStream methodsFor: 'streaming'!
  832. lf
  833. stream lf
  834. !
  835. nextPut: aString
  836. stream nextPut: aString
  837. !
  838. nextPutAll: aString
  839. stream nextPutAll: aString
  840. !
  841. nextPutAssignment
  842. stream nextPutAll: '='
  843. !
  844. nextPutBlockContextFor: anIRClosure during: aBlock
  845. self
  846. nextPutAll: 'return smalltalk.withContext(function(', anIRClosure scope alias, ') {'; lf.
  847. aBlock value.
  848. self
  849. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  850. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  851. anIRClosure locals
  852. do: [ :each |
  853. self
  854. nextPutAll: each asVariableName;
  855. nextPutAll: ':';
  856. nextPutAll: each asVariableName ]
  857. separatedBy: [ self nextPutAll: ',' ].
  858. self
  859. nextPutAll: '},';
  860. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')})'
  861. !
  862. nextPutClassRefFunction: aString
  863. "Creates an inner function $aString into method and called as `$Foo()`whenever the global is accessed.
  864. This ensures that undefined global access will answer `nil`"
  865. stream
  866. nextPutAll: 'function $';
  867. nextPutAll: aString;
  868. nextPutAll: '(){return globals.';
  869. nextPutAll: aString;
  870. nextPutAll: '||(typeof ';
  871. nextPutAll: aString;
  872. nextPutAll: '=="undefined"?nil:';
  873. nextPutAll: aString;
  874. nextPutAll: ')}';
  875. lf
  876. !
  877. nextPutClosureWith: aBlock arguments: anArray
  878. stream nextPutAll: '(function('.
  879. anArray
  880. do: [ :each | stream nextPutAll: each asVariableName ]
  881. separatedBy: [ stream nextPut: ',' ].
  882. stream nextPutAll: '){'; lf.
  883. aBlock value.
  884. stream nextPutAll: '})'
  885. !
  886. nextPutContextFor: aMethod during: aBlock
  887. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  888. self
  889. nextPutAll: 'return smalltalk.withContext(function(', aMethod scope alias, ') { '; lf.
  890. aBlock value.
  891. self
  892. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  893. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  894. aMethod locals
  895. do: [ :each |
  896. self
  897. nextPutAll: each asVariableName;
  898. nextPutAll: ':';
  899. nextPutAll: each asVariableName ]
  900. separatedBy: [ self nextPutAll: ',' ].
  901. self
  902. nextPutAll: '},';
  903. nextPutAll: aMethod theClass asJavascript;
  904. nextPutAll: ')})'
  905. !
  906. nextPutFunctionWith: aBlock arguments: anArray
  907. stream nextPutAll: 'fn: function('.
  908. anArray
  909. do: [ :each | stream nextPutAll: each asVariableName ]
  910. separatedBy: [ stream nextPut: ',' ].
  911. stream nextPutAll: '){'; lf.
  912. stream nextPutAll: 'var self=this;'; lf.
  913. aBlock value.
  914. stream nextPutAll: '}'
  915. !
  916. nextPutIf: aBlock with: anotherBlock
  917. stream nextPutAll: 'if('.
  918. aBlock value.
  919. stream nextPutAll: '){'; lf.
  920. anotherBlock value.
  921. stream nextPutAll: '}'
  922. !
  923. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  924. stream nextPutAll: 'if('.
  925. aBlock value.
  926. stream nextPutAll: '){'; lf.
  927. ifBlock value.
  928. stream nextPutAll: '} else {'; lf.
  929. elseBlock value.
  930. stream nextPutAll: '}'
  931. !
  932. nextPutMethodDeclaration: aMethod with: aBlock
  933. stream
  934. nextPutAll: 'smalltalk.method({'; lf;
  935. nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
  936. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  937. aBlock value.
  938. stream
  939. nextPutAll: ',', String lf, 'messageSends: ';
  940. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  941. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  942. nextPutAll: 'referencedClasses: ['.
  943. aMethod classReferences
  944. do: [ :each | stream nextPutAll: each asJavascript ]
  945. separatedBy: [ stream nextPutAll: ',' ].
  946. stream
  947. nextPutAll: ']';
  948. nextPutAll: '})'
  949. !
  950. nextPutNonLocalReturnHandlingWith: aBlock
  951. stream
  952. nextPutAll: 'var $early={};'; lf;
  953. nextPutAll: 'try {'; lf.
  954. aBlock value.
  955. stream
  956. nextPutAll: '}'; lf;
  957. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  958. !
  959. nextPutNonLocalReturnWith: aBlock
  960. stream nextPutAll: 'throw $early=['.
  961. aBlock value.
  962. stream nextPutAll: ']'
  963. !
  964. nextPutReturn
  965. stream nextPutAll: 'return '
  966. !
  967. nextPutReturnWith: aBlock
  968. self nextPutReturn.
  969. aBlock value
  970. !
  971. nextPutSendIndexFor: anIRSend
  972. self
  973. nextPutAll: ';'; lf;
  974. nextPutAll: anIRSend scope alias;
  975. nextPutAll: '.sendIdx[';
  976. nextPutAll: anIRSend selector asJavascript;
  977. nextPutAll: ']=';
  978. nextPutAll: anIRSend index asString
  979. !
  980. nextPutSequenceWith: aBlock
  981. "stream
  982. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  983. aBlock value.
  984. "stream
  985. nextPutAll: '};'; lf"
  986. !
  987. nextPutStatementWith: aBlock
  988. aBlock value.
  989. stream nextPutAll: ';'; lf
  990. !
  991. nextPutVars: aCollection
  992. aCollection ifEmpty: [ ^ self ].
  993. stream nextPutAll: 'var '.
  994. aCollection
  995. do: [ :each | stream nextPutAll: each ]
  996. separatedBy: [ stream nextPutAll: ',' ].
  997. stream nextPutAll: ';'; lf
  998. ! !
  999. !BlockClosure methodsFor: '*Compiler-IR'!
  1000. appendToInstruction: anIRInstruction
  1001. anIRInstruction appendBlock: self
  1002. ! !