2
0

Compiler-IR.st 23 KB

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