Compiler-IR.st 25 KB

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