Annotation of gforth/cross.fs, revision 1.106

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

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