Compiler-IR.st 23 KB

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