Coverage Report

Created: 2023-08-28 06:23

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