1 /* Disassemble AVR instructions.
2 Copyright (C) 1999-2024 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. */
25 #include "disassemble.h"
27 #include "libiberty.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
,
59 enum disassembler_style
* style
,
63 disassemble_info
* info
)
70 /* Any register operand. */
73 insn
= (insn
& 0xf) | ((insn
& 0x0200) >> 5); /* Source register. */
75 insn
= (insn
& 0x01f0) >> 4; /* Destination register. */
77 sprintf (buf
, "r%d", insn
);
78 *style
= dis_style_register
;
83 sprintf (buf
, "r%d", 16 + (insn
& 0xf));
85 sprintf (buf
, "r%d", 16 + ((insn
& 0xf0) >> 4));
86 *style
= dis_style_register
;
90 sprintf (buf
, "r%d", 24 + ((insn
& 0x30) >> 3));
91 *style
= dis_style_register
;
96 sprintf (buf
, "r%d", 16 + (insn
& 7));
98 sprintf (buf
, "r%d", 16 + ((insn
>> 4) & 7));
99 *style
= dis_style_register
;
104 sprintf (buf
, "r%d", (insn
& 0xf) * 2);
106 sprintf (buf
, "r%d", ((insn
& 0xf0) >> 3));
107 *style
= dis_style_register
;
114 switch (insn
& 0x100f)
116 case 0x0000: xyz
= "Z"; break;
117 case 0x1001: xyz
= "Z+"; break;
118 case 0x1002: xyz
= "-Z"; break;
119 case 0x0008: xyz
= "Y"; break;
120 case 0x1009: xyz
= "Y+"; break;
121 case 0x100a: xyz
= "-Y"; break;
122 case 0x100c: xyz
= "X"; break;
123 case 0x100d: xyz
= "X+"; break;
124 case 0x100e: xyz
= "-X"; break;
125 default: xyz
= "??"; ok
= 0;
129 if (AVR_UNDEF_P (insn
))
130 sprintf (comment
, _("undefined"));
132 *style
= dis_style_register
;
138 /* Check for post-increment. */
140 for (s
= opcode_str
; *s
; ++s
)
144 if (insn
& (1 << (15 - (s
- opcode_str
))))
151 if (AVR_UNDEF_P (insn
))
152 sprintf (comment
, _("undefined"));
153 *style
= dis_style_register
;
161 x
|= (insn
>> 7) & (3 << 3);
162 x
|= (insn
>> 8) & (1 << 5);
168 sprintf (buf
, "+%d", x
);
169 sprintf (comment
, "0x%02x", x
);
170 *style
= dis_style_register
;
176 *sym_addr
= ((((insn
& 1) | ((insn
& 0x1f0) >> 3)) << 16) | insn2
) * 2;
177 /* See PR binutils/2454. Ideally we would like to display the hex
178 value of the address only once, but this would mean recoding
179 objdump_print_address() which would affect many targets. */
180 sprintf (buf
, "%#lx", (unsigned long) *sym_addr
);
181 strcpy (comment
, comment_start
);
182 info
->insn_info_valid
= 1;
183 info
->insn_type
= dis_jsr
;
184 info
->target
= *sym_addr
;
185 *style
= dis_style_address
;
190 int rel_addr
= (((insn
& 0xfff) ^ 0x800) - 0x800) * 2;
191 sprintf (buf
, ".%+-8d", rel_addr
);
193 *sym_addr
= pc
+ 2 + rel_addr
;
194 strcpy (comment
, comment_start
);
195 info
->insn_info_valid
= 1;
196 info
->insn_type
= dis_branch
;
197 info
->target
= *sym_addr
;
198 *style
= dis_style_address_offset
;
204 int rel_addr
= ((((insn
>> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
206 sprintf (buf
, ".%+-8d", rel_addr
);
208 *sym_addr
= pc
+ 2 + rel_addr
;
209 strcpy (comment
, comment_start
);
210 info
->insn_info_valid
= 1;
211 info
->insn_type
= dis_condbranch
;
212 info
->target
= *sym_addr
;
213 *style
= dis_style_address_offset
;
219 unsigned int val
= insn2
| 0x800000;
222 sprintf (buf
, "0x%04X", insn2
);
223 strcpy (comment
, comment_start
);
224 *style
= dis_style_immediate
;
230 unsigned int val
= ((insn
& 0xf) | ((insn
& 0x600) >> 5)
231 | ((insn
& 0x100) >> 2));
232 if ((insn
& 0x100) == 0)
235 *sym_addr
= val
| 0x800000;
236 sprintf (buf
, "0x%02x", val
);
237 strcpy (comment
, comment_start
);
238 *style
= dis_style_immediate
;
243 sprintf (buf
, "0x%02X", ((insn
& 0xf00) >> 4) | (insn
& 0xf));
244 sprintf (comment
, "%d", ((insn
& 0xf00) >> 4) | (insn
& 0xf));
245 *style
= dis_style_immediate
;
250 /* xgettext:c-format */
251 opcodes_error_handler (_("internal disassembler error"));
253 *style
= dis_style_immediate
;
260 x
= (insn
& 0xf) | ((insn
>> 2) & 0x30);
261 sprintf (buf
, "0x%02x", x
);
262 sprintf (comment
, "%d", x
);
263 *style
= dis_style_immediate
;
268 sprintf (buf
, "%d", insn
& 7);
269 *style
= dis_style_immediate
;
273 sprintf (buf
, "%d", (insn
>> 4) & 7);
274 *style
= dis_style_immediate
;
282 x
|= (insn
>> 5) & 0x30;
283 sprintf (buf
, "0x%02x", x
);
284 sprintf (comment
, "%d", x
);
285 *style
= dis_style_address
;
293 x
= (insn
>> 3) & 0x1f;
294 sprintf (buf
, "0x%02x", x
);
295 sprintf (comment
, "%d", x
);
296 *style
= dis_style_address
;
301 sprintf (buf
, "%d", (insn
>> 4) & 15);
302 *style
= dis_style_immediate
;
311 /* xgettext:c-format */
312 opcodes_error_handler (_("unknown constraint `%c'"), constraint
);
319 /* Read the opcode from ADDR. Return 0 in success and save opcode
320 in *INSN, otherwise, return -1. */
323 avrdis_opcode (bfd_vma addr
, disassemble_info
*info
, uint16_t *insn
)
328 status
= info
->read_memory_func (addr
, buffer
, 2, info
);
332 *insn
= bfd_getl16 (buffer
);
336 info
->memory_error_func (status
, addr
, info
);
342 print_insn_avr (bfd_vma addr
, disassemble_info
*info
)
344 uint16_t insn
, insn2
;
345 const struct avr_opcodes_s
*opcode
;
346 static unsigned int *maskptr
;
347 void *stream
= info
->stream
;
348 fprintf_styled_ftype prin
= info
->fprintf_styled_func
;
349 static unsigned int *avr_bin_masks
;
350 static int initialized
;
353 char op1
[20], op2
[20], comment1
[40], comment2
[40];
354 enum disassembler_style style_op1
, style_op2
;
355 int sym_op1
= 0, sym_op2
= 0;
356 bfd_vma sym_addr1
, sym_addr2
;
358 /* Clear instruction information field. */
359 info
->insn_info_valid
= 0;
360 info
->branch_delay_insns
= 0;
362 info
->insn_type
= dis_noninsn
;
368 unsigned int nopcodes
;
370 /* PR 4045: Try to avoid duplicating the 0x prefix that
371 objdump_print_addr() will put on addresses when there
372 is no symbol table available. */
373 if (info
->symtab_size
== 0)
376 nopcodes
= sizeof (avr_opcodes
) / sizeof (struct avr_opcodes_s
);
378 avr_bin_masks
= xmalloc (nopcodes
* sizeof (unsigned int));
380 for (opcode
= avr_opcodes
, maskptr
= avr_bin_masks
;
385 unsigned int bin
= 0;
386 unsigned int mask
= 0;
388 for (s
= opcode
->opcode
; *s
; ++s
)
393 mask
|= (*s
== '1' || *s
== '0');
395 assert (s
- opcode
->opcode
== 16);
396 assert (opcode
->bin_opcode
== bin
);
403 if (avrdis_opcode (addr
, info
, &insn
) != 0)
406 for (opcode
= avr_opcodes
, maskptr
= avr_bin_masks
;
410 if ((opcode
->isa
== AVR_ISA_TINY
) && (info
->mach
!= bfd_mach_avrtiny
))
412 if ((insn
& *maskptr
) == opcode
->bin_opcode
)
416 /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
417 `std b+0,r' as `st b,r' (next entry in the table). */
419 if (AVR_DISP0_P (insn
))
426 style_op1
= dis_style_text
;
427 style_op2
= dis_style_text
;
431 char *constraints
= opcode
->constraints
;
432 char *opcode_str
= opcode
->opcode
;
437 if (opcode
->insn_size
> 1)
439 if (avrdis_opcode (addr
+ 2, info
, &insn2
) != 0)
444 if (*constraints
&& *constraints
!= '?')
446 int regs
= REGISTER_P (*constraints
);
448 ok
= avr_operand (insn
, insn2
, addr
, *constraints
, opcode_str
, op1
,
449 comment1
, &style_op1
, 0, &sym_op1
, &sym_addr1
,
452 if (ok
&& *(++constraints
) == ',')
453 ok
= avr_operand (insn
, insn2
, addr
, *(++constraints
), opcode_str
,
454 op2
, *comment1
? comment2
: comment1
,
455 &style_op2
, regs
, &sym_op2
, &sym_addr2
,
462 /* Unknown opcode, or invalid combination of operands. */
463 sprintf (op1
, "0x%04x", insn
);
465 sprintf (comment1
, "????");
469 (*prin
) (stream
, ok
? dis_style_mnemonic
: dis_style_assembler_directive
,
470 "%s", ok
? opcode
->name
: ".word");
473 (*prin
) (stream
, style_op1
, "\t%s", op1
);
477 (*prin
) (stream
, dis_style_text
, ", ");
478 (*prin
) (stream
, style_op2
, "%s", op2
);
482 (*prin
) (stream
, dis_style_comment_start
, "\t; %s", comment1
);
485 info
->print_address_func (sym_addr1
, info
);
488 (*prin
) (stream
, dis_style_comment_start
, " %s", comment2
);
491 info
->print_address_func (sym_addr2
, info
);