Compiler-IR.st 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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. self irMethod attachments
  661. at: aString
  662. put: (NativeFunction
  663. constructorNamed: #Function
  664. value: 'return this.', irMethod selector asJavaScriptMethodName, '()')
  665. ! !
  666. ParentFakingPathDagVisitor subclass: #IRVisitor
  667. slots: {}
  668. package: 'Compiler-IR'!
  669. !IRVisitor methodsFor: 'visiting'!
  670. visitDagNode: aNode
  671. ^ self visitDagNodeVariantSimple: aNode
  672. !
  673. visitIRAssignment: anIRAssignment
  674. ^ self visitDagNode: anIRAssignment
  675. !
  676. visitIRBlockReturn: anIRBlockReturn
  677. ^ self visitIRReturn: anIRBlockReturn
  678. !
  679. visitIRBlockSequence: anIRBlockSequence
  680. ^ self visitIRSequence: anIRBlockSequence
  681. !
  682. visitIRClosure: anIRClosure
  683. ^ self visitDagNode: anIRClosure
  684. !
  685. visitIRDynamicArray: anIRDynamicArray
  686. ^ self visitDagNode: anIRDynamicArray
  687. !
  688. visitIRDynamicDictionary: anIRDynamicDictionary
  689. ^ self visitDagNode: anIRDynamicDictionary
  690. !
  691. visitIRMethod: anIRMethod
  692. ^ self visitDagNode: anIRMethod
  693. !
  694. visitIRNonLocalReturn: anIRNonLocalReturn
  695. ^ self visitDagNode: anIRNonLocalReturn
  696. !
  697. visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
  698. ^ self visitDagNode: anIRNonLocalReturnHandling
  699. !
  700. visitIRReturn: anIRReturn
  701. ^ self visitDagNode: anIRReturn
  702. !
  703. visitIRSend: anIRSend
  704. ^ self visitDagNode: anIRSend
  705. !
  706. visitIRSequence: anIRSequence
  707. ^ self visitDagNode: anIRSequence
  708. !
  709. visitIRTempDeclaration: anIRTempDeclaration
  710. ^ self visitDagNode: anIRTempDeclaration
  711. !
  712. visitIRValue: anIRValue
  713. ^ self visitDagNode: anIRValue
  714. !
  715. visitIRVariable: anIRVariable
  716. ^ self visitDagNode: anIRVariable
  717. !
  718. visitIRVerbatim: anIRVerbatim
  719. ^ self visitDagNode: anIRVerbatim
  720. ! !
  721. IRVisitor subclass: #IRJSTranslator
  722. slots: {#stream. #currentClass}
  723. package: 'Compiler-IR'!
  724. !IRJSTranslator methodsFor: 'accessing'!
  725. contents
  726. ^ self stream contents
  727. !
  728. currentClass
  729. ^ currentClass
  730. !
  731. currentClass: aClass
  732. currentClass := aClass
  733. !
  734. stream
  735. ^ stream
  736. !
  737. stream: aStream
  738. stream := aStream
  739. ! !
  740. !IRJSTranslator methodsFor: 'initialization'!
  741. initialize
  742. super initialize.
  743. stream := JSStream new.
  744. ! !
  745. !IRJSTranslator methodsFor: 'visiting'!
  746. visitIRAssignment: anIRAssignment
  747. self stream
  748. nextPutAssignLhs: [self visit: anIRAssignment left]
  749. rhs: [self visit: anIRAssignment right].
  750. !
  751. visitIRClosure: anIRClosure
  752. self stream
  753. nextPutClosureWith: [
  754. self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
  755. each name asVariableName ]).
  756. self stream
  757. nextPutBlockContextFor: anIRClosure
  758. during: [ super visitIRClosure: anIRClosure ] ]
  759. arguments: anIRClosure arguments
  760. !
  761. visitIRDynamicArray: anIRDynamicArray
  762. self
  763. visitInstructionList: anIRDynamicArray dagChildren
  764. enclosedBetween: '[' and: ']'
  765. !
  766. visitIRDynamicDictionary: anIRDynamicDictionary
  767. self
  768. visitInstructionList: anIRDynamicDictionary dagChildren
  769. enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
  770. !
  771. visitIRMethod: anIRMethod
  772. self stream
  773. nextPutFunctionWith: [
  774. self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
  775. each name asVariableName ]).
  776. self stream nextPutContextFor: anIRMethod during: [
  777. anIRMethod internalVariables ifNotEmpty: [ :internalVars |
  778. self stream nextPutVars:
  779. (internalVars collect: [ :each | each variable alias ]) asSet ].
  780. anIRMethod scope hasNonLocalReturn
  781. ifTrue: [
  782. self stream nextPutNonLocalReturnHandlingWith: [
  783. super visitIRMethod: anIRMethod ] ]
  784. ifFalse: [ super visitIRMethod: anIRMethod ] ]]
  785. arguments: anIRMethod arguments.
  786. ^ anIRMethod compiledSource: self contents; yourself
  787. !
  788. visitIRNonLocalReturn: anIRNonLocalReturn
  789. self stream nextPutNonLocalReturnWith: [
  790. super visitIRNonLocalReturn: anIRNonLocalReturn ]
  791. !
  792. visitIRReturn: anIRReturn
  793. self stream nextPutReturnWith: [
  794. super visitIRReturn: anIRReturn ]
  795. !
  796. visitIRSend: anIRSend
  797. | prefixes suffixes workBlock |
  798. prefixes := #().
  799. suffixes := #().
  800. workBlock := [ self visitSend: anIRSend ].
  801. anIRSend index < (anIRSend method sendIndexes at: anIRSend selector) size ifTrue: [
  802. suffixes add:
  803. anIRSend scope alias,
  804. '.sendIdx[',
  805. anIRSend selector asJavaScriptSource,
  806. ']=',
  807. anIRSend index asString ].
  808. anIRSend receiver isSuper ifTrue: [
  809. prefixes add: anIRSend scope alias, '.supercall = true'.
  810. suffixes add: anIRSend scope alias, '.supercall = false'.
  811. workBlock := [ self visitSuperSend: anIRSend ] ].
  812. self stream nextPutBefore: prefixes after: suffixes with: workBlock
  813. !
  814. visitIRSequence: anIRSequence
  815. anIRSequence dagChildren do: [ :each |
  816. self stream nextPutStatementWith: [ self visit: each ] ]
  817. !
  818. visitIRTempDeclaration: anIRTempDeclaration
  819. "self stream
  820. nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
  821. lf"
  822. !
  823. visitIRValue: anIRValue
  824. self stream nextPutAll: anIRValue value asJavaScriptSource
  825. !
  826. visitIRVariable: anIRVariable
  827. self stream nextPutAll: anIRVariable variable alias
  828. !
  829. visitIRVerbatim: anIRVerbatim
  830. self stream nextPutAll: anIRVerbatim source
  831. !
  832. visitInstructionList: anArray enclosedBetween: aString and: anotherString
  833. self stream nextPutAll: aString.
  834. anArray
  835. do: [ :each | self visit: each ]
  836. separatedBy: [ self stream nextPutAll: ',' ].
  837. stream nextPutAll: anotherString
  838. !
  839. visitReceiver: anIRInstruction
  840. anIRInstruction asReceiver
  841. ifNotNil: [ :instr | self visit: instr ]
  842. ifNil: [
  843. self stream nextPutAll: '$recv('.
  844. self visit: anIRInstruction.
  845. self stream nextPutAll: ')' ]
  846. !
  847. visitSend: anIRSend
  848. self visitReceiver: anIRSend receiver.
  849. self stream nextPutAll: '.', anIRSend javaScriptSelector.
  850. self
  851. visitInstructionList: anIRSend arguments
  852. enclosedBetween: '(' and: ')'
  853. !
  854. visitSuperSend: anIRSend
  855. self stream
  856. nextPutAll: anIRSend receiver variable lookupAsJavaScriptSource, '.';
  857. nextPutAll: anIRSend javaScriptSelector, '.call'.
  858. self
  859. visitInstructionList: {anIRSend receiver asReceiver}, anIRSend arguments
  860. enclosedBetween: '(' and: ')'
  861. ! !
  862. Object subclass: #JSStream
  863. slots: {#stream. #omitSemicolon}
  864. package: 'Compiler-IR'!
  865. !JSStream methodsFor: 'accessing'!
  866. contents
  867. ^ stream contents
  868. !
  869. omitSemicolon
  870. ^ omitSemicolon
  871. !
  872. omitSemicolon: aBoolean
  873. omitSemicolon := aBoolean
  874. ! !
  875. !JSStream methodsFor: 'initialization'!
  876. initialize
  877. super initialize.
  878. stream := '' writeStream.
  879. ! !
  880. !JSStream methodsFor: 'streaming'!
  881. lf
  882. stream lf
  883. !
  884. nextPut: aString
  885. stream nextPut: aString
  886. !
  887. nextPutAll: aString
  888. stream nextPutAll: aString
  889. !
  890. nextPutAssignLhs: aBlock rhs: anotherBlock
  891. aBlock value.
  892. stream nextPutAll: '='.
  893. anotherBlock value
  894. !
  895. nextPutBefore: prefixCollection after: suffixCollection with: aBlock
  896. suffixCollection isEmpty
  897. ifTrue: [ self nextPutBefore: prefixCollection with: aBlock ]
  898. ifFalse: [
  899. self
  900. nextPutAll: '['; nextPutBefore: prefixCollection with: aBlock; lf;
  901. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf.
  902. suffixCollection do: [ :each | self nextPutAll: ','; nextPutAll: each ].
  903. self
  904. lf;
  905. nextPutAll: '//>>excludeEnd("ctx");'; lf;
  906. nextPutAll: '][0]' ]
  907. !
  908. nextPutBefore: aCollection with: aBlock
  909. aCollection isEmpty ifTrue: [ aBlock value ] ifFalse: [
  910. self nextPutAll: '('; lf; nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf.
  911. aCollection do: [ :each | self nextPutAll: each; nextPutAll: ',' ].
  912. self lf; nextPutAll: '//>>excludeEnd("ctx");'; lf.
  913. aBlock value.
  914. self nextPutAll: ')' ]
  915. !
  916. nextPutBlockContextFor: anIRClosure during: aBlock
  917. anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  918. self
  919. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  920. lf;
  921. nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
  922. lf;
  923. nextPutAll: '//>>excludeEnd("ctx");';
  924. lf.
  925. aBlock value.
  926. self
  927. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  928. lf;
  929. nextPutAll: '}, function(', anIRClosure scope alias, ') {';
  930. nextPutAll: anIRClosure scope alias, '.fillBlock({'.
  931. anIRClosure locals
  932. do: [ :each |
  933. self
  934. nextPutAll: each asVariableName;
  935. nextPutAll: ':';
  936. nextPutAll: each asVariableName ]
  937. separatedBy: [ self nextPutAll: ',' ].
  938. self
  939. nextPutAll: '},';
  940. nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
  941. lf;
  942. nextPutAll: '//>>excludeEnd("ctx");'
  943. !
  944. nextPutClosureWith: aBlock arguments: anArray
  945. stream nextPutAll: '(function('.
  946. anArray
  947. do: [ :each | stream nextPutAll: each asVariableName ]
  948. separatedBy: [ stream nextPut: ',' ].
  949. stream nextPutAll: '){'; lf.
  950. aBlock value.
  951. stream lf; nextPutAll: '})'
  952. !
  953. nextPutContextFor: aMethod during: aBlock
  954. aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
  955. self
  956. nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
  957. lf;
  958. nextPutAll: 'return $core.withContext(function(', aMethod 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(', aMethod scope alias, ') {', aMethod scope alias;
  967. nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
  968. aMethod 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. lf;
  978. nextPutAll: '//>>excludeEnd("ctx");'
  979. !
  980. nextPutFunctionWith: aBlock arguments: anArray
  981. stream nextPutAll: 'function ('.
  982. anArray
  983. do: [ :each | stream nextPutAll: each asVariableName ]
  984. separatedBy: [ stream nextPut: ',' ].
  985. stream nextPutAll: '){'; lf.
  986. stream nextPutAll: 'var self=this,$self=this;'; lf.
  987. aBlock value.
  988. stream lf; nextPutAll: '}'
  989. !
  990. nextPutIf: aBlock then: anotherBlock
  991. stream nextPutAll: 'if('.
  992. aBlock value.
  993. stream nextPutAll: '){'; lf.
  994. anotherBlock value.
  995. stream nextPutAll: '}'.
  996. self omitSemicolon: true
  997. !
  998. nextPutIf: aBlock then: ifBlock else: elseBlock
  999. stream nextPutAll: 'if('.
  1000. aBlock value.
  1001. stream nextPutAll: '){'; lf.
  1002. ifBlock value.
  1003. stream nextPutAll: '} else {'; lf.
  1004. elseBlock value.
  1005. stream nextPutAll: '}'.
  1006. self omitSemicolon: true
  1007. !
  1008. nextPutNonLocalReturnHandlingWith: aBlock
  1009. stream
  1010. nextPutAll: 'var $early={};'; lf;
  1011. nextPutAll: 'try {'; lf.
  1012. aBlock value.
  1013. stream
  1014. nextPutAll: '}'; lf;
  1015. nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
  1016. !
  1017. nextPutNonLocalReturnWith: aBlock
  1018. stream nextPutAll: 'throw $early=['.
  1019. aBlock value.
  1020. stream nextPutAll: ']'
  1021. !
  1022. nextPutReturnWith: aBlock
  1023. stream nextPutAll: 'return '.
  1024. aBlock value
  1025. !
  1026. nextPutStatementWith: aBlock
  1027. self omitSemicolon: false.
  1028. aBlock value.
  1029. self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
  1030. self omitSemicolon: false.
  1031. stream lf
  1032. !
  1033. nextPutVars: aCollection
  1034. aCollection ifNotEmpty: [
  1035. stream nextPutAll: 'var '.
  1036. aCollection
  1037. do: [ :each | stream nextPutAll: each ]
  1038. separatedBy: [ stream nextPutAll: ',' ].
  1039. stream nextPutAll: ';'; lf ]
  1040. ! !
  1041. IRPragmator setTraitComposition: {TPragmator} asTraitComposition!
  1042. ! !
  1043. !ASTNode methodsFor: '*Compiler-IR'!
  1044. requiresSmalltalkContext
  1045. "Answer true if the receiver requires a smalltalk context.
  1046. Only send nodes require a context.
  1047. If no node requires a context, the method will be compiled without one.
  1048. See `IRJSTranslator` and `JSStream` for context creation"
  1049. ^ self dagChildren anySatisfy: [ :each | each requiresSmalltalkContext ]
  1050. ! !
  1051. !AssignmentNode methodsFor: '*Compiler-IR'!
  1052. subtreeNeedsAliasing
  1053. ^ true
  1054. ! !
  1055. !BlockNode methodsFor: '*Compiler-IR'!
  1056. subtreeNeedsAliasing
  1057. ^ self shouldBeAliased
  1058. ! !
  1059. !CascadeNode methodsFor: '*Compiler-IR'!
  1060. subtreeNeedsAliasing
  1061. ^ true
  1062. ! !
  1063. !ExpressionNode methodsFor: '*Compiler-IR'!
  1064. subtreeNeedsAliasing
  1065. ^ self shouldBeAliased or: [
  1066. self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  1067. ! !
  1068. !JSStatementNode methodsFor: '*Compiler-IR'!
  1069. requiresSmalltalkContext
  1070. ^ true
  1071. ! !
  1072. !PseudoVar methodsFor: '*Compiler-IR'!
  1073. asReceiver
  1074. ^ self class receiverNames
  1075. at: self name
  1076. ifPresent: [ :newName | self copy name: newName; yourself ]
  1077. ifAbsent: [ self ]
  1078. ! !
  1079. !ScopeVar methodsFor: '*Compiler-IR'!
  1080. asReceiver
  1081. "Return customized copy to use as receiver,
  1082. or self if suffices."
  1083. ^ nil
  1084. ! !
  1085. !SendNode methodsFor: '*Compiler-IR'!
  1086. requiresSmalltalkContext
  1087. ^ true
  1088. ! !