Annotation of gforth/cross.fs, revision 1.116

1.1       anton       1: \ CROSS.FS     The Cross-Compiler                      06oct92py
                      2: \ Idea and implementation: Bernd Paysan (py)
1.30      anton       3: 
1.86      anton       4: \ Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
1.30      anton       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
1.87      anton      20: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      21: 
1.67      jwilke     22: 0 
                     23: [IF]
                     24: 
                     25: ToDo:
1.107     jwilke     26: - Crossdoc destination ./doc/crossdoc.fd makes no sense when
                     27:   cross.fs is used seperately. jaw
                     28: - Do we need this char translation with >address and in branchoffset? 
                     29:   (>body also affected) jaw
                     30: - MAXU etc. can be done with dlit,
1.67      jwilke     31: 
                     32: [THEN]
1.48      anton      33: 
1.77      jwilke     34: hex
                     35: 
                     36: \ debugging for compiling
                     37: 
                     38: \ print stack at each colon definition
                     39: \ : : save-input cr bl word count type restore-input throw .s : ;
                     40: 
                     41: \ print stack at each created word
                     42: \ : create save-input cr bl word count type restore-input throw .s create ;
                     43: 
                     44: 
                     45: \ \ -------------  Setup Vocabularies
                     46: 
                     47: \ Remark: Vocabulary is not ANS, but it should work...
                     48: 
                     49: Vocabulary Cross
                     50: Vocabulary Target
                     51: Vocabulary Ghosts
                     52: Vocabulary Minimal
                     53: only Forth also Target also also
                     54: definitions Forth
                     55: 
1.78      jwilke     56: : T  previous Ghosts also Target ; immediate
1.77      jwilke     57: : G  Ghosts ; immediate
                     58: : H  previous Forth also Cross ; immediate
                     59: 
                     60: forth definitions
                     61: 
1.78      jwilke     62: : T  previous Ghosts also Target ; immediate
1.77      jwilke     63: : G  Ghosts ; immediate
                     64: 
1.105     jwilke     65: 
1.77      jwilke     66: : >cross  also Cross definitions previous ;
                     67: : >target also Target definitions previous ;
                     68: : >minimal also Minimal definitions previous ;
                     69: 
                     70: H
                     71: 
                     72: >CROSS
                     73: 
                     74: \ find out whether we are compiling with gforth
                     75: 
                     76: : defined? bl word find nip ;
                     77: defined? emit-file defined? toupper and \ drop 0
                     78: [IF]
                     79: \ use this in a gforth system
                     80: : \GFORTH ; immediate
                     81: : \ANSI postpone \ ; immediate
                     82: [ELSE]
                     83: : \GFORTH postpone \ ; immediate
                     84: : \ANSI ; immediate
                     85: [THEN]
                     86: 
                     87: \ANSI : [IFUNDEF] defined? 0= postpone [IF] ; immediate
                     88: \ANSI : [IFDEF] defined? postpone [IF] ; immediate
                     89: 0 \ANSI drop 1
                     90: [IF]
                     91: : \G postpone \ ; immediate
                     92: : rdrop postpone r> postpone drop ; immediate
                     93: : name bl word count ;
                     94: : bounds over + swap ;
                     95: : scan >r BEGIN dup WHILE over c@ r@ <> WHILE 1 /string REPEAT THEN rdrop ;
                     96: : linked here over @ , swap ! ;
                     97: : alias create , DOES> @ EXECUTE ;
                     98: : defer ['] noop alias ;
                     99: : is state @ 
                    100:   IF ' >body postpone literal postpone ! 
                    101:   ELSE ' >body ! THEN ; immediate
                    102: : 0>= 0< 0= ;
                    103: : d<> rot <> -rot <> or ;
                    104: : toupper dup [char] a [char] z 1+ within IF [char] A [char] a - + THEN ;
                    105: Variable ebuf
                    106: : emit-file ( c fd -- ior ) swap ebuf c! ebuf 1 chars rot write-file ;
                    107: 0a Constant #lf
                    108: 0d Constant #cr
                    109: 
                    110: [IFUNDEF] Warnings Variable Warnings [THEN]
                    111: 
                    112: \ \ Number parsing                                     23feb93py
                    113: 
                    114: \ number? number                                       23feb93py
                    115: 
                    116: Variable dpl
                    117: 
                    118: hex
                    119: Create bases   10 ,   2 ,   A , 100 ,
                    120: \              16     2    10   character
                    121: 
                    122: \ !! protect BASE saving wrapper against exceptions
                    123: : getbase ( addr u -- addr' u' )
                    124:     over c@ [char] $ - dup 4 u<
                    125:     IF
                    126:        cells bases + @ base ! 1 /string
                    127:     ELSE
                    128:        drop
                    129:     THEN ;
                    130: 
                    131: : sign? ( addr u -- addr u flag )
                    132:     over c@ [char] - =  dup >r
                    133:     IF
                    134:        1 /string
                    135:     THEN
                    136:     r> ;
                    137: 
                    138: : s>unumber? ( addr u -- ud flag )
1.78      jwilke    139:     over [char] ' =
                    140:     IF         \ a ' alone is rather unusual :-)
                    141:        drop char+ c@ 0 true EXIT 
                    142:     THEN
1.77      jwilke    143:     base @ >r  dpl on  getbase
                    144:     0. 2swap
                    145:     BEGIN ( d addr len )
                    146:        dup >r >number dup
                    147:     WHILE \ there are characters left
                    148:        dup r> -
                    149:     WHILE \ the last >number parsed something
                    150:        dup 1- dpl ! over c@ [char] . =
                    151:     WHILE \ the current char is '.'
                    152:        1 /string
                    153:     REPEAT  THEN \ there are unparseable characters left
                    154:        2drop false
                    155:     ELSE
                    156:        rdrop 2drop true
                    157:     THEN
                    158:     r> base ! ;
                    159: 
                    160: \ ouch, this is complicated; there must be a simpler way - anton
                    161: : s>number? ( addr len -- d f )
                    162:     \ converts string addr len into d, flag indicates success
                    163:     sign? >r
                    164:     s>unumber?
                    165:     0= IF
                    166:         rdrop false
                    167:     ELSE \ no characters left, all ok
                    168:        r>
                    169:        IF
                    170:            dnegate
                    171:        THEN
                    172:        true
                    173:     THEN ;
                    174: 
                    175: : s>number ( addr len -- d )
                    176:     \ don't use this, there is no way to tell success
                    177:     s>number? drop ;
                    178: 
                    179: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
                    180:     s>number? 0=
                    181:     IF
                    182:        2drop false  EXIT
                    183:     THEN
                    184:     dpl @ dup 0< IF
                    185:        nip
                    186:     ELSE
                    187:        1+
                    188:     THEN ;
                    189: 
                    190: : number? ( string -- string 0 / n -1 / d 0> )
                    191:     dup >r count snumber? dup if
                    192:        rdrop
                    193:     else
                    194:        r> swap
                    195:     then ;
                    196: 
                    197: : number ( string -- d )
                    198:     number? ?dup 0= abort" ?"  0<
                    199:     IF
                    200:        s>d
                    201:     THEN ;
                    202: 
                    203: [THEN]
1.48      anton     204: 
1.115     jwilke    205: \ this provides assert( and struct stuff
                    206: \GFORTH [IFUNDEF] assert1(
                    207: \GFORTH also forth definitions require assert.fs previous
                    208: \GFORTH [THEN]
                    209: 
                    210: >CROSS
                    211: 
1.52      jwilke    212: hex     \ the defualt base for the cross-compiler is hex !!
1.78      jwilke    213: \ Warnings off
1.52      jwilke    214: 
                    215: \ words that are generaly useful
                    216: 
1.59      pazsan    217: : KB  400 * ;
1.52      jwilke    218: : >wordlist ( vocabulary-xt -- wordlist-struct )
                    219:   also execute get-order swap >r 1- set-order r> ;
                    220: 
                    221: : umax 2dup u< IF swap THEN drop ;
                    222: : umin 2dup u> IF swap THEN drop ;
1.1       anton     223: 
1.23      pazsan    224: : string, ( c-addr u -- )
                    225:     \ puts down string as cstring
                    226:     dup c, here swap chars dup allot move ;
1.5       pazsan    227: 
1.77      jwilke    228: : ," [char] " parse string, ;
                    229: 
1.52      jwilke    230: : SetValue ( n -- <name> )
1.54      pazsan    231: \G Same behaviour as "Value" if the <name> is not defined
                    232: \G Same behaviour as "to" if <name> is defined
1.52      jwilke    233: \G SetValue searches in the current vocabulary
1.68      jwilke    234:   save-input bl word >r restore-input throw r> count
                    235:   get-current search-wordlist
                    236:   IF   drop >r
                    237:        \ we have to set current to be topmost context wordlist
                    238:        get-order get-order get-current swap 1+ set-order
                    239:        r> ['] to execute
1.74      jwilke    240:        set-order
1.68      jwilke    241:   ELSE Value THEN ;
1.52      jwilke    242: 
                    243: : DefaultValue ( n -- <name> )
1.54      pazsan    244: \G Same behaviour as "Value" if the <name> is not defined
                    245: \G DefaultValue searches in the current vocabulary
1.52      jwilke    246:  save-input bl word >r restore-input throw r> count
                    247:  get-current search-wordlist
1.56      pazsan    248:  IF bl word drop 2drop ELSE Value THEN ;
1.1       anton     249: 
                    250: hex
                    251: 
1.52      jwilke    252: \ 1 Constant Cross-Flag        \ to check whether assembler compiler plug-ins are
                    253:                        \ for cross-compiling
                    254: \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
                    255: 
                    256: : comment? ( c-addr u -- c-addr u )
                    257:         2dup s" (" compare 0=
                    258:         IF    postpone (
                    259:         ELSE  2dup s" \" compare 0= IF postpone \ THEN
                    260:         THEN ;
                    261: 
1.105     jwilke    262: : X    bl word count [ ' target >wordlist ] Literal search-wordlist
                    263:        IF      state @ IF compile,
                    264:                ELSE execute THEN
                    265:        ELSE    -1 ABORT" Cross: access method not supported!"
                    266:        THEN ; immediate
                    267: 
1.52      jwilke    268: \ Begin CROSS COMPILER:
                    269: 
1.74      jwilke    270: \ debugging
1.52      jwilke    271: 
1.74      jwilke    272: 0 [IF]
                    273: 
                    274: This implements debugflags for the cross compiler and the compiled
                    275: images. It works identical to the has-flags in the environment.
                    276: The debugflags are defined in a vocabluary. If the word exists and
                    277: its value is true, the flag is switched on.
                    278: 
                    279: [THEN]
                    280: 
1.77      jwilke    281: >CROSS
                    282: 
1.74      jwilke    283: Vocabulary debugflags  \ debug flags for cross
                    284: also debugflags get-order over
                    285: Constant debugflags-wl
                    286: set-order previous
                    287: 
                    288: : DebugFlag
                    289:   get-current >r debugflags-wl set-current
                    290:   SetValue
                    291:   r> set-current ;
                    292: 
                    293: : Debug? ( adr u -- flag )
                    294: \G return true if debug flag is defined or switched on
                    295:   debugflags-wl search-wordlist
                    296:   IF EXECUTE
                    297:   ELSE false THEN ;
                    298: 
                    299: : D? ( <name> -- flag )
                    300: \G return true if debug flag is defined or switched on
                    301: \G while compiling we do not return the current value but
                    302:   bl word count debug? ;
                    303: 
                    304: : [d?]
                    305: \G compile the value-xt so the debug flag can be switched
                    306: \G the flag must exist!
                    307:   bl word count debugflags-wl search-wordlist
                    308:   IF   compile,
                    309:   ELSE  -1 ABORT" unknown debug flag"
                    310:        \ POSTPONE false 
                    311:   THEN ; immediate
1.54      pazsan    312: 
1.77      jwilke    313: \ \ --------------------       source file
                    314: 
                    315: decimal
                    316: 
                    317: Variable cross-file-list
                    318: 0 cross-file-list !
                    319: Variable target-file-list
                    320: 0 target-file-list !
                    321: Variable host-file-list
                    322: 0 host-file-list !
                    323: 
                    324: cross-file-list Value file-list
                    325: 0 Value source-desc
                    326: 
                    327: \ file loading
                    328: 
                    329: : >fl-id   1 cells + ;
                    330: : >fl-name 2 cells + ;
                    331: 
                    332: Variable filelist 0 filelist !
                    333: Create NoFile ," #load-file#"
                    334: 
                    335: : loadfile ( -- adr )
                    336:   source-desc ?dup IF >fl-name ELSE NoFile THEN ;
                    337: 
                    338: : sourcefilename ( -- adr len ) 
                    339:   loadfile count ;
                    340: 
                    341: \ANSI : sourceline# 0 ;
                    342: 
                    343: \ \ --------------------       path handling from kernel/paths.fs
                    344: 
                    345: \ paths.fs path file handling                                    03may97jaw
                    346: 
                    347: \ -Changing the search-path:
                    348: \ fpath+ <path>        adds a directory to the searchpath
                    349: \ fpath= <path>|<path> makes complete now searchpath
                    350: \                      seperator is |
                    351: \ .fpath               displays the search path
                    352: \ remark I: 
                    353: \ a ./ in the beginning of filename is expanded to the directory the
                    354: \ current file comes from. ./ can also be included in the search-path!
                    355: \ ~+/ loads from the current working directory
                    356: 
                    357: \ remark II:
                    358: \ if there is no sufficient space for the search path increase it!
                    359: 
                    360: 
                    361: \ -Creating custom paths:
                    362: 
                    363: \ It is possible to use the search mechanism on yourself.
                    364: 
                    365: \ Make a buffer for the path:
                    366: \ create mypath        100 chars ,     \ maximum length (is checked)
                    367: \              0 ,             \ real len
                    368: \              100 chars allot \ space for path
                    369: \ use the same functions as above with:
                    370: \ mypath path+ 
                    371: \ mypath path=
                    372: \ mypath .path
                    373: 
                    374: \ do a open with the search path:
                    375: \ open-path-file ( adr len path -- fd adr len ior )
                    376: \ the file is opened read-only; if the file is not found an error is generated
                    377: 
                    378: \ questions to: wilke@jwdt.com
                    379: 
                    380: [IFUNDEF] +place
                    381: : +place ( adr len adr )
                    382:         2dup >r >r
                    383:         dup c@ char+ + swap move
                    384:         r> r> dup c@ rot + swap c! ;
                    385: [THEN]
                    386: 
                    387: [IFUNDEF] place
                    388: : place ( c-addr1 u c-addr2 )
                    389:         2dup c! char+ swap move ;
                    390: [THEN]
                    391: 
                    392: \ if we have path handling, use this and the setup of it
                    393: [IFUNDEF] open-fpath-file
                    394: 
                    395: create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
                    396: sourcepath value fpath
                    397: 
                    398: : also-path ( adr len path^ -- )
                    399:   >r
                    400:   \ len check
                    401:   r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
                    402:   \ copy into
                    403:   tuck r@ cell+ dup @ cell+ + swap cmove
                    404:   \ make delimiter
                    405:   0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
                    406:   ;
                    407: 
                    408: : only-path ( adr len path^ -- )
                    409:   dup 0 swap cell+ ! also-path ;
                    410: 
                    411: : path+ ( path-addr  "dir" -- ) \ gforth
                    412:     \G Add the directory @var{dir} to the search path @var{path-addr}.
                    413:     name rot also-path ;
                    414: 
                    415: : fpath+ ( "dir" ) \ gforth
                    416:     \G Add directory @var{dir} to the Forth search path.
                    417:     fpath path+ ;
                    418: 
                    419: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
                    420:     \G Make a complete new search path; the path separator is |.
                    421:     name 2dup bounds ?DO i c@ [char] | = IF 0 i c! THEN LOOP
                    422:     rot only-path ;
                    423: 
                    424: : fpath= ( "dir1|dir2|dir3" ) \ gforth
                    425:     \G Make a complete new Forth search path; the path separator is |.
                    426:     fpath path= ;
                    427: 
                    428: : path>counted  cell+ dup cell+ swap @ ;
                    429: 
                    430: : next-path ( adr len -- adr2 len2 )
                    431:   2dup 0 scan
                    432:   dup 0= IF     2drop 0 -rot 0 -rot EXIT THEN
                    433:   >r 1+ -rot r@ 1- -rot
                    434:   r> - ;
                    435: 
                    436: : previous-path ( path^ -- )
                    437:   dup path>counted
                    438:   BEGIN tuck dup WHILE repeat ;
                    439: 
                    440: : .path ( path-addr -- ) \ gforth
                    441:     \G Display the contents of the search path @var{path-addr}.
                    442:     path>counted
                    443:     BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
                    444: 
                    445: : .fpath ( -- ) \ gforth
                    446:     \G Display the contents of the Forth search path.
                    447:     fpath .path ;
                    448: 
                    449: : absolut-path? ( addr u -- flag ) \ gforth
                    450:     \G A path is absolute if it starts with a / or a ~ (~ expansion),
                    451:     \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
                    452:     \G it has a colon as second character ("C:...").  Paths simply
                    453:     \G containing a / are not absolute!
                    454:     2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/....
                    455:     over c@ [char] / = >r
                    456:     over c@ [char] ~ = >r
                    457:     \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
                    458:     2 min S" ./" compare 0=
                    459:     r> r> r> or or or ;
                    460: 
                    461: Create ofile 0 c, 255 chars allot
                    462: Create tfile 0 c, 255 chars allot
                    463: 
                    464: : pathsep? dup [char] / = swap [char] \ = or ;
                    465: 
                    466: : need/   ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
                    467: 
                    468: : extractpath ( adr len -- adr len2 )
                    469:   BEGIN dup WHILE 1-
                    470:         2dup + c@ pathsep? IF EXIT THEN
                    471:   REPEAT ;
                    472: 
                    473: : remove~+ ( -- )
                    474:     ofile count 3 min s" ~+/" compare 0=
                    475:     IF
                    476:        ofile count 3 /string ofile place
                    477:     THEN ;
                    478: 
                    479: : expandtopic ( -- ) \ stack effect correct? - anton
                    480:     \ expands "./" into an absolute name
                    481:     ofile count 2 min s" ./" compare 0=
                    482:     IF
                    483:        ofile count 1 /string tfile place
                    484:        0 ofile c! sourcefilename extractpath ofile place
                    485:        ofile c@ IF need/ THEN
                    486:        tfile count over c@ pathsep? IF 1 /string THEN
                    487:        ofile +place
                    488:     THEN ;
                    489: 
                    490: : compact.. ( adr len -- adr2 len2 )
1.85      pazsan    491:     \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
                    492:     over swap
                    493:     BEGIN  dup  WHILE
                    494:         dup >r '/ scan 2dup 4 min s" /../" compare 0=
                    495:         IF
                    496:             dup r> - >r 4 /string over r> + 4 -
                    497:             swap 2dup + >r move dup r> over -
                    498:         ELSE
                    499:             rdrop dup 1 min /string
                    500:         THEN
                    501:     REPEAT  drop over - ;
1.77      jwilke    502: 
                    503: : reworkdir ( -- )
                    504:   remove~+
                    505:   ofile count compact..
                    506:   nip ofile c! ;
                    507: 
                    508: : open-ofile ( -- fid ior )
                    509:     \G opens the file whose name is in ofile
                    510:     expandtopic reworkdir
                    511:     ofile count r/o open-file ;
                    512: 
                    513: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
                    514:   0 ofile ! >r >r ofile place need/
                    515:   r> r> ofile +place
                    516:   open-ofile ;
                    517: 
                    518: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
                    519:     \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
                    520:     \G If found, the resulting path and an open file descriptor
                    521:     \G are returned. If the file is not found, @var{ior} is non-zero.
                    522:   >r
                    523:   2dup absolut-path?
                    524:   IF    rdrop
                    525:         ofile place open-ofile
                    526:        dup 0= IF >r ofile count r> THEN EXIT
                    527:   ELSE  r> path>counted
                    528:         BEGIN  next-path dup
                    529:         WHILE  5 pick 5 pick check-path
                    530:         0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
                    531:   REPEAT
                    532:         2drop 2drop 2drop -38
                    533:   THEN ;
                    534: 
                    535: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
                    536:     \G Look in the Forth search path for the file specified by @var{addr1 u1}.
                    537:     \G If found, the resulting path and an open file descriptor
                    538:     \G are returned. If the file is not found, @var{ior} is non-zero.
                    539:     fpath open-path-file ;
                    540: 
                    541: fpath= ~+
                    542: 
                    543: [THEN]
                    544: 
                    545: \ \ --------------------       include require                 13may99jaw
                    546: 
                    547: >CROSS
                    548: 
                    549: : add-included-file ( adr len -- adr )
                    550:   dup >fl-name char+ allocate throw >r
                    551:   file-list @ r@ ! r@ file-list !
                    552:   r@ >fl-name place r> ;
                    553: 
                    554: : included? ( c-addr u -- f )
                    555:   file-list
                    556:   BEGIN        @ dup
                    557:   WHILE        >r 2dup r@ >fl-name count compare 0=
                    558:        IF rdrop 2drop true EXIT THEN
                    559:        r>
                    560:   REPEAT
                    561:   2drop drop false ;   
                    562: 
                    563: false DebugFlag showincludedfiles
                    564: 
                    565: : included1 ( fd adr u -- )
                    566: \ include file adr u / fd
                    567: \ we don't use fd with include-file, because the forth system
                    568: \ doesn't know the name of the file to get a nice error report
                    569:   [d?] showincludedfiles
                    570:   IF   cr ." Including: " 2dup type ." ..." THEN
                    571:   rot close-file throw
                    572:   source-desc >r
                    573:   add-included-file to source-desc 
                    574:   sourcefilename
                    575:   ['] included catch
                    576:   r> to source-desc 
                    577:   throw ;
                    578: 
                    579: : included ( adr len -- )
                    580:        cross-file-list to file-list
                    581:        open-fpath-file throw 
                    582:         included1 ;
                    583: 
                    584: : required ( adr len -- )
                    585:        cross-file-list to file-list
                    586:        open-fpath-file throw \ 2dup cr ." R:" type
                    587:        2dup included?
                    588:        IF      2drop close-file throw
                    589:        ELSE    included1
                    590:        THEN ;
                    591: 
                    592: : include bl word count included ;
                    593: 
                    594: : require bl word count required ;
                    595: 
1.78      jwilke    596: 0 [IF]
                    597: 
1.77      jwilke    598: also forth definitions previous
                    599: 
                    600: : included ( adr len -- ) included ;
                    601: 
                    602: : required ( adr len -- ) required ;
                    603: 
                    604: : include include ;
                    605: 
                    606: : require require ;
                    607: 
1.78      jwilke    608: [THEN]
                    609: 
1.77      jwilke    610: >CROSS
                    611: hex
                    612: 
                    613: \ \ --------------------        Error Handling                  05aug97jaw
                    614: 
                    615: \ Flags
                    616: 
                    617: also forth definitions  \ these values may be predefined before
                    618:                         \ the cross-compiler is loaded
                    619: 
                    620: false DefaultValue stack-warn           \ check on empty stack at any definition
                    621: false DefaultValue create-forward-warn   \ warn on forward declaration of created words
                    622: 
                    623: previous >CROSS
                    624: 
                    625: : .dec
                    626:   base @ decimal swap . base ! ;
                    627: 
                    628: : .sourcepos
                    629:   cr sourcefilename type ." :"
                    630:   sourceline# .dec ;
                    631: 
                    632: : warnhead
                    633: \G display error-message head
                    634: \G perhaps with linenumber and filename
                    635:   .sourcepos ." Warning: " ;
                    636: 
                    637: : empty? depth IF .sourcepos ." Stack not empty!"  THEN ;
                    638: 
                    639: stack-warn [IF]
                    640: : defempty? empty? ;
                    641: [ELSE]
                    642: : defempty? ; immediate
                    643: [THEN]
                    644: 
1.105     jwilke    645: \ \ --------------------        Compiler Plug Ins               01aug97jaw
                    646: 
                    647: >CROSS
                    648: 
                    649: \ Compiler States
                    650: 
                    651: Variable comp-state
                    652: 0 Constant interpreting
                    653: 1 Constant compiling
                    654: 2 Constant resolving
                    655: 3 Constant assembling
                    656: 
                    657: : compiling? comp-state @ compiling = ;
                    658: 
                    659: : pi-undefined -1 ABORT" Plugin undefined" ;
                    660: 
                    661: : Plugin ( -- : pluginname )
                    662:   Create 
                    663:   \ for normal cross-compiling only one action
                    664:   \ exists, this fields are identical. For the instant
                    665:   \ simulation environment we need, two actions for each plugin
                    666:   \ the target one and the one that generates the simulation code
                    667:   ['] pi-undefined , \ action
                    668:   ['] pi-undefined , \ target plugin action
                    669:   8765 ,     \ plugin magic
                    670:   DOES> perform ;
                    671: 
                    672: Plugin DummyPlugin
                    673: 
                    674: : 'PI ( -- addr : pluginname )
                    675:   ' >body dup 2 cells + @ 8765 <> ABORT" not a plugin" ;
                    676: 
                    677: : plugin-of ( xt -- : pluginname )
                    678:   dup 'PI 2! ;
                    679: 
                    680: : action-of ( xt -- : plunginname )
                    681:   'PI cell+ ! ;
                    682: 
                    683: : TPA ( -- : plugin )
                    684: \ target plugin action
                    685: \ executes current target action of plugin
                    686:   'PI cell+ POSTPONE literal POSTPONE perform ; immediate
                    687: 
                    688: Variable ppi-temp 0 ppi-temp !
                    689: 
                    690: : pa:
                    691: \g define plugin action
                    692:   ppi-temp @ ABORT" pa: definition not closed"
                    693:   'PI ppi-temp ! :noname ;
                    694: 
                    695: : ;pa
                    696: \g end a definition for plugin action
                    697:   POSTPONE ; ppi-temp @ ! 0 ppi-temp ! ; immediate
                    698: 
                    699: 
1.107     jwilke    700: Plugin dlit, ( d -- )                  \ compile numerical value the target
1.105     jwilke    701: Plugin lit, ( n -- )
                    702: Plugin alit, ( n -- )
                    703: 
                    704: Plugin branch, ( target-addr -- )      \ compiles a branch
                    705: Plugin ?branch, ( target-addr -- )     \ compiles a ?branch
                    706: Plugin branchmark, ( -- branch-addr )  \ reserves room for a branch
                    707: Plugin ?branchmark, ( -- branch-addr ) \ reserves room for a ?branch
                    708: Plugin ?domark, ( -- branch-addr )     \ reserves room for a ?do branch
                    709: Plugin branchto, ( -- )                        \ actual program position is target of a branch (do e.g. alignment)
                    710: ' NOOP plugin-of branchto, 
                    711: Plugin branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
                    712: Plugin branchtomark, ( -- target-addr )        \ marks a branch destination
                    713: 
                    714: Plugin colon, ( tcfa -- )              \ compiles call to tcfa at current position
                    715: Plugin prim, ( tcfa -- )               \ compiles primitive invocation
                    716: Plugin colonmark, ( -- addr )          \ marks a colon call
                    717: Plugin colon-resolve ( tcfa addr -- )
                    718: 
                    719: Plugin addr-resolve ( target-addr addr -- )
                    720: Plugin doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
                    721: 
                    722: Plugin ncontrols? ( [ xn ... x1 ] n -- ) \ checks wheter n control structures are open
                    723: Plugin if,     ( -- if-token )
                    724: Plugin else,   ( if-token -- if-token )
                    725: Plugin then,   ( if-token -- )
                    726: Plugin ahead,
                    727: Plugin begin,
                    728: Plugin while,
                    729: Plugin until,
                    730: Plugin again,
                    731: Plugin repeat,
                    732: Plugin cs-swap ( x1 x2 -- x2 x1 )
                    733: 
                    734: Plugin case,   ( -- n )
                    735: Plugin of,     ( n -- x1 n )
                    736: Plugin endof,  ( x1 n -- x2 n )
                    737: Plugin endcase,        ( x1 .. xn n -- )
                    738: 
                    739: Plugin do,     ( -- do-token )
                    740: Plugin ?do,    ( -- ?do-token )
                    741: Plugin for,    ( -- for-token )
                    742: Plugin loop,   ( do-token / ?do-token -- )
                    743: Plugin +loop,  ( do-token / ?do-token -- )
                    744: Plugin next,   ( for-token )
                    745: Plugin leave,  ( -- )
                    746: Plugin ?leave,         ( -- )
                    747: 
                    748: [IFUNDEF] ca>native
                    749: Plugin ca>native       
                    750: [THEN]
                    751: 
                    752: Plugin doprim, \ compiles start of a primitive
                    753: Plugin docol,          \ compiles start of a colon definition
                    754: Plugin doer,           
                    755: Plugin fini,      \ compiles end of definition ;s
                    756: Plugin doeshandler,
                    757: Plugin dodoes,
                    758: 
                    759: Plugin colon-start
                    760: ' noop plugin-of colon-start
                    761: Plugin colon-end
                    762: ' noop plugin-of colon-end
                    763: 
                    764: Plugin ]comp     \ starts compilation
                    765: ' noop plugin-of ]comp
                    766: Plugin comp[     \ ends compilation
                    767: ' noop plugin-of comp[
                    768: 
                    769: Plugin t>body             \ we need the system >body
                    770:                        \ and the target >body
                    771: 
                    772: >TARGET
                    773: : >body t>body ;
                    774: 
                    775: 
1.103     jwilke    776: \ Ghost Builder                                        06oct92py
                    777: 
1.105     jwilke    778: >CROSS
1.103     jwilke    779: hex
1.105     jwilke    780: \ Values for ghost magic
1.103     jwilke    781: 4711 Constant <fwd>             4712 Constant <res>
                    782: 4713 Constant <imm>             4714 Constant <do:>
                    783: 4715 Constant <skip>
                    784: 
1.105     jwilke    785: \ Bitmask for ghost flags
1.103     jwilke    786: 1 Constant <unique>
1.105     jwilke    787: 2 Constant <primitive>
                    788: 
                    789: \ FXIME: move this to general stuff?
                    790: : set-flag ( addr flag -- )
                    791:   over @ or swap ! ;
                    792: 
                    793: : reset-flag ( addr flag -- )
                    794:   invert over @ and swap ! ;
                    795: 
                    796: : get-flag ( addr flag -- f )
                    797:   swap @ and 0<> ;
                    798:   
1.103     jwilke    799: 
                    800: Struct
1.52      jwilke    801: 
1.103     jwilke    802:   \ link to next ghost (always the first element)
                    803:   cell% field >next-ghost
1.52      jwilke    804: 
1.103     jwilke    805:   \ type of ghost
                    806:   cell% field >magic
                    807:                
                    808:   \ pointer where ghost is in target, or if unresolved
                    809:   \ points to the where we have to resolve (linked-list)
                    810:   cell% field >link
1.52      jwilke    811: 
1.106     jwilke    812:   \ execution semantics (while target compiling) of ghost
1.103     jwilke    813:   cell% field >exec
1.52      jwilke    814: 
1.106     jwilke    815:   \ compilation action of this ghost; this is what is
                    816:   \ done to compile a call (or whatever) to this definition.
                    817:   \ E.g. >comp contains the semantic of postpone s"
                    818:   \ whereas >exec-compile contains the semantic of s"
1.105     jwilke    819:   cell% field >comp
                    820: 
1.106     jwilke    821:   \ Compilation sematics (while parsing) of this ghost. E.g. 
                    822:   \ "\" will skip the rest of line.
                    823:   \ These semantics are defined by Cond: and
                    824:   \ if a word is made immediate in instant, then the >exec2 field
                    825:   \ gets copied to here
1.103     jwilke    826:   cell% field >exec-compile
1.52      jwilke    827: 
1.106     jwilke    828:   \ Additional execution semantics of this ghost. This is used
                    829:   \ for code generated by instant and for the doer-xt of created
                    830:   \ words
1.103     jwilke    831:   cell% field >exec2
1.52      jwilke    832: 
1.103     jwilke    833:   cell% field >created
1.52      jwilke    834: 
1.103     jwilke    835:   \ the xt of the created ghost word itself
                    836:   cell% field >ghost-xt
1.52      jwilke    837: 
1.103     jwilke    838:   \ pointer to the counted string of the assiciated
                    839:   \ assembler label
                    840:   cell% field >asm-name
1.52      jwilke    841: 
1.103     jwilke    842:   \ mapped primitives have a special address, so
                    843:   \ we are able to detect them
                    844:   cell% field >asm-dummyaddr
                    845:                        
                    846:   \ for builder (create, variable...) words
                    847:   \ the execution symantics of words built are placed here
                    848:   \ this is a doer ghost or a dummy ghost
                    849:   cell% field >do:ghost
1.93      pazsan    850: 
1.103     jwilke    851:   cell% field >ghost-flags
1.93      pazsan    852: 
1.103     jwilke    853:   cell% field >ghost-name
1.52      jwilke    854: 
1.103     jwilke    855: End-Struct ghost-struct
1.52      jwilke    856: 
1.103     jwilke    857: Variable ghost-list
                    858: 0 ghost-list !
1.52      jwilke    859: 
                    860: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
1.103     jwilke    861: \ Variable last-ghost  \ last ghost that is created
1.52      jwilke    862: Variable last-header-ghost \ last ghost definitions with header
                    863: 
1.103     jwilke    864: \ space for ghosts resolve structure
                    865: \ we create ghosts in a sepearte space
                    866: \ and not to the current host dp, because this
                    867: \ gives trouble with instant while compiling and creating
                    868: \ a ghost for a forward reference
                    869: \ BTW: we cannot allocate another memory region
                    870: \ because allot will check the overflow!!
                    871: Variable cross-space-dp
                    872: Create cross-space 250000 allot here 100 allot align 
                    873: Constant cross-space-end
                    874: cross-space cross-space-dp !
                    875: Variable cross-space-dp-orig
                    876: 
                    877: : cross-space-used cross-space-dp @ cross-space - ;
                    878: 
                    879: : >space ( -- )
                    880:   dp @ cross-space-dp-orig !
                    881:   cross-space-dp @ dp ! ;
                    882: 
                    883: : space> ( -- )
                    884:   dp @ dup cross-space-dp !
                    885:   cross-space-end u> ABORT" CROSS: cross-space overflow"
                    886:   cross-space-dp-orig @ dp ! ;
                    887: 
1.106     jwilke    888: \ this is just for debugging, to see this in the backtrace
1.103     jwilke    889: : execute-exec execute ;
                    890: : execute-exec2 execute ;
                    891: : execute-exec-compile execute ;
                    892: 
                    893: : NoExec
                    894:   executed-ghost @ >exec2 @
                    895:   ?dup 
                    896:   IF   execute-exec2
                    897:   ELSE true ABORT" CROSS: Don't execute ghost, or immediate target word"
                    898:   THEN ;
                    899: 
1.105     jwilke    900: Defer is-forward
                    901: 
1.103     jwilke    902: : (ghostheader) ( -- )
1.105     jwilke    903:   ghost-list linked <fwd> , 0 , ['] NoExec , ['] is-forward , 
                    904:   0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ;
1.103     jwilke    905: 
                    906: : ghostheader ( -- ) (ghostheader) 0 , ;
                    907: 
                    908: ' Ghosts >wordlist Constant ghosts-wordlist
                    909: 
                    910: \ the current wordlist for ghost definitions in the host
                    911: ghosts-wordlist Value current-ghosts
                    912: 
1.52      jwilke    913: : Make-Ghost ( "name" -- ghost )
1.103     jwilke    914:   >space
                    915:   \ save current and create in ghost vocabulary
                    916:   get-current >r current-ghosts set-current
                    917:   >in @ Create >in !
                    918:   \ some forth systems like iForth need the immediate directly
                    919:   \ after the word is created
                    920:   \ restore current
                    921:   r> set-current
                    922:   here (ghostheader)
                    923:   bl word count string, align
                    924:   space>
                    925:   \ set ghost-xt field by doing a search
                    926:   dup >ghost-name count 
                    927:   current-ghosts search-wordlist
                    928:   0= ABORT" CROSS: Just created, must be there!"
                    929:   over >ghost-xt !
                    930:   DOES> 
                    931:       dup executed-ghost !
                    932:       >exec @ execute-exec ;
1.52      jwilke    933: 
                    934: \ ghost words                                          14oct92py
                    935: \                                          changed:    10may93py/jaw
                    936: 
1.103     jwilke    937: Defer search-ghosts
                    938: 
                    939: : (search-ghosts) ( adr len -- cfa true | 0 )
                    940:   current-ghosts search-wordlist ; 
                    941: 
                    942:   ' (search-ghosts) IS search-ghosts
                    943: 
                    944: : gsearch ( addr len -- ghost true | 0 )
                    945:   search-ghosts
                    946:   dup IF swap >body swap THEN ;
                    947: 
                    948: : gfind   ( string -- ghost true / string false )
1.52      jwilke    949: \ searches for string in word-list ghosts
1.103     jwilke    950:   \ dup count type space
                    951:   dup >r count gsearch
                    952:   dup IF rdrop ELSE r> swap THEN ;
1.52      jwilke    953: 
                    954: : gdiscover ( xt -- ghost true | xt false )
1.103     jwilke    955:   >r ghost-list
1.52      jwilke    956:   BEGIN @ dup
1.103     jwilke    957:   WHILE dup >magic @ <fwd> <>
                    958:         IF dup >link @ r@ =
                    959:            IF rdrop true EXIT THEN
                    960:         THEN
1.52      jwilke    961:   REPEAT
1.103     jwilke    962:   drop r> false ;
1.52      jwilke    963: 
1.105     jwilke    964: : xt>ghost ( xt -- ghost )
                    965:   gdiscover 0= ABORT" CROSS: ghost not found for this xt" ;
                    966: 
1.103     jwilke    967: : Ghost   ( "name" -- ghost )
                    968:   >in @ bl word gfind IF  nip EXIT  THEN
1.52      jwilke    969:   drop  >in !  Make-Ghost ;
                    970: 
                    971: : >ghostname ( ghost -- adr len )
1.103     jwilke    972:   >ghost-name count ;
                    973: 
                    974: : forward? ( ghost -- flag )
                    975:   >magic @ <fwd> = ;
                    976: 
                    977: : undefined? ( ghost -- flag )
                    978:   >magic @ dup <fwd> = swap <skip> = or ;
                    979: 
                    980: : immediate? ( ghost -- flag )
                    981:   >magic @ <imm> = ;
                    982: 
                    983: Variable TWarnings
                    984: TWarnings on
                    985: Variable Exists-Warnings
                    986: Exists-Warnings on
                    987: 
                    988: : exists-warning ( ghost -- ghost )
                    989:   TWarnings @ Exists-Warnings @ and
                    990:   IF dup >ghostname warnhead type ."  exists " THEN ;
                    991: 
                    992: \ : HeaderGhost Ghost ;
                    993: 
                    994: Variable reuse-ghosts reuse-ghosts off
                    995: 
                    996: : HeaderGhost ( "name" -- ghost )
                    997:   >in @ 
                    998:   bl word count 
                    999: \  2dup type space
                   1000:   current-ghosts search-wordlist
                   1001:   IF  >body dup undefined? reuse-ghosts @ or
                   1002:       IF   nip EXIT
                   1003:       ELSE exists-warning 
                   1004:       THEN
                   1005:       drop >in ! 
                   1006:   ELSE >in ! 
                   1007:   THEN 
                   1008:   \ we keep the execution semantics of the prviously
                   1009:   \ defined words, this is a workaround
                   1010:   \ for the redefined \ until vocs work
                   1011:   Make-Ghost ;
                   1012:  
1.78      jwilke   1013: : .ghost ( ghost -- ) >ghostname type ;
                   1014: 
1.77      jwilke   1015: \ ' >ghostname ALIAS @name
1.52      jwilke   1016: 
1.115     jwilke   1017: : findghost ( "ghostname" -- ghost ) 
                   1018:   bl word gfind 0= ABORT" CROSS: Ghost don't exists" ;
                   1019: 
1.103     jwilke   1020: : [G'] ( -- ghost : name )
                   1021: \G ticks a ghost and returns its address
1.115     jwilke   1022:   findghost
                   1023:   state @ IF postpone literal THEN ; immediate
1.103     jwilke   1024: 
1.105     jwilke   1025: : g>xt ( ghost -- xt )
                   1026: \G Returns the xt (cfa) of a ghost. Issues a warning if undefined.
1.103     jwilke   1027:   dup undefined? ABORT" CROSS: forward " >link @ ;
                   1028:    
1.105     jwilke   1029: : g>body ( ghost -- body )
                   1030: \G Returns the body-address (pfa) of a ghost. 
                   1031: \G Issues a warning if undefined (a forward-reference).
                   1032:   g>xt X >body ;
                   1033: 
1.103     jwilke   1034: 1 Constant <label>
1.52      jwilke   1035: 
1.103     jwilke   1036: Struct
                   1037:   \ bitmask of address type (not used for now)
                   1038:   cell% field addr-type
                   1039:   \ if this address is an xt, this field points to the ghost
                   1040:   cell% field addr-xt-ghost
                   1041:   \ a bit mask that tells as what part of the cell
                   1042:   \ is refenced from an address pointer, used for assembler generation
                   1043:   cell% field addr-refs
                   1044: End-Struct addr-struct
                   1045: 
                   1046: : %allocerase ( align size -- addr )
                   1047:   dup >r %alloc dup r> erase ;
                   1048: 
                   1049: \ returns the addr struct, define it if 0 reference
                   1050: : define-addr-struct ( addr -- struct-addr )
                   1051:   dup @ ?dup IF nip EXIT THEN
                   1052:   addr-struct %allocerase tuck swap ! ;
1.75      jwilke   1053: 
1.52      jwilke   1054: \ Predefined ghosts                                    12dec92py
                   1055: 
1.114     jwilke   1056: Ghost - drop \ need a ghost otherwise "-" would be treated as a number
                   1057: 
1.103     jwilke   1058: Ghost 0=                                        drop
                   1059: Ghost branch    Ghost ?branch                   2drop
                   1060: Ghost (do)      Ghost (?do)                     2drop
                   1061: Ghost (for)                                     drop
                   1062: Ghost (loop)    Ghost (+loop)                   2drop
                   1063: Ghost (next)                                    drop
                   1064: Ghost unloop    Ghost ;S                        2drop
                   1065: Ghost lit       Ghost (compile) Ghost !         2drop drop
                   1066: Ghost (does>)   Ghost noop                      2drop
                   1067: Ghost (.")      Ghost (S")      Ghost (ABORT")  2drop drop
                   1068: Ghost '                                         drop
                   1069: Ghost :docol    Ghost :doesjump Ghost :dodoes   2drop drop
                   1070: Ghost :dovar                                   drop
                   1071: Ghost over      Ghost =         Ghost drop      2drop drop
                   1072: Ghost 2drop drop
                   1073: Ghost 2dup drop
1.115     jwilke   1074: Ghost state drop
                   1075: Ghost call drop
                   1076: Ghost @ drop
                   1077: Ghost useraddr drop
                   1078: Ghost execute drop
                   1079: Ghost + drop
                   1080: Ghost (C") drop
                   1081: Ghost decimal drop
                   1082: Ghost hex drop
1.52      jwilke   1083: 
                   1084: \ \ Parameter for target systems                         06oct92py
                   1085: 
1.103     jwilke   1086: 
                   1087: >cross
1.52      jwilke   1088: \ we define it ans like...
                   1089: wordlist Constant target-environment
                   1090: 
1.103     jwilke   1091: \ save information of current dictionary to restore with environ>
                   1092: Variable env-current 
1.52      jwilke   1093: 
                   1094: : >ENVIRON get-current env-current ! target-environment set-current ;
                   1095: : ENVIRON> env-current @ set-current ; 
1.43      pazsan   1096: 
1.48      anton    1097: >TARGET
1.43      pazsan   1098: 
1.103     jwilke   1099: : environment? ( addr len -- [ x ] true | false )
                   1100: \G returns the content of environment variable and true or
                   1101: \G false if not present
                   1102:    target-environment search-wordlist 
                   1103:    IF EXECUTE true ELSE false THEN ;
                   1104: 
                   1105: : $has? ( addr len -- x | false )
                   1106: \G returns the content of environment variable 
                   1107: \G or false if not present
                   1108:    T environment? H dup IF drop THEN ;
                   1109: 
                   1110: : e? ( "name" -- x )
                   1111: \G returns the content of environment variable. 
                   1112: \G The variable is expected to exist. If not, issue an error.
                   1113:    bl word count T environment? H 
                   1114:    0= ABORT" environment variable not defined!" ;
                   1115: 
                   1116: : has? ( "name" --- x | false )
                   1117: \G returns the content of environment variable 
                   1118: \G or false if not present
                   1119:    bl word count T $has? H ;
1.52      jwilke   1120: 
1.48      anton    1121: 
1.54      pazsan   1122: >ENVIRON get-order get-current swap 1+ set-order
                   1123: true SetValue compiler
1.83      pazsan   1124: true SetValue cross
1.54      pazsan   1125: true SetValue standard-threading
                   1126: >TARGET previous
1.52      jwilke   1127: 
1.77      jwilke   1128: 0
1.103     jwilke   1129: [IFDEF] mach-file drop mach-file count 1 [THEN]
                   1130: [IFDEF] machine-file drop machine-file 1 [THEN]
                   1131: [IF]   included hex
1.77      jwilke   1132: [ELSE]  cr ." No machine description!" ABORT 
                   1133: [THEN]
1.43      pazsan   1134: 
1.53      jwilke   1135: >ENVIRON
                   1136: 
1.54      pazsan   1137: T has? ec H
                   1138: [IF]
                   1139: false DefaultValue relocate
                   1140: false DefaultValue file
                   1141: false DefaultValue OS
                   1142: false DefaultValue prims
                   1143: false DefaultValue floating
                   1144: false DefaultValue glocals
                   1145: false DefaultValue dcomps
                   1146: false DefaultValue hash
                   1147: false DefaultValue xconds
                   1148: false DefaultValue header
1.105     jwilke   1149: false DefaultValue backtrace
                   1150: false DefaultValue new-input
                   1151: false DefaultValue peephole
1.54      pazsan   1152: [THEN]
                   1153: 
                   1154: true DefaultValue interpreter
                   1155: true DefaultValue ITC
                   1156: false DefaultValue rom
1.73      jwilke   1157: true DefaultValue standardthreading
1.53      jwilke   1158: 
1.52      jwilke   1159: >TARGET
1.53      jwilke   1160: s" relocate" T environment? H 
1.103     jwilke   1161: \ JAW why set NIL to this?!
                   1162: [IF]   drop \ SetValue NIL
1.113     jwilke   1163: [ELSE] >ENVIRON X NIL SetValue relocate
1.53      jwilke   1164: [THEN]
1.113     jwilke   1165: >TARGET
                   1166: 
                   1167: 0 Constant NIL
1.43      pazsan   1168: 
                   1169: >CROSS
                   1170: 
1.52      jwilke   1171: \ \ Create additional parameters                         19jan95py
1.19      pazsan   1172: 
1.71      jwilke   1173: \ currently cross only works for host machines with address-unit-bits
                   1174: \ eual to 8 because of s! and sc!
                   1175: \ but I start to query the environment just to modularize a little bit
                   1176: 
                   1177: : check-address-unit-bits ( -- )       
                   1178: \      s" ADDRESS-UNIT-BITS" environment?
                   1179: \      IF 8 <> ELSE true THEN
                   1180: \      ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
                   1181: 
                   1182: \      shit, this doesn't work because environment? is only defined for 
                   1183: \      gforth.fi and not kernl???.fi
                   1184:        ;
                   1185: 
                   1186: check-address-unit-bits
                   1187: 8 Constant bits/byte   \ we define: byte is address-unit
                   1188: 
                   1189: 1 bits/byte lshift Constant maxbyte 
1.77      jwilke   1190: \ this sets byte size for the target machine, (probably right guess) jaw
1.67      jwilke   1191: 
1.19      pazsan   1192: T
1.71      jwilke   1193: NIL                    Constant TNIL
                   1194: cell                   Constant tcell
                   1195: cell<<                 Constant tcell<<
                   1196: cell>bit               Constant tcell>bit
                   1197: bits/char              Constant tbits/char
                   1198: bits/char H bits/byte T /      
                   1199:                        Constant tchar
                   1200: float                  Constant tfloat
                   1201: 1 bits/char lshift     Constant tmaxchar
                   1202: [IFUNDEF] bits/byte
                   1203: 8                      Constant tbits/byte
                   1204: [ELSE]
                   1205: bits/byte              Constant tbits/byte
                   1206: [THEN]
1.19      pazsan   1207: H
1.81      pazsan   1208: tbits/char bits/byte / Constant tbyte
1.71      jwilke   1209: 
1.19      pazsan   1210: 
1.48      anton    1211: \ Variables                                            06oct92py
                   1212: 
                   1213: Variable image
                   1214: Variable tlast    TNIL tlast !  \ Last name field
                   1215: Variable tlastcfa \ Last code field
                   1216: Variable bit$
1.52      jwilke   1217: 
                   1218: \ statistics                                           10jun97jaw
                   1219: 
                   1220: Variable headers-named 0 headers-named !
                   1221: Variable user-vars 0 user-vars !
                   1222: 
1.67      jwilke   1223: : target>bitmask-size ( u1 -- u2 )
                   1224:   1- tcell>bit rshift 1+ ;
                   1225: 
1.105     jwilke   1226: : allocatetarget ( size -- adr )
1.67      jwilke   1227:   dup allocate ABORT" CROSS: No memory for target"
                   1228:   swap over swap erase ;
1.52      jwilke   1229: 
1.54      pazsan   1230: \ \ memregion.fs
1.52      jwilke   1231: 
                   1232: 
                   1233: Variable last-defined-region    \ pointer to last defined region
                   1234: Variable region-link            \ linked list with all regions
                   1235: Variable mirrored-link          \ linked list for mirrored regions
                   1236: 0 dup mirrored-link ! region-link !
                   1237: 
                   1238: 
1.103     jwilke   1239: : >rname 7 cells + ;
                   1240: : >rbm   4 cells + ;
                   1241: : >rmem  5 cells + ;
                   1242: : >rtype 6 cells + ;
1.67      jwilke   1243: : >rlink 3 cells + ;
1.52      jwilke   1244: : >rdp 2 cells + ;
                   1245: : >rlen cell+ ;
                   1246: : >rstart ;
                   1247: 
1.113     jwilke   1248: : (region) ( addr len region -- )
                   1249: \G change startaddress and length of an existing region
                   1250:   >r r@ last-defined-region !
                   1251:   r@ >rlen ! dup r@ >rstart ! r> >rdp ! ;
1.52      jwilke   1252: 
1.103     jwilke   1253: : region ( addr len -- )                
                   1254: \G create a new region
1.52      jwilke   1255:   \ check whether predefined region exists 
                   1256:   save-input bl word find >r >r restore-input throw r> r> 0= 
                   1257:   IF   \ make region
                   1258:        drop
                   1259:        save-input create restore-input throw
                   1260:        here last-defined-region !
                   1261:        over ( startaddr ) , ( length ) , ( dp ) ,
1.103     jwilke   1262:        region-link linked 0 , 0 , 0 , bl word count string,
1.52      jwilke   1263:   ELSE \ store new parameters in region
                   1264:         bl word drop
1.113     jwilke   1265:        >body (region)
1.52      jwilke   1266:   THEN ;
                   1267: 
1.103     jwilke   1268: : borders ( region -- startaddr endaddr ) 
                   1269: \G returns lower and upper region border
1.67      jwilke   1270:   dup >rstart @ swap >rlen @ over + ;
1.52      jwilke   1271: 
1.103     jwilke   1272: : extent  ( region -- startaddr len )   
                   1273: \G returns the really used area
1.67      jwilke   1274:   dup >rstart @ swap >rdp @ over - ;
1.52      jwilke   1275: 
1.103     jwilke   1276: : area ( region -- startaddr totallen ) 
                   1277: \G returns the total area
1.79      jwilke   1278:   dup >rstart @ swap >rlen @ ;
1.52      jwilke   1279: 
1.103     jwilke   1280: : mirrored                              
                   1281: \G mark a region as mirrored
1.52      jwilke   1282:   mirrored-link
1.68      jwilke   1283:   align linked last-defined-region @ , ;
1.52      jwilke   1284: 
1.67      jwilke   1285: : .addr ( u -- )
                   1286: \G prints a 16 or 32 Bit nice hex value
1.52      jwilke   1287:   base @ >r hex
                   1288:   tcell 2 u>
1.77      jwilke   1289:   IF s>d <# # # # # [char] . hold # # # # #> type
1.52      jwilke   1290:   ELSE s>d <# # # # # # #> type
1.103     jwilke   1291:   THEN r> base ! space ;
1.52      jwilke   1292: 
                   1293: : .regions                      \G display region statistic
                   1294: 
                   1295:   \ we want to list the regions in the right order
                   1296:   \ so first collect all regions on stack
                   1297:   0 region-link @
                   1298:   BEGIN dup WHILE dup @ REPEAT drop
                   1299:   BEGIN dup
1.67      jwilke   1300:   WHILE cr
                   1301:         0 >rlink - >r
                   1302:         r@ >rname count tuck type
1.52      jwilke   1303:         12 swap - 0 max spaces space
1.67      jwilke   1304:         ." Start: " r@ >rstart @ dup .addr space
                   1305:         ." End: " r@ >rlen @ + .addr space
                   1306:         ." DP: " r> >rdp @ .addr
1.52      jwilke   1307:   REPEAT drop
1.53      jwilke   1308:   s" rom" T $has? H 0= ?EXIT
1.52      jwilke   1309:   cr ." Mirrored:"
                   1310:   mirrored-link @
                   1311:   BEGIN dup
1.67      jwilke   1312:   WHILE space dup cell+ @ >rname count type @
1.52      jwilke   1313:   REPEAT drop cr
                   1314:   ;
                   1315: 
                   1316: \ -------- predefined regions
                   1317: 
                   1318: 0 0 region address-space
                   1319: \ total memory addressed and used by the target system
                   1320: 
                   1321: 0 0 region dictionary
                   1322: \ rom area for the compiler
                   1323: 
1.53      jwilke   1324: T has? rom H
1.52      jwilke   1325: [IF]
                   1326: 0 0 region ram-dictionary mirrored
                   1327: \ ram area for the compiler
                   1328: [ELSE]
                   1329: ' dictionary ALIAS ram-dictionary
                   1330: [THEN]
                   1331: 
                   1332: 0 0 region return-stack
                   1333: 
                   1334: 0 0 region data-stack
                   1335: 
                   1336: 0 0 region tib-region
                   1337: 
                   1338: ' dictionary ALIAS rom-dictionary
                   1339: 
                   1340: 
1.105     jwilke   1341: : setup-target ( -- )   \G initialize target's memory space
1.53      jwilke   1342:   s" rom" T $has? H
1.52      jwilke   1343:   IF  \ check for ram and rom...
1.72      jwilke   1344:       \ address-space area nip 0<>
1.67      jwilke   1345:       ram-dictionary area nip 0<>
                   1346:       rom-dictionary area nip 0<>
1.72      jwilke   1347:       and 0=
1.52      jwilke   1348:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
                   1349:   THEN
                   1350:   address-space area nip
                   1351:   IF
                   1352:       address-space area
                   1353:   ELSE
                   1354:       dictionary area
                   1355:   THEN
1.67      jwilke   1356:   nip 0=
1.52      jwilke   1357:   ABORT" CROSS: define at least address-space or dictionary!!"
1.67      jwilke   1358: 
                   1359:   \ allocate target for each region
                   1360:   region-link
                   1361:   BEGIN @ dup
                   1362:   WHILE dup
                   1363:         0 >rlink - >r
                   1364:         r@ >rlen @
                   1365:         IF      \ allocate mem
1.103     jwilke   1366:                 r@ >rlen @ allocatetarget dup image !
1.67      jwilke   1367:                 r@ >rmem !
                   1368: 
1.103     jwilke   1369:                 r@ >rlen @
1.67      jwilke   1370:                 target>bitmask-size allocatetarget
1.72      jwilke   1371:                 dup bit$ !
1.103     jwilke   1372:                 r@ >rbm !
1.67      jwilke   1373: 
1.103     jwilke   1374:                r@ >rlen @
                   1375:                tcell / 1+ cells allocatetarget r@ >rtype !
                   1376: 
                   1377:                rdrop
1.67      jwilke   1378:         ELSE    r> drop THEN
1.72      jwilke   1379:    REPEAT drop ;
                   1380: 
1.105     jwilke   1381: \ MakeKernel                                                           22feb99jaw
1.72      jwilke   1382: 
1.113     jwilke   1383: : makekernel ( targetsize -- )
                   1384: \G convenience word to setup the memory of the target
                   1385: \G used by main.fs of the c-engine based systems
                   1386:   100 swap dictionary (region)
                   1387:   setup-target ;
1.72      jwilke   1388: 
                   1389: >MINIMAL
                   1390: : makekernel makekernel ;
                   1391: >CROSS
1.52      jwilke   1392: 
1.54      pazsan   1393: \ \ switched tdp for rom support                               03jun97jaw
1.52      jwilke   1394: 
                   1395: \ second value is here to store some maximal value for statistics
                   1396: \ tempdp is also embedded here but has nothing to do with rom support
                   1397: \ (needs switched dp)
                   1398: 
                   1399: variable tempdp        0 ,     \ temporary dp for resolving
                   1400: variable tempdp-save
                   1401: 
                   1402: 0 [IF]
                   1403: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
                   1404: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
                   1405: 
                   1406: \
                   1407: variable sramdp                \ start of ram-area for forth
                   1408: variable sromdp                \ start of rom-area for forth
                   1409: 
                   1410: [THEN]
                   1411: 
                   1412: 
                   1413: 0 value tdp
                   1414: variable fixed         \ flag: true: no automatic switching
                   1415:                        \       false: switching is done automatically
                   1416: 
                   1417: \ Switch-Policy:
                   1418: \
                   1419: \ a header is always compiled into rom
                   1420: \ after a created word (create and variable) compilation goes to ram
                   1421: \
                   1422: \ Be careful: If you want to make the data behind create into rom
                   1423: \ you have to put >rom before create!
                   1424: 
                   1425: variable constflag constflag off
                   1426: 
1.67      jwilke   1427: : activate ( region -- )
                   1428: \G next code goes to this region
                   1429:   >rdp to tdp ;
                   1430: 
1.52      jwilke   1431: : (switchram)
1.53      jwilke   1432:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
1.67      jwilke   1433:   ram-dictionary activate ;
1.52      jwilke   1434: 
                   1435: : switchram
                   1436:   constflag @
                   1437:   IF constflag off ELSE (switchram) THEN ;
                   1438: 
                   1439: : switchrom
1.67      jwilke   1440:   fixed @ ?EXIT rom-dictionary activate ;
1.52      jwilke   1441: 
                   1442: : >tempdp ( addr -- ) 
                   1443:   tdp tempdp-save ! tempdp to tdp tdp ! ;
                   1444: : tempdp> ( -- )
                   1445:   tempdp-save @ to tdp ;
                   1446: 
                   1447: : >ram  fixed off (switchram) fixed on ;
                   1448: : >rom  fixed off switchrom fixed on ;
                   1449: : >auto fixed off switchrom ;
                   1450: 
                   1451: 
                   1452: 
                   1453: \ : romstart dup sromdp ! romdp ! ;
                   1454: \ : ramstart dup sramdp ! ramdp ! ;
                   1455: 
1.67      jwilke   1456: \ default compilation goes to rom
1.52      jwilke   1457: \ when romable support is off, only the rom switch is used (!!)
                   1458: >auto
                   1459: 
1.48      anton    1460: : there  tdp @ ;
                   1461: 
1.52      jwilke   1462: >TARGET
1.48      anton    1463: 
1.52      jwilke   1464: \ \ Target Memory Handling
1.1       anton    1465: 
                   1466: \ Byte ordering and cell size                          06oct92py
                   1467: 
1.19      pazsan   1468: : cell+         tcell + ;
                   1469: : cells         tcell<< lshift ;
1.71      jwilke   1470: : chars         tchar * ;
                   1471: : char+                tchar + ;
1.19      pazsan   1472: : floats       tfloat * ;
1.6       anton    1473:     
1.1       anton    1474: >CROSS
1.19      pazsan   1475: : cell/         tcell<< rshift ;
1.1       anton    1476: >TARGET
                   1477: 20 CONSTANT bl
1.52      jwilke   1478: \ TNIL Constant NIL
1.1       anton    1479: 
                   1480: >CROSS
                   1481: 
1.20      pazsan   1482: bigendian
                   1483: [IF]
1.106     jwilke   1484:    : DS!  ( d addr -- )  tcell bounds swap 1-
1.20      pazsan   1485:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
1.106     jwilke   1486:    : DS@  ( addr -- d )  >r 0 0 r> tcell bounds
                   1487:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP ;
1.57      pazsan   1488:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
                   1489:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
                   1490:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
                   1491:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
1.19      pazsan   1492: [ELSE]
1.106     jwilke   1493:    : DS!  ( d addr -- )  tcell bounds
1.20      pazsan   1494:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
1.106     jwilke   1495:    : DS@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
                   1496:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP ;
1.57      pazsan   1497:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
                   1498:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
                   1499:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
                   1500:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
1.1       anton    1501: [THEN]
                   1502: 
1.106     jwilke   1503: : S! ( n addr -- ) >r s>d r> DS! ;
                   1504: : S@ ( addr -- n ) DS@ d>s ;
                   1505: 
1.67      jwilke   1506: : taddr>region ( taddr -- region | 0 )
                   1507: \G finds for a target-address the correct region
                   1508: \G returns 0 if taddr is not in range of a target memory region
                   1509:   region-link
                   1510:   BEGIN @ dup
                   1511:   WHILE dup >r
                   1512:         0 >rlink - >r
                   1513:         r@ >rlen @
                   1514:         IF      dup r@ borders within
                   1515:                 IF r> r> drop nip EXIT THEN
                   1516:         THEN
                   1517:         r> drop
                   1518:         r>
                   1519:   REPEAT
                   1520:   2drop 0 ;
                   1521: 
1.103     jwilke   1522: : taddr>region-abort ( taddr -- region | 0 )
                   1523:   dup taddr>region dup 0= 
                   1524:   IF    drop cr ." Wrong address: " .addr
                   1525:         -1 ABORT" Address out of range!"
                   1526:   THEN nip ;
                   1527: 
1.67      jwilke   1528: : (>regionimage) ( taddr -- 'taddr )
                   1529:   dup
                   1530:   \ find region we want to address
1.103     jwilke   1531:   taddr>region-abort
1.67      jwilke   1532:   >r
                   1533:   \ calculate offset in region
                   1534:   r@ >rstart @ -
                   1535:   \ add regions real address in our memory
                   1536:   r> >rmem @ + ;
                   1537: 
1.103     jwilke   1538: : (>regionbm) ( taddr -- 'taddr bitmaskbaseaddr )
                   1539:   dup
                   1540:   \ find region we want to address
                   1541:   taddr>region-abort
                   1542:   >r
                   1543:   \ calculate offset in region
                   1544:   r@ >rstart @ -
                   1545:   \ add regions real address in our memory
                   1546:   r> >rbm @ ;
                   1547: 
                   1548: : (>regiontype) ( taddr -- 'taddr )
                   1549:   dup
                   1550:   \ find region we want to address
                   1551:   taddr>region-abort
                   1552:   >r
                   1553:   \ calculate offset in region
                   1554:   r@ >rstart @ - tcell / cells
                   1555:   \ add regions real address in our memory
                   1556:   r> >rtype @ + ;
                   1557:   
1.1       anton    1558: \ Bit string manipulation                               06oct92py
                   1559: \                                                       9may93jaw
                   1560: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
                   1561: : bits ( n -- n ) chars Bittable + c@ ;
                   1562: 
                   1563: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
                   1564: : +bit ( addr n -- )  >bit over c@ or swap c! ;
1.4       pazsan   1565: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
1.67      jwilke   1566: 
1.103     jwilke   1567: : @relbit ( taddr -- f ) (>regionbm) swap cell/ >bit swap c@ and ;
                   1568: 
1.84      jwilke   1569: : (relon) ( taddr -- )  
                   1570:   [ [IFDEF] fd-relocation-table ]
                   1571:   s" +" fd-relocation-table write-file throw
                   1572:   dup s>d <# #s #> fd-relocation-table write-line throw
                   1573:   [ [THEN] ]
1.103     jwilke   1574:   (>regionbm) swap cell/ +bit ;
1.84      jwilke   1575: 
                   1576: : (reloff) ( taddr -- ) 
                   1577:   [ [IFDEF] fd-relocation-table ]
                   1578:   s" -" fd-relocation-table write-file throw
                   1579:   dup s>d <# #s #> fd-relocation-table write-line throw
                   1580:   [ [THEN] ]
1.103     jwilke   1581:   (>regionbm) swap cell/ -bit ;
1.67      jwilke   1582: 
                   1583: : (>image) ( taddr -- absaddr ) image @ + ;
                   1584: 
                   1585: DEFER >image
                   1586: DEFER relon
                   1587: DEFER reloff
                   1588: DEFER correcter
                   1589: 
                   1590: T has? relocate H
                   1591: [IF]
                   1592: ' (relon) IS relon
                   1593: ' (reloff) IS reloff
1.103     jwilke   1594: ' (>regionimage) IS >image
1.67      jwilke   1595: [ELSE]
                   1596: ' drop IS relon
                   1597: ' drop IS reloff
1.68      jwilke   1598: ' (>regionimage) IS >image
1.67      jwilke   1599: [THEN]
1.1       anton    1600: 
                   1601: \ Target memory access                                 06oct92py
                   1602: 
                   1603: : align+  ( taddr -- rest )
1.48      anton    1604:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
1.22      anton    1605: : cfalign+  ( taddr -- rest )
1.39      pazsan   1606:     \ see kernel.fs:cfaligned
1.43      pazsan   1607:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
1.1       anton    1608: 
                   1609: >TARGET
                   1610: : aligned ( taddr -- ta-addr )  dup align+ + ;
                   1611: \ assumes cell alignment granularity (as GNU C)
                   1612: 
1.22      anton    1613: : cfaligned ( taddr1 -- taddr2 )
1.39      pazsan   1614:     \ see kernel.fs
1.22      anton    1615:     dup cfalign+ + ;
                   1616: 
1.52      jwilke   1617: : @  ( taddr -- w )     >image S@ ;
                   1618: : !  ( w taddr -- )     >image S! ;
1.57      pazsan   1619: : c@ ( taddr -- char )  >image Sc@ ;
                   1620: : c! ( char taddr -- )  >image Sc! ;
1.7       anton    1621: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
1.82      pazsan   1622: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
1.1       anton    1623: 
                   1624: \ Target compilation primitives                        06oct92py
                   1625: \ included A!                                          16may93jaw
                   1626: 
                   1627: : here  ( -- there )    there ;
                   1628: : allot ( n -- )        tdp +! ;
1.78      jwilke   1629: : ,     ( w -- )        T here H tcell T allot  ! H ;
                   1630: : c,    ( char -- )     T here H tchar T allot c! H ;
                   1631: : align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
1.22      anton    1632: : cfalign ( -- )
1.78      jwilke   1633:     T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
1.1       anton    1634: 
1.71      jwilke   1635: : >address             dup 0>= IF tbyte / THEN ; \ ?? jaw 
1.59      pazsan   1636: : A!                    swap >address swap dup relon T ! H ;
                   1637: : A,    ( w -- )        >address T here H relon T , H ;
1.1       anton    1638: 
1.103     jwilke   1639: 
                   1640: \ \ --------------------        Host/Target copy etc.          29aug01jaw
                   1641: 
                   1642: 
                   1643: >CROSS
                   1644: 
1.107     jwilke   1645: : TD! >image DS! ;
                   1646: : TD@ >image DS@ ;
                   1647: 
1.103     jwilke   1648: : th-count ( taddr -- host-addr len )
                   1649: \G returns host address of target string
                   1650:   assert1( tbyte 1 = )
                   1651:   dup X c@ swap X char+ >image swap ;
                   1652: 
                   1653: : ht-move ( haddr taddr len -- )
                   1654: \G moves data from host-addr to destination in target-addr
                   1655: \G character by character
                   1656:   swap -rot bounds ?DO I c@ over X c! X char+ LOOP drop ;
                   1657: 
                   1658: 2Variable last-string
                   1659: 
                   1660: : ht-string,  ( addr count -- )
                   1661:   dup there swap last-string 2!
                   1662:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
                   1663: 
                   1664: >TARGET
                   1665: 
                   1666: : count dup X c@ swap X char+ swap ;
1.110     jwilke   1667: 
                   1668: : on           -1 -1 rot TD!  ; 
1.103     jwilke   1669: : off          T 0 swap ! H ;
1.1       anton    1670: 
1.107     jwilke   1671: : tcmove ( source dest len -- )
                   1672: \G cmove in target memory
                   1673:   tchar * bounds
                   1674:   ?DO  dup T c@ H I T c! H 1+
                   1675:   tchar +LOOP  drop ;
                   1676: 
                   1677: : td, ( d -- )
                   1678: \G Store a host value as one cell into the target
                   1679:   there tcell X allot TD! ;
                   1680: 
                   1681: \ \ Load Assembler
                   1682: 
                   1683: >TARGET
                   1684: H also Forth definitions
                   1685: 
                   1686: \ FIXME: should we include the assembler really in the forth 
                   1687: \ dictionary?!?!?!? This conflicts with the existing assembler 
                   1688: \ of the host forth system!!
                   1689: [IFDEF] asm-include asm-include [THEN] hex
                   1690: 
                   1691: previous
                   1692: 
                   1693: 
1.52      jwilke   1694: >CROSS
                   1695: 
1.103     jwilke   1696: : (cc) T a, H ;                                        ' (cc) plugin-of colon,
1.105     jwilke   1697: : (prim) T a, H ;                              ' (prim) plugin-of prim,
1.52      jwilke   1698: 
1.105     jwilke   1699: : (cr) >tempdp ]comp prim, comp[ tempdp> ;     ' (cr) plugin-of colon-resolve
1.103     jwilke   1700: : (ar) T ! H ;                                 ' (ar) plugin-of addr-resolve
1.54      pazsan   1701: : (dr)  ( ghost res-pnt target-addr addr )
                   1702:        >tempdp drop over 
                   1703:        dup >magic @ <do:> =
                   1704:        IF      doer,
                   1705:        ELSE    dodoes,
                   1706:        THEN 
1.103     jwilke   1707:        tempdp> ;                               ' (dr) plugin-of doer-resolve
1.54      pazsan   1708: 
                   1709: : (cm) ( -- addr )
                   1710:     T here align H
1.109     jwilke   1711:     -1 prim, ;                                 ' (cm) plugin-of colonmark,
1.1       anton    1712: 
1.52      jwilke   1713: >TARGET
1.105     jwilke   1714: : compile, ( xt -- )
1.107     jwilke   1715:   dup xt>ghost >comp @ EXECUTE ;
1.52      jwilke   1716: >CROSS
1.1       anton    1717: 
1.102     jwilke   1718: \ resolve structure
                   1719: 
                   1720: : >next ;              \ link to next field
                   1721: : >tag cell+ ;         \ indecates type of reference: 0: call, 1: address, 2: doer
                   1722: : >taddr cell+ cell+ ; 
                   1723: : >ghost 3 cells + ;
                   1724: : >file 4 cells + ;
                   1725: : >line 5 cells + ;
                   1726: 
                   1727: : (refered) ( ghost addr tag -- )
                   1728: \G creates a reference to ghost at address taddr
1.103     jwilke   1729:     >space
                   1730:     rot >link linked
1.102     jwilke   1731:     ( taddr tag ) ,
                   1732:     ( taddr ) , 
                   1733:     last-header-ghost @ , 
                   1734:     loadfile , 
                   1735:     sourceline# , 
1.103     jwilke   1736:     space>
1.102     jwilke   1737:   ;
                   1738: 
1.52      jwilke   1739: : refered ( ghost tag -- )
1.53      jwilke   1740: \G creates a resolve structure
1.54      pazsan   1741:     T here aligned H swap (refered)
                   1742:   ;
                   1743: 
                   1744: : killref ( addr ghost -- )
                   1745: \G kills a forward reference to ghost at position addr
                   1746: \G this is used to eleminate a :dovar refence after making a DOES>
                   1747:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
                   1748:     swap >r >link
                   1749:     BEGIN dup @ dup  ( addr last this )
                   1750:     WHILE dup >taddr @ r@ =
                   1751:         IF   @ over !
                   1752:         ELSE nip THEN
                   1753:     REPEAT rdrop 2drop 
1.53      jwilke   1754:   ;
1.48      anton    1755: 
1.52      jwilke   1756: Defer resolve-warning
1.1       anton    1757: 
1.52      jwilke   1758: : reswarn-test ( ghost res-struct -- ghost res-struct )
1.78      jwilke   1759:   over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
1.1       anton    1760: 
1.52      jwilke   1761: : reswarn-forward ( ghost res-struct -- ghost res-struct )
1.78      jwilke   1762:   over warnhead .ghost dup ."  is referenced in " 
                   1763:   >ghost @ .ghost ;
1.1       anton    1764: 
1.52      jwilke   1765: \ ' reswarn-test IS resolve-warning
                   1766:  
1.1       anton    1767: \ resolve                                              14oct92py
                   1768: 
1.54      pazsan   1769:  : resolve-loop ( ghost resolve-list tcfa -- )
                   1770:     >r
                   1771:     BEGIN dup WHILE 
                   1772: \        dup >tag @ 2 = IF reswarn-forward THEN
                   1773:          resolve-warning 
                   1774:          r@ over >taddr @ 
                   1775:          2 pick >tag @
                   1776:          CASE  0 OF colon-resolve ENDOF
                   1777:                1 OF addr-resolve ENDOF
                   1778:                2 OF doer-resolve ENDOF
                   1779:          ENDCASE
                   1780:          @ \ next list element
                   1781:     REPEAT 2drop rdrop 
                   1782:   ;
1.52      jwilke   1783: 
                   1784: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
                   1785: \  >r dup >link @
                   1786: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
1.1       anton    1787: 
                   1788: \ exists                                                9may93jaw
                   1789: 
1.103     jwilke   1790: : exists ( ghost tcfa -- )
                   1791: \G print warning and set new target link in ghost
                   1792:   swap exists-warning
                   1793:   >link ! ;
1.52      jwilke   1794: 
1.105     jwilke   1795: : colon-resolved   ( ghost -- )
                   1796:     >link @ colon, ; \ compile-call
                   1797: 
                   1798: : prim-resolved  ( ghost -- )
                   1799:     >link @ prim, ;
                   1800: 
1.110     jwilke   1801: \ FIXME: not used currently
1.105     jwilke   1802: : does-resolved ( ghost -- )
                   1803:     dup g>body alit, >do:ghost @ g>body colon, ;
                   1804: 
                   1805: : (is-forward)   ( ghost -- )
                   1806:   colonmark, 0 (refered) ; \ compile space for call
                   1807: ' (is-forward) IS is-forward
1.1       anton    1808: 
                   1809: : resolve  ( ghost tcfa -- )
1.54      pazsan   1810: \G resolve referencies to ghost with tcfa
1.103     jwilke   1811:     dup taddr>region 0<> IF
                   1812:       2dup (>regiontype) define-addr-struct addr-xt-ghost 
                   1813: 
                   1814:       \ we define new address only if empty
1.105     jwilke   1815:       \ this is for not to take over the alias ghost
                   1816:       \ (different ghost, but identical xt)
1.103     jwilke   1817:       \ but the very first that really defines it
                   1818:       dup @ 0= IF ! ELSE 2drop THEN
                   1819:     THEN
                   1820: 
                   1821:     \ is ghost resolved?, second resolve means another 
                   1822:     \ definition with the same name
1.75      jwilke   1823:     over undefined? 0= IF  exists EXIT THEN
1.54      pazsan   1824:     \ get linked-list
                   1825:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
                   1826:     \ mark ghost as resolved
                   1827:     dup r@ >link ! <res> r@ >magic !
1.105     jwilke   1828:     r@ >comp @ ['] is-forward = IF
                   1829:        ['] prim-resolved  r@ >comp !  THEN
1.54      pazsan   1830:     \ loop through forward referencies
                   1831:     r> -rot 
                   1832:     comp-state @ >r Resolving comp-state !
                   1833:     resolve-loop 
                   1834:     r> comp-state !
                   1835: 
                   1836:     ['] noop IS resolve-warning 
1.52      jwilke   1837:   ;
1.1       anton    1838: 
                   1839: \ gexecute ghost,                                      01nov92py
                   1840: 
1.103     jwilke   1841: : (gexecute)   ( ghost -- )
1.105     jwilke   1842:   dup >comp @ EXECUTE ;
1.103     jwilke   1843: 
                   1844: : gexecute ( ghost -- )
1.110     jwilke   1845:   dup >magic @ <imm> = IF -1 ABORT" CROSS: gexecute on immediate word" THEN
1.103     jwilke   1846:   (gexecute) ;
1.52      jwilke   1847: 
                   1848: : addr,  ( ghost -- )
1.105     jwilke   1849:   dup forward? IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
1.52      jwilke   1850: 
1.1       anton    1851: \ .unresolved                                          11may93jaw
                   1852: 
                   1853: variable ResolveFlag
                   1854: 
                   1855: \ ?touched                                             11may93jaw
                   1856: 
1.52      jwilke   1857: : ?touched ( ghost -- flag ) dup forward? swap >link @
1.1       anton    1858:                                0 <> and ;
                   1859: 
1.53      jwilke   1860: : .forwarddefs ( ghost -- )
1.103     jwilke   1861:   ."  appeared in:"
                   1862:   >link
                   1863:   BEGIN        @ dup
                   1864:   WHILE        cr 5 spaces
                   1865:        dup >ghost @ .ghost
                   1866:        ."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
                   1867:        ."  line " dup >line @ .dec
                   1868:   REPEAT 
                   1869:   drop ;
                   1870: 
                   1871: : ?resolved  ( ghost -- )
                   1872:   dup ?touched
                   1873:   IF   ResolveFlag on 
                   1874:        dup cr .ghost .forwarddefs
1.53      jwilke   1875:   ELSE         drop 
                   1876:   THEN ;
1.1       anton    1877: 
                   1878: : .unresolved  ( -- )
                   1879:   ResolveFlag off cr ." Unresolved: "
1.103     jwilke   1880:   ghost-list
1.1       anton    1881:   BEGIN @ dup
                   1882:   WHILE dup ?resolved
1.10      anton    1883:   REPEAT drop ResolveFlag @
                   1884:   IF
1.48      anton    1885:       -1 abort" Unresolved words!"
1.10      anton    1886:   ELSE
                   1887:       ." Nothing!"
                   1888:   THEN
                   1889:   cr ;
1.1       anton    1890: 
1.52      jwilke   1891: : .stats
                   1892:   base @ >r decimal
                   1893:   cr ." named Headers: " headers-named @ . 
                   1894:   r> base ! ;
                   1895: 
1.79      jwilke   1896: >MINIMAL
                   1897: 
                   1898: : .unresolved .unresolved ;
                   1899: 
1.1       anton    1900: >CROSS
                   1901: \ Header states                                        12dec92py
                   1902: 
1.102     jwilke   1903: \ : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
1.90      pazsan   1904: bigendian [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+
                   1905: : flag! ( w -- )   tlast @ flag+ + dup >r T c@ xor r> c! H ;
1.1       anton    1906: 
                   1907: VARIABLE ^imm
                   1908: 
1.105     jwilke   1909: \ !! should be target wordsize specific
                   1910: $80 constant alias-mask
                   1911: $40 constant immediate-mask
                   1912: $20 constant restrict-mask
                   1913: 
1.1       anton    1914: >TARGET
1.105     jwilke   1915: : immediate     immediate-mask flag!
1.18      pazsan   1916:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
1.1       anton    1917:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                   1918:                 <imm> ^imm @ ! ;
1.105     jwilke   1919: : restrict      restrict-mask flag! ;
1.52      jwilke   1920: 
1.54      pazsan   1921: : isdoer       
                   1922: \G define a forth word as doer, this makes obviously only sence on
                   1923: \G forth processors such as the PSC1000
                   1924:                <do:> last-header-ghost @ >magic ! ;
1.1       anton    1925: >CROSS
                   1926: 
                   1927: \ Target Header Creation                               01nov92py
                   1928: 
1.103     jwilke   1929: : ht-lstring, ( addr count -- )
1.102     jwilke   1930:   dup T , H bounds  ?DO  I c@ T c, H  LOOP ;
                   1931: 
1.103     jwilke   1932: >TARGET
                   1933: : name,  ( "name" -- )  bl word count ht-lstring, X cfalign ;
1.1       anton    1934: : view,   ( -- ) ( dummy ) ;
1.52      jwilke   1935: >CROSS
1.1       anton    1936: 
1.25      pazsan   1937: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
                   1938: 
1.55      pazsan   1939: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
1.25      pazsan   1940: \ contains the file-id of the documentation file
                   1941: 
1.40      pazsan   1942: : T-\G ( -- )
1.25      pazsan   1943:     source >in @ /string doc-file-id write-line throw
1.40      pazsan   1944:     postpone \ ;
1.25      pazsan   1945: 
1.39      pazsan   1946: Variable to-doc  to-doc on
1.25      pazsan   1947: 
                   1948: : cross-doc-entry  ( -- )
                   1949:     to-doc @ tlast @ 0<> and   \ not an anonymous (i.e. noname) header
                   1950:     IF
                   1951:        s" " doc-file-id write-line throw
                   1952:        s" make-doc " doc-file-id write-file throw
1.102     jwilke   1953: 
1.105     jwilke   1954:        Last-Header-Ghost @ >ghostname doc-file-id write-file throw
1.25      pazsan   1955:        >in @
                   1956:        [char] ( parse 2drop
                   1957:        [char] ) parse doc-file-id write-file throw
                   1958:        s"  )" doc-file-id write-file throw
                   1959:        [char] \ parse 2drop                                    
1.40      pazsan   1960:        T-\G
1.25      pazsan   1961:        >in !
1.39      pazsan   1962:     THEN ;
1.25      pazsan   1963: 
1.28      pazsan   1964: \ Target TAGS creation
                   1965: 
1.39      pazsan   1966: s" kernel.TAGS" r/w create-file throw value tag-file-id
1.116   ! pazsan   1967: s" kernel.tags" r/w create-file throw value vi-tag-file-id
1.28      pazsan   1968: \ contains the file-id of the tags file
                   1969: 
                   1970: Create tag-beg 2 c,  7F c, bl c,
                   1971: Create tag-end 2 c,  bl c, 01 c,
                   1972: Create tag-bof 1 c,  0C c,
1.116   ! pazsan   1973: Create tag-tab 1 c,  09 c,
1.28      pazsan   1974: 
                   1975: 2variable last-loadfilename 0 0 last-loadfilename 2!
                   1976:            
                   1977: : put-load-file-name ( -- )
1.77      jwilke   1978:     sourcefilename last-loadfilename 2@ d<>
1.28      pazsan   1979:     IF
                   1980:        tag-bof count tag-file-id write-line throw
1.31      anton    1981:        sourcefilename 2dup
1.28      pazsan   1982:        tag-file-id write-file throw
                   1983:        last-loadfilename 2!
                   1984:        s" ,0" tag-file-id write-line throw
                   1985:     THEN ;
                   1986: 
1.116   ! pazsan   1987: : cross-gnu-tag-entry  ( -- )
1.28      pazsan   1988:     tlast @ 0<>        \ not an anonymous (i.e. noname) header
                   1989:     IF
                   1990:        put-load-file-name
                   1991:        source >in @ min tag-file-id write-file throw
                   1992:        tag-beg count tag-file-id write-file throw
1.116   ! pazsan   1993:        Last-Header-Ghost @ >ghostname tag-file-id write-file throw
1.28      pazsan   1994:        tag-end count tag-file-id write-file throw
1.31      anton    1995:        base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
1.28      pazsan   1996: \      >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
                   1997:        s" ,0" tag-file-id write-line throw
                   1998:        base !
                   1999:     THEN ;
1.116   ! pazsan   2000: 
        !          2001: : cross-vi-tag-entry ( -- )
        !          2002:     tlast @ 0<>        \ not an anonymous (i.e. noname) header
        !          2003:     IF
        !          2004:        sourcefilename vi-tag-file-id write-file throw
        !          2005:        tag-tab count vi-tag-file-id write-file throw
        !          2006:        Last-Header-Ghost @ >ghostname vi-tag-file-id write-file throw
        !          2007:        tag-tab count vi-tag-file-id write-file throw
        !          2008:        s" /^" vi-tag-file-id write-file throw
        !          2009:        source vi-tag-file-id write-file throw
        !          2010:        s" $/" vi-tag-file-id write-line throw
        !          2011:     THEN ;
        !          2012: 
        !          2013: : cross-tag-entry ( -- )
        !          2014:     cross-gnu-tag-entry
        !          2015:     cross-vi-tag-entry ;
1.28      pazsan   2016: 
1.43      pazsan   2017: \ Check for words
                   2018: 
                   2019: Defer skip? ' false IS skip?
                   2020: 
1.103     jwilke   2021: : skipdef ( "name" -- )
1.79      jwilke   2022: \G skip definition of an undefined word in undef-words and
                   2023: \G all-words mode
1.103     jwilke   2024:     Ghost dup forward?
1.75      jwilke   2025:     IF  >magic <skip> swap !
                   2026:     ELSE drop THEN ;
                   2027: 
1.103     jwilke   2028: : tdefined? ( "name" -- flag ) 
                   2029:     Ghost undefined? 0= ;
1.75      jwilke   2030: 
1.103     jwilke   2031: : defined2? ( "name" -- flag ) 
1.75      jwilke   2032: \G return true for anything else than forward, even for <skip>
                   2033: \G that's what we want
1.103     jwilke   2034:     Ghost forward? 0= ;
1.43      pazsan   2035: 
1.103     jwilke   2036: : forced? ( "name" -- flag )
1.79      jwilke   2037: \G return ture if it is a foreced skip with defskip
1.103     jwilke   2038:     Ghost >magic @ <skip> = ;
1.79      jwilke   2039: 
1.43      pazsan   2040: : needed? ( -- flag ) \ name
1.48      anton    2041: \G returns a false flag when
                   2042: \G a word is not defined
                   2043: \G a forward reference exists
                   2044: \G so the definition is not skipped!
                   2045:     bl word gfind
1.75      jwilke   2046:     IF dup undefined?
1.48      anton    2047:        nip
                   2048:        0=
                   2049:     ELSE  drop true  THEN ;
1.43      pazsan   2050: 
1.44      pazsan   2051: : doer? ( -- flag ) \ name
1.103     jwilke   2052:     Ghost >magic @ <do:> = ;
1.44      pazsan   2053: 
1.43      pazsan   2054: : skip-defs ( -- )
                   2055:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
                   2056: 
1.28      pazsan   2057: \ Target header creation
                   2058: 
1.54      pazsan   2059: Variable NoHeaderFlag
                   2060: NoHeaderFlag off
1.1       anton    2061: 
1.54      pazsan   2062: : 0.r ( n1 n2 -- ) 
                   2063:     base @ >r hex 
                   2064:     0 swap <# 0 ?DO # LOOP #> type 
                   2065:     r> base ! ;
1.84      jwilke   2066: 
                   2067: : .sym ( adr len -- )
                   2068: \G escapes / and \ to produce sed output
1.52      jwilke   2069:   bounds 
                   2070:   DO I c@ dup
1.77      jwilke   2071:        CASE    [char] / OF drop ." \/" ENDOF
                   2072:                [char] \ OF drop ." \\" ENDOF
1.52      jwilke   2073:                dup OF emit ENDOF
                   2074:        ENDCASE
1.54      pazsan   2075:     LOOP ;
1.52      jwilke   2076: 
1.103     jwilke   2077: Defer setup-execution-semantics
                   2078: 0 Value lastghost
                   2079: 
                   2080: : (THeader ( "name" -- ghost )
1.54      pazsan   2081:     \  >in @ bl word count type 2 spaces >in !
                   2082:     \ wordheaders will always be compiled to rom
                   2083:     switchrom
                   2084:     \ build header in target
                   2085:     NoHeaderFlag @
                   2086:     IF  NoHeaderFlag off
                   2087:     ELSE
                   2088:        T align H view,
1.78      jwilke   2089:        tlast @ dup 0> IF tcell - THEN T A, H  there tlast !
1.54      pazsan   2090:        1 headers-named +!      \ Statistic
                   2091:        >in @ T name, H >in !
                   2092:     THEN
                   2093:     T cfalign here H tlastcfa !
1.84      jwilke   2094:     \ Old Symbol table sed-script
1.54      pazsan   2095: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
1.103     jwilke   2096:     HeaderGhost
1.84      jwilke   2097:     \ output symbol table to extra file
                   2098:     [ [IFDEF] fd-symbol-table ]
                   2099:       base @ hex there s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base !
                   2100:       s" :" fd-symbol-table write-file throw
                   2101:       dup >ghostname fd-symbol-table write-line throw
                   2102:     [ [THEN] ]
1.103     jwilke   2103:     dup Last-Header-Ghost ! dup to lastghost
1.54      pazsan   2104:     dup >magic ^imm !     \ a pointer for immediate
1.105     jwilke   2105:     alias-mask flag!
1.103     jwilke   2106:     cross-doc-entry cross-tag-entry 
                   2107:     setup-execution-semantics
                   2108:     ;
1.1       anton    2109: 
1.52      jwilke   2110: \ this is the resolver information from ":"
                   2111: \ resolving is done by ";"
1.103     jwilke   2112: Variable ;Resolve 1 cells allot
                   2113: 
                   2114: : hereresolve ( ghost -- )
                   2115:   there resolve 0 ;Resolve ! ;
1.1       anton    2116: 
1.11      pazsan   2117: : Theader  ( "name" -- ghost )
1.103     jwilke   2118:   (THeader dup hereresolve ;
                   2119: 
                   2120: Variable aprim-nr -20 aprim-nr !
                   2121: 
                   2122: : copy-execution-semantics ( ghost-from ghost-dest -- )
                   2123:   >r
                   2124:   dup >exec @ r@ >exec !
                   2125:   dup >exec2 @ r@ >exec2 !
                   2126:   dup >exec-compile @ r@ >exec-compile !
                   2127:   dup >ghost-xt @ r@ >ghost-xt !
                   2128:   dup >created @ r@ >created !
                   2129:   rdrop drop ;
1.1       anton    2130: 
                   2131: >TARGET
1.103     jwilke   2132: 
1.1       anton    2133: : Alias    ( cfa -- ) \ name
1.103     jwilke   2134:   >in @ skip? IF  2drop  EXIT  THEN  >in !
                   2135:   (THeader ( S xt ghost )
1.105     jwilke   2136:   2dup swap xt>ghost swap copy-execution-semantics
                   2137:   over resolve T A, H alias-mask flag! ;
1.103     jwilke   2138: 
                   2139: Variable last-prim-ghost
                   2140: 0 last-prim-ghost !
                   2141: 
                   2142: : asmprimname, ( ghost -- : name ) 
                   2143:   dup last-prim-ghost !
                   2144:   >r
                   2145:   here bl word count string, r@ >asm-name !
                   2146:   aprim-nr @ r> >asm-dummyaddr ! ;
                   2147: 
                   2148: Defer setup-prim-semantics
                   2149: 
1.112     jwilke   2150: : mapprim   ( "forthname" "asmlabel" -- ) 
1.103     jwilke   2151:   THeader -1 aprim-nr +! aprim-nr @ T A, H
                   2152:   asmprimname, 
                   2153:   setup-prim-semantics ;
                   2154: 
1.112     jwilke   2155: : mapprim:   ( "forthname" "asmlabel" -- ) 
1.103     jwilke   2156:   -1 aprim-nr +! aprim-nr @
                   2157:   Ghost tuck swap resolve <do:> swap tuck >magic !
                   2158:   asmprimname, ;
                   2159: 
1.42      pazsan   2160: : Alias:   ( cfa -- ) \ name
1.103     jwilke   2161:   >in @ skip? IF  2drop  EXIT  THEN  >in !
                   2162:   dup 0< s" prims" T $has? H 0= and
                   2163:   IF
                   2164:       .sourcepos ." needs doer: " >in @ bl word count type >in ! cr
                   2165:   THEN
                   2166:   Ghost tuck swap resolve <do:> swap >magic ! ;
1.64      pazsan   2167: 
                   2168: Variable prim#
                   2169: : first-primitive ( n -- )  prim# ! ;
                   2170: : Primitive  ( -- ) \ name
1.103     jwilke   2171:   >in @ skip? IF  2drop  EXIT  THEN  >in !
                   2172:   dup 0< s" prims" T $has? H 0= and
                   2173:   IF
                   2174:      .sourcepos ." needs prim: " >in @ bl word count type >in ! cr
                   2175:   THEN
                   2176:   prim# @ (THeader ( S xt ghost )
1.105     jwilke   2177:   dup >ghost-flags <primitive> set-flag
                   2178:   over resolve T A, H alias-mask flag!
1.103     jwilke   2179:   -1 prim# +! ;
1.1       anton    2180: >CROSS
                   2181: 
                   2182: \ Conditionals and Comments                            11may93jaw
                   2183: 
1.103     jwilke   2184: \G saves the existing cond action, this is used for redefining in
                   2185: \G instant
                   2186: Variable cond-xt-old
                   2187: 
                   2188: : cond-target ( -- )
                   2189: \G Compiles semantic of redefined cond into new one
                   2190:   cond-xt-old @ compile, ; immediate restrict
                   2191: 
1.1       anton    2192: : ;Cond
                   2193:   postpone ;
                   2194:   swap ! ;  immediate
                   2195: 
1.103     jwilke   2196: : Cond: ( "name" -- ) 
                   2197: \g defines a conditional or another word that must
                   2198: \g be executed directly while compiling
                   2199: \g these words have no interpretative semantics by default
                   2200:   Ghost
                   2201:   >exec-compile
                   2202:   dup @ cond-xt-old !
1.1       anton    2203:   :NONAME ;
                   2204: 
                   2205: 
                   2206: : Comment ( -- )
1.103     jwilke   2207:   >in @ Ghost swap >in ! ' swap 
                   2208:   2dup >exec-compile ! >exec ! ;
1.1       anton    2209: 
                   2210: Comment (       Comment \
                   2211: 
                   2212: \ compile                                              10may93jaw
                   2213: 
1.103     jwilke   2214: : compile  ( "name" -- ) \ name
1.115     jwilke   2215:   findghost
1.103     jwilke   2216:   dup >exec-compile @ ?dup
                   2217:   IF    nip compile,
                   2218:   ELSE  postpone literal postpone gexecute  THEN ;  immediate restrict
                   2219:             
1.52      jwilke   2220: >TARGET
                   2221: 
1.105     jwilke   2222: : '  ( -- xt ) 
                   2223: \G returns the target-cfa of a ghost
1.52      jwilke   2224:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1.105     jwilke   2225:   g>xt ;
1.52      jwilke   2226: 
1.103     jwilke   2227: \ FIXME: this works for the current use cases, but is not
                   2228: \ in all cases correct ;-) 
                   2229: : comp' X ' 0 ;
                   2230: 
1.52      jwilke   2231: Cond: [']  T ' H alit, ;Cond
                   2232: 
                   2233: >CROSS
                   2234: 
                   2235: : [T']
                   2236: \ returns the target-cfa of a ghost, or compiles it as literal
1.115     jwilke   2237:   postpone [G'] 
                   2238:   state @ IF postpone g>xt ELSE g>xt THEN ; immediate
1.42      pazsan   2239: 
1.52      jwilke   2240: \ \ threading modell                                   13dec92py
                   2241: \ modularized                                          14jun97jaw
                   2242: 
1.106     jwilke   2243: T 2 cells H Value xt>body
1.52      jwilke   2244: 
1.103     jwilke   2245: : (>body)   ( cfa -- pfa ) 
1.105     jwilke   2246:   xt>body + ;                                          ' (>body) plugin-of t>body
                   2247: 
                   2248: : fillcfa   ( usedcells -- )
1.106     jwilke   2249:   T cells H xt>body swap -
1.105     jwilke   2250:   assert1( dup 0 >= )
                   2251:   0 ?DO 0 X c, tchar +LOOP ;
1.52      jwilke   2252: 
1.103     jwilke   2253: : (doer,)   ( ghost -- ) 
1.105     jwilke   2254:   addr, 1 fillcfa ;                                    ' (doer,) plugin-of doer,
1.52      jwilke   2255: 
1.105     jwilke   2256: : (docol,)  ( -- ) [G'] :docol (doer,) ;               ' (docol,) plugin-of docol,
1.52      jwilke   2257: 
                   2258: : (doprim,) ( -- )
1.105     jwilke   2259:   there xt>body + ca>native T a, H 1 fillcfa ;         ' (doprim,) plugin-of doprim,
1.52      jwilke   2260: 
                   2261: : (doeshandler,) ( -- ) 
1.105     jwilke   2262:   T cfalign H compile :doesjump T 0 , H ;              ' (doeshandler,) plugin-of doeshandler,
1.52      jwilke   2263: 
                   2264: : (dodoes,) ( does-action-ghost -- )
                   2265:   ]comp [G'] :dodoes gexecute comp[
                   2266:   addr,
1.103     jwilke   2267:   \ the relocator in the c engine, does not like the
                   2268:   \ does-address to marked for relocation
                   2269:   [ T e? ec H 0= [IF] ] T here H tcell - reloff [ [THEN] ]
1.105     jwilke   2270:   2 fillcfa ;                                          ' (dodoes,) plugin-of dodoes,
1.52      jwilke   2271: 
1.107     jwilke   2272: : (dlit,) ( n -- ) compile lit td, ;                   ' (dlit,) plugin-of dlit,
                   2273: 
                   2274: : (lit,) ( n -- )  s>d dlit, ;                         ' (lit,) plugin-of lit,
1.52      jwilke   2275: 
1.67      jwilke   2276: \ if we dont produce relocatable code alit, defaults to lit, jaw
1.84      jwilke   2277: \ this is just for convenience, so we don't have to define alit,
                   2278: \ seperately for embedded systems....
                   2279: T has? relocate H
1.67      jwilke   2280: [IF]
1.105     jwilke   2281: : (alit,) ( n -- )  compile lit T  a, H ;              ' (alit,) plugin-of alit,
1.67      jwilke   2282: [ELSE]
1.105     jwilke   2283: : (alit,) ( n -- )  lit, ;                             ' (alit,) plugin-of alit,
1.67      jwilke   2284: [THEN]
1.52      jwilke   2285: 
1.105     jwilke   2286: : (fini,)         compile ;s ;                         ' (fini,) plugin-of fini,
1.42      pazsan   2287: 
1.43      pazsan   2288: [IFUNDEF] (code) 
                   2289: Defer (code)
                   2290: Defer (end-code)
                   2291: [THEN]
                   2292: 
1.1       anton    2293: >TARGET
1.43      pazsan   2294: : Code
1.52      jwilke   2295:   defempty?
1.48      anton    2296:   (THeader there resolve
1.53      jwilke   2297:   [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
1.52      jwilke   2298:   doprim, 
1.48      anton    2299:   [THEN]
                   2300:   depth (code) ;
1.43      pazsan   2301: 
                   2302: : Code:
1.52      jwilke   2303:   defempty?
1.103     jwilke   2304:     Ghost dup there ca>native resolve  <do:> swap >magic !
1.43      pazsan   2305:     depth (code) ;
                   2306: 
                   2307: : end-code
1.52      jwilke   2308:     (end-code)
1.43      pazsan   2309:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                   2310:     ELSE true ABORT" CROSS: Stack empty" THEN
1.52      jwilke   2311:     ;
1.14      anton    2312: 
1.1       anton    2313: >CROSS
1.52      jwilke   2314: 
1.1       anton    2315: \ tLiteral                                             12dec92py
                   2316: 
                   2317: >TARGET
1.40      pazsan   2318: Cond: \G  T-\G ;Cond
                   2319: 
1.105     jwilke   2320: Cond: Literal  ( n -- )   lit, ;Cond
1.103     jwilke   2321: Cond: ALiteral ( n -- )   alit, ;Cond
1.1       anton    2322: 
                   2323: : Char ( "<char>" -- )  bl word char+ c@ ;
1.103     jwilke   2324: Cond: [Char]   ( "<char>" -- )  Char  lit, ;Cond
                   2325: 
1.104     jwilke   2326: tchar 1 = [IF]
1.110     jwilke   2327: Cond: chars ;Cond 
1.104     jwilke   2328: [THEN]
1.1       anton    2329: 
1.43      pazsan   2330: \ some special literals                                        27jan97jaw
                   2331: 
1.52      jwilke   2332: \ !! Known Bug: Special Literals and plug-ins work only correct
1.102     jwilke   2333: \ on 16 and 32 Bit Targets and 32 Bit Hosts!
1.52      jwilke   2334: 
1.107     jwilke   2335: \ This section could be done with dlit, now. But first I need
                   2336: \ some test code JAW
                   2337: 
1.43      pazsan   2338: Cond: MAXU
1.102     jwilke   2339:   tcell 1 cells u> 
                   2340:   IF   compile lit tcell 0 ?DO FF T c, H LOOP 
                   2341:   ELSE ffffffff lit, THEN
1.52      jwilke   2342:   ;Cond
1.43      pazsan   2343: 
                   2344: Cond: MINI
1.102     jwilke   2345:   tcell 1 cells u>
                   2346:   IF   compile lit bigendian 
                   2347:        IF      80 T c, H tcell 1 ?DO 0 T c, H LOOP 
                   2348:        ELSE    tcell 1 ?DO 0 T c, H LOOP 80 T c, H
                   2349:        THEN
                   2350:   ELSE tcell 2 = IF 8000 ELSE 80000000 THEN lit, THEN
1.52      jwilke   2351:   ;Cond
1.43      pazsan   2352:  
                   2353: Cond: MAXI
1.102     jwilke   2354:  tcell 1 cells u>
                   2355:  IF    compile lit bigendian 
                   2356:        IF      7F T c, H tcell 1 ?DO FF T c, H LOOP
                   2357:        ELSE    tcell 1 ?DO FF T c, H LOOP 7F T c, H
                   2358:        THEN
                   2359:  ELSE  tcell 2 = IF 7fff ELSE 7fffffff THEN lit, THEN
1.43      pazsan   2360:  ;Cond
                   2361: 
1.1       anton    2362: >CROSS
1.115     jwilke   2363: 
1.1       anton    2364: \ Target compiling loop                                12dec92py
                   2365: \ ">tib trick thrown out                               10may93jaw
                   2366: \ number? defined at the top                           11may93jaw
1.77      jwilke   2367: \ replaced >in by save-input                           
                   2368: 
                   2369: : discard 0 ?DO drop LOOP ;
1.1       anton    2370: 
                   2371: \ compiled word might leave items on stack!
1.77      jwilke   2372: : tcom ( x1 .. xn n name -- )
                   2373: \  dup count type space
1.103     jwilke   2374:   gfind 
                   2375:   IF    >r ( discard saved input state ) discard r>
                   2376:        dup >exec-compile @ ?dup
                   2377:         IF   nip execute-exec-compile ELSE gexecute  THEN 
1.77      jwilke   2378:        EXIT 
                   2379:   THEN
                   2380:   number? dup  
                   2381:   IF   0> IF swap lit,  THEN  lit, discard
1.103     jwilke   2382:   ELSE 2drop restore-input throw Ghost gexecute THEN  ;
1.1       anton    2383: 
                   2384: \ : ; DOES>                                            13dec92py
                   2385: \ ]                                                     9may93py/jaw
                   2386: 
1.115     jwilke   2387: >CROSS
                   2388: 
1.105     jwilke   2389: : compiling-state ( -- )
                   2390: \G set states to compililng
1.54      pazsan   2391:     Compiling comp-state !
1.103     jwilke   2392:     \ if we have a state in target, change it with the compile state
1.105     jwilke   2393:     [G'] state dup undefined? 0= 
                   2394:     IF >ghost-xt @ execute X on ELSE drop THEN ;
                   2395: 
                   2396: : interpreting-state ( -- )
                   2397: \G set states to interpreting
                   2398:    \ if target has a state variable, change it according to our state
                   2399:    [G'] state dup undefined? 0= 
                   2400:    IF >ghost-xt @ execute X off ELSE drop THEN
                   2401:    Interpreting comp-state ! ;
1.115     jwilke   2402: 
                   2403: >TARGET
1.105     jwilke   2404: 
                   2405: : ] 
                   2406:     compiling-state
1.1       anton    2407:     BEGIN
1.77      jwilke   2408:         BEGIN save-input bl word
                   2409:               dup c@ 0= WHILE drop discard refill 0=
1.1       anton    2410:               ABORT" CROSS: End of file while target compiling"
                   2411:         REPEAT
                   2412:         tcom
1.103     jwilke   2413:         compiling? 0=
1.1       anton    2414:     UNTIL ;
                   2415: 
                   2416: \ by the way: defining a second interpreter (a compiler-)loop
                   2417: \             is not allowed if a system should be ans conform
                   2418: 
1.110     jwilke   2419: : (:) ( ghost -- ) 
                   2420: \ common factor of : and :noname. Prepare ;Resolve and start definition
                   2421:    ;Resolve ! there ;Resolve cell+ !
                   2422:    docol, ]comp  colon-start depth T ] H ;
                   2423: 
1.1       anton    2424: : : ( -- colon-sys ) \ Name
1.52      jwilke   2425:   defempty?
                   2426:   constflag off \ don't let this flag work over colon defs
                   2427:                \ just to go sure nothing unwanted happens
1.43      pazsan   2428:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
1.110     jwilke   2429:   (THeader (:) ;
1.1       anton    2430: 
1.37      pazsan   2431: : :noname ( -- colon-sys )
1.110     jwilke   2432:   X cfalign there 
                   2433:   \ define a nameless ghost
                   2434:   here ghostheader dup last-header-ghost ! dup to lastghost
                   2435:   (:) ;  
1.37      pazsan   2436: 
1.103     jwilke   2437: Cond: EXIT ( -- )   compile ;S  ;Cond
1.6       anton    2438: 
                   2439: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1.2       pazsan   2440: 
1.52      jwilke   2441: >CROSS
                   2442: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
                   2443:          ;Resolve cell+ @ ;
                   2444: 
                   2445: >TARGET
                   2446: 
1.103     jwilke   2447: Cond: recurse ( -- ) Last-Header-Ghost @ gexecute ;Cond
1.52      jwilke   2448: 
1.103     jwilke   2449: Cond: ; ( -- ) 
1.105     jwilke   2450:        depth ?dup 
                   2451:        IF   1- <> ABORT" CROSS: Stack changed"
                   2452:        ELSE true ABORT" CROSS: Stack empty" 
                   2453:        THEN
                   2454:        colon-end
                   2455:        fini,
                   2456:        comp[
                   2457:        ;Resolve @ 
                   2458:        IF      ;Resolve @ ;Resolve cell+ @ resolve 
                   2459:                ['] colon-resolved ;Resolve @ >comp !
                   2460:        THEN
                   2461:        interpreting-state
                   2462:        ;Cond
                   2463: 
                   2464: Cond: [ ( -- ) interpreting-state ;Cond
1.1       anton    2465: 
                   2466: >CROSS
1.54      pazsan   2467: 
                   2468: Create GhostDummy ghostheader
                   2469: <res> GhostDummy >magic !
                   2470: 
1.52      jwilke   2471: : !does ( does-action -- )
                   2472: \ !! zusammenziehen und dodoes, machen!
1.54      pazsan   2473:     tlastcfa @ [G'] :dovar killref
                   2474: \    tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
1.52      jwilke   2475: \ !! geht so nicht, da dodoes, ghost will!
1.54      pazsan   2476:     GhostDummy >link ! GhostDummy 
                   2477:     tlastcfa @ >tempdp dodoes, tempdp> ;
1.1       anton    2478: 
1.103     jwilke   2479: 
                   2480: Defer instant-interpret-does>-hook
                   2481: 
                   2482: : resolve-does>-part ( -- )
                   2483: \ resolve words made by builders
1.105     jwilke   2484:   Last-Header-Ghost @ >do:ghost @ ?dup 
                   2485:   IF    there resolve 
                   2486:         \ TODO: set special DOES> resolver action here
                   2487:   THEN ;
1.103     jwilke   2488: 
1.1       anton    2489: >TARGET
1.103     jwilke   2490: Cond: DOES>
                   2491:         compile (does>) doeshandler,
                   2492:         resolve-does>-part
1.1       anton    2493:         ;Cond
1.103     jwilke   2494: 
                   2495: : DOES> switchrom doeshandler, T here H !does 
                   2496:   instant-interpret-does>-hook
                   2497:   depth T ] H ;
1.1       anton    2498: 
                   2499: >CROSS
1.108     jwilke   2500: \ Creation                                              01nov92py
1.1       anton    2501: 
                   2502: \ Builder                                               11may93jaw
                   2503: 
1.108     jwilke   2504: 0 Value built
                   2505: 
1.105     jwilke   2506: : Builder    ( Create-xt do-ghost "name" -- )
1.52      jwilke   2507: \ builds up a builder in current vocabulary
                   2508: \ create-xt is executed when word is interpreted
1.106     jwilke   2509: \ do:-xt is executed when the created word from builder is executed
1.105     jwilke   2510: \ for do:-xt an additional entry after the normal ghost-entrys is used
1.52      jwilke   2511: 
1.109     jwilke   2512:   ghost to built 
1.108     jwilke   2513:   built >created @ 0= IF
                   2514:     built >created on
                   2515:     ['] prim-resolved built >comp ! 
                   2516:   THEN ;
1.1       anton    2517: 
1.52      jwilke   2518: : gdoes,  ( ghost -- )
                   2519: \ makes the codefield for a word that is built
1.103     jwilke   2520:   >do:ghost @ dup undefined? 0=
1.52      jwilke   2521:   IF
1.42      pazsan   2522:        dup >magic @ <do:> =
1.54      pazsan   2523:        IF       doer, 
                   2524:        ELSE    dodoes,
                   2525:        THEN
                   2526:        EXIT
1.52      jwilke   2527:   THEN
                   2528: \  compile :dodoes gexecute
                   2529: \  T here H tcell - reloff 
1.54      pazsan   2530:   2 refered 
                   2531:   0 fillcfa
                   2532:   ;
1.1       anton    2533: 
1.103     jwilke   2534: : takeover-x-semantics ( S constructor-ghost new-ghost -- )
1.105     jwilke   2535: \g stores execution semantic and compilation semantic in the built word
                   2536:    swap >do:ghost @ 
1.110     jwilke   2537:    \ we use the >exec2 field for the semantic of a created word,
                   2538:    \ using exec or exec2 makes no difference for normal cross-compilation
                   2539:    \ but is usefull for instant where the exec field is already
                   2540:    \ defined (e.g. Vocabularies)
1.105     jwilke   2541:    2dup >exec @ swap >exec2 ! 
                   2542:    >comp @ swap >comp ! ;
1.103     jwilke   2543: 
1.52      jwilke   2544: : TCreate ( <name> -- )
                   2545:   create-forward-warn
                   2546:   IF ['] reswarn-forward IS resolve-warning THEN
1.103     jwilke   2547:   executed-ghost @ (Theader
                   2548:   dup >created on
                   2549:   2dup takeover-x-semantics hereresolve gdoes, ;
1.52      jwilke   2550: 
                   2551: : RTCreate ( <name> -- )
                   2552: \ creates a new word with code-field in ram
                   2553:   create-forward-warn
                   2554:   IF ['] reswarn-forward IS resolve-warning THEN
                   2555:   \ make Alias
1.103     jwilke   2556:   executed-ghost @ (THeader 
                   2557:   dup >created on
                   2558:   2dup takeover-x-semantics
1.105     jwilke   2559:   there 0 T a, H alias-mask flag!
1.103     jwilke   2560:   \ store poiter to code-field
1.52      jwilke   2561:   switchram T cfalign H
                   2562:   there swap T ! H
                   2563:   there tlastcfa ! 
1.103     jwilke   2564:   hereresolve gdoes, ;
1.1       anton    2565: 
                   2566: : Build:  ( -- [xt] [colon-sys] )
1.52      jwilke   2567:   :noname postpone TCreate ;
                   2568: 
                   2569: : BuildSmart:  ( -- [xt] [colon-sys] )
                   2570:   :noname
1.53      jwilke   2571:   [ T has? rom H [IF] ]
1.52      jwilke   2572:   postpone RTCreate
                   2573:   [ [ELSE] ]
                   2574:   postpone TCreate 
                   2575:   [ [THEN] ] ;
1.1       anton    2576: 
1.108     jwilke   2577: : ;Build
                   2578:   postpone ; built >exec ! ; immediate
                   2579: 
1.1       anton    2580: : gdoes>  ( ghost -- addr flag )
1.110     jwilke   2581:   executed-ghost @ g>body ;
1.1       anton    2582: 
                   2583: \ DO: ;DO                                               11may93jaw
                   2584: 
1.108     jwilke   2585: : do:ghost! ( ghost -- ) built >do:ghost ! ;
                   2586: : doexec! ( xt -- ) built >do:ghost @ >exec ! ;
                   2587: 
                   2588: : DO:     ( -- [xt] [colon-sys] )
                   2589:   here ghostheader do:ghost!
1.110     jwilke   2590:   :noname postpone gdoes> ;
1.1       anton    2591: 
1.108     jwilke   2592: : by:     ( -- [xt] [colon-sys] ) \ name
                   2593:   Ghost do:ghost!
1.110     jwilke   2594:   :noname postpone gdoes> ;
1.42      pazsan   2595: 
1.108     jwilke   2596: : ;DO ( [xt] [colon-sys] -- )
                   2597:   postpone ; doexec! ; immediate
1.1       anton    2598: 
1.109     jwilke   2599: : by      ( -- ) \ Name
1.108     jwilke   2600:   Ghost >do:ghost @ do:ghost! ;
1.107     jwilke   2601: 
1.109     jwilke   2602: : compile: ( --[xt] [colon-sys] )
1.107     jwilke   2603: \G defines a compile time action for created words
                   2604: \G by this builder
                   2605:   :noname ;
                   2606: 
1.109     jwilke   2607: : ;compile ( [xt] [colon-sys] -- )
                   2608:   postpone ; built >do:ghost @ >comp ! ; immediate
1.107     jwilke   2609: 
1.1       anton    2610: \ Variables and Constants                              05dec92py
                   2611: 
1.108     jwilke   2612: Builder (Constant)
                   2613: Build:  ( n -- ) ;Build
1.103     jwilke   2614: by: :docon ( target-body-addr -- n ) T @ H ;DO
1.52      jwilke   2615: 
1.108     jwilke   2616: Builder Constant
                   2617: Build:  ( n -- ) T , H ;Build
1.52      jwilke   2618: by (Constant)
                   2619: 
1.108     jwilke   2620: Builder AConstant
                   2621: Build:  ( n -- ) T A, H ;Build
1.52      jwilke   2622: by (Constant)
                   2623: 
1.108     jwilke   2624: Builder 2Constant
                   2625: Build:  ( d -- ) T , , H ;Build
1.52      jwilke   2626: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
                   2627: 
1.108     jwilke   2628: Builder Create
                   2629: BuildSmart: ;Build
1.103     jwilke   2630: by: :dovar ( target-body-addr -- addr ) ;DO
1.1       anton    2631: 
1.108     jwilke   2632: Builder Variable
1.53      jwilke   2633: T has? rom H [IF]
1.108     jwilke   2634: Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 , H ( switchrom ) ;Build
1.52      jwilke   2635: by (Constant)
                   2636: [ELSE]
1.108     jwilke   2637: Build: T 0 , H ;Build
1.1       anton    2638: by Create
1.52      jwilke   2639: [THEN]
1.1       anton    2640: 
1.108     jwilke   2641: Builder 2Variable
1.53      jwilke   2642: T has? rom H [IF]
1.108     jwilke   2643: Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;Build
1.54      pazsan   2644: by (Constant)
                   2645: [ELSE]
1.108     jwilke   2646: Build: T 0 , 0 , H ;Build
1.54      pazsan   2647: by Create
                   2648: [THEN]
                   2649: 
1.108     jwilke   2650: Builder AVariable
1.54      pazsan   2651: T has? rom H [IF]
1.108     jwilke   2652: Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 A, H ( switchrom ) ;Build
1.52      jwilke   2653: by (Constant)
                   2654: [ELSE]
1.108     jwilke   2655: Build: T 0 A, H ;Build
1.1       anton    2656: by Create
1.52      jwilke   2657: [THEN]
1.1       anton    2658: 
1.3       pazsan   2659: \ User variables                                       04may94py
                   2660: 
                   2661: Variable tup  0 tup !
                   2662: Variable tudp 0 tudp !
1.78      jwilke   2663: 
1.3       pazsan   2664: : u,  ( n -- udp )
                   2665:   tup @ tudp @ + T  ! H
1.19      pazsan   2666:   tudp @ dup T cell+ H tudp ! ;
1.78      jwilke   2667: 
1.3       pazsan   2668: : au, ( n -- udp )
                   2669:   tup @ tudp @ + T A! H
1.19      pazsan   2670:   tudp @ dup T cell+ H tudp ! ;
1.78      jwilke   2671: 
1.108     jwilke   2672: Builder User
                   2673: Build: 0 u, X , ;Build
1.78      jwilke   2674: by: :douser ( ghost -- up-addr )  X @ tup @ + ;DO
1.1       anton    2675: 
1.108     jwilke   2676: Builder 2User
                   2677: Build: 0 u, X , 0 u, drop ;Build
1.3       pazsan   2678: by User
1.1       anton    2679: 
1.108     jwilke   2680: Builder AUser
                   2681: Build: 0 au, X , ;Build
1.3       pazsan   2682: by User
1.1       anton    2683: 
1.108     jwilke   2684: Builder (Value)
                   2685: Build:  ( n -- ) ;Build
                   2686: by: :docon ( target-body-addr -- n ) T @ H ;DO
                   2687: 
1.1       anton    2688: Builder Value
1.108     jwilke   2689: BuildSmart: T , H ;Build
                   2690: by (Value)
1.1       anton    2691: 
1.32      pazsan   2692: Builder AValue
1.108     jwilke   2693: BuildSmart: T A, H ;Build
                   2694: by (Value)
1.32      pazsan   2695: 
1.103     jwilke   2696: Defer texecute
                   2697: 
1.108     jwilke   2698: Builder Defer
                   2699: BuildSmart:  ( -- ) [T'] noop T A, H ;Build
1.103     jwilke   2700: by: :dodefer ( ghost -- ) X @ texecute ;DO
1.37      pazsan   2701: 
1.108     jwilke   2702: Builder interpret/compile:
                   2703: Build: ( inter comp -- ) swap T immediate A, A, H ;Build
1.37      pazsan   2704: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1.24      pazsan   2705: 
                   2706: \ Sturctures                                           23feb95py
                   2707: 
                   2708: : nalign ( addr1 n -- addr2 )
                   2709: \ addr2 is the aligned version of addr1 wrt the alignment size n
                   2710:  1- tuck +  swap invert and ;
1.109     jwilke   2711: 
1.24      pazsan   2712: 
1.108     jwilke   2713: Builder (Field)
                   2714: Build: ;Build
1.44      pazsan   2715: by: :dofield T @ H + ;DO
                   2716: 
1.108     jwilke   2717: Builder Field
1.51      anton    2718: Build: ( align1 offset1 align size "name" --  align2 offset2 )
                   2719:     rot dup T , H ( align1 align size offset1 )
1.108     jwilke   2720:     + >r nalign r> ;Build
1.44      pazsan   2721: by (Field)
1.24      pazsan   2722: 
1.109     jwilke   2723: >TARGET
1.51      anton    2724: : struct  T 1 chars 0 H ;
1.24      pazsan   2725: : end-struct  T 2Constant H ;
                   2726: 
1.52      jwilke   2727: : cell% ( n -- size align )
1.51      anton    2728:     T 1 cells H dup ;
1.109     jwilke   2729: >CROSS
1.88      pazsan   2730: 
1.105     jwilke   2731: \ Input-Methods                                            01py
1.103     jwilke   2732: 
1.108     jwilke   2733: Builder input-method
                   2734: Build: ( m v -- m' v )  dup T , cell+ H ;Build
1.88      pazsan   2735: DO:  abort" Not in cross mode" ;DO
                   2736: 
1.108     jwilke   2737: Builder input-var
                   2738: Build: ( m v size -- m v' )  over T , H + ;Build
1.88      pazsan   2739: DO:  abort" Not in cross mode" ;DO
1.24      pazsan   2740: 
1.108     jwilke   2741: \ Peephole optimization                                        05sep01jaw
                   2742: 
                   2743: \ this section defines different compilation
                   2744: \ actions for created words
                   2745: \ this will help the peephole optimizer
                   2746: \ I (jaw) took this from bernds lates cross-compiler
                   2747: \ changes but seperated it from the original
                   2748: \ Builder words. The final plan is to put this
                   2749: \ into a seperate file, together with the peephole
                   2750: \ optimizer for cross
                   2751: 
                   2752: 
                   2753: T has? peephole H [IF]
                   2754: 
1.109     jwilke   2755: >CROSS
                   2756: : (callc) compile call T >body a, H ;          ' (callc) plugin-of colon,
                   2757: 
                   2758: \ if we want this, we have to spilt aconstant
                   2759: \ and constant!!
                   2760: \ Builder (Constant)
                   2761: \ compile: g>body X @ lit, ;compile
1.108     jwilke   2762: 
                   2763: Builder (Constant)
1.109     jwilke   2764: compile: g>body alit, compile @ ;compile
1.108     jwilke   2765: 
                   2766: Builder (Value)
                   2767: compile: g>body alit, compile @ ;compile
                   2768: 
                   2769: \ this changes also Variable, AVariable and 2Variable
                   2770: Builder Create
                   2771: \ compile: g>body alit, ;compile
                   2772: 
                   2773: Builder User
                   2774: compile: g>body compile useraddr T @ , H ;compile
                   2775: 
                   2776: Builder Defer
                   2777: compile: g>body alit, compile @ compile execute ;compile
1.102     jwilke   2778: 
1.108     jwilke   2779: Builder (Field)
                   2780: compile: g>body T @ H lit, compile + ;compile
1.102     jwilke   2781: 
1.108     jwilke   2782: [THEN]
1.103     jwilke   2783: 
1.52      jwilke   2784: \ structural conditionals                              17dec92py
                   2785: 
                   2786: >CROSS
1.103     jwilke   2787: : (ncontrols?) ( n -- ) 
                   2788: \g We expect n open control structures
                   2789:   depth over u<= 
                   2790:   ABORT" CROSS: unstructured, stack underflow"
                   2791:   0 ?DO I pick 0= 
                   2792:         ABORT" CROSS: unstructured" 
                   2793:   LOOP ;                                       ' (ncontrols?) plugin-of ncontrols?
                   2794: 
                   2795: \ : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
                   2796: \ : sys?        ( sys -- sys )    dup 0= ?struc ;
                   2797: 
1.52      jwilke   2798: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
                   2799: 
1.78      jwilke   2800: : branchoffset ( src dest -- )  - tchar / ; \ ?? jaw
1.52      jwilke   2801: 
1.78      jwilke   2802: : >resolve    ( sys -- )        
                   2803:        X here ( dup ." >" hex. ) over branchoffset swap X ! ;
1.52      jwilke   2804: 
1.78      jwilke   2805: : <resolve    ( sys -- )
                   2806:        X here ( dup ." <" hex. ) branchoffset X , ;
1.52      jwilke   2807: 
1.78      jwilke   2808: :noname compile branch X here branchoffset X , ;
1.54      pazsan   2809:   IS branch, ( target-addr -- )
1.78      jwilke   2810: :noname compile ?branch X here branchoffset X , ;
1.54      pazsan   2811:   IS ?branch, ( target-addr -- )
                   2812: :noname compile branch T here 0 , H ;
                   2813:   IS branchmark, ( -- branchtoken )
                   2814: :noname compile ?branch T here 0 , H ;
                   2815:   IS ?branchmark, ( -- branchtoken )
                   2816: :noname T here 0 , H ;
                   2817:   IS ?domark, ( -- branchtoken )
1.78      jwilke   2818: :noname dup X @ ?struc X here over branchoffset swap X ! ;
1.54      pazsan   2819:   IS branchtoresolve, ( branchtoken -- )
1.78      jwilke   2820: :noname branchto, X here ;
1.54      pazsan   2821:   IS branchtomark, ( -- target-addr )
1.52      jwilke   2822: 
                   2823: >TARGET
                   2824: 
                   2825: \ Structural Conditionals                              12dec92py
                   2826: 
1.103     jwilke   2827: \ CLEANUP Cond: BUT       sys? swap ;Cond
                   2828: \ CLEANUP Cond: YET       sys? dup ;Cond
1.52      jwilke   2829: 
                   2830: >CROSS
                   2831: 
1.78      jwilke   2832: Variable tleavings 0 tleavings !
1.52      jwilke   2833: 
1.54      pazsan   2834: : (done) ( addr -- )
                   2835:     tleavings @
                   2836:     BEGIN  dup
                   2837:     WHILE
                   2838:        >r dup r@ cell+ @ \ address of branch
                   2839:        u> 0=      \ lower than DO?     
                   2840:     WHILE
                   2841:        r@ 2 cells + @ \ branch token
                   2842:        branchtoresolve,
                   2843:        r@ @ r> free throw
                   2844:     REPEAT  r>  THEN
                   2845:     tleavings ! drop ;
                   2846: 
1.53      jwilke   2847: >TARGET
                   2848: 
1.103     jwilke   2849: \ What for? ANS? JAW Cond: DONE   ( addr -- )  (done) ;Cond
1.53      jwilke   2850: 
                   2851: >CROSS
1.54      pazsan   2852: : (leave) ( branchtoken -- )
1.53      jwilke   2853:     3 cells allocate throw >r
                   2854:     T here H r@ cell+ !
                   2855:     r@ 2 cells + !
                   2856:     tleavings @ r@ !
                   2857:     r> tleavings ! ;
                   2858: >TARGET
                   2859: 
1.103     jwilke   2860: : (leave,) ( -- ) 
                   2861:   branchmark, (leave) ;                        ' (leave,) plugin-of leave,
                   2862: 
                   2863: : (?leave,) ( -- )
                   2864:   compile 0= ?branchmark, (leave) ;            ' (?leave,) plugin-of ?leave,
                   2865: 
                   2866: Cond: LEAVE     leave, ;Cond
                   2867: Cond: ?LEAVE    ?leave, ;Cond
1.53      jwilke   2868: 
1.54      pazsan   2869: >CROSS
                   2870: \ !!JW ToDo : Move to general tools section
                   2871: 
                   2872: : to1 ( x1 x2 xn n -- addr )
1.103     jwilke   2873: \G packs n stack elements in am allocated memory region
1.54      pazsan   2874:    dup dup 1+ cells allocate throw dup >r swap 1+
                   2875:    0 DO tuck ! cell+ LOOP
                   2876:    drop r> ;
1.103     jwilke   2877: 
1.54      pazsan   2878: : 1to ( addr -- x1 x2 xn )
                   2879: \G unpacks the elements saved by to1
                   2880:     dup @ swap over cells + swap
                   2881:     0 DO  dup @ swap 1 cells -  LOOP
                   2882:     free throw ;
                   2883: 
                   2884: : loop]     branchto, dup <resolve tcell - (done) ;
                   2885: 
                   2886: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
                   2887: 
                   2888: >TARGET
                   2889: 
1.52      jwilke   2890: \ Structural Conditionals                              12dec92py
                   2891: 
1.103     jwilke   2892: : (cs-swap) ( x1 x2 -- x2 x1 )
                   2893:   swap ;                                       ' (cs-swap) plugin-of cs-swap
                   2894: 
                   2895: : (ahead,) branchmark, ;                       ' (ahead,) plugin-of ahead,
                   2896: 
                   2897: : (if,) ?branchmark, ;                                 ' (if,) plugin-of if,
                   2898: 
                   2899: : (then,) branchto, branchtoresolve, ;                 ' (then,) plugin-of then,
                   2900: 
                   2901: : (else,) ( ahead ) branchmark, 
                   2902:           swap 
                   2903:           ( then ) branchto, branchtoresolve, ;        ' (else,) plugin-of else,
                   2904: 
                   2905: : (begin,) branchtomark, ;                     ' (begin,) plugin-of begin,
                   2906: 
                   2907: : (while,) ( if ) ?branchmark,
                   2908:            swap ;                              ' (while,) plugin-of while,
                   2909: 
                   2910: : (again,) branch, ;                           ' (again,) plugin-of again,
                   2911: 
                   2912: : (until,) ?branch, ;                          ' (until,) plugin-of until,
                   2913: 
                   2914: : (repeat,) ( again ) branch,
                   2915:             ( then ) branchto, branchtoresolve, ; ' (repeat,) plugin-of repeat,
                   2916: 
                   2917: : (case,)   ( -- n )
                   2918:   0 ;                                          ' (case,) plugin-of case,
                   2919: 
                   2920: : (of,) ( n -- x1 n )
                   2921:   1+ >r 
                   2922:   compile over compile = 
                   2923:   if, compile drop r> ;                                ' (of,) plugin-of of,
                   2924: 
                   2925: : (endof,) ( x1 n -- x2 n )
                   2926:   >r 1 ncontrols? else, r> ;                   ' (endof,) plugin-of endof,
                   2927: 
                   2928: : (endcase,) ( x1 .. xn n -- )
                   2929:   compile drop 0 ?DO 1 ncontrols? then, LOOP ; ' (endcase,) plugin-of endcase,
                   2930: 
1.53      jwilke   2931: >TARGET
1.103     jwilke   2932: Cond: AHEAD     ahead, ;Cond
                   2933: Cond: IF        if,  ;Cond
                   2934: Cond: THEN      1 ncontrols? then, ;Cond
                   2935: Cond: ENDIF     1 ncontrols? then, ;Cond
                   2936: Cond: ELSE      1 ncontrols? else, ;Cond
                   2937: 
                   2938: Cond: BEGIN     begin, ;Cond
                   2939: Cond: WHILE     1 ncontrols? while, ;Cond
                   2940: Cond: AGAIN     1 ncontrols? again, ;Cond
                   2941: Cond: UNTIL     1 ncontrols? until, ;Cond
                   2942: Cond: REPEAT    2 ncontrols? repeat, ;Cond
                   2943: 
                   2944: Cond: CASE      case, ;Cond
                   2945: Cond: OF        of, ;Cond
                   2946: Cond: ENDOF     endof, ;Cond
                   2947: Cond: ENDCASE   endcase, ;Cond
1.1       anton    2948: 
                   2949: \ Structural Conditionals                              12dec92py
                   2950: 
1.103     jwilke   2951: : (do,) ( -- target-addr )
                   2952:   \ ?? i think 0 is too much! jaw
1.54      pazsan   2953:     0 compile (do)
1.103     jwilke   2954:     branchtomark,  2 to1 ;                     ' (do,) plugin-of do,
1.54      pazsan   2955: 
1.103     jwilke   2956: \ alternative for if no ?do
                   2957: \ : (do,)
1.54      pazsan   2958: \     compile 2dup compile = compile IF
                   2959: \     compile 2drop compile ELSE
                   2960: \     compile (do) branchtomark, 2 to1 ;
                   2961:     
1.103     jwilke   2962: : (?do,) ( -- target-addr )
1.54      pazsan   2963:     0 compile (?do)  ?domark, (leave)
1.103     jwilke   2964:     branchtomark,  2 to1 ;                     ' (?do,) plugin-of ?do,
                   2965: 
                   2966: : (for,) ( -- target-addr )
                   2967:   compile (for) branchtomark, ;                        ' (for,) plugin-of for,
                   2968: 
                   2969: : (loop,) ( target-addr -- )
                   2970:   1to compile (loop)  loop] 
                   2971:   compile unloop skiploop] ;                   ' (loop,) plugin-of loop,
                   2972: 
                   2973: : (+loop,) ( target-addr -- )
                   2974:   1to compile (+loop)  loop] 
                   2975:   compile unloop skiploop] ;                   ' (+loop,) plugin-of +loop,
                   2976: 
                   2977: : (next,) 
                   2978:   compile (next)  loop] compile unloop ;       ' (next,) plugin-of next,
                   2979: 
                   2980: Cond: DO       do, ;Cond
                   2981: Cond: ?DO      ?do, ;Cond
                   2982: Cond: FOR      for, ;Cond
                   2983: 
                   2984: Cond: LOOP     1 ncontrols? loop, ;Cond
                   2985: Cond: +LOOP    1 ncontrols? +loop, ;Cond
                   2986: Cond: NEXT     1 ncontrols? next, ;Cond
1.52      jwilke   2987: 
1.1       anton    2988: \ String words                                         23feb93py
                   2989: 
1.103     jwilke   2990: : ,"            [char] " parse ht-string, X align ;
1.1       anton    2991: 
1.103     jwilke   2992: Cond: ."        compile (.")     T ," H ;Cond
                   2993: Cond: S"        compile (S")     T ," H ;Cond
                   2994: Cond: C"        compile (C")     T ," H ;Cond
                   2995: Cond: ABORT"    compile (ABORT") T ," H ;Cond
1.1       anton    2996: 
                   2997: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
1.66      pazsan   2998: : IS            T >address ' >body ! H ;
1.9       pazsan   2999: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
                   3000: : TO            T ' >body ! H ;
1.1       anton    3001: 
1.52      jwilke   3002: Cond: defers   T ' >body @ compile, H ;Cond
                   3003: 
1.1       anton    3004: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
                   3005: 
                   3006: \ linked list primitive
1.79      jwilke   3007: : linked        X here over X @ X A, swap X ! ;
1.52      jwilke   3008: : chained      T linked A, H ;
1.1       anton    3009: 
                   3010: : err"   s" ErrLink linked" evaluate T , H
1.103     jwilke   3011:          [char] " parse ht-string, X align ;
1.1       anton    3012: 
                   3013: : env"  [char] " parse s" EnvLink linked" evaluate
1.103     jwilke   3014:         ht-string, X align X , ;
1.1       anton    3015: 
                   3016: : 2env" [char] " parse s" EnvLink linked" evaluate
1.103     jwilke   3017:         here >r ht-string, X align X , X ,
1.1       anton    3018:         r> dup T c@ H 80 and swap T c! H ;
                   3019: 
                   3020: \ compile must be last                                 22feb93py
                   3021: 
1.103     jwilke   3022: Cond: [compile] ( -- ) \ name
                   3023: \g For immediate words, works even if forward reference
                   3024:       bl word gfind 0= ABORT" CROSS: Can't compile"
                   3025:       (gexecute) ;Cond
                   3026:           
                   3027: Cond: postpone ( -- ) \ name
                   3028:       bl word gfind 0= ABORT" CROSS: Can't compile"
                   3029:       dup >magic @ <fwd> =
                   3030:       ABORT" CROSS: Can't postpone on forward declaration"
                   3031:       dup >magic @ <imm> =
                   3032:       IF   (gexecute)
                   3033:       ELSE compile (compile) addr, THEN ;Cond
1.54      pazsan   3034:           
1.77      jwilke   3035: \ save-cross                                           17mar93py
                   3036: 
                   3037: hex
                   3038: 
                   3039: >CROSS
                   3040: Create magic  s" Gforth2x" here over allot swap move
                   3041: 
                   3042: bigendian 1+ \ strangely, in magic big=0, little=1
                   3043: tcell 1 = 0 and or
                   3044: tcell 2 = 2 and or
                   3045: tcell 4 = 4 and or
                   3046: tcell 8 = 6 and or
                   3047: tchar 1 = 00 and or
                   3048: tchar 2 = 28 and or
                   3049: tchar 4 = 50 and or
                   3050: tchar 8 = 78 and or
                   3051: magic 7 + c!
                   3052: 
                   3053: : save-cross ( "image-name" "binary-name" -- )
                   3054:   bl parse ." Saving to " 2dup type cr
                   3055:   w/o bin create-file throw >r
1.113     jwilke   3056:   s" header" X $has? IF
1.80      anton    3057:       s" #! "           r@ write-file throw
                   3058:       bl parse          r@ write-file throw
                   3059:       s"  --image-file" r@ write-file throw
1.77      jwilke   3060:       #lf       r@ emit-file throw
                   3061:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
                   3062:       ?do
                   3063:          bl over emit-file throw
                   3064:       loop
                   3065:       drop
                   3066:       magic 8       r@ write-file throw \ write magic
                   3067:   ELSE
                   3068:       bl parse 2drop
                   3069:   THEN
                   3070:   image @ there 
                   3071:   r@ write-file throw \ write image
1.113     jwilke   3072:   s" relocate" X $has? IF
1.77      jwilke   3073:       bit$  @ there 1- tcell>bit rshift 1+
                   3074:                 r@ write-file throw \ write tags
                   3075:   THEN
                   3076:   r> close-file throw ;
                   3077: 
                   3078: : save-region ( addr len -- )
                   3079:   bl parse w/o bin create-file throw >r
                   3080:   swap >image swap r@ write-file throw
                   3081:   r> close-file throw ;
                   3082: 
1.110     jwilke   3083: \ save-asm-region                                      29aug01jaw
1.103     jwilke   3084: 
                   3085: Variable name-ptr
                   3086: Create name-buf 200 chars allot
                   3087: : init-name-buf name-buf name-ptr ! ;
                   3088: : nb, name-ptr @ c! 1 chars name-ptr +! ;
                   3089: : $nb, ( adr len -- ) bounds ?DO I c@ nb, LOOP ;
                   3090: : @nb name-ptr @ name-buf tuck - ;
                   3091: 
                   3092: \ stores a usefull string representation of the character
                   3093: \ in the name buffer
                   3094: : name-char, ( c -- )
                   3095:   dup 'a 'z 1+ within IF nb, EXIT THEN
                   3096:   dup 'A 'Z 1+ within IF $20 + nb, EXIT THEN
                   3097:   dup '0 '9 1+ within IF nb, EXIT THEN
                   3098:   CASE '+ OF s" _PLUS" $nb, ENDOF
                   3099:        '- OF s" _MINUS" $nb, ENDOF
                   3100:        '* OF s" _STAR" $nb, ENDOF
                   3101:        '/ OF s" _SLASH" $nb, ENDOF
                   3102:        '' OF s" _TICK" $nb, ENDOF
                   3103:        '( OF s" _OPAREN" $nb, ENDOF
                   3104:        ') OF s" _CPAREN" $nb, ENDOF
                   3105:        '[ OF s" _OBRACKET" $nb, ENDOF
                   3106:        '] OF s" _CBRACKET" $nb, ENDOF
                   3107:        '! OF s" _STORE" $nb, ENDOF
                   3108:        '@ OF s" _FETCH" $nb, ENDOF
                   3109:        '> OF s" _GREATER" $nb, ENDOF
                   3110:        '< OF s" _LESS" $nb, ENDOF
                   3111:        '= OF s" _EQUAL" $nb, ENDOF
                   3112:        '# OF s" _HASH" $nb, ENDOF
                   3113:        '? OF s" _QUEST" $nb, ENDOF
                   3114:        ': OF s" _COL" $nb, ENDOF
                   3115:        '; OF s" _SEMICOL" $nb, ENDOF
                   3116:        ', OF s" _COMMA" $nb, ENDOF
                   3117:        '. OF s" _DOT" $nb, ENDOF
                   3118:        '" OF s" _DQUOT" $nb, ENDOF
                   3119:        dup 
                   3120:        base @ >r hex s>d <# #s 'X hold '_ hold #> $nb, r> base !
                   3121:   ENDCASE ;
                   3122:  
                   3123: : label-from-ghostname ( ghost -- addr len )
                   3124:   dup >ghostname init-name-buf 'L nb, bounds 
                   3125:   ?DO I c@ name-char, LOOP 
                   3126:   \ we add the address to a name to make in unique
                   3127:   \ because one name may appear more then once
                   3128:   \ there are names (e.g. boot) that may be reference from other
                   3129:   \ assembler source files, so we declare them as unique
                   3130:   \ and don't add the address suffix
                   3131:   dup >ghost-flags @ <unique> and 0= 
                   3132:   IF   s" __" $nb, >link @ base @ >r hex 0 <# #s 'L hold #> r> base ! $nb, 
                   3133:   ELSE drop 
                   3134:   THEN
                   3135:   @nb ;
                   3136: 
1.111     jwilke   3137: \ FIXME why disabled?!
1.103     jwilke   3138: : label-from-ghostnameXX ( ghost -- addr len )
                   3139: \ same as (label-from-ghostname) but caches generated names
                   3140:   dup >asm-name @ ?dup IF nip count EXIT THEN
                   3141:  \ dup >r (label-from-ghostname) 2dup
                   3142:   align here >r string, align
                   3143:   r> r> >asm-name ! ;
                   3144: 
                   3145: : primghostdiscover ( xt -- ghost true | xt false )
                   3146:   dup 0= IF false EXIT THEN
                   3147:   >r last-prim-ghost
                   3148:   BEGIN @ dup
                   3149:   WHILE dup >asm-dummyaddr @ r@ =
                   3150:         IF rdrop true EXIT THEN
                   3151:   REPEAT
                   3152:   drop r> false ;
                   3153: 
                   3154: : gdiscover2 ( xt -- ghost true | xt false ) 
                   3155:   dup taddr>region 0= IF false EXIT THEN
                   3156:   dup (>regiontype) @ dup 0= IF drop false EXIT THEN
                   3157:   addr-xt-ghost @ dup 0= IF drop false EXIT THEN
                   3158:   nip true ;
                   3159: \  dup >ghost-name @ IF nip true ELSE drop false THEN ;
                   3160: 
                   3161: \ generates a label name for the target address
                   3162: : generate-label-name ( taddr -- addr len )
                   3163:   gdiscover2
                   3164:   IF dup >magic @ <do:> =
                   3165:      IF >asm-name @ count EXIT THEN
                   3166:      label-from-ghostname
                   3167:   ELSE
                   3168:      primghostdiscover
                   3169:      IF   >asm-name @ count 
                   3170:      ELSE base @ >r hex 0 <# #s 'L hold #> r> base !
                   3171:      THEN
                   3172:   THEN ;
                   3173: 
                   3174: Variable outfile-fd
                   3175: 
                   3176: : $out ( adr len -- ) outfile-fd @ write-file throw  ;
                   3177: : nlout newline $out ;
                   3178: : .ux ( n -- ) 
                   3179:   base @ hex swap 0 <# #S #> $out base ! ;
                   3180: 
                   3181: : save-asm-region-part-aligned ( taddr len -- 'taddr 'len )
                   3182:   dup cell/ 0 
                   3183:   ?DO nlout s"    .word " $out over @relbit 
                   3184:       IF   over X @ generate-label-name $out
                   3185:       ELSE over X @ s" 0x0" $out .ux
                   3186:       THEN
                   3187:       tcell /string
                   3188:   LOOP ;
                   3189: 
                   3190: : print-bytes ( taddr len n -- taddr' len' )
                   3191:   over min dup 0> 
                   3192:   IF   nlout s"    .byte " $out 0 
                   3193:        ?DO  I 0> IF s" , " $out THEN
                   3194:             over X c@ s" 0x0" $out .ux 1 /string 
                   3195:        LOOP 
                   3196:   THEN ;
                   3197: 
                   3198: : save-asm-region-part ( addr len -- )
                   3199:   over dup X aligned swap - ?dup
                   3200:   IF   print-bytes THEN
                   3201:   save-asm-region-part-aligned
                   3202:   dup dup X aligned swap - ?dup
                   3203:   IF   2 pick @relbit ABORT" relocated field splitted"
                   3204:        print-bytes
                   3205:   THEN
                   3206:   2drop ;
                   3207: 
                   3208: : print-label ( taddr -- )
                   3209:   nlout generate-label-name $out s" :" $out ;
                   3210: 
                   3211: : snl-calc ( taddr taddr2 -- )
                   3212:   tuck over - ;
                   3213: 
                   3214: : skip-nolables ( taddr -- taddr2 taddr len )
                   3215: \G skips memory region where no lables are defined
                   3216: \G starting from taddr+1
                   3217: \G Labels will be introduced for each reference mark
                   3218: \G in addr-refs.
                   3219: \G This word deals with lables at byte addresses as well.
                   3220: \G The main idea is to have an intro part which
                   3221: \G skips until the next cell boundary, the middle part
                   3222: \G which skips whole cells very efficiently and the third
                   3223: \G part which skips the bytes to the label in a cell
                   3224:   dup 1+ dup (>regiontype) 
                   3225:   ( S taddr taddr-realstart type-addr )
                   3226:   dup @ dup IF addr-refs @ THEN
                   3227:   swap >r
                   3228:   over align+ tuck tcell swap - rshift swap 0
                   3229:   DO dup 1 and 
                   3230:      IF drop rdrop snl-calc UNLOOP EXIT THEN 
                   3231:      2/ swap 1+ swap 
                   3232:   LOOP
                   3233:   drop r> cell+
                   3234:   ( S .. taddr2 type-addr ) dup
                   3235:   BEGIN dup @ dup IF addr-refs @ THEN 0= WHILE cell+ REPEAT
                   3236:   dup >r swap - 1 cells / tcell * + r>
                   3237:   ( S .. taddr2+skiplencells type-addr )
                   3238:   @ addr-refs @ 1 tcell lshift or
                   3239:   BEGIN dup 1 and 0= WHILE swap 1+ swap 2/ REPEAT drop
                   3240:   ( S .. taddr2+skiplencells+skiplenbytes )
                   3241:   snl-calc ;
                   3242: 
                   3243: : insert-label ( taddr -- )
                   3244:   dup 0= IF drop EXIT THEN
                   3245:   \ ignore everything which points outside our memory regions
                   3246:   \ maybe a primitive pointer or whatever
                   3247:   dup taddr>region 0= IF drop EXIT THEN
                   3248:   dup >r (>regiontype) define-addr-struct addr-refs dup @ 
                   3249:   r> tcell 1- and 1 swap lshift or swap ! ;
                   3250: 
                   3251: \ this generates a sorted list of addresses which must be labels
                   3252: \ it scans therefore a whole region
                   3253: : generate-label-list-region ( taddr len -- )
                   3254:   BEGIN over @relbit IF over X @ insert-label THEN
                   3255:         tcell /string dup 0< 
                   3256:   UNTIL 2drop ;
                   3257: 
                   3258: : generate-label-list ( -- )
                   3259:   region-link
                   3260:   BEGIN @ dup WHILE 
                   3261:         dup 0 >rlink - extent 
                   3262:         ?dup IF generate-label-list-region ELSE drop THEN
                   3263:   REPEAT drop ;
                   3264: 
                   3265: : create-outfile ( addr len -- )
                   3266:   w/o bin create-file throw outfile-fd ! ;
                   3267: 
                   3268: : close-outfile ( -- )
                   3269:   outfile-fd @ close-file throw ;
                   3270: 
                   3271: : (save-asm-region) ( region -- )
                   3272:   \ ." label list..."
                   3273:   generate-label-list
                   3274:   \ ." ok!" cr
                   3275:   extent ( S taddr len )
                   3276:   over insert-label
                   3277:   2dup + dup insert-label >r ( R end-label )
                   3278:   ( S taddr len ) drop
                   3279:   BEGIN
                   3280:      dup print-label
                   3281:      dup r@ <> WHILE
                   3282:      skip-nolables save-asm-region-part
                   3283:   REPEAT drop rdrop ;
                   3284: 
                   3285: : lineout ( addr len -- )
                   3286:   outfile-fd @ write-line throw ;  
                   3287: 
                   3288: : save-asm-region ( region adr len -- )
                   3289:   create-outfile (save-asm-region) close-outfile ;
                   3290: 
1.54      pazsan   3291: \ \ minimal definitions
                   3292:           
1.77      jwilke   3293: >MINIMAL also minimal
                   3294: 
1.1       anton    3295: \ Usefull words                                        13feb93py
                   3296: 
                   3297: : KB  400 * ;
                   3298: 
1.54      pazsan   3299: \ \ [IF] [ELSE] [THEN] ...                             14sep97jaw
                   3300: 
                   3301: \ it is useful to define our own structures and not to rely
1.109     jwilke   3302: \ on the words in the host system
                   3303: \ The words in the host system might be defined with vocabularies
1.54      pazsan   3304: \ this doesn't work with our self-made compile-loop
                   3305: 
                   3306: Create parsed 20 chars allot   \ store word we parsed
                   3307: 
                   3308: : upcase
                   3309:     parsed count bounds
                   3310:     ?DO I c@ toupper I c! LOOP ;
                   3311: 
                   3312: : [ELSE]
                   3313:     1 BEGIN
                   3314:        BEGIN bl word count dup WHILE
1.77      jwilke   3315:            comment? 20 umin parsed place upcase parsed count
1.54      pazsan   3316:            2dup s" [IF]" compare 0= >r 
                   3317:            2dup s" [IFUNDEF]" compare 0= >r
                   3318:            2dup s" [IFDEF]" compare 0= r> or r> or
                   3319:            IF   2drop 1+
                   3320:            ELSE 2dup s" [ELSE]" compare 0=
                   3321:                IF   2drop 1- dup
                   3322:                    IF 1+
                   3323:                    THEN
                   3324:                ELSE
                   3325:                    2dup s" [ENDIF]" compare 0= >r
                   3326:                    s" [THEN]" compare 0= r> or
                   3327:                    IF 1- THEN
                   3328:                THEN
                   3329:            THEN
                   3330:            ?dup 0= ?EXIT
                   3331:        REPEAT
                   3332:        2drop refill 0=
                   3333:     UNTIL drop ; immediate
                   3334:   
                   3335: : [THEN] ( -- ) ; immediate
                   3336: 
                   3337: : [ENDIF] ( -- ) ; immediate
                   3338: 
                   3339: : [IF] ( flag -- )
                   3340:     0= IF postpone [ELSE] THEN ; immediate 
                   3341: 
                   3342: Cond: [IF]      postpone [IF] ;Cond
                   3343: Cond: [THEN]    postpone [THEN] ;Cond
                   3344: Cond: [ELSE]    postpone [ELSE] ;Cond
                   3345: 
1.1       anton    3346: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
                   3347: 
1.77      jwilke   3348: : defined? tdefined? ;
1.44      pazsan   3349: : needed? needed? ;
                   3350: : doer? doer? ;
1.1       anton    3351: 
1.52      jwilke   3352: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
                   3353: 
                   3354: : directive? 
1.77      jwilke   3355:   bl word count [ ' target >wordlist ] literal search-wordlist 
1.52      jwilke   3356:   dup IF nip THEN ;
                   3357: 
                   3358: : [IFDEF]  >in @ directive? swap >in !
1.77      jwilke   3359:           0= IF tdefined? ELSE name 2drop true THEN
1.52      jwilke   3360:           postpone [IF] ;
                   3361: 
1.77      jwilke   3362: : [IFUNDEF] tdefined? 0= postpone [IF] ;
1.1       anton    3363: 
1.54      pazsan   3364: Cond: [IFDEF]   postpone [IFDEF] ;Cond
                   3365: 
                   3366: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
                   3367: 
1.1       anton    3368: \ C: \- \+ Conditional Compiling                         09jun93jaw
                   3369: 
1.77      jwilke   3370: : C: >in @ tdefined? 0=
                   3371:      IF    >in ! X :
1.1       anton    3372:      ELSE drop
                   3373:         BEGIN bl word dup c@
                   3374:               IF   count comment? s" ;" compare 0= ?EXIT
                   3375:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
                   3376:               THEN
                   3377:         AGAIN
                   3378:      THEN ;
                   3379: 
1.74      jwilke   3380: : d? d? ;
                   3381: 
                   3382: \G doesn't skip line when debug switch is on
                   3383: : \D D? 0= IF postpone \ THEN ;
1.52      jwilke   3384: 
1.48      anton    3385: \G interprets the line if word is not defined
1.77      jwilke   3386: : \- tdefined? IF postpone \ THEN ;
1.48      anton    3387: 
                   3388: \G interprets the line if word is defined
1.77      jwilke   3389: : \+ tdefined? 0= IF postpone \ THEN ;
1.1       anton    3390: 
1.48      anton    3391: Cond: \- \- ;Cond
                   3392: Cond: \+ \+ ;Cond
1.52      jwilke   3393: Cond: \D \D ;Cond
1.48      anton    3394: 
                   3395: : ?? bl word find IF execute ELSE drop 0 THEN ;
                   3396: 
                   3397: : needed:
                   3398: \G defines ghost for words that we want to be compiled
1.103     jwilke   3399:   BEGIN >in @ bl word c@ WHILE >in ! Ghost drop REPEAT drop ;
1.48      anton    3400: 
1.1       anton    3401: \ words that should be in minimal
1.52      jwilke   3402: 
                   3403: create s-buffer 50 chars allot
                   3404: 
1.77      jwilke   3405: bigendian Constant bigendian
1.1       anton    3406: 
1.52      jwilke   3407: : here there ;
1.67      jwilke   3408: : equ constant ;
                   3409: : mark there constant ;
1.54      pazsan   3410: 
                   3411: \ compiler directives
1.52      jwilke   3412: : >ram >ram ;
                   3413: : >rom >rom ;
                   3414: : >auto >auto ;
                   3415: : >tempdp >tempdp ;
                   3416: : tempdp> tempdp> ;
                   3417: : const constflag on ;
1.103     jwilke   3418: 
                   3419: : Redefinitions-start
                   3420: \G Starts a redefinition section. Warnings are disabled and
                   3421: \G existing ghosts are reused. This is used in the kernel
                   3422: \G where ( and \ and the like are redefined
                   3423:   twarnings off warnings off reuse-ghosts on ;
                   3424: 
                   3425: : Redefinitions-end
                   3426: \G Ends a redefinition section. Warnings are enabled again.
                   3427:   twarnings on warnings on reuse-ghosts off ;
                   3428: 
                   3429: : warnings name 3 = 
                   3430:   IF twarnings off warnings off ELSE twarnings on warnings on THEN drop ;
1.102     jwilke   3431: 
1.60      anton    3432: : | ;
                   3433: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
1.52      jwilke   3434: 
1.48      anton    3435: : save-cross save-cross ;
1.52      jwilke   3436: : save-region save-region ;
                   3437: : tdump swap >image swap dump ;
                   3438: 
1.48      anton    3439: also forth 
1.52      jwilke   3440: [IFDEF] Label           : Label defempty? Label ; [THEN] 
                   3441: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
1.77      jwilke   3442: \ [IFDEF] builttag     : builttag builttag ;   [THEN]
1.48      anton    3443: previous
                   3444: 
1.52      jwilke   3445: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
1.43      pazsan   3446: : + + ;
1.52      jwilke   3447: : 1+ 1 + ;
                   3448: : 2+ 2 + ;
1.43      pazsan   3449: : 1- 1- ;
                   3450: : - - ;
1.52      jwilke   3451: : and and ;
                   3452: : or or ;
1.43      pazsan   3453: : 2* 2* ;
                   3454: : * * ;
                   3455: : / / ;
                   3456: : dup dup ;
                   3457: : over over ;
                   3458: : swap swap ;
                   3459: : rot rot ;
                   3460: : drop drop ;
                   3461: : =   = ;
                   3462: : 0=   0= ;
                   3463: : lshift lshift ;
                   3464: : 2/ 2/ ;
1.103     jwilke   3465: \ : . . ;
1.1       anton    3466: 
1.79      jwilke   3467: : all-words    ['] forced?    IS skip? ;
1.43      pazsan   3468: : needed-words ['] needed?  IS skip? ;
1.75      jwilke   3469: : undef-words  ['] defined2? IS skip? ;
                   3470: : skipdef skipdef ;
1.1       anton    3471: 
1.40      pazsan   3472: : \  postpone \ ;  immediate
1.47      pazsan   3473: : \G T-\G ; immediate
1.40      pazsan   3474: : (  postpone ( ;  immediate
1.1       anton    3475: : include bl word count included ;
1.103     jwilke   3476: : included swap >image swap included ;
1.52      jwilke   3477: : require require ;
1.103     jwilke   3478: : needs require ;
1.1       anton    3479: : .( [char] ) parse type ;
1.52      jwilke   3480: : ." [char] " parse type ;
1.1       anton    3481: : cr cr ;
                   3482: 
1.77      jwilke   3483: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
                   3484: 
                   3485: \ only forth also cross also minimal definitions order
1.1       anton    3486: 
                   3487: \ cross-compiler words
                   3488: 
1.103     jwilke   3489: : decimal       decimal [g'] decimal >exec2 @ ?dup IF EXECUTE THEN ;
                   3490: : hex           hex [g'] hex >exec2 @ ?dup IF EXECUTE THEN ;
1.1       anton    3491: 
1.77      jwilke   3492: \ : tudp          X tudp ;
                   3493: \ : tup           X tup ;
                   3494: 
                   3495: : doc-off       false to-doc ! ;
                   3496: : doc-on        true  to-doc ! ;
1.39      pazsan   3497: 
1.103     jwilke   3498: : declareunique ( "name" -- )
                   3499: \G Sets the unique flag for a ghost. The assembler output
                   3500: \G generates labels with the ghostname concatenated with the address
                   3501: \G while cross-compiling. The address is concatenated
                   3502: \G because we have double occurences of the same name.
                   3503: \G If we want to reference the labels from the assembler or C
                   3504: \G code we declare them unique, so the address is skipped.
                   3505:   Ghost >ghost-flags dup @ <unique> or swap ! ;
                   3506: 
                   3507: \ [IFDEF] dbg : dbg dbg ; [THEN]
1.39      pazsan   3508: 
1.1       anton    3509: \ for debugging...
1.103     jwilke   3510: \ : dbg dbg ;
                   3511: : horder         order ;
                   3512: : hwords        words ;
                   3513: \ : words      also ghosts 
                   3514: \                words previous ;
1.105     jwilke   3515: : .s            .s ;
1.1       anton    3516: : bye           bye ;
1.99      pazsan   3517: 
1.103     jwilke   3518: \ dummy
                   3519: : group 0 word drop ;
                   3520: 
1.1       anton    3521: \ turnkey direction
                   3522: : H forth ; immediate
                   3523: : T minimal ; immediate
                   3524: : G ghosts ; immediate
                   3525: 
                   3526: 
                   3527: \ these ones are pefered:
                   3528: 
1.74      jwilke   3529: : unlock previous forth also cross ;
1.52      jwilke   3530: 
1.77      jwilke   3531: \ also minimal
1.103     jwilke   3532: >cross
                   3533: 
                   3534: : turnkey 
                   3535:    ghosts-wordlist 1 set-order
                   3536:    also target definitions
                   3537:    also Minimal also ;
                   3538: 
                   3539: >minimal
                   3540: 
                   3541: : [[+++
                   3542:   turnkey unlock ;
1.1       anton    3543: 
                   3544: unlock definitions also minimal
1.77      jwilke   3545: 
1.103     jwilke   3546: : lock   turnkey ;
                   3547: 
                   3548: Defer +++]]-hook
                   3549: : +++]] +++]]-hook lock ;
                   3550: 
                   3551: LOCK
1.77      jwilke   3552: \ load cross compiler extension defined in mach file
                   3553: 
                   3554: UNLOCK >CROSS
                   3555: 
                   3556: [IFDEF] extend-cross extend-cross [THEN]
                   3557: 
                   3558: LOCK

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