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