File:  [gforth] / gforth / kernel / comp.fs
Revision 1.31: download - view: text, annotated - select for diffs
Sun Jan 28 17:39:33 2001 UTC (23 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
SEE bugfix (coming from long names)
NEXTNAME bugfix (but still length restrictions)

    1: \ compiler definitions						14sep97jaw
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000 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 2
   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, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: \ \ Revisions-Log
   22: 
   23: \	put in seperate file				14sep97jaw	
   24: 
   25: \ \ here allot , c, A,						17dec92py
   26: 
   27: [IFUNDEF] allot
   28: [IFUNDEF] forthstart
   29: : allot ( n -- ) \ core
   30:     dup unused u> -8 and throw
   31:     dp +! ;
   32: [THEN]
   33: [THEN]
   34: 
   35: \ we default to this version if we have nothing else 05May99jaw
   36: [IFUNDEF] allot
   37: : allot ( n -- ) \ core
   38:     \G Reserve @i{n} address units of data space without
   39:     \G initialization. @i{n} is a signed number, passing a negative
   40:     \G @i{n} releases memory.  In ANS Forth you can only deallocate
   41:     \G memory from the current contiguous region in this way.  In
   42:     \G Gforth you can deallocate anything in this way but named words.
   43:     \G The system does not check this restriction.
   44:     here +
   45:     dup 1- usable-dictionary-end forthstart within -8 and throw
   46:     dp ! ;
   47: [THEN]
   48: 
   49: : c,    ( c -- ) \ core c-comma
   50:     \G Reserve data space for one char and store @i{c} in the space.
   51:     here 1 chars allot c! ;
   52: 
   53: : ,     ( w -- ) \ core comma
   54:     \G Reserve data space for one cell and store @i{w} in the space.
   55:     here cell allot  ! ;
   56: 
   57: : 2,	( w1 w2 -- ) \ gforth
   58:     \G Reserve data space for two cells and store the double @i{w1
   59:     \G w2} there, @i{w2} first (lower address).
   60:     here 2 cells allot 2! ;
   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: : falign ( -- ) \ float f-align
   73:     \G If the data-space pointer is not float-aligned, reserve
   74:     \G enough space to align it.
   75:     here dup faligned swap
   76:     ?DO
   77: 	bl c,
   78:     LOOP ;
   79: 
   80: : maxalign ( -- ) \ gforth
   81:     \G Align data-space pointer for all alignment requirements.
   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
   89: \G Align data-space pointer for code field requirements (i.e., such
   90: \G that the corresponding body is maxaligned).
   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
  108:     dup c, here swap chars dup allot move ;
  109: 
  110: : longstring, ( c-addr u -- ) \ gforth
  111:     \G puts down string as cstring
  112:     dup , here swap chars dup allot move ;
  113: 
  114: : header, ( c-addr u -- ) \ gforth
  115:     name-too-long?
  116:     align here last !
  117:     current @ 1 or A,	\ link field; before revealing, it contains the
  118: 			\ tagged reveal-into wordlist
  119:     longstring, cfalign
  120:     alias-mask lastflags cset ;
  121: 
  122: : input-stream-header ( "name" -- )
  123:     name name-too-short? header, ;
  124: 
  125: : input-stream ( -- )  \ general
  126:     \G switches back to getting the name from the input stream ;
  127:     ['] input-stream-header IS (header) ;
  128: 
  129: ' input-stream-header IS (header)
  130: 
  131: \ !! make that a 2variable
  132: create nextname-buffer 32 chars allot
  133: 
  134: : nextname-header ( -- )
  135:     nextname-buffer count header,
  136:     input-stream ;
  137: 
  138: \ the next name is given in the string
  139: 
  140: : nextname ( c-addr u -- ) \ gforth
  141:     \g The next defined word will have the name @var{c-addr u}; the
  142:     \g defining word will leave the input stream alone.
  143:     dup &31 u> -&19 and throw \ !! make buffer variable-sized
  144:     name-too-long?
  145:     nextname-buffer c! ( c-addr )
  146:     nextname-buffer count move
  147:     ['] nextname-header IS (header) ;
  148: 
  149: : noname-header ( -- )
  150:     0 last ! cfalign
  151:     input-stream ;
  152: 
  153: : noname ( -- ) \ gforth
  154:     \g The next defined word will be anonymous. The defining word will
  155:     \g leave the input stream alone. The xt of the defined word will
  156:     \g be given by @code{lastxt}.
  157:     ['] noname-header IS (header) ;
  158: 
  159: : lastxt ( -- xt ) \ gforth
  160:     \G @i{xt} is the execution token of the last word defined.
  161:     \ The main purpose of this word is to get the xt of words defined using noname
  162:     lastcfa @ ;
  163: 
  164: \ \ literals							17dec92py
  165: 
  166: : Literal  ( compilation n -- ; run-time -- n ) \ core
  167:     \G Compilation semantics: compile the run-time semantics.@*
  168:     \G Run-time Semantics: push @i{n}.@*
  169:     \G Interpretation semantics: undefined.
  170: [ [IFDEF] lit, ]
  171:     lit,
  172: [ [ELSE] ]
  173:     postpone lit ,
  174: [ [THEN] ] ; immediate restrict
  175: 
  176: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
  177: [ [IFDEF] alit, ]
  178:     alit,
  179: [ [ELSE] ]
  180:     postpone lit A, 
  181: [ [THEN] ] ; immediate restrict
  182: 
  183: : char   ( '<spaces>ccc' -- c ) \ core
  184:     \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
  185:     \G display code representing the first character of @i{ccc}.
  186:     bl word char+ c@ ;
  187: 
  188: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
  189:     \G Compilation: skip leading spaces. Parse the string
  190:     \G @i{ccc}. Run-time: return @i{c}, the display code
  191:     \G representing the first character of @i{ccc}.  Interpretation
  192:     \G semantics for this word are undefined.
  193:     char postpone Literal ; immediate restrict
  194: 
  195: \ \ threading							17mar93py
  196: 
  197: : cfa,     ( code-address -- )  \ gforth	cfa-comma
  198:     here
  199:     dup lastcfa !
  200:     0 A, 0 ,  code-address! ;
  201: 
  202: [IFUNDEF] compile,
  203: : compile, ( xt -- )	\ core-ext	compile-comma
  204:     \G  Compile the word represented by the execution token @i{xt}
  205:     \G  into the current definition.
  206:     A, ;
  207: [THEN]
  208: 
  209: : !does    ( addr -- ) \ gforth	store-does
  210:     lastxt does-code! ;
  211: 
  212: : (does>)  ( R: addr -- )
  213:     r> cfaligned /does-handler + !does ;
  214: 
  215: : dodoes,  ( -- )
  216:   cfalign here /does-handler allot does-handler! ;
  217: 
  218: : (compile) ( -- ) \ gforth
  219:     r> dup cell+ >r @ compile, ;
  220: 
  221: \ \ ticks
  222: 
  223: : name>comp ( nt -- w xt ) \ gforth
  224:     \G @i{w xt} is the compilation token for the word @i{nt}.
  225:     (name>comp)
  226:     1 = if
  227:         ['] execute
  228:     else
  229:         ['] compile,
  230:     then ;
  231: 
  232: : [(')]  ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
  233:     (') postpone ALiteral ; immediate restrict
  234: 
  235: : [']  ( compilation. "name" -- ; run-time. -- xt ) \ core      bracket-tick
  236:     \g @i{xt} represents @i{name}'s interpretation
  237:     \g semantics. Perform @code{-14 throw} if the word has no
  238:     \g interpretation semantics.
  239:     ' postpone ALiteral ; immediate restrict
  240: 
  241: : COMP'    ( "name" -- w xt ) \ gforth  comp-tick
  242:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
  243:     (') name>comp ;
  244: 
  245: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
  246:     \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
  247:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
  248: 
  249: : postpone, ( w xt -- ) \ gforth	postpone-comma
  250:     \g Compile the compilation semantics represented by the
  251:     \g compilation token @i{w xt}.
  252:     dup ['] execute =
  253:     if
  254: 	drop compile,
  255:     else
  256: 	dup ['] compile, =
  257: 	if
  258: 	    drop POSTPONE (compile) a,
  259: 	else
  260: 	    swap POSTPONE aliteral compile,
  261: 	then
  262:     then ;
  263: 
  264: : POSTPONE ( "name" -- ) \ core
  265:     \g Compiles the compilation semantics of @i{name}.
  266:     COMP' postpone, ; immediate restrict
  267: 
  268: \ \ recurse							17may93jaw
  269: 
  270: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
  271:     \g Call the current definition.
  272:     lastxt compile, ; immediate restrict
  273: 
  274: \ \ compiler loop
  275: 
  276: : compiler ( c-addr u -- )
  277:     2dup find-name dup
  278:     if ( c-addr u nt )
  279: 	nip nip name>comp execute
  280:     else
  281: 	drop
  282: 	2dup snumber? dup
  283: 	IF
  284: 	    0>
  285: 	    IF
  286: 		swap postpone Literal
  287: 	    THEN
  288: 	    postpone Literal
  289: 	    2drop
  290: 	ELSE
  291: 	    drop compiler-notfound
  292: 	THEN
  293:     then ;
  294: 
  295: : [ ( -- ) \  core	left-bracket
  296:     \G Enter interpretation state. Immediate word.
  297:     ['] interpreter  IS parser state off ; immediate
  298: 
  299: : ] ( -- ) \ core	right-bracket
  300:     \G Enter compilation state.
  301:     ['] compiler     IS parser state on  ;
  302: 
  303: \ \ Strings							22feb93py
  304: 
  305: : ," ( "string"<"> -- ) [char] " parse
  306:   here over char+ allot  place align ;
  307: 
  308: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
  309:     \G Compilation: compile the string specified by @i{c-addr1},
  310:     \G @i{u} into the current definition. Run-time: return
  311:     \G @i{c-addr2 u} describing the address and length of the
  312:     \G string.
  313:     postpone (S") here over char+ allot  place align ;
  314:                                              immediate restrict
  315: 
  316: \ \ abort"							22feb93py
  317: 
  318: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext	abort-quote
  319:     \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
  320:     \G displaying the string @i{ccc} if there is no exception frame on the
  321:     \G exception stack.
  322:     postpone (abort") ," ;        immediate restrict
  323: 
  324: \ \ Header states						23feb93py
  325: 
  326: : cset ( bmask c-addr -- )
  327:     tuck @ or swap ! ; 
  328: 
  329: : creset ( bmask c-addr -- )
  330:     tuck @ swap invert and swap ! ; 
  331: 
  332: : ctoggle ( bmask c-addr -- )
  333:     tuck @ xor swap ! ; 
  334: 
  335: : lastflags ( -- c-addr )
  336:     \ the address of the flags byte in the last header
  337:     \ aborts if the last defined word was headerless
  338:     last @ dup 0= abort" last word was headerless" cell+ ;
  339: 
  340: : immediate ( -- ) \ core
  341:     \G Make the compilation semantics of a word be to @code{execute}
  342:     \G the execution semantics.
  343:     immediate-mask lastflags cset ;
  344: 
  345: : restrict ( -- ) \ gforth
  346:     \G A synonym for @code{compile-only}
  347:     restrict-mask lastflags cset ;
  348: 
  349: ' restrict alias compile-only ( -- ) \ gforth
  350: \G Remove the interpretation semantics of a word.
  351: 
  352: \ \ Create Variable User Constant                        	17mar93py
  353: 
  354: : Alias    ( xt "name" -- ) \ gforth
  355:     Header reveal
  356:     alias-mask lastflags creset
  357:     dup A, lastcfa ! ;
  358: 
  359: doer? :dovar [IF]
  360: 
  361: : Create ( "name" -- ) \ core
  362:     Header reveal dovar: cfa, ;
  363: [ELSE]
  364: 
  365: : Create ( "name" -- ) \ core
  366:     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
  367: [THEN]
  368: 
  369: : Variable ( "name" -- ) \ core
  370:     Create 0 , ;
  371: 
  372: : AVariable ( "name" -- ) \ gforth
  373:     Create 0 A, ;
  374: 
  375: : 2Variable ( "name" -- ) \ double two-variable
  376:     create 0 , 0 , ;
  377: 
  378: : uallot ( n -- ) \ gforth
  379:     udp @ swap udp +! ;
  380: 
  381: doer? :douser [IF]
  382: 
  383: : User ( "name" -- ) \ gforth
  384:     Header reveal douser: cfa, cell uallot , ;
  385: 
  386: : AUser ( "name" -- ) \ gforth
  387:     User ;
  388: [ELSE]
  389: 
  390: : User Create cell uallot , DOES> @ up @ + ;
  391: 
  392: : AUser User ;
  393: [THEN]
  394: 
  395: doer? :docon [IF]
  396:     : (Constant)  Header reveal docon: cfa, ;
  397: [ELSE]
  398:     : (Constant)  Create DOES> @ ;
  399: [THEN]
  400: 
  401: : Constant ( w "name" -- ) \ core
  402:     \G Define a constant @i{name} with value @i{w}.
  403:     \G  
  404:     \G @i{name} execution: @i{-- w}
  405:     (Constant) , ;
  406: 
  407: : AConstant ( addr "name" -- ) \ gforth
  408:     (Constant) A, ;
  409: 
  410: : Value ( w "name" -- ) \ core-ext
  411:     (Constant) , ;
  412: 
  413: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
  414:     Create ( w1 w2 "name" -- )
  415:         2,
  416:     DOES> ( -- w1 w2 )
  417:         2@ ;
  418:     
  419: doer? :dofield [IF]
  420:     : (Field)  Header reveal dofield: cfa, ;
  421: [ELSE]
  422:     : (Field)  Create DOES> @ + ;
  423: [THEN]
  424: \ IS Defer What's Defers TO                            24feb93py
  425: 
  426: doer? :dodefer [IF]
  427: 
  428: : Defer ( "name" -- ) \ gforth
  429:     \ !! shouldn't it be initialized with abort or something similar?
  430:     Header Reveal dodefer: cfa,
  431:     ['] noop A, ;
  432: 
  433: [ELSE]
  434: 
  435: : Defer ( "name" -- ) \ gforth
  436:     Create ['] noop A,
  437: DOES> @ execute ;
  438: 
  439: [THEN]
  440: 
  441: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
  442:     \G Compiles the present contents of the deferred word @i{name}
  443:     \G into the current definition.  I.e., this produces static
  444:     \G binding as if @i{name} was not deferred.
  445:     ' >body @ compile, ; immediate
  446: 
  447: :noname
  448:     dodoes, here !does ]
  449:     defstart :-hook ;
  450: :noname
  451:     ;-hook ?struc
  452:     [ has? xconds [IF] ] exit-like [ [THEN] ]
  453:     postpone (does>) dodoes,
  454:     defstart :-hook ;
  455: interpret/compile: DOES>  ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core        does
  456: 
  457: : <IS> ( "name" xt -- ) \ gforth
  458:     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
  459:     ' >body ! ;
  460: 
  461: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
  462:     \g At run-time, changes the @code{defer}red word @var{name} to
  463:     \g execute @var{xt}.
  464:     ' >body postpone ALiteral postpone ! ; immediate restrict
  465: 
  466: ' <IS>
  467: ' [IS]
  468: interpret/compile: IS ( xt "name" -- ) \ gforth
  469: \G A combined word made up from @code{<IS>} and @code{[IS]}.
  470: 
  471: ' <IS>
  472: ' [IS]
  473: interpret/compile: TO ( w "name" -- ) \ core-ext
  474: 
  475: :noname    ' >body @ ;
  476: :noname    ' >body postpone ALiteral postpone @ ;
  477: interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
  478: \G @i{Xt} is the XT that is currently assigned to @i{name}.
  479: 
  480: \ \ interpret/compile:
  481: 
  482: struct
  483:     >body
  484:     cell% field interpret/compile-int
  485:     cell% field interpret/compile-comp
  486: end-struct interpret/compile-struct
  487: 
  488: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
  489:     Create immediate swap A, A,
  490: DOES>
  491:     abort" executed primary cfa of an interpret/compile: word" ;
  492: \    state @ IF  cell+  THEN  perform ;
  493: 
  494: : interpret/compile? ( xt -- flag )
  495:     >does-code ['] DOES> >does-code = ;
  496: 
  497: \ \ : ;                                                  	24feb93py
  498: 
  499: defer :-hook ( sys1 -- sys2 )
  500: 
  501: defer ;-hook ( sys2 -- sys1 )
  502: 
  503: 0 Constant defstart
  504: 
  505: [IFDEF] docol,
  506: : (:noname) ( -- colon-sys )
  507:     \ common factor of : and :noname
  508:     docol, ]comp defstart ] :-hook ;
  509: [ELSE]
  510: : (:noname) ( -- colon-sys )
  511:     \ common factor of : and :noname
  512:     docol: cfa, defstart ] :-hook ;
  513: [THEN]
  514: 
  515: : : ( "name" -- colon-sys ) \ core	colon
  516:     Header (:noname) ;
  517: 
  518: : :noname ( -- xt colon-sys ) \ core-ext	colon-no-name
  519:     0 last !
  520:     cfalign here (:noname) ;
  521: 
  522: [IFDEF] fini,
  523: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core   semicolon
  524:     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
  525: [ELSE]
  526: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core	semicolon
  527:     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
  528: [THEN]
  529: 
  530: \ \ Search list handling: reveal words, recursive		23feb93py
  531: 
  532: : last?   ( -- false / nfa nfa )
  533:     last @ ?dup ;
  534: 
  535: : (reveal) ( nt wid -- )
  536:     wordlist-id dup >r
  537:     @ over ( name>link ) ! 
  538:     r> ! ;
  539: 
  540: \ make entry in wordlist-map
  541: ' (reveal) f83search reveal-method !
  542: 
  543: Variable warnings ( -- addr ) \ gforth
  544: G -1 warnings T !
  545: 
  546: : check-shadow  ( addr count wid -- )
  547:     \G prints a warning if the string is already present in the wordlist
  548:     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
  549: 	>stderr
  550: 	." redefined " name>string 2dup type
  551: 	compare 0<> if
  552: 	    ."  with " type
  553: 	else
  554: 	    2drop
  555: 	then
  556: 	space space EXIT
  557:     then
  558:     2drop 2drop ;
  559: 
  560: : reveal ( -- ) \ gforth
  561:     last?
  562:     if \ the last word has a header
  563: 	dup ( name>link ) @ 1 and
  564: 	if \ it is still hidden
  565: 	    dup ( name>link ) @ 1 xor		( nt wid )
  566: 	    2dup >r name>string r> check-shadow ( nt wid )
  567: 	    dup wordlist-map @ reveal-method perform
  568: 	else
  569: 	    drop
  570: 	then
  571:     then ;
  572: 
  573: : rehash  ( wid -- )
  574:     dup wordlist-map @ rehash-method perform ;
  575: 
  576: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
  577: \g Make the current definition visible, enabling it to call itself
  578: \g recursively.
  579: 	immediate restrict

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