cpu-sh.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* BFD library support routines for the Renesas / SuperH SH architecture.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Hacked by Steve Chamberlain of Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "../opcodes/sh-opc.h"
  21. #define N(NUMBER, PRINT, DEFAULT, NEXT) \
  22. { \
  23. 32, /* Bits in a word. */ \
  24. 32, /* Bits in an address. */ \
  25. 8, /* Bits in a byte. */ \
  26. bfd_arch_sh, \
  27. NUMBER, \
  28. "sh", \
  29. PRINT, \
  30. 1, /* Section alignment power. */ \
  31. DEFAULT, \
  32. bfd_default_compatible, \
  33. bfd_default_scan, \
  34. bfd_arch_default_fill, \
  35. NEXT, \
  36. 0 /* Maximum offset of a reloc from the start of an insn. */ \
  37. }
  38. static const bfd_arch_info_type arch_info_struct[] =
  39. {
  40. N (bfd_mach_sh2, "sh2", false, arch_info_struct + 1),
  41. N (bfd_mach_sh2e, "sh2e", false, arch_info_struct + 2),
  42. N (bfd_mach_sh_dsp, "sh-dsp", false, arch_info_struct + 3),
  43. N (bfd_mach_sh3, "sh3", false, arch_info_struct + 4),
  44. N (bfd_mach_sh3_nommu, "sh3-nommu", false, arch_info_struct + 5),
  45. N (bfd_mach_sh3_dsp, "sh3-dsp", false, arch_info_struct + 6),
  46. N (bfd_mach_sh3e, "sh3e", false, arch_info_struct + 7),
  47. N (bfd_mach_sh4, "sh4", false, arch_info_struct + 8),
  48. N (bfd_mach_sh4a, "sh4a", false, arch_info_struct + 9),
  49. N (bfd_mach_sh4al_dsp, "sh4al-dsp", false, arch_info_struct + 10),
  50. N (bfd_mach_sh4_nofpu, "sh4-nofpu", false, arch_info_struct + 11),
  51. N (bfd_mach_sh4_nommu_nofpu, "sh4-nommu-nofpu", false, arch_info_struct + 12),
  52. N (bfd_mach_sh4a_nofpu, "sh4a-nofpu", false, arch_info_struct + 13),
  53. N (bfd_mach_sh2a, "sh2a", false, arch_info_struct + 14),
  54. N (bfd_mach_sh2a_nofpu, "sh2a-nofpu", false, arch_info_struct + 15),
  55. N (bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, "sh2a-nofpu-or-sh4-nommu-nofpu", false, arch_info_struct + 16),
  56. N (bfd_mach_sh2a_nofpu_or_sh3_nommu, "sh2a-nofpu-or-sh3-nommu", false, arch_info_struct + 17),
  57. N (bfd_mach_sh2a_or_sh4, "sh2a-or-sh4", false, arch_info_struct + 18),
  58. N (bfd_mach_sh2a_or_sh3e, "sh2a-or-sh3e", false, NULL)
  59. };
  60. const bfd_arch_info_type bfd_sh_arch =
  61. N (bfd_mach_sh, "sh", true, arch_info_struct + 0);
  62. /* This table defines the mappings from the BFD internal numbering
  63. system to the opcodes internal flags system.
  64. It is used by the functions defined below.
  65. The prototypes for these SH specific functions are found in
  66. sh-opc.h . */
  67. static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
  68. {
  69. { bfd_mach_sh, arch_sh1, arch_sh_up },
  70. { bfd_mach_sh2, arch_sh2, arch_sh2_up },
  71. { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
  72. { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
  73. { bfd_mach_sh2a, arch_sh2a, arch_sh2a_up },
  74. { bfd_mach_sh2a_nofpu, arch_sh2a_nofpu, arch_sh2a_nofpu_up },
  75. { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
  76. { bfd_mach_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu_up },
  77. { bfd_mach_sh2a_or_sh4, arch_sh2a_or_sh4, arch_sh2a_or_sh4_up },
  78. { bfd_mach_sh2a_or_sh3e, arch_sh2a_or_sh3e, arch_sh2a_or_sh3e_up },
  79. { bfd_mach_sh3, arch_sh3, arch_sh3_up },
  80. { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
  81. { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
  82. { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
  83. { bfd_mach_sh4, arch_sh4, arch_sh4_up },
  84. { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
  85. { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
  86. { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofpu_up },
  87. { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
  88. { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofpu_up },
  89. { 0, 0, 0 } /* Terminator. */
  90. };
  91. /* Convert a BFD mach number into the right opcodes arch flags
  92. using the table above. */
  93. unsigned int
  94. sh_get_arch_from_bfd_mach (unsigned long mach)
  95. {
  96. int i = 0;
  97. while (bfd_to_arch_table[i].bfd_mach != 0)
  98. if (bfd_to_arch_table[i].bfd_mach == mach)
  99. return bfd_to_arch_table[i].arch;
  100. else
  101. i++;
  102. /* Machine not found. */
  103. BFD_FAIL();
  104. return SH_ARCH_UNKNOWN_ARCH;
  105. }
  106. /* Convert a BFD mach number into a set of opcodes arch flags
  107. describing all the compatible architectures (i.e. arch_up)
  108. using the table above. */
  109. unsigned int
  110. sh_get_arch_up_from_bfd_mach (unsigned long mach)
  111. {
  112. int i = 0;
  113. while (bfd_to_arch_table[i].bfd_mach != 0)
  114. if (bfd_to_arch_table[i].bfd_mach == mach)
  115. return bfd_to_arch_table[i].arch_up;
  116. else
  117. i++;
  118. /* Machine not found. */
  119. BFD_FAIL();
  120. return SH_ARCH_UNKNOWN_ARCH;
  121. }
  122. /* Convert an arbitary arch_set - not necessarily corresponding
  123. directly to anything in the table above - to the most generic
  124. architecture which supports all the required features, and
  125. return the corresponding BFD mach. */
  126. unsigned long
  127. sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
  128. {
  129. unsigned long result = 0;
  130. unsigned int best = ~arch_set;
  131. unsigned int co_mask = ~0;
  132. int i = 0;
  133. /* If arch_set permits variants with no coprocessor then do not allow
  134. the other irrelevant co-processor bits to influence the choice:
  135. e.g. if dsp is disallowed by arch_set, then the algorithm would
  136. prefer fpu variants over nofpu variants because they also disallow
  137. dsp - even though the nofpu would be the most correct choice.
  138. This assumes that EVERY fpu/dsp variant has a no-coprocessor
  139. counter-part, or their non-fpu/dsp instructions do not have the
  140. no co-processor bit set. */
  141. if (arch_set & arch_sh_no_co)
  142. co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
  143. while (bfd_to_arch_table[i].bfd_mach != 0)
  144. {
  145. unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
  146. /* Conceptually: Find the architecture with the least number
  147. of extra features or, if they have the same number, then
  148. the greatest number of required features. Disregard
  149. architectures where the required features alone do
  150. not describe a valid architecture. */
  151. if (((try & ~arch_set) < (best & ~arch_set)
  152. || ((try & ~arch_set) == (best & ~arch_set)
  153. && (~try & arch_set) < (~best & arch_set)))
  154. && SH_MERGE_ARCH_SET_VALID (try, arch_set))
  155. {
  156. result = bfd_to_arch_table[i].bfd_mach;
  157. best = try;
  158. }
  159. i++;
  160. }
  161. /* This might happen if a new variant is added to sh-opc.h
  162. but no corresponding entry is added to the table above. */
  163. BFD_ASSERT (result != 0);
  164. return result;
  165. }