2
0

Compiler-IR.st 23 KB

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