author | Claus Gittinger <cg@exept.de> |
Thu, 31 Aug 2000 12:03:58 +0200 | |
changeset 1080 | bd3f19f6009a |
parent 1035 | 8848672cb893 |
child 1093 | 41e224d479b5 |
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 |
]. |
|
991
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
94 |
(globalName = 'Smalltalk') ifTrue:[ |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
95 |
( #( isSmalltalkX isVisualWorks isSqueak |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
96 |
isSmalltalkMT isDolphinSmalltalk isVisualAge |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
97 |
isSmalltalkV) includes:selector) |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
98 |
ifTrue:[ |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
99 |
(recVal respondsTo:selector) ifTrue:[ |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
100 |
canFold := true |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
101 |
] |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
102 |
] |
16411f243a75
compile-time-eval Smalltalk #isSmalltalkXXX messages.
Claus Gittinger <cg@exept.de>
parents:
904
diff
changeset
|
103 |
]. |
625 | 104 |
"/ no, this 'optimization' is not good - |
105 |
"/ if bytecode is transported to another machine. |
|
106 |
"/ However, the JIT compiler compensates for this ;-) |
|
107 |
"/ (globalName = 'SmallInteger') ifTrue:[ |
|
108 |
"/ ( #( minVal maxVal ) includes:selector) |
|
109 |
"/ ifTrue:[ |
|
110 |
"/ (recVal respondsTo:selector) ifTrue:[ |
|
111 |
"/ canFold := true |
|
112 |
"/ ] |
|
113 |
"/ ] |
|
114 |
"/ ] |
|
115 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
116 |
|
625 | 117 |
r isConstant ifTrue:[ |
118 |
"check if we can do it ..." |
|
119 |
recVal := r evaluate. |
|
120 |
||
121 |
" |
|
122 |
we could do much more here - but then, we need a dependency from |
|
123 |
the folded selectors method to the method we generate code for ... |
|
124 |
limit optimizations to those that will never change |
|
125 |
(or, if you change them, it will crash badly anyway ...) |
|
126 |
" |
|
127 |
recVal respondsToArithmetic ifTrue:[ |
|
128 |
(#( negated abs asPoint degreesToRadians radiansToDegrees |
|
129 |
exp ln log sqrt reciprocal |
|
130 |
arcCos arcSin arcTan sin cos tan) includes:selector) |
|
131 |
ifTrue:[ |
|
132 |
canFold := true |
|
133 |
] |
|
134 |
]. |
|
135 |
recVal isCharacter ifTrue:[ |
|
136 |
(#( asciiValue asInteger digitValue) includes:selector) |
|
137 |
ifTrue:[ |
|
138 |
canFold := true |
|
139 |
] |
|
140 |
]. |
|
141 |
recVal isString ifTrue:[ |
|
142 |
(selector == #withCRs) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
143 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
144 |
and:[(folding >= #level2) or:[folding == #full]] |
803 | 145 |
]. |
146 |
(selector == #size) 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 >= #level1) or:[folding == #full]] |
625 | 149 |
] |
150 |
]. |
|
151 |
(recVal isMemberOf:Array) ifTrue:[ |
|
152 |
(#(asFloatArray asDoubleArray) includes:selector) ifTrue:[ |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
153 |
canFold := folding isSymbol |
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
154 |
and:[(folding >= #level2) or:[folding == #full]] |
625 | 155 |
] |
156 |
] |
|
157 |
] |
|
158 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
159 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
160 |
canFold ifTrue:[ |
625 | 161 |
(recVal respondsTo:selector) ifTrue:[ |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
162 |
SignalSet anySignal "Number domainErrorSignal" handle:[:ex | |
625 | 163 |
"in case of an error, abort fold and return original" |
164 |
ex return |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
165 |
] do:[ |
625 | 166 |
result := recVal perform:selector. |
167 |
^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result |
|
168 |
]. |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
169 |
"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
|
170 |
^ 'error occured when evaluating constant expression' |
625 | 171 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
172 |
]. |
0 | 173 |
]. |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
174 |
|
15 | 175 |
^ (self basicNew) receiver:r selector:selectorString args:nil lineno:0 |
625 | 176 |
|
993
902eec8d132b
oops - folding argument is not always a symbol.
Claus Gittinger <cg@exept.de>
parents:
991
diff
changeset
|
177 |
"Modified: / 16.11.1999 / 21:49:17 / cg" |
0 | 178 |
! ! |
179 |
||
180 |
!UnaryNode methodsFor:'checks'! |
|
181 |
||
182 |
plausibilityCheck |
|
183 |
"check for funny selector - careful to do string compare instead |
|
184 |
of symbol identity compare: I dont want to introduce these as symbols |
|
185 |
into the system (would make the '... is nowhere implemented' warning |
|
186 |
go away." |
|
187 |
||
188 |
((selector = 'self') or:[ |
|
189 |
(selector = 'super') or:[ |
|
17 | 190 |
(selector = 'thisContext') or:[ |
191 |
(selector = 'true') or:[ |
|
192 |
(selector = 'false') or:[ |
|
193 |
(Smalltalk includesKey:selector)]]]]]) ifTrue:[ |
|
904 | 194 |
^ 'funny selector; possible missing ''.'' or keyword' |
0 | 195 |
]. |
17 | 196 |
|
904 | 197 |
" |
198 |
more to come |
|
199 |
... |
|
200 |
" |
|
201 |
||
202 |
^ super plausibilityCheck |
|
0 | 203 |
! ! |
204 |
||
205 |
!UnaryNode methodsFor:'code generation'! |
|
206 |
||
104 | 207 |
codeOn:aStream inBlock:b for:aCompiler |
11 | 208 |
"append bytecode for the receiver to aStream." |
209 |
||
626 | 210 |
|rSel notSelector| |
211 |
||
867
7d20544c2c63
oops - late selector as symbol
Claus Gittinger <cg@exept.de>
parents:
861
diff
changeset
|
212 |
selector := selector asSymbol. |
7d20544c2c63
oops - late selector as symbol
Claus Gittinger <cg@exept.de>
parents:
861
diff
changeset
|
213 |
|
11 | 214 |
" |
215 |
optimize |
|
626 | 216 |
(a == b) not -> (a ~~ b) |
217 |
(a ~~ b) not -> (a == b) |
|
0 | 218 |
" |
219 |
(selector == #not) ifTrue:[ |
|
626 | 220 |
(receiver class == BinaryNode) ifTrue:[ |
221 |
((rSel := receiver selector) == #==) ifTrue:[ |
|
222 |
notSelector := #~~ |
|
223 |
] ifFalse:[ |
|
224 |
(rSel == #~~) ifTrue:[ |
|
225 |
notSelector := #== |
|
226 |
] |
|
227 |
]. |
|
228 |
notSelector notNil ifTrue:[ |
|
229 |
(BinaryNode receiver:(receiver receiver) |
|
230 |
selector:notSelector |
|
231 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler. |
|
232 |
^ self |
|
233 |
] |
|
234 |
] |
|
0 | 235 |
]. |
236 |
||
110 | 237 |
" |
238 |
optimize |
|
626 | 239 |
Float pi |
110 | 240 |
" |
241 |
(selector == #pi) ifTrue:[ |
|
626 | 242 |
(receiver isGlobal) ifTrue:[ |
243 |
receiver name = 'Float' ifTrue:[ |
|
244 |
aCompiler addLiteral:#Float; addLiteral:selector. |
|
245 |
(ConstantNode type:#Float value:(Float pi)) |
|
246 |
codeOn:aStream inBlock:b for:aCompiler. |
|
247 |
^ self |
|
248 |
] |
|
249 |
] |
|
110 | 250 |
]. |
251 |
||
104 | 252 |
^ super codeOn:aStream inBlock:b for:aCompiler |
626 | 253 |
|
254 |
"Modified: 23.10.1997 / 02:04:51 / cg" |
|
0 | 255 |
! ! |
256 |
||
1035 | 257 |
!UnaryNode methodsFor:'evaluation'! |
140 | 258 |
|
259 |
evaluate |
|
260 |
"evaluate the expression represented by the receiver" |
|
261 |
||
861
ccaa0ba5e7af
generate selector symbols late (when compiling or evaluating)
Claus Gittinger <cg@exept.de>
parents:
803
diff
changeset
|
262 |
selector := selector asSymbol. |
ccaa0ba5e7af
generate selector symbols late (when compiling or evaluating)
Claus Gittinger <cg@exept.de>
parents:
803
diff
changeset
|
263 |
|
140 | 264 |
receiver isSuper ifTrue:[ |
861
ccaa0ba5e7af
generate selector symbols late (when compiling or evaluating)
Claus Gittinger <cg@exept.de>
parents:
803
diff
changeset
|
265 |
^ super evaluate |
140 | 266 |
]. |
267 |
^ (receiver evaluate) perform:selector |
|
268 |
! ! |
|
269 |
||
1080 | 270 |
!UnaryNode methodsFor:'printing & storing'! |
0 | 271 |
|
272 |
printOn:aStream indent:i |
|
11 | 273 |
"prettyprint the expression represented by the receiver" |
274 |
||
0 | 275 |
|needParen| |
276 |
||
277 |
needParen := false. |
|
278 |
receiver isMessage ifTrue:[ |
|
104 | 279 |
receiver isUnaryMessage ifFalse:[ |
280 |
needParen := true |
|
281 |
]. |
|
0 | 282 |
]. |
283 |
needParen ifTrue:[ |
|
104 | 284 |
aStream nextPutAll:'(' |
0 | 285 |
]. |
286 |
receiver printOn:aStream. |
|
287 |
needParen ifTrue:[ |
|
104 | 288 |
aStream nextPutAll:') ' |
0 | 289 |
]. |
290 |
aStream space. |
|
291 |
selector printString printOn:aStream. |
|
292 |
" aStream space. " |
|
293 |
! ! |
|
140 | 294 |
|
295 |
!UnaryNode methodsFor:'queries'! |
|
296 |
||
297 |
isUnaryMessage |
|
298 |
"return true, if this node is one for a unary message" |
|
626 | 299 |
|
140 | 300 |
^ true |
626 | 301 |
|
302 |
"Modified: 23.10.1997 / 02:05:18 / cg" |
|
140 | 303 |
! ! |
304 |
||
148 | 305 |
!UnaryNode class methodsFor:'documentation'! |
306 |
||
307 |
version |
|
1080 | 308 |
^ '$Header: /cvs/stx/stx/libcomp/UnaryNode.st,v 1.33 2000-08-31 10:03:51 cg Exp $' |
148 | 309 |
! ! |