Annotation of gforth/fi2c.fs, revision 1.16

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

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