1
0

Compiler-IR.st 25 KB

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