Annotation of gforth/cross.fs, revision 1.108

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

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