1
0

Compiler-IR.st 19 KB

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