Compiler-IR.st 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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://lolg.it/amber/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. | receiver |
  114. receiver := aNode receiver.
  115. receiver isImmutable ifFalse: [
  116. | alias |
  117. alias := self alias: receiver.
  118. receiver := VariableNode new binding: alias variable ].
  119. aNode nodes do: [ :each | each receiver: receiver ].
  120. aNode nodes allButLast do: [ :each |
  121. self sequence add: (self visit: each) ].
  122. ^ self visitOrAlias: aNode nodes last
  123. !
  124. visitDynamicArrayNode: aNode
  125. | array |
  126. array := IRDynamicArray new.
  127. (self aliasTemporally: aNode nodes) do: [ :each | array add: each ].
  128. ^ array
  129. !
  130. visitDynamicDictionaryNode: aNode
  131. | dictionary |
  132. dictionary := IRDynamicDictionary new.
  133. (self aliasTemporally: aNode nodes) do: [ :each | dictionary add: each ].
  134. ^ dictionary
  135. !
  136. visitJSStatementNode: aNode
  137. ^ IRVerbatim new
  138. source: aNode source crlfSanitized;
  139. yourself
  140. !
  141. visitMethodNode: aNode
  142. self method: (IRMethod new
  143. source: self source crlfSanitized;
  144. theClass: self theClass;
  145. arguments: aNode arguments;
  146. selector: aNode selector;
  147. sendIndexes: aNode sendIndexes;
  148. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  149. classReferences: aNode classReferences;
  150. scope: aNode scope;
  151. yourself).
  152. aNode scope temps do: [ :each |
  153. self method add: (IRTempDeclaration new
  154. name: each name;
  155. scope: aNode scope;
  156. yourself) ].
  157. aNode nodes do: [ :each | self method add: (self visit: each) ].
  158. aNode scope hasLocalReturn ifFalse: [self method
  159. add: (IRReturn new
  160. add: (IRVariable new
  161. variable: (aNode scope pseudoVars at: 'self');
  162. yourself);
  163. yourself);
  164. add: (IRVerbatim new source: ''; yourself) ].
  165. ^ self method
  166. !
  167. visitOrAlias: aNode
  168. ^ aNode shouldBeAliased
  169. ifTrue: [ self alias: aNode ]
  170. ifFalse: [ self visit: aNode ]
  171. !
  172. visitReturnNode: aNode
  173. | return |
  174. return := aNode nonLocalReturn
  175. ifTrue: [ IRNonLocalReturn new ]
  176. ifFalse: [ IRReturn new ].
  177. return scope: aNode scope.
  178. aNode nodes do: [ :each |
  179. return add: (self visitOrAlias: each) ].
  180. ^ return
  181. !
  182. visitSendNode: aNode
  183. | send |
  184. send := IRSend new.
  185. send
  186. selector: aNode selector;
  187. index: aNode index.
  188. (self aliasTemporally: aNode nodes) do: [ :each | send add: each ].
  189. ^ send
  190. !
  191. visitSequenceNode: aNode
  192. ^ self
  193. withSequence: IRSequence new
  194. do: [
  195. aNode nodes do: [ :each | | instruction |
  196. instruction := self visitOrAlias: each.
  197. instruction isVariable ifFalse: [
  198. self sequence add: instruction ] ]]
  199. !
  200. visitValueNode: aNode
  201. ^ IRValue new
  202. value: aNode value;
  203. yourself
  204. !
  205. visitVariableNode: aNode
  206. ^ IRVariable new
  207. variable: aNode binding;
  208. yourself
  209. ! !
  210. Object subclass: #IRInstruction
  211. instanceVariableNames: 'parent instructions'
  212. package: 'Compiler-IR'!
  213. !IRInstruction commentStamp!
  214. I am the abstract root class of the IR (intermediate representation) instructions class hierarchy.
  215. The IR graph is used to emit JavaScript code using a JSStream.!
  216. !IRInstruction methodsFor: 'accessing'!
  217. instructions
  218. ^ instructions ifNil: [ instructions := OrderedCollection new ]
  219. !
  220. method
  221. ^ self parent method
  222. !
  223. parent
  224. ^ parent
  225. !
  226. parent: anIRInstruction
  227. parent := anIRInstruction
  228. !
  229. scope
  230. ^ self parent ifNotNil: [ :node |
  231. node scope ]
  232. ! !
  233. !IRInstruction methodsFor: 'building'!
  234. add: anObject
  235. anObject parent: self.
  236. ^ self instructions add: anObject
  237. !
  238. remove: anIRInstruction
  239. self instructions remove: anIRInstruction
  240. !
  241. replace: anIRInstruction with: anotherIRInstruction
  242. anotherIRInstruction parent: self.
  243. self instructions
  244. at: (self instructions indexOf: anIRInstruction)
  245. put: anotherIRInstruction
  246. !
  247. replaceWith: anIRInstruction
  248. self parent replace: self with: anIRInstruction
  249. ! !
  250. !IRInstruction methodsFor: 'testing'!
  251. canBeAssigned
  252. ^ true
  253. !
  254. isClosure
  255. ^ false
  256. !
  257. isInlined
  258. ^ false
  259. !
  260. isLocalReturn
  261. ^ false
  262. !
  263. isMethod
  264. ^ false
  265. !
  266. isReturn
  267. ^ false
  268. !
  269. isSend
  270. ^ false
  271. !
  272. isSequence
  273. ^ false
  274. !
  275. isTempDeclaration
  276. ^ false
  277. !
  278. isVariable
  279. ^ false
  280. !
  281. needsBoxingAsReceiver
  282. ^ true
  283. ! !
  284. !IRInstruction methodsFor: 'visiting'!
  285. accept: aVisitor
  286. ^ aVisitor visitIRInstruction: self
  287. ! !
  288. !IRInstruction class methodsFor: 'instance creation'!
  289. on: aBuilder
  290. ^ self new
  291. builder: aBuilder;
  292. yourself
  293. ! !
  294. IRInstruction subclass: #IRAssignment
  295. instanceVariableNames: ''
  296. package: 'Compiler-IR'!
  297. !IRAssignment methodsFor: 'accessing'!
  298. left
  299. ^ self instructions first
  300. !
  301. right
  302. ^ self instructions last
  303. ! !
  304. !IRAssignment methodsFor: 'visiting'!
  305. accept: aVisitor
  306. ^ aVisitor visitIRAssignment: self
  307. ! !
  308. IRInstruction subclass: #IRDynamicArray
  309. instanceVariableNames: ''
  310. package: 'Compiler-IR'!
  311. !IRDynamicArray methodsFor: 'visiting'!
  312. accept: aVisitor
  313. ^ aVisitor visitIRDynamicArray: self
  314. ! !
  315. IRInstruction subclass: #IRDynamicDictionary
  316. instanceVariableNames: ''
  317. package: 'Compiler-IR'!
  318. !IRDynamicDictionary methodsFor: 'visiting'!
  319. accept: aVisitor
  320. ^ aVisitor visitIRDynamicDictionary: self
  321. ! !
  322. IRInstruction subclass: #IRScopedInstruction
  323. instanceVariableNames: 'scope'
  324. package: 'Compiler-IR'!
  325. !IRScopedInstruction methodsFor: 'accessing'!
  326. scope
  327. ^ scope
  328. !
  329. scope: aScope
  330. scope := aScope
  331. ! !
  332. IRScopedInstruction subclass: #IRClosureInstruction
  333. instanceVariableNames: 'arguments requiresSmalltalkContext'
  334. package: 'Compiler-IR'!
  335. !IRClosureInstruction methodsFor: 'accessing'!
  336. arguments
  337. ^ arguments ifNil: [ #() ]
  338. !
  339. arguments: aCollection
  340. arguments := aCollection
  341. !
  342. locals
  343. ^ self arguments copy
  344. addAll: (self tempDeclarations collect: [ :each | each name ]);
  345. yourself
  346. !
  347. requiresSmalltalkContext
  348. ^ requiresSmalltalkContext ifNil: [ false ]
  349. !
  350. requiresSmalltalkContext: anObject
  351. requiresSmalltalkContext := anObject
  352. !
  353. scope: aScope
  354. super scope: aScope.
  355. aScope instruction: self
  356. !
  357. tempDeclarations
  358. ^ self instructions select: [ :each |
  359. each isTempDeclaration ]
  360. ! !
  361. IRClosureInstruction subclass: #IRClosure
  362. instanceVariableNames: ''
  363. package: 'Compiler-IR'!
  364. !IRClosure methodsFor: 'accessing'!
  365. sequence
  366. ^ self instructions last
  367. ! !
  368. !IRClosure methodsFor: 'testing'!
  369. isClosure
  370. ^ true
  371. ! !
  372. !IRClosure methodsFor: 'visiting'!
  373. accept: aVisitor
  374. ^ aVisitor visitIRClosure: self
  375. ! !
  376. IRClosureInstruction subclass: #IRMethod
  377. instanceVariableNames: 'theClass source selector classReferences sendIndexes requiresSmalltalkContext internalVariables'
  378. package: 'Compiler-IR'!
  379. !IRMethod commentStamp!
  380. I am a method instruction!
  381. !IRMethod methodsFor: 'accessing'!
  382. classReferences
  383. ^ classReferences
  384. !
  385. classReferences: aCollection
  386. classReferences := aCollection
  387. !
  388. internalVariables
  389. ^ internalVariables ifNil: [ internalVariables := Set new ]
  390. !
  391. messageSends
  392. ^ self sendIndexes keys
  393. !
  394. method
  395. ^ self
  396. !
  397. selector
  398. ^ selector
  399. !
  400. selector: aString
  401. selector := aString
  402. !
  403. sendIndexes
  404. ^ sendIndexes
  405. !
  406. sendIndexes: aDictionary
  407. sendIndexes := aDictionary
  408. !
  409. source
  410. ^ source
  411. !
  412. source: aString
  413. source := aString
  414. !
  415. theClass
  416. ^ theClass
  417. !
  418. theClass: aClass
  419. theClass := aClass
  420. ! !
  421. !IRMethod methodsFor: 'testing'!
  422. isMethod
  423. ^ true
  424. ! !
  425. !IRMethod methodsFor: 'visiting'!
  426. accept: aVisitor
  427. ^ aVisitor visitIRMethod: self
  428. ! !
  429. IRScopedInstruction subclass: #IRReturn
  430. instanceVariableNames: ''
  431. package: 'Compiler-IR'!
  432. !IRReturn commentStamp!
  433. I am a local return instruction.!
  434. !IRReturn methodsFor: 'accessing'!
  435. expression
  436. ^ self instructions single
  437. !
  438. scope
  439. ^ scope ifNil: [ self parent scope ]
  440. ! !
  441. !IRReturn methodsFor: 'testing'!
  442. canBeAssigned
  443. ^ false
  444. !
  445. isBlockReturn
  446. ^ false
  447. !
  448. isLocalReturn
  449. ^ true
  450. !
  451. isNonLocalReturn
  452. ^ self isLocalReturn not
  453. !
  454. isReturn
  455. ^ true
  456. ! !
  457. !IRReturn methodsFor: 'visiting'!
  458. accept: aVisitor
  459. ^ aVisitor visitIRReturn: self
  460. ! !
  461. IRReturn subclass: #IRBlockReturn
  462. instanceVariableNames: ''
  463. package: 'Compiler-IR'!
  464. !IRBlockReturn commentStamp!
  465. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  466. !IRBlockReturn methodsFor: 'testing'!
  467. isBlockReturn
  468. ^ true
  469. ! !
  470. !IRBlockReturn methodsFor: 'visiting'!
  471. accept: aVisitor
  472. ^ aVisitor visitIRBlockReturn: self
  473. ! !
  474. IRReturn subclass: #IRNonLocalReturn
  475. instanceVariableNames: ''
  476. package: 'Compiler-IR'!
  477. !IRNonLocalReturn commentStamp!
  478. I am a non local return instruction.
  479. Non local returns are handled using a try/catch JavaScript statement.
  480. See `IRNonLocalReturnHandling` class.!
  481. !IRNonLocalReturn methodsFor: 'testing'!
  482. isLocalReturn
  483. ^ false
  484. ! !
  485. !IRNonLocalReturn methodsFor: 'visiting'!
  486. accept: aVisitor
  487. ^ aVisitor visitIRNonLocalReturn: self
  488. ! !
  489. IRScopedInstruction subclass: #IRTempDeclaration
  490. instanceVariableNames: 'name'
  491. package: 'Compiler-IR'!
  492. !IRTempDeclaration methodsFor: 'accessing'!
  493. name
  494. ^ name
  495. !
  496. name: aString
  497. name := aString
  498. ! !
  499. !IRTempDeclaration methodsFor: 'testing'!
  500. isTempDeclaration
  501. ^ true
  502. ! !
  503. !IRTempDeclaration methodsFor: 'visiting'!
  504. accept: aVisitor
  505. ^ aVisitor visitIRTempDeclaration: self
  506. ! !
  507. IRInstruction subclass: #IRSend
  508. instanceVariableNames: 'selector index'
  509. package: 'Compiler-IR'!
  510. !IRSend commentStamp!
  511. I am a message send instruction.!
  512. !IRSend methodsFor: 'accessing'!
  513. arguments
  514. ^ self instructions allButFirst
  515. !
  516. index
  517. ^ index
  518. !
  519. index: anInteger
  520. index := anInteger
  521. !
  522. receiver
  523. ^ self instructions first
  524. !
  525. selector
  526. ^ selector
  527. !
  528. selector: aString
  529. selector := aString
  530. ! !
  531. !IRSend methodsFor: 'testing'!
  532. isSend
  533. ^ true
  534. !
  535. isSuperSend
  536. | receiver |
  537. receiver := self receiver.
  538. ^ receiver isVariable and: [ receiver variable name = 'super' ]
  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 stream
  711. nextPutAssignLhs: [self visit: anIRAssignment left]
  712. rhs: [self visit: anIRAssignment right].
  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
  726. visitInstructionList: anIRDynamicArray instructions
  727. enclosedBetween: '[' and: ']'
  728. !
  729. visitIRDynamicDictionary: anIRDynamicDictionary
  730. self
  731. visitInstructionList: anIRDynamicDictionary instructions
  732. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  733. !
  734. visitIRMethod: anIRMethod
  735. self stream
  736. nextPutMethodDeclaration: anIRMethod
  737. with: [ self stream
  738. nextPutFunctionWith: [
  739. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  740. each name asVariableName ]).
  741. self stream nextPutContextFor: anIRMethod during: [
  742. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  743. self stream nextPutVars:
  744. (internalVars asSet collect: [ :each | each variable alias ]) ].
  745. anIRMethod scope hasNonLocalReturn
  746. ifTrue: [
  747. self stream nextPutNonLocalReturnHandlingWith: [
  748. super visitIRMethod: anIRMethod ] ]
  749. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  750. arguments: anIRMethod arguments ]
  751. !
  752. visitIRNonLocalReturn: anIRNonLocalReturn
  753. self stream nextPutNonLocalReturnWith: [
  754. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  755. !
  756. visitIRReturn: anIRReturn
  757. self stream nextPutReturnWith: [
  758. super visitIRReturn: anIRReturn ]
  759. !
  760. visitIRSend: anIRSend
  761. | sends superclass |
  762. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  763. anIRSend isSuperSend
  764. ifTrue: [ self visitSuperSend: anIRSend ]
  765. ifFalse: [ self visitSend: anIRSend ].
  766. (sends > 1 and: [ anIRSend index < sends ])
  767. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  768. !
  769. visitIRSequence: anIRSequence
  770. self stream nextPutSequenceWith: [
  771. anIRSequence instructions do: [ :each |
  772. self stream nextPutStatementWith: (self visit: each) ] ]
  773. !
  774. visitIRTempDeclaration: anIRTempDeclaration
  775. "self stream
  776. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  777. lf"
  778. !
  779. visitIRValue: anIRValue
  780. self stream nextPutAll: anIRValue value asJavascript
  781. !
  782. visitIRVariable: anIRVariable
  783. anIRVariable variable name = 'thisContext'
  784. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  785. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  786. !
  787. visitIRVerbatim: anIRVerbatim
  788. self stream nextPutStatementWith: [
  789. self stream nextPutAll: anIRVerbatim source ]
  790. !
  791. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  792. self stream nextPutAll: aString.
  793. anArray
  794. do: [ :each | self visit: each ]
  795. separatedBy: [ self stream nextPutAll: ',' ].
  796. stream nextPutAll: anotherString
  797. !
  798. visitReceiver: anIRInstruction
  799. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  800. self stream nextPutAll: '$recv('.
  801. self visit: anIRInstruction.
  802. self stream nextPutAll: ')'
  803. !
  804. visitSend: anIRSend
  805. self visitReceiver: anIRSend receiver.
  806. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  807. self
  808. visitInstructionList: anIRSend arguments
  809. enclosedBetween: '(' and: ')'
  810. !
  811. visitSuperSend: anIRSend
  812. self stream
  813. nextPutAll: '('; lf;
  814. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  815. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  816. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  817. nextPutAll: '(', self currentClass asJavascript;
  818. nextPutAll: '.superclass||$boot.nilAsClass).fn.prototype.';
  819. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  820. nextPutAll: '$recv(self), '.
  821. self
  822. visitInstructionList: anIRSend arguments
  823. enclosedBetween: '[' and: ']'.
  824. self stream
  825. nextPutAll: '));'; lf;
  826. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  827. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  828. nextPutAll: '//>>excludeEnd("ctx");'
  829. ! !
  830. Object subclass: #JSStream
  831. instanceVariableNames: 'stream'
  832. package: 'Compiler-IR'!
  833. !JSStream methodsFor: 'accessing'!
  834. contents
  835. ^ stream contents
  836. ! !
  837. !JSStream methodsFor: 'initialization'!
  838. initialize
  839. super initialize.
  840. stream := '' writeStream.
  841. ! !
  842. !JSStream methodsFor: 'streaming'!
  843. lf
  844. stream lf
  845. !
  846. nextPut: aString
  847. stream nextPut: aString
  848. !
  849. nextPutAll: aString
  850. stream nextPutAll: aString
  851. !
  852. nextPutAssignLhs: aBlock rhs: anotherBlock
  853. aBlock value.
  854. stream nextPutAll: '='.
  855. anotherBlock value
  856. !
  857. nextPutBlockContextFor: anIRClosure during: aBlock
  858. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  859. self
  860. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  861. lf;
  862. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  863. lf;
  864. nextPutAll: '//>>excludeEnd("ctx");';
  865. lf.
  866. aBlock value.
  867. self
  868. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  869. lf;
  870. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  871. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  872. anIRClosure locals
  873. do: [ :each |
  874. self
  875. nextPutAll: each asVariableName;
  876. nextPutAll: ':';
  877. nextPutAll: each asVariableName ]
  878. separatedBy: [ self nextPutAll: ',' ].
  879. self
  880. nextPutAll: '},';
  881. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  882. lf;
  883. nextPutAll: '//>>excludeEnd("ctx");'
  884. !
  885. nextPutClosureWith: aBlock arguments: anArray
  886. stream nextPutAll: '(function('.
  887. anArray
  888. do: [ :each | stream nextPutAll: each asVariableName ]
  889. separatedBy: [ stream nextPut: ',' ].
  890. stream nextPutAll: '){'; lf.
  891. aBlock value.
  892. stream lf; nextPutAll: '})'
  893. !
  894. nextPutContextFor: aMethod during: aBlock
  895. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  896. self
  897. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  898. lf;
  899. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  900. lf;
  901. nextPutAll: '//>>excludeEnd("ctx");';
  902. lf.
  903. aBlock value.
  904. self
  905. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  906. lf;
  907. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  908. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  909. aMethod locals
  910. do: [ :each |
  911. self
  912. nextPutAll: each asVariableName;
  913. nextPutAll: ':';
  914. nextPutAll: each asVariableName ]
  915. separatedBy: [ self nextPutAll: ',' ].
  916. self
  917. nextPutAll: '},';
  918. nextPutAll: aMethod theClass asJavascript;
  919. nextPutAll: ')});';
  920. lf;
  921. nextPutAll: '//>>excludeEnd("ctx");'
  922. !
  923. nextPutFunctionWith: aBlock arguments: anArray
  924. stream nextPutAll: 'fn: function('.
  925. anArray
  926. do: [ :each | stream nextPutAll: each asVariableName ]
  927. separatedBy: [ stream nextPut: ',' ].
  928. stream nextPutAll: '){'; lf.
  929. stream nextPutAll: 'var self=this;'; lf.
  930. aBlock value.
  931. stream lf; nextPutAll: '}'
  932. !
  933. nextPutIf: aBlock then: anotherBlock
  934. stream nextPutAll: 'if('.
  935. aBlock value.
  936. stream nextPutAll: '){'; lf.
  937. anotherBlock value.
  938. stream nextPutAll: '}'
  939. !
  940. nextPutIf: aBlock then: ifBlock else: elseBlock
  941. stream nextPutAll: 'if('.
  942. aBlock value.
  943. stream nextPutAll: '){'; lf.
  944. ifBlock value.
  945. stream nextPutAll: '} else {'; lf.
  946. elseBlock value.
  947. stream nextPutAll: '}'
  948. !
  949. nextPutMethodDeclaration: aMethod with: aBlock
  950. stream
  951. nextPutAll: '$core.method({'; lf;
  952. nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
  953. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  954. aBlock value.
  955. stream
  956. nextPutAll: ',', String lf, 'messageSends: ';
  957. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  958. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  959. nextPutAll: 'referencedClasses: ['.
  960. aMethod classReferences
  961. do: [ :each | stream nextPutAll: each asJavascript ]
  962. separatedBy: [ stream nextPutAll: ',' ].
  963. stream
  964. nextPutAll: ']';
  965. nextPutAll: '})'
  966. !
  967. nextPutNonLocalReturnHandlingWith: aBlock
  968. stream
  969. nextPutAll: 'var $early={};'; lf;
  970. nextPutAll: 'try {'; lf.
  971. aBlock value.
  972. stream
  973. nextPutAll: '}'; lf;
  974. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  975. !
  976. nextPutNonLocalReturnWith: aBlock
  977. stream nextPutAll: 'throw $early=['.
  978. aBlock value.
  979. stream nextPutAll: ']'
  980. !
  981. nextPutReturnWith: aBlock
  982. stream nextPutAll: 'return '.
  983. aBlock value
  984. !
  985. nextPutSendIndexFor: anIRSend
  986. self
  987. nextPutAll: ';'; lf;
  988. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  989. nextPutAll: anIRSend scope alias;
  990. nextPutAll: '.sendIdx[';
  991. nextPutAll: anIRSend selector asJavascript;
  992. nextPutAll: ']=';
  993. nextPutAll: anIRSend index asString;
  994. nextPutAll: ';'; lf;
  995. nextPutAll: '//>>excludeEnd("ctx")'
  996. !
  997. nextPutSequenceWith: aBlock
  998. "stream
  999. nextPutAll: 'switch($core.thisContext.pc){'; lf."
  1000. aBlock value.
  1001. "stream
  1002. nextPutAll: '};'; lf"
  1003. !
  1004. nextPutStatementWith: aBlock
  1005. aBlock value.
  1006. stream nextPutAll: ';'; lf
  1007. !
  1008. nextPutVars: aCollection
  1009. aCollection ifNotEmpty: [
  1010. stream nextPutAll: 'var '.
  1011. aCollection
  1012. do: [ :each | stream nextPutAll: each ]
  1013. separatedBy: [ stream nextPutAll: ',' ].
  1014. stream nextPutAll: ';'; lf ]
  1015. ! !
  1016. !BlockClosure methodsFor: '*Compiler-IR'!
  1017. appendToInstruction: anIRInstruction
  1018. anIRInstruction appendBlock: self
  1019. ! !