Annotation of gforth/cross.fs, revision 1.46

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

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