Coverage Report

Created: 2026-05-11 07:54

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