author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:31:47 +0100 | |
changeset 135 | aa4f7b8f121e |
parent 106 | 2653f74569d1 |
child 140 | 1ef1d1395146 |
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 |
|
14 |
instanceVariableNames:'' |
|
10 | 15 |
classVariableNames:'TrueNode FalseNode NilNode Const0Node Const1Node |
47 | 16 |
Float0Node' |
0 | 17 |
poolDictionaries:'' |
18 |
category:'System-Compiler-Support' |
|
19 |
! |
|
20 |
||
20 | 21 |
!ConstantNode class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
47 | 26 |
All Rights Reserved |
20 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
version |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
38 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ConstNode.st,v 1.17 1995-11-11 15:30:17 cg Exp $' |
20 | 39 |
! |
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
node for parse-trees, representing literal constants |
|
44 |
" |
|
45 |
! ! |
|
0 | 46 |
|
47 |
!ConstantNode class methodsFor:'queries'! |
|
48 |
||
49 |
typeOfConstant:anObject |
|
62 | 50 |
"return the constantNode type for an object" |
51 |
||
52 |
"the most common case first ..." |
|
53 |
||
54 |
(anObject isMemberOf:SmallInteger) ifTrue:[ |
|
55 |
^ #Integer |
|
56 |
]. |
|
57 |
||
98 | 58 |
anObject isNil ifTrue:[ |
59 |
^ #Nil |
|
60 |
]. |
|
61 |
||
6 | 62 |
anObject isNumber ifTrue:[ |
62 | 63 |
"the most common case first ..." |
64 |
(anObject isMemberOf:Float) ifTrue:[ |
|
65 |
^ #Float |
|
66 |
]. |
|
67 |
anObject isInteger ifTrue:[ |
|
47 | 68 |
^ #Integer |
69 |
]. |
|
0 | 70 |
]. |
71 |
(anObject == true) ifTrue:[ |
|
47 | 72 |
^ #True |
0 | 73 |
]. |
74 |
(anObject == false) ifTrue:[ |
|
47 | 75 |
^ #False |
0 | 76 |
]. |
77 |
^ #Literal |
|
78 |
! ! |
|
79 |
||
80 |
!ConstantNode class methodsFor:'instance creation'! |
|
81 |
||
98 | 82 |
value:val |
83 |
^ self type:(self typeOfConstant:val) value:val |
|
84 |
! |
|
85 |
||
0 | 86 |
type:t value:val |
13 | 87 |
"some constant nodes are used so often, its worth caching them" |
0 | 88 |
(t == #True) ifTrue:[ |
47 | 89 |
TrueNode isNil ifTrue:[ |
90 |
TrueNode := (self basicNew) type:t value:val |
|
91 |
]. |
|
92 |
^ TrueNode |
|
0 | 93 |
]. |
94 |
(t == #False) ifTrue:[ |
|
47 | 95 |
FalseNode isNil ifTrue:[ |
96 |
FalseNode := (self basicNew) type:t value:val |
|
97 |
]. |
|
98 |
^ FalseNode |
|
0 | 99 |
]. |
100 |
(t == #Nil) ifTrue:[ |
|
47 | 101 |
NilNode isNil ifTrue:[ |
102 |
NilNode := (self basicNew) type:t value:val |
|
103 |
]. |
|
104 |
^ NilNode |
|
0 | 105 |
]. |
106 |
(t == #Integer) ifTrue:[ |
|
47 | 107 |
(val == 0) ifTrue:[ |
108 |
Const0Node isNil ifTrue:[ |
|
109 |
Const0Node := (self basicNew) type:t value:val |
|
110 |
]. |
|
111 |
^ Const0Node |
|
112 |
]. |
|
113 |
(val == 1) ifTrue:[ |
|
114 |
Const1Node isNil ifTrue:[ |
|
115 |
Const1Node := (self basicNew) type:t value:val |
|
116 |
]. |
|
117 |
^ Const1Node |
|
118 |
] |
|
0 | 119 |
]. |
120 |
(t == #Float) ifTrue:[ |
|
47 | 121 |
(val = 0.0) ifTrue:[ |
122 |
Float0Node isNil ifTrue:[ |
|
123 |
Float0Node := (self basicNew) type:t value:val |
|
124 |
]. |
|
125 |
^ Float0Node |
|
126 |
] |
|
0 | 127 |
]. |
128 |
^ (self basicNew) type:t value:val |
|
129 |
! ! |
|
130 |
||
13 | 131 |
!ConstantNode methodsFor:'accessing'! |
132 |
||
133 |
type:t value:val |
|
134 |
type := t. |
|
135 |
value := val |
|
136 |
! ! |
|
137 |
||
0 | 138 |
!ConstantNode methodsFor:'queries'! |
139 |
||
140 |
isConstant |
|
141 |
^ true |
|
142 |
! ! |
|
143 |
||
144 |
!ConstantNode methodsFor:'evaluating'! |
|
145 |
||
146 |
evaluate |
|
147 |
^ value |
|
148 |
! |
|
149 |
||
150 |
store:aValue |
|
13 | 151 |
"not reached - parser checks for this" |
152 |
||
0 | 153 |
self error:'store not allowed'. |
154 |
^ aValue |
|
155 |
! ! |
|
156 |
||
157 |
!ConstantNode methodsFor:'code generation'! |
|
158 |
||
104 | 159 |
codeOn:aStream inBlock:b for:aCompiler |
0 | 160 |
"generated code for the constant" |
161 |
||
104 | 162 |
|code index| |
96 | 163 |
|
0 | 164 |
(type == #Integer) ifTrue:[ |
47 | 165 |
(value between: -128 and:127) ifTrue:[ |
166 |
(value == 0) ifTrue:[ |
|
96 | 167 |
code := #push0 |
47 | 168 |
]. |
169 |
(value == 1) ifTrue:[ |
|
96 | 170 |
code := #push1. |
47 | 171 |
]. |
172 |
(value == 2) ifTrue:[ |
|
96 | 173 |
code := #push2. |
47 | 174 |
]. |
175 |
(value == -1) ifTrue:[ |
|
96 | 176 |
code := #pushMinus1. |
47 | 177 |
]. |
96 | 178 |
code notNil ifTrue:[ |
179 |
aStream nextPut:code. ^ self |
|
180 |
]. |
|
181 |
||
182 |
aStream nextPut:#pushNum; nextPut:value. |
|
47 | 183 |
^ self |
184 |
]. |
|
185 |
(value between:16r-8000 and:16r7FFF) ifTrue:[ |
|
96 | 186 |
aStream nextPut:#pushNum16; nextPut:value; nextPut:0. |
47 | 187 |
^ self |
188 |
] |
|
0 | 189 |
]. |
190 |
(type == #Nil) ifTrue:[ |
|
96 | 191 |
code := #pushNil. |
0 | 192 |
]. |
193 |
(type == #True) ifTrue:[ |
|
96 | 194 |
code := #pushTrue. |
0 | 195 |
]. |
196 |
(type == #False) ifTrue:[ |
|
96 | 197 |
code := #pushFalse. |
0 | 198 |
]. |
96 | 199 |
code notNil ifTrue:[ |
200 |
aStream nextPut:code. ^ self |
|
201 |
]. |
|
106 | 202 |
|
203 |
"/ kludge for backward compatibility |
|
204 |
aCompiler isNil ifTrue:[ |
|
205 |
aStream nextPut:#pushLit; nextPut:value. |
|
206 |
^ self. |
|
207 |
]. |
|
208 |
||
104 | 209 |
index := aCompiler addLiteral:value. |
210 |
index <= 8 ifTrue:[ |
|
211 |
aStream nextPut:(#(pushLit1 pushLit2 pushLit3 pushLit4 |
|
212 |
pushLit5 pushLit6 pushLit7 pushLit8) at:index). |
|
213 |
] ifFalse:[ |
|
106 | 214 |
index < 256 ifTrue:[ |
215 |
aStream nextPut:#pushLitS; nextPut:index |
|
216 |
] ifFalse:[ |
|
217 |
aStream nextPut:#pushLitL; nextPut:index; nextPut:0 |
|
218 |
]. |
|
104 | 219 |
]. |
0 | 220 |
! |
221 |
||
104 | 222 |
codeStoreOn:aStream inBlock:codeBlock valueNeeded:valueNeeded for:aCompiler |
13 | 223 |
"not sent - parser checks for this" |
0 | 224 |
|
225 |
^ self error:'assignment to literals not allowed' |
|
226 |
! ! |
|
227 |
||
228 |
!ConstantNode methodsFor:'printing'! |
|
229 |
||
13 | 230 |
displayString |
231 |
^ value displayString |
|
232 |
! |
|
233 |
||
0 | 234 |
printOn:aStream indent:i |
235 |
value storeOn:aStream |
|
236 |
! ! |