Compiler-IR.st 25 KB

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