Compiler-IR.st 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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: 'visiting'!
  301. accept: aVisitor
  302. ^ aVisitor visitIRAssignment: self
  303. ! !
  304. IRInstruction subclass: #IRDynamicArray
  305. instanceVariableNames: ''
  306. package: 'Compiler-IR'!
  307. !IRDynamicArray methodsFor: 'visiting'!
  308. accept: aVisitor
  309. ^ aVisitor visitIRDynamicArray: self
  310. ! !
  311. IRInstruction subclass: #IRDynamicDictionary
  312. instanceVariableNames: ''
  313. package: 'Compiler-IR'!
  314. !IRDynamicDictionary methodsFor: 'visiting'!
  315. accept: 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 instructions select: [ :each |
  355. each isTempDeclaration ]
  356. ! !
  357. IRClosureInstruction subclass: #IRClosure
  358. instanceVariableNames: ''
  359. package: 'Compiler-IR'!
  360. !IRClosure methodsFor: 'accessing'!
  361. sequence
  362. ^ self instructions last
  363. ! !
  364. !IRClosure methodsFor: 'testing'!
  365. isClosure
  366. ^ true
  367. ! !
  368. !IRClosure methodsFor: 'visiting'!
  369. accept: 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. isMethod
  388. ^ true
  389. !
  390. messageSends
  391. ^ self sendIndexes keys
  392. !
  393. method
  394. ^ self
  395. !
  396. selector
  397. ^ selector
  398. !
  399. selector: aString
  400. selector := aString
  401. !
  402. sendIndexes
  403. ^ sendIndexes
  404. !
  405. sendIndexes: aDictionary
  406. sendIndexes := aDictionary
  407. !
  408. source
  409. ^ source
  410. !
  411. source: aString
  412. source := aString
  413. !
  414. theClass
  415. ^ theClass
  416. !
  417. theClass: aClass
  418. theClass := aClass
  419. ! !
  420. !IRMethod methodsFor: 'visiting'!
  421. accept: aVisitor
  422. ^ aVisitor visitIRMethod: self
  423. ! !
  424. IRScopedInstruction subclass: #IRReturn
  425. instanceVariableNames: ''
  426. package: 'Compiler-IR'!
  427. !IRReturn commentStamp!
  428. I am a local return instruction.!
  429. !IRReturn methodsFor: 'accessing'!
  430. scope
  431. ^ scope ifNil: [ self parent scope ]
  432. ! !
  433. !IRReturn methodsFor: 'testing'!
  434. canBeAssigned
  435. ^ false
  436. !
  437. isBlockReturn
  438. ^ false
  439. !
  440. isLocalReturn
  441. ^ true
  442. !
  443. isNonLocalReturn
  444. ^ self isLocalReturn not
  445. !
  446. isReturn
  447. ^ true
  448. ! !
  449. !IRReturn methodsFor: 'visiting'!
  450. accept: aVisitor
  451. ^ aVisitor visitIRReturn: self
  452. ! !
  453. IRReturn subclass: #IRBlockReturn
  454. instanceVariableNames: ''
  455. package: 'Compiler-IR'!
  456. !IRBlockReturn commentStamp!
  457. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  458. !IRBlockReturn methodsFor: 'testing'!
  459. isBlockReturn
  460. ^ true
  461. ! !
  462. !IRBlockReturn methodsFor: 'visiting'!
  463. accept: aVisitor
  464. ^ aVisitor visitIRBlockReturn: self
  465. ! !
  466. IRReturn subclass: #IRNonLocalReturn
  467. instanceVariableNames: ''
  468. package: 'Compiler-IR'!
  469. !IRNonLocalReturn commentStamp!
  470. I am a non local return instruction.
  471. Non local returns are handled using a try/catch JavaScript statement.
  472. See `IRNonLocalReturnHandling` class.!
  473. !IRNonLocalReturn methodsFor: 'testing'!
  474. isLocalReturn
  475. ^ false
  476. ! !
  477. !IRNonLocalReturn methodsFor: 'visiting'!
  478. accept: aVisitor
  479. ^ aVisitor visitIRNonLocalReturn: self
  480. ! !
  481. IRScopedInstruction subclass: #IRTempDeclaration
  482. instanceVariableNames: 'name'
  483. package: 'Compiler-IR'!
  484. !IRTempDeclaration methodsFor: 'accessing'!
  485. name
  486. ^ name
  487. !
  488. name: aString
  489. name := aString
  490. ! !
  491. !IRTempDeclaration methodsFor: 'testing'!
  492. isTempDeclaration
  493. ^ true
  494. ! !
  495. !IRTempDeclaration methodsFor: 'visiting'!
  496. accept: aVisitor
  497. ^ aVisitor visitIRTempDeclaration: self
  498. ! !
  499. IRInstruction subclass: #IRSend
  500. instanceVariableNames: 'selector index'
  501. package: 'Compiler-IR'!
  502. !IRSend commentStamp!
  503. I am a message send instruction.!
  504. !IRSend methodsFor: 'accessing'!
  505. index
  506. ^ index
  507. !
  508. index: anInteger
  509. index := anInteger
  510. !
  511. isSuperSend
  512. | receiver |
  513. receiver := self instructions first.
  514. ^ receiver isVariable and: [ receiver variable name = 'super' ]
  515. !
  516. selector
  517. ^ selector
  518. !
  519. selector: aString
  520. selector := aString
  521. ! !
  522. !IRSend methodsFor: 'testing'!
  523. isSend
  524. ^ true
  525. ! !
  526. !IRSend methodsFor: 'visiting'!
  527. accept: aVisitor
  528. ^ aVisitor visitIRSend: self
  529. ! !
  530. IRInstruction subclass: #IRSequence
  531. instanceVariableNames: ''
  532. package: 'Compiler-IR'!
  533. !IRSequence methodsFor: 'testing'!
  534. isSequence
  535. ^ true
  536. ! !
  537. !IRSequence methodsFor: 'visiting'!
  538. accept: aVisitor
  539. ^ aVisitor visitIRSequence: self
  540. ! !
  541. IRSequence subclass: #IRBlockSequence
  542. instanceVariableNames: ''
  543. package: 'Compiler-IR'!
  544. !IRBlockSequence methodsFor: 'visiting'!
  545. accept: aVisitor
  546. ^ aVisitor visitIRBlockSequence: self
  547. ! !
  548. IRInstruction subclass: #IRValue
  549. instanceVariableNames: 'value'
  550. package: 'Compiler-IR'!
  551. !IRValue commentStamp!
  552. I am the simplest possible instruction. I represent a value.!
  553. !IRValue methodsFor: 'accessing'!
  554. value
  555. ^ value
  556. !
  557. value: aString
  558. value := aString
  559. ! !
  560. !IRValue methodsFor: 'testing'!
  561. needsBoxingAsReceiver
  562. ^ false
  563. ! !
  564. !IRValue methodsFor: 'visiting'!
  565. accept: aVisitor
  566. ^ aVisitor visitIRValue: self
  567. ! !
  568. IRInstruction subclass: #IRVariable
  569. instanceVariableNames: 'variable'
  570. package: 'Compiler-IR'!
  571. !IRVariable commentStamp!
  572. I am a variable instruction.!
  573. !IRVariable methodsFor: 'accessing'!
  574. variable
  575. ^ variable
  576. !
  577. variable: aScopeVariable
  578. variable := aScopeVariable
  579. ! !
  580. !IRVariable methodsFor: 'testing'!
  581. isVariable
  582. ^ true
  583. !
  584. needsBoxingAsReceiver
  585. ^ self variable isPseudoVar not
  586. ! !
  587. !IRVariable methodsFor: 'visiting'!
  588. accept: aVisitor
  589. ^ aVisitor visitIRVariable: self
  590. ! !
  591. IRInstruction subclass: #IRVerbatim
  592. instanceVariableNames: 'source'
  593. package: 'Compiler-IR'!
  594. !IRVerbatim methodsFor: 'accessing'!
  595. source
  596. ^ source
  597. !
  598. source: aString
  599. source := aString
  600. ! !
  601. !IRVerbatim methodsFor: 'visiting'!
  602. accept: aVisitor
  603. ^ aVisitor visitIRVerbatim: self
  604. ! !
  605. Object subclass: #IRVisitor
  606. instanceVariableNames: ''
  607. package: 'Compiler-IR'!
  608. !IRVisitor methodsFor: 'visiting'!
  609. visit: anIRInstruction
  610. ^ anIRInstruction accept: self
  611. !
  612. visitIRAssignment: anIRAssignment
  613. ^ self visitIRInstruction: anIRAssignment
  614. !
  615. visitIRBlockReturn: anIRBlockReturn
  616. ^ self visitIRReturn: anIRBlockReturn
  617. !
  618. visitIRBlockSequence: anIRBlockSequence
  619. ^ self visitIRSequence: anIRBlockSequence
  620. !
  621. visitIRClosure: anIRClosure
  622. ^ self visitIRInstruction: anIRClosure
  623. !
  624. visitIRDynamicArray: anIRDynamicArray
  625. ^ self visitIRInstruction: anIRDynamicArray
  626. !
  627. visitIRDynamicDictionary: anIRDynamicDictionary
  628. ^ self visitIRInstruction: anIRDynamicDictionary
  629. !
  630. visitIRInlinedClosure: anIRInlinedClosure
  631. ^ self visitIRClosure: anIRInlinedClosure
  632. !
  633. visitIRInlinedSequence: anIRInlinedSequence
  634. ^ self visitIRSequence: anIRInlinedSequence
  635. !
  636. visitIRInstruction: anIRInstruction
  637. anIRInstruction instructions do: [ :each | self visit: each ].
  638. ^ anIRInstruction
  639. !
  640. visitIRMethod: anIRMethod
  641. ^ self visitIRInstruction: anIRMethod
  642. !
  643. visitIRNonLocalReturn: anIRNonLocalReturn
  644. ^ self visitIRInstruction: anIRNonLocalReturn
  645. !
  646. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  647. ^ self visitIRInstruction: anIRNonLocalReturnHandling
  648. !
  649. visitIRReturn: anIRReturn
  650. ^ self visitIRInstruction: anIRReturn
  651. !
  652. visitIRSend: anIRSend
  653. ^ self visitIRInstruction: anIRSend
  654. !
  655. visitIRSequence: anIRSequence
  656. ^ self visitIRInstruction: anIRSequence
  657. !
  658. visitIRTempDeclaration: anIRTempDeclaration
  659. ^ self visitIRInstruction: anIRTempDeclaration
  660. !
  661. visitIRValue: anIRValue
  662. ^ self visitIRInstruction: anIRValue
  663. !
  664. visitIRVariable: anIRVariable
  665. ^ self visitIRInstruction: anIRVariable
  666. !
  667. visitIRVerbatim: anIRVerbatim
  668. ^ self visitIRInstruction: anIRVerbatim
  669. ! !
  670. IRVisitor subclass: #IRJSTranslator
  671. instanceVariableNames: 'stream currentClass'
  672. package: 'Compiler-IR'!
  673. !IRJSTranslator methodsFor: 'accessing'!
  674. contents
  675. ^ self stream contents
  676. !
  677. currentClass
  678. ^ currentClass
  679. !
  680. currentClass: aClass
  681. currentClass := aClass
  682. !
  683. stream
  684. ^ stream
  685. !
  686. stream: aStream
  687. stream := aStream
  688. ! !
  689. !IRJSTranslator methodsFor: 'initialization'!
  690. initialize
  691. super initialize.
  692. stream := JSStream new.
  693. ! !
  694. !IRJSTranslator methodsFor: 'visiting'!
  695. visitIRAssignment: anIRAssignment
  696. self stream
  697. nextPutAssignLhs: [self visit: anIRAssignment instructions first]
  698. rhs: [self visit: anIRAssignment instructions last].
  699. !
  700. visitIRClosure: anIRClosure
  701. self stream
  702. nextPutClosureWith: [
  703. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  704. each name asVariableName ]).
  705. self stream
  706. nextPutBlockContextFor: anIRClosure
  707. during: [ super visitIRClosure: anIRClosure ] ]
  708. arguments: anIRClosure arguments
  709. !
  710. visitIRDynamicArray: anIRDynamicArray
  711. self
  712. visitInstructionList: anIRDynamicArray instructions
  713. enclosedBetween: '[' and: ']'
  714. !
  715. visitIRDynamicDictionary: anIRDynamicDictionary
  716. self
  717. visitInstructionList: anIRDynamicDictionary instructions
  718. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  719. !
  720. visitIRMethod: anIRMethod
  721. self stream
  722. nextPutMethodDeclaration: anIRMethod
  723. with: [ self stream
  724. nextPutFunctionWith: [
  725. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  726. each name asVariableName ]).
  727. self stream nextPutContextFor: anIRMethod during: [
  728. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  729. self stream nextPutVars:
  730. (internalVars asSet collect: [ :each | each variable alias ]) ].
  731. anIRMethod scope hasNonLocalReturn
  732. ifTrue: [
  733. self stream nextPutNonLocalReturnHandlingWith: [
  734. super visitIRMethod: anIRMethod ] ]
  735. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  736. arguments: anIRMethod arguments ]
  737. !
  738. visitIRNonLocalReturn: anIRNonLocalReturn
  739. self stream nextPutNonLocalReturnWith: [
  740. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  741. !
  742. visitIRReturn: anIRReturn
  743. self stream nextPutReturnWith: [
  744. super visitIRReturn: anIRReturn ]
  745. !
  746. visitIRSend: anIRSend
  747. | sends superclass |
  748. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  749. anIRSend isSuperSend
  750. ifTrue: [ self visitSuperSend: anIRSend ]
  751. ifFalse: [ self visitSend: anIRSend ].
  752. (sends > 1 and: [ anIRSend index < sends ])
  753. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  754. !
  755. visitIRSequence: anIRSequence
  756. self stream nextPutSequenceWith: [
  757. anIRSequence instructions do: [ :each |
  758. self stream nextPutStatementWith: (self visit: each) ] ]
  759. !
  760. visitIRTempDeclaration: anIRTempDeclaration
  761. "self stream
  762. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  763. lf"
  764. !
  765. visitIRValue: anIRValue
  766. self stream nextPutAll: anIRValue value asJavascript
  767. !
  768. visitIRVariable: anIRVariable
  769. anIRVariable variable name = 'thisContext'
  770. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  771. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  772. !
  773. visitIRVerbatim: anIRVerbatim
  774. self stream nextPutStatementWith: [
  775. self stream nextPutAll: anIRVerbatim source ]
  776. !
  777. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  778. self stream nextPutAll: aString.
  779. anArray
  780. do: [ :each | self visit: each ]
  781. separatedBy: [ self stream nextPutAll: ',' ].
  782. stream nextPutAll: anotherString
  783. !
  784. visitReceiver: anIRInstruction
  785. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  786. self stream nextPutAll: '$recv('.
  787. self visit: anIRInstruction.
  788. self stream nextPutAll: ')'
  789. !
  790. visitSend: anIRSend
  791. self visitReceiver: anIRSend instructions first.
  792. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  793. self
  794. visitInstructionList: anIRSend instructions allButFirst
  795. enclosedBetween: '(' and: ')'
  796. !
  797. visitSuperSend: anIRSend
  798. self stream
  799. nextPutAll: '('; lf;
  800. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  801. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  802. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  803. nextPutAll: '(', self currentClass asJavascript;
  804. nextPutAll: '.superclass||$boot.nilAsClass).fn.prototype.';
  805. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  806. nextPutAll: '$recv(self), '.
  807. self
  808. visitInstructionList: anIRSend instructions allButFirst
  809. enclosedBetween: '[' and: ']'.
  810. self stream
  811. nextPutAll: '));'; lf;
  812. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  813. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  814. nextPutAll: '//>>excludeEnd("ctx");'
  815. ! !
  816. Object subclass: #JSStream
  817. instanceVariableNames: 'stream'
  818. package: 'Compiler-IR'!
  819. !JSStream methodsFor: 'accessing'!
  820. contents
  821. ^ stream contents
  822. ! !
  823. !JSStream methodsFor: 'initialization'!
  824. initialize
  825. super initialize.
  826. stream := '' writeStream.
  827. ! !
  828. !JSStream methodsFor: 'streaming'!
  829. lf
  830. stream lf
  831. !
  832. nextPut: aString
  833. stream nextPut: aString
  834. !
  835. nextPutAll: aString
  836. stream nextPutAll: aString
  837. !
  838. nextPutAssignLhs: aBlock rhs: anotherBlock
  839. aBlock value.
  840. stream nextPutAll: '='.
  841. anotherBlock value
  842. !
  843. nextPutBlockContextFor: anIRClosure during: aBlock
  844. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  845. self
  846. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  847. lf;
  848. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  849. lf;
  850. nextPutAll: '//>>excludeEnd("ctx");';
  851. lf.
  852. aBlock value.
  853. self
  854. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  855. lf;
  856. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  857. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  858. anIRClosure locals
  859. do: [ :each |
  860. self
  861. nextPutAll: each asVariableName;
  862. nextPutAll: ':';
  863. nextPutAll: each asVariableName ]
  864. separatedBy: [ self nextPutAll: ',' ].
  865. self
  866. nextPutAll: '},';
  867. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  868. lf;
  869. nextPutAll: '//>>excludeEnd("ctx");'
  870. !
  871. nextPutClosureWith: aBlock arguments: anArray
  872. stream nextPutAll: '(function('.
  873. anArray
  874. do: [ :each | stream nextPutAll: each asVariableName ]
  875. separatedBy: [ stream nextPut: ',' ].
  876. stream nextPutAll: '){'; lf.
  877. aBlock value.
  878. stream lf; nextPutAll: '})'
  879. !
  880. nextPutContextFor: aMethod during: aBlock
  881. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  882. self
  883. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  884. lf;
  885. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  886. lf;
  887. nextPutAll: '//>>excludeEnd("ctx");';
  888. lf.
  889. aBlock value.
  890. self
  891. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  892. lf;
  893. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  894. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  895. aMethod locals
  896. do: [ :each |
  897. self
  898. nextPutAll: each asVariableName;
  899. nextPutAll: ':';
  900. nextPutAll: each asVariableName ]
  901. separatedBy: [ self nextPutAll: ',' ].
  902. self
  903. nextPutAll: '},';
  904. nextPutAll: aMethod theClass asJavascript;
  905. nextPutAll: ')});';
  906. lf;
  907. nextPutAll: '//>>excludeEnd("ctx");'
  908. !
  909. nextPutFunctionWith: aBlock arguments: anArray
  910. stream nextPutAll: 'fn: function('.
  911. anArray
  912. do: [ :each | stream nextPutAll: each asVariableName ]
  913. separatedBy: [ stream nextPut: ',' ].
  914. stream nextPutAll: '){'; lf.
  915. stream nextPutAll: 'var self=this;'; lf.
  916. aBlock value.
  917. stream lf; nextPutAll: '}'
  918. !
  919. nextPutIf: aBlock then: anotherBlock
  920. stream nextPutAll: 'if('.
  921. aBlock value.
  922. stream nextPutAll: '){'; lf.
  923. anotherBlock value.
  924. stream nextPutAll: '}'
  925. !
  926. nextPutIf: aBlock then: ifBlock else: elseBlock
  927. stream nextPutAll: 'if('.
  928. aBlock value.
  929. stream nextPutAll: '){'; lf.
  930. ifBlock value.
  931. stream nextPutAll: '} else {'; lf.
  932. elseBlock value.
  933. stream nextPutAll: '}'
  934. !
  935. nextPutMethodDeclaration: aMethod with: aBlock
  936. stream
  937. nextPutAll: '$core.method({'; lf;
  938. nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
  939. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  940. aBlock value.
  941. stream
  942. nextPutAll: ',', String lf, 'messageSends: ';
  943. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  944. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  945. nextPutAll: 'referencedClasses: ['.
  946. aMethod classReferences
  947. do: [ :each | stream nextPutAll: each asJavascript ]
  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 asJavascript;
  978. nextPutAll: ']=';
  979. nextPutAll: anIRSend index asString;
  980. nextPutAll: ';'; lf;
  981. nextPutAll: '//>>excludeEnd("ctx")'
  982. !
  983. nextPutSequenceWith: aBlock
  984. "stream
  985. nextPutAll: 'switch($core.thisContext.pc){'; lf."
  986. aBlock value.
  987. "stream
  988. nextPutAll: '};'; lf"
  989. !
  990. nextPutStatementWith: aBlock
  991. aBlock value.
  992. stream nextPutAll: ';'; lf
  993. !
  994. nextPutVars: aCollection
  995. aCollection ifNotEmpty: [
  996. stream nextPutAll: 'var '.
  997. aCollection
  998. do: [ :each | stream nextPutAll: each ]
  999. separatedBy: [ stream nextPutAll: ',' ].
  1000. stream nextPutAll: ';'; lf ]
  1001. ! !
  1002. !BlockClosure methodsFor: '*Compiler-IR'!
  1003. appendToInstruction: anIRInstruction
  1004. anIRInstruction appendBlock: self
  1005. ! !