File:  [gforth] / gforth / cross.fs
Revision 1.161: download - view: text, annotated - select for diffs
Sun Dec 31 13:39:11 2006 UTC (12 years, 11 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 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 abranch
 1179: true DefaultValue f83headerstring
 1180: true DefaultValue control-rack
 1181: [THEN]
 1182: 
 1183: true DefaultValue gforthcross
 1184: true DefaultValue interpreter
 1185: true DefaultValue ITC
 1186: false DefaultValue rom
 1187: false DefaultValue flash
 1188: true DefaultValue standardthreading
 1189: 
 1190: \ ANSForth environment  stuff
 1191: 8 DefaultValue ADDRESS-UNIT-BITS
 1192: 255 DefaultValue MAX-CHAR
 1193: 255 DefaultValue /COUNTED-STRING
 1194: 
 1195: >TARGET
 1196: s" relocate" T environment? H 
 1197: \ JAW why set NIL to this?!
 1198: [IF]	drop \ SetValue NIL
 1199: [ELSE]	>ENVIRON X NIL SetValue relocate
 1200: [THEN]
 1201: >TARGET
 1202: 
 1203: 0 Constant NIL
 1204: 
 1205: >CROSS
 1206: 
 1207: \ \ Create additional parameters                         19jan95py
 1208: 
 1209: \ currently cross only works for host machines with address-unit-bits
 1210: \ eual to 8 because of s! and sc!
 1211: \ but I start to query the environment just to modularize a little bit
 1212: 
 1213: : check-address-unit-bits ( -- )	
 1214: \	s" ADDRESS-UNIT-BITS" environment?
 1215: \	IF 8 <> ELSE true THEN
 1216: \	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
 1217: 
 1218: \	shit, this doesn't work because environment? is only defined for 
 1219: \	gforth.fi and not kernl???.fi
 1220: 	;
 1221: 
 1222: check-address-unit-bits
 1223: 8 Constant bits/byte	\ we define: byte is address-unit
 1224: 
 1225: 1 bits/byte lshift Constant maxbyte 
 1226: \ this sets byte size for the target machine, (probably right guess) jaw
 1227: 
 1228: T
 1229: NIL		   	Constant TNIL
 1230: cell               	Constant tcell
 1231: cell<<             	Constant tcell<<
 1232: cell>bit           	Constant tcell>bit
 1233: bits/char          	Constant tbits/char
 1234: bits/char H bits/byte T /      
 1235: 			Constant tchar
 1236: float             	Constant tfloat
 1237: 1 bits/char lshift 	Constant tmaxchar
 1238: [IFUNDEF] bits/byte
 1239: 8			Constant tbits/byte
 1240: [ELSE]
 1241: bits/byte		Constant tbits/byte
 1242: [THEN]
 1243: H
 1244: tbits/char bits/byte /	Constant tbyte
 1245: 
 1246: 
 1247: \ Variables                                            06oct92py
 1248: 
 1249: Variable (tlast)    
 1250: (tlast) Value tlast TNIL tlast !  \ Last name field
 1251: Variable tlastcfa \ Last code field
 1252: 
 1253: \ statistics						10jun97jaw
 1254: 
 1255: Variable headers-named 0 headers-named !
 1256: Variable user-vars 0 user-vars !
 1257: 
 1258: : target>bitmask-size ( u1 -- u2 )
 1259:   1- tcell>bit rshift 1+ ;
 1260: 
 1261: : allocatetarget ( size -- adr )
 1262:   dup allocate ABORT" CROSS: No memory for target"
 1263:   swap over swap erase ;
 1264: 
 1265: \ \ memregion.fs
 1266: 
 1267: 
 1268: Variable last-defined-region    \ pointer to last defined region
 1269: Variable region-link            \ linked list with all regions
 1270: Variable mirrored-link          \ linked list for mirrored regions
 1271: 0 dup mirrored-link ! region-link !
 1272: 
 1273: : >rname 9 cells + ;
 1274: : >rtouch 8 cells + ; \ executed when region is accessed
 1275: : >rbm   4 cells + ; \ bitfield per cell witch indicates relocation
 1276: : >rmem  5 cells + ;
 1277: : >rtype 6 cells + ; \ field per cell witch points to a type struct
 1278: : >rrom 7 cells + ;  \ a -1 indicates that this region is rom
 1279: : >rlink 3 cells + ;
 1280: : >rdp 2 cells + ;
 1281: : >rlen cell+ ;
 1282: : >rstart ;
 1283: 
 1284: : (region) ( addr len region -- )
 1285: \G change startaddress and length of an existing region
 1286:   >r r@ last-defined-region !
 1287:   r@ >rlen ! dup r@ >rstart ! r> >rdp ! ;
 1288: 
 1289: : uninitialized -1 ABORT" CROSS: Region is uninitialized" ;
 1290: 
 1291: : region ( addr len -- "name" )                
 1292: \G create a new region
 1293:   \ check whether predefined region exists 
 1294:   save-input bl word find >r >r restore-input throw r> r> 0= 
 1295:   IF	\ make region
 1296: 	drop
 1297: 	save-input create restore-input throw
 1298: 	here last-defined-region !
 1299: 	over ( startaddr ) , ( length ) , ( dp ) ,
 1300: 	region-link linked 0 , 0 , 0 , 0 , 
 1301:         ['] uninitialized ,
 1302:         bl word count string,
 1303:   ELSE	\ store new parameters in region
 1304:         bl word drop
 1305: 	>body (region)
 1306:   THEN ;
 1307: 
 1308: : borders ( region -- startaddr endaddr ) 
 1309: \G returns lower and upper region border
 1310:   dup >rstart @ swap >rlen @ over + ;
 1311: 
 1312: : extent  ( region -- startaddr len )   
 1313: \G returns the really used area
 1314:   dup >rstart @ swap >rdp @ over - ;
 1315: 
 1316: : area ( region -- startaddr totallen ) 
 1317: \G returns the total area
 1318:   dup >rstart @ swap >rlen @ ;
 1319: 
 1320: : dp@ ( region -- dp )
 1321:   >rdp @ ;
 1322: 
 1323: : mirrored ( -- )                              
 1324: \G mark last defined region as mirrored
 1325:   mirrored-link
 1326:   align linked last-defined-region @ , ;
 1327: 
 1328: : writeprotected
 1329: \G mark a region as write protected
 1330:   -1 last-defined-region @ >rrom ! ;
 1331: 
 1332: : .addr ( u -- )
 1333: \G prints a 16 or 32 Bit nice hex value
 1334:   base @ >r hex
 1335:   tcell 2 u>
 1336:   IF s>d <# # # # # [char] . hold # # # # #> type
 1337:   ELSE s>d <# # # # # # #> type
 1338:   THEN r> base ! space ;
 1339: 
 1340: : .regions                      \G display region statistic
 1341: 
 1342:   \ we want to list the regions in the right order
 1343:   \ so first collect all regions on stack
 1344:   0 region-link @
 1345:   BEGIN dup WHILE dup @ REPEAT drop
 1346:   BEGIN dup
 1347:   WHILE cr
 1348:         0 >rlink - >r
 1349:         r@ >rname count tuck type
 1350:         12 swap - 0 max spaces space
 1351:         ." Start: " r@ >rstart @ dup .addr space
 1352:         ." End: " r@ >rlen @ + .addr space
 1353:         ." DP: " r> >rdp @ .addr
 1354:   REPEAT drop
 1355:   s" rom" T $has? H 0= ?EXIT
 1356:   cr ." Mirrored:"
 1357:   mirrored-link @
 1358:   BEGIN dup
 1359:   WHILE space dup cell+ @ >rname count type @
 1360:   REPEAT drop cr
 1361:   ;
 1362: 
 1363: \ -------- predefined regions
 1364: 
 1365: 0 0 region address-space
 1366: \ total memory addressed and used by the target system
 1367: 
 1368: 0 0 region user-region
 1369: \ data for user variables goes here
 1370: \ this has to be defined before dictionary or ram-dictionary
 1371: 
 1372: 0 0 region dictionary
 1373: \ rom area for the compiler
 1374: 
 1375: T has? rom H
 1376: [IF]
 1377: 0 0 region ram-dictionary mirrored
 1378: \ ram area for the compiler
 1379: [ELSE]
 1380: ' dictionary ALIAS ram-dictionary
 1381: [THEN]
 1382: 
 1383: 0 0 region return-stack
 1384: 
 1385: 0 0 region data-stack
 1386: 
 1387: 0 0 region tib-region
 1388: 
 1389: ' dictionary ALIAS rom-dictionary
 1390: 
 1391: : setup-region ( region -- )
 1392:   >r
 1393:   \ allocate mem
 1394:   r@ >rlen @ allocatetarget
 1395:   r@ >rmem !
 1396: 
 1397:   r@ >rlen @
 1398:   target>bitmask-size allocatetarget
 1399:   r@ >rbm !
 1400: 
 1401:   r@ >rlen @
 1402:   tcell / 1+ cells allocatetarget r@ >rtype !
 1403: 
 1404:   ['] noop r@ >rtouch !
 1405:   rdrop ;
 1406: 
 1407: : setup-target ( -- )   \G initialize target's memory space
 1408:   s" rom" T $has? H
 1409:   IF  \ check for ram and rom...
 1410:       \ address-space area nip 0<>
 1411:       ram-dictionary area nip 0<>
 1412:       rom-dictionary area nip 0<>
 1413:       and 0=
 1414:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
 1415:   THEN
 1416:   address-space area nip
 1417:   IF
 1418:       address-space area
 1419:   ELSE
 1420:       dictionary area
 1421:   THEN
 1422:   nip 0=
 1423:   ABORT" CROSS: define at least address-space or dictionary!!"
 1424: 
 1425:   \ allocate target for each region
 1426:   region-link
 1427:   BEGIN @ dup
 1428:   WHILE dup
 1429:         0 >rlink - >r
 1430:         r@ >rlen @
 1431:         IF      r@ setup-region
 1432:         THEN    rdrop
 1433:    REPEAT drop ;
 1434: 
 1435: \ MakeKernel                                           		22feb99jaw
 1436: 
 1437: : makekernel ( start targetsize -- )
 1438: \G convenience word to setup the memory of the target
 1439: \G used by main.fs of the c-engine based systems
 1440:   dictionary (region) setup-target ;
 1441: 
 1442: >MINIMAL
 1443: : makekernel makekernel ;
 1444: >CROSS
 1445: 
 1446: \ \ switched tdp for rom support				03jun97jaw
 1447: 
 1448: \ second value is here to store some maximal value for statistics
 1449: \ tempdp is also embedded here but has nothing to do with rom support
 1450: \ (needs switched dp)
 1451: 
 1452: variable tempdp	0 ,	\ temporary dp for resolving
 1453: variable tempdp-save
 1454: 
 1455: 0 [IF]
 1456: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
 1457: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
 1458: 
 1459: \
 1460: variable sramdp		\ start of ram-area for forth
 1461: variable sromdp		\ start of rom-area for forth
 1462: 
 1463: [THEN]
 1464: 
 1465: 0 Value current-region
 1466: 0 Value tdp
 1467: Variable fixed		\ flag: true: no automatic switching
 1468: 			\	false: switching is done automatically
 1469: 
 1470: \ Switch-Policy:
 1471: \
 1472: \ a header is always compiled into rom
 1473: \ after a created word (create and variable) compilation goes to ram
 1474: \
 1475: \ Be careful: If you want to make the data behind create into rom
 1476: \ you have to put >rom before create!
 1477: 
 1478: variable constflag constflag off
 1479: 
 1480: : activate ( region -- )
 1481: \G next code goes to this region
 1482:   dup to current-region >rdp to tdp ;
 1483: 
 1484: : (switchram)
 1485:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
 1486:   ram-dictionary activate ;
 1487: 
 1488: : switchram
 1489:   constflag @
 1490:   IF constflag off ELSE (switchram) THEN ;
 1491: 
 1492: : switchrom
 1493:   fixed @ ?EXIT rom-dictionary activate ;
 1494: 
 1495: : >tempdp ( addr -- ) 
 1496:   tdp tempdp-save ! tempdp to tdp tdp ! ;
 1497: : tempdp> ( -- )
 1498:   tempdp-save @ to tdp ;
 1499: 
 1500: : >ram  fixed off (switchram) fixed on ;
 1501: : >rom  fixed off switchrom fixed on ;
 1502: : >auto fixed off switchrom ;
 1503: 
 1504: 
 1505: 
 1506: \ : romstart dup sromdp ! romdp ! ;
 1507: \ : ramstart dup sramdp ! ramdp ! ;
 1508: 
 1509: \ default compilation goes to rom
 1510: \ when romable support is off, only the rom switch is used (!!)
 1511: >auto
 1512: 
 1513: : there  tdp @ ;
 1514: 
 1515: >TARGET
 1516: 
 1517: \ \ Target Memory Handling
 1518: 
 1519: \ Byte ordering and cell size                          06oct92py
 1520: 
 1521: : cell+         tcell + ;
 1522: : cells         tcell<< lshift ;
 1523: : chars         tchar * ;
 1524: : char+		tchar + ;
 1525: : floats	tfloat * ;
 1526:     
 1527: >CROSS
 1528: : cell/         tcell<< rshift ;
 1529: >TARGET
 1530: 20 CONSTANT bl
 1531: \ TNIL Constant NIL
 1532: 
 1533: >CROSS
 1534: 
 1535: bigendian
 1536: [IF]
 1537:    : DS!  ( d addr -- )  tcell bounds swap 1-
 1538:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1539:    : DS@  ( addr -- d )  >r 0 0 r> tcell bounds
 1540:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP ;
 1541:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
 1542:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1543:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
 1544:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1545: [ELSE]
 1546:    : DS!  ( d addr -- )  tcell bounds
 1547:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1548:    : DS@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
 1549:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP ;
 1550:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
 1551:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1552:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
 1553:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1554: [THEN]
 1555: 
 1556: : S! ( n addr -- ) >r s>d r> DS! ;
 1557: : S@ ( addr -- n ) DS@ d>s ;
 1558: 
 1559: : taddr>region ( taddr -- region | 0 )
 1560: \G finds for a target-address the correct region
 1561: \G returns 0 if taddr is not in range of a target memory region
 1562:   region-link
 1563:   BEGIN @ dup
 1564:   WHILE dup >r
 1565:         0 >rlink - >r
 1566:         r@ >rlen @
 1567:         IF      dup r@ borders within
 1568:                 IF r> r> drop nip 
 1569:                    dup >rtouch @ EXECUTE EXIT 
 1570:                 THEN
 1571:         THEN
 1572:         r> drop
 1573:         r>
 1574:   REPEAT
 1575:   2drop 0 ;
 1576: 
 1577: : taddr>region-abort ( taddr -- region | 0 )
 1578: \G Same as taddr>region but aborts if taddr is not
 1579: \G a valid address in the target address space
 1580:   dup taddr>region dup 0= 
 1581:   IF    drop cr ." Wrong address: " .addr
 1582:         -1 ABORT" Address out of range!"
 1583:   THEN nip ;
 1584: 
 1585: : (>regionimage) ( taddr -- 'taddr )
 1586:   dup
 1587:   \ find region we want to address
 1588:   taddr>region-abort
 1589:   >r
 1590:   \ calculate offset in region
 1591:   r@ >rstart @ -
 1592:   \ add regions real address in our memory
 1593:   r> >rmem @ + ;
 1594: 
 1595: : (>regionramimage) ( taddr -- 'taddr )
 1596: \G same as (>regionimage) but aborts if the region is rom
 1597:   dup
 1598:   \ find region we want to address
 1599:   taddr>region-abort
 1600:   >r
 1601:   r@ >rrom @ ABORT" CROSS: region is write-protected!"
 1602:   \ calculate offset in region
 1603:   r@ >rstart @ -
 1604:   \ add regions real address in our memory
 1605:   r> >rmem @ + ;
 1606: 
 1607: : (>regionbm) ( taddr -- 'taddr bitmaskbaseaddr )
 1608:   dup
 1609:   \ find region we want to address
 1610:   taddr>region-abort
 1611:   >r
 1612:   \ calculate offset in region
 1613:   r@ >rstart @ -
 1614:   \ add regions real address in our memory
 1615:   r> >rbm @ ;
 1616: 
 1617: : (>regiontype) ( taddr -- 'taddr )
 1618:   dup
 1619:   \ find region we want to address
 1620:   taddr>region-abort
 1621:   >r
 1622:   \ calculate offset in region
 1623:   r@ >rstart @ - tcell / cells
 1624:   \ add regions real address in our memory
 1625:   r> >rtype @ + ;
 1626:   
 1627: \ Bit string manipulation                               06oct92py
 1628: \                                                       9may93jaw
 1629: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
 1630: : bits ( n -- n ) chars Bittable + c@ ;
 1631: 
 1632: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
 1633: : +bit ( addr n -- )  >bit over c@ or swap c! ;
 1634: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
 1635: 
 1636: : @relbit ( taddr -- f ) (>regionbm) swap cell/ >bit swap c@ and ;
 1637: 
 1638: : (relon) ( taddr -- )  
 1639:   [ [IFDEF] fd-relocation-table ]
 1640:   s" +" fd-relocation-table write-file throw
 1641:   dup s>d <# #s #> fd-relocation-table write-line throw
 1642:   [ [THEN] ]
 1643:   (>regionbm) swap cell/ +bit ;
 1644: 
 1645: : (reloff) ( taddr -- ) 
 1646:   [ [IFDEF] fd-relocation-table ]
 1647:   s" -" fd-relocation-table write-file throw
 1648:   dup s>d <# #s #> fd-relocation-table write-line throw
 1649:   [ [THEN] ]
 1650:   (>regionbm) swap cell/ -bit ;
 1651: 
 1652: DEFER >image
 1653: DEFER >ramimage
 1654: DEFER relon
 1655: DEFER reloff
 1656: DEFER correcter
 1657: 
 1658: T has? relocate H
 1659: [IF]
 1660: ' (relon) IS relon
 1661: ' (reloff) IS reloff
 1662: ' (>regionimage) IS >image
 1663: ' (>regionimage) IS >ramimage
 1664: [ELSE]
 1665: ' drop IS relon
 1666: ' drop IS reloff
 1667: ' (>regionimage) IS >image
 1668: ' (>regionimage) IS >ramimage
 1669: [THEN]
 1670: 
 1671: : enforce-writeprotection ( -- )
 1672:   ['] (>regionramimage) IS >ramimage ;
 1673: 
 1674: : relax-writeprotection ( -- )
 1675:   ['] (>regionimage) IS >ramimage ;
 1676: 
 1677: : writeprotection-relaxed? ( -- )
 1678:   ['] >ramimage >body @ ['] (>regionimage) = ;
 1679: 
 1680: \ Target memory access                                 06oct92py
 1681: 
 1682: : align+  ( taddr -- rest )
 1683:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
 1684: : cfalign+  ( taddr -- rest )
 1685:     \ see kernel.fs:cfaligned
 1686:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
 1687: 
 1688: >TARGET
 1689: : aligned ( taddr -- ta-addr )  dup align+ + ;
 1690: \ assumes cell alignment granularity (as GNU C)
 1691: 
 1692: : cfaligned ( taddr1 -- taddr2 )
 1693:     \ see kernel.fs
 1694:     dup cfalign+ + ;
 1695: 
 1696: : @  ( taddr -- w )     >image S@ ;
 1697: : !  ( w taddr -- )     >ramimage S! ;
 1698: : c@ ( taddr -- char )  >image Sc@ ;
 1699: : c! ( char taddr -- )  >ramimage Sc! ;
 1700: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
 1701: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
 1702: 
 1703: \ Target compilation primitives                        06oct92py
 1704: \ included A!                                          16may93jaw
 1705: 
 1706: : here  ( -- there )    there ;
 1707: : allot ( n -- )        tdp +! ;
 1708: : ,     ( w -- )        T here H tcell T allot  ! H ;
 1709: : c,    ( char -- )     T here H tchar T allot c! H ;
 1710: : align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
 1711: : cfalign ( -- )
 1712:     T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
 1713: 
 1714: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
 1715: : A!                    swap >address swap dup relon T ! H ;
 1716: : A,    ( w -- )        >address T here H relon T , H ;
 1717: 
 1718: \ high-level ghosts
 1719: 
 1720: >CROSS
 1721: 
 1722: Ghost (do)      Ghost (?do)                     2drop
 1723: Ghost (for)                                     drop
 1724: Ghost (loop)    Ghost (+loop)                   2drop
 1725: Ghost (next)                                    drop
 1726: Ghost (does>)   Ghost (does>1)  Ghost (does>2)  2drop drop
 1727: Ghost compile,                                  drop
 1728: Ghost (.")      Ghost (S")      Ghost (ABORT")  2drop drop
 1729: Ghost (C")      Ghost c(abort") Ghost type      2drop drop
 1730: Ghost '                                         drop
 1731: 
 1732: \ user ghosts
 1733: 
 1734: Ghost state drop
 1735: 
 1736: \ \ --------------------        Host/Target copy etc.     	29aug01jaw
 1737: 
 1738: 
 1739: >CROSS
 1740: 
 1741: : TD! >image DS! ;
 1742: : TD@ >image DS@ ;
 1743: 
 1744: : th-count ( taddr -- host-addr len )
 1745: \G returns host address of target string
 1746:   assert1( tbyte 1 = )
 1747:   dup X c@ swap X char+ >image swap ;
 1748: 
 1749: : ht-move ( haddr taddr len -- )
 1750: \G moves data from host-addr to destination in target-addr
 1751: \G character by character
 1752:   swap -rot bounds ?DO I c@ over X c! X char+ LOOP drop ;
 1753: 
 1754: 2Variable last-string
 1755: X has? rom [IF] $60 [ELSE] $00 [THEN] Constant header-masks
 1756: 
 1757: : ht-header,  ( addr count -- )
 1758:   dup there swap last-string 2!
 1759:     dup header-masks or T c, H bounds  ?DO  I c@ T c, H  LOOP ;
 1760: : ht-string,  ( addr count -- )
 1761:   dup there swap last-string 2!
 1762:     dup T c, H bounds  ?DO  I c@ T c, H  LOOP ;
 1763: : ht-mem, ( addr count )
 1764:     bounds ?DO  I c@  T c, H  LOOP ;
 1765: 
 1766: >TARGET
 1767: 
 1768: : count dup X c@ swap X char+ swap ;
 1769: 
 1770: : on		>r -1 -1 r> TD!  ; 
 1771: : off   	T 0 swap ! H ;
 1772: 
 1773: : tcmove ( source dest len -- )
 1774: \G cmove in target memory
 1775:   tchar * bounds
 1776:   ?DO  dup T c@ H I T c! H 1+
 1777:   tchar +LOOP  drop ;
 1778: 
 1779: : tcallot ( char size -- )
 1780:     0 ?DO  dup T c, H  tchar +LOOP  drop ;
 1781: 
 1782: : td, ( d -- )
 1783: \G Store a host value as one cell into the target
 1784:   there tcell X allot TD! ;
 1785: 
 1786: \ \ Load Assembler
 1787: 
 1788: >TARGET
 1789: H also Forth definitions
 1790: 
 1791: \ FIXME: should we include the assembler really in the forth 
 1792: \ dictionary?!?!?!? This conflicts with the existing assembler 
 1793: \ of the host forth system!!
 1794: [IFDEF] asm-include asm-include [THEN] hex
 1795: 
 1796: previous
 1797: 
 1798: 
 1799: >CROSS
 1800: 
 1801: : (cc) T a, H ;					' (cc) plugin-of colon,
 1802: : (prim) T a, H ;				' (prim) plugin-of prim,
 1803: 
 1804: : (cr) >tempdp colon, tempdp> ;                 ' (cr) plugin-of colon-resolve
 1805: : (ar) T ! H ;					' (ar) plugin-of addr-resolve
 1806: : (dr)  ( ghost res-pnt target-addr addr )
 1807: 	>tempdp drop over 
 1808: 	dup >magic @ <do:> =
 1809: 	IF 	doer,
 1810: 	ELSE	dodoes,
 1811: 	THEN 
 1812: 	tempdp> ;				' (dr) plugin-of doer-resolve
 1813: 
 1814: : (cm) ( -- addr )
 1815:     there -1 colon, ;				' (cm) plugin-of colonmark,
 1816: 
 1817: >TARGET
 1818: : compile, ( xt -- )
 1819:   dup xt>ghost >comp @ EXECUTE ;
 1820: >CROSS
 1821: 
 1822: \ resolve structure
 1823: 
 1824: : >next ;		\ link to next field
 1825: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
 1826: : >taddr cell+ cell+ ;	
 1827: : >ghost 3 cells + ;
 1828: : >file 4 cells + ;
 1829: : >line 5 cells + ;
 1830: 
 1831: : (refered) ( ghost addr tag -- )
 1832: \G creates a reference to ghost at address taddr
 1833:     >space
 1834:     rot >link linked
 1835:     ( taddr tag ) ,
 1836:     ( taddr ) , 
 1837:     last-header-ghost @ , 
 1838:     loadfile , 
 1839:     sourceline# , 
 1840:     space>
 1841: ;
 1842: 
 1843: : refered ( ghost tag -- )
 1844: \G creates a resolve structure
 1845:     T here aligned H swap (refered)
 1846:   ;
 1847: 
 1848: : killref ( addr ghost -- )
 1849: \G kills a forward reference to ghost at position addr
 1850: \G this is used to eleminate a :dovar refence after making a DOES>
 1851:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
 1852:     swap >r >link
 1853:     BEGIN dup @ dup  ( addr last this )
 1854:     WHILE dup >taddr @ r@ =
 1855:  	 IF   @ over !
 1856: 	 ELSE nip THEN
 1857:     REPEAT rdrop 2drop 
 1858:   ;
 1859: 
 1860: Defer resolve-warning
 1861: 
 1862: : reswarn-test ( ghost res-struct -- ghost res-struct )
 1863:   over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
 1864: 
 1865: : reswarn-forward ( ghost res-struct -- ghost res-struct )
 1866:   over warnhead .ghost dup ."  is referenced in " 
 1867:   >ghost @ .ghost ;
 1868: 
 1869: \ ' reswarn-test IS resolve-warning
 1870:  
 1871: \ resolve                                              14oct92py
 1872: 
 1873:  : resolve-loop ( ghost resolve-list tcfa -- )
 1874:     >r
 1875:     BEGIN dup WHILE 
 1876: \  	  dup >tag @ 2 = IF reswarn-forward THEN
 1877: 	  resolve-warning 
 1878: 	  r@ over >taddr @ 
 1879: 	  2 pick >tag @
 1880: 	  CASE	0 OF colon-resolve ENDOF
 1881: 		1 OF addr-resolve ENDOF
 1882: 		2 OF doer-resolve ENDOF
 1883: 	  ENDCASE
 1884: 	  @ \ next list element
 1885:     REPEAT 2drop rdrop 
 1886:   ;
 1887: 
 1888: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
 1889: \  >r dup >link @
 1890: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
 1891: 
 1892: \ exists                                                9may93jaw
 1893: 
 1894: : exists ( ghost tcfa -- )
 1895: \G print warning and set new target link in ghost
 1896:   swap exists-warning
 1897:   >link ! ;
 1898: 
 1899: : colon-resolved   ( ghost -- )
 1900: \ compiles a call to a colon definition,
 1901: \ compile action for >comp field
 1902:     >link @ colon, ; 
 1903: 
 1904: : prim-resolved  ( ghost -- )
 1905: \ compiles a call to a primitive
 1906:     >link @ prim, ;
 1907: 
 1908: : (is-forward)   ( ghost -- )
 1909:     colonmark, 0 (refered) ; \ compile space for call
 1910: ' (is-forward) IS is-forward
 1911: 
 1912: 0 Value resolved
 1913: 
 1914: : resolve-forward-references ( ghost resolve-list -- )
 1915:     \ loop through forward referencies
 1916:     comp-state @ >r Resolving comp-state !
 1917:     over >link @ resolve-loop 
 1918:     r> comp-state !
 1919: 
 1920:     ['] noop IS resolve-warning ;
 1921: 
 1922: 
 1923: : (resolve) ( ghost tcfa -- ghost resolve-list )
 1924:     \ check for a valid address, it is a primitive reference
 1925:     \ otherwise
 1926:     dup taddr>region 0<> IF
 1927:       \ define this address in the region address type table
 1928:       2dup (>regiontype) define-addr-struct addr-xt-ghost 
 1929:       \ we define new address only if empty
 1930:       \ this is for not to take over the alias ghost
 1931:       \ (different ghost, but identical xt)
 1932:       \ but the very first that really defines it
 1933:       dup @ 0= IF ! ELSE 2drop THEN
 1934:     THEN
 1935:     swap >r
 1936:     r@ to resolved
 1937: 
 1938: \    r@ >comp @ ['] is-forward =
 1939: \    ABORT" >comp action not set on a resolved ghost"
 1940: 
 1941:     \ copmile action defaults to colon-resolved
 1942:     \ if this is not right something must be set before
 1943:     \ calling resolve
 1944:     r@ >comp @ ['] is-forward = IF
 1945:        ['] colon-resolved r@ >comp !
 1946:    THEN
 1947:     r@ >link @ swap \ ( list tcfa R: ghost )
 1948:     \ mark ghost as resolved
 1949:     r@ >link ! <res> r@ >magic !
 1950:     r> swap ;
 1951: 
 1952: : resolve  ( ghost tcfa -- )
 1953: \G resolve referencies to ghost with tcfa
 1954:     \ is ghost resolved?, second resolve means another 
 1955:     \ definition with the same name
 1956:     over undefined? 0= IF  exists EXIT THEN
 1957:     (resolve)
 1958:     ( ghost resolve-list )
 1959:     resolve-forward-references ;
 1960: 
 1961: : resolve-noforwards ( ghost tcfa -- )
 1962: \G Same as resolve but complain if there are any
 1963: \G forward references on this ghost
 1964:    \ is ghost resolved?, second resolve means another 
 1965:    \ definition with the same name
 1966:    over undefined? 0= IF  exists EXIT THEN
 1967:    (resolve)
 1968:    IF cr ." No forward references allowed on: " .ghost cr
 1969:       -1 ABORT" Illegal forward reference"
 1970:    THEN
 1971:    drop ;
 1972: 
 1973: \ gexecute ghost,                                      01nov92py
 1974: 
 1975: : (gexecute)   ( ghost -- )
 1976:   dup >comp @ EXECUTE ;
 1977: 
 1978: : gexecute ( ghost -- )
 1979:   dup >magic @ <imm> = ABORT" CROSS: gexecute on immediate word"
 1980:   (gexecute) ;
 1981: 
 1982: : addr,  ( ghost -- )
 1983:   dup forward? IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1984: 
 1985: \ .unresolved                                          11may93jaw
 1986: 
 1987: variable ResolveFlag
 1988: 
 1989: \ ?touched                                             11may93jaw
 1990: 
 1991: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1992:                                0 <> and ;
 1993: 
 1994: : .forwarddefs ( ghost -- )
 1995:   ."  appeared in:"
 1996:   >link
 1997:   BEGIN	@ dup
 1998:   WHILE	cr 5 spaces
 1999: 	dup >ghost @ .ghost
 2000: 	."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 2001: 	."  line " dup >line @ .dec
 2002:   REPEAT 
 2003:   drop ;
 2004: 
 2005: : ?resolved  ( ghost -- )
 2006:   dup ?touched
 2007:   IF  	ResolveFlag on 
 2008: 	dup cr .ghost .forwarddefs
 2009:   ELSE 	drop 
 2010:   THEN ;
 2011: 
 2012: : .unresolved  ( -- )
 2013:   ResolveFlag off cr ." Unresolved: "
 2014:   ghost-list
 2015:   BEGIN @ dup
 2016:   WHILE dup ?resolved
 2017:   REPEAT drop ResolveFlag @
 2018:   IF
 2019:       -1 abort" Unresolved words!"
 2020:   ELSE
 2021:       ." Nothing!"
 2022:   THEN
 2023:   cr ;
 2024: 
 2025: : .stats
 2026:   base @ >r decimal
 2027:   cr ." named Headers: " headers-named @ . 
 2028:   r> base ! ;
 2029: 
 2030: >MINIMAL
 2031: 
 2032: : .unresolved .unresolved ;
 2033: 
 2034: >CROSS
 2035: \ Header states                                        12dec92py
 2036: 
 2037: \ : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
 2038: X has? f83headerstring bigendian or [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+
 2039: : flag! ( w -- )   tlast @ flag+ + dup >r T c@ xor r> c! H ;
 2040: 
 2041: VARIABLE ^imm
 2042: 
 2043: \ !! should be target wordsize specific
 2044: $80 constant alias-mask
 2045: $40 constant immediate-mask
 2046: $20 constant restrict-mask
 2047: 
 2048: >TARGET
 2049: : immediate     immediate-mask flag!
 2050:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 2051:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 2052:                 <imm> ^imm @ ! ;
 2053: : restrict      restrict-mask flag! ;
 2054: 
 2055: : isdoer	
 2056: \G define a forth word as doer, this makes obviously only sence on
 2057: \G forth processors such as the PSC1000
 2058: 		<do:> last-header-ghost @ >magic ! ;
 2059: >CROSS
 2060: 
 2061: \ Target Header Creation                               01nov92py
 2062: 
 2063: : ht-lstring, ( addr count -- )
 2064:   dup T , H bounds  ?DO  I c@ T c, H  LOOP ;
 2065: 
 2066: >TARGET
 2067: X has? f83headerstring [IF]
 2068: : name,  ( "name" -- )  bl word count ht-header, X cfalign ;
 2069: [ELSE]
 2070: : name,  ( "name" -- )  bl word count ht-lstring, X cfalign ;
 2071: [THEN]
 2072: : view,   ( -- ) ( dummy ) ;
 2073: >CROSS
 2074: 
 2075: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
 2076: 
 2077: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
 2078: \ contains the file-id of the documentation file
 2079: 
 2080: : T-\G ( -- )
 2081:     source >in @ /string doc-file-id write-line throw
 2082:     postpone \ ;
 2083: 
 2084: Variable to-doc  to-doc on
 2085: 
 2086: : cross-doc-entry  ( -- )
 2087:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
 2088:     IF
 2089: 	s" " doc-file-id write-line throw
 2090: 	s" make-doc " doc-file-id write-file throw
 2091: 
 2092: 	Last-Header-Ghost @ >ghostname doc-file-id write-file throw
 2093: 	>in @
 2094: 	[char] ( parse 2drop
 2095: 	[char] ) parse doc-file-id write-file throw
 2096: 	s"  )" doc-file-id write-file throw
 2097: 	[char] \ parse 2drop					
 2098: 	T-\G
 2099: 	>in !
 2100:     THEN ;
 2101: 
 2102: \ Target TAGS creation
 2103: 
 2104: s" kernel.TAGS" r/w create-file throw value tag-file-id
 2105: s" kernel.tags" r/w create-file throw value vi-tag-file-id
 2106: \ contains the file-id of the tags file
 2107: 
 2108: Create tag-beg 1 c,  7F c,
 2109: Create tag-end 1 c,  01 c,
 2110: Create tag-bof 1 c,  0C c,
 2111: Create tag-tab 1 c,  09 c,
 2112: 
 2113: 2variable last-loadfilename 0 0 last-loadfilename 2!
 2114: 	    
 2115: : put-load-file-name ( -- )
 2116:     sourcefilename last-loadfilename 2@ d<>
 2117:     IF
 2118: 	tag-bof count tag-file-id write-line throw
 2119: 	sourcefilename 2dup
 2120: 	tag-file-id write-file throw
 2121: 	last-loadfilename 2!
 2122: 	s" ,0" tag-file-id write-line throw
 2123:     THEN ;
 2124: 
 2125: : cross-gnu-tag-entry  ( -- )
 2126:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 2127:     IF
 2128: 	put-load-file-name
 2129: 	source >in @ min tag-file-id write-file throw
 2130: 	tag-beg count tag-file-id write-file throw
 2131: 	Last-Header-Ghost @ >ghostname tag-file-id write-file throw
 2132: 	tag-end count tag-file-id write-file throw
 2133: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
 2134: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
 2135: 	s" ,0" tag-file-id write-line throw
 2136: 	base !
 2137:     THEN ;
 2138: 
 2139: : cross-vi-tag-entry ( -- )
 2140:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 2141:     IF
 2142: 	sourcefilename vi-tag-file-id write-file throw
 2143: 	tag-tab count vi-tag-file-id write-file throw
 2144: 	Last-Header-Ghost @ >ghostname vi-tag-file-id write-file throw
 2145: 	tag-tab count vi-tag-file-id write-file throw
 2146: 	s" /^" vi-tag-file-id write-file throw
 2147: 	source vi-tag-file-id write-file throw
 2148: 	s" $/" vi-tag-file-id write-line throw
 2149:     THEN ;
 2150: 
 2151: : cross-tag-entry ( -- )
 2152:     cross-gnu-tag-entry
 2153:     cross-vi-tag-entry ;
 2154: 
 2155: \ Check for words
 2156: 
 2157: Defer skip? ' false IS skip?
 2158: 
 2159: : skipdef ( "name" -- )
 2160: \G skip definition of an undefined word in undef-words and
 2161: \G all-words mode
 2162:     Ghost dup forward?
 2163:     IF  >magic <skip> swap !
 2164:     ELSE drop THEN ;
 2165: 
 2166: : tdefined? ( "name" -- flag ) 
 2167:     Ghost undefined? 0= ;
 2168: 
 2169: : defined2? ( "name" -- flag ) 
 2170: \G return true for anything else than forward, even for <skip>
 2171: \G that's what we want
 2172:     Ghost forward? 0= ;
 2173: 
 2174: : forced? ( "name" -- flag )
 2175: \G return ture if it is a foreced skip with defskip
 2176:     Ghost >magic @ <skip> = ;
 2177: 
 2178: : needed? ( -- flag ) \ name
 2179: \G returns a false flag when
 2180: \G a word is not defined
 2181: \G a forward reference exists
 2182: \G so the definition is not skipped!
 2183:     bl word gfind
 2184:     IF dup undefined?
 2185: 	nip
 2186: 	0=
 2187:     ELSE  drop true  THEN ;
 2188: 
 2189: : doer? ( "name" -- 0 | addr ) \ name
 2190:     Ghost dup >magic @ <do:> = 
 2191:     IF >link @ ELSE drop 0 THEN ;
 2192: 
 2193: : skip-defs ( -- )
 2194:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
 2195: 
 2196: \ Target header creation
 2197: 
 2198: Variable NoHeaderFlag
 2199: NoHeaderFlag off
 2200: 
 2201: : 0.r ( n1 n2 -- ) 
 2202:     base @ >r hex 
 2203:     0 swap <# 0 ?DO # LOOP #> type 
 2204:     r> base ! ;
 2205: 
 2206: : .sym ( adr len -- )
 2207: \G escapes / and \ to produce sed output
 2208:   bounds 
 2209:   DO I c@ dup
 2210: 	CASE	[char] / OF drop ." \/" ENDOF
 2211: 		[char] \ OF drop ." \\" ENDOF
 2212: 		dup OF emit ENDOF
 2213: 	ENDCASE
 2214:     LOOP ;
 2215: 
 2216: Defer setup-execution-semantics  ' noop IS setup-execution-semantics
 2217: 0 Value lastghost
 2218: 
 2219: : (THeader ( "name" -- ghost )
 2220:     \  >in @ bl word count type 2 spaces >in !
 2221:     \ wordheaders will always be compiled to rom
 2222:     switchrom
 2223:     \ build header in target
 2224:     NoHeaderFlag @
 2225:     IF  NoHeaderFlag off
 2226:     ELSE
 2227: 	T align H view,
 2228: 	tlast @ dup 0> IF tcell - THEN T A, H  there tlast !
 2229: 	1 headers-named +!	\ Statistic
 2230: 	>in @ T name, H >in !
 2231:     THEN
 2232:     T cfalign here H tlastcfa !
 2233:     \ Old Symbol table sed-script
 2234: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
 2235:     HeaderGhost
 2236:     \ output symbol table to extra file
 2237:     dup >ghostname there symentry
 2238:     dup Last-Header-Ghost ! dup to lastghost
 2239:     dup >magic ^imm !     \ a pointer for immediate
 2240:     alias-mask flag!
 2241:     cross-doc-entry cross-tag-entry 
 2242:     setup-execution-semantics
 2243:     ;
 2244: 
 2245: \ this is the resolver information from ":"
 2246: \ resolving is done by ";"
 2247: Variable ;Resolve 1 cells allot
 2248: 
 2249: : hereresolve ( ghost -- )
 2250:   there resolve 0 ;Resolve ! ;
 2251: 
 2252: : Theader  ( "name" -- ghost )
 2253:   (THeader dup hereresolve ;
 2254: 
 2255: Variable aprim-nr -20 aprim-nr !
 2256: 
 2257: : copy-execution-semantics ( ghost-from ghost-dest -- )
 2258:   >r
 2259:   dup >exec @ r@ >exec !
 2260:   dup >comp @ r@ >comp !
 2261:   dup >exec2 @ r@ >exec2 !
 2262:   dup >exec-compile @ r@ >exec-compile !
 2263:   dup >ghost-xt @ r@ >ghost-xt !
 2264:   dup >created @ r@ >created !
 2265:   rdrop drop ;
 2266: 
 2267: >TARGET
 2268: 
 2269: : Alias    ( cfa -- ) \ name
 2270:   >in @ skip? IF  2drop  EXIT  THEN  >in !
 2271:   (THeader ( S xt ghost )
 2272:   2dup swap xt>ghost swap copy-execution-semantics
 2273:   over resolve T A, H alias-mask flag! ;
 2274: 
 2275: Variable last-prim-ghost
 2276: 0 last-prim-ghost !
 2277: 
 2278: : asmprimname, ( ghost -- : name ) 
 2279:   dup last-prim-ghost !
 2280:   >r
 2281:   here bl word count string, r@ >asm-name !
 2282:   aprim-nr @ r> >asm-dummyaddr ! ;
 2283: 
 2284: Defer setup-prim-semantics
 2285: 
 2286: : mapprim   ( "forthname" "asmlabel" -- ) 
 2287:   THeader -1 aprim-nr +! aprim-nr @ T A, H
 2288:   asmprimname, 
 2289:   setup-prim-semantics ;
 2290: 
 2291: : mapprim:   ( "forthname" "asmlabel" -- ) 
 2292:   -1 aprim-nr +! aprim-nr @
 2293:   Ghost tuck swap resolve-noforwards <do:> swap tuck >magic !
 2294:   asmprimname, ;
 2295: 
 2296: : Doer:   ( cfa -- ) \ name
 2297:   >in @ skip? IF  2drop  EXIT  THEN  >in !
 2298:   dup 0< s" prims" T $has? H 0= and
 2299:   IF
 2300:       .sourcepos ." needs doer: " >in @ bl word count type >in ! cr
 2301:   THEN
 2302:   Ghost
 2303:   tuck swap resolve-noforwards <do:> swap >magic ! ;
 2304: 
 2305: Variable prim#
 2306: : first-primitive ( n -- )  prim# ! ;
 2307: : group 0 word drop prim# @ 1- -$200 and prim# ! ;
 2308: : groupadd  ( n -- )  drop ;
 2309: : Primitive  ( -- ) \ name
 2310:   >in @ skip? IF  drop  EXIT  THEN  >in !
 2311:   s" prims" T $has? H 0=
 2312:   IF
 2313:      .sourcepos ." needs prim: " >in @ bl word count type >in ! cr
 2314:   THEN
 2315:   prim# @ (THeader ( S xt ghost )
 2316:   ['] prim-resolved over >comp !
 2317:   dup >ghost-flags <primitive> set-flag
 2318:   over resolve-noforwards T A, H alias-mask flag!
 2319:   -1 prim# +! ;
 2320: >CROSS
 2321: 
 2322: \ Conditionals and Comments                            11may93jaw
 2323: 
 2324: \G saves the existing cond action, this is used for redefining in
 2325: \G instant
 2326: Variable cond-xt-old
 2327: 
 2328: : cond-target ( -- )
 2329: \G Compiles semantic of redefined cond into new one
 2330:   cond-xt-old @ compile, ; immediate restrict
 2331: 
 2332: : ;Cond
 2333:   postpone ;
 2334:   swap ! ;  immediate
 2335: 
 2336: : Cond: ( "name" -- ) 
 2337: \g defines a conditional or another word that must
 2338: \g be executed directly while compiling
 2339: \g these words have no interpretative semantics by default
 2340:   Ghost
 2341:   >exec-compile
 2342:   dup @ cond-xt-old !
 2343:   :NONAME ;
 2344: 
 2345: 
 2346: : Comment ( -- )
 2347:   >in @ Ghost swap >in ! ' swap 
 2348:   2dup >exec-compile ! >exec ! ;
 2349: 
 2350: Comment (       Comment \
 2351: 
 2352: \ compile                                              10may93jaw
 2353: 
 2354: : compile  ( "name" -- ) \ name
 2355:   findghost
 2356:   dup >exec-compile @ ?dup
 2357:   IF    nip compile,
 2358:   ELSE  postpone literal postpone gexecute  THEN ;  immediate restrict
 2359:             
 2360: >TARGET
 2361: 
 2362: : '  ( -- xt ) 
 2363: \G returns the target-cfa of a ghost
 2364:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 2365:   g>xt ;
 2366: 
 2367: \ FIXME: this works for the current use cases, but is not
 2368: \ in all cases correct ;-) 
 2369: : comp' X ' 0 ;
 2370: 
 2371: Cond: [']  T ' H alit, ;Cond
 2372: 
 2373: >CROSS
 2374: 
 2375: : [T']
 2376: \ returns the target-cfa of a ghost, or compiles it as literal
 2377:   postpone [G'] 
 2378:   state @ IF postpone g>xt ELSE g>xt THEN ; immediate
 2379: 
 2380: \ \ threading modell					13dec92py
 2381: \ modularized						14jun97jaw
 2382: 
 2383: T 2 cells H Value xt>body
 2384: 
 2385: : (>body)   ( cfa -- pfa ) 
 2386:   xt>body + ;						' (>body) plugin-of t>body
 2387: 
 2388: : fillcfa   ( usedcells -- )
 2389:   T cells H xt>body swap -
 2390:   assert1( dup 0 >= )
 2391:   0 ?DO 0 X c, tchar +LOOP ;
 2392: 
 2393: : (doer,)   ( ghost -- ) 
 2394:   addr, 1 fillcfa ;   					' (doer,) plugin-of doer,
 2395: 
 2396: : (docol,)  ( -- ) [G'] :docol (doer,) ;		' (docol,) plugin-of docol,
 2397: 
 2398:                                                         ' NOOP plugin-of ca>native
 2399: 
 2400: : (doprim,) ( -- )
 2401:   there xt>body + ca>native T a, H 1 fillcfa ;		' (doprim,) plugin-of doprim,
 2402: 
 2403: : (doeshandler,) ( -- ) 
 2404:   T cfalign H [G'] :doesjump addr, T 0 , H ; 		' (doeshandler,) plugin-of doeshandler,
 2405: 
 2406: : (dodoes,) ( does-action-ghost -- )
 2407:   ]comp [G'] :dodoes addr, comp[
 2408:   addr,
 2409:   \ the relocator in the c engine, does not like the
 2410:   \ does-address to marked for relocation
 2411:   [ T e? ec H 0= [IF] ] T here H tcell - reloff [ [THEN] ]
 2412:   2 fillcfa ;						' (dodoes,) plugin-of dodoes,
 2413: 
 2414: : (dlit,) ( n -- ) compile lit td, ;			' (dlit,) plugin-of dlit,
 2415: 
 2416: : (lit,) ( n -- )  s>d dlit, ;				' (lit,) plugin-of lit,
 2417: 
 2418: \ if we dont produce relocatable code alit, defaults to lit, jaw
 2419: \ this is just for convenience, so we don't have to define alit,
 2420: \ seperately for embedded systems....
 2421: T has? relocate H
 2422: [IF]
 2423: : (alit,) ( n -- )  compile lit T  a, H ;		' (alit,) plugin-of alit,
 2424: [ELSE]
 2425: : (alit,) ( n -- )  lit, ;				' (alit,) plugin-of alit,
 2426: [THEN]
 2427: 
 2428: : (fini,)         compile ;s ;				' (fini,) plugin-of fini,
 2429: 
 2430: [IFUNDEF] (code) 
 2431: Defer (code)
 2432: Defer (end-code)
 2433: [THEN]
 2434: 
 2435: >TARGET
 2436: : Code
 2437:   defempty?
 2438:   (THeader ( ghost )
 2439:   ['] prim-resolved over >comp !
 2440:   there resolve-noforwards
 2441:   
 2442:   [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
 2443:   doprim, 
 2444:   [THEN]
 2445:   depth (code) ;
 2446: 
 2447: \ FIXME : no-compile -1 ABORT" this ghost is not for compilation" ;
 2448: 
 2449: : Code:
 2450:   defempty?
 2451:     Ghost >r 
 2452:     r@ >ghostname there symentry
 2453:     r@ there ca>native resolve-noforwards
 2454:     <do:> r@ >magic !
 2455:     r> drop
 2456:     depth (code) ;
 2457: 
 2458: : end-code
 2459:     (end-code)
 2460:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 2461:     ELSE true ABORT" CROSS: Stack empty" THEN
 2462:     ;
 2463: 
 2464: >CROSS
 2465: 
 2466: \ tLiteral                                             12dec92py
 2467: 
 2468: >TARGET
 2469: Cond: \G  T-\G ;Cond
 2470: 
 2471: Cond: Literal  ( n -- )   lit, ;Cond
 2472: Cond: ALiteral ( n -- )   alit, ;Cond
 2473: 
 2474: : Char ( "<char>" -- )  bl word char+ c@ ;
 2475: Cond: [Char]   ( "<char>" -- )  Char  lit, ;Cond
 2476: 
 2477: : (x#) ( adr len base -- )
 2478:   base @ >r base ! 0 0 name >number 2drop drop r> base ! ;
 2479: 
 2480: : d# $0a (x#) ;
 2481: : h# $010 (x#) ;
 2482: 
 2483: Cond: d# $0a (x#) lit, ;Cond
 2484: Cond: h# $010 (x#) lit, ;Cond
 2485: 
 2486: tchar 1 = [IF]
 2487: Cond: chars ;Cond 
 2488: [THEN]
 2489: 
 2490: \ some special literals					27jan97jaw
 2491: 
 2492: Cond: MAXU
 2493:   -1 s>d dlit,
 2494:   ;Cond
 2495: 
 2496: tcell 2 = tcell 4 = or tcell 8 = or 0=
 2497: [IF]
 2498: .( Warning: MINI and MAXI may not work with this host) cr
 2499: [THEN]
 2500: 
 2501: Cond: MINI
 2502:   tcell 2 = IF $8000 ELSE $80000000 THEN 0
 2503:   tcell 8 = IF swap THEN dlit,
 2504:   ;Cond
 2505:  
 2506: Cond: MAXI
 2507:   tcell 2 = IF $7fff ELSE $7fffffff THEN 0
 2508:   tcell 8 = IF drop -1 swap THEN dlit,
 2509:   ;Cond
 2510: 
 2511: >CROSS
 2512: 
 2513: \ Target compiling loop                                12dec92py
 2514: \ ">tib trick thrown out                               10may93jaw
 2515: \ number? defined at the top                           11may93jaw
 2516: \ replaced >in by save-input				
 2517: 
 2518: : discard 0 ?DO drop LOOP ;
 2519: 
 2520: \ compiled word might leave items on stack!
 2521: : tcom ( x1 .. xn n name -- )
 2522: \  dup count type space
 2523:   gfind 
 2524:   IF    >r ( discard saved input state ) discard r>
 2525: 	dup >exec-compile @ ?dup
 2526:         IF   nip execute-exec-compile ELSE gexecute  THEN 
 2527: 	EXIT 
 2528:   THEN
 2529:   number? dup  
 2530:   IF	0> IF swap lit,  THEN  lit, discard
 2531:   ELSE	2drop restore-input throw Ghost gexecute THEN  ;
 2532: 
 2533: \ : ; DOES>                                            13dec92py
 2534: \ ]                                                     9may93py/jaw
 2535: 
 2536: >CROSS
 2537: 
 2538: : compiling-state ( -- )
 2539: \G set states to compililng
 2540:     Compiling comp-state !
 2541:     \ if we have a state in target, change it with the compile state
 2542:     [G'] state dup undefined? 0= 
 2543:     IF >ghost-xt @ execute X on ELSE drop THEN ;
 2544: 
 2545: : interpreting-state ( -- )
 2546: \G set states to interpreting
 2547:    \ if target has a state variable, change it according to our state
 2548:    [G'] state dup undefined? 0= 
 2549:    IF >ghost-xt @ execute X off ELSE drop THEN
 2550:    Interpreting comp-state ! ;
 2551: 
 2552: >TARGET
 2553: 
 2554: : ] 
 2555:     compiling-state
 2556:     BEGIN
 2557:         BEGIN save-input bl word
 2558:               dup c@ 0= WHILE drop discard refill 0=
 2559:               ABORT" CROSS: End of file while target compiling"
 2560:         REPEAT
 2561:         tcom
 2562:         compiling? 0=
 2563:     UNTIL ;
 2564: 
 2565: \ by the way: defining a second interpreter (a compiler-)loop
 2566: \             is not allowed if a system should be ans conform
 2567: 
 2568: : (:) ( ghost -- ) 
 2569: \ common factor of : and :noname. Prepare ;Resolve and start definition
 2570:    ;Resolve ! there ;Resolve cell+ !
 2571:    docol, ]comp  colon-start depth T ] H ;
 2572: 
 2573: : : ( -- colon-sys ) \ Name
 2574:   defempty?
 2575:   constflag off \ don't let this flag work over colon defs
 2576: 		\ just to go sure nothing unwanted happens
 2577:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
 2578:   (THeader (:) ;
 2579: 
 2580: : :noname ( -- colon-sys )
 2581:   switchrom X cfalign there 
 2582:   \ define a nameless ghost
 2583:   here ghostheader dup last-header-ghost ! dup to lastghost
 2584:   (:) ;  
 2585: 
 2586: Cond: EXIT ( -- )   compile ;S  ;Cond
 2587: 
 2588: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
 2589: 
 2590: >CROSS
 2591: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
 2592:          ;Resolve cell+ @ ;
 2593: 
 2594: >TARGET
 2595: 
 2596: Cond: recurse ( -- ) Last-Header-Ghost @ gexecute ;Cond
 2597: 
 2598: Cond: ; ( -- ) 
 2599: 	depth ?dup 
 2600: 	IF   1- <> ABORT" CROSS: Stack changed"
 2601: 	ELSE true ABORT" CROSS: Stack empty" 
 2602: 	THEN
 2603: 	colon-end
 2604: 	fini,
 2605: 	comp[
 2606: 	;Resolve @ 
 2607: 	IF  ['] colon-resolved ;Resolve @ >comp !
 2608: 	    ;Resolve @ ;Resolve cell+ @ resolve 
 2609: 	THEN
 2610: 	interpreting-state
 2611: 	;Cond
 2612: 
 2613: Cond: [ ( -- ) interpreting-state ;Cond
 2614: 
 2615: >CROSS
 2616: 
 2617: 0 Value created
 2618: 
 2619: : !does ( does-action -- )
 2620:     tlastcfa @ [G'] :dovar killref
 2621:     >space here >r ghostheader space>
 2622:     ['] colon-resolved r@ >comp !
 2623:     r@ created >do:ghost ! r@ swap resolve
 2624:     r> tlastcfa @ >tempdp dodoes, tempdp> ;
 2625: 
 2626: Defer instant-interpret-does>-hook  ' noop IS instant-interpret-does>-hook
 2627: 
 2628: T has? peephole H [IF]
 2629: : does-resolved ( ghost -- )
 2630:     compile does-exec g>xt T a, H ;
 2631: [ELSE]
 2632: : does-resolved ( ghost -- )
 2633:     g>xt T a, H ;
 2634: [THEN]
 2635: 
 2636: : resolve-does>-part ( -- )
 2637: \ resolve words made by builders
 2638:   Last-Header-Ghost @ >do:ghost @ ?dup 
 2639:   IF  there resolve  THEN ;
 2640: 
 2641: >TARGET
 2642: Cond: DOES>
 2643:         T here H [ T has? peephole H [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] T cells
 2644:         H + alit, compile (does>2) compile ;s
 2645:         doeshandler, resolve-does>-part
 2646:         ;Cond
 2647: 
 2648: : DOES>
 2649:     ['] does-resolved created >comp !
 2650:     switchrom doeshandler, T here H !does 
 2651:     instant-interpret-does>-hook
 2652:     depth T ] H ;
 2653: 
 2654: >CROSS
 2655: \ Creation                                              01nov92py
 2656: 
 2657: \ Builder                                               11may93jaw
 2658: 
 2659: 0 Value built
 2660: 
 2661: : Builder    ( Create-xt do-ghost "name" -- )
 2662: \ builds up a builder in current vocabulary
 2663: \ create-xt is executed when word is interpreted
 2664: \ do:-xt is executed when the created word from builder is executed
 2665: \ for do:-xt an additional entry after the normal ghost-entrys is used
 2666: 
 2667:   ghost to built 
 2668:   built >created @ 0= IF
 2669:     built >created on
 2670:   THEN ;
 2671: 
 2672: : gdoes,  ( ghost -- )
 2673: \ makes the codefield for a word that is built
 2674:   >do:ghost @ dup undefined? 0=
 2675:   IF
 2676: 	dup >magic @ <do:> =
 2677: 	IF 	 doer, 
 2678: 	ELSE	dodoes,
 2679: 	THEN
 2680: 	EXIT
 2681:   THEN
 2682: \  compile :dodoes gexecute
 2683: \  T here H tcell - reloff 
 2684:   2 refered 
 2685:   0 fillcfa
 2686:   ;
 2687: 
 2688: : takeover-x-semantics ( S constructor-ghost new-ghost -- )
 2689:    \g stores execution semantic and compilation semantic in the built word
 2690:    swap >do:ghost @ 2dup swap >do:ghost !
 2691:    \ we use the >exec2 field for the semantic of a created word,
 2692:    \ using exec or exec2 makes no difference for normal cross-compilation
 2693:    \ but is usefull for instant where the exec field is already
 2694:    \ defined (e.g. Vocabularies)
 2695:    2dup >exec @ swap >exec2 ! 
 2696:    >comp @ swap >comp ! ;
 2697: 
 2698: 0 Value createhere
 2699: 
 2700: : create-resolve ( -- )
 2701:     created createhere resolve 0 ;Resolve ! ;
 2702: : create-resolve-immediate ( -- )
 2703:     create-resolve T immediate H ;
 2704: 
 2705: : TCreate ( <name> -- )
 2706:   create-forward-warn
 2707:   IF ['] reswarn-forward IS resolve-warning THEN
 2708:   executed-ghost @ (Theader
 2709:   dup >created on  dup to created
 2710:   2dup takeover-x-semantics
 2711:   there to createhere drop gdoes, ;
 2712: 
 2713: : RTCreate ( <name> -- )
 2714: \ creates a new word with code-field in ram
 2715:   create-forward-warn
 2716:   IF ['] reswarn-forward IS resolve-warning THEN
 2717:   \ make Alias
 2718:   executed-ghost @ (THeader 
 2719:   dup >created on  dup to created
 2720:   2dup takeover-x-semantics
 2721:   there 0 T a, H alias-mask flag!
 2722:   \ store poiter to code-field
 2723:   switchram T cfalign H
 2724:   there swap T ! H
 2725:   there tlastcfa ! 
 2726:   there to createhere drop gdoes, ;
 2727: 
 2728: : Build:  ( -- [xt] [colon-sys] )
 2729:   :noname postpone TCreate ;
 2730: 
 2731: : BuildSmart:  ( -- [xt] [colon-sys] )
 2732:   :noname
 2733:   [ T has? rom H [IF] ]
 2734:   postpone RTCreate
 2735:   [ [ELSE] ]
 2736:   postpone TCreate 
 2737:   [ [THEN] ] ;
 2738: 
 2739: : ;Build
 2740:   postpone create-resolve postpone ; built >exec ! ; immediate
 2741: 
 2742: : ;Build-immediate
 2743:     postpone create-resolve-immediate
 2744:     postpone ; built >exec ! ; immediate
 2745: 
 2746: : gdoes>  ( ghost -- addr flag )
 2747:   executed-ghost @ g>body ;
 2748: 
 2749: \ DO: ;DO                                               11may93jaw
 2750: 
 2751: : do:ghost! ( ghost -- ) built >do:ghost ! ;
 2752: : doexec! ( xt -- ) built >do:ghost @ >exec ! ;
 2753: 
 2754: : DO:     ( -- [xt] [colon-sys] )
 2755:   here ghostheader do:ghost!
 2756:   :noname postpone gdoes> ;
 2757: 
 2758: : by:     ( -- [xt] [colon-sys] ) \ name
 2759:   Ghost do:ghost!
 2760:   :noname postpone gdoes> ;
 2761: 
 2762: : ;DO ( [xt] [colon-sys] -- )
 2763:   postpone ; doexec! ; immediate
 2764: 
 2765: : by      ( -- ) \ Name
 2766:   Ghost >do:ghost @ do:ghost! ;
 2767: 
 2768: : compile: ( --[xt] [colon-sys] )
 2769: \G defines a compile time action for created words
 2770: \G by this builder
 2771:   :noname ;
 2772: 
 2773: : ;compile ( [xt] [colon-sys] -- )
 2774:   postpone ; built >do:ghost @ >comp ! ; immediate
 2775: 
 2776: \ Variables and Constants                              05dec92py
 2777: 
 2778: Builder (Constant)
 2779: Build:  ( n -- ) ;Build
 2780: by: :docon ( target-body-addr -- n ) T @ H ;DO
 2781: 
 2782: Builder Constant
 2783: Build:  ( n -- ) T , H ;Build
 2784: by (Constant)
 2785: 
 2786: Builder AConstant
 2787: Build:  ( n -- ) T A, H ;Build
 2788: by (Constant)
 2789: 
 2790: Builder 2Constant
 2791: Build:  ( d -- ) T , , H ;Build
 2792: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
 2793: 
 2794: Builder Create
 2795: BuildSmart: ;Build
 2796: by: :dovar ( target-body-addr -- addr ) ;DO
 2797: 
 2798: Builder Variable
 2799: T has? rom H [IF]
 2800: Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 , H ( switchrom ) ;Build
 2801: by (Constant)
 2802: [ELSE]
 2803: Build: T 0 , H ;Build
 2804: by Create
 2805: [THEN]
 2806: 
 2807: Builder 2Variable
 2808: T has? rom H [IF]
 2809: Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;Build
 2810: by (Constant)
 2811: [ELSE]
 2812: Build: T 0 , 0 , H ;Build
 2813: by Create
 2814: [THEN]
 2815: 
 2816: Builder AVariable
 2817: T has? rom H [IF]
 2818: Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 A, H ( switchrom ) ;Build
 2819: by (Constant)
 2820: [ELSE]
 2821: Build: T 0 A, H ;Build
 2822: by Create
 2823: [THEN]
 2824: 
 2825: \ User variables                                       04may94py
 2826: 
 2827: : tup@ user-region >rstart @ ;
 2828: 
 2829: \ Variable tup  0 tup !
 2830: \ Variable tudp 0 tudp !
 2831: 
 2832: : u,  ( n -- udp )
 2833:   current-region >r user-region activate
 2834:   X here swap X , tup@ -
 2835:   r> activate ;
 2836: 
 2837: : au, ( n -- udp )
 2838:   current-region >r user-region activate
 2839:   X here swap X a, tup@ - 
 2840:   r> activate ;
 2841: 
 2842: T has? no-userspace H [IF]
 2843: 
 2844: : buildby
 2845:   ghost >exec @ built >exec ! ;
 2846: 
 2847: Builder User
 2848: buildby Variable
 2849: by Variable
 2850: 
 2851: Builder 2User
 2852: buildby 2Variable
 2853: by 2Variable
 2854: 
 2855: Builder AUser
 2856: buildby AVariable
 2857: by AVariable
 2858: 
 2859: [ELSE]
 2860: 
 2861: Builder User
 2862: Build: 0 u, X , ;Build
 2863: by: :douser ( ghost -- up-addr )  X @ tup@ + ;DO
 2864: 
 2865: Builder 2User
 2866: Build: 0 u, X , 0 u, drop ;Build
 2867: by User
 2868: 
 2869: Builder AUser
 2870: Build: 0 au, X , ;Build
 2871: by User
 2872: 
 2873: [THEN]
 2874: 
 2875: T has? rom H [IF]
 2876: Builder (Value)
 2877: Build:  ( n -- ) ;Build
 2878: by: :dovalue ( target-body-addr -- n ) T @ @ H ;DO
 2879: 
 2880: Builder Value
 2881: Build: T here 0 A, H switchram T align here swap ! , H ;Build
 2882: by (Value)
 2883: 
 2884: Builder AValue
 2885: Build: T here 0 A, H switchram T align here swap ! A, H ;Build
 2886: by (Value)
 2887: [ELSE]
 2888: Builder (Value)
 2889: Build:  ( n -- ) ;Build
 2890: by: :dovalue ( target-body-addr -- n ) T @ H ;DO
 2891: 
 2892: Builder Value
 2893: BuildSmart: T , H ;Build
 2894: by (Value)
 2895: 
 2896: Builder AValue
 2897: BuildSmart: T A, H ;Build
 2898: by (Value)
 2899: [THEN]
 2900: 
 2901: Defer texecute
 2902: 
 2903: Builder Defer
 2904: T has? rom H [IF]
 2905:     Build: ( -- )  T here 0 A, H switchram T align here swap ! H [T'] noop T A, H ( switchrom ) ;Build
 2906:     by: :dodefer ( ghost -- ) X @ X @ texecute ;DO
 2907: [ELSE]
 2908:     BuildSmart:  ( -- ) [T'] noop T A, H ;Build
 2909:     by: :dodefer ( ghost -- ) X @ texecute ;DO
 2910: [THEN]
 2911: 
 2912: Builder interpret/compile:
 2913: Build: ( inter comp -- ) swap T A, A, H ;Build-immediate
 2914: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2915: 
 2916: \ Sturctures                                           23feb95py
 2917: 
 2918: : nalign ( addr1 n -- addr2 )
 2919: \ addr2 is the aligned version of addr1 wrt the alignment size n
 2920:  1- tuck +  swap invert and ;
 2921: 
 2922: 
 2923: Builder (Field)
 2924: Build: ;Build
 2925: by: :dofield T @ H + ;DO
 2926: 
 2927: Builder Field
 2928: Build: ( align1 offset1 align size "name" --  align2 offset2 )
 2929:     rot dup T , H ( align1 align size offset1 )
 2930:     + >r nalign r> ;Build
 2931: by (Field)
 2932: 
 2933: >TARGET
 2934: : struct  T 1 chars 0 H ;
 2935: : end-struct  T 2Constant H ;
 2936: 
 2937: : cell% ( n -- size align )
 2938:     T 1 cells H dup ;
 2939: >CROSS
 2940: 
 2941: \ Input-Methods                                            01py
 2942: 
 2943: Builder input-method
 2944: Build: ( m v -- m' v )  dup T , cell+ H ;Build
 2945: DO:  abort" Not in cross mode" ;DO
 2946: 
 2947: Builder input-var
 2948: Build: ( m v size -- m v' )  over T , H + ;Build
 2949: DO:  abort" Not in cross mode" ;DO
 2950: 
 2951: \ Peephole optimization					05sep01jaw
 2952: 
 2953: \ this section defines different compilation
 2954: \ actions for created words
 2955: \ this will help the peephole optimizer
 2956: \ I (jaw) took this from bernds latest cross-compiler
 2957: \ changes but seperated it from the original
 2958: \ Builder words. The final plan is to put this
 2959: \ into a seperate file, together with the peephole
 2960: \ optimizer for cross
 2961: 
 2962: 
 2963: T has? peephole H [IF]
 2964: 
 2965: \ .( loading peephole optimization) cr
 2966: 
 2967: >CROSS
 2968: 
 2969: : (callc) compile call T >body a, H ;		' (callc) plugin-of colon,
 2970: : (callcm) T here 0 a, 0 a, H ;                 ' (callcm) plugin-of colonmark,
 2971: : (call-res) >tempdp resolved gexecute tempdp> drop ;
 2972:                                                 ' (call-res) plugin-of colon-resolve
 2973: : (pprim) dup 0< IF  $4000 -  ELSE
 2974:     cr ." wrong usage of (prim) "
 2975:     dup gdiscover IF  .ghost  ELSE  .  THEN  cr -1 throw  THEN
 2976:     T a, H ;					' (pprim) plugin-of prim,
 2977: 
 2978: \ if we want this, we have to spilt aconstant
 2979: \ and constant!!
 2980: \ Builder (Constant)
 2981: \ compile: g>body X @ lit, ;compile
 2982: 
 2983: Builder (Constant)
 2984: compile: g>body compile lit@ T a, H ;compile
 2985: 
 2986: Builder (Value)
 2987: compile: g>body compile lit@ T a, H ;compile
 2988: 
 2989: \ this changes also Variable, AVariable and 2Variable
 2990: Builder Create
 2991: compile: g>body alit, ;compile
 2992: 
 2993: Builder User
 2994: compile: g>body compile useraddr T @ , H ;compile
 2995: 
 2996: Builder Defer
 2997: compile: g>body compile lit-perform T A, H ;compile
 2998: 
 2999: Builder (Field)
 3000: compile: g>body T @ H compile lit+ T , H ;compile
 3001: 
 3002: Builder interpret/compile:
 3003: compile: does-resolved ;compile
 3004: 
 3005: Builder input-method
 3006: compile: does-resolved ;compile
 3007: 
 3008: Builder input-var
 3009: compile: does-resolved ;compile
 3010: 
 3011: [THEN]
 3012: 
 3013: \ structural conditionals                              17dec92py
 3014: 
 3015: >CROSS
 3016: : (ncontrols?) ( n -- ) 
 3017: \g We expect n open control structures
 3018:   depth over u<= 
 3019:   ABORT" CROSS: unstructured, stack underflow"
 3020:   0 ?DO I pick 0= 
 3021:         ABORT" CROSS: unstructured" 
 3022:   LOOP ;					' (ncontrols?) plugin-of ncontrols?
 3023: 
 3024: \ : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
 3025: \ : sys?        ( sys -- sys )    dup 0= ?struc ;
 3026: 
 3027: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
 3028: 
 3029: X has? abranch [IF]
 3030:     : branchoffset ( src dest -- )  drop ;
 3031:     : offset, ( n -- )  X A, ;
 3032: [ELSE]
 3033:     : branchoffset ( src dest -- )  - tchar / ; \ ?? jaw
 3034:     : offset, ( n -- )  X , ;
 3035: [THEN]
 3036: 
 3037: :noname compile branch X here branchoffset offset, ;
 3038:   IS branch, ( target-addr -- )
 3039: :noname compile ?branch X here branchoffset offset, ;
 3040:   IS ?branch, ( target-addr -- )
 3041: :noname compile branch T here 0 H offset, ;
 3042:   IS branchmark, ( -- branchtoken )
 3043: :noname compile ?branch T here 0 H offset, ;
 3044:   IS ?branchmark, ( -- branchtoken )
 3045: :noname T here 0 H offset, ;
 3046:   IS ?domark, ( -- branchtoken )
 3047: :noname dup X @ ?struc X here over branchoffset swap X ! ;
 3048:   IS branchtoresolve, ( branchtoken -- )
 3049: :noname branchto, X here ;
 3050:   IS branchtomark, ( -- target-addr )
 3051: 
 3052: >TARGET
 3053: 
 3054: \ Structural Conditionals                              12dec92py
 3055: 
 3056: \ CLEANUP Cond: BUT       sys? swap ;Cond
 3057: \ CLEANUP Cond: YET       sys? dup ;Cond
 3058: 
 3059: >CROSS
 3060: 
 3061: Variable tleavings 0 tleavings !
 3062: 
 3063: : (done) ( do-addr -- )
 3064: \G resolve branches of leave and ?leave and ?do
 3065: \G do-addr is the address of the beginning of our
 3066: \G loop so we can take care of nested loops
 3067:     tleavings @
 3068:     BEGIN  dup
 3069:     WHILE
 3070: 	>r dup r@ cell+ @ \ address of branch
 3071: 	u> 0=	   \ lower than DO?	
 3072:     WHILE
 3073: 	r@ 2 cells + @ \ branch token
 3074: 	branchtoresolve,
 3075: 	r@ @ r> free throw
 3076:     REPEAT  r>  THEN
 3077:     tleavings ! drop ;
 3078: 
 3079: >TARGET
 3080: 
 3081: \ What for? ANS? JAW Cond: DONE   ( addr -- )  (done) ;Cond
 3082: 
 3083: >CROSS
 3084: : (leave) ( branchtoken -- )
 3085:     3 cells allocate throw >r
 3086:     T here H r@ cell+ !
 3087:     r@ 2 cells + !
 3088:     tleavings @ r@ !
 3089:     r> tleavings ! ;
 3090: >TARGET
 3091: 
 3092: : (leave,) ( -- ) 
 3093:   branchmark, (leave) ; 			' (leave,) plugin-of leave,
 3094: 
 3095: : (?leave,) ( -- )
 3096:   compile 0= ?branchmark, (leave) ; 		' (?leave,) plugin-of ?leave,
 3097: 
 3098: Cond: LEAVE     leave, ;Cond
 3099: Cond: ?LEAVE    ?leave, ;Cond
 3100: 
 3101: >CROSS
 3102: \ !!JW ToDo : Move to general tools section
 3103: 
 3104: : to1 ( x1 x2 xn n -- addr )
 3105: \G packs n stack elements in am allocated memory region
 3106:    dup dup 1+ cells allocate throw dup >r swap 1+
 3107:    0 DO tuck ! cell+ LOOP
 3108:    drop r> ;
 3109: 
 3110: : 1to ( addr -- x1 x2 xn )
 3111: \G unpacks the elements saved by to1
 3112:     dup @ swap over cells + swap
 3113:     0 DO  dup @ swap 1 cells -  LOOP
 3114:     free throw ;
 3115: 
 3116: : loop] ( target-addr -- )
 3117:   branchto, 
 3118:   dup 	X here branchoffset offset, 
 3119:   tcell - (done) ;
 3120: 
 3121: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
 3122: 
 3123: >TARGET
 3124: 
 3125: \ Structural Conditionals                              12dec92py
 3126: 
 3127: : (cs-swap) ( x1 x2 -- x2 x1 )
 3128:   swap ;					' (cs-swap) plugin-of cs-swap
 3129: 
 3130: : (ahead,) branchmark, ; 			' (ahead,) plugin-of ahead,
 3131: 
 3132: : (if,) ?branchmark, ; 				' (if,) plugin-of if,
 3133: 
 3134: : (then,) branchto, branchtoresolve, ; 		' (then,) plugin-of then,
 3135: 
 3136: : (else,) ( ahead ) branchmark, 
 3137:           swap 
 3138:           ( then ) branchto, branchtoresolve, ;	' (else,) plugin-of else,
 3139: 
 3140: : (begin,) branchtomark, ; 			' (begin,) plugin-of begin,
 3141: 
 3142: : (while,) ( if ) ?branchmark,
 3143:            swap ; 				' (while,) plugin-of while,
 3144: 
 3145: : (again,) branch, ;				' (again,) plugin-of again,
 3146: 
 3147: : (until,) ?branch, ;				' (until,) plugin-of until,
 3148: 
 3149: : (repeat,) ( again ) branch,
 3150:             ( then ) branchto, branchtoresolve, ; ' (repeat,) plugin-of repeat,
 3151: 
 3152: : (case,)   ( -- n )
 3153:   0 ;						' (case,) plugin-of case,
 3154: 
 3155: : (of,) ( n -- x1 n )
 3156:   1+ >r 
 3157:   compile over compile = 
 3158:   if, compile drop r> ;				' (of,) plugin-of of,
 3159: 
 3160: : (endof,) ( x1 n -- x2 n )
 3161:   >r 1 ncontrols? else, r> ;			' (endof,) plugin-of endof,
 3162: 
 3163: : (endcase,) ( x1 .. xn n -- )
 3164:   compile drop 0 ?DO 1 ncontrols? then, LOOP ;	' (endcase,) plugin-of endcase,
 3165: 
 3166: >TARGET
 3167: Cond: AHEAD     ahead, ;Cond
 3168: Cond: IF        if,  ;Cond
 3169: Cond: THEN      1 ncontrols? then, ;Cond
 3170: Cond: ENDIF     1 ncontrols? then, ;Cond
 3171: Cond: ELSE      1 ncontrols? else, ;Cond
 3172: 
 3173: Cond: BEGIN     begin, ;Cond
 3174: Cond: WHILE     1 ncontrols? while, ;Cond
 3175: Cond: AGAIN     1 ncontrols? again, ;Cond
 3176: Cond: UNTIL     1 ncontrols? until, ;Cond
 3177: Cond: REPEAT    2 ncontrols? repeat, ;Cond
 3178: 
 3179: Cond: CASE      case, ;Cond
 3180: Cond: OF        of, ;Cond
 3181: Cond: ENDOF     endof, ;Cond
 3182: Cond: ENDCASE   endcase, ;Cond
 3183: 
 3184: \ Structural Conditionals                              12dec92py
 3185: 
 3186: : (do,) ( -- target-addr )
 3187:   \ ?? i think 0 is too much! jaw
 3188:     0 compile (do)
 3189:     branchtomark,  2 to1 ;			' (do,) plugin-of do,
 3190: 
 3191: \ alternative for if no ?do
 3192: \ : (do,)
 3193: \     compile 2dup compile = compile IF
 3194: \     compile 2drop compile ELSE
 3195: \     compile (do) branchtomark, 2 to1 ;
 3196:     
 3197: : (?do,) ( -- target-addr )
 3198:     0 compile (?do)  ?domark, (leave)
 3199:     branchtomark,  2 to1 ;			' (?do,) plugin-of ?do,
 3200: 
 3201: : (for,) ( -- target-addr )
 3202:   compile (for) branchtomark, ;			' (for,) plugin-of for,
 3203: 
 3204: : (loop,) ( target-addr -- )
 3205:   1to compile (loop)  loop] 
 3206:   compile unloop skiploop] ;			' (loop,) plugin-of loop,
 3207: 
 3208: : (+loop,) ( target-addr -- )
 3209:   1to compile (+loop)  loop] 
 3210:   compile unloop skiploop] ;			' (+loop,) plugin-of +loop,
 3211: 
 3212: : (next,) 
 3213:   compile (next)  loop] compile unloop ;	' (next,) plugin-of next,
 3214: 
 3215: Cond: DO      	do, ;Cond
 3216: Cond: ?DO     	?do, ;Cond
 3217: Cond: FOR	for, ;Cond
 3218: 
 3219: Cond: LOOP	1 ncontrols? loop, ;Cond
 3220: Cond: +LOOP	1 ncontrols? +loop, ;Cond
 3221: Cond: NEXT	1 ncontrols? next, ;Cond
 3222: 
 3223: \ String words                                         23feb93py
 3224: 
 3225: : ,"            [char] " parse ht-string, X align ;
 3226: 
 3227: X has? control-rack [IF]
 3228: Cond: ."        compile (.")     T ," H ;Cond
 3229: Cond: S"        compile (S")     T ," H ;Cond
 3230: Cond: C"        compile (C")     T ," H ;Cond
 3231: Cond: ABORT"    compile (ABORT") T ," H ;Cond
 3232: [ELSE]
 3233: Cond: ."        '" parse tuck 2>r ahead, there 2r> ht-mem, X align
 3234:                 >r then, r> compile ALiteral compile Literal compile type ;Cond
 3235: Cond: S"        '" parse tuck 2>r ahead, there 2r> ht-mem, X align
 3236:                 >r then, r> compile ALiteral compile Literal ;Cond
 3237: Cond: C"        ahead, there [char] " parse ht-string, X align
 3238:                 >r then, r> compile ALiteral ;Cond
 3239: Cond: ABORT"    if, ahead, there [char] " parse ht-string, X align
 3240:                 >r then, r> compile ALiteral compile c(abort") then, ;Cond
 3241: [THEN]
 3242: 
 3243: X has? rom [IF]
 3244: Cond: IS        T ' >body @ H compile ALiteral compile ! ;Cond
 3245: : IS            T >address ' >body @ ! H ;
 3246: Cond: TO        T ' >body @ H compile ALiteral compile ! ;Cond
 3247: : TO            T ' >body @ ! H ;
 3248: Cond: CTO       T ' >body H compile ALiteral compile ! ;Cond
 3249: : CTO           T ' >body ! H ;
 3250: [ELSE]
 3251: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
 3252: : IS            T >address ' >body ! H ;
 3253: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
 3254: : TO            T ' >body ! H ;
 3255: [THEN]
 3256: 
 3257: Cond: defers	T ' >body @ compile, H ;Cond
 3258: 
 3259: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
 3260: 
 3261: \ linked list primitive
 3262: : linked        X here over X @ X A, swap X ! ;
 3263: : chained	T linked A, H ;
 3264: 
 3265: : err"   s" ErrLink linked" evaluate T , H
 3266:          [char] " parse ht-string, X align ;
 3267: 
 3268: : env"  [char] " parse s" EnvLink linked" evaluate
 3269:         ht-string, X align X , ;
 3270: 
 3271: : 2env" [char] " parse s" EnvLink linked" evaluate
 3272:         here >r ht-string, X align X , X ,
 3273:         r> dup T c@ H 80 and swap T c! H ;
 3274: 
 3275: \ compile must be last                                 22feb93py
 3276: 
 3277: Cond: [compile] ( -- ) \ name
 3278: \g For immediate words, works even if forward reference
 3279:       bl word gfind 0= ABORT" CROSS: Can't compile"
 3280:       (gexecute) ;Cond
 3281: 	   
 3282: Cond: postpone ( -- ) \ name
 3283:       bl word gfind 0= ABORT" CROSS: Can't compile"
 3284:       dup >magic @ <fwd> =
 3285:       ABORT" CROSS: Can't postpone on forward declaration"
 3286:       dup >magic @ <imm> =
 3287:       IF   (gexecute)
 3288:       ELSE >link @ alit, compile compile,  THEN ;Cond
 3289: 	   
 3290: \ save-cross                                           17mar93py
 3291: 
 3292: hex
 3293: 
 3294: >CROSS
 3295: Create magic  s" Gforth3x" here over allot swap move
 3296: 
 3297: bigendian 1+ \ strangely, in magic big=0, little=1
 3298: tcell 1 = 0 and or
 3299: tcell 2 = 2 and or
 3300: tcell 4 = 4 and or
 3301: tcell 8 = 6 and or
 3302: tchar 1 = 00 and or
 3303: tchar 2 = 28 and or
 3304: tchar 4 = 50 and or
 3305: tchar 8 = 78 and or
 3306: magic 7 + c!
 3307: 
 3308: : save-cross ( "image-name" "binary-name" -- )
 3309:   .regions \  s" ec" X $has? IF  .regions  THEN
 3310:   bl parse ." Saving to " 2dup type cr
 3311:   w/o bin create-file throw >r
 3312:   s" header" X $has? IF
 3313:       s" #! "           r@ write-file throw
 3314:       bl parse          r@ write-file throw
 3315:       s"  --image-file" r@ write-file throw
 3316:       #lf       r@ emit-file throw
 3317:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
 3318:       ?do
 3319: 	  bl over emit-file throw
 3320:       loop
 3321:       drop
 3322:       magic 8       r@ write-file throw \ write magic
 3323:   ELSE
 3324:       bl parse 2drop
 3325:   THEN
 3326:   >rom dictionary >rmem @ there
 3327:   s" rom" X $has? IF  dictionary >rstart @ -  THEN
 3328:   r@ write-file throw \ write image
 3329:   s" relocate" X $has? IF
 3330:       dictionary >rbm @ there 1- tcell>bit rshift 1+
 3331:                 r@ write-file throw \ write tags
 3332:   THEN
 3333:   r> close-file throw ;
 3334: 
 3335: : save-region ( addr len -- )
 3336:   bl parse w/o bin create-file throw >r
 3337:   swap >image swap r@ write-file throw
 3338:   r> close-file throw ;
 3339: 
 3340: \ save-asm-region                         		29aug01jaw
 3341: 
 3342: Variable name-ptr
 3343: Create name-buf 200 chars allot
 3344: : init-name-buf name-buf name-ptr ! ;
 3345: : nb, name-ptr @ c! 1 chars name-ptr +! ;
 3346: : $nb, ( adr len -- ) bounds ?DO I c@ nb, LOOP ;
 3347: : @nb name-ptr @ name-buf tuck - ;
 3348: 
 3349: \ stores a usefull string representation of the character
 3350: \ in the name buffer
 3351: : name-char, ( c -- )
 3352:   dup 'a 'z 1+ within IF nb, EXIT THEN
 3353:   dup 'A 'Z 1+ within IF $20 + nb, EXIT THEN
 3354:   dup '0 '9 1+ within IF nb, EXIT THEN
 3355:   CASE '+ OF s" _PLUS" $nb, ENDOF
 3356:        '- OF s" _MINUS" $nb, ENDOF
 3357:        '* OF s" _STAR" $nb, ENDOF
 3358:        '/ OF s" _SLASH" $nb, ENDOF
 3359:        '' OF s" _TICK" $nb, ENDOF
 3360:        '( OF s" _OPAREN" $nb, ENDOF
 3361:        ') OF s" _CPAREN" $nb, ENDOF
 3362:        '[ OF s" _OBRACKET" $nb, ENDOF
 3363:        '] OF s" _CBRACKET" $nb, ENDOF
 3364:        '! OF s" _STORE" $nb, ENDOF
 3365:        '@ OF s" _FETCH" $nb, ENDOF
 3366:        '> OF s" _GREATER" $nb, ENDOF
 3367:        '< OF s" _LESS" $nb, ENDOF
 3368:        '= OF s" _EQUAL" $nb, ENDOF
 3369:        '# OF s" _HASH" $nb, ENDOF
 3370:        '? OF s" _QUEST" $nb, ENDOF
 3371:        ': OF s" _COL" $nb, ENDOF
 3372:        '; OF s" _SEMICOL" $nb, ENDOF
 3373:        ', OF s" _COMMA" $nb, ENDOF
 3374:        '. OF s" _DOT" $nb, ENDOF
 3375:        '" OF s" _DQUOT" $nb, ENDOF
 3376:        dup 
 3377:        base @ >r hex s>d <# #s 'X hold '_ hold #> $nb, r> base !
 3378:   ENDCASE ;
 3379:  
 3380: : label-from-ghostname ( ghost -- addr len )
 3381:   dup >ghostname init-name-buf 'L nb, bounds 
 3382:   ?DO I c@ name-char, LOOP 
 3383:   \ we add the address to a name to make in unique
 3384:   \ because one name may appear more then once
 3385:   \ there are names (e.g. boot) that may be reference from other
 3386:   \ assembler source files, so we declare them as unique
 3387:   \ and don't add the address suffix
 3388:   dup >ghost-flags @ <unique> and 0= 
 3389:   IF   s" __" $nb, >link @ base @ >r hex 0 <# #s 'L hold #> r> base ! $nb, 
 3390:   ELSE drop 
 3391:   THEN
 3392:   @nb ;
 3393: 
 3394: \ FIXME why disabled?!
 3395: : label-from-ghostnameXX ( ghost -- addr len )
 3396: \ same as (label-from-ghostname) but caches generated names
 3397:   dup >asm-name @ ?dup IF nip count EXIT THEN
 3398:  \ dup >r (label-from-ghostname) 2dup
 3399:   align here >r string, align
 3400:   r> r> >asm-name ! ;
 3401: 
 3402: : primghostdiscover ( xt -- ghost true | xt false )
 3403:   dup 0= IF false EXIT THEN
 3404:   >r last-prim-ghost
 3405:   BEGIN @ dup
 3406:   WHILE dup >asm-dummyaddr @ r@ =
 3407:         IF rdrop true EXIT THEN
 3408:   REPEAT
 3409:   drop r> false ;
 3410: 
 3411: : gdiscover2 ( xt -- ghost true | xt false ) 
 3412:   dup taddr>region 0= IF false EXIT THEN
 3413:   dup (>regiontype) @ dup 0= IF drop false EXIT THEN
 3414:   addr-xt-ghost @ dup 0= IF drop false EXIT THEN
 3415:   nip true ;
 3416: \  dup >ghost-name @ IF nip true ELSE drop false THEN ;
 3417: 
 3418: \ generates a label name for the target address
 3419: : generate-label-name ( taddr -- addr len )
 3420:   gdiscover2
 3421:   IF dup >magic @ <do:> =
 3422:      IF >asm-name @ count EXIT THEN
 3423:      label-from-ghostname
 3424:   ELSE
 3425:      primghostdiscover
 3426:      IF   >asm-name @ count 
 3427:      ELSE base @ >r hex 0 <# #s 'L hold #> r> base !
 3428:      THEN
 3429:   THEN ;
 3430: 
 3431: Variable outfile-fd
 3432: 
 3433: : $out ( adr len -- ) outfile-fd @ write-file throw  ;
 3434: : nlout newline $out ;
 3435: : .ux ( n -- ) 
 3436:   base @ hex swap 0 <# #S #> $out base ! ;
 3437: 
 3438: : save-asm-region-part-aligned ( taddr len -- 'taddr 'len )
 3439:   dup cell/ 0 
 3440:   ?DO nlout s"    .word " $out over @relbit 
 3441:       IF   over X @ generate-label-name $out
 3442:       ELSE over X @ s" 0x0" $out .ux
 3443:       THEN
 3444:       tcell /string
 3445:   LOOP ;
 3446: 
 3447: : print-bytes ( taddr len n -- taddr' len' )
 3448:   over min dup 0> 
 3449:   IF   nlout s"    .byte " $out 0 
 3450:        ?DO  I 0> IF s" , " $out THEN
 3451:             over X c@ s" 0x0" $out .ux 1 /string 
 3452:        LOOP 
 3453:   THEN ;
 3454: 
 3455: : save-asm-region-part ( addr len -- )
 3456:   over dup X aligned swap - ?dup
 3457:   IF   print-bytes THEN
 3458:   save-asm-region-part-aligned
 3459:   dup dup X aligned swap - ?dup
 3460:   IF   2 pick @relbit ABORT" relocated field splitted"
 3461:        print-bytes
 3462:   THEN
 3463:   2drop ;
 3464: 
 3465: : print-label ( taddr -- )
 3466:   nlout generate-label-name $out s" :" $out ;
 3467: 
 3468: : snl-calc ( taddr taddr2 -- )
 3469:   tuck over - ;
 3470: 
 3471: : skip-nolables ( taddr -- taddr2 taddr len )
 3472: \G skips memory region where no lables are defined
 3473: \G starting from taddr+1
 3474: \G Labels will be introduced for each reference mark
 3475: \G in addr-refs.
 3476: \G This word deals with lables at byte addresses as well.
 3477: \G The main idea is to have an intro part which
 3478: \G skips until the next cell boundary, the middle part
 3479: \G which skips whole cells very efficiently and the third
 3480: \G part which skips the bytes to the label in a cell
 3481:   dup 1+ dup (>regiontype) 
 3482:   ( S taddr taddr-realstart type-addr )
 3483:   dup @ dup IF addr-refs @ THEN
 3484:   swap >r
 3485:   over align+ tuck tcell swap - rshift swap 0
 3486:   ?DO dup 1 and 
 3487:      IF drop rdrop snl-calc UNLOOP EXIT THEN 
 3488:      2/ swap 1+ swap 
 3489:   LOOP
 3490:   drop r> cell+
 3491:   ( S .. taddr2 type-addr ) dup
 3492:   BEGIN dup @ dup IF addr-refs @ THEN 0= WHILE cell+ REPEAT
 3493:   dup >r swap - 1 cells / tcell * + r>
 3494:   ( S .. taddr2+skiplencells type-addr )
 3495:   @ addr-refs @ 1 tcell lshift or
 3496:   BEGIN dup 1 and 0= WHILE swap 1+ swap 2/ REPEAT drop
 3497:   ( S .. taddr2+skiplencells+skiplenbytes )
 3498:   snl-calc ;
 3499: 
 3500: : insert-label ( taddr -- )
 3501:   dup 0= IF drop EXIT THEN
 3502:   \ ignore everything which points outside our memory regions
 3503:   \ maybe a primitive pointer or whatever
 3504:   dup taddr>region 0= IF drop EXIT THEN
 3505:   dup >r (>regiontype) define-addr-struct addr-refs dup @ 
 3506:   r> tcell 1- and 1 swap lshift or swap ! ;
 3507: 
 3508: \ this generates a sorted list of addresses which must be labels
 3509: \ it scans therefore a whole region
 3510: : generate-label-list-region ( taddr len -- )
 3511:   BEGIN over @relbit IF over X @ insert-label THEN
 3512:         tcell /string dup 0< 
 3513:   UNTIL 2drop ;
 3514: 
 3515: : generate-label-list ( -- )
 3516:   region-link
 3517:   BEGIN @ dup WHILE 
 3518:         dup 0 >rlink - extent 
 3519:         ?dup IF generate-label-list-region ELSE drop THEN
 3520:   REPEAT drop ;
 3521: 
 3522: : create-outfile ( addr len -- )
 3523:   w/o bin create-file throw outfile-fd ! ;
 3524: 
 3525: : close-outfile ( -- )
 3526:   outfile-fd @ close-file throw ;
 3527: 
 3528: : (save-asm-region) ( region -- )
 3529:   \ ." label list..."
 3530:   generate-label-list
 3531:   \ ." ok!" cr
 3532:   extent ( S taddr len )
 3533:   over insert-label
 3534:   2dup + dup insert-label >r ( R end-label )
 3535:   ( S taddr len ) drop
 3536:   BEGIN
 3537:      dup print-label
 3538:      dup r@ <> WHILE
 3539:      skip-nolables save-asm-region-part
 3540:   REPEAT drop rdrop ;
 3541: 
 3542: : lineout ( addr len -- )
 3543:   outfile-fd @ write-line throw ;  
 3544: 
 3545: : save-asm-region ( region adr len -- )
 3546:   create-outfile (save-asm-region) close-outfile ;
 3547: 
 3548: \ \ minimal definitions
 3549: 	   
 3550: >MINIMAL also minimal
 3551: 
 3552: \ Usefull words                                        13feb93py
 3553: 
 3554: : KB  400 * ;
 3555: 
 3556: \ \ [IF] [ELSE] [THEN] ...				14sep97jaw
 3557: 
 3558: \ it is useful to define our own structures and not to rely
 3559: \ on the words in the host system
 3560: \ The words in the host system might be defined with vocabularies
 3561: \ this doesn't work with our self-made compile-loop
 3562: 
 3563: Create parsed 20 chars allot	\ store word we parsed
 3564: 
 3565: : upcase
 3566:     parsed count bounds
 3567:     ?DO I c@ toupper I c! LOOP ;
 3568: 
 3569: : [ELSE]
 3570:     1 BEGIN
 3571: 	BEGIN bl word count dup WHILE
 3572: 	    comment? 20 umin parsed place upcase parsed count
 3573: 	    2dup s" [IF]" str= >r 
 3574: 	    2dup s" [IFUNDEF]" str= >r
 3575: 	    2dup s" [IFDEF]" str= r> or r> or
 3576: 	    IF   2drop 1+
 3577: 	    ELSE 2dup s" [ELSE]" str=
 3578: 		IF   2drop 1- dup
 3579: 		    IF 1+
 3580: 		    THEN
 3581: 		ELSE
 3582: 		    2dup s" [ENDIF]" str= >r
 3583: 		    s" [THEN]" str= r> or
 3584: 		    IF 1- THEN
 3585: 		THEN
 3586: 	    THEN
 3587: 	    ?dup 0= ?EXIT
 3588: 	REPEAT
 3589: 	2drop refill 0=
 3590:     UNTIL drop ; immediate
 3591:   
 3592: : [THEN] ( -- ) ; immediate
 3593: 
 3594: : [ENDIF] ( -- ) ; immediate
 3595: 
 3596: : [IF] ( flag -- )
 3597:     0= IF postpone [ELSE] THEN ; immediate 
 3598: 
 3599: Cond: [IF]      postpone [IF] ;Cond
 3600: Cond: [THEN]    postpone [THEN] ;Cond
 3601: Cond: [ELSE]    postpone [ELSE] ;Cond
 3602: 
 3603: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
 3604: 
 3605: : defined? tdefined? ;
 3606: : needed? needed? ;
 3607: : doer? doer? ;
 3608: 
 3609: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
 3610: 
 3611: : directive? 
 3612:   bl word count [ ' target >wordlist ] literal search-wordlist 
 3613:   dup IF nip THEN ;
 3614: 
 3615: : [IFDEF]  >in @ directive? swap >in !
 3616: 	   0= IF tdefined? ELSE name 2drop true THEN
 3617: 	   postpone [IF] ;
 3618: 
 3619: : [IFUNDEF] tdefined? 0= postpone [IF] ;
 3620: 
 3621: Cond: [IFDEF]   postpone [IFDEF] ;Cond
 3622: 
 3623: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
 3624: 
 3625: \ C: \- \+ Conditional Compiling                         09jun93jaw
 3626: 
 3627: : C: >in @ tdefined? 0=
 3628:      IF    >in ! X :
 3629:      ELSE drop
 3630:         BEGIN bl word dup c@
 3631:               IF   count comment? s" ;" str= ?EXIT
 3632:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
 3633:               THEN
 3634:         AGAIN
 3635:      THEN ;
 3636: 
 3637: : d? d? ;
 3638: 
 3639: : \D ( -- "debugswitch" ) 
 3640: \G doesn't skip line when debug switch is on
 3641:     D? 0= IF postpone \ THEN ;
 3642: 
 3643: : \- ( -- "wordname" )
 3644: \G interprets the line if word is not defined
 3645:    tdefined? IF postpone \ THEN ;
 3646: 
 3647: : \+ ( -- "wordname" )
 3648: \G interprets the line if word is defined
 3649:    tdefined? 0= IF postpone \ THEN ;
 3650: 
 3651: : \? ( -- "envorinstring" )
 3652: \G Skip line if environmental variable evaluates to false
 3653:    X has? 0= IF postpone \ THEN ;
 3654: 
 3655: Cond: \- \- ;Cond
 3656: Cond: \+ \+ ;Cond
 3657: Cond: \D \D ;Cond
 3658: Cond: \? \? ;Cond
 3659: 
 3660: : ?? bl word find IF execute ELSE drop 0 THEN ;
 3661: 
 3662: : needed:
 3663: \G defines ghost for words that we want to be compiled
 3664:   BEGIN >in @ bl word c@ WHILE >in ! Ghost drop REPEAT drop ;
 3665: 
 3666: \ words that should be in minimal
 3667: 
 3668: create s-buffer 50 chars allot
 3669: 
 3670: bigendian Constant bigendian
 3671: 
 3672: : here there ;
 3673: : equ constant ;
 3674: : mark there constant ;
 3675: 
 3676: \ compiler directives
 3677: : >ram >ram ;
 3678: : >rom >rom ;
 3679: : >auto >auto ;
 3680: : >tempdp >tempdp ;
 3681: : tempdp> tempdp> ;
 3682: : const constflag on ;
 3683: 
 3684: : Redefinitions-start
 3685: \G Starts a redefinition section. Warnings are disabled and
 3686: \G existing ghosts are reused. This is used in the kernel
 3687: \G where ( and \ and the like are redefined
 3688:   twarnings off warnings off reuse-ghosts on ;
 3689: 
 3690: : Redefinitions-end
 3691: \G Ends a redefinition section. Warnings are enabled again.
 3692:   twarnings on warnings on reuse-ghosts off ;
 3693: 
 3694: : warnings name 3 = 
 3695:   IF twarnings off warnings off ELSE twarnings on warnings on THEN drop ;
 3696: 
 3697: : | ;
 3698: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
 3699: 
 3700: : save-cross save-cross ;
 3701: : save-region save-region ;
 3702: : tdump swap >image swap dump ;
 3703: 
 3704: also forth 
 3705: [IFDEF] Label           : Label defempty? Label ; [THEN] 
 3706: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
 3707: \ [IFDEF] builttag	: builttag builttag ;	[THEN]
 3708: previous
 3709: 
 3710: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
 3711: : + + ;
 3712: : 1+ 1 + ;
 3713: : 2+ 2 + ;
 3714: : 1- 1- ;
 3715: : - - ;
 3716: : and and ;
 3717: : or or ;
 3718: : 2* 2* ;
 3719: : * * ;
 3720: : / / ;
 3721: : dup dup ;
 3722: : ?dup ?dup ;
 3723: : over over ;
 3724: : swap swap ;
 3725: : rot rot ;
 3726: : drop drop ;
 3727: : 2drop 2drop ;
 3728: : =   = ;
 3729: : <>  <> ;
 3730: : 0=   0= ;
 3731: : lshift lshift ;
 3732: : 2/ 2/ ;
 3733: : hex. base @ $10 base ! swap . base ! ;
 3734: : invert invert ;
 3735: : linkstring ( addr u n addr -- )
 3736:     X here over X @ X , swap X ! X , ht-string, X align ;
 3737: \ : . . ;
 3738: 
 3739: : all-words    ['] forced?    IS skip? ;
 3740: : needed-words ['] needed?  IS skip? ;
 3741: : undef-words  ['] defined2? IS skip? ;
 3742: : skipdef skipdef ;
 3743: 
 3744: : \  postpone \ ;  immediate
 3745: : \G T-\G ; immediate
 3746: : (  postpone ( ;  immediate
 3747: : include bl word count included ;
 3748: : included swap >image swap included ;
 3749: : require require ;
 3750: : needs require ;
 3751: : .( [char] ) parse type ;
 3752: : ERROR" [char] " parse 
 3753:   rot 
 3754:   IF cr ." *** " type ."  ***" -1 ABORT" CROSS: Target error, see text above" 
 3755:   ELSE 2drop 
 3756:   THEN ;
 3757: : ." [char] " parse type ;
 3758: : cr cr ;
 3759: 
 3760: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
 3761: 
 3762: \ only forth also cross also minimal definitions order
 3763: 
 3764: \ cross-compiler words
 3765: 
 3766: : decimal       decimal [g'] decimal >exec2 @ ?dup IF EXECUTE THEN ;
 3767: : hex           hex [g'] hex >exec2 @ ?dup IF EXECUTE THEN ;
 3768: 
 3769: \ : tudp          X tudp ;
 3770: \ : tup           X tup ;
 3771: 
 3772: : doc-off       false to-doc ! ;
 3773: : doc-on        true  to-doc ! ;
 3774: 
 3775: : declareunique ( "name" -- )
 3776: \ Sets the unique flag for a ghost. The assembler output
 3777: \ generates labels with the ghostname concatenated with the address
 3778: \ while cross-compiling. The address is concatenated
 3779: \ because we have double occurences of the same name.
 3780: \ If we want to reference the labels from the assembler or C
 3781: \ code we declare them unique, so the address is skipped.
 3782:   Ghost >ghost-flags dup @ <unique> or swap ! ;
 3783: 
 3784: \ [IFDEF] dbg : dbg dbg ; [THEN]
 3785: 
 3786: \ for debugging...
 3787: \ : dbg dbg ;
 3788: : horder         order ;
 3789: : hwords        words ;
 3790: \ : words 	also ghosts 
 3791: \                words previous ;
 3792: : .s            .s ;
 3793: : depth         depth ;
 3794: : bye           bye ;
 3795: 
 3796: \ dummy
 3797: 
 3798: \ turnkey direction
 3799: : H forth ; immediate
 3800: : T minimal ; immediate
 3801: : G ghosts ; immediate
 3802: 
 3803: 
 3804: \ these ones are pefered:
 3805: 
 3806: : unlock previous forth also cross ;
 3807: 
 3808: \ also minimal
 3809: >cross
 3810: 
 3811: : turnkey 
 3812:    ghosts-wordlist 1 set-order
 3813:    also target definitions
 3814:    also Minimal also ;
 3815: 
 3816: >minimal
 3817: 
 3818: : [[+++
 3819:   turnkey unlock ;
 3820: 
 3821: unlock definitions also minimal
 3822: 
 3823: : lock   turnkey ;
 3824: 
 3825: Defer +++]]-hook
 3826: : +++]] +++]]-hook lock ;
 3827: 
 3828: LOCK
 3829: \ load cross compiler extension defined in mach file
 3830: 
 3831: UNLOCK >CROSS
 3832: 
 3833: [IFDEF] extend-cross extend-cross [THEN]
 3834: 
 3835: LOCK

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