0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
0
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Compiler-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
UnaryNode comment:'
|
|
21 |
|
4
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
0
|
23 |
All Rights Reserved
|
|
24 |
|
11
|
25 |
$Header: /cvs/stx/stx/libcomp/UnaryNode.st,v 1.4 1994-01-09 21:30:25 claus Exp $
|
0
|
26 |
'!
|
|
27 |
|
|
28 |
!UnaryNode class methodsFor:'instance creation'!
|
|
29 |
|
|
30 |
receiver:r selector:s
|
11
|
31 |
"return a new UnaryNode for sending selector s to receiver r.
|
|
32 |
Fold constants."
|
|
33 |
|
0
|
34 |
^ self receiver:r selector:s fold:true
|
|
35 |
!
|
|
36 |
|
|
37 |
receiver:r selector:s fold:folding
|
11
|
38 |
"return a new UnaryNode for sending selector s to receiver r.
|
|
39 |
If folding is true, fold constant expressions."
|
|
40 |
|
0
|
41 |
|result recVal sym|
|
|
42 |
|
|
43 |
"
|
|
44 |
The constant folding code can usually not optimize things - this may change
|
|
45 |
when some kind of constant declaration is added to smalltalk.
|
|
46 |
"
|
|
47 |
folding ifTrue:[
|
|
48 |
"do constant folding ..."
|
|
49 |
r isConstant ifTrue:[
|
|
50 |
"check if we can do it ..."
|
|
51 |
recVal := r evaluate.
|
|
52 |
s knownAsSymbol ifTrue:[
|
|
53 |
(recVal respondsTo:sym) ifTrue:[
|
|
54 |
"
|
|
55 |
we could do much more here - but then, we need a dependency from
|
|
56 |
the folded selectors method to the method we generate code for ...
|
|
57 |
limit optimizations to those that will never change
|
|
58 |
(or, if you change them, it will crash badly anyway ...)
|
|
59 |
"
|
|
60 |
Number domainErrorSignal handle:[:ex |
|
|
61 |
ex return
|
|
62 |
] do:[
|
|
63 |
sym := s asSymbol.
|
|
64 |
recVal respondsToArithmetic ifTrue:[
|
|
65 |
(#( negated abs asPoint degreesToRadians radiansToDegrees
|
|
66 |
exp ln log sqrt reciprocal
|
|
67 |
arcCos arcSin arcTan sin cos tan) includes:sym)
|
|
68 |
ifTrue:[
|
|
69 |
result := recVal perform:sym.
|
|
70 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
71 |
value:result
|
|
72 |
]
|
|
73 |
].
|
|
74 |
(recVal isMemberOf:Character) ifTrue:[
|
|
75 |
(#( asciiValue asInteger digitValue) includes:sym)
|
|
76 |
ifTrue:[
|
|
77 |
result := recVal perform:sym.
|
|
78 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
79 |
value:result
|
|
80 |
]
|
|
81 |
].
|
|
82 |
(recVal isMemberOf:String) ifTrue:[
|
|
83 |
(sym == #withCRs) ifTrue:[
|
|
84 |
result := recVal perform:sym.
|
|
85 |
^ ConstantNode type:(ConstantNode typeOfConstant:result)
|
|
86 |
value:result
|
|
87 |
]
|
|
88 |
].
|
|
89 |
^ (self basicNew) receiver:r selector:s args:nil lineno:0
|
|
90 |
].
|
|
91 |
"when we reach here, something went wrong (something like 0.0 log)"
|
|
92 |
^ 'error occured when evaluating constant expression'
|
|
93 |
]
|
|
94 |
]
|
|
95 |
]
|
|
96 |
].
|
|
97 |
^ (self basicNew) receiver:r selector:s args:nil lineno:0
|
|
98 |
! !
|
|
99 |
|
|
100 |
!UnaryNode methodsFor:'queries'!
|
|
101 |
|
|
102 |
isUnaryMessage
|
11
|
103 |
"return true, if this node is one for a unary message"
|
0
|
104 |
^ true
|
|
105 |
! !
|
|
106 |
|
|
107 |
!UnaryNode methodsFor:'checks'!
|
|
108 |
|
|
109 |
plausibilityCheck
|
|
110 |
"check for funny selector - careful to do string compare instead
|
|
111 |
of symbol identity compare: I dont want to introduce these as symbols
|
|
112 |
into the system (would make the '... is nowhere implemented' warning
|
|
113 |
go away."
|
|
114 |
|
|
115 |
((selector = 'self') or:[
|
|
116 |
(selector = 'super') or:[
|
|
117 |
(Smalltalk includesKey:selector)]]) ifTrue:[
|
|
118 |
^ 'funny selector; possible missing ''.'' or keyword'
|
|
119 |
].
|
3
|
120 |
"more to come ..."
|
0
|
121 |
^ nil
|
|
122 |
! !
|
|
123 |
|
|
124 |
!UnaryNode methodsFor:'evaluating'!
|
|
125 |
|
|
126 |
evaluate
|
11
|
127 |
"evaluate the expression represented by the receiver"
|
|
128 |
|
0
|
129 |
^ (receiver evaluate) perform:selector
|
|
130 |
! !
|
|
131 |
|
|
132 |
!UnaryNode methodsFor:'code generation'!
|
|
133 |
|
|
134 |
codeOn:aStream inBlock:b
|
11
|
135 |
"append bytecode for the receiver to aStream."
|
|
136 |
|
|
137 |
"
|
|
138 |
optimize
|
0
|
139 |
(a == b) not -> (a ~~ b)
|
|
140 |
(a ~~ b) not -> (a == b)
|
|
141 |
"
|
|
142 |
(selector == #not) ifTrue:[
|
|
143 |
(receiver class == BinaryNode) ifTrue:[
|
|
144 |
(receiver selector == #==) ifTrue:[
|
|
145 |
(BinaryNode receiver:(receiver receiver)
|
|
146 |
selector:#~~
|
|
147 |
arg:(receiver arg)) codeOn:aStream inBlock:b.
|
|
148 |
^ self
|
|
149 |
].
|
|
150 |
(receiver selector == #~~) ifTrue:[
|
|
151 |
(BinaryNode receiver:(receiver receiver)
|
|
152 |
selector:#==
|
|
153 |
arg:(receiver arg)) codeOn:aStream inBlock:b.
|
|
154 |
^ self
|
|
155 |
]
|
|
156 |
]
|
|
157 |
].
|
|
158 |
|
|
159 |
^ super codeOn:aStream inBlock:b
|
|
160 |
! !
|
|
161 |
|
|
162 |
!UnaryNode methodsFor:'printing'!
|
|
163 |
|
|
164 |
printOn:aStream indent:i
|
11
|
165 |
"prettyprint the expression represented by the receiver"
|
|
166 |
|
0
|
167 |
|needParen|
|
|
168 |
|
|
169 |
needParen := false.
|
|
170 |
receiver isMessage ifTrue:[
|
|
171 |
receiver isUnaryMessage ifFalse:[
|
|
172 |
needParen := true
|
|
173 |
].
|
|
174 |
].
|
|
175 |
needParen ifTrue:[
|
|
176 |
aStream nextPutAll:'('
|
|
177 |
].
|
|
178 |
receiver printOn:aStream.
|
|
179 |
needParen ifTrue:[
|
|
180 |
aStream nextPutAll:') '
|
|
181 |
].
|
|
182 |
aStream space.
|
|
183 |
selector printString printOn:aStream.
|
|
184 |
" aStream space. "
|
|
185 |
! !
|