Coverage Report

Created: 2023-08-28 06:31

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