Annotation of gforth/cross.fs, revision 1.105

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

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