Annotation of gforth/cross.fs, revision 1.121

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

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