Compiler-IR.st 24 KB

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