Compiler-IR.st 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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 result |
  71. threshold := 0.
  72. aCollection withIndexDo: [ :each :i |
  73. each subtreeNeedsAliasing
  74. ifTrue: [ threshold := i ] ].
  75. result := OrderedCollection new.
  76. aCollection withIndexDo: [ :each :i |
  77. result add: (i <= threshold
  78. ifTrue: [ self alias: each ]
  79. ifFalse: [ self visit: each ]) ].
  80. ^ result
  81. !
  82. visitAssignmentNode: aNode
  83. | left right assignment |
  84. right := self visit: aNode right.
  85. left := self visit: aNode left.
  86. self addToSequence: (IRAssignment new
  87. add: left;
  88. add: right;
  89. yourself).
  90. ^ left
  91. !
  92. visitBlockNode: aNode
  93. | closure |
  94. closure := IRClosure new
  95. arguments: aNode parameters;
  96. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  97. scope: aNode scope;
  98. yourself.
  99. aNode scope temps do: [ :each |
  100. closure add: (IRTempDeclaration new
  101. name: each name;
  102. scope: aNode scope;
  103. yourself) ].
  104. aNode dagChildren do: [ :each | closure add: (self visit: each) ].
  105. ^ closure
  106. !
  107. visitBlockSequenceNode: aNode
  108. ^ self
  109. withSequence: IRBlockSequence new
  110. do: [
  111. aNode dagChildren ifNotEmpty: [
  112. aNode dagChildren allButLast do: [ :each |
  113. self addToSequence: (self visitOrAlias: each) ].
  114. aNode dagChildren last isReturnNode
  115. ifFalse: [ self addToSequence: (IRBlockReturn new add: (self visitOrAlias: aNode dagChildren last); yourself) ]
  116. ifTrue: [ self addToSequence: (self visitOrAlias: aNode dagChildren last) ] ]]
  117. !
  118. visitCascadeNode: aNode
  119. | receiver |
  120. receiver := aNode receiver.
  121. receiver isImmutable ifFalse: [
  122. | alias |
  123. alias := self alias: receiver.
  124. receiver := VariableNode new binding: alias variable ].
  125. aNode dagChildren do: [ :each | each receiver: receiver ].
  126. aNode dagChildren allButLast do: [ :each |
  127. self addToSequence: (self visit: each) ].
  128. ^ self visitOrAlias: aNode dagChildren last
  129. !
  130. visitDynamicArrayNode: aNode
  131. | array |
  132. array := IRDynamicArray new.
  133. (self aliasTemporally: aNode dagChildren) do: [ :each | array add: each ].
  134. ^ array
  135. !
  136. visitDynamicDictionaryNode: aNode
  137. | dictionary |
  138. dictionary := IRDynamicDictionary new.
  139. (self aliasTemporally: aNode dagChildren) do: [ :each | dictionary add: each ].
  140. ^ dictionary
  141. !
  142. visitJSStatementNode: aNode
  143. ^ IRVerbatim new
  144. source: aNode source crlfSanitized;
  145. yourself
  146. !
  147. visitMethodNode: aNode
  148. self method: (IRMethod new
  149. source: self source crlfSanitized;
  150. pragmas: (aNode pragmas collect: [ :each |
  151. Message
  152. selector: each selector
  153. arguments: (each arguments collect: [ :eachArg |
  154. eachArg isString ifTrue: [ eachArg crlfSanitized ] ifFalse: [ eachArg ]])]);
  155. theClass: self theClass;
  156. arguments: aNode arguments;
  157. selector: aNode selector;
  158. sendIndexes: aNode sendIndexes;
  159. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  160. classReferences: aNode classReferences;
  161. scope: aNode scope;
  162. yourself).
  163. aNode scope temps do: [ :each |
  164. self method add: (IRTempDeclaration new
  165. name: each name;
  166. scope: aNode scope;
  167. yourself) ].
  168. self method dagChildren addAll: (self visitAllChildren: aNode).
  169. aNode scope hasLocalReturn ifFalse: [self method
  170. add: (IRReturn new
  171. add: (IRVariable new
  172. variable: (aNode scope pseudoVars at: 'self');
  173. yourself);
  174. yourself);
  175. add: (IRVerbatim new source: ';', String lf; yourself) ].
  176. ^ self method
  177. !
  178. visitOrAlias: aNode
  179. ^ aNode shouldBeAliased
  180. ifTrue: [ self alias: aNode ]
  181. ifFalse: [ self visit: aNode ]
  182. !
  183. visitReturnNode: aNode
  184. | return |
  185. return := aNode nonLocalReturn
  186. ifTrue: [ IRNonLocalReturn new ]
  187. ifFalse: [ IRReturn new ].
  188. return scope: aNode scope.
  189. aNode dagChildren do: [ :each |
  190. return add: (self visitOrAlias: each) ].
  191. ^ return
  192. !
  193. visitSendNode: aNode
  194. | send |
  195. send := IRSend new.
  196. send
  197. selector: aNode selector;
  198. index: aNode index.
  199. (self aliasTemporally: aNode dagChildren) do: [ :each | send add: each ].
  200. ^ send
  201. !
  202. visitSequenceNode: aNode
  203. ^ self
  204. withSequence: IRSequence new
  205. do: [ aNode dagChildren do: [ :each |
  206. self addToSequence: (self visitOrAlias: each) ] ]
  207. !
  208. visitValueNode: aNode
  209. ^ IRValue new
  210. value: aNode value;
  211. yourself
  212. !
  213. visitVariableNode: aNode
  214. ^ IRVariable new
  215. variable: aNode binding;
  216. yourself
  217. ! !
  218. DagParentNode subclass: #IRInstruction
  219. slots: {#parent}
  220. package: 'Compiler-IR'!
  221. !IRInstruction commentStamp!
  222. I am the abstract root class of the IR (intermediate representation) instructions class hierarchy.
  223. The IR graph is used to emit JavaScript code using a JSStream.!
  224. !IRInstruction methodsFor: 'accessing'!
  225. method
  226. ^ self parent method
  227. !
  228. parent
  229. ^ parent
  230. !
  231. parent: anIRInstruction
  232. parent := anIRInstruction
  233. !
  234. scope
  235. ^ self parent ifNotNil: [ :node |
  236. node scope ]
  237. ! !
  238. !IRInstruction methodsFor: 'building'!
  239. add: anObject
  240. ^ self addDagChild: anObject
  241. !
  242. remove: anIRInstruction
  243. self dagChildren remove: anIRInstruction
  244. !
  245. replace: anIRInstruction with: anotherIRInstruction
  246. anotherIRInstruction parent: self.
  247. self dagChildren
  248. at: (self dagChildren indexOf: anIRInstruction)
  249. put: anotherIRInstruction
  250. !
  251. replaceWith: anIRInstruction
  252. self parent replace: self with: anIRInstruction
  253. ! !
  254. !IRInstruction methodsFor: 'testing'!
  255. isClosure
  256. ^ false
  257. !
  258. isInlined
  259. ^ false
  260. !
  261. isMethod
  262. ^ false
  263. !
  264. isSelf
  265. ^ false
  266. !
  267. isSend
  268. ^ false
  269. !
  270. isSequence
  271. ^ false
  272. !
  273. isSuper
  274. ^ false
  275. !
  276. isTempDeclaration
  277. ^ false
  278. !
  279. isVariable
  280. ^ false
  281. !
  282. needsBoxingAsReceiver
  283. ^ true
  284. !
  285. yieldsValue
  286. ^ true
  287. ! !
  288. !IRInstruction class methodsFor: 'instance creation'!
  289. on: aBuilder
  290. ^ self new
  291. builder: aBuilder;
  292. yourself
  293. ! !
  294. IRInstruction subclass: #IRAssignment
  295. slots: {}
  296. package: 'Compiler-IR'!
  297. !IRAssignment methodsFor: 'accessing'!
  298. left
  299. ^ self dagChildren first
  300. !
  301. right
  302. ^ self dagChildren last
  303. ! !
  304. !IRAssignment methodsFor: 'visiting'!
  305. acceptDagVisitor: aVisitor
  306. ^ aVisitor visitIRAssignment: self
  307. ! !
  308. IRInstruction subclass: #IRDynamicArray
  309. slots: {}
  310. package: 'Compiler-IR'!
  311. !IRDynamicArray methodsFor: 'visiting'!
  312. acceptDagVisitor: aVisitor
  313. ^ aVisitor visitIRDynamicArray: self
  314. ! !
  315. IRInstruction subclass: #IRDynamicDictionary
  316. slots: {}
  317. package: 'Compiler-IR'!
  318. !IRDynamicDictionary methodsFor: 'visiting'!
  319. acceptDagVisitor: aVisitor
  320. ^ aVisitor visitIRDynamicDictionary: self
  321. ! !
  322. IRInstruction subclass: #IRScopedInstruction
  323. slots: {#scope}
  324. package: 'Compiler-IR'!
  325. !IRScopedInstruction methodsFor: 'accessing'!
  326. scope
  327. ^ scope
  328. !
  329. scope: aScope
  330. scope := aScope
  331. ! !
  332. IRScopedInstruction subclass: #IRClosureInstruction
  333. slots: {#arguments. #requiresSmalltalkContext}
  334. package: 'Compiler-IR'!
  335. !IRClosureInstruction methodsFor: 'accessing'!
  336. arguments
  337. ^ arguments ifNil: [ #() ]
  338. !
  339. arguments: aCollection
  340. arguments := aCollection
  341. !
  342. locals
  343. ^ self arguments, (self tempDeclarations collect: [ :each | each name ])
  344. !
  345. requiresSmalltalkContext
  346. ^ requiresSmalltalkContext ifNil: [ false ]
  347. !
  348. requiresSmalltalkContext: anObject
  349. requiresSmalltalkContext := anObject
  350. !
  351. scope: aScope
  352. super scope: aScope.
  353. aScope instruction: self
  354. !
  355. tempDeclarations
  356. ^ self dagChildren select: [ :each |
  357. each isTempDeclaration ]
  358. ! !
  359. IRClosureInstruction subclass: #IRClosure
  360. slots: {}
  361. package: 'Compiler-IR'!
  362. !IRClosure methodsFor: 'accessing'!
  363. sequence
  364. ^ self dagChildren last
  365. ! !
  366. !IRClosure methodsFor: 'testing'!
  367. isClosure
  368. ^ true
  369. ! !
  370. !IRClosure methodsFor: 'visiting'!
  371. acceptDagVisitor: aVisitor
  372. ^ aVisitor visitIRClosure: self
  373. ! !
  374. IRClosureInstruction subclass: #IRMethod
  375. slots: {#theClass. #source. #compiledSource. #attachments. #selector. #pragmas. #classReferences. #sendIndexes. #internalVariables}
  376. package: 'Compiler-IR'!
  377. !IRMethod commentStamp!
  378. I am a method instruction!
  379. !IRMethod methodsFor: 'accessing'!
  380. attachments
  381. ^ attachments ifNil: [ attachments := #{} ]
  382. !
  383. classReferences
  384. ^ classReferences
  385. !
  386. classReferences: aCollection
  387. classReferences := aCollection
  388. !
  389. compiledSource
  390. ^ compiledSource
  391. !
  392. compiledSource: anObject
  393. compiledSource := anObject
  394. !
  395. internalVariables
  396. ^ internalVariables ifNil: [ internalVariables := Set new ]
  397. !
  398. messageSends
  399. ^ self sendIndexes keys
  400. !
  401. method
  402. ^ self
  403. !
  404. pragmas
  405. ^ pragmas
  406. !
  407. pragmas: aCollection
  408. pragmas := aCollection
  409. !
  410. selector
  411. ^ selector
  412. !
  413. selector: aString
  414. selector := aString
  415. !
  416. sendIndexes
  417. ^ sendIndexes
  418. !
  419. sendIndexes: aDictionary
  420. sendIndexes := aDictionary
  421. !
  422. source
  423. ^ source
  424. !
  425. source: aString
  426. source := aString
  427. !
  428. theClass
  429. ^ theClass
  430. !
  431. theClass: aClass
  432. theClass := aClass
  433. ! !
  434. !IRMethod methodsFor: 'testing'!
  435. isMethod
  436. ^ true
  437. ! !
  438. !IRMethod methodsFor: 'visiting'!
  439. acceptDagVisitor: aVisitor
  440. ^ aVisitor visitIRMethod: self
  441. ! !
  442. IRScopedInstruction subclass: #IRReturn
  443. slots: {}
  444. package: 'Compiler-IR'!
  445. !IRReturn commentStamp!
  446. I am a local return instruction.!
  447. !IRReturn methodsFor: 'accessing'!
  448. expression
  449. ^ self dagChildren single
  450. !
  451. scope
  452. ^ scope ifNil: [ self parent scope ]
  453. ! !
  454. !IRReturn methodsFor: 'testing'!
  455. yieldsValue
  456. ^ false
  457. ! !
  458. !IRReturn methodsFor: 'visiting'!
  459. acceptDagVisitor: aVisitor
  460. ^ aVisitor visitIRReturn: self
  461. ! !
  462. IRReturn subclass: #IRBlockReturn
  463. slots: {}
  464. package: 'Compiler-IR'!
  465. !IRBlockReturn commentStamp!
  466. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  467. !IRBlockReturn methodsFor: 'visiting'!
  468. acceptDagVisitor: aVisitor
  469. ^ aVisitor visitIRBlockReturn: self
  470. ! !
  471. IRReturn subclass: #IRNonLocalReturn
  472. slots: {}
  473. package: 'Compiler-IR'!
  474. !IRNonLocalReturn commentStamp!
  475. I am a non local return instruction.
  476. Non local returns are handled using a try/catch JavaScript statement.
  477. See `IRNonLocalReturnHandling` class.!
  478. !IRNonLocalReturn methodsFor: 'visiting'!
  479. acceptDagVisitor: aVisitor
  480. ^ aVisitor visitIRNonLocalReturn: self
  481. ! !
  482. IRScopedInstruction subclass: #IRTempDeclaration
  483. slots: {#name}
  484. package: 'Compiler-IR'!
  485. !IRTempDeclaration methodsFor: 'accessing'!
  486. name
  487. ^ name
  488. !
  489. name: aString
  490. name := aString
  491. ! !
  492. !IRTempDeclaration methodsFor: 'testing'!
  493. isTempDeclaration
  494. ^ true
  495. ! !
  496. !IRTempDeclaration methodsFor: 'visiting'!
  497. acceptDagVisitor: aVisitor
  498. ^ aVisitor visitIRTempDeclaration: self
  499. ! !
  500. IRInstruction subclass: #IRSend
  501. slots: {#selector. #javaScriptSelector. #index}
  502. package: 'Compiler-IR'!
  503. !IRSend commentStamp!
  504. I am a message send instruction.!
  505. !IRSend methodsFor: 'accessing'!
  506. arguments
  507. ^ self dagChildren allButFirst
  508. !
  509. index
  510. ^ index
  511. !
  512. index: anInteger
  513. index := anInteger
  514. !
  515. javaScriptSelector
  516. ^ javaScriptSelector ifNil: [ javaScriptSelector := self selector asJavaScriptMethodName ]
  517. !
  518. javaScriptSelector: aString
  519. javaScriptSelector := aString
  520. !
  521. receiver
  522. ^ self dagChildren first
  523. !
  524. selector
  525. ^ selector
  526. !
  527. selector: aString
  528. selector := aString
  529. ! !
  530. !IRSend methodsFor: 'testing'!
  531. isSend
  532. ^ true
  533. ! !
  534. !IRSend methodsFor: 'visiting'!
  535. acceptDagVisitor: aVisitor
  536. ^ aVisitor visitIRSend: self
  537. ! !
  538. IRInstruction subclass: #IRSequence
  539. slots: {}
  540. package: 'Compiler-IR'!
  541. !IRSequence methodsFor: 'testing'!
  542. isSequence
  543. ^ true
  544. ! !
  545. !IRSequence methodsFor: 'visiting'!
  546. acceptDagVisitor: aVisitor
  547. ^ aVisitor visitIRSequence: self
  548. ! !
  549. IRSequence subclass: #IRBlockSequence
  550. slots: {}
  551. package: 'Compiler-IR'!
  552. !IRBlockSequence methodsFor: 'visiting'!
  553. acceptDagVisitor: aVisitor
  554. ^ aVisitor visitIRBlockSequence: self
  555. ! !
  556. IRInstruction subclass: #IRValue
  557. slots: {#value}
  558. package: 'Compiler-IR'!
  559. !IRValue commentStamp!
  560. I am the simplest possible instruction. I represent a value.!
  561. !IRValue methodsFor: 'accessing'!
  562. value
  563. ^ value
  564. !
  565. value: aString
  566. value := aString
  567. ! !
  568. !IRValue methodsFor: 'testing'!
  569. needsBoxingAsReceiver
  570. ^ false
  571. ! !
  572. !IRValue methodsFor: 'visiting'!
  573. acceptDagVisitor: aVisitor
  574. ^ aVisitor visitIRValue: self
  575. ! !
  576. IRInstruction subclass: #IRVariable
  577. slots: {#variable}
  578. package: 'Compiler-IR'!
  579. !IRVariable commentStamp!
  580. I am a variable instruction.!
  581. !IRVariable methodsFor: 'accessing'!
  582. variable
  583. ^ variable
  584. !
  585. variable: aScopeVariable
  586. variable := aScopeVariable
  587. ! !
  588. !IRVariable methodsFor: 'testing'!
  589. isSelf
  590. ^ self variable isSelf
  591. !
  592. isSuper
  593. ^ self variable isSuper
  594. !
  595. isVariable
  596. ^ true
  597. !
  598. needsBoxingAsReceiver
  599. ^ self variable isPseudoVar not
  600. ! !
  601. !IRVariable methodsFor: 'visiting'!
  602. acceptDagVisitor: aVisitor
  603. ^ aVisitor visitIRVariable: self
  604. ! !
  605. IRInstruction subclass: #IRVerbatim
  606. slots: {#source}
  607. package: 'Compiler-IR'!
  608. !IRVerbatim methodsFor: 'accessing'!
  609. source
  610. ^ source
  611. !
  612. source: aString
  613. source := aString
  614. ! !
  615. !IRVerbatim methodsFor: 'visiting'!
  616. acceptDagVisitor: aVisitor
  617. ^ aVisitor visitIRVerbatim: self
  618. ! !
  619. Object subclass: #IRPragmator
  620. slots: {#irMethod}
  621. package: 'Compiler-IR'!
  622. !IRPragmator methodsFor: 'accessing'!
  623. irMethod
  624. ^ irMethod
  625. !
  626. irMethod: anObject
  627. irMethod := anObject
  628. ! !
  629. !IRPragmator methodsFor: 'visiting'!
  630. value: anIRMethod
  631. self irMethod: anIRMethod.
  632. self processPragmas: anIRMethod pragmas.
  633. ^ anIRMethod
  634. ! !
  635. IRPragmator subclass: #IRLatePragmator
  636. slots: {}
  637. package: 'Compiler-IR'!
  638. !IRLatePragmator methodsFor: 'pragmas'!
  639. jsOverride: aString
  640. self irMethod attachments
  641. at: aString
  642. put: (NativeFunction
  643. constructorNamed: #Function
  644. value: 'return this.', irMethod selector asJavaScriptMethodName, '()')
  645. ! !
  646. ParentFakingPathDagVisitor subclass: #IRVisitor
  647. slots: {}
  648. package: 'Compiler-IR'!
  649. !IRVisitor methodsFor: 'visiting'!
  650. visitDagNode: aNode
  651. ^ self visitDagNodeVariantSimple: aNode
  652. !
  653. visitIRAssignment: anIRAssignment
  654. ^ self visitDagNode: anIRAssignment
  655. !
  656. visitIRBlockReturn: anIRBlockReturn
  657. ^ self visitIRReturn: anIRBlockReturn
  658. !
  659. visitIRBlockSequence: anIRBlockSequence
  660. ^ self visitIRSequence: anIRBlockSequence
  661. !
  662. visitIRClosure: anIRClosure
  663. ^ self visitDagNode: anIRClosure
  664. !
  665. visitIRDynamicArray: anIRDynamicArray
  666. ^ self visitDagNode: anIRDynamicArray
  667. !
  668. visitIRDynamicDictionary: anIRDynamicDictionary
  669. ^ self visitDagNode: anIRDynamicDictionary
  670. !
  671. visitIRInlinedClosure: anIRInlinedClosure
  672. ^ self visitIRClosure: anIRInlinedClosure
  673. !
  674. visitIRInlinedSequence: anIRInlinedSequence
  675. ^ self visitIRSequence: anIRInlinedSequence
  676. !
  677. visitIRMethod: anIRMethod
  678. ^ self visitDagNode: anIRMethod
  679. !
  680. visitIRNonLocalReturn: anIRNonLocalReturn
  681. ^ self visitDagNode: anIRNonLocalReturn
  682. !
  683. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  684. ^ self visitDagNode: anIRNonLocalReturnHandling
  685. !
  686. visitIRReturn: anIRReturn
  687. ^ self visitDagNode: anIRReturn
  688. !
  689. visitIRSend: anIRSend
  690. ^ self visitDagNode: anIRSend
  691. !
  692. visitIRSequence: anIRSequence
  693. ^ self visitDagNode: anIRSequence
  694. !
  695. visitIRTempDeclaration: anIRTempDeclaration
  696. ^ self visitDagNode: anIRTempDeclaration
  697. !
  698. visitIRValue: anIRValue
  699. ^ self visitDagNode: anIRValue
  700. !
  701. visitIRVariable: anIRVariable
  702. ^ self visitDagNode: anIRVariable
  703. !
  704. visitIRVerbatim: anIRVerbatim
  705. ^ self visitDagNode: anIRVerbatim
  706. ! !
  707. IRVisitor subclass: #IRJSTranslator
  708. slots: {#stream. #currentClass}
  709. package: 'Compiler-IR'!
  710. !IRJSTranslator methodsFor: 'accessing'!
  711. contents
  712. ^ self stream contents
  713. !
  714. currentClass
  715. ^ currentClass
  716. !
  717. currentClass: aClass
  718. currentClass := aClass
  719. !
  720. stream
  721. ^ stream
  722. !
  723. stream: aStream
  724. stream := aStream
  725. ! !
  726. !IRJSTranslator methodsFor: 'initialization'!
  727. initialize
  728. super initialize.
  729. stream := JSStream new.
  730. ! !
  731. !IRJSTranslator methodsFor: 'visiting'!
  732. visitIRAssignment: anIRAssignment
  733. self stream
  734. nextPutAssignLhs: [self visit: anIRAssignment left]
  735. rhs: [self visit: anIRAssignment right].
  736. !
  737. visitIRClosure: anIRClosure
  738. self stream
  739. nextPutClosureWith: [
  740. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  741. each name asVariableName ]).
  742. self stream
  743. nextPutBlockContextFor: anIRClosure
  744. during: [ super visitIRClosure: anIRClosure ] ]
  745. arguments: anIRClosure arguments
  746. !
  747. visitIRDynamicArray: anIRDynamicArray
  748. self
  749. visitInstructionList: anIRDynamicArray dagChildren
  750. enclosedBetween: '[' and: ']'
  751. !
  752. visitIRDynamicDictionary: anIRDynamicDictionary
  753. self
  754. visitInstructionList: anIRDynamicDictionary dagChildren
  755. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  756. !
  757. visitIRMethod: anIRMethod
  758. self stream
  759. nextPutFunctionWith: [
  760. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  761. each name asVariableName ]).
  762. self stream nextPutContextFor: anIRMethod during: [
  763. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  764. self stream nextPutVars:
  765. (internalVars collect: [ :each | each variable alias ]) asSet ].
  766. anIRMethod scope hasNonLocalReturn
  767. ifTrue: [
  768. self stream nextPutNonLocalReturnHandlingWith: [
  769. super visitIRMethod: anIRMethod ] ]
  770. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  771. arguments: anIRMethod arguments.
  772. ^ anIRMethod compiledSource: self contents; yourself
  773. !
  774. visitIRNonLocalReturn: anIRNonLocalReturn
  775. self stream nextPutNonLocalReturnWith: [
  776. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  777. !
  778. visitIRReturn: anIRReturn
  779. self stream nextPutReturnWith: [
  780. super visitIRReturn: anIRReturn ]
  781. !
  782. visitIRSend: anIRSend
  783. | sends superclass |
  784. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  785. anIRSend receiver isSuper
  786. ifTrue: [ self visitSuperSend: anIRSend ]
  787. ifFalse: [ self visitSend: anIRSend ].
  788. anIRSend index < sends
  789. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  790. !
  791. visitIRSequence: anIRSequence
  792. anIRSequence dagChildren do: [ :each |
  793. self stream nextPutStatementWith: [ self visit: each ] ]
  794. !
  795. visitIRTempDeclaration: anIRTempDeclaration
  796. "self stream
  797. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  798. lf"
  799. !
  800. visitIRValue: anIRValue
  801. self stream nextPutAll: anIRValue value asJavaScriptSource
  802. !
  803. visitIRVariable: anIRVariable
  804. self stream nextPutAll: anIRVariable variable alias
  805. !
  806. visitIRVerbatim: anIRVerbatim
  807. self stream nextPutAll: anIRVerbatim source
  808. !
  809. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  810. self stream nextPutAll: aString.
  811. anArray
  812. do: [ :each | self visit: each ]
  813. separatedBy: [ self stream nextPutAll: ',' ].
  814. stream nextPutAll: anotherString
  815. !
  816. visitReceiver: anIRInstruction
  817. | instr |
  818. anIRInstruction isSelf
  819. ifTrue: [ instr := anIRInstruction copy
  820. variable: (anIRInstruction variable copy name: '$self'; yourself);
  821. yourself ]
  822. ifFalse: [ instr := anIRInstruction ].
  823. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  824. self stream nextPutAll: '$recv('.
  825. self visit: instr.
  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}, 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. sends := (self method sendIndexes at: self selector) size.
  1064. ^ (super shouldBeAliased or: [
  1065. self isReferenced and: [
  1066. self index < sends or: [
  1067. self superSend ] ] ])
  1068. !
  1069. subtreeNeedsAliasing
  1070. ^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
  1071. ! !