Compiler-IR.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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: aNode
  53. | variable |
  54. aNode isImmutable ifTrue: [ ^ self visit: aNode ].
  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: aNode);
  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: aNode
  177. ^ aNode shouldBeAliased
  178. ifTrue: [ self alias: aNode ]
  179. ifFalse: [ self visit: aNode ]
  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. subtreeNeedsAliasing
  1040. ^ self shouldBeAliased or: [
  1041. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1042. ! !
  1043. !AssignmentNode methodsFor: '*Compiler-IR'!
  1044. subtreeNeedsAliasing
  1045. ^ true
  1046. ! !
  1047. !BlockNode methodsFor: '*Compiler-IR'!
  1048. subtreeNeedsAliasing
  1049. ^ self shouldBeAliased
  1050. ! !
  1051. !CascadeNode methodsFor: '*Compiler-IR'!
  1052. subtreeNeedsAliasing
  1053. ^ true
  1054. ! !
  1055. !JSStatementNode methodsFor: '*Compiler-IR'!
  1056. requiresSmalltalkContext
  1057. ^ true
  1058. ! !
  1059. !PseudoVar methodsFor: '*Compiler-IR'!
  1060. asReceiver
  1061. ^ self class receiverNames
  1062. at: self name
  1063. ifPresent: [ :newName | self copy name: newName; yourself ]
  1064. ifAbsent: [ self ]
  1065. ! !
  1066. !ScopeVar methodsFor: '*Compiler-IR'!
  1067. asReceiver
  1068. "Return customized copy to use as receiver,
  1069. or self if suffices."
  1070. ^ nil
  1071. ! !
  1072. !SendNode methodsFor: '*Compiler-IR'!
  1073. requiresSmalltalkContext
  1074. ^ true
  1075. ! !