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