Annotation of gforth/cross.fs, revision 1.156

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

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