File:  [gforth] / gforth / cross.fs
Revision 1.115: download - view: text, annotated - select for diffs
Sun Sep 16 10:21:13 2001 UTC (22 years, 6 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
fix: cross now works again on top of kernlXYZ.fi

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

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