File:  [gforth] / gforth / cross.fs
Revision 1.119: download - view: text, annotated - select for diffs
Sat Jan 5 22:58:59 2002 UTC (17 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Thrown out static vocabulary stack
Changed cross to make mixed threading workable

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

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