0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
47
|
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 |
ParseNode subclass:#MessageNode
|
|
14 |
instanceVariableNames:'receiver selector argArray lineNr'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Compiler-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
MessageNode comment:'
|
4
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
47
|
22 |
All Rights Reserved
|
33
|
23 |
|
75
|
24 |
$Header: /cvs/stx/stx/libcomp/MessageNode.st,v 1.21 1995-02-28 21:01:58 claus Exp $
|
20
|
25 |
'!
|
0
|
26 |
|
20
|
27 |
!MessageNode class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
47
|
32 |
All Rights Reserved
|
20
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
75
|
45 |
$Header: /cvs/stx/stx/libcomp/MessageNode.st,v 1.21 1995-02-28 21:01:58 claus Exp $
|
20
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
node for parse-trees, representing message sends
|
|
52 |
"
|
|
53 |
! !
|
0
|
54 |
|
|
55 |
!MessageNode class methodsFor:'instance creation'!
|
|
56 |
|
|
57 |
receiver:recNode selector:selectorString
|
|
58 |
^ (self basicNew) receiver:recNode selector:selectorString args:nil lineno:0
|
|
59 |
!
|
|
60 |
|
|
61 |
receiver:recNode selector:selectorString arg:argNode
|
|
62 |
^ self receiver:recNode selector:selectorString arg:argNode fold:true
|
|
63 |
!
|
|
64 |
|
|
65 |
receiver:recNode selector:selectorString arg:argNode fold:folding
|
53
|
66 |
|result recVal argVal selector canFold|
|
0
|
67 |
|
20
|
68 |
"
|
|
69 |
The constant folding code can usually not optimize much
|
|
70 |
- this may change when some kind of constant/macro declaration is added to smalltalk.
|
|
71 |
"
|
0
|
72 |
folding ifTrue:[
|
47
|
73 |
"do constant folding ..."
|
|
74 |
(recNode isConstant and:[argNode isConstant]) ifTrue:[
|
|
75 |
"check if we can do it ..."
|
63
|
76 |
selector := selectorString asSymbolIfInterned.
|
|
77 |
selector notNil ifTrue:[
|
47
|
78 |
recVal := recNode evaluate.
|
|
79 |
(recVal respondsTo:selector) ifTrue:[
|
53
|
80 |
canFold := false.
|
47
|
81 |
"
|
|
82 |
we could do much more here - but then, we need a dependency from
|
|
83 |
the folded selectors method to the method we generate code for ...
|
|
84 |
limit optimizations to those that will never change
|
|
85 |
(or - if you change them - you will crash so bad ...)
|
|
86 |
"
|
|
87 |
argVal := argNode evaluate.
|
|
88 |
(recVal respondsToArithmetic and:[argVal respondsToArithmetic]) ifTrue:[
|
|
89 |
(#( @ + - * / // \\ min: max: quo:) includes:selector) ifTrue:[
|
|
90 |
(#( / // \\ ) includes:selector) ifTrue:[
|
|
91 |
argVal = 0 ifTrue:[
|
|
92 |
^ 'division by zero in constant expression'
|
|
93 |
].
|
|
94 |
].
|
53
|
95 |
canFold := true
|
47
|
96 |
]
|
|
97 |
].
|
|
98 |
(recVal isMemberOf:String) ifTrue:[
|
53
|
99 |
(argVal isInteger and:[selector == #at:]) ifTrue:[
|
|
100 |
canFold := true
|
47
|
101 |
].
|
53
|
102 |
((argVal isMemberOf:String) and:[selector == #',']) ifTrue:[
|
|
103 |
canFold := true
|
47
|
104 |
]
|
53
|
105 |
].
|
|
106 |
canFold ifTrue:[
|
|
107 |
(SignalSet anySignal catch:[
|
|
108 |
result := recVal perform:selector with:argVal.
|
|
109 |
]) ifTrue:[
|
|
110 |
^ 'error in constant expression'
|
|
111 |
].
|
|
112 |
^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
|
47
|
113 |
]
|
|
114 |
]
|
|
115 |
]
|
53
|
116 |
].
|
|
117 |
"
|
|
118 |
the folloing optimization cannot be done (although it would be nice)
|
|
119 |
since the array may be modified later.
|
|
120 |
"
|
|
121 |
"/ (recNode isGlobal and:[argNode isConstant])ifTrue:[
|
|
122 |
"/ selectorString knownAsSymbol ifTrue:[
|
|
123 |
"/ selector := selectorString asSymbol.
|
|
124 |
"/ recVal := recNode evaluate.
|
|
125 |
"/ (recVal respondsTo:selector) ifTrue:[
|
|
126 |
"/ (recVal == FloatArray) ifTrue:[
|
|
127 |
"/ argVal := argNode evaluate.
|
|
128 |
"/ (argVal isMemberOf:Array) ifTrue:[
|
|
129 |
"/ (SignalSet anySignal catch:[
|
|
130 |
"/ result := recVal perform:selector with:argVal with:argVal.
|
|
131 |
"/ ]) ifTrue:[
|
|
132 |
"/ ^ 'error in constant expression'
|
|
133 |
"/ ].
|
|
134 |
"/ ^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
|
|
135 |
"/ ]
|
|
136 |
"/ ]
|
|
137 |
"/ ]
|
|
138 |
"/ ]
|
|
139 |
"/ ]
|
0
|
140 |
].
|
|
141 |
^ (self basicNew) receiver:recNode selector:selectorString args:(Array with:argNode) lineno:0
|
|
142 |
!
|
|
143 |
|
20
|
144 |
receiver:recNode selector:selectorString arg1:argNode1 arg2:argNode2 fold:folding
|
|
145 |
|result recVal argVal selector|
|
|
146 |
|
|
147 |
"
|
|
148 |
This is just a demonstration - of how complex constants can be folded.
|
|
149 |
This was inspired by some discussion in c.l.s about enhancing the language - I prefer
|
|
150 |
enhancing the compiler ....
|
|
151 |
The following optimization will convert '#(...) with:#(...) collect:[...]' into an array constant,
|
|
152 |
allowing a constant arrays of complex objects.
|
|
153 |
|
|
154 |
Notice: this method is normally disabled - its just a demo after all.
|
|
155 |
"
|
|
156 |
folding ifTrue:[
|
47
|
157 |
"do constant folding ..."
|
|
158 |
(recNode isConstant and:[argNode1 isConstant]) ifTrue:[
|
|
159 |
"check if we can do it ..."
|
63
|
160 |
selector := selectorString asSymbolIfInterned.
|
|
161 |
selector notNil ifTrue:[
|
47
|
162 |
recVal := recNode evaluate.
|
|
163 |
(recVal respondsTo:selector) ifTrue:[
|
|
164 |
"
|
|
165 |
we could do much more here - but then, we need a dependency from
|
|
166 |
the folded selectors method to the method we generate code for ...
|
|
167 |
limit optimizations to those that will never change
|
|
168 |
(or - if you change them - you will crash so bad ...)
|
|
169 |
"
|
|
170 |
argVal := argNode1 evaluate.
|
|
171 |
((recVal isMemberOf:Array) and:[argVal isMemberOf:Array]) ifTrue:[
|
|
172 |
(selector == #with:collect:) ifTrue:[
|
|
173 |
(argNode2 isMemberOf:BlockNode) ifTrue:[
|
|
174 |
(SignalSet anySignal catch:[
|
|
175 |
result := recVal perform:selector with:argVal with:(argNode2 evaluate).
|
|
176 |
]) ifTrue:[
|
|
177 |
^ 'error in constant expression'
|
|
178 |
].
|
|
179 |
^ ConstantNode type:(ConstantNode typeOfConstant:result) value:result
|
|
180 |
]
|
|
181 |
]
|
|
182 |
]
|
|
183 |
]
|
|
184 |
]
|
|
185 |
]
|
20
|
186 |
].
|
|
187 |
^ (self basicNew) receiver:recNode selector:selectorString args:(Array with:argNode1 with:argNode2) lineno:0
|
|
188 |
!
|
|
189 |
|
0
|
190 |
receiver:recNode selector:selectorString args:anArray
|
|
191 |
^ self receiver:recNode selector:selectorString args:anArray fold:true
|
|
192 |
!
|
|
193 |
|
|
194 |
receiver:recNode selector:selectorString args:argArray fold:folding
|
20
|
195 |
folding ifTrue:[
|
47
|
196 |
(argArray size == 1) ifTrue:[
|
|
197 |
^ self receiver:recNode selector:selectorString arg:(argArray at:1) fold:true
|
|
198 |
].
|
20
|
199 |
|
47
|
200 |
"uncomment the follwoing for a nice array initializer optimization ..."
|
20
|
201 |
"/ (argArray size == 2) ifTrue:[
|
|
202 |
"/ ^ self receiver:recNode selector:selectorString arg1:(argArray at:1) arg2:(argArray at:2) fold:true
|
|
203 |
"/ ].
|
0
|
204 |
].
|
|
205 |
^ (self basicNew) receiver:recNode selector:selectorString args:argArray lineno:0
|
|
206 |
! !
|
|
207 |
|
|
208 |
!MessageNode methodsFor:'accessing'!
|
|
209 |
|
|
210 |
receiver:r selector:s args:a lineno:l
|
|
211 |
receiver := r.
|
|
212 |
selector := s asSymbol.
|
|
213 |
argArray := a.
|
|
214 |
lineNr := l
|
|
215 |
!
|
|
216 |
|
|
217 |
receiver
|
|
218 |
^ receiver
|
|
219 |
!
|
|
220 |
|
|
221 |
selector
|
|
222 |
^ selector
|
|
223 |
!
|
|
224 |
|
|
225 |
args
|
|
226 |
^ argArray
|
|
227 |
!
|
|
228 |
|
|
229 |
arg1
|
|
230 |
^ argArray at:1
|
|
231 |
!
|
|
232 |
|
6
|
233 |
lineNumber
|
|
234 |
^ lineNr
|
|
235 |
!
|
|
236 |
|
0
|
237 |
lineNumber:num
|
|
238 |
lineNr := num
|
|
239 |
! !
|
|
240 |
|
3
|
241 |
!MessageNode methodsFor:'queries'!
|
0
|
242 |
|
|
243 |
isMessage
|
|
244 |
^ true
|
3
|
245 |
! !
|
|
246 |
|
|
247 |
!MessageNode class methodsFor:'queries'!
|
0
|
248 |
|
6
|
249 |
hasLineNumber:sel
|
|
250 |
"return true, if special send code needs lineNr"
|
|
251 |
|
|
252 |
(sel == #==) ifTrue:[^ false].
|
|
253 |
(sel == #~~) ifTrue:[^ false].
|
|
254 |
(sel == #class) ifTrue:[^ false].
|
|
255 |
^ true
|
|
256 |
!
|
|
257 |
|
0
|
258 |
isBuiltInUnarySelector:sel
|
19
|
259 |
"return true, if unary selector sel is built-in.
|
|
260 |
(i.e. there is a single bytecode for it)"
|
0
|
261 |
|
|
262 |
(sel == #peek) ifTrue:[^ true].
|
|
263 |
(sel == #value) ifTrue:[^ true].
|
|
264 |
(sel == #next) ifTrue:[^ true].
|
|
265 |
(sel == #class) ifTrue:[^ true].
|
|
266 |
(sel == #size) ifTrue:[^ true].
|
|
267 |
(sel == #x) ifTrue:[^ true].
|
|
268 |
(sel == #y) ifTrue:[^ true].
|
|
269 |
(sel == #width) ifTrue:[^ true].
|
|
270 |
(sel == #height) ifTrue:[^ true].
|
|
271 |
(sel == #origin) ifTrue:[^ true].
|
|
272 |
(sel == #extent) ifTrue:[^ true].
|
6
|
273 |
(sel == #asInteger) ifTrue:[^ true].
|
|
274 |
(sel == #rounded) ifTrue:[^ true].
|
0
|
275 |
^ false
|
|
276 |
!
|
|
277 |
|
|
278 |
isBuiltIn1ArgSelector:sel
|
19
|
279 |
"return true, if selector sel is built-in.
|
|
280 |
(i.e. there is a single bytecode for it)"
|
0
|
281 |
|
|
282 |
(sel == #at:) ifTrue:[^ true].
|
|
283 |
(sel == #value:) ifTrue:[^ true].
|
|
284 |
(sel == #bitAnd:) ifTrue:[^ true].
|
|
285 |
(sel == #bitOr:) ifTrue:[^ true].
|
|
286 |
^ false
|
|
287 |
!
|
|
288 |
|
|
289 |
isBuiltIn2ArgSelector:sel
|
19
|
290 |
"return true, if selector sel is built-in.
|
|
291 |
(i.e. there is a single bytecode for it)"
|
0
|
292 |
|
|
293 |
(sel == #at:put:) ifTrue:[^ true].
|
|
294 |
^ false
|
|
295 |
! !
|
|
296 |
|
|
297 |
!MessageNode methodsFor:'printing'!
|
|
298 |
|
|
299 |
printOn:aStream indent:i
|
|
300 |
|needParen selectorParts index index2 arg|
|
|
301 |
|
|
302 |
(#(whileTrue: whileFalse:) includes:selector) ifTrue:[
|
62
|
303 |
receiver isBlock ifTrue:[
|
47
|
304 |
^ self printWhileOn:aStream indent:i
|
|
305 |
].
|
0
|
306 |
].
|
|
307 |
|
|
308 |
index := 1.
|
|
309 |
selectorParts := OrderedCollection new.
|
|
310 |
[index == 0] whileFalse:[
|
47
|
311 |
index2 := selector indexOf:$: startingAt:index.
|
|
312 |
index2 ~~ 0 ifTrue:[
|
|
313 |
selectorParts add:(selector copyFrom:index to:index2).
|
|
314 |
index2 := index2 + 1
|
|
315 |
].
|
|
316 |
index := index2
|
0
|
317 |
].
|
|
318 |
|
|
319 |
needParen := false.
|
|
320 |
receiver isMessage ifTrue:[
|
47
|
321 |
receiver isUnaryMessage ifFalse:[
|
|
322 |
receiver isBinaryMessage ifFalse:[
|
|
323 |
needParen := true
|
|
324 |
].
|
|
325 |
].
|
0
|
326 |
].
|
|
327 |
needParen ifTrue:[
|
47
|
328 |
aStream nextPutAll:'('
|
0
|
329 |
].
|
|
330 |
receiver printOn:aStream indent:i.
|
|
331 |
needParen ifTrue:[
|
47
|
332 |
aStream nextPutAll:')'
|
0
|
333 |
].
|
|
334 |
|
|
335 |
1 to:(argArray size) do:[:argIndex |
|
47
|
336 |
aStream space.
|
|
337 |
(selectorParts at:argIndex) printOn:aStream.
|
|
338 |
aStream space.
|
|
339 |
arg := argArray at:argIndex.
|
|
340 |
needParen := false.
|
|
341 |
arg isMessage ifTrue:[
|
|
342 |
arg isBinaryMessage ifFalse:[
|
|
343 |
arg isUnaryMessage ifFalse:[
|
|
344 |
needParen := true
|
|
345 |
]
|
|
346 |
].
|
|
347 |
].
|
|
348 |
needParen ifTrue:[
|
|
349 |
aStream nextPutAll:'('
|
|
350 |
].
|
|
351 |
arg printOn:aStream indent:i.
|
|
352 |
needParen ifTrue:[
|
|
353 |
aStream nextPutAll:') '
|
|
354 |
].
|
0
|
355 |
]
|
|
356 |
!
|
|
357 |
|
|
358 |
printWhileOn:aStream indent:i
|
12
|
359 |
|needParen arg|
|
0
|
360 |
|
|
361 |
"special handling of whileTrue/whileFalse"
|
|
362 |
|
|
363 |
aStream nextPutAll:'['.
|
|
364 |
receiver statements printOn:aStream indent:i.
|
|
365 |
aStream nextPutAll:'] whileTrue: '.
|
|
366 |
|
|
367 |
arg := argArray at:1.
|
|
368 |
needParen := false.
|
|
369 |
arg isMessage ifTrue:[
|
47
|
370 |
arg isBinaryMessage ifFalse:[
|
|
371 |
arg isUnaryMessage ifFalse:[
|
|
372 |
needParen := true
|
|
373 |
]
|
|
374 |
].
|
0
|
375 |
].
|
|
376 |
needParen ifTrue:[
|
47
|
377 |
aStream nextPutAll:'('
|
0
|
378 |
].
|
|
379 |
arg printOn:aStream indent:i.
|
|
380 |
needParen ifTrue:[
|
47
|
381 |
aStream nextPutAll:') '
|
0
|
382 |
].
|
|
383 |
! !
|
|
384 |
|
|
385 |
!MessageNode methodsFor:'checks'!
|
|
386 |
|
|
387 |
plausibilityCheck
|
|
388 |
|rec arg operand|
|
|
389 |
|
3
|
390 |
"
|
|
391 |
it once took me almost an hour, to find a '==' which
|
|
392 |
should have been an '=' (you cannot compare floats with ==)
|
19
|
393 |
(well, I looked at the '==' at least 50 times -
|
|
394 |
- but didn't think about it ...).
|
|
395 |
thats reason enough to add this check here.
|
|
396 |
I will add more as heuristic knowledge increases ...
|
|
397 |
(send me comments on common programming errors ...)
|
|
398 |
"
|
|
399 |
|
|
400 |
"
|
|
401 |
check #== appled to Floats, Strings or Fractions
|
0
|
402 |
"
|
|
403 |
((selector == #==) or:[selector == #~~]) ifTrue:[
|
47
|
404 |
receiver isConstant ifTrue:[
|
|
405 |
rec := receiver evaluate.
|
|
406 |
((rec isMemberOf:String) or:[
|
|
407 |
(rec isMemberOf:Float) or:[
|
|
408 |
(rec isMemberOf:Fraction)]]) ifTrue:[
|
|
409 |
operand := rec
|
|
410 |
].
|
|
411 |
].
|
|
412 |
(argArray at:1) isConstant ifTrue:[
|
|
413 |
arg := (argArray at:1) evaluate.
|
|
414 |
((arg isMemberOf:String) or:[
|
|
415 |
(arg isMemberOf:Float) or:[
|
|
416 |
(arg isMemberOf:Fraction)]]) ifTrue:[
|
|
417 |
operand := arg
|
|
418 |
].
|
|
419 |
].
|
|
420 |
operand notNil ifTrue:[
|
|
421 |
(selector == #==) ifTrue:[
|
|
422 |
^ 'identity compare is unsafe here'
|
|
423 |
].
|
|
424 |
^ 'identity compare will usually return true here'
|
|
425 |
]
|
0
|
426 |
].
|
3
|
427 |
|
|
428 |
"
|
19
|
429 |
[...] ifTrue:...
|
3
|
430 |
an error often occuring when you are a beginner ...
|
|
431 |
"
|
|
432 |
((selector == #ifTrue:) or:[selector == #ifFalse:]) ifTrue:[
|
47
|
433 |
receiver isBlock ifTrue:[
|
|
434 |
(Block canUnderstand:selector) ifFalse:[
|
|
435 |
^ 'blocks usually do not respond to ' , selector , ' messages'
|
|
436 |
].
|
|
437 |
].
|
|
438 |
(argArray at:1) isBlock ifFalse:[
|
|
439 |
^ 'will fail at runtime, if argument to ' , selector , ' does not evaluate to a block'
|
|
440 |
]
|
3
|
441 |
].
|
19
|
442 |
((selector == #ifTrue:ifFalse:) or:[selector == #ifFalse:ifTrue:]) ifTrue:[
|
47
|
443 |
receiver isBlock ifTrue:[
|
|
444 |
(Block canUnderstand:selector) ifFalse:[
|
|
445 |
^ 'blocks usually do not respond to ' , selector , ' messages'
|
|
446 |
].
|
|
447 |
].
|
|
448 |
(argArray at:1) isBlock ifFalse:[
|
|
449 |
^ 'will fail at runtime, if 1st. argument to ' , selector , ' does not evaluate to a block'
|
|
450 |
].
|
|
451 |
(argArray at:2) isBlock ifFalse:[
|
|
452 |
^ 'will fail at runtime, if 2nd. argument to ' , selector , ' does not evaluate to a block'
|
|
453 |
]
|
3
|
454 |
].
|
19
|
455 |
|
|
456 |
"
|
|
457 |
(...) whileTrue:[
|
|
458 |
"
|
3
|
459 |
((selector == #whileTrue:) or:[selector == #whileFalse:]) ifTrue:[
|
47
|
460 |
receiver isBlock ifFalse:[
|
|
461 |
"
|
|
462 |
only warn, if code was originally parenthized
|
|
463 |
"
|
|
464 |
receiver parenthized ifTrue:[
|
|
465 |
^ 'will fail at runtime, if receiver of ' , selector , ' does not evaluate to a block'
|
|
466 |
]
|
|
467 |
].
|
|
468 |
(argArray at:1) isBlock ifFalse:[
|
|
469 |
^ 'will fail at runtime, if argument to ' , selector , ' does not evaluate to a block'
|
|
470 |
].
|
3
|
471 |
].
|
0
|
472 |
^ nil
|
|
473 |
! !
|
|
474 |
|
|
475 |
!MessageNode methodsFor:'evaluating'!
|
|
476 |
|
|
477 |
evaluate
|
13
|
478 |
|r nargs argValueArray class|
|
|
479 |
|
|
480 |
receiver isSuper ifTrue:[
|
47
|
481 |
r := receiver value.
|
49
|
482 |
receiver isHere ifTrue:[
|
|
483 |
class := receiver definingClass.
|
|
484 |
] ifFalse:[
|
|
485 |
class := receiver definingClass superclass.
|
|
486 |
].
|
47
|
487 |
argArray notNil ifTrue:[
|
|
488 |
argValueArray := argArray collect:[:arg | arg evaluate].
|
|
489 |
] ifFalse:[
|
|
490 |
argValueArray := #()
|
|
491 |
].
|
|
492 |
^ r perform:selector inClass:class withArguments:argValueArray
|
13
|
493 |
].
|
|
494 |
|
0
|
495 |
|
|
496 |
argArray isNil ifTrue:[
|
47
|
497 |
^ (receiver evaluate) perform:selector
|
0
|
498 |
].
|
|
499 |
nargs := argArray size.
|
|
500 |
(nargs == 1) ifTrue:[
|
47
|
501 |
^ (receiver evaluate) perform:selector with:(argArray at:1) evaluate
|
0
|
502 |
].
|
|
503 |
(nargs == 2) ifTrue:[
|
47
|
504 |
^ (receiver evaluate) perform:selector
|
|
505 |
with:(argArray at:1) evaluate
|
|
506 |
with:(argArray at:2) evaluate
|
0
|
507 |
].
|
|
508 |
(nargs == 3) ifTrue:[
|
47
|
509 |
^ (receiver evaluate) perform:selector
|
|
510 |
with:(argArray at:1) evaluate
|
|
511 |
with:(argArray at:2) evaluate
|
|
512 |
with:(argArray at:3) evaluate
|
0
|
513 |
].
|
|
514 |
r := receiver evaluate.
|
13
|
515 |
argValueArray := argArray collect:[:arg | arg evaluate].
|
0
|
516 |
^ r perform:selector withArguments:argValueArray
|
|
517 |
!
|
|
518 |
|
|
519 |
evaluateForCascade
|
13
|
520 |
|r nargs argValueArray class|
|
|
521 |
|
|
522 |
receiver isSuper ifTrue:[
|
47
|
523 |
r := receiver value.
|
49
|
524 |
receiver isHere ifTrue:[
|
|
525 |
class := receiver definingClass.
|
|
526 |
] ifFalse:[
|
|
527 |
class := receiver definingClass superclass.
|
|
528 |
].
|
47
|
529 |
argArray notNil ifTrue:[
|
|
530 |
argValueArray := argArray collect:[:arg | arg evaluate].
|
|
531 |
] ifFalse:[
|
|
532 |
argValueArray := #()
|
|
533 |
].
|
|
534 |
r perform:selector inClass:class withArguments:argValueArray.
|
|
535 |
^ r
|
13
|
536 |
].
|
0
|
537 |
|
|
538 |
r := receiver evaluate.
|
|
539 |
argArray isNil ifTrue:[
|
47
|
540 |
r perform:selector.
|
|
541 |
^ r
|
0
|
542 |
].
|
|
543 |
nargs := argArray size.
|
|
544 |
(nargs == 1) ifTrue:[
|
47
|
545 |
r perform:selector with:(argArray at:1) evaluate.
|
|
546 |
^ r
|
0
|
547 |
].
|
|
548 |
(nargs == 2) ifTrue:[
|
47
|
549 |
r perform:selector with:(argArray at:1) evaluate
|
|
550 |
with:(argArray at:2) evaluate.
|
|
551 |
^ r
|
0
|
552 |
].
|
|
553 |
(nargs == 3) ifTrue:[
|
47
|
554 |
r perform:selector with:(argArray at:1) evaluate
|
|
555 |
with:(argArray at:2) evaluate
|
|
556 |
with:(argArray at:3) evaluate.
|
|
557 |
^ r
|
0
|
558 |
].
|
13
|
559 |
argValueArray := argArray collect:[:arg | arg evaluate].
|
0
|
560 |
r perform:selector withArguments:argValueArray.
|
|
561 |
^ r
|
|
562 |
! !
|
|
563 |
|
|
564 |
!MessageNode methodsFor:'code generation'!
|
|
565 |
|
|
566 |
codeForSideEffectOn:aStream inBlock:b
|
|
567 |
self codeOn:aStream inBlock:b valueNeeded:false
|
|
568 |
!
|
|
569 |
|
|
570 |
codeOn:aStream inBlock:b
|
|
571 |
self codeOn:aStream inBlock:b valueNeeded:true
|
|
572 |
!
|
|
573 |
|
|
574 |
optimizedConditionFor:aReceiver with:aByteCode
|
|
575 |
|rec sel|
|
|
576 |
|
|
577 |
rec := aReceiver.
|
19
|
578 |
(rec isBlock "class == BlockNode") ifTrue:[
|
47
|
579 |
rec statements nextStatement isNil ifTrue:[
|
|
580 |
rec := rec statements expression
|
|
581 |
]
|
0
|
582 |
].
|
19
|
583 |
(rec isUnaryMessage "class == UnaryNode") ifTrue:[
|
47
|
584 |
sel := rec selector.
|
|
585 |
(sel == #isNil) ifTrue:[
|
|
586 |
(aByteCode == #trueJump) ifTrue:[^ #nilJump].
|
|
587 |
(aByteCode == #falseJump) ifTrue:[^ #notNilJump]
|
|
588 |
].
|
|
589 |
(sel == #notNil) ifTrue:[
|
|
590 |
(aByteCode == #trueJump) ifTrue:[^ #notNilJump].
|
|
591 |
(aByteCode == #falseJump) ifTrue:[^ #nilJump]
|
|
592 |
].
|
|
593 |
(sel == #not) ifTrue:[
|
|
594 |
(aByteCode == #trueJump) ifTrue:[^ #falseJump].
|
|
595 |
(aByteCode == #falseJump) ifTrue:[^ #trueJump]
|
|
596 |
].
|
|
597 |
^ nil
|
0
|
598 |
].
|
19
|
599 |
(rec isBinaryMessage "class == BinaryNode") ifTrue:[
|
47
|
600 |
sel := rec selector.
|
|
601 |
rec arg1 isConstant ifTrue:[
|
|
602 |
(rec arg1 value == 0) ifTrue:[
|
|
603 |
(sel == #==) ifTrue:[
|
|
604 |
(aByteCode == #trueJump) ifTrue:[^ #zeroJump].
|
|
605 |
(aByteCode == #falseJump) ifTrue:[^ #notZeroJump]
|
|
606 |
].
|
|
607 |
(sel == #~~) ifTrue:[
|
|
608 |
(aByteCode == #falseJump) ifTrue:[^ #zeroJump].
|
|
609 |
(aByteCode == #trueJump) ifTrue:[^ #notZeroJump]
|
|
610 |
].
|
|
611 |
^ nil
|
|
612 |
]
|
|
613 |
].
|
|
614 |
(sel == #==) ifTrue:[
|
|
615 |
(aByteCode == #trueJump) ifTrue:[^ #eqJump].
|
|
616 |
(aByteCode == #falseJump) ifTrue:[^ #notEqJump]
|
|
617 |
].
|
|
618 |
(sel == #~~) ifTrue:[
|
|
619 |
(aByteCode == #falseJump) ifTrue:[^ #eqJump].
|
|
620 |
(aByteCode == #trueJump) ifTrue:[^ #notEqJump]
|
|
621 |
]
|
0
|
622 |
].
|
|
623 |
^ nil
|
|
624 |
!
|
|
625 |
|
|
626 |
codeWhileOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
627 |
"generate code for [...] whilexxx:[ ... ]"
|
|
628 |
|
|
629 |
|pos pos2 theReceiver theArg theByteCode optByteCode|
|
|
630 |
|
|
631 |
(selector == #whileTrue:) ifTrue:[
|
47
|
632 |
theByteCode := #falseJump
|
0
|
633 |
] ifFalse:[
|
47
|
634 |
theByteCode := #trueJump
|
0
|
635 |
].
|
|
636 |
|
|
637 |
theReceiver := receiver.
|
|
638 |
optByteCode := self optimizedConditionFor:theReceiver with:theByteCode.
|
|
639 |
optByteCode notNil ifTrue:[
|
47
|
640 |
((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
|
|
641 |
theArg := receiver statements expression arg1
|
|
642 |
].
|
|
643 |
theReceiver := receiver statements expression receiver.
|
|
644 |
theByteCode := optByteCode
|
0
|
645 |
].
|
|
646 |
|
|
647 |
valueNeeded ifTrue:[aStream nextPut:#pushNil].
|
|
648 |
pos := aStream position.
|
|
649 |
optByteCode notNil ifTrue:[
|
47
|
650 |
theReceiver codeOn:aStream inBlock:b.
|
|
651 |
theArg notNil ifTrue:[
|
|
652 |
theArg codeOn:aStream inBlock:b
|
|
653 |
]
|
0
|
654 |
] ifFalse:[
|
47
|
655 |
theReceiver codeInlineOn:aStream inBlock:b
|
0
|
656 |
].
|
53
|
657 |
|
|
658 |
(lineNr between:1 and:255) ifTrue:[
|
|
659 |
aStream nextPut:#lineno.
|
|
660 |
aStream nextPut:lineNr.
|
|
661 |
].
|
|
662 |
|
0
|
663 |
aStream nextPut:theByteCode.
|
|
664 |
pos2 := aStream position.
|
|
665 |
aStream nextPut:0.
|
|
666 |
valueNeeded ifTrue:[aStream nextPut:#drop].
|
|
667 |
(argArray at:1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
668 |
aStream nextPut:#jump.
|
|
669 |
aStream nextPut:pos.
|
|
670 |
(aStream contents) at:pos2 put:(aStream position)
|
|
671 |
!
|
|
672 |
|
|
673 |
codeTimesRepeatOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
674 |
"generate code for n timesRepeat:[ ... ]"
|
|
675 |
|
12
|
676 |
|pos pos2 theReceiver|
|
0
|
677 |
|
|
678 |
theReceiver := receiver.
|
|
679 |
theReceiver codeOn:aStream inBlock:b.
|
|
680 |
valueNeeded ifTrue:[aStream nextPut:#dup].
|
|
681 |
|
|
682 |
pos := aStream position.
|
|
683 |
aStream nextPut:#dup.
|
|
684 |
aStream nextPut:#push0.
|
|
685 |
aStream nextPut:#>.
|
6
|
686 |
aStream nextPut:lineNr.
|
0
|
687 |
aStream nextPut:#falseJump.
|
|
688 |
pos2 := aStream position.
|
|
689 |
aStream nextPut:0.
|
|
690 |
|
|
691 |
(argArray at:1) codeInlineOn:aStream inBlock:b valueNeeded:false.
|
|
692 |
aStream nextPut:#minus1.
|
6
|
693 |
aStream nextPut:lineNr.
|
0
|
694 |
aStream nextPut:#jump.
|
|
695 |
aStream nextPut:pos.
|
|
696 |
|
75
|
697 |
(aStream contents) at:pos2 put:(aStream position).
|
74
|
698 |
aStream nextPut:#drop.
|
0
|
699 |
!
|
|
700 |
|
|
701 |
codeIfElseOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
702 |
"generate code for x ifxxx:[ ... ] yyy:[ ...]"
|
|
703 |
|
|
704 |
|pos pos2 theReceiver theArg theByteCode optByteCode|
|
|
705 |
|
|
706 |
theReceiver := receiver.
|
|
707 |
(selector == #ifTrue:ifFalse:) ifTrue:[
|
47
|
708 |
theByteCode := #falseJump
|
0
|
709 |
] ifFalse:[
|
47
|
710 |
(selector == #ifFalse:ifTrue:) ifTrue:[
|
|
711 |
theByteCode := #trueJump
|
|
712 |
]
|
0
|
713 |
].
|
|
714 |
optByteCode := self optimizedConditionFor:theReceiver
|
47
|
715 |
with:theByteCode.
|
0
|
716 |
optByteCode notNil ifTrue:[
|
47
|
717 |
((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
|
|
718 |
theArg := theReceiver arg1
|
|
719 |
].
|
|
720 |
theReceiver := theReceiver receiver.
|
|
721 |
theByteCode := optByteCode
|
0
|
722 |
].
|
|
723 |
theByteCode notNil ifTrue:[
|
47
|
724 |
theReceiver codeOn:aStream inBlock:b.
|
|
725 |
theArg notNil ifTrue:[
|
|
726 |
theArg codeOn:aStream inBlock:b
|
|
727 |
].
|
53
|
728 |
|
|
729 |
(lineNr between:1 and:255) ifTrue:[
|
|
730 |
aStream nextPut:#lineno.
|
|
731 |
aStream nextPut:lineNr.
|
|
732 |
].
|
|
733 |
|
47
|
734 |
aStream nextPut:theByteCode.
|
|
735 |
pos := aStream position.
|
|
736 |
aStream nextPut:0.
|
|
737 |
(argArray at:1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
738 |
aStream nextPut:#jump.
|
|
739 |
pos2 := aStream position.
|
|
740 |
aStream nextPut:0.
|
|
741 |
(aStream contents) at:pos put:(aStream position).
|
|
742 |
(argArray at:2) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
743 |
(aStream contents) at:pos2 put:(aStream position)
|
0
|
744 |
]
|
|
745 |
!
|
|
746 |
|
|
747 |
codeIfOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
748 |
"generate code for x ifxxx:[ ... ]"
|
|
749 |
|
|
750 |
|pos pos2 theReceiver theArg theByteCode optByteCode subsel|
|
|
751 |
|
|
752 |
theReceiver := receiver.
|
|
753 |
|
19
|
754 |
(theReceiver isMessage "class == MessageNode") ifTrue:[
|
47
|
755 |
subsel := theReceiver selector.
|
|
756 |
(subsel == #and:) ifTrue:[
|
|
757 |
self codeAndIfOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
758 |
^ self
|
|
759 |
].
|
|
760 |
(subsel == #or:) ifTrue:[
|
|
761 |
self codeOrIfOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
762 |
^ self
|
|
763 |
]
|
0
|
764 |
].
|
|
765 |
(selector == #ifTrue:) ifTrue:[
|
47
|
766 |
theByteCode := #falseJump
|
0
|
767 |
] ifFalse:[
|
47
|
768 |
theByteCode := #trueJump
|
0
|
769 |
].
|
|
770 |
optByteCode := self optimizedConditionFor:theReceiver
|
47
|
771 |
with:theByteCode.
|
0
|
772 |
optByteCode notNil ifTrue:[
|
47
|
773 |
((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
|
|
774 |
theArg := theReceiver arg1
|
|
775 |
].
|
|
776 |
theReceiver := theReceiver receiver.
|
|
777 |
theByteCode := optByteCode
|
0
|
778 |
].
|
|
779 |
|
|
780 |
theReceiver codeOn:aStream inBlock:b.
|
|
781 |
theArg notNil ifTrue:[
|
47
|
782 |
theArg codeOn:aStream inBlock:b
|
0
|
783 |
].
|
53
|
784 |
|
|
785 |
(lineNr between:1 and:255) ifTrue:[
|
|
786 |
aStream nextPut:#lineno.
|
|
787 |
aStream nextPut:lineNr.
|
|
788 |
].
|
|
789 |
|
0
|
790 |
aStream nextPut:theByteCode.
|
|
791 |
pos := aStream position.
|
|
792 |
aStream nextPut:0.
|
|
793 |
(argArray at: 1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
794 |
valueNeeded ifTrue:[
|
47
|
795 |
aStream nextPut:#jump.
|
|
796 |
pos2 := aStream position.
|
|
797 |
aStream nextPut:0.
|
|
798 |
(aStream contents) at:pos put:(aStream position).
|
|
799 |
aStream nextPut:#pushNil.
|
|
800 |
(aStream contents) at:pos2 put:(aStream position)
|
0
|
801 |
] ifFalse:[
|
47
|
802 |
(aStream contents) at:pos put:(aStream position)
|
0
|
803 |
]
|
|
804 |
!
|
|
805 |
|
|
806 |
codeAndIfOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
807 |
"generate code for (x and:[y]) ifxxx:[ ... ]"
|
|
808 |
|
|
809 |
|theByteCode optByteCode theReceiver theArg pos1 pos2 pos3|
|
|
810 |
|
|
811 |
|
|
812 |
theByteCode := #falseJump.
|
|
813 |
theReceiver := receiver receiver.
|
|
814 |
|
|
815 |
optByteCode := self optimizedConditionFor:theReceiver
|
47
|
816 |
with:theByteCode.
|
0
|
817 |
optByteCode notNil ifTrue:[
|
47
|
818 |
((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
|
|
819 |
theArg := theReceiver arg1
|
|
820 |
].
|
|
821 |
theReceiver := theReceiver receiver.
|
|
822 |
theByteCode := optByteCode
|
0
|
823 |
].
|
|
824 |
theReceiver codeOn:aStream inBlock:b.
|
|
825 |
theArg notNil ifTrue:[
|
47
|
826 |
theArg codeOn:aStream inBlock:b
|
0
|
827 |
].
|
|
828 |
aStream nextPut:theByteCode.
|
|
829 |
pos1 := aStream position.
|
|
830 |
aStream nextPut:0.
|
|
831 |
|
|
832 |
theReceiver := receiver arg1.
|
|
833 |
theReceiver codeInlineOn:aStream inBlock:b.
|
|
834 |
(selector == #ifTrue:) ifTrue:[
|
47
|
835 |
aStream nextPut:#falseJump
|
0
|
836 |
] ifFalse:[
|
47
|
837 |
aStream nextPut:#trueJump
|
0
|
838 |
].
|
|
839 |
pos2 := aStream position.
|
|
840 |
aStream nextPut:0.
|
|
841 |
(selector == #ifFalse:) ifTrue:[
|
47
|
842 |
(aStream contents) at:pos1 put:(aStream position)
|
0
|
843 |
].
|
|
844 |
(argArray at: 1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
845 |
valueNeeded ifTrue:[
|
47
|
846 |
aStream nextPut:#jump.
|
|
847 |
pos3 := aStream position.
|
|
848 |
aStream nextPut:0.
|
|
849 |
(selector == #ifTrue:) ifTrue:[
|
|
850 |
(aStream contents) at:pos1 put:(aStream position)
|
|
851 |
].
|
|
852 |
(aStream contents) at:pos2 put:(aStream position).
|
|
853 |
aStream nextPut:#pushNil.
|
|
854 |
(aStream contents) at:pos3 put:(aStream position)
|
0
|
855 |
] ifFalse:[
|
47
|
856 |
(selector == #ifTrue:) ifTrue:[
|
|
857 |
(aStream contents) at:pos1 put:(aStream position)
|
|
858 |
].
|
|
859 |
(aStream contents) at:pos2 put:(aStream position)
|
0
|
860 |
]
|
|
861 |
!
|
|
862 |
|
|
863 |
codeOrIfOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
864 |
"generate code for (x or:[y]) ifxxx:[ ... ]"
|
|
865 |
|
|
866 |
|theByteCode optByteCode theReceiver theArg pos1 pos2 pos3|
|
|
867 |
|
|
868 |
theByteCode := #trueJump.
|
|
869 |
theReceiver := receiver receiver.
|
|
870 |
|
3
|
871 |
optByteCode := self optimizedConditionFor:theReceiver with:theByteCode.
|
0
|
872 |
optByteCode notNil ifTrue:[
|
47
|
873 |
((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
|
|
874 |
theArg := theReceiver arg1
|
|
875 |
].
|
|
876 |
theReceiver := theReceiver receiver.
|
|
877 |
theByteCode := optByteCode
|
0
|
878 |
].
|
|
879 |
theReceiver codeOn:aStream inBlock:b.
|
|
880 |
theArg notNil ifTrue:[
|
47
|
881 |
theArg codeOn:aStream inBlock:b
|
0
|
882 |
].
|
|
883 |
aStream nextPut:theByteCode.
|
|
884 |
pos1 := aStream position.
|
|
885 |
aStream nextPut:0.
|
|
886 |
|
3
|
887 |
|
0
|
888 |
theReceiver := receiver arg1.
|
3
|
889 |
|
|
890 |
"new:"
|
0
|
891 |
(selector == #ifTrue:) ifTrue:[
|
47
|
892 |
theByteCode := #falseJump
|
0
|
893 |
] ifFalse:[
|
47
|
894 |
theByteCode := #trueJump
|
3
|
895 |
].
|
|
896 |
optByteCode := self optimizedConditionFor:theReceiver with:theByteCode.
|
|
897 |
optByteCode notNil ifTrue:[
|
47
|
898 |
theReceiver isBlock ifTrue:[
|
|
899 |
theReceiver := theReceiver statements expression
|
|
900 |
].
|
|
901 |
((optByteCode == #eqJump) or:[optByteCode == #notEqJump]) ifTrue:[
|
|
902 |
theArg := theReceiver arg1
|
|
903 |
].
|
|
904 |
theReceiver := theReceiver receiver.
|
|
905 |
theByteCode := optByteCode.
|
3
|
906 |
|
47
|
907 |
theReceiver codeOn:aStream inBlock:b.
|
|
908 |
theArg notNil ifTrue:[
|
|
909 |
theArg codeOn:aStream inBlock:b
|
|
910 |
].
|
|
911 |
aStream nextPut:theByteCode.
|
3
|
912 |
|
|
913 |
] ifFalse:[
|
|
914 |
"org"
|
47
|
915 |
theReceiver codeInlineOn:aStream inBlock:b.
|
|
916 |
(selector == #ifTrue:) ifTrue:[
|
|
917 |
aStream nextPut:#falseJump
|
|
918 |
] ifFalse:[
|
|
919 |
aStream nextPut:#trueJump
|
|
920 |
].
|
0
|
921 |
].
|
|
922 |
pos2 := aStream position.
|
|
923 |
aStream nextPut:0.
|
|
924 |
(selector == #ifTrue:) ifTrue:[
|
47
|
925 |
(aStream contents) at:pos1 put:(aStream position)
|
0
|
926 |
].
|
|
927 |
(argArray at: 1) codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
928 |
valueNeeded ifTrue:[
|
47
|
929 |
aStream nextPut:#jump.
|
|
930 |
pos3 := aStream position.
|
|
931 |
aStream nextPut:0.
|
|
932 |
(selector == #ifFalse:) ifTrue:[
|
|
933 |
(aStream contents) at:pos1 put:(aStream position)
|
|
934 |
].
|
|
935 |
(aStream contents) at:pos2 put:(aStream position).
|
|
936 |
aStream nextPut:#pushNil.
|
|
937 |
(aStream contents) at:pos3 put:(aStream position)
|
0
|
938 |
] ifFalse:[
|
47
|
939 |
(selector == #ifFalse:) ifTrue:[
|
|
940 |
(aStream contents) at:pos1 put:(aStream position)
|
|
941 |
].
|
|
942 |
(aStream contents) at:pos2 put:(aStream position)
|
0
|
943 |
]
|
|
944 |
!
|
|
945 |
|
|
946 |
codeAndOrOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
947 |
"generate code for x and/or:[y] - but not in an if"
|
|
948 |
|
|
949 |
|pos theReceiver theByteCode|
|
|
950 |
|
|
951 |
self halt.
|
|
952 |
theReceiver := receiver.
|
|
953 |
(selector == #and:) ifTrue:[
|
47
|
954 |
theByteCode := #falseJump
|
0
|
955 |
] ifFalse:[
|
47
|
956 |
theByteCode := #trueJump
|
0
|
957 |
].
|
|
958 |
"
|
|
959 |
(self canOptimizeConditionFor:receiver) ifTrue:[
|
47
|
960 |
theByteCode := self optimizedConditionFor:theReceiver
|
|
961 |
with:theByteCode.
|
|
962 |
theReceiver := theReceiver receiver
|
0
|
963 |
].
|
|
964 |
"
|
|
965 |
theReceiver codeOn:aStream inBlock:b.
|
|
966 |
aStream nextPut:theByteCode.
|
|
967 |
pos := aStream position.
|
|
968 |
aStream nextPut:0.
|
|
969 |
(argArray at: 1) codeInlineOn:aStream inBlock:b.
|
|
970 |
(aStream contents) at:pos put:(aStream position).
|
|
971 |
valueNeeded ifFalse:[aStream nextPut:#drop]
|
|
972 |
!
|
|
973 |
|
|
974 |
codeOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
975 |
|nargs isBuiltIn|
|
|
976 |
|
|
977 |
argArray isNil ifTrue:[
|
47
|
978 |
nargs := 0
|
0
|
979 |
] ifFalse:[
|
47
|
980 |
nargs := argArray size
|
0
|
981 |
].
|
|
982 |
|
|
983 |
isBuiltIn := false.
|
|
984 |
|
|
985 |
(nargs == 0) ifTrue:[
|
47
|
986 |
(receiver type == #ThisContext) ifTrue:[
|
|
987 |
valueNeeded ifFalse:[
|
|
988 |
"for now, only do it in methods"
|
|
989 |
b isNil ifTrue:[
|
|
990 |
(selector == #restart) ifTrue:[
|
|
991 |
aStream nextPut:#jump. "jump to start"
|
|
992 |
aStream nextPut:1.
|
|
993 |
^ self
|
|
994 |
].
|
|
995 |
].
|
|
996 |
(selector == #return) ifTrue:[ "^ nil"
|
|
997 |
aStream nextPut:#retNil.
|
|
998 |
^ self
|
|
999 |
].
|
|
1000 |
]
|
|
1001 |
].
|
52
|
1002 |
|
|
1003 |
receiver isBlock ifTrue:[
|
|
1004 |
selector == #value ifTrue:[
|
|
1005 |
receiver codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
1006 |
^ self
|
|
1007 |
].
|
|
1008 |
].
|
47
|
1009 |
isBuiltIn := self class isBuiltInUnarySelector:selector
|
0
|
1010 |
].
|
|
1011 |
|
|
1012 |
(nargs == 1) ifTrue:[
|
47
|
1013 |
(receiver type == #ThisContext) ifTrue:[
|
|
1014 |
valueNeeded ifFalse:[
|
|
1015 |
(selector == #return:) ifTrue:[
|
|
1016 |
(argArray at:1) codeOn:aStream inBlock:b. "^ value"
|
|
1017 |
aStream nextPut:#retTop.
|
|
1018 |
^ self
|
|
1019 |
].
|
|
1020 |
].
|
|
1021 |
].
|
19
|
1022 |
|
47
|
1023 |
((argArray at:1) isBlock) ifTrue:[
|
|
1024 |
((selector == #ifTrue:) or:[selector == #ifFalse:]) ifTrue:[
|
55
|
1025 |
receiver isBlock ifFalse:[
|
|
1026 |
self codeIfOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
1027 |
^ self
|
|
1028 |
].
|
47
|
1029 |
].
|
0
|
1030 |
"
|
47
|
1031 |
((selector == #and:) or:[selector == #or:]) ifTrue:[
|
|
1032 |
self codeAndOrOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
1033 |
^ self
|
|
1034 |
].
|
0
|
1035 |
"
|
47
|
1036 |
(selector == #timesRepeat:) ifTrue:[
|
|
1037 |
(receiver isConstant and:[receiver evaluate isNumber]) ifTrue:[
|
|
1038 |
self codeTimesRepeatOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
1039 |
^ self
|
|
1040 |
]
|
|
1041 |
].
|
0
|
1042 |
|
47
|
1043 |
((selector == #whileTrue:) or:[selector == #whileFalse:]) ifTrue:[
|
|
1044 |
(receiver isBlock) ifTrue:[
|
|
1045 |
self codeWhileOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
1046 |
^ self
|
|
1047 |
]
|
|
1048 |
]
|
|
1049 |
].
|
|
1050 |
isBuiltIn := self class isBuiltIn1ArgSelector:selector
|
0
|
1051 |
].
|
|
1052 |
|
|
1053 |
(nargs == 2) ifTrue:[
|
47
|
1054 |
((selector == #ifTrue:ifFalse:) or:[selector == #ifFalse:ifTrue:]) ifTrue:[
|
55
|
1055 |
receiver isBlock ifFalse:[
|
|
1056 |
(argArray at:1) isBlock ifTrue:[
|
|
1057 |
(argArray at:2) isBlock ifTrue:[
|
|
1058 |
self codeIfElseOn:aStream inBlock:b valueNeeded:valueNeeded.
|
|
1059 |
^ self
|
|
1060 |
]
|
47
|
1061 |
]
|
|
1062 |
]
|
|
1063 |
].
|
|
1064 |
isBuiltIn := self class isBuiltIn2ArgSelector:selector
|
0
|
1065 |
].
|
|
1066 |
|
|
1067 |
"can we use a send-bytecode ?"
|
|
1068 |
isBuiltIn ifTrue:[
|
48
|
1069 |
receiver isSuper ifFalse:[
|
47
|
1070 |
receiver codeOn:aStream inBlock:b.
|
|
1071 |
(nargs > 0) ifTrue:[
|
|
1072 |
(argArray at:1) codeOn:aStream inBlock:b.
|
|
1073 |
(nargs > 1) ifTrue:[
|
|
1074 |
(argArray at:2) codeOn:aStream inBlock:b
|
|
1075 |
]
|
|
1076 |
].
|
|
1077 |
aStream nextPut:selector.
|
|
1078 |
(self class hasLineNumber:selector) ifTrue:[
|
|
1079 |
aStream nextPut:lineNr.
|
|
1080 |
].
|
|
1081 |
valueNeeded ifFalse:[
|
|
1082 |
aStream nextPut:#drop
|
|
1083 |
].
|
|
1084 |
^ self
|
|
1085 |
]
|
0
|
1086 |
].
|
|
1087 |
|
|
1088 |
((nargs == 0) and:[selector == #yourself]) ifTrue:[
|
47
|
1089 |
"yourself is often added to get the receiver -
|
|
1090 |
we get it without the yourself-message"
|
0
|
1091 |
|
47
|
1092 |
valueNeeded ifTrue:[
|
|
1093 |
receiver codeOn:aStream inBlock:b
|
|
1094 |
].
|
|
1095 |
^ self
|
0
|
1096 |
].
|
|
1097 |
|
|
1098 |
"no - generate a send"
|
|
1099 |
((receiver type ~~ #Self)
|
|
1100 |
or:[nargs > 3]) ifTrue:[
|
47
|
1101 |
receiver codeOn:aStream inBlock:b
|
0
|
1102 |
].
|
|
1103 |
argArray notNil ifTrue:[
|
47
|
1104 |
argArray do:[:arg |
|
|
1105 |
arg codeOn:aStream inBlock:b
|
|
1106 |
]
|
0
|
1107 |
].
|
48
|
1108 |
receiver isSuper ifTrue:[
|
49
|
1109 |
receiver isHere ifTrue:[
|
|
1110 |
aStream nextPut:#hereSend
|
|
1111 |
] ifFalse:[
|
|
1112 |
aStream nextPut:#superSend.
|
|
1113 |
].
|
47
|
1114 |
aStream nextPut:lineNr.
|
|
1115 |
aStream nextPut:selector.
|
|
1116 |
aStream nextPut:nargs.
|
|
1117 |
aStream nextPut:nil.
|
|
1118 |
valueNeeded ifFalse:[
|
|
1119 |
aStream nextPut:#drop
|
|
1120 |
].
|
|
1121 |
^ self
|
0
|
1122 |
].
|
49
|
1123 |
|
|
1124 |
(nargs <= 3) ifTrue:[
|
|
1125 |
|codes|
|
|
1126 |
|
47
|
1127 |
valueNeeded ifTrue:[
|
|
1128 |
(receiver type == #Self) ifTrue:[
|
49
|
1129 |
codes := #(sendSelf0 sendSelf1 sendSelf2 sendSelf3)
|
47
|
1130 |
] ifFalse:[
|
49
|
1131 |
codes := #(send0 send1 send2 send3)
|
47
|
1132 |
]
|
|
1133 |
] ifFalse:[
|
|
1134 |
(receiver type == #Self) ifTrue:[
|
49
|
1135 |
codes := #(sendSelfDrop0 sendSelfDrop1 sendSelfDrop2 sendSelfDrop3)
|
47
|
1136 |
] ifFalse:[
|
49
|
1137 |
codes := #(sendDrop0 sendDrop1 sendDrop2 sendDrop3)
|
47
|
1138 |
]
|
|
1139 |
].
|
49
|
1140 |
aStream nextPut:(codes at:nargs + 1).
|
47
|
1141 |
aStream nextPut:lineNr.
|
|
1142 |
aStream nextPut:selector.
|
|
1143 |
^ self
|
0
|
1144 |
].
|
49
|
1145 |
|
0
|
1146 |
valueNeeded ifTrue:[
|
47
|
1147 |
aStream nextPut:#send
|
0
|
1148 |
] ifFalse:[
|
47
|
1149 |
aStream nextPut:#sendDrop
|
0
|
1150 |
].
|
|
1151 |
aStream nextPut:lineNr.
|
|
1152 |
aStream nextPut:selector.
|
|
1153 |
aStream nextPut:nargs
|
|
1154 |
!
|
|
1155 |
|
|
1156 |
codeSendOn:aStream inBlock:b valueNeeded:valueNeeded
|
|
1157 |
"like code on, but assumes that receiver has already been
|
|
1158 |
coded onto stack - needed for cascade"
|
|
1159 |
|
|
1160 |
|nargs isBuiltIn|
|
|
1161 |
|
|
1162 |
argArray isNil ifTrue:[
|
47
|
1163 |
nargs := 0
|
0
|
1164 |
] ifFalse:[
|
47
|
1165 |
nargs := argArray size
|
0
|
1166 |
].
|
|
1167 |
|
|
1168 |
isBuiltIn := false.
|
|
1169 |
|
|
1170 |
(nargs == 0) ifTrue:[
|
47
|
1171 |
isBuiltIn := self class isBuiltInUnarySelector:selector
|
0
|
1172 |
].
|
|
1173 |
(nargs == 1) ifTrue:[
|
47
|
1174 |
isBuiltIn := self class isBuiltIn1ArgSelector:selector
|
0
|
1175 |
].
|
|
1176 |
(nargs == 2) ifTrue:[
|
47
|
1177 |
isBuiltIn := self class isBuiltIn2ArgSelector:selector
|
0
|
1178 |
].
|
|
1179 |
|
|
1180 |
"can we use a send-bytecode ?"
|
|
1181 |
isBuiltIn ifTrue:[
|
48
|
1182 |
receiver isSuper ifFalse:[
|
47
|
1183 |
(nargs > 0) ifTrue:[
|
|
1184 |
(argArray at:1) codeOn:aStream inBlock:b.
|
|
1185 |
(nargs > 1) ifTrue:[
|
|
1186 |
(argArray at:2) codeOn:aStream inBlock:b
|
|
1187 |
]
|
|
1188 |
].
|
|
1189 |
aStream nextPut:selector.
|
|
1190 |
(self class hasLineNumber:selector) ifTrue:[
|
|
1191 |
aStream nextPut:lineNr.
|
|
1192 |
].
|
|
1193 |
valueNeeded ifFalse:[
|
|
1194 |
aStream nextPut:#drop
|
|
1195 |
].
|
|
1196 |
^ self
|
|
1197 |
]
|
0
|
1198 |
].
|
|
1199 |
|
|
1200 |
argArray notNil ifTrue:[
|
47
|
1201 |
argArray do:[:arg |
|
|
1202 |
arg codeOn:aStream inBlock:b
|
|
1203 |
]
|
0
|
1204 |
].
|
|
1205 |
|
48
|
1206 |
receiver isSuper ifTrue:[
|
49
|
1207 |
receiver isHere ifTrue:[
|
|
1208 |
aStream nextPut:#hereSend
|
|
1209 |
] ifFalse:[
|
|
1210 |
aStream nextPut:#superSend.
|
|
1211 |
].
|
47
|
1212 |
aStream nextPut:lineNr.
|
|
1213 |
aStream nextPut:selector.
|
|
1214 |
aStream nextPut:nargs.
|
|
1215 |
aStream nextPut:nil.
|
|
1216 |
valueNeeded ifFalse:[
|
|
1217 |
aStream nextPut:#drop
|
|
1218 |
].
|
|
1219 |
^ self
|
0
|
1220 |
].
|
|
1221 |
(nargs == 0) ifTrue:[
|
47
|
1222 |
(selector == #yourself) ifTrue:[
|
|
1223 |
"yourself is often added to get the receiver -
|
|
1224 |
we get it without the yourself-message"
|
0
|
1225 |
|
47
|
1226 |
valueNeeded ifFalse:[
|
|
1227 |
aStream nextPut:#drop
|
|
1228 |
].
|
|
1229 |
^ self
|
|
1230 |
].
|
49
|
1231 |
].
|
0
|
1232 |
|
49
|
1233 |
(nargs <= 3) ifTrue:[
|
47
|
1234 |
valueNeeded ifTrue:[
|
49
|
1235 |
aStream nextPut:( #( send0 send1 send2 send3) at:(nargs+1) ).
|
47
|
1236 |
] ifFalse:[
|
49
|
1237 |
aStream nextPut:( #( sendDrop0 sendDrop1 sendDrop2 sendDrop3) at:(nargs+1) ).
|
47
|
1238 |
].
|
|
1239 |
aStream nextPut:lineNr.
|
|
1240 |
aStream nextPut:selector.
|
|
1241 |
^ self
|
0
|
1242 |
].
|
49
|
1243 |
|
0
|
1244 |
valueNeeded ifTrue:[
|
47
|
1245 |
aStream nextPut:#send
|
0
|
1246 |
] ifFalse:[
|
47
|
1247 |
aStream nextPut:#sendDrop
|
0
|
1248 |
].
|
|
1249 |
aStream nextPut:lineNr.
|
|
1250 |
aStream nextPut:selector.
|
|
1251 |
aStream nextPut:nargs
|
|
1252 |
!
|
|
1253 |
|
|
1254 |
codeForCascadeOn:aStream inBlock:b
|
|
1255 |
"like codeOn, but always leave the receiver instead of the result"
|
|
1256 |
|nargs isBuiltIn|
|
|
1257 |
|
|
1258 |
argArray isNil ifTrue:[
|
47
|
1259 |
nargs := 0
|
0
|
1260 |
] ifFalse:[
|
47
|
1261 |
nargs := argArray size
|
0
|
1262 |
].
|
|
1263 |
|
|
1264 |
isBuiltIn := false.
|
|
1265 |
|
|
1266 |
(nargs == 0) ifTrue:[
|
47
|
1267 |
isBuiltIn := self class isBuiltInUnarySelector:selector
|
0
|
1268 |
].
|
|
1269 |
(nargs == 1) ifTrue:[
|
47
|
1270 |
isBuiltIn := self class isBuiltIn1ArgSelector:selector
|
0
|
1271 |
].
|
|
1272 |
(nargs == 2) ifTrue:[
|
47
|
1273 |
isBuiltIn := self class isBuiltIn2ArgSelector:selector
|
0
|
1274 |
].
|
|
1275 |
|
|
1276 |
receiver codeOn:aStream inBlock:b.
|
|
1277 |
aStream nextPut:#dup.
|
|
1278 |
|
|
1279 |
"can we use a send-bytecode ?"
|
|
1280 |
isBuiltIn ifTrue:[
|
48
|
1281 |
receiver isSuper ifFalse:[
|
47
|
1282 |
(nargs > 0) ifTrue:[
|
|
1283 |
(argArray at:1) codeOn:aStream inBlock:b.
|
|
1284 |
(nargs > 1) ifTrue:[
|
|
1285 |
(argArray at:2) codeOn:aStream inBlock:b
|
|
1286 |
]
|
|
1287 |
].
|
|
1288 |
aStream nextPut:selector.
|
|
1289 |
aStream nextPut:lineNr.
|
|
1290 |
aStream nextPut:#drop.
|
|
1291 |
^ self
|
|
1292 |
]
|
0
|
1293 |
].
|
|
1294 |
|
|
1295 |
"no - generate a send"
|
|
1296 |
argArray notNil ifTrue:[
|
47
|
1297 |
argArray do:[:arg |
|
|
1298 |
arg codeOn:aStream inBlock:b
|
|
1299 |
]
|
0
|
1300 |
].
|
48
|
1301 |
receiver isSuper ifTrue:[
|
49
|
1302 |
receiver isHere ifTrue:[
|
|
1303 |
aStream nextPut:#hereSend
|
|
1304 |
] ifFalse:[
|
|
1305 |
aStream nextPut:#superSend.
|
|
1306 |
].
|
47
|
1307 |
aStream nextPut:lineNr.
|
|
1308 |
aStream nextPut:selector.
|
|
1309 |
aStream nextPut:nargs.
|
|
1310 |
aStream nextPut:nil.
|
|
1311 |
aStream nextPut:#drop.
|
|
1312 |
^ self
|
0
|
1313 |
].
|
49
|
1314 |
(nargs <= 3) ifTrue:[
|
|
1315 |
aStream nextPut:( #( sendDrop0 sendDrop1 sendDrop2 sendDrop3) at:(nargs+1) ).
|
47
|
1316 |
aStream nextPut:lineNr.
|
|
1317 |
aStream nextPut:selector.
|
|
1318 |
^ self
|
0
|
1319 |
].
|
49
|
1320 |
|
0
|
1321 |
aStream nextPut:#sendDrop.
|
|
1322 |
aStream nextPut:lineNr.
|
|
1323 |
aStream nextPut:selector.
|
|
1324 |
aStream nextPut:nargs
|
|
1325 |
! !
|