Compiler-IR.st 22 KB

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