Compiler-IR.st 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. anIRVariable name = 'thisContext'
  700. ifTrue: [ self stream nextPutAll: 'smalltalk.getThisContext()' ]
  701. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  702. !
  703. visitIRVerbatim: anIRVerbatim
  704. self stream nextPutStatementWith: [
  705. self stream nextPutAll: anIRVerbatim source ]
  706. ! !
  707. Object subclass: #JSStream
  708. instanceVariableNames: 'stream'
  709. package: 'Compiler-IR'!
  710. !JSStream methodsFor: 'accessing'!
  711. contents
  712. ^ stream contents
  713. ! !
  714. !JSStream methodsFor: 'initialization'!
  715. initialize
  716. super initialize.
  717. stream := '' writeStream.
  718. ! !
  719. !JSStream methodsFor: 'streaming'!
  720. lf
  721. stream lf
  722. !
  723. nextPut: aString
  724. stream nextPut: aString
  725. !
  726. nextPutAll: aString
  727. stream nextPutAll: aString
  728. !
  729. nextPutAssignment
  730. stream nextPutAll: '='
  731. !
  732. nextPutClosureWith: aBlock arguments: anArray
  733. stream nextPutAll: '(function('.
  734. anArray
  735. do: [ :each | stream nextPutAll: each asVariableName ]
  736. separatedBy: [ stream nextPut: ',' ].
  737. stream nextPutAll: '){'; lf.
  738. aBlock value.
  739. stream nextPutAll: '})'
  740. !
  741. nextPutFunctionWith: aBlock arguments: anArray
  742. stream nextPutAll: 'fn: function('.
  743. anArray
  744. do: [ :each | stream nextPutAll: each asVariableName ]
  745. separatedBy: [ stream nextPut: ',' ].
  746. stream nextPutAll: '){'; lf.
  747. stream nextPutAll: 'var self=this;'; lf.
  748. aBlock value.
  749. stream nextPutAll: '}'
  750. !
  751. nextPutIf: aBlock with: anotherBlock
  752. stream nextPutAll: 'if('.
  753. aBlock value.
  754. stream nextPutAll: '){'; lf.
  755. anotherBlock value.
  756. stream nextPutAll: '}'
  757. !
  758. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  759. stream nextPutAll: 'if('.
  760. aBlock value.
  761. stream nextPutAll: '){'; lf.
  762. ifBlock value.
  763. stream nextPutAll: '} else {'; lf.
  764. elseBlock value.
  765. stream nextPutAll: '}'
  766. !
  767. nextPutMethodDeclaration: aMethod with: aBlock
  768. stream
  769. nextPutAll: 'smalltalk.method({'; lf;
  770. nextPutAll: 'selector: "', aMethod selector, '",'; lf;
  771. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  772. aBlock value.
  773. stream
  774. nextPutAll: ',', String lf, 'messageSends: ';
  775. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  776. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  777. nextPutAll: 'referencedClasses: ['.
  778. aMethod classReferences
  779. do: [:each | stream nextPutAll: each asJavascript]
  780. separatedBy: [stream nextPutAll: ','].
  781. stream
  782. nextPutAll: ']';
  783. nextPutAll: '})'
  784. !
  785. nextPutNonLocalReturnHandlingWith: aBlock
  786. stream
  787. nextPutAll: 'var $early={};'; lf;
  788. nextPutAll: 'try {'; lf.
  789. aBlock value.
  790. stream
  791. nextPutAll: '}'; lf;
  792. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  793. !
  794. nextPutNonLocalReturnWith: aBlock
  795. stream nextPutAll: 'throw $early=['.
  796. aBlock value.
  797. stream nextPutAll: ']'
  798. !
  799. nextPutReturn
  800. stream nextPutAll: 'return '
  801. !
  802. nextPutReturnWith: aBlock
  803. self nextPutReturn.
  804. aBlock value
  805. !
  806. nextPutSendTo: receiver selector: selector arguments: arguments
  807. stream nextPutAll: 'smalltalk.send('.
  808. receiver emitOn: self.
  809. stream nextPutAll: ',"', selector asSelector, '",['.
  810. arguments
  811. do: [ :each | each emitOn: self ]
  812. separatedBy: [ stream nextPutAll: ',' ].
  813. stream nextPutAll: '])'
  814. !
  815. nextPutSequenceWith: aBlock
  816. "stream
  817. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  818. aBlock value.
  819. "stream
  820. nextPutAll: '};'; lf"
  821. !
  822. nextPutStatement: anInteger with: aBlock
  823. stream nextPutAll: 'case ', anInteger asString, ':'; lf.
  824. self nextPutStatementWith: aBlock.
  825. stream nextPutAll: 'smalltalk.thisContext.pc=', (anInteger + 1) asString, ';'; lf
  826. !
  827. nextPutStatementWith: aBlock
  828. aBlock value.
  829. stream nextPutAll: ';'; lf
  830. !
  831. nextPutVar: aString
  832. stream nextPutAll: 'var ', aString, ';'; lf
  833. !
  834. nextPutVars: aCollection
  835. stream nextPutAll: 'var '.
  836. aCollection
  837. do: [ :each | stream nextPutAll: each ]
  838. separatedBy: [ stream nextPutAll: ',' ].
  839. stream nextPutAll: ';'; lf
  840. ! !
  841. !BlockClosure methodsFor: '*Compiler-IR'!
  842. appendToInstruction: anIRInstruction
  843. anIRInstruction appendBlock: self
  844. ! !
  845. !String methodsFor: '*Compiler-IR'!
  846. asVariableName
  847. ^ (Smalltalk current reservedWords includes: self)
  848. ifTrue: [ self, '_' ]
  849. ifFalse: [ self ]
  850. ! !