Compiler-IR.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. aNode nodes inject: aNode receiver into: [ :previous :each |
  114. | receiver |
  115. receiver := previous isImmutable
  116. ifTrue: [ previous ]
  117. ifFalse: [
  118. | alias |
  119. alias := self alias: previous.
  120. VariableNode new binding: alias variable ].
  121. each receiver: receiver.
  122. receiver ].
  123. aNode nodes allButLast do: [ :each |
  124. self sequence add: (self visit: each) ].
  125. ^ self visitOrAlias: aNode nodes last
  126. !
  127. visitDynamicArrayNode: aNode
  128. | array |
  129. array := IRDynamicArray new.
  130. (self aliasTemporally: aNode nodes) do: [ :each | array add: each ].
  131. ^ array
  132. !
  133. visitDynamicDictionaryNode: aNode
  134. | dictionary |
  135. dictionary := IRDynamicDictionary new.
  136. (self aliasTemporally: aNode nodes) do: [ :each | dictionary add: each ].
  137. ^ dictionary
  138. !
  139. visitJSStatementNode: aNode
  140. ^ IRVerbatim new
  141. source: aNode source crlfSanitized;
  142. yourself
  143. !
  144. visitMethodNode: aNode
  145. self method: (IRMethod new
  146. source: self source crlfSanitized;
  147. theClass: self theClass;
  148. arguments: aNode arguments;
  149. selector: aNode selector;
  150. sendIndexes: aNode sendIndexes;
  151. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  152. classReferences: aNode classReferences;
  153. scope: aNode scope;
  154. yourself).
  155. aNode scope temps do: [ :each |
  156. self method add: (IRTempDeclaration new
  157. name: each name;
  158. scope: aNode scope;
  159. yourself) ].
  160. aNode nodes do: [ :each | self method add: (self visit: each) ].
  161. aNode scope hasLocalReturn ifFalse: [self method
  162. add: (IRReturn new
  163. add: (IRVariable new
  164. variable: (aNode scope pseudoVars at: 'self');
  165. yourself);
  166. yourself);
  167. add: (IRVerbatim new source: ''; yourself) ].
  168. ^ self method
  169. !
  170. visitOrAlias: aNode
  171. ^ aNode shouldBeAliased
  172. ifTrue: [ self alias: aNode ]
  173. ifFalse: [ self visit: aNode ]
  174. !
  175. visitReturnNode: aNode
  176. | return |
  177. return := aNode nonLocalReturn
  178. ifTrue: [ IRNonLocalReturn new ]
  179. ifFalse: [ IRReturn new ].
  180. return scope: aNode scope.
  181. aNode nodes do: [ :each |
  182. return add: (self visitOrAlias: each) ].
  183. ^ return
  184. !
  185. visitSendNode: aNode
  186. | send all receiver arguments |
  187. send := IRSend new.
  188. send
  189. selector: aNode selector;
  190. index: aNode index.
  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 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. theClass
  422. ^ theClass
  423. !
  424. theClass: aClass
  425. theClass := aClass
  426. ! !
  427. !IRMethod methodsFor: 'visiting'!
  428. accept: aVisitor
  429. ^ aVisitor visitIRMethod: self
  430. ! !
  431. IRScopedInstruction subclass: #IRReturn
  432. instanceVariableNames: ''
  433. package: 'Compiler-IR'!
  434. !IRReturn commentStamp!
  435. I am a local return instruction.!
  436. !IRReturn methodsFor: 'accessing'!
  437. scope
  438. ^ scope ifNil: [ self parent scope ]
  439. ! !
  440. !IRReturn methodsFor: 'testing'!
  441. canBeAssigned
  442. ^ false
  443. !
  444. isBlockReturn
  445. ^ false
  446. !
  447. isLocalReturn
  448. ^ true
  449. !
  450. isNonLocalReturn
  451. ^ self isLocalReturn not
  452. !
  453. isReturn
  454. ^ true
  455. ! !
  456. !IRReturn methodsFor: 'visiting'!
  457. accept: aVisitor
  458. ^ aVisitor visitIRReturn: self
  459. ! !
  460. IRReturn subclass: #IRBlockReturn
  461. instanceVariableNames: ''
  462. package: 'Compiler-IR'!
  463. !IRBlockReturn commentStamp!
  464. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  465. !IRBlockReturn methodsFor: 'testing'!
  466. isBlockReturn
  467. ^ true
  468. ! !
  469. !IRBlockReturn methodsFor: 'visiting'!
  470. accept: aVisitor
  471. ^ aVisitor visitIRBlockReturn: self
  472. ! !
  473. IRReturn subclass: #IRNonLocalReturn
  474. instanceVariableNames: ''
  475. package: 'Compiler-IR'!
  476. !IRNonLocalReturn commentStamp!
  477. I am a non local return instruction.
  478. Non local returns are handled using a try/catch JavaScript statement.
  479. See `IRNonLocalReturnHandling` class.!
  480. !IRNonLocalReturn methodsFor: 'testing'!
  481. isLocalReturn
  482. ^ false
  483. ! !
  484. !IRNonLocalReturn methodsFor: 'visiting'!
  485. accept: aVisitor
  486. ^ aVisitor visitIRNonLocalReturn: self
  487. ! !
  488. IRScopedInstruction subclass: #IRTempDeclaration
  489. instanceVariableNames: 'name'
  490. package: 'Compiler-IR'!
  491. !IRTempDeclaration methodsFor: 'accessing'!
  492. name
  493. ^ name
  494. !
  495. name: aString
  496. name := aString
  497. ! !
  498. !IRTempDeclaration methodsFor: 'testing'!
  499. isTempDeclaration
  500. ^ true
  501. ! !
  502. !IRTempDeclaration methodsFor: 'visiting'!
  503. accept: aVisitor
  504. ^ aVisitor visitIRTempDeclaration: self
  505. ! !
  506. IRInstruction subclass: #IRSend
  507. instanceVariableNames: 'selector index'
  508. package: 'Compiler-IR'!
  509. !IRSend commentStamp!
  510. I am a message send instruction.!
  511. !IRSend methodsFor: 'accessing'!
  512. index
  513. ^ index
  514. !
  515. index: anInteger
  516. index := anInteger
  517. !
  518. isSuperSend
  519. | receiver |
  520. receiver := self instructions first.
  521. ^ receiver isVariable and: [ receiver variable name = 'super' ]
  522. !
  523. selector
  524. ^ selector
  525. !
  526. selector: aString
  527. selector := aString
  528. ! !
  529. !IRSend methodsFor: 'testing'!
  530. isSend
  531. ^ true
  532. ! !
  533. !IRSend methodsFor: 'visiting'!
  534. accept: aVisitor
  535. ^ aVisitor visitIRSend: self
  536. ! !
  537. IRInstruction subclass: #IRSequence
  538. instanceVariableNames: ''
  539. package: 'Compiler-IR'!
  540. !IRSequence methodsFor: 'testing'!
  541. isSequence
  542. ^ true
  543. ! !
  544. !IRSequence methodsFor: 'visiting'!
  545. accept: aVisitor
  546. ^ aVisitor visitIRSequence: self
  547. ! !
  548. IRSequence subclass: #IRBlockSequence
  549. instanceVariableNames: ''
  550. package: 'Compiler-IR'!
  551. !IRBlockSequence methodsFor: 'visiting'!
  552. accept: aVisitor
  553. ^ aVisitor visitIRBlockSequence: self
  554. ! !
  555. IRInstruction subclass: #IRValue
  556. instanceVariableNames: 'value'
  557. package: 'Compiler-IR'!
  558. !IRValue commentStamp!
  559. I am the simplest possible instruction. I represent a value.!
  560. !IRValue methodsFor: 'accessing'!
  561. value
  562. ^ value
  563. !
  564. value: aString
  565. value := aString
  566. ! !
  567. !IRValue methodsFor: 'testing'!
  568. needsBoxingAsReceiver
  569. ^ false
  570. ! !
  571. !IRValue methodsFor: 'visiting'!
  572. accept: aVisitor
  573. ^ aVisitor visitIRValue: self
  574. ! !
  575. IRInstruction subclass: #IRVariable
  576. instanceVariableNames: 'variable'
  577. package: 'Compiler-IR'!
  578. !IRVariable commentStamp!
  579. I am a variable instruction.!
  580. !IRVariable methodsFor: 'accessing'!
  581. variable
  582. ^ variable
  583. !
  584. variable: aScopeVariable
  585. variable := aScopeVariable
  586. ! !
  587. !IRVariable methodsFor: 'testing'!
  588. isVariable
  589. ^ true
  590. !
  591. needsBoxingAsReceiver
  592. ^ self variable isPseudoVar not
  593. ! !
  594. !IRVariable methodsFor: 'visiting'!
  595. accept: aVisitor
  596. ^ aVisitor visitIRVariable: self
  597. ! !
  598. IRInstruction subclass: #IRVerbatim
  599. instanceVariableNames: 'source'
  600. package: 'Compiler-IR'!
  601. !IRVerbatim methodsFor: 'accessing'!
  602. source
  603. ^ source
  604. !
  605. source: aString
  606. source := aString
  607. ! !
  608. !IRVerbatim methodsFor: 'visiting'!
  609. accept: aVisitor
  610. ^ aVisitor visitIRVerbatim: self
  611. ! !
  612. Object subclass: #IRVisitor
  613. instanceVariableNames: ''
  614. package: 'Compiler-IR'!
  615. !IRVisitor methodsFor: 'visiting'!
  616. visit: anIRInstruction
  617. ^ anIRInstruction accept: self
  618. !
  619. visitIRAssignment: anIRAssignment
  620. ^ self visitIRInstruction: anIRAssignment
  621. !
  622. visitIRBlockReturn: anIRBlockReturn
  623. ^ self visitIRReturn: anIRBlockReturn
  624. !
  625. visitIRBlockSequence: anIRBlockSequence
  626. ^ self visitIRSequence: anIRBlockSequence
  627. !
  628. visitIRClosure: anIRClosure
  629. ^ self visitIRInstruction: anIRClosure
  630. !
  631. visitIRDynamicArray: anIRDynamicArray
  632. ^ self visitIRInstruction: anIRDynamicArray
  633. !
  634. visitIRDynamicDictionary: anIRDynamicDictionary
  635. ^ self visitIRInstruction: anIRDynamicDictionary
  636. !
  637. visitIRInlinedClosure: anIRInlinedClosure
  638. ^ self visitIRClosure: anIRInlinedClosure
  639. !
  640. visitIRInlinedSequence: anIRInlinedSequence
  641. ^ self visitIRSequence: anIRInlinedSequence
  642. !
  643. visitIRInstruction: anIRInstruction
  644. anIRInstruction instructions do: [ :each | self visit: each ].
  645. ^ anIRInstruction
  646. !
  647. visitIRMethod: anIRMethod
  648. ^ self visitIRInstruction: anIRMethod
  649. !
  650. visitIRNonLocalReturn: anIRNonLocalReturn
  651. ^ self visitIRInstruction: anIRNonLocalReturn
  652. !
  653. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  654. ^ self visitIRInstruction: anIRNonLocalReturnHandling
  655. !
  656. visitIRReturn: anIRReturn
  657. ^ self visitIRInstruction: anIRReturn
  658. !
  659. visitIRSend: anIRSend
  660. ^ self visitIRInstruction: anIRSend
  661. !
  662. visitIRSequence: anIRSequence
  663. ^ self visitIRInstruction: anIRSequence
  664. !
  665. visitIRTempDeclaration: anIRTempDeclaration
  666. ^ self visitIRInstruction: anIRTempDeclaration
  667. !
  668. visitIRValue: anIRValue
  669. ^ self visitIRInstruction: anIRValue
  670. !
  671. visitIRVariable: anIRVariable
  672. ^ self visitIRInstruction: anIRVariable
  673. !
  674. visitIRVerbatim: anIRVerbatim
  675. ^ self visitIRInstruction: anIRVerbatim
  676. ! !
  677. IRVisitor subclass: #IRJSTranslator
  678. instanceVariableNames: 'stream currentClass'
  679. package: 'Compiler-IR'!
  680. !IRJSTranslator methodsFor: 'accessing'!
  681. contents
  682. ^ self stream contents
  683. !
  684. currentClass
  685. ^ currentClass
  686. !
  687. currentClass: aClass
  688. currentClass := aClass
  689. !
  690. stream
  691. ^ stream
  692. !
  693. stream: aStream
  694. stream := aStream
  695. ! !
  696. !IRJSTranslator methodsFor: 'initialization'!
  697. initialize
  698. super initialize.
  699. stream := JSStream new.
  700. ! !
  701. !IRJSTranslator methodsFor: 'visiting'!
  702. visitIRAssignment: anIRAssignment
  703. self stream
  704. nextPutAssignLhs: [self visit: anIRAssignment instructions first]
  705. rhs: [self visit: anIRAssignment instructions last].
  706. !
  707. visitIRClosure: anIRClosure
  708. self stream
  709. nextPutClosureWith: [
  710. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  711. each name asVariableName ]).
  712. self stream
  713. nextPutBlockContextFor: anIRClosure
  714. during: [ super visitIRClosure: anIRClosure ] ]
  715. arguments: anIRClosure arguments
  716. !
  717. visitIRDynamicArray: anIRDynamicArray
  718. self
  719. visitInstructionList: anIRDynamicArray instructions
  720. enclosedBetween: '[' and: ']'
  721. !
  722. visitIRDynamicDictionary: anIRDynamicDictionary
  723. self
  724. visitInstructionList: anIRDynamicDictionary instructions
  725. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  726. !
  727. visitIRMethod: anIRMethod
  728. self stream
  729. nextPutMethodDeclaration: anIRMethod
  730. with: [ self stream
  731. nextPutFunctionWith: [
  732. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  733. each name asVariableName ]).
  734. anIRMethod classReferences do: [ :each | self stream nextPutClassRefFunction: each ].
  735. self stream nextPutContextFor: anIRMethod during: [
  736. anIRMethod internalVariables notEmpty ifTrue: [
  737. self stream nextPutVars: (anIRMethod internalVariables asSet collect: [ :each |
  738. each variable alias ]) ].
  739. anIRMethod scope hasNonLocalReturn
  740. ifTrue: [
  741. self stream nextPutNonLocalReturnHandlingWith: [
  742. super visitIRMethod: anIRMethod ] ]
  743. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  744. arguments: anIRMethod arguments ]
  745. !
  746. visitIRNonLocalReturn: anIRNonLocalReturn
  747. self stream nextPutNonLocalReturnWith: [
  748. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  749. !
  750. visitIRReturn: anIRReturn
  751. self stream nextPutReturnWith: [
  752. super visitIRReturn: anIRReturn ]
  753. !
  754. visitIRSend: anIRSend
  755. | sends superclass |
  756. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  757. anIRSend isSuperSend
  758. ifTrue: [ self visitSuperSend: anIRSend ]
  759. ifFalse: [ self visitSend: anIRSend ].
  760. (sends > 1 and: [ anIRSend index < sends ])
  761. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  762. !
  763. visitIRSequence: anIRSequence
  764. self stream nextPutSequenceWith: [
  765. anIRSequence instructions do: [ :each |
  766. self stream nextPutStatementWith: (self visit: each) ] ]
  767. !
  768. visitIRTempDeclaration: anIRTempDeclaration
  769. "self stream
  770. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  771. lf"
  772. !
  773. visitIRValue: anIRValue
  774. self stream nextPutAll: anIRValue value asJavascript
  775. !
  776. visitIRVariable: anIRVariable
  777. anIRVariable variable name = 'thisContext'
  778. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  779. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  780. !
  781. visitIRVerbatim: anIRVerbatim
  782. self stream nextPutStatementWith: [
  783. self stream nextPutAll: anIRVerbatim source ]
  784. !
  785. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  786. self stream nextPutAll: aString.
  787. anArray
  788. do: [ :each | self visit: each ]
  789. separatedBy: [ self stream nextPutAll: ',' ].
  790. stream nextPutAll: anotherString
  791. !
  792. visitReceiver: anIRInstruction
  793. anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
  794. self stream nextPutAll: '$recv('.
  795. self visit: anIRInstruction.
  796. self stream nextPutAll: ')'
  797. !
  798. visitSend: anIRSend
  799. self visitReceiver: anIRSend instructions first.
  800. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  801. self
  802. visitInstructionList: anIRSend instructions allButFirst
  803. enclosedBetween: '(' and: ')'
  804. !
  805. visitSuperSend: anIRSend
  806. self stream
  807. nextPutAll: '('; lf;
  808. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  809. nextPutAll: anIRSend scope alias, '.supercall = true, '; lf;
  810. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  811. nextPutAll: '(', self currentClass asJavascript;
  812. nextPutAll: '.superclass||$boot.dnu).fn.prototype.';
  813. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  814. nextPutAll: '$recv(self), '.
  815. self
  816. visitInstructionList: anIRSend instructions allButFirst
  817. enclosedBetween: '[' and: ']'.
  818. self stream
  819. nextPutAll: '));'; lf;
  820. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  821. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  822. nextPutAll: '//>>excludeEnd("ctx");'
  823. ! !
  824. Object subclass: #JSStream
  825. instanceVariableNames: 'stream'
  826. package: 'Compiler-IR'!
  827. !JSStream methodsFor: 'accessing'!
  828. contents
  829. ^ stream contents
  830. ! !
  831. !JSStream methodsFor: 'initialization'!
  832. initialize
  833. super initialize.
  834. stream := '' writeStream.
  835. ! !
  836. !JSStream methodsFor: 'streaming'!
  837. lf
  838. stream lf
  839. !
  840. nextPut: aString
  841. stream nextPut: aString
  842. !
  843. nextPutAll: aString
  844. stream nextPutAll: aString
  845. !
  846. nextPutAssignLhs: aBlock rhs: anotherBlock
  847. aBlock value.
  848. stream nextPutAll: '='.
  849. anotherBlock value
  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 $core.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 $core.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 then: anotherBlock
  943. stream nextPutAll: 'if('.
  944. aBlock value.
  945. stream nextPutAll: '){'; lf.
  946. anotherBlock value.
  947. stream nextPutAll: '}'
  948. !
  949. nextPutIf: aBlock then: ifBlock else: 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: '$core.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. nextPutReturnWith: aBlock
  991. stream nextPutAll: 'return '.
  992. aBlock value
  993. !
  994. nextPutSendIndexFor: anIRSend
  995. self
  996. nextPutAll: ';'; lf;
  997. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  998. nextPutAll: anIRSend scope alias;
  999. nextPutAll: '.sendIdx[';
  1000. nextPutAll: anIRSend selector asJavascript;
  1001. nextPutAll: ']=';
  1002. nextPutAll: anIRSend index asString;
  1003. nextPutAll: ';'; lf;
  1004. nextPutAll: '//>>excludeEnd("ctx")'
  1005. !
  1006. nextPutSequenceWith: aBlock
  1007. "stream
  1008. nextPutAll: 'switch($core.thisContext.pc){'; lf."
  1009. aBlock value.
  1010. "stream
  1011. nextPutAll: '};'; lf"
  1012. !
  1013. nextPutStatementWith: aBlock
  1014. aBlock value.
  1015. stream nextPutAll: ';'; lf
  1016. !
  1017. nextPutVars: aCollection
  1018. aCollection ifNotEmpty: [
  1019. stream nextPutAll: 'var '.
  1020. aCollection
  1021. do: [ :each | stream nextPutAll: each ]
  1022. separatedBy: [ stream nextPutAll: ',' ].
  1023. stream nextPutAll: ';'; lf ]
  1024. ! !
  1025. !BlockClosure methodsFor: '*Compiler-IR'!
  1026. appendToInstruction: anIRInstruction
  1027. anIRInstruction appendBlock: self
  1028. ! !