Compiler-IR.st 24 KB

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