File:  [gforth] / gforth / cross.fs
Revision 1.24: download - view: text, annotated - select for diffs
Thu Feb 23 20:17:16 1995 UTC (29 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added structure support in kernal
fixed bug on dictionary expand (512 wordlist limit)

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

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