Compiler-IR.st 24 KB

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