Compiler-IR.st 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. addToSequence: anInstruction
  47. anInstruction ifNotNil: [
  48. anInstruction isVariable ifFalse: [
  49. self sequence add: anInstruction ] ].
  50. ^ anInstruction
  51. !
  52. alias: aNode
  53. | variable |
  54. aNode isImmutable ifTrue: [ ^ self visit: aNode ].
  55. variable := IRVariable new
  56. variable: (AliasVar new name: '$', self nextAlias);
  57. yourself.
  58. self addToSequence: (IRAssignment new
  59. add: variable;
  60. add: (self visit: aNode);
  61. yourself).
  62. self method internalVariables add: variable.
  63. ^ variable
  64. !
  65. aliasTemporally: aCollection
  66. "https://lolg.it/amber/amber/issues/296
  67. If a node is aliased, all preceding ones are aliased as well.
  68. The tree is iterated twice. First we get the aliasing dependency,
  69. then the aliasing itself is done"
  70. | threshold result |
  71. threshold := 0.
  72. aCollection withIndexDo: [ :each :i |
  73. each subtreeNeedsAliasing
  74. ifTrue: [ threshold := i ] ].
  75. result := OrderedCollection new.
  76. aCollection withIndexDo: [ :each :i |
  77. result add: (i <= threshold
  78. ifTrue: [ self alias: each ]
  79. ifFalse: [ self visit: each ]) ].
  80. ^ result
  81. !
  82. visitAssignmentNode: aNode
  83. | left right assignment |
  84. right := self visit: aNode right.
  85. left := self visit: aNode left.
  86. self addToSequence: (IRAssignment new
  87. add: left;
  88. add: right;
  89. yourself).
  90. ^ left
  91. !
  92. visitBlockNode: aNode
  93. | closure |
  94. closure := IRClosure new
  95. arguments: aNode parameters;
  96. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  97. scope: aNode scope;
  98. yourself.
  99. aNode scope temps do: [ :each |
  100. closure add: (IRTempDeclaration new
  101. name: each name;
  102. scope: aNode scope;
  103. yourself) ].
  104. aNode dagChildren do: [ :each | closure add: (self visit: each) ].
  105. ^ closure
  106. !
  107. visitBlockSequenceNode: aNode
  108. ^ self
  109. withSequence: IRBlockSequence new
  110. do: [
  111. aNode dagChildren ifNotEmpty: [
  112. aNode dagChildren allButLast do: [ :each |
  113. self addToSequence: (self visitOrAlias: each) ].
  114. aNode dagChildren last isReturnNode
  115. ifFalse: [ self addToSequence: (IRBlockReturn new add: (self visitOrAlias: aNode dagChildren last); yourself) ]
  116. ifTrue: [ self addToSequence: (self visitOrAlias: aNode dagChildren last) ] ]]
  117. !
  118. visitCascadeNode: aNode
  119. | receiver |
  120. receiver := aNode receiver.
  121. receiver isImmutable ifFalse: [
  122. | alias |
  123. alias := self alias: receiver.
  124. receiver := VariableNode new binding: alias variable ].
  125. aNode dagChildren do: [ :each | each receiver: receiver ].
  126. aNode dagChildren allButLast do: [ :each |
  127. self addToSequence: (self visit: each) ].
  128. ^ self visitOrAlias: aNode dagChildren last
  129. !
  130. visitDynamicArrayNode: aNode
  131. | array |
  132. array := IRDynamicArray new.
  133. (self aliasTemporally: aNode dagChildren) do: [ :each | array add: each ].
  134. ^ array
  135. !
  136. visitDynamicDictionaryNode: aNode
  137. | dictionary |
  138. dictionary := IRDynamicDictionary new.
  139. (self aliasTemporally: aNode dagChildren) do: [ :each | dictionary add: each ].
  140. ^ dictionary
  141. !
  142. visitJSStatementNode: aNode
  143. ^ IRVerbatim new
  144. source: aNode source crlfSanitized;
  145. yourself
  146. !
  147. visitMethodNode: aNode
  148. self method: (IRMethod new
  149. source: self source crlfSanitized;
  150. theClass: self theClass;
  151. arguments: aNode arguments;
  152. selector: aNode selector;
  153. sendIndexes: aNode sendIndexes;
  154. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  155. classReferences: aNode classReferences;
  156. scope: aNode scope;
  157. yourself).
  158. aNode scope temps do: [ :each |
  159. self method add: (IRTempDeclaration new
  160. name: each name;
  161. scope: aNode scope;
  162. yourself) ].
  163. aNode dagChildren do: [ :each | self method add: (self visit: each) ].
  164. aNode scope hasLocalReturn ifFalse: [self method
  165. add: (IRReturn new
  166. add: (IRVariable new
  167. variable: (aNode scope pseudoVars at: 'self');
  168. yourself);
  169. yourself);
  170. add: (IRVerbatim new source: ';', String lf; yourself) ].
  171. ^ self method
  172. !
  173. visitOrAlias: aNode
  174. ^ aNode shouldBeAliased
  175. ifTrue: [ self alias: aNode ]
  176. ifFalse: [ self visit: aNode ]
  177. !
  178. visitReturnNode: aNode
  179. | return |
  180. return := aNode nonLocalReturn
  181. ifTrue: [ IRNonLocalReturn new ]
  182. ifFalse: [ IRReturn new ].
  183. return scope: aNode scope.
  184. aNode dagChildren do: [ :each |
  185. return add: (self visitOrAlias: each) ].
  186. ^ return
  187. !
  188. visitSendNode: aNode
  189. | send |
  190. send := IRSend new.
  191. send
  192. selector: aNode selector;
  193. index: aNode index.
  194. (self aliasTemporally: aNode dagChildren) do: [ :each | send add: each ].
  195. ^ send
  196. !
  197. visitSequenceNode: aNode
  198. ^ self
  199. withSequence: IRSequence new
  200. do: [ aNode dagChildren do: [ :each |
  201. self addToSequence: (self visitOrAlias: each) ] ]
  202. !
  203. visitValueNode: aNode
  204. ^ IRValue new
  205. value: aNode value;
  206. yourself
  207. !
  208. visitVariableNode: aNode
  209. ^ IRVariable new
  210. variable: aNode binding;
  211. yourself
  212. ! !
  213. DagParentNode subclass: #IRInstruction
  214. instanceVariableNames: 'parent'
  215. package: 'Compiler-IR'!
  216. !IRInstruction commentStamp!
  217. I am the abstract root class of the IR (intermediate representation) instructions class hierarchy.
  218. The IR graph is used to emit JavaScript code using a JSStream.!
  219. !IRInstruction methodsFor: 'accessing'!
  220. method
  221. ^ self parent method
  222. !
  223. parent
  224. ^ parent
  225. !
  226. parent: anIRInstruction
  227. parent := anIRInstruction
  228. !
  229. scope
  230. ^ self parent ifNotNil: [ :node |
  231. node scope ]
  232. ! !
  233. !IRInstruction methodsFor: 'building'!
  234. add: anObject
  235. anObject parent: self.
  236. ^ self dagChildren add: anObject
  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. isClosure
  252. ^ false
  253. !
  254. isInlined
  255. ^ false
  256. !
  257. isMethod
  258. ^ false
  259. !
  260. isSelf
  261. ^ false
  262. !
  263. isSend
  264. ^ false
  265. !
  266. isSequence
  267. ^ false
  268. !
  269. isSuper
  270. ^ false
  271. !
  272. isTempDeclaration
  273. ^ false
  274. !
  275. isVariable
  276. ^ false
  277. !
  278. needsBoxingAsReceiver
  279. ^ true
  280. !
  281. yieldsValue
  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. yieldsValue
  439. ^ false
  440. ! !
  441. !IRReturn methodsFor: 'visiting'!
  442. acceptDagVisitor: aVisitor
  443. ^ aVisitor visitIRReturn: self
  444. ! !
  445. IRReturn subclass: #IRBlockReturn
  446. instanceVariableNames: ''
  447. package: 'Compiler-IR'!
  448. !IRBlockReturn commentStamp!
  449. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  450. !IRBlockReturn methodsFor: 'visiting'!
  451. acceptDagVisitor: aVisitor
  452. ^ aVisitor visitIRBlockReturn: self
  453. ! !
  454. IRReturn subclass: #IRNonLocalReturn
  455. instanceVariableNames: ''
  456. package: 'Compiler-IR'!
  457. !IRNonLocalReturn commentStamp!
  458. I am a non local return instruction.
  459. Non local returns are handled using a try/catch JavaScript statement.
  460. See `IRNonLocalReturnHandling` class.!
  461. !IRNonLocalReturn methodsFor: 'visiting'!
  462. acceptDagVisitor: aVisitor
  463. ^ aVisitor visitIRNonLocalReturn: self
  464. ! !
  465. IRScopedInstruction subclass: #IRTempDeclaration
  466. instanceVariableNames: 'name'
  467. package: 'Compiler-IR'!
  468. !IRTempDeclaration methodsFor: 'accessing'!
  469. name
  470. ^ name
  471. !
  472. name: aString
  473. name := aString
  474. ! !
  475. !IRTempDeclaration methodsFor: 'testing'!
  476. isTempDeclaration
  477. ^ true
  478. ! !
  479. !IRTempDeclaration methodsFor: 'visiting'!
  480. acceptDagVisitor: aVisitor
  481. ^ aVisitor visitIRTempDeclaration: self
  482. ! !
  483. IRInstruction subclass: #IRSend
  484. instanceVariableNames: 'selector index'
  485. package: 'Compiler-IR'!
  486. !IRSend commentStamp!
  487. I am a message send instruction.!
  488. !IRSend methodsFor: 'accessing'!
  489. arguments
  490. ^ self dagChildren allButFirst
  491. !
  492. index
  493. ^ index
  494. !
  495. index: anInteger
  496. index := anInteger
  497. !
  498. receiver
  499. ^ self dagChildren first
  500. !
  501. selector
  502. ^ selector
  503. !
  504. selector: aString
  505. selector := aString
  506. ! !
  507. !IRSend methodsFor: 'testing'!
  508. isSend
  509. ^ true
  510. ! !
  511. !IRSend methodsFor: 'visiting'!
  512. acceptDagVisitor: aVisitor
  513. ^ aVisitor visitIRSend: self
  514. ! !
  515. IRInstruction subclass: #IRSequence
  516. instanceVariableNames: ''
  517. package: 'Compiler-IR'!
  518. !IRSequence methodsFor: 'testing'!
  519. isSequence
  520. ^ true
  521. ! !
  522. !IRSequence methodsFor: 'visiting'!
  523. acceptDagVisitor: aVisitor
  524. ^ aVisitor visitIRSequence: self
  525. ! !
  526. IRSequence subclass: #IRBlockSequence
  527. instanceVariableNames: ''
  528. package: 'Compiler-IR'!
  529. !IRBlockSequence methodsFor: 'visiting'!
  530. acceptDagVisitor: aVisitor
  531. ^ aVisitor visitIRBlockSequence: self
  532. ! !
  533. IRInstruction subclass: #IRValue
  534. instanceVariableNames: 'value'
  535. package: 'Compiler-IR'!
  536. !IRValue commentStamp!
  537. I am the simplest possible instruction. I represent a value.!
  538. !IRValue methodsFor: 'accessing'!
  539. value
  540. ^ value
  541. !
  542. value: aString
  543. value := aString
  544. ! !
  545. !IRValue methodsFor: 'testing'!
  546. needsBoxingAsReceiver
  547. ^ false
  548. ! !
  549. !IRValue methodsFor: 'visiting'!
  550. acceptDagVisitor: aVisitor
  551. ^ aVisitor visitIRValue: self
  552. ! !
  553. IRInstruction subclass: #IRVariable
  554. instanceVariableNames: 'variable'
  555. package: 'Compiler-IR'!
  556. !IRVariable commentStamp!
  557. I am a variable instruction.!
  558. !IRVariable methodsFor: 'accessing'!
  559. variable
  560. ^ variable
  561. !
  562. variable: aScopeVariable
  563. variable := aScopeVariable
  564. ! !
  565. !IRVariable methodsFor: 'testing'!
  566. isSelf
  567. ^ self variable isSelf
  568. !
  569. isSuper
  570. ^ self variable isSuper
  571. !
  572. isVariable
  573. ^ true
  574. !
  575. needsBoxingAsReceiver
  576. ^ self variable isPseudoVar not
  577. ! !
  578. !IRVariable methodsFor: 'visiting'!
  579. acceptDagVisitor: aVisitor
  580. ^ aVisitor visitIRVariable: self
  581. ! !
  582. IRInstruction subclass: #IRVerbatim
  583. instanceVariableNames: 'source'
  584. package: 'Compiler-IR'!
  585. !IRVerbatim methodsFor: 'accessing'!
  586. source
  587. ^ source
  588. !
  589. source: aString
  590. source := aString
  591. ! !
  592. !IRVerbatim methodsFor: 'visiting'!
  593. acceptDagVisitor: aVisitor
  594. ^ aVisitor visitIRVerbatim: self
  595. ! !
  596. ParentFakingPathDagVisitor subclass: #IRVisitor
  597. instanceVariableNames: ''
  598. package: 'Compiler-IR'!
  599. !IRVisitor methodsFor: 'visiting'!
  600. visitDagNode: aNode
  601. ^ self visitDagNodeVariantSimple: aNode
  602. !
  603. visitIRAssignment: anIRAssignment
  604. ^ self visitDagNode: anIRAssignment
  605. !
  606. visitIRBlockReturn: anIRBlockReturn
  607. ^ self visitIRReturn: anIRBlockReturn
  608. !
  609. visitIRBlockSequence: anIRBlockSequence
  610. ^ self visitIRSequence: anIRBlockSequence
  611. !
  612. visitIRClosure: anIRClosure
  613. ^ self visitDagNode: anIRClosure
  614. !
  615. visitIRDynamicArray: anIRDynamicArray
  616. ^ self visitDagNode: anIRDynamicArray
  617. !
  618. visitIRDynamicDictionary: anIRDynamicDictionary
  619. ^ self visitDagNode: anIRDynamicDictionary
  620. !
  621. visitIRInlinedClosure: anIRInlinedClosure
  622. ^ self visitIRClosure: anIRInlinedClosure
  623. !
  624. visitIRInlinedSequence: anIRInlinedSequence
  625. ^ self visitIRSequence: anIRInlinedSequence
  626. !
  627. visitIRMethod: anIRMethod
  628. ^ self visitDagNode: anIRMethod
  629. !
  630. visitIRNonLocalReturn: anIRNonLocalReturn
  631. ^ self visitDagNode: anIRNonLocalReturn
  632. !
  633. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  634. ^ self visitDagNode: anIRNonLocalReturnHandling
  635. !
  636. visitIRReturn: anIRReturn
  637. ^ self visitDagNode: anIRReturn
  638. !
  639. visitIRSend: anIRSend
  640. ^ self visitDagNode: anIRSend
  641. !
  642. visitIRSequence: anIRSequence
  643. ^ self visitDagNode: anIRSequence
  644. !
  645. visitIRTempDeclaration: anIRTempDeclaration
  646. ^ self visitDagNode: anIRTempDeclaration
  647. !
  648. visitIRValue: anIRValue
  649. ^ self visitDagNode: anIRValue
  650. !
  651. visitIRVariable: anIRVariable
  652. ^ self visitDagNode: anIRVariable
  653. !
  654. visitIRVerbatim: anIRVerbatim
  655. ^ self visitDagNode: anIRVerbatim
  656. ! !
  657. IRVisitor subclass: #IRJSTranslator
  658. instanceVariableNames: 'stream currentClass'
  659. package: 'Compiler-IR'!
  660. !IRJSTranslator methodsFor: 'accessing'!
  661. contents
  662. ^ self stream contents
  663. !
  664. currentClass
  665. ^ currentClass
  666. !
  667. currentClass: aClass
  668. currentClass := aClass
  669. !
  670. stream
  671. ^ stream
  672. !
  673. stream: aStream
  674. stream := aStream
  675. ! !
  676. !IRJSTranslator methodsFor: 'initialization'!
  677. initialize
  678. super initialize.
  679. stream := JSStream new.
  680. ! !
  681. !IRJSTranslator methodsFor: 'visiting'!
  682. visitIRAssignment: anIRAssignment
  683. self stream
  684. nextPutAssignLhs: [self visit: anIRAssignment left]
  685. rhs: [self visit: anIRAssignment right].
  686. !
  687. visitIRClosure: anIRClosure
  688. self stream
  689. nextPutClosureWith: [
  690. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  691. each name asVariableName ]).
  692. self stream
  693. nextPutBlockContextFor: anIRClosure
  694. during: [ super visitIRClosure: anIRClosure ] ]
  695. arguments: anIRClosure arguments
  696. !
  697. visitIRDynamicArray: anIRDynamicArray
  698. self
  699. visitInstructionList: anIRDynamicArray dagChildren
  700. enclosedBetween: '[' and: ']'
  701. !
  702. visitIRDynamicDictionary: anIRDynamicDictionary
  703. self
  704. visitInstructionList: anIRDynamicDictionary dagChildren
  705. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  706. !
  707. visitIRMethod: anIRMethod
  708. self stream
  709. nextPutMethodDeclaration: anIRMethod
  710. with: [ self stream
  711. nextPutFunctionWith: [
  712. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  713. each name asVariableName ]).
  714. self stream nextPutContextFor: anIRMethod during: [
  715. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  716. self stream nextPutVars:
  717. (internalVars asSet collect: [ :each | each variable alias ]) ].
  718. anIRMethod scope hasNonLocalReturn
  719. ifTrue: [
  720. self stream nextPutNonLocalReturnHandlingWith: [
  721. super visitIRMethod: anIRMethod ] ]
  722. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  723. arguments: anIRMethod arguments ].
  724. ^ self contents
  725. !
  726. visitIRNonLocalReturn: anIRNonLocalReturn
  727. self stream nextPutNonLocalReturnWith: [
  728. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  729. !
  730. visitIRReturn: anIRReturn
  731. self stream nextPutReturnWith: [
  732. super visitIRReturn: anIRReturn ]
  733. !
  734. visitIRSend: anIRSend
  735. | sends superclass |
  736. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  737. anIRSend receiver isSuper
  738. ifTrue: [ self visitSuperSend: anIRSend ]
  739. ifFalse: [ self visitSend: anIRSend ].
  740. anIRSend index < sends
  741. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  742. !
  743. visitIRSequence: anIRSequence
  744. anIRSequence dagChildren do: [ :each |
  745. self stream nextPutStatementWith: [ self visit: each ] ]
  746. !
  747. visitIRTempDeclaration: anIRTempDeclaration
  748. "self stream
  749. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  750. lf"
  751. !
  752. visitIRValue: anIRValue
  753. self stream nextPutAll: anIRValue value asJavaScriptSource
  754. !
  755. visitIRVariable: anIRVariable
  756. anIRVariable variable name = 'thisContext'
  757. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  758. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  759. !
  760. visitIRVerbatim: anIRVerbatim
  761. self stream nextPutAll: anIRVerbatim source
  762. !
  763. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  764. self stream nextPutAll: aString.
  765. anArray
  766. do: [ :each | self visit: each ]
  767. separatedBy: [ self stream nextPutAll: ',' ].
  768. stream nextPutAll: anotherString
  769. !
  770. visitReceiver: anIRInstruction
  771. | instr |
  772. anIRInstruction isSelf
  773. ifTrue: [ instr := anIRInstruction copy
  774. variable: (anIRInstruction variable copy name: '$self'; yourself);
  775. yourself ]
  776. ifFalse: [ instr := anIRInstruction ].
  777. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  778. self stream nextPutAll: '$recv('.
  779. self visit: instr.
  780. self stream nextPutAll: ')'
  781. !
  782. visitSend: anIRSend
  783. self visitReceiver: anIRSend receiver.
  784. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  785. self
  786. visitInstructionList: anIRSend arguments
  787. enclosedBetween: '(' and: ')'
  788. !
  789. visitSuperSend: anIRSend
  790. self stream
  791. nextPutAll: '('; lf;
  792. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  793. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  794. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  795. nextPutAll: '(', self currentClass asJavaScriptSource;
  796. nextPutAll: '.superclass||$boot.nilAsClass).fn.prototype.';
  797. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  798. nextPutAll: '$self, '.
  799. self
  800. visitInstructionList: anIRSend arguments
  801. enclosedBetween: '[' and: ']'.
  802. self stream
  803. nextPutAll: '));'; lf;
  804. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  805. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  806. nextPutAll: '//>>excludeEnd("ctx");'
  807. ! !
  808. Object subclass: #JSStream
  809. instanceVariableNames: 'stream omitSemicolon'
  810. package: 'Compiler-IR'!
  811. !JSStream methodsFor: 'accessing'!
  812. contents
  813. ^ stream contents
  814. !
  815. omitSemicolon
  816. ^ omitSemicolon
  817. !
  818. omitSemicolon: aBoolean
  819. omitSemicolon := aBoolean
  820. ! !
  821. !JSStream methodsFor: 'initialization'!
  822. initialize
  823. super initialize.
  824. stream := '' writeStream.
  825. ! !
  826. !JSStream methodsFor: 'streaming'!
  827. lf
  828. stream lf
  829. !
  830. nextPut: aString
  831. stream nextPut: aString
  832. !
  833. nextPutAll: aString
  834. stream nextPutAll: aString
  835. !
  836. nextPutAssignLhs: aBlock rhs: anotherBlock
  837. aBlock value.
  838. stream nextPutAll: '='.
  839. anotherBlock value
  840. !
  841. nextPutBlockContextFor: anIRClosure during: aBlock
  842. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  843. self
  844. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  845. lf;
  846. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  847. lf;
  848. nextPutAll: '//>>excludeEnd("ctx");';
  849. lf.
  850. aBlock value.
  851. self
  852. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  853. lf;
  854. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  855. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  856. anIRClosure locals
  857. do: [ :each |
  858. self
  859. nextPutAll: each asVariableName;
  860. nextPutAll: ':';
  861. nextPutAll: each asVariableName ]
  862. separatedBy: [ self nextPutAll: ',' ].
  863. self
  864. nextPutAll: '},';
  865. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  866. lf;
  867. nextPutAll: '//>>excludeEnd("ctx");'
  868. !
  869. nextPutClosureWith: aBlock arguments: anArray
  870. stream nextPutAll: '(function('.
  871. anArray
  872. do: [ :each | stream nextPutAll: each asVariableName ]
  873. separatedBy: [ stream nextPut: ',' ].
  874. stream nextPutAll: '){'; lf.
  875. aBlock value.
  876. stream lf; nextPutAll: '})'
  877. !
  878. nextPutContextFor: aMethod during: aBlock
  879. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  880. self
  881. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  882. lf;
  883. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  884. lf;
  885. nextPutAll: '//>>excludeEnd("ctx");';
  886. lf.
  887. aBlock value.
  888. self
  889. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  890. lf;
  891. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  892. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  893. aMethod locals
  894. do: [ :each |
  895. self
  896. nextPutAll: each asVariableName;
  897. nextPutAll: ':';
  898. nextPutAll: each asVariableName ]
  899. separatedBy: [ self nextPutAll: ',' ].
  900. self
  901. nextPutAll: '},';
  902. nextPutAll: aMethod theClass asJavaScriptSource;
  903. nextPutAll: ')});';
  904. lf;
  905. nextPutAll: '//>>excludeEnd("ctx");'
  906. !
  907. nextPutFunctionWith: aBlock arguments: anArray
  908. stream nextPutAll: 'fn: function ('.
  909. anArray
  910. do: [ :each | stream nextPutAll: each asVariableName ]
  911. separatedBy: [ stream nextPut: ',' ].
  912. stream nextPutAll: '){'; lf.
  913. stream nextPutAll: 'var self=this,$self=this;'; lf.
  914. aBlock value.
  915. stream lf; nextPutAll: '}'
  916. !
  917. nextPutIf: aBlock then: anotherBlock
  918. stream nextPutAll: 'if('.
  919. aBlock value.
  920. stream nextPutAll: '){'; lf.
  921. anotherBlock value.
  922. stream nextPutAll: '}'.
  923. self omitSemicolon: true
  924. !
  925. nextPutIf: aBlock then: ifBlock else: elseBlock
  926. stream nextPutAll: 'if('.
  927. aBlock value.
  928. stream nextPutAll: '){'; lf.
  929. ifBlock value.
  930. stream nextPutAll: '} else {'; lf.
  931. elseBlock value.
  932. stream nextPutAll: '}'.
  933. self omitSemicolon: true
  934. !
  935. nextPutMethodDeclaration: aMethod with: aBlock
  936. stream
  937. nextPutAll: '$core.method({'; lf;
  938. nextPutAll: 'selector: ', aMethod selector asJavaScriptSource, ','; lf;
  939. nextPutAll: 'source: ', aMethod source asJavaScriptSource, ',';lf.
  940. aBlock value.
  941. stream
  942. nextPutAll: ',', String lf, 'messageSends: ';
  943. nextPutAll: aMethod messageSends asArray asJavaScriptSource, ','; lf;
  944. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavaScriptSource, ','; lf;
  945. nextPutAll: 'referencedClasses: ['.
  946. aMethod classReferences
  947. do: [ :each | stream nextPutAll: each asJavaScriptSource ]
  948. separatedBy: [ stream nextPutAll: ',' ].
  949. stream
  950. nextPutAll: ']';
  951. nextPutAll: '})'
  952. !
  953. nextPutNonLocalReturnHandlingWith: aBlock
  954. stream
  955. nextPutAll: 'var $early={};'; lf;
  956. nextPutAll: 'try {'; lf.
  957. aBlock value.
  958. stream
  959. nextPutAll: '}'; lf;
  960. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  961. !
  962. nextPutNonLocalReturnWith: aBlock
  963. stream nextPutAll: 'throw $early=['.
  964. aBlock value.
  965. stream nextPutAll: ']'
  966. !
  967. nextPutReturnWith: aBlock
  968. stream nextPutAll: 'return '.
  969. aBlock value
  970. !
  971. nextPutSendIndexFor: anIRSend
  972. self
  973. nextPutAll: ';'; lf;
  974. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  975. nextPutAll: anIRSend scope alias;
  976. nextPutAll: '.sendIdx[';
  977. nextPutAll: anIRSend selector asJavaScriptSource;
  978. nextPutAll: ']=';
  979. nextPutAll: anIRSend index asString;
  980. nextPutAll: ';'; lf;
  981. nextPutAll: '//>>excludeEnd("ctx")'
  982. !
  983. nextPutStatementWith: aBlock
  984. self omitSemicolon: false.
  985. aBlock value.
  986. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  987. self omitSemicolon: false.
  988. stream lf
  989. !
  990. nextPutVars: aCollection
  991. aCollection ifNotEmpty: [
  992. stream nextPutAll: 'var '.
  993. aCollection
  994. do: [ :each | stream nextPutAll: each ]
  995. separatedBy: [ stream nextPutAll: ',' ].
  996. stream nextPutAll: ';'; lf ]
  997. ! !
  998. !ASTNode methodsFor: '*Compiler-IR'!
  999. isReferenced
  1000. "Answer true if the receiver is referenced by other nodes.
  1001. Do not take sequences or assignments into account"
  1002. ^ (self parent isSequenceNode or: [
  1003. self parent isAssignmentNode ]) not
  1004. !
  1005. subtreeNeedsAliasing
  1006. ^ self shouldBeAliased or: [
  1007. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1008. ! !
  1009. !AssignmentNode methodsFor: '*Compiler-IR'!
  1010. shouldBeAliased
  1011. ^ super shouldBeAliased or: [ self isReferenced ]
  1012. ! !
  1013. !BlockClosure methodsFor: '*Compiler-IR'!
  1014. appendToInstruction: anIRInstruction
  1015. anIRInstruction appendBlock: self
  1016. ! !
  1017. !BlockNode methodsFor: '*Compiler-IR'!
  1018. subtreeNeedsAliasing
  1019. ^ self shouldBeAliased
  1020. ! !
  1021. !CascadeNode methodsFor: '*Compiler-IR'!
  1022. subtreeNeedsAliasing
  1023. ^ self parent isSequenceNode not
  1024. ! !
  1025. !SendNode methodsFor: '*Compiler-IR'!
  1026. shouldBeAliased
  1027. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  1028. See IRJSVisitor >> visitIRSend:"
  1029. | sends |
  1030. sends := (self method sendIndexes at: self selector) size.
  1031. ^ (super shouldBeAliased or: [
  1032. self isReferenced and: [
  1033. self index < sends or: [
  1034. self superSend ] ] ])
  1035. !
  1036. subtreeNeedsAliasing
  1037. ^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
  1038. ! !