Coverage Report

Created: 2026-07-25 10:20

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