author | Claus Gittinger <cg@exept.de> |
Sat, 18 May 1996 17:33:35 +0200 | |
changeset 278 | 8c8c96856b41 |
parent 263 | 3b21d0991eff |
child 333 | 96bb5c3dab61 |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
45 | 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:#BlockNode |
|
168 | 14 |
instanceVariableNames:'blockArgs statements home inlineBlock exitBlock blockVars |
15 |
needsHome lineNr' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
0 | 19 |
! |
20 |
||
20 | 21 |
!BlockNode class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
45 | 26 |
All Rights Reserved |
0 | 27 |
|
20 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
0 | 36 |
|
20 | 37 |
documentation |
38 |
" |
|
39 |
node for parse-trees, representing blocks |
|
261 | 40 |
This is a helper class for the compiler. |
263 | 41 |
|
42 |
[author:] |
|
43 |
Claus Gittinger |
|
20 | 44 |
" |
45 |
! ! |
|
0 | 46 |
|
47 |
!BlockNode class methodsFor:'instance creation'! |
|
48 |
||
3 | 49 |
arguments:argList home:h variables:vars |
50 |
^ (self basicNew) setArguments:argList home:h variables:vars |
|
0 | 51 |
! ! |
52 |
||
53 |
!BlockNode methodsFor:'accessing'! |
|
54 |
||
55 |
arguments |
|
56 |
^ blockArgs |
|
57 |
! |
|
58 |
||
26 | 59 |
arguments:argList |
60 |
blockArgs := argList |
|
61 |
! |
|
62 |
||
140 | 63 |
home |
64 |
^ home |
|
0 | 65 |
! |
66 |
||
67 |
home:aBlock |
|
68 |
home := aBlock |
|
69 |
! |
|
70 |
||
71 |
inlineBlock |
|
72 |
^ inlineBlock |
|
73 |
! |
|
74 |
||
75 |
inlineBlock:aBoolean |
|
76 |
inlineBlock := aBoolean |
|
77 |
! |
|
78 |
||
140 | 79 |
lineNumber:aNumber |
80 |
lineNr := aNumber |
|
81 |
! |
|
82 |
||
0 | 83 |
needsHome |
84 |
^ needsHome |
|
85 |
! |
|
86 |
||
87 |
needsHome:aBoolean |
|
88 |
needsHome := aBoolean |
|
6 | 89 |
! |
90 |
||
140 | 91 |
statements |
92 |
^ statements |
|
93 |
! |
|
94 |
||
95 |
statements:s |
|
96 |
statements := s |
|
97 |
! |
|
98 |
||
99 |
variables |
|
100 |
^ blockVars |
|
101 |
! |
|
102 |
||
103 |
variables:varList |
|
104 |
blockVars := varList |
|
105 |
! ! |
|
106 |
||
107 |
!BlockNode methodsFor:'block messages'! |
|
108 |
||
109 |
doesNotUnderstand:aMessage |
|
110 |
|numArgs kludgeBlock| |
|
111 |
||
112 |
(Block implements:(aMessage selector)) ifTrue:[ |
|
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
113 |
|
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
114 |
"/ mhmh - a message which I dont understand, but Block implements |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
115 |
"/ send it to a kludgeblock, which will evaluate me again ..." |
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
116 |
|
140 | 117 |
numArgs := blockArgs size. |
118 |
numArgs == 0 ifTrue:[ |
|
119 |
kludgeBlock := [self value] |
|
120 |
] ifFalse:[ |
|
121 |
numArgs == 1 ifTrue:[ |
|
122 |
kludgeBlock := [:a1 | self value:a1]. |
|
123 |
] ifFalse:[ |
|
124 |
numArgs == 2 ifTrue:[ |
|
125 |
kludgeBlock := [:a1 :a2 | self value:a1 value:a2]. |
|
126 |
] ifFalse:[ |
|
127 |
numArgs == 3 ifTrue:[ |
|
128 |
kludgeBlock := [:a1 :a2 :a3| self value:a1 value:a2 value:a3]. |
|
129 |
] ifFalse:[ |
|
130 |
numArgs == 4 ifTrue:[ |
|
131 |
kludgeBlock := [:a1 :a2 :a3 :a4| self value:a1 value:a2 value:a3 value:a4]. |
|
132 |
] ifFalse:[ |
|
133 |
^ self error:'only support blocks with up-to 4 args' |
|
134 |
] |
|
135 |
] |
|
136 |
] |
|
137 |
] |
|
138 |
]. |
|
139 |
^ kludgeBlock perform:aMessage selector withArguments:aMessage arguments |
|
140 |
]. |
|
141 |
^ super doesNotUnderstand:aMessage |
|
0 | 142 |
! ! |
143 |
||
140 | 144 |
!BlockNode methodsFor:'code generation'! |
145 |
||
146 |
checkForSimpleBlock |
|
147 |
"simple things can be made cheap blocks right now - |
|
148 |
resulting in a simple pushLit instruction ..." |
|
149 |
||
150 |
|cheapy e val code| |
|
151 |
||
152 |
statements isNil ifTrue:[ |
|
153 |
"a []-block" |
|
154 |
||
155 |
val := nil |
|
156 |
] ifFalse:[ |
|
157 |
statements nextStatement notNil ifTrue:[^ nil]. |
|
158 |
(statements isMemberOf:StatementNode) ifFalse:[^ nil]. |
|
159 |
||
160 |
e := statements expression. |
|
161 |
e isConstant ifFalse:[^ nil]. |
|
162 |
||
163 |
val := e value. |
|
164 |
]. |
|
165 |
||
166 |
val == 0 ifTrue:[ |
|
167 |
"a [0]-block" |
|
168 |
||
169 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#ret0). |
|
170 |
]. |
|
171 |
val == 1 ifTrue:[ |
|
172 |
"a [1]-block" |
|
173 |
||
174 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#push1) |
|
175 |
with:(ByteCodeCompiler byteCodeFor:#retTop). |
|
176 |
]. |
|
177 |
||
178 |
val == true ifTrue:[ |
|
179 |
"a [true]-block" |
|
180 |
||
181 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retTrue). |
|
182 |
]. |
|
183 |
||
184 |
val == false ifTrue:[ |
|
185 |
"a [false]-block" |
|
186 |
||
187 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retFalse). |
|
188 |
]. |
|
189 |
||
190 |
val == nil ifTrue:[ |
|
191 |
"a [nil]-block" |
|
192 |
||
193 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retNil). |
|
194 |
]. |
|
195 |
||
196 |
code notNil ifTrue:[ |
|
197 |
cheapy := Block code:nil |
|
198 |
byteCode:code |
|
199 |
numArgs:(blockArgs size) |
|
200 |
sourcePosition:nil |
|
201 |
initialPC:nil |
|
202 |
literals:nil |
|
203 |
dynamic:false. |
|
204 |
^ ConstantNode type:#Block value:cheapy |
|
205 |
]. |
|
206 |
||
207 |
^ nil |
|
208 |
! |
|
209 |
||
210 |
codeForSideEffectOn:aStream inBlock:b for:aCompiler |
|
211 |
"generate code for this statement - value not needed. |
|
212 |
For blocks, no code is generated at all." |
|
213 |
||
214 |
^ self |
|
215 |
! |
|
216 |
||
217 |
codeInlineOn:aStream inBlock:b for:aCompiler |
|
218 |
self codeInlineOn:aStream inBlock:b valueNeeded:true for:aCompiler |
|
219 |
! |
|
220 |
||
221 |
codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded for:aCompiler |
|
222 |
|thisStatement nextStatement| |
|
223 |
||
224 |
blockVars notNil ifTrue:[ |
|
225 |
"cannot currently compile this block inline (have to move blockvars into |
|
226 |
surrounding method. generate a make-block and send it value" |
|
0 | 227 |
|
278 | 228 |
"/ Transcript showCR:'cannot (yet) compile block with blockvars inline'. |
140 | 229 |
self codeOn:aStream inBlock:b for:aCompiler. |
230 |
aStream nextPut:#value. |
|
199
1b3b350a3f59
moved bytecode query methods into compiler;
Claus Gittinger <cg@exept.de>
parents:
170
diff
changeset
|
231 |
(aCompiler hasLineNumber:#value) ifTrue:[ |
140 | 232 |
aStream nextPut:lineNr. |
233 |
]. |
|
234 |
valueNeeded ifFalse:[ |
|
235 |
aStream nextPut:#drop |
|
236 |
]. |
|
237 |
^ self |
|
238 |
]. |
|
239 |
inlineBlock := true. |
|
240 |
statements isNil ifTrue:[ |
|
241 |
valueNeeded ifTrue:[ |
|
242 |
aStream nextPut:#pushNil |
|
243 |
] |
|
244 |
] ifFalse:[ |
|
245 |
thisStatement := statements. |
|
246 |
[thisStatement notNil] whileTrue:[ |
|
247 |
nextStatement := thisStatement nextStatement. |
|
248 |
(nextStatement notNil or:[valueNeeded not]) ifTrue:[ |
|
249 |
thisStatement codeForSideEffectOn:aStream inBlock:b for:aCompiler |
|
250 |
] ifFalse:[ |
|
251 |
thisStatement codeOn:aStream inBlock:b for:aCompiler |
|
252 |
]. |
|
253 |
thisStatement := nextStatement |
|
254 |
] |
|
255 |
] |
|
256 |
! |
|
257 |
||
258 |
codeOn:aStream inBlock:b for:aCompiler |
|
259 |
|thisStatement nextStatement pos code cheapy| |
|
260 |
||
261 |
cheapy := self checkForSimpleBlock. |
|
262 |
cheapy notNil ifTrue:[ |
|
263 |
cheapy codeOn:aStream inBlock:b for:aCompiler. |
|
264 |
^ self |
|
265 |
]. |
|
266 |
||
267 |
pos := aStream position. |
|
62 | 268 |
|
140 | 269 |
aStream nextPut:#makeBlock. "+0" |
270 |
aStream nextPut:0. "+1" |
|
271 |
aStream nextPut:(blockVars size). "+2" |
|
272 |
aStream nextPut:(blockArgs size). "+3" |
|
273 |
statements isNil ifTrue:[ |
|
274 |
aStream nextPut:#pushNil "+4" |
|
275 |
] ifFalse:[ |
|
276 |
thisStatement := statements. |
|
277 |
[thisStatement notNil] whileTrue:[ |
|
278 |
nextStatement := thisStatement nextStatement. |
|
279 |
nextStatement notNil ifTrue:[ |
|
280 |
thisStatement codeForSideEffectOn:aStream inBlock:self for:aCompiler |
|
281 |
] ifFalse:[ |
|
282 |
thisStatement codeOn:aStream inBlock:self for:aCompiler |
|
283 |
]. |
|
284 |
thisStatement := nextStatement |
|
285 |
] |
|
286 |
]. |
|
287 |
aStream nextPut:#retTop. |
|
288 |
||
289 |
"check for [0]-block; |
|
290 |
these are sometimes used as in ... ifAbsent:[0] |
|
291 |
" |
|
292 |
code := (aStream contents). |
|
293 |
(code at:pos+4) == #push0 ifTrue:[ |
|
294 |
(code at:pos+5) == #retTop ifTrue:[ |
|
295 |
aStream position:pos. |
|
296 |
code grow:pos. |
|
297 |
aStream nextPut:#mk0Block. |
|
298 |
^ self |
|
299 |
] |
|
300 |
]. |
|
301 |
||
302 |
"check for [nil]-block; |
|
303 |
these come to play when code in blocks is commented |
|
304 |
out, or as dummy exception blocks |
|
305 |
" |
|
306 |
code := (aStream contents). |
|
307 |
(code at:pos+4) == #pushNil ifTrue:[ |
|
308 |
(code at:pos+5) == #retTop ifTrue:[ |
|
309 |
aStream position:pos. |
|
310 |
code grow:pos. |
|
311 |
aStream nextPut:#mkNilBlock. |
|
312 |
^ self |
|
313 |
] |
|
314 |
]. |
|
315 |
||
316 |
(aStream contents) at:pos+1 put:(aStream position) |
|
0 | 317 |
! ! |
318 |
||
319 |
!BlockNode methodsFor:'evaluating'! |
|
320 |
||
140 | 321 |
evaluate |
322 |
^ self |
|
323 |
! |
|
324 |
||
0 | 325 |
exitWith:something |
326 |
"return via return-statement" |
|
327 |
||
328 |
home notNil ifTrue:[ |
|
45 | 329 |
home exitWith:something |
0 | 330 |
]. |
331 |
exitBlock value:something. |
|
332 |
^ something |
|
333 |
! |
|
334 |
||
335 |
value |
|
336 |
(blockArgs size ~~ 0) ifTrue:[ |
|
45 | 337 |
^ self wrongNumberOfArguments:0 |
0 | 338 |
]. |
339 |
statements isNil ifTrue:[^ nil]. |
|
340 |
exitBlock := [:val | ^ val]. |
|
341 |
^ statements evaluate |
|
342 |
! |
|
343 |
||
344 |
value:anArg |
|
345 |
|oldValue val| |
|
346 |
||
347 |
(blockArgs size ~~ 1) ifTrue:[ |
|
45 | 348 |
^ self wrongNumberOfArguments:1 |
0 | 349 |
]. |
350 |
statements isNil ifTrue:[^ nil]. |
|
351 |
||
352 |
oldValue := (blockArgs at:1) value. |
|
353 |
(blockArgs at:1) value:anArg. |
|
354 |
||
15 | 355 |
exitBlock := [:v | |
45 | 356 |
(blockArgs at:1) value:oldValue. |
357 |
^ v |
|
0 | 358 |
]. |
359 |
||
360 |
val := statements evaluate. |
|
361 |
||
362 |
(blockArgs at:1) value:oldValue. |
|
363 |
^ val |
|
364 |
! |
|
365 |
||
366 |
value:arg1 value:arg2 |
|
367 |
|oldValue1 oldValue2 val| |
|
368 |
||
369 |
(blockArgs size ~~ 2) ifTrue:[ |
|
45 | 370 |
^ self wrongNumberOfArguments:2 |
0 | 371 |
]. |
372 |
statements isNil ifTrue:[^ nil]. |
|
373 |
||
374 |
oldValue1 := (blockArgs at:1) value. |
|
375 |
oldValue2 := (blockArgs at:2) value. |
|
376 |
(blockArgs at:1) value:arg1. |
|
377 |
(blockArgs at:2) value:arg2. |
|
378 |
||
15 | 379 |
exitBlock := [:v | |
45 | 380 |
(blockArgs at:1) value:oldValue1. |
381 |
(blockArgs at:2) value:oldValue2. |
|
382 |
^ v |
|
0 | 383 |
]. |
384 |
||
385 |
val := statements evaluate. |
|
386 |
||
387 |
(blockArgs at:1) value:oldValue1. |
|
388 |
(blockArgs at:2) value:oldValue2. |
|
389 |
^ val |
|
390 |
! |
|
391 |
||
392 |
value:arg1 value:arg2 value:arg3 |
|
393 |
|oldValue1 oldValue2 oldValue3 val| |
|
394 |
||
395 |
(blockArgs size ~~ 3) ifTrue:[ |
|
45 | 396 |
^ self wrongNumberOfArguments:3 |
0 | 397 |
]. |
398 |
statements isNil ifTrue:[^ nil]. |
|
399 |
||
400 |
oldValue1 := (blockArgs at:1) value. |
|
401 |
oldValue2 := (blockArgs at:2) value. |
|
402 |
oldValue3 := (blockArgs at:3) value. |
|
403 |
(blockArgs at:1) value:arg1. |
|
404 |
(blockArgs at:2) value:arg2. |
|
405 |
(blockArgs at:3) value:arg3. |
|
406 |
||
15 | 407 |
exitBlock := [:v | |
45 | 408 |
(blockArgs at:1) value:oldValue1. |
409 |
(blockArgs at:2) value:oldValue2. |
|
410 |
(blockArgs at:3) value:oldValue3. |
|
411 |
^ v |
|
0 | 412 |
]. |
413 |
||
414 |
val := statements evaluate. |
|
415 |
||
416 |
(blockArgs at:1) value:oldValue1. |
|
417 |
(blockArgs at:2) value:oldValue2. |
|
418 |
(blockArgs at:3) value:oldValue3. |
|
419 |
^ val |
|
420 |
! |
|
421 |
||
422 |
value:arg1 value:arg2 value:arg3 value:arg4 |
|
423 |
|oldValue1 oldValue2 oldValue3 oldValue4 val| |
|
424 |
||
425 |
(blockArgs size ~~ 4) ifTrue:[ |
|
45 | 426 |
^ self wrongNumberOfArguments:4 |
0 | 427 |
]. |
428 |
statements isNil ifTrue:[^ nil]. |
|
429 |
||
430 |
oldValue1 := (blockArgs at:1) value. |
|
431 |
oldValue2 := (blockArgs at:2) value. |
|
432 |
oldValue3 := (blockArgs at:3) value. |
|
433 |
oldValue4 := (blockArgs at:4) value. |
|
434 |
(blockArgs at:1) value:arg1. |
|
435 |
(blockArgs at:2) value:arg2. |
|
436 |
(blockArgs at:3) value:arg3. |
|
437 |
(blockArgs at:4) value:arg4. |
|
438 |
||
15 | 439 |
exitBlock := [:v | |
45 | 440 |
(blockArgs at:1) value:oldValue1. |
441 |
(blockArgs at:2) value:oldValue2. |
|
442 |
(blockArgs at:3) value:oldValue3. |
|
443 |
(blockArgs at:4) value:oldValue4. |
|
444 |
^ v |
|
0 | 445 |
]. |
446 |
||
447 |
val := statements evaluate. |
|
448 |
||
449 |
(blockArgs at:1) value:oldValue1. |
|
450 |
(blockArgs at:2) value:oldValue2. |
|
451 |
(blockArgs at:3) value:oldValue3. |
|
452 |
(blockArgs at:4) value:oldValue4. |
|
453 |
^ val |
|
140 | 454 |
! |
3 | 455 |
|
456 |
valueWithArguments:argArray |
|
457 |
|oldValues val| |
|
458 |
||
459 |
(blockArgs size ~~ argArray size) ifTrue:[ |
|
45 | 460 |
^ self wrongNumberOfArguments:argArray size |
3 | 461 |
]. |
462 |
statements isNil ifTrue:[^ nil]. |
|
463 |
||
464 |
oldValues := Array new:(argArray size). |
|
465 |
1 to:argArray size do:[:i | |
|
45 | 466 |
oldValues at:i put:(blockArgs at:i) value. |
467 |
(blockArgs at:i) value:(argArray at:i). |
|
3 | 468 |
]. |
15 | 469 |
exitBlock := [:v | |
45 | 470 |
1 to:argArray size do:[:i | |
471 |
( blockArgs at:i) value:(oldValues at:i) |
|
472 |
]. |
|
473 |
^ v |
|
3 | 474 |
]. |
475 |
||
476 |
val := statements evaluate. |
|
477 |
||
478 |
1 to:argArray size do:[:i | |
|
45 | 479 |
(blockArgs at:i) value:(oldValues at:i) |
3 | 480 |
]. |
481 |
^ val |
|
140 | 482 |
! |
483 |
||
484 |
wrongNumberOfArguments:numberGiven |
|
485 |
Block argumentSignal |
|
486 |
raiseRequestWith:self |
|
487 |
errorString:('block got ' , numberGiven printString , |
|
488 |
' args while ' , blockArgs size printString , ' where expected') |
|
0 | 489 |
! ! |
490 |
||
491 |
!BlockNode methodsFor:'looping'! |
|
492 |
||
493 |
whileFalse:aBlock |
|
26 | 494 |
self value ifTrue:[^ nil]. |
495 |
aBlock value. |
|
496 |
thisContext restart |
|
0 | 497 |
! |
498 |
||
140 | 499 |
whileTrue:aBlock |
500 |
self value ifFalse:[^ nil]. |
|
501 |
aBlock value. |
|
502 |
thisContext restart |
|
0 | 503 |
! ! |
504 |
||
505 |
!BlockNode methodsFor:'printing'! |
|
506 |
||
507 |
printOn:aStream indent:i |
|
98 | 508 |
|n "{Class: SmallInteger }"| |
509 |
||
0 | 510 |
aStream nextPut:$[. |
98 | 511 |
(n := blockArgs size) > 0 ifTrue:[ |
512 |
1 to:n do:[:index | |
|
96 | 513 |
aStream nextPut:$:. |
514 |
aStream nextPutAll:(blockArgs at:index) name. |
|
515 |
aStream space. |
|
516 |
]. |
|
98 | 517 |
aStream nextPut:$|. |
518 |
aStream space. |
|
519 |
]. |
|
520 |
(n := blockVars size) > 0 ifTrue:[ |
|
521 |
aStream nextPut:$|. |
|
522 |
1 to:n do:[:index | |
|
523 |
aStream nextPutAll:(blockVars at:index) name. |
|
524 |
aStream space. |
|
525 |
]. |
|
526 |
aStream nextPut:$|. |
|
0 | 527 |
]. |
528 |
statements notNil ifTrue:[ |
|
45 | 529 |
aStream cr. |
530 |
statements printAllOn:aStream indent:i + 4. |
|
531 |
aStream cr. |
|
532 |
aStream spaces:i. |
|
0 | 533 |
]. |
534 |
aStream nextPut:$] |
|
535 |
! ! |
|
140 | 536 |
|
537 |
!BlockNode methodsFor:'private accessing'! |
|
538 |
||
539 |
setArguments:argList home:h variables:vars |
|
540 |
inlineBlock := false. |
|
541 |
needsHome := false. |
|
542 |
blockArgs := argList. |
|
543 |
home := h. |
|
544 |
blockVars := vars |
|
545 |
! ! |
|
546 |
||
547 |
!BlockNode methodsFor:'queries'! |
|
548 |
||
549 |
isBlock |
|
550 |
"a kludge, to have blocknodes mimic blocks" |
|
551 |
||
552 |
^ true |
|
168 | 553 |
! |
554 |
||
169 | 555 |
simpleSendBlockExpression |
556 |
blockVars notNil ifTrue:[^ nil]. |
|
557 |
statements isNil ifTrue:[^ nil]. |
|
558 |
statements nextStatement notNil ifTrue:[^ nil]. |
|
559 |
^ statements expression |
|
560 |
||
561 |
"Created: 13.12.1995 / 20:06:09 / cg" |
|
140 | 562 |
! ! |
563 |
||
148 | 564 |
!BlockNode class methodsFor:'documentation'! |
565 |
||
566 |
version |
|
278 | 567 |
^ '$Header: /cvs/stx/stx/libcomp/BlockNode.st,v 1.28 1996-05-18 15:33:26 cg Exp $' |
148 | 568 |
! ! |