Coverage Report

Created: 2026-07-12 09:22

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.8k
{
40
84.8k
  const struct loongarch_opcode *it;
41
84.8k
  struct loongarch_ase *ase;
42
84.8k
  size_t i;
43
1.17M
  for (ase = loongarch_ASEs; ase->enabled; ase++)
44
1.12M
    {
45
1.12M
      if (!*ase->enabled || (ase->include && !*ase->include)
46
1.12M
    || (ase->exclude && *ase->exclude))
47
0
  continue;
48
49
1.12M
      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.12M
      it = ase->opc_htab[LARCH_INSN_OPC (insn)];
64
48.3M
      for (; it->name; it++)
65
47.2M
  if ((insn & it->mask) == it->match && it->mask
66
39.9k
      && !(it->include && !*it->include)
67
39.9k
      && !(it->exclude && *it->exclude))
68
39.9k
    {
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
39.9k
      if (LARCH_INSN_AMSWAP_W (insn)
74
109
    && (LARCH_GET_RD (insn) != LARCH_GET_RJ (insn))
75
44
    && (it->pinfo & INSN_DIS_ALIAS))
76
0
        continue;
77
39.9k
      return it;
78
39.9k
    }
79
1.12M
    }
80
44.8k
  return NULL;
81
84.8k
}
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
156k
{
150
156k
  static int need_comma = 0;
151
156k
  struct disassemble_info *info = context;
152
156k
  insn_t insn = *(insn_t *) info->private_data;
153
156k
  int32_t imm, u_imm;
154
156k
  enum disassembler_style style;
155
156k
  bool is_ud_2nd_arg = false;
156
157
156k
  if (LARCH_INSN_AMSWAP_W (insn)
158
435
      && (LARCH_GET_RD (insn) == LARCH_GET_RJ (insn))
159
259
      && (LARCH_GET_RK (insn) == 1)
160
3
      && loongarch_dis_show_aliases
161
3
      && need_comma)
162
2
    is_ud_2nd_arg = true;
163
164
156k
  if (esc1)
165
116k
    {
166
      /* The "ud ui5" does not nedd a comma.  */
167
116k
      if (need_comma && !is_ud_2nd_arg)
168
76.9k
  info->fprintf_styled_func (info->stream, dis_style_text, ", ");
169
116k
      need_comma = 1;
170
116k
      imm = loongarch_decode_imm (bit_field, insn, 1);
171
116k
      u_imm = loongarch_decode_imm (bit_field, insn, 0);
172
116k
    }
173
174
156k
  switch (esc1)
175
156k
    {
176
70.0k
    case 'r':
177
70.0k
      switch (esc2)
178
70.0k
  {
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
70.0k
    default:
186
70.0k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_r_disname[u_imm]);
187
70.0k
  }
188
70.0k
      break;
189
70.0k
    case 'f':
190
9.44k
      switch (esc2)
191
9.44k
  {
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
9.18k
  default:
196
9.18k
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_f_disname[u_imm]);
197
9.44k
  }
198
9.44k
      break;
199
9.44k
    case 'c':
200
927
      switch (esc2)
201
927
  {
202
274
  case 'r':
203
274
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_cr_disname[u_imm]);
204
274
    break;
205
653
  default:
206
653
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_c_disname[u_imm]);
207
927
  }
208
927
      break;
209
2.30k
    case 'v':
210
2.30k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_v_disname[u_imm]);
211
2.30k
      break;
212
4.45k
    case 'x':
213
4.45k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_x_disname[u_imm]);
214
4.45k
      break;
215
11.2k
    case 'u':
216
11.2k
      style = esc2 == 'o' ? dis_style_address_offset : dis_style_immediate;
217
11.2k
      info->fprintf_styled_func (info->stream, style, "0x%x", u_imm);
218
11.2k
      break;
219
18.4k
    case 's':
220
18.4k
      switch (esc2)
221
18.4k
  {
222
8.48k
  case 'b':
223
13.0k
  case 'o':
224
    /* Both represent address offsets.  */
225
13.0k
    style = dis_style_address_offset;
226
13.0k
    break;
227
5.40k
  default:
228
5.40k
    style = dis_style_immediate;
229
5.40k
    break;
230
18.4k
  }
231
18.4k
      info->fprintf_styled_func (info->stream, style, "%d", imm);
232
18.4k
      switch (esc2)
233
18.4k
  {
234
8.48k
  case 'b':
235
8.48k
    info->insn_type = dis_branch;
236
8.48k
    info->target += imm;
237
18.4k
  }
238
18.4k
      break;
239
39.9k
    case '\0':
240
39.9k
      need_comma = 0;
241
156k
    }
242
156k
  return 0;
243
156k
}
244
245
static void
246
disassemble_one (insn_t insn, struct disassemble_info *info)
247
84.8k
{
248
84.8k
  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.8k
  if (!opc)
296
44.8k
    {
297
44.8k
      info->insn_type = dis_noninsn;
298
44.8k
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive, ".word\t\t");
299
44.8k
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%08x", insn);
300
44.8k
      return;
301
44.8k
    }
302
303
39.9k
  info->insn_type = dis_nonbranch;
304
39.9k
  if (opc->format == NULL || opc->format[0] == '\0')
305
23
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
306
23
        "%s", opc->name);
307
39.9k
  else
308
39.9k
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
309
39.9k
        "%-12s", opc->name);
310
311
39.9k
  {
312
39.9k
    char *fake_args = xmalloc (strlen (opc->format) + 1);
313
39.9k
    const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2];
314
39.9k
    strcpy (fake_args, opc->format);
315
39.9k
    if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs))
316
39.9k
      info->fprintf_styled_func (info->stream, dis_style_text, "\t");
317
39.9k
    info->private_data = &insn;
318
39.9k
    loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info);
319
39.9k
    free (fake_args);
320
39.9k
  }
321
322
39.9k
  if (info->insn_type == dis_branch || info->insn_type == dis_condbranch)
323
8.48k
    {
324
8.48k
      info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# ");
325
8.48k
      info->print_address_func (info->target, info);
326
8.48k
    }
327
39.9k
}
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
102
    {
351
102
      info->memory_error_func (status, memaddr, info);
352
102
      return -1; /* loongarch_insn_length (0); */
353
102
    }
354
355
84.8k
  disassemble_one (insn, info);
356
357
84.8k
  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
}