Coverage Report

Created: 2023-08-28 06:23

/src/binutils-gdb/opcodes/d30v-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble D30V instructions.
2
   Copyright (C) 1997-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/d30v.h"
24
#include "disassemble.h"
25
#include "opintl.h"
26
#include "libiberty.h"
27
28
0
#define PC_MASK 0xFFFFFFFF
29
30
/* Return 0 if lookup fails,
31
   1 if found and only one form,
32
   2 if found and there are short and long forms.  */
33
34
static int
35
lookup_opcode (struct d30v_insn *insn, long num, int is_long)
36
0
{
37
0
  int i = 0, op_index;
38
0
  struct d30v_format *f;
39
0
  struct d30v_opcode *op = (struct d30v_opcode *) d30v_opcode_table;
40
0
  int op1 = (num >> 25) & 0x7;
41
0
  int op2 = (num >> 20) & 0x1f;
42
0
  int mod = (num >> 18) & 0x3;
43
44
  /* Find the opcode.  */
45
0
  do
46
0
    {
47
0
      if ((op->op1 == op1) && (op->op2 == op2))
48
0
  break;
49
0
      op++;
50
0
    }
51
0
  while (op->name);
52
53
0
  if (!op || !op->name)
54
0
    return 0;
55
56
0
  while (op->op1 == op1 && op->op2 == op2)
57
0
    {
58
      /* Scan through all the formats for the opcode.  */
59
0
      op_index = op->format[i++];
60
0
      do
61
0
  {
62
0
    f = (struct d30v_format *) &d30v_format_table[op_index];
63
0
    while (f->form == op_index)
64
0
      {
65
0
        if ((!is_long || f->form >= LONG) && (f->modifier == mod))
66
0
    {
67
0
      insn->form = f;
68
0
      break;
69
0
    }
70
0
        f++;
71
0
      }
72
0
    if (insn->form)
73
0
      break;
74
0
  }
75
0
      while ((op_index = op->format[i++]) != 0);
76
0
      if (insn->form)
77
0
  break;
78
0
      op++;
79
0
      i = 0;
80
0
    }
81
0
  if (insn->form == NULL)
82
0
    return 0;
83
84
0
  insn->op = op;
85
0
  insn->ecc = (num >> 28) & 0x7;
86
0
  if (op->format[1])
87
0
    return 2;
88
0
  else
89
0
    return 1;
90
0
}
91
92
static int
93
extract_value (uint64_t num, const struct d30v_operand *oper, int is_long)
94
0
{
95
0
  unsigned int val;
96
0
  int shift = 12 - oper->position;
97
0
  unsigned int mask = (0xFFFFFFFF >> (32 - oper->bits));
98
99
0
  if (is_long)
100
0
    {
101
0
      if (oper->bits == 32)
102
  /* Piece together 32-bit constant.  */
103
0
  val = ((num & 0x3FFFF)
104
0
         | ((num & 0xFF00000) >> 2)
105
0
         | ((num & 0x3F00000000LL) >> 6));
106
0
      else
107
0
  val = (num >> (32 + shift)) & mask;
108
0
    }
109
0
  else
110
0
    val = (num >> shift) & mask;
111
112
0
  if (oper->flags & OPERAND_SHIFT)
113
0
    val <<= 3;
114
115
0
  return val;
116
0
}
117
118
static void
119
print_insn (struct disassemble_info *info,
120
      bfd_vma memaddr,
121
      uint64_t num,
122
      struct d30v_insn *insn,
123
      int is_long,
124
      int show_ext)
125
0
{
126
0
  unsigned int val, opnum;
127
0
  const struct d30v_operand *oper;
128
0
  int i, match, need_comma = 0, need_paren = 0, found_control = 0;
129
0
  unsigned int opind = 0;
130
131
0
  (*info->fprintf_func) (info->stream, "%s", insn->op->name);
132
133
  /* Check for CMP or CMPU.  */
134
0
  if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
135
0
    {
136
0
      opind++;
137
0
      val =
138
0
  extract_value (num,
139
0
           &d30v_operand_table[insn->form->operands[0]],
140
0
           is_long);
141
0
      (*info->fprintf_func) (info->stream, "%s", d30v_cc_names[val]);
142
0
    }
143
144
  /* Add in ".s" or ".l".  */
145
0
  if (show_ext == 2)
146
0
    {
147
0
      if (is_long)
148
0
  (*info->fprintf_func) (info->stream, ".l");
149
0
      else
150
0
  (*info->fprintf_func) (info->stream, ".s");
151
0
    }
152
153
0
  if (insn->ecc)
154
0
    (*info->fprintf_func) (info->stream, "/%s", d30v_ecc_names[insn->ecc]);
155
156
0
  (*info->fprintf_func) (info->stream, "\t");
157
158
0
  while (opind < ARRAY_SIZE (insn->form->operands)
159
0
   && (opnum = insn->form->operands[opind++]) != 0)
160
0
    {
161
0
      int bits;
162
163
0
      oper = &d30v_operand_table[opnum];
164
0
      bits = oper->bits;
165
0
      if (oper->flags & OPERAND_SHIFT)
166
0
  bits += 3;
167
168
0
      if (need_comma
169
0
    && oper->flags != OPERAND_PLUS
170
0
    && oper->flags != OPERAND_MINUS)
171
0
  {
172
0
    need_comma = 0;
173
0
    (*info->fprintf_func) (info->stream, ", ");
174
0
  }
175
176
0
      if (oper->flags == OPERAND_ATMINUS)
177
0
  {
178
0
    (*info->fprintf_func) (info->stream, "@-");
179
0
    continue;
180
0
  }
181
0
      if (oper->flags == OPERAND_MINUS)
182
0
  {
183
0
    (*info->fprintf_func) (info->stream, "-");
184
0
    continue;
185
0
  }
186
0
      if (oper->flags == OPERAND_PLUS)
187
0
  {
188
0
    (*info->fprintf_func) (info->stream, "+");
189
0
    continue;
190
0
  }
191
0
      if (oper->flags == OPERAND_ATSIGN)
192
0
  {
193
0
    (*info->fprintf_func) (info->stream, "@");
194
0
    continue;
195
0
  }
196
0
      if (oper->flags == OPERAND_ATPAR)
197
0
  {
198
0
    (*info->fprintf_func) (info->stream, "@(");
199
0
    need_paren = 1;
200
0
    continue;
201
0
  }
202
203
0
      if (oper->flags == OPERAND_SPECIAL)
204
0
  continue;
205
206
0
      val = extract_value (num, oper, is_long);
207
208
0
      if (oper->flags & OPERAND_REG)
209
0
  {
210
0
    match = 0;
211
0
    if (oper->flags & OPERAND_CONTROL)
212
0
      {
213
0
        const struct d30v_operand *oper3
214
0
    = &d30v_operand_table[insn->form->operands[2]];
215
0
        int id = extract_value (num, oper3, is_long);
216
217
0
        found_control = 1;
218
0
        switch (id)
219
0
    {
220
0
    case 0:
221
0
      val |= OPERAND_CONTROL;
222
0
      break;
223
0
    case 1:
224
0
    case 2:
225
0
      val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
226
0
      break;
227
0
    case 3:
228
0
      val |= OPERAND_FLAG;
229
0
      break;
230
0
    default:
231
      /* xgettext: c-format */
232
0
      opcodes_error_handler (_("illegal id (%d)"), id);
233
0
      abort ();
234
0
    }
235
0
      }
236
0
    else if (oper->flags & OPERAND_ACC)
237
0
      val |= OPERAND_ACC;
238
0
    else if (oper->flags & OPERAND_FLAG)
239
0
      val |= OPERAND_FLAG;
240
0
    for (i = 0; i < reg_name_cnt (); i++)
241
0
      {
242
0
        if (val == pre_defined_registers[i].value)
243
0
    {
244
0
      if (pre_defined_registers[i].pname)
245
0
        (*info->fprintf_func)
246
0
          (info->stream, "%s", pre_defined_registers[i].pname);
247
0
      else
248
0
        (*info->fprintf_func)
249
0
          (info->stream, "%s", pre_defined_registers[i].name);
250
0
      match = 1;
251
0
      break;
252
0
    }
253
0
      }
254
0
    if (match == 0)
255
0
      {
256
        /* This would only get executed if a register was not in
257
     the register table.  */
258
0
        (*info->fprintf_func)
259
0
    (info->stream, _("<unknown register %d>"), val & 0x3F);
260
0
      }
261
0
  }
262
      /* repeati has a relocation, but its first argument is a plain
263
   immediate.  OTOH instructions like djsri have a pc-relative
264
   delay target, but an absolute jump target.  Therefore, a test
265
   of insn->op->reloc_flag is not specific enough; we must test
266
   if the actual operand we are handling now is pc-relative.  */
267
0
      else if (oper->flags & OPERAND_PCREL)
268
0
  {
269
0
    int neg = 0;
270
271
    /* IMM6S3 is unsigned.  */
272
0
    if (oper->flags & OPERAND_SIGNED || bits == 32)
273
0
      {
274
0
        unsigned int sign = 1u << (bits - 1);
275
0
        if (val & sign)
276
0
    {
277
0
      val = -val & (sign + sign - 1);
278
0
      neg = 1;
279
0
    }
280
0
      }
281
0
    if (neg)
282
0
      {
283
0
        (*info->fprintf_func) (info->stream, "-%x\t(", val);
284
0
        (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
285
0
        (*info->fprintf_func) (info->stream, ")");
286
0
      }
287
0
    else
288
0
      {
289
0
        (*info->fprintf_func) (info->stream, "%x\t(", val);
290
0
        (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
291
0
        (*info->fprintf_func) (info->stream, ")");
292
0
      }
293
0
  }
294
0
      else if (insn->op->reloc_flag == RELOC_ABS)
295
0
  {
296
0
    (*info->print_address_func) (val, info);
297
0
  }
298
0
      else
299
0
  {
300
0
    if (oper->flags & OPERAND_SIGNED)
301
0
      {
302
0
        unsigned int sign = 1u << (bits - 1);
303
304
0
        if (val & sign)
305
0
    {
306
0
      val = -val & (sign + sign - 1);
307
0
      (*info->fprintf_func) (info->stream, "-");
308
0
    }
309
0
      }
310
0
    (*info->fprintf_func) (info->stream, "0x%x", val);
311
0
  }
312
      /* If there is another operand, then write a comma and space.  */
313
0
      if (opind < ARRAY_SIZE (insn->form->operands)
314
0
    && insn->form->operands[opind]
315
0
    && !(found_control && opind == 2))
316
0
  need_comma = 1;
317
0
    }
318
0
  if (need_paren)
319
0
    (*info->fprintf_func) (info->stream, ")");
320
0
}
321
322
int
323
print_insn_d30v (bfd_vma memaddr, struct disassemble_info *info)
324
0
{
325
0
  int status, result;
326
0
  bfd_byte buffer[12];
327
0
  uint32_t in1, in2;
328
0
  struct d30v_insn insn;
329
0
  uint64_t num;
330
331
0
  insn.form = NULL;
332
333
0
  info->bytes_per_line = 8;
334
0
  info->bytes_per_chunk = 4;
335
0
  info->display_endian = BFD_ENDIAN_BIG;
336
337
0
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
338
0
  if (status != 0)
339
0
    {
340
0
      (*info->memory_error_func) (status, memaddr, info);
341
0
      return -1;
342
0
    }
343
0
  in1 = bfd_getb32 (buffer);
344
345
0
  status = (*info->read_memory_func) (memaddr + 4, buffer, 4, info);
346
0
  if (status != 0)
347
0
    {
348
0
      info->bytes_per_line = 8;
349
0
      if (!(result = lookup_opcode (&insn, in1, 0)))
350
0
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in1);
351
0
      else
352
0
  print_insn (info, memaddr, (uint64_t) in1, &insn, 0, result);
353
0
      return 4;
354
0
    }
355
0
  in2 = bfd_getb32 (buffer);
356
357
0
  if (in1 & in2 & FM01)
358
0
    {
359
      /* LONG instruction.  */
360
0
      if (!(result = lookup_opcode (&insn, in1, 1)))
361
0
  {
362
0
    (*info->fprintf_func) (info->stream, ".long\t0x%x,0x%x", in1, in2);
363
0
    return 8;
364
0
  }
365
0
      num = (uint64_t) in1 << 32 | in2;
366
0
      print_insn (info, memaddr, num, &insn, 1, result);
367
0
    }
368
0
  else
369
0
    {
370
0
      num = in1;
371
0
      if (!(result = lookup_opcode (&insn, in1, 0)))
372
0
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in1);
373
0
      else
374
0
  print_insn (info, memaddr, num, &insn, 0, result);
375
376
0
      switch (((in1 >> 31) << 1) | (in2 >> 31))
377
0
  {
378
0
  case 0:
379
0
    (*info->fprintf_func) (info->stream, "\t||\t");
380
0
    break;
381
0
  case 1:
382
0
    (*info->fprintf_func) (info->stream, "\t->\t");
383
0
    break;
384
0
  case 2:
385
0
    (*info->fprintf_func) (info->stream, "\t<-\t");
386
0
  default:
387
0
    break;
388
0
  }
389
390
0
      insn.form = NULL;
391
0
      num = in2;
392
0
      if (!(result = lookup_opcode (&insn, in2, 0)))
393
0
  (*info->fprintf_func) (info->stream, ".long\t0x%x", in2);
394
0
      else
395
0
  print_insn (info, memaddr, num, &insn, 0, result);
396
0
    }
397
0
  return 8;
398
0
}