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

1.1       pazsan      1: \ compiler definitions                                         14sep97jaw
                      2: 
1.95      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
1.6       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.93      anton       9: \ as published by the Free Software Foundation, either version 3
1.6       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.93      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.6       anton      19: 
1.1       pazsan     20: \ \ Revisions-Log
                     21: 
                     22: \      put in seperate file                            14sep97jaw      
                     23: 
                     24: \ \ here allot , c, A,                                         17dec92py
                     25: 
1.14      jwilke     26: [IFUNDEF] allot
                     27: [IFUNDEF] forthstart
                     28: : allot ( n -- ) \ core
                     29:     dup unused u> -8 and throw
                     30:     dp +! ;
                     31: [THEN]
                     32: [THEN]
                     33: 
                     34: \ we default to this version if we have nothing else 05May99jaw
                     35: [IFUNDEF] allot
1.1       pazsan     36: : allot ( n -- ) \ core
1.23      anton      37:     \G Reserve @i{n} address units of data space without
                     38:     \G initialization. @i{n} is a signed number, passing a negative
                     39:     \G @i{n} releases memory.  In ANS Forth you can only deallocate
                     40:     \G memory from the current contiguous region in this way.  In
                     41:     \G Gforth you can deallocate anything in this way but named words.
                     42:     \G 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: 
1.22      crook      48: : c,    ( c -- ) \ core c-comma
1.15      crook      49:     \G Reserve data space for one char and store @i{c} in the space.
1.84      pazsan     50:     here 1 chars allot [ has? flash [IF] ] flashc! [ [ELSE] ] c! [ [THEN] ] ;
1.1       pazsan     51: 
1.22      crook      52: : ,     ( w -- ) \ core comma
1.15      crook      53:     \G Reserve data space for one cell and store @i{w} in the space.
1.84      pazsan     54:     here cell allot [ has? flash [IF] ] flash! [ [ELSE] ] ! [ [THEN] ] ;
1.1       pazsan     55: 
                     56: : 2,   ( w1 w2 -- ) \ gforth
1.15      crook      57:     \G Reserve data space for two cells and store the double @i{w1
1.24      anton      58:     \G w2} there, @i{w2} first (lower address).
1.84      pazsan     59:     here 2 cells allot  [ has? flash [IF] ] tuck flash! cell+ flash!
                     60:        [ [ELSE] ] 2! [ [THEN] ] ;
1.1       pazsan     61: 
                     62: \ : aligned ( addr -- addr' ) \ core
                     63: \     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
                     64: 
                     65: : align ( -- ) \ core
1.15      crook      66:     \G If the data-space pointer is not aligned, reserve enough space to align it.
1.1       pazsan     67:     here dup aligned swap ?DO  bl c,  LOOP ;
                     68: 
1.22      crook      69: \ : faligned ( addr -- f-addr ) \ float f-aligned
1.1       pazsan     70: \     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ; 
                     71: 
1.22      crook      72: : falign ( -- ) \ float f-align
1.15      crook      73:     \G If the data-space pointer is not float-aligned, reserve
                     74:     \G enough space to align it.
1.1       pazsan     75:     here dup faligned swap
                     76:     ?DO
                     77:        bl c,
                     78:     LOOP ;
                     79: 
1.9       anton      80: : maxalign ( -- ) \ gforth
1.23      anton      81:     \G Align data-space pointer for all alignment requirements.
1.1       pazsan     82:     here dup maxaligned swap
                     83:     ?DO
                     84:        bl c,
                     85:     LOOP ;
                     86: 
                     87: \ the code field is aligned if its body is maxaligned
                     88: ' maxalign Alias cfalign ( -- ) \ gforth
1.24      anton      89: \G Align data-space pointer for code field requirements (i.e., such
                     90: \G that the corresponding body is maxaligned).
1.1       pazsan     91: 
                     92: ' , alias A, ( addr -- ) \ gforth
                     93: 
                     94: ' NOOP ALIAS const
                     95: 
                     96: \ \ Header                                                     23feb93py
                     97: 
                     98: \ input-stream, nextname and noname are quite ugly (passing
                     99: \ information through global variables), but they are useful for dealing
                    100: \ with existing/independent defining words
                    101: 
                    102: defer (header)
                    103: defer header ( -- ) \ gforth
                    104: ' (header) IS header
                    105: 
                    106: : string, ( c-addr u -- ) \ gforth
                    107:     \G puts down string as cstring
1.83      pazsan    108:     dup [ has? rom [IF] ] $E0 [ [ELSE] ] alias-mask [ [THEN] ] or c,
1.84      pazsan    109: [ has? flash [IF] ]
                    110:     bounds ?DO  I c@ c,  LOOP
                    111: [ [ELSE] ]
                    112:     here swap chars dup allot move
                    113: [ [THEN] ] ;
1.1       pazsan    114: 
1.30      anton     115: : longstring, ( c-addr u -- ) \ gforth
1.55      anton     116:     \G puts down string as longcstring
1.30      anton     117:     dup , here swap chars dup allot move ;
                    118: 
1.96    ! anton     119: variable next-prelude
        !           120: 
        !           121: : prelude, ( -- )
        !           122:     next-prelude @ if
        !           123:        align next-prelude @ ,
        !           124:     then ;
        !           125: 
1.1       pazsan    126: : header, ( c-addr u -- ) \ gforth
                    127:     name-too-long?
1.53      anton     128:     dup max-name-length @ max max-name-length !
1.96    ! anton     129:     prelude,
1.1       pazsan    130:     align here last !
1.94      pazsan    131: [ has? ec [IF] ]
1.83      pazsan    132:     -1 A,
                    133: [ [ELSE] ]
1.1       pazsan    134:     current @ 1 or A,  \ link field; before revealing, it contains the
                    135:                        \ tagged reveal-into wordlist
1.83      pazsan    136: [ [THEN] ]
1.78      pazsan    137: [ has? f83headerstring [IF] ]
                    138:        string,
                    139: [ [ELSE] ]
1.83      pazsan    140:        longstring, alias-mask lastflags cset
1.96    ! anton     141:        next-prelude @ 0<> prelude-mask and lastflags cset
        !           142:        next-prelude off
1.78      pazsan    143: [ [THEN] ]
1.83      pazsan    144:     cfalign ;
1.1       pazsan    145: 
                    146: : input-stream-header ( "name" -- )
1.80      pazsan    147:     parse-name name-too-short? header, ;
1.1       pazsan    148: 
                    149: : input-stream ( -- )  \ general
                    150:     \G switches back to getting the name from the input stream ;
                    151:     ['] input-stream-header IS (header) ;
                    152: 
                    153: ' input-stream-header IS (header)
                    154: 
1.32      anton     155: 2variable nextname-string
1.1       pazsan    156: 
1.33      pazsan    157: has? OS [IF]
1.1       pazsan    158: : nextname-header ( -- )
1.32      anton     159:     nextname-string 2@ header,
                    160:     nextname-string free-mem-var
1.1       pazsan    161:     input-stream ;
1.33      pazsan    162: [THEN]
1.1       pazsan    163: 
                    164: \ the next name is given in the string
                    165: 
1.33      pazsan    166: has? OS [IF]
1.1       pazsan    167: : nextname ( c-addr u -- ) \ gforth
1.19      anton     168:     \g The next defined word will have the name @var{c-addr u}; the
                    169:     \g defining word will leave the input stream alone.
1.1       pazsan    170:     name-too-long?
1.32      anton     171:     nextname-string free-mem-var
                    172:     save-mem nextname-string 2!
1.1       pazsan    173:     ['] nextname-header IS (header) ;
1.33      pazsan    174: [THEN]
1.1       pazsan    175: 
                    176: : noname-header ( -- )
                    177:     0 last ! cfalign
                    178:     input-stream ;
                    179: 
                    180: : noname ( -- ) \ gforth
1.19      anton     181:     \g The next defined word will be anonymous. The defining word will
                    182:     \g leave the input stream alone. The xt of the defined word will
1.56      anton     183:     \g be given by @code{latestxt}.
1.1       pazsan    184:     ['] noname-header IS (header) ;
                    185: 
1.56      anton     186: : latestxt ( -- xt ) \ gforth
1.15      crook     187:     \G @i{xt} is the execution token of the last word defined.
1.13      crook     188:     \ The main purpose of this word is to get the xt of words defined using noname
1.1       pazsan    189:     lastcfa @ ;
                    190: 
1.56      anton     191: ' latestxt alias lastxt \ gforth-obsolete
                    192: \G old name for @code{latestxt}.
                    193: 
                    194: : latest ( -- nt ) \ gforth
                    195: \G @var{nt} is the name token of the last word defined; it is 0 if the
                    196: \G last word has no name.
                    197:     last @ ;
                    198: 
1.1       pazsan    199: \ \ literals                                                   17dec92py
                    200: 
                    201: : Literal  ( compilation n -- ; run-time -- n ) \ core
1.27      anton     202:     \G Compilation semantics: compile the run-time semantics.@*
                    203:     \G Run-time Semantics: push @i{n}.@*
                    204:     \G Interpretation semantics: undefined.
1.14      jwilke    205: [ [IFDEF] lit, ]
                    206:     lit,
                    207: [ [ELSE] ]
1.27      anton     208:     postpone lit ,
1.14      jwilke    209: [ [THEN] ] ; immediate restrict
1.1       pazsan    210: 
1.72      anton     211: : 2Literal ( compilation w1 w2 -- ; run-time  -- w1 w2 ) \ double two-literal
                    212:     \G Compile appropriate code such that, at run-time, @i{w1 w2} are
                    213:     \G placed on the stack. Interpretation semantics are undefined.
                    214:     swap postpone Literal  postpone Literal ; immediate restrict
                    215: 
1.1       pazsan    216: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
1.14      jwilke    217: [ [IFDEF] alit, ]
                    218:     alit,
                    219: [ [ELSE] ]
                    220:     postpone lit A, 
                    221: [ [THEN] ] ; immediate restrict
1.1       pazsan    222: 
1.70      pazsan    223: Defer char@ ( addr u -- char addr' u' )
                    224: :noname  over c@ -rot 1 /string ; IS char@
                    225: 
1.8       crook     226: : char   ( '<spaces>ccc' -- c ) \ core
1.15      crook     227:     \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
                    228:     \G display code representing the first character of @i{ccc}.
1.87      pazsan    229:     parse-name char@ 2drop ;
1.1       pazsan    230: 
1.8       crook     231: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
1.10      crook     232:     \G Compilation: skip leading spaces. Parse the string
1.15      crook     233:     \G @i{ccc}. Run-time: return @i{c}, the display code
                    234:     \G representing the first character of @i{ccc}.  Interpretation
1.10      crook     235:     \G semantics for this word are undefined.
1.1       pazsan    236:     char postpone Literal ; immediate restrict
                    237: 
                    238: \ \ threading                                                  17mar93py
                    239: 
                    240: : cfa,     ( code-address -- )  \ gforth       cfa-comma
                    241:     here
                    242:     dup lastcfa !
1.84      pazsan    243:     [ has? rom [IF] ] 2 cells allot [ [ELSE] ] 0 A, 0 , [ [THEN] ]
                    244:     code-address! ;
1.1       pazsan    245: 
1.14      jwilke    246: [IFUNDEF] compile,
1.34      anton     247: defer compile, ( xt -- )       \ core-ext      compile-comma
                    248: \G  Compile the word represented by the execution token @i{xt}
                    249: \G  into the current definition.
                    250: 
                    251: ' , is compile,
1.14      jwilke    252: [THEN]
1.1       pazsan    253: 
1.80      pazsan    254: has? ec 0= [IF]
1.40      anton     255: defer basic-block-end ( -- )
                    256: 
1.60      anton     257: :noname ( -- )
                    258:     0 compile-prim1 ;
                    259: is basic-block-end
1.80      pazsan    260: [THEN]
1.40      anton     261: 
1.36      pazsan    262: has? peephole [IF]
1.40      anton     263: 
                    264: \ dynamic only    
1.49      anton     265: : peephole-compile, ( xt -- )
                    266:     \ compile xt, appending its code to the current dynamic superinstruction
                    267:     here swap , compile-prim1 ;
1.38      anton     268:     
1.35      anton     269: : compile-to-prims, ( xt -- )
                    270:     \G compile xt to use primitives (and their peephole optimization)
                    271:     \G instead of ","-ing the xt.
                    272:     \ !! all POSTPONEs here postpone primitives; this can be optimized
1.39      pazsan    273:     dup >does-code if
1.66      anton     274:        ['] does-exec peephole-compile, , EXIT
                    275:        \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
1.35      anton     276:     then
                    277:     dup >code-address CASE
1.91      pazsan    278:        dovalue: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
                    279:        docon:   OF >body @ ['] lit peephole-compile, , EXIT ENDOF
1.66      anton     280:        \ docon:   OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
1.42      anton     281:        \ docon is also used by VALUEs, so don't @ at compile time
1.66      anton     282:        docol:   OF >body ['] call peephole-compile, , EXIT ENDOF
                    283:        dovar:   OF >body ['] lit peephole-compile, , EXIT ENDOF
                    284:        douser:  OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
                    285:        dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
                    286:        dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
                    287:        \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
1.48      anton     288:        \ code words and ;code-defined words (code words could be optimized):
1.66      anton     289:        dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN
1.35      anton     290:     ENDCASE
1.49      anton     291:     peephole-compile, ;
1.35      anton     292: 
                    293: ' compile-to-prims, IS compile,
1.36      pazsan    294: [ELSE]
                    295: ' , is compile,
                    296: [THEN]
1.34      anton     297: 
1.1       pazsan    298: : !does    ( addr -- ) \ gforth        store-does
1.56      anton     299:     latestxt does-code! ;
1.1       pazsan    300: 
1.65      anton     301: \ !! unused, but ifdefed/gosted in some places
1.1       pazsan    302: : (does>)  ( R: addr -- )
1.62      anton     303:     r> cfaligned /does-handler + !does ; \ !! no gforth-native
1.1       pazsan    304: 
1.64      anton     305: : (does>2)  ( addr -- )
                    306:     cfaligned /does-handler + !does ;
                    307: 
1.1       pazsan    308: : dodoes,  ( -- )
                    309:   cfalign here /does-handler allot does-handler! ;
                    310: 
1.61      pazsan    311: : (compile) ( -- ) \ gforth-obsolete: dummy
1.63      anton     312:     true abort" (compile) doesn't work, use POSTPONE instead" ;
1.1       pazsan    313: 
                    314: \ \ ticks
                    315: 
1.90      anton     316: : name>comp ( nt -- w xt ) \ gforth name-to-comp
1.15      crook     317:     \G @i{w xt} is the compilation token for the word @i{nt}.
1.1       pazsan    318:     (name>comp)
                    319:     1 = if
                    320:         ['] execute
                    321:     else
                    322:         ['] compile,
                    323:     then ;
                    324: 
                    325: : [(')]  ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
                    326:     (') postpone ALiteral ; immediate restrict
                    327: 
                    328: : [']  ( compilation. "name" -- ; run-time. -- xt ) \ core      bracket-tick
1.15      crook     329:     \g @i{xt} represents @i{name}'s interpretation
                    330:     \g semantics. Perform @code{-14 throw} if the word has no
1.1       pazsan    331:     \g interpretation semantics.
                    332:     ' postpone ALiteral ; immediate restrict
                    333: 
1.5       anton     334: : COMP'    ( "name" -- w xt ) \ gforth  comp-tick
1.15      crook     335:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1       pazsan    336:     (') name>comp ;
                    337: 
                    338: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.15      crook     339:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1       pazsan    340:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
                    341: 
1.18      jwilke    342: : postpone, ( w xt -- ) \ gforth       postpone-comma
1.26      anton     343:     \g Compile the compilation semantics represented by the
                    344:     \g compilation token @i{w xt}.
1.18      jwilke    345:     dup ['] execute =
                    346:     if
                    347:        drop compile,
                    348:     else
1.61      pazsan    349:        swap POSTPONE aliteral compile,
1.18      jwilke    350:     then ;
                    351: 
                    352: : POSTPONE ( "name" -- ) \ core
                    353:     \g Compiles the compilation semantics of @i{name}.
1.48      anton     354:     COMP' postpone, ; immediate
1.18      jwilke    355: 
1.1       pazsan    356: \ \ recurse                                                    17may93jaw
                    357: 
                    358: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10      crook     359:     \g Call the current definition.
1.56      anton     360:     latestxt compile, ; immediate restrict
1.1       pazsan    361: 
                    362: \ \ compiler loop
                    363: 
1.72      anton     364: : compiler1 ( c-addr u -- ... xt )
1.96    ! anton     365:     2dup find-name-run-prelude dup
1.1       pazsan    366:     if ( c-addr u nt )
1.72      anton     367:        nip nip name>comp
1.1       pazsan    368:     else
                    369:        drop
1.72      anton     370:        2dup 2>r snumber? dup
1.1       pazsan    371:        IF
                    372:            0>
                    373:            IF
1.72      anton     374:                ['] 2literal
                    375:            ELSE
                    376:                ['] literal
1.1       pazsan    377:            THEN
1.72      anton     378:            2rdrop
1.1       pazsan    379:        ELSE
1.72      anton     380:            drop 2r> compiler-notfound1
1.1       pazsan    381:        THEN
                    382:     then ;
                    383: 
1.22      crook     384: : [ ( -- ) \  core     left-bracket
1.8       crook     385:     \G Enter interpretation state. Immediate word.
1.72      anton     386:     ['] interpreter1  IS parser1 state off ; immediate
1.1       pazsan    387: 
                    388: : ] ( -- ) \ core      right-bracket
1.8       crook     389:     \G Enter compilation state.
1.72      anton     390:     ['] compiler1     IS parser1 state on  ;
1.1       pazsan    391: 
                    392: \ \ Strings                                                    22feb93py
                    393: 
1.44      anton     394: : S, ( addr u -- )
1.45      anton     395:     \ allot string as counted string
1.84      pazsan    396: [ has? flash [IF] ]
                    397:     dup c, bounds ?DO  I c@ c,  LOOP
                    398: [ [ELSE] ]
                    399:     here over char+ allot  place align
                    400: [ [THEN] ] ;
1.45      anton     401: 
                    402: : mem, ( addr u -- )
                    403:     \ allot the memory block HERE (do alignment yourself)
1.84      pazsan    404: [ has? flash [IF] ]
                    405:     bounds ?DO  I c@ c,  LOOP
                    406: [ [ELSE] ]
                    407:     here over allot swap move
                    408: [ [THEN] ] ;
1.1       pazsan    409: 
1.44      anton     410: : ," ( "string"<"> -- )
                    411:     [char] " parse s, ;
1.1       pazsan    412: 
                    413: \ \ Header states                                              23feb93py
                    414: 
1.83      pazsan    415: \ problematic only for big endian machines
                    416: 
                    417: has? f83headerstring [IF]
                    418: : cset ( bmask c-addr -- )
                    419:     tuck c@ or swap c! ; 
                    420: 
                    421: : creset ( bmask c-addr -- )
                    422:     tuck c@ swap invert and swap c! ; 
                    423: 
                    424: : ctoggle ( bmask c-addr -- )
                    425:     tuck c@ xor swap c! ; 
                    426: [ELSE]
1.1       pazsan    427: : cset ( bmask c-addr -- )
1.30      anton     428:     tuck @ or swap ! ; 
1.1       pazsan    429: 
                    430: : creset ( bmask c-addr -- )
1.30      anton     431:     tuck @ swap invert and swap ! ; 
1.1       pazsan    432: 
                    433: : ctoggle ( bmask c-addr -- )
1.30      anton     434:     tuck @ xor swap ! ; 
1.83      pazsan    435: [THEN]
1.1       pazsan    436: 
                    437: : lastflags ( -- c-addr )
                    438:     \ the address of the flags byte in the last header
                    439:     \ aborts if the last defined word was headerless
1.56      anton     440:     latest dup 0= abort" last word was headerless" cell+ ;
1.1       pazsan    441: 
                    442: : immediate ( -- ) \ core
1.10      crook     443:     \G Make the compilation semantics of a word be to @code{execute}
                    444:     \G the execution semantics.
1.83      pazsan    445:     immediate-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
1.1       pazsan    446: 
                    447: : restrict ( -- ) \ gforth
1.10      crook     448:     \G A synonym for @code{compile-only}
1.83      pazsan    449:     restrict-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
1.18      jwilke    450: 
1.1       pazsan    451: ' restrict alias compile-only ( -- ) \ gforth
1.10      crook     452: \G Remove the interpretation semantics of a word.
1.1       pazsan    453: 
                    454: \ \ Create Variable User Constant                              17mar93py
                    455: 
1.15      crook     456: : Alias    ( xt "name" -- ) \ gforth
1.1       pazsan    457:     Header reveal
                    458:     alias-mask lastflags creset
                    459:     dup A, lastcfa ! ;
                    460: 
                    461: doer? :dovar [IF]
                    462: 
                    463: : Create ( "name" -- ) \ core
                    464:     Header reveal dovar: cfa, ;
                    465: [ELSE]
                    466: 
                    467: : Create ( "name" -- ) \ core
                    468:     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
                    469: [THEN]
                    470: 
1.85      pazsan    471: has? flash [IF]
1.87      pazsan    472:     : (variable) dpp @ normal-dp = IF  Create dpp @
1.85      pazsan    473:        ELSE  normal-dp @ Constant dpp @ ram  THEN ;
                    474: : Variable ( "name" -- ) \ core
                    475:     (Variable) 0 , dpp ! ;
                    476: 
                    477: : AVariable ( "name" -- ) \ gforth
                    478:     (Variable) 0 A, dpp ! ;
                    479: 
                    480: : 2Variable ( "name" -- ) \ double two-variable
                    481:     (Variable) 0 , 0 , dpp ! ;
                    482: [ELSE]
1.1       pazsan    483: : Variable ( "name" -- ) \ core
                    484:     Create 0 , ;
                    485: 
                    486: : AVariable ( "name" -- ) \ gforth
                    487:     Create 0 A, ;
                    488: 
1.22      crook     489: : 2Variable ( "name" -- ) \ double two-variable
1.85      pazsan    490:     Create 0 , 0 , ;
                    491: [THEN]
1.1       pazsan    492: 
1.75      pazsan    493: has? no-userspace 0= [IF]
1.21      crook     494: : uallot ( n -- ) \ gforth
                    495:     udp @ swap udp +! ;
1.1       pazsan    496: 
                    497: doer? :douser [IF]
                    498: 
                    499: : User ( "name" -- ) \ gforth
                    500:     Header reveal douser: cfa, cell uallot , ;
                    501: 
                    502: : AUser ( "name" -- ) \ gforth
                    503:     User ;
                    504: [ELSE]
                    505: 
                    506: : User Create cell uallot , DOES> @ up @ + ;
                    507: 
                    508: : AUser User ;
1.75      pazsan    509: [THEN]
1.1       pazsan    510: [THEN]
                    511: 
                    512: doer? :docon [IF]
                    513:     : (Constant)  Header reveal docon: cfa, ;
                    514: [ELSE]
                    515:     : (Constant)  Create DOES> @ ;
                    516: [THEN]
                    517: 
1.76      pazsan    518: doer? :dovalue [IF]
                    519:     : (Value)  Header reveal dovalue: cfa, ;
                    520: [ELSE]
                    521:     has? rom [IF]
                    522:        : (Value)  Create DOES> @ @ ;
                    523:     [ELSE]
                    524:        : (Value)  Create DOES> @ ;
                    525:     [THEN]
                    526: [THEN]
                    527: 
1.1       pazsan    528: : Constant ( w "name" -- ) \ core
1.15      crook     529:     \G Define a constant @i{name} with value @i{w}.
1.1       pazsan    530:     \G  
1.15      crook     531:     \G @i{name} execution: @i{-- w}
1.1       pazsan    532:     (Constant) , ;
                    533: 
                    534: : AConstant ( addr "name" -- ) \ gforth
                    535:     (Constant) A, ;
                    536: 
1.84      pazsan    537: has? flash [IF]
                    538: : Value ( w "name" -- ) \ core-ext
                    539:     (Value) dpp @ >r here cell allot >r
                    540:     ram here >r , r> r> flash! r> dpp ! ;
                    541: 
                    542: ' Value alias AValue
                    543: [ELSE]
1.1       pazsan    544: : Value ( w "name" -- ) \ core-ext
1.76      pazsan    545:     (Value) , ;
1.1       pazsan    546: 
1.37      jwilke    547: : AValue ( w "name" -- ) \ core-ext
1.76      pazsan    548:     (Value) A, ;
1.84      pazsan    549: [THEN]
1.37      jwilke    550: 
1.22      crook     551: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
1.1       pazsan    552:     Create ( w1 w2 "name" -- )
                    553:         2,
                    554:     DOES> ( -- w1 w2 )
                    555:         2@ ;
                    556:     
                    557: doer? :dofield [IF]
                    558:     : (Field)  Header reveal dofield: cfa, ;
                    559: [ELSE]
                    560:     : (Field)  Create DOES> @ + ;
                    561: [THEN]
1.39      pazsan    562: 
                    563: \ \ interpret/compile:
                    564: 
                    565: struct
                    566:     >body
                    567:     cell% field interpret/compile-int
                    568:     cell% field interpret/compile-comp
                    569: end-struct interpret/compile-struct
                    570: 
                    571: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
                    572:     Create immediate swap A, A,
                    573: DOES>
                    574:     abort" executed primary cfa of an interpret/compile: word" ;
                    575: \    state @ IF  cell+  THEN  perform ;
                    576: 
1.1       pazsan    577: \ IS Defer What's Defers TO                            24feb93py
                    578: 
1.68      anton     579: defer defer-default ( -- )
1.69      anton     580: ' abort is defer-default
                    581: \ default action for deferred words (overridden by a warning later)
1.67      anton     582:     
1.1       pazsan    583: doer? :dodefer [IF]
                    584: 
                    585: : Defer ( "name" -- ) \ gforth
1.67      anton     586: \G Define a deferred word @i{name}; its execution semantics can be
                    587: \G set with @code{defer!} or @code{is} (and they have to, before first
                    588: \G executing @i{name}.
1.1       pazsan    589:     Header Reveal dodefer: cfa,
1.86      pazsan    590:     [ has? rom [IF] ] here >r cell allot
                    591:     dpp @ ram here r> flash! ['] defer-default A, dpp !
                    592:     [ [ELSE] ] ['] defer-default A, [ [THEN] ] ;
1.18      jwilke    593: 
1.1       pazsan    594: [ELSE]
                    595: 
1.77      pazsan    596:     has? rom [IF]
                    597:        : Defer ( "name" -- ) \ gforth
1.86      pazsan    598:            Create here >r cell allot
                    599:            dpp @ ram here r> flash! ['] defer-default A, dpp !
1.77      pazsan    600:          DOES> @ @ execute ;
                    601:     [ELSE]
                    602:        : Defer ( "name" -- ) \ gforth
                    603:            Create ['] defer-default A,
                    604:          DOES> @ execute ;
                    605:     [THEN]
1.1       pazsan    606: [THEN]
                    607: 
1.67      anton     608: : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
                    609: \G @i{xt} represents the word currently associated with the deferred
                    610: \G word @i{xt-deferred}.
1.86      pazsan    611:     >body @ [ has? rom [IF] ] @ [ [THEN] ] ;
1.67      anton     612: 
1.25      anton     613: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
                    614:     \G Compiles the present contents of the deferred word @i{name}
                    615:     \G into the current definition.  I.e., this produces static
                    616:     \G binding as if @i{name} was not deferred.
1.67      anton     617:     ' defer@ compile, ; immediate
1.18      jwilke    618: 
                    619: :noname
                    620:     dodoes, here !does ]
                    621:     defstart :-hook ;
                    622: :noname
                    623:     ;-hook ?struc
                    624:     [ has? xconds [IF] ] exit-like [ [THEN] ]
1.84      pazsan    625:     here [ has? peephole [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] cells +
                    626:     postpone aliteral postpone (does>2) [compile] exit
1.80      pazsan    627:     [ has? peephole [IF] ] finish-code [ [THEN] ] dodoes,
1.18      jwilke    628:     defstart :-hook ;
                    629: interpret/compile: DOES>  ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core        does
                    630: 
1.73      anton     631: : defer! ( xt xt-deferred -- ) \ gforth  defer-store
1.67      anton     632: \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
1.86      pazsan    633:     >body [ has? rom [IF] ] @ [ [THEN] ] ! ;
1.67      anton     634:     
1.20      anton     635: : <IS> ( "name" xt -- ) \ gforth
                    636:     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
1.67      anton     637:     ' defer! ;
1.20      anton     638: 
                    639: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
                    640:     \g At run-time, changes the @code{defer}red word @var{name} to
                    641:     \g execute @var{xt}.
1.67      anton     642:     ' postpone ALiteral postpone defer! ; immediate restrict
1.18      jwilke    643: 
1.20      anton     644: ' <IS>
1.18      jwilke    645: ' [IS]
1.67      anton     646: interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
                    647: \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
                    648: \G Its compilation semantics parses at compile time.
1.18      jwilke    649: 
1.20      anton     650: ' <IS>
                    651: ' [IS]
                    652: interpret/compile: TO ( w "name" -- ) \ core-ext
1.18      jwilke    653: 
                    654: : interpret/compile? ( xt -- flag )
                    655:     >does-code ['] DOES> >does-code = ;
1.1       pazsan    656: 
                    657: \ \ : ;                                                        24feb93py
                    658: 
                    659: defer :-hook ( sys1 -- sys2 )
                    660: 
                    661: defer ;-hook ( sys2 -- sys1 )
                    662: 
1.16      jwilke    663: 0 Constant defstart
                    664: 
1.14      jwilke    665: [IFDEF] docol,
                    666: : (:noname) ( -- colon-sys )
                    667:     \ common factor of : and :noname
1.34      anton     668:     docol, ]comp
1.14      jwilke    669: [ELSE]
1.7       anton     670: : (:noname) ( -- colon-sys )
                    671:     \ common factor of : and :noname
1.34      anton     672:     docol: cfa,
1.14      jwilke    673: [THEN]
1.40      anton     674:     defstart ] :-hook ;
1.7       anton     675: 
1.1       pazsan    676: : : ( "name" -- colon-sys ) \ core     colon
1.7       anton     677:     Header (:noname) ;
                    678: 
                    679: : :noname ( -- xt colon-sys ) \ core-ext       colon-no-name
                    680:     0 last !
                    681:     cfalign here (:noname) ;
1.1       pazsan    682: 
1.14      jwilke    683: [IFDEF] fini,
                    684: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core   semicolon
                    685:     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
                    686: [ELSE]
1.1       pazsan    687: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core    semicolon
1.57      pazsan    688:     ;-hook ?struc [compile] exit
                    689:     [ has? peephole [IF] ] finish-code [ [THEN] ]
                    690:     reveal postpone [ ; immediate restrict
1.14      jwilke    691: [THEN]
1.1       pazsan    692: 
                    693: \ \ Search list handling: reveal words, recursive              23feb93py
                    694: 
                    695: : last?   ( -- false / nfa nfa )
1.56      anton     696:     latest ?dup ;
1.1       pazsan    697: 
1.83      pazsan    698: Variable warnings ( -- addr ) \ gforth
                    699: G -1 warnings T !
                    700: 
                    701: has? ec [IF]
                    702: : reveal ( -- ) \ gforth
                    703:     last?
                    704:     if \ the last word has a header
                    705:        dup ( name>link ) @ -1 =
                    706:        if \ it is still hidden
1.88      pazsan    707:            forth-wordlist dup >r @ over
1.84      pazsan    708:            [ has? flash [IF] ] flash! [ [ELSE] ] ! [  [THEN] ] r> !
1.83      pazsan    709:        else
                    710:            drop
                    711:        then
                    712:     then ;
                    713: [ELSE]
1.1       pazsan    714: : (reveal) ( nt wid -- )
1.3       pazsan    715:     wordlist-id dup >r
1.1       pazsan    716:     @ over ( name>link ) ! 
                    717:     r> ! ;
                    718: 
                    719: \ make entry in wordlist-map
                    720: ' (reveal) f83search reveal-method !
                    721: 
                    722: : check-shadow  ( addr count wid -- )
1.2       pazsan    723:     \G prints a warning if the string is already present in the wordlist
                    724:     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
                    725:        >stderr
                    726:        ." redefined " name>string 2dup type
1.43      anton     727:        str= 0= if
1.2       pazsan    728:            ."  with " type
                    729:        else
                    730:            2drop
                    731:        then
                    732:        space space EXIT
                    733:     then
                    734:     2drop 2drop ;
1.1       pazsan    735: 
                    736: : reveal ( -- ) \ gforth
                    737:     last?
                    738:     if \ the last word has a header
                    739:        dup ( name>link ) @ 1 and
                    740:        if \ it is still hidden
                    741:            dup ( name>link ) @ 1 xor           ( nt wid )
                    742:            2dup >r name>string r> check-shadow ( nt wid )
1.83      pazsan    743:            dup wordlist-map @ reveal-method perform
1.1       pazsan    744:        else
                    745:            drop
                    746:        then
                    747:     then ;
                    748: 
                    749: : rehash  ( wid -- )
                    750:     dup wordlist-map @ rehash-method perform ;
1.80      pazsan    751: [THEN]
1.1       pazsan    752: 
                    753: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
1.10      crook     754: \g Make the current definition visible, enabling it to call itself
1.1       pazsan    755: \g recursively.
                    756:        immediate restrict

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