Compiler-IR.st 26 KB

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