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