1
0

Compiler-IR.st 24 KB

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