File:  [gforth] / gforth / cross.fs
Revision 1.83: download - view: text, annotated - select for diffs
Fri Dec 31 21:39:59 1999 UTC (19 years, 11 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Started cross compiler documentation
Made 4stack, 8086, and MISC Gforth-EC work again

    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 -- )  bit$ @ swap cell/ +bit ;
 1184: : (reloff) ( taddr -- ) bit$ @ swap cell/ -bit ;
 1185: 
 1186: : (>image) ( taddr -- absaddr ) image @ + ;
 1187: 
 1188: DEFER >image
 1189: DEFER relon
 1190: DEFER reloff
 1191: DEFER correcter
 1192: 
 1193: T has? relocate H
 1194: [IF]
 1195: ' (relon) IS relon
 1196: ' (reloff) IS reloff
 1197: ' (>image) IS >image
 1198: [ELSE]
 1199: ' drop IS relon
 1200: ' drop IS reloff
 1201: ' (>regionimage) IS >image
 1202: [THEN]
 1203: 
 1204: \ Target memory access                                 06oct92py
 1205: 
 1206: : align+  ( taddr -- rest )
 1207:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
 1208: : cfalign+  ( taddr -- rest )
 1209:     \ see kernel.fs:cfaligned
 1210:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
 1211: 
 1212: >TARGET
 1213: : aligned ( taddr -- ta-addr )  dup align+ + ;
 1214: \ assumes cell alignment granularity (as GNU C)
 1215: 
 1216: : cfaligned ( taddr1 -- taddr2 )
 1217:     \ see kernel.fs
 1218:     dup cfalign+ + ;
 1219: 
 1220: : @  ( taddr -- w )     >image S@ ;
 1221: : !  ( w taddr -- )     >image S! ;
 1222: : c@ ( taddr -- char )  >image Sc@ ;
 1223: : c! ( char taddr -- )  >image Sc! ;
 1224: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
 1225: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
 1226: 
 1227: \ Target compilation primitives                        06oct92py
 1228: \ included A!                                          16may93jaw
 1229: 
 1230: : here  ( -- there )    there ;
 1231: : allot ( n -- )        tdp +! ;
 1232: : ,     ( w -- )        T here H tcell T allot  ! H ;
 1233: : c,    ( char -- )     T here H tchar T allot c! H ;
 1234: : align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
 1235: : cfalign ( -- )
 1236:     T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
 1237: 
 1238: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
 1239: : A!                    swap >address swap dup relon T ! H ;
 1240: : A,    ( w -- )        >address T here H relon T , H ;
 1241: 
 1242: >CROSS
 1243: 
 1244: : tcmove ( source dest len -- )
 1245: \G cmove in target memory
 1246:   tchar * bounds
 1247:   ?DO  dup T c@ H I T c! H 1+
 1248:   tchar +LOOP  drop ;
 1249: 
 1250: \ \ Load Assembler
 1251: 
 1252: >TARGET
 1253: H also Forth definitions
 1254: 
 1255: : X 	bl word count [ ' target >wordlist ] Literal search-wordlist
 1256: 	IF	state @ IF compile,
 1257: 		ELSE execute THEN
 1258: 	ELSE	-1 ABORT" Cross: access method not supported!"
 1259: 	THEN ; immediate
 1260: 
 1261: [IFDEF] asm-include asm-include [THEN] hex
 1262: 
 1263: previous
 1264: >CROSS H
 1265: 
 1266: \ \ --------------------        Compiler Plug Ins               01aug97jaw
 1267: 
 1268: \  Compiler States
 1269: 
 1270: Variable comp-state
 1271: 0 Constant interpreting
 1272: 1 Constant compiling
 1273: 2 Constant resolving
 1274: 3 Constant assembling
 1275: 
 1276: Defer lit, ( n -- )
 1277: Defer alit, ( n -- )
 1278: 
 1279: Defer branch, ( target-addr -- )	\ compiles a branch
 1280: Defer ?branch, ( target-addr -- )	\ compiles a ?branch
 1281: Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
 1282: Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
 1283: Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
 1284: Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
 1285: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
 1286: Defer branchfrom, ( -- )		\ ?!
 1287: Defer branchtomark, ( -- target-addr )	\ marks a branch destination
 1288: 
 1289: Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
 1290: Defer colonmark, ( -- addr )		\ marks a colon call
 1291: Defer colon-resolve ( tcfa addr -- )
 1292: 
 1293: Defer addr-resolve ( target-addr addr -- )
 1294: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
 1295: 
 1296: Defer do,	( -- do-token )
 1297: Defer ?do,	( -- ?do-token )
 1298: Defer for,	( -- for-token )
 1299: Defer loop,	( do-token / ?do-token -- )
 1300: Defer +loop,	( do-token / ?do-token -- )
 1301: Defer next,	( for-token )
 1302: 
 1303: [IFUNDEF] ca>native
 1304: defer ca>native	
 1305: [THEN]
 1306: 
 1307: >TARGET
 1308: DEFER >body             \ we need the system >body
 1309: 			\ and the target >body
 1310: >CROSS
 1311: T 2 cells H VALUE xt>body
 1312: DEFER doprim,	\ compiles start of a primitive
 1313: DEFER docol,   	\ compiles start of a colon definition
 1314: DEFER doer,		
 1315: DEFER fini,      \ compiles end of definition ;s
 1316: DEFER doeshandler,
 1317: DEFER dodoes,
 1318: 
 1319: DEFER ]comp     \ starts compilation
 1320: DEFER comp[     \ ends compilation
 1321: 
 1322: : (cc) T a, H ;					' (cc) IS colon,
 1323: 
 1324: : (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
 1325: : (ar) T ! H ;					' (ar) IS addr-resolve
 1326: : (dr)  ( ghost res-pnt target-addr addr )
 1327: 	>tempdp drop over 
 1328: 	dup >magic @ <do:> =
 1329: 	IF 	doer,
 1330: 	ELSE	dodoes,
 1331: 	THEN 
 1332: 	tempdp> ;				' (dr) IS doer-resolve
 1333: 
 1334: : (cm) ( -- addr )
 1335:     T here align H
 1336:     -1 colon, ;					' (cm) IS colonmark,
 1337: 
 1338: >TARGET
 1339: : compile, colon, ;
 1340: >CROSS
 1341: 
 1342: \ resolve structure
 1343: 
 1344: : >next ;		\ link to next field
 1345: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
 1346: : >taddr cell+ cell+ ;	
 1347: : >ghost 3 cells + ;
 1348: : >file 4 cells + ;
 1349: : >line 5 cells + ;
 1350: 
 1351: : (refered) ( ghost addr tag -- )
 1352: \G creates a reference to ghost at address taddr
 1353:     rot >r here r@ >link @ , r> >link ! 
 1354:     ( taddr tag ) ,
 1355:     ( taddr ) , 
 1356:     last-header-ghost @ , 
 1357:     loadfile , 
 1358:     sourceline# , 
 1359:   ;
 1360: 
 1361: : refered ( ghost tag -- )
 1362: \G creates a resolve structure
 1363:     T here aligned H swap (refered)
 1364:   ;
 1365: 
 1366: : killref ( addr ghost -- )
 1367: \G kills a forward reference to ghost at position addr
 1368: \G this is used to eleminate a :dovar refence after making a DOES>
 1369:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
 1370:     swap >r >link
 1371:     BEGIN dup @ dup  ( addr last this )
 1372:     WHILE dup >taddr @ r@ =
 1373:  	 IF   @ over !
 1374: 	 ELSE nip THEN
 1375:     REPEAT rdrop 2drop 
 1376:   ;
 1377: 
 1378: Defer resolve-warning
 1379: 
 1380: : reswarn-test ( ghost res-struct -- ghost res-struct )
 1381:   over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
 1382: 
 1383: : reswarn-forward ( ghost res-struct -- ghost res-struct )
 1384:   over warnhead .ghost dup ."  is referenced in " 
 1385:   >ghost @ .ghost ;
 1386: 
 1387: \ ' reswarn-test IS resolve-warning
 1388:  
 1389: \ resolve                                              14oct92py
 1390: 
 1391:  : resolve-loop ( ghost resolve-list tcfa -- )
 1392:     >r
 1393:     BEGIN dup WHILE 
 1394: \  	  dup >tag @ 2 = IF reswarn-forward THEN
 1395: 	  resolve-warning 
 1396: 	  r@ over >taddr @ 
 1397: 	  2 pick >tag @
 1398: 	  CASE	0 OF colon-resolve ENDOF
 1399: 		1 OF addr-resolve ENDOF
 1400: 		2 OF doer-resolve ENDOF
 1401: 	  ENDCASE
 1402: 	  @ \ next list element
 1403:     REPEAT 2drop rdrop 
 1404:   ;
 1405: 
 1406: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
 1407: \  >r dup >link @
 1408: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
 1409: 
 1410: \ exists                                                9may93jaw
 1411: 
 1412: Variable TWarnings
 1413: TWarnings on
 1414: Variable Exists-Warnings
 1415: Exists-Warnings on
 1416: 
 1417: : exists ( ghost tcfa -- )
 1418:   over GhostNames
 1419:   BEGIN @ dup
 1420:   WHILE 2dup cell+ @ =
 1421:   UNTIL
 1422:         2 cells + count
 1423:         TWarnings @ Exists-Warnings @ and
 1424:         IF warnhead type ."  exists"
 1425:         ELSE 2drop THEN
 1426:         drop swap >link !
 1427:   ELSE  true abort" CROSS: Ghostnames inconsistent "
 1428:   THEN ;
 1429: 
 1430: : resolve  ( ghost tcfa -- )
 1431: \G resolve referencies to ghost with tcfa
 1432:     \ is ghost resolved?, second resolve means another definition with the
 1433:     \ same name
 1434:     over undefined? 0= IF  exists EXIT THEN
 1435:     \ get linked-list
 1436:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
 1437:     \ mark ghost as resolved
 1438:     dup r@ >link ! <res> r@ >magic !
 1439:     \ loop through forward referencies
 1440:     r> -rot 
 1441:     comp-state @ >r Resolving comp-state !
 1442:     resolve-loop 
 1443:     r> comp-state !
 1444: 
 1445:     ['] noop IS resolve-warning 
 1446:   ;
 1447: 
 1448: \ gexecute ghost,                                      01nov92py
 1449: 
 1450: : is-forward   ( ghost -- )
 1451:   colonmark, 0 (refered) ; \ compile space for call
 1452: 
 1453: : is-resolved   ( ghost -- )
 1454:   >link @ colon, ; \ compile-call
 1455: 
 1456: : gexecute   ( ghost -- )
 1457:   dup @ <fwd> = IF  is-forward  ELSE  is-resolved  THEN ;
 1458: 
 1459: : addr,  ( ghost -- )
 1460:   dup @ <fwd> = IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1461: 
 1462: \ !! : ghost,     ghost  gexecute ;
 1463: 
 1464: \ .unresolved                                          11may93jaw
 1465: 
 1466: variable ResolveFlag
 1467: 
 1468: \ ?touched                                             11may93jaw
 1469: 
 1470: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1471:                                0 <> and ;
 1472: 
 1473: : .forwarddefs ( ghost -- )
 1474: 	."  appeared in:"
 1475: 	>link
 1476: 	BEGIN	@ dup
 1477: 	WHILE	cr 5 spaces
 1478: 		dup >ghost @ .ghost
 1479: 		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 1480: 		."  line " dup >line @ .dec
 1481: 	REPEAT 
 1482: 	drop ;
 1483: 
 1484: : ?resolved  ( ghostname -- )
 1485:   dup cell+ @ ?touched
 1486:   IF  	dup 
 1487: 	cell+ cell+ count cr type ResolveFlag on 
 1488: 	cell+ @ .forwarddefs
 1489:   ELSE 	drop 
 1490:   THEN ;
 1491: 
 1492: : .unresolved  ( -- )
 1493:   ResolveFlag off cr ." Unresolved: "
 1494:   Ghostnames
 1495:   BEGIN @ dup
 1496:   WHILE dup ?resolved
 1497:   REPEAT drop ResolveFlag @
 1498:   IF
 1499:       -1 abort" Unresolved words!"
 1500:   ELSE
 1501:       ." Nothing!"
 1502:   THEN
 1503:   cr ;
 1504: 
 1505: : .stats
 1506:   base @ >r decimal
 1507:   cr ." named Headers: " headers-named @ . 
 1508:   r> base ! ;
 1509: 
 1510: >MINIMAL
 1511: 
 1512: : .unresolved .unresolved ;
 1513: 
 1514: >CROSS
 1515: \ Header states                                        12dec92py
 1516: 
 1517: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
 1518: 
 1519: VARIABLE ^imm
 1520: 
 1521: >TARGET
 1522: : immediate     40 flag!
 1523:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 1524:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 1525:                 <imm> ^imm @ ! ;
 1526: : restrict      20 flag! ;
 1527: 
 1528: : isdoer	
 1529: \G define a forth word as doer, this makes obviously only sence on
 1530: \G forth processors such as the PSC1000
 1531: 		<do:> last-header-ghost @ >magic ! ;
 1532: >CROSS
 1533: 
 1534: \ Target Header Creation                               01nov92py
 1535: 
 1536: >TARGET
 1537: : string,  ( addr count -- )
 1538:   dup T c, H bounds  ?DO  I c@ T c, H  LOOP ; 
 1539: : name,  ( "name" -- )  bl word count T string, cfalign H ;
 1540: : view,   ( -- ) ( dummy ) ;
 1541: >CROSS
 1542: 
 1543: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
 1544: 
 1545: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
 1546: \ contains the file-id of the documentation file
 1547: 
 1548: : T-\G ( -- )
 1549:     source >in @ /string doc-file-id write-line throw
 1550:     postpone \ ;
 1551: 
 1552: Variable to-doc  to-doc on
 1553: 
 1554: : cross-doc-entry  ( -- )
 1555:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
 1556:     IF
 1557: 	s" " doc-file-id write-line throw
 1558: 	s" make-doc " doc-file-id write-file throw
 1559: 
 1560: 	tlast @ >image count 1F and doc-file-id write-file throw
 1561: 	>in @
 1562: 	[char] ( parse 2drop
 1563: 	[char] ) parse doc-file-id write-file throw
 1564: 	s"  )" doc-file-id write-file throw
 1565: 	[char] \ parse 2drop					
 1566: 	T-\G
 1567: 	>in !
 1568:     THEN ;
 1569: 
 1570: \ Target TAGS creation
 1571: 
 1572: s" kernel.TAGS" r/w create-file throw value tag-file-id
 1573: \ contains the file-id of the tags file
 1574: 
 1575: Create tag-beg 2 c,  7F c, bl c,
 1576: Create tag-end 2 c,  bl c, 01 c,
 1577: Create tag-bof 1 c,  0C c,
 1578: 
 1579: 2variable last-loadfilename 0 0 last-loadfilename 2!
 1580: 	    
 1581: : put-load-file-name ( -- )
 1582:     sourcefilename last-loadfilename 2@ d<>
 1583:     IF
 1584: 	tag-bof count tag-file-id write-line throw
 1585: 	sourcefilename 2dup
 1586: 	tag-file-id write-file throw
 1587: 	last-loadfilename 2!
 1588: 	s" ,0" tag-file-id write-line throw
 1589:     THEN ;
 1590: 
 1591: : cross-tag-entry  ( -- )
 1592:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 1593:     IF
 1594: 	put-load-file-name
 1595: 	source >in @ min tag-file-id write-file throw
 1596: 	tag-beg count tag-file-id write-file throw
 1597: 	tlast @ >image count 1F and tag-file-id write-file throw
 1598: 	tag-end count tag-file-id write-file throw
 1599: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
 1600: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
 1601: 	s" ,0" tag-file-id write-line throw
 1602: 	base !
 1603:     THEN ;
 1604: 
 1605: \ Check for words
 1606: 
 1607: Defer skip? ' false IS skip?
 1608: 
 1609: : skipdef ( <name> -- )
 1610: \G skip definition of an undefined word in undef-words and
 1611: \G all-words mode
 1612:     ghost dup forward?
 1613:     IF  >magic <skip> swap !
 1614:     ELSE drop THEN ;
 1615: 
 1616: : tdefined? ( -- flag ) \ name
 1617:     ghost undefined? 0= ;
 1618: 
 1619: : defined2? ( -- flag ) \ name
 1620: \G return true for anything else than forward, even for <skip>
 1621: \G that's what we want
 1622:     ghost forward? 0= ;
 1623: 
 1624: : forced? ( -- flag ) \ name
 1625: \G return ture if it is a foreced skip with defskip
 1626:     ghost >magic @ <skip> = ;
 1627: 
 1628: : needed? ( -- flag ) \ name
 1629: \G returns a false flag when
 1630: \G a word is not defined
 1631: \G a forward reference exists
 1632: \G so the definition is not skipped!
 1633:     bl word gfind
 1634:     IF dup undefined?
 1635: 	nip
 1636: 	0=
 1637:     ELSE  drop true  THEN ;
 1638: 
 1639: : doer? ( -- flag ) \ name
 1640:     ghost >magic @ <do:> = ;
 1641: 
 1642: : skip-defs ( -- )
 1643:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
 1644: 
 1645: \ Target header creation
 1646: 
 1647: Variable NoHeaderFlag
 1648: NoHeaderFlag off
 1649: 
 1650: : 0.r ( n1 n2 -- ) 
 1651:     base @ >r hex 
 1652:     0 swap <# 0 ?DO # LOOP #> type 
 1653:     r> base ! ;
 1654: : .sym
 1655:   bounds 
 1656:   DO I c@ dup
 1657: 	CASE	[char] / OF drop ." \/" ENDOF
 1658: 		[char] \ OF drop ." \\" ENDOF
 1659: 		dup OF emit ENDOF
 1660: 	ENDCASE
 1661:     LOOP ;
 1662: 
 1663: : (Theader ( "name" -- ghost )
 1664:     \  >in @ bl word count type 2 spaces >in !
 1665:     \ wordheaders will always be compiled to rom
 1666:     switchrom
 1667:     \ build header in target
 1668:     NoHeaderFlag @
 1669:     IF  NoHeaderFlag off
 1670:     ELSE
 1671: 	T align H view,
 1672: 	tlast @ dup 0> IF tcell - THEN T A, H  there tlast !
 1673: 	1 headers-named +!	\ Statistic
 1674: 	>in @ T name, H >in !
 1675:     THEN
 1676:     T cfalign here H tlastcfa !
 1677:     \ Symbol table
 1678: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
 1679:     ghost
 1680:     dup Last-Header-Ghost !
 1681:     dup >magic ^imm !     \ a pointer for immediate
 1682:     Already @
 1683:     IF  dup >end tdoes !
 1684:     ELSE 0 tdoes !
 1685:     THEN
 1686:     80 flag!
 1687:     cross-doc-entry cross-tag-entry ;
 1688: 
 1689: VARIABLE ;Resolve 1 cells allot
 1690: \ this is the resolver information from ":"
 1691: \ resolving is done by ";"
 1692: 
 1693: : Theader  ( "name" -- ghost )
 1694:   (THeader dup there resolve 0 ;Resolve ! ;
 1695: 
 1696: >TARGET
 1697: : Alias    ( cfa -- ) \ name
 1698:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1699:     dup 0< s" prims" T $has? H 0= and
 1700:     IF
 1701: 	.sourcepos ." needs prim: " >in @ bl word count type >in ! cr
 1702:     THEN
 1703:     (THeader over resolve T A, H 80 flag! ;
 1704: : Alias:   ( cfa -- ) \ name
 1705:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1706:     dup 0< s" prims" T $has? H 0= and
 1707:     IF
 1708: 	.sourcepos ." needs doer: " >in @ bl word count type >in ! cr
 1709:     THEN
 1710:     ghost tuck swap resolve <do:> swap >magic ! ;
 1711: 
 1712: Variable prim#
 1713: : first-primitive ( n -- )  prim# ! ;
 1714: : Primitive  ( -- ) \ name
 1715:     prim# @ T Alias H  -1 prim# +! ;
 1716: >CROSS
 1717: 
 1718: \ Conditionals and Comments                            11may93jaw
 1719: 
 1720: : ;Cond
 1721:   postpone ;
 1722:   swap ! ;  immediate
 1723: 
 1724: : Cond: ( -- ) \ name {code } ;
 1725:   atonce on
 1726:   ghost
 1727:   >exec
 1728:   :NONAME ;
 1729: 
 1730: : restrict? ( -- )
 1731: \ aborts on interprete state - ae
 1732:   state @ 0= ABORT" CROSS: Restricted" ;
 1733: 
 1734: : Comment ( -- )
 1735:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
 1736: 
 1737: Comment (       Comment \
 1738: 
 1739: \ compile                                              10may93jaw
 1740: 
 1741: : compile  ( -- ) \ name
 1742:   restrict?
 1743:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
 1744:   0> ( immediate? )
 1745:   IF    >exec @ compile,
 1746:   ELSE  postpone literal postpone gexecute  THEN ;
 1747:                                         immediate
 1748: 
 1749: : [G'] 
 1750: \G ticks a ghost and returns its address
 1751:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1752:   state @
 1753:   IF   postpone literal
 1754:   THEN ; immediate
 1755: 
 1756: : ghost>cfa
 1757:   dup undefined? ABORT" CROSS: forward " >link @ ;
 1758:                
 1759: >TARGET
 1760: 
 1761: : '  ( -- cfa ) 
 1762: \ returns the target-cfa of a ghost
 1763:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1764:   ghost>cfa ;
 1765: 
 1766: Cond: [']  T ' H alit, ;Cond
 1767: 
 1768: >CROSS
 1769: 
 1770: : [T']
 1771: \ returns the target-cfa of a ghost, or compiles it as literal
 1772:   postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
 1773: 
 1774: \ \ threading modell					13dec92py
 1775: \ modularized						14jun97jaw
 1776: 
 1777: : fillcfa   ( usedcells -- )
 1778:   T cells H xt>body swap - 0 ?DO 0 X c, tchar +LOOP ;
 1779: 
 1780: : (>body)   ( cfa -- pfa ) xt>body + ;		' (>body) T IS >body H
 1781: 
 1782: : (doer,)   ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ;   ' (doer,) IS doer,
 1783: 
 1784: : (docol,)  ( -- ) [G'] :docol doer, ;		' (docol,) IS docol,
 1785: 
 1786: : (doprim,) ( -- )
 1787:   there xt>body + ca>native T a, H 1 fillcfa ;	' (doprim,) IS doprim,
 1788: 
 1789: : (doeshandler,) ( -- ) 
 1790:   T cfalign H compile :doesjump T 0 , H ; 	' (doeshandler,) IS doeshandler,
 1791: 
 1792: : (dodoes,) ( does-action-ghost -- )
 1793:   ]comp [G'] :dodoes gexecute comp[
 1794:   addr,
 1795:   T here H tcell - reloff 2 fillcfa ;		' (dodoes,) IS dodoes,
 1796: 
 1797: : (lit,) ( n -- )   compile lit T  ,  H ;	' (lit,) IS lit,
 1798: 
 1799: \ if we dont produce relocatable code alit, defaults to lit, jaw
 1800: has? relocate
 1801: [IF]
 1802: : (alit,) ( n -- )  compile lit T  a, H ;	' (alit,) IS alit,
 1803: [ELSE]
 1804: : (alit,) ( n -- )  lit, ;			' (alit,) IS alit,
 1805: [THEN]
 1806: 
 1807: : (fini,)         compile ;s ;                ' (fini,) IS fini,
 1808: 
 1809: [IFUNDEF] (code) 
 1810: Defer (code)
 1811: Defer (end-code)
 1812: [THEN]
 1813: 
 1814: >TARGET
 1815: : Code
 1816:   defempty?
 1817:   (THeader there resolve
 1818:   [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
 1819:   doprim, 
 1820:   [THEN]
 1821:   depth (code) ;
 1822: 
 1823: : Code:
 1824:   defempty?
 1825:     ghost dup there ca>native resolve  <do:> swap >magic !
 1826:     depth (code) ;
 1827: 
 1828: : end-code
 1829:     (end-code)
 1830:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1831:     ELSE true ABORT" CROSS: Stack empty" THEN
 1832:     ;
 1833: 
 1834: >CROSS
 1835: 
 1836: \ tLiteral                                             12dec92py
 1837: 
 1838: >TARGET
 1839: Cond: \G  T-\G ;Cond
 1840: 
 1841: Cond:  Literal ( n -- )   restrict? lit, ;Cond
 1842: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
 1843: 
 1844: : Char ( "<char>" -- )  bl word char+ c@ ;
 1845: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
 1846: 
 1847: \ some special literals					27jan97jaw
 1848: 
 1849: \ !! Known Bug: Special Literals and plug-ins work only correct
 1850: \ on 16 and 32 Bit Targets and 32 Bit Hosts!
 1851: 
 1852: Cond: MAXU
 1853:   restrict? 
 1854:   tcell 1 cells u> 
 1855:   IF	compile lit tcell 0 ?DO FF T c, H LOOP 
 1856:   ELSE	ffffffff lit, THEN
 1857:   ;Cond
 1858: 
 1859: Cond: MINI
 1860:   restrict?
 1861:   tcell 1 cells u>
 1862:   IF	compile lit bigendian 
 1863:   	IF	80 T c, H tcell 1 ?DO 0 T c, H LOOP 
 1864:   	ELSE  	tcell 1 ?DO 0 T c, H LOOP 80 T c, H
 1865:   	THEN
 1866:   ELSE	tcell 2 = IF 8000 ELSE 80000000 THEN lit, THEN
 1867:   ;Cond
 1868:  
 1869: Cond: MAXI
 1870:  restrict?
 1871:  tcell 1 cells u>
 1872:  IF	compile lit bigendian 
 1873: 	IF 	7F T c, H tcell 1 ?DO FF T c, H LOOP
 1874: 	ELSE 	tcell 1 ?DO FF T c, H LOOP 7F T c, H
 1875:  	THEN
 1876:  ELSE	tcell 2 = IF 7fff ELSE 7fffffff THEN lit, THEN
 1877:  ;Cond
 1878: 
 1879: >CROSS
 1880: \ Target compiling loop                                12dec92py
 1881: \ ">tib trick thrown out                               10may93jaw
 1882: \ number? defined at the top                           11may93jaw
 1883: \ replaced >in by save-input				
 1884: 
 1885: : discard 0 ?DO drop LOOP ;
 1886: 
 1887: \ compiled word might leave items on stack!
 1888: : tcom ( x1 .. xn n name -- )
 1889: \  dup count type space
 1890:   gfind  ?dup
 1891:   IF    >r >r discard r> r>
 1892: 	0> IF	>exec @ execute
 1893: 	ELSE	gexecute  THEN 
 1894: 	EXIT 
 1895:   THEN
 1896:   number? dup  
 1897:   IF	0> IF swap lit,  THEN  lit, discard
 1898:   ELSE	2drop restore-input throw ghost gexecute THEN  ;
 1899: 
 1900: >TARGET
 1901: \ : ; DOES>                                            13dec92py
 1902: \ ]                                                     9may93py/jaw
 1903: 
 1904: : ] state on
 1905:     Compiling comp-state !
 1906:     BEGIN
 1907:         BEGIN save-input bl word
 1908:               dup c@ 0= WHILE drop discard refill 0=
 1909:               ABORT" CROSS: End of file while target compiling"
 1910:         REPEAT
 1911:         tcom
 1912:         state @
 1913:         0=
 1914:     UNTIL ;
 1915: 
 1916: \ by the way: defining a second interpreter (a compiler-)loop
 1917: \             is not allowed if a system should be ans conform
 1918: 
 1919: : : ( -- colon-sys ) \ Name
 1920:   defempty?
 1921:   constflag off \ don't let this flag work over colon defs
 1922: 		\ just to go sure nothing unwanted happens
 1923:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
 1924:   (THeader ;Resolve ! there ;Resolve cell+ !
 1925:   docol, ]comp depth T ] H ;
 1926: 
 1927: : :noname ( -- colon-sys )
 1928:   T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
 1929: 
 1930: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
 1931: 
 1932: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
 1933: 
 1934: >CROSS
 1935: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
 1936:          ;Resolve cell+ @ ;
 1937: 
 1938: >TARGET
 1939: 
 1940: Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
 1941: 
 1942: Cond: ; ( -- ) restrict?
 1943:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1944:                           ELSE true ABORT" CROSS: Stack empty" THEN
 1945:                fini,
 1946:                comp[
 1947:                state off
 1948:                ;Resolve @
 1949:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
 1950: 		Interpreting comp-state !
 1951:                ;Cond
 1952: Cond: [  restrict? state off Interpreting comp-state ! ;Cond
 1953: 
 1954: >CROSS
 1955: 
 1956: Create GhostDummy ghostheader
 1957: <res> GhostDummy >magic !
 1958: 
 1959: : !does ( does-action -- )
 1960: \ !! zusammenziehen und dodoes, machen!
 1961:     tlastcfa @ [G'] :dovar killref
 1962: \    tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
 1963: \ !! geht so nicht, da dodoes, ghost will!
 1964:     GhostDummy >link ! GhostDummy 
 1965:     tlastcfa @ >tempdp dodoes, tempdp> ;
 1966: 
 1967: >TARGET
 1968: Cond: DOES> restrict?
 1969:         compile (does>) doeshandler, 
 1970: 	\ resolve words made by builders
 1971: 	tdoes @ ?dup IF  @ T here H resolve THEN
 1972:         ;Cond
 1973: : DOES> switchrom doeshandler, T here H !does depth T ] H ;
 1974: 
 1975: >CROSS
 1976: \ Creation                                             01nov92py
 1977: 
 1978: \ Builder                                               11may93jaw
 1979: 
 1980: : Builder    ( Create-xt do:-xt "name" -- )
 1981: \ builds up a builder in current vocabulary
 1982: \ create-xt is executed when word is interpreted
 1983: \ do:-xt is executet when the created word from builder is executed
 1984: \ for do:-xt an additional entry after the normal ghost-enrys is used
 1985: 
 1986:   Make-Ghost 		( Create-xt do:-xt ghost )
 1987:   rot swap		( do:-xt Create-xt ghost )
 1988:   >exec ! , ;
 1989: \  rot swap >exec dup @ ['] NoExec <>
 1990: \  IF 2drop ELSE ! THEN , ;
 1991: 
 1992: : gdoes,  ( ghost -- )
 1993: \ makes the codefield for a word that is built
 1994:   >end @ dup undefined? 0=
 1995:   IF
 1996: 	dup >magic @ <do:> =
 1997: 	IF 	 doer, 
 1998: 	ELSE	dodoes,
 1999: 	THEN
 2000: 	EXIT
 2001:   THEN
 2002: \  compile :dodoes gexecute
 2003: \  T here H tcell - reloff 
 2004:   2 refered 
 2005:   0 fillcfa
 2006:   ;
 2007: 
 2008: : TCreate ( <name> -- )
 2009:   executed-ghost @
 2010:   create-forward-warn
 2011:   IF ['] reswarn-forward IS resolve-warning THEN
 2012:   Theader >r dup gdoes,
 2013: \ stores execution semantic in the built word
 2014: \ if the word already has a semantic (concerns S", IS, .", DOES>)
 2015: \ then keep it
 2016:   >end @ >exec @ r> >exec dup @ ['] NoExec =
 2017:   IF ! ELSE 2drop THEN ;
 2018: 
 2019: : RTCreate ( <name> -- )
 2020: \ creates a new word with code-field in ram
 2021:   executed-ghost @
 2022:   create-forward-warn
 2023:   IF ['] reswarn-forward IS resolve-warning THEN
 2024:   \ make Alias
 2025:   (THeader there 0 T a, H 80 flag! ( S executed-ghost new-ghost )
 2026:   \ store  poiter to code-field
 2027:   switchram T cfalign H
 2028:   there swap T ! H
 2029:   there tlastcfa ! 
 2030:   dup there resolve 0 ;Resolve !
 2031:   >r dup gdoes,
 2032: \ stores execution semantic in the built word
 2033: \ if the word already has a semantic (concerns S", IS, .", DOES>)
 2034: \ then keep it
 2035:   >end @ >exec @ r> >exec dup @ ['] NoExec =
 2036:   IF ! ELSE 2drop THEN ;
 2037: 
 2038: : Build:  ( -- [xt] [colon-sys] )
 2039:   :noname postpone TCreate ;
 2040: 
 2041: : BuildSmart:  ( -- [xt] [colon-sys] )
 2042:   :noname
 2043:   [ T has? rom H [IF] ]
 2044:   postpone RTCreate
 2045:   [ [ELSE] ]
 2046:   postpone TCreate 
 2047:   [ [THEN] ] ;
 2048: 
 2049: : gdoes>  ( ghost -- addr flag )
 2050:   executed-ghost @
 2051:   state @ IF  gexecute true EXIT  THEN
 2052:   >link @ T >body H false ;
 2053: 
 2054: \ DO: ;DO                                               11may93jaw
 2055: \ changed to ?EXIT                                      10may93jaw
 2056: 
 2057: : DO:     ( -- addr [xt] [colon-sys] )
 2058:   here ghostheader
 2059:   :noname postpone gdoes> postpone ?EXIT ;
 2060: 
 2061: : by:     ( -- addr [xt] [colon-sys] ) \ name
 2062:   ghost
 2063:   :noname postpone gdoes> postpone ?EXIT ;
 2064: 
 2065: : ;DO ( addr [xt] [colon-sys] -- addr )
 2066:   postpone ;    ( S addr xt )
 2067:   over >exec ! ; immediate
 2068: 
 2069: : by      ( -- addr ) \ Name
 2070:   ghost >end @ ;
 2071: 
 2072: >TARGET
 2073: \ Variables and Constants                              05dec92py
 2074: 
 2075: Build:  ( n -- ) ;
 2076: by: :docon ( ghost -- n ) T @ H ;DO
 2077: Builder (Constant)
 2078: 
 2079: Build:  ( n -- ) T , H ;
 2080: by (Constant)
 2081: Builder Constant
 2082: 
 2083: Build:  ( n -- ) T A, H ;
 2084: by (Constant)
 2085: Builder AConstant
 2086: 
 2087: Build:  ( d -- ) T , , H ;
 2088: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
 2089: Builder 2Constant
 2090: 
 2091: BuildSmart: ;
 2092: by: :dovar ( ghost -- addr ) ;DO
 2093: Builder Create
 2094: 
 2095: T has? rom H [IF]
 2096: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 2097: by (Constant)
 2098: Builder Variable
 2099: [ELSE]
 2100: Build: T 0 , H ;
 2101: by Create
 2102: Builder Variable
 2103: [THEN]
 2104: 
 2105: T has? rom H [IF]
 2106: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
 2107: by (Constant)
 2108: Builder 2Variable
 2109: [ELSE]
 2110: Build: T 0 , 0 , H ;
 2111: by Create
 2112: Builder 2Variable
 2113: [THEN]
 2114: 
 2115: T has? rom H [IF]
 2116: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 2117: by (Constant)
 2118: Builder AVariable
 2119: [ELSE]
 2120: Build: T 0 A, H ;
 2121: by Create
 2122: Builder AVariable
 2123: [THEN]
 2124: 
 2125: \ User variables                                       04may94py
 2126: 
 2127: >CROSS
 2128: 
 2129: Variable tup  0 tup !
 2130: Variable tudp 0 tudp !
 2131: 
 2132: : u,  ( n -- udp )
 2133:   tup @ tudp @ + T  ! H
 2134:   tudp @ dup T cell+ H tudp ! ;
 2135: 
 2136: : au, ( n -- udp )
 2137:   tup @ tudp @ + T A! H
 2138:   tudp @ dup T cell+ H tudp ! ;
 2139: 
 2140: >TARGET
 2141: 
 2142: Build: 0 u, X , ;
 2143: by: :douser ( ghost -- up-addr )  X @ tup @ + ;DO
 2144: Builder User
 2145: 
 2146: Build: 0 u, X , 0 u, drop ;
 2147: by User
 2148: Builder 2User
 2149: 
 2150: Build: 0 au, X , ;
 2151: by User
 2152: Builder AUser
 2153: 
 2154: BuildSmart: T , H ;
 2155: by (Constant)
 2156: Builder Value
 2157: 
 2158: BuildSmart: T A, H ;
 2159: by (Constant)
 2160: Builder AValue
 2161: 
 2162: BuildSmart:  ( -- ) [T'] noop T A, H ;
 2163: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2164: Builder Defer
 2165: 
 2166: Build: ( inter comp -- ) swap T immediate A, A, H ;
 2167: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2168: Builder interpret/compile:
 2169: 
 2170: \ Sturctures                                           23feb95py
 2171: 
 2172: >CROSS
 2173: : nalign ( addr1 n -- addr2 )
 2174: \ addr2 is the aligned version of addr1 wrt the alignment size n
 2175:  1- tuck +  swap invert and ;
 2176: >TARGET
 2177: 
 2178: Build: ;
 2179: by: :dofield T @ H + ;DO
 2180: Builder (Field)
 2181: 
 2182: Build: ( align1 offset1 align size "name" --  align2 offset2 )
 2183:     rot dup T , H ( align1 align size offset1 )
 2184:     + >r nalign r> ;
 2185: by (Field)
 2186: Builder Field
 2187: 
 2188: : struct  T 1 chars 0 H ;
 2189: : end-struct  T 2Constant H ;
 2190: 
 2191: : cell% ( n -- size align )
 2192:     T 1 cells H dup ;
 2193: 
 2194: \ structural conditionals                              17dec92py
 2195: 
 2196: >CROSS
 2197: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
 2198: : sys?        ( sys -- sys )    dup 0= ?struc ;
 2199: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
 2200: 
 2201: : branchoffset ( src dest -- )  - tchar / ; \ ?? jaw
 2202: 
 2203: : >resolve    ( sys -- )        
 2204: 	X here ( dup ." >" hex. ) over branchoffset swap X ! ;
 2205: 
 2206: : <resolve    ( sys -- )
 2207: 	X here ( dup ." <" hex. ) branchoffset X , ;
 2208: 
 2209: :noname compile branch X here branchoffset X , ;
 2210:   IS branch, ( target-addr -- )
 2211: :noname compile ?branch X here branchoffset X , ;
 2212:   IS ?branch, ( target-addr -- )
 2213: :noname compile branch T here 0 , H ;
 2214:   IS branchmark, ( -- branchtoken )
 2215: :noname compile ?branch T here 0 , H ;
 2216:   IS ?branchmark, ( -- branchtoken )
 2217: :noname T here 0 , H ;
 2218:   IS ?domark, ( -- branchtoken )
 2219: :noname dup X @ ?struc X here over branchoffset swap X ! ;
 2220:   IS branchtoresolve, ( branchtoken -- )
 2221: :noname branchto, X here ;
 2222:   IS branchtomark, ( -- target-addr )
 2223: 
 2224: >TARGET
 2225: 
 2226: \ Structural Conditionals                              12dec92py
 2227: 
 2228: Cond: BUT       restrict? sys? swap ;Cond
 2229: Cond: YET       restrict? sys? dup ;Cond
 2230: 
 2231: >CROSS
 2232: 
 2233: Variable tleavings 0 tleavings !
 2234: 
 2235: : (done) ( addr -- )
 2236:     tleavings @
 2237:     BEGIN  dup
 2238:     WHILE
 2239: 	>r dup r@ cell+ @ \ address of branch
 2240: 	u> 0=	   \ lower than DO?	
 2241:     WHILE
 2242: 	r@ 2 cells + @ \ branch token
 2243: 	branchtoresolve,
 2244: 	r@ @ r> free throw
 2245:     REPEAT  r>  THEN
 2246:     tleavings ! drop ;
 2247: 
 2248: >TARGET
 2249: 
 2250: Cond: DONE   ( addr -- )  restrict? (done) ;Cond
 2251: 
 2252: >CROSS
 2253: : (leave) ( branchtoken -- )
 2254:     3 cells allocate throw >r
 2255:     T here H r@ cell+ !
 2256:     r@ 2 cells + !
 2257:     tleavings @ r@ !
 2258:     r> tleavings ! ;
 2259: >TARGET
 2260: 
 2261: Cond: LEAVE     restrict? branchmark, (leave) ;Cond
 2262: Cond: ?LEAVE    restrict? compile 0=  ?branchmark, (leave)  ;Cond
 2263: 
 2264: >CROSS
 2265: \ !!JW ToDo : Move to general tools section
 2266: 
 2267: : to1 ( x1 x2 xn n -- addr )
 2268: \G packs n stack elements in a allocated memory region
 2269:    dup dup 1+ cells allocate throw dup >r swap 1+
 2270:    0 DO tuck ! cell+ LOOP
 2271:    drop r> ;
 2272: : 1to ( addr -- x1 x2 xn )
 2273: \G unpacks the elements saved by to1
 2274:     dup @ swap over cells + swap
 2275:     0 DO  dup @ swap 1 cells -  LOOP
 2276:     free throw ;
 2277: 
 2278: : loop]     branchto, dup <resolve tcell - (done) ;
 2279: 
 2280: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
 2281: 
 2282: >TARGET
 2283: 
 2284: \ Structural Conditionals                              12dec92py
 2285: 
 2286: >TARGET
 2287: Cond: AHEAD     restrict? branchmark, ;Cond
 2288: Cond: IF        restrict? ?branchmark, ;Cond
 2289: Cond: THEN      restrict? sys? branchto, branchtoresolve, ;Cond
 2290: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
 2291: 
 2292: Cond: BEGIN     restrict? branchtomark, ;Cond
 2293: Cond: WHILE     restrict? sys? compile IF swap ;Cond
 2294: Cond: AGAIN     restrict? sys? branch, ;Cond
 2295: Cond: UNTIL     restrict? sys? ?branch, ;Cond
 2296: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
 2297: 
 2298: Cond: CASE      restrict? 0 ;Cond
 2299: Cond: OF        restrict? 1+ >r compile over compile =
 2300:                 compile IF compile drop r> ;Cond
 2301: Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
 2302: Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond
 2303: 
 2304: \ Structural Conditionals                              12dec92py
 2305: 
 2306: :noname \ ?? i think 0 is too much! jaw
 2307:     0 compile (do)
 2308:     branchtomark,  2 to1 ;
 2309:   IS do, ( -- target-addr )
 2310: 
 2311: \ :noname
 2312: \     compile 2dup compile = compile IF
 2313: \     compile 2drop compile ELSE
 2314: \     compile (do) branchtomark, 2 to1 ;
 2315: \   IS ?do,
 2316:     
 2317: :noname
 2318:     0 compile (?do)  ?domark, (leave)
 2319:     branchtomark,  2 to1 ;
 2320:   IS ?do, ( -- target-addr )
 2321: :noname compile (for) branchtomark, ;
 2322:   IS for, ( -- target-addr )
 2323: :noname 1to compile (loop)  loop] compile unloop skiploop] ;
 2324:   IS loop, ( target-addr -- )
 2325: :noname 1to compile (+loop)  loop] compile unloop skiploop] ;
 2326:   IS +loop, ( target-addr -- )
 2327: :noname compile (next)  loop] compile unloop ;
 2328:   IS next, ( target-addr -- )
 2329: 
 2330: Cond: DO      	restrict? do, ;Cond
 2331: Cond: ?DO     	restrict? ?do, ;Cond
 2332: Cond: FOR	restrict? for, ;Cond
 2333: 
 2334: Cond: LOOP	restrict? sys? loop, ;Cond
 2335: Cond: +LOOP	restrict? sys? +loop, ;Cond
 2336: Cond: NEXT	restrict? sys? next, ;Cond
 2337: 
 2338: \ String words                                         23feb93py
 2339: 
 2340: : ,"            [char] " parse T string, align H ;
 2341: 
 2342: Cond: ."        restrict? compile (.")     T ," H ;Cond
 2343: Cond: S"        restrict? compile (S")     T ," H ;Cond
 2344: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
 2345: 
 2346: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
 2347: : IS            T >address ' >body ! H ;
 2348: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
 2349: : TO            T ' >body ! H ;
 2350: 
 2351: Cond: defers	T ' >body @ compile, H ;Cond
 2352: : on		T -1 swap ! H ; 
 2353: : off   	T 0 swap ! H ;
 2354: 
 2355: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
 2356: 
 2357: \ linked list primitive
 2358: : linked        X here over X @ X A, swap X ! ;
 2359: : chained	T linked A, H ;
 2360: 
 2361: : err"   s" ErrLink linked" evaluate T , H
 2362:          [char] " parse T string, align H ;
 2363: 
 2364: : env"  [char] " parse s" EnvLink linked" evaluate
 2365:         T string, align , H ;
 2366: 
 2367: : 2env" [char] " parse s" EnvLink linked" evaluate
 2368:         here >r T string, align , , H
 2369:         r> dup T c@ H 80 and swap T c! H ;
 2370: 
 2371: \ compile must be last                                 22feb93py
 2372: 
 2373: Cond: compile ( -- ) restrict? \ name
 2374:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2375:       0> IF    gexecute
 2376:          ELSE  dup >magic @ <imm> =
 2377:                IF   gexecute
 2378:                ELSE compile (compile) addr, THEN THEN ;Cond
 2379: 
 2380: Cond: postpone ( -- ) restrict? \ name
 2381:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2382:       0> IF    gexecute
 2383:          ELSE  dup >magic @ <imm> =
 2384:                IF   gexecute
 2385: 	       ELSE compile (compile) addr, THEN THEN ;Cond
 2386: 	   
 2387: \ save-cross                                           17mar93py
 2388: 
 2389: hex
 2390: 
 2391: >CROSS
 2392: Create magic  s" Gforth2x" here over allot swap move
 2393: 
 2394: bigendian 1+ \ strangely, in magic big=0, little=1
 2395: tcell 1 = 0 and or
 2396: tcell 2 = 2 and or
 2397: tcell 4 = 4 and or
 2398: tcell 8 = 6 and or
 2399: tchar 1 = 00 and or
 2400: tchar 2 = 28 and or
 2401: tchar 4 = 50 and or
 2402: tchar 8 = 78 and or
 2403: magic 7 + c!
 2404: 
 2405: : save-cross ( "image-name" "binary-name" -- )
 2406:   bl parse ." Saving to " 2dup type cr
 2407:   w/o bin create-file throw >r
 2408:   TNIL IF
 2409:       s" #! "           r@ write-file throw
 2410:       bl parse          r@ write-file throw
 2411:       s"  --image-file" r@ write-file throw
 2412:       #lf       r@ emit-file throw
 2413:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
 2414:       ?do
 2415: 	  bl over emit-file throw
 2416:       loop
 2417:       drop
 2418:       magic 8       r@ write-file throw \ write magic
 2419:   ELSE
 2420:       bl parse 2drop
 2421:   THEN
 2422:   image @ there 
 2423:   r@ write-file throw \ write image
 2424:   TNIL IF
 2425:       bit$  @ there 1- tcell>bit rshift 1+
 2426:                 r@ write-file throw \ write tags
 2427:   THEN
 2428:   r> close-file throw ;
 2429: 
 2430: : save-region ( addr len -- )
 2431:   bl parse w/o bin create-file throw >r
 2432:   swap >image swap r@ write-file throw
 2433:   r> close-file throw ;
 2434: 
 2435: \ \ minimal definitions
 2436: 	   
 2437: >MINIMAL also minimal
 2438: 
 2439: \ Usefull words                                        13feb93py
 2440: 
 2441: : KB  400 * ;
 2442: 
 2443: \ \ [IF] [ELSE] [THEN] ...				14sep97jaw
 2444: 
 2445: \ it is useful to define our own structures and not to rely
 2446: \ on the words in the compiler
 2447: \ The words in the compiler might be defined with vocabularies
 2448: \ this doesn't work with our self-made compile-loop
 2449: 
 2450: Create parsed 20 chars allot	\ store word we parsed
 2451: 
 2452: : upcase
 2453:     parsed count bounds
 2454:     ?DO I c@ toupper I c! LOOP ;
 2455: 
 2456: : [ELSE]
 2457:     1 BEGIN
 2458: 	BEGIN bl word count dup WHILE
 2459: 	    comment? 20 umin parsed place upcase parsed count
 2460: 	    2dup s" [IF]" compare 0= >r 
 2461: 	    2dup s" [IFUNDEF]" compare 0= >r
 2462: 	    2dup s" [IFDEF]" compare 0= r> or r> or
 2463: 	    IF   2drop 1+
 2464: 	    ELSE 2dup s" [ELSE]" compare 0=
 2465: 		IF   2drop 1- dup
 2466: 		    IF 1+
 2467: 		    THEN
 2468: 		ELSE
 2469: 		    2dup s" [ENDIF]" compare 0= >r
 2470: 		    s" [THEN]" compare 0= r> or
 2471: 		    IF 1- THEN
 2472: 		THEN
 2473: 	    THEN
 2474: 	    ?dup 0= ?EXIT
 2475: 	REPEAT
 2476: 	2drop refill 0=
 2477:     UNTIL drop ; immediate
 2478:   
 2479: : [THEN] ( -- ) ; immediate
 2480: 
 2481: : [ENDIF] ( -- ) ; immediate
 2482: 
 2483: : [IF] ( flag -- )
 2484:     0= IF postpone [ELSE] THEN ; immediate 
 2485: 
 2486: Cond: [IF]      postpone [IF] ;Cond
 2487: Cond: [THEN]    postpone [THEN] ;Cond
 2488: Cond: [ELSE]    postpone [ELSE] ;Cond
 2489: 
 2490: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
 2491: 
 2492: : defined? tdefined? ;
 2493: : needed? needed? ;
 2494: : doer? doer? ;
 2495: 
 2496: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
 2497: 
 2498: : directive? 
 2499:   bl word count [ ' target >wordlist ] literal search-wordlist 
 2500:   dup IF nip THEN ;
 2501: 
 2502: : [IFDEF]  >in @ directive? swap >in !
 2503: 	   0= IF tdefined? ELSE name 2drop true THEN
 2504: 	   postpone [IF] ;
 2505: 
 2506: : [IFUNDEF] tdefined? 0= postpone [IF] ;
 2507: 
 2508: Cond: [IFDEF]   postpone [IFDEF] ;Cond
 2509: 
 2510: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
 2511: 
 2512: \ C: \- \+ Conditional Compiling                         09jun93jaw
 2513: 
 2514: : C: >in @ tdefined? 0=
 2515:      IF    >in ! X :
 2516:      ELSE drop
 2517:         BEGIN bl word dup c@
 2518:               IF   count comment? s" ;" compare 0= ?EXIT
 2519:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
 2520:               THEN
 2521:         AGAIN
 2522:      THEN ;
 2523: 
 2524: : d? d? ;
 2525: 
 2526: \G doesn't skip line when debug switch is on
 2527: : \D D? 0= IF postpone \ THEN ;
 2528: 
 2529: \G interprets the line if word is not defined
 2530: : \- tdefined? IF postpone \ THEN ;
 2531: 
 2532: \G interprets the line if word is defined
 2533: : \+ tdefined? 0= IF postpone \ THEN ;
 2534: 
 2535: Cond: \- \- ;Cond
 2536: Cond: \+ \+ ;Cond
 2537: Cond: \D \D ;Cond
 2538: 
 2539: : ?? bl word find IF execute ELSE drop 0 THEN ;
 2540: 
 2541: : needed:
 2542: \G defines ghost for words that we want to be compiled
 2543:   BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
 2544: 
 2545: \ words that should be in minimal
 2546: 
 2547: create s-buffer 50 chars allot
 2548: 
 2549: bigendian Constant bigendian
 2550: 
 2551: : here there ;
 2552: : equ constant ;
 2553: : mark there constant ;
 2554: 
 2555: \ compiler directives
 2556: : >ram >ram ;
 2557: : >rom >rom ;
 2558: : >auto >auto ;
 2559: : >tempdp >tempdp ;
 2560: : tempdp> tempdp> ;
 2561: : const constflag on ;
 2562: : warnings name 3 = 0= twarnings ! drop ;
 2563: : | ;
 2564: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
 2565: 
 2566: : save-cross save-cross ;
 2567: : save-region save-region ;
 2568: : tdump swap >image swap dump ;
 2569: 
 2570: also forth 
 2571: [IFDEF] Label           : Label defempty? Label ; [THEN] 
 2572: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
 2573: \ [IFDEF] builttag	: builttag builttag ;	[THEN]
 2574: previous
 2575: 
 2576: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
 2577: : + + ;
 2578: : 1+ 1 + ;
 2579: : 2+ 2 + ;
 2580: : 1- 1- ;
 2581: : - - ;
 2582: : and and ;
 2583: : or or ;
 2584: : 2* 2* ;
 2585: : * * ;
 2586: : / / ;
 2587: : dup dup ;
 2588: : over over ;
 2589: : swap swap ;
 2590: : rot rot ;
 2591: : drop drop ;
 2592: : =   = ;
 2593: : 0=   0= ;
 2594: : lshift lshift ;
 2595: : 2/ 2/ ;
 2596: : . . ;
 2597: 
 2598: : all-words    ['] forced?    IS skip? ;
 2599: : needed-words ['] needed?  IS skip? ;
 2600: : undef-words  ['] defined2? IS skip? ;
 2601: : skipdef skipdef ;
 2602: 
 2603: : \  postpone \ ;  immediate
 2604: : \G T-\G ; immediate
 2605: : (  postpone ( ;  immediate
 2606: : include bl word count included ;
 2607: : require require ;
 2608: : .( [char] ) parse type ;
 2609: : ." [char] " parse type ;
 2610: : cr cr ;
 2611: 
 2612: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
 2613: 
 2614: \ only forth also cross also minimal definitions order
 2615: 
 2616: \ cross-compiler words
 2617: 
 2618: : decimal       decimal ;
 2619: : hex           hex ;
 2620: 
 2621: \ : tudp          X tudp ;
 2622: \ : tup           X tup ;
 2623: 
 2624: : doc-off       false to-doc ! ;
 2625: : doc-on        true  to-doc ! ;
 2626: 
 2627: [IFDEF] dbg : dbg dbg ; [THEN]
 2628: 
 2629: \ for debugging...
 2630: : order         order ;
 2631: : hwords         words ;
 2632: : words 	also ghosts words previous ;
 2633: : .s            .s ;
 2634: : bye           bye ;
 2635: 
 2636: \ turnkey direction
 2637: : H forth ; immediate
 2638: : T minimal ; immediate
 2639: : G ghosts ; immediate
 2640: 
 2641: : turnkey 
 2642:    \GFORTH 0 set-order also ghosts
 2643:    \ANSI [ ' ghosts >wordlist ] Literal 1 set-order
 2644:    also target definitions
 2645:    also Minimal also ;
 2646: 
 2647: \ these ones are pefered:
 2648: 
 2649: : lock   turnkey ;
 2650: : unlock previous forth also cross ;
 2651: 
 2652: \ also minimal
 2653: : [[ also unlock ;
 2654: : ]] previous previous also also ;
 2655: 
 2656: unlock definitions also minimal
 2657: : lock   lock ;
 2658: lock
 2659: 
 2660: \ load cross compiler extension defined in mach file
 2661: 
 2662: UNLOCK >CROSS
 2663: 
 2664: [IFDEF] extend-cross extend-cross [THEN]
 2665: 
 2666: LOCK

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