File:  [gforth] / gforth / cross.fs
Revision 1.103: download - view: text, annotated - select for diffs
Tue Sep 4 13:06:06 2001 UTC (18 years, 3 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
lots of changes from the G&D project
- relocatable assembler output
- vocabularies support prepared
- lots of cleanup
- hooks for instant (target execution while cross-compilation)
- peephole changes from bernd missing
- genereation of kernl32l.fi works correctly

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

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