Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ppc-dis.c -- Disassemble PowerPC instructions |
6f2750fe | 2 | Copyright (C) 1994-2016 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Ian Lance Taylor, Cygnus Support |
4 | ||
9b201bb5 NC |
5 | This file is part of the GNU opcodes library. |
6 | ||
7 | This library 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, or (at your option) | |
10 | any later version. | |
11 | ||
12 | It is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
15 | License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this file; see the file COPYING. If not, write to the | |
19 | Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, | |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
df7b86aa | 23 | #include <stdio.h> |
252b5132 | 24 | #include "dis-asm.h" |
b9c361e0 | 25 | #include "elf-bfd.h" |
94caa966 | 26 | #include "elf/ppc.h" |
69fe9ce5 | 27 | #include "opintl.h" |
252b5132 RH |
28 | #include "opcode/ppc.h" |
29 | ||
30 | /* This file provides several disassembler functions, all of which use | |
31 | the disassembler interface defined in dis-asm.h. Several functions | |
32 | are provided because this file handles disassembly for the PowerPC | |
33 | in both big and little endian mode and also for the POWER (RS/6000) | |
34 | chip. */ | |
fa452fa6 PB |
35 | static int print_insn_powerpc (bfd_vma, struct disassemble_info *, int, |
36 | ppc_cpu_t); | |
252b5132 | 37 | |
fa452fa6 PB |
38 | struct dis_private |
39 | { | |
40 | /* Stash the result of parsing disassembler_options here. */ | |
41 | ppc_cpu_t dialect; | |
b240011a | 42 | } private; |
fa452fa6 PB |
43 | |
44 | #define POWERPC_DIALECT(INFO) \ | |
45 | (((struct dis_private *) ((INFO)->private_data))->dialect) | |
418c1742 | 46 | |
69fe9ce5 AM |
47 | struct ppc_mopt { |
48 | const char *opt; | |
49 | ppc_cpu_t cpu; | |
50 | ppc_cpu_t sticky; | |
51 | }; | |
52 | ||
53 | struct ppc_mopt ppc_opts[] = { | |
14b57c7c | 54 | { "403", PPC_OPCODE_PPC | PPC_OPCODE_403, |
69fe9ce5 | 55 | 0 }, |
14b57c7c | 56 | { "405", PPC_OPCODE_PPC | PPC_OPCODE_403 | PPC_OPCODE_405, |
69fe9ce5 | 57 | 0 }, |
bdc70b4a AM |
58 | { "440", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_440 |
59 | | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI), | |
69fe9ce5 | 60 | 0 }, |
bdc70b4a AM |
61 | { "464", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_440 |
62 | | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI), | |
69fe9ce5 | 63 | 0 }, |
bdc70b4a AM |
64 | { "476", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_440 |
65 | | PPC_OPCODE_476 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5), | |
9fe54b1c | 66 | 0 }, |
14b57c7c | 67 | { "601", PPC_OPCODE_PPC | PPC_OPCODE_601, |
69fe9ce5 | 68 | 0 }, |
14b57c7c | 69 | { "603", PPC_OPCODE_PPC, |
69fe9ce5 | 70 | 0 }, |
14b57c7c | 71 | { "604", PPC_OPCODE_PPC, |
69fe9ce5 | 72 | 0 }, |
14b57c7c | 73 | { "620", PPC_OPCODE_PPC | PPC_OPCODE_64, |
69fe9ce5 | 74 | 0 }, |
14b57c7c | 75 | { "7400", PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC, |
69fe9ce5 | 76 | 0 }, |
14b57c7c | 77 | { "7410", PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC, |
69fe9ce5 | 78 | 0 }, |
14b57c7c | 79 | { "7450", PPC_OPCODE_PPC | PPC_OPCODE_7450 | PPC_OPCODE_ALTIVEC, |
69fe9ce5 | 80 | 0 }, |
14b57c7c | 81 | { "7455", PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC, |
69fe9ce5 | 82 | 0 }, |
14b57c7c | 83 | { "750cl", PPC_OPCODE_PPC | PPC_OPCODE_750 | PPC_OPCODE_PPCPS |
69fe9ce5 | 84 | , 0 }, |
14b57c7c | 85 | { "821", PPC_OPCODE_PPC | PPC_OPCODE_860, |
ef5a96d5 | 86 | 0 }, |
14b57c7c | 87 | { "850", PPC_OPCODE_PPC | PPC_OPCODE_860, |
ef5a96d5 | 88 | 0 }, |
14b57c7c | 89 | { "860", PPC_OPCODE_PPC | PPC_OPCODE_860, |
ef5a96d5 | 90 | 0 }, |
bdc70b4a AM |
91 | { "a2", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_POWER4 |
92 | | PPC_OPCODE_POWER5 | PPC_OPCODE_CACHELCK | PPC_OPCODE_64 | |
93 | | PPC_OPCODE_A2), | |
cdc51b07 | 94 | 0 }, |
14b57c7c | 95 | { "altivec", PPC_OPCODE_PPC, |
c7a5aa9c | 96 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 }, |
69fe9ce5 AM |
97 | { "any", 0, |
98 | PPC_OPCODE_ANY }, | |
14b57c7c | 99 | { "booke", PPC_OPCODE_PPC | PPC_OPCODE_BOOKE, |
69fe9ce5 | 100 | 0 }, |
14b57c7c | 101 | { "booke32", PPC_OPCODE_PPC | PPC_OPCODE_BOOKE, |
69fe9ce5 | 102 | 0 }, |
bdc70b4a AM |
103 | { "cell", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 |
104 | | PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC), | |
69fe9ce5 | 105 | 0 }, |
14b57c7c | 106 | { "com", PPC_OPCODE_COMMON, |
69fe9ce5 | 107 | 0 }, |
dfdaec14 AJ |
108 | { "e200z4", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE| PPC_OPCODE_SPE |
109 | | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK | |
110 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
111 | | PPC_OPCODE_E500 | PPC_OPCODE_E200Z4), | |
112 | PPC_OPCODE_VLE }, | |
14b57c7c | 113 | { "e300", PPC_OPCODE_PPC | PPC_OPCODE_E300, |
69fe9ce5 AM |
114 | 0 }, |
115 | { "e500", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE | |
116 | | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK | |
117 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
e01d869a | 118 | | PPC_OPCODE_E500), |
69fe9ce5 AM |
119 | 0 }, |
120 | { "e500mc", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL | |
121 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
122 | | PPC_OPCODE_E500MC), | |
123 | 0 }, | |
0dc93057 AM |
124 | { "e500mc64", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL |
125 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
63d0fa4e AM |
126 | | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_POWER5 |
127 | | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7), | |
0dc93057 | 128 | 0 }, |
aea77599 AM |
129 | { "e5500", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL |
130 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
131 | | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | |
132 | | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
133 | | PPC_OPCODE_POWER7), | |
134 | 0 }, | |
135 | { "e6500", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL | |
136 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
137 | | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_ALTIVEC | |
138 | | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_E6500 | PPC_OPCODE_POWER4 | |
139 | | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7), | |
140 | 0 }, | |
69fe9ce5 AM |
141 | { "e500x2", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE |
142 | | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK | |
143 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
e01d869a | 144 | | PPC_OPCODE_E500), |
69fe9ce5 | 145 | 0 }, |
14b57c7c | 146 | { "efs", PPC_OPCODE_PPC | PPC_OPCODE_EFS, |
69fe9ce5 | 147 | 0 }, |
14b57c7c | 148 | { "power4", PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4, |
69fe9ce5 | 149 | 0 }, |
bdc70b4a AM |
150 | { "power5", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 |
151 | | PPC_OPCODE_POWER5), | |
69fe9ce5 | 152 | 0 }, |
bdc70b4a AM |
153 | { "power6", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 |
154 | | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC), | |
69fe9ce5 | 155 | 0 }, |
bdc70b4a AM |
156 | { "power7", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64 |
157 | | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
158 | | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX), | |
69fe9ce5 | 159 | 0 }, |
5817ffd1 PB |
160 | { "power8", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64 |
161 | | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
162 | | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_HTM | |
163 | | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_VSX), | |
164 | 0 }, | |
a680de9a PB |
165 | { "power9", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64 |
166 | | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
167 | | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9 | |
168 | | PPC_OPCODE_HTM | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | |
169 | | PPC_OPCODE_VSX | PPC_OPCODE_VSX3 ), | |
170 | 0 }, | |
14b57c7c | 171 | { "ppc", PPC_OPCODE_PPC, |
69fe9ce5 | 172 | 0 }, |
14b57c7c | 173 | { "ppc32", PPC_OPCODE_PPC, |
69fe9ce5 | 174 | 0 }, |
14b57c7c | 175 | { "ppc64", PPC_OPCODE_PPC | PPC_OPCODE_64, |
69fe9ce5 | 176 | 0 }, |
14b57c7c | 177 | { "ppc64bridge", PPC_OPCODE_PPC | PPC_OPCODE_64_BRIDGE, |
69fe9ce5 | 178 | 0 }, |
14b57c7c | 179 | { "ppcps", PPC_OPCODE_PPC | PPC_OPCODE_PPCPS, |
69fe9ce5 | 180 | 0 }, |
14b57c7c | 181 | { "pwr", PPC_OPCODE_POWER, |
69fe9ce5 | 182 | 0 }, |
14b57c7c | 183 | { "pwr2", PPC_OPCODE_POWER | PPC_OPCODE_POWER2, |
cdc51b07 | 184 | 0 }, |
14b57c7c | 185 | { "pwr4", PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4, |
cdc51b07 | 186 | 0 }, |
bdc70b4a AM |
187 | { "pwr5", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 |
188 | | PPC_OPCODE_POWER5), | |
cdc51b07 | 189 | 0 }, |
bdc70b4a AM |
190 | { "pwr5x", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 |
191 | | PPC_OPCODE_POWER5), | |
cdc51b07 | 192 | 0 }, |
bdc70b4a AM |
193 | { "pwr6", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4 |
194 | | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC), | |
cdc51b07 | 195 | 0 }, |
bdc70b4a AM |
196 | { "pwr7", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64 |
197 | | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
198 | | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX), | |
69fe9ce5 | 199 | 0 }, |
5817ffd1 PB |
200 | { "pwr8", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64 |
201 | | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
202 | | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_HTM | |
203 | | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_VSX), | |
204 | 0 }, | |
a680de9a PB |
205 | { "pwr9", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64 |
206 | | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | |
207 | | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9 | |
208 | | PPC_OPCODE_HTM | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | |
209 | | PPC_OPCODE_VSX | PPC_OPCODE_VSX3 ), | |
210 | 0 }, | |
14b57c7c | 211 | { "pwrx", PPC_OPCODE_POWER | PPC_OPCODE_POWER2, |
69fe9ce5 | 212 | 0 }, |
14b57c7c | 213 | { "spe", PPC_OPCODE_PPC | PPC_OPCODE_EFS, |
69fe9ce5 | 214 | PPC_OPCODE_SPE }, |
bdc70b4a AM |
215 | { "titan", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_PMR |
216 | | PPC_OPCODE_RFMCI | PPC_OPCODE_TITAN), | |
ce3d2015 | 217 | 0 }, |
14b57c7c AM |
218 | { "vle", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE| PPC_OPCODE_SPE |
219 | | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK | |
220 | | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI | |
221 | | PPC_OPCODE_E500), | |
b9c361e0 | 222 | PPC_OPCODE_VLE }, |
14b57c7c | 223 | { "vsx", PPC_OPCODE_PPC, |
a680de9a | 224 | PPC_OPCODE_VSX | PPC_OPCODE_VSX3 }, |
14b57c7c | 225 | { "htm", PPC_OPCODE_PPC, |
5817ffd1 | 226 | PPC_OPCODE_HTM }, |
69fe9ce5 AM |
227 | }; |
228 | ||
b9c361e0 JL |
229 | /* Switch between Booke and VLE dialects for interlinked dumps. */ |
230 | static ppc_cpu_t | |
231 | get_powerpc_dialect (struct disassemble_info *info) | |
232 | { | |
233 | ppc_cpu_t dialect = 0; | |
234 | ||
235 | dialect = POWERPC_DIALECT (info); | |
236 | ||
237 | /* Disassemble according to the section headers flags for VLE-mode. */ | |
238 | if (dialect & PPC_OPCODE_VLE | |
94caa966 AM |
239 | && info->section->owner != NULL |
240 | && bfd_get_flavour (info->section->owner) == bfd_target_elf_flavour | |
241 | && elf_object_id (info->section->owner) == PPC32_ELF_DATA | |
242 | && (elf_section_flags (info->section) & SHF_PPC_VLE) != 0) | |
b9c361e0 JL |
243 | return dialect; |
244 | else | |
245 | return dialect & ~ PPC_OPCODE_VLE; | |
246 | } | |
247 | ||
69fe9ce5 AM |
248 | /* Handle -m and -M options that set cpu type, and .machine arg. */ |
249 | ||
250 | ppc_cpu_t | |
776fc418 | 251 | ppc_parse_cpu (ppc_cpu_t ppc_cpu, ppc_cpu_t *sticky, const char *arg) |
69fe9ce5 | 252 | { |
69fe9ce5 AM |
253 | unsigned int i; |
254 | ||
255 | for (i = 0; i < sizeof (ppc_opts) / sizeof (ppc_opts[0]); i++) | |
256 | if (strcmp (ppc_opts[i].opt, arg) == 0) | |
257 | { | |
258 | if (ppc_opts[i].sticky) | |
259 | { | |
776fc418 AM |
260 | *sticky |= ppc_opts[i].sticky; |
261 | if ((ppc_cpu & ~*sticky) != 0) | |
69fe9ce5 AM |
262 | break; |
263 | } | |
264 | ppc_cpu = ppc_opts[i].cpu; | |
265 | break; | |
266 | } | |
267 | if (i >= sizeof (ppc_opts) / sizeof (ppc_opts[0])) | |
268 | return 0; | |
269 | ||
776fc418 | 270 | ppc_cpu |= *sticky; |
69fe9ce5 AM |
271 | return ppc_cpu; |
272 | } | |
273 | ||
274 | /* Determine which set of machines to disassemble for. */ | |
418c1742 | 275 | |
b240011a | 276 | static void |
fa452fa6 | 277 | powerpc_init_dialect (struct disassemble_info *info) |
418c1742 | 278 | { |
69fe9ce5 | 279 | ppc_cpu_t dialect = 0; |
776fc418 | 280 | ppc_cpu_t sticky = 0; |
69fe9ce5 | 281 | char *arg; |
fa452fa6 PB |
282 | struct dis_private *priv = calloc (sizeof (*priv), 1); |
283 | ||
284 | if (priv == NULL) | |
b240011a | 285 | priv = &private; |
418c1742 | 286 | |
776fc418 AM |
287 | switch (info->mach) |
288 | { | |
289 | case bfd_mach_ppc_403: | |
290 | case bfd_mach_ppc_403gc: | |
4f6ffcd3 | 291 | dialect = ppc_parse_cpu (dialect, &sticky, "403"); |
776fc418 AM |
292 | break; |
293 | case bfd_mach_ppc_405: | |
4f6ffcd3 | 294 | dialect = ppc_parse_cpu (dialect, &sticky, "405"); |
776fc418 AM |
295 | break; |
296 | case bfd_mach_ppc_601: | |
4f6ffcd3 | 297 | dialect = ppc_parse_cpu (dialect, &sticky, "601"); |
776fc418 AM |
298 | break; |
299 | case bfd_mach_ppc_a35: | |
300 | case bfd_mach_ppc_rs64ii: | |
301 | case bfd_mach_ppc_rs64iii: | |
4f6ffcd3 | 302 | dialect = ppc_parse_cpu (dialect, &sticky, "pwr2") | PPC_OPCODE_64; |
776fc418 AM |
303 | break; |
304 | case bfd_mach_ppc_e500: | |
4f6ffcd3 | 305 | dialect = ppc_parse_cpu (dialect, &sticky, "e500"); |
776fc418 AM |
306 | break; |
307 | case bfd_mach_ppc_e500mc: | |
4f6ffcd3 | 308 | dialect = ppc_parse_cpu (dialect, &sticky, "e500mc"); |
776fc418 AM |
309 | break; |
310 | case bfd_mach_ppc_e500mc64: | |
4f6ffcd3 | 311 | dialect = ppc_parse_cpu (dialect, &sticky, "e500mc64"); |
776fc418 AM |
312 | break; |
313 | case bfd_mach_ppc_e5500: | |
4f6ffcd3 | 314 | dialect = ppc_parse_cpu (dialect, &sticky, "e5500"); |
776fc418 AM |
315 | break; |
316 | case bfd_mach_ppc_e6500: | |
4f6ffcd3 | 317 | dialect = ppc_parse_cpu (dialect, &sticky, "e6500"); |
776fc418 AM |
318 | break; |
319 | case bfd_mach_ppc_titan: | |
4f6ffcd3 | 320 | dialect = ppc_parse_cpu (dialect, &sticky, "titan"); |
776fc418 AM |
321 | break; |
322 | case bfd_mach_ppc_vle: | |
4f6ffcd3 | 323 | dialect = ppc_parse_cpu (dialect, &sticky, "vle"); |
776fc418 AM |
324 | break; |
325 | default: | |
a680de9a | 326 | dialect = ppc_parse_cpu (dialect, &sticky, "power9") | PPC_OPCODE_ANY; |
776fc418 AM |
327 | } |
328 | ||
69fe9ce5 AM |
329 | arg = info->disassembler_options; |
330 | while (arg != NULL) | |
331 | { | |
332 | ppc_cpu_t new_cpu = 0; | |
333 | char *end = strchr (arg, ','); | |
9b4e5766 | 334 | |
69fe9ce5 AM |
335 | if (end != NULL) |
336 | *end = 0; | |
9b4e5766 | 337 | |
776fc418 | 338 | if ((new_cpu = ppc_parse_cpu (dialect, &sticky, arg)) != 0) |
69fe9ce5 AM |
339 | dialect = new_cpu; |
340 | else if (strcmp (arg, "32") == 0) | |
7102e95e | 341 | dialect &= ~(ppc_cpu_t) PPC_OPCODE_64; |
69fe9ce5 | 342 | else if (strcmp (arg, "64") == 0) |
bdc70b4a | 343 | dialect |= PPC_OPCODE_64; |
69fe9ce5 AM |
344 | else |
345 | fprintf (stderr, _("warning: ignoring unknown -M%s option\n"), arg); | |
9622b051 | 346 | |
69fe9ce5 AM |
347 | if (end != NULL) |
348 | *end++ = ','; | |
349 | arg = end; | |
350 | } | |
661bd698 | 351 | |
fa452fa6 PB |
352 | info->private_data = priv; |
353 | POWERPC_DIALECT(info) = dialect; | |
b240011a AM |
354 | } |
355 | ||
b9c361e0 JL |
356 | #define PPC_OPCD_SEGS 64 |
357 | static unsigned short powerpc_opcd_indices[PPC_OPCD_SEGS+1]; | |
358 | #define VLE_OPCD_SEGS 32 | |
359 | static unsigned short vle_opcd_indices[VLE_OPCD_SEGS+1]; | |
b240011a AM |
360 | |
361 | /* Calculate opcode table indices to speed up disassembly, | |
362 | and init dialect. */ | |
363 | ||
364 | void | |
365 | disassemble_init_powerpc (struct disassemble_info *info) | |
366 | { | |
367 | int i; | |
d6688282 | 368 | unsigned short last; |
fa452fa6 | 369 | |
27c49e9a | 370 | if (powerpc_opcd_indices[PPC_OPCD_SEGS] == 0) |
b240011a | 371 | { |
b240011a | 372 | |
27c49e9a AB |
373 | i = powerpc_num_opcodes; |
374 | while (--i >= 0) | |
375 | { | |
376 | unsigned op = PPC_OP (powerpc_opcodes[i].opcode); | |
377 | ||
378 | powerpc_opcd_indices[op] = i; | |
379 | } | |
380 | ||
381 | last = powerpc_num_opcodes; | |
382 | for (i = PPC_OPCD_SEGS; i > 0; --i) | |
383 | { | |
384 | if (powerpc_opcd_indices[i] == 0) | |
385 | powerpc_opcd_indices[i] = last; | |
386 | last = powerpc_opcd_indices[i]; | |
387 | } | |
388 | ||
389 | i = vle_num_opcodes; | |
390 | while (--i >= 0) | |
391 | { | |
392 | unsigned op = VLE_OP (vle_opcodes[i].opcode, vle_opcodes[i].mask); | |
393 | unsigned seg = VLE_OP_TO_SEG (op); | |
394 | ||
395 | vle_opcd_indices[seg] = i; | |
396 | } | |
397 | ||
398 | last = vle_num_opcodes; | |
399 | for (i = VLE_OPCD_SEGS; i > 0; --i) | |
400 | { | |
401 | if (vle_opcd_indices[i] == 0) | |
402 | vle_opcd_indices[i] = last; | |
403 | last = vle_opcd_indices[i]; | |
404 | } | |
b9c361e0 JL |
405 | } |
406 | ||
b240011a AM |
407 | if (info->arch == bfd_arch_powerpc) |
408 | powerpc_init_dialect (info); | |
418c1742 MG |
409 | } |
410 | ||
411 | /* Print a big endian PowerPC instruction. */ | |
252b5132 RH |
412 | |
413 | int | |
823bbe9d | 414 | print_insn_big_powerpc (bfd_vma memaddr, struct disassemble_info *info) |
252b5132 | 415 | { |
b9c361e0 | 416 | return print_insn_powerpc (memaddr, info, 1, get_powerpc_dialect (info)); |
252b5132 RH |
417 | } |
418 | ||
418c1742 | 419 | /* Print a little endian PowerPC instruction. */ |
252b5132 RH |
420 | |
421 | int | |
823bbe9d | 422 | print_insn_little_powerpc (bfd_vma memaddr, struct disassemble_info *info) |
252b5132 | 423 | { |
b9c361e0 | 424 | return print_insn_powerpc (memaddr, info, 0, get_powerpc_dialect (info)); |
252b5132 RH |
425 | } |
426 | ||
427 | /* Print a POWER (RS/6000) instruction. */ | |
428 | ||
429 | int | |
823bbe9d | 430 | print_insn_rs6000 (bfd_vma memaddr, struct disassemble_info *info) |
252b5132 RH |
431 | { |
432 | return print_insn_powerpc (memaddr, info, 1, PPC_OPCODE_POWER); | |
433 | } | |
434 | ||
ea192fa3 PB |
435 | /* Extract the operand value from the PowerPC or POWER instruction. */ |
436 | ||
437 | static long | |
438 | operand_value_powerpc (const struct powerpc_operand *operand, | |
fa452fa6 | 439 | unsigned long insn, ppc_cpu_t dialect) |
ea192fa3 PB |
440 | { |
441 | long value; | |
442 | int invalid; | |
443 | /* Extract the value from the instruction. */ | |
444 | if (operand->extract) | |
445 | value = (*operand->extract) (insn, dialect, &invalid); | |
446 | else | |
447 | { | |
b9c361e0 JL |
448 | if (operand->shift >= 0) |
449 | value = (insn >> operand->shift) & operand->bitm; | |
450 | else | |
451 | value = (insn << -operand->shift) & operand->bitm; | |
ea192fa3 PB |
452 | if ((operand->flags & PPC_OPERAND_SIGNED) != 0) |
453 | { | |
454 | /* BITM is always some number of zeros followed by some | |
b9c361e0 | 455 | number of ones, followed by some number of zeros. */ |
ea192fa3 PB |
456 | unsigned long top = operand->bitm; |
457 | /* top & -top gives the rightmost 1 bit, so this | |
458 | fills in any trailing zeros. */ | |
459 | top |= (top & -top) - 1; | |
460 | top &= ~(top >> 1); | |
461 | value = (value ^ top) - top; | |
462 | } | |
463 | } | |
464 | ||
465 | return value; | |
466 | } | |
467 | ||
468 | /* Determine whether the optional operand(s) should be printed. */ | |
469 | ||
470 | static int | |
471 | skip_optional_operands (const unsigned char *opindex, | |
fa452fa6 | 472 | unsigned long insn, ppc_cpu_t dialect) |
ea192fa3 PB |
473 | { |
474 | const struct powerpc_operand *operand; | |
475 | ||
476 | for (; *opindex != 0; opindex++) | |
477 | { | |
478 | operand = &powerpc_operands[*opindex]; | |
479 | if ((operand->flags & PPC_OPERAND_NEXT) != 0 | |
480 | || ((operand->flags & PPC_OPERAND_OPTIONAL) != 0 | |
11a0cf2e PB |
481 | && operand_value_powerpc (operand, insn, dialect) != |
482 | ppc_optional_operand_value (operand))) | |
ea192fa3 PB |
483 | return 0; |
484 | } | |
485 | ||
486 | return 1; | |
487 | } | |
488 | ||
d6688282 AM |
489 | /* Find a match for INSN in the opcode table, given machine DIALECT. |
490 | A DIALECT of -1 is special, matching all machine opcode variations. */ | |
b9c361e0 | 491 | |
d6688282 AM |
492 | static const struct powerpc_opcode * |
493 | lookup_powerpc (unsigned long insn, ppc_cpu_t dialect) | |
494 | { | |
495 | const struct powerpc_opcode *opcode; | |
496 | const struct powerpc_opcode *opcode_end; | |
497 | unsigned long op; | |
498 | ||
499 | /* Get the major opcode of the instruction. */ | |
500 | op = PPC_OP (insn); | |
501 | ||
502 | /* Find the first match in the opcode table for this major opcode. */ | |
503 | opcode_end = powerpc_opcodes + powerpc_opcd_indices[op + 1]; | |
504 | for (opcode = powerpc_opcodes + powerpc_opcd_indices[op]; | |
505 | opcode < opcode_end; | |
506 | ++opcode) | |
507 | { | |
508 | const unsigned char *opindex; | |
509 | const struct powerpc_operand *operand; | |
510 | int invalid; | |
511 | ||
512 | if ((insn & opcode->mask) != opcode->opcode | |
513 | || (dialect != (ppc_cpu_t) -1 | |
514 | && ((opcode->flags & dialect) == 0 | |
515 | || (opcode->deprecated & dialect) != 0))) | |
516 | continue; | |
517 | ||
518 | /* Check validity of operands. */ | |
519 | invalid = 0; | |
520 | for (opindex = opcode->operands; *opindex != 0; opindex++) | |
521 | { | |
522 | operand = powerpc_operands + *opindex; | |
523 | if (operand->extract) | |
524 | (*operand->extract) (insn, dialect, &invalid); | |
525 | } | |
526 | if (invalid) | |
527 | continue; | |
528 | ||
529 | return opcode; | |
530 | } | |
531 | ||
532 | return NULL; | |
533 | } | |
534 | ||
b9c361e0 JL |
535 | /* Find a match for INSN in the VLE opcode table. */ |
536 | ||
537 | static const struct powerpc_opcode * | |
538 | lookup_vle (unsigned long insn) | |
539 | { | |
540 | const struct powerpc_opcode *opcode; | |
541 | const struct powerpc_opcode *opcode_end; | |
542 | unsigned op, seg; | |
543 | ||
544 | op = PPC_OP (insn); | |
545 | if (op >= 0x20 && op <= 0x37) | |
546 | { | |
547 | /* This insn has a 4-bit opcode. */ | |
548 | op &= 0x3c; | |
549 | } | |
550 | seg = VLE_OP_TO_SEG (op); | |
551 | ||
552 | /* Find the first match in the opcode table for this major opcode. */ | |
553 | opcode_end = vle_opcodes + vle_opcd_indices[seg + 1]; | |
554 | for (opcode = vle_opcodes + vle_opcd_indices[seg]; | |
555 | opcode < opcode_end; | |
556 | ++opcode) | |
557 | { | |
558 | unsigned long table_opcd = opcode->opcode; | |
559 | unsigned long table_mask = opcode->mask; | |
560 | bfd_boolean table_op_is_short = PPC_OP_SE_VLE(table_mask); | |
561 | unsigned long insn2; | |
562 | const unsigned char *opindex; | |
563 | const struct powerpc_operand *operand; | |
564 | int invalid; | |
565 | ||
566 | insn2 = insn; | |
567 | if (table_op_is_short) | |
568 | insn2 >>= 16; | |
569 | if ((insn2 & table_mask) != table_opcd) | |
570 | continue; | |
571 | ||
572 | /* Check validity of operands. */ | |
573 | invalid = 0; | |
574 | for (opindex = opcode->operands; *opindex != 0; ++opindex) | |
575 | { | |
576 | operand = powerpc_operands + *opindex; | |
577 | if (operand->extract) | |
578 | (*operand->extract) (insn, (ppc_cpu_t)0, &invalid); | |
579 | } | |
580 | if (invalid) | |
581 | continue; | |
582 | ||
583 | return opcode; | |
584 | } | |
585 | ||
586 | return NULL; | |
587 | } | |
588 | ||
252b5132 RH |
589 | /* Print a PowerPC or POWER instruction. */ |
590 | ||
591 | static int | |
823bbe9d AM |
592 | print_insn_powerpc (bfd_vma memaddr, |
593 | struct disassemble_info *info, | |
594 | int bigendian, | |
fa452fa6 | 595 | ppc_cpu_t dialect) |
252b5132 RH |
596 | { |
597 | bfd_byte buffer[4]; | |
598 | int status; | |
599 | unsigned long insn; | |
600 | const struct powerpc_opcode *opcode; | |
b9c361e0 | 601 | bfd_boolean insn_is_short; |
252b5132 RH |
602 | |
603 | status = (*info->read_memory_func) (memaddr, buffer, 4, info); | |
604 | if (status != 0) | |
605 | { | |
b9c361e0 JL |
606 | /* The final instruction may be a 2-byte VLE insn. */ |
607 | if ((dialect & PPC_OPCODE_VLE) != 0) | |
608 | { | |
609 | /* Clear buffer so unused bytes will not have garbage in them. */ | |
610 | buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0; | |
611 | status = (*info->read_memory_func) (memaddr, buffer, 2, info); | |
612 | if (status != 0) | |
613 | { | |
614 | (*info->memory_error_func) (status, memaddr, info); | |
615 | return -1; | |
616 | } | |
617 | } | |
618 | else | |
619 | { | |
620 | (*info->memory_error_func) (status, memaddr, info); | |
621 | return -1; | |
622 | } | |
252b5132 RH |
623 | } |
624 | ||
625 | if (bigendian) | |
626 | insn = bfd_getb32 (buffer); | |
627 | else | |
628 | insn = bfd_getl32 (buffer); | |
629 | ||
b9c361e0 JL |
630 | /* Get the major opcode of the insn. */ |
631 | opcode = NULL; | |
632 | insn_is_short = FALSE; | |
633 | if ((dialect & PPC_OPCODE_VLE) != 0) | |
634 | { | |
635 | opcode = lookup_vle (insn); | |
636 | if (opcode != NULL) | |
637 | insn_is_short = PPC_OP_SE_VLE(opcode->mask); | |
638 | } | |
639 | if (opcode == NULL) | |
640 | opcode = lookup_powerpc (insn, dialect); | |
d6688282 AM |
641 | if (opcode == NULL && (dialect & PPC_OPCODE_ANY) != 0) |
642 | opcode = lookup_powerpc (insn, (ppc_cpu_t) -1); | |
252b5132 | 643 | |
d6688282 | 644 | if (opcode != NULL) |
252b5132 | 645 | { |
252b5132 RH |
646 | const unsigned char *opindex; |
647 | const struct powerpc_operand *operand; | |
252b5132 RH |
648 | int need_comma; |
649 | int need_paren; | |
ea192fa3 | 650 | int skip_optional; |
252b5132 | 651 | |
252b5132 | 652 | if (opcode->operands[0] != 0) |
fdd12ef3 AM |
653 | (*info->fprintf_func) (info->stream, "%-7s ", opcode->name); |
654 | else | |
655 | (*info->fprintf_func) (info->stream, "%s", opcode->name); | |
252b5132 | 656 | |
b9c361e0 JL |
657 | if (insn_is_short) |
658 | /* The operands will be fetched out of the 16-bit instruction. */ | |
659 | insn >>= 16; | |
660 | ||
252b5132 RH |
661 | /* Now extract and print the operands. */ |
662 | need_comma = 0; | |
663 | need_paren = 0; | |
ea192fa3 | 664 | skip_optional = -1; |
252b5132 RH |
665 | for (opindex = opcode->operands; *opindex != 0; opindex++) |
666 | { | |
667 | long value; | |
668 | ||
669 | operand = powerpc_operands + *opindex; | |
670 | ||
671 | /* Operands that are marked FAKE are simply ignored. We | |
672 | already made sure that the extract function considered | |
673 | the instruction to be valid. */ | |
674 | if ((operand->flags & PPC_OPERAND_FAKE) != 0) | |
675 | continue; | |
676 | ||
ea192fa3 PB |
677 | /* If all of the optional operands have the value zero, |
678 | then don't print any of them. */ | |
65b650b4 AM |
679 | if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0) |
680 | { | |
681 | if (skip_optional < 0) | |
682 | skip_optional = skip_optional_operands (opindex, insn, | |
683 | dialect); | |
684 | if (skip_optional) | |
685 | continue; | |
686 | } | |
252b5132 | 687 | |
ea192fa3 PB |
688 | value = operand_value_powerpc (operand, insn, dialect); |
689 | ||
252b5132 RH |
690 | if (need_comma) |
691 | { | |
692 | (*info->fprintf_func) (info->stream, ","); | |
693 | need_comma = 0; | |
694 | } | |
695 | ||
696 | /* Print the operand as directed by the flags. */ | |
fdd12ef3 AM |
697 | if ((operand->flags & PPC_OPERAND_GPR) != 0 |
698 | || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0)) | |
252b5132 RH |
699 | (*info->fprintf_func) (info->stream, "r%ld", value); |
700 | else if ((operand->flags & PPC_OPERAND_FPR) != 0) | |
701 | (*info->fprintf_func) (info->stream, "f%ld", value); | |
786e2c0f C |
702 | else if ((operand->flags & PPC_OPERAND_VR) != 0) |
703 | (*info->fprintf_func) (info->stream, "v%ld", value); | |
9b4e5766 PB |
704 | else if ((operand->flags & PPC_OPERAND_VSR) != 0) |
705 | (*info->fprintf_func) (info->stream, "vs%ld", value); | |
252b5132 RH |
706 | else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0) |
707 | (*info->print_address_func) (memaddr + value, info); | |
708 | else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0) | |
709 | (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info); | |
43e65147 | 710 | else if ((operand->flags & PPC_OPERAND_FSL) != 0) |
081ba1b3 AM |
711 | (*info->fprintf_func) (info->stream, "fsl%ld", value); |
712 | else if ((operand->flags & PPC_OPERAND_FCR) != 0) | |
713 | (*info->fprintf_func) (info->stream, "fcr%ld", value); | |
714 | else if ((operand->flags & PPC_OPERAND_UDI) != 0) | |
715 | (*info->fprintf_func) (info->stream, "%ld", value); | |
b9c361e0 JL |
716 | else if ((operand->flags & PPC_OPERAND_CR_REG) != 0 |
717 | && (((dialect & PPC_OPCODE_PPC) != 0) | |
718 | || ((dialect & PPC_OPCODE_VLE) != 0))) | |
719 | (*info->fprintf_func) (info->stream, "cr%ld", value); | |
720 | else if (((operand->flags & PPC_OPERAND_CR_BIT) != 0) | |
721 | && (((dialect & PPC_OPCODE_PPC) != 0) | |
722 | || ((dialect & PPC_OPCODE_VLE) != 0))) | |
252b5132 | 723 | { |
b9c361e0 JL |
724 | static const char *cbnames[4] = { "lt", "gt", "eq", "so" }; |
725 | int cr; | |
726 | int cc; | |
727 | ||
728 | cr = value >> 2; | |
729 | if (cr != 0) | |
730 | (*info->fprintf_func) (info->stream, "4*cr%d+", cr); | |
731 | cc = value & 3; | |
732 | (*info->fprintf_func) (info->stream, "%s", cbnames[cc]); | |
252b5132 | 733 | } |
70dc4e32 | 734 | else |
d908c8af | 735 | (*info->fprintf_func) (info->stream, "%d", (int) value); |
252b5132 RH |
736 | |
737 | if (need_paren) | |
738 | { | |
739 | (*info->fprintf_func) (info->stream, ")"); | |
740 | need_paren = 0; | |
741 | } | |
742 | ||
743 | if ((operand->flags & PPC_OPERAND_PARENS) == 0) | |
744 | need_comma = 1; | |
745 | else | |
746 | { | |
747 | (*info->fprintf_func) (info->stream, "("); | |
748 | need_paren = 1; | |
749 | } | |
750 | } | |
751 | ||
b9c361e0 JL |
752 | /* We have found and printed an instruction. |
753 | If it was a short VLE instruction we have more to do. */ | |
754 | if (insn_is_short) | |
755 | { | |
756 | memaddr += 2; | |
757 | return 2; | |
758 | } | |
759 | else | |
760 | /* Otherwise, return. */ | |
761 | return 4; | |
252b5132 RH |
762 | } |
763 | ||
764 | /* We could not find a match. */ | |
765 | (*info->fprintf_func) (info->stream, ".long 0x%lx", insn); | |
766 | ||
767 | return 4; | |
768 | } | |
07dd56a9 NC |
769 | |
770 | void | |
823bbe9d | 771 | print_ppc_disassembler_options (FILE *stream) |
07dd56a9 | 772 | { |
69fe9ce5 AM |
773 | unsigned int i, col; |
774 | ||
775 | fprintf (stream, _("\n\ | |
07dd56a9 | 776 | The following PPC specific disassembler options are supported for use with\n\ |
69fe9ce5 AM |
777 | the -M switch:\n")); |
778 | ||
779 | for (col = 0, i = 0; i < sizeof (ppc_opts) / sizeof (ppc_opts[0]); i++) | |
780 | { | |
781 | col += fprintf (stream, " %s,", ppc_opts[i].opt); | |
782 | if (col > 66) | |
783 | { | |
784 | fprintf (stream, "\n"); | |
785 | col = 0; | |
786 | } | |
787 | } | |
788 | fprintf (stream, " 32, 64\n"); | |
07dd56a9 | 789 | } |