Compiler-IR.st 26 KB

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