2
0

Compiler-IR.st 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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._from_(['.
  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 asArray 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: self currentClass asJavascript;
  810. nextPutAll: '.superclass.fn.prototype.';
  811. nextPutAll: anIRSend selector asSelector, '.apply(';
  812. nextPutAll: '_st('.
  813. self visit: anIRSend instructions first.
  814. self stream nextPutAll: '), ['.
  815. anIRSend instructions allButFirst
  816. do: [ :each | self visit: each ]
  817. separatedBy: [ self stream nextPutAll: ',' ].
  818. self stream nextPutAll: '])'
  819. ! !
  820. Object subclass: #JSStream
  821. instanceVariableNames: 'stream'
  822. package: 'Compiler-IR'!
  823. !JSStream methodsFor: 'accessing'!
  824. contents
  825. ^ stream contents
  826. ! !
  827. !JSStream methodsFor: 'initialization'!
  828. initialize
  829. super initialize.
  830. stream := '' writeStream.
  831. ! !
  832. !JSStream methodsFor: 'streaming'!
  833. lf
  834. stream lf
  835. !
  836. nextPut: aString
  837. stream nextPut: aString
  838. !
  839. nextPutAll: aString
  840. stream nextPutAll: aString
  841. !
  842. nextPutAssignment
  843. stream nextPutAll: '='
  844. !
  845. nextPutBlockContextFor: anIRClosure during: aBlock
  846. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  847. self
  848. nextPutAll: 'return smalltalk.withContext(function(', anIRClosure scope alias, ') {'; lf.
  849. aBlock value.
  850. self
  851. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  852. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  853. anIRClosure locals
  854. do: [ :each |
  855. self
  856. nextPutAll: each asVariableName;
  857. nextPutAll: ':';
  858. nextPutAll: each asVariableName ]
  859. separatedBy: [ self nextPutAll: ',' ].
  860. self
  861. nextPutAll: '},';
  862. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')})'
  863. !
  864. nextPutClassRefFunction: aString
  865. "Creates an inner function $aString into method and called as `$Foo()`whenever the global is accessed.
  866. This ensures that undefined global access will answer `nil`"
  867. stream
  868. nextPutAll: 'function $';
  869. nextPutAll: aString;
  870. nextPutAll: '(){return globals.';
  871. nextPutAll: aString;
  872. nextPutAll: '||(typeof ';
  873. nextPutAll: aString;
  874. nextPutAll: '=="undefined"?nil:';
  875. nextPutAll: aString;
  876. nextPutAll: ')}';
  877. lf
  878. !
  879. nextPutClosureWith: aBlock arguments: anArray
  880. stream nextPutAll: '(function('.
  881. anArray
  882. do: [ :each | stream nextPutAll: each asVariableName ]
  883. separatedBy: [ stream nextPut: ',' ].
  884. stream nextPutAll: '){'; lf.
  885. aBlock value.
  886. stream nextPutAll: '})'
  887. !
  888. nextPutContextFor: aMethod during: aBlock
  889. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  890. self
  891. nextPutAll: 'return smalltalk.withContext(function(', aMethod scope alias, ') { '; lf.
  892. aBlock value.
  893. self
  894. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  895. nextPutAll: '.fill(self,', aMethod selector asJavascript, ',{'.
  896. aMethod locals
  897. do: [ :each |
  898. self
  899. nextPutAll: each asVariableName;
  900. nextPutAll: ':';
  901. nextPutAll: each asVariableName ]
  902. separatedBy: [ self nextPutAll: ',' ].
  903. self
  904. nextPutAll: '},';
  905. nextPutAll: aMethod theClass asJavascript;
  906. nextPutAll: ')})'
  907. !
  908. nextPutFunctionWith: aBlock arguments: anArray
  909. stream nextPutAll: 'fn: function('.
  910. anArray
  911. do: [ :each | stream nextPutAll: each asVariableName ]
  912. separatedBy: [ stream nextPut: ',' ].
  913. stream nextPutAll: '){'; lf.
  914. stream nextPutAll: 'var self=this;'; lf.
  915. aBlock value.
  916. stream nextPutAll: '}'
  917. !
  918. nextPutIf: aBlock with: anotherBlock
  919. stream nextPutAll: 'if('.
  920. aBlock value.
  921. stream nextPutAll: '){'; lf.
  922. anotherBlock value.
  923. stream nextPutAll: '}'
  924. !
  925. nextPutIfElse: aBlock with: ifBlock with: elseBlock
  926. stream nextPutAll: 'if('.
  927. aBlock value.
  928. stream nextPutAll: '){'; lf.
  929. ifBlock value.
  930. stream nextPutAll: '} else {'; lf.
  931. elseBlock value.
  932. stream nextPutAll: '}'
  933. !
  934. nextPutMethodDeclaration: aMethod with: aBlock
  935. stream
  936. nextPutAll: 'smalltalk.method({'; lf;
  937. nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
  938. nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
  939. aBlock value.
  940. stream
  941. nextPutAll: ',', String lf, 'messageSends: ';
  942. nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
  943. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
  944. nextPutAll: 'referencedClasses: ['.
  945. aMethod classReferences
  946. do: [ :each | stream nextPutAll: each asJavascript ]
  947. separatedBy: [ stream nextPutAll: ',' ].
  948. stream
  949. nextPutAll: ']';
  950. nextPutAll: '})'
  951. !
  952. nextPutNonLocalReturnHandlingWith: aBlock
  953. stream
  954. nextPutAll: 'var $early={};'; lf;
  955. nextPutAll: 'try {'; lf.
  956. aBlock value.
  957. stream
  958. nextPutAll: '}'; lf;
  959. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  960. !
  961. nextPutNonLocalReturnWith: aBlock
  962. stream nextPutAll: 'throw $early=['.
  963. aBlock value.
  964. stream nextPutAll: ']'
  965. !
  966. nextPutReturn
  967. stream nextPutAll: 'return '
  968. !
  969. nextPutReturnWith: aBlock
  970. self nextPutReturn.
  971. aBlock value
  972. !
  973. nextPutSendIndexFor: anIRSend
  974. self
  975. nextPutAll: ';'; lf;
  976. nextPutAll: anIRSend scope alias;
  977. nextPutAll: '.sendIdx[';
  978. nextPutAll: anIRSend selector asJavascript;
  979. nextPutAll: ']=';
  980. nextPutAll: anIRSend index asString
  981. !
  982. nextPutSequenceWith: aBlock
  983. "stream
  984. nextPutAll: 'switch(smalltalk.thisContext.pc){'; lf."
  985. aBlock value.
  986. "stream
  987. nextPutAll: '};'; lf"
  988. !
  989. nextPutStatementWith: aBlock
  990. aBlock value.
  991. stream nextPutAll: ';'; lf
  992. !
  993. nextPutVars: aCollection
  994. aCollection ifEmpty: [ ^ self ].
  995. stream nextPutAll: 'var '.
  996. aCollection
  997. do: [ :each | stream nextPutAll: each ]
  998. separatedBy: [ stream nextPutAll: ',' ].
  999. stream nextPutAll: ';'; lf
  1000. ! !
  1001. !BlockClosure methodsFor: '*Compiler-IR'!
  1002. appendToInstruction: anIRInstruction
  1003. anIRInstruction appendBlock: self
  1004. ! !