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