Coverage Report

Created: 2023-08-28 06:30

/src/binutils-gdb/opcodes/avr-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble AVR instructions.
2
   Copyright (C) 1999-2023 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
       enum disassembler_style *  style,
60
       int                 regs,
61
       int *               sym,
62
       bfd_vma *           sym_addr,
63
       disassemble_info *  info)
64
0
{
65
0
  int ok = 1;
66
0
  *sym = 0;
67
68
0
  switch (constraint)
69
0
    {
70
      /* Any register operand.  */
71
0
    case 'r':
72
0
      if (regs)
73
0
  insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register.  */
74
0
      else
75
0
  insn = (insn & 0x01f0) >> 4; /* Destination register.  */
76
77
0
      sprintf (buf, "r%d", insn);
78
0
      *style = dis_style_register;
79
0
      break;
80
81
0
    case 'd':
82
0
      if (regs)
83
0
  sprintf (buf, "r%d", 16 + (insn & 0xf));
84
0
      else
85
0
  sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
86
0
      *style = dis_style_register;
87
0
      break;
88
89
0
    case 'w':
90
0
      sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
91
0
      *style = dis_style_register;
92
0
      break;
93
94
0
    case 'a':
95
0
      if (regs)
96
0
  sprintf (buf, "r%d", 16 + (insn & 7));
97
0
      else
98
0
  sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
99
0
      *style = dis_style_register;
100
0
      break;
101
102
0
    case 'v':
103
0
      if (regs)
104
0
  sprintf (buf, "r%d", (insn & 0xf) * 2);
105
0
      else
106
0
  sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
107
0
      *style = dis_style_register;
108
0
      break;
109
110
0
    case 'e':
111
0
      {
112
0
  char *xyz;
113
114
0
  switch (insn & 0x100f)
115
0
    {
116
0
      case 0x0000: xyz = "Z";  break;
117
0
      case 0x1001: xyz = "Z+"; break;
118
0
      case 0x1002: xyz = "-Z"; break;
119
0
      case 0x0008: xyz = "Y";  break;
120
0
      case 0x1009: xyz = "Y+"; break;
121
0
      case 0x100a: xyz = "-Y"; break;
122
0
      case 0x100c: xyz = "X";  break;
123
0
      case 0x100d: xyz = "X+"; break;
124
0
      case 0x100e: xyz = "-X"; break;
125
0
      default: xyz = "??"; ok = 0;
126
0
    }
127
0
  strcpy (buf, xyz);
128
129
0
  if (AVR_UNDEF_P (insn))
130
0
    sprintf (comment, _("undefined"));
131
0
      }
132
0
      *style = dis_style_register;
133
0
      break;
134
135
0
    case 'z':
136
0
      *buf++ = 'Z';
137
138
      /* Check for post-increment. */
139
0
      char *s;
140
0
      for (s = opcode_str; *s; ++s)
141
0
        {
142
0
          if (*s == '+')
143
0
            {
144
0
        if (insn & (1 << (15 - (s - opcode_str))))
145
0
    *buf++ = '+';
146
0
              break;
147
0
            }
148
0
        }
149
150
0
      *buf = '\0';
151
0
      if (AVR_UNDEF_P (insn))
152
0
  sprintf (comment, _("undefined"));
153
0
      *style = dis_style_register;
154
0
      break;
155
156
0
    case 'b':
157
0
      {
158
0
  unsigned int x;
159
160
0
  x = (insn & 7);
161
0
  x |= (insn >> 7) & (3 << 3);
162
0
  x |= (insn >> 8) & (1 << 5);
163
164
0
  if (insn & 0x8)
165
0
    *buf++ = 'Y';
166
0
  else
167
0
    *buf++ = 'Z';
168
0
  sprintf (buf, "+%d", x);
169
0
  sprintf (comment, "0x%02x", x);
170
0
  *style = dis_style_register;
171
0
      }
172
0
      break;
173
174
0
    case 'h':
175
0
      *sym = 1;
176
0
      *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
0
      sprintf (buf, "%#lx", (unsigned long) *sym_addr);
181
0
      strcpy (comment, comment_start);
182
0
      info->insn_info_valid = 1;
183
0
      info->insn_type = dis_jsr;
184
0
      info->target = *sym_addr;
185
0
      *style = dis_style_address;
186
0
      break;
187
188
0
    case 'L':
189
0
      {
190
0
  int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
191
0
  sprintf (buf, ".%+-8d", rel_addr);
192
0
        *sym = 1;
193
0
        *sym_addr = pc + 2 + rel_addr;
194
0
  strcpy (comment, comment_start);
195
0
        info->insn_info_valid = 1;
196
0
        info->insn_type = dis_branch;
197
0
        info->target = *sym_addr;
198
0
  *style = dis_style_address_offset;
199
0
      }
200
0
      break;
201
202
0
    case 'l':
203
0
      {
204
0
  int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
205
206
0
  sprintf (buf, ".%+-8d", rel_addr);
207
0
        *sym = 1;
208
0
        *sym_addr = pc + 2 + rel_addr;
209
0
  strcpy (comment, comment_start);
210
0
        info->insn_info_valid = 1;
211
0
        info->insn_type = dis_condbranch;
212
0
        info->target = *sym_addr;
213
0
  *style = dis_style_address_offset;
214
0
      }
215
0
      break;
216
217
0
    case 'i':
218
0
      {
219
0
        unsigned int val = insn2 | 0x800000;
220
0
        *sym = 1;
221
0
        *sym_addr = val;
222
0
        sprintf (buf, "0x%04X", insn2);
223
0
        strcpy (comment, comment_start);
224
0
  *style = dis_style_immediate;
225
0
      }
226
0
      break;
227
228
0
    case 'j':
229
0
      {
230
0
        unsigned int val = ((insn & 0xf) | ((insn & 0x600) >> 5)
231
0
                                         | ((insn & 0x100) >> 2));
232
0
  if ((insn & 0x100) == 0)
233
0
    val |= 0x80;
234
0
        *sym = 1;
235
0
        *sym_addr = val | 0x800000;
236
0
        sprintf (buf, "0x%02x", val);
237
0
        strcpy (comment, comment_start);
238
0
  *style = dis_style_immediate;
239
0
      }
240
0
      break;
241
242
0
    case 'M':
243
0
      sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
244
0
      sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
245
0
      *style = dis_style_immediate;
246
0
      break;
247
248
0
    case 'n':
249
0
      sprintf (buf, "??");
250
      /* xgettext:c-format */
251
0
      opcodes_error_handler (_("internal disassembler error"));
252
0
      ok = 0;
253
0
      *style = dis_style_immediate;
254
0
      break;
255
256
0
    case 'K':
257
0
      {
258
0
  unsigned int x;
259
260
0
  x = (insn & 0xf) | ((insn >> 2) & 0x30);
261
0
  sprintf (buf, "0x%02x", x);
262
0
  sprintf (comment, "%d", x);
263
0
  *style = dis_style_immediate;
264
0
      }
265
0
      break;
266
267
0
    case 's':
268
0
      sprintf (buf, "%d", insn & 7);
269
0
      *style = dis_style_immediate;
270
0
      break;
271
272
0
    case 'S':
273
0
      sprintf (buf, "%d", (insn >> 4) & 7);
274
0
      *style = dis_style_immediate;
275
0
      break;
276
277
0
    case 'P':
278
0
      {
279
0
  unsigned int x;
280
281
0
  x = (insn & 0xf);
282
0
  x |= (insn >> 5) & 0x30;
283
0
  sprintf (buf, "0x%02x", x);
284
0
  sprintf (comment, "%d", x);
285
0
  *style = dis_style_address;
286
0
      }
287
0
      break;
288
289
0
    case 'p':
290
0
      {
291
0
  unsigned int x;
292
293
0
  x = (insn >> 3) & 0x1f;
294
0
  sprintf (buf, "0x%02x", x);
295
0
  sprintf (comment, "%d", x);
296
0
  *style = dis_style_address;
297
0
      }
298
0
      break;
299
300
0
    case 'E':
301
0
      sprintf (buf, "%d", (insn >> 4) & 15);
302
0
      *style = dis_style_immediate;
303
0
      break;
304
305
0
    case '?':
306
0
      *buf = '\0';
307
0
      break;
308
309
0
    default:
310
0
      sprintf (buf, "??");
311
      /* xgettext:c-format */
312
0
      opcodes_error_handler (_("unknown constraint `%c'"), constraint);
313
0
      ok = 0;
314
0
    }
315
316
0
    return ok;
317
0
}
318
319
/* Read the opcode from ADDR.  Return 0 in success and save opcode
320
   in *INSN, otherwise, return -1.  */
321
322
static int
323
avrdis_opcode (bfd_vma addr, disassemble_info *info, uint16_t *insn)
324
0
{
325
0
  bfd_byte buffer[2];
326
0
  int status;
327
328
0
  status = info->read_memory_func (addr, buffer, 2, info);
329
330
0
  if (status == 0)
331
0
    {
332
0
      *insn = bfd_getl16 (buffer);
333
0
      return 0;
334
0
    }
335
336
0
  info->memory_error_func (status, addr, info);
337
0
  return -1;
338
0
}
339
340
341
int
342
print_insn_avr (bfd_vma addr, disassemble_info *info)
343
0
{
344
0
  uint16_t insn, insn2;
345
0
  const struct avr_opcodes_s *opcode;
346
0
  static unsigned int *maskptr;
347
0
  void *stream = info->stream;
348
0
  fprintf_styled_ftype prin = info->fprintf_styled_func;
349
0
  static unsigned int *avr_bin_masks;
350
0
  static int initialized;
351
0
  int cmd_len = 2;
352
0
  int ok = 0;
353
0
  char op1[20], op2[20], comment1[40], comment2[40];
354
0
  enum disassembler_style style_op1, style_op2;
355
0
  int sym_op1 = 0, sym_op2 = 0;
356
0
  bfd_vma sym_addr1, sym_addr2;
357
358
  /* Clear instruction information field.  */
359
0
  info->insn_info_valid = 0;
360
0
  info->branch_delay_insns = 0;
361
0
  info->data_size = 0;
362
0
  info->insn_type = dis_noninsn;
363
0
  info->target = 0;
364
0
  info->target2 = 0;
365
366
0
  if (!initialized)
367
0
    {
368
0
      unsigned int nopcodes;
369
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
0
      if (info->symtab_size == 0)
374
0
  comment_start = " ";
375
376
0
      nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
377
378
0
      avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
379
380
0
      for (opcode = avr_opcodes, maskptr = avr_bin_masks;
381
0
     opcode->name;
382
0
     opcode++, maskptr++)
383
0
  {
384
0
    char * s;
385
0
    unsigned int bin = 0;
386
0
    unsigned int mask = 0;
387
388
0
    for (s = opcode->opcode; *s; ++s)
389
0
      {
390
0
        bin <<= 1;
391
0
        mask <<= 1;
392
0
        bin |= (*s == '1');
393
0
        mask |= (*s == '1' || *s == '0');
394
0
      }
395
0
    assert (s - opcode->opcode == 16);
396
0
    assert (opcode->bin_opcode == bin);
397
0
    *maskptr = mask;
398
0
  }
399
400
0
      initialized = 1;
401
0
    }
402
403
0
  if (avrdis_opcode (addr, info, &insn)  != 0)
404
0
    return -1;
405
406
0
  for (opcode = avr_opcodes, maskptr = avr_bin_masks;
407
0
       opcode->name;
408
0
       opcode++, maskptr++)
409
0
    {
410
0
      if ((opcode->isa == AVR_ISA_TINY) && (info->mach != bfd_mach_avrtiny))
411
0
        continue;
412
0
      if ((insn & *maskptr) == opcode->bin_opcode)
413
0
        break;
414
0
    }
415
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).  */
418
419
0
  if (AVR_DISP0_P (insn))
420
0
    opcode++;
421
422
0
  op1[0] = 0;
423
0
  op2[0] = 0;
424
0
  comment1[0] = 0;
425
0
  comment2[0] = 0;
426
0
  style_op1 = dis_style_text;
427
0
  style_op2 = dis_style_text;
428
429
0
  if (opcode->name)
430
0
    {
431
0
      char *constraints = opcode->constraints;
432
0
      char *opcode_str = opcode->opcode;
433
434
0
      insn2 = 0;
435
0
      ok = 1;
436
437
0
      if (opcode->insn_size > 1)
438
0
  {
439
0
    if (avrdis_opcode (addr + 2, info, &insn2) != 0)
440
0
      return -1;
441
0
    cmd_len = 4;
442
0
  }
443
444
0
      if (*constraints && *constraints != '?')
445
0
  {
446
0
    int regs = REGISTER_P (*constraints);
447
448
0
    ok = avr_operand (insn, insn2, addr, *constraints, opcode_str, op1,
449
0
          comment1, &style_op1, 0, &sym_op1, &sym_addr1,
450
0
          info);
451
452
0
    if (ok && *(++constraints) == ',')
453
0
      ok = avr_operand (insn, insn2, addr, *(++constraints), opcode_str,
454
0
            op2, *comment1 ? comment2 : comment1,
455
0
            &style_op2, regs, &sym_op2, &sym_addr2,
456
0
            info);
457
0
  }
458
0
    }
459
460
0
  if (!ok)
461
0
    {
462
      /* Unknown opcode, or invalid combination of operands.  */
463
0
      sprintf (op1, "0x%04x", insn);
464
0
      op2[0] = 0;
465
0
      sprintf (comment1, "????");
466
0
      comment2[0] = 0;
467
0
    }
468
469
0
  (*prin) (stream, ok ? dis_style_mnemonic : dis_style_assembler_directive,
470
0
     "%s", ok ? opcode->name : ".word");
471
  
472
0
  if (*op1)
473
0
    (*prin) (stream, style_op1, "\t%s", op1);
474
475
0
  if (*op2)
476
0
    {
477
0
      (*prin) (stream, dis_style_text, ", ");
478
0
      (*prin) (stream, style_op2, "%s", op2);
479
0
    }
480
481
0
  if (*comment1)
482
0
    (*prin) (stream, dis_style_comment_start, "\t; %s", comment1);
483
484
0
  if (sym_op1)
485
0
    info->print_address_func (sym_addr1, info);
486
487
0
  if (*comment2)
488
0
    (*prin) (stream, dis_style_comment_start, " %s", comment2);
489
490
0
  if (sym_op2)
491
0
    info->print_address_func (sym_addr2, info);
492
493
0
  return cmd_len;
494
0
}