Coverage Report

Created: 2025-06-24 06:45

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