[gforth] / gforth / glocals.fs  

gforth: gforth/glocals.fs


1 : anton 1.15 \ A powerful locals implementation
2 :    
3 : anton 1.59 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2007 Free Software Foundation, Inc.
4 : anton 1.15
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 : anton 1.60 \ as published by the Free Software Foundation, either version 3
10 : anton 1.15 \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 : anton 1.60 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.15
20 :    
21 : anton 1.16 \ More documentation can be found in the manual and in
22 :     \ http://www.complang.tuwien.ac.at/papers/ertl94l.ps.gz
23 :    
24 : anton 1.1 \ Local variables are quite important for writing readable programs, but
25 :     \ IMO (anton) they are the worst part of the standard. There they are very
26 :     \ restricted and have an ugly interface.
27 :    
28 :     \ So, we implement the locals wordset, but do not recommend using
29 :     \ locals-ext (which is a really bad user interface for locals).
30 :    
31 :     \ We also have a nice and powerful user-interface for locals: locals are
32 :     \ defined with
33 :    
34 :     \ { local1 local2 ... }
35 :     \ or
36 :     \ { local1 local2 ... -- ... }
37 :     \ (anything after the -- is just a comment)
38 :    
39 :     \ Every local in this list consists of an optional type specification
40 :     \ and a name. If there is only the name, it stands for a cell-sized
41 :     \ value (i.e., you get the value of the local variable, not it's
42 :     \ address). The following type specifiers stand before the name:
43 :    
44 :     \ Specifier Type Access
45 :     \ W: Cell value
46 :     \ W^ Cell address
47 :     \ D: Double value
48 :     \ D^ Double address
49 :     \ F: Float value
50 :     \ F^ Float address
51 :     \ C: Char value
52 :     \ C^ Char address
53 :    
54 :     \ The local variables are initialized with values from the appropriate
55 :     \ stack. In contrast to the examples in the standard document our locals
56 :     \ take the arguments in the expected way: The last local gets the top of
57 :     \ stack, the second last gets the second stack item etc. An example:
58 :    
59 :     \ : CX* { F: Ar F: Ai F: Br F: Bi -- Cr Ci }
60 :     \ \ complex multiplication
61 :     \ Ar Br f* Ai Bi f* f-
62 :     \ Ar Bi f* Ai Br f* f+ ;
63 :    
64 :     \ There will also be a way to add user types, but it is not yet decided,
65 :     \ how. Ideas are welcome.
66 :    
67 :     \ Locals defined in this manner live until (!! see below).
68 :     \ Their names can be used during this time to get
69 :     \ their value or address; The addresses produced in this way become
70 :     \ invalid at the end of the lifetime.
71 :    
72 :     \ Values can be changed with TO, but this is not recomended (TO is a
73 :     \ kludge and words lose the single-assignment property, which makes them
74 :     \ harder to analyse).
75 :    
76 :     \ As for the internals, we use a special locals stack. This eliminates
77 :     \ the problems and restrictions of reusing the return stack and allows
78 :     \ to store floats as locals: the return stack is not guaranteed to be
79 :     \ aligned correctly, but our locals stack must be float-aligned between
80 :     \ words.
81 :    
82 :     \ Other things about the internals are pretty unclear now.
83 :    
84 :     \ Currently locals may only be
85 :     \ defined at the outer level and TO is not supported.
86 :    
87 : anton 1.33 require search.fs
88 : anton 1.14 require float.fs
89 : jwilke 1.47 require extend.fs \ for case
90 : anton 1.1
91 : anton 1.14 : compile-@local ( n -- ) \ gforth compile-fetch-local
92 : anton 1.3 case
93 : pazsan 1.7 0 of postpone @local0 endof
94 :     1 cells of postpone @local1 endof
95 :     2 cells of postpone @local2 endof
96 :     3 cells of postpone @local3 endof
97 : anton 1.3 ( otherwise ) dup postpone @local# ,
98 :     endcase ;
99 :    
100 : anton 1.14 : compile-f@local ( n -- ) \ gforth compile-f-fetch-local
101 : anton 1.3 case
102 : pazsan 1.7 0 of postpone f@local0 endof
103 :     1 floats of postpone f@local1 endof
104 : anton 1.3 ( otherwise ) dup postpone f@local# ,
105 :     endcase ;
106 :    
107 : pazsan 1.27 \ locals stuff needed for control structures
108 :    
109 :     : compile-lp+! ( n -- ) \ gforth compile-l-p-plus-store
110 :     dup negate locals-size +!
111 :     0 over = if
112 :     else -1 cells over = if postpone lp-
113 :     else 1 floats over = if postpone lp+
114 :     else 2 floats over = if postpone lp+2
115 :     else postpone lp+!# dup ,
116 :     then then then then drop ;
117 :    
118 :     : adjust-locals-size ( n -- ) \ gforth
119 :     \ sets locals-size to n and generates an appropriate lp+!
120 :     locals-size @ swap - compile-lp+! ;
121 :    
122 : anton 1.1 \ the locals stack grows downwards (see primitives)
123 :     \ of the local variables of a group (in braces) the leftmost is on top,
124 :     \ i.e. by going onto the locals stack the order is reversed.
125 :     \ there are alignment gaps if necessary.
126 :     \ lp must have the strictest alignment (usually float) across calls;
127 :     \ for simplicity we align it strictly for every group.
128 :    
129 : anton 1.5 slowvoc @
130 :     slowvoc on \ we want a linked list for the vocabulary locals
131 : anton 1.1 vocabulary locals \ this contains the local variables
132 : pazsan 1.37 ' locals >body wordlist-id ' locals-list >body !
133 : anton 1.5 slowvoc !
134 : anton 1.1
135 :     create locals-buffer 1000 allot \ !! limited and unsafe
136 :     \ here the names of the local variables are stored
137 :     \ we would have problems storing them at the normal dp
138 :    
139 :     variable locals-dp \ so here's the special dp for locals.
140 :    
141 :     : alignlp-w ( n1 -- n2 )
142 :     \ cell-align size and generate the corresponding code for aligning lp
143 : anton 1.3 aligned dup adjust-locals-size ;
144 : anton 1.1
145 :     : alignlp-f ( n1 -- n2 )
146 : anton 1.3 faligned dup adjust-locals-size ;
147 : anton 1.1
148 :     \ a local declaration group (the braces stuff) is compiled by calling
149 :     \ the appropriate compile-pushlocal for the locals, starting with the
150 :     \ righmost local; the names are already created earlier, the
151 :     \ compile-pushlocal just inserts the offsets from the frame base.
152 :    
153 :     : compile-pushlocal-w ( a-addr -- ) ( run-time: w -- )
154 :     \ compiles a push of a local variable, and adjusts locals-size
155 :     \ stores the offset of the local variable to a-addr
156 :     locals-size @ alignlp-w cell+ dup locals-size !
157 :     swap !
158 :     postpone >l ;
159 :    
160 : pazsan 1.27 \ locals list operations
161 :    
162 :     : common-list ( list1 list2 -- list3 ) \ gforth-internal
163 :     \ list1 and list2 are lists, where the heads are at higher addresses than
164 :     \ the tail. list3 is the largest sublist of both lists.
165 :     begin
166 :     2dup u<>
167 :     while
168 :     2dup u>
169 :     if
170 :     swap
171 :     then
172 :     @
173 :     repeat
174 :     drop ;
175 :    
176 :     : sub-list? ( list1 list2 -- f ) \ gforth-internal
177 :     \ true iff list1 is a sublist of list2
178 :     begin
179 :     2dup u<
180 :     while
181 :     @
182 :     repeat
183 :     = ;
184 :    
185 :     : list-size ( list -- u ) \ gforth-internal
186 : pazsan 1.36 \ size of the locals frame represented by list
187 :     0 ( list n )
188 :     begin
189 :     over 0<>
190 :     while
191 :     over
192 :     ((name>)) >body @ max
193 :     swap @ swap ( get next )
194 :     repeat
195 :     faligned nip ;
196 : pazsan 1.27
197 :     : set-locals-size-list ( list -- )
198 : pazsan 1.37 dup locals-list !
199 : pazsan 1.36 list-size locals-size ! ;
200 : pazsan 1.27
201 :     : check-begin ( list -- )
202 :     \ warn if list is not a sublist of locals-list
203 : pazsan 1.37 locals-list @ sub-list? 0= if
204 : pazsan 1.27 \ !! print current position
205 :     ." compiler was overly optimistic about locals at a BEGIN" cr
206 :     \ !! print assumption and reality
207 :     then ;
208 :    
209 : anton 1.1 : compile-pushlocal-f ( a-addr -- ) ( run-time: f -- )
210 :     locals-size @ alignlp-f float+ dup locals-size !
211 :     swap !
212 :     postpone f>l ;
213 :    
214 :     : compile-pushlocal-d ( a-addr -- ) ( run-time: w1 w2 -- )
215 :     locals-size @ alignlp-w cell+ cell+ dup locals-size !
216 :     swap !
217 :     postpone swap postpone >l postpone >l ;
218 :    
219 :     : compile-pushlocal-c ( a-addr -- ) ( run-time: w -- )
220 : anton 1.3 -1 chars compile-lp+!
221 : anton 1.1 locals-size @ swap !
222 :     postpone lp@ postpone c! ;
223 :    
224 :     : create-local ( " name" -- a-addr )
225 : anton 1.9 \ defines the local "name"; the offset of the local shall be
226 :     \ stored in a-addr
227 : anton 1.61 dp
228 :     locals-dp dpp !
229 : anton 1.1 create
230 : anton 1.61 immediate restrict
231 :     here 0 , ( place for the offset )
232 :     swap dpp ! ;
233 : anton 1.1
234 : anton 1.3 : lp-offset ( n1 -- n2 )
235 :     \ converts the offset from the frame start to an offset from lp and
236 :     \ i.e., the address of the local is lp+locals_size-offset
237 :     locals-size @ swap - ;
238 :    
239 : anton 1.1 : lp-offset, ( n -- )
240 :     \ converts the offset from the frame start to an offset from lp and
241 :     \ adds it as inline argument to a preceding locals primitive
242 : anton 1.3 lp-offset , ;
243 : anton 1.1
244 :     vocabulary locals-types \ this contains all the type specifyers, -- and }
245 :     locals-types definitions
246 :    
247 : anton 1.14 : W: ( "name" -- a-addr xt ) \ gforth w-colon
248 :     create-local
249 : anton 1.1 \ xt produces the appropriate locals pushing code when executed
250 :     ['] compile-pushlocal-w
251 :     does> ( Compilation: -- ) ( Run-time: -- w )
252 :     \ compiles a local variable access
253 : anton 1.3 @ lp-offset compile-@local ;
254 : anton 1.1
255 : anton 1.14 : W^ ( "name" -- a-addr xt ) \ gforth w-caret
256 :     create-local
257 : anton 1.1 ['] compile-pushlocal-w
258 :     does> ( Compilation: -- ) ( Run-time: -- w )
259 :     postpone laddr# @ lp-offset, ;
260 :    
261 : anton 1.14 : F: ( "name" -- a-addr xt ) \ gforth f-colon
262 :     create-local
263 : anton 1.1 ['] compile-pushlocal-f
264 :     does> ( Compilation: -- ) ( Run-time: -- w )
265 : anton 1.3 @ lp-offset compile-f@local ;
266 : anton 1.1
267 : anton 1.14 : F^ ( "name" -- a-addr xt ) \ gforth f-caret
268 :     create-local
269 : anton 1.1 ['] compile-pushlocal-f
270 :     does> ( Compilation: -- ) ( Run-time: -- w )
271 :     postpone laddr# @ lp-offset, ;
272 :    
273 : anton 1.14 : D: ( "name" -- a-addr xt ) \ gforth d-colon
274 :     create-local
275 : anton 1.1 ['] compile-pushlocal-d
276 :     does> ( Compilation: -- ) ( Run-time: -- w )
277 :     postpone laddr# @ lp-offset, postpone 2@ ;
278 :    
279 : anton 1.14 : D^ ( "name" -- a-addr xt ) \ gforth d-caret
280 :     create-local
281 : anton 1.1 ['] compile-pushlocal-d
282 :     does> ( Compilation: -- ) ( Run-time: -- w )
283 :     postpone laddr# @ lp-offset, ;
284 :    
285 : anton 1.14 : C: ( "name" -- a-addr xt ) \ gforth c-colon
286 :     create-local
287 : anton 1.1 ['] compile-pushlocal-c
288 :     does> ( Compilation: -- ) ( Run-time: -- w )
289 :     postpone laddr# @ lp-offset, postpone c@ ;
290 :    
291 : anton 1.14 : C^ ( "name" -- a-addr xt ) \ gforth c-caret
292 :     create-local
293 : anton 1.1 ['] compile-pushlocal-c
294 :     does> ( Compilation: -- ) ( Run-time: -- w )
295 :     postpone laddr# @ lp-offset, ;
296 :    
297 :     \ you may want to make comments in a locals definitions group:
298 : anton 1.44 ' \ alias \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ core-ext,block-ext backslash
299 : anton 1.42 \G Comment till the end of the line if @code{BLK} contains 0 (i.e.,
300 :     \G while not loading a block), parse and discard the remainder of the
301 :     \G parse area. Otherwise, parse and discard all subsequent characters
302 :     \G in the parse area corresponding to the current line.
303 :     immediate
304 : crook 1.39
305 :     ' ( alias ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file paren
306 : anton 1.42 \G Comment, usually till the next @code{)}: parse and discard all
307 :     \G subsequent characters in the parse area until ")" is
308 :     \G encountered. During interactive input, an end-of-line also acts as
309 :     \G a comment terminator. For file input, it does not; if the
310 :     \G end-of-file is encountered whilst parsing for the ")" delimiter,
311 :     \G Gforth will generate a warning.
312 : crook 1.39 immediate
313 : anton 1.1
314 :     forth definitions
315 : anton 1.54 also locals-types
316 :    
317 :     \ these "locals" are used for comparison in TO
318 : anton 1.1
319 : anton 1.61 here locals-dp !
320 : anton 1.54 c: some-clocal 2drop
321 :     d: some-dlocal 2drop
322 :     f: some-flocal 2drop
323 :     w: some-wlocal 2drop
324 : anton 1.61 locals-dp @ dp !
325 : anton 1.54
326 : anton 1.1 \ the following gymnastics are for declaring locals without type specifier.
327 :     \ we exploit a feature of our dictionary: every wordlist
328 :     \ has it's own methods for finding words etc.
329 :     \ So we create a vocabulary new-locals, that creates a 'w:' local named x
330 :     \ when it is asked if it contains x.
331 :    
332 :     : new-locals-find ( caddr u w -- nfa )
333 :     \ this is the find method of the new-locals vocabulary
334 :     \ make a new local with name caddr u; w is ignored
335 :     \ the returned nfa denotes a word that produces what W: produces
336 :     \ !! do the whole thing without nextname
337 : anton 1.3 drop nextname
338 : anton 1.43 ['] W: >head-noprim ;
339 : anton 1.1
340 :     previous
341 :    
342 :     : new-locals-reveal ( -- )
343 :     true abort" this should not happen: new-locals-reveal" ;
344 :    
345 : anton 1.22 create new-locals-map ( -- wordlist-map )
346 : anton 1.29 ' new-locals-find A,
347 :     ' new-locals-reveal A,
348 :     ' drop A, \ rehash method
349 : jwilke 1.34 ' drop A,
350 : anton 1.1
351 : jwilke 1.41 new-locals-map mappedwordlist Constant new-locals-wl
352 :    
353 :     \ slowvoc @
354 :     \ slowvoc on
355 :     \ vocabulary new-locals
356 :     \ slowvoc !
357 :     \ new-locals-map ' new-locals >body wordlist-map A! \ !! use special access words
358 : anton 1.1
359 :     \ and now, finally, the user interface words
360 : anton 1.53 : { ( -- latestxt wid 0 ) \ gforth open-brace
361 :     latestxt get-current
362 : jwilke 1.41 get-order new-locals-wl swap 1+ set-order
363 : anton 1.32 also locals definitions locals-types
364 : anton 1.1 0 TO locals-wordlist
365 :     0 postpone [ ; immediate
366 :    
367 :     locals-types definitions
368 :    
369 : anton 1.53 : } ( latestxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace
370 : anton 1.1 \ ends locals definitions
371 : anton 1.61 ]
372 : anton 1.1 begin
373 :     dup
374 :     while
375 :     execute
376 :     repeat
377 :     drop
378 :     locals-size @ alignlp-f locals-size ! \ the strictest alignment
379 :     previous previous
380 : anton 1.32 set-current lastcfa !
381 : pazsan 1.37 locals-list 0 wordlist-id - TO locals-wordlist ;
382 : anton 1.1
383 : anton 1.14 : -- ( addr wid 0 ... -- ) \ gforth dash-dash
384 : anton 1.1 }
385 : anton 1.9 [char] } parse 2drop ;
386 : anton 1.1
387 :     forth definitions
388 :    
389 :     \ A few thoughts on automatic scopes for locals and how they can be
390 :     \ implemented:
391 :    
392 :     \ We have to combine locals with the control structures. My basic idea
393 :     \ was to start the life of a local at the declaration point. The life
394 :     \ would end at any control flow join (THEN, BEGIN etc.) where the local
395 :     \ is lot live on both input flows (note that the local can still live in
396 :     \ other, later parts of the control flow). This would make a local live
397 :     \ as long as you expected and sometimes longer (e.g. a local declared in
398 :     \ a BEGIN..UNTIL loop would still live after the UNTIL).
399 :    
400 :     \ The following example illustrates the problems of this approach:
401 :    
402 :     \ { z }
403 :     \ if
404 :     \ { x }
405 :     \ begin
406 :     \ { y }
407 :     \ [ 1 cs-roll ] then
408 :     \ ...
409 :     \ until
410 :    
411 :     \ x lives only until the BEGIN, but the compiler does not know this
412 :     \ until it compiles the UNTIL (it can deduce it at the THEN, because at
413 :     \ that point x lives in no thread, but that does not help much). This is
414 :     \ solved by optimistically assuming at the BEGIN that x lives, but
415 :     \ warning at the UNTIL that it does not. The user is then responsible
416 :     \ for checking that x is only used where it lives.
417 :    
418 :     \ The produced code might look like this (leaving out alignment code):
419 :    
420 :     \ >l ( z )
421 :     \ ?branch <then>
422 :     \ >l ( x )
423 :     \ <begin>:
424 :     \ >l ( y )
425 :     \ lp+!# 8 ( RIP: x,y )
426 :     \ <then>:
427 :     \ ...
428 :     \ lp+!# -4 ( adjust lp to <begin> state )
429 :     \ ?branch <begin>
430 :     \ lp+!# 4 ( undo adjust )
431 :    
432 :     \ The BEGIN problem also has another incarnation:
433 :    
434 :     \ AHEAD
435 :     \ BEGIN
436 :     \ x
437 :     \ [ 1 CS-ROLL ] THEN
438 :     \ { x }
439 :     \ ...
440 :     \ UNTIL
441 :    
442 :     \ should be legal: The BEGIN is not a control flow join in this case,
443 :     \ since it cannot be entered from the top; therefore the definition of x
444 :     \ dominates the use. But the compiler processes the use first, and since
445 :     \ it does not look ahead to notice the definition, it will complain
446 :     \ about it. Here's another variation of this problem:
447 :    
448 :     \ IF
449 :     \ { x }
450 :     \ ELSE
451 :     \ ...
452 :     \ AHEAD
453 :     \ BEGIN
454 :     \ x
455 :     \ [ 2 CS-ROLL ] THEN
456 :     \ ...
457 :     \ UNTIL
458 :    
459 :     \ In this case x is defined before the use, and the definition dominates
460 :     \ the use, but the compiler does not know this until it processes the
461 :     \ UNTIL. So what should the compiler assume does live at the BEGIN, if
462 :     \ the BEGIN is not a control flow join? The safest assumption would be
463 :     \ the intersection of all locals lists on the control flow
464 :     \ stack. However, our compiler assumes that the same variables are live
465 :     \ as on the top of the control flow stack. This covers the following case:
466 :    
467 :     \ { x }
468 :     \ AHEAD
469 :     \ BEGIN
470 :     \ x
471 :     \ [ 1 CS-ROLL ] THEN
472 :     \ ...
473 :     \ UNTIL
474 :    
475 :     \ If this assumption is too optimistic, the compiler will warn the user.
476 :    
477 : anton 1.28 \ Implementation:
478 : anton 1.1
479 : anton 1.3 \ explicit scoping
480 : anton 1.1
481 : anton 1.14 : scope ( compilation -- scope ; run-time -- ) \ gforth
482 : pazsan 1.36 cs-push-part scopestart ; immediate
483 :    
484 :     : adjust-locals-list ( wid -- )
485 : pazsan 1.37 locals-list @ common-list
486 : pazsan 1.36 dup list-size adjust-locals-size
487 : pazsan 1.37 locals-list ! ;
488 : anton 1.3
489 : anton 1.14 : endscope ( compilation scope -- ; run-time -- ) \ gforth
490 : pazsan 1.36 scope?
491 :     drop adjust-locals-list ; immediate
492 : anton 1.1
493 : anton 1.3 \ adapt the hooks
494 : anton 1.1
495 : anton 1.3 : locals-:-hook ( sys -- sys addr xt n )
496 :     \ addr is the nfa of the defined word, xt its xt
497 : anton 1.1 DEFERS :-hook
498 : anton 1.53 latest latestxt
499 : anton 1.1 clear-leave-stack
500 :     0 locals-size !
501 :     locals-buffer locals-dp !
502 : pazsan 1.37 0 locals-list !
503 : anton 1.3 dead-code off
504 :     defstart ;
505 : anton 1.1
506 : anton 1.3 : locals-;-hook ( sys addr xt sys -- sys )
507 :     def?
508 : anton 1.1 0 TO locals-wordlist
509 : anton 1.3 0 adjust-locals-size ( not every def ends with an exit )
510 : anton 1.1 lastcfa ! last !
511 :     DEFERS ;-hook ;
512 :    
513 : anton 1.28 \ THEN (another control flow from before joins the current one):
514 :     \ The new locals-list is the intersection of the current locals-list and
515 :     \ the orig-local-list. The new locals-size is the (alignment-adjusted)
516 :     \ size of the new locals-list. The following code is generated:
517 :     \ lp+!# (current-locals-size - orig-locals-size)
518 :     \ <then>:
519 :     \ lp+!# (orig-locals-size - new-locals-size)
520 :    
521 :     \ Of course "lp+!# 0" is not generated. Still this is admittedly a bit
522 :     \ inefficient, e.g. if there is a locals declaration between IF and
523 :     \ ELSE. However, if ELSE generates an appropriate "lp+!#" before the
524 :     \ branch, there will be none after the target <then>.
525 :    
526 : anton 1.30 : (then-like) ( orig -- )
527 :     dead-orig =
528 : pazsan 1.27 if
529 : anton 1.30 >resolve drop
530 : pazsan 1.27 else
531 :     dead-code @
532 :     if
533 : anton 1.30 >resolve set-locals-size-list dead-code off
534 : pazsan 1.27 else \ both live
535 : anton 1.30 over list-size adjust-locals-size
536 :     >resolve
537 : pazsan 1.36 adjust-locals-list
538 : pazsan 1.27 then
539 :     then ;
540 :    
541 :     : (begin-like) ( -- )
542 :     dead-code @ if
543 :     \ set up an assumption of the locals visible here. if the
544 :     \ users want something to be visible, they have to declare
545 :     \ that using ASSUME-LIVE
546 :     backedge-locals @ set-locals-size-list
547 :     then
548 :     dead-code off ;
549 :    
550 :     \ AGAIN (the current control flow joins another, earlier one):
551 :     \ If the dest-locals-list is not a subset of the current locals-list,
552 :     \ issue a warning (see below). The following code is generated:
553 :     \ lp+!# (current-local-size - dest-locals-size)
554 :     \ branch <begin>
555 :    
556 :     : (again-like) ( dest -- addr )
557 :     over list-size adjust-locals-size
558 :     swap check-begin POSTPONE unreachable ;
559 :    
560 :     \ UNTIL (the current control flow may join an earlier one or continue):
561 :     \ Similar to AGAIN. The new locals-list and locals-size are the current
562 :     \ ones. The following code is generated:
563 :     \ ?branch-lp+!# <begin> (current-local-size - dest-locals-size)
564 :    
565 :     : (until-like) ( list addr xt1 xt2 -- )
566 :     \ list and addr are a fragment of a cs-item
567 :     \ xt1 is the conditional branch without lp adjustment, xt2 is with
568 :     >r >r
569 :     locals-size @ 2 pick list-size - dup if ( list dest-addr adjustment )
570 :     r> drop r> compile,
571 :     swap <resolve ( list adjustment ) ,
572 :     else ( list dest-addr adjustment )
573 :     drop
574 :     r> compile, <resolve
575 :     r> drop
576 :     then ( list )
577 :     check-begin ;
578 :    
579 :     : (exit-like) ( -- )
580 :     0 adjust-locals-size ;
581 :    
582 : anton 1.1 ' locals-:-hook IS :-hook
583 :     ' locals-;-hook IS ;-hook
584 : pazsan 1.27
585 :     ' (then-like) IS then-like
586 :     ' (begin-like) IS begin-like
587 :     ' (again-like) IS again-like
588 :     ' (until-like) IS until-like
589 :     ' (exit-like) IS exit-like
590 : anton 1.1
591 :     \ The words in the locals dictionary space are not deleted until the end
592 :     \ of the current word. This is a bit too conservative, but very simple.
593 :    
594 :     \ There are a few cases to consider: (see above)
595 :    
596 :     \ after AGAIN, AHEAD, EXIT (the current control flow is dead):
597 :     \ We have to special-case the above cases against that. In this case the
598 :     \ things above are not control flow joins. Everything should be taken
599 :     \ over from the live flow. No lp+!# is generated.
600 :    
601 :     \ About warning against uses of dead locals. There are several options:
602 :    
603 :     \ 1) Do not complain (After all, this is Forth;-)
604 :    
605 :     \ 2) Additional restrictions can be imposed so that the situation cannot
606 :     \ arise; the programmer would have to introduce explicit scoping
607 :     \ declarations in cases like the above one. I.e., complain if there are
608 :     \ locals that are live before the BEGIN but not before the corresponding
609 :     \ AGAIN (replace DO etc. for BEGIN and UNTIL etc. for AGAIN).
610 :    
611 :     \ 3) The real thing: i.e. complain, iff a local lives at a BEGIN, is
612 :     \ used on a path starting at the BEGIN, and does not live at the
613 :     \ corresponding AGAIN. This is somewhat hard to implement. a) How does
614 :     \ the compiler know when it is working on a path starting at a BEGIN
615 :     \ (consider "{ x } if begin [ 1 cs-roll ] else x endif again")? b) How
616 :     \ is the usage info stored?
617 :    
618 :     \ For now I'll resort to alternative 2. When it produces warnings they
619 :     \ will often be spurious, but warnings should be rare. And better
620 :     \ spurious warnings now and then than days of bug-searching.
621 :    
622 :     \ Explicit scoping of locals is implemented by cs-pushing the current
623 :     \ locals-list and -size (and an unused cell, to make the size equal to
624 :     \ the other entries) at the start of the scope, and restoring them at
625 :     \ the end of the scope to the intersection, like THEN does.
626 :    
627 :    
628 :     \ And here's finally the ANS standard stuff
629 :    
630 : anton 1.14 : (local) ( addr u -- ) \ local paren-local-paren
631 : anton 1.3 \ a little space-inefficient, but well deserved ;-)
632 :     \ In exchange, there are no restrictions whatsoever on using (local)
633 : anton 1.4 \ as long as you use it in a definition
634 : anton 1.3 dup
635 :     if
636 :     nextname POSTPONE { [ also locals-types ] W: } [ previous ]
637 :     else
638 :     2drop
639 :     endif ;
640 : anton 1.1
641 : anton 1.56 : >definer ( xt -- definer ) \ gforth
642 : anton 1.48 \G @var{Definer} is a unique identifier for the way the @var{xt}
643 :     \G was defined. Words defined with different @code{does>}-codes
644 :     \G have different definers. The definer can be used for
645 :     \G comparison and in @code{definer!}.
646 : anton 1.30 dup >does-code
647 :     ?dup-if
648 :     nip 1 or
649 : anton 1.4 else
650 :     >code-address
651 :     then ;
652 :    
653 : anton 1.56 : definer! ( definer xt -- ) \ gforth
654 : anton 1.48 \G The word represented by @var{xt} changes its behaviour to the
655 :     \G behaviour associated with @var{definer}.
656 : anton 1.4 over 1 and if
657 : anton 1.13 swap [ 1 invert ] literal and does-code!
658 : anton 1.4 else
659 :     code-address!
660 :     then ;
661 :    
662 : pazsan 1.23 :noname
663 : anton 1.31 ' dup >definer [ ' locals-wordlist ] literal >definer =
664 : pazsan 1.23 if
665 :     >body !
666 :     else
667 :     -&32 throw
668 :     endif ;
669 :     :noname
670 : anton 1.28 comp' drop dup >definer
671 : anton 1.21 case
672 : anton 1.30 [ ' locals-wordlist ] literal >definer \ value
673 : anton 1.21 OF >body POSTPONE Aliteral POSTPONE ! ENDOF
674 : anton 1.35 \ !! dependent on c: etc. being does>-defining words
675 :     \ this works, because >definer uses >does-code in this case,
676 :     \ which produces a relocatable address
677 : anton 1.54 [ comp' some-clocal drop ] literal >definer
678 : anton 1.21 OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF
679 : anton 1.54 [ comp' some-wlocal drop ] literal >definer
680 : anton 1.21 OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF
681 : anton 1.54 [ comp' some-dlocal drop ] literal >definer
682 : anton 1.21 OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF
683 : anton 1.54 [ comp' some-flocal drop ] literal >definer
684 : anton 1.21 OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF
685 :     -&32 throw
686 : pazsan 1.23 endcase ;
687 : anton 1.24 interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local
688 : anton 1.1
689 : anton 1.58 : locals| ( ... "name ..." -- ) \ local-ext locals-bar
690 : anton 1.14 \ don't use 'locals|'! use '{'! A portable and free '{'
691 : anton 1.21 \ implementation is compat/anslocals.fs
692 : anton 1.8 BEGIN
693 : anton 1.49 name 2dup s" |" str= 0=
694 : anton 1.8 WHILE
695 :     (local)
696 :     REPEAT
697 : anton 1.14 drop 0 (local) ; immediate restrict

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help