| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 | 
							- Smalltalk createPackage: 'Compiler-IR'!
 
- NodeVisitor subclass: #IRASTTranslator
 
- 	instanceVariableNames: 'source theClass method sequence nextAlias'
 
- 	package: 'Compiler-IR'!
 
- !IRASTTranslator commentStamp!
 
- I am the AST (abstract syntax tree) visitor responsible for building the intermediate representation graph.!
 
- !IRASTTranslator methodsFor: 'accessing'!
 
- 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
 
- ! !
 
- !IRASTTranslator methodsFor: 'visiting'!
 
- alias: aNode
 
- 	| variable |
 
- 	aNode isImmutable ifTrue: [ ^ self visit: aNode ].
 
- 	variable := IRVariable new
 
- 		variable: (AliasVar new name: '$', self nextAlias);
 
- 		yourself.
 
- 	self sequence add: (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 result |
 
- 	threshold := 0.
 
- 	
 
- 	aCollection withIndexDo: [ :each :i |
 
- 		each subtreeNeedsAliasing
 
- 			ifTrue: [ threshold := i ] ].
 
- 	result := OrderedCollection new.
 
- 	aCollection withIndexDo: [ :each :i |
 
- 		result add: (i <= threshold
 
- 			ifTrue: [ self alias: each ]
 
- 			ifFalse: [ self visit: each ]) ].
 
- 	^ result
 
- !
 
- visitAssignmentNode: aNode
 
- 	| left right assignment |
 
- 	right := self visit: aNode right.
 
- 	left := self visit: aNode left.
 
- 	self sequence add: (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 sequence add: (self visitOrAlias: each) ].
 
- 				aNode dagChildren last isReturnNode
 
- 					ifFalse: [ self sequence add: (IRBlockReturn new add: (self visitOrAlias: aNode dagChildren last); yourself) ]
 
- 					ifTrue: [ self sequence add: (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 sequence add: (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;
 
- 		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) ].
 
- 	aNode dagChildren do: [ :each | self method add: (self visit: each) ].
 
- 	aNode scope hasLocalReturn ifFalse: [self method
 
- 		add: (IRReturn new
 
- 			add: (IRVariable new
 
- 				variable: (aNode scope pseudoVars at: 'self');
 
- 				yourself);
 
- 			yourself);
 
- 		add: (IRVerbatim new source: ''; 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 | | instruction |
 
- 				instruction := self visitOrAlias: each.
 
- 				instruction isVariable ifFalse: [
 
- 					self sequence add: instruction ] ]]
 
- !
 
- visitValueNode: aNode
 
- 	^ IRValue new
 
- 		value: aNode value;
 
- 		yourself
 
- !
 
- visitVariableNode: aNode
 
- 	^ IRVariable new
 
- 		variable: aNode binding;
 
- 		yourself
 
- ! !
 
- DagParentNode subclass: #IRInstruction
 
- 	instanceVariableNames: 'parent'
 
- 	package: 'Compiler-IR'!
 
- !IRInstruction commentStamp!
 
- 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.!
 
- !IRInstruction methodsFor: 'accessing'!
 
- method
 
- 	^ self parent method
 
- !
 
- parent
 
- 	^ parent
 
- !
 
- parent: anIRInstruction
 
- 	parent := anIRInstruction
 
- !
 
- scope
 
- 	^ self parent ifNotNil: [ :node | 
 
- 		node scope ]
 
- ! !
 
- !IRInstruction methodsFor: 'building'!
 
- add: anObject
 
- 	anObject parent: self.
 
- 	^ self dagChildren add: 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
 
- ! !
 
- !IRInstruction methodsFor: 'testing'!
 
- isClosure
 
- 	^ false
 
- !
 
- isInlined
 
- 	^ false
 
- !
 
- isMethod
 
- 	^ false
 
- !
 
- isSend
 
- 	^ false
 
- !
 
- isSequence
 
- 	^ false
 
- !
 
- isTempDeclaration
 
- 	^ false
 
- !
 
- isVariable
 
- 	^ false
 
- !
 
- needsBoxingAsReceiver
 
- 	^ true
 
- !
 
- yieldsValue
 
- 	^ true
 
- ! !
 
- !IRInstruction class methodsFor: 'instance creation'!
 
- on: aBuilder
 
- 	^ self new
 
- 		builder: aBuilder;
 
- 		yourself
 
- ! !
 
- IRInstruction subclass: #IRAssignment
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRAssignment methodsFor: 'accessing'!
 
- left
 
- 	^ self dagChildren first
 
- !
 
- right
 
- 	^ self dagChildren last
 
- ! !
 
- !IRAssignment methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRAssignment: self
 
- ! !
 
- IRInstruction subclass: #IRDynamicArray
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRDynamicArray methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRDynamicArray: self
 
- ! !
 
- IRInstruction subclass: #IRDynamicDictionary
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRDynamicDictionary methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRDynamicDictionary: self
 
- ! !
 
- IRInstruction subclass: #IRScopedInstruction
 
- 	instanceVariableNames: 'scope'
 
- 	package: 'Compiler-IR'!
 
- !IRScopedInstruction methodsFor: 'accessing'!
 
- scope
 
- 	^ scope
 
- !
 
- scope: aScope
 
- 	scope := aScope
 
- ! !
 
- IRScopedInstruction subclass: #IRClosureInstruction
 
- 	instanceVariableNames: 'arguments requiresSmalltalkContext'
 
- 	package: 'Compiler-IR'!
 
- !IRClosureInstruction methodsFor: 'accessing'!
 
- arguments
 
- 	^ arguments ifNil: [ #() ]
 
- !
 
- arguments: aCollection
 
- 	arguments := aCollection
 
- !
 
- locals
 
- 	^ self arguments copy
 
- 		addAll: (self tempDeclarations collect: [ :each | each name ]);
 
- 		yourself
 
- !
 
- 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
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRClosure methodsFor: 'accessing'!
 
- sequence
 
- 	^ self dagChildren last
 
- ! !
 
- !IRClosure methodsFor: 'testing'!
 
- isClosure
 
- 	^ true
 
- ! !
 
- !IRClosure methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRClosure: self
 
- ! !
 
- IRClosureInstruction subclass: #IRMethod
 
- 	instanceVariableNames: 'theClass source selector classReferences sendIndexes requiresSmalltalkContext internalVariables'
 
- 	package: 'Compiler-IR'!
 
- !IRMethod commentStamp!
 
- I am a method instruction!
 
- !IRMethod methodsFor: 'accessing'!
 
- classReferences
 
- 	^ classReferences
 
- !
 
- classReferences: aCollection
 
- 	classReferences := aCollection
 
- !
 
- internalVariables
 
- 	^ internalVariables ifNil: [ internalVariables := Set new ]
 
- !
 
- messageSends
 
- 	^ self sendIndexes keys
 
- !
 
- method
 
- 	^ self
 
- !
 
- selector
 
- 	^ selector
 
- !
 
- selector: aString
 
- 	selector := aString
 
- !
 
- sendIndexes
 
- 	^ sendIndexes
 
- !
 
- sendIndexes: aDictionary
 
- 	sendIndexes := aDictionary
 
- !
 
- source
 
- 	^ source
 
- !
 
- source: aString
 
- 	source := aString
 
- !
 
- theClass
 
- 	^ theClass
 
- !
 
- theClass: aClass
 
- 	theClass := aClass
 
- ! !
 
- !IRMethod methodsFor: 'testing'!
 
- isMethod
 
- 	^ true
 
- ! !
 
- !IRMethod methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRMethod: self
 
- ! !
 
- IRScopedInstruction subclass: #IRReturn
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRReturn commentStamp!
 
- I am a local return instruction.!
 
- !IRReturn methodsFor: 'accessing'!
 
- expression
 
- 	^ self dagChildren single
 
- !
 
- scope
 
- 	^ scope ifNil: [ self parent scope ]
 
- ! !
 
- !IRReturn methodsFor: 'testing'!
 
- yieldsValue
 
- 	^ false
 
- ! !
 
- !IRReturn methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRReturn: self
 
- ! !
 
- IRReturn subclass: #IRBlockReturn
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRBlockReturn commentStamp!
 
- Smalltalk blocks return their last statement. I am a implicit block return instruction.!
 
- !IRBlockReturn methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRBlockReturn: self
 
- ! !
 
- IRReturn subclass: #IRNonLocalReturn
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRNonLocalReturn commentStamp!
 
- I am a non local return instruction.
 
- Non local returns are handled using a try/catch JavaScript statement.
 
- See `IRNonLocalReturnHandling` class.!
 
- !IRNonLocalReturn methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRNonLocalReturn: self
 
- ! !
 
- IRScopedInstruction subclass: #IRTempDeclaration
 
- 	instanceVariableNames: 'name'
 
- 	package: 'Compiler-IR'!
 
- !IRTempDeclaration methodsFor: 'accessing'!
 
- name
 
- 	^ name
 
- !
 
- name: aString
 
- 	name := aString
 
- ! !
 
- !IRTempDeclaration methodsFor: 'testing'!
 
- isTempDeclaration
 
- 	^ true
 
- ! !
 
- !IRTempDeclaration methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRTempDeclaration: self
 
- ! !
 
- IRInstruction subclass: #IRSend
 
- 	instanceVariableNames: 'selector index'
 
- 	package: 'Compiler-IR'!
 
- !IRSend commentStamp!
 
- I am a message send instruction.!
 
- !IRSend methodsFor: 'accessing'!
 
- arguments
 
- 	^ self dagChildren allButFirst
 
- !
 
- index
 
- 	^ index
 
- !
 
- index: anInteger
 
- 	index := anInteger
 
- !
 
- receiver
 
- 	^ self dagChildren first
 
- !
 
- selector
 
- 	^ selector
 
- !
 
- selector: aString
 
- 	selector := aString
 
- ! !
 
- !IRSend methodsFor: 'testing'!
 
- isSend
 
- 	^ true
 
- !
 
- isSuperSend
 
- 	| receiver |
 
- 	receiver := self receiver.
 
- 	^ receiver isVariable and: [ receiver variable name = 'super' ]
 
- ! !
 
- !IRSend methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRSend: self
 
- ! !
 
- IRInstruction subclass: #IRSequence
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRSequence methodsFor: 'testing'!
 
- isSequence
 
- 	^ true
 
- ! !
 
- !IRSequence methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRSequence: self
 
- ! !
 
- IRSequence subclass: #IRBlockSequence
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRBlockSequence methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRBlockSequence: self
 
- ! !
 
- IRInstruction subclass: #IRValue
 
- 	instanceVariableNames: 'value'
 
- 	package: 'Compiler-IR'!
 
- !IRValue commentStamp!
 
- I am the simplest possible instruction. I represent a value.!
 
- !IRValue methodsFor: 'accessing'!
 
- value
 
- 	^ value
 
- !
 
- value: aString
 
- 	value := aString
 
- ! !
 
- !IRValue methodsFor: 'testing'!
 
- needsBoxingAsReceiver
 
- 	^ false
 
- ! !
 
- !IRValue methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRValue: self
 
- ! !
 
- IRInstruction subclass: #IRVariable
 
- 	instanceVariableNames: 'variable'
 
- 	package: 'Compiler-IR'!
 
- !IRVariable commentStamp!
 
- I am a variable instruction.!
 
- !IRVariable methodsFor: 'accessing'!
 
- variable
 
- 	^ variable
 
- !
 
- variable: aScopeVariable
 
- 	variable := aScopeVariable
 
- ! !
 
- !IRVariable methodsFor: 'testing'!
 
- isVariable
 
- 	^ true
 
- !
 
- needsBoxingAsReceiver
 
- 	^ self variable isPseudoVar not
 
- ! !
 
- !IRVariable methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRVariable: self
 
- ! !
 
- IRInstruction subclass: #IRVerbatim
 
- 	instanceVariableNames: 'source'
 
- 	package: 'Compiler-IR'!
 
- !IRVerbatim methodsFor: 'accessing'!
 
- source
 
- 	^ source
 
- !
 
- source: aString
 
- 	source := aString
 
- ! !
 
- !IRVerbatim methodsFor: 'visiting'!
 
- acceptDagVisitor: aVisitor
 
- 	^ aVisitor visitIRVerbatim: self
 
- ! !
 
- ParentFakingPathDagVisitor subclass: #IRVisitor
 
- 	instanceVariableNames: ''
 
- 	package: 'Compiler-IR'!
 
- !IRVisitor methodsFor: 'visiting'!
 
- 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
 
- 	instanceVariableNames: 'stream currentClass'
 
- 	package: 'Compiler-IR'!
 
- !IRJSTranslator methodsFor: 'accessing'!
 
- contents
 
- 	^ self stream contents
 
- !
 
- currentClass
 
- 	^ currentClass
 
- !
 
- currentClass: aClass
 
- 	currentClass := aClass
 
- !
 
- stream
 
- 	^ stream
 
- !
 
- stream: aStream
 
- 	stream := aStream
 
- ! !
 
- !IRJSTranslator methodsFor: 'initialization'!
 
- initialize
 
- 	super initialize.
 
- 	stream := JSStream new.
 
- ! !
 
- !IRJSTranslator methodsFor: 'visiting'!
 
- 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
 
- 		nextPutMethodDeclaration: anIRMethod
 
- 		with: [ 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 asSet collect: [ :each | each variable alias ]) ].
 
- 				anIRMethod scope hasNonLocalReturn
 
- 					ifTrue: [
 
- 						self stream nextPutNonLocalReturnHandlingWith: [
 
- 							super visitIRMethod: anIRMethod ] ]
 
- 					ifFalse: [ super visitIRMethod: anIRMethod ] ]]
 
- 			arguments: anIRMethod arguments ].
 
- 	^ self contents
 
- !
 
- 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 isSuperSend
 
- 		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 asJavascript
 
- !
 
- visitIRVariable: anIRVariable
 
- 	anIRVariable variable name = 'thisContext'
 
- 		ifTrue: [ self stream nextPutAll: '$core.getThisContext()' ]
 
- 		ifFalse: [ self stream nextPutAll: anIRVariable variable alias ]
 
- !
 
- visitIRVerbatim: anIRVerbatim
 
- 	self stream nextPutStatementWith: [
 
- 		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
 
- 	anIRInstruction needsBoxingAsReceiver ifFalse: [ ^ self visit: anIRInstruction ].
 
- 	
 
- 	self stream nextPutAll: '$recv('.
 
- 	self visit: anIRInstruction.
 
- 	self stream nextPutAll: ')'
 
- !
 
- visitSend: anIRSend
 
- 	self visitReceiver: anIRSend receiver.
 
- 	self stream nextPutAll: '.', anIRSend selector asJavaScriptMethodName.
 
- 	self
 
- 		visitInstructionList: anIRSend arguments
 
- 		enclosedBetween: '(' and: ')'
 
- !
 
- visitSuperSend: anIRSend
 
- 	self stream
 
- 		nextPutAll: '('; lf;
 
- 		nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
 
- 		nextPutAll: anIRSend scope alias, '.supercall = true,'; lf;
 
- 		nextPutAll: '//>>excludeEnd("ctx");'; lf;
 
- 		nextPutAll: '(', self currentClass asJavascript;
 
- 		nextPutAll: '.superclass||$boot.nilAsClass).fn.prototype.';
 
- 		nextPutAll: anIRSend selector asJavaScriptMethodName, '.apply(';
 
- 		nextPutAll: '$recv(self), '.
 
- 	self
 
- 		visitInstructionList: anIRSend arguments
 
- 		enclosedBetween: '[' and: ']'.
 
- 	self stream 
 
- 		nextPutAll: '));'; lf;
 
- 		nextPutAll: '//>>excludeStart("ctx", pragmas.excludeDebugContexts);'; lf;
 
- 		nextPutAll: anIRSend scope alias, '.supercall = false;'; lf;
 
- 		nextPutAll: '//>>excludeEnd("ctx");'
 
- ! !
 
- Object subclass: #JSStream
 
- 	instanceVariableNames: 'stream omitSemicolon'
 
- 	package: 'Compiler-IR'!
 
- !JSStream methodsFor: 'accessing'!
 
- contents
 
- 	^ stream contents
 
- !
 
- omitSemicolon
 
- 	^ omitSemicolon
 
- !
 
- omitSemicolon: aBoolean
 
- 	omitSemicolon := aBoolean
 
- ! !
 
- !JSStream methodsFor: 'initialization'!
 
- initialize
 
- 	super initialize.
 
- 	stream := '' writeStream.
 
- ! !
 
- !JSStream methodsFor: 'streaming'!
 
- 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 asJavascript, ',{'.
 
- 	aMethod locals
 
- 		do: [ :each |
 
- 			self
 
- 				nextPutAll: each asVariableName;
 
- 				nextPutAll: ':';
 
- 				nextPutAll: each asVariableName ]
 
- 		separatedBy: [ self nextPutAll: ',' ].
 
- 	
 
- 	self
 
- 		nextPutAll: '},';
 
- 		nextPutAll: aMethod theClass asJavascript;
 
- 		nextPutAll: ')});';
 
- 		lf;
 
- 		nextPutAll: '//>>excludeEnd("ctx");'
 
- !
 
- nextPutFunctionWith: aBlock arguments: anArray
 
- 	stream nextPutAll: 'fn: function('.
 
- 	anArray
 
- 		do: [ :each | stream nextPutAll: each asVariableName ]
 
- 		separatedBy: [ stream nextPut: ',' ].
 
- 	stream nextPutAll: '){'; lf.
 
- 	stream nextPutAll: 'var 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
 
- !
 
- nextPutMethodDeclaration: aMethod with: aBlock
 
- 	stream
 
- 		nextPutAll: '$core.method({'; lf;
 
- 		nextPutAll: 'selector: ', aMethod selector asJavascript, ','; lf;
 
- 		nextPutAll: 'source: ', aMethod source asJavascript, ',';lf.
 
- 	aBlock value.
 
- 	stream
 
- 		nextPutAll: ',', String lf, 'messageSends: ';
 
- 		nextPutAll: aMethod messageSends asArray asJavascript, ','; lf;
 
- 		nextPutAll: 'args: ', (aMethod arguments collect: [ :each | each value ]) asArray asJavascript, ','; lf;
 
- 		nextPutAll: 'referencedClasses: ['.
 
- 	aMethod classReferences
 
- 		do: [ :each | stream nextPutAll: each asJavascript ]
 
- 		separatedBy: [ stream nextPutAll: ',' ].
 
- 	stream
 
- 		nextPutAll: ']';
 
- 		nextPutAll: '})'
 
- !
 
- 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 asJavascript;
 
- 		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
 
- !
 
- nextPutVars: aCollection
 
- 	aCollection ifNotEmpty: [
 
- 		stream nextPutAll: 'var '.
 
- 		aCollection
 
- 			do: [ :each | stream nextPutAll: each ]
 
- 			separatedBy: [ stream nextPutAll: ',' ].
 
- 		stream nextPutAll: ';'; lf ]
 
- ! !
 
- !ASTNode methodsFor: '*Compiler-IR'!
 
- isReferenced
 
- 	"Answer true if the receiver is referenced by other nodes.
 
- 	Do not take sequences or assignments into account"
 
- 	
 
- 	^ (self parent isSequenceNode or: [
 
- 		self parent isAssignmentNode ]) not
 
- !
 
- subtreeNeedsAliasing
 
- 	^ self shouldBeAliased or: [
 
- 		self dagChildren anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
 
- ! !
 
- !AssignmentNode methodsFor: '*Compiler-IR'!
 
- shouldBeAliased
 
- 	^ super shouldBeAliased or: [ self isReferenced ]
 
- ! !
 
- !BlockClosure methodsFor: '*Compiler-IR'!
 
- appendToInstruction: anIRInstruction
 
- 	anIRInstruction appendBlock: self
 
- ! !
 
- !BlockNode methodsFor: '*Compiler-IR'!
 
- subtreeNeedsAliasing
 
- 	^ self shouldBeAliased
 
- ! !
 
- !CascadeNode methodsFor: '*Compiler-IR'!
 
- subtreeNeedsAliasing
 
- 	^ self parent isSequenceNode not
 
- ! !
 
- !SendNode methodsFor: '*Compiler-IR'!
 
- shouldBeAliased
 
- 	"Because we keep track of send indexes, some send nodes need additional care for aliasing. 
 
- 	See IRJSVisitor >> visitIRSend:"
 
- 	
 
- 	| sends |
 
- 	
 
- 	sends := (self method sendIndexes at: self selector) size.
 
- 	
 
- 	^ (super shouldBeAliased or: [
 
- 		self isReferenced and: [
 
- 			self index < sends or: [
 
- 				self superSend ] ] ])
 
- !
 
- subtreeNeedsAliasing
 
- 	^ self shouldBeInlined or: [ super subtreeNeedsAliasing ]
 
- ! !
 
 
  |