Compiler-IR.st 25 KB

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