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