/* Disassemble AVR instructions.
- Copyright (C) 1999-2015 Free Software Foundation, Inc.
+ Copyright (C) 1999-2017 Free Software Foundation, Inc.
Contributed by Denis Chertykov <denisc@overta.ru>
#include "dis-asm.h"
#include "opintl.h"
#include "libiberty.h"
+#include "bfd_stdint.h"
struct avr_opcodes_s
{
insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register. */
else
insn = (insn & 0x01f0) >> 4; /* Destination register. */
-
+
sprintf (buf, "r%d", insn);
break;
else
sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
break;
-
+
case 'w':
sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
break;
-
+
case 'a':
if (regs)
sprintf (buf, "r%d", 16 + (insn & 7));
case 'b':
{
unsigned int x;
-
+
x = (insn & 7);
x |= (insn >> 7) & (3 << 3);
x |= (insn >> 8) & (1 << 5);
-
+
if (insn & 0x8)
*buf++ = 'Y';
else
sprintf (comment, "0x%02x", x);
}
break;
-
+
case 'h':
*sym = 1;
*sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
/* See PR binutils/2454. Ideally we would like to display the hex
value of the address only once, but this would mean recoding
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);
break;
-
+
case 'L':
{
int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
break;
case 'i':
- sprintf (buf, "0x%04X", insn2);
+ {
+ unsigned int val = insn2 | 0x800000;
+ *sym = 1;
+ *sym_addr = val;
+ sprintf (buf, "0x%04X", insn2);
+ strcpy (comment, comment_start);
+ }
break;
case 'j':
{
unsigned int val = ((insn & 0xf) | ((insn & 0x600) >> 5)
| ((insn & 0x100) >> 2));
- if (val > 0 && !(insn & 0x100))
- val |= 0x80;
+ *sym = 1;
+ *sym_addr = val | 0x800000;
sprintf (buf, "0x%02x", val);
- sprintf (buf, "%d", val);
+ strcpy (comment, comment_start);
}
break;
-
+
case 'M':
sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
fprintf (stderr, _("Internal disassembler error"));
ok = 0;
break;
-
+
case 'K':
{
unsigned int x;
sprintf (comment, "%d", x);
}
break;
-
+
case 's':
sprintf (buf, "%d", insn & 7);
break;
-
+
case 'S':
sprintf (buf, "%d", (insn >> 4) & 7);
break;
-
+
case 'P':
{
unsigned int x;
case 'p':
{
unsigned int x;
-
+
x = (insn >> 3) & 0x1f;
sprintf (buf, "0x%02x", x);
sprintf (comment, "%d", x);
}
break;
-
+
case 'E':
sprintf (buf, "%d", (insn >> 4) & 15);
break;
-
+
case '?':
*buf = '\0';
break;
-
+
default:
sprintf (buf, "??");
fprintf (stderr, _("unknown constraint `%c'"), constraint);
return ok;
}
-static unsigned short
-avrdis_opcode (bfd_vma addr, disassemble_info *info)
+/* Read the opcode from ADDR. Return 0 in success and save opcode
+ in *INSN, otherwise, return -1. */
+
+static int
+avrdis_opcode (bfd_vma addr, disassemble_info *info, uint16_t *insn)
{
bfd_byte buffer[2];
int status;
status = info->read_memory_func (addr, buffer, 2, info);
if (status == 0)
- return bfd_getl16 (buffer);
+ {
+ *insn = bfd_getl16 (buffer);
+ return 0;
+ }
info->memory_error_func (status, addr, info);
return -1;
int
print_insn_avr (bfd_vma addr, disassemble_info *info)
{
- unsigned int insn, insn2;
+ uint16_t insn, insn2;
const struct avr_opcodes_s *opcode;
static unsigned int *maskptr;
void *stream = info->stream;
comment_start = " ";
nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
-
+
avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
for (opcode = avr_opcodes, maskptr = avr_bin_masks;
char * s;
unsigned int bin = 0;
unsigned int mask = 0;
-
+
for (s = opcode->opcode; *s; ++s)
{
bin <<= 1;
initialized = 1;
}
- insn = avrdis_opcode (addr, info);
-
+ if (avrdis_opcode (addr, info, &insn) != 0)
+ return -1;
+
for (opcode = avr_opcodes, maskptr = avr_bin_masks;
opcode->name;
opcode++, maskptr++)
if ((insn & *maskptr) == opcode->bin_opcode)
break;
}
-
+
/* Special case: disassemble `ldd r,b+0' as `ld r,b', and
`std b+0,r' as `st b,r' (next entry in the table). */
if (opcode->insn_size > 1)
{
- insn2 = avrdis_opcode (addr + 2, info);
+ if (avrdis_opcode (addr + 2, info, &insn2) != 0)
+ return -1;
cmd_len = 4;
}