2004-05-28 Andrew Stubbs <andrew.stubbs@superh.com>
[deliverable/binutils-gdb.git] / bfd / cpu-sh.c
1 /* BFD library support routines for the Renesas / SuperH SH architecture.
2 Copyright 1993, 1994, 1997, 1998, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Hacked by Steve Chamberlain of Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "../opcodes/sh-opc.h"
26
27 #define SH_NEXT &arch_info_struct[0]
28 #define SH2_NEXT &arch_info_struct[1]
29 #define SH2E_NEXT &arch_info_struct[2]
30 #define SH_DSP_NEXT &arch_info_struct[3]
31 #define SH3_NEXT &arch_info_struct[4]
32 #define SH3_NOMMU_NEXT &arch_info_struct[5]
33 #define SH3_DSP_NEXT &arch_info_struct[6]
34 #define SH3E_NEXT &arch_info_struct[7]
35 #define SH4_NEXT &arch_info_struct[8]
36 #define SH4A_NEXT &arch_info_struct[9]
37 #define SH4AL_DSP_NEXT &arch_info_struct[10]
38 #define SH4_NOFPU_NEXT &arch_info_struct[11]
39 #define SH4_NOMMU_NOFPU_NEXT &arch_info_struct[12]
40 #define SH4A_NOFPU_NEXT &arch_info_struct[13]
41 #define SH64_NEXT NULL
42
43 static const bfd_arch_info_type arch_info_struct[] =
44 {
45 {
46 32, /* 32 bits in a word */
47 32, /* 32 bits in an address */
48 8, /* 8 bits in a byte */
49 bfd_arch_sh,
50 bfd_mach_sh2,
51 "sh", /* arch_name */
52 "sh2", /* printable name */
53 1,
54 FALSE, /* not the default */
55 bfd_default_compatible,
56 bfd_default_scan,
57 SH2_NEXT
58 },
59 {
60 32, /* 32 bits in a word */
61 32, /* 32 bits in an address */
62 8, /* 8 bits in a byte */
63 bfd_arch_sh,
64 bfd_mach_sh2e,
65 "sh", /* arch_name */
66 "sh2e", /* printable name */
67 1,
68 FALSE, /* not the default */
69 bfd_default_compatible,
70 bfd_default_scan,
71 SH2E_NEXT
72 },
73 {
74 32, /* 32 bits in a word */
75 32, /* 32 bits in an address */
76 8, /* 8 bits in a byte */
77 bfd_arch_sh,
78 bfd_mach_sh_dsp,
79 "sh", /* arch_name */
80 "sh-dsp", /* printable name */
81 1,
82 FALSE, /* not the default */
83 bfd_default_compatible,
84 bfd_default_scan,
85 SH_DSP_NEXT
86 },
87 {
88 32, /* 32 bits in a word */
89 32, /* 32 bits in an address */
90 8, /* 8 bits in a byte */
91 bfd_arch_sh,
92 bfd_mach_sh3,
93 "sh", /* arch_name */
94 "sh3", /* printable name */
95 1,
96 FALSE, /* not the default */
97 bfd_default_compatible,
98 bfd_default_scan,
99 SH3_NEXT
100 },
101 {
102 32, /* 32 bits in a word */
103 32, /* 32 bits in an address */
104 8, /* 8 bits in a byte */
105 bfd_arch_sh,
106 bfd_mach_sh3_nommu,
107 "sh", /* arch_name */
108 "sh3-nommu", /* printable name */
109 1,
110 FALSE, /* not the default */
111 bfd_default_compatible,
112 bfd_default_scan,
113 SH3_NOMMU_NEXT
114 },
115 {
116 32, /* 32 bits in a word */
117 32, /* 32 bits in an address */
118 8, /* 8 bits in a byte */
119 bfd_arch_sh,
120 bfd_mach_sh3_dsp,
121 "sh", /* arch_name */
122 "sh3-dsp", /* printable name */
123 1,
124 FALSE, /* not the default */
125 bfd_default_compatible,
126 bfd_default_scan,
127 SH3_DSP_NEXT
128 },
129 {
130 32, /* 32 bits in a word */
131 32, /* 32 bits in an address */
132 8, /* 8 bits in a byte */
133 bfd_arch_sh,
134 bfd_mach_sh3e,
135 "sh", /* arch_name */
136 "sh3e", /* printable name */
137 1,
138 FALSE, /* not the default */
139 bfd_default_compatible,
140 bfd_default_scan,
141 SH3E_NEXT
142 },
143 {
144 32, /* 32 bits in a word */
145 32, /* 32 bits in an address */
146 8, /* 8 bits in a byte */
147 bfd_arch_sh,
148 bfd_mach_sh4,
149 "sh", /* arch_name */
150 "sh4", /* printable name */
151 1,
152 FALSE, /* not the default */
153 bfd_default_compatible,
154 bfd_default_scan,
155 SH4_NEXT
156 },
157 {
158 32, /* 32 bits in a word */
159 32, /* 32 bits in an address */
160 8, /* 8 bits in a byte */
161 bfd_arch_sh,
162 bfd_mach_sh4a,
163 "sh", /* arch_name */
164 "sh4a", /* printable name */
165 1,
166 FALSE, /* not the default */
167 bfd_default_compatible,
168 bfd_default_scan,
169 SH4A_NEXT
170 },
171 {
172 32, /* 32 bits in a word */
173 32, /* 32 bits in an address */
174 8, /* 8 bits in a byte */
175 bfd_arch_sh,
176 bfd_mach_sh4al_dsp,
177 "sh", /* arch_name */
178 "sh4al-dsp", /* printable name */
179 1,
180 FALSE, /* not the default */
181 bfd_default_compatible,
182 bfd_default_scan,
183 SH4AL_DSP_NEXT
184 },
185 {
186 32, /* 32 bits in a word */
187 32, /* 32 bits in an address */
188 8, /* 8 bits in a byte */
189 bfd_arch_sh,
190 bfd_mach_sh4_nofpu,
191 "sh", /* arch_name */
192 "sh4-nofpu", /* printable name */
193 1,
194 FALSE, /* not the default */
195 bfd_default_compatible,
196 bfd_default_scan,
197 SH4_NOFPU_NEXT
198 },
199 {
200 32, /* 32 bits in a word */
201 32, /* 32 bits in an address */
202 8, /* 8 bits in a byte */
203 bfd_arch_sh,
204 bfd_mach_sh4_nommu_nofpu,
205 "sh", /* arch_name */
206 "sh4-nommu-nofpu", /* printable name */
207 1,
208 FALSE, /* not the default */
209 bfd_default_compatible,
210 bfd_default_scan,
211 SH4_NOMMU_NOFPU_NEXT
212 },
213 {
214 32, /* 32 bits in a word */
215 32, /* 32 bits in an address */
216 8, /* 8 bits in a byte */
217 bfd_arch_sh,
218 bfd_mach_sh4a_nofpu,
219 "sh", /* arch_name */
220 "sh4a-nofpu", /* printable name */
221 1,
222 FALSE, /* not the default */
223 bfd_default_compatible,
224 bfd_default_scan,
225 SH4A_NOFPU_NEXT
226 },
227 {
228 64, /* 64 bits in a word */
229 64, /* 64 bits in an address */
230 8, /* 8 bits in a byte */
231 bfd_arch_sh,
232 bfd_mach_sh5,
233 "sh", /* arch_name */
234 "sh5", /* printable name */
235 1,
236 FALSE, /* not the default */
237 bfd_default_compatible,
238 bfd_default_scan,
239 SH64_NEXT
240 },
241 };
242
243 const bfd_arch_info_type bfd_sh_arch =
244 {
245 32, /* 32 bits in a word */
246 32, /* 32 bits in an address */
247 8, /* 8 bits in a byte */
248 bfd_arch_sh,
249 bfd_mach_sh,
250 "sh", /* arch_name */
251 "sh", /* printable name */
252 1,
253 TRUE, /* the default machine */
254 bfd_default_compatible,
255 bfd_default_scan,
256 SH_NEXT
257 };
258
259
260 /* This table defines the mappings from the BFD internal numbering
261 system to the opcodes internal flags system.
262 It is used by the functions defined below.
263 The prototypes for these SH specific functions are found in
264 sh-opc.h . */
265
266 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
267 {
268 { bfd_mach_sh, arch_sh1, arch_sh1_up },
269 { bfd_mach_sh2, arch_sh2, arch_sh2_up },
270 { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
271 { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
272 { bfd_mach_sh3, arch_sh3, arch_sh3_up },
273 { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
274 { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
275 { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
276 { bfd_mach_sh4, arch_sh4, arch_sh4_up },
277 { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
278 { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
279 { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofp_up },
280 { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
281 { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofp_up },
282 { 0, 0, 0 } /* Terminator. */
283 };
284
285
286 /* Convert a BFD mach number into the right opcodes arch flags
287 using the table above. */
288
289 unsigned int
290 sh_get_arch_from_bfd_mach (unsigned long mach)
291 {
292 int i = 0;
293
294 while (bfd_to_arch_table[i].bfd_mach != 0)
295 if (bfd_to_arch_table[i].bfd_mach == mach)
296 return bfd_to_arch_table[i].arch;
297 else
298 i++;
299
300 /* mach not found. */
301 BFD_FAIL();
302
303 return SH_ARCH_UNKNOWN_ARCH;
304 }
305
306
307 /* Convert a BFD mach number into a set of opcodes arch flags
308 describing all the compatible architectures (i.e. arch_up)
309 using the table above. */
310
311 unsigned int
312 sh_get_arch_up_from_bfd_mach (unsigned long mach)
313 {
314 int i = 0;
315
316 while (bfd_to_arch_table[i].bfd_mach != 0)
317 if (bfd_to_arch_table[i].bfd_mach == mach)
318 return bfd_to_arch_table[i].arch_up;
319 else
320 i++;
321
322 /* mach not found. */
323 BFD_FAIL();
324
325 return SH_ARCH_UNKNOWN_ARCH;
326 }
327
328
329 /* Convert an arbitary arch_set - not necessarily corresponding
330 directly to anything in the table above - to the most generic
331 architecture which supports all the required features, and
332 return the corresponding BFD mach. */
333
334 unsigned long
335 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
336 {
337 unsigned long result = 0;
338 unsigned int best = ~arch_set;
339 unsigned int co_mask = ~0;
340 int i = 0;
341
342 /* If arch_set permits variants with no coprocessor then do not allow
343 the other irrelevant co-processor bits to influence the choice:
344 e.g. if dsp is disallowed by arch_set, then the algorithm would
345 prefer fpu variants over nofpu variants because they also disallow
346 dsp - even though the nofpu would be the most correct choice.
347 This assumes that EVERY fpu/dsp variant has a no-coprocessor
348 counter-part, or their non-fpu/dsp instructions do not have the
349 no co-processor bit set. */
350 if (arch_set & arch_sh_no_co)
351 co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
352
353 while (bfd_to_arch_table[i].bfd_mach != 0)
354 {
355 unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
356
357 /* Conceptually: Find the architecture with the least number
358 of extra features or, if they have the same number, then
359 the greatest number of required features. Disregard
360 architectures where the required features alone do
361 not describe a valid architecture. */
362 if (((try & ~arch_set) < (best & ~arch_set)
363 || ((try & ~arch_set) == (best & ~arch_set)
364 && (~try & arch_set) < (~best & arch_set)))
365 && SH_MERGE_ARCH_SET_VALID (try, arch_set))
366 {
367 result = bfd_to_arch_table[i].bfd_mach;
368 best = try;
369 }
370
371 i++;
372 }
373
374 /* This might happen if a new variant is added to sh-opc.h
375 but no corresponding entry is added to the table above. */
376 BFD_ASSERT (result != 0);
377
378 return result;
379 }
380
381
382 /* Merge the architecture type of two BFD files, such that the
383 resultant architecture supports all the features required
384 by the two input BFDs.
385 If the input BFDs are multually incompatible - i.e. one uses
386 DSP while the other uses FPU - or there is no known architecture
387 that fits the requirements then an error is emitted. */
388
389 bfd_boolean
390 sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
391 {
392 unsigned int old_arch, new_arch, merged_arch;
393
394 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
395 return FALSE;
396
397 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
398 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
399
400 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
401
402 if (!SH_VALID_CO_ARCH_SET (merged_arch))
403 {
404 (*_bfd_error_handler)
405 ("%s: uses %s instructions while previous modules use %s instructions",
406 bfd_archive_filename (ibfd),
407 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
408 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
409 bfd_set_error (bfd_error_bad_value);
410 return FALSE;
411 }
412 else if (!SH_VALID_ARCH_SET (merged_arch))
413 {
414 (*_bfd_error_handler)
415 ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
416 bfd_printable_name (obfd),
417 bfd_printable_name (ibfd));
418 bfd_set_error (bfd_error_bad_value);
419 return FALSE;
420 }
421
422 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
423 sh_get_bfd_mach_from_arch_set (merged_arch));
424
425 return TRUE;
426 }
This page took 0.038316 seconds and 4 git commands to generate.