include/
[deliverable/binutils-gdb.git] / opcodes / ppc-dis.c
1 /* ppc-dis.c -- Disassemble PowerPC instructions
2 Copyright 1994, 1995, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support
5
6 This file is part of the GNU opcodes library.
7
8 This library 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, or (at your option)
11 any later version.
12
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this file; see the file COPYING. If not, write to the
20 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include <stdio.h>
24 #include "sysdep.h"
25 #include "dis-asm.h"
26 #include "opintl.h"
27 #include "opcode/ppc.h"
28
29 /* This file provides several disassembler functions, all of which use
30 the disassembler interface defined in dis-asm.h. Several functions
31 are provided because this file handles disassembly for the PowerPC
32 in both big and little endian mode and also for the POWER (RS/6000)
33 chip. */
34 static int print_insn_powerpc (bfd_vma, struct disassemble_info *, int,
35 ppc_cpu_t);
36
37 struct dis_private
38 {
39 /* Stash the result of parsing disassembler_options here. */
40 ppc_cpu_t dialect;
41 };
42
43 #define POWERPC_DIALECT(INFO) \
44 (((struct dis_private *) ((INFO)->private_data))->dialect)
45
46 struct ppc_mopt {
47 const char *opt;
48 ppc_cpu_t cpu;
49 ppc_cpu_t sticky;
50 };
51
52 struct ppc_mopt ppc_opts[] = {
53 { "403", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_403
54 | PPC_OPCODE_32),
55 0 },
56 { "405", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_403
57 | PPC_OPCODE_405 | PPC_OPCODE_32),
58 0 },
59 { "440", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_32
60 | PPC_OPCODE_440 | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI),
61 0 },
62 { "464", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_32
63 | PPC_OPCODE_440 | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI),
64 0 },
65 { "476", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ISEL
66 | PPC_OPCODE_440 | PPC_OPCODE_476 | PPC_OPCODE_POWER4
67 | PPC_OPCODE_POWER5),
68 0 },
69 { "601", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_601
70 | PPC_OPCODE_32),
71 0 },
72 { "603", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_32),
73 0 },
74 { "604", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_32),
75 0 },
76 { "620", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64),
77 0 },
78 { "7400", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ALTIVEC
79 | PPC_OPCODE_32),
80 0 },
81 { "7410", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ALTIVEC
82 | PPC_OPCODE_32),
83 0 },
84 { "7450", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ALTIVEC
85 | PPC_OPCODE_32),
86 0 },
87 { "7455", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ALTIVEC
88 | PPC_OPCODE_32),
89 0 },
90 { "750cl", (PPC_OPCODE_PPC | PPC_OPCODE_PPCPS)
91 , 0 },
92 { "altivec", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC),
93 PPC_OPCODE_ALTIVEC },
94 { "any", 0,
95 PPC_OPCODE_ANY },
96 { "booke", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_32),
97 0 },
98 { "booke32", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_32),
99 0 },
100 { "cell", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64
101 | PPC_OPCODE_POWER4 | PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC),
102 0 },
103 { "com", (PPC_OPCODE_COMMON | PPC_OPCODE_32),
104 0 },
105 { "e300", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_32
106 | PPC_OPCODE_E300),
107 0 },
108 { "e500", (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_E500MC),
112 0 },
113 { "e500mc", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
114 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
115 | PPC_OPCODE_E500MC),
116 0 },
117 { "e500mc64", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
118 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
119 | PPC_OPCODE_64 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
120 | PPC_OPCODE_POWER7),
121 0 },
122 { "e500x2", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE
123 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
124 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
125 | PPC_OPCODE_E500MC),
126 0 },
127 { "efs", (PPC_OPCODE_PPC | PPC_OPCODE_EFS),
128 0 },
129 { "power4", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64
130 | PPC_OPCODE_POWER4),
131 0 },
132 { "power5", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64
133 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5),
134 0 },
135 { "power6", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64
136 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
137 | PPC_OPCODE_ALTIVEC),
138 0 },
139 { "power7", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ISEL
140 | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5
141 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC
142 | PPC_OPCODE_VSX),
143 0 },
144 { "ppc", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_32),
145 0 },
146 { "ppc32", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_32),
147 0 },
148 { "ppc64", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64),
149 0 },
150 { "ppc64bridge", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_64_BRIDGE
151 | PPC_OPCODE_64),
152 0 },
153 { "a2", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC | PPC_OPCODE_ISEL
154 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_CACHELCK
155 | PPC_OPCODE_64 | PPC_OPCODE_A2),
156 0 },
157 { "ppcps", (PPC_OPCODE_PPC | PPC_OPCODE_PPCPS),
158 0 },
159 { "pwr", (PPC_OPCODE_POWER | PPC_OPCODE_32),
160 0 },
161 { "pwr2", (PPC_OPCODE_POWER | PPC_OPCODE_POWER2 | PPC_OPCODE_32),
162 0 },
163 { "pwrx", (PPC_OPCODE_POWER | PPC_OPCODE_POWER2 | PPC_OPCODE_32),
164 0 },
165 { "spe", (PPC_OPCODE_PPC | PPC_OPCODE_EFS),
166 PPC_OPCODE_SPE },
167 { "titan", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_32
168 | PPC_OPCODE_PMR | PPC_OPCODE_RFMCI | PPC_OPCODE_TITAN),
169 0 },
170 { "vsx", (PPC_OPCODE_PPC | PPC_OPCODE_CLASSIC),
171 PPC_OPCODE_VSX },
172 };
173
174 /* Handle -m and -M options that set cpu type, and .machine arg. */
175
176 ppc_cpu_t
177 ppc_parse_cpu (ppc_cpu_t ppc_cpu, const char *arg)
178 {
179 /* Sticky bits. */
180 ppc_cpu_t retain_flags = ppc_cpu & (PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX
181 | PPC_OPCODE_SPE | PPC_OPCODE_ANY);
182 unsigned int i;
183
184 for (i = 0; i < sizeof (ppc_opts) / sizeof (ppc_opts[0]); i++)
185 if (strcmp (ppc_opts[i].opt, arg) == 0)
186 {
187 if (ppc_opts[i].sticky)
188 {
189 retain_flags |= ppc_opts[i].sticky;
190 if ((ppc_cpu & ~(PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX
191 | PPC_OPCODE_SPE | PPC_OPCODE_ANY)) != 0)
192 break;
193 }
194 ppc_cpu = ppc_opts[i].cpu;
195 break;
196 }
197 if (i >= sizeof (ppc_opts) / sizeof (ppc_opts[0]))
198 return 0;
199
200 ppc_cpu |= retain_flags;
201 return ppc_cpu;
202 }
203
204 /* Determine which set of machines to disassemble for. */
205
206 static int
207 powerpc_init_dialect (struct disassemble_info *info)
208 {
209 ppc_cpu_t dialect = 0;
210 char *arg;
211 struct dis_private *priv = calloc (sizeof (*priv), 1);
212
213 if (priv == NULL)
214 return FALSE;
215
216 arg = info->disassembler_options;
217 while (arg != NULL)
218 {
219 ppc_cpu_t new_cpu = 0;
220 char *end = strchr (arg, ',');
221
222 if (end != NULL)
223 *end = 0;
224
225 if ((new_cpu = ppc_parse_cpu (dialect, arg)) != 0)
226 dialect = new_cpu;
227 else if (strcmp (arg, "32") == 0)
228 {
229 dialect &= ~PPC_OPCODE_64;
230 dialect |= PPC_OPCODE_32;
231 }
232 else if (strcmp (arg, "64") == 0)
233 {
234 dialect |= PPC_OPCODE_64;
235 dialect &= ~PPC_OPCODE_32;
236 }
237 else
238 fprintf (stderr, _("warning: ignoring unknown -M%s option\n"), arg);
239
240 if (end != NULL)
241 *end++ = ',';
242 arg = end;
243 }
244
245 if ((dialect & ~(PPC_OPCODE_32 | PPC_OPCODE_64)) == 0)
246 {
247 if (info->mach == bfd_mach_ppc64)
248 dialect |= PPC_OPCODE_64;
249 else
250 dialect |= PPC_OPCODE_32;
251 /* Choose a reasonable default. */
252 dialect |= (PPC_OPCODE_PPC | PPC_OPCODE_COMMON | PPC_OPCODE_CLASSIC
253 | PPC_OPCODE_601 | PPC_OPCODE_ALTIVEC);
254 }
255
256 info->private_data = priv;
257 POWERPC_DIALECT(info) = dialect;
258
259 return TRUE;
260 }
261
262 /* Print a big endian PowerPC instruction. */
263
264 int
265 print_insn_big_powerpc (bfd_vma memaddr, struct disassemble_info *info)
266 {
267 if (info->private_data == NULL && !powerpc_init_dialect (info))
268 return -1;
269 return print_insn_powerpc (memaddr, info, 1, POWERPC_DIALECT(info));
270 }
271
272 /* Print a little endian PowerPC instruction. */
273
274 int
275 print_insn_little_powerpc (bfd_vma memaddr, struct disassemble_info *info)
276 {
277 if (info->private_data == NULL && !powerpc_init_dialect (info))
278 return -1;
279 return print_insn_powerpc (memaddr, info, 0, POWERPC_DIALECT(info));
280 }
281
282 /* Print a POWER (RS/6000) instruction. */
283
284 int
285 print_insn_rs6000 (bfd_vma memaddr, struct disassemble_info *info)
286 {
287 return print_insn_powerpc (memaddr, info, 1, PPC_OPCODE_POWER);
288 }
289
290 /* Extract the operand value from the PowerPC or POWER instruction. */
291
292 static long
293 operand_value_powerpc (const struct powerpc_operand *operand,
294 unsigned long insn, ppc_cpu_t dialect)
295 {
296 long value;
297 int invalid;
298 /* Extract the value from the instruction. */
299 if (operand->extract)
300 value = (*operand->extract) (insn, dialect, &invalid);
301 else
302 {
303 value = (insn >> operand->shift) & operand->bitm;
304 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
305 {
306 /* BITM is always some number of zeros followed by some
307 number of ones, followed by some numer of zeros. */
308 unsigned long top = operand->bitm;
309 /* top & -top gives the rightmost 1 bit, so this
310 fills in any trailing zeros. */
311 top |= (top & -top) - 1;
312 top &= ~(top >> 1);
313 value = (value ^ top) - top;
314 }
315 }
316
317 return value;
318 }
319
320 /* Determine whether the optional operand(s) should be printed. */
321
322 static int
323 skip_optional_operands (const unsigned char *opindex,
324 unsigned long insn, ppc_cpu_t dialect)
325 {
326 const struct powerpc_operand *operand;
327
328 for (; *opindex != 0; opindex++)
329 {
330 operand = &powerpc_operands[*opindex];
331 if ((operand->flags & PPC_OPERAND_NEXT) != 0
332 || ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
333 && operand_value_powerpc (operand, insn, dialect) != 0))
334 return 0;
335 }
336
337 return 1;
338 }
339
340 /* Print a PowerPC or POWER instruction. */
341
342 static int
343 print_insn_powerpc (bfd_vma memaddr,
344 struct disassemble_info *info,
345 int bigendian,
346 ppc_cpu_t dialect)
347 {
348 bfd_byte buffer[4];
349 int status;
350 unsigned long insn;
351 const struct powerpc_opcode *opcode;
352 const struct powerpc_opcode *opcode_end;
353 unsigned long op;
354 ppc_cpu_t dialect_orig = dialect;
355
356 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
357 if (status != 0)
358 {
359 (*info->memory_error_func) (status, memaddr, info);
360 return -1;
361 }
362
363 if (bigendian)
364 insn = bfd_getb32 (buffer);
365 else
366 insn = bfd_getl32 (buffer);
367
368 /* Get the major opcode of the instruction. */
369 op = PPC_OP (insn);
370
371 /* Find the first match in the opcode table. We could speed this up
372 a bit by doing a binary search on the major opcode. */
373 opcode_end = powerpc_opcodes + powerpc_num_opcodes;
374 again:
375 for (opcode = powerpc_opcodes; opcode < opcode_end; opcode++)
376 {
377 unsigned long table_op;
378 const unsigned char *opindex;
379 const struct powerpc_operand *operand;
380 int invalid;
381 int need_comma;
382 int need_paren;
383 int skip_optional;
384
385 table_op = PPC_OP (opcode->opcode);
386 if (op < table_op)
387 break;
388 if (op > table_op)
389 continue;
390
391 if ((insn & opcode->mask) != opcode->opcode
392 || (opcode->flags & dialect) == 0
393 || (opcode->deprecated & dialect_orig) != 0)
394 continue;
395
396 /* Make two passes over the operands. First see if any of them
397 have extraction functions, and, if they do, make sure the
398 instruction is valid. */
399 invalid = 0;
400 for (opindex = opcode->operands; *opindex != 0; opindex++)
401 {
402 operand = powerpc_operands + *opindex;
403 if (operand->extract)
404 (*operand->extract) (insn, dialect, &invalid);
405 }
406 if (invalid)
407 continue;
408
409 /* The instruction is valid. */
410 if (opcode->operands[0] != 0)
411 (*info->fprintf_func) (info->stream, "%-7s ", opcode->name);
412 else
413 (*info->fprintf_func) (info->stream, "%s", opcode->name);
414
415 /* Now extract and print the operands. */
416 need_comma = 0;
417 need_paren = 0;
418 skip_optional = -1;
419 for (opindex = opcode->operands; *opindex != 0; opindex++)
420 {
421 long value;
422
423 operand = powerpc_operands + *opindex;
424
425 /* Operands that are marked FAKE are simply ignored. We
426 already made sure that the extract function considered
427 the instruction to be valid. */
428 if ((operand->flags & PPC_OPERAND_FAKE) != 0)
429 continue;
430
431 /* If all of the optional operands have the value zero,
432 then don't print any of them. */
433 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
434 {
435 if (skip_optional < 0)
436 skip_optional = skip_optional_operands (opindex, insn,
437 dialect);
438 if (skip_optional)
439 continue;
440 }
441
442 value = operand_value_powerpc (operand, insn, dialect);
443
444 if (need_comma)
445 {
446 (*info->fprintf_func) (info->stream, ",");
447 need_comma = 0;
448 }
449
450 /* Print the operand as directed by the flags. */
451 if ((operand->flags & PPC_OPERAND_GPR) != 0
452 || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0))
453 (*info->fprintf_func) (info->stream, "r%ld", value);
454 else if ((operand->flags & PPC_OPERAND_FPR) != 0)
455 (*info->fprintf_func) (info->stream, "f%ld", value);
456 else if ((operand->flags & PPC_OPERAND_VR) != 0)
457 (*info->fprintf_func) (info->stream, "v%ld", value);
458 else if ((operand->flags & PPC_OPERAND_VSR) != 0)
459 (*info->fprintf_func) (info->stream, "vs%ld", value);
460 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
461 (*info->print_address_func) (memaddr + value, info);
462 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
463 (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info);
464 else if ((operand->flags & PPC_OPERAND_FSL) != 0)
465 (*info->fprintf_func) (info->stream, "fsl%ld", value);
466 else if ((operand->flags & PPC_OPERAND_FCR) != 0)
467 (*info->fprintf_func) (info->stream, "fcr%ld", value);
468 else if ((operand->flags & PPC_OPERAND_UDI) != 0)
469 (*info->fprintf_func) (info->stream, "%ld", value);
470 else if ((operand->flags & PPC_OPERAND_CR) != 0
471 && (dialect & PPC_OPCODE_PPC) != 0)
472 {
473 if (operand->bitm == 7)
474 (*info->fprintf_func) (info->stream, "cr%ld", value);
475 else
476 {
477 static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
478 int cr;
479 int cc;
480
481 cr = value >> 2;
482 if (cr != 0)
483 (*info->fprintf_func) (info->stream, "4*cr%d+", cr);
484 cc = value & 3;
485 (*info->fprintf_func) (info->stream, "%s", cbnames[cc]);
486 }
487 }
488 else
489 (*info->fprintf_func) (info->stream, "%ld", value);
490
491 if (need_paren)
492 {
493 (*info->fprintf_func) (info->stream, ")");
494 need_paren = 0;
495 }
496
497 if ((operand->flags & PPC_OPERAND_PARENS) == 0)
498 need_comma = 1;
499 else
500 {
501 (*info->fprintf_func) (info->stream, "(");
502 need_paren = 1;
503 }
504 }
505
506 /* We have found and printed an instruction; return. */
507 return 4;
508 }
509
510 if ((dialect & PPC_OPCODE_ANY) != 0)
511 {
512 dialect = ~PPC_OPCODE_ANY;
513 goto again;
514 }
515
516 /* We could not find a match. */
517 (*info->fprintf_func) (info->stream, ".long 0x%lx", insn);
518
519 return 4;
520 }
521
522 void
523 print_ppc_disassembler_options (FILE *stream)
524 {
525 unsigned int i, col;
526
527 fprintf (stream, _("\n\
528 The following PPC specific disassembler options are supported for use with\n\
529 the -M switch:\n"));
530
531 for (col = 0, i = 0; i < sizeof (ppc_opts) / sizeof (ppc_opts[0]); i++)
532 {
533 col += fprintf (stream, " %s,", ppc_opts[i].opt);
534 if (col > 66)
535 {
536 fprintf (stream, "\n");
537 col = 0;
538 }
539 }
540 fprintf (stream, " 32, 64\n");
541 }
This page took 0.041877 seconds and 5 git commands to generate.