Compiler-IR.st 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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. #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. receiver
  510. ^ self dagChildren first
  511. !
  512. selector
  513. ^ selector
  514. !
  515. selector: aString
  516. selector := aString
  517. ! !
  518. !IRSend methodsFor: 'testing'!
  519. isSend
  520. ^ true
  521. ! !
  522. !IRSend methodsFor: 'visiting'!
  523. acceptDagVisitor: aVisitor
  524. ^ aVisitor visitIRSend: self
  525. ! !
  526. IRInstruction subclass: #IRSequence
  527. slots: {}
  528. package: 'Compiler-IR'!
  529. !IRSequence methodsFor: 'testing'!
  530. isSequence
  531. ^ true
  532. ! !
  533. !IRSequence methodsFor: 'visiting'!
  534. acceptDagVisitor: aVisitor
  535. ^ aVisitor visitIRSequence: self
  536. ! !
  537. IRSequence subclass: #IRBlockSequence
  538. slots: {}
  539. package: 'Compiler-IR'!
  540. !IRBlockSequence methodsFor: 'visiting'!
  541. acceptDagVisitor: aVisitor
  542. ^ aVisitor visitIRBlockSequence: self
  543. ! !
  544. IRInstruction subclass: #IRValue
  545. slots: {#value}
  546. package: 'Compiler-IR'!
  547. !IRValue commentStamp!
  548. I am the simplest possible instruction. I represent a value.!
  549. !IRValue methodsFor: 'accessing'!
  550. value
  551. ^ value
  552. !
  553. value: aString
  554. value := aString
  555. ! !
  556. !IRValue methodsFor: 'testing'!
  557. needsBoxingAsReceiver
  558. ^ false
  559. ! !
  560. !IRValue methodsFor: 'visiting'!
  561. acceptDagVisitor: aVisitor
  562. ^ aVisitor visitIRValue: self
  563. ! !
  564. IRInstruction subclass: #IRVariable
  565. slots: {#variable}
  566. package: 'Compiler-IR'!
  567. !IRVariable commentStamp!
  568. I am a variable instruction.!
  569. !IRVariable methodsFor: 'accessing'!
  570. variable
  571. ^ variable
  572. !
  573. variable: aScopeVariable
  574. variable := aScopeVariable
  575. ! !
  576. !IRVariable methodsFor: 'testing'!
  577. isSelf
  578. ^ self variable isSelf
  579. !
  580. isSuper
  581. ^ self variable isSuper
  582. !
  583. isVariable
  584. ^ true
  585. !
  586. needsBoxingAsReceiver
  587. ^ self variable isPseudoVar not
  588. ! !
  589. !IRVariable methodsFor: 'visiting'!
  590. acceptDagVisitor: aVisitor
  591. ^ aVisitor visitIRVariable: self
  592. ! !
  593. IRInstruction subclass: #IRVerbatim
  594. slots: {#source}
  595. package: 'Compiler-IR'!
  596. !IRVerbatim methodsFor: 'accessing'!
  597. source
  598. ^ source
  599. !
  600. source: aString
  601. source := aString
  602. ! !
  603. !IRVerbatim methodsFor: 'visiting'!
  604. acceptDagVisitor: aVisitor
  605. ^ aVisitor visitIRVerbatim: self
  606. ! !
  607. ParentFakingPathDagVisitor subclass: #IRVisitor
  608. slots: {}
  609. package: 'Compiler-IR'!
  610. !IRVisitor methodsFor: 'visiting'!
  611. visitDagNode: aNode
  612. ^ self visitDagNodeVariantSimple: aNode
  613. !
  614. visitIRAssignment: anIRAssignment
  615. ^ self visitDagNode: anIRAssignment
  616. !
  617. visitIRBlockReturn: anIRBlockReturn
  618. ^ self visitIRReturn: anIRBlockReturn
  619. !
  620. visitIRBlockSequence: anIRBlockSequence
  621. ^ self visitIRSequence: anIRBlockSequence
  622. !
  623. visitIRClosure: anIRClosure
  624. ^ self visitDagNode: anIRClosure
  625. !
  626. visitIRDynamicArray: anIRDynamicArray
  627. ^ self visitDagNode: anIRDynamicArray
  628. !
  629. visitIRDynamicDictionary: anIRDynamicDictionary
  630. ^ self visitDagNode: anIRDynamicDictionary
  631. !
  632. visitIRInlinedClosure: anIRInlinedClosure
  633. ^ self visitIRClosure: anIRInlinedClosure
  634. !
  635. visitIRInlinedSequence: anIRInlinedSequence
  636. ^ self visitIRSequence: anIRInlinedSequence
  637. !
  638. visitIRMethod: anIRMethod
  639. ^ self visitDagNode: anIRMethod
  640. !
  641. visitIRNonLocalReturn: anIRNonLocalReturn
  642. ^ self visitDagNode: anIRNonLocalReturn
  643. !
  644. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  645. ^ self visitDagNode: anIRNonLocalReturnHandling
  646. !
  647. visitIRReturn: anIRReturn
  648. ^ self visitDagNode: anIRReturn
  649. !
  650. visitIRSend: anIRSend
  651. ^ self visitDagNode: anIRSend
  652. !
  653. visitIRSequence: anIRSequence
  654. ^ self visitDagNode: anIRSequence
  655. !
  656. visitIRTempDeclaration: anIRTempDeclaration
  657. ^ self visitDagNode: anIRTempDeclaration
  658. !
  659. visitIRValue: anIRValue
  660. ^ self visitDagNode: anIRValue
  661. !
  662. visitIRVariable: anIRVariable
  663. ^ self visitDagNode: anIRVariable
  664. !
  665. visitIRVerbatim: anIRVerbatim
  666. ^ self visitDagNode: anIRVerbatim
  667. ! !
  668. IRVisitor subclass: #IRJSTranslator
  669. slots: {#stream. #currentClass}
  670. package: 'Compiler-IR'!
  671. !IRJSTranslator methodsFor: 'accessing'!
  672. contents
  673. ^ self stream contents
  674. !
  675. currentClass
  676. ^ currentClass
  677. !
  678. currentClass: aClass
  679. currentClass := aClass
  680. !
  681. stream
  682. ^ stream
  683. !
  684. stream: aStream
  685. stream := aStream
  686. ! !
  687. !IRJSTranslator methodsFor: 'building'!
  688. buildMethodDeclaration: aMethod with: aBlock
  689. ^ #{
  690. #selector -> aMethod selector.
  691. #source -> aMethod source.
  692. #pragmas -> aMethod pragmas.
  693. #fn -> [ aBlock value. self contents ] value.
  694. #messageSends -> aMethod messageSends asArray.
  695. #args -> (aMethod arguments collect: [ :each | each value ]) asArray.
  696. #referencedClasses -> aMethod classReferences asArray.
  697. }
  698. ! !
  699. !IRJSTranslator methodsFor: 'initialization'!
  700. initialize
  701. super initialize.
  702. stream := JSStream new.
  703. ! !
  704. !IRJSTranslator methodsFor: 'visiting'!
  705. visitIRAssignment: anIRAssignment
  706. self stream
  707. nextPutAssignLhs: [self visit: anIRAssignment left]
  708. rhs: [self visit: anIRAssignment right].
  709. !
  710. visitIRClosure: anIRClosure
  711. self stream
  712. nextPutClosureWith: [
  713. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  714. each name asVariableName ]).
  715. self stream
  716. nextPutBlockContextFor: anIRClosure
  717. during: [ super visitIRClosure: anIRClosure ] ]
  718. arguments: anIRClosure arguments
  719. !
  720. visitIRDynamicArray: anIRDynamicArray
  721. self
  722. visitInstructionList: anIRDynamicArray dagChildren
  723. enclosedBetween: '[' and: ']'
  724. !
  725. visitIRDynamicDictionary: anIRDynamicDictionary
  726. self
  727. visitInstructionList: anIRDynamicDictionary dagChildren
  728. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  729. !
  730. visitIRMethod: anIRMethod
  731. ^ self
  732. buildMethodDeclaration: anIRMethod
  733. with: [ self stream
  734. nextPutFunctionWith: [
  735. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  736. each name asVariableName ]).
  737. self stream nextPutContextFor: anIRMethod during: [
  738. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  739. self stream nextPutVars:
  740. (internalVars asSet collect: [ :each | each variable alias ]) ].
  741. anIRMethod scope hasNonLocalReturn
  742. ifTrue: [
  743. self stream nextPutNonLocalReturnHandlingWith: [
  744. super visitIRMethod: anIRMethod ] ]
  745. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  746. arguments: anIRMethod arguments ]
  747. !
  748. visitIRNonLocalReturn: anIRNonLocalReturn
  749. self stream nextPutNonLocalReturnWith: [
  750. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  751. !
  752. visitIRReturn: anIRReturn
  753. self stream nextPutReturnWith: [
  754. super visitIRReturn: anIRReturn ]
  755. !
  756. visitIRSend: anIRSend
  757. | sends superclass |
  758. sends := (anIRSend method sendIndexes at: anIRSend selector) size.
  759. anIRSend receiver isSuper
  760. ifTrue: [ self visitSuperSend: anIRSend ]
  761. ifFalse: [ self visitSend: anIRSend ].
  762. anIRSend index < sends
  763. ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
  764. !
  765. visitIRSequence: anIRSequence
  766. anIRSequence dagChildren do: [ :each |
  767. self stream nextPutStatementWith: [ self visit: each ] ]
  768. !
  769. visitIRTempDeclaration: anIRTempDeclaration
  770. "self stream
  771. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  772. lf"
  773. !
  774. visitIRValue: anIRValue
  775. self stream nextPutAll: anIRValue value asJavaScriptSource
  776. !
  777. visitIRVariable: anIRVariable
  778. anIRVariable variable name = 'thisContext'
  779. ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
  780. ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
  781. !
  782. visitIRVerbatim: anIRVerbatim
  783. self stream nextPutAll: anIRVerbatim source
  784. !
  785. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  786. self stream nextPutAll: aString.
  787. anArray
  788. do: [ :each | self visit: each ]
  789. separatedBy: [ self stream nextPutAll: ',' ].
  790. stream nextPutAll: anotherString
  791. !
  792. visitReceiver: anIRInstruction
  793. | instr |
  794. anIRInstruction isSelf
  795. ifTrue: [ instr := anIRInstruction copy
  796. variable: (anIRInstruction variable copy name: '$self'; yourself);
  797. yourself ]
  798. ifFalse: [ instr := anIRInstruction ].
  799. instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
  800. self stream nextPutAll: '$recv('.
  801. self visit: instr.
  802. self stream nextPutAll: ')'
  803. !
  804. visitSend: anIRSend
  805. self visitReceiver: anIRSend receiver.
  806. self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
  807. self
  808. visitInstructionList: anIRSend arguments
  809. enclosedBetween: '(' and: ')'
  810. !
  811. visitSuperSend: anIRSend
  812. self stream
  813. nextPutAll: '('; lf;
  814. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  815. nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
  816. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  817. nextPutAll: '(', self currentClass asJavaScriptSource;
  818. nextPutAll: '.superclass||$boot.nilAsClass).fn.prototype.';
  819. nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
  820. nextPutAll: '$self, '.
  821. self
  822. visitInstructionList: anIRSend arguments
  823. enclosedBetween: '[' and: ']'.
  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. Object subclass: #JSStream
  831. slots: {#stream. #omitSemicolon}
  832. package: 'Compiler-IR'!
  833. !JSStream methodsFor: 'accessing'!
  834. contents
  835. ^ stream contents
  836. !
  837. omitSemicolon
  838. ^ omitSemicolon
  839. !
  840. omitSemicolon: aBoolean
  841. omitSemicolon := aBoolean
  842. ! !
  843. !JSStream methodsFor: 'initialization'!
  844. initialize
  845. super initialize.
  846. stream := '' writeStream.
  847. ! !
  848. !JSStream methodsFor: 'streaming'!
  849. lf
  850. stream lf
  851. !
  852. nextPut: aString
  853. stream nextPut: aString
  854. !
  855. nextPutAll: aString
  856. stream nextPutAll: aString
  857. !
  858. nextPutAssignLhs: aBlock rhs: anotherBlock
  859. aBlock value.
  860. stream nextPutAll: '='.
  861. anotherBlock value
  862. !
  863. nextPutBlockContextFor: anIRClosure during: aBlock
  864. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  865. self
  866. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  867. lf;
  868. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  869. lf;
  870. nextPutAll: '//>>excludeEnd("ctx");';
  871. lf.
  872. aBlock value.
  873. self
  874. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  875. lf;
  876. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  877. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  878. anIRClosure locals
  879. do: [ :each |
  880. self
  881. nextPutAll: each asVariableName;
  882. nextPutAll: ':';
  883. nextPutAll: each asVariableName ]
  884. separatedBy: [ self nextPutAll: ',' ].
  885. self
  886. nextPutAll: '},';
  887. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  888. lf;
  889. nextPutAll: '//>>excludeEnd("ctx");'
  890. !
  891. nextPutClosureWith: aBlock arguments: anArray
  892. stream nextPutAll: '(function('.
  893. anArray
  894. do: [ :each | stream nextPutAll: each asVariableName ]
  895. separatedBy: [ stream nextPut: ',' ].
  896. stream nextPutAll: '){'; lf.
  897. aBlock value.
  898. stream lf; nextPutAll: '})'
  899. !
  900. nextPutContextFor: aMethod during: aBlock
  901. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  902. self
  903. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  904. lf;
  905. nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
  906. lf;
  907. nextPutAll: '//>>excludeEnd("ctx");';
  908. lf.
  909. aBlock value.
  910. self
  911. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  912. lf;
  913. nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
  914. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  915. aMethod locals
  916. do: [ :each |
  917. self
  918. nextPutAll: each asVariableName;
  919. nextPutAll: ':';
  920. nextPutAll: each asVariableName ]
  921. separatedBy: [ self nextPutAll: ',' ].
  922. self
  923. nextPutAll: '})});';
  924. lf;
  925. nextPutAll: '//>>excludeEnd("ctx");'
  926. !
  927. nextPutFunctionWith: aBlock arguments: anArray
  928. stream nextPutAll: '(function ('.
  929. anArray
  930. do: [ :each | stream nextPutAll: each asVariableName ]
  931. separatedBy: [ stream nextPut: ',' ].
  932. stream nextPutAll: '){'; lf.
  933. stream nextPutAll: 'var self=this,$self=this;'; lf.
  934. aBlock value.
  935. stream lf; nextPutAll: '})'
  936. !
  937. nextPutIf: aBlock then: anotherBlock
  938. stream nextPutAll: 'if('.
  939. aBlock value.
  940. stream nextPutAll: '){'; lf.
  941. anotherBlock value.
  942. stream nextPutAll: '}'.
  943. self omitSemicolon: true
  944. !
  945. nextPutIf: aBlock then: ifBlock else: elseBlock
  946. stream nextPutAll: 'if('.
  947. aBlock value.
  948. stream nextPutAll: '){'; lf.
  949. ifBlock value.
  950. stream nextPutAll: '} else {'; lf.
  951. elseBlock value.
  952. stream nextPutAll: '}'.
  953. self omitSemicolon: true
  954. !
  955. nextPutMethodDeclaration: aMethod with: aBlock
  956. stream
  957. nextPutAll: '$core.method({'; lf;
  958. nextPutAll: 'selector: ', aMethod selector asJavaScriptSource, ','; lf;
  959. nextPutAll: 'source: ', aMethod source asJavaScriptSource, ',';lf.
  960. aBlock value.
  961. stream
  962. nextPutAll: ',', String lf, 'messageSends: ';
  963. nextPutAll: aMethod messageSends asArray asJavaScriptSource, ','; lf;
  964. nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavaScriptSource, ','; lf;
  965. nextPutAll: 'referencedClasses: ['.
  966. aMethod classReferences
  967. do: [ :each | stream nextPutAll: each asJavaScriptSource ]
  968. separatedBy: [ stream nextPutAll: ',' ].
  969. stream
  970. nextPutAll: ']';
  971. nextPutAll: '})'
  972. !
  973. nextPutNonLocalReturnHandlingWith: aBlock
  974. stream
  975. nextPutAll: 'var $early={};'; lf;
  976. nextPutAll: 'try {'; lf.
  977. aBlock value.
  978. stream
  979. nextPutAll: '}'; lf;
  980. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  981. !
  982. nextPutNonLocalReturnWith: aBlock
  983. stream nextPutAll: 'throw $early=['.
  984. aBlock value.
  985. stream nextPutAll: ']'
  986. !
  987. nextPutReturnWith: aBlock
  988. stream nextPutAll: 'return '.
  989. aBlock value
  990. !
  991. nextPutSendIndexFor: anIRSend
  992. self
  993. nextPutAll: ';'; lf;
  994. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
  995. nextPutAll: anIRSend scope alias;
  996. nextPutAll: '.sendIdx[';
  997. nextPutAll: anIRSend selector asJavaScriptSource;
  998. nextPutAll: ']=';
  999. nextPutAll: anIRSend index asString;
  1000. nextPutAll: ';'; lf;
  1001. nextPutAll: '//>>excludeEnd("ctx")'
  1002. !
  1003. nextPutStatementWith: aBlock
  1004. self omitSemicolon: false.
  1005. aBlock value.
  1006. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  1007. self omitSemicolon: false.
  1008. stream lf
  1009. !
  1010. nextPutVars: aCollection
  1011. aCollection ifNotEmpty: [
  1012. stream nextPutAll: 'var '.
  1013. aCollection
  1014. do: [ :each | stream nextPutAll: each ]
  1015. separatedBy: [ stream nextPutAll: ',' ].
  1016. stream nextPutAll: ';'; lf ]
  1017. ! !
  1018. !ASTNode methodsFor: '*Compiler-IR'!
  1019. isReferenced
  1020. "Answer true if the receiver is referenced by other nodes.
  1021. Do not take sequences or assignments into account"
  1022. ^ (self parent isSequenceNode or: [
  1023. self parent isAssignmentNode ]) not
  1024. !
  1025. subtreeNeedsAliasing
  1026. ^ self shouldBeAliased or: [
  1027. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1028. ! !
  1029. !AssignmentNode methodsFor: '*Compiler-IR'!
  1030. shouldBeAliased
  1031. ^ super shouldBeAliased or: [ self isReferenced ]
  1032. ! !
  1033. !BlockClosure methodsFor: '*Compiler-IR'!
  1034. appendToInstruction: anIRInstruction
  1035. anIRInstruction appendBlock: self
  1036. ! !
  1037. !BlockNode methodsFor: '*Compiler-IR'!
  1038. subtreeNeedsAliasing
  1039. ^ self shouldBeAliased
  1040. ! !
  1041. !CascadeNode methodsFor: '*Compiler-IR'!
  1042. subtreeNeedsAliasing
  1043. ^ self parent isSequenceNode not
  1044. ! !
  1045. !SendNode methodsFor: '*Compiler-IR'!
  1046. shouldBeAliased
  1047. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  1048. See IRJSVisitor >> visitIRSend:"
  1049. | sends |
  1050. sends := (self method sendIndexes at: self selector) size.
  1051. ^ (super shouldBeAliased or: [
  1052. self isReferenced and: [
  1053. self index < sends or: [
  1054. self superSend ] ] ])
  1055. !
  1056. subtreeNeedsAliasing
  1057. ^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
  1058. ! !