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

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: 
1.4       anton     207: : postpone, ( w xt -- ) \ gforth       postpone-comma
1.15      crook     208:     \g Compile the compilation semantics represented by @i{w xt}.
1.1       pazsan    209:     dup ['] execute =
                    210:     if
                    211:        drop compile,
                    212:     else
                    213:        dup ['] compile, =
                    214:        if
1.14      jwilke    215:            drop POSTPONE (compile) a,
1.1       pazsan    216:        else
                    217:            swap POSTPONE aliteral compile,
                    218:        then
                    219:     then ;
                    220: 
                    221: : POSTPONE ( "name" -- ) \ core
1.15      crook     222:     \g Compiles the compilation semantics of @i{name}.
1.1       pazsan    223:     COMP' postpone, ; immediate restrict
                    224: 
                    225: struct
                    226:     >body
                    227:     cell% field interpret/compile-int
                    228:     cell% field interpret/compile-comp
                    229: end-struct interpret/compile-struct
                    230: 
                    231: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
                    232:     Create immediate swap A, A,
                    233: DOES>
                    234:     abort" executed primary cfa of an interpret/compile: word" ;
                    235: \    state @ IF  cell+  THEN  perform ;
                    236: 
                    237: \ \ ticks
                    238: 
                    239: : name>comp ( nt -- w xt ) \ gforth
1.15      crook     240:     \G @i{w xt} is the compilation token for the word @i{nt}.
1.1       pazsan    241:     (name>comp)
                    242:     1 = if
                    243:         ['] execute
                    244:     else
                    245:         ['] compile,
                    246:     then ;
                    247: 
                    248: : [(')]  ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
                    249:     (') postpone ALiteral ; immediate restrict
                    250: 
                    251: : [']  ( compilation. "name" -- ; run-time. -- xt ) \ core      bracket-tick
1.15      crook     252:     \g @i{xt} represents @i{name}'s interpretation
                    253:     \g semantics. Perform @code{-14 throw} if the word has no
1.1       pazsan    254:     \g interpretation semantics.
                    255:     ' postpone ALiteral ; immediate restrict
                    256: 
1.5       anton     257: : COMP'    ( "name" -- w xt ) \ gforth  comp-tick
1.15      crook     258:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1       pazsan    259:     (') name>comp ;
                    260: 
                    261: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.15      crook     262:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1       pazsan    263:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
                    264: 
                    265: \ \ recurse                                                    17may93jaw
                    266: 
                    267: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10      crook     268:     \g Call the current definition.
1.1       pazsan    269:     lastxt compile, ; immediate restrict
                    270: 
                    271: \ \ compiler loop
                    272: 
                    273: : compiler ( c-addr u -- )
                    274:     2dup find-name dup
                    275:     if ( c-addr u nt )
                    276:        nip nip name>comp execute
                    277:     else
                    278:        drop
                    279:        2dup snumber? dup
                    280:        IF
                    281:            0>
                    282:            IF
                    283:                swap postpone Literal
                    284:            THEN
                    285:            postpone Literal
                    286:            2drop
                    287:        ELSE
                    288:            drop compiler-notfound
                    289:        THEN
                    290:     then ;
                    291: 
                    292: : [ ( -- ) \ core      left-bracket
1.8       crook     293:     \G Enter interpretation state. Immediate word.
1.1       pazsan    294:     ['] interpreter  IS parser state off ; immediate
                    295: 
                    296: : ] ( -- ) \ core      right-bracket
1.8       crook     297:     \G Enter compilation state.
1.1       pazsan    298:     ['] compiler     IS parser state on  ;
                    299: 
                    300: \ \ Strings                                                    22feb93py
                    301: 
                    302: : ," ( "string"<"> -- ) [char] " parse
                    303:   here over char+ allot  place align ;
                    304: 
                    305: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
1.15      crook     306:     \G Compilation: compile the string specified by @i{c-addr1},
                    307:     \G @i{u} into the current definition. Run-time: return
                    308:     \G @i{c-addr2 u} describing the address and length of the
1.10      crook     309:     \G string.
1.1       pazsan    310:     postpone (S") here over char+ allot  place align ;
                    311:                                              immediate restrict
                    312: 
                    313: \ \ abort"                                                     22feb93py
                    314: 
                    315: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext        abort-quote
1.15      crook     316:     \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
                    317:     \G displaying the string @i{ccc} if there is no exception frame on the
1.10      crook     318:     \G exception stack.
1.1       pazsan    319:     postpone (abort") ," ;        immediate restrict
                    320: 
                    321: \ \ Header states                                              23feb93py
                    322: 
                    323: : cset ( bmask c-addr -- )
                    324:     tuck c@ or swap c! ; 
                    325: 
                    326: : creset ( bmask c-addr -- )
                    327:     tuck c@ swap invert and swap c! ; 
                    328: 
                    329: : ctoggle ( bmask c-addr -- )
                    330:     tuck c@ xor swap c! ; 
                    331: 
                    332: : lastflags ( -- c-addr )
                    333:     \ the address of the flags byte in the last header
                    334:     \ aborts if the last defined word was headerless
                    335:     last @ dup 0= abort" last word was headerless" cell+ ;
                    336: 
                    337: : immediate ( -- ) \ core
1.10      crook     338:     \G Make the compilation semantics of a word be to @code{execute}
                    339:     \G the execution semantics.
1.1       pazsan    340:     immediate-mask lastflags cset ;
                    341: 
                    342: : restrict ( -- ) \ gforth
1.10      crook     343:     \G A synonym for @code{compile-only}
1.1       pazsan    344:     restrict-mask lastflags cset ;
                    345: ' restrict alias compile-only ( -- ) \ gforth
1.10      crook     346: \G Remove the interpretation semantics of a word.
1.1       pazsan    347: 
                    348: \ \ Create Variable User Constant                              17mar93py
                    349: 
1.15      crook     350: : Alias    ( xt "name" -- ) \ gforth
1.1       pazsan    351:     Header reveal
                    352:     alias-mask lastflags creset
                    353:     dup A, lastcfa ! ;
                    354: 
                    355: doer? :dovar [IF]
                    356: 
                    357: : Create ( "name" -- ) \ core
                    358:     Header reveal dovar: cfa, ;
                    359: [ELSE]
                    360: 
                    361: : Create ( "name" -- ) \ core
                    362:     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
                    363: [THEN]
                    364: 
                    365: : Variable ( "name" -- ) \ core
                    366:     Create 0 , ;
                    367: 
                    368: : AVariable ( "name" -- ) \ gforth
                    369:     Create 0 A, ;
                    370: 
                    371: : 2Variable ( "name" -- ) \ double
                    372:     create 0 , 0 , ;
                    373: 
                    374: : uallot ( n -- )  udp @ swap udp +! ;
                    375: 
                    376: doer? :douser [IF]
                    377: 
                    378: : User ( "name" -- ) \ gforth
                    379:     Header reveal douser: cfa, cell uallot , ;
                    380: 
                    381: : AUser ( "name" -- ) \ gforth
                    382:     User ;
                    383: [ELSE]
                    384: 
                    385: : User Create cell uallot , DOES> @ up @ + ;
                    386: 
                    387: : AUser User ;
                    388: [THEN]
                    389: 
                    390: doer? :docon [IF]
                    391:     : (Constant)  Header reveal docon: cfa, ;
                    392: [ELSE]
                    393:     : (Constant)  Create DOES> @ ;
                    394: [THEN]
                    395: 
                    396: : Constant ( w "name" -- ) \ core
1.15      crook     397:     \G Define a constant @i{name} with value @i{w}.
1.1       pazsan    398:     \G  
1.15      crook     399:     \G @i{name} execution: @i{-- w}
1.1       pazsan    400:     (Constant) , ;
                    401: 
                    402: : AConstant ( addr "name" -- ) \ gforth
                    403:     (Constant) A, ;
                    404: 
                    405: : Value ( w "name" -- ) \ core-ext
                    406:     (Constant) , ;
                    407: 
                    408: : 2Constant ( w1 w2 "name" -- ) \ double
                    409:     Create ( w1 w2 "name" -- )
                    410:         2,
                    411:     DOES> ( -- w1 w2 )
                    412:         2@ ;
                    413:     
                    414: doer? :dofield [IF]
                    415:     : (Field)  Header reveal dofield: cfa, ;
                    416: [ELSE]
                    417:     : (Field)  Create DOES> @ + ;
                    418: [THEN]
                    419: \ IS Defer What's Defers TO                            24feb93py
                    420: 
                    421: doer? :dodefer [IF]
                    422: 
                    423: : Defer ( "name" -- ) \ gforth
                    424:     \ !! shouldn't it be initialized with abort or something similar?
                    425:     Header Reveal dodefer: cfa,
                    426:     ['] noop A, ;
                    427: [ELSE]
                    428: 
                    429: : Defer ( "name" -- ) \ gforth
                    430:     Create ['] noop A,
                    431: DOES> @ execute ;
                    432: [THEN]
                    433: 
                    434: : Defers ( "name" -- ) \ gforth
                    435:     ' >body @ compile, ; immediate
                    436: 
                    437: \ \ : ;                                                        24feb93py
                    438: 
                    439: defer :-hook ( sys1 -- sys2 )
                    440: 
                    441: defer ;-hook ( sys2 -- sys1 )
                    442: 
1.16      jwilke    443: 0 Constant defstart
                    444: 
1.14      jwilke    445: [IFDEF] docol,
                    446: : (:noname) ( -- colon-sys )
                    447:     \ common factor of : and :noname
                    448:     docol, ]comp defstart ] :-hook ;
                    449: [ELSE]
1.7       anton     450: : (:noname) ( -- colon-sys )
                    451:     \ common factor of : and :noname
                    452:     docol: cfa, defstart ] :-hook ;
1.14      jwilke    453: [THEN]
1.7       anton     454: 
1.1       pazsan    455: : : ( "name" -- colon-sys ) \ core     colon
1.7       anton     456:     Header (:noname) ;
                    457: 
                    458: : :noname ( -- xt colon-sys ) \ core-ext       colon-no-name
                    459:     0 last !
                    460:     cfalign here (:noname) ;
1.1       pazsan    461: 
1.14      jwilke    462: [IFDEF] fini,
                    463: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core   semicolon
                    464:     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
                    465: [ELSE]
1.1       pazsan    466: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core    semicolon
                    467:     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
1.14      jwilke    468: [THEN]
1.1       pazsan    469: 
                    470: \ \ Search list handling: reveal words, recursive              23feb93py
                    471: 
                    472: : last?   ( -- false / nfa nfa )
                    473:     last @ ?dup ;
                    474: 
                    475: : (reveal) ( nt wid -- )
1.3       pazsan    476:     wordlist-id dup >r
1.1       pazsan    477:     @ over ( name>link ) ! 
                    478:     r> ! ;
                    479: 
                    480: \ make entry in wordlist-map
                    481: ' (reveal) f83search reveal-method !
                    482: 
                    483: Variable warnings ( -- addr ) \ gforth
                    484: G -1 warnings T !
                    485: 
                    486: : check-shadow  ( addr count wid -- )
1.2       pazsan    487:     \G prints a warning if the string is already present in the wordlist
                    488:     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
                    489:        >stderr
                    490:        ." redefined " name>string 2dup type
                    491:        compare 0<> if
                    492:            ."  with " type
                    493:        else
                    494:            2drop
                    495:        then
                    496:        space space EXIT
                    497:     then
                    498:     2drop 2drop ;
1.1       pazsan    499: 
                    500: : reveal ( -- ) \ gforth
                    501:     last?
                    502:     if \ the last word has a header
                    503:        dup ( name>link ) @ 1 and
                    504:        if \ it is still hidden
                    505:            dup ( name>link ) @ 1 xor           ( nt wid )
                    506:            2dup >r name>string r> check-shadow ( nt wid )
                    507:            dup wordlist-map @ reveal-method perform
                    508:        else
                    509:            drop
                    510:        then
                    511:     then ;
                    512: 
                    513: : rehash  ( wid -- )
                    514:     dup wordlist-map @ rehash-method perform ;
                    515: 
                    516: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
1.10      crook     517: \g Make the current definition visible, enabling it to call itself
1.1       pazsan    518: \g recursively.
                    519:        immediate restrict

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