1 /* Disassemble AVR instructions.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This file is part of libopcodes.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "libiberty.h"
34 int insn_size; /* In words. */
36 unsigned int bin_opcode;
39 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
40 {#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
42 const struct avr_opcodes_s avr_opcodes[] =
44 #include "opcode/avr.h"
45 {NULL, NULL, NULL, 0, 0, 0}
48 static const char * comment_start = "0x";
51 avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
52 char *opcode_str, char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
59 /* Any register operand. */
62 insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register. */
64 insn = (insn & 0x01f0) >> 4; /* Destination register. */
66 sprintf (buf, "r%d", insn);
71 sprintf (buf, "r%d", 16 + (insn & 0xf));
73 sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
77 sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
82 sprintf (buf, "r%d", 16 + (insn & 7));
84 sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
89 sprintf (buf, "r%d", (insn & 0xf) * 2);
91 sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
98 switch (insn & 0x100f)
100 case 0x0000: xyz = "Z"; break;
101 case 0x1001: xyz = "Z+"; break;
102 case 0x1002: xyz = "-Z"; break;
103 case 0x0008: xyz = "Y"; break;
104 case 0x1009: xyz = "Y+"; break;
105 case 0x100a: xyz = "-Y"; break;
106 case 0x100c: xyz = "X"; break;
107 case 0x100d: xyz = "X+"; break;
108 case 0x100e: xyz = "-X"; break;
109 default: xyz = "??"; ok = 0;
113 if (AVR_UNDEF_P (insn))
114 sprintf (comment, _("undefined"));
121 /* Check for post-increment. */
123 for (s = opcode_str; *s; ++s)
127 if (insn & (1 << (15 - (s - opcode_str))))
134 if (AVR_UNDEF_P (insn))
135 sprintf (comment, _("undefined"));
143 x |= (insn >> 7) & (3 << 3);
144 x |= (insn >> 8) & (1 << 5);
150 sprintf (buf, "+%d", x);
151 sprintf (comment, "0x%02x", x);
157 *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
158 /* See PR binutils/2454. Ideally we would like to display the hex
159 value of the address only once, but this would mean recoding
160 objdump_print_address() which would affect many targets. */
161 sprintf (buf, "%#lx", (unsigned long) *sym_addr);
162 strcpy (comment, comment_start);
167 int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
168 sprintf (buf, ".%+-8d", rel_addr);
170 *sym_addr = pc + 2 + rel_addr;
171 strcpy (comment, comment_start);
177 int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
179 sprintf (buf, ".%+-8d", rel_addr);
181 *sym_addr = pc + 2 + rel_addr;
182 strcpy (comment, comment_start);
187 sprintf (buf, "0x%04X", insn2);
191 sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
192 sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
197 fprintf (stderr, _("Internal disassembler error"));
205 x = (insn & 0xf) | ((insn >> 2) & 0x30);
206 sprintf (buf, "0x%02x", x);
207 sprintf (comment, "%d", x);
212 sprintf (buf, "%d", insn & 7);
216 sprintf (buf, "%d", (insn >> 4) & 7);
224 x |= (insn >> 5) & 0x30;
225 sprintf (buf, "0x%02x", x);
226 sprintf (comment, "%d", x);
234 x = (insn >> 3) & 0x1f;
235 sprintf (buf, "0x%02x", x);
236 sprintf (comment, "%d", x);
241 sprintf (buf, "%d", (insn >> 4) & 15);
250 fprintf (stderr, _("unknown constraint `%c'"), constraint);
257 static unsigned short
258 avrdis_opcode (bfd_vma addr, disassemble_info *info)
263 status = info->read_memory_func (addr, buffer, 2, info);
266 return bfd_getl16 (buffer);
268 info->memory_error_func (status, addr, info);
274 print_insn_avr (bfd_vma addr, disassemble_info *info)
276 unsigned int insn, insn2;
277 const struct avr_opcodes_s *opcode;
278 static unsigned int *maskptr;
279 void *stream = info->stream;
280 fprintf_ftype prin = info->fprintf_func;
281 static unsigned int *avr_bin_masks;
282 static int initialized;
285 char op1[20], op2[20], comment1[40], comment2[40];
286 int sym_op1 = 0, sym_op2 = 0;
287 bfd_vma sym_addr1, sym_addr2;
292 unsigned int nopcodes;
294 /* PR 4045: Try to avoid duplicating the 0x prefix that
295 objdump_print_addr() will put on addresses when there
296 is no symbol table available. */
297 if (info->symtab_size == 0)
300 nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
302 avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
304 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
309 unsigned int bin = 0;
310 unsigned int mask = 0;
312 for (s = opcode->opcode; *s; ++s)
317 mask |= (*s == '1' || *s == '0');
319 assert (s - opcode->opcode == 16);
320 assert (opcode->bin_opcode == bin);
327 insn = avrdis_opcode (addr, info);
329 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
332 if ((insn & *maskptr) == opcode->bin_opcode)
335 /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
336 `std b+0,r' as `st b,r' (next entry in the table). */
338 if (AVR_DISP0_P (insn))
348 char *constraints = opcode->constraints;
349 char *opcode_str = opcode->opcode;
354 if (opcode->insn_size > 1)
356 insn2 = avrdis_opcode (addr + 2, info);
360 if (*constraints && *constraints != '?')
362 int regs = REGISTER_P (*constraints);
364 ok = avr_operand (insn, insn2, addr, *constraints, opcode_str, op1, comment1, 0, &sym_op1, &sym_addr1);
366 if (ok && *(++constraints) == ',')
367 ok = avr_operand (insn, insn2, addr, *(++constraints), opcode_str, op2,
368 *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
374 /* Unknown opcode, or invalid combination of operands. */
375 sprintf (op1, "0x%04x", insn);
377 sprintf (comment1, "????");
381 (*prin) (stream, "%s", ok ? opcode->name : ".word");
384 (*prin) (stream, "\t%s", op1);
387 (*prin) (stream, ", %s", op2);
390 (*prin) (stream, "\t; %s", comment1);
393 info->print_address_func (sym_addr1, info);
396 (*prin) (stream, " %s", comment2);
399 info->print_address_func (sym_addr2, info);