1 /* Disassemble AVR instructions.
2 Copyright 1999, 2000, 2002, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 Contributed by Denis Chertykov <denisc@overta.ru>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include "libiberty.h"
32 int insn_size; /* In words. */
34 unsigned int bin_opcode;
37 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38 {#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
40 const struct avr_opcodes_s avr_opcodes[] =
42 #include "opcode/avr.h"
43 {NULL, NULL, NULL, 0, 0, 0}
46 static const char * comment_start = "0x";
49 avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
50 char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
57 /* Any register operand. */
60 insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register. */
62 insn = (insn & 0x01f0) >> 4; /* Destination register. */
64 sprintf (buf, "r%d", insn);
69 sprintf (buf, "r%d", 16 + (insn & 0xf));
71 sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
75 sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
80 sprintf (buf, "r%d", 16 + (insn & 7));
82 sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
87 sprintf (buf, "r%d", (insn & 0xf) * 2);
89 sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
96 switch (insn & 0x100f)
98 case 0x0000: xyz = "Z"; break;
99 case 0x1001: xyz = "Z+"; break;
100 case 0x1002: xyz = "-Z"; break;
101 case 0x0008: xyz = "Y"; break;
102 case 0x1009: xyz = "Y+"; break;
103 case 0x100a: xyz = "-Y"; break;
104 case 0x100c: xyz = "X"; break;
105 case 0x100d: xyz = "X+"; break;
106 case 0x100e: xyz = "-X"; break;
107 default: xyz = "??"; ok = 0;
111 if (AVR_UNDEF_P (insn))
112 sprintf (comment, _("undefined"));
121 if (AVR_UNDEF_P (insn))
122 sprintf (comment, _("undefined"));
130 x |= (insn >> 7) & (3 << 3);
131 x |= (insn >> 8) & (1 << 5);
137 sprintf (buf, "+%d", x);
138 sprintf (comment, "0x%02x", x);
144 *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
145 /* See PR binutils/2454. Ideally we would like to display the hex
146 value of the address only once, but this would mean recoding
147 objdump_print_address() which would affect many targets. */
148 sprintf (buf, "%#lx", (unsigned long) *sym_addr);
149 sprintf (comment, comment_start);
154 int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
155 sprintf (buf, ".%+-8d", rel_addr);
157 *sym_addr = pc + 2 + rel_addr;
158 sprintf (comment, comment_start);
164 int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
166 sprintf (buf, ".%+-8d", rel_addr);
168 *sym_addr = pc + 2 + rel_addr;
169 sprintf (comment, comment_start);
174 sprintf (buf, "0x%04X", insn2);
178 sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
179 sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
184 fprintf (stderr, _("Internal disassembler error"));
192 x = (insn & 0xf) | ((insn >> 2) & 0x30);
193 sprintf (buf, "0x%02x", x);
194 sprintf (comment, "%d", x);
199 sprintf (buf, "%d", insn & 7);
203 sprintf (buf, "%d", (insn >> 4) & 7);
211 x |= (insn >> 5) & 0x30;
212 sprintf (buf, "0x%02x", x);
213 sprintf (comment, "%d", x);
221 x = (insn >> 3) & 0x1f;
222 sprintf (buf, "0x%02x", x);
223 sprintf (comment, "%d", x);
233 fprintf (stderr, _("unknown constraint `%c'"), constraint);
240 static unsigned short
241 avrdis_opcode (bfd_vma addr, disassemble_info *info)
246 status = info->read_memory_func (addr, buffer, 2, info);
249 return bfd_getl16 (buffer);
251 info->memory_error_func (status, addr, info);
257 print_insn_avr (bfd_vma addr, disassemble_info *info)
259 unsigned int insn, insn2;
260 const struct avr_opcodes_s *opcode;
261 static unsigned int *maskptr;
262 void *stream = info->stream;
263 fprintf_ftype prin = info->fprintf_func;
264 static unsigned int *avr_bin_masks;
265 static int initialized;
268 char op1[20], op2[20], comment1[40], comment2[40];
269 int sym_op1 = 0, sym_op2 = 0;
270 bfd_vma sym_addr1, sym_addr2;
275 unsigned int nopcodes;
277 /* PR 4045: Try to avoid duplicating the 0x prefix that
278 objdump_print_addr() will put on addresses when there
279 is no symbol table available. */
280 if (info->symtab_size == 0)
283 nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
285 avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
287 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
292 unsigned int bin = 0;
293 unsigned int mask = 0;
295 for (s = opcode->opcode; *s; ++s)
300 mask |= (*s == '1' || *s == '0');
302 assert (s - opcode->opcode == 16);
303 assert (opcode->bin_opcode == bin);
310 insn = avrdis_opcode (addr, info);
312 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
315 if ((insn & *maskptr) == opcode->bin_opcode)
318 /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
319 `std b+0,r' as `st b,r' (next entry in the table). */
321 if (AVR_DISP0_P (insn))
331 char *op = opcode->constraints;
336 if (opcode->insn_size > 1)
338 insn2 = avrdis_opcode (addr + 2, info);
342 if (*op && *op != '?')
344 int regs = REGISTER_P (*op);
346 ok = avr_operand (insn, insn2, addr, *op, op1, comment1, 0, &sym_op1, &sym_addr1);
348 if (ok && *(++op) == ',')
349 ok = avr_operand (insn, insn2, addr, *(++op), op2,
350 *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
356 /* Unknown opcode, or invalid combination of operands. */
357 sprintf (op1, "0x%04x", insn);
359 sprintf (comment1, "????");
363 (*prin) (stream, "%s", ok ? opcode->name : ".word");
366 (*prin) (stream, "\t%s", op1);
369 (*prin) (stream, ", %s", op2);
372 (*prin) (stream, "\t; %s", comment1);
375 info->print_address_func (sym_addr1, info);
378 (*prin) (stream, " %s", comment2);
381 info->print_address_func (sym_addr2, info);