Compiler-IR.st 26 KB

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