Coverage Report

Created: 2025-07-08 11:15

/src/binutils-gdb/opcodes/d10v-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble D10V instructions.
2
   Copyright (C) 1996-2025 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
977
#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
53.9k
{
37
53.9k
  int num, shift;
38
39
53.9k
  if (oper->flags == OPERAND_ATMINUS)
40
99
    {
41
99
      (*info->fprintf_func) (info->stream, "@-");
42
99
      return;
43
99
    }
44
53.8k
  if (oper->flags == OPERAND_MINUS)
45
608
    {
46
608
      (*info->fprintf_func) (info->stream, "-");
47
608
      return;
48
608
    }
49
53.2k
  if (oper->flags == OPERAND_PLUS)
50
561
    {
51
561
      (*info->fprintf_func) (info->stream, "+");
52
561
      return;
53
561
    }
54
52.6k
  if (oper->flags == OPERAND_ATSIGN)
55
2.22k
    {
56
2.22k
      (*info->fprintf_func) (info->stream, "@");
57
2.22k
      return;
58
2.22k
    }
59
50.4k
  if (oper->flags == OPERAND_ATPAR)
60
901
    {
61
901
      (*info->fprintf_func) (info->stream, "@(");
62
901
      return;
63
901
    }
64
65
49.5k
  shift = oper->shift;
66
67
  /* The LONG_L format shifts registers over by 15.  */
68
49.5k
  if (op->format == LONG_L && (oper->flags & OPERAND_REG))
69
2.38k
    shift += 15;
70
71
49.5k
  num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
72
73
49.5k
  if (oper->flags & OPERAND_REG)
74
43.3k
    {
75
43.3k
      int i;
76
43.3k
      int match = 0;
77
78
43.3k
      num += (oper->flags
79
43.3k
        & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
80
43.3k
      if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
81
3.22k
  num += num ? OPERAND_ACC1 : OPERAND_ACC0;
82
1.59M
      for (i = 0; i < d10v_reg_name_cnt (); i++)
83
1.59M
  {
84
1.59M
    if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
85
43.2k
      {
86
43.2k
        if (d10v_predefined_registers[i].pname)
87
3.18k
    (*info->fprintf_func) (info->stream, "%s",
88
3.18k
               d10v_predefined_registers[i].pname);
89
40.0k
        else
90
40.0k
    (*info->fprintf_func) (info->stream, "%s",
91
40.0k
               d10v_predefined_registers[i].name);
92
43.2k
        match = 1;
93
43.2k
        break;
94
43.2k
      }
95
1.59M
  }
96
43.3k
      if (match == 0)
97
50
  {
98
    /* This would only get executed if a register was not in the
99
       register table.  */
100
50
    if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
101
0
      (*info->fprintf_func) (info->stream, "a");
102
50
    else if (oper->flags & OPERAND_CONTROL)
103
0
      (*info->fprintf_func) (info->stream, "cr");
104
50
    else if (oper->flags & OPERAND_REG)
105
50
      (*info->fprintf_func) (info->stream, "r");
106
50
    (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
107
50
  }
108
43.3k
    }
109
6.21k
  else
110
6.21k
    {
111
      /* Addresses are right-shifted by 2.  */
112
6.21k
      if (oper->flags & OPERAND_ADDR)
113
977
  {
114
977
    long max;
115
977
    int neg = 0;
116
117
977
    max = (1 << (oper->bits - 1));
118
977
    if (num & max)
119
335
      {
120
335
        num = -num & ((1 << oper->bits) - 1);
121
335
        neg = 1;
122
335
      }
123
977
    num = num << 2;
124
977
    if (info->flags & INSN_HAS_RELOC)
125
0
      (*info->print_address_func) (num & PC_MASK, info);
126
977
    else
127
977
      {
128
977
        if (neg)
129
335
    (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
130
642
        else
131
642
    (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
132
977
      }
133
977
  }
134
5.23k
      else
135
5.23k
  {
136
5.23k
    if (oper->flags & OPERAND_SIGNED)
137
1.91k
      {
138
1.91k
        int max = (1 << (oper->bits - 1));
139
1.91k
        if (num & max)
140
1.01k
    {
141
1.01k
      num = -num & ((1 << oper->bits) - 1);
142
1.01k
      (*info->fprintf_func) (info->stream, "-");
143
1.01k
    }
144
1.91k
      }
145
5.23k
    (*info->fprintf_func) (info->stream, "0x%x", num);
146
5.23k
  }
147
6.21k
    }
148
49.5k
}
149
150
static void
151
dis_long (unsigned long insn,
152
    bfd_vma memaddr,
153
    struct disassemble_info *info)
154
5.64k
{
155
5.64k
  int i;
156
5.64k
  struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
157
5.64k
  struct d10v_operand *oper;
158
5.64k
  int need_paren = 0;
159
5.64k
  int match = 0;
160
161
870k
  while (op->name)
162
866k
    {
163
866k
      if ((op->format & LONG_OPCODE)
164
866k
    && ((op->mask & insn) == (unsigned long) op->opcode))
165
1.31k
  {
166
1.31k
    match = 1;
167
1.31k
    (*info->fprintf_func) (info->stream, "%s\t", op->name);
168
169
6.00k
    for (i = 0; op->operands[i]; i++)
170
4.68k
      {
171
4.68k
        oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
172
4.68k
        if (oper->flags == OPERAND_ATPAR)
173
901
    need_paren = 1;
174
4.68k
        print_operand (oper, insn, op, memaddr, info);
175
4.68k
        if (op->operands[i + 1] && oper->bits
176
4.68k
      && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
177
4.68k
      && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
178
2.45k
    (*info->fprintf_func) (info->stream, ", ");
179
4.68k
      }
180
1.31k
    break;
181
1.31k
  }
182
864k
      op++;
183
864k
    }
184
185
5.64k
  if (!match)
186
4.33k
    (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
187
188
5.64k
  if (need_paren)
189
901
    (*info->fprintf_func) (info->stream, ")");
190
5.64k
}
191
192
static void
193
dis_2_short (unsigned long insn,
194
       bfd_vma memaddr,
195
       struct disassemble_info *info,
196
       int order)
197
15.7k
{
198
15.7k
  int i, j;
199
15.7k
  unsigned int ins[2];
200
15.7k
  struct d10v_opcode *op;
201
15.7k
  int match, num_match = 0;
202
15.7k
  struct d10v_operand *oper;
203
15.7k
  int need_paren = 0;
204
205
15.7k
  ins[0] = (insn & 0x3FFFFFFF) >> 15;
206
15.7k
  ins[1] = insn & 0x00007FFF;
207
208
47.2k
  for (j = 0; j < 2; j++)
209
31.4k
    {
210
31.4k
      op = (struct d10v_opcode *) d10v_opcodes;
211
31.4k
      match = 0;
212
4.08M
      while (op->name)
213
4.07M
  {
214
4.07M
    if ((op->format & SHORT_OPCODE)
215
4.07M
        && ((((unsigned int) op->mask) & ins[j])
216
2.98M
      == (unsigned int) op->opcode))
217
22.3k
      {
218
22.3k
        (*info->fprintf_func) (info->stream, "%s\t", op->name);
219
71.5k
        for (i = 0; op->operands[i]; i++)
220
49.2k
    {
221
49.2k
      oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
222
49.2k
      if (oper->flags == OPERAND_ATPAR)
223
0
        need_paren = 1;
224
49.2k
      print_operand (oper, ins[j], op, memaddr, info);
225
49.2k
      if (op->operands[i + 1] && oper->bits
226
49.2k
          && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
227
49.2k
          && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
228
23.6k
        (*info->fprintf_func) (info->stream, ", ");
229
49.2k
    }
230
22.3k
        match = 1;
231
22.3k
        num_match++;
232
22.3k
        break;
233
22.3k
      }
234
4.05M
    op++;
235
4.05M
  }
236
31.4k
      if (!match)
237
9.12k
  (*info->fprintf_func) (info->stream, "unknown");
238
239
31.4k
      switch (order)
240
31.4k
  {
241
3.66k
  case 0:
242
3.66k
    (*info->fprintf_func) (info->stream, "\t->\t");
243
3.66k
    order = -1;
244
3.66k
    break;
245
3.78k
  case 1:
246
3.78k
    (*info->fprintf_func) (info->stream, "\t<-\t");
247
3.78k
    order = -1;
248
3.78k
    break;
249
8.29k
  case 2:
250
8.29k
    (*info->fprintf_func) (info->stream, "\t||\t");
251
8.29k
    order = -1;
252
8.29k
    break;
253
15.7k
  default:
254
15.7k
    break;
255
31.4k
  }
256
31.4k
    }
257
258
15.7k
  if (num_match == 0)
259
1.53k
    (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
260
261
15.7k
  if (need_paren)
262
0
    (*info->fprintf_func) (info->stream, ")");
263
15.7k
}
264
265
int
266
print_insn_d10v (bfd_vma memaddr, struct disassemble_info *info)
267
21.4k
{
268
21.4k
  int status;
269
21.4k
  bfd_byte buffer[4];
270
21.4k
  unsigned long insn;
271
272
21.4k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
273
21.4k
  if (status != 0)
274
103
    {
275
103
      (*info->memory_error_func) (status, memaddr, info);
276
103
      return -1;
277
103
    }
278
21.3k
  insn = bfd_getb32 (buffer);
279
280
21.3k
  status = insn & FM11;
281
21.3k
  switch (status)
282
21.3k
    {
283
8.29k
    case 0:
284
8.29k
      dis_2_short (insn, memaddr, info, 2);
285
8.29k
      break;
286
3.66k
    case FM01:
287
3.66k
      dis_2_short (insn, memaddr, info, 0);
288
3.66k
      break;
289
3.78k
    case FM10:
290
3.78k
      dis_2_short (insn, memaddr, info, 1);
291
3.78k
      break;
292
5.64k
    case FM11:
293
5.64k
      dis_long (insn, memaddr, info);
294
5.64k
      break;
295
21.3k
    }
296
21.3k
  return 4;
297
21.3k
}