packaging: Enable testing infrastructure
[external/binutils.git] / bfd / cpu-sh.c
1 /* BFD library support routines for the Renesas / SuperH SH architecture.
2    Copyright (C) 1993-2019 Free Software Foundation, Inc.
3    Hacked by Steve Chamberlain of Cygnus Support.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (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., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.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 SH2A_NEXT                          arch_info_struct + 14
42 #define SH2A_NOFPU_NEXT                    arch_info_struct + 15
43 #define SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT arch_info_struct + 16
44 #define SH2A_NOFPU_OR_SH3_NOMMU_NEXT       arch_info_struct + 17
45 #define SH2A_OR_SH4_NEXT                   arch_info_struct + 18
46 #define SH2A_OR_SH3E_NEXT                  NULL
47
48 static const bfd_arch_info_type arch_info_struct[] =
49 {
50   {
51     32,                         /* 32 bits in a word.  */
52     32,                         /* 32 bits in an address.  */
53     8,                          /* 8 bits in a byte.  */
54     bfd_arch_sh,
55     bfd_mach_sh2,
56     "sh",                       /* Architecture name.  */
57     "sh2",                      /* Machine name.  */
58     1,
59     FALSE,                      /* Not the default.  */
60     bfd_default_compatible,
61     bfd_default_scan,
62     bfd_arch_default_fill,
63     SH2_NEXT
64   },
65   {
66     32,                         /* 32 bits in a word.  */
67     32,                         /* 32 bits in an address.  */
68     8,                          /* 8 bits in a byte.  */
69     bfd_arch_sh,
70     bfd_mach_sh2e,
71     "sh",                       /* Architecture name.  */
72     "sh2e",                     /* Machine name.  */
73     1,
74     FALSE,                      /* Not the default.  */
75     bfd_default_compatible,
76     bfd_default_scan,
77     bfd_arch_default_fill,
78     SH2E_NEXT
79   },
80   {
81     32,                         /* 32 bits in a word.  */
82     32,                         /* 32 bits in an address.  */
83     8,                          /* 8 bits in a byte.  */
84     bfd_arch_sh,
85     bfd_mach_sh_dsp,
86     "sh",                       /* Architecture name.   */
87     "sh-dsp",                   /* Machine name.  */
88     1,
89     FALSE,                      /* Not the default.  */
90     bfd_default_compatible,
91     bfd_default_scan,
92     bfd_arch_default_fill,
93     SH_DSP_NEXT
94   },
95   {
96     32,                         /* 32 bits in a word.  */
97     32,                         /* 32 bits in an address.  */
98     8,                          /* 8 bits in a byte.  */
99     bfd_arch_sh,
100     bfd_mach_sh3,
101     "sh",                       /* Architecture name.   */
102     "sh3",                      /* Machine name.  */
103     1,
104     FALSE,                      /* Not the default.  */
105     bfd_default_compatible,
106     bfd_default_scan,
107     bfd_arch_default_fill,
108     SH3_NEXT
109   },
110   {
111     32,                         /* 32 bits in a word.  */
112     32,                         /* 32 bits in an address.  */
113     8,                          /* 8 bits in a byte.  */
114     bfd_arch_sh,
115     bfd_mach_sh3_nommu,
116     "sh",                       /* Architecture name.   */
117     "sh3-nommu",                /* Machine name.  */
118     1,
119     FALSE,                      /* Not the default.  */
120     bfd_default_compatible,
121     bfd_default_scan,
122     bfd_arch_default_fill,
123     SH3_NOMMU_NEXT
124   },
125   {
126     32,                         /* 32 bits in a word.  */
127     32,                         /* 32 bits in an address.  */
128     8,                          /* 8 bits in a byte.  */
129     bfd_arch_sh,
130     bfd_mach_sh3_dsp,
131     "sh",                       /* Architecture name.   */
132     "sh3-dsp",                  /* Machine name.  */
133     1,
134     FALSE,                      /* Not the default.  */
135     bfd_default_compatible,
136     bfd_default_scan,
137     bfd_arch_default_fill,
138     SH3_DSP_NEXT
139   },
140   {
141     32,                         /* 32 bits in a word.  */
142     32,                         /* 32 bits in an address.  */
143     8,                          /* 8 bits in a byte.  */
144     bfd_arch_sh,
145     bfd_mach_sh3e,
146     "sh",                       /* Architecture name.   */
147     "sh3e",                     /* Machine name.  */
148     1,
149     FALSE,                      /* Not the default.  */
150     bfd_default_compatible,
151     bfd_default_scan,
152     bfd_arch_default_fill,
153     SH3E_NEXT
154   },
155   {
156     32,                         /* 32 bits in a word.  */
157     32,                         /* 32 bits in an address.  */
158     8,                          /* 8 bits in a byte.  */
159     bfd_arch_sh,
160     bfd_mach_sh4,
161     "sh",                       /* Architecture name.   */
162     "sh4",                      /* Machine name.  */
163     1,
164     FALSE,                      /* Not the default.  */
165     bfd_default_compatible,
166     bfd_default_scan,
167     bfd_arch_default_fill,
168     SH4_NEXT
169   },
170   {
171     32,                         /* 32 bits in a word.  */
172     32,                         /* 32 bits in an address.  */
173     8,                          /* 8 bits in a byte.  */
174     bfd_arch_sh,
175     bfd_mach_sh4a,
176     "sh",                       /* Architecture name.   */
177     "sh4a",                     /* Machine name.  */
178     1,
179     FALSE,                      /* Not the default.  */
180     bfd_default_compatible,
181     bfd_default_scan,
182     bfd_arch_default_fill,
183     SH4A_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_sh4al_dsp,
191     "sh",                       /* Architecture name.   */
192     "sh4al-dsp",                /* Machine name.  */
193     1,
194     FALSE,                      /* Not the default.  */
195     bfd_default_compatible,
196     bfd_default_scan,
197     bfd_arch_default_fill,
198     SH4AL_DSP_NEXT
199   },
200   {
201     32,                         /* 32 bits in a word.  */
202     32,                         /* 32 bits in an address.  */
203     8,                          /* 8 bits in a byte.  */
204     bfd_arch_sh,
205     bfd_mach_sh4_nofpu,
206     "sh",                       /* Architecture name.   */
207     "sh4-nofpu",                /* Machine name.  */
208     1,
209     FALSE,                      /* Not the default.  */
210     bfd_default_compatible,
211     bfd_default_scan,
212     bfd_arch_default_fill,
213     SH4_NOFPU_NEXT
214   },
215   {
216     32,                         /* 32 bits in a word.  */
217     32,                         /* 32 bits in an address.  */
218     8,                          /* 8 bits in a byte.  */
219     bfd_arch_sh,
220     bfd_mach_sh4_nommu_nofpu,
221     "sh",                       /* Architecture name.   */
222     "sh4-nommu-nofpu",          /* Machine name.  */
223     1,
224     FALSE,                      /* Not the default.  */
225     bfd_default_compatible,
226     bfd_default_scan,
227     bfd_arch_default_fill,
228     SH4_NOMMU_NOFPU_NEXT
229   },
230   {
231     32,                         /* 32 bits in a word.  */
232     32,                         /* 32 bits in an address.  */
233     8,                          /* 8 bits in a byte.  */
234     bfd_arch_sh,
235     bfd_mach_sh4a_nofpu,
236     "sh",                       /* Architecture name.   */
237     "sh4a-nofpu",               /* Machine name.  */
238     1,
239     FALSE,                      /* Not the default.  */
240     bfd_default_compatible,
241     bfd_default_scan,
242     bfd_arch_default_fill,
243     SH4A_NOFPU_NEXT
244   },
245   {
246     32,                         /* 32 bits in a word.  */
247     32,                         /* 32 bits in an address.  */
248     8,                          /* 8 bits in a byte.  */
249     bfd_arch_sh,
250     bfd_mach_sh2a,
251     "sh",                       /* Architecture name.  */
252     "sh2a",                     /* Machine name.  */
253     1,
254     FALSE,                      /* Not the default.  */
255     bfd_default_compatible,
256     bfd_default_scan,
257     bfd_arch_default_fill,
258     SH2A_NEXT
259   },
260   {
261     32,                         /* 32 bits in a word.  */
262     32,                         /* 32 bits in an address.  */
263     8,                          /* 8 bits in a byte.  */
264     bfd_arch_sh,
265     bfd_mach_sh2a_nofpu,
266     "sh",                       /* Architecture name.  */
267     "sh2a-nofpu",               /* Machine name.  */
268     1,
269     FALSE,                      /* Not the default.  */
270     bfd_default_compatible,
271     bfd_default_scan,
272     bfd_arch_default_fill,
273     SH2A_NOFPU_NEXT
274   },
275   {
276     32,                         /* 32 bits in a word.  */
277     32,                         /* 32 bits in an address.  */
278     8,                          /* 8 bits in a byte.  */
279     bfd_arch_sh,
280     bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,
281     "sh",                       /* Architecture name.  */
282     "sh2a-nofpu-or-sh4-nommu-nofpu",            /* Machine name.  */
283     1,
284     FALSE,                      /* Not the default.  */
285     bfd_default_compatible,
286     bfd_default_scan,
287     bfd_arch_default_fill,
288     SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
289   },
290   {
291     32,                         /* 32 bits in a word.  */
292     32,                         /* 32 bits in an address.  */
293     8,                          /* 8 bits in a byte.  */
294     bfd_arch_sh,
295     bfd_mach_sh2a_nofpu_or_sh3_nommu,
296     "sh",                       /* Architecture name. .  */
297     "sh2a-nofpu-or-sh3-nommu",  /* Machine name.  */
298     1,
299     FALSE,                      /* Not the default.  */
300     bfd_default_compatible,
301     bfd_default_scan,
302     bfd_arch_default_fill,
303     SH2A_NOFPU_OR_SH3_NOMMU_NEXT
304   },
305   {
306     32,                         /* 32 bits in a word.  */
307     32,                         /* 32 bits in an address.  */
308     8,                          /* 8 bits in a byte.  */
309     bfd_arch_sh,
310     bfd_mach_sh2a_or_sh4,
311     "sh",                       /* Architecture name.  */
312     "sh2a-or-sh4",              /* Machine name.  */
313     1,
314     FALSE,                      /* Not the default.  */
315     bfd_default_compatible,
316     bfd_default_scan,
317     bfd_arch_default_fill,
318     SH2A_OR_SH4_NEXT
319   },
320   {
321     32,                         /* 32 bits in a word.  */
322     32,                         /* 32 bits in an address.  */
323     8,                          /* 8 bits in a byte.  */
324     bfd_arch_sh,
325     bfd_mach_sh2a_or_sh3e,
326     "sh",                       /* Architecture name.  */
327     "sh2a-or-sh3e",             /* Machine name.  */
328     1,
329     FALSE,                      /* Not the default.  */
330     bfd_default_compatible,
331     bfd_default_scan,
332     bfd_arch_default_fill,
333     SH2A_OR_SH3E_NEXT
334   },
335 };
336
337 const bfd_arch_info_type bfd_sh_arch =
338 {
339   32,                           /* 32 bits in a word.  */
340   32,                           /* 32 bits in an address.  */
341   8,                            /* 8 bits in a byte.  */
342   bfd_arch_sh,
343   bfd_mach_sh,
344   "sh",                         /* Architecture name.   */
345   "sh",                         /* Machine name.  */
346   1,
347   TRUE,                         /* The default machine.  */
348   bfd_default_compatible,
349   bfd_default_scan,
350   bfd_arch_default_fill,
351   SH_NEXT
352 };
353
354
355 /* This table defines the mappings from the BFD internal numbering
356    system to the opcodes internal flags system.
357    It is used by the functions defined below.
358    The prototypes for these SH specific functions are found in
359    sh-opc.h .  */
360
361 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
362 {
363   { bfd_mach_sh,              arch_sh1,             arch_sh_up },
364   { bfd_mach_sh2,             arch_sh2,             arch_sh2_up },
365   { bfd_mach_sh2e,            arch_sh2e,            arch_sh2e_up },
366   { bfd_mach_sh_dsp,          arch_sh_dsp,          arch_sh_dsp_up },
367   { bfd_mach_sh2a,            arch_sh2a,            arch_sh2a_up },
368   { bfd_mach_sh2a_nofpu,      arch_sh2a_nofpu,      arch_sh2a_nofpu_up },
369
370   { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,         arch_sh2a_nofpu_or_sh4_nommu_nofpu,   arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
371   { bfd_mach_sh2a_nofpu_or_sh3_nommu,               arch_sh2a_nofpu_or_sh3_nommu,         arch_sh2a_nofpu_or_sh3_nommu_up },
372   { bfd_mach_sh2a_or_sh4,     arch_sh2a_or_sh4,     arch_sh2a_or_sh4_up },
373   { bfd_mach_sh2a_or_sh3e,    arch_sh2a_or_sh3e,    arch_sh2a_or_sh3e_up },
374
375   { bfd_mach_sh3,             arch_sh3,             arch_sh3_up },
376   { bfd_mach_sh3_nommu,       arch_sh3_nommu,       arch_sh3_nommu_up },
377   { bfd_mach_sh3_dsp,         arch_sh3_dsp,         arch_sh3_dsp_up },
378   { bfd_mach_sh3e,            arch_sh3e,            arch_sh3e_up },
379   { bfd_mach_sh4,             arch_sh4,             arch_sh4_up },
380   { bfd_mach_sh4a,            arch_sh4a,            arch_sh4a_up },
381   { bfd_mach_sh4al_dsp,       arch_sh4al_dsp,       arch_sh4al_dsp_up },
382   { bfd_mach_sh4_nofpu,       arch_sh4_nofpu,       arch_sh4_nofpu_up },
383   { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
384   { bfd_mach_sh4a_nofpu,      arch_sh4a_nofpu,      arch_sh4a_nofpu_up },
385   { 0, 0, 0 }   /* Terminator.  */
386 };
387
388
389 /* Convert a BFD mach number into the right opcodes arch flags
390    using the table above.  */
391
392 unsigned int
393 sh_get_arch_from_bfd_mach (unsigned long mach)
394 {
395   int i = 0;
396
397   while (bfd_to_arch_table[i].bfd_mach != 0)
398     if (bfd_to_arch_table[i].bfd_mach == mach)
399       return bfd_to_arch_table[i].arch;
400     else
401       i++;
402
403   /* Machine not found.   */
404   BFD_FAIL();
405
406   return SH_ARCH_UNKNOWN_ARCH;
407 }
408
409
410 /* Convert a BFD mach number into a set of opcodes arch flags
411    describing all the compatible architectures (i.e. arch_up)
412    using the table above.  */
413
414 unsigned int
415 sh_get_arch_up_from_bfd_mach (unsigned long mach)
416 {
417   int i = 0;
418
419   while (bfd_to_arch_table[i].bfd_mach != 0)
420     if (bfd_to_arch_table[i].bfd_mach == mach)
421       return bfd_to_arch_table[i].arch_up;
422     else
423       i++;
424
425   /* Machine not found.  */
426   BFD_FAIL();
427
428   return SH_ARCH_UNKNOWN_ARCH;
429 }
430
431
432 /* Convert an arbitary arch_set - not necessarily corresponding
433    directly to anything in the table above - to the most generic
434    architecture which supports all the required features, and
435    return the corresponding BFD mach.  */
436
437 unsigned long
438 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
439 {
440   unsigned long result = 0;
441   unsigned int best = ~arch_set;
442   unsigned int co_mask = ~0;
443   int i = 0;
444
445   /* If arch_set permits variants with no coprocessor then do not allow
446      the other irrelevant co-processor bits to influence the choice:
447        e.g. if dsp is disallowed by arch_set, then the algorithm would
448        prefer fpu variants over nofpu variants because they also disallow
449        dsp - even though the nofpu would be the most correct choice.
450      This assumes that EVERY fpu/dsp variant has a no-coprocessor
451      counter-part, or their non-fpu/dsp instructions do not have the
452      no co-processor bit set.  */
453   if (arch_set & arch_sh_no_co)
454     co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
455
456   while (bfd_to_arch_table[i].bfd_mach != 0)
457     {
458       unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
459
460       /* Conceptually: Find the architecture with the least number
461          of extra features or, if they have the same number, then
462          the greatest number of required features.  Disregard
463          architectures where the required features alone do
464          not describe a valid architecture.  */
465       if (((try & ~arch_set) < (best & ~arch_set)
466            || ((try & ~arch_set) == (best & ~arch_set)
467                && (~try & arch_set) < (~best & arch_set)))
468           && SH_MERGE_ARCH_SET_VALID (try, arch_set))
469         {
470           result = bfd_to_arch_table[i].bfd_mach;
471           best = try;
472         }
473
474       i++;
475     }
476
477   /* This might happen if a new variant is added to sh-opc.h
478      but no corresponding entry is added to the table above.  */
479   BFD_ASSERT (result != 0);
480
481   return result;
482 }