File:  [gforth] / gforth / fi2c.fs
Revision 1.17: download - view: text, annotated - select for diffs
Sun Mar 18 22:35:52 2007 UTC (16 years, 6 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed some small problems further along to compile on NXT

    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: 
   53: : kb 1024 * ;
   54: 
   55: include machpc.fs
   56: ENVIRON>
   57: 
   58: : prefix? ( string u1 prefix u2 -- flag )
   59:     tuck 2>r umin 2r> str= ;
   60: 
   61: s" NULL" groups @ cell+ $!
   62: 
   63: : scan-ifs ( fd -- ) >r 1
   64:     BEGIN  pad $100 r@ read-line throw  WHILE
   65: 	    pad swap
   66: 	    2dup s" #ifdef HAS_" prefix? >r
   67: 	    2dup s" #else" prefix? >r
   68: 	    s" #endif" prefix? r> or r> - +
   69: 	dup 0= UNTIL  THEN  rdrop drop ;
   70: 		
   71: : read-groups ( addr u -- )
   72:     r/o open-file throw >r  0 2
   73:     BEGIN  pad $100 r@ read-line throw  WHILE
   74: 	    pad swap
   75: 	    2dup s" #ifdef HAS_" prefix?
   76: 	    IF
   77: 		11 /string $has? 0= IF r@ scan-ifs  THEN
   78: 	    ELSE  2dup s" #else" prefix?
   79: 		IF  r@ scan-ifs
   80: 		ELSE  2dup s" GROUP(" prefix?
   81: 		    IF  2drop drop 1+ 0  ELSE
   82: 			2dup s" INST_ADDR(" prefix?
   83: 			IF  &10 /string 2dup ') scan nip -
   84: 			    2over cells swap cells groups + @ + $!
   85: 			    1+
   86: 			ELSE  2drop  THEN  THEN  THEN  THEN
   87:     REPEAT
   88:     2drop r> close-file throw ;
   89: s" prim_lab.i" read-groups
   90: 
   91: Variable bswap?
   92: Variable endian
   93: Variable tchars
   94: Variable tcell
   95: Variable au
   96: 
   97: : t@ ( addr -- x )
   98:     endian @ IF
   99: 	0 swap tcell @ bounds ?DO
  100: 	    8 lshift I c@ or
  101: 	LOOP
  102:     ELSE
  103: 	0 swap tcell @ bounds ?DO
  104: 	    8 rshift I c@ 8 tcell @ 1- * lshift or
  105: 	LOOP
  106:     THEN
  107:     dup 1 8 tcell @ * 1- lshift and negate or ;
  108: 
  109: 1 cells 4 = [IF]
  110: : bswap ( n -- n' )  bswap? @ 0= ?EXIT  0
  111:     over 24 rshift $FF       and or
  112:     over  8 rshift $FF00     and or
  113:     over  8 lshift $FF0000   and or
  114:     over 24 lshift $FF000000 and or nip ;
  115: [THEN]
  116: 
  117: 1 cells 8 = [IF]
  118: : bswap ( n -- n' )  bswap? @ 0= ?EXIT  0
  119:     over 56 rshift $FF               and or
  120:     over 40 rshift $FF00             and or
  121:     over 24 rshift $FF0000           and or
  122:     over  8 rshift $FF000000         and or
  123:     over  8 lshift $FF00000000       and or
  124:     over 24 lshift $FF0000000000     and or
  125:     over 40 lshift $FF000000000000   and or
  126:     over 56 lshift $FF00000000000000 and or
  127:     nip ;
  128: [THEN]
  129: 
  130: : search-magic ( fd -- )  >r
  131:     BEGIN  magicbuf 8 r@ read-file throw  8 =  WHILE
  132: 	magicbuf s" Gforth3" tuck str=  UNTIL
  133:     ELSE  true abort" No magic found"  THEN
  134:     1 magicbuf 7 + c@ 5 rshift 3 and lshift tchars !
  135:     1 magicbuf 7 + c@ 1 rshift 3 and lshift tcell !
  136:     1 magicbuf 7 + c@ 3 rshift 3 and lshift au !
  137:     magicbuf 7 + c@ 1 and 0= dup endian !
  138:     [ pad off 1 pad ! pad c@ 1 = ] Literal = bswap? !
  139:     ." /* Image with " tcell @ . ." bytes cell, " tchars @ . ." bytes per char and " au @ . ." bytes per address unit */" cr
  140:     rdrop ;
  141: 
  142: Create image-header  4 cells allot
  143: Variable image-cells
  144: Variable bitmap-chars
  145: 
  146: : read-header ( fd -- )
  147:     image-header 4 tcell @ * rot read-file throw drop
  148:     image-header 2 tcell @ * + t@ tchars @ * tcell @ / au @ /
  149:     dup image-cells ! 1- 8 / tchars @ / 1+ bitmap-chars !
  150:     image-cells @ tcell @ * allocate throw to image
  151:     bitmap-chars @ allocate throw to bitmap ;
  152: 
  153: : read-dictionary ( fd -- )  >r
  154:     image image-cells @ tcell @ * r> read-file throw drop ;
  155: 
  156: : read-bitmap ( fd -- )  >r
  157:     bitmap bitmap-chars @ tchars @ * r> read-file throw drop ;
  158: 
  159: : .08x ( n -- ) 0 <# tcell  @ 0 ?DO # # LOOP 'x hold '0 hold #> type ;
  160: : .02x ( n -- ) 0 <# tchars @ 0 ?DO # # LOOP 'x hold '0 hold #> type ;
  161: 
  162: : bit@ ( addr n -- flag )
  163:     dup 7 and $80 swap rshift >r 3 rshift + c@ r> and
  164:     0<> ;
  165: 
  166: : .image ( -- )
  167:     image-cells @ 0 ?DO
  168: 	I 1 + I' min I ?DO  space image I tcell @ * + t@
  169: 	    bitmap I bit@ IF
  170: 		dup 0< IF
  171: 		    dup -1 = IF
  172: 			drop ." NULL"
  173: 		    ELSE
  174: 			negate dup $3E00 and 9 rshift swap $1FF and
  175: 			over cells groups + @ over $1FF and cells +
  176: 			dup @ 0= IF  drop
  177: 			    2dup 0 8 d= IF  2drop s" doesjump"
  178: 			    ELSE
  179: 				<# '] hold 0 #S 2drop '[ hold '] hold
  180: 				0 #S '[ hold #>
  181: 			    THEN
  182: 			ELSE
  183: 			    >r 2drop r> $@
  184: 			THEN
  185: 			." INST_ADDR(" type ." )"
  186: 		    THEN
  187: 		ELSE
  188: 		    dup IF  ." ((void*)image)+"  THEN  .08x
  189: 		THEN
  190: 	    ELSE
  191: 		." (void*)" .08x
  192: 	    THEN  ." ,"
  193: 	LOOP cr
  194:     1 +LOOP ;
  195: 
  196: : .reloc ( -- )
  197:     bitmap-chars @ tchars @ * 0 ?DO
  198: 	I $10 + I' min I ?DO  space
  199: 	    0 I tchars @ bounds ?DO  8 lshift bitmap I + c@ +  LOOP
  200: 	    .02x ." ," tchars @ +LOOP cr
  201: 	$10 +LOOP ;
  202: 
  203: : read-image ( addr u -- )
  204:     r/o bin open-file throw >r
  205:     r@ search-magic
  206:     r@ file-position throw r@ read-header r@ reposition-file throw
  207:     r@ read-dictionary r@ read-bitmap r> close-file throw ;
  208: 
  209: : .imagesize ( -- )
  210:     image-header 3 tcell @ * + t@ tchars @ * tcell @ / au @ / .08x ;
  211: 
  212: : .relocsize ( -- )
  213:     bitmap-chars @ .08x ;
  214: 
  215: : fi2c ( addr u -- )  base @ >r hex
  216:     read-image
  217:     ." static const void* image[" .imagesize ." ] = {" cr .image ." };" cr
  218:     ." #ifdef USE_RELOC" cr
  219:     ." const char reloc_bits[" .relocsize ." ] = {" cr .reloc ." };" cr
  220:     ." #endif" cr
  221:     r> base ! ;
  222: 

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