Coverage Report

Created: 2026-03-10 08:46

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