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: 'testing'!
  253. isClosure
  254. ^ false
  255. !
  256. isInlined
  257. ^ false
  258. !
  259. isMethod
  260. ^ false
  261. !
  262. isSelf
  263. ^ false
  264. !
  265. isSend
  266. ^ false
  267. !
  268. isSequence
  269. ^ false
  270. !
  271. isSuper
  272. ^ false
  273. !
  274. isTempDeclaration
  275. ^ false
  276. !
  277. isVariable
  278. ^ false
  279. !
  280. needsBoxingAsReceiver
  281. ^ true
  282. !
  283. yieldsValue
  284. ^ true
  285. ! !
  286. !IRInstruction class methodsFor: 'instance creation'!
  287. on: aBuilder
  288. ^ self new
  289. builder: aBuilder;
  290. yourself
  291. ! !
  292. IRInstruction subclass: #IRAssignment
  293. slots: {}
  294. package: 'Compiler-IR'!
  295. !IRAssignment methodsFor: 'accessing'!
  296. left
  297. ^ self dagChildren first
  298. !
  299. right
  300. ^ self dagChildren last
  301. ! !
  302. !IRAssignment methodsFor: 'visiting'!
  303. acceptDagVisitor: aVisitor
  304. ^ aVisitor visitIRAssignment: self
  305. ! !
  306. IRInstruction subclass: #IRDynamicArray
  307. slots: {}
  308. package: 'Compiler-IR'!
  309. !IRDynamicArray methodsFor: 'visiting'!
  310. acceptDagVisitor: aVisitor
  311. ^ aVisitor visitIRDynamicArray: self
  312. ! !
  313. IRInstruction subclass: #IRDynamicDictionary
  314. slots: {}
  315. package: 'Compiler-IR'!
  316. !IRDynamicDictionary methodsFor: 'visiting'!
  317. acceptDagVisitor: aVisitor
  318. ^ aVisitor visitIRDynamicDictionary: self
  319. ! !
  320. IRInstruction subclass: #IRScopedInstruction
  321. slots: {#scope}
  322. package: 'Compiler-IR'!
  323. !IRScopedInstruction methodsFor: 'accessing'!
  324. scope
  325. ^ scope
  326. !
  327. scope: aScope
  328. scope := aScope
  329. ! !
  330. IRScopedInstruction subclass: #IRClosureInstruction
  331. slots: {#arguments. #requiresSmalltalkContext}
  332. package: 'Compiler-IR'!
  333. !IRClosureInstruction methodsFor: 'accessing'!
  334. arguments
  335. ^ arguments ifNil: [ #() ]
  336. !
  337. arguments: aCollection
  338. arguments := aCollection
  339. !
  340. locals
  341. ^ self arguments, (self tempDeclarations collect: [ :each | each name ])
  342. !
  343. requiresSmalltalkContext
  344. ^ requiresSmalltalkContext ifNil: [ false ]
  345. !
  346. requiresSmalltalkContext: anObject
  347. requiresSmalltalkContext := anObject
  348. !
  349. scope: aScope
  350. super scope: aScope.
  351. aScope instruction: self
  352. !
  353. tempDeclarations
  354. ^ self dagChildren select: [ :each |
  355. each isTempDeclaration ]
  356. ! !
  357. IRClosureInstruction subclass: #IRClosure
  358. slots: {}
  359. package: 'Compiler-IR'!
  360. !IRClosure methodsFor: 'accessing'!
  361. sequence
  362. ^ self dagChildren last
  363. ! !
  364. !IRClosure methodsFor: 'testing'!
  365. isClosure
  366. ^ true
  367. ! !
  368. !IRClosure methodsFor: 'visiting'!
  369. acceptDagVisitor: aVisitor
  370. ^ aVisitor visitIRClosure: self
  371. ! !
  372. IRClosureInstruction subclass: #IRMethod
  373. slots: {#theClass. #source. #compiledSource. #attachments. #selector. #pragmas. #classReferences. #sendIndexes. #internalVariables}
  374. package: 'Compiler-IR'!
  375. !IRMethod commentStamp!
  376. I am a method instruction!
  377. !IRMethod methodsFor: 'accessing'!
  378. attachments
  379. ^ attachments ifNil: [ attachments := #{} ]
  380. !
  381. classReferences
  382. ^ classReferences
  383. !
  384. classReferences: aCollection
  385. classReferences := aCollection
  386. !
  387. compiledSource
  388. ^ compiledSource
  389. !
  390. compiledSource: anObject
  391. compiledSource := anObject
  392. !
  393. internalVariables
  394. ^ internalVariables ifNil: [ internalVariables := Set new ]
  395. !
  396. messageSends
  397. ^ self sendIndexes keys
  398. !
  399. method
  400. ^ self
  401. !
  402. pragmas
  403. ^ pragmas
  404. !
  405. pragmas: aCollection
  406. pragmas := aCollection
  407. !
  408. selector
  409. ^ selector
  410. !
  411. selector: aString
  412. selector := aString
  413. !
  414. sendIndexes
  415. ^ sendIndexes
  416. !
  417. sendIndexes: aDictionary
  418. sendIndexes := aDictionary
  419. !
  420. source
  421. ^ source
  422. !
  423. source: aString
  424. source := aString
  425. !
  426. theClass
  427. ^ theClass
  428. !
  429. theClass: aClass
  430. theClass := aClass
  431. ! !
  432. !IRMethod methodsFor: 'testing'!
  433. isMethod
  434. ^ true
  435. ! !
  436. !IRMethod methodsFor: 'visiting'!
  437. acceptDagVisitor: aVisitor
  438. ^ aVisitor visitIRMethod: self
  439. ! !
  440. IRScopedInstruction subclass: #IRReturn
  441. slots: {}
  442. package: 'Compiler-IR'!
  443. !IRReturn commentStamp!
  444. I am a local return instruction.!
  445. !IRReturn methodsFor: 'accessing'!
  446. expression
  447. ^ self dagChildren single
  448. !
  449. scope
  450. ^ scope ifNil: [ self parent scope ]
  451. ! !
  452. !IRReturn methodsFor: 'testing'!
  453. yieldsValue
  454. ^ false
  455. ! !
  456. !IRReturn methodsFor: 'visiting'!
  457. acceptDagVisitor: aVisitor
  458. ^ aVisitor visitIRReturn: self
  459. ! !
  460. IRReturn subclass: #IRBlockReturn
  461. slots: {}
  462. package: 'Compiler-IR'!
  463. !IRBlockReturn commentStamp!
  464. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  465. !IRBlockReturn methodsFor: 'visiting'!
  466. acceptDagVisitor: aVisitor
  467. ^ aVisitor visitIRBlockReturn: self
  468. ! !
  469. IRReturn subclass: #IRNonLocalReturn
  470. slots: {}
  471. package: 'Compiler-IR'!
  472. !IRNonLocalReturn commentStamp!
  473. I am a non local return instruction.
  474. Non local returns are handled using a try/catch JavaScript statement.
  475. See `IRNonLocalReturnHandling` class.!
  476. !IRNonLocalReturn methodsFor: 'visiting'!
  477. acceptDagVisitor: aVisitor
  478. ^ aVisitor visitIRNonLocalReturn: self
  479. ! !
  480. IRScopedInstruction subclass: #IRTempDeclaration
  481. slots: {#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. acceptDagVisitor: aVisitor
  496. ^ aVisitor visitIRTempDeclaration: self
  497. ! !
  498. IRInstruction subclass: #IRSend
  499. slots: {#selector. #javaScriptSelector. #index}
  500. package: 'Compiler-IR'!
  501. !IRSend commentStamp!
  502. I am a message send instruction.!
  503. !IRSend methodsFor: 'accessing'!
  504. arguments
  505. ^ self dagChildren allButFirst
  506. !
  507. index
  508. ^ index
  509. !
  510. index: anInteger
  511. index := anInteger
  512. !
  513. javaScriptSelector
  514. ^ javaScriptSelector ifNil: [ javaScriptSelector := self selector asJavaScriptMethodName ]
  515. !
  516. javaScriptSelector: aString
  517. javaScriptSelector := aString
  518. !
  519. receiver
  520. ^ self dagChildren first
  521. !
  522. selector
  523. ^ selector
  524. !
  525. selector: aString
  526. selector := aString
  527. ! !
  528. !IRSend methodsFor: 'testing'!
  529. isSend
  530. ^ true
  531. ! !
  532. !IRSend methodsFor: 'visiting'!
  533. acceptDagVisitor: aVisitor
  534. ^ aVisitor visitIRSend: self
  535. ! !
  536. IRInstruction subclass: #IRSequence
  537. slots: {}
  538. package: 'Compiler-IR'!
  539. !IRSequence methodsFor: 'testing'!
  540. isSequence
  541. ^ true
  542. ! !
  543. !IRSequence methodsFor: 'visiting'!
  544. acceptDagVisitor: aVisitor
  545. ^ aVisitor visitIRSequence: self
  546. ! !
  547. IRSequence subclass: #IRBlockSequence
  548. slots: {}
  549. package: 'Compiler-IR'!
  550. !IRBlockSequence methodsFor: 'visiting'!
  551. acceptDagVisitor: aVisitor
  552. ^ aVisitor visitIRBlockSequence: self
  553. ! !
  554. IRInstruction subclass: #IRValue
  555. slots: {#value}
  556. package: 'Compiler-IR'!
  557. !IRValue commentStamp!
  558. I am the simplest possible instruction. I represent a value.!
  559. !IRValue methodsFor: 'accessing'!
  560. value
  561. ^ value
  562. !
  563. value: aString
  564. value := aString
  565. ! !
  566. !IRValue methodsFor: 'testing'!
  567. needsBoxingAsReceiver
  568. ^ false
  569. ! !
  570. !IRValue methodsFor: 'visiting'!
  571. acceptDagVisitor: aVisitor
  572. ^ aVisitor visitIRValue: self
  573. ! !
  574. IRInstruction subclass: #IRVariable
  575. slots: {#variable}
  576. package: 'Compiler-IR'!
  577. !IRVariable commentStamp!
  578. I am a variable instruction.!
  579. !IRVariable methodsFor: 'accessing'!
  580. variable
  581. ^ variable
  582. !
  583. variable: aScopeVariable
  584. variable := aScopeVariable
  585. ! !
  586. !IRVariable methodsFor: 'testing'!
  587. isSelf
  588. ^ self variable isSelf
  589. !
  590. isSuper
  591. ^ self variable isSuper
  592. !
  593. isVariable
  594. ^ true
  595. !
  596. needsBoxingAsReceiver
  597. ^ self variable isPseudoVar not
  598. ! !
  599. !IRVariable methodsFor: 'visiting'!
  600. acceptDagVisitor: aVisitor
  601. ^ aVisitor visitIRVariable: self
  602. ! !
  603. IRInstruction subclass: #IRVerbatim
  604. slots: {#source}
  605. package: 'Compiler-IR'!
  606. !IRVerbatim methodsFor: 'accessing'!
  607. source
  608. ^ source
  609. !
  610. source: aString
  611. source := aString
  612. ! !
  613. !IRVerbatim methodsFor: 'visiting'!
  614. acceptDagVisitor: aVisitor
  615. ^ aVisitor visitIRVerbatim: self
  616. ! !
  617. Object subclass: #IRPragmator
  618. slots: {#irMethod}
  619. package: 'Compiler-IR'!
  620. !IRPragmator methodsFor: 'accessing'!
  621. irMethod
  622. ^ irMethod
  623. !
  624. irMethod: anObject
  625. irMethod := anObject
  626. ! !
  627. !IRPragmator methodsFor: 'visiting'!
  628. value: anIRMethod
  629. self irMethod: anIRMethod.
  630. self processPragmas: anIRMethod pragmas.
  631. ^ anIRMethod
  632. ! !
  633. IRPragmator subclass: #IRLatePragmator
  634. slots: {}
  635. package: 'Compiler-IR'!
  636. !IRLatePragmator methodsFor: 'pragmas'!
  637. jsOverride: aString
  638. self irMethod attachments
  639. at: aString
  640. put: (NativeFunction
  641. constructorNamed: #Function
  642. value: 'return this.', irMethod selector asJavaScriptMethodName, '()')
  643. ! !
  644. ParentFakingPathDagVisitor subclass: #IRVisitor
  645. slots: {}
  646. package: 'Compiler-IR'!
  647. !IRVisitor methodsFor: 'visiting'!
  648. visitDagNode: aNode
  649. ^ self visitDagNodeVariantSimple: aNode
  650. !
  651. visitIRAssignment: anIRAssignment
  652. ^ self visitDagNode: anIRAssignment
  653. !
  654. visitIRBlockReturn: anIRBlockReturn
  655. ^ self visitIRReturn: anIRBlockReturn
  656. !
  657. visitIRBlockSequence: anIRBlockSequence
  658. ^ self visitIRSequence: anIRBlockSequence
  659. !
  660. visitIRClosure: anIRClosure
  661. ^ self visitDagNode: anIRClosure
  662. !
  663. visitIRDynamicArray: anIRDynamicArray
  664. ^ self visitDagNode: anIRDynamicArray
  665. !
  666. visitIRDynamicDictionary: anIRDynamicDictionary
  667. ^ self visitDagNode: anIRDynamicDictionary
  668. !
  669. visitIRInlinedClosure: anIRInlinedClosure
  670. ^ self visitIRClosure: anIRInlinedClosure
  671. !
  672. visitIRInlinedSequence: anIRInlinedSequence
  673. ^ self visitIRSequence: anIRInlinedSequence
  674. !
  675. visitIRMethod: anIRMethod
  676. ^ self visitDagNode: anIRMethod
  677. !
  678. visitIRNonLocalReturn: anIRNonLocalReturn
  679. ^ self visitDagNode: anIRNonLocalReturn
  680. !
  681. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  682. ^ self visitDagNode: anIRNonLocalReturnHandling
  683. !
  684. visitIRReturn: anIRReturn
  685. ^ self visitDagNode: anIRReturn
  686. !
  687. visitIRSend: anIRSend
  688. ^ self visitDagNode: anIRSend
  689. !
  690. visitIRSequence: anIRSequence
  691. ^ self visitDagNode: anIRSequence
  692. !
  693. visitIRTempDeclaration: anIRTempDeclaration
  694. ^ self visitDagNode: anIRTempDeclaration
  695. !
  696. visitIRValue: anIRValue
  697. ^ self visitDagNode: anIRValue
  698. !
  699. visitIRVariable: anIRVariable
  700. ^ self visitDagNode: anIRVariable
  701. !
  702. visitIRVerbatim: anIRVerbatim
  703. ^ self visitDagNode: anIRVerbatim
  704. ! !
  705. IRVisitor subclass: #IRJSTranslator
  706. slots: {#stream. #currentClass}
  707. package: 'Compiler-IR'!
  708. !IRJSTranslator methodsFor: 'accessing'!
  709. contents
  710. ^ self stream contents
  711. !
  712. currentClass
  713. ^ currentClass
  714. !
  715. currentClass: aClass
  716. currentClass := aClass
  717. !
  718. stream
  719. ^ stream
  720. !
  721. stream: aStream
  722. stream := aStream
  723. ! !
  724. !IRJSTranslator methodsFor: 'initialization'!
  725. initialize
  726. super initialize.
  727. stream := JSStream new.
  728. ! !
  729. !IRJSTranslator methodsFor: 'visiting'!
  730. visitIRAssignment: anIRAssignment
  731. self stream
  732. nextPutAssignLhs: [self visit: anIRAssignment left]
  733. rhs: [self visit: anIRAssignment right].
  734. !
  735. visitIRClosure: anIRClosure
  736. self stream
  737. nextPutClosureWith: [
  738. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  739. each name asVariableName ]).
  740. self stream
  741. nextPutBlockContextFor: anIRClosure
  742. during: [ super visitIRClosure: anIRClosure ] ]
  743. arguments: anIRClosure arguments
  744. !
  745. visitIRDynamicArray: anIRDynamicArray
  746. self
  747. visitInstructionList: anIRDynamicArray dagChildren
  748. enclosedBetween: '[' and: ']'
  749. !
  750. visitIRDynamicDictionary: anIRDynamicDictionary
  751. self
  752. visitInstructionList: anIRDynamicDictionary dagChildren
  753. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  754. !
  755. visitIRMethod: anIRMethod
  756. self stream
  757. nextPutFunctionWith: [
  758. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  759. each name asVariableName ]).
  760. self stream nextPutContextFor: anIRMethod during: [
  761. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  762. self stream nextPutVars:
  763. (internalVars collect: [ :each | each variable alias ]) asSet ].
  764. anIRMethod scope hasNonLocalReturn
  765. ifTrue: [
  766. self stream nextPutNonLocalReturnHandlingWith: [
  767. super visitIRMethod: anIRMethod ] ]
  768. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  769. arguments: anIRMethod arguments.
  770. ^ anIRMethod compiledSource: self contents; yourself
  771. !
  772. visitIRNonLocalReturn: anIRNonLocalReturn
  773. self stream nextPutNonLocalReturnWith: [
  774. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  775. !
  776. visitIRReturn: anIRReturn
  777. self stream nextPutReturnWith: [
  778. super visitIRReturn: anIRReturn ]
  779. !
  780. visitIRSend: anIRSend
  781. | sends superclass |
  782. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  783. anIRSend receiver isSuper
  784. ifTrue: [ self visitSuperSend: anIRSend ]
  785. ifFalse: [ self visitSend: anIRSend ].
  786. anIRSend index < sends
  787. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  788. !
  789. visitIRSequence: anIRSequence
  790. anIRSequence dagChildren do: [ :each |
  791. self stream nextPutStatementWith: [ self visit: each ] ]
  792. !
  793. visitIRTempDeclaration: anIRTempDeclaration
  794. "self stream
  795. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  796. lf"
  797. !
  798. visitIRValue: anIRValue
  799. self stream nextPutAll: anIRValue value asJavaScriptSource
  800. !
  801. visitIRVariable: anIRVariable
  802. self stream nextPutAll: anIRVariable variable alias
  803. !
  804. visitIRVerbatim: anIRVerbatim
  805. self stream nextPutAll: anIRVerbatim source
  806. !
  807. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  808. self stream nextPutAll: aString.
  809. anArray
  810. do: [ :each | self visit: each ]
  811. separatedBy: [ self stream nextPutAll: ',' ].
  812. stream nextPutAll: anotherString
  813. !
  814. visitReceiver: anIRInstruction
  815. | instr |
  816. anIRInstruction isSelf
  817. ifTrue: [ instr := anIRInstruction copy
  818. variable: (anIRInstruction variable copy name: '$self'; yourself);
  819. yourself ]
  820. ifFalse: [ instr := anIRInstruction ].
  821. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  822. self stream nextPutAll: '$recv('.
  823. self visit: instr.
  824. self stream nextPutAll: ')'
  825. !
  826. visitSend: anIRSend
  827. self visitReceiver: anIRSend receiver.
  828. self stream nextPutAll: '.', anIRSend javaScriptSelector.
  829. self
  830. visitInstructionList: anIRSend arguments
  831. enclosedBetween: '(' and: ')'
  832. !
  833. visitSuperSend: anIRSend
  834. self stream nextPutSupercallFor: anIRSend with: [
  835. self stream
  836. nextPutAll: anIRSend receiver variable lookupAsJavaScriptSource, '.';
  837. nextPutAll: anIRSend javaScriptSelector, '.call'.
  838. self
  839. visitInstructionList: {anIRSend receiver}, anIRSend arguments
  840. enclosedBetween: '(' and: ')' ]
  841. ! !
  842. Object subclass: #JSStream
  843. slots: {#stream. #omitSemicolon}
  844. package: 'Compiler-IR'!
  845. !JSStream methodsFor: 'accessing'!
  846. contents
  847. ^ stream contents
  848. !
  849. omitSemicolon
  850. ^ omitSemicolon
  851. !
  852. omitSemicolon: aBoolean
  853. omitSemicolon := aBoolean
  854. ! !
  855. !JSStream methodsFor: 'initialization'!
  856. initialize
  857. super initialize.
  858. stream := '' writeStream.
  859. ! !
  860. !JSStream methodsFor: 'streaming'!
  861. lf
  862. stream lf
  863. !
  864. nextPut: aString
  865. stream nextPut: aString
  866. !
  867. nextPutAll: aString
  868. stream nextPutAll: aString
  869. !
  870. nextPutAssignLhs: aBlock rhs: anotherBlock
  871. aBlock value.
  872. stream nextPutAll: '='.
  873. anotherBlock value
  874. !
  875. nextPutBlockContextFor: anIRClosure during: aBlock
  876. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  877. self
  878. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  879. lf;
  880. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  881. lf;
  882. nextPutAll: '//>>excludeEnd("ctx");';
  883. lf.
  884. aBlock value.
  885. self
  886. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  887. lf;
  888. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  889. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  890. anIRClosure locals
  891. do: [ :each |
  892. self
  893. nextPutAll: each asVariableName;
  894. nextPutAll: ':';
  895. nextPutAll: each asVariableName ]
  896. separatedBy: [ self nextPutAll: ',' ].
  897. self
  898. nextPutAll: '},';
  899. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  900. lf;
  901. nextPutAll: '//>>excludeEnd("ctx");'
  902. !
  903. nextPutClosureWith: aBlock arguments: anArray
  904. stream nextPutAll: '(function('.
  905. anArray
  906. do: [ :each | stream nextPutAll: each asVariableName ]
  907. separatedBy: [ stream nextPut: ',' ].
  908. stream nextPutAll: '){'; lf.
  909. aBlock value.
  910. stream lf; nextPutAll: '})'
  911. !
  912. nextPutContextFor: aMethod during: aBlock
  913. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  914. self
  915. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  916. lf;
  917. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  918. lf;
  919. nextPutAll: '//>>excludeEnd("ctx");';
  920. lf.
  921. aBlock value.
  922. self
  923. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  924. lf;
  925. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  926. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  927. aMethod locals
  928. do: [ :each |
  929. self
  930. nextPutAll: each asVariableName;
  931. nextPutAll: ':';
  932. nextPutAll: each asVariableName ]
  933. separatedBy: [ self nextPutAll: ',' ].
  934. self
  935. nextPutAll: '})});';
  936. lf;
  937. nextPutAll: '//>>excludeEnd("ctx");'
  938. !
  939. nextPutFunctionWith: aBlock arguments: anArray
  940. stream nextPutAll: 'function ('.
  941. anArray
  942. do: [ :each | stream nextPutAll: each asVariableName ]
  943. separatedBy: [ stream nextPut: ',' ].
  944. stream nextPutAll: '){'; lf.
  945. stream nextPutAll: 'var self=this,$self=this;'; lf.
  946. aBlock value.
  947. stream lf; nextPutAll: '}'
  948. !
  949. nextPutIf: aBlock then: anotherBlock
  950. stream nextPutAll: 'if('.
  951. aBlock value.
  952. stream nextPutAll: '){'; lf.
  953. anotherBlock value.
  954. stream nextPutAll: '}'.
  955. self omitSemicolon: true
  956. !
  957. nextPutIf: aBlock then: ifBlock else: elseBlock
  958. stream nextPutAll: 'if('.
  959. aBlock value.
  960. stream nextPutAll: '){'; lf.
  961. ifBlock value.
  962. stream nextPutAll: '} else {'; lf.
  963. elseBlock value.
  964. stream nextPutAll: '}'.
  965. self omitSemicolon: true
  966. !
  967. nextPutNonLocalReturnHandlingWith: aBlock
  968. stream
  969. nextPutAll: 'var $early={};'; lf;
  970. nextPutAll: 'try {'; lf.
  971. aBlock value.
  972. stream
  973. nextPutAll: '}'; lf;
  974. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  975. !
  976. nextPutNonLocalReturnWith: aBlock
  977. stream nextPutAll: 'throw $early=['.
  978. aBlock value.
  979. stream nextPutAll: ']'
  980. !
  981. nextPutReturnWith: aBlock
  982. stream nextPutAll: 'return '.
  983. aBlock value
  984. !
  985. nextPutSendIndexFor: anIRSend
  986. self
  987. nextPutAll: ';'; lf;
  988. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  989. nextPutAll: anIRSend scope alias;
  990. nextPutAll: '.sendIdx[';
  991. nextPutAll: anIRSend selector asJavaScriptSource;
  992. nextPutAll: ']=';
  993. nextPutAll: anIRSend index asString;
  994. nextPutAll: ';'; lf;
  995. nextPutAll: '//>>excludeEnd("ctx")'
  996. !
  997. nextPutStatementWith: aBlock
  998. self omitSemicolon: false.
  999. aBlock value.
  1000. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  1001. self omitSemicolon: false.
  1002. stream lf
  1003. !
  1004. nextPutSupercallFor: anIRSend with: aBlock
  1005. self
  1006. nextPutAll: '('; lf;
  1007. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  1008. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  1009. nextPutAll: '//>>excludeEnd("ctx");'; lf.
  1010. aBlock value.
  1011. self
  1012. nextPutAll: ');'; lf;
  1013. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  1014. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  1015. nextPutAll: '//>>excludeEnd("ctx");'
  1016. !
  1017. nextPutVars: aCollection
  1018. aCollection ifNotEmpty: [
  1019. stream nextPutAll: 'var '.
  1020. aCollection
  1021. do: [ :each | stream nextPutAll: each ]
  1022. separatedBy: [ stream nextPutAll: ',' ].
  1023. stream nextPutAll: ';'; lf ]
  1024. ! !
  1025. IRPragmator setTraitComposition: {TPragmator} asTraitComposition!
  1026. ! !
  1027. !ASTNode methodsFor: '*Compiler-IR'!
  1028. isReferenced
  1029. "Answer true if the receiver is referenced by other nodes.
  1030. Do not take sequences or assignments into account"
  1031. self parent isSequenceNode ifTrue: [ ^ false ].
  1032. self parent isAssignmentNode ifTrue: [ ^ false ].
  1033. self parent isCascadeNode ifTrue: [ ^ self parent isReferenced ].
  1034. ^ true
  1035. !
  1036. subtreeNeedsAliasing
  1037. ^ self shouldBeAliased or: [
  1038. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1039. ! !
  1040. !AssignmentNode methodsFor: '*Compiler-IR'!
  1041. shouldBeAliased
  1042. ^ super shouldBeAliased or: [ self isReferenced ]
  1043. ! !
  1044. !BlockClosure methodsFor: '*Compiler-IR'!
  1045. appendToInstruction: anIRInstruction
  1046. anIRInstruction appendBlock: self
  1047. ! !
  1048. !BlockNode methodsFor: '*Compiler-IR'!
  1049. subtreeNeedsAliasing
  1050. ^ self shouldBeAliased
  1051. ! !
  1052. !CascadeNode methodsFor: '*Compiler-IR'!
  1053. subtreeNeedsAliasing
  1054. ^ self parent isSequenceNode not
  1055. ! !
  1056. !SendNode methodsFor: '*Compiler-IR'!
  1057. shouldBeAliased
  1058. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  1059. See IRJSVisitor >> visitIRSend:"
  1060. | sends |
  1061. super shouldBeAliased ifTrue: [ ^ true ].
  1062. self isReferenced ifFalse: [ ^ false ].
  1063. self superSend ifTrue: [ ^ true ].
  1064. sends := (self method sendIndexes at: self selector) size.
  1065. ^ self index < sends
  1066. ! !