1
0

Compiler-IR.st 24 KB

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