Annotation of gforth/cross.fs, revision 1.39

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
1.39    ! pazsan    176: : makekernel ( targetsize -- targetsize )
1.1       anton     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 )
1.39    ! pazsan    197:     \ see kernel.fs:cfaligned
1.22      anton     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 )
1.39    ! pazsan    205:     \ see kernel.fs
1.22      anton     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
1.5       pazsan    281:   dup count [ ' ghosts >body ] ALiteral search-wordlist
1.38      anton     282:   dup IF >r >body nip r>  THEN ;
1.1       anton     283: 
                    284: VARIABLE Already
                    285: 
                    286: : ghost   ( "name" -- ghost )
                    287:   Already off
1.13      pazsan    288:   >in @  bl word gfind   IF  Already on nip EXIT  THEN
1.1       anton     289:   drop  >in !  Make-Ghost ;
                    290: 
                    291: \ resolve                                              14oct92py
                    292: 
                    293: : resolve-loop ( ghost tcfa -- ghost tcfa )
                    294:   >r dup >link @
                    295:   BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
                    296: 
                    297: \ exists                                                9may93jaw
                    298: 
                    299: : exists ( ghost tcfa -- )
                    300:   over GhostNames
                    301:   BEGIN @ dup
                    302:   WHILE 2dup cell+ @ =
                    303:   UNTIL
1.18      pazsan    304:         2 cells + count cr ." CROSS: Exists: " type 4 spaces drop
1.1       anton     305:         swap cell+ !
1.24      pazsan    306:   ELSE  true abort" CROSS: Ghostnames inconsistent "
1.1       anton     307:   THEN ;
                    308: 
                    309: : resolve  ( ghost tcfa -- )
                    310:   over >magic @ <fwd> <>  IF  exists EXIT THEN
                    311:   resolve-loop  over >link ! <res> swap >magic ! ;
                    312: 
                    313: \ gexecute ghost,                                      01nov92py
                    314: 
                    315: : do-forward   ( ghost -- )
                    316:   >link dup @  there rot !  T  A,  H ;
                    317: : do-resolve   ( ghost -- )
                    318:   >link @                   T  A,  H ;
                    319: 
                    320: : gexecute   ( ghost -- )   dup @
                    321:              <fwd> = IF  do-forward  ELSE  do-resolve  THEN ;
                    322: : ghost,     ghost  gexecute ;
                    323: 
                    324: \ .unresolved                                          11may93jaw
                    325: 
                    326: variable ResolveFlag
                    327: 
                    328: \ ?touched                                             11may93jaw
                    329: 
                    330: : ?touched ( ghost -- flag ) dup >magic @ <fwd> = swap >link @
                    331:                                0 <> and ;
                    332: 
                    333: : ?resolved  ( ghostname -- )
                    334:   dup cell+ @ ?touched
                    335:   IF  cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;
                    336: 
                    337: >MINIMAL
                    338: : .unresolved  ( -- )
                    339:   ResolveFlag off cr ." Unresolved: "
                    340:   Ghostnames
                    341:   BEGIN @ dup
                    342:   WHILE dup ?resolved
1.10      anton     343:   REPEAT drop ResolveFlag @
                    344:   IF
1.11      pazsan    345:       abort" Unresolved words!"
1.10      anton     346:   ELSE
                    347:       ." Nothing!"
                    348:   THEN
                    349:   cr ;
1.1       anton     350: 
                    351: >CROSS
                    352: \ Header states                                        12dec92py
                    353: 
                    354: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
                    355: 
                    356: VARIABLE ^imm
                    357: 
                    358: >TARGET
1.36      anton     359: : immediate     40 flag!
1.18      pazsan    360:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
1.1       anton     361:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                    362:                 <imm> ^imm @ ! ;
1.36      anton     363: : restrict      20 flag! ;
1.1       anton     364: >CROSS
                    365: 
                    366: \ ALIAS2 ansforth conform alias                          9may93jaw
                    367: 
                    368: : ALIAS2 create here 0 , DOES> @ execute ;
                    369: \ usage:
1.18      pazsan    370: \ ' <name> alias2 bla !
1.1       anton     371: 
                    372: \ Target Header Creation                               01nov92py
                    373: 
                    374: : string,  ( addr count -- )
1.28      pazsan    375:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
1.22      anton     376: : name,  ( "name" -- )  bl word count string, T cfalign H ;
1.1       anton     377: : view,   ( -- ) ( dummy ) ;
                    378: 
1.25      pazsan    379: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
                    380: 
                    381: s" crossdoc.fd" r/w create-file throw value doc-file-id
                    382: \ contains the file-id of the documentation file
                    383: 
                    384: : \G ( -- )
                    385:     source >in @ /string doc-file-id write-line throw
                    386:     source >in ! drop ; immediate
                    387: 
1.39    ! pazsan    388: Variable to-doc  to-doc on
1.25      pazsan    389: 
                    390: : cross-doc-entry  ( -- )
                    391:     to-doc @ tlast @ 0<> and   \ not an anonymous (i.e. noname) header
                    392:     IF
                    393:        s" " doc-file-id write-line throw
                    394:        s" make-doc " doc-file-id write-file throw
                    395:        tlast @ >image count $1F and doc-file-id write-file throw
                    396:        >in @
                    397:        [char] ( parse 2drop
                    398:        [char] ) parse doc-file-id write-file throw
                    399:        s"  )" doc-file-id write-file throw
                    400:        [char] \ parse 2drop                                    
                    401:        POSTPONE \g
                    402:        >in !
1.39    ! pazsan    403:     THEN ;
1.25      pazsan    404: 
1.28      pazsan    405: \ Target TAGS creation
                    406: 
1.39    ! pazsan    407: s" kernel.TAGS" r/w create-file throw value tag-file-id
1.28      pazsan    408: \ contains the file-id of the tags file
                    409: 
                    410: Create tag-beg 2 c,  7F c, bl c,
                    411: Create tag-end 2 c,  bl c, 01 c,
                    412: Create tag-bof 1 c,  0C c,
                    413: 
                    414: 2variable last-loadfilename 0 0 last-loadfilename 2!
                    415:            
                    416: : put-load-file-name ( -- )
                    417:     loadfilename 2@ last-loadfilename 2@ d<>
                    418:     IF
                    419:        tag-bof count tag-file-id write-line throw
1.31      anton     420:        sourcefilename 2dup
1.28      pazsan    421:        tag-file-id write-file throw
                    422:        last-loadfilename 2!
                    423:        s" ,0" tag-file-id write-line throw
                    424:     THEN ;
                    425: 
                    426: : cross-tag-entry  ( -- )
                    427:     tlast @ 0<>        \ not an anonymous (i.e. noname) header
                    428:     IF
                    429:        put-load-file-name
                    430:        source >in @ min tag-file-id write-file throw
                    431:        tag-beg count tag-file-id write-file throw
                    432:        tlast @ >image count $1F and tag-file-id write-file throw
                    433:        tag-end count tag-file-id write-file throw
1.31      anton     434:        base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
1.28      pazsan    435: \      >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
                    436:        s" ,0" tag-file-id write-line throw
                    437:        base !
                    438:     THEN ;
                    439: 
                    440: \ Target header creation
                    441: 
1.1       anton     442: VARIABLE CreateFlag CreateFlag off
                    443: 
                    444: : (Theader ( "name" -- ghost ) T align H view,
                    445:   tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
                    446:   >in @ name, >in ! T here H tlastcfa !
                    447:   CreateFlag @ IF
1.18      pazsan    448:        >in @ alias2 swap >in !         \ create alias in target
                    449:        >in @ ghost swap >in !
                    450:        swap also ghosts ' previous swap !     \ tick ghost and store in alias
                    451:        CreateFlag off
1.1       anton     452:   ELSE ghost THEN
                    453:   dup >magic ^imm !     \ a pointer for immediate
                    454:   Already @ IF  dup >end tdoes !
                    455:   ELSE 0 tdoes ! THEN
1.25      pazsan    456:   80 flag!
1.28      pazsan    457:   cross-doc-entry cross-tag-entry ;
1.1       anton     458: 
                    459: VARIABLE ;Resolve 1 cells allot
                    460: 
1.11      pazsan    461: : Theader  ( "name" -- ghost )
                    462:   (THeader dup there resolve 0 ;Resolve ! ;
1.1       anton     463: 
                    464: >TARGET
                    465: : Alias    ( cfa -- ) \ name
                    466:   (THeader over resolve T A, H 80 flag! ;
                    467: >CROSS
                    468: 
                    469: \ Conditionals and Comments                            11may93jaw
                    470: 
                    471: : ;Cond
                    472:   postpone ;
                    473:   swap ! ;  immediate
                    474: 
                    475: : Cond: ( -- ) \ name {code } ;
                    476:   atonce on
                    477:   ghost
                    478:   >exec
                    479:   :NONAME ;
                    480: 
                    481: : restrict? ( -- )
                    482: \ aborts on interprete state - ae
                    483:   state @ 0= ABORT" CROSS: Restricted" ;
                    484: 
                    485: : Comment ( -- )
                    486:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
                    487: 
                    488: Comment (       Comment \
                    489: 
                    490: \ Predefined ghosts                                    12dec92py
                    491: 
                    492: ghost 0=                                        drop
                    493: ghost branch    ghost ?branch                   2drop
                    494: ghost (do)      ghost (?do)                     2drop
                    495: ghost (for)                                     drop
                    496: ghost (loop)    ghost (+loop)                   2drop
                    497: ghost (next)                                    drop
1.2       pazsan    498: ghost unloop    ghost ;S                        2drop
1.1       anton     499: ghost lit       ghost (compile) ghost !         2drop drop
1.29      anton     500: ghost (does>)   ghost noop                      2drop
1.1       anton     501: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
1.9       pazsan    502: ghost '
1.1       anton     503: 
                    504: \ compile                                              10may93jaw
                    505: 
                    506: : compile  ( -- ) \ name
                    507:   restrict?
1.13      pazsan    508:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
1.1       anton     509:   0> ( immediate? )
                    510:   IF    >exec @ compile,
                    511:   ELSE  postpone literal postpone gexecute  THEN ;
                    512:                                         immediate
                    513: 
                    514: >TARGET
1.13      pazsan    515: : '  ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined "
1.1       anton     516:   dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
                    517: 
                    518: Cond: [']  compile lit ghost gexecute ;Cond
1.14      anton     519: 
                    520: Cond: chars ;Cond
1.1       anton     521: 
                    522: >CROSS
                    523: \ tLiteral                                             12dec92py
                    524: 
                    525: : lit, ( n -- )   compile lit T  ,  H ;
                    526: : alit, ( n -- )  compile lit T A,  H ;
                    527: 
                    528: >TARGET
                    529: Cond:  Literal ( n -- )   restrict? lit, ;Cond
                    530: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
                    531: 
                    532: : Char ( "<char>" -- )  bl word char+ c@ ;
                    533: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
                    534: 
                    535: >CROSS
                    536: \ Target compiling loop                                12dec92py
                    537: \ ">tib trick thrown out                               10may93jaw
                    538: \ number? defined at the top                           11may93jaw
                    539: 
                    540: \ compiled word might leave items on stack!
                    541: : tcom ( in name -- )
                    542:   gfind  ?dup  IF    0> IF    nip >exec @ execute
                    543:                         ELSE  nip gexecute  THEN EXIT THEN
                    544:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
                    545:                ELSE  2drop >in !
                    546:                ghost gexecute THEN  ;
                    547: 
                    548: >TARGET
                    549: \ : ; DOES>                                            13dec92py
                    550: \ ]                                                     9may93py/jaw
                    551: 
                    552: : ] state on
                    553:     BEGIN
1.13      pazsan    554:         BEGIN >in @ bl word
1.1       anton     555:               dup c@ 0= WHILE 2drop refill 0=
                    556:               ABORT" CROSS: End of file while target compiling"
                    557:         REPEAT
                    558:         tcom
                    559:         state @
                    560:         0=
                    561:     UNTIL ;
                    562: 
                    563: \ by the way: defining a second interpreter (a compiler-)loop
                    564: \             is not allowed if a system should be ans conform
                    565: 
                    566: : : ( -- colon-sys ) \ Name
                    567:   (THeader ;Resolve ! there ;Resolve cell+ !
                    568:   docol, depth T ] H ;
                    569: 
1.37      pazsan    570: : :noname ( -- colon-sys )
                    571:   T align H there docol, depth T ] H ;
                    572: 
1.2       pazsan    573: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
1.6       anton     574: 
                    575: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1.2       pazsan    576: 
1.1       anton     577: Cond: ; ( -- ) restrict?
                    578:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                    579:                           ELSE true ABORT" CROSS: Stack empty" THEN
1.2       pazsan    580:                compile ;S state off
1.1       anton     581:                ;Resolve @
                    582:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
                    583:                ;Cond
                    584: Cond: [  restrict? state off ;Cond
                    585: 
                    586: >CROSS
                    587: : !does  :dodoes tlastcfa @ tuck T ! cell+ ! H ;
                    588: 
                    589: >TARGET
                    590: Cond: DOES> restrict?
1.29      anton     591:         compile (does>) dodoes, tdoes @ ?dup IF  @ T here H resolve THEN
1.1       anton     592:         ;Cond
                    593: : DOES> dodoes, T here H !does depth T ] H ;
                    594: 
                    595: >CROSS
                    596: \ Creation                                             01nov92py
                    597: 
                    598: \ Builder                                               11may93jaw
                    599: 
                    600: : Builder    ( Create do: "name" -- )
                    601:   >in @ alias2 swap dup >in ! >r >r
                    602:   Make-Ghost rot swap >exec ! ,
                    603:   r> r> >in !
1.11      pazsan    604:   also ghosts ' previous swap ! ;
                    605: \  DOES>  dup >exec @ execute ;
1.1       anton     606: 
                    607: : gdoes,  ( ghost -- )  >end @ dup >magic @ <fwd> <>
                    608:   IF dup >link @ dup 0< IF T A, 0 , H drop EXIT THEN drop THEN
1.4       pazsan    609:   :dodoes T A, H gexecute T here H cell - reloff ;
1.1       anton     610: 
1.11      pazsan    611: : TCreate ( -- )
                    612:   last-ghost @
1.1       anton     613:   CreateFlag on
1.11      pazsan    614:   Theader >r dup gdoes,
                    615:   >end @ >exec @ r> >exec ! ;
1.1       anton     616: 
                    617: : Build:  ( -- [xt] [colon-sys] )
                    618:   :noname  postpone TCreate ;
                    619: 
                    620: : gdoes>  ( ghost -- addr flag )
1.11      pazsan    621:   last-ghost @
1.1       anton     622:   state @ IF  gexecute true EXIT  THEN
                    623:   cell+ @ T >body H false ;
                    624: 
                    625: \ DO: ;DO                                               11may93jaw
                    626: \ changed to ?EXIT                                      10may93jaw
                    627: 
                    628: : DO:     ( -- addr [xt] [colon-sys] )
                    629:   here ghostheader
1.11      pazsan    630:   :noname postpone gdoes> postpone ?EXIT ;
1.1       anton     631: 
                    632: : ;DO ( addr [xt] [colon-sys] -- )
                    633:   postpone ;    ( S addr xt )
                    634:   over >exec ! ; immediate
                    635: 
                    636: : by      ( -- addr ) \ Name
                    637:   ghost >end @ ;
                    638: 
                    639: >TARGET
                    640: \ Variables and Constants                              05dec92py
                    641: 
                    642: Build:  ;
                    643: DO: ( ghost -- addr ) ;DO
                    644: Builder Create
                    645: by Create :dovar resolve
                    646: 
                    647: Build: T 0 , H ;
                    648: by Create
                    649: Builder Variable
                    650: 
                    651: Build: T 0 A, H ;
                    652: by Create
                    653: Builder AVariable
                    654: 
1.3       pazsan    655: \ User variables                                       04may94py
                    656: 
                    657: >CROSS
                    658: Variable tup  0 tup !
                    659: Variable tudp 0 tudp !
                    660: : u,  ( n -- udp )
                    661:   tup @ tudp @ + T  ! H
1.19      pazsan    662:   tudp @ dup T cell+ H tudp ! ;
1.3       pazsan    663: : au, ( n -- udp )
                    664:   tup @ tudp @ + T A! H
1.19      pazsan    665:   tudp @ dup T cell+ H tudp ! ;
1.3       pazsan    666: >TARGET
                    667: 
                    668: Build: T 0 u, , H ;
                    669: DO: ( ghost -- up-addr )  T @ H tup @ + ;DO
1.1       anton     670: Builder User
1.3       pazsan    671: by User :douser resolve
1.1       anton     672: 
1.3       pazsan    673: Build: T 0 u, , 0 u, drop H ;
                    674: by User
1.1       anton     675: Builder 2User
                    676: 
1.3       pazsan    677: Build: T 0 au, , H ;
                    678: by User
1.1       anton     679: Builder AUser
                    680: 
                    681: Build:  ( n -- ) T , H ;
                    682: DO: ( ghost -- n ) T @ H ;DO
                    683: Builder Constant
                    684: by Constant :docon resolve
                    685: 
                    686: Build:  ( n -- ) T A, H ;
                    687: by Constant
                    688: Builder AConstant
                    689: 
1.24      pazsan    690: Build:  ( d -- ) T , , H ;
                    691: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
                    692: Builder 2Constant
                    693: 
1.1       anton     694: Build: T 0 , H ;
                    695: by Constant
                    696: Builder Value
                    697: 
1.32      pazsan    698: Build: T 0 A, H ;
                    699: by Constant
                    700: Builder AValue
                    701: 
1.1       anton     702: Build:  ( -- ) compile noop ;
                    703: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
                    704: Builder Defer
1.10      anton     705: by Defer :dodefer resolve
1.37      pazsan    706: 
                    707: Build:  ( inter comp -- ) swap T immediate A, A, H ;
                    708: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1.38      anton     709: Builder interpret/compile:
1.24      pazsan    710: 
                    711: \ Sturctures                                           23feb95py
                    712: 
                    713: >CROSS
                    714: : nalign ( addr1 n -- addr2 )
                    715: \ addr2 is the aligned version of addr1 wrt the alignment size n
                    716:  1- tuck +  swap invert and ;
                    717: >TARGET
                    718: 
                    719: Build:         >r rot r@ nalign  dup T , H  ( align1 size offset )
                    720:        + swap r> nalign ;
                    721: DO: T @ H + ;DO
                    722: Builder Field
1.29      anton     723: by Field :dofield resolve
1.24      pazsan    724: 
                    725: : struct  T 0 1 chars H ;
                    726: : end-struct  T 2Constant H ;
                    727: 
                    728: : cells: ( n -- size align )
                    729:     T cells 1 cells H ;
                    730: 
                    731: \ ' 2Constant Alias2 end-struct
                    732: \ 0 1 T Chars H 2Constant struct
1.1       anton     733: 
                    734: \ structural conditionals                              17dec92py
                    735: 
                    736: >CROSS
                    737: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
                    738: : sys?        ( sys -- sys )    dup 0= ?struc ;
                    739: : >mark       ( -- sys )        T here  0 , H ;
                    740: : >resolve    ( sys -- )        T here over - swap ! H ;
                    741: : <resolve    ( sys -- )        T here - , H ;
                    742: >TARGET
                    743: 
                    744: \ Structural Conditionals                              12dec92py
                    745: 
                    746: Cond: BUT       restrict? sys? swap ;Cond
                    747: Cond: YET       restrict? sys? dup ;Cond
                    748: 
                    749: >CROSS
                    750: Variable tleavings
                    751: >TARGET
                    752: 
                    753: Cond: DONE   ( addr -- )  restrict? tleavings @
                    754:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
                    755:       tleavings ! drop ;Cond
                    756: 
                    757: >CROSS
                    758: : (leave  T here H tleavings @ T , H  tleavings ! ;
                    759: >TARGET
                    760: 
                    761: Cond: LEAVE     restrict? compile branch (leave ;Cond
                    762: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond
                    763: 
                    764: \ Structural Conditionals                              12dec92py
                    765: 
                    766: Cond: AHEAD     restrict? compile branch >mark ;Cond
                    767: Cond: IF        restrict? compile ?branch >mark ;Cond
                    768: Cond: THEN      restrict? sys? dup T @ H ?struc >resolve ;Cond
                    769: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
                    770: 
                    771: Cond: BEGIN     restrict? T here H ;Cond
                    772: Cond: WHILE     restrict? sys? compile IF swap ;Cond
                    773: Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
                    774: Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
                    775: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
                    776: 
                    777: \ Structural Conditionals                              12dec92py
                    778: 
                    779: Cond: DO        restrict? compile (do)   T here H ;Cond
                    780: Cond: ?DO       restrict? compile (?do)  (leave T here H ;Cond
                    781: Cond: FOR       restrict? compile (for)  T here H ;Cond
                    782: 
                    783: >CROSS
                    784: : loop]   dup <resolve cell - compile DONE compile unloop ;
                    785: >TARGET
                    786: 
                    787: Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
                    788: Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
                    789: Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond
                    790: 
                    791: \ String words                                         23feb93py
                    792: 
                    793: : ,"            [char] " parse string, T align H ;
                    794: 
                    795: Cond: ."        restrict? compile (.")     T ," H ;Cond
                    796: Cond: S"        restrict? compile (S")     T ," H ;Cond
                    797: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
                    798: 
                    799: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
                    800: : IS            T ' >body ! H ;
1.9       pazsan    801: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
                    802: : TO            T ' >body ! H ;
1.1       anton     803: 
                    804: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
                    805: 
                    806: \ linked list primitive
                    807: : linked        T here over @ A, swap ! H ;
                    808: 
                    809: : err"   s" ErrLink linked" evaluate T , H
                    810:          [char] " parse string, T align H ;
                    811: 
                    812: : env"  [char] " parse s" EnvLink linked" evaluate
                    813:         string, T align , H ;
                    814: 
                    815: : 2env" [char] " parse s" EnvLink linked" evaluate
                    816:         here >r string, T align , , H
                    817:         r> dup T c@ H 80 and swap T c! H ;
                    818: 
                    819: \ compile must be last                                 22feb93py
                    820: 
                    821: Cond: compile ( -- ) restrict? \ name
1.13      pazsan    822:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1       anton     823:       0> IF    gexecute
                    824:          ELSE  dup >magic @ <imm> =
                    825:                IF   gexecute
                    826:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    827: 
                    828: Cond: postpone ( -- ) restrict? \ name
1.13      pazsan    829:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1       anton     830:       0> IF    gexecute
                    831:          ELSE  dup >magic @ <imm> =
                    832:                IF   gexecute
                    833:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    834: 
                    835: >MINIMAL
                    836: also minimal
                    837: \ Usefull words                                        13feb93py
                    838: 
                    839: : KB  400 * ;
                    840: 
                    841: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
                    842: 
1.13      pazsan    843: : there? bl word gfind IF >magic @ <fwd> <> ELSE drop false THEN ;
1.1       anton     844: 
                    845: : [IFDEF] there? postpone [IF] ;
                    846: : [IFUNDEF] there? 0= postpone [IF] ;
                    847: 
                    848: \ C: \- \+ Conditional Compiling                         09jun93jaw
                    849: 
                    850: : C: >in @ there? 0=
                    851:      IF    >in ! T : H
                    852:      ELSE drop
                    853:         BEGIN bl word dup c@
                    854:               IF   count comment? s" ;" compare 0= ?EXIT
                    855:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
                    856:               THEN
                    857:         AGAIN
                    858:      THEN ;
                    859: 
                    860: also minimal
                    861: 
                    862: : \- there? IF postpone \ THEN ;
                    863: : \+ there? 0= IF postpone \ THEN ;
                    864: 
                    865: : [IF]   postpone [IF] ;
                    866: : [THEN] postpone [THEN] ;
                    867: : [ELSE] postpone [ELSE] ;
                    868: 
                    869: Cond: [IF]      [IF] ;Cond
                    870: Cond: [IFDEF]   [IFDEF] ;Cond
                    871: Cond: [IFUNDEF] [IFUNDEF] ;Cond
                    872: Cond: [THEN]    [THEN] ;Cond
                    873: Cond: [ELSE]    [ELSE] ;Cond
                    874: 
                    875: \ save-cross                                           17mar93py
                    876: 
                    877: \ i'm not interested in bigforth features this time    10may93jaw
                    878: \ [IFDEF] file
                    879: \ also file
                    880: \ [THEN]
                    881: \ included throw after create-file                     11may93jaw
                    882: 
1.12      anton     883: bigendian Constant bigendian
1.1       anton     884: 
1.34      anton     885: Create magic  s" Gforth10" here over allot swap move
1.26      pazsan    886: 
1.35      anton     887: char 1 bigendian + cell + magic 7 + c!
1.26      pazsan    888: 
1.34      anton     889: : save-cross ( "image-name" "binary-name" -- )
                    890:   bl parse ." Saving to " 2dup type cr
1.1       anton     891:   w/o bin create-file throw >r
1.34      anton     892:   s" #! "  r@ write-file throw
                    893:   bl parse r@ write-file throw
                    894:   s"  -i"  r@ write-file throw
                    895:   #lf      r@ emit-file throw
                    896:   r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
                    897:   ?do
                    898:       bl over emit-file throw
                    899:   loop
                    900:   drop
1.26      pazsan    901:   magic 8       r@ write-file throw \ write magic
1.16      pazsan    902:   image @ there r@ write-file throw \ write image
                    903:   bit$  @ there 1- cell>bit rshift 1+
                    904:                 r@ write-file throw \ write tags
1.1       anton     905:   r> close-file throw ;
                    906: 
                    907: \ words that should be in minimal
                    908: 
                    909: : + + ;         : 1- 1- ;
                    910: : - - ;         : 2* 2* ;
1.11      pazsan    911: : * * ;         : / / ;
1.1       anton     912: : dup dup ;     : over over ;
                    913: : swap swap ;   : rot rot ;
1.19      pazsan    914: : drop drop ;   : =   = ;
1.13      pazsan    915: : lshift lshift ; : 2/ 2/ ;
1.19      pazsan    916: : . . ;
1.13      pazsan    917: cell constant cell
1.1       anton     918: 
                    919: \ include bug5.fs
                    920: \ only forth also minimal definitions
                    921: 
1.25      pazsan    922: : \  postpone \ ;
                    923: : \G postpone \G ;
                    924: : (  postpone ( ;
1.1       anton     925: : include bl word count included ;
                    926: : .( [char] ) parse type ;
                    927: : cr cr ;
                    928: 
                    929: : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
                    930: only forth also minimal definitions
                    931: 
                    932: \ cross-compiler words
                    933: 
                    934: : decimal       decimal ;
                    935: : hex           hex ;
                    936: 
1.3       pazsan    937: : tudp          T tudp H ;
1.39    ! pazsan    938: : tup           T tup H ;
        !           939: 
        !           940: : doc-off       false T to-doc H ! ;
        !           941: : doc-on        true  T to-doc H ! ;
        !           942: 
        !           943: minimal
1.1       anton     944: 
                    945: \ for debugging...
                    946: : order         order ;
                    947: : words         words ;
                    948: : .s            .s ;
                    949: 
                    950: : bye           bye ;
                    951: 
                    952: \ turnkey direction
                    953: : H forth ; immediate
                    954: : T minimal ; immediate
                    955: : G ghosts ; immediate
                    956: 
                    957: : turnkey  0 set-order also Target definitions
                    958:            also Minimal also ;
                    959: 
                    960: \ these ones are pefered:
                    961: 
                    962: : lock   turnkey ;
                    963: : unlock forth also cross ;
                    964: 
                    965: unlock definitions also minimal
                    966: : lock   lock ;
                    967: lock

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