Compiler-IR.st 25 KB

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