author | Stefan Vogel <sv@exept.de> |
Fri, 06 Mar 1998 16:38:37 +0100 | |
changeset 657 | 0ecf1ff6f6bf |
parent 612 | 2748896a66c8 |
child 710 | fc74f135494d |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
47 | 3 |
All Rights Reserved |
0 | 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 |
||
612 | 13 |
'From Smalltalk/X, Version:3.1.10 on 20-sep-1997 at 11:49:43 pm' ! |
14 |
||
0 | 15 |
PrimaryNode subclass:#ConstantNode |
242
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
16 |
instanceVariableNames:'' |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
17 |
classVariableNames:'TrueNode FalseNode NilNode Const0Node Const1Node Float0Node' |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
18 |
poolDictionaries:'' |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
19 |
category:'System-Compiler-Support' |
0 | 20 |
! |
21 |
||
20 | 22 |
!ConstantNode class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
47 | 27 |
All Rights Reserved |
20 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
node for parse-trees, representing literal constants |
|
261 | 41 |
This is a helper class for the compiler. |
263 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
20 | 45 |
" |
0 | 46 |
! ! |
47 |
||
48 |
!ConstantNode class methodsFor:'instance creation'! |
|
49 |
||
50 |
type:t value:val |
|
13 | 51 |
"some constant nodes are used so often, its worth caching them" |
0 | 52 |
(t == #True) ifTrue:[ |
47 | 53 |
TrueNode isNil ifTrue:[ |
54 |
TrueNode := (self basicNew) type:t value:val |
|
55 |
]. |
|
56 |
^ TrueNode |
|
0 | 57 |
]. |
58 |
(t == #False) ifTrue:[ |
|
47 | 59 |
FalseNode isNil ifTrue:[ |
60 |
FalseNode := (self basicNew) type:t value:val |
|
61 |
]. |
|
62 |
^ FalseNode |
|
0 | 63 |
]. |
64 |
(t == #Nil) ifTrue:[ |
|
47 | 65 |
NilNode isNil ifTrue:[ |
66 |
NilNode := (self basicNew) type:t value:val |
|
67 |
]. |
|
68 |
^ NilNode |
|
0 | 69 |
]. |
70 |
(t == #Integer) ifTrue:[ |
|
47 | 71 |
(val == 0) ifTrue:[ |
72 |
Const0Node isNil ifTrue:[ |
|
73 |
Const0Node := (self basicNew) type:t value:val |
|
74 |
]. |
|
75 |
^ Const0Node |
|
76 |
]. |
|
77 |
(val == 1) ifTrue:[ |
|
78 |
Const1Node isNil ifTrue:[ |
|
79 |
Const1Node := (self basicNew) type:t value:val |
|
80 |
]. |
|
81 |
^ Const1Node |
|
82 |
] |
|
0 | 83 |
]. |
84 |
(t == #Float) ifTrue:[ |
|
47 | 85 |
(val = 0.0) ifTrue:[ |
86 |
Float0Node isNil ifTrue:[ |
|
87 |
Float0Node := (self basicNew) type:t value:val |
|
88 |
]. |
|
89 |
^ Float0Node |
|
90 |
] |
|
0 | 91 |
]. |
92 |
^ (self basicNew) type:t value:val |
|
140 | 93 |
! |
94 |
||
95 |
value:val |
|
96 |
^ self type:(self typeOfConstant:val) value:val |
|
97 |
! ! |
|
98 |
||
99 |
!ConstantNode class methodsFor:'queries'! |
|
100 |
||
101 |
typeOfConstant:anObject |
|
102 |
"return the constantNode type for an object" |
|
103 |
||
104 |
"the most common case first ..." |
|
105 |
||
106 |
(anObject isMemberOf:SmallInteger) ifTrue:[ |
|
107 |
^ #Integer |
|
108 |
]. |
|
109 |
||
110 |
anObject isNil ifTrue:[ |
|
111 |
^ #Nil |
|
112 |
]. |
|
113 |
||
114 |
anObject isNumber ifTrue:[ |
|
115 |
"the most common case first ..." |
|
116 |
(anObject isMemberOf:Float) ifTrue:[ |
|
117 |
^ #Float |
|
118 |
]. |
|
119 |
anObject isInteger ifTrue:[ |
|
120 |
^ #Integer |
|
121 |
]. |
|
122 |
]. |
|
123 |
(anObject == true) ifTrue:[ |
|
124 |
^ #True |
|
125 |
]. |
|
126 |
(anObject == false) ifTrue:[ |
|
127 |
^ #False |
|
128 |
]. |
|
129 |
^ #Literal |
|
0 | 130 |
! ! |
131 |
||
13 | 132 |
!ConstantNode methodsFor:'accessing'! |
133 |
||
134 |
type:t value:val |
|
135 |
type := t. |
|
136 |
value := val |
|
137 |
! ! |
|
138 |
||
0 | 139 |
!ConstantNode methodsFor:'code generation'! |
140 |
||
104 | 141 |
codeOn:aStream inBlock:b for:aCompiler |
0 | 142 |
"generated code for the constant" |
143 |
||
104 | 144 |
|code index| |
96 | 145 |
|
0 | 146 |
(type == #Integer) ifTrue:[ |
47 | 147 |
(value between: -128 and:127) ifTrue:[ |
148 |
(value == 0) ifTrue:[ |
|
96 | 149 |
code := #push0 |
47 | 150 |
]. |
151 |
(value == 1) ifTrue:[ |
|
96 | 152 |
code := #push1. |
47 | 153 |
]. |
154 |
(value == 2) ifTrue:[ |
|
96 | 155 |
code := #push2. |
47 | 156 |
]. |
157 |
(value == -1) ifTrue:[ |
|
96 | 158 |
code := #pushMinus1. |
47 | 159 |
]. |
96 | 160 |
code notNil ifTrue:[ |
161 |
aStream nextPut:code. ^ self |
|
162 |
]. |
|
163 |
||
164 |
aStream nextPut:#pushNum; nextPut:value. |
|
47 | 165 |
^ self |
166 |
]. |
|
167 |
(value between:16r-8000 and:16r7FFF) ifTrue:[ |
|
96 | 168 |
aStream nextPut:#pushNum16; nextPut:value; nextPut:0. |
47 | 169 |
^ self |
170 |
] |
|
0 | 171 |
]. |
172 |
(type == #Nil) ifTrue:[ |
|
96 | 173 |
code := #pushNil. |
0 | 174 |
]. |
175 |
(type == #True) ifTrue:[ |
|
96 | 176 |
code := #pushTrue. |
0 | 177 |
]. |
178 |
(type == #False) ifTrue:[ |
|
96 | 179 |
code := #pushFalse. |
0 | 180 |
]. |
96 | 181 |
code notNil ifTrue:[ |
182 |
aStream nextPut:code. ^ self |
|
183 |
]. |
|
106 | 184 |
|
185 |
"/ kludge for backward compatibility |
|
186 |
aCompiler isNil ifTrue:[ |
|
187 |
aStream nextPut:#pushLit; nextPut:value. |
|
188 |
^ self. |
|
189 |
]. |
|
190 |
||
104 | 191 |
index := aCompiler addLiteral:value. |
192 |
index <= 8 ifTrue:[ |
|
193 |
aStream nextPut:(#(pushLit1 pushLit2 pushLit3 pushLit4 |
|
194 |
pushLit5 pushLit6 pushLit7 pushLit8) at:index). |
|
195 |
] ifFalse:[ |
|
106 | 196 |
index < 256 ifTrue:[ |
197 |
aStream nextPut:#pushLitS; nextPut:index |
|
198 |
] ifFalse:[ |
|
199 |
aStream nextPut:#pushLitL; nextPut:index; nextPut:0 |
|
200 |
]. |
|
104 | 201 |
]. |
0 | 202 |
! |
203 |
||
104 | 204 |
codeStoreOn:aStream inBlock:codeBlock valueNeeded:valueNeeded for:aCompiler |
13 | 205 |
"not sent - parser checks for this" |
0 | 206 |
|
207 |
^ self error:'assignment to literals not allowed' |
|
208 |
! ! |
|
209 |
||
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
210 |
!ConstantNode methodsFor:'enumeration'! |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
211 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
212 |
nodeDo:anEnumerator |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
213 |
"helper for parse tree walking" |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
214 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
215 |
^ anEnumerator doLiteral:self value:value |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
216 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
217 |
"Modified: 19.6.1997 / 16:40:59 / cg" |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
218 |
! ! |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
219 |
|
140 | 220 |
!ConstantNode methodsFor:'evaluating'! |
221 |
||
222 |
evaluate |
|
223 |
^ value |
|
224 |
! |
|
225 |
||
226 |
store:aValue |
|
227 |
"not reached - parser checks for this" |
|
228 |
||
229 |
self error:'store not allowed'. |
|
230 |
^ aValue |
|
231 |
! ! |
|
232 |
||
0 | 233 |
!ConstantNode methodsFor:'printing'! |
234 |
||
13 | 235 |
displayString |
612 | 236 |
"return a printed representation of the receiver for displaying" |
237 |
||
13 | 238 |
^ value displayString |
612 | 239 |
|
240 |
"Modified: 20.9.1997 / 11:39:07 / cg" |
|
13 | 241 |
! |
242 |
||
0 | 243 |
printOn:aStream indent:i |
244 |
value storeOn:aStream |
|
245 |
! ! |
|
140 | 246 |
|
247 |
!ConstantNode methodsFor:'queries'! |
|
248 |
||
242
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
249 |
canReuseAsArg:anotherNode |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
250 |
|otherValue| |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
251 |
|
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
252 |
anotherNode isConstant ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
253 |
anotherNode type ~~ type ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
254 |
|
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
255 |
otherValue := anotherNode evaluate. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
256 |
(value isMemberOf:SmallInteger) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
257 |
(value == 0) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
258 |
(value == 1) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
259 |
(value == 2) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
260 |
(value == -1) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
261 |
^ otherValue == value |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
262 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
263 |
(value isMemberOf:Float) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
264 |
^ (otherValue isMemberOf:Float) |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
265 |
and:[otherValue = value] |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
266 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
267 |
(value isMemberOf:Symbol) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
268 |
^ otherValue == value |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
269 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
270 |
(value isMemberOf:String) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
271 |
^ (otherValue isMemberOf:String) |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
272 |
and:[otherValue = value] |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
273 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
274 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
275 |
^ false |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
276 |
|
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
277 |
"Created: 14.4.1996 / 00:43:14 / cg" |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
278 |
"Modified: 14.4.1996 / 01:00:29 / cg" |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
279 |
! |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
280 |
|
140 | 281 |
isConstant |
282 |
^ true |
|
283 |
! ! |
|
284 |
||
148 | 285 |
!ConstantNode class methodsFor:'documentation'! |
286 |
||
287 |
version |
|
612 | 288 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ConstNode.st,v 1.24 1997-09-20 21:03:30 cg Exp $' |
148 | 289 |
! ! |