Annotation of gforth/cross.fs, revision 1.45

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: 
1.43      pazsan     94: \ Parameter for target systems                         06oct92py
                     95: 
                     96: mach-file count included
                     97: 
                     98: also Forth definitions
                     99: 
                    100: [IFDEF] asm-include asm-include [THEN]
                    101: 
                    102: previous
                    103: 
                    104: >CROSS
                    105: 
1.1       anton     106: \ Variables                                            06oct92py
                    107: 
                    108: Variable image
                    109: Variable tlast    NIL tlast !  \ Last name field
                    110: Variable tlastcfa \ Last code field
                    111: Variable tdoes    \ Resolve does> calls
                    112: Variable bit$
                    113: Variable tdp
                    114: : there  tdp @ ;
                    115: 
1.19      pazsan    116: \ Create additional parameters                         19jan95py
                    117: 
                    118: T
                    119: cell               Constant tcell
                    120: cell<<             Constant tcell<<
                    121: cell>bit           Constant tcell>bit
                    122: bits/byte          Constant tbits/byte
                    123: float              Constant tfloat
                    124: 1 bits/byte lshift Constant maxbyte
                    125: H
                    126: 
1.1       anton     127: >TARGET
                    128: 
                    129: \ Byte ordering and cell size                          06oct92py
                    130: 
1.19      pazsan    131: : cell+         tcell + ;
                    132: : cells         tcell<< lshift ;
1.1       anton     133: : chars         ;
1.19      pazsan    134: : floats       tfloat * ;
1.6       anton     135:     
1.1       anton     136: >CROSS
1.19      pazsan    137: : cell/         tcell<< rshift ;
1.1       anton     138: >TARGET
                    139: 20 CONSTANT bl
                    140: -1 Constant NIL
                    141: 
                    142: >CROSS
                    143: 
1.20      pazsan    144: bigendian
                    145: [IF]
                    146:    : T!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
                    147:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
                    148:    : T@  ( addr -- n )  >r 0 0 r> tcell bounds
                    149:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
1.19      pazsan    150: [ELSE]
1.20      pazsan    151:    : T!  ( n addr -- )  >r s>d r> tcell bounds
                    152:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
                    153:    : T@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
                    154:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
1.1       anton     155: [THEN]
                    156: 
                    157: \ Memory initialisation                                05dec92py
                    158: \ Fixed bug in else part                               11may93jaw
                    159: 
                    160: [IFDEF] Memory \ Memory is a bigFORTH feature
1.5       pazsan    161:    also Memory
1.1       anton     162:    : initmem ( var len -- )
                    163:      2dup swap handle! >r @ r> erase ;
1.5       pazsan    164:    toss
1.1       anton     165: [ELSE]
                    166:    : initmem ( var len -- )
                    167:      tuck allocate abort" CROSS: No memory for target"
                    168:      ( len var adr ) dup rot !
                    169:      ( len adr ) swap erase ;
                    170: [THEN]
                    171: 
                    172: \ MakeKernal                                           12dec92py
                    173: 
                    174: >MINIMAL
1.39      pazsan    175: : makekernel ( targetsize -- targetsize )
1.1       anton     176:   bit$  over 1- cell>bit rshift 1+ initmem
                    177:   image over initmem tdp off ;
                    178: 
                    179: >CROSS
                    180: \ Bit string manipulation                               06oct92py
                    181: \                                                       9may93jaw
                    182: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
                    183: : bits ( n -- n ) chars Bittable + c@ ;
                    184: 
                    185: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
                    186: : +bit ( addr n -- )  >bit over c@ or swap c! ;
1.4       pazsan    187: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
1.1       anton     188: : relon ( taddr -- )  bit$ @ swap cell/ +bit ;
1.4       pazsan    189: : reloff ( taddr -- )  bit$ @ swap cell/ -bit ;
1.1       anton     190: 
                    191: \ Target memory access                                 06oct92py
                    192: 
                    193: : align+  ( taddr -- rest )
                    194:     cell tuck 1- and - [ cell 1- ] Literal and ;
1.22      anton     195: : cfalign+  ( taddr -- rest )
1.39      pazsan    196:     \ see kernel.fs:cfaligned
1.43      pazsan    197:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
1.1       anton     198: 
                    199: >TARGET
                    200: : aligned ( taddr -- ta-addr )  dup align+ + ;
                    201: \ assumes cell alignment granularity (as GNU C)
                    202: 
1.22      anton     203: : cfaligned ( taddr1 -- taddr2 )
1.39      pazsan    204:     \ see kernel.fs
1.22      anton     205:     dup cfalign+ + ;
                    206: 
1.1       anton     207: >CROSS
                    208: : >image ( taddr -- absaddr )  image @ + ;
                    209: >TARGET
1.19      pazsan    210: : @  ( taddr -- w )     >image t@ ;
                    211: : !  ( w taddr -- )     >image t! ;
1.1       anton     212: : c@ ( taddr -- char )  >image c@ ;
                    213: : c! ( char taddr -- )  >image c! ;
1.7       anton     214: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
                    215: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
1.1       anton     216: 
                    217: \ Target compilation primitives                        06oct92py
                    218: \ included A!                                          16may93jaw
                    219: 
                    220: : here  ( -- there )    there ;
                    221: : allot ( n -- )        tdp +! ;
                    222: : ,     ( w -- )        T here H cell T allot  ! H ;
                    223: : c,    ( char -- )     T here    1 allot c! H ;
                    224: : align ( -- )          T here H align+ 0 ?DO  bl T c, H LOOP ;
1.22      anton     225: : cfalign ( -- )
                    226:     T here H cfalign+ 0 ?DO  bl T c, H LOOP ;
1.1       anton     227: 
                    228: : A!                    dup relon T ! H ;
                    229: : A,    ( w -- )        T here H relon T , H ;
                    230: 
                    231: >CROSS
                    232: 
                    233: \ threading modell                                     13dec92py
                    234: 
                    235: >TARGET
                    236: : >body   ( cfa -- pfa ) T cell+ cell+ H ;
                    237: >CROSS
                    238: 
                    239: \ Ghost Builder                                        06oct92py
                    240: 
                    241: \ <T T> new version with temp variable                 10may93jaw
                    242: 
                    243: VARIABLE VocTemp
                    244: 
                    245: : <T  get-current VocTemp ! also Ghosts definitions ;
                    246: : T>  previous VocTemp @ set-current ;
                    247: 
                    248: 4711 Constant <fwd>             4712 Constant <res>
1.42      pazsan    249: 4713 Constant <imm>             4714 Constant <do:>
1.1       anton     250: 
                    251: \ iForth makes only immediate directly after create
                    252: \ make atonce trick! ?
                    253: 
                    254: Variable atonce atonce off
                    255: 
                    256: : NoExec true ABORT" CROSS: Don't execute ghost" ;
                    257: 
                    258: : GhostHeader <fwd> , 0 , ['] NoExec , ;
                    259: 
1.43      pazsan    260: : >magic ;
                    261: : >link cell+ ;
                    262: : >exec cell+ cell+ ;
1.1       anton     263: : >end 3 cells + ;
                    264: 
1.11      pazsan    265: Variable last-ghost
1.1       anton     266: : Make-Ghost ( "name" -- ghost )
                    267:   >in @ GhostName swap >in !
                    268:   <T Create atonce @ IF immediate atonce off THEN
                    269:   here tuck swap ! ghostheader T>
1.11      pazsan    270:   DOES> dup last-ghost ! >exec @ execute ;
1.1       anton     271: 
                    272: \ ghost words                                          14oct92py
                    273: \                                          changed:    10may93py/jaw
                    274: 
                    275: : gfind   ( string -- ghost true/1 / string false )
                    276: \ searches for string in word-list ghosts
1.5       pazsan    277:   dup count [ ' ghosts >body ] ALiteral search-wordlist
1.38      anton     278:   dup IF >r >body nip r>  THEN ;
1.1       anton     279: 
                    280: VARIABLE Already
                    281: 
                    282: : ghost   ( "name" -- ghost )
                    283:   Already off
1.13      pazsan    284:   >in @  bl word gfind   IF  Already on nip EXIT  THEN
1.1       anton     285:   drop  >in !  Make-Ghost ;
                    286: 
                    287: \ resolve                                              14oct92py
                    288: 
                    289: : resolve-loop ( ghost tcfa -- ghost tcfa )
                    290:   >r dup >link @
                    291:   BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
                    292: 
                    293: \ exists                                                9may93jaw
                    294: 
                    295: : exists ( ghost tcfa -- )
                    296:   over GhostNames
                    297:   BEGIN @ dup
                    298:   WHILE 2dup cell+ @ =
                    299:   UNTIL
1.18      pazsan    300:         2 cells + count cr ." CROSS: Exists: " type 4 spaces drop
1.1       anton     301:         swap cell+ !
1.24      pazsan    302:   ELSE  true abort" CROSS: Ghostnames inconsistent "
1.1       anton     303:   THEN ;
                    304: 
                    305: : resolve  ( ghost tcfa -- )
                    306:   over >magic @ <fwd> <>  IF  exists EXIT THEN
                    307:   resolve-loop  over >link ! <res> swap >magic ! ;
                    308: 
                    309: \ gexecute ghost,                                      01nov92py
                    310: 
                    311: : do-forward   ( ghost -- )
                    312:   >link dup @  there rot !  T  A,  H ;
                    313: : do-resolve   ( ghost -- )
                    314:   >link @                   T  A,  H ;
                    315: 
                    316: : gexecute   ( ghost -- )   dup @
                    317:              <fwd> = IF  do-forward  ELSE  do-resolve  THEN ;
                    318: : ghost,     ghost  gexecute ;
                    319: 
                    320: \ .unresolved                                          11may93jaw
                    321: 
                    322: variable ResolveFlag
                    323: 
                    324: \ ?touched                                             11may93jaw
                    325: 
                    326: : ?touched ( ghost -- flag ) dup >magic @ <fwd> = swap >link @
                    327:                                0 <> and ;
                    328: 
                    329: : ?resolved  ( ghostname -- )
                    330:   dup cell+ @ ?touched
                    331:   IF  cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;
                    332: 
                    333: >MINIMAL
                    334: : .unresolved  ( -- )
                    335:   ResolveFlag off cr ." Unresolved: "
                    336:   Ghostnames
                    337:   BEGIN @ dup
                    338:   WHILE dup ?resolved
1.10      anton     339:   REPEAT drop ResolveFlag @
                    340:   IF
1.11      pazsan    341:       abort" Unresolved words!"
1.10      anton     342:   ELSE
                    343:       ." Nothing!"
                    344:   THEN
                    345:   cr ;
1.1       anton     346: 
                    347: >CROSS
                    348: \ Header states                                        12dec92py
                    349: 
                    350: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
                    351: 
                    352: VARIABLE ^imm
                    353: 
                    354: >TARGET
1.36      anton     355: : immediate     40 flag!
1.18      pazsan    356:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
1.1       anton     357:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                    358:                 <imm> ^imm @ ! ;
1.36      anton     359: : restrict      20 flag! ;
1.1       anton     360: >CROSS
                    361: 
                    362: \ ALIAS2 ansforth conform alias                          9may93jaw
                    363: 
                    364: : ALIAS2 create here 0 , DOES> @ execute ;
                    365: \ usage:
1.18      pazsan    366: \ ' <name> alias2 bla !
1.1       anton     367: 
                    368: \ Target Header Creation                               01nov92py
                    369: 
                    370: : string,  ( addr count -- )
1.28      pazsan    371:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
1.22      anton     372: : name,  ( "name" -- )  bl word count string, T cfalign H ;
1.1       anton     373: : view,   ( -- ) ( dummy ) ;
                    374: 
1.25      pazsan    375: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
                    376: 
                    377: s" crossdoc.fd" r/w create-file throw value doc-file-id
                    378: \ contains the file-id of the documentation file
                    379: 
1.40      pazsan    380: : T-\G ( -- )
1.25      pazsan    381:     source >in @ /string doc-file-id write-line throw
1.40      pazsan    382:     postpone \ ;
1.25      pazsan    383: 
1.39      pazsan    384: Variable to-doc  to-doc on
1.25      pazsan    385: 
                    386: : cross-doc-entry  ( -- )
                    387:     to-doc @ tlast @ 0<> and   \ not an anonymous (i.e. noname) header
                    388:     IF
                    389:        s" " doc-file-id write-line throw
                    390:        s" make-doc " doc-file-id write-file throw
                    391:        tlast @ >image count $1F and doc-file-id write-file throw
                    392:        >in @
                    393:        [char] ( parse 2drop
                    394:        [char] ) parse doc-file-id write-file throw
                    395:        s"  )" doc-file-id write-file throw
                    396:        [char] \ parse 2drop                                    
1.40      pazsan    397:        T-\G
1.25      pazsan    398:        >in !
1.39      pazsan    399:     THEN ;
1.25      pazsan    400: 
1.28      pazsan    401: \ Target TAGS creation
                    402: 
1.39      pazsan    403: s" kernel.TAGS" r/w create-file throw value tag-file-id
1.28      pazsan    404: \ contains the file-id of the tags file
                    405: 
                    406: Create tag-beg 2 c,  7F c, bl c,
                    407: Create tag-end 2 c,  bl c, 01 c,
                    408: Create tag-bof 1 c,  0C c,
                    409: 
                    410: 2variable last-loadfilename 0 0 last-loadfilename 2!
                    411:            
                    412: : put-load-file-name ( -- )
                    413:     loadfilename 2@ last-loadfilename 2@ d<>
                    414:     IF
                    415:        tag-bof count tag-file-id write-line throw
1.31      anton     416:        sourcefilename 2dup
1.28      pazsan    417:        tag-file-id write-file throw
                    418:        last-loadfilename 2!
                    419:        s" ,0" tag-file-id write-line throw
                    420:     THEN ;
                    421: 
                    422: : cross-tag-entry  ( -- )
                    423:     tlast @ 0<>        \ not an anonymous (i.e. noname) header
                    424:     IF
                    425:        put-load-file-name
                    426:        source >in @ min tag-file-id write-file throw
                    427:        tag-beg count tag-file-id write-file throw
                    428:        tlast @ >image count $1F and tag-file-id write-file throw
                    429:        tag-end count tag-file-id write-file throw
1.31      anton     430:        base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
1.28      pazsan    431: \      >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
                    432:        s" ,0" tag-file-id write-line throw
                    433:        base !
                    434:     THEN ;
                    435: 
1.43      pazsan    436: \ Check for words
                    437: 
                    438: Defer skip? ' false IS skip?
                    439: 
                    440: : defined? ( -- flag ) \ name
                    441:     ghost >magic @ <fwd> <> ;
                    442: 
                    443: : needed? ( -- flag ) \ name
                    444:     ghost dup >magic @ <fwd> =
                    445:     IF  >link @ 0<>  ELSE  drop false  THEN ;
                    446: 
1.44      pazsan    447: : doer? ( -- flag ) \ name
                    448:     ghost >magic @ <do:> = ;
                    449: 
1.43      pazsan    450: : skip-defs ( -- )
                    451:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
                    452: 
1.28      pazsan    453: \ Target header creation
                    454: 
1.1       anton     455: VARIABLE CreateFlag CreateFlag off
                    456: 
1.43      pazsan    457: : (Theader ( "name" -- ghost )
                    458: \  >in @ bl word count type 2 spaces >in !
                    459:   T align H view,
1.1       anton     460:   tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
                    461:   >in @ name, >in ! T here H tlastcfa !
                    462:   CreateFlag @ IF
1.18      pazsan    463:        >in @ alias2 swap >in !         \ create alias in target
                    464:        >in @ ghost swap >in !
                    465:        swap also ghosts ' previous swap !     \ tick ghost and store in alias
                    466:        CreateFlag off
1.1       anton     467:   ELSE ghost THEN
                    468:   dup >magic ^imm !     \ a pointer for immediate
                    469:   Already @ IF  dup >end tdoes !
                    470:   ELSE 0 tdoes ! THEN
1.25      pazsan    471:   80 flag!
1.28      pazsan    472:   cross-doc-entry cross-tag-entry ;
1.1       anton     473: 
                    474: VARIABLE ;Resolve 1 cells allot
                    475: 
1.11      pazsan    476: : Theader  ( "name" -- ghost )
                    477:   (THeader dup there resolve 0 ;Resolve ! ;
1.1       anton     478: 
                    479: >TARGET
                    480: : Alias    ( cfa -- ) \ name
1.43      pazsan    481:     >in @ skip? IF  2drop  EXIT  THEN  >in !
                    482:     dup 0< has-prims 0= and
                    483:     IF
                    484:        ." needs prim: " >in @ bl word count type >in ! cr
                    485:     THEN
                    486:     (THeader over resolve T A, H 80 flag! ;
1.42      pazsan    487: : Alias:   ( cfa -- ) \ name
1.43      pazsan    488:     >in @ skip? IF  2drop  EXIT  THEN  >in !
                    489:     dup 0< has-prims 0= and
                    490:     IF
                    491:        ." needs doer: " >in @ bl word count type >in ! cr
                    492:     THEN
                    493:     ghost tuck swap resolve <do:> swap >magic ! ;
1.1       anton     494: >CROSS
                    495: 
                    496: \ Conditionals and Comments                            11may93jaw
                    497: 
                    498: : ;Cond
                    499:   postpone ;
                    500:   swap ! ;  immediate
                    501: 
                    502: : Cond: ( -- ) \ name {code } ;
                    503:   atonce on
                    504:   ghost
                    505:   >exec
                    506:   :NONAME ;
                    507: 
                    508: : restrict? ( -- )
                    509: \ aborts on interprete state - ae
                    510:   state @ 0= ABORT" CROSS: Restricted" ;
                    511: 
                    512: : Comment ( -- )
                    513:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
                    514: 
                    515: Comment (       Comment \
                    516: 
                    517: \ Predefined ghosts                                    12dec92py
                    518: 
                    519: ghost 0=                                        drop
                    520: ghost branch    ghost ?branch                   2drop
                    521: ghost (do)      ghost (?do)                     2drop
                    522: ghost (for)                                     drop
                    523: ghost (loop)    ghost (+loop)                   2drop
                    524: ghost (next)                                    drop
1.2       pazsan    525: ghost unloop    ghost ;S                        2drop
1.1       anton     526: ghost lit       ghost (compile) ghost !         2drop drop
1.29      anton     527: ghost (does>)   ghost noop                      2drop
1.1       anton     528: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
1.41      pazsan    529: ghost '                                         drop
1.42      pazsan    530: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
1.45    ! pazsan    531: ghost over      ghost =         ghost drop      2drop drop
1.1       anton     532: 
                    533: \ compile                                              10may93jaw
                    534: 
                    535: : compile  ( -- ) \ name
                    536:   restrict?
1.13      pazsan    537:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
1.1       anton     538:   0> ( immediate? )
                    539:   IF    >exec @ compile,
                    540:   ELSE  postpone literal postpone gexecute  THEN ;
                    541:                                         immediate
                    542: 
1.42      pazsan    543: \ generic threading modell
                    544: : docol,  ( -- ) compile :docol T 0 , H ;
                    545: 
                    546: : dodoes, ( -- ) compile :doesjump T 0 , H ;
                    547: 
1.43      pazsan    548: [IFUNDEF] (code) 
                    549: Defer (code)
                    550: Defer (end-code)
                    551: [THEN]
                    552: 
1.1       anton     553: >TARGET
1.43      pazsan    554: : Code
                    555:     (THeader there resolve
                    556:     there 2 T cells H + T a, 0 , H
                    557:     depth (code) ;
                    558: 
                    559: : Code:
                    560:     ghost dup there resolve  <do:> swap >magic !
                    561:     depth (code) ;
                    562: 
                    563: : end-code
                    564:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                    565:     ELSE true ABORT" CROSS: Stack empty" THEN
                    566:     (end-code) ;
                    567:                
1.13      pazsan    568: : '  ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined "
1.1       anton     569:   dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
                    570: 
                    571: Cond: [']  compile lit ghost gexecute ;Cond
1.14      anton     572: 
                    573: Cond: chars ;Cond
1.1       anton     574: 
                    575: >CROSS
                    576: \ tLiteral                                             12dec92py
                    577: 
                    578: : lit, ( n -- )   compile lit T  ,  H ;
                    579: : alit, ( n -- )  compile lit T A,  H ;
                    580: 
                    581: >TARGET
1.40      pazsan    582: Cond: \G  T-\G ;Cond
                    583: 
1.1       anton     584: Cond:  Literal ( n -- )   restrict? lit, ;Cond
                    585: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
                    586: 
                    587: : Char ( "<char>" -- )  bl word char+ c@ ;
                    588: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
                    589: 
1.43      pazsan    590: \ some special literals                                        27jan97jaw
                    591: 
                    592: Cond: MAXU
                    593:  restrict? compile lit 
                    594:  tcell 0 ?DO FF T c, H LOOP ;Cond
                    595: 
                    596: Cond: MINI
                    597:  restrict? compile lit
                    598:  bigendian IF
                    599:  80 T c, H tcell 1 ?DO 0 T c, H LOOP 
                    600:  ELSE
                    601:  tcell 1 ?DO 0 T c, H LOOP 80 T c, H
                    602:  THEN
                    603:  ;Cond
                    604:  
                    605: Cond: MAXI
                    606:  restrict? compile lit
                    607:  bigendian IF
                    608:  7F T c, H tcell 1 ?DO FF T c, H LOOP 
                    609:  ELSE
                    610:  tcell 1 ?DO FF T c, H LOOP 7F T c, H
                    611:  THEN
                    612:  ;Cond
                    613: 
1.1       anton     614: >CROSS
                    615: \ Target compiling loop                                12dec92py
                    616: \ ">tib trick thrown out                               10may93jaw
                    617: \ number? defined at the top                           11may93jaw
                    618: 
                    619: \ compiled word might leave items on stack!
                    620: : tcom ( in name -- )
                    621:   gfind  ?dup  IF    0> IF    nip >exec @ execute
                    622:                         ELSE  nip gexecute  THEN EXIT THEN
                    623:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
                    624:                ELSE  2drop >in !
                    625:                ghost gexecute THEN  ;
                    626: 
                    627: >TARGET
                    628: \ : ; DOES>                                            13dec92py
                    629: \ ]                                                     9may93py/jaw
                    630: 
                    631: : ] state on
                    632:     BEGIN
1.13      pazsan    633:         BEGIN >in @ bl word
1.1       anton     634:               dup c@ 0= WHILE 2drop refill 0=
                    635:               ABORT" CROSS: End of file while target compiling"
                    636:         REPEAT
                    637:         tcom
                    638:         state @
                    639:         0=
                    640:     UNTIL ;
                    641: 
                    642: \ by the way: defining a second interpreter (a compiler-)loop
                    643: \             is not allowed if a system should be ans conform
                    644: 
                    645: : : ( -- colon-sys ) \ Name
1.43      pazsan    646:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
1.1       anton     647:   (THeader ;Resolve ! there ;Resolve cell+ !
                    648:   docol, depth T ] H ;
                    649: 
1.37      pazsan    650: : :noname ( -- colon-sys )
                    651:   T align H there docol, depth T ] H ;
                    652: 
1.2       pazsan    653: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
1.6       anton     654: 
                    655: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1.2       pazsan    656: 
1.1       anton     657: Cond: ; ( -- ) restrict?
                    658:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                    659:                           ELSE true ABORT" CROSS: Stack empty" THEN
1.2       pazsan    660:                compile ;S state off
1.1       anton     661:                ;Resolve @
                    662:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
                    663:                ;Cond
                    664: Cond: [  restrict? state off ;Cond
                    665: 
                    666: >CROSS
1.42      pazsan    667: : !does
                    668:     tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
1.1       anton     669: 
                    670: >TARGET
                    671: Cond: DOES> restrict?
1.29      anton     672:         compile (does>) dodoes, tdoes @ ?dup IF  @ T here H resolve THEN
1.1       anton     673:         ;Cond
                    674: : DOES> dodoes, T here H !does depth T ] H ;
                    675: 
                    676: >CROSS
                    677: \ Creation                                             01nov92py
                    678: 
                    679: \ Builder                                               11may93jaw
                    680: 
                    681: : Builder    ( Create do: "name" -- )
                    682:   >in @ alias2 swap dup >in ! >r >r
                    683:   Make-Ghost rot swap >exec ! ,
                    684:   r> r> >in !
1.11      pazsan    685:   also ghosts ' previous swap ! ;
                    686: \  DOES>  dup >exec @ execute ;
1.1       anton     687: 
                    688: : gdoes,  ( ghost -- )  >end @ dup >magic @ <fwd> <>
1.42      pazsan    689:     IF
                    690:        dup >magic @ <do:> =
                    691:        IF  gexecute T 0 , H  EXIT THEN
                    692:     THEN
                    693:     compile :dodoes gexecute T here H cell - reloff ;
1.1       anton     694: 
1.11      pazsan    695: : TCreate ( -- )
                    696:   last-ghost @
1.1       anton     697:   CreateFlag on
1.11      pazsan    698:   Theader >r dup gdoes,
                    699:   >end @ >exec @ r> >exec ! ;
1.1       anton     700: 
                    701: : Build:  ( -- [xt] [colon-sys] )
                    702:   :noname  postpone TCreate ;
                    703: 
                    704: : gdoes>  ( ghost -- addr flag )
1.11      pazsan    705:   last-ghost @
1.1       anton     706:   state @ IF  gexecute true EXIT  THEN
                    707:   cell+ @ T >body H false ;
                    708: 
                    709: \ DO: ;DO                                               11may93jaw
                    710: \ changed to ?EXIT                                      10may93jaw
                    711: 
                    712: : DO:     ( -- addr [xt] [colon-sys] )
                    713:   here ghostheader
1.11      pazsan    714:   :noname postpone gdoes> postpone ?EXIT ;
1.1       anton     715: 
1.42      pazsan    716: : by:     ( -- addr [xt] [colon-sys] ) \ name
                    717:   ghost
                    718:   :noname postpone gdoes> postpone ?EXIT ;
                    719: 
1.1       anton     720: : ;DO ( addr [xt] [colon-sys] -- )
                    721:   postpone ;    ( S addr xt )
                    722:   over >exec ! ; immediate
                    723: 
                    724: : by      ( -- addr ) \ Name
                    725:   ghost >end @ ;
                    726: 
                    727: >TARGET
                    728: \ Variables and Constants                              05dec92py
                    729: 
                    730: Build:  ;
1.42      pazsan    731: by: :dovar ( ghost -- addr ) ;DO
1.1       anton     732: Builder Create
                    733: 
                    734: Build: T 0 , H ;
                    735: by Create
                    736: Builder Variable
                    737: 
                    738: Build: T 0 A, H ;
                    739: by Create
                    740: Builder AVariable
                    741: 
1.3       pazsan    742: \ User variables                                       04may94py
                    743: 
                    744: >CROSS
                    745: Variable tup  0 tup !
                    746: Variable tudp 0 tudp !
                    747: : u,  ( n -- udp )
                    748:   tup @ tudp @ + T  ! H
1.19      pazsan    749:   tudp @ dup T cell+ H tudp ! ;
1.3       pazsan    750: : au, ( n -- udp )
                    751:   tup @ tudp @ + T A! H
1.19      pazsan    752:   tudp @ dup T cell+ H tudp ! ;
1.3       pazsan    753: >TARGET
                    754: 
                    755: Build: T 0 u, , H ;
1.42      pazsan    756: by: :douser ( ghost -- up-addr )  T @ H tup @ + ;DO
1.1       anton     757: Builder User
                    758: 
1.3       pazsan    759: Build: T 0 u, , 0 u, drop H ;
                    760: by User
1.1       anton     761: Builder 2User
                    762: 
1.3       pazsan    763: Build: T 0 au, , H ;
                    764: by User
1.1       anton     765: Builder AUser
                    766: 
1.44      pazsan    767: Build:  ( n -- ) ;
                    768: by: :docon ( ghost -- n ) T @ H ;DO
                    769: Builder (Constant)
                    770: 
1.1       anton     771: Build:  ( n -- ) T , H ;
1.44      pazsan    772: by (Constant)
1.1       anton     773: Builder Constant
                    774: 
                    775: Build:  ( n -- ) T A, H ;
1.44      pazsan    776: by (Constant)
1.1       anton     777: Builder AConstant
                    778: 
1.24      pazsan    779: Build:  ( d -- ) T , , H ;
                    780: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
                    781: Builder 2Constant
                    782: 
1.45    ! pazsan    783: Build: T , H ;
1.44      pazsan    784: by (Constant)
1.1       anton     785: Builder Value
                    786: 
1.45    ! pazsan    787: Build: T A, H ;
1.44      pazsan    788: by (Constant)
1.32      pazsan    789: Builder AValue
                    790: 
1.1       anton     791: Build:  ( -- ) compile noop ;
1.42      pazsan    792: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1.1       anton     793: Builder Defer
1.37      pazsan    794: 
                    795: Build:  ( inter comp -- ) swap T immediate A, A, H ;
                    796: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1.38      anton     797: Builder interpret/compile:
1.24      pazsan    798: 
                    799: \ Sturctures                                           23feb95py
                    800: 
                    801: >CROSS
                    802: : nalign ( addr1 n -- addr2 )
                    803: \ addr2 is the aligned version of addr1 wrt the alignment size n
                    804:  1- tuck +  swap invert and ;
                    805: >TARGET
                    806: 
1.44      pazsan    807: Build: ;
                    808: by: :dofield T @ H + ;DO
                    809: Builder (Field)
                    810: 
1.24      pazsan    811: Build:         >r rot r@ nalign  dup T , H  ( align1 size offset )
                    812:        + swap r> nalign ;
1.44      pazsan    813: by (Field)
1.24      pazsan    814: Builder Field
                    815: 
                    816: : struct  T 0 1 chars H ;
                    817: : end-struct  T 2Constant H ;
                    818: 
                    819: : cells: ( n -- size align )
                    820:     T cells 1 cells H ;
                    821: 
                    822: \ ' 2Constant Alias2 end-struct
                    823: \ 0 1 T Chars H 2Constant struct
1.1       anton     824: 
                    825: \ structural conditionals                              17dec92py
                    826: 
                    827: >CROSS
                    828: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
                    829: : sys?        ( sys -- sys )    dup 0= ?struc ;
                    830: : >mark       ( -- sys )        T here  0 , H ;
                    831: : >resolve    ( sys -- )        T here over - swap ! H ;
                    832: : <resolve    ( sys -- )        T here - , H ;
                    833: >TARGET
                    834: 
                    835: \ Structural Conditionals                              12dec92py
                    836: 
                    837: Cond: BUT       restrict? sys? swap ;Cond
                    838: Cond: YET       restrict? sys? dup ;Cond
                    839: 
                    840: >CROSS
                    841: Variable tleavings
                    842: >TARGET
                    843: 
                    844: Cond: DONE   ( addr -- )  restrict? tleavings @
                    845:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
                    846:       tleavings ! drop ;Cond
                    847: 
                    848: >CROSS
                    849: : (leave  T here H tleavings @ T , H  tleavings ! ;
                    850: >TARGET
                    851: 
                    852: Cond: LEAVE     restrict? compile branch (leave ;Cond
                    853: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond
                    854: 
                    855: \ Structural Conditionals                              12dec92py
                    856: 
                    857: Cond: AHEAD     restrict? compile branch >mark ;Cond
                    858: Cond: IF        restrict? compile ?branch >mark ;Cond
                    859: Cond: THEN      restrict? sys? dup T @ H ?struc >resolve ;Cond
                    860: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
                    861: 
                    862: Cond: BEGIN     restrict? T here H ;Cond
                    863: Cond: WHILE     restrict? sys? compile IF swap ;Cond
                    864: Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
                    865: Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
                    866: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
1.45    ! pazsan    867: 
        !           868: Cond: CASE      restrict? 0 ;Cond
        !           869: Cond: OF        restrict? 1+ >r compile over compile = compile IF compile drop
        !           870:                 r> ;Cond
        !           871: Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
        !           872: Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond
1.1       anton     873: 
                    874: \ Structural Conditionals                              12dec92py
                    875: 
                    876: Cond: DO        restrict? compile (do)   T here H ;Cond
                    877: Cond: ?DO       restrict? compile (?do)  (leave T here H ;Cond
                    878: Cond: FOR       restrict? compile (for)  T here H ;Cond
                    879: 
                    880: >CROSS
                    881: : loop]   dup <resolve cell - compile DONE compile unloop ;
                    882: >TARGET
                    883: 
                    884: Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
                    885: Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
                    886: Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond
                    887: 
                    888: \ String words                                         23feb93py
                    889: 
                    890: : ,"            [char] " parse string, T align H ;
                    891: 
                    892: Cond: ."        restrict? compile (.")     T ," H ;Cond
                    893: Cond: S"        restrict? compile (S")     T ," H ;Cond
                    894: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
                    895: 
                    896: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
                    897: : IS            T ' >body ! H ;
1.9       pazsan    898: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
                    899: : TO            T ' >body ! H ;
1.1       anton     900: 
                    901: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
                    902: 
                    903: \ linked list primitive
                    904: : linked        T here over @ A, swap ! H ;
                    905: 
                    906: : err"   s" ErrLink linked" evaluate T , H
                    907:          [char] " parse string, T align H ;
                    908: 
                    909: : env"  [char] " parse s" EnvLink linked" evaluate
                    910:         string, T align , H ;
                    911: 
                    912: : 2env" [char] " parse s" EnvLink linked" evaluate
                    913:         here >r string, T align , , H
                    914:         r> dup T c@ H 80 and swap T c! H ;
                    915: 
                    916: \ compile must be last                                 22feb93py
                    917: 
                    918: Cond: compile ( -- ) restrict? \ name
1.13      pazsan    919:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1       anton     920:       0> IF    gexecute
                    921:          ELSE  dup >magic @ <imm> =
                    922:                IF   gexecute
                    923:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    924: 
                    925: Cond: postpone ( -- ) restrict? \ name
1.13      pazsan    926:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
1.1       anton     927:       0> IF    gexecute
                    928:          ELSE  dup >magic @ <imm> =
                    929:                IF   gexecute
                    930:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    931: 
                    932: >MINIMAL
                    933: also minimal
                    934: \ Usefull words                                        13feb93py
                    935: 
                    936: : KB  400 * ;
                    937: 
                    938: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
                    939: 
1.43      pazsan    940: : defined? defined? ;
1.44      pazsan    941: : needed? needed? ;
                    942: : doer? doer? ;
1.1       anton     943: 
1.43      pazsan    944: : [IFDEF] defined? postpone [IF] ;
                    945: : [IFUNDEF] defined? 0= postpone [IF] ;
1.1       anton     946: 
                    947: \ C: \- \+ Conditional Compiling                         09jun93jaw
                    948: 
1.43      pazsan    949: : C: >in @ defined? 0=
1.1       anton     950:      IF    >in ! T : H
                    951:      ELSE drop
                    952:         BEGIN bl word dup c@
                    953:               IF   count comment? s" ;" compare 0= ?EXIT
                    954:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
                    955:               THEN
                    956:         AGAIN
                    957:      THEN ;
                    958: 
                    959: also minimal
                    960: 
1.43      pazsan    961: : \- defined? IF postpone \ THEN ;
                    962: : \+ defined? 0= IF postpone \ THEN ;
1.1       anton     963: 
                    964: : [IF]   postpone [IF] ;
                    965: : [THEN] postpone [THEN] ;
                    966: : [ELSE] postpone [ELSE] ;
                    967: 
                    968: Cond: [IF]      [IF] ;Cond
                    969: Cond: [IFDEF]   [IFDEF] ;Cond
                    970: Cond: [IFUNDEF] [IFUNDEF] ;Cond
                    971: Cond: [THEN]    [THEN] ;Cond
                    972: Cond: [ELSE]    [ELSE] ;Cond
                    973: 
                    974: \ save-cross                                           17mar93py
                    975: 
                    976: \ i'm not interested in bigforth features this time    10may93jaw
                    977: \ [IFDEF] file
                    978: \ also file
                    979: \ [THEN]
                    980: \ included throw after create-file                     11may93jaw
                    981: 
1.12      anton     982: bigendian Constant bigendian
1.1       anton     983: 
1.34      anton     984: Create magic  s" Gforth10" here over allot swap move
1.26      pazsan    985: 
1.35      anton     986: char 1 bigendian + cell + magic 7 + c!
1.26      pazsan    987: 
1.34      anton     988: : save-cross ( "image-name" "binary-name" -- )
                    989:   bl parse ." Saving to " 2dup type cr
1.1       anton     990:   w/o bin create-file throw >r
1.43      pazsan    991:   NIL IF
                    992:       s" #! "   r@ write-file throw
                    993:       bl parse  r@ write-file throw
                    994:       s"  -i"   r@ write-file throw
                    995:       #lf       r@ emit-file throw
                    996:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
                    997:       ?do
                    998:          bl over emit-file throw
                    999:       loop
                   1000:       drop
                   1001:       magic 8       r@ write-file throw \ write magic
                   1002:   ELSE
                   1003:       bl parse 2drop
                   1004:   THEN
1.16      pazsan   1005:   image @ there r@ write-file throw \ write image
1.43      pazsan   1006:   NIL IF
                   1007:       bit$  @ there 1- cell>bit rshift 1+
1.16      pazsan   1008:                 r@ write-file throw \ write tags
1.43      pazsan   1009:   THEN
1.1       anton    1010:   r> close-file throw ;
                   1011: 
                   1012: \ words that should be in minimal
                   1013: 
1.43      pazsan   1014: : here there ;
                   1015: also forth [IFDEF] Label : Label Label ; [THEN] previous
                   1016: : + + ;
                   1017: : or or ;
                   1018: : 1- 1- ;
                   1019: : - - ;
                   1020: : 2* 2* ;
                   1021: : * * ;
                   1022: : / / ;
                   1023: : dup dup ;
                   1024: : over over ;
                   1025: : swap swap ;
                   1026: : rot rot ;
                   1027: : drop drop ;
                   1028: : =   = ;
                   1029: : 0=   0= ;
                   1030: : lshift lshift ;
                   1031: : 2/ 2/ ;
1.19      pazsan   1032: : . . ;
1.42      pazsan   1033: 
                   1034: mach-file count included
1.1       anton    1035: 
1.43      pazsan   1036: : all-words    ['] false    IS skip? ;
                   1037: : needed-words ['] needed?  IS skip? ;
                   1038: : undef-words  ['] defined? IS skip? ;
1.1       anton    1039: 
1.40      pazsan   1040: : \  postpone \ ;  immediate
                   1041: : (  postpone ( ;  immediate
1.1       anton    1042: : include bl word count included ;
                   1043: : .( [char] ) parse type ;
                   1044: : cr cr ;
                   1045: 
                   1046: : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
                   1047: only forth also minimal definitions
                   1048: 
                   1049: \ cross-compiler words
                   1050: 
                   1051: : decimal       decimal ;
                   1052: : hex           hex ;
                   1053: 
1.3       pazsan   1054: : tudp          T tudp H ;
1.39      pazsan   1055: : tup           T tup H ;
                   1056: 
                   1057: : doc-off       false T to-doc H ! ;
                   1058: : doc-on        true  T to-doc H ! ;
                   1059: 
                   1060: minimal
1.1       anton    1061: 
                   1062: \ for debugging...
                   1063: : order         order ;
                   1064: : words         words ;
                   1065: : .s            .s ;
                   1066: 
                   1067: : bye           bye ;
                   1068: 
                   1069: \ turnkey direction
                   1070: : H forth ; immediate
                   1071: : T minimal ; immediate
                   1072: : G ghosts ; immediate
                   1073: 
                   1074: : turnkey  0 set-order also Target definitions
                   1075:            also Minimal also ;
                   1076: 
                   1077: \ these ones are pefered:
                   1078: 
                   1079: : lock   turnkey ;
                   1080: : unlock forth also cross ;
                   1081: 
                   1082: unlock definitions also minimal
                   1083: : lock   lock ;
                   1084: lock

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