Compiler-IR.st 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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: (IRReturn 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. isLocalReturn
  375. ^ self isReturn
  376. !
  377. isNonLocalReturn
  378. ^ self isLocalReturn not
  379. !
  380. isReturn
  381. ^ true
  382. ! !
  383. !IRReturn methodsFor: 'visiting'!
  384. accept: aVisitor
  385. ^ aVisitor visitIRReturn: self
  386. ! !
  387. IRReturn subclass: #IRNonLocalReturn
  388. instanceVariableNames: ''
  389. package: 'Compiler-IR'!
  390. !IRNonLocalReturn commentStamp!
  391. I am a non local return instruction.
  392. Non local returns are handled using a try/catch JS statement.
  393. See IRNonLocalReturnHandling class!
  394. !IRNonLocalReturn methodsFor: 'testing'!
  395. isLocalReturn
  396. ^ false
  397. ! !
  398. !IRNonLocalReturn methodsFor: 'visiting'!
  399. accept: aVisitor
  400. ^ aVisitor visitIRNonLocalReturn: self
  401. ! !
  402. IRInstruction subclass: #IRSend
  403. instanceVariableNames: 'selector classSend index'
  404. package: 'Compiler-IR'!
  405. !IRSend commentStamp!
  406. I am a message send instruction.!
  407. !IRSend methodsFor: 'accessing'!
  408. classSend
  409. ^ classSend
  410. !
  411. classSend: aClass
  412. classSend := aClass
  413. !
  414. index
  415. ^ index
  416. !
  417. index: anInteger
  418. index := anInteger
  419. !
  420. selector
  421. ^ selector
  422. !
  423. selector: aString
  424. selector := aString
  425. ! !
  426. !IRSend methodsFor: 'testing'!
  427. isSend
  428. ^ true
  429. ! !
  430. !IRSend methodsFor: 'visiting'!
  431. accept: aVisitor
  432. ^ aVisitor visitIRSend: self
  433. ! !
  434. IRInstruction subclass: #IRSequence
  435. instanceVariableNames: ''
  436. package: 'Compiler-IR'!
  437. !IRSequence methodsFor: 'testing'!
  438. isSequence
  439. ^ true
  440. ! !
  441. !IRSequence methodsFor: 'visiting'!
  442. accept: aVisitor
  443. ^ aVisitor visitIRSequence: self
  444. ! !
  445. IRSequence subclass: #IRBlockSequence
  446. instanceVariableNames: ''
  447. package: 'Compiler-IR'!
  448. !IRBlockSequence methodsFor: 'visiting'!
  449. accept: aVisitor
  450. ^ aVisitor visitIRBlockSequence: self
  451. ! !
  452. IRInstruction subclass: #IRTempDeclaration
  453. instanceVariableNames: 'name'
  454. package: 'Compiler-IR'!
  455. !IRTempDeclaration commentStamp!
  456. I am a temporary variable declaration instruction!
  457. !IRTempDeclaration methodsFor: 'accessing'!
  458. name
  459. ^ name
  460. !
  461. name: aString
  462. name := aString
  463. ! !
  464. !IRTempDeclaration methodsFor: 'visiting'!
  465. accept: aVisitor
  466. ^ aVisitor visitIRTempDeclaration: self
  467. !
  468. isTempDeclaration
  469. ^ true
  470. ! !
  471. IRInstruction subclass: #IRValue
  472. instanceVariableNames: 'value'
  473. package: 'Compiler-IR'!
  474. !IRValue commentStamp!
  475. I am the simplest possible instruction. I represent a value.!
  476. !IRValue methodsFor: 'accessing'!
  477. value
  478. ^value
  479. !
  480. value: aString
  481. value := aString
  482. ! !
  483. !IRValue methodsFor: 'visiting'!
  484. accept: aVisitor
  485. ^ aVisitor visitIRValue: self
  486. ! !
  487. IRInstruction subclass: #IRVariable
  488. instanceVariableNames: 'variable'
  489. package: 'Compiler-IR'!
  490. !IRVariable commentStamp!
  491. I am a variable instruction.!
  492. !IRVariable methodsFor: 'accessing'!
  493. variable
  494. ^ variable
  495. !
  496. variable: aScopeVariable
  497. variable := aScopeVariable
  498. ! !
  499. !IRVariable methodsFor: 'testing'!
  500. isVariable
  501. ^ true
  502. ! !
  503. !IRVariable methodsFor: 'visiting'!
  504. accept: aVisitor
  505. ^ aVisitor visitIRVariable: self
  506. ! !
  507. IRInstruction subclass: #IRVerbatim
  508. instanceVariableNames: 'source'
  509. package: 'Compiler-IR'!
  510. !IRVerbatim methodsFor: 'accessing'!
  511. source
  512. ^ source
  513. !
  514. source: aString
  515. source := aString
  516. ! !
  517. !IRVerbatim methodsFor: 'visiting'!
  518. accept: aVisitor
  519. ^ aVisitor visitIRVerbatim: self
  520. ! !
  521. Object subclass: #IRVisitor
  522. instanceVariableNames: ''
  523. package: 'Compiler-IR'!
  524. !IRVisitor methodsFor: 'visiting'!
  525. visit: anIRInstruction
  526. ^ anIRInstruction accept: self
  527. !
  528. visitIRAssignment: anIRAssignment
  529. ^ self visitIRInstruction: anIRAssignment
  530. !
  531. visitIRBlockSequence: anIRBlockSequence
  532. ^ self visitIRSequence: anIRBlockSequence
  533. !
  534. visitIRClosure: anIRClosure
  535. ^ self visitIRInstruction: anIRClosure
  536. !
  537. visitIRDynamicArray: anIRDynamicArray
  538. ^ self visitIRInstruction: anIRDynamicArray
  539. !
  540. visitIRDynamicDictionary: anIRDynamicDictionary
  541. ^ self visitIRInstruction: anIRDynamicDictionary
  542. !
  543. visitIRInstruction: anIRInstruction
  544. anIRInstruction instructions do: [ :each | self visit: each ].
  545. ^ anIRInstruction
  546. !
  547. visitIRMethod: anIRMethod
  548. ^ self visitIRInstruction: anIRMethod
  549. !
  550. visitIRNonLocalReturn: anIRNonLocalReturn
  551. ^ self visitIRInstruction: anIRNonLocalReturn
  552. !
  553. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  554. ^ self visitIRInstruction: anIRNonLocalReturnHandling
  555. !
  556. visitIRReturn: anIRReturn
  557. ^ self visitIRInstruction: anIRReturn
  558. !
  559. visitIRSend: anIRSend
  560. ^ self visitIRInstruction: anIRSend
  561. !
  562. visitIRSequence: anIRSequence
  563. ^ self visitIRInstruction: anIRSequence
  564. !
  565. visitIRTempDeclaration: anIRTempDeclaration
  566. ^ self visitIRInstruction: anIRTempDeclaration
  567. !
  568. visitIRValue: anIRValue
  569. ^ self visitIRInstruction: anIRValue
  570. !
  571. visitIRVariable: anIRVariable
  572. ^ self visitIRInstruction: anIRVariable
  573. !
  574. visitIRVerbatim: anIRVerbatim
  575. ^ self visitIRInstruction: anIRVerbatim
  576. ! !
  577. IRVisitor subclass: #IRJSTranslator
  578. instanceVariableNames: 'stream'
  579. package: 'Compiler-IR'!
  580. !IRJSTranslator methodsFor: 'accessing'!
  581. contents
  582. ^ self stream contents
  583. !
  584. stream
  585. ^ stream
  586. !
  587. stream: aStream
  588. stream := aStream
  589. ! !
  590. !IRJSTranslator methodsFor: 'initialization'!
  591. initialize
  592. super initialize.
  593. stream := JSStream new.
  594. ! !
  595. !IRJSTranslator methodsFor: 'visiting'!
  596. visitIRAssignment: anIRAssignment
  597. self visit: anIRAssignment instructions first.
  598. self stream nextPutAssignment.
  599. self visit: anIRAssignment instructions last.
  600. !
  601. visitIRClosure: anIRClosure
  602. self stream
  603. nextPutClosureWith: [ super visitIRClosure: anIRClosure ]
  604. arguments: anIRClosure arguments
  605. !
  606. visitIRDynamicArray: anIRDynamicArray
  607. self stream nextPutAll: '['.
  608. anIRDynamicArray instructions
  609. do: [ :each | self visit: each ]
  610. separatedBy: [ self stream nextPutAll: ',' ].
  611. stream nextPutAll: ']'
  612. !
  613. visitIRDynamicDictionary: anIRDynamicDictionary
  614. self stream nextPutAll: 'smalltalk.HashedCollection._fromPairs_(['.
  615. anIRDynamicDictionary instructions
  616. do: [ :each | self visit: each ]
  617. separatedBy: [self stream nextPutAll: ',' ].
  618. self stream nextPutAll: '])'
  619. !
  620. visitIRMethod: anIRMethod
  621. self stream
  622. nextPutMethodDeclaration: anIRMethod
  623. with: [ self stream
  624. nextPutFunctionWith: [
  625. anIRMethod internalVariables notEmpty ifTrue: [
  626. self stream nextPutVars: (anIRMethod internalVariables asArray collect: [ :each |
  627. each variable alias ]) ].
  628. anIRMethod scope hasNonLocalReturn
  629. ifTrue: [
  630. self stream nextPutNonLocalReturnHandlingWith: [
  631. super visitIRMethod: anIRMethod ]]
  632. ifFalse: [ super visitIRMethod: anIRMethod ]]
  633. arguments: anIRMethod arguments ]
  634. !
  635. visitIRNonLocalReturn: anIRNonLocalReturn
  636. self stream nextPutNonLocalReturnWith: [
  637. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  638. !
  639. visitIRReturn: anIRReturn
  640. self stream nextPutReturnWith: [
  641. super visitIRReturn: anIRReturn ]
  642. !
  643. visitIRSend: anIRSend
  644. self stream nextPutAll: 'smalltalk.send('.
  645. self visit: anIRSend instructions first.
  646. self stream nextPutAll: ',"', anIRSend selector asSelector, '",['.
  647. anIRSend instructions allButFirst
  648. do: [ :each | self visit: each ]
  649. separatedBy: [ self stream nextPutAll: ',' ].
  650. self stream nextPutAll: ']'.
  651. "anIRSend index > 1
  652. ifTrue: [
  653. anIRSend classSend
  654. ifNil: [ self stream nextPutAll: ',undefined' ]
  655. ifNotNil: [ self stream nextPutAll: ',', anIRSend classSend asJavascript ].
  656. self stream nextPutAll: ',', anIRSend index asString ]
  657. ifFalse: ["
  658. anIRSend classSend ifNotNil: [
  659. self stream nextPutAll: ',', anIRSend classSend asJavascript ]"]".
  660. self stream nextPutAll: ')'
  661. !
  662. visitIRSequence: anIRSequence
  663. self stream nextPutSequenceWith: [
  664. anIRSequence instructions do: [ :each |
  665. self stream nextPutStatementWith: (self visit: each) ]]
  666. !
  667. visitIRTempDeclaration: anIRTempDeclaration
  668. self stream nextPutVar: anIRTempDeclaration name asVariableName
  669. !
  670. visitIRValue: anIRValue
  671. self stream nextPutAll: anIRValue value asJavascript
  672. !
  673. visitIRVariable: anIRVariable
  674. self stream nextPutAll: anIRVariable variable alias
  675. !
  676. visitIRVerbatim: anIRVerbatim
  677. self stream nextPutStatementWith: [
  678. self stream nextPutAll: anIRVerbatim source ]
  679. ! !
  680. Object subclass: #JSStream
  681. instanceVariableNames: 'stream'
  682. package: 'Compiler-IR'!
  683. !JSStream methodsFor: 'accessing'!
  684. contents
  685. ^ stream contents
  686. ! !
  687. !JSStream methodsFor: 'initialization'!
  688. initialize
  689. super initialize.
  690. stream := '' writeStream.
  691. ! !
  692. !JSStream methodsFor: 'streaming'!
  693. lf
  694. stream lf
  695. !
  696. nextPut: aString
  697. stream nextPut: aString
  698. !
  699. nextPutAll: aString
  700. stream nextPutAll: aString
  701. !
  702. nextPutAssignment
  703. stream nextPutAll: '='
  704. !
  705. nextPutClosureWith: aBlock arguments: anArray
  706. stream nextPutAll: '(function('.
  707. anArray
  708. do: [ :each | stream nextPutAll: each asVariableName ]
  709. separatedBy: [ stream nextPut: ',' ].
  710. stream nextPutAll: '){'; lf.
  711. aBlock value.
  712. stream nextPutAll: '})'
  713. !
  714. nextPutFunctionWith: aBlock arguments: anArray
  715. stream nextPutAll: 'fn: function('.
  716. anArray
  717. do: [ :each | stream nextPutAll: each asVariableName ]
  718. separatedBy: [ stream nextPut: ',' ].
  719. stream nextPutAll: '){'; lf.
  720. stream nextPutAll: 'var self=this;'; lf.
  721. aBlock value.
  722. stream nextPutAll: '}'
  723. !
  724. nextPutIf: aBlock with: anotherBlock
  725. stream nextPutAll: 'if('.
  726. aBlock value.
  727. stream nextPutAll: '){'; lf.
  728. anotherBlock value.
  729. stream nextPutAll: '}'
  730. !
  731. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  732. stream nextPutAll: 'if('.
  733. aBlock value.
  734. stream nextPutAll: '){'; lf.
  735. ifBlock value.
  736. stream nextPutAll: '} else {'; lf.
  737. elseBlock value.
  738. stream nextPutAll: '}'
  739. !
  740. nextPutMethodDeclaration: aMethod with: aBlock
  741. stream
  742. nextPutAll: 'smalltalk.method({'; lf;
  743. nextPutAll: 'selector: "', aMethod selector, '",'; lf;
  744. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  745. aBlock value.
  746. stream
  747. nextPutAll: ',', String lf, 'messageSends: ';
  748. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  749. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  750. nextPutAll: 'referencedClasses: ['.
  751. aMethod classReferences
  752. do: [:each | stream nextPutAll: each asJavascript]
  753. separatedBy: [stream nextPutAll: ','].
  754. stream
  755. nextPutAll: ']';
  756. nextPutAll: '})'
  757. !
  758. nextPutNonLocalReturnHandlingWith: aBlock
  759. stream
  760. nextPutAll: 'var $early={};'; lf;
  761. nextPutAll: 'try {'; lf.
  762. aBlock value.
  763. stream
  764. nextPutAll: '}'; lf;
  765. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  766. !
  767. nextPutNonLocalReturnWith: aBlock
  768. stream nextPutAll: 'throw $early=['.
  769. aBlock value.
  770. stream nextPutAll: ']'
  771. !
  772. nextPutReturn
  773. stream nextPutAll: 'return '
  774. !
  775. nextPutReturnWith: aBlock
  776. self nextPutReturn.
  777. aBlock value
  778. !
  779. nextPutSendTo: receiver selector: selector arguments: arguments
  780. stream nextPutAll: 'smalltalk.send('.
  781. receiver emitOn: self.
  782. stream nextPutAll: ',"', selector asSelector, '",['.
  783. arguments
  784. do: [ :each | each emitOn: self ]
  785. separatedBy: [ stream nextPutAll: ',' ].
  786. stream nextPutAll: '])'
  787. !
  788. nextPutSequenceWith: aBlock
  789. "stream
  790. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  791. aBlock value.
  792. "stream
  793. nextPutAll: '};'; lf"
  794. !
  795. nextPutStatement: anInteger with: aBlock
  796. stream nextPutAll: 'case ', anInteger asString, ':'; lf.
  797. self nextPutStatementWith: aBlock.
  798. stream nextPutAll: 'smalltalk.thisContext.pc=', (anInteger + 1) asString, ';'; lf
  799. !
  800. nextPutStatementWith: aBlock
  801. aBlock value.
  802. stream nextPutAll: ';'; lf
  803. !
  804. nextPutVar: aString
  805. stream nextPutAll: 'var ', aString, ';'; lf
  806. !
  807. nextPutVars: aCollection
  808. stream nextPutAll: 'var '.
  809. aCollection
  810. do: [ :each | stream nextPutAll: each ]
  811. separatedBy: [ stream nextPutAll: ',' ].
  812. stream nextPutAll: ';'; lf
  813. ! !
  814. !BlockClosure methodsFor: '*Compiler-IR'!
  815. appendToInstruction: anIRInstruction
  816. anIRInstruction appendBlock: self
  817. ! !
  818. !String methodsFor: '*Compiler-IR'!
  819. asVariableName
  820. ^ (Smalltalk current reservedWords includes: self)
  821. ifTrue: [ self, '_' ]
  822. ifFalse: [ self ]
  823. ! !