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 |
|
0
|
48 |
^ self receiver:r selector:s fold:true
|
|
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 |
|
15
|
55 |
|result recVal selector|
|
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 |
"
|
|
61 |
folding ifTrue:[
|
104
|
62 |
"do constant folding ..."
|
84
|
63 |
r isGlobal ifTrue:[
|
|
64 |
(r name = 'Character') ifTrue:[
|
|
65 |
recVal := r evaluate.
|
|
66 |
selector := selectorString asSymbolIfInterned.
|
|
67 |
selector notNil ifTrue:[
|
104
|
68 |
(#( tab cr space) includes:selector)
|
|
69 |
ifTrue:[
|
|
70 |
(recVal respondsTo:selector) ifTrue:[
|
|
71 |
result := recVal perform:selector.
|
|
72 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
73 |
value:result
|
|
74 |
]
|
84
|
75 |
]
|
|
76 |
]
|
|
77 |
]
|
|
78 |
].
|
104
|
79 |
r isConstant ifTrue:[
|
|
80 |
"check if we can do it ..."
|
|
81 |
recVal := r evaluate.
|
|
82 |
selector := selectorString asSymbolIfInterned.
|
|
83 |
selector notNil ifTrue:[
|
|
84 |
(recVal respondsTo:selector) ifTrue:[
|
|
85 |
"
|
|
86 |
we could do much more here - but then, we need a dependency from
|
|
87 |
the folded selectors method to the method we generate code for ...
|
|
88 |
limit optimizations to those that will never change
|
|
89 |
(or, if you change them, it will crash badly anyway ...)
|
|
90 |
"
|
|
91 |
SignalSet anySignal "Number domainErrorSignal" handle:[:ex |
|
|
92 |
"in case of an error, abort fold and return original"
|
|
93 |
ex return
|
|
94 |
] do:[
|
|
95 |
recVal respondsToArithmetic ifTrue:[
|
|
96 |
(#( negated abs asPoint degreesToRadians radiansToDegrees
|
|
97 |
exp ln log sqrt reciprocal
|
|
98 |
arcCos arcSin arcTan sin cos tan) includes:selector)
|
|
99 |
ifTrue:[
|
|
100 |
result := recVal perform:selector.
|
|
101 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
102 |
value:result
|
|
103 |
]
|
|
104 |
].
|
|
105 |
recVal isCharacter ifTrue:[
|
|
106 |
(#( asciiValue asInteger digitValue) includes:selector)
|
|
107 |
ifTrue:[
|
|
108 |
result := recVal perform:selector.
|
|
109 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
110 |
value:result
|
|
111 |
]
|
|
112 |
].
|
|
113 |
recVal isString ifTrue:[
|
|
114 |
(selector == #withCRs) ifTrue:[
|
|
115 |
result := recVal perform:selector.
|
|
116 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
117 |
value:result
|
|
118 |
]
|
|
119 |
].
|
|
120 |
(recVal isMemberOf:Array) ifTrue:[
|
|
121 |
(#(asFloatArray asDoubleArray) includes:selector) ifTrue:[
|
|
122 |
result := recVal perform:selector.
|
|
123 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
124 |
value:result
|
|
125 |
]
|
|
126 |
].
|
|
127 |
^ (self basicNew) receiver:r selector:selector args:nil lineno:0
|
|
128 |
].
|
|
129 |
"when we reach here, something went wrong (something like 0.0 log)"
|
|
130 |
^ 'error occured when evaluating constant expression'
|
|
131 |
]
|
|
132 |
]
|
|
133 |
]
|
0
|
134 |
].
|
15
|
135 |
^ (self basicNew) receiver:r selector:selectorString args:nil lineno:0
|
0
|
136 |
! !
|
|
137 |
|
|
138 |
!UnaryNode methodsFor:'checks'!
|
|
139 |
|
|
140 |
plausibilityCheck
|
|
141 |
"check for funny selector - careful to do string compare instead
|
|
142 |
of symbol identity compare: I dont want to introduce these as symbols
|
|
143 |
into the system (would make the '... is nowhere implemented' warning
|
|
144 |
go away."
|
|
145 |
|
|
146 |
((selector = 'self') or:[
|
|
147 |
(selector = 'super') or:[
|
17
|
148 |
(selector = 'thisContext') or:[
|
|
149 |
(selector = 'true') or:[
|
|
150 |
(selector = 'false') or:[
|
|
151 |
(Smalltalk includesKey:selector)]]]]]) ifTrue:[
|
104
|
152 |
^ 'funny selector; possible missing ''.'' or keyword'
|
0
|
153 |
].
|
17
|
154 |
|
3
|
155 |
"more to come ..."
|
0
|
156 |
^ nil
|
|
157 |
! !
|
|
158 |
|
|
159 |
!UnaryNode methodsFor:'code generation'!
|
|
160 |
|
104
|
161 |
codeOn:aStream inBlock:b for:aCompiler
|
11
|
162 |
"append bytecode for the receiver to aStream."
|
|
163 |
|
|
164 |
"
|
|
165 |
optimize
|
104
|
166 |
(a == b) not -> (a ~~ b)
|
|
167 |
(a ~~ b) not -> (a == b)
|
0
|
168 |
"
|
|
169 |
(selector == #not) ifTrue:[
|
104
|
170 |
(receiver class == BinaryNode) ifTrue:[
|
|
171 |
(receiver selector == #==) ifTrue:[
|
|
172 |
(BinaryNode receiver:(receiver receiver)
|
|
173 |
selector:#~~
|
|
174 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler.
|
|
175 |
^ self
|
|
176 |
].
|
|
177 |
(receiver selector == #~~) ifTrue:[
|
|
178 |
(BinaryNode receiver:(receiver receiver)
|
|
179 |
selector:#==
|
|
180 |
arg:(receiver arg)) codeOn:aStream inBlock:b for:aCompiler.
|
|
181 |
^ self
|
|
182 |
]
|
|
183 |
]
|
0
|
184 |
].
|
|
185 |
|
110
|
186 |
"
|
|
187 |
optimize
|
|
188 |
Float pi
|
|
189 |
"
|
|
190 |
(selector == #pi) ifTrue:[
|
|
191 |
(receiver isGlobal) ifTrue:[
|
|
192 |
receiver name = 'Float' ifTrue:[
|
|
193 |
aCompiler addLiteral:#Float; addLiteral:selector.
|
|
194 |
(ConstantNode type:#Float value:(Float pi))
|
|
195 |
codeOn:aStream inBlock:b for:aCompiler.
|
|
196 |
^ self
|
|
197 |
]
|
|
198 |
]
|
|
199 |
].
|
|
200 |
|
104
|
201 |
^ super codeOn:aStream inBlock:b for:aCompiler
|
0
|
202 |
! !
|
|
203 |
|
140
|
204 |
!UnaryNode methodsFor:'evaluating'!
|
|
205 |
|
|
206 |
evaluate
|
|
207 |
"evaluate the expression represented by the receiver"
|
|
208 |
|
|
209 |
receiver isSuper ifTrue:[
|
|
210 |
^ super evaluate
|
|
211 |
].
|
|
212 |
^ (receiver evaluate) perform:selector
|
|
213 |
! !
|
|
214 |
|
0
|
215 |
!UnaryNode methodsFor:'printing'!
|
|
216 |
|
|
217 |
printOn:aStream indent:i
|
11
|
218 |
"prettyprint the expression represented by the receiver"
|
|
219 |
|
0
|
220 |
|needParen|
|
|
221 |
|
|
222 |
needParen := false.
|
|
223 |
receiver isMessage ifTrue:[
|
104
|
224 |
receiver isUnaryMessage ifFalse:[
|
|
225 |
needParen := true
|
|
226 |
].
|
0
|
227 |
].
|
|
228 |
needParen ifTrue:[
|
104
|
229 |
aStream nextPutAll:'('
|
0
|
230 |
].
|
|
231 |
receiver printOn:aStream.
|
|
232 |
needParen ifTrue:[
|
104
|
233 |
aStream nextPutAll:') '
|
0
|
234 |
].
|
|
235 |
aStream space.
|
|
236 |
selector printString printOn:aStream.
|
|
237 |
" aStream space. "
|
|
238 |
! !
|
140
|
239 |
|
|
240 |
!UnaryNode methodsFor:'queries'!
|
|
241 |
|
|
242 |
isUnaryMessage
|
|
243 |
"return true, if this node is one for a unary message"
|
|
244 |
^ true
|
|
245 |
! !
|
|
246 |
|
148
|
247 |
!UnaryNode class methodsFor:'documentation'!
|
|
248 |
|
|
249 |
version
|
|
250 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/UnaryNd.st,v 1.19 1995-12-03 12:16:31 cg Exp $'
|
|
251 |
! !
|