File:  [gforth] / gforth / cross.fs
Revision 1.128: download - view: text, annotated - select for diffs
Sat Sep 14 08:20:18 2002 UTC (17 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Added compat/strcomp.fs, introducing STR=, STRING-PREFIX?, and STR<
replaced most occurences of COMPARE with STR= and STRING-PREFIX?

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

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