Coverage Report

Created: 2026-03-10 08:46

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
2.67k
#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
118k
{
37
118k
  int num, shift;
38
39
118k
  if (oper->flags == OPERAND_ATMINUS)
40
161
    {
41
161
      (*info->fprintf_func) (info->stream, "@-");
42
161
      return;
43
161
    }
44
118k
  if (oper->flags == OPERAND_MINUS)
45
916
    {
46
916
      (*info->fprintf_func) (info->stream, "-");
47
916
      return;
48
916
    }
49
117k
  if (oper->flags == OPERAND_PLUS)
50
1.13k
    {
51
1.13k
      (*info->fprintf_func) (info->stream, "+");
52
1.13k
      return;
53
1.13k
    }
54
116k
  if (oper->flags == OPERAND_ATSIGN)
55
4.93k
    {
56
4.93k
      (*info->fprintf_func) (info->stream, "@");
57
4.93k
      return;
58
4.93k
    }
59
111k
  if (oper->flags == OPERAND_ATPAR)
60
1.86k
    {
61
1.86k
      (*info->fprintf_func) (info->stream, "@(");
62
1.86k
      return;
63
1.86k
    }
64
65
109k
  shift = oper->shift;
66
67
  /* The LONG_L format shifts registers over by 15.  */
68
109k
  if (op->format == LONG_L && (oper->flags & OPERAND_REG))
69
5.08k
    shift += 15;
70
71
109k
  num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
72
73
109k
  if (oper->flags & OPERAND_REG)
74
96.2k
    {
75
96.2k
      int i;
76
96.2k
      int match = 0;
77
78
96.2k
      num += (oper->flags
79
96.2k
        & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
80
96.2k
      if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
81
7.05k
  num += num ? OPERAND_ACC1 : OPERAND_ACC0;
82
3.49M
      for (i = 0; i < d10v_reg_name_cnt (); i++)
83
3.49M
  {
84
3.49M
    if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
85
96.0k
      {
86
96.0k
        if (d10v_predefined_registers[i].pname)
87
7.17k
    (*info->fprintf_func) (info->stream, "%s",
88
7.17k
               d10v_predefined_registers[i].pname);
89
88.9k
        else
90
88.9k
    (*info->fprintf_func) (info->stream, "%s",
91
88.9k
               d10v_predefined_registers[i].name);
92
96.0k
        match = 1;
93
96.0k
        break;
94
96.0k
      }
95
3.49M
  }
96
96.2k
      if (match == 0)
97
194
  {
98
    /* This would only get executed if a register was not in the
99
       register table.  */
100
194
    if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
101
0
      (*info->fprintf_func) (info->stream, "a");
102
194
    else if (oper->flags & OPERAND_CONTROL)
103
0
      (*info->fprintf_func) (info->stream, "cr");
104
194
    else if (oper->flags & OPERAND_REG)
105
194
      (*info->fprintf_func) (info->stream, "r");
106
194
    (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
107
194
  }
108
96.2k
    }
109
13.0k
  else
110
13.0k
    {
111
      /* Addresses are right-shifted by 2.  */
112
13.0k
      if (oper->flags & OPERAND_ADDR)
113
2.67k
  {
114
2.67k
    long max;
115
2.67k
    int neg = 0;
116
117
2.67k
    max = (1 << (oper->bits - 1));
118
2.67k
    if (num & max)
119
1.07k
      {
120
1.07k
        num = -num & ((1 << oper->bits) - 1);
121
1.07k
        neg = 1;
122
1.07k
      }
123
2.67k
    num = num << 2;
124
2.67k
    if (info->flags & INSN_HAS_RELOC)
125
0
      (*info->print_address_func) (num & PC_MASK, info);
126
2.67k
    else
127
2.67k
      {
128
2.67k
        if (neg)
129
1.07k
    (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
130
1.59k
        else
131
1.59k
    (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
132
2.67k
      }
133
2.67k
  }
134
10.4k
      else
135
10.4k
  {
136
10.4k
    if (oper->flags & OPERAND_SIGNED)
137
4.45k
      {
138
4.45k
        int max = (1 << (oper->bits - 1));
139
4.45k
        if (num & max)
140
2.55k
    {
141
2.55k
      num = -num & ((1 << oper->bits) - 1);
142
2.55k
      (*info->fprintf_func) (info->stream, "-");
143
2.55k
    }
144
4.45k
      }
145
10.4k
    (*info->fprintf_func) (info->stream, "0x%x", num);
146
10.4k
  }
147
13.0k
    }
148
109k
}
149
150
static void
151
dis_long (unsigned long insn,
152
    bfd_vma memaddr,
153
    struct disassemble_info *info)
154
10.8k
{
155
10.8k
  int i;
156
10.8k
  struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
157
10.8k
  struct d10v_operand *oper;
158
10.8k
  int need_paren = 0;
159
10.8k
  int match = 0;
160
161
1.62M
  while (op->name)
162
1.61M
    {
163
1.61M
      if ((op->format & LONG_OPCODE)
164
366k
    && ((op->mask & insn) == (unsigned long) op->opcode))
165
2.72k
  {
166
2.72k
    match = 1;
167
2.72k
    (*info->fprintf_func) (info->stream, "%s\t", op->name);
168
169
12.4k
    for (i = 0; op->operands[i]; i++)
170
9.75k
      {
171
9.75k
        oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
172
9.75k
        if (oper->flags == OPERAND_ATPAR)
173
1.86k
    need_paren = 1;
174
9.75k
        print_operand (oper, insn, op, memaddr, info);
175
9.75k
        if (op->operands[i + 1] && oper->bits
176
5.15k
      && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
177
5.15k
      && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
178
5.15k
    (*info->fprintf_func) (info->stream, ", ");
179
9.75k
      }
180
2.72k
    break;
181
2.72k
  }
182
1.61M
      op++;
183
1.61M
    }
184
185
10.8k
  if (!match)
186
8.12k
    (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
187
188
10.8k
  if (need_paren)
189
1.86k
    (*info->fprintf_func) (info->stream, ")");
190
10.8k
}
191
192
static void
193
dis_2_short (unsigned long insn,
194
       bfd_vma memaddr,
195
       struct disassemble_info *info,
196
       int order)
197
35.0k
{
198
35.0k
  int i, j;
199
35.0k
  unsigned int ins[2];
200
35.0k
  struct d10v_opcode *op;
201
35.0k
  int match, num_match = 0;
202
35.0k
  struct d10v_operand *oper;
203
35.0k
  int need_paren = 0;
204
205
35.0k
  ins[0] = (insn & 0x3FFFFFFF) >> 15;
206
35.0k
  ins[1] = insn & 0x00007FFF;
207
208
105k
  for (j = 0; j < 2; j++)
209
70.0k
    {
210
70.0k
      op = (struct d10v_opcode *) d10v_opcodes;
211
70.0k
      match = 0;
212
9.17M
      while (op->name)
213
9.15M
  {
214
9.15M
    if ((op->format & SHORT_OPCODE)
215
6.71M
        && ((((unsigned int) op->mask) & ins[j])
216
6.71M
      == (unsigned int) op->opcode))
217
49.2k
      {
218
49.2k
        (*info->fprintf_func) (info->stream, "%s\t", op->name);
219
157k
        for (i = 0; op->operands[i]; i++)
220
108k
    {
221
108k
      oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
222
108k
      if (oper->flags == OPERAND_ATPAR)
223
0
        need_paren = 1;
224
108k
      print_operand (oper, ins[j], op, memaddr, info);
225
108k
      if (op->operands[i + 1] && oper->bits
226
54.4k
          && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
227
53.3k
          && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
228
52.3k
        (*info->fprintf_func) (info->stream, ", ");
229
108k
    }
230
49.2k
        match = 1;
231
49.2k
        num_match++;
232
49.2k
        break;
233
49.2k
      }
234
9.10M
    op++;
235
9.10M
  }
236
70.0k
      if (!match)
237
20.8k
  (*info->fprintf_func) (info->stream, "unknown");
238
239
70.0k
      switch (order)
240
70.0k
  {
241
9.17k
  case 0:
242
9.17k
    (*info->fprintf_func) (info->stream, "\t->\t");
243
9.17k
    order = -1;
244
9.17k
    break;
245
6.45k
  case 1:
246
6.45k
    (*info->fprintf_func) (info->stream, "\t<-\t");
247
6.45k
    order = -1;
248
6.45k
    break;
249
19.4k
  case 2:
250
19.4k
    (*info->fprintf_func) (info->stream, "\t||\t");
251
19.4k
    order = -1;
252
19.4k
    break;
253
35.0k
  default:
254
35.0k
    break;
255
70.0k
  }
256
70.0k
    }
257
258
35.0k
  if (num_match == 0)
259
4.28k
    (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
260
261
35.0k
  if (need_paren)
262
0
    (*info->fprintf_func) (info->stream, ")");
263
35.0k
}
264
265
int
266
print_insn_d10v (bfd_vma memaddr, struct disassemble_info *info)
267
45.9k
{
268
45.9k
  int status;
269
45.9k
  bfd_byte buffer[4];
270
45.9k
  unsigned long insn;
271
272
45.9k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
273
45.9k
  if (status != 0)
274
81
    {
275
81
      (*info->memory_error_func) (status, memaddr, info);
276
81
      return -1;
277
81
    }
278
45.8k
  insn = bfd_getb32 (buffer);
279
280
45.8k
  status = insn & FM11;
281
45.8k
  switch (status)
282
45.8k
    {
283
19.4k
    case 0:
284
19.4k
      dis_2_short (insn, memaddr, info, 2);
285
19.4k
      break;
286
9.17k
    case FM01:
287
9.17k
      dis_2_short (insn, memaddr, info, 0);
288
9.17k
      break;
289
6.45k
    case FM10:
290
6.45k
      dis_2_short (insn, memaddr, info, 1);
291
6.45k
      break;
292
10.8k
    case FM11:
293
10.8k
      dis_long (insn, memaddr, info);
294
10.8k
      break;
295
45.8k
    }
296
45.8k
  return 4;
297
45.8k
}