2005-03-20 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / opcodes / avr-dis.c
index ee91d7f223c6b609609370b76f353886cba7f631..95ebf1805e8bebaa043e8f4809d023d8d9dd890c 100644 (file)
@@ -1,5 +1,5 @@
 /* Disassemble AVR instructions.
-   Copyright (C) 1999, 2000 Free Software Foundation, Inc.
+   Copyright 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
 
    Contributed by Denis Chertykov <denisc@overta.ru>
 
@@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "sysdep.h"
 #include "dis-asm.h"
 #include "opintl.h"
-
+#include "libiberty.h"
 
 struct avr_opcodes_s
 {
@@ -31,33 +31,27 @@ struct avr_opcodes_s
   int insn_size;               /* in words */
   int isa;
   unsigned int bin_opcode;
-  unsigned int bin_mask;
 };
 
 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
-{#NAME, CONSTR, OPCODE, SIZE, ISA, BIN, 0},
+{#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
 
-struct avr_opcodes_s avr_opcodes[] =
+const struct avr_opcodes_s avr_opcodes[] =
 {
   #include "opcode/avr.h"
-  {NULL, NULL, NULL, 0, 0, 0, 0}
+  {NULL, NULL, NULL, 0, 0, 0}
 };
 
+static int avr_operand (unsigned int, unsigned int, unsigned int, int,
+                        char *, char *, int, int *, bfd_vma *);
 
-static void avr_operand (unsigned int insn, unsigned int insn2,
-                        unsigned int pc, int constraint, char *buf,
-                        char *comment, int regs);
-
-static void
-avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
-     unsigned int insn;
-     unsigned int insn2;
-     unsigned int pc;
-     int constraint;
-     char *buf;
-     char *comment;
-     int regs;
+static int
+avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
+             char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
 {
+  int ok = 1;
+  *sym = 0;
+
   switch (constraint)
     {
       /* Any register operand.  */
@@ -96,18 +90,27 @@ avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
       break;
 
     case 'e':
-      if (insn & 0x2)
-       *buf++ = '-';
-      switch ((insn >> 2) & 0x3)
-       {
-       case 0: *buf++ = 'Z'; break;
-       case 2: *buf++ = 'Y'; break;
-       case 3: *buf++ = 'X'; break;
-       default: buf += sprintf (buf, _(" unknown register ")); break;
-       }
-      if (insn & 0x1)
-       *buf++ = '+';
-      *buf = '\0';
+      {
+       char *xyz;
+
+       switch (insn & 0x100f)
+         {
+           case 0x0000: xyz = "Z";  break;
+           case 0x1001: xyz = "Z+"; break;
+           case 0x1002: xyz = "-Z"; break;
+           case 0x0008: xyz = "Y";  break;
+           case 0x1009: xyz = "Y+"; break;
+           case 0x100a: xyz = "-Y"; break;
+           case 0x100c: xyz = "X";  break;
+           case 0x100d: xyz = "X+"; break;
+           case 0x100e: xyz = "-X"; break;
+           default: xyz = "??"; ok = 0;
+         }
+       sprintf (buf, xyz);
+
+       if (AVR_UNDEF_P (insn))
+         sprintf (comment, _("undefined"));
+      }
       break;
 
     case 'z':
@@ -115,11 +118,13 @@ avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
       if (insn & 0x1)
        *buf++ = '+';
       *buf = '\0';
+      if (AVR_UNDEF_P (insn))
+       sprintf (comment, _("undefined"));
       break;
 
     case 'b':
       {
-       unsigned int x = insn;
+       unsigned int x;
        
        x = (insn & 7);
        x |= (insn >> 7) & (3 << 3);
@@ -135,15 +140,18 @@ avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
       break;
       
     case 'h':
-      sprintf (buf, "0x%x",
-              ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2);
+      *sym = 1;
+      *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
+      sprintf (buf, "0x");
       break;
       
     case 'L':
       {
        int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
        sprintf (buf, ".%+-8d", rel_addr);
-       sprintf (comment, "0x%x", pc + 2 + rel_addr);
+        *sym = 1;
+        *sym_addr = pc + 2 + rel_addr;
+       sprintf (comment, "0x");
       }
       break;
 
@@ -151,7 +159,9 @@ avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
       {
        int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
        sprintf (buf, ".%+-8d", rel_addr);
-       sprintf (comment, "0x%x", pc + 2 + rel_addr);
+        *sym = 1;
+        *sym_addr = pc + 2 + rel_addr;
+       sprintf (comment, "0x");
       }
       break;
 
@@ -165,11 +175,19 @@ avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
       break;
 
     case 'n':
-      sprintf (buf, _("Internal disassembler error"));
+      sprintf (buf, "??");
+      fprintf (stderr, _("Internal disassembler error"));
+      ok = 0;
       break;
       
     case 'K':
-      sprintf (buf, "%d", (insn & 0xf) | ((insn >> 2) & 0x30));
+      {
+       unsigned int x;
+
+       x = (insn & 0xf) | ((insn >> 2) & 0x30);
+       sprintf (buf, "0x%02x", x);
+       sprintf (comment, "%d", x);
+      }
       break;
       
     case 's':
@@ -205,8 +223,12 @@ avr_operand (insn, insn2, pc, constraint, buf, comment, regs)
       break;
       
     default:
-      sprintf (buf, _("unknown constraint `%c'"), constraint);
+      sprintf (buf, "??");
+      fprintf (stderr, _("unknown constraint `%c'"), constraint);
+      ok = 0;
     }
+
+    return ok;
 }
 
 static unsigned short avrdis_opcode PARAMS ((bfd_vma, disassemble_info *));
@@ -234,17 +256,30 @@ print_insn_avr(addr, info)
      disassemble_info *info;
 {
   unsigned int insn, insn2;
-  struct avr_opcodes_s *opcode;
+  const struct avr_opcodes_s *opcode;
+  static unsigned int *maskptr;
   void *stream = info->stream;
   fprintf_ftype prin = info->fprintf_func;
+  static unsigned int *avr_bin_masks;
   static int initialized;
   int cmd_len = 2;
+  int ok = 0;
+  char op1[20], op2[20], comment1[40], comment2[40];
+  int sym_op1 = 0, sym_op2 = 0;
+  bfd_vma sym_addr1, sym_addr2;
 
   if (!initialized)
     {
-      initialized = 1;
+      unsigned int nopcodes;
+
+      nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
       
-      for (opcode = avr_opcodes; opcode->name; opcode++)
+      avr_bin_masks = (unsigned int *)
+       xmalloc (nopcodes * sizeof (unsigned int));
+
+      for (opcode = avr_opcodes, maskptr = avr_bin_masks;
+          opcode->name;
+          opcode++, maskptr++)
        {
          char * s;
          unsigned int bin = 0;
@@ -259,28 +294,39 @@ print_insn_avr(addr, info)
            }
          assert (s - opcode->opcode == 16);
          assert (opcode->bin_opcode == bin);
-         opcode->bin_mask = mask;
+         *maskptr = mask;
        }
+
+      initialized = 1;
     }
 
   insn = avrdis_opcode (addr, info);
   
-  for (opcode = avr_opcodes; opcode->name; opcode++)
+  for (opcode = avr_opcodes, maskptr = avr_bin_masks;
+       opcode->name;
+       opcode++, maskptr++)
     {
-      if ((insn & opcode->bin_mask) == opcode->bin_opcode)
+      if ((insn & *maskptr) == opcode->bin_opcode)
        break;
     }
   
+  /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
+     `std b+0,r' as `st b,r' (next entry in the table).  */
+
+  if (AVR_DISP0_P (insn))
+    opcode++;
+
+  op1[0] = 0;
+  op2[0] = 0;
+  comment1[0] = 0;
+  comment2[0] = 0;
+
   if (opcode->name)
     {
-      char op1[20], op2[20], comment1[40], comment2[40];
       char *op = opcode->constraints;
 
-      op1[0] = 0;
-      op2[0] = 0;
-      comment1[0] = 0;
-      comment2[0] = 0;
       insn2 = 0;
+      ok = 1;
 
       if (opcode->insn_size > 1)
        {
@@ -292,29 +338,42 @@ print_insn_avr(addr, info)
        {
          int regs = REGISTER_P (*op);
 
-         avr_operand (insn, insn2, addr, *op, op1, comment1, 0);
+         ok = avr_operand (insn, insn2, addr, *op, op1, comment1, 0, &sym_op1, &sym_addr1);
 
-         if (*(++op) == ',')
-           avr_operand (insn, insn2, addr, *(++op), op2,
-                        *comment1 ? comment2 : comment1, regs);
+         if (ok && *(++op) == ',')
+           ok = avr_operand (insn, insn2, addr, *(++op), op2,
+                             *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
        }
+    }
 
-      (*prin) (stream, "    %-8s", opcode->name);
+  if (!ok)
+    {
+      /* Unknown opcode, or invalid combination of operands.  */
+      sprintf (op1, "0x%04x", insn);
+      op2[0] = 0;
+      sprintf (comment1, "????");
+      comment2[0] = 0;
+    }
 
-      if (*op1)
-       (*prin) (stream, "%s", op1);
+  (*prin) (stream, "%s", ok ? opcode->name : ".word");
 
-      if (*op2)
-       (*prin) (stream, ", %s", op2);
+  if (*op1)
+      (*prin) (stream, "\t%s", op1);
 
-      if (*comment1)
-       (*prin) (stream, "\t; %s", comment1);
+  if (*op2)
+    (*prin) (stream, ", %s", op2);
+
+  if (*comment1)
+    (*prin) (stream, "\t; %s", comment1);
+
+  if (sym_op1)
+    info->print_address_func (sym_addr1, info);
+
+  if (*comment2)
+    (*prin) (stream, " %s", comment2);
+
+  if (sym_op2)
+    info->print_address_func (sym_addr2, info);
 
-      if (*comment2)
-       (*prin) (stream, " %s", comment2);
-    }
-  else
-   (*prin) (stream, ".word 0x%04x\t; ????", insn);
-  
   return cmd_len;
 }
This page took 0.026695 seconds and 4 git commands to generate.