[gforth] / gforth / kernel / comp.fs  

gforth: gforth/kernel/comp.fs

Diff for /gforth/kernel/comp.fs between version 1.43 and 1.67

version 1.43, Sat Sep 14 08:20:21 2002 UTC version 1.67, Sun Nov 28 20:20:38 2004 UTC
Line 1 
Line 1 
 \ compiler definitions                                          14sep97jaw  \ compiler definitions                                          14sep97jaw
   
 \ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.  \ Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
   
 \ This file is part of Gforth.  \ This file is part of Gforth.
   
Line 108 
Line 108 
     dup c, here swap chars dup allot move ;      dup c, here swap chars dup allot move ;
   
 : longstring, ( c-addr u -- ) \ gforth  : longstring, ( c-addr u -- ) \ gforth
     \G puts down string as cstring      \G puts down string as longcstring
     dup , here swap chars dup allot move ;      dup , here swap chars dup allot move ;
   
 : header, ( c-addr u -- ) \ gforth  : header, ( c-addr u -- ) \ gforth
     name-too-long?      name-too-long?
       dup max-name-length @ max max-name-length !
     align here last !      align here last !
     current @ 1 or A,   \ link field; before revealing, it contains the      current @ 1 or A,   \ link field; before revealing, it contains the
                         \ tagged reveal-into wordlist                          \ tagged reveal-into wordlist
Line 156 
Line 157 
 : noname ( -- ) \ gforth  : noname ( -- ) \ gforth
     \g The next defined word will be anonymous. The defining word will      \g The next defined word will be anonymous. The defining word will
     \g leave the input stream alone. The xt of the defined word will      \g leave the input stream alone. The xt of the defined word will
     \g be given by @code{lastxt}.      \g be given by @code{latestxt}.
     ['] noname-header IS (header) ;      ['] noname-header IS (header) ;
   
 : lastxt ( -- xt ) \ gforth  : latestxt ( -- xt ) \ gforth
     \G @i{xt} is the execution token of the last word defined.      \G @i{xt} is the execution token of the last word defined.
     \ The main purpose of this word is to get the xt of words defined using noname      \ The main purpose of this word is to get the xt of words defined using noname
     lastcfa @ ;      lastcfa @ ;
   
   ' latestxt alias lastxt \ gforth-obsolete
   \G old name for @code{latestxt}.
   
   : latest ( -- nt ) \ gforth
   \G @var{nt} is the name token of the last word defined; it is 0 if the
   \G last word has no name.
       last @ ;
   
 \ \ literals                                                    17dec92py  \ \ literals                                                    17dec92py
   
 : Literal  ( compilation n -- ; run-time -- n ) \ core  : Literal  ( compilation n -- ; run-time -- n ) \ core
Line 212 
Line 221 
   
 defer basic-block-end ( -- )  defer basic-block-end ( -- )
   
 : bb-end ( -- )  :noname ( -- )
     0 last-compiled ! ;      0 compile-prim1 ;
 ' bb-end is basic-block-end  is basic-block-end
   
 has? peephole [IF]  has? peephole [IF]
   
 \ dynamic only  \ dynamic only
 \  : peephole-compile, ( xt -- )  : peephole-compile, ( xt -- )
 \      \ compile xt, appending its code to the current dynamic superinstruction  
 \      compile-prim , ;  
   
 \ static only  
 \  : peephole-compile, ( xt -- )  
 \      \ compile xt, possibly combining it with the previous compiled xt  
 \      \ into a superinstruction (static superinstructions)  
 \      last-compiled @ ?dup if  
 \       @ over peeptable peephole-opt ?dup if  
 \           last-compiled @ ! drop EXIT  
 \       then  
 \      then  
 \      here last-compiled !  
 \      dyn-compile, ;  
   
 : dyn-compile! ( xt -- )  
     \ compile xt, appending its code to the current dynamic superinstruction      \ compile xt, appending its code to the current dynamic superinstruction
     compile-prim last-compiled-here @ ! ;      here swap , compile-prim1 ;
   
 :noname ( -- )  
     last-compiled @ if  
         last-compiled @ dyn-compile!  
         0 last-compiled !  
     then ;  
 is basic-block-end  
   
 : static-compile, ( xt -- )  
     \ compile xt, possibly combining it with the previous compiled xt  
     \ into a superinstruction (static superinstructions)  
     last-compiled @ ?dup if  
         over peeptable peephole-opt ?dup if ( xt comb-xt )  
             last-compiled ! drop EXIT  
         then ( xt )  
         last-compiled @ dyn-compile!  
     then ( xt )  
     last-compiled !  
     here last-compiled-here ! 0 , ;  
   
 : compile-to-prims, ( xt -- )  : compile-to-prims, ( xt -- )
     \G compile xt to use primitives (and their peephole optimization)      \G compile xt to use primitives (and their peephole optimization)
     \G instead of ","-ing the xt.      \G instead of ","-ing the xt.
     \ !! all POSTPONEs here postpone primitives; this can be optimized      \ !! all POSTPONEs here postpone primitives; this can be optimized
     dup >does-code if      dup >does-code if
         POSTPONE does-exec , EXIT          ['] does-exec peephole-compile, , EXIT
         \ dup >body POSTPONE literal POSTPONE call >does-code , EXIT          \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
     then      then
     dup >code-address CASE      dup >code-address CASE
         \ docon:   OF >body POSTPONE lit@ , EXIT ENDOF          docon:   OF >body ['] lit@ peephole-compile, , EXIT ENDOF
         docon:   OF >body POSTPONE literal POSTPONE @ EXIT ENDOF          \ docon:   OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
         \ docon is also used by VALUEs, so don't @ at compile time          \ docon is also used by VALUEs, so don't @ at compile time
         docol:   OF >body POSTPONE call , EXIT ENDOF          docol:   OF >body ['] call peephole-compile, , EXIT ENDOF
         dovar:   OF >body POSTPONE literal EXIT ENDOF          dovar:   OF >body ['] lit peephole-compile, , EXIT ENDOF
         douser:  OF >body @ POSTPONE useraddr , EXIT ENDOF          douser:  OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
         dodefer: OF >body POSTPONE lit-perform , EXIT ENDOF          dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
         \ dofield: OF >body @ POSTPONE lit+ , EXIT ENDOF          dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
         dofield: OF >body @ POSTPONE literal POSTPONE + EXIT ENDOF          \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
           \ code words and ;code-defined words (code words could be optimized):
           dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN
     ENDCASE      ENDCASE
     static-compile, ;      peephole-compile, ;
   
 ' compile-to-prims, IS compile,  ' compile-to-prims, IS compile,
 [ELSE]  [ELSE]
Line 285 
Line 261 
 [THEN]  [THEN]
   
 : !does    ( addr -- ) \ gforth store-does  : !does    ( addr -- ) \ gforth store-does
     lastxt does-code! ;      latestxt does-code! ;
   
   \ !! unused, but ifdefed/gosted in some places
 : (does>)  ( R: addr -- )  : (does>)  ( R: addr -- )
     r> cfaligned /does-handler + !does ;      r> cfaligned /does-handler + !does ; \ !! no gforth-native
   
   : (does>2)  ( addr -- )
       cfaligned /does-handler + !does ;
   
 : dodoes,  ( -- )  : dodoes,  ( -- )
   cfalign here /does-handler allot does-handler! ;    cfalign here /does-handler allot does-handler! ;
   
 : (compile) ( -- ) \ gforth  : (compile) ( -- ) \ gforth-obsolete: dummy
     r> dup cell+ >r @ compile, ;      true abort" (compile) doesn't work, use POSTPONE instead" ;
   
 \ \ ticks  \ \ ticks
   
Line 331 
Line 311 
     if      if
         drop compile,          drop compile,
     else      else
         dup ['] compile, =  
         if  
             drop POSTPONE (compile) a,  
         else  
             swap POSTPONE aliteral compile,              swap POSTPONE aliteral compile,
         then  
     then ;      then ;
   
 : POSTPONE ( "name" -- ) \ core  : POSTPONE ( "name" -- ) \ core
     \g Compiles the compilation semantics of @i{name}.      \g Compiles the compilation semantics of @i{name}.
     COMP' postpone, ; immediate restrict      COMP' postpone, ; immediate
   
 \ \ recurse                                                     17may93jaw  \ \ recurse                                                     17may93jaw
   
 : recurse ( compilation -- ; run-time ?? -- ?? ) \ core  : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
     \g Call the current definition.      \g Call the current definition.
     lastxt compile, ; immediate restrict      latestxt compile, ; immediate restrict
   
 \ \ compiler loop  \ \ compiler loop
   
Line 380 
Line 355 
   
 \ \ Strings                                                     22feb93py  \ \ Strings                                                     22feb93py
   
 : ," ( "string"<"> -- ) [char] " parse  : S, ( addr u -- )
       \ allot string as counted string
   here over char+ allot  place align ;    here over char+ allot  place align ;
   
 : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string  : mem, ( addr u -- )
     \G Compilation: compile the string specified by @i{c-addr1},      \ allot the memory block HERE (do alignment yourself)
     \G @i{u} into the current definition. Run-time: return      here over allot swap move ;
     \G @i{c-addr2 u} describing the address and length of the  
     \G string.  
     postpone (S") here over char+ allot  place align ;  
                                              immediate restrict  
   
 \ \ abort"                                                      22feb93py  
   
 : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote  : ," ( "string"<"> -- )
     \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},      [char] " parse s, ;
     \G displaying the string @i{ccc} if there is no exception frame on the  
     \G exception stack.  
     postpone (abort") ," ;        immediate restrict  
   
 \ \ Header states                                               23feb93py  \ \ Header states                                               23feb93py
   
Line 413 
Line 380 
 : lastflags ( -- c-addr )  : lastflags ( -- c-addr )
     \ the address of the flags byte in the last header      \ the address of the flags byte in the last header
     \ aborts if the last defined word was headerless      \ aborts if the last defined word was headerless
     last @ dup 0= abort" last word was headerless" cell+ ;      latest dup 0= abort" last word was headerless" cell+ ;
   
 : immediate ( -- ) \ core  : immediate ( -- ) \ core
     \G Make the compilation semantics of a word be to @code{execute}      \G Make the compilation semantics of a word be to @code{execute}
Line 519 
Line 486 
   
 \ IS Defer What's Defers TO                            24feb93py  \ IS Defer What's Defers TO                            24feb93py
   
   : defer-default ( -- ) \ gforth
       \ might change into a THROW in the future
       ; \ >stderr ." uninitialized deferred word" ;
   
 doer? :dodefer [IF]  doer? :dodefer [IF]
   
 : Defer ( "name" -- ) \ gforth  : Defer ( "name" -- ) \ gforth
     \ !! shouldn't it be initialized with abort or something similar?  \G Define a deferred word @i{name}; its execution semantics can be
   \G set with @code{defer!} or @code{is} (and they have to, before first
   \G executing @i{name}.
     Header Reveal dodefer: cfa,      Header Reveal dodefer: cfa,
     ['] noop A, ;      ['] defer-default A, ;
   
 [ELSE]  [ELSE]
   
 : Defer ( "name" -- ) \ gforth  : Defer ( "name" -- ) \ gforth
     Create ['] noop A,      Create ['] defer-default A,
 DOES> @ execute ;  DOES> @ execute ;
   
 [THEN]  [THEN]
   
   : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
   \G @i{xt} represents the word currently associated with the deferred
   \G word @i{xt-deferred}.
       >body @ ;
   
 : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth  : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
     \G Compiles the present contents of the deferred word @i{name}      \G Compiles the present contents of the deferred word @i{name}
     \G into the current definition.  I.e., this produces static      \G into the current definition.  I.e., this produces static
     \G binding as if @i{name} was not deferred.      \G binding as if @i{name} was not deferred.
     ' >body @ compile, ; immediate      ' defer@ compile, ; immediate
   
 :noname  :noname
     dodoes, here !does ]      dodoes, here !does ]
Line 546 
Line 524 
 :noname  :noname
     ;-hook ?struc      ;-hook ?struc
     [ has? xconds [IF] ] exit-like [ [THEN] ]      [ has? xconds [IF] ] exit-like [ [THEN] ]
     postpone (does>) dodoes,      here 5 cells + postpone aliteral postpone (does>2) [compile] exit
       finish-code dodoes,
     defstart :-hook ;      defstart :-hook ;
 interpret/compile: DOES>  ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core        does  interpret/compile: DOES>  ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core        does
   
   : defer! ( xt xt-deferred -- )
   \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
       >body ! ;
   
 : <IS> ( "name" xt -- ) \ gforth  : <IS> ( "name" xt -- ) \ gforth
     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.      \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
     ' >body ! ;      ' defer! ;
   
 : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is  : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
     \g At run-time, changes the @code{defer}red word @var{name} to      \g At run-time, changes the @code{defer}red word @var{name} to
     \g execute @var{xt}.      \g execute @var{xt}.
     ' >body postpone ALiteral postpone ! ; immediate restrict      ' postpone ALiteral postpone defer! ; immediate restrict
   
 ' <IS>  ' <IS>
 ' [IS]  ' [IS]
 interpret/compile: IS ( xt "name" -- ) \ gforth  interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
 \G A combined word made up from @code{<IS>} and @code{[IS]}.  \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
   \G Its compilation semantics parses at compile time.
   
 ' <IS>  ' <IS>
 ' [IS]  ' [IS]
 interpret/compile: TO ( w "name" -- ) \ core-ext  interpret/compile: TO ( w "name" -- ) \ core-ext
   
 :noname    ' >body @ ;  
 :noname    ' >body postpone ALiteral postpone @ ;  
 interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth  
 \G @i{Xt} is the XT that is currently assigned to @i{name}.  
   
 : interpret/compile? ( xt -- flag )  : interpret/compile? ( xt -- flag )
     >does-code ['] DOES> >does-code = ;      >does-code ['] DOES> >does-code = ;
   
Line 607 
Line 586 
     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict      ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
 [ELSE]  [ELSE]
 : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core     semicolon  : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core     semicolon
     ;-hook ?struc [compile] exit reveal postpone [ ; immediate restrict      ;-hook ?struc [compile] exit
       [ has? peephole [IF] ] finish-code [ [THEN] ]
       reveal postpone [ ; immediate restrict
 [THEN]  [THEN]
   
 \ \ Search list handling: reveal words, recursive               23feb93py  \ \ Search list handling: reveal words, recursive               23feb93py
   
 : last?   ( -- false / nfa nfa )  : last?   ( -- false / nfa nfa )
     last @ ?dup ;      latest ?dup ;
   
 : (reveal) ( nt wid -- )  : (reveal) ( nt wid -- )
     wordlist-id dup >r      wordlist-id dup >r


Generate output suitable for use with a patch program
Legend:
Removed from v.1.43  
changed lines
  Added in v.1.67

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help