Coverage Report

Created: 2026-09-14 08:07

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
7.80k
#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
261k
{
37
261k
  int i = 0, op_index;
38
261k
  struct d30v_format *f;
39
261k
  struct d30v_opcode *op = (struct d30v_opcode *) d30v_opcode_table;
40
261k
  int op1 = (num >> 25) & 0x7;
41
261k
  int op2 = (num >> 20) & 0x1f;
42
261k
  int mod = (num >> 18) & 0x3;
43
44
  /* Find the opcode.  */
45
261k
  do
46
18.4M
    {
47
18.4M
      if ((op->op1 == op1) && (op->op2 == op2))
48
181k
  break;
49
18.2M
      op++;
50
18.2M
    }
51
18.2M
  while (op->name);
52
53
261k
  if (!op || !op->name)
54
79.8k
    return 0;
55
56
227k
  while (op->op1 == op1 && op->op2 == op2)
57
187k
    {
58
      /* Scan through all the formats for the opcode.  */
59
187k
      op_index = op->format[i++];
60
187k
      do
61
228k
  {
62
228k
    f = (struct d30v_format *) &d30v_format_table[op_index];
63
380k
    while (f->form == op_index)
64
293k
      {
65
293k
        if ((!is_long || f->form >= LONG) && (f->modifier == mod))
66
141k
    {
67
141k
      insn->form = f;
68
141k
      break;
69
141k
    }
70
151k
        f++;
71
151k
      }
72
228k
    if (insn->form)
73
141k
      break;
74
228k
  }
75
187k
      while ((op_index = op->format[i++]) != 0);
76
187k
      if (insn->form)
77
141k
  break;
78
46.0k
      op++;
79
46.0k
      i = 0;
80
46.0k
    }
81
181k
  if (insn->form == NULL)
82
39.6k
    return 0;
83
84
141k
  insn->op = op;
85
141k
  insn->ecc = (num >> 28) & 0x7;
86
141k
  if (op->format[1])
87
117k
    return 2;
88
24.1k
  else
89
24.1k
    return 1;
90
141k
}
91
92
static int
93
extract_value (uint64_t num, const struct d30v_operand *oper, int is_long)
94
261k
{
95
261k
  unsigned int val;
96
261k
  int shift = 12 - oper->position;
97
261k
  unsigned int mask = (0xFFFFFFFF >> (32 - oper->bits));
98
99
261k
  if (is_long)
100
8.69k
    {
101
8.69k
      if (oper->bits == 32)
102
  /* Piece together 32-bit constant.  */
103
3.03k
  val = ((num & 0x3FFFF)
104
3.03k
         | ((num & 0xFF00000) >> 2)
105
3.03k
         | ((num & 0x3F00000000LL) >> 6));
106
5.65k
      else
107
5.65k
  val = (num >> (32 + shift)) & mask;
108
8.69k
    }
109
253k
  else
110
253k
    val = (num >> shift) & mask;
111
112
261k
  if (oper->flags & OPERAND_SHIFT)
113
10.0k
    val <<= 3;
114
115
261k
  return val;
116
261k
}
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
141k
{
126
141k
  unsigned int val, opnum;
127
141k
  const struct d30v_operand *oper;
128
141k
  int i, match, need_comma = 0, need_paren = 0, found_control = 0;
129
141k
  unsigned int opind = 0;
130
131
141k
  (*info->fprintf_func) (info->stream, "%s", insn->op->name);
132
133
  /* Check for CMP or CMPU.  */
134
141k
  if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
135
1.17k
    {
136
1.17k
      opind++;
137
1.17k
      val =
138
1.17k
  extract_value (num,
139
1.17k
           &d30v_operand_table[insn->form->operands[0]],
140
1.17k
           is_long);
141
1.17k
      (*info->fprintf_func) (info->stream, "%s", d30v_cc_names[val]);
142
1.17k
    }
143
144
  /* Add in ".s" or ".l".  */
145
141k
  if (show_ext == 2)
146
117k
    {
147
117k
      if (is_long)
148
3.03k
  (*info->fprintf_func) (info->stream, ".l");
149
114k
      else
150
114k
  (*info->fprintf_func) (info->stream, ".s");
151
117k
    }
152
153
141k
  if (insn->ecc)
154
56.7k
    (*info->fprintf_func) (info->stream, "/%s", d30v_ecc_names[insn->ecc]);
155
156
141k
  (*info->fprintf_func) (info->stream, "\t");
157
158
427k
  while (opind < ARRAY_SIZE (insn->form->operands)
159
418k
   && (opnum = insn->form->operands[opind++]) != 0)
160
285k
    {
161
285k
      int bits;
162
163
285k
      oper = &d30v_operand_table[opnum];
164
285k
      bits = oper->bits;
165
285k
      if (oper->flags & OPERAND_SHIFT)
166
10.0k
  bits += 3;
167
168
285k
      if (need_comma
169
128k
    && oper->flags != OPERAND_PLUS
170
122k
    && oper->flags != OPERAND_MINUS)
171
120k
  {
172
120k
    need_comma = 0;
173
120k
    (*info->fprintf_func) (info->stream, ", ");
174
120k
  }
175
176
285k
      if (oper->flags == OPERAND_ATMINUS)
177
0
  {
178
0
    (*info->fprintf_func) (info->stream, "@-");
179
0
    continue;
180
0
  }
181
285k
      if (oper->flags == OPERAND_MINUS)
182
2.19k
  {
183
2.19k
    (*info->fprintf_func) (info->stream, "-");
184
2.19k
    continue;
185
2.19k
  }
186
283k
      if (oper->flags == OPERAND_PLUS)
187
6.50k
  {
188
6.50k
    (*info->fprintf_func) (info->stream, "+");
189
6.50k
    continue;
190
6.50k
  }
191
277k
      if (oper->flags == OPERAND_ATSIGN)
192
0
  {
193
0
    (*info->fprintf_func) (info->stream, "@");
194
0
    continue;
195
0
  }
196
277k
      if (oper->flags == OPERAND_ATPAR)
197
16.5k
  {
198
16.5k
    (*info->fprintf_func) (info->stream, "@(");
199
16.5k
    need_paren = 1;
200
16.5k
    continue;
201
16.5k
  }
202
203
260k
      if (oper->flags == OPERAND_SPECIAL)
204
1.39k
  continue;
205
206
259k
      val = extract_value (num, oper, is_long);
207
208
259k
      if (oper->flags & OPERAND_REG)
209
230k
  {
210
230k
    match = 0;
211
230k
    if (oper->flags & OPERAND_CONTROL)
212
1.39k
      {
213
1.39k
        const struct d30v_operand *oper3
214
1.39k
    = &d30v_operand_table[insn->form->operands[2]];
215
1.39k
        int id = extract_value (num, oper3, is_long);
216
217
1.39k
        found_control = 1;
218
1.39k
        switch (id)
219
1.39k
    {
220
620
    case 0:
221
620
      val |= OPERAND_CONTROL;
222
620
      break;
223
292
    case 1:
224
563
    case 2:
225
563
      val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
226
563
      break;
227
214
    case 3:
228
214
      val |= OPERAND_FLAG;
229
214
      break;
230
0
    default:
231
      /* xgettext: c-format */
232
0
      opcodes_error_handler (_("illegal id (%d)"), id);
233
0
      abort ();
234
1.39k
    }
235
1.39k
      }
236
229k
    else if (oper->flags & OPERAND_ACC)
237
815
      val |= OPERAND_ACC;
238
228k
    else if (oper->flags & OPERAND_FLAG)
239
5.93k
      val |= OPERAND_FLAG;
240
24.9M
    for (i = 0; i < reg_name_cnt (); i++)
241
24.9M
      {
242
24.9M
        if (val == pre_defined_registers[i].value)
243
230k
    {
244
230k
      if (pre_defined_registers[i].pname)
245
8.02k
        (*info->fprintf_func)
246
8.02k
          (info->stream, "%s", pre_defined_registers[i].pname);
247
222k
      else
248
222k
        (*info->fprintf_func)
249
222k
          (info->stream, "%s", pre_defined_registers[i].name);
250
230k
      match = 1;
251
230k
      break;
252
230k
    }
253
24.9M
      }
254
230k
    if (match == 0)
255
210
      {
256
        /* This would only get executed if a register was not in
257
     the register table.  */
258
210
        (*info->fprintf_func)
259
210
    (info->stream, _("<unknown register %d>"), val & 0x3F);
260
210
      }
261
230k
  }
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
28.7k
      else if (oper->flags & OPERAND_PCREL)
268
7.80k
  {
269
7.80k
    int neg = 0;
270
271
    /* IMM6S3 is unsigned.  */
272
7.80k
    if (oper->flags & OPERAND_SIGNED || bits == 32)
273
5.71k
      {
274
5.71k
        unsigned int sign = 1u << (bits - 1);
275
5.71k
        if (val & sign)
276
2.34k
    {
277
2.34k
      val = -val & (sign + sign - 1);
278
2.34k
      neg = 1;
279
2.34k
    }
280
5.71k
      }
281
7.80k
    if (neg)
282
2.34k
      {
283
2.34k
        (*info->fprintf_func) (info->stream, "-%x\t(", val);
284
2.34k
        (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
285
2.34k
        (*info->fprintf_func) (info->stream, ")");
286
2.34k
      }
287
5.45k
    else
288
5.45k
      {
289
5.45k
        (*info->fprintf_func) (info->stream, "%x\t(", val);
290
5.45k
        (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
291
5.45k
        (*info->fprintf_func) (info->stream, ")");
292
5.45k
      }
293
7.80k
  }
294
20.9k
      else if (insn->op->reloc_flag == RELOC_ABS)
295
2.56k
  {
296
2.56k
    (*info->print_address_func) (val, info);
297
2.56k
  }
298
18.3k
      else
299
18.3k
  {
300
18.3k
    if (oper->flags & OPERAND_SIGNED)
301
13.6k
      {
302
13.6k
        unsigned int sign = 1u << (bits - 1);
303
304
13.6k
        if (val & sign)
305
6.20k
    {
306
6.20k
      val = -val & (sign + sign - 1);
307
6.20k
      (*info->fprintf_func) (info->stream, "-");
308
6.20k
    }
309
13.6k
      }
310
18.3k
    (*info->fprintf_func) (info->stream, "0x%x", val);
311
18.3k
  }
312
      /* If there is another operand, then write a comma and space.  */
313
259k
      if (opind < ARRAY_SIZE (insn->form->operands)
314
250k
    && insn->form->operands[opind]
315
121k
    && !(found_control && opind == 2))
316
120k
  need_comma = 1;
317
259k
    }
318
141k
  if (need_paren)
319
16.5k
    (*info->fprintf_func) (info->stream, ")");
320
141k
}
321
322
int
323
print_insn_d30v (bfd_vma memaddr, struct disassemble_info *info)
324
143k
{
325
143k
  int status, result;
326
143k
  bfd_byte buffer[12];
327
143k
  uint32_t in1, in2;
328
143k
  struct d30v_insn insn;
329
143k
  uint64_t num;
330
331
143k
  insn.form = NULL;
332
333
143k
  info->bytes_per_line = 8;
334
143k
  info->bytes_per_chunk = 4;
335
143k
  info->display_endian = BFD_ENDIAN_BIG;
336
337
143k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
338
143k
  if (status != 0)
339
223
    {
340
223
      (*info->memory_error_func) (status, memaddr, info);
341
223
      return -1;
342
223
    }
343
143k
  in1 = bfd_getb32 (buffer);
344
345
143k
  status = (*info->read_memory_func) (memaddr + 4, buffer, 4, info);
346
143k
  if (status != 0)
347
305
    {
348
305
      info->bytes_per_line = 8;
349
305
      if (!(result = lookup_opcode (&insn, in1, 0)))
350
137
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in1);
351
168
      else
352
168
  print_insn (info, memaddr, (uint64_t) in1, &insn, 0, result);
353
305
      return 4;
354
305
    }
355
143k
  in2 = bfd_getb32 (buffer);
356
357
143k
  if (in1 & in2 & FM01)
358
25.4k
    {
359
      /* LONG instruction.  */
360
25.4k
      if (!(result = lookup_opcode (&insn, in1, 1)))
361
22.4k
  {
362
22.4k
    (*info->fprintf_func) (info->stream, ".long\t0x%x,0x%x", in1, in2);
363
22.4k
    return 8;
364
22.4k
  }
365
3.03k
      num = (uint64_t) in1 << 32 | in2;
366
3.03k
      print_insn (info, memaddr, num, &insn, 1, result);
367
3.03k
    }
368
117k
  else
369
117k
    {
370
117k
      num = in1;
371
117k
      if (!(result = lookup_opcode (&insn, in1, 0)))
372
49.6k
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in1);
373
68.0k
      else
374
68.0k
  print_insn (info, memaddr, num, &insn, 0, result);
375
376
117k
      switch (((in1 >> 31) << 1) | (in2 >> 31))
377
117k
  {
378
76.6k
  case 0:
379
76.6k
    (*info->fprintf_func) (info->stream, "\t||\t");
380
76.6k
    break;
381
20.5k
  case 1:
382
20.5k
    (*info->fprintf_func) (info->stream, "\t->\t");
383
20.5k
    break;
384
20.4k
  case 2:
385
20.4k
    (*info->fprintf_func) (info->stream, "\t<-\t");
386
20.4k
  default:
387
20.4k
    break;
388
117k
  }
389
390
117k
      insn.form = NULL;
391
117k
      num = in2;
392
117k
      if (!(result = lookup_opcode (&insn, in2, 0)))
393
47.3k
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in2);
394
70.3k
      else
395
70.3k
  print_insn (info, memaddr, num, &insn, 0, result);
396
117k
    }
397
120k
  return 8;
398
143k
}