Compiler-IR.st 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. Smalltalk current createPackage: 'Compiler-IR'!
  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. method
  216. ^ self parent method
  217. !
  218. parent
  219. ^ parent
  220. !
  221. parent: anIRInstruction
  222. parent := anIRInstruction
  223. ! !
  224. !IRInstruction methodsFor: 'building'!
  225. add: anObject
  226. anObject parent: self.
  227. ^ self instructions add: anObject
  228. !
  229. remove
  230. self parent remove: self
  231. !
  232. remove: anIRInstruction
  233. self instructions remove: anIRInstruction
  234. !
  235. replace: anIRInstruction with: anotherIRInstruction
  236. anotherIRInstruction parent: self.
  237. self instructions
  238. at: (self instructions indexOf: anIRInstruction)
  239. put: anotherIRInstruction
  240. !
  241. replaceWith: anIRInstruction
  242. self parent replace: self with: anIRInstruction
  243. ! !
  244. !IRInstruction methodsFor: 'testing'!
  245. canBeAssigned
  246. ^ true
  247. !
  248. isClosure
  249. ^ false
  250. !
  251. isInlined
  252. ^ false
  253. !
  254. isLocalReturn
  255. ^ false
  256. !
  257. isMethod
  258. ^ false
  259. !
  260. isReturn
  261. ^ false
  262. !
  263. isSend
  264. ^ false
  265. !
  266. isSequence
  267. ^ false
  268. !
  269. isTempDeclaration
  270. ^ false
  271. !
  272. isVariable
  273. ^ false
  274. ! !
  275. !IRInstruction methodsFor: 'visiting'!
  276. accept: aVisitor
  277. ^ aVisitor visitIRInstruction: self
  278. ! !
  279. !IRInstruction class methodsFor: 'instance creation'!
  280. on: aBuilder
  281. ^ self new
  282. builder: aBuilder;
  283. yourself
  284. ! !
  285. IRInstruction subclass: #IRAssignment
  286. instanceVariableNames: ''
  287. package: 'Compiler-IR'!
  288. !IRAssignment methodsFor: 'visiting'!
  289. accept: aVisitor
  290. ^ aVisitor visitIRAssignment: self
  291. ! !
  292. IRInstruction subclass: #IRDynamicArray
  293. instanceVariableNames: ''
  294. package: 'Compiler-IR'!
  295. !IRDynamicArray methodsFor: 'visiting'!
  296. accept: aVisitor
  297. ^ aVisitor visitIRDynamicArray: self
  298. ! !
  299. IRInstruction subclass: #IRDynamicDictionary
  300. instanceVariableNames: ''
  301. package: 'Compiler-IR'!
  302. !IRDynamicDictionary methodsFor: 'visiting'!
  303. accept: aVisitor
  304. ^ aVisitor visitIRDynamicDictionary: self
  305. ! !
  306. IRInstruction subclass: #IRScopedInstruction
  307. instanceVariableNames: 'scope'
  308. package: 'Compiler-IR'!
  309. !IRScopedInstruction methodsFor: 'accessing'!
  310. scope
  311. ^ scope
  312. !
  313. scope: aScope
  314. scope := aScope
  315. ! !
  316. IRScopedInstruction subclass: #IRClosureInstruction
  317. instanceVariableNames: 'arguments'
  318. package: 'Compiler-IR'!
  319. !IRClosureInstruction methodsFor: 'accessing'!
  320. arguments
  321. ^ arguments ifNil: [ #() ]
  322. !
  323. arguments: aCollection
  324. arguments := aCollection
  325. !
  326. locals
  327. ^ self arguments copy
  328. addAll: (self tempDeclarations collect: [ :each | each name ]);
  329. yourself
  330. !
  331. scope: aScope
  332. super scope: aScope.
  333. aScope instruction: self
  334. !
  335. tempDeclarations
  336. ^ self instructions select: [ :each |
  337. each isTempDeclaration ]
  338. ! !
  339. IRClosureInstruction subclass: #IRClosure
  340. instanceVariableNames: ''
  341. package: 'Compiler-IR'!
  342. !IRClosure methodsFor: 'accessing'!
  343. sequence
  344. ^ self instructions last
  345. ! !
  346. !IRClosure methodsFor: 'testing'!
  347. isClosure
  348. ^ true
  349. ! !
  350. !IRClosure methodsFor: 'visiting'!
  351. accept: aVisitor
  352. ^ aVisitor visitIRClosure: self
  353. ! !
  354. IRClosureInstruction subclass: #IRMethod
  355. instanceVariableNames: 'theClass source selector classReferences messageSends superSends internalVariables'
  356. package: 'Compiler-IR'!
  357. !IRMethod commentStamp!
  358. I am a method instruction!
  359. !IRMethod methodsFor: 'accessing'!
  360. classReferences
  361. ^ classReferences
  362. !
  363. classReferences: aCollection
  364. classReferences := aCollection
  365. !
  366. internalVariables
  367. ^ internalVariables ifNil: [ internalVariables := Set new ]
  368. !
  369. isMethod
  370. ^ true
  371. !
  372. messageSends
  373. ^ messageSends
  374. !
  375. messageSends: aCollection
  376. messageSends := aCollection
  377. !
  378. method
  379. ^ self
  380. !
  381. selector
  382. ^ selector
  383. !
  384. selector: aString
  385. selector := aString
  386. !
  387. source
  388. ^ source
  389. !
  390. source: aString
  391. source := aString
  392. !
  393. superSends
  394. ^ superSends
  395. !
  396. superSends: aCollection
  397. superSends := aCollection
  398. !
  399. theClass
  400. ^ theClass
  401. !
  402. theClass: aClass
  403. theClass := aClass
  404. ! !
  405. !IRMethod methodsFor: 'visiting'!
  406. accept: aVisitor
  407. ^ aVisitor visitIRMethod: self
  408. ! !
  409. IRScopedInstruction subclass: #IRReturn
  410. instanceVariableNames: ''
  411. package: 'Compiler-IR'!
  412. !IRReturn commentStamp!
  413. I am a local return instruction.!
  414. !IRReturn methodsFor: 'testing'!
  415. canBeAssigned
  416. ^ false
  417. !
  418. isBlockReturn
  419. ^ false
  420. !
  421. isLocalReturn
  422. ^ true
  423. !
  424. isNonLocalReturn
  425. ^ self isLocalReturn not
  426. !
  427. isReturn
  428. ^ true
  429. ! !
  430. !IRReturn methodsFor: 'visiting'!
  431. accept: aVisitor
  432. ^ aVisitor visitIRReturn: self
  433. ! !
  434. IRReturn subclass: #IRBlockReturn
  435. instanceVariableNames: ''
  436. package: 'Compiler-IR'!
  437. !IRBlockReturn commentStamp!
  438. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  439. !IRBlockReturn methodsFor: 'testing'!
  440. isBlockReturn
  441. ^ true
  442. ! !
  443. !IRBlockReturn methodsFor: 'visiting'!
  444. accept: aVisitor
  445. ^ aVisitor visitIRBlockReturn: self
  446. ! !
  447. IRReturn subclass: #IRNonLocalReturn
  448. instanceVariableNames: ''
  449. package: 'Compiler-IR'!
  450. !IRNonLocalReturn commentStamp!
  451. I am a non local return instruction.
  452. Non local returns are handled using a try/catch JS statement.
  453. See IRNonLocalReturnHandling class!
  454. !IRNonLocalReturn methodsFor: 'testing'!
  455. isLocalReturn
  456. ^ false
  457. ! !
  458. !IRNonLocalReturn methodsFor: 'visiting'!
  459. accept: aVisitor
  460. ^ aVisitor visitIRNonLocalReturn: self
  461. ! !
  462. IRScopedInstruction subclass: #IRTempDeclaration
  463. instanceVariableNames: 'name'
  464. package: 'Compiler-IR'!
  465. !IRTempDeclaration methodsFor: 'accessing'!
  466. name
  467. ^ name
  468. !
  469. name: aString
  470. name := aString
  471. ! !
  472. !IRTempDeclaration methodsFor: 'testing'!
  473. isTempDeclaration
  474. ^ true
  475. ! !
  476. !IRTempDeclaration methodsFor: 'visiting'!
  477. accept: aVisitor
  478. ^ aVisitor visitIRTempDeclaration: self
  479. ! !
  480. IRInstruction subclass: #IRSend
  481. instanceVariableNames: 'selector classSend index'
  482. package: 'Compiler-IR'!
  483. !IRSend commentStamp!
  484. I am a message send instruction.!
  485. !IRSend methodsFor: 'accessing'!
  486. classSend
  487. ^ classSend
  488. !
  489. classSend: aClass
  490. classSend := aClass
  491. !
  492. index
  493. ^ index
  494. !
  495. index: anInteger
  496. index := anInteger
  497. !
  498. selector
  499. ^ selector
  500. !
  501. selector: aString
  502. selector := aString
  503. ! !
  504. !IRSend methodsFor: 'testing'!
  505. isSend
  506. ^ true
  507. ! !
  508. !IRSend methodsFor: 'visiting'!
  509. accept: aVisitor
  510. ^ aVisitor visitIRSend: self
  511. ! !
  512. IRInstruction subclass: #IRSequence
  513. instanceVariableNames: ''
  514. package: 'Compiler-IR'!
  515. !IRSequence methodsFor: 'testing'!
  516. isSequence
  517. ^ true
  518. ! !
  519. !IRSequence methodsFor: 'visiting'!
  520. accept: aVisitor
  521. ^ aVisitor visitIRSequence: self
  522. ! !
  523. IRSequence subclass: #IRBlockSequence
  524. instanceVariableNames: ''
  525. package: 'Compiler-IR'!
  526. !IRBlockSequence methodsFor: 'visiting'!
  527. accept: aVisitor
  528. ^ aVisitor visitIRBlockSequence: self
  529. ! !
  530. IRInstruction subclass: #IRValue
  531. instanceVariableNames: 'value'
  532. package: 'Compiler-IR'!
  533. !IRValue commentStamp!
  534. I am the simplest possible instruction. I represent a value.!
  535. !IRValue methodsFor: 'accessing'!
  536. value
  537. ^value
  538. !
  539. value: aString
  540. value := aString
  541. ! !
  542. !IRValue methodsFor: 'visiting'!
  543. accept: aVisitor
  544. ^ aVisitor visitIRValue: self
  545. ! !
  546. IRInstruction subclass: #IRVariable
  547. instanceVariableNames: 'variable'
  548. package: 'Compiler-IR'!
  549. !IRVariable commentStamp!
  550. I am a variable instruction.!
  551. !IRVariable methodsFor: 'accessing'!
  552. variable
  553. ^ variable
  554. !
  555. variable: aScopeVariable
  556. variable := aScopeVariable
  557. ! !
  558. !IRVariable methodsFor: 'testing'!
  559. isVariable
  560. ^ true
  561. ! !
  562. !IRVariable methodsFor: 'visiting'!
  563. accept: aVisitor
  564. ^ aVisitor visitIRVariable: self
  565. ! !
  566. IRInstruction subclass: #IRVerbatim
  567. instanceVariableNames: 'source'
  568. package: 'Compiler-IR'!
  569. !IRVerbatim methodsFor: 'accessing'!
  570. source
  571. ^ source
  572. !
  573. source: aString
  574. source := aString
  575. ! !
  576. !IRVerbatim methodsFor: 'visiting'!
  577. accept: aVisitor
  578. ^ aVisitor visitIRVerbatim: self
  579. ! !
  580. Object subclass: #IRVisitor
  581. instanceVariableNames: ''
  582. package: 'Compiler-IR'!
  583. !IRVisitor methodsFor: 'visiting'!
  584. visit: anIRInstruction
  585. ^ anIRInstruction accept: self
  586. !
  587. visitIRAssignment: anIRAssignment
  588. ^ self visitIRInstruction: anIRAssignment
  589. !
  590. visitIRBlockReturn: anIRBlockReturn
  591. ^ self visitIRReturn: anIRBlockReturn
  592. !
  593. visitIRBlockSequence: anIRBlockSequence
  594. ^ self visitIRSequence: anIRBlockSequence
  595. !
  596. visitIRClosure: anIRClosure
  597. ^ self visitIRInstruction: anIRClosure
  598. !
  599. visitIRDynamicArray: anIRDynamicArray
  600. ^ self visitIRInstruction: anIRDynamicArray
  601. !
  602. visitIRDynamicDictionary: anIRDynamicDictionary
  603. ^ self visitIRInstruction: anIRDynamicDictionary
  604. !
  605. visitIRInlinedClosure: anIRInlinedClosure
  606. ^ self visitIRClosure: anIRInlinedClosure
  607. !
  608. visitIRInlinedSequence: anIRInlinedSequence
  609. ^ self visitIRSequence: anIRInlinedSequence
  610. !
  611. visitIRInstruction: anIRInstruction
  612. anIRInstruction instructions do: [ :each | self visit: each ].
  613. ^ anIRInstruction
  614. !
  615. visitIRMethod: anIRMethod
  616. ^ self visitIRInstruction: anIRMethod
  617. !
  618. visitIRNonLocalReturn: anIRNonLocalReturn
  619. ^ self visitIRInstruction: anIRNonLocalReturn
  620. !
  621. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  622. ^ self visitIRInstruction: anIRNonLocalReturnHandling
  623. !
  624. visitIRReturn: anIRReturn
  625. ^ self visitIRInstruction: anIRReturn
  626. !
  627. visitIRSend: anIRSend
  628. ^ self visitIRInstruction: anIRSend
  629. !
  630. visitIRSequence: anIRSequence
  631. ^ self visitIRInstruction: anIRSequence
  632. !
  633. visitIRTempDeclaration: anIRTempDeclaration
  634. ^ self visitIRInstruction: anIRTempDeclaration
  635. !
  636. visitIRValue: anIRValue
  637. ^ self visitIRInstruction: anIRValue
  638. !
  639. visitIRVariable: anIRVariable
  640. ^ self visitIRInstruction: anIRVariable
  641. !
  642. visitIRVerbatim: anIRVerbatim
  643. ^ self visitIRInstruction: anIRVerbatim
  644. ! !
  645. IRVisitor subclass: #IRJSTranslator
  646. instanceVariableNames: 'stream'
  647. package: 'Compiler-IR'!
  648. !IRJSTranslator methodsFor: 'accessing'!
  649. contents
  650. ^ self stream contents
  651. !
  652. stream
  653. ^ stream
  654. !
  655. stream: aStream
  656. stream := aStream
  657. ! !
  658. !IRJSTranslator methodsFor: 'initialization'!
  659. initialize
  660. super initialize.
  661. stream := JSStream new.
  662. ! !
  663. !IRJSTranslator methodsFor: 'visiting'!
  664. visitIRAssignment: anIRAssignment
  665. self visit: anIRAssignment instructions first.
  666. self stream nextPutAssignment.
  667. self visit: anIRAssignment instructions last.
  668. !
  669. visitIRClosure: anIRClosure
  670. self stream
  671. nextPutClosureWith: [
  672. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  673. each name asVariableName ]).
  674. self stream
  675. nextPutBlockContextFor: anIRClosure
  676. during: [ super visitIRClosure: anIRClosure ] ]
  677. arguments: anIRClosure arguments
  678. !
  679. visitIRDynamicArray: anIRDynamicArray
  680. self stream nextPutAll: '['.
  681. anIRDynamicArray instructions
  682. do: [ :each | self visit: each ]
  683. separatedBy: [ self stream nextPutAll: ',' ].
  684. stream nextPutAll: ']'
  685. !
  686. visitIRDynamicDictionary: anIRDynamicDictionary
  687. self stream nextPutAll: 'smalltalk.HashedCollection._fromPairs_(['.
  688. anIRDynamicDictionary instructions
  689. do: [ :each | self visit: each ]
  690. separatedBy: [self stream nextPutAll: ',' ].
  691. self stream nextPutAll: '])'
  692. !
  693. visitIRMethod: anIRMethod
  694. self stream
  695. nextPutMethodDeclaration: anIRMethod
  696. with: [ self stream
  697. nextPutFunctionWith: [
  698. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  699. each name asVariableName ]).
  700. self stream nextPutContextFor: anIRMethod during: [
  701. anIRMethod internalVariables notEmpty ifTrue: [
  702. self stream nextPutVars: (anIRMethod internalVariables asArray collect: [ :each |
  703. each variable alias ]) ].
  704. anIRMethod scope hasNonLocalReturn
  705. ifTrue: [
  706. self stream nextPutNonLocalReturnHandlingWith: [
  707. super visitIRMethod: anIRMethod ]]
  708. ifFalse: [ super visitIRMethod: anIRMethod ]]]
  709. arguments: anIRMethod arguments ]
  710. !
  711. visitIRNonLocalReturn: anIRNonLocalReturn
  712. self stream nextPutNonLocalReturnWith: [
  713. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  714. !
  715. visitIRReturn: anIRReturn
  716. self stream nextPutReturnWith: [
  717. super visitIRReturn: anIRReturn ]
  718. !
  719. visitIRSend: anIRSend
  720. anIRSend classSend
  721. ifNil: [
  722. self stream nextPutAll: '_st('.
  723. self visit: anIRSend instructions first.
  724. self stream nextPutAll: ').', anIRSend selector asSelector, '('.
  725. anIRSend instructions allButFirst
  726. do: [ :each | self visit: each ]
  727. separatedBy: [ self stream nextPutAll: ',' ].
  728. self stream nextPutAll: ')' ]
  729. ifNotNil: [
  730. self stream
  731. nextPutAll: anIRSend classSend asJavascript, '.fn.prototype.';
  732. nextPutAll: anIRSend selector asSelector, '.apply(';
  733. nextPutAll: '_st('.
  734. self visit: anIRSend instructions first.
  735. self stream nextPutAll: '), ['.
  736. anIRSend instructions allButFirst
  737. do: [ :each | self visit: each ]
  738. separatedBy: [ self stream nextPutAll: ',' ].
  739. self stream nextPutAll: '])' ]
  740. !
  741. visitIRSequence: anIRSequence
  742. self stream nextPutSequenceWith: [
  743. anIRSequence instructions do: [ :each |
  744. self stream nextPutStatementWith: (self visit: each) ]]
  745. !
  746. visitIRTempDeclaration: anIRTempDeclaration
  747. "self stream
  748. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  749. lf"
  750. !
  751. visitIRValue: anIRValue
  752. self stream nextPutAll: anIRValue value asJavascript
  753. !
  754. visitIRVariable: anIRVariable
  755. anIRVariable variable name = 'thisContext'
  756. ifTrue: [ self stream nextPutAll: 'smalltalk.getThisContext()' ]
  757. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  758. !
  759. visitIRVerbatim: anIRVerbatim
  760. self stream nextPutStatementWith: [
  761. self stream nextPutAll: anIRVerbatim source ]
  762. ! !
  763. Object subclass: #JSStream
  764. instanceVariableNames: 'stream'
  765. package: 'Compiler-IR'!
  766. !JSStream methodsFor: 'accessing'!
  767. contents
  768. ^ stream contents
  769. ! !
  770. !JSStream methodsFor: 'initialization'!
  771. initialize
  772. super initialize.
  773. stream := '' writeStream.
  774. ! !
  775. !JSStream methodsFor: 'streaming'!
  776. lf
  777. stream lf
  778. !
  779. nextPut: aString
  780. stream nextPut: aString
  781. !
  782. nextPutAll: aString
  783. stream nextPutAll: aString
  784. !
  785. nextPutAssignment
  786. stream nextPutAll: '='
  787. !
  788. nextPutBlockContextFor: anIRClosure during: aBlock
  789. self
  790. nextPutAll: 'return smalltalk.withContext(function(', anIRClosure scope alias, ') {';
  791. nextPutAll: String cr.
  792. aBlock value.
  793. self
  794. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  795. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  796. anIRClosure locals
  797. do: [ :each |
  798. self
  799. nextPutAll: each asVariableName;
  800. nextPutAll: ':';
  801. nextPutAll: each asVariableName]
  802. separatedBy: [ self nextPutAll: ',' ].
  803. self
  804. nextPutAll: '},';
  805. nextPutAll: anIRClosure method scope alias, ')})'
  806. !
  807. nextPutClosureWith: aBlock arguments: anArray
  808. stream nextPutAll: '(function('.
  809. anArray
  810. do: [ :each | stream nextPutAll: each asVariableName ]
  811. separatedBy: [ stream nextPut: ',' ].
  812. stream nextPutAll: '){'; lf.
  813. aBlock value.
  814. stream nextPutAll: '})'
  815. !
  816. nextPutContextFor: aMethod during: aBlock
  817. self
  818. nextPutAll: 'return smalltalk.withContext(function(', aMethod scope alias, ') { ';
  819. nextPutAll: String cr.
  820. aBlock value.
  821. self
  822. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  823. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  824. aMethod locals
  825. do: [ :each |
  826. self
  827. nextPutAll: each asVariableName;
  828. nextPutAll: ':';
  829. nextPutAll: each asVariableName]
  830. separatedBy: [ self nextPutAll: ',' ].
  831. self
  832. nextPutAll: '},';
  833. nextPutAll: aMethod theClass asJavascript;
  834. nextPutAll: ')})'
  835. !
  836. nextPutFunctionWith: aBlock arguments: anArray
  837. stream nextPutAll: 'fn: function('.
  838. anArray
  839. do: [ :each | stream nextPutAll: each asVariableName ]
  840. separatedBy: [ stream nextPut: ',' ].
  841. stream nextPutAll: '){'; lf.
  842. stream nextPutAll: 'var self=this;'; lf.
  843. aBlock value.
  844. stream nextPutAll: '}'
  845. !
  846. nextPutIf: aBlock with: anotherBlock
  847. stream nextPutAll: 'if('.
  848. aBlock value.
  849. stream nextPutAll: '){'; lf.
  850. anotherBlock value.
  851. stream nextPutAll: '}'
  852. !
  853. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  854. stream nextPutAll: 'if('.
  855. aBlock value.
  856. stream nextPutAll: '){'; lf.
  857. ifBlock value.
  858. stream nextPutAll: '} else {'; lf.
  859. elseBlock value.
  860. stream nextPutAll: '}'
  861. !
  862. nextPutMethodDeclaration: aMethod with: aBlock
  863. stream
  864. nextPutAll: 'smalltalk.method({'; lf;
  865. nextPutAll: 'selector: "', aMethod selector, '",'; lf;
  866. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  867. aBlock value.
  868. stream
  869. nextPutAll: ',', String lf, 'messageSends: ';
  870. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  871. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  872. nextPutAll: 'referencedClasses: ['.
  873. aMethod classReferences
  874. do: [:each | stream nextPutAll: each asJavascript]
  875. separatedBy: [stream nextPutAll: ','].
  876. stream
  877. nextPutAll: ']';
  878. nextPutAll: '})'
  879. !
  880. nextPutNonLocalReturnHandlingWith: aBlock
  881. stream
  882. nextPutAll: 'var $early={};'; lf;
  883. nextPutAll: 'try {'; lf.
  884. aBlock value.
  885. stream
  886. nextPutAll: '}'; lf;
  887. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  888. !
  889. nextPutNonLocalReturnWith: aBlock
  890. stream nextPutAll: 'throw $early=['.
  891. aBlock value.
  892. stream nextPutAll: ']'
  893. !
  894. nextPutReturn
  895. stream nextPutAll: 'return '
  896. !
  897. nextPutReturnWith: aBlock
  898. self nextPutReturn.
  899. aBlock value
  900. !
  901. nextPutSequenceWith: aBlock
  902. "stream
  903. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  904. aBlock value.
  905. "stream
  906. nextPutAll: '};'; lf"
  907. !
  908. nextPutStatement: anInteger with: aBlock
  909. stream nextPutAll: 'case ', anInteger asString, ':'; lf.
  910. self nextPutStatementWith: aBlock.
  911. stream nextPutAll: 'smalltalk.thisContext.pc=', (anInteger + 1) asString, ';'; lf
  912. !
  913. nextPutStatementWith: aBlock
  914. aBlock value.
  915. stream nextPutAll: ';'; lf
  916. !
  917. nextPutVars: aCollection
  918. aCollection ifEmpty: [ ^self ].
  919. stream nextPutAll: 'var '.
  920. aCollection
  921. do: [ :each | stream nextPutAll: each ]
  922. separatedBy: [ stream nextPutAll: ',' ].
  923. stream nextPutAll: ';'; lf
  924. ! !
  925. !BlockClosure methodsFor: '*Compiler-IR'!
  926. appendToInstruction: anIRInstruction
  927. anIRInstruction appendBlock: self
  928. ! !
  929. !String methodsFor: '*Compiler-IR'!
  930. asVariableName
  931. ^ (Smalltalk current reservedWords includes: self)
  932. ifTrue: [ self, '_' ]
  933. ifFalse: [ self ]
  934. ! !