home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / opcodes / arm-dis.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  10KB  |  453 lines

  1. /* Instruction printing code for the ARM
  2.    Copyright (C) 1994 Free Software Foundation, Inc. 
  3.    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
  4.  
  5. This file is part of libopcodes. 
  6.  
  7. This program is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your option)
  10. any later version. 
  11.  
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15. more details. 
  16.  
  17. You should have received a copy of the GNU General Public License along with
  18. This program; if not, write to the Free Software Foundation, Inc., 675
  19.  Mass Ave, Boston, MA 02111-1307, USA.  
  20. */
  21.  
  22. #include "dis-asm.h"
  23. #define DEFINE_TABLE
  24. #include "arm-opc.h"
  25.  
  26.  
  27. static char *arm_conditional[] =
  28. {"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
  29.  "hi", "ls", "ge", "lt", "gt", "le", "", "nv"};
  30.  
  31. static char *arm_regnames[] =
  32. {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  33.  "r8", "r9", "sl", "fp", "ip", "sp", "lr", "pc"};
  34.  
  35. static char *arm_fp_const[] =
  36. {"0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0"};
  37.  
  38. static char *arm_shift[] = 
  39. {"lsl", "lsr", "asr", "ror"};
  40.  
  41. static void
  42. arm_decode_shift (given, func, stream)
  43.      long given;
  44.      fprintf_ftype func;
  45.      void *stream;
  46. {
  47.   func (stream, "%s", arm_regnames[given & 0xf]);
  48.   if ((given & 0xff0) != 0)
  49.     {
  50.       if ((given & 0x10) == 0)
  51.     {
  52.       int amount = (given & 0xf80) >> 7;
  53.       int shift = (given & 0x60) >> 5;
  54.       if (amount == 0)
  55.         {
  56.           if (shift == 3)
  57.         {
  58.           func (stream, ", rrx");
  59.           return;
  60.         }
  61.           amount = 32;
  62.         }
  63.       func (stream, ", %s #%x", arm_shift[shift], amount);
  64.     }
  65.       else
  66.     func (stream, ", %s %s", arm_shift[(given & 0x60) >> 5],
  67.           arm_regnames[(given & 0xf00) >> 8]);
  68.     }
  69. }
  70.  
  71. /* Print one instruction from PC on INFO->STREAM.
  72.    Return the size of the instruction (always 4 on ARM). */
  73.  
  74. static int
  75. print_insn_arm (pc, info, given)
  76.      bfd_vma         pc;
  77.      struct disassemble_info *info;
  78.      long given;
  79. {
  80.   struct arm_opcode *insn;
  81.   void *stream = info->stream;
  82.   fprintf_ftype func = info->fprintf_func;
  83.  
  84.   func (stream, "%08x\t", given);
  85.  
  86.   for (insn = arm_opcodes; insn->assembler; insn++)
  87.     {
  88.       if ((given & insn->mask) == insn->value)
  89.     {
  90.       char *c;
  91.       for (c = insn->assembler; *c; c++)
  92.         {
  93.           if (*c == '%')
  94.         {
  95.           switch (*++c)
  96.             {
  97.             case '%':
  98.               func (stream, "%%");
  99.               break;
  100.  
  101.             case 'a':
  102.               if (((given & 0x000f0000) == 0x000f0000)
  103.               && ((given & 0x02000000) == 0))
  104.             {
  105.               int offset = given & 0xfff;
  106.               if ((given & 0x00800000) == 0)
  107.                 offset = -offset;
  108.               (*info->print_address_func)
  109.                 (offset + pc + 8, info);
  110.             }
  111.               else
  112.             {
  113.               func (stream, "[%s", 
  114.                 arm_regnames[(given >> 16) & 0xf]);
  115.               if ((given & 0x01000000) != 0)
  116.                 {
  117.                   if ((given & 0x02000000) == 0)
  118.                 {
  119.                   int offset = given & 0xfff;
  120.                   if (offset)
  121.                     func (stream, ", %s#%x",
  122.                       (((given & 0x00800000) == 0)
  123.                        ? "-" : ""), offset);
  124.                 }
  125.                   else
  126.                 {
  127.                   func (stream, ", %s",
  128.                     (((given & 0x00800000) == 0)
  129.                      ? "-" : ""));
  130.                   arm_decode_shift (given, func, stream);
  131.                 }
  132.  
  133.                   func (stream, "]%s", 
  134.                     ((given & 0x00200000) != 0) ? "!" : "");
  135.                 }
  136.               else
  137.                 {
  138.                   if ((given & 0x02000000) == 0)
  139.                 {
  140.                   int offset = given & 0xfff;
  141.                   if (offset)
  142.                     func (stream, "], %s#%x",
  143.                       (((given & 0x00800000) == 0)
  144.                        ? "-" : ""), offset);
  145.                   else 
  146.                     func (stream, "]");
  147.                 }
  148.                   else
  149.                 {
  150.                   func (stream, "], %s",
  151.                     (((given & 0x00800000) == 0) 
  152.                      ? "-" : ""));
  153.                   arm_decode_shift (given, func, stream);
  154.                 }
  155.                 }
  156.             }
  157.               break;
  158.               
  159.             case 'b':
  160.               (*info->print_address_func)
  161.             (BDISP (given) * 4 + pc + 8, info);
  162.               break;
  163.  
  164.             case 'c':
  165.               func (stream, "%s",
  166.                 arm_conditional [(given >> 28) & 0xf]);
  167.               break;
  168.  
  169.             case 'm':
  170.               {
  171.             int started = 0;
  172.             int reg;
  173.  
  174.             func (stream, "{");
  175.             for (reg = 0; reg < 16; reg++)
  176.               if ((given & (1 << reg)) != 0)
  177.                 {
  178.                   if (started)
  179.                 func (stream, ", ");
  180.                   started = 1;
  181.                   func (stream, "%s", arm_regnames[reg]);
  182.                 }
  183.             func (stream, "}");
  184.               }
  185.               break;
  186.  
  187.             case 'o':
  188.               if ((given & 0x02000000) != 0)
  189.             {
  190.               int rotate = (given & 0xf00) >> 7;
  191.               int immed = (given & 0xff);
  192.               func (stream, "#%x",
  193.                 ((immed << (32 - rotate))
  194.                  | (immed >> rotate)) & 0xffffffff);
  195.             }
  196.               else
  197.             arm_decode_shift (given, func, stream);
  198.               break;
  199.  
  200.             case 'p':
  201.               if ((given & 0x0000f000) == 0x0000f000)
  202.             func (stream, "p");
  203.               break;
  204.  
  205.             case 't':
  206.               if ((given & 0x01200000) == 0x00200000)
  207.             func (stream, "t");
  208.               break;
  209.  
  210.             case 'A':
  211.               func (stream, "[%s", arm_regnames [(given >> 16) & 0xf]);
  212.               if ((given & 0x01000000) != 0)
  213.             {
  214.               int offset = given & 0xff;
  215.               if (offset)
  216.                 func (stream, ", %s#%x]%s",
  217.                   ((given & 0x00800000) == 0 ? "-" : ""),
  218.                   offset * 4,
  219.                   ((given & 0x00200000) != 0 ? "!" : ""));
  220.               else
  221.                 func (stream, "]");
  222.             }
  223.               else
  224.             {
  225.               int offset = given & 0xff;
  226.               if (offset)
  227.                 func (stream, "], %s#%x",
  228.                   ((given & 0x00800000) == 0 ? "-" : ""),
  229.                   offset * 4);
  230.               else
  231.                 func (stream, "]");
  232.             }
  233.               break;
  234.  
  235.             case 'C':
  236.               switch (given & 0x00090000)
  237.             {
  238.             case 0:
  239.               func (stream, "_???");
  240.               break;
  241.             case 0x10000:
  242.               func (stream, "_ctl");
  243.               break;
  244.             case 0x80000:
  245.               func (stream, "_flg");
  246.               break;
  247.             }
  248.               break;
  249.  
  250.             case 'F':
  251.               switch (given & 0x00408000)
  252.             {
  253.             case 0:
  254.               func (stream, "4");
  255.               break;
  256.             case 0x8000:
  257.               func (stream, "1");
  258.               break;
  259.             case 0x00400000:
  260.               func (stream, "2");
  261.               break;
  262.             default:
  263.               func (stream, "3");
  264.             }
  265.               break;
  266.             
  267.             case 'P':
  268.               switch (given & 0x00080080)
  269.             {
  270.             case 0:
  271.               func (stream, "s");
  272.               break;
  273.             case 0x80:
  274.               func (stream, "d");
  275.               break;
  276.             case 0x00080000:
  277.               func (stream, "e");
  278.               break;
  279.             default:
  280.               func (stream, "<illegal precision>");
  281.               break;
  282.             }
  283.               break;
  284.             case 'Q':
  285.               switch (given & 0x00408000)
  286.             {
  287.             case 0:
  288.               func (stream, "s");
  289.               break;
  290.             case 0x8000:
  291.               func (stream, "d");
  292.               break;
  293.             case 0x00400000:
  294.               func (stream, "e");
  295.               break;
  296.             default:
  297.               func (stream, "p");
  298.               break;
  299.             }
  300.               break;
  301.             case 'R':
  302.               switch (given & 0x60)
  303.             {
  304.             case 0:
  305.               break;
  306.             case 0x20:
  307.               func (stream, "p");
  308.               break;
  309.             case 0x40:
  310.               func (stream, "m");
  311.               break;
  312.             default:
  313.               func (stream, "z");
  314.               break;
  315.             }
  316.               break;
  317.  
  318.             case '0': case '1': case '2': case '3': case '4': 
  319.             case '5': case '6': case '7': case '8': case '9':
  320.               {
  321.             int bitstart = *c++ - '0';
  322.             int bitend = 0;
  323.             while (*c >= '0' && *c <= '9')
  324.               bitstart = (bitstart * 10) + *c++ - '0';
  325.  
  326.             switch (*c)
  327.               {
  328.               case '-':
  329.                 c++;
  330.                 while (*c >= '0' && *c <= '9')
  331.                   bitend = (bitend * 10) + *c++ - '0';
  332.                 if (!bitend)
  333.                   abort ();
  334.                 switch (*c)
  335.                   {
  336.                   case 'r':
  337.                 {
  338.                   long reg;
  339.                   reg = given >> bitstart;
  340.                   reg &= (2 << (bitend - bitstart)) - 1;
  341.                   func (stream, "%s", arm_regnames[reg]);
  342.                 }
  343.                 break;
  344.                   case 'd':
  345.                 {
  346.                   long reg;
  347.                   reg = given >> bitstart;
  348.                   reg &= (2 << (bitend - bitstart)) - 1;
  349.                   func (stream, "%d", reg);
  350.                 }
  351.                 break;
  352.                   case 'x':
  353.                 {
  354.                   long reg;
  355.                   reg = given >> bitstart;
  356.                   reg &= (2 << (bitend - bitstart)) - 1;
  357.                   func (stream, "0x%08x", reg);
  358.                 }
  359.                 break;
  360.                   case 'f':
  361.                 {
  362.                   long reg;
  363.                   reg = given >> bitstart;
  364.                   reg &= (2 << (bitend - bitstart)) - 1;
  365.                   if (reg > 7)
  366.                     func (stream, "#%s",
  367.                       arm_fp_const[reg & 7]);
  368.                   else
  369.                     func (stream, "f%d", reg);
  370.                 }
  371.                 break;
  372.                   default:
  373.                 abort ();
  374.                   }
  375.                 break;
  376.               case '`':
  377.                 c++;
  378.                 if ((given & (1 << bitstart)) == 0)
  379.                   func (stream, "%c", *c);
  380.                 break;
  381.               case '\'':
  382.                 c++;
  383.                 if ((given & (1 << bitstart)) != 0)
  384.                   func (stream, "%c", *c);
  385.                 break;
  386.               case '?':
  387.                 ++c;
  388.                 if ((given & (1 << bitstart)) != 0)
  389.                   func (stream, "%c", *c++);
  390.                 else
  391.                   func (stream, "%c", *++c);
  392.                 break;
  393.               default:
  394.                 abort ();
  395.               }
  396.             break;
  397.  
  398.               default:
  399.             abort ();
  400.               }
  401.             }
  402.         }
  403.           else
  404.         func (stream, "%c", *c);
  405.         }
  406.       return 4;
  407.     }
  408.     }
  409.   abort ();
  410. }
  411.  
  412. int
  413. print_insn_big_arm (pc, info)
  414.      bfd_vma pc;
  415.      struct disassemble_info *info;
  416. {
  417.   unsigned char b[4];
  418.   long given;
  419.   int status;
  420.  
  421.   status = (*info->read_memory_func) (pc, (bfd_byte *) &b[0], 4, info);
  422.   if (status != 0)
  423.     {
  424.       (*info->memory_error_func) (status, pc, info);
  425.       return -1;
  426.     }
  427.  
  428.   given = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
  429.  
  430.   return print_insn_arm (pc, info, given);
  431. }
  432.  
  433. int
  434. print_insn_little_arm (pc, info)
  435.      bfd_vma pc;
  436.      struct disassemble_info *info;
  437. {
  438.   unsigned char b[4];
  439.   long given;
  440.   int status;
  441.  
  442.   status = (*info->read_memory_func) (pc, (bfd_byte *) &b[0], 4, info);
  443.   if (status != 0)
  444.     {
  445.       (*info->memory_error_func) (status, pc, info);
  446.       return -1;
  447.     }
  448.  
  449.   given = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
  450.  
  451.   return print_insn_arm (pc, info, given);
  452. }
  453.