File:  [gforth] / gforth / cross.fs
Revision 1.114: download - view: text, annotated - select for diffs
Wed Sep 12 14:55:54 2001 UTC (22 years, 7 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
added comment

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

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