Compiler-IR.st 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. anIRMethod classReferences do: [ :each | self stream nextPutClassRefFunction: each ].
  701. self stream nextPutContextFor: anIRMethod during: [
  702. anIRMethod internalVariables notEmpty ifTrue: [
  703. self stream nextPutVars: (anIRMethod internalVariables asArray collect: [ :each |
  704. each variable alias ]) ].
  705. anIRMethod scope hasNonLocalReturn
  706. ifTrue: [
  707. self stream nextPutNonLocalReturnHandlingWith: [
  708. super visitIRMethod: anIRMethod ]]
  709. ifFalse: [ super visitIRMethod: anIRMethod ]]]
  710. arguments: anIRMethod arguments ]
  711. !
  712. visitIRNonLocalReturn: anIRNonLocalReturn
  713. self stream nextPutNonLocalReturnWith: [
  714. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  715. !
  716. visitIRReturn: anIRReturn
  717. self stream nextPutReturnWith: [
  718. super visitIRReturn: anIRReturn ]
  719. !
  720. visitIRSend: anIRSend
  721. anIRSend classSend
  722. ifNil: [
  723. self stream nextPutAll: '_st('.
  724. self visit: anIRSend instructions first.
  725. self stream nextPutAll: ').', anIRSend selector asSelector, '('.
  726. anIRSend instructions allButFirst
  727. do: [ :each | self visit: each ]
  728. separatedBy: [ self stream nextPutAll: ',' ].
  729. self stream nextPutAll: ')' ]
  730. ifNotNil: [
  731. self stream
  732. nextPutAll: anIRSend classSend asJavascript, '.fn.prototype.';
  733. nextPutAll: anIRSend selector asSelector, '.apply(';
  734. nextPutAll: '_st('.
  735. self visit: anIRSend instructions first.
  736. self stream nextPutAll: '), ['.
  737. anIRSend instructions allButFirst
  738. do: [ :each | self visit: each ]
  739. separatedBy: [ self stream nextPutAll: ',' ].
  740. self stream nextPutAll: '])' ]
  741. !
  742. visitIRSequence: anIRSequence
  743. self stream nextPutSequenceWith: [
  744. anIRSequence instructions do: [ :each |
  745. self stream nextPutStatementWith: (self visit: each) ]]
  746. !
  747. visitIRTempDeclaration: anIRTempDeclaration
  748. "self stream
  749. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  750. lf"
  751. !
  752. visitIRValue: anIRValue
  753. self stream nextPutAll: anIRValue value asJavascript
  754. !
  755. visitIRVariable: anIRVariable
  756. anIRVariable variable name = 'thisContext'
  757. ifTrue: [ self stream nextPutAll: 'smalltalk.getThisContext()' ]
  758. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  759. !
  760. visitIRVerbatim: anIRVerbatim
  761. self stream nextPutStatementWith: [
  762. self stream nextPutAll: anIRVerbatim source ]
  763. ! !
  764. Object subclass: #JSStream
  765. instanceVariableNames: 'stream'
  766. package: 'Compiler-IR'!
  767. !JSStream methodsFor: 'accessing'!
  768. contents
  769. ^ stream contents
  770. ! !
  771. !JSStream methodsFor: 'initialization'!
  772. initialize
  773. super initialize.
  774. stream := '' writeStream.
  775. ! !
  776. !JSStream methodsFor: 'streaming'!
  777. lf
  778. stream lf
  779. !
  780. nextPut: aString
  781. stream nextPut: aString
  782. !
  783. nextPutAll: aString
  784. stream nextPutAll: aString
  785. !
  786. nextPutAssignment
  787. stream nextPutAll: '='
  788. !
  789. nextPutBlockContextFor: anIRClosure during: aBlock
  790. self
  791. nextPutAll: 'return smalltalk.withContext(function(', anIRClosure scope alias, ') {';
  792. nextPutAll: String cr.
  793. aBlock value.
  794. self
  795. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  796. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  797. anIRClosure locals
  798. do: [ :each |
  799. self
  800. nextPutAll: each asVariableName;
  801. nextPutAll: ':';
  802. nextPutAll: each asVariableName]
  803. separatedBy: [ self nextPutAll: ',' ].
  804. self
  805. nextPutAll: '},';
  806. nextPutAll: anIRClosure method scope alias, ')})'
  807. !
  808. nextPutClassRefFunction: aString
  809. stream
  810. nextPutAll: 'function $';
  811. nextPutAll: aString;
  812. nextPutAll: '(){return smalltalk.';
  813. nextPutAll: aString;
  814. nextPutAll: '||(typeof ';
  815. nextPutAll: aString;
  816. nextPutAll: '=="undefined"?nil:';
  817. nextPutAll: aString;
  818. nextPutAll: ')}';
  819. lf
  820. !
  821. nextPutClosureWith: aBlock arguments: anArray
  822. stream nextPutAll: '(function('.
  823. anArray
  824. do: [ :each | stream nextPutAll: each asVariableName ]
  825. separatedBy: [ stream nextPut: ',' ].
  826. stream nextPutAll: '){'; lf.
  827. aBlock value.
  828. stream nextPutAll: '})'
  829. !
  830. nextPutContextFor: aMethod during: aBlock
  831. self
  832. nextPutAll: 'return smalltalk.withContext(function(', aMethod scope alias, ') { ';
  833. nextPutAll: String cr.
  834. aBlock value.
  835. self
  836. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  837. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  838. aMethod locals
  839. do: [ :each |
  840. self
  841. nextPutAll: each asVariableName;
  842. nextPutAll: ':';
  843. nextPutAll: each asVariableName]
  844. separatedBy: [ self nextPutAll: ',' ].
  845. self
  846. nextPutAll: '},';
  847. nextPutAll: aMethod theClass asJavascript;
  848. nextPutAll: ')})'
  849. !
  850. nextPutFunctionWith: aBlock arguments: anArray
  851. stream nextPutAll: 'fn: function('.
  852. anArray
  853. do: [ :each | stream nextPutAll: each asVariableName ]
  854. separatedBy: [ stream nextPut: ',' ].
  855. stream nextPutAll: '){'; lf.
  856. stream nextPutAll: 'var self=this;'; lf.
  857. aBlock value.
  858. stream nextPutAll: '}'
  859. !
  860. nextPutIf: aBlock with: anotherBlock
  861. stream nextPutAll: 'if('.
  862. aBlock value.
  863. stream nextPutAll: '){'; lf.
  864. anotherBlock value.
  865. stream nextPutAll: '}'
  866. !
  867. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  868. stream nextPutAll: 'if('.
  869. aBlock value.
  870. stream nextPutAll: '){'; lf.
  871. ifBlock value.
  872. stream nextPutAll: '} else {'; lf.
  873. elseBlock value.
  874. stream nextPutAll: '}'
  875. !
  876. nextPutMethodDeclaration: aMethod with: aBlock
  877. stream
  878. nextPutAll: 'smalltalk.method({'; lf;
  879. nextPutAll: 'selector: "', aMethod selector, '",'; lf;
  880. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  881. aBlock value.
  882. stream
  883. nextPutAll: ',', String lf, 'messageSends: ';
  884. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  885. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  886. nextPutAll: 'referencedClasses: ['.
  887. aMethod classReferences
  888. do: [:each | stream nextPutAll: each asJavascript]
  889. separatedBy: [stream nextPutAll: ','].
  890. stream
  891. nextPutAll: ']';
  892. nextPutAll: '})'
  893. !
  894. nextPutNonLocalReturnHandlingWith: aBlock
  895. stream
  896. nextPutAll: 'var $early={};'; lf;
  897. nextPutAll: 'try {'; lf.
  898. aBlock value.
  899. stream
  900. nextPutAll: '}'; lf;
  901. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  902. !
  903. nextPutNonLocalReturnWith: aBlock
  904. stream nextPutAll: 'throw $early=['.
  905. aBlock value.
  906. stream nextPutAll: ']'
  907. !
  908. nextPutReturn
  909. stream nextPutAll: 'return '
  910. !
  911. nextPutReturnWith: aBlock
  912. self nextPutReturn.
  913. aBlock value
  914. !
  915. nextPutSequenceWith: aBlock
  916. "stream
  917. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  918. aBlock value.
  919. "stream
  920. nextPutAll: '};'; lf"
  921. !
  922. nextPutStatement: anInteger with: aBlock
  923. stream nextPutAll: 'case ', anInteger asString, ':'; lf.
  924. self nextPutStatementWith: aBlock.
  925. stream nextPutAll: 'smalltalk.thisContext.pc=', (anInteger + 1) asString, ';'; lf
  926. !
  927. nextPutStatementWith: aBlock
  928. aBlock value.
  929. stream nextPutAll: ';'; lf
  930. !
  931. nextPutVars: aCollection
  932. aCollection ifEmpty: [ ^self ].
  933. stream nextPutAll: 'var '.
  934. aCollection
  935. do: [ :each | stream nextPutAll: each ]
  936. separatedBy: [ stream nextPutAll: ',' ].
  937. stream nextPutAll: ';'; lf
  938. ! !
  939. !BlockClosure methodsFor: '*Compiler-IR'!
  940. appendToInstruction: anIRInstruction
  941. anIRInstruction appendBlock: self
  942. ! !
  943. !String methodsFor: '*Compiler-IR'!
  944. asVariableName
  945. ^ (Smalltalk current reservedWords includes: self)
  946. ifTrue: [ self, '_' ]
  947. ifFalse: [ self ]
  948. ! !