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