Coverage Report

Created: 2026-05-11 07:54

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