Coverage Report

Created: 2023-08-28 06:31

/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
63.1k
#define get_field_rd(buf, instr)   get_field (buf, instr, RD_MASK, RD_LOW)
33
68.1k
#define get_field_r1(buf, instr)   get_field (buf, instr, RA_MASK, RA_LOW)
34
41.2k
#define get_field_r2(buf, instr)   get_field (buf, instr, RB_MASK, RB_LOW)
35
10.4k
#define get_int_field_imm(instr)   ((instr & IMM_MASK) >> IMM_LOW)
36
21.5k
#define get_int_field_r1(instr)    ((instr & RA_MASK) >> RA_LOW)
37
38
202k
#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
202k
{
50
202k
#ifdef ENABLE_CHECKING
51
202k
  if (buf->which >= NUM_STRBUFS)
52
0
    abort ();
53
202k
#endif
54
202k
  return buf->str[buf->which++];
55
202k
}
56
57
static char *
58
get_field (struct string_buf *buf, long instr, long mask, unsigned short low)
59
172k
{
60
172k
  char *p = strbuf (buf);
61
62
172k
  sprintf (p, "%s%d", register_prefix, (int)((instr & mask) >> low));
63
172k
  return p;
64
172k
}
65
66
static char *
67
get_field_imm (struct string_buf *buf, long instr)
68
27.7k
{
69
27.7k
  char *p = strbuf (buf);
70
71
27.7k
  sprintf (p, "%d", (short)((instr & IMM_MASK) >> IMM_LOW));
72
27.7k
  return p;
73
27.7k
}
74
75
static char *
76
get_field_imm5 (struct string_buf *buf, long instr)
77
491
{
78
491
  char *p = strbuf (buf);
79
80
491
  sprintf (p, "%d", (short)((instr & IMM5_MASK) >> IMM_LOW));
81
491
  return p;
82
491
}
83
84
static char *
85
get_field_imm5_mbar (struct string_buf *buf, long instr)
86
141
{
87
141
  char *p = strbuf (buf);
88
89
141
  sprintf (p, "%d", (short)((instr & IMM5_MBAR_MASK) >> IMM_MBAR));
90
141
  return p;
91
141
}
92
93
static char *
94
get_field_rfsl (struct string_buf *buf, long instr)
95
1.03k
{
96
1.03k
  char *p = strbuf (buf);
97
98
1.03k
  sprintf (p, "%s%d", fsl_register_prefix,
99
1.03k
     (short)((instr & RFSL_MASK) >> IMM_LOW));
100
1.03k
  return p;
101
1.03k
}
102
103
static char *
104
get_field_imm15 (struct string_buf *buf, long instr)
105
22
{
106
22
  char *p = strbuf (buf);
107
108
22
  sprintf (p, "%d", (short)((instr & IMM15_MASK) >> IMM_LOW));
109
22
  return p;
110
22
}
111
112
static char *
113
get_field_special (struct string_buf *buf, long instr,
114
       const struct op_code_struct *op)
115
487
{
116
487
  char *p = strbuf (buf);
117
487
  char *spr;
118
119
487
  switch ((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask))
120
487
    {
121
1
    case REG_MSR_MASK :
122
1
      spr = "msr";
123
1
      break;
124
0
    case REG_PC_MASK :
125
0
      spr = "pc";
126
0
      break;
127
24
    case REG_EAR_MASK :
128
24
      spr = "ear";
129
24
      break;
130
3
    case REG_ESR_MASK :
131
3
      spr = "esr";
132
3
      break;
133
1
    case REG_FSR_MASK :
134
1
      spr = "fsr";
135
1
      break;
136
0
    case REG_BTR_MASK :
137
0
      spr = "btr";
138
0
      break;
139
16
    case REG_EDR_MASK :
140
16
      spr = "edr";
141
16
      break;
142
12
    case REG_PID_MASK :
143
12
      spr = "pid";
144
12
      break;
145
26
    case REG_ZPR_MASK :
146
26
      spr = "zpr";
147
26
      break;
148
0
    case REG_TLBX_MASK :
149
0
      spr = "tlbx";
150
0
      break;
151
15
    case REG_TLBLO_MASK :
152
15
      spr = "tlblo";
153
15
      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
389
    default :
167
389
      if (((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask) & 0xE000)
168
389
    == REG_PVR_MASK)
169
173
  {
170
173
    sprintf (p, "%spvr%d", register_prefix,
171
173
       (unsigned short)(((instr & IMM_MASK) >> IMM_LOW)
172
173
            ^ op->immval_mask) ^ REG_PVR_MASK);
173
173
    return p;
174
173
  }
175
216
      else
176
216
  spr = "pc";
177
216
      break;
178
487
    }
179
180
314
   sprintf (p, "%s%s", register_prefix, spr);
181
314
   return p;
182
487
}
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
74.2k
{
189
74.2k
  unsigned char       ibytes[4];
190
74.2k
  int                 status;
191
74.2k
  const struct op_code_struct *op;
192
74.2k
  unsigned long inst;
193
194
74.2k
  status = info->read_memory_func (memaddr, ibytes, 4, info);
195
196
74.2k
  if (status != 0)
197
62
    {
198
62
      info->memory_error_func (status, memaddr, info);
199
62
      return 0;
200
62
    }
201
202
74.2k
  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
69.6k
  else if (info->endian == BFD_ENDIAN_LITTLE)
206
69.6k
    inst = (((unsigned) ibytes[3] << 24) | (ibytes[2] << 16)
207
69.6k
      | (ibytes[1] << 8) | ibytes[0]);
208
0
  else
209
0
    abort ();
210
211
  /* Just a linear search of the table.  */
212
13.5M
  for (op = microblaze_opcodes; op->name != 0; op ++)
213
13.5M
    if (op->bit_sequence == (inst & op->opcode_mask))
214
74.2k
      break;
215
216
74.2k
  *opr = op;
217
74.2k
  return inst;
218
74.2k
}
219
220
221
int
222
print_insn_microblaze (bfd_vma memaddr, struct disassemble_info * info)
223
72.0k
{
224
72.0k
  fprintf_ftype print_func = info->fprintf_func;
225
72.0k
  void *stream = info->stream;
226
72.0k
  unsigned long inst, prev_inst;
227
72.0k
  const struct op_code_struct *op, *pop;
228
72.0k
  int immval = 0;
229
72.0k
  bool immfound = false;
230
72.0k
  static bfd_vma prev_insn_addr = -1; /* Init the prev insn addr.  */
231
72.0k
  static int prev_insn_vma = -1;  /* Init the prev insn vma.  */
232
72.0k
  int curr_insn_vma = info->buffer_vma;
233
72.0k
  struct string_buf buf;
234
235
72.0k
  buf.which = 0;
236
72.0k
  info->bytes_per_chunk = 4;
237
238
72.0k
  inst = read_insn_microblaze (memaddr, info, &op);
239
72.0k
  if (inst == 0)
240
127
    return -1;
241
242
71.9k
  if (prev_insn_vma == curr_insn_vma)
243
2.32k
    {
244
2.32k
      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.32k
    }
261
262
  /* Make curr insn as prev insn.  */
263
71.9k
  prev_insn_addr = memaddr;
264
71.9k
  prev_insn_vma = curr_insn_vma;
265
266
71.9k
  if (op->name == NULL)
267
0
    print_func (stream, ".short 0x%04x", (unsigned int) inst);
268
71.9k
  else
269
71.9k
    {
270
71.9k
      print_func (stream, "%s", op->name);
271
272
71.9k
      switch (op->inst_type)
273
71.9k
  {
274
38.7k
  case INST_TYPE_RD_R1_R2:
275
38.7k
    print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
276
38.7k
          get_field_r1 (&buf, inst), get_field_r2 (&buf, inst));
277
38.7k
    break;
278
21.5k
  case INST_TYPE_RD_R1_IMM:
279
21.5k
    print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
280
21.5k
          get_field_r1 (&buf, inst), get_field_imm (&buf, inst));
281
21.5k
    if (info->print_address_func && get_int_field_r1 (inst) == 0
282
21.5k
        && info->symbol_at_address_func)
283
4.31k
      {
284
4.31k
        if (immfound)
285
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
286
4.31k
        else
287
4.31k
    {
288
4.31k
      immval = get_int_field_imm (inst);
289
4.31k
      if (immval & 0x8000)
290
2.43k
        immval |= 0xFFFF0000;
291
4.31k
    }
292
4.31k
        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
4.31k
      }
298
21.5k
    break;
299
491
  case INST_TYPE_RD_R1_IMM5:
300
491
    print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
301
491
          get_field_r1 (&buf, inst), get_field_imm5 (&buf, inst));
302
491
    break;
303
868
  case INST_TYPE_RD_RFSL:
304
868
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
305
868
          get_field_rfsl (&buf, inst));
306
868
    break;
307
42
  case INST_TYPE_R1_RFSL:
308
42
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
309
42
          get_field_rfsl (&buf, inst));
310
42
    break;
311
487
  case INST_TYPE_RD_SPECIAL:
312
487
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
313
487
          get_field_special (&buf, inst, op));
314
487
    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
625
  case INST_TYPE_RD_R1:
320
625
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
321
625
          get_field_r1 (&buf, inst));
322
625
    break;
323
1.81k
  case INST_TYPE_R1_R2:
324
1.81k
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
325
1.81k
          get_field_r2 (&buf, inst));
326
1.81k
    break;
327
4.56k
  case INST_TYPE_R1_IMM:
328
4.56k
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
329
4.56k
          get_field_imm (&buf, inst));
330
    /* The non-pc relative instructions are returns, which shouldn't
331
       have a label printed.  */
332
4.56k
    if (info->print_address_func && op->inst_offset_type == INST_PC_OFFSET
333
4.56k
        && info->symbol_at_address_func)
334
4.52k
      {
335
4.52k
        if (immfound)
336
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
337
4.52k
        else
338
4.52k
    {
339
4.52k
      immval = get_int_field_imm (inst);
340
4.52k
      if (immval & 0x8000)
341
4.08k
        immval |= 0xFFFF0000;
342
4.52k
    }
343
4.52k
        immval += memaddr;
344
4.52k
        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
4.52k
        else
350
4.52k
    {
351
4.52k
      print_func (stream, "\t\t// ");
352
4.52k
      print_func (stream, "%x", immval);
353
4.52k
    }
354
4.52k
      }
355
4.56k
    break;
356
100
  case INST_TYPE_RD_IMM:
357
100
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
358
100
          get_field_imm (&buf, inst));
359
100
    if (info->print_address_func && info->symbol_at_address_func)
360
100
      {
361
100
        if (immfound)
362
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
363
100
        else
364
100
    {
365
100
      immval = get_int_field_imm (inst);
366
100
      if (immval & 0x8000)
367
69
        immval |= 0xFFFF0000;
368
100
    }
369
100
        if (op->inst_offset_type == INST_PC_OFFSET)
370
21
    immval += (int) memaddr;
371
100
        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
100
      }
377
100
    break;
378
1.55k
  case INST_TYPE_IMM:
379
1.55k
    print_func (stream, "\t%s", get_field_imm (&buf, inst));
380
1.55k
    if (info->print_address_func && info->symbol_at_address_func
381
1.55k
        && op->instr != imm)
382
1.50k
      {
383
1.50k
        if (immfound)
384
0
    immval |= (get_int_field_imm (inst) & 0x0000ffff);
385
1.50k
        else
386
1.50k
    {
387
1.50k
      immval = get_int_field_imm (inst);
388
1.50k
      if (immval & 0x8000)
389
436
        immval |= 0xFFFF0000;
390
1.50k
    }
391
1.50k
        if (op->inst_offset_type == INST_PC_OFFSET)
392
780
    immval += (int) memaddr;
393
1.50k
        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
1.50k
        else if (op->inst_offset_type == INST_PC_OFFSET)
399
780
    {
400
780
      print_func (stream, "\t\t// ");
401
780
      print_func (stream, "%x", immval);
402
780
    }
403
1.50k
      }
404
1.55k
    break;
405
261
  case INST_TYPE_RD_R2:
406
261
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
407
261
          get_field_r2 (&buf, inst));
408
261
    break;
409
173
  case INST_TYPE_R2:
410
173
    print_func (stream, "\t%s", get_field_r2 (&buf, inst));
411
173
    break;
412
0
  case INST_TYPE_R1:
413
0
    print_func (stream, "\t%s", get_field_r1 (&buf, inst));
414
0
    break;
415
271
  case INST_TYPE_R1_R2_SPECIAL:
416
271
    print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
417
271
          get_field_r2 (&buf, inst));
418
271
    break;
419
22
  case INST_TYPE_RD_IMM15:
420
22
    print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
421
22
          get_field_imm15 (&buf, inst));
422
22
    break;
423
    /* For mbar insn.  */
424
141
  case INST_TYPE_IMM5:
425
141
    print_func (stream, "\t%s", get_field_imm5_mbar (&buf, inst));
426
141
    break;
427
    /* For mbar 16 or sleep insn.  */
428
59
  case INST_TYPE_NONE:
429
59
    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
128
  case INST_TYPE_RFSL:
435
128
    print_func (stream, "\t%s", get_field_rfsl (&buf, inst));
436
128
    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
71.9k
  }
443
71.9k
    }
444
445
  /* Say how many bytes we consumed.  */
446
71.9k
  return 4;
447
71.9k
}
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
}