author | Claus Gittinger <cg@exept.de> |
Thu, 14 May 1998 19:24:30 +0200 | |
changeset 710 | fc74f135494d |
parent 612 | 2748896a66c8 |
child 711 | 25b9a501b97d |
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 |
||
710 | 132 |
lineNr:ignoredLineNumber |
133 |
||
134 |
"Created: / 14.5.1998 / 19:24:17 / cg" |
|
135 |
! |
|
136 |
||
13 | 137 |
type:t value:val |
138 |
type := t. |
|
139 |
value := val |
|
140 |
! ! |
|
141 |
||
0 | 142 |
!ConstantNode methodsFor:'code generation'! |
143 |
||
104 | 144 |
codeOn:aStream inBlock:b for:aCompiler |
0 | 145 |
"generated code for the constant" |
146 |
||
104 | 147 |
|code index| |
96 | 148 |
|
0 | 149 |
(type == #Integer) ifTrue:[ |
47 | 150 |
(value between: -128 and:127) ifTrue:[ |
151 |
(value == 0) ifTrue:[ |
|
96 | 152 |
code := #push0 |
47 | 153 |
]. |
154 |
(value == 1) ifTrue:[ |
|
96 | 155 |
code := #push1. |
47 | 156 |
]. |
157 |
(value == 2) ifTrue:[ |
|
96 | 158 |
code := #push2. |
47 | 159 |
]. |
160 |
(value == -1) ifTrue:[ |
|
96 | 161 |
code := #pushMinus1. |
47 | 162 |
]. |
96 | 163 |
code notNil ifTrue:[ |
164 |
aStream nextPut:code. ^ self |
|
165 |
]. |
|
166 |
||
167 |
aStream nextPut:#pushNum; nextPut:value. |
|
47 | 168 |
^ self |
169 |
]. |
|
170 |
(value between:16r-8000 and:16r7FFF) ifTrue:[ |
|
96 | 171 |
aStream nextPut:#pushNum16; nextPut:value; nextPut:0. |
47 | 172 |
^ self |
173 |
] |
|
0 | 174 |
]. |
175 |
(type == #Nil) ifTrue:[ |
|
96 | 176 |
code := #pushNil. |
0 | 177 |
]. |
178 |
(type == #True) ifTrue:[ |
|
96 | 179 |
code := #pushTrue. |
0 | 180 |
]. |
181 |
(type == #False) ifTrue:[ |
|
96 | 182 |
code := #pushFalse. |
0 | 183 |
]. |
96 | 184 |
code notNil ifTrue:[ |
185 |
aStream nextPut:code. ^ self |
|
186 |
]. |
|
106 | 187 |
|
188 |
"/ kludge for backward compatibility |
|
189 |
aCompiler isNil ifTrue:[ |
|
190 |
aStream nextPut:#pushLit; nextPut:value. |
|
191 |
^ self. |
|
192 |
]. |
|
193 |
||
104 | 194 |
index := aCompiler addLiteral:value. |
195 |
index <= 8 ifTrue:[ |
|
196 |
aStream nextPut:(#(pushLit1 pushLit2 pushLit3 pushLit4 |
|
197 |
pushLit5 pushLit6 pushLit7 pushLit8) at:index). |
|
198 |
] ifFalse:[ |
|
106 | 199 |
index < 256 ifTrue:[ |
200 |
aStream nextPut:#pushLitS; nextPut:index |
|
201 |
] ifFalse:[ |
|
202 |
aStream nextPut:#pushLitL; nextPut:index; nextPut:0 |
|
203 |
]. |
|
104 | 204 |
]. |
0 | 205 |
! |
206 |
||
104 | 207 |
codeStoreOn:aStream inBlock:codeBlock valueNeeded:valueNeeded for:aCompiler |
13 | 208 |
"not sent - parser checks for this" |
0 | 209 |
|
210 |
^ self error:'assignment to literals not allowed' |
|
211 |
! ! |
|
212 |
||
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
213 |
!ConstantNode methodsFor:'enumeration'! |
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 |
nodeDo:anEnumerator |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
216 |
"helper for parse tree walking" |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
217 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
218 |
^ anEnumerator doLiteral:self value:value |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
219 |
|
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
220 |
"Modified: 19.6.1997 / 16:40:59 / cg" |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
221 |
! ! |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
263
diff
changeset
|
222 |
|
140 | 223 |
!ConstantNode methodsFor:'evaluating'! |
224 |
||
225 |
evaluate |
|
226 |
^ value |
|
227 |
! |
|
228 |
||
229 |
store:aValue |
|
230 |
"not reached - parser checks for this" |
|
231 |
||
232 |
self error:'store not allowed'. |
|
233 |
^ aValue |
|
234 |
! ! |
|
235 |
||
0 | 236 |
!ConstantNode methodsFor:'printing'! |
237 |
||
13 | 238 |
displayString |
612 | 239 |
"return a printed representation of the receiver for displaying" |
240 |
||
13 | 241 |
^ value displayString |
612 | 242 |
|
243 |
"Modified: 20.9.1997 / 11:39:07 / cg" |
|
13 | 244 |
! |
245 |
||
0 | 246 |
printOn:aStream indent:i |
247 |
value storeOn:aStream |
|
248 |
! ! |
|
140 | 249 |
|
250 |
!ConstantNode methodsFor:'queries'! |
|
251 |
||
242
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
252 |
canReuseAsArg:anotherNode |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
253 |
|otherValue| |
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 |
anotherNode isConstant ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
256 |
anotherNode type ~~ type ifTrue:[^ 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 |
otherValue := anotherNode evaluate. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
259 |
(value isMemberOf:SmallInteger) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
260 |
(value == 0) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
261 |
(value == 1) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
262 |
(value == 2) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
263 |
(value == -1) ifTrue:[^ false]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
264 |
^ otherValue == value |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
265 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
266 |
(value isMemberOf:Float) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
267 |
^ (otherValue isMemberOf:Float) |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
268 |
and:[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:Symbol) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
271 |
^ otherValue == value |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
272 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
273 |
(value isMemberOf:String) ifTrue:[ |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
274 |
^ (otherValue isMemberOf:String) |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
275 |
and:[otherValue = value] |
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 |
]. |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
278 |
^ false |
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 |
"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
|
281 |
"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
|
282 |
! |
3cca9ffd2620
use dup if possible when pushing args
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
283 |
|
140 | 284 |
isConstant |
285 |
^ true |
|
286 |
! ! |
|
287 |
||
148 | 288 |
!ConstantNode class methodsFor:'documentation'! |
289 |
||
290 |
version |
|
710 | 291 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ConstNode.st,v 1.25 1998-05-14 17:24:30 cg Exp $' |
148 | 292 |
! ! |