Coverage Report

Created: 2023-08-28 06:31

/src/binutils-gdb/opcodes/spu-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble SPU instructions
2
3
   Copyright (C) 2006-2023 Free Software Foundation, Inc.
4
5
   This file is part of the GNU opcodes library.
6
7
   This library is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3, or (at your option)
10
   any later version.
11
12
   It is distributed in the hope that it will be useful, but WITHOUT
13
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15
   License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this file; see the file COPYING.  If not, write to the
19
   Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include <stdio.h>
24
#include "disassemble.h"
25
#include "opcode/spu.h"
26
27
/* This file provides a disassembler function which uses
28
   the disassembler interface defined in dis-asm.h.   */
29
30
extern const struct spu_opcode spu_opcodes[];
31
extern const int spu_num_opcodes;
32
33
static const struct spu_opcode *spu_disassemble_table[(1<<11)];
34
35
static void
36
init_spu_disassemble (void)
37
2
{
38
2
  int i;
39
40
  /* If two instructions have the same opcode then we prefer the first
41
   * one.  In most cases it is just an alternate mnemonic. */
42
488
  for (i = 0; i < spu_num_opcodes; i++)
43
486
    {
44
486
      int o = spu_opcodes[i].opcode;
45
486
      if (o >= (1 << 11))
46
0
  abort ();
47
486
      if (spu_disassemble_table[o] == 0)
48
398
  spu_disassemble_table[o] = &spu_opcodes[i];
49
486
    }
50
2
}
51
52
/* Determine the instruction from the 10 least significant bits. */
53
static const struct spu_opcode *
54
get_index_for_opcode (unsigned int insn)
55
712k
{
56
712k
  const struct spu_opcode *op_index;
57
712k
  unsigned int opcode = insn >> (32-11);
58
59
  /* Init the table.  This assumes that element 0/opcode 0 (currently
60
   * NOP) is always used */
61
712k
  if (spu_disassemble_table[0] == 0)
62
2
    init_spu_disassemble ();
63
64
712k
  if ((op_index = spu_disassemble_table[opcode & 0x780]) != 0
65
712k
      && op_index->insn_type == RRR)
66
194k
    return op_index;
67
68
518k
  if ((op_index = spu_disassemble_table[opcode & 0x7f0]) != 0
69
518k
      && (op_index->insn_type == RI18 || op_index->insn_type == LBT))
70
15.2k
    return op_index;
71
72
502k
  if ((op_index = spu_disassemble_table[opcode & 0x7f8]) != 0
73
502k
      && op_index->insn_type == RI10)
74
99.5k
    return op_index;
75
76
403k
  if ((op_index = spu_disassemble_table[opcode & 0x7fc]) != 0
77
403k
      && (op_index->insn_type == RI16))
78
18.7k
    return op_index;
79
80
384k
  if ((op_index = spu_disassemble_table[opcode & 0x7fe]) != 0
81
384k
      && (op_index->insn_type == RI8))
82
1.87k
    return op_index;
83
84
382k
  if ((op_index = spu_disassemble_table[opcode & 0x7ff]) != 0)
85
146k
    return op_index;
86
87
235k
  return 0;
88
382k
}
89
90
/* Print a Spu instruction.  */
91
92
int
93
print_insn_spu (bfd_vma memaddr, struct disassemble_info *info)
94
712k
{
95
712k
  bfd_byte buffer[4];
96
712k
  int value;
97
712k
  int hex_value;
98
712k
  int status;
99
712k
  unsigned int insn;
100
712k
  const struct spu_opcode *op_index;
101
712k
  enum spu_insns tag;
102
103
712k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
104
712k
  if (status != 0)
105
237
    {
106
237
      (*info->memory_error_func) (status, memaddr, info);
107
237
      return -1;
108
237
    }
109
110
712k
  insn = bfd_getb32 (buffer);
111
112
712k
  op_index = get_index_for_opcode (insn);
113
114
712k
  if (op_index == 0)
115
235k
    {
116
235k
      (*info->fprintf_func) (info->stream, ".long 0x%x", insn);
117
235k
    }
118
476k
  else
119
476k
    {
120
476k
      int i;
121
476k
      int paren = 0;
122
476k
      tag = (enum spu_insns)(op_index - spu_opcodes);
123
476k
      (*info->fprintf_func) (info->stream, "%s", op_index->mnemonic);
124
476k
      if (tag == M_BI || tag == M_BISL || tag == M_IRET || tag == M_BISLED
125
476k
    || tag == M_BIHNZ || tag == M_BIHZ || tag == M_BINZ || tag == M_BIZ
126
476k
          || tag == M_SYNC || tag == M_HBR)
127
8.76k
  {
128
8.76k
    int fb = (insn >> (32-18)) & 0x7f;
129
8.76k
    if (fb & 0x40)
130
4.91k
      (*info->fprintf_func) (info->stream, tag == M_SYNC ? "c" : "p");
131
8.76k
    if (fb & 0x20)
132
3.26k
      (*info->fprintf_func) (info->stream, "d");
133
8.76k
    if (fb & 0x10)
134
4.91k
      (*info->fprintf_func) (info->stream, "e");
135
8.76k
  }
136
476k
      if (op_index->arg[0] != 0)
137
397k
  (*info->fprintf_func) (info->stream, "\t");
138
476k
      hex_value = 0;
139
1.82M
      for (i = 1;  i <= op_index->arg[0]; i++)
140
1.35M
  {
141
1.35M
    int arg = op_index->arg[i];
142
1.35M
    if (arg != A_P && !paren && i > 1)
143
926k
      (*info->fprintf_func) (info->stream, ",");
144
145
1.35M
    switch (arg)
146
1.35M
      {
147
382k
      case A_T:
148
382k
        (*info->fprintf_func) (info->stream, "$%d",
149
382k
             DECODE_INSN_RT (insn));
150
382k
        break;
151
360k
      case A_A:
152
360k
        (*info->fprintf_func) (info->stream, "$%d",
153
360k
             DECODE_INSN_RA (insn));
154
360k
        break;
155
244k
      case A_B:
156
244k
        (*info->fprintf_func) (info->stream, "$%d",
157
244k
             DECODE_INSN_RB (insn));
158
244k
        break;
159
194k
      case A_C:
160
194k
        (*info->fprintf_func) (info->stream, "$%d",
161
194k
             DECODE_INSN_RC (insn));
162
194k
        break;
163
484
      case A_S:
164
484
        (*info->fprintf_func) (info->stream, "$sp%d",
165
484
             DECODE_INSN_RA (insn));
166
484
        break;
167
1.21k
      case A_H:
168
1.21k
        (*info->fprintf_func) (info->stream, "$ch%d",
169
1.21k
             DECODE_INSN_RA (insn));
170
1.21k
        break;
171
14.1k
      case A_P:
172
14.1k
        paren++;
173
14.1k
        (*info->fprintf_func) (info->stream, "(");
174
14.1k
        break;
175
1.29k
      case A_U7A:
176
1.29k
        (*info->fprintf_func) (info->stream, "%d",
177
1.29k
             173 - DECODE_INSN_U8 (insn));
178
1.29k
        break;
179
582
      case A_U7B:
180
582
        (*info->fprintf_func) (info->stream, "%d",
181
582
             155 - DECODE_INSN_U8 (insn));
182
582
        break;
183
312
      case A_S3:
184
1.98k
      case A_S6:
185
2.99k
      case A_S7:
186
5.41k
      case A_S7N:
187
5.76k
      case A_U3:
188
6.23k
      case A_U5:
189
6.29k
      case A_U6:
190
6.90k
      case A_U7:
191
6.90k
        hex_value = DECODE_INSN_I7 (insn);
192
6.90k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
193
6.90k
        break;
194
11.3k
      case A_S11:
195
11.3k
        (*info->print_address_func) (memaddr + DECODE_INSN_I9a (insn) * 4,
196
11.3k
             info);
197
11.3k
        break;
198
249
      case A_S11I:
199
249
        (*info->print_address_func) (memaddr + DECODE_INSN_I9b (insn) * 4,
200
249
             info);
201
249
        break;
202
74.5k
      case A_S10:
203
85.9k
      case A_S10B:
204
85.9k
        hex_value = DECODE_INSN_I10 (insn);
205
85.9k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
206
85.9k
        break;
207
13.6k
      case A_S14:
208
13.6k
        hex_value = DECODE_INSN_I10 (insn) * 16;
209
13.6k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
210
13.6k
        break;
211
1.17k
      case A_S16:
212
1.17k
        hex_value = DECODE_INSN_I16 (insn);
213
1.17k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
214
1.17k
        break;
215
3.85k
      case A_X16:
216
3.85k
        hex_value = DECODE_INSN_U16 (insn);
217
3.85k
        (*info->fprintf_func) (info->stream, "%u", hex_value);
218
3.85k
        break;
219
12.9k
      case A_R18:
220
12.9k
        value = DECODE_INSN_I16 (insn) * 4;
221
12.9k
        if (value == 0)
222
995
    (*info->fprintf_func) (info->stream, "%d", value);
223
11.9k
        else
224
11.9k
    {
225
11.9k
      hex_value = memaddr + value;
226
11.9k
      (*info->print_address_func) (hex_value & 0x3ffff, info);
227
11.9k
    }
228
12.9k
        break;
229
12.0k
      case A_S18:
230
12.0k
        value = DECODE_INSN_U16 (insn) * 4;
231
12.0k
        if (value == 0)
232
1.61k
    (*info->fprintf_func) (info->stream, "%d", value);
233
10.4k
        else
234
10.4k
    (*info->print_address_func) (value, info);
235
12.0k
        break;
236
3.91k
      case A_U18:
237
3.91k
        value = DECODE_INSN_U18 (insn);
238
3.91k
        if (value == 0 || !(*info->symbol_at_address_func)(0, info))
239
3.91k
    {
240
3.91k
      hex_value = value;
241
3.91k
      (*info->fprintf_func) (info->stream, "%u", value);
242
3.91k
    }
243
0
        else
244
0
    (*info->print_address_func) (value, info);
245
3.91k
        break;
246
0
      case A_U14:
247
0
        hex_value = DECODE_INSN_U14 (insn);
248
0
        (*info->fprintf_func) (info->stream, "%u", hex_value);
249
0
        break;
250
1.35M
      }
251
1.35M
    if (arg != A_P && paren)
252
14.1k
      {
253
14.1k
        (*info->fprintf_func) (info->stream, ")");
254
14.1k
        paren--;
255
14.1k
      }
256
1.35M
  }
257
476k
      if (hex_value > 16)
258
71.1k
  (*info->fprintf_func) (info->stream, "\t# %x", hex_value);
259
476k
    }
260
712k
  return 4;
261
712k
}