File:  [gforth] / gforth / cross.fs
Revision 1.38: download - view: text, annotated - select for diffs
Wed Aug 21 14:58:38 1996 UTC (27 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
a little cleanup in 386.h
renamed special: to interpret/compile:
renamed save-string to save-mem
added extend-mem
replaced (name>) with ((name>))
replaced name> with name>int and name>comp
renamed compile-only to compile-only-error
replaced xt>i with name>int
replaced xt>c with name>comp
removed xt>s
removed found
search-wordlist now delivers interpretation-xt
replaced (sfind) with find-name
replaced C' with COMP' and [C'] with [COMP']
removed S' and [S']
added hex.
added some helper words
adapted other words to the changes
started documenting the intergration of Gforth in applications

    1: \ CROSS.FS     The Cross-Compiler                      06oct92py
    2: \ Idea and implementation: Bernd Paysan (py)
    3: 
    4: \ Copyright (C) 1995 Free Software Foundation, Inc.
    5: 
    6: \ This file is part of Gforth.
    7: 
    8: \ Gforth is free software; you can redistribute it and/or
    9: \ modify it under the terms of the GNU General Public License
   10: \ as published by the Free Software Foundation; either version 2
   11: \ of the License, or (at your option) any later version.
   12: 
   13: \ This program is distributed in the hope that it will be useful,
   14: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: \ GNU General Public License for more details.
   17: 
   18: \ You should have received a copy of the GNU General Public License
   19: \ along with this program; if not, write to the Free Software
   20: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   21: 
   22: \ Log:
   23: \       changed in ; [ to state off           12may93jaw
   24: \       included place +place                 12may93jaw
   25: \       for a created word (variable, constant...)
   26: \       is now an alias in the target voabulary.
   27: \       this means it is no longer necessary to
   28: \       switch between vocabularies for variable
   29: \       initialization                        12may93jaw
   30: \       discovered error in DOES>
   31: \       replaced !does with (;code)           16may93jaw
   32: \       made complete redesign and
   33: \       introduced two vocs method
   34: \       to be asure that the right words
   35: \       are found                             08jun93jaw
   36: \       btw:  ! works not with 16 bit
   37: \             targets                         09jun93jaw
   38: \       added: 2user and value                11jun93jaw
   39: 
   40: \ include other.fs       \ ansforth extentions for cross
   41: 
   42: : string, ( c-addr u -- )
   43:     \ puts down string as cstring
   44:     dup c, here swap chars dup allot move ;
   45: ' falign Alias cfalign
   46: : comment? ( c-addr u -- c-addr u )
   47:         2dup s" (" compare 0=
   48:         IF    postpone (
   49:         ELSE  2dup s" \" compare 0= IF postpone \ THEN
   50:         THEN ;
   51: 
   52: decimal
   53: 
   54: \ Begin CROSS COMPILER:
   55: 
   56: \ GhostNames                                            9may93jaw
   57: \ second name source to search trough list
   58: 
   59: VARIABLE GhostNames
   60: 0 GhostNames !
   61: : GhostName ( -- addr )
   62:     here GhostNames @ , GhostNames ! here 0 ,
   63:     bl word count
   64:     \ 2dup type space
   65:     string, cfalign ;
   66: 
   67: hex
   68: 
   69: 
   70: Vocabulary Cross
   71: Vocabulary Target
   72: Vocabulary Ghosts
   73: VOCABULARY Minimal
   74: only Forth also Target also also
   75: definitions Forth
   76: 
   77: : T  previous Cross also Target ; immediate
   78: : G  Ghosts ; immediate
   79: : H  previous Forth also Cross ; immediate
   80: 
   81: forth definitions
   82: 
   83: : T  previous Cross also Target ; immediate
   84: : G  Ghosts ; immediate
   85: 
   86: : >cross  also Cross definitions previous ;
   87: : >target also Target definitions previous ;
   88: : >minimal also Minimal definitions previous ;
   89: 
   90: H
   91: 
   92: >CROSS
   93: 
   94: \ Variables                                            06oct92py
   95: 
   96: -1 Constant NIL
   97: Variable image
   98: Variable tlast    NIL tlast !  \ Last name field
   99: Variable tlastcfa \ Last code field
  100: Variable tdoes    \ Resolve does> calls
  101: Variable bit$
  102: Variable tdp
  103: : there  tdp @ ;
  104: 
  105: \ Parameter for target systems                         06oct92py
  106: 
  107: included
  108: 
  109: \ Create additional parameters                         19jan95py
  110: 
  111: T
  112: cell               Constant tcell
  113: cell<<             Constant tcell<<
  114: cell>bit           Constant tcell>bit
  115: bits/byte          Constant tbits/byte
  116: float              Constant tfloat
  117: 1 bits/byte lshift Constant maxbyte
  118: H
  119: 
  120: >TARGET
  121: 
  122: \ Byte ordering and cell size                          06oct92py
  123: 
  124: : cell+         tcell + ;
  125: : cells         tcell<< lshift ;
  126: : chars         ;
  127: : floats	tfloat * ;
  128:     
  129: >CROSS
  130: : cell/         tcell<< rshift ;
  131: >TARGET
  132: 20 CONSTANT bl
  133: -1 Constant NIL
  134: -2 Constant :docol
  135: -3 Constant :docon
  136: -4 Constant :dovar
  137: -5 Constant :douser
  138: -6 Constant :dodefer
  139: -7 Constant :dofield
  140: -8 Constant :dodoes
  141: -9 Constant :doesjump
  142: 
  143: >CROSS
  144: 
  145: bigendian
  146: [IF]
  147:    : T!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
  148:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
  149:    : T@  ( addr -- n )  >r 0 0 r> tcell bounds
  150:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
  151: [ELSE]
  152:    : T!  ( n addr -- )  >r s>d r> tcell bounds
  153:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
  154:    : T@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
  155:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
  156: [THEN]
  157: 
  158: \ Memory initialisation                                05dec92py
  159: \ Fixed bug in else part                               11may93jaw
  160: 
  161: [IFDEF] Memory \ Memory is a bigFORTH feature
  162:    also Memory
  163:    : initmem ( var len -- )
  164:      2dup swap handle! >r @ r> erase ;
  165:    toss
  166: [ELSE]
  167:    : initmem ( var len -- )
  168:      tuck allocate abort" CROSS: No memory for target"
  169:      ( len var adr ) dup rot !
  170:      ( len adr ) swap erase ;
  171: [THEN]
  172: 
  173: \ MakeKernal                                           12dec92py
  174: 
  175: >MINIMAL
  176: : makekernal ( targetsize -- targetsize )
  177:   bit$  over 1- cell>bit rshift 1+ initmem
  178:   image over initmem tdp off ;
  179: 
  180: >CROSS
  181: \ Bit string manipulation                               06oct92py
  182: \                                                       9may93jaw
  183: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
  184: : bits ( n -- n ) chars Bittable + c@ ;
  185: 
  186: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
  187: : +bit ( addr n -- )  >bit over c@ or swap c! ;
  188: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
  189: : relon ( taddr -- )  bit$ @ swap cell/ +bit ;
  190: : reloff ( taddr -- )  bit$ @ swap cell/ -bit ;
  191: 
  192: \ Target memory access                                 06oct92py
  193: 
  194: : align+  ( taddr -- rest )
  195:     cell tuck 1- and - [ cell 1- ] Literal and ;
  196: : cfalign+  ( taddr -- rest )
  197:     \ see kernal.fs:cfaligned
  198:     float tuck 1- and - [ float 1- ] Literal and ;
  199: 
  200: >TARGET
  201: : aligned ( taddr -- ta-addr )  dup align+ + ;
  202: \ assumes cell alignment granularity (as GNU C)
  203: 
  204: : cfaligned ( taddr1 -- taddr2 )
  205:     \ see kernal.fs
  206:     dup cfalign+ + ;
  207: 
  208: >CROSS
  209: : >image ( taddr -- absaddr )  image @ + ;
  210: >TARGET
  211: : @  ( taddr -- w )     >image t@ ;
  212: : !  ( w taddr -- )     >image t! ;
  213: : c@ ( taddr -- char )  >image c@ ;
  214: : c! ( char taddr -- )  >image c! ;
  215: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
  216: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
  217: 
  218: \ Target compilation primitives                        06oct92py
  219: \ included A!                                          16may93jaw
  220: 
  221: : here  ( -- there )    there ;
  222: : allot ( n -- )        tdp +! ;
  223: : ,     ( w -- )        T here H cell T allot  ! H ;
  224: : c,    ( char -- )     T here    1 allot c! H ;
  225: : align ( -- )          T here H align+ 0 ?DO  bl T c, H LOOP ;
  226: : cfalign ( -- )
  227:     T here H cfalign+ 0 ?DO  bl T c, H LOOP ;
  228: 
  229: : A!                    dup relon T ! H ;
  230: : A,    ( w -- )        T here H relon T , H ;
  231: 
  232: >CROSS
  233: 
  234: \ threading modell                                     13dec92py
  235: 
  236: \ generic threading modell
  237: : docol,  ( -- ) :docol T A, 0 , H ;
  238: 
  239: >TARGET
  240: : >body   ( cfa -- pfa ) T cell+ cell+ H ;
  241: >CROSS
  242: 
  243: : dodoes, ( -- ) T :doesjump A, 0 , H ;
  244: 
  245: \ Ghost Builder                                        06oct92py
  246: 
  247: \ <T T> new version with temp variable                 10may93jaw
  248: 
  249: VARIABLE VocTemp
  250: 
  251: : <T  get-current VocTemp ! also Ghosts definitions ;
  252: : T>  previous VocTemp @ set-current ;
  253: 
  254: 4711 Constant <fwd>             4712 Constant <res>
  255: 4713 Constant <imm>
  256: 
  257: \ iForth makes only immediate directly after create
  258: \ make atonce trick! ?
  259: 
  260: Variable atonce atonce off
  261: 
  262: : NoExec true ABORT" CROSS: Don't execute ghost" ;
  263: 
  264: : GhostHeader <fwd> , 0 , ['] NoExec , ;
  265: 
  266: : >magic ; : >link cell+ ; : >exec cell+ cell+ ;
  267: : >end 3 cells + ;
  268: 
  269: Variable last-ghost
  270: : Make-Ghost ( "name" -- ghost )
  271:   >in @ GhostName swap >in !
  272:   <T Create atonce @ IF immediate atonce off THEN
  273:   here tuck swap ! ghostheader T>
  274:   DOES> dup last-ghost ! >exec @ execute ;
  275: 
  276: \ ghost words                                          14oct92py
  277: \                                          changed:    10may93py/jaw
  278: 
  279: : gfind   ( string -- ghost true/1 / string false )
  280: \ searches for string in word-list ghosts
  281:   dup count [ ' ghosts >body ] ALiteral search-wordlist
  282:   dup IF >r >body nip r>  THEN ;
  283: 
  284: VARIABLE Already
  285: 
  286: : ghost   ( "name" -- ghost )
  287:   Already off
  288:   >in @  bl word gfind   IF  Already on nip EXIT  THEN
  289:   drop  >in !  Make-Ghost ;
  290: 
  291: \ resolve                                              14oct92py
  292: 
  293: : resolve-loop ( ghost tcfa -- ghost tcfa )
  294:   >r dup >link @
  295:   BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
  296: 
  297: \ exists                                                9may93jaw
  298: 
  299: : exists ( ghost tcfa -- )
  300:   over GhostNames
  301:   BEGIN @ dup
  302:   WHILE 2dup cell+ @ =
  303:   UNTIL
  304:         2 cells + count cr ." CROSS: Exists: " type 4 spaces drop
  305:         swap cell+ !
  306:   ELSE  true abort" CROSS: Ghostnames inconsistent "
  307:   THEN ;
  308: 
  309: : resolve  ( ghost tcfa -- )
  310:   over >magic @ <fwd> <>  IF  exists EXIT THEN
  311:   resolve-loop  over >link ! <res> swap >magic ! ;
  312: 
  313: \ gexecute ghost,                                      01nov92py
  314: 
  315: : do-forward   ( ghost -- )
  316:   >link dup @  there rot !  T  A,  H ;
  317: : do-resolve   ( ghost -- )
  318:   >link @                   T  A,  H ;
  319: 
  320: : gexecute   ( ghost -- )   dup @
  321:              <fwd> = IF  do-forward  ELSE  do-resolve  THEN ;
  322: : ghost,     ghost  gexecute ;
  323: 
  324: \ .unresolved                                          11may93jaw
  325: 
  326: variable ResolveFlag
  327: 
  328: \ ?touched                                             11may93jaw
  329: 
  330: : ?touched ( ghost -- flag ) dup >magic @ <fwd> = swap >link @
  331:                                0 <> and ;
  332: 
  333: : ?resolved  ( ghostname -- )
  334:   dup cell+ @ ?touched
  335:   IF  cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;
  336: 
  337: >MINIMAL
  338: : .unresolved  ( -- )
  339:   ResolveFlag off cr ." Unresolved: "
  340:   Ghostnames
  341:   BEGIN @ dup
  342:   WHILE dup ?resolved
  343:   REPEAT drop ResolveFlag @
  344:   IF
  345:       abort" Unresolved words!"
  346:   ELSE
  347:       ." Nothing!"
  348:   THEN
  349:   cr ;
  350: 
  351: >CROSS
  352: \ Header states                                        12dec92py
  353: 
  354: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
  355: 
  356: VARIABLE ^imm
  357: 
  358: >TARGET
  359: : immediate     40 flag!
  360:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
  361:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
  362:                 <imm> ^imm @ ! ;
  363: : restrict      20 flag! ;
  364: >CROSS
  365: 
  366: \ ALIAS2 ansforth conform alias                          9may93jaw
  367: 
  368: : ALIAS2 create here 0 , DOES> @ execute ;
  369: \ usage:
  370: \ ' <name> alias2 bla !
  371: 
  372: \ Target Header Creation                               01nov92py
  373: 
  374: : string,  ( addr count -- )
  375:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
  376: : name,  ( "name" -- )  bl word count string, T cfalign H ;
  377: : view,   ( -- ) ( dummy ) ;
  378: 
  379: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
  380: 
  381: s" crossdoc.fd" r/w create-file throw value doc-file-id
  382: \ contains the file-id of the documentation file
  383: 
  384: : \G ( -- )
  385:     source >in @ /string doc-file-id write-line throw
  386:     source >in ! drop ; immediate
  387: 
  388: Variable to-doc
  389: 
  390: : cross-doc-entry  ( -- )
  391:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
  392:     IF
  393: 	s" " doc-file-id write-line throw
  394: 	s" make-doc " doc-file-id write-file throw
  395: 	tlast @ >image count $1F and doc-file-id write-file throw
  396: 	>in @
  397: 	[char] ( parse 2drop
  398: 	[char] ) parse doc-file-id write-file throw
  399: 	s"  )" doc-file-id write-file throw
  400: 	[char] \ parse 2drop					
  401: 	POSTPONE \g
  402: 	>in !
  403:     THEN  to-doc on ;
  404: 
  405: \ Target TAGS creation
  406: 
  407: s" kernal.TAGS" r/w create-file throw value tag-file-id
  408: \ contains the file-id of the tags file
  409: 
  410: Create tag-beg 2 c,  7F c, bl c,
  411: Create tag-end 2 c,  bl c, 01 c,
  412: Create tag-bof 1 c,  0C c,
  413: 
  414: 2variable last-loadfilename 0 0 last-loadfilename 2!
  415: 	    
  416: : put-load-file-name ( -- )
  417:     loadfilename 2@ last-loadfilename 2@ d<>
  418:     IF
  419: 	tag-bof count tag-file-id write-line throw
  420: 	sourcefilename 2dup
  421: 	tag-file-id write-file throw
  422: 	last-loadfilename 2!
  423: 	s" ,0" tag-file-id write-line throw
  424:     THEN ;
  425: 
  426: : cross-tag-entry  ( -- )
  427:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
  428:     IF
  429: 	put-load-file-name
  430: 	source >in @ min tag-file-id write-file throw
  431: 	tag-beg count tag-file-id write-file throw
  432: 	tlast @ >image count $1F and tag-file-id write-file throw
  433: 	tag-end count tag-file-id write-file throw
  434: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
  435: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
  436: 	s" ,0" tag-file-id write-line throw
  437: 	base !
  438:     THEN ;
  439: 
  440: \ Target header creation
  441: 
  442: VARIABLE CreateFlag CreateFlag off
  443: 
  444: : (Theader ( "name" -- ghost ) T align H view,
  445:   tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
  446:   >in @ name, >in ! T here H tlastcfa !
  447:   CreateFlag @ IF
  448:        >in @ alias2 swap >in !         \ create alias in target
  449:        >in @ ghost swap >in !
  450:        swap also ghosts ' previous swap !     \ tick ghost and store in alias
  451:        CreateFlag off
  452:   ELSE ghost THEN
  453:   dup >magic ^imm !     \ a pointer for immediate
  454:   Already @ IF  dup >end tdoes !
  455:   ELSE 0 tdoes ! THEN
  456:   80 flag!
  457:   cross-doc-entry cross-tag-entry ;
  458: 
  459: VARIABLE ;Resolve 1 cells allot
  460: 
  461: : Theader  ( "name" -- ghost )
  462:   (THeader dup there resolve 0 ;Resolve ! ;
  463: 
  464: >TARGET
  465: : Alias    ( cfa -- ) \ name
  466:   dup 0< IF  to-doc off  THEN
  467:   (THeader over resolve T A, H 80 flag! ;
  468: >CROSS
  469: 
  470: \ Conditionals and Comments                            11may93jaw
  471: 
  472: : ;Cond
  473:   postpone ;
  474:   swap ! ;  immediate
  475: 
  476: : Cond: ( -- ) \ name {code } ;
  477:   atonce on
  478:   ghost
  479:   >exec
  480:   :NONAME ;
  481: 
  482: : restrict? ( -- )
  483: \ aborts on interprete state - ae
  484:   state @ 0= ABORT" CROSS: Restricted" ;
  485: 
  486: : Comment ( -- )
  487:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
  488: 
  489: Comment (       Comment \
  490: 
  491: \ Predefined ghosts                                    12dec92py
  492: 
  493: ghost 0=                                        drop
  494: ghost branch    ghost ?branch                   2drop
  495: ghost (do)      ghost (?do)                     2drop
  496: ghost (for)                                     drop
  497: ghost (loop)    ghost (+loop)                   2drop
  498: ghost (next)                                    drop
  499: ghost unloop    ghost ;S                        2drop
  500: ghost lit       ghost (compile) ghost !         2drop drop
  501: ghost (does>)   ghost noop                      2drop
  502: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  503: ghost '
  504: 
  505: \ compile                                              10may93jaw
  506: 
  507: : compile  ( -- ) \ name
  508:   restrict?
  509:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
  510:   0> ( immediate? )
  511:   IF    >exec @ compile,
  512:   ELSE  postpone literal postpone gexecute  THEN ;
  513:                                         immediate
  514: 
  515: >TARGET
  516: : '  ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined "
  517:   dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
  518: 
  519: Cond: [']  compile lit ghost gexecute ;Cond
  520: 
  521: Cond: chars ;Cond
  522: 
  523: >CROSS
  524: \ tLiteral                                             12dec92py
  525: 
  526: : lit, ( n -- )   compile lit T  ,  H ;
  527: : alit, ( n -- )  compile lit T A,  H ;
  528: 
  529: >TARGET
  530: Cond:  Literal ( n -- )   restrict? lit, ;Cond
  531: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
  532: 
  533: : Char ( "<char>" -- )  bl word char+ c@ ;
  534: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
  535: 
  536: >CROSS
  537: \ Target compiling loop                                12dec92py
  538: \ ">tib trick thrown out                               10may93jaw
  539: \ number? defined at the top                           11may93jaw
  540: 
  541: \ compiled word might leave items on stack!
  542: : tcom ( in name -- )
  543:   gfind  ?dup  IF    0> IF    nip >exec @ execute
  544:                         ELSE  nip gexecute  THEN EXIT THEN
  545:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
  546:                ELSE  2drop >in !
  547:                ghost gexecute THEN  ;
  548: 
  549: >TARGET
  550: \ : ; DOES>                                            13dec92py
  551: \ ]                                                     9may93py/jaw
  552: 
  553: : ] state on
  554:     BEGIN
  555:         BEGIN >in @ bl word
  556:               dup c@ 0= WHILE 2drop refill 0=
  557:               ABORT" CROSS: End of file while target compiling"
  558:         REPEAT
  559:         tcom
  560:         state @
  561:         0=
  562:     UNTIL ;
  563: 
  564: \ by the way: defining a second interpreter (a compiler-)loop
  565: \             is not allowed if a system should be ans conform
  566: 
  567: : : ( -- colon-sys ) \ Name
  568:   (THeader ;Resolve ! there ;Resolve cell+ !
  569:   docol, depth T ] H ;
  570: 
  571: : :noname ( -- colon-sys )
  572:   T align H there docol, depth T ] H ;
  573: 
  574: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
  575: 
  576: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
  577: 
  578: Cond: ; ( -- ) restrict?
  579:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
  580:                           ELSE true ABORT" CROSS: Stack empty" THEN
  581:                compile ;S state off
  582:                ;Resolve @
  583:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
  584:                ;Cond
  585: Cond: [  restrict? state off ;Cond
  586: 
  587: >CROSS
  588: : !does  :dodoes tlastcfa @ tuck T ! cell+ ! H ;
  589: 
  590: >TARGET
  591: Cond: DOES> restrict?
  592:         compile (does>) dodoes, tdoes @ ?dup IF  @ T here H resolve THEN
  593:         ;Cond
  594: : DOES> dodoes, T here H !does depth T ] H ;
  595: 
  596: >CROSS
  597: \ Creation                                             01nov92py
  598: 
  599: \ Builder                                               11may93jaw
  600: 
  601: : Builder    ( Create do: "name" -- )
  602:   >in @ alias2 swap dup >in ! >r >r
  603:   Make-Ghost rot swap >exec ! ,
  604:   r> r> >in !
  605:   also ghosts ' previous swap ! ;
  606: \  DOES>  dup >exec @ execute ;
  607: 
  608: : gdoes,  ( ghost -- )  >end @ dup >magic @ <fwd> <>
  609:   IF dup >link @ dup 0< IF T A, 0 , H drop EXIT THEN drop THEN
  610:   :dodoes T A, H gexecute T here H cell - reloff ;
  611: 
  612: : TCreate ( -- )
  613:   last-ghost @
  614:   CreateFlag on
  615:   Theader >r dup gdoes,
  616:   >end @ >exec @ r> >exec ! ;
  617: 
  618: : Build:  ( -- [xt] [colon-sys] )
  619:   :noname  postpone TCreate ;
  620: 
  621: : gdoes>  ( ghost -- addr flag )
  622:   last-ghost @
  623:   state @ IF  gexecute true EXIT  THEN
  624:   cell+ @ T >body H false ;
  625: 
  626: \ DO: ;DO                                               11may93jaw
  627: \ changed to ?EXIT                                      10may93jaw
  628: 
  629: : DO:     ( -- addr [xt] [colon-sys] )
  630:   here ghostheader
  631:   :noname postpone gdoes> postpone ?EXIT ;
  632: 
  633: : ;DO ( addr [xt] [colon-sys] -- )
  634:   postpone ;    ( S addr xt )
  635:   over >exec ! ; immediate
  636: 
  637: : by      ( -- addr ) \ Name
  638:   ghost >end @ ;
  639: 
  640: >TARGET
  641: \ Variables and Constants                              05dec92py
  642: 
  643: Build:  ;
  644: DO: ( ghost -- addr ) ;DO
  645: Builder Create
  646: by Create :dovar resolve
  647: 
  648: Build: T 0 , H ;
  649: by Create
  650: Builder Variable
  651: 
  652: Build: T 0 A, H ;
  653: by Create
  654: Builder AVariable
  655: 
  656: \ User variables                                       04may94py
  657: 
  658: >CROSS
  659: Variable tup  0 tup !
  660: Variable tudp 0 tudp !
  661: : u,  ( n -- udp )
  662:   tup @ tudp @ + T  ! H
  663:   tudp @ dup T cell+ H tudp ! ;
  664: : au, ( n -- udp )
  665:   tup @ tudp @ + T A! H
  666:   tudp @ dup T cell+ H tudp ! ;
  667: >TARGET
  668: 
  669: Build: T 0 u, , H ;
  670: DO: ( ghost -- up-addr )  T @ H tup @ + ;DO
  671: Builder User
  672: by User :douser resolve
  673: 
  674: Build: T 0 u, , 0 u, drop H ;
  675: by User
  676: Builder 2User
  677: 
  678: Build: T 0 au, , H ;
  679: by User
  680: Builder AUser
  681: 
  682: Build:  ( n -- ) T , H ;
  683: DO: ( ghost -- n ) T @ H ;DO
  684: Builder Constant
  685: by Constant :docon resolve
  686: 
  687: Build:  ( n -- ) T A, H ;
  688: by Constant
  689: Builder AConstant
  690: 
  691: Build:  ( d -- ) T , , H ;
  692: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
  693: Builder 2Constant
  694: 
  695: Build: T 0 , H ;
  696: by Constant
  697: Builder Value
  698: 
  699: Build: T 0 A, H ;
  700: by Constant
  701: Builder AValue
  702: 
  703: Build:  ( -- ) compile noop ;
  704: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
  705: Builder Defer
  706: by Defer :dodefer resolve
  707: 
  708: Build:  ( inter comp -- ) swap T immediate A, A, H ;
  709: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
  710: Builder interpret/compile:
  711: 
  712: \ Sturctures                                           23feb95py
  713: 
  714: >CROSS
  715: : nalign ( addr1 n -- addr2 )
  716: \ addr2 is the aligned version of addr1 wrt the alignment size n
  717:  1- tuck +  swap invert and ;
  718: >TARGET
  719: 
  720: Build: 	>r rot r@ nalign  dup T , H  ( align1 size offset )
  721: 	+ swap r> nalign ;
  722: DO: T @ H + ;DO
  723: Builder Field
  724: by Field :dofield resolve
  725: 
  726: : struct  T 0 1 chars H ;
  727: : end-struct  T 2Constant H ;
  728: 
  729: : cells: ( n -- size align )
  730:     T cells 1 cells H ;
  731: 
  732: \ ' 2Constant Alias2 end-struct
  733: \ 0 1 T Chars H 2Constant struct
  734: 
  735: \ structural conditionals                              17dec92py
  736: 
  737: >CROSS
  738: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
  739: : sys?        ( sys -- sys )    dup 0= ?struc ;
  740: : >mark       ( -- sys )        T here  0 , H ;
  741: : >resolve    ( sys -- )        T here over - swap ! H ;
  742: : <resolve    ( sys -- )        T here - , H ;
  743: >TARGET
  744: 
  745: \ Structural Conditionals                              12dec92py
  746: 
  747: Cond: BUT       restrict? sys? swap ;Cond
  748: Cond: YET       restrict? sys? dup ;Cond
  749: 
  750: >CROSS
  751: Variable tleavings
  752: >TARGET
  753: 
  754: Cond: DONE   ( addr -- )  restrict? tleavings @
  755:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
  756:       tleavings ! drop ;Cond
  757: 
  758: >CROSS
  759: : (leave  T here H tleavings @ T , H  tleavings ! ;
  760: >TARGET
  761: 
  762: Cond: LEAVE     restrict? compile branch (leave ;Cond
  763: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond
  764: 
  765: \ Structural Conditionals                              12dec92py
  766: 
  767: Cond: AHEAD     restrict? compile branch >mark ;Cond
  768: Cond: IF        restrict? compile ?branch >mark ;Cond
  769: Cond: THEN      restrict? sys? dup T @ H ?struc >resolve ;Cond
  770: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
  771: 
  772: Cond: BEGIN     restrict? T here H ;Cond
  773: Cond: WHILE     restrict? sys? compile IF swap ;Cond
  774: Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
  775: Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
  776: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
  777: 
  778: \ Structural Conditionals                              12dec92py
  779: 
  780: Cond: DO        restrict? compile (do)   T here H ;Cond
  781: Cond: ?DO       restrict? compile (?do)  (leave T here H ;Cond
  782: Cond: FOR       restrict? compile (for)  T here H ;Cond
  783: 
  784: >CROSS
  785: : loop]   dup <resolve cell - compile DONE compile unloop ;
  786: >TARGET
  787: 
  788: Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
  789: Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
  790: Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond
  791: 
  792: \ String words                                         23feb93py
  793: 
  794: : ,"            [char] " parse string, T align H ;
  795: 
  796: Cond: ."        restrict? compile (.")     T ," H ;Cond
  797: Cond: S"        restrict? compile (S")     T ," H ;Cond
  798: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
  799: 
  800: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
  801: : IS            T ' >body ! H ;
  802: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
  803: : TO            T ' >body ! H ;
  804: 
  805: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
  806: 
  807: \ linked list primitive
  808: : linked        T here over @ A, swap ! H ;
  809: 
  810: : err"   s" ErrLink linked" evaluate T , H
  811:          [char] " parse string, T align H ;
  812: 
  813: : env"  [char] " parse s" EnvLink linked" evaluate
  814:         string, T align , H ;
  815: 
  816: : 2env" [char] " parse s" EnvLink linked" evaluate
  817:         here >r string, T align , , H
  818:         r> dup T c@ H 80 and swap T c! H ;
  819: 
  820: \ compile must be last                                 22feb93py
  821: 
  822: Cond: compile ( -- ) restrict? \ name
  823:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
  824:       0> IF    gexecute
  825:          ELSE  dup >magic @ <imm> =
  826:                IF   gexecute
  827:                ELSE compile (compile) gexecute THEN THEN ;Cond
  828: 
  829: Cond: postpone ( -- ) restrict? \ name
  830:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
  831:       0> IF    gexecute
  832:          ELSE  dup >magic @ <imm> =
  833:                IF   gexecute
  834:                ELSE compile (compile) gexecute THEN THEN ;Cond
  835: 
  836: >MINIMAL
  837: also minimal
  838: \ Usefull words                                        13feb93py
  839: 
  840: : KB  400 * ;
  841: 
  842: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
  843: 
  844: : there? bl word gfind IF >magic @ <fwd> <> ELSE drop false THEN ;
  845: 
  846: : [IFDEF] there? postpone [IF] ;
  847: : [IFUNDEF] there? 0= postpone [IF] ;
  848: 
  849: \ C: \- \+ Conditional Compiling                         09jun93jaw
  850: 
  851: : C: >in @ there? 0=
  852:      IF    >in ! T : H
  853:      ELSE drop
  854:         BEGIN bl word dup c@
  855:               IF   count comment? s" ;" compare 0= ?EXIT
  856:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
  857:               THEN
  858:         AGAIN
  859:      THEN ;
  860: 
  861: also minimal
  862: 
  863: : \- there? IF postpone \ THEN ;
  864: : \+ there? 0= IF postpone \ THEN ;
  865: 
  866: : [IF]   postpone [IF] ;
  867: : [THEN] postpone [THEN] ;
  868: : [ELSE] postpone [ELSE] ;
  869: 
  870: Cond: [IF]      [IF] ;Cond
  871: Cond: [IFDEF]   [IFDEF] ;Cond
  872: Cond: [IFUNDEF] [IFUNDEF] ;Cond
  873: Cond: [THEN]    [THEN] ;Cond
  874: Cond: [ELSE]    [ELSE] ;Cond
  875: 
  876: \ save-cross                                           17mar93py
  877: 
  878: \ i'm not interested in bigforth features this time    10may93jaw
  879: \ [IFDEF] file
  880: \ also file
  881: \ [THEN]
  882: \ included throw after create-file                     11may93jaw
  883: 
  884: bigendian Constant bigendian
  885: 
  886: Create magic  s" Gforth10" here over allot swap move
  887: 
  888: char 1 bigendian + cell + magic 7 + c!
  889: 
  890: : save-cross ( "image-name" "binary-name" -- )
  891:   bl parse ." Saving to " 2dup type cr
  892:   w/o bin create-file throw >r
  893:   s" #! "  r@ write-file throw
  894:   bl parse r@ write-file throw
  895:   s"  -i"  r@ write-file throw
  896:   #lf      r@ emit-file throw
  897:   r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
  898:   ?do
  899:       bl over emit-file throw
  900:   loop
  901:   drop
  902:   magic 8       r@ write-file throw \ write magic
  903:   image @ there r@ write-file throw \ write image
  904:   bit$  @ there 1- cell>bit rshift 1+
  905:                 r@ write-file throw \ write tags
  906:   r> close-file throw ;
  907: 
  908: \ words that should be in minimal
  909: 
  910: : + + ;         : 1- 1- ;
  911: : - - ;         : 2* 2* ;
  912: : * * ;         : / / ;
  913: : dup dup ;     : over over ;
  914: : swap swap ;   : rot rot ;
  915: : drop drop ;   : =   = ;
  916: : lshift lshift ; : 2/ 2/ ;
  917: : . . ;
  918: cell constant cell
  919: 
  920: \ include bug5.fs
  921: \ only forth also minimal definitions
  922: 
  923: : \  postpone \ ;
  924: : \G postpone \G ;
  925: : (  postpone ( ;
  926: : include bl word count included ;
  927: : .( [char] ) parse type ;
  928: : cr cr ;
  929: 
  930: : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
  931: only forth also minimal definitions
  932: 
  933: \ cross-compiler words
  934: 
  935: : decimal       decimal ;
  936: : hex           hex ;
  937: 
  938: : tudp          T tudp H ;
  939: : tup           T tup H ;  minimal
  940: 
  941: \ for debugging...
  942: : order         order ;
  943: : words         words ;
  944: : .s            .s ;
  945: 
  946: : bye           bye ;
  947: 
  948: \ turnkey direction
  949: : H forth ; immediate
  950: : T minimal ; immediate
  951: : G ghosts ; immediate
  952: 
  953: : turnkey  0 set-order also Target definitions
  954:            also Minimal also ;
  955: 
  956: \ these ones are pefered:
  957: 
  958: : lock   turnkey ;
  959: : unlock forth also cross ;
  960: 
  961: unlock definitions also minimal
  962: : lock   lock ;
  963: lock

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