Annotation of gforth/kernel/cond.fs, revision 1.23

1.1       anton       1: \ Structural Conditionals                              12dec92py
                      2: 
1.21      anton       3: \ Copyright (C) 1995,1996,1997,2000,2003,2004,2007 Free Software Foundation, Inc.
1.1       anton       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
1.22      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       anton      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
1.22      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       anton      19: 
                     20: here 0 , \ just a dummy, the real value of locals-list is patched into it in glocals.fs
                     21: AConstant locals-list \ acts like a variable that contains
                     22:                      \ a linear list of locals names
                     23: 
                     24: 
                     25: variable dead-code \ true if normal code at "here" would be dead
                     26: variable backedge-locals
                     27:     \ contains the locals list that BEGIN will assume to be live on
                     28:     \ the back edge if the BEGIN is unreachable from above. Set by
                     29:     \ ASSUME-LIVE, reset by UNREACHABLE.
                     30: 
                     31: : UNREACHABLE ( -- ) \ gforth
                     32:     \ declares the current point of execution as unreachable
                     33:     dead-code on
                     34:     0 backedge-locals ! ; immediate
                     35: 
                     36: : ASSUME-LIVE ( orig -- orig ) \ gforth
                     37:     \ used immediatly before a BEGIN that is not reachable from
                     38:     \ above.  causes the BEGIN to assume that the same locals are live
                     39:     \ as at the orig point
                     40:     dup orig?
                     41:     2 pick backedge-locals ! ; immediate
                     42:     
                     43: \ Control Flow Stack
                     44: \ orig, etc. have the following structure:
                     45: \ type ( defstart, live-orig, dead-orig, dest, do-dest, scopestart) ( TOS )
                     46: \ address (of the branch or the instruction to be branched to) (second)
                     47: \ locals-list (valid at address) (third)
                     48: 
                     49: \ types
1.6       jwilke     50: [IFUNDEF] defstart 
                     51: 0 constant defstart    \ usally defined in comp.fs
                     52: [THEN]
1.1       anton      53: 1 constant live-orig
                     54: 2 constant dead-orig
                     55: 3 constant dest \ the loopback branch is always assumed live
                     56: 4 constant do-dest
                     57: 5 constant scopestart
                     58: 
                     59: : def? ( n -- )
                     60:     defstart <> abort" unstructured " ;
                     61: 
                     62: : orig? ( n -- )
                     63:  dup live-orig <> swap dead-orig <> and abort" expected orig " ;
                     64: 
                     65: : dest? ( n -- )
                     66:  dest <> abort" expected dest " ;
                     67: 
                     68: : do-dest? ( n -- )
                     69:  do-dest <> abort" expected do-dest " ;
                     70: 
                     71: : scope? ( n -- )
                     72:  scopestart <> abort" expected scope " ;
                     73: 
                     74: : non-orig? ( n -- )
                     75:  dest scopestart 1+ within 0= abort" expected dest, do-dest or scope" ;
                     76: 
                     77: : cs-item? ( n -- )
                     78:  live-orig scopestart 1+ within 0= abort" expected control flow stack item" ;
                     79: 
                     80: 3 constant cs-item-size
                     81: 
1.7       crook      82: : CS-PICK ( ... u -- ... destu ) \ tools-ext c-s-pick
1.1       anton      83:  1+ cs-item-size * 1- >r
                     84:  r@ pick  r@ pick  r@ pick
                     85:  rdrop
                     86:  dup non-orig? ;
                     87: 
1.7       crook      88: : CS-ROLL ( destu/origu .. dest0/orig0 u -- .. dest0/orig0 destu/origu ) \ tools-ext c-s-roll
1.1       anton      89:  1+ cs-item-size * 1- >r
                     90:  r@ roll r@ roll r@ roll
                     91:  rdrop
                     92:  dup cs-item? ; 
                     93: 
                     94: : cs-push-part ( -- list addr )
1.3       pazsan     95:  locals-list @ here ;
1.1       anton      96: 
                     97: : cs-push-orig ( -- orig )
                     98:  cs-push-part dead-code @
                     99:  if
                    100:    dead-orig
                    101:  else
                    102:    live-orig
                    103:  then ;   
                    104: 
                    105: \ Structural Conditionals                              12dec92py
                    106: 
1.18      anton     107: defer other-control-flow ( -- )
                    108: \ hook for control-flow stuff that's not handled by begin-like etc.
                    109: 
1.1       anton     110: : ?struc      ( flag -- )       abort" unstructured " ;
                    111: : sys?        ( sys -- )        dup 0= ?struc ;
                    112: : >mark ( -- orig )
1.18      anton     113:  cs-push-orig 0 , other-control-flow ;
1.11      anton     114: : >resolve    ( addr -- )
1.13      anton     115:     here swap !
1.12      anton     116:     basic-block-end ;
1.13      anton     117: : <resolve    ( addr -- )        , ;
1.1       anton     118: 
                    119: : BUT
                    120:     1 cs-roll ;                      immediate restrict
                    121: : YET
                    122:     0 cs-pick ;                       immediate restrict
                    123: 
                    124: \ Structural Conditionals                              12dec92py
                    125: 
                    126: : AHEAD ( compilation -- orig ; run-time -- ) \ tools-ext
1.14      pazsan    127:     POSTPONE branch  >mark  POSTPONE unreachable ; immediate restrict
1.1       anton     128: 
                    129: : IF ( compilation -- orig ; run-time f -- ) \ core
1.14      pazsan    130:  POSTPONE ?branch >mark ; immediate restrict
1.1       anton     131: 
                    132: : ?DUP-IF ( compilation -- orig ; run-time n -- n| ) \ gforth  question-dupe-if
1.5       crook     133: \G This is the preferred alternative to the idiom "@code{?DUP IF}", since it can be
1.1       anton     134: \G better handled by tools like stack checkers. Besides, it's faster.
1.14      pazsan    135:     POSTPONE ?dup-?branch >mark ;       immediate restrict
1.1       anton     136: 
                    137: : ?DUP-0=-IF ( compilation -- orig ; run-time n -- n| ) \ gforth       question-dupe-zero-equals-if
1.14      pazsan    138:     POSTPONE ?dup-0=-?branch >mark ;       immediate restrict
1.1       anton     139: 
                    140: Defer then-like ( orig -- )
                    141: : cs>addr ( orig/dest -- )  drop >resolve drop ;
                    142: ' cs>addr IS then-like
                    143: 
                    144: : THEN ( compilation orig -- ; run-time -- ) \ core
                    145:     dup orig?  then-like ; immediate restrict
                    146: 
                    147: ' THEN alias ENDIF ( compilation orig -- ; run-time -- ) \ gforth
                    148: immediate restrict
                    149: \ Same as "THEN". This is what you use if your program will be seen by
                    150: \ people who have not been brought up with Forth (or who have been
                    151: \ brought up with fig-Forth).
                    152: 
1.20      anton     153: : ELSE ( compilation orig1 -- orig2 ; run-time -- ) \ core
1.1       anton     154:     POSTPONE ahead
                    155:     1 cs-roll
                    156:     POSTPONE then ; immediate restrict
                    157: 
                    158: Defer begin-like ( -- )
                    159: ' noop IS begin-like
                    160: 
                    161: : BEGIN ( compilation -- dest ; run-time -- ) \ core
1.11      anton     162:     begin-like cs-push-part dest
1.12      anton     163:     basic-block-end ; immediate restrict
1.1       anton     164: 
                    165: Defer again-like ( dest -- addr )
                    166: ' nip IS again-like
                    167: 
                    168: : AGAIN ( compilation dest -- ; run-time -- ) \ core-ext
1.14      pazsan    169:     dest? again-like  POSTPONE branch  <resolve ; immediate restrict
1.1       anton     170: 
1.8       anton     171: Defer until-like ( list addr xt1 xt2 -- )
                    172: :noname ( list addr xt1 xt2 -- )
                    173:     drop compile, <resolve drop ;
                    174: IS until-like
1.1       anton     175: 
                    176: : UNTIL ( compilation dest -- ; run-time f -- ) \ core
1.14      pazsan    177:     dest? ['] ?branch ['] ?branch-lp+!# until-like ; immediate restrict
1.1       anton     178: 
                    179: : WHILE ( compilation dest -- orig dest ; run-time f -- ) \ core
                    180:     POSTPONE if
                    181:     1 cs-roll ; immediate restrict
                    182: 
                    183: : REPEAT ( compilation orig dest -- ; run-time -- ) \ core
                    184:     POSTPONE again
                    185:     POSTPONE then ; immediate restrict
                    186: 
                    187: \ counted loops
                    188: 
                    189: \ leave poses a little problem here
                    190: \ we have to store more than just the address of the branch, so the
                    191: \ traditional linked list approach is no longer viable.
                    192: \ This is solved by storing the information about the leavings in a
                    193: \ special stack.
                    194: 
                    195: \ !! remove the fixed size limit. 'Tis not hard.
                    196: 20 constant leave-stack-size
                    197: create leave-stack  60 cells allot
                    198: Avariable leave-sp  leave-stack 3 cells + leave-sp !
                    199: 
                    200: : clear-leave-stack ( -- )
                    201:     leave-stack leave-sp ! ;
                    202: 
                    203: \ : leave-empty? ( -- f )
                    204: \  leave-sp @ leave-stack = ;
                    205: 
                    206: : >leave ( orig -- )
                    207:     \ push on leave-stack
                    208:     leave-sp @
                    209:     dup [ leave-stack 60 cells + ] Aliteral
                    210:     >= abort" leave-stack full"
                    211:     tuck ! cell+
                    212:     tuck ! cell+
                    213:     tuck ! cell+
                    214:     leave-sp ! ;
                    215: 
                    216: : leave> ( -- orig )
                    217:     \ pop from leave-stack
                    218:     leave-sp @
                    219:     dup leave-stack <= IF
                    220:        drop 0 0 0  EXIT  THEN
                    221:     cell - dup @ swap
                    222:     cell - dup @ swap
                    223:     cell - dup @ swap
                    224:     leave-sp ! ;
                    225: 
                    226: : DONE ( compilation orig -- ; run-time -- ) \ gforth
1.23    ! pazsan    227:     \g resolves all LEAVEs up to the compilaton orig (from a BEGIN)
1.1       anton     228:     drop >r drop
                    229:     begin
                    230:        leave>
                    231:        over r@ u>=
                    232:     while
                    233:        POSTPONE then
                    234:     repeat
                    235:     >leave rdrop ; immediate restrict
                    236: 
                    237: : LEAVE ( compilation -- ; run-time loop-sys -- ) \ core
                    238:     POSTPONE ahead
                    239:     >leave ; immediate restrict
                    240: 
                    241: : ?LEAVE ( compilation -- ; run-time f | f loop-sys -- ) \ gforth      question-leave
                    242:     POSTPONE 0= POSTPONE if
                    243:     >leave ; immediate restrict
                    244: 
                    245: : DO ( compilation -- do-sys ; run-time w1 w2 -- loop-sys ) \ core
                    246:     POSTPONE (do)
                    247:     POSTPONE begin drop do-dest
                    248:     ( 0 0 0 >leave ) ; immediate restrict
                    249: 
                    250: : ?do-like ( -- do-sys )
                    251:     ( 0 0 0 >leave )
                    252:     >mark >leave
                    253:     POSTPONE begin drop do-dest ;
                    254: 
                    255: : ?DO ( compilation -- do-sys ; run-time w1 w2 -- | loop-sys ) \ core-ext      question-do
1.14      pazsan    256:     POSTPONE (?do) ?do-like ; immediate restrict
1.1       anton     257: 
                    258: : +DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys ) \ gforth        plus-do
1.14      pazsan    259:     POSTPONE (+do) ?do-like ; immediate restrict
1.1       anton     260: 
                    261: : U+DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys )        \ gforth        u-plus-do
1.14      pazsan    262:     POSTPONE (u+do) ?do-like ; immediate restrict
1.1       anton     263: 
                    264: : -DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys ) \ gforth        minus-do
1.14      pazsan    265:     POSTPONE (-do) ?do-like ; immediate restrict
1.1       anton     266: 
                    267: : U-DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys )        \ gforth        u-minus-do
1.14      pazsan    268:     POSTPONE (u-do) ?do-like ; immediate restrict
1.1       anton     269: 
                    270: : FOR ( compilation -- do-sys ; run-time u -- loop-sys )       \ gforth
                    271:     POSTPONE (for)
                    272:     POSTPONE begin drop do-dest
                    273:     ( 0 0 0 >leave ) ; immediate restrict
                    274: 
                    275: \ LOOP etc. are just like UNTIL
                    276: 
                    277: : loop-like ( do-sys xt1 xt2 -- )
                    278:     >r >r 0 cs-pick swap cell - swap 1 cs-roll r> r> rot do-dest?
                    279:     until-like  POSTPONE done  POSTPONE unloop ;
                    280: 
                    281: : LOOP ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 )   \ core
1.14      pazsan    282:  ['] (loop) ['] (loop)-lp+!# loop-like ; immediate restrict
1.1       anton     283: 
                    284: : +LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 )        \ core  plus-loop
1.14      pazsan    285:  ['] (+loop) ['] (+loop)-lp+!# loop-like ; immediate restrict
1.1       anton     286: 
                    287: \ !! should the compiler warn about +DO..-LOOP?
                    288: : -LOOP ( compilation do-sys -- ; run-time loop-sys1 u -- | loop-sys2 )        \ gforth        minus-loop
1.14      pazsan    289:  ['] (-loop) ['] (-loop)-lp+!# loop-like ; immediate restrict
1.1       anton     290: 
                    291: \ A symmetric version of "+LOOP". I.e., "-high -low ?DO -inc S+LOOP"
                    292: \ will iterate as often as "high low ?DO inc S+LOOP". For positive
                    293: \ increments it behaves like "+LOOP". Use S+LOOP instead of +LOOP for
                    294: \ negative increments.
                    295: : S+LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 )       \ gforth        s-plus-loop
1.14      pazsan    296:  ['] (s+loop) ['] (s+loop)-lp+!# loop-like ; immediate restrict
1.1       anton     297: 
                    298: : NEXT ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 ) \ gforth
1.14      pazsan    299:  ['] (next) ['] (next)-lp+!# loop-like ; immediate restrict
1.1       anton     300: 
                    301: \ Structural Conditionals                              12dec92py
                    302: 
                    303: Defer exit-like ( -- )
                    304: ' noop IS exit-like
                    305: 
                    306: : EXIT ( compilation -- ; run-time nest-sys -- ) \ core
1.12      anton     307: \G Return to the calling definition; usually used as a way of
                    308: \G forcing an early return from a definition. Before
                    309: \G @code{EXIT}ing you must clean up the return stack and
                    310: \G @code{UNLOOP} any outstanding @code{?DO}...@code{LOOP}s.
1.1       anton     311:     exit-like
                    312:     POSTPONE ;s
1.12      anton     313:     basic-block-end
1.1       anton     314:     POSTPONE unreachable ; immediate restrict
                    315: 
                    316: : ?EXIT ( -- ) ( compilation -- ; run-time nest-sys f -- | nest-sys ) \ gforth
                    317:      POSTPONE if POSTPONE exit POSTPONE then ; immediate restrict
                    318: 

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