File:  [gforth] / gforth / cross.fs
Revision 1.126: download - view: text, annotated - select for diffs
Thu Mar 21 17:26:00 2002 UTC (17 years, 8 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
fix?!

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

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