Annotation of gforth/cross.fs, revision 1.30

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

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