Compiler-IR.st 26 KB

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