Compiler-IR.st 26 KB

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