File:  [gforth] / gforth / glocals.fs
Revision 1.66: download - view: text, annotated - select for diffs
Tue Feb 7 18:15:27 2012 UTC (8 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
bugfix: list operations no longer require specific arrangement in memory
        (that assumption became invalid by ALLOCATEing local names)

    1: \ A powerful locals implementation
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2007,2011 Free Software Foundation, Inc.
    4: 
    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: \ as published by the Free Software Foundation, either version 3
   10: \ 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: \ along with this program. If not, see http://www.gnu.org/licenses/.
   19: 
   20: 
   21: \ More documentation can be found in the manual and in
   22: \ http://www.complang.tuwien.ac.at/papers/ertl94l.ps.gz
   23: 
   24: \ 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: require search.fs
   88: require float.fs
   89: require extend.fs \ for case
   90: 
   91: : compile-@local ( n -- ) \ gforth compile-fetch-local
   92:  case
   93:     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:    ( otherwise ) dup postpone @local# ,
   98:  endcase ;
   99: 
  100: : compile-f@local ( n -- ) \ gforth compile-f-fetch-local
  101:  case
  102:     0        of postpone f@local0 endof
  103:     1 floats of postpone f@local1 endof
  104:    ( otherwise ) dup postpone f@local# ,
  105:  endcase ;
  106: 
  107: \ 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: \ 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: slowvoc @
  130: slowvoc on \ we want a linked list for the vocabulary locals
  131: vocabulary locals \ this contains the local variables
  132: ' locals >body wordlist-id ' locals-list >body !
  133: slowvoc !
  134: 
  135: variable locals-mem-list \ linked list of all locals name memory in
  136: 0 locals-mem-list !      \ the current (outer-level) definition
  137: 
  138: : free-list ( addr -- )
  139:     \ free all members of a linked list (link field is first)
  140:     begin
  141: 	dup while
  142: 	    dup @ swap free throw
  143:     repeat
  144:     drop ;
  145: 
  146: : prepend-list ( addr1 addr2 -- )
  147:     \ addr1 is the address of a list element, addr2 is the address of
  148:     \ the cell containing the address of the first list element
  149:     2dup @ swap ! \ store link to next element
  150:     ! ; \ store pointer to new first element
  151: 
  152: : alignlp-w ( n1 -- n2 )
  153:     \ cell-align size and generate the corresponding code for aligning lp
  154:     aligned dup adjust-locals-size ;
  155: 
  156: : alignlp-f ( n1 -- n2 )
  157:     faligned dup adjust-locals-size ;
  158: 
  159: \ a local declaration group (the braces stuff) is compiled by calling
  160: \ the appropriate compile-pushlocal for the locals, starting with the
  161: \ righmost local; the names are already created earlier, the
  162: \ compile-pushlocal just inserts the offsets from the frame base.
  163: 
  164: : compile-pushlocal-w ( a-addr -- ) ( run-time: w -- )
  165: \ compiles a push of a local variable, and adjusts locals-size
  166: \ stores the offset of the local variable to a-addr
  167:     locals-size @ alignlp-w cell+ dup locals-size !
  168:     swap !
  169:     postpone >l ;
  170: 
  171: \ locals list operations
  172: 
  173: : list-length ( list -- u )
  174:     0 swap begin ( u1 list1 )
  175: 	dup while
  176: 	    @ swap 1+ swap
  177:     repeat
  178:     drop ;
  179: 
  180: : /list ( list1 u -- list2 )
  181:     \ list2 is list1 with the first u elements removed
  182:     0 ?do
  183: 	@
  184:     loop ;
  185: 
  186: : common-list ( list1 list2 -- list3 )
  187:     \ list3 is the largest common tail of both lists.
  188:     over list-length over list-length - dup 0< if
  189: 	negate >r swap r>
  190:     then ( long short u )
  191:     rot swap /list begin ( list3 list4 )
  192: 	2dup u<> while
  193: 	    @ swap @
  194:     repeat
  195:     drop ;
  196: 
  197: : sub-list? ( list1 list2 -- f )
  198:     \ true iff list1 is a sublist of list2
  199:     over list-length over list-length swap - 0 max /list = ;
  200: 
  201: \ : ocommon-list ( list1 list2 -- list3 ) \ gforth-internal
  202: \ \ list1 and list2 are lists, where the heads are at higher addresses than
  203: \ \ the tail. list3 is the largest sublist of both lists.
  204: \  begin
  205: \    2dup u<>
  206: \  while
  207: \    2dup u>
  208: \    if
  209: \      swap
  210: \    then
  211: \    @
  212: \  repeat
  213: \  drop ;
  214: 
  215: \ : osub-list? ( list1 list2 -- f ) \ gforth-internal
  216: \ \ true iff list1 is a sublist of list2
  217: \  begin
  218: \    2dup u<
  219: \  while
  220: \    @
  221: \  repeat
  222: \  = ;
  223: 
  224: \ defer common-list
  225: \ defer sub-list?
  226: 
  227: \ ' ocommon-list is common-list
  228: \ ' osub-list?   is sub-list?
  229: 
  230: : list-size ( list -- u ) \ gforth-internal
  231:     \ size of the locals frame represented by list
  232:     0 ( list n )
  233:     begin
  234: 	over 0<>
  235:     while
  236: 	over
  237: 	((name>)) >body @ max
  238: 	swap @ swap ( get next )
  239:     repeat
  240:     faligned nip ;
  241: 
  242: : set-locals-size-list ( list -- )
  243:     dup locals-list !
  244:     list-size locals-size ! ;
  245: 
  246: : check-begin ( list -- )
  247: \ warn if list is not a sublist of locals-list
  248:  locals-list @ sub-list? 0= if
  249:    \ !! print current position
  250:      >stderr ." compiler was overly optimistic about locals at a BEGIN" cr
  251:    \ !! print assumption and reality
  252:  then ;
  253: 
  254: : compile-pushlocal-f ( a-addr -- ) ( run-time: f -- )
  255:     locals-size @ alignlp-f float+ dup locals-size !
  256:     swap !
  257:     postpone f>l ;
  258: 
  259: : compile-pushlocal-d ( a-addr -- ) ( run-time: w1 w2 -- )
  260:     locals-size @ alignlp-w cell+ cell+ dup locals-size !
  261:     swap !
  262:     postpone swap postpone >l postpone >l ;
  263: 
  264: : compile-pushlocal-c ( a-addr -- ) ( run-time: w -- )
  265:     -1 chars compile-lp+!
  266:     locals-size @ swap !
  267:     postpone lp@ postpone c! ;
  268: 
  269: 7 cells 32 + constant locals-name-size \ 32-char name + fields + wiggle room
  270: 
  271: : create-local1 ( "name" -- a-addr )
  272:     create
  273:     immediate restrict
  274:     here 0 , ( place for the offset ) ;
  275: 
  276: variable dict-execute-dp \ the special dp for DICT-EXECUTE
  277: 
  278: 0 value dict-execute-ude \ USABLE-DICTIONARY-END during DICT-EXECUTE
  279: 
  280: : dict-execute1 ( ... addr1 addr2 xt -- ... )
  281:     \ execute xt with HERE set to addr1 and USABLE-DICTIONARY-END set to addr2
  282:     dict-execute-dp @ dp 2>r
  283:     dict-execute-ude ['] usable-dictionary-end defer@ 2>r
  284:     swap to dict-execute-ude
  285:     ['] dict-execute-ude is usable-dictionary-end
  286:     swap to dict-execute-dp
  287:     dict-execute-dp dpp !
  288:     catch
  289:     2r> is usable-dictionary-end to dict-execute-ude
  290:     2r> dpp ! dict-execute-dp !
  291:     throw ;
  292: 
  293: defer dict-execute ( ... addr1 addr2 xt -- ... )
  294: 
  295: :noname ( ... addr1 addr2 xt -- ... )
  296:     \ first have a dummy routine, for SOME-CLOCAL etc. below
  297:     nip nip execute ;
  298: is dict-execute
  299: 
  300: : create-local ( " name" -- a-addr )
  301:     \ defines the local "name"; the offset of the local shall be
  302:     \ stored in a-addr
  303:     locals-name-size allocate throw
  304:     dup locals-mem-list prepend-list
  305:     locals-name-size cell /string over + ['] create-local1 dict-execute ;
  306: 
  307: variable locals-dp \ so here's the special dp for locals.
  308: 
  309: : lp-offset ( n1 -- n2 )
  310: \ converts the offset from the frame start to an offset from lp and
  311: \ i.e., the address of the local is lp+locals_size-offset
  312:   locals-size @ swap - ;
  313: 
  314: : lp-offset, ( n -- )
  315: \ converts the offset from the frame start to an offset from lp and
  316: \ adds it as inline argument to a preceding locals primitive
  317:   lp-offset , ;
  318: 
  319: vocabulary locals-types \ this contains all the type specifyers, -- and }
  320: locals-types definitions
  321: 
  322: : W: ( "name" -- a-addr xt ) \ gforth w-colon
  323:     create-local
  324: 	\ xt produces the appropriate locals pushing code when executed
  325: 	['] compile-pushlocal-w
  326:     does> ( Compilation: -- ) ( Run-time: -- w )
  327:         \ compiles a local variable access
  328: 	@ lp-offset compile-@local ;
  329: 
  330: : W^ ( "name" -- a-addr xt ) \ gforth w-caret
  331:     create-local
  332: 	['] compile-pushlocal-w
  333:     does> ( Compilation: -- ) ( Run-time: -- w )
  334: 	postpone laddr# @ lp-offset, ;
  335: 
  336: : F: ( "name" -- a-addr xt ) \ gforth f-colon
  337:     create-local
  338: 	['] compile-pushlocal-f
  339:     does> ( Compilation: -- ) ( Run-time: -- w )
  340: 	@ lp-offset compile-f@local ;
  341: 
  342: : F^ ( "name" -- a-addr xt ) \ gforth f-caret
  343:     create-local
  344: 	['] compile-pushlocal-f
  345:     does> ( Compilation: -- ) ( Run-time: -- w )
  346: 	postpone laddr# @ lp-offset, ;
  347: 
  348: : D: ( "name" -- a-addr xt ) \ gforth d-colon
  349:     create-local
  350: 	['] compile-pushlocal-d
  351:     does> ( Compilation: -- ) ( Run-time: -- w )
  352: 	postpone laddr# @ lp-offset, postpone 2@ ;
  353: 
  354: : D^ ( "name" -- a-addr xt ) \ gforth d-caret
  355:     create-local
  356: 	['] compile-pushlocal-d
  357:     does> ( Compilation: -- ) ( Run-time: -- w )
  358: 	postpone laddr# @ lp-offset, ;
  359: 
  360: : C: ( "name" -- a-addr xt ) \ gforth c-colon
  361:     create-local
  362: 	['] compile-pushlocal-c
  363:     does> ( Compilation: -- ) ( Run-time: -- w )
  364: 	postpone laddr# @ lp-offset, postpone c@ ;
  365: 
  366: : C^ ( "name" -- a-addr xt ) \ gforth c-caret
  367:     create-local
  368: 	['] compile-pushlocal-c
  369:     does> ( Compilation: -- ) ( Run-time: -- w )
  370: 	postpone laddr# @ lp-offset, ;
  371: 
  372: \ you may want to make comments in a locals definitions group:
  373: ' \ alias \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ core-ext,block-ext backslash
  374: \G Comment till the end of the line if @code{BLK} contains 0 (i.e.,
  375: \G while not loading a block), parse and discard the remainder of the
  376: \G parse area. Otherwise, parse and discard all subsequent characters
  377: \G in the parse area corresponding to the current line.
  378: immediate
  379: 
  380: ' ( alias ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file	paren
  381: \G Comment, usually till the next @code{)}: parse and discard all
  382: \G subsequent characters in the parse area until ")" is
  383: \G encountered. During interactive input, an end-of-line also acts as
  384: \G a comment terminator. For file input, it does not; if the
  385: \G end-of-file is encountered whilst parsing for the ")" delimiter,
  386: \G Gforth will generate a warning.
  387: immediate
  388: 
  389: forth definitions
  390: also locals-types
  391:     
  392: \ these "locals" are used for comparison in TO
  393: c: some-clocal 2drop
  394: d: some-dlocal 2drop
  395: f: some-flocal 2drop
  396: w: some-wlocal 2drop
  397: 
  398: ' dict-execute1 is dict-execute \ now the real thing
  399:     
  400: \ the following gymnastics are for declaring locals without type specifier.
  401: \ we exploit a feature of our dictionary: every wordlist
  402: \ has it's own methods for finding words etc.
  403: \ So we create a vocabulary new-locals, that creates a 'w:' local named x
  404: \ when it is asked if it contains x.
  405: 
  406: : new-locals-find ( caddr u w -- nfa )
  407: \ this is the find method of the new-locals vocabulary
  408: \ make a new local with name caddr u; w is ignored
  409: \ the returned nfa denotes a word that produces what W: produces
  410: \ !! do the whole thing without nextname
  411:     drop nextname
  412:     ['] W: >head-noprim ;
  413: 
  414: previous
  415: 
  416: : new-locals-reveal ( -- )
  417:   true abort" this should not happen: new-locals-reveal" ;
  418: 
  419: create new-locals-map ( -- wordlist-map )
  420: ' new-locals-find A,
  421: ' new-locals-reveal A,
  422: ' drop A, \ rehash method
  423: ' drop A,
  424: 
  425: new-locals-map mappedwordlist Constant new-locals-wl
  426: 
  427: \ slowvoc @
  428: \ slowvoc on
  429: \ vocabulary new-locals
  430: \ slowvoc !
  431: \ new-locals-map ' new-locals >body wordlist-map A! \ !! use special access words
  432: 
  433: \ and now, finally, the user interface words
  434: : { ( -- latestxt wid 0 ) \ gforth open-brace
  435:     latestxt get-current
  436:     get-order new-locals-wl swap 1+ set-order
  437:     also locals definitions locals-types
  438:     0 TO locals-wordlist
  439:     0 postpone [ ; immediate
  440: 
  441: locals-types definitions
  442: 
  443: : } ( latestxt wid 0 a-addr1 xt1 ... -- ) \ gforth close-brace
  444:     \ ends locals definitions
  445:     ]
  446:     begin
  447: 	dup
  448:     while
  449: 	execute
  450:     repeat
  451:     drop
  452:     locals-size @ alignlp-f locals-size ! \ the strictest alignment
  453:     previous previous
  454:     set-current lastcfa !
  455:     locals-list 0 wordlist-id - TO locals-wordlist ;
  456: 
  457: : -- ( addr wid 0 ... -- ) \ gforth dash-dash
  458:     }
  459:     [char] } parse 2drop ;
  460: 
  461: forth definitions
  462: 
  463: \ A few thoughts on automatic scopes for locals and how they can be
  464: \ implemented:
  465: 
  466: \ We have to combine locals with the control structures. My basic idea
  467: \ was to start the life of a local at the declaration point. The life
  468: \ would end at any control flow join (THEN, BEGIN etc.) where the local
  469: \ is lot live on both input flows (note that the local can still live in
  470: \ other, later parts of the control flow). This would make a local live
  471: \ as long as you expected and sometimes longer (e.g. a local declared in
  472: \ a BEGIN..UNTIL loop would still live after the UNTIL).
  473: 
  474: \ The following example illustrates the problems of this approach:
  475: 
  476: \ { z }
  477: \ if
  478: \   { x }
  479: \ begin
  480: \   { y }
  481: \ [ 1 cs-roll ] then
  482: \   ...
  483: \ until
  484: 
  485: \ x lives only until the BEGIN, but the compiler does not know this
  486: \ until it compiles the UNTIL (it can deduce it at the THEN, because at
  487: \ that point x lives in no thread, but that does not help much). This is
  488: \ solved by optimistically assuming at the BEGIN that x lives, but
  489: \ warning at the UNTIL that it does not. The user is then responsible
  490: \ for checking that x is only used where it lives.
  491: 
  492: \ The produced code might look like this (leaving out alignment code):
  493: 
  494: \ >l ( z )
  495: \ ?branch <then>
  496: \ >l ( x )
  497: \ <begin>:
  498: \ >l ( y )
  499: \ lp+!# 8 ( RIP: x,y )
  500: \ <then>:
  501: \ ...
  502: \ lp+!# -4 ( adjust lp to <begin> state )
  503: \ ?branch <begin>
  504: \ lp+!# 4 ( undo adjust )
  505: 
  506: \ The BEGIN problem also has another incarnation:
  507: 
  508: \ AHEAD
  509: \ BEGIN
  510: \   x
  511: \ [ 1 CS-ROLL ] THEN
  512: \   { x }
  513: \   ...
  514: \ UNTIL
  515: 
  516: \ should be legal: The BEGIN is not a control flow join in this case,
  517: \ since it cannot be entered from the top; therefore the definition of x
  518: \ dominates the use. But the compiler processes the use first, and since
  519: \ it does not look ahead to notice the definition, it will complain
  520: \ about it. Here's another variation of this problem:
  521: 
  522: \ IF
  523: \   { x }
  524: \ ELSE
  525: \   ...
  526: \ AHEAD
  527: \ BEGIN
  528: \   x
  529: \ [ 2 CS-ROLL ] THEN
  530: \   ...
  531: \ UNTIL
  532: 
  533: \ In this case x is defined before the use, and the definition dominates
  534: \ the use, but the compiler does not know this until it processes the
  535: \ UNTIL. So what should the compiler assume does live at the BEGIN, if
  536: \ the BEGIN is not a control flow join? The safest assumption would be
  537: \ the intersection of all locals lists on the control flow
  538: \ stack. However, our compiler assumes that the same variables are live
  539: \ as on the top of the control flow stack. This covers the following case:
  540: 
  541: \ { x }
  542: \ AHEAD
  543: \ BEGIN
  544: \   x
  545: \ [ 1 CS-ROLL ] THEN
  546: \   ...
  547: \ UNTIL
  548: 
  549: \ If this assumption is too optimistic, the compiler will warn the user.
  550: 
  551: \ Implementation:
  552: 
  553: \ explicit scoping
  554: 
  555: : scope ( compilation  -- scope ; run-time  -- ) \ gforth
  556:     cs-push-part scopestart ; immediate
  557: 
  558: : adjust-locals-list ( wid -- )
  559:     locals-list @ common-list
  560:     dup list-size adjust-locals-size
  561:     locals-list ! ;
  562: 
  563: : endscope ( compilation scope -- ; run-time  -- ) \ gforth
  564:     scope?
  565:     drop  adjust-locals-list ; immediate
  566: 
  567: \ adapt the hooks
  568: 
  569: : locals-:-hook ( sys -- sys addr xt n )
  570:     \ addr is the nfa of the defined word, xt its xt
  571:     DEFERS :-hook
  572:     latest latestxt
  573:     clear-leave-stack
  574:     0 locals-size !
  575:     locals-mem-list @ free-list
  576:     0 locals-mem-list !
  577:     0 locals-list !
  578:     dead-code off
  579:     defstart ;
  580: 
  581: : locals-;-hook ( sys addr xt sys -- sys )
  582:     def?
  583:     0 TO locals-wordlist
  584:     0 adjust-locals-size ( not every def ends with an exit )
  585:     lastcfa ! last !
  586:     DEFERS ;-hook ;
  587: 
  588: \ THEN (another control flow from before joins the current one):
  589: \ The new locals-list is the intersection of the current locals-list and
  590: \ the orig-local-list. The new locals-size is the (alignment-adjusted)
  591: \ size of the new locals-list. The following code is generated:
  592: \ lp+!# (current-locals-size - orig-locals-size)
  593: \ <then>:
  594: \ lp+!# (orig-locals-size - new-locals-size)
  595: 
  596: \ Of course "lp+!# 0" is not generated. Still this is admittedly a bit
  597: \ inefficient, e.g. if there is a locals declaration between IF and
  598: \ ELSE. However, if ELSE generates an appropriate "lp+!#" before the
  599: \ branch, there will be none after the target <then>.
  600: 
  601: : (then-like) ( orig -- )
  602:     dead-orig =
  603:     if
  604: 	>resolve drop
  605:     else
  606:         dead-code @
  607:         if
  608: 	    >resolve set-locals-size-list dead-code off
  609: 	else \ both live
  610: 	    over list-size adjust-locals-size
  611: 	    >resolve
  612: 	    adjust-locals-list
  613: 	then
  614:     then ;
  615: 
  616: : (begin-like) ( -- )
  617:     dead-code @ if
  618: 	\ set up an assumption of the locals visible here.  if the
  619: 	\ users want something to be visible, they have to declare
  620: 	\ that using ASSUME-LIVE
  621: 	backedge-locals @ set-locals-size-list
  622:     then
  623:     dead-code off ;
  624: 
  625: \ AGAIN (the current control flow joins another, earlier one):
  626: \ If the dest-locals-list is not a subset of the current locals-list,
  627: \ issue a warning (see below). The following code is generated:
  628: \ lp+!# (current-local-size - dest-locals-size)
  629: \ branch <begin>
  630: 
  631: : (again-like) ( dest -- addr )
  632:     over list-size adjust-locals-size
  633:     swap check-begin  POSTPONE unreachable ;
  634: 
  635: \ UNTIL (the current control flow may join an earlier one or continue):
  636: \ Similar to AGAIN. The new locals-list and locals-size are the current
  637: \ ones. The following code is generated:
  638: \ ?branch-lp+!# <begin> (current-local-size - dest-locals-size)
  639: 
  640: : (until-like) ( list addr xt1 xt2 -- )
  641:     \ list and addr are a fragment of a cs-item
  642:     \ xt1 is the conditional branch without lp adjustment, xt2 is with
  643:     >r >r
  644:     locals-size @ 2 pick list-size - dup if ( list dest-addr adjustment )
  645: 	r> drop r> compile,
  646: 	swap <resolve ( list adjustment ) ,
  647:     else ( list dest-addr adjustment )
  648: 	drop
  649: 	r> compile, <resolve
  650: 	r> drop
  651:     then ( list )
  652:     check-begin ;
  653: 
  654: : (exit-like) ( -- )
  655:     0 adjust-locals-size ;
  656: 
  657: ' locals-:-hook IS :-hook
  658: ' locals-;-hook IS ;-hook
  659: 
  660: ' (then-like)  IS then-like
  661: ' (begin-like) IS begin-like
  662: ' (again-like) IS again-like
  663: ' (until-like) IS until-like
  664: ' (exit-like)  IS exit-like
  665: 
  666: \ The words in the locals dictionary space are not deleted until the end
  667: \ of the current word. This is a bit too conservative, but very simple.
  668: 
  669: \ There are a few cases to consider: (see above)
  670: 
  671: \ after AGAIN, AHEAD, EXIT (the current control flow is dead):
  672: \ We have to special-case the above cases against that. In this case the
  673: \ things above are not control flow joins. Everything should be taken
  674: \ over from the live flow. No lp+!# is generated.
  675: 
  676: \ About warning against uses of dead locals. There are several options:
  677: 
  678: \ 1) Do not complain (After all, this is Forth;-)
  679: 
  680: \ 2) Additional restrictions can be imposed so that the situation cannot
  681: \ arise; the programmer would have to introduce explicit scoping
  682: \ declarations in cases like the above one. I.e., complain if there are
  683: \ locals that are live before the BEGIN but not before the corresponding
  684: \ AGAIN (replace DO etc. for BEGIN and UNTIL etc. for AGAIN).
  685: 
  686: \ 3) The real thing: i.e. complain, iff a local lives at a BEGIN, is
  687: \ used on a path starting at the BEGIN, and does not live at the
  688: \ corresponding AGAIN. This is somewhat hard to implement. a) How does
  689: \ the compiler know when it is working on a path starting at a BEGIN
  690: \ (consider "{ x } if begin [ 1 cs-roll ] else x endif again")? b) How
  691: \ is the usage info stored?
  692: 
  693: \ For now I'll resort to alternative 2. When it produces warnings they
  694: \ will often be spurious, but warnings should be rare. And better
  695: \ spurious warnings now and then than days of bug-searching.
  696: 
  697: \ Explicit scoping of locals is implemented by cs-pushing the current
  698: \ locals-list and -size (and an unused cell, to make the size equal to
  699: \ the other entries) at the start of the scope, and restoring them at
  700: \ the end of the scope to the intersection, like THEN does.
  701: 
  702: 
  703: \ And here's finally the ANS standard stuff
  704: 
  705: : (local) ( addr u -- ) \ local paren-local-paren
  706:     \ a little space-inefficient, but well deserved ;-)
  707:     \ In exchange, there are no restrictions whatsoever on using (local)
  708:     \ as long as you use it in a definition
  709:     dup
  710:     if
  711: 	nextname POSTPONE { [ also locals-types ] W: } [ previous ]
  712:     else
  713: 	2drop
  714:     endif ;
  715: 
  716: : >definer ( xt -- definer ) \ gforth
  717:     \G @var{Definer} is a unique identifier for the way the @var{xt}
  718:     \G was defined.  Words defined with different @code{does>}-codes
  719:     \G have different definers.  The definer can be used for
  720:     \G comparison and in @code{definer!}.
  721:     dup >does-code
  722:     ?dup-if
  723: 	nip 1 or
  724:     else
  725: 	>code-address
  726:     then ;
  727: 
  728: : definer! ( definer xt -- ) \ gforth
  729:     \G The word represented by @var{xt} changes its behaviour to the
  730:     \G behaviour associated with @var{definer}.
  731:     over 1 and if
  732: 	swap [ 1 invert ] literal and does-code!
  733:     else
  734: 	code-address!
  735:     then ;
  736: 
  737: :noname
  738:     ' dup >definer [ ' locals-wordlist ] literal >definer =
  739:     if
  740: 	>body !
  741:     else
  742: 	-&32 throw
  743:     endif ;
  744: :noname
  745:     comp' drop dup >definer
  746:     case
  747: 	[ ' locals-wordlist ] literal >definer \ value
  748: 	OF >body POSTPONE Aliteral POSTPONE ! ENDOF
  749: 	\ !! dependent on c: etc. being does>-defining words
  750: 	\ this works, because >definer uses >does-code in this case,
  751: 	\ which produces a relocatable address
  752: 	[ comp' some-clocal drop ] literal >definer
  753: 	OF POSTPONE laddr# >body @ lp-offset, POSTPONE c! ENDOF
  754: 	[ comp' some-wlocal drop ] literal >definer
  755: 	OF POSTPONE laddr# >body @ lp-offset, POSTPONE ! ENDOF
  756: 	[ comp' some-dlocal drop ] literal >definer
  757: 	OF POSTPONE laddr# >body @ lp-offset, POSTPONE 2! ENDOF
  758: 	[ comp' some-flocal drop ] literal >definer
  759: 	OF POSTPONE laddr# >body @ lp-offset, POSTPONE f! ENDOF
  760: 	-&32 throw
  761:     endcase ;
  762: interpret/compile: TO ( c|w|d|r "name" -- ) \ core-ext,local
  763: 
  764: : locals| ( ... "name ..." -- ) \ local-ext locals-bar
  765:     \ don't use 'locals|'! use '{'! A portable and free '{'
  766:     \ implementation is compat/anslocals.fs
  767:     BEGIN
  768: 	name 2dup s" |" str= 0=
  769:     WHILE
  770: 	(local)
  771:     REPEAT
  772:     drop 0 (local) ; immediate restrict

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>