Compiler-IR.st 26 KB

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