Annotation of gforth/cross.fs, revision 1.20

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

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