Compiler-IR.st 26 KB

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