File:  [gforth] / gforth / kernel / comp.fs
Revision 1.29: download - view: text, annotated - select for diffs
Sat Sep 23 15:47:09 2000 UTC (20 years, 10 months ago) by anton
Branches: MAIN
CVS tags: v0-5-0, HEAD
changed FSF address in copyright messages

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

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