File:  [gforth] / gforth / cross.fs
Revision 1.120: download - view: text, annotated - select for diffs
Tue Mar 19 11:13:08 2002 UTC (22 years, 1 month ago) by jwilke
Branches: MAIN
CVS tags: HEAD
no functional change

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

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