Annotation of gforth/cross.fs, revision 1.129

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

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