Coverage Report

Created: 2025-07-08 11:15

/src/binutils-gdb/opcodes/loongarch-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* LoongArch opcode support.
2
   Copyright (C) 2021-2025 Free Software Foundation, Inc.
3
   Contributed by Loongson Ltd.
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 program; see the file COPYING3.  If not,
19
   see <http://www.gnu.org/licenses/>.  */
20
21
#include "sysdep.h"
22
#include "disassemble.h"
23
#include "opintl.h"
24
#include "opcode/loongarch.h"
25
#include "libiberty.h"
26
#include <stdlib.h>
27
28
static bool loongarch_dis_show_aliases = true;
29
static const char *const *loongarch_r_disname = NULL;
30
static const char *const *loongarch_f_disname = NULL;
31
static const char *const *loongarch_fc_disname = NULL;
32
static const char *const *loongarch_c_disname = NULL;
33
static const char *const *loongarch_cr_disname = NULL;
34
static const char *const *loongarch_v_disname = NULL;
35
static const char *const *loongarch_x_disname = NULL;
36
37
static const struct loongarch_opcode *
38
get_loongarch_opcode_by_binfmt (insn_t insn)
39
38.6k
{
40
38.6k
  const struct loongarch_opcode *it;
41
38.6k
  struct loongarch_ase *ase;
42
38.6k
  size_t i;
43
546k
  for (ase = loongarch_ASEs; ase->enabled; ase++)
44
523k
    {
45
523k
      if (!*ase->enabled || (ase->include && !*ase->include)
46
523k
    || (ase->exclude && *ase->exclude))
47
0
  continue;
48
49
523k
      if (!ase->opc_htab_inited)
50
36
  {
51
3.52k
    for (it = ase->opcodes; it->mask; it++)
52
3.49k
      if (!ase->opc_htab[LARCH_INSN_OPC (it->match)]
53
3.49k
    && it->macro == NULL
54
3.49k
    && (!(it->pinfo & INSN_DIS_ALIAS)
55
42
        || loongarch_dis_show_aliases))
56
42
        ase->opc_htab[LARCH_INSN_OPC (it->match)] = it;
57
612
    for (i = 0; i < 16; i++)
58
576
      if (!ase->opc_htab[i])
59
534
        ase->opc_htab[i] = it;
60
36
    ase->opc_htab_inited = 1;
61
36
  }
62
63
523k
      it = ase->opc_htab[LARCH_INSN_OPC (insn)];
64
21.7M
      for (; it->name; it++)
65
21.2M
  if ((insn & it->mask) == it->match && it->mask
66
21.2M
      && !(it->include && !*it->include)
67
21.2M
      && !(it->exclude && *it->exclude))
68
15.2k
    return it;
69
523k
    }
70
23.3k
  return NULL;
71
38.6k
}
72
73
static void
74
set_default_loongarch_dis_options (void)
75
2
{
76
2
  LARCH_opts.ase_ilp32 = 1;
77
2
  LARCH_opts.ase_lp64 = 1;
78
2
  LARCH_opts.ase_sf = 1;
79
2
  LARCH_opts.ase_df = 1;
80
2
  LARCH_opts.ase_lsx = 1;
81
2
  LARCH_opts.ase_lasx = 1;
82
2
  LARCH_opts.ase_lvz = 1;
83
2
  LARCH_opts.ase_lbt = 1;
84
85
2
  loongarch_r_disname = loongarch_r_alias;
86
2
  loongarch_f_disname = loongarch_f_alias;
87
2
  loongarch_fc_disname = loongarch_fc_normal_name;
88
2
  loongarch_c_disname = loongarch_c_normal_name;
89
2
  loongarch_cr_disname = loongarch_cr_normal_name;
90
2
  loongarch_v_disname = loongarch_v_normal_name;
91
2
  loongarch_x_disname = loongarch_x_normal_name;
92
2
}
93
94
static int
95
parse_loongarch_dis_option (const char *option)
96
0
{
97
0
  if (strcmp (option, "no-aliases") == 0)
98
0
    {
99
0
      loongarch_dis_show_aliases = false;
100
0
      return 0;
101
0
    }
102
103
0
  if (strcmp (option, "numeric") == 0)
104
0
    {
105
0
      loongarch_r_disname = loongarch_r_normal_name;
106
0
      loongarch_f_disname = loongarch_f_normal_name;
107
0
      return 0;
108
0
    }
109
110
0
  return -1;
111
0
}
112
113
static int
114
parse_loongarch_dis_options (const char *opts_in)
115
2
{
116
2
  set_default_loongarch_dis_options ();
117
118
2
  if (opts_in == NULL)
119
2
    return 0;
120
121
0
  char *opts, *opt, *opt_end;
122
0
  opts = xmalloc (strlen (opts_in) + 1);
123
0
  strcpy (opts, opts_in);
124
125
0
  for (opt = opt_end = opts; opt_end != NULL; opt = opt_end + 1)
126
0
    {
127
0
      if ((opt_end = strchr (opt, ',')) != NULL)
128
0
  *opt_end = 0;
129
0
      if (parse_loongarch_dis_option (opt) != 0)
130
0
  return -1;
131
0
    }
132
0
  free (opts);
133
0
  return 0;
134
0
}
135
136
static int32_t
137
dis_one_arg (char esc1, char esc2, const char *bit_field,
138
       const char *arg ATTRIBUTE_UNUSED, void *context)
139
57.5k
{
140
57.5k
  static int need_comma = 0;
141
57.5k
  struct disassemble_info *info = context;
142
57.5k
  insn_t insn = *(insn_t *) info->private_data;
143
57.5k
  int32_t imm, u_imm;
144
57.5k
  enum disassembler_style style;
145
146
57.5k
  if (esc1)
147
42.2k
    {
148
42.2k
      if (need_comma)
149
27.0k
  info->fprintf_styled_func (info->stream, dis_style_text, ", ");
150
42.2k
      need_comma = 1;
151
42.2k
      imm = loongarch_decode_imm (bit_field, insn, 1);
152
42.2k
      u_imm = loongarch_decode_imm (bit_field, insn, 0);
153
42.2k
    }
154
155
57.5k
  switch (esc1)
156
57.5k
    {
157
24.3k
    case 'r':
158
24.3k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_r_disname[u_imm]);
159
24.3k
      break;
160
915
    case 'f':
161
915
      switch (esc2)
162
915
  {
163
50
  case 'c':
164
50
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_fc_disname[u_imm]);
165
50
    break;
166
865
  default:
167
865
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_f_disname[u_imm]);
168
915
  }
169
915
      break;
170
915
    case 'c':
171
544
      switch (esc2)
172
544
  {
173
135
  case 'r':
174
135
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_cr_disname[u_imm]);
175
135
    break;
176
409
  default:
177
409
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_c_disname[u_imm]);
178
544
  }
179
544
      break;
180
625
    case 'v':
181
625
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_v_disname[u_imm]);
182
625
      break;
183
1.50k
    case 'x':
184
1.50k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_x_disname[u_imm]);
185
1.50k
      break;
186
4.62k
    case 'u':
187
4.62k
      style = esc2 == 'o' ? dis_style_address_offset : dis_style_immediate;
188
4.62k
      info->fprintf_styled_func (info->stream, style, "0x%x", u_imm);
189
4.62k
      break;
190
9.67k
    case 's':
191
9.67k
      switch (esc2)
192
9.67k
  {
193
5.62k
  case 'b':
194
7.81k
  case 'o':
195
    /* Both represent address offsets.  */
196
7.81k
    style = dis_style_address_offset;
197
7.81k
    break;
198
1.86k
  default:
199
1.86k
    style = dis_style_immediate;
200
1.86k
    break;
201
9.67k
  }
202
9.67k
      info->fprintf_styled_func (info->stream, style, "%d", imm);
203
9.67k
      switch (esc2)
204
9.67k
  {
205
5.62k
  case 'b':
206
5.62k
    info->insn_type = dis_branch;
207
5.62k
    info->target += imm;
208
9.67k
  }
209
9.67k
      break;
210
15.2k
    case '\0':
211
15.2k
      need_comma = 0;
212
57.5k
    }
213
57.5k
  return 0;
214
57.5k
}
215
216
static void
217
disassemble_one (insn_t insn, struct disassemble_info *info)
218
38.6k
{
219
38.6k
  const struct loongarch_opcode *opc = get_loongarch_opcode_by_binfmt (insn);
220
221
#ifdef LOONGARCH_DEBUG
222
  char have_space[32] = { 0 };
223
  insn_t t;
224
  int i;
225
  const char *t_f = opc ? opc->format : NULL;
226
  if (t_f)
227
    while (*t_f)
228
      {
229
  while (('a' <= t_f[0] && t_f[0] <= 'z')
230
         || ('A' <= t_f[0] && t_f[0] <= 'Z')
231
         || t_f[0] == ',')
232
    t_f++;
233
  while (1)
234
    {
235
      i = strtol (t_f, &t_f, 10);
236
      have_space[i] = 1;
237
      t_f++; /* ':' */
238
      i += strtol (t_f, &t_f, 10);
239
      have_space[i] = 1;
240
      if (t_f[0] == '|')
241
        t_f++;
242
      else
243
        break;
244
    }
245
  if (t_f[0] == '<')
246
    t_f += 2; /* '<' '<' */
247
  strtol (t_f, &t_f, 10);
248
      }
249
250
  have_space[28] = 1;
251
  have_space[0] = 0;
252
  t = ~((insn_t) -1 >> 1);
253
  for (i = 31; 0 <= i; i--)
254
    {
255
      if (t & insn)
256
  info->fprintf_styled_func (info->stream, dis_style_text, "1");
257
      else
258
  info->fprintf_styled_func (info->stream, dis_style_text, "0");
259
      if (have_space[i])
260
  info->fprintf_styled_func (info->stream, dis_style_text, " ");
261
      t = t >> 1;
262
    }
263
  info->fprintf_styled_func (info->stream, dis_style_text, "\t");
264
#endif
265
266
38.6k
  if (!opc)
267
23.3k
    {
268
23.3k
      info->insn_type = dis_noninsn;
269
23.3k
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive, ".word\t\t");
270
23.3k
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%08x", insn);
271
23.3k
      return;
272
23.3k
    }
273
274
15.2k
  info->insn_type = dis_nonbranch;
275
15.2k
  if (opc->format == NULL || opc->format[0] == '\0')
276
44
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
277
44
        "%s", opc->name);
278
15.2k
  else
279
15.2k
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
280
15.2k
        "%-12s", opc->name);
281
282
15.2k
  {
283
15.2k
    char *fake_args = xmalloc (strlen (opc->format) + 1);
284
15.2k
    const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2];
285
15.2k
    strcpy (fake_args, opc->format);
286
15.2k
    if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs))
287
15.2k
      info->fprintf_styled_func (info->stream, dis_style_text, "\t");
288
15.2k
    info->private_data = &insn;
289
15.2k
    loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info);
290
15.2k
    free (fake_args);
291
15.2k
  }
292
293
15.2k
  if (info->insn_type == dis_branch || info->insn_type == dis_condbranch)
294
5.62k
    {
295
5.62k
      info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# ");
296
5.62k
      info->print_address_func (info->target, info);
297
5.62k
    }
298
15.2k
}
299
300
int
301
print_insn_loongarch (bfd_vma memaddr, struct disassemble_info *info)
302
38.7k
{
303
38.7k
  insn_t insn;
304
38.7k
  int status;
305
306
38.7k
  static int not_init_yet = 1;
307
38.7k
  if (not_init_yet)
308
2
    {
309
2
      parse_loongarch_dis_options (info->disassembler_options);
310
2
      not_init_yet = 0;
311
2
    }
312
313
38.7k
  info->bytes_per_chunk = 4;
314
38.7k
  info->bytes_per_line = 4;
315
38.7k
  info->display_endian = BFD_ENDIAN_LITTLE;
316
38.7k
  info->insn_info_valid = 1;
317
38.7k
  info->target = memaddr;
318
319
38.7k
  if ((status = info->read_memory_func (memaddr, (bfd_byte *) &insn,
320
38.7k
          sizeof (insn), info)) != 0)
321
100
    {
322
100
      info->memory_error_func (status, memaddr, info);
323
100
      return -1; /* loongarch_insn_length (0); */
324
100
    }
325
326
38.6k
  disassemble_one (insn, info);
327
328
38.6k
  return loongarch_insn_length (insn);
329
38.7k
}
330
331
void
332
print_loongarch_disassembler_options (FILE *stream)
333
0
{
334
0
  fprintf (stream, _("\n\
335
0
The following LoongArch disassembler options are supported for use\n\
336
0
with the -M switch (multiple options should be separated by commas):\n"));
337
338
0
  fprintf (stream, _("\n\
339
0
    no-aliases    Use canonical instruction forms.\n"));
340
0
  fprintf (stream, _("\n\
341
0
    numeric       Print numeric register names, rather than ABI names.\n"));
342
0
  fprintf (stream, _("\n"));
343
0
}