Coverage Report

Created: 2023-08-28 06:30

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