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. all := self aliasTemporally: { aNode receiver }, aNode arguments.
  190. receiver := all first.
  191. arguments := all allButFirst.
  192. send add: receiver.
  193. arguments do: [ :each | send add: each ].
  194. ^ send
  195. !
  196. visitSequenceNode: aNode
  197. ^ self
  198. withSequence: IRSequence new
  199. do: [
  200. aNode nodes do: [ :each | | instruction |
  201. instruction := self visitOrAlias: each.
  202. instruction isVariable ifFalse: [
  203. self sequence add: instruction ] ]]
  204. !
  205. visitValueNode: aNode
  206. ^ IRValue new
  207. value: aNode value;
  208. yourself
  209. !
  210. visitVariableNode: aNode
  211. ^ IRVariable new
  212. variable: aNode binding;
  213. yourself
  214. ! !
  215. Object subclass: #IRInstruction
  216. instanceVariableNames: 'parent instructions'
  217. package: 'Compiler-IR'!
  218. !IRInstruction commentStamp!
  219. I am the abstract root class of the IR (intermediate representation) instructions class hierarchy.
  220. The IR graph is used to emit JavaScript code using a JSStream.!
  221. !IRInstruction methodsFor: 'accessing'!
  222. instructions
  223. ^ instructions ifNil: [ instructions := OrderedCollection new ]
  224. !
  225. method
  226. ^ self parent method
  227. !
  228. parent
  229. ^ parent
  230. !
  231. parent: anIRInstruction
  232. parent := anIRInstruction
  233. !
  234. scope
  235. ^ self parent ifNotNil: [ :node |
  236. node scope ]
  237. ! !
  238. !IRInstruction methodsFor: 'building'!
  239. add: anObject
  240. anObject parent: self.
  241. ^ self instructions add: anObject
  242. !
  243. remove
  244. self parent remove: self
  245. !
  246. remove: anIRInstruction
  247. self instructions remove: anIRInstruction
  248. !
  249. replace: anIRInstruction with: anotherIRInstruction
  250. anotherIRInstruction parent: self.
  251. self instructions
  252. at: (self instructions indexOf: anIRInstruction)
  253. put: anotherIRInstruction
  254. !
  255. replaceWith: anIRInstruction
  256. self parent replace: self with: anIRInstruction
  257. ! !
  258. !IRInstruction methodsFor: 'testing'!
  259. canBeAssigned
  260. ^ true
  261. !
  262. isClosure
  263. ^ false
  264. !
  265. isInlined
  266. ^ false
  267. !
  268. isLocalReturn
  269. ^ false
  270. !
  271. isMethod
  272. ^ false
  273. !
  274. isReturn
  275. ^ false
  276. !
  277. isSend
  278. ^ false
  279. !
  280. isSequence
  281. ^ false
  282. !
  283. isTempDeclaration
  284. ^ false
  285. !
  286. isVariable
  287. ^ false
  288. !
  289. needsBoxingAsReceiver
  290. ^ true
  291. ! !
  292. !IRInstruction methodsFor: 'visiting'!
  293. accept: aVisitor
  294. ^ aVisitor visitIRInstruction: self
  295. ! !
  296. !IRInstruction class methodsFor: 'instance creation'!
  297. on: aBuilder
  298. ^ self new
  299. builder: aBuilder;
  300. yourself
  301. ! !
  302. IRInstruction subclass: #IRAssignment
  303. instanceVariableNames: ''
  304. package: 'Compiler-IR'!
  305. !IRAssignment methodsFor: 'visiting'!
  306. accept: aVisitor
  307. ^ aVisitor visitIRAssignment: self
  308. ! !
  309. IRInstruction subclass: #IRDynamicArray
  310. instanceVariableNames: ''
  311. package: 'Compiler-IR'!
  312. !IRDynamicArray methodsFor: 'visiting'!
  313. accept: aVisitor
  314. ^ aVisitor visitIRDynamicArray: self
  315. ! !
  316. IRInstruction subclass: #IRDynamicDictionary
  317. instanceVariableNames: ''
  318. package: 'Compiler-IR'!
  319. !IRDynamicDictionary methodsFor: 'visiting'!
  320. accept: aVisitor
  321. ^ aVisitor visitIRDynamicDictionary: self
  322. ! !
  323. IRInstruction subclass: #IRScopedInstruction
  324. instanceVariableNames: 'scope'
  325. package: 'Compiler-IR'!
  326. !IRScopedInstruction methodsFor: 'accessing'!
  327. scope
  328. ^ scope
  329. !
  330. scope: aScope
  331. scope := aScope
  332. ! !
  333. IRScopedInstruction subclass: #IRClosureInstruction
  334. instanceVariableNames: 'arguments requiresSmalltalkContext'
  335. package: 'Compiler-IR'!
  336. !IRClosureInstruction methodsFor: 'accessing'!
  337. arguments
  338. ^ arguments ifNil: [ #() ]
  339. !
  340. arguments: aCollection
  341. arguments := aCollection
  342. !
  343. locals
  344. ^ self arguments copy
  345. addAll: (self tempDeclarations collect: [ :each | each name ]);
  346. yourself
  347. !
  348. requiresSmalltalkContext
  349. ^ requiresSmalltalkContext ifNil: [ false ]
  350. !
  351. requiresSmalltalkContext: anObject
  352. requiresSmalltalkContext := anObject
  353. !
  354. scope: aScope
  355. super scope: aScope.
  356. aScope instruction: self
  357. !
  358. tempDeclarations
  359. ^ self instructions select: [ :each |
  360. each isTempDeclaration ]
  361. ! !
  362. IRClosureInstruction subclass: #IRClosure
  363. instanceVariableNames: ''
  364. package: 'Compiler-IR'!
  365. !IRClosure methodsFor: 'accessing'!
  366. sequence
  367. ^ self instructions last
  368. ! !
  369. !IRClosure methodsFor: 'testing'!
  370. isClosure
  371. ^ true
  372. ! !
  373. !IRClosure methodsFor: 'visiting'!
  374. accept: aVisitor
  375. ^ aVisitor visitIRClosure: self
  376. ! !
  377. IRClosureInstruction subclass: #IRMethod
  378. instanceVariableNames: 'theClass source selector classReferences sendIndexes requiresSmalltalkContext internalVariables'
  379. package: 'Compiler-IR'!
  380. !IRMethod commentStamp!
  381. I am a method instruction!
  382. !IRMethod methodsFor: 'accessing'!
  383. classReferences
  384. ^ classReferences
  385. !
  386. classReferences: aCollection
  387. classReferences := aCollection
  388. !
  389. internalVariables
  390. ^ internalVariables ifNil: [ internalVariables := Set new ]
  391. !
  392. isMethod
  393. ^ true
  394. !
  395. messageSends
  396. ^ self sendIndexes keys
  397. !
  398. method
  399. ^ self
  400. !
  401. selector
  402. ^ selector
  403. !
  404. selector: aString
  405. selector := aString
  406. !
  407. sendIndexes
  408. ^ sendIndexes
  409. !
  410. sendIndexes: aDictionary
  411. sendIndexes := aDictionary
  412. !
  413. source
  414. ^ source
  415. !
  416. source: aString
  417. source := aString
  418. !
  419. theClass
  420. ^ theClass
  421. !
  422. theClass: aClass
  423. theClass := aClass
  424. ! !
  425. !IRMethod methodsFor: 'visiting'!
  426. accept: aVisitor
  427. ^ aVisitor visitIRMethod: self
  428. ! !
  429. IRScopedInstruction subclass: #IRReturn
  430. instanceVariableNames: ''
  431. package: 'Compiler-IR'!
  432. !IRReturn commentStamp!
  433. I am a local return instruction.!
  434. !IRReturn methodsFor: 'accessing'!
  435. scope
  436. ^ scope ifNil: [ self parent scope ]
  437. ! !
  438. !IRReturn methodsFor: 'testing'!
  439. canBeAssigned
  440. ^ false
  441. !
  442. isBlockReturn
  443. ^ false
  444. !
  445. isLocalReturn
  446. ^ true
  447. !
  448. isNonLocalReturn
  449. ^ self isLocalReturn not
  450. !
  451. isReturn
  452. ^ true
  453. ! !
  454. !IRReturn methodsFor: 'visiting'!
  455. accept: aVisitor
  456. ^ aVisitor visitIRReturn: self
  457. ! !
  458. IRReturn subclass: #IRBlockReturn
  459. instanceVariableNames: ''
  460. package: 'Compiler-IR'!
  461. !IRBlockReturn commentStamp!
  462. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  463. !IRBlockReturn methodsFor: 'testing'!
  464. isBlockReturn
  465. ^ true
  466. ! !
  467. !IRBlockReturn methodsFor: 'visiting'!
  468. accept: aVisitor
  469. ^ aVisitor visitIRBlockReturn: self
  470. ! !
  471. IRReturn subclass: #IRNonLocalReturn
  472. instanceVariableNames: ''
  473. package: 'Compiler-IR'!
  474. !IRNonLocalReturn commentStamp!
  475. I am a non local return instruction.
  476. Non local returns are handled using a try/catch JavaScript statement.
  477. See `IRNonLocalReturnHandling` class.!
  478. !IRNonLocalReturn methodsFor: 'testing'!
  479. isLocalReturn
  480. ^ false
  481. ! !
  482. !IRNonLocalReturn methodsFor: 'visiting'!
  483. accept: aVisitor
  484. ^ aVisitor visitIRNonLocalReturn: self
  485. ! !
  486. IRScopedInstruction subclass: #IRTempDeclaration
  487. instanceVariableNames: 'name'
  488. package: 'Compiler-IR'!
  489. !IRTempDeclaration methodsFor: 'accessing'!
  490. name
  491. ^ name
  492. !
  493. name: aString
  494. name := aString
  495. ! !
  496. !IRTempDeclaration methodsFor: 'testing'!
  497. isTempDeclaration
  498. ^ true
  499. ! !
  500. !IRTempDeclaration methodsFor: 'visiting'!
  501. accept: aVisitor
  502. ^ aVisitor visitIRTempDeclaration: self
  503. ! !
  504. IRInstruction subclass: #IRSend
  505. instanceVariableNames: 'selector index'
  506. package: 'Compiler-IR'!
  507. !IRSend commentStamp!
  508. I am a message send instruction.!
  509. !IRSend methodsFor: 'accessing'!
  510. index
  511. ^ index
  512. !
  513. index: anInteger
  514. index := anInteger
  515. !
  516. isSuperSend
  517. | receiver |
  518. receiver := self instructions first.
  519. ^ receiver isVariable and: [
  520. receiver variable isPseudoVar and: [
  521. receiver variable name = 'super' ] ]
  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
  719. visitInstructionList: anIRDynamicArray instructions
  720. enclosedBetween: '[' and: ']'
  721. !
  722. visitIRDynamicDictionary: anIRDynamicDictionary
  723. self
  724. visitInstructionList: anIRDynamicDictionary instructions
  725. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  726. !
  727. visitIRMethod: anIRMethod
  728. self stream
  729. nextPutMethodDeclaration: anIRMethod
  730. with: [ self stream
  731. nextPutFunctionWith: [
  732. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  733. each name asVariableName ]).
  734. anIRMethod classReferences do: [ :each | self stream nextPutClassRefFunction: each ].
  735. self stream nextPutContextFor: anIRMethod during: [
  736. anIRMethod internalVariables notEmpty ifTrue: [
  737. self stream nextPutVars: (anIRMethod internalVariables asSet collect: [ :each |
  738. each variable alias ]) ].
  739. anIRMethod scope hasNonLocalReturn
  740. ifTrue: [
  741. self stream nextPutNonLocalReturnHandlingWith: [
  742. super visitIRMethod: anIRMethod ] ]
  743. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  744. arguments: anIRMethod arguments ]
  745. !
  746. visitIRNonLocalReturn: anIRNonLocalReturn
  747. self stream nextPutNonLocalReturnWith: [
  748. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  749. !
  750. visitIRReturn: anIRReturn
  751. self stream nextPutReturnWith: [
  752. super visitIRReturn: anIRReturn ]
  753. !
  754. visitIRSend: anIRSend
  755. | sends superclass |
  756. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  757. anIRSend isSuperSend
  758. ifTrue: [ self visitSuperSend: anIRSend ]
  759. ifFalse: [ self visitSend: anIRSend ].
  760. (sends > 1 and: [ anIRSend index < sends ])
  761. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  762. !
  763. visitIRSequence: anIRSequence
  764. self stream nextPutSequenceWith: [
  765. anIRSequence instructions do: [ :each |
  766. self stream nextPutStatementWith: (self visit: each) ] ]
  767. !
  768. visitIRTempDeclaration: anIRTempDeclaration
  769. "self stream
  770. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  771. lf"
  772. !
  773. visitIRValue: anIRValue
  774. self stream nextPutAll: anIRValue value asJavascript
  775. !
  776. visitIRVariable: anIRVariable
  777. anIRVariable variable name = 'thisContext'
  778. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  779. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  780. !
  781. visitIRVerbatim: anIRVerbatim
  782. self stream nextPutStatementWith: [
  783. self stream nextPutAll: anIRVerbatim source ]
  784. !
  785. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  786. self stream nextPutAll: aString.
  787. anArray
  788. do: [ :each | self visit: each ]
  789. separatedBy: [ self stream nextPutAll: ',' ].
  790. stream nextPutAll: anotherString
  791. !
  792. visitReceiver: anIRInstruction
  793. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  794. self stream nextPutAll: '$recv('.
  795. self visit: anIRInstruction.
  796. self stream nextPutAll: ')'
  797. !
  798. visitSend: anIRSend
  799. self visitReceiver: anIRSend instructions first.
  800. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  801. self
  802. visitInstructionList: anIRSend instructions allButFirst
  803. enclosedBetween: '(' and: ')'
  804. !
  805. visitSuperSend: anIRSend
  806. self stream
  807. nextPutAll: '('; lf;
  808. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  809. nextPutAll: anIRSend scope alias, '.supercall = true, '; lf;
  810. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  811. nextPutAll: '(', self currentClass asJavascript;
  812. nextPutAll: '.superclass||$boot.dnu).fn.prototype.';
  813. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  814. nextPutAll: '$recv(self), '.
  815. self
  816. visitInstructionList: anIRSend instructions allButFirst
  817. enclosedBetween: '[' and: ']'.
  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. ! !