ppc-dis.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* ppc-dis.c -- Disassemble PowerPC instructions
  2.    Copyright 1994 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support
  4. This file is part of GDB, GAS, and the GNU binutils.
  5. GDB, GAS, and the GNU binutils are free software; you can redistribute
  6. them and/or modify them under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either version
  8. 2, or (at your option) any later version.
  9. GDB, GAS, and the GNU binutils are distributed in the hope that they
  10. will be useful, but WITHOUT ANY WARRANTY; without even the implied
  11. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
  12. the GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this file; see the file COPYING.  If not, write to the Free
  15. Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  16. #include "nonstdio.h"
  17. #include "ansidecl.h"
  18. #include "ppc.h"
  19. static int print_insn_powerpc PARAMS ((FILE *, unsigned long insn,
  20.        unsigned long memaddr, int dialect));
  21. extern void print_address PARAMS((unsigned long memaddr));
  22. /* Print a big endian PowerPC instruction.  For convenience, also
  23.    disassemble instructions supported by the Motorola PowerPC 601.  */
  24. int
  25. print_insn_big_powerpc (FILE *out, unsigned long insn, unsigned long memaddr)
  26. {
  27.   return print_insn_powerpc (out, insn, memaddr,
  28.      PPC_OPCODE_PPC | PPC_OPCODE_601);
  29. }
  30. /* Print a PowerPC or POWER instruction.  */
  31. static int
  32. print_insn_powerpc (FILE *out, unsigned long insn, unsigned long memaddr,
  33.     int dialect)
  34. {
  35.   const struct powerpc_opcode *opcode;
  36.   const struct powerpc_opcode *opcode_end;
  37.   unsigned long op;
  38.   /* Get the major opcode of the instruction.  */
  39.   op = PPC_OP (insn);
  40.   /* Find the first match in the opcode table.  We could speed this up
  41.      a bit by doing a binary search on the major opcode.  */
  42.   opcode_end = powerpc_opcodes + powerpc_num_opcodes;
  43.   for (opcode = powerpc_opcodes; opcode < opcode_end; opcode++)
  44.     {
  45.       unsigned long table_op;
  46.       const unsigned char *opindex;
  47.       const struct powerpc_operand *operand;
  48.       int invalid;
  49.       int need_comma;
  50.       int need_paren;
  51.       table_op = PPC_OP (opcode->opcode);
  52.       if (op < table_op)
  53. break;
  54.       if (op > table_op)
  55. continue;
  56.       if ((insn & opcode->mask) != opcode->opcode
  57.   || (opcode->flags & dialect) == 0)
  58. continue;
  59.       /* Make two passes over the operands.  First see if any of them
  60.  have extraction functions, and, if they do, make sure the
  61.  instruction is valid.  */
  62.       invalid = 0;
  63.       for (opindex = opcode->operands; *opindex != 0; opindex++)
  64. {
  65.   operand = powerpc_operands + *opindex;
  66.   if (operand->extract)
  67.     (*operand->extract) (insn, &invalid);
  68. }
  69.       if (invalid)
  70. continue;
  71.       /* The instruction is valid.  */
  72.       fprintf(out, "%s", opcode->name);
  73.       if (opcode->operands[0] != 0)
  74. fprintf(out, "t");
  75.       /* Now extract and print the operands.  */
  76.       need_comma = 0;
  77.       need_paren = 0;
  78.       for (opindex = opcode->operands; *opindex != 0; opindex++)
  79. {
  80.   long value;
  81.   operand = powerpc_operands + *opindex;
  82.   /* Operands that are marked FAKE are simply ignored.  We
  83.      already made sure that the extract function considered
  84.      the instruction to be valid.  */
  85.   if ((operand->flags & PPC_OPERAND_FAKE) != 0)
  86.     continue;
  87.   /* Extract the value from the instruction.  */
  88.   if (operand->extract)
  89.     value = (*operand->extract) (insn, (int *) 0);
  90.   else
  91.     {
  92.       value = (insn >> operand->shift) & ((1 << operand->bits) - 1);
  93.       if ((operand->flags & PPC_OPERAND_SIGNED) != 0
  94.   && (value & (1 << (operand->bits - 1))) != 0)
  95. value -= 1 << operand->bits;
  96.     }
  97.   /* If the operand is optional, and the value is zero, don't
  98.      print anything.  */
  99.   if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
  100.       && (operand->flags & PPC_OPERAND_NEXT) == 0
  101.       && value == 0)
  102.     continue;
  103.   if (need_comma)
  104.     {
  105.       fprintf(out, ",");
  106.       need_comma = 0;
  107.     }
  108.   /* Print the operand as directed by the flags.  */
  109.   if ((operand->flags & PPC_OPERAND_GPR) != 0)
  110.     fprintf(out, "r%ld", value);
  111.   else if ((operand->flags & PPC_OPERAND_FPR) != 0)
  112.     fprintf(out, "f%ld", value);
  113.   else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
  114.     print_address (memaddr + value);
  115.   else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
  116.     print_address (value & 0xffffffff);
  117.   else if ((operand->flags & PPC_OPERAND_CR) == 0
  118.    || (dialect & PPC_OPCODE_PPC) == 0)
  119.     fprintf(out, "%ld", value);
  120.   else
  121.     {
  122.       if (operand->bits == 3)
  123. fprintf(out, "cr%d", value);
  124.       else
  125. {
  126.   static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
  127.   int cr;
  128.   int cc;
  129.   cr = value >> 2;
  130.   if (cr != 0)
  131.     fprintf(out, "4*cr%d", cr);
  132.   cc = value & 3;
  133.   if (cc != 0)
  134.     {
  135.       if (cr != 0)
  136. fprintf(out, "+");
  137.       fprintf(out, "%s", cbnames[cc]);
  138.     }
  139. }
  140.     }
  141.   if (need_paren)
  142.     {
  143.       fprintf(out, ")");
  144.       need_paren = 0;
  145.     }
  146.   if ((operand->flags & PPC_OPERAND_PARENS) == 0)
  147.     need_comma = 1;
  148.   else
  149.     {
  150.       fprintf(out, "(");
  151.       need_paren = 1;
  152.     }
  153. }
  154.       /* We have found and printed an instruction; return.  */
  155.       return 4;
  156.     }
  157.   /* We could not find a match.  */
  158.   fprintf(out, ".long 0x%lx", insn);
  159.   return 4;
  160. }