Compiler-IR.st 28 KB

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