author | Claus Gittinger <cg@exept.de> |
Tue, 04 Apr 2006 14:25:12 +0200 | |
changeset 1734 | fb882528e19b |
parent 1589 | e50e9f5bf31b |
child 1777 | 242e89fa2170 |
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)" |
1533 | 178 |
^ ParseErrorNode errorString:'error occured while 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 |
||
1502
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
196 |
|selectorSymbol| |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
197 |
|
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
198 |
selectorSymbol := selector asSymbolIfInterned. |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
199 |
selectorSymbol notNil ifTrue:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
200 |
((selectorSymbol == #self) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
201 |
(selectorSymbol == #super) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
202 |
(selectorSymbol == #thisContext) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
203 |
(selectorSymbol == #nil) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
204 |
(selectorSymbol == #true) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
205 |
(selectorSymbol == #false) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
206 |
(Smalltalk includesKey:selectorSymbol)]]]]]]) ifTrue:[ |
1734 | 207 |
^ 'funny selector (known as global); possible missing ''.'' or keyword-colon.' |
1502
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
208 |
]. |
0 | 209 |
]. |
17 | 210 |
|
904 | 211 |
" |
212 |
more to come |
|
213 |
... |
|
214 |
" |
|
215 |
||
216 |
^ super plausibilityCheck |
|
0 | 217 |
! ! |
218 |
||
219 |
!UnaryNode methodsFor:'code generation'! |
|
220 |
||
104 | 221 |
codeOn:aStream inBlock:b for:aCompiler |
11 | 222 |
"append bytecode for the receiver to aStream." |
223 |
||
626 | 224 |
|rSel notSelector| |
225 |
||
11 | 226 |
" |
227 |
optimize |
|
626 | 228 |
(a == b) not -> (a ~~ b) |
229 |
(a ~~ b) not -> (a == b) |
|
0 | 230 |
" |
231 |
(selector == #not) ifTrue:[ |
|
626 | 232 |
(receiver class == BinaryNode) ifTrue:[ |
233 |
((rSel := receiver selector) == #==) ifTrue:[ |
|
234 |
notSelector := #~~ |
|
235 |
] ifFalse:[ |
|
236 |
(rSel == #~~) ifTrue:[ |
|
237 |
notSelector := #== |
|
238 |
] |
|
239 |
]. |
|
240 |
notSelector notNil ifTrue:[ |
|
1465
2b2eea66362a
ensure literals of optimized messages are in lit-array
Claus Gittinger <cg@exept.de>
parents:
1387
diff
changeset
|
241 |
aCompiler addLiteral:selector; addLiteral:rSel. |
2b2eea66362a
ensure literals of optimized messages are in lit-array
Claus Gittinger <cg@exept.de>
parents:
1387
diff
changeset
|
242 |
|
626 | 243 |
(BinaryNode receiver:(receiver receiver) |
244 |
selector:notSelector |
|
245 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler. |
|
246 |
^ self |
|
247 |
] |
|
248 |
] |
|
0 | 249 |
]. |
250 |
||
110 | 251 |
" |
252 |
optimize |
|
626 | 253 |
Float pi |
110 | 254 |
" |
255 |
(selector == #pi) ifTrue:[ |
|
626 | 256 |
(receiver isGlobal) ifTrue:[ |
257 |
receiver name = 'Float' ifTrue:[ |
|
258 |
aCompiler addLiteral:#Float; addLiteral:selector. |
|
259 |
(ConstantNode type:#Float value:(Float pi)) |
|
260 |
codeOn:aStream inBlock:b for:aCompiler. |
|
261 |
^ self |
|
262 |
] |
|
263 |
] |
|
110 | 264 |
]. |
265 |
||
104 | 266 |
^ super codeOn:aStream inBlock:b for:aCompiler |
626 | 267 |
|
268 |
"Modified: 23.10.1997 / 02:04:51 / cg" |
|
0 | 269 |
! ! |
270 |
||
1035 | 271 |
!UnaryNode methodsFor:'evaluation'! |
140 | 272 |
|
1384 | 273 |
evaluateIn:anEnvironment |
140 | 274 |
"evaluate the expression represented by the receiver" |
275 |
||
1500
3c4cb012b24b
Lazy conversion of selector strings to symbols.
Stefan Vogel <sv@exept.de>
parents:
1465
diff
changeset
|
276 |
selector := selector asSymbol. |
140 | 277 |
receiver isSuper ifTrue:[ |
1384 | 278 |
^ super evaluateIn:anEnvironment |
140 | 279 |
]. |
1384 | 280 |
^ (receiver evaluateIn:anEnvironment) perform:selector |
140 | 281 |
! ! |
282 |
||
1080 | 283 |
!UnaryNode methodsFor:'printing & storing'! |
0 | 284 |
|
1589 | 285 |
printOn:aStream indent:i |
11 | 286 |
"prettyprint the expression represented by the receiver" |
1589 | 287 |
|
288 |
receiver printOn:aStream indent:i parenthized:(receiver precedence < self precedence). |
|
0 | 289 |
aStream space. |
290 |
selector printString printOn:aStream. |
|
1589 | 291 |
|
292 |
"Modified: / 20-04-2005 / 14:36:26 / cg" |
|
0 | 293 |
! ! |
140 | 294 |
|
295 |
!UnaryNode methodsFor:'queries'! |
|
296 |
||
1120 | 297 |
isConstant |
298 |
"return true, if this node evaluates to a constant value" |
|
299 |
||
300 |
"/ not worth it ... |
|
301 |
"/ (and might lead to trouble whenever a common bytecode standard |
|
302 |
"/ is ever available) |
|
303 |
"/ |
|
304 |
"/ receiver isGlobal ifTrue:[ |
|
305 |
"/ receiver name = 'Smalltalk' ifTrue:[ |
|
306 |
"/ selector == #isSmalltalkX ifTrue:[ |
|
307 |
"/ ^ true |
|
308 |
"/ ]. |
|
309 |
"/ selector == #isSqueak ifTrue:[ |
|
310 |
"/ ^ true |
|
311 |
"/ ]. |
|
312 |
"/ selector == #isVisualWorks ifTrue:[ |
|
313 |
"/ ^ true |
|
314 |
"/ ]. |
|
315 |
"/ selector == #isVisualAge ifTrue:[ |
|
316 |
"/ ^ true |
|
317 |
"/ ]. |
|
318 |
"/ ] |
|
319 |
"/ ]. |
|
320 |
||
321 |
^ false |
|
322 |
! |
|
323 |
||
140 | 324 |
isUnaryMessage |
325 |
"return true, if this node is one for a unary message" |
|
626 | 326 |
|
140 | 327 |
^ true |
626 | 328 |
|
329 |
"Modified: 23.10.1997 / 02:05:18 / cg" |
|
1589 | 330 |
! |
331 |
||
332 |
precedence |
|
333 |
^ 100 |
|
334 |
||
335 |
"Created: / 20-04-2005 / 14:10:34 / cg" |
|
140 | 336 |
! ! |
337 |
||
148 | 338 |
!UnaryNode class methodsFor:'documentation'! |
339 |
||
340 |
version |
|
1734 | 341 |
^ '$Header: /cvs/stx/stx/libcomp/UnaryNode.st,v 1.46 2006-04-04 12:25:12 cg Exp $' |
148 | 342 |
! ! |