Coverage Report

Created: 2026-03-10 08:46

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