Coverage Report

Created: 2026-11-21 09:00

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 bool loongarch_dis_show_annotate = false;
30
static const char *const *loongarch_r_disname = NULL;
31
static const char *const *loongarch_f_disname = NULL;
32
static const char *const *loongarch_fc_disname = NULL;
33
static const char *const *loongarch_c_disname = NULL;
34
static const char *const *loongarch_cr_disname = NULL;
35
static const char *const *loongarch_v_disname = NULL;
36
static const char *const *loongarch_x_disname = NULL;
37
38
static const struct loongarch_opcode *
39
get_loongarch_opcode_by_binfmt (insn_t insn)
40
97.8k
{
41
97.8k
  const struct loongarch_opcode *it;
42
97.8k
  struct loongarch_ase *ase;
43
97.8k
  size_t i;
44
1.36M
  for (ase = loongarch_ASEs; ase->enabled; ase++)
45
1.31M
    {
46
1.31M
      if (!*ase->enabled || (ase->include && !*ase->include)
47
1.31M
    || (ase->exclude && *ase->exclude))
48
0
  continue;
49
50
1.31M
      if (!ase->opc_htab_inited)
51
36
  {
52
3.52k
    for (it = ase->opcodes; it->mask; it++)
53
3.49k
      if (!ase->opc_htab[LARCH_INSN_OPC (it->match)]
54
44
    && it->macro == NULL
55
44
    && (!(it->pinfo & INSN_DIS_ALIAS)
56
8
        || loongarch_dis_show_aliases))
57
44
        ase->opc_htab[LARCH_INSN_OPC (it->match)] = it;
58
612
    for (i = 0; i < 16; i++)
59
576
      if (!ase->opc_htab[i])
60
532
        ase->opc_htab[i] = it;
61
36
    ase->opc_htab_inited = 1;
62
36
  }
63
64
1.31M
      it = ase->opc_htab[LARCH_INSN_OPC (insn)];
65
56.4M
      for (; it->name; it++)
66
55.1M
  if ((insn & it->mask) == it->match && it->mask
67
44.7k
      && !(it->include && !*it->include)
68
44.7k
      && !(it->exclude && *it->exclude))
69
44.7k
    {
70
      /* ud ui5 need rd==rj. We should continue searching
71
         for the next `it` if rd != rj. Furthermore, we need
72
         `it->pinfo` to ensure that only the `it` in loongarch
73
         alias_opcodes[] is skipped.  */
74
44.7k
      if (LARCH_INSN_AMSWAP_W (insn)
75
167
    && (LARCH_GET_RD (insn) != LARCH_GET_RJ (insn))
76
91
    && (it->pinfo & INSN_DIS_ALIAS))
77
21
        continue;
78
44.7k
      return it;
79
44.7k
    }
80
1.31M
    }
81
53.1k
  return NULL;
82
97.8k
}
83
84
static void
85
set_default_loongarch_dis_options (void)
86
2
{
87
2
  LARCH_opts.ase_ilp32 = 1;
88
2
  LARCH_opts.ase_lp64 = 1;
89
2
  LARCH_opts.ase_sf = 1;
90
2
  LARCH_opts.ase_df = 1;
91
2
  LARCH_opts.ase_lsx = 1;
92
2
  LARCH_opts.ase_lasx = 1;
93
2
  LARCH_opts.ase_lvz = 1;
94
2
  LARCH_opts.ase_lbt = 1;
95
96
2
  loongarch_r_disname = loongarch_r_alias;
97
2
  loongarch_f_disname = loongarch_f_alias;
98
2
  loongarch_fc_disname = loongarch_fc_normal_name;
99
2
  loongarch_c_disname = loongarch_c_normal_name;
100
2
  loongarch_cr_disname = loongarch_cr_normal_name;
101
2
  loongarch_v_disname = loongarch_v_normal_name;
102
2
  loongarch_x_disname = loongarch_x_normal_name;
103
2
}
104
105
static int
106
parse_loongarch_dis_option (const char *option)
107
0
{
108
0
  if (strcmp (option, "no-aliases") == 0)
109
0
    {
110
0
      loongarch_dis_show_aliases = false;
111
0
      return 0;
112
0
    }
113
114
0
  if (strcmp (option, "numeric") == 0)
115
0
    {
116
0
      loongarch_r_disname = loongarch_r_normal_name;
117
0
      loongarch_f_disname = loongarch_f_normal_name;
118
0
      return 0;
119
0
    }
120
121
0
  if (strcmp (option, "annotate") == 0)
122
0
    {
123
0
      loongarch_dis_show_annotate = true;
124
0
      return 0;
125
0
    }
126
127
0
  return -1;
128
0
}
129
130
static int
131
parse_loongarch_dis_options (const char *opts_in)
132
2
{
133
2
  set_default_loongarch_dis_options ();
134
135
2
  if (opts_in == NULL)
136
2
    return 0;
137
138
0
  char *opts, *opt, *opt_end;
139
0
  opts = xmalloc (strlen (opts_in) + 1);
140
0
  strcpy (opts, opts_in);
141
142
0
  for (opt = opt_end = opts; opt_end != NULL; opt = opt_end + 1)
143
0
    {
144
0
      if ((opt_end = strchr (opt, ',')) != NULL)
145
0
  *opt_end = 0;
146
0
      if (parse_loongarch_dis_option (opt) != 0)
147
0
  return -1;
148
0
    }
149
0
  free (opts);
150
0
  return 0;
151
0
}
152
153
static int32_t
154
dis_one_arg (char esc1, char esc2, const char *bit_field,
155
       const char *arg ATTRIBUTE_UNUSED, void *context)
156
176k
{
157
176k
  static int need_comma = 0;
158
176k
  struct disassemble_info *info = context;
159
176k
  insn_t insn = *(insn_t *) info->private_data;
160
176k
  int32_t imm, u_imm;
161
176k
  enum disassembler_style style;
162
176k
  bool is_ud_2nd_arg = false;
163
164
176k
  if (LARCH_INSN_AMSWAP_W (insn)
165
571
      && (LARCH_GET_RD (insn) == LARCH_GET_RJ (insn))
166
291
      && (LARCH_GET_RK (insn) == 1)
167
39
      && loongarch_dis_show_aliases
168
39
      && need_comma)
169
26
    is_ud_2nd_arg = true;
170
171
176k
  if (esc1)
172
131k
    {
173
      /* The "ud ui5" does not nedd a comma.  */
174
131k
      if (need_comma && !is_ud_2nd_arg)
175
86.9k
  info->fprintf_styled_func (info->stream, dis_style_text, ", ");
176
131k
      need_comma = 1;
177
131k
      imm = loongarch_decode_imm (bit_field, insn, 1);
178
131k
      u_imm = loongarch_decode_imm (bit_field, insn, 0);
179
131k
    }
180
181
176k
  switch (esc1)
182
176k
    {
183
79.0k
    case 'r':
184
79.0k
      switch (esc2)
185
79.0k
  {
186
26
    case 'u':
187
      /* The "ud ui5" only needs to print one parameter.  */
188
26
      if (is_ud_2nd_arg)
189
13
        break;
190
13
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%x", u_imm);
191
13
      break;
192
79.0k
    default:
193
79.0k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_r_disname[u_imm]);
194
79.0k
  }
195
79.0k
      break;
196
79.0k
    case 'f':
197
10.3k
      switch (esc2)
198
10.3k
  {
199
22
  case 'c':
200
22
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_fc_disname[u_imm]);
201
22
    break;
202
10.3k
  default:
203
10.3k
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_f_disname[u_imm]);
204
10.3k
  }
205
10.3k
      break;
206
10.3k
    case 'c':
207
1.24k
      switch (esc2)
208
1.24k
  {
209
479
  case 'r':
210
479
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_cr_disname[u_imm]);
211
479
    break;
212
769
  default:
213
769
    info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_c_disname[u_imm]);
214
1.24k
  }
215
1.24k
      break;
216
3.26k
    case 'v':
217
3.26k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_v_disname[u_imm]);
218
3.26k
      break;
219
4.58k
    case 'x':
220
4.58k
      info->fprintf_styled_func (info->stream, dis_style_register, "%s", loongarch_x_disname[u_imm]);
221
4.58k
      break;
222
13.8k
    case 'u':
223
13.8k
      style = esc2 == 'o' ? dis_style_address_offset : dis_style_immediate;
224
13.8k
      info->fprintf_styled_func (info->stream, style, "0x%x", u_imm);
225
13.8k
      break;
226
19.2k
    case 's':
227
19.2k
      switch (esc2)
228
19.2k
  {
229
7.04k
  case 'b':
230
12.9k
  case 'o':
231
    /* Both represent address offsets.  */
232
12.9k
    style = dis_style_address_offset;
233
12.9k
    break;
234
6.34k
  default:
235
6.34k
    style = dis_style_immediate;
236
6.34k
    break;
237
19.2k
  }
238
19.2k
      info->fprintf_styled_func (info->stream, style, "%d", imm);
239
19.2k
      switch (esc2)
240
19.2k
  {
241
7.04k
  case 'b':
242
7.04k
    info->insn_type = dis_branch;
243
7.04k
    info->target += imm;
244
19.2k
  }
245
19.2k
      break;
246
44.7k
    case '\0':
247
44.7k
      need_comma = 0;
248
176k
    }
249
176k
  return 0;
250
176k
}
251
252
static void
253
disassemble_annotate_one (insn_t insn, struct disassemble_info *info)
254
0
{
255
0
  if (LARCH_INSN_DBAR (insn))
256
0
    {
257
0
      if ((insn & 0x700) == 0x700)
258
0
  {
259
0
    info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# SA-RAR");
260
0
    return;
261
0
  }
262
263
      /* Currently, for reserved hint values such as 0xf, 0x1f and 0x7000, even
264
   though execution behavior matches hint=0 on most hardware, their exact
265
   semantics are undefined.
266
   We adopt a conservative approach and skip annotation for these values.  */
267
0
      if (!(insn & 0x7fe0))
268
0
  {
269
0
    unsigned int hint = LARCH_GET_RD (insn);
270
271
    /* If hint value is 0xf or 0x1f, its no specific semantics.  */
272
0
    if ((hint & 0xf) == 0xf)
273
0
      return;
274
275
    /* dbar imm[4:0] annotate layout:
276
       CRW|RW
277
       ||| ||_ bit 0
278
       ||| |__ bit 1
279
       |||____ bit 2
280
       ||_____ bit 3
281
       |______ bit 4
282
       Bit 0: false denotes W, true denotes null, W declare store after this
283
       dbar are constrained by the barrier.
284
       Bit 1: false denotes R, true denotes null, R declare load after this
285
       dbar are constrained by the barrier.
286
       Bit 2: false denotes W, true denotes null, W declare store before this
287
       dbar are constrained by the barrier.
288
       Bit 3: false denotes R, true denotes null, R declare load before this
289
       dbar are constrained by the barrier.
290
       Bit 4: false denotes null, true denotes C, C declare only cached
291
       load\store are constrained by the barrier.
292
       For exmaple, immediate value 0x10 is annotated as CRW|RW that only
293
       cached load and store operations before and after this dbar are
294
       constrained by the barrier.  */
295
0
    char annotate[7] = {' ', ' ', ' ', '|', ' ', ' ', '\0'};
296
0
    if ((hint & (1u << 0)) == 0)
297
0
      annotate[5] = 'W';
298
0
    if ((hint & (1u << 1)) == 0)
299
0
      annotate[4] = 'R';
300
0
    if ((hint & (1u << 2)) == 0)
301
0
      annotate[2] = 'W';
302
0
    if ((hint & (1u << 3)) == 0)
303
0
      annotate[1] = 'R';
304
0
    if ((hint & (1u << 4)) != 0)
305
0
      annotate[0] = 'C';
306
307
0
    info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# ");
308
0
    info->fprintf_styled_func (info->stream, dis_style_sub_mnemonic, "%s", annotate);
309
0
    return;
310
0
  }
311
0
    }
312
313
0
  return;
314
0
}
315
316
static void
317
disassemble_one (insn_t insn, struct disassemble_info *info)
318
97.8k
{
319
97.8k
  const struct loongarch_opcode *opc = get_loongarch_opcode_by_binfmt (insn);
320
321
#ifdef LOONGARCH_DEBUG
322
  char have_space[32] = { 0 };
323
  insn_t t;
324
  int i;
325
  const char *t_f = opc ? opc->format : NULL;
326
  if (t_f)
327
    while (*t_f)
328
      {
329
  while (('a' <= t_f[0] && t_f[0] <= 'z')
330
         || ('A' <= t_f[0] && t_f[0] <= 'Z')
331
         || t_f[0] == ',')
332
    t_f++;
333
  while (1)
334
    {
335
      i = strtol (t_f, &t_f, 10);
336
      have_space[i] = 1;
337
      t_f++; /* ':' */
338
      i += strtol (t_f, &t_f, 10);
339
      have_space[i] = 1;
340
      if (t_f[0] == '|')
341
        t_f++;
342
      else
343
        break;
344
    }
345
  if (t_f[0] == '<')
346
    t_f += 2; /* '<' '<' */
347
  strtol (t_f, &t_f, 10);
348
      }
349
350
  have_space[28] = 1;
351
  have_space[0] = 0;
352
  t = ~((insn_t) -1 >> 1);
353
  for (i = 31; 0 <= i; i--)
354
    {
355
      if (t & insn)
356
  info->fprintf_styled_func (info->stream, dis_style_text, "1");
357
      else
358
  info->fprintf_styled_func (info->stream, dis_style_text, "0");
359
      if (have_space[i])
360
  info->fprintf_styled_func (info->stream, dis_style_text, " ");
361
      t = t >> 1;
362
    }
363
  info->fprintf_styled_func (info->stream, dis_style_text, "\t");
364
#endif
365
366
97.8k
  if (!opc)
367
53.1k
    {
368
53.1k
      info->insn_type = dis_noninsn;
369
53.1k
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive, ".word\t\t");
370
53.1k
      info->fprintf_styled_func (info->stream, dis_style_immediate, "0x%08x", insn);
371
53.1k
      return;
372
53.1k
    }
373
374
44.7k
  info->insn_type = dis_nonbranch;
375
44.7k
  if (opc->format == NULL || opc->format[0] == '\0')
376
95
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
377
95
        "%s", opc->name);
378
44.6k
  else
379
44.6k
    info->fprintf_styled_func (info->stream, dis_style_mnemonic,
380
44.6k
        "%-12s", opc->name);
381
382
44.7k
  {
383
44.7k
    char *fake_args = xmalloc (strlen (opc->format) + 1);
384
44.7k
    const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2];
385
44.7k
    strcpy (fake_args, opc->format);
386
44.7k
    if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs))
387
44.6k
      info->fprintf_styled_func (info->stream, dis_style_text, "\t");
388
44.7k
    info->private_data = &insn;
389
44.7k
    loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info);
390
44.7k
    free (fake_args);
391
44.7k
  }
392
393
44.7k
  if (loongarch_dis_show_annotate)
394
0
    disassemble_annotate_one (insn, info);
395
396
44.7k
  if (info->insn_type == dis_branch || info->insn_type == dis_condbranch)
397
7.04k
    {
398
7.04k
      info->fprintf_styled_func (info->stream, dis_style_comment_start, "\t# ");
399
7.04k
      info->print_address_func (info->target, info);
400
7.04k
    }
401
44.7k
}
402
403
int
404
print_insn_loongarch (bfd_vma memaddr, struct disassemble_info *info)
405
97.9k
{
406
97.9k
  bfd_byte buffer[4];
407
97.9k
  insn_t insn;
408
97.9k
  int status;
409
410
97.9k
  static int not_init_yet = 1;
411
97.9k
  if (not_init_yet)
412
2
    {
413
2
      parse_loongarch_dis_options (info->disassembler_options);
414
2
      not_init_yet = 0;
415
2
    }
416
417
97.9k
  info->bytes_per_chunk = 4;
418
97.9k
  info->bytes_per_line = 4;
419
97.9k
  info->display_endian = BFD_ENDIAN_LITTLE;
420
97.9k
  info->insn_info_valid = 1;
421
97.9k
  info->target = memaddr;
422
423
97.9k
  if ((status = info->read_memory_func (memaddr, buffer, sizeof (buffer),
424
97.9k
          info)) != 0)
425
145
    {
426
145
      info->memory_error_func (status, memaddr, info);
427
145
      return -1; /* loongarch_insn_length (0); */
428
145
    }
429
430
97.8k
  insn = bfd_getl32 (buffer);
431
432
97.8k
  disassemble_one (insn, info);
433
434
97.8k
  return loongarch_insn_length (insn);
435
97.9k
}
436
437
void
438
print_loongarch_disassembler_options (FILE *stream)
439
0
{
440
0
  fprintf (stream, _("\n\
441
0
The following LoongArch disassembler options are supported for use\n\
442
0
with the -M switch (multiple options should be separated by commas):\n"));
443
444
0
  fprintf (stream, _("\n\
445
0
    no-aliases    Use canonical instruction forms.\n"));
446
0
  fprintf (stream, _("\n\
447
0
    numeric       Print numeric register names, rather than ABI names.\n"));
448
0
  fprintf (stream, _("\n\
449
0
    annotate      Print LoongArch instruction annotations.\n"));
450
0
  fprintf (stream, _("\
451
0
      dbar annotate layout:\n\
452
0
      'C': only cached load and store operations are constrained by the barrier;\n\
453
0
      '|' left-side 'RW': all load and store before the dbar are constrained by the barrier;\n\
454
0
      '|' right-side 'RW': all load and store after the dbar are constrained by the barrier.\n"));
455
  fprintf (stream, _("\n"));
456
0
}