Compiler-IR.st 25 KB

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