OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [opcodes/] [avr-dis.c] - Diff between revs 18 and 158

Only display areas with differences | Details | Blame | View Log

Rev 18 Rev 158
/* Disassemble AVR instructions.
/* Disassemble AVR instructions.
   Copyright 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008
   Copyright 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
   Contributed by Denis Chertykov <denisc@overta.ru>
   Contributed by Denis Chertykov <denisc@overta.ru>
 
 
   This file is part of libopcodes.
   This file is part of libopcodes.
 
 
   This library is free software; you can redistribute it and/or modify
   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
   any later version.
 
 
   It is distributed in the hope that it will be useful, but WITHOUT
   It is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.
   License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */
   MA 02110-1301, USA.  */
 
 
#include <assert.h>
#include <assert.h>
#include "sysdep.h"
#include "sysdep.h"
#include "dis-asm.h"
#include "dis-asm.h"
#include "opintl.h"
#include "opintl.h"
#include "libiberty.h"
#include "libiberty.h"
 
 
struct avr_opcodes_s
struct avr_opcodes_s
{
{
  char *name;
  char *name;
  char *constraints;
  char *constraints;
  char *opcode;
  char *opcode;
  int insn_size;                /* In words.  */
  int insn_size;                /* In words.  */
  int isa;
  int isa;
  unsigned int bin_opcode;
  unsigned int bin_opcode;
};
};
 
 
#define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
#define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
{#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
{#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
 
 
const struct avr_opcodes_s avr_opcodes[] =
const struct avr_opcodes_s avr_opcodes[] =
{
{
  #include "opcode/avr.h"
  #include "opcode/avr.h"
  {NULL, NULL, NULL, 0, 0, 0}
  {NULL, NULL, NULL, 0, 0, 0}
};
};
 
 
static const char * comment_start = "0x";
static const char * comment_start = "0x";
 
 
static int
static int
avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
             char *opcode_str, char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
             char *opcode_str, char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
{
{
  int ok = 1;
  int ok = 1;
  *sym = 0;
  *sym = 0;
 
 
  switch (constraint)
  switch (constraint)
    {
    {
      /* Any register operand.  */
      /* Any register operand.  */
    case 'r':
    case 'r':
      if (regs)
      if (regs)
        insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register.  */
        insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register.  */
      else
      else
        insn = (insn & 0x01f0) >> 4; /* Destination register.  */
        insn = (insn & 0x01f0) >> 4; /* Destination register.  */
 
 
      sprintf (buf, "r%d", insn);
      sprintf (buf, "r%d", insn);
      break;
      break;
 
 
    case 'd':
    case 'd':
      if (regs)
      if (regs)
        sprintf (buf, "r%d", 16 + (insn & 0xf));
        sprintf (buf, "r%d", 16 + (insn & 0xf));
      else
      else
        sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
        sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
      break;
      break;
 
 
    case 'w':
    case 'w':
      sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
      sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
      break;
      break;
 
 
    case 'a':
    case 'a':
      if (regs)
      if (regs)
        sprintf (buf, "r%d", 16 + (insn & 7));
        sprintf (buf, "r%d", 16 + (insn & 7));
      else
      else
        sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
        sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
      break;
      break;
 
 
    case 'v':
    case 'v':
      if (regs)
      if (regs)
        sprintf (buf, "r%d", (insn & 0xf) * 2);
        sprintf (buf, "r%d", (insn & 0xf) * 2);
      else
      else
        sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
        sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
      break;
      break;
 
 
    case 'e':
    case 'e':
      {
      {
        char *xyz;
        char *xyz;
 
 
        switch (insn & 0x100f)
        switch (insn & 0x100f)
          {
          {
            case 0x0000: xyz = "Z";  break;
            case 0x0000: xyz = "Z";  break;
            case 0x1001: xyz = "Z+"; break;
            case 0x1001: xyz = "Z+"; break;
            case 0x1002: xyz = "-Z"; break;
            case 0x1002: xyz = "-Z"; break;
            case 0x0008: xyz = "Y";  break;
            case 0x0008: xyz = "Y";  break;
            case 0x1009: xyz = "Y+"; break;
            case 0x1009: xyz = "Y+"; break;
            case 0x100a: xyz = "-Y"; break;
            case 0x100a: xyz = "-Y"; break;
            case 0x100c: xyz = "X";  break;
            case 0x100c: xyz = "X";  break;
            case 0x100d: xyz = "X+"; break;
            case 0x100d: xyz = "X+"; break;
            case 0x100e: xyz = "-X"; break;
            case 0x100e: xyz = "-X"; break;
            default: xyz = "??"; ok = 0;
            default: xyz = "??"; ok = 0;
          }
          }
        strcpy (buf, xyz);
        strcpy (buf, xyz);
 
 
        if (AVR_UNDEF_P (insn))
        if (AVR_UNDEF_P (insn))
          sprintf (comment, _("undefined"));
          sprintf (comment, _("undefined"));
      }
      }
      break;
      break;
 
 
    case 'z':
    case 'z':
      *buf++ = 'Z';
      *buf++ = 'Z';
 
 
      /* Check for post-increment. */
      /* Check for post-increment. */
      char *s;
      char *s;
      for (s = opcode_str; *s; ++s)
      for (s = opcode_str; *s; ++s)
        {
        {
          if (*s == '+')
          if (*s == '+')
            {
            {
 
              if (insn & (1 << (15 - (s - opcode_str))))
        *buf++ = '+';
        *buf++ = '+';
              break;
              break;
            }
            }
        }
        }
 
 
      *buf = '\0';
      *buf = '\0';
      if (AVR_UNDEF_P (insn))
      if (AVR_UNDEF_P (insn))
        sprintf (comment, _("undefined"));
        sprintf (comment, _("undefined"));
      break;
      break;
 
 
    case 'b':
    case 'b':
      {
      {
        unsigned int x;
        unsigned int x;
 
 
        x = (insn & 7);
        x = (insn & 7);
        x |= (insn >> 7) & (3 << 3);
        x |= (insn >> 7) & (3 << 3);
        x |= (insn >> 8) & (1 << 5);
        x |= (insn >> 8) & (1 << 5);
 
 
        if (insn & 0x8)
        if (insn & 0x8)
          *buf++ = 'Y';
          *buf++ = 'Y';
        else
        else
          *buf++ = 'Z';
          *buf++ = 'Z';
        sprintf (buf, "+%d", x);
        sprintf (buf, "+%d", x);
        sprintf (comment, "0x%02x", x);
        sprintf (comment, "0x%02x", x);
      }
      }
      break;
      break;
 
 
    case 'h':
    case 'h':
      *sym = 1;
      *sym = 1;
      *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
      *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
      /* See PR binutils/2454.  Ideally we would like to display the hex
      /* See PR binutils/2454.  Ideally we would like to display the hex
         value of the address only once, but this would mean recoding
         value of the address only once, but this would mean recoding
         objdump_print_address() which would affect many targets.  */
         objdump_print_address() which would affect many targets.  */
      sprintf (buf, "%#lx", (unsigned long) *sym_addr);
      sprintf (buf, "%#lx", (unsigned long) *sym_addr);
      strcpy (comment, comment_start);
      strcpy (comment, comment_start);
      break;
      break;
 
 
    case 'L':
    case 'L':
      {
      {
        int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
        int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
        sprintf (buf, ".%+-8d", rel_addr);
        sprintf (buf, ".%+-8d", rel_addr);
        *sym = 1;
        *sym = 1;
        *sym_addr = pc + 2 + rel_addr;
        *sym_addr = pc + 2 + rel_addr;
        strcpy (comment, comment_start);
        strcpy (comment, comment_start);
      }
      }
      break;
      break;
 
 
    case 'l':
    case 'l':
      {
      {
        int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
        int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
 
 
        sprintf (buf, ".%+-8d", rel_addr);
        sprintf (buf, ".%+-8d", rel_addr);
        *sym = 1;
        *sym = 1;
        *sym_addr = pc + 2 + rel_addr;
        *sym_addr = pc + 2 + rel_addr;
        strcpy (comment, comment_start);
        strcpy (comment, comment_start);
      }
      }
      break;
      break;
 
 
    case 'i':
    case 'i':
      sprintf (buf, "0x%04X", insn2);
      sprintf (buf, "0x%04X", insn2);
      break;
      break;
 
 
    case 'M':
    case 'M':
      sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
      sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
      sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
      sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
      break;
      break;
 
 
    case 'n':
    case 'n':
      sprintf (buf, "??");
      sprintf (buf, "??");
      fprintf (stderr, _("Internal disassembler error"));
      fprintf (stderr, _("Internal disassembler error"));
      ok = 0;
      ok = 0;
      break;
      break;
 
 
    case 'K':
    case 'K':
      {
      {
        unsigned int x;
        unsigned int x;
 
 
        x = (insn & 0xf) | ((insn >> 2) & 0x30);
        x = (insn & 0xf) | ((insn >> 2) & 0x30);
        sprintf (buf, "0x%02x", x);
        sprintf (buf, "0x%02x", x);
        sprintf (comment, "%d", x);
        sprintf (comment, "%d", x);
      }
      }
      break;
      break;
 
 
    case 's':
    case 's':
      sprintf (buf, "%d", insn & 7);
      sprintf (buf, "%d", insn & 7);
      break;
      break;
 
 
    case 'S':
    case 'S':
      sprintf (buf, "%d", (insn >> 4) & 7);
      sprintf (buf, "%d", (insn >> 4) & 7);
      break;
      break;
 
 
    case 'P':
    case 'P':
      {
      {
        unsigned int x;
        unsigned int x;
 
 
        x = (insn & 0xf);
        x = (insn & 0xf);
        x |= (insn >> 5) & 0x30;
        x |= (insn >> 5) & 0x30;
        sprintf (buf, "0x%02x", x);
        sprintf (buf, "0x%02x", x);
        sprintf (comment, "%d", x);
        sprintf (comment, "%d", x);
      }
      }
      break;
      break;
 
 
    case 'p':
    case 'p':
      {
      {
        unsigned int x;
        unsigned int x;
 
 
        x = (insn >> 3) & 0x1f;
        x = (insn >> 3) & 0x1f;
        sprintf (buf, "0x%02x", x);
        sprintf (buf, "0x%02x", x);
        sprintf (comment, "%d", x);
        sprintf (comment, "%d", x);
      }
      }
      break;
      break;
 
 
    case 'E':
    case 'E':
      sprintf (buf, "%d", (insn >> 4) & 15);
      sprintf (buf, "%d", (insn >> 4) & 15);
      break;
      break;
 
 
    case '?':
    case '?':
      *buf = '\0';
      *buf = '\0';
      break;
      break;
 
 
    default:
    default:
      sprintf (buf, "??");
      sprintf (buf, "??");
      fprintf (stderr, _("unknown constraint `%c'"), constraint);
      fprintf (stderr, _("unknown constraint `%c'"), constraint);
      ok = 0;
      ok = 0;
    }
    }
 
 
    return ok;
    return ok;
}
}
 
 
static unsigned short
static unsigned short
avrdis_opcode (bfd_vma addr, disassemble_info *info)
avrdis_opcode (bfd_vma addr, disassemble_info *info)
{
{
  bfd_byte buffer[2];
  bfd_byte buffer[2];
  int status;
  int status;
 
 
  status = info->read_memory_func (addr, buffer, 2, info);
  status = info->read_memory_func (addr, buffer, 2, info);
 
 
  if (status == 0)
  if (status == 0)
    return bfd_getl16 (buffer);
    return bfd_getl16 (buffer);
 
 
  info->memory_error_func (status, addr, info);
  info->memory_error_func (status, addr, info);
  return -1;
  return -1;
}
}
 
 
 
 
int
int
print_insn_avr (bfd_vma addr, disassemble_info *info)
print_insn_avr (bfd_vma addr, disassemble_info *info)
{
{
  unsigned int insn, insn2;
  unsigned int insn, insn2;
  const struct avr_opcodes_s *opcode;
  const struct avr_opcodes_s *opcode;
  static unsigned int *maskptr;
  static unsigned int *maskptr;
  void *stream = info->stream;
  void *stream = info->stream;
  fprintf_ftype prin = info->fprintf_func;
  fprintf_ftype prin = info->fprintf_func;
  static unsigned int *avr_bin_masks;
  static unsigned int *avr_bin_masks;
  static int initialized;
  static int initialized;
  int cmd_len = 2;
  int cmd_len = 2;
  int ok = 0;
  int ok = 0;
  char op1[20], op2[20], comment1[40], comment2[40];
  char op1[20], op2[20], comment1[40], comment2[40];
  int sym_op1 = 0, sym_op2 = 0;
  int sym_op1 = 0, sym_op2 = 0;
  bfd_vma sym_addr1, sym_addr2;
  bfd_vma sym_addr1, sym_addr2;
 
 
 
 
  if (!initialized)
  if (!initialized)
    {
    {
      unsigned int nopcodes;
      unsigned int nopcodes;
 
 
      /* PR 4045: Try to avoid duplicating the 0x prefix that
      /* PR 4045: Try to avoid duplicating the 0x prefix that
         objdump_print_addr() will put on addresses when there
         objdump_print_addr() will put on addresses when there
         is no symbol table available.  */
         is no symbol table available.  */
      if (info->symtab_size == 0)
      if (info->symtab_size == 0)
        comment_start = " ";
        comment_start = " ";
 
 
      nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
      nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
 
 
      avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
      avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
 
 
      for (opcode = avr_opcodes, maskptr = avr_bin_masks;
      for (opcode = avr_opcodes, maskptr = avr_bin_masks;
           opcode->name;
           opcode->name;
           opcode++, maskptr++)
           opcode++, maskptr++)
        {
        {
          char * s;
          char * s;
          unsigned int bin = 0;
          unsigned int bin = 0;
          unsigned int mask = 0;
          unsigned int mask = 0;
 
 
          for (s = opcode->opcode; *s; ++s)
          for (s = opcode->opcode; *s; ++s)
            {
            {
              bin <<= 1;
              bin <<= 1;
              mask <<= 1;
              mask <<= 1;
              bin |= (*s == '1');
              bin |= (*s == '1');
              mask |= (*s == '1' || *s == '0');
              mask |= (*s == '1' || *s == '0');
            }
            }
          assert (s - opcode->opcode == 16);
          assert (s - opcode->opcode == 16);
          assert (opcode->bin_opcode == bin);
          assert (opcode->bin_opcode == bin);
          *maskptr = mask;
          *maskptr = mask;
        }
        }
 
 
      initialized = 1;
      initialized = 1;
    }
    }
 
 
  insn = avrdis_opcode (addr, info);
  insn = avrdis_opcode (addr, info);
 
 
  for (opcode = avr_opcodes, maskptr = avr_bin_masks;
  for (opcode = avr_opcodes, maskptr = avr_bin_masks;
       opcode->name;
       opcode->name;
       opcode++, maskptr++)
       opcode++, maskptr++)
    if ((insn & *maskptr) == opcode->bin_opcode)
    if ((insn & *maskptr) == opcode->bin_opcode)
      break;
      break;
 
 
  /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
  /* 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).  */
     `std b+0,r' as `st b,r' (next entry in the table).  */
 
 
  if (AVR_DISP0_P (insn))
  if (AVR_DISP0_P (insn))
    opcode++;
    opcode++;
 
 
  op1[0] = 0;
  op1[0] = 0;
  op2[0] = 0;
  op2[0] = 0;
  comment1[0] = 0;
  comment1[0] = 0;
  comment2[0] = 0;
  comment2[0] = 0;
 
 
  if (opcode->name)
  if (opcode->name)
    {
    {
      char *constraints = opcode->constraints;
      char *constraints = opcode->constraints;
      char *opcode_str = opcode->opcode;
      char *opcode_str = opcode->opcode;
 
 
      insn2 = 0;
      insn2 = 0;
      ok = 1;
      ok = 1;
 
 
      if (opcode->insn_size > 1)
      if (opcode->insn_size > 1)
        {
        {
          insn2 = avrdis_opcode (addr + 2, info);
          insn2 = avrdis_opcode (addr + 2, info);
          cmd_len = 4;
          cmd_len = 4;
        }
        }
 
 
      if (*constraints && *constraints != '?')
      if (*constraints && *constraints != '?')
        {
        {
          int regs = REGISTER_P (*constraints);
          int regs = REGISTER_P (*constraints);
 
 
          ok = avr_operand (insn, insn2, addr, *constraints, opcode_str, op1, comment1, 0, &sym_op1, &sym_addr1);
          ok = avr_operand (insn, insn2, addr, *constraints, opcode_str, op1, comment1, 0, &sym_op1, &sym_addr1);
 
 
          if (ok && *(++constraints) == ',')
          if (ok && *(++constraints) == ',')
            ok = avr_operand (insn, insn2, addr, *(++constraints), opcode_str, op2,
            ok = avr_operand (insn, insn2, addr, *(++constraints), opcode_str, op2,
                              *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
                              *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
        }
        }
    }
    }
 
 
  if (!ok)
  if (!ok)
    {
    {
      /* Unknown opcode, or invalid combination of operands.  */
      /* Unknown opcode, or invalid combination of operands.  */
      sprintf (op1, "0x%04x", insn);
      sprintf (op1, "0x%04x", insn);
      op2[0] = 0;
      op2[0] = 0;
      sprintf (comment1, "????");
      sprintf (comment1, "????");
      comment2[0] = 0;
      comment2[0] = 0;
    }
    }
 
 
  (*prin) (stream, "%s", ok ? opcode->name : ".word");
  (*prin) (stream, "%s", ok ? opcode->name : ".word");
 
 
  if (*op1)
  if (*op1)
      (*prin) (stream, "\t%s", op1);
      (*prin) (stream, "\t%s", op1);
 
 
  if (*op2)
  if (*op2)
    (*prin) (stream, ", %s", op2);
    (*prin) (stream, ", %s", op2);
 
 
  if (*comment1)
  if (*comment1)
    (*prin) (stream, "\t; %s", comment1);
    (*prin) (stream, "\t; %s", comment1);
 
 
  if (sym_op1)
  if (sym_op1)
    info->print_address_func (sym_addr1, info);
    info->print_address_func (sym_addr1, info);
 
 
  if (*comment2)
  if (*comment2)
    (*prin) (stream, " %s", comment2);
    (*prin) (stream, " %s", comment2);
 
 
  if (sym_op2)
  if (sym_op2)
    info->print_address_func (sym_addr2, info);
    info->print_address_func (sym_addr2, info);
 
 
  return cmd_len;
  return cmd_len;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.