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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [opcodes/] [cr16-dis.c] - Diff between revs 827 and 840

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 827 Rev 840
/* Disassembler code for CR16.
/* Disassembler code for CR16.
   Copyright 2007, 2008, 2009  Free Software Foundation, Inc.
   Copyright 2007, 2008, 2009  Free Software Foundation, Inc.
   Contributed by M R Swami Reddy (MR.Swami.Reddy@nsc.com).
   Contributed by M R Swami Reddy (MR.Swami.Reddy@nsc.com).
 
 
   This file is part of GAS, GDB and the GNU binutils.
   This file is part of GAS, GDB and the GNU binutils.
 
 
   This program is free software; you can redistribute it and/or modify it
   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 the
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3, or (at your option)
   Free Software Foundation; either version 3, or (at your option)
   any later version.
   any later version.
 
 
   This program is distributed in the hope that it will be useful, but WITHOUT
   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.
   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 Foundation,
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 
#include "dis-asm.h"
#include "dis-asm.h"
#include "sysdep.h"
#include "sysdep.h"
#include "opcode/cr16.h"
#include "opcode/cr16.h"
#include "libiberty.h"
#include "libiberty.h"
 
 
/* String to print when opcode was not matched.  */
/* String to print when opcode was not matched.  */
#define ILLEGAL  "illegal"
#define ILLEGAL  "illegal"
  /* Escape to 16-bit immediate.  */
  /* Escape to 16-bit immediate.  */
#define ESCAPE_16_BIT  0xB
#define ESCAPE_16_BIT  0xB
 
 
/* Extract 'n_bits' from 'a' starting from offset 'offs'.  */
/* Extract 'n_bits' from 'a' starting from offset 'offs'.  */
#define EXTRACT(a, offs, n_bits)                    \
#define EXTRACT(a, offs, n_bits)                    \
  (n_bits == 32 ? (((a) >> (offs)) & 0xffffffffL)   \
  (n_bits == 32 ? (((a) >> (offs)) & 0xffffffffL)   \
  : (((a) >> (offs)) & ((1 << (n_bits)) -1)))
  : (((a) >> (offs)) & ((1 << (n_bits)) -1)))
 
 
/* Set Bit Mask - a mask to set all bits starting from offset 'offs'.  */
/* Set Bit Mask - a mask to set all bits starting from offset 'offs'.  */
#define SBM(offs)  ((((1 << (32 - offs)) -1) << (offs)))
#define SBM(offs)  ((((1 << (32 - offs)) -1) << (offs)))
 
 
typedef unsigned long dwordU;
typedef unsigned long dwordU;
typedef unsigned short wordU;
typedef unsigned short wordU;
 
 
typedef struct
typedef struct
{
{
  dwordU val;
  dwordU val;
  int nbits;
  int nbits;
} parameter;
} parameter;
 
 
/* Structure to map valid 'cinv' instruction options.  */
/* Structure to map valid 'cinv' instruction options.  */
 
 
typedef struct
typedef struct
  {
  {
    /* Cinv printed string.  */
    /* Cinv printed string.  */
    char *istr;
    char *istr;
    /* Value corresponding to the string.  */
    /* Value corresponding to the string.  */
    char *ostr;
    char *ostr;
  }
  }
cinv_entry;
cinv_entry;
 
 
/* CR16 'cinv' options mapping.  */
/* CR16 'cinv' options mapping.  */
const cinv_entry cr16_cinvs[] =
const cinv_entry cr16_cinvs[] =
{
{
  {"cinv[i]",     "cinv    [i]"},
  {"cinv[i]",     "cinv    [i]"},
  {"cinv[i,u]",   "cinv    [i,u]"},
  {"cinv[i,u]",   "cinv    [i,u]"},
  {"cinv[d]",     "cinv    [d]"},
  {"cinv[d]",     "cinv    [d]"},
  {"cinv[d,u]",   "cinv    [d,u]"},
  {"cinv[d,u]",   "cinv    [d,u]"},
  {"cinv[d,i]",   "cinv    [d,i]"},
  {"cinv[d,i]",   "cinv    [d,i]"},
  {"cinv[d,i,u]", "cinv    [d,i,u]"}
  {"cinv[d,i,u]", "cinv    [d,i,u]"}
};
};
 
 
/* Number of valid 'cinv' instruction options.  */
/* Number of valid 'cinv' instruction options.  */
static int NUMCINVS = ARRAY_SIZE (cr16_cinvs);
static int NUMCINVS = ARRAY_SIZE (cr16_cinvs);
 
 
/* Enum to distinguish different registers argument types.  */
/* Enum to distinguish different registers argument types.  */
typedef enum REG_ARG_TYPE
typedef enum REG_ARG_TYPE
  {
  {
    /* General purpose register (r<N>).  */
    /* General purpose register (r<N>).  */
    REG_ARG = 0,
    REG_ARG = 0,
    /*Processor register   */
    /*Processor register   */
    P_ARG,
    P_ARG,
  }
  }
REG_ARG_TYPE;
REG_ARG_TYPE;
 
 
/* Current opcode table entry we're disassembling.  */
/* Current opcode table entry we're disassembling.  */
const inst *instruction;
const inst *instruction;
/* Current instruction we're disassembling.  */
/* Current instruction we're disassembling.  */
ins currInsn;
ins currInsn;
/* The current instruction is read into 3 consecutive words.  */
/* The current instruction is read into 3 consecutive words.  */
wordU words[3];
wordU words[3];
/* Contains all words in appropriate order.  */
/* Contains all words in appropriate order.  */
ULONGLONG allWords;
ULONGLONG allWords;
/* Holds the current processed argument number.  */
/* Holds the current processed argument number.  */
int processing_argument_number;
int processing_argument_number;
/* Nonzero means a IMM4 instruction.  */
/* Nonzero means a IMM4 instruction.  */
int imm4flag;
int imm4flag;
/* Nonzero means the instruction's original size is
/* Nonzero means the instruction's original size is
   incremented (escape sequence is used).  */
   incremented (escape sequence is used).  */
int size_changed;
int size_changed;
 
 
 
 
/* Print the constant expression length.  */
/* Print the constant expression length.  */
 
 
static char *
static char *
print_exp_len (int size)
print_exp_len (int size)
{
{
  switch (size)
  switch (size)
    {
    {
    case 4:
    case 4:
    case 5:
    case 5:
    case 6:
    case 6:
    case 8:
    case 8:
    case 14:
    case 14:
    case 16:
    case 16:
      return ":s";
      return ":s";
    case 20:
    case 20:
    case 24:
    case 24:
    case 32:
    case 32:
      return ":m";
      return ":m";
    case 48:
    case 48:
      return ":l";
      return ":l";
    default:
    default:
      return "";
      return "";
    }
    }
}
}
 
 
 
 
/* Retrieve the number of operands for the current assembled instruction.  */
/* Retrieve the number of operands for the current assembled instruction.  */
 
 
static int
static int
get_number_of_operands (void)
get_number_of_operands (void)
{
{
  int i;
  int i;
 
 
  for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
  for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++)
    ;
    ;
 
 
  return i;
  return i;
}
}
 
 
/* Return the bit size for a given operand.  */
/* Return the bit size for a given operand.  */
 
 
static int
static int
getbits (operand_type op)
getbits (operand_type op)
{
{
  if (op < MAX_OPRD)
  if (op < MAX_OPRD)
    return cr16_optab[op].bit_size;
    return cr16_optab[op].bit_size;
 
 
  return 0;
  return 0;
}
}
 
 
/* Return the argument type of a given operand.  */
/* Return the argument type of a given operand.  */
 
 
static argtype
static argtype
getargtype (operand_type op)
getargtype (operand_type op)
{
{
  if (op < MAX_OPRD)
  if (op < MAX_OPRD)
    return cr16_optab[op].arg_type;
    return cr16_optab[op].arg_type;
 
 
  return nullargs;
  return nullargs;
}
}
 
 
/* Given a 'CC' instruction constant operand, return its corresponding
/* Given a 'CC' instruction constant operand, return its corresponding
   string. This routine is used when disassembling the 'CC' instruction.  */
   string. This routine is used when disassembling the 'CC' instruction.  */
 
 
static char *
static char *
getccstring (unsigned cc)
getccstring (unsigned cc)
{
{
  return (char *) cr16_b_cond_tab[cc];
  return (char *) cr16_b_cond_tab[cc];
}
}
 
 
 
 
/* Given a 'cinv' instruction constant operand, return its corresponding
/* Given a 'cinv' instruction constant operand, return its corresponding
   string. This routine is used when disassembling the 'cinv' instruction. */
   string. This routine is used when disassembling the 'cinv' instruction. */
 
 
static char *
static char *
getcinvstring (const char *str)
getcinvstring (const char *str)
{
{
  const cinv_entry *cinv;
  const cinv_entry *cinv;
 
 
  for (cinv = cr16_cinvs; cinv < (cr16_cinvs + NUMCINVS); cinv++)
  for (cinv = cr16_cinvs; cinv < (cr16_cinvs + NUMCINVS); cinv++)
    if (strcmp (cinv->istr, str) == 0)
    if (strcmp (cinv->istr, str) == 0)
      return cinv->ostr;
      return cinv->ostr;
 
 
  return ILLEGAL;
  return ILLEGAL;
}
}
 
 
/* Given the trap index in dispatch table, return its name.
/* Given the trap index in dispatch table, return its name.
   This routine is used when disassembling the 'excp' instruction.  */
   This routine is used when disassembling the 'excp' instruction.  */
 
 
static char *
static char *
gettrapstring (unsigned int index)
gettrapstring (unsigned int index)
{
{
  const trap_entry *trap;
  const trap_entry *trap;
 
 
  for (trap = cr16_traps; trap < cr16_traps + NUMTRAPS; trap++)
  for (trap = cr16_traps; trap < cr16_traps + NUMTRAPS; trap++)
    if (trap->entry == index)
    if (trap->entry == index)
      return trap->name;
      return trap->name;
 
 
  return ILLEGAL;
  return ILLEGAL;
}
}
 
 
/* Given a register enum value, retrieve its name.  */
/* Given a register enum value, retrieve its name.  */
 
 
static char *
static char *
getregname (reg r)
getregname (reg r)
{
{
  const reg_entry *reg = cr16_regtab + r;
  const reg_entry *reg = cr16_regtab + r;
 
 
  if (reg->type != CR16_R_REGTYPE)
  if (reg->type != CR16_R_REGTYPE)
    return ILLEGAL;
    return ILLEGAL;
 
 
  return reg->name;
  return reg->name;
}
}
 
 
/* Given a register pair enum value, retrieve its name.  */
/* Given a register pair enum value, retrieve its name.  */
 
 
static char *
static char *
getregpname (reg r)
getregpname (reg r)
{
{
  const reg_entry *reg = cr16_regptab + r;
  const reg_entry *reg = cr16_regptab + r;
 
 
  if (reg->type != CR16_RP_REGTYPE)
  if (reg->type != CR16_RP_REGTYPE)
    return ILLEGAL;
    return ILLEGAL;
 
 
  return reg->name;
  return reg->name;
}
}
 
 
/* Given a index register pair enum value, retrieve its name.  */
/* Given a index register pair enum value, retrieve its name.  */
 
 
static char *
static char *
getidxregpname (reg r)
getidxregpname (reg r)
{
{
  const reg_entry *reg;
  const reg_entry *reg;
 
 
  switch (r)
  switch (r)
   {
   {
   case 0: r = 0; break;
   case 0: r = 0; break;
   case 1: r = 2; break;
   case 1: r = 2; break;
   case 2: r = 4; break;
   case 2: r = 4; break;
   case 3: r = 6; break;
   case 3: r = 6; break;
   case 4: r = 8; break;
   case 4: r = 8; break;
   case 5: r = 10; break;
   case 5: r = 10; break;
   case 6: r = 3; break;
   case 6: r = 3; break;
   case 7: r = 5; break;
   case 7: r = 5; break;
   default:
   default:
     break;
     break;
   }
   }
 
 
  reg = cr16_regptab + r;
  reg = cr16_regptab + r;
 
 
  if (reg->type != CR16_RP_REGTYPE)
  if (reg->type != CR16_RP_REGTYPE)
    return ILLEGAL;
    return ILLEGAL;
 
 
  return reg->name;
  return reg->name;
}
}
 
 
/* Getting a processor register name.  */
/* Getting a processor register name.  */
 
 
static char *
static char *
getprocregname (int index)
getprocregname (int index)
{
{
  const reg_entry *r;
  const reg_entry *r;
 
 
  for (r = cr16_pregtab; r < cr16_pregtab + NUMPREGS; r++)
  for (r = cr16_pregtab; r < cr16_pregtab + NUMPREGS; r++)
    if (r->image == index)
    if (r->image == index)
      return r->name;
      return r->name;
 
 
  return "ILLEGAL REGISTER";
  return "ILLEGAL REGISTER";
}
}
 
 
/* Getting a processor register name - 32 bit size.  */
/* Getting a processor register name - 32 bit size.  */
 
 
static char *
static char *
getprocpregname (int index)
getprocpregname (int index)
{
{
  const reg_entry *r;
  const reg_entry *r;
 
 
  for (r = cr16_pregptab; r < cr16_pregptab + NUMPREGPS; r++)
  for (r = cr16_pregptab; r < cr16_pregptab + NUMPREGPS; r++)
    if (r->image == index)
    if (r->image == index)
      return r->name;
      return r->name;
 
 
  return "ILLEGAL REGISTER";
  return "ILLEGAL REGISTER";
}
}
 
 
/* START and END are relating 'allWords' struct, which is 48 bits size.
/* START and END are relating 'allWords' struct, which is 48 bits size.
 
 
                          START|--------|END
                          START|--------|END
             +---------+---------+---------+---------+
             +---------+---------+---------+---------+
             |         |   V    |     A    |   L     |
             |         |   V    |     A    |   L     |
             +---------+---------+---------+---------+
             +---------+---------+---------+---------+
                       0         16        32        48
                       0         16        32        48
    words                  [0]       [1]       [2]      */
    words                  [0]       [1]       [2]      */
 
 
static parameter
static parameter
makelongparameter (ULONGLONG val, int start, int end)
makelongparameter (ULONGLONG val, int start, int end)
{
{
  parameter p;
  parameter p;
 
 
  p.val = (dwordU) EXTRACT (val, 48 - end, end - start);
  p.val = (dwordU) EXTRACT (val, 48 - end, end - start);
  p.nbits = end - start;
  p.nbits = end - start;
  return p;
  return p;
}
}
 
 
/* Build a mask of the instruction's 'constant' opcode,
/* Build a mask of the instruction's 'constant' opcode,
   based on the instruction's printing flags.  */
   based on the instruction's printing flags.  */
 
 
static unsigned long
static unsigned long
build_mask (void)
build_mask (void)
{
{
  unsigned long mask = SBM (instruction->match_bits);
  unsigned long mask = SBM (instruction->match_bits);
 
 
  /* Adjust mask for bcond with 32-bit size instruction.  */
  /* Adjust mask for bcond with 32-bit size instruction.  */
  if ((IS_INSN_MNEMONIC("b") && instruction->size == 2))
  if ((IS_INSN_MNEMONIC("b") && instruction->size == 2))
    mask = 0xff0f0000;
    mask = 0xff0f0000;
 
 
  return mask;
  return mask;
}
}
 
 
/* Search for a matching opcode. Return 1 for success, 0 for failure.  */
/* Search for a matching opcode. Return 1 for success, 0 for failure.  */
 
 
static int
static int
match_opcode (void)
match_opcode (void)
{
{
  unsigned long mask;
  unsigned long mask;
  /* The instruction 'constant' opcode doewsn't exceed 32 bits.  */
  /* The instruction 'constant' opcode doewsn't exceed 32 bits.  */
  unsigned long doubleWord = (words[1] + (words[0] << 16)) & 0xffffffff;
  unsigned long doubleWord = (words[1] + (words[0] << 16)) & 0xffffffff;
 
 
  /* Start searching from end of instruction table.  */
  /* Start searching from end of instruction table.  */
  instruction = &cr16_instruction[NUMOPCODES - 2];
  instruction = &cr16_instruction[NUMOPCODES - 2];
 
 
  /* Loop over instruction table until a full match is found.  */
  /* Loop over instruction table until a full match is found.  */
  while (instruction >= cr16_instruction)
  while (instruction >= cr16_instruction)
    {
    {
      mask = build_mask ();
      mask = build_mask ();
      /* Adjust mask for bcond with 32-bit size instruction */
      /* Adjust mask for bcond with 32-bit size instruction */
      if ((IS_INSN_MNEMONIC("b") && instruction->size == 2))
      if ((IS_INSN_MNEMONIC("b") && instruction->size == 2))
        mask = 0xff0f0000;
        mask = 0xff0f0000;
 
 
      if ((doubleWord & mask) == BIN (instruction->match,
      if ((doubleWord & mask) == BIN (instruction->match,
                                      instruction->match_bits))
                                      instruction->match_bits))
        return 1;
        return 1;
      else
      else
        instruction--;
        instruction--;
    }
    }
  return 0;
  return 0;
}
}
 
 
/* Set the proper parameter value for different type of arguments.  */
/* Set the proper parameter value for different type of arguments.  */
 
 
static void
static void
make_argument (argument * a, int start_bits)
make_argument (argument * a, int start_bits)
{
{
  int inst_bit_size;
  int inst_bit_size;
  parameter p;
  parameter p;
 
 
  if ((instruction->size == 3) && a->size >= 16)
  if ((instruction->size == 3) && a->size >= 16)
    inst_bit_size = 48;
    inst_bit_size = 48;
  else
  else
    inst_bit_size = 32;
    inst_bit_size = 32;
 
 
  switch (a->type)
  switch (a->type)
    {
    {
    case arg_r:
    case arg_r:
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
      a->r = p.val;
      a->r = p.val;
      break;
      break;
 
 
    case arg_rp:
    case arg_rp:
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
      a->rp = p.val;
      a->rp = p.val;
      break;
      break;
 
 
    case arg_pr:
    case arg_pr:
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
      a->pr = p.val;
      a->pr = p.val;
      break;
      break;
 
 
    case arg_prp:
    case arg_prp:
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
      a->prp = p.val;
      a->prp = p.val;
      break;
      break;
 
 
    case arg_ic:
    case arg_ic:
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
      a->constant = p.val;
      a->constant = p.val;
      break;
      break;
 
 
    case arg_cc:
    case arg_cc:
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + a->size),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
 
 
      a->cc = p.val;
      a->cc = p.val;
      break;
      break;
 
 
    case arg_idxr:
    case arg_idxr:
      if ((IS_INSN_MNEMONIC ("cbitb"))
      if ((IS_INSN_MNEMONIC ("cbitb"))
          || (IS_INSN_MNEMONIC ("sbitb"))
          || (IS_INSN_MNEMONIC ("sbitb"))
          || (IS_INSN_MNEMONIC ("tbitb")))
          || (IS_INSN_MNEMONIC ("tbitb")))
        p = makelongparameter (allWords, 8, 9);
        p = makelongparameter (allWords, 8, 9);
      else
      else
        p = makelongparameter (allWords, 9, 10);
        p = makelongparameter (allWords, 9, 10);
      a->i_r = p.val;
      a->i_r = p.val;
      p = makelongparameter (allWords, inst_bit_size - a->size, inst_bit_size);
      p = makelongparameter (allWords, inst_bit_size - a->size, inst_bit_size);
      a->constant = p.val;
      a->constant = p.val;
      break;
      break;
 
 
    case arg_idxrp:
    case arg_idxrp:
      p = makelongparameter (allWords, start_bits + 12, start_bits + 13);
      p = makelongparameter (allWords, start_bits + 12, start_bits + 13);
      a->i_r = p.val;
      a->i_r = p.val;
      p = makelongparameter (allWords, start_bits + 13, start_bits + 16);
      p = makelongparameter (allWords, start_bits + 13, start_bits + 16);
      a->rp = p.val;
      a->rp = p.val;
      if (inst_bit_size > 32)
      if (inst_bit_size > 32)
        {
        {
          p = makelongparameter (allWords, inst_bit_size - start_bits - 12,
          p = makelongparameter (allWords, inst_bit_size - start_bits - 12,
                                 inst_bit_size);
                                 inst_bit_size);
          a->constant = ((p.val & 0xffff) | (p.val >> 8 & 0xf0000));
          a->constant = ((p.val & 0xffff) | (p.val >> 8 & 0xf0000));
        }
        }
      else if (instruction->size == 2)
      else if (instruction->size == 2)
        {
        {
          p = makelongparameter (allWords, inst_bit_size - 22, inst_bit_size);
          p = makelongparameter (allWords, inst_bit_size - 22, inst_bit_size);
          a->constant = (p.val & 0xf) | (((p.val >>20) & 0x3) << 4)
          a->constant = (p.val & 0xf) | (((p.val >>20) & 0x3) << 4)
            | ((p.val >>14 & 0x3) << 6) | (((p.val >>7) & 0x1f) <<7);
            | ((p.val >>14 & 0x3) << 6) | (((p.val >>7) & 0x1f) <<7);
        }
        }
      else if (instruction->size == 1 && a->size == 0)
      else if (instruction->size == 1 && a->size == 0)
        a->constant = 0;
        a->constant = 0;
 
 
      break;
      break;
 
 
    case arg_rbase:
    case arg_rbase:
      p = makelongparameter (allWords, inst_bit_size, inst_bit_size);
      p = makelongparameter (allWords, inst_bit_size, inst_bit_size);
      a->constant = p.val;
      a->constant = p.val;
      p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
      p = makelongparameter (allWords, inst_bit_size - (start_bits + 4),
                             inst_bit_size - start_bits);
                             inst_bit_size - start_bits);
      a->r = p.val;
      a->r = p.val;
      break;
      break;
 
 
    case arg_cr:
    case arg_cr:
      p = makelongparameter (allWords, start_bits + 12, start_bits + 16);
      p = makelongparameter (allWords, start_bits + 12, start_bits + 16);
      a->r = p.val;
      a->r = p.val;
      p = makelongparameter (allWords, inst_bit_size - 16, inst_bit_size);
      p = makelongparameter (allWords, inst_bit_size - 16, inst_bit_size);
      a->constant = p.val;
      a->constant = p.val;
      break;
      break;
 
 
    case arg_crp:
    case arg_crp:
      if (instruction->size == 1)
      if (instruction->size == 1)
        p = makelongparameter (allWords, 12, 16);
        p = makelongparameter (allWords, 12, 16);
      else
      else
        p = makelongparameter (allWords, start_bits + 12, start_bits + 16);
        p = makelongparameter (allWords, start_bits + 12, start_bits + 16);
      a->rp = p.val;
      a->rp = p.val;
 
 
      if (inst_bit_size > 32)
      if (inst_bit_size > 32)
        {
        {
          p = makelongparameter (allWords, inst_bit_size - start_bits - 12,
          p = makelongparameter (allWords, inst_bit_size - start_bits - 12,
                                 inst_bit_size);
                                 inst_bit_size);
          a->constant = ((p.val & 0xffff) | (p.val >> 8 & 0xf0000));
          a->constant = ((p.val & 0xffff) | (p.val >> 8 & 0xf0000));
        }
        }
      else if (instruction->size == 2)
      else if (instruction->size == 2)
        {
        {
          p = makelongparameter (allWords, inst_bit_size - 16, inst_bit_size);
          p = makelongparameter (allWords, inst_bit_size - 16, inst_bit_size);
          a->constant = p.val;
          a->constant = p.val;
        }
        }
      else if (instruction->size == 1 && a->size != 0)
      else if (instruction->size == 1 && a->size != 0)
        {
        {
          p = makelongparameter (allWords, 4, 8);
          p = makelongparameter (allWords, 4, 8);
          if (IS_INSN_MNEMONIC ("loadw")
          if (IS_INSN_MNEMONIC ("loadw")
              || IS_INSN_MNEMONIC ("loadd")
              || IS_INSN_MNEMONIC ("loadd")
              || IS_INSN_MNEMONIC ("storw")
              || IS_INSN_MNEMONIC ("storw")
              || IS_INSN_MNEMONIC ("stord"))
              || IS_INSN_MNEMONIC ("stord"))
            a->constant = (p.val * 2);
            a->constant = (p.val * 2);
          else
          else
            a->constant = p.val;
            a->constant = p.val;
        }
        }
      else /* below case for 0x0(reg pair) */
      else /* below case for 0x0(reg pair) */
        a->constant = 0;
        a->constant = 0;
 
 
      break;
      break;
 
 
    case arg_c:
    case arg_c:
 
 
      if ((IS_INSN_TYPE (BRANCH_INS))
      if ((IS_INSN_TYPE (BRANCH_INS))
          || (IS_INSN_MNEMONIC ("bal"))
          || (IS_INSN_MNEMONIC ("bal"))
          || (IS_INSN_TYPE (CSTBIT_INS))
          || (IS_INSN_TYPE (CSTBIT_INS))
          || (IS_INSN_TYPE (LD_STOR_INS)))
          || (IS_INSN_TYPE (LD_STOR_INS)))
        {
        {
          switch (a->size)
          switch (a->size)
            {
            {
            case 8 :
            case 8 :
              p = makelongparameter (allWords, 0, start_bits);
              p = makelongparameter (allWords, 0, start_bits);
              a->constant = ((((p.val&0xf00)>>4)) | (p.val&0xf));
              a->constant = ((((p.val&0xf00)>>4)) | (p.val&0xf));
              break;
              break;
 
 
            case 24:
            case 24:
              if (instruction->size == 3)
              if (instruction->size == 3)
                {
                {
                  p = makelongparameter (allWords, 16, inst_bit_size);
                  p = makelongparameter (allWords, 16, inst_bit_size);
                  a->constant = ((((p.val>>16)&0xf) << 20)
                  a->constant = ((((p.val>>16)&0xf) << 20)
                                 | (((p.val>>24)&0xf) << 16)
                                 | (((p.val>>24)&0xf) << 16)
                                 | (p.val & 0xffff));
                                 | (p.val & 0xffff));
                }
                }
              else if (instruction->size == 2)
              else if (instruction->size == 2)
                {
                {
                  p = makelongparameter (allWords, 8, inst_bit_size);
                  p = makelongparameter (allWords, 8, inst_bit_size);
                  a->constant = p.val;
                  a->constant = p.val;
                }
                }
              break;
              break;
 
 
            default:
            default:
              p = makelongparameter (allWords, inst_bit_size - (start_bits +
              p = makelongparameter (allWords, inst_bit_size - (start_bits +
                                                                a->size), inst_bit_size - start_bits);
                                                                a->size), inst_bit_size - start_bits);
              a->constant = p.val;
              a->constant = p.val;
              break;
              break;
            }
            }
        }
        }
      else
      else
        {
        {
          p = makelongparameter (allWords, inst_bit_size -
          p = makelongparameter (allWords, inst_bit_size -
                                 (start_bits + a->size),
                                 (start_bits + a->size),
                                 inst_bit_size - start_bits);
                                 inst_bit_size - start_bits);
          a->constant = p.val;
          a->constant = p.val;
        }
        }
      break;
      break;
 
 
    default:
    default:
      break;
      break;
    }
    }
}
}
 
 
/*  Print a single argument.  */
/*  Print a single argument.  */
 
 
static void
static void
print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info)
print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info)
{
{
  LONGLONG longdisp, mask;
  LONGLONG longdisp, mask;
  int sign_flag = 0;
  int sign_flag = 0;
  int relative = 0;
  int relative = 0;
  bfd_vma number;
  bfd_vma number;
  PTR stream = info->stream;
  PTR stream = info->stream;
  fprintf_ftype func = info->fprintf_func;
  fprintf_ftype func = info->fprintf_func;
 
 
  switch (a->type)
  switch (a->type)
    {
    {
    case arg_r:
    case arg_r:
      func (stream, "%s", getregname (a->r));
      func (stream, "%s", getregname (a->r));
      break;
      break;
 
 
    case arg_rp:
    case arg_rp:
      func (stream, "%s", getregpname (a->rp));
      func (stream, "%s", getregpname (a->rp));
      break;
      break;
 
 
    case arg_pr:
    case arg_pr:
      func (stream, "%s", getprocregname (a->pr));
      func (stream, "%s", getprocregname (a->pr));
      break;
      break;
 
 
    case arg_prp:
    case arg_prp:
      func (stream, "%s", getprocpregname (a->prp));
      func (stream, "%s", getprocpregname (a->prp));
      break;
      break;
 
 
    case arg_cc:
    case arg_cc:
      func (stream, "%s", getccstring (a->cc));
      func (stream, "%s", getccstring (a->cc));
      func (stream, "%s", "\t");
      func (stream, "%s", "\t");
      break;
      break;
 
 
    case arg_ic:
    case arg_ic:
      if (IS_INSN_MNEMONIC ("excp"))
      if (IS_INSN_MNEMONIC ("excp"))
        {
        {
          func (stream, "%s", gettrapstring (a->constant));
          func (stream, "%s", gettrapstring (a->constant));
          break;
          break;
        }
        }
      else if ((IS_INSN_TYPE (ARITH_INS) || IS_INSN_TYPE (ARITH_BYTE_INS))
      else if ((IS_INSN_TYPE (ARITH_INS) || IS_INSN_TYPE (ARITH_BYTE_INS))
               && ((instruction->size == 1) && (a->constant == 9)))
               && ((instruction->size == 1) && (a->constant == 9)))
        func (stream, "$%d", -1);
        func (stream, "$%d", -1);
      else if (INST_HAS_REG_LIST)
      else if (INST_HAS_REG_LIST)
        func (stream, "$0x%lx", a->constant +1);
        func (stream, "$0x%lx", a->constant +1);
      else if (IS_INSN_TYPE (SHIFT_INS))
      else if (IS_INSN_TYPE (SHIFT_INS))
        {
        {
          longdisp = a->constant;
          longdisp = a->constant;
          mask = ((LONGLONG)1 << a->size) - 1;
          mask = ((LONGLONG)1 << a->size) - 1;
          if (longdisp & ((LONGLONG)1 << (a->size -1)))
          if (longdisp & ((LONGLONG)1 << (a->size -1)))
            {
            {
              sign_flag = 1;
              sign_flag = 1;
              longdisp = ~(longdisp) + 1;
              longdisp = ~(longdisp) + 1;
            }
            }
          a->constant = (unsigned long int) (longdisp & mask);
          a->constant = (unsigned long int) (longdisp & mask);
          func (stream, "$%d", ((int)(sign_flag ? -a->constant :
          func (stream, "$%d", ((int)(sign_flag ? -a->constant :
                                      a->constant)));
                                      a->constant)));
        }
        }
      else
      else
        func (stream, "$0x%lx", a->constant);
        func (stream, "$0x%lx", a->constant);
      switch (a->size)
      switch (a->size)
        {
        {
        case 4  : case 5  : case 6  : case 8  :
        case 4  : case 5  : case 6  : case 8  :
          func (stream, "%s", ":s"); break;
          func (stream, "%s", ":s"); break;
        case 16 : case 20 : func (stream, "%s", ":m"); break;
        case 16 : case 20 : func (stream, "%s", ":m"); break;
        case 24 : case 32 : func (stream, "%s", ":l"); break;
        case 24 : case 32 : func (stream, "%s", ":l"); break;
        default: break;
        default: break;
        }
        }
      break;
      break;
 
 
    case arg_idxr:
    case arg_idxr:
      if (a->i_r == 0) func (stream, "[r12]");
      if (a->i_r == 0) func (stream, "[r12]");
      if (a->i_r == 1) func (stream, "[r13]");
      if (a->i_r == 1) func (stream, "[r13]");
      func (stream, "0x%lx", a->constant);
      func (stream, "0x%lx", a->constant);
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", print_exp_len (instruction->size * 16));
      break;
      break;
 
 
    case arg_idxrp:
    case arg_idxrp:
      if (a->i_r == 0) func (stream, "[r12]");
      if (a->i_r == 0) func (stream, "[r12]");
      if (a->i_r == 1) func (stream, "[r13]");
      if (a->i_r == 1) func (stream, "[r13]");
      func (stream, "0x%lx", a->constant);
      func (stream, "0x%lx", a->constant);
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", getidxregpname (a->rp));
      func (stream, "%s", getidxregpname (a->rp));
      break;
      break;
 
 
    case arg_rbase:
    case arg_rbase:
      func (stream, "(%s)", getregname (a->r));
      func (stream, "(%s)", getregname (a->r));
      break;
      break;
 
 
    case arg_cr:
    case arg_cr:
      func (stream, "0x%lx", a->constant);
      func (stream, "0x%lx", a->constant);
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "(%s)", getregname (a->r));
      func (stream, "(%s)", getregname (a->r));
      break;
      break;
 
 
    case arg_crp:
    case arg_crp:
      func (stream, "0x%lx", a->constant);
      func (stream, "0x%lx", a->constant);
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", getregpname (a->rp));
      func (stream, "%s", getregpname (a->rp));
      break;
      break;
 
 
    case arg_c:
    case arg_c:
      /*Removed the *2 part as because implicit zeros are no more required.
      /*Removed the *2 part as because implicit zeros are no more required.
        Have to fix this as this needs a bit of extension in terms of branch
        Have to fix this as this needs a bit of extension in terms of branch
        instructions. */
        instructions. */
      if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal"))
      if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal"))
        {
        {
          relative = 1;
          relative = 1;
          longdisp = a->constant;
          longdisp = a->constant;
          /* REVISIT: To sync with WinIDEA and CR16 4.1tools, the below
          /* REVISIT: To sync with WinIDEA and CR16 4.1tools, the below
             line commented */
             line commented */
          /* longdisp <<= 1; */
          /* longdisp <<= 1; */
          mask = ((LONGLONG)1 << a->size) - 1;
          mask = ((LONGLONG)1 << a->size) - 1;
          switch (a->size)
          switch (a->size)
            {
            {
            case 8  :
            case 8  :
              {
              {
                longdisp <<= 1;
                longdisp <<= 1;
                if (longdisp & ((LONGLONG)1 << a->size))
                if (longdisp & ((LONGLONG)1 << a->size))
                  {
                  {
                    sign_flag = 1;
                    sign_flag = 1;
                    longdisp = ~(longdisp) + 1;
                    longdisp = ~(longdisp) + 1;
                  }
                  }
                break;
                break;
              }
              }
            case 16 :
            case 16 :
            case 24 :
            case 24 :
              {
              {
                if (longdisp & 1)
                if (longdisp & 1)
                  {
                  {
                    sign_flag = 1;
                    sign_flag = 1;
                    longdisp = ~(longdisp) + 1;
                    longdisp = ~(longdisp) + 1;
                  }
                  }
                break;
                break;
              }
              }
            default:
            default:
              func (stream, "Wrong offset used in branch/bal instruction");
              func (stream, "Wrong offset used in branch/bal instruction");
              break;
              break;
            }
            }
          a->constant = (unsigned long int) (longdisp & mask);
          a->constant = (unsigned long int) (longdisp & mask);
        }
        }
      /* For branch Neq instruction it is 2*offset + 2.  */
      /* For branch Neq instruction it is 2*offset + 2.  */
      else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
      else if (IS_INSN_TYPE (BRANCH_NEQ_INS))
        a->constant = 2 * a->constant + 2;
        a->constant = 2 * a->constant + 2;
 
 
      if ((!IS_INSN_TYPE (CSTBIT_INS)) && (!IS_INSN_TYPE (LD_STOR_INS)))
      if ((!IS_INSN_TYPE (CSTBIT_INS)) && (!IS_INSN_TYPE (LD_STOR_INS)))
        (sign_flag) ? func (stream, "%s", "*-"): func (stream, "%s","*+");
        (sign_flag) ? func (stream, "%s", "*-"): func (stream, "%s","*+");
 
 
      /* PR 10173: Avoid printing the 0x prefix twice.  */
      /* PR 10173: Avoid printing the 0x prefix twice.  */
      if (info->num_symbols > 0)
      if (info->num_symbols > 0)
        func (stream, "%s", "0x");
        func (stream, "%s", "0x");
      number = ((relative ? memaddr : 0) +
      number = ((relative ? memaddr : 0) +
                (sign_flag ? ((- a->constant) & 0xffffffe) : a->constant));
                (sign_flag ? ((- a->constant) & 0xffffffe) : a->constant));
 
 
      (*info->print_address_func) ((number & ((1 << 24) - 1)), info);
      (*info->print_address_func) ((number & ((1 << 24) - 1)), info);
 
 
      func (stream, "%s", print_exp_len (instruction->size * 16));
      func (stream, "%s", print_exp_len (instruction->size * 16));
      break;
      break;
 
 
    default:
    default:
      break;
      break;
    }
    }
}
}
 
 
/* Print all the arguments of CURRINSN instruction.  */
/* Print all the arguments of CURRINSN instruction.  */
 
 
static void
static void
print_arguments (ins *currInsn, bfd_vma memaddr, struct disassemble_info *info)
print_arguments (ins *currInsn, bfd_vma memaddr, struct disassemble_info *info)
{
{
  int i;
  int i;
 
 
  /* For "pop/push/popret RA instruction only.  */
  /* For "pop/push/popret RA instruction only.  */
  if ((IS_INSN_MNEMONIC ("pop")
  if ((IS_INSN_MNEMONIC ("pop")
       || (IS_INSN_MNEMONIC ("popret")
       || (IS_INSN_MNEMONIC ("popret")
           || (IS_INSN_MNEMONIC ("push"))))
           || (IS_INSN_MNEMONIC ("push"))))
      && currInsn->nargs == 1)
      && currInsn->nargs == 1)
    {
    {
      info->fprintf_func (info->stream, "RA");
      info->fprintf_func (info->stream, "RA");
      return;
      return;
    }
    }
 
 
  for (i = 0; i < currInsn->nargs; i++)
  for (i = 0; i < currInsn->nargs; i++)
    {
    {
      processing_argument_number = i;
      processing_argument_number = i;
 
 
      /* For "bal (ra), disp17" instruction only.  */
      /* For "bal (ra), disp17" instruction only.  */
      if ((IS_INSN_MNEMONIC ("bal")) && (i == 0) && instruction->size == 2)
      if ((IS_INSN_MNEMONIC ("bal")) && (i == 0) && instruction->size == 2)
        {
        {
          info->fprintf_func (info->stream, "(ra),");
          info->fprintf_func (info->stream, "(ra),");
          continue;
          continue;
        }
        }
 
 
      if ((INST_HAS_REG_LIST) && (i == 2))
      if ((INST_HAS_REG_LIST) && (i == 2))
        info->fprintf_func (info->stream, "RA");
        info->fprintf_func (info->stream, "RA");
      else
      else
        print_arg (&currInsn->arg[i], memaddr, info);
        print_arg (&currInsn->arg[i], memaddr, info);
 
 
      if ((i != currInsn->nargs - 1) && (!IS_INSN_MNEMONIC ("b")))
      if ((i != currInsn->nargs - 1) && (!IS_INSN_MNEMONIC ("b")))
        info->fprintf_func (info->stream, ",");
        info->fprintf_func (info->stream, ",");
    }
    }
}
}
 
 
/* Build the instruction's arguments.  */
/* Build the instruction's arguments.  */
 
 
static void
static void
make_instruction (void)
make_instruction (void)
{
{
  int i;
  int i;
  unsigned int shift;
  unsigned int shift;
 
 
  for (i = 0; i < currInsn.nargs; i++)
  for (i = 0; i < currInsn.nargs; i++)
    {
    {
      argument a;
      argument a;
 
 
      memset (&a, 0, sizeof (a));
      memset (&a, 0, sizeof (a));
      a.type = getargtype (instruction->operands[i].op_type);
      a.type = getargtype (instruction->operands[i].op_type);
      a.size = getbits (instruction->operands[i].op_type);
      a.size = getbits (instruction->operands[i].op_type);
      shift = instruction->operands[i].shift;
      shift = instruction->operands[i].shift;
 
 
      make_argument (&a, shift);
      make_argument (&a, shift);
      currInsn.arg[i] = a;
      currInsn.arg[i] = a;
    }
    }
 
 
  /* Calculate instruction size (in bytes).  */
  /* Calculate instruction size (in bytes).  */
  currInsn.size = instruction->size + (size_changed ? 1 : 0);
  currInsn.size = instruction->size + (size_changed ? 1 : 0);
  /* Now in bits.  */
  /* Now in bits.  */
  currInsn.size *= 2;
  currInsn.size *= 2;
}
}
 
 
/* Retrieve a single word from a given memory address.  */
/* Retrieve a single word from a given memory address.  */
 
 
static wordU
static wordU
get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info)
get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info)
{
{
  bfd_byte buffer[4];
  bfd_byte buffer[4];
  int status;
  int status;
  wordU insn = 0;
  wordU insn = 0;
 
 
  status = info->read_memory_func (memaddr, buffer, 2, info);
  status = info->read_memory_func (memaddr, buffer, 2, info);
 
 
  if (status == 0)
  if (status == 0)
    insn = (wordU) bfd_getl16 (buffer);
    insn = (wordU) bfd_getl16 (buffer);
 
 
  return insn;
  return insn;
}
}
 
 
/* Retrieve multiple words (3) from a given memory address.  */
/* Retrieve multiple words (3) from a given memory address.  */
 
 
static void
static void
get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info)
get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info)
{
{
  int i;
  int i;
  bfd_vma mem;
  bfd_vma mem;
 
 
  for (i = 0, mem = memaddr; i < 3; i++, mem += 2)
  for (i = 0, mem = memaddr; i < 3; i++, mem += 2)
    words[i] = get_word_at_PC (mem, info);
    words[i] = get_word_at_PC (mem, info);
 
 
  allWords =
  allWords =
    ((ULONGLONG) words[0] << 32) + ((unsigned long) words[1] << 16) + words[2];
    ((ULONGLONG) words[0] << 32) + ((unsigned long) words[1] << 16) + words[2];
}
}
 
 
/* Prints the instruction by calling print_arguments after proper matching.  */
/* Prints the instruction by calling print_arguments after proper matching.  */
 
 
int
int
print_insn_cr16 (bfd_vma memaddr, struct disassemble_info *info)
print_insn_cr16 (bfd_vma memaddr, struct disassemble_info *info)
{
{
  int is_decoded;     /* Nonzero means instruction has a match.  */
  int is_decoded;     /* Nonzero means instruction has a match.  */
 
 
  /* Initialize global variables.  */
  /* Initialize global variables.  */
  imm4flag = 0;
  imm4flag = 0;
  size_changed = 0;
  size_changed = 0;
 
 
  /* Retrieve the encoding from current memory location.  */
  /* Retrieve the encoding from current memory location.  */
  get_words_at_PC (memaddr, info);
  get_words_at_PC (memaddr, info);
  /* Find a matching opcode in table.  */
  /* Find a matching opcode in table.  */
  is_decoded = match_opcode ();
  is_decoded = match_opcode ();
  /* If found, print the instruction's mnemonic and arguments.  */
  /* If found, print the instruction's mnemonic and arguments.  */
  if (is_decoded > 0 && (words[0] << 16 || words[1]) != 0)
  if (is_decoded > 0 && (words[0] << 16 || words[1]) != 0)
    {
    {
      if (strneq (instruction->mnemonic, "cinv", 4))
      if (strneq (instruction->mnemonic, "cinv", 4))
        info->fprintf_func (info->stream,"%s", getcinvstring (instruction->mnemonic));
        info->fprintf_func (info->stream,"%s", getcinvstring (instruction->mnemonic));
      else
      else
        info->fprintf_func (info->stream, "%s", instruction->mnemonic);
        info->fprintf_func (info->stream, "%s", instruction->mnemonic);
 
 
      if (((currInsn.nargs = get_number_of_operands ()) != 0)
      if (((currInsn.nargs = get_number_of_operands ()) != 0)
          && ! (IS_INSN_MNEMONIC ("b")))
          && ! (IS_INSN_MNEMONIC ("b")))
        info->fprintf_func (info->stream, "\t");
        info->fprintf_func (info->stream, "\t");
      make_instruction ();
      make_instruction ();
      /* For push/pop/pushrtn with RA instructions.  */
      /* For push/pop/pushrtn with RA instructions.  */
      if ((INST_HAS_REG_LIST) && ((words[0] >> 7) & 0x1))
      if ((INST_HAS_REG_LIST) && ((words[0] >> 7) & 0x1))
        currInsn.nargs +=1;
        currInsn.nargs +=1;
      print_arguments (&currInsn, memaddr, info);
      print_arguments (&currInsn, memaddr, info);
      return currInsn.size;
      return currInsn.size;
    }
    }
 
 
  /* No match found.  */
  /* No match found.  */
  info->fprintf_func (info->stream,"%s ",ILLEGAL);
  info->fprintf_func (info->stream,"%s ",ILLEGAL);
  return 2;
  return 2;
}
}
 
 

powered by: WebSVN 2.1.0

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