File:  [gforth] / gforth / fi2c.fs
Revision 1.18: download - view: text, annotated - select for diffs
Sun Mar 25 21:30:59 2007 UTC (12 years, 3 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
C-based Gforth EC starts to work

    1: \ Convert image to C include file
    2: 
    3: \ Copyright (C) 1998,1999,2002,2003 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: require string.fs
   22: 
   23: 0 Value image
   24: 0 Value bitmap
   25: 
   26: Create magicbuf 8 allot
   27: 
   28: Create groups 32 0 [DO] 512 cells allocate throw dup 512 cells erase , [LOOP]
   29: 
   30: \ we define it ans like...
   31: wordlist Constant target-environment
   32: 
   33: \ save information of current dictionary to restore with environ>
   34: Variable env-current 
   35: 
   36: : >ENVIRON get-current env-current ! target-environment set-current
   37:   also target-environment context ! ;
   38: : ENVIRON> previous env-current @ set-current ; 
   39: 
   40: : t-env? ( addr len -- [ x ] true | false )
   41: \G returns the content of environment variable and true or
   42: \G false if not present
   43:    target-environment search-wordlist 
   44:    IF EXECUTE true ELSE false THEN ;
   45: 
   46: : $has? ( addr u -- x | false )
   47: \G returns the content of environment variable 
   48: \G or false if not present
   49:    t-env? dup IF drop THEN ;
   50: 
   51: ' Value Alias DefaultValue
   52: ' Value Alias SetValue
   53: 
   54: : kb 1024 * ;
   55: 
   56: ' noop alias T
   57: ' noop alias H
   58: 
   59: : has?  parse-name 2drop true ;
   60: 
   61: include machpc.fs
   62: ENVIRON>
   63: 
   64: : prefix? ( string u1 prefix u2 -- flag )
   65:     tuck 2>r umin 2r> str= ;
   66: 
   67: s" NULL" groups @ cell+ $!
   68: 
   69: : scan-ifs ( fd -- ) >r 1
   70:     BEGIN  pad $100 r@ read-line throw  WHILE
   71: 	    pad swap
   72: 	    2dup s" #ifdef HAS_" prefix? >r
   73: 	    2dup s" #else" prefix? >r
   74: 	    s" #endif" prefix? r> or r> - +
   75: 	dup 0= UNTIL  THEN  rdrop drop ;
   76: 		
   77: : read-groups ( addr u -- )
   78:     r/o open-file throw >r  0 2
   79:     BEGIN  pad $100 r@ read-line throw  WHILE
   80: 	    pad swap
   81: 	    2dup s" #ifdef HAS_" prefix?
   82: 	    IF
   83: 		11 /string $has? 0= IF r@ scan-ifs  THEN
   84: 	    ELSE  2dup s" #else" prefix?
   85: 		IF  r@ scan-ifs
   86: 		ELSE  2dup s" GROUP(" prefix?
   87: 		    IF  2drop drop 1+ 0  ELSE
   88: 			2dup s" INST_ADDR(" prefix?
   89: 			IF  &10 /string 2dup ') scan nip -
   90: 			    2over cells swap cells groups + @ + $!
   91: 			    1+
   92: 			ELSE  2drop  THEN  THEN  THEN  THEN
   93:     REPEAT
   94:     2drop r> close-file throw ;
   95: s" prim_lab.i" read-groups
   96: 
   97: Variable bswap?
   98: Variable endian
   99: Variable tchars
  100: Variable tcell
  101: Variable au
  102: 
  103: : t@ ( addr -- x )
  104:     endian @ IF
  105: 	0 swap tcell @ bounds ?DO
  106: 	    8 lshift I c@ or
  107: 	LOOP
  108:     ELSE
  109: 	0 swap tcell @ bounds ?DO
  110: 	    8 rshift I c@ 8 tcell @ 1- * lshift or
  111: 	LOOP
  112:     THEN
  113:     dup 1 8 tcell @ * 1- lshift and negate or ;
  114: 
  115: : search-magic ( fd -- )  >r
  116:     BEGIN  magicbuf 8 r@ read-file throw  8 =  WHILE
  117: 	magicbuf s" Gforth3" tuck str=  UNTIL
  118:     ELSE  true abort" No magic found"  THEN
  119:     1 magicbuf 7 + c@ 5 rshift 3 and lshift tchars !
  120:     1 magicbuf 7 + c@ 1 rshift 3 and lshift tcell !
  121:     1 magicbuf 7 + c@ 3 rshift 3 and lshift au !
  122:     magicbuf 7 + c@ 1 and 0= dup endian !
  123:     [ pad off 1 pad ! pad c@ 1 = ] Literal = bswap? !
  124:     ." /* Image with " tcell @ . ." bytes cell, " tchars @ . ." bytes per char and " au @ . ." bytes per address unit */" cr
  125:     rdrop ;
  126: 
  127: Create image-header  4 cells allot
  128: Variable image-cells
  129: Variable bitmap-chars
  130: 
  131: : read-header ( fd -- )
  132:     image-header 4 tcell @ * rot read-file throw drop
  133:     image-header 2 tcell @ * + t@ tchars @ * tcell @ / au @ /
  134:     dup image-cells ! 1- 8 / tchars @ / 1+ bitmap-chars !
  135:     image-cells @ tcell @ * allocate throw to image
  136:     bitmap-chars @ allocate throw to bitmap ;
  137: 
  138: : read-dictionary ( fd -- )  >r
  139:     image image-cells @ tcell @ * r> read-file throw drop ;
  140: 
  141: : read-bitmap ( fd -- )  >r
  142:     bitmap bitmap-chars @ tchars @ * r> read-file throw drop ;
  143: 
  144: : .08x ( n -- ) 0 <# tcell  @ 0 ?DO # # LOOP 'x hold '0 hold #> type ;
  145: : .02x ( n -- ) 0 <# tchars @ 0 ?DO # # LOOP 'x hold '0 hold #> type ;
  146: 
  147: : bit@ ( addr n -- flag )
  148:     dup 7 and $80 swap rshift >r 3 rshift + c@ r> and
  149:     0<> ;
  150: 
  151: : .image ( -- )
  152:     image-cells @ 0 ?DO
  153: 	I 1 + I' min I ?DO  space image I tcell @ * + t@
  154: 	    bitmap I bit@ IF
  155: 		dup 0< IF
  156: 		    dup -1 = IF
  157: 			drop ." NULL"
  158: 		    ELSE
  159: 			negate dup $3E00 and 9 rshift swap $1FF and
  160: 			over cells groups + @ over $1FF and cells +
  161: 			dup @ 0= IF  drop
  162: 			    2dup 0 8 d= IF  2drop s" doesjump"
  163: 			    ELSE
  164: 				<# '] hold 0 #S 2drop '[ hold '] hold
  165: 				0 #S '[ hold #>
  166: 			    THEN
  167: 			ELSE
  168: 			    >r 2drop r> $@
  169: 			THEN
  170: 			." INST_ADDR(" type ." )"
  171: 		    THEN
  172: 		ELSE
  173: 		    dup IF  ." ((void*)image)+"  THEN  .08x
  174: 		THEN
  175: 	    ELSE
  176: 		." (void*)" .08x
  177: 	    THEN  ." ,"
  178: 	LOOP cr
  179:     1 +LOOP ;
  180: 
  181: : .reloc ( -- )
  182:     bitmap-chars @ tchars @ * 0 ?DO
  183: 	I $10 + I' min I ?DO  space
  184: 	    0 I tchars @ bounds ?DO  8 lshift bitmap I + c@ +  LOOP
  185: 	    .02x ." ," tchars @ +LOOP cr
  186: 	$10 +LOOP ;
  187: 
  188: : read-image ( addr u -- )
  189:     r/o bin open-file throw >r
  190:     r@ search-magic
  191:     r@ file-position throw r@ read-header r@ reposition-file throw
  192:     r@ read-dictionary r@ read-bitmap r> close-file throw ;
  193: 
  194: : .imagesize ( -- )
  195:     image-header 3 tcell @ * + t@ tchars @ * tcell @ / au @ / .08x ;
  196: 
  197: : .relocsize ( -- )
  198:     bitmap-chars @ .08x ;
  199: 
  200: : fi2c ( addr u -- )  base @ >r hex
  201:     read-image
  202:     ." static void* image[" .imagesize ." ] = {" cr .image ." };" cr
  203:     ." #ifdef USE_RELOC" cr
  204:     ." const char reloc_bits[" .relocsize ." ] = {" cr .reloc ." };" cr
  205:     ." #endif" cr
  206:     r> base ! ;
  207: 

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