Annotation of gforth/comp-i.fs, revision 1.12
1.1 anton 1: \ Compare nonrelocatable images and produce a relocatable image
2:
1.12 ! anton 3: \ Copyright (C) 1996,1997,1998,2002 Free Software Foundation, Inc.
1.1 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.5 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 20:
21: s" address-unit-bits" environment? drop constant bits/au
22: 6 constant dodoes-tag
23:
24: : write-cell { w^ w file-id -- ior }
25: \ write a cell to the file
26: w cell file-id write-file ;
27:
28: : th ( addr1 n -- addr2 )
29: cells + ;
30:
1.7 anton 31: : bset ( bmask c-addr -- )
32: tuck c@ or swap c! ;
33:
1.1 anton 34: : set-bit { u addr -- }
35: \ set bit u in bit-vector addr
36: u bits/au /mod
37: >r 1 bits/au 1- rot - lshift
1.7 anton 38: r> addr + bset ;
1.1 anton 39:
1.9 anton 40: : image-data { image1 image2 i-field expected-offset -- base offset }
41: image1 i-field + @ image2 i-field + @ over - { base offset }
42: offset 0=
43: if
44: ." : images have the same base address; producing only a data-relocatable image" cr
45: else
46: offset abs expected-offset <> abort" images produced by different engines"
47: ." offset=" offset . cr
48: 0 image1 i-field + ! 0 image2 i-field + !
49: endif
50: base offset ;
1.11 pazsan 51:
52: Create tag-offsets
53: include kernel/groups.fs
54: tag-offsets $20 cells + here tuck - dup allot erase
55:
56: : >tag ( index -- tag )
57: dup dodoes-tag 2 + > IF dodoes-tag 2 + -
58: $21 1 DO dup tag-offsets I cells + @ < IF
59: tag-offsets I 1- cells + @ - I 9 lshift + negate
60: UNLOOP EXIT THEN LOOP
61: dodoes-tag 2 + +
62: THEN -2 swap - ;
1.9 anton 63:
1.1 anton 64: : compare-images { image1 image2 reloc-bits size file-id -- }
65: \G compares image1 and image2 (of size cells) and sets reloc-bits.
66: \G offset is the difference for relocated addresses
67: \ this definition is certainly to long and too complex, but is
68: \ hard to factor.
69: image1 @ image2 @ over - { dbase doffset }
70: doffset 0= abort" images have the same dictionary base address"
71: ." data offset=" doffset . cr
1.9 anton 72: ." code" image1 image2 cell 26 cells image-data { cbase coffset }
73: ." xt" image1 image2 11 cells 22 cells image-data { xbase xoffset }
1.1 anton 74: size 0
75: u+do
76: image1 i th @ image2 i th @ { cell1 cell2 }
77: cell1 doffset + cell2 =
78: if
79: cell1 dbase - file-id write-cell throw
80: i reloc-bits set-bit
81: else
82: coffset 0<> cell1 coffset + cell2 = and
83: if
84: cell1 cbase - cell / { tag }
85: tag dodoes-tag =
86: if
87: \ make sure that the next cell will not be tagged
1.9 anton 88: \ !! can probably be optimized away with hybrid threading
1.1 anton 89: dbase negate image1 i 1+ th +!
90: dbase doffset + negate image2 i 1+ th +!
91: endif
1.11 pazsan 92: tag >tag $4000 xor file-id write-cell throw
1.1 anton 93: i reloc-bits set-bit
94: else
1.9 anton 95: xoffset 0<> cell1 xoffset + cell2 = and
1.1 anton 96: if
1.9 anton 97: cell1 xbase - cell / { tag }
98: tag dodoes-tag =
99: if
100: \ make sure that the next cell will not be tagged
101: \ !! can probably be optimized away with hybrid threading
102: dbase negate image1 i 1+ th +!
103: dbase doffset + negate image2 i 1+ th +!
104: endif
1.11 pazsan 105: tag >tag file-id write-cell throw
1.9 anton 106: i reloc-bits set-bit
107: else
108: cell1 file-id write-cell throw
109: cell1 cell2 <>
110: if
111: 0 i th 9 u.r cell1 17 u.r cell2 17 u.r cr
112: endif
1.1 anton 113: endif
114: endif
115: endif
116: loop ;
117:
118: : comp-image ( "image-file1" "image-file2" "new-image" -- )
119: name slurp-file { image1 size1 }
1.10 anton 120: image1 size1 s" Gforth3" search 0= abort" not a Gforth image"
1.1 anton 121: drop 8 + image1 - { header-offset }
122: size1 aligned size1 <> abort" unaligned image size"
1.8 anton 123: image1 header-offset + 2 cells + @ header-offset + size1 <> abort" header gives wrong size"
1.1 anton 124: name slurp-file { image2 size2 }
125: size1 size2 <> abort" image sizes differ"
126: name ( "new-image" ) w/o bin create-file throw { outfile }
127: size1 header-offset - 1- cell / bits/au / 1+ { reloc-size }
128: reloc-size allocate throw { reloc-bits }
129: reloc-bits reloc-size erase
130: image1 header-offset outfile write-file throw
131: base @ hex
132: image1 header-offset + image2 header-offset + reloc-bits
133: size1 header-offset - aligned cell / outfile compare-images
134: base !
135: reloc-bits reloc-size outfile write-file throw
136: outfile close-file throw ;
137:
138:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>