Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/loongarch-dis.c
Line
Count
Source
1
/* LoongArch opcode support.
2
   Copyright (C) 2021-2026 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
84.9k
{
40
84.9k
  const struct loongarch_opcode *it;
41
84.9k
  struct loongarch_ase *ase;
42
84.9k
  size_t i;
43
1.20M
  for (ase = loongarch_ASEs; ase->enabled; ase++)
44
1.15M
    {
45
1.15M
      if (!*ase->enabled || (ase->include && !*ase->include)
46
1.15M
    || (ase->exclude && *ase->exclude))
47
0
  continue;
48
49
1.15M
      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
44
    && it->macro == NULL
54
44
    && (!(it->pinfo & INSN_DIS_ALIAS)
55
8
        || loongarch_dis_show_aliases))
56
44
        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
532
        ase->opc_htab[i] = it;
60
36
    ase->opc_htab_inited = 1;
61
36
  }
62
63
1.15M
      it = ase->opc_htab[LARCH_INSN_OPC (insn)];
64
49.1M
      for (; it->name; it++)
65
48.0M
  if ((insn & it->mask) == it->match && it->mask
66
36.5k
      && !(it->include && !*it->include)
67
36.5k
      && !(it->exclude && *it->exclude))
68
36.5k
    {
69
      /* ud ui5 need rd==rj. We should continue searching
70
         for the next `it` if rd != rj. Furthermore, we need
71
         `it->pinfo` to ensure that only the `it` in loongarch
72
         alias_opcodes[] is skipped.  */
73
36.5k
      if (LARCH_INSN_AMSWAP_W (insn)
74
88
    && (LARCH_GET_RD (insn) != LARCH_GET_RJ (insn))
75
43
    && (it->pinfo & INSN_DIS_ALIAS))
76
0
        continue;
77
36.5k
      return it;
78
36.5k
    }
79
1.15M
    }
80
48.3k
  return NULL;
81
84.9k
}
82
83
static void
84
set_default_loongarch_dis_options (void)
85
2
{
86
2
  LARCH_opts.ase_ilp32 = 1;
87
2
  LARCH_opts.ase_lp64 = 1;
88
2
  LARCH_opts.ase_sf = 1;
89
2
  LARCH_opts.ase_df = 1;
90
2
  LARCH_opts.ase_lsx = 1;
91
2
  LARCH_opts.ase_lasx = 1;
92
2
  LARCH_opts.ase_lvz = 1;
93
2
  LARCH_opts.ase_lbt = 1;
94
95
2
  loongarch_r_disname = loongarch_r_alias;
96
2
  loongarch_f_disname = loongarch_f_alias;
97
2
  loongarch_fc_disname = loongarch_fc_normal_name;
98
2
  loongarch_c_disname = loongarch_c_normal_name;
99
2
  loongarch_cr_disname = loongarch_cr_normal_name;
100
2
  loongarch_v_disname = loongarch_v_normal_name;
101
2
  loongarch_x_disname = loongarch_x_normal_name;
102
2
}
103
104
static int
105
parse_loongarch_dis_option (const char *option)
106
0
{
107
0
  if (strcmp (option, "no-aliases") == 0)
108
0
    {
109
0
      loongarch_dis_show_aliases = false;
110
0
      return 0;
111
0
    }
112
113
0
  if (strcmp (option, "numeric") == 0)
114
0
    {
115
0
      loongarch_r_disname = loongarch_r_normal_name;
116
0
      loongarch_f_disname = loongarch_f_normal_name;
117
0
      return 0;
118
0
    }
119
120
0
  return -1;
121
0
}
122
123
static int
124
parse_loongarch_dis_options (const char *opts_in)
125
2
{
126
2
  set_default_loongarch_dis_options ();
127
128
2
  if (opts_in == NULL)
129
2
    return 0;
130
131
0
  char *opts, *opt, *opt_end;
132
0
  opts = xmalloc (strlen (opts_in) + 1);
133
0
  strcpy (opts, opts_in);
134
135
0
  for (opt = opt_end = opts; opt_end != NULL; opt = opt_end + 1)
136
0
    {
137
0
      if ((opt_end = strchr (opt, ',')) != NULL)
138
0
  *opt_end = 0;
139
0
      if (parse_loongarch_dis_option (opt) != 0)
140
0
  return -1;
141
0
    }
142
0
  free (opts);
143
0
  return 0;
144
0
}
145
146
static int32_t
147
dis_one_arg (char esc1, char esc2, const char *bit_field,
148
       const char *arg ATTRIBUTE_UNUSED, void *context)
149
141k
{
150
141k
  static int need_comma = 0;
151
141k
  struct disassemble_info *info = context;
152
141k
  insn_t insn = *(insn_t *) info->private_data;
153
141k
  int32_t imm, u_imm;
154
141k
  enum disassembler_style style;
155
141k
  bool is_ud_2nd_arg = false;
156
157
141k
  if (LARCH_INSN_AMSWAP_W (insn)
158
352
      && (LARCH_GET_RD (insn) == LARCH_GET_RJ (insn))
159
180
      && (LARCH_GET_RK (insn) == 1)
160
0
      && loongarch_dis_show_aliases
161
0
      && need_comma)
162
0
    is_ud_2nd_arg = true;
163
164
141k
  if (esc1)
165
105k
    {
166
      /* The "ud ui5" does not nedd a comma.  */
167
105k
      if (need_comma && !is_ud_2nd_arg)
168
68.6k
  info->fprintf_styled_func (info->stream, dis_style_text, ", ");
169
105k
      need_comma = 1;
170
105k
      imm = loongarch_decode_imm (bit_field, insn, 1);
171
105k
      u_imm = loongarch_decode_imm (bit_field, insn, 0);
172
105k
    }
173
174
141k
  switch (esc1)
175
141k
    {
176
63.8k
    case 'r':
177
63.8k
      switch (esc2)
178
63.8k
  {
179
0
    case 'u':
180
      /* The "ud ui5" only needs to print one parameter.  */
181
0
      if (is_ud_2nd_arg)
182
0
        break;
183
0
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%x", u_imm);
184
0
      break;
185
63.8k
    default:
186
63.8k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_r_disname[u_imm]);
187
63.8k
  }
188
63.8k
      break;
189
63.8k
    case 'f':
190
8.47k
      switch (esc2)
191
8.47k
  {
192
21
  case 'c':
193
21
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_fc_disname[u_imm]);
194
21
    break;
195
8.45k
  default:
196
8.45k
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_f_disname[u_imm]);
197
8.47k
  }
198
8.47k
      break;
199
8.47k
    case 'c':
200
1.08k
      switch (esc2)
201
1.08k
  {
202
331
  case 'r':
203
331
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_cr_disname[u_imm]);
204
331
    break;
205
749
  default:
206
749
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_c_disname[u_imm]);
207
1.08k
  }
208
1.08k
      break;
209
2.40k
    case 'v':
210
2.40k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_v_disname[u_imm]);
211
2.40k
      break;
212
2.64k
    case 'x':
213
2.64k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_x_disname[u_imm]);
214
2.64k
      break;
215
9.78k
    case 'u':
216
9.78k
      style = esc2 == 'o' ? dis_style_address_offset : dis_style_immediate;
217
9.78k
      info->fprintf_styled_func (info->stream, style, "0x%x", u_imm);
218
9.78k
      break;
219
16.8k
    case 's':
220
16.8k
      switch (esc2)
221
16.8k
  {
222
7.76k
  case 'b':
223
11.4k
  case 'o':
224
    /* Both represent address offsets.  */
225
11.4k
    style = dis_style_address_offset;
226
11.4k
    break;
227
5.44k
  default:
228
5.44k
    style = dis_style_immediate;
229
5.44k
    break;
230
16.8k
  }
231
16.8k
      info->fprintf_styled_func (info->stream, style, "%d", imm);
232
16.8k
      switch (esc2)
233
16.8k
  {
234
7.76k
  case 'b':
235
7.76k
    info->insn_type = dis_branch;
236
7.76k
    info->target += imm;
237
16.8k
  }
238
16.8k
      break;
239
36.5k
    case '\0':
240
36.5k
      need_comma = 0;
241
141k
    }
242
141k
  return 0;
243
141k
}
244
245
static void
246
disassemble_one (insn_t insn, struct disassemble_info *info)
247
84.9k
{
248
84.9k
  const struct loongarch_opcode *opc = get_loongarch_opcode_by_binfmt (insn);
249
250
#ifdef LOONGARCH_DEBUG
251
  char have_space[32] = { 0 };
252
  insn_t t;
253
  int i;
254
  const char *t_f = opc ? opc->format : NULL;
255
  if (t_f)
256
    while (*t_f)
257
      {
258
  while (('a' <= t_f[0] && t_f[0] <= 'z')
259
         || ('A' <= t_f[0] && t_f[0] <= 'Z')
260
         || t_f[0] == ',')
261
    t_f++;
262
  while (1)
263
    {
264
      i = strtol (t_f, &t_f, 10);
265
      have_space[i] = 1;
266
      t_f++; /* ':' */
267
      i += strtol (t_f, &t_f, 10);
268
      have_space[i] = 1;
269
      if (t_f[0] == '|')
270
        t_f++;
271
      else
272
        break;
273
    }
274
  if (t_f[0] == '<')
275
    t_f += 2; /* '<' '<' */
276
  strtol (t_f, &t_f, 10);
277
      }
278
279
  have_space[28] = 1;
280
  have_space[0] = 0;
281
  t = ~((insn_t) -1 >> 1);
282
  for (i = 31; 0 <= i; i--)
283
    {
284
      if (t & insn)
285
  info->fprintf_styled_func (info->stream, dis_style_text, "1");
286
      else
287
  info->fprintf_styled_func (info->stream, dis_style_text, "0");
288
      if (have_space[i])
289
  info->fprintf_styled_func (info->stream, dis_style_text, " ");
290
      t = t >> 1;
291
    }
292
  info->fprintf_styled_func (info->stream, dis_style_text, "\t");
293
#endif
294
295
84.9k
  if (!opc)
296
48.3k
    {
297
48.3k
      info->insn_type = dis_noninsn;
298
48.3k
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive, ".word\t\t");
299
48.3k
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%08x", insn);
300
48.3k
      return;
301
48.3k
    }
302
303
36.5k
  info->insn_type = dis_nonbranch;
304
36.5k
  if (opc->format == NULL || opc->format[0] == '\0')
305
12
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
306
12
        "%s", opc->name);
307
36.5k
  else
308
36.5k
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
309
36.5k
        "%-12s", opc->name);
310
311
36.5k
  {
312
36.5k
    char *fake_args = xmalloc (strlen (opc->format) + 1);
313
36.5k
    const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2];
314
36.5k
    strcpy (fake_args, opc->format);
315
36.5k
    if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs))
316
36.5k
      info->fprintf_styled_func (info->stream, dis_style_text, "\t");
317
36.5k
    info->private_data = &insn;
318
36.5k
    loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info);
319
36.5k
    free (fake_args);
320
36.5k
  }
321
322
36.5k
  if (info->insn_type == dis_branch || info->insn_type == dis_condbranch)
323
7.76k
    {
324
7.76k
      info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# ");
325
7.76k
      info->print_address_func (info->target, info);
326
7.76k
    }
327
36.5k
}
328
329
int
330
print_insn_loongarch (bfd_vma memaddr, struct disassemble_info *info)
331
84.9k
{
332
84.9k
  insn_t insn;
333
84.9k
  int status;
334
335
84.9k
  static int not_init_yet = 1;
336
84.9k
  if (not_init_yet)
337
2
    {
338
2
      parse_loongarch_dis_options (info->disassembler_options);
339
2
      not_init_yet = 0;
340
2
    }
341
342
84.9k
  info->bytes_per_chunk = 4;
343
84.9k
  info->bytes_per_line = 4;
344
84.9k
  info->display_endian = BFD_ENDIAN_LITTLE;
345
84.9k
  info->insn_info_valid = 1;
346
84.9k
  info->target = memaddr;
347
348
84.9k
  if ((status = info->read_memory_func (memaddr, (bfd_byte *) &insn,
349
84.9k
          sizeof (insn), info)) != 0)
350
81
    {
351
81
      info->memory_error_func (status, memaddr, info);
352
81
      return -1; /* loongarch_insn_length (0); */
353
81
    }
354
355
84.9k
  disassemble_one (insn, info);
356
357
84.9k
  return loongarch_insn_length (insn);
358
84.9k
}
359
360
void
361
print_loongarch_disassembler_options (FILE *stream)
362
0
{
363
0
  fprintf (stream, _("\n\
364
0
The following LoongArch disassembler options are supported for use\n\
365
0
with the -M switch (multiple options should be separated by commas):\n"));
366
367
0
  fprintf (stream, _("\n\
368
0
    no-aliases    Use canonical instruction forms.\n"));
369
0
  fprintf (stream, _("\n\
370
0
    numeric       Print numeric register names, rather than ABI names.\n"));
371
  fprintf (stream, _("\n"));
372
0
}