File:  [gforth] / gforth / cross.fs
Revision 1.165: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:58 2007 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

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

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