Compiler-IR.st 27 KB

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