File:  [gforth] / gforth / kernel / comp.fs
Revision 1.108: download - view: text, annotated - select for diffs
Thu Sep 22 10:05:11 2011 UTC (12 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Simple BUFFER: implementation

    1: \ compiler definitions						14sep97jaw
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation, either version 3
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program. If not, see http://www.gnu.org/licenses/.
   19: 
   20: \ \ Revisions-Log
   21: 
   22: \	put in seperate file				14sep97jaw	
   23: 
   24: \ \ here allot , c, A,						17dec92py
   25: 
   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
   36: : allot ( n -- ) \ core
   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.
   43:     here +
   44:     dup 1- usable-dictionary-end forthstart within -8 and throw
   45:     dp ! ;
   46: [THEN]
   47: 
   48: : c,    ( c -- ) \ core c-comma
   49:     \G Reserve data space for one char and store @i{c} in the space.
   50:     here 1 chars allot [ has? flash [IF] ] flashc! [ [ELSE] ] c! [ [THEN] ] ;
   51: 
   52: : ,     ( w -- ) \ core comma
   53:     \G Reserve data space for one cell and store @i{w} in the space.
   54:     here cell allot [ has? flash [IF] ] flash! [ [ELSE] ] ! [ [THEN] ] ;
   55: 
   56: : 2,	( w1 w2 -- ) \ gforth
   57:     \G Reserve data space for two cells and store the double @i{w1
   58:     \G w2} there, @i{w2} first (lower address).
   59:     here 2 cells allot  [ has? flash [IF] ] tuck flash! cell+ flash!
   60: 	[ [ELSE] ] 2! [ [THEN] ] ;
   61: 
   62: \ : aligned ( addr -- addr' ) \ core
   63: \     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
   64: 
   65: : align ( -- ) \ core
   66:     \G If the data-space pointer is not aligned, reserve enough space to align it.
   67:     here dup aligned swap ?DO  bl c,  LOOP ;
   68: 
   69: \ : faligned ( addr -- f-addr ) \ float f-aligned
   70: \     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ; 
   71: 
   72: has? ec 0= [IF]
   73: : falign ( -- ) \ float f-align
   74:     \G If the data-space pointer is not float-aligned, reserve
   75:     \G enough space to align it.
   76:     here dup faligned swap
   77:     ?DO
   78: 	bl c,
   79:     LOOP ;
   80: [THEN]
   81: 
   82: : maxalign ( -- ) \ gforth
   83:     \G Align data-space pointer for all alignment requirements.
   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
   91: \G Align data-space pointer for code field requirements (i.e., such
   92: \G that the corresponding body is maxaligned).
   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
  106:     dup [ has? rom [IF] ] $E0 [ [ELSE] ] alias-mask [ [THEN] ] or c,
  107: [ has? flash [IF] ]
  108:     bounds ?DO  I c@ c,  LOOP
  109: [ [ELSE] ]
  110:     here swap chars dup allot move
  111: [ [THEN] ] ;
  112: 
  113: : longstring, ( c-addr u -- ) \ gforth
  114:     \G puts down string as longcstring
  115:     dup , here swap chars dup allot move ;
  116: 
  117: [IFDEF] prelude-mask
  118: variable next-prelude
  119: 
  120: : prelude, ( -- )
  121:     next-prelude @ if
  122: 	align next-prelude @ ,
  123:     then ;
  124: [THEN]
  125: 
  126: : header, ( c-addr u -- ) \ gforth
  127:     name-too-long?
  128:     dup max-name-length @ max max-name-length !
  129:     [ [IFDEF] prelude-mask ] prelude, [ [THEN] ]
  130:     align here last !
  131: [ has? ec [IF] ]
  132:     -1 A,
  133: [ [ELSE] ]
  134:     current @ 1 or A,	\ link field; before revealing, it contains the
  135: 			\ tagged reveal-into wordlist
  136: [ [THEN] ]
  137: [ has? f83headerstring [IF] ]
  138: 	string,
  139: [ [ELSE] ]
  140: 	longstring, alias-mask lastflags cset
  141: 	next-prelude @ 0<> prelude-mask and lastflags cset
  142: 	next-prelude off
  143: [ [THEN] ]
  144:     cfalign ;
  145: 
  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: 
  154: : input-stream-header ( "name" -- )
  155:     parse-name name-too-short? header, ;
  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: 
  163: 2variable nextname-string
  164: 
  165: : nextname-header ( -- )
  166:     nextname-string 2@ header,
  167:     nextname-string free-mem-var
  168:     input-stream ;
  169: 
  170: \ the next name is given in the string
  171: 
  172: : nextname ( c-addr u -- ) \ gforth
  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.
  175:     name-too-long?
  176:     nextname-string free-mem-var
  177:     save-mem nextname-string 2!
  178:     ['] nextname-header IS (header) ;
  179: 
  180: : noname-header ( -- )
  181:     0 last ! cfalign
  182:     input-stream ;
  183: 
  184: : noname ( -- ) \ gforth
  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
  187:     \g be given by @code{latestxt}.
  188:     ['] noname-header IS (header) ;
  189: [THEN]
  190: 
  191: : latestxt ( -- xt ) \ gforth
  192:     \G @i{xt} is the execution token of the last word defined.
  193:     \ The main purpose of this word is to get the xt of words defined using noname
  194:     lastcfa @ ;
  195: 
  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: 
  204: \ \ literals							17dec92py
  205: 
  206: : Literal  ( compilation n -- ; run-time -- n ) \ core
  207:     \G Compilation semantics: compile the run-time semantics.@*
  208:     \G Run-time Semantics: push @i{n}.@*
  209:     \G Interpretation semantics: undefined.
  210: [ [IFDEF] lit, ]
  211:     lit,
  212: [ [ELSE] ]
  213:     postpone lit ,
  214: [ [THEN] ] ; immediate restrict
  215: 
  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: 
  221: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
  222: [ [IFDEF] alit, ]
  223:     alit,
  224: [ [ELSE] ]
  225:     postpone lit A, 
  226: [ [THEN] ] ; immediate restrict
  227: 
  228: Defer char@ ( addr u -- char addr' u' )
  229: :noname  over c@ -rot 1 /string ; IS char@
  230: 
  231: : char   ( '<spaces>ccc' -- c ) \ core
  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}.
  234:     parse-name char@ 2drop ;
  235: 
  236: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
  237:     \G Compilation: skip leading spaces. Parse the string
  238:     \G @i{ccc}. Run-time: return @i{c}, the display code
  239:     \G representing the first character of @i{ccc}.  Interpretation
  240:     \G semantics for this word are undefined.
  241:     char postpone Literal ; immediate restrict
  242: 
  243: \ \ threading							17mar93py
  244: 
  245: : cfa,     ( code-address -- )  \ gforth	cfa-comma
  246:     here
  247:     dup lastcfa !
  248:     [ has? rom [IF] ] 2 cells allot [ [ELSE] ] 0 A, 0 , [ [THEN] ]
  249:     code-address! ;
  250: 
  251: [IFUNDEF] compile,
  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,
  257: [THEN]
  258: 
  259: has? ec 0= [IF]
  260: defer basic-block-end ( -- )
  261: 
  262: :noname ( -- )
  263:     0 compile-prim1 ;
  264: is basic-block-end
  265: [THEN]
  266: 
  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]
  276: 
  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
  281:     dup >does-code if
  282: 	['] does-exec peephole-compile, , EXIT
  283: 	\ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
  284:     then
  285:     dup >code-address CASE
  286: 	dovalue: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
  287: 	docon:   OF >body @ ['] lit peephole-compile, , EXIT ENDOF
  288: 	\ docon:   OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
  289: 	\ docon is also used by VALUEs, so don't @ at compile time
  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
  295: 	\ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
  296: 	doabicode: OF >body ['] abi-call peephole-compile, , EXIT ENDOF
  297: 	do;abicode: OF ['] ;abi-code-exec peephole-compile, , EXIT ENDOF
  298: 	\ code words and ;code-defined words (code words could be
  299: 	\ optimized, if we could identify them):
  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
  306: 	    \ drop POSTPONE literal ['] execute peephole-compile, EXIT
  307: 	THEN
  308:     ENDCASE
  309:     peephole-compile, ;
  310: 
  311: ' compile-to-prims, IS compile,
  312: [ELSE]
  313: ' , is compile,
  314: [THEN]
  315: 
  316: : !does    ( addr -- ) \ gforth	store-does
  317:     latestxt does-code! ;
  318: 
  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 ;
  325: 
  326: : (compile) ( -- ) \ gforth-obsolete: dummy
  327:     true abort" (compile) doesn't work, use POSTPONE instead" ;
  328: 
  329: \ \ ticks
  330: 
  331: : name>comp ( nt -- w xt ) \ gforth name-to-comp
  332:     \G @i{w xt} is the compilation token for the word @i{nt}.
  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
  344:     \g @i{xt} represents @i{name}'s interpretation
  345:     \g semantics. Perform @code{-14 throw} if the word has no
  346:     \g interpretation semantics.
  347:     ' postpone ALiteral ; immediate restrict
  348: 
  349: : COMP'    ( "name" -- w xt ) \ gforth  comp-tick
  350:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
  351:     (') name>comp ;
  352: 
  353: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
  354:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
  355:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
  356: 
  357: : postpone, ( w xt -- ) \ gforth	postpone-comma
  358:     \g Compile the compilation semantics represented by the
  359:     \g compilation token @i{w xt}.
  360:     dup ['] execute =
  361:     if
  362: 	drop compile,
  363:     else
  364: 	swap POSTPONE aliteral compile,
  365:     then ;
  366: 
  367: : POSTPONE ( "name" -- ) \ core
  368:     \g Compiles the compilation semantics of @i{name}.
  369:     COMP' postpone, ; immediate
  370: 
  371: \ \ recurse							17may93jaw
  372: 
  373: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
  374:     \g Call the current definition.
  375:     latestxt compile, ; immediate restrict
  376: 
  377: \ \ compiler loop
  378: 
  379: : compiler1 ( c-addr u -- ... xt )
  380:     2dup find-name [ [IFDEF] prelude-mask ] run-prelude [ [THEN] ] dup
  381:     if ( c-addr u nt )
  382: 	nip nip name>comp
  383:     else
  384: 	drop
  385: 	2dup 2>r snumber? dup
  386: 	IF
  387: 	    0>
  388: 	    IF
  389: 		['] 2literal
  390: 	    ELSE
  391: 		['] literal
  392: 	    THEN
  393: 	    2rdrop
  394: 	ELSE
  395: 	    drop 2r> compiler-notfound1
  396: 	THEN
  397:     then ;
  398: 
  399: : [ ( -- ) \  core	left-bracket
  400:     \G Enter interpretation state. Immediate word.
  401:     ['] interpreter1  IS parser1 state off ; immediate
  402: 
  403: : ] ( -- ) \ core	right-bracket
  404:     \G Enter compilation state.
  405:     ['] compiler1     IS parser1 state on  ;
  406: 
  407: \ \ Strings							22feb93py
  408: 
  409: : S, ( addr u -- )
  410:     \ allot string as counted string
  411: [ has? flash [IF] ]
  412:     dup c, bounds ?DO  I c@ c,  LOOP
  413: [ [ELSE] ]
  414:     here over char+ allot  place align
  415: [ [THEN] ] ;
  416: 
  417: : mem, ( addr u -- )
  418:     \ allot the memory block HERE (do alignment yourself)
  419: [ has? flash [IF] ]
  420:     bounds ?DO  I c@ c,  LOOP
  421: [ [ELSE] ]
  422:     here over allot swap move
  423: [ [THEN] ] ;
  424: 
  425: : ," ( "string"<"> -- )
  426:     [char] " parse s, ;
  427: 
  428: \ \ Header states						23feb93py
  429: 
  430: \ problematic only for big endian machines
  431: 
  432: has? f83headerstring [IF]
  433: : cset ( bmask c-addr -- )
  434:     tuck c@ or swap c! ; 
  435: 
  436: : creset ( bmask c-addr -- )
  437:     tuck c@ swap invert and swap c! ; 
  438: 
  439: : ctoggle ( bmask c-addr -- )
  440:     tuck c@ xor swap c! ; 
  441: [ELSE]
  442: : cset ( bmask c-addr -- )
  443:     tuck @ or swap ! ; 
  444: 
  445: : creset ( bmask c-addr -- )
  446:     tuck @ swap invert and swap ! ; 
  447: 
  448: : ctoggle ( bmask c-addr -- )
  449:     tuck @ xor swap ! ; 
  450: [THEN]
  451: 
  452: : lastflags ( -- c-addr )
  453:     \ the address of the flags byte in the last header
  454:     \ aborts if the last defined word was headerless
  455:     latest dup 0= abort" last word was headerless" cell+ ;
  456: 
  457: : immediate ( -- ) \ core
  458:     \G Make the compilation semantics of a word be to @code{execute}
  459:     \G the execution semantics.
  460:     immediate-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
  461: 
  462: : restrict ( -- ) \ gforth
  463:     \G A synonym for @code{compile-only}
  464:     restrict-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
  465: 
  466: ' restrict alias compile-only ( -- ) \ gforth
  467: \G Remove the interpretation semantics of a word.
  468: 
  469: \ \ Create Variable User Constant                        	17mar93py
  470: 
  471: : Alias    ( xt "name" -- ) \ gforth
  472:     Header reveal
  473:     alias-mask lastflags creset
  474:     dup A, lastcfa ! ;
  475: 
  476: doer? :dovar [IF]
  477: 
  478: : Create ( "name" -- ) \ core
  479:     Header reveal dovar: cfa, ;
  480: [ELSE]
  481: 
  482: : Create ( "name" -- ) \ core
  483:     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
  484: [THEN]
  485: 
  486: : buffer: ( u "name" -- ) \ core ext
  487:     Create allot ;
  488: 
  489: has? flash [IF]
  490:     : (variable) dpp @ normal-dp = IF  Create dpp @
  491: 	ELSE  normal-dp @ Constant dpp @ ram  THEN ;
  492: : Variable ( "name" -- ) \ core
  493:     (Variable) 0 , dpp ! ;
  494: 
  495: : AVariable ( "name" -- ) \ gforth
  496:     (Variable) 0 A, dpp ! ;
  497: 
  498: : 2Variable ( "name" -- ) \ double two-variable
  499:     (Variable) 0 , 0 , dpp ! ;
  500: [ELSE]
  501: : Variable ( "name" -- ) \ core
  502:     Create 0 , ;
  503: 
  504: : AVariable ( "name" -- ) \ gforth
  505:     Create 0 A, ;
  506: 
  507: : 2Variable ( "name" -- ) \ double two-variable
  508:     Create 0 , 0 , ;
  509: [THEN]
  510: 
  511: has? no-userspace 0= [IF]
  512: : uallot ( n -- ) \ gforth
  513:     udp @ swap udp +! ;
  514: 
  515: doer? :douser [IF]
  516: 
  517: : User ( "name" -- ) \ gforth
  518:     Header reveal douser: cfa, cell uallot , ;
  519: 
  520: : AUser ( "name" -- ) \ gforth
  521:     User ;
  522: [ELSE]
  523: 
  524: : User Create cell uallot , DOES> @ up @ + ;
  525: 
  526: : AUser User ;
  527: [THEN]
  528: [THEN]
  529: 
  530: doer? :docon [IF]
  531:     : (Constant)  Header reveal docon: cfa, ;
  532: [ELSE]
  533:     : (Constant)  Create DOES> @ ;
  534: [THEN]
  535: 
  536: doer? :dovalue [IF]
  537:     : (Value)  Header reveal dovalue: cfa, ;
  538: [ELSE]
  539:     has? rom [IF]
  540: 	: (Value)  Create DOES> @ @ ;
  541:     [ELSE]
  542: 	: (Value)  Create DOES> @ ;
  543:     [THEN]
  544: [THEN]
  545: 
  546: : Constant ( w "name" -- ) \ core
  547:     \G Define a constant @i{name} with value @i{w}.
  548:     \G  
  549:     \G @i{name} execution: @i{-- w}
  550:     (Constant) , ;
  551: 
  552: : AConstant ( addr "name" -- ) \ gforth
  553:     (Constant) A, ;
  554: 
  555: has? flash [IF]
  556: : Value ( w "name" -- ) \ core-ext
  557:     (Value) dpp @ >r here cell allot >r
  558:     ram here >r , r> r> flash! r> dpp ! ;
  559: 
  560: ' Value alias AValue
  561: [ELSE]
  562: : Value ( w "name" -- ) \ core-ext
  563:     (Value) , ;
  564: 
  565: : AValue ( w "name" -- ) \ core-ext
  566:     (Value) A, ;
  567: [THEN]
  568: 
  569: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
  570:     Create ( w1 w2 "name" -- )
  571:         2,
  572:     DOES> ( -- w1 w2 )
  573:         2@ ;
  574:     
  575: doer? :dofield [IF]
  576:     : (Field)  Header reveal dofield: cfa, ;
  577: [ELSE]
  578:     : (Field)  Create DOES> @ + ;
  579: [THEN]
  580: 
  581: \ \ interpret/compile:
  582: 
  583: struct
  584:     >body
  585:     cell% field interpret/compile-int
  586:     cell% field interpret/compile-comp
  587: end-struct interpret/compile-struct
  588: 
  589: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
  590:     Create immediate swap A, A,
  591: DOES>
  592:     abort" executed primary cfa of an interpret/compile: word" ;
  593: \    state @ IF  cell+  THEN  perform ;
  594: 
  595: \ IS Defer What's Defers TO                            24feb93py
  596: 
  597: defer defer-default ( -- )
  598: ' abort is defer-default
  599: \ default action for deferred words (overridden by a warning later)
  600:     
  601: doer? :dodefer [IF]
  602: 
  603: : Defer ( "name" -- ) \ gforth
  604: \G Define a deferred word @i{name}; its execution semantics can be
  605: \G set with @code{defer!} or @code{is} (and they have to, before first
  606: \G executing @i{name}.
  607:     Header Reveal dodefer: cfa,
  608:     [ has? rom [IF] ] here >r cell allot
  609:     dpp @ ram here r> flash! ['] defer-default A, dpp !
  610:     [ [ELSE] ] ['] defer-default A, [ [THEN] ] ;
  611: 
  612: [ELSE]
  613: 
  614:     has? rom [IF]
  615: 	: Defer ( "name" -- ) \ gforth
  616: 	    Create here >r cell allot
  617: 	    dpp @ ram here r> flash! ['] defer-default A, dpp !
  618: 	  DOES> @ @ execute ;
  619:     [ELSE]
  620: 	: Defer ( "name" -- ) \ gforth
  621: 	    Create ['] defer-default A,
  622: 	  DOES> @ execute ;
  623:     [THEN]
  624: [THEN]
  625: 
  626: : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
  627: \G @i{xt} represents the word currently associated with the deferred
  628: \G word @i{xt-deferred}.
  629:     >body @ [ has? rom [IF] ] @ [ [THEN] ] ;
  630: 
  631: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
  632:     \G Compiles the present contents of the deferred word @i{name}
  633:     \G into the current definition.  I.e., this produces static
  634:     \G binding as if @i{name} was not deferred.
  635:     ' defer@ compile, ; immediate
  636: 
  637: : does>-like ( xt -- )
  638:     \ xt ( addr -- ) is !does or !;abi-code etc, addr is the address
  639:     \ that should be stored right after the code address.
  640:     >r ;-hook ?struc
  641:     [ has? xconds [IF] ] exit-like [ [THEN] ]
  642:     here [ has? peephole [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] cells +
  643:     postpone aliteral r> compile, [compile] exit
  644:     [ has? peephole [IF] ] finish-code [ [THEN] ]
  645:     defstart ;
  646: 
  647: :noname
  648:     here !does ]
  649:     defstart :-hook ;
  650: :noname
  651:     ['] !does does>-like :-hook ;
  652: interpret/compile: DOES>  ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core        does
  653: 
  654: : defer! ( xt xt-deferred -- ) \ gforth  defer-store
  655: \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
  656:     >body [ has? rom [IF] ] @ [ [THEN] ] ! ;
  657:     
  658: : <IS> ( "name" xt -- ) \ gforth
  659:     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
  660:     ' defer! ;
  661: 
  662: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
  663:     \g At run-time, changes the @code{defer}red word @var{name} to
  664:     \g execute @var{xt}.
  665:     ' postpone ALiteral postpone defer! ; immediate restrict
  666: 
  667: ' <IS>
  668: ' [IS]
  669: interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
  670: \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
  671: \G Its compilation semantics parses at compile time.
  672: 
  673: ' <IS>
  674: ' [IS]
  675: interpret/compile: TO ( w "name" -- ) \ core-ext
  676: 
  677: : interpret/compile? ( xt -- flag )
  678:     >does-code ['] DOES> >does-code = ;
  679: 
  680: \ \ : ;                                                  	24feb93py
  681: 
  682: defer :-hook ( sys1 -- sys2 )
  683: 
  684: defer ;-hook ( sys2 -- sys1 )
  685: 
  686: 0 Constant defstart
  687: 
  688: [IFDEF] docol,
  689: : (:noname) ( -- colon-sys )
  690:     \ common factor of : and :noname
  691:     docol, ]comp
  692: [ELSE]
  693: : (:noname) ( -- colon-sys )
  694:     \ common factor of : and :noname
  695:     docol: cfa,
  696: [THEN]
  697:     defstart ] :-hook ;
  698: 
  699: : : ( "name" -- colon-sys ) \ core	colon
  700:     Header (:noname) ;
  701: 
  702: : :noname ( -- xt colon-sys ) \ core-ext	colon-no-name
  703:     0 last !
  704:     cfalign here (:noname) ;
  705: 
  706: [IFDEF] fini,
  707: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core   semicolon
  708:     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
  709: [ELSE]
  710: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core	semicolon
  711:     ;-hook ?struc [compile] exit
  712:     [ has? peephole [IF] ] finish-code [ [THEN] ]
  713:     reveal postpone [ ; immediate restrict
  714: [THEN]
  715: 
  716: \ \ Search list handling: reveal words, recursive		23feb93py
  717: 
  718: : last?   ( -- false / nfa nfa )
  719:     latest ?dup ;
  720: 
  721: Variable warnings ( -- addr ) \ gforth
  722: G -1 warnings T !
  723: 
  724: has? ec [IF]
  725: : reveal ( -- ) \ gforth
  726:     last?
  727:     if \ the last word has a header
  728: 	dup ( name>link ) @ -1 =
  729: 	if \ it is still hidden
  730: 	    forth-wordlist dup >r @ over
  731: 	    [ has? flash [IF] ] flash! [ [ELSE] ] ! [  [THEN] ] r> !
  732: 	else
  733: 	    drop
  734: 	then
  735:     then ;
  736: [ELSE]
  737: : (reveal) ( nt wid -- )
  738:     wordlist-id dup >r
  739:     @ over ( name>link ) ! 
  740:     r> ! ;
  741: 
  742: \ make entry in wordlist-map
  743: ' (reveal) f83search reveal-method !
  744: 
  745: : check-shadow  ( addr count wid -- )
  746:     \G prints a warning if the string is already present in the wordlist
  747:     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
  748: 	>stderr
  749: 	." redefined " name>string 2dup type
  750: 	str= 0= if
  751: 	    ."  with " type
  752: 	else
  753: 	    2drop
  754: 	then
  755: 	space space EXIT
  756:     then
  757:     2drop 2drop ;
  758: 
  759: : reveal ( -- ) \ gforth
  760:     last?
  761:     if \ the last word has a header
  762: 	dup ( name>link ) @ 1 and
  763: 	if \ it is still hidden
  764: 	    dup ( name>link ) @ 1 xor		( nt wid )
  765: 	    2dup >r name>string r> check-shadow ( nt wid )
  766: 	    dup wordlist-map @ reveal-method perform
  767: 	else
  768: 	    drop
  769: 	then
  770:     then ;
  771: 
  772: : rehash  ( wid -- )
  773:     dup wordlist-map @ rehash-method perform ;
  774: [THEN]
  775: 
  776: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
  777: \g Make the current definition visible, enabling it to call itself
  778: \g recursively.
  779: 	immediate restrict

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