author | Claus Gittinger <cg@exept.de> |
Tue, 25 Oct 2011 17:29:30 +0200 | |
changeset 2749 | 6c9702c9ddc8 |
parent 2596 | bb8c6fe79ce8 |
child 2750 | b6e4ba4a4410 |
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 |
" |
|
1079 | 12 |
"{ Package: 'stx:libcomp' }" |
13 |
||
0 | 14 |
ParseNode subclass:#BlockNode |
168 | 15 |
instanceVariableNames:'blockArgs statements home inlineBlock exitBlock blockVars |
557
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
16 |
needsHome lineNr endLineNr blockArgAccessedInBlock numTemp |
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
17 |
maxNumTemp indexOfFirstTemp subBlocks accessedOuterBlockVars |
2501 | 18 |
possiblyInlined invocationSelector charStartIndex charEndIndex |
19 |
index inlined endPointIndex' |
|
168 | 20 |
classVariableNames:'' |
21 |
poolDictionaries:'' |
|
22 |
category:'System-Compiler-Support' |
|
0 | 23 |
! |
24 |
||
366 | 25 |
!BlockNode class methodsFor:'documentation'! |
20 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
45 | 30 |
All Rights Reserved |
0 | 31 |
|
20 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
0 | 40 |
|
20 | 41 |
documentation |
42 |
" |
|
43 |
node for parse-trees, representing blocks |
|
261 | 44 |
This is a helper class for the compiler. |
263 | 45 |
|
46 |
[author:] |
|
47 |
Claus Gittinger |
|
20 | 48 |
" |
49 |
! ! |
|
0 | 50 |
|
366 | 51 |
!BlockNode class methodsFor:'instance creation'! |
0 | 52 |
|
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
53 |
arguments:argList home:homeBlock variables:vars |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
54 |
|newBlock| |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
55 |
|
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
56 |
newBlock := (self basicNew) setArguments:argList home:homeBlock variables:vars. |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
57 |
homeBlock notNil ifTrue:[ |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
58 |
homeBlock rememberSubBlock:newBlock |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
59 |
]. |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
60 |
^ newBlock |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
61 |
|
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
62 |
"Modified: 28.6.1997 / 15:14:45 / cg" |
0 | 63 |
! ! |
64 |
||
65 |
!BlockNode methodsFor:'accessing'! |
|
66 |
||
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
67 |
accessedOuterBlockVars |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
68 |
"return a collection of outer blockVars/args which are accessed |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
69 |
in this block" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
70 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
71 |
^ accessedOuterBlockVars ? #() |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
72 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
73 |
"Modified: 18.6.1997 / 12:06:31 / cg" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
74 |
"Created: 2.7.1997 / 17:29:56 / cg" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
75 |
! |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
76 |
|
0 | 77 |
arguments |
78 |
^ blockArgs |
|
79 |
! |
|
80 |
||
26 | 81 |
arguments:argList |
82 |
blockArgs := argList |
|
83 |
! |
|
84 |
||
534 | 85 |
blockArgAccessed |
86 |
"return true if any block argument is accessed in the block" |
|
87 |
||
88 |
^ blockArgAccessedInBlock ? false |
|
89 |
||
90 |
"Modified: 18.6.1997 / 12:06:31 / cg" |
|
91 |
! |
|
92 |
||
93 |
blockArgAccessed:aBoolen |
|
94 |
"set/clear the flag stating if any block argument is accessed in the block" |
|
95 |
||
96 |
blockArgAccessedInBlock := aBoolen |
|
97 |
||
98 |
"Created: 18.6.1997 / 11:35:00 / cg" |
|
99 |
"Modified: 18.6.1997 / 12:06:43 / cg" |
|
100 |
! |
|
101 |
||
2501 | 102 |
charEndIndex |
103 |
^ charEndIndex |
|
104 |
! |
|
105 |
||
106 |
charEndIndex:something |
|
107 |
charEndIndex := something. |
|
108 |
! |
|
109 |
||
110 |
charStartIndex |
|
111 |
^ charStartIndex |
|
112 |
! |
|
113 |
||
114 |
charStartIndex:something |
|
115 |
charStartIndex := something. |
|
116 |
! |
|
117 |
||
394 | 118 |
endLineNumber |
119 |
^ endLineNr |
|
120 |
||
121 |
"Created: 23.10.1996 / 15:51:32 / cg" |
|
122 |
! |
|
123 |
||
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
124 |
endLineNumber:aNumber |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
125 |
endLineNr := aNumber |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
126 |
|
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
127 |
"Created: 21.10.1996 / 14:17:57 / cg" |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
128 |
! |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
129 |
|
2501 | 130 |
endPointIndex |
131 |
^ endPointIndex |
|
2129 | 132 |
! |
133 |
||
2501 | 134 |
endPointIndex:something |
135 |
endPointIndex := something. |
|
2129 | 136 |
! |
137 |
||
140 | 138 |
home |
139 |
^ home |
|
0 | 140 |
! |
141 |
||
142 |
home:aBlock |
|
143 |
home := aBlock |
|
144 |
! |
|
145 |
||
559 | 146 |
indexOfFirstTemp:index |
147 |
indexOfFirstTemp := index |
|
148 |
||
149 |
"Created: 25.6.1997 / 17:24:27 / cg" |
|
150 |
! |
|
151 |
||
555 | 152 |
inlineBlock:aBoolean |
153 |
inlineBlock := aBoolean |
|
0 | 154 |
! |
155 |
||
2354 | 156 |
invocationSelector |
157 |
"the selector, by which this block is invoked" |
|
158 |
||
159 |
^ invocationSelector |
|
677 | 160 |
|
2354 | 161 |
"Created: / 27-04-2010 / 11:56:22 / cg" |
162 |
! |
|
677 | 163 |
|
2354 | 164 |
invokationSelector |
165 |
<resource: #obsolete> |
|
166 |
^ invocationSelector |
|
167 |
||
168 |
"Created: / 02-04-1998 / 15:00:06 / cg" |
|
169 |
"Modified: / 27-04-2010 / 11:56:44 / cg" |
|
677 | 170 |
! |
171 |
||
555 | 172 |
isInlineBlock |
2749 | 173 |
^ (inlineBlock == true) |
555 | 174 |
|
2749 | 175 |
"Created: / 25-06-1997 / 14:11:33 / cg" |
0 | 176 |
! |
177 |
||
394 | 178 |
lineNumber |
179 |
^ lineNr |
|
180 |
||
181 |
"Created: 23.10.1996 / 15:51:50 / cg" |
|
182 |
! |
|
183 |
||
140 | 184 |
lineNumber:aNumber |
185 |
lineNr := aNumber |
|
186 |
! |
|
187 |
||
1505 | 188 |
lineNumberOfFirstMessage |
189 |
^ statements lineNumberOfFirstMessage |
|
190 |
||
191 |
"Created: 23.10.1996 / 15:51:50 / cg" |
|
192 |
! |
|
193 |
||
0 | 194 |
needsHome |
195 |
^ needsHome |
|
196 |
! |
|
197 |
||
198 |
needsHome:aBoolean |
|
199 |
needsHome := aBoolean |
|
6 | 200 |
! |
201 |
||
2056 | 202 |
nestingLevel |
203 |
^ home isNil ifTrue:[ 0 ] ifFalse:[ home nestingLevel + 1] |
|
204 |
||
205 |
"Created: / 15-01-2008 / 11:54:57 / cg" |
|
206 |
! |
|
207 |
||
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
208 |
possiblyInlined:aBoolean |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
209 |
possiblyInlined := aBoolean |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
210 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
211 |
"Created: 2.7.1997 / 11:32:00 / cg" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
212 |
! |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
213 |
|
677 | 214 |
possiblyInlined:aBoolean withSelector:selector |
215 |
possiblyInlined := aBoolean. |
|
2354 | 216 |
invocationSelector := selector. |
677 | 217 |
|
2354 | 218 |
"Created: / 02-04-1998 / 19:07:53 / cg" |
219 |
"Modified: / 27-04-2010 / 11:56:51 / cg" |
|
677 | 220 |
! |
221 |
||
140 | 222 |
statements |
223 |
^ statements |
|
224 |
! |
|
225 |
||
226 |
statements:s |
|
2596 | 227 |
|
228 |
statements notNil ifTrue:[statements parent: nil]. |
|
229 |
statements := s. |
|
230 |
statements notNil ifTrue:[statements parent: self]. |
|
231 |
||
232 |
"Modified: / 20-07-2011 / 19:49:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
140 | 233 |
! |
234 |
||
235 |
variables |
|
236 |
^ blockVars |
|
237 |
! |
|
238 |
||
239 |
variables:varList |
|
240 |
blockVars := varList |
|
241 |
! ! |
|
242 |
||
243 |
!BlockNode methodsFor:'block messages'! |
|
244 |
||
245 |
doesNotUnderstand:aMessage |
|
246 |
|numArgs kludgeBlock| |
|
247 |
||
1251
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
248 |
(Block includesSelector:(aMessage selector)) ifTrue:[ |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
249 |
|
1251
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
250 |
"/ mhmh - a message which I dont understand, but Block implements |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
251 |
"/ send it to a kludgeblock, which will evaluate me again ..." |
212
ff39051e219f
more constant folding options
Claus Gittinger <cg@exept.de>
parents:
199
diff
changeset
|
252 |
|
1251
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
253 |
numArgs := blockArgs size. |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
254 |
numArgs == 0 ifTrue:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
255 |
kludgeBlock := [self value] |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
256 |
] ifFalse:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
257 |
numArgs == 1 ifTrue:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
258 |
kludgeBlock := [:a1 | self value:a1]. |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
259 |
] ifFalse:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
260 |
numArgs == 2 ifTrue:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
261 |
kludgeBlock := [:a1 :a2 | self value:a1 value:a2]. |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
262 |
] ifFalse:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
263 |
numArgs == 3 ifTrue:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
264 |
kludgeBlock := [:a1 :a2 :a3| self value:a1 value:a2 value:a3]. |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
265 |
] ifFalse:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
266 |
numArgs == 4 ifTrue:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
267 |
kludgeBlock := [:a1 :a2 :a3 :a4| self value:a1 value:a2 value:a3 value:a4]. |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
268 |
] ifFalse:[ |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
269 |
^ self error:'only support blocks with up-to 4 args' |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
270 |
] |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
271 |
] |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
272 |
] |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
273 |
] |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
274 |
]. |
e5d27409e4f2
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1135
diff
changeset
|
275 |
^ kludgeBlock perform:aMessage selector withArguments:aMessage arguments |
140 | 276 |
]. |
277 |
^ super doesNotUnderstand:aMessage |
|
0 | 278 |
! ! |
279 |
||
140 | 280 |
!BlockNode methodsFor:'code generation'! |
281 |
||
1401 | 282 |
checkForSimpleBlockCodeOn:aStream for:aCompiler |
140 | 283 |
"simple things can be made cheap blocks right now - |
1305 | 284 |
resulting in a simple pushLit instruction ... |
285 |
Return: |
|
286 |
true -- bytecodes are on aStream |
|
287 |
aParseNode -- Parsenode creating the cheap block |
|
288 |
nil -- cheap block not possible |
|
289 |
" |
|
140 | 290 |
|
1401 | 291 |
|cheapy val isConstRet singleInsn code stackSize| |
140 | 292 |
|
932 | 293 |
isConstRet := false. |
1305 | 294 |
|
140 | 295 |
statements isNil ifTrue:[ |
1305 | 296 |
self numArgs == 0 ifTrue:[ |
297 |
"a []-block" |
|
298 |
aStream nextPut:#mkNilBlock. |
|
299 |
^ true. |
|
300 |
]. |
|
932 | 301 |
val := nil. |
302 |
isConstRet := true. |
|
1401 | 303 |
] ifFalse:[statements isConstant ifTrue:[ |
304 |
val := statements expression value. |
|
305 |
self numArgs == 0 ifTrue:[ |
|
306 |
val == 0 ifTrue:[ |
|
307 |
aStream nextPut:#mk0Block. |
|
308 |
^ true. |
|
309 |
]. |
|
310 |
val isNil ifTrue:[ |
|
311 |
aStream nextPut:#mkNilBlock. |
|
312 |
^ true. |
|
313 |
]. |
|
314 |
]. |
|
315 |
isConstRet := true. |
|
316 |
]]. |
|
140 | 317 |
|
932 | 318 |
isConstRet ifTrue:[ |
319 |
stackSize := 0. |
|
506
3dc049d3a2d7
care for stackSize when creating blocks.
Claus Gittinger <cg@exept.de>
parents:
458
diff
changeset
|
320 |
|
1305 | 321 |
(val isInteger and:[val between:-128 and:127]) ifTrue:[ |
322 |
"a [-128..127]-block" |
|
323 |
val < 0 ifTrue:[val := val + 256]. |
|
324 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:#retNum) |
|
325 |
with:val. |
|
326 |
] ifFalse:[val == true ifTrue:[ |
|
327 |
"a [true]-block" |
|
328 |
singleInsn := #retTrue |
|
329 |
] ifFalse:[val == false ifTrue:[ |
|
330 |
"a [false]-block" |
|
331 |
singleInsn := #retFalse |
|
332 |
] ifFalse:[val isNil ifTrue:[ |
|
333 |
"a [nil]-block" |
|
334 |
singleInsn := #retNil |
|
335 |
]]]]. |
|
932 | 336 |
singleInsn notNil ifTrue:[ |
337 |
code := ByteArray with:(ByteCodeCompiler byteCodeFor:singleInsn). |
|
338 |
]. |
|
140 | 339 |
]. |
340 |
||
341 |
code notNil ifTrue:[ |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
342 |
cheapy := CheapBlock |
366 | 343 |
byteCode:code |
344 |
numArgs:(blockArgs size) |
|
1135
7708c6a185a2
must ensure that nArgs and nVar flag setting is correct
Claus Gittinger <cg@exept.de>
parents:
1079
diff
changeset
|
345 |
numVars:0 |
506
3dc049d3a2d7
care for stackSize when creating blocks.
Claus Gittinger <cg@exept.de>
parents:
458
diff
changeset
|
346 |
numStack:stackSize |
366 | 347 |
sourcePosition:nil |
348 |
initialPC:nil |
|
349 |
literals:nil. |
|
1305 | 350 |
^ ConstantNode type:#Block value:cheapy. |
140 | 351 |
]. |
352 |
||
353 |
^ nil |
|
366 | 354 |
|
506
3dc049d3a2d7
care for stackSize when creating blocks.
Claus Gittinger <cg@exept.de>
parents:
458
diff
changeset
|
355 |
"Modified: 13.4.1997 / 00:05:29 / cg" |
140 | 356 |
! |
357 |
||
358 |
codeForSideEffectOn:aStream inBlock:b for:aCompiler |
|
359 |
"generate code for this statement - value not needed. |
|
360 |
For blocks, no code is generated at all." |
|
361 |
||
2129 | 362 |
aCompiler |
2130 | 363 |
warning:(statements isNil |
364 |
ifTrue:[ 'useless empty block.' ] |
|
365 |
ifFalse:[ 'useless block - the statement(s) inside will not be evaluated.' ]) |
|
2129 | 366 |
position:startPosition to:endPosition. |
140 | 367 |
^ self |
368 |
! |
|
369 |
||
370 |
codeInlineOn:aStream inBlock:b for:aCompiler |
|
371 |
self codeInlineOn:aStream inBlock:b valueNeeded:true for:aCompiler |
|
372 |
! |
|
373 |
||
374 |
codeInlineOn:aStream inBlock:b valueNeeded:valueNeeded for:aCompiler |
|
588 | 375 |
|thisStatement nextStatement tmpIndex firstTempIndex codeBlock subAccess| |
140 | 376 |
|
377 |
blockVars notNil ifTrue:[ |
|
559 | 378 |
"/ have to move blockvars into surrounding context |
0 | 379 |
|
559 | 380 |
"/ find the first non-inlined block |
381 |
codeBlock := b. |
|
382 |
[codeBlock notNil and:[codeBlock isInlineBlock]] whileTrue:[ |
|
383 |
codeBlock := codeBlock home. |
|
553 | 384 |
]. |
385 |
||
559 | 386 |
blockVars do:[:aBlockVar | |
387 |
codeBlock isNil ifTrue:[ |
|
388 |
"/ in method - add more temps to the method |
|
389 |
tmpIndex := aCompiler addTempVar. |
|
390 |
] ifFalse:[ |
|
391 |
"/ in another block - add more temps to the block |
|
392 |
tmpIndex := codeBlock addTempVar |
|
393 |
]. |
|
394 |
||
395 |
firstTempIndex isNil ifTrue:[ |
|
396 |
firstTempIndex := tmpIndex. |
|
397 |
indexOfFirstTemp isNil ifTrue:[ |
|
398 |
indexOfFirstTemp := tmpIndex |
|
399 |
]. |
|
400 |
]. |
|
401 |
||
402 |
"/ block vars must be nilled |
|
403 |
"/ (in case the previous block left some value there). |
|
404 |
"/ This nilling should be optimized away, if |
|
405 |
"/ the variable gets a value assigned before the first send. |
|
406 |
||
407 |
aStream nextPut:#pushNil. |
|
408 |
codeBlock isNil ifTrue:[ |
|
409 |
"/ in method |
|
410 |
(tmpIndex <= 6) ifTrue:[ |
|
411 |
aStream nextPut:(#(storeMethodVar1 storeMethodVar2 |
|
412 |
storeMethodVar3 storeMethodVar4 |
|
413 |
storeMethodVar5 storeMethodVar6) at:tmpIndex). |
|
414 |
] ifFalse:[ |
|
415 |
aStream nextPut:#storeMethodVar; nextPut:tmpIndex. |
|
416 |
] |
|
417 |
] ifFalse:[ |
|
418 |
"/ in another block |
|
419 |
aStream nextPut:#storeBlockVar; nextPut:tmpIndex. |
|
420 |
] |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
421 |
]. |
140 | 422 |
]. |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
423 |
|
140 | 424 |
inlineBlock := true. |
555 | 425 |
|
140 | 426 |
statements isNil ifTrue:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
427 |
valueNeeded ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
428 |
aStream nextPut:#pushNil |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
429 |
] |
140 | 430 |
] ifFalse:[ |
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
431 |
thisStatement := statements. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
432 |
[thisStatement notNil] whileTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
433 |
nextStatement := thisStatement nextStatement. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
434 |
(nextStatement notNil or:[valueNeeded not]) ifTrue:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
435 |
thisStatement codeForSideEffectOn:aStream inBlock:b for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
436 |
] ifFalse:[ |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
437 |
thisStatement codeOn:aStream inBlock:b for:aCompiler |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
438 |
]. |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
439 |
thisStatement := nextStatement |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
440 |
] |
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
441 |
]. |
559 | 442 |
|
386 | 443 |
"/ endLineNr notNil ifTrue:[ |
444 |
"/ ParseNode codeLineNumber:endLineNr on:aStream for:aCompiler |
|
445 |
"/ ]. |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
446 |
|
559 | 447 |
"/ pop off temps (blockVars). |
448 |
"/ also, they are nilled to prevent any temp stuff |
|
449 |
"/ from NOT being garbage collected. |
|
450 |
"/ could optimize here, temps are reused soon. |
|
451 |
||
452 |
blockVars notNil ifTrue:[ |
|
588 | 453 |
"/ if any subblock refers to locals or args, |
454 |
"/ do not nil out ... |
|
455 |
subAccess := false. |
|
456 |
self allSubBlocksDo:[:aSubBlockNode | |
|
457 |
aSubBlockNode accessedOuterBlockVars do:[:aVarNode | |
|
458 |
|b| |
|
459 |
||
460 |
aVarNode block == self ifTrue:[ |
|
461 |
subAccess := true |
|
462 |
] |
|
463 |
] |
|
464 |
]. |
|
465 |
subAccess ifFalse:[ |
|
466 |
tmpIndex := firstTempIndex. |
|
467 |
||
559 | 468 |
blockVars do:[:dummy | |
469 |
aStream nextPut:#pushNil. |
|
588 | 470 |
codeBlock isNil ifTrue:[ |
471 |
aStream nextPut:#storeMethodVar; nextPut:tmpIndex. |
|
593
41a1c4554d83
oops - nilling of inlined subBlock vars ...
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
472 |
aCompiler removeTempVar. |
588 | 473 |
] ifFalse:[ |
474 |
aStream nextPut:#storeBlockVar; nextPut:tmpIndex. |
|
593
41a1c4554d83
oops - nilling of inlined subBlock vars ...
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
475 |
codeBlock removeTempVar. |
588 | 476 |
]. |
477 |
tmpIndex := tmpIndex + 1. |
|
478 |
]. |
|
479 |
]. |
|
559 | 480 |
]. |
481 |
||
593
41a1c4554d83
oops - nilling of inlined subBlock vars ...
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
482 |
"Modified: 30.7.1997 / 12:21:07 / cg" |
140 | 483 |
! |
484 |
||
485 |
codeOn:aStream inBlock:b for:aCompiler |
|
1305 | 486 |
|thisStatement nextStatement lastStatement pos code cheapy| |
140 | 487 |
|
1401 | 488 |
cheapy := self checkForSimpleBlockCodeOn:aStream for:aCompiler. |
140 | 489 |
cheapy notNil ifTrue:[ |
1305 | 490 |
"if result == true - code is already on aStream" |
491 |
cheapy ~~ true ifTrue:[ |
|
492 |
cheapy codeOn:aStream inBlock:b for:aCompiler. |
|
493 |
]. |
|
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
494 |
^ self |
140 | 495 |
]. |
496 |
||
1305 | 497 |
"cheap block detection filters out blocks with no statements" |
498 |
||
1372
3aef25b07d7a
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
499 |
pos := aStream position1Based. |
62 | 500 |
|
555 | 501 |
aStream nextPut:#makeBlock. "+0" |
502 |
aStream nextPut:0. "+1" |
|
1723 | 503 |
aStream nextPut:(blockVars size + (maxNumTemp?0)). "+2" |
555 | 504 |
aStream nextPut:(blockArgs size). "+3" |
505 |
"+4" |
|
1305 | 506 |
thisStatement := statements. |
507 |
[thisStatement notNil] whileTrue:[ |
|
508 |
nextStatement := thisStatement nextStatement. |
|
509 |
nextStatement notNil ifTrue:[ |
|
510 |
thisStatement codeForSideEffectOn:aStream inBlock:self for:aCompiler |
|
511 |
] ifFalse:[ |
|
512 |
lastStatement := thisStatement |
|
379
24399d5c465c
optionally mark the blocks-end with a lineNr.
Claus Gittinger <cg@exept.de>
parents:
366
diff
changeset
|
513 |
]. |
1305 | 514 |
thisStatement := nextStatement |
140 | 515 |
]. |
516 |
||
1305 | 517 |
lastStatement |
518 |
codeForSimpleReturnOn:aStream |
|
519 |
inBlock:self |
|
520 |
lineNumber:endLineNr |
|
521 |
for:aCompiler. |
|
522 |
||
562
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
523 |
code := (aStream contents). |
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
524 |
|
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
525 |
"/ sigh - during coding, inlined subBlocks may have added more |
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
526 |
"/ tempVars; patch the nvar byte ... |
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
527 |
|
1723 | 528 |
code at:pos+2 put:(blockVars size + (maxNumTemp?0)). |
562
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
529 |
|
1305 | 530 |
"set the end of the block's code" |
1372
3aef25b07d7a
code rewritten to be independent of stream zero-base
Claus Gittinger <cg@exept.de>
parents:
1319
diff
changeset
|
531 |
code at:pos+1 put:(aStream position1Based) |
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
532 |
|
562
36bd00734940
care for numVars in makeBlock (inlined blocks add vars)
Claus Gittinger <cg@exept.de>
parents:
559
diff
changeset
|
533 |
"Modified: 26.6.1997 / 10:48:56 / cg" |
555 | 534 |
! ! |
535 |
||
536 |
!BlockNode methodsFor:'code generation helpers'! |
|
537 |
||
538 |
addTempVar |
|
539 |
"add a temporary variable; return its position (1-based). |
|
540 |
Used when a block with args/locals is inlined." |
|
541 |
||
2749 | 542 |
(inlineBlock == true) ifTrue:[ |
1319 | 543 |
self error:'addTempVar to inlineBlock - should not happen' |
585 | 544 |
]. |
559 | 545 |
|
557
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
546 |
numTemp isNil ifTrue:[numTemp := maxNumTemp := 0]. |
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
547 |
numTemp := numTemp + 1. |
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
548 |
maxNumTemp := maxNumTemp max:numTemp. |
559 | 549 |
^ numTemp + self numVars |
555 | 550 |
|
2749 | 551 |
"Modified: / 25-10-2011 / 16:49:50 / cg" |
555 | 552 |
! |
553 |
||
554 |
removeTempVar |
|
555 |
"remove a temporary variable" |
|
556 |
||
557
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
557 |
numTemp := numTemp - 1. |
555 | 558 |
|
559 |
"Created: 25.6.1997 / 14:04:20 / cg" |
|
557
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
560 |
"Modified: 25.6.1997 / 15:07:07 / cg" |
0 | 561 |
! ! |
562 |
||
1035 | 563 |
!BlockNode methodsFor:'enumerating'! |
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
564 |
|
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
565 |
allSubBlocksDo:aBlock |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
566 |
"recursively enumerate all of my subblocks" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
567 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
568 |
subBlocks notNil ifTrue:[ |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
569 |
subBlocks do:[:aSubBlockNode | |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
570 |
aBlock value:aSubBlockNode. |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
571 |
aSubBlockNode allSubBlocksDo:aBlock. |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
572 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
573 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
574 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
575 |
"Created: 2.7.1997 / 10:51:59 / cg" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
576 |
! |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
577 |
|
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
578 |
nodeDo:anEnumerator |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
579 |
"helper for parse tree walking" |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
580 |
|
544
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
581 |
|args| |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
582 |
|
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
583 |
args := blockArgs ? #(). |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
584 |
args := args collect:[:var | |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
585 |
|p| |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
586 |
|
1933 | 587 |
p := Variable name:var. |
588 |
"/ p := ParameterNode new. |
|
589 |
"/ p variable:var |
|
544
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
590 |
]. |
fcfff6f8bc31
more ST80 compatible enumeration
Claus Gittinger <cg@exept.de>
parents:
539
diff
changeset
|
591 |
^ anEnumerator doBlock:self arguments:args body:statements |
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
592 |
|
1933 | 593 |
"Created: / 19-06-1997 / 16:38:30 / cg" |
594 |
"Modified: / 22-10-2006 / 12:03:27 / cg" |
|
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
595 |
! ! |
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
534
diff
changeset
|
596 |
|
1035 | 597 |
!BlockNode methodsFor:'evaluation'! |
0 | 598 |
|
1384 | 599 |
evaluateIn:anEnvironment |
140 | 600 |
^ self |
601 |
! |
|
602 |
||
0 | 603 |
exitWith:something |
604 |
"return via return-statement" |
|
605 |
||
606 |
home notNil ifTrue:[ |
|
45 | 607 |
home exitWith:something |
0 | 608 |
]. |
609 |
exitBlock value:something. |
|
610 |
^ something |
|
611 |
! |
|
612 |
||
613 |
value |
|
614 |
(blockArgs size ~~ 0) ifTrue:[ |
|
1390 | 615 |
^ self wrongNumberOfArguments:0 |
0 | 616 |
]. |
617 |
statements isNil ifTrue:[^ nil]. |
|
618 |
exitBlock := [:val | ^ val]. |
|
619 |
^ statements evaluate |
|
620 |
! |
|
621 |
||
622 |
value:anArg |
|
623 |
|oldValue val| |
|
624 |
||
625 |
(blockArgs size ~~ 1) ifTrue:[ |
|
1390 | 626 |
^ self wrongNumberOfArguments:1 |
0 | 627 |
]. |
628 |
statements isNil ifTrue:[^ nil]. |
|
629 |
||
630 |
oldValue := (blockArgs at:1) value. |
|
631 |
(blockArgs at:1) value:anArg. |
|
632 |
||
15 | 633 |
exitBlock := [:v | |
1390 | 634 |
(blockArgs at:1) value:oldValue. |
635 |
^ v |
|
0 | 636 |
]. |
637 |
||
638 |
val := statements evaluate. |
|
639 |
||
640 |
(blockArgs at:1) value:oldValue. |
|
641 |
^ val |
|
642 |
! |
|
643 |
||
644 |
value:arg1 value:arg2 |
|
645 |
|oldValue1 oldValue2 val| |
|
646 |
||
647 |
(blockArgs size ~~ 2) ifTrue:[ |
|
1390 | 648 |
^ self wrongNumberOfArguments:2 |
0 | 649 |
]. |
650 |
statements isNil ifTrue:[^ nil]. |
|
651 |
||
652 |
oldValue1 := (blockArgs at:1) value. |
|
653 |
oldValue2 := (blockArgs at:2) value. |
|
654 |
(blockArgs at:1) value:arg1. |
|
655 |
(blockArgs at:2) value:arg2. |
|
656 |
||
15 | 657 |
exitBlock := [:v | |
1390 | 658 |
(blockArgs at:1) value:oldValue1. |
659 |
(blockArgs at:2) value:oldValue2. |
|
660 |
^ v |
|
0 | 661 |
]. |
662 |
||
663 |
val := statements evaluate. |
|
664 |
||
665 |
(blockArgs at:1) value:oldValue1. |
|
666 |
(blockArgs at:2) value:oldValue2. |
|
667 |
^ val |
|
668 |
! |
|
669 |
||
670 |
value:arg1 value:arg2 value:arg3 |
|
671 |
|oldValue1 oldValue2 oldValue3 val| |
|
672 |
||
673 |
(blockArgs size ~~ 3) ifTrue:[ |
|
1390 | 674 |
^ self wrongNumberOfArguments:3 |
0 | 675 |
]. |
676 |
statements isNil ifTrue:[^ nil]. |
|
677 |
||
678 |
oldValue1 := (blockArgs at:1) value. |
|
679 |
oldValue2 := (blockArgs at:2) value. |
|
680 |
oldValue3 := (blockArgs at:3) value. |
|
681 |
(blockArgs at:1) value:arg1. |
|
682 |
(blockArgs at:2) value:arg2. |
|
683 |
(blockArgs at:3) value:arg3. |
|
684 |
||
15 | 685 |
exitBlock := [:v | |
1390 | 686 |
(blockArgs at:1) value:oldValue1. |
687 |
(blockArgs at:2) value:oldValue2. |
|
688 |
(blockArgs at:3) value:oldValue3. |
|
689 |
^ v |
|
0 | 690 |
]. |
691 |
||
692 |
val := statements evaluate. |
|
693 |
||
694 |
(blockArgs at:1) value:oldValue1. |
|
695 |
(blockArgs at:2) value:oldValue2. |
|
696 |
(blockArgs at:3) value:oldValue3. |
|
697 |
^ val |
|
698 |
! |
|
699 |
||
700 |
value:arg1 value:arg2 value:arg3 value:arg4 |
|
701 |
|oldValue1 oldValue2 oldValue3 oldValue4 val| |
|
702 |
||
703 |
(blockArgs size ~~ 4) ifTrue:[ |
|
1390 | 704 |
^ self wrongNumberOfArguments:4 |
0 | 705 |
]. |
706 |
statements isNil ifTrue:[^ nil]. |
|
707 |
||
708 |
oldValue1 := (blockArgs at:1) value. |
|
709 |
oldValue2 := (blockArgs at:2) value. |
|
710 |
oldValue3 := (blockArgs at:3) value. |
|
711 |
oldValue4 := (blockArgs at:4) value. |
|
712 |
(blockArgs at:1) value:arg1. |
|
713 |
(blockArgs at:2) value:arg2. |
|
714 |
(blockArgs at:3) value:arg3. |
|
715 |
(blockArgs at:4) value:arg4. |
|
716 |
||
15 | 717 |
exitBlock := [:v | |
1390 | 718 |
(blockArgs at:1) value:oldValue1. |
719 |
(blockArgs at:2) value:oldValue2. |
|
720 |
(blockArgs at:3) value:oldValue3. |
|
721 |
(blockArgs at:4) value:oldValue4. |
|
722 |
^ v |
|
0 | 723 |
]. |
724 |
||
725 |
val := statements evaluate. |
|
726 |
||
727 |
(blockArgs at:1) value:oldValue1. |
|
728 |
(blockArgs at:2) value:oldValue2. |
|
729 |
(blockArgs at:3) value:oldValue3. |
|
730 |
(blockArgs at:4) value:oldValue4. |
|
731 |
^ val |
|
140 | 732 |
! |
3 | 733 |
|
734 |
valueWithArguments:argArray |
|
735 |
|oldValues val| |
|
736 |
||
737 |
(blockArgs size ~~ argArray size) ifTrue:[ |
|
1390 | 738 |
^ self wrongNumberOfArguments:argArray size |
3 | 739 |
]. |
740 |
statements isNil ifTrue:[^ nil]. |
|
741 |
||
742 |
oldValues := Array new:(argArray size). |
|
743 |
1 to:argArray size do:[:i | |
|
1390 | 744 |
oldValues at:i put:(blockArgs at:i) value. |
745 |
(blockArgs at:i) value:(argArray at:i). |
|
3 | 746 |
]. |
15 | 747 |
exitBlock := [:v | |
1390 | 748 |
1 to:argArray size do:[:i | |
749 |
( blockArgs at:i) value:(oldValues at:i) |
|
750 |
]. |
|
751 |
^ v |
|
3 | 752 |
]. |
753 |
||
754 |
val := statements evaluate. |
|
755 |
||
756 |
1 to:argArray size do:[:i | |
|
1390 | 757 |
(blockArgs at:i) value:(oldValues at:i) |
3 | 758 |
]. |
759 |
^ val |
|
140 | 760 |
! |
761 |
||
1390 | 762 |
wrongNumberOfArguments:numArgsGiven |
584
23b2124d7765
Fix spelling for wrongNumberOfArguments message.
Stefan Vogel <sv@exept.de>
parents:
579
diff
changeset
|
763 |
"report that the number of arguments given does not match the number expected" |
23b2124d7765
Fix spelling for wrongNumberOfArguments message.
Stefan Vogel <sv@exept.de>
parents:
579
diff
changeset
|
764 |
|
1390 | 765 |
WrongNumberOfArgumentsError |
584
23b2124d7765
Fix spelling for wrongNumberOfArguments message.
Stefan Vogel <sv@exept.de>
parents:
579
diff
changeset
|
766 |
raiseRequestWith:self |
2077
127ac6e095ad
wrongNumberOfArguments: error message
Claus Gittinger <cg@exept.de>
parents:
2056
diff
changeset
|
767 |
errorString:('block got %1 arg(s) where %2 expected' |
127ac6e095ad
wrongNumberOfArguments: error message
Claus Gittinger <cg@exept.de>
parents:
2056
diff
changeset
|
768 |
bindWith:numArgsGiven |
127ac6e095ad
wrongNumberOfArguments: error message
Claus Gittinger <cg@exept.de>
parents:
2056
diff
changeset
|
769 |
with:blockArgs size) |
584
23b2124d7765
Fix spelling for wrongNumberOfArguments message.
Stefan Vogel <sv@exept.de>
parents:
579
diff
changeset
|
770 |
|
23b2124d7765
Fix spelling for wrongNumberOfArguments message.
Stefan Vogel <sv@exept.de>
parents:
579
diff
changeset
|
771 |
"Modified: 8.7.1997 / 01:04:27 / stefan" |
0 | 772 |
! ! |
773 |
||
774 |
!BlockNode methodsFor:'looping'! |
|
775 |
||
776 |
whileFalse:aBlock |
|
26 | 777 |
self value ifTrue:[^ nil]. |
778 |
aBlock value. |
|
779 |
thisContext restart |
|
0 | 780 |
! |
781 |
||
140 | 782 |
whileTrue:aBlock |
1853 | 783 |
"evaluate the argument, aBlock while the receiver evaluates to true." |
784 |
||
140 | 785 |
self value ifFalse:[^ nil]. |
786 |
aBlock value. |
|
787 |
thisContext restart |
|
1853 | 788 |
|
789 |
"Modified: / 22-08-2006 / 14:40:48 / cg" |
|
0 | 790 |
! ! |
791 |
||
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
792 |
!BlockNode methodsFor:'misc'! |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
793 |
|
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
794 |
rememberOuterBlockVarAccess:aVariableNode |
579 | 795 |
accessedOuterBlockVars isNil ifTrue:[ |
796 |
accessedOuterBlockVars := OrderedCollection new. |
|
797 |
]. |
|
798 |
accessedOuterBlockVars add:aVariableNode |
|
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
799 |
|
579 | 800 |
"Modified: 2.7.1997 / 18:52:49 / cg" |
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
801 |
! |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
802 |
|
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
803 |
rememberSubBlock:aBlockNode |
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
804 |
subBlocks isNil ifTrue:[ |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
805 |
subBlocks := OrderedCollection new. |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
806 |
]. |
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
807 |
subBlocks add:aBlockNode |
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
808 |
|
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
809 |
"Created: 28.6.1997 / 15:13:20 / cg" |
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
810 |
"Modified: 2.7.1997 / 11:11:26 / cg" |
569
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
811 |
! ! |
88a1b1d118fd
preps to remember used args/vars
Claus Gittinger <cg@exept.de>
parents:
562
diff
changeset
|
812 |
|
1079 | 813 |
!BlockNode methodsFor:'printing & storing'! |
0 | 814 |
|
815 |
printOn:aStream indent:i |
|
98 | 816 |
|n "{Class: SmallInteger }"| |
817 |
||
0 | 818 |
aStream nextPut:$[. |
98 | 819 |
(n := blockArgs size) > 0 ifTrue:[ |
1723 | 820 |
1 to:n do:[:index | |
821 |
aStream nextPut:$:. |
|
822 |
aStream nextPutAll:(blockArgs at:index) name. |
|
823 |
aStream space. |
|
824 |
]. |
|
825 |
aStream nextPutAll:'| '. |
|
98 | 826 |
]. |
827 |
(n := blockVars size) > 0 ifTrue:[ |
|
1723 | 828 |
aStream nextPut:$|. |
829 |
1 to:n do:[:index | |
|
830 |
aStream nextPutAll:(blockVars at:index) name. |
|
831 |
aStream space. |
|
832 |
]. |
|
833 |
aStream nextPut:$|. |
|
0 | 834 |
]. |
835 |
statements notNil ifTrue:[ |
|
1723 | 836 |
aStream cr. |
837 |
statements printAllOn:aStream indent:i + 4. |
|
838 |
aStream cr. |
|
839 |
aStream spaces:i. |
|
0 | 840 |
]. |
841 |
aStream nextPut:$] |
|
842 |
! ! |
|
140 | 843 |
|
1415 | 844 |
!BlockNode methodsFor:'private-accessing'! |
140 | 845 |
|
846 |
setArguments:argList home:h variables:vars |
|
847 |
inlineBlock := false. |
|
848 |
needsHome := false. |
|
849 |
blockArgs := argList. |
|
850 |
home := h. |
|
557
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
851 |
blockVars := vars. |
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
852 |
numTemp := maxNumTemp := 0. |
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
853 |
|
8202a13be88b
preps to move vars of inlined blocks to outer context
Claus Gittinger <cg@exept.de>
parents:
555
diff
changeset
|
854 |
"Modified: 25.6.1997 / 15:07:52 / cg" |
140 | 855 |
! ! |
856 |
||
857 |
!BlockNode methodsFor:'queries'! |
|
858 |
||
1566
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
859 |
argumentCount |
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
860 |
"ANSI alias for numArgs: return the number of arguments the block represented by myself |
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
861 |
expects for evaluation" |
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
862 |
|
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
863 |
^ blockArgs size |
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
864 |
! |
e53724995493
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
1513
diff
changeset
|
865 |
|
394 | 866 |
collectBlocksInto:aCollection |
867 |
aCollection add:self. |
|
458 | 868 |
statements notNil ifTrue:[statements collectBlocksInto:aCollection] |
394 | 869 |
|
870 |
"Created: 23.10.1996 / 15:45:16 / cg" |
|
871 |
"Modified: 23.10.1996 / 16:02:57 / cg" |
|
872 |
! |
|
873 |
||
2383 | 874 |
containsReturn |
875 |
statements isNil ifTrue:[ |
|
876 |
^ false |
|
877 |
]. |
|
878 |
^ statements containsReturnInAnyStatement |
|
879 |
! |
|
880 |
||
333
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
881 |
endsWithReturn |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
882 |
statements isNil ifTrue:[ |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
883 |
^ false |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
884 |
]. |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
885 |
^ statements listEndsWithReturn |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
886 |
|
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
887 |
"Created: 19.8.1996 / 14:36:32 / cg" |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
888 |
! |
96bb5c3dab61
added query if blocks statementList ends in a return;
Claus Gittinger <cg@exept.de>
parents:
278
diff
changeset
|
889 |
|
559 | 890 |
indexOfFirstTemp |
891 |
^ indexOfFirstTemp |
|
892 |
||
893 |
"Created: 25.6.1997 / 15:39:11 / cg" |
|
894 |
! |
|
895 |
||
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
896 |
isInlinable |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
897 |
"return true, if the receiver is inlinable. |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
898 |
For now, do NOT inline a block, if it has args/vars |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
899 |
which are accessed by subblocks, which are themself |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
900 |
not inlinable. |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
901 |
This limitation is needed for the following piece of code to work: |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
902 |
1 to:10 do:[:i | |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
903 |
.... [ something with i ] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
904 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
905 |
If this block was inlined, each subblock would get the same i |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
906 |
(which is the old ST/V behavior)" |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
907 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
908 |
possiblyInlined == true ifFalse:[ |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
909 |
^ false |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
910 |
]. |
2152 | 911 |
inlined == true ifTrue:[ |
912 |
^ true |
|
913 |
]. |
|
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
914 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
915 |
(self numArgs ~~ 0 or:[self numVars ~~ 0]) ifTrue:[ |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
916 |
"/ any subblock, which accesses a var/arg of myself ? |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
917 |
self allSubBlocksDo:[:aSubBlockNode | |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
918 |
aSubBlockNode accessedOuterBlockVars do:[:aVarNode | |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
919 |
|b| |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
920 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
921 |
aVarNode block == self ifTrue:[ |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
922 |
"/ all-inbetween inlinable ? |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
923 |
b := aSubBlockNode. |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
924 |
[b ~~ self] whileTrue:[ |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
925 |
b isInlinable ifFalse:[ |
575 | 926 |
"/ 'not inlined due to access: ' print. aVarNode displayString printCR. |
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
927 |
^ false |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
928 |
]. |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
929 |
b := b home |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
930 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
931 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
932 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
933 |
] |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
934 |
]. |
2152 | 935 |
inlined := true. |
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
936 |
^ true |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
937 |
|
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
938 |
"Created: 2.7.1997 / 10:43:37 / cg" |
579 | 939 |
"Modified: 2.7.1997 / 18:55:36 / cg" |
574
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
940 |
! |
993d1182782b
fixed inlining of blocks where subBlocks
Claus Gittinger <cg@exept.de>
parents:
569
diff
changeset
|
941 |
|
394 | 942 |
numArgs |
522 | 943 |
"return the number of arguments the block represented by myself |
944 |
expects for evaluation" |
|
945 |
||
394 | 946 |
^ blockArgs size |
947 |
||
948 |
"Created: 23.10.1996 / 15:57:04 / cg" |
|
522 | 949 |
"Modified: 7.5.1997 / 15:34:35 / cg" |
394 | 950 |
! |
951 |
||
952 |
numVars |
|
953 |
^ blockVars size |
|
954 |
||
955 |
"Created: 23.10.1996 / 16:17:07 / cg" |
|
956 |
! |
|
957 |
||
169 | 958 |
simpleSendBlockExpression |
959 |
blockVars notNil ifTrue:[^ nil]. |
|
960 |
statements isNil ifTrue:[^ nil]. |
|
961 |
statements nextStatement notNil ifTrue:[^ nil]. |
|
962 |
^ statements expression |
|
963 |
||
964 |
"Created: 13.12.1995 / 20:06:09 / cg" |
|
140 | 965 |
! ! |
966 |
||
2325 | 967 |
!BlockNode methodsFor:'testing'! |
968 |
||
969 |
isBlock |
|
970 |
"a kludge, to have blocknodes mimic blocks" |
|
971 |
||
972 |
^ true |
|
973 |
! |
|
974 |
||
975 |
isBlockNode |
|
976 |
^ true |
|
977 |
! |
|
978 |
||
979 |
isEmptyBlock |
|
980 |
^ statements isNil |
|
981 |
! |
|
982 |
||
983 |
isJavaScriptBlock |
|
984 |
^ false |
|
985 |
! |
|
986 |
||
987 |
isSingleExpressionBlock |
|
988 |
^ statements notNil |
|
989 |
and:[ statements nextStatement isNil |
|
990 |
and:[ statements isSimpleExpression ]] |
|
991 |
! ! |
|
992 |
||
2596 | 993 |
!BlockNode methodsFor:'visiting'! |
994 |
||
995 |
acceptVisitor:aVisitor |
|
996 |
"Double dispatch back to the visitor, passing my type encoded in |
|
997 |
the selector (visitor pattern)" |
|
998 |
||
999 |
"stub code automatically generated - please change if required" |
|
1000 |
||
1001 |
^ aVisitor visitBlockNode:self |
|
1002 |
! ! |
|
1003 |
||
366 | 1004 |
!BlockNode class methodsFor:'documentation'! |
148 | 1005 |
|
2501 | 1006 |
version_CVS |
2749 | 1007 |
^ '$Header: /cvs/stx/stx/libcomp/BlockNode.st,v 1.86 2011-10-25 15:29:30 cg Exp $' |
2325 | 1008 |
! |
1009 |
||
2501 | 1010 |
version_SVN |
2596 | 1011 |
^ '§ Id §' |
148 | 1012 |
! ! |