Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/d10v-dis.c
Line
Count
Source
1
/* Disassemble D10V instructions.
2
   Copyright (C) 1996-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/d10v.h"
24
#include "disassemble.h"
25
26
/* The PC wraps at 18 bits, except for the segment number,
27
   so use this mask to keep the parts we want.  */
28
1.40k
#define PC_MASK 0x0303FFFF
29
30
static void
31
print_operand (struct d10v_operand *oper,
32
         unsigned long insn,
33
         struct d10v_opcode *op,
34
         bfd_vma memaddr,
35
         struct disassemble_info *info)
36
101k
{
37
101k
  int num, shift;
38
39
101k
  if (oper->flags == OPERAND_ATMINUS)
40
105
    {
41
105
      (*info->fprintf_func) (info->stream, "@-");
42
105
      return;
43
105
    }
44
101k
  if (oper->flags == OPERAND_MINUS)
45
587
    {
46
587
      (*info->fprintf_func) (info->stream, "-");
47
587
      return;
48
587
    }
49
100k
  if (oper->flags == OPERAND_PLUS)
50
1.31k
    {
51
1.31k
      (*info->fprintf_func) (info->stream, "+");
52
1.31k
      return;
53
1.31k
    }
54
99.6k
  if (oper->flags == OPERAND_ATSIGN)
55
3.96k
    {
56
3.96k
      (*info->fprintf_func) (info->stream, "@");
57
3.96k
      return;
58
3.96k
    }
59
95.6k
  if (oper->flags == OPERAND_ATPAR)
60
1.43k
    {
61
1.43k
      (*info->fprintf_func) (info->stream, "@(");
62
1.43k
      return;
63
1.43k
    }
64
65
94.2k
  shift = oper->shift;
66
67
  /* The LONG_L format shifts registers over by 15.  */
68
94.2k
  if (op->format == LONG_L && (oper->flags & OPERAND_REG))
69
4.05k
    shift += 15;
70
71
94.2k
  num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
72
73
94.2k
  if (oper->flags & OPERAND_REG)
74
84.0k
    {
75
84.0k
      int i;
76
84.0k
      int match = 0;
77
78
84.0k
      num += (oper->flags
79
84.0k
        & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
80
84.0k
      if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
81
6.32k
  num += num ? OPERAND_ACC1 : OPERAND_ACC0;
82
3.02M
      for (i = 0; i < d10v_reg_name_cnt (); i++)
83
3.02M
  {
84
3.02M
    if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
85
83.9k
      {
86
83.9k
        if (d10v_predefined_registers[i].pname)
87
6.05k
    (*info->fprintf_func) (info->stream, "%s",
88
6.05k
               d10v_predefined_registers[i].pname);
89
77.9k
        else
90
77.9k
    (*info->fprintf_func) (info->stream, "%s",
91
77.9k
               d10v_predefined_registers[i].name);
92
83.9k
        match = 1;
93
83.9k
        break;
94
83.9k
      }
95
3.02M
  }
96
84.0k
      if (match == 0)
97
101
  {
98
    /* This would only get executed if a register was not in the
99
       register table.  */
100
101
    if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
101
0
      (*info->fprintf_func) (info->stream, "a");
102
101
    else if (oper->flags & OPERAND_CONTROL)
103
0
      (*info->fprintf_func) (info->stream, "cr");
104
101
    else if (oper->flags & OPERAND_REG)
105
101
      (*info->fprintf_func) (info->stream, "r");
106
101
    (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
107
101
  }
108
84.0k
    }
109
10.1k
  else
110
10.1k
    {
111
      /* Addresses are right-shifted by 2.  */
112
10.1k
      if (oper->flags & OPERAND_ADDR)
113
1.40k
  {
114
1.40k
    long max;
115
1.40k
    int neg = 0;
116
117
1.40k
    max = (1 << (oper->bits - 1));
118
1.40k
    if (num & max)
119
447
      {
120
447
        num = -num & ((1 << oper->bits) - 1);
121
447
        neg = 1;
122
447
      }
123
1.40k
    num = num << 2;
124
1.40k
    if (info->flags & INSN_HAS_RELOC)
125
0
      (*info->print_address_func) (num & PC_MASK, info);
126
1.40k
    else
127
1.40k
      {
128
1.40k
        if (neg)
129
447
    (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
130
957
        else
131
957
    (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
132
1.40k
      }
133
1.40k
  }
134
8.72k
      else
135
8.72k
  {
136
8.72k
    if (oper->flags & OPERAND_SIGNED)
137
3.84k
      {
138
3.84k
        int max = (1 << (oper->bits - 1));
139
3.84k
        if (num & max)
140
1.87k
    {
141
1.87k
      num = -num & ((1 << oper->bits) - 1);
142
1.87k
      (*info->fprintf_func) (info->stream, "-");
143
1.87k
    }
144
3.84k
      }
145
8.72k
    (*info->fprintf_func) (info->stream, "0x%x", num);
146
8.72k
  }
147
10.1k
    }
148
94.2k
}
149
150
static void
151
dis_long (unsigned long insn,
152
    bfd_vma memaddr,
153
    struct disassemble_info *info)
154
7.99k
{
155
7.99k
  int i;
156
7.99k
  struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
157
7.99k
  struct d10v_operand *oper;
158
7.99k
  int need_paren = 0;
159
7.99k
  int match = 0;
160
161
1.17M
  while (op->name)
162
1.17M
    {
163
1.17M
      if ((op->format & LONG_OPCODE)
164
266k
    && ((op->mask & insn) == (unsigned long) op->opcode))
165
2.17k
  {
166
2.17k
    match = 1;
167
2.17k
    (*info->fprintf_func) (info->stream, "%s\t", op->name);
168
169
9.92k
    for (i = 0; op->operands[i]; i++)
170
7.74k
      {
171
7.74k
        oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
172
7.74k
        if (oper->flags == OPERAND_ATPAR)
173
1.43k
    need_paren = 1;
174
7.74k
        print_operand (oper, insn, op, memaddr, info);
175
7.74k
        if (op->operands[i + 1] && oper->bits
176
4.13k
      && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
177
4.13k
      && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
178
4.13k
    (*info->fprintf_func) (info->stream, ", ");
179
7.74k
      }
180
2.17k
    break;
181
2.17k
  }
182
1.17M
      op++;
183
1.17M
    }
184
185
7.99k
  if (!match)
186
5.82k
    (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
187
188
7.99k
  if (need_paren)
189
1.43k
    (*info->fprintf_func) (info->stream, ")");
190
7.99k
}
191
192
static void
193
dis_2_short (unsigned long insn,
194
       bfd_vma memaddr,
195
       struct disassemble_info *info,
196
       int order)
197
29.0k
{
198
29.0k
  int i, j;
199
29.0k
  unsigned int ins[2];
200
29.0k
  struct d10v_opcode *op;
201
29.0k
  int match, num_match = 0;
202
29.0k
  struct d10v_operand *oper;
203
29.0k
  int need_paren = 0;
204
205
29.0k
  ins[0] = (insn & 0x3FFFFFFF) >> 15;
206
29.0k
  ins[1] = insn & 0x00007FFF;
207
208
87.0k
  for (j = 0; j < 2; j++)
209
58.0k
    {
210
58.0k
      op = (struct d10v_opcode *) d10v_opcodes;
211
58.0k
      match = 0;
212
7.58M
      while (op->name)
213
7.57M
  {
214
7.57M
    if ((op->format & SHORT_OPCODE)
215
5.55M
        && ((((unsigned int) op->mask) & ins[j])
216
5.55M
      == (unsigned int) op->opcode))
217
42.0k
      {
218
42.0k
        (*info->fprintf_func) (info->stream, "%s\t", op->name);
219
135k
        for (i = 0; op->operands[i]; i++)
220
93.8k
    {
221
93.8k
      oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
222
93.8k
      if (oper->flags == OPERAND_ATPAR)
223
0
        need_paren = 1;
224
93.8k
      print_operand (oper, ins[j], op, memaddr, info);
225
93.8k
      if (op->operands[i + 1] && oper->bits
226
47.8k
          && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
227
46.5k
          && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
228
45.9k
        (*info->fprintf_func) (info->stream, ", ");
229
93.8k
    }
230
42.0k
        match = 1;
231
42.0k
        num_match++;
232
42.0k
        break;
233
42.0k
      }
234
7.52M
    op++;
235
7.52M
  }
236
58.0k
      if (!match)
237
15.9k
  (*info->fprintf_func) (info->stream, "unknown");
238
239
58.0k
      switch (order)
240
58.0k
  {
241
7.50k
  case 0:
242
7.50k
    (*info->fprintf_func) (info->stream, "\t->\t");
243
7.50k
    order = -1;
244
7.50k
    break;
245
4.85k
  case 1:
246
4.85k
    (*info->fprintf_func) (info->stream, "\t<-\t");
247
4.85k
    order = -1;
248
4.85k
    break;
249
16.6k
  case 2:
250
16.6k
    (*info->fprintf_func) (info->stream, "\t||\t");
251
16.6k
    order = -1;
252
16.6k
    break;
253
29.0k
  default:
254
29.0k
    break;
255
58.0k
  }
256
58.0k
    }
257
258
29.0k
  if (num_match == 0)
259
3.04k
    (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
260
261
29.0k
  if (need_paren)
262
0
    (*info->fprintf_func) (info->stream, ")");
263
29.0k
}
264
265
int
266
print_insn_d10v (bfd_vma memaddr, struct disassemble_info *info)
267
37.0k
{
268
37.0k
  int status;
269
37.0k
  bfd_byte buffer[4];
270
37.0k
  unsigned long insn;
271
272
37.0k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
273
37.0k
  if (status != 0)
274
78
    {
275
78
      (*info->memory_error_func) (status, memaddr, info);
276
78
      return -1;
277
78
    }
278
37.0k
  insn = bfd_getb32 (buffer);
279
280
37.0k
  status = insn & FM11;
281
37.0k
  switch (status)
282
37.0k
    {
283
16.6k
    case 0:
284
16.6k
      dis_2_short (insn, memaddr, info, 2);
285
16.6k
      break;
286
7.50k
    case FM01:
287
7.50k
      dis_2_short (insn, memaddr, info, 0);
288
7.50k
      break;
289
4.85k
    case FM10:
290
4.85k
      dis_2_short (insn, memaddr, info, 1);
291
4.85k
      break;
292
7.99k
    case FM11:
293
7.99k
      dis_long (insn, memaddr, info);
294
7.99k
      break;
295
37.0k
    }
296
37.0k
  return 4;
297
37.0k
}