[gforth] / gforth / comp-i.fs  

gforth: gforth/comp-i.fs


1 : anton 1.1 \ Compare nonrelocatable images and produce a relocatable image
2 :    
3 : anton 1.18 \ Copyright (C) 1996,1997,1998,2002,2003,2004,2007 Free Software Foundation, Inc.
4 : anton 1.1
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 : anton 1.19 \ as published by the Free Software Foundation, either version 3
10 : anton 1.1 \ 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 : anton 1.19 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.1
20 :     s" address-unit-bits" environment? drop constant bits/au
21 : pazsan 1.17 7 constant dodoes-tag
22 : anton 1.1
23 :     : write-cell { w^ w file-id -- ior }
24 :     \ write a cell to the file
25 :     w cell file-id write-file ;
26 :    
27 :     : th ( addr1 n -- addr2 )
28 :     cells + ;
29 :    
30 : anton 1.7 : bset ( bmask c-addr -- )
31 :     tuck c@ or swap c! ;
32 :    
33 : anton 1.1 : set-bit { u addr -- }
34 :     \ set bit u in bit-vector addr
35 :     u bits/au /mod
36 :     >r 1 bits/au 1- rot - lshift
37 : anton 1.7 r> addr + bset ;
38 : anton 1.1
39 : anton 1.9 : image-data { image1 image2 i-field expected-offset -- base offset }
40 :     image1 i-field + @ image2 i-field + @ over - { base offset }
41 :     offset 0=
42 :     if
43 :     ." : images have the same base address; producing only a data-relocatable image" cr
44 :     else
45 : anton 1.15 \ the following sanity check produces false positices with exec-shield
46 :     \ offset abs expected-offset <> abort" images produced by different engines"
47 : anton 1.9 ." offset=" offset . cr
48 :     0 image1 i-field + ! 0 image2 i-field + !
49 :     endif
50 :     base offset ;
51 : pazsan 1.11
52 :     : >tag ( index -- tag )
53 : pazsan 1.13 dup dodoes-tag 2 + > IF
54 : pazsan 1.11 $21 1 DO dup tag-offsets I cells + @ < IF
55 : pazsan 1.13 tag-offsets I 1- cells + @ - I 1- 9 lshift + negate
56 : pazsan 1.11 UNLOOP EXIT THEN LOOP
57 :     THEN -2 swap - ;
58 : anton 1.9
59 : anton 1.1 : compare-images { image1 image2 reloc-bits size file-id -- }
60 :     \G compares image1 and image2 (of size cells) and sets reloc-bits.
61 :     \G offset is the difference for relocated addresses
62 :     \ this definition is certainly to long and too complex, but is
63 :     \ hard to factor.
64 :     image1 @ image2 @ over - { dbase doffset }
65 :     doffset 0= abort" images have the same dictionary base address"
66 :     ." data offset=" doffset . cr
67 : anton 1.9 ." code" image1 image2 cell 26 cells image-data { cbase coffset }
68 :     ." xt" image1 image2 11 cells 22 cells image-data { xbase xoffset }
69 : anton 1.1 size 0
70 :     u+do
71 :     image1 i th @ image2 i th @ { cell1 cell2 }
72 :     cell1 doffset + cell2 =
73 :     if
74 :     cell1 dbase - file-id write-cell throw
75 :     i reloc-bits set-bit
76 :     else
77 :     coffset 0<> cell1 coffset + cell2 = and
78 :     if
79 :     cell1 cbase - cell / { tag }
80 : pazsan 1.11 tag >tag $4000 xor file-id write-cell throw
81 : anton 1.1 i reloc-bits set-bit
82 :     else
83 : anton 1.9 xoffset 0<> cell1 xoffset + cell2 = and
84 : anton 1.1 if
85 : anton 1.9 cell1 xbase - cell / { tag }
86 : pazsan 1.11 tag >tag file-id write-cell throw
87 : anton 1.9 i reloc-bits set-bit
88 :     else
89 :     cell1 file-id write-cell throw
90 :     cell1 cell2 <>
91 :     if
92 :     0 i th 9 u.r cell1 17 u.r cell2 17 u.r cr
93 :     endif
94 : anton 1.1 endif
95 :     endif
96 :     endif
97 :     loop ;
98 :    
99 :     : comp-image ( "image-file1" "image-file2" "new-image" -- )
100 :     name slurp-file { image1 size1 }
101 : anton 1.10 image1 size1 s" Gforth3" search 0= abort" not a Gforth image"
102 : anton 1.1 drop 8 + image1 - { header-offset }
103 :     size1 aligned size1 <> abort" unaligned image size"
104 : anton 1.8 image1 header-offset + 2 cells + @ header-offset + size1 <> abort" header gives wrong size"
105 : anton 1.1 name slurp-file { image2 size2 }
106 :     size1 size2 <> abort" image sizes differ"
107 :     name ( "new-image" ) w/o bin create-file throw { outfile }
108 :     size1 header-offset - 1- cell / bits/au / 1+ { reloc-size }
109 :     reloc-size allocate throw { reloc-bits }
110 :     reloc-bits reloc-size erase
111 :     image1 header-offset outfile write-file throw
112 :     base @ hex
113 :     image1 header-offset + image2 header-offset + reloc-bits
114 :     size1 header-offset - aligned cell / outfile compare-images
115 :     base !
116 :     reloc-bits reloc-size outfile write-file throw
117 :     outfile close-file throw ;
118 :    
119 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help