1
0

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