Compiler-IR.st 24 KB

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