Coverage Report

Created: 2023-08-28 06:30

/src/binutils-gdb/opcodes/microblaze-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble Xilinx microblaze instructions.
2
3
   Copyright (C) 2009-2023 Free Software Foundation, Inc.
4
5
   This file is part of the GNU opcodes library.
6
7
   This library 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 3, or (at your option)
10
   any later version.
11
12
   It is distributed in the hope that it will be useful, but WITHOUT
13
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15
   License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this file; see the file COPYING.  If not, write to the
19
   Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
23
#include "sysdep.h"
24
#define STATIC_TABLE
25
#define DEFINE_TABLE
26
27
#include "disassemble.h"
28
#include <strings.h>
29
#include "microblaze-opc.h"
30
#include "microblaze-dis.h"
31
32
2.29k
#define get_field_rd(buf, instr)   get_field (buf, instr, RD_MASK, RD_LOW)
33
2.29k
#define get_field_r1(buf, instr)   get_field (buf, instr, RA_MASK, RA_LOW)
34
1.63k
#define get_field_r2(buf, instr)   get_field (buf, instr, RB_MASK, RB_LOW)
35
81
#define get_int_field_imm(instr)   ((instr & IMM_MASK) >> IMM_LOW)
36
611
#define get_int_field_r1(instr)    ((instr & RA_MASK) >> RA_LOW)
37
38
6.90k
#define NUM_STRBUFS 3
39
#define STRBUF_SIZE 25
40
41
struct string_buf
42
{
43
  unsigned int which;
44
  char str[NUM_STRBUFS][STRBUF_SIZE];
45
};
46
47
static inline char *
48
strbuf (struct string_buf *buf)
49
6.90k
{
50
6.90k
#ifdef ENABLE_CHECKING
51
6.90k
  if (buf->which >= NUM_STRBUFS)
52
0
    abort ();
53
6.90k
#endif
54
6.90k
  return buf->str[buf->which++];
55
6.90k
}
56
57
static char *
58
get_field (struct string_buf *buf, long instr, long mask, unsigned short low)
59
6.21k
{
60
6.21k
  char *p = strbuf (buf);
61
62
6.21k
  sprintf (p, "%s%d", register_prefix, (int)((instr & mask) >> low));
63
6.21k
  return p;
64
6.21k
}
65
66
static char *
67
get_field_imm (struct string_buf *buf, long instr)
68
629
{
69
629
  char *p = strbuf (buf);
70
71
629
  sprintf (p, "%d", (short)((instr & IMM_MASK) >> IMM_LOW));
72
629
  return p;
73
629
}
74
75
static char *
76
get_field_imm5 (struct string_buf *buf, long instr)
77
22
{
78
22
  char *p = strbuf (buf);
79
80
22
  sprintf (p, "%d", (short)((instr & IMM5_MASK) >> IMM_LOW));
81
22
  return p;
82
22
}
83
84
static char *
85
get_field_imm5_mbar (struct string_buf *buf, long instr)
86
2
{
87
2
  char *p = strbuf (buf);
88
89
2
  sprintf (p, "%d", (short)((instr & IMM5_MBAR_MASK) >> IMM_MBAR));
90
2
  return p;
91
2
}
92
93
static char *
94
get_field_rfsl (struct string_buf *buf, long instr)
95
31
{
96
31
  char *p = strbuf (buf);
97
98
31
  sprintf (p, "%s%d", fsl_register_prefix,
99
31
     (short)((instr & RFSL_MASK) >> IMM_LOW));
100
31
  return p;
101
31
}
102
103
static char *
104
get_field_imm15 (struct string_buf *buf, long instr)
105
0
{
106
0
  char *p = strbuf (buf);
107
108
0
  sprintf (p, "%d", (short)((instr & IMM15_MASK) >> IMM_LOW));
109
0
  return p;
110
0
}
111
112
static char *
113
get_field_special (struct string_buf *buf, long instr,
114
       const struct op_code_struct *op)
115
0
{
116
0
  char *p = strbuf (buf);
117
0
  char *spr;
118
119
0
  switch ((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask))
120
0
    {
121
0
    case REG_MSR_MASK :
122
0
      spr = "msr";
123
0
      break;
124
0
    case REG_PC_MASK :
125
0
      spr = "pc";
126
0
      break;
127
0
    case REG_EAR_MASK :
128
0
      spr = "ear";
129
0
      break;
130
0
    case REG_ESR_MASK :
131
0
      spr = "esr";
132
0
      break;
133
0
    case REG_FSR_MASK :
134
0
      spr = "fsr";
135
0
      break;
136
0
    case REG_BTR_MASK :
137
0
      spr = "btr";
138
0
      break;
139
0
    case REG_EDR_MASK :
140
0
      spr = "edr";
141
0
      break;
142
0
    case REG_PID_MASK :
143
0
      spr = "pid";
144
0
      break;
145
0
    case REG_ZPR_MASK :
146
0
      spr = "zpr";
147
0
      break;
148
0
    case REG_TLBX_MASK :
149
0
      spr = "tlbx";
150
0
      break;
151
0
    case REG_TLBLO_MASK :
152
0
      spr = "tlblo";
153
0
      break;
154
0
    case REG_TLBHI_MASK :
155
0
      spr = "tlbhi";
156
0
      break;
157
0
    case REG_TLBSX_MASK :
158
0
      spr = "tlbsx";
159
0
      break;
160
0
    case REG_SHR_MASK :
161
0
      spr = "shr";
162
0
      break;
163
0
    case REG_SLR_MASK :
164
0
      spr = "slr";
165
0
      break;
166
0
    default :
167
0
      if (((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask) & 0xE000)
168
0
    == REG_PVR_MASK)
169
0
  {
170
0
    sprintf (p, "%spvr%d", register_prefix,
171
0
       (unsigned short)(((instr & IMM_MASK) >> IMM_LOW)
172
0
            ^ op->immval_mask) ^ REG_PVR_MASK);
173
0
    return p;
174
0
  }
175
0
      else
176
0
  spr = "pc";
177
0
      break;
178
0
    }
179
180
0
   sprintf (p, "%s%s", register_prefix, spr);
181
0
   return p;
182
0
}
183
184
static unsigned long
185
read_insn_microblaze (bfd_vma memaddr,
186
          struct disassemble_info *info,
187
          const struct op_code_struct **opr)
188
4.63k
{
189
4.63k
  unsigned char       ibytes[4];
190
4.63k
  int                 status;
191
4.63k
  const struct op_code_struct *op;
192
4.63k
  unsigned long inst;
193
194
4.63k
  status = info->read_memory_func (memaddr, ibytes, 4, info);
195
196
4.63k
  if (status != 0)
197
13
    {
198
13
      info->memory_error_func (status, memaddr, info);
199
13
      return 0;
200
13
    }
201
202
4.61k
  if (info->endian == BFD_ENDIAN_BIG)
203
4.61k
    inst = (((unsigned) ibytes[0] << 24) | (ibytes[1] << 16)
204
4.61k
      | (ibytes[2] << 8) | ibytes[3]);
205
0
  else if (info->endian == BFD_ENDIAN_LITTLE)
206
0
    inst = (((unsigned) ibytes[3] << 24) | (ibytes[2] << 16)
207
0
      | (ibytes[1] << 8) | ibytes[0]);
208
0
  else
209
0
    abort ();
210
211
  /* Just a linear search of the table.  */
212
916k
  for (op = microblaze_opcodes; op->name != 0; op ++)
213
916k
    if (op->bit_sequence == (inst & op->opcode_mask))
214
4.61k
      break;
215
216
4.61k
  *opr = op;
217
4.61k
  return inst;
218
4.61k
}
219
220
221
int
222
print_insn_microblaze (bfd_vma memaddr, struct disassemble_info * info)
223
2.38k
{
224
2.38k
  fprintf_ftype print_func = info->fprintf_func;
225
2.38k
  void *stream = info->stream;
226
2.38k
  unsigned long inst, prev_inst;
227
2.38k
  const struct op_code_struct *op, *pop;
228
2.38k
  int immval = 0;
229
2.38k
  bool immfound = false;
230
2.38k
  static bfd_vma prev_insn_addr = -1; /* Init the prev insn addr.  */
231
2.38k
  static int prev_insn_vma = -1;  /* Init the prev insn vma.  */
232
2.38k
  int curr_insn_vma = info->buffer_vma;
233
2.38k
  struct string_buf buf;
234
235
2.38k
  buf.which = 0;
236
2.38k
  info->bytes_per_chunk = 4;
237
238
2.38k
  inst = read_insn_microblaze (memaddr, info, &op);
239
2.38k
  if (inst == 0)
240
45
    return -1;
241
242
2.33k
  if (prev_insn_vma == curr_insn_vma)
243
2.31k
    {
244
2.31k
      if (memaddr-(info->bytes_per_chunk) == prev_insn_addr)
245
2.24k
  {
246
2.24k
    prev_inst = read_insn_microblaze (prev_insn_addr, info, &pop);
247
2.24k
    if (prev_inst == 0)
248
0
      return -1;
249
2.24k
    if (pop->instr == imm)
250
0
      {
251
0
        immval = (get_int_field_imm (prev_inst) << 16) & 0xffff0000;
252
0
        immfound = true;
253
0
      }
254
2.24k
    else
255
2.24k
      {
256
2.24k
        immval = 0;
257
2.24k
        immfound = false;
258
2.24k
      }
259
2.24k
  }
260
2.31k
    }
261
262
  /* Make curr insn as prev insn.  */
263
2.33k
  prev_insn_addr = memaddr;
264
2.33k
  prev_insn_vma = curr_insn_vma;
265
266
2.33k
  if (op->name == NULL)
267
0
    print_func (stream, ".short 0x%04x", (unsigned int) inst);
268
2.33k
  else
269
2.33k
    {
270
2.33k
      print_func (stream, "%s", op->name);
271
272
2.33k
      switch (op->inst_type)
273
2.33k
  {
274
1.60k
  case INST_TYPE_RD_R1_R2:
275
1.60k
    print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
276
1.60k
          get_field_r1 (&buf, inst), get_field_r2 (&buf, inst));
277
1.60k
    break;
278
611
  case INST_TYPE_RD_R1_IMM:
279
611
    print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
280
611
          get_field_r1 (&buf, inst), get_field_imm (&buf, inst));
281
611
    if (info->print_address_func && get_int_field_r1 (inst) == 0
282
611
        && info->symbol_at_address_func)
283
67
      {
284
67
        if (immfound)
285
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
286
67
        else
287
67
    {
288
67
      immval = get_int_field_imm (inst);
289
67
      if (immval & 0x8000)
290
33
        immval |= 0xFFFF0000;
291
67
    }
292
67
        if (immval > 0 && info->symbol_at_address_func (immval, info))
293
0
    {
294
0
      print_func (stream, "\t// ");
295
0
      info->print_address_func (immval, info);
296
0
    }
297
67
      }
298
611
    break;
299
22
  case INST_TYPE_RD_R1_IMM5:
300
22
    print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
301
22
          get_field_r1 (&buf, inst), get_field_imm5 (&buf, inst));
302
22
    break;
303
16
  case INST_TYPE_RD_RFSL:
304
16
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
305
16
          get_field_rfsl (&buf, inst));
306
16
    break;
307
10
  case INST_TYPE_R1_RFSL:
308
10
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
309
10
          get_field_rfsl (&buf, inst));
310
10
    break;
311
0
  case INST_TYPE_RD_SPECIAL:
312
0
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
313
0
          get_field_special (&buf, inst, op));
314
0
    break;
315
0
  case INST_TYPE_SPECIAL_R1:
316
0
    print_func (stream, "\t%s, %s", get_field_special (&buf, inst, op),
317
0
          get_field_r1 (&buf, inst));
318
0
    break;
319
22
  case INST_TYPE_RD_R1:
320
22
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
321
22
          get_field_r1 (&buf, inst));
322
22
    break;
323
7
  case INST_TYPE_R1_R2:
324
7
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
325
7
          get_field_r2 (&buf, inst));
326
7
    break;
327
15
  case INST_TYPE_R1_IMM:
328
15
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
329
15
          get_field_imm (&buf, inst));
330
    /* The non-pc relative instructions are returns, which shouldn't
331
       have a label printed.  */
332
15
    if (info->print_address_func && op->inst_offset_type == INST_PC_OFFSET
333
15
        && info->symbol_at_address_func)
334
11
      {
335
11
        if (immfound)
336
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
337
11
        else
338
11
    {
339
11
      immval = get_int_field_imm (inst);
340
11
      if (immval & 0x8000)
341
7
        immval |= 0xFFFF0000;
342
11
    }
343
11
        immval += memaddr;
344
11
        if (immval > 0 && info->symbol_at_address_func (immval, info))
345
0
    {
346
0
      print_func (stream, "\t// ");
347
0
      info->print_address_func (immval, info);
348
0
    }
349
11
        else
350
11
    {
351
11
      print_func (stream, "\t\t// ");
352
11
      print_func (stream, "%x", immval);
353
11
    }
354
11
      }
355
15
    break;
356
3
  case INST_TYPE_RD_IMM:
357
3
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
358
3
          get_field_imm (&buf, inst));
359
3
    if (info->print_address_func && info->symbol_at_address_func)
360
3
      {
361
3
        if (immfound)
362
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
363
3
        else
364
3
    {
365
3
      immval = get_int_field_imm (inst);
366
3
      if (immval & 0x8000)
367
2
        immval |= 0xFFFF0000;
368
3
    }
369
3
        if (op->inst_offset_type == INST_PC_OFFSET)
370
1
    immval += (int) memaddr;
371
3
        if (info->symbol_at_address_func (immval, info))
372
0
    {
373
0
      print_func (stream, "\t// ");
374
0
      info->print_address_func (immval, info);
375
0
    }
376
3
      }
377
3
    break;
378
0
  case INST_TYPE_IMM:
379
0
    print_func (stream, "\t%s", get_field_imm (&buf, inst));
380
0
    if (info->print_address_func && info->symbol_at_address_func
381
0
        && op->instr != imm)
382
0
      {
383
0
        if (immfound)
384
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
385
0
        else
386
0
    {
387
0
      immval = get_int_field_imm (inst);
388
0
      if (immval & 0x8000)
389
0
        immval |= 0xFFFF0000;
390
0
    }
391
0
        if (op->inst_offset_type == INST_PC_OFFSET)
392
0
    immval += (int) memaddr;
393
0
        if (immval > 0 && info->symbol_at_address_func (immval, info))
394
0
    {
395
0
      print_func (stream, "\t// ");
396
0
      info->print_address_func (immval, info);
397
0
    }
398
0
        else if (op->inst_offset_type == INST_PC_OFFSET)
399
0
    {
400
0
      print_func (stream, "\t\t// ");
401
0
      print_func (stream, "%x", immval);
402
0
    }
403
0
      }
404
0
    break;
405
11
  case INST_TYPE_RD_R2:
406
11
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
407
11
          get_field_r2 (&buf, inst));
408
11
    break;
409
6
  case INST_TYPE_R2:
410
6
    print_func (stream, "\t%s", get_field_r2 (&buf, inst));
411
6
    break;
412
0
  case INST_TYPE_R1:
413
0
    print_func (stream, "\t%s", get_field_r1 (&buf, inst));
414
0
    break;
415
1
  case INST_TYPE_R1_R2_SPECIAL:
416
1
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
417
1
          get_field_r2 (&buf, inst));
418
1
    break;
419
0
  case INST_TYPE_RD_IMM15:
420
0
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
421
0
          get_field_imm15 (&buf, inst));
422
0
    break;
423
    /* For mbar insn.  */
424
2
  case INST_TYPE_IMM5:
425
2
    print_func (stream, "\t%s", get_field_imm5_mbar (&buf, inst));
426
2
    break;
427
    /* For mbar 16 or sleep insn.  */
428
1
  case INST_TYPE_NONE:
429
1
    break;
430
    /* For tuqula instruction */
431
0
  case INST_TYPE_RD:
432
0
    print_func (stream, "\t%s", get_field_rd (&buf, inst));
433
0
    break;
434
5
  case INST_TYPE_RFSL:
435
5
    print_func (stream, "\t%s", get_field_rfsl (&buf, inst));
436
5
    break;
437
0
  default:
438
    /* If the disassembler lags the instruction set.  */
439
0
    print_func (stream, "\tundecoded operands, inst is 0x%04x",
440
0
          (unsigned int) inst);
441
0
    break;
442
2.33k
  }
443
2.33k
    }
444
445
  /* Say how many bytes we consumed.  */
446
2.33k
  return 4;
447
2.33k
}
448
449
enum microblaze_instr
450
get_insn_microblaze (long inst,
451
           bool *isunsignedimm,
452
           enum microblaze_instr_type *insn_type,
453
           short *delay_slots)
454
0
{
455
0
  const struct op_code_struct *op;
456
0
  *isunsignedimm = false;
457
458
  /* Just a linear search of the table.  */
459
0
  for (op = microblaze_opcodes; op->name != 0; op ++)
460
0
    if (op->bit_sequence == (inst & op->opcode_mask))
461
0
      break;
462
463
0
  if (op->name == 0)
464
0
    return invalid_inst;
465
0
  else
466
0
    {
467
0
      *isunsignedimm = (op->inst_type == INST_TYPE_RD_R1_UNSIGNED_IMM);
468
0
      *insn_type = op->instr_type;
469
0
      *delay_slots = op->delay_slots;
470
0
      return op->instr;
471
0
    }
472
0
}
473
474
enum microblaze_instr
475
microblaze_decode_insn (long insn, int *rd, int *ra, int *rb, int *immed)
476
0
{
477
0
  enum microblaze_instr op;
478
0
  bool t1;
479
0
  enum microblaze_instr_type t2;
480
0
  short t3;
481
482
0
  op = get_insn_microblaze (insn, &t1, &t2, &t3);
483
0
  *rd = (insn & RD_MASK) >> RD_LOW;
484
0
  *ra = (insn & RA_MASK) >> RA_LOW;
485
0
  *rb = (insn & RB_MASK) >> RB_LOW;
486
0
  t3 = (insn & IMM_MASK) >> IMM_LOW;
487
0
  *immed = (int) t3;
488
0
  return (op);
489
0
}
490
491
unsigned long
492
microblaze_get_target_address (long inst, bool immfound, int immval,
493
             long pcval, long r1val, long r2val,
494
             bool *targetvalid,
495
             bool *unconditionalbranch)
496
0
{
497
0
  const struct op_code_struct *op;
498
0
  long targetaddr = 0;
499
500
0
  *unconditionalbranch = false;
501
  /* Just a linear search of the table.  */
502
0
  for (op = microblaze_opcodes; op->name != 0; op ++)
503
0
    if (op->bit_sequence == (inst & op->opcode_mask))
504
0
      break;
505
506
0
  if (op->name == 0)
507
0
    {
508
0
      *targetvalid = false;
509
0
    }
510
0
  else if (op->instr_type == branch_inst)
511
0
    {
512
0
      switch (op->inst_type)
513
0
  {
514
0
        case INST_TYPE_R2:
515
0
          *unconditionalbranch = true;
516
        /* Fall through.  */
517
0
        case INST_TYPE_RD_R2:
518
0
        case INST_TYPE_R1_R2:
519
0
          targetaddr = r2val;
520
0
          *targetvalid = true;
521
0
          if (op->inst_offset_type == INST_PC_OFFSET)
522
0
      targetaddr += pcval;
523
0
          break;
524
0
        case INST_TYPE_IMM:
525
0
          *unconditionalbranch = true;
526
        /* Fall through.  */
527
0
        case INST_TYPE_RD_IMM:
528
0
        case INST_TYPE_R1_IMM:
529
0
          if (immfound)
530
0
      {
531
0
        targetaddr = (immval << 16) & 0xffff0000;
532
0
        targetaddr |= (get_int_field_imm (inst) & 0x0000ffff);
533
0
      }
534
0
    else
535
0
      {
536
0
        targetaddr = get_int_field_imm (inst);
537
0
        if (targetaddr & 0x8000)
538
0
          targetaddr |= 0xFFFF0000;
539
0
            }
540
0
          if (op->inst_offset_type == INST_PC_OFFSET)
541
0
      targetaddr += pcval;
542
0
          *targetvalid = true;
543
0
          break;
544
0
  default:
545
0
    *targetvalid = false;
546
0
    break;
547
0
        }
548
0
    }
549
0
  else if (op->instr_type == return_inst)
550
0
    {
551
0
      if (immfound)
552
0
  {
553
0
    targetaddr = (immval << 16) & 0xffff0000;
554
0
    targetaddr |= (get_int_field_imm (inst) & 0x0000ffff);
555
0
  }
556
0
      else
557
0
  {
558
0
    targetaddr = get_int_field_imm (inst);
559
0
    if (targetaddr & 0x8000)
560
0
      targetaddr |= 0xFFFF0000;
561
0
  }
562
0
      targetaddr += r1val;
563
0
      *targetvalid = true;
564
0
    }
565
0
  else
566
0
    *targetvalid = false;
567
0
  return targetaddr;
568
0
}