Annotation of gforth/cross.fs, revision 1.181

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

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