author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 19:09:53 +0200 | |
changeset 263 | 3b21d0991eff |
parent 261 | 0372e948ca2d |
child 625 | 0b93edcf1116 |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
104 | 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 |
MessageNode subclass:#UnaryNode |
|
261 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'System-Compiler-Support' |
|
0 | 18 |
! |
19 |
||
20 | 20 |
!UnaryNode class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
104 | 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 unary messages |
|
261 | 39 |
This is a helper class for the compiler. |
263 | 40 |
|
41 |
[author:] |
|
42 |
Claus Gittinger |
|
20 | 43 |
" |
44 |
! ! |
|
0 | 45 |
|
46 |
!UnaryNode class methodsFor:'instance creation'! |
|
47 |
||
48 |
receiver:r selector:s |
|
11 | 49 |
"return a new UnaryNode for sending selector s to receiver r. |
50 |
Fold constants." |
|
51 |
||
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
52 |
^ self receiver:r selector:s fold:nil |
0 | 53 |
! |
54 |
||
15 | 55 |
receiver:r selector:selectorString fold:folding |
56 |
"return a new UnaryNode for sending selector selectorString to receiver r. |
|
11 | 57 |
If folding is true, fold constant expressions." |
58 |
||
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
59 |
|result recVal selector canFold globalName| |
0 | 60 |
|
61 |
" |
|
62 |
The constant folding code can usually not optimize things - this may change |
|
63 |
when some kind of constant declaration is added to smalltalk. |
|
64 |
" |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
65 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
66 |
canFold := false. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
67 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
68 |
folding notNil ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
69 |
selector := selectorString asSymbolIfInterned. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
70 |
selector notNil ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
71 |
"/ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
72 |
"/ do constant folding ... |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
73 |
"/ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
74 |
r isGlobal ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
75 |
globalName := r name. |
84 | 76 |
recVal := r evaluate. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
77 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
78 |
(globalName = 'Character') ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
79 |
( #( tab cr space backspace esc ) includes:selector) |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
80 |
ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
81 |
canFold := true |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
82 |
] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
83 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
84 |
(globalName = 'Float') ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
85 |
( #( pi unity zero ) includes:selector) |
104 | 86 |
ifTrue:[ |
87 |
(recVal respondsTo:selector) ifTrue:[ |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
88 |
canFold := true |
104 | 89 |
] |
84 | 90 |
] |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
91 |
] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
92 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
93 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
94 |
r isConstant ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
95 |
"check if we can do it ..." |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
96 |
recVal := r evaluate. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
97 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
98 |
" |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
99 |
we could do much more here - but then, we need a dependency from |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
100 |
the folded selectors method to the method we generate code for ... |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
101 |
limit optimizations to those that will never change |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
102 |
(or, if you change them, it will crash badly anyway ...) |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
103 |
" |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
104 |
recVal respondsToArithmetic ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
105 |
(#( negated abs asPoint degreesToRadians radiansToDegrees |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
106 |
exp ln log sqrt reciprocal |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
107 |
arcCos arcSin arcTan sin cos tan) includes:selector) |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
108 |
ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
109 |
canFold := true |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
110 |
] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
111 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
112 |
recVal isCharacter ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
113 |
(#( asciiValue asInteger digitValue) includes:selector) |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
114 |
ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
115 |
canFold := true |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
116 |
] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
117 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
118 |
recVal isString ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
119 |
(selector == #withCRs) ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
120 |
canFold := (folding >= #level2) or:[folding == #full] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
121 |
] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
122 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
123 |
(recVal isMemberOf:Array) ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
124 |
(#(asFloatArray asDoubleArray) includes:selector) ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
125 |
canFold := (folding >= #level2) or:[folding == #full] |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
126 |
] |
84 | 127 |
] |
128 |
] |
|
129 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
130 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
131 |
canFold ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
132 |
(recVal respondsTo:selector) ifTrue:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
133 |
SignalSet anySignal "Number domainErrorSignal" handle:[:ex | |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
134 |
"in case of an error, abort fold and return original" |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
135 |
ex return |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
136 |
] do:[ |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
137 |
result := recVal perform:selector. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
138 |
^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
139 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
140 |
"when we reach here, something went wrong (something like 0.0 log)" |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
141 |
^ 'error occured when evaluating constant expression' |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
142 |
]. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
143 |
]. |
0 | 144 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
145 |
|
15 | 146 |
^ (self basicNew) receiver:r selector:selectorString args:nil lineno:0 |
0 | 147 |
! ! |
148 |
||
149 |
!UnaryNode methodsFor:'checks'! |
|
150 |
||
151 |
plausibilityCheck |
|
152 |
"check for funny selector - careful to do string compare instead |
|
153 |
of symbol identity compare: I dont want to introduce these as symbols |
|
154 |
into the system (would make the '... is nowhere implemented' warning |
|
155 |
go away." |
|
156 |
||
157 |
((selector = 'self') or:[ |
|
158 |
(selector = 'super') or:[ |
|
17 | 159 |
(selector = 'thisContext') or:[ |
160 |
(selector = 'true') or:[ |
|
161 |
(selector = 'false') or:[ |
|
162 |
(Smalltalk includesKey:selector)]]]]]) ifTrue:[ |
|
104 | 163 |
^ 'funny selector; possible missing ''.'' or keyword' |
0 | 164 |
]. |
17 | 165 |
|
3 | 166 |
"more to come ..." |
0 | 167 |
^ nil |
168 |
! ! |
|
169 |
||
170 |
!UnaryNode methodsFor:'code generation'! |
|
171 |
||
104 | 172 |
codeOn:aStream inBlock:b for:aCompiler |
11 | 173 |
"append bytecode for the receiver to aStream." |
174 |
||
175 |
" |
|
176 |
optimize |
|
104 | 177 |
(a == b) not -> (a ~~ b) |
178 |
(a ~~ b) not -> (a == b) |
|
0 | 179 |
" |
180 |
(selector == #not) ifTrue:[ |
|
104 | 181 |
(receiver class == BinaryNode) ifTrue:[ |
182 |
(receiver selector == #==) ifTrue:[ |
|
183 |
(BinaryNode receiver:(receiver receiver) |
|
184 |
selector:#~~ |
|
185 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler. |
|
186 |
^ self |
|
187 |
]. |
|
188 |
(receiver selector == #~~) ifTrue:[ |
|
189 |
(BinaryNode receiver:(receiver receiver) |
|
190 |
selector:#== |
|
191 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler. |
|
192 |
^ self |
|
193 |
] |
|
194 |
] |
|
0 | 195 |
]. |
196 |
||
110 | 197 |
" |
198 |
optimize |
|
199 |
Float pi |
|
200 |
" |
|
201 |
(selector == #pi) ifTrue:[ |
|
202 |
(receiver isGlobal) ifTrue:[ |
|
203 |
receiver name = 'Float' ifTrue:[ |
|
204 |
aCompiler addLiteral:#Float; addLiteral:selector. |
|
205 |
(ConstantNode type:#Float value:(Float pi)) |
|
206 |
codeOn:aStream inBlock:b for:aCompiler. |
|
207 |
^ self |
|
208 |
] |
|
209 |
] |
|
210 |
]. |
|
211 |
||
104 | 212 |
^ super codeOn:aStream inBlock:b for:aCompiler |
0 | 213 |
! ! |
214 |
||
140 | 215 |
!UnaryNode methodsFor:'evaluating'! |
216 |
||
217 |
evaluate |
|
218 |
"evaluate the expression represented by the receiver" |
|
219 |
||
220 |
receiver isSuper ifTrue:[ |
|
221 |
^ super evaluate |
|
222 |
]. |
|
223 |
^ (receiver evaluate) perform:selector |
|
224 |
! ! |
|
225 |
||
0 | 226 |
!UnaryNode methodsFor:'printing'! |
227 |
||
228 |
printOn:aStream indent:i |
|
11 | 229 |
"prettyprint the expression represented by the receiver" |
230 |
||
0 | 231 |
|needParen| |
232 |
||
233 |
needParen := false. |
|
234 |
receiver isMessage ifTrue:[ |
|
104 | 235 |
receiver isUnaryMessage ifFalse:[ |
236 |
needParen := true |
|
237 |
]. |
|
0 | 238 |
]. |
239 |
needParen ifTrue:[ |
|
104 | 240 |
aStream nextPutAll:'(' |
0 | 241 |
]. |
242 |
receiver printOn:aStream. |
|
243 |
needParen ifTrue:[ |
|
104 | 244 |
aStream nextPutAll:') ' |
0 | 245 |
]. |
246 |
aStream space. |
|
247 |
selector printString printOn:aStream. |
|
248 |
" aStream space. " |
|
249 |
! ! |
|
140 | 250 |
|
251 |
!UnaryNode methodsFor:'queries'! |
|
252 |
||
253 |
isUnaryMessage |
|
254 |
"return true, if this node is one for a unary message" |
|
255 |
^ true |
|
256 |
! ! |
|
257 |
||
148 | 258 |
!UnaryNode class methodsFor:'documentation'! |
259 |
||
260 |
version |
|
263 | 261 |
^ '$Header: /cvs/stx/stx/libcomp/UnaryNode.st,v 1.22 1996-04-25 17:08:29 cg Exp $' |
148 | 262 |
! ! |