File:  [gforth] / gforth / cross.fs
Revision 1.105: download - view: text, annotated - select for diffs
Wed Sep 5 09:42:38 2001 UTC (18 years, 3 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
merged in the sepearation from colon, and prim, through the >comp field in the ghost
still TODOs:
- peephole stuff
- unification of new >exec-compile and >comp fields

a running kernl32l.fi was created successfully with this cross.fs

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

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