Compiler-IR.st 27 KB

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