Coverage Report

Created: 2026-07-25 10:20

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