File:  [gforth] / gforth / cross.fs
Revision 1.84: download - view: text, annotated - select for diffs
Thu May 4 09:31:16 2000 UTC (23 years, 11 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
Fixed bug for cross-compiling with gforth.fi image.
Added output of symbol table and relocation table for debugging.

    1: \ CROSS.FS     The Cross-Compiler                      06oct92py
    2: \ Idea and implementation: Bernd Paysan (py)
    3: 
    4: \ Copyright (C) 1995,1996,1997,1998,1999 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., 675 Mass Ave, Cambridge, MA 02139, 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 >r -1 >r
  479:   BEGIN dup WHILE
  480: 	over c@ pathsep? 
  481: 	IF 	r@ -1 =
  482: 		IF	r> drop dup >r
  483: 		ELSE	2dup 1 /string 
  484: 			3 min s" ../" compare
  485: 			0=
  486: 			IF	r@ over - ( diff )
  487: 				2 pick swap - ( dest-adr )
  488: 				>r 3 /string r> swap 2dup >r >r
  489: 				move r> r>
  490: 			ELSE	r> drop dup >r
  491: 			THEN
  492: 		THEN
  493: 	THEN
  494: 	1 /string
  495:   REPEAT 
  496:   r> drop 
  497:   drop r> tuck - ;
  498: 
  499: : reworkdir ( -- )
  500:   remove~+
  501:   ofile count compact..
  502:   nip ofile c! ;
  503: 
  504: : open-ofile ( -- fid ior )
  505:     \G opens the file whose name is in ofile
  506:     expandtopic reworkdir
  507:     ofile count r/o open-file ;
  508: 
  509: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
  510:   0 ofile ! >r >r ofile place need/
  511:   r> r> ofile +place
  512:   open-ofile ;
  513: 
  514: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
  515:     \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
  516:     \G If found, the resulting path and an open file descriptor
  517:     \G are returned. If the file is not found, @var{ior} is non-zero.
  518:   >r
  519:   2dup absolut-path?
  520:   IF    rdrop
  521:         ofile place open-ofile
  522: 	dup 0= IF >r ofile count r> THEN EXIT
  523:   ELSE  r> path>counted
  524:         BEGIN  next-path dup
  525:         WHILE  5 pick 5 pick check-path
  526:         0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
  527:   REPEAT
  528:         2drop 2drop 2drop -38
  529:   THEN ;
  530: 
  531: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
  532:     \G Look in the Forth search path for the file specified by @var{addr1 u1}.
  533:     \G If found, the resulting path and an open file descriptor
  534:     \G are returned. If the file is not found, @var{ior} is non-zero.
  535:     fpath open-path-file ;
  536: 
  537: fpath= ~+
  538: 
  539: [THEN]
  540: 
  541: \ \ --------------------	include require			13may99jaw
  542: 
  543: >CROSS
  544: 
  545: : add-included-file ( adr len -- adr )
  546:   dup >fl-name char+ allocate throw >r
  547:   file-list @ r@ ! r@ file-list !
  548:   r@ >fl-name place r> ;
  549: 
  550: : included? ( c-addr u -- f )
  551:   file-list
  552:   BEGIN	@ dup
  553:   WHILE	>r 2dup r@ >fl-name count compare 0=
  554: 	IF rdrop 2drop true EXIT THEN
  555: 	r>
  556:   REPEAT
  557:   2drop drop false ;	
  558: 
  559: false DebugFlag showincludedfiles
  560: 
  561: : included1 ( fd adr u -- )
  562: \ include file adr u / fd
  563: \ we don't use fd with include-file, because the forth system
  564: \ doesn't know the name of the file to get a nice error report
  565:   [d?] showincludedfiles
  566:   IF	cr ." Including: " 2dup type ." ..." THEN
  567:   rot close-file throw
  568:   source-desc >r
  569:   add-included-file to source-desc 
  570:   sourcefilename
  571:   ['] included catch
  572:   r> to source-desc 
  573:   throw ;
  574: 
  575: : included ( adr len -- )
  576: 	cross-file-list to file-list
  577: 	open-fpath-file throw 
  578:         included1 ;
  579: 
  580: : required ( adr len -- )
  581: 	cross-file-list to file-list
  582: 	open-fpath-file throw \ 2dup cr ." R:" type
  583: 	2dup included?
  584: 	IF 	2drop close-file throw
  585: 	ELSE	included1
  586: 	THEN ;
  587: 
  588: : include bl word count included ;
  589: 
  590: : require bl word count required ;
  591: 
  592: 0 [IF]
  593: 
  594: also forth definitions previous
  595: 
  596: : included ( adr len -- ) included ;
  597: 
  598: : required ( adr len -- ) required ;
  599: 
  600: : include include ;
  601: 
  602: : require require ;
  603: 
  604: [THEN]
  605: 
  606: >CROSS
  607: hex
  608: 
  609: \ \ --------------------        Error Handling                  05aug97jaw
  610: 
  611: \ Flags
  612: 
  613: also forth definitions  \ these values may be predefined before
  614:                         \ the cross-compiler is loaded
  615: 
  616: false DefaultValue stack-warn   	 \ check on empty stack at any definition
  617: false DefaultValue create-forward-warn   \ warn on forward declaration of created words
  618: 
  619: previous >CROSS
  620: 
  621: : .dec
  622:   base @ decimal swap . base ! ;
  623: 
  624: : .sourcepos
  625:   cr sourcefilename type ." :"
  626:   sourceline# .dec ;
  627: 
  628: : warnhead
  629: \G display error-message head
  630: \G perhaps with linenumber and filename
  631:   .sourcepos ." Warning: " ;
  632: 
  633: : empty? depth IF .sourcepos ." Stack not empty!"  THEN ;
  634: 
  635: stack-warn [IF]
  636: : defempty? empty? ;
  637: [ELSE]
  638: : defempty? ; immediate
  639: [THEN]
  640: 
  641: \ \ GhostNames Ghosts                                  9may93jaw
  642: 
  643: \ second name source to search trough list
  644: 
  645: VARIABLE GhostNames
  646: 0 GhostNames !
  647: 
  648: : GhostName ( -- addr )
  649:     align here GhostNames @ , GhostNames ! here 0 ,
  650:     bl word count
  651:     \ 2dup type space
  652:     string, \ !! cfalign ?
  653:     align ;
  654: 
  655: \ Ghost Builder                                        06oct92py
  656: 
  657: \ <T T> new version with temp variable                 10may93jaw
  658: 
  659: VARIABLE VocTemp
  660: 
  661: : <T  get-current VocTemp ! also Ghosts definitions ;
  662: : T>  previous VocTemp @ set-current ;
  663: 
  664: hex
  665: 4711 Constant <fwd>             4712 Constant <res>
  666: 4713 Constant <imm>             4714 Constant <do:>
  667: 4715 Constant <skip>
  668: 
  669: \ iForth makes only immediate directly after create
  670: \ make atonce trick! ?
  671: 
  672: Variable atonce atonce off
  673: 
  674: : NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;
  675: 
  676: : GhostHeader <fwd> , 0 , ['] NoExec , ;
  677: 
  678: : >magic ;		\ type of ghost
  679: : >link cell+ ;		\ pointer where ghost is in target, or if unresolved
  680: 			\ points to the where we have to resolve (linked-list)
  681: : >exec cell+ cell+ ;	\ execution symantics (while target compiling) of ghost
  682: : >end 3 cells + ;	\ room for additional tags
  683: 			\ for builder (create, variable...) words the
  684: 			\ execution symantics of words built are placed here
  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: : Make-Ghost ( "name" -- ghost )
  691:   >in @ GhostName swap >in !
  692:   <T Create atonce @ IF immediate atonce off THEN
  693:   here tuck swap ! ghostheader T>
  694:   dup last-ghost !
  695:   DOES> dup executed-ghost ! >exec @ execute ;
  696: 
  697: \ ghost words                                          14oct92py
  698: \                                          changed:    10may93py/jaw
  699: 
  700: : gfind   ( string -- ghost true/1 / string false )
  701: \ searches for string in word-list ghosts
  702:   dup count [ ' ghosts >wordlist ] Literal search-wordlist
  703:   dup IF >r >body nip r>  THEN ;
  704: 
  705: : gdiscover ( xt -- ghost true | xt false )
  706:   GhostNames
  707:   BEGIN @ dup
  708:   WHILE 2dup
  709:         cell+ @ dup >magic @ <fwd> <>
  710:         >r >link @ = r> and
  711:         IF cell+ @ nip true EXIT THEN
  712:   REPEAT
  713:   drop false ;
  714: 
  715: VARIABLE Already
  716: 
  717: : ghost   ( "name" -- ghost )
  718:   Already off
  719:   >in @  bl word gfind   IF  atonce off Already on nip EXIT  THEN
  720:   drop  >in !  Make-Ghost ;
  721: 
  722: : >ghostname ( ghost -- adr len )
  723:   GhostNames
  724:   BEGIN @ dup
  725:   WHILE 2dup cell+ @ =
  726:   UNTIL nip 2 cells + count
  727:   ELSE  2drop 
  728: 	\ true abort" CROSS: Ghostnames inconsistent"
  729: 	s" ?!?!?!"
  730:   THEN ;
  731: 
  732: : .ghost ( ghost -- ) >ghostname type ;
  733: 
  734: \ ' >ghostname ALIAS @name
  735: 
  736: : forward? ( ghost -- flag )
  737:   >magic @ <fwd> = ;
  738: 
  739: : undefined? ( ghost -- flag )
  740:   >magic @ dup <fwd> = swap <skip> = or ;
  741: 
  742: \ Predefined ghosts                                    12dec92py
  743: 
  744: ghost 0=                                        drop
  745: ghost branch    ghost ?branch                   2drop
  746: ghost (do)      ghost (?do)                     2drop
  747: ghost (for)                                     drop
  748: ghost (loop)    ghost (+loop)                   2drop
  749: ghost (next)                                    drop
  750: ghost unloop    ghost ;S                        2drop
  751: ghost lit       ghost (compile) ghost !         2drop drop
  752: ghost (does>)   ghost noop                      2drop
  753: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  754: ghost '                                         drop
  755: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
  756: ghost :dovar					drop
  757: ghost over      ghost =         ghost drop      2drop drop
  758: ghost - drop
  759: ghost 2drop drop
  760: ghost 2dup drop
  761: 
  762: \ \ Parameter for target systems                         06oct92py
  763: 
  764: \ we define it ans like...
  765: wordlist Constant target-environment
  766: 
  767: VARIABLE env-current \ save information of current dictionary to restore with environ>
  768: 
  769: : >ENVIRON get-current env-current ! target-environment set-current ;
  770: : ENVIRON> env-current @ set-current ; 
  771: 
  772: >TARGET
  773: 
  774: : environment? ( adr len -- [ x ] true | false )
  775:   target-environment search-wordlist 
  776:   IF execute true ELSE false THEN ;
  777: 
  778: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
  779: 
  780: : has? 	bl word count T environment? H 
  781: 	IF 	\ environment variable is present, return its value
  782: 	ELSE	\ environment variable is not present, return false
  783: 		false \ debug true ABORT" arg" 
  784: 	THEN ;
  785: 
  786: : $has? T environment? H IF ELSE false THEN ;
  787: 
  788: >ENVIRON get-order get-current swap 1+ set-order
  789: true SetValue compiler
  790: true SetValue cross
  791: true SetValue standard-threading
  792: >TARGET previous
  793: 
  794: 0
  795: [IFDEF] mach-file mach-file count 1 [THEN]
  796: [IFDEF] machine-file machine-file 1 [THEN]
  797: [IF] 	included hex drop
  798: [ELSE]  cr ." No machine description!" ABORT 
  799: [THEN]
  800: 
  801: >ENVIRON
  802: 
  803: T has? ec H
  804: [IF]
  805: false DefaultValue relocate
  806: false DefaultValue file
  807: false DefaultValue OS
  808: false DefaultValue prims
  809: false DefaultValue floating
  810: false DefaultValue glocals
  811: false DefaultValue dcomps
  812: false DefaultValue hash
  813: false DefaultValue xconds
  814: false DefaultValue header
  815: [THEN]
  816: 
  817: true DefaultValue interpreter
  818: true DefaultValue ITC
  819: false DefaultValue rom
  820: true DefaultValue standardthreading
  821: 
  822: >TARGET
  823: s" relocate" T environment? H 
  824: [IF]	SetValue NIL
  825: [ELSE]	>ENVIRON T NIL H SetValue relocate
  826: [THEN]
  827: 
  828: >CROSS
  829: 
  830: \ \ Create additional parameters                         19jan95py
  831: 
  832: \ currently cross only works for host machines with address-unit-bits
  833: \ eual to 8 because of s! and sc!
  834: \ but I start to query the environment just to modularize a little bit
  835: 
  836: : check-address-unit-bits ( -- )	
  837: \	s" ADDRESS-UNIT-BITS" environment?
  838: \	IF 8 <> ELSE true THEN
  839: \	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
  840: 
  841: \	shit, this doesn't work because environment? is only defined for 
  842: \	gforth.fi and not kernl???.fi
  843: 	;
  844: 
  845: check-address-unit-bits
  846: 8 Constant bits/byte	\ we define: byte is address-unit
  847: 
  848: 1 bits/byte lshift Constant maxbyte 
  849: \ this sets byte size for the target machine, (probably right guess) jaw
  850: 
  851: T
  852: NIL		   	Constant TNIL
  853: cell               	Constant tcell
  854: cell<<             	Constant tcell<<
  855: cell>bit           	Constant tcell>bit
  856: bits/char          	Constant tbits/char
  857: bits/char H bits/byte T /      
  858: 			Constant tchar
  859: float             	Constant tfloat
  860: 1 bits/char lshift 	Constant tmaxchar
  861: [IFUNDEF] bits/byte
  862: 8			Constant tbits/byte
  863: [ELSE]
  864: bits/byte		Constant tbits/byte
  865: [THEN]
  866: H
  867: tbits/char bits/byte /	Constant tbyte
  868: 
  869: 
  870: \ Variables                                            06oct92py
  871: 
  872: Variable image
  873: Variable tlast    TNIL tlast !  \ Last name field
  874: Variable tlastcfa \ Last code field
  875: Variable tdoes    \ Resolve does> calls
  876: Variable bit$
  877: 
  878: \ statistics						10jun97jaw
  879: 
  880: Variable headers-named 0 headers-named !
  881: Variable user-vars 0 user-vars !
  882: 
  883: : target>bitmask-size ( u1 -- u2 )
  884:   1- tcell>bit rshift 1+ ;
  885: 
  886: : allocatetarget ( size --- adr )
  887:   dup allocate ABORT" CROSS: No memory for target"
  888:   swap over swap erase ;
  889: 
  890: \ \ memregion.fs
  891: 
  892: 
  893: Variable last-defined-region    \ pointer to last defined region
  894: Variable region-link            \ linked list with all regions
  895: Variable mirrored-link          \ linked list for mirrored regions
  896: 0 dup mirrored-link ! region-link !
  897: 
  898: 
  899: : >rname 6 cells + ;
  900: : >rbm   5 cells + ;
  901: : >rmem  4 cells + ;
  902: : >rlink 3 cells + ;
  903: : >rdp 2 cells + ;
  904: : >rlen cell+ ;
  905: : >rstart ;
  906: 
  907: 
  908: : region ( addr len -- )                \G create a new region
  909:   \ check whether predefined region exists 
  910:   save-input bl word find >r >r restore-input throw r> r> 0= 
  911:   IF	\ make region
  912: 	drop
  913: 	save-input create restore-input throw
  914: 	here last-defined-region !
  915: 	over ( startaddr ) , ( length ) , ( dp ) ,
  916: 	region-link linked 0 , 0 , bl word count string,
  917:   ELSE	\ store new parameters in region
  918:         bl word drop
  919: 	>body >r r@ last-defined-region !
  920: 	r@ >rlen ! dup r@ >rstart ! r> >rdp !
  921:   THEN ;
  922: 
  923: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
  924:   dup >rstart @ swap >rlen @ over + ;
  925: 
  926: : extent  ( region -- startaddr len )   \G returns the really used area
  927:   dup >rstart @ swap >rdp @ over - ;
  928: 
  929: : area ( region -- startaddr totallen ) \G returns the total area
  930:   dup >rstart @ swap >rlen @ ;
  931: 
  932: : mirrored                              \G mark a region as mirrored
  933:   mirrored-link
  934:   align linked last-defined-region @ , ;
  935: 
  936: : .addr ( u -- )
  937: \G prints a 16 or 32 Bit nice hex value
  938:   base @ >r hex
  939:   tcell 2 u>
  940:   IF s>d <# # # # # [char] . hold # # # # #> type
  941:   ELSE s>d <# # # # # # #> type
  942:   THEN r> base ! ;
  943: 
  944: : .regions                      \G display region statistic
  945: 
  946:   \ we want to list the regions in the right order
  947:   \ so first collect all regions on stack
  948:   0 region-link @
  949:   BEGIN dup WHILE dup @ REPEAT drop
  950:   BEGIN dup
  951:   WHILE cr
  952:         0 >rlink - >r
  953:         r@ >rname count tuck type
  954:         12 swap - 0 max spaces space
  955:         ." Start: " r@ >rstart @ dup .addr space
  956:         ." End: " r@ >rlen @ + .addr space
  957:         ." DP: " r> >rdp @ .addr
  958:   REPEAT drop
  959:   s" rom" T $has? H 0= ?EXIT
  960:   cr ." Mirrored:"
  961:   mirrored-link @
  962:   BEGIN dup
  963:   WHILE space dup cell+ @ >rname count type @
  964:   REPEAT drop cr
  965:   ;
  966: 
  967: \ -------- predefined regions
  968: 
  969: 0 0 region address-space
  970: \ total memory addressed and used by the target system
  971: 
  972: 0 0 region dictionary
  973: \ rom area for the compiler
  974: 
  975: T has? rom H
  976: [IF]
  977: 0 0 region ram-dictionary mirrored
  978: \ ram area for the compiler
  979: [ELSE]
  980: ' dictionary ALIAS ram-dictionary
  981: [THEN]
  982: 
  983: 0 0 region return-stack
  984: 
  985: 0 0 region data-stack
  986: 
  987: 0 0 region tib-region
  988: 
  989: ' dictionary ALIAS rom-dictionary
  990: 
  991: 
  992: : setup-target ( -- )   \G initialize targets memory space
  993:   s" rom" T $has? H
  994:   IF  \ check for ram and rom...
  995:       \ address-space area nip 0<>
  996:       ram-dictionary area nip 0<>
  997:       rom-dictionary area nip 0<>
  998:       and 0=
  999:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
 1000:   THEN
 1001:   address-space area nip
 1002:   IF
 1003:       address-space area
 1004:   ELSE
 1005:       dictionary area
 1006:   THEN
 1007:   nip 0=
 1008:   ABORT" CROSS: define at least address-space or dictionary!!"
 1009: 
 1010:   \ allocate target for each region
 1011:   region-link
 1012:   BEGIN @ dup
 1013:   WHILE dup
 1014:         0 >rlink - >r
 1015:         r@ >rlen @
 1016:         IF      \ allocate mem
 1017:                 r@ >rlen @ dup
 1018: 
 1019:                 allocatetarget dup image !
 1020:                 r@ >rmem !
 1021: 
 1022:                 target>bitmask-size allocatetarget
 1023:                 dup bit$ !
 1024:                 r> >rbm !
 1025: 
 1026:         ELSE    r> drop THEN
 1027:    REPEAT drop ;
 1028: 
 1029: \ MakeKernal                                           		22feb99jaw
 1030: 
 1031: : makekernel ( targetsize -- targetsize )
 1032:   dup dictionary >rlen ! setup-target ;
 1033: 
 1034: >MINIMAL
 1035: : makekernel makekernel ;
 1036: >CROSS
 1037: 
 1038: \ \ switched tdp for rom support				03jun97jaw
 1039: 
 1040: \ second value is here to store some maximal value for statistics
 1041: \ tempdp is also embedded here but has nothing to do with rom support
 1042: \ (needs switched dp)
 1043: 
 1044: variable tempdp	0 ,	\ temporary dp for resolving
 1045: variable tempdp-save
 1046: 
 1047: 0 [IF]
 1048: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
 1049: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
 1050: 
 1051: \
 1052: variable sramdp		\ start of ram-area for forth
 1053: variable sromdp		\ start of rom-area for forth
 1054: 
 1055: [THEN]
 1056: 
 1057: 
 1058: 0 value tdp
 1059: variable fixed		\ flag: true: no automatic switching
 1060: 			\	false: switching is done automatically
 1061: 
 1062: \ Switch-Policy:
 1063: \
 1064: \ a header is always compiled into rom
 1065: \ after a created word (create and variable) compilation goes to ram
 1066: \
 1067: \ Be careful: If you want to make the data behind create into rom
 1068: \ you have to put >rom before create!
 1069: 
 1070: variable constflag constflag off
 1071: 
 1072: : activate ( region -- )
 1073: \G next code goes to this region
 1074:   >rdp to tdp ;
 1075: 
 1076: : (switchram)
 1077:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
 1078:   ram-dictionary activate ;
 1079: 
 1080: : switchram
 1081:   constflag @
 1082:   IF constflag off ELSE (switchram) THEN ;
 1083: 
 1084: : switchrom
 1085:   fixed @ ?EXIT rom-dictionary activate ;
 1086: 
 1087: : >tempdp ( addr -- ) 
 1088:   tdp tempdp-save ! tempdp to tdp tdp ! ;
 1089: : tempdp> ( -- )
 1090:   tempdp-save @ to tdp ;
 1091: 
 1092: : >ram  fixed off (switchram) fixed on ;
 1093: : >rom  fixed off switchrom fixed on ;
 1094: : >auto fixed off switchrom ;
 1095: 
 1096: 
 1097: 
 1098: \ : romstart dup sromdp ! romdp ! ;
 1099: \ : ramstart dup sramdp ! ramdp ! ;
 1100: 
 1101: \ default compilation goes to rom
 1102: \ when romable support is off, only the rom switch is used (!!)
 1103: >auto
 1104: 
 1105: : there  tdp @ ;
 1106: 
 1107: >TARGET
 1108: 
 1109: \ \ Target Memory Handling
 1110: 
 1111: \ Byte ordering and cell size                          06oct92py
 1112: 
 1113: : cell+         tcell + ;
 1114: : cells         tcell<< lshift ;
 1115: : chars         tchar * ;
 1116: : char+		tchar + ;
 1117: : floats	tfloat * ;
 1118:     
 1119: >CROSS
 1120: : cell/         tcell<< rshift ;
 1121: >TARGET
 1122: 20 CONSTANT bl
 1123: \ TNIL Constant NIL
 1124: 
 1125: >CROSS
 1126: 
 1127: bigendian
 1128: [IF]
 1129:    : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
 1130:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1131:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds
 1132:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1133:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
 1134:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1135:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
 1136:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1137: [ELSE]
 1138:    : S!  ( n addr -- )  >r s>d r> tcell bounds
 1139:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1140:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
 1141:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1142:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
 1143:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1144:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
 1145:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1146: [THEN]
 1147: 
 1148: : taddr>region ( taddr -- region | 0 )
 1149: \G finds for a target-address the correct region
 1150: \G returns 0 if taddr is not in range of a target memory region
 1151:   region-link
 1152:   BEGIN @ dup
 1153:   WHILE dup >r
 1154:         0 >rlink - >r
 1155:         r@ >rlen @
 1156:         IF      dup r@ borders within
 1157:                 IF r> r> drop nip EXIT THEN
 1158:         THEN
 1159:         r> drop
 1160:         r>
 1161:   REPEAT
 1162:   2drop 0 ;
 1163: 
 1164: : (>regionimage) ( taddr -- 'taddr )
 1165:   dup
 1166:   \ find region we want to address
 1167:   taddr>region dup 0= ABORT" Address out of range!"
 1168:   >r
 1169:   \ calculate offset in region
 1170:   r@ >rstart @ -
 1171:   \ add regions real address in our memory
 1172:   r> >rmem @ + ;
 1173: 
 1174: \ Bit string manipulation                               06oct92py
 1175: \                                                       9may93jaw
 1176: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
 1177: : bits ( n -- n ) chars Bittable + c@ ;
 1178: 
 1179: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
 1180: : +bit ( addr n -- )  >bit over c@ or swap c! ;
 1181: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
 1182: 
 1183: : (relon) ( taddr -- )  
 1184:   [ [IFDEF] fd-relocation-table ]
 1185:   s" +" fd-relocation-table write-file throw
 1186:   dup s>d <# #s #> fd-relocation-table write-line throw
 1187:   [ [THEN] ]
 1188:   bit$ @ swap cell/ +bit ;
 1189: 
 1190: : (reloff) ( taddr -- ) 
 1191:   [ [IFDEF] fd-relocation-table ]
 1192:   s" -" fd-relocation-table write-file throw
 1193:   dup s>d <# #s #> fd-relocation-table write-line throw
 1194:   [ [THEN] ]
 1195:   bit$ @ swap cell/ -bit ;
 1196: 
 1197: : (>image) ( taddr -- absaddr ) image @ + ;
 1198: 
 1199: DEFER >image
 1200: DEFER relon
 1201: DEFER reloff
 1202: DEFER correcter
 1203: 
 1204: T has? relocate H
 1205: [IF]
 1206: ' (relon) IS relon
 1207: ' (reloff) IS reloff
 1208: ' (>image) IS >image
 1209: [ELSE]
 1210: ' drop IS relon
 1211: ' drop IS reloff
 1212: ' (>regionimage) IS >image
 1213: [THEN]
 1214: 
 1215: \ Target memory access                                 06oct92py
 1216: 
 1217: : align+  ( taddr -- rest )
 1218:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
 1219: : cfalign+  ( taddr -- rest )
 1220:     \ see kernel.fs:cfaligned
 1221:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
 1222: 
 1223: >TARGET
 1224: : aligned ( taddr -- ta-addr )  dup align+ + ;
 1225: \ assumes cell alignment granularity (as GNU C)
 1226: 
 1227: : cfaligned ( taddr1 -- taddr2 )
 1228:     \ see kernel.fs
 1229:     dup cfalign+ + ;
 1230: 
 1231: : @  ( taddr -- w )     >image S@ ;
 1232: : !  ( w taddr -- )     >image S! ;
 1233: : c@ ( taddr -- char )  >image Sc@ ;
 1234: : c! ( char taddr -- )  >image Sc! ;
 1235: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
 1236: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
 1237: 
 1238: \ Target compilation primitives                        06oct92py
 1239: \ included A!                                          16may93jaw
 1240: 
 1241: : here  ( -- there )    there ;
 1242: : allot ( n -- )        tdp +! ;
 1243: : ,     ( w -- )        T here H tcell T allot  ! H ;
 1244: : c,    ( char -- )     T here H tchar T allot c! H ;
 1245: : align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
 1246: : cfalign ( -- )
 1247:     T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
 1248: 
 1249: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
 1250: : A!                    swap >address swap dup relon T ! H ;
 1251: : A,    ( w -- )        >address T here H relon T , H ;
 1252: 
 1253: >CROSS
 1254: 
 1255: : tcmove ( source dest len -- )
 1256: \G cmove in target memory
 1257:   tchar * bounds
 1258:   ?DO  dup T c@ H I T c! H 1+
 1259:   tchar +LOOP  drop ;
 1260: 
 1261: \ \ Load Assembler
 1262: 
 1263: >TARGET
 1264: H also Forth definitions
 1265: 
 1266: : X 	bl word count [ ' target >wordlist ] Literal search-wordlist
 1267: 	IF	state @ IF compile,
 1268: 		ELSE execute THEN
 1269: 	ELSE	-1 ABORT" Cross: access method not supported!"
 1270: 	THEN ; immediate
 1271: 
 1272: [IFDEF] asm-include asm-include [THEN] hex
 1273: 
 1274: previous
 1275: >CROSS H
 1276: 
 1277: \ \ --------------------        Compiler Plug Ins               01aug97jaw
 1278: 
 1279: \  Compiler States
 1280: 
 1281: Variable comp-state
 1282: 0 Constant interpreting
 1283: 1 Constant compiling
 1284: 2 Constant resolving
 1285: 3 Constant assembling
 1286: 
 1287: Defer lit, ( n -- )
 1288: Defer alit, ( n -- )
 1289: 
 1290: Defer branch, ( target-addr -- )	\ compiles a branch
 1291: Defer ?branch, ( target-addr -- )	\ compiles a ?branch
 1292: Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
 1293: Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
 1294: Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
 1295: Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
 1296: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
 1297: Defer branchfrom, ( -- )		\ ?!
 1298: Defer branchtomark, ( -- target-addr )	\ marks a branch destination
 1299: 
 1300: Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
 1301: Defer colonmark, ( -- addr )		\ marks a colon call
 1302: Defer colon-resolve ( tcfa addr -- )
 1303: 
 1304: Defer addr-resolve ( target-addr addr -- )
 1305: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
 1306: 
 1307: Defer do,	( -- do-token )
 1308: Defer ?do,	( -- ?do-token )
 1309: Defer for,	( -- for-token )
 1310: Defer loop,	( do-token / ?do-token -- )
 1311: Defer +loop,	( do-token / ?do-token -- )
 1312: Defer next,	( for-token )
 1313: 
 1314: [IFUNDEF] ca>native
 1315: defer ca>native	
 1316: [THEN]
 1317: 
 1318: >TARGET
 1319: DEFER >body             \ we need the system >body
 1320: 			\ and the target >body
 1321: >CROSS
 1322: T 2 cells H VALUE xt>body
 1323: DEFER doprim,	\ compiles start of a primitive
 1324: DEFER docol,   	\ compiles start of a colon definition
 1325: DEFER doer,		
 1326: DEFER fini,      \ compiles end of definition ;s
 1327: DEFER doeshandler,
 1328: DEFER dodoes,
 1329: 
 1330: DEFER ]comp     \ starts compilation
 1331: DEFER comp[     \ ends compilation
 1332: 
 1333: : (cc) T a, H ;					' (cc) IS colon,
 1334: 
 1335: : (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
 1336: : (ar) T ! H ;					' (ar) IS addr-resolve
 1337: : (dr)  ( ghost res-pnt target-addr addr )
 1338: 	>tempdp drop over 
 1339: 	dup >magic @ <do:> =
 1340: 	IF 	doer,
 1341: 	ELSE	dodoes,
 1342: 	THEN 
 1343: 	tempdp> ;				' (dr) IS doer-resolve
 1344: 
 1345: : (cm) ( -- addr )
 1346:     T here align H
 1347:     -1 colon, ;					' (cm) IS colonmark,
 1348: 
 1349: >TARGET
 1350: : compile, colon, ;
 1351: >CROSS
 1352: 
 1353: \ resolve structure
 1354: 
 1355: : >next ;		\ link to next field
 1356: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
 1357: : >taddr cell+ cell+ ;	
 1358: : >ghost 3 cells + ;
 1359: : >file 4 cells + ;
 1360: : >line 5 cells + ;
 1361: 
 1362: : (refered) ( ghost addr tag -- )
 1363: \G creates a reference to ghost at address taddr
 1364:     rot >r here r@ >link @ , r> >link ! 
 1365:     ( taddr tag ) ,
 1366:     ( taddr ) , 
 1367:     last-header-ghost @ , 
 1368:     loadfile , 
 1369:     sourceline# , 
 1370:   ;
 1371: 
 1372: : refered ( ghost tag -- )
 1373: \G creates a resolve structure
 1374:     T here aligned H swap (refered)
 1375:   ;
 1376: 
 1377: : killref ( addr ghost -- )
 1378: \G kills a forward reference to ghost at position addr
 1379: \G this is used to eleminate a :dovar refence after making a DOES>
 1380:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
 1381:     swap >r >link
 1382:     BEGIN dup @ dup  ( addr last this )
 1383:     WHILE dup >taddr @ r@ =
 1384:  	 IF   @ over !
 1385: 	 ELSE nip THEN
 1386:     REPEAT rdrop 2drop 
 1387:   ;
 1388: 
 1389: Defer resolve-warning
 1390: 
 1391: : reswarn-test ( ghost res-struct -- ghost res-struct )
 1392:   over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
 1393: 
 1394: : reswarn-forward ( ghost res-struct -- ghost res-struct )
 1395:   over warnhead .ghost dup ."  is referenced in " 
 1396:   >ghost @ .ghost ;
 1397: 
 1398: \ ' reswarn-test IS resolve-warning
 1399:  
 1400: \ resolve                                              14oct92py
 1401: 
 1402:  : resolve-loop ( ghost resolve-list tcfa -- )
 1403:     >r
 1404:     BEGIN dup WHILE 
 1405: \  	  dup >tag @ 2 = IF reswarn-forward THEN
 1406: 	  resolve-warning 
 1407: 	  r@ over >taddr @ 
 1408: 	  2 pick >tag @
 1409: 	  CASE	0 OF colon-resolve ENDOF
 1410: 		1 OF addr-resolve ENDOF
 1411: 		2 OF doer-resolve ENDOF
 1412: 	  ENDCASE
 1413: 	  @ \ next list element
 1414:     REPEAT 2drop rdrop 
 1415:   ;
 1416: 
 1417: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
 1418: \  >r dup >link @
 1419: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
 1420: 
 1421: \ exists                                                9may93jaw
 1422: 
 1423: Variable TWarnings
 1424: TWarnings on
 1425: Variable Exists-Warnings
 1426: Exists-Warnings on
 1427: 
 1428: : exists ( ghost tcfa -- )
 1429:   over GhostNames
 1430:   BEGIN @ dup
 1431:   WHILE 2dup cell+ @ =
 1432:   UNTIL
 1433:         2 cells + count
 1434:         TWarnings @ Exists-Warnings @ and
 1435:         IF warnhead type ."  exists"
 1436:         ELSE 2drop THEN
 1437:         drop swap >link !
 1438:   ELSE  true abort" CROSS: Ghostnames inconsistent "
 1439:   THEN ;
 1440: 
 1441: : resolve  ( ghost tcfa -- )
 1442: \G resolve referencies to ghost with tcfa
 1443:     \ is ghost resolved?, second resolve means another definition with the
 1444:     \ same name
 1445:     over undefined? 0= IF  exists EXIT THEN
 1446:     \ get linked-list
 1447:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
 1448:     \ mark ghost as resolved
 1449:     dup r@ >link ! <res> r@ >magic !
 1450:     \ loop through forward referencies
 1451:     r> -rot 
 1452:     comp-state @ >r Resolving comp-state !
 1453:     resolve-loop 
 1454:     r> comp-state !
 1455: 
 1456:     ['] noop IS resolve-warning 
 1457:   ;
 1458: 
 1459: \ gexecute ghost,                                      01nov92py
 1460: 
 1461: : is-forward   ( ghost -- )
 1462:   colonmark, 0 (refered) ; \ compile space for call
 1463: 
 1464: : is-resolved   ( ghost -- )
 1465:   >link @ colon, ; \ compile-call
 1466: 
 1467: : gexecute   ( ghost -- )
 1468:   dup @ <fwd> = IF  is-forward  ELSE  is-resolved  THEN ;
 1469: 
 1470: : addr,  ( ghost -- )
 1471:   dup @ <fwd> = IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1472: 
 1473: \ !! : ghost,     ghost  gexecute ;
 1474: 
 1475: \ .unresolved                                          11may93jaw
 1476: 
 1477: variable ResolveFlag
 1478: 
 1479: \ ?touched                                             11may93jaw
 1480: 
 1481: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1482:                                0 <> and ;
 1483: 
 1484: : .forwarddefs ( ghost -- )
 1485: 	."  appeared in:"
 1486: 	>link
 1487: 	BEGIN	@ dup
 1488: 	WHILE	cr 5 spaces
 1489: 		dup >ghost @ .ghost
 1490: 		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 1491: 		."  line " dup >line @ .dec
 1492: 	REPEAT 
 1493: 	drop ;
 1494: 
 1495: : ?resolved  ( ghostname -- )
 1496:   dup cell+ @ ?touched
 1497:   IF  	dup 
 1498: 	cell+ cell+ count cr type ResolveFlag on 
 1499: 	cell+ @ .forwarddefs
 1500:   ELSE 	drop 
 1501:   THEN ;
 1502: 
 1503: : .unresolved  ( -- )
 1504:   ResolveFlag off cr ." Unresolved: "
 1505:   Ghostnames
 1506:   BEGIN @ dup
 1507:   WHILE dup ?resolved
 1508:   REPEAT drop ResolveFlag @
 1509:   IF
 1510:       -1 abort" Unresolved words!"
 1511:   ELSE
 1512:       ." Nothing!"
 1513:   THEN
 1514:   cr ;
 1515: 
 1516: : .stats
 1517:   base @ >r decimal
 1518:   cr ." named Headers: " headers-named @ . 
 1519:   r> base ! ;
 1520: 
 1521: >MINIMAL
 1522: 
 1523: : .unresolved .unresolved ;
 1524: 
 1525: >CROSS
 1526: \ Header states                                        12dec92py
 1527: 
 1528: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
 1529: 
 1530: VARIABLE ^imm
 1531: 
 1532: >TARGET
 1533: : immediate     40 flag!
 1534:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 1535:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 1536:                 <imm> ^imm @ ! ;
 1537: : restrict      20 flag! ;
 1538: 
 1539: : isdoer	
 1540: \G define a forth word as doer, this makes obviously only sence on
 1541: \G forth processors such as the PSC1000
 1542: 		<do:> last-header-ghost @ >magic ! ;
 1543: >CROSS
 1544: 
 1545: \ Target Header Creation                               01nov92py
 1546: 
 1547: >TARGET
 1548: : string,  ( addr count -- )
 1549:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
 1550: : name,  ( "name" -- )  bl word count T string, 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:     80 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 80 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 80 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: \ structural conditionals                              17dec92py
 2216: 
 2217: >CROSS
 2218: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
 2219: : sys?        ( sys -- sys )    dup 0= ?struc ;
 2220: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
 2221: 
 2222: : branchoffset ( src dest -- )  - tchar / ; \ ?? jaw
 2223: 
 2224: : >resolve    ( sys -- )        
 2225: 	X here ( dup ." >" hex. ) over branchoffset swap X ! ;
 2226: 
 2227: : <resolve    ( sys -- )
 2228: 	X here ( dup ." <" hex. ) branchoffset X , ;
 2229: 
 2230: :noname compile branch X here branchoffset X , ;
 2231:   IS branch, ( target-addr -- )
 2232: :noname compile ?branch X here branchoffset X , ;
 2233:   IS ?branch, ( target-addr -- )
 2234: :noname compile branch T here 0 , H ;
 2235:   IS branchmark, ( -- branchtoken )
 2236: :noname compile ?branch T here 0 , H ;
 2237:   IS ?branchmark, ( -- branchtoken )
 2238: :noname T here 0 , H ;
 2239:   IS ?domark, ( -- branchtoken )
 2240: :noname dup X @ ?struc X here over branchoffset swap X ! ;
 2241:   IS branchtoresolve, ( branchtoken -- )
 2242: :noname branchto, X here ;
 2243:   IS branchtomark, ( -- target-addr )
 2244: 
 2245: >TARGET
 2246: 
 2247: \ Structural Conditionals                              12dec92py
 2248: 
 2249: Cond: BUT       restrict? sys? swap ;Cond
 2250: Cond: YET       restrict? sys? dup ;Cond
 2251: 
 2252: >CROSS
 2253: 
 2254: Variable tleavings 0 tleavings !
 2255: 
 2256: : (done) ( addr -- )
 2257:     tleavings @
 2258:     BEGIN  dup
 2259:     WHILE
 2260: 	>r dup r@ cell+ @ \ address of branch
 2261: 	u> 0=	   \ lower than DO?	
 2262:     WHILE
 2263: 	r@ 2 cells + @ \ branch token
 2264: 	branchtoresolve,
 2265: 	r@ @ r> free throw
 2266:     REPEAT  r>  THEN
 2267:     tleavings ! drop ;
 2268: 
 2269: >TARGET
 2270: 
 2271: Cond: DONE   ( addr -- )  restrict? (done) ;Cond
 2272: 
 2273: >CROSS
 2274: : (leave) ( branchtoken -- )
 2275:     3 cells allocate throw >r
 2276:     T here H r@ cell+ !
 2277:     r@ 2 cells + !
 2278:     tleavings @ r@ !
 2279:     r> tleavings ! ;
 2280: >TARGET
 2281: 
 2282: Cond: LEAVE     restrict? branchmark, (leave) ;Cond
 2283: Cond: ?LEAVE    restrict? compile 0=  ?branchmark, (leave)  ;Cond
 2284: 
 2285: >CROSS
 2286: \ !!JW ToDo : Move to general tools section
 2287: 
 2288: : to1 ( x1 x2 xn n -- addr )
 2289: \G packs n stack elements in a allocated memory region
 2290:    dup dup 1+ cells allocate throw dup >r swap 1+
 2291:    0 DO tuck ! cell+ LOOP
 2292:    drop r> ;
 2293: : 1to ( addr -- x1 x2 xn )
 2294: \G unpacks the elements saved by to1
 2295:     dup @ swap over cells + swap
 2296:     0 DO  dup @ swap 1 cells -  LOOP
 2297:     free throw ;
 2298: 
 2299: : loop]     branchto, dup <resolve tcell - (done) ;
 2300: 
 2301: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
 2302: 
 2303: >TARGET
 2304: 
 2305: \ Structural Conditionals                              12dec92py
 2306: 
 2307: >TARGET
 2308: Cond: AHEAD     restrict? branchmark, ;Cond
 2309: Cond: IF        restrict? ?branchmark, ;Cond
 2310: Cond: THEN      restrict? sys? branchto, branchtoresolve, ;Cond
 2311: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
 2312: 
 2313: Cond: BEGIN     restrict? branchtomark, ;Cond
 2314: Cond: WHILE     restrict? sys? compile IF swap ;Cond
 2315: Cond: AGAIN     restrict? sys? branch, ;Cond
 2316: Cond: UNTIL     restrict? sys? ?branch, ;Cond
 2317: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
 2318: 
 2319: Cond: CASE      restrict? 0 ;Cond
 2320: Cond: OF        restrict? 1+ >r compile over compile =
 2321:                 compile IF compile drop r> ;Cond
 2322: Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
 2323: Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond
 2324: 
 2325: \ Structural Conditionals                              12dec92py
 2326: 
 2327: :noname \ ?? i think 0 is too much! jaw
 2328:     0 compile (do)
 2329:     branchtomark,  2 to1 ;
 2330:   IS do, ( -- target-addr )
 2331: 
 2332: \ :noname
 2333: \     compile 2dup compile = compile IF
 2334: \     compile 2drop compile ELSE
 2335: \     compile (do) branchtomark, 2 to1 ;
 2336: \   IS ?do,
 2337:     
 2338: :noname
 2339:     0 compile (?do)  ?domark, (leave)
 2340:     branchtomark,  2 to1 ;
 2341:   IS ?do, ( -- target-addr )
 2342: :noname compile (for) branchtomark, ;
 2343:   IS for, ( -- target-addr )
 2344: :noname 1to compile (loop)  loop] compile unloop skiploop] ;
 2345:   IS loop, ( target-addr -- )
 2346: :noname 1to compile (+loop)  loop] compile unloop skiploop] ;
 2347:   IS +loop, ( target-addr -- )
 2348: :noname compile (next)  loop] compile unloop ;
 2349:   IS next, ( target-addr -- )
 2350: 
 2351: Cond: DO      	restrict? do, ;Cond
 2352: Cond: ?DO     	restrict? ?do, ;Cond
 2353: Cond: FOR	restrict? for, ;Cond
 2354: 
 2355: Cond: LOOP	restrict? sys? loop, ;Cond
 2356: Cond: +LOOP	restrict? sys? +loop, ;Cond
 2357: Cond: NEXT	restrict? sys? next, ;Cond
 2358: 
 2359: \ String words                                         23feb93py
 2360: 
 2361: : ,"            [char] " parse T string, align H ;
 2362: 
 2363: Cond: ."        restrict? compile (.")     T ," H ;Cond
 2364: Cond: S"        restrict? compile (S")     T ," H ;Cond
 2365: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
 2366: 
 2367: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
 2368: : IS            T >address ' >body ! H ;
 2369: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
 2370: : TO            T ' >body ! H ;
 2371: 
 2372: Cond: defers	T ' >body @ compile, H ;Cond
 2373: : on		T -1 swap ! H ; 
 2374: : off   	T 0 swap ! H ;
 2375: 
 2376: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
 2377: 
 2378: \ linked list primitive
 2379: : linked        X here over X @ X A, swap X ! ;
 2380: : chained	T linked A, H ;
 2381: 
 2382: : err"   s" ErrLink linked" evaluate T , H
 2383:          [char] " parse T string, align H ;
 2384: 
 2385: : env"  [char] " parse s" EnvLink linked" evaluate
 2386:         T string, align , H ;
 2387: 
 2388: : 2env" [char] " parse s" EnvLink linked" evaluate
 2389:         here >r T string, align , , H
 2390:         r> dup T c@ H 80 and swap T c! H ;
 2391: 
 2392: \ compile must be last                                 22feb93py
 2393: 
 2394: Cond: compile ( -- ) restrict? \ name
 2395:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2396:       0> IF    gexecute
 2397:          ELSE  dup >magic @ <imm> =
 2398:                IF   gexecute
 2399:                ELSE compile (compile) addr, THEN THEN ;Cond
 2400: 
 2401: Cond: postpone ( -- ) restrict? \ name
 2402:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2403:       0> IF    gexecute
 2404:          ELSE  dup >magic @ <imm> =
 2405:                IF   gexecute
 2406: 	       ELSE compile (compile) addr, THEN THEN ;Cond
 2407: 	   
 2408: \ save-cross                                           17mar93py
 2409: 
 2410: hex
 2411: 
 2412: >CROSS
 2413: Create magic  s" Gforth2x" here over allot swap move
 2414: 
 2415: bigendian 1+ \ strangely, in magic big=0, little=1
 2416: tcell 1 = 0 and or
 2417: tcell 2 = 2 and or
 2418: tcell 4 = 4 and or
 2419: tcell 8 = 6 and or
 2420: tchar 1 = 00 and or
 2421: tchar 2 = 28 and or
 2422: tchar 4 = 50 and or
 2423: tchar 8 = 78 and or
 2424: magic 7 + c!
 2425: 
 2426: : save-cross ( "image-name" "binary-name" -- )
 2427:   bl parse ." Saving to " 2dup type cr
 2428:   w/o bin create-file throw >r
 2429:   TNIL IF
 2430:       s" #! "           r@ write-file throw
 2431:       bl parse          r@ write-file throw
 2432:       s"  --image-file" r@ write-file throw
 2433:       #lf       r@ emit-file throw
 2434:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
 2435:       ?do
 2436: 	  bl over emit-file throw
 2437:       loop
 2438:       drop
 2439:       magic 8       r@ write-file throw \ write magic
 2440:   ELSE
 2441:       bl parse 2drop
 2442:   THEN
 2443:   image @ there 
 2444:   r@ write-file throw \ write image
 2445:   TNIL IF
 2446:       bit$  @ there 1- tcell>bit rshift 1+
 2447:                 r@ write-file throw \ write tags
 2448:   THEN
 2449:   r> close-file throw ;
 2450: 
 2451: : save-region ( addr len -- )
 2452:   bl parse w/o bin create-file throw >r
 2453:   swap >image swap r@ write-file throw
 2454:   r> close-file throw ;
 2455: 
 2456: \ \ minimal definitions
 2457: 	   
 2458: >MINIMAL also minimal
 2459: 
 2460: \ Usefull words                                        13feb93py
 2461: 
 2462: : KB  400 * ;
 2463: 
 2464: \ \ [IF] [ELSE] [THEN] ...				14sep97jaw
 2465: 
 2466: \ it is useful to define our own structures and not to rely
 2467: \ on the words in the compiler
 2468: \ The words in the compiler might be defined with vocabularies
 2469: \ this doesn't work with our self-made compile-loop
 2470: 
 2471: Create parsed 20 chars allot	\ store word we parsed
 2472: 
 2473: : upcase
 2474:     parsed count bounds
 2475:     ?DO I c@ toupper I c! LOOP ;
 2476: 
 2477: : [ELSE]
 2478:     1 BEGIN
 2479: 	BEGIN bl word count dup WHILE
 2480: 	    comment? 20 umin parsed place upcase parsed count
 2481: 	    2dup s" [IF]" compare 0= >r 
 2482: 	    2dup s" [IFUNDEF]" compare 0= >r
 2483: 	    2dup s" [IFDEF]" compare 0= r> or r> or
 2484: 	    IF   2drop 1+
 2485: 	    ELSE 2dup s" [ELSE]" compare 0=
 2486: 		IF   2drop 1- dup
 2487: 		    IF 1+
 2488: 		    THEN
 2489: 		ELSE
 2490: 		    2dup s" [ENDIF]" compare 0= >r
 2491: 		    s" [THEN]" compare 0= r> or
 2492: 		    IF 1- THEN
 2493: 		THEN
 2494: 	    THEN
 2495: 	    ?dup 0= ?EXIT
 2496: 	REPEAT
 2497: 	2drop refill 0=
 2498:     UNTIL drop ; immediate
 2499:   
 2500: : [THEN] ( -- ) ; immediate
 2501: 
 2502: : [ENDIF] ( -- ) ; immediate
 2503: 
 2504: : [IF] ( flag -- )
 2505:     0= IF postpone [ELSE] THEN ; immediate 
 2506: 
 2507: Cond: [IF]      postpone [IF] ;Cond
 2508: Cond: [THEN]    postpone [THEN] ;Cond
 2509: Cond: [ELSE]    postpone [ELSE] ;Cond
 2510: 
 2511: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
 2512: 
 2513: : defined? tdefined? ;
 2514: : needed? needed? ;
 2515: : doer? doer? ;
 2516: 
 2517: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
 2518: 
 2519: : directive? 
 2520:   bl word count [ ' target >wordlist ] literal search-wordlist 
 2521:   dup IF nip THEN ;
 2522: 
 2523: : [IFDEF]  >in @ directive? swap >in !
 2524: 	   0= IF tdefined? ELSE name 2drop true THEN
 2525: 	   postpone [IF] ;
 2526: 
 2527: : [IFUNDEF] tdefined? 0= postpone [IF] ;
 2528: 
 2529: Cond: [IFDEF]   postpone [IFDEF] ;Cond
 2530: 
 2531: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
 2532: 
 2533: \ C: \- \+ Conditional Compiling                         09jun93jaw
 2534: 
 2535: : C: >in @ tdefined? 0=
 2536:      IF    >in ! X :
 2537:      ELSE drop
 2538:         BEGIN bl word dup c@
 2539:               IF   count comment? s" ;" compare 0= ?EXIT
 2540:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
 2541:               THEN
 2542:         AGAIN
 2543:      THEN ;
 2544: 
 2545: : d? d? ;
 2546: 
 2547: \G doesn't skip line when debug switch is on
 2548: : \D D? 0= IF postpone \ THEN ;
 2549: 
 2550: \G interprets the line if word is not defined
 2551: : \- tdefined? IF postpone \ THEN ;
 2552: 
 2553: \G interprets the line if word is defined
 2554: : \+ tdefined? 0= IF postpone \ THEN ;
 2555: 
 2556: Cond: \- \- ;Cond
 2557: Cond: \+ \+ ;Cond
 2558: Cond: \D \D ;Cond
 2559: 
 2560: : ?? bl word find IF execute ELSE drop 0 THEN ;
 2561: 
 2562: : needed:
 2563: \G defines ghost for words that we want to be compiled
 2564:   BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
 2565: 
 2566: \ words that should be in minimal
 2567: 
 2568: create s-buffer 50 chars allot
 2569: 
 2570: bigendian Constant bigendian
 2571: 
 2572: : here there ;
 2573: : equ constant ;
 2574: : mark there constant ;
 2575: 
 2576: \ compiler directives
 2577: : >ram >ram ;
 2578: : >rom >rom ;
 2579: : >auto >auto ;
 2580: : >tempdp >tempdp ;
 2581: : tempdp> tempdp> ;
 2582: : const constflag on ;
 2583: : warnings name 3 = 0= twarnings ! drop ;
 2584: : | ;
 2585: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
 2586: 
 2587: : save-cross save-cross ;
 2588: : save-region save-region ;
 2589: : tdump swap >image swap dump ;
 2590: 
 2591: also forth 
 2592: [IFDEF] Label           : Label defempty? Label ; [THEN] 
 2593: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
 2594: \ [IFDEF] builttag	: builttag builttag ;	[THEN]
 2595: previous
 2596: 
 2597: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
 2598: : + + ;
 2599: : 1+ 1 + ;
 2600: : 2+ 2 + ;
 2601: : 1- 1- ;
 2602: : - - ;
 2603: : and and ;
 2604: : or or ;
 2605: : 2* 2* ;
 2606: : * * ;
 2607: : / / ;
 2608: : dup dup ;
 2609: : over over ;
 2610: : swap swap ;
 2611: : rot rot ;
 2612: : drop drop ;
 2613: : =   = ;
 2614: : 0=   0= ;
 2615: : lshift lshift ;
 2616: : 2/ 2/ ;
 2617: : . . ;
 2618: 
 2619: : all-words    ['] forced?    IS skip? ;
 2620: : needed-words ['] needed?  IS skip? ;
 2621: : undef-words  ['] defined2? IS skip? ;
 2622: : skipdef skipdef ;
 2623: 
 2624: : \  postpone \ ;  immediate
 2625: : \G T-\G ; immediate
 2626: : (  postpone ( ;  immediate
 2627: : include bl word count included ;
 2628: : require require ;
 2629: : .( [char] ) parse type ;
 2630: : ." [char] " parse type ;
 2631: : cr cr ;
 2632: 
 2633: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
 2634: 
 2635: \ only forth also cross also minimal definitions order
 2636: 
 2637: \ cross-compiler words
 2638: 
 2639: : decimal       decimal ;
 2640: : hex           hex ;
 2641: 
 2642: \ : tudp          X tudp ;
 2643: \ : tup           X tup ;
 2644: 
 2645: : doc-off       false to-doc ! ;
 2646: : doc-on        true  to-doc ! ;
 2647: 
 2648: [IFDEF] dbg : dbg dbg ; [THEN]
 2649: 
 2650: \ for debugging...
 2651: : order         order ;
 2652: : hwords         words ;
 2653: : words 	also ghosts words previous ;
 2654: : .s            .s ;
 2655: : bye           bye ;
 2656: 
 2657: \ turnkey direction
 2658: : H forth ; immediate
 2659: : T minimal ; immediate
 2660: : G ghosts ; immediate
 2661: 
 2662: : turnkey 
 2663:    \GFORTH 0 set-order also ghosts
 2664:    \ANSI [ ' ghosts >wordlist ] Literal 1 set-order
 2665:    also target definitions
 2666:    also Minimal also ;
 2667: 
 2668: \ these ones are pefered:
 2669: 
 2670: : lock   turnkey ;
 2671: : unlock previous forth also cross ;
 2672: 
 2673: \ also minimal
 2674: : [[ also unlock ;
 2675: : ]] previous previous also also ;
 2676: 
 2677: unlock definitions also minimal
 2678: : lock   lock ;
 2679: lock
 2680: 
 2681: \ load cross compiler extension defined in mach file
 2682: 
 2683: UNLOCK >CROSS
 2684: 
 2685: [IFDEF] extend-cross extend-cross [THEN]
 2686: 
 2687: LOCK

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