1
0

Compiler-IR.st 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. #compiledSource. #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. compiledSource
  389. ^ compiledSource
  390. !
  391. compiledSource: anObject
  392. compiledSource := anObject
  393. !
  394. internalVariables
  395. ^ internalVariables ifNil: [ internalVariables := Set new ]
  396. !
  397. messageSends
  398. ^ self sendIndexes keys
  399. !
  400. method
  401. ^ self
  402. !
  403. pragmas
  404. ^ pragmas
  405. !
  406. pragmas: aCollection
  407. pragmas := aCollection
  408. !
  409. selector
  410. ^ selector
  411. !
  412. selector: aString
  413. selector := aString
  414. !
  415. sendIndexes
  416. ^ sendIndexes
  417. !
  418. sendIndexes: aDictionary
  419. sendIndexes := aDictionary
  420. !
  421. source
  422. ^ source
  423. !
  424. source: aString
  425. source := aString
  426. !
  427. theClass
  428. ^ theClass
  429. !
  430. theClass: aClass
  431. theClass := aClass
  432. ! !
  433. !IRMethod methodsFor: 'testing'!
  434. isMethod
  435. ^ true
  436. ! !
  437. !IRMethod methodsFor: 'visiting'!
  438. acceptDagVisitor: aVisitor
  439. ^ aVisitor visitIRMethod: self
  440. ! !
  441. IRScopedInstruction subclass: #IRReturn
  442. slots: {}
  443. package: 'Compiler-IR'!
  444. !IRReturn commentStamp!
  445. I am a local return instruction.!
  446. !IRReturn methodsFor: 'accessing'!
  447. expression
  448. ^ self dagChildren single
  449. !
  450. scope
  451. ^ scope ifNil: [ self parent scope ]
  452. ! !
  453. !IRReturn methodsFor: 'testing'!
  454. yieldsValue
  455. ^ false
  456. ! !
  457. !IRReturn methodsFor: 'visiting'!
  458. acceptDagVisitor: aVisitor
  459. ^ aVisitor visitIRReturn: self
  460. ! !
  461. IRReturn subclass: #IRBlockReturn
  462. slots: {}
  463. package: 'Compiler-IR'!
  464. !IRBlockReturn commentStamp!
  465. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  466. !IRBlockReturn methodsFor: 'visiting'!
  467. acceptDagVisitor: aVisitor
  468. ^ aVisitor visitIRBlockReturn: self
  469. ! !
  470. IRReturn subclass: #IRNonLocalReturn
  471. slots: {}
  472. package: 'Compiler-IR'!
  473. !IRNonLocalReturn commentStamp!
  474. I am a non local return instruction.
  475. Non local returns are handled using a try/catch JavaScript statement.
  476. See `IRNonLocalReturnHandling` class.!
  477. !IRNonLocalReturn methodsFor: 'visiting'!
  478. acceptDagVisitor: aVisitor
  479. ^ aVisitor visitIRNonLocalReturn: self
  480. ! !
  481. IRScopedInstruction subclass: #IRTempDeclaration
  482. slots: {#name}
  483. package: 'Compiler-IR'!
  484. !IRTempDeclaration methodsFor: 'accessing'!
  485. name
  486. ^ name
  487. !
  488. name: aString
  489. name := aString
  490. ! !
  491. !IRTempDeclaration methodsFor: 'testing'!
  492. isTempDeclaration
  493. ^ true
  494. ! !
  495. !IRTempDeclaration methodsFor: 'visiting'!
  496. acceptDagVisitor: aVisitor
  497. ^ aVisitor visitIRTempDeclaration: self
  498. ! !
  499. IRInstruction subclass: #IRSend
  500. slots: {#selector. #javaScriptSelector. #index}
  501. package: 'Compiler-IR'!
  502. !IRSend commentStamp!
  503. I am a message send instruction.!
  504. !IRSend methodsFor: 'accessing'!
  505. arguments
  506. ^ self dagChildren allButFirst
  507. !
  508. index
  509. ^ index
  510. !
  511. index: anInteger
  512. index := anInteger
  513. !
  514. javaScriptSelector
  515. ^ javaScriptSelector ifNil: [ javaScriptSelector := self selector asJavaScriptMethodName ]
  516. !
  517. javaScriptSelector: aString
  518. javaScriptSelector := aString
  519. !
  520. receiver
  521. ^ self dagChildren first
  522. !
  523. selector
  524. ^ selector
  525. !
  526. selector: aString
  527. selector := aString
  528. ! !
  529. !IRSend methodsFor: 'testing'!
  530. isSend
  531. ^ true
  532. ! !
  533. !IRSend methodsFor: 'visiting'!
  534. acceptDagVisitor: aVisitor
  535. ^ aVisitor visitIRSend: self
  536. ! !
  537. IRInstruction subclass: #IRSequence
  538. slots: {}
  539. package: 'Compiler-IR'!
  540. !IRSequence methodsFor: 'testing'!
  541. isSequence
  542. ^ true
  543. ! !
  544. !IRSequence methodsFor: 'visiting'!
  545. acceptDagVisitor: aVisitor
  546. ^ aVisitor visitIRSequence: self
  547. ! !
  548. IRSequence subclass: #IRBlockSequence
  549. slots: {}
  550. package: 'Compiler-IR'!
  551. !IRBlockSequence methodsFor: 'visiting'!
  552. acceptDagVisitor: aVisitor
  553. ^ aVisitor visitIRBlockSequence: self
  554. ! !
  555. IRInstruction subclass: #IRValue
  556. slots: {#value}
  557. package: 'Compiler-IR'!
  558. !IRValue commentStamp!
  559. I am the simplest possible instruction. I represent a value.!
  560. !IRValue methodsFor: 'accessing'!
  561. value
  562. ^ value
  563. !
  564. value: aString
  565. value := aString
  566. ! !
  567. !IRValue methodsFor: 'testing'!
  568. needsBoxingAsReceiver
  569. ^ false
  570. ! !
  571. !IRValue methodsFor: 'visiting'!
  572. acceptDagVisitor: aVisitor
  573. ^ aVisitor visitIRValue: self
  574. ! !
  575. IRInstruction subclass: #IRVariable
  576. slots: {#variable}
  577. package: 'Compiler-IR'!
  578. !IRVariable commentStamp!
  579. I am a variable instruction.!
  580. !IRVariable methodsFor: 'accessing'!
  581. variable
  582. ^ variable
  583. !
  584. variable: aScopeVariable
  585. variable := aScopeVariable
  586. ! !
  587. !IRVariable methodsFor: 'testing'!
  588. isSelf
  589. ^ self variable isSelf
  590. !
  591. isSuper
  592. ^ self variable isSuper
  593. !
  594. isVariable
  595. ^ true
  596. !
  597. needsBoxingAsReceiver
  598. ^ self variable isPseudoVar not
  599. ! !
  600. !IRVariable methodsFor: 'visiting'!
  601. acceptDagVisitor: aVisitor
  602. ^ aVisitor visitIRVariable: self
  603. ! !
  604. IRInstruction subclass: #IRVerbatim
  605. slots: {#source}
  606. package: 'Compiler-IR'!
  607. !IRVerbatim methodsFor: 'accessing'!
  608. source
  609. ^ source
  610. !
  611. source: aString
  612. source := aString
  613. ! !
  614. !IRVerbatim methodsFor: 'visiting'!
  615. acceptDagVisitor: aVisitor
  616. ^ aVisitor visitIRVerbatim: self
  617. ! !
  618. ParentFakingPathDagVisitor subclass: #IRVisitor
  619. slots: {}
  620. package: 'Compiler-IR'!
  621. !IRVisitor methodsFor: 'visiting'!
  622. visitDagNode: aNode
  623. ^ self visitDagNodeVariantSimple: aNode
  624. !
  625. visitIRAssignment: anIRAssignment
  626. ^ self visitDagNode: anIRAssignment
  627. !
  628. visitIRBlockReturn: anIRBlockReturn
  629. ^ self visitIRReturn: anIRBlockReturn
  630. !
  631. visitIRBlockSequence: anIRBlockSequence
  632. ^ self visitIRSequence: anIRBlockSequence
  633. !
  634. visitIRClosure: anIRClosure
  635. ^ self visitDagNode: anIRClosure
  636. !
  637. visitIRDynamicArray: anIRDynamicArray
  638. ^ self visitDagNode: anIRDynamicArray
  639. !
  640. visitIRDynamicDictionary: anIRDynamicDictionary
  641. ^ self visitDagNode: anIRDynamicDictionary
  642. !
  643. visitIRInlinedClosure: anIRInlinedClosure
  644. ^ self visitIRClosure: anIRInlinedClosure
  645. !
  646. visitIRInlinedSequence: anIRInlinedSequence
  647. ^ self visitIRSequence: anIRInlinedSequence
  648. !
  649. visitIRMethod: anIRMethod
  650. ^ self visitDagNode: anIRMethod
  651. !
  652. visitIRNonLocalReturn: anIRNonLocalReturn
  653. ^ self visitDagNode: anIRNonLocalReturn
  654. !
  655. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  656. ^ self visitDagNode: anIRNonLocalReturnHandling
  657. !
  658. visitIRReturn: anIRReturn
  659. ^ self visitDagNode: anIRReturn
  660. !
  661. visitIRSend: anIRSend
  662. ^ self visitDagNode: anIRSend
  663. !
  664. visitIRSequence: anIRSequence
  665. ^ self visitDagNode: anIRSequence
  666. !
  667. visitIRTempDeclaration: anIRTempDeclaration
  668. ^ self visitDagNode: anIRTempDeclaration
  669. !
  670. visitIRValue: anIRValue
  671. ^ self visitDagNode: anIRValue
  672. !
  673. visitIRVariable: anIRVariable
  674. ^ self visitDagNode: anIRVariable
  675. !
  676. visitIRVerbatim: anIRVerbatim
  677. ^ self visitDagNode: anIRVerbatim
  678. ! !
  679. IRVisitor subclass: #IRJSTranslator
  680. slots: {#stream. #currentClass}
  681. package: 'Compiler-IR'!
  682. !IRJSTranslator methodsFor: 'accessing'!
  683. contents
  684. ^ self stream contents
  685. !
  686. currentClass
  687. ^ currentClass
  688. !
  689. currentClass: aClass
  690. currentClass := aClass
  691. !
  692. stream
  693. ^ stream
  694. !
  695. stream: aStream
  696. stream := aStream
  697. ! !
  698. !IRJSTranslator methodsFor: 'initialization'!
  699. initialize
  700. super initialize.
  701. stream := JSStream new.
  702. ! !
  703. !IRJSTranslator methodsFor: 'visiting'!
  704. visitIRAssignment: anIRAssignment
  705. self stream
  706. nextPutAssignLhs: [self visit: anIRAssignment left]
  707. rhs: [self visit: anIRAssignment right].
  708. !
  709. visitIRClosure: anIRClosure
  710. self stream
  711. nextPutClosureWith: [
  712. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  713. each name asVariableName ]).
  714. self stream
  715. nextPutBlockContextFor: anIRClosure
  716. during: [ super visitIRClosure: anIRClosure ] ]
  717. arguments: anIRClosure arguments
  718. !
  719. visitIRDynamicArray: anIRDynamicArray
  720. self
  721. visitInstructionList: anIRDynamicArray dagChildren
  722. enclosedBetween: '[' and: ']'
  723. !
  724. visitIRDynamicDictionary: anIRDynamicDictionary
  725. self
  726. visitInstructionList: anIRDynamicDictionary dagChildren
  727. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  728. !
  729. visitIRMethod: anIRMethod
  730. self stream
  731. nextPutFunctionWith: [
  732. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  733. each name asVariableName ]).
  734. self stream nextPutContextFor: anIRMethod during: [
  735. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  736. self stream nextPutVars:
  737. (internalVars asSet collect: [ :each | each variable alias ]) ].
  738. anIRMethod scope hasNonLocalReturn
  739. ifTrue: [
  740. self stream nextPutNonLocalReturnHandlingWith: [
  741. super visitIRMethod: anIRMethod ] ]
  742. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  743. arguments: anIRMethod arguments.
  744. ^ anIRMethod compiledSource: self contents; yourself
  745. !
  746. visitIRNonLocalReturn: anIRNonLocalReturn
  747. self stream nextPutNonLocalReturnWith: [
  748. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  749. !
  750. visitIRReturn: anIRReturn
  751. self stream nextPutReturnWith: [
  752. super visitIRReturn: anIRReturn ]
  753. !
  754. visitIRSend: anIRSend
  755. | sends superclass |
  756. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  757. anIRSend receiver isSuper
  758. ifTrue: [ self visitSuperSend: anIRSend ]
  759. ifFalse: [ self visitSend: anIRSend ].
  760. anIRSend index < sends
  761. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  762. !
  763. visitIRSequence: anIRSequence
  764. anIRSequence dagChildren do: [ :each |
  765. self stream nextPutStatementWith: [ self visit: each ] ]
  766. !
  767. visitIRTempDeclaration: anIRTempDeclaration
  768. "self stream
  769. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  770. lf"
  771. !
  772. visitIRValue: anIRValue
  773. self stream nextPutAll: anIRValue value asJavaScriptSource
  774. !
  775. visitIRVariable: anIRVariable
  776. anIRVariable variable name = 'thisContext'
  777. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  778. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  779. !
  780. visitIRVerbatim: anIRVerbatim
  781. self stream nextPutAll: anIRVerbatim source
  782. !
  783. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  784. self stream nextPutAll: aString.
  785. anArray
  786. do: [ :each | self visit: each ]
  787. separatedBy: [ self stream nextPutAll: ',' ].
  788. stream nextPutAll: anotherString
  789. !
  790. visitReceiver: anIRInstruction
  791. | instr |
  792. anIRInstruction isSelf
  793. ifTrue: [ instr := anIRInstruction copy
  794. variable: (anIRInstruction variable copy name: '$self'; yourself);
  795. yourself ]
  796. ifFalse: [ instr := anIRInstruction ].
  797. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  798. self stream nextPutAll: '$recv('.
  799. self visit: instr.
  800. self stream nextPutAll: ')'
  801. !
  802. visitSend: anIRSend
  803. self visitReceiver: anIRSend receiver.
  804. self stream nextPutAll: '.', anIRSend javaScriptSelector.
  805. self
  806. visitInstructionList: anIRSend arguments
  807. enclosedBetween: '(' and: ')'
  808. !
  809. visitSuperSend: anIRSend
  810. self stream nextPutSupercallFor: anIRSend with: [
  811. self stream
  812. nextPutAll: anIRSend receiver variable lookupAsJavaScriptSource, '.';
  813. nextPutAll: anIRSend javaScriptSelector, '.call'.
  814. self
  815. visitInstructionList: {IRVerbatim new source: '$self'; yourself}, anIRSend arguments
  816. enclosedBetween: '(' and: ')' ]
  817. ! !
  818. Object subclass: #JSStream
  819. slots: {#stream. #omitSemicolon}
  820. package: 'Compiler-IR'!
  821. !JSStream methodsFor: 'accessing'!
  822. contents
  823. ^ stream contents
  824. !
  825. omitSemicolon
  826. ^ omitSemicolon
  827. !
  828. omitSemicolon: aBoolean
  829. omitSemicolon := aBoolean
  830. ! !
  831. !JSStream methodsFor: 'initialization'!
  832. initialize
  833. super initialize.
  834. stream := '' writeStream.
  835. ! !
  836. !JSStream methodsFor: 'streaming'!
  837. lf
  838. stream lf
  839. !
  840. nextPut: aString
  841. stream nextPut: aString
  842. !
  843. nextPutAll: aString
  844. stream nextPutAll: aString
  845. !
  846. nextPutAssignLhs: aBlock rhs: anotherBlock
  847. aBlock value.
  848. stream nextPutAll: '='.
  849. anotherBlock value
  850. !
  851. nextPutBlockContextFor: anIRClosure during: aBlock
  852. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  853. self
  854. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  855. lf;
  856. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  857. lf;
  858. nextPutAll: '//>>excludeEnd("ctx");';
  859. lf.
  860. aBlock value.
  861. self
  862. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  863. lf;
  864. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  865. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  866. anIRClosure locals
  867. do: [ :each |
  868. self
  869. nextPutAll: each asVariableName;
  870. nextPutAll: ':';
  871. nextPutAll: each asVariableName ]
  872. separatedBy: [ self nextPutAll: ',' ].
  873. self
  874. nextPutAll: '},';
  875. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  876. lf;
  877. nextPutAll: '//>>excludeEnd("ctx");'
  878. !
  879. nextPutClosureWith: aBlock arguments: anArray
  880. stream nextPutAll: '(function('.
  881. anArray
  882. do: [ :each | stream nextPutAll: each asVariableName ]
  883. separatedBy: [ stream nextPut: ',' ].
  884. stream nextPutAll: '){'; lf.
  885. aBlock value.
  886. stream lf; nextPutAll: '})'
  887. !
  888. nextPutContextFor: aMethod during: aBlock
  889. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  890. self
  891. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  892. lf;
  893. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  894. lf;
  895. nextPutAll: '//>>excludeEnd("ctx");';
  896. lf.
  897. aBlock value.
  898. self
  899. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  900. lf;
  901. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  902. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  903. aMethod locals
  904. do: [ :each |
  905. self
  906. nextPutAll: each asVariableName;
  907. nextPutAll: ':';
  908. nextPutAll: each asVariableName ]
  909. separatedBy: [ self nextPutAll: ',' ].
  910. self
  911. nextPutAll: '})});';
  912. lf;
  913. nextPutAll: '//>>excludeEnd("ctx");'
  914. !
  915. nextPutFunctionWith: aBlock arguments: anArray
  916. stream nextPutAll: 'function ('.
  917. anArray
  918. do: [ :each | stream nextPutAll: each asVariableName ]
  919. separatedBy: [ stream nextPut: ',' ].
  920. stream nextPutAll: '){'; lf.
  921. stream nextPutAll: 'var self=this,$self=this;'; lf.
  922. aBlock value.
  923. stream lf; nextPutAll: '}'
  924. !
  925. nextPutIf: aBlock then: anotherBlock
  926. stream nextPutAll: 'if('.
  927. aBlock value.
  928. stream nextPutAll: '){'; lf.
  929. anotherBlock value.
  930. stream nextPutAll: '}'.
  931. self omitSemicolon: true
  932. !
  933. nextPutIf: aBlock then: ifBlock else: elseBlock
  934. stream nextPutAll: 'if('.
  935. aBlock value.
  936. stream nextPutAll: '){'; lf.
  937. ifBlock value.
  938. stream nextPutAll: '} else {'; lf.
  939. elseBlock value.
  940. stream nextPutAll: '}'.
  941. self omitSemicolon: true
  942. !
  943. nextPutNonLocalReturnHandlingWith: aBlock
  944. stream
  945. nextPutAll: 'var $early={};'; lf;
  946. nextPutAll: 'try {'; lf.
  947. aBlock value.
  948. stream
  949. nextPutAll: '}'; lf;
  950. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  951. !
  952. nextPutNonLocalReturnWith: aBlock
  953. stream nextPutAll: 'throw $early=['.
  954. aBlock value.
  955. stream nextPutAll: ']'
  956. !
  957. nextPutReturnWith: aBlock
  958. stream nextPutAll: 'return '.
  959. aBlock value
  960. !
  961. nextPutSendIndexFor: anIRSend
  962. self
  963. nextPutAll: ';'; lf;
  964. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  965. nextPutAll: anIRSend scope alias;
  966. nextPutAll: '.sendIdx[';
  967. nextPutAll: anIRSend selector asJavaScriptSource;
  968. nextPutAll: ']=';
  969. nextPutAll: anIRSend index asString;
  970. nextPutAll: ';'; lf;
  971. nextPutAll: '//>>excludeEnd("ctx")'
  972. !
  973. nextPutStatementWith: aBlock
  974. self omitSemicolon: false.
  975. aBlock value.
  976. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  977. self omitSemicolon: false.
  978. stream lf
  979. !
  980. nextPutSupercallFor: anIRSend with: aBlock
  981. self
  982. nextPutAll: '('; lf;
  983. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  984. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  985. nextPutAll: '//>>excludeEnd("ctx");'; lf.
  986. aBlock value.
  987. self
  988. nextPutAll: ');'; lf;
  989. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  990. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  991. nextPutAll: '//>>excludeEnd("ctx");'
  992. !
  993. nextPutVars: aCollection
  994. aCollection ifNotEmpty: [
  995. stream nextPutAll: 'var '.
  996. aCollection
  997. do: [ :each | stream nextPutAll: each ]
  998. separatedBy: [ stream nextPutAll: ',' ].
  999. stream nextPutAll: ';'; lf ]
  1000. ! !
  1001. !ASTNode methodsFor: '*Compiler-IR'!
  1002. isReferenced
  1003. "Answer true if the receiver is referenced by other nodes.
  1004. Do not take sequences or assignments into account"
  1005. self parent isSequenceNode ifTrue: [ ^ false ].
  1006. self parent isAssignmentNode ifTrue: [ ^ false ].
  1007. self parent isCascadeNode ifTrue: [ ^ self parent isReferenced ].
  1008. ^ true
  1009. !
  1010. subtreeNeedsAliasing
  1011. ^ self shouldBeAliased or: [
  1012. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1013. ! !
  1014. !AssignmentNode methodsFor: '*Compiler-IR'!
  1015. shouldBeAliased
  1016. ^ super shouldBeAliased or: [ self isReferenced ]
  1017. ! !
  1018. !BlockClosure methodsFor: '*Compiler-IR'!
  1019. appendToInstruction: anIRInstruction
  1020. anIRInstruction appendBlock: self
  1021. ! !
  1022. !BlockNode methodsFor: '*Compiler-IR'!
  1023. subtreeNeedsAliasing
  1024. ^ self shouldBeAliased
  1025. ! !
  1026. !CascadeNode methodsFor: '*Compiler-IR'!
  1027. subtreeNeedsAliasing
  1028. ^ self parent isSequenceNode not
  1029. ! !
  1030. !SendNode methodsFor: '*Compiler-IR'!
  1031. shouldBeAliased
  1032. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  1033. See IRJSVisitor >> visitIRSend:"
  1034. | sends |
  1035. sends := (self method sendIndexes at: self selector) size.
  1036. ^ (super shouldBeAliased or: [
  1037. self isReferenced and: [
  1038. self index < sends or: [
  1039. self superSend ] ] ])
  1040. !
  1041. subtreeNeedsAliasing
  1042. ^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
  1043. ! !