Coverage Report

Created: 2026-04-04 08:16

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