File:  [gforth] / gforth / cross.fs
Revision 1.122: download - view: text, annotated - select for diffs
Wed Mar 20 11:28:26 2002 UTC (22 years, 1 month ago) by jwilke
Branches: MAIN
CVS tags: HEAD
No forward references to native code definitions
(primitives and doer) are allowed any more
(didn't work in any case before, but now a warning is issued)

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

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