File:  [gforth] / gforth / cross.fs
Revision 1.42: download - view: text, annotated - select for diffs
Thu Feb 6 21:22:58 1997 UTC (27 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Started to merge in changes made for gforth-EC project.

    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: mach-file count 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: 
  135: >CROSS
  136: 
  137: bigendian
  138: [IF]
  139:    : T!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
  140:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
  141:    : T@  ( addr -- n )  >r 0 0 r> tcell bounds
  142:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
  143: [ELSE]
  144:    : T!  ( n addr -- )  >r s>d r> tcell bounds
  145:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
  146:    : T@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
  147:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
  148: [THEN]
  149: 
  150: \ Memory initialisation                                05dec92py
  151: \ Fixed bug in else part                               11may93jaw
  152: 
  153: [IFDEF] Memory \ Memory is a bigFORTH feature
  154:    also Memory
  155:    : initmem ( var len -- )
  156:      2dup swap handle! >r @ r> erase ;
  157:    toss
  158: [ELSE]
  159:    : initmem ( var len -- )
  160:      tuck allocate abort" CROSS: No memory for target"
  161:      ( len var adr ) dup rot !
  162:      ( len adr ) swap erase ;
  163: [THEN]
  164: 
  165: \ MakeKernal                                           12dec92py
  166: 
  167: >MINIMAL
  168: : makekernel ( targetsize -- targetsize )
  169:   bit$  over 1- cell>bit rshift 1+ initmem
  170:   image over initmem tdp off ;
  171: 
  172: >CROSS
  173: \ Bit string manipulation                               06oct92py
  174: \                                                       9may93jaw
  175: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
  176: : bits ( n -- n ) chars Bittable + c@ ;
  177: 
  178: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
  179: : +bit ( addr n -- )  >bit over c@ or swap c! ;
  180: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
  181: : relon ( taddr -- )  bit$ @ swap cell/ +bit ;
  182: : reloff ( taddr -- )  bit$ @ swap cell/ -bit ;
  183: 
  184: \ Target memory access                                 06oct92py
  185: 
  186: : align+  ( taddr -- rest )
  187:     cell tuck 1- and - [ cell 1- ] Literal and ;
  188: : cfalign+  ( taddr -- rest )
  189:     \ see kernel.fs:cfaligned
  190:     float tuck 1- and - [ float 1- ] Literal and ;
  191: 
  192: >TARGET
  193: : aligned ( taddr -- ta-addr )  dup align+ + ;
  194: \ assumes cell alignment granularity (as GNU C)
  195: 
  196: : cfaligned ( taddr1 -- taddr2 )
  197:     \ see kernel.fs
  198:     dup cfalign+ + ;
  199: 
  200: >CROSS
  201: : >image ( taddr -- absaddr )  image @ + ;
  202: >TARGET
  203: : @  ( taddr -- w )     >image t@ ;
  204: : !  ( w taddr -- )     >image t! ;
  205: : c@ ( taddr -- char )  >image c@ ;
  206: : c! ( char taddr -- )  >image c! ;
  207: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
  208: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
  209: 
  210: \ Target compilation primitives                        06oct92py
  211: \ included A!                                          16may93jaw
  212: 
  213: : here  ( -- there )    there ;
  214: : allot ( n -- )        tdp +! ;
  215: : ,     ( w -- )        T here H cell T allot  ! H ;
  216: : c,    ( char -- )     T here    1 allot c! H ;
  217: : align ( -- )          T here H align+ 0 ?DO  bl T c, H LOOP ;
  218: : cfalign ( -- )
  219:     T here H cfalign+ 0 ?DO  bl T c, H LOOP ;
  220: 
  221: : A!                    dup relon T ! H ;
  222: : A,    ( w -- )        T here H relon T , H ;
  223: 
  224: >CROSS
  225: 
  226: \ threading modell                                     13dec92py
  227: 
  228: >TARGET
  229: : >body   ( cfa -- pfa ) T cell+ cell+ H ;
  230: >CROSS
  231: 
  232: \ Ghost Builder                                        06oct92py
  233: 
  234: \ <T T> new version with temp variable                 10may93jaw
  235: 
  236: VARIABLE VocTemp
  237: 
  238: : <T  get-current VocTemp ! also Ghosts definitions ;
  239: : T>  previous VocTemp @ set-current ;
  240: 
  241: 4711 Constant <fwd>             4712 Constant <res>
  242: 4713 Constant <imm>             4714 Constant <do:>
  243: 
  244: \ iForth makes only immediate directly after create
  245: \ make atonce trick! ?
  246: 
  247: Variable atonce atonce off
  248: 
  249: : NoExec true ABORT" CROSS: Don't execute ghost" ;
  250: 
  251: : GhostHeader <fwd> , 0 , ['] NoExec , ;
  252: 
  253: : >magic ; : >link cell+ ; : >exec cell+ cell+ ;
  254: : >end 3 cells + ;
  255: 
  256: Variable last-ghost
  257: : Make-Ghost ( "name" -- ghost )
  258:   >in @ GhostName swap >in !
  259:   <T Create atonce @ IF immediate atonce off THEN
  260:   here tuck swap ! ghostheader T>
  261:   DOES> dup last-ghost ! >exec @ execute ;
  262: 
  263: \ ghost words                                          14oct92py
  264: \                                          changed:    10may93py/jaw
  265: 
  266: : gfind   ( string -- ghost true/1 / string false )
  267: \ searches for string in word-list ghosts
  268:   dup count [ ' ghosts >body ] ALiteral search-wordlist
  269:   dup IF >r >body nip r>  THEN ;
  270: 
  271: VARIABLE Already
  272: 
  273: : ghost   ( "name" -- ghost )
  274:   Already off
  275:   >in @  bl word gfind   IF  Already on nip EXIT  THEN
  276:   drop  >in !  Make-Ghost ;
  277: 
  278: \ resolve                                              14oct92py
  279: 
  280: : resolve-loop ( ghost tcfa -- ghost tcfa )
  281:   >r dup >link @
  282:   BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
  283: 
  284: \ exists                                                9may93jaw
  285: 
  286: : exists ( ghost tcfa -- )
  287:   over GhostNames
  288:   BEGIN @ dup
  289:   WHILE 2dup cell+ @ =
  290:   UNTIL
  291:         2 cells + count cr ." CROSS: Exists: " type 4 spaces drop
  292:         swap cell+ !
  293:   ELSE  true abort" CROSS: Ghostnames inconsistent "
  294:   THEN ;
  295: 
  296: : resolve  ( ghost tcfa -- )
  297:   over >magic @ <fwd> <>  IF  exists EXIT THEN
  298:   resolve-loop  over >link ! <res> swap >magic ! ;
  299: 
  300: \ gexecute ghost,                                      01nov92py
  301: 
  302: : do-forward   ( ghost -- )
  303:   >link dup @  there rot !  T  A,  H ;
  304: : do-resolve   ( ghost -- )
  305:   >link @                   T  A,  H ;
  306: 
  307: : gexecute   ( ghost -- )   dup @
  308:              <fwd> = IF  do-forward  ELSE  do-resolve  THEN ;
  309: : ghost,     ghost  gexecute ;
  310: 
  311: \ .unresolved                                          11may93jaw
  312: 
  313: variable ResolveFlag
  314: 
  315: \ ?touched                                             11may93jaw
  316: 
  317: : ?touched ( ghost -- flag ) dup >magic @ <fwd> = swap >link @
  318:                                0 <> and ;
  319: 
  320: : ?resolved  ( ghostname -- )
  321:   dup cell+ @ ?touched
  322:   IF  cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;
  323: 
  324: >MINIMAL
  325: : .unresolved  ( -- )
  326:   ResolveFlag off cr ." Unresolved: "
  327:   Ghostnames
  328:   BEGIN @ dup
  329:   WHILE dup ?resolved
  330:   REPEAT drop ResolveFlag @
  331:   IF
  332:       abort" Unresolved words!"
  333:   ELSE
  334:       ." Nothing!"
  335:   THEN
  336:   cr ;
  337: 
  338: >CROSS
  339: \ Header states                                        12dec92py
  340: 
  341: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
  342: 
  343: VARIABLE ^imm
  344: 
  345: >TARGET
  346: : immediate     40 flag!
  347:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
  348:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
  349:                 <imm> ^imm @ ! ;
  350: : restrict      20 flag! ;
  351: >CROSS
  352: 
  353: \ ALIAS2 ansforth conform alias                          9may93jaw
  354: 
  355: : ALIAS2 create here 0 , DOES> @ execute ;
  356: \ usage:
  357: \ ' <name> alias2 bla !
  358: 
  359: \ Target Header Creation                               01nov92py
  360: 
  361: : string,  ( addr count -- )
  362:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
  363: : name,  ( "name" -- )  bl word count string, T cfalign H ;
  364: : view,   ( -- ) ( dummy ) ;
  365: 
  366: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
  367: 
  368: s" crossdoc.fd" r/w create-file throw value doc-file-id
  369: \ contains the file-id of the documentation file
  370: 
  371: : T-\G ( -- )
  372:     source >in @ /string doc-file-id write-line throw
  373:     postpone \ ;
  374: 
  375: Variable to-doc  to-doc on
  376: 
  377: : cross-doc-entry  ( -- )
  378:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
  379:     IF
  380: 	s" " doc-file-id write-line throw
  381: 	s" make-doc " doc-file-id write-file throw
  382: 	tlast @ >image count $1F and doc-file-id write-file throw
  383: 	>in @
  384: 	[char] ( parse 2drop
  385: 	[char] ) parse doc-file-id write-file throw
  386: 	s"  )" doc-file-id write-file throw
  387: 	[char] \ parse 2drop					
  388: 	T-\G
  389: 	>in !
  390:     THEN ;
  391: 
  392: \ Target TAGS creation
  393: 
  394: s" kernel.TAGS" r/w create-file throw value tag-file-id
  395: \ contains the file-id of the tags file
  396: 
  397: Create tag-beg 2 c,  7F c, bl c,
  398: Create tag-end 2 c,  bl c, 01 c,
  399: Create tag-bof 1 c,  0C c,
  400: 
  401: 2variable last-loadfilename 0 0 last-loadfilename 2!
  402: 	    
  403: : put-load-file-name ( -- )
  404:     loadfilename 2@ last-loadfilename 2@ d<>
  405:     IF
  406: 	tag-bof count tag-file-id write-line throw
  407: 	sourcefilename 2dup
  408: 	tag-file-id write-file throw
  409: 	last-loadfilename 2!
  410: 	s" ,0" tag-file-id write-line throw
  411:     THEN ;
  412: 
  413: : cross-tag-entry  ( -- )
  414:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
  415:     IF
  416: 	put-load-file-name
  417: 	source >in @ min tag-file-id write-file throw
  418: 	tag-beg count tag-file-id write-file throw
  419: 	tlast @ >image count $1F and tag-file-id write-file throw
  420: 	tag-end count tag-file-id write-file throw
  421: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
  422: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
  423: 	s" ,0" tag-file-id write-line throw
  424: 	base !
  425:     THEN ;
  426: 
  427: \ Target header creation
  428: 
  429: VARIABLE CreateFlag CreateFlag off
  430: 
  431: : (Theader ( "name" -- ghost ) T align H view,
  432:   tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
  433:   >in @ name, >in ! T here H tlastcfa !
  434:   CreateFlag @ IF
  435:        >in @ alias2 swap >in !         \ create alias in target
  436:        >in @ ghost swap >in !
  437:        swap also ghosts ' previous swap !     \ tick ghost and store in alias
  438:        CreateFlag off
  439:   ELSE ghost THEN
  440:   dup >magic ^imm !     \ a pointer for immediate
  441:   Already @ IF  dup >end tdoes !
  442:   ELSE 0 tdoes ! THEN
  443:   80 flag!
  444:   cross-doc-entry cross-tag-entry ;
  445: 
  446: VARIABLE ;Resolve 1 cells allot
  447: 
  448: : Theader  ( "name" -- ghost )
  449:   (THeader dup there resolve 0 ;Resolve ! ;
  450: 
  451: >TARGET
  452: : Alias    ( cfa -- ) \ name
  453:   (THeader over resolve T A, H 80 flag! ;
  454: : Alias:   ( cfa -- ) \ name
  455:   ghost tuck swap resolve <do:> swap >magic ! ;
  456: >CROSS
  457: 
  458: \ Conditionals and Comments                            11may93jaw
  459: 
  460: : ;Cond
  461:   postpone ;
  462:   swap ! ;  immediate
  463: 
  464: : Cond: ( -- ) \ name {code } ;
  465:   atonce on
  466:   ghost
  467:   >exec
  468:   :NONAME ;
  469: 
  470: : restrict? ( -- )
  471: \ aborts on interprete state - ae
  472:   state @ 0= ABORT" CROSS: Restricted" ;
  473: 
  474: : Comment ( -- )
  475:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
  476: 
  477: Comment (       Comment \
  478: 
  479: \ Predefined ghosts                                    12dec92py
  480: 
  481: ghost 0=                                        drop
  482: ghost branch    ghost ?branch                   2drop
  483: ghost (do)      ghost (?do)                     2drop
  484: ghost (for)                                     drop
  485: ghost (loop)    ghost (+loop)                   2drop
  486: ghost (next)                                    drop
  487: ghost unloop    ghost ;S                        2drop
  488: ghost lit       ghost (compile) ghost !         2drop drop
  489: ghost (does>)   ghost noop                      2drop
  490: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  491: ghost '                                         drop
  492: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
  493: 
  494: \ compile                                              10may93jaw
  495: 
  496: : compile  ( -- ) \ name
  497:   restrict?
  498:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
  499:   0> ( immediate? )
  500:   IF    >exec @ compile,
  501:   ELSE  postpone literal postpone gexecute  THEN ;
  502:                                         immediate
  503: 
  504: \ generic threading modell
  505: : docol,  ( -- ) compile :docol T 0 , H ;
  506: 
  507: : dodoes, ( -- ) compile :doesjump T 0 , H ;
  508: 
  509: >TARGET
  510: : '  ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined "
  511:   dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
  512: 
  513: Cond: [']  compile lit ghost gexecute ;Cond
  514: 
  515: Cond: chars ;Cond
  516: 
  517: >CROSS
  518: \ tLiteral                                             12dec92py
  519: 
  520: : lit, ( n -- )   compile lit T  ,  H ;
  521: : alit, ( n -- )  compile lit T A,  H ;
  522: 
  523: >TARGET
  524: Cond: \G  T-\G ;Cond
  525: 
  526: Cond:  Literal ( n -- )   restrict? lit, ;Cond
  527: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
  528: 
  529: : Char ( "<char>" -- )  bl word char+ c@ ;
  530: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
  531: 
  532: >CROSS
  533: \ Target compiling loop                                12dec92py
  534: \ ">tib trick thrown out                               10may93jaw
  535: \ number? defined at the top                           11may93jaw
  536: 
  537: \ compiled word might leave items on stack!
  538: : tcom ( in name -- )
  539:   gfind  ?dup  IF    0> IF    nip >exec @ execute
  540:                         ELSE  nip gexecute  THEN EXIT THEN
  541:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
  542:                ELSE  2drop >in !
  543:                ghost gexecute THEN  ;
  544: 
  545: >TARGET
  546: \ : ; DOES>                                            13dec92py
  547: \ ]                                                     9may93py/jaw
  548: 
  549: : ] state on
  550:     BEGIN
  551:         BEGIN >in @ bl word
  552:               dup c@ 0= WHILE 2drop refill 0=
  553:               ABORT" CROSS: End of file while target compiling"
  554:         REPEAT
  555:         tcom
  556:         state @
  557:         0=
  558:     UNTIL ;
  559: 
  560: \ by the way: defining a second interpreter (a compiler-)loop
  561: \             is not allowed if a system should be ans conform
  562: 
  563: : : ( -- colon-sys ) \ Name
  564:   (THeader ;Resolve ! there ;Resolve cell+ !
  565:   docol, depth T ] H ;
  566: 
  567: : :noname ( -- colon-sys )
  568:   T align H there docol, depth T ] H ;
  569: 
  570: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
  571: 
  572: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
  573: 
  574: Cond: ; ( -- ) restrict?
  575:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
  576:                           ELSE true ABORT" CROSS: Stack empty" THEN
  577:                compile ;S state off
  578:                ;Resolve @
  579:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
  580:                ;Cond
  581: Cond: [  restrict? state off ;Cond
  582: 
  583: >CROSS
  584: : !does
  585:     tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
  586: 
  587: >TARGET
  588: Cond: DOES> restrict?
  589:         compile (does>) dodoes, tdoes @ ?dup IF  @ T here H resolve THEN
  590:         ;Cond
  591: : DOES> dodoes, T here H !does depth T ] H ;
  592: 
  593: >CROSS
  594: \ Creation                                             01nov92py
  595: 
  596: \ Builder                                               11may93jaw
  597: 
  598: : Builder    ( Create do: "name" -- )
  599:   >in @ alias2 swap dup >in ! >r >r
  600:   Make-Ghost rot swap >exec ! ,
  601:   r> r> >in !
  602:   also ghosts ' previous swap ! ;
  603: \  DOES>  dup >exec @ execute ;
  604: 
  605: : gdoes,  ( ghost -- )  >end @ dup >magic @ <fwd> <>
  606:     IF
  607: 	dup >magic @ <do:> =
  608: 	IF  gexecute T 0 , H  EXIT THEN
  609:     THEN
  610:     compile :dodoes 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: : by:     ( -- addr [xt] [colon-sys] ) \ name
  634:   ghost
  635:   :noname postpone gdoes> postpone ?EXIT ;
  636: 
  637: : ;DO ( addr [xt] [colon-sys] -- )
  638:   postpone ;    ( S addr xt )
  639:   over >exec ! ; immediate
  640: 
  641: : by      ( -- addr ) \ Name
  642:   ghost >end @ ;
  643: 
  644: >TARGET
  645: \ Variables and Constants                              05dec92py
  646: 
  647: Build:  ;
  648: by: :dovar ( ghost -- addr ) ;DO
  649: Builder Create
  650: 
  651: Build: T 0 , H ;
  652: by Create
  653: Builder Variable
  654: 
  655: Build: T 0 A, H ;
  656: by Create
  657: Builder AVariable
  658: 
  659: \ User variables                                       04may94py
  660: 
  661: >CROSS
  662: Variable tup  0 tup !
  663: Variable tudp 0 tudp !
  664: : u,  ( n -- udp )
  665:   tup @ tudp @ + T  ! H
  666:   tudp @ dup T cell+ H tudp ! ;
  667: : au, ( n -- udp )
  668:   tup @ tudp @ + T A! H
  669:   tudp @ dup T cell+ H tudp ! ;
  670: >TARGET
  671: 
  672: Build: T 0 u, , H ;
  673: by: :douser ( ghost -- up-addr )  T @ H tup @ + ;DO
  674: Builder User
  675: 
  676: Build: T 0 u, , 0 u, drop H ;
  677: by User
  678: Builder 2User
  679: 
  680: Build: T 0 au, , H ;
  681: by User
  682: Builder AUser
  683: 
  684: Build:  ( n -- ) T , H ;
  685: by: :docon ( ghost -- n ) T @ H ;DO
  686: Builder Constant
  687: 
  688: Build:  ( n -- ) T A, H ;
  689: by Constant
  690: Builder AConstant
  691: 
  692: Build:  ( d -- ) T , , H ;
  693: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
  694: Builder 2Constant
  695: 
  696: Build: T 0 , H ;
  697: by Constant
  698: Builder Value
  699: 
  700: Build: T 0 A, H ;
  701: by Constant
  702: Builder AValue
  703: 
  704: Build:  ( -- ) compile noop ;
  705: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
  706: Builder Defer
  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: by: :dofield T @ H + ;DO
  723: Builder Field
  724: 
  725: : struct  T 0 1 chars H ;
  726: : end-struct  T 2Constant H ;
  727: 
  728: : cells: ( n -- size align )
  729:     T cells 1 cells H ;
  730: 
  731: \ ' 2Constant Alias2 end-struct
  732: \ 0 1 T Chars H 2Constant struct
  733: 
  734: \ structural conditionals                              17dec92py
  735: 
  736: >CROSS
  737: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
  738: : sys?        ( sys -- sys )    dup 0= ?struc ;
  739: : >mark       ( -- sys )        T here  0 , H ;
  740: : >resolve    ( sys -- )        T here over - swap ! H ;
  741: : <resolve    ( sys -- )        T here - , H ;
  742: >TARGET
  743: 
  744: \ Structural Conditionals                              12dec92py
  745: 
  746: Cond: BUT       restrict? sys? swap ;Cond
  747: Cond: YET       restrict? sys? dup ;Cond
  748: 
  749: >CROSS
  750: Variable tleavings
  751: >TARGET
  752: 
  753: Cond: DONE   ( addr -- )  restrict? tleavings @
  754:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
  755:       tleavings ! drop ;Cond
  756: 
  757: >CROSS
  758: : (leave  T here H tleavings @ T , H  tleavings ! ;
  759: >TARGET
  760: 
  761: Cond: LEAVE     restrict? compile branch (leave ;Cond
  762: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond
  763: 
  764: \ Structural Conditionals                              12dec92py
  765: 
  766: Cond: AHEAD     restrict? compile branch >mark ;Cond
  767: Cond: IF        restrict? compile ?branch >mark ;Cond
  768: Cond: THEN      restrict? sys? dup T @ H ?struc >resolve ;Cond
  769: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
  770: 
  771: Cond: BEGIN     restrict? T here H ;Cond
  772: Cond: WHILE     restrict? sys? compile IF swap ;Cond
  773: Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
  774: Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
  775: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
  776: 
  777: \ Structural Conditionals                              12dec92py
  778: 
  779: Cond: DO        restrict? compile (do)   T here H ;Cond
  780: Cond: ?DO       restrict? compile (?do)  (leave T here H ;Cond
  781: Cond: FOR       restrict? compile (for)  T here H ;Cond
  782: 
  783: >CROSS
  784: : loop]   dup <resolve cell - compile DONE compile unloop ;
  785: >TARGET
  786: 
  787: Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
  788: Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
  789: Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond
  790: 
  791: \ String words                                         23feb93py
  792: 
  793: : ,"            [char] " parse string, T align H ;
  794: 
  795: Cond: ."        restrict? compile (.")     T ," H ;Cond
  796: Cond: S"        restrict? compile (S")     T ," H ;Cond
  797: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
  798: 
  799: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
  800: : IS            T ' >body ! H ;
  801: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
  802: : TO            T ' >body ! H ;
  803: 
  804: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
  805: 
  806: \ linked list primitive
  807: : linked        T here over @ A, swap ! H ;
  808: 
  809: : err"   s" ErrLink linked" evaluate T , H
  810:          [char] " parse string, T align H ;
  811: 
  812: : env"  [char] " parse s" EnvLink linked" evaluate
  813:         string, T align , H ;
  814: 
  815: : 2env" [char] " parse s" EnvLink linked" evaluate
  816:         here >r string, T align , , H
  817:         r> dup T c@ H 80 and swap T c! H ;
  818: 
  819: \ compile must be last                                 22feb93py
  820: 
  821: Cond: compile ( -- ) restrict? \ name
  822:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
  823:       0> IF    gexecute
  824:          ELSE  dup >magic @ <imm> =
  825:                IF   gexecute
  826:                ELSE compile (compile) gexecute THEN THEN ;Cond
  827: 
  828: Cond: postpone ( -- ) restrict? \ name
  829:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
  830:       0> IF    gexecute
  831:          ELSE  dup >magic @ <imm> =
  832:                IF   gexecute
  833:                ELSE compile (compile) gexecute THEN THEN ;Cond
  834: 
  835: >MINIMAL
  836: also minimal
  837: \ Usefull words                                        13feb93py
  838: 
  839: : KB  400 * ;
  840: 
  841: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
  842: 
  843: : there? bl word gfind IF >magic @ <fwd> <> ELSE drop false THEN ;
  844: 
  845: : [IFDEF] there? postpone [IF] ;
  846: : [IFUNDEF] there? 0= postpone [IF] ;
  847: 
  848: \ C: \- \+ Conditional Compiling                         09jun93jaw
  849: 
  850: : C: >in @ there? 0=
  851:      IF    >in ! T : H
  852:      ELSE drop
  853:         BEGIN bl word dup c@
  854:               IF   count comment? s" ;" compare 0= ?EXIT
  855:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
  856:               THEN
  857:         AGAIN
  858:      THEN ;
  859: 
  860: also minimal
  861: 
  862: : \- there? IF postpone \ THEN ;
  863: : \+ there? 0= IF postpone \ THEN ;
  864: 
  865: : [IF]   postpone [IF] ;
  866: : [THEN] postpone [THEN] ;
  867: : [ELSE] postpone [ELSE] ;
  868: 
  869: Cond: [IF]      [IF] ;Cond
  870: Cond: [IFDEF]   [IFDEF] ;Cond
  871: Cond: [IFUNDEF] [IFUNDEF] ;Cond
  872: Cond: [THEN]    [THEN] ;Cond
  873: Cond: [ELSE]    [ELSE] ;Cond
  874: 
  875: \ save-cross                                           17mar93py
  876: 
  877: \ i'm not interested in bigforth features this time    10may93jaw
  878: \ [IFDEF] file
  879: \ also file
  880: \ [THEN]
  881: \ included throw after create-file                     11may93jaw
  882: 
  883: bigendian Constant bigendian
  884: 
  885: Create magic  s" Gforth10" here over allot swap move
  886: 
  887: char 1 bigendian + cell + magic 7 + c!
  888: 
  889: : save-cross ( "image-name" "binary-name" -- )
  890:   bl parse ." Saving to " 2dup type cr
  891:   w/o bin create-file throw >r
  892:   s" #! "  r@ write-file throw
  893:   bl parse r@ write-file throw
  894:   s"  -i"  r@ write-file throw
  895:   #lf      r@ emit-file throw
  896:   r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
  897:   ?do
  898:       bl over emit-file throw
  899:   loop
  900:   drop
  901:   magic 8       r@ write-file throw \ write magic
  902:   image @ there r@ write-file throw \ write image
  903:   bit$  @ there 1- cell>bit rshift 1+
  904:                 r@ write-file throw \ write tags
  905:   r> close-file throw ;
  906: 
  907: \ words that should be in minimal
  908: 
  909: : + + ;         : 1- 1- ;
  910: : - - ;         : 2* 2* ;
  911: : * * ;         : / / ;
  912: : dup dup ;     : over over ;
  913: : swap swap ;   : rot rot ;
  914: : drop drop ;   : =   = ;
  915: : lshift lshift ; : 2/ 2/ ;
  916: : . . ;
  917: \ cell constant cell
  918: 
  919: mach-file count included
  920: 
  921: \ include bug5.fs
  922: \ only forth also minimal definitions
  923: 
  924: : \  postpone \ ;  immediate
  925: : (  postpone ( ;  immediate
  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 ;
  940: 
  941: : doc-off       false T to-doc H ! ;
  942: : doc-on        true  T to-doc H ! ;
  943: 
  944: minimal
  945: 
  946: \ for debugging...
  947: : order         order ;
  948: : words         words ;
  949: : .s            .s ;
  950: 
  951: : bye           bye ;
  952: 
  953: \ turnkey direction
  954: : H forth ; immediate
  955: : T minimal ; immediate
  956: : G ghosts ; immediate
  957: 
  958: : turnkey  0 set-order also Target definitions
  959:            also Minimal also ;
  960: 
  961: \ these ones are pefered:
  962: 
  963: : lock   turnkey ;
  964: : unlock forth also cross ;
  965: 
  966: unlock definitions also minimal
  967: : lock   lock ;
  968: lock

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