author | Claus Gittinger <cg@exept.de> |
Tue, 29 Oct 2002 10:39:37 +0100 | |
changeset 1322 | da25e31f423c |
parent 1311 | 42182a1fbe77 |
child 1384 | 0db9682870d9 |
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 |
||
1080 | 13 |
"{ Package: 'stx:libcomp' }" |
14 |
||
0 | 15 |
MessageNode subclass:#UnaryNode |
261 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'System-Compiler-Support' |
|
0 | 20 |
! |
21 |
||
20 | 22 |
!UnaryNode class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
104 | 27 |
All Rights Reserved |
20 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
node for parse-trees, representing unary messages |
|
261 | 41 |
This is a helper class for the compiler. |
263 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
20 | 45 |
" |
46 |
! ! |
|
0 | 47 |
|
48 |
!UnaryNode class methodsFor:'instance creation'! |
|
49 |
||
50 |
receiver:r selector:s |
|
11 | 51 |
"return a new UnaryNode for sending selector s to receiver r. |
52 |
Fold constants." |
|
53 |
||
991
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
54 |
^ self receiver:r selector:s fold:true |
0 | 55 |
! |
56 |
||
15 | 57 |
receiver:r selector:selectorString fold:folding |
58 |
"return a new UnaryNode for sending selector selectorString to receiver r. |
|
11 | 59 |
If folding is true, fold constant expressions." |
60 |
||
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
61 |
|result recVal selector canFold globalName| |
0 | 62 |
|
63 |
" |
|
64 |
The constant folding code can usually not optimize things - this may change |
|
65 |
when some kind of constant declaration is added to smalltalk. |
|
66 |
" |
|
212
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 |
canFold := false. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
69 |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
70 |
(folding notNil and:[folding ~~ false]) ifTrue:[ |
625 | 71 |
selector := selectorString asSymbolIfInterned. |
72 |
selector notNil ifTrue:[ |
|
73 |
"/ |
|
74 |
"/ do constant folding ... |
|
75 |
"/ |
|
76 |
r isGlobal ifTrue:[ |
|
77 |
globalName := r name. |
|
78 |
recVal := r evaluate. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
79 |
|
625 | 80 |
(globalName = 'Character') ifTrue:[ |
81 |
( #( tab cr space backspace esc ) includes:selector) |
|
82 |
ifTrue:[ |
|
83 |
canFold := true |
|
84 |
] |
|
85 |
]. |
|
86 |
(globalName = 'Float') ifTrue:[ |
|
87 |
( #( pi unity zero ) includes:selector) |
|
88 |
ifTrue:[ |
|
89 |
(recVal respondsTo:selector) ifTrue:[ |
|
90 |
canFold := true |
|
91 |
] |
|
92 |
] |
|
93 |
]. |
|
1144 | 94 |
"/ no, this 'optimization' is not good - |
95 |
"/ if bytecode is transported to another machine. |
|
96 |
"/ However, the JIT compiler compensates for this ;-) |
|
97 |
"/ (globalName = 'Smalltalk') ifTrue:[ |
|
98 |
"/ ( #( isSmalltalkX isVisualWorks isSqueak |
|
99 |
"/ isSmalltalkMT isDolphinSmalltalk isVisualAge |
|
100 |
"/ isSmalltalkV) includes:selector) |
|
101 |
"/ ifTrue:[ |
|
102 |
"/ (recVal respondsTo:selector) ifTrue:[ |
|
103 |
"/ canFold := true |
|
104 |
"/ ] |
|
105 |
"/ ] |
|
106 |
"/ ]. |
|
107 |
||
625 | 108 |
"/ no, this 'optimization' is not good - |
109 |
"/ if bytecode is transported to another machine. |
|
110 |
"/ However, the JIT compiler compensates for this ;-) |
|
111 |
"/ (globalName = 'SmallInteger') ifTrue:[ |
|
112 |
"/ ( #( minVal maxVal ) includes:selector) |
|
113 |
"/ ifTrue:[ |
|
114 |
"/ (recVal respondsTo:selector) ifTrue:[ |
|
115 |
"/ canFold := true |
|
116 |
"/ ] |
|
117 |
"/ ] |
|
118 |
"/ ] |
|
119 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
120 |
|
625 | 121 |
r isConstant ifTrue:[ |
122 |
"check if we can do it ..." |
|
123 |
recVal := r evaluate. |
|
124 |
||
125 |
" |
|
126 |
we could do much more here - but then, we need a dependency from |
|
127 |
the folded selectors method to the method we generate code for ... |
|
128 |
limit optimizations to those that will never change |
|
129 |
(or, if you change them, it will crash badly anyway ...) |
|
130 |
" |
|
131 |
recVal respondsToArithmetic ifTrue:[ |
|
132 |
(#( negated abs asPoint degreesToRadians radiansToDegrees |
|
133 |
exp ln log sqrt reciprocal |
|
134 |
arcCos arcSin arcTan sin cos tan) includes:selector) |
|
135 |
ifTrue:[ |
|
136 |
canFold := true |
|
137 |
] |
|
138 |
]. |
|
139 |
recVal isCharacter ifTrue:[ |
|
1093 | 140 |
(#( asciiValue asInteger digitValue asString) includes:selector) |
625 | 141 |
ifTrue:[ |
142 |
canFold := true |
|
143 |
] |
|
144 |
]. |
|
145 |
recVal isString ifTrue:[ |
|
146 |
(selector == #withCRs) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
147 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
148 |
and:[(folding >= #level2) or:[folding == #full]] |
803 | 149 |
]. |
150 |
(selector == #size) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
151 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
152 |
and:[(folding >= #level1) or:[folding == #full]] |
1093 | 153 |
]. |
154 |
(selector == #asSymbol) ifTrue:[ |
|
155 |
canFold := folding isSymbol |
|
156 |
and:[(folding >= #level1) or:[folding == #full]] |
|
157 |
]. |
|
625 | 158 |
]. |
159 |
(recVal isMemberOf:Array) ifTrue:[ |
|
160 |
(#(asFloatArray asDoubleArray) includes:selector) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
161 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
162 |
and:[(folding >= #level2) or:[folding == #full]] |
625 | 163 |
] |
164 |
] |
|
165 |
] |
|
166 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
167 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
168 |
canFold ifTrue:[ |
625 | 169 |
(recVal respondsTo:selector) ifTrue:[ |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
170 |
SignalSet anySignal "Number domainErrorSignal" handle:[:ex | |
625 | 171 |
"in case of an error, abort fold and return original" |
172 |
ex return |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
173 |
] do:[ |
625 | 174 |
result := recVal perform:selector. |
175 |
^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result |
|
176 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
177 |
"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
|
178 |
^ 'error occured when evaluating constant expression' |
625 | 179 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
180 |
]. |
0 | 181 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
182 |
|
15 | 183 |
^ (self basicNew) receiver:r selector:selectorString args:nil lineno:0 |
625 | 184 |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
185 |
"Modified: / 16.11.1999 / 21:49:17 / cg" |
0 | 186 |
! ! |
187 |
||
188 |
!UnaryNode methodsFor:'checks'! |
|
189 |
||
190 |
plausibilityCheck |
|
191 |
"check for funny selector - careful to do string compare instead |
|
192 |
of symbol identity compare: I dont want to introduce these as symbols |
|
193 |
into the system (would make the '... is nowhere implemented' warning |
|
194 |
go away." |
|
195 |
||
1197 | 196 |
selector := selector asSymbol. |
197 |
||
198 |
((selector == #'self') or:[ |
|
199 |
(selector == #'super') or:[ |
|
200 |
(selector == #'thisContext') or:[ |
|
1311 | 201 |
(selector == #'nil') or:[ |
1197 | 202 |
(selector == #'true') or:[ |
203 |
(selector == #'false') or:[ |
|
1311 | 204 |
(Smalltalk includesKey:selector)]]]]]]) ifTrue:[ |
904 | 205 |
^ 'funny selector; possible missing ''.'' or keyword' |
0 | 206 |
]. |
17 | 207 |
|
904 | 208 |
" |
209 |
more to come |
|
210 |
... |
|
211 |
" |
|
212 |
||
213 |
^ super plausibilityCheck |
|
0 | 214 |
! ! |
215 |
||
216 |
!UnaryNode methodsFor:'code generation'! |
|
217 |
||
104 | 218 |
codeOn:aStream inBlock:b for:aCompiler |
11 | 219 |
"append bytecode for the receiver to aStream." |
220 |
||
626 | 221 |
|rSel notSelector| |
222 |
||
867
7d20544c2c63
oops - late selector as symbol
Claus Gittinger <cg@exept.de>
parents:
861
diff
changeset
|
223 |
selector := selector asSymbol. |
7d20544c2c63
oops - late selector as symbol
Claus Gittinger <cg@exept.de>
parents:
861
diff
changeset
|
224 |
|
11 | 225 |
" |
226 |
optimize |
|
626 | 227 |
(a == b) not -> (a ~~ b) |
228 |
(a ~~ b) not -> (a == b) |
|
0 | 229 |
" |
230 |
(selector == #not) ifTrue:[ |
|
626 | 231 |
(receiver class == BinaryNode) ifTrue:[ |
232 |
((rSel := receiver selector) == #==) ifTrue:[ |
|
233 |
notSelector := #~~ |
|
234 |
] ifFalse:[ |
|
235 |
(rSel == #~~) ifTrue:[ |
|
236 |
notSelector := #== |
|
237 |
] |
|
238 |
]. |
|
239 |
notSelector notNil ifTrue:[ |
|
240 |
(BinaryNode receiver:(receiver receiver) |
|
241 |
selector:notSelector |
|
242 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler. |
|
243 |
^ self |
|
244 |
] |
|
245 |
] |
|
0 | 246 |
]. |
247 |
||
110 | 248 |
" |
249 |
optimize |
|
626 | 250 |
Float pi |
110 | 251 |
" |
252 |
(selector == #pi) ifTrue:[ |
|
626 | 253 |
(receiver isGlobal) ifTrue:[ |
254 |
receiver name = 'Float' ifTrue:[ |
|
255 |
aCompiler addLiteral:#Float; addLiteral:selector. |
|
256 |
(ConstantNode type:#Float value:(Float pi)) |
|
257 |
codeOn:aStream inBlock:b for:aCompiler. |
|
258 |
^ self |
|
259 |
] |
|
260 |
] |
|
110 | 261 |
]. |
262 |
||
104 | 263 |
^ super codeOn:aStream inBlock:b for:aCompiler |
626 | 264 |
|
265 |
"Modified: 23.10.1997 / 02:04:51 / cg" |
|
0 | 266 |
! ! |
267 |
||
1035 | 268 |
!UnaryNode methodsFor:'evaluation'! |
140 | 269 |
|
270 |
evaluate |
|
271 |
"evaluate the expression represented by the receiver" |
|
272 |
||
861
ccaa0ba5e7af
generate selector symbols late (when compiling or evaluating)
Claus Gittinger <cg@exept.de>
parents:
803
diff
changeset
|
273 |
selector := selector asSymbol. |
ccaa0ba5e7af
generate selector symbols late (when compiling or evaluating)
Claus Gittinger <cg@exept.de>
parents:
803
diff
changeset
|
274 |
|
140 | 275 |
receiver isSuper ifTrue:[ |
861
ccaa0ba5e7af
generate selector symbols late (when compiling or evaluating)
Claus Gittinger <cg@exept.de>
parents:
803
diff
changeset
|
276 |
^ super evaluate |
140 | 277 |
]. |
278 |
^ (receiver evaluate) perform:selector |
|
279 |
! ! |
|
280 |
||
1080 | 281 |
!UnaryNode methodsFor:'printing & storing'! |
0 | 282 |
|
283 |
printOn:aStream indent:i |
|
11 | 284 |
"prettyprint the expression represented by the receiver" |
285 |
||
0 | 286 |
|needParen| |
287 |
||
288 |
needParen := false. |
|
289 |
receiver isMessage ifTrue:[ |
|
104 | 290 |
receiver isUnaryMessage ifFalse:[ |
291 |
needParen := true |
|
292 |
]. |
|
0 | 293 |
]. |
294 |
needParen ifTrue:[ |
|
104 | 295 |
aStream nextPutAll:'(' |
0 | 296 |
]. |
297 |
receiver printOn:aStream. |
|
298 |
needParen ifTrue:[ |
|
104 | 299 |
aStream nextPutAll:') ' |
0 | 300 |
]. |
301 |
aStream space. |
|
302 |
selector printString printOn:aStream. |
|
303 |
" aStream space. " |
|
304 |
! ! |
|
140 | 305 |
|
306 |
!UnaryNode methodsFor:'queries'! |
|
307 |
||
1120 | 308 |
isConstant |
309 |
"return true, if this node evaluates to a constant value" |
|
310 |
||
311 |
"/ not worth it ... |
|
312 |
"/ (and might lead to trouble whenever a common bytecode standard |
|
313 |
"/ is ever available) |
|
314 |
"/ |
|
315 |
"/ receiver isGlobal ifTrue:[ |
|
316 |
"/ receiver name = 'Smalltalk' ifTrue:[ |
|
317 |
"/ selector == #isSmalltalkX ifTrue:[ |
|
318 |
"/ ^ true |
|
319 |
"/ ]. |
|
320 |
"/ selector == #isSqueak ifTrue:[ |
|
321 |
"/ ^ true |
|
322 |
"/ ]. |
|
323 |
"/ selector == #isVisualWorks ifTrue:[ |
|
324 |
"/ ^ true |
|
325 |
"/ ]. |
|
326 |
"/ selector == #isVisualAge ifTrue:[ |
|
327 |
"/ ^ true |
|
328 |
"/ ]. |
|
329 |
"/ ] |
|
330 |
"/ ]. |
|
331 |
||
332 |
^ false |
|
333 |
! |
|
334 |
||
140 | 335 |
isUnaryMessage |
336 |
"return true, if this node is one for a unary message" |
|
626 | 337 |
|
140 | 338 |
^ true |
626 | 339 |
|
340 |
"Modified: 23.10.1997 / 02:05:18 / cg" |
|
140 | 341 |
! ! |
342 |
||
148 | 343 |
!UnaryNode class methodsFor:'documentation'! |
344 |
||
345 |
version |
|
1311 | 346 |
^ '$Header: /cvs/stx/stx/libcomp/UnaryNode.st,v 1.38 2002-08-19 16:17:09 cg Exp $' |
148 | 347 |
! ! |