Compiler-IR.st 26 KB

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