Annotation of gforth/cross.fs, revision 1.118

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

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