Compiler-IR.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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 stream nextPutAll: '['.
  726. anIRDynamicArray instructions
  727. do: [ :each | self visit: each ]
  728. separatedBy: [ self stream nextPutAll: ',' ].
  729. stream nextPutAll: ']'
  730. !
  731. visitIRDynamicDictionary: anIRDynamicDictionary
  732. self stream nextPutAll: '$globals.HashedCollection._newFromPairs_(['.
  733. anIRDynamicDictionary instructions
  734. do: [ :each | self visit: each ]
  735. separatedBy: [ self stream nextPutAll: ',' ].
  736. self stream nextPutAll: '])'
  737. !
  738. visitIRMethod: anIRMethod
  739. self stream
  740. nextPutMethodDeclaration: anIRMethod
  741. with: [ self stream
  742. nextPutFunctionWith: [
  743. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  744. each name asVariableName ]).
  745. anIRMethod classReferences do: [ :each | self stream nextPutClassRefFunction: each ].
  746. self stream nextPutContextFor: anIRMethod during: [
  747. anIRMethod internalVariables notEmpty ifTrue: [
  748. self stream nextPutVars: (anIRMethod internalVariables asSet collect: [ :each |
  749. each variable alias ]) ].
  750. anIRMethod scope hasNonLocalReturn
  751. ifTrue: [
  752. self stream nextPutNonLocalReturnHandlingWith: [
  753. super visitIRMethod: anIRMethod ] ]
  754. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  755. arguments: anIRMethod arguments ]
  756. !
  757. visitIRNonLocalReturn: anIRNonLocalReturn
  758. self stream nextPutNonLocalReturnWith: [
  759. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  760. !
  761. visitIRReturn: anIRReturn
  762. self stream nextPutReturnWith: [
  763. super visitIRReturn: anIRReturn ]
  764. !
  765. visitIRSend: anIRSend
  766. | sends superclass |
  767. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  768. anIRSend classSend = false
  769. ifTrue: [ self visitSend: anIRSend ]
  770. ifFalse: [ self visitSuperSend: anIRSend ].
  771. (sends > 1 and: [ anIRSend index < sends ])
  772. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  773. !
  774. visitIRSequence: anIRSequence
  775. self stream nextPutSequenceWith: [
  776. anIRSequence instructions do: [ :each |
  777. self stream nextPutStatementWith: (self visit: each) ] ]
  778. !
  779. visitIRTempDeclaration: anIRTempDeclaration
  780. "self stream
  781. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  782. lf"
  783. !
  784. visitIRValue: anIRValue
  785. self stream nextPutAll: anIRValue value asJavascript
  786. !
  787. visitIRVariable: anIRVariable
  788. anIRVariable variable name = 'thisContext'
  789. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  790. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  791. !
  792. visitIRVerbatim: anIRVerbatim
  793. self stream nextPutStatementWith: [
  794. self stream nextPutAll: anIRVerbatim source ]
  795. !
  796. visitReceiver: anIRInstruction
  797. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  798. self stream nextPutAll: '$recv('.
  799. self visit: anIRInstruction.
  800. self stream nextPutAll: ')'
  801. !
  802. visitSend: anIRSend
  803. self visitReceiver: anIRSend instructions first.
  804. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName, '('.
  805. anIRSend instructions allButFirst
  806. do: [ :each | self visit: each ]
  807. separatedBy: [ self stream nextPutAll: ',' ].
  808. self stream nextPutAll: ')'
  809. !
  810. visitSuperSend: anIRSend
  811. self stream
  812. nextPutAll: '('; lf;
  813. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  814. nextPutAll: anIRSend scope alias, '.supercall = true, '; lf;
  815. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  816. nextPutAll: '(', self currentClass asJavascript;
  817. nextPutAll: '.superclass||$boot.dnu).fn.prototype.';
  818. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  819. nextPutAll: '$recv('.
  820. self visit: anIRSend instructions first.
  821. self stream nextPutAll: '), ['.
  822. anIRSend instructions allButFirst
  823. do: [ :each | self visit: each ]
  824. separatedBy: [ self stream nextPutAll: ',' ].
  825. self stream
  826. nextPutAll: ']));'; lf;
  827. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  828. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  829. nextPutAll: '//>>excludeEnd("ctx");'
  830. ! !
  831. Object subclass: #JSStream
  832. instanceVariableNames: 'stream'
  833. package: 'Compiler-IR'!
  834. !JSStream methodsFor: 'accessing'!
  835. contents
  836. ^ stream contents
  837. ! !
  838. !JSStream methodsFor: 'initialization'!
  839. initialize
  840. super initialize.
  841. stream := '' writeStream.
  842. ! !
  843. !JSStream methodsFor: 'streaming'!
  844. lf
  845. stream lf
  846. !
  847. nextPut: aString
  848. stream nextPut: aString
  849. !
  850. nextPutAll: aString
  851. stream nextPutAll: aString
  852. !
  853. nextPutAssignLhs: aBlock rhs: anotherBlock
  854. aBlock value.
  855. stream nextPutAll: '='.
  856. anotherBlock value
  857. !
  858. nextPutBlockContextFor: anIRClosure during: aBlock
  859. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  860. self
  861. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  862. lf;
  863. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  864. lf;
  865. nextPutAll: '//>>excludeEnd("ctx");';
  866. lf.
  867. aBlock value.
  868. self
  869. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  870. lf;
  871. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  872. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  873. anIRClosure locals
  874. do: [ :each |
  875. self
  876. nextPutAll: each asVariableName;
  877. nextPutAll: ':';
  878. nextPutAll: each asVariableName ]
  879. separatedBy: [ self nextPutAll: ',' ].
  880. self
  881. nextPutAll: '},';
  882. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  883. lf;
  884. nextPutAll: '//>>excludeEnd("ctx");'
  885. !
  886. nextPutClassRefFunction: aString
  887. "Creates an inner function $aString into method and called as `$Foo()`whenever the global is accessed.
  888. This ensures that undefined global access will answer `nil`"
  889. stream
  890. nextPutAll: 'function $';
  891. nextPutAll: aString;
  892. nextPutAll: '(){return $globals.';
  893. nextPutAll: aString;
  894. nextPutAll: '||(typeof ';
  895. nextPutAll: aString;
  896. nextPutAll: '=="undefined"?nil:';
  897. nextPutAll: aString;
  898. nextPutAll: ')}';
  899. lf
  900. !
  901. nextPutClosureWith: aBlock arguments: anArray
  902. stream nextPutAll: '(function('.
  903. anArray
  904. do: [ :each | stream nextPutAll: each asVariableName ]
  905. separatedBy: [ stream nextPut: ',' ].
  906. stream nextPutAll: '){'; lf.
  907. aBlock value.
  908. stream lf; nextPutAll: '})'
  909. !
  910. nextPutContextFor: aMethod during: aBlock
  911. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  912. self
  913. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  914. lf;
  915. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  916. lf;
  917. nextPutAll: '//>>excludeEnd("ctx");';
  918. lf.
  919. aBlock value.
  920. self
  921. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  922. lf;
  923. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  924. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  925. aMethod locals
  926. do: [ :each |
  927. self
  928. nextPutAll: each asVariableName;
  929. nextPutAll: ':';
  930. nextPutAll: each asVariableName ]
  931. separatedBy: [ self nextPutAll: ',' ].
  932. self
  933. nextPutAll: '},';
  934. nextPutAll: aMethod theClass asJavascript;
  935. nextPutAll: ')});';
  936. lf;
  937. nextPutAll: '//>>excludeEnd("ctx");'
  938. !
  939. nextPutFunctionWith: aBlock arguments: anArray
  940. stream nextPutAll: 'fn: function('.
  941. anArray
  942. do: [ :each | stream nextPutAll: each asVariableName ]
  943. separatedBy: [ stream nextPut: ',' ].
  944. stream nextPutAll: '){'; lf.
  945. stream nextPutAll: 'var self=this;'; lf.
  946. aBlock value.
  947. stream lf; nextPutAll: '}'
  948. !
  949. nextPutIf: aBlock then: anotherBlock
  950. stream nextPutAll: 'if('.
  951. aBlock value.
  952. stream nextPutAll: '){'; lf.
  953. anotherBlock value.
  954. stream nextPutAll: '}'
  955. !
  956. nextPutIf: aBlock then: ifBlock else: elseBlock
  957. stream nextPutAll: 'if('.
  958. aBlock value.
  959. stream nextPutAll: '){'; lf.
  960. ifBlock value.
  961. stream nextPutAll: '} else {'; lf.
  962. elseBlock value.
  963. stream nextPutAll: '}'
  964. !
  965. nextPutMethodDeclaration: aMethod with: aBlock
  966. stream
  967. nextPutAll: '$core.method({'; lf;
  968. nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
  969. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  970. aBlock value.
  971. stream
  972. nextPutAll: ',', String lf, 'messageSends: ';
  973. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  974. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  975. nextPutAll: 'referencedClasses: ['.
  976. aMethod classReferences
  977. do: [ :each | stream nextPutAll: each asJavascript ]
  978. separatedBy: [ stream nextPutAll: ',' ].
  979. stream
  980. nextPutAll: ']';
  981. nextPutAll: '})'
  982. !
  983. nextPutNonLocalReturnHandlingWith: aBlock
  984. stream
  985. nextPutAll: 'var $early={};'; lf;
  986. nextPutAll: 'try {'; lf.
  987. aBlock value.
  988. stream
  989. nextPutAll: '}'; lf;
  990. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  991. !
  992. nextPutNonLocalReturnWith: aBlock
  993. stream nextPutAll: 'throw $early=['.
  994. aBlock value.
  995. stream nextPutAll: ']'
  996. !
  997. nextPutReturnWith: aBlock
  998. stream nextPutAll: 'return '.
  999. aBlock value
  1000. !
  1001. nextPutSendIndexFor: anIRSend
  1002. self
  1003. nextPutAll: ';'; lf;
  1004. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  1005. nextPutAll: anIRSend scope alias;
  1006. nextPutAll: '.sendIdx[';
  1007. nextPutAll: anIRSend selector asJavascript;
  1008. nextPutAll: ']=';
  1009. nextPutAll: anIRSend index asString;
  1010. nextPutAll: ';'; lf;
  1011. nextPutAll: '//>>excludeEnd("ctx")'
  1012. !
  1013. nextPutSequenceWith: aBlock
  1014. "stream
  1015. nextPutAll: 'switch($core.thisContext.pc){'; lf."
  1016. aBlock value.
  1017. "stream
  1018. nextPutAll: '};'; lf"
  1019. !
  1020. nextPutStatementWith: aBlock
  1021. aBlock value.
  1022. stream nextPutAll: ';'; lf
  1023. !
  1024. nextPutVars: aCollection
  1025. aCollection ifNotEmpty: [
  1026. stream nextPutAll: 'var '.
  1027. aCollection
  1028. do: [ :each | stream nextPutAll: each ]
  1029. separatedBy: [ stream nextPutAll: ',' ].
  1030. stream nextPutAll: ';'; lf ]
  1031. ! !
  1032. !BlockClosure methodsFor: '*Compiler-IR'!
  1033. appendToInstruction: anIRInstruction
  1034. anIRInstruction appendBlock: self
  1035. ! !