Compiler-IR.st 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. Smalltalk current createPackage: 'Compiler-IR' properties: #{}!
  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. parent
  216. ^ parent
  217. !
  218. parent: anIRInstruction
  219. parent := anIRInstruction
  220. ! !
  221. !IRInstruction methodsFor: 'building'!
  222. add: anObject
  223. anObject parent: self.
  224. ^ self instructions add: anObject
  225. !
  226. remove
  227. self parent remove: self
  228. !
  229. remove: anIRInstruction
  230. self instructions remove: anIRInstruction
  231. !
  232. replace: anIRInstruction with: anotherIRInstruction
  233. anotherIRInstruction parent: self.
  234. self instructions
  235. at: (self instructions indexOf: anIRInstruction)
  236. put: anotherIRInstruction
  237. !
  238. replaceWith: anIRInstruction
  239. self parent replace: self with: anIRInstruction
  240. ! !
  241. !IRInstruction methodsFor: 'testing'!
  242. canBeAssigned
  243. ^ true
  244. !
  245. isClosure
  246. ^ false
  247. !
  248. isInlined
  249. ^ false
  250. !
  251. isLocalReturn
  252. ^ false
  253. !
  254. isReturn
  255. ^ false
  256. !
  257. isSend
  258. ^ false
  259. !
  260. isSequence
  261. ^ false
  262. !
  263. isTempDeclaration
  264. ^ false
  265. !
  266. isVariable
  267. ^ false
  268. ! !
  269. !IRInstruction methodsFor: 'visiting'!
  270. accept: aVisitor
  271. ^ aVisitor visitIRInstruction: self
  272. ! !
  273. !IRInstruction class methodsFor: 'instance creation'!
  274. on: aBuilder
  275. ^ self new
  276. builder: aBuilder;
  277. yourself
  278. ! !
  279. IRInstruction subclass: #IRAssignment
  280. instanceVariableNames: ''
  281. package: 'Compiler-IR'!
  282. !IRAssignment methodsFor: 'visiting'!
  283. accept: aVisitor
  284. ^ aVisitor visitIRAssignment: self
  285. ! !
  286. IRInstruction subclass: #IRDynamicArray
  287. instanceVariableNames: ''
  288. package: 'Compiler-IR'!
  289. !IRDynamicArray methodsFor: 'visiting'!
  290. accept: aVisitor
  291. ^ aVisitor visitIRDynamicArray: self
  292. ! !
  293. IRInstruction subclass: #IRDynamicDictionary
  294. instanceVariableNames: ''
  295. package: 'Compiler-IR'!
  296. !IRDynamicDictionary methodsFor: 'visiting'!
  297. accept: aVisitor
  298. ^ aVisitor visitIRDynamicDictionary: self
  299. ! !
  300. IRInstruction subclass: #IRScopedInstruction
  301. instanceVariableNames: 'scope'
  302. package: 'Compiler-IR'!
  303. !IRScopedInstruction methodsFor: 'accessing'!
  304. scope
  305. ^ scope
  306. !
  307. scope: aScope
  308. scope := aScope
  309. ! !
  310. IRScopedInstruction subclass: #IRClosure
  311. instanceVariableNames: 'arguments'
  312. package: 'Compiler-IR'!
  313. !IRClosure methodsFor: 'accessing'!
  314. arguments
  315. ^ arguments ifNil: [ #() ]
  316. !
  317. arguments: aCollection
  318. arguments := aCollection
  319. !
  320. scope: aScope
  321. super scope: aScope.
  322. aScope instruction: self
  323. !
  324. sequence
  325. ^ self instructions last
  326. ! !
  327. !IRClosure methodsFor: 'testing'!
  328. isClosure
  329. ^ true
  330. ! !
  331. !IRClosure methodsFor: 'visiting'!
  332. accept: aVisitor
  333. ^ aVisitor visitIRClosure: self
  334. ! !
  335. IRScopedInstruction subclass: #IRMethod
  336. instanceVariableNames: 'theClass source selector classReferences messageSends superSends arguments internalVariables'
  337. package: 'Compiler-IR'!
  338. !IRMethod commentStamp!
  339. I am a method instruction!
  340. !IRMethod methodsFor: 'accessing'!
  341. arguments
  342. ^ arguments
  343. !
  344. arguments: aCollection
  345. arguments := aCollection
  346. !
  347. classReferences
  348. ^ classReferences
  349. !
  350. classReferences: aCollection
  351. classReferences := aCollection
  352. !
  353. internalVariables
  354. ^ internalVariables ifNil: [ internalVariables := Set new ]
  355. !
  356. messageSends
  357. ^ messageSends
  358. !
  359. messageSends: aCollection
  360. messageSends := aCollection
  361. !
  362. scope: aScope
  363. super scope: aScope.
  364. aScope instruction: self
  365. !
  366. selector
  367. ^ selector
  368. !
  369. selector: aString
  370. selector := aString
  371. !
  372. source
  373. ^ source
  374. !
  375. source: aString
  376. source := aString
  377. !
  378. superSends
  379. ^ superSends
  380. !
  381. superSends: aCollection
  382. superSends := aCollection
  383. !
  384. theClass
  385. ^ theClass
  386. !
  387. theClass: aClass
  388. theClass := aClass
  389. ! !
  390. !IRMethod methodsFor: 'visiting'!
  391. accept: aVisitor
  392. ^ aVisitor visitIRMethod: self
  393. ! !
  394. IRScopedInstruction subclass: #IRReturn
  395. instanceVariableNames: ''
  396. package: 'Compiler-IR'!
  397. !IRReturn commentStamp!
  398. I am a local return instruction.!
  399. !IRReturn methodsFor: 'testing'!
  400. canBeAssigned
  401. ^ false
  402. !
  403. isBlockReturn
  404. ^ false
  405. !
  406. isLocalReturn
  407. ^ true
  408. !
  409. isNonLocalReturn
  410. ^ self isLocalReturn not
  411. !
  412. isReturn
  413. ^ true
  414. ! !
  415. !IRReturn methodsFor: 'visiting'!
  416. accept: aVisitor
  417. ^ aVisitor visitIRReturn: self
  418. ! !
  419. IRReturn subclass: #IRBlockReturn
  420. instanceVariableNames: ''
  421. package: 'Compiler-IR'!
  422. !IRBlockReturn commentStamp!
  423. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  424. !IRBlockReturn methodsFor: 'testing'!
  425. isBlockReturn
  426. ^ true
  427. ! !
  428. !IRBlockReturn methodsFor: 'visiting'!
  429. accept: aVisitor
  430. ^ aVisitor visitIRBlockReturn: self
  431. ! !
  432. IRReturn subclass: #IRNonLocalReturn
  433. instanceVariableNames: ''
  434. package: 'Compiler-IR'!
  435. !IRNonLocalReturn commentStamp!
  436. I am a non local return instruction.
  437. Non local returns are handled using a try/catch JS statement.
  438. See IRNonLocalReturnHandling class!
  439. !IRNonLocalReturn methodsFor: 'testing'!
  440. isLocalReturn
  441. ^ false
  442. ! !
  443. !IRNonLocalReturn methodsFor: 'visiting'!
  444. accept: aVisitor
  445. ^ aVisitor visitIRNonLocalReturn: self
  446. ! !
  447. IRScopedInstruction subclass: #IRTempDeclaration
  448. instanceVariableNames: 'name'
  449. package: 'Compiler-IR'!
  450. !IRTempDeclaration methodsFor: 'accessing'!
  451. name
  452. ^ name
  453. !
  454. name: aString
  455. name := aString
  456. ! !
  457. !IRTempDeclaration methodsFor: 'visiting'!
  458. accept: aVisitor
  459. ^ aVisitor visitIRTempDeclaration: self
  460. ! !
  461. IRInstruction subclass: #IRSend
  462. instanceVariableNames: 'selector classSend index'
  463. package: 'Compiler-IR'!
  464. !IRSend commentStamp!
  465. I am a message send instruction.!
  466. !IRSend methodsFor: 'accessing'!
  467. classSend
  468. ^ classSend
  469. !
  470. classSend: aClass
  471. classSend := aClass
  472. !
  473. index
  474. ^ index
  475. !
  476. index: anInteger
  477. index := anInteger
  478. !
  479. javascriptSelector
  480. ^ self classSend
  481. ifNil: [ self selector asSelector ]
  482. ifNotNil: [ self selector asSuperSelector ]
  483. !
  484. selector
  485. ^ selector
  486. !
  487. selector: aString
  488. selector := aString
  489. ! !
  490. !IRSend methodsFor: 'testing'!
  491. isSend
  492. ^ true
  493. ! !
  494. !IRSend methodsFor: 'visiting'!
  495. accept: aVisitor
  496. ^ aVisitor visitIRSend: self
  497. ! !
  498. IRInstruction subclass: #IRSequence
  499. instanceVariableNames: ''
  500. package: 'Compiler-IR'!
  501. !IRSequence methodsFor: 'testing'!
  502. isSequence
  503. ^ true
  504. ! !
  505. !IRSequence methodsFor: 'visiting'!
  506. accept: aVisitor
  507. ^ aVisitor visitIRSequence: self
  508. ! !
  509. IRSequence subclass: #IRBlockSequence
  510. instanceVariableNames: ''
  511. package: 'Compiler-IR'!
  512. !IRBlockSequence methodsFor: 'visiting'!
  513. accept: aVisitor
  514. ^ aVisitor visitIRBlockSequence: self
  515. ! !
  516. IRInstruction subclass: #IRValue
  517. instanceVariableNames: 'value'
  518. package: 'Compiler-IR'!
  519. !IRValue commentStamp!
  520. I am the simplest possible instruction. I represent a value.!
  521. !IRValue methodsFor: 'accessing'!
  522. value
  523. ^value
  524. !
  525. value: aString
  526. value := aString
  527. ! !
  528. !IRValue methodsFor: 'visiting'!
  529. accept: aVisitor
  530. ^ aVisitor visitIRValue: self
  531. ! !
  532. IRInstruction subclass: #IRVariable
  533. instanceVariableNames: 'variable'
  534. package: 'Compiler-IR'!
  535. !IRVariable commentStamp!
  536. I am a variable instruction.!
  537. !IRVariable methodsFor: 'accessing'!
  538. variable
  539. ^ variable
  540. !
  541. variable: aScopeVariable
  542. variable := aScopeVariable
  543. ! !
  544. !IRVariable methodsFor: 'testing'!
  545. isVariable
  546. ^ true
  547. ! !
  548. !IRVariable methodsFor: 'visiting'!
  549. accept: aVisitor
  550. ^ aVisitor visitIRVariable: self
  551. ! !
  552. IRInstruction subclass: #IRVerbatim
  553. instanceVariableNames: 'source'
  554. package: 'Compiler-IR'!
  555. !IRVerbatim methodsFor: 'accessing'!
  556. source
  557. ^ source
  558. !
  559. source: aString
  560. source := aString
  561. ! !
  562. !IRVerbatim methodsFor: 'visiting'!
  563. accept: aVisitor
  564. ^ aVisitor visitIRVerbatim: self
  565. ! !
  566. Object subclass: #IRVisitor
  567. instanceVariableNames: ''
  568. package: 'Compiler-IR'!
  569. !IRVisitor methodsFor: 'visiting'!
  570. visit: anIRInstruction
  571. ^ anIRInstruction accept: self
  572. !
  573. visitIRAssignment: anIRAssignment
  574. ^ self visitIRInstruction: anIRAssignment
  575. !
  576. visitIRBlockReturn: anIRBlockReturn
  577. ^ self visitIRReturn: anIRBlockReturn
  578. !
  579. visitIRBlockSequence: anIRBlockSequence
  580. ^ self visitIRSequence: anIRBlockSequence
  581. !
  582. visitIRClosure: anIRClosure
  583. ^ self visitIRInstruction: anIRClosure
  584. !
  585. visitIRDynamicArray: anIRDynamicArray
  586. ^ self visitIRInstruction: anIRDynamicArray
  587. !
  588. visitIRDynamicDictionary: anIRDynamicDictionary
  589. ^ self visitIRInstruction: anIRDynamicDictionary
  590. !
  591. visitIRInlinedClosure: anIRInlinedClosure
  592. ^ self visitIRClosure: anIRInlinedClosure
  593. !
  594. visitIRInlinedSequence: anIRInlinedSequence
  595. ^ self visitIRSequence: anIRInlinedSequence
  596. !
  597. visitIRInstruction: anIRInstruction
  598. anIRInstruction instructions do: [ :each | self visit: each ].
  599. ^ anIRInstruction
  600. !
  601. visitIRMethod: anIRMethod
  602. ^ self visitIRInstruction: anIRMethod
  603. !
  604. visitIRNonLocalReturn: anIRNonLocalReturn
  605. ^ self visitIRInstruction: anIRNonLocalReturn
  606. !
  607. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  608. ^ self visitIRInstruction: anIRNonLocalReturnHandling
  609. !
  610. visitIRReturn: anIRReturn
  611. ^ self visitIRInstruction: anIRReturn
  612. !
  613. visitIRSend: anIRSend
  614. ^ self visitIRInstruction: anIRSend
  615. !
  616. visitIRSequence: anIRSequence
  617. ^ self visitIRInstruction: anIRSequence
  618. !
  619. visitIRTempDeclaration: anIRTempDeclaration
  620. ^ self visitIRInstruction: anIRTempDeclaration
  621. !
  622. visitIRValue: anIRValue
  623. ^ self visitIRInstruction: anIRValue
  624. !
  625. visitIRVariable: anIRVariable
  626. ^ self visitIRInstruction: anIRVariable
  627. !
  628. visitIRVerbatim: anIRVerbatim
  629. ^ self visitIRInstruction: anIRVerbatim
  630. ! !
  631. IRVisitor subclass: #IRJSTranslator
  632. instanceVariableNames: 'stream'
  633. package: 'Compiler-IR'!
  634. !IRJSTranslator methodsFor: 'accessing'!
  635. contents
  636. ^ self stream contents
  637. !
  638. stream
  639. ^ stream
  640. !
  641. stream: aStream
  642. stream := aStream
  643. ! !
  644. !IRJSTranslator methodsFor: 'initialization'!
  645. initialize
  646. super initialize.
  647. stream := JSStream new.
  648. ! !
  649. !IRJSTranslator methodsFor: 'visiting'!
  650. visitIRAssignment: anIRAssignment
  651. self visit: anIRAssignment instructions first.
  652. self stream nextPutAssignment.
  653. self visit: anIRAssignment instructions last.
  654. !
  655. visitIRClosure: anIRClosure
  656. self stream
  657. nextPutClosureWith: [
  658. self stream
  659. nextPutBlockContextFor: anIRClosure
  660. during: [ super visitIRClosure: anIRClosure ] ]
  661. arguments: anIRClosure arguments
  662. !
  663. visitIRDynamicArray: anIRDynamicArray
  664. self stream nextPutAll: '['.
  665. anIRDynamicArray instructions
  666. do: [ :each | self visit: each ]
  667. separatedBy: [ self stream nextPutAll: ',' ].
  668. stream nextPutAll: ']'
  669. !
  670. visitIRDynamicDictionary: anIRDynamicDictionary
  671. self stream nextPutAll: 'smalltalk.HashedCollection._fromPairs_(['.
  672. anIRDynamicDictionary instructions
  673. do: [ :each | self visit: each ]
  674. separatedBy: [self stream nextPutAll: ',' ].
  675. self stream nextPutAll: '])'
  676. !
  677. visitIRMethod: anIRMethod
  678. self stream
  679. nextPutMethodDeclaration: anIRMethod
  680. with: [ self stream
  681. nextPutFunctionWith: [
  682. self stream nextPutContextFor: anIRMethod during: [
  683. anIRMethod internalVariables notEmpty ifTrue: [
  684. self stream nextPutVars: (anIRMethod internalVariables asArray collect: [ :each |
  685. each variable alias ]) ].
  686. anIRMethod scope hasNonLocalReturn
  687. ifTrue: [
  688. self stream nextPutNonLocalReturnHandlingWith: [
  689. super visitIRMethod: anIRMethod ]]
  690. ifFalse: [ super visitIRMethod: anIRMethod ]]]
  691. arguments: anIRMethod arguments ]
  692. !
  693. visitIRNonLocalReturn: anIRNonLocalReturn
  694. self stream nextPutNonLocalReturnWith: [
  695. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  696. !
  697. visitIRReturn: anIRReturn
  698. self stream nextPutReturnWith: [
  699. super visitIRReturn: anIRReturn ]
  700. !
  701. visitIRSend: anIRSend
  702. anIRSend classSend
  703. ifNil: [
  704. self stream nextPutAll: '_st('.
  705. self visit: anIRSend instructions first.
  706. self stream nextPutAll: ').', anIRSend selector asSelector, '('.
  707. anIRSend instructions allButFirst
  708. do: [ :each | self visit: each ]
  709. separatedBy: [ self stream nextPutAll: ',' ].
  710. self stream nextPutAll: ')' ]
  711. ifNotNil: [
  712. self stream
  713. nextPutAll: anIRSend classSend asJavascript, '.fn.prototype.';
  714. nextPutAll: anIRSend selector asSelector, '.apply(';
  715. nextPutAll: '_st('.
  716. self visit: anIRSend instructions first.
  717. self stream nextPutAll: '), ['.
  718. anIRSend instructions allButFirst
  719. do: [ :each | self visit: each ]
  720. separatedBy: [ self stream nextPutAll: ',' ].
  721. self stream nextPutAll: '])' ]
  722. !
  723. visitIRSequence: anIRSequence
  724. self stream nextPutSequenceWith: [
  725. anIRSequence instructions do: [ :each |
  726. self stream nextPutStatementWith: (self visit: each) ]]
  727. !
  728. visitIRTempDeclaration: anIRTempDeclaration
  729. self stream
  730. nextPutAll: anIRTempDeclaration scope alias, '.locals.', anIRTempDeclaration name, '=nil;';
  731. lf
  732. !
  733. visitIRValue: anIRValue
  734. self stream nextPutAll: anIRValue value asJavascript
  735. !
  736. visitIRVariable: anIRVariable
  737. anIRVariable variable name = 'thisContext'
  738. ifTrue: [ self stream nextPutAll: 'smalltalk.getThisContext()' ]
  739. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  740. !
  741. visitIRVerbatim: anIRVerbatim
  742. self stream nextPutStatementWith: [
  743. self stream nextPutAll: anIRVerbatim source ]
  744. ! !
  745. Object subclass: #JSStream
  746. instanceVariableNames: 'stream'
  747. package: 'Compiler-IR'!
  748. !JSStream methodsFor: 'accessing'!
  749. contents
  750. ^ stream contents
  751. ! !
  752. !JSStream methodsFor: 'initialization'!
  753. initialize
  754. super initialize.
  755. stream := '' writeStream.
  756. ! !
  757. !JSStream methodsFor: 'streaming'!
  758. lf
  759. stream lf
  760. !
  761. nextPut: aString
  762. stream nextPut: aString
  763. !
  764. nextPutAll: aString
  765. stream nextPutAll: aString
  766. !
  767. nextPutAssignment
  768. stream nextPutAll: '='
  769. !
  770. nextPutBlockContextFor: anIRClosure during: aBlock
  771. self
  772. nextPutAll: 'return smalltalk.withContext(function(', anIRClosure scope alias, ') { ';
  773. nextPutAll: String cr.
  774. aBlock value.
  775. self nextPutAll: '})'
  776. !
  777. nextPutClosureWith: aBlock arguments: anArray
  778. stream nextPutAll: '(function('.
  779. anArray
  780. do: [ :each | stream nextPutAll: each asVariableName ]
  781. separatedBy: [ stream nextPut: ',' ].
  782. stream nextPutAll: '){'; lf.
  783. aBlock value.
  784. stream nextPutAll: '})'
  785. !
  786. nextPutContextFor: aMethod during: aBlock
  787. self
  788. nextPutAll: 'return smalltalk.withContext(function(', aMethod scope alias, ') { ';
  789. nextPutAll: String cr.
  790. aBlock value.
  791. self
  792. nextPutAll: '}, self, ';
  793. nextPutAll: aMethod selector asJavascript, ', ['.
  794. aMethod arguments
  795. do: [ :each | self nextPutAll: each asVariableName ]
  796. separatedBy: [ self nextPutAll: ',' ].
  797. self nextPutAll: '], ';
  798. nextPutAll: aMethod theClass asJavascript;
  799. nextPutAll: ')'
  800. !
  801. nextPutFunctionWith: aBlock arguments: anArray
  802. stream nextPutAll: 'fn: function('.
  803. anArray
  804. do: [ :each | stream nextPutAll: each asVariableName ]
  805. separatedBy: [ stream nextPut: ',' ].
  806. stream nextPutAll: '){'; lf.
  807. stream nextPutAll: 'var self=this;'; lf.
  808. aBlock value.
  809. stream nextPutAll: '}'
  810. !
  811. nextPutIf: aBlock with: anotherBlock
  812. stream nextPutAll: 'if('.
  813. aBlock value.
  814. stream nextPutAll: '){'; lf.
  815. anotherBlock value.
  816. stream nextPutAll: '}'
  817. !
  818. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  819. stream nextPutAll: 'if('.
  820. aBlock value.
  821. stream nextPutAll: '){'; lf.
  822. ifBlock value.
  823. stream nextPutAll: '} else {'; lf.
  824. elseBlock value.
  825. stream nextPutAll: '}'
  826. !
  827. nextPutMethodDeclaration: aMethod with: aBlock
  828. stream
  829. nextPutAll: 'smalltalk.method({'; lf;
  830. nextPutAll: 'selector: "', aMethod selector, '",'; lf;
  831. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  832. aBlock value.
  833. stream
  834. nextPutAll: ',', String lf, 'messageSends: ';
  835. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  836. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  837. nextPutAll: 'referencedClasses: ['.
  838. aMethod classReferences
  839. do: [:each | stream nextPutAll: each asJavascript]
  840. separatedBy: [stream nextPutAll: ','].
  841. stream
  842. nextPutAll: ']';
  843. nextPutAll: '})'
  844. !
  845. nextPutNonLocalReturnHandlingWith: aBlock
  846. stream
  847. nextPutAll: 'var $early={};'; lf;
  848. nextPutAll: 'try {'; lf.
  849. aBlock value.
  850. stream
  851. nextPutAll: '}'; lf;
  852. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  853. !
  854. nextPutNonLocalReturnWith: aBlock
  855. stream nextPutAll: 'throw $early=['.
  856. aBlock value.
  857. stream nextPutAll: ']'
  858. !
  859. nextPutReturn
  860. stream nextPutAll: 'return '
  861. !
  862. nextPutReturnWith: aBlock
  863. self nextPutReturn.
  864. aBlock value
  865. !
  866. nextPutSequenceWith: aBlock
  867. "stream
  868. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  869. aBlock value.
  870. "stream
  871. nextPutAll: '};'; lf"
  872. !
  873. nextPutStatement: anInteger with: aBlock
  874. stream nextPutAll: 'case ', anInteger asString, ':'; lf.
  875. self nextPutStatementWith: aBlock.
  876. stream nextPutAll: 'smalltalk.thisContext.pc=', (anInteger + 1) asString, ';'; lf
  877. !
  878. nextPutStatementWith: aBlock
  879. aBlock value.
  880. stream nextPutAll: ';'; lf
  881. !
  882. nextPutVar: aString
  883. stream nextPutAll: 'var ', aString, ';'; lf
  884. !
  885. nextPutVars: aCollection
  886. stream nextPutAll: 'var '.
  887. aCollection
  888. do: [ :each | stream nextPutAll: each ]
  889. separatedBy: [ stream nextPutAll: ',' ].
  890. stream nextPutAll: ';'; lf
  891. ! !
  892. !BlockClosure methodsFor: '*Compiler-IR'!
  893. appendToInstruction: anIRInstruction
  894. anIRInstruction appendBlock: self
  895. ! !
  896. !String methodsFor: '*Compiler-IR'!
  897. asVariableName
  898. ^ (Smalltalk current reservedWords includes: self)
  899. ifTrue: [ self, '_' ]
  900. ifFalse: [ self ]
  901. ! !