File:  [gforth] / gforth / cross.fs
Revision 1.93: download - view: text, annotated - select for diffs
Sun Mar 11 21:47:27 2001 UTC (23 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
First steps to get peephole optimizing into cross

    1: \ CROSS.FS     The Cross-Compiler                      06oct92py
    2: \ Idea and implementation: Bernd Paysan (py)
    3: 
    4: \ Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
    5: 
    6: \ This file is part of Gforth.
    7: 
    8: \ Gforth is free software; you can redistribute it and/or
    9: \ modify it under the terms of the GNU General Public License
   10: \ as published by the Free Software Foundation; either version 2
   11: \ of the License, or (at your option) any later version.
   12: 
   13: \ This program is distributed in the hope that it will be useful,
   14: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: \ GNU General Public License for more details.
   17: 
   18: \ You should have received a copy of the GNU General Public License
   19: \ along with this program; if not, write to the Free Software
   20: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   21: 
   22: 0 
   23: [IF]
   24: 
   25: ToDo:
   26: Crossdoc destination ./doc/crossdoc.fd makes no sense when
   27: cross.fs is uses seperately. jaw
   28: Do we need this char translation with >address and in branchoffset? 
   29: (>body also affected) jaw
   30: Clean up mark> and >resolve stuff jaw
   31: 
   32: [THEN]
   33: 
   34: hex
   35: 
   36: \ debugging for compiling
   37: 
   38: \ print stack at each colon definition
   39: \ : : save-input cr bl word count type restore-input throw .s : ;
   40: 
   41: \ print stack at each created word
   42: \ : create save-input cr bl word count type restore-input throw .s create ;
   43: 
   44: 
   45: \ \ -------------  Setup Vocabularies
   46: 
   47: \ Remark: Vocabulary is not ANS, but it should work...
   48: 
   49: Vocabulary Cross
   50: Vocabulary Target
   51: Vocabulary Ghosts
   52: Vocabulary Minimal
   53: only Forth also Target also also
   54: definitions Forth
   55: 
   56: : T  previous Ghosts also Target ; immediate
   57: : G  Ghosts ; immediate
   58: : H  previous Forth also Cross ; immediate
   59: 
   60: forth definitions
   61: 
   62: : T  previous Ghosts also Target ; immediate
   63: : G  Ghosts ; immediate
   64: 
   65: : >cross  also Cross definitions previous ;
   66: : >target also Target definitions previous ;
   67: : >minimal also Minimal definitions previous ;
   68: 
   69: H
   70: 
   71: >CROSS
   72: 
   73: \ find out whether we are compiling with gforth
   74: 
   75: : defined? bl word find nip ;
   76: defined? emit-file defined? toupper and \ drop 0
   77: [IF]
   78: \ use this in a gforth system
   79: : \GFORTH ; immediate
   80: : \ANSI postpone \ ; immediate
   81: [ELSE]
   82: : \GFORTH postpone \ ; immediate
   83: : \ANSI ; immediate
   84: [THEN]
   85: 
   86: \ANSI : [IFUNDEF] defined? 0= postpone [IF] ; immediate
   87: \ANSI : [IFDEF] defined? postpone [IF] ; immediate
   88: 0 \ANSI drop 1
   89: [IF]
   90: : \G postpone \ ; immediate
   91: : rdrop postpone r> postpone drop ; immediate
   92: : name bl word count ;
   93: : bounds over + swap ;
   94: : scan >r BEGIN dup WHILE over c@ r@ <> WHILE 1 /string REPEAT THEN rdrop ;
   95: : linked here over @ , swap ! ;
   96: : alias create , DOES> @ EXECUTE ;
   97: : defer ['] noop alias ;
   98: : is state @ 
   99:   IF ' >body postpone literal postpone ! 
  100:   ELSE ' >body ! THEN ; immediate
  101: : 0>= 0< 0= ;
  102: : d<> rot <> -rot <> or ;
  103: : toupper dup [char] a [char] z 1+ within IF [char] A [char] a - + THEN ;
  104: Variable ebuf
  105: : emit-file ( c fd -- ior ) swap ebuf c! ebuf 1 chars rot write-file ;
  106: 0a Constant #lf
  107: 0d Constant #cr
  108: 
  109: [IFUNDEF] Warnings Variable Warnings [THEN]
  110: 
  111: \ \ Number parsing					23feb93py
  112: 
  113: \ number? number                                       23feb93py
  114: 
  115: Variable dpl
  116: 
  117: hex
  118: Create bases   10 ,   2 ,   A , 100 ,
  119: \              16     2    10   character
  120: 
  121: \ !! protect BASE saving wrapper against exceptions
  122: : getbase ( addr u -- addr' u' )
  123:     over c@ [char] $ - dup 4 u<
  124:     IF
  125: 	cells bases + @ base ! 1 /string
  126:     ELSE
  127: 	drop
  128:     THEN ;
  129: 
  130: : sign? ( addr u -- addr u flag )
  131:     over c@ [char] - =  dup >r
  132:     IF
  133: 	1 /string
  134:     THEN
  135:     r> ;
  136: 
  137: : s>unumber? ( addr u -- ud flag )
  138:     over [char] ' =
  139:     IF 	\ a ' alone is rather unusual :-)
  140: 	drop char+ c@ 0 true EXIT 
  141:     THEN
  142:     base @ >r  dpl on  getbase
  143:     0. 2swap
  144:     BEGIN ( d addr len )
  145: 	dup >r >number dup
  146:     WHILE \ there are characters left
  147: 	dup r> -
  148:     WHILE \ the last >number parsed something
  149: 	dup 1- dpl ! over c@ [char] . =
  150:     WHILE \ the current char is '.'
  151: 	1 /string
  152:     REPEAT  THEN \ there are unparseable characters left
  153: 	2drop false
  154:     ELSE
  155: 	rdrop 2drop true
  156:     THEN
  157:     r> base ! ;
  158: 
  159: \ ouch, this is complicated; there must be a simpler way - anton
  160: : s>number? ( addr len -- d f )
  161:     \ converts string addr len into d, flag indicates success
  162:     sign? >r
  163:     s>unumber?
  164:     0= IF
  165:         rdrop false
  166:     ELSE \ no characters left, all ok
  167: 	r>
  168: 	IF
  169: 	    dnegate
  170: 	THEN
  171: 	true
  172:     THEN ;
  173: 
  174: : s>number ( addr len -- d )
  175:     \ don't use this, there is no way to tell success
  176:     s>number? drop ;
  177: 
  178: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
  179:     s>number? 0=
  180:     IF
  181: 	2drop false  EXIT
  182:     THEN
  183:     dpl @ dup 0< IF
  184: 	nip
  185:     ELSE
  186: 	1+
  187:     THEN ;
  188: 
  189: : number? ( string -- string 0 / n -1 / d 0> )
  190:     dup >r count snumber? dup if
  191: 	rdrop
  192:     else
  193: 	r> swap
  194:     then ;
  195: 
  196: : number ( string -- d )
  197:     number? ?dup 0= abort" ?"  0<
  198:     IF
  199: 	s>d
  200:     THEN ;
  201: 
  202: [THEN]
  203: 
  204: hex     \ the defualt base for the cross-compiler is hex !!
  205: \ Warnings off
  206: 
  207: \ words that are generaly useful
  208: 
  209: : KB  400 * ;
  210: : >wordlist ( vocabulary-xt -- wordlist-struct )
  211:   also execute get-order swap >r 1- set-order r> ;
  212: 
  213: : umax 2dup u< IF swap THEN drop ;
  214: : umin 2dup u> IF swap THEN drop ;
  215: 
  216: : string, ( c-addr u -- )
  217:     \ puts down string as cstring
  218:     dup c, here swap chars dup allot move ;
  219: 
  220: : ," [char] " parse string, ;
  221: 
  222: : SetValue ( n -- <name> )
  223: \G Same behaviour as "Value" if the <name> is not defined
  224: \G Same behaviour as "to" if <name> is defined
  225: \G SetValue searches in the current vocabulary
  226:   save-input bl word >r restore-input throw r> count
  227:   get-current search-wordlist
  228:   IF	drop >r
  229: 	\ we have to set current to be topmost context wordlist
  230: 	get-order get-order get-current swap 1+ set-order
  231: 	r> ['] to execute
  232: 	set-order
  233:   ELSE Value THEN ;
  234: 
  235: : DefaultValue ( n -- <name> )
  236: \G Same behaviour as "Value" if the <name> is not defined
  237: \G DefaultValue searches in the current vocabulary
  238:  save-input bl word >r restore-input throw r> count
  239:  get-current search-wordlist
  240:  IF bl word drop 2drop ELSE Value THEN ;
  241: 
  242: hex
  243: 
  244: \ 1 Constant Cross-Flag	\ to check whether assembler compiler plug-ins are
  245: 			\ for cross-compiling
  246: \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
  247: 
  248: : comment? ( c-addr u -- c-addr u )
  249:         2dup s" (" compare 0=
  250:         IF    postpone (
  251:         ELSE  2dup s" \" compare 0= IF postpone \ THEN
  252:         THEN ;
  253: 
  254: \ Begin CROSS COMPILER:
  255: 
  256: \ debugging
  257: 
  258: 0 [IF]
  259: 
  260: This implements debugflags for the cross compiler and the compiled
  261: images. It works identical to the has-flags in the environment.
  262: The debugflags are defined in a vocabluary. If the word exists and
  263: its value is true, the flag is switched on.
  264: 
  265: [THEN]
  266: 
  267: >CROSS
  268: 
  269: Vocabulary debugflags	\ debug flags for cross
  270: also debugflags get-order over
  271: Constant debugflags-wl
  272: set-order previous
  273: 
  274: : DebugFlag
  275:   get-current >r debugflags-wl set-current
  276:   SetValue
  277:   r> set-current ;
  278: 
  279: : Debug? ( adr u -- flag )
  280: \G return true if debug flag is defined or switched on
  281:   debugflags-wl search-wordlist
  282:   IF EXECUTE
  283:   ELSE false THEN ;
  284: 
  285: : D? ( <name> -- flag )
  286: \G return true if debug flag is defined or switched on
  287: \G while compiling we do not return the current value but
  288:   bl word count debug? ;
  289: 
  290: : [d?]
  291: \G compile the value-xt so the debug flag can be switched
  292: \G the flag must exist!
  293:   bl word count debugflags-wl search-wordlist
  294:   IF 	compile,
  295:   ELSE  -1 ABORT" unknown debug flag"
  296: 	\ POSTPONE false 
  297:   THEN ; immediate
  298: 
  299: \ \ --------------------	source file
  300: 
  301: decimal
  302: 
  303: Variable cross-file-list
  304: 0 cross-file-list !
  305: Variable target-file-list
  306: 0 target-file-list !
  307: Variable host-file-list
  308: 0 host-file-list !
  309: 
  310: cross-file-list Value file-list
  311: 0 Value source-desc
  312: 
  313: \ file loading
  314: 
  315: : >fl-id   1 cells + ;
  316: : >fl-name 2 cells + ;
  317: 
  318: Variable filelist 0 filelist !
  319: Create NoFile ," #load-file#"
  320: 
  321: : loadfile ( -- adr )
  322:   source-desc ?dup IF >fl-name ELSE NoFile THEN ;
  323: 
  324: : sourcefilename ( -- adr len ) 
  325:   loadfile count ;
  326: 
  327: \ANSI : sourceline# 0 ;
  328: 
  329: \ \ --------------------	path handling from kernel/paths.fs
  330: 
  331: \ paths.fs path file handling                                    03may97jaw
  332: 
  333: \ -Changing the search-path:
  334: \ fpath+ <path> 	adds a directory to the searchpath
  335: \ fpath= <path>|<path>	makes complete now searchpath
  336: \ 			seperator is |
  337: \ .fpath		displays the search path
  338: \ remark I: 
  339: \ a ./ in the beginning of filename is expanded to the directory the
  340: \ current file comes from. ./ can also be included in the search-path!
  341: \ ~+/ loads from the current working directory
  342: 
  343: \ remark II:
  344: \ if there is no sufficient space for the search path increase it!
  345: 
  346: 
  347: \ -Creating custom paths:
  348: 
  349: \ It is possible to use the search mechanism on yourself.
  350: 
  351: \ Make a buffer for the path:
  352: \ create mypath	100 chars , 	\ maximum length (is checked)
  353: \ 		0 ,		\ real len
  354: \ 		100 chars allot \ space for path
  355: \ use the same functions as above with:
  356: \ mypath path+ 
  357: \ mypath path=
  358: \ mypath .path
  359: 
  360: \ do a open with the search path:
  361: \ open-path-file ( adr len path -- fd adr len ior )
  362: \ the file is opened read-only; if the file is not found an error is generated
  363: 
  364: \ questions to: wilke@jwdt.com
  365: 
  366: [IFUNDEF] +place
  367: : +place ( adr len adr )
  368:         2dup >r >r
  369:         dup c@ char+ + swap move
  370:         r> r> dup c@ rot + swap c! ;
  371: [THEN]
  372: 
  373: [IFUNDEF] place
  374: : place ( c-addr1 u c-addr2 )
  375:         2dup c! char+ swap move ;
  376: [THEN]
  377: 
  378: \ if we have path handling, use this and the setup of it
  379: [IFUNDEF] open-fpath-file
  380: 
  381: create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
  382: sourcepath value fpath
  383: 
  384: : also-path ( adr len path^ -- )
  385:   >r
  386:   \ len check
  387:   r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
  388:   \ copy into
  389:   tuck r@ cell+ dup @ cell+ + swap cmove
  390:   \ make delimiter
  391:   0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
  392:   ;
  393: 
  394: : only-path ( adr len path^ -- )
  395:   dup 0 swap cell+ ! also-path ;
  396: 
  397: : path+ ( path-addr  "dir" -- ) \ gforth
  398:     \G Add the directory @var{dir} to the search path @var{path-addr}.
  399:     name rot also-path ;
  400: 
  401: : fpath+ ( "dir" ) \ gforth
  402:     \G Add directory @var{dir} to the Forth search path.
  403:     fpath path+ ;
  404: 
  405: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
  406:     \G Make a complete new search path; the path separator is |.
  407:     name 2dup bounds ?DO i c@ [char] | = IF 0 i c! THEN LOOP
  408:     rot only-path ;
  409: 
  410: : fpath= ( "dir1|dir2|dir3" ) \ gforth
  411:     \G Make a complete new Forth search path; the path separator is |.
  412:     fpath path= ;
  413: 
  414: : path>counted  cell+ dup cell+ swap @ ;
  415: 
  416: : next-path ( adr len -- adr2 len2 )
  417:   2dup 0 scan
  418:   dup 0= IF     2drop 0 -rot 0 -rot EXIT THEN
  419:   >r 1+ -rot r@ 1- -rot
  420:   r> - ;
  421: 
  422: : previous-path ( path^ -- )
  423:   dup path>counted
  424:   BEGIN tuck dup WHILE repeat ;
  425: 
  426: : .path ( path-addr -- ) \ gforth
  427:     \G Display the contents of the search path @var{path-addr}.
  428:     path>counted
  429:     BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
  430: 
  431: : .fpath ( -- ) \ gforth
  432:     \G Display the contents of the Forth search path.
  433:     fpath .path ;
  434: 
  435: : absolut-path? ( addr u -- flag ) \ gforth
  436:     \G A path is absolute if it starts with a / or a ~ (~ expansion),
  437:     \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
  438:     \G it has a colon as second character ("C:...").  Paths simply
  439:     \G containing a / are not absolute!
  440:     2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/....
  441:     over c@ [char] / = >r
  442:     over c@ [char] ~ = >r
  443:     \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
  444:     2 min S" ./" compare 0=
  445:     r> r> r> or or or ;
  446: 
  447: Create ofile 0 c, 255 chars allot
  448: Create tfile 0 c, 255 chars allot
  449: 
  450: : pathsep? dup [char] / = swap [char] \ = or ;
  451: 
  452: : need/   ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
  453: 
  454: : extractpath ( adr len -- adr len2 )
  455:   BEGIN dup WHILE 1-
  456:         2dup + c@ pathsep? IF EXIT THEN
  457:   REPEAT ;
  458: 
  459: : remove~+ ( -- )
  460:     ofile count 3 min s" ~+/" compare 0=
  461:     IF
  462: 	ofile count 3 /string ofile place
  463:     THEN ;
  464: 
  465: : expandtopic ( -- ) \ stack effect correct? - anton
  466:     \ expands "./" into an absolute name
  467:     ofile count 2 min s" ./" compare 0=
  468:     IF
  469: 	ofile count 1 /string tfile place
  470: 	0 ofile c! sourcefilename extractpath ofile place
  471: 	ofile c@ IF need/ THEN
  472: 	tfile count over c@ pathsep? IF 1 /string THEN
  473: 	ofile +place
  474:     THEN ;
  475: 
  476: : compact.. ( adr len -- adr2 len2 )
  477:     \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
  478:     over swap
  479:     BEGIN  dup  WHILE
  480:         dup >r '/ scan 2dup 4 min s" /../" compare 0=
  481:         IF
  482:             dup r> - >r 4 /string over r> + 4 -
  483:             swap 2dup + >r move dup r> over -
  484:         ELSE
  485:             rdrop dup 1 min /string
  486:         THEN
  487:     REPEAT  drop over - ;
  488: 
  489: : reworkdir ( -- )
  490:   remove~+
  491:   ofile count compact..
  492:   nip ofile c! ;
  493: 
  494: : open-ofile ( -- fid ior )
  495:     \G opens the file whose name is in ofile
  496:     expandtopic reworkdir
  497:     ofile count r/o open-file ;
  498: 
  499: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
  500:   0 ofile ! >r >r ofile place need/
  501:   r> r> ofile +place
  502:   open-ofile ;
  503: 
  504: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
  505:     \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
  506:     \G If found, the resulting path and an open file descriptor
  507:     \G are returned. If the file is not found, @var{ior} is non-zero.
  508:   >r
  509:   2dup absolut-path?
  510:   IF    rdrop
  511:         ofile place open-ofile
  512: 	dup 0= IF >r ofile count r> THEN EXIT
  513:   ELSE  r> path>counted
  514:         BEGIN  next-path dup
  515:         WHILE  5 pick 5 pick check-path
  516:         0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
  517:   REPEAT
  518:         2drop 2drop 2drop -38
  519:   THEN ;
  520: 
  521: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
  522:     \G Look in the Forth search path for the file specified by @var{addr1 u1}.
  523:     \G If found, the resulting path and an open file descriptor
  524:     \G are returned. If the file is not found, @var{ior} is non-zero.
  525:     fpath open-path-file ;
  526: 
  527: fpath= ~+
  528: 
  529: [THEN]
  530: 
  531: \ \ --------------------	include require			13may99jaw
  532: 
  533: >CROSS
  534: 
  535: : add-included-file ( adr len -- adr )
  536:   dup >fl-name char+ allocate throw >r
  537:   file-list @ r@ ! r@ file-list !
  538:   r@ >fl-name place r> ;
  539: 
  540: : included? ( c-addr u -- f )
  541:   file-list
  542:   BEGIN	@ dup
  543:   WHILE	>r 2dup r@ >fl-name count compare 0=
  544: 	IF rdrop 2drop true EXIT THEN
  545: 	r>
  546:   REPEAT
  547:   2drop drop false ;	
  548: 
  549: false DebugFlag showincludedfiles
  550: 
  551: : included1 ( fd adr u -- )
  552: \ include file adr u / fd
  553: \ we don't use fd with include-file, because the forth system
  554: \ doesn't know the name of the file to get a nice error report
  555:   [d?] showincludedfiles
  556:   IF	cr ." Including: " 2dup type ." ..." THEN
  557:   rot close-file throw
  558:   source-desc >r
  559:   add-included-file to source-desc 
  560:   sourcefilename
  561:   ['] included catch
  562:   r> to source-desc 
  563:   throw ;
  564: 
  565: : included ( adr len -- )
  566: 	cross-file-list to file-list
  567: 	open-fpath-file throw 
  568:         included1 ;
  569: 
  570: : required ( adr len -- )
  571: 	cross-file-list to file-list
  572: 	open-fpath-file throw \ 2dup cr ." R:" type
  573: 	2dup included?
  574: 	IF 	2drop close-file throw
  575: 	ELSE	included1
  576: 	THEN ;
  577: 
  578: : include bl word count included ;
  579: 
  580: : require bl word count required ;
  581: 
  582: 0 [IF]
  583: 
  584: also forth definitions previous
  585: 
  586: : included ( adr len -- ) included ;
  587: 
  588: : required ( adr len -- ) required ;
  589: 
  590: : include include ;
  591: 
  592: : require require ;
  593: 
  594: [THEN]
  595: 
  596: >CROSS
  597: hex
  598: 
  599: \ \ --------------------        Error Handling                  05aug97jaw
  600: 
  601: \ Flags
  602: 
  603: also forth definitions  \ these values may be predefined before
  604:                         \ the cross-compiler is loaded
  605: 
  606: false DefaultValue stack-warn   	 \ check on empty stack at any definition
  607: false DefaultValue create-forward-warn   \ warn on forward declaration of created words
  608: 
  609: previous >CROSS
  610: 
  611: : .dec
  612:   base @ decimal swap . base ! ;
  613: 
  614: : .sourcepos
  615:   cr sourcefilename type ." :"
  616:   sourceline# .dec ;
  617: 
  618: : warnhead
  619: \G display error-message head
  620: \G perhaps with linenumber and filename
  621:   .sourcepos ." Warning: " ;
  622: 
  623: : empty? depth IF .sourcepos ." Stack not empty!"  THEN ;
  624: 
  625: stack-warn [IF]
  626: : defempty? empty? ;
  627: [ELSE]
  628: : defempty? ; immediate
  629: [THEN]
  630: 
  631: \ \ GhostNames Ghosts                                  9may93jaw
  632: 
  633: \ second name source to search trough list
  634: 
  635: VARIABLE GhostNames
  636: 0 GhostNames !
  637: 
  638: : GhostName ( -- addr )
  639:     align here GhostNames @ , GhostNames ! here 0 ,
  640:     bl word count
  641:     \ 2dup type space
  642:     string, \ !! cfalign ?
  643:     align ;
  644: 
  645: \ Ghost Builder                                        06oct92py
  646: 
  647: \ <T T> new version with temp variable                 10may93jaw
  648: 
  649: VARIABLE VocTemp
  650: 
  651: : <T  get-current VocTemp ! also Ghosts definitions ;
  652: : T>  previous VocTemp @ set-current ;
  653: 
  654: hex
  655: 4711 Constant <fwd>             4712 Constant <res>
  656: 4713 Constant <imm>             4714 Constant <do:>
  657: 4715 Constant <skip>
  658: 
  659: \  Compiler States
  660: 
  661: Variable comp-state
  662: 0 Constant interpreting
  663: 1 Constant compiling
  664: 2 Constant resolving
  665: 3 Constant assembling
  666: 
  667: Defer lit, ( n -- )
  668: Defer alit, ( n -- )
  669: 
  670: Defer branch, ( target-addr -- )	\ compiles a branch
  671: Defer ?branch, ( target-addr -- )	\ compiles a ?branch
  672: Defer branchmark, ( -- branch-addr )	\ reserves room for a branch
  673: Defer ?branchmark, ( -- branch-addr )	\ reserves room for a ?branch
  674: Defer ?domark, ( -- branch-addr )	\ reserves room for a ?do branch
  675: Defer branchto, ( -- )			\ actual program position is target of a branch (do e.g. alignment)
  676: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
  677: Defer branchfrom, ( -- )		\ ?!
  678: Defer branchtomark, ( -- target-addr )	\ marks a branch destination
  679: 
  680: Defer colon, ( tcfa -- )		\ compiles call to tcfa at current position
  681: Defer colonmark, ( -- addr )		\ marks a colon call
  682: Defer colon-resolve ( tcfa addr -- )
  683: 
  684: Defer addr-resolve ( target-addr addr -- )
  685: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
  686: 
  687: Defer do,	( -- do-token )
  688: Defer ?do,	( -- ?do-token )
  689: Defer for,	( -- for-token )
  690: Defer loop,	( do-token / ?do-token -- )
  691: Defer +loop,	( do-token / ?do-token -- )
  692: Defer next,	( for-token )
  693: 
  694: [IFUNDEF] ca>native
  695: defer ca>native	
  696: [THEN]
  697: 
  698: \ ghost structure
  699: 
  700: : >magic ;		\ type of ghost
  701: : >link cell+ ;		\ pointer where ghost is in target, or if unresolved
  702: 			\ points to the where we have to resolve (linked-list)
  703: : >exec cell+ cell+ ;	\ execution symantics (while target compiling) of ghost
  704: : >comp 3 cells + ;     \ compilation semantics
  705: : >end 4 cells + ;	\ room for additional tags
  706: 			\ for builder (create, variable...) words the
  707: 			\ execution symantics of words built are placed here
  708: 
  709: \ resolve structure
  710: 
  711: : >next ;		\ link to next field
  712: : >tag cell+ ;		\ indecates type of reference: 0: call, 1: address, 2: doer
  713: : >taddr cell+ cell+ ;	
  714: : >ghost 3 cells + ;
  715: : >file 4 cells + ;
  716: : >line 5 cells + ;
  717: 
  718: \ refer variables
  719: 
  720: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
  721: Variable last-ghost	\ last ghost that is created
  722: Variable last-header-ghost \ last ghost definitions with header
  723: 
  724: : (refered) ( ghost addr tag -- )
  725: \G creates a reference to ghost at address taddr
  726:     rot >r here r@ >link @ , r> >link ! 
  727:     ( taddr tag ) ,
  728:     ( taddr ) , 
  729:     last-header-ghost @ , 
  730:     loadfile , 
  731:     sourceline# , 
  732:   ;
  733: 
  734: \ iForth makes only immediate directly after create
  735: \ make atonce trick! ?
  736: 
  737: Variable atonce atonce off
  738: 
  739: : NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;
  740: 
  741: : is-forward   ( ghost -- )
  742:   colonmark, 0 (refered) ; \ compile space for call
  743: 
  744: : GhostHeader <fwd> , 0 , ['] NoExec , ['] is-forward , ;
  745: 
  746: : Make-Ghost ( "name" -- ghost )
  747:   >in @ GhostName swap >in !
  748:   <T Create atonce @ IF immediate atonce off THEN
  749:   here tuck swap ! ghostheader T>
  750:   dup last-ghost !
  751:   DOES> dup executed-ghost ! >exec @ execute ;
  752: 
  753: \ ghost words                                          14oct92py
  754: \                                          changed:    10may93py/jaw
  755: 
  756: : gfind   ( string -- ghost true/1 / string false )
  757: \ searches for string in word-list ghosts
  758:   dup count [ ' ghosts >wordlist ] Literal search-wordlist
  759:   dup IF >r >body nip r>  THEN ;
  760: 
  761: : gdiscover ( xt -- ghost true | xt false )
  762:   GhostNames
  763:   BEGIN @ dup
  764:   WHILE 2dup
  765:         cell+ @ dup >magic @ <fwd> <>
  766:         >r >link @ = r> and
  767:         IF cell+ @ nip true EXIT THEN
  768:   REPEAT
  769:   drop false ;
  770: 
  771: VARIABLE Already
  772: 
  773: : ghost   ( "name" -- ghost )
  774:   Already off
  775:   >in @  bl word gfind   IF  atonce off Already on nip EXIT  THEN
  776:   drop  >in !  Make-Ghost ;
  777: 
  778: : >ghostname ( ghost -- adr len )
  779:   GhostNames
  780:   BEGIN @ dup
  781:   WHILE 2dup cell+ @ =
  782:   UNTIL nip 2 cells + count
  783:   ELSE  2drop 
  784: 	\ true abort" CROSS: Ghostnames inconsistent"
  785: 	s" ?!?!?!"
  786:   THEN ;
  787: 
  788: : .ghost ( ghost -- ) >ghostname type ;
  789: 
  790: \ ' >ghostname ALIAS @name
  791: 
  792: : forward? ( ghost -- flag )
  793:   >magic @ <fwd> = ;
  794: 
  795: : undefined? ( ghost -- flag )
  796:   >magic @ dup <fwd> = swap <skip> = or ;
  797: 
  798: \ Predefined ghosts                                    12dec92py
  799: 
  800: ghost 0=                                        drop
  801: ghost branch    ghost ?branch                   2drop
  802: ghost (do)      ghost (?do)                     2drop
  803: ghost (for)                                     drop
  804: ghost (loop)    ghost (+loop)                   2drop
  805: ghost (next)                                    drop
  806: ghost unloop    ghost ;S                        2drop
  807: ghost lit       ghost (compile) ghost !         2drop drop
  808: ghost (does>)   ghost noop                      2drop
  809: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
  810: ghost '                                         drop
  811: ghost :docol    ghost :doesjump ghost :dodoes   2drop drop
  812: ghost :dovar	ghost :dodefer  ghost :dofield  2drop drop
  813: ghost over      ghost =         ghost drop      2drop drop
  814: ghost call      ghost useraddr  ghost execute   2drop drop
  815: ghost +         ghost -         ghost @         2drop drop
  816: ghost 2drop drop
  817: ghost 2dup drop
  818: 
  819: \ \ Parameter for target systems                         06oct92py
  820: 
  821: \ we define it ans like...
  822: wordlist Constant target-environment
  823: 
  824: VARIABLE env-current \ save information of current dictionary to restore with environ>
  825: 
  826: : >ENVIRON get-current env-current ! target-environment set-current ;
  827: : ENVIRON> env-current @ set-current ; 
  828: 
  829: >TARGET
  830: 
  831: : environment? ( adr len -- [ x ] true | false )
  832:   target-environment search-wordlist 
  833:   IF execute true ELSE false THEN ;
  834: 
  835: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
  836: 
  837: : has? 	bl word count T environment? H 
  838: 	IF 	\ environment variable is present, return its value
  839: 	ELSE	\ environment variable is not present, return false
  840: 		false \ debug true ABORT" arg" 
  841: 	THEN ;
  842: 
  843: : $has? T environment? H IF ELSE false THEN ;
  844: 
  845: >ENVIRON get-order get-current swap 1+ set-order
  846: true SetValue compiler
  847: true SetValue cross
  848: true SetValue standard-threading
  849: >TARGET previous
  850: 
  851: 0
  852: [IFDEF] mach-file mach-file count 1 [THEN]
  853: [IFDEF] machine-file machine-file 1 [THEN]
  854: [IF] 	included hex drop
  855: [ELSE]  cr ." No machine description!" ABORT 
  856: [THEN]
  857: 
  858: >ENVIRON
  859: 
  860: T has? ec H
  861: [IF]
  862: false DefaultValue relocate
  863: false DefaultValue file
  864: false DefaultValue OS
  865: false DefaultValue prims
  866: false DefaultValue floating
  867: false DefaultValue glocals
  868: false DefaultValue dcomps
  869: false DefaultValue hash
  870: false DefaultValue xconds
  871: false DefaultValue header
  872: false DefaultValue backtrace
  873: false DefaultValue new-input
  874: [THEN]
  875: 
  876: true DefaultValue interpreter
  877: true DefaultValue ITC
  878: false DefaultValue rom
  879: true DefaultValue standardthreading
  880: 
  881: >TARGET
  882: s" relocate" T environment? H 
  883: [IF]	SetValue NIL
  884: [ELSE]	>ENVIRON T NIL H SetValue relocate
  885: [THEN]
  886: 
  887: >CROSS
  888: 
  889: \ \ Create additional parameters                         19jan95py
  890: 
  891: \ currently cross only works for host machines with address-unit-bits
  892: \ eual to 8 because of s! and sc!
  893: \ but I start to query the environment just to modularize a little bit
  894: 
  895: : check-address-unit-bits ( -- )	
  896: \	s" ADDRESS-UNIT-BITS" environment?
  897: \	IF 8 <> ELSE true THEN
  898: \	ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
  899: 
  900: \	shit, this doesn't work because environment? is only defined for 
  901: \	gforth.fi and not kernl???.fi
  902: 	;
  903: 
  904: check-address-unit-bits
  905: 8 Constant bits/byte	\ we define: byte is address-unit
  906: 
  907: 1 bits/byte lshift Constant maxbyte 
  908: \ this sets byte size for the target machine, (probably right guess) jaw
  909: 
  910: T
  911: NIL		   	Constant TNIL
  912: cell               	Constant tcell
  913: cell<<             	Constant tcell<<
  914: cell>bit           	Constant tcell>bit
  915: bits/char          	Constant tbits/char
  916: bits/char H bits/byte T /      
  917: 			Constant tchar
  918: float             	Constant tfloat
  919: 1 bits/char lshift 	Constant tmaxchar
  920: [IFUNDEF] bits/byte
  921: 8			Constant tbits/byte
  922: [ELSE]
  923: bits/byte		Constant tbits/byte
  924: [THEN]
  925: H
  926: tbits/char bits/byte /	Constant tbyte
  927: 
  928: 
  929: \ Variables                                            06oct92py
  930: 
  931: Variable image
  932: Variable tlast    TNIL tlast !  \ Last name field
  933: Variable tlastcfa \ Last code field
  934: Variable tdoes    \ Resolve does> calls
  935: Variable bit$
  936: 
  937: \ statistics						10jun97jaw
  938: 
  939: Variable headers-named 0 headers-named !
  940: Variable user-vars 0 user-vars !
  941: 
  942: : target>bitmask-size ( u1 -- u2 )
  943:   1- tcell>bit rshift 1+ ;
  944: 
  945: : allocatetarget ( size --- adr )
  946:   dup allocate ABORT" CROSS: No memory for target"
  947:   swap over swap erase ;
  948: 
  949: \ \ memregion.fs
  950: 
  951: 
  952: Variable last-defined-region    \ pointer to last defined region
  953: Variable region-link            \ linked list with all regions
  954: Variable mirrored-link          \ linked list for mirrored regions
  955: 0 dup mirrored-link ! region-link !
  956: 
  957: 
  958: : >rname 6 cells + ;
  959: : >rbm   5 cells + ;
  960: : >rmem  4 cells + ;
  961: : >rlink 3 cells + ;
  962: : >rdp 2 cells + ;
  963: : >rlen cell+ ;
  964: : >rstart ;
  965: 
  966: 
  967: : region ( addr len -- )                \G create a new region
  968:   \ check whether predefined region exists 
  969:   save-input bl word find >r >r restore-input throw r> r> 0= 
  970:   IF	\ make region
  971: 	drop
  972: 	save-input create restore-input throw
  973: 	here last-defined-region !
  974: 	over ( startaddr ) , ( length ) , ( dp ) ,
  975: 	region-link linked 0 , 0 , bl word count string,
  976:   ELSE	\ store new parameters in region
  977:         bl word drop
  978: 	>body >r r@ last-defined-region !
  979: 	r@ >rlen ! dup r@ >rstart ! r> >rdp !
  980:   THEN ;
  981: 
  982: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
  983:   dup >rstart @ swap >rlen @ over + ;
  984: 
  985: : extent  ( region -- startaddr len )   \G returns the really used area
  986:   dup >rstart @ swap >rdp @ over - ;
  987: 
  988: : area ( region -- startaddr totallen ) \G returns the total area
  989:   dup >rstart @ swap >rlen @ ;
  990: 
  991: : mirrored                              \G mark a region as mirrored
  992:   mirrored-link
  993:   align linked last-defined-region @ , ;
  994: 
  995: : .addr ( u -- )
  996: \G prints a 16 or 32 Bit nice hex value
  997:   base @ >r hex
  998:   tcell 2 u>
  999:   IF s>d <# # # # # [char] . hold # # # # #> type
 1000:   ELSE s>d <# # # # # # #> type
 1001:   THEN r> base ! ;
 1002: 
 1003: : .regions                      \G display region statistic
 1004: 
 1005:   \ we want to list the regions in the right order
 1006:   \ so first collect all regions on stack
 1007:   0 region-link @
 1008:   BEGIN dup WHILE dup @ REPEAT drop
 1009:   BEGIN dup
 1010:   WHILE cr
 1011:         0 >rlink - >r
 1012:         r@ >rname count tuck type
 1013:         12 swap - 0 max spaces space
 1014:         ." Start: " r@ >rstart @ dup .addr space
 1015:         ." End: " r@ >rlen @ + .addr space
 1016:         ." DP: " r> >rdp @ .addr
 1017:   REPEAT drop
 1018:   s" rom" T $has? H 0= ?EXIT
 1019:   cr ." Mirrored:"
 1020:   mirrored-link @
 1021:   BEGIN dup
 1022:   WHILE space dup cell+ @ >rname count type @
 1023:   REPEAT drop cr
 1024:   ;
 1025: 
 1026: \ -------- predefined regions
 1027: 
 1028: 0 0 region address-space
 1029: \ total memory addressed and used by the target system
 1030: 
 1031: 0 0 region dictionary
 1032: \ rom area for the compiler
 1033: 
 1034: T has? rom H
 1035: [IF]
 1036: 0 0 region ram-dictionary mirrored
 1037: \ ram area for the compiler
 1038: [ELSE]
 1039: ' dictionary ALIAS ram-dictionary
 1040: [THEN]
 1041: 
 1042: 0 0 region return-stack
 1043: 
 1044: 0 0 region data-stack
 1045: 
 1046: 0 0 region tib-region
 1047: 
 1048: ' dictionary ALIAS rom-dictionary
 1049: 
 1050: 
 1051: : setup-target ( -- )   \G initialize targets memory space
 1052:   s" rom" T $has? H
 1053:   IF  \ check for ram and rom...
 1054:       \ address-space area nip 0<>
 1055:       ram-dictionary area nip 0<>
 1056:       rom-dictionary area nip 0<>
 1057:       and 0=
 1058:       ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
 1059:   THEN
 1060:   address-space area nip
 1061:   IF
 1062:       address-space area
 1063:   ELSE
 1064:       dictionary area
 1065:   THEN
 1066:   nip 0=
 1067:   ABORT" CROSS: define at least address-space or dictionary!!"
 1068: 
 1069:   \ allocate target for each region
 1070:   region-link
 1071:   BEGIN @ dup
 1072:   WHILE dup
 1073:         0 >rlink - >r
 1074:         r@ >rlen @
 1075:         IF      \ allocate mem
 1076:                 r@ >rlen @ dup
 1077: 
 1078:                 allocatetarget dup image !
 1079:                 r@ >rmem !
 1080: 
 1081:                 target>bitmask-size allocatetarget
 1082:                 dup bit$ !
 1083:                 r> >rbm !
 1084: 
 1085:         ELSE    r> drop THEN
 1086:    REPEAT drop ;
 1087: 
 1088: \ MakeKernal                                           		22feb99jaw
 1089: 
 1090: : makekernel ( targetsize -- targetsize )
 1091:   dup dictionary >rlen ! setup-target ;
 1092: 
 1093: >MINIMAL
 1094: : makekernel makekernel ;
 1095: >CROSS
 1096: 
 1097: \ \ switched tdp for rom support				03jun97jaw
 1098: 
 1099: \ second value is here to store some maximal value for statistics
 1100: \ tempdp is also embedded here but has nothing to do with rom support
 1101: \ (needs switched dp)
 1102: 
 1103: variable tempdp	0 ,	\ temporary dp for resolving
 1104: variable tempdp-save
 1105: 
 1106: 0 [IF]
 1107: variable romdp 0 ,      \ Dictionary-Pointer for ramarea
 1108: variable ramdp 0 ,      \ Dictionary-Pointer for romarea
 1109: 
 1110: \
 1111: variable sramdp		\ start of ram-area for forth
 1112: variable sromdp		\ start of rom-area for forth
 1113: 
 1114: [THEN]
 1115: 
 1116: 
 1117: 0 value tdp
 1118: variable fixed		\ flag: true: no automatic switching
 1119: 			\	false: switching is done automatically
 1120: 
 1121: \ Switch-Policy:
 1122: \
 1123: \ a header is always compiled into rom
 1124: \ after a created word (create and variable) compilation goes to ram
 1125: \
 1126: \ Be careful: If you want to make the data behind create into rom
 1127: \ you have to put >rom before create!
 1128: 
 1129: variable constflag constflag off
 1130: 
 1131: : activate ( region -- )
 1132: \G next code goes to this region
 1133:   >rdp to tdp ;
 1134: 
 1135: : (switchram)
 1136:   fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
 1137:   ram-dictionary activate ;
 1138: 
 1139: : switchram
 1140:   constflag @
 1141:   IF constflag off ELSE (switchram) THEN ;
 1142: 
 1143: : switchrom
 1144:   fixed @ ?EXIT rom-dictionary activate ;
 1145: 
 1146: : >tempdp ( addr -- ) 
 1147:   tdp tempdp-save ! tempdp to tdp tdp ! ;
 1148: : tempdp> ( -- )
 1149:   tempdp-save @ to tdp ;
 1150: 
 1151: : >ram  fixed off (switchram) fixed on ;
 1152: : >rom  fixed off switchrom fixed on ;
 1153: : >auto fixed off switchrom ;
 1154: 
 1155: 
 1156: 
 1157: \ : romstart dup sromdp ! romdp ! ;
 1158: \ : ramstart dup sramdp ! ramdp ! ;
 1159: 
 1160: \ default compilation goes to rom
 1161: \ when romable support is off, only the rom switch is used (!!)
 1162: >auto
 1163: 
 1164: : there  tdp @ ;
 1165: 
 1166: >TARGET
 1167: 
 1168: \ \ Target Memory Handling
 1169: 
 1170: \ Byte ordering and cell size                          06oct92py
 1171: 
 1172: : cell+         tcell + ;
 1173: : cells         tcell<< lshift ;
 1174: : chars         tchar * ;
 1175: : char+		tchar + ;
 1176: : floats	tfloat * ;
 1177:     
 1178: >CROSS
 1179: : cell/         tcell<< rshift ;
 1180: >TARGET
 1181: 20 CONSTANT bl
 1182: \ TNIL Constant NIL
 1183: 
 1184: >CROSS
 1185: 
 1186: bigendian
 1187: [IF]
 1188:    : S!  ( n addr -- )  >r s>d r> tcell bounds swap 1-
 1189:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1190:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds
 1191:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1192:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds swap 1-
 1193:      DO  maxbyte ud/mod rot I c!  -1 +LOOP  2drop ;
 1194:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds
 1195:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  LOOP d>s ;
 1196: [ELSE]
 1197:    : S!  ( n addr -- )  >r s>d r> tcell bounds
 1198:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1199:    : S@  ( addr -- n )  >r 0 0 r> tcell bounds swap 1-
 1200:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1201:    : Sc!  ( n addr -- )  >r s>d r> tchar bounds
 1202:      DO  maxbyte ud/mod rot I c!  LOOP  2drop ;
 1203:    : Sc@  ( addr -- n )  >r 0 0 r> tchar bounds swap 1-
 1204:      DO  maxbyte * swap maxbyte um* rot + swap I c@ + swap  -1 +LOOP d>s ;
 1205: [THEN]
 1206: 
 1207: : taddr>region ( taddr -- region | 0 )
 1208: \G finds for a target-address the correct region
 1209: \G returns 0 if taddr is not in range of a target memory region
 1210:   region-link
 1211:   BEGIN @ dup
 1212:   WHILE dup >r
 1213:         0 >rlink - >r
 1214:         r@ >rlen @
 1215:         IF      dup r@ borders within
 1216:                 IF r> r> drop nip EXIT THEN
 1217:         THEN
 1218:         r> drop
 1219:         r>
 1220:   REPEAT
 1221:   2drop 0 ;
 1222: 
 1223: : (>regionimage) ( taddr -- 'taddr )
 1224:   dup
 1225:   \ find region we want to address
 1226:   taddr>region dup 0= ABORT" Address out of range!"
 1227:   >r
 1228:   \ calculate offset in region
 1229:   r@ >rstart @ -
 1230:   \ add regions real address in our memory
 1231:   r> >rmem @ + ;
 1232: 
 1233: \ Bit string manipulation                               06oct92py
 1234: \                                                       9may93jaw
 1235: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
 1236: : bits ( n -- n ) chars Bittable + c@ ;
 1237: 
 1238: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
 1239: : +bit ( addr n -- )  >bit over c@ or swap c! ;
 1240: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
 1241: 
 1242: : (relon) ( taddr -- )  
 1243:   [ [IFDEF] fd-relocation-table ]
 1244:   s" +" fd-relocation-table write-file throw
 1245:   dup s>d <# #s #> fd-relocation-table write-line throw
 1246:   [ [THEN] ]
 1247:   bit$ @ swap cell/ +bit ;
 1248: 
 1249: : (reloff) ( taddr -- ) 
 1250:   [ [IFDEF] fd-relocation-table ]
 1251:   s" -" fd-relocation-table write-file throw
 1252:   dup s>d <# #s #> fd-relocation-table write-line throw
 1253:   [ [THEN] ]
 1254:   bit$ @ swap cell/ -bit ;
 1255: 
 1256: : (>image) ( taddr -- absaddr ) image @ + ;
 1257: 
 1258: DEFER >image
 1259: DEFER relon
 1260: DEFER reloff
 1261: DEFER correcter
 1262: 
 1263: T has? relocate H
 1264: [IF]
 1265: ' (relon) IS relon
 1266: ' (reloff) IS reloff
 1267: ' (>image) IS >image
 1268: [ELSE]
 1269: ' drop IS relon
 1270: ' drop IS reloff
 1271: ' (>regionimage) IS >image
 1272: [THEN]
 1273: 
 1274: \ Target memory access                                 06oct92py
 1275: 
 1276: : align+  ( taddr -- rest )
 1277:     tcell tuck 1- and - [ tcell 1- ] Literal and ;
 1278: : cfalign+  ( taddr -- rest )
 1279:     \ see kernel.fs:cfaligned
 1280:     /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
 1281: 
 1282: >TARGET
 1283: : aligned ( taddr -- ta-addr )  dup align+ + ;
 1284: \ assumes cell alignment granularity (as GNU C)
 1285: 
 1286: : cfaligned ( taddr1 -- taddr2 )
 1287:     \ see kernel.fs
 1288:     dup cfalign+ + ;
 1289: 
 1290: : @  ( taddr -- w )     >image S@ ;
 1291: : !  ( w taddr -- )     >image S! ;
 1292: : c@ ( taddr -- char )  >image Sc@ ;
 1293: : c! ( char taddr -- )  >image Sc! ;
 1294: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
 1295: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
 1296: 
 1297: \ Target compilation primitives                        06oct92py
 1298: \ included A!                                          16may93jaw
 1299: 
 1300: : here  ( -- there )    there ;
 1301: : allot ( n -- )        tdp +! ;
 1302: : ,     ( w -- )        T here H tcell T allot  ! H ;
 1303: : c,    ( char -- )     T here H tchar T allot c! H ;
 1304: : align ( -- )          T here H align+ 0 ?DO  bl T c, H tchar +LOOP ;
 1305: : cfalign ( -- )
 1306:     T here H cfalign+ 0 ?DO  bl T c, H tchar +LOOP ;
 1307: 
 1308: : >address		dup 0>= IF tbyte / THEN ; \ ?? jaw 
 1309: : A!                    swap >address swap dup relon T ! H ;
 1310: : A,    ( w -- )        >address T here H relon T , H ;
 1311: 
 1312: >CROSS
 1313: 
 1314: : tcmove ( source dest len -- )
 1315: \G cmove in target memory
 1316:   tchar * bounds
 1317:   ?DO  dup T c@ H I T c! H 1+
 1318:   tchar +LOOP  drop ;
 1319: 
 1320: \ \ Load Assembler
 1321: 
 1322: >TARGET
 1323: H also Forth definitions
 1324: 
 1325: : X 	bl word count [ ' target >wordlist ] Literal search-wordlist
 1326: 	IF	state @ IF compile,
 1327: 		ELSE execute THEN
 1328: 	ELSE	-1 ABORT" Cross: access method not supported!"
 1329: 	THEN ; immediate
 1330: 
 1331: [IFDEF] asm-include asm-include [THEN] hex
 1332: 
 1333: previous
 1334: >CROSS H
 1335: 
 1336: \ \ --------------------        Compiler Plug Ins               01aug97jaw
 1337: 
 1338: >TARGET
 1339: DEFER >body             \ we need the system >body
 1340: 			\ and the target >body
 1341: >CROSS
 1342: T 2 cells H VALUE xt>body
 1343: DEFER doprim,	\ compiles start of a primitive
 1344: DEFER docol,   	\ compiles start of a colon definition
 1345: DEFER doer,		
 1346: DEFER fini,      \ compiles end of definition ;s
 1347: DEFER doeshandler,
 1348: DEFER dodoes,
 1349: 
 1350: DEFER ]comp     \ starts compilation
 1351: DEFER comp[     \ ends compilation
 1352: 
 1353: : (cc) T a, H ;					' (cc) IS colon,
 1354: 
 1355: : (cr) >tempdp ]comp colon, comp[ tempdp> ; 	' (cr) IS colon-resolve
 1356: : (ar) T ! H ;					' (ar) IS addr-resolve
 1357: : (dr)  ( ghost res-pnt target-addr addr )
 1358: 	>tempdp drop over 
 1359: 	dup >magic @ <do:> =
 1360: 	IF 	doer,
 1361: 	ELSE	dodoes,
 1362: 	THEN 
 1363: 	tempdp> ;				' (dr) IS doer-resolve
 1364: 
 1365: : (cm) ( -- addr )
 1366:     T here align H
 1367:     -1 colon, ;					' (cm) IS colonmark,
 1368: 
 1369: >TARGET
 1370: : compile, colon, ;
 1371: >CROSS
 1372: 
 1373: : refered ( ghost tag -- )
 1374: \G creates a resolve structure
 1375:     T here aligned H swap (refered)
 1376:   ;
 1377: 
 1378: : killref ( addr ghost -- )
 1379: \G kills a forward reference to ghost at position addr
 1380: \G this is used to eleminate a :dovar refence after making a DOES>
 1381:     dup >magic @ <fwd> <> IF 2drop EXIT THEN
 1382:     swap >r >link
 1383:     BEGIN dup @ dup  ( addr last this )
 1384:     WHILE dup >taddr @ r@ =
 1385:  	 IF   @ over !
 1386: 	 ELSE nip THEN
 1387:     REPEAT rdrop 2drop 
 1388:   ;
 1389: 
 1390: Defer resolve-warning
 1391: 
 1392: : reswarn-test ( ghost res-struct -- ghost res-struct )
 1393:   over cr ." Resolving " .ghost dup ."  in " >ghost @ .ghost ;
 1394: 
 1395: : reswarn-forward ( ghost res-struct -- ghost res-struct )
 1396:   over warnhead .ghost dup ."  is referenced in " 
 1397:   >ghost @ .ghost ;
 1398: 
 1399: \ ' reswarn-test IS resolve-warning
 1400:  
 1401: \ resolve                                              14oct92py
 1402: 
 1403:  : resolve-loop ( ghost resolve-list tcfa -- )
 1404:     >r
 1405:     BEGIN dup WHILE 
 1406: \  	  dup >tag @ 2 = IF reswarn-forward THEN
 1407: 	  resolve-warning 
 1408: 	  r@ over >taddr @ 
 1409: 	  2 pick >tag @
 1410: 	  CASE	0 OF colon-resolve ENDOF
 1411: 		1 OF addr-resolve ENDOF
 1412: 		2 OF doer-resolve ENDOF
 1413: 	  ENDCASE
 1414: 	  @ \ next list element
 1415:     REPEAT 2drop rdrop 
 1416:   ;
 1417: 
 1418: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
 1419: \  >r dup >link @
 1420: \  BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
 1421: 
 1422: \ exists                                                9may93jaw
 1423: 
 1424: Variable TWarnings
 1425: TWarnings on
 1426: Variable Exists-Warnings
 1427: Exists-Warnings on
 1428: 
 1429: : exists ( ghost tcfa -- )
 1430:   over GhostNames
 1431:   BEGIN @ dup
 1432:   WHILE 2dup cell+ @ =
 1433:   UNTIL
 1434:         2 cells + count
 1435:         TWarnings @ Exists-Warnings @ and
 1436:         IF warnhead type ."  exists"
 1437:         ELSE 2drop THEN
 1438:         drop swap >link !
 1439:   ELSE  true abort" CROSS: Ghostnames inconsistent "
 1440:   THEN ;
 1441: 
 1442: : is-resolved   ( ghost -- )
 1443:   >link @ colon, ; \ compile-call
 1444: 
 1445: : resolve  ( ghost tcfa -- )
 1446: \G resolve referencies to ghost with tcfa
 1447:     \ is ghost resolved?, second resolve means another definition with the
 1448:     \ same name
 1449:     over undefined? 0= IF  exists EXIT THEN
 1450:     \ get linked-list
 1451:     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
 1452:     \ mark ghost as resolved
 1453:     dup r@ >link ! <res> r@ >magic !
 1454:     r@ >comp @ ['] is-forward = IF  ['] is-resolved r@ >comp !  THEN
 1455:     \ loop through forward referencies
 1456:     r> -rot 
 1457:     comp-state @ >r Resolving comp-state !
 1458:     resolve-loop 
 1459:     r> comp-state !
 1460: 
 1461:     ['] noop IS resolve-warning 
 1462:   ;
 1463: 
 1464: \ gexecute ghost,                                      01nov92py
 1465: 
 1466: : gexecute   ( ghost -- )
 1467:     dup >comp @ execute ;
 1468: 
 1469: : addr,  ( ghost -- )
 1470:   dup forward? IF  1 refered 0 T a, H ELSE >link @ T a, H THEN ;
 1471: 
 1472: \ !! : ghost,     ghost  gexecute ;
 1473: 
 1474: \ .unresolved                                          11may93jaw
 1475: 
 1476: variable ResolveFlag
 1477: 
 1478: \ ?touched                                             11may93jaw
 1479: 
 1480: : ?touched ( ghost -- flag ) dup forward? swap >link @
 1481:                                0 <> and ;
 1482: 
 1483: : .forwarddefs ( ghost -- )
 1484: 	."  appeared in:"
 1485: 	>link
 1486: 	BEGIN	@ dup
 1487: 	WHILE	cr 5 spaces
 1488: 		dup >ghost @ .ghost
 1489: 		."  file " dup >file @ ?dup IF count type ELSE ." CON" THEN
 1490: 		."  line " dup >line @ .dec
 1491: 	REPEAT 
 1492: 	drop ;
 1493: 
 1494: : ?resolved  ( ghostname -- )
 1495:   dup cell+ @ ?touched
 1496:   IF  	dup 
 1497: 	cell+ cell+ count cr type ResolveFlag on 
 1498: 	cell+ @ .forwarddefs
 1499:   ELSE 	drop 
 1500:   THEN ;
 1501: 
 1502: : .unresolved  ( -- )
 1503:   ResolveFlag off cr ." Unresolved: "
 1504:   Ghostnames
 1505:   BEGIN @ dup
 1506:   WHILE dup ?resolved
 1507:   REPEAT drop ResolveFlag @
 1508:   IF
 1509:       -1 abort" Unresolved words!"
 1510:   ELSE
 1511:       ." Nothing!"
 1512:   THEN
 1513:   cr ;
 1514: 
 1515: : .stats
 1516:   base @ >r decimal
 1517:   cr ." named Headers: " headers-named @ . 
 1518:   r> base ! ;
 1519: 
 1520: >MINIMAL
 1521: 
 1522: : .unresolved .unresolved ;
 1523: 
 1524: >CROSS
 1525: \ Header states                                        12dec92py
 1526: 
 1527: bigendian [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+
 1528: : flag! ( w -- )   tlast @ flag+ + dup >r T c@ xor r> c! H ;
 1529: 
 1530: VARIABLE ^imm
 1531: 
 1532: \ !! should be target wordsize specific
 1533: $80 constant alias-mask
 1534: $40 constant immediate-mask
 1535: $20 constant restrict-mask
 1536: 
 1537: >TARGET
 1538: : immediate     immediate-mask flag!
 1539:                 ^imm @ @ dup <imm> = IF  drop  EXIT  THEN
 1540:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
 1541:                 <imm> ^imm @ ! ;
 1542: : restrict      restrict-mask flag! ;
 1543: 
 1544: : isdoer	
 1545: \G define a forth word as doer, this makes obviously only sence on
 1546: \G forth processors such as the PSC1000
 1547: 		<do:> last-header-ghost @ >magic ! ;
 1548: >CROSS
 1549: 
 1550: \ Target Header Creation                               01nov92py
 1551: 
 1552: >TARGET
 1553: : string,  ( addr count -- )
 1554:     dup T c, H bounds  ?DO  I c@ T c, H  LOOP ;
 1555: : lstring, ( addr count -- )
 1556:     dup T , H bounds  ?DO  I c@ T c, H  LOOP ;
 1557: : name,  ( "name" -- )  bl word count T lstring, cfalign H ;
 1558: : view,   ( -- ) ( dummy ) ;
 1559: >CROSS
 1560: 
 1561: \ Target Document Creation (goes to crossdoc.fd)       05jul95py
 1562: 
 1563: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
 1564: \ contains the file-id of the documentation file
 1565: 
 1566: : T-\G ( -- )
 1567:     source >in @ /string doc-file-id write-line throw
 1568:     postpone \ ;
 1569: 
 1570: Variable to-doc  to-doc on
 1571: 
 1572: : cross-doc-entry  ( -- )
 1573:     to-doc @ tlast @ 0<> and	\ not an anonymous (i.e. noname) header
 1574:     IF
 1575: 	s" " doc-file-id write-line throw
 1576: 	s" make-doc " doc-file-id write-file throw
 1577: 
 1578: 	tlast @ >image count 1F and doc-file-id write-file throw
 1579: 	>in @
 1580: 	[char] ( parse 2drop
 1581: 	[char] ) parse doc-file-id write-file throw
 1582: 	s"  )" doc-file-id write-file throw
 1583: 	[char] \ parse 2drop					
 1584: 	T-\G
 1585: 	>in !
 1586:     THEN ;
 1587: 
 1588: \ Target TAGS creation
 1589: 
 1590: s" kernel.TAGS" r/w create-file throw value tag-file-id
 1591: \ contains the file-id of the tags file
 1592: 
 1593: Create tag-beg 2 c,  7F c, bl c,
 1594: Create tag-end 2 c,  bl c, 01 c,
 1595: Create tag-bof 1 c,  0C c,
 1596: 
 1597: 2variable last-loadfilename 0 0 last-loadfilename 2!
 1598: 	    
 1599: : put-load-file-name ( -- )
 1600:     sourcefilename last-loadfilename 2@ d<>
 1601:     IF
 1602: 	tag-bof count tag-file-id write-line throw
 1603: 	sourcefilename 2dup
 1604: 	tag-file-id write-file throw
 1605: 	last-loadfilename 2!
 1606: 	s" ,0" tag-file-id write-line throw
 1607:     THEN ;
 1608: 
 1609: : cross-tag-entry  ( -- )
 1610:     tlast @ 0<>	\ not an anonymous (i.e. noname) header
 1611:     IF
 1612: 	put-load-file-name
 1613: 	source >in @ min tag-file-id write-file throw
 1614: 	tag-beg count tag-file-id write-file throw
 1615: 	tlast @ >image count 1F and tag-file-id write-file throw
 1616: 	tag-end count tag-file-id write-file throw
 1617: 	base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
 1618: \	>in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
 1619: 	s" ,0" tag-file-id write-line throw
 1620: 	base !
 1621:     THEN ;
 1622: 
 1623: \ Check for words
 1624: 
 1625: Defer skip? ' false IS skip?
 1626: 
 1627: : skipdef ( <name> -- )
 1628: \G skip definition of an undefined word in undef-words and
 1629: \G all-words mode
 1630:     ghost dup forward?
 1631:     IF  >magic <skip> swap !
 1632:     ELSE drop THEN ;
 1633: 
 1634: : tdefined? ( -- flag ) \ name
 1635:     ghost undefined? 0= ;
 1636: 
 1637: : defined2? ( -- flag ) \ name
 1638: \G return true for anything else than forward, even for <skip>
 1639: \G that's what we want
 1640:     ghost forward? 0= ;
 1641: 
 1642: : forced? ( -- flag ) \ name
 1643: \G return ture if it is a foreced skip with defskip
 1644:     ghost >magic @ <skip> = ;
 1645: 
 1646: : needed? ( -- flag ) \ name
 1647: \G returns a false flag when
 1648: \G a word is not defined
 1649: \G a forward reference exists
 1650: \G so the definition is not skipped!
 1651:     bl word gfind
 1652:     IF dup undefined?
 1653: 	nip
 1654: 	0=
 1655:     ELSE  drop true  THEN ;
 1656: 
 1657: : doer? ( -- flag ) \ name
 1658:     ghost >magic @ <do:> = ;
 1659: 
 1660: : skip-defs ( -- )
 1661:     BEGIN  refill  WHILE  source -trailing nip 0= UNTIL  THEN ;
 1662: 
 1663: \ Target header creation
 1664: 
 1665: Variable NoHeaderFlag
 1666: NoHeaderFlag off
 1667: 
 1668: : 0.r ( n1 n2 -- ) 
 1669:     base @ >r hex 
 1670:     0 swap <# 0 ?DO # LOOP #> type 
 1671:     r> base ! ;
 1672: 
 1673: : .sym ( adr len -- )
 1674: \G escapes / and \ to produce sed output
 1675:   bounds 
 1676:   DO I c@ dup
 1677: 	CASE	[char] / OF drop ." \/" ENDOF
 1678: 		[char] \ OF drop ." \\" ENDOF
 1679: 		dup OF emit ENDOF
 1680: 	ENDCASE
 1681:     LOOP ;
 1682: 
 1683: : (Theader ( "name" -- ghost )
 1684:     \  >in @ bl word count type 2 spaces >in !
 1685:     \ wordheaders will always be compiled to rom
 1686:     switchrom
 1687:     \ build header in target
 1688:     NoHeaderFlag @
 1689:     IF  NoHeaderFlag off
 1690:     ELSE
 1691: 	T align H view,
 1692: 	tlast @ dup 0> IF tcell - THEN T A, H  there tlast !
 1693: 	1 headers-named +!	\ Statistic
 1694: 	>in @ T name, H >in !
 1695:     THEN
 1696:     T cfalign here H tlastcfa !
 1697:     \ Old Symbol table sed-script
 1698: \    >in @ cr ." sym:s/CFA=" there 4 0.r ." /"  bl word count .sym ." /g" cr >in !
 1699:     ghost
 1700:     \ output symbol table to extra file
 1701:     [ [IFDEF] fd-symbol-table ]
 1702:       base @ hex there s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base !
 1703:       s" :" fd-symbol-table write-file throw
 1704:       dup >ghostname fd-symbol-table write-line throw
 1705:     [ [THEN] ]
 1706:     dup Last-Header-Ghost !
 1707:     dup >magic ^imm !     \ a pointer for immediate
 1708:     Already @
 1709:     IF  dup >end tdoes !
 1710:     ELSE 0 tdoes !
 1711:     THEN
 1712:     alias-mask flag!
 1713:     cross-doc-entry cross-tag-entry ;
 1714: 
 1715: VARIABLE ;Resolve 1 cells allot
 1716: \ this is the resolver information from ":"
 1717: \ resolving is done by ";"
 1718: 
 1719: : Theader  ( "name" -- ghost )
 1720:   (THeader dup there resolve 0 ;Resolve ! ;
 1721: 
 1722: >TARGET
 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 prim: " >in @ bl word count type >in ! cr
 1728:     THEN
 1729:     (THeader over resolve T A, H alias-mask flag! ;
 1730: : Alias:   ( cfa -- ) \ name
 1731:     >in @ skip? IF  2drop  EXIT  THEN  >in !
 1732:     dup 0< s" prims" T $has? H 0= and
 1733:     IF
 1734: 	.sourcepos ." needs doer: " >in @ bl word count type >in ! cr
 1735:     THEN
 1736:     ghost tuck swap resolve <do:> swap >magic ! ;
 1737: 
 1738: Variable prim#
 1739: : first-primitive ( n -- )  prim# ! ;
 1740: : Primitive  ( -- ) \ name
 1741:     prim# @ T Alias H  -1 prim# +! ;
 1742: >CROSS
 1743: 
 1744: \ Conditionals and Comments                            11may93jaw
 1745: 
 1746: : ;Cond
 1747:   postpone ;
 1748:   swap ! ;  immediate
 1749: 
 1750: : Cond: ( -- ) \ name {code } ;
 1751:   atonce on
 1752:   ghost
 1753:   >exec
 1754:   :NONAME ;
 1755: 
 1756: : restrict? ( -- )
 1757: \ aborts on interprete state - ae
 1758:   state @ 0= ABORT" CROSS: Restricted" ;
 1759: 
 1760: : Comment ( -- )
 1761:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
 1762: 
 1763: Comment (       Comment \
 1764: 
 1765: \ compile                                              10may93jaw
 1766: 
 1767: : compile  ( -- ) \ name
 1768:   restrict?
 1769:   bl word gfind dup 0= ABORT" CROSS: Can't compile "
 1770:   0> ( immediate? )
 1771:   IF    >exec @ compile,
 1772:   ELSE  postpone literal postpone gexecute  THEN ;
 1773:                                         immediate
 1774: 
 1775: : [G'] 
 1776: \G ticks a ghost and returns its address
 1777:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1778:   state @
 1779:   IF   postpone literal
 1780:   THEN ; immediate
 1781: 
 1782: : ghost>cfa
 1783:   dup undefined? ABORT" CROSS: forward " >link @ ;
 1784:                
 1785: >TARGET
 1786: 
 1787: : '  ( -- cfa ) 
 1788: \ returns the target-cfa of a ghost
 1789:   bl word gfind 0= ABORT" CROSS: Ghost don't exists"
 1790:   ghost>cfa ;
 1791: 
 1792: Cond: [']  T ' H alit, ;Cond
 1793: 
 1794: >CROSS
 1795: 
 1796: : [T']
 1797: \ returns the target-cfa of a ghost, or compiles it as literal
 1798:   postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
 1799: 
 1800: \ \ threading modell					13dec92py
 1801: \ modularized						14jun97jaw
 1802: 
 1803: : fillcfa   ( usedcells -- )
 1804:   T cells H xt>body swap - 0 ?DO 0 X c, tchar +LOOP ;
 1805: 
 1806: : (>body)   ( cfa -- pfa ) xt>body + ;		' (>body) T IS >body H
 1807: 
 1808: : (doer,)   ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ;   ' (doer,) IS doer,
 1809: 
 1810: : (docol,)  ( -- ) [G'] :docol doer, ;		' (docol,) IS docol,
 1811: 
 1812: : (doprim,) ( -- )
 1813:   there xt>body + ca>native T a, H 1 fillcfa ;	' (doprim,) IS doprim,
 1814: 
 1815: : (doeshandler,) ( -- ) 
 1816:   T cfalign H compile :doesjump T 0 , H ; 	' (doeshandler,) IS doeshandler,
 1817: 
 1818: : (dodoes,) ( does-action-ghost -- )
 1819:   ]comp [G'] :dodoes gexecute comp[
 1820:   addr,
 1821:   T here H tcell - reloff 2 fillcfa ;		' (dodoes,) IS dodoes,
 1822: 
 1823: : (lit,) ( n -- )   compile lit T  ,  H ;	' (lit,) IS lit,
 1824: 
 1825: \ if we dont produce relocatable code alit, defaults to lit, jaw
 1826: \ this is just for convenience, so we don't have to define alit,
 1827: \ seperately for embedded systems....
 1828: T has? relocate H
 1829: [IF]
 1830: : (alit,) ( n -- )  compile lit T  a, H ;	' (alit,) IS alit,
 1831: [ELSE]
 1832: : (alit,) ( n -- )  lit, ;			' (alit,) IS alit,
 1833: [THEN]
 1834: 
 1835: : (fini,)         compile ;s ;                ' (fini,) IS fini,
 1836: 
 1837: [IFUNDEF] (code) 
 1838: Defer (code)
 1839: Defer (end-code)
 1840: [THEN]
 1841: 
 1842: >TARGET
 1843: : Code
 1844:   defempty?
 1845:   (THeader there resolve
 1846:   [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
 1847:   doprim, 
 1848:   [THEN]
 1849:   depth (code) ;
 1850: 
 1851: : Code:
 1852:   defempty?
 1853:     ghost dup there ca>native resolve  <do:> swap >magic !
 1854:     depth (code) ;
 1855: 
 1856: : end-code
 1857:     (end-code)
 1858:     depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1859:     ELSE true ABORT" CROSS: Stack empty" THEN
 1860:     ;
 1861: 
 1862: >CROSS
 1863: 
 1864: \ tLiteral                                             12dec92py
 1865: 
 1866: >TARGET
 1867: Cond: \G  T-\G ;Cond
 1868: 
 1869: Cond:  Literal ( n -- )   restrict? lit, ;Cond
 1870: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
 1871: 
 1872: : Char ( "<char>" -- )  bl word char+ c@ ;
 1873: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
 1874: 
 1875: \ some special literals					27jan97jaw
 1876: 
 1877: \ !! Known Bug: Special Literals and plug-ins work only correct
 1878: \ on targets with char = 8 bit
 1879: 
 1880: Cond: MAXU
 1881:   restrict? 
 1882:   compile lit tcell 0 ?DO FF T c, H LOOP 
 1883:   ;Cond
 1884: 
 1885: Cond: MINI
 1886:   restrict?
 1887:   compile lit bigendian 
 1888:   IF	80 T c, H tcell 1 ?DO 0 T c, H LOOP 
 1889:   ELSE  tcell 1 ?DO 0 T c, H LOOP 80 T c, H
 1890:   THEN
 1891:   ;Cond
 1892:  
 1893: Cond: MAXI
 1894:  restrict?
 1895:  compile lit bigendian 
 1896:  IF 	7F T c, H tcell 1 ?DO FF T c, H LOOP
 1897:  ELSE 	tcell 1 ?DO FF T c, H LOOP 7F T c, H
 1898:  THEN
 1899:  ;Cond
 1900: 
 1901: >CROSS
 1902: \ Target compiling loop                                12dec92py
 1903: \ ">tib trick thrown out                               10may93jaw
 1904: \ number? defined at the top                           11may93jaw
 1905: \ replaced >in by save-input				
 1906: 
 1907: : discard 0 ?DO drop LOOP ;
 1908: 
 1909: \ compiled word might leave items on stack!
 1910: : tcom ( x1 .. xn n name -- )
 1911: \  dup count type space
 1912:   gfind  ?dup
 1913:   IF    >r >r discard r> r>
 1914: 	0> IF	>exec @ execute
 1915: 	ELSE	gexecute  THEN 
 1916: 	EXIT 
 1917:   THEN
 1918:   number? dup  
 1919:   IF	0> IF swap lit,  THEN  lit, discard
 1920:   ELSE	2drop restore-input throw ghost gexecute THEN  ;
 1921: 
 1922: >TARGET
 1923: \ : ; DOES>                                            13dec92py
 1924: \ ]                                                     9may93py/jaw
 1925: 
 1926: : ] state on
 1927:     Compiling comp-state !
 1928:     BEGIN
 1929:         BEGIN save-input bl word
 1930:               dup c@ 0= WHILE drop discard refill 0=
 1931:               ABORT" CROSS: End of file while target compiling"
 1932:         REPEAT
 1933:         tcom
 1934:         state @
 1935:         0=
 1936:     UNTIL ;
 1937: 
 1938: \ by the way: defining a second interpreter (a compiler-)loop
 1939: \             is not allowed if a system should be ans conform
 1940: 
 1941: : : ( -- colon-sys ) \ Name
 1942:   defempty?
 1943:   constflag off \ don't let this flag work over colon defs
 1944: 		\ just to go sure nothing unwanted happens
 1945:   >in @ skip? IF  drop skip-defs  EXIT  THEN  >in !
 1946:   (THeader ;Resolve ! there ;Resolve cell+ !
 1947:   docol, ]comp depth T ] H ;
 1948: 
 1949: : :noname ( -- colon-sys )
 1950:   T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
 1951: 
 1952: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
 1953: 
 1954: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
 1955: 
 1956: >CROSS
 1957: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
 1958:          ;Resolve cell+ @ ;
 1959: 
 1960: >TARGET
 1961: 
 1962: Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
 1963: 
 1964: Cond: ; ( -- ) restrict?
 1965:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
 1966:                           ELSE true ABORT" CROSS: Stack empty" THEN
 1967:                fini,
 1968:                comp[
 1969:                state off
 1970:                ;Resolve @
 1971:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
 1972: 		Interpreting comp-state !
 1973:                ;Cond
 1974: Cond: [  restrict? state off Interpreting comp-state ! ;Cond
 1975: 
 1976: >CROSS
 1977: 
 1978: Create GhostDummy ghostheader
 1979: <res> GhostDummy >magic !
 1980: 
 1981: : !does ( does-action -- )
 1982: \ !! zusammenziehen und dodoes, machen!
 1983:     tlastcfa @ [G'] :dovar killref
 1984: \    tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
 1985: \ !! geht so nicht, da dodoes, ghost will!
 1986:     GhostDummy >link ! GhostDummy 
 1987:     tlastcfa @ >tempdp dodoes, tempdp> ;
 1988: 
 1989: >TARGET
 1990: Cond: DOES> restrict?
 1991:         compile (does>) doeshandler, 
 1992: 	\ resolve words made by builders
 1993: 	tdoes @ ?dup IF  @ T here H resolve THEN
 1994:         ;Cond
 1995: : DOES> switchrom doeshandler, T here H !does depth T ] H ;
 1996: 
 1997: >CROSS
 1998: \ Creation                                             01nov92py
 1999: 
 2000: \ Builder                                               11may93jaw
 2001: 
 2002: : Builder    ( Create-xt do:-xt "name" -- )
 2003: \ builds up a builder in current vocabulary
 2004: \ create-xt is executed when word is interpreted
 2005: \ do:-xt is executet when the created word from builder is executed
 2006: \ for do:-xt an additional entry after the normal ghost-enrys is used
 2007: 
 2008:   Make-Ghost 		( Create-xt do:-xt ghost )
 2009:   rot swap		( do:-xt Create-xt ghost )
 2010:   >exec ! , ;
 2011: 
 2012: : gdoes,  ( ghost -- )
 2013: \ makes the codefield for a word that is built
 2014:   >end @ dup undefined? 0=
 2015:   IF
 2016: 	dup >magic @ <do:> =
 2017: 	IF 	 doer, 
 2018: 	ELSE	dodoes,
 2019: 	THEN
 2020: 	EXIT
 2021:   THEN
 2022: \  compile :dodoes gexecute
 2023: \  T here H tcell - reloff 
 2024:   2 refered 
 2025:   0 fillcfa
 2026:   ;
 2027: 
 2028: : TCreate ( <name> -- )
 2029:   executed-ghost @
 2030:   create-forward-warn
 2031:   IF ['] reswarn-forward IS resolve-warning THEN
 2032:   Theader >r dup gdoes,
 2033: \ stores execution semantic in the built word
 2034: \ if the word already has a semantic (concerns S", IS, .", DOES>)
 2035: \ then keep it
 2036:   >end @
 2037:   dup >exec @ r@ >exec dup @ ['] NoExec =  IF ! ELSE 2drop THEN
 2038:   >comp @ r> >comp ! ;
 2039: 
 2040: : RTCreate ( <name> -- )
 2041: \ creates a new word with code-field in ram
 2042:   executed-ghost @
 2043:   create-forward-warn
 2044:   IF ['] reswarn-forward IS resolve-warning THEN
 2045:   \ make Alias
 2046:   (THeader there 0 T a, H alias-mask flag! ( S executed-ghost new-ghost )
 2047:   \ store  poiter to code-field
 2048:   switchram T cfalign H
 2049:   there swap T ! H
 2050:   there tlastcfa ! 
 2051:   dup there resolve 0 ;Resolve !
 2052:   >r dup gdoes,
 2053: \ stores execution semantic in the built word
 2054: \ if the word already has a semantic (concerns S", IS, .", DOES>)
 2055: \ then keep it
 2056:   >end @ >exec @ r> >exec dup @ ['] NoExec =
 2057:   IF ! ELSE 2drop THEN ;
 2058: 
 2059: : Build:  ( -- [xt] [colon-sys] )
 2060:   :noname postpone TCreate ;
 2061: 
 2062: : BuildSmart:  ( -- [xt] [colon-sys] )
 2063:   :noname
 2064:   [ T has? rom H [IF] ]
 2065:   postpone RTCreate
 2066:   [ [ELSE] ]
 2067:   postpone TCreate 
 2068:   [ [THEN] ] ;
 2069: 
 2070: : g>body ( ghost -- body )
 2071:     >link @ T >body H ;
 2072: : gdoes>  ( ghost -- addr flag )
 2073:   executed-ghost @
 2074:   state @ IF  gexecute true EXIT  THEN
 2075:   g>body false ;
 2076: 
 2077: \ DO: ;DO                                               11may93jaw
 2078: \ changed to ?EXIT                                      10may93jaw
 2079: 
 2080: : DO:     ( -- ghost [xt] [colon-sys] )
 2081:   here ghostheader
 2082:   :noname postpone gdoes> postpone ?EXIT ;
 2083: 
 2084: : by:     ( -- ghost [xt] [colon-sys] ) \ name
 2085:   ghost
 2086:   :noname postpone gdoes> postpone ?EXIT ;
 2087: 
 2088: : ;DO ( ghost [xt] [colon-sys] -- ghost )
 2089:   postpone ;    ( S addr xt )
 2090:   over >exec ! ; immediate
 2091: 
 2092: : compile: ( ghost -- ghost [xt] [colon-sys] )
 2093:     :noname  postpone g>body ;
 2094: : ;compile ( ghost [xt] [colon-sys] -- ghost )
 2095:     postpone ;  over >comp ! ;
 2096: 
 2097: : by      ( -- ghost ) \ Name
 2098:   ghost >end @ ;
 2099: 
 2100: >TARGET
 2101: \ Variables and Constants                              05dec92py
 2102: 
 2103: Build:  ( n -- ) ;
 2104: by: :docon ( ghost -- n ) T @ H ;DO
 2105: \ compile: alit, compile @ ;compile
 2106: Builder (Constant)
 2107: 
 2108: Build:  ( n -- ) T , H ;
 2109: by (Constant)
 2110: Builder Constant
 2111: 
 2112: Build:  ( n -- ) T A, H ;
 2113: by (Constant)
 2114: Builder AConstant
 2115: 
 2116: Build:  ( d -- ) T , , H ;
 2117: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
 2118: Builder 2Constant
 2119: 
 2120: BuildSmart: ;
 2121: by: :dovar ( ghost -- addr ) ;DO
 2122: \ compile: alit, ;compile
 2123: Builder Create
 2124: 
 2125: T has? rom H [IF]
 2126: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 2127: by (Constant)
 2128: Builder Variable
 2129: [ELSE]
 2130: Build: T 0 , H ;
 2131: by Create
 2132: Builder Variable
 2133: [THEN]
 2134: 
 2135: T has? rom H [IF]
 2136: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
 2137: by (Constant)
 2138: Builder 2Variable
 2139: [ELSE]
 2140: Build: T 0 , 0 , H ;
 2141: by Create
 2142: Builder 2Variable
 2143: [THEN]
 2144: 
 2145: T has? rom H [IF]
 2146: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
 2147: by (Constant)
 2148: Builder AVariable
 2149: [ELSE]
 2150: Build: T 0 A, H ;
 2151: by Create
 2152: Builder AVariable
 2153: [THEN]
 2154: 
 2155: \ User variables                                       04may94py
 2156: 
 2157: >CROSS
 2158: 
 2159: Variable tup  0 tup !
 2160: Variable tudp 0 tudp !
 2161: 
 2162: : u,  ( n -- udp )
 2163:   tup @ tudp @ + T  ! H
 2164:   tudp @ dup T cell+ H tudp ! ;
 2165: 
 2166: : au, ( n -- udp )
 2167:   tup @ tudp @ + T A! H
 2168:   tudp @ dup T cell+ H tudp ! ;
 2169: 
 2170: >TARGET
 2171: 
 2172: Build: 0 u, X , ;
 2173: by: :douser ( ghost -- up-addr )  X @ tup @ + ;DO
 2174: \ compile: compile useraddr @ , ;compile
 2175: Builder User
 2176: 
 2177: Build: 0 u, X , 0 u, drop ;
 2178: by User
 2179: Builder 2User
 2180: 
 2181: Build: 0 au, X , ;
 2182: by User
 2183: Builder AUser
 2184: 
 2185: BuildSmart: T , H ;
 2186: by (Constant)
 2187: Builder Value
 2188: 
 2189: BuildSmart: T A, H ;
 2190: by (Constant)
 2191: Builder AValue
 2192: 
 2193: BuildSmart:  ( -- ) [T'] noop T A, H ;
 2194: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2195: \ compile: alit, compile @ compile execute ;compile
 2196: Builder Defer
 2197: 
 2198: Build: ( inter comp -- ) swap T immediate A, A, H ;
 2199: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
 2200: Builder interpret/compile:
 2201: 
 2202: \ Sturctures                                           23feb95py
 2203: 
 2204: >CROSS
 2205: : nalign ( addr1 n -- addr2 )
 2206: \ addr2 is the aligned version of addr1 wrt the alignment size n
 2207:  1- tuck +  swap invert and ;
 2208: >TARGET
 2209: 
 2210: Build: ;
 2211: by: :dofield T @ H + ;DO
 2212: \ compile: T @ H lit, compile + ;compile
 2213: Builder (Field)
 2214: 
 2215: Build: ( align1 offset1 align size "name" --  align2 offset2 )
 2216:     rot dup T , H ( align1 align size offset1 )
 2217:     + >r nalign r> ;
 2218: by (Field)
 2219: Builder Field
 2220: 
 2221: : struct  T 1 chars 0 H ;
 2222: : end-struct  T 2Constant H ;
 2223: 
 2224: : cell% ( n -- size align )
 2225:     T 1 cells H dup ;
 2226: 
 2227: Build: ( m v -- m' v )  dup T , cell+ H ;
 2228: DO:  abort" Not in cross mode" ;DO
 2229: Builder input-method
 2230: 
 2231: Build: ( m v size -- m v' )  over T , H + ;
 2232: DO:  abort" Not in cross mode" ;DO
 2233: Builder input-var
 2234: 
 2235: \ structural conditionals                              17dec92py
 2236: 
 2237: >CROSS
 2238: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
 2239: : sys?        ( sys -- sys )    dup 0= ?struc ;
 2240: : >mark       ( -- sys )        T here  ( dup ." M" hex. ) 0 , H ;
 2241: 
 2242: : branchoffset ( src dest -- )  - tchar / ; \ ?? jaw
 2243: 
 2244: : >resolve    ( sys -- )        
 2245: 	X here ( dup ." >" hex. ) over branchoffset swap X ! ;
 2246: 
 2247: : <resolve    ( sys -- )
 2248: 	X here ( dup ." <" hex. ) branchoffset X , ;
 2249: 
 2250: :noname compile branch X here branchoffset X , ;
 2251:   IS branch, ( target-addr -- )
 2252: :noname compile ?branch X here branchoffset X , ;
 2253:   IS ?branch, ( target-addr -- )
 2254: :noname compile branch T here 0 , H ;
 2255:   IS branchmark, ( -- branchtoken )
 2256: :noname compile ?branch T here 0 , H ;
 2257:   IS ?branchmark, ( -- branchtoken )
 2258: :noname T here 0 , H ;
 2259:   IS ?domark, ( -- branchtoken )
 2260: :noname dup X @ ?struc X here over branchoffset swap X ! ;
 2261:   IS branchtoresolve, ( branchtoken -- )
 2262: :noname branchto, X here ;
 2263:   IS branchtomark, ( -- target-addr )
 2264: 
 2265: >TARGET
 2266: 
 2267: \ Structural Conditionals                              12dec92py
 2268: 
 2269: Cond: BUT       restrict? sys? swap ;Cond
 2270: Cond: YET       restrict? sys? dup ;Cond
 2271: 
 2272: >CROSS
 2273: 
 2274: Variable tleavings 0 tleavings !
 2275: 
 2276: : (done) ( addr -- )
 2277:     tleavings @
 2278:     BEGIN  dup
 2279:     WHILE
 2280: 	>r dup r@ cell+ @ \ address of branch
 2281: 	u> 0=	   \ lower than DO?	
 2282:     WHILE
 2283: 	r@ 2 cells + @ \ branch token
 2284: 	branchtoresolve,
 2285: 	r@ @ r> free throw
 2286:     REPEAT  r>  THEN
 2287:     tleavings ! drop ;
 2288: 
 2289: >TARGET
 2290: 
 2291: Cond: DONE   ( addr -- )  restrict? (done) ;Cond
 2292: 
 2293: >CROSS
 2294: : (leave) ( branchtoken -- )
 2295:     3 cells allocate throw >r
 2296:     T here H r@ cell+ !
 2297:     r@ 2 cells + !
 2298:     tleavings @ r@ !
 2299:     r> tleavings ! ;
 2300: >TARGET
 2301: 
 2302: Cond: LEAVE     restrict? branchmark, (leave) ;Cond
 2303: Cond: ?LEAVE    restrict? compile 0=  ?branchmark, (leave)  ;Cond
 2304: 
 2305: >CROSS
 2306: \ !!JW ToDo : Move to general tools section
 2307: 
 2308: : to1 ( x1 x2 xn n -- addr )
 2309: \G packs n stack elements in a allocated memory region
 2310:    dup dup 1+ cells allocate throw dup >r swap 1+
 2311:    0 DO tuck ! cell+ LOOP
 2312:    drop r> ;
 2313: : 1to ( addr -- x1 x2 xn )
 2314: \G unpacks the elements saved by to1
 2315:     dup @ swap over cells + swap
 2316:     0 DO  dup @ swap 1 cells -  LOOP
 2317:     free throw ;
 2318: 
 2319: : loop]     branchto, dup <resolve tcell - (done) ;
 2320: 
 2321: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
 2322: 
 2323: >TARGET
 2324: 
 2325: \ Structural Conditionals                              12dec92py
 2326: 
 2327: >TARGET
 2328: Cond: AHEAD     restrict? branchmark, ;Cond
 2329: Cond: IF        restrict? ?branchmark, ;Cond
 2330: Cond: THEN      restrict? sys? branchto, branchtoresolve, ;Cond
 2331: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
 2332: 
 2333: Cond: BEGIN     restrict? branchtomark, ;Cond
 2334: Cond: WHILE     restrict? sys? compile IF swap ;Cond
 2335: Cond: AGAIN     restrict? sys? branch, ;Cond
 2336: Cond: UNTIL     restrict? sys? ?branch, ;Cond
 2337: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
 2338: 
 2339: Cond: CASE      restrict? 0 ;Cond
 2340: Cond: OF        restrict? 1+ >r compile over compile =
 2341:                 compile IF compile drop r> ;Cond
 2342: Cond: ENDOF     restrict? >r compile ELSE r> ;Cond
 2343: Cond: ENDCASE   restrict? compile drop 0 ?DO  compile THEN  LOOP ;Cond
 2344: 
 2345: \ Structural Conditionals                              12dec92py
 2346: 
 2347: :noname \ ?? i think 0 is too much! jaw
 2348:     0 compile (do)
 2349:     branchtomark,  2 to1 ;
 2350:   IS do, ( -- target-addr )
 2351: 
 2352: \ :noname
 2353: \     compile 2dup compile = compile IF
 2354: \     compile 2drop compile ELSE
 2355: \     compile (do) branchtomark, 2 to1 ;
 2356: \   IS ?do,
 2357:     
 2358: :noname
 2359:     0 compile (?do)  ?domark, (leave)
 2360:     branchtomark,  2 to1 ;
 2361:   IS ?do, ( -- target-addr )
 2362: :noname compile (for) branchtomark, ;
 2363:   IS for, ( -- target-addr )
 2364: :noname 1to compile (loop)  loop] compile unloop skiploop] ;
 2365:   IS loop, ( target-addr -- )
 2366: :noname 1to compile (+loop)  loop] compile unloop skiploop] ;
 2367:   IS +loop, ( target-addr -- )
 2368: :noname compile (next)  loop] compile unloop ;
 2369:   IS next, ( target-addr -- )
 2370: 
 2371: Cond: DO      	restrict? do, ;Cond
 2372: Cond: ?DO     	restrict? ?do, ;Cond
 2373: Cond: FOR	restrict? for, ;Cond
 2374: 
 2375: Cond: LOOP	restrict? sys? loop, ;Cond
 2376: Cond: +LOOP	restrict? sys? +loop, ;Cond
 2377: Cond: NEXT	restrict? sys? next, ;Cond
 2378: 
 2379: \ String words                                         23feb93py
 2380: 
 2381: : ,"            [char] " parse T string, align H ;
 2382: 
 2383: Cond: ."        restrict? compile (.")     T ," H ;Cond
 2384: Cond: S"        restrict? compile (S")     T ," H ;Cond
 2385: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
 2386: 
 2387: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
 2388: : IS            T >address ' >body ! H ;
 2389: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
 2390: : TO            T ' >body ! H ;
 2391: 
 2392: Cond: defers	T ' >body @ compile, H ;Cond
 2393: : on		T -1 swap ! H ; 
 2394: : off   	T 0 swap ! H ;
 2395: 
 2396: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
 2397: 
 2398: \ linked list primitive
 2399: : linked        X here over X @ X A, swap X ! ;
 2400: : chained	T linked A, H ;
 2401: 
 2402: : err"   s" ErrLink linked" evaluate T , H
 2403:          [char] " parse T string, align H ;
 2404: 
 2405: : env"  [char] " parse s" EnvLink linked" evaluate
 2406:         T string, align , H ;
 2407: 
 2408: : 2env" [char] " parse s" EnvLink linked" evaluate
 2409:         here >r T string, align , , H
 2410:         r> dup T c@ H 80 and swap T c! H ;
 2411: 
 2412: \ compile must be last                                 22feb93py
 2413: 
 2414: Cond: compile ( -- ) restrict? \ name
 2415:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2416:       0> IF    gexecute
 2417:          ELSE  dup >magic @ <imm> =
 2418:                IF   gexecute
 2419:                ELSE compile (compile) addr, THEN THEN ;Cond
 2420: 
 2421: Cond: postpone ( -- ) restrict? \ name
 2422:       bl word gfind dup 0= ABORT" CROSS: Can't compile"
 2423:       0> IF    gexecute
 2424:          ELSE  dup >magic @ <imm> =
 2425:                IF   gexecute
 2426: 	       ELSE compile (compile) addr, THEN THEN ;Cond
 2427: 	   
 2428: \ save-cross                                           17mar93py
 2429: 
 2430: hex
 2431: 
 2432: >CROSS
 2433: Create magic  s" Gforth2x" here over allot swap move
 2434: 
 2435: bigendian 1+ \ strangely, in magic big=0, little=1
 2436: tcell 1 = 0 and or
 2437: tcell 2 = 2 and or
 2438: tcell 4 = 4 and or
 2439: tcell 8 = 6 and or
 2440: tchar 1 = 00 and or
 2441: tchar 2 = 28 and or
 2442: tchar 4 = 50 and or
 2443: tchar 8 = 78 and or
 2444: magic 7 + c!
 2445: 
 2446: : save-cross ( "image-name" "binary-name" -- )
 2447:   bl parse ." Saving to " 2dup type cr
 2448:   w/o bin create-file throw >r
 2449:   TNIL IF
 2450:       s" #! "           r@ write-file throw
 2451:       bl parse          r@ write-file throw
 2452:       s"  --image-file" r@ write-file throw
 2453:       #lf       r@ emit-file throw
 2454:       r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
 2455:       ?do
 2456: 	  bl over emit-file throw
 2457:       loop
 2458:       drop
 2459:       magic 8       r@ write-file throw \ write magic
 2460:   ELSE
 2461:       bl parse 2drop
 2462:   THEN
 2463:   image @ there 
 2464:   r@ write-file throw \ write image
 2465:   TNIL IF
 2466:       bit$  @ there 1- tcell>bit rshift 1+
 2467:                 r@ write-file throw \ write tags
 2468:   THEN
 2469:   r> close-file throw ;
 2470: 
 2471: : save-region ( addr len -- )
 2472:   bl parse w/o bin create-file throw >r
 2473:   swap >image swap r@ write-file throw
 2474:   r> close-file throw ;
 2475: 
 2476: \ \ minimal definitions
 2477: 	   
 2478: >MINIMAL also minimal
 2479: 
 2480: \ Usefull words                                        13feb93py
 2481: 
 2482: : KB  400 * ;
 2483: 
 2484: \ \ [IF] [ELSE] [THEN] ...				14sep97jaw
 2485: 
 2486: \ it is useful to define our own structures and not to rely
 2487: \ on the words in the compiler
 2488: \ The words in the compiler might be defined with vocabularies
 2489: \ this doesn't work with our self-made compile-loop
 2490: 
 2491: Create parsed 20 chars allot	\ store word we parsed
 2492: 
 2493: : upcase
 2494:     parsed count bounds
 2495:     ?DO I c@ toupper I c! LOOP ;
 2496: 
 2497: : [ELSE]
 2498:     1 BEGIN
 2499: 	BEGIN bl word count dup WHILE
 2500: 	    comment? 20 umin parsed place upcase parsed count
 2501: 	    2dup s" [IF]" compare 0= >r 
 2502: 	    2dup s" [IFUNDEF]" compare 0= >r
 2503: 	    2dup s" [IFDEF]" compare 0= r> or r> or
 2504: 	    IF   2drop 1+
 2505: 	    ELSE 2dup s" [ELSE]" compare 0=
 2506: 		IF   2drop 1- dup
 2507: 		    IF 1+
 2508: 		    THEN
 2509: 		ELSE
 2510: 		    2dup s" [ENDIF]" compare 0= >r
 2511: 		    s" [THEN]" compare 0= r> or
 2512: 		    IF 1- THEN
 2513: 		THEN
 2514: 	    THEN
 2515: 	    ?dup 0= ?EXIT
 2516: 	REPEAT
 2517: 	2drop refill 0=
 2518:     UNTIL drop ; immediate
 2519:   
 2520: : [THEN] ( -- ) ; immediate
 2521: 
 2522: : [ENDIF] ( -- ) ; immediate
 2523: 
 2524: : [IF] ( flag -- )
 2525:     0= IF postpone [ELSE] THEN ; immediate 
 2526: 
 2527: Cond: [IF]      postpone [IF] ;Cond
 2528: Cond: [THEN]    postpone [THEN] ;Cond
 2529: Cond: [ELSE]    postpone [ELSE] ;Cond
 2530: 
 2531: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
 2532: 
 2533: : defined? tdefined? ;
 2534: : needed? needed? ;
 2535: : doer? doer? ;
 2536: 
 2537: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
 2538: 
 2539: : directive? 
 2540:   bl word count [ ' target >wordlist ] literal search-wordlist 
 2541:   dup IF nip THEN ;
 2542: 
 2543: : [IFDEF]  >in @ directive? swap >in !
 2544: 	   0= IF tdefined? ELSE name 2drop true THEN
 2545: 	   postpone [IF] ;
 2546: 
 2547: : [IFUNDEF] tdefined? 0= postpone [IF] ;
 2548: 
 2549: Cond: [IFDEF]   postpone [IFDEF] ;Cond
 2550: 
 2551: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
 2552: 
 2553: \ C: \- \+ Conditional Compiling                         09jun93jaw
 2554: 
 2555: : C: >in @ tdefined? 0=
 2556:      IF    >in ! X :
 2557:      ELSE drop
 2558:         BEGIN bl word dup c@
 2559:               IF   count comment? s" ;" compare 0= ?EXIT
 2560:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
 2561:               THEN
 2562:         AGAIN
 2563:      THEN ;
 2564: 
 2565: : d? d? ;
 2566: 
 2567: \G doesn't skip line when debug switch is on
 2568: : \D D? 0= IF postpone \ THEN ;
 2569: 
 2570: \G interprets the line if word is not defined
 2571: : \- tdefined? IF postpone \ THEN ;
 2572: 
 2573: \G interprets the line if word is defined
 2574: : \+ tdefined? 0= IF postpone \ THEN ;
 2575: 
 2576: Cond: \- \- ;Cond
 2577: Cond: \+ \+ ;Cond
 2578: Cond: \D \D ;Cond
 2579: 
 2580: : ?? bl word find IF execute ELSE drop 0 THEN ;
 2581: 
 2582: : needed:
 2583: \G defines ghost for words that we want to be compiled
 2584:   BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
 2585: 
 2586: \ words that should be in minimal
 2587: 
 2588: create s-buffer 50 chars allot
 2589: 
 2590: bigendian Constant bigendian
 2591: 
 2592: : here there ;
 2593: : equ constant ;
 2594: : mark there constant ;
 2595: 
 2596: \ compiler directives
 2597: : >ram >ram ;
 2598: : >rom >rom ;
 2599: : >auto >auto ;
 2600: : >tempdp >tempdp ;
 2601: : tempdp> tempdp> ;
 2602: : const constflag on ;
 2603: : warnings name 3 = 0= twarnings ! drop ;
 2604: : | ;
 2605: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
 2606: 
 2607: : save-cross save-cross ;
 2608: : save-region save-region ;
 2609: : tdump swap >image swap dump ;
 2610: 
 2611: also forth 
 2612: [IFDEF] Label           : Label defempty? Label ; [THEN] 
 2613: [IFDEF] start-macros    : start-macros defempty? start-macros ; [THEN]
 2614: \ [IFDEF] builttag	: builttag builttag ;	[THEN]
 2615: previous
 2616: 
 2617: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
 2618: : + + ;
 2619: : 1+ 1 + ;
 2620: : 2+ 2 + ;
 2621: : 1- 1- ;
 2622: : - - ;
 2623: : and and ;
 2624: : or or ;
 2625: : 2* 2* ;
 2626: : * * ;
 2627: : / / ;
 2628: : dup dup ;
 2629: : over over ;
 2630: : swap swap ;
 2631: : rot rot ;
 2632: : drop drop ;
 2633: : =   = ;
 2634: : 0=   0= ;
 2635: : lshift lshift ;
 2636: : 2/ 2/ ;
 2637: : . . ;
 2638: 
 2639: : all-words    ['] forced?    IS skip? ;
 2640: : needed-words ['] needed?  IS skip? ;
 2641: : undef-words  ['] defined2? IS skip? ;
 2642: : skipdef skipdef ;
 2643: 
 2644: : \  postpone \ ;  immediate
 2645: : \G T-\G ; immediate
 2646: : (  postpone ( ;  immediate
 2647: : include bl word count included ;
 2648: : require require ;
 2649: : .( [char] ) parse type ;
 2650: : ." [char] " parse type ;
 2651: : cr cr ;
 2652: 
 2653: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
 2654: 
 2655: \ only forth also cross also minimal definitions order
 2656: 
 2657: \ cross-compiler words
 2658: 
 2659: : decimal       decimal ;
 2660: : hex           hex ;
 2661: 
 2662: \ : tudp          X tudp ;
 2663: \ : tup           X tup ;
 2664: 
 2665: : doc-off       false to-doc ! ;
 2666: : doc-on        true  to-doc ! ;
 2667: 
 2668: [IFDEF] dbg : dbg dbg ; [THEN]
 2669: 
 2670: \ for debugging...
 2671: : order         order ;
 2672: : hwords         words ;
 2673: : words 	also ghosts words previous ;
 2674: : .s            .s ;
 2675: : bye           bye ;
 2676: 
 2677: \ turnkey direction
 2678: : H forth ; immediate
 2679: : T minimal ; immediate
 2680: : G ghosts ; immediate
 2681: 
 2682: : turnkey 
 2683:    \GFORTH 0 set-order also ghosts
 2684:    \ANSI [ ' ghosts >wordlist ] Literal 1 set-order
 2685:    also target definitions
 2686:    also Minimal also ;
 2687: 
 2688: \ these ones are pefered:
 2689: 
 2690: : lock   turnkey ;
 2691: : unlock previous forth also cross ;
 2692: 
 2693: \ also minimal
 2694: : [[ also unlock ;
 2695: : ]] previous previous also also ;
 2696: 
 2697: unlock definitions also minimal
 2698: : lock   lock ;
 2699: lock
 2700: 
 2701: \ load cross compiler extension defined in mach file
 2702: 
 2703: UNLOCK >CROSS
 2704: 
 2705: [IFDEF] extend-cross extend-cross [THEN]
 2706: 
 2707: LOCK

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