Compiler-IR.st 25 KB

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