Compiler-IR.st 27 KB

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