forked from pharo-project/pharo
/
OCAbstractMethodScope.class.st
290 lines (234 loc) · 7.46 KB
/
OCAbstractMethodScope.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"
I am an abstract superclass for Block and Method scopes
"
Class {
#name : #OCAbstractMethodScope,
#superclass : #OCAbstractScope,
#instVars : [
'thisContextVar',
'tempVars',
'copiedVars2',
'tempVector',
'id',
'tempVectorVar'
],
#category : #'OpalCompiler-Core-Semantics'
}
{ #category : #'temp vars - copying' }
OCAbstractMethodScope >> addCopyingTemp: aTempVar [
^copiedVars2 at: aTempVar name asSymbol put: (OCCopyingTempVariable new
originalVar: aTempVar originalVar;
name: aTempVar name;
escaping: aTempVar escaping;
usage: aTempVar usage;
scope: self;
yourself)
]
{ #category : #'temp vars - copying' }
OCAbstractMethodScope >> addCopyingTempToAllScopesUpToDefTemp: aVar [
(self hasCopyingTempNamed: aVar name) ifFalse: [self addCopyingTemp: aVar].
self = aVar scope ifTrue: [^ self].
^ self outerScope addCopyingTempToAllScopesUpToDefTemp: aVar.
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> addTemp: name [
^self addTemp: OCTempVariable new withName: name
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> addTemp: aTempVariable withName: aString [
^ tempVars
at: aString
put: (aTempVariable
name: aString;
scope: self;
yourself)
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> addVectorTemp: aTemp [
^ tempVector
at: aTemp name
put: (OCVectorTempVariable new
name: aTemp name;
vectorName: self tempVectorName;
scope: self;
usage: aTemp usage;
escaping: aTemp escaping;
yourself)
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> allTempNames [
^self allTemps collect: [: each | each name]
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> allTemps [
"return all temps defined, even the ones in the outer scope that are not used in the current.
This includes the arguments. We do not need to care about shadowed temps as temp shadowing is not allowed."
^ Array streamContents: [ :str |
self outerScope allTemps
do: [ :var |
(self localTempNames includes: var name)
ifFalse: [ str nextPut: var ] ].
str nextPutAll: self localTemps ]
]
{ #category : #'temp vars - copying' }
OCAbstractMethodScope >> copiedVarNames [
^ copiedVars2 keys
]
{ #category : #'temp vars - copying' }
OCAbstractMethodScope >> copiedVars [
^ copiedVars2
]
{ #category : #lookup }
OCAbstractMethodScope >> hasBindingThatBeginsWith: aString [
"check weather there are any temporaries defined that start with aString"
(copiedVars2 hasBindingThatBeginsWith: aString) ifTrue: [ ^true ].
(tempVector hasBindingThatBeginsWith: aString) ifTrue: [ ^true ].
(tempVars hasBindingThatBeginsWith: aString) ifTrue: [ ^true ].
(thisContextVar name beginsWith: aString) ifTrue: [ ^true ].
^ self outerScope hasBindingThatBeginsWith: aString
]
{ #category : #'temp vars - copying' }
OCAbstractMethodScope >> hasCopyingTempNamed: name [
^copiedVars2 values anySatisfy: [:each | each name = name]
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> hasTempVector [
^ tempVector isNotEmpty
]
{ #category : #accessing }
OCAbstractMethodScope >> id: int [
id := int
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> indexFromIRForVarNamed: aName [
^self outerNotOptimizedScope node irInstruction indexForVarNamed: aName
]
{ #category : #initialization }
OCAbstractMethodScope >> initialize [
tempVars := OrderedDictionary new.
tempVector := Dictionary new.
copiedVars2 := Dictionary new.
id := 0.
thisContextVar := OCThisContextVariable new
scope: self; yourself.
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> localTempNames [
^self localTemps collect: [:each | each name]
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> localTemps [
"all temps accessed in the context... for tempVectors, it takes all the vars even those not used here"
^ Array streamContents: [ :str |
copiedVars2 valuesDo: [ :var |
var isStoringTempVector
ifTrue: [ var originalVar scope tempVector
do: [ :tempVectorVars | str nextPut: tempVectorVars ] ] ].
str nextPutAll: tempVars values]
]
{ #category : #lookup }
OCAbstractMethodScope >> lookupDefiningContextForVariable: var startingFrom: aContext [
"Is this the definition context for var? If it not, we look in the outer context using the corresponding outer scope. If found, we return the context"
^self = var scope
ifFalse: [ self outerScope lookupDefiningContextForVariable: var startingFrom: (self nextOuterScopeContextOf: aContext) ]
ifTrue: [ aContext ]
]
{ #category : #lookup }
OCAbstractMethodScope >> lookupVar: name [
copiedVars2 at: name ifPresent: [:v | ^ v].
tempVector at: name ifPresent: [:v | ^ v].
tempVars at: name ifPresent: [:v | ^ v].
name = thisContextVar name ifTrue: [^ thisContextVar].
name = self tempVectorName ifTrue: [ ^ self tempVectorVar ].
^self outerScope lookupVar: name
]
{ #category : #lookup }
OCAbstractMethodScope >> lookupVarForDeclaration: name [
"This is a version of #lookupVar: that skips the OCRequestorScope"
tempVars at: name ifPresent: [:v | ^ v].
name = thisContextVar name ifTrue: [^ thisContextVar].
^self outerScope lookupVarForDeclaration: name
]
{ #category : #scope }
OCAbstractMethodScope >> methodScope [
^ self outerScope methodScope
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> moveToVectorTemp: aTempVar [
self addVectorTemp: aTempVar.
self removeTemp: aTempVar.
]
{ #category : #scope }
OCAbstractMethodScope >> newBlockScope: int [
^ OCBlockScope new outerScope: self; id: int; yourself.
]
{ #category : #scope }
OCAbstractMethodScope >> newOptimizedBlockScope: int [
^ OCOptimizedBlockScope new
outerScope: self;
id: int;
yourself.
]
{ #category : #lookup }
OCAbstractMethodScope >> nextOuterScopeContextOf: aContext [
^aContext
]
{ #category : #scope }
OCAbstractMethodScope >> outerNotOptimizedScope [
^self
]
{ #category : #scope }
OCAbstractMethodScope >> popScope [
"Propogate free var usages to their outer vars, then return outer scope"
^ self outerScope
]
{ #category : #printing }
OCAbstractMethodScope >> printOn: stream [
super printOn: stream.
stream space.
self scopeLevel printOn: stream.
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> removeTemp: tempVar [
tempVars removeKey: tempVar name
]
{ #category : #'temp vars - copying' }
OCAbstractMethodScope >> setCopyingTempToAllScopesUpToDefTemp: aVar to: aValue from: aContext [
"we need to update all the copies if we change the value of a copied temp"
self = aVar scope
ifTrue: [ ^ aVar writeFromLocalContext: aContext put: aValue ].
(self lookupVar: aVar name) writeFromLocalContext: aContext put: aValue.
^self outerScope
setCopyingTempToAllScopesUpToDefTemp: aVar
to: aValue
from: (self nextOuterScopeContextOf: aContext)
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> tempVarNames [
^ tempVars keys
]
{ #category : #'temp vars' }
OCAbstractMethodScope >> tempVars [
^ tempVars
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> tempVector [
^ tempVector
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> tempVectorName [
"the name of the tempVector is not a valid name of a temp variable
This way we avoid name clashes "
^'0vector', id asString
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> tempVectorVar [
^ tempVectorVar ifNil: [ tempVectorVar := OCTempVariable new
name: self tempVectorName;
scope: self;
yourself]
]
{ #category : #'temp vars - vector' }
OCAbstractMethodScope >> tempVectorVarNames [
^ tempVector collect: [:each| each name] as: Array
]