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

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

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