File:  [gforth] / gforth / cross.fs
Revision 1.123: download - view: text, annotated - select for diffs
Thu Mar 21 16:35:18 2002 UTC (17 years, 8 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
added symentry and \?

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

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