Compiler-IR.st 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 ifTrue: [ true ] ifFalse: [
  64. each shouldBeAliased ifTrue: [ true ] ifFalse: [
  65. (each hasOpeningStatements ifTrue: [ true ] ifFalse: [ each subtreeNeedsAliasing ]) ifTrue: [ shouldAlias := true ].
  66. false ] ] ]
  67. 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 visit: 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 visit: 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. | irSequence |
  142. self method: (IRMethod new
  143. source: self source crlfSanitized;
  144. pragmas: (aNode pragmas collect: [ :each |
  145. Message
  146. selector: each selector
  147. arguments: (each arguments collect: [ :eachArg |
  148. eachArg isString ifTrue: [ eachArg crlfSanitized ] ifFalse: [ eachArg ]])]);
  149. theClass: self theClass;
  150. arguments: aNode arguments;
  151. selector: aNode selector;
  152. sendIndexes: aNode sendIndexes;
  153. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  154. classReferences: aNode classReferences;
  155. scope: aNode scope;
  156. yourself).
  157. aNode scope temps do: [ :each |
  158. self method add: (IRTempDeclaration new
  159. name: each name;
  160. scope: aNode scope;
  161. yourself) ].
  162. self method add: (irSequence := self visit: aNode sequenceNode).
  163. aNode scope hasLocalReturn ifFalse: [ irSequence
  164. add: (IRReturn new
  165. add: (IRVariable new
  166. variable: (aNode scope pseudoVars at: 'self');
  167. yourself);
  168. yourself) ].
  169. ^ self method
  170. !
  171. visitReturnNode: aNode
  172. ^ (aNode nonLocalReturn
  173. ifTrue: [ IRNonLocalReturn new ]
  174. ifFalse: [ IRReturn new ])
  175. scope: aNode scope;
  176. add: (self visit: aNode expression);
  177. yourself
  178. !
  179. visitSendNode: aNode
  180. | send |
  181. send := IRSend new.
  182. send
  183. selector: aNode selector;
  184. javaScriptSelector: aNode javaScriptSelector;
  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. self irMethod arguments ifNotEmpty: [
  663. CompilerError signal: 'Must use <jsOverride:> in unary method.' ].
  664. self irMethod attachments
  665. at: aString
  666. put: (NativeFunction
  667. constructorNamed: #Function
  668. value: 'return this.', irMethod selector asJavaScriptMethodName, '()')
  669. !
  670. jsOverride: aString args: aCollection
  671. | argList |
  672. self irMethod arguments = aCollection ifFalse: [
  673. CompilerError signal: 'Argument mismatch in <jsOverride:args:>.' ].
  674. argList := ',' join: aCollection.
  675. self irMethod attachments
  676. at: aString
  677. put: (NativeFunction
  678. constructorNamed: #Function
  679. value: argList
  680. value: 'return this.', irMethod selector asJavaScriptMethodName, '(', argList, ')')
  681. ! !
  682. ParentFakingPathDagVisitor subclass: #IRVisitor
  683. slots: {}
  684. package: 'Compiler-IR'!
  685. !IRVisitor methodsFor: 'visiting'!
  686. visitDagNode: aNode
  687. ^ self visitDagNodeVariantSimple: aNode
  688. !
  689. visitIRAssignment: anIRAssignment
  690. ^ self visitDagNode: anIRAssignment
  691. !
  692. visitIRBlockReturn: anIRBlockReturn
  693. ^ self visitIRReturn: anIRBlockReturn
  694. !
  695. visitIRBlockSequence: anIRBlockSequence
  696. ^ self visitIRSequence: anIRBlockSequence
  697. !
  698. visitIRClosure: anIRClosure
  699. ^ self visitDagNode: anIRClosure
  700. !
  701. visitIRDynamicArray: anIRDynamicArray
  702. ^ self visitDagNode: anIRDynamicArray
  703. !
  704. visitIRDynamicDictionary: anIRDynamicDictionary
  705. ^ self visitDagNode: anIRDynamicDictionary
  706. !
  707. visitIRMethod: anIRMethod
  708. ^ self visitDagNode: anIRMethod
  709. !
  710. visitIRNonLocalReturn: anIRNonLocalReturn
  711. ^ self visitDagNode: anIRNonLocalReturn
  712. !
  713. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  714. ^ self visitDagNode: anIRNonLocalReturnHandling
  715. !
  716. visitIRReturn: anIRReturn
  717. ^ self visitDagNode: anIRReturn
  718. !
  719. visitIRSend: anIRSend
  720. ^ self visitDagNode: anIRSend
  721. !
  722. visitIRSequence: anIRSequence
  723. ^ self visitDagNode: anIRSequence
  724. !
  725. visitIRTempDeclaration: anIRTempDeclaration
  726. ^ self visitDagNode: anIRTempDeclaration
  727. !
  728. visitIRValue: anIRValue
  729. ^ self visitDagNode: anIRValue
  730. !
  731. visitIRVariable: anIRVariable
  732. ^ self visitDagNode: anIRVariable
  733. !
  734. visitIRVerbatim: anIRVerbatim
  735. ^ self visitDagNode: anIRVerbatim
  736. ! !
  737. IRVisitor subclass: #IRJSTranslator
  738. slots: {#stream. #currentClass}
  739. package: 'Compiler-IR'!
  740. !IRJSTranslator methodsFor: 'accessing'!
  741. contents
  742. ^ self stream contents
  743. !
  744. currentClass
  745. ^ currentClass
  746. !
  747. currentClass: aClass
  748. currentClass := aClass
  749. !
  750. stream
  751. ^ stream
  752. !
  753. stream: aStream
  754. stream := aStream
  755. ! !
  756. !IRJSTranslator methodsFor: 'initialization'!
  757. initialize
  758. super initialize.
  759. stream := JSStream new.
  760. ! !
  761. !IRJSTranslator methodsFor: 'visiting'!
  762. visitIRAssignment: anIRAssignment
  763. self stream
  764. nextPutAssignLhs: [self visit: anIRAssignment left]
  765. rhs: [self visit: anIRAssignment right].
  766. !
  767. visitIRClosure: anIRClosure
  768. self stream
  769. nextPutClosureWith: [
  770. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  771. each name asVariableName ]).
  772. self stream
  773. nextPutBlockContextFor: anIRClosure
  774. during: [ super visitIRClosure: anIRClosure ] ]
  775. arguments: anIRClosure arguments
  776. !
  777. visitIRDynamicArray: anIRDynamicArray
  778. self
  779. visitInstructionList: anIRDynamicArray dagChildren
  780. enclosedBetween: '[' and: ']'
  781. !
  782. visitIRDynamicDictionary: anIRDynamicDictionary
  783. self
  784. visitInstructionList: anIRDynamicDictionary dagChildren
  785. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  786. !
  787. visitIRMethod: anIRMethod
  788. self stream
  789. nextPutFunctionWith: [
  790. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  791. each name asVariableName ]).
  792. self stream nextPutContextFor: anIRMethod during: [
  793. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  794. self stream nextPutVars:
  795. (internalVars collect: [ :each | each variable alias ]) asSet ].
  796. anIRMethod scope hasNonLocalReturn
  797. ifTrue: [
  798. self stream nextPutNonLocalReturnHandlingWith: [
  799. super visitIRMethod: anIRMethod ] ]
  800. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  801. arguments: anIRMethod arguments.
  802. ^ anIRMethod compiledSource: self contents; yourself
  803. !
  804. visitIRNonLocalReturn: anIRNonLocalReturn
  805. self stream nextPutNonLocalReturnWith: [
  806. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  807. !
  808. visitIRReturn: anIRReturn
  809. self stream nextPutReturnWith: [
  810. super visitIRReturn: anIRReturn ]
  811. !
  812. visitIRSend: anIRSend
  813. | prefixes suffixes workBlock |
  814. prefixes := #().
  815. suffixes := #().
  816. workBlock := [ self visitSend: anIRSend ].
  817. anIRSend index < (anIRSend method sendIndexes at: anIRSend selector) size ifTrue: [
  818. suffixes add:
  819. anIRSend scope alias,
  820. '.sendIdx[',
  821. anIRSend selector asJavaScriptSource,
  822. ']=',
  823. anIRSend index asString ].
  824. anIRSend receiver isSuper ifTrue: [
  825. prefixes add: anIRSend scope alias, '.supercall = true'.
  826. suffixes add: anIRSend scope alias, '.supercall = false'.
  827. workBlock := [ self visitSuperSend: anIRSend ] ].
  828. self stream nextPutBefore: prefixes after: suffixes with: workBlock
  829. !
  830. visitIRSequence: anIRSequence
  831. anIRSequence dagChildren do: [ :each |
  832. self stream nextPutStatementWith: [ self visit: each ] ]
  833. !
  834. visitIRTempDeclaration: anIRTempDeclaration
  835. "self stream
  836. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  837. lf"
  838. !
  839. visitIRValue: anIRValue
  840. self stream nextPutAll: anIRValue value asJavaScriptSource
  841. !
  842. visitIRVariable: anIRVariable
  843. self stream nextPutAll: anIRVariable variable alias
  844. !
  845. visitIRVerbatim: anIRVerbatim
  846. self stream nextPutAll: anIRVerbatim source
  847. !
  848. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  849. self stream nextPutAll: aString.
  850. anArray
  851. do: [ :each | self visit: each ]
  852. separatedBy: [ self stream nextPutAll: ',' ].
  853. stream nextPutAll: anotherString
  854. !
  855. visitReceiver: anIRInstruction
  856. anIRInstruction asReceiver
  857. ifNotNil: [ :instr | self visit: instr ]
  858. ifNil: [
  859. self stream nextPutAll: '$recv('.
  860. self visit: anIRInstruction.
  861. self stream nextPutAll: ')' ]
  862. !
  863. visitSend: anIRSend
  864. self visitReceiver: anIRSend receiver.
  865. self stream nextPutAll: '.', anIRSend javaScriptSelector.
  866. self
  867. visitInstructionList: anIRSend arguments
  868. enclosedBetween: '(' and: ')'
  869. !
  870. visitSuperSend: anIRSend
  871. self stream
  872. nextPutAll: anIRSend receiver variable lookupAsJavaScriptSource, '.';
  873. nextPutAll: anIRSend javaScriptSelector, '.call'.
  874. self
  875. visitInstructionList: {anIRSend receiver asReceiver}, anIRSend arguments
  876. enclosedBetween: '(' and: ')'
  877. ! !
  878. Object subclass: #JSStream
  879. slots: {#stream. #omitSemicolon}
  880. package: 'Compiler-IR'!
  881. !JSStream methodsFor: 'accessing'!
  882. contents
  883. ^ stream contents
  884. !
  885. omitSemicolon
  886. ^ omitSemicolon
  887. !
  888. omitSemicolon: aBoolean
  889. omitSemicolon := aBoolean
  890. ! !
  891. !JSStream methodsFor: 'initialization'!
  892. initialize
  893. super initialize.
  894. stream := '' writeStream.
  895. ! !
  896. !JSStream methodsFor: 'streaming'!
  897. lf
  898. stream lf
  899. !
  900. nextPut: aString
  901. stream nextPut: aString
  902. !
  903. nextPutAll: aString
  904. stream nextPutAll: aString
  905. !
  906. nextPutAssignLhs: aBlock rhs: anotherBlock
  907. aBlock value.
  908. stream nextPutAll: '='.
  909. anotherBlock value
  910. !
  911. nextPutBefore: prefixCollection after: suffixCollection with: aBlock
  912. suffixCollection isEmpty
  913. ifTrue: [ self nextPutBefore: prefixCollection with: aBlock ]
  914. ifFalse: [
  915. self
  916. nextPutAll: '['; nextPutBefore: prefixCollection with: aBlock; lf;
  917. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf.
  918. suffixCollection do: [ :each | self nextPutAll: ','; nextPutAll: each ].
  919. self
  920. lf;
  921. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  922. nextPutAll: '][0]' ]
  923. !
  924. nextPutBefore: aCollection with: aBlock
  925. aCollection isEmpty ifTrue: [ aBlock value ] ifFalse: [
  926. self nextPutAll: '('; lf; nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf.
  927. aCollection do: [ :each | self nextPutAll: each; nextPutAll: ',' ].
  928. self lf; nextPutAll: '//>>excludeEnd("ctx");'; lf.
  929. aBlock value.
  930. self nextPutAll: ')' ]
  931. !
  932. nextPutBlockContextFor: anIRClosure during: aBlock
  933. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  934. self
  935. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  936. lf;
  937. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  938. lf;
  939. nextPutAll: '//>>excludeEnd("ctx");';
  940. lf.
  941. aBlock value.
  942. self
  943. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  944. lf;
  945. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  946. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  947. anIRClosure locals
  948. do: [ :each |
  949. self
  950. nextPutAll: each asVariableName;
  951. nextPutAll: ':';
  952. nextPutAll: each asVariableName ]
  953. separatedBy: [ self nextPutAll: ',' ].
  954. self
  955. nextPutAll: '},';
  956. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  957. lf;
  958. nextPutAll: '//>>excludeEnd("ctx");'
  959. !
  960. nextPutClosureWith: aBlock arguments: anArray
  961. stream nextPutAll: '(function('.
  962. anArray
  963. do: [ :each | stream nextPutAll: each asVariableName ]
  964. separatedBy: [ stream nextPut: ',' ].
  965. stream nextPutAll: '){'; lf.
  966. aBlock value.
  967. stream lf; nextPutAll: '})'
  968. !
  969. nextPutContextFor: aMethod during: aBlock
  970. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  971. self
  972. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  973. lf;
  974. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  975. lf;
  976. nextPutAll: '//>>excludeEnd("ctx");';
  977. lf.
  978. aBlock value.
  979. self
  980. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  981. lf;
  982. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  983. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  984. aMethod locals
  985. do: [ :each |
  986. self
  987. nextPutAll: each asVariableName;
  988. nextPutAll: ':';
  989. nextPutAll: each asVariableName ]
  990. separatedBy: [ self nextPutAll: ',' ].
  991. self
  992. nextPutAll: '})});';
  993. lf;
  994. nextPutAll: '//>>excludeEnd("ctx");'
  995. !
  996. nextPutFunctionWith: aBlock arguments: anArray
  997. stream nextPutAll: 'function ('.
  998. anArray
  999. do: [ :each | stream nextPutAll: each asVariableName ]
  1000. separatedBy: [ stream nextPut: ',' ].
  1001. stream nextPutAll: '){'; lf.
  1002. stream nextPutAll: 'var self=this,$self=this;'; lf.
  1003. aBlock value.
  1004. stream lf; nextPutAll: '}'
  1005. !
  1006. nextPutIf: aBlock then: anotherBlock
  1007. stream nextPutAll: 'if('.
  1008. aBlock value.
  1009. stream nextPutAll: '){'; lf.
  1010. anotherBlock value.
  1011. stream nextPutAll: '}'.
  1012. self omitSemicolon: true
  1013. !
  1014. nextPutIf: aBlock then: ifBlock else: elseBlock
  1015. stream nextPutAll: 'if('.
  1016. aBlock value.
  1017. stream nextPutAll: '){'; lf.
  1018. ifBlock value.
  1019. stream nextPutAll: '} else {'; lf.
  1020. elseBlock value.
  1021. stream nextPutAll: '}'.
  1022. self omitSemicolon: true
  1023. !
  1024. nextPutNonLocalReturnHandlingWith: aBlock
  1025. stream
  1026. nextPutAll: 'var $early={};'; lf;
  1027. nextPutAll: 'try {'; lf.
  1028. aBlock value.
  1029. stream
  1030. nextPutAll: '}'; lf;
  1031. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  1032. !
  1033. nextPutNonLocalReturnWith: aBlock
  1034. stream nextPutAll: 'throw $early=['.
  1035. aBlock value.
  1036. stream nextPutAll: ']'
  1037. !
  1038. nextPutReturnWith: aBlock
  1039. stream nextPutAll: 'return '.
  1040. aBlock value
  1041. !
  1042. nextPutStatementWith: aBlock
  1043. self omitSemicolon: false.
  1044. aBlock value.
  1045. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  1046. self omitSemicolon: false.
  1047. stream lf
  1048. !
  1049. nextPutVars: aCollection
  1050. aCollection ifNotEmpty: [
  1051. stream nextPutAll: 'var '.
  1052. aCollection
  1053. do: [ :each | stream nextPutAll: each ]
  1054. separatedBy: [ stream nextPutAll: ',' ].
  1055. stream nextPutAll: ';'; lf ]
  1056. ! !
  1057. IRPragmator setTraitComposition: {TPragmator} asTraitComposition!
  1058. ! !
  1059. !ASTNode methodsFor: '*Compiler-IR'!
  1060. requiresSmalltalkContext
  1061. "Answer true if the receiver requires a smalltalk context.
  1062. Only send nodes require a context.
  1063. If no node requires a context, the method will be compiled without one.
  1064. See `IRJSTranslator` and `JSStream` for context creation"
  1065. ^ self dagChildren anySatisfy: [ :each | each requiresSmalltalkContext ]
  1066. ! !
  1067. !AssignmentNode methodsFor: '*Compiler-IR'!
  1068. hasOpeningStatements
  1069. ^ true
  1070. ! !
  1071. !BlockNode methodsFor: '*Compiler-IR'!
  1072. subtreeNeedsAliasing
  1073. ^ false
  1074. ! !
  1075. !CascadeNode methodsFor: '*Compiler-IR'!
  1076. hasOpeningStatements
  1077. ^ true
  1078. ! !
  1079. !ExpressionNode methodsFor: '*Compiler-IR'!
  1080. hasOpeningStatements
  1081. ^ false
  1082. !
  1083. subtreeNeedsAliasing
  1084. ^ self dagChildren anySatisfy: [ :each |
  1085. each shouldBeAliased ifTrue: [ true ] ifFalse: [
  1086. each hasOpeningStatements ifTrue: [ true ] ifFalse: [
  1087. each subtreeNeedsAliasing ] ] ]
  1088. ! !
  1089. !JSStatementNode methodsFor: '*Compiler-IR'!
  1090. requiresSmalltalkContext
  1091. ^ true
  1092. ! !
  1093. !PseudoVar methodsFor: '*Compiler-IR'!
  1094. asReceiver
  1095. ^ self class receiverNames
  1096. at: self name
  1097. ifPresent: [ :newName | self copy name: newName; yourself ]
  1098. ifAbsent: [ self ]
  1099. ! !
  1100. !ScopeVar methodsFor: '*Compiler-IR'!
  1101. asReceiver
  1102. "Return customized copy to use as receiver,
  1103. or self if suffices."
  1104. ^ nil
  1105. ! !
  1106. !SendNode methodsFor: '*Compiler-IR'!
  1107. requiresSmalltalkContext
  1108. ^ true
  1109. ! !