Compiler-IR.st 26 KB

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