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

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

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