#include "sysdep.h"
#include "dis-asm.h"
#include "opcode/arc.h"
#include "elf-bfd.h"
#include "elf/arc.h"
#include "opintl.h"
static int print_insn_arc_base_little PARAMS ((bfd_vma, disassemble_info *));
static int print_insn_arc_base_big PARAMS ((bfd_vma, disassemble_info *));
static int print_insn PARAMS ((bfd_vma, disassemble_info *, int, int));
static int
print_insn (pc, info, mach, big_p)
bfd_vma pc;
disassemble_info *info;
int mach;
int big_p;
{
const struct arc_opcode *opcode;
bfd_byte buffer[4];
void *stream = info->stream;
fprintf_ftype func = info->fprintf_func;
int status;
arc_insn insn[2];
int got_limm_p = 0;
static int initialized = 0;
static int current_mach = 0;
if (!initialized || mach != current_mach)
{
initialized = 1;
current_mach = arc_get_opcode_mach (mach, big_p);
arc_opcode_init_tables (current_mach);
}
status = (*info->read_memory_func) (pc, buffer, 4, info);
if (status != 0)
{
(*info->memory_error_func) (status, pc, info);
return -1;
}
if (big_p)
insn[0] = bfd_getb32 (buffer);
else
insn[0] = bfd_getl32 (buffer);
(*func) (stream, "%08lx\t", insn[0]);
opcode = arc_opcode_lookup_dis (insn[0]);
for ( ; opcode != NULL; opcode = ARC_OPCODE_NEXT_DIS (opcode))
{
char *syn;
int mods,invalid;
long value;
const struct arc_operand *operand;
const struct arc_operand_value *opval;
if ((insn[0] & opcode->mask) != opcode->value)
continue;
if (! arc_opcode_supported (opcode))
continue;
arc_opcode_init_extract ();
invalid = 0;
for (syn = opcode->syntax; *syn; ++syn)
{
int c;
if (*syn != '%' || *++syn == '%')
continue;
mods = 0;
c = *syn;
while (ARC_MOD_P (arc_operands[arc_operand_map[c]].flags))
{
mods |= arc_operands[arc_operand_map[c]].flags & ARC_MOD_BITS;
++syn;
c = *syn;
}
operand = arc_operands + arc_operand_map[c];
if (operand->extract)
(*operand->extract) (insn, operand, mods,
(const struct arc_operand_value **) NULL,
&invalid);
}
if (invalid)
continue;
if (arc_opcode_limm_p (NULL))
{
status = (*info->read_memory_func) (pc + 4, buffer, 4, info);
if (status != 0)
{
(*info->memory_error_func) (status, pc, info);
return -1;
}
if (big_p)
insn[1] = bfd_getb32 (buffer);
else
insn[1] = bfd_getl32 (buffer);
got_limm_p = 1;
}
for (syn = opcode->syntax; *syn; ++syn)
{
int c;
if (*syn != '%' || *++syn == '%')
{
(*func) (stream, "%c", *syn);
continue;
}
mods = 0;
c = *syn;
while (ARC_MOD_P (arc_operands[arc_operand_map[c]].flags))
{
mods |= arc_operands[arc_operand_map[c]].flags & ARC_MOD_BITS;
++syn;
c = *syn;
}
operand = arc_operands + arc_operand_map[c];
opval = NULL;
if (operand->extract)
{
value = (*operand->extract) (insn, operand, mods,
&opval, (int *) NULL);
}
else
{
value = (insn[0] >> operand->shift) & ((1 << operand->bits) - 1);
if ((operand->flags & ARC_OPERAND_SIGNED)
&& (value & (1 << (operand->bits - 1))))
value -= 1 << operand->bits;
if (operand->flags & ARC_OPERAND_SUFFIX)
opval = arc_opcode_lookup_suffix (operand, value);
}
if (operand->flags & ARC_OPERAND_FAKE)
;
else if (operand->flags & ARC_OPERAND_SUFFIX)
{
if (value != 0)
{
if (opval)
(*func) (stream, "%s%s",
mods & ARC_MOD_DOT ? "." : "",
opval->name);
else
(*func) (stream, "%s%c%d",
mods & ARC_MOD_DOT ? "." : "",
operand->fmt, value);
}
}
else if (operand->flags & ARC_OPERAND_RELATIVE_BRANCH)
(*info->print_address_func) (pc + 4 + value, info);
else if (operand->flags & ARC_OPERAND_ABSOLUTE_BRANCH)
(*info->print_address_func) ((bfd_vma) value & 0xffffffff, info);
else if (operand->flags & ARC_OPERAND_ADDRESS)
(*info->print_address_func) ((bfd_vma) value & 0xffffffff, info);
else if (opval)
(*func) (stream, "%s", opval->name);
else
(*func) (stream, "%ld", value);
}
return got_limm_p ? 8 : 4;
}
(*func) (stream, _("*unknown*"));
return 4;
}
disassembler_ftype
arc_get_disassembler (mach, big_p)
int mach;
int big_p;
{
switch (mach)
{
case bfd_mach_arc_base:
return big_p ? print_insn_arc_base_big : print_insn_arc_base_little;
}
return print_insn_arc_base_little;
}
static int
print_insn_arc_base_little (pc, info)
bfd_vma pc;
disassemble_info *info;
{
return print_insn (pc, info, bfd_mach_arc_base, 0);
}
static int
print_insn_arc_base_big (pc, info)
bfd_vma pc;
disassemble_info *info;
{
return print_insn (pc, info, bfd_mach_arc_base, 1);
}