Coverage Report

Created: 2023-06-29 07:13

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