1 /* Disassemble AVR instructions.
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
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. */
25 #include "disassemble.h"
27 #include "libiberty.h"
28 #include "bfd_stdint.h"
35 int insn_size; /* In words. */
37 unsigned int bin_opcode;
40 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
41 {#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
43 const struct avr_opcodes_s avr_opcodes[] =
45 #include "opcode/avr.h"
46 {NULL, NULL, NULL, 0, 0, 0}
49 static const char * comment_start = "0x";
52 avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
53 char *opcode_str, char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
60 /* Any register operand. */
63 insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register. */
65 insn = (insn & 0x01f0) >> 4; /* Destination register. */
67 sprintf (buf, "r%d", insn);
72 sprintf (buf, "r%d", 16 + (insn & 0xf));
74 sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
78 sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
83 sprintf (buf, "r%d", 16 + (insn & 7));
85 sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
90 sprintf (buf, "r%d", (insn & 0xf) * 2);
92 sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
99 switch (insn & 0x100f)
101 case 0x0000: xyz = "Z"; break;
102 case 0x1001: xyz = "Z+"; break;
103 case 0x1002: xyz = "-Z"; break;
104 case 0x0008: xyz = "Y"; break;
105 case 0x1009: xyz = "Y+"; break;
106 case 0x100a: xyz = "-Y"; break;
107 case 0x100c: xyz = "X"; break;
108 case 0x100d: xyz = "X+"; break;
109 case 0x100e: xyz = "-X"; break;
110 default: xyz = "??"; ok = 0;
114 if (AVR_UNDEF_P (insn))
115 sprintf (comment, _("undefined"));
122 /* Check for post-increment. */
124 for (s = opcode_str; *s; ++s)
128 if (insn & (1 << (15 - (s - opcode_str))))
135 if (AVR_UNDEF_P (insn))
136 sprintf (comment, _("undefined"));
144 x |= (insn >> 7) & (3 << 3);
145 x |= (insn >> 8) & (1 << 5);
151 sprintf (buf, "+%d", x);
152 sprintf (comment, "0x%02x", x);
158 *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
159 /* See PR binutils/2454. Ideally we would like to display the hex
160 value of the address only once, but this would mean recoding
161 objdump_print_address() which would affect many targets. */
162 sprintf (buf, "%#lx", (unsigned long) *sym_addr);
163 strcpy (comment, comment_start);
168 int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
169 sprintf (buf, ".%+-8d", rel_addr);
171 *sym_addr = pc + 2 + rel_addr;
172 strcpy (comment, comment_start);
178 int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
180 sprintf (buf, ".%+-8d", rel_addr);
182 *sym_addr = pc + 2 + rel_addr;
183 strcpy (comment, comment_start);
189 unsigned int val = insn2 | 0x800000;
192 sprintf (buf, "0x%04X", insn2);
193 strcpy (comment, comment_start);
199 unsigned int val = ((insn & 0xf) | ((insn & 0x600) >> 5)
200 | ((insn & 0x100) >> 2));
201 if ((insn & 0x100) == 0)
204 *sym_addr = val | 0x800000;
205 sprintf (buf, "0x%02x", val);
206 strcpy (comment, comment_start);
211 sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
212 sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
217 /* xgettext:c-format */
218 opcodes_error_handler (_("internal disassembler error"));
226 x = (insn & 0xf) | ((insn >> 2) & 0x30);
227 sprintf (buf, "0x%02x", x);
228 sprintf (comment, "%d", x);
233 sprintf (buf, "%d", insn & 7);
237 sprintf (buf, "%d", (insn >> 4) & 7);
245 x |= (insn >> 5) & 0x30;
246 sprintf (buf, "0x%02x", x);
247 sprintf (comment, "%d", x);
255 x = (insn >> 3) & 0x1f;
256 sprintf (buf, "0x%02x", x);
257 sprintf (comment, "%d", x);
262 sprintf (buf, "%d", (insn >> 4) & 15);
271 /* xgettext:c-format */
272 opcodes_error_handler (_("unknown constraint `%c'"), constraint);
279 /* Read the opcode from ADDR. Return 0 in success and save opcode
280 in *INSN, otherwise, return -1. */
283 avrdis_opcode (bfd_vma addr, disassemble_info *info, uint16_t *insn)
288 status = info->read_memory_func (addr, buffer, 2, info);
292 *insn = bfd_getl16 (buffer);
296 info->memory_error_func (status, addr, info);
302 print_insn_avr (bfd_vma addr, disassemble_info *info)
304 uint16_t insn, insn2;
305 const struct avr_opcodes_s *opcode;
306 static unsigned int *maskptr;
307 void *stream = info->stream;
308 fprintf_ftype prin = info->fprintf_func;
309 static unsigned int *avr_bin_masks;
310 static int initialized;
313 char op1[20], op2[20], comment1[40], comment2[40];
314 int sym_op1 = 0, sym_op2 = 0;
315 bfd_vma sym_addr1, sym_addr2;
320 unsigned int nopcodes;
322 /* PR 4045: Try to avoid duplicating the 0x prefix that
323 objdump_print_addr() will put on addresses when there
324 is no symbol table available. */
325 if (info->symtab_size == 0)
328 nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
330 avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
332 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
337 unsigned int bin = 0;
338 unsigned int mask = 0;
340 for (s = opcode->opcode; *s; ++s)
345 mask |= (*s == '1' || *s == '0');
347 assert (s - opcode->opcode == 16);
348 assert (opcode->bin_opcode == bin);
355 if (avrdis_opcode (addr, info, &insn) != 0)
358 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
362 if ((opcode->isa == AVR_ISA_TINY) && (info->mach != bfd_mach_avrtiny))
364 if ((insn & *maskptr) == opcode->bin_opcode)
368 /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
369 `std b+0,r' as `st b,r' (next entry in the table). */
371 if (AVR_DISP0_P (insn))
381 char *constraints = opcode->constraints;
382 char *opcode_str = opcode->opcode;
387 if (opcode->insn_size > 1)
389 if (avrdis_opcode (addr + 2, info, &insn2) != 0)
394 if (*constraints && *constraints != '?')
396 int regs = REGISTER_P (*constraints);
398 ok = avr_operand (insn, insn2, addr, *constraints, opcode_str, op1, comment1, 0, &sym_op1, &sym_addr1);
400 if (ok && *(++constraints) == ',')
401 ok = avr_operand (insn, insn2, addr, *(++constraints), opcode_str, op2,
402 *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
408 /* Unknown opcode, or invalid combination of operands. */
409 sprintf (op1, "0x%04x", insn);
411 sprintf (comment1, "????");
415 (*prin) (stream, "%s", ok ? opcode->name : ".word");
418 (*prin) (stream, "\t%s", op1);
421 (*prin) (stream, ", %s", op2);
424 (*prin) (stream, "\t; %s", comment1);
427 info->print_address_func (sym_addr1, info);
430 (*prin) (stream, " %s", comment2);
433 info->print_address_func (sym_addr2, info);