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