Annotation of gforth/cross.fs, revision 1.27

1.1       anton       1: \ CROSS.FS     The Cross-Compiler                      06oct92py
1.23      pazsan      2: \ $Id: cross.fs,v 1.22 1995/02/06 18:14:30 anton Exp $
1.1       anton       3: \ Idea and implementation: Bernd Paysan (py)
1.15      pazsan      4: \ Copyright 1992-94 by the GNU Forth Development Group
1.1       anton       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: 
1.9       pazsan     24: \ include other.fs       \ ansforth extentions for cross
1.1       anton      25: 
1.23      pazsan     26: : string, ( c-addr u -- )
                     27:     \ puts down string as cstring
                     28:     dup c, here swap chars dup allot move ;
                     29: ' falign Alias cfalign
1.5       pazsan     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: 
1.1       anton      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 )
1.22      anton      46:     here GhostNames @ , GhostNames ! here 0 ,
                     47:     bl word count
                     48:     \ 2dup type space
                     49:     string, cfalign ;
1.1       anton      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: 
1.13      pazsan     91: included
1.1       anton      92: 
1.19      pazsan     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: 
1.1       anton     104: >TARGET
                    105: 
                    106: \ Byte ordering and cell size                          06oct92py
                    107: 
1.19      pazsan    108: : cell+         tcell + ;
                    109: : cells         tcell<< lshift ;
1.1       anton     110: : chars         ;
1.19      pazsan    111: : floats       tfloat * ;
1.6       anton     112:     
1.1       anton     113: >CROSS
1.19      pazsan    114: : cell/         tcell<< rshift ;
1.1       anton     115: >TARGET
                    116: 20 CONSTANT bl
                    117: -1 Constant NIL
                    118: -2 Constant :docol
                    119: -3 Constant :docon
                    120: -4 Constant :dovar
1.3       pazsan    121: -5 Constant :douser
1.10      anton     122: -6 Constant :dodefer
1.24      pazsan    123: -7 Constant :dostruc
                    124: -8 Constant :dodoes
                    125: -9 Constant :doesjump
1.1       anton     126: 
                    127: >CROSS
                    128: 
1.20      pazsan    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 ;
1.19      pazsan    135: [ELSE]
1.20      pazsan    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 ;
1.1       anton     140: [THEN]
                    141: 
                    142: \ Memory initialisation                                05dec92py
                    143: \ Fixed bug in else part                               11may93jaw
                    144: 
                    145: [IFDEF] Memory \ Memory is a bigFORTH feature
1.5       pazsan    146:    also Memory
1.1       anton     147:    : initmem ( var len -- )
                    148:      2dup swap handle! >r @ r> erase ;
1.5       pazsan    149:    toss
1.1       anton     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! ;
1.4       pazsan    172: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
1.1       anton     173: : relon ( taddr -- )  bit$ @ swap cell/ +bit ;
1.4       pazsan    174: : reloff ( taddr -- )  bit$ @ swap cell/ -bit ;
1.1       anton     175: 
                    176: \ Target memory access                                 06oct92py
                    177: 
                    178: : align+  ( taddr -- rest )
                    179:     cell tuck 1- and - [ cell 1- ] Literal and ;
1.22      anton     180: : cfalign+  ( taddr -- rest )
                    181:     \ see kernal.fs:cfaligned
                    182:     float tuck 1- and - [ float 1- ] Literal and ;
1.1       anton     183: 
                    184: >TARGET
                    185: : aligned ( taddr -- ta-addr )  dup align+ + ;
                    186: \ assumes cell alignment granularity (as GNU C)
                    187: 
1.22      anton     188: : cfaligned ( taddr1 -- taddr2 )
                    189:     \ see kernal.fs
                    190:     dup cfalign+ + ;
                    191: 
1.1       anton     192: >CROSS
                    193: : >image ( taddr -- absaddr )  image @ + ;
                    194: >TARGET
1.19      pazsan    195: : @  ( taddr -- w )     >image t@ ;
                    196: : !  ( w taddr -- )     >image t! ;
1.1       anton     197: : c@ ( taddr -- char )  >image c@ ;
                    198: : c! ( char taddr -- )  >image c! ;
1.7       anton     199: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
                    200: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
1.1       anton     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 ;
1.22      anton     210: : cfalign ( -- )
                    211:     T here H cfalign+ 0 ?DO  bl T c, H LOOP ;
1.1       anton     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: 
1.3       pazsan    227: : dodoes, ( -- ) T :doesjump A, 0 , H ;
1.1       anton     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: 
1.11      pazsan    253: Variable last-ghost
1.1       anton     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>
1.11      pazsan    258:   DOES> dup last-ghost ! >exec @ execute ;
1.1       anton     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
1.5       pazsan    266:   dup count [ ' ghosts >body ] ALiteral search-wordlist
1.13      pazsan    267:   dup IF  >r >body nip r>  THEN ;
1.1       anton     268: 
                    269: VARIABLE Already
                    270: 
                    271: : ghost   ( "name" -- ghost )
                    272:   Already off
1.13      pazsan    273:   >in @  bl word gfind   IF  Already on nip EXIT  THEN
1.1       anton     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
1.18      pazsan    289:         2 cells + count cr ." CROSS: Exists: " type 4 spaces drop
1.1       anton     290:         swap cell+ !
1.24      pazsan    291:   ELSE  true abort" CROSS: Ghostnames inconsistent "
1.1       anton     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
1.10      anton     328:   REPEAT drop ResolveFlag @
                    329:   IF
1.11      pazsan    330:       abort" Unresolved words!"
1.10      anton     331:   ELSE
                    332:       ." Nothing!"
                    333:   THEN
                    334:   cr ;
1.1       anton     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!
1.18      pazsan    345:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
1.1       anton     346:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                    347:                 <imm> ^imm @ ! ;
1.8       pazsan    348: : restrict      40 flag! ;
1.1       anton     349: >CROSS
                    350: 
                    351: \ ALIAS2 ansforth conform alias                          9may93jaw
                    352: 
                    353: : ALIAS2 create here 0 , DOES> @ execute ;
                    354: \ usage:
1.18      pazsan    355: \ ' <name> alias2 bla !
1.1       anton     356: 
                    357: \ Target Header Creation                               01nov92py
                    358: 
                    359: : string,  ( addr count -- )
                    360:   dup T c, H bounds  DO  I c@ T c, H  LOOP ; 
1.22      anton     361: : name,  ( "name" -- )  bl word count string, T cfalign H ;
1.1       anton     362: : view,   ( -- ) ( dummy ) ;
                    363: 
1.25      pazsan    364: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
                    365: 
                    366: s" crossdoc.fd" r/w create-file throw value doc-file-id
                    367: \ contains the file-id of the documentation file
                    368: 
                    369: : \G ( -- )
                    370:     source >in @ /string doc-file-id write-line throw
                    371:     source >in ! drop ; immediate
                    372: 
                    373: Variable to-doc
                    374: 
                    375: : cross-doc-entry  ( -- )
                    376:     to-doc @ tlast @ 0<> and   \ not an anonymous (i.e. noname) header
                    377:     IF
                    378:        s" " doc-file-id write-line throw
                    379:        s" make-doc " doc-file-id write-file throw
                    380:        tlast @ >image count $1F and doc-file-id write-file throw
                    381:        >in @
                    382:        [char] ( parse 2drop
                    383:        [char] ) parse doc-file-id write-file throw
                    384:        s"  )" doc-file-id write-file throw
                    385:        [char] \ parse 2drop                                    
                    386:        POSTPONE \g
                    387:        >in !
                    388:     THEN  to-doc on ;
                    389: 
1.1       anton     390: VARIABLE CreateFlag CreateFlag off
                    391: 
                    392: : (Theader ( "name" -- ghost ) T align H view,
                    393:   tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
                    394:   >in @ name, >in ! T here H tlastcfa !
                    395:   CreateFlag @ IF
1.18      pazsan    396:        >in @ alias2 swap >in !         \ create alias in target
                    397:        >in @ ghost swap >in !
                    398:        swap also ghosts ' previous swap !     \ tick ghost and store in alias
                    399:        CreateFlag off
1.1       anton     400:   ELSE ghost THEN
                    401:   dup >magic ^imm !     \ a pointer for immediate
                    402:   Already @ IF  dup >end tdoes !
                    403:   ELSE 0 tdoes ! THEN
1.25      pazsan    404:   80 flag!
                    405:   cross-doc-entry ;
1.1       anton     406: 
                    407: VARIABLE ;Resolve 1 cells allot
                    408: 
1.11      pazsan    409: : Theader  ( "name" -- ghost )
                    410:   (THeader dup there resolve 0 ;Resolve ! ;
1.1       anton     411: 
                    412: >TARGET
                    413: : Alias    ( cfa -- ) \ name
1.25      pazsan    414:   dup 0< IF  to-doc off  THEN
1.1       anton     415:   (THeader over resolve T A, H 80 flag! ;
                    416: >CROSS
                    417: 
                    418: \ Conditionals and Comments                            11may93jaw
                    419: 
                    420: : ;Cond
                    421:   postpone ;
                    422:   swap ! ;  immediate
                    423: 
                    424: : Cond: ( -- ) \ name {code } ;
                    425:   atonce on
                    426:   ghost
                    427:   >exec
                    428:   :NONAME ;
                    429: 
                    430: : restrict? ( -- )
                    431: \ aborts on interprete state - ae
                    432:   state @ 0= ABORT" CROSS: Restricted" ;
                    433: 
                    434: : Comment ( -- )
                    435:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
                    436: 
                    437: Comment (       Comment \
                    438: 
                    439: \ Predefined ghosts                                    12dec92py
                    440: 
                    441: ghost 0=                                        drop
                    442: ghost branch    ghost ?branch                   2drop
                    443: ghost (do)      ghost (?do)                     2drop
                    444: ghost (for)                                     drop
                    445: ghost (loop)    ghost (+loop)                   2drop
                    446: ghost (next)                                    drop
1.2       pazsan    447: ghost unloop    ghost ;S                        2drop
1.1       anton     448: ghost lit       ghost (compile) ghost !         2drop drop
                    449: ghost (;code)   ghost noop                      2drop
                    450: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
1.9       pazsan    451: ghost '
1.1       anton     452: 
                    453: \ compile                                              10may93jaw
                    454: 
                    455: : compile  ( -- ) \ name
                    456:   restrict?
1.13      pazsan    457:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
1.1       anton     458:   0> ( immediate? )
                    459:   IF    >exec @ compile,
                    460:   ELSE  postpone literal postpone gexecute  THEN ;
                    461:                                         immediate
                    462: 
                    463: >TARGET
1.13      pazsan    464: : '  ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined "
1.1       anton     465:   dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
                    466: 
                    467: Cond: [']  compile lit ghost gexecute ;Cond
1.14      anton     468: 
                    469: Cond: chars ;Cond
1.1       anton     470: 
                    471: >CROSS
                    472: \ tLiteral                                             12dec92py
                    473: 
                    474: : lit, ( n -- )   compile lit T  ,  H ;
                    475: : alit, ( n -- )  compile lit T A,  H ;
                    476: 
                    477: >TARGET
                    478: Cond:  Literal ( n -- )   restrict? lit, ;Cond
                    479: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
                    480: 
                    481: : Char ( "<char>" -- )  bl word char+ c@ ;
                    482: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
                    483: 
                    484: >CROSS
                    485: \ Target compiling loop                                12dec92py
                    486: \ ">tib trick thrown out                               10may93jaw
                    487: \ number? defined at the top                           11may93jaw
                    488: 
                    489: \ compiled word might leave items on stack!
                    490: : tcom ( in name -- )
                    491:   gfind  ?dup  IF    0> IF    nip >exec @ execute
                    492:                         ELSE  nip gexecute  THEN EXIT THEN
                    493:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
                    494:                ELSE  2drop >in !
                    495:                ghost gexecute THEN  ;
                    496: 
                    497: >TARGET
                    498: \ : ; DOES>                                            13dec92py
                    499: \ ]                                                     9may93py/jaw
                    500: 
                    501: : ] state on
                    502:     BEGIN
1.13      pazsan    503:         BEGIN >in @ bl word
1.1       anton     504:               dup c@ 0= WHILE 2drop refill 0=
                    505:               ABORT" CROSS: End of file while target compiling"
                    506:         REPEAT
                    507:         tcom
                    508:         state @
                    509:         0=
                    510:     UNTIL ;
                    511: 
                    512: \ by the way: defining a second interpreter (a compiler-)loop
                    513: \             is not allowed if a system should be ans conform
                    514: 
                    515: : : ( -- colon-sys ) \ Name
                    516:   (THeader ;Resolve ! there ;Resolve cell+ !
                    517:   docol, depth T ] H ;
                    518: 
1.2       pazsan    519: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
1.6       anton     520: 
                    521: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1.2       pazsan    522: 
1.1       anton     523: Cond: ; ( -- ) restrict?
                    524:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                    525:                           ELSE true ABORT" CROSS: Stack empty" THEN
1.2       pazsan    526:                compile ;S state off
1.1       anton     527:                ;Resolve @
                    528:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
                    529:                ;Cond
                    530: Cond: [  restrict? state off ;Cond
                    531: 
                    532: >CROSS
                    533: : !does  :dodoes tlastcfa @ tuck T ! cell+ ! H ;
                    534: 
                    535: >TARGET
                    536: Cond: DOES> restrict?
                    537:         compile (;code) dodoes, tdoes @ ?dup IF  @ T here H resolve THEN
                    538:         ;Cond
                    539: : DOES> dodoes, T here H !does depth T ] H ;
                    540: 
                    541: >CROSS
                    542: \ Creation                                             01nov92py
                    543: 
                    544: \ Builder                                               11may93jaw
                    545: 
                    546: : Builder    ( Create do: "name" -- )
                    547:   >in @ alias2 swap dup >in ! >r >r
                    548:   Make-Ghost rot swap >exec ! ,
                    549:   r> r> >in !
1.11      pazsan    550:   also ghosts ' previous swap ! ;
                    551: \  DOES>  dup >exec @ execute ;
1.1       anton     552: 
                    553: : gdoes,  ( ghost -- )  >end @ dup >magic @ <fwd> <>
                    554:   IF dup >link @ dup 0< IF T A, 0 , H drop EXIT THEN drop THEN
1.4       pazsan    555:   :dodoes T A, H gexecute T here H cell - reloff ;
1.1       anton     556: 
1.11      pazsan    557: : TCreate ( -- )
                    558:   last-ghost @
1.1       anton     559:   CreateFlag on
1.11      pazsan    560:   Theader >r dup gdoes,
                    561:   >end @ >exec @ r> >exec ! ;
1.1       anton     562: 
                    563: : Build:  ( -- [xt] [colon-sys] )
                    564:   :noname  postpone TCreate ;
                    565: 
                    566: : gdoes>  ( ghost -- addr flag )
1.11      pazsan    567:   last-ghost @
1.1       anton     568:   state @ IF  gexecute true EXIT  THEN
                    569:   cell+ @ T >body H false ;
                    570: 
                    571: \ DO: ;DO                                               11may93jaw
                    572: \ changed to ?EXIT                                      10may93jaw
                    573: 
                    574: : DO:     ( -- addr [xt] [colon-sys] )
                    575:   here ghostheader
1.11      pazsan    576:   :noname postpone gdoes> postpone ?EXIT ;
1.1       anton     577: 
                    578: : ;DO ( addr [xt] [colon-sys] -- )
                    579:   postpone ;    ( S addr xt )
                    580:   over >exec ! ; immediate
                    581: 
                    582: : by      ( -- addr ) \ Name
                    583:   ghost >end @ ;
                    584: 
                    585: >TARGET
                    586: \ Variables and Constants                              05dec92py
                    587: 
                    588: Build:  ;
                    589: DO: ( ghost -- addr ) ;DO
                    590: Builder Create
                    591: by Create :dovar resolve
                    592: 
                    593: Build: T 0 , H ;
                    594: by Create
                    595: Builder Variable
                    596: 
                    597: Build: T 0 A, H ;
                    598: by Create
                    599: Builder AVariable
                    600: 
1.3       pazsan    601: \ User variables                                       04may94py
                    602: 
                    603: >CROSS
                    604: Variable tup  0 tup !
                    605: Variable tudp 0 tudp !
                    606: : u,  ( n -- udp )
                    607:   tup @ tudp @ + T  ! H
1.19      pazsan    608:   tudp @ dup T cell+ H tudp ! ;
1.3       pazsan    609: : au, ( n -- udp )
                    610:   tup @ tudp @ + T A! H
1.19      pazsan    611:   tudp @ dup T cell+ H tudp ! ;
1.3       pazsan    612: >TARGET
                    613: 
                    614: Build: T 0 u, , H ;
                    615: DO: ( ghost -- up-addr )  T @ H tup @ + ;DO
1.1       anton     616: Builder User
1.3       pazsan    617: by User :douser resolve
1.1       anton     618: 
1.3       pazsan    619: Build: T 0 u, , 0 u, drop H ;
                    620: by User
1.1       anton     621: Builder 2User
                    622: 
1.3       pazsan    623: Build: T 0 au, , H ;
                    624: by User
1.1       anton     625: Builder AUser
                    626: 
                    627: Build:  ( n -- ) T , H ;
                    628: DO: ( ghost -- n ) T @ H ;DO
                    629: Builder Constant
                    630: by Constant :docon resolve
                    631: 
                    632: Build:  ( n -- ) T A, H ;
                    633: by Constant
                    634: Builder AConstant
                    635: 
1.24      pazsan    636: Build:  ( d -- ) T , , H ;
                    637: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
                    638: Builder 2Constant
                    639: 
1.1       anton     640: Build: T 0 , H ;
                    641: by Constant
                    642: Builder Value
                    643: 
                    644: Build:  ( -- ) compile noop ;
                    645: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
                    646: Builder Defer
1.10      anton     647: by Defer :dodefer resolve
1.24      pazsan    648: 
                    649: \ Sturctures                                           23feb95py
                    650: 
                    651: >CROSS
                    652: : nalign ( addr1 n -- addr2 )
                    653: \ addr2 is the aligned version of addr1 wrt the alignment size n
                    654:  1- tuck +  swap invert and ;
                    655: >TARGET
                    656: 
                    657: Build:         >r rot r@ nalign  dup T , H  ( align1 size offset )
                    658:        + swap r> nalign ;
                    659: DO: T @ H + ;DO
                    660: Builder Field
                    661: by Field :dostruc resolve
                    662: 
                    663: : struct  T 0 1 chars H ;
                    664: : end-struct  T 2Constant H ;
                    665: 
                    666: : cells: ( n -- size align )
                    667:     T cells 1 cells H ;
                    668: 
                    669: \ ' 2Constant Alias2 end-struct
                    670: \ 0 1 T Chars H 2Constant struct
1.1       anton     671: 
                    672: \ structural conditionals                              17dec92py
                    673: 
                    674: >CROSS
                    675: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
                    676: : sys?        ( sys -- sys )    dup 0= ?struc ;
                    677: : >mark       ( -- sys )        T here  0 , H ;
                    678: : >resolve    ( sys -- )        T here over - swap ! H ;
                    679: : <resolve    ( sys -- )        T here - , H ;
                    680: >TARGET
                    681: 
                    682: \ Structural Conditionals                              12dec92py
                    683: 
                    684: Cond: BUT       restrict? sys? swap ;Cond
                    685: Cond: YET       restrict? sys? dup ;Cond
                    686: 
                    687: >CROSS
                    688: Variable tleavings
                    689: >TARGET
                    690: 
                    691: Cond: DONE   ( addr -- )  restrict? tleavings @
                    692:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
                    693:       tleavings ! drop ;Cond
                    694: 
                    695: >CROSS
                    696: : (leave  T here H tleavings @ T , H  tleavings ! ;
                    697: >TARGET
                    698: 
                    699: Cond: LEAVE     restrict? compile branch (leave ;Cond
                    700: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond
                    701: 
                    702: \ Structural Conditionals                              12dec92py
                    703: 
                    704: Cond: AHEAD     restrict? compile branch >mark ;Cond
                    705: Cond: IF        restrict? compile ?branch >mark ;Cond
                    706: Cond: THEN      restrict? sys? dup T @ H ?struc >resolve ;Cond
                    707: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
                    708: 
                    709: Cond: BEGIN     restrict? T here H ;Cond
                    710: Cond: WHILE     restrict? sys? compile IF swap ;Cond
                    711: Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
                    712: Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
                    713: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
                    714: 
                    715: \ Structural Conditionals                              12dec92py
                    716: 
                    717: Cond: DO        restrict? compile (do)   T here H ;Cond
                    718: Cond: ?DO       restrict? compile (?do)  (leave T here H ;Cond
                    719: Cond: FOR       restrict? compile (for)  T here H ;Cond
                    720: 
                    721: >CROSS
                    722: : loop]   dup <resolve cell - compile DONE compile unloop ;
                    723: >TARGET
                    724: 
                    725: Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
                    726: Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
                    727: Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond
                    728: 
                    729: \ String words                                         23feb93py
                    730: 
                    731: : ,"            [char] " parse string, T align H ;
                    732: 
                    733: Cond: ."        restrict? compile (.")     T ," H ;Cond
                    734: Cond: S"        restrict? compile (S")     T ," H ;Cond
                    735: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
                    736: 
                    737: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
                    738: : IS            T ' >body ! H ;
1.9       pazsan    739: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
                    740: : TO            T ' >body ! H ;
1.1       anton     741: 
                    742: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
                    743: 
                    744: \ linked list primitive
                    745: : linked        T here over @ A, swap ! H ;
                    746: 
                    747: : err"   s" ErrLink linked" evaluate T , H
                    748:          [char] " parse string, T align H ;
                    749: 
                    750: : env"  [char] " parse s" EnvLink linked" evaluate
                    751:         string, T align , H ;
                    752: 
                    753: : 2env" [char] " parse s" EnvLink linked" evaluate
                    754:         here >r string, T align , , H
                    755:         r> dup T c@ H 80 and swap T c! H ;
                    756: 
                    757: \ compile must be last                                 22feb93py
                    758: 
                    759: Cond: compile ( -- ) restrict? \ name
1.13      pazsan    760:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1       anton     761:       0> IF    gexecute
                    762:          ELSE  dup >magic @ <imm> =
                    763:                IF   gexecute
                    764:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    765: 
                    766: Cond: postpone ( -- ) restrict? \ name
1.13      pazsan    767:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1       anton     768:       0> IF    gexecute
                    769:          ELSE  dup >magic @ <imm> =
                    770:                IF   gexecute
                    771:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    772: 
                    773: >MINIMAL
                    774: also minimal
                    775: \ Usefull words                                        13feb93py
                    776: 
                    777: : KB  400 * ;
                    778: 
                    779: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
                    780: 
1.13      pazsan    781: : there? bl word gfind IF >magic @ <fwd> <> ELSE drop false THEN ;
1.1       anton     782: 
                    783: : [IFDEF] there? postpone [IF] ;
                    784: : [IFUNDEF] there? 0= postpone [IF] ;
                    785: 
                    786: \ C: \- \+ Conditional Compiling                         09jun93jaw
                    787: 
                    788: : C: >in @ there? 0=
                    789:      IF    >in ! T : H
                    790:      ELSE drop
                    791:         BEGIN bl word dup c@
                    792:               IF   count comment? s" ;" compare 0= ?EXIT
                    793:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
                    794:               THEN
                    795:         AGAIN
                    796:      THEN ;
                    797: 
                    798: also minimal
                    799: 
                    800: : \- there? IF postpone \ THEN ;
                    801: : \+ there? 0= IF postpone \ THEN ;
                    802: 
                    803: : [IF]   postpone [IF] ;
                    804: : [THEN] postpone [THEN] ;
                    805: : [ELSE] postpone [ELSE] ;
                    806: 
                    807: Cond: [IF]      [IF] ;Cond
                    808: Cond: [IFDEF]   [IFDEF] ;Cond
                    809: Cond: [IFUNDEF] [IFUNDEF] ;Cond
                    810: Cond: [THEN]    [THEN] ;Cond
                    811: Cond: [ELSE]    [ELSE] ;Cond
                    812: 
                    813: \ save-cross                                           17mar93py
                    814: 
                    815: \ i'm not interested in bigforth features this time    10may93jaw
                    816: \ [IFDEF] file
                    817: \ also file
                    818: \ [THEN]
                    819: \ included throw after create-file                     11may93jaw
                    820: 
1.12      anton     821: bigendian Constant bigendian
1.1       anton     822: 
1.26      pazsan    823: Create magic  s" gforth00" here over allot swap move
                    824: 
                    825: [char] 1 bigendian + cell + magic 7 + c!
                    826: 
1.1       anton     827: : save-cross ( "name" -- )
                    828:   bl parse ." Saving to " 2dup type
                    829:   w/o bin create-file throw >r
1.26      pazsan    830:   magic 8       r@ write-file throw \ write magic
1.16      pazsan    831:   image @ there r@ write-file throw \ write image
                    832:   bit$  @ there 1- cell>bit rshift 1+
                    833:                 r@ write-file throw \ write tags
1.1       anton     834:   r> close-file throw ;
                    835: 
                    836: \ words that should be in minimal
                    837: 
                    838: : + + ;         : 1- 1- ;
                    839: : - - ;         : 2* 2* ;
1.11      pazsan    840: : * * ;         : / / ;
1.1       anton     841: : dup dup ;     : over over ;
                    842: : swap swap ;   : rot rot ;
1.19      pazsan    843: : drop drop ;   : =   = ;
1.13      pazsan    844: : lshift lshift ; : 2/ 2/ ;
1.19      pazsan    845: : . . ;
1.13      pazsan    846: cell constant cell
1.1       anton     847: 
                    848: \ include bug5.fs
                    849: \ only forth also minimal definitions
                    850: 
1.25      pazsan    851: : \  postpone \ ;
                    852: : \G postpone \G ;
                    853: : (  postpone ( ;
1.1       anton     854: : include bl word count included ;
                    855: : .( [char] ) parse type ;
                    856: : cr cr ;
                    857: 
                    858: : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
                    859: only forth also minimal definitions
                    860: 
                    861: \ cross-compiler words
                    862: 
                    863: : decimal       decimal ;
                    864: : hex           hex ;
                    865: 
1.3       pazsan    866: : tudp          T tudp H ;
                    867: : tup           T tup H ;  minimal
1.1       anton     868: 
                    869: \ for debugging...
                    870: : order         order ;
                    871: : words         words ;
                    872: : .s            .s ;
                    873: 
                    874: : bye           bye ;
                    875: 
                    876: \ turnkey direction
                    877: : H forth ; immediate
                    878: : T minimal ; immediate
                    879: : G ghosts ; immediate
                    880: 
                    881: : turnkey  0 set-order also Target definitions
                    882:            also Minimal also ;
                    883: 
                    884: \ these ones are pefered:
                    885: 
                    886: : lock   turnkey ;
                    887: : unlock forth also cross ;
                    888: 
                    889: unlock definitions also minimal
                    890: : lock   lock ;
                    891: lock

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