Coverage Report

Created: 2026-07-12 09:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/aarch64-dis.c
Line
Count
Source
1
/* aarch64-dis.c -- AArch64 disassembler.
2
   Copyright (C) 2009-2026 Free Software Foundation, Inc.
3
   Contributed by ARM 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 <stdint.h>
23
#include "disassemble.h"
24
#include "libiberty.h"
25
#include "opintl.h"
26
#include "aarch64-dis.h"
27
#include "elf-bfd.h"
28
#include "safe-ctype.h"
29
#include "obstack.h"
30
31
#define obstack_chunk_alloc xmalloc
32
#define obstack_chunk_free free
33
34
14.8M
#define INSNLEN 4
35
36
/* This character is used to encode style information within the output
37
   buffers.  See get_style_text and print_operands for more details.  */
38
220M
#define STYLE_MARKER_CHAR '\002'
39
40
/* Cached mapping symbol state.  */
41
enum map_type
42
{
43
  MAP_INSN,
44
  MAP_DATA
45
};
46
47
static aarch64_feature_set arch_variant; /* See select_aarch64_variant.  */
48
static enum map_type last_type;
49
static int last_mapping_sym = -1;
50
static bfd_vma last_stop_offset = 0;
51
static bfd_vma last_mapping_addr = 0;
52
static bool annotate_undefined_insns = false;
53
54
/* Other options */
55
static int no_aliases = 0;  /* If set disassemble as most general inst.  */
56
static int no_notes = 0;  /* If set do not print disassemble notes in the
57
           output as comments.  */
58
59
/* Currently active instruction sequence.  */
60
static aarch64_instr_sequence insn_sequence;
61
62
static void
63
set_default_aarch64_dis_options (struct disassemble_info *info ATTRIBUTE_UNUSED)
64
0
{
65
0
}
66
67
static void
68
parse_aarch64_dis_option (const char *option, unsigned int len ATTRIBUTE_UNUSED)
69
0
{
70
  /* Try to match options that are simple flags */
71
0
  if (startswith (option, "no-aliases"))
72
0
    {
73
0
      no_aliases = 1;
74
0
      return;
75
0
    }
76
77
0
  if (startswith (option, "aliases"))
78
0
    {
79
0
      no_aliases = 0;
80
0
      return;
81
0
    }
82
83
0
  if (startswith (option, "no-notes"))
84
0
    {
85
0
      no_notes = 1;
86
0
      return;
87
0
    }
88
89
0
  if (startswith (option, "notes"))
90
0
    {
91
0
      no_notes = 0;
92
0
      return;
93
0
    }
94
95
0
  if (startswith (option, "annotate"))
96
0
    {
97
0
      annotate_undefined_insns = true;
98
0
      return;
99
0
    }
100
101
0
  if (startswith (option, "no-annotate"))
102
0
    {
103
0
      annotate_undefined_insns = false;
104
0
      return;
105
0
    }
106
107
#ifdef DEBUG_AARCH64
108
  if (startswith (option, "debug_dump"))
109
    {
110
      debug_dump = 1;
111
      return;
112
    }
113
#endif /* DEBUG_AARCH64 */
114
115
  /* Invalid option.  */
116
0
  opcodes_error_handler (_("unrecognised disassembler option: %s"), option);
117
0
}
118
119
static void
120
parse_aarch64_dis_options (const char *options)
121
0
{
122
0
  const char *option_end;
123
124
0
  if (options == NULL)
125
0
    return;
126
127
0
  while (*options != '\0')
128
0
    {
129
      /* Skip empty options.  */
130
0
      if (*options == ',')
131
0
  {
132
0
    options++;
133
0
    continue;
134
0
  }
135
136
      /* We know that *options is neither NUL or a comma.  */
137
0
      option_end = options + 1;
138
0
      while (*option_end != ',' && *option_end != '\0')
139
0
  option_end++;
140
141
0
      parse_aarch64_dis_option (options, option_end - options);
142
143
      /* Go on to the next one.  If option_end points to a comma, it
144
   will be skipped above.  */
145
0
      options = option_end;
146
0
    }
147
0
}
148

149
/* Functions doing the instruction disassembling.  */
150
151
/* The unnamed arguments consist of the number of fields and information about
152
   these fields where the VALUE will be extracted from CODE and returned.
153
   MASK can be zero or the base mask of the opcode.
154
155
   N.B. the fields are required to be in such an order than the most signficant
156
   field for VALUE comes the first, e.g. the <index> in
157
    SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>]
158
   is encoded in H:L:M in some cases, the fields H:L:M should be passed in
159
   the order of H, L, M.  */
160
161
aarch64_insn
162
extract_fields (aarch64_insn code, aarch64_insn mask, ...)
163
1.58M
{
164
1.58M
  uint32_t num;
165
1.58M
  va_list va;
166
167
1.58M
  va_start (va, mask);
168
1.58M
  num = va_arg (va, uint32_t);
169
1.58M
  assert (num <= 5);
170
1.58M
  aarch64_insn value = 0x0;
171
5.28M
  while (num--)
172
3.69M
    {
173
3.69M
      aarch64_field field = va_arg (va, aarch64_field);
174
3.69M
      value <<= field.width;
175
3.69M
      value |= extract_field (field, code, mask);
176
3.69M
    }
177
1.58M
  va_end (va);
178
1.58M
  return value;
179
1.58M
}
180
181
/* Extract the value of all fields in SELF->fields after START from
182
   instruction CODE.  The least significant bit comes from the final field.  */
183
184
static aarch64_insn
185
extract_all_fields_after (const aarch64_operand *self, unsigned int start,
186
        aarch64_insn code)
187
13.0M
{
188
13.0M
  aarch64_insn value;
189
13.0M
  unsigned int i;
190
191
13.0M
  value = 0;
192
13.0M
  for (i = start;
193
26.9M
       i < ARRAY_SIZE (self->fields) && self->fields[i].width != 0; ++i)
194
13.9M
    {
195
13.9M
      aarch64_field field = self->fields[i];
196
13.9M
      value <<= field.width;
197
13.9M
      value |= extract_field (field, code, 0);
198
13.9M
    }
199
13.0M
  return value;
200
13.0M
}
201
202
/* Extract the value of all fields in SELF->fields from instruction CODE.
203
   The least significant bit comes from the final field.  */
204
205
static aarch64_insn
206
extract_all_fields (const aarch64_operand *self, aarch64_insn code)
207
13.0M
{
208
13.0M
  return extract_all_fields_after (self, 0, code);
209
13.0M
}
210
211
/* Sign-extend bit I of VALUE.  */
212
static inline uint64_t
213
sign_extend (aarch64_insn value, unsigned i)
214
1.99M
{
215
1.99M
  uint64_t ret, sign;
216
217
1.99M
  assert (i < 32);
218
1.99M
  ret = value;
219
1.99M
  sign = (uint64_t) 1 << i;
220
1.99M
  return ((ret & (sign + sign - 1)) ^ sign) - sign;
221
1.99M
}
222
223
/* N.B. the following inline helpfer functions create a dependency on the
224
   order of operand qualifier enumerators.  */
225
226
/* Given VALUE, return qualifier for a general purpose register.  */
227
static inline enum aarch64_opnd_qualifier
228
get_greg_qualifier_from_value (aarch64_insn value)
229
2.34M
{
230
2.34M
  enum aarch64_opnd_qualifier qualifier = AARCH64_OPND_QLF_W + value;
231
2.34M
  if (value <= 0x1
232
2.34M
      && aarch64_get_qualifier_standard_value (qualifier) == value)
233
2.34M
    return qualifier;
234
0
  return AARCH64_OPND_QLF_ERR;
235
2.34M
}
236
237
/* Given VALUE, return qualifier for a vector register.  This does not support
238
   decoding instructions that accept the 2H vector type.  */
239
240
static inline enum aarch64_opnd_qualifier
241
get_vreg_qualifier_from_value (aarch64_insn value)
242
203k
{
243
203k
  enum aarch64_opnd_qualifier qualifier = AARCH64_OPND_QLF_V_8B + value;
244
245
  /* Instructions using vector type 2H should not call this function.  Skip over
246
     the 2H qualifier.  */
247
203k
  if (qualifier >= AARCH64_OPND_QLF_V_2H)
248
138k
    qualifier += 1;
249
250
203k
  if (value <= 0x8
251
203k
      && aarch64_get_qualifier_standard_value (qualifier) == value)
252
203k
    return qualifier;
253
0
  return AARCH64_OPND_QLF_ERR;
254
203k
}
255
256
/* Given VALUE, return qualifier for an FP or AdvSIMD scalar register.  */
257
static inline enum aarch64_opnd_qualifier
258
get_sreg_qualifier_from_value (aarch64_insn value)
259
216k
{
260
216k
  enum aarch64_opnd_qualifier qualifier = AARCH64_OPND_QLF_S_B + value;
261
262
216k
  if (value <= 0x4
263
212k
      && aarch64_get_qualifier_standard_value (qualifier) == value)
264
212k
    return qualifier;
265
3.34k
  return AARCH64_OPND_QLF_ERR;
266
216k
}
267
268
/* Given the instruction in *INST which is probably half way through the
269
   decoding and our caller wants to know the expected qualifier for operand
270
   I.  Return such a qualifier if we can establish it; otherwise return
271
   AARCH64_OPND_QLF_UNKNOWN.  */
272
273
static aarch64_opnd_qualifier_t
274
get_expected_qualifier (const aarch64_inst *inst, int i)
275
1.05M
{
276
1.05M
  aarch64_opnd_qualifier_seq_t qualifiers;
277
  /* Should not be called if the qualifier is known.  */
278
1.05M
  if (inst->operands[i].qualifier == AARCH64_OPND_QLF_UNKNOWN)
279
1.05M
    {
280
1.05M
      int invalid_count;
281
1.05M
      if (aarch64_find_best_match (inst, inst->opcode->qualifiers_list,
282
1.05M
           i, qualifiers, &invalid_count))
283
1.02M
  return qualifiers[i];
284
26.4k
      else
285
26.4k
  return AARCH64_OPND_QLF_UNKNOWN;
286
1.05M
    }
287
0
  else
288
0
    return AARCH64_OPND_QLF_ERR;
289
1.05M
}
290
291
/* Operand extractors.  */
292
293
bool
294
aarch64_ext_none (const aarch64_operand *self ATTRIBUTE_UNUSED,
295
      aarch64_opnd_info *info ATTRIBUTE_UNUSED,
296
      const aarch64_insn code ATTRIBUTE_UNUSED,
297
      const aarch64_inst *inst ATTRIBUTE_UNUSED,
298
      aarch64_operand_error *errors ATTRIBUTE_UNUSED)
299
1.95k
{
300
1.95k
  return true;
301
1.95k
}
302
303
bool
304
aarch64_ext_regno (const aarch64_operand *self, aarch64_opnd_info *info,
305
       const aarch64_insn code,
306
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
307
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
308
9.38M
{
309
9.38M
  info->reg.regno = extract_all_fields (self, code);
310
9.38M
  return true;
311
9.38M
}
312
313
bool
314
aarch64_ext_regno_pair (const aarch64_operand *self ATTRIBUTE_UNUSED, aarch64_opnd_info *info,
315
       const aarch64_insn code ATTRIBUTE_UNUSED,
316
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
317
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
318
10.0k
{
319
10.0k
  assert (info->idx == 1
320
10.0k
    || info->idx == 2
321
10.0k
    || info->idx == 3
322
10.0k
    || info->idx == 5);
323
324
10.0k
  unsigned prev_regno = inst->operands[info->idx - 1].reg.regno;
325
10.0k
  info->reg.regno = (prev_regno == 0x1f) ? 0x1f
326
10.0k
           : prev_regno + 1;
327
10.0k
  return true;
328
10.0k
}
329
330
/* e.g. IC <ic_op>{, <Xt>}.  */
331
bool
332
aarch64_ext_regrt_sysins (const aarch64_operand *self, aarch64_opnd_info *info,
333
        const aarch64_insn code,
334
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
335
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
336
950
{
337
950
  info->reg.regno = extract_field (self->fields[0], code, 0);
338
950
  assert (info->idx == 1
339
950
    && (aarch64_get_operand_class (inst->operands[0].type)
340
950
        == AARCH64_OPND_CLASS_SYSTEM));
341
  /* This will make the constraint checking happy and more importantly will
342
     help the disassembler determine whether this operand is optional or
343
     not.  */
344
345
950
  info->present
346
950
    = (info->reg.regno != 31
347
295
       || aarch64_sys_ins_reg_has_xt (inst->operands[0].sysins_op));
348
950
  return true;
349
950
}
350
351
/* e.g. SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>].  */
352
bool
353
aarch64_ext_reglane (const aarch64_operand *self, aarch64_opnd_info *info,
354
         const aarch64_insn code,
355
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
356
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
357
121k
{
358
  /* regno */
359
121k
  info->reglane.regno = extract_field (self->fields[0], code,
360
121k
               inst->opcode->mask);
361
362
  /* Index and/or type.  */
363
121k
  if (inst->opcode->iclass == asisdone
364
120k
    || inst->opcode->iclass == asimdins)
365
14.7k
    {
366
14.7k
      if (info->type == AARCH64_OPND_En
367
10.8k
    && inst->opcode->operands[0] == AARCH64_OPND_Ed)
368
3.39k
  {
369
3.39k
    unsigned shift;
370
    /* index2 for e.g. INS <Vd>.<Ts>[<index1>], <Vn>.<Ts>[<index2>].  */
371
3.39k
    assert (info->idx == 1);  /* Vn */
372
3.39k
    aarch64_insn value = extract_field (FLD_imm4_11, code, 0);
373
    /* Depend on AARCH64_OPND_Ed to determine the qualifier.  */
374
3.39k
    info->qualifier = get_expected_qualifier (inst, info->idx);
375
3.39k
    if (info->qualifier == AARCH64_OPND_QLF_ERR)
376
0
      return 0;
377
3.39k
    shift = get_logsz (aarch64_get_qualifier_esize (info->qualifier));
378
3.39k
    info->reglane.index = value >> shift;
379
3.39k
  }
380
11.4k
      else
381
11.4k
  {
382
    /* index and type for e.g. DUP <V><d>, <Vn>.<T>[<index>].
383
       imm5<3:0>  <V>
384
       0000 RESERVED
385
       xxx1 B
386
       xx10 H
387
       x100 S
388
       1000 D  */
389
11.4k
    int pos = -1;
390
11.4k
    aarch64_insn value = extract_field (FLD_imm5, code, 0);
391
26.2k
    while (++pos <= 3 && (value & 0x1) == 0)
392
14.8k
      value >>= 1;
393
11.4k
    if (pos > 3)
394
1.13k
      return false;
395
10.2k
    info->qualifier = get_sreg_qualifier_from_value (pos);
396
10.2k
    if (info->qualifier == AARCH64_OPND_QLF_ERR)
397
0
      return 0;
398
10.2k
    info->reglane.index = (unsigned) (value >> 1);
399
10.2k
  }
400
14.7k
    }
401
106k
  else if (inst->opcode->iclass == dotproduct)
402
29.1k
    {
403
      /* Need information in other operand(s) to help decoding.  */
404
29.1k
      info->qualifier = get_expected_qualifier (inst, info->idx);
405
29.1k
      if (info->qualifier == AARCH64_OPND_QLF_ERR)
406
0
  return 0;
407
29.1k
      switch (info->qualifier)
408
29.1k
  {
409
22.3k
  case AARCH64_OPND_QLF_S_4B:
410
23.5k
  case AARCH64_OPND_QLF_S_2H:
411
    /* L:H */
412
23.5k
    info->reglane.index = extract_fields (code, 0, 2, FLD_H, FLD_L);
413
23.5k
    info->reglane.regno &= 0x1f;
414
23.5k
    break;
415
2.26k
  case AARCH64_OPND_QLF_S_2B:
416
    /* h:l:m */
417
2.26k
    info->reglane.index = extract_fields (code, 0, 3, FLD_H, FLD_L,
418
2.26k
            FLD_M);
419
2.26k
    info->reglane.regno &= 0xf;
420
2.26k
    break;
421
3.29k
  default:
422
3.29k
    return false;
423
29.1k
  }
424
29.1k
    }
425
77.3k
  else if (inst->opcode->iclass == cryptosm3)
426
1.09k
    {
427
      /* index for e.g. SM3TT2A <Vd>.4S, <Vn>.4S, <Vm>S[<imm2>].  */
428
1.09k
      info->reglane.index = extract_field (FLD_SM3_imm2, code, 0);
429
1.09k
    }
430
76.2k
  else
431
76.2k
    {
432
      /* Index only for e.g. SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>]
433
         or SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>].  */
434
435
      /* Need information in other operand(s) to help decoding.  */
436
76.2k
      info->qualifier = get_expected_qualifier (inst, info->idx);
437
76.2k
      if (info->qualifier == AARCH64_OPND_QLF_ERR)
438
0
  return 0;
439
76.2k
      switch (info->qualifier)
440
76.2k
  {
441
3.03k
  case AARCH64_OPND_QLF_S_B:
442
    /* H:imm3 */
443
3.03k
    info->reglane.index = extract_fields (code, 0, 2, FLD_H,
444
3.03k
            FLD_imm3_19);
445
3.03k
    info->reglane.regno &= 0x7;
446
3.03k
    break;
447
448
40.9k
  case AARCH64_OPND_QLF_S_H:
449
40.9k
  case AARCH64_OPND_QLF_S_2B:
450
40.9k
    if (info->type == AARCH64_OPND_Em16)
451
34.8k
      {
452
        /* h:l:m */
453
34.8k
        info->reglane.index = extract_fields (code, 0, 3, FLD_H, FLD_L,
454
34.8k
                FLD_M);
455
34.8k
        info->reglane.regno &= 0xf;
456
34.8k
      }
457
6.05k
    else
458
6.05k
      {
459
        /* h:l */
460
6.05k
        info->reglane.index = extract_fields (code, 0, 2, FLD_H, FLD_L);
461
6.05k
      }
462
40.9k
    break;
463
8.14k
  case AARCH64_OPND_QLF_S_S:
464
8.14k
  case AARCH64_OPND_QLF_S_4B:
465
    /* h:l */
466
8.14k
    info->reglane.index = extract_fields (code, 0, 2, FLD_H, FLD_L);
467
8.14k
    break;
468
1.02k
  case AARCH64_OPND_QLF_S_D:
469
    /* H */
470
1.02k
    info->reglane.index = extract_field (FLD_H, code, 0);
471
1.02k
    break;
472
23.1k
  default:
473
23.1k
    return false;
474
76.2k
  }
475
476
53.1k
      if (inst->opcode->op == OP_FCMLA_ELEM
477
7.13k
    && info->qualifier != AARCH64_OPND_QLF_S_H)
478
1.07k
  {
479
    /* Complex operand takes two elements.  */
480
1.07k
    if (info->reglane.index & 1)
481
356
      return false;
482
722
    info->reglane.index /= 2;
483
722
  }
484
53.1k
    }
485
486
93.3k
  return true;
487
121k
}
488
489
bool
490
aarch64_ext_reglist (const aarch64_operand *self, aarch64_opnd_info *info,
491
         const aarch64_insn code,
492
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
493
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
494
15.5k
{
495
  /* R */
496
15.5k
  info->reglist.first_regno = extract_field (self->fields[0], code, 0);
497
  /* len */
498
15.5k
  info->reglist.num_regs = extract_field (FLD_len, code, 0) + 1;
499
15.5k
  info->reglist.stride = 1;
500
15.5k
  return true;
501
15.5k
}
502
503
/* Decode Rt and opcode fields of Vt in AdvSIMD load/store instructions.  */
504
bool
505
aarch64_ext_ldst_reglist (const aarch64_operand *self ATTRIBUTE_UNUSED,
506
        aarch64_opnd_info *info, const aarch64_insn code,
507
        const aarch64_inst *inst,
508
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
509
54.3k
{
510
54.3k
  aarch64_insn value;
511
  /* Number of elements in each structure to be loaded/stored.  */
512
54.3k
  unsigned expected_num = get_opcode_dependent_value (inst->opcode);
513
514
54.3k
  static const struct
515
54.3k
    {
516
54.3k
      unsigned num_regs:8;
517
54.3k
      unsigned num_elements:8;
518
54.3k
      bool is_reserved:1;
519
54.3k
    } data [] =
520
54.3k
  {   {4, 4, false},
521
54.3k
      {4, 4, true},
522
54.3k
      {4, 1, false},
523
54.3k
      {4, 2, false},
524
54.3k
      {3, 3, false},
525
54.3k
      {3, 3, true},
526
54.3k
      {3, 1, false},
527
54.3k
      {1, 1, false},
528
54.3k
      {2, 2, false},
529
54.3k
      {2, 2, true},
530
54.3k
      {2, 1, false},
531
54.3k
  };
532
533
  /* Rt */
534
54.3k
  info->reglist.first_regno = extract_field (FLD_Rt, code, 0);
535
  /* opcode */
536
54.3k
  value = extract_field (FLD_opcode, code, 0);
537
  /* PR 21595: Check for a bogus value.  */
538
54.3k
  if (value >= ARRAY_SIZE (data))
539
20.1k
    return false;
540
34.2k
  if (expected_num != data[value].num_elements || data[value].is_reserved)
541
21.4k
    return false;
542
12.8k
  info->reglist.num_regs = data[value].num_regs;
543
12.8k
  info->reglist.stride = 1;
544
545
12.8k
  return true;
546
34.2k
}
547
548
/* Decode Rt and S fields of Vt in AdvSIMD load single structure to all
549
   lanes instructions.  */
550
bool
551
aarch64_ext_ldst_reglist_r (const aarch64_operand *self ATTRIBUTE_UNUSED,
552
          aarch64_opnd_info *info, const aarch64_insn code,
553
          const aarch64_inst *inst,
554
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
555
1.86k
{
556
1.86k
  aarch64_insn value;
557
558
  /* Rt */
559
1.86k
  info->reglist.first_regno = extract_field (FLD_Rt, code, 0);
560
  /* S */
561
1.86k
  value = extract_field (FLD_S, code, 0);
562
563
  /* Number of registers is equal to the number of elements in
564
     each structure to be loaded/stored.  */
565
1.86k
  info->reglist.num_regs = get_opcode_dependent_value (inst->opcode);
566
1.86k
  assert (info->reglist.num_regs >= 1 && info->reglist.num_regs <= 4);
567
568
  /* Except when it is LD1R.  */
569
1.86k
  if (info->reglist.num_regs == 1 && value == (aarch64_insn) 1)
570
0
    info->reglist.num_regs = 2;
571
572
1.86k
  info->reglist.stride = 1;
573
1.86k
  return true;
574
1.86k
}
575
576
/* Decode AdvSIMD vector register list for AdvSIMD lut instructions.
577
   The number of of registers in the list is determined by the opcode
578
   flag.  */
579
bool
580
aarch64_ext_lut_reglist (const aarch64_operand *self, aarch64_opnd_info *info,
581
         const aarch64_insn code,
582
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
583
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
584
3.49k
{
585
3.49k
  info->reglist.first_regno = extract_field (self->fields[0], code, 0);
586
3.49k
  info->reglist.num_regs = get_opcode_dependent_value (inst->opcode);
587
3.49k
  info->reglist.stride = 1;
588
3.49k
  return true;
589
3.49k
}
590
591
/* Decode Q, opcode<2:1>, S, size and Rt fields of Vt in AdvSIMD
592
   load/store single element instructions.  */
593
bool
594
aarch64_ext_ldst_elemlist (const aarch64_operand *self ATTRIBUTE_UNUSED,
595
         aarch64_opnd_info *info, const aarch64_insn code,
596
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
597
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
598
29.7k
{
599
29.7k
  aarch64_field field = AARCH64_FIELD_NIL;
600
29.7k
  aarch64_insn QSsize;    /* fields Q:S:size.  */
601
29.7k
  aarch64_insn opcodeh2;  /* opcode<2:1> */
602
603
  /* Rt */
604
29.7k
  info->reglist.first_regno = extract_field (FLD_Rt, code, 0);
605
606
  /* Decode the index, opcode<2:1> and size.  */
607
29.7k
  gen_sub_field (FLD_asisdlso_opcode, 1, 2, &field);
608
29.7k
  opcodeh2 = extract_field (field, code, 0);
609
29.7k
  QSsize = extract_fields (code, 0, 3, FLD_Q, FLD_S, FLD_vldst_size);
610
29.7k
  switch (opcodeh2)
611
29.7k
    {
612
13.1k
    case 0x0:
613
13.1k
      info->qualifier = AARCH64_OPND_QLF_S_B;
614
      /* Index encoded in "Q:S:size".  */
615
13.1k
      info->reglist.index = QSsize;
616
13.1k
      break;
617
5.88k
    case 0x1:
618
5.88k
      if (QSsize & 0x1)
619
  /* UND.  */
620
3.06k
  return false;
621
2.82k
      info->qualifier = AARCH64_OPND_QLF_S_H;
622
      /* Index encoded in "Q:S:size<1>".  */
623
2.82k
      info->reglist.index = QSsize >> 1;
624
2.82k
      break;
625
4.62k
    case 0x2:
626
4.62k
      if ((QSsize >> 1) & 0x1)
627
  /* UND.  */
628
1.96k
  return false;
629
2.65k
      if ((QSsize & 0x1) == 0)
630
1.33k
  {
631
1.33k
    info->qualifier = AARCH64_OPND_QLF_S_S;
632
    /* Index encoded in "Q:S".  */
633
1.33k
    info->reglist.index = QSsize >> 2;
634
1.33k
  }
635
1.32k
      else
636
1.32k
  {
637
1.32k
    if (extract_field (FLD_S, code, 0))
638
      /* UND */
639
542
      return false;
640
778
    info->qualifier = AARCH64_OPND_QLF_S_D;
641
    /* Index encoded in "Q".  */
642
778
    info->reglist.index = QSsize >> 3;
643
778
  }
644
2.11k
      break;
645
6.05k
    default:
646
6.05k
      return false;
647
29.7k
    }
648
649
18.1k
  info->reglist.has_index = 1;
650
18.1k
  info->reglist.num_regs = 0;
651
18.1k
  info->reglist.stride = 1;
652
  /* Number of registers is equal to the number of elements in
653
     each structure to be loaded/stored.  */
654
18.1k
  info->reglist.num_regs = get_opcode_dependent_value (inst->opcode);
655
18.1k
  assert (info->reglist.num_regs >= 1 && info->reglist.num_regs <= 4);
656
657
18.1k
  return true;
658
18.1k
}
659
660
/* Decode fields immh:immb and/or Q for e.g.
661
   SSHR <Vd>.<T>, <Vn>.<T>, #<shift>
662
   or SSHR <V><d>, <V><n>, #<shift>.  */
663
664
bool
665
aarch64_ext_advsimd_imm_shift (const aarch64_operand *self ATTRIBUTE_UNUSED,
666
             aarch64_opnd_info *info, const aarch64_insn code,
667
             const aarch64_inst *inst,
668
             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
669
36.1k
{
670
36.1k
  int pos;
671
36.1k
  aarch64_insn Q, imm, immh;
672
36.1k
  enum aarch64_insn_class iclass = inst->opcode->iclass;
673
674
36.1k
  immh = extract_field (FLD_immh, code, 0);
675
36.1k
  if (immh == 0)
676
4.98k
    return false;
677
31.1k
  imm = extract_fields (code, 0, 2, FLD_immh, FLD_immb);
678
31.1k
  pos = 4;
679
  /* Get highest set bit in immh.  */
680
53.8k
  while (--pos >= 0 && (immh & 0x8) == 0)
681
22.7k
    immh <<= 1;
682
683
31.1k
  assert ((iclass == asimdshf || iclass == asisdshf)
684
31.1k
    && (info->type == AARCH64_OPND_IMM_VLSR
685
31.1k
        || info->type == AARCH64_OPND_IMM_VLSL));
686
687
31.1k
  if (iclass == asimdshf)
688
20.8k
    {
689
20.8k
      Q = extract_field (FLD_Q, code, 0);
690
      /* immh Q <T>
691
   0000 x SEE AdvSIMD modified immediate
692
   0001 0 8B
693
   0001 1 16B
694
   001x 0 4H
695
   001x 1 8H
696
   01xx 0 2S
697
   01xx 1 4S
698
   1xxx 0 RESERVED
699
   1xxx 1 2D  */
700
20.8k
      info->qualifier =
701
20.8k
  get_vreg_qualifier_from_value ((pos << 1) | (int) Q);
702
20.8k
      if (info->qualifier == AARCH64_OPND_QLF_ERR)
703
0
  return false;
704
20.8k
    }
705
10.3k
  else
706
10.3k
    {
707
10.3k
      info->qualifier = get_sreg_qualifier_from_value (pos);
708
10.3k
      if (info->qualifier == AARCH64_OPND_QLF_ERR)
709
0
  return 0;
710
10.3k
    }
711
712
31.1k
  if (info->type == AARCH64_OPND_IMM_VLSR)
713
    /* immh <shift>
714
       0000 SEE AdvSIMD modified immediate
715
       0001 (16-UInt(immh:immb))
716
       001x (32-UInt(immh:immb))
717
       01xx (64-UInt(immh:immb))
718
       1xxx (128-UInt(immh:immb))  */
719
22.2k
    info->imm.value = (16 << pos) - imm;
720
8.86k
  else
721
    /* immh:immb
722
       immh <shift>
723
       0000 SEE AdvSIMD modified immediate
724
       0001 (UInt(immh:immb)-8)
725
       001x (UInt(immh:immb)-16)
726
       01xx (UInt(immh:immb)-32)
727
       1xxx (UInt(immh:immb)-64)  */
728
8.86k
    info->imm.value = imm - (8 << pos);
729
730
31.1k
  return true;
731
31.1k
}
732
733
/* Decode shift immediate for e.g. sshr (imm).  */
734
bool
735
aarch64_ext_shll_imm (const aarch64_operand *self ATTRIBUTE_UNUSED,
736
          aarch64_opnd_info *info, const aarch64_insn code,
737
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
738
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
739
132
{
740
132
  int64_t imm;
741
132
  aarch64_insn val;
742
132
  val = extract_field (FLD_size, code, 0);
743
132
  switch (val)
744
132
    {
745
1
    case 0: imm = 8; break;
746
116
    case 1: imm = 16; break;
747
15
    case 2: imm = 32; break;
748
0
    default: return false;
749
132
    }
750
132
  info->imm.value = imm;
751
132
  return true;
752
132
}
753
754
/* Decode imm for e.g. BFM <Wd>, <Wn>, #<immr>, #<imms>.
755
   value in the field(s) will be extracted as unsigned immediate value.  */
756
bool
757
aarch64_ext_imm (const aarch64_operand *self, aarch64_opnd_info *info,
758
     const aarch64_insn code,
759
     const aarch64_inst *inst,
760
     aarch64_operand_error *errors ATTRIBUTE_UNUSED)
761
3.52M
{
762
3.52M
  uint64_t imm;
763
764
3.52M
  imm = extract_all_fields (self, code);
765
766
3.52M
  if (operand_need_sign_extension (self))
767
1.31M
    imm = sign_extend (imm, get_operand_fields_width (self) - 1);
768
769
3.52M
  if (operand_need_shift_by_two (self))
770
872k
    imm <<= 2;
771
2.65M
  else if (operand_need_shift_by_three (self))
772
97
    imm <<= 3;
773
2.65M
  else if (operand_need_shift_by_four (self))
774
3.05k
    imm <<= 4;
775
776
3.52M
  if (info->type == AARCH64_OPND_ADDR_ADRP)
777
130k
    imm <<= 12;
778
779
3.52M
  if (inst->operands[0].type == AARCH64_OPND_PSTATEFIELD
780
223
      && inst->operands[0].sysreg.flags & F_IMM_IN_CRM)
781
0
    imm &= PSTATE_DECODE_CRM_IMM (inst->operands[0].sysreg.flags);
782
783
3.52M
  info->imm.value = imm;
784
3.52M
  return true;
785
3.52M
}
786
787
/* Decode imm and its shifter for e.g. MOVZ <Wd>, #<imm16>{, LSL #<shift>}.  */
788
bool
789
aarch64_ext_imm_half (const aarch64_operand *self, aarch64_opnd_info *info,
790
          const aarch64_insn code,
791
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
792
          aarch64_operand_error *errors)
793
96.0k
{
794
96.0k
  aarch64_ext_imm (self, info, code, inst, errors);
795
96.0k
  info->shifter.kind = AARCH64_MOD_LSL;
796
96.0k
  info->shifter.amount = extract_field (FLD_hw, code, 0) << 4;
797
96.0k
  return true;
798
96.0k
}
799
800
/* Decode cmode and "a:b:c:d:e:f:g:h" for e.g.
801
     MOVI <Vd>.<T>, #<imm8> {, LSL #<amount>}.  */
802
bool
803
aarch64_ext_advsimd_imm_modified (const aarch64_operand *self ATTRIBUTE_UNUSED,
804
          aarch64_opnd_info *info,
805
          const aarch64_insn code,
806
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
807
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
808
6.29k
{
809
6.29k
  uint64_t imm;
810
6.29k
  enum aarch64_opnd_qualifier opnd0_qualifier = inst->operands[0].qualifier;
811
6.29k
  aarch64_field field = AARCH64_FIELD_NIL;
812
813
6.29k
  assert (info->idx == 1);
814
815
6.29k
  if (info->type == AARCH64_OPND_SIMD_FPIMM)
816
2.24k
    info->imm.is_fp = 1;
817
818
  /* a:b:c:d:e:f:g:h */
819
6.29k
  imm = extract_fields (code, 0, 2, FLD_abc, FLD_defgh);
820
6.29k
  if (!info->imm.is_fp && aarch64_get_qualifier_esize (opnd0_qualifier) == 8)
821
853
    {
822
      /* Either MOVI <Dd>, #<imm>
823
   or     MOVI <Vd>.2D, #<imm>.
824
   <imm> is a 64-bit immediate
825
   'aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffgggggggghhhhhhhh',
826
   encoded in "a:b:c:d:e:f:g:h".  */
827
853
      int i;
828
853
      unsigned abcdefgh = imm;
829
7.67k
      for (imm = 0ull, i = 0; i < 8; i++)
830
6.82k
  if (((abcdefgh >> i) & 0x1) != 0)
831
3.92k
    imm |= 0xffull << (8 * i);
832
853
    }
833
6.29k
  info->imm.value = imm;
834
835
  /* cmode */
836
6.29k
  info->qualifier = get_expected_qualifier (inst, info->idx);
837
6.29k
  if (info->qualifier == AARCH64_OPND_QLF_ERR)
838
0
    return 0;
839
6.29k
  switch (info->qualifier)
840
6.29k
    {
841
3.09k
    case AARCH64_OPND_QLF_NIL:
842
      /* no shift */
843
3.09k
      info->shifter.kind = AARCH64_MOD_NONE;
844
3.09k
      return 1;
845
2.78k
    case AARCH64_OPND_QLF_LSL:
846
      /* shift zeros */
847
2.78k
      info->shifter.kind = AARCH64_MOD_LSL;
848
2.78k
      switch (aarch64_get_qualifier_esize (opnd0_qualifier))
849
2.78k
  {
850
2.16k
  case 4: gen_sub_field (FLD_cmode, 1, 2, &field); break; /* per word */
851
518
  case 2: gen_sub_field (FLD_cmode, 1, 1, &field); break; /* per half */
852
101
  case 1: gen_sub_field (FLD_cmode, 1, 0, &field); break; /* per byte */
853
0
  default: return false;
854
2.78k
  }
855
      /* 00: 0; 01: 8; 10:16; 11:24.  */
856
2.78k
      info->shifter.amount = extract_field (field, code, 0) << 3;
857
2.78k
      break;
858
417
    case AARCH64_OPND_QLF_MSL:
859
      /* shift ones */
860
417
      info->shifter.kind = AARCH64_MOD_MSL;
861
417
      gen_sub_field (FLD_cmode, 0, 1, &field);    /* per word */
862
417
      info->shifter.amount = extract_field (field, code, 0) ? 16 : 8;
863
417
      break;
864
0
    default:
865
0
      return false;
866
6.29k
    }
867
868
3.19k
  return true;
869
6.29k
}
870
871
/* Decode an 8-bit floating-point immediate.  */
872
bool
873
aarch64_ext_fpimm (const aarch64_operand *self, aarch64_opnd_info *info,
874
       const aarch64_insn code,
875
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
876
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
877
1.61k
{
878
1.61k
  info->imm.value = extract_all_fields (self, code);
879
1.61k
  info->imm.is_fp = 1;
880
1.61k
  return true;
881
1.61k
}
882
883
/* Decode a 1-bit rotate immediate (#90 or #270).  */
884
bool
885
aarch64_ext_imm_rotate1 (const aarch64_operand *self, aarch64_opnd_info *info,
886
       const aarch64_insn code,
887
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
888
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
889
1.57k
{
890
1.57k
  uint64_t rot = extract_field (self->fields[0], code, 0);
891
1.57k
  assert (rot < 2U);
892
1.57k
  info->imm.value = rot * 180 + 90;
893
1.57k
  return true;
894
1.57k
}
895
896
/* Decode a 2-bit rotate immediate (#0, #90, #180 or #270).  */
897
bool
898
aarch64_ext_imm_rotate2 (const aarch64_operand *self, aarch64_opnd_info *info,
899
       const aarch64_insn code,
900
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
901
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
902
24.5k
{
903
24.5k
  uint64_t rot = extract_field (self->fields[0], code, 0);
904
24.5k
  assert (rot < 4U);
905
24.5k
  info->imm.value = rot * 90;
906
24.5k
  return true;
907
24.5k
}
908
909
/* Decode scale for e.g. SCVTF <Dd>, <Wn>, #<fbits>.  */
910
bool
911
aarch64_ext_fbits (const aarch64_operand *self ATTRIBUTE_UNUSED,
912
       aarch64_opnd_info *info, const aarch64_insn code,
913
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
914
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
915
2.44k
{
916
2.44k
  info->imm.value = 64- extract_field (FLD_scale, code, 0);
917
2.44k
  return true;
918
2.44k
}
919
920
/* Decode arithmetic immediate for e.g.
921
     SUBS <Wd>, <Wn|WSP>, #<imm> {, <shift>}.  */
922
bool
923
aarch64_ext_aimm (const aarch64_operand *self ATTRIBUTE_UNUSED,
924
      aarch64_opnd_info *info, const aarch64_insn code,
925
      const aarch64_inst *inst ATTRIBUTE_UNUSED,
926
      aarch64_operand_error *errors ATTRIBUTE_UNUSED)
927
518k
{
928
518k
  aarch64_insn value;
929
930
518k
  info->shifter.kind = AARCH64_MOD_LSL;
931
  /* shift */
932
518k
  value = extract_field (FLD_shift, code, 0);
933
518k
  if (value >= 2)
934
123k
    return false;
935
394k
  info->shifter.amount = value ? 12 : 0;
936
  /* imm12 (unsigned) */
937
394k
  info->imm.value = extract_field (FLD_imm12, code, 0);
938
939
394k
  return true;
940
518k
}
941
942
/* Return true if VALUE is a valid logical immediate encoding, storing the
943
   decoded value in *RESULT if so.  ESIZE is the number of bytes in the
944
   decoded immediate.  */
945
static bool
946
decode_limm (uint32_t esize, aarch64_insn value, int64_t *result)
947
386k
{
948
386k
  uint64_t imm, mask;
949
386k
  uint32_t N, R, S;
950
386k
  unsigned simd_size;
951
952
  /* value is N:immr:imms.  */
953
386k
  S = value & 0x3f;
954
386k
  R = (value >> 6) & 0x3f;
955
386k
  N = (value >> 12) & 0x1;
956
957
  /* The immediate value is S+1 bits to 1, left rotated by SIMDsize - R
958
     (in other words, right rotated by R), then replicated.  */
959
386k
  if (N != 0)
960
107k
    {
961
107k
      simd_size = 64;
962
107k
      mask = 0xffffffffffffffffull;
963
107k
    }
964
279k
  else
965
279k
    {
966
279k
      switch (S)
967
279k
  {
968
218k
  case 0x00 ... 0x1f: /* 0xxxxx */ simd_size = 32;           break;
969
29.9k
  case 0x20 ... 0x2f: /* 10xxxx */ simd_size = 16; S &= 0xf; break;
970
14.6k
  case 0x30 ... 0x37: /* 110xxx */ simd_size =  8; S &= 0x7; break;
971
7.89k
  case 0x38 ... 0x3b: /* 1110xx */ simd_size =  4; S &= 0x3; break;
972
3.29k
  case 0x3c ... 0x3d: /* 11110x */ simd_size =  2; S &= 0x1; break;
973
4.82k
  default: return false;
974
279k
  }
975
274k
      mask = (1ull << simd_size) - 1;
976
      /* Top bits are IGNORED.  */
977
274k
      R &= simd_size - 1;
978
274k
    }
979
980
381k
  if (simd_size > esize * 8)
981
65.9k
    return false;
982
983
  /* NOTE: if S = simd_size - 1 we get 0xf..f which is rejected.  */
984
315k
  if (S == simd_size - 1)
985
5.18k
    return false;
986
  /* S+1 consecutive bits to 1.  */
987
  /* NOTE: S can't be 63 due to detection above.  */
988
310k
  imm = (1ull << (S + 1)) - 1;
989
  /* Rotate to the left by simd_size - R.  */
990
310k
  if (R != 0)
991
242k
    imm = ((imm << (simd_size - R)) & mask) | (imm >> R);
992
  /* Replicate the value according to SIMD size.  */
993
310k
  switch (simd_size)
994
310k
    {
995
2.30k
    case  2: imm = (imm <<  2) | imm;
996
      /* Fall through.  */
997
8.73k
    case  4: imm = (imm <<  4) | imm;
998
      /* Fall through.  */
999
22.5k
    case  8: imm = (imm <<  8) | imm;
1000
      /* Fall through.  */
1001
51.9k
    case 16: imm = (imm << 16) | imm;
1002
      /* Fall through.  */
1003
270k
    case 32: imm = (imm << 32) | imm;
1004
      /* Fall through.  */
1005
310k
    case 64: break;
1006
0
    default: return 0;
1007
310k
    }
1008
1009
310k
  *result = imm & ~((uint64_t) -1 << (esize * 4) << (esize * 4));
1010
1011
310k
  return true;
1012
310k
}
1013
1014
/* Decode a logical immediate for e.g. ORR <Wd|WSP>, <Wn>, #<imm>.  */
1015
bool
1016
aarch64_ext_limm (const aarch64_operand *self,
1017
      aarch64_opnd_info *info, const aarch64_insn code,
1018
      const aarch64_inst *inst,
1019
      aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1020
386k
{
1021
386k
  uint32_t esize;
1022
386k
  aarch64_insn value;
1023
1024
386k
  value = extract_fields (code, 0, 3, self->fields[0], self->fields[1],
1025
386k
        self->fields[2]);
1026
386k
  esize = aarch64_get_qualifier_esize (inst->operands[0].qualifier);
1027
386k
  return decode_limm (esize, value, &info->imm.value);
1028
386k
}
1029
1030
/* Decode a logical immediate for the BIC alias of AND (etc.).  */
1031
bool
1032
aarch64_ext_inv_limm (const aarch64_operand *self,
1033
          aarch64_opnd_info *info, const aarch64_insn code,
1034
          const aarch64_inst *inst,
1035
          aarch64_operand_error *errors)
1036
0
{
1037
0
  if (!aarch64_ext_limm (self, info, code, inst, errors))
1038
0
    return false;
1039
0
  info->imm.value = ~info->imm.value;
1040
0
  return true;
1041
0
}
1042
1043
/* Decode Ft for e.g. STR <Qt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]
1044
   or LDP <Qt1>, <Qt2>, [<Xn|SP>], #<imm>.  */
1045
bool
1046
aarch64_ext_ft (const aarch64_operand *self ATTRIBUTE_UNUSED,
1047
    aarch64_opnd_info *info,
1048
    const aarch64_insn code, const aarch64_inst *inst,
1049
    aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1050
589k
{
1051
589k
  aarch64_insn value;
1052
1053
  /* Rt */
1054
589k
  info->reg.regno = extract_field (FLD_Rt, code, 0);
1055
1056
  /* size */
1057
589k
  value = extract_field (FLD_ldst_size, code, 0);
1058
589k
  if (inst->opcode->iclass == ldstpair_indexed
1059
479k
      || inst->opcode->iclass == ldstnapair_offs
1060
395k
      || inst->opcode->iclass == ldstpair_off
1061
317k
      || inst->opcode->iclass == loadlit)
1062
370k
    {
1063
370k
      switch (value)
1064
370k
  {
1065
103k
  case 0: info->qualifier = AARCH64_OPND_QLF_S_S; break;
1066
112k
  case 1: info->qualifier = AARCH64_OPND_QLF_S_D; break;
1067
72.1k
  case 2: info->qualifier = AARCH64_OPND_QLF_S_Q; break;
1068
82.3k
  default: return false;
1069
370k
  }
1070
370k
    }
1071
218k
  else
1072
218k
    {
1073
      /* opc1:size */
1074
218k
      value = extract_fields (code, 0, 2, FLD_opc1, FLD_ldst_size);
1075
218k
      if (value > 0x4)
1076
69.9k
  return false;
1077
148k
      info->qualifier = get_sreg_qualifier_from_value (value);
1078
148k
      if (info->qualifier == AARCH64_OPND_QLF_ERR)
1079
0
  return false;
1080
148k
    }
1081
1082
436k
  return true;
1083
589k
}
1084
1085
/* Decode the address operand for e.g. STXRB <Ws>, <Wt>, [<Xn|SP>{,#0}].  */
1086
bool
1087
aarch64_ext_addr_simple (const aarch64_operand *self ATTRIBUTE_UNUSED,
1088
       aarch64_opnd_info *info,
1089
       aarch64_insn code,
1090
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
1091
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1092
248k
{
1093
  /* Rn */
1094
248k
  info->addr.base_regno = extract_field (FLD_Rn, code, 0);
1095
248k
  return true;
1096
248k
}
1097
1098
/* Decode the address operand for rcpc3 instructions with optional load/store
1099
   datasize offset, e.g. STILPP <Xs>, <Xt>, [<Xn|SP>{,#-16}]! and
1100
   LIDAP <Xs>, <Xt>, [<Xn|SP>]{,#-16}.  */
1101
bool
1102
aarch64_ext_rcpc3_addr_opt_offset (const aarch64_operand *self ATTRIBUTE_UNUSED,
1103
           aarch64_opnd_info *info,
1104
           aarch64_insn code,
1105
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
1106
           aarch64_operand_error *err ATTRIBUTE_UNUSED)
1107
16.1k
{
1108
16.1k
  info->addr.base_regno = extract_field (FLD_Rn, code, 0);
1109
16.1k
  if (!extract_field (FLD_opc2, code, 0))
1110
1.67k
    {
1111
1.67k
      info->addr.writeback = 1;
1112
1113
1.67k
      enum aarch64_opnd type;
1114
1.67k
      for (int i = 0; i < AARCH64_MAX_OPND_NUM; i++)
1115
1.67k
  {
1116
1.67k
    type = info[i].type;
1117
1.67k
    if (aarch64_operands[type].op_class == AARCH64_OPND_CLASS_ADDRESS)
1118
1.67k
      break;
1119
1.67k
  }
1120
1121
1.67k
      assert (aarch64_operands[type].op_class == AARCH64_OPND_CLASS_ADDRESS);
1122
1.67k
      int offset = calc_ldst_datasize (inst->operands);
1123
1124
1.67k
      switch (type)
1125
1.67k
  {
1126
640
  case AARCH64_OPND_RCPC3_ADDR_OPT_PREIND_WB:
1127
912
  case AARCH64_OPND_RCPC3_ADDR_PREIND_WB:
1128
912
    info->addr.offset.imm = -offset;
1129
912
    info->addr.preind = 1;
1130
912
    break;
1131
700
  case AARCH64_OPND_RCPC3_ADDR_OPT_POSTIND:
1132
758
  case AARCH64_OPND_RCPC3_ADDR_POSTIND:
1133
758
    info->addr.offset.imm = offset;
1134
758
    info->addr.postind = 1;
1135
758
    break;
1136
0
  default:
1137
0
    return false;
1138
1.67k
  }
1139
1.67k
    }
1140
16.1k
  return true;
1141
16.1k
}
1142
1143
bool
1144
aarch64_ext_rcpc3_addr_offset (const aarch64_operand *self ATTRIBUTE_UNUSED,
1145
             aarch64_opnd_info *info,
1146
             aarch64_insn code,
1147
             const aarch64_inst *inst ATTRIBUTE_UNUSED,
1148
             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1149
6.43k
{
1150
6.43k
  info->qualifier = get_expected_qualifier (inst, info->idx);
1151
6.43k
  if (info->qualifier == AARCH64_OPND_QLF_ERR)
1152
0
    return 0;
1153
1154
  /* Rn */
1155
6.43k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1156
1157
  /* simm9 */
1158
6.43k
  aarch64_insn imm = extract_fields (code, 0, 1, self->fields[1]);
1159
6.43k
  info->addr.offset.imm = sign_extend (imm, 8);
1160
6.43k
  return true;
1161
6.43k
}
1162
1163
/* Decode the address operand for e.g.
1164
     stlur <Xt>, [<Xn|SP>{, <amount>}].  */
1165
bool
1166
aarch64_ext_addr_offset (const aarch64_operand *self ATTRIBUTE_UNUSED,
1167
       aarch64_opnd_info *info,
1168
       aarch64_insn code, const aarch64_inst *inst,
1169
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1170
21.4k
{
1171
21.4k
  info->qualifier = get_expected_qualifier (inst, info->idx);
1172
21.4k
  if (info->qualifier == AARCH64_OPND_QLF_ERR)
1173
0
    return 0;
1174
1175
  /* Rn */
1176
21.4k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1177
1178
  /* simm9 */
1179
21.4k
  aarch64_insn imm = extract_fields (code, 0, 1, self->fields[1]);
1180
21.4k
  info->addr.offset.imm = sign_extend (imm, 8);
1181
21.4k
  if (extract_field (self->fields[2], code, 0) == 1) {
1182
0
    info->addr.writeback = 1;
1183
0
    info->addr.preind = 1;
1184
0
  }
1185
21.4k
  return true;
1186
21.4k
}
1187
1188
/* Decode the address operand for e.g.
1189
     STR <Qt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}].  */
1190
bool
1191
aarch64_ext_addr_regoff (const aarch64_operand *self ATTRIBUTE_UNUSED,
1192
       aarch64_opnd_info *info,
1193
       aarch64_insn code, const aarch64_inst *inst,
1194
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1195
63.7k
{
1196
63.7k
  aarch64_insn S, value;
1197
1198
  /* Rn */
1199
63.7k
  info->addr.base_regno = extract_field (FLD_Rn, code, 0);
1200
  /* Rm */
1201
63.7k
  info->addr.offset.regno = extract_field (FLD_Rm, code, 0);
1202
  /* option */
1203
63.7k
  value = extract_field (FLD_option, code, 0);
1204
63.7k
  info->shifter.kind =
1205
63.7k
    aarch64_get_operand_modifier_from_value (value, true /* extend_p */);
1206
  /* Fix-up the shifter kind; although the table-driven approach is
1207
     efficient, it is slightly inflexible, thus needing this fix-up.  */
1208
63.7k
  if (info->shifter.kind == AARCH64_MOD_UXTX)
1209
17.6k
    info->shifter.kind = AARCH64_MOD_LSL;
1210
  /* S */
1211
63.7k
  S = extract_field (FLD_S, code, 0);
1212
63.7k
  if (S == 0)
1213
12.8k
    {
1214
12.8k
      info->shifter.amount = 0;
1215
12.8k
      info->shifter.amount_present = 0;
1216
12.8k
    }
1217
50.9k
  else
1218
50.9k
    {
1219
50.9k
      int size;
1220
      /* Need information in other operand(s) to help achieve the decoding
1221
   from 'S' field.  */
1222
50.9k
      info->qualifier = get_expected_qualifier (inst, info->idx);
1223
50.9k
      if (info->qualifier == AARCH64_OPND_QLF_ERR)
1224
0
  return 0;
1225
      /* Get the size of the data element that is accessed, which may be
1226
   different from that of the source register size, e.g. in strb/ldrb.  */
1227
50.9k
      size = aarch64_get_qualifier_esize (info->qualifier);
1228
50.9k
      info->shifter.amount = get_logsz (size);
1229
50.9k
      info->shifter.amount_present = 1;
1230
50.9k
    }
1231
1232
63.7k
  return true;
1233
63.7k
}
1234
1235
/* Decode the address operand for e.g. LDRSW <Xt>, [<Xn|SP>], #<simm>.  */
1236
bool
1237
aarch64_ext_addr_simm (const aarch64_operand *self, aarch64_opnd_info *info,
1238
           aarch64_insn code, const aarch64_inst *inst,
1239
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1240
635k
{
1241
635k
  aarch64_insn imm;
1242
635k
  info->qualifier = get_expected_qualifier (inst, info->idx);
1243
635k
  if (info->qualifier == AARCH64_OPND_QLF_ERR)
1244
0
    return 0;
1245
1246
  /* Rn */
1247
635k
  info->addr.base_regno = extract_field (FLD_Rn, code, 0);
1248
  /* simm (imm9 or imm7)  */
1249
635k
  imm = extract_field (self->fields[0], code, 0);
1250
635k
  info->addr.offset.imm
1251
635k
    = sign_extend (imm, self->fields[0].width - 1);
1252
635k
  if (self->fields[0].width == 7
1253
110k
      || info->qualifier == AARCH64_OPND_QLF_imm_tag)
1254
    /* scaled immediate in ld/st pair instructions.  */
1255
532k
    info->addr.offset.imm *= aarch64_get_qualifier_esize (info->qualifier);
1256
  /* qualifier */
1257
635k
  if (inst->opcode->iclass == ldst_unscaled
1258
584k
      || inst->opcode->iclass == ldstnapair_offs
1259
454k
      || inst->opcode->iclass == ldstpair_off
1260
285k
      || inst->opcode->iclass == ldst_unpriv)
1261
362k
    info->addr.writeback = 0;
1262
273k
  else
1263
273k
    {
1264
      /* pre/post- index */
1265
273k
      info->addr.writeback = 1;
1266
273k
      if (extract_field (self->fields[1], code, 0) == 1)
1267
136k
  info->addr.preind = 1;
1268
136k
      else
1269
136k
  info->addr.postind = 1;
1270
273k
    }
1271
1272
635k
  return true;
1273
635k
}
1274
1275
/* Decode the address operand for e.g. LDRSW <Xt>, [<Xn|SP>{, #<simm>}].  */
1276
bool
1277
aarch64_ext_addr_uimm12 (const aarch64_operand *self, aarch64_opnd_info *info,
1278
       aarch64_insn code,
1279
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
1280
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1281
216k
{
1282
216k
  int shift;
1283
216k
  info->qualifier = get_expected_qualifier (inst, info->idx);
1284
216k
  if (info->qualifier == AARCH64_OPND_QLF_ERR)
1285
0
    return 0;
1286
216k
  shift = get_logsz (aarch64_get_qualifier_esize (info->qualifier));
1287
  /* Rn */
1288
216k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1289
  /* uimm12 */
1290
216k
  info->addr.offset.imm = extract_field (self->fields[1], code, 0) << shift;
1291
216k
  return true;
1292
216k
}
1293
1294
/* Decode the address operand for e.g. LDRAA <Xt>, [<Xn|SP>{, #<simm>}].  */
1295
bool
1296
aarch64_ext_addr_simm10 (const aarch64_operand *self, aarch64_opnd_info *info,
1297
       aarch64_insn code,
1298
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
1299
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1300
9.90k
{
1301
9.90k
  aarch64_insn imm;
1302
1303
9.90k
  info->qualifier = get_expected_qualifier (inst, info->idx);
1304
9.90k
  if (info->qualifier == AARCH64_OPND_QLF_ERR)
1305
0
    return 0;
1306
  /* Rn */
1307
9.90k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1308
  /* simm10 */
1309
9.90k
  imm = extract_fields (code, 0, 2, self->fields[1], self->fields[2]);
1310
9.90k
  info->addr.offset.imm = sign_extend (imm, 9) << 3;
1311
9.90k
  if (extract_field (self->fields[3], code, 0) == 1) {
1312
5.47k
    info->addr.writeback = 1;
1313
5.47k
    info->addr.preind = 1;
1314
5.47k
  }
1315
9.90k
  return true;
1316
9.90k
}
1317
1318
/* Decode the address operand for e.g.
1319
     LD1 {<Vt>.<T>, <Vt2>.<T>, <Vt3>.<T>}, [<Xn|SP>], <Xm|#<amount>>.  */
1320
bool
1321
aarch64_ext_simd_addr_post (const aarch64_operand *self ATTRIBUTE_UNUSED,
1322
          aarch64_opnd_info *info,
1323
          aarch64_insn code, const aarch64_inst *inst,
1324
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1325
18.2k
{
1326
  /* The opcode dependent area stores the number of elements in
1327
     each structure to be loaded/stored.  */
1328
18.2k
  int is_ld1r = get_opcode_dependent_value (inst->opcode) == 1;
1329
1330
  /* Rn */
1331
18.2k
  info->addr.base_regno = extract_field (FLD_Rn, code, 0);
1332
  /* Rm | #<amount>  */
1333
18.2k
  info->addr.offset.regno = extract_field (FLD_Rm, code, 0);
1334
18.2k
  if (info->addr.offset.regno == 31)
1335
2.74k
    {
1336
2.74k
      if (inst->opcode->operands[0] == AARCH64_OPND_LVt_AL)
1337
  /* Special handling of loading single structure to all lane.  */
1338
493
  info->addr.offset.imm = (is_ld1r ? 1
1339
493
         : inst->operands[0].reglist.num_regs)
1340
493
    * aarch64_get_qualifier_esize (inst->operands[0].qualifier);
1341
2.25k
      else
1342
2.25k
  info->addr.offset.imm = inst->operands[0].reglist.num_regs
1343
2.25k
    * aarch64_get_qualifier_esize (inst->operands[0].qualifier)
1344
2.25k
    * aarch64_get_qualifier_nelem (inst->operands[0].qualifier);
1345
2.74k
    }
1346
15.5k
  else
1347
15.5k
    info->addr.offset.is_reg = 1;
1348
18.2k
  info->addr.writeback = 1;
1349
1350
18.2k
  return true;
1351
18.2k
}
1352
1353
/* Decode the condition operand for e.g. CSEL <Xd>, <Xn>, <Xm>, <cond>.  */
1354
bool
1355
aarch64_ext_cond (const aarch64_operand *self ATTRIBUTE_UNUSED,
1356
      aarch64_opnd_info *info,
1357
      aarch64_insn code, const aarch64_inst *inst ATTRIBUTE_UNUSED,
1358
      aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1359
24.9k
{
1360
24.9k
  aarch64_insn value;
1361
  /* cond */
1362
24.9k
  value = extract_field (FLD_cond, code, 0);
1363
24.9k
  info->cond = get_cond_from_value (value);
1364
24.9k
  return true;
1365
24.9k
}
1366
1367
/* Decode the system register operand for e.g. MRS <Xt>, <systemreg>.  */
1368
bool
1369
aarch64_ext_sysreg (const aarch64_operand *self ATTRIBUTE_UNUSED,
1370
        aarch64_opnd_info *info,
1371
        aarch64_insn code,
1372
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
1373
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1374
13.0k
{
1375
  /* op0:op1:CRn:CRm:op2 */
1376
13.0k
  info->sysreg.value = extract_fields (code, 0, 5, FLD_op0, FLD_op1, FLD_CRn,
1377
13.0k
               FLD_CRm, FLD_op2);
1378
13.0k
  info->sysreg.flags = 0;
1379
1380
  /* If a system instruction, check which restrictions should be on the register
1381
     value during decoding, these will be enforced then.  */
1382
13.0k
  if (inst->opcode->iclass == ic_system)
1383
13.0k
    {
1384
      /* Check to see if it's read-only, else check if it's write only.
1385
   if it's both or unspecified don't care.  */
1386
13.0k
      if ((inst->opcode->flags & (F_SYS_READ | F_SYS_WRITE)) == F_SYS_READ)
1387
5.66k
  info->sysreg.flags = F_REG_READ;
1388
7.41k
      else if ((inst->opcode->flags & (F_SYS_READ | F_SYS_WRITE))
1389
7.41k
         == F_SYS_WRITE)
1390
7.41k
  info->sysreg.flags = F_REG_WRITE;
1391
13.0k
    }
1392
1393
13.0k
  return true;
1394
13.0k
}
1395
1396
/* Decode the PSTATE field operand for e.g. MSR <pstatefield>, #<imm>.  */
1397
bool
1398
aarch64_ext_pstatefield (const aarch64_operand *self ATTRIBUTE_UNUSED,
1399
       aarch64_opnd_info *info, aarch64_insn code,
1400
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
1401
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1402
244
{
1403
244
  int i;
1404
244
  aarch64_insn fld_crm = extract_field (FLD_CRm, code, 0);
1405
  /* op1:op2 */
1406
244
  info->pstatefield = extract_fields (code, 0, 2, FLD_op1, FLD_op2);
1407
1.10k
  for (i = 0; aarch64_pstatefields[i].name != NULL; ++i)
1408
1.08k
    if (aarch64_pstatefields[i].value == (aarch64_insn)info->pstatefield)
1409
286
      {
1410
        /* PSTATEFIELD name can be encoded partially in CRm[3:1].  */
1411
286
        uint32_t flags = aarch64_pstatefields[i].flags;
1412
286
        if ((flags & F_REG_IN_CRM)
1413
63
            && ((fld_crm & 0xe) != PSTATE_DECODE_CRM (flags)))
1414
63
          continue;
1415
223
        info->sysreg.flags = flags;
1416
223
        return true;
1417
286
      }
1418
  /* Reserved value in <pstatefield>.  */
1419
21
  return false;
1420
244
}
1421
1422
/* Decode the system instruction op operand for e.g. AT <at_op>, <Xt>.  */
1423
bool
1424
aarch64_ext_sysins_op (const aarch64_operand *self ATTRIBUTE_UNUSED,
1425
           aarch64_opnd_info *info,
1426
           aarch64_insn code,
1427
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
1428
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1429
20.6k
{
1430
20.6k
  int i;
1431
20.6k
  aarch64_insn value;
1432
20.6k
  const aarch64_sys_ins_reg *sysins_ops;
1433
  /* op0:op1:CRn:CRm:op2 */
1434
20.6k
  value = extract_fields (code, 0, 5,
1435
20.6k
        FLD_op0, FLD_op1, FLD_CRn,
1436
20.6k
        FLD_CRm, FLD_op2);
1437
1438
20.6k
  switch (info->type)
1439
20.6k
    {
1440
2.77k
    case AARCH64_OPND_GIC: sysins_ops = aarch64_sys_ins_gic; break;
1441
2.77k
    case AARCH64_OPND_GICR: sysins_ops = aarch64_sys_ins_gicr; break;
1442
722
    case AARCH64_OPND_GSB: sysins_ops = aarch64_sys_ins_gsb; break;
1443
2.42k
    case AARCH64_OPND_SYSREG_AT: sysins_ops = aarch64_sys_regs_at; break;
1444
2.49k
    case AARCH64_OPND_SYSREG_DC: sysins_ops = aarch64_sys_regs_dc; break;
1445
2.49k
    case AARCH64_OPND_SYSREG_IC: sysins_ops = aarch64_sys_regs_ic; break;
1446
2.69k
    case AARCH64_OPND_SYSREG_TLBI: sysins_ops = aarch64_sys_regs_tlbi; break;
1447
1.51k
    case AARCH64_OPND_SYSREG_TLBIP: sysins_ops = aarch64_sys_regs_tlbi; break;
1448
2.79k
    case AARCH64_OPND_SYSREG_PLBI: sysins_ops = aarch64_sys_regs_plbi; break;
1449
0
    case AARCH64_OPND_SYSREG_SR:
1450
0
  sysins_ops = aarch64_sys_regs_sr;
1451
   /* Let's remove op2 for rctx.  Refer to comments in the definition of
1452
      aarch64_sys_regs_sr[].  */
1453
0
  value = value & ~(0x7);
1454
0
  break;
1455
0
    default: return false;
1456
20.6k
    }
1457
1458
1.04M
  for (i = 0; sysins_ops[i].name != NULL; ++i)
1459
1.02M
    if (sysins_ops[i].value == value)
1460
1.15k
      {
1461
1.15k
  info->sysins_op = sysins_ops + i;
1462
1.15k
  DEBUG_TRACE ("%s found value: %x, has_xt: %d, i: %d.",
1463
1.15k
         info->sysins_op->name,
1464
1.15k
         (unsigned)info->sysins_op->value,
1465
1.15k
         aarch64_sys_ins_reg_has_xt (info->sysins_op), i);
1466
1.15k
  return true;
1467
1.15k
      }
1468
1469
19.5k
  return false;
1470
20.6k
}
1471
1472
/* Decode the memory barrier option operand for e.g. DMB <option>|#<imm>.  */
1473
1474
bool
1475
aarch64_ext_barrier (const aarch64_operand *self ATTRIBUTE_UNUSED,
1476
         aarch64_opnd_info *info,
1477
         aarch64_insn code,
1478
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
1479
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1480
396
{
1481
  /* CRm */
1482
396
  info->barrier = aarch64_barrier_options + extract_field (FLD_CRm, code, 0);
1483
396
  return true;
1484
396
}
1485
1486
/* Decode the memory barrier option operand for DSB <option>nXS|#<imm>.  */
1487
1488
bool
1489
aarch64_ext_barrier_dsb_nxs (const aarch64_operand *self ATTRIBUTE_UNUSED,
1490
         aarch64_opnd_info *info,
1491
         aarch64_insn code,
1492
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
1493
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1494
422
{
1495
  /* For the DSB nXS barrier variant immediate is encoded in 2-bit field.  */
1496
422
  aarch64_insn field = extract_field (FLD_CRm_dsb_nxs, code, 0);
1497
422
  info->barrier = aarch64_barrier_dsb_nxs_options + field;
1498
422
  return true;
1499
422
}
1500
1501
/* Decode the prefetch operation option operand for e.g.
1502
     PRFM <prfop>, [<Xn|SP>{, #<pimm>}].  */
1503
1504
bool
1505
aarch64_ext_prfop (const aarch64_operand *self ATTRIBUTE_UNUSED,
1506
       aarch64_opnd_info *info,
1507
       aarch64_insn code, const aarch64_inst *inst ATTRIBUTE_UNUSED,
1508
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1509
42.2k
{
1510
  /* prfop in Rt */
1511
42.2k
  info->prfop = aarch64_prfops + extract_field (FLD_Rt, code, 0);
1512
42.2k
  return true;
1513
42.2k
}
1514
1515
/* Decode the hint number for an alias taking an operand.  Set info->hint_option
1516
   to the matching name/value pair in aarch64_hint_options.  */
1517
1518
bool
1519
aarch64_ext_hint (const aarch64_operand *self ATTRIBUTE_UNUSED,
1520
      aarch64_opnd_info *info,
1521
      aarch64_insn code,
1522
      const aarch64_inst *inst ATTRIBUTE_UNUSED,
1523
      aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1524
351
{
1525
  /* CRm:op2.  */
1526
351
  unsigned hint_number;
1527
351
  int i;
1528
1529
351
  hint_number = extract_fields (code, 0, 2, FLD_CRm, FLD_op2);
1530
1531
2.74k
  for (i = 0; aarch64_hint_options[i].name != NULL; i++)
1532
2.74k
    {
1533
2.74k
      if (hint_number == aarch64_hint_options[i].value)
1534
351
  {
1535
351
    info->hint_option = &(aarch64_hint_options[i]);
1536
351
    return true;
1537
351
  }
1538
2.74k
    }
1539
1540
0
  return false;
1541
351
}
1542
1543
/* Decode the extended register operand for e.g.
1544
     STR <Qt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}].  */
1545
bool
1546
aarch64_ext_reg_extended (const aarch64_operand *self ATTRIBUTE_UNUSED,
1547
        aarch64_opnd_info *info,
1548
        aarch64_insn code,
1549
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
1550
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1551
47.4k
{
1552
47.4k
  aarch64_insn value;
1553
1554
  /* Rm */
1555
47.4k
  info->reg.regno = extract_field (FLD_Rm, code, 0);
1556
  /* option */
1557
47.4k
  value = extract_field (FLD_option, code, 0);
1558
47.4k
  info->shifter.kind =
1559
47.4k
    aarch64_get_operand_modifier_from_value (value, true /* extend_p */);
1560
  /* imm3 */
1561
47.4k
  info->shifter.amount = extract_field (FLD_imm3_10, code,  0);
1562
1563
  /* This makes the constraint checking happy.  */
1564
47.4k
  info->shifter.operator_present = 1;
1565
1566
  /* Assume inst->operands[0].qualifier has been resolved.  */
1567
47.4k
  assert (inst->operands[0].qualifier != AARCH64_OPND_QLF_UNKNOWN);
1568
47.4k
  info->qualifier = AARCH64_OPND_QLF_W;
1569
47.4k
  if (inst->operands[0].qualifier == AARCH64_OPND_QLF_X
1570
14.0k
      && (info->shifter.kind == AARCH64_MOD_UXTX
1571
11.5k
    || info->shifter.kind == AARCH64_MOD_SXTX))
1572
5.18k
    info->qualifier = AARCH64_OPND_QLF_X;
1573
1574
47.4k
  return true;
1575
47.4k
}
1576
1577
/* Decode the shifted register operand for e.g.
1578
     SUBS <Xd>, <Xn>, <Xm> {, <shift> #<amount>}.  */
1579
bool
1580
aarch64_ext_reg_shifted (const aarch64_operand *self ATTRIBUTE_UNUSED,
1581
       aarch64_opnd_info *info,
1582
       aarch64_insn code,
1583
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
1584
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1585
494k
{
1586
494k
  aarch64_insn value;
1587
1588
  /* Rm */
1589
494k
  info->reg.regno = extract_field (FLD_Rm, code, 0);
1590
  /* shift */
1591
494k
  value = extract_field (FLD_shift, code, 0);
1592
494k
  info->shifter.kind =
1593
494k
    aarch64_get_operand_modifier_from_value (value, false /* extend_p */);
1594
494k
  if (info->shifter.kind == AARCH64_MOD_ROR
1595
99.4k
      && inst->opcode->iclass != log_shift)
1596
    /* ROR is not available for the shifted register operand in arithmetic
1597
       instructions.  */
1598
21.4k
    return false;
1599
  /* imm6 */
1600
472k
  info->shifter.amount = extract_field (FLD_imm6_10, code,  0);
1601
1602
  /* This makes the constraint checking happy.  */
1603
472k
  info->shifter.operator_present = 1;
1604
1605
472k
  return true;
1606
494k
}
1607
1608
/* Decode the LSL-shifted register operand for e.g.
1609
     ADDPT <Xd|SP>, <Xn|SP>, <Xm>{, LSL #<amount>}.  */
1610
bool
1611
aarch64_ext_reg_lsl_shifted (const aarch64_operand *self ATTRIBUTE_UNUSED,
1612
           aarch64_opnd_info *info,
1613
           aarch64_insn code,
1614
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
1615
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1616
773
{
1617
  /* Rm */
1618
773
  info->reg.regno = extract_field (FLD_Rm, code, 0);
1619
  /* imm3 */
1620
773
  info->shifter.kind = AARCH64_MOD_LSL;
1621
773
  info->shifter.amount = extract_field (FLD_imm3_10, code,  0);
1622
773
  return true;
1623
773
}
1624
1625
/* Decode an SVE address [<base>, #<offset>*<factor>, MUL VL],
1626
   where <offset> is given by the OFFSET parameter and where <factor> is
1627
   1 plus SELF's operand-dependent value.  fields[0] specifies the field
1628
   that holds <base>.  */
1629
static bool
1630
aarch64_ext_sve_addr_reg_mul_vl (const aarch64_operand *self,
1631
         aarch64_opnd_info *info, aarch64_insn code,
1632
         int64_t offset)
1633
91.5k
{
1634
91.5k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1635
91.5k
  info->addr.offset.imm = offset * (1 + get_operand_specific_data (self));
1636
91.5k
  info->addr.offset.is_reg = false;
1637
91.5k
  info->addr.writeback = false;
1638
91.5k
  info->addr.preind = true;
1639
91.5k
  if (offset != 0)
1640
85.9k
    info->shifter.kind = AARCH64_MOD_MUL_VL;
1641
91.5k
  info->shifter.amount = 1;
1642
91.5k
  info->shifter.operator_present = (info->addr.offset.imm != 0);
1643
91.5k
  info->shifter.amount_present = false;
1644
91.5k
  return true;
1645
91.5k
}
1646
1647
/* Decode an SVE address [<base>, #<simm4>*<factor>, MUL VL],
1648
   where <simm4> is a 4-bit signed value and where <factor> is 1 plus
1649
   SELF's operand-dependent value.  fields[0] specifies the field that
1650
   holds <base>.  <simm4> is encoded in the SVE_imm4 field.  */
1651
bool
1652
aarch64_ext_sve_addr_ri_s4xvl (const aarch64_operand *self,
1653
             aarch64_opnd_info *info, aarch64_insn code,
1654
             const aarch64_inst *inst ATTRIBUTE_UNUSED,
1655
             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1656
83.8k
{
1657
83.8k
  int offset;
1658
1659
83.8k
  offset = extract_field (FLD_SVE_imm4, code, 0);
1660
83.8k
  offset = ((offset + 8) & 15) - 8;
1661
83.8k
  return aarch64_ext_sve_addr_reg_mul_vl (self, info, code, offset);
1662
83.8k
}
1663
1664
/* Decode an SVE address [<base>, #<simm6>*<factor>, MUL VL],
1665
   where <simm6> is a 6-bit signed value and where <factor> is 1 plus
1666
   SELF's operand-dependent value.  fields[0] specifies the field that
1667
   holds <base>.  <simm6> is encoded in the SVE_imm6 field.  */
1668
bool
1669
aarch64_ext_sve_addr_ri_s6xvl (const aarch64_operand *self,
1670
             aarch64_opnd_info *info, aarch64_insn code,
1671
             const aarch64_inst *inst ATTRIBUTE_UNUSED,
1672
             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1673
2.71k
{
1674
2.71k
  int offset;
1675
1676
2.71k
  offset = extract_field (FLD_SVE_imm6, code, 0);
1677
2.71k
  offset = (((offset + 32) & 63) - 32);
1678
2.71k
  return aarch64_ext_sve_addr_reg_mul_vl (self, info, code, offset);
1679
2.71k
}
1680
1681
/* Decode an SVE address [<base>, #<simm9>*<factor>, MUL VL],
1682
   where <simm9> is a 9-bit signed value and where <factor> is 1 plus
1683
   SELF's operand-dependent value.  fields[0] specifies the field that
1684
   holds <base>.  <simm9> is encoded in the concatenation of the SVE_imm6
1685
   and imm3 fields, with imm3 being the less-significant part.  */
1686
bool
1687
aarch64_ext_sve_addr_ri_s9xvl (const aarch64_operand *self,
1688
             aarch64_opnd_info *info,
1689
             aarch64_insn code,
1690
             const aarch64_inst *inst ATTRIBUTE_UNUSED,
1691
             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1692
5.06k
{
1693
5.06k
  int offset;
1694
1695
5.06k
  offset = extract_fields (code, 0, 2, FLD_SVE_imm6, FLD_imm3_10);
1696
5.06k
  offset = (((offset + 256) & 511) - 256);
1697
5.06k
  return aarch64_ext_sve_addr_reg_mul_vl (self, info, code, offset);
1698
5.06k
}
1699
1700
/* Decode an SVE address [<base>, #<offset> << <shift>], where <offset>
1701
   is given by the OFFSET parameter and where <shift> is SELF's operand-
1702
   dependent value.  fields[0] specifies the base register field <base>.  */
1703
static bool
1704
aarch64_ext_sve_addr_reg_imm (const aarch64_operand *self,
1705
            aarch64_opnd_info *info, aarch64_insn code,
1706
            int64_t offset)
1707
30.0k
{
1708
30.0k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1709
30.0k
  info->addr.offset.imm = offset * (1 << get_operand_specific_data (self));
1710
30.0k
  info->addr.offset.is_reg = false;
1711
30.0k
  info->addr.writeback = false;
1712
30.0k
  info->addr.preind = true;
1713
30.0k
  info->shifter.operator_present = false;
1714
30.0k
  info->shifter.amount_present = false;
1715
30.0k
  return true;
1716
30.0k
}
1717
1718
/* Decode an SVE address [X<n>, #<SVE_imm4> << <shift>], where <SVE_imm4>
1719
   is a 4-bit signed number and where <shift> is SELF's operand-dependent
1720
   value.  fields[0] specifies the base register field.  */
1721
bool
1722
aarch64_ext_sve_addr_ri_s4 (const aarch64_operand *self,
1723
          aarch64_opnd_info *info, aarch64_insn code,
1724
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
1725
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1726
3.30k
{
1727
3.30k
  int offset = sign_extend (extract_field (FLD_SVE_imm4, code, 0), 3);
1728
3.30k
  return aarch64_ext_sve_addr_reg_imm (self, info, code, offset);
1729
3.30k
}
1730
1731
/* Decode an SVE address [X<n>, #<SVE_imm6> << <shift>], where <SVE_imm6>
1732
   is a 6-bit unsigned number and where <shift> is SELF's operand-dependent
1733
   value.  fields[0] specifies the base register field.  */
1734
bool
1735
aarch64_ext_sve_addr_ri_u6 (const aarch64_operand *self,
1736
          aarch64_opnd_info *info, aarch64_insn code,
1737
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
1738
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1739
12.4k
{
1740
12.4k
  int offset = extract_field (FLD_SVE_imm6, code, 0);
1741
12.4k
  return aarch64_ext_sve_addr_reg_imm (self, info, code, offset);
1742
12.4k
}
1743
1744
/* Decode an SVE address [X<n>, X<m>{, LSL #<shift>}], where <shift>
1745
   is SELF's operand-dependent value.  fields[0] specifies the base
1746
   register field and fields[1] specifies the offset register field.  */
1747
bool
1748
aarch64_ext_sve_addr_rr_lsl (const aarch64_operand *self,
1749
           aarch64_opnd_info *info, aarch64_insn code,
1750
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
1751
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1752
157k
{
1753
157k
  int index_regno;
1754
1755
157k
  index_regno = extract_field (self->fields[1], code, 0);
1756
157k
  if (index_regno == 31 && (self->flags & OPD_F_NO_ZR) != 0)
1757
1.46k
    return false;
1758
1759
156k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1760
156k
  info->addr.offset.regno = index_regno;
1761
156k
  info->addr.offset.is_reg = true;
1762
156k
  info->addr.writeback = false;
1763
156k
  info->addr.preind = true;
1764
156k
  info->shifter.kind = AARCH64_MOD_LSL;
1765
156k
  info->shifter.amount = get_operand_specific_data (self);
1766
156k
  info->shifter.operator_present = (info->shifter.amount != 0);
1767
156k
  info->shifter.amount_present = (info->shifter.amount != 0);
1768
156k
  return true;
1769
157k
}
1770
1771
/* Decode an SVE address [X<n>, Z<m>.<T>, (S|U)XTW {#<shift>}], where
1772
   <shift> is SELF's operand-dependent value.  fields[0] specifies the
1773
   base register field, fields[1] specifies the offset register field and
1774
   fields[2] is a single-bit field that selects SXTW over UXTW.  */
1775
bool
1776
aarch64_ext_sve_addr_rz_xtw (const aarch64_operand *self,
1777
           aarch64_opnd_info *info, aarch64_insn code,
1778
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
1779
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1780
64.7k
{
1781
64.7k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1782
64.7k
  info->addr.offset.regno = extract_field (self->fields[1], code, 0);
1783
64.7k
  info->addr.offset.is_reg = true;
1784
64.7k
  info->addr.writeback = false;
1785
64.7k
  info->addr.preind = true;
1786
64.7k
  if (extract_field (self->fields[2], code, 0))
1787
31.2k
    info->shifter.kind = AARCH64_MOD_SXTW;
1788
33.5k
  else
1789
33.5k
    info->shifter.kind = AARCH64_MOD_UXTW;
1790
64.7k
  info->shifter.amount = get_operand_specific_data (self);
1791
64.7k
  info->shifter.operator_present = true;
1792
64.7k
  info->shifter.amount_present = (info->shifter.amount != 0);
1793
64.7k
  return true;
1794
64.7k
}
1795
1796
/* Decode an SVE address [Z<n>.<T>, #<imm5> << <shift>], where <imm5> is a
1797
   5-bit unsigned number and where <shift> is SELF's operand-dependent value.
1798
   fields[0] specifies the base register field.  */
1799
bool
1800
aarch64_ext_sve_addr_zi_u5 (const aarch64_operand *self,
1801
          aarch64_opnd_info *info, aarch64_insn code,
1802
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
1803
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1804
14.3k
{
1805
14.3k
  int offset = extract_field (FLD_imm5, code, 0);
1806
14.3k
  return aarch64_ext_sve_addr_reg_imm (self, info, code, offset);
1807
14.3k
}
1808
1809
/* Decode an SVE address [Z<n>.<T>, Z<m>.<T>{, <modifier> {#<msz>}}],
1810
   where <modifier> is given by KIND and where <msz> is a 2-bit unsigned
1811
   number.  fields[0] specifies the base register field and fields[1]
1812
   specifies the offset register field.  */
1813
static bool
1814
aarch64_ext_sve_addr_zz (const aarch64_operand *self, aarch64_opnd_info *info,
1815
       aarch64_insn code, enum aarch64_modifier_kind kind)
1816
2.07k
{
1817
2.07k
  info->addr.base_regno = extract_field (self->fields[0], code, 0);
1818
2.07k
  info->addr.offset.regno = extract_field (self->fields[1], code, 0);
1819
2.07k
  info->addr.offset.is_reg = true;
1820
2.07k
  info->addr.writeback = false;
1821
2.07k
  info->addr.preind = true;
1822
2.07k
  info->shifter.kind = kind;
1823
2.07k
  info->shifter.amount = extract_field (FLD_SVE_msz, code, 0);
1824
2.07k
  info->shifter.operator_present = (kind != AARCH64_MOD_LSL
1825
920
            || info->shifter.amount != 0);
1826
2.07k
  info->shifter.amount_present = (info->shifter.amount != 0);
1827
2.07k
  return true;
1828
2.07k
}
1829
1830
/* Decode an SVE address [Z<n>.<T>, Z<m>.<T>{, LSL #<msz>}], where
1831
   <msz> is a 2-bit unsigned number.  fields[0] specifies the base register
1832
   field and fields[1] specifies the offset register field.  */
1833
bool
1834
aarch64_ext_sve_addr_zz_lsl (const aarch64_operand *self,
1835
           aarch64_opnd_info *info, aarch64_insn code,
1836
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
1837
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1838
920
{
1839
920
  return aarch64_ext_sve_addr_zz (self, info, code, AARCH64_MOD_LSL);
1840
920
}
1841
1842
/* Decode an SVE address [Z<n>.<T>, Z<m>.<T>, SXTW {#<msz>}], where
1843
   <msz> is a 2-bit unsigned number.  fields[0] specifies the base register
1844
   field and fields[1] specifies the offset register field.  */
1845
bool
1846
aarch64_ext_sve_addr_zz_sxtw (const aarch64_operand *self,
1847
            aarch64_opnd_info *info, aarch64_insn code,
1848
            const aarch64_inst *inst ATTRIBUTE_UNUSED,
1849
            aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1850
289
{
1851
289
  return aarch64_ext_sve_addr_zz (self, info, code, AARCH64_MOD_SXTW);
1852
289
}
1853
1854
/* Decode an SVE address [Z<n>.<T>, Z<m>.<T>, UXTW {#<msz>}], where
1855
   <msz> is a 2-bit unsigned number.  fields[0] specifies the base register
1856
   field and fields[1] specifies the offset register field.  */
1857
bool
1858
aarch64_ext_sve_addr_zz_uxtw (const aarch64_operand *self,
1859
            aarch64_opnd_info *info, aarch64_insn code,
1860
            const aarch64_inst *inst ATTRIBUTE_UNUSED,
1861
            aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1862
862
{
1863
862
  return aarch64_ext_sve_addr_zz (self, info, code, AARCH64_MOD_UXTW);
1864
862
}
1865
1866
/* Finish decoding an SVE arithmetic immediate, given that INFO already
1867
   has the raw field value and that the low 8 bits decode to VALUE.  */
1868
static bool
1869
decode_sve_aimm (aarch64_opnd_info *info, int64_t value)
1870
16.7k
{
1871
16.7k
  info->shifter.kind = AARCH64_MOD_LSL;
1872
16.7k
  info->shifter.amount = 0;
1873
16.7k
  if (info->imm.value & 0x100)
1874
5.38k
    {
1875
5.38k
      if (value == 0)
1876
  /* Decode 0x100 as #0, LSL #8.  */
1877
477
  info->shifter.amount = 8;
1878
4.90k
      else
1879
4.90k
  value *= 256;
1880
5.38k
    }
1881
16.7k
  info->shifter.operator_present = (info->shifter.amount != 0);
1882
16.7k
  info->shifter.amount_present = (info->shifter.amount != 0);
1883
16.7k
  info->imm.value = value;
1884
16.7k
  return true;
1885
16.7k
}
1886
1887
/* Decode an SVE ADD/SUB immediate.  */
1888
bool
1889
aarch64_ext_sve_aimm (const aarch64_operand *self,
1890
          aarch64_opnd_info *info, const aarch64_insn code,
1891
          const aarch64_inst *inst,
1892
          aarch64_operand_error *errors)
1893
2.54k
{
1894
2.54k
  return (aarch64_ext_imm (self, info, code, inst, errors)
1895
2.54k
    && decode_sve_aimm (info, (uint8_t) info->imm.value));
1896
2.54k
}
1897
1898
bool
1899
aarch64_ext_sve_aligned_reglist (const aarch64_operand *self,
1900
         aarch64_opnd_info *info, aarch64_insn code,
1901
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
1902
         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1903
53.3k
{
1904
53.3k
  unsigned int num_regs = get_operand_specific_data (self);
1905
53.3k
  info->reglist.first_regno = extract_all_fields (self, code);
1906
53.3k
  info->reglist.num_regs = num_regs;
1907
53.3k
  info->reglist.stride = 1;
1908
53.3k
  return true;
1909
53.3k
}
1910
1911
/* Decode an SVE CPY/DUP immediate.  */
1912
bool
1913
aarch64_ext_sve_asimm (const aarch64_operand *self,
1914
           aarch64_opnd_info *info, const aarch64_insn code,
1915
           const aarch64_inst *inst,
1916
           aarch64_operand_error *errors)
1917
14.1k
{
1918
14.1k
  return (aarch64_ext_imm (self, info, code, inst, errors)
1919
14.1k
    && decode_sve_aimm (info, (int8_t) info->imm.value));
1920
14.1k
}
1921
1922
/* Decode a single-bit immediate that selects between #0.5 and #1.0.
1923
   The fields array specifies which field to use.  */
1924
bool
1925
aarch64_ext_sve_float_half_one (const aarch64_operand *self,
1926
        aarch64_opnd_info *info, aarch64_insn code,
1927
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
1928
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1929
69
{
1930
69
  if (extract_field (self->fields[0], code, 0))
1931
23
    info->imm.value = 0x3f800000;
1932
46
  else
1933
46
    info->imm.value = 0x3f000000;
1934
69
  info->imm.is_fp = true;
1935
69
  return true;
1936
69
}
1937
1938
/* Decode a single-bit immediate that selects between #0.5 and #2.0.
1939
   The fields array specifies which field to use.  */
1940
bool
1941
aarch64_ext_sve_float_half_two (const aarch64_operand *self,
1942
        aarch64_opnd_info *info, aarch64_insn code,
1943
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
1944
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1945
229
{
1946
229
  if (extract_field (self->fields[0], code, 0))
1947
97
    info->imm.value = 0x40000000;
1948
132
  else
1949
132
    info->imm.value = 0x3f000000;
1950
229
  info->imm.is_fp = true;
1951
229
  return true;
1952
229
}
1953
1954
/* Decode a single-bit immediate that selects between #0.0 and #1.0.
1955
   The fields array specifies which field to use.  */
1956
bool
1957
aarch64_ext_sve_float_zero_one (const aarch64_operand *self,
1958
        aarch64_opnd_info *info, aarch64_insn code,
1959
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
1960
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1961
1.07k
{
1962
1.07k
  if (extract_field (self->fields[0], code, 0))
1963
113
    info->imm.value = 0x3f800000;
1964
962
  else
1965
962
    info->imm.value = 0x0;
1966
1.07k
  info->imm.is_fp = true;
1967
1.07k
  return true;
1968
1.07k
}
1969
1970
/* Decode SME instruction such as MOVZA ZA tile slice to vector.  */
1971
bool
1972
aarch64_ext_sme_za_tile_to_vec (const aarch64_operand *self,
1973
        aarch64_opnd_info *info, aarch64_insn code,
1974
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
1975
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
1976
770
{
1977
770
  aarch64_insn Qsize;   /* fields Q:S:size.  */
1978
770
  int fld_v = extract_field (self->fields[0], code, 0);
1979
770
  int fld_rv = extract_field (self->fields[1], code, 0);
1980
770
  int fld_zan_imm =  extract_field (FLD_imm4_5, code, 0);
1981
1982
770
  Qsize = extract_fields (inst->value, 0, 2, FLD_SME_size_22, FLD_SME_Q);
1983
770
  switch (Qsize)
1984
770
    {
1985
207
    case 0x0:
1986
207
      info->qualifier = AARCH64_OPND_QLF_S_B;
1987
207
      info->indexed_za.regno = 0;
1988
207
      info->indexed_za.index.imm = fld_zan_imm;
1989
207
      break;
1990
297
    case 0x2:
1991
297
      info->qualifier = AARCH64_OPND_QLF_S_H;
1992
297
      info->indexed_za.regno = fld_zan_imm >> 3;
1993
297
      info->indexed_za.index.imm = fld_zan_imm & 0x07;
1994
297
      break;
1995
46
    case 0x4:
1996
46
      info->qualifier = AARCH64_OPND_QLF_S_S;
1997
46
      info->indexed_za.regno = fld_zan_imm >> 2;
1998
46
      info->indexed_za.index.imm = fld_zan_imm & 0x03;
1999
46
      break;
2000
125
    case 0x6:
2001
125
      info->qualifier = AARCH64_OPND_QLF_S_D;
2002
125
      info->indexed_za.regno = fld_zan_imm >> 1;
2003
125
      info->indexed_za.index.imm = fld_zan_imm & 0x01;
2004
125
      break;
2005
95
    case 0x7:
2006
95
      info->qualifier = AARCH64_OPND_QLF_S_Q;
2007
95
      info->indexed_za.regno = fld_zan_imm;
2008
95
      break;
2009
0
    default:
2010
0
      return false;
2011
770
    }
2012
2013
770
  info->indexed_za.index.regno = fld_rv + 12;
2014
770
  info->indexed_za.v = fld_v;
2015
2016
770
  return true;
2017
770
}
2018
2019
/* Decode ZA tile vector, vector indicator, vector selector, qualifier and
2020
   immediate on numerous SME instruction fields such as MOVA.  */
2021
bool
2022
aarch64_ext_sme_za_hv_tiles (const aarch64_operand *self,
2023
                             aarch64_opnd_info *info, aarch64_insn code,
2024
                             const aarch64_inst *inst ATTRIBUTE_UNUSED,
2025
                             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2026
113k
{
2027
113k
  int fld_size = extract_field (self->fields[0], code, 0);
2028
113k
  int fld_q = extract_field (self->fields[1], code, 0);
2029
113k
  int fld_v = extract_field (self->fields[2], code, 0);
2030
113k
  int fld_rv = extract_field (self->fields[3], code, 0);
2031
113k
  int fld_zan_imm = extract_field (self->fields[4], code, 0);
2032
2033
  /* Deduce qualifier encoded in size and Q fields.  */
2034
113k
  if (fld_size == 0)
2035
15.5k
    {
2036
15.5k
      info->indexed_za.regno = 0;
2037
15.5k
      info->indexed_za.index.imm = fld_zan_imm;
2038
15.5k
    }
2039
98.0k
  else if (fld_size == 1)
2040
4.03k
    {
2041
4.03k
      info->indexed_za.regno = fld_zan_imm >> 3;
2042
4.03k
      info->indexed_za.index.imm = fld_zan_imm & 0x07;
2043
4.03k
    }
2044
93.9k
  else if (fld_size == 2)
2045
4.86k
    {
2046
4.86k
      info->indexed_za.regno = fld_zan_imm >> 2;
2047
4.86k
      info->indexed_za.index.imm = fld_zan_imm & 0x03;
2048
4.86k
    }
2049
89.1k
  else if (fld_size == 3 && fld_q == 0)
2050
73.1k
    {
2051
73.1k
      info->indexed_za.regno = fld_zan_imm >> 1;
2052
73.1k
      info->indexed_za.index.imm = fld_zan_imm & 0x01;
2053
73.1k
    }
2054
15.9k
  else if (fld_size == 3 && fld_q == 1)
2055
15.9k
    {
2056
15.9k
      info->indexed_za.regno = fld_zan_imm;
2057
15.9k
      info->indexed_za.index.imm = 0;
2058
15.9k
    }
2059
0
  else
2060
0
    return false;
2061
2062
113k
  info->indexed_za.index.regno = fld_rv + 12;
2063
113k
  info->indexed_za.v = fld_v;
2064
2065
113k
  return true;
2066
113k
}
2067
2068
bool
2069
aarch64_ext_sme_za_hv_tiles_range (const aarch64_operand *self,
2070
           aarch64_opnd_info *info, aarch64_insn code,
2071
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
2072
           aarch64_operand_error *errors
2073
             ATTRIBUTE_UNUSED)
2074
6.21k
{
2075
6.21k
  int ebytes = aarch64_get_qualifier_esize (info->qualifier);
2076
6.21k
  int range_size = get_opcode_dependent_value (inst->opcode);
2077
6.21k
  int fld_v = extract_field (self->fields[0], code, 0);
2078
6.21k
  int fld_rv = extract_field (self->fields[1], code, 0);
2079
6.21k
  int fld_zan_imm = extract_field (self->fields[2], code, 0);
2080
6.21k
  int max_value = 16 / range_size / ebytes;
2081
2082
6.21k
  if (max_value == 0)
2083
150
    max_value = 1;
2084
2085
6.21k
  int regno = fld_zan_imm / max_value;
2086
6.21k
  if (regno >= ebytes)
2087
118
    return false;
2088
2089
6.09k
  info->indexed_za.regno = regno;
2090
6.09k
  info->indexed_za.index.imm = (fld_zan_imm % max_value) * range_size;
2091
6.09k
  info->indexed_za.index.countm1 = range_size - 1;
2092
6.09k
  info->indexed_za.index.regno = fld_rv + 12;
2093
6.09k
  info->indexed_za.v = fld_v;
2094
2095
6.09k
  return true;
2096
6.21k
}
2097
2098
/* Decode in SME instruction ZERO list of up to eight 64-bit element tile names
2099
   separated by commas, encoded in the "imm8" field.
2100
2101
   For programmer convenience an assembler must also accept the names of
2102
   32-bit, 16-bit and 8-bit element tiles which are converted into the
2103
   corresponding set of 64-bit element tiles.
2104
*/
2105
bool
2106
aarch64_ext_sme_za_list (const aarch64_operand *self,
2107
                         aarch64_opnd_info *info, aarch64_insn code,
2108
                         const aarch64_inst *inst ATTRIBUTE_UNUSED,
2109
                         aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2110
0
{
2111
0
  int mask = extract_field (self->fields[0], code, 0);
2112
0
  info->imm.value = mask;
2113
0
  return true;
2114
0
}
2115
2116
/* Decode ZA array vector select register (Rv field), optional vector and
2117
   memory offset (imm4_11 field).
2118
*/
2119
bool
2120
aarch64_ext_sme_za_array (const aarch64_operand *self,
2121
                          aarch64_opnd_info *info, aarch64_insn code,
2122
                          const aarch64_inst *inst,
2123
                          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2124
49.0k
{
2125
49.0k
  int regno = extract_field (self->fields[0], code, 0);
2126
49.0k
  if (info->type == AARCH64_OPND_SME_ZA_array_off4)
2127
951
    regno += 12;
2128
48.0k
  else
2129
48.0k
    regno += 8;
2130
49.0k
  int imm = extract_field (self->fields[1], code, 0);
2131
49.0k
  int num_offsets = get_operand_specific_data (self);
2132
49.0k
  if (num_offsets == 0)
2133
11.4k
    num_offsets = 1;
2134
49.0k
  info->indexed_za.index.regno = regno;
2135
49.0k
  info->indexed_za.index.imm = imm * num_offsets;
2136
49.0k
  info->indexed_za.index.countm1 = num_offsets - 1;
2137
49.0k
  info->indexed_za.group_size = get_opcode_dependent_value (inst->opcode);
2138
49.0k
  return true;
2139
49.0k
}
2140
2141
/* Decode two ZA tile slice (V, Rv, off3| ZAn ,off2 | ZAn, ol| ZAn) feilds.  */
2142
bool
2143
aarch64_ext_sme_za_vrs1 (const aarch64_operand *self,
2144
        aarch64_opnd_info *info, aarch64_insn code,
2145
        const aarch64_inst *inst,
2146
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2147
544
{
2148
544
  int v = extract_field (self->fields[0], code, 0);
2149
544
  int regno = 12 + extract_field (self->fields[1], code, 0);
2150
544
  int imm, za_reg, num_offset = 2;
2151
2152
544
  switch (info->qualifier)
2153
544
    {
2154
198
    case AARCH64_OPND_QLF_S_B:
2155
198
      imm = extract_field (self->fields[2], code, 0);
2156
198
      info->indexed_za.index.imm = imm * num_offset;
2157
198
      break;
2158
50
    case AARCH64_OPND_QLF_S_H:
2159
81
    case AARCH64_OPND_QLF_S_S:
2160
81
      za_reg = extract_field (self->fields[2], code, 0);
2161
81
      imm = extract_field (self->fields[3], code, 0);
2162
81
      info->indexed_za.index.imm = imm * num_offset;
2163
81
      info->indexed_za.regno = za_reg;
2164
81
      break;
2165
265
    case AARCH64_OPND_QLF_S_D:
2166
265
      za_reg = extract_field (self->fields[2], code, 0);
2167
265
      info->indexed_za.regno = za_reg;
2168
265
      break;
2169
0
    default:
2170
0
      return false;
2171
544
    }
2172
2173
544
  info->indexed_za.index.regno = regno;
2174
544
  info->indexed_za.index.countm1 = num_offset - 1;
2175
544
  info->indexed_za.v = v;
2176
544
  info->indexed_za.group_size = get_opcode_dependent_value (inst->opcode);
2177
544
  return true;
2178
544
}
2179
2180
/* Decode four ZA tile slice (V, Rv, off3| ZAn ,off2 | ZAn, ol| ZAn) feilds.  */
2181
bool
2182
aarch64_ext_sme_za_vrs2 (const aarch64_operand *self,
2183
        aarch64_opnd_info *info, aarch64_insn code,
2184
        const aarch64_inst *inst,
2185
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2186
702
{
2187
702
  int v = extract_field (self->fields[0], code, 0);
2188
702
  int regno = 12 + extract_field (self->fields[1], code, 0);
2189
702
  int imm, za_reg, num_offset =4;
2190
2191
702
  switch (info->qualifier)
2192
702
    {
2193
200
    case AARCH64_OPND_QLF_S_B:
2194
200
      imm = extract_field (self->fields[2], code, 0);
2195
200
      info->indexed_za.index.imm = imm * num_offset;
2196
200
      break;
2197
61
    case AARCH64_OPND_QLF_S_H:
2198
61
      za_reg = extract_field (self->fields[2], code, 0);
2199
61
      imm = extract_field (self->fields[3], code, 0);
2200
61
      info->indexed_za.index.imm = imm * num_offset;
2201
61
      info->indexed_za.regno = za_reg;
2202
61
      break;
2203
201
    case AARCH64_OPND_QLF_S_S:
2204
441
    case AARCH64_OPND_QLF_S_D:
2205
441
      za_reg = extract_field (self->fields[2], code, 0);
2206
441
      info->indexed_za.regno = za_reg;
2207
441
      break;
2208
0
    default:
2209
0
      return false;
2210
702
    }
2211
2212
702
  info->indexed_za.index.regno = regno;
2213
702
  info->indexed_za.index.countm1 = num_offset - 1;
2214
702
  info->indexed_za.v = v;
2215
702
  info->indexed_za.group_size = get_opcode_dependent_value (inst->opcode);
2216
702
  return true;
2217
702
}
2218
2219
bool
2220
aarch64_ext_sme_addr_ri_u4xvl (const aarch64_operand *self,
2221
                               aarch64_opnd_info *info, aarch64_insn code,
2222
                               const aarch64_inst *inst ATTRIBUTE_UNUSED,
2223
                               aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2224
951
{
2225
951
  int regno = extract_field (self->fields[0], code, 0);
2226
951
  int imm = extract_field (self->fields[1], code, 0);
2227
951
  info->addr.base_regno = regno;
2228
951
  info->addr.offset.imm = imm;
2229
  /* MUL VL operator is always present for this operand.  */
2230
951
  info->shifter.kind = AARCH64_MOD_MUL_VL;
2231
951
  info->shifter.operator_present = (imm != 0);
2232
951
  return true;
2233
951
}
2234
2235
/* Decode {SM|ZA} filed for SMSTART and SMSTOP instructions.  */
2236
bool
2237
aarch64_ext_sme_sm_za (const aarch64_operand *self,
2238
                       aarch64_opnd_info *info, aarch64_insn code,
2239
                       const aarch64_inst *inst ATTRIBUTE_UNUSED,
2240
                       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2241
21
{
2242
21
  info->pstatefield = 0x1b;
2243
21
  aarch64_insn fld_crm = extract_field (self->fields[0], code, 0);
2244
21
  fld_crm >>= 1;    /* CRm[3:1].  */
2245
2246
21
  if (fld_crm == 0x1)
2247
0
    info->reg.regno = 's';
2248
21
  else if (fld_crm == 0x2)
2249
0
    info->reg.regno = 'z';
2250
21
  else
2251
21
    return false;
2252
2253
0
  return true;
2254
21
}
2255
2256
bool
2257
aarch64_ext_sme_pred_reg_with_index (const aarch64_operand *self,
2258
             aarch64_opnd_info *info, aarch64_insn code,
2259
             const aarch64_inst *inst ATTRIBUTE_UNUSED,
2260
             aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2261
6.93k
{
2262
6.93k
  aarch64_insn fld_rm = extract_field (self->fields[0], code, 0);
2263
6.93k
  aarch64_insn fld_pn = extract_field (self->fields[1], code, 0);
2264
6.93k
  aarch64_insn fld_i1 = extract_field (self->fields[2], code, 0);
2265
6.93k
  aarch64_insn fld_tszh = extract_field (self->fields[3], code, 0);
2266
6.93k
  aarch64_insn fld_tszl = extract_field (self->fields[4], code, 0);
2267
6.93k
  int imm;
2268
2269
6.93k
  info->indexed_za.regno = fld_pn;
2270
6.93k
  info->indexed_za.index.regno = fld_rm + 12;
2271
2272
6.93k
  if (fld_tszl & 0x1)
2273
2.11k
    imm = (fld_i1 << 3) | (fld_tszh << 2) | (fld_tszl >> 1);
2274
4.82k
  else if (fld_tszl & 0x2)
2275
3.36k
    imm = (fld_i1 << 2) | (fld_tszh << 1) | (fld_tszl >> 2);
2276
1.46k
  else if (fld_tszl & 0x4)
2277
970
    imm = (fld_i1 << 1) | fld_tszh;
2278
490
  else if (fld_tszh)
2279
490
    imm = fld_i1;
2280
0
  else
2281
0
    return false;
2282
2283
6.93k
  info->indexed_za.index.imm = imm;
2284
6.93k
  return true;
2285
6.93k
}
2286
2287
/* Decode Zn[MM], where MM has a 7-bit triangular encoding.  The fields
2288
   array specifies which field to use for Zn.  MM is encoded in the
2289
   concatenation of imm5 and SVE_tszh, with imm5 being the less
2290
   significant part.  */
2291
bool
2292
aarch64_ext_sve_index (const aarch64_operand *self,
2293
           aarch64_opnd_info *info, aarch64_insn code,
2294
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
2295
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2296
2.47k
{
2297
2.47k
  int val;
2298
2299
2.47k
  info->reglane.regno = extract_field (self->fields[0], code, 0);
2300
2.47k
  val = extract_all_fields_after (self, 1, code);
2301
2.47k
  if ((val & 31) == 0)
2302
0
    return 0;
2303
4.22k
  while ((val & 1) == 0)
2304
1.75k
    val /= 2;
2305
2.47k
  info->reglane.index = val / 2;
2306
2.47k
  return true;
2307
2.47k
}
2308
2309
/* Decode a logical immediate for the MOV alias of SVE DUPM.  */
2310
bool
2311
aarch64_ext_sve_limm_mov (const aarch64_operand *self,
2312
        aarch64_opnd_info *info, const aarch64_insn code,
2313
        const aarch64_inst *inst,
2314
        aarch64_operand_error *errors)
2315
4.87k
{
2316
4.87k
  int esize = aarch64_get_qualifier_esize (inst->operands[0].qualifier);
2317
4.87k
  return (aarch64_ext_limm (self, info, code, inst, errors)
2318
4.87k
    && aarch64_sve_dupm_mov_immediate_p (info->imm.value, esize));
2319
4.87k
}
2320
2321
/* Decode Zn[MM], where Zn occupies the least-significant part of the field
2322
   and where MM occupies the most-significant part.  The operand-dependent
2323
   value specifies the number of bits in Zn.  */
2324
bool
2325
aarch64_ext_sve_quad_index (const aarch64_operand *self,
2326
          aarch64_opnd_info *info, aarch64_insn code,
2327
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
2328
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2329
30.5k
{
2330
30.5k
  unsigned int reg_bits = get_operand_specific_data (self);
2331
30.5k
  unsigned int val = extract_all_fields (self, code);
2332
30.5k
  info->reglane.regno = val & ((1 << reg_bits) - 1);
2333
30.5k
  info->reglane.index = val >> reg_bits;
2334
30.5k
  return true;
2335
30.5k
}
2336
2337
/* Decode {Zn.<T> - Zm.<T>}.  The fields array specifies which field
2338
   to use for Zn.  The opcode-dependent value specifies the number
2339
   of registers in the list.  */
2340
bool
2341
aarch64_ext_sve_reglist (const aarch64_operand *self,
2342
       aarch64_opnd_info *info, aarch64_insn code,
2343
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
2344
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2345
262k
{
2346
262k
  info->reglist.first_regno = extract_field (self->fields[0], code, 0);
2347
262k
  info->reglist.num_regs = get_opcode_dependent_value (inst->opcode);
2348
262k
  info->reglist.stride = 1;
2349
262k
  return true;
2350
262k
}
2351
2352
/* Decode {Zn.<T> , Zm.<T>}.  The fields array specifies which field
2353
   to use for Zn.  The opcode-dependent value specifies the number
2354
   of registers in the list.  */
2355
bool
2356
aarch64_ext_sve_reglist_zt (const aarch64_operand *self,
2357
          aarch64_opnd_info *info, aarch64_insn code,
2358
          const aarch64_inst *inst ATTRIBUTE_UNUSED,
2359
          aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2360
0
{
2361
0
  info->reglist.first_regno = extract_field (self->fields[0], code, 0);
2362
0
  info->reglist.num_regs = get_operand_specific_data (self);
2363
0
  info->reglist.stride = 1;
2364
0
  return true;
2365
0
}
2366
2367
/* Decode { <Zm1>-<Zm2> }[<index>].  The fields array specifies which field
2368
   to use for Zm.  The opcode-dependent value specifies the number
2369
   of registers in the list.  */
2370
bool
2371
aarch64_ext_sve_reglist_index (const aarch64_operand *self,
2372
       aarch64_opnd_info *info, aarch64_insn code,
2373
       const aarch64_inst *inst ATTRIBUTE_UNUSED,
2374
       aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2375
169
{
2376
169
  info->reglist.first_regno = extract_field (self->fields[0], code, 0);
2377
169
  info->reglist.num_regs = get_opcode_dependent_value (inst->opcode);
2378
169
  info->reglist.stride = 1;
2379
169
  info->reglist.has_index = true;
2380
169
  info->reglist.index = extract_field (FLD_imm1_22, code, 0);
2381
169
  return true;
2382
169
}
2383
2384
/* Decode a strided register list.  The first field holds the top bit
2385
   (0 or 16) and the second field holds the lower bits.  The stride is
2386
   16 divided by the list length.  */
2387
bool
2388
aarch64_ext_sve_strided_reglist (const aarch64_operand *self,
2389
         aarch64_opnd_info *info, aarch64_insn code,
2390
         const aarch64_inst *inst ATTRIBUTE_UNUSED,
2391
         aarch64_operand_error *errors
2392
           ATTRIBUTE_UNUSED)
2393
11.0k
{
2394
11.0k
  unsigned int upper = extract_field (self->fields[0], code, 0);
2395
11.0k
  unsigned int lower = extract_field (self->fields[1], code, 0);
2396
11.0k
  info->reglist.first_regno = upper * 16 + lower;
2397
11.0k
  info->reglist.num_regs = get_operand_specific_data (self);
2398
11.0k
  info->reglist.stride = 16 / info->reglist.num_regs;
2399
11.0k
  return true;
2400
11.0k
}
2401
2402
/* Decode <pattern>{, MUL #<amount>}.  The fields array specifies which
2403
   fields to use for <pattern>.  <amount> - 1 is encoded in the SVE_imm4
2404
   field.  */
2405
bool
2406
aarch64_ext_sve_scale (const aarch64_operand *self,
2407
           aarch64_opnd_info *info, aarch64_insn code,
2408
           const aarch64_inst *inst, aarch64_operand_error *errors)
2409
6.80k
{
2410
6.80k
  int val;
2411
2412
6.80k
  if (!aarch64_ext_imm (self, info, code, inst, errors))
2413
0
    return false;
2414
6.80k
  val = extract_field (FLD_SVE_imm4, code, 0);
2415
6.80k
  info->shifter.kind = AARCH64_MOD_MUL;
2416
6.80k
  info->shifter.amount = val + 1;
2417
6.80k
  info->shifter.operator_present = (val != 0);
2418
6.80k
  info->shifter.amount_present = (val != 0);
2419
6.80k
  return true;
2420
6.80k
}
2421
2422
/* Return the top set bit in VALUE, which is expected to be relatively
2423
   small.  */
2424
static uint64_t
2425
get_top_bit (uint64_t value)
2426
10.4k
{
2427
36.5k
  while ((value & -value) != value)
2428
26.0k
    value -= value & -value;
2429
10.4k
  return value;
2430
10.4k
}
2431
2432
/* Decode an SVE shift-left immediate.  */
2433
bool
2434
aarch64_ext_sve_shlimm (const aarch64_operand *self,
2435
      aarch64_opnd_info *info, const aarch64_insn code,
2436
      const aarch64_inst *inst, aarch64_operand_error *errors)
2437
2.62k
{
2438
2.62k
  if (!aarch64_ext_imm (self, info, code, inst, errors)
2439
2.62k
      || info->imm.value == 0)
2440
0
    return false;
2441
2442
2.62k
  info->imm.value -= get_top_bit (info->imm.value);
2443
2.62k
  return true;
2444
2.62k
}
2445
2446
/* Decode an SVE shift-right immediate.  */
2447
bool
2448
aarch64_ext_sve_shrimm (const aarch64_operand *self,
2449
      aarch64_opnd_info *info, const aarch64_insn code,
2450
      const aarch64_inst *inst, aarch64_operand_error *errors)
2451
7.84k
{
2452
7.84k
  if (!aarch64_ext_imm (self, info, code, inst, errors)
2453
7.84k
      || info->imm.value == 0)
2454
0
    return false;
2455
2456
7.84k
  info->imm.value = get_top_bit (info->imm.value) * 2 - info->imm.value;
2457
7.84k
  return true;
2458
7.84k
}
2459
2460
/* Decode X0-X30.  Register 31 is unallocated.  */
2461
bool
2462
aarch64_ext_x0_to_x30 (const aarch64_operand *self, aarch64_opnd_info *info,
2463
           const aarch64_insn code,
2464
           const aarch64_inst *inst ATTRIBUTE_UNUSED,
2465
           aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2466
28.4k
{
2467
28.4k
  info->reg.regno = extract_field (self->fields[0], code, 0);
2468
28.4k
  return info->reg.regno <= 30;
2469
28.4k
}
2470
2471
/* Decode an indexed register, with the last five field bits holding the
2472
   register number and the remaining bits holding the index.  */
2473
bool
2474
aarch64_ext_simple_index (const aarch64_operand *self, aarch64_opnd_info *info,
2475
        const aarch64_insn code,
2476
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
2477
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2478
48.5k
{
2479
48.5k
  unsigned int val = extract_all_fields (self, code);
2480
48.5k
  info->reglane.regno = val & 31;
2481
48.5k
  info->reglane.index = val >> 5;
2482
48.5k
  return true;
2483
48.5k
}
2484
2485
/* Decode a plain shift-right immediate, when there is only a single
2486
   element size.  */
2487
bool
2488
aarch64_ext_plain_shrimm (const aarch64_operand *self, aarch64_opnd_info *info,
2489
        const aarch64_insn code,
2490
        const aarch64_inst *inst ATTRIBUTE_UNUSED,
2491
        aarch64_operand_error *errors ATTRIBUTE_UNUSED)
2492
338
{
2493
338
  unsigned int base = 1 << get_operand_field_width (self, 0);
2494
338
  info->imm.value = base - extract_field (self->fields[0], code, 0);
2495
338
  return true;
2496
338
}
2497

2498
/* Bitfields that are commonly used to encode certain operands' information
2499
   may be partially used as part of the base opcode in some instructions.
2500
   For example, the bit 1 of the field 'size' in
2501
     FCVTXN <Vb><d>, <Va><n>
2502
   is actually part of the base opcode, while only size<0> is available
2503
   for encoding the register type.  Another example is the AdvSIMD
2504
   instruction ORR (register), in which the field 'size' is also used for
2505
   the base opcode, leaving only the field 'Q' available to encode the
2506
   vector register arrangement specifier '8B' or '16B'.
2507
2508
   This function tries to deduce the qualifier from the value of partially
2509
   constrained field(s).  Given the VALUE of such a field or fields, the
2510
   qualifiers CANDIDATES and the MASK (indicating which bits are valid for
2511
   operand encoding), the function returns the matching qualifier or
2512
   AARCH64_OPND_QLF_ERR if nothing matches.
2513
2514
   N.B. CANDIDATES is a group of possible qualifiers that are valid for
2515
   one operand; it has a maximum of AARCH64_MAX_QLF_SEQ_NUM qualifiers and
2516
   may end with AARCH64_OPND_QLF_UNUSED.  */
2517
2518
static enum aarch64_opnd_qualifier
2519
get_qualifier_from_partial_encoding (aarch64_insn value,
2520
             const enum aarch64_opnd_qualifier* \
2521
             candidates,
2522
             aarch64_insn mask)
2523
156k
{
2524
156k
  int i;
2525
156k
  DEBUG_TRACE ("enter with value: %d, mask: %d", (int)value, (int)mask);
2526
273k
  for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i)
2527
273k
    {
2528
273k
      aarch64_insn standard_value;
2529
273k
      if (candidates[i] == AARCH64_OPND_QLF_UNUSED)
2530
21.5k
  break;
2531
252k
      standard_value = aarch64_get_qualifier_standard_value (candidates[i]);
2532
252k
      if ((standard_value & mask) == (value & mask))
2533
135k
  return candidates[i];
2534
252k
    }
2535
21.5k
  return AARCH64_OPND_QLF_ERR;
2536
156k
}
2537
2538
/* Given a list of qualifier sequences, return all possible valid qualifiers
2539
   for operand IDX in QUALIFIERS.
2540
   Assume QUALIFIERS is an array whose length is large enough.  */
2541
2542
static void
2543
get_operand_possible_qualifiers (int idx,
2544
         const aarch64_opnd_qualifier_seq_t *list,
2545
         enum aarch64_opnd_qualifier *qualifiers)
2546
156k
{
2547
156k
  int i;
2548
517k
  for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i)
2549
517k
    if ((qualifiers[i] = list[i][idx]) == AARCH64_OPND_QLF_UNUSED)
2550
156k
      break;
2551
156k
}
2552
2553
/* Decode the size Q field for e.g. SHADD.
2554
   We tag one operand with the qualifer according to the code;
2555
   whether the qualifier is valid for this opcode or not, it is the
2556
   duty of the semantic checking.  */
2557
2558
static int
2559
decode_sizeq (aarch64_inst *inst)
2560
321k
{
2561
321k
  int idx;
2562
321k
  aarch64_insn code;
2563
321k
  aarch64_insn value, mask;
2564
321k
  aarch64_field fld_sz;
2565
321k
  enum aarch64_opnd_qualifier candidates[AARCH64_MAX_QLF_SEQ_NUM];
2566
2567
321k
  if (inst->opcode->iclass == asisdlse
2568
299k
     || inst->opcode->iclass == asisdlsep
2569
267k
     || inst->opcode->iclass == asisdlso
2570
267k
     || inst->opcode->iclass == asisdlsop)
2571
56.2k
    fld_sz = FLD_vldst_size;
2572
265k
  else
2573
265k
    fld_sz = FLD_size;
2574
2575
321k
  code = inst->value;
2576
321k
  value = extract_fields (code, inst->opcode->mask, 2, fld_sz, FLD_Q);
2577
  /* Obtain the info that which bits of fields Q and size are actually
2578
     available for operand encoding.  Opcodes like FMAXNM and FMLA have
2579
     size[1] unavailable.  */
2580
321k
  mask = extract_fields (~inst->opcode->mask, 0, 2, fld_sz, FLD_Q);
2581
2582
  /* The index of the operand we are going to tag a qualifier and the qualifer
2583
     itself are reasoned from the value of the size and Q fields and the
2584
     possible valid qualifier lists.  */
2585
321k
  idx = aarch64_select_operand_for_sizeq_field_coding (inst->opcode);
2586
321k
  DEBUG_TRACE ("key idx: %d", idx);
2587
2588
  /* For most related instruciton, size:Q are fully available for operand
2589
     encoding.  */
2590
321k
  if (mask == 0x7)
2591
171k
    {
2592
171k
      inst->operands[idx].qualifier = get_vreg_qualifier_from_value (value);
2593
171k
      if (inst->operands[idx].qualifier == AARCH64_OPND_QLF_ERR)
2594
0
  return 0;
2595
171k
      return 1;
2596
171k
    }
2597
2598
149k
  get_operand_possible_qualifiers (idx, inst->opcode->qualifiers_list,
2599
149k
           candidates);
2600
#ifdef DEBUG_AARCH64
2601
  if (debug_dump)
2602
    {
2603
      int i;
2604
      for (i = 0; candidates[i] != AARCH64_OPND_QLF_UNUSED
2605
     && i < AARCH64_MAX_QLF_SEQ_NUM; ++i)
2606
  DEBUG_TRACE ("qualifier %d: %s", i,
2607
         aarch64_get_qualifier_name(candidates[i]));
2608
      DEBUG_TRACE ("%d, %d", (int)value, (int)mask);
2609
    }
2610
#endif /* DEBUG_AARCH64 */
2611
2612
149k
  enum aarch64_opnd_qualifier qualifier
2613
149k
    = get_qualifier_from_partial_encoding (value, candidates, mask);
2614
2615
149k
  if (qualifier == AARCH64_OPND_QLF_ERR)
2616
21.5k
    return 0;
2617
2618
128k
  inst->operands[idx].qualifier = qualifier;
2619
128k
  return 1;
2620
149k
}
2621
2622
/* Decode size[0]:Q, i.e. bit 22 and bit 30, for
2623
     e.g. FCVTN<Q> <Vd>.<Tb>, <Vn>.<Ta>.  */
2624
2625
static int
2626
decode_asimd_fcvt (aarch64_inst *inst)
2627
788
{
2628
788
  aarch64_field field = AARCH64_FIELD_NIL;
2629
788
  aarch64_insn value;
2630
788
  enum aarch64_opnd_qualifier qualifier;
2631
2632
788
  gen_sub_field (FLD_size, 0, 1, &field);
2633
788
  value = extract_field (field, inst->value, 0);
2634
788
  qualifier = value == 0 ? AARCH64_OPND_QLF_V_4S
2635
788
    : AARCH64_OPND_QLF_V_2D;
2636
788
  switch (inst->opcode->op)
2637
788
    {
2638
222
    case OP_FCVTN:
2639
310
    case OP_FCVTN2:
2640
      /* FCVTN<Q> <Vd>.<Tb>, <Vn>.<Ta>.  */
2641
310
      inst->operands[1].qualifier = qualifier;
2642
310
      break;
2643
422
    case OP_FCVTL:
2644
478
    case OP_FCVTL2:
2645
      /* FCVTL<Q> <Vd>.<Ta>, <Vn>.<Tb>.  */
2646
478
      inst->operands[0].qualifier = qualifier;
2647
478
      break;
2648
0
    default:
2649
0
      return 0;
2650
788
    }
2651
2652
788
  return 1;
2653
788
}
2654
2655
/* Decode size[0], i.e. bit 22, for
2656
     e.g. FCVTXN <Vb><d>, <Va><n>.  */
2657
2658
static int
2659
decode_asisd_fcvtxn (aarch64_inst *inst)
2660
96
{
2661
96
  aarch64_field field = AARCH64_FIELD_NIL;
2662
96
  gen_sub_field (FLD_size, 0, 1, &field);
2663
96
  if (!extract_field (field, inst->value, 0))
2664
51
    return 0;
2665
45
  inst->operands[0].qualifier = AARCH64_OPND_QLF_S_S;
2666
45
  return 1;
2667
96
}
2668
2669
/* Decode the 'opc' field for e.g. FCVT <Dd>, <Sn>.  */
2670
static int
2671
decode_fcvt (aarch64_inst *inst)
2672
469
{
2673
469
  enum aarch64_opnd_qualifier qualifier;
2674
469
  aarch64_insn value;
2675
469
  const aarch64_field field = AARCH64_FIELD (15, 2);
2676
2677
  /* opc dstsize */
2678
469
  value = extract_field (field, inst->value, 0);
2679
469
  switch (value)
2680
469
    {
2681
41
    case 0: qualifier = AARCH64_OPND_QLF_S_S; break;
2682
198
    case 1: qualifier = AARCH64_OPND_QLF_S_D; break;
2683
129
    case 3: qualifier = AARCH64_OPND_QLF_S_H; break;
2684
101
    default: return 0;
2685
469
    }
2686
368
  inst->operands[0].qualifier = qualifier;
2687
2688
368
  return 1;
2689
469
}
2690
2691
/* Do miscellaneous decodings that are not common enough to be driven by
2692
   flags.  */
2693
2694
static int
2695
do_misc_decoding (aarch64_inst *inst)
2696
13.8k
{
2697
13.8k
  unsigned int value;
2698
13.8k
  switch (inst->opcode->op)
2699
13.8k
    {
2700
469
    case OP_FCVT:
2701
469
      return decode_fcvt (inst);
2702
2703
222
    case OP_FCVTN:
2704
310
    case OP_FCVTN2:
2705
732
    case OP_FCVTL:
2706
788
    case OP_FCVTL2:
2707
788
      return decode_asimd_fcvt (inst);
2708
2709
96
    case OP_FCVTXN_S:
2710
96
      return decode_asisd_fcvtxn (inst);
2711
2712
1.09k
    case OP_MOV_P_P:
2713
1.26k
    case OP_MOVS_P_P:
2714
      /* ORR/ORRS alias with Pn == Pm == Pg.  */
2715
1.26k
      value = extract_field (AARCH64_FIELD (5, 4), inst->value, 0);
2716
1.26k
      return (value == extract_field (AARCH64_FIELD (16, 4), inst->value, 0)
2717
701
        && value == extract_field (AARCH64_FIELD (10, 4),
2718
701
           inst->value, 0));
2719
2720
6.87k
    case OP_MOV_Z_P_Z:
2721
      /* SEL alias with Zd == Zm.  */
2722
6.87k
      return (extract_field (AARCH64_FIELD (0, 5), inst->value, 0)
2723
6.87k
        == extract_field (AARCH64_FIELD (16, 5), inst->value, 0));
2724
2725
1.25k
    case OP_MOV_Z_V:
2726
      /* DUP alias with zero index.  Index and size use a triangle encoding,
2727
   and we already know that one of the bottom 5 bits is nonzero, so we
2728
   just need to check that the bitcount is at most 1.  */
2729
1.25k
      value = extract_fields (inst->value, 0, 2, AARCH64_FIELD (22, 2),
2730
1.25k
            AARCH64_FIELD (16, 5));
2731
1.25k
      return value == (value & -value);
2732
2733
268
    case OP_MOV_Z_Z:
2734
      /* ORR alias with Zn == Zm.  */
2735
268
      return (extract_field (AARCH64_FIELD (5, 5), inst->value, 0)
2736
268
        == extract_field (AARCH64_FIELD (16, 5), inst->value, 0));
2737
2738
462
    case OP_MOVM_P_P_P:
2739
      /* SEL alias with Pd == Pm.  */
2740
462
      return (extract_field (AARCH64_FIELD (0, 4), inst->value, 0)
2741
462
        == extract_field (AARCH64_FIELD (16, 4), inst->value, 0));
2742
2743
469
    case OP_MOVZS_P_P_P:
2744
797
    case OP_MOVZ_P_P_P:
2745
      /* AND/ANDS alias with Pn == Pm.  */
2746
797
      return (extract_field (AARCH64_FIELD (5, 4), inst->value, 0)
2747
797
        == extract_field (AARCH64_FIELD (16, 4), inst->value, 0));
2748
2749
383
    case OP_NOTS_P_P_P_Z:
2750
1.54k
    case OP_NOT_P_P_P_Z:
2751
      /* EOR/EORS alias with Pm == Pg.  */
2752
1.54k
      return (extract_field (AARCH64_FIELD (16, 4), inst->value, 0)
2753
1.54k
        == extract_field (AARCH64_FIELD (10, 4), inst->value, 0));
2754
2755
0
    default:
2756
0
      return 0;
2757
13.8k
    }
2758
13.8k
}
2759
2760
/* Opcodes that have fields shared by multiple operands are usually flagged
2761
   with flags.  In this function, we detect such flags, decode the related
2762
   field(s) and store the information in one of the related operands.  The
2763
   'one' operand is not any operand but one of the operands that can
2764
   accommodate all the information that has been decoded.  */
2765
2766
static int
2767
do_special_decoding (aarch64_inst *inst)
2768
2.84M
{
2769
2.84M
  int idx;
2770
2.84M
  aarch64_insn value;
2771
  /* Condition for truly conditional executed instructions, e.g. b.cond.  */
2772
2.84M
  if (inst->opcode->flags & F_COND)
2773
38.5k
    {
2774
38.5k
      value = extract_field (FLD_cond2, inst->value, 0);
2775
38.5k
      inst->cond = get_cond_from_value (value);
2776
38.5k
    }
2777
  /* 'sf' field.  */
2778
2.84M
  if (inst->opcode->flags & F_SF)
2779
2.10M
    {
2780
2.10M
      idx = select_operand_for_sf_field_coding (inst->opcode);
2781
2.10M
      value = extract_field (FLD_sf, inst->value, 0);
2782
2.10M
      if (inst->opcode->iclass == fprcvtfloat2int
2783
2.10M
    || inst->opcode->iclass == fprcvtint2float)
2784
1.73k
  {
2785
1.73k
    if (value == 0)
2786
870
      inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_S;
2787
869
    else
2788
869
      inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_D;
2789
1.73k
  }
2790
2.10M
      else
2791
2.10M
  inst->operands[idx].qualifier = get_greg_qualifier_from_value (value);
2792
2.10M
      if (inst->operands[idx].qualifier == AARCH64_OPND_QLF_ERR)
2793
0
  return 0;
2794
2.10M
      if ((inst->opcode->flags & F_N)
2795
143k
    && extract_field (FLD_N, inst->value, 0) != value)
2796
69.5k
  return 0;
2797
2.10M
    }
2798
  /* 'sf' field.  */
2799
2.77M
  if (inst->opcode->flags & F_LSE_SZ)
2800
34.2k
    {
2801
34.2k
      idx = select_operand_for_sf_field_coding (inst->opcode);
2802
34.2k
      value = extract_field (FLD_lse_sz, inst->value, 0);
2803
34.2k
      inst->operands[idx].qualifier = get_greg_qualifier_from_value (value);
2804
34.2k
      if (inst->operands[idx].qualifier == AARCH64_OPND_QLF_ERR)
2805
0
  return 0;
2806
34.2k
    }
2807
  /* rcpc3 'size' field.  */
2808
2.77M
  if (inst->opcode->flags & F_RCPC3_SIZE)
2809
25.9k
    {
2810
25.9k
      value = extract_field (FLD_rcpc3_size, inst->value, 0);
2811
25.9k
      for (int i = 0;
2812
64.4k
     aarch64_operands[inst->operands[i].type].op_class != AARCH64_OPND_CLASS_ADDRESS;
2813
38.4k
     i++)
2814
41.8k
  {
2815
41.8k
    if (aarch64_operands[inst->operands[i].type].op_class
2816
41.8k
        == AARCH64_OPND_CLASS_INT_REG)
2817
32.0k
      {
2818
32.0k
        inst->operands[i].qualifier = get_greg_qualifier_from_value (value & 1);
2819
32.0k
        if (inst->operands[i].qualifier == AARCH64_OPND_QLF_ERR)
2820
0
    return 0;
2821
32.0k
      }
2822
9.77k
    else if (aarch64_operands[inst->operands[i].type].op_class
2823
9.77k
        == AARCH64_OPND_CLASS_FP_REG)
2824
9.77k
      {
2825
9.77k
        value += (extract_field (FLD_opc1, inst->value, 0) << 2);
2826
9.77k
        inst->operands[i].qualifier = get_sreg_qualifier_from_value (value);
2827
9.77k
        if (inst->operands[i].qualifier == AARCH64_OPND_QLF_ERR)
2828
3.34k
    return 0;
2829
9.77k
      }
2830
41.8k
  }
2831
25.9k
    }
2832
2833
  /* size:Q fields.  */
2834
2.76M
  if (inst->opcode->flags & F_SIZEQ)
2835
321k
    return decode_sizeq (inst);
2836
2837
2.44M
  if (inst->opcode->flags & F_FPTYPE)
2838
63.1k
    {
2839
63.1k
      idx = select_operand_for_fptype_field_coding (inst->opcode);
2840
63.1k
      value = extract_field (FLD_type, inst->value, 0);
2841
63.1k
      switch (value)
2842
63.1k
  {
2843
21.7k
  case 0: inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_S; break;
2844
6.93k
  case 1: inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_D; break;
2845
23.5k
  case 3: inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_H; break;
2846
10.8k
  default: return 0;
2847
63.1k
  }
2848
63.1k
    }
2849
2850
2.43M
  if (inst->opcode->flags & F_SSIZE)
2851
17.8k
    {
2852
      /* N.B. some opcodes like FCMGT <V><d>, <V><n>, #0 have the size[1] as part
2853
   of the base opcode.  */
2854
17.8k
      aarch64_insn mask;
2855
17.8k
      enum aarch64_opnd_qualifier candidates[AARCH64_MAX_QLF_SEQ_NUM];
2856
17.8k
      idx = select_operand_for_scalar_size_field_coding (inst->opcode);
2857
17.8k
      value = extract_field (FLD_size, inst->value, inst->opcode->mask);
2858
17.8k
      mask = extract_field (FLD_size, ~inst->opcode->mask, 0);
2859
      /* For most related instruciton, the 'size' field is fully available for
2860
   operand encoding.  */
2861
17.8k
      if (mask == 0x3)
2862
11.1k
  {
2863
11.1k
    inst->operands[idx].qualifier = get_sreg_qualifier_from_value (value);
2864
11.1k
    if (inst->operands[idx].qualifier == AARCH64_OPND_QLF_ERR)
2865
0
      return 0;
2866
11.1k
  }
2867
6.72k
      else
2868
6.72k
  {
2869
6.72k
    get_operand_possible_qualifiers (idx, inst->opcode->qualifiers_list,
2870
6.72k
             candidates);
2871
6.72k
    inst->operands[idx].qualifier
2872
6.72k
      = get_qualifier_from_partial_encoding (value, candidates, mask);
2873
6.72k
  }
2874
17.8k
    }
2875
2876
2.43M
  if (inst->opcode->flags & F_LSFE_SZ)
2877
12.6k
    {
2878
12.6k
      value = extract_field (FLD_ldst_size, inst->value, 0);
2879
2880
12.6k
      if (value > 0x3)
2881
0
  return 0;
2882
2883
12.6k
      for (int i = 0;
2884
37.8k
     aarch64_operands[inst->operands[i].type].op_class != AARCH64_OPND_CLASS_ADDRESS;
2885
25.2k
     i++)
2886
25.2k
  {
2887
25.2k
    inst->operands[i].qualifier = get_sreg_qualifier_from_value (value);
2888
25.2k
    if (inst->operands[i].qualifier == AARCH64_OPND_QLF_ERR)
2889
0
      return 0;
2890
25.2k
  }
2891
12.6k
    }
2892
2893
2.43M
  if (inst->opcode->flags & F_T)
2894
11.5k
    {
2895
      /* Num of consecutive '0's on the right side of imm5<3:0>.  */
2896
11.5k
      int num = 0;
2897
11.5k
      unsigned val, Q;
2898
11.5k
      assert (aarch64_get_operand_class (inst->opcode->operands[0])
2899
11.5k
        == AARCH64_OPND_CLASS_SIMD_REG);
2900
      /* imm5<3:0>  q <t>
2901
   0000   x reserved
2902
   xxx1   0 8b
2903
   xxx1   1 16b
2904
   xx10   0 4h
2905
   xx10   1 8h
2906
   x100   0 2s
2907
   x100   1 4s
2908
   1000   0 reserved
2909
   1000   1 2d  */
2910
11.5k
      val = extract_field (FLD_imm5, inst->value, 0);
2911
26.3k
      while ((val & 0x1) == 0 && ++num <= 3)
2912
14.8k
  val >>= 1;
2913
11.5k
      if (num > 3)
2914
1.60k
  return 0;
2915
9.93k
      Q = (unsigned) extract_field (FLD_Q, inst->value, inst->opcode->mask);
2916
9.93k
      inst->operands[0].qualifier =
2917
9.93k
  get_vreg_qualifier_from_value ((num << 1) | Q);
2918
9.93k
      if (inst->operands[0].qualifier == AARCH64_OPND_QLF_ERR)
2919
0
  return 0;
2920
2921
9.93k
    }
2922
2923
2.43M
  if ((inst->opcode->flags & F_OPD_SIZE) && inst->opcode->iclass == sve2_urqvs)
2924
603
    {
2925
603
      unsigned size;
2926
603
      size = (unsigned) extract_field (FLD_size, inst->value,
2927
603
               inst->opcode->mask);
2928
603
      inst->operands[0].qualifier
2929
603
  = get_vreg_qualifier_from_value (1 + (size << 1));
2930
603
      if (inst->operands[0].qualifier == AARCH64_OPND_QLF_ERR)
2931
0
  return 0;
2932
603
      inst->operands[2].qualifier = get_sreg_qualifier_from_value (size);
2933
603
      if (inst->operands[2].qualifier == AARCH64_OPND_QLF_ERR)
2934
0
  return 0;
2935
603
    }
2936
2937
2.43M
  if (inst->opcode->flags & F_GPRSIZE_IN_Q)
2938
169k
    {
2939
      /* Use Rt to encode in the case of e.g.
2940
   STXP <Ws>, <Xt1>, <Xt2>, [<Xn|SP>{,#0}].  */
2941
169k
      idx = aarch64_operand_index (inst->opcode->operands, AARCH64_OPND_Rt);
2942
169k
      if (idx == -1)
2943
6.00k
  {
2944
    /* Otherwise use the result operand, which has to be a integer
2945
       register.  */
2946
6.00k
    assert (aarch64_get_operand_class (inst->opcode->operands[0])
2947
6.00k
      == AARCH64_OPND_CLASS_INT_REG);
2948
6.00k
    idx = 0;
2949
6.00k
  }
2950
169k
      assert (idx == 0 || idx == 1);
2951
169k
      value = extract_field (FLD_Q, inst->value, 0);
2952
169k
      inst->operands[idx].qualifier = get_greg_qualifier_from_value (value);
2953
169k
      if (inst->operands[idx].qualifier == AARCH64_OPND_QLF_ERR)
2954
0
  return 0;
2955
169k
    }
2956
2957
2.43M
  if (inst->opcode->flags & F_LDS_SIZE)
2958
32.8k
    {
2959
32.8k
      aarch64_field field = AARCH64_FIELD_NIL;
2960
32.8k
      assert (aarch64_get_operand_class (inst->opcode->operands[0])
2961
32.8k
        == AARCH64_OPND_CLASS_INT_REG);
2962
32.8k
      gen_sub_field (FLD_opc, 0, 1, &field);
2963
32.8k
      value = extract_field (field, inst->value, 0);
2964
32.8k
      inst->operands[0].qualifier
2965
32.8k
  = value ? AARCH64_OPND_QLF_W : AARCH64_OPND_QLF_X;
2966
32.8k
    }
2967
2968
  /* Miscellaneous decoding; done as the last step.  */
2969
2.43M
  if (inst->opcode->flags & F_MISC)
2970
13.8k
    return do_misc_decoding (inst);
2971
2972
2.42M
  return 1;
2973
2.43M
}
2974
2975
/* Converters converting a real opcode instruction to its alias form.  */
2976
2977
/* ROR <Wd>, <Ws>, #<shift>
2978
     is equivalent to:
2979
   EXTR <Wd>, <Ws>, <Ws>, #<shift>.  */
2980
static int
2981
convert_extr_to_ror (aarch64_inst *inst)
2982
2.69k
{
2983
2.69k
  if (inst->operands[1].reg.regno == inst->operands[2].reg.regno)
2984
385
    {
2985
385
      copy_operand_info (inst, 2, 3);
2986
385
      inst->operands[3].type = AARCH64_OPND_NIL;
2987
385
      return 1;
2988
385
    }
2989
2.31k
  return 0;
2990
2.69k
}
2991
2992
/* UXTL<Q> <Vd>.<Ta>, <Vn>.<Tb>
2993
     is equivalent to:
2994
   USHLL<Q> <Vd>.<Ta>, <Vn>.<Tb>, #0.  */
2995
static int
2996
convert_shll_to_xtl (aarch64_inst *inst)
2997
795
{
2998
795
  if (inst->operands[2].imm.value == 0)
2999
660
    {
3000
660
      inst->operands[2].type = AARCH64_OPND_NIL;
3001
660
      return 1;
3002
660
    }
3003
135
  return 0;
3004
795
}
3005
3006
/* Convert
3007
     UBFM <Xd>, <Xn>, #<shift>, #63.
3008
   to
3009
     LSR <Xd>, <Xn>, #<shift>.  */
3010
static int
3011
convert_bfm_to_sr (aarch64_inst *inst)
3012
21.2k
{
3013
21.2k
  int64_t imms, val;
3014
3015
21.2k
  imms = inst->operands[3].imm.value;
3016
21.2k
  val = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 31 : 63;
3017
21.2k
  if (imms == val)
3018
471
    {
3019
471
      inst->operands[3].type = AARCH64_OPND_NIL;
3020
471
      return 1;
3021
471
    }
3022
3023
20.7k
  return 0;
3024
21.2k
}
3025
3026
/* Convert MOV to ORR.  */
3027
static int
3028
convert_orr_to_mov (aarch64_inst *inst)
3029
659
{
3030
  /* MOV <Vd>.<T>, <Vn>.<T>
3031
     is equivalent to:
3032
     ORR <Vd>.<T>, <Vn>.<T>, <Vn>.<T>.  */
3033
659
  if (inst->operands[1].reg.regno == inst->operands[2].reg.regno)
3034
97
    {
3035
97
      inst->operands[2].type = AARCH64_OPND_NIL;
3036
97
      return 1;
3037
97
    }
3038
562
  return 0;
3039
659
}
3040
3041
/* When <imms> >= <immr>, the instruction written:
3042
     SBFX <Xd>, <Xn>, #<lsb>, #<width>
3043
   is equivalent to:
3044
     SBFM <Xd>, <Xn>, #<lsb>, #(<lsb>+<width>-1).  */
3045
3046
static int
3047
convert_bfm_to_bfx (aarch64_inst *inst)
3048
27.9k
{
3049
27.9k
  int64_t immr, imms;
3050
3051
27.9k
  immr = inst->operands[2].imm.value;
3052
27.9k
  imms = inst->operands[3].imm.value;
3053
27.9k
  if (imms >= immr)
3054
13.2k
    {
3055
13.2k
      int64_t lsb = immr;
3056
13.2k
      inst->operands[2].imm.value = lsb;
3057
13.2k
      inst->operands[3].imm.value = imms + 1 - lsb;
3058
      /* The two opcodes have different qualifiers for
3059
   the immediate operands; reset to help the checking.  */
3060
13.2k
      reset_operand_qualifier (inst, 2);
3061
13.2k
      reset_operand_qualifier (inst, 3);
3062
13.2k
      return 1;
3063
13.2k
    }
3064
3065
14.7k
  return 0;
3066
27.9k
}
3067
3068
/* When <imms> < <immr>, the instruction written:
3069
     SBFIZ <Xd>, <Xn>, #<lsb>, #<width>
3070
   is equivalent to:
3071
     SBFM <Xd>, <Xn>, #((64-<lsb>)&0x3f), #(<width>-1).  */
3072
3073
static int
3074
convert_bfm_to_bfi (aarch64_inst *inst)
3075
14.7k
{
3076
14.7k
  int64_t immr, imms, val;
3077
3078
14.7k
  immr = inst->operands[2].imm.value;
3079
14.7k
  imms = inst->operands[3].imm.value;
3080
14.7k
  val = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 32 : 64;
3081
14.7k
  if (imms < immr)
3082
14.7k
    {
3083
14.7k
      inst->operands[2].imm.value = (val - immr) & (val - 1);
3084
14.7k
      inst->operands[3].imm.value = imms + 1;
3085
      /* The two opcodes have different qualifiers for
3086
   the immediate operands; reset to help the checking.  */
3087
14.7k
      reset_operand_qualifier (inst, 2);
3088
14.7k
      reset_operand_qualifier (inst, 3);
3089
14.7k
      return 1;
3090
14.7k
    }
3091
3092
0
  return 0;
3093
14.7k
}
3094
3095
/* The instruction written:
3096
     BFC <Xd>, #<lsb>, #<width>
3097
   is equivalent to:
3098
     BFM <Xd>, XZR, #((64-<lsb>)&0x3f), #(<width>-1).  */
3099
3100
static int
3101
convert_bfm_to_bfc (aarch64_inst *inst)
3102
387
{
3103
387
  int64_t immr, imms, val;
3104
3105
  /* Should have been assured by the base opcode value.  */
3106
387
  assert (inst->operands[1].reg.regno == 0x1f);
3107
3108
387
  immr = inst->operands[2].imm.value;
3109
387
  imms = inst->operands[3].imm.value;
3110
387
  val = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 32 : 64;
3111
387
  if (imms < immr)
3112
132
    {
3113
      /* Drop XZR from the second operand.  */
3114
132
      copy_operand_info (inst, 1, 2);
3115
132
      copy_operand_info (inst, 2, 3);
3116
132
      inst->operands[3].type = AARCH64_OPND_NIL;
3117
3118
      /* Recalculate the immediates.  */
3119
132
      inst->operands[1].imm.value = (val - immr) & (val - 1);
3120
132
      inst->operands[2].imm.value = imms + 1;
3121
3122
      /* The two opcodes have different qualifiers for the operands; reset to
3123
   help the checking.  */
3124
132
      reset_operand_qualifier (inst, 1);
3125
132
      reset_operand_qualifier (inst, 2);
3126
132
      reset_operand_qualifier (inst, 3);
3127
3128
132
      return 1;
3129
132
    }
3130
3131
255
  return 0;
3132
387
}
3133
3134
/* The instruction written:
3135
     LSL <Xd>, <Xn>, #<shift>
3136
   is equivalent to:
3137
     UBFM <Xd>, <Xn>, #((64-<shift>)&0x3f), #(63-<shift>).  */
3138
3139
static int
3140
convert_ubfm_to_lsl (aarch64_inst *inst)
3141
7.39k
{
3142
7.39k
  int64_t immr = inst->operands[2].imm.value;
3143
7.39k
  int64_t imms = inst->operands[3].imm.value;
3144
7.39k
  int64_t val
3145
7.39k
    = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 31 : 63;
3146
3147
7.39k
  if ((immr == 0 && imms == val) || immr == imms + 1)
3148
173
    {
3149
173
      inst->operands[3].type = AARCH64_OPND_NIL;
3150
173
      inst->operands[2].imm.value = val - imms;
3151
173
      return 1;
3152
173
    }
3153
3154
7.21k
  return 0;
3155
7.39k
}
3156
3157
/* CINC <Wd>, <Wn>, <cond>
3158
     is equivalent to:
3159
   CSINC <Wd>, <Wn>, <Wn>, invert(<cond>)
3160
     where <cond> is not AL or NV.  */
3161
3162
static int
3163
convert_from_csel (aarch64_inst *inst)
3164
3.49k
{
3165
3.49k
  if (inst->operands[1].reg.regno == inst->operands[2].reg.regno
3166
461
      && (inst->operands[3].cond->value & 0xe) != 0xe)
3167
229
    {
3168
229
      copy_operand_info (inst, 2, 3);
3169
229
      inst->operands[2].cond = get_inverted_cond (inst->operands[3].cond);
3170
229
      inst->operands[3].type = AARCH64_OPND_NIL;
3171
229
      return 1;
3172
229
    }
3173
3.26k
  return 0;
3174
3.49k
}
3175
3176
/* CSET <Wd>, <cond>
3177
     is equivalent to:
3178
   CSINC <Wd>, WZR, WZR, invert(<cond>)
3179
     where <cond> is not AL or NV.  */
3180
3181
static int
3182
convert_csinc_to_cset (aarch64_inst *inst)
3183
405
{
3184
405
  if (inst->operands[1].reg.regno == 0x1f
3185
405
      && inst->operands[2].reg.regno == 0x1f
3186
405
      && (inst->operands[3].cond->value & 0xe) != 0xe)
3187
177
    {
3188
177
      copy_operand_info (inst, 1, 3);
3189
177
      inst->operands[1].cond = get_inverted_cond (inst->operands[3].cond);
3190
177
      inst->operands[3].type = AARCH64_OPND_NIL;
3191
177
      inst->operands[2].type = AARCH64_OPND_NIL;
3192
177
      return 1;
3193
177
    }
3194
228
  return 0;
3195
405
}
3196
3197
/* MOV <Wd>, #<imm>
3198
     is equivalent to:
3199
   MOVZ <Wd>, #<imm16_5>, LSL #<shift>.
3200
3201
   A disassembler may output ORR, MOVZ and MOVN as a MOV mnemonic, except when
3202
   ORR has an immediate that could be generated by a MOVZ or MOVN instruction,
3203
   or where a MOVN has an immediate that could be encoded by MOVZ, or where
3204
   MOVZ/MOVN #0 have a shift amount other than LSL #0, in which case the
3205
   machine-instruction mnemonic must be used.  */
3206
3207
static int
3208
convert_movewide_to_mov (aarch64_inst *inst)
3209
54.7k
{
3210
54.7k
  uint64_t value = inst->operands[1].imm.value;
3211
  /* MOVZ/MOVN #0 have a shift amount other than LSL #0.  */
3212
54.7k
  if (value == 0 && inst->operands[1].shifter.amount != 0)
3213
189
    return 0;
3214
54.5k
  inst->operands[1].type = AARCH64_OPND_IMM_MOV;
3215
54.5k
  inst->operands[1].shifter.kind = AARCH64_MOD_NONE;
3216
54.5k
  value <<= inst->operands[1].shifter.amount;
3217
  /* As an alias convertor, it has to be clear that the INST->OPCODE
3218
     is the opcode of the real instruction.  */
3219
54.5k
  if (inst->opcode->op == OP_MOVN)
3220
29.3k
    {
3221
29.3k
      int is32 = inst->operands[0].qualifier == AARCH64_OPND_QLF_W;
3222
29.3k
      value = ~value;
3223
      /* A MOVN has an immediate that could be encoded by MOVZ.  */
3224
29.3k
      if (aarch64_wide_constant_p (value, is32, NULL))
3225
107
  return 0;
3226
29.3k
    }
3227
54.4k
  inst->operands[1].imm.value = value;
3228
54.4k
  inst->operands[1].shifter.amount = 0;
3229
54.4k
  return 1;
3230
54.5k
}
3231
3232
/* MOV <Wd>, #<imm>
3233
     is equivalent to:
3234
   ORR <Wd>, WZR, #<imm>.
3235
3236
   A disassembler may output ORR, MOVZ and MOVN as a MOV mnemonic, except when
3237
   ORR has an immediate that could be generated by a MOVZ or MOVN instruction,
3238
   or where a MOVN has an immediate that could be encoded by MOVZ, or where
3239
   MOVZ/MOVN #0 have a shift amount other than LSL #0, in which case the
3240
   machine-instruction mnemonic must be used.  */
3241
3242
static int
3243
convert_movebitmask_to_mov (aarch64_inst *inst)
3244
1.11k
{
3245
1.11k
  int is32;
3246
1.11k
  uint64_t value;
3247
3248
  /* Should have been assured by the base opcode value.  */
3249
1.11k
  assert (inst->operands[1].reg.regno == 0x1f);
3250
1.11k
  copy_operand_info (inst, 1, 2);
3251
1.11k
  is32 = inst->operands[0].qualifier == AARCH64_OPND_QLF_W;
3252
1.11k
  inst->operands[1].type = AARCH64_OPND_IMM_MOV;
3253
1.11k
  value = inst->operands[1].imm.value;
3254
  /* ORR has an immediate that could be generated by a MOVZ or MOVN
3255
     instruction.  */
3256
1.11k
  if (inst->operands[0].reg.regno != 0x1f
3257
806
      && (aarch64_wide_constant_p (value, is32, NULL)
3258
583
    || aarch64_wide_constant_p (~value, is32, NULL)))
3259
359
    return 0;
3260
3261
753
  inst->operands[2].type = AARCH64_OPND_NIL;
3262
753
  return 1;
3263
1.11k
}
3264
3265
/* Some alias opcodes are disassembled by being converted from their real-form.
3266
   N.B. INST->OPCODE is the real opcode rather than the alias.  */
3267
3268
static int
3269
convert_to_alias (aarch64_inst *inst, const aarch64_opcode *alias)
3270
135k
{
3271
135k
  switch (alias->op)
3272
135k
    {
3273
13.7k
    case OP_ASR_IMM:
3274
21.2k
    case OP_LSR_IMM:
3275
21.2k
      return convert_bfm_to_sr (inst);
3276
7.39k
    case OP_LSL_IMM:
3277
7.39k
      return convert_ubfm_to_lsl (inst);
3278
1.12k
    case OP_CINC:
3279
2.52k
    case OP_CINV:
3280
3.49k
    case OP_CNEG:
3281
3.49k
      return convert_from_csel (inst);
3282
177
    case OP_CSET:
3283
405
    case OP_CSETM:
3284
405
      return convert_csinc_to_cset (inst);
3285
7.21k
    case OP_UBFX:
3286
14.6k
    case OP_BFXIL:
3287
27.9k
    case OP_SBFX:
3288
27.9k
      return convert_bfm_to_bfx (inst);
3289
8.58k
    case OP_SBFIZ:
3290
11.2k
    case OP_BFI:
3291
14.7k
    case OP_UBFIZ:
3292
14.7k
      return convert_bfm_to_bfi (inst);
3293
387
    case OP_BFC:
3294
387
      return convert_bfm_to_bfc (inst);
3295
659
    case OP_MOV_V:
3296
659
      return convert_orr_to_mov (inst);
3297
25.3k
    case OP_MOV_IMM_WIDE:
3298
54.7k
    case OP_MOV_IMM_WIDEN:
3299
54.7k
      return convert_movewide_to_mov (inst);
3300
1.11k
    case OP_MOV_IMM_LOG:
3301
1.11k
      return convert_movebitmask_to_mov (inst);
3302
2.69k
    case OP_ROR_IMM:
3303
2.69k
      return convert_extr_to_ror (inst);
3304
37
    case OP_SXTL:
3305
92
    case OP_SXTL2:
3306
716
    case OP_UXTL:
3307
795
    case OP_UXTL2:
3308
795
      return convert_shll_to_xtl (inst);
3309
0
    default:
3310
0
      return 0;
3311
135k
    }
3312
135k
}
3313
3314
static bool
3315
aarch64_opcode_decode (const aarch64_opcode *, const aarch64_insn,
3316
           aarch64_inst *, int, aarch64_operand_error *errors);
3317
3318
/* Given the instruction information in *INST, check if the instruction has
3319
   any alias form that can be used to represent *INST.  If the answer is yes,
3320
   update *INST to be in the form of the determined alias.  */
3321
3322
/* In the opcode description table, the following flags are used in opcode
3323
   entries to help establish the relations between the real and alias opcodes:
3324
3325
  F_ALIAS:  opcode is an alias
3326
  F_HAS_ALIAS:  opcode has alias(es)
3327
  F_P1
3328
  F_P2
3329
  F_P3:   Disassembly preference priority 1-3 (the larger the
3330
      higher).  If nothing is specified, it is the priority
3331
      0 by default, i.e. the lowest priority.
3332
3333
   Although the relation between the machine and the alias instructions are not
3334
   explicitly described, it can be easily determined from the base opcode
3335
   values, masks and the flags F_ALIAS and F_HAS_ALIAS in their opcode
3336
   description entries:
3337
3338
   The mask of an alias opcode must be equal to or a super-set (i.e. more
3339
   constrained) of that of the aliased opcode; so is the base opcode value.
3340
3341
   if (opcode_has_alias (real) && alias_opcode_p (opcode)
3342
       && (opcode->mask & real->mask) == real->mask
3343
       && (real->mask & opcode->opcode) == (real->mask & real->opcode))
3344
   then OPCODE is an alias of, and only of, the REAL instruction
3345
3346
   The alias relationship is forced flat-structured to keep related algorithm
3347
   simple; an opcode entry cannot be flagged with both F_ALIAS and F_HAS_ALIAS.
3348
3349
   During the disassembling, the decoding decision tree (in
3350
   opcodes/aarch64-dis-2.c) always returns an machine instruction opcode entry;
3351
   if the decoding of such a machine instruction succeeds (and -Mno-aliases is
3352
   not specified), the disassembler will check whether there is any alias
3353
   instruction exists for this real instruction.  If there is, the disassembler
3354
   will try to disassemble the 32-bit binary again using the alias's rule, or
3355
   try to convert the IR to the form of the alias.  In the case of the multiple
3356
   aliases, the aliases are tried one by one from the highest priority
3357
   (currently the flag F_P3) to the lowest priority (no priority flag), and the
3358
   first succeeds first adopted.
3359
3360
   You may ask why there is a need for the conversion of IR from one form to
3361
   another in handling certain aliases.  This is because on one hand it avoids
3362
   adding more operand code to handle unusual encoding/decoding; on other
3363
   hand, during the disassembling, the conversion is an effective approach to
3364
   check the condition of an alias (as an alias may be adopted only if certain
3365
   conditions are met).
3366
3367
   In order to speed up the alias opcode lookup, aarch64-gen has preprocessed
3368
   aarch64_opcode_table and generated aarch64_find_alias_opcode and
3369
   aarch64_find_next_alias_opcode (in opcodes/aarch64-dis-2.c) to help.  */
3370
3371
static void
3372
determine_disassembling_preference (struct aarch64_inst *inst,
3373
            aarch64_operand_error *errors)
3374
6.18M
{
3375
6.18M
  const aarch64_opcode *opcode;
3376
6.18M
  const aarch64_opcode *alias;
3377
3378
6.18M
  opcode = inst->opcode;
3379
3380
  /* This opcode does not have an alias, so use itself.  */
3381
6.18M
  if (!opcode_has_alias (opcode))
3382
5.42M
    return;
3383
3384
755k
  alias = aarch64_find_alias_opcode (opcode);
3385
755k
  assert (alias);
3386
3387
#ifdef DEBUG_AARCH64
3388
  if (debug_dump)
3389
    {
3390
      const aarch64_opcode *tmp = alias;
3391
      printf ("####   LIST    orderd: ");
3392
      while (tmp)
3393
  {
3394
    printf ("%s, ", tmp->name);
3395
    tmp = aarch64_find_next_alias_opcode (tmp);
3396
  }
3397
      printf ("\n");
3398
    }
3399
#endif /* DEBUG_AARCH64 */
3400
3401
1.55M
  for (; alias; alias = aarch64_find_next_alias_opcode (alias))
3402
1.55M
    {
3403
1.55M
      DEBUG_TRACE ("try %s", alias->name);
3404
1.55M
      assert (alias_opcode_p (alias) || opcode_has_alias (opcode));
3405
3406
      /* An alias can be a pseudo opcode which will never be used in the
3407
   disassembly, e.g. BIC logical immediate is such a pseudo opcode
3408
   aliasing AND.  */
3409
1.55M
      if (pseudo_opcode_p (alias))
3410
138k
  {
3411
138k
    DEBUG_TRACE ("skip pseudo %s", alias->name);
3412
138k
    continue;
3413
138k
  }
3414
3415
1.41M
      if ((inst->value & alias->mask) != alias->opcode)
3416
569k
  {
3417
569k
    DEBUG_TRACE ("skip %s as base opcode not match", alias->name);
3418
569k
    continue;
3419
569k
  }
3420
3421
843k
      if (!AARCH64_CPU_HAS_ALL_FEATURES (arch_variant, *alias->avariant))
3422
53
  {
3423
53
    DEBUG_TRACE ("skip %s: we're missing features", alias->name);
3424
53
    continue;
3425
53
  }
3426
3427
      /* No need to do any complicated transformation on operands, if the alias
3428
   opcode does not have any operand.  */
3429
843k
      if (aarch64_num_of_operands (alias) == 0 && alias->opcode == inst->value)
3430
167
  {
3431
167
    DEBUG_TRACE ("succeed with 0-operand opcode %s", alias->name);
3432
167
    aarch64_replace_opcode (inst, alias);
3433
167
    return;
3434
167
  }
3435
843k
      if (alias->flags & F_CONV)
3436
135k
  {
3437
135k
    aarch64_inst copy;
3438
135k
    memcpy (&copy, inst, sizeof (aarch64_inst));
3439
    /* ALIAS is the preference as long as the instruction can be
3440
       successfully converted to the form of ALIAS.  */
3441
135k
    if (convert_to_alias (&copy, alias) == 1)
3442
85.4k
      {
3443
85.4k
        aarch64_replace_opcode (&copy, alias);
3444
85.4k
        if (aarch64_match_operands_constraint (&copy, NULL) != 1)
3445
0
    {
3446
0
      DEBUG_TRACE ("FAILED with alias %s ", alias->name);
3447
0
    }
3448
85.4k
        else
3449
85.4k
    {
3450
85.4k
      DEBUG_TRACE ("succeed with %s via conversion", alias->name);
3451
85.4k
      memcpy (inst, &copy, sizeof (aarch64_inst));
3452
85.4k
    }
3453
85.4k
        return;
3454
85.4k
      }
3455
135k
  }
3456
707k
      else
3457
707k
  {
3458
    /* Directly decode the alias opcode.  */
3459
707k
    aarch64_inst temp;
3460
707k
    memset (&temp, '\0', sizeof (aarch64_inst));
3461
707k
    if (aarch64_opcode_decode (alias, inst->value, &temp, 1, errors) == 1)
3462
669k
      {
3463
669k
        DEBUG_TRACE ("succeed with %s via direct decoding", alias->name);
3464
669k
        memcpy (inst, &temp, sizeof (aarch64_inst));
3465
669k
        return;
3466
669k
      }
3467
707k
  }
3468
843k
    }
3469
755k
}
3470
3471
/* Some instructions (including all SVE ones) use the instruction class
3472
   to describe how a qualifiers_list index is represented in the instruction
3473
   encoding.  If INST is such an instruction, decode the appropriate fields
3474
   and fill in the operand qualifiers accordingly.  Return true if no
3475
   problems are found.  */
3476
3477
static bool
3478
aarch64_decode_variant_using_iclass (aarch64_inst *inst)
3479
7.60M
{
3480
7.60M
  int i, variant;
3481
3482
7.60M
  variant = 0;
3483
7.60M
  switch (inst->opcode->iclass)
3484
7.60M
    {
3485
71.3k
    case sme_mov:
3486
71.3k
      variant = extract_fields (inst->value, 0, 2, FLD_SME_Q, FLD_SME_size_22);
3487
71.3k
      if (variant >= 4 && variant < 7)
3488
1.15k
  return false;
3489
70.1k
      if (variant == 7)
3490
780
  variant = 4;
3491
70.1k
      break;
3492
3493
7.09k
    case sme_psel:
3494
7.09k
      i = extract_fields (inst->value, 0, 2, FLD_SME_tszh, FLD_SME_tszl);
3495
7.09k
      if (i == 0)
3496
166
  return false;
3497
13.7k
      while ((i & 1) == 0)
3498
6.77k
  {
3499
6.77k
    i >>= 1;
3500
6.77k
    variant += 1;
3501
6.77k
  }
3502
6.93k
      break;
3503
3504
644
    case sme_shift:
3505
644
      i = extract_field (FLD_SVE_tszh, inst->value, 0);
3506
644
      goto sve_shift;
3507
3508
151
    case sme_size_12_bh:
3509
151
      variant = extract_field (FLD_S, inst->value, 0);
3510
151
      if (variant > 1)
3511
0
  return false;
3512
151
      break;
3513
3514
1.58k
    case sme_size_12_bhs:
3515
1.58k
      variant = extract_field (FLD_SME_size_12, inst->value, 0);
3516
1.58k
      if (variant >= 3)
3517
373
  return false;
3518
1.20k
      break;
3519
3520
1.20k
    case sme_size_12_hs:
3521
328
      variant = extract_field (FLD_SME_size_12, inst->value, 0);
3522
328
      if (variant != 1 && variant != 2)
3523
70
  return false;
3524
258
      variant -= 1;
3525
258
      break;
3526
3527
114
    case sme_size_12_b:
3528
114
      variant = extract_field (FLD_SME_size_12, inst->value, 0);
3529
114
      if (variant != 0)
3530
91
  return false;
3531
23
      break;
3532
3533
9.61k
    case sme_size_22:
3534
9.61k
      variant = extract_field (FLD_SME_size_22, inst->value, 0);
3535
9.61k
      break;
3536
3537
2.60k
    case sme_size_22_hsd:
3538
2.60k
      variant = extract_field (FLD_SME_size_22, inst->value, 0);
3539
2.60k
      if (variant < 1)
3540
856
  return false;
3541
1.74k
      variant -= 1;
3542
1.74k
      break;
3543
3544
173
    case sme_sz_23:
3545
173
      variant = extract_field (FLD_SME_sz_23, inst->value, 0);
3546
173
      break;
3547
3548
13.7k
    case sve_cpy:
3549
13.7k
      variant = extract_fields (inst->value, 0, 2, FLD_size, FLD_SVE_M_14);
3550
13.7k
      break;
3551
3552
3.34k
    case sve_index:
3553
3.34k
      i = extract_field (FLD_imm5, inst->value, 0);
3554
3555
3.34k
      if ((i & 31) == 0)
3556
421
  return false;
3557
6.08k
      while ((i & 1) == 0)
3558
3.16k
  {
3559
3.16k
    i >>= 1;
3560
3.16k
    variant += 1;
3561
3.16k
  }
3562
2.92k
      break;
3563
3564
41.8k
    case sve_limm:
3565
      /* Pick the smallest applicable element size.  */
3566
41.8k
      if ((inst->value & 0x20600) == 0x600)
3567
7.44k
  variant = 0;
3568
34.3k
      else if ((inst->value & 0x20400) == 0x400)
3569
3.94k
  variant = 1;
3570
30.4k
      else if ((inst->value & 0x20000) == 0)
3571
27.2k
  variant = 2;
3572
3.13k
      else
3573
3.13k
  variant = 3;
3574
41.8k
      break;
3575
3576
1.16k
    case sme2_mov:
3577
      /* .D is preferred over the other sizes in disassembly.  */
3578
1.16k
      variant = 3;
3579
1.16k
      break;
3580
3581
130k
    case sme_misc:
3582
552k
    case sve_misc:
3583
      /* These instructions have only a single variant.  */
3584
552k
      break;
3585
3586
3.50k
    case sve_movprfx:
3587
3.50k
      variant = extract_fields (inst->value, 0, 2, FLD_size, FLD_SVE_M_16);
3588
3.50k
      break;
3589
3590
183
    case sve_pred_zm:
3591
183
      variant = extract_field (FLD_SVE_M_4, inst->value, 0);
3592
183
      break;
3593
3594
4.63k
    case sve_shift_pred:
3595
4.63k
      i = extract_fields (inst->value, 0, 2, FLD_SVE_tszh, FLD_SVE_tszl_8);
3596
6.43k
    sve_shift:
3597
6.43k
      if (i == 0)
3598
2.76k
  return false;
3599
9.91k
      while (i != 1)
3600
6.24k
  {
3601
6.24k
    i >>= 1;
3602
6.24k
    variant += 1;
3603
6.24k
  }
3604
3.66k
      break;
3605
3606
1.15k
    case sve_shift_unpred:
3607
1.15k
      i = extract_fields (inst->value, 0, 2, FLD_SVE_tszh, FLD_SVE_tszl_19);
3608
1.15k
      goto sve_shift;
3609
3610
11.6k
    case sve_size_bhs:
3611
11.6k
      variant = extract_field (FLD_size, inst->value, 0);
3612
11.6k
      if (variant >= 3)
3613
2.70k
  return false;
3614
8.95k
      break;
3615
3616
156k
    case sve_size_bhsd:
3617
156k
      variant = extract_field (FLD_size, inst->value, 0);
3618
156k
      break;
3619
3620
174k
    case sve_size_hsd:
3621
174k
      i = extract_field (FLD_size, inst->value, 0);
3622
174k
      if (i < 1)
3623
38.0k
  return false;
3624
136k
      variant = i - 1;
3625
136k
      break;
3626
3627
670
    case sme_fp_sd:
3628
4.66k
    case sme_int_sd:
3629
5.59k
    case sve_size_bh:
3630
12.1k
    case sve_size_sd:
3631
12.1k
      variant = extract_field (FLD_SVE_sz, inst->value, 0);
3632
12.1k
      break;
3633
3634
5.92k
    case sve_size_sd2:
3635
5.92k
      variant = extract_field (FLD_SVE_sz2, inst->value, 0);
3636
5.92k
      break;
3637
3638
670
    case sve_size_sd3:
3639
670
      variant = extract_field (FLD_SVE_sz3, inst->value, 0);
3640
670
      break;
3641
3642
150
    case sve_size_sd4:
3643
150
      variant = extract_field (FLD_SVE_sz4, inst->value, 0);
3644
150
      break;
3645
3646
289
    case sve_size_hsd2:
3647
289
      i = extract_field (FLD_SVE_size, inst->value, 0);
3648
289
      if (i < 1)
3649
119
  return false;
3650
170
      variant = i - 1;
3651
170
      break;
3652
3653
145
    case sve_size_hsd3:
3654
145
      i = extract_field (FLD_len, inst->value, 0);
3655
145
      if (i < 1)
3656
32
  return false;
3657
113
      variant = i - 1;
3658
113
      break;
3659
3660
1.05k
    case sve_size_13:
3661
      /* Ignore low bit of this field since that is set in the opcode for
3662
   instructions of this iclass.  */
3663
1.05k
      i = (extract_field (FLD_size, inst->value, 0) & 2);
3664
1.05k
      variant = (i >> 1);
3665
1.05k
      break;
3666
3667
2.32k
    case sve_shift_tsz_bhsd:
3668
2.32k
      i = extract_fields (inst->value, 0, 2, FLD_SVE_tszh, FLD_SVE_tszl_19);
3669
2.32k
      if (i == 0)
3670
449
  return false;
3671
4.48k
      while (i != 1)
3672
2.60k
  {
3673
2.60k
    i >>= 1;
3674
2.60k
    variant += 1;
3675
2.60k
  }
3676
1.87k
      break;
3677
3678
681
    case sve_size_tsz_bhs:
3679
681
      i = extract_fields (inst->value, 0, 2, FLD_SVE_sz, FLD_SVE_tszl_19);
3680
681
      if (i == 0)
3681
155
  return false;
3682
928
      while (i != 1)
3683
448
  {
3684
448
    if (i & 1)
3685
46
      return false;
3686
402
    i >>= 1;
3687
402
    variant += 1;
3688
402
  }
3689
480
      break;
3690
3691
5.99k
    case sve_shift_tsz_hsd:
3692
      /* This is also used for some instructions with hs variants only, in
3693
      which case FLD_SVE_sz will always be zero.  */
3694
5.99k
      i = extract_fields (inst->value, 0, 2, FLD_SVE_sz, FLD_SVE_tszl_19);
3695
5.99k
      if (i == 0)
3696
1.06k
  return false;
3697
12.2k
      while (i != 1)
3698
7.36k
  {
3699
7.36k
    i >>= 1;
3700
7.36k
    variant += 1;
3701
7.36k
  }
3702
4.92k
      break;
3703
3704
6.51M
    default:
3705
      /* No mapping between instruction class and qualifiers.  */
3706
6.51M
      return true;
3707
7.60M
    }
3708
3709
8.31M
  for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
3710
7.27M
    inst->operands[i].qualifier = inst->opcode->qualifiers_list[variant][i];
3711
1.03M
  return true;
3712
7.60M
}
3713
/* Decode the CODE according to OPCODE; fill INST.  Return 0 if the decoding
3714
   fails, which meanes that CODE is not an instruction of OPCODE; otherwise
3715
   return 1.
3716
3717
   If OPCODE has alias(es) and NOALIASES_P is 0, an alias opcode may be
3718
   determined and used to disassemble CODE; this is done just before the
3719
   return.  */
3720
3721
static bool
3722
aarch64_opcode_decode (const aarch64_opcode *opcode, const aarch64_insn code,
3723
           aarch64_inst *inst, int noaliases_p,
3724
           aarch64_operand_error *errors)
3725
17.7M
{
3726
17.7M
  int i;
3727
3728
17.7M
  DEBUG_TRACE ("enter with %s", opcode->name);
3729
3730
17.7M
  assert (opcode && inst);
3731
3732
  /* Clear inst.  */
3733
17.7M
  memset (inst, '\0', sizeof (aarch64_inst));
3734
3735
  /* Check the base opcode.  */
3736
17.7M
  if ((code & opcode->mask) != (opcode->opcode & opcode->mask))
3737
10.0M
    {
3738
10.0M
      DEBUG_TRACE ("base opcode match FAIL");
3739
10.0M
      goto decode_fail;
3740
10.0M
    }
3741
3742
7.72M
  inst->opcode = opcode;
3743
7.72M
  inst->value = code;
3744
3745
  /* Assign operand codes and indexes, and set qualifiers to UNKNOWN.  */
3746
26.1M
  for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
3747
26.1M
    {
3748
26.1M
      if (opcode->operands[i] == AARCH64_OPND_NIL)
3749
7.72M
  break;
3750
18.4M
      inst->operands[i].type = opcode->operands[i];
3751
18.4M
      inst->operands[i].idx = i;
3752
18.4M
      inst->operands[i].qualifier = AARCH64_OPND_QLF_UNKNOWN;
3753
18.4M
    }
3754
3755
  /* Call the opcode decoder indicated by flags.  */
3756
7.72M
  if (opcode_has_special_coder (opcode) && do_special_decoding (inst) == 0)
3757
117k
    {
3758
117k
      DEBUG_TRACE ("opcode flag-based decoder FAIL");
3759
117k
      goto decode_fail;
3760
117k
    }
3761
3762
  /* Possibly use the instruction class to determine the correct
3763
     qualifier.  */
3764
7.60M
  if (!aarch64_decode_variant_using_iclass (inst))
3765
48.4k
    {
3766
48.4k
      DEBUG_TRACE ("iclass-based decoder FAIL");
3767
48.4k
      goto decode_fail;
3768
48.4k
    }
3769
3770
  /* Call operand decoders.  */
3771
24.6M
  for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
3772
24.6M
    {
3773
24.6M
      const aarch64_operand *opnd;
3774
24.6M
      enum aarch64_opnd type;
3775
3776
24.6M
      type = opcode->operands[i];
3777
24.6M
      if (type == AARCH64_OPND_NIL)
3778
7.07M
  break;
3779
17.5M
      opnd = &aarch64_operands[type];
3780
17.5M
      if (operand_has_extractor (opnd)
3781
17.5M
    && (! aarch64_extract_operand (opnd, &inst->operands[i], code, inst,
3782
17.5M
           errors)))
3783
482k
  {
3784
482k
    DEBUG_TRACE ("operand decoder FAIL at operand %d", i);
3785
482k
    goto decode_fail;
3786
482k
  }
3787
17.5M
    }
3788
3789
  /* If the opcode has a verifier, then check it now.  */
3790
7.07M
  if (opcode->verifier
3791
59.4k
      && opcode->verifier (inst, code, 0, false, errors, NULL) != ERR_OK)
3792
8.62k
    {
3793
8.62k
      DEBUG_TRACE ("operand verifier FAIL");
3794
8.62k
      goto decode_fail;
3795
8.62k
    }
3796
3797
  /* Match the qualifiers.  */
3798
7.06M
  if (aarch64_match_operands_constraint (inst, NULL) == 1)
3799
6.85M
    {
3800
      /* Arriving here, the CODE has been determined as a valid instruction
3801
   of OPCODE and *INST has been filled with information of this OPCODE
3802
   instruction.  Before the return, check if the instruction has any
3803
   alias and should be disassembled in the form of its alias instead.
3804
   If the answer is yes, *INST will be updated.  */
3805
6.85M
      if (!noaliases_p)
3806
6.18M
  determine_disassembling_preference (inst, errors);
3807
6.85M
      DEBUG_TRACE ("SUCCESS");
3808
6.85M
      return true;
3809
6.85M
    }
3810
212k
  else
3811
212k
    {
3812
212k
      DEBUG_TRACE ("constraint matching FAIL");
3813
212k
    }
3814
3815
10.9M
 decode_fail:
3816
10.9M
  return false;
3817
7.06M
}
3818

3819
/* This does some user-friendly fix-up to *INST.  It is currently focus on
3820
   the adjustment of qualifiers to help the printed instruction
3821
   recognized/understood more easily.  */
3822
3823
static void
3824
user_friendly_fixup (aarch64_inst *inst)
3825
6.18M
{
3826
6.18M
  switch (inst->opcode->iclass)
3827
6.18M
    {
3828
143k
    case testbranch:
3829
      /* TBNZ Xn|Wn, #uimm6, label
3830
   Test and Branch Not Zero: conditionally jumps to label if bit number
3831
   uimm6 in register Xn is not zero.  The bit number implies the width of
3832
   the register, which may be written and should be disassembled as Wn if
3833
   uimm is less than 32. Limited to a branch offset range of +/- 32KiB.
3834
   */
3835
143k
      if (inst->operands[1].imm.value < 32)
3836
92.5k
  inst->operands[0].qualifier = AARCH64_OPND_QLF_W;
3837
143k
      break;
3838
6.04M
    default: break;
3839
6.18M
    }
3840
6.18M
}
3841
3842
/* Decode INSN and fill in *INST the instruction information.  An alias
3843
   opcode may be filled in *INSN if NOALIASES_P is FALSE.  Return zero on
3844
   success.  */
3845
3846
enum err_type
3847
aarch64_decode_insn (aarch64_insn insn, aarch64_inst *inst,
3848
         bool noaliases_p,
3849
         aarch64_operand_error *errors)
3850
14.8M
{
3851
14.8M
  const aarch64_opcode *opcode = aarch64_opcode_lookup (insn);
3852
3853
#ifdef DEBUG_AARCH64
3854
  if (debug_dump)
3855
    {
3856
      const aarch64_opcode *tmp = opcode;
3857
      printf ("\n");
3858
      DEBUG_TRACE ("opcode lookup:");
3859
      while (tmp != NULL)
3860
  {
3861
    aarch64_verbose ("  %s", tmp->name);
3862
    tmp = aarch64_find_next_opcode (tmp);
3863
  }
3864
    }
3865
#endif /* DEBUG_AARCH64 */
3866
3867
  /* A list of opcodes may have been found, as aarch64_opcode_lookup cannot
3868
     distinguish some opcodes, e.g. SSHR and MOVI, which almost share the same
3869
     opcode field and value, apart from the difference that one of them has an
3870
     extra field as part of the opcode, but such a field is used for operand
3871
     encoding in other opcode(s) ('immh' in the case of the example).  */
3872
25.7M
  while (opcode != NULL)
3873
17.0M
    {
3874
      /* But only one opcode can be decoded successfully for, as the
3875
   decoding routine will check the constraint carefully.  */
3876
17.0M
      if (aarch64_opcode_decode (opcode, insn, inst, noaliases_p, errors) == 1)
3877
6.18M
  return ERR_OK;
3878
10.9M
      opcode = aarch64_find_next_opcode (opcode);
3879
10.9M
    }
3880
3881
8.65M
  return ERR_UND;
3882
14.8M
}
3883
3884
/* Return a short string to indicate a switch to STYLE.  These strings
3885
   will be embedded into the disassembled operand text (as produced by
3886
   aarch64_print_operand), and then spotted in the print_operands function
3887
   so that the disassembler output can be split by style.  */
3888
3889
static const char *
3890
get_style_text (enum disassembler_style style)
3891
33.7M
{
3892
33.7M
  static bool init = false;
3893
33.7M
  static char formats[16][4];
3894
33.7M
  unsigned num;
3895
3896
  /* First time through we build a string for every possible format.  This
3897
     code relies on there being no more than 16 different styles (there's
3898
     an assert below for this).  */
3899
33.7M
  if (!init)
3900
2
    {
3901
2
      int i;
3902
3903
34
      for (i = 0; i <= 0xf; ++i)
3904
32
  {
3905
32
    int res ATTRIBUTE_UNUSED
3906
32
      = snprintf (&formats[i][0], sizeof (formats[i]), "%c%x%c",
3907
32
      STYLE_MARKER_CHAR, i, STYLE_MARKER_CHAR);
3908
32
    assert (res == 3);
3909
32
  }
3910
3911
2
      init = true;
3912
2
    }
3913
3914
  /* Return the string that marks switching to STYLE.  */
3915
33.7M
  num = (unsigned) style;
3916
33.7M
  assert (style <= 0xf);
3917
33.7M
  return formats[num];
3918
33.7M
}
3919
3920
/* Callback used by aarch64_print_operand to apply STYLE to the
3921
   disassembler output created from FMT and ARGS.  The STYLER object holds
3922
   any required state.  Must return a pointer to a string (created from FMT
3923
   and ARGS) that will continue to be valid until the complete disassembled
3924
   instruction has been printed.
3925
3926
   We return a string that includes two embedded style markers, the first,
3927
   places at the start of the string, indicates a switch to STYLE, and the
3928
   second, placed at the end of the string, indicates a switch back to the
3929
   default text style.
3930
3931
   Later, when we print the operand text we take care to collapse any
3932
   adjacent style markers, and to ignore any style markers that appear at
3933
   the very end of a complete operand string.  */
3934
3935
static const char *aarch64_apply_style (struct aarch64_styler *styler,
3936
          enum disassembler_style style,
3937
          const char *fmt,
3938
          va_list args)
3939
16.8M
{
3940
16.8M
  int res;
3941
16.8M
  char *ptr, *tmp;
3942
16.8M
  struct obstack *stack = (struct obstack *) styler->state;
3943
16.8M
  va_list ap;
3944
3945
  /* These are the two strings for switching styles.  */
3946
16.8M
  const char *style_on = get_style_text (style);
3947
16.8M
  const char *style_off = get_style_text (dis_style_text);
3948
3949
  /* Calculate space needed once FMT and ARGS are expanded.  */
3950
16.8M
  va_copy (ap, args);
3951
16.8M
  res = vsnprintf (NULL, 0, fmt, ap);
3952
16.8M
  va_end (ap);
3953
16.8M
  assert (res >= 0);
3954
3955
  /* Allocate space on the obstack for the expanded FMT and ARGS, as well
3956
     as the two strings for switching styles, then write all of these
3957
     strings onto the obstack.  */
3958
16.8M
  ptr = (char *) obstack_alloc (stack, res + strlen (style_on)
3959
16.8M
        + strlen (style_off) + 1);
3960
16.8M
  tmp = stpcpy (ptr, style_on);
3961
16.8M
  res = vsnprintf (tmp, (res + 1), fmt, args);
3962
16.8M
  assert (res >= 0);
3963
16.8M
  tmp += res;
3964
16.8M
  strcpy (tmp, style_off);
3965
3966
16.8M
  return ptr;
3967
16.8M
}
3968
3969
/* Print operands.  */
3970
3971
static void
3972
print_operands (bfd_vma pc, const aarch64_opcode *opcode,
3973
    const aarch64_opnd_info *opnds, struct disassemble_info *info,
3974
    bool *has_notes)
3975
6.18M
{
3976
6.18M
  char *notes = NULL;
3977
6.18M
  int i, pcrel_p, num_printed;
3978
6.18M
  struct aarch64_styler styler;
3979
6.18M
  struct obstack content;
3980
6.18M
  obstack_init (&content);
3981
3982
6.18M
  styler.apply_style = aarch64_apply_style;
3983
6.18M
  styler.state = (void *) &content;
3984
3985
20.0M
  for (i = 0, num_printed = 0; i < AARCH64_MAX_OPND_NUM; ++i)
3986
20.0M
    {
3987
20.0M
      char str[128];
3988
20.0M
      char cmt[128];
3989
3990
      /* We regard the opcode operand info more, however we also look into
3991
   the inst->operands to support the disassembling of the optional
3992
   operand.
3993
   The two operand code should be the same in all cases, apart from
3994
   when the operand can be optional.  */
3995
20.0M
      if (opcode->operands[i] == AARCH64_OPND_NIL
3996
13.8M
    || opnds[i].type == AARCH64_OPND_NIL)
3997
6.18M
  break;
3998
3999
      /* Generate the operand string in STR.  */
4000
13.8M
      aarch64_print_operand (str, sizeof (str), pc, opcode, opnds, i, &pcrel_p,
4001
13.8M
           &info->target, &notes, cmt, sizeof (cmt),
4002
13.8M
           arch_variant, &styler);
4003
4004
      /* Print the delimiter (taking account of omitted operand(s)).  */
4005
13.8M
      if (str[0] != '\0')
4006
13.8M
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s",
4007
13.8M
              num_printed++ == 0 ? "\t" : ", ");
4008
4009
      /* Print the operand.  */
4010
13.8M
      if (pcrel_p)
4011
1.25M
  (*info->print_address_func) (info->target, info);
4012
12.5M
      else
4013
12.5M
  {
4014
    /* This operand came from aarch64_print_operand, and will include
4015
       embedded strings indicating which style each character should
4016
       have.  In the following code we split the text based on
4017
       CURR_STYLE, and call the styled print callback to print each
4018
       block of text in the appropriate style.  */
4019
12.5M
    char *start, *curr;
4020
12.5M
    enum disassembler_style curr_style = dis_style_text;
4021
4022
12.5M
    start = curr = str;
4023
12.5M
    do
4024
107M
      {
4025
107M
        if (*curr == '\0'
4026
94.8M
      || (*curr == STYLE_MARKER_CHAR
4027
94.8M
          && ISXDIGIT (*(curr + 1))
4028
31.2M
          && *(curr + 2) == STYLE_MARKER_CHAR))
4029
43.7M
    {
4030
      /* Output content between our START position and CURR.  */
4031
43.7M
      int len = curr - start;
4032
43.7M
      if (len > 0)
4033
22.5M
        {
4034
22.5M
          if ((*info->fprintf_styled_func) (info->stream,
4035
22.5M
              curr_style,
4036
22.5M
              "%.*s",
4037
22.5M
              len, start) < 0)
4038
0
      break;
4039
22.5M
        }
4040
4041
43.7M
      if (*curr == '\0')
4042
12.5M
        break;
4043
4044
      /* Skip over the initial STYLE_MARKER_CHAR.  */
4045
31.2M
      ++curr;
4046
4047
      /* Update the CURR_STYLE.  As there are less than 16
4048
         styles, it is possible, that if the input is corrupted
4049
         in some way, that we might set CURR_STYLE to an
4050
         invalid value.  Don't worry though, we check for this
4051
         situation.  */
4052
31.2M
      if (*curr >= '0' && *curr <= '9')
4053
31.2M
        curr_style = (enum disassembler_style) (*curr - '0');
4054
0
      else if (*curr >= 'a' && *curr <= 'f')
4055
0
        curr_style = (enum disassembler_style) (*curr - 'a' + 10);
4056
0
      else
4057
0
        curr_style = dis_style_text;
4058
4059
      /* Check for an invalid style having been selected.  This
4060
         should never happen, but it doesn't hurt to be a
4061
         little paranoid.  */
4062
31.2M
      if (curr_style > dis_style_comment_start)
4063
0
        curr_style = dis_style_text;
4064
4065
      /* Skip the hex character, and the closing STYLE_MARKER_CHAR.  */
4066
31.2M
      curr += 2;
4067
4068
      /* Reset the START to after the style marker.  */
4069
31.2M
      start = curr;
4070
31.2M
    }
4071
63.6M
        else
4072
63.6M
    ++curr;
4073
107M
      }
4074
12.5M
    while (true);
4075
12.5M
  }
4076
4077
      /* Print the comment.  This works because only the last operand ever
4078
   adds a comment.  If that ever changes then we'll need to be
4079
   smarter here.  */
4080
13.8M
      if (cmt[0] != '\0')
4081
68.1k
  (*info->fprintf_styled_func) (info->stream, dis_style_comment_start,
4082
68.1k
              "\t// %s", cmt);
4083
13.8M
    }
4084
4085
6.18M
    if (notes && !no_notes)
4086
199
      {
4087
199
  *has_notes = true;
4088
199
  (*info->fprintf_styled_func) (info->stream, dis_style_comment_start,
4089
199
              "  // note: %s", notes);
4090
199
      }
4091
4092
6.18M
    obstack_free (&content, NULL);
4093
6.18M
}
4094
4095
/* Set NAME to a copy of INST's mnemonic with the "." suffix removed.  */
4096
4097
static void
4098
remove_dot_suffix (char *name, const aarch64_inst *inst)
4099
77.0k
{
4100
77.0k
  const char *ptr;
4101
77.0k
  size_t len;
4102
4103
77.0k
  ptr = strchr (inst->opcode->name, '.');
4104
77.0k
  assert (ptr && inst->cond);
4105
77.0k
  len = ptr - inst->opcode->name;
4106
77.0k
  assert (len < 8);
4107
77.0k
  strncpy (name, inst->opcode->name, len);
4108
77.0k
  name[len] = '\0';
4109
77.0k
}
4110
4111
/* Print the instruction mnemonic name.  */
4112
4113
static void
4114
print_mnemonic_name (const aarch64_inst *inst, struct disassemble_info *info)
4115
6.18M
{
4116
6.18M
  if (inst->opcode->flags & F_COND)
4117
38.5k
    {
4118
      /* For instructions that are truly conditionally executed, e.g. b.cond,
4119
   prepare the full mnemonic name with the corresponding condition
4120
   suffix.  */
4121
38.5k
      char name[8];
4122
4123
38.5k
      remove_dot_suffix (name, inst);
4124
38.5k
      (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
4125
38.5k
            "%s.%s", name, inst->cond->names[0]);
4126
38.5k
    }
4127
6.14M
  else
4128
6.14M
    (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
4129
6.14M
          "%s", inst->opcode->name);
4130
6.18M
}
4131
4132
/* Decide whether we need to print a comment after the operands of
4133
   instruction INST.  */
4134
4135
static void
4136
print_comment (const aarch64_inst *inst, struct disassemble_info *info)
4137
6.18M
{
4138
6.18M
  if (inst->opcode->flags & F_COND)
4139
38.5k
    {
4140
38.5k
      char name[8];
4141
38.5k
      unsigned int i, num_conds;
4142
4143
38.5k
      remove_dot_suffix (name, inst);
4144
38.5k
      num_conds = ARRAY_SIZE (inst->cond->names);
4145
70.9k
      for (i = 1; i < num_conds && inst->cond->names[i]; ++i)
4146
32.4k
  (*info->fprintf_styled_func) (info->stream, dis_style_comment_start,
4147
32.4k
              "%s %s.%s",
4148
32.4k
              i == 1 ? "  //" : ",",
4149
32.4k
              name, inst->cond->names[i]);
4150
38.5k
    }
4151
6.18M
}
4152
4153
/* Build notes from verifiers into a string for printing.  */
4154
4155
static void
4156
print_verifier_notes (aarch64_operand_error *detail,
4157
          struct disassemble_info *info)
4158
11.5k
{
4159
11.5k
  if (no_notes)
4160
0
    return;
4161
4162
  /* The output of the verifier cannot be a fatal error, otherwise the assembly
4163
     would not have succeeded.  We can safely ignore these.  */
4164
11.5k
  assert (detail->non_fatal);
4165
4166
11.5k
  (*info->fprintf_styled_func) (info->stream, dis_style_comment_start,
4167
11.5k
        "  // note: ");
4168
11.5k
  switch (detail->kind)
4169
11.5k
    {
4170
2.97k
    case AARCH64_OPDE_A_SHOULD_FOLLOW_B:
4171
2.97k
      (*info->fprintf_styled_func) (info->stream, dis_style_text,
4172
2.97k
            _("this `%s' should have an immediately"
4173
2.97k
              " preceding `%s'"),
4174
2.97k
            detail->data[0].s, detail->data[1].s);
4175
2.97k
      break;
4176
4177
3.86k
    case AARCH64_OPDE_EXPECTED_A_AFTER_B:
4178
3.86k
      (*info->fprintf_styled_func) (info->stream, dis_style_text,
4179
3.86k
            _("expected `%s' after previous `%s'"),
4180
3.86k
            detail->data[0].s, detail->data[1].s);
4181
3.86k
      break;
4182
4183
4.76k
    default:
4184
4.76k
      assert (detail->error);
4185
4.76k
      (*info->fprintf_styled_func) (info->stream, dis_style_text,
4186
4.76k
            "%s", detail->error);
4187
4.76k
      if (detail->index >= 0)
4188
1.35k
  (*info->fprintf_styled_func) (info->stream, dis_style_text,
4189
1.35k
              " at operand %d", detail->index + 1);
4190
4.76k
      break;
4191
11.5k
    }
4192
11.5k
}
4193
4194
/* Print the instruction according to *INST.  */
4195
4196
static void
4197
print_aarch64_insn (bfd_vma pc, const aarch64_inst *inst,
4198
        const aarch64_insn code,
4199
        struct disassemble_info *info,
4200
        aarch64_operand_error *mismatch_details)
4201
6.18M
{
4202
6.18M
  bool has_notes = false;
4203
4204
6.18M
  print_mnemonic_name (inst, info);
4205
6.18M
  print_operands (pc, inst->opcode, inst->operands, info, &has_notes);
4206
6.18M
  print_comment (inst, info);
4207
4208
  /* We've already printed a note, not enough space to print more so exit.
4209
     Usually notes shouldn't overlap so it shouldn't happen that we have a note
4210
     from a register and instruction at the same time.  */
4211
6.18M
  if (has_notes)
4212
199
    return;
4213
4214
  /* Always run constraint verifiers, this is needed because constraints need to
4215
     maintain a global state regardless of whether the instruction has the flag
4216
     set or not.  */
4217
6.18M
  enum err_type result = verify_constraints (inst, code, pc, false,
4218
6.18M
               mismatch_details, &insn_sequence);
4219
6.18M
  switch (result)
4220
6.18M
    {
4221
11.5k
    case ERR_VFI:
4222
11.5k
      print_verifier_notes (mismatch_details, info);
4223
11.5k
      break;
4224
0
    case ERR_UND:
4225
0
    case ERR_UNP:
4226
6.17M
    default:
4227
6.17M
      break;
4228
6.18M
    }
4229
6.18M
}
4230
4231
/* Entry-point of the instruction disassembler and printer.  */
4232
4233
static void
4234
print_insn_aarch64_word (bfd_vma pc,
4235
       uint32_t word,
4236
       struct disassemble_info *info,
4237
       aarch64_operand_error *errors)
4238
14.8M
{
4239
14.8M
  static const char *err_msg[ERR_NR_ENTRIES+1] =
4240
14.8M
    {
4241
14.8M
      [ERR_OK]  = "_",
4242
14.8M
      [ERR_UND] = "undefined",
4243
14.8M
      [ERR_UNP] = "unpredictable",
4244
14.8M
    };
4245
4246
14.8M
  enum err_type ret;
4247
14.8M
  aarch64_inst inst;
4248
4249
14.8M
  info->insn_info_valid = 1;
4250
14.8M
  info->branch_delay_insns = 0;
4251
14.8M
  info->data_size = 0;
4252
14.8M
  info->target = 0;
4253
14.8M
  info->target2 = 0;
4254
4255
14.8M
  if (info->flags & INSN_HAS_RELOC)
4256
    /* If the instruction has a reloc associated with it, then
4257
       the offset field in the instruction will actually be the
4258
       addend for the reloc.  (If we are using REL type relocs).
4259
       In such cases, we can ignore the pc when computing
4260
       addresses, since the addend is not currently pc-relative.  */
4261
0
    pc = 0;
4262
4263
14.8M
  ret = aarch64_decode_insn (word, &inst, no_aliases, errors);
4264
4265
14.8M
  switch (ret)
4266
14.8M
    {
4267
8.65M
    case ERR_UND:
4268
8.65M
    case ERR_UNP:
4269
      /* Handle undefined instructions.  */
4270
8.65M
      info->insn_type = dis_noninsn;
4271
8.65M
      (*info->fprintf_styled_func) (info->stream,
4272
8.65M
            dis_style_assembler_directive,
4273
8.65M
            ".inst\t");
4274
8.65M
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
4275
8.65M
            "0x%08x", word);
4276
8.65M
      asymbol * sym = NULL;
4277
      /* See if this "instruction" is actually the address of something.  */
4278
8.65M
      if (annotate_undefined_insns
4279
    /* Skip values that have been explicitly tagged as code.  */
4280
0
    && last_type == MAP_DATA
4281
    /* Skip static object files as symbol values have not be resolved yet.  */
4282
0
    && info->section != NULL
4283
0
    && info->section->owner != NULL
4284
0
    && (info->section->owner->flags & (EXEC_P | DYNAMIC)))
4285
0
  {
4286
0
    sym = info->symbol_at_address_func (word, info);
4287
0
    if (sym != NULL)
4288
0
      info->fprintf_styled_func (info->stream, dis_style_symbol,
4289
0
               " ; [%s]", sym->name);
4290
0
  }
4291
8.65M
      if (sym == NULL)
4292
8.65M
  info->fprintf_styled_func (info->stream, dis_style_comment_start,
4293
8.65M
           " ; %s", err_msg[ret]);
4294
8.65M
      break;
4295
6.18M
    case ERR_OK:
4296
6.18M
      user_friendly_fixup (&inst);
4297
6.18M
      if (inst.opcode->iclass == condbranch
4298
6.14M
    || inst.opcode->iclass == testbranch
4299
6.00M
    || inst.opcode->iclass == compbranch)
4300
411k
        info->insn_type = dis_condbranch;
4301
5.77M
      else if (inst.opcode->iclass == branch_imm)
4302
219k
        info->insn_type = dis_jsr;
4303
6.18M
      print_aarch64_insn (pc, &inst, word, info, errors);
4304
6.18M
      break;
4305
0
    default:
4306
0
      abort ();
4307
14.8M
    }
4308
14.8M
}
4309
4310
/* Disallow mapping symbols ($x, $d etc) from
4311
   being displayed in symbol relative addresses.  */
4312
4313
bool
4314
aarch64_symbol_is_valid (asymbol * sym,
4315
       struct disassemble_info * info ATTRIBUTE_UNUSED)
4316
3
{
4317
3
  const char * name;
4318
4319
3
  if (sym == NULL)
4320
0
    return false;
4321
4322
3
  name = bfd_asymbol_name (sym);
4323
4324
3
  return name
4325
3
    && (name[0] != '$'
4326
0
  || (name[1] != 'x' && name[1] != 'd')
4327
0
  || (name[2] != '\0' && name[2] != '.'));
4328
3
}
4329
4330
/* Print data bytes on INFO->STREAM.  */
4331
4332
static void
4333
print_insn_data (bfd_vma pc ATTRIBUTE_UNUSED,
4334
     uint32_t word,
4335
     struct disassemble_info *info,
4336
     aarch64_operand_error *errors ATTRIBUTE_UNUSED)
4337
0
{
4338
0
  switch (info->bytes_per_chunk)
4339
0
    {
4340
0
    case 1:
4341
0
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive,
4342
0
         ".byte\t");
4343
0
      info->fprintf_styled_func (info->stream, dis_style_immediate,
4344
0
         "0x%02x", word);
4345
0
      break;
4346
0
    case 2:
4347
0
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive,
4348
0
         ".short\t");
4349
0
      info->fprintf_styled_func (info->stream, dis_style_immediate,
4350
0
         "0x%04x", word);
4351
0
      break;
4352
0
    case 4:
4353
0
      info->fprintf_styled_func (info->stream, dis_style_assembler_directive,
4354
0
         ".word\t");
4355
0
      info->fprintf_styled_func (info->stream, dis_style_immediate,
4356
0
         "0x%08x", word);
4357
0
      break;
4358
0
    default:
4359
0
      abort ();
4360
0
    }
4361
0
}
4362
4363
/* Try to infer the code or data type from a symbol.
4364
   Returns nonzero if *MAP_TYPE was set.  */
4365
4366
static int
4367
get_sym_code_type (struct disassemble_info *info, int n,
4368
       enum map_type *map_type)
4369
27
{
4370
27
  asymbol * as;
4371
27
  elf_symbol_type *es;
4372
27
  unsigned int type;
4373
27
  const char *name;
4374
4375
  /* If the symbol is in a different section, ignore it.  */
4376
27
  if (info->section != NULL && info->section != info->symtab[n]->section)
4377
0
    return false;
4378
4379
27
  if (n >= info->symtab_size)
4380
0
    return false;
4381
4382
27
  as = info->symtab[n];
4383
27
  if (bfd_asymbol_flavour (as) != bfd_target_elf_flavour)
4384
0
    return false;
4385
27
  es = (elf_symbol_type *) as;
4386
4387
27
  type = ELF_ST_TYPE (es->internal_elf_sym.st_info);
4388
4389
  /* If the symbol has function type then use that.  */
4390
27
  if (type == STT_FUNC)
4391
27
    {
4392
27
      *map_type = MAP_INSN;
4393
27
      return true;
4394
27
    }
4395
4396
  /* Check for mapping symbols.  */
4397
0
  name = bfd_asymbol_name(info->symtab[n]);
4398
0
  if (name[0] == '$'
4399
0
      && (name[1] == 'x' || name[1] == 'd')
4400
0
      && (name[2] == '\0' || name[2] == '.'))
4401
0
    {
4402
0
      *map_type = (name[1] == 'x' ? MAP_INSN : MAP_DATA);
4403
0
      return true;
4404
0
    }
4405
4406
0
  return false;
4407
0
}
4408
4409
/* Set the feature bits in arch_variant in order to get the correct disassembly
4410
   for the chosen architecture variant.
4411
4412
   Currently we only restrict disassembly for Armv8-R and otherwise enable all
4413
   non-R-profile features.  */
4414
static void
4415
select_aarch64_variant (unsigned mach)
4416
2
{
4417
2
  switch (mach)
4418
2
    {
4419
0
    case bfd_mach_aarch64_8R:
4420
0
      AARCH64_SET_FEATURE (arch_variant, AARCH64_ARCH_V8R);
4421
0
      break;
4422
2
    default:
4423
2
      arch_variant = (aarch64_feature_set) AARCH64_ALL_FEATURES;
4424
2
      AARCH64_CLEAR_FEATURE (arch_variant, arch_variant, V8R);
4425
2
    }
4426
2
}
4427
4428
/* Entry-point of the AArch64 disassembler.  */
4429
4430
int
4431
print_insn_aarch64 (bfd_vma pc,
4432
        struct disassemble_info *info)
4433
14.8M
{
4434
14.8M
  bfd_byte  buffer[INSNLEN];
4435
14.8M
  int   status;
4436
14.8M
  void    (*printer) (bfd_vma, uint32_t, struct disassemble_info *,
4437
14.8M
          aarch64_operand_error *);
4438
14.8M
  bool   found = false;
4439
14.8M
  unsigned int  size = 4;
4440
14.8M
  unsigned long data;
4441
14.8M
  aarch64_operand_error errors;
4442
14.8M
  static bool set_features;
4443
4444
14.8M
  if (info->disassembler_options)
4445
0
    {
4446
0
      set_default_aarch64_dis_options (info);
4447
4448
0
      parse_aarch64_dis_options (info->disassembler_options);
4449
4450
      /* To avoid repeated parsing of these options, we remove them here.  */
4451
0
      info->disassembler_options = NULL;
4452
0
    }
4453
4454
14.8M
  if (!set_features)
4455
2
    {
4456
2
      select_aarch64_variant (info->mach);
4457
2
      set_features = true;
4458
2
    }
4459
4460
  /* Aarch64 instructions are always little-endian */
4461
14.8M
  info->endian_code = BFD_ENDIAN_LITTLE;
4462
4463
  /* Default to DATA.  A text section is required by the ABI to contain an
4464
     INSN mapping symbol at the start.  A data section has no such
4465
     requirement, hence if no mapping symbol is found the section must
4466
     contain only data.  This however isn't very useful if the user has
4467
     fully stripped the binaries.  If this is the case use the section
4468
     attributes to determine the default.  If we have no section default to
4469
     INSN as well, as we may be disassembling some raw bytes on a baremetal
4470
     HEX file or similar.  */
4471
14.8M
  enum map_type type = MAP_DATA;
4472
14.8M
  if ((info->section && info->section->flags & SEC_CODE) || !info->section)
4473
11.9M
    type = MAP_INSN;
4474
4475
  /* First check the full symtab for a mapping symbol, even if there
4476
     are no usable non-mapping symbols for this address.  */
4477
14.8M
  if (info->symtab_size != 0
4478
9
      && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour)
4479
9
    {
4480
9
      int last_sym = -1;
4481
9
      bfd_vma addr, section_vma = 0;
4482
9
      bool can_use_search_opt_p;
4483
9
      int n;
4484
4485
9
      if (pc <= last_mapping_addr)
4486
0
  last_mapping_sym = -1;
4487
4488
      /* Start scanning at the start of the function, or wherever
4489
   we finished last time.  */
4490
9
      n = info->symtab_pos + 1;
4491
4492
      /* If the last stop offset is different from the current one it means we
4493
   are disassembling a different glob of bytes.  As such the optimization
4494
   would not be safe and we should start over.  */
4495
9
      can_use_search_opt_p = last_mapping_sym >= 0
4496
8
           && info->stop_offset == last_stop_offset;
4497
4498
9
      if (n >= last_mapping_sym && can_use_search_opt_p)
4499
0
  n = last_mapping_sym;
4500
4501
      /* Look down while we haven't passed the location being disassembled.
4502
   The reason for this is that there's no defined order between a symbol
4503
   and an mapping symbol that may be at the same address.  We may have to
4504
   look at least one position ahead.  */
4505
36
      for (; n < info->symtab_size; n++)
4506
27
  {
4507
27
    addr = bfd_asymbol_value (info->symtab[n]);
4508
27
    if (addr > pc)
4509
0
      break;
4510
27
    if (get_sym_code_type (info, n, &type))
4511
27
      {
4512
27
        last_sym = n;
4513
27
        found = true;
4514
27
      }
4515
27
  }
4516
4517
9
      if (!found)
4518
0
  {
4519
0
    n = info->symtab_pos;
4520
0
    if (n >= last_mapping_sym && can_use_search_opt_p)
4521
0
      n = last_mapping_sym;
4522
4523
    /* No mapping symbol found at this address.  Look backwards
4524
       for a preceeding one, but don't go pass the section start
4525
       otherwise a data section with no mapping symbol can pick up
4526
       a text mapping symbol of a preceeding section.  The documentation
4527
       says section can be NULL, in which case we will seek up all the
4528
       way to the top.  */
4529
0
    if (info->section)
4530
0
      section_vma = info->section->vma;
4531
4532
0
    for (; n >= 0; n--)
4533
0
      {
4534
0
        addr = bfd_asymbol_value (info->symtab[n]);
4535
0
        if (addr < section_vma)
4536
0
    break;
4537
4538
0
        if (get_sym_code_type (info, n, &type))
4539
0
    {
4540
0
      last_sym = n;
4541
0
      found = true;
4542
0
      break;
4543
0
    }
4544
0
      }
4545
0
  }
4546
4547
9
      last_mapping_sym = last_sym;
4548
9
      last_type = type;
4549
9
      last_stop_offset = info->stop_offset;
4550
4551
      /* Look a little bit ahead to see if we should print out
4552
   less than four bytes of data.  If there's a symbol,
4553
   mapping or otherwise, after two bytes then don't
4554
   print more.  */
4555
9
      if (last_type == MAP_DATA)
4556
0
  {
4557
0
    size = 4 - (pc & 3);
4558
0
    for (n = last_sym + 1; n < info->symtab_size; n++)
4559
0
      {
4560
0
        addr = bfd_asymbol_value (info->symtab[n]);
4561
0
        if (addr > pc)
4562
0
    {
4563
0
      if (addr - pc < size)
4564
0
        size = addr - pc;
4565
0
      break;
4566
0
    }
4567
0
      }
4568
    /* If the next symbol is after three bytes, we need to
4569
       print only part of the data, so that we can use either
4570
       .byte or .short.  */
4571
0
    if (size == 3)
4572
0
      size = (pc & 1) ? 1 : 2;
4573
0
  }
4574
9
    }
4575
14.8M
  else
4576
14.8M
    last_type = type;
4577
4578
  /* PR 10263: Disassemble data if requested to do so by the user.  */
4579
14.8M
  if (last_type == MAP_DATA && ((info->flags & DISASSEMBLE_DATA) == 0))
4580
0
    {
4581
      /* size was set above.  */
4582
0
      info->bytes_per_chunk = size;
4583
0
      info->display_endian = info->endian;
4584
0
      printer = print_insn_data;
4585
0
    }
4586
14.8M
  else
4587
14.8M
    {
4588
14.8M
      info->bytes_per_chunk = size = INSNLEN;
4589
14.8M
      info->display_endian = info->endian_code;
4590
14.8M
      printer = print_insn_aarch64_word;
4591
14.8M
    }
4592
4593
14.8M
  status = (*info->read_memory_func) (pc, buffer, size, info);
4594
14.8M
  if (status != 0)
4595
14.7k
    {
4596
14.7k
      (*info->memory_error_func) (status, pc, info);
4597
14.7k
      return -1;
4598
14.7k
    }
4599
4600
14.8M
  data = bfd_get_bits (buffer, size * 8,
4601
14.8M
           info->display_endian == BFD_ENDIAN_BIG);
4602
4603
14.8M
  (*printer) (pc, data, info, &errors);
4604
4605
14.8M
  return size;
4606
14.8M
}
4607

4608
void
4609
print_aarch64_disassembler_options (FILE *stream)
4610
0
{
4611
0
  fprintf (stream, _("\n\
4612
0
The following AARCH64 specific disassembler options are supported for use\n\
4613
0
with the -M switch (multiple options should be separated by commas):\n"));
4614
4615
0
  fprintf (stream, _("\n\
4616
0
  no-aliases         Don't print instruction aliases.\n"));
4617
4618
0
  fprintf (stream, _("\n\
4619
0
  aliases            Do print instruction aliases.\n"));
4620
4621
0
  fprintf (stream, _("\n\
4622
0
  no-notes         Don't print instruction notes.\n"));
4623
4624
0
  fprintf (stream, _("\n\
4625
0
  notes            Do print instruction notes.\n"));
4626
4627
0
  fprintf (stream, _("\n\
4628
0
  annotate         Display symbol names for undefined instructions.\n"));
4629
4630
0
  fprintf (stream, _("\n\
4631
0
  no-annotate       Do not display symbol names for undefined instructions.\n"));
4632
4633
#ifdef DEBUG_AARCH64
4634
  fprintf (stream, _("\n\
4635
  debug_dump         Temp switch for debug trace.\n"));
4636
#endif /* DEBUG_AARCH64 */
4637
4638
  fprintf (stream, _("\n"));
4639
0
}