Compiler-IR.st 20 KB

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