Compiler-IR.st 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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: anIRInstruction
  236. self dagChildren remove: anIRInstruction
  237. !
  238. replace: anIRInstruction with: anotherIRInstruction
  239. anotherIRInstruction parent: self.
  240. self dagChildren
  241. at: (self dagChildren indexOf: anIRInstruction)
  242. put: anotherIRInstruction
  243. !
  244. replaceWith: anIRInstruction
  245. self parent replace: self with: anIRInstruction
  246. ! !
  247. !IRInstruction methodsFor: 'testing'!
  248. isClosure
  249. ^ false
  250. !
  251. isInlined
  252. ^ false
  253. !
  254. isMethod
  255. ^ false
  256. !
  257. isSelf
  258. ^ false
  259. !
  260. isSend
  261. ^ false
  262. !
  263. isSequence
  264. ^ false
  265. !
  266. isSuper
  267. ^ false
  268. !
  269. isTempDeclaration
  270. ^ false
  271. !
  272. isVariable
  273. ^ false
  274. !
  275. needsBoxingAsReceiver
  276. ^ true
  277. !
  278. yieldsValue
  279. ^ true
  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: 'accessing'!
  291. left
  292. ^ self dagChildren first
  293. !
  294. right
  295. ^ self dagChildren last
  296. ! !
  297. !IRAssignment methodsFor: 'visiting'!
  298. acceptDagVisitor: aVisitor
  299. ^ aVisitor visitIRAssignment: self
  300. ! !
  301. IRInstruction subclass: #IRDynamicArray
  302. instanceVariableNames: ''
  303. package: 'Compiler-IR'!
  304. !IRDynamicArray methodsFor: 'visiting'!
  305. acceptDagVisitor: aVisitor
  306. ^ aVisitor visitIRDynamicArray: self
  307. ! !
  308. IRInstruction subclass: #IRDynamicDictionary
  309. instanceVariableNames: ''
  310. package: 'Compiler-IR'!
  311. !IRDynamicDictionary methodsFor: 'visiting'!
  312. acceptDagVisitor: aVisitor
  313. ^ aVisitor visitIRDynamicDictionary: self
  314. ! !
  315. IRInstruction subclass: #IRScopedInstruction
  316. instanceVariableNames: 'scope'
  317. package: 'Compiler-IR'!
  318. !IRScopedInstruction methodsFor: 'accessing'!
  319. scope
  320. ^ scope
  321. !
  322. scope: aScope
  323. scope := aScope
  324. ! !
  325. IRScopedInstruction subclass: #IRClosureInstruction
  326. instanceVariableNames: 'arguments requiresSmalltalkContext'
  327. package: 'Compiler-IR'!
  328. !IRClosureInstruction methodsFor: 'accessing'!
  329. arguments
  330. ^ arguments ifNil: [ #() ]
  331. !
  332. arguments: aCollection
  333. arguments := aCollection
  334. !
  335. locals
  336. ^ self arguments copy
  337. addAll: (self tempDeclarations collect: [ :each | each name ]);
  338. yourself
  339. !
  340. requiresSmalltalkContext
  341. ^ requiresSmalltalkContext ifNil: [ false ]
  342. !
  343. requiresSmalltalkContext: anObject
  344. requiresSmalltalkContext := anObject
  345. !
  346. scope: aScope
  347. super scope: aScope.
  348. aScope instruction: self
  349. !
  350. tempDeclarations
  351. ^ self dagChildren select: [ :each |
  352. each isTempDeclaration ]
  353. ! !
  354. IRClosureInstruction subclass: #IRClosure
  355. instanceVariableNames: ''
  356. package: 'Compiler-IR'!
  357. !IRClosure methodsFor: 'accessing'!
  358. sequence
  359. ^ self dagChildren last
  360. ! !
  361. !IRClosure methodsFor: 'testing'!
  362. isClosure
  363. ^ true
  364. ! !
  365. !IRClosure methodsFor: 'visiting'!
  366. acceptDagVisitor: aVisitor
  367. ^ aVisitor visitIRClosure: self
  368. ! !
  369. IRClosureInstruction subclass: #IRMethod
  370. instanceVariableNames: 'theClass source selector classReferences sendIndexes requiresSmalltalkContext internalVariables'
  371. package: 'Compiler-IR'!
  372. !IRMethod commentStamp!
  373. I am a method instruction!
  374. !IRMethod methodsFor: 'accessing'!
  375. classReferences
  376. ^ classReferences
  377. !
  378. classReferences: aCollection
  379. classReferences := aCollection
  380. !
  381. internalVariables
  382. ^ internalVariables ifNil: [ internalVariables := Set new ]
  383. !
  384. messageSends
  385. ^ self sendIndexes keys
  386. !
  387. method
  388. ^ self
  389. !
  390. selector
  391. ^ selector
  392. !
  393. selector: aString
  394. selector := aString
  395. !
  396. sendIndexes
  397. ^ sendIndexes
  398. !
  399. sendIndexes: aDictionary
  400. sendIndexes := aDictionary
  401. !
  402. source
  403. ^ source
  404. !
  405. source: aString
  406. source := aString
  407. !
  408. theClass
  409. ^ theClass
  410. !
  411. theClass: aClass
  412. theClass := aClass
  413. ! !
  414. !IRMethod methodsFor: 'testing'!
  415. isMethod
  416. ^ true
  417. ! !
  418. !IRMethod methodsFor: 'visiting'!
  419. acceptDagVisitor: aVisitor
  420. ^ aVisitor visitIRMethod: self
  421. ! !
  422. IRScopedInstruction subclass: #IRReturn
  423. instanceVariableNames: ''
  424. package: 'Compiler-IR'!
  425. !IRReturn commentStamp!
  426. I am a local return instruction.!
  427. !IRReturn methodsFor: 'accessing'!
  428. expression
  429. ^ self dagChildren single
  430. !
  431. scope
  432. ^ scope ifNil: [ self parent scope ]
  433. ! !
  434. !IRReturn methodsFor: 'testing'!
  435. yieldsValue
  436. ^ false
  437. ! !
  438. !IRReturn methodsFor: 'visiting'!
  439. acceptDagVisitor: aVisitor
  440. ^ aVisitor visitIRReturn: self
  441. ! !
  442. IRReturn subclass: #IRBlockReturn
  443. instanceVariableNames: ''
  444. package: 'Compiler-IR'!
  445. !IRBlockReturn commentStamp!
  446. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  447. !IRBlockReturn methodsFor: 'visiting'!
  448. acceptDagVisitor: aVisitor
  449. ^ aVisitor visitIRBlockReturn: self
  450. ! !
  451. IRReturn subclass: #IRNonLocalReturn
  452. instanceVariableNames: ''
  453. package: 'Compiler-IR'!
  454. !IRNonLocalReturn commentStamp!
  455. I am a non local return instruction.
  456. Non local returns are handled using a try/catch JavaScript statement.
  457. See `IRNonLocalReturnHandling` class.!
  458. !IRNonLocalReturn methodsFor: 'visiting'!
  459. acceptDagVisitor: aVisitor
  460. ^ aVisitor visitIRNonLocalReturn: self
  461. ! !
  462. IRScopedInstruction subclass: #IRTempDeclaration
  463. instanceVariableNames: 'name'
  464. package: 'Compiler-IR'!
  465. !IRTempDeclaration methodsFor: 'accessing'!
  466. name
  467. ^ name
  468. !
  469. name: aString
  470. name := aString
  471. ! !
  472. !IRTempDeclaration methodsFor: 'testing'!
  473. isTempDeclaration
  474. ^ true
  475. ! !
  476. !IRTempDeclaration methodsFor: 'visiting'!
  477. acceptDagVisitor: aVisitor
  478. ^ aVisitor visitIRTempDeclaration: self
  479. ! !
  480. IRInstruction subclass: #IRSend
  481. instanceVariableNames: 'selector index'
  482. package: 'Compiler-IR'!
  483. !IRSend commentStamp!
  484. I am a message send instruction.!
  485. !IRSend methodsFor: 'accessing'!
  486. arguments
  487. ^ self dagChildren allButFirst
  488. !
  489. index
  490. ^ index
  491. !
  492. index: anInteger
  493. index := anInteger
  494. !
  495. receiver
  496. ^ self dagChildren first
  497. !
  498. selector
  499. ^ selector
  500. !
  501. selector: aString
  502. selector := aString
  503. ! !
  504. !IRSend methodsFor: 'testing'!
  505. isSend
  506. ^ true
  507. ! !
  508. !IRSend methodsFor: 'visiting'!
  509. acceptDagVisitor: aVisitor
  510. ^ aVisitor visitIRSend: self
  511. ! !
  512. IRInstruction subclass: #IRSequence
  513. instanceVariableNames: ''
  514. package: 'Compiler-IR'!
  515. !IRSequence methodsFor: 'testing'!
  516. isSequence
  517. ^ true
  518. ! !
  519. !IRSequence methodsFor: 'visiting'!
  520. acceptDagVisitor: aVisitor
  521. ^ aVisitor visitIRSequence: self
  522. ! !
  523. IRSequence subclass: #IRBlockSequence
  524. instanceVariableNames: ''
  525. package: 'Compiler-IR'!
  526. !IRBlockSequence methodsFor: 'visiting'!
  527. acceptDagVisitor: aVisitor
  528. ^ aVisitor visitIRBlockSequence: self
  529. ! !
  530. IRInstruction subclass: #IRValue
  531. instanceVariableNames: 'value'
  532. package: 'Compiler-IR'!
  533. !IRValue commentStamp!
  534. I am the simplest possible instruction. I represent a value.!
  535. !IRValue methodsFor: 'accessing'!
  536. value
  537. ^ value
  538. !
  539. value: aString
  540. value := aString
  541. ! !
  542. !IRValue methodsFor: 'testing'!
  543. needsBoxingAsReceiver
  544. ^ false
  545. ! !
  546. !IRValue methodsFor: 'visiting'!
  547. acceptDagVisitor: aVisitor
  548. ^ aVisitor visitIRValue: self
  549. ! !
  550. IRInstruction subclass: #IRVariable
  551. instanceVariableNames: 'variable'
  552. package: 'Compiler-IR'!
  553. !IRVariable commentStamp!
  554. I am a variable instruction.!
  555. !IRVariable methodsFor: 'accessing'!
  556. variable
  557. ^ variable
  558. !
  559. variable: aScopeVariable
  560. variable := aScopeVariable
  561. ! !
  562. !IRVariable methodsFor: 'testing'!
  563. isSelf
  564. ^ self variable isSelf
  565. !
  566. isSuper
  567. ^ self variable isSuper
  568. !
  569. isVariable
  570. ^ true
  571. !
  572. needsBoxingAsReceiver
  573. ^ self variable isPseudoVar not
  574. ! !
  575. !IRVariable methodsFor: 'visiting'!
  576. acceptDagVisitor: aVisitor
  577. ^ aVisitor visitIRVariable: self
  578. ! !
  579. IRInstruction subclass: #IRVerbatim
  580. instanceVariableNames: 'source'
  581. package: 'Compiler-IR'!
  582. !IRVerbatim methodsFor: 'accessing'!
  583. source
  584. ^ source
  585. !
  586. source: aString
  587. source := aString
  588. ! !
  589. !IRVerbatim methodsFor: 'visiting'!
  590. acceptDagVisitor: aVisitor
  591. ^ aVisitor visitIRVerbatim: self
  592. ! !
  593. ParentFakingPathDagVisitor subclass: #IRVisitor
  594. instanceVariableNames: ''
  595. package: 'Compiler-IR'!
  596. !IRVisitor methodsFor: 'visiting'!
  597. visitDagNode: aNode
  598. ^ self visitDagNodeVariantSimple: aNode
  599. !
  600. visitIRAssignment: anIRAssignment
  601. ^ self visitDagNode: anIRAssignment
  602. !
  603. visitIRBlockReturn: anIRBlockReturn
  604. ^ self visitIRReturn: anIRBlockReturn
  605. !
  606. visitIRBlockSequence: anIRBlockSequence
  607. ^ self visitIRSequence: anIRBlockSequence
  608. !
  609. visitIRClosure: anIRClosure
  610. ^ self visitDagNode: anIRClosure
  611. !
  612. visitIRDynamicArray: anIRDynamicArray
  613. ^ self visitDagNode: anIRDynamicArray
  614. !
  615. visitIRDynamicDictionary: anIRDynamicDictionary
  616. ^ self visitDagNode: anIRDynamicDictionary
  617. !
  618. visitIRInlinedClosure: anIRInlinedClosure
  619. ^ self visitIRClosure: anIRInlinedClosure
  620. !
  621. visitIRInlinedSequence: anIRInlinedSequence
  622. ^ self visitIRSequence: anIRInlinedSequence
  623. !
  624. visitIRMethod: anIRMethod
  625. ^ self visitDagNode: anIRMethod
  626. !
  627. visitIRNonLocalReturn: anIRNonLocalReturn
  628. ^ self visitDagNode: anIRNonLocalReturn
  629. !
  630. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  631. ^ self visitDagNode: anIRNonLocalReturnHandling
  632. !
  633. visitIRReturn: anIRReturn
  634. ^ self visitDagNode: anIRReturn
  635. !
  636. visitIRSend: anIRSend
  637. ^ self visitDagNode: anIRSend
  638. !
  639. visitIRSequence: anIRSequence
  640. ^ self visitDagNode: anIRSequence
  641. !
  642. visitIRTempDeclaration: anIRTempDeclaration
  643. ^ self visitDagNode: anIRTempDeclaration
  644. !
  645. visitIRValue: anIRValue
  646. ^ self visitDagNode: anIRValue
  647. !
  648. visitIRVariable: anIRVariable
  649. ^ self visitDagNode: anIRVariable
  650. !
  651. visitIRVerbatim: anIRVerbatim
  652. ^ self visitDagNode: anIRVerbatim
  653. ! !
  654. IRVisitor subclass: #IRJSTranslator
  655. instanceVariableNames: 'stream currentClass'
  656. package: 'Compiler-IR'!
  657. !IRJSTranslator methodsFor: 'accessing'!
  658. contents
  659. ^ self stream contents
  660. !
  661. currentClass
  662. ^ currentClass
  663. !
  664. currentClass: aClass
  665. currentClass := aClass
  666. !
  667. stream
  668. ^ stream
  669. !
  670. stream: aStream
  671. stream := aStream
  672. ! !
  673. !IRJSTranslator methodsFor: 'initialization'!
  674. initialize
  675. super initialize.
  676. stream := JSStream new.
  677. ! !
  678. !IRJSTranslator methodsFor: 'visiting'!
  679. visitIRAssignment: anIRAssignment
  680. self stream
  681. nextPutAssignLhs: [self visit: anIRAssignment left]
  682. rhs: [self visit: anIRAssignment right].
  683. !
  684. visitIRClosure: anIRClosure
  685. self stream
  686. nextPutClosureWith: [
  687. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  688. each name asVariableName ]).
  689. self stream
  690. nextPutBlockContextFor: anIRClosure
  691. during: [ super visitIRClosure: anIRClosure ] ]
  692. arguments: anIRClosure arguments
  693. !
  694. visitIRDynamicArray: anIRDynamicArray
  695. self
  696. visitInstructionList: anIRDynamicArray dagChildren
  697. enclosedBetween: '[' and: ']'
  698. !
  699. visitIRDynamicDictionary: anIRDynamicDictionary
  700. self
  701. visitInstructionList: anIRDynamicDictionary dagChildren
  702. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  703. !
  704. visitIRMethod: anIRMethod
  705. self stream
  706. nextPutMethodDeclaration: anIRMethod
  707. with: [ self stream
  708. nextPutFunctionWith: [
  709. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  710. each name asVariableName ]).
  711. self stream nextPutContextFor: anIRMethod during: [
  712. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  713. self stream nextPutVars:
  714. (internalVars asSet collect: [ :each | each variable alias ]) ].
  715. anIRMethod scope hasNonLocalReturn
  716. ifTrue: [
  717. self stream nextPutNonLocalReturnHandlingWith: [
  718. super visitIRMethod: anIRMethod ] ]
  719. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  720. arguments: anIRMethod arguments ].
  721. ^ self contents
  722. !
  723. visitIRNonLocalReturn: anIRNonLocalReturn
  724. self stream nextPutNonLocalReturnWith: [
  725. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  726. !
  727. visitIRReturn: anIRReturn
  728. self stream nextPutReturnWith: [
  729. super visitIRReturn: anIRReturn ]
  730. !
  731. visitIRSend: anIRSend
  732. | sends superclass |
  733. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  734. anIRSend receiver isSuper
  735. ifTrue: [ self visitSuperSend: anIRSend ]
  736. ifFalse: [ self visitSend: anIRSend ].
  737. anIRSend index < sends
  738. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  739. !
  740. visitIRSequence: anIRSequence
  741. anIRSequence dagChildren do: [ :each |
  742. self stream nextPutStatementWith: [ self visit: each ] ]
  743. !
  744. visitIRTempDeclaration: anIRTempDeclaration
  745. "self stream
  746. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  747. lf"
  748. !
  749. visitIRValue: anIRValue
  750. self stream nextPutAll: anIRValue value asJavaScriptSource
  751. !
  752. visitIRVariable: anIRVariable
  753. anIRVariable variable name = 'thisContext'
  754. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  755. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  756. !
  757. visitIRVerbatim: anIRVerbatim
  758. self stream nextPutStatementWith: [
  759. self stream nextPutAll: anIRVerbatim source ]
  760. !
  761. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  762. self stream nextPutAll: aString.
  763. anArray
  764. do: [ :each | self visit: each ]
  765. separatedBy: [ self stream nextPutAll: ',' ].
  766. stream nextPutAll: anotherString
  767. !
  768. visitReceiver: anIRInstruction
  769. | instr |
  770. anIRInstruction isSelf
  771. ifTrue: [ instr := anIRInstruction copy
  772. variable: (anIRInstruction variable copy name: '$self'; yourself);
  773. yourself ]
  774. ifFalse: [ instr := anIRInstruction ].
  775. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  776. self stream nextPutAll: '$recv('.
  777. self visit: instr.
  778. self stream nextPutAll: ')'
  779. !
  780. visitSend: anIRSend
  781. self visitReceiver: anIRSend receiver.
  782. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  783. self
  784. visitInstructionList: anIRSend arguments
  785. enclosedBetween: '(' and: ')'
  786. !
  787. visitSuperSend: anIRSend
  788. self stream
  789. nextPutAll: '('; lf;
  790. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  791. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  792. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  793. nextPutAll: '(', self currentClass asJavaScriptSource;
  794. nextPutAll: '.superclass||$boot.nilAsClass).fn.prototype.';
  795. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  796. nextPutAll: '$self, '.
  797. self
  798. visitInstructionList: anIRSend arguments
  799. enclosedBetween: '[' and: ']'.
  800. self stream
  801. nextPutAll: '));'; lf;
  802. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  803. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  804. nextPutAll: '//>>excludeEnd("ctx");'
  805. ! !
  806. Object subclass: #JSStream
  807. instanceVariableNames: 'stream omitSemicolon'
  808. package: 'Compiler-IR'!
  809. !JSStream methodsFor: 'accessing'!
  810. contents
  811. ^ stream contents
  812. !
  813. omitSemicolon
  814. ^ omitSemicolon
  815. !
  816. omitSemicolon: aBoolean
  817. omitSemicolon := aBoolean
  818. ! !
  819. !JSStream methodsFor: 'initialization'!
  820. initialize
  821. super initialize.
  822. stream := '' writeStream.
  823. ! !
  824. !JSStream methodsFor: 'streaming'!
  825. lf
  826. stream lf
  827. !
  828. nextPut: aString
  829. stream nextPut: aString
  830. !
  831. nextPutAll: aString
  832. stream nextPutAll: aString
  833. !
  834. nextPutAssignLhs: aBlock rhs: anotherBlock
  835. aBlock value.
  836. stream nextPutAll: '='.
  837. anotherBlock value
  838. !
  839. nextPutBlockContextFor: anIRClosure during: aBlock
  840. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  841. self
  842. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  843. lf;
  844. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  845. lf;
  846. nextPutAll: '//>>excludeEnd("ctx");';
  847. lf.
  848. aBlock value.
  849. self
  850. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  851. lf;
  852. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  853. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  854. anIRClosure locals
  855. do: [ :each |
  856. self
  857. nextPutAll: each asVariableName;
  858. nextPutAll: ':';
  859. nextPutAll: each asVariableName ]
  860. separatedBy: [ self nextPutAll: ',' ].
  861. self
  862. nextPutAll: '},';
  863. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  864. lf;
  865. nextPutAll: '//>>excludeEnd("ctx");'
  866. !
  867. nextPutClosureWith: aBlock arguments: anArray
  868. stream nextPutAll: '(function('.
  869. anArray
  870. do: [ :each | stream nextPutAll: each asVariableName ]
  871. separatedBy: [ stream nextPut: ',' ].
  872. stream nextPutAll: '){'; lf.
  873. aBlock value.
  874. stream lf; nextPutAll: '})'
  875. !
  876. nextPutContextFor: aMethod during: aBlock
  877. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  878. self
  879. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  880. lf;
  881. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  882. lf;
  883. nextPutAll: '//>>excludeEnd("ctx");';
  884. lf.
  885. aBlock value.
  886. self
  887. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  888. lf;
  889. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  890. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  891. aMethod locals
  892. do: [ :each |
  893. self
  894. nextPutAll: each asVariableName;
  895. nextPutAll: ':';
  896. nextPutAll: each asVariableName ]
  897. separatedBy: [ self nextPutAll: ',' ].
  898. self
  899. nextPutAll: '},';
  900. nextPutAll: aMethod theClass asJavaScriptSource;
  901. nextPutAll: ')});';
  902. lf;
  903. nextPutAll: '//>>excludeEnd("ctx");'
  904. !
  905. nextPutFunctionWith: aBlock arguments: anArray
  906. stream nextPutAll: 'fn: function('.
  907. anArray
  908. do: [ :each | stream nextPutAll: each asVariableName ]
  909. separatedBy: [ stream nextPut: ',' ].
  910. stream nextPutAll: '){'; lf.
  911. stream nextPutAll: 'var self=this,$self=this;'; lf.
  912. aBlock value.
  913. stream lf; nextPutAll: '}'
  914. !
  915. nextPutIf: aBlock then: anotherBlock
  916. stream nextPutAll: 'if('.
  917. aBlock value.
  918. stream nextPutAll: '){'; lf.
  919. anotherBlock value.
  920. stream nextPutAll: '}'.
  921. self omitSemicolon: true
  922. !
  923. nextPutIf: aBlock then: ifBlock else: elseBlock
  924. stream nextPutAll: 'if('.
  925. aBlock value.
  926. stream nextPutAll: '){'; lf.
  927. ifBlock value.
  928. stream nextPutAll: '} else {'; lf.
  929. elseBlock value.
  930. stream nextPutAll: '}'.
  931. self omitSemicolon: true
  932. !
  933. nextPutMethodDeclaration: aMethod with: aBlock
  934. stream
  935. nextPutAll: '$core.method({'; lf;
  936. nextPutAll: 'selector: ', aMethod selector asJavaScriptSource, ','; lf;
  937. nextPutAll: 'source: ', aMethod source asJavaScriptSource, ',';lf.
  938. aBlock value.
  939. stream
  940. nextPutAll: ',', String lf, 'messageSends: ';
  941. nextPutAll: aMethod messageSends asArray asJavaScriptSource, ','; lf;
  942. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavaScriptSource, ','; lf;
  943. nextPutAll: 'referencedClasses: ['.
  944. aMethod classReferences
  945. do: [ :each | stream nextPutAll: each asJavaScriptSource ]
  946. separatedBy: [ stream nextPutAll: ',' ].
  947. stream
  948. nextPutAll: ']';
  949. nextPutAll: '})'
  950. !
  951. nextPutNonLocalReturnHandlingWith: aBlock
  952. stream
  953. nextPutAll: 'var $early={};'; lf;
  954. nextPutAll: 'try {'; lf.
  955. aBlock value.
  956. stream
  957. nextPutAll: '}'; lf;
  958. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  959. !
  960. nextPutNonLocalReturnWith: aBlock
  961. stream nextPutAll: 'throw $early=['.
  962. aBlock value.
  963. stream nextPutAll: ']'
  964. !
  965. nextPutReturnWith: aBlock
  966. stream nextPutAll: 'return '.
  967. aBlock value
  968. !
  969. nextPutSendIndexFor: anIRSend
  970. self
  971. nextPutAll: ';'; lf;
  972. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  973. nextPutAll: anIRSend scope alias;
  974. nextPutAll: '.sendIdx[';
  975. nextPutAll: anIRSend selector asJavaScriptSource;
  976. nextPutAll: ']=';
  977. nextPutAll: anIRSend index asString;
  978. nextPutAll: ';'; lf;
  979. nextPutAll: '//>>excludeEnd("ctx")'
  980. !
  981. nextPutStatementWith: aBlock
  982. self omitSemicolon: false.
  983. aBlock value.
  984. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  985. self omitSemicolon: false.
  986. stream lf
  987. !
  988. nextPutVars: aCollection
  989. aCollection ifNotEmpty: [
  990. stream nextPutAll: 'var '.
  991. aCollection
  992. do: [ :each | stream nextPutAll: each ]
  993. separatedBy: [ stream nextPutAll: ',' ].
  994. stream nextPutAll: ';'; lf ]
  995. ! !
  996. !ASTNode methodsFor: '*Compiler-IR'!
  997. isReferenced
  998. "Answer true if the receiver is referenced by other nodes.
  999. Do not take sequences or assignments into account"
  1000. ^ (self parent isSequenceNode or: [
  1001. self parent isAssignmentNode ]) not
  1002. !
  1003. subtreeNeedsAliasing
  1004. ^ self shouldBeAliased or: [
  1005. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1006. ! !
  1007. !AssignmentNode methodsFor: '*Compiler-IR'!
  1008. shouldBeAliased
  1009. ^ super shouldBeAliased or: [ self isReferenced ]
  1010. ! !
  1011. !BlockClosure methodsFor: '*Compiler-IR'!
  1012. appendToInstruction: anIRInstruction
  1013. anIRInstruction appendBlock: self
  1014. ! !
  1015. !BlockNode methodsFor: '*Compiler-IR'!
  1016. subtreeNeedsAliasing
  1017. ^ self shouldBeAliased
  1018. ! !
  1019. !CascadeNode methodsFor: '*Compiler-IR'!
  1020. subtreeNeedsAliasing
  1021. ^ self parent isSequenceNode not
  1022. ! !
  1023. !SendNode methodsFor: '*Compiler-IR'!
  1024. shouldBeAliased
  1025. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  1026. See IRJSVisitor >> visitIRSend:"
  1027. | sends |
  1028. sends := (self method sendIndexes at: self selector) size.
  1029. ^ (super shouldBeAliased or: [
  1030. self isReferenced and: [
  1031. self index < sends or: [
  1032. self superSend ] ] ])
  1033. !
  1034. subtreeNeedsAliasing
  1035. ^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
  1036. ! !