Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/d30v-dis.c
Line
Count
Source
1
/* Disassemble D30V instructions.
2
   Copyright (C) 1997-2026 Free Software Foundation, Inc.
3
4
   This file is part of the GNU opcodes library.
5
6
   This library is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3, or (at your option)
9
   any later version.
10
11
   It is distributed in the hope that it will be useful, but WITHOUT
12
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14
   License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include <stdio.h>
23
#include "opcode/d30v.h"
24
#include "disassemble.h"
25
#include "opintl.h"
26
#include "libiberty.h"
27
28
5.93k
#define PC_MASK 0xFFFFFFFF
29
30
/* Return 0 if lookup fails,
31
   1 if found and only one form,
32
   2 if found and there are short and long forms.  */
33
34
static int
35
lookup_opcode (struct d30v_insn *insn, long num, int is_long)
36
172k
{
37
172k
  int i = 0, op_index;
38
172k
  struct d30v_format *f;
39
172k
  struct d30v_opcode *op = (struct d30v_opcode *) d30v_opcode_table;
40
172k
  int op1 = (num >> 25) & 0x7;
41
172k
  int op2 = (num >> 20) & 0x1f;
42
172k
  int mod = (num >> 18) & 0x3;
43
44
  /* Find the opcode.  */
45
172k
  do
46
12.5M
    {
47
12.5M
      if ((op->op1 == op1) && (op->op2 == op2))
48
116k
  break;
49
12.4M
      op++;
50
12.4M
    }
51
12.4M
  while (op->name);
52
53
172k
  if (!op || !op->name)
54
56.3k
    return 0;
55
56
147k
  while (op->op1 == op1 && op->op2 == op2)
57
120k
    {
58
      /* Scan through all the formats for the opcode.  */
59
120k
      op_index = op->format[i++];
60
120k
      do
61
146k
  {
62
146k
    f = (struct d30v_format *) &d30v_format_table[op_index];
63
243k
    while (f->form == op_index)
64
186k
      {
65
186k
        if ((!is_long || f->form >= LONG) && (f->modifier == mod))
66
89.4k
    {
67
89.4k
      insn->form = f;
68
89.4k
      break;
69
89.4k
    }
70
97.0k
        f++;
71
97.0k
      }
72
146k
    if (insn->form)
73
89.4k
      break;
74
146k
  }
75
120k
      while ((op_index = op->format[i++]) != 0);
76
120k
      if (insn->form)
77
89.4k
  break;
78
31.0k
      op++;
79
31.0k
      i = 0;
80
31.0k
    }
81
116k
  if (insn->form == NULL)
82
27.0k
    return 0;
83
84
89.4k
  insn->op = op;
85
89.4k
  insn->ecc = (num >> 28) & 0x7;
86
89.4k
  if (op->format[1])
87
74.3k
    return 2;
88
15.1k
  else
89
15.1k
    return 1;
90
89.4k
}
91
92
static int
93
extract_value (uint64_t num, const struct d30v_operand *oper, int is_long)
94
163k
{
95
163k
  unsigned int val;
96
163k
  int shift = 12 - oper->position;
97
163k
  unsigned int mask = (0xFFFFFFFF >> (32 - oper->bits));
98
99
163k
  if (is_long)
100
3.90k
    {
101
3.90k
      if (oper->bits == 32)
102
  /* Piece together 32-bit constant.  */
103
1.42k
  val = ((num & 0x3FFFF)
104
1.42k
         | ((num & 0xFF00000) >> 2)
105
1.42k
         | ((num & 0x3F00000000LL) >> 6));
106
2.48k
      else
107
2.48k
  val = (num >> (32 + shift)) & mask;
108
3.90k
    }
109
159k
  else
110
159k
    val = (num >> shift) & mask;
111
112
163k
  if (oper->flags & OPERAND_SHIFT)
113
7.25k
    val <<= 3;
114
115
163k
  return val;
116
163k
}
117
118
static void
119
print_insn (struct disassemble_info *info,
120
      bfd_vma memaddr,
121
      uint64_t num,
122
      struct d30v_insn *insn,
123
      int is_long,
124
      int show_ext)
125
89.4k
{
126
89.4k
  unsigned int val, opnum;
127
89.4k
  const struct d30v_operand *oper;
128
89.4k
  int i, match, need_comma = 0, need_paren = 0, found_control = 0;
129
89.4k
  unsigned int opind = 0;
130
131
89.4k
  (*info->fprintf_func) (info->stream, "%s", insn->op->name);
132
133
  /* Check for CMP or CMPU.  */
134
89.4k
  if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
135
374
    {
136
374
      opind++;
137
374
      val =
138
374
  extract_value (num,
139
374
           &d30v_operand_table[insn->form->operands[0]],
140
374
           is_long);
141
374
      (*info->fprintf_func) (info->stream, "%s", d30v_cc_names[val]);
142
374
    }
143
144
  /* Add in ".s" or ".l".  */
145
89.4k
  if (show_ext == 2)
146
74.3k
    {
147
74.3k
      if (is_long)
148
1.42k
  (*info->fprintf_func) (info->stream, ".l");
149
72.9k
      else
150
72.9k
  (*info->fprintf_func) (info->stream, ".s");
151
74.3k
    }
152
153
89.4k
  if (insn->ecc)
154
35.3k
    (*info->fprintf_func) (info->stream, "/%s", d30v_ecc_names[insn->ecc]);
155
156
89.4k
  (*info->fprintf_func) (info->stream, "\t");
157
158
269k
  while (opind < ARRAY_SIZE (insn->form->operands)
159
263k
   && (opnum = insn->form->operands[opind++]) != 0)
160
180k
    {
161
180k
      int bits;
162
163
180k
      oper = &d30v_operand_table[opnum];
164
180k
      bits = oper->bits;
165
180k
      if (oper->flags & OPERAND_SHIFT)
166
7.25k
  bits += 3;
167
168
180k
      if (need_comma
169
80.3k
    && oper->flags != OPERAND_PLUS
170
75.4k
    && oper->flags != OPERAND_MINUS)
171
73.9k
  {
172
73.9k
    need_comma = 0;
173
73.9k
    (*info->fprintf_func) (info->stream, ", ");
174
73.9k
  }
175
176
180k
      if (oper->flags == OPERAND_ATMINUS)
177
0
  {
178
0
    (*info->fprintf_func) (info->stream, "@-");
179
0
    continue;
180
0
  }
181
180k
      if (oper->flags == OPERAND_MINUS)
182
1.50k
  {
183
1.50k
    (*info->fprintf_func) (info->stream, "-");
184
1.50k
    continue;
185
1.50k
  }
186
178k
      if (oper->flags == OPERAND_PLUS)
187
4.87k
  {
188
4.87k
    (*info->fprintf_func) (info->stream, "+");
189
4.87k
    continue;
190
4.87k
  }
191
174k
      if (oper->flags == OPERAND_ATSIGN)
192
0
  {
193
0
    (*info->fprintf_func) (info->stream, "@");
194
0
    continue;
195
0
  }
196
174k
      if (oper->flags == OPERAND_ATPAR)
197
11.2k
  {
198
11.2k
    (*info->fprintf_func) (info->stream, "@(");
199
11.2k
    need_paren = 1;
200
11.2k
    continue;
201
11.2k
  }
202
203
162k
      if (oper->flags == OPERAND_SPECIAL)
204
1.08k
  continue;
205
206
161k
      val = extract_value (num, oper, is_long);
207
208
161k
      if (oper->flags & OPERAND_REG)
209
144k
  {
210
144k
    match = 0;
211
144k
    if (oper->flags & OPERAND_CONTROL)
212
1.08k
      {
213
1.08k
        const struct d30v_operand *oper3
214
1.08k
    = &d30v_operand_table[insn->form->operands[2]];
215
1.08k
        int id = extract_value (num, oper3, is_long);
216
217
1.08k
        found_control = 1;
218
1.08k
        switch (id)
219
1.08k
    {
220
501
    case 0:
221
501
      val |= OPERAND_CONTROL;
222
501
      break;
223
389
    case 1:
224
500
    case 2:
225
500
      val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
226
500
      break;
227
88
    case 3:
228
88
      val |= OPERAND_FLAG;
229
88
      break;
230
0
    default:
231
      /* xgettext: c-format */
232
0
      opcodes_error_handler (_("illegal id (%d)"), id);
233
0
      abort ();
234
1.08k
    }
235
1.08k
      }
236
143k
    else if (oper->flags & OPERAND_ACC)
237
555
      val |= OPERAND_ACC;
238
142k
    else if (oper->flags & OPERAND_FLAG)
239
3.41k
      val |= OPERAND_FLAG;
240
15.6M
    for (i = 0; i < reg_name_cnt (); i++)
241
15.6M
      {
242
15.6M
        if (val == pre_defined_registers[i].value)
243
144k
    {
244
144k
      if (pre_defined_registers[i].pname)
245
5.15k
        (*info->fprintf_func)
246
5.15k
          (info->stream, "%s", pre_defined_registers[i].pname);
247
139k
      else
248
139k
        (*info->fprintf_func)
249
139k
          (info->stream, "%s", pre_defined_registers[i].name);
250
144k
      match = 1;
251
144k
      break;
252
144k
    }
253
15.6M
      }
254
144k
    if (match == 0)
255
82
      {
256
        /* This would only get executed if a register was not in
257
     the register table.  */
258
82
        (*info->fprintf_func)
259
82
    (info->stream, _("<unknown register %d>"), val & 0x3F);
260
82
      }
261
144k
  }
262
      /* repeati has a relocation, but its first argument is a plain
263
   immediate.  OTOH instructions like djsri have a pc-relative
264
   delay target, but an absolute jump target.  Therefore, a test
265
   of insn->op->reloc_flag is not specific enough; we must test
266
   if the actual operand we are handling now is pc-relative.  */
267
17.2k
      else if (oper->flags & OPERAND_PCREL)
268
5.93k
  {
269
5.93k
    int neg = 0;
270
271
    /* IMM6S3 is unsigned.  */
272
5.93k
    if (oper->flags & OPERAND_SIGNED || bits == 32)
273
3.93k
      {
274
3.93k
        unsigned int sign = 1u << (bits - 1);
275
3.93k
        if (val & sign)
276
1.32k
    {
277
1.32k
      val = -val & (sign + sign - 1);
278
1.32k
      neg = 1;
279
1.32k
    }
280
3.93k
      }
281
5.93k
    if (neg)
282
1.32k
      {
283
1.32k
        (*info->fprintf_func) (info->stream, "-%x\t(", val);
284
1.32k
        (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
285
1.32k
        (*info->fprintf_func) (info->stream, ")");
286
1.32k
      }
287
4.61k
    else
288
4.61k
      {
289
4.61k
        (*info->fprintf_func) (info->stream, "%x\t(", val);
290
4.61k
        (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
291
4.61k
        (*info->fprintf_func) (info->stream, ")");
292
4.61k
      }
293
5.93k
  }
294
11.2k
      else if (insn->op->reloc_flag == RELOC_ABS)
295
1.58k
  {
296
1.58k
    (*info->print_address_func) (val, info);
297
1.58k
  }
298
9.69k
      else
299
9.69k
  {
300
9.69k
    if (oper->flags & OPERAND_SIGNED)
301
7.10k
      {
302
7.10k
        unsigned int sign = 1u << (bits - 1);
303
304
7.10k
        if (val & sign)
305
3.38k
    {
306
3.38k
      val = -val & (sign + sign - 1);
307
3.38k
      (*info->fprintf_func) (info->stream, "-");
308
3.38k
    }
309
7.10k
      }
310
9.69k
    (*info->fprintf_func) (info->stream, "0x%x", val);
311
9.69k
  }
312
      /* If there is another operand, then write a comma and space.  */
313
161k
      if (opind < ARRAY_SIZE (insn->form->operands)
314
155k
    && insn->form->operands[opind]
315
75.0k
    && !(found_control && opind == 2))
316
73.9k
  need_comma = 1;
317
161k
    }
318
89.4k
  if (need_paren)
319
11.2k
    (*info->fprintf_func) (info->stream, ")");
320
89.4k
}
321
322
int
323
print_insn_d30v (bfd_vma memaddr, struct disassemble_info *info)
324
96.0k
{
325
96.0k
  int status, result;
326
96.0k
  bfd_byte buffer[12];
327
96.0k
  uint32_t in1, in2;
328
96.0k
  struct d30v_insn insn;
329
96.0k
  uint64_t num;
330
331
96.0k
  insn.form = NULL;
332
333
96.0k
  info->bytes_per_line = 8;
334
96.0k
  info->bytes_per_chunk = 4;
335
96.0k
  info->display_endian = BFD_ENDIAN_BIG;
336
337
96.0k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
338
96.0k
  if (status != 0)
339
114
    {
340
114
      (*info->memory_error_func) (status, memaddr, info);
341
114
      return -1;
342
114
    }
343
95.9k
  in1 = bfd_getb32 (buffer);
344
345
95.9k
  status = (*info->read_memory_func) (memaddr + 4, buffer, 4, info);
346
95.9k
  if (status != 0)
347
174
    {
348
174
      info->bytes_per_line = 8;
349
174
      if (!(result = lookup_opcode (&insn, in1, 0)))
350
79
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in1);
351
95
      else
352
95
  print_insn (info, memaddr, (uint64_t) in1, &insn, 0, result);
353
174
      return 4;
354
174
    }
355
95.7k
  in2 = bfd_getb32 (buffer);
356
357
95.7k
  if (in1 & in2 & FM01)
358
18.6k
    {
359
      /* LONG instruction.  */
360
18.6k
      if (!(result = lookup_opcode (&insn, in1, 1)))
361
17.2k
  {
362
17.2k
    (*info->fprintf_func) (info->stream, ".long\t0x%x,0x%x", in1, in2);
363
17.2k
    return 8;
364
17.2k
  }
365
1.42k
      num = (uint64_t) in1 << 32 | in2;
366
1.42k
      print_insn (info, memaddr, num, &insn, 1, result);
367
1.42k
    }
368
77.0k
  else
369
77.0k
    {
370
77.0k
      num = in1;
371
77.0k
      if (!(result = lookup_opcode (&insn, in1, 0)))
372
33.6k
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in1);
373
43.4k
      else
374
43.4k
  print_insn (info, memaddr, num, &insn, 0, result);
375
376
77.0k
      switch (((in1 >> 31) << 1) | (in2 >> 31))
377
77.0k
  {
378
48.6k
  case 0:
379
48.6k
    (*info->fprintf_func) (info->stream, "\t||\t");
380
48.6k
    break;
381
14.2k
  case 1:
382
14.2k
    (*info->fprintf_func) (info->stream, "\t->\t");
383
14.2k
    break;
384
14.1k
  case 2:
385
14.1k
    (*info->fprintf_func) (info->stream, "\t<-\t");
386
14.1k
  default:
387
14.1k
    break;
388
77.0k
  }
389
390
77.0k
      insn.form = NULL;
391
77.0k
      num = in2;
392
77.0k
      if (!(result = lookup_opcode (&insn, in2, 0)))
393
32.5k
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in2);
394
44.5k
      else
395
44.5k
  print_insn (info, memaddr, num, &insn, 0, result);
396
77.0k
    }
397
78.4k
  return 8;
398
95.7k
}