Coverage Report

Created: 2026-05-11 07:54

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