Compiler-IR.st 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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: [ :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 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 index > 1 ifTrue: [
  741. self stream nextPutSendIndexFor: anIRSend ].
  742. anIRSend classSend
  743. ifNil: [ self visitSend: anIRSend ]
  744. ifNotNil: [ self visitSuperSend: anIRSend ]
  745. !
  746. visitIRSequence: anIRSequence
  747. self stream nextPutSequenceWith: [
  748. anIRSequence instructions do: [ :each |
  749. self stream nextPutStatementWith: (self visit: each) ]]
  750. !
  751. visitIRTempDeclaration: anIRTempDeclaration
  752. "self stream
  753. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  754. lf"
  755. !
  756. visitIRValue: anIRValue
  757. self stream nextPutAll: anIRValue value asJavascript
  758. !
  759. visitIRVariable: anIRVariable
  760. anIRVariable variable name = 'thisContext'
  761. ifTrue: [ self stream nextPutAll: 'smalltalk.getThisContext()' ]
  762. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  763. !
  764. visitIRVerbatim: anIRVerbatim
  765. self stream nextPutStatementWith: [
  766. self stream nextPutAll: anIRVerbatim source ]
  767. !
  768. visitReceiver: anIRInstruction
  769. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  770. self stream nextPutAll: '_st('.
  771. self visit: anIRInstruction.
  772. self stream nextPutAll: ')'
  773. !
  774. visitSend: anIRSend
  775. self visitReceiver: anIRSend instructions first.
  776. self stream nextPutAll: '.', anIRSend selector asSelector, '('.
  777. anIRSend instructions allButFirst
  778. do: [ :each | self visit: each ]
  779. separatedBy: [ self stream nextPutAll: ',' ].
  780. self stream nextPutAll: ')'
  781. !
  782. visitSuperSend: anIRSend
  783. self stream
  784. nextPutAll: self currentClass asJavascript;
  785. nextPutAll: '.superclass.fn.prototype.';
  786. nextPutAll: anIRSend selector asSelector, '.apply(';
  787. nextPutAll: '_st('.
  788. self visit: anIRSend instructions first.
  789. self stream nextPutAll: '), ['.
  790. anIRSend instructions allButFirst
  791. do: [ :each | self visit: each ]
  792. separatedBy: [ self stream nextPutAll: ',' ].
  793. self stream nextPutAll: '])'
  794. ! !
  795. Object subclass: #JSStream
  796. instanceVariableNames: 'stream'
  797. package: 'Compiler-IR'!
  798. !JSStream methodsFor: 'accessing'!
  799. contents
  800. ^ stream contents
  801. ! !
  802. !JSStream methodsFor: 'initialization'!
  803. initialize
  804. super initialize.
  805. stream := '' writeStream.
  806. ! !
  807. !JSStream methodsFor: 'streaming'!
  808. lf
  809. stream lf
  810. !
  811. nextPut: aString
  812. stream nextPut: aString
  813. !
  814. nextPutAll: aString
  815. stream nextPutAll: aString
  816. !
  817. nextPutAssignment
  818. stream nextPutAll: '='
  819. !
  820. nextPutBlockContextFor: anIRClosure during: aBlock
  821. self
  822. nextPutAll: 'return smalltalk.withContext(function(', anIRClosure scope alias, ') {'; lf.
  823. aBlock value.
  824. self
  825. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  826. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  827. anIRClosure locals
  828. do: [ :each |
  829. self
  830. nextPutAll: each asVariableName;
  831. nextPutAll: ':';
  832. nextPutAll: each asVariableName]
  833. separatedBy: [ self nextPutAll: ',' ].
  834. self
  835. nextPutAll: '},';
  836. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')})'
  837. !
  838. nextPutClassRefFunction: aString
  839. "Creates an inner function $aString into method and called as `$Foo()`whenever the global is accessed.
  840. This ensures that undefined global access will answer `nil`"
  841. stream
  842. nextPutAll: 'function $';
  843. nextPutAll: aString;
  844. nextPutAll: '(){return smalltalk.';
  845. nextPutAll: aString;
  846. nextPutAll: '||(typeof ';
  847. nextPutAll: aString;
  848. nextPutAll: '=="undefined"?nil:';
  849. nextPutAll: aString;
  850. nextPutAll: ')}';
  851. lf
  852. !
  853. nextPutClosureWith: aBlock arguments: anArray
  854. stream nextPutAll: '(function('.
  855. anArray
  856. do: [ :each | stream nextPutAll: each asVariableName ]
  857. separatedBy: [ stream nextPut: ',' ].
  858. stream nextPutAll: '){'; lf.
  859. aBlock value.
  860. stream nextPutAll: '})'
  861. !
  862. nextPutContextFor: aMethod during: aBlock
  863. self
  864. nextPutAll: 'return smalltalk.withContext(function(', aMethod scope alias, ') { '; lf.
  865. aBlock value.
  866. self
  867. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  868. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  869. aMethod locals
  870. do: [ :each |
  871. self
  872. nextPutAll: each asVariableName;
  873. nextPutAll: ':';
  874. nextPutAll: each asVariableName]
  875. separatedBy: [ self nextPutAll: ',' ].
  876. self
  877. nextPutAll: '},';
  878. nextPutAll: aMethod theClass asJavascript;
  879. nextPutAll: ')})'
  880. !
  881. nextPutFunctionWith: aBlock arguments: anArray
  882. stream nextPutAll: 'fn: function('.
  883. anArray
  884. do: [ :each | stream nextPutAll: each asVariableName ]
  885. separatedBy: [ stream nextPut: ',' ].
  886. stream nextPutAll: '){'; lf.
  887. stream nextPutAll: 'var self=this;'; lf.
  888. aBlock value.
  889. stream nextPutAll: '}'
  890. !
  891. nextPutIf: aBlock with: anotherBlock
  892. stream nextPutAll: 'if('.
  893. aBlock value.
  894. stream nextPutAll: '){'; lf.
  895. anotherBlock value.
  896. stream nextPutAll: '}'
  897. !
  898. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  899. stream nextPutAll: 'if('.
  900. aBlock value.
  901. stream nextPutAll: '){'; lf.
  902. ifBlock value.
  903. stream nextPutAll: '} else {'; lf.
  904. elseBlock value.
  905. stream nextPutAll: '}'
  906. !
  907. nextPutMethodDeclaration: aMethod with: aBlock
  908. stream
  909. nextPutAll: 'smalltalk.method({'; lf;
  910. nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
  911. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  912. aBlock value.
  913. stream
  914. nextPutAll: ',', String lf, 'messageSends: ';
  915. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  916. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  917. nextPutAll: 'referencedClasses: ['.
  918. aMethod classReferences
  919. do: [:each | stream nextPutAll: each asJavascript]
  920. separatedBy: [stream nextPutAll: ','].
  921. stream
  922. nextPutAll: ']';
  923. nextPutAll: '})'
  924. !
  925. nextPutNonLocalReturnHandlingWith: aBlock
  926. stream
  927. nextPutAll: 'var $early={};'; lf;
  928. nextPutAll: 'try {'; lf.
  929. aBlock value.
  930. stream
  931. nextPutAll: '}'; lf;
  932. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  933. !
  934. nextPutNonLocalReturnWith: aBlock
  935. stream nextPutAll: 'throw $early=['.
  936. aBlock value.
  937. stream nextPutAll: ']'
  938. !
  939. nextPutReturn
  940. stream nextPutAll: 'return '
  941. !
  942. nextPutReturnWith: aBlock
  943. self nextPutReturn.
  944. aBlock value
  945. !
  946. nextPutSendIndexFor: anIRSend
  947. self
  948. nextPutAll: anIRSend scope alias;
  949. nextPutAll: '.sendIdx["';
  950. nextPutAll: anIRSend selector;
  951. nextPutAll: '"]=';
  952. nextPutAll: anIRSend index asString;
  953. lf
  954. !
  955. nextPutSequenceWith: aBlock
  956. "stream
  957. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  958. aBlock value.
  959. "stream
  960. nextPutAll: '};'; lf"
  961. !
  962. nextPutStatementWith: aBlock
  963. aBlock value.
  964. stream nextPutAll: ';'; lf
  965. !
  966. nextPutVars: aCollection
  967. aCollection ifEmpty: [ ^self ].
  968. stream nextPutAll: 'var '.
  969. aCollection
  970. do: [ :each | stream nextPutAll: each ]
  971. separatedBy: [ stream nextPutAll: ',' ].
  972. stream nextPutAll: ';'; lf
  973. ! !
  974. !BlockClosure methodsFor: '*Compiler-IR'!
  975. appendToInstruction: anIRInstruction
  976. anIRInstruction appendBlock: self
  977. ! !
  978. !String methodsFor: '*Compiler-IR'!
  979. asVariableName
  980. ^ (Smalltalk current reservedWords includes: self)
  981. ifTrue: [ self, '_' ]
  982. ifFalse: [ self ]
  983. ! !