ld/
[binutils.git] / bfd / cpu-m68k.c
blob2e6f6c690508671038c1cc12a1cc80f93f9d6401
1 /* BFD library support routines for architectures.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1997, 1998, 2000, 2001, 2002,
3 2003, 2004, 2006 Free Software Foundation, Inc.
4 Hacked by Steve Chamberlain of Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "opcode/m68k.h"
27 static const bfd_arch_info_type *
28 bfd_m68k_compatible (const bfd_arch_info_type *a,
29 const bfd_arch_info_type *b);
31 #define N(name, print,d,next) \
32 { 32, 32, 8, bfd_arch_m68k, name, "m68k",print,2,d,bfd_m68k_compatible,bfd_default_scan, next, }
34 static const bfd_arch_info_type arch_info_struct[] =
36 N(bfd_mach_m68000, "m68k:68000", FALSE, &arch_info_struct[1]),
37 N(bfd_mach_m68008, "m68k:68008", FALSE, &arch_info_struct[2]),
38 N(bfd_mach_m68010, "m68k:68010", FALSE, &arch_info_struct[3]),
39 N(bfd_mach_m68020, "m68k:68020", FALSE, &arch_info_struct[4]),
40 N(bfd_mach_m68030, "m68k:68030", FALSE, &arch_info_struct[5]),
41 N(bfd_mach_m68040, "m68k:68040", FALSE, &arch_info_struct[6]),
42 N(bfd_mach_m68060, "m68k:68060", FALSE, &arch_info_struct[7]),
43 N(bfd_mach_cpu32, "m68k:cpu32", FALSE, &arch_info_struct[8]),
44 N(bfd_mach_fido, "m68k:fido", FALSE, &arch_info_struct[9]),
46 /* Various combinations of CF architecture features */
47 N(bfd_mach_mcf_isa_a_nodiv, "m68k:isa-a:nodiv",
48 FALSE, &arch_info_struct[10]),
49 N(bfd_mach_mcf_isa_a, "m68k:isa-a",
50 FALSE, &arch_info_struct[11]),
51 N(bfd_mach_mcf_isa_a_mac, "m68k:isa-a:mac",
52 FALSE, &arch_info_struct[12]),
53 N(bfd_mach_mcf_isa_a_emac, "m68k:isa-a:emac",
54 FALSE, &arch_info_struct[13]),
55 N(bfd_mach_mcf_isa_aplus, "m68k:isa-aplus",
56 FALSE, &arch_info_struct[14]),
57 N(bfd_mach_mcf_isa_aplus_mac, "m68k:isa-aplus:mac",
58 FALSE, &arch_info_struct[15]),
59 N(bfd_mach_mcf_isa_aplus_emac, "m68k:isa-aplus:emac",
60 FALSE, &arch_info_struct[16]),
61 N(bfd_mach_mcf_isa_b_nousp, "m68k:isa-b:nousp",
62 FALSE, &arch_info_struct[17]),
63 N(bfd_mach_mcf_isa_b_nousp_mac, "m68k:isa-b:nousp:mac",
64 FALSE, &arch_info_struct[18]),
65 N(bfd_mach_mcf_isa_b_nousp_emac, "m68k:isa-b:nousp:emac",
66 FALSE, &arch_info_struct[19]),
67 N(bfd_mach_mcf_isa_b, "m68k:isa-b",
68 FALSE, &arch_info_struct[20]),
69 N(bfd_mach_mcf_isa_b_mac, "m68k:isa-b:mac",
70 FALSE, &arch_info_struct[21]),
71 N(bfd_mach_mcf_isa_b_emac, "m68k:isa-b:emac",
72 FALSE, &arch_info_struct[22]),
73 N(bfd_mach_mcf_isa_b_float, "m68k:isa-b:float",
74 FALSE, &arch_info_struct[23]),
75 N(bfd_mach_mcf_isa_b_float_mac, "m68k:isa-b:float:mac",
76 FALSE, &arch_info_struct[24]),
77 N(bfd_mach_mcf_isa_b_float_emac, "m68k:isa-b:float:emac",
78 FALSE, &arch_info_struct[25]),
80 /* Legacy names for CF architectures */
81 N(bfd_mach_mcf_isa_a_nodiv, "m68k:5200", FALSE, &arch_info_struct[26]),
82 N(bfd_mach_mcf_isa_a_mac,"m68k:5206e", FALSE, &arch_info_struct[27]),
83 N(bfd_mach_mcf_isa_a_mac, "m68k:5307", FALSE, &arch_info_struct[28]),
84 N(bfd_mach_mcf_isa_b_nousp_mac, "m68k:5407", FALSE, &arch_info_struct[29]),
85 N(bfd_mach_mcf_isa_aplus_emac, "m68k:528x", FALSE, &arch_info_struct[30]),
86 N(bfd_mach_mcf_isa_aplus_emac, "m68k:521x", FALSE, &arch_info_struct[31]),
87 N(bfd_mach_mcf_isa_a_emac, "m68k:5249", FALSE, &arch_info_struct[32]),
88 N(bfd_mach_mcf_isa_b_float_emac, "m68k:547x",
89 FALSE, &arch_info_struct[33]),
90 N(bfd_mach_mcf_isa_b_float_emac, "m68k:548x",
91 FALSE, &arch_info_struct[34]),
92 N(bfd_mach_mcf_isa_b_float_emac, "m68k:cfv4e", FALSE, 0),
95 const bfd_arch_info_type bfd_m68k_arch =
96 N(0, "m68k", TRUE, &arch_info_struct[0]);
98 /* Table indexed by bfd_mach_arch number indicating which
99 architectural features are supported. */
100 static const unsigned m68k_arch_features[] =
103 m68000|m68881|m68851,
104 m68000|m68881|m68851,
105 m68010|m68881|m68851,
106 m68020|m68881|m68851,
107 m68030|m68881|m68851,
108 m68040|m68881|m68851,
109 m68060|m68881|m68851,
110 cpu32|m68881,
111 fido_a|m68881,
112 mcfisa_a,
113 mcfisa_a|mcfhwdiv,
114 mcfisa_a|mcfhwdiv|mcfmac,
115 mcfisa_a|mcfhwdiv|mcfemac,
116 mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp,
117 mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp|mcfmac,
118 mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp|mcfemac,
119 mcfisa_a|mcfhwdiv|mcfisa_b,
120 mcfisa_a|mcfhwdiv|mcfisa_b|mcfmac,
121 mcfisa_a|mcfhwdiv|mcfisa_b|mcfemac,
122 mcfisa_a|mcfhwdiv|mcfisa_b|mcfusp,
123 mcfisa_a|mcfhwdiv|mcfisa_b|mcfusp|mcfmac,
124 mcfisa_a|mcfhwdiv|mcfisa_b|mcfusp|mcfemac,
125 mcfisa_a|mcfhwdiv|mcfisa_b|mcfusp|cfloat,
126 mcfisa_a|mcfhwdiv|mcfisa_b|mcfusp|cfloat|mcfmac,
127 mcfisa_a|mcfhwdiv|mcfisa_b|mcfusp|cfloat|mcfemac,
130 /* Return the count of bits set in MASK */
131 static unsigned
132 bit_count (unsigned mask)
134 unsigned ix;
136 for (ix = 0; mask; ix++)
137 /* Clear the LSB set */
138 mask ^= mask & -mask;
139 return ix;
142 /* Return the architectural features supported by MACH */
144 unsigned
145 bfd_m68k_mach_to_features (int mach)
147 if ((unsigned)mach
148 >= sizeof (m68k_arch_features) / sizeof (m68k_arch_features[0]))
149 mach = 0;
150 return m68k_arch_features[mach];
153 /* Return the bfd machine that most closely represents the
154 architectural features. We find the machine with the smallest
155 number of additional features. If there is no such machine, we
156 find the one with the smallest number of missing features. */
158 int bfd_m68k_features_to_mach (unsigned features)
160 int superset = 0, subset = 0;
161 unsigned extra = 99, missing = 99;
162 unsigned ix;
164 for (ix = 0;
165 ix != sizeof (m68k_arch_features) / sizeof (m68k_arch_features[0]);
166 ix++)
168 unsigned this_extra, this_missing;
170 if (m68k_arch_features[ix] == features)
171 return ix;
172 this_extra = bit_count (m68k_arch_features[ix] & ~features);
173 if (this_extra < extra)
175 extra = this_extra;
176 superset = ix;
179 this_missing = bit_count (features & ~m68k_arch_features[ix]);
180 if (this_missing < missing)
182 missing = this_missing;
183 superset = ix;
186 return superset ? superset : subset;
189 static const bfd_arch_info_type *
190 bfd_m68k_compatible (const bfd_arch_info_type *a,
191 const bfd_arch_info_type *b)
193 if (a->arch != b->arch)
194 return NULL;
196 if (a->bits_per_word != b->bits_per_word)
197 return NULL;
199 if (!a->mach)
200 return b;
201 if (!b->mach)
202 return a;
204 if (a->mach <= bfd_mach_m68060 && b->mach <= bfd_mach_m68060)
205 /* Merge m68k machine. */
206 return a->mach > b->mach ? a : b;
207 else if (a->mach >= bfd_mach_cpu32 && b->mach >= bfd_mach_cpu32)
209 /* Merge the machine features. */
210 unsigned features = (bfd_m68k_mach_to_features (a->mach)
211 | bfd_m68k_mach_to_features (b->mach));
213 /* CPU32 and Coldfire are incompatible. */
214 if ((~features & (cpu32 | mcfisa_a)) == 0)
215 return NULL;
217 /* Fido and Coldfire are incompatible. */
218 if ((~features & (fido_a | mcfisa_a)) == 0)
219 return NULL;
221 /* ISA A+ and ISA B are incompatible. */
222 if ((~features & (mcfisa_aa | mcfisa_b)) == 0)
223 return NULL;
225 /* MAC and EMAC code cannot be merged. */
226 if ((~features & (mcfmac | mcfemac)) == 0)
227 return NULL;
229 /* CPU32 is compatible with Fido except that Fido does not
230 support tbl instructions. Warn when the user wants to mix
231 the two. */
232 if ((a->mach == bfd_mach_cpu32 && b->mach == bfd_mach_fido)
233 || (a->mach == bfd_mach_fido && b->mach == bfd_mach_cpu32))
235 static int cpu32_fido_mix_warning;
236 if (!cpu32_fido_mix_warning)
238 cpu32_fido_mix_warning = 1;
239 (*_bfd_error_handler) ("warning: linking CPU32 objects with fido objects");
241 return bfd_lookup_arch (a->arch,
242 bfd_m68k_features_to_mach (fido_a | m68881));
245 return bfd_lookup_arch (a->arch, bfd_m68k_features_to_mach (features));
247 else
248 /* They are incompatible. */
249 return NULL;