Coverage Report

Created: 2026-07-25 10:20

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
91.5k
{
40
91.5k
  const struct loongarch_opcode *it;
41
91.5k
  struct loongarch_ase *ase;
42
91.5k
  size_t i;
43
1.24M
  for (ase = loongarch_ASEs; ase->enabled; ase++)
44
1.19M
    {
45
1.19M
      if (!*ase->enabled || (ase->include && !*ase->include)
46
1.19M
    || (ase->exclude && *ase->exclude))
47
0
  continue;
48
49
1.19M
      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.19M
      it = ase->opc_htab[LARCH_INSN_OPC (insn)];
64
51.7M
      for (; it->name; it++)
65
50.6M
  if ((insn & it->mask) == it->match && it->mask
66
43.3k
      && !(it->include && !*it->include)
67
43.3k
      && !(it->exclude && *it->exclude))
68
43.3k
    {
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
43.3k
      if (LARCH_INSN_AMSWAP_W (insn)
74
92
    && (LARCH_GET_RD (insn) != LARCH_GET_RJ (insn))
75
44
    && (it->pinfo & INSN_DIS_ALIAS))
76
0
        continue;
77
43.3k
      return it;
78
43.3k
    }
79
1.19M
    }
80
48.2k
  return NULL;
81
91.5k
}
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
169k
{
150
169k
  static int need_comma = 0;
151
169k
  struct disassemble_info *info = context;
152
169k
  insn_t insn = *(insn_t *) info->private_data;
153
169k
  int32_t imm, u_imm;
154
169k
  enum disassembler_style style;
155
169k
  bool is_ud_2nd_arg = false;
156
157
169k
  if (LARCH_INSN_AMSWAP_W (insn)
158
367
      && (LARCH_GET_RD (insn) == LARCH_GET_RJ (insn))
159
191
      && (LARCH_GET_RK (insn) == 1)
160
3
      && loongarch_dis_show_aliases
161
3
      && need_comma)
162
2
    is_ud_2nd_arg = true;
163
164
169k
  if (esc1)
165
126k
    {
166
      /* The "ud ui5" does not nedd a comma.  */
167
126k
      if (need_comma && !is_ud_2nd_arg)
168
83.0k
  info->fprintf_styled_func (info->stream, dis_style_text, ", ");
169
126k
      need_comma = 1;
170
126k
      imm = loongarch_decode_imm (bit_field, insn, 1);
171
126k
      u_imm = loongarch_decode_imm (bit_field, insn, 0);
172
126k
    }
173
174
169k
  switch (esc1)
175
169k
    {
176
75.2k
    case 'r':
177
75.2k
      switch (esc2)
178
75.2k
  {
179
2
    case 'u':
180
      /* The "ud ui5" only needs to print one parameter.  */
181
2
      if (is_ud_2nd_arg)
182
1
        break;
183
1
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%x", u_imm);
184
1
      break;
185
75.2k
    default:
186
75.2k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_r_disname[u_imm]);
187
75.2k
  }
188
75.2k
      break;
189
75.2k
    case 'f':
190
8.02k
      switch (esc2)
191
8.02k
  {
192
257
  case 'c':
193
257
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_fc_disname[u_imm]);
194
257
    break;
195
7.77k
  default:
196
7.77k
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_f_disname[u_imm]);
197
8.02k
  }
198
8.02k
      break;
199
8.02k
    case 'c':
200
1.00k
      switch (esc2)
201
1.00k
  {
202
330
  case 'r':
203
330
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_cr_disname[u_imm]);
204
330
    break;
205
678
  default:
206
678
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_c_disname[u_imm]);
207
1.00k
  }
208
1.00k
      break;
209
2.69k
    case 'v':
210
2.69k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_v_disname[u_imm]);
211
2.69k
      break;
212
5.10k
    case 'x':
213
5.10k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_x_disname[u_imm]);
214
5.10k
      break;
215
12.8k
    case 'u':
216
12.8k
      style = esc2 == 'o' ? dis_style_address_offset : dis_style_immediate;
217
12.8k
      info->fprintf_styled_func (info->stream, style, "0x%x", u_imm);
218
12.8k
      break;
219
21.3k
    case 's':
220
21.3k
      switch (esc2)
221
21.3k
  {
222
9.39k
  case 'b':
223
14.8k
  case 'o':
224
    /* Both represent address offsets.  */
225
14.8k
    style = dis_style_address_offset;
226
14.8k
    break;
227
6.55k
  default:
228
6.55k
    style = dis_style_immediate;
229
6.55k
    break;
230
21.3k
  }
231
21.3k
      info->fprintf_styled_func (info->stream, style, "%d", imm);
232
21.3k
      switch (esc2)
233
21.3k
  {
234
9.39k
  case 'b':
235
9.39k
    info->insn_type = dis_branch;
236
9.39k
    info->target += imm;
237
21.3k
  }
238
21.3k
      break;
239
43.3k
    case '\0':
240
43.3k
      need_comma = 0;
241
169k
    }
242
169k
  return 0;
243
169k
}
244
245
static void
246
disassemble_one (insn_t insn, struct disassemble_info *info)
247
91.5k
{
248
91.5k
  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
91.5k
  if (!opc)
296
48.2k
    {
297
48.2k
      info->insn_type = dis_noninsn;
298
48.2k
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive, ".word\t\t");
299
48.2k
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%08x", insn);
300
48.2k
      return;
301
48.2k
    }
302
303
43.3k
  info->insn_type = dis_nonbranch;
304
43.3k
  if (opc->format == NULL || opc->format[0] == '\0')
305
56
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
306
56
        "%s", opc->name);
307
43.3k
  else
308
43.3k
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
309
43.3k
        "%-12s", opc->name);
310
311
43.3k
  {
312
43.3k
    char *fake_args = xmalloc (strlen (opc->format) + 1);
313
43.3k
    const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2];
314
43.3k
    strcpy (fake_args, opc->format);
315
43.3k
    if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs))
316
43.3k
      info->fprintf_styled_func (info->stream, dis_style_text, "\t");
317
43.3k
    info->private_data = &insn;
318
43.3k
    loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info);
319
43.3k
    free (fake_args);
320
43.3k
  }
321
322
43.3k
  if (info->insn_type == dis_branch || info->insn_type == dis_condbranch)
323
9.39k
    {
324
9.39k
      info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# ");
325
9.39k
      info->print_address_func (info->target, info);
326
9.39k
    }
327
43.3k
}
328
329
int
330
print_insn_loongarch (bfd_vma memaddr, struct disassemble_info *info)
331
91.7k
{
332
91.7k
  insn_t insn;
333
91.7k
  int status;
334
335
91.7k
  static int not_init_yet = 1;
336
91.7k
  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
91.7k
  info->bytes_per_chunk = 4;
343
91.7k
  info->bytes_per_line = 4;
344
91.7k
  info->display_endian = BFD_ENDIAN_LITTLE;
345
91.7k
  info->insn_info_valid = 1;
346
91.7k
  info->target = memaddr;
347
348
91.7k
  if ((status = info->read_memory_func (memaddr, (bfd_byte *) &insn,
349
91.7k
          sizeof (insn), info)) != 0)
350
129
    {
351
129
      info->memory_error_func (status, memaddr, info);
352
129
      return -1; /* loongarch_insn_length (0); */
353
129
    }
354
355
91.5k
  disassemble_one (insn, info);
356
357
91.5k
  return loongarch_insn_length (insn);
358
91.7k
}
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
}