File:  [gforth] / gforth / cross.fs
Revision 1.92: download - view: text, annotated - select for diffs
Sun Feb 4 22:37:12 2001 UTC (18 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed problems with new word header format for Gforth EC (now only tested
for gf8086.com)

    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: \ \ GhostNames Ghosts                                  9may93jaw
  632: 
  633: \ second name source to search trough list
  634: 
  635: VARIABLE GhostNames
  636: 0 GhostNames !
  637: 
  638: : GhostName ( -- addr )
  639:     align here GhostNames @ , GhostNames ! here 0 ,
  640:     bl word count
  641:     \ 2dup type space
  642:     string, \ !! cfalign ?
  643:     align ;
  644: 
  645: \ Ghost Builder                                        06oct92py
  646: 
  647: \ <T T> new version with temp variable                 10may93jaw
  648: 
  649: VARIABLE VocTemp
  650: 
  651: : <T  get-current VocTemp ! also Ghosts definitions ;
  652: : T>  previous VocTemp @ set-current ;
  653: 
  654: hex
  655: 4711 Constant <fwd>             4712 Constant <res>
  656: 4713 Constant <imm>             4714 Constant <do:>
  657: 4715 Constant <skip>
  658: 
  659: \ iForth makes only immediate directly after create
  660: \ make atonce trick! ?
  661: 
  662: Variable atonce atonce off
  663: 
  664: : NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;
  665: 
  666: : GhostHeader <fwd> , 0 , ['] NoExec , ;
  667: 
  668: : >magic ;		\ type of ghost
  669: : >link cell+ ;		\ pointer where ghost is in target, or if unresolved
  670: 			\ points to the where we have to resolve (linked-list)
  671: : >exec cell+ cell+ ;	\ execution symantics (while target compiling) of ghost
  672: : >end 3 cells + ;	\ room for additional tags
  673: 			\ for builder (create, variable...) words the
  674: 			\ execution symantics of words built are placed here
  675: 
  676: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
  677: Variable last-ghost	\ last ghost that is created
  678: Variable last-header-ghost \ last ghost definitions with header
  679: 
  680: : Make-Ghost ( "name" -- ghost )
  681:   >in @ GhostName swap >in !
  682:   <T Create atonce @ IF immediate atonce off THEN
  683:   here tuck swap ! ghostheader T>
  684:   dup last-ghost !
  685:   DOES> dup executed-ghost ! >exec @ execute ;
  686: 
  687: \ ghost words                                          14oct92py
  688: \                                          changed:    10may93py/jaw
  689: 
  690: : gfind   ( string -- ghost true/1 / string false )
  691: \ searches for string in word-list ghosts
  692:   dup count [ ' ghosts >wordlist ] Literal search-wordlist
  693:   dup IF >r >body nip r>  THEN ;
  694: 
  695: : gdiscover ( xt -- ghost true | xt false )
  696:   GhostNames
  697:   BEGIN @ dup
  698:   WHILE 2dup
  699:         cell+ @ dup >magic @ <fwd> <>
  700:         >r >link @ = r> and
  701:         IF cell+ @ nip true EXIT THEN
  702:   REPEAT
  703:   drop false ;
  704: 
  705: VARIABLE Already
  706: 
  707: : ghost   ( "name" -- ghost )
  708:   Already off
  709:   >in @  bl word gfind   IF  atonce off Already on nip EXIT  THEN
  710:   drop  >in !  Make-Ghost ;
  711: 
  712: : >ghostname ( ghost -- adr len )
  713:   GhostNames
  714:   BEGIN @ dup
  715:   WHILE 2dup cell+ @ =
  716:   UNTIL nip 2 cells + count
  717:   ELSE  2drop 
  718: 	\ true abort" CROSS: Ghostnames inconsistent"
  719: 	s" ?!?!?!"
  720:   THEN ;
  721: 
  722: : .ghost ( ghost -- ) >ghostname type ;
  723: 
  724: \ ' >ghostname ALIAS @name
  725: 
  726: : forward? ( ghost -- flag )
  727:   >magic @ <fwd> = ;
  728: 
  729: : undefined? ( ghost -- flag )
  730:   >magic @ dup <fwd> = swap <skip> = or ;
  731: 
  732: \ Predefined ghosts                                    12dec92py
  733: 
  734: ghost 0=                                        drop
  735: ghost branch    ghost ?branch                   2drop
  736: ghost (do)      ghost (?do)                     2drop
  737: ghost (for)                                     drop
  738: ghost (loop)    ghost (+loop)                   2drop
  739: ghost (next)                                    drop
  740: ghost unloop    ghost ;S                        2drop
  741: ghost lit       ghost (compile) ghost !         2drop drop
  742: ghost (does>)   ghost noop                      2drop
  743: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  744: ghost '                                         drop
  745: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
  746: ghost :dovar					drop
  747: ghost over      ghost =         ghost drop      2drop drop
  748: ghost - drop
  749: ghost 2drop drop
  750: ghost 2dup drop
  751: 
  752: \ \ Parameter for target systems                         06oct92py
  753: 
  754: \ we define it ans like...
  755: wordlist Constant target-environment
  756: 
  757: VARIABLE env-current \ save information of current dictionary to restore with environ>
  758: 
  759: : >ENVIRON get-current env-current ! target-environment set-current ;
  760: : ENVIRON> env-current @ set-current ; 
  761: 
  762: >TARGET
  763: 
  764: : environment? ( adr len -- [ x ] true | false )
  765:   target-environment search-wordlist 
  766:   IF execute true ELSE false THEN ;
  767: 
  768: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
  769: 
  770: : has? 	bl word count T environment? H 
  771: 	IF 	\ environment variable is present, return its value
  772: 	ELSE	\ environment variable is not present, return false
  773: 		false \ debug true ABORT" arg" 
  774: 	THEN ;
  775: 
  776: : $has? T environment? H IF ELSE false THEN ;
  777: 
  778: >ENVIRON get-order get-current swap 1+ set-order
  779: true SetValue compiler
  780: true SetValue cross
  781: true SetValue standard-threading
  782: >TARGET previous
  783: 
  784: 0
  785: [IFDEF] mach-file mach-file count 1 [THEN]
  786: [IFDEF] machine-file machine-file 1 [THEN]
  787: [IF] 	included hex drop
  788: [ELSE]  cr ." No machine description!" ABORT 
  789: [THEN]
  790: 
  791: >ENVIRON
  792: 
  793: T has? ec H
  794: [IF]
  795: false DefaultValue relocate
  796: false DefaultValue file
  797: false DefaultValue OS
  798: false DefaultValue prims
  799: false DefaultValue floating
  800: false DefaultValue glocals
  801: false DefaultValue dcomps
  802: false DefaultValue hash
  803: false DefaultValue xconds
  804: false DefaultValue header
  805: false DefaultValue backtrace
  806: false DefaultValue new-input
  807: [THEN]
  808: 
  809: true DefaultValue interpreter
  810: true DefaultValue ITC
  811: false DefaultValue rom
  812: true DefaultValue standardthreading
  813: 
  814: >TARGET
  815: s" relocate" T environment? H 
  816: [IF]	SetValue NIL
  817: [ELSE]	>ENVIRON T NIL H SetValue relocate
  818: [THEN]
  819: 
  820: >CROSS
  821: 
  822: \ \ Create additional parameters                         19jan95py
  823: 
  824: \ currently cross only works for host machines with address-unit-bits
  825: \ eual to 8 because of s! and sc!
  826: \ but I start to query the environment just to modularize a little bit
  827: 
  828: : check-address-unit-bits ( -- )	
  829: \	s" ADDRESS-UNIT-BITS" environment?
  830: \	IF 8 <> ELSE true THEN
  831: \	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
  832: 
  833: \	shit, this doesn't work because environment? is only defined for 
  834: \	gforth.fi and not kernl???.fi
  835: 	;
  836: 
  837: check-address-unit-bits
  838: 8 Constant bits/byte	\ we define: byte is address-unit
  839: 
  840: 1 bits/byte lshift Constant maxbyte 
  841: \ this sets byte size for the target machine, (probably right guess) jaw
  842: 
  843: T
  844: NIL		   	Constant TNIL
  845: cell               	Constant tcell
  846: cell<<             	Constant tcell<<
  847: cell>bit           	Constant tcell>bit
  848: bits/char          	Constant tbits/char
  849: bits/char H bits/byte T /      
  850: 			Constant tchar
  851: float             	Constant tfloat
  852: 1 bits/char lshift 	Constant tmaxchar
  853: [IFUNDEF] bits/byte
  854: 8			Constant tbits/byte
  855: [ELSE]
  856: bits/byte		Constant tbits/byte
  857: [THEN]
  858: H
  859: tbits/char bits/byte /	Constant tbyte
  860: 
  861: 
  862: \ Variables                                            06oct92py
  863: 
  864: Variable image
  865: Variable tlast    TNIL tlast !  \ Last name field
  866: Variable tlastcfa \ Last code field
  867: Variable tdoes    \ Resolve does> calls
  868: Variable bit$
  869: 
  870: \ statistics						10jun97jaw
  871: 
  872: Variable headers-named 0 headers-named !
  873: Variable user-vars 0 user-vars !
  874: 
  875: : target>bitmask-size ( u1 -- u2 )
  876:   1- tcell>bit rshift 1+ ;
  877: 
  878: : allocatetarget ( size --- adr )
  879:   dup allocate ABORT" CROSS: No memory for target"
  880:   swap over swap erase ;
  881: 
  882: \ \ memregion.fs
  883: 
  884: 
  885: Variable last-defined-region    \ pointer to last defined region
  886: Variable region-link            \ linked list with all regions
  887: Variable mirrored-link          \ linked list for mirrored regions
  888: 0 dup mirrored-link ! region-link !
  889: 
  890: 
  891: : >rname 6 cells + ;
  892: : >rbm   5 cells + ;
  893: : >rmem  4 cells + ;
  894: : >rlink 3 cells + ;
  895: : >rdp 2 cells + ;
  896: : >rlen cell+ ;
  897: : >rstart ;
  898: 
  899: 
  900: : region ( addr len -- )                \G create a new region
  901:   \ check whether predefined region exists 
  902:   save-input bl word find >r >r restore-input throw r> r> 0= 
  903:   IF	\ make region
  904: 	drop
  905: 	save-input create restore-input throw
  906: 	here last-defined-region !
  907: 	over ( startaddr ) , ( length ) , ( dp ) ,
  908: 	region-link linked 0 , 0 , bl word count string,
  909:   ELSE	\ store new parameters in region
  910:         bl word drop
  911: 	>body >r r@ last-defined-region !
  912: 	r@ >rlen ! dup r@ >rstart ! r> >rdp !
  913:   THEN ;
  914: 
  915: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
  916:   dup >rstart @ swap >rlen @ over + ;
  917: 
  918: : extent  ( region -- startaddr len )   \G returns the really used area
  919:   dup >rstart @ swap >rdp @ over - ;
  920: 
  921: : area ( region -- startaddr totallen ) \G returns the total area
  922:   dup >rstart @ swap >rlen @ ;
  923: 
  924: : mirrored                              \G mark a region as mirrored
  925:   mirrored-link
  926:   align linked last-defined-region @ , ;
  927: 
  928: : .addr ( u -- )
  929: \G prints a 16 or 32 Bit nice hex value
  930:   base @ >r hex
  931:   tcell 2 u>
  932:   IF s>d <# # # # # [char] . hold # # # # #> type
  933:   ELSE s>d <# # # # # # #> type
  934:   THEN r> base ! ;
  935: 
  936: : .regions                      \G display region statistic
  937: 
  938:   \ we want to list the regions in the right order
  939:   \ so first collect all regions on stack
  940:   0 region-link @
  941:   BEGIN dup WHILE dup @ REPEAT drop
  942:   BEGIN dup
  943:   WHILE cr
  944:         0 >rlink - >r
  945:         r@ >rname count tuck type
  946:         12 swap - 0 max spaces space
  947:         ." Start: " r@ >rstart @ dup .addr space
  948:         ." End: " r@ >rlen @ + .addr space
  949:         ." DP: " r> >rdp @ .addr
  950:   REPEAT drop
  951:   s" rom" T $has? H 0= ?EXIT
  952:   cr ." Mirrored:"
  953:   mirrored-link @
  954:   BEGIN dup
  955:   WHILE space dup cell+ @ >rname count type @
  956:   REPEAT drop cr
  957:   ;
  958: 
  959: \ -------- predefined regions
  960: 
  961: 0 0 region address-space
  962: \ total memory addressed and used by the target system
  963: 
  964: 0 0 region dictionary
  965: \ rom area for the compiler
  966: 
  967: T has? rom H
  968: [IF]
  969: 0 0 region ram-dictionary mirrored
  970: \ ram area for the compiler
  971: [ELSE]
  972: ' dictionary ALIAS ram-dictionary
  973: [THEN]
  974: 
  975: 0 0 region return-stack
  976: 
  977: 0 0 region data-stack
  978: 
  979: 0 0 region tib-region
  980: 
  981: ' dictionary ALIAS rom-dictionary
  982: 
  983: 
  984: : setup-target ( -- )   \G initialize targets memory space
  985:   s" rom" T $has? H
  986:   IF  \ check for ram and rom...
  987:       \ address-space area nip 0<>
  988:       ram-dictionary area nip 0<>
  989:       rom-dictionary area nip 0<>
  990:       and 0=
  991:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
  992:   THEN
  993:   address-space area nip
  994:   IF
  995:       address-space area
  996:   ELSE
  997:       dictionary area
  998:   THEN
  999:   nip 0=
 1000:   ABORT" CROSS: define at least address-space or dictionary!!"
 1001: 
 1002:   \ allocate target for each region
 1003:   region-link
 1004:   BEGIN @ dup
 1005:   WHILE dup
 1006:         0 >rlink - >r
 1007:         r@ >rlen @
 1008:         IF      \ allocate mem
 1009:                 r@ >rlen @ dup
 1010: 
 1011:                 allocatetarget dup image !
 1012:                 r@ >rmem !
 1013: 
 1014:                 target>bitmask-size allocatetarget
 1015:                 dup bit$ !
 1016:                 r> >rbm !
 1017: 
 1018:         ELSE    r> drop THEN
 1019:    REPEAT drop ;
 1020: 
 1021: \ MakeKernal                                           		22feb99jaw
 1022: 
 1023: : makekernel ( targetsize -- targetsize )
 1024:   dup dictionary >rlen ! setup-target ;
 1025: 
 1026: >MINIMAL
 1027: : makekernel makekernel ;
 1028: >CROSS
 1029: 
 1030: \ \ switched tdp for rom support				03jun97jaw
 1031: 
 1032: \ second value is here to store some maximal value for statistics
 1033: \ tempdp is also embedded here but has nothing to do with rom support
 1034: \ (needs switched dp)
 1035: 
 1036: variable tempdp	0 ,	\ temporary dp for resolving
 1037: variable tempdp-save
 1038: 
 1039: 0 [IF]
 1040: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
 1041: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
 1042: 
 1043: \
 1044: variable sramdp		\ start of ram-area for forth
 1045: variable sromdp		\ start of rom-area for forth
 1046: 
 1047: [THEN]
 1048: 
 1049: 
 1050: 0 value tdp
 1051: variable fixed		\ flag: true: no automatic switching
 1052: 			\	false: switching is done automatically
 1053: 
 1054: \ Switch-Policy:
 1055: \
 1056: \ a header is always compiled into rom
 1057: \ after a created word (create and variable) compilation goes to ram
 1058: \
 1059: \ Be careful: If you want to make the data behind create into rom
 1060: \ you have to put >rom before create!
 1061: 
 1062: variable constflag constflag off
 1063: 
 1064: : activate ( region -- )
 1065: \G next code goes to this region
 1066:   >rdp to tdp ;
 1067: 
 1068: : (switchram)
 1069:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
 1070:   ram-dictionary activate ;
 1071: 
 1072: : switchram
 1073:   constflag @
 1074:   IF constflag off ELSE (switchram) THEN ;
 1075: 
 1076: : switchrom
 1077:   fixed @ ?EXIT rom-dictionary activate ;
 1078: 
 1079: : >tempdp ( addr -- ) 
 1080:   tdp tempdp-save ! tempdp to tdp tdp ! ;
 1081: : tempdp> ( -- )
 1082:   tempdp-save @ to tdp ;
 1083: 
 1084: : >ram  fixed off (switchram) fixed on ;
 1085: : >rom  fixed off switchrom fixed on ;
 1086: : >auto fixed off switchrom ;
 1087: 
 1088: 
 1089: 
 1090: \ : romstart dup sromdp ! romdp ! ;
 1091: \ : ramstart dup sramdp ! ramdp ! ;
 1092: 
 1093: \ default compilation goes to rom
 1094: \ when romable support is off, only the rom switch is used (!!)
 1095: >auto
 1096: 
 1097: : there  tdp @ ;
 1098: 
 1099: >TARGET
 1100: 
 1101: \ \ Target Memory Handling
 1102: 
 1103: \ Byte ordering and cell size                          06oct92py
 1104: 
 1105: : cell+         tcell + ;
 1106: : cells         tcell<< lshift ;
 1107: : chars         tchar * ;
 1108: : char+		tchar + ;
 1109: : floats	tfloat * ;
 1110:     
 1111: >CROSS
 1112: : cell/         tcell<< rshift ;
 1113: >TARGET
 1114: 20 CONSTANT bl
 1115: \ TNIL Constant NIL
 1116: 
 1117: >CROSS
 1118: 
 1119: bigendian
 1120: [IF]
 1121:    : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
 1122:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1123:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds
 1124:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1125:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
 1126:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1127:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
 1128:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1129: [ELSE]
 1130:    : S!  ( n addr -- )  >r s>d r> tcell bounds
 1131:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1132:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
 1133:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1134:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
 1135:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1136:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
 1137:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1138: [THEN]
 1139: 
 1140: : taddr>region ( taddr -- region | 0 )
 1141: \G finds for a target-address the correct region
 1142: \G returns 0 if taddr is not in range of a target memory region
 1143:   region-link
 1144:   BEGIN @ dup
 1145:   WHILE dup >r
 1146:         0 >rlink - >r
 1147:         r@ >rlen @
 1148:         IF      dup r@ borders within
 1149:                 IF r> r> drop nip EXIT THEN
 1150:         THEN
 1151:         r> drop
 1152:         r>
 1153:   REPEAT
 1154:   2drop 0 ;
 1155: 
 1156: : (>regionimage) ( taddr -- 'taddr )
 1157:   dup
 1158:   \ find region we want to address
 1159:   taddr>region dup 0= ABORT" Address out of range!"
 1160:   >r
 1161:   \ calculate offset in region
 1162:   r@ >rstart @ -
 1163:   \ add regions real address in our memory
 1164:   r> >rmem @ + ;
 1165: 
 1166: \ Bit string manipulation                               06oct92py
 1167: \                                                       9may93jaw
 1168: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
 1169: : bits ( n -- n ) chars Bittable + c@ ;
 1170: 
 1171: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
 1172: : +bit ( addr n -- )  >bit over c@ or swap c! ;
 1173: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
 1174: 
 1175: : (relon) ( taddr -- )  
 1176:   [ [IFDEF] fd-relocation-table ]
 1177:   s" +" fd-relocation-table write-file throw
 1178:   dup s>d <# #s #> fd-relocation-table write-line throw
 1179:   [ [THEN] ]
 1180:   bit$ @ swap cell/ +bit ;
 1181: 
 1182: : (reloff) ( taddr -- ) 
 1183:   [ [IFDEF] fd-relocation-table ]
 1184:   s" -" fd-relocation-table write-file throw
 1185:   dup s>d <# #s #> fd-relocation-table write-line throw
 1186:   [ [THEN] ]
 1187:   bit$ @ swap cell/ -bit ;
 1188: 
 1189: : (>image) ( taddr -- absaddr ) image @ + ;
 1190: 
 1191: DEFER >image
 1192: DEFER relon
 1193: DEFER reloff
 1194: DEFER correcter
 1195: 
 1196: T has? relocate H
 1197: [IF]
 1198: ' (relon) IS relon
 1199: ' (reloff) IS reloff
 1200: ' (>image) IS >image
 1201: [ELSE]
 1202: ' drop IS relon
 1203: ' drop IS reloff
 1204: ' (>regionimage) IS >image
 1205: [THEN]
 1206: 
 1207: \ Target memory access                                 06oct92py
 1208: 
 1209: : align+  ( taddr -- rest )
 1210:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
 1211: : cfalign+  ( taddr -- rest )
 1212:     \ see kernel.fs:cfaligned
 1213:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
 1214: 
 1215: >TARGET
 1216: : aligned ( taddr -- ta-addr )  dup align+ + ;
 1217: \ assumes cell alignment granularity (as GNU C)
 1218: 
 1219: : cfaligned ( taddr1 -- taddr2 )
 1220:     \ see kernel.fs
 1221:     dup cfalign+ + ;
 1222: 
 1223: : @  ( taddr -- w )     >image S@ ;
 1224: : !  ( w taddr -- )     >image S! ;
 1225: : c@ ( taddr -- char )  >image Sc@ ;
 1226: : c! ( char taddr -- )  >image Sc! ;
 1227: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
 1228: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
 1229: 
 1230: \ Target compilation primitives                        06oct92py
 1231: \ included A!                                          16may93jaw
 1232: 
 1233: : here  ( -- there )    there ;
 1234: : allot ( n -- )        tdp +! ;
 1235: : ,     ( w -- )        T here H tcell T allot  ! H ;
 1236: : c,    ( char -- )     T here H tchar T allot c! H ;
 1237: : align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
 1238: : cfalign ( -- )
 1239:     T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
 1240: 
 1241: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
 1242: : A!                    swap >address swap dup relon T ! H ;
 1243: : A,    ( w -- )        >address T here H relon T , H ;
 1244: 
 1245: >CROSS
 1246: 
 1247: : tcmove ( source dest len -- )
 1248: \G cmove in target memory
 1249:   tchar * bounds
 1250:   ?DO  dup T c@ H I T c! H 1+
 1251:   tchar +LOOP  drop ;
 1252: 
 1253: \ \ Load Assembler
 1254: 
 1255: >TARGET
 1256: H also Forth definitions
 1257: 
 1258: : X 	bl word count [ ' target >wordlist ] Literal search-wordlist
 1259: 	IF	state @ IF compile,
 1260: 		ELSE execute THEN
 1261: 	ELSE	-1 ABORT" Cross: access method not supported!"
 1262: 	THEN ; immediate
 1263: 
 1264: [IFDEF] asm-include asm-include [THEN] hex
 1265: 
 1266: previous
 1267: >CROSS H
 1268: 
 1269: \ \ --------------------        Compiler Plug Ins               01aug97jaw
 1270: 
 1271: \  Compiler States
 1272: 
 1273: Variable comp-state
 1274: 0 Constant interpreting
 1275: 1 Constant compiling
 1276: 2 Constant resolving
 1277: 3 Constant assembling
 1278: 
 1279: Defer lit, ( n -- )
 1280: Defer alit, ( n -- )
 1281: 
 1282: Defer branch, ( target-addr -- )	\ compiles a branch
 1283: Defer ?branch, ( target-addr -- )	\ compiles a ?branch
 1284: Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
 1285: Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
 1286: Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
 1287: Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
 1288: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
 1289: Defer branchfrom, ( -- )		\ ?!
 1290: Defer branchtomark, ( -- target-addr )	\ marks a branch destination
 1291: 
 1292: Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
 1293: Defer colonmark, ( -- addr )		\ marks a colon call
 1294: Defer colon-resolve ( tcfa addr -- )
 1295: 
 1296: Defer addr-resolve ( target-addr addr -- )
 1297: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
 1298: 
 1299: Defer do,	( -- do-token )
 1300: Defer ?do,	( -- ?do-token )
 1301: Defer for,	( -- for-token )
 1302: Defer loop,	( do-token / ?do-token -- )
 1303: Defer +loop,	( do-token / ?do-token -- )
 1304: Defer next,	( for-token )
 1305: 
 1306: [IFUNDEF] ca>native
 1307: defer ca>native	
 1308: [THEN]
 1309: 
 1310: >TARGET
 1311: DEFER >body             \ we need the system >body
 1312: 			\ and the target >body
 1313: >CROSS
 1314: T 2 cells H VALUE xt>body
 1315: DEFER doprim,	\ compiles start of a primitive
 1316: DEFER docol,   	\ compiles start of a colon definition
 1317: DEFER doer,		
 1318: DEFER fini,      \ compiles end of definition ;s
 1319: DEFER doeshandler,
 1320: DEFER dodoes,
 1321: 
 1322: DEFER ]comp     \ starts compilation
 1323: DEFER comp[     \ ends compilation
 1324: 
 1325: : (cc) T a, H ;					' (cc) IS colon,
 1326: 
 1327: : (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
 1328: : (ar) T ! H ;					' (ar) IS addr-resolve
 1329: : (dr)  ( ghost res-pnt target-addr addr )
 1330: 	>tempdp drop over 
 1331: 	dup >magic @ <do:> =
 1332: 	IF 	doer,
 1333: 	ELSE	dodoes,
 1334: 	THEN 
 1335: 	tempdp> ;				' (dr) IS doer-resolve
 1336: 
 1337: : (cm) ( -- addr )
 1338:     T here align H
 1339:     -1 colon, ;					' (cm) IS colonmark,
 1340: 
 1341: >TARGET
 1342: : compile, colon, ;
 1343: >CROSS
 1344: 
 1345: \ resolve structure
 1346: 
 1347: : >next ;		\ link to next field
 1348: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
 1349: : >taddr cell+ cell+ ;	
 1350: : >ghost 3 cells + ;
 1351: : >file 4 cells + ;
 1352: : >line 5 cells + ;
 1353: 
 1354: : (refered) ( ghost addr tag -- )
 1355: \G creates a reference to ghost at address taddr
 1356:     rot >r here r@ >link @ , r> >link ! 
 1357:     ( taddr tag ) ,
 1358:     ( taddr ) , 
 1359:     last-header-ghost @ , 
 1360:     loadfile , 
 1361:     sourceline# , 
 1362:   ;
 1363: 
 1364: : refered ( ghost tag -- )
 1365: \G creates a resolve structure
 1366:     T here aligned H swap (refered)
 1367:   ;
 1368: 
 1369: : killref ( addr ghost -- )
 1370: \G kills a forward reference to ghost at position addr
 1371: \G this is used to eleminate a :dovar refence after making a DOES>
 1372:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
 1373:     swap >r >link
 1374:     BEGIN dup @ dup  ( addr last this )
 1375:     WHILE dup >taddr @ r@ =
 1376:  	 IF   @ over !
 1377: 	 ELSE nip THEN
 1378:     REPEAT rdrop 2drop 
 1379:   ;
 1380: 
 1381: Defer resolve-warning
 1382: 
 1383: : reswarn-test ( ghost res-struct -- ghost res-struct )
 1384:   over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
 1385: 
 1386: : reswarn-forward ( ghost res-struct -- ghost res-struct )
 1387:   over warnhead .ghost dup ."  is referenced in " 
 1388:   >ghost @ .ghost ;
 1389: 
 1390: \ ' reswarn-test IS resolve-warning
 1391:  
 1392: \ resolve                                              14oct92py
 1393: 
 1394:  : resolve-loop ( ghost resolve-list tcfa -- )
 1395:     >r
 1396:     BEGIN dup WHILE 
 1397: \  	  dup >tag @ 2 = IF reswarn-forward THEN
 1398: 	  resolve-warning 
 1399: 	  r@ over >taddr @ 
 1400: 	  2 pick >tag @
 1401: 	  CASE	0 OF colon-resolve ENDOF
 1402: 		1 OF addr-resolve ENDOF
 1403: 		2 OF doer-resolve ENDOF
 1404: 	  ENDCASE
 1405: 	  @ \ next list element
 1406:     REPEAT 2drop rdrop 
 1407:   ;
 1408: 
 1409: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
 1410: \  >r dup >link @
 1411: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
 1412: 
 1413: \ exists                                                9may93jaw
 1414: 
 1415: Variable TWarnings
 1416: TWarnings on
 1417: Variable Exists-Warnings
 1418: Exists-Warnings on
 1419: 
 1420: : exists ( ghost tcfa -- )
 1421:   over GhostNames
 1422:   BEGIN @ dup
 1423:   WHILE 2dup cell+ @ =
 1424:   UNTIL
 1425:         2 cells + count
 1426:         TWarnings @ Exists-Warnings @ and
 1427:         IF warnhead type ."  exists"
 1428:         ELSE 2drop THEN
 1429:         drop swap >link !
 1430:   ELSE  true abort" CROSS: Ghostnames inconsistent "
 1431:   THEN ;
 1432: 
 1433: : resolve  ( ghost tcfa -- )
 1434: \G resolve referencies to ghost with tcfa
 1435:     \ is ghost resolved?, second resolve means another definition with the
 1436:     \ same name
 1437:     over undefined? 0= IF  exists EXIT THEN
 1438:     \ get linked-list
 1439:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
 1440:     \ mark ghost as resolved
 1441:     dup r@ >link ! <res> r@ >magic !
 1442:     \ loop through forward referencies
 1443:     r> -rot 
 1444:     comp-state @ >r Resolving comp-state !
 1445:     resolve-loop 
 1446:     r> comp-state !
 1447: 
 1448:     ['] noop IS resolve-warning 
 1449:   ;
 1450: 
 1451: \ gexecute ghost,                                      01nov92py
 1452: 
 1453: : is-forward   ( ghost -- )
 1454:   colonmark, 0 (refered) ; \ compile space for call
 1455: 
 1456: : is-resolved   ( ghost -- )
 1457:   >link @ colon, ; \ compile-call
 1458: 
 1459: : gexecute   ( ghost -- )
 1460:   dup @ <fwd> = IF  is-forward  ELSE  is-resolved  THEN ;
 1461: 
 1462: : addr,  ( ghost -- )
 1463:   dup @ <fwd> = IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1464: 
 1465: \ !! : ghost,     ghost  gexecute ;
 1466: 
 1467: \ .unresolved                                          11may93jaw
 1468: 
 1469: variable ResolveFlag
 1470: 
 1471: \ ?touched                                             11may93jaw
 1472: 
 1473: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1474:                                0 <> and ;
 1475: 
 1476: : .forwarddefs ( ghost -- )
 1477: 	."  appeared in:"
 1478: 	>link
 1479: 	BEGIN	@ dup
 1480: 	WHILE	cr 5 spaces
 1481: 		dup >ghost @ .ghost
 1482: 		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 1483: 		."  line " dup >line @ .dec
 1484: 	REPEAT 
 1485: 	drop ;
 1486: 
 1487: : ?resolved  ( ghostname -- )
 1488:   dup cell+ @ ?touched
 1489:   IF  	dup 
 1490: 	cell+ cell+ count cr type ResolveFlag on 
 1491: 	cell+ @ .forwarddefs
 1492:   ELSE 	drop 
 1493:   THEN ;
 1494: 
 1495: : .unresolved  ( -- )
 1496:   ResolveFlag off cr ." Unresolved: "
 1497:   Ghostnames
 1498:   BEGIN @ dup
 1499:   WHILE dup ?resolved
 1500:   REPEAT drop ResolveFlag @
 1501:   IF
 1502:       -1 abort" Unresolved words!"
 1503:   ELSE
 1504:       ." Nothing!"
 1505:   THEN
 1506:   cr ;
 1507: 
 1508: : .stats
 1509:   base @ >r decimal
 1510:   cr ." named Headers: " headers-named @ . 
 1511:   r> base ! ;
 1512: 
 1513: >MINIMAL
 1514: 
 1515: : .unresolved .unresolved ;
 1516: 
 1517: >CROSS
 1518: \ Header states                                        12dec92py
 1519: 
 1520: bigendian [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+
 1521: : flag! ( w -- )   tlast @ flag+ + dup >r T c@ xor r> c! H ;
 1522: 
 1523: VARIABLE ^imm
 1524: 
 1525: \ !! should be target wordsize specific
 1526: $80 constant alias-mask
 1527: $40 constant immediate-mask
 1528: $20 constant restrict-mask
 1529: 
 1530: >TARGET
 1531: : immediate     immediate-mask flag!
 1532:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 1533:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 1534:                 <imm> ^imm @ ! ;
 1535: : restrict      restrict-mask flag! ;
 1536: 
 1537: : isdoer	
 1538: \G define a forth word as doer, this makes obviously only sence on
 1539: \G forth processors such as the PSC1000
 1540: 		<do:> last-header-ghost @ >magic ! ;
 1541: >CROSS
 1542: 
 1543: \ Target Header Creation                               01nov92py
 1544: 
 1545: >TARGET
 1546: : string,  ( addr count -- )
 1547:     dup T c, H bounds  ?DO  I c@ T c, H  LOOP ;
 1548: : lstring, ( addr count -- )
 1549:     dup T , H bounds  ?DO  I c@ T c, H  LOOP ;
 1550: : name,  ( "name" -- )  bl word count T lstring, cfalign H ;
 1551: : view,   ( -- ) ( dummy ) ;
 1552: >CROSS
 1553: 
 1554: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
 1555: 
 1556: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
 1557: \ contains the file-id of the documentation file
 1558: 
 1559: : T-\G ( -- )
 1560:     source >in @ /string doc-file-id write-line throw
 1561:     postpone \ ;
 1562: 
 1563: Variable to-doc  to-doc on
 1564: 
 1565: : cross-doc-entry  ( -- )
 1566:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
 1567:     IF
 1568: 	s" " doc-file-id write-line throw
 1569: 	s" make-doc " doc-file-id write-file throw
 1570: 
 1571: 	tlast @ >image count 1F and doc-file-id write-file throw
 1572: 	>in @
 1573: 	[char] ( parse 2drop
 1574: 	[char] ) parse doc-file-id write-file throw
 1575: 	s"  )" doc-file-id write-file throw
 1576: 	[char] \ parse 2drop					
 1577: 	T-\G
 1578: 	>in !
 1579:     THEN ;
 1580: 
 1581: \ Target TAGS creation
 1582: 
 1583: s" kernel.TAGS" r/w create-file throw value tag-file-id
 1584: \ contains the file-id of the tags file
 1585: 
 1586: Create tag-beg 2 c,  7F c, bl c,
 1587: Create tag-end 2 c,  bl c, 01 c,
 1588: Create tag-bof 1 c,  0C c,
 1589: 
 1590: 2variable last-loadfilename 0 0 last-loadfilename 2!
 1591: 	    
 1592: : put-load-file-name ( -- )
 1593:     sourcefilename last-loadfilename 2@ d<>
 1594:     IF
 1595: 	tag-bof count tag-file-id write-line throw
 1596: 	sourcefilename 2dup
 1597: 	tag-file-id write-file throw
 1598: 	last-loadfilename 2!
 1599: 	s" ,0" tag-file-id write-line throw
 1600:     THEN ;
 1601: 
 1602: : cross-tag-entry  ( -- )
 1603:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 1604:     IF
 1605: 	put-load-file-name
 1606: 	source >in @ min tag-file-id write-file throw
 1607: 	tag-beg count tag-file-id write-file throw
 1608: 	tlast @ >image count 1F and tag-file-id write-file throw
 1609: 	tag-end count tag-file-id write-file throw
 1610: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
 1611: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
 1612: 	s" ,0" tag-file-id write-line throw
 1613: 	base !
 1614:     THEN ;
 1615: 
 1616: \ Check for words
 1617: 
 1618: Defer skip? ' false IS skip?
 1619: 
 1620: : skipdef ( <name> -- )
 1621: \G skip definition of an undefined word in undef-words and
 1622: \G all-words mode
 1623:     ghost dup forward?
 1624:     IF  >magic <skip> swap !
 1625:     ELSE drop THEN ;
 1626: 
 1627: : tdefined? ( -- flag ) \ name
 1628:     ghost undefined? 0= ;
 1629: 
 1630: : defined2? ( -- flag ) \ name
 1631: \G return true for anything else than forward, even for <skip>
 1632: \G that's what we want
 1633:     ghost forward? 0= ;
 1634: 
 1635: : forced? ( -- flag ) \ name
 1636: \G return ture if it is a foreced skip with defskip
 1637:     ghost >magic @ <skip> = ;
 1638: 
 1639: : needed? ( -- flag ) \ name
 1640: \G returns a false flag when
 1641: \G a word is not defined
 1642: \G a forward reference exists
 1643: \G so the definition is not skipped!
 1644:     bl word gfind
 1645:     IF dup undefined?
 1646: 	nip
 1647: 	0=
 1648:     ELSE  drop true  THEN ;
 1649: 
 1650: : doer? ( -- flag ) \ name
 1651:     ghost >magic @ <do:> = ;
 1652: 
 1653: : skip-defs ( -- )
 1654:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
 1655: 
 1656: \ Target header creation
 1657: 
 1658: Variable NoHeaderFlag
 1659: NoHeaderFlag off
 1660: 
 1661: : 0.r ( n1 n2 -- ) 
 1662:     base @ >r hex 
 1663:     0 swap <# 0 ?DO # LOOP #> type 
 1664:     r> base ! ;
 1665: 
 1666: : .sym ( adr len -- )
 1667: \G escapes / and \ to produce sed output
 1668:   bounds 
 1669:   DO I c@ dup
 1670: 	CASE	[char] / OF drop ." \/" ENDOF
 1671: 		[char] \ OF drop ." \\" ENDOF
 1672: 		dup OF emit ENDOF
 1673: 	ENDCASE
 1674:     LOOP ;
 1675: 
 1676: : (Theader ( "name" -- ghost )
 1677:     \  >in @ bl word count type 2 spaces >in !
 1678:     \ wordheaders will always be compiled to rom
 1679:     switchrom
 1680:     \ build header in target
 1681:     NoHeaderFlag @
 1682:     IF  NoHeaderFlag off
 1683:     ELSE
 1684: 	T align H view,
 1685: 	tlast @ dup 0> IF tcell - THEN T A, H  there tlast !
 1686: 	1 headers-named +!	\ Statistic
 1687: 	>in @ T name, H >in !
 1688:     THEN
 1689:     T cfalign here H tlastcfa !
 1690:     \ Old Symbol table sed-script
 1691: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
 1692:     ghost
 1693:     \ output symbol table to extra file
 1694:     [ [IFDEF] fd-symbol-table ]
 1695:       base @ hex there s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base !
 1696:       s" :" fd-symbol-table write-file throw
 1697:       dup >ghostname fd-symbol-table write-line throw
 1698:     [ [THEN] ]
 1699:     dup Last-Header-Ghost !
 1700:     dup >magic ^imm !     \ a pointer for immediate
 1701:     Already @
 1702:     IF  dup >end tdoes !
 1703:     ELSE 0 tdoes !
 1704:     THEN
 1705:     alias-mask flag!
 1706:     cross-doc-entry cross-tag-entry ;
 1707: 
 1708: VARIABLE ;Resolve 1 cells allot
 1709: \ this is the resolver information from ":"
 1710: \ resolving is done by ";"
 1711: 
 1712: : Theader  ( "name" -- ghost )
 1713:   (THeader dup there resolve 0 ;Resolve ! ;
 1714: 
 1715: >TARGET
 1716: : Alias    ( cfa -- ) \ name
 1717:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1718:     dup 0< s" prims" T $has? H 0= and
 1719:     IF
 1720: 	.sourcepos ." needs prim: " >in @ bl word count type >in ! cr
 1721:     THEN
 1722:     (THeader over resolve T A, H alias-mask flag! ;
 1723: : Alias:   ( cfa -- ) \ name
 1724:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1725:     dup 0< s" prims" T $has? H 0= and
 1726:     IF
 1727: 	.sourcepos ." needs doer: " >in @ bl word count type >in ! cr
 1728:     THEN
 1729:     ghost tuck swap resolve <do:> swap >magic ! ;
 1730: 
 1731: Variable prim#
 1732: : first-primitive ( n -- )  prim# ! ;
 1733: : Primitive  ( -- ) \ name
 1734:     prim# @ T Alias H  -1 prim# +! ;
 1735: >CROSS
 1736: 
 1737: \ Conditionals and Comments                            11may93jaw
 1738: 
 1739: : ;Cond
 1740:   postpone ;
 1741:   swap ! ;  immediate
 1742: 
 1743: : Cond: ( -- ) \ name {code } ;
 1744:   atonce on
 1745:   ghost
 1746:   >exec
 1747:   :NONAME ;
 1748: 
 1749: : restrict? ( -- )
 1750: \ aborts on interprete state - ae
 1751:   state @ 0= ABORT" CROSS: Restricted" ;
 1752: 
 1753: : Comment ( -- )
 1754:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
 1755: 
 1756: Comment (       Comment \
 1757: 
 1758: \ compile                                              10may93jaw
 1759: 
 1760: : compile  ( -- ) \ name
 1761:   restrict?
 1762:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
 1763:   0> ( immediate? )
 1764:   IF    >exec @ compile,
 1765:   ELSE  postpone literal postpone gexecute  THEN ;
 1766:                                         immediate
 1767: 
 1768: : [G'] 
 1769: \G ticks a ghost and returns its address
 1770:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1771:   state @
 1772:   IF   postpone literal
 1773:   THEN ; immediate
 1774: 
 1775: : ghost>cfa
 1776:   dup undefined? ABORT" CROSS: forward " >link @ ;
 1777:                
 1778: >TARGET
 1779: 
 1780: : '  ( -- cfa ) 
 1781: \ returns the target-cfa of a ghost
 1782:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1783:   ghost>cfa ;
 1784: 
 1785: Cond: [']  T ' H alit, ;Cond
 1786: 
 1787: >CROSS
 1788: 
 1789: : [T']
 1790: \ returns the target-cfa of a ghost, or compiles it as literal
 1791:   postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
 1792: 
 1793: \ \ threading modell					13dec92py
 1794: \ modularized						14jun97jaw
 1795: 
 1796: : fillcfa   ( usedcells -- )
 1797:   T cells H xt>body swap - 0 ?DO 0 X c, tchar +LOOP ;
 1798: 
 1799: : (>body)   ( cfa -- pfa ) xt>body + ;		' (>body) T IS >body H
 1800: 
 1801: : (doer,)   ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ;   ' (doer,) IS doer,
 1802: 
 1803: : (docol,)  ( -- ) [G'] :docol doer, ;		' (docol,) IS docol,
 1804: 
 1805: : (doprim,) ( -- )
 1806:   there xt>body + ca>native T a, H 1 fillcfa ;	' (doprim,) IS doprim,
 1807: 
 1808: : (doeshandler,) ( -- ) 
 1809:   T cfalign H compile :doesjump T 0 , H ; 	' (doeshandler,) IS doeshandler,
 1810: 
 1811: : (dodoes,) ( does-action-ghost -- )
 1812:   ]comp [G'] :dodoes gexecute comp[
 1813:   addr,
 1814:   T here H tcell - reloff 2 fillcfa ;		' (dodoes,) IS dodoes,
 1815: 
 1816: : (lit,) ( n -- )   compile lit T  ,  H ;	' (lit,) IS lit,
 1817: 
 1818: \ if we dont produce relocatable code alit, defaults to lit, jaw
 1819: \ this is just for convenience, so we don't have to define alit,
 1820: \ seperately for embedded systems....
 1821: T has? relocate H
 1822: [IF]
 1823: : (alit,) ( n -- )  compile lit T  a, H ;	' (alit,) IS alit,
 1824: [ELSE]
 1825: : (alit,) ( n -- )  lit, ;			' (alit,) IS alit,
 1826: [THEN]
 1827: 
 1828: : (fini,)         compile ;s ;                ' (fini,) IS fini,
 1829: 
 1830: [IFUNDEF] (code) 
 1831: Defer (code)
 1832: Defer (end-code)
 1833: [THEN]
 1834: 
 1835: >TARGET
 1836: : Code
 1837:   defempty?
 1838:   (THeader there resolve
 1839:   [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
 1840:   doprim, 
 1841:   [THEN]
 1842:   depth (code) ;
 1843: 
 1844: : Code:
 1845:   defempty?
 1846:     ghost dup there ca>native resolve  <do:> swap >magic !
 1847:     depth (code) ;
 1848: 
 1849: : end-code
 1850:     (end-code)
 1851:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1852:     ELSE true ABORT" CROSS: Stack empty" THEN
 1853:     ;
 1854: 
 1855: >CROSS
 1856: 
 1857: \ tLiteral                                             12dec92py
 1858: 
 1859: >TARGET
 1860: Cond: \G  T-\G ;Cond
 1861: 
 1862: Cond:  Literal ( n -- )   restrict? lit, ;Cond
 1863: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
 1864: 
 1865: : Char ( "<char>" -- )  bl word char+ c@ ;
 1866: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
 1867: 
 1868: \ some special literals					27jan97jaw
 1869: 
 1870: \ !! Known Bug: Special Literals and plug-ins work only correct
 1871: \ on 16 and 32 Bit Targets and 32 Bit Hosts!
 1872: 
 1873: Cond: MAXU
 1874:   restrict? 
 1875:   tcell 1 cells u> 
 1876:   IF	compile lit tcell 0 ?DO FF T c, H LOOP 
 1877:   ELSE	ffffffff lit, THEN
 1878:   ;Cond
 1879: 
 1880: Cond: MINI
 1881:   restrict?
 1882:   tcell 1 cells u>
 1883:   IF	compile lit bigendian 
 1884:   	IF	80 T c, H tcell 1 ?DO 0 T c, H LOOP 
 1885:   	ELSE  	tcell 1 ?DO 0 T c, H LOOP 80 T c, H
 1886:   	THEN
 1887:   ELSE	tcell 2 = IF 8000 ELSE 80000000 THEN lit, THEN
 1888:   ;Cond
 1889:  
 1890: Cond: MAXI
 1891:  restrict?
 1892:  tcell 1 cells u>
 1893:  IF	compile lit bigendian 
 1894: 	IF 	7F T c, H tcell 1 ?DO FF T c, H LOOP
 1895: 	ELSE 	tcell 1 ?DO FF T c, H LOOP 7F T c, H
 1896:  	THEN
 1897:  ELSE	tcell 2 = IF 7fff ELSE 7fffffff THEN lit, THEN
 1898:  ;Cond
 1899: 
 1900: >CROSS
 1901: \ Target compiling loop                                12dec92py
 1902: \ ">tib trick thrown out                               10may93jaw
 1903: \ number? defined at the top                           11may93jaw
 1904: \ replaced >in by save-input				
 1905: 
 1906: : discard 0 ?DO drop LOOP ;
 1907: 
 1908: \ compiled word might leave items on stack!
 1909: : tcom ( x1 .. xn n name -- )
 1910: \  dup count type space
 1911:   gfind  ?dup
 1912:   IF    >r >r discard r> r>
 1913: 	0> IF	>exec @ execute
 1914: 	ELSE	gexecute  THEN 
 1915: 	EXIT 
 1916:   THEN
 1917:   number? dup  
 1918:   IF	0> IF swap lit,  THEN  lit, discard
 1919:   ELSE	2drop restore-input throw ghost gexecute THEN  ;
 1920: 
 1921: >TARGET
 1922: \ : ; DOES>                                            13dec92py
 1923: \ ]                                                     9may93py/jaw
 1924: 
 1925: : ] state on
 1926:     Compiling comp-state !
 1927:     BEGIN
 1928:         BEGIN save-input bl word
 1929:               dup c@ 0= WHILE drop discard refill 0=
 1930:               ABORT" CROSS: End of file while target compiling"
 1931:         REPEAT
 1932:         tcom
 1933:         state @
 1934:         0=
 1935:     UNTIL ;
 1936: 
 1937: \ by the way: defining a second interpreter (a compiler-)loop
 1938: \             is not allowed if a system should be ans conform
 1939: 
 1940: : : ( -- colon-sys ) \ Name
 1941:   defempty?
 1942:   constflag off \ don't let this flag work over colon defs
 1943: 		\ just to go sure nothing unwanted happens
 1944:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
 1945:   (THeader ;Resolve ! there ;Resolve cell+ !
 1946:   docol, ]comp depth T ] H ;
 1947: 
 1948: : :noname ( -- colon-sys )
 1949:   T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
 1950: 
 1951: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
 1952: 
 1953: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
 1954: 
 1955: >CROSS
 1956: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
 1957:          ;Resolve cell+ @ ;
 1958: 
 1959: >TARGET
 1960: 
 1961: Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
 1962: 
 1963: Cond: ; ( -- ) restrict?
 1964:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1965:                           ELSE true ABORT" CROSS: Stack empty" THEN
 1966:                fini,
 1967:                comp[
 1968:                state off
 1969:                ;Resolve @
 1970:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
 1971: 		Interpreting comp-state !
 1972:                ;Cond
 1973: Cond: [  restrict? state off Interpreting comp-state ! ;Cond
 1974: 
 1975: >CROSS
 1976: 
 1977: Create GhostDummy ghostheader
 1978: <res> GhostDummy >magic !
 1979: 
 1980: : !does ( does-action -- )
 1981: \ !! zusammenziehen und dodoes, machen!
 1982:     tlastcfa @ [G'] :dovar killref
 1983: \    tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
 1984: \ !! geht so nicht, da dodoes, ghost will!
 1985:     GhostDummy >link ! GhostDummy 
 1986:     tlastcfa @ >tempdp dodoes, tempdp> ;
 1987: 
 1988: >TARGET
 1989: Cond: DOES> restrict?
 1990:         compile (does>) doeshandler, 
 1991: 	\ resolve words made by builders
 1992: 	tdoes @ ?dup IF  @ T here H resolve THEN
 1993:         ;Cond
 1994: : DOES> switchrom doeshandler, T here H !does depth T ] H ;
 1995: 
 1996: >CROSS
 1997: \ Creation                                             01nov92py
 1998: 
 1999: \ Builder                                               11may93jaw
 2000: 
 2001: : Builder    ( Create-xt do:-xt "name" -- )
 2002: \ builds up a builder in current vocabulary
 2003: \ create-xt is executed when word is interpreted
 2004: \ do:-xt is executet when the created word from builder is executed
 2005: \ for do:-xt an additional entry after the normal ghost-enrys is used
 2006: 
 2007:   Make-Ghost 		( Create-xt do:-xt ghost )
 2008:   rot swap		( do:-xt Create-xt ghost )
 2009:   >exec ! , ;
 2010: \  rot swap >exec dup @ ['] NoExec <>
 2011: \  IF 2drop ELSE ! THEN , ;
 2012: 
 2013: : gdoes,  ( ghost -- )
 2014: \ makes the codefield for a word that is built
 2015:   >end @ dup undefined? 0=
 2016:   IF
 2017: 	dup >magic @ <do:> =
 2018: 	IF 	 doer, 
 2019: 	ELSE	dodoes,
 2020: 	THEN
 2021: 	EXIT
 2022:   THEN
 2023: \  compile :dodoes gexecute
 2024: \  T here H tcell - reloff 
 2025:   2 refered 
 2026:   0 fillcfa
 2027:   ;
 2028: 
 2029: : TCreate ( <name> -- )
 2030:   executed-ghost @
 2031:   create-forward-warn
 2032:   IF ['] reswarn-forward IS resolve-warning THEN
 2033:   Theader >r dup gdoes,
 2034: \ stores execution semantic in the built word
 2035: \ if the word already has a semantic (concerns S", IS, .", DOES>)
 2036: \ then keep it
 2037:   >end @ >exec @ r> >exec dup @ ['] NoExec =
 2038:   IF ! ELSE 2drop THEN ;
 2039: 
 2040: : RTCreate ( <name> -- )
 2041: \ creates a new word with code-field in ram
 2042:   executed-ghost @
 2043:   create-forward-warn
 2044:   IF ['] reswarn-forward IS resolve-warning THEN
 2045:   \ make Alias
 2046:   (THeader there 0 T a, H alias-mask flag! ( S executed-ghost new-ghost )
 2047:   \ store  poiter to code-field
 2048:   switchram T cfalign H
 2049:   there swap T ! H
 2050:   there tlastcfa ! 
 2051:   dup there resolve 0 ;Resolve !
 2052:   >r dup gdoes,
 2053: \ stores execution semantic in the built word
 2054: \ if the word already has a semantic (concerns S", IS, .", DOES>)
 2055: \ then keep it
 2056:   >end @ >exec @ r> >exec dup @ ['] NoExec =
 2057:   IF ! ELSE 2drop THEN ;
 2058: 
 2059: : Build:  ( -- [xt] [colon-sys] )
 2060:   :noname postpone TCreate ;
 2061: 
 2062: : BuildSmart:  ( -- [xt] [colon-sys] )
 2063:   :noname
 2064:   [ T has? rom H [IF] ]
 2065:   postpone RTCreate
 2066:   [ [ELSE] ]
 2067:   postpone TCreate 
 2068:   [ [THEN] ] ;
 2069: 
 2070: : gdoes>  ( ghost -- addr flag )
 2071:   executed-ghost @
 2072:   state @ IF  gexecute true EXIT  THEN
 2073:   >link @ T >body H false ;
 2074: 
 2075: \ DO: ;DO                                               11may93jaw
 2076: \ changed to ?EXIT                                      10may93jaw
 2077: 
 2078: : DO:     ( -- addr [xt] [colon-sys] )
 2079:   here ghostheader
 2080:   :noname postpone gdoes> postpone ?EXIT ;
 2081: 
 2082: : by:     ( -- addr [xt] [colon-sys] ) \ name
 2083:   ghost
 2084:   :noname postpone gdoes> postpone ?EXIT ;
 2085: 
 2086: : ;DO ( addr [xt] [colon-sys] -- addr )
 2087:   postpone ;    ( S addr xt )
 2088:   over >exec ! ; immediate
 2089: 
 2090: : by      ( -- addr ) \ Name
 2091:   ghost >end @ ;
 2092: 
 2093: >TARGET
 2094: \ Variables and Constants                              05dec92py
 2095: 
 2096: Build:  ( n -- ) ;
 2097: by: :docon ( ghost -- n ) T @ H ;DO
 2098: Builder (Constant)
 2099: 
 2100: Build:  ( n -- ) T , H ;
 2101: by (Constant)
 2102: Builder Constant
 2103: 
 2104: Build:  ( n -- ) T A, H ;
 2105: by (Constant)
 2106: Builder AConstant
 2107: 
 2108: Build:  ( d -- ) T , , H ;
 2109: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
 2110: Builder 2Constant
 2111: 
 2112: BuildSmart: ;
 2113: by: :dovar ( ghost -- addr ) ;DO
 2114: Builder Create
 2115: 
 2116: T has? rom H [IF]
 2117: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 2118: by (Constant)
 2119: Builder Variable
 2120: [ELSE]
 2121: Build: T 0 , H ;
 2122: by Create
 2123: Builder Variable
 2124: [THEN]
 2125: 
 2126: T has? rom H [IF]
 2127: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
 2128: by (Constant)
 2129: Builder 2Variable
 2130: [ELSE]
 2131: Build: T 0 , 0 , H ;
 2132: by Create
 2133: Builder 2Variable
 2134: [THEN]
 2135: 
 2136: T has? rom H [IF]
 2137: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 2138: by (Constant)
 2139: Builder AVariable
 2140: [ELSE]
 2141: Build: T 0 A, H ;
 2142: by Create
 2143: Builder AVariable
 2144: [THEN]
 2145: 
 2146: \ User variables                                       04may94py
 2147: 
 2148: >CROSS
 2149: 
 2150: Variable tup  0 tup !
 2151: Variable tudp 0 tudp !
 2152: 
 2153: : u,  ( n -- udp )
 2154:   tup @ tudp @ + T  ! H
 2155:   tudp @ dup T cell+ H tudp ! ;
 2156: 
 2157: : au, ( n -- udp )
 2158:   tup @ tudp @ + T A! H
 2159:   tudp @ dup T cell+ H tudp ! ;
 2160: 
 2161: >TARGET
 2162: 
 2163: Build: 0 u, X , ;
 2164: by: :douser ( ghost -- up-addr )  X @ tup @ + ;DO
 2165: Builder User
 2166: 
 2167: Build: 0 u, X , 0 u, drop ;
 2168: by User
 2169: Builder 2User
 2170: 
 2171: Build: 0 au, X , ;
 2172: by User
 2173: Builder AUser
 2174: 
 2175: BuildSmart: T , H ;
 2176: by (Constant)
 2177: Builder Value
 2178: 
 2179: BuildSmart: T A, H ;
 2180: by (Constant)
 2181: Builder AValue
 2182: 
 2183: BuildSmart:  ( -- ) [T'] noop T A, H ;
 2184: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2185: Builder Defer
 2186: 
 2187: Build: ( inter comp -- ) swap T immediate A, A, H ;
 2188: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2189: Builder interpret/compile:
 2190: 
 2191: \ Sturctures                                           23feb95py
 2192: 
 2193: >CROSS
 2194: : nalign ( addr1 n -- addr2 )
 2195: \ addr2 is the aligned version of addr1 wrt the alignment size n
 2196:  1- tuck +  swap invert and ;
 2197: >TARGET
 2198: 
 2199: Build: ;
 2200: by: :dofield T @ H + ;DO
 2201: Builder (Field)
 2202: 
 2203: Build: ( align1 offset1 align size "name" --  align2 offset2 )
 2204:     rot dup T , H ( align1 align size offset1 )
 2205:     + >r nalign r> ;
 2206: by (Field)
 2207: Builder Field
 2208: 
 2209: : struct  T 1 chars 0 H ;
 2210: : end-struct  T 2Constant H ;
 2211: 
 2212: : cell% ( n -- size align )
 2213:     T 1 cells H dup ;
 2214: 
 2215: Build: ( m v -- m' v )  dup T , cell+ H ;
 2216: DO:  abort" Not in cross mode" ;DO
 2217: Builder input-method
 2218: 
 2219: Build: ( m v size -- m v' )  over T , H + ;
 2220: DO:  abort" Not in cross mode" ;DO
 2221: Builder input-var
 2222: 
 2223: \ structural conditionals                              17dec92py
 2224: 
 2225: >CROSS
 2226: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
 2227: : sys?        ( sys -- sys )    dup 0= ?struc ;
 2228: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
 2229: 
 2230: : branchoffset ( src dest -- )  - tchar / ; \ ?? jaw
 2231: 
 2232: : >resolve    ( sys -- )        
 2233: 	X here ( dup ." >" hex. ) over branchoffset swap X ! ;
 2234: 
 2235: : <resolve    ( sys -- )
 2236: 	X here ( dup ." <" hex. ) branchoffset X , ;
 2237: 
 2238: :noname compile branch X here branchoffset X , ;
 2239:   IS branch, ( target-addr -- )
 2240: :noname compile ?branch X here branchoffset X , ;
 2241:   IS ?branch, ( target-addr -- )
 2242: :noname compile branch T here 0 , H ;
 2243:   IS branchmark, ( -- branchtoken )
 2244: :noname compile ?branch T here 0 , H ;
 2245:   IS ?branchmark, ( -- branchtoken )
 2246: :noname T here 0 , H ;
 2247:   IS ?domark, ( -- branchtoken )
 2248: :noname dup X @ ?struc X here over branchoffset swap X ! ;
 2249:   IS branchtoresolve, ( branchtoken -- )
 2250: :noname branchto, X here ;
 2251:   IS branchtomark, ( -- target-addr )
 2252: 
 2253: >TARGET
 2254: 
 2255: \ Structural Conditionals                              12dec92py
 2256: 
 2257: Cond: BUT       restrict? sys? swap ;Cond
 2258: Cond: YET       restrict? sys? dup ;Cond
 2259: 
 2260: >CROSS
 2261: 
 2262: Variable tleavings 0 tleavings !
 2263: 
 2264: : (done) ( addr -- )
 2265:     tleavings @
 2266:     BEGIN  dup
 2267:     WHILE
 2268: 	>r dup r@ cell+ @ \ address of branch
 2269: 	u> 0=	   \ lower than DO?	
 2270:     WHILE
 2271: 	r@ 2 cells + @ \ branch token
 2272: 	branchtoresolve,
 2273: 	r@ @ r> free throw
 2274:     REPEAT  r>  THEN
 2275:     tleavings ! drop ;
 2276: 
 2277: >TARGET
 2278: 
 2279: Cond: DONE   ( addr -- )  restrict? (done) ;Cond
 2280: 
 2281: >CROSS
 2282: : (leave) ( branchtoken -- )
 2283:     3 cells allocate throw >r
 2284:     T here H r@ cell+ !
 2285:     r@ 2 cells + !
 2286:     tleavings @ r@ !
 2287:     r> tleavings ! ;
 2288: >TARGET
 2289: 
 2290: Cond: LEAVE     restrict? branchmark, (leave) ;Cond
 2291: Cond: ?LEAVE    restrict? compile 0=  ?branchmark, (leave)  ;Cond
 2292: 
 2293: >CROSS
 2294: \ !!JW ToDo : Move to general tools section
 2295: 
 2296: : to1 ( x1 x2 xn n -- addr )
 2297: \G packs n stack elements in a allocated memory region
 2298:    dup dup 1+ cells allocate throw dup >r swap 1+
 2299:    0 DO tuck ! cell+ LOOP
 2300:    drop r> ;
 2301: : 1to ( addr -- x1 x2 xn )
 2302: \G unpacks the elements saved by to1
 2303:     dup @ swap over cells + swap
 2304:     0 DO  dup @ swap 1 cells -  LOOP
 2305:     free throw ;
 2306: 
 2307: : loop]     branchto, dup <resolve tcell - (done) ;
 2308: 
 2309: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
 2310: 
 2311: >TARGET
 2312: 
 2313: \ Structural Conditionals                              12dec92py
 2314: 
 2315: >TARGET
 2316: Cond: AHEAD     restrict? branchmark, ;Cond
 2317: Cond: IF        restrict? ?branchmark, ;Cond
 2318: Cond: THEN      restrict? sys? branchto, branchtoresolve, ;Cond
 2319: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
 2320: 
 2321: Cond: BEGIN     restrict? branchtomark, ;Cond
 2322: Cond: WHILE     restrict? sys? compile IF swap ;Cond
 2323: Cond: AGAIN     restrict? sys? branch, ;Cond
 2324: Cond: UNTIL     restrict? sys? ?branch, ;Cond
 2325: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
 2326: 
 2327: Cond: CASE      restrict? 0 ;Cond
 2328: Cond: OF        restrict? 1+ >r compile over compile =
 2329:                 compile IF compile drop r> ;Cond
 2330: Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
 2331: Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond
 2332: 
 2333: \ Structural Conditionals                              12dec92py
 2334: 
 2335: :noname \ ?? i think 0 is too much! jaw
 2336:     0 compile (do)
 2337:     branchtomark,  2 to1 ;
 2338:   IS do, ( -- target-addr )
 2339: 
 2340: \ :noname
 2341: \     compile 2dup compile = compile IF
 2342: \     compile 2drop compile ELSE
 2343: \     compile (do) branchtomark, 2 to1 ;
 2344: \   IS ?do,
 2345:     
 2346: :noname
 2347:     0 compile (?do)  ?domark, (leave)
 2348:     branchtomark,  2 to1 ;
 2349:   IS ?do, ( -- target-addr )
 2350: :noname compile (for) branchtomark, ;
 2351:   IS for, ( -- target-addr )
 2352: :noname 1to compile (loop)  loop] compile unloop skiploop] ;
 2353:   IS loop, ( target-addr -- )
 2354: :noname 1to compile (+loop)  loop] compile unloop skiploop] ;
 2355:   IS +loop, ( target-addr -- )
 2356: :noname compile (next)  loop] compile unloop ;
 2357:   IS next, ( target-addr -- )
 2358: 
 2359: Cond: DO      	restrict? do, ;Cond
 2360: Cond: ?DO     	restrict? ?do, ;Cond
 2361: Cond: FOR	restrict? for, ;Cond
 2362: 
 2363: Cond: LOOP	restrict? sys? loop, ;Cond
 2364: Cond: +LOOP	restrict? sys? +loop, ;Cond
 2365: Cond: NEXT	restrict? sys? next, ;Cond
 2366: 
 2367: \ String words                                         23feb93py
 2368: 
 2369: : ,"            [char] " parse T string, align H ;
 2370: 
 2371: Cond: ."        restrict? compile (.")     T ," H ;Cond
 2372: Cond: S"        restrict? compile (S")     T ," H ;Cond
 2373: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
 2374: 
 2375: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
 2376: : IS            T >address ' >body ! H ;
 2377: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
 2378: : TO            T ' >body ! H ;
 2379: 
 2380: Cond: defers	T ' >body @ compile, H ;Cond
 2381: : on		T -1 swap ! H ; 
 2382: : off   	T 0 swap ! H ;
 2383: 
 2384: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
 2385: 
 2386: \ linked list primitive
 2387: : linked        X here over X @ X A, swap X ! ;
 2388: : chained	T linked A, H ;
 2389: 
 2390: : err"   s" ErrLink linked" evaluate T , H
 2391:          [char] " parse T string, align H ;
 2392: 
 2393: : env"  [char] " parse s" EnvLink linked" evaluate
 2394:         T string, align , H ;
 2395: 
 2396: : 2env" [char] " parse s" EnvLink linked" evaluate
 2397:         here >r T string, align , , H
 2398:         r> dup T c@ H 80 and swap T c! H ;
 2399: 
 2400: \ compile must be last                                 22feb93py
 2401: 
 2402: Cond: compile ( -- ) restrict? \ name
 2403:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2404:       0> IF    gexecute
 2405:          ELSE  dup >magic @ <imm> =
 2406:                IF   gexecute
 2407:                ELSE compile (compile) addr, THEN THEN ;Cond
 2408: 
 2409: Cond: postpone ( -- ) restrict? \ name
 2410:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2411:       0> IF    gexecute
 2412:          ELSE  dup >magic @ <imm> =
 2413:                IF   gexecute
 2414: 	       ELSE compile (compile) addr, THEN THEN ;Cond
 2415: 	   
 2416: \ save-cross                                           17mar93py
 2417: 
 2418: hex
 2419: 
 2420: >CROSS
 2421: Create magic  s" Gforth2x" here over allot swap move
 2422: 
 2423: bigendian 1+ \ strangely, in magic big=0, little=1
 2424: tcell 1 = 0 and or
 2425: tcell 2 = 2 and or
 2426: tcell 4 = 4 and or
 2427: tcell 8 = 6 and or
 2428: tchar 1 = 00 and or
 2429: tchar 2 = 28 and or
 2430: tchar 4 = 50 and or
 2431: tchar 8 = 78 and or
 2432: magic 7 + c!
 2433: 
 2434: : save-cross ( "image-name" "binary-name" -- )
 2435:   bl parse ." Saving to " 2dup type cr
 2436:   w/o bin create-file throw >r
 2437:   TNIL IF
 2438:       s" #! "           r@ write-file throw
 2439:       bl parse          r@ write-file throw
 2440:       s"  --image-file" r@ write-file throw
 2441:       #lf       r@ emit-file throw
 2442:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
 2443:       ?do
 2444: 	  bl over emit-file throw
 2445:       loop
 2446:       drop
 2447:       magic 8       r@ write-file throw \ write magic
 2448:   ELSE
 2449:       bl parse 2drop
 2450:   THEN
 2451:   image @ there 
 2452:   r@ write-file throw \ write image
 2453:   TNIL IF
 2454:       bit$  @ there 1- tcell>bit rshift 1+
 2455:                 r@ write-file throw \ write tags
 2456:   THEN
 2457:   r> close-file throw ;
 2458: 
 2459: : save-region ( addr len -- )
 2460:   bl parse w/o bin create-file throw >r
 2461:   swap >image swap r@ write-file throw
 2462:   r> close-file throw ;
 2463: 
 2464: \ \ minimal definitions
 2465: 	   
 2466: >MINIMAL also minimal
 2467: 
 2468: \ Usefull words                                        13feb93py
 2469: 
 2470: : KB  400 * ;
 2471: 
 2472: \ \ [IF] [ELSE] [THEN] ...				14sep97jaw
 2473: 
 2474: \ it is useful to define our own structures and not to rely
 2475: \ on the words in the compiler
 2476: \ The words in the compiler might be defined with vocabularies
 2477: \ this doesn't work with our self-made compile-loop
 2478: 
 2479: Create parsed 20 chars allot	\ store word we parsed
 2480: 
 2481: : upcase
 2482:     parsed count bounds
 2483:     ?DO I c@ toupper I c! LOOP ;
 2484: 
 2485: : [ELSE]
 2486:     1 BEGIN
 2487: 	BEGIN bl word count dup WHILE
 2488: 	    comment? 20 umin parsed place upcase parsed count
 2489: 	    2dup s" [IF]" compare 0= >r 
 2490: 	    2dup s" [IFUNDEF]" compare 0= >r
 2491: 	    2dup s" [IFDEF]" compare 0= r> or r> or
 2492: 	    IF   2drop 1+
 2493: 	    ELSE 2dup s" [ELSE]" compare 0=
 2494: 		IF   2drop 1- dup
 2495: 		    IF 1+
 2496: 		    THEN
 2497: 		ELSE
 2498: 		    2dup s" [ENDIF]" compare 0= >r
 2499: 		    s" [THEN]" compare 0= r> or
 2500: 		    IF 1- THEN
 2501: 		THEN
 2502: 	    THEN
 2503: 	    ?dup 0= ?EXIT
 2504: 	REPEAT
 2505: 	2drop refill 0=
 2506:     UNTIL drop ; immediate
 2507:   
 2508: : [THEN] ( -- ) ; immediate
 2509: 
 2510: : [ENDIF] ( -- ) ; immediate
 2511: 
 2512: : [IF] ( flag -- )
 2513:     0= IF postpone [ELSE] THEN ; immediate 
 2514: 
 2515: Cond: [IF]      postpone [IF] ;Cond
 2516: Cond: [THEN]    postpone [THEN] ;Cond
 2517: Cond: [ELSE]    postpone [ELSE] ;Cond
 2518: 
 2519: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
 2520: 
 2521: : defined? tdefined? ;
 2522: : needed? needed? ;
 2523: : doer? doer? ;
 2524: 
 2525: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
 2526: 
 2527: : directive? 
 2528:   bl word count [ ' target >wordlist ] literal search-wordlist 
 2529:   dup IF nip THEN ;
 2530: 
 2531: : [IFDEF]  >in @ directive? swap >in !
 2532: 	   0= IF tdefined? ELSE name 2drop true THEN
 2533: 	   postpone [IF] ;
 2534: 
 2535: : [IFUNDEF] tdefined? 0= postpone [IF] ;
 2536: 
 2537: Cond: [IFDEF]   postpone [IFDEF] ;Cond
 2538: 
 2539: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
 2540: 
 2541: \ C: \- \+ Conditional Compiling                         09jun93jaw
 2542: 
 2543: : C: >in @ tdefined? 0=
 2544:      IF    >in ! X :
 2545:      ELSE drop
 2546:         BEGIN bl word dup c@
 2547:               IF   count comment? s" ;" compare 0= ?EXIT
 2548:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
 2549:               THEN
 2550:         AGAIN
 2551:      THEN ;
 2552: 
 2553: : d? d? ;
 2554: 
 2555: \G doesn't skip line when debug switch is on
 2556: : \D D? 0= IF postpone \ THEN ;
 2557: 
 2558: \G interprets the line if word is not defined
 2559: : \- tdefined? IF postpone \ THEN ;
 2560: 
 2561: \G interprets the line if word is defined
 2562: : \+ tdefined? 0= IF postpone \ THEN ;
 2563: 
 2564: Cond: \- \- ;Cond
 2565: Cond: \+ \+ ;Cond
 2566: Cond: \D \D ;Cond
 2567: 
 2568: : ?? bl word find IF execute ELSE drop 0 THEN ;
 2569: 
 2570: : needed:
 2571: \G defines ghost for words that we want to be compiled
 2572:   BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
 2573: 
 2574: \ words that should be in minimal
 2575: 
 2576: create s-buffer 50 chars allot
 2577: 
 2578: bigendian Constant bigendian
 2579: 
 2580: : here there ;
 2581: : equ constant ;
 2582: : mark there constant ;
 2583: 
 2584: \ compiler directives
 2585: : >ram >ram ;
 2586: : >rom >rom ;
 2587: : >auto >auto ;
 2588: : >tempdp >tempdp ;
 2589: : tempdp> tempdp> ;
 2590: : const constflag on ;
 2591: : warnings name 3 = 0= twarnings ! drop ;
 2592: : | ;
 2593: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
 2594: 
 2595: : save-cross save-cross ;
 2596: : save-region save-region ;
 2597: : tdump swap >image swap dump ;
 2598: 
 2599: also forth 
 2600: [IFDEF] Label           : Label defempty? Label ; [THEN] 
 2601: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
 2602: \ [IFDEF] builttag	: builttag builttag ;	[THEN]
 2603: previous
 2604: 
 2605: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
 2606: : + + ;
 2607: : 1+ 1 + ;
 2608: : 2+ 2 + ;
 2609: : 1- 1- ;
 2610: : - - ;
 2611: : and and ;
 2612: : or or ;
 2613: : 2* 2* ;
 2614: : * * ;
 2615: : / / ;
 2616: : dup dup ;
 2617: : over over ;
 2618: : swap swap ;
 2619: : rot rot ;
 2620: : drop drop ;
 2621: : =   = ;
 2622: : 0=   0= ;
 2623: : lshift lshift ;
 2624: : 2/ 2/ ;
 2625: : . . ;
 2626: 
 2627: : all-words    ['] forced?    IS skip? ;
 2628: : needed-words ['] needed?  IS skip? ;
 2629: : undef-words  ['] defined2? IS skip? ;
 2630: : skipdef skipdef ;
 2631: 
 2632: : \  postpone \ ;  immediate
 2633: : \G T-\G ; immediate
 2634: : (  postpone ( ;  immediate
 2635: : include bl word count included ;
 2636: : require require ;
 2637: : .( [char] ) parse type ;
 2638: : ." [char] " parse type ;
 2639: : cr cr ;
 2640: 
 2641: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
 2642: 
 2643: \ only forth also cross also minimal definitions order
 2644: 
 2645: \ cross-compiler words
 2646: 
 2647: : decimal       decimal ;
 2648: : hex           hex ;
 2649: 
 2650: \ : tudp          X tudp ;
 2651: \ : tup           X tup ;
 2652: 
 2653: : doc-off       false to-doc ! ;
 2654: : doc-on        true  to-doc ! ;
 2655: 
 2656: [IFDEF] dbg : dbg dbg ; [THEN]
 2657: 
 2658: \ for debugging...
 2659: : order         order ;
 2660: : hwords         words ;
 2661: : words 	also ghosts words previous ;
 2662: : .s            .s ;
 2663: : bye           bye ;
 2664: 
 2665: \ turnkey direction
 2666: : H forth ; immediate
 2667: : T minimal ; immediate
 2668: : G ghosts ; immediate
 2669: 
 2670: : turnkey 
 2671:    \GFORTH 0 set-order also ghosts
 2672:    \ANSI [ ' ghosts >wordlist ] Literal 1 set-order
 2673:    also target definitions
 2674:    also Minimal also ;
 2675: 
 2676: \ these ones are pefered:
 2677: 
 2678: : lock   turnkey ;
 2679: : unlock previous forth also cross ;
 2680: 
 2681: \ also minimal
 2682: : [[ also unlock ;
 2683: : ]] previous previous also also ;
 2684: 
 2685: unlock definitions also minimal
 2686: : lock   lock ;
 2687: lock
 2688: 
 2689: \ load cross compiler extension defined in mach file
 2690: 
 2691: UNLOCK >CROSS
 2692: 
 2693: [IFDEF] extend-cross extend-cross [THEN]
 2694: 
 2695: LOCK

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