Compiler-IR.st 26 KB

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