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