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

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

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