Compiler-IR.st 25 KB

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