Annotation of gforth/kernel/comp.fs, revision 1.18

1.1       pazsan      1: \ compiler definitions                                         14sep97jaw
                      2: 
1.6       anton       3: \ Copyright (C) 1995,1996,1997,1998 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 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: 
1.1       pazsan     21: \ \ Revisions-Log
                     22: 
                     23: \      put in seperate file                            14sep97jaw      
                     24: 
                     25: \ \ here allot , c, A,                                         17dec92py
                     26: 
1.14      jwilke     27: [IFUNDEF] allot
                     28: [IFUNDEF] forthstart
                     29: : allot ( n -- ) \ core
                     30:     dup unused u> -8 and throw
                     31:     dp +! ;
                     32: [THEN]
                     33: [THEN]
                     34: 
                     35: \ we default to this version if we have nothing else 05May99jaw
                     36: [IFUNDEF] allot
1.1       pazsan     37: : allot ( n -- ) \ core
1.17      anton      38:     \G Reserve or release @i{n} address units of data space without
                     39:     \G initialization; @i{n} is a signed number.  In ANS Forth you can
                     40:     \G only deallocate memory from the current contiguous region in
                     41:     \G this way.  In Gforth you can deallocate anything in this way
                     42:     \G but named words.  The system does not check this restriction.
1.12      anton      43:     here +
                     44:     dup 1- usable-dictionary-end forthstart within -8 and throw
                     45:     dp ! ;
1.14      jwilke     46: [THEN]
1.1       pazsan     47: 
                     48: : c,    ( c -- ) \ core
1.15      crook      49:     \G Reserve data space for one char and store @i{c} in the space.
1.1       pazsan     50:     here 1 chars allot c! ;
                     51: 
1.11      crook      52: : ,     ( w -- ) \ core
1.15      crook      53:     \G Reserve data space for one cell and store @i{w} in the space.
1.1       pazsan     54:     here cell allot  ! ;
                     55: 
                     56: : 2,   ( w1 w2 -- ) \ gforth
1.15      crook      57:     \G Reserve data space for two cells and store the double @i{w1
1.11      crook      58:     \G w2} in the space.
1.1       pazsan     59:     here 2 cells allot 2! ;
                     60: 
                     61: \ : aligned ( addr -- addr' ) \ core
                     62: \     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
                     63: 
                     64: : align ( -- ) \ core
1.15      crook      65:     \G If the data-space pointer is not aligned, reserve enough space to align it.
1.1       pazsan     66:     here dup aligned swap ?DO  bl c,  LOOP ;
                     67: 
                     68: \ : faligned ( addr -- f-addr ) \ float
                     69: \     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ; 
                     70: 
                     71: : falign ( -- ) \ float
1.15      crook      72:     \G If the data-space pointer is not float-aligned, reserve
                     73:     \G enough space to align it.
1.1       pazsan     74:     here dup faligned swap
                     75:     ?DO
                     76:        bl c,
                     77:     LOOP ;
                     78: 
1.9       anton      79: : maxalign ( -- ) \ gforth
1.1       pazsan     80:     here dup maxaligned swap
                     81:     ?DO
                     82:        bl c,
                     83:     LOOP ;
                     84: 
                     85: \ the code field is aligned if its body is maxaligned
                     86: ' maxalign Alias cfalign ( -- ) \ gforth
                     87: 
                     88: ' , alias A, ( addr -- ) \ gforth
                     89: 
                     90: ' NOOP ALIAS const
                     91: 
                     92: \ \ Header                                                     23feb93py
                     93: 
                     94: \ input-stream, nextname and noname are quite ugly (passing
                     95: \ information through global variables), but they are useful for dealing
                     96: \ with existing/independent defining words
                     97: 
                     98: defer (header)
                     99: defer header ( -- ) \ gforth
                    100: ' (header) IS header
                    101: 
                    102: : string, ( c-addr u -- ) \ gforth
                    103:     \G puts down string as cstring
                    104:     dup c, here swap chars dup allot move ;
                    105: 
                    106: : header, ( c-addr u -- ) \ gforth
                    107:     name-too-long?
                    108:     align here last !
                    109:     current @ 1 or A,  \ link field; before revealing, it contains the
                    110:                        \ tagged reveal-into wordlist
                    111:     string, cfalign
                    112:     alias-mask lastflags cset ;
                    113: 
                    114: : input-stream-header ( "name" -- )
                    115:     name name-too-short? header, ;
                    116: 
                    117: : input-stream ( -- )  \ general
                    118:     \G switches back to getting the name from the input stream ;
                    119:     ['] input-stream-header IS (header) ;
                    120: 
                    121: ' input-stream-header IS (header)
                    122: 
                    123: \ !! make that a 2variable
                    124: create nextname-buffer 32 chars allot
                    125: 
                    126: : nextname-header ( -- )
                    127:     nextname-buffer count header,
                    128:     input-stream ;
                    129: 
                    130: \ the next name is given in the string
                    131: 
                    132: : nextname ( c-addr u -- ) \ gforth
                    133:     name-too-long?
                    134:     nextname-buffer c! ( c-addr )
                    135:     nextname-buffer count move
                    136:     ['] nextname-header IS (header) ;
                    137: 
                    138: : noname-header ( -- )
                    139:     0 last ! cfalign
                    140:     input-stream ;
                    141: 
                    142: : noname ( -- ) \ gforth
                    143: \ the next defined word remains anonymous. The xt of that word is given by lastxt
                    144:     ['] noname-header IS (header) ;
                    145: 
                    146: : lastxt ( -- xt ) \ gforth
1.15      crook     147:     \G @i{xt} is the execution token of the last word defined.
1.13      crook     148:     \ The main purpose of this word is to get the xt of words defined using noname
1.1       pazsan    149:     lastcfa @ ;
                    150: 
                    151: \ \ literals                                                   17dec92py
                    152: 
                    153: : Literal  ( compilation n -- ; run-time -- n ) \ core
1.15      crook     154:     \G Compile appropriate code such that, at run-time, @i{n} is placed
1.8       crook     155:     \G on the stack. Interpretation semantics are undefined.
1.14      jwilke    156: [ [IFDEF] lit, ]
                    157:     lit,
                    158: [ [ELSE] ]
                    159:     postpone lit , 
                    160: [ [THEN] ] ; immediate restrict
1.1       pazsan    161: 
                    162: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
1.14      jwilke    163: [ [IFDEF] alit, ]
                    164:     alit,
                    165: [ [ELSE] ]
                    166:     postpone lit A, 
                    167: [ [THEN] ] ; immediate restrict
1.1       pazsan    168: 
1.8       crook     169: : char   ( '<spaces>ccc' -- c ) \ core
1.15      crook     170:     \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
                    171:     \G display code representing the first character of @i{ccc}.
1.1       pazsan    172:     bl word char+ c@ ;
                    173: 
1.8       crook     174: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
1.10      crook     175:     \G Compilation: skip leading spaces. Parse the string
1.15      crook     176:     \G @i{ccc}. Run-time: return @i{c}, the display code
                    177:     \G representing the first character of @i{ccc}.  Interpretation
1.10      crook     178:     \G semantics for this word are undefined.
1.1       pazsan    179:     char postpone Literal ; immediate restrict
                    180: 
                    181: \ \ threading                                                  17mar93py
                    182: 
                    183: : cfa,     ( code-address -- )  \ gforth       cfa-comma
                    184:     here
                    185:     dup lastcfa !
                    186:     0 A, 0 ,  code-address! ;
                    187: 
1.14      jwilke    188: [IFUNDEF] compile,
1.1       pazsan    189: : compile, ( xt -- )   \ core-ext      compile-comma
1.15      crook     190:     \G  Compile the word represented by the execution token, @i{xt},
                    191:     \G  into the current definition.
1.1       pazsan    192:     A, ;
1.14      jwilke    193: [THEN]
1.1       pazsan    194: 
                    195: : !does    ( addr -- ) \ gforth        store-does
                    196:     lastxt does-code! ;
                    197: 
                    198: : (does>)  ( R: addr -- )
                    199:     r> cfaligned /does-handler + !does ;
                    200: 
                    201: : dodoes,  ( -- )
                    202:   cfalign here /does-handler allot does-handler! ;
                    203: 
                    204: : (compile) ( -- ) \ gforth
                    205:     r> dup cell+ >r @ compile, ;
                    206: 
                    207: \ \ ticks
                    208: 
                    209: : name>comp ( nt -- w xt ) \ gforth
1.15      crook     210:     \G @i{w xt} is the compilation token for the word @i{nt}.
1.1       pazsan    211:     (name>comp)
                    212:     1 = if
                    213:         ['] execute
                    214:     else
                    215:         ['] compile,
                    216:     then ;
                    217: 
                    218: : [(')]  ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
                    219:     (') postpone ALiteral ; immediate restrict
                    220: 
                    221: : [']  ( compilation. "name" -- ; run-time. -- xt ) \ core      bracket-tick
1.15      crook     222:     \g @i{xt} represents @i{name}'s interpretation
                    223:     \g semantics. Perform @code{-14 throw} if the word has no
1.1       pazsan    224:     \g interpretation semantics.
                    225:     ' postpone ALiteral ; immediate restrict
                    226: 
1.5       anton     227: : COMP'    ( "name" -- w xt ) \ gforth  comp-tick
1.15      crook     228:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1       pazsan    229:     (') name>comp ;
                    230: 
                    231: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.15      crook     232:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1       pazsan    233:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
                    234: 
1.18    ! jwilke    235: : postpone, ( w xt -- ) \ gforth       postpone-comma
        !           236:     \g Compile the compilation semantics represented by @i{w xt}.
        !           237:     dup ['] execute =
        !           238:     if
        !           239:        drop compile,
        !           240:     else
        !           241:        dup ['] compile, =
        !           242:        if
        !           243:            drop POSTPONE (compile) a,
        !           244:        else
        !           245:            swap POSTPONE aliteral compile,
        !           246:        then
        !           247:     then ;
        !           248: 
        !           249: : POSTPONE ( "name" -- ) \ core
        !           250:     \g Compiles the compilation semantics of @i{name}.
        !           251:     COMP' postpone, ; immediate restrict
        !           252: 
1.1       pazsan    253: \ \ recurse                                                    17may93jaw
                    254: 
                    255: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10      crook     256:     \g Call the current definition.
1.1       pazsan    257:     lastxt compile, ; immediate restrict
                    258: 
                    259: \ \ compiler loop
                    260: 
                    261: : compiler ( c-addr u -- )
                    262:     2dup find-name dup
                    263:     if ( c-addr u nt )
                    264:        nip nip name>comp execute
                    265:     else
                    266:        drop
                    267:        2dup snumber? dup
                    268:        IF
                    269:            0>
                    270:            IF
                    271:                swap postpone Literal
                    272:            THEN
                    273:            postpone Literal
                    274:            2drop
                    275:        ELSE
                    276:            drop compiler-notfound
                    277:        THEN
                    278:     then ;
                    279: 
                    280: : [ ( -- ) \ core      left-bracket
1.8       crook     281:     \G Enter interpretation state. Immediate word.
1.1       pazsan    282:     ['] interpreter  IS parser state off ; immediate
                    283: 
                    284: : ] ( -- ) \ core      right-bracket
1.8       crook     285:     \G Enter compilation state.
1.1       pazsan    286:     ['] compiler     IS parser state on  ;
                    287: 
                    288: \ \ Strings                                                    22feb93py
                    289: 
                    290: : ," ( "string"<"> -- ) [char] " parse
                    291:   here over char+ allot  place align ;
                    292: 
                    293: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
1.15      crook     294:     \G Compilation: compile the string specified by @i{c-addr1},
                    295:     \G @i{u} into the current definition. Run-time: return
                    296:     \G @i{c-addr2 u} describing the address and length of the
1.10      crook     297:     \G string.
1.1       pazsan    298:     postpone (S") here over char+ allot  place align ;
                    299:                                              immediate restrict
                    300: 
                    301: \ \ abort"                                                     22feb93py
                    302: 
                    303: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext        abort-quote
1.15      crook     304:     \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
                    305:     \G displaying the string @i{ccc} if there is no exception frame on the
1.10      crook     306:     \G exception stack.
1.1       pazsan    307:     postpone (abort") ," ;        immediate restrict
                    308: 
                    309: \ \ Header states                                              23feb93py
                    310: 
                    311: : cset ( bmask c-addr -- )
                    312:     tuck c@ or swap c! ; 
                    313: 
                    314: : creset ( bmask c-addr -- )
                    315:     tuck c@ swap invert and swap c! ; 
                    316: 
                    317: : ctoggle ( bmask c-addr -- )
                    318:     tuck c@ xor swap c! ; 
                    319: 
                    320: : lastflags ( -- c-addr )
                    321:     \ the address of the flags byte in the last header
                    322:     \ aborts if the last defined word was headerless
                    323:     last @ dup 0= abort" last word was headerless" cell+ ;
                    324: 
                    325: : immediate ( -- ) \ core
1.10      crook     326:     \G Make the compilation semantics of a word be to @code{execute}
                    327:     \G the execution semantics.
1.1       pazsan    328:     immediate-mask lastflags cset ;
                    329: 
                    330: : restrict ( -- ) \ gforth
1.10      crook     331:     \G A synonym for @code{compile-only}
1.1       pazsan    332:     restrict-mask lastflags cset ;
1.18    ! jwilke    333: 
1.1       pazsan    334: ' restrict alias compile-only ( -- ) \ gforth
1.10      crook     335: \G Remove the interpretation semantics of a word.
1.1       pazsan    336: 
                    337: \ \ Create Variable User Constant                              17mar93py
                    338: 
1.15      crook     339: : Alias    ( xt "name" -- ) \ gforth
1.1       pazsan    340:     Header reveal
                    341:     alias-mask lastflags creset
                    342:     dup A, lastcfa ! ;
                    343: 
                    344: doer? :dovar [IF]
                    345: 
                    346: : Create ( "name" -- ) \ core
                    347:     Header reveal dovar: cfa, ;
                    348: [ELSE]
                    349: 
                    350: : Create ( "name" -- ) \ core
                    351:     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
                    352: [THEN]
                    353: 
                    354: : Variable ( "name" -- ) \ core
                    355:     Create 0 , ;
                    356: 
                    357: : AVariable ( "name" -- ) \ gforth
                    358:     Create 0 A, ;
                    359: 
                    360: : 2Variable ( "name" -- ) \ double
                    361:     create 0 , 0 , ;
                    362: 
                    363: : uallot ( n -- )  udp @ swap udp +! ;
                    364: 
                    365: doer? :douser [IF]
                    366: 
                    367: : User ( "name" -- ) \ gforth
                    368:     Header reveal douser: cfa, cell uallot , ;
                    369: 
                    370: : AUser ( "name" -- ) \ gforth
                    371:     User ;
                    372: [ELSE]
                    373: 
                    374: : User Create cell uallot , DOES> @ up @ + ;
                    375: 
                    376: : AUser User ;
                    377: [THEN]
                    378: 
                    379: doer? :docon [IF]
                    380:     : (Constant)  Header reveal docon: cfa, ;
                    381: [ELSE]
                    382:     : (Constant)  Create DOES> @ ;
                    383: [THEN]
                    384: 
                    385: : Constant ( w "name" -- ) \ core
1.15      crook     386:     \G Define a constant @i{name} with value @i{w}.
1.1       pazsan    387:     \G  
1.15      crook     388:     \G @i{name} execution: @i{-- w}
1.1       pazsan    389:     (Constant) , ;
                    390: 
                    391: : AConstant ( addr "name" -- ) \ gforth
                    392:     (Constant) A, ;
                    393: 
                    394: : Value ( w "name" -- ) \ core-ext
                    395:     (Constant) , ;
                    396: 
                    397: : 2Constant ( w1 w2 "name" -- ) \ double
                    398:     Create ( w1 w2 "name" -- )
                    399:         2,
                    400:     DOES> ( -- w1 w2 )
                    401:         2@ ;
                    402:     
                    403: doer? :dofield [IF]
                    404:     : (Field)  Header reveal dofield: cfa, ;
                    405: [ELSE]
                    406:     : (Field)  Create DOES> @ + ;
                    407: [THEN]
                    408: \ IS Defer What's Defers TO                            24feb93py
                    409: 
                    410: doer? :dodefer [IF]
                    411: 
                    412: : Defer ( "name" -- ) \ gforth
                    413:     \ !! shouldn't it be initialized with abort or something similar?
                    414:     Header Reveal dodefer: cfa,
                    415:     ['] noop A, ;
1.18    ! jwilke    416: 
1.1       pazsan    417: [ELSE]
                    418: 
                    419: : Defer ( "name" -- ) \ gforth
                    420:     Create ['] noop A,
                    421: DOES> @ execute ;
1.18    ! jwilke    422: 
1.1       pazsan    423: [THEN]
                    424: 
                    425: : Defers ( "name" -- ) \ gforth
                    426:     ' >body @ compile, ; immediate
1.18    ! jwilke    427: 
        !           428: :noname
        !           429:     dodoes, here !does ]
        !           430:     defstart :-hook ;
        !           431: :noname
        !           432:     ;-hook ?struc
        !           433:     [ has? xconds [IF] ] exit-like [ [THEN] ]
        !           434:     postpone (does>) dodoes,
        !           435:     defstart :-hook ;
        !           436: interpret/compile: DOES>  ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core        does
        !           437: 
        !           438: : [IS] ( compilation "name" -- ; run-time xt -- ) \ possibly-gforth bracket-is
        !           439:     ' >body postpone ALiteral postpone ! ; immediate restrict
        !           440: 
        !           441: :noname    ' >body ! ;
        !           442: ' [IS]
        !           443: interpret/compile: IS ( xt "name" -- ) \ gforth
        !           444: 
        !           445: ' IS Alias TO ( w "name" -- ) \ core-ext
        !           446: immediate
        !           447: 
        !           448: :noname    ' >body @ ;
        !           449: :noname    ' >body postpone ALiteral postpone @ ;
        !           450: interpret/compile: What's ( "name" -- addr ) \ gforth
        !           451: 
        !           452: \ \ interpret/compile:
        !           453: 
        !           454: struct
        !           455:     >body
        !           456:     cell% field interpret/compile-int
        !           457:     cell% field interpret/compile-comp
        !           458: end-struct interpret/compile-struct
        !           459: 
        !           460: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
        !           461:     Create immediate swap A, A,
        !           462: DOES>
        !           463:     abort" executed primary cfa of an interpret/compile: word" ;
        !           464: \    state @ IF  cell+  THEN  perform ;
        !           465: 
        !           466: : interpret/compile? ( xt -- flag )
        !           467:     >does-code ['] DOES> >does-code = ;
1.1       pazsan    468: 
                    469: \ \ : ;                                                        24feb93py
                    470: 
                    471: defer :-hook ( sys1 -- sys2 )
                    472: 
                    473: defer ;-hook ( sys2 -- sys1 )
                    474: 
1.16      jwilke    475: 0 Constant defstart
                    476: 
1.14      jwilke    477: [IFDEF] docol,
                    478: : (:noname) ( -- colon-sys )
                    479:     \ common factor of : and :noname
                    480:     docol, ]comp defstart ] :-hook ;
                    481: [ELSE]
1.7       anton     482: : (:noname) ( -- colon-sys )
                    483:     \ common factor of : and :noname
                    484:     docol: cfa, defstart ] :-hook ;
1.14      jwilke    485: [THEN]
1.7       anton     486: 
1.1       pazsan    487: : : ( "name" -- colon-sys ) \ core     colon
1.7       anton     488:     Header (:noname) ;
                    489: 
                    490: : :noname ( -- xt colon-sys ) \ core-ext       colon-no-name
                    491:     0 last !
                    492:     cfalign here (:noname) ;
1.1       pazsan    493: 
1.14      jwilke    494: [IFDEF] fini,
                    495: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core   semicolon
                    496:     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
                    497: [ELSE]
1.1       pazsan    498: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core    semicolon
                    499:     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
1.14      jwilke    500: [THEN]
1.1       pazsan    501: 
                    502: \ \ Search list handling: reveal words, recursive              23feb93py
                    503: 
                    504: : last?   ( -- false / nfa nfa )
                    505:     last @ ?dup ;
                    506: 
                    507: : (reveal) ( nt wid -- )
1.3       pazsan    508:     wordlist-id dup >r
1.1       pazsan    509:     @ over ( name>link ) ! 
                    510:     r> ! ;
                    511: 
                    512: \ make entry in wordlist-map
                    513: ' (reveal) f83search reveal-method !
                    514: 
                    515: Variable warnings ( -- addr ) \ gforth
                    516: G -1 warnings T !
                    517: 
                    518: : check-shadow  ( addr count wid -- )
1.2       pazsan    519:     \G prints a warning if the string is already present in the wordlist
                    520:     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
                    521:        >stderr
                    522:        ." redefined " name>string 2dup type
                    523:        compare 0<> if
                    524:            ."  with " type
                    525:        else
                    526:            2drop
                    527:        then
                    528:        space space EXIT
                    529:     then
                    530:     2drop 2drop ;
1.1       pazsan    531: 
                    532: : reveal ( -- ) \ gforth
                    533:     last?
                    534:     if \ the last word has a header
                    535:        dup ( name>link ) @ 1 and
                    536:        if \ it is still hidden
                    537:            dup ( name>link ) @ 1 xor           ( nt wid )
                    538:            2dup >r name>string r> check-shadow ( nt wid )
                    539:            dup wordlist-map @ reveal-method perform
                    540:        else
                    541:            drop
                    542:        then
                    543:     then ;
                    544: 
                    545: : rehash  ( wid -- )
                    546:     dup wordlist-map @ rehash-method perform ;
                    547: 
                    548: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
1.10      crook     549: \g Make the current definition visible, enabling it to call itself
1.1       pazsan    550: \g recursively.
                    551:        immediate restrict

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