File:  [gforth] / gforth / cross.fs
Revision 1.124: download - view: text, annotated - select for diffs
Thu Mar 21 17:11:10 2002 UTC (22 years, 1 month ago) by jwilke
Branches: MAIN
CVS tags: HEAD
added optional write protection for memory regions

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

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