Compiler-IR.st 26 KB

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