Fix potentially uninitialised variables in the Windows tools
[binutils-gdb.git] / opcodes / avr-dis.c
1 /* Disassemble AVR instructions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3
4 Contributed by Denis Chertykov <denisc@overta.ru>
5
6 This file is part of libopcodes.
7
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)
11 any later version.
12
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.
17
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. */
22
23 #include "sysdep.h"
24 #include <assert.h>
25 #include "disassemble.h"
26 #include "opintl.h"
27 #include "libiberty.h"
28 #include <stdint.h>
29
30 struct avr_opcodes_s
31 {
32 char *name;
33 char *constraints;
34 char *opcode;
35 int insn_size; /* In words. */
36 int isa;
37 unsigned int bin_opcode;
38 };
39
40 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
41 {#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
42
43 const struct avr_opcodes_s avr_opcodes[] =
44 {
45 #include "opcode/avr.h"
46 {NULL, NULL, NULL, 0, 0, 0}
47 };
48
49 static const char * comment_start = "0x";
50
51 static int
52 avr_operand (unsigned int insn,
53 unsigned int insn2,
54 unsigned int pc,
55 int constraint,
56 char * opcode_str,
57 char * buf,
58 char * comment,
59 int regs,
60 int * sym,
61 bfd_vma * sym_addr,
62 disassemble_info * info)
63 {
64 int ok = 1;
65 *sym = 0;
66
67 switch (constraint)
68 {
69 /* Any register operand. */
70 case 'r':
71 if (regs)
72 insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register. */
73 else
74 insn = (insn & 0x01f0) >> 4; /* Destination register. */
75
76 sprintf (buf, "r%d", insn);
77 break;
78
79 case 'd':
80 if (regs)
81 sprintf (buf, "r%d", 16 + (insn & 0xf));
82 else
83 sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
84 break;
85
86 case 'w':
87 sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
88 break;
89
90 case 'a':
91 if (regs)
92 sprintf (buf, "r%d", 16 + (insn & 7));
93 else
94 sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
95 break;
96
97 case 'v':
98 if (regs)
99 sprintf (buf, "r%d", (insn & 0xf) * 2);
100 else
101 sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
102 break;
103
104 case 'e':
105 {
106 char *xyz;
107
108 switch (insn & 0x100f)
109 {
110 case 0x0000: xyz = "Z"; break;
111 case 0x1001: xyz = "Z+"; break;
112 case 0x1002: xyz = "-Z"; break;
113 case 0x0008: xyz = "Y"; break;
114 case 0x1009: xyz = "Y+"; break;
115 case 0x100a: xyz = "-Y"; break;
116 case 0x100c: xyz = "X"; break;
117 case 0x100d: xyz = "X+"; break;
118 case 0x100e: xyz = "-X"; break;
119 default: xyz = "??"; ok = 0;
120 }
121 strcpy (buf, xyz);
122
123 if (AVR_UNDEF_P (insn))
124 sprintf (comment, _("undefined"));
125 }
126 break;
127
128 case 'z':
129 *buf++ = 'Z';
130
131 /* Check for post-increment. */
132 char *s;
133 for (s = opcode_str; *s; ++s)
134 {
135 if (*s == '+')
136 {
137 if (insn & (1 << (15 - (s - opcode_str))))
138 *buf++ = '+';
139 break;
140 }
141 }
142
143 *buf = '\0';
144 if (AVR_UNDEF_P (insn))
145 sprintf (comment, _("undefined"));
146 break;
147
148 case 'b':
149 {
150 unsigned int x;
151
152 x = (insn & 7);
153 x |= (insn >> 7) & (3 << 3);
154 x |= (insn >> 8) & (1 << 5);
155
156 if (insn & 0x8)
157 *buf++ = 'Y';
158 else
159 *buf++ = 'Z';
160 sprintf (buf, "+%d", x);
161 sprintf (comment, "0x%02x", x);
162 }
163 break;
164
165 case 'h':
166 *sym = 1;
167 *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
168 /* See PR binutils/2454. Ideally we would like to display the hex
169 value of the address only once, but this would mean recoding
170 objdump_print_address() which would affect many targets. */
171 sprintf (buf, "%#lx", (unsigned long) *sym_addr);
172 strcpy (comment, comment_start);
173 info->insn_info_valid = 1;
174 info->insn_type = dis_jsr;
175 info->target = *sym_addr;
176 break;
177
178 case 'L':
179 {
180 int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
181 sprintf (buf, ".%+-8d", rel_addr);
182 *sym = 1;
183 *sym_addr = pc + 2 + rel_addr;
184 strcpy (comment, comment_start);
185 info->insn_info_valid = 1;
186 info->insn_type = dis_branch;
187 info->target = *sym_addr;
188 }
189 break;
190
191 case 'l':
192 {
193 int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
194
195 sprintf (buf, ".%+-8d", rel_addr);
196 *sym = 1;
197 *sym_addr = pc + 2 + rel_addr;
198 strcpy (comment, comment_start);
199 info->insn_info_valid = 1;
200 info->insn_type = dis_condbranch;
201 info->target = *sym_addr;
202 }
203 break;
204
205 case 'i':
206 {
207 unsigned int val = insn2 | 0x800000;
208 *sym = 1;
209 *sym_addr = val;
210 sprintf (buf, "0x%04X", insn2);
211 strcpy (comment, comment_start);
212 }
213 break;
214
215 case 'j':
216 {
217 unsigned int val = ((insn & 0xf) | ((insn & 0x600) >> 5)
218 | ((insn & 0x100) >> 2));
219 if ((insn & 0x100) == 0)
220 val |= 0x80;
221 *sym = 1;
222 *sym_addr = val | 0x800000;
223 sprintf (buf, "0x%02x", val);
224 strcpy (comment, comment_start);
225 }
226 break;
227
228 case 'M':
229 sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
230 sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
231 break;
232
233 case 'n':
234 sprintf (buf, "??");
235 /* xgettext:c-format */
236 opcodes_error_handler (_("internal disassembler error"));
237 ok = 0;
238 break;
239
240 case 'K':
241 {
242 unsigned int x;
243
244 x = (insn & 0xf) | ((insn >> 2) & 0x30);
245 sprintf (buf, "0x%02x", x);
246 sprintf (comment, "%d", x);
247 }
248 break;
249
250 case 's':
251 sprintf (buf, "%d", insn & 7);
252 break;
253
254 case 'S':
255 sprintf (buf, "%d", (insn >> 4) & 7);
256 break;
257
258 case 'P':
259 {
260 unsigned int x;
261
262 x = (insn & 0xf);
263 x |= (insn >> 5) & 0x30;
264 sprintf (buf, "0x%02x", x);
265 sprintf (comment, "%d", x);
266 }
267 break;
268
269 case 'p':
270 {
271 unsigned int x;
272
273 x = (insn >> 3) & 0x1f;
274 sprintf (buf, "0x%02x", x);
275 sprintf (comment, "%d", x);
276 }
277 break;
278
279 case 'E':
280 sprintf (buf, "%d", (insn >> 4) & 15);
281 break;
282
283 case '?':
284 *buf = '\0';
285 break;
286
287 default:
288 sprintf (buf, "??");
289 /* xgettext:c-format */
290 opcodes_error_handler (_("unknown constraint `%c'"), constraint);
291 ok = 0;
292 }
293
294 return ok;
295 }
296
297 /* Read the opcode from ADDR. Return 0 in success and save opcode
298 in *INSN, otherwise, return -1. */
299
300 static int
301 avrdis_opcode (bfd_vma addr, disassemble_info *info, uint16_t *insn)
302 {
303 bfd_byte buffer[2];
304 int status;
305
306 status = info->read_memory_func (addr, buffer, 2, info);
307
308 if (status == 0)
309 {
310 *insn = bfd_getl16 (buffer);
311 return 0;
312 }
313
314 info->memory_error_func (status, addr, info);
315 return -1;
316 }
317
318
319 int
320 print_insn_avr (bfd_vma addr, disassemble_info *info)
321 {
322 uint16_t insn, insn2;
323 const struct avr_opcodes_s *opcode;
324 static unsigned int *maskptr;
325 void *stream = info->stream;
326 fprintf_ftype prin = info->fprintf_func;
327 static unsigned int *avr_bin_masks;
328 static int initialized;
329 int cmd_len = 2;
330 int ok = 0;
331 char op1[20], op2[20], comment1[40], comment2[40];
332 int sym_op1 = 0, sym_op2 = 0;
333 bfd_vma sym_addr1, sym_addr2;
334
335 /* Clear instruction information field. */
336 info->insn_info_valid = 0;
337 info->branch_delay_insns = 0;
338 info->data_size = 0;
339 info->insn_type = dis_noninsn;
340 info->target = 0;
341 info->target2 = 0;
342
343 if (!initialized)
344 {
345 unsigned int nopcodes;
346
347 /* PR 4045: Try to avoid duplicating the 0x prefix that
348 objdump_print_addr() will put on addresses when there
349 is no symbol table available. */
350 if (info->symtab_size == 0)
351 comment_start = " ";
352
353 nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
354
355 avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
356
357 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
358 opcode->name;
359 opcode++, maskptr++)
360 {
361 char * s;
362 unsigned int bin = 0;
363 unsigned int mask = 0;
364
365 for (s = opcode->opcode; *s; ++s)
366 {
367 bin <<= 1;
368 mask <<= 1;
369 bin |= (*s == '1');
370 mask |= (*s == '1' || *s == '0');
371 }
372 assert (s - opcode->opcode == 16);
373 assert (opcode->bin_opcode == bin);
374 *maskptr = mask;
375 }
376
377 initialized = 1;
378 }
379
380 if (avrdis_opcode (addr, info, &insn) != 0)
381 return -1;
382
383 for (opcode = avr_opcodes, maskptr = avr_bin_masks;
384 opcode->name;
385 opcode++, maskptr++)
386 {
387 if ((opcode->isa == AVR_ISA_TINY) && (info->mach != bfd_mach_avrtiny))
388 continue;
389 if ((insn & *maskptr) == opcode->bin_opcode)
390 break;
391 }
392
393 /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
394 `std b+0,r' as `st b,r' (next entry in the table). */
395
396 if (AVR_DISP0_P (insn))
397 opcode++;
398
399 op1[0] = 0;
400 op2[0] = 0;
401 comment1[0] = 0;
402 comment2[0] = 0;
403
404 if (opcode->name)
405 {
406 char *constraints = opcode->constraints;
407 char *opcode_str = opcode->opcode;
408
409 insn2 = 0;
410 ok = 1;
411
412 if (opcode->insn_size > 1)
413 {
414 if (avrdis_opcode (addr + 2, info, &insn2) != 0)
415 return -1;
416 cmd_len = 4;
417 }
418
419 if (*constraints && *constraints != '?')
420 {
421 int regs = REGISTER_P (*constraints);
422
423 ok = avr_operand (insn, insn2, addr, *constraints, opcode_str, op1,
424 comment1, 0, &sym_op1, &sym_addr1, info);
425
426 if (ok && *(++constraints) == ',')
427 ok = avr_operand (insn, insn2, addr, *(++constraints), opcode_str,
428 op2, *comment1 ? comment2 : comment1, regs,
429 &sym_op2, &sym_addr2, info);
430 }
431 }
432
433 if (!ok)
434 {
435 /* Unknown opcode, or invalid combination of operands. */
436 sprintf (op1, "0x%04x", insn);
437 op2[0] = 0;
438 sprintf (comment1, "????");
439 comment2[0] = 0;
440 }
441
442 (*prin) (stream, "%s", ok ? opcode->name : ".word");
443
444 if (*op1)
445 (*prin) (stream, "\t%s", op1);
446
447 if (*op2)
448 (*prin) (stream, ", %s", op2);
449
450 if (*comment1)
451 (*prin) (stream, "\t; %s", comment1);
452
453 if (sym_op1)
454 info->print_address_func (sym_addr1, info);
455
456 if (*comment2)
457 (*prin) (stream, " %s", comment2);
458
459 if (sym_op2)
460 info->print_address_func (sym_addr2, info);
461
462 return cmd_len;
463 }