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