Compiler-IR.st 26 KB

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