Coverage Report

Created: 2025-07-08 11:15

/src/binutils-gdb/opcodes/spu-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble SPU instructions
2
3
   Copyright (C) 2006-2025 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
200k
{
56
200k
  const struct spu_opcode *op_index;
57
200k
  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
200k
  if (spu_disassemble_table[0] == 0)
62
2
    init_spu_disassemble ();
63
64
200k
  if ((op_index = spu_disassemble_table[opcode & 0x780]) != 0
65
200k
      && op_index->insn_type == RRR)
66
44.9k
    return op_index;
67
68
155k
  if ((op_index = spu_disassemble_table[opcode & 0x7f0]) != 0
69
155k
      && (op_index->insn_type == RI18 || op_index->insn_type == LBT))
70
6.40k
    return op_index;
71
72
149k
  if ((op_index = spu_disassemble_table[opcode & 0x7f8]) != 0
73
149k
      && op_index->insn_type == RI10)
74
17.0k
    return op_index;
75
76
132k
  if ((op_index = spu_disassemble_table[opcode & 0x7fc]) != 0
77
132k
      && (op_index->insn_type == RI16))
78
6.64k
    return op_index;
79
80
125k
  if ((op_index = spu_disassemble_table[opcode & 0x7fe]) != 0
81
125k
      && (op_index->insn_type == RI8))
82
881
    return op_index;
83
84
124k
  if ((op_index = spu_disassemble_table[opcode & 0x7ff]) != 0)
85
68.1k
    return op_index;
86
87
56.3k
  return 0;
88
124k
}
89
90
/* Print a Spu instruction.  */
91
92
int
93
print_insn_spu (bfd_vma memaddr, struct disassemble_info *info)
94
200k
{
95
200k
  bfd_byte buffer[4];
96
200k
  int value;
97
200k
  int hex_value;
98
200k
  int status;
99
200k
  unsigned int insn;
100
200k
  const struct spu_opcode *op_index;
101
200k
  enum spu_insns tag;
102
103
200k
  status = (*info->read_memory_func) (memaddr, buffer, 4, info);
104
200k
  if (status != 0)
105
180
    {
106
180
      (*info->memory_error_func) (status, memaddr, info);
107
180
      return -1;
108
180
    }
109
110
200k
  insn = bfd_getb32 (buffer);
111
112
200k
  op_index = get_index_for_opcode (insn);
113
114
200k
  if (op_index == 0)
115
56.3k
    {
116
56.3k
      (*info->fprintf_func) (info->stream, ".long 0x%x", insn);
117
56.3k
    }
118
144k
  else
119
144k
    {
120
144k
      int i;
121
144k
      int paren = 0;
122
144k
      tag = (enum spu_insns)(op_index - spu_opcodes);
123
144k
      (*info->fprintf_func) (info->stream, "%s", op_index->mnemonic);
124
144k
      if (tag == M_BI || tag == M_BISL || tag == M_IRET || tag == M_BISLED
125
144k
    || tag == M_BIHNZ || tag == M_BIHZ || tag == M_BINZ || tag == M_BIZ
126
144k
          || tag == M_SYNC || tag == M_HBR)
127
6.16k
  {
128
6.16k
    int fb = (insn >> (32-18)) & 0x7f;
129
6.16k
    if (fb & 0x40)
130
3.53k
      (*info->fprintf_func) (info->stream, tag == M_SYNC ? "c" : "p");
131
6.16k
    if (fb & 0x20)
132
1.18k
      (*info->fprintf_func) (info->stream, "d");
133
6.16k
    if (fb & 0x10)
134
4.33k
      (*info->fprintf_func) (info->stream, "e");
135
6.16k
  }
136
144k
      if (op_index->arg[0] != 0)
137
91.9k
  (*info->fprintf_func) (info->stream, "\t");
138
144k
      hex_value = 0;
139
445k
      for (i = 1;  i <= op_index->arg[0]; i++)
140
301k
  {
141
301k
    int arg = op_index->arg[i];
142
301k
    if (arg != A_P && !paren && i > 1)
143
206k
      (*info->fprintf_func) (info->stream, ",");
144
145
301k
    switch (arg)
146
301k
      {
147
86.1k
      case A_T:
148
86.1k
        (*info->fprintf_func) (info->stream, "$%d",
149
86.1k
             DECODE_INSN_RT (insn));
150
86.1k
        break;
151
78.1k
      case A_A:
152
78.1k
        (*info->fprintf_func) (info->stream, "$%d",
153
78.1k
             DECODE_INSN_RA (insn));
154
78.1k
        break;
155
51.6k
      case A_B:
156
51.6k
        (*info->fprintf_func) (info->stream, "$%d",
157
51.6k
             DECODE_INSN_RB (insn));
158
51.6k
        break;
159
44.9k
      case A_C:
160
44.9k
        (*info->fprintf_func) (info->stream, "$%d",
161
44.9k
             DECODE_INSN_RC (insn));
162
44.9k
        break;
163
162
      case A_S:
164
162
        (*info->fprintf_func) (info->stream, "$sp%d",
165
162
             DECODE_INSN_RA (insn));
166
162
        break;
167
383
      case A_H:
168
383
        (*info->fprintf_func) (info->stream, "$ch%d",
169
383
             DECODE_INSN_RA (insn));
170
383
        break;
171
1.48k
      case A_P:
172
1.48k
        paren++;
173
1.48k
        (*info->fprintf_func) (info->stream, "(");
174
1.48k
        break;
175
759
      case A_U7A:
176
759
        (*info->fprintf_func) (info->stream, "%d",
177
759
             173 - DECODE_INSN_U8 (insn));
178
759
        break;
179
122
      case A_U7B:
180
122
        (*info->fprintf_func) (info->stream, "%d",
181
122
             155 - DECODE_INSN_U8 (insn));
182
122
        break;
183
324
      case A_S3:
184
446
      case A_S6:
185
677
      case A_S7:
186
1.66k
      case A_S7N:
187
2.07k
      case A_U3:
188
2.27k
      case A_U5:
189
2.35k
      case A_U6:
190
3.07k
      case A_U7:
191
3.07k
        hex_value = DECODE_INSN_I7 (insn);
192
3.07k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
193
3.07k
        break;
194
4.04k
      case A_S11:
195
4.04k
        (*info->print_address_func) (memaddr + DECODE_INSN_I9a (insn) * 4,
196
4.04k
             info);
197
4.04k
        break;
198
172
      case A_S11I:
199
172
        (*info->print_address_func) (memaddr + DECODE_INSN_I9b (insn) * 4,
200
172
             info);
201
172
        break;
202
11.9k
      case A_S10:
203
16.0k
      case A_S10B:
204
16.0k
        hex_value = DECODE_INSN_I10 (insn);
205
16.0k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
206
16.0k
        break;
207
1.00k
      case A_S14:
208
1.00k
        hex_value = DECODE_INSN_I10 (insn) * 16;
209
1.00k
        (*info->fprintf_func) (info->stream, "%d", hex_value);
210
1.00k
        break;
211
260
      case A_S16:
212
260
        hex_value = DECODE_INSN_I16 (insn);
213
260
        (*info->fprintf_func) (info->stream, "%d", hex_value);
214
260
        break;
215
1.12k
      case A_X16:
216
1.12k
        hex_value = DECODE_INSN_U16 (insn);
217
1.12k
        (*info->fprintf_func) (info->stream, "%u", hex_value);
218
1.12k
        break;
219
3.84k
      case A_R18:
220
3.84k
        value = DECODE_INSN_I16 (insn) * 4;
221
3.84k
        if (value == 0)
222
343
    (*info->fprintf_func) (info->stream, "%d", value);
223
3.49k
        else
224
3.49k
    {
225
3.49k
      hex_value = memaddr + value;
226
3.49k
      (*info->print_address_func) (hex_value & 0x3ffff, info);
227
3.49k
    }
228
3.84k
        break;
229
5.47k
      case A_S18:
230
5.47k
        value = DECODE_INSN_U16 (insn) * 4;
231
5.47k
        if (value == 0)
232
456
    (*info->fprintf_func) (info->stream, "%d", value);
233
5.01k
        else
234
5.01k
    (*info->print_address_func) (value, info);
235
5.47k
        break;
236
2.35k
      case A_U18:
237
2.35k
        value = DECODE_INSN_U18 (insn);
238
2.35k
        if (value == 0 || !(*info->symbol_at_address_func)(0, info))
239
2.35k
    {
240
2.35k
      hex_value = value;
241
2.35k
      (*info->fprintf_func) (info->stream, "%u", value);
242
2.35k
    }
243
0
        else
244
0
    (*info->print_address_func) (value, info);
245
2.35k
        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
301k
      }
251
301k
    if (arg != A_P && paren)
252
1.48k
      {
253
1.48k
        (*info->fprintf_func) (info->stream, ")");
254
1.48k
        paren--;
255
1.48k
      }
256
301k
  }
257
144k
      if (hex_value > 16)
258
17.0k
  (*info->fprintf_func) (info->stream, "\t# %x", hex_value);
259
144k
    }
260
200k
  return 4;
261
200k
}