Coverage Report

Created: 2026-11-21 09:00

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