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