Compiler-IR.st 26 KB

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