Annotation of gforth/cross.fs, revision 1.117

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

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