Coverage Report

Created: 2025-07-08 11:15

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