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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [opcodes/] [d30v-dis.c] - Diff between revs 578 and 1765

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

Rev 578 Rev 1765
/* Disassemble D30V instructions.
/* Disassemble D30V instructions.
   Copyright 1997, 1998, 2000 Free Software Foundation, Inc.
   Copyright 1997, 1998, 2000 Free Software Foundation, Inc.
 
 
This program is free software; you can redistribute it and/or modify
This program 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 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 
#include <stdio.h>
#include <stdio.h>
#include "sysdep.h"
#include "sysdep.h"
#include "opcode/d30v.h" 
#include "opcode/d30v.h" 
#include "dis-asm.h"
#include "dis-asm.h"
#include "opintl.h"
#include "opintl.h"
 
 
#define PC_MASK 0xFFFFFFFF
#define PC_MASK 0xFFFFFFFF
 
 
static int lookup_opcode PARAMS (( struct d30v_insn *insn, long num, int is_long ));
static int lookup_opcode PARAMS (( struct d30v_insn *insn, long num, int is_long ));
static void print_insn PARAMS (( struct disassemble_info *info, bfd_vma memaddr, long long num,
static void print_insn PARAMS (( struct disassemble_info *info, bfd_vma memaddr, long long num,
                                 struct d30v_insn *insn, int is_long, int show_ext ));
                                 struct d30v_insn *insn, int is_long, int show_ext ));
static int extract_value PARAMS (( long long num, struct d30v_operand *oper, int is_long ));
static int extract_value PARAMS (( long long num, struct d30v_operand *oper, int is_long ));
 
 
int
int
print_insn_d30v (memaddr, info)
print_insn_d30v (memaddr, info)
     bfd_vma memaddr;
     bfd_vma memaddr;
     struct disassemble_info *info;
     struct disassemble_info *info;
{
{
  int status, result;
  int status, result;
  bfd_byte buffer[12];
  bfd_byte buffer[12];
  unsigned long in1,in2;
  unsigned long in1,in2;
  struct d30v_insn insn;
  struct d30v_insn insn;
  long long num;
  long long num;
 
 
  insn.form = (struct d30v_format *)NULL;
  insn.form = (struct d30v_format *)NULL;
 
 
  info->bytes_per_line = 8;
  info->bytes_per_line = 8;
  info->bytes_per_chunk = 4;
  info->bytes_per_chunk = 4;
  info->display_endian = BFD_ENDIAN_BIG;
  info->display_endian = BFD_ENDIAN_BIG;
 
 
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
  if (status != 0)
  if (status != 0)
    {
    {
      (*info->memory_error_func) (status, memaddr, info);
      (*info->memory_error_func) (status, memaddr, info);
      return -1;
      return -1;
    }
    }
  in1 = bfd_getb32 (buffer);
  in1 = bfd_getb32 (buffer);
 
 
  status = (*info->read_memory_func) (memaddr+4, buffer, 4, info);
  status = (*info->read_memory_func) (memaddr+4, buffer, 4, info);
  if (status != 0)
  if (status != 0)
    {
    {
      info->bytes_per_line = 8;
      info->bytes_per_line = 8;
      if (!(result = lookup_opcode(&insn, in1, 0)))
      if (!(result = lookup_opcode(&insn, in1, 0)))
        (*info->fprintf_func) (info->stream, ".long\t0x%x",in1);
        (*info->fprintf_func) (info->stream, ".long\t0x%x",in1);
      else
      else
        print_insn(info, memaddr, (long long) in1, &insn, 0, result);
        print_insn(info, memaddr, (long long) in1, &insn, 0, result);
      return 4;
      return 4;
    }
    }
  in2 = bfd_getb32 (buffer);
  in2 = bfd_getb32 (buffer);
 
 
  if (in1 & in2 & FM01)
  if (in1 & in2 & FM01)
    {
    {
      /* LONG instruction */
      /* LONG instruction */
      if (!(result = lookup_opcode(&insn, in1, 1)))
      if (!(result = lookup_opcode(&insn, in1, 1)))
        {
        {
          (*info->fprintf_func) (info->stream, ".long\t0x%x,0x%x",in1,in2);
          (*info->fprintf_func) (info->stream, ".long\t0x%x,0x%x",in1,in2);
          return 8;
          return 8;
        }
        }
      num = (long long)in1 << 32 | in2;
      num = (long long)in1 << 32 | in2;
      print_insn(info, memaddr, num, &insn, 1, result);
      print_insn(info, memaddr, num, &insn, 1, result);
    }
    }
  else
  else
    {
    {
      num = in1;
      num = in1;
      if (!(result = lookup_opcode(&insn, in1, 0)))
      if (!(result = lookup_opcode(&insn, in1, 0)))
        (*info->fprintf_func) (info->stream, ".long\t0x%x",in1);
        (*info->fprintf_func) (info->stream, ".long\t0x%x",in1);
      else
      else
        print_insn(info, memaddr, num, &insn, 0, result);
        print_insn(info, memaddr, num, &insn, 0, result);
 
 
      switch ( ((in1>>31)<<1) | (in2>>31) )
      switch ( ((in1>>31)<<1) | (in2>>31) )
        {
        {
        case 0:
        case 0:
          (*info->fprintf_func) (info->stream, "\t||\t");
          (*info->fprintf_func) (info->stream, "\t||\t");
          break;
          break;
        case 1:
        case 1:
          (*info->fprintf_func) (info->stream, "\t->\t");
          (*info->fprintf_func) (info->stream, "\t->\t");
          break;
          break;
        case 2:
        case 2:
          (*info->fprintf_func) (info->stream, "\t<-\t");
          (*info->fprintf_func) (info->stream, "\t<-\t");
        default:
        default:
          break;
          break;
        }
        }
 
 
      insn.form = (struct d30v_format *)NULL;
      insn.form = (struct d30v_format *)NULL;
      num = in2;
      num = in2;
      if (!(result = lookup_opcode(&insn, in2, 0)))
      if (!(result = lookup_opcode(&insn, in2, 0)))
        (*info->fprintf_func) (info->stream, ".long\t0x%x",in2);
        (*info->fprintf_func) (info->stream, ".long\t0x%x",in2);
      else
      else
        print_insn(info, memaddr, num, &insn, 0, result);
        print_insn(info, memaddr, num, &insn, 0, result);
 
 
    }
    }
  return 8;
  return 8;
}
}
 
 
 
 
/* returns 0 if lookup fails */
/* returns 0 if lookup fails */
/* 1 if found and only one form */
/* 1 if found and only one form */
/* 2 if found and there are short and long forms */
/* 2 if found and there are short and long forms */
static int
static int
lookup_opcode (insn, num, is_long)
lookup_opcode (insn, num, is_long)
     struct d30v_insn *insn;
     struct d30v_insn *insn;
     long num;
     long num;
     int is_long;
     int is_long;
{
{
  int i=0, index;
  int i=0, index;
  struct d30v_format *f;
  struct d30v_format *f;
  struct d30v_opcode *op = (struct d30v_opcode *)d30v_opcode_table;
  struct d30v_opcode *op = (struct d30v_opcode *)d30v_opcode_table;
  int op1 = (num >> 25) & 0x7;
  int op1 = (num >> 25) & 0x7;
  int op2 = (num >> 20) & 0x1f;
  int op2 = (num >> 20) & 0x1f;
  int mod = (num >> 18) & 0x3;
  int mod = (num >> 18) & 0x3;
 
 
  /* find the opcode */
  /* find the opcode */
  do {
  do {
    if ((op->op1 == op1) && (op->op2 == op2))
    if ((op->op1 == op1) && (op->op2 == op2))
      break;
      break;
    op++;
    op++;
  } while (op->name);
  } while (op->name);
 
 
  if (!op || !op->name)
  if (!op || !op->name)
    return 0;
    return 0;
 
 
  while (op->op1 == op1 && op->op2 == op2)
  while (op->op1 == op1 && op->op2 == op2)
    {
    {
      /* scan through all the formats for the opcode  */
      /* scan through all the formats for the opcode  */
      index = op->format[i++];
      index = op->format[i++];
      do
      do
        {
        {
          f = (struct d30v_format *)&d30v_format_table[index];
          f = (struct d30v_format *)&d30v_format_table[index];
          while (f->form == index)
          while (f->form == index)
            {
            {
              if ((!is_long || f->form >= LONG) && (f->modifier == mod))
              if ((!is_long || f->form >= LONG) && (f->modifier == mod))
                {
                {
                  insn->form = f;
                  insn->form = f;
                  break;
                  break;
                }
                }
              f++;
              f++;
            }
            }
          if (insn->form)
          if (insn->form)
            break;
            break;
        } while ((index = op->format[i++]) != 0);
        } while ((index = op->format[i++]) != 0);
      if (insn->form)
      if (insn->form)
        break;
        break;
      op++;
      op++;
      i=0;
      i=0;
    }
    }
  if (insn->form == NULL)
  if (insn->form == NULL)
    return 0;
    return 0;
 
 
  insn->op = op;
  insn->op = op;
  insn->ecc = (num >> 28) & 0x7;
  insn->ecc = (num >> 28) & 0x7;
  if (op->format[1])
  if (op->format[1])
    return 2;
    return 2;
  else
  else
    return 1;
    return 1;
}
}
 
 
 
 
static void
static void
print_insn ( info, memaddr, num, insn, is_long, show_ext )
print_insn ( info, memaddr, num, insn, is_long, show_ext )
     struct disassemble_info *info;
     struct disassemble_info *info;
     bfd_vma memaddr;
     bfd_vma memaddr;
     long long num;
     long long num;
     struct d30v_insn *insn;
     struct d30v_insn *insn;
     int is_long;
     int is_long;
     int show_ext;
     int show_ext;
{
{
  int val, opnum, need_comma=0;
  int val, opnum, need_comma=0;
  struct d30v_operand *oper;
  struct d30v_operand *oper;
  int i, match, opind=0, need_paren=0, found_control=0;
  int i, match, opind=0, need_paren=0, found_control=0;
 
 
  (*info->fprintf_func) (info->stream, "%s",insn->op->name);
  (*info->fprintf_func) (info->stream, "%s",insn->op->name);
 
 
  /* check for CMP or CMPU */
  /* check for CMP or CMPU */
  if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
  if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
    {
    {
      opind++;
      opind++;
      val = extract_value(num,(struct d30v_operand *)&d30v_operand_table[insn->form->operands[0]],is_long);
      val = extract_value(num,(struct d30v_operand *)&d30v_operand_table[insn->form->operands[0]],is_long);
      (*info->fprintf_func) (info->stream, "%s",d30v_cc_names[val]);
      (*info->fprintf_func) (info->stream, "%s",d30v_cc_names[val]);
    }
    }
 
 
  /* add in ".s" or ".l" */
  /* add in ".s" or ".l" */
  if (show_ext == 2)
  if (show_ext == 2)
    {
    {
      if (is_long)
      if (is_long)
        (*info->fprintf_func) (info->stream, ".l");
        (*info->fprintf_func) (info->stream, ".l");
      else
      else
        (*info->fprintf_func) (info->stream, ".s");
        (*info->fprintf_func) (info->stream, ".s");
    }
    }
 
 
  if (insn->ecc)
  if (insn->ecc)
    (*info->fprintf_func) (info->stream, "/%s",d30v_ecc_names[insn->ecc]);
    (*info->fprintf_func) (info->stream, "/%s",d30v_ecc_names[insn->ecc]);
 
 
  (*info->fprintf_func) (info->stream, "\t");
  (*info->fprintf_func) (info->stream, "\t");
 
 
  while ((opnum = insn->form->operands[opind++]) != 0)
  while ((opnum = insn->form->operands[opind++]) != 0)
    {
    {
      int bits;
      int bits;
      oper = (struct d30v_operand *)&d30v_operand_table[opnum];
      oper = (struct d30v_operand *)&d30v_operand_table[opnum];
      bits = oper->bits;
      bits = oper->bits;
      if (oper->flags & OPERAND_SHIFT)
      if (oper->flags & OPERAND_SHIFT)
        bits += 3;
        bits += 3;
 
 
      if (need_comma && oper->flags != OPERAND_PLUS && oper->flags != OPERAND_MINUS)
      if (need_comma && oper->flags != OPERAND_PLUS && oper->flags != OPERAND_MINUS)
        {
        {
          need_comma=0;
          need_comma=0;
          (*info->fprintf_func) (info->stream, ", ");
          (*info->fprintf_func) (info->stream, ", ");
        }
        }
 
 
      if (oper->flags == OPERAND_ATMINUS)
      if (oper->flags == OPERAND_ATMINUS)
        {
        {
          (*info->fprintf_func) (info->stream, "@-");
          (*info->fprintf_func) (info->stream, "@-");
          continue;
          continue;
        }
        }
      if (oper->flags == OPERAND_MINUS)
      if (oper->flags == OPERAND_MINUS)
        {
        {
          (*info->fprintf_func) (info->stream, "-");
          (*info->fprintf_func) (info->stream, "-");
          continue;
          continue;
        }
        }
      if (oper->flags == OPERAND_PLUS)
      if (oper->flags == OPERAND_PLUS)
        {
        {
          (*info->fprintf_func) (info->stream, "+");
          (*info->fprintf_func) (info->stream, "+");
          continue;
          continue;
        }
        }
      if (oper->flags == OPERAND_ATSIGN)
      if (oper->flags == OPERAND_ATSIGN)
        {
        {
          (*info->fprintf_func) (info->stream, "@");
          (*info->fprintf_func) (info->stream, "@");
          continue;
          continue;
        }
        }
      if (oper->flags == OPERAND_ATPAR)
      if (oper->flags == OPERAND_ATPAR)
        {
        {
          (*info->fprintf_func) (info->stream, "@(");
          (*info->fprintf_func) (info->stream, "@(");
          need_paren = 1;
          need_paren = 1;
          continue;
          continue;
        }
        }
 
 
      if (oper->flags == OPERAND_SPECIAL)
      if (oper->flags == OPERAND_SPECIAL)
        continue;
        continue;
 
 
      val = extract_value(num, oper, is_long);
      val = extract_value(num, oper, is_long);
 
 
      if (oper->flags & OPERAND_REG)
      if (oper->flags & OPERAND_REG)
        {
        {
          match = 0;
          match = 0;
          if (oper->flags & OPERAND_CONTROL)
          if (oper->flags & OPERAND_CONTROL)
            {
            {
              struct d30v_operand *oper3 =
              struct d30v_operand *oper3 =
                (struct d30v_operand *)&d30v_operand_table[insn->form->operands[2]];
                (struct d30v_operand *)&d30v_operand_table[insn->form->operands[2]];
              int id = extract_value (num, oper3, is_long );
              int id = extract_value (num, oper3, is_long );
              found_control = 1;
              found_control = 1;
              switch ( id )
              switch ( id )
                {
                {
                case 0:
                case 0:
                  val |= OPERAND_CONTROL;
                  val |= OPERAND_CONTROL;
                  break;
                  break;
                case 1:
                case 1:
                case 2:
                case 2:
                  val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
                  val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
                  break;
                  break;
                case 3:
                case 3:
                  val |= OPERAND_FLAG;
                  val |= OPERAND_FLAG;
                  break;
                  break;
                default:
                default:
                  fprintf(stderr,"illegal id (%d)\n",id);
                  fprintf(stderr,"illegal id (%d)\n",id);
                }
                }
            }
            }
          else if (oper->flags & OPERAND_ACC)
          else if (oper->flags & OPERAND_ACC)
            val |= OPERAND_ACC;
            val |= OPERAND_ACC;
          else if (oper->flags & OPERAND_FLAG)
          else if (oper->flags & OPERAND_FLAG)
            val |= OPERAND_FLAG;
            val |= OPERAND_FLAG;
          for (i=0;i<reg_name_cnt();i++)
          for (i=0;i<reg_name_cnt();i++)
            {
            {
              if (val == pre_defined_registers[i].value)
              if (val == pre_defined_registers[i].value)
                {
                {
                  if (pre_defined_registers[i].pname)
                  if (pre_defined_registers[i].pname)
                    (*info->fprintf_func)
                    (*info->fprintf_func)
                      (info->stream, "%s",pre_defined_registers[i].pname);
                      (info->stream, "%s",pre_defined_registers[i].pname);
                  else
                  else
                    (*info->fprintf_func)
                    (*info->fprintf_func)
                      (info->stream, "%s",pre_defined_registers[i].name);
                      (info->stream, "%s",pre_defined_registers[i].name);
                  match=1;
                  match=1;
                  break;
                  break;
                }
                }
            }
            }
          if (match==0)
          if (match==0)
            {
            {
              /* this would only get executed if a register was not in the
              /* this would only get executed if a register was not in the
                 register table */
                 register table */
              (*info->fprintf_func)
              (*info->fprintf_func)
                (info->stream, _("<unknown register %d>"), val & 0x3F);
                (info->stream, _("<unknown register %d>"), val & 0x3F);
            }
            }
        }
        }
      /* repeati has a relocation, but its first argument is a plain
      /* repeati has a relocation, but its first argument is a plain
         immediate.  OTOH instructions like djsri have a pc-relative
         immediate.  OTOH instructions like djsri have a pc-relative
         delay target, but a absolute jump target.  Therefore, a test
         delay target, but a absolute jump target.  Therefore, a test
         of insn->op->reloc_flag is not specific enough; we must test
         of insn->op->reloc_flag is not specific enough; we must test
         if the actual operand we are handling now is pc-relative.  */
         if the actual operand we are handling now is pc-relative.  */
      else if (oper->flags & OPERAND_PCREL)
      else if (oper->flags & OPERAND_PCREL)
        {
        {
          int neg = 0;
          int neg = 0;
 
 
          /* IMM6S3 is unsigned.  */
          /* IMM6S3 is unsigned.  */
          if (oper->flags & OPERAND_SIGNED || bits == 32)
          if (oper->flags & OPERAND_SIGNED || bits == 32)
            {
            {
              long max;
              long max;
              max = (1 << (bits - 1));
              max = (1 << (bits - 1));
              if (val & max)
              if (val & max)
                {
                {
                  if (bits == 32)
                  if (bits == 32)
                    val = -val;
                    val = -val;
                  else
                  else
                    val = -val & ((1 << bits)-1);
                    val = -val & ((1 << bits)-1);
                  neg = 1;
                  neg = 1;
                }
                }
            }
            }
          if (neg)
          if (neg)
            {
            {
              (*info->fprintf_func) (info->stream, "-%x\t(",val);
              (*info->fprintf_func) (info->stream, "-%x\t(",val);
              (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
              (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
              (*info->fprintf_func) (info->stream, ")");
              (*info->fprintf_func) (info->stream, ")");
            }
            }
          else
          else
            {
            {
              (*info->fprintf_func) (info->stream, "%x\t(",val);
              (*info->fprintf_func) (info->stream, "%x\t(",val);
              (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
              (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
              (*info->fprintf_func) (info->stream, ")");
              (*info->fprintf_func) (info->stream, ")");
            }
            }
        }
        }
      else if (insn->op->reloc_flag == RELOC_ABS)
      else if (insn->op->reloc_flag == RELOC_ABS)
        {
        {
          (*info->print_address_func) (val, info);
          (*info->print_address_func) (val, info);
        }
        }
      else
      else
        {
        {
          if (oper->flags & OPERAND_SIGNED)
          if (oper->flags & OPERAND_SIGNED)
            {
            {
              int max = (1 << (bits - 1));
              int max = (1 << (bits - 1));
              if (val & max)
              if (val & max)
                {
                {
                  val = -val;
                  val = -val;
                  if (bits < 32)
                  if (bits < 32)
                    val &= ((1 << bits) - 1);
                    val &= ((1 << bits) - 1);
                  (*info->fprintf_func) (info->stream, "-");
                  (*info->fprintf_func) (info->stream, "-");
                }
                }
            }
            }
          (*info->fprintf_func) (info->stream, "0x%x",val);
          (*info->fprintf_func) (info->stream, "0x%x",val);
        }
        }
      /* if there is another operand, then write a comma and space */
      /* if there is another operand, then write a comma and space */
      if (insn->form->operands[opind] && !(found_control && opind == 2))
      if (insn->form->operands[opind] && !(found_control && opind == 2))
        need_comma = 1;
        need_comma = 1;
    }
    }
  if (need_paren)
  if (need_paren)
    (*info->fprintf_func) (info->stream, ")");
    (*info->fprintf_func) (info->stream, ")");
}
}
 
 
 
 
 
 
static int
static int
extract_value (num, oper, is_long)
extract_value (num, oper, is_long)
     long long num;
     long long num;
     struct d30v_operand *oper;
     struct d30v_operand *oper;
     int is_long;
     int is_long;
{
{
  int val;
  int val;
  int shift = 12 - oper->position;
  int shift = 12 - oper->position;
  int mask = (0xFFFFFFFF >> (32 - oper->bits));
  int mask = (0xFFFFFFFF >> (32 - oper->bits));
 
 
  if (is_long)
  if (is_long)
    {
    {
      if (oper->bits == 32)
      if (oper->bits == 32)
        {
        {
          /* piece together 32-bit constant */
          /* piece together 32-bit constant */
          val = ((num & 0x3FFFF)
          val = ((num & 0x3FFFF)
                 | ((num & 0xFF00000) >> 2)
                 | ((num & 0xFF00000) >> 2)
                 | ((num & 0x3F00000000LL) >> 6));
                 | ((num & 0x3F00000000LL) >> 6));
        }
        }
      else
      else
        val = (num >> (32 + shift)) & mask;
        val = (num >> (32 + shift)) & mask;
    }
    }
  else
  else
    val = (num >> shift) & mask;
    val = (num >> shift) & mask;
 
 
  if (oper->flags & OPERAND_SHIFT)
  if (oper->flags & OPERAND_SHIFT)
    val <<= 3;
    val <<= 3;
 
 
  return val;
  return val;
}
}
 
 

powered by: WebSVN 2.1.0

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