123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380 |
- Smalltalk createPackage: 'Compiler-IR'!
- NodeVisitor subclass: #IRASTTranslator
- slots: {#source. #theClass. #method. #sequence. #nextAlias}
- package: 'Compiler-IR'!
- I am the AST (abstract syntax tree) visitor responsible for building the intermediate representation graph.!
- method
- ^ method
- method: anIRMethod
- method := anIRMethod
- nextAlias
- nextAlias ifNil: [ nextAlias := 0 ].
- nextAlias := nextAlias + 1.
- ^ nextAlias asString
- sequence
- ^ sequence
- sequence: anIRSequence
- sequence := anIRSequence
- source
- ^ source
- source: aString
- source := aString
- theClass
- ^ theClass
- theClass: aClass
- theClass := aClass
- withSequence: aSequence do: aBlock
- | outerSequence |
- outerSequence := self sequence.
- self sequence: aSequence.
- aBlock value.
- self sequence: outerSequence.
- ^ aSequence
- addToSequence: anInstruction
- anInstruction ifNotNil: [
- anInstruction isVariable ifFalse: [
- self sequence add: anInstruction ] ].
- ^ anInstruction
- alias: aNode
- | variable |
- aNode isImmutable ifTrue: [ ^ self visit: aNode ].
- variable := IRVariable new
- variable: (AliasVar new name: '$', self nextAlias);
- yourself.
- self addToSequence: (IRAssignment new
- add: variable;
- add: (self visit: aNode);
- yourself).
- self method internalVariables add: variable.
- ^ variable
- aliasTemporally: aCollection
- "https://lolg.it/amber/amber/issues/296
-
- If a node is aliased, all preceding ones are aliased as well.
- The tree is iterated twice. First we get the aliasing dependency,
- then the aliasing itself is done"
- | threshold shouldAlias |
- threshold := aCollection reversed
- detect: [ :each | each subtreeNeedsAliasing ]
- ifNone: [ ^ self visitAll: aCollection ].
- shouldAlias := true.
- ^ aCollection collect: [ :each |
- shouldAlias
- ifTrue: [ each == threshold ifTrue: [ shouldAlias := false ]. self alias: each ]
- ifFalse: [ self visit: each ] ]
- visitAssignmentNode: aNode
- | left right assignment |
- right := self visit: aNode right.
- left := self visit: aNode left.
- self addToSequence: (IRAssignment new
- add: left;
- add: right;
- yourself).
- ^ left
- visitBlockNode: aNode
- | closure |
- closure := IRClosure new
- arguments: aNode parameters;
- requiresSmalltalkContext: aNode requiresSmalltalkContext;
- scope: aNode scope;
- yourself.
- aNode scope temps do: [ :each |
- closure add: (IRTempDeclaration new
- name: each name;
- scope: aNode scope;
- yourself) ].
- aNode dagChildren do: [ :each | closure add: (self visit: each) ].
- ^ closure
- visitBlockSequenceNode: aNode
- ^ self
- withSequence: IRBlockSequence new
- do: [
- aNode dagChildren ifNotEmpty: [
- aNode dagChildren allButLast do: [ :each |
- self addToSequence: (self visitOrAlias: each) ].
- aNode dagChildren last isReturnNode
- ifFalse: [ self addToSequence: (IRBlockReturn new add: (self visitOrAlias: aNode dagChildren last); yourself) ]
- ifTrue: [ self addToSequence: (self visitOrAlias: aNode dagChildren last) ] ]]
- visitCascadeNode: aNode
- | receiver |
- receiver := aNode receiver.
- receiver isImmutable ifFalse: [
- | alias |
- alias := self alias: receiver.
- receiver := VariableNode new binding: alias variable ].
- aNode dagChildren do: [ :each | each receiver: receiver ].
- aNode dagChildren allButLast do: [ :each |
- self addToSequence: (self visit: each) ].
- ^ self visitOrAlias: aNode dagChildren last
- visitDynamicArrayNode: aNode
- | array |
- array := IRDynamicArray new.
- (self aliasTemporally: aNode dagChildren) do: [ :each | array add: each ].
- ^ array
- visitDynamicDictionaryNode: aNode
- | dictionary |
- dictionary := IRDynamicDictionary new.
- (self aliasTemporally: aNode dagChildren) do: [ :each | dictionary add: each ].
- ^ dictionary
- visitJSStatementNode: aNode
- ^ IRVerbatim new
- source: aNode source crlfSanitized;
- yourself
- visitMethodNode: aNode
- self method: (IRMethod new
- source: self source crlfSanitized;
- pragmas: (aNode pragmas collect: [ :each |
- Message
- selector: each selector
- arguments: (each arguments collect: [ :eachArg |
- eachArg isString ifTrue: [ eachArg crlfSanitized ] ifFalse: [ eachArg ]])]);
- theClass: self theClass;
- arguments: aNode arguments;
- selector: aNode selector;
- sendIndexes: aNode sendIndexes;
- requiresSmalltalkContext: aNode requiresSmalltalkContext;
- classReferences: aNode classReferences;
- scope: aNode scope;
- yourself).
- aNode scope temps do: [ :each |
- self method add: (IRTempDeclaration new
- name: each name;
- scope: aNode scope;
- yourself) ].
- self method dagChildren addAll: (self visitAllChildren: aNode).
- aNode scope hasLocalReturn ifFalse: [self method
- add: (IRReturn new
- add: (IRVariable new
- variable: (aNode scope pseudoVars at: 'self');
- yourself);
- yourself);
- add: (IRVerbatim new source: ';', String lf; yourself) ].
- ^ self method
- visitOrAlias: aNode
- ^ aNode shouldBeAliased
- ifTrue: [ self alias: aNode ]
- ifFalse: [ self visit: aNode ]
- visitReturnNode: aNode
- | return |
- return := aNode nonLocalReturn
- ifTrue: [ IRNonLocalReturn new ]
- ifFalse: [ IRReturn new ].
- return scope: aNode scope.
- aNode dagChildren do: [ :each |
- return add: (self visitOrAlias: each) ].
- ^ return
- visitSendNode: aNode
- | send |
- send := IRSend new.
- send
- selector: aNode selector;
- index: aNode index.
-
- (self aliasTemporally: aNode dagChildren) do: [ :each | send add: each ].
- ^ send
- visitSequenceNode: aNode
- ^ self
- withSequence: IRSequence new
- do: [ aNode dagChildren do: [ :each |
- self addToSequence: (self visitOrAlias: each) ] ]
- visitValueNode: aNode
- ^ IRValue new
- value: aNode value;
- yourself
- visitVariableNode: aNode
- ^ IRVariable new
- variable: aNode binding;
- yourself
- DagParentNode subclass: #IRInstruction
- slots: {#parent}
- package: 'Compiler-IR'!
- I am the abstract root class of the IR (intermediate representation) instructions class hierarchy.
- The IR graph is used to emit JavaScript code using a JSStream.!
- method
- ^ self parent method
- parent
- ^ parent
- parent: anIRInstruction
- parent := anIRInstruction
- scope
- ^ self parent ifNotNil: [ :node |
- node scope ]
- add: anObject
- ^ self addDagChild: anObject
- remove: anIRInstruction
- self dagChildren remove: anIRInstruction
- replace: anIRInstruction with: anotherIRInstruction
- anotherIRInstruction parent: self.
- self dagChildren
- at: (self dagChildren indexOf: anIRInstruction)
- put: anotherIRInstruction
- replaceWith: anIRInstruction
- self parent replace: self with: anIRInstruction
- isClosure
- ^ false
- isInlined
- ^ false
- isMethod
- ^ false
- isSelf
- ^ false
- isSend
- ^ false
- isSequence
- ^ false
- isSuper
- ^ false
- isTempDeclaration
- ^ false
- isVariable
- ^ false
- needsBoxingAsReceiver
- ^ true
- yieldsValue
- ^ true
- on: aBuilder
- ^ self new
- builder: aBuilder;
- yourself
- IRInstruction subclass: #IRAssignment
- slots: {}
- package: 'Compiler-IR'!
- left
- ^ self dagChildren first
- right
- ^ self dagChildren last
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRAssignment: self
- IRInstruction subclass: #IRDynamicArray
- slots: {}
- package: 'Compiler-IR'!
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRDynamicArray: self
- IRInstruction subclass: #IRDynamicDictionary
- slots: {}
- package: 'Compiler-IR'!
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRDynamicDictionary: self
- IRInstruction subclass: #IRScopedInstruction
- slots: {#scope}
- package: 'Compiler-IR'!
- scope
- ^ scope
- scope: aScope
- scope := aScope
- IRScopedInstruction subclass: #IRClosureInstruction
- slots: {#arguments. #requiresSmalltalkContext}
- package: 'Compiler-IR'!
- arguments
- ^ arguments ifNil: [ #() ]
- arguments: aCollection
- arguments := aCollection
- locals
- ^ self arguments, (self tempDeclarations collect: [ :each | each name ])
- requiresSmalltalkContext
- ^ requiresSmalltalkContext ifNil: [ false ]
- requiresSmalltalkContext: anObject
- requiresSmalltalkContext := anObject
- scope: aScope
- super scope: aScope.
- aScope instruction: self
- tempDeclarations
- ^ self dagChildren select: [ :each |
- each isTempDeclaration ]
- IRClosureInstruction subclass: #IRClosure
- slots: {}
- package: 'Compiler-IR'!
- sequence
- ^ self dagChildren last
- isClosure
- ^ true
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRClosure: self
- IRClosureInstruction subclass: #IRMethod
- slots: {#theClass. #source. #compiledSource. #attachments. #selector. #pragmas. #classReferences. #sendIndexes. #internalVariables}
- package: 'Compiler-IR'!
- I am a method instruction!
- attachments
- ^ attachments ifNil: [ attachments := #{} ]
- classReferences
- ^ classReferences
- classReferences: aCollection
- classReferences := aCollection
- compiledSource
- ^ compiledSource
- compiledSource: anObject
- compiledSource := anObject
- internalVariables
- ^ internalVariables ifNil: [ internalVariables := Set new ]
- messageSends
- ^ self sendIndexes keys
- method
- ^ self
- pragmas
- ^ pragmas
- pragmas: aCollection
- pragmas := aCollection
- selector
- ^ selector
- selector: aString
- selector := aString
- sendIndexes
- ^ sendIndexes
- sendIndexes: aDictionary
- sendIndexes := aDictionary
- source
- ^ source
- source: aString
- source := aString
- theClass
- ^ theClass
- theClass: aClass
- theClass := aClass
- isMethod
- ^ true
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRMethod: self
- IRScopedInstruction subclass: #IRReturn
- slots: {}
- package: 'Compiler-IR'!
- I am a local return instruction.!
- expression
- ^ self dagChildren single
- scope
- ^ scope ifNil: [ self parent scope ]
- yieldsValue
- ^ false
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRReturn: self
- IRReturn subclass: #IRBlockReturn
- slots: {}
- package: 'Compiler-IR'!
- Smalltalk blocks return their last statement. I am a implicit block return instruction.!
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRBlockReturn: self
- IRReturn subclass: #IRNonLocalReturn
- slots: {}
- package: 'Compiler-IR'!
- I am a non local return instruction.
- Non local returns are handled using a try/catch JavaScript statement.
- See `IRNonLocalReturnHandling` class.!
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRNonLocalReturn: self
- IRScopedInstruction subclass: #IRTempDeclaration
- slots: {#name}
- package: 'Compiler-IR'!
- name
- ^ name
- name: aString
- name := aString
- isTempDeclaration
- ^ true
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRTempDeclaration: self
- IRInstruction subclass: #IRSend
- slots: {#selector. #javaScriptSelector. #index}
- package: 'Compiler-IR'!
- I am a message send instruction.!
- arguments
- ^ self dagChildren allButFirst
- index
- ^ index
- index: anInteger
- index := anInteger
- javaScriptSelector
- ^ javaScriptSelector ifNil: [ javaScriptSelector := self selector asJavaScriptMethodName ]
- javaScriptSelector: aString
- javaScriptSelector := aString
- receiver
- ^ self dagChildren first
- selector
- ^ selector
- selector: aString
- selector := aString
- isSend
- ^ true
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRSend: self
- IRInstruction subclass: #IRSequence
- slots: {}
- package: 'Compiler-IR'!
- isSequence
- ^ true
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRSequence: self
- IRSequence subclass: #IRBlockSequence
- slots: {}
- package: 'Compiler-IR'!
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRBlockSequence: self
- IRInstruction subclass: #IRValue
- slots: {#value}
- package: 'Compiler-IR'!
- I am the simplest possible instruction. I represent a value.!
- value
- ^ value
- value: aString
- value := aString
- needsBoxingAsReceiver
- ^ false
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRValue: self
- IRInstruction subclass: #IRVariable
- slots: {#variable}
- package: 'Compiler-IR'!
- I am a variable instruction.!
- variable
- ^ variable
- variable: aScopeVariable
- variable := aScopeVariable
- isSelf
- ^ self variable isSelf
- isSuper
- ^ self variable isSuper
- isVariable
- ^ true
- needsBoxingAsReceiver
- ^ self variable isPseudoVar not
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRVariable: self
- IRInstruction subclass: #IRVerbatim
- slots: {#source}
- package: 'Compiler-IR'!
- source
- ^ source
- source: aString
- source := aString
- acceptDagVisitor: aVisitor
- ^ aVisitor visitIRVerbatim: self
- Object subclass: #IRPragmator
- slots: {#irMethod}
- package: 'Compiler-IR'!
- irMethod
- ^ irMethod
- irMethod: anObject
- irMethod := anObject
- value: anIRMethod
- self irMethod: anIRMethod.
- self processPragmas: anIRMethod pragmas.
- ^ anIRMethod
- IRPragmator subclass: #IRLatePragmator
- slots: {}
- package: 'Compiler-IR'!
- jsOverride: aString
- self irMethod attachments
- at: aString
- put: (NativeFunction
- constructorNamed: #Function
- value: 'return this.', irMethod selector asJavaScriptMethodName, '()')
- ParentFakingPathDagVisitor subclass: #IRVisitor
- slots: {}
- package: 'Compiler-IR'!
- visitDagNode: aNode
- ^ self visitDagNodeVariantSimple: aNode
- visitIRAssignment: anIRAssignment
- ^ self visitDagNode: anIRAssignment
- visitIRBlockReturn: anIRBlockReturn
- ^ self visitIRReturn: anIRBlockReturn
- visitIRBlockSequence: anIRBlockSequence
- ^ self visitIRSequence: anIRBlockSequence
- visitIRClosure: anIRClosure
- ^ self visitDagNode: anIRClosure
- visitIRDynamicArray: anIRDynamicArray
- ^ self visitDagNode: anIRDynamicArray
- visitIRDynamicDictionary: anIRDynamicDictionary
- ^ self visitDagNode: anIRDynamicDictionary
- visitIRInlinedClosure: anIRInlinedClosure
- ^ self visitIRClosure: anIRInlinedClosure
- visitIRInlinedSequence: anIRInlinedSequence
- ^ self visitIRSequence: anIRInlinedSequence
- visitIRMethod: anIRMethod
- ^ self visitDagNode: anIRMethod
- visitIRNonLocalReturn: anIRNonLocalReturn
- ^ self visitDagNode: anIRNonLocalReturn
- visitIRNonLocalReturnHandling: anIRNonLocalReturnHandling
- ^ self visitDagNode: anIRNonLocalReturnHandling
- visitIRReturn: anIRReturn
- ^ self visitDagNode: anIRReturn
- visitIRSend: anIRSend
- ^ self visitDagNode: anIRSend
- visitIRSequence: anIRSequence
- ^ self visitDagNode: anIRSequence
- visitIRTempDeclaration: anIRTempDeclaration
- ^ self visitDagNode: anIRTempDeclaration
- visitIRValue: anIRValue
- ^ self visitDagNode: anIRValue
- visitIRVariable: anIRVariable
- ^ self visitDagNode: anIRVariable
- visitIRVerbatim: anIRVerbatim
- ^ self visitDagNode: anIRVerbatim
- IRVisitor subclass: #IRJSTranslator
- slots: {#stream. #currentClass}
- package: 'Compiler-IR'!
- contents
- ^ self stream contents
- currentClass
- ^ currentClass
- currentClass: aClass
- currentClass := aClass
- stream
- ^ stream
- stream: aStream
- stream := aStream
- initialize
- super initialize.
- stream := JSStream new.
- visitIRAssignment: anIRAssignment
- self stream
- nextPutAssignLhs: [self visit: anIRAssignment left]
- rhs: [self visit: anIRAssignment right].
- visitIRClosure: anIRClosure
- self stream
- nextPutClosureWith: [
- self stream nextPutVars: (anIRClosure tempDeclarations collect: [ :each |
- each name asVariableName ]).
- self stream
- nextPutBlockContextFor: anIRClosure
- during: [ super visitIRClosure: anIRClosure ] ]
- arguments: anIRClosure arguments
- visitIRDynamicArray: anIRDynamicArray
- self
- visitInstructionList: anIRDynamicArray dagChildren
- enclosedBetween: '[' and: ']'
- visitIRDynamicDictionary: anIRDynamicDictionary
- self
- visitInstructionList: anIRDynamicDictionary dagChildren
- enclosedBetween: '$globals.HashedCollection._newFromPairs_([' and: '])'
- visitIRMethod: anIRMethod
- self stream
- nextPutFunctionWith: [
- self stream nextPutVars: (anIRMethod tempDeclarations collect: [ :each |
- each name asVariableName ]).
- self stream nextPutContextFor: anIRMethod during: [
- anIRMethod internalVariables ifNotEmpty: [ :internalVars |
- self stream nextPutVars:
- (internalVars collect: [ :each | each variable alias ]) asSet ].
- anIRMethod scope hasNonLocalReturn
- ifTrue: [
- self stream nextPutNonLocalReturnHandlingWith: [
- super visitIRMethod: anIRMethod ] ]
- ifFalse: [ super visitIRMethod: anIRMethod ] ]]
- arguments: anIRMethod arguments.
-
- ^ anIRMethod compiledSource: self contents; yourself
- visitIRNonLocalReturn: anIRNonLocalReturn
- self stream nextPutNonLocalReturnWith: [
- super visitIRNonLocalReturn: anIRNonLocalReturn ]
- visitIRReturn: anIRReturn
- self stream nextPutReturnWith: [
- super visitIRReturn: anIRReturn ]
- visitIRSend: anIRSend
- | sends superclass |
- sends := (anIRSend method sendIndexes at: anIRSend selector) size.
-
- anIRSend receiver isSuper
- ifTrue: [ self visitSuperSend: anIRSend ]
- ifFalse: [ self visitSend: anIRSend ].
-
- anIRSend index < sends
- ifTrue: [ self stream nextPutSendIndexFor: anIRSend ]
- visitIRSequence: anIRSequence
- anIRSequence dagChildren do: [ :each |
- self stream nextPutStatementWith: [ self visit: each ] ]
- visitIRTempDeclaration: anIRTempDeclaration
- "self stream
- nextPutAll: 'var ', anIRTempDeclaration name asVariableName, ';';
- lf"
- visitIRValue: anIRValue
- self stream nextPutAll: anIRValue value asJavaScriptSource
- visitIRVariable: anIRVariable
- self stream nextPutAll: anIRVariable variable alias
- visitIRVerbatim: anIRVerbatim
- self stream nextPutAll: anIRVerbatim source
- visitInstructionList: anArray enclosedBetween: aString and: anotherString
- self stream nextPutAll: aString.
- anArray
- do: [ :each | self visit: each ]
- separatedBy: [ self stream nextPutAll: ',' ].
- stream nextPutAll: anotherString
- visitReceiver: anIRInstruction
- | instr |
- anIRInstruction isSelf
- ifTrue: [ instr := anIRInstruction copy
- variable: (anIRInstruction variable copy name: '$self'; yourself);
- yourself ]
- ifFalse: [ instr := anIRInstruction ].
-
- instr needsBoxingAsReceiver ifFalse: [ ^ self visit: instr ].
-
- self stream nextPutAll: '$recv('.
- self visit: instr.
- self stream nextPutAll: ')'
- visitSend: anIRSend
- self visitReceiver: anIRSend receiver.
- self stream nextPutAll: '.', anIRSend javaScriptSelector.
- self
- visitInstructionList: anIRSend arguments
- enclosedBetween: '(' and: ')'
- visitSuperSend: anIRSend
- self stream nextPutSupercallFor: anIRSend with: [
- self stream
- nextPutAll: anIRSend receiver variable lookupAsJavaScriptSource, '.';
- nextPutAll: anIRSend javaScriptSelector, '.call'.
- self
- visitInstructionList: {anIRSend receiver}, anIRSend arguments
- enclosedBetween: '(' and: ')' ]
- Object subclass: #JSStream
- slots: {#stream. #omitSemicolon}
- package: 'Compiler-IR'!
- contents
- ^ stream contents
- omitSemicolon
- ^ omitSemicolon
- omitSemicolon: aBoolean
- omitSemicolon := aBoolean
- initialize
- super initialize.
- stream := '' writeStream.
- lf
- stream lf
- nextPut: aString
- stream nextPut: aString
- nextPutAll: aString
- stream nextPutAll: aString
- nextPutAssignLhs: aBlock rhs: anotherBlock
- aBlock value.
- stream nextPutAll: '='.
- anotherBlock value
- nextPutBlockContextFor: anIRClosure during: aBlock
- anIRClosure requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
- self
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
- lf;
- nextPutAll: 'return $core.withContext(function(', anIRClosure scope alias, ') {';
- lf;
- nextPutAll: '//>>excludeEnd("ctx");';
- lf.
-
- aBlock value.
-
- self
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
- lf;
- nextPutAll: '}, function(', anIRClosure scope alias, ') {';
- nextPutAll: anIRClosure scope alias, '.fillBlock({'.
-
- anIRClosure locals
- do: [ :each |
- self
- nextPutAll: each asVariableName;
- nextPutAll: ':';
- nextPutAll: each asVariableName ]
- separatedBy: [ self nextPutAll: ',' ].
-
- self
- nextPutAll: '},';
- nextPutAll: anIRClosure scope outerScope alias, ',', anIRClosure scope blockIndex asString, ')});';
- lf;
- nextPutAll: '//>>excludeEnd("ctx");'
- nextPutClosureWith: aBlock arguments: anArray
- stream nextPutAll: '(function('.
- anArray
- do: [ :each | stream nextPutAll: each asVariableName ]
- separatedBy: [ stream nextPut: ',' ].
- stream nextPutAll: '){'; lf.
- aBlock value.
- stream lf; nextPutAll: '})'
- nextPutContextFor: aMethod during: aBlock
- aMethod requiresSmalltalkContext ifFalse: [ ^ aBlock value ].
-
- self
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
- lf;
- nextPutAll: 'return $core.withContext(function(', aMethod scope alias, ') {';
- lf;
- nextPutAll: '//>>excludeEnd("ctx");';
- lf.
- aBlock value.
-
- self
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);';
- lf;
- nextPutAll: '}, function(', aMethod scope alias, ') {', aMethod scope alias;
- nextPutAll: '.fill(self,', aMethod selector asJavaScriptSource, ',{'.
- aMethod locals
- do: [ :each |
- self
- nextPutAll: each asVariableName;
- nextPutAll: ':';
- nextPutAll: each asVariableName ]
- separatedBy: [ self nextPutAll: ',' ].
-
- self
- nextPutAll: '})});';
- lf;
- nextPutAll: '//>>excludeEnd("ctx");'
- nextPutFunctionWith: aBlock arguments: anArray
- stream nextPutAll: 'function ('.
- anArray
- do: [ :each | stream nextPutAll: each asVariableName ]
- separatedBy: [ stream nextPut: ',' ].
- stream nextPutAll: '){'; lf.
- stream nextPutAll: 'var self=this,$self=this;'; lf.
- aBlock value.
- stream lf; nextPutAll: '}'
- nextPutIf: aBlock then: anotherBlock
- stream nextPutAll: 'if('.
- aBlock value.
- stream nextPutAll: '){'; lf.
- anotherBlock value.
- stream nextPutAll: '}'.
- self omitSemicolon: true
- nextPutIf: aBlock then: ifBlock else: elseBlock
- stream nextPutAll: 'if('.
- aBlock value.
- stream nextPutAll: '){'; lf.
- ifBlock value.
- stream nextPutAll: '} else {'; lf.
- elseBlock value.
- stream nextPutAll: '}'.
- self omitSemicolon: true
- nextPutNonLocalReturnHandlingWith: aBlock
- stream
- nextPutAll: 'var $early={};'; lf;
- nextPutAll: 'try {'; lf.
- aBlock value.
- stream
- nextPutAll: '}'; lf;
- nextPutAll: 'catch(e) {if(e===$early)return e[0]; throw e}'; lf
- nextPutNonLocalReturnWith: aBlock
- stream nextPutAll: 'throw $early=['.
- aBlock value.
- stream nextPutAll: ']'
- nextPutReturnWith: aBlock
- stream nextPutAll: 'return '.
- aBlock value
- nextPutSendIndexFor: anIRSend
- self
- nextPutAll: ';'; lf;
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
- nextPutAll: anIRSend scope alias;
- nextPutAll: '.sendIdx[';
- nextPutAll: anIRSend selector asJavaScriptSource;
- nextPutAll: ']=';
- nextPutAll: anIRSend index asString;
- nextPutAll: ';'; lf;
- nextPutAll: '//>>excludeEnd("ctx")'
- nextPutStatementWith: aBlock
- self omitSemicolon: false.
- aBlock value.
- self omitSemicolon ifFalse: [ stream nextPutAll: ';' ].
- self omitSemicolon: false.
- stream lf
- nextPutSupercallFor: anIRSend with: aBlock
- self
- nextPutAll: '('; lf;
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
- nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
- nextPutAll: '//>>excludeEnd("ctx");'; lf.
- aBlock value.
- self
- nextPutAll: ');'; lf;
- nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
- nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
- nextPutAll: '//>>excludeEnd("ctx");'
- nextPutVars: aCollection
- aCollection ifNotEmpty: [
- stream nextPutAll: 'var '.
- aCollection
- do: [ :each | stream nextPutAll: each ]
- separatedBy: [ stream nextPutAll: ',' ].
- stream nextPutAll: ';'; lf ]
- IRPragmator setTraitComposition: {TPragmator} asTraitComposition!
- isReferenced
- "Answer true if the receiver is referenced by other nodes.
- Do not take sequences or assignments into account"
-
- self parent isSequenceNode ifTrue: [ ^ false ].
- self parent isAssignmentNode ifTrue: [ ^ false ].
- self parent isCascadeNode ifTrue: [ ^ self parent isReferenced ].
-
- ^ true
- subtreeNeedsAliasing
- ^ self shouldBeAliased or: [
- self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
- shouldBeAliased
- ^ super shouldBeAliased or: [ self isReferenced ]
- appendToInstruction: anIRInstruction
- anIRInstruction appendBlock: self
- subtreeNeedsAliasing
- ^ self shouldBeAliased
- subtreeNeedsAliasing
- ^ self parent isSequenceNode not
- shouldBeAliased
- "Because we keep track of send indexes, some send nodes need additional care for aliasing.
- See IRJSVisitor >> visitIRSend:"
-
- | sends |
-
- super shouldBeAliased ifTrue: [ ^ true ].
- self isReferenced ifFalse: [ ^ false ].
- self superSend ifTrue: [ ^ true ].
- sends := (self method sendIndexes at: self selector) size.
- ^ self index < sends
|