Compiler-IR.st 28 KB

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