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