Compiler-IR.st 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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 result |
  71. threshold := 0.
  72. aCollection withIndexDo: [ :each :i |
  73. each subtreeNeedsAliasing
  74. ifTrue: [ threshold := i ] ].
  75. result := OrderedCollection new.
  76. aCollection withIndexDo: [ :each :i |
  77. result add: (i <= threshold
  78. ifTrue: [ self alias: each ]
  79. ifFalse: [ self visit: each ]) ].
  80. ^ result
  81. !
  82. visitAssignmentNode: aNode
  83. | left right assignment |
  84. right := self visit: aNode right.
  85. left := self visit: aNode left.
  86. self addToSequence: (IRAssignment new
  87. add: left;
  88. add: right;
  89. yourself).
  90. ^ left
  91. !
  92. visitBlockNode: aNode
  93. | closure |
  94. closure := IRClosure new
  95. arguments: aNode parameters;
  96. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  97. scope: aNode scope;
  98. yourself.
  99. aNode scope temps do: [ :each |
  100. closure add: (IRTempDeclaration new
  101. name: each name;
  102. scope: aNode scope;
  103. yourself) ].
  104. aNode dagChildren do: [ :each | closure add: (self visit: each) ].
  105. ^ closure
  106. !
  107. visitBlockSequenceNode: aNode
  108. ^ self
  109. withSequence: IRBlockSequence new
  110. do: [
  111. aNode dagChildren ifNotEmpty: [
  112. aNode dagChildren allButLast do: [ :each |
  113. self addToSequence: (self visitOrAlias: each) ].
  114. aNode dagChildren last isReturnNode
  115. ifFalse: [ self addToSequence: (IRBlockReturn new add: (self visitOrAlias: aNode dagChildren last); yourself) ]
  116. ifTrue: [ self addToSequence: (self visitOrAlias: aNode dagChildren last) ] ]]
  117. !
  118. visitCascadeNode: aNode
  119. | receiver |
  120. receiver := aNode receiver.
  121. receiver isImmutable ifFalse: [
  122. | alias |
  123. alias := self alias: receiver.
  124. receiver := VariableNode new binding: alias variable ].
  125. aNode dagChildren do: [ :each | each receiver: receiver ].
  126. aNode dagChildren allButLast do: [ :each |
  127. self addToSequence: (self visit: each) ].
  128. ^ self visitOrAlias: aNode dagChildren last
  129. !
  130. visitDynamicArrayNode: aNode
  131. | array |
  132. array := IRDynamicArray new.
  133. (self aliasTemporally: aNode dagChildren) do: [ :each | array add: each ].
  134. ^ array
  135. !
  136. visitDynamicDictionaryNode: aNode
  137. | dictionary |
  138. dictionary := IRDynamicDictionary new.
  139. (self aliasTemporally: aNode dagChildren) do: [ :each | dictionary add: each ].
  140. ^ dictionary
  141. !
  142. visitJSStatementNode: aNode
  143. ^ IRVerbatim new
  144. source: aNode source crlfSanitized;
  145. yourself
  146. !
  147. visitMethodNode: aNode
  148. self method: (IRMethod new
  149. source: self source crlfSanitized;
  150. pragmas: (aNode pragmas collect: [ :each |
  151. Message
  152. selector: each selector
  153. arguments: (each arguments collect: [ :eachArg |
  154. eachArg isString ifTrue: [ eachArg crlfSanitized ] ifFalse: [ eachArg ]])]);
  155. theClass: self theClass;
  156. arguments: aNode arguments;
  157. selector: aNode selector;
  158. sendIndexes: aNode sendIndexes;
  159. requiresSmalltalkContext: aNode requiresSmalltalkContext;
  160. classReferences: aNode classReferences;
  161. scope: aNode scope;
  162. yourself).
  163. aNode scope temps do: [ :each |
  164. self method add: (IRTempDeclaration new
  165. name: each name;
  166. scope: aNode scope;
  167. yourself) ].
  168. self method dagChildren addAll: (self visitAllChildren: aNode).
  169. aNode scope hasLocalReturn ifFalse: [self method
  170. add: (IRReturn new
  171. add: (IRVariable new
  172. variable: (aNode scope pseudoVars at: 'self');
  173. yourself);
  174. yourself);
  175. add: (IRVerbatim new source: ';', String lf; yourself) ].
  176. ^ self method
  177. !
  178. visitOrAlias: aNode
  179. ^ aNode shouldBeAliased
  180. ifTrue: [ self alias: aNode ]
  181. ifFalse: [ self visit: aNode ]
  182. !
  183. visitReturnNode: aNode
  184. | return |
  185. return := aNode nonLocalReturn
  186. ifTrue: [ IRNonLocalReturn new ]
  187. ifFalse: [ IRReturn new ].
  188. return scope: aNode scope.
  189. aNode dagChildren do: [ :each |
  190. return add: (self visitOrAlias: each) ].
  191. ^ return
  192. !
  193. visitSendNode: aNode
  194. | send |
  195. send := IRSend new.
  196. send
  197. selector: aNode selector;
  198. index: aNode index.
  199. (self aliasTemporally: aNode dagChildren) do: [ :each | send add: each ].
  200. ^ send
  201. !
  202. visitSequenceNode: aNode
  203. ^ self
  204. withSequence: IRSequence new
  205. do: [ aNode dagChildren do: [ :each |
  206. self addToSequence: (self visitOrAlias: each) ] ]
  207. !
  208. visitValueNode: aNode
  209. ^ IRValue new
  210. value: aNode value;
  211. yourself
  212. !
  213. visitVariableNode: aNode
  214. ^ IRVariable new
  215. variable: aNode binding;
  216. yourself
  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: 'testing'!
  255. isClosure
  256. ^ false
  257. !
  258. isInlined
  259. ^ false
  260. !
  261. isMethod
  262. ^ false
  263. !
  264. isSelf
  265. ^ false
  266. !
  267. isSend
  268. ^ false
  269. !
  270. isSequence
  271. ^ false
  272. !
  273. isSuper
  274. ^ false
  275. !
  276. isTempDeclaration
  277. ^ false
  278. !
  279. isVariable
  280. ^ false
  281. !
  282. needsBoxingAsReceiver
  283. ^ true
  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 copy
  344. addAll: (self tempDeclarations collect: [ :each | each name ]);
  345. yourself
  346. !
  347. requiresSmalltalkContext
  348. ^ requiresSmalltalkContext ifNil: [ false ]
  349. !
  350. requiresSmalltalkContext: anObject
  351. requiresSmalltalkContext := anObject
  352. !
  353. scope: aScope
  354. super scope: aScope.
  355. aScope instruction: self
  356. !
  357. tempDeclarations
  358. ^ self dagChildren select: [ :each |
  359. each isTempDeclaration ]
  360. ! !
  361. IRClosureInstruction subclass: #IRClosure
  362. slots: {}
  363. package: 'Compiler-IR'!
  364. !IRClosure methodsFor: 'accessing'!
  365. sequence
  366. ^ self dagChildren last
  367. ! !
  368. !IRClosure methodsFor: 'testing'!
  369. isClosure
  370. ^ true
  371. ! !
  372. !IRClosure methodsFor: 'visiting'!
  373. acceptDagVisitor: aVisitor
  374. ^ aVisitor visitIRClosure: self
  375. ! !
  376. IRClosureInstruction subclass: #IRMethod
  377. slots: {#theClass. #source. #compiledSource. #attachments. #selector. #pragmas. #classReferences. #sendIndexes. #internalVariables}
  378. package: 'Compiler-IR'!
  379. !IRMethod commentStamp!
  380. I am a method instruction!
  381. !IRMethod methodsFor: 'accessing'!
  382. attachments
  383. ^ attachments ifNil: [ attachments := #{} ]
  384. !
  385. classReferences
  386. ^ classReferences
  387. !
  388. classReferences: aCollection
  389. classReferences := aCollection
  390. !
  391. compiledSource
  392. ^ compiledSource
  393. !
  394. compiledSource: anObject
  395. compiledSource := anObject
  396. !
  397. internalVariables
  398. ^ internalVariables ifNil: [ internalVariables := Set new ]
  399. !
  400. messageSends
  401. ^ self sendIndexes keys
  402. !
  403. method
  404. ^ self
  405. !
  406. pragmas
  407. ^ pragmas
  408. !
  409. pragmas: aCollection
  410. pragmas := aCollection
  411. !
  412. selector
  413. ^ selector
  414. !
  415. selector: aString
  416. selector := aString
  417. !
  418. sendIndexes
  419. ^ sendIndexes
  420. !
  421. sendIndexes: aDictionary
  422. sendIndexes := aDictionary
  423. !
  424. source
  425. ^ source
  426. !
  427. source: aString
  428. source := aString
  429. !
  430. theClass
  431. ^ theClass
  432. !
  433. theClass: aClass
  434. theClass := aClass
  435. ! !
  436. !IRMethod methodsFor: 'testing'!
  437. isMethod
  438. ^ true
  439. ! !
  440. !IRMethod methodsFor: 'visiting'!
  441. acceptDagVisitor: aVisitor
  442. ^ aVisitor visitIRMethod: self
  443. ! !
  444. IRScopedInstruction subclass: #IRReturn
  445. slots: {}
  446. package: 'Compiler-IR'!
  447. !IRReturn commentStamp!
  448. I am a local return instruction.!
  449. !IRReturn methodsFor: 'accessing'!
  450. expression
  451. ^ self dagChildren single
  452. !
  453. scope
  454. ^ scope ifNil: [ self parent scope ]
  455. ! !
  456. !IRReturn methodsFor: 'testing'!
  457. yieldsValue
  458. ^ false
  459. ! !
  460. !IRReturn methodsFor: 'visiting'!
  461. acceptDagVisitor: aVisitor
  462. ^ aVisitor visitIRReturn: self
  463. ! !
  464. IRReturn subclass: #IRBlockReturn
  465. slots: {}
  466. package: 'Compiler-IR'!
  467. !IRBlockReturn commentStamp!
  468. Smalltalk blocks return their last statement. I am a implicit block return instruction.!
  469. !IRBlockReturn methodsFor: 'visiting'!
  470. acceptDagVisitor: aVisitor
  471. ^ aVisitor visitIRBlockReturn: self
  472. ! !
  473. IRReturn subclass: #IRNonLocalReturn
  474. slots: {}
  475. package: 'Compiler-IR'!
  476. !IRNonLocalReturn commentStamp!
  477. I am a non local return instruction.
  478. Non local returns are handled using a try/catch JavaScript statement.
  479. See `IRNonLocalReturnHandling` class.!
  480. !IRNonLocalReturn methodsFor: 'visiting'!
  481. acceptDagVisitor: aVisitor
  482. ^ aVisitor visitIRNonLocalReturn: self
  483. ! !
  484. IRScopedInstruction subclass: #IRTempDeclaration
  485. slots: {#name}
  486. package: 'Compiler-IR'!
  487. !IRTempDeclaration methodsFor: 'accessing'!
  488. name
  489. ^ name
  490. !
  491. name: aString
  492. name := aString
  493. ! !
  494. !IRTempDeclaration methodsFor: 'testing'!
  495. isTempDeclaration
  496. ^ true
  497. ! !
  498. !IRTempDeclaration methodsFor: 'visiting'!
  499. acceptDagVisitor: aVisitor
  500. ^ aVisitor visitIRTempDeclaration: self
  501. ! !
  502. IRInstruction subclass: #IRSend
  503. slots: {#selector. #javaScriptSelector. #index}
  504. package: 'Compiler-IR'!
  505. !IRSend commentStamp!
  506. I am a message send instruction.!
  507. !IRSend methodsFor: 'accessing'!
  508. arguments
  509. ^ self dagChildren allButFirst
  510. !
  511. index
  512. ^ index
  513. !
  514. index: anInteger
  515. index := anInteger
  516. !
  517. javaScriptSelector
  518. ^ javaScriptSelector ifNil: [ javaScriptSelector := self selector asJavaScriptMethodName ]
  519. !
  520. javaScriptSelector: aString
  521. javaScriptSelector := aString
  522. !
  523. receiver
  524. ^ self dagChildren first
  525. !
  526. selector
  527. ^ selector
  528. !
  529. selector: aString
  530. selector := aString
  531. ! !
  532. !IRSend methodsFor: 'testing'!
  533. isSend
  534. ^ true
  535. ! !
  536. !IRSend methodsFor: 'visiting'!
  537. acceptDagVisitor: aVisitor
  538. ^ aVisitor visitIRSend: self
  539. ! !
  540. IRInstruction subclass: #IRSequence
  541. slots: {}
  542. package: 'Compiler-IR'!
  543. !IRSequence methodsFor: 'testing'!
  544. isSequence
  545. ^ true
  546. ! !
  547. !IRSequence methodsFor: 'visiting'!
  548. acceptDagVisitor: aVisitor
  549. ^ aVisitor visitIRSequence: self
  550. ! !
  551. IRSequence subclass: #IRBlockSequence
  552. slots: {}
  553. package: 'Compiler-IR'!
  554. !IRBlockSequence methodsFor: 'visiting'!
  555. acceptDagVisitor: aVisitor
  556. ^ aVisitor visitIRBlockSequence: self
  557. ! !
  558. IRInstruction subclass: #IRValue
  559. slots: {#value}
  560. package: 'Compiler-IR'!
  561. !IRValue commentStamp!
  562. I am the simplest possible instruction. I represent a value.!
  563. !IRValue methodsFor: 'accessing'!
  564. value
  565. ^ value
  566. !
  567. value: aString
  568. value := aString
  569. ! !
  570. !IRValue methodsFor: 'testing'!
  571. needsBoxingAsReceiver
  572. ^ false
  573. ! !
  574. !IRValue methodsFor: 'visiting'!
  575. acceptDagVisitor: aVisitor
  576. ^ aVisitor visitIRValue: self
  577. ! !
  578. IRInstruction subclass: #IRVariable
  579. slots: {#variable}
  580. package: 'Compiler-IR'!
  581. !IRVariable commentStamp!
  582. I am a variable instruction.!
  583. !IRVariable methodsFor: 'accessing'!
  584. variable
  585. ^ variable
  586. !
  587. variable: aScopeVariable
  588. variable := aScopeVariable
  589. ! !
  590. !IRVariable methodsFor: 'testing'!
  591. isSelf
  592. ^ self variable isSelf
  593. !
  594. isSuper
  595. ^ self variable isSuper
  596. !
  597. isVariable
  598. ^ true
  599. !
  600. needsBoxingAsReceiver
  601. ^ self variable isPseudoVar not
  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. | sends superclass |
  786. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  787. anIRSend receiver isSuper
  788. ifTrue: [ self visitSuperSend: anIRSend ]
  789. ifFalse: [ self visitSend: anIRSend ].
  790. anIRSend index < sends
  791. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  792. !
  793. visitIRSequence: anIRSequence
  794. anIRSequence dagChildren do: [ :each |
  795. self stream nextPutStatementWith: [ self visit: each ] ]
  796. !
  797. visitIRTempDeclaration: anIRTempDeclaration
  798. "self stream
  799. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  800. lf"
  801. !
  802. visitIRValue: anIRValue
  803. self stream nextPutAll: anIRValue value asJavaScriptSource
  804. !
  805. visitIRVariable: anIRVariable
  806. self stream nextPutAll: anIRVariable variable alias
  807. !
  808. visitIRVerbatim: anIRVerbatim
  809. self stream nextPutAll: anIRVerbatim source
  810. !
  811. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  812. self stream nextPutAll: aString.
  813. anArray
  814. do: [ :each | self visit: each ]
  815. separatedBy: [ self stream nextPutAll: ',' ].
  816. stream nextPutAll: anotherString
  817. !
  818. visitReceiver: anIRInstruction
  819. | instr |
  820. anIRInstruction isSelf
  821. ifTrue: [ instr := anIRInstruction copy
  822. variable: (anIRInstruction variable copy name: '$self'; yourself);
  823. yourself ]
  824. ifFalse: [ instr := anIRInstruction ].
  825. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  826. self stream nextPutAll: '$recv('.
  827. self visit: instr.
  828. self stream nextPutAll: ')'
  829. !
  830. visitSend: anIRSend
  831. self visitReceiver: anIRSend receiver.
  832. self stream nextPutAll: '.', anIRSend javaScriptSelector.
  833. self
  834. visitInstructionList: anIRSend arguments
  835. enclosedBetween: '(' and: ')'
  836. !
  837. visitSuperSend: anIRSend
  838. self stream nextPutSupercallFor: anIRSend with: [
  839. self stream
  840. nextPutAll: anIRSend receiver variable lookupAsJavaScriptSource, '.';
  841. nextPutAll: anIRSend javaScriptSelector, '.call'.
  842. self
  843. visitInstructionList: {anIRSend receiver}, anIRSend arguments
  844. enclosedBetween: '(' and: ')' ]
  845. ! !
  846. Object subclass: #JSStream
  847. slots: {#stream. #omitSemicolon}
  848. package: 'Compiler-IR'!
  849. !JSStream methodsFor: 'accessing'!
  850. contents
  851. ^ stream contents
  852. !
  853. omitSemicolon
  854. ^ omitSemicolon
  855. !
  856. omitSemicolon: aBoolean
  857. omitSemicolon := aBoolean
  858. ! !
  859. !JSStream methodsFor: 'initialization'!
  860. initialize
  861. super initialize.
  862. stream := '' writeStream.
  863. ! !
  864. !JSStream methodsFor: 'streaming'!
  865. lf
  866. stream lf
  867. !
  868. nextPut: aString
  869. stream nextPut: aString
  870. !
  871. nextPutAll: aString
  872. stream nextPutAll: aString
  873. !
  874. nextPutAssignLhs: aBlock rhs: anotherBlock
  875. aBlock value.
  876. stream nextPutAll: '='.
  877. anotherBlock value
  878. !
  879. nextPutBlockContextFor: anIRClosure during: aBlock
  880. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  881. self
  882. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  883. lf;
  884. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  885. lf;
  886. nextPutAll: '//>>excludeEnd("ctx");';
  887. lf.
  888. aBlock value.
  889. self
  890. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  891. lf;
  892. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  893. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  894. anIRClosure locals
  895. do: [ :each |
  896. self
  897. nextPutAll: each asVariableName;
  898. nextPutAll: ':';
  899. nextPutAll: each asVariableName ]
  900. separatedBy: [ self nextPutAll: ',' ].
  901. self
  902. nextPutAll: '},';
  903. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  904. lf;
  905. nextPutAll: '//>>excludeEnd("ctx");'
  906. !
  907. nextPutClosureWith: aBlock arguments: anArray
  908. stream nextPutAll: '(function('.
  909. anArray
  910. do: [ :each | stream nextPutAll: each asVariableName ]
  911. separatedBy: [ stream nextPut: ',' ].
  912. stream nextPutAll: '){'; lf.
  913. aBlock value.
  914. stream lf; nextPutAll: '})'
  915. !
  916. nextPutContextFor: aMethod during: aBlock
  917. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  918. self
  919. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  920. lf;
  921. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  922. lf;
  923. nextPutAll: '//>>excludeEnd("ctx");';
  924. lf.
  925. aBlock value.
  926. self
  927. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  928. lf;
  929. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  930. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  931. aMethod locals
  932. do: [ :each |
  933. self
  934. nextPutAll: each asVariableName;
  935. nextPutAll: ':';
  936. nextPutAll: each asVariableName ]
  937. separatedBy: [ self nextPutAll: ',' ].
  938. self
  939. nextPutAll: '})});';
  940. lf;
  941. nextPutAll: '//>>excludeEnd("ctx");'
  942. !
  943. nextPutFunctionWith: aBlock arguments: anArray
  944. stream nextPutAll: 'function ('.
  945. anArray
  946. do: [ :each | stream nextPutAll: each asVariableName ]
  947. separatedBy: [ stream nextPut: ',' ].
  948. stream nextPutAll: '){'; lf.
  949. stream nextPutAll: 'var self=this,$self=this;'; lf.
  950. aBlock value.
  951. stream lf; nextPutAll: '}'
  952. !
  953. nextPutIf: aBlock then: anotherBlock
  954. stream nextPutAll: 'if('.
  955. aBlock value.
  956. stream nextPutAll: '){'; lf.
  957. anotherBlock value.
  958. stream nextPutAll: '}'.
  959. self omitSemicolon: true
  960. !
  961. nextPutIf: aBlock then: ifBlock else: elseBlock
  962. stream nextPutAll: 'if('.
  963. aBlock value.
  964. stream nextPutAll: '){'; lf.
  965. ifBlock value.
  966. stream nextPutAll: '} else {'; lf.
  967. elseBlock value.
  968. stream nextPutAll: '}'.
  969. self omitSemicolon: true
  970. !
  971. nextPutNonLocalReturnHandlingWith: aBlock
  972. stream
  973. nextPutAll: 'var $early={};'; lf;
  974. nextPutAll: 'try {'; lf.
  975. aBlock value.
  976. stream
  977. nextPutAll: '}'; lf;
  978. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  979. !
  980. nextPutNonLocalReturnWith: aBlock
  981. stream nextPutAll: 'throw $early=['.
  982. aBlock value.
  983. stream nextPutAll: ']'
  984. !
  985. nextPutReturnWith: aBlock
  986. stream nextPutAll: 'return '.
  987. aBlock value
  988. !
  989. nextPutSendIndexFor: anIRSend
  990. self
  991. nextPutAll: ';'; lf;
  992. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  993. nextPutAll: anIRSend scope alias;
  994. nextPutAll: '.sendIdx[';
  995. nextPutAll: anIRSend selector asJavaScriptSource;
  996. nextPutAll: ']=';
  997. nextPutAll: anIRSend index asString;
  998. nextPutAll: ';'; lf;
  999. nextPutAll: '//>>excludeEnd("ctx")'
  1000. !
  1001. nextPutStatementWith: aBlock
  1002. self omitSemicolon: false.
  1003. aBlock value.
  1004. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  1005. self omitSemicolon: false.
  1006. stream lf
  1007. !
  1008. nextPutSupercallFor: anIRSend with: aBlock
  1009. self
  1010. nextPutAll: '('; lf;
  1011. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  1012. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  1013. nextPutAll: '//>>excludeEnd("ctx");'; lf.
  1014. aBlock value.
  1015. self
  1016. nextPutAll: ');'; lf;
  1017. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  1018. nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
  1019. nextPutAll: '//>>excludeEnd("ctx");'
  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. isReferenced
  1033. "Answer true if the receiver is referenced by other nodes.
  1034. Do not take sequences or assignments into account"
  1035. self parent isSequenceNode ifTrue: [ ^ false ].
  1036. self parent isAssignmentNode ifTrue: [ ^ false ].
  1037. self parent isCascadeNode ifTrue: [ ^ self parent isReferenced ].
  1038. ^ true
  1039. !
  1040. subtreeNeedsAliasing
  1041. ^ self shouldBeAliased or: [
  1042. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1043. ! !
  1044. !AssignmentNode methodsFor: '*Compiler-IR'!
  1045. shouldBeAliased
  1046. ^ super shouldBeAliased or: [ self isReferenced ]
  1047. ! !
  1048. !BlockClosure methodsFor: '*Compiler-IR'!
  1049. appendToInstruction: anIRInstruction
  1050. anIRInstruction appendBlock: self
  1051. ! !
  1052. !BlockNode methodsFor: '*Compiler-IR'!
  1053. subtreeNeedsAliasing
  1054. ^ self shouldBeAliased
  1055. ! !
  1056. !CascadeNode methodsFor: '*Compiler-IR'!
  1057. subtreeNeedsAliasing
  1058. ^ self parent isSequenceNode not
  1059. ! !
  1060. !SendNode methodsFor: '*Compiler-IR'!
  1061. shouldBeAliased
  1062. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  1063. See IRJSVisitor >> visitIRSend:"
  1064. | sends |
  1065. sends := (self method sendIndexes at: self selector) size.
  1066. ^ (super shouldBeAliased or: [
  1067. self isReferenced and: [
  1068. self index < sends or: [
  1069. self superSend ] ] ])
  1070. !
  1071. subtreeNeedsAliasing
  1072. ^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
  1073. ! !