Compiler-IR.st 27 KB

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