Coverage Report

Created: 2026-10-02 09:53

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