Compiler-IR.st 23 KB

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