Compiler-IR.st 27 KB

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