Coverage Report

Created: 2026-07-25 10:20

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