author | Claus Gittinger <cg@exept.de> |
Wed, 28 May 2008 11:42:30 +0200 | |
changeset 2098 | a03891b6fc30 |
parent 2094 | 87201834a364 |
child 2111 | ecfcbcbe299f |
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 |
" |
|
1080 | 12 |
"{ Package: 'stx:libcomp' }" |
13 |
||
0 | 14 |
MessageNode subclass:#UnaryNode |
261 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
0 | 19 |
! |
20 |
||
20 | 21 |
!UnaryNode class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
104 | 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 |
documentation |
|
38 |
" |
|
39 |
node for parse-trees, representing unary messages |
|
261 | 40 |
This is a helper class for the compiler. |
263 | 41 |
|
42 |
[author:] |
|
43 |
Claus Gittinger |
|
20 | 44 |
" |
45 |
! ! |
|
0 | 46 |
|
47 |
!UnaryNode class methodsFor:'instance creation'! |
|
48 |
||
49 |
receiver:r selector:s |
|
11 | 50 |
"return a new UnaryNode for sending selector s to receiver r. |
51 |
Fold constants." |
|
52 |
||
991
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
53 |
^ self receiver:r selector:s fold:true |
0 | 54 |
! |
55 |
||
15 | 56 |
receiver:r selector:selectorString fold:folding |
57 |
"return a new UnaryNode for sending selector selectorString to receiver r. |
|
11 | 58 |
If folding is true, fold constant expressions." |
59 |
||
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
60 |
|result recVal selector canFold globalName| |
0 | 61 |
|
62 |
" |
|
63 |
The constant folding code can usually not optimize things - this may change |
|
64 |
when some kind of constant declaration is added to smalltalk. |
|
65 |
" |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
66 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
67 |
canFold := false. |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
68 |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
69 |
(folding notNil and:[folding ~~ false]) ifTrue:[ |
625 | 70 |
selector := selectorString asSymbolIfInterned. |
71 |
selector notNil ifTrue:[ |
|
72 |
"/ |
|
73 |
"/ do constant folding ... |
|
74 |
"/ |
|
75 |
r isGlobal ifTrue:[ |
|
76 |
globalName := r name. |
|
77 |
recVal := r evaluate. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
78 |
|
625 | 79 |
(globalName = 'Character') ifTrue:[ |
2001 | 80 |
( #( tab cr lf return space backspace esc ) includes:selector) |
625 | 81 |
ifTrue:[ |
82 |
canFold := true |
|
83 |
] |
|
84 |
]. |
|
85 |
(globalName = 'Float') ifTrue:[ |
|
2001 | 86 |
( #( pi e NaN unity zero ) includes:selector) |
625 | 87 |
ifTrue:[ |
88 |
(recVal respondsTo:selector) ifTrue:[ |
|
89 |
canFold := true |
|
90 |
] |
|
91 |
] |
|
92 |
]. |
|
2001 | 93 |
(globalName = 'String') ifTrue:[ |
94 |
( #( cr crlf lf ) includes:selector) |
|
95 |
ifTrue:[ |
|
96 |
(recVal respondsTo:selector) ifTrue:[ |
|
97 |
canFold := true |
|
98 |
] |
|
99 |
] |
|
100 |
]. |
|
101 |
||
1144 | 102 |
"/ no, this 'optimization' is not good - |
103 |
"/ if bytecode is transported to another machine. |
|
104 |
"/ However, the JIT compiler compensates for this ;-) |
|
105 |
"/ (globalName = 'Smalltalk') ifTrue:[ |
|
106 |
"/ ( #( isSmalltalkX isVisualWorks isSqueak |
|
107 |
"/ isSmalltalkMT isDolphinSmalltalk isVisualAge |
|
108 |
"/ isSmalltalkV) includes:selector) |
|
109 |
"/ ifTrue:[ |
|
110 |
"/ (recVal respondsTo:selector) ifTrue:[ |
|
111 |
"/ canFold := true |
|
112 |
"/ ] |
|
113 |
"/ ] |
|
114 |
"/ ]. |
|
115 |
||
625 | 116 |
"/ no, this 'optimization' is not good - |
117 |
"/ if bytecode is transported to another machine. |
|
118 |
"/ However, the JIT compiler compensates for this ;-) |
|
119 |
"/ (globalName = 'SmallInteger') ifTrue:[ |
|
120 |
"/ ( #( minVal maxVal ) includes:selector) |
|
121 |
"/ ifTrue:[ |
|
122 |
"/ (recVal respondsTo:selector) ifTrue:[ |
|
123 |
"/ canFold := true |
|
124 |
"/ ] |
|
125 |
"/ ] |
|
126 |
"/ ] |
|
127 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
128 |
|
625 | 129 |
r isConstant ifTrue:[ |
130 |
"check if we can do it ..." |
|
131 |
recVal := r evaluate. |
|
132 |
||
133 |
" |
|
134 |
we could do much more here - but then, we need a dependency from |
|
135 |
the folded selectors method to the method we generate code for ... |
|
136 |
limit optimizations to those that will never change |
|
137 |
(or, if you change them, it will crash badly anyway ...) |
|
138 |
" |
|
139 |
recVal respondsToArithmetic ifTrue:[ |
|
140 |
(#( negated abs asPoint degreesToRadians radiansToDegrees |
|
141 |
exp ln log sqrt reciprocal |
|
142 |
arcCos arcSin arcTan sin cos tan) includes:selector) |
|
143 |
ifTrue:[ |
|
144 |
canFold := true |
|
145 |
] |
|
146 |
]. |
|
147 |
recVal isCharacter ifTrue:[ |
|
1093 | 148 |
(#( asciiValue asInteger digitValue asString) includes:selector) |
625 | 149 |
ifTrue:[ |
150 |
canFold := true |
|
151 |
] |
|
152 |
]. |
|
153 |
recVal isString ifTrue:[ |
|
154 |
(selector == #withCRs) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
155 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
156 |
and:[(folding >= #level2) or:[folding == #full]] |
803 | 157 |
]. |
158 |
(selector == #size) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
159 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
160 |
and:[(folding >= #level1) or:[folding == #full]] |
1093 | 161 |
]. |
162 |
(selector == #asSymbol) ifTrue:[ |
|
163 |
canFold := folding isSymbol |
|
164 |
and:[(folding >= #level1) or:[folding == #full]] |
|
165 |
]. |
|
625 | 166 |
]. |
167 |
(recVal isMemberOf:Array) ifTrue:[ |
|
168 |
(#(asFloatArray asDoubleArray) includes:selector) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
169 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
170 |
and:[(folding >= #level2) or:[folding == #full]] |
625 | 171 |
] |
172 |
] |
|
173 |
] |
|
174 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
175 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
176 |
canFold ifTrue:[ |
625 | 177 |
(recVal respondsTo:selector) ifTrue:[ |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
178 |
SignalSet anySignal "Number domainErrorSignal" handle:[:ex | |
625 | 179 |
"in case of an error, abort fold and return original" |
180 |
ex return |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
181 |
] do:[ |
625 | 182 |
result := recVal perform:selector. |
183 |
^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result |
|
184 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
185 |
"when we reach here, something went wrong (something like 0.0 log)" |
1533 | 186 |
^ ParseErrorNode errorString:'error occured while evaluating constant expression' |
625 | 187 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
188 |
]. |
0 | 189 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
190 |
|
15 | 191 |
^ (self basicNew) receiver:r selector:selectorString args:nil lineno:0 |
625 | 192 |
|
2001 | 193 |
"Modified: / 05-03-2007 / 15:11:26 / cg" |
0 | 194 |
! ! |
195 |
||
196 |
!UnaryNode methodsFor:'checks'! |
|
197 |
||
2094 | 198 |
plausibilityCheckIn:aParser |
0 | 199 |
"check for funny selector - careful to do string compare instead |
200 |
of symbol identity compare: I dont want to introduce these as symbols |
|
201 |
into the system (would make the '... is nowhere implemented' warning |
|
202 |
go away." |
|
203 |
||
1502
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
204 |
|selectorSymbol| |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
205 |
|
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
206 |
selectorSymbol := selector asSymbolIfInterned. |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
207 |
selectorSymbol notNil ifTrue:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
208 |
((selectorSymbol == #self) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
209 |
(selectorSymbol == #super) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
210 |
(selectorSymbol == #thisContext) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
211 |
(selectorSymbol == #nil) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
212 |
(selectorSymbol == #true) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
213 |
(selectorSymbol == #false) or:[ |
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
214 |
(Smalltalk includesKey:selectorSymbol)]]]]]]) ifTrue:[ |
2094 | 215 |
(aParser alreadyWarnedUnimplementedSelectors includes:selectorSymbol) ifFalse:[ |
216 |
aParser alreadyWarnedUnimplementedSelectors add:selectorSymbol. |
|
217 |
^ 'funny selector: ',selectorSymbol allBold,' (known as global); possible missing ''.'' or keyword-colon.' |
|
218 |
]. |
|
1502
de3c4e208b9f
Avoid 'is not a symbol in globalKnown' debug messages from librun
Stefan Vogel <sv@exept.de>
parents:
1500
diff
changeset
|
219 |
]. |
0 | 220 |
]. |
17 | 221 |
|
904 | 222 |
" |
223 |
more to come |
|
224 |
... |
|
225 |
" |
|
226 |
||
2094 | 227 |
^ super plausibilityCheckIn:aParser |
1777 | 228 |
|
229 |
"Modified: / 16-07-2006 / 16:16:25 / cg" |
|
0 | 230 |
! ! |
231 |
||
232 |
!UnaryNode methodsFor:'code generation'! |
|
233 |
||
104 | 234 |
codeOn:aStream inBlock:b for:aCompiler |
11 | 235 |
"append bytecode for the receiver to aStream." |
236 |
||
626 | 237 |
|rSel notSelector| |
238 |
||
11 | 239 |
" |
240 |
optimize |
|
626 | 241 |
(a == b) not -> (a ~~ b) |
242 |
(a ~~ b) not -> (a == b) |
|
0 | 243 |
" |
244 |
(selector == #not) ifTrue:[ |
|
626 | 245 |
(receiver class == BinaryNode) ifTrue:[ |
246 |
((rSel := receiver selector) == #==) ifTrue:[ |
|
247 |
notSelector := #~~ |
|
248 |
] ifFalse:[ |
|
249 |
(rSel == #~~) ifTrue:[ |
|
250 |
notSelector := #== |
|
251 |
] |
|
252 |
]. |
|
253 |
notSelector notNil ifTrue:[ |
|
1465
2b2eea66362a
ensure literals of optimized messages are in lit-array
Claus Gittinger <cg@exept.de>
parents:
1387
diff
changeset
|
254 |
aCompiler addLiteral:selector; addLiteral:rSel. |
2b2eea66362a
ensure literals of optimized messages are in lit-array
Claus Gittinger <cg@exept.de>
parents:
1387
diff
changeset
|
255 |
|
1998 | 256 |
(BinaryNode |
257 |
receiver:(receiver receiver) |
|
258 |
selector:notSelector |
|
259 |
arg:(receiver arg)) |
|
260 |
codeOn:aStream |
|
261 |
inBlock:b |
|
262 |
for:aCompiler. |
|
626 | 263 |
^ self |
264 |
] |
|
265 |
] |
|
0 | 266 |
]. |
267 |
||
104 | 268 |
^ super codeOn:aStream inBlock:b for:aCompiler |
626 | 269 |
|
2001 | 270 |
"Modified: / 05-03-2007 / 15:11:35 / cg" |
0 | 271 |
! ! |
272 |
||
1035 | 273 |
!UnaryNode methodsFor:'evaluation'! |
140 | 274 |
|
1384 | 275 |
evaluateIn:anEnvironment |
140 | 276 |
"evaluate the expression represented by the receiver" |
277 |
||
2036
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
278 |
|r| |
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
279 |
|
1500
3c4cb012b24b
Lazy conversion of selector strings to symbols.
Stefan Vogel <sv@exept.de>
parents:
1465
diff
changeset
|
280 |
selector := selector asSymbol. |
140 | 281 |
receiver isSuper ifTrue:[ |
1384 | 282 |
^ super evaluateIn:anEnvironment |
140 | 283 |
]. |
2036
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
284 |
r := receiver evaluateIn:anEnvironment. |
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
285 |
selector == #class ifTrue:[ |
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
286 |
^ r class. |
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
287 |
]. |
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
288 |
^ r perform:selector |
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
289 |
|
989b8836fc32
do #class inline to avoid #perform:
Claus Gittinger <cg@exept.de>
parents:
2001
diff
changeset
|
290 |
"Modified: / 04-06-2007 / 17:46:31 / cg" |
140 | 291 |
! ! |
292 |
||
1080 | 293 |
!UnaryNode methodsFor:'printing & storing'! |
0 | 294 |
|
1589 | 295 |
printOn:aStream indent:i |
11 | 296 |
"prettyprint the expression represented by the receiver" |
1589 | 297 |
|
298 |
receiver printOn:aStream indent:i parenthized:(receiver precedence < self precedence). |
|
0 | 299 |
aStream space. |
300 |
selector printString printOn:aStream. |
|
1589 | 301 |
|
302 |
"Modified: / 20-04-2005 / 14:36:26 / cg" |
|
0 | 303 |
! ! |
140 | 304 |
|
305 |
!UnaryNode methodsFor:'queries'! |
|
306 |
||
307 |
isUnaryMessage |
|
308 |
"return true, if this node is one for a unary message" |
|
626 | 309 |
|
140 | 310 |
^ true |
626 | 311 |
|
312 |
"Modified: 23.10.1997 / 02:05:18 / cg" |
|
1589 | 313 |
! |
314 |
||
315 |
precedence |
|
316 |
^ 100 |
|
317 |
||
318 |
"Created: / 20-04-2005 / 14:10:34 / cg" |
|
140 | 319 |
! ! |
320 |
||
148 | 321 |
!UnaryNode class methodsFor:'documentation'! |
322 |
||
323 |
version |
|
2094 | 324 |
^ '$Header: /cvs/stx/stx/libcomp/UnaryNode.st,v 1.51 2008-05-26 08:35:08 cg Exp $' |
148 | 325 |
! ! |