Compiler-IR.st 28 KB

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