Compiler-IR.st 28 KB

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