14
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
53
|
3 |
All Rights Reserved
|
14
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
PrimaryNode subclass:#VariableNode
|
140
|
14 |
instanceVariableNames:'name token selfValue selfClass index block'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Compiler-Support'
|
14
|
18 |
!
|
|
19 |
|
20
|
20 |
!VariableNode class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
53
|
25 |
All Rights Reserved
|
20
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
|
36 |
documentation
|
|
37 |
"
|
|
38 |
node for parse-trees, representing variables
|
|
39 |
"
|
140
|
40 |
!
|
|
41 |
|
|
42 |
version
|
|
43 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/VarNode.st,v 1.13 1995-11-23 02:15:59 cg Exp $'
|
20
|
44 |
! !
|
14
|
45 |
|
|
46 |
!VariableNode class methodsFor:'instance creation'!
|
|
47 |
|
98
|
48 |
type:t class:class name:n
|
|
49 |
^ (self basicNew) type:t class:class name:n
|
|
50 |
!
|
|
51 |
|
14
|
52 |
type:t index:i selfValue:s
|
|
53 |
^ (self basicNew) type:t index:i selfValue:s
|
|
54 |
!
|
|
55 |
|
140
|
56 |
type:t name:n
|
|
57 |
^ (self basicNew) type:t name:n
|
|
58 |
!
|
|
59 |
|
|
60 |
type:t name:n index:i selfClass:s
|
|
61 |
^ (self basicNew) type:t name:n index:i selfClass:s
|
|
62 |
!
|
|
63 |
|
|
64 |
type:t name:n index:i selfValue:s
|
|
65 |
^ (self basicNew) type:t name:n index:i selfValue:s
|
14
|
66 |
!
|
|
67 |
|
|
68 |
type:t name:n token:tok index:i
|
|
69 |
^ (self basicNew) type:t name:n token:tok index:i
|
|
70 |
!
|
|
71 |
|
140
|
72 |
type:t name:n token:tok index:i block:b
|
|
73 |
^ (self basicNew) type:t name:n token:tok index:i block:b
|
14
|
74 |
!
|
|
75 |
|
140
|
76 |
type:t name:n value:val
|
|
77 |
^ (self basicNew) type:t name:n value:val
|
14
|
78 |
!
|
|
79 |
|
140
|
80 |
type:t token:tok
|
|
81 |
^ (self basicNew) type:t token:tok
|
14
|
82 |
!
|
|
83 |
|
|
84 |
type:t token:tok index:i
|
|
85 |
^ (self basicNew) type:t token:tok index:i
|
|
86 |
!
|
|
87 |
|
|
88 |
type:t token:tok index:i block:b
|
|
89 |
^ (self basicNew) type:t token:tok index:i block:b
|
|
90 |
! !
|
|
91 |
|
|
92 |
!VariableNode methodsFor:'accessing'!
|
|
93 |
|
140
|
94 |
index
|
|
95 |
^ index
|
14
|
96 |
!
|
|
97 |
|
140
|
98 |
name
|
|
99 |
^ name
|
14
|
100 |
!
|
|
101 |
|
140
|
102 |
type:t class:class name:n
|
14
|
103 |
type := t.
|
|
104 |
value := nil.
|
140
|
105 |
name := n.
|
|
106 |
selfClass := class
|
14
|
107 |
!
|
|
108 |
|
|
109 |
type:t index:i selfValue:s
|
|
110 |
type := t.
|
|
111 |
value := nil.
|
|
112 |
index := i.
|
|
113 |
selfValue := s
|
|
114 |
!
|
|
115 |
|
140
|
116 |
type:t name:n
|
98
|
117 |
type := t.
|
|
118 |
value := nil.
|
14
|
119 |
name := n
|
|
120 |
!
|
|
121 |
|
|
122 |
type:t name:n index:i selfClass:s
|
|
123 |
type := t.
|
|
124 |
value := nil.
|
|
125 |
index := i.
|
|
126 |
selfClass := s.
|
|
127 |
name := n
|
|
128 |
!
|
|
129 |
|
140
|
130 |
type:t name:n index:i selfValue:s
|
|
131 |
type := t.
|
|
132 |
value := nil.
|
|
133 |
index := i.
|
|
134 |
selfValue := s.
|
|
135 |
name := n
|
|
136 |
!
|
|
137 |
|
14
|
138 |
type:t name:n token:tok index:i
|
|
139 |
type := t.
|
|
140 |
index := i.
|
|
141 |
token := tok.
|
|
142 |
name := n
|
|
143 |
!
|
|
144 |
|
|
145 |
type:t name:n token:tok index:i block:b
|
|
146 |
type := t.
|
|
147 |
index := i.
|
|
148 |
block := b.
|
|
149 |
token := tok.
|
|
150 |
name := n
|
|
151 |
!
|
|
152 |
|
140
|
153 |
type:t name:n value:val
|
|
154 |
type := t.
|
|
155 |
name := n.
|
|
156 |
value := val
|
|
157 |
!
|
|
158 |
|
|
159 |
type:t token:tok
|
|
160 |
type := t.
|
|
161 |
token := tok
|
14
|
162 |
!
|
|
163 |
|
140
|
164 |
type:t token:tok index:i
|
|
165 |
type := t.
|
|
166 |
index := i.
|
|
167 |
token := tok
|
14
|
168 |
!
|
|
169 |
|
140
|
170 |
type:t token:tok index:i block:b
|
|
171 |
type := t.
|
|
172 |
index := i.
|
|
173 |
block := b.
|
|
174 |
token := tok
|
14
|
175 |
! !
|
|
176 |
|
|
177 |
!VariableNode methodsFor:'code generation'!
|
|
178 |
|
104
|
179 |
codeForSideEffectOn:aStream inBlock:b for:aCompiler
|
14
|
180 |
"no code at all"
|
|
181 |
^ self
|
|
182 |
!
|
|
183 |
|
104
|
184 |
codeOn:aStream inBlock:codeBlock for:aCompiler
|
117
|
185 |
|theCode b deltaLevel litIndex|
|
14
|
186 |
|
|
187 |
(type == #MethodArg) ifTrue:[
|
53
|
188 |
(index <= 4) ifTrue:[
|
|
189 |
aStream nextPut:(#(pushMethodArg1
|
|
190 |
pushMethodArg2
|
|
191 |
pushMethodArg3
|
|
192 |
pushMethodArg4) at:index).
|
|
193 |
^ self
|
|
194 |
].
|
98
|
195 |
aStream nextPut:#pushMethodArg; nextPut:index.
|
53
|
196 |
^ self
|
14
|
197 |
].
|
|
198 |
(type == #MethodVariable) ifTrue:[
|
53
|
199 |
(index <= 6) ifTrue:[
|
|
200 |
aStream nextPut:(#(pushMethodVar1
|
|
201 |
pushMethodVar2
|
|
202 |
pushMethodVar3
|
|
203 |
pushMethodVar4
|
|
204 |
pushMethodVar5
|
|
205 |
pushMethodVar6) at:index).
|
|
206 |
^ self
|
|
207 |
].
|
98
|
208 |
aStream nextPut:#pushMethodVar; nextPut:index.
|
53
|
209 |
^ self
|
14
|
210 |
].
|
|
211 |
(type == #InstanceVariable) ifTrue:[
|
53
|
212 |
(index <= 10) ifTrue:[
|
|
213 |
theCode := #(pushInstVar1 pushInstVar2 pushInstVar3
|
|
214 |
pushInstVar4 pushInstVar5 pushInstVar6
|
|
215 |
pushInstVar7 pushInstVar8 pushInstVar9
|
|
216 |
pushInstVar10) at:index.
|
|
217 |
aStream nextPut:theCode.
|
|
218 |
^ self
|
|
219 |
].
|
98
|
220 |
aStream nextPut:#pushInstVar; nextPut:index.
|
53
|
221 |
^ self
|
14
|
222 |
].
|
|
223 |
(type == #BlockArg) ifTrue:[
|
53
|
224 |
"find deltaLevel to block, where argument was defined"
|
|
225 |
b := codeBlock.
|
|
226 |
deltaLevel := 0.
|
|
227 |
[b notNil and:[b ~~ block]] whileTrue:[
|
|
228 |
b inlineBlock ifFalse:[
|
|
229 |
deltaLevel := deltaLevel + 1
|
|
230 |
].
|
|
231 |
b := b home
|
|
232 |
].
|
|
233 |
(deltaLevel == 0) ifTrue:[
|
|
234 |
(index <= 4) ifTrue:[
|
|
235 |
theCode := #(pushBlockArg1 pushBlockArg2 pushBlockArg3
|
|
236 |
pushBlockArg4) at:index.
|
|
237 |
aStream nextPut:theCode.
|
|
238 |
^ self
|
|
239 |
].
|
|
240 |
aStream nextPut:#pushBlockArg.
|
|
241 |
] ifFalse:[
|
|
242 |
(deltaLevel == 1) ifTrue:[
|
|
243 |
aStream nextPut:#pushOuter1BlockArg
|
|
244 |
] ifFalse:[
|
|
245 |
(deltaLevel == 2) ifTrue:[
|
|
246 |
aStream nextPut:#pushOuter2BlockArg
|
|
247 |
] ifFalse:[
|
98
|
248 |
aStream nextPut:#pushOuterBlockArg; nextPut:deltaLevel
|
53
|
249 |
]
|
|
250 |
].
|
|
251 |
].
|
98
|
252 |
aStream nextPut:index.
|
53
|
253 |
^ self
|
14
|
254 |
].
|
|
255 |
(type == #GlobalVariable) ifTrue:[
|
117
|
256 |
"/ aStream nextPut:#pushGlobal; nextPut:name.
|
|
257 |
litIndex := aCompiler addLiteral:name asSymbol.
|
|
258 |
litIndex < 256 ifTrue:[
|
|
259 |
aStream nextPut:#pushGlobalS; nextPut:litIndex
|
|
260 |
] ifFalse:[
|
|
261 |
aStream nextPut:#pushGlobalL; nextPut:litIndex; nextPut:0
|
|
262 |
].
|
98
|
263 |
"slot for generation and cell address (4 byte)"
|
117
|
264 |
aStream next:5 put:0.
|
53
|
265 |
^ self
|
14
|
266 |
].
|
|
267 |
(type == #ClassVariable) ifTrue:[
|
117
|
268 |
"/ aStream nextPut:#pushClassVar; nextPut:(selfClass name , ':' , name) asSymbol.
|
|
269 |
litIndex := aCompiler addLiteral:(selfClass name , ':' , name) asSymbol.
|
|
270 |
litIndex < 256 ifTrue:[
|
|
271 |
aStream nextPut:#pushClassVarS; nextPut:litIndex
|
|
272 |
] ifFalse:[
|
|
273 |
aStream nextPut:#pushClassVarL; nextPut:litIndex; nextPut:0
|
|
274 |
].
|
98
|
275 |
"slot for generation and cell address (4 byte)"
|
117
|
276 |
aStream next:5 put:0.
|
53
|
277 |
^ self
|
14
|
278 |
].
|
|
279 |
(type == #BlockVariable) ifTrue:[
|
53
|
280 |
"find deltaLevel to block, where variable was defined"
|
|
281 |
b := codeBlock.
|
|
282 |
deltaLevel := 0.
|
|
283 |
[b notNil and:[b ~~ block]] whileTrue:[
|
|
284 |
b inlineBlock ifFalse:[
|
|
285 |
deltaLevel := deltaLevel + 1
|
|
286 |
].
|
|
287 |
b := b home
|
|
288 |
].
|
14
|
289 |
|
53
|
290 |
(deltaLevel == 0) ifTrue:[
|
|
291 |
aStream nextPut:#pushBlockVar.
|
|
292 |
] ifFalse:[
|
98
|
293 |
aStream nextPut:#pushOuterBlockVar; nextPut:deltaLevel.
|
53
|
294 |
].
|
98
|
295 |
aStream nextPut:index.
|
53
|
296 |
^ self
|
14
|
297 |
].
|
|
298 |
(type == #ClassInstanceVariable) ifTrue:[
|
98
|
299 |
aStream nextPut:#pushClassInstVar; nextPut:index.
|
53
|
300 |
^ self
|
14
|
301 |
].
|
|
302 |
(type == #ThisContext) ifTrue:[
|
53
|
303 |
aStream nextPut:#pushThisContext. ^ self
|
14
|
304 |
].
|
|
305 |
|
|
306 |
"not reached"
|
|
307 |
self halt:'bad type'.
|
|
308 |
!
|
|
309 |
|
104
|
310 |
codeStoreOn:aStream inBlock:codeBlock valueNeeded:valueNeeded for:aCompiler
|
117
|
311 |
|theCode b deltaLevel litIndex|
|
14
|
312 |
|
|
313 |
valueNeeded ifTrue:[
|
53
|
314 |
aStream nextPut:#dup
|
14
|
315 |
].
|
|
316 |
(type == #MethodVariable) ifTrue:[
|
53
|
317 |
(index <= 6) ifTrue:[
|
|
318 |
theCode := #(storeMethodVar1 storeMethodVar2
|
|
319 |
storeMethodVar3 storeMethodVar4
|
|
320 |
storeMethodVar5 storeMethodVar6) at:index.
|
|
321 |
aStream nextPut:theCode.
|
|
322 |
^ self
|
|
323 |
].
|
98
|
324 |
aStream nextPut:#storeMethodVar; nextPut:index.
|
53
|
325 |
^ self
|
14
|
326 |
].
|
|
327 |
(type == #InstanceVariable) ifTrue:[
|
53
|
328 |
(index <= 10) ifTrue:[
|
|
329 |
theCode := #(storeInstVar1 storeInstVar2
|
|
330 |
storeInstVar3 storeInstVar4
|
|
331 |
storeInstVar5 storeInstVar6
|
|
332 |
storeInstVar7 storeInstVar8
|
|
333 |
storeInstVar9 storeInstVar10) at:index.
|
|
334 |
aStream nextPut:theCode.
|
|
335 |
^ self
|
|
336 |
].
|
98
|
337 |
aStream nextPut:#storeInstVar; nextPut:index.
|
53
|
338 |
^ self
|
14
|
339 |
].
|
|
340 |
(type == #BlockVariable) ifTrue:[
|
53
|
341 |
"find deltaLevel to block, where variable was defined"
|
|
342 |
b := codeBlock.
|
|
343 |
deltaLevel := 0.
|
|
344 |
[b notNil and:[b ~~ block]] whileTrue:[
|
|
345 |
b inlineBlock ifFalse:[
|
|
346 |
deltaLevel := deltaLevel + 1
|
|
347 |
].
|
|
348 |
b := b home
|
|
349 |
].
|
14
|
350 |
|
53
|
351 |
(deltaLevel == 0) ifTrue:[
|
98
|
352 |
aStream nextPut:#storeBlockVar
|
53
|
353 |
] ifFalse:[
|
98
|
354 |
aStream nextPut:#storeOuterBlockVar; nextPut:deltaLevel
|
53
|
355 |
].
|
98
|
356 |
aStream nextPut:index.
|
53
|
357 |
^ self
|
14
|
358 |
].
|
117
|
359 |
(type == #GlobalVariable) ifTrue:[
|
|
360 |
"/ aStream nextPut:#storeGlobal; nextPut:name.
|
|
361 |
litIndex := aCompiler addLiteral:name asSymbol.
|
|
362 |
litIndex < 256 ifTrue:[
|
|
363 |
aStream nextPut:#storeGlobalS; nextPut:litIndex
|
|
364 |
] ifFalse:[
|
|
365 |
aStream nextPut:#storeGlobalL; nextPut:litIndex; nextPut:0
|
|
366 |
].
|
|
367 |
"slot for generation and cell address (4 byte)"
|
|
368 |
aStream nextPut:0; nextPut:0; nextPut:0; nextPut:0; nextPut:0.
|
|
369 |
^ self
|
|
370 |
].
|
14
|
371 |
(type == #ClassVariable) ifTrue:[
|
117
|
372 |
"/ aStream nextPut:#storeClassVar; nextPut:(selfClass name , ':' , name) asSymbol.
|
|
373 |
litIndex := aCompiler addLiteral:(selfClass name , ':' , name) asSymbol.
|
|
374 |
litIndex < 256 ifTrue:[
|
|
375 |
aStream nextPut:#storeClassVarS; nextPut:litIndex
|
|
376 |
] ifFalse:[
|
|
377 |
aStream nextPut:#storeClassVarL; nextPut:litIndex; nextPut:0
|
|
378 |
].
|
98
|
379 |
"slot for generation and cell address (4 byte)"
|
|
380 |
aStream nextPut:0; nextPut:0; nextPut:0; nextPut:0; nextPut:0.
|
53
|
381 |
^ self
|
14
|
382 |
].
|
|
383 |
(type == #ClassInstanceVariable) ifTrue:[
|
98
|
384 |
aStream nextPut:#storeClassInstVar; nextPut:index.
|
53
|
385 |
^ self
|
14
|
386 |
].
|
106
|
387 |
"not reached"
|
14
|
388 |
^ self error:'bad assignment'
|
|
389 |
! !
|
|
390 |
|
140
|
391 |
!VariableNode methodsFor:'evaluating'!
|
|
392 |
|
|
393 |
evaluate
|
|
394 |
(type == #MethodVariable
|
|
395 |
or:[type == #BlockArg
|
|
396 |
or:[type == #BlockVariable]]) ifTrue:[
|
|
397 |
^ token variableValue
|
|
398 |
].
|
|
399 |
(type == #InstanceVariable) ifTrue:[
|
|
400 |
^ selfValue instVarAt:index
|
|
401 |
].
|
|
402 |
(type == #GlobalVariable) ifTrue:[
|
|
403 |
(Smalltalk includesKey:name) ifTrue:[
|
|
404 |
^ Smalltalk at:name
|
|
405 |
].
|
|
406 |
"
|
|
407 |
self error:('global ' , name , ' is undefined').
|
|
408 |
"
|
|
409 |
|
|
410 |
^ UndefinedVariable name:name.
|
|
411 |
^ nil
|
|
412 |
].
|
|
413 |
(type == #ClassVariable) ifTrue:[
|
|
414 |
^ Smalltalk at:(selfClass name , ':' , name) asSymbol
|
|
415 |
].
|
|
416 |
(type == #ClassInstanceVariable) ifTrue:[
|
|
417 |
^ selfClass instVarAt:index
|
|
418 |
].
|
|
419 |
(type == #ThisContext) ifTrue:[
|
|
420 |
^ thisContext
|
|
421 |
].
|
|
422 |
"not reached"
|
|
423 |
self halt:'bad type'.
|
|
424 |
^ value
|
|
425 |
!
|
|
426 |
|
|
427 |
store:aValue
|
|
428 |
(type == #MethodVariable
|
|
429 |
or:[type == #BlockVariable]) ifTrue:[
|
|
430 |
token value:aValue. ^ aValue
|
|
431 |
].
|
|
432 |
(type == #InstanceVariable) ifTrue:[
|
|
433 |
^ selfValue instVarAt:index put:aValue
|
|
434 |
].
|
|
435 |
(type == #GlobalVariable) ifTrue:[
|
|
436 |
^ Smalltalk at:name put:aValue
|
|
437 |
].
|
|
438 |
(type == #ClassVariable) ifTrue:[
|
|
439 |
^ Smalltalk at:(selfClass name , ':' , name) asSymbol put:aValue
|
|
440 |
].
|
|
441 |
(type == #ClassInstanceVariable) ifTrue:[
|
|
442 |
^ selfClass instVarAt:index put:aValue
|
|
443 |
].
|
|
444 |
"not reached"
|
|
445 |
self halt:'bad type'.
|
|
446 |
^ aValue
|
|
447 |
! !
|
|
448 |
|
14
|
449 |
!VariableNode methodsFor:'printing'!
|
|
450 |
|
|
451 |
displayString
|
|
452 |
^ 'InterpreterVariable(' , self printString , ')'
|
|
453 |
!
|
|
454 |
|
|
455 |
printOn:aStream indent:i
|
98
|
456 |
|
|
457 |
(type == #MethodArg "/ actually only a debug-check
|
|
458 |
or:[type == #MethodVariable
|
|
459 |
or:[type == #InstanceVariable
|
|
460 |
or:[type == #BlockArg
|
|
461 |
or:[type == #GlobalVariable
|
|
462 |
or:[type == #ClassVariable
|
|
463 |
or:[type == #BlockVariable
|
|
464 |
or:[type == #ClassInstanceVariable]]]]]]]) ifTrue:[
|
53
|
465 |
aStream nextPutAll:name. ^ self
|
14
|
466 |
].
|
|
467 |
(type == #ThisContext) ifTrue:[
|
53
|
468 |
aStream nextPutAll:'thisContext'. ^ self
|
14
|
469 |
].
|
|
470 |
"not reached"
|
|
471 |
self halt:'bad type'.
|
|
472 |
! !
|
140
|
473 |
|
|
474 |
!VariableNode methodsFor:'queries'!
|
|
475 |
|
|
476 |
isGlobal
|
|
477 |
"return true, if this is a node for a global variable"
|
|
478 |
|
|
479 |
^ (type == #GlobalVariable) and:[Smalltalk includesKey:name]
|
|
480 |
! !
|
|
481 |
|