Coverage Report

Created: 2026-04-04 08:16

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