]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-aarch64.c
aarch64: Add sysreg features to +d128 dependencies
[thirdparty/binutils-gdb.git] / gas / config / tc-aarch64.c
CommitLineData
a06ea964
NC
1/* tc-aarch64.c -- Assemble for the AArch64 ISA
2
fd67aa11 3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
a06ea964
NC
4 Contributed by ARM Ltd.
5
6 This file is part of GAS.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the license, or
11 (at your option) any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING3. If not,
20 see <http://www.gnu.org/licenses/>. */
21
22#include "as.h"
23#include <limits.h>
24#include <stdarg.h>
3dfb1b6d 25#include <stdint.h>
a06ea964
NC
26#define NO_RELOC 0
27#include "safe-ctype.h"
28#include "subsegs.h"
29#include "obstack.h"
30
31#ifdef OBJ_ELF
32#include "elf/aarch64.h"
b52c4ee4
IB
33#include "dw2gencfi.h"
34#include "sframe.h"
35#include "gen-sframe.h"
a06ea964
NC
36#endif
37
c60b3806 38#include "dw2gencfi.h"
a06ea964
NC
39#include "dwarf2dbg.h"
40
a06ea964
NC
41#define streq(a, b) (strcmp (a, b) == 0)
42
f4c51f60
JW
43#define END_OF_INSN '\0'
44
a06ea964
NC
45static aarch64_feature_set cpu_variant;
46
47/* Variables that we set while parsing command-line options. Once all
48 options have been read we re-process these values to set the real
49 assembly flags. */
50static const aarch64_feature_set *mcpu_cpu_opt = NULL;
51static const aarch64_feature_set *march_cpu_opt = NULL;
52
53/* Constants for known architecture features. */
4abb672a 54static const aarch64_feature_set cpu_default = AARCH64_ARCH_FEATURES (V8A);
a06ea964 55
7e84b55d
TC
56/* Currently active instruction sequence. */
57static aarch64_instr_sequence *insn_sequence = NULL;
58
a06ea964
NC
59#ifdef OBJ_ELF
60/* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
61static symbolS *GOT_symbol;
c60b3806 62#endif
cec5225b 63
69091a2c
YZ
64/* Which ABI to use. */
65enum aarch64_abi_type
66{
3c0367d0
JW
67 AARCH64_ABI_NONE = 0,
68 AARCH64_ABI_LP64 = 1,
c60b3806
JT
69 AARCH64_ABI_ILP32 = 2,
70 AARCH64_ABI_LLP64 = 3
69091a2c
YZ
71};
72
b52c4ee4 73unsigned int aarch64_sframe_cfa_sp_reg;
3e3e792a 74/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
75unsigned int aarch64_sframe_cfa_fp_reg;
76unsigned int aarch64_sframe_cfa_ra_reg;
77
3c0367d0
JW
78#ifndef DEFAULT_ARCH
79#define DEFAULT_ARCH "aarch64"
80#endif
81
c60b3806 82#ifdef OBJ_ELF
3c0367d0
JW
83/* DEFAULT_ARCH is initialized in gas/configure.tgt. */
84static const char *default_arch = DEFAULT_ARCH;
c60b3806 85#endif
3c0367d0 86
69091a2c 87/* AArch64 ABI for the output file. */
3c0367d0 88static enum aarch64_abi_type aarch64_abi = AARCH64_ABI_NONE;
69091a2c 89
cec5225b
YZ
90/* When non-zero, program to a 32-bit model, in which the C data types
91 int, long and all pointer types are 32-bit objects (ILP32); or to a
92 64-bit model, in which the C int type is 32-bits but the C long type
93 and all pointer types are 64-bit objects (LP64). */
69091a2c 94#define ilp32_p (aarch64_abi == AARCH64_ABI_ILP32)
c60b3806
JT
95
96/* When non zero, C types int and long are 32 bit,
97 pointers, however are 64 bit */
98#define llp64_p (aarch64_abi == AARCH64_ABI_LLP64)
a06ea964 99
f06935a5 100enum vector_el_type
a06ea964
NC
101{
102 NT_invtype = -1,
103 NT_b,
104 NT_h,
105 NT_s,
106 NT_d,
d50c751e
RS
107 NT_q,
108 NT_zero,
109 NT_merge
a06ea964
NC
110};
111
8f9a77af 112/* Bits for DEFINED field in vector_type_el. */
f11ad6bc
RS
113#define NTA_HASTYPE 1
114#define NTA_HASINDEX 2
115#define NTA_HASVARWIDTH 4
a06ea964 116
8f9a77af 117struct vector_type_el
a06ea964 118{
f06935a5 119 enum vector_el_type type;
a06ea964 120 unsigned char defined;
8cde0b98 121 unsigned element_size;
a06ea964
NC
122 unsigned width;
123 int64_t index;
124};
125
126#define FIXUP_F_HAS_EXPLICIT_SHIFT 0x00000001
127
128struct reloc
129{
130 bfd_reloc_code_real_type type;
131 expressionS exp;
132 int pc_rel;
133 enum aarch64_opnd opnd;
134 uint32_t flags;
135 unsigned need_libopcodes_p : 1;
136};
137
138struct aarch64_instruction
139{
140 /* libopcodes structure for instruction intermediate representation. */
141 aarch64_inst base;
142 /* Record assembly errors found during the parsing. */
3e4525ca 143 aarch64_operand_error parsing_error;
a06ea964
NC
144 /* The condition that appears in the assembly line. */
145 int cond;
146 /* Relocation information (including the GAS internal fixup). */
147 struct reloc reloc;
148 /* Need to generate an immediate in the literal pool. */
149 unsigned gen_lit_pool : 1;
150};
151
152typedef struct aarch64_instruction aarch64_instruction;
153
154static aarch64_instruction inst;
155
5b7c81bd
AM
156static bool parse_operands (char *, const aarch64_opcode *);
157static bool programmer_friendly_fixup (aarch64_instruction *);
a06ea964 158
e426521e
RS
159/* If an AARCH64_OPDE_SYNTAX_ERROR has no error string, its first three
160 data fields contain the following information:
161
162 data[0].i:
163 A mask of register types that would have been acceptable as bare
164 operands, outside of a register list. In addition, SEF_DEFAULT_ERROR
165 is set if a general parsing error occured for an operand (that is,
166 an error not related to registers, and having no error string).
167
168 data[1].i:
169 A mask of register types that would have been acceptable inside
170 a register list. In addition, SEF_IN_REGLIST is set if the
171 operand contained a '{' and if we got to the point of trying
172 to parse a register inside a list.
173
174 data[2].i:
175 The mask associated with the register that was actually seen, or 0
176 if none. A nonzero value describes a register inside a register
177 list if data[1].i & SEF_IN_REGLIST, otherwise it describes a bare
178 register.
179
180 The idea is that stringless errors from multiple opcode templates can
181 be ORed together to give a summary of the available alternatives. */
182#define SEF_DEFAULT_ERROR (1U << 31)
183#define SEF_IN_REGLIST (1U << 31)
184
33eaf5de 185/* Diagnostics inline function utilities.
a06ea964 186
33eaf5de 187 These are lightweight utilities which should only be called by parse_operands
a06ea964
NC
188 and other parsers. GAS processes each assembly line by parsing it against
189 instruction template(s), in the case of multiple templates (for the same
190 mnemonic name), those templates are tried one by one until one succeeds or
191 all fail. An assembly line may fail a few templates before being
192 successfully parsed; an error saved here in most cases is not a user error
193 but an error indicating the current template is not the right template.
194 Therefore it is very important that errors can be saved at a low cost during
195 the parsing; we don't want to slow down the whole parsing by recording
196 non-user errors in detail.
197
33eaf5de 198 Remember that the objective is to help GAS pick up the most appropriate
a06ea964
NC
199 error message in the case of multiple templates, e.g. FMOV which has 8
200 templates. */
201
202static inline void
203clear_error (void)
204{
3e4525ca 205 memset (&inst.parsing_error, 0, sizeof (inst.parsing_error));
a06ea964 206 inst.parsing_error.kind = AARCH64_OPDE_NIL;
a06ea964
NC
207}
208
5b7c81bd 209static inline bool
a06ea964
NC
210error_p (void)
211{
212 return inst.parsing_error.kind != AARCH64_OPDE_NIL;
213}
214
a06ea964
NC
215static inline void
216set_error (enum aarch64_operand_error_kind kind, const char *error)
217{
3e4525ca
RS
218 memset (&inst.parsing_error, 0, sizeof (inst.parsing_error));
219 inst.parsing_error.index = -1;
a06ea964
NC
220 inst.parsing_error.kind = kind;
221 inst.parsing_error.error = error;
222}
223
224static inline void
225set_recoverable_error (const char *error)
226{
227 set_error (AARCH64_OPDE_RECOVERABLE, error);
228}
229
230/* Use the DESC field of the corresponding aarch64_operand entry to compose
231 the error message. */
232static inline void
233set_default_error (void)
234{
235 set_error (AARCH64_OPDE_SYNTAX_ERROR, NULL);
e426521e
RS
236 inst.parsing_error.data[0].i = SEF_DEFAULT_ERROR;
237}
238
a06ea964
NC
239static inline void
240set_syntax_error (const char *error)
241{
242 set_error (AARCH64_OPDE_SYNTAX_ERROR, error);
243}
244
245static inline void
246set_first_syntax_error (const char *error)
247{
248 if (! error_p ())
249 set_error (AARCH64_OPDE_SYNTAX_ERROR, error);
250}
251
252static inline void
253set_fatal_syntax_error (const char *error)
254{
255 set_error (AARCH64_OPDE_FATAL_SYNTAX_ERROR, error);
256}
257\f
a06ea964
NC
258/* Return value for certain parsers when the parsing fails; those parsers
259 return the information of the parsed result, e.g. register number, on
260 success. */
261#define PARSE_FAIL -1
262
263/* This is an invalid condition code that means no conditional field is
264 present. */
265#define COND_ALWAYS 0x10
266
a06ea964
NC
267typedef struct
268{
269 const char *template;
270 uint32_t value;
271} asm_nzcv;
272
273struct reloc_entry
274{
275 char *name;
276 bfd_reloc_code_real_type reloc;
277};
278
a06ea964
NC
279/* Macros to define the register types and masks for the purpose
280 of parsing. */
281
282#undef AARCH64_REG_TYPES
283#define AARCH64_REG_TYPES \
284 BASIC_REG_TYPE(R_32) /* w[0-30] */ \
285 BASIC_REG_TYPE(R_64) /* x[0-30] */ \
286 BASIC_REG_TYPE(SP_32) /* wsp */ \
287 BASIC_REG_TYPE(SP_64) /* sp */ \
2f4e3a2c
RS
288 BASIC_REG_TYPE(ZR_32) /* wzr */ \
289 BASIC_REG_TYPE(ZR_64) /* xzr */ \
a06ea964
NC
290 BASIC_REG_TYPE(FP_B) /* b[0-31] *//* NOTE: keep FP_[BHSDQ] consecutive! */\
291 BASIC_REG_TYPE(FP_H) /* h[0-31] */ \
292 BASIC_REG_TYPE(FP_S) /* s[0-31] */ \
293 BASIC_REG_TYPE(FP_D) /* d[0-31] */ \
294 BASIC_REG_TYPE(FP_Q) /* q[0-31] */ \
2f4e3a2c
RS
295 BASIC_REG_TYPE(V) /* v[0-31] */ \
296 BASIC_REG_TYPE(Z) /* z[0-31] */ \
297 BASIC_REG_TYPE(P) /* p[0-15] */ \
503fae12 298 BASIC_REG_TYPE(PN) /* pn[0-15] */ \
e9e1ddbb 299 BASIC_REG_TYPE(ZA) /* za */ \
fd855fbb
RS
300 BASIC_REG_TYPE(ZAT) /* za[0-15] (ZA tile) */ \
301 BASIC_REG_TYPE(ZATH) /* za[0-15]h (ZA tile horizontal slice) */ \
302 BASIC_REG_TYPE(ZATV) /* za[0-15]v (ZA tile vertical slice) */ \
cbd11b88 303 BASIC_REG_TYPE(ZT0) /* zt0 */ \
e1b988bb 304 /* Typecheck: any 64-bit int reg (inc SP exc XZR). */ \
a06ea964 305 MULTI_REG_TYPE(R64_SP, REG_TYPE(R_64) | REG_TYPE(SP_64)) \
4df068de
RS
306 /* Typecheck: same, plus SVE registers. */ \
307 MULTI_REG_TYPE(SVE_BASE, REG_TYPE(R_64) | REG_TYPE(SP_64) \
2f4e3a2c 308 | REG_TYPE(Z)) \
e1b988bb 309 /* Typecheck: x[0-30], w[0-30] or [xw]zr. */ \
2f4e3a2c
RS
310 MULTI_REG_TYPE(R_ZR, REG_TYPE(R_32) | REG_TYPE(R_64) \
311 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64)) \
4df068de
RS
312 /* Typecheck: same, plus SVE registers. */ \
313 MULTI_REG_TYPE(SVE_OFFSET, REG_TYPE(R_32) | REG_TYPE(R_64) \
2f4e3a2c
RS
314 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64) \
315 | REG_TYPE(Z)) \
e1b988bb
RS
316 /* Typecheck: x[0-30], w[0-30] or {w}sp. */ \
317 MULTI_REG_TYPE(R_SP, REG_TYPE(R_32) | REG_TYPE(R_64) \
318 | REG_TYPE(SP_32) | REG_TYPE(SP_64)) \
319 /* Typecheck: any int (inc {W}SP inc [WX]ZR). */ \
2f4e3a2c 320 MULTI_REG_TYPE(R_ZR_SP, REG_TYPE(R_32) | REG_TYPE(R_64) \
a06ea964 321 | REG_TYPE(SP_32) | REG_TYPE(SP_64) \
2f4e3a2c 322 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64)) \
a06ea964
NC
323 /* Typecheck: any [BHSDQ]P FP. */ \
324 MULTI_REG_TYPE(BHSDQ, REG_TYPE(FP_B) | REG_TYPE(FP_H) \
325 | REG_TYPE(FP_S) | REG_TYPE(FP_D) | REG_TYPE(FP_Q)) \
e1b988bb 326 /* Typecheck: any int or [BHSDQ]P FP or V reg (exc SP inc [WX]ZR). */ \
2f4e3a2c
RS
327 MULTI_REG_TYPE(R_ZR_BHSDQ_V, REG_TYPE(R_32) | REG_TYPE(R_64) \
328 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64) | REG_TYPE(V) \
a06ea964
NC
329 | REG_TYPE(FP_B) | REG_TYPE(FP_H) \
330 | REG_TYPE(FP_S) | REG_TYPE(FP_D) | REG_TYPE(FP_Q)) \
5b2b928e
JB
331 /* Typecheck: as above, but also Zn, Pn, and {W}SP. This should only \
332 be used for SVE instructions, since Zn and Pn are valid symbols \
c0890d26 333 in other contexts. */ \
2f4e3a2c 334 MULTI_REG_TYPE(R_ZR_SP_BHSDQ_VZP, REG_TYPE(R_32) | REG_TYPE(R_64) \
5b2b928e 335 | REG_TYPE(SP_32) | REG_TYPE(SP_64) \
2f4e3a2c 336 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64) | REG_TYPE(V) \
c0890d26
RS
337 | REG_TYPE(FP_B) | REG_TYPE(FP_H) \
338 | REG_TYPE(FP_S) | REG_TYPE(FP_D) | REG_TYPE(FP_Q) \
2f4e3a2c 339 | REG_TYPE(Z) | REG_TYPE(P)) \
6efa6601
RS
340 /* Likewise, but with predicate-as-counter registers added. */ \
341 MULTI_REG_TYPE(R_ZR_SP_BHSDQ_VZP_PN, REG_TYPE(R_32) | REG_TYPE(R_64) \
342 | REG_TYPE(SP_32) | REG_TYPE(SP_64) \
343 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64) | REG_TYPE(V) \
344 | REG_TYPE(FP_B) | REG_TYPE(FP_H) \
345 | REG_TYPE(FP_S) | REG_TYPE(FP_D) | REG_TYPE(FP_Q) \
346 | REG_TYPE(Z) | REG_TYPE(P) | REG_TYPE(PN)) \
a06ea964
NC
347 /* Any integer register; used for error messages only. */ \
348 MULTI_REG_TYPE(R_N, REG_TYPE(R_32) | REG_TYPE(R_64) \
349 | REG_TYPE(SP_32) | REG_TYPE(SP_64) \
2f4e3a2c 350 | REG_TYPE(ZR_32) | REG_TYPE(ZR_64)) \
e426521e 351 /* Any vector register. */ \
2f4e3a2c 352 MULTI_REG_TYPE(VZ, REG_TYPE(V) | REG_TYPE(Z)) \
e426521e 353 /* An SVE vector or predicate register. */ \
2f4e3a2c 354 MULTI_REG_TYPE(ZP, REG_TYPE(Z) | REG_TYPE(P)) \
e426521e 355 /* Any vector or predicate register. */ \
2f4e3a2c 356 MULTI_REG_TYPE(VZP, REG_TYPE(V) | REG_TYPE(Z) | REG_TYPE(P)) \
e9e1ddbb
RS
357 /* The whole of ZA or a single tile. */ \
358 MULTI_REG_TYPE(ZA_ZAT, REG_TYPE(ZA) | REG_TYPE(ZAT)) \
83dbd40d
RS
359 /* A horizontal or vertical slice of a ZA tile. */ \
360 MULTI_REG_TYPE(ZATHV, REG_TYPE(ZATH) | REG_TYPE(ZATV)) \
a06ea964 361 /* Pseudo type to mark the end of the enumerator sequence. */ \
e426521e 362 END_REG_TYPE(MAX)
a06ea964
NC
363
364#undef BASIC_REG_TYPE
365#define BASIC_REG_TYPE(T) REG_TYPE_##T,
366#undef MULTI_REG_TYPE
367#define MULTI_REG_TYPE(T,V) BASIC_REG_TYPE(T)
e426521e
RS
368#undef END_REG_TYPE
369#define END_REG_TYPE(T) BASIC_REG_TYPE(T)
a06ea964
NC
370
371/* Register type enumerators. */
8a0b252a 372typedef enum aarch64_reg_type_
a06ea964
NC
373{
374 /* A list of REG_TYPE_*. */
375 AARCH64_REG_TYPES
376} aarch64_reg_type;
377
378#undef BASIC_REG_TYPE
379#define BASIC_REG_TYPE(T) 1 << REG_TYPE_##T,
380#undef REG_TYPE
381#define REG_TYPE(T) (1 << REG_TYPE_##T)
382#undef MULTI_REG_TYPE
383#define MULTI_REG_TYPE(T,V) V,
e426521e
RS
384#undef END_REG_TYPE
385#define END_REG_TYPE(T) 0
a06ea964 386
8a0b252a
TS
387/* Structure for a hash table entry for a register. */
388typedef struct
389{
390 const char *name;
391 unsigned char number;
392 ENUM_BITFIELD (aarch64_reg_type_) type : 8;
393 unsigned char builtin;
394} reg_entry;
395
a06ea964
NC
396/* Values indexed by aarch64_reg_type to assist the type checking. */
397static const unsigned reg_type_masks[] =
398{
399 AARCH64_REG_TYPES
400};
401
402#undef BASIC_REG_TYPE
403#undef REG_TYPE
404#undef MULTI_REG_TYPE
e426521e 405#undef END_REG_TYPE
a06ea964
NC
406#undef AARCH64_REG_TYPES
407
e426521e
RS
408/* We expected one of the registers in MASK to be specified. If a register
409 of some kind was specified, SEEN is a mask that contains that register,
410 otherwise it is zero.
411
412 If it is possible to provide a relatively pithy message that describes
413 the error exactly, return a string that does so, reporting the error
414 against "operand %d". Return null otherwise.
415
416 From a QoI perspective, any REG_TYPE_* that is passed as the first
417 argument to set_expected_reg_error should generally have its own message.
418 Providing messages for combinations of such REG_TYPE_*s can be useful if
419 it is possible to summarize the combination in a relatively natural way.
420 On the other hand, it seems better to avoid long lists of unrelated
421 things. */
422
a06ea964 423static const char *
e426521e
RS
424get_reg_expected_msg (unsigned int mask, unsigned int seen)
425{
426 /* First handle messages that use SEEN. */
427 if ((mask & reg_type_masks[REG_TYPE_ZAT])
428 && (seen & reg_type_masks[REG_TYPE_ZATHV]))
429 return N_("expected an unsuffixed ZA tile at operand %d");
430
431 if ((mask & reg_type_masks[REG_TYPE_ZATHV])
432 && (seen & reg_type_masks[REG_TYPE_ZAT]))
433 return N_("missing horizontal or vertical suffix at operand %d");
434
435 if ((mask & reg_type_masks[REG_TYPE_ZA])
436 && (seen & (reg_type_masks[REG_TYPE_ZAT]
437 | reg_type_masks[REG_TYPE_ZATHV])))
438 return N_("expected 'za' rather than a ZA tile at operand %d");
439
503fae12
RS
440 if ((mask & reg_type_masks[REG_TYPE_PN])
441 && (seen & reg_type_masks[REG_TYPE_P]))
442 return N_("expected a predicate-as-counter rather than predicate-as-mask"
443 " register at operand %d");
444
445 if ((mask & reg_type_masks[REG_TYPE_P])
446 && (seen & reg_type_masks[REG_TYPE_PN]))
447 return N_("expected a predicate-as-mask rather than predicate-as-counter"
448 " register at operand %d");
449
e426521e
RS
450 /* Integer, zero and stack registers. */
451 if (mask == reg_type_masks[REG_TYPE_R_64])
452 return N_("expected a 64-bit integer register at operand %d");
2f4e3a2c 453 if (mask == reg_type_masks[REG_TYPE_R_ZR])
e426521e
RS
454 return N_("expected an integer or zero register at operand %d");
455 if (mask == reg_type_masks[REG_TYPE_R_SP])
456 return N_("expected an integer or stack pointer register at operand %d");
457
458 /* Floating-point and SIMD registers. */
459 if (mask == reg_type_masks[REG_TYPE_BHSDQ])
460 return N_("expected a scalar SIMD or floating-point register"
461 " at operand %d");
2f4e3a2c 462 if (mask == reg_type_masks[REG_TYPE_V])
e426521e 463 return N_("expected an Advanced SIMD vector register at operand %d");
2f4e3a2c 464 if (mask == reg_type_masks[REG_TYPE_Z])
e426521e 465 return N_("expected an SVE vector register at operand %d");
503fae12
RS
466 if (mask == reg_type_masks[REG_TYPE_P]
467 || mask == (reg_type_masks[REG_TYPE_P] | reg_type_masks[REG_TYPE_PN]))
468 /* Use this error for "predicate-as-mask only" and "either kind of
469 predicate". We report a more specific error if P is used where
470 PN is expected, and vice versa, so the issue at this point is
471 "predicate-like" vs. "not predicate-like". */
e426521e 472 return N_("expected an SVE predicate register at operand %d");
99e01a66
RS
473 if (mask == reg_type_masks[REG_TYPE_PN])
474 return N_("expected an SVE predicate-as-counter register at operand %d");
e426521e
RS
475 if (mask == reg_type_masks[REG_TYPE_VZ])
476 return N_("expected a vector register at operand %d");
477 if (mask == reg_type_masks[REG_TYPE_ZP])
478 return N_("expected an SVE vector or predicate register at operand %d");
479 if (mask == reg_type_masks[REG_TYPE_VZP])
480 return N_("expected a vector or predicate register at operand %d");
481
cbd11b88 482 /* SME-related registers. */
e426521e
RS
483 if (mask == reg_type_masks[REG_TYPE_ZA])
484 return N_("expected a ZA array vector at operand %d");
cbd11b88
RS
485 if (mask == (reg_type_masks[REG_TYPE_ZA_ZAT] | reg_type_masks[REG_TYPE_ZT0]))
486 return N_("expected ZT0 or a ZA mask at operand %d");
e426521e
RS
487 if (mask == reg_type_masks[REG_TYPE_ZAT])
488 return N_("expected a ZA tile at operand %d");
489 if (mask == reg_type_masks[REG_TYPE_ZATHV])
490 return N_("expected a ZA tile slice at operand %d");
491
492 /* Integer and vector combos. */
2f4e3a2c 493 if (mask == (reg_type_masks[REG_TYPE_R_ZR] | reg_type_masks[REG_TYPE_V]))
e426521e
RS
494 return N_("expected an integer register or Advanced SIMD vector register"
495 " at operand %d");
2f4e3a2c 496 if (mask == (reg_type_masks[REG_TYPE_R_ZR] | reg_type_masks[REG_TYPE_Z]))
e426521e
RS
497 return N_("expected an integer register or SVE vector register"
498 " at operand %d");
2f4e3a2c 499 if (mask == (reg_type_masks[REG_TYPE_R_ZR] | reg_type_masks[REG_TYPE_VZ]))
e426521e 500 return N_("expected an integer or vector register at operand %d");
2f4e3a2c 501 if (mask == (reg_type_masks[REG_TYPE_R_ZR] | reg_type_masks[REG_TYPE_P]))
e426521e 502 return N_("expected an integer or predicate register at operand %d");
2f4e3a2c 503 if (mask == (reg_type_masks[REG_TYPE_R_ZR] | reg_type_masks[REG_TYPE_VZP]))
e426521e
RS
504 return N_("expected an integer, vector or predicate register"
505 " at operand %d");
506
507 /* SVE and SME combos. */
2f4e3a2c 508 if (mask == (reg_type_masks[REG_TYPE_Z] | reg_type_masks[REG_TYPE_ZATHV]))
e426521e
RS
509 return N_("expected an SVE vector register or ZA tile slice"
510 " at operand %d");
511
512 return NULL;
513}
514
515/* Record that we expected a register of type TYPE but didn't see one.
516 REG is the register that we actually saw, or null if we didn't see a
517 recognized register. FLAGS is SEF_IN_REGLIST if we are parsing the
518 contents of a register list, otherwise it is zero. */
519
520static inline void
521set_expected_reg_error (aarch64_reg_type type, const reg_entry *reg,
522 unsigned int flags)
a06ea964 523{
e426521e
RS
524 assert (flags == 0 || flags == SEF_IN_REGLIST);
525 set_error (AARCH64_OPDE_SYNTAX_ERROR, NULL);
526 if (flags & SEF_IN_REGLIST)
527 inst.parsing_error.data[1].i = reg_type_masks[type] | flags;
528 else
529 inst.parsing_error.data[0].i = reg_type_masks[type];
530 if (reg)
531 inst.parsing_error.data[2].i = reg_type_masks[reg->type];
532}
a06ea964 533
e426521e
RS
534/* Record that we expected a register list containing registers of type TYPE,
535 but didn't see the opening '{'. If we saw a register instead, REG is the
536 register that we saw, otherwise it is null. */
537
538static inline void
539set_expected_reglist_error (aarch64_reg_type type, const reg_entry *reg)
540{
541 set_error (AARCH64_OPDE_SYNTAX_ERROR, NULL);
542 inst.parsing_error.data[1].i = reg_type_masks[type];
543 if (reg)
544 inst.parsing_error.data[2].i = reg_type_masks[reg->type];
a06ea964
NC
545}
546
547/* Some well known registers that we refer to directly elsewhere. */
548#define REG_SP 31
c469c864 549#define REG_ZR 31
a06ea964
NC
550
551/* Instructions take 4 bytes in the object file. */
552#define INSN_SIZE 4
553
629310ab
ML
554static htab_t aarch64_ops_hsh;
555static htab_t aarch64_cond_hsh;
556static htab_t aarch64_shift_hsh;
557static htab_t aarch64_sys_regs_hsh;
558static htab_t aarch64_pstatefield_hsh;
559static htab_t aarch64_sys_regs_ic_hsh;
560static htab_t aarch64_sys_regs_dc_hsh;
561static htab_t aarch64_sys_regs_at_hsh;
562static htab_t aarch64_sys_regs_tlbi_hsh;
563static htab_t aarch64_sys_regs_sr_hsh;
564static htab_t aarch64_reg_hsh;
565static htab_t aarch64_barrier_opt_hsh;
566static htab_t aarch64_nzcv_hsh;
567static htab_t aarch64_pldop_hsh;
568static htab_t aarch64_hint_opt_hsh;
a06ea964
NC
569
570/* Stuff needed to resolve the label ambiguity
571 As:
572 ...
573 label: <insn>
574 may differ from:
575 ...
576 label:
577 <insn> */
578
579static symbolS *last_label_seen;
580
581/* Literal pool structure. Held on a per-section
582 and per-sub-section basis. */
583
584#define MAX_LITERAL_POOL_SIZE 1024
55d9b4c1
NC
585typedef struct literal_expression
586{
587 expressionS exp;
588 /* If exp.op == O_big then this bignum holds a copy of the global bignum value. */
589 LITTLENUM_TYPE * bignum;
590} literal_expression;
591
a06ea964
NC
592typedef struct literal_pool
593{
55d9b4c1 594 literal_expression literals[MAX_LITERAL_POOL_SIZE];
a06ea964
NC
595 unsigned int next_free_entry;
596 unsigned int id;
597 symbolS *symbol;
598 segT section;
599 subsegT sub_section;
600 int size;
601 struct literal_pool *next;
602} literal_pool;
603
604/* Pointer to a linked list of literal pools. */
605static literal_pool *list_of_pools = NULL;
606\f
607/* Pure syntax. */
608
609/* This array holds the chars that always start a comment. If the
610 pre-processor is disabled, these aren't very useful. */
611const char comment_chars[] = "";
612
613/* This array holds the chars that only start a comment at the beginning of
614 a line. If the line seems to have the form '# 123 filename'
615 .line and .file directives will appear in the pre-processed output. */
616/* Note that input_file.c hand checks for '#' at the beginning of the
617 first line of the input file. This is because the compiler outputs
618 #NO_APP at the beginning of its output. */
619/* Also note that comments like this one will always work. */
620const char line_comment_chars[] = "#";
621
622const char line_separator_chars[] = ";";
623
624/* Chars that can be used to separate mant
625 from exp in floating point numbers. */
626const char EXP_CHARS[] = "eE";
627
628/* Chars that mean this number is a floating point constant. */
629/* As in 0f12.456 */
630/* or 0d1.2345e12 */
631
7727283e 632const char FLT_CHARS[] = "rRsSfFdDxXeEpPhHb";
a06ea964
NC
633
634/* Prefix character that indicates the start of an immediate value. */
635#define is_immediate_prefix(C) ((C) == '#')
636
637/* Separator character handling. */
638
639#define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
640
5b7c81bd 641static inline bool
a06ea964
NC
642skip_past_char (char **str, char c)
643{
644 if (**str == c)
645 {
646 (*str)++;
5b7c81bd 647 return true;
a06ea964
NC
648 }
649 else
5b7c81bd 650 return false;
a06ea964
NC
651}
652
653#define skip_past_comma(str) skip_past_char (str, ',')
654
655/* Arithmetic expressions (possibly involving symbols). */
656
eac4eb8e 657static bool in_aarch64_get_expression = false;
a06ea964 658
eac4eb8e
NC
659/* Third argument to aarch64_get_expression. */
660#define GE_NO_PREFIX false
661#define GE_OPT_PREFIX true
662
663/* Fourth argument to aarch64_get_expression. */
664#define ALLOW_ABSENT false
665#define REJECT_ABSENT true
666
a06ea964
NC
667/* Return TRUE if the string pointed by *STR is successfully parsed
668 as an valid expression; *EP will be filled with the information of
eac4eb8e
NC
669 such an expression. Otherwise return FALSE.
670
671 If ALLOW_IMMEDIATE_PREFIX is true then skip a '#' at the start.
c1723a81 672 If REJECT_ABSENT is true then trat missing expressions as an error. */
a06ea964 673
5b7c81bd 674static bool
eac4eb8e
NC
675aarch64_get_expression (expressionS * ep,
676 char ** str,
677 bool allow_immediate_prefix,
c1723a81 678 bool reject_absent)
a06ea964
NC
679{
680 char *save_in;
681 segT seg;
eac4eb8e 682 bool prefix_present = false;
a06ea964 683
eac4eb8e 684 if (allow_immediate_prefix)
a06ea964 685 {
a06ea964
NC
686 if (is_immediate_prefix (**str))
687 {
688 (*str)++;
eac4eb8e 689 prefix_present = true;
a06ea964 690 }
a06ea964
NC
691 }
692
693 memset (ep, 0, sizeof (expressionS));
694
695 save_in = input_line_pointer;
696 input_line_pointer = *str;
eac4eb8e 697 in_aarch64_get_expression = true;
c1723a81 698 seg = expression (ep);
eac4eb8e 699 in_aarch64_get_expression = false;
a06ea964
NC
700
701 if (ep->X_op == O_illegal || (reject_absent && ep->X_op == O_absent))
702 {
703 /* We found a bad expression in md_operand(). */
704 *str = input_line_pointer;
705 input_line_pointer = save_in;
eac4eb8e 706 if (prefix_present && ! error_p ())
a06ea964
NC
707 set_fatal_syntax_error (_("bad expression"));
708 else
709 set_first_syntax_error (_("bad expression"));
5b7c81bd 710 return false;
a06ea964
NC
711 }
712
713#ifdef OBJ_AOUT
714 if (seg != absolute_section
715 && seg != text_section
716 && seg != data_section
eac4eb8e
NC
717 && seg != bss_section
718 && seg != undefined_section)
a06ea964
NC
719 {
720 set_syntax_error (_("bad segment"));
721 *str = input_line_pointer;
722 input_line_pointer = save_in;
5b7c81bd 723 return false;
a06ea964
NC
724 }
725#else
726 (void) seg;
727#endif
728
a06ea964
NC
729 *str = input_line_pointer;
730 input_line_pointer = save_in;
5b7c81bd 731 return true;
a06ea964
NC
732}
733
734/* Turn a string in input_line_pointer into a floating point constant
735 of type TYPE, and store the appropriate bytes in *LITP. The number
736 of LITTLENUMS emitted is stored in *SIZEP. An error message is
737 returned, or NULL on OK. */
738
6d4af3c2 739const char *
a06ea964
NC
740md_atof (int type, char *litP, int *sizeP)
741{
742 return ieee_md_atof (type, litP, sizeP, target_big_endian);
743}
744
745/* We handle all bad expressions here, so that we can report the faulty
746 instruction in the error message. */
747void
748md_operand (expressionS * exp)
749{
eac4eb8e 750 if (in_aarch64_get_expression)
a06ea964
NC
751 exp->X_op = O_illegal;
752}
753
754/* Immediate values. */
755
756/* Errors may be set multiple times during parsing or bit encoding
757 (particularly in the Neon bits), but usually the earliest error which is set
758 will be the most meaningful. Avoid overwriting it with later (cascading)
759 errors by calling this function. */
760
761static void
762first_error (const char *error)
763{
764 if (! error_p ())
765 set_syntax_error (error);
766}
767
2b0f3761 768/* Similar to first_error, but this function accepts formatted error
a06ea964
NC
769 message. */
770static void
771first_error_fmt (const char *format, ...)
772{
773 va_list args;
774 enum
775 { size = 100 };
776 /* N.B. this single buffer will not cause error messages for different
777 instructions to pollute each other; this is because at the end of
778 processing of each assembly line, error message if any will be
779 collected by as_bad. */
780 static char buffer[size];
781
782 if (! error_p ())
783 {
3e0baa28 784 int ret ATTRIBUTE_UNUSED;
a06ea964
NC
785 va_start (args, format);
786 ret = vsnprintf (buffer, size, format, args);
787 know (ret <= size - 1 && ret >= 0);
788 va_end (args);
789 set_syntax_error (buffer);
790 }
791}
792
363c5c8b
RS
793/* Internal helper routine converting a vector_type_el structure *VECTYPE
794 to a corresponding operand qualifier. */
795
796static inline aarch64_opnd_qualifier_t
797vectype_to_qualifier (const struct vector_type_el *vectype)
798{
799 /* Element size in bytes indexed by vector_el_type. */
800 const unsigned char ele_size[5]
801 = {1, 2, 4, 8, 16};
802 const unsigned int ele_base [5] =
803 {
804 AARCH64_OPND_QLF_V_4B,
805 AARCH64_OPND_QLF_V_2H,
806 AARCH64_OPND_QLF_V_2S,
807 AARCH64_OPND_QLF_V_1D,
808 AARCH64_OPND_QLF_V_1Q
809 };
810
811 if (!vectype->defined || vectype->type == NT_invtype)
812 goto vectype_conversion_fail;
813
814 if (vectype->type == NT_zero)
815 return AARCH64_OPND_QLF_P_Z;
816 if (vectype->type == NT_merge)
817 return AARCH64_OPND_QLF_P_M;
818
819 gas_assert (vectype->type >= NT_b && vectype->type <= NT_q);
820
821 if (vectype->defined & (NTA_HASINDEX | NTA_HASVARWIDTH))
822 {
823 /* Special case S_4B. */
824 if (vectype->type == NT_b && vectype->width == 4)
825 return AARCH64_OPND_QLF_S_4B;
826
827 /* Special case S_2H. */
828 if (vectype->type == NT_h && vectype->width == 2)
829 return AARCH64_OPND_QLF_S_2H;
830
831 /* Vector element register. */
832 return AARCH64_OPND_QLF_S_B + vectype->type;
833 }
834 else
835 {
836 /* Vector register. */
837 int reg_size = ele_size[vectype->type] * vectype->width;
838 unsigned offset;
839 unsigned shift;
840 if (reg_size != 16 && reg_size != 8 && reg_size != 4)
841 goto vectype_conversion_fail;
842
843 /* The conversion is by calculating the offset from the base operand
844 qualifier for the vector type. The operand qualifiers are regular
845 enough that the offset can established by shifting the vector width by
846 a vector-type dependent amount. */
847 shift = 0;
848 if (vectype->type == NT_b)
849 shift = 3;
850 else if (vectype->type == NT_h || vectype->type == NT_s)
851 shift = 2;
852 else if (vectype->type >= NT_d)
853 shift = 1;
854 else
855 gas_assert (0);
856
857 offset = ele_base [vectype->type] + (vectype->width >> shift);
858 gas_assert (AARCH64_OPND_QLF_V_4B <= offset
859 && offset <= AARCH64_OPND_QLF_V_1Q);
860 return offset;
861 }
862
863 vectype_conversion_fail:
864 first_error (_("bad vector arrangement type"));
865 return AARCH64_OPND_QLF_NIL;
866}
867
a06ea964
NC
868/* Register parsing. */
869
870/* Generic register parser which is called by other specialized
871 register parsers.
872 CCP points to what should be the beginning of a register name.
873 If it is indeed a valid register name, advance CCP over it and
874 return the reg_entry structure; otherwise return NULL.
875 It does not issue diagnostics. */
876
877static reg_entry *
878parse_reg (char **ccp)
879{
880 char *start = *ccp;
881 char *p;
882 reg_entry *reg;
883
884#ifdef REGISTER_PREFIX
885 if (*start != REGISTER_PREFIX)
886 return NULL;
887 start++;
888#endif
889
890 p = start;
891 if (!ISALPHA (*p) || !is_name_beginner (*p))
892 return NULL;
893
894 do
895 p++;
896 while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
897
629310ab 898 reg = (reg_entry *) str_hash_find_n (aarch64_reg_hsh, start, p - start);
a06ea964
NC
899
900 if (!reg)
901 return NULL;
902
903 *ccp = p;
904 return reg;
905}
906
30ba1d7e
RS
907/* Return the operand qualifier associated with all uses of REG, or
908 AARCH64_OPND_QLF_NIL if none. AARCH64_OPND_QLF_NIL means either
909 that qualifiers don't apply to REG or that qualifiers are added
910 using suffixes. */
911
912static aarch64_opnd_qualifier_t
913inherent_reg_qualifier (const reg_entry *reg)
914{
915 switch (reg->type)
916 {
917 case REG_TYPE_R_32:
918 case REG_TYPE_SP_32:
2f4e3a2c 919 case REG_TYPE_ZR_32:
30ba1d7e
RS
920 return AARCH64_OPND_QLF_W;
921
922 case REG_TYPE_R_64:
923 case REG_TYPE_SP_64:
2f4e3a2c 924 case REG_TYPE_ZR_64:
30ba1d7e
RS
925 return AARCH64_OPND_QLF_X;
926
927 case REG_TYPE_FP_B:
928 case REG_TYPE_FP_H:
929 case REG_TYPE_FP_S:
930 case REG_TYPE_FP_D:
931 case REG_TYPE_FP_Q:
932 return AARCH64_OPND_QLF_S_B + (reg->type - REG_TYPE_FP_B);
933
934 default:
935 return AARCH64_OPND_QLF_NIL;
936 }
937}
938
a06ea964
NC
939/* Return TRUE if REG->TYPE is a valid type of TYPE; otherwise
940 return FALSE. */
5b7c81bd 941static bool
a06ea964
NC
942aarch64_check_reg_type (const reg_entry *reg, aarch64_reg_type type)
943{
e1b988bb 944 return (reg_type_masks[type] & (1 << reg->type)) != 0;
a06ea964
NC
945}
946
4df068de
RS
947/* Try to parse a base or offset register. Allow SVE base and offset
948 registers if REG_TYPE includes SVE registers. Return the register
949 entry on success, setting *QUALIFIER to the register qualifier.
950 Return null otherwise.
e1b988bb 951
a06ea964
NC
952 Note that this function does not issue any diagnostics. */
953
e1b988bb 954static const reg_entry *
4df068de
RS
955aarch64_addr_reg_parse (char **ccp, aarch64_reg_type reg_type,
956 aarch64_opnd_qualifier_t *qualifier)
a06ea964
NC
957{
958 char *str = *ccp;
959 const reg_entry *reg = parse_reg (&str);
960
961 if (reg == NULL)
e1b988bb 962 return NULL;
a06ea964
NC
963
964 switch (reg->type)
965 {
2f4e3a2c
RS
966 case REG_TYPE_Z:
967 if ((reg_type_masks[reg_type] & (1 << REG_TYPE_Z)) == 0
4df068de
RS
968 || str[0] != '.')
969 return NULL;
970 switch (TOLOWER (str[1]))
971 {
972 case 's':
973 *qualifier = AARCH64_OPND_QLF_S_S;
974 break;
975 case 'd':
976 *qualifier = AARCH64_OPND_QLF_S_D;
977 break;
978 default:
979 return NULL;
980 }
981 str += 2;
982 break;
983
a06ea964 984 default:
2f4e3a2c 985 if (!aarch64_check_reg_type (reg, REG_TYPE_R_ZR_SP))
30ba1d7e
RS
986 return NULL;
987 *qualifier = inherent_reg_qualifier (reg);
988 break;
a06ea964
NC
989 }
990
991 *ccp = str;
992
e1b988bb 993 return reg;
a06ea964
NC
994}
995
4df068de
RS
996/* Try to parse a base or offset register. Return the register entry
997 on success, setting *QUALIFIER to the register qualifier. Return null
998 otherwise.
999
1000 Note that this function does not issue any diagnostics. */
1001
1002static const reg_entry *
1003aarch64_reg_parse_32_64 (char **ccp, aarch64_opnd_qualifier_t *qualifier)
1004{
2f4e3a2c 1005 return aarch64_addr_reg_parse (ccp, REG_TYPE_R_ZR_SP, qualifier);
4df068de
RS
1006}
1007
f11ad6bc
RS
1008/* Parse the qualifier of a vector register or vector element of type
1009 REG_TYPE. Fill in *PARSED_TYPE and return TRUE if the parsing
1010 succeeds; otherwise return FALSE.
a06ea964
NC
1011
1012 Accept only one occurrence of:
65a55fbb 1013 4b 8b 16b 2h 4h 8h 2s 4s 1d 2d
a06ea964 1014 b h s d q */
5b7c81bd 1015static bool
f11ad6bc
RS
1016parse_vector_type_for_operand (aarch64_reg_type reg_type,
1017 struct vector_type_el *parsed_type, char **str)
a06ea964
NC
1018{
1019 char *ptr = *str;
1020 unsigned width;
1021 unsigned element_size;
f06935a5 1022 enum vector_el_type type;
a06ea964
NC
1023
1024 /* skip '.' */
d50c751e 1025 gas_assert (*ptr == '.');
a06ea964
NC
1026 ptr++;
1027
2f4e3a2c 1028 if (reg_type != REG_TYPE_V || !ISDIGIT (*ptr))
a06ea964
NC
1029 {
1030 width = 0;
1031 goto elt_size;
1032 }
1033 width = strtoul (ptr, &ptr, 10);
1034 if (width != 1 && width != 2 && width != 4 && width != 8 && width != 16)
1035 {
1036 first_error_fmt (_("bad size %d in vector width specifier"), width);
5b7c81bd 1037 return false;
a06ea964
NC
1038 }
1039
dc1e8a47 1040 elt_size:
a06ea964
NC
1041 switch (TOLOWER (*ptr))
1042 {
1043 case 'b':
1044 type = NT_b;
1045 element_size = 8;
1046 break;
1047 case 'h':
1048 type = NT_h;
1049 element_size = 16;
1050 break;
1051 case 's':
1052 type = NT_s;
1053 element_size = 32;
1054 break;
1055 case 'd':
1056 type = NT_d;
1057 element_size = 64;
1058 break;
1059 case 'q':
2f4e3a2c 1060 if (reg_type != REG_TYPE_V || width == 1)
a06ea964
NC
1061 {
1062 type = NT_q;
1063 element_size = 128;
1064 break;
1065 }
1066 /* fall through. */
1067 default:
1068 if (*ptr != '\0')
1069 first_error_fmt (_("unexpected character `%c' in element size"), *ptr);
1070 else
1071 first_error (_("missing element size"));
5b7c81bd 1072 return false;
a06ea964 1073 }
65a55fbb
TC
1074 if (width != 0 && width * element_size != 64
1075 && width * element_size != 128
1076 && !(width == 2 && element_size == 16)
1077 && !(width == 4 && element_size == 8))
a06ea964
NC
1078 {
1079 first_error_fmt (_
1080 ("invalid element size %d and vector size combination %c"),
1081 width, *ptr);
5b7c81bd 1082 return false;
a06ea964
NC
1083 }
1084 ptr++;
1085
1086 parsed_type->type = type;
1087 parsed_type->width = width;
8cde0b98 1088 parsed_type->element_size = element_size;
a06ea964
NC
1089
1090 *str = ptr;
1091
5b7c81bd 1092 return true;
a06ea964
NC
1093}
1094
d50c751e
RS
1095/* *STR contains an SVE zero/merge predication suffix. Parse it into
1096 *PARSED_TYPE and point *STR at the end of the suffix. */
1097
5b7c81bd 1098static bool
d50c751e
RS
1099parse_predication_for_operand (struct vector_type_el *parsed_type, char **str)
1100{
1101 char *ptr = *str;
1102
1103 /* Skip '/'. */
1104 gas_assert (*ptr == '/');
1105 ptr++;
1106 switch (TOLOWER (*ptr))
1107 {
1108 case 'z':
1109 parsed_type->type = NT_zero;
1110 break;
1111 case 'm':
1112 parsed_type->type = NT_merge;
1113 break;
1114 default:
1115 if (*ptr != '\0' && *ptr != ',')
1116 first_error_fmt (_("unexpected character `%c' in predication type"),
1117 *ptr);
1118 else
1119 first_error (_("missing predication type"));
5b7c81bd 1120 return false;
d50c751e
RS
1121 }
1122 parsed_type->width = 0;
1123 *str = ptr + 1;
5b7c81bd 1124 return true;
d50c751e
RS
1125}
1126
c211f268
RS
1127/* Return true if CH is a valid suffix character for registers of
1128 type TYPE. */
1129
1130static bool
1131aarch64_valid_suffix_char_p (aarch64_reg_type type, char ch)
1132{
1133 switch (type)
1134 {
2f4e3a2c
RS
1135 case REG_TYPE_V:
1136 case REG_TYPE_Z:
e9e1ddbb 1137 case REG_TYPE_ZA:
c211f268
RS
1138 case REG_TYPE_ZAT:
1139 case REG_TYPE_ZATH:
1140 case REG_TYPE_ZATV:
1141 return ch == '.';
1142
2f4e3a2c 1143 case REG_TYPE_P:
503fae12 1144 case REG_TYPE_PN:
c211f268
RS
1145 return ch == '.' || ch == '/';
1146
1147 default:
1148 return false;
1149 }
1150}
1151
61dac77e
RS
1152/* Parse an index expression at *STR, storing it in *IMM on success. */
1153
1154static bool
1155parse_index_expression (char **str, int64_t *imm)
1156{
1157 expressionS exp;
1158
1159 aarch64_get_expression (&exp, str, GE_NO_PREFIX, REJECT_ABSENT);
1160 if (exp.X_op != O_constant)
1161 {
1162 first_error (_("constant expression required"));
1163 return false;
1164 }
1165 *imm = exp.X_add_number;
1166 return true;
1167}
1168
a06ea964
NC
1169/* Parse a register of the type TYPE.
1170
074c89d2 1171 Return null if the string pointed to by *CCP is not a valid register
a06ea964
NC
1172 name or the parsed register is not of TYPE.
1173
074c89d2
RS
1174 Otherwise return the register, and optionally return the register
1175 shape and element index information in *TYPEINFO.
a06ea964 1176
c211f268
RS
1177 FLAGS includes PTR_IN_REGLIST if the caller is parsing a register list.
1178
1179 FLAGS includes PTR_FULL_REG if the function should ignore any potential
405f0c41
RS
1180 register index.
1181
1182 FLAGS includes PTR_GOOD_MATCH if we are sufficiently far into parsing
1183 an operand that we can be confident that it is a good match. */
a06ea964 1184
074c89d2 1185#define PTR_IN_REGLIST (1U << 0)
c211f268 1186#define PTR_FULL_REG (1U << 1)
405f0c41 1187#define PTR_GOOD_MATCH (1U << 2)
074c89d2
RS
1188
1189static const reg_entry *
1190parse_typed_reg (char **ccp, aarch64_reg_type type,
1191 struct vector_type_el *typeinfo, unsigned int flags)
a06ea964
NC
1192{
1193 char *str = *ccp;
4f53c99c 1194 bool is_alpha = ISALPHA (*str);
a06ea964 1195 const reg_entry *reg = parse_reg (&str);
8f9a77af
RS
1196 struct vector_type_el atype;
1197 struct vector_type_el parsetype;
5b7c81bd 1198 bool is_typed_vecreg = false;
e426521e 1199 unsigned int err_flags = (flags & PTR_IN_REGLIST) ? SEF_IN_REGLIST : 0;
a06ea964
NC
1200
1201 atype.defined = 0;
1202 atype.type = NT_invtype;
1203 atype.width = -1;
8cde0b98 1204 atype.element_size = 0;
a06ea964
NC
1205 atype.index = 0;
1206
1207 if (reg == NULL)
1208 {
1209 if (typeinfo)
1210 *typeinfo = atype;
4f53c99c 1211 if (!is_alpha && (flags & PTR_IN_REGLIST))
b9ca3896 1212 set_fatal_syntax_error (_("syntax error in register list"));
405f0c41
RS
1213 else if (flags & PTR_GOOD_MATCH)
1214 set_fatal_syntax_error (NULL);
b9ca3896 1215 else
e426521e 1216 set_expected_reg_error (type, reg, err_flags);
074c89d2 1217 return NULL;
a06ea964
NC
1218 }
1219
1220 if (! aarch64_check_reg_type (reg, type))
1221 {
1222 DEBUG_TRACE ("reg type check failed");
405f0c41
RS
1223 if (flags & PTR_GOOD_MATCH)
1224 set_fatal_syntax_error (NULL);
1225 else
e426521e 1226 set_expected_reg_error (type, reg, err_flags);
074c89d2 1227 return NULL;
a06ea964
NC
1228 }
1229 type = reg->type;
1230
c211f268 1231 if (aarch64_valid_suffix_char_p (reg->type, *str))
a06ea964 1232 {
d50c751e
RS
1233 if (*str == '.')
1234 {
1235 if (!parse_vector_type_for_operand (type, &parsetype, &str))
074c89d2 1236 return NULL;
8cde0b98
RS
1237 if ((reg->type == REG_TYPE_ZAT
1238 || reg->type == REG_TYPE_ZATH
1239 || reg->type == REG_TYPE_ZATV)
4f53c99c 1240 && reg->number * 8U >= parsetype.element_size)
8cde0b98
RS
1241 {
1242 set_syntax_error (_("ZA tile number out of range"));
1243 return NULL;
1244 }
d50c751e
RS
1245 }
1246 else
1247 {
1248 if (!parse_predication_for_operand (&parsetype, &str))
074c89d2 1249 return NULL;
d50c751e 1250 }
a235d3ae 1251
a06ea964 1252 /* Register if of the form Vn.[bhsdq]. */
5b7c81bd 1253 is_typed_vecreg = true;
a06ea964 1254
2f4e3a2c 1255 if (type != REG_TYPE_V)
f11ad6bc
RS
1256 {
1257 /* The width is always variable; we don't allow an integer width
1258 to be specified. */
1259 gas_assert (parsetype.width == 0);
1260 atype.defined |= NTA_HASVARWIDTH | NTA_HASTYPE;
1261 }
1262 else if (parsetype.width == 0)
a06ea964
NC
1263 /* Expect index. In the new scheme we cannot have
1264 Vn.[bhsdq] represent a scalar. Therefore any
1265 Vn.[bhsdq] should have an index following it.
33eaf5de 1266 Except in reglists of course. */
a06ea964
NC
1267 atype.defined |= NTA_HASINDEX;
1268 else
1269 atype.defined |= NTA_HASTYPE;
1270
1271 atype.type = parsetype.type;
1272 atype.width = parsetype.width;
1273 }
1274
c211f268 1275 if (!(flags & PTR_FULL_REG) && skip_past_char (&str, '['))
a06ea964 1276 {
a06ea964 1277 /* Reject Sn[index] syntax. */
cbd11b88
RS
1278 if (reg->type != REG_TYPE_Z
1279 && reg->type != REG_TYPE_PN
1280 && reg->type != REG_TYPE_ZT0
1281 && !is_typed_vecreg)
a06ea964
NC
1282 {
1283 first_error (_("this type of register can't be indexed"));
074c89d2 1284 return NULL;
a06ea964
NC
1285 }
1286
074c89d2 1287 if (flags & PTR_IN_REGLIST)
a06ea964
NC
1288 {
1289 first_error (_("index not allowed inside register list"));
074c89d2 1290 return NULL;
a06ea964
NC
1291 }
1292
1293 atype.defined |= NTA_HASINDEX;
1294
61dac77e
RS
1295 if (!parse_index_expression (&str, &atype.index))
1296 return NULL;
a06ea964
NC
1297
1298 if (! skip_past_char (&str, ']'))
074c89d2 1299 return NULL;
a06ea964 1300 }
074c89d2 1301 else if (!(flags & PTR_IN_REGLIST) && (atype.defined & NTA_HASINDEX) != 0)
a06ea964
NC
1302 {
1303 /* Indexed vector register expected. */
1304 first_error (_("indexed vector register expected"));
074c89d2 1305 return NULL;
a06ea964
NC
1306 }
1307
1308 /* A vector reg Vn should be typed or indexed. */
2f4e3a2c 1309 if (type == REG_TYPE_V && atype.defined == 0)
a06ea964
NC
1310 {
1311 first_error (_("invalid use of vector register"));
1312 }
1313
1314 if (typeinfo)
1315 *typeinfo = atype;
1316
a06ea964
NC
1317 *ccp = str;
1318
074c89d2 1319 return reg;
a06ea964
NC
1320}
1321
1322/* Parse register.
1323
074c89d2 1324 Return the register on success; return null otherwise.
a06ea964
NC
1325
1326 If this is a NEON vector register with additional type information, fill
1327 in the struct pointed to by VECTYPE (if non-NULL).
1328
074c89d2 1329 This parser does not handle register lists. */
a06ea964 1330
074c89d2 1331static const reg_entry *
a06ea964 1332aarch64_reg_parse (char **ccp, aarch64_reg_type type,
074c89d2 1333 struct vector_type_el *vectype)
a06ea964 1334{
074c89d2 1335 return parse_typed_reg (ccp, type, vectype, 0);
a06ea964
NC
1336}
1337
5b7c81bd 1338static inline bool
8f9a77af 1339eq_vector_type_el (struct vector_type_el e1, struct vector_type_el e2)
a06ea964 1340{
8cde0b98
RS
1341 return (e1.type == e2.type
1342 && e1.defined == e2.defined
1343 && e1.width == e2.width
1344 && e1.element_size == e2.element_size
1345 && e1.index == e2.index);
a06ea964
NC
1346}
1347
99e01a66
RS
1348/* Return the register number mask for registers of type REG_TYPE. */
1349
1350static inline int
1351reg_type_mask (aarch64_reg_type reg_type)
1352{
1353 return reg_type == REG_TYPE_P ? 15 : 31;
1354}
1355
10d76650
RS
1356/* This function parses a list of vector registers of type TYPE.
1357 On success, it returns the parsed register list information in the
1358 following encoded format:
a06ea964
NC
1359
1360 bit 18-22 | 13-17 | 7-11 | 2-6 | 0-1
1361 4th regno | 3rd regno | 2nd regno | 1st regno | num_of_reg
1362
1363 The information of the register shape and/or index is returned in
1364 *VECTYPE.
1365
1366 It returns PARSE_FAIL if the register list is invalid.
1367
1368 The list contains one to four registers.
1369 Each register can be one of:
1370 <Vt>.<T>[<index>]
1371 <Vt>.<T>
1372 All <T> should be identical.
1373 All <index> should be identical.
1374 There are restrictions on <Vt> numbers which are checked later
1375 (by reg_list_valid_p). */
1376
1377static int
10d76650
RS
1378parse_vector_reg_list (char **ccp, aarch64_reg_type type,
1379 struct vector_type_el *vectype)
a06ea964
NC
1380{
1381 char *str = *ccp;
1382 int nb_regs;
8f9a77af 1383 struct vector_type_el typeinfo, typeinfo_first;
f679aaa4 1384 uint32_t val, val_range, mask;
a06ea964
NC
1385 int in_range;
1386 int ret_val;
5b7c81bd
AM
1387 bool error = false;
1388 bool expect_index = false;
405f0c41 1389 unsigned int ptr_flags = PTR_IN_REGLIST;
a06ea964
NC
1390
1391 if (*str != '{')
1392 {
e426521e 1393 set_expected_reglist_error (type, parse_reg (&str));
a06ea964
NC
1394 return PARSE_FAIL;
1395 }
1396 str++;
1397
1398 nb_regs = 0;
1399 typeinfo_first.defined = 0;
1400 typeinfo_first.type = NT_invtype;
1401 typeinfo_first.width = -1;
8cde0b98 1402 typeinfo_first.element_size = 0;
a06ea964
NC
1403 typeinfo_first.index = 0;
1404 ret_val = 0;
f679aaa4
AM
1405 val = -1u;
1406 val_range = -1u;
a06ea964 1407 in_range = 0;
99e01a66 1408 mask = reg_type_mask (type);
a06ea964
NC
1409 do
1410 {
1411 if (in_range)
1412 {
1413 str++; /* skip over '-' */
1414 val_range = val;
1415 }
074c89d2 1416 const reg_entry *reg = parse_typed_reg (&str, type, &typeinfo,
405f0c41 1417 ptr_flags);
074c89d2 1418 if (!reg)
a06ea964
NC
1419 {
1420 set_first_syntax_error (_("invalid vector register in list"));
5b7c81bd 1421 error = true;
a06ea964
NC
1422 continue;
1423 }
074c89d2 1424 val = reg->number;
a06ea964 1425 /* reject [bhsd]n */
2f4e3a2c 1426 if (type == REG_TYPE_V && typeinfo.defined == 0)
a06ea964
NC
1427 {
1428 set_first_syntax_error (_("invalid scalar register in list"));
5b7c81bd 1429 error = true;
a06ea964
NC
1430 continue;
1431 }
1432
1433 if (typeinfo.defined & NTA_HASINDEX)
5b7c81bd 1434 expect_index = true;
a06ea964
NC
1435
1436 if (in_range)
1437 {
4eede8c2 1438 if (val == val_range)
a06ea964
NC
1439 {
1440 set_first_syntax_error
1441 (_("invalid range in vector register list"));
5b7c81bd 1442 error = true;
a06ea964 1443 }
99e01a66 1444 val_range = (val_range + 1) & mask;
a06ea964
NC
1445 }
1446 else
1447 {
1448 val_range = val;
1449 if (nb_regs == 0)
1450 typeinfo_first = typeinfo;
8f9a77af 1451 else if (! eq_vector_type_el (typeinfo_first, typeinfo))
a06ea964
NC
1452 {
1453 set_first_syntax_error
1454 (_("type mismatch in vector register list"));
5b7c81bd 1455 error = true;
a06ea964
NC
1456 }
1457 }
1458 if (! error)
4eede8c2 1459 for (;;)
a06ea964 1460 {
f679aaa4 1461 ret_val |= val_range << ((5 * nb_regs) & 31);
a06ea964 1462 nb_regs++;
4eede8c2
RS
1463 if (val_range == val)
1464 break;
99e01a66 1465 val_range = (val_range + 1) & mask;
a06ea964
NC
1466 }
1467 in_range = 0;
405f0c41 1468 ptr_flags |= PTR_GOOD_MATCH;
a06ea964
NC
1469 }
1470 while (skip_past_comma (&str) || (in_range = 1, *str == '-'));
1471
1472 skip_whitespace (str);
1473 if (*str != '}')
1474 {
1475 set_first_syntax_error (_("end of vector register list not found"));
5b7c81bd 1476 error = true;
a06ea964
NC
1477 }
1478 str++;
1479
1480 skip_whitespace (str);
1481
1482 if (expect_index)
1483 {
1484 if (skip_past_char (&str, '['))
1485 {
61dac77e
RS
1486 if (!parse_index_expression (&str, &typeinfo_first.index))
1487 error = true;
a06ea964 1488 if (! skip_past_char (&str, ']'))
5b7c81bd 1489 error = true;
a06ea964
NC
1490 }
1491 else
1492 {
1493 set_first_syntax_error (_("expected index"));
5b7c81bd 1494 error = true;
a06ea964
NC
1495 }
1496 }
1497
1498 if (nb_regs > 4)
1499 {
1500 set_first_syntax_error (_("too many registers in vector register list"));
5b7c81bd 1501 error = true;
a06ea964
NC
1502 }
1503 else if (nb_regs == 0)
1504 {
1505 set_first_syntax_error (_("empty vector register list"));
5b7c81bd 1506 error = true;
a06ea964
NC
1507 }
1508
1509 *ccp = str;
1510 if (! error)
1511 *vectype = typeinfo_first;
1512
1513 return error ? PARSE_FAIL : (ret_val << 2) | (nb_regs - 1);
1514}
1515
1516/* Directives: register aliases. */
1517
1518static reg_entry *
1519insert_reg_alias (char *str, int number, aarch64_reg_type type)
1520{
1521 reg_entry *new;
1522 const char *name;
1523
629310ab 1524 if ((new = str_hash_find (aarch64_reg_hsh, str)) != 0)
a06ea964
NC
1525 {
1526 if (new->builtin)
1527 as_warn (_("ignoring attempt to redefine built-in register '%s'"),
1528 str);
1529
1530 /* Only warn about a redefinition if it's not defined as the
1531 same register. */
1532 else if (new->number != number || new->type != type)
1533 as_warn (_("ignoring redefinition of register alias '%s'"), str);
1534
1535 return NULL;
1536 }
1537
1538 name = xstrdup (str);
add39d23 1539 new = XNEW (reg_entry);
a06ea964
NC
1540
1541 new->name = name;
1542 new->number = number;
1543 new->type = type;
5b7c81bd 1544 new->builtin = false;
a06ea964 1545
fe0e921f 1546 str_hash_insert (aarch64_reg_hsh, name, new, 0);
a06ea964
NC
1547
1548 return new;
1549}
1550
1551/* Look for the .req directive. This is of the form:
1552
1553 new_register_name .req existing_register_name
1554
1555 If we find one, or if it looks sufficiently like one that we want to
1556 handle any error here, return TRUE. Otherwise return FALSE. */
1557
5b7c81bd 1558static bool
a06ea964
NC
1559create_register_alias (char *newname, char *p)
1560{
1561 const reg_entry *old;
1562 char *oldname, *nbuf;
1563 size_t nlen;
1564
1565 /* The input scrubber ensures that whitespace after the mnemonic is
1566 collapsed to single spaces. */
1567 oldname = p;
d34049e8 1568 if (!startswith (oldname, " .req "))
5b7c81bd 1569 return false;
a06ea964
NC
1570
1571 oldname += 6;
1572 if (*oldname == '\0')
5b7c81bd 1573 return false;
a06ea964 1574
629310ab 1575 old = str_hash_find (aarch64_reg_hsh, oldname);
a06ea964
NC
1576 if (!old)
1577 {
1578 as_warn (_("unknown register '%s' -- .req ignored"), oldname);
5b7c81bd 1579 return true;
a06ea964
NC
1580 }
1581
1582 /* If TC_CASE_SENSITIVE is defined, then newname already points to
1583 the desired alias name, and p points to its end. If not, then
1584 the desired alias name is in the global original_case_string. */
1585#ifdef TC_CASE_SENSITIVE
1586 nlen = p - newname;
1587#else
1588 newname = original_case_string;
1589 nlen = strlen (newname);
1590#endif
1591
29a2809e 1592 nbuf = xmemdup0 (newname, nlen);
a06ea964
NC
1593
1594 /* Create aliases under the new name as stated; an all-lowercase
1595 version of the new name; and an all-uppercase version of the new
1596 name. */
1597 if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
1598 {
1599 for (p = nbuf; *p; p++)
1600 *p = TOUPPER (*p);
1601
1602 if (strncmp (nbuf, newname, nlen))
1603 {
1604 /* If this attempt to create an additional alias fails, do not bother
1605 trying to create the all-lower case alias. We will fail and issue
1606 a second, duplicate error message. This situation arises when the
1607 programmer does something like:
1608 foo .req r0
1609 Foo .req r1
1610 The second .req creates the "Foo" alias but then fails to create
1611 the artificial FOO alias because it has already been created by the
1612 first .req. */
1613 if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
e1fa0163
NC
1614 {
1615 free (nbuf);
5b7c81bd 1616 return true;
e1fa0163 1617 }
a06ea964
NC
1618 }
1619
1620 for (p = nbuf; *p; p++)
1621 *p = TOLOWER (*p);
1622
1623 if (strncmp (nbuf, newname, nlen))
1624 insert_reg_alias (nbuf, old->number, old->type);
1625 }
1626
e1fa0163 1627 free (nbuf);
5b7c81bd 1628 return true;
a06ea964
NC
1629}
1630
1631/* Should never be called, as .req goes between the alias and the
1632 register name, not at the beginning of the line. */
1633static void
1634s_req (int a ATTRIBUTE_UNUSED)
1635{
1636 as_bad (_("invalid syntax for .req directive"));
1637}
1638
1639/* The .unreq directive deletes an alias which was previously defined
1640 by .req. For example:
1641
1642 my_alias .req r11
1643 .unreq my_alias */
1644
1645static void
1646s_unreq (int a ATTRIBUTE_UNUSED)
1647{
1648 char *name;
1649 char saved_char;
1650
1651 name = input_line_pointer;
e8f20526 1652 input_line_pointer = find_end_of_line (input_line_pointer, flag_m68k_mri);
a06ea964
NC
1653 saved_char = *input_line_pointer;
1654 *input_line_pointer = 0;
1655
1656 if (!*name)
1657 as_bad (_("invalid syntax for .unreq directive"));
1658 else
1659 {
629310ab 1660 reg_entry *reg = str_hash_find (aarch64_reg_hsh, name);
a06ea964
NC
1661
1662 if (!reg)
1663 as_bad (_("unknown register alias '%s'"), name);
1664 else if (reg->builtin)
1665 as_warn (_("ignoring attempt to undefine built-in register '%s'"),
1666 name);
1667 else
1668 {
1669 char *p;
1670 char *nbuf;
1671
629310ab 1672 str_hash_delete (aarch64_reg_hsh, name);
a06ea964
NC
1673 free ((char *) reg->name);
1674 free (reg);
1675
1676 /* Also locate the all upper case and all lower case versions.
1677 Do not complain if we cannot find one or the other as it
1678 was probably deleted above. */
1679
1680 nbuf = strdup (name);
1681 for (p = nbuf; *p; p++)
1682 *p = TOUPPER (*p);
629310ab 1683 reg = str_hash_find (aarch64_reg_hsh, nbuf);
a06ea964
NC
1684 if (reg)
1685 {
629310ab 1686 str_hash_delete (aarch64_reg_hsh, nbuf);
a06ea964
NC
1687 free ((char *) reg->name);
1688 free (reg);
1689 }
1690
1691 for (p = nbuf; *p; p++)
1692 *p = TOLOWER (*p);
629310ab 1693 reg = str_hash_find (aarch64_reg_hsh, nbuf);
a06ea964
NC
1694 if (reg)
1695 {
629310ab 1696 str_hash_delete (aarch64_reg_hsh, nbuf);
a06ea964
NC
1697 free ((char *) reg->name);
1698 free (reg);
1699 }
1700
1701 free (nbuf);
1702 }
1703 }
1704
1705 *input_line_pointer = saved_char;
1706 demand_empty_rest_of_line ();
1707}
1708
1709/* Directives: Instruction set selection. */
1710
c60b3806 1711#if defined OBJ_ELF || defined OBJ_COFF
a06ea964
NC
1712/* This code is to handle mapping symbols as defined in the ARM AArch64 ELF
1713 spec. (See "Mapping symbols", section 4.5.4, ARM AAELF64 version 0.05).
1714 Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
1715 and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped. */
1716
1717/* Create a new mapping symbol for the transition to STATE. */
1718
1719static void
1720make_mapping_symbol (enum mstate state, valueT value, fragS * frag)
1721{
1722 symbolS *symbolP;
1723 const char *symname;
1724 int type;
1725
1726 switch (state)
1727 {
1728 case MAP_DATA:
1729 symname = "$d";
1730 type = BSF_NO_FLAGS;
1731 break;
1732 case MAP_INSN:
1733 symname = "$x";
1734 type = BSF_NO_FLAGS;
1735 break;
1736 default:
1737 abort ();
1738 }
1739
e01e1cee 1740 symbolP = symbol_new (symname, now_seg, frag, value);
a06ea964
NC
1741 symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
1742
1743 /* Save the mapping symbols for future reference. Also check that
1744 we do not place two mapping symbols at the same offset within a
1745 frag. We'll handle overlap between frags in
1746 check_mapping_symbols.
1747
1748 If .fill or other data filling directive generates zero sized data,
1749 the mapping symbol for the following code will have the same value
1750 as the one generated for the data filling directive. In this case,
1751 we replace the old symbol with the new one at the same address. */
1752 if (value == 0)
1753 {
1754 if (frag->tc_frag_data.first_map != NULL)
1755 {
1756 know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
1757 symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP,
1758 &symbol_lastP);
1759 }
1760 frag->tc_frag_data.first_map = symbolP;
1761 }
1762 if (frag->tc_frag_data.last_map != NULL)
1763 {
1764 know (S_GET_VALUE (frag->tc_frag_data.last_map) <=
1765 S_GET_VALUE (symbolP));
1766 if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
1767 symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP,
1768 &symbol_lastP);
1769 }
1770 frag->tc_frag_data.last_map = symbolP;
1771}
1772
1773/* We must sometimes convert a region marked as code to data during
1774 code alignment, if an odd number of bytes have to be padded. The
1775 code mapping symbol is pushed to an aligned address. */
1776
1777static void
1778insert_data_mapping_symbol (enum mstate state,
1779 valueT value, fragS * frag, offsetT bytes)
1780{
1781 /* If there was already a mapping symbol, remove it. */
1782 if (frag->tc_frag_data.last_map != NULL
1783 && S_GET_VALUE (frag->tc_frag_data.last_map) ==
1784 frag->fr_address + value)
1785 {
1786 symbolS *symp = frag->tc_frag_data.last_map;
1787
1788 if (value == 0)
1789 {
1790 know (frag->tc_frag_data.first_map == symp);
1791 frag->tc_frag_data.first_map = NULL;
1792 }
1793 frag->tc_frag_data.last_map = NULL;
1794 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1795 }
1796
1797 make_mapping_symbol (MAP_DATA, value, frag);
1798 make_mapping_symbol (state, value + bytes, frag);
1799}
1800
1801static void mapping_state_2 (enum mstate state, int max_chars);
1802
1803/* Set the mapping state to STATE. Only call this when about to
1804 emit some STATE bytes to the file. */
1805
1806void
1807mapping_state (enum mstate state)
1808{
1809 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
1810
a578ef7e
JW
1811 if (state == MAP_INSN)
1812 /* AArch64 instructions require 4-byte alignment. When emitting
1813 instructions into any section, record the appropriate section
1814 alignment. */
1815 record_alignment (now_seg, 2);
1816
448eb63d
RL
1817 if (mapstate == state)
1818 /* The mapping symbol has already been emitted.
1819 There is nothing else to do. */
1820 return;
1821
c1baaddf 1822#define TRANSITION(from, to) (mapstate == (from) && state == (to))
a97902de
RL
1823 if (TRANSITION (MAP_UNDEFINED, MAP_DATA) && !subseg_text_p (now_seg))
1824 /* Emit MAP_DATA within executable section in order. Otherwise, it will be
c1baaddf 1825 evaluated later in the next else. */
a06ea964 1826 return;
c1baaddf
RL
1827 else if (TRANSITION (MAP_UNDEFINED, MAP_INSN))
1828 {
1829 /* Only add the symbol if the offset is > 0:
1830 if we're at the first frag, check it's size > 0;
1831 if we're not at the first frag, then for sure
1832 the offset is > 0. */
1833 struct frag *const frag_first = seg_info (now_seg)->frchainP->frch_root;
1834 const int add_symbol = (frag_now != frag_first)
1835 || (frag_now_fix () > 0);
1836
1837 if (add_symbol)
1838 make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
1839 }
1840#undef TRANSITION
a06ea964
NC
1841
1842 mapping_state_2 (state, 0);
a06ea964
NC
1843}
1844
1845/* Same as mapping_state, but MAX_CHARS bytes have already been
1846 allocated. Put the mapping symbol that far back. */
1847
1848static void
1849mapping_state_2 (enum mstate state, int max_chars)
1850{
1851 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
1852
1853 if (!SEG_NORMAL (now_seg))
1854 return;
1855
1856 if (mapstate == state)
1857 /* The mapping symbol has already been emitted.
1858 There is nothing else to do. */
1859 return;
1860
1861 seg_info (now_seg)->tc_segment_info_data.mapstate = state;
1862 make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
1863}
1864#else
1865#define mapping_state(x) /* nothing */
1866#define mapping_state_2(x, y) /* nothing */
1867#endif
1868
3e73bcf7 1869/* Directives: alignment. */
a06ea964
NC
1870
1871static void
1872s_even (int ignore ATTRIBUTE_UNUSED)
1873{
1874 /* Never make frag if expect extra pass. */
1875 if (!need_pass_2)
1876 frag_align (1, 0, 0);
1877
1878 record_alignment (now_seg, 1);
1879
1880 demand_empty_rest_of_line ();
1881}
1882
1883/* Directives: Literal pools. */
1884
1885static literal_pool *
1886find_literal_pool (int size)
1887{
1888 literal_pool *pool;
1889
1890 for (pool = list_of_pools; pool != NULL; pool = pool->next)
1891 {
1892 if (pool->section == now_seg
1893 && pool->sub_section == now_subseg && pool->size == size)
1894 break;
1895 }
1896
1897 return pool;
1898}
1899
1900static literal_pool *
1901find_or_make_literal_pool (int size)
1902{
1903 /* Next literal pool ID number. */
1904 static unsigned int latest_pool_num = 1;
1905 literal_pool *pool;
1906
1907 pool = find_literal_pool (size);
1908
1909 if (pool == NULL)
1910 {
1911 /* Create a new pool. */
add39d23 1912 pool = XNEW (literal_pool);
a06ea964
NC
1913 if (!pool)
1914 return NULL;
1915
1916 /* Currently we always put the literal pool in the current text
1917 section. If we were generating "small" model code where we
1918 knew that all code and initialised data was within 1MB then
1919 we could output literals to mergeable, read-only data
1920 sections. */
1921
1922 pool->next_free_entry = 0;
1923 pool->section = now_seg;
1924 pool->sub_section = now_subseg;
1925 pool->size = size;
1926 pool->next = list_of_pools;
1927 pool->symbol = NULL;
1928
1929 /* Add it to the list. */
1930 list_of_pools = pool;
1931 }
1932
1933 /* New pools, and emptied pools, will have a NULL symbol. */
1934 if (pool->symbol == NULL)
1935 {
1936 pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
e01e1cee 1937 &zero_address_frag, 0);
a06ea964
NC
1938 pool->id = latest_pool_num++;
1939 }
1940
1941 /* Done. */
1942 return pool;
1943}
1944
1945/* Add the literal of size SIZE in *EXP to the relevant literal pool.
1946 Return TRUE on success, otherwise return FALSE. */
5b7c81bd 1947static bool
a06ea964
NC
1948add_to_lit_pool (expressionS *exp, int size)
1949{
1950 literal_pool *pool;
1951 unsigned int entry;
1952
1953 pool = find_or_make_literal_pool (size);
1954
1955 /* Check if this literal value is already in the pool. */
1956 for (entry = 0; entry < pool->next_free_entry; entry++)
1957 {
55d9b4c1
NC
1958 expressionS * litexp = & pool->literals[entry].exp;
1959
1960 if ((litexp->X_op == exp->X_op)
a06ea964 1961 && (exp->X_op == O_constant)
55d9b4c1
NC
1962 && (litexp->X_add_number == exp->X_add_number)
1963 && (litexp->X_unsigned == exp->X_unsigned))
a06ea964
NC
1964 break;
1965
55d9b4c1 1966 if ((litexp->X_op == exp->X_op)
a06ea964 1967 && (exp->X_op == O_symbol)
55d9b4c1
NC
1968 && (litexp->X_add_number == exp->X_add_number)
1969 && (litexp->X_add_symbol == exp->X_add_symbol)
1970 && (litexp->X_op_symbol == exp->X_op_symbol))
a06ea964
NC
1971 break;
1972 }
1973
1974 /* Do we need to create a new entry? */
1975 if (entry == pool->next_free_entry)
1976 {
1977 if (entry >= MAX_LITERAL_POOL_SIZE)
1978 {
1979 set_syntax_error (_("literal pool overflow"));
5b7c81bd 1980 return false;
a06ea964
NC
1981 }
1982
55d9b4c1 1983 pool->literals[entry].exp = *exp;
a06ea964 1984 pool->next_free_entry += 1;
55d9b4c1
NC
1985 if (exp->X_op == O_big)
1986 {
1987 /* PR 16688: Bignums are held in a single global array. We must
1988 copy and preserve that value now, before it is overwritten. */
add39d23
TS
1989 pool->literals[entry].bignum = XNEWVEC (LITTLENUM_TYPE,
1990 exp->X_add_number);
55d9b4c1
NC
1991 memcpy (pool->literals[entry].bignum, generic_bignum,
1992 CHARS_PER_LITTLENUM * exp->X_add_number);
1993 }
1994 else
1995 pool->literals[entry].bignum = NULL;
a06ea964
NC
1996 }
1997
1998 exp->X_op = O_symbol;
1999 exp->X_add_number = ((int) entry) * size;
2000 exp->X_add_symbol = pool->symbol;
2001
5b7c81bd 2002 return true;
a06ea964
NC
2003}
2004
2005/* Can't use symbol_new here, so have to create a symbol and then at
33eaf5de 2006 a later date assign it a value. That's what these functions do. */
a06ea964
NC
2007
2008static void
2009symbol_locate (symbolS * symbolP,
2010 const char *name,/* It is copied, the caller can modify. */
2011 segT segment, /* Segment identifier (SEG_<something>). */
2012 valueT valu, /* Symbol value. */
2013 fragS * frag) /* Associated fragment. */
2014{
e57e6ddc 2015 size_t name_length;
a06ea964
NC
2016 char *preserved_copy_of_name;
2017
2018 name_length = strlen (name) + 1; /* +1 for \0. */
2019 obstack_grow (&notes, name, name_length);
2020 preserved_copy_of_name = obstack_finish (&notes);
2021
2022#ifdef tc_canonicalize_symbol_name
2023 preserved_copy_of_name =
2024 tc_canonicalize_symbol_name (preserved_copy_of_name);
2025#endif
2026
2027 S_SET_NAME (symbolP, preserved_copy_of_name);
2028
2029 S_SET_SEGMENT (symbolP, segment);
2030 S_SET_VALUE (symbolP, valu);
2031 symbol_clear_list_pointers (symbolP);
2032
2033 symbol_set_frag (symbolP, frag);
2034
2035 /* Link to end of symbol chain. */
2036 {
2037 extern int symbol_table_frozen;
2038
2039 if (symbol_table_frozen)
2040 abort ();
2041 }
2042
2043 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
2044
2045 obj_symbol_new_hook (symbolP);
2046
2047#ifdef tc_symbol_new_hook
2048 tc_symbol_new_hook (symbolP);
2049#endif
2050
2051#ifdef DEBUG_SYMS
2052 verify_symbol_chain (symbol_rootP, symbol_lastP);
2053#endif /* DEBUG_SYMS */
2054}
2055
2056
2057static void
2058s_ltorg (int ignored ATTRIBUTE_UNUSED)
2059{
2060 unsigned int entry;
2061 literal_pool *pool;
2062 char sym_name[20];
2063 int align;
2064
67a32447 2065 for (align = 2; align <= 4; align++)
a06ea964
NC
2066 {
2067 int size = 1 << align;
2068
2069 pool = find_literal_pool (size);
2070 if (pool == NULL || pool->symbol == NULL || pool->next_free_entry == 0)
2071 continue;
2072
a06ea964
NC
2073 /* Align pool as you have word accesses.
2074 Only make a frag if we have to. */
2075 if (!need_pass_2)
2076 frag_align (align, 0, 0);
2077
7ea12e5c
NC
2078 mapping_state (MAP_DATA);
2079
a06ea964
NC
2080 record_alignment (now_seg, align);
2081
2082 sprintf (sym_name, "$$lit_\002%x", pool->id);
2083
2084 symbol_locate (pool->symbol, sym_name, now_seg,
2085 (valueT) frag_now_fix (), frag_now);
2086 symbol_table_insert (pool->symbol);
2087
2088 for (entry = 0; entry < pool->next_free_entry; entry++)
55d9b4c1
NC
2089 {
2090 expressionS * exp = & pool->literals[entry].exp;
2091
2092 if (exp->X_op == O_big)
2093 {
2094 /* PR 16688: Restore the global bignum value. */
2095 gas_assert (pool->literals[entry].bignum != NULL);
2096 memcpy (generic_bignum, pool->literals[entry].bignum,
2097 CHARS_PER_LITTLENUM * exp->X_add_number);
2098 }
2099
2100 /* First output the expression in the instruction to the pool. */
2101 emit_expr (exp, size); /* .word|.xword */
2102
2103 if (exp->X_op == O_big)
2104 {
2105 free (pool->literals[entry].bignum);
2106 pool->literals[entry].bignum = NULL;
2107 }
2108 }
a06ea964
NC
2109
2110 /* Mark the pool as empty. */
2111 pool->next_free_entry = 0;
2112 pool->symbol = NULL;
2113 }
2114}
2115
b152649d 2116#if defined(OBJ_ELF) || defined(OBJ_COFF)
a06ea964
NC
2117/* Forward declarations for functions below, in the MD interface
2118 section. */
a06ea964
NC
2119static struct reloc_table_entry * find_reloc_table_entry (char **);
2120
2121/* Directives: Data. */
2122/* N.B. the support for relocation suffix in this directive needs to be
2123 implemented properly. */
2124
2125static void
b152649d 2126s_aarch64_cons (int nbytes)
a06ea964
NC
2127{
2128 expressionS exp;
2129
2130#ifdef md_flush_pending_output
2131 md_flush_pending_output ();
2132#endif
2133
2134 if (is_it_end_of_statement ())
2135 {
2136 demand_empty_rest_of_line ();
2137 return;
2138 }
2139
2140#ifdef md_cons_align
2141 md_cons_align (nbytes);
2142#endif
2143
2144 mapping_state (MAP_DATA);
2145 do
2146 {
2147 struct reloc_table_entry *reloc;
2148
2149 expression (&exp);
2150
2151 if (exp.X_op != O_symbol)
2152 emit_expr (&exp, (unsigned int) nbytes);
2153 else
2154 {
2155 skip_past_char (&input_line_pointer, '#');
2156 if (skip_past_char (&input_line_pointer, ':'))
2157 {
2158 reloc = find_reloc_table_entry (&input_line_pointer);
2159 if (reloc == NULL)
2160 as_bad (_("unrecognized relocation suffix"));
2161 else
2162 as_bad (_("unimplemented relocation suffix"));
2163 ignore_rest_of_line ();
2164 return;
2165 }
2166 else
2167 emit_expr (&exp, (unsigned int) nbytes);
2168 }
2169 }
2170 while (*input_line_pointer++ == ',');
2171
2172 /* Put terminator back into stream. */
2173 input_line_pointer--;
2174 demand_empty_rest_of_line ();
2175}
b152649d
MH
2176#endif
2177
2178#ifdef OBJ_ELF
2179/* Forward declarations for functions below, in the MD interface
2180 section. */
2181 static fixS *fix_new_aarch64 (fragS *, int, short, expressionS *, int, int);
a06ea964 2182
f166ae01
SN
2183/* Mark symbol that it follows a variant PCS convention. */
2184
2185static void
2186s_variant_pcs (int ignored ATTRIBUTE_UNUSED)
2187{
2188 char *name;
2189 char c;
2190 symbolS *sym;
2191 asymbol *bfdsym;
2192 elf_symbol_type *elfsym;
2193
2194 c = get_symbol_name (&name);
2195 if (!*name)
2196 as_bad (_("Missing symbol name in directive"));
2197 sym = symbol_find_or_make (name);
2198 restore_line_pointer (c);
2199 demand_empty_rest_of_line ();
2200 bfdsym = symbol_get_bfdsym (sym);
c1229f84 2201 elfsym = elf_symbol_from (bfdsym);
f166ae01
SN
2202 gas_assert (elfsym);
2203 elfsym->internal_elf_sym.st_other |= STO_AARCH64_VARIANT_PCS;
2204}
a06ea964
NC
2205#endif /* OBJ_ELF */
2206
2207/* Output a 32-bit word, but mark as an instruction. */
2208
2209static void
2210s_aarch64_inst (int ignored ATTRIBUTE_UNUSED)
2211{
2212 expressionS exp;
0f47cb17 2213 unsigned n = 0;
a06ea964
NC
2214
2215#ifdef md_flush_pending_output
2216 md_flush_pending_output ();
2217#endif
2218
2219 if (is_it_end_of_statement ())
2220 {
2221 demand_empty_rest_of_line ();
2222 return;
2223 }
2224
a97902de 2225 /* Sections are assumed to start aligned. In executable section, there is no
c1baaddf
RL
2226 MAP_DATA symbol pending. So we only align the address during
2227 MAP_DATA --> MAP_INSN transition.
eb9d6cc9 2228 For other sections, this is not guaranteed. */
c1baaddf 2229 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
eb9d6cc9 2230 if (!need_pass_2 && subseg_text_p (now_seg) && mapstate == MAP_DATA)
a06ea964 2231 frag_align_code (2, 0);
c1baaddf 2232
a06ea964
NC
2233#ifdef OBJ_ELF
2234 mapping_state (MAP_INSN);
2235#endif
2236
2237 do
2238 {
2239 expression (&exp);
2240 if (exp.X_op != O_constant)
2241 {
2242 as_bad (_("constant expression required"));
2243 ignore_rest_of_line ();
2244 return;
2245 }
2246
2247 if (target_big_endian)
2248 {
2249 unsigned int val = exp.X_add_number;
2250 exp.X_add_number = SWAP_32 (val);
2251 }
0f47cb17
JB
2252 emit_expr (&exp, INSN_SIZE);
2253 ++n;
a06ea964
NC
2254 }
2255 while (*input_line_pointer++ == ',');
2256
0f47cb17
JB
2257 dwarf2_emit_insn (n * INSN_SIZE);
2258
a06ea964
NC
2259 /* Put terminator back into stream. */
2260 input_line_pointer--;
2261 demand_empty_rest_of_line ();
2262}
2263
3a67e1a6
ST
2264static void
2265s_aarch64_cfi_b_key_frame (int ignored ATTRIBUTE_UNUSED)
2266{
2267 demand_empty_rest_of_line ();
2268 struct fde_entry *fde = frchain_now->frch_cfi_data->cur_fde_data;
2269 fde->pauth_key = AARCH64_PAUTH_KEY_B;
2270}
2271
a06ea964 2272#ifdef OBJ_ELF
43a357f9
RL
2273/* Emit BFD_RELOC_AARCH64_TLSDESC_ADD on the next ADD instruction. */
2274
2275static void
2276s_tlsdescadd (int ignored ATTRIBUTE_UNUSED)
2277{
2278 expressionS exp;
2279
2280 expression (&exp);
2281 frag_grow (4);
2282 fix_new_aarch64 (frag_now, frag_more (0) - frag_now->fr_literal, 4, &exp, 0,
2283 BFD_RELOC_AARCH64_TLSDESC_ADD);
2284
2285 demand_empty_rest_of_line ();
2286}
2287
a06ea964
NC
2288/* Emit BFD_RELOC_AARCH64_TLSDESC_CALL on the next BLR instruction. */
2289
2290static void
2291s_tlsdesccall (int ignored ATTRIBUTE_UNUSED)
2292{
2293 expressionS exp;
2294
2295 /* Since we're just labelling the code, there's no need to define a
2296 mapping symbol. */
2297 expression (&exp);
2298 /* Make sure there is enough room in this frag for the following
2299 blr. This trick only works if the blr follows immediately after
2300 the .tlsdesc directive. */
2301 frag_grow (4);
2302 fix_new_aarch64 (frag_now, frag_more (0) - frag_now->fr_literal, 4, &exp, 0,
2303 BFD_RELOC_AARCH64_TLSDESC_CALL);
2304
2305 demand_empty_rest_of_line ();
2306}
43a357f9
RL
2307
2308/* Emit BFD_RELOC_AARCH64_TLSDESC_LDR on the next LDR instruction. */
2309
2310static void
2311s_tlsdescldr (int ignored ATTRIBUTE_UNUSED)
2312{
2313 expressionS exp;
2314
2315 expression (&exp);
2316 frag_grow (4);
2317 fix_new_aarch64 (frag_now, frag_more (0) - frag_now->fr_literal, 4, &exp, 0,
2318 BFD_RELOC_AARCH64_TLSDESC_LDR);
2319
2320 demand_empty_rest_of_line ();
2321}
a06ea964
NC
2322#endif /* OBJ_ELF */
2323
528e4f46
MH
2324#ifdef TE_PE
2325static void
2326s_secrel (int dummy ATTRIBUTE_UNUSED)
2327{
2328 expressionS exp;
2329
2330 do
2331 {
2332 expression (&exp);
2333 if (exp.X_op == O_symbol)
2334 exp.X_op = O_secrel;
2335
2336 emit_expr (&exp, 4);
2337 }
2338 while (*input_line_pointer++ == ',');
2339
2340 input_line_pointer--;
2341 demand_empty_rest_of_line ();
2342}
7697bc65
MH
2343
2344void
2345tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
2346{
2347 expressionS exp;
2348
2349 exp.X_op = O_secrel;
2350 exp.X_add_symbol = symbol;
2351 exp.X_add_number = 0;
2352 emit_expr (&exp, size);
2353}
6f4a117f
MH
2354
2355static void
2356s_secidx (int dummy ATTRIBUTE_UNUSED)
2357{
2358 expressionS exp;
2359
2360 do
2361 {
2362 expression (&exp);
2363 if (exp.X_op == O_symbol)
2364 exp.X_op = O_secidx;
2365
2366 emit_expr (&exp, 2);
2367 }
2368 while (*input_line_pointer++ == ',');
2369
2370 input_line_pointer--;
2371 demand_empty_rest_of_line ();
2372}
528e4f46
MH
2373#endif /* TE_PE */
2374
a06ea964
NC
2375static void s_aarch64_arch (int);
2376static void s_aarch64_cpu (int);
ae527cd8 2377static void s_aarch64_arch_extension (int);
a06ea964
NC
2378
2379/* This table describes all the machine specific pseudo-ops the assembler
2380 has to support. The fields are:
2381 pseudo-op name without dot
2382 function to call to execute this pseudo-op
2383 Integer arg to pass to the function. */
2384
2385const pseudo_typeS md_pseudo_table[] = {
2386 /* Never called because '.req' does not start a line. */
2387 {"req", s_req, 0},
2388 {"unreq", s_unreq, 0},
a06ea964
NC
2389 {"even", s_even, 0},
2390 {"ltorg", s_ltorg, 0},
2391 {"pool", s_ltorg, 0},
2392 {"cpu", s_aarch64_cpu, 0},
2393 {"arch", s_aarch64_arch, 0},
ae527cd8 2394 {"arch_extension", s_aarch64_arch_extension, 0},
a06ea964 2395 {"inst", s_aarch64_inst, 0},
3a67e1a6 2396 {"cfi_b_key_frame", s_aarch64_cfi_b_key_frame, 0},
a06ea964 2397#ifdef OBJ_ELF
43a357f9 2398 {"tlsdescadd", s_tlsdescadd, 0},
a06ea964 2399 {"tlsdesccall", s_tlsdesccall, 0},
43a357f9 2400 {"tlsdescldr", s_tlsdescldr, 0},
f166ae01 2401 {"variant_pcs", s_variant_pcs, 0},
b152649d
MH
2402#endif
2403#if defined(OBJ_ELF) || defined(OBJ_COFF)
2404 {"word", s_aarch64_cons, 4},
2405 {"long", s_aarch64_cons, 4},
2406 {"xword", s_aarch64_cons, 8},
2407 {"dword", s_aarch64_cons, 8},
528e4f46
MH
2408#endif
2409#ifdef TE_PE
2410 {"secrel32", s_secrel, 0},
6f4a117f 2411 {"secidx", s_secidx, 0},
a06ea964 2412#endif
b20d3859 2413 {"float16", float_cons, 'h'},
eb5bbc48 2414 {"bfloat16", float_cons, 'b'},
a06ea964
NC
2415 {0, 0, 0}
2416};
2417\f
2418
2419/* Check whether STR points to a register name followed by a comma or the
2420 end of line; REG_TYPE indicates which register types are checked
2421 against. Return TRUE if STR is such a register name; otherwise return
2422 FALSE. The function does not intend to produce any diagnostics, but since
2423 the register parser aarch64_reg_parse, which is called by this function,
2424 does produce diagnostics, we call clear_error to clear any diagnostics
2425 that may be generated by aarch64_reg_parse.
2426 Also, the function returns FALSE directly if there is any user error
2427 present at the function entry. This prevents the existing diagnostics
2428 state from being spoiled.
2429 The function currently serves parse_constant_immediate and
2430 parse_big_immediate only. */
5b7c81bd 2431static bool
a06ea964
NC
2432reg_name_p (char *str, aarch64_reg_type reg_type)
2433{
074c89d2 2434 const reg_entry *reg;
a06ea964
NC
2435
2436 /* Prevent the diagnostics state from being spoiled. */
2437 if (error_p ())
5b7c81bd 2438 return false;
a06ea964 2439
074c89d2 2440 reg = aarch64_reg_parse (&str, reg_type, NULL);
a06ea964
NC
2441
2442 /* Clear the parsing error that may be set by the reg parser. */
2443 clear_error ();
2444
074c89d2 2445 if (!reg)
5b7c81bd 2446 return false;
a06ea964
NC
2447
2448 skip_whitespace (str);
f405494f 2449 if (*str == ',' || is_end_of_line[(unsigned char) *str])
5b7c81bd 2450 return true;
a06ea964 2451
5b7c81bd 2452 return false;
a06ea964
NC
2453}
2454
2455/* Parser functions used exclusively in instruction operands. */
2456
2457/* Parse an immediate expression which may not be constant.
2458
2459 To prevent the expression parser from pushing a register name
2460 into the symbol table as an undefined symbol, firstly a check is
1799c0d0
RS
2461 done to find out whether STR is a register of type REG_TYPE followed
2462 by a comma or the end of line. Return FALSE if STR is such a string. */
a06ea964 2463
5b7c81bd 2464static bool
1799c0d0
RS
2465parse_immediate_expression (char **str, expressionS *exp,
2466 aarch64_reg_type reg_type)
a06ea964 2467{
1799c0d0 2468 if (reg_name_p (*str, reg_type))
a06ea964
NC
2469 {
2470 set_recoverable_error (_("immediate operand required"));
5b7c81bd 2471 return false;
a06ea964
NC
2472 }
2473
c1723a81 2474 aarch64_get_expression (exp, str, GE_OPT_PREFIX, REJECT_ABSENT);
a06ea964
NC
2475
2476 if (exp->X_op == O_absent)
2477 {
2478 set_fatal_syntax_error (_("missing immediate expression"));
5b7c81bd 2479 return false;
a06ea964
NC
2480 }
2481
5b7c81bd 2482 return true;
a06ea964
NC
2483}
2484
2485/* Constant immediate-value read function for use in insn parsing.
2486 STR points to the beginning of the immediate (with the optional
1799c0d0
RS
2487 leading #); *VAL receives the value. REG_TYPE says which register
2488 names should be treated as registers rather than as symbolic immediates.
a06ea964
NC
2489
2490 Return TRUE on success; otherwise return FALSE. */
2491
5b7c81bd 2492static bool
1799c0d0 2493parse_constant_immediate (char **str, int64_t *val, aarch64_reg_type reg_type)
a06ea964
NC
2494{
2495 expressionS exp;
2496
1799c0d0 2497 if (! parse_immediate_expression (str, &exp, reg_type))
5b7c81bd 2498 return false;
a06ea964
NC
2499
2500 if (exp.X_op != O_constant)
2501 {
2502 set_syntax_error (_("constant expression required"));
5b7c81bd 2503 return false;
a06ea964
NC
2504 }
2505
2506 *val = exp.X_add_number;
5b7c81bd 2507 return true;
a06ea964
NC
2508}
2509
2510static uint32_t
2511encode_imm_float_bits (uint32_t imm)
2512{
2513 return ((imm >> 19) & 0x7f) /* b[25:19] -> b[6:0] */
2514 | ((imm >> (31 - 7)) & 0x80); /* b[31] -> b[7] */
2515}
2516
62b0d0d5
YZ
2517/* Return TRUE if the single-precision floating-point value encoded in IMM
2518 can be expressed in the AArch64 8-bit signed floating-point format with
2519 3-bit exponent and normalized 4 bits of precision; in other words, the
2520 floating-point value must be expressable as
2521 (+/-) n / 16 * power (2, r)
2522 where n and r are integers such that 16 <= n <=31 and -3 <= r <= 4. */
2523
5b7c81bd 2524static bool
a06ea964
NC
2525aarch64_imm_float_p (uint32_t imm)
2526{
62b0d0d5
YZ
2527 /* If a single-precision floating-point value has the following bit
2528 pattern, it can be expressed in the AArch64 8-bit floating-point
2529 format:
2530
2531 3 32222222 2221111111111
a06ea964 2532 1 09876543 21098765432109876543210
62b0d0d5
YZ
2533 n Eeeeeexx xxxx0000000000000000000
2534
2535 where n, e and each x are either 0 or 1 independently, with
2536 E == ~ e. */
a06ea964 2537
62b0d0d5
YZ
2538 uint32_t pattern;
2539
2540 /* Prepare the pattern for 'Eeeeee'. */
2541 if (((imm >> 30) & 0x1) == 0)
2542 pattern = 0x3e000000;
a06ea964 2543 else
62b0d0d5
YZ
2544 pattern = 0x40000000;
2545
2546 return (imm & 0x7ffff) == 0 /* lower 19 bits are 0. */
2547 && ((imm & 0x7e000000) == pattern); /* bits 25 - 29 == ~ bit 30. */
a06ea964
NC
2548}
2549
04a3379a
RS
2550/* Return TRUE if the IEEE double value encoded in IMM can be expressed
2551 as an IEEE float without any loss of precision. Store the value in
2552 *FPWORD if so. */
62b0d0d5 2553
5b7c81bd 2554static bool
04a3379a 2555can_convert_double_to_float (uint64_t imm, uint32_t *fpword)
62b0d0d5
YZ
2556{
2557 /* If a double-precision floating-point value has the following bit
04a3379a 2558 pattern, it can be expressed in a float:
62b0d0d5 2559
04a3379a
RS
2560 6 66655555555 5544 44444444 33333333 33222222 22221111 111111
2561 3 21098765432 1098 76543210 98765432 10987654 32109876 54321098 76543210
2562 n E~~~eeeeeee ssss ssssssss ssssssss SSS00000 00000000 00000000 00000000
62b0d0d5 2563
04a3379a
RS
2564 -----------------------------> nEeeeeee esssssss ssssssss sssssSSS
2565 if Eeee_eeee != 1111_1111
2566
2567 where n, e, s and S are either 0 or 1 independently and where ~ is the
2568 inverse of E. */
62b0d0d5
YZ
2569
2570 uint32_t pattern;
2571 uint32_t high32 = imm >> 32;
04a3379a 2572 uint32_t low32 = imm;
62b0d0d5 2573
04a3379a
RS
2574 /* Lower 29 bits need to be 0s. */
2575 if ((imm & 0x1fffffff) != 0)
5b7c81bd 2576 return false;
62b0d0d5
YZ
2577
2578 /* Prepare the pattern for 'Eeeeeeeee'. */
2579 if (((high32 >> 30) & 0x1) == 0)
04a3379a 2580 pattern = 0x38000000;
62b0d0d5
YZ
2581 else
2582 pattern = 0x40000000;
2583
04a3379a
RS
2584 /* Check E~~~. */
2585 if ((high32 & 0x78000000) != pattern)
5b7c81bd 2586 return false;
04a3379a
RS
2587
2588 /* Check Eeee_eeee != 1111_1111. */
2589 if ((high32 & 0x7ff00000) == 0x47f00000)
5b7c81bd 2590 return false;
04a3379a
RS
2591
2592 *fpword = ((high32 & 0xc0000000) /* 1 n bit and 1 E bit. */
2593 | ((high32 << 3) & 0x3ffffff8) /* 7 e and 20 s bits. */
2594 | (low32 >> 29)); /* 3 S bits. */
5b7c81bd 2595 return true;
62b0d0d5
YZ
2596}
2597
165d4950
RS
2598/* Return true if we should treat OPERAND as a double-precision
2599 floating-point operand rather than a single-precision one. */
5b7c81bd 2600static bool
165d4950
RS
2601double_precision_operand_p (const aarch64_opnd_info *operand)
2602{
2603 /* Check for unsuffixed SVE registers, which are allowed
2604 for LDR and STR but not in instructions that require an
2605 immediate. We get better error messages if we arbitrarily
2606 pick one size, parse the immediate normally, and then
2607 report the match failure in the normal way. */
2608 return (operand->qualifier == AARCH64_OPND_QLF_NIL
2609 || aarch64_get_qualifier_esize (operand->qualifier) == 8);
2610}
2611
62b0d0d5
YZ
2612/* Parse a floating-point immediate. Return TRUE on success and return the
2613 value in *IMMED in the format of IEEE754 single-precision encoding.
2614 *CCP points to the start of the string; DP_P is TRUE when the immediate
2615 is expected to be in double-precision (N.B. this only matters when
1799c0d0
RS
2616 hexadecimal representation is involved). REG_TYPE says which register
2617 names should be treated as registers rather than as symbolic immediates.
62b0d0d5 2618
874d7e6e
RS
2619 This routine accepts any IEEE float; it is up to the callers to reject
2620 invalid ones. */
62b0d0d5 2621
5b7c81bd
AM
2622static bool
2623parse_aarch64_imm_float (char **ccp, int *immed, bool dp_p,
1799c0d0 2624 aarch64_reg_type reg_type)
a06ea964
NC
2625{
2626 char *str = *ccp;
2627 char *fpnum;
2628 LITTLENUM_TYPE words[MAX_LITTLENUMS];
62b0d0d5
YZ
2629 int64_t val = 0;
2630 unsigned fpword = 0;
5b7c81bd 2631 bool hex_p = false;
a06ea964
NC
2632
2633 skip_past_char (&str, '#');
2634
a06ea964
NC
2635 fpnum = str;
2636 skip_whitespace (fpnum);
2637
d34049e8 2638 if (startswith (fpnum, "0x"))
62b0d0d5
YZ
2639 {
2640 /* Support the hexadecimal representation of the IEEE754 encoding.
2641 Double-precision is expected when DP_P is TRUE, otherwise the
2642 representation should be in single-precision. */
1799c0d0 2643 if (! parse_constant_immediate (&str, &val, reg_type))
62b0d0d5
YZ
2644 goto invalid_fp;
2645
2646 if (dp_p)
2647 {
04a3379a 2648 if (!can_convert_double_to_float (val, &fpword))
62b0d0d5
YZ
2649 goto invalid_fp;
2650 }
2651 else if ((uint64_t) val > 0xffffffff)
2652 goto invalid_fp;
2653 else
2654 fpword = val;
2655
5b7c81bd 2656 hex_p = true;
62b0d0d5 2657 }
66881839
TC
2658 else if (reg_name_p (str, reg_type))
2659 {
2660 set_recoverable_error (_("immediate operand required"));
5b7c81bd 2661 return false;
a06ea964
NC
2662 }
2663
62b0d0d5 2664 if (! hex_p)
a06ea964 2665 {
a06ea964
NC
2666 int i;
2667
62b0d0d5
YZ
2668 if ((str = atof_ieee (str, 's', words)) == NULL)
2669 goto invalid_fp;
2670
a06ea964
NC
2671 /* Our FP word must be 32 bits (single-precision FP). */
2672 for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
2673 {
2674 fpword <<= LITTLENUM_NUMBER_OF_BITS;
2675 fpword |= words[i];
2676 }
62b0d0d5 2677 }
a06ea964 2678
874d7e6e
RS
2679 *immed = fpword;
2680 *ccp = str;
5b7c81bd 2681 return true;
a06ea964 2682
dc1e8a47 2683 invalid_fp:
a06ea964 2684 set_fatal_syntax_error (_("invalid floating-point constant"));
5b7c81bd 2685 return false;
a06ea964
NC
2686}
2687
2688/* Less-generic immediate-value read function with the possibility of loading
2689 a big (64-bit) immediate, as required by AdvSIMD Modified immediate
2690 instructions.
2691
2692 To prevent the expression parser from pushing a register name into the
2693 symbol table as an undefined symbol, a check is firstly done to find
1799c0d0
RS
2694 out whether STR is a register of type REG_TYPE followed by a comma or
2695 the end of line. Return FALSE if STR is such a register. */
a06ea964 2696
5b7c81bd 2697static bool
1799c0d0 2698parse_big_immediate (char **str, int64_t *imm, aarch64_reg_type reg_type)
a06ea964
NC
2699{
2700 char *ptr = *str;
2701
1799c0d0 2702 if (reg_name_p (ptr, reg_type))
a06ea964
NC
2703 {
2704 set_syntax_error (_("immediate operand required"));
5b7c81bd 2705 return false;
a06ea964
NC
2706 }
2707
c1723a81 2708 aarch64_get_expression (&inst.reloc.exp, &ptr, GE_OPT_PREFIX, REJECT_ABSENT);
a06ea964
NC
2709
2710 if (inst.reloc.exp.X_op == O_constant)
2711 *imm = inst.reloc.exp.X_add_number;
2712
2713 *str = ptr;
2714
5b7c81bd 2715 return true;
a06ea964
NC
2716}
2717
2718/* Set operand IDX of the *INSTR that needs a GAS internal fixup.
2719 if NEED_LIBOPCODES is non-zero, the fixup will need
2720 assistance from the libopcodes. */
2721
2722static inline void
2723aarch64_set_gas_internal_fixup (struct reloc *reloc,
2724 const aarch64_opnd_info *operand,
2725 int need_libopcodes_p)
2726{
2727 reloc->type = BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP;
2728 reloc->opnd = operand->type;
2729 if (need_libopcodes_p)
2730 reloc->need_libopcodes_p = 1;
2731};
2732
2733/* Return TRUE if the instruction needs to be fixed up later internally by
2734 the GAS; otherwise return FALSE. */
2735
5b7c81bd 2736static inline bool
a06ea964
NC
2737aarch64_gas_internal_fixup_p (void)
2738{
2739 return inst.reloc.type == BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP;
2740}
2741
33eaf5de 2742/* Assign the immediate value to the relevant field in *OPERAND if
a06ea964
NC
2743 RELOC->EXP is a constant expression; otherwise, flag that *OPERAND
2744 needs an internal fixup in a later stage.
2745 ADDR_OFF_P determines whether it is the field ADDR.OFFSET.IMM or
2746 IMM.VALUE that may get assigned with the constant. */
2747static inline void
2748assign_imm_if_const_or_fixup_later (struct reloc *reloc,
2749 aarch64_opnd_info *operand,
2750 int addr_off_p,
2751 int need_libopcodes_p,
2752 int skip_p)
2753{
2754 if (reloc->exp.X_op == O_constant)
2755 {
2756 if (addr_off_p)
2757 operand->addr.offset.imm = reloc->exp.X_add_number;
2758 else
2759 operand->imm.value = reloc->exp.X_add_number;
2760 reloc->type = BFD_RELOC_UNUSED;
2761 }
2762 else
2763 {
2764 aarch64_set_gas_internal_fixup (reloc, operand, need_libopcodes_p);
2765 /* Tell libopcodes to ignore this operand or not. This is helpful
2766 when one of the operands needs to be fixed up later but we need
2767 libopcodes to check the other operands. */
2768 operand->skip = skip_p;
2769 }
2770}
2771
2772/* Relocation modifiers. Each entry in the table contains the textual
2773 name for the relocation which may be placed before a symbol used as
2774 a load/store offset, or add immediate. It must be surrounded by a
2775 leading and trailing colon, for example:
2776
2777 ldr x0, [x1, #:rello:varsym]
2778 add x0, x1, #:rello:varsym */
2779
2780struct reloc_table_entry
2781{
2782 const char *name;
2783 int pc_rel;
6f4a313b 2784 bfd_reloc_code_real_type adr_type;
a06ea964
NC
2785 bfd_reloc_code_real_type adrp_type;
2786 bfd_reloc_code_real_type movw_type;
2787 bfd_reloc_code_real_type add_type;
2788 bfd_reloc_code_real_type ldst_type;
74ad790c 2789 bfd_reloc_code_real_type ld_literal_type;
a06ea964
NC
2790};
2791
eac4eb8e
NC
2792static struct reloc_table_entry reloc_table[] =
2793{
a06ea964
NC
2794 /* Low 12 bits of absolute address: ADD/i and LDR/STR */
2795 {"lo12", 0,
6f4a313b 2796 0, /* adr_type */
a06ea964
NC
2797 0,
2798 0,
2799 BFD_RELOC_AARCH64_ADD_LO12,
74ad790c
MS
2800 BFD_RELOC_AARCH64_LDST_LO12,
2801 0},
a06ea964
NC
2802
2803 /* Higher 21 bits of pc-relative page offset: ADRP */
2804 {"pg_hi21", 1,
6f4a313b 2805 0, /* adr_type */
a06ea964
NC
2806 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
2807 0,
2808 0,
74ad790c 2809 0,
a06ea964
NC
2810 0},
2811
2812 /* Higher 21 bits of pc-relative page offset: ADRP, no check */
2813 {"pg_hi21_nc", 1,
6f4a313b 2814 0, /* adr_type */
a06ea964
NC
2815 BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL,
2816 0,
2817 0,
74ad790c 2818 0,
a06ea964
NC
2819 0},
2820
2821 /* Most significant bits 0-15 of unsigned address/value: MOVZ */
2822 {"abs_g0", 0,
6f4a313b 2823 0, /* adr_type */
a06ea964
NC
2824 0,
2825 BFD_RELOC_AARCH64_MOVW_G0,
2826 0,
74ad790c 2827 0,
a06ea964
NC
2828 0},
2829
2830 /* Most significant bits 0-15 of signed address/value: MOVN/Z */
2831 {"abs_g0_s", 0,
6f4a313b 2832 0, /* adr_type */
a06ea964
NC
2833 0,
2834 BFD_RELOC_AARCH64_MOVW_G0_S,
2835 0,
74ad790c 2836 0,
a06ea964
NC
2837 0},
2838
2839 /* Less significant bits 0-15 of address/value: MOVK, no check */
2840 {"abs_g0_nc", 0,
6f4a313b 2841 0, /* adr_type */
a06ea964
NC
2842 0,
2843 BFD_RELOC_AARCH64_MOVW_G0_NC,
2844 0,
74ad790c 2845 0,
a06ea964
NC
2846 0},
2847
2848 /* Most significant bits 16-31 of unsigned address/value: MOVZ */
2849 {"abs_g1", 0,
6f4a313b 2850 0, /* adr_type */
a06ea964
NC
2851 0,
2852 BFD_RELOC_AARCH64_MOVW_G1,
2853 0,
74ad790c 2854 0,
a06ea964
NC
2855 0},
2856
2857 /* Most significant bits 16-31 of signed address/value: MOVN/Z */
2858 {"abs_g1_s", 0,
6f4a313b 2859 0, /* adr_type */
a06ea964
NC
2860 0,
2861 BFD_RELOC_AARCH64_MOVW_G1_S,
2862 0,
74ad790c 2863 0,
a06ea964
NC
2864 0},
2865
2866 /* Less significant bits 16-31 of address/value: MOVK, no check */
2867 {"abs_g1_nc", 0,
6f4a313b 2868 0, /* adr_type */
a06ea964
NC
2869 0,
2870 BFD_RELOC_AARCH64_MOVW_G1_NC,
2871 0,
74ad790c 2872 0,
a06ea964
NC
2873 0},
2874
2875 /* Most significant bits 32-47 of unsigned address/value: MOVZ */
2876 {"abs_g2", 0,
6f4a313b 2877 0, /* adr_type */
a06ea964
NC
2878 0,
2879 BFD_RELOC_AARCH64_MOVW_G2,
2880 0,
74ad790c 2881 0,
a06ea964
NC
2882 0},
2883
2884 /* Most significant bits 32-47 of signed address/value: MOVN/Z */
2885 {"abs_g2_s", 0,
6f4a313b 2886 0, /* adr_type */
a06ea964
NC
2887 0,
2888 BFD_RELOC_AARCH64_MOVW_G2_S,
2889 0,
74ad790c 2890 0,
a06ea964
NC
2891 0},
2892
2893 /* Less significant bits 32-47 of address/value: MOVK, no check */
2894 {"abs_g2_nc", 0,
6f4a313b 2895 0, /* adr_type */
a06ea964
NC
2896 0,
2897 BFD_RELOC_AARCH64_MOVW_G2_NC,
2898 0,
74ad790c 2899 0,
a06ea964
NC
2900 0},
2901
2902 /* Most significant bits 48-63 of signed/unsigned address/value: MOVZ */
2903 {"abs_g3", 0,
6f4a313b 2904 0, /* adr_type */
a06ea964
NC
2905 0,
2906 BFD_RELOC_AARCH64_MOVW_G3,
2907 0,
74ad790c 2908 0,
a06ea964 2909 0},
4aa2c5e2 2910
32247401
RL
2911 /* Most significant bits 0-15 of signed/unsigned address/value: MOVZ */
2912 {"prel_g0", 1,
2913 0, /* adr_type */
2914 0,
2915 BFD_RELOC_AARCH64_MOVW_PREL_G0,
2916 0,
2917 0,
2918 0},
2919
2920 /* Most significant bits 0-15 of signed/unsigned address/value: MOVK */
2921 {"prel_g0_nc", 1,
2922 0, /* adr_type */
2923 0,
2924 BFD_RELOC_AARCH64_MOVW_PREL_G0_NC,
2925 0,
2926 0,
2927 0},
2928
2929 /* Most significant bits 16-31 of signed/unsigned address/value: MOVZ */
2930 {"prel_g1", 1,
2931 0, /* adr_type */
2932 0,
2933 BFD_RELOC_AARCH64_MOVW_PREL_G1,
2934 0,
2935 0,
2936 0},
2937
2938 /* Most significant bits 16-31 of signed/unsigned address/value: MOVK */
2939 {"prel_g1_nc", 1,
2940 0, /* adr_type */
2941 0,
2942 BFD_RELOC_AARCH64_MOVW_PREL_G1_NC,
2943 0,
2944 0,
2945 0},
2946
2947 /* Most significant bits 32-47 of signed/unsigned address/value: MOVZ */
2948 {"prel_g2", 1,
2949 0, /* adr_type */
2950 0,
2951 BFD_RELOC_AARCH64_MOVW_PREL_G2,
2952 0,
2953 0,
2954 0},
2955
2956 /* Most significant bits 32-47 of signed/unsigned address/value: MOVK */
2957 {"prel_g2_nc", 1,
2958 0, /* adr_type */
2959 0,
2960 BFD_RELOC_AARCH64_MOVW_PREL_G2_NC,
2961 0,
2962 0,
2963 0},
2964
2965 /* Most significant bits 48-63 of signed/unsigned address/value: MOVZ */
2966 {"prel_g3", 1,
2967 0, /* adr_type */
2968 0,
2969 BFD_RELOC_AARCH64_MOVW_PREL_G3,
2970 0,
2971 0,
2972 0},
2973
a06ea964
NC
2974 /* Get to the page containing GOT entry for a symbol. */
2975 {"got", 1,
6f4a313b 2976 0, /* adr_type */
a06ea964
NC
2977 BFD_RELOC_AARCH64_ADR_GOT_PAGE,
2978 0,
2979 0,
74ad790c 2980 0,
4aa2c5e2
MS
2981 BFD_RELOC_AARCH64_GOT_LD_PREL19},
2982
a06ea964
NC
2983 /* 12 bit offset into the page containing GOT entry for that symbol. */
2984 {"got_lo12", 0,
6f4a313b 2985 0, /* adr_type */
a06ea964
NC
2986 0,
2987 0,
2988 0,
74ad790c
MS
2989 BFD_RELOC_AARCH64_LD_GOT_LO12_NC,
2990 0},
a06ea964 2991
ca632371
RL
2992 /* 0-15 bits of address/value: MOVk, no check. */
2993 {"gotoff_g0_nc", 0,
2994 0, /* adr_type */
2995 0,
2996 BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC,
2997 0,
2998 0,
2999 0},
3000
654248e7
RL
3001 /* Most significant bits 16-31 of address/value: MOVZ. */
3002 {"gotoff_g1", 0,
3003 0, /* adr_type */
3004 0,
3005 BFD_RELOC_AARCH64_MOVW_GOTOFF_G1,
3006 0,
3007 0,
3008 0},
3009
87f5fbcc
RL
3010 /* 15 bit offset into the page containing GOT entry for that symbol. */
3011 {"gotoff_lo15", 0,
3012 0, /* adr_type */
3013 0,
3014 0,
3015 0,
3016 BFD_RELOC_AARCH64_LD64_GOTOFF_LO15,
3017 0},
3018
3b957e5b
RL
3019 /* Get to the page containing GOT TLS entry for a symbol */
3020 {"gottprel_g0_nc", 0,
3021 0, /* adr_type */
3022 0,
3023 BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC,
3024 0,
3025 0,
3026 0},
3027
3028 /* Get to the page containing GOT TLS entry for a symbol */
3029 {"gottprel_g1", 0,
3030 0, /* adr_type */
3031 0,
3032 BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1,
3033 0,
3034 0,
3035 0},
3036
a06ea964
NC
3037 /* Get to the page containing GOT TLS entry for a symbol */
3038 {"tlsgd", 0,
3c12b054 3039 BFD_RELOC_AARCH64_TLSGD_ADR_PREL21, /* adr_type */
a06ea964
NC
3040 BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21,
3041 0,
3042 0,
74ad790c 3043 0,
a06ea964
NC
3044 0},
3045
3046 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
3047 {"tlsgd_lo12", 0,
6f4a313b 3048 0, /* adr_type */
a06ea964
NC
3049 0,
3050 0,
3051 BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC,
74ad790c 3052 0,
a06ea964
NC
3053 0},
3054
3e8286c0
RL
3055 /* Lower 16 bits address/value: MOVk. */
3056 {"tlsgd_g0_nc", 0,
3057 0, /* adr_type */
3058 0,
3059 BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC,
3060 0,
3061 0,
3062 0},
3063
1aa66fb1
RL
3064 /* Most significant bits 16-31 of address/value: MOVZ. */
3065 {"tlsgd_g1", 0,
3066 0, /* adr_type */
3067 0,
3068 BFD_RELOC_AARCH64_TLSGD_MOVW_G1,
3069 0,
3070 0,
3071 0},
3072
a06ea964
NC
3073 /* Get to the page containing GOT TLS entry for a symbol */
3074 {"tlsdesc", 0,
389b8029 3075 BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21, /* adr_type */
418009c2 3076 BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21,
a06ea964
NC
3077 0,
3078 0,
74ad790c 3079 0,
1ada945d 3080 BFD_RELOC_AARCH64_TLSDESC_LD_PREL19},
a06ea964
NC
3081
3082 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
3083 {"tlsdesc_lo12", 0,
6f4a313b 3084 0, /* adr_type */
a06ea964
NC
3085 0,
3086 0,
f955cccf 3087 BFD_RELOC_AARCH64_TLSDESC_ADD_LO12,
74ad790c
MS
3088 BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC,
3089 0},
a06ea964 3090
6c37fedc
JW
3091 /* Get to the page containing GOT TLS entry for a symbol.
3092 The same as GD, we allocate two consecutive GOT slots
3093 for module index and module offset, the only difference
33eaf5de 3094 with GD is the module offset should be initialized to
6c37fedc
JW
3095 zero without any outstanding runtime relocation. */
3096 {"tlsldm", 0,
3097 BFD_RELOC_AARCH64_TLSLD_ADR_PREL21, /* adr_type */
1107e076 3098 BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21,
6c37fedc
JW
3099 0,
3100 0,
3101 0,
3102 0},
3103
a12fad50
JW
3104 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
3105 {"tlsldm_lo12_nc", 0,
3106 0, /* adr_type */
3107 0,
3108 0,
3109 BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC,
3110 0,
3111 0},
3112
70151fb5
JW
3113 /* 12 bit offset into the module TLS base address. */
3114 {"dtprel_lo12", 0,
3115 0, /* adr_type */
3116 0,
3117 0,
3118 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12,
4c562523 3119 BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12,
70151fb5
JW
3120 0},
3121
13289c10
JW
3122 /* Same as dtprel_lo12, no overflow check. */
3123 {"dtprel_lo12_nc", 0,
3124 0, /* adr_type */
3125 0,
3126 0,
3127 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC,
4c562523 3128 BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC,
13289c10
JW
3129 0},
3130
49df5539
JW
3131 /* bits[23:12] of offset to the module TLS base address. */
3132 {"dtprel_hi12", 0,
3133 0, /* adr_type */
3134 0,
3135 0,
3136 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12,
3137 0,
3138 0},
3139
3140 /* bits[15:0] of offset to the module TLS base address. */
3141 {"dtprel_g0", 0,
3142 0, /* adr_type */
3143 0,
3144 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0,
3145 0,
3146 0,
3147 0},
3148
3149 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0. */
3150 {"dtprel_g0_nc", 0,
3151 0, /* adr_type */
3152 0,
3153 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC,
3154 0,
3155 0,
3156 0},
3157
3158 /* bits[31:16] of offset to the module TLS base address. */
3159 {"dtprel_g1", 0,
3160 0, /* adr_type */
3161 0,
3162 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1,
3163 0,
3164 0,
3165 0},
3166
3167 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1. */
3168 {"dtprel_g1_nc", 0,
3169 0, /* adr_type */
3170 0,
3171 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC,
3172 0,
3173 0,
3174 0},
3175
3176 /* bits[47:32] of offset to the module TLS base address. */
3177 {"dtprel_g2", 0,
3178 0, /* adr_type */
3179 0,
3180 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2,
3181 0,
3182 0,
3183 0},
3184
43a357f9
RL
3185 /* Lower 16 bit offset into GOT entry for a symbol */
3186 {"tlsdesc_off_g0_nc", 0,
3187 0, /* adr_type */
3188 0,
3189 BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC,
3190 0,
3191 0,
3192 0},
3193
3194 /* Higher 16 bit offset into GOT entry for a symbol */
3195 {"tlsdesc_off_g1", 0,
3196 0, /* adr_type */
3197 0,
3198 BFD_RELOC_AARCH64_TLSDESC_OFF_G1,
3199 0,
3200 0,
3201 0},
3202
a06ea964
NC
3203 /* Get to the page containing GOT TLS entry for a symbol */
3204 {"gottprel", 0,
6f4a313b 3205 0, /* adr_type */
a06ea964
NC
3206 BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,
3207 0,
3208 0,
74ad790c 3209 0,
043bf05a 3210 BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19},
a06ea964
NC
3211
3212 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
3213 {"gottprel_lo12", 0,
6f4a313b 3214 0, /* adr_type */
a06ea964
NC
3215 0,
3216 0,
3217 0,
74ad790c
MS
3218 BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC,
3219 0},
a06ea964
NC
3220
3221 /* Get tp offset for a symbol. */
3222 {"tprel", 0,
6f4a313b 3223 0, /* adr_type */
a06ea964
NC
3224 0,
3225 0,
3226 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12,
74ad790c 3227 0,
a06ea964
NC
3228 0},
3229
3230 /* Get tp offset for a symbol. */
3231 {"tprel_lo12", 0,
6f4a313b 3232 0, /* adr_type */
a06ea964
NC
3233 0,
3234 0,
3235 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12,
84f1b9fb 3236 BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12,
a06ea964
NC
3237 0},
3238
3239 /* Get tp offset for a symbol. */
3240 {"tprel_hi12", 0,
6f4a313b 3241 0, /* adr_type */
a06ea964
NC
3242 0,
3243 0,
3244 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12,
74ad790c 3245 0,
a06ea964
NC
3246 0},
3247
3248 /* Get tp offset for a symbol. */
3249 {"tprel_lo12_nc", 0,
6f4a313b 3250 0, /* adr_type */
a06ea964
NC
3251 0,
3252 0,
3253 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC,
84f1b9fb 3254 BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12_NC,
a06ea964
NC
3255 0},
3256
3257 /* Most significant bits 32-47 of address/value: MOVZ. */
3258 {"tprel_g2", 0,
6f4a313b 3259 0, /* adr_type */
a06ea964
NC
3260 0,
3261 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2,
3262 0,
74ad790c 3263 0,
a06ea964
NC
3264 0},
3265
3266 /* Most significant bits 16-31 of address/value: MOVZ. */
3267 {"tprel_g1", 0,
6f4a313b 3268 0, /* adr_type */
a06ea964
NC
3269 0,
3270 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1,
3271 0,
74ad790c 3272 0,
a06ea964
NC
3273 0},
3274
3275 /* Most significant bits 16-31 of address/value: MOVZ, no check. */
3276 {"tprel_g1_nc", 0,
6f4a313b 3277 0, /* adr_type */
a06ea964
NC
3278 0,
3279 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC,
3280 0,
74ad790c 3281 0,
a06ea964
NC
3282 0},
3283
3284 /* Most significant bits 0-15 of address/value: MOVZ. */
3285 {"tprel_g0", 0,
6f4a313b 3286 0, /* adr_type */
a06ea964
NC
3287 0,
3288 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0,
3289 0,
74ad790c 3290 0,
a06ea964
NC
3291 0},
3292
3293 /* Most significant bits 0-15 of address/value: MOVZ, no check. */
3294 {"tprel_g0_nc", 0,
6f4a313b 3295 0, /* adr_type */
a06ea964
NC
3296 0,
3297 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC,
3298 0,
74ad790c 3299 0,
a06ea964 3300 0},
a921b5bd
JW
3301
3302 /* 15bit offset from got entry to base address of GOT table. */
3303 {"gotpage_lo15", 0,
3304 0,
3305 0,
3306 0,
3307 0,
3308 BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15,
3309 0},
3d715ce4
JW
3310
3311 /* 14bit offset from got entry to base address of GOT table. */
3312 {"gotpage_lo14", 0,
3313 0,
3314 0,
3315 0,
3316 0,
3317 BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14,
3318 0},
a06ea964
NC
3319};
3320
3321/* Given the address of a pointer pointing to the textual name of a
3322 relocation as may appear in assembler source, attempt to find its
3323 details in reloc_table. The pointer will be updated to the character
3324 after the trailing colon. On failure, NULL will be returned;
3325 otherwise return the reloc_table_entry. */
3326
3327static struct reloc_table_entry *
3328find_reloc_table_entry (char **str)
3329{
3330 unsigned int i;
3331 for (i = 0; i < ARRAY_SIZE (reloc_table); i++)
3332 {
3333 int length = strlen (reloc_table[i].name);
3334
3335 if (strncasecmp (reloc_table[i].name, *str, length) == 0
3336 && (*str)[length] == ':')
3337 {
3338 *str += (length + 1);
3339 return &reloc_table[i];
3340 }
3341 }
3342
3343 return NULL;
3344}
3345
eac4eb8e
NC
3346/* Returns 0 if the relocation should never be forced,
3347 1 if the relocation must be forced, and -1 if either
3348 result is OK. */
3349
3350static signed int
3351aarch64_force_reloc (unsigned int type)
3352{
3353 switch (type)
3354 {
3355 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
3356 /* Perform these "immediate" internal relocations
3357 even if the symbol is extern or weak. */
3358 return 0;
3359
3360 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
3361 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
3362 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
3363 /* Pseudo relocs that need to be fixed up according to
3364 ilp32_p. */
15b7af6c 3365 return 1;
eac4eb8e
NC
3366
3367 case BFD_RELOC_AARCH64_ADD_LO12:
3368 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
3369 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
3370 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
3371 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
3372 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3373 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
3374 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
3375 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
3376 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
3377 case BFD_RELOC_AARCH64_LDST128_LO12:
3378 case BFD_RELOC_AARCH64_LDST16_LO12:
3379 case BFD_RELOC_AARCH64_LDST32_LO12:
3380 case BFD_RELOC_AARCH64_LDST64_LO12:
3381 case BFD_RELOC_AARCH64_LDST8_LO12:
04dfe7aa 3382 case BFD_RELOC_AARCH64_LDST_LO12:
eac4eb8e
NC
3383 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
3384 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
3385 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
3386 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
3387 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
3388 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
3389 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
3390 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
3391 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
3392 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3393 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
3394 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
3395 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
3396 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
3397 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
3398 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
3399 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3400 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
3401 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
3402 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
3403 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
3404 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
3405 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
3406 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
3407 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
3408 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
3409 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
3410 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
3411 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
3412 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
3413 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
3414 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
3415 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
04dfe7aa
JB
3416 case BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12:
3417 case BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC:
eac4eb8e
NC
3418 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
3419 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
3420 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
3421 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
3422 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
3423 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
3424 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
3425 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
3426 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
3427 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
3428 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
3429 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
3430 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
04dfe7aa
JB
3431 case BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12:
3432 case BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12_NC:
eac4eb8e
NC
3433 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
3434 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
3435 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
3436 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
3437 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
3438 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
3439 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
3440 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
3441 /* Always leave these relocations for the linker. */
3442 return 1;
3443
3444 default:
3445 return -1;
3446 }
3447}
3448
3449int
3450aarch64_force_relocation (struct fix *fixp)
3451{
3452 int res = aarch64_force_reloc (fixp->fx_r_type);
3453
3454 if (res == -1)
3455 return generic_force_reloc (fixp);
3456 return res;
3457}
3458
a06ea964
NC
3459/* Mode argument to parse_shift and parser_shifter_operand. */
3460enum parse_shift_mode
3461{
98907a70 3462 SHIFTED_NONE, /* no shifter allowed */
a06ea964
NC
3463 SHIFTED_ARITH_IMM, /* "rn{,lsl|lsr|asl|asr|uxt|sxt #n}" or
3464 "#imm{,lsl #n}" */
3465 SHIFTED_LOGIC_IMM, /* "rn{,lsl|lsr|asl|asr|ror #n}" or
3466 "#imm" */
3467 SHIFTED_LSL, /* bare "lsl #n" */
2442d846 3468 SHIFTED_MUL, /* bare "mul #n" */
a06ea964 3469 SHIFTED_LSL_MSL, /* "lsl|msl #n" */
98907a70 3470 SHIFTED_MUL_VL, /* "mul vl" */
a06ea964
NC
3471 SHIFTED_REG_OFFSET /* [su]xtw|sxtx {#n} or lsl #n */
3472};
3473
3474/* Parse a <shift> operator on an AArch64 data processing instruction.
3475 Return TRUE on success; otherwise return FALSE. */
5b7c81bd 3476static bool
a06ea964
NC
3477parse_shift (char **str, aarch64_opnd_info *operand, enum parse_shift_mode mode)
3478{
3479 const struct aarch64_name_value_pair *shift_op;
3480 enum aarch64_modifier_kind kind;
3481 expressionS exp;
3482 int exp_has_prefix;
3483 char *s = *str;
3484 char *p = s;
3485
3486 for (p = *str; ISALPHA (*p); p++)
3487 ;
3488
3489 if (p == *str)
3490 {
3491 set_syntax_error (_("shift expression expected"));
5b7c81bd 3492 return false;
a06ea964
NC
3493 }
3494
629310ab 3495 shift_op = str_hash_find_n (aarch64_shift_hsh, *str, p - *str);
a06ea964
NC
3496
3497 if (shift_op == NULL)
3498 {
3499 set_syntax_error (_("shift operator expected"));
5b7c81bd 3500 return false;
a06ea964
NC
3501 }
3502
3503 kind = aarch64_get_operand_modifier (shift_op);
3504
3505 if (kind == AARCH64_MOD_MSL && mode != SHIFTED_LSL_MSL)
3506 {
3507 set_syntax_error (_("invalid use of 'MSL'"));
5b7c81bd 3508 return false;
a06ea964
NC
3509 }
3510
2442d846 3511 if (kind == AARCH64_MOD_MUL
98907a70
RS
3512 && mode != SHIFTED_MUL
3513 && mode != SHIFTED_MUL_VL)
2442d846
RS
3514 {
3515 set_syntax_error (_("invalid use of 'MUL'"));
5b7c81bd 3516 return false;
2442d846
RS
3517 }
3518
a06ea964
NC
3519 switch (mode)
3520 {
3521 case SHIFTED_LOGIC_IMM:
535b785f 3522 if (aarch64_extend_operator_p (kind))
a06ea964
NC
3523 {
3524 set_syntax_error (_("extending shift is not permitted"));
5b7c81bd 3525 return false;
a06ea964
NC
3526 }
3527 break;
3528
3529 case SHIFTED_ARITH_IMM:
3530 if (kind == AARCH64_MOD_ROR)
3531 {
3532 set_syntax_error (_("'ROR' shift is not permitted"));
5b7c81bd 3533 return false;
a06ea964
NC
3534 }
3535 break;
3536
3537 case SHIFTED_LSL:
3538 if (kind != AARCH64_MOD_LSL)
3539 {
3540 set_syntax_error (_("only 'LSL' shift is permitted"));
5b7c81bd 3541 return false;
a06ea964
NC
3542 }
3543 break;
3544
2442d846
RS
3545 case SHIFTED_MUL:
3546 if (kind != AARCH64_MOD_MUL)
3547 {
3548 set_syntax_error (_("only 'MUL' is permitted"));
5b7c81bd 3549 return false;
2442d846
RS
3550 }
3551 break;
3552
98907a70
RS
3553 case SHIFTED_MUL_VL:
3554 /* "MUL VL" consists of two separate tokens. Require the first
3555 token to be "MUL" and look for a following "VL". */
3556 if (kind == AARCH64_MOD_MUL)
3557 {
3558 skip_whitespace (p);
3559 if (strncasecmp (p, "vl", 2) == 0 && !ISALPHA (p[2]))
3560 {
3561 p += 2;
3562 kind = AARCH64_MOD_MUL_VL;
3563 break;
3564 }
3565 }
3566 set_syntax_error (_("only 'MUL VL' is permitted"));
5b7c81bd 3567 return false;
98907a70 3568
a06ea964
NC
3569 case SHIFTED_REG_OFFSET:
3570 if (kind != AARCH64_MOD_UXTW && kind != AARCH64_MOD_LSL
3571 && kind != AARCH64_MOD_SXTW && kind != AARCH64_MOD_SXTX)
3572 {
3573 set_fatal_syntax_error
3574 (_("invalid shift for the register offset addressing mode"));
5b7c81bd 3575 return false;
a06ea964
NC
3576 }
3577 break;
3578
3579 case SHIFTED_LSL_MSL:
3580 if (kind != AARCH64_MOD_LSL && kind != AARCH64_MOD_MSL)
3581 {
3582 set_syntax_error (_("invalid shift operator"));
5b7c81bd 3583 return false;
a06ea964
NC
3584 }
3585 break;
3586
3587 default:
3588 abort ();
3589 }
3590
3591 /* Whitespace can appear here if the next thing is a bare digit. */
3592 skip_whitespace (p);
3593
3594 /* Parse shift amount. */
3595 exp_has_prefix = 0;
98907a70 3596 if ((mode == SHIFTED_REG_OFFSET && *p == ']') || kind == AARCH64_MOD_MUL_VL)
a06ea964
NC
3597 exp.X_op = O_absent;
3598 else
3599 {
3600 if (is_immediate_prefix (*p))
3601 {
3602 p++;
3603 exp_has_prefix = 1;
3604 }
c1723a81 3605 aarch64_get_expression (&exp, &p, GE_NO_PREFIX, ALLOW_ABSENT);
a06ea964 3606 }
98907a70
RS
3607 if (kind == AARCH64_MOD_MUL_VL)
3608 /* For consistency, give MUL VL the same shift amount as an implicit
3609 MUL #1. */
3610 operand->shifter.amount = 1;
3611 else if (exp.X_op == O_absent)
a06ea964 3612 {
535b785f 3613 if (!aarch64_extend_operator_p (kind) || exp_has_prefix)
a06ea964
NC
3614 {
3615 set_syntax_error (_("missing shift amount"));
5b7c81bd 3616 return false;
a06ea964
NC
3617 }
3618 operand->shifter.amount = 0;
3619 }
3620 else if (exp.X_op != O_constant)
3621 {
3622 set_syntax_error (_("constant shift amount required"));
5b7c81bd 3623 return false;
a06ea964 3624 }
2442d846
RS
3625 /* For parsing purposes, MUL #n has no inherent range. The range
3626 depends on the operand and will be checked by operand-specific
3627 routines. */
3628 else if (kind != AARCH64_MOD_MUL
3629 && (exp.X_add_number < 0 || exp.X_add_number > 63))
a06ea964
NC
3630 {
3631 set_fatal_syntax_error (_("shift amount out of range 0 to 63"));
5b7c81bd 3632 return false;
a06ea964
NC
3633 }
3634 else
3635 {
3636 operand->shifter.amount = exp.X_add_number;
3637 operand->shifter.amount_present = 1;
3638 }
3639
3640 operand->shifter.operator_present = 1;
3641 operand->shifter.kind = kind;
3642
3643 *str = p;
5b7c81bd 3644 return true;
a06ea964
NC
3645}
3646
3647/* Parse a <shifter_operand> for a data processing instruction:
3648
3649 #<immediate>
3650 #<immediate>, LSL #imm
3651
3652 Validation of immediate operands is deferred to md_apply_fix.
3653
3654 Return TRUE on success; otherwise return FALSE. */
3655
5b7c81bd 3656static bool
a06ea964
NC
3657parse_shifter_operand_imm (char **str, aarch64_opnd_info *operand,
3658 enum parse_shift_mode mode)
3659{
3660 char *p;
3661
3662 if (mode != SHIFTED_ARITH_IMM && mode != SHIFTED_LOGIC_IMM)
5b7c81bd 3663 return false;
a06ea964
NC
3664
3665 p = *str;
3666
3667 /* Accept an immediate expression. */
eac4eb8e 3668 if (! aarch64_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX,
c1723a81 3669 REJECT_ABSENT))
5b7c81bd 3670 return false;
a06ea964
NC
3671
3672 /* Accept optional LSL for arithmetic immediate values. */
3673 if (mode == SHIFTED_ARITH_IMM && skip_past_comma (&p))
3674 if (! parse_shift (&p, operand, SHIFTED_LSL))
5b7c81bd 3675 return false;
a06ea964
NC
3676
3677 /* Not accept any shifter for logical immediate values. */
3678 if (mode == SHIFTED_LOGIC_IMM && skip_past_comma (&p)
3679 && parse_shift (&p, operand, mode))
3680 {
3681 set_syntax_error (_("unexpected shift operator"));
5b7c81bd 3682 return false;
a06ea964
NC
3683 }
3684
3685 *str = p;
5b7c81bd 3686 return true;
a06ea964
NC
3687}
3688
3689/* Parse a <shifter_operand> for a data processing instruction:
3690
3691 <Rm>
3692 <Rm>, <shift>
3693 #<immediate>
3694 #<immediate>, LSL #imm
3695
3696 where <shift> is handled by parse_shift above, and the last two
3697 cases are handled by the function above.
3698
3699 Validation of immediate operands is deferred to md_apply_fix.
3700
3701 Return TRUE on success; otherwise return FALSE. */
3702
5b7c81bd 3703static bool
a06ea964
NC
3704parse_shifter_operand (char **str, aarch64_opnd_info *operand,
3705 enum parse_shift_mode mode)
3706{
e1b988bb
RS
3707 const reg_entry *reg;
3708 aarch64_opnd_qualifier_t qualifier;
a06ea964
NC
3709 enum aarch64_operand_class opd_class
3710 = aarch64_get_operand_class (operand->type);
3711
e1b988bb
RS
3712 reg = aarch64_reg_parse_32_64 (str, &qualifier);
3713 if (reg)
a06ea964
NC
3714 {
3715 if (opd_class == AARCH64_OPND_CLASS_IMMEDIATE)
3716 {
3717 set_syntax_error (_("unexpected register in the immediate operand"));
5b7c81bd 3718 return false;
a06ea964
NC
3719 }
3720
2f4e3a2c 3721 if (!aarch64_check_reg_type (reg, REG_TYPE_R_ZR))
a06ea964 3722 {
2f4e3a2c 3723 set_expected_reg_error (REG_TYPE_R_ZR, reg, 0);
5b7c81bd 3724 return false;
a06ea964
NC
3725 }
3726
e1b988bb
RS
3727 operand->reg.regno = reg->number;
3728 operand->qualifier = qualifier;
a06ea964
NC
3729
3730 /* Accept optional shift operation on register. */
3731 if (! skip_past_comma (str))
5b7c81bd 3732 return true;
a06ea964
NC
3733
3734 if (! parse_shift (str, operand, mode))
5b7c81bd 3735 return false;
a06ea964 3736
5b7c81bd 3737 return true;
a06ea964
NC
3738 }
3739 else if (opd_class == AARCH64_OPND_CLASS_MODIFIED_REG)
3740 {
3741 set_syntax_error
3742 (_("integer register expected in the extended/shifted operand "
3743 "register"));
5b7c81bd 3744 return false;
a06ea964
NC
3745 }
3746
3747 /* We have a shifted immediate variable. */
3748 return parse_shifter_operand_imm (str, operand, mode);
3749}
3750
4792a423
YK
3751static bool
3752parse_reg_lsl_shifter_operand (char **str, aarch64_opnd_info *operand)
3753{
3754 aarch64_opnd_qualifier_t qualifier;
3755 const reg_entry *reg = aarch64_reg_parse_32_64 (str, &qualifier);
3756
3757 if (reg)
3758 {
3759 if (!aarch64_check_reg_type (reg, REG_TYPE_R_ZR))
3760 {
3761 set_expected_reg_error (REG_TYPE_R_ZR, reg, 0);
3762 return false;
3763 }
3764
3765 operand->reg.regno = reg->number;
3766 operand->qualifier = qualifier;
3767
3768 /* Accept optional LSL shift operation on register. */
3769 if (!skip_past_comma (str))
3770 return true;
3771
3772 if (!parse_shift (str, operand, SHIFTED_LSL))
3773 return false;
3774
3775 return true;
3776 }
3777 else
3778 {
3779 set_syntax_error
3780 (_("integer register expected in the shifted operand "
3781 "register"));
3782 return false;
3783 }
3784}
3785
a06ea964
NC
3786/* Return TRUE on success; return FALSE otherwise. */
3787
5b7c81bd 3788static bool
a06ea964
NC
3789parse_shifter_operand_reloc (char **str, aarch64_opnd_info *operand,
3790 enum parse_shift_mode mode)
3791{
3792 char *p = *str;
3793
3794 /* Determine if we have the sequence of characters #: or just :
3795 coming next. If we do, then we check for a :rello: relocation
3796 modifier. If we don't, punt the whole lot to
3797 parse_shifter_operand. */
3798
3799 if ((p[0] == '#' && p[1] == ':') || p[0] == ':')
3800 {
3801 struct reloc_table_entry *entry;
3802
3803 if (p[0] == '#')
3804 p += 2;
3805 else
3806 p++;
3807 *str = p;
3808
3809 /* Try to parse a relocation. Anything else is an error. */
3810 if (!(entry = find_reloc_table_entry (str)))
3811 {
3812 set_syntax_error (_("unknown relocation modifier"));
5b7c81bd 3813 return false;
a06ea964
NC
3814 }
3815
3816 if (entry->add_type == 0)
3817 {
3818 set_syntax_error
3819 (_("this relocation modifier is not allowed on this instruction"));
5b7c81bd 3820 return false;
a06ea964
NC
3821 }
3822
3823 /* Save str before we decompose it. */
3824 p = *str;
3825
3826 /* Next, we parse the expression. */
eac4eb8e 3827 if (! aarch64_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX,
c1723a81 3828 REJECT_ABSENT))
5b7c81bd 3829 return false;
eac4eb8e 3830
a06ea964
NC
3831 /* Record the relocation type (use the ADD variant here). */
3832 inst.reloc.type = entry->add_type;
3833 inst.reloc.pc_rel = entry->pc_rel;
3834
3835 /* If str is empty, we've reached the end, stop here. */
3836 if (**str == '\0')
5b7c81bd 3837 return true;
a06ea964 3838
55d9b4c1 3839 /* Otherwise, we have a shifted reloc modifier, so rewind to
a06ea964
NC
3840 recover the variable name and continue parsing for the shifter. */
3841 *str = p;
3842 return parse_shifter_operand_imm (str, operand, mode);
3843 }
3844
3845 return parse_shifter_operand (str, operand, mode);
3846}
3847
3848/* Parse all forms of an address expression. Information is written
3849 to *OPERAND and/or inst.reloc.
3850
3851 The A64 instruction set has the following addressing modes:
3852
3853 Offset
4df068de
RS
3854 [base] // in SIMD ld/st structure
3855 [base{,#0}] // in ld/st exclusive
a06ea964
NC
3856 [base{,#imm}]
3857 [base,Xm{,LSL #imm}]
3858 [base,Xm,SXTX {#imm}]
3859 [base,Wm,(S|U)XTW {#imm}]
3860 Pre-indexed
1820262b 3861 [base]! // in ldraa/ldrab exclusive
a06ea964
NC
3862 [base,#imm]!
3863 Post-indexed
3864 [base],#imm
4df068de 3865 [base],Xm // in SIMD ld/st structure
a06ea964
NC
3866 PC-relative (literal)
3867 label
4df068de 3868 SVE:
98907a70 3869 [base,#imm,MUL VL]
4df068de
RS
3870 [base,Zm.D{,LSL #imm}]
3871 [base,Zm.S,(S|U)XTW {#imm}]
3872 [base,Zm.D,(S|U)XTW {#imm}] // ignores top 32 bits of Zm.D elements
3873 [Zn.S,#imm]
3874 [Zn.D,#imm]
c469c864 3875 [Zn.S{, Xm}]
4df068de
RS
3876 [Zn.S,Zm.S{,LSL #imm}] // in ADR
3877 [Zn.D,Zm.D{,LSL #imm}] // in ADR
3878 [Zn.D,Zm.D,(S|U)XTW {#imm}] // in ADR
a06ea964
NC
3879
3880 (As a convenience, the notation "=immediate" is permitted in conjunction
3881 with the pc-relative literal load instructions to automatically place an
3882 immediate value or symbolic address in a nearby literal pool and generate
3883 a hidden label which references it.)
3884
3885 Upon a successful parsing, the address structure in *OPERAND will be
3886 filled in the following way:
3887
3888 .base_regno = <base>
3889 .offset.is_reg // 1 if the offset is a register
3890 .offset.imm = <imm>
3891 .offset.regno = <Rm>
3892
3893 For different addressing modes defined in the A64 ISA:
3894
3895 Offset
3896 .pcrel=0; .preind=1; .postind=0; .writeback=0
3897 Pre-indexed
3898 .pcrel=0; .preind=1; .postind=0; .writeback=1
3899 Post-indexed
3900 .pcrel=0; .preind=0; .postind=1; .writeback=1
3901 PC-relative (literal)
3902 .pcrel=1; .preind=1; .postind=0; .writeback=0
3903
3904 The shift/extension information, if any, will be stored in .shifter.
4df068de
RS
3905 The base and offset qualifiers will be stored in *BASE_QUALIFIER and
3906 *OFFSET_QUALIFIER respectively, with NIL being used if there's no
3907 corresponding register.
a06ea964 3908
4df068de 3909 BASE_TYPE says which types of base register should be accepted and
98907a70
RS
3910 OFFSET_TYPE says the same for offset registers. IMM_SHIFT_MODE
3911 is the type of shifter that is allowed for immediate offsets,
3912 or SHIFTED_NONE if none.
3913
3914 In all other respects, it is the caller's responsibility to check
3915 for addressing modes not supported by the instruction, and to set
3916 inst.reloc.type. */
a06ea964 3917
5b7c81bd 3918static bool
4df068de
RS
3919parse_address_main (char **str, aarch64_opnd_info *operand,
3920 aarch64_opnd_qualifier_t *base_qualifier,
3921 aarch64_opnd_qualifier_t *offset_qualifier,
98907a70
RS
3922 aarch64_reg_type base_type, aarch64_reg_type offset_type,
3923 enum parse_shift_mode imm_shift_mode)
a06ea964
NC
3924{
3925 char *p = *str;
e1b988bb 3926 const reg_entry *reg;
a06ea964
NC
3927 expressionS *exp = &inst.reloc.exp;
3928
4df068de
RS
3929 *base_qualifier = AARCH64_OPND_QLF_NIL;
3930 *offset_qualifier = AARCH64_OPND_QLF_NIL;
a06ea964
NC
3931 if (! skip_past_char (&p, '['))
3932 {
3933 /* =immediate or label. */
3934 operand->addr.pcrel = 1;
3935 operand->addr.preind = 1;
3936
f41aef5f
RE
3937 /* #:<reloc_op>:<symbol> */
3938 skip_past_char (&p, '#');
73866052 3939 if (skip_past_char (&p, ':'))
f41aef5f 3940 {
6f4a313b 3941 bfd_reloc_code_real_type ty;
f41aef5f
RE
3942 struct reloc_table_entry *entry;
3943
3944 /* Try to parse a relocation modifier. Anything else is
3945 an error. */
3946 entry = find_reloc_table_entry (&p);
3947 if (! entry)
3948 {
3949 set_syntax_error (_("unknown relocation modifier"));
5b7c81bd 3950 return false;
f41aef5f
RE
3951 }
3952
6f4a313b
MS
3953 switch (operand->type)
3954 {
3955 case AARCH64_OPND_ADDR_PCREL21:
3956 /* adr */
3957 ty = entry->adr_type;
3958 break;
3959
3960 default:
74ad790c 3961 ty = entry->ld_literal_type;
6f4a313b
MS
3962 break;
3963 }
3964
3965 if (ty == 0)
f41aef5f
RE
3966 {
3967 set_syntax_error
3968 (_("this relocation modifier is not allowed on this "
3969 "instruction"));
5b7c81bd 3970 return false;
f41aef5f
RE
3971 }
3972
3973 /* #:<reloc_op>: */
c1723a81 3974 if (! aarch64_get_expression (exp, &p, GE_NO_PREFIX, REJECT_ABSENT))
f41aef5f
RE
3975 {
3976 set_syntax_error (_("invalid relocation expression"));
5b7c81bd 3977 return false;
f41aef5f 3978 }
f41aef5f 3979 /* #:<reloc_op>:<expr> */
6f4a313b
MS
3980 /* Record the relocation type. */
3981 inst.reloc.type = ty;
f41aef5f
RE
3982 inst.reloc.pc_rel = entry->pc_rel;
3983 }
3984 else
a06ea964 3985 {
f41aef5f
RE
3986 if (skip_past_char (&p, '='))
3987 /* =immediate; need to generate the literal in the literal pool. */
3988 inst.gen_lit_pool = 1;
3989
c1723a81 3990 if (!aarch64_get_expression (exp, &p, GE_NO_PREFIX, REJECT_ABSENT))
f41aef5f
RE
3991 {
3992 set_syntax_error (_("invalid address"));
5b7c81bd 3993 return false;
f41aef5f 3994 }
a06ea964
NC
3995 }
3996
3997 *str = p;
5b7c81bd 3998 return true;
a06ea964
NC
3999 }
4000
4001 /* [ */
4002
72c1dab0 4003 bool alpha_base_p = ISALPHA (*p);
4df068de
RS
4004 reg = aarch64_addr_reg_parse (&p, base_type, base_qualifier);
4005 if (!reg || !aarch64_check_reg_type (reg, base_type))
a06ea964 4006 {
72c1dab0
RS
4007 if (reg
4008 && aarch64_check_reg_type (reg, REG_TYPE_R_SP)
4009 && *base_qualifier == AARCH64_OPND_QLF_W)
4010 set_syntax_error (_("expected a 64-bit base register"));
4011 else if (alpha_base_p)
4012 set_syntax_error (_("invalid base register"));
4013 else
4014 set_syntax_error (_("expected a base register"));
5b7c81bd 4015 return false;
a06ea964 4016 }
e1b988bb 4017 operand->addr.base_regno = reg->number;
a06ea964
NC
4018
4019 /* [Xn */
4020 if (skip_past_comma (&p))
4021 {
4022 /* [Xn, */
4023 operand->addr.preind = 1;
4024
4df068de 4025 reg = aarch64_addr_reg_parse (&p, offset_type, offset_qualifier);
e1b988bb 4026 if (reg)
a06ea964 4027 {
4df068de 4028 if (!aarch64_check_reg_type (reg, offset_type))
e1b988bb 4029 {
72c1dab0 4030 set_syntax_error (_("invalid offset register"));
5b7c81bd 4031 return false;
e1b988bb
RS
4032 }
4033
a06ea964 4034 /* [Xn,Rm */
e1b988bb 4035 operand->addr.offset.regno = reg->number;
a06ea964
NC
4036 operand->addr.offset.is_reg = 1;
4037 /* Shifted index. */
4038 if (skip_past_comma (&p))
4039 {
4040 /* [Xn,Rm, */
4041 if (! parse_shift (&p, operand, SHIFTED_REG_OFFSET))
4042 /* Use the diagnostics set in parse_shift, so not set new
4043 error message here. */
5b7c81bd 4044 return false;
a06ea964
NC
4045 }
4046 /* We only accept:
c469c864 4047 [base,Xm] # For vector plus scalar SVE2 indexing.
a06ea964
NC
4048 [base,Xm{,LSL #imm}]
4049 [base,Xm,SXTX {#imm}]
4050 [base,Wm,(S|U)XTW {#imm}] */
4051 if (operand->shifter.kind == AARCH64_MOD_NONE
4052 || operand->shifter.kind == AARCH64_MOD_LSL
4053 || operand->shifter.kind == AARCH64_MOD_SXTX)
4054 {
4df068de 4055 if (*offset_qualifier == AARCH64_OPND_QLF_W)
a06ea964
NC
4056 {
4057 set_syntax_error (_("invalid use of 32-bit register offset"));
5b7c81bd 4058 return false;
a06ea964 4059 }
4df068de 4060 if (aarch64_get_qualifier_esize (*base_qualifier)
c469c864
MM
4061 != aarch64_get_qualifier_esize (*offset_qualifier)
4062 && (operand->type != AARCH64_OPND_SVE_ADDR_ZX
4063 || *base_qualifier != AARCH64_OPND_QLF_S_S
4064 || *offset_qualifier != AARCH64_OPND_QLF_X))
4df068de
RS
4065 {
4066 set_syntax_error (_("offset has different size from base"));
5b7c81bd 4067 return false;
4df068de 4068 }
a06ea964 4069 }
4df068de 4070 else if (*offset_qualifier == AARCH64_OPND_QLF_X)
a06ea964
NC
4071 {
4072 set_syntax_error (_("invalid use of 64-bit register offset"));
5b7c81bd 4073 return false;
a06ea964
NC
4074 }
4075 }
4076 else
4077 {
4078 /* [Xn,#:<reloc_op>:<symbol> */
4079 skip_past_char (&p, '#');
73866052 4080 if (skip_past_char (&p, ':'))
a06ea964
NC
4081 {
4082 struct reloc_table_entry *entry;
4083
4084 /* Try to parse a relocation modifier. Anything else is
4085 an error. */
4086 if (!(entry = find_reloc_table_entry (&p)))
4087 {
4088 set_syntax_error (_("unknown relocation modifier"));
5b7c81bd 4089 return false;
a06ea964
NC
4090 }
4091
4092 if (entry->ldst_type == 0)
4093 {
4094 set_syntax_error
4095 (_("this relocation modifier is not allowed on this "
4096 "instruction"));
5b7c81bd 4097 return false;
a06ea964
NC
4098 }
4099
4100 /* [Xn,#:<reloc_op>: */
4101 /* We now have the group relocation table entry corresponding to
4102 the name in the assembler source. Next, we parse the
4103 expression. */
c1723a81 4104 if (! aarch64_get_expression (exp, &p, GE_NO_PREFIX, REJECT_ABSENT))
a06ea964
NC
4105 {
4106 set_syntax_error (_("invalid relocation expression"));
5b7c81bd 4107 return false;
a06ea964
NC
4108 }
4109
4110 /* [Xn,#:<reloc_op>:<expr> */
4111 /* Record the load/store relocation type. */
4112 inst.reloc.type = entry->ldst_type;
4113 inst.reloc.pc_rel = entry->pc_rel;
4114 }
98907a70 4115 else
a06ea964 4116 {
c1723a81 4117 if (! aarch64_get_expression (exp, &p, GE_OPT_PREFIX, REJECT_ABSENT))
98907a70
RS
4118 {
4119 set_syntax_error (_("invalid expression in the address"));
5b7c81bd 4120 return false;
98907a70
RS
4121 }
4122 /* [Xn,<expr> */
4123 if (imm_shift_mode != SHIFTED_NONE && skip_past_comma (&p))
4124 /* [Xn,<expr>,<shifter> */
4125 if (! parse_shift (&p, operand, imm_shift_mode))
5b7c81bd 4126 return false;
a06ea964 4127 }
a06ea964
NC
4128 }
4129 }
4130
4131 if (! skip_past_char (&p, ']'))
4132 {
4133 set_syntax_error (_("']' expected"));
5b7c81bd 4134 return false;
a06ea964
NC
4135 }
4136
4137 if (skip_past_char (&p, '!'))
4138 {
4139 if (operand->addr.preind && operand->addr.offset.is_reg)
4140 {
4141 set_syntax_error (_("register offset not allowed in pre-indexed "
4142 "addressing mode"));
5b7c81bd 4143 return false;
a06ea964
NC
4144 }
4145 /* [Xn]! */
4146 operand->addr.writeback = 1;
4147 }
4148 else if (skip_past_comma (&p))
4149 {
4150 /* [Xn], */
4151 operand->addr.postind = 1;
4152 operand->addr.writeback = 1;
4153
4154 if (operand->addr.preind)
4155 {
4156 set_syntax_error (_("cannot combine pre- and post-indexing"));
5b7c81bd 4157 return false;
a06ea964
NC
4158 }
4159
4df068de 4160 reg = aarch64_reg_parse_32_64 (&p, offset_qualifier);
73866052 4161 if (reg)
a06ea964
NC
4162 {
4163 /* [Xn],Xm */
e1b988bb 4164 if (!aarch64_check_reg_type (reg, REG_TYPE_R_64))
a06ea964 4165 {
72c1dab0 4166 set_syntax_error (_("invalid offset register"));
5b7c81bd 4167 return false;
a06ea964 4168 }
e1b988bb
RS
4169
4170 operand->addr.offset.regno = reg->number;
a06ea964
NC
4171 operand->addr.offset.is_reg = 1;
4172 }
c1723a81 4173 else if (! aarch64_get_expression (exp, &p, GE_OPT_PREFIX, REJECT_ABSENT))
a06ea964
NC
4174 {
4175 /* [Xn],#expr */
4176 set_syntax_error (_("invalid expression in the address"));
5b7c81bd 4177 return false;
a06ea964
NC
4178 }
4179 }
4180
4181 /* If at this point neither .preind nor .postind is set, we have a
1820262b
DB
4182 bare [Rn]{!}; only accept [Rn]! as a shorthand for [Rn,#0]! for ldraa and
4183 ldrab, accept [Rn] as a shorthand for [Rn,#0].
c469c864
MM
4184 For SVE2 vector plus scalar offsets, allow [Zn.<T>] as shorthand for
4185 [Zn.<T>, xzr]. */
a06ea964
NC
4186 if (operand->addr.preind == 0 && operand->addr.postind == 0)
4187 {
550fd7bf 4188 if (operand->addr.writeback)
a06ea964 4189 {
1820262b
DB
4190 if (operand->type == AARCH64_OPND_ADDR_SIMM10)
4191 {
4192 /* Accept [Rn]! as a shorthand for [Rn,#0]! */
4193 operand->addr.offset.is_reg = 0;
4194 operand->addr.offset.imm = 0;
4195 operand->addr.preind = 1;
4196 }
4197 else
4198 {
4199 /* Reject [Rn]! */
4200 set_syntax_error (_("missing offset in the pre-indexed address"));
5b7c81bd 4201 return false;
1820262b 4202 }
a06ea964 4203 }
1820262b 4204 else
c469c864 4205 {
1820262b
DB
4206 operand->addr.preind = 1;
4207 if (operand->type == AARCH64_OPND_SVE_ADDR_ZX)
4208 {
4209 operand->addr.offset.is_reg = 1;
4210 operand->addr.offset.regno = REG_ZR;
4211 *offset_qualifier = AARCH64_OPND_QLF_X;
4212 }
4213 else
4214 {
4215 inst.reloc.exp.X_op = O_constant;
4216 inst.reloc.exp.X_add_number = 0;
4217 }
c469c864 4218 }
a06ea964
NC
4219 }
4220
4221 *str = p;
5b7c81bd 4222 return true;
a06ea964
NC
4223}
4224
73866052
RS
4225/* Parse a base AArch64 address (as opposed to an SVE one). Return TRUE
4226 on success. */
5b7c81bd 4227static bool
73866052 4228parse_address (char **str, aarch64_opnd_info *operand)
a06ea964 4229{
4df068de
RS
4230 aarch64_opnd_qualifier_t base_qualifier, offset_qualifier;
4231 return parse_address_main (str, operand, &base_qualifier, &offset_qualifier,
2f4e3a2c 4232 REG_TYPE_R64_SP, REG_TYPE_R_ZR, SHIFTED_NONE);
4df068de
RS
4233}
4234
98907a70 4235/* Parse an address in which SVE vector registers and MUL VL are allowed.
4df068de
RS
4236 The arguments have the same meaning as for parse_address_main.
4237 Return TRUE on success. */
5b7c81bd 4238static bool
4df068de
RS
4239parse_sve_address (char **str, aarch64_opnd_info *operand,
4240 aarch64_opnd_qualifier_t *base_qualifier,
4241 aarch64_opnd_qualifier_t *offset_qualifier)
4242{
4243 return parse_address_main (str, operand, base_qualifier, offset_qualifier,
98907a70
RS
4244 REG_TYPE_SVE_BASE, REG_TYPE_SVE_OFFSET,
4245 SHIFTED_MUL_VL);
a06ea964
NC
4246}
4247
6327658e
RS
4248/* Parse a register X0-X30. The register must be 64-bit and register 31
4249 is unallocated. */
4250static bool
4251parse_x0_to_x30 (char **str, aarch64_opnd_info *operand)
4252{
4253 const reg_entry *reg = parse_reg (str);
4254 if (!reg || !aarch64_check_reg_type (reg, REG_TYPE_R_64))
4255 {
e426521e 4256 set_expected_reg_error (REG_TYPE_R_64, reg, 0);
6327658e
RS
4257 return false;
4258 }
4259 operand->reg.regno = reg->number;
4260 operand->qualifier = AARCH64_OPND_QLF_X;
4261 return true;
4262}
4263
a06ea964
NC
4264/* Parse an operand for a MOVZ, MOVN or MOVK instruction.
4265 Return TRUE on success; otherwise return FALSE. */
5b7c81bd 4266static bool
a06ea964
NC
4267parse_half (char **str, int *internal_fixup_p)
4268{
671eeb28 4269 char *p = *str;
a06ea964 4270
a06ea964
NC
4271 skip_past_char (&p, '#');
4272
4273 gas_assert (internal_fixup_p);
4274 *internal_fixup_p = 0;
4275
4276 if (*p == ':')
4277 {
4278 struct reloc_table_entry *entry;
4279
4280 /* Try to parse a relocation. Anything else is an error. */
4281 ++p;
eac4eb8e 4282
a06ea964
NC
4283 if (!(entry = find_reloc_table_entry (&p)))
4284 {
4285 set_syntax_error (_("unknown relocation modifier"));
5b7c81bd 4286 return false;
a06ea964
NC
4287 }
4288
4289 if (entry->movw_type == 0)
4290 {
4291 set_syntax_error
4292 (_("this relocation modifier is not allowed on this instruction"));
5b7c81bd 4293 return false;
a06ea964
NC
4294 }
4295
4296 inst.reloc.type = entry->movw_type;
4297 }
4298 else
4299 *internal_fixup_p = 1;
4300
c1723a81 4301 if (! aarch64_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX, REJECT_ABSENT))
5b7c81bd 4302 return false;
a06ea964
NC
4303
4304 *str = p;
5b7c81bd 4305 return true;
a06ea964
NC
4306}
4307
4308/* Parse an operand for an ADRP instruction:
4309 ADRP <Xd>, <label>
4310 Return TRUE on success; otherwise return FALSE. */
4311
5b7c81bd 4312static bool
a06ea964
NC
4313parse_adrp (char **str)
4314{
4315 char *p;
4316
4317 p = *str;
4318 if (*p == ':')
4319 {
4320 struct reloc_table_entry *entry;
4321
4322 /* Try to parse a relocation. Anything else is an error. */
4323 ++p;
4324 if (!(entry = find_reloc_table_entry (&p)))
4325 {
4326 set_syntax_error (_("unknown relocation modifier"));
5b7c81bd 4327 return false;
a06ea964
NC
4328 }
4329
4330 if (entry->adrp_type == 0)
4331 {
4332 set_syntax_error
4333 (_("this relocation modifier is not allowed on this instruction"));
5b7c81bd 4334 return false;
a06ea964
NC
4335 }
4336
4337 inst.reloc.type = entry->adrp_type;
4338 }
4339 else
4340 inst.reloc.type = BFD_RELOC_AARCH64_ADR_HI21_PCREL;
4341
4342 inst.reloc.pc_rel = 1;
c1723a81 4343 if (! aarch64_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX, REJECT_ABSENT))
5b7c81bd 4344 return false;
a06ea964 4345 *str = p;
5b7c81bd 4346 return true;
a06ea964
NC
4347}
4348
4349/* Miscellaneous. */
4350
245d2e3f
RS
4351/* Parse a symbolic operand such as "pow2" at *STR. ARRAY is an array
4352 of SIZE tokens in which index I gives the token for field value I,
99e01a66
RS
4353 or is null if field value I is invalid. If the symbolic operand
4354 can also be given as a 0-based integer, REG_TYPE says which register
4355 names should be treated as registers rather than as symbolic immediates
4356 while parsing that integer. REG_TYPE is REG_TYPE_MAX otherwise.
245d2e3f
RS
4357
4358 Return true on success, moving *STR past the operand and storing the
4359 field value in *VAL. */
4360
4361static int
4362parse_enum_string (char **str, int64_t *val, const char *const *array,
4363 size_t size, aarch64_reg_type reg_type)
4364{
4365 expressionS exp;
4366 char *p, *q;
4367 size_t i;
4368
4369 /* Match C-like tokens. */
4370 p = q = *str;
4371 while (ISALNUM (*q))
4372 q++;
4373
4374 for (i = 0; i < size; ++i)
4375 if (array[i]
4376 && strncasecmp (array[i], p, q - p) == 0
4377 && array[i][q - p] == 0)
4378 {
4379 *val = i;
4380 *str = q;
5b7c81bd 4381 return true;
245d2e3f
RS
4382 }
4383
99e01a66
RS
4384 if (reg_type == REG_TYPE_MAX)
4385 return false;
4386
245d2e3f 4387 if (!parse_immediate_expression (&p, &exp, reg_type))
5b7c81bd 4388 return false;
245d2e3f
RS
4389
4390 if (exp.X_op == O_constant
4391 && (uint64_t) exp.X_add_number < size)
4392 {
4393 *val = exp.X_add_number;
4394 *str = p;
5b7c81bd 4395 return true;
245d2e3f
RS
4396 }
4397
4398 /* Use the default error for this operand. */
5b7c81bd 4399 return false;
245d2e3f
RS
4400}
4401
a06ea964
NC
4402/* Parse an option for a preload instruction. Returns the encoding for the
4403 option, or PARSE_FAIL. */
4404
4405static int
4406parse_pldop (char **str)
4407{
4408 char *p, *q;
4409 const struct aarch64_name_value_pair *o;
4410
4411 p = q = *str;
4412 while (ISALNUM (*q))
4413 q++;
4414
629310ab 4415 o = str_hash_find_n (aarch64_pldop_hsh, p, q - p);
a06ea964
NC
4416 if (!o)
4417 return PARSE_FAIL;
4418
4419 *str = q;
4420 return o->value;
4421}
4422
4423/* Parse an option for a barrier instruction. Returns the encoding for the
4424 option, or PARSE_FAIL. */
4425
4426static int
4427parse_barrier (char **str)
4428{
4429 char *p, *q;
05cfb0d8 4430 const struct aarch64_name_value_pair *o;
a06ea964
NC
4431
4432 p = q = *str;
4433 while (ISALPHA (*q))
4434 q++;
4435
629310ab 4436 o = str_hash_find_n (aarch64_barrier_opt_hsh, p, q - p);
a06ea964
NC
4437 if (!o)
4438 return PARSE_FAIL;
4439
4440 *str = q;
4441 return o->value;
4442}
4443
c58f84d8
SP
4444/* Parse an option for barrier, bti and guarded control stack data
4445 synchronization instructions. Return true on matching the target
4446 options else return false. */
1e6f4800 4447
c58f84d8
SP
4448static bool
4449parse_hint_opt (const char *name, char **str,
4450 const struct aarch64_name_value_pair ** hint_opt)
ff605452
SD
4451{
4452 char *p, *q;
4453 const struct aarch64_name_value_pair *o;
4454
4455 p = q = *str;
4456 while (ISALPHA (*q))
4457 q++;
4458
629310ab 4459 o = str_hash_find_n (aarch64_hint_opt_hsh, p, q - p);
ff605452 4460 if (!o)
c58f84d8 4461 return false;
ff605452 4462
c58f84d8
SP
4463 if ((strcmp ("gcsb", name) == 0 && o->value != HINT_OPD_DSYNC)
4464 || ((strcmp ("psb", name) == 0 || strcmp ("tsb", name) == 0)
4465 && o->value != HINT_OPD_CSYNC)
4466 || ((strcmp ("bti", name) == 0)
4467 && (o->value != HINT_OPD_C && o->value != HINT_OPD_J
4468 && o->value != HINT_OPD_JC)))
4469 return false;
ff605452
SD
4470
4471 *str = q;
4472 *hint_opt = o;
c58f84d8 4473 return true;
ff605452
SD
4474}
4475
971eda73
PW
4476/* Parse STR for reg of REG_TYPE and following '.' and QUALIFIER.
4477 Function returns REG_ENTRY struct and QUALIFIER [bhsdq] or NULL
4478 on failure. Format:
4479
4480 REG_TYPE.QUALIFIER
4481
4482 Side effect: Update STR with current parse position of success.
b9ca3896
RS
4483
4484 FLAGS is as for parse_typed_reg. */
971eda73
PW
4485
4486static const reg_entry *
4487parse_reg_with_qual (char **str, aarch64_reg_type reg_type,
b9ca3896 4488 aarch64_opnd_qualifier_t *qualifier, unsigned int flags)
971eda73 4489{
c211f268
RS
4490 struct vector_type_el vectype;
4491 const reg_entry *reg = parse_typed_reg (str, reg_type, &vectype,
b9ca3896 4492 PTR_FULL_REG | flags);
c211f268
RS
4493 if (!reg)
4494 return NULL;
971eda73 4495
e9e1ddbb
RS
4496 if (vectype.type == NT_invtype)
4497 *qualifier = AARCH64_OPND_QLF_NIL;
4498 else
4499 {
4500 *qualifier = vectype_to_qualifier (&vectype);
4501 if (*qualifier == AARCH64_OPND_QLF_NIL)
4502 return NULL;
4503 }
971eda73 4504
c211f268 4505 return reg;
971eda73
PW
4506}
4507
7bb5f07c
PW
4508/* Parse STR for unsigned, immediate (1-2 digits) in format:
4509
4510 #<imm>
4511 <imm>
4512
4513 Function return TRUE if immediate was found, or FALSE.
4514*/
4515static bool
4516parse_sme_immediate (char **str, int64_t *imm)
4517{
4518 int64_t val;
4519 if (! parse_constant_immediate (str, &val, REG_TYPE_R_N))
4520 return false;
4521
4522 *imm = val;
4523 return true;
4524}
4525
7da28504 4526/* Parse index with selection register and immediate offset:
7bb5f07c
PW
4527
4528 [<Wv>, <imm>]
4529 [<Wv>, #<imm>]
89e06ec1 4530 [<Ws>, <offsf>:<offsl>]
7bb5f07c 4531
1d597b88 4532 Return true on success, populating OPND with the parsed index. */
7bb5f07c 4533
7bb5f07c 4534static bool
1d597b88 4535parse_sme_za_index (char **str, struct aarch64_indexed_za *opnd)
7bb5f07c
PW
4536{
4537 const reg_entry *reg;
4538
4539 if (!skip_past_char (str, '['))
4540 {
4541 set_syntax_error (_("expected '['"));
4542 return false;
4543 }
4544
7da28504 4545 /* The selection register, encoded in the 2-bit Rv field. */
7bb5f07c 4546 reg = parse_reg (str);
7da28504 4547 if (reg == NULL || reg->type != REG_TYPE_R_32)
7bb5f07c 4548 {
7da28504 4549 set_syntax_error (_("expected a 32-bit selection register"));
7bb5f07c
PW
4550 return false;
4551 }
1d597b88 4552 opnd->index.regno = reg->number;
7bb5f07c 4553
f42fba98 4554 if (!skip_past_char (str, ','))
7bb5f07c 4555 {
f42fba98 4556 set_syntax_error (_("missing immediate offset"));
7bb5f07c
PW
4557 return false;
4558 }
4559
1d597b88 4560 if (!parse_sme_immediate (str, &opnd->index.imm))
7bb5f07c 4561 {
f42fba98 4562 set_syntax_error (_("expected a constant immediate offset"));
7bb5f07c
PW
4563 return false;
4564 }
4565
586c6281
RS
4566 if (skip_past_char (str, ':'))
4567 {
4568 int64_t end;
4569 if (!parse_sme_immediate (str, &end))
4570 {
4571 set_syntax_error (_("expected a constant immediate offset"));
4572 return false;
4573 }
4574 if (end < opnd->index.imm)
4575 {
4576 set_syntax_error (_("the last offset is less than the"
4577 " first offset"));
4578 return false;
4579 }
4580 if (end == opnd->index.imm)
4581 {
4582 set_syntax_error (_("the last offset is equal to the"
4583 " first offset"));
4584 return false;
4585 }
4586 opnd->index.countm1 = (uint64_t) end - opnd->index.imm;
4587 }
4588
e2dc4040
RS
4589 opnd->group_size = 0;
4590 if (skip_past_char (str, ','))
4591 {
4592 if (strncasecmp (*str, "vgx2", 4) == 0 && !ISALPHA ((*str)[4]))
4593 {
4594 *str += 4;
4595 opnd->group_size = 2;
4596 }
4597 else if (strncasecmp (*str, "vgx4", 4) == 0 && !ISALPHA ((*str)[4]))
4598 {
4599 *str += 4;
4600 opnd->group_size = 4;
4601 }
4602 else
4603 {
4604 set_syntax_error (_("invalid vector group size"));
4605 return false;
4606 }
4607 }
4608
7bb5f07c
PW
4609 if (!skip_past_char (str, ']'))
4610 {
4611 set_syntax_error (_("expected ']'"));
4612 return false;
4613 }
4614
4615 return true;
4616}
4617
9d862382
RS
4618/* Parse a register of type REG_TYPE that might have an element type
4619 qualifier and that is indexed by two values: a 32-bit register,
7da28504
RS
4620 followed by an immediate. The ranges of the register and the
4621 immediate vary by opcode and are checked in libopcodes.
9d862382 4622
1d597b88 4623 Return true on success, populating OPND with information about
9d862382 4624 the operand and setting QUALIFIER to the register qualifier.
7bb5f07c
PW
4625
4626 Field format examples:
4627
9d862382
RS
4628 <Pm>.<T>[<Wv>< #<imm>]
4629 ZA[<Wv>, #<imm>]
4630 <ZAn><HV>.<T>[<Wv>, #<imm>]
89e06ec1 4631 <ZAn><HV>.<T>[<Ws>, <offsf>:<offsl>]
b9ca3896
RS
4632
4633 FLAGS is as for parse_typed_reg. */
4634
1d597b88 4635static bool
9d862382
RS
4636parse_dual_indexed_reg (char **str, aarch64_reg_type reg_type,
4637 struct aarch64_indexed_za *opnd,
b9ca3896
RS
4638 aarch64_opnd_qualifier_t *qualifier,
4639 unsigned int flags)
7bb5f07c 4640{
b9ca3896 4641 const reg_entry *reg = parse_reg_with_qual (str, reg_type, qualifier, flags);
83dbd40d 4642 if (!reg)
1d597b88 4643 return false;
83dbd40d 4644
1d597b88
RS
4645 opnd->v = aarch64_check_reg_type (reg, REG_TYPE_ZATV);
4646 opnd->regno = reg->number;
7bb5f07c 4647
ff60bcbf 4648 return parse_sme_za_index (str, opnd);
7bb5f07c
PW
4649}
4650
1d597b88
RS
4651/* Like parse_sme_za_hv_tiles_operand, but expect braces around the
4652 operand. */
01a4d082 4653
1d597b88 4654static bool
01a4d082 4655parse_sme_za_hv_tiles_operand_with_braces (char **str,
1d597b88 4656 struct aarch64_indexed_za *opnd,
01a4d082
PW
4657 aarch64_opnd_qualifier_t *qualifier)
4658{
01a4d082
PW
4659 if (!skip_past_char (str, '{'))
4660 {
e426521e 4661 set_expected_reglist_error (REG_TYPE_ZATHV, parse_reg (str));
1d597b88 4662 return false;
01a4d082
PW
4663 }
4664
b9ca3896
RS
4665 if (!parse_dual_indexed_reg (str, REG_TYPE_ZATHV, opnd, qualifier,
4666 PTR_IN_REGLIST))
1d597b88 4667 return false;
01a4d082
PW
4668
4669 if (!skip_past_char (str, '}'))
4670 {
4671 set_syntax_error (_("expected '}'"));
1d597b88 4672 return false;
01a4d082
PW
4673 }
4674
1d597b88 4675 return true;
01a4d082
PW
4676}
4677
1cad938d
PW
4678/* Parse list of up to eight 64-bit element tile names separated by commas in
4679 SME's ZERO instruction:
4680
4681 ZERO { <mask> }
4682
4683 Function returns <mask>:
4684
4685 an 8-bit list of 64-bit element tiles named ZA0.D to ZA7.D.
4686*/
4687static int
4688parse_sme_zero_mask(char **str)
4689{
4690 char *q;
4691 int mask;
4692 aarch64_opnd_qualifier_t qualifier;
405f0c41 4693 unsigned int ptr_flags = PTR_IN_REGLIST;
1cad938d
PW
4694
4695 mask = 0x00;
4696 q = *str;
4697 do
4698 {
e9e1ddbb 4699 const reg_entry *reg = parse_reg_with_qual (&q, REG_TYPE_ZA_ZAT,
405f0c41 4700 &qualifier, ptr_flags);
e9e1ddbb
RS
4701 if (!reg)
4702 return PARSE_FAIL;
4703
4704 if (reg->type == REG_TYPE_ZA)
4705 {
4706 if (qualifier != AARCH64_OPND_QLF_NIL)
4707 {
4708 set_syntax_error ("ZA should not have a size suffix");
4709 return PARSE_FAIL;
4710 }
4711 /* { ZA } is assembled as all-ones immediate. */
4712 mask = 0xff;
4713 }
4714 else
4715 {
1cad938d 4716 int regno = reg->number;
8cde0b98 4717 if (qualifier == AARCH64_OPND_QLF_S_B)
1cad938d
PW
4718 {
4719 /* { ZA0.B } is assembled as all-ones immediate. */
4720 mask = 0xff;
4721 }
8cde0b98 4722 else if (qualifier == AARCH64_OPND_QLF_S_H)
1cad938d 4723 mask |= 0x55 << regno;
8cde0b98 4724 else if (qualifier == AARCH64_OPND_QLF_S_S)
1cad938d 4725 mask |= 0x11 << regno;
8cde0b98 4726 else if (qualifier == AARCH64_OPND_QLF_S_D)
1cad938d 4727 mask |= 0x01 << regno;
e9e1ddbb
RS
4728 else if (qualifier == AARCH64_OPND_QLF_S_Q)
4729 {
4730 set_syntax_error (_("ZA tile masks do not operate at .Q"
4731 " granularity"));
4732 return PARSE_FAIL;
4733 }
4734 else if (qualifier == AARCH64_OPND_QLF_NIL)
4735 {
4736 set_syntax_error (_("missing ZA tile size"));
4737 return PARSE_FAIL;
4738 }
1cad938d
PW
4739 else
4740 {
e9e1ddbb 4741 set_syntax_error (_("invalid ZA tile"));
1cad938d
PW
4742 return PARSE_FAIL;
4743 }
1cad938d 4744 }
405f0c41 4745 ptr_flags |= PTR_GOOD_MATCH;
1cad938d
PW
4746 }
4747 while (skip_past_char (&q, ','));
4748
4749 *str = q;
4750 return mask;
4751}
4752
4753/* Wraps in curly braces <mask> operand ZERO instruction:
4754
4755 ZERO { <mask> }
4756
4757 Function returns value of <mask> bit-field.
4758*/
4759static int
4760parse_sme_list_of_64bit_tiles (char **str)
4761{
4762 int regno;
4763
4764 if (!skip_past_char (str, '{'))
4765 {
4766 set_syntax_error (_("expected '{'"));
4767 return PARSE_FAIL;
4768 }
4769
4770 /* Empty <mask> list is an all-zeros immediate. */
4771 if (!skip_past_char (str, '}'))
4772 {
4773 regno = parse_sme_zero_mask (str);
4774 if (regno == PARSE_FAIL)
4775 return PARSE_FAIL;
4776
4777 if (!skip_past_char (str, '}'))
4778 {
4779 set_syntax_error (_("expected '}'"));
4780 return PARSE_FAIL;
4781 }
4782 }
4783 else
4784 regno = 0x00;
4785
4786 return regno;
4787}
7bb5f07c 4788
3dd032c5
PW
4789/* Parse streaming mode operand for SMSTART and SMSTOP.
4790
4791 {SM | ZA}
4792
4793 Function returns 's' if SM or 'z' if ZM is parsed. Otherwise PARSE_FAIL.
4794*/
4795static int
4796parse_sme_sm_za (char **str)
4797{
4798 char *p, *q;
4799
4800 p = q = *str;
4801 while (ISALPHA (*q))
4802 q++;
4803
4804 if ((q - p != 2)
4805 || (strncasecmp ("sm", p, 2) != 0 && strncasecmp ("za", p, 2) != 0))
4806 {
4807 set_syntax_error (_("expected SM or ZA operand"));
4808 return PARSE_FAIL;
4809 }
4810
4811 *str = q;
4812 return TOLOWER (p[0]);
4813}
4814
a06ea964 4815/* Parse a system register or a PSTATE field name for an MSR/MRS instruction.
a203d9b7 4816 Returns the encoding for the option, or PARSE_FAIL.
a06ea964
NC
4817
4818 If IMPLE_DEFINED_P is non-zero, the function will also try to parse the
72ca8fad
MW
4819 implementation defined system register name S<op0>_<op1>_<Cn>_<Cm>_<op2>.
4820
4821 If PSTATEFIELD_P is non-zero, the function will parse the name as a PSTATE
4822 field, otherwise as a system register.
4823*/
a06ea964
NC
4824
4825static int
629310ab 4826parse_sys_reg (char **str, htab_t sys_regs,
561a72d4 4827 int imple_defined_p, int pstatefield_p,
9af8f671 4828 uint32_t* flags, bool sysreg128_p)
a06ea964
NC
4829{
4830 char *p, *q;
fa63795f 4831 char buf[AARCH64_MAX_SYSREG_NAME_LEN];
49eec193 4832 const aarch64_sys_reg *o;
a06ea964
NC
4833 int value;
4834
4835 p = buf;
4836 for (q = *str; ISALNUM (*q) || *q == '_'; q++)
fa63795f 4837 if (p < buf + (sizeof (buf) - 1))
a06ea964
NC
4838 *p++ = TOLOWER (*q);
4839 *p = '\0';
fa63795f
AC
4840
4841 /* If the name is longer than AARCH64_MAX_SYSREG_NAME_LEN then it cannot be a
4842 valid system register. This is enforced by construction of the hash
4843 table. */
4844 if (p - buf != q - *str)
4845 return PARSE_FAIL;
a06ea964 4846
629310ab 4847 o = str_hash_find (sys_regs, buf);
a06ea964
NC
4848 if (!o)
4849 {
4850 if (!imple_defined_p)
4851 return PARSE_FAIL;
4852 else
4853 {
df7b4545 4854 /* Parse S<op0>_<op1>_<Cn>_<Cm>_<op2>. */
a06ea964 4855 unsigned int op0, op1, cn, cm, op2;
df7b4545
JW
4856
4857 if (sscanf (buf, "s%u_%u_c%u_c%u_%u", &op0, &op1, &cn, &cm, &op2)
4858 != 5)
a06ea964 4859 return PARSE_FAIL;
df7b4545 4860 if (op0 > 3 || op1 > 7 || cn > 15 || cm > 15 || op2 > 7)
a06ea964
NC
4861 return PARSE_FAIL;
4862 value = (op0 << 14) | (op1 << 11) | (cn << 7) | (cm << 3) | op2;
561a72d4
TC
4863 if (flags)
4864 *flags = 0;
a06ea964
NC
4865 }
4866 }
4867 else
49eec193 4868 {
72ca8fad
MW
4869 if (pstatefield_p && !aarch64_pstatefield_supported_p (cpu_variant, o))
4870 as_bad (_("selected processor does not support PSTATE field "
4871 "name '%s'"), buf);
f7cb161e 4872 if (!pstatefield_p
38cf07a6 4873 && !aarch64_sys_ins_reg_supported_p (cpu_variant, o->name,
63445353 4874 o->flags, &o->features))
72ca8fad
MW
4875 as_bad (_("selected processor does not support system register "
4876 "name '%s'"), buf);
9af8f671
VDN
4877 if (sysreg128_p && !aarch64_sys_reg_128bit_p (o->flags))
4878 as_bad (_("128-bit-wide accsess not allowed on selected system"
4879 " register '%s'"), buf);
f7cb161e 4880 if (aarch64_sys_reg_deprecated_p (o->flags))
49eec193 4881 as_warn (_("system register name '%s' is deprecated and may be "
72ca8fad 4882 "removed in a future release"), buf);
49eec193 4883 value = o->value;
561a72d4
TC
4884 if (flags)
4885 *flags = o->flags;
49eec193 4886 }
a06ea964
NC
4887
4888 *str = q;
4889 return value;
4890}
4891
4892/* Parse a system reg for ic/dc/at/tlbi instructions. Returns the table entry
4893 for the option, or NULL. */
4894
4895static const aarch64_sys_ins_reg *
0796bfa4 4896parse_sys_ins_reg (char **str, htab_t sys_ins_regs, bool sysreg128_p)
a06ea964
NC
4897{
4898 char *p, *q;
fa63795f 4899 char buf[AARCH64_MAX_SYSREG_NAME_LEN];
a06ea964
NC
4900 const aarch64_sys_ins_reg *o;
4901
4902 p = buf;
4903 for (q = *str; ISALNUM (*q) || *q == '_'; q++)
fa63795f 4904 if (p < buf + (sizeof (buf) - 1))
a06ea964
NC
4905 *p++ = TOLOWER (*q);
4906 *p = '\0';
4907
fa63795f
AC
4908 /* If the name is longer than AARCH64_MAX_SYSREG_NAME_LEN then it cannot be a
4909 valid system register. This is enforced by construction of the hash
4910 table. */
4911 if (p - buf != q - *str)
4912 return NULL;
4913
629310ab 4914 o = str_hash_find (sys_ins_regs, buf);
0796bfa4 4915 if (!o || (sysreg128_p && !aarch64_sys_reg_128bit_p (o->flags)))
a06ea964
NC
4916 return NULL;
4917
38cf07a6 4918 if (!aarch64_sys_ins_reg_supported_p (cpu_variant,
63445353 4919 o->name, o->flags, &o->features))
d6bf7ce6
MW
4920 as_bad (_("selected processor does not support system register "
4921 "name '%s'"), buf);
f7cb161e
PW
4922 if (aarch64_sys_reg_deprecated_p (o->flags))
4923 as_warn (_("system register name '%s' is deprecated and may be "
4924 "removed in a future release"), buf);
d6bf7ce6 4925
a06ea964
NC
4926 *str = q;
4927 return o;
4928}
4929\f
4930#define po_char_or_fail(chr) do { \
4931 if (! skip_past_char (&str, chr)) \
4932 goto failure; \
4933} while (0)
4934
4935#define po_reg_or_fail(regtype) do { \
074c89d2
RS
4936 reg = aarch64_reg_parse (&str, regtype, NULL); \
4937 if (!reg) \
e426521e 4938 goto failure; \
a06ea964
NC
4939 } while (0)
4940
30ba1d7e
RS
4941#define po_int_fp_reg_or_fail(reg_type) do { \
4942 reg = parse_reg (&str); \
e1b988bb 4943 if (!reg || !aarch64_check_reg_type (reg, reg_type)) \
a06ea964 4944 { \
e426521e 4945 set_expected_reg_error (reg_type, reg, 0); \
a06ea964
NC
4946 goto failure; \
4947 } \
e1b988bb 4948 info->reg.regno = reg->number; \
30ba1d7e 4949 info->qualifier = inherent_reg_qualifier (reg); \
a06ea964
NC
4950 } while (0)
4951
4952#define po_imm_nc_or_fail() do { \
1799c0d0 4953 if (! parse_constant_immediate (&str, &val, imm_reg_type)) \
a06ea964
NC
4954 goto failure; \
4955 } while (0)
4956
4957#define po_imm_or_fail(min, max) do { \
1799c0d0 4958 if (! parse_constant_immediate (&str, &val, imm_reg_type)) \
a06ea964
NC
4959 goto failure; \
4960 if (val < min || val > max) \
4961 { \
4962 set_fatal_syntax_error (_("immediate value out of range "\
4963#min " to "#max)); \
4964 goto failure; \
4965 } \
4966 } while (0)
4967
245d2e3f
RS
4968#define po_enum_or_fail(array) do { \
4969 if (!parse_enum_string (&str, &val, array, \
4970 ARRAY_SIZE (array), imm_reg_type)) \
4971 goto failure; \
4972 } while (0)
4973
99e01a66
RS
4974#define po_strict_enum_or_fail(array) do { \
4975 if (!parse_enum_string (&str, &val, array, \
4976 ARRAY_SIZE (array), REG_TYPE_MAX)) \
4977 goto failure; \
4978 } while (0)
4979
a06ea964
NC
4980#define po_misc_or_fail(expr) do { \
4981 if (!expr) \
4982 goto failure; \
4983 } while (0)
4984\f
38c5aa5e
RS
4985/* A primitive log calculator. */
4986
4987static inline unsigned int
4988get_log2 (unsigned int n)
4989{
4990 unsigned int count = 0;
4991 while (n > 1)
4992 {
4993 n >>= 1;
4994 count += 1;
4995 }
4996 return count;
4997}
4998
a06ea964
NC
4999/* encode the 12-bit imm field of Add/sub immediate */
5000static inline uint32_t
5001encode_addsub_imm (uint32_t imm)
5002{
5003 return imm << 10;
5004}
5005
5006/* encode the shift amount field of Add/sub immediate */
5007static inline uint32_t
5008encode_addsub_imm_shift_amount (uint32_t cnt)
5009{
5010 return cnt << 22;
5011}
5012
5013
5014/* encode the imm field of Adr instruction */
5015static inline uint32_t
5016encode_adr_imm (uint32_t imm)
5017{
5018 return (((imm & 0x3) << 29) /* [1:0] -> [30:29] */
5019 | ((imm & (0x7ffff << 2)) << 3)); /* [20:2] -> [23:5] */
5020}
5021
5022/* encode the immediate field of Move wide immediate */
5023static inline uint32_t
5024encode_movw_imm (uint32_t imm)
5025{
5026 return imm << 5;
5027}
5028
5029/* encode the 26-bit offset of unconditional branch */
5030static inline uint32_t
5031encode_branch_ofs_26 (uint32_t ofs)
5032{
5033 return ofs & ((1 << 26) - 1);
5034}
5035
5036/* encode the 19-bit offset of conditional branch and compare & branch */
5037static inline uint32_t
5038encode_cond_branch_ofs_19 (uint32_t ofs)
5039{
5040 return (ofs & ((1 << 19) - 1)) << 5;
5041}
5042
5043/* encode the 19-bit offset of ld literal */
5044static inline uint32_t
5045encode_ld_lit_ofs_19 (uint32_t ofs)
5046{
5047 return (ofs & ((1 << 19) - 1)) << 5;
5048}
5049
5050/* Encode the 14-bit offset of test & branch. */
5051static inline uint32_t
5052encode_tst_branch_ofs_14 (uint32_t ofs)
5053{
5054 return (ofs & ((1 << 14) - 1)) << 5;
5055}
5056
5057/* Encode the 16-bit imm field of svc/hvc/smc. */
5058static inline uint32_t
5059encode_svc_imm (uint32_t imm)
5060{
5061 return imm << 5;
5062}
5063
5064/* Reencode add(s) to sub(s), or sub(s) to add(s). */
5065static inline uint32_t
5066reencode_addsub_switch_add_sub (uint32_t opcode)
5067{
5068 return opcode ^ (1 << 30);
5069}
5070
5071static inline uint32_t
5072reencode_movzn_to_movz (uint32_t opcode)
5073{
5074 return opcode | (1 << 30);
5075}
5076
5077static inline uint32_t
5078reencode_movzn_to_movn (uint32_t opcode)
5079{
5080 return opcode & ~(1 << 30);
5081}
5082
5083/* Overall per-instruction processing. */
5084
5085/* We need to be able to fix up arbitrary expressions in some statements.
5086 This is so that we can handle symbols that are an arbitrary distance from
5087 the pc. The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
5088 which returns part of an address in a form which will be valid for
5089 a data instruction. We do this by pushing the expression into a symbol
5090 in the expr_section, and creating a fix for that. */
5091
5092static fixS *
5093fix_new_aarch64 (fragS * frag,
5094 int where,
f7cb161e
PW
5095 short int size,
5096 expressionS * exp,
5097 int pc_rel,
5098 int reloc)
a06ea964
NC
5099{
5100 fixS *new_fix;
5101
5102 switch (exp->X_op)
5103 {
5104 case O_constant:
5105 case O_symbol:
5106 case O_add:
5107 case O_subtract:
5108 new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
5109 break;
5110
5111 default:
5112 new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
5113 pc_rel, reloc);
5114 break;
5115 }
5116 return new_fix;
5117}
5118\f
5119/* Diagnostics on operands errors. */
5120
a52e6fd3
YZ
5121/* By default, output verbose error message.
5122 Disable the verbose error message by -mno-verbose-error. */
5123static int verbose_error_p = 1;
a06ea964
NC
5124
5125#ifdef DEBUG_AARCH64
5126/* N.B. this is only for the purpose of debugging. */
5127const char* operand_mismatch_kind_names[] =
5128{
5129 "AARCH64_OPDE_NIL",
5130 "AARCH64_OPDE_RECOVERABLE",
63eff947
RS
5131 "AARCH64_OPDE_A_SHOULD_FOLLOW_B",
5132 "AARCH64_OPDE_EXPECTED_A_AFTER_B",
a06ea964
NC
5133 "AARCH64_OPDE_SYNTAX_ERROR",
5134 "AARCH64_OPDE_FATAL_SYNTAX_ERROR",
5135 "AARCH64_OPDE_INVALID_VARIANT",
e2dc4040 5136 "AARCH64_OPDE_INVALID_VG_SIZE",
f5b57fea
RS
5137 "AARCH64_OPDE_REG_LIST_LENGTH",
5138 "AARCH64_OPDE_REG_LIST_STRIDE",
c1817dc2
RS
5139 "AARCH64_OPDE_UNTIED_IMMS",
5140 "AARCH64_OPDE_UNTIED_OPERAND",
a06ea964
NC
5141 "AARCH64_OPDE_OUT_OF_RANGE",
5142 "AARCH64_OPDE_UNALIGNED",
a06ea964 5143 "AARCH64_OPDE_OTHER_ERROR",
859f51df 5144 "AARCH64_OPDE_INVALID_REGNO",
a06ea964
NC
5145};
5146#endif /* DEBUG_AARCH64 */
5147
5148/* Return TRUE if LHS is of higher severity than RHS, otherwise return FALSE.
5149
5150 When multiple errors of different kinds are found in the same assembly
5151 line, only the error of the highest severity will be picked up for
5152 issuing the diagnostics. */
5153
5b7c81bd 5154static inline bool
a06ea964
NC
5155operand_error_higher_severity_p (enum aarch64_operand_error_kind lhs,
5156 enum aarch64_operand_error_kind rhs)
5157{
5158 gas_assert (AARCH64_OPDE_RECOVERABLE > AARCH64_OPDE_NIL);
63eff947
RS
5159 gas_assert (AARCH64_OPDE_A_SHOULD_FOLLOW_B > AARCH64_OPDE_RECOVERABLE);
5160 gas_assert (AARCH64_OPDE_EXPECTED_A_AFTER_B > AARCH64_OPDE_RECOVERABLE);
5161 gas_assert (AARCH64_OPDE_SYNTAX_ERROR > AARCH64_OPDE_A_SHOULD_FOLLOW_B);
5162 gas_assert (AARCH64_OPDE_SYNTAX_ERROR > AARCH64_OPDE_EXPECTED_A_AFTER_B);
a06ea964
NC
5163 gas_assert (AARCH64_OPDE_FATAL_SYNTAX_ERROR > AARCH64_OPDE_SYNTAX_ERROR);
5164 gas_assert (AARCH64_OPDE_INVALID_VARIANT > AARCH64_OPDE_FATAL_SYNTAX_ERROR);
e2dc4040
RS
5165 gas_assert (AARCH64_OPDE_INVALID_VG_SIZE > AARCH64_OPDE_INVALID_VARIANT);
5166 gas_assert (AARCH64_OPDE_REG_LIST_LENGTH > AARCH64_OPDE_INVALID_VG_SIZE);
f5b57fea
RS
5167 gas_assert (AARCH64_OPDE_REG_LIST_STRIDE > AARCH64_OPDE_REG_LIST_LENGTH);
5168 gas_assert (AARCH64_OPDE_OUT_OF_RANGE > AARCH64_OPDE_REG_LIST_STRIDE);
a06ea964 5169 gas_assert (AARCH64_OPDE_UNALIGNED > AARCH64_OPDE_OUT_OF_RANGE);
f5b57fea 5170 gas_assert (AARCH64_OPDE_OTHER_ERROR > AARCH64_OPDE_REG_LIST_STRIDE);
859f51df 5171 gas_assert (AARCH64_OPDE_INVALID_REGNO > AARCH64_OPDE_OTHER_ERROR);
a06ea964
NC
5172 return lhs > rhs;
5173}
5174
5175/* Helper routine to get the mnemonic name from the assembly instruction
5176 line; should only be called for the diagnosis purpose, as there is
5177 string copy operation involved, which may affect the runtime
5178 performance if used in elsewhere. */
5179
5180static const char*
5181get_mnemonic_name (const char *str)
5182{
5183 static char mnemonic[32];
5184 char *ptr;
5185
5186 /* Get the first 15 bytes and assume that the full name is included. */
5187 strncpy (mnemonic, str, 31);
5188 mnemonic[31] = '\0';
5189
5190 /* Scan up to the end of the mnemonic, which must end in white space,
5191 '.', or end of string. */
5192 for (ptr = mnemonic; is_part_of_name(*ptr); ++ptr)
5193 ;
5194
5195 *ptr = '\0';
5196
5197 /* Append '...' to the truncated long name. */
5198 if (ptr - mnemonic == 31)
5199 mnemonic[28] = mnemonic[29] = mnemonic[30] = '.';
5200
5201 return mnemonic;
5202}
5203
5204static void
5205reset_aarch64_instruction (aarch64_instruction *instruction)
5206{
5207 memset (instruction, '\0', sizeof (aarch64_instruction));
5208 instruction->reloc.type = BFD_RELOC_UNUSED;
5209}
5210
33eaf5de 5211/* Data structures storing one user error in the assembly code related to
a06ea964
NC
5212 operands. */
5213
5214struct operand_error_record
5215{
5216 const aarch64_opcode *opcode;
5217 aarch64_operand_error detail;
5218 struct operand_error_record *next;
5219};
5220
5221typedef struct operand_error_record operand_error_record;
5222
5223struct operand_errors
5224{
5225 operand_error_record *head;
5226 operand_error_record *tail;
5227};
5228
5229typedef struct operand_errors operand_errors;
5230
5231/* Top-level data structure reporting user errors for the current line of
5232 the assembly code.
5233 The way md_assemble works is that all opcodes sharing the same mnemonic
5234 name are iterated to find a match to the assembly line. In this data
5235 structure, each of the such opcodes will have one operand_error_record
5236 allocated and inserted. In other words, excessive errors related with
5237 a single opcode are disregarded. */
5238operand_errors operand_error_report;
5239
5240/* Free record nodes. */
5241static operand_error_record *free_opnd_error_record_nodes = NULL;
5242
5243/* Initialize the data structure that stores the operand mismatch
5244 information on assembling one line of the assembly code. */
5245static void
5246init_operand_error_report (void)
5247{
5248 if (operand_error_report.head != NULL)
5249 {
5250 gas_assert (operand_error_report.tail != NULL);
5251 operand_error_report.tail->next = free_opnd_error_record_nodes;
5252 free_opnd_error_record_nodes = operand_error_report.head;
5253 operand_error_report.head = NULL;
5254 operand_error_report.tail = NULL;
5255 return;
5256 }
5257 gas_assert (operand_error_report.tail == NULL);
5258}
5259
5260/* Return TRUE if some operand error has been recorded during the
5261 parsing of the current assembly line using the opcode *OPCODE;
5262 otherwise return FALSE. */
5b7c81bd 5263static inline bool
a06ea964
NC
5264opcode_has_operand_error_p (const aarch64_opcode *opcode)
5265{
5266 operand_error_record *record = operand_error_report.head;
5267 return record && record->opcode == opcode;
5268}
5269
5270/* Add the error record *NEW_RECORD to operand_error_report. The record's
5271 OPCODE field is initialized with OPCODE.
5272 N.B. only one record for each opcode, i.e. the maximum of one error is
5273 recorded for each instruction template. */
5274
5275static void
5276add_operand_error_record (const operand_error_record* new_record)
5277{
5278 const aarch64_opcode *opcode = new_record->opcode;
5279 operand_error_record* record = operand_error_report.head;
5280
5281 /* The record may have been created for this opcode. If not, we need
5282 to prepare one. */
5283 if (! opcode_has_operand_error_p (opcode))
5284 {
5285 /* Get one empty record. */
5286 if (free_opnd_error_record_nodes == NULL)
5287 {
325801bd 5288 record = XNEW (operand_error_record);
a06ea964
NC
5289 }
5290 else
5291 {
5292 record = free_opnd_error_record_nodes;
5293 free_opnd_error_record_nodes = record->next;
5294 }
5295 record->opcode = opcode;
5296 /* Insert at the head. */
5297 record->next = operand_error_report.head;
5298 operand_error_report.head = record;
5299 if (operand_error_report.tail == NULL)
5300 operand_error_report.tail = record;
5301 }
5302 else if (record->detail.kind != AARCH64_OPDE_NIL
5303 && record->detail.index <= new_record->detail.index
5304 && operand_error_higher_severity_p (record->detail.kind,
5305 new_record->detail.kind))
5306 {
5307 /* In the case of multiple errors found on operands related with a
5308 single opcode, only record the error of the leftmost operand and
5309 only if the error is of higher severity. */
5310 DEBUG_TRACE ("error %s on operand %d not added to the report due to"
5311 " the existing error %s on operand %d",
5312 operand_mismatch_kind_names[new_record->detail.kind],
5313 new_record->detail.index,
5314 operand_mismatch_kind_names[record->detail.kind],
5315 record->detail.index);
5316 return;
5317 }
5318
5319 record->detail = new_record->detail;
5320}
5321
5322static inline void
5323record_operand_error_info (const aarch64_opcode *opcode,
5324 aarch64_operand_error *error_info)
5325{
5326 operand_error_record record;
5327 record.opcode = opcode;
5328 record.detail = *error_info;
5329 add_operand_error_record (&record);
5330}
5331
5332/* Record an error of kind KIND and, if ERROR is not NULL, of the detailed
5333 error message *ERROR, for operand IDX (count from 0). */
5334
5335static void
5336record_operand_error (const aarch64_opcode *opcode, int idx,
5337 enum aarch64_operand_error_kind kind,
5338 const char* error)
5339{
5340 aarch64_operand_error info;
5341 memset(&info, 0, sizeof (info));
5342 info.index = idx;
5343 info.kind = kind;
5344 info.error = error;
5b7c81bd 5345 info.non_fatal = false;
a06ea964
NC
5346 record_operand_error_info (opcode, &info);
5347}
5348
5349static void
5350record_operand_error_with_data (const aarch64_opcode *opcode, int idx,
5351 enum aarch64_operand_error_kind kind,
5352 const char* error, const int *extra_data)
5353{
5354 aarch64_operand_error info;
5355 info.index = idx;
5356 info.kind = kind;
5357 info.error = error;
63eff947
RS
5358 info.data[0].i = extra_data[0];
5359 info.data[1].i = extra_data[1];
5360 info.data[2].i = extra_data[2];
5b7c81bd 5361 info.non_fatal = false;
a06ea964
NC
5362 record_operand_error_info (opcode, &info);
5363}
5364
5365static void
5366record_operand_out_of_range_error (const aarch64_opcode *opcode, int idx,
5367 const char* error, int lower_bound,
5368 int upper_bound)
5369{
5370 int data[3] = {lower_bound, upper_bound, 0};
5371 record_operand_error_with_data (opcode, idx, AARCH64_OPDE_OUT_OF_RANGE,
5372 error, data);
5373}
5374
5375/* Remove the operand error record for *OPCODE. */
5376static void ATTRIBUTE_UNUSED
5377remove_operand_error_record (const aarch64_opcode *opcode)
5378{
5379 if (opcode_has_operand_error_p (opcode))
5380 {
5381 operand_error_record* record = operand_error_report.head;
5382 gas_assert (record != NULL && operand_error_report.tail != NULL);
5383 operand_error_report.head = record->next;
5384 record->next = free_opnd_error_record_nodes;
5385 free_opnd_error_record_nodes = record;
5386 if (operand_error_report.head == NULL)
5387 {
5388 gas_assert (operand_error_report.tail == record);
5389 operand_error_report.tail = NULL;
5390 }
5391 }
5392}
5393
5394/* Given the instruction in *INSTR, return the index of the best matched
5395 qualifier sequence in the list (an array) headed by QUALIFIERS_LIST.
5396
5397 Return -1 if there is no qualifier sequence; return the first match
5398 if there is multiple matches found. */
5399
5400static int
5401find_best_match (const aarch64_inst *instr,
5402 const aarch64_opnd_qualifier_seq_t *qualifiers_list)
5403{
5404 int i, num_opnds, max_num_matched, idx;
5405
5406 num_opnds = aarch64_num_of_operands (instr->opcode);
5407 if (num_opnds == 0)
5408 {
5409 DEBUG_TRACE ("no operand");
5410 return -1;
5411 }
5412
5413 max_num_matched = 0;
4989adac 5414 idx = 0;
a06ea964
NC
5415
5416 /* For each pattern. */
5417 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
5418 {
5419 int j, num_matched;
5420 const aarch64_opnd_qualifier_t *qualifiers = *qualifiers_list;
5421
5422 /* Most opcodes has much fewer patterns in the list. */
535b785f 5423 if (empty_qualifier_sequence_p (qualifiers))
a06ea964
NC
5424 {
5425 DEBUG_TRACE_IF (i == 0, "empty list of qualifier sequence");
a06ea964
NC
5426 break;
5427 }
5428
5429 for (j = 0, num_matched = 0; j < num_opnds; ++j, ++qualifiers)
5430 if (*qualifiers == instr->operands[j].qualifier)
5431 ++num_matched;
5432
5433 if (num_matched > max_num_matched)
5434 {
5435 max_num_matched = num_matched;
5436 idx = i;
5437 }
5438 }
5439
5440 DEBUG_TRACE ("return with %d", idx);
5441 return idx;
5442}
5443
33eaf5de 5444/* Assign qualifiers in the qualifier sequence (headed by QUALIFIERS) to the
a06ea964
NC
5445 corresponding operands in *INSTR. */
5446
5447static inline void
5448assign_qualifier_sequence (aarch64_inst *instr,
5449 const aarch64_opnd_qualifier_t *qualifiers)
5450{
5451 int i = 0;
5452 int num_opnds = aarch64_num_of_operands (instr->opcode);
5453 gas_assert (num_opnds);
5454 for (i = 0; i < num_opnds; ++i, ++qualifiers)
5455 instr->operands[i].qualifier = *qualifiers;
5456}
5457
76a4c1e0
AB
5458/* Callback used by aarch64_print_operand to apply STYLE to the
5459 disassembler output created from FMT and ARGS. The STYLER object holds
5460 any required state. Must return a pointer to a string (created from FMT
5461 and ARGS) that will continue to be valid until the complete disassembled
5462 instruction has been printed.
5463
5464 We don't currently add any styling to the output of the disassembler as
5465 used within assembler error messages, and so STYLE is ignored here. A
5466 new string is allocated on the obstack help within STYLER and returned
5467 to the caller. */
5468
5469static const char *aarch64_apply_style
5470 (struct aarch64_styler *styler,
5471 enum disassembler_style style ATTRIBUTE_UNUSED,
5472 const char *fmt, va_list args)
5473{
5474 int res;
5475 char *ptr;
5476 struct obstack *stack = (struct obstack *) styler->state;
5477 va_list ap;
5478
5479 /* Calculate the required space. */
5480 va_copy (ap, args);
5481 res = vsnprintf (NULL, 0, fmt, ap);
5482 va_end (ap);
5483 gas_assert (res >= 0);
5484
5485 /* Allocate space on the obstack and format the result. */
5486 ptr = (char *) obstack_alloc (stack, res + 1);
5487 res = vsnprintf (ptr, (res + 1), fmt, args);
5488 gas_assert (res >= 0);
5489
5490 return ptr;
5491}
5492
a06ea964
NC
5493/* Print operands for the diagnosis purpose. */
5494
5495static void
5496print_operands (char *buf, const aarch64_opcode *opcode,
5497 const aarch64_opnd_info *opnds)
5498{
5499 int i;
76a4c1e0
AB
5500 struct aarch64_styler styler;
5501 struct obstack content;
5502 obstack_init (&content);
5503
5504 styler.apply_style = aarch64_apply_style;
5505 styler.state = (void *) &content;
a06ea964
NC
5506
5507 for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
5508 {
08d3b0cc 5509 char str[128];
6837a663 5510 char cmt[128];
a06ea964
NC
5511
5512 /* We regard the opcode operand info more, however we also look into
5513 the inst->operands to support the disassembling of the optional
5514 operand.
5515 The two operand code should be the same in all cases, apart from
5516 when the operand can be optional. */
5517 if (opcode->operands[i] == AARCH64_OPND_NIL
5518 || opnds[i].type == AARCH64_OPND_NIL)
5519 break;
5520
5521 /* Generate the operand string in STR. */
7d02540a 5522 aarch64_print_operand (str, sizeof (str), 0, opcode, opnds, i, NULL, NULL,
76a4c1e0 5523 NULL, cmt, sizeof (cmt), cpu_variant, &styler);
a06ea964
NC
5524
5525 /* Delimiter. */
5526 if (str[0] != '\0')
ad43e107 5527 strcat (buf, i == 0 ? " " : ", ");
a06ea964
NC
5528
5529 /* Append the operand string. */
5530 strcat (buf, str);
6837a663
AB
5531
5532 /* Append a comment. This works because only the last operand ever
5533 adds a comment. If that ever changes then we'll need to be
5534 smarter here. */
5535 if (cmt[0] != '\0')
5536 {
5537 strcat (buf, "\t// ");
5538 strcat (buf, cmt);
5539 }
a06ea964 5540 }
76a4c1e0
AB
5541
5542 obstack_free (&content, NULL);
a06ea964
NC
5543}
5544
5545/* Send to stderr a string as information. */
5546
5547static void
5548output_info (const char *format, ...)
5549{
3b4dbbbf 5550 const char *file;
a06ea964
NC
5551 unsigned int line;
5552 va_list args;
5553
3b4dbbbf 5554 file = as_where (&line);
a06ea964
NC
5555 if (file)
5556 {
5557 if (line != 0)
5558 fprintf (stderr, "%s:%u: ", file, line);
5559 else
5560 fprintf (stderr, "%s: ", file);
5561 }
5562 fprintf (stderr, _("Info: "));
5563 va_start (args, format);
5564 vfprintf (stderr, format, args);
5565 va_end (args);
5566 (void) putc ('\n', stderr);
5567}
5568
e426521e
RS
5569/* See if the AARCH64_OPDE_SYNTAX_ERROR error described by DETAIL
5570 relates to registers or register lists. If so, return a string that
5571 reports the error against "operand %d", otherwise return null. */
5572
5573static const char *
5574get_reg_error_message (const aarch64_operand_error *detail)
5575{
5576 /* Handle the case where we found a register that was expected
5577 to be in a register list outside of a register list. */
5578 if ((detail->data[1].i & detail->data[2].i) != 0
5579 && (detail->data[1].i & SEF_IN_REGLIST) == 0)
5580 return _("missing braces at operand %d");
5581
5582 /* If some opcodes expected a register, and we found a register,
5583 complain about the difference. */
5584 if (detail->data[2].i)
5585 {
5586 unsigned int expected = (detail->data[1].i & SEF_IN_REGLIST
5587 ? detail->data[1].i & ~SEF_IN_REGLIST
5588 : detail->data[0].i & ~SEF_DEFAULT_ERROR);
5589 const char *msg = get_reg_expected_msg (expected, detail->data[2].i);
5590 if (!msg)
5591 msg = N_("unexpected register type at operand %d");
5592 return msg;
5593 }
5594
5595 /* Handle the case where we got to the point of trying to parse a
5596 register within a register list, but didn't find a known register. */
5597 if (detail->data[1].i & SEF_IN_REGLIST)
5598 {
5599 unsigned int expected = detail->data[1].i & ~SEF_IN_REGLIST;
5600 const char *msg = get_reg_expected_msg (expected, 0);
5601 if (!msg)
5602 msg = _("invalid register list at operand %d");
5603 return msg;
5604 }
5605
5606 /* Punt if register-related problems weren't the only errors. */
5607 if (detail->data[0].i & SEF_DEFAULT_ERROR)
5608 return NULL;
5609
5610 /* Handle the case where the only acceptable things are registers. */
5611 if (detail->data[1].i == 0)
5612 {
5613 const char *msg = get_reg_expected_msg (detail->data[0].i, 0);
5614 if (!msg)
5615 msg = _("expected a register at operand %d");
5616 return msg;
5617 }
5618
5619 /* Handle the case where the only acceptable things are register lists,
5620 and there was no opening '{'. */
5621 if (detail->data[0].i == 0)
5622 return _("expected '{' at operand %d");
5623
5624 return _("expected a register or register list at operand %d");
5625}
5626
a06ea964
NC
5627/* Output one operand error record. */
5628
5629static void
5630output_operand_error_record (const operand_error_record *record, char *str)
5631{
28f013d5
JB
5632 const aarch64_operand_error *detail = &record->detail;
5633 int idx = detail->index;
a06ea964 5634 const aarch64_opcode *opcode = record->opcode;
28f013d5 5635 enum aarch64_opnd opd_code = (idx >= 0 ? opcode->operands[idx]
a06ea964 5636 : AARCH64_OPND_NIL);
a06ea964 5637
7d02540a
TC
5638 typedef void (*handler_t)(const char *format, ...);
5639 handler_t handler = detail->non_fatal ? as_warn : as_bad;
e426521e 5640 const char *msg = detail->error;
7d02540a 5641
a06ea964
NC
5642 switch (detail->kind)
5643 {
5644 case AARCH64_OPDE_NIL:
5645 gas_assert (0);
5646 break;
63eff947
RS
5647
5648 case AARCH64_OPDE_A_SHOULD_FOLLOW_B:
5649 handler (_("this `%s' should have an immediately preceding `%s'"
5650 " -- `%s'"),
5651 detail->data[0].s, detail->data[1].s, str);
5652 break;
5653
5654 case AARCH64_OPDE_EXPECTED_A_AFTER_B:
5655 handler (_("the preceding `%s' should be followed by `%s` rather"
5656 " than `%s` -- `%s'"),
5657 detail->data[1].s, detail->data[0].s, opcode->name, str);
5658 break;
5659
a06ea964 5660 case AARCH64_OPDE_SYNTAX_ERROR:
e426521e
RS
5661 if (!msg && idx >= 0)
5662 {
5663 msg = get_reg_error_message (detail);
5664 if (msg)
5665 {
5666 char *full_msg = xasprintf (msg, idx + 1);
5667 handler (_("%s -- `%s'"), full_msg, str);
5668 free (full_msg);
5669 break;
5670 }
5671 }
5672 /* Fall through. */
5673
a06ea964
NC
5674 case AARCH64_OPDE_RECOVERABLE:
5675 case AARCH64_OPDE_FATAL_SYNTAX_ERROR:
5676 case AARCH64_OPDE_OTHER_ERROR:
a06ea964
NC
5677 /* Use the prepared error message if there is, otherwise use the
5678 operand description string to describe the error. */
e426521e 5679 if (msg != NULL)
a06ea964 5680 {
28f013d5 5681 if (idx < 0)
e426521e 5682 handler (_("%s -- `%s'"), msg, str);
a06ea964 5683 else
7d02540a 5684 handler (_("%s at operand %d -- `%s'"),
e426521e 5685 msg, idx + 1, str);
a06ea964
NC
5686 }
5687 else
28f013d5
JB
5688 {
5689 gas_assert (idx >= 0);
7d02540a
TC
5690 handler (_("operand %d must be %s -- `%s'"), idx + 1,
5691 aarch64_get_operand_desc (opd_code), str);
28f013d5 5692 }
a06ea964
NC
5693 break;
5694
5695 case AARCH64_OPDE_INVALID_VARIANT:
7d02540a 5696 handler (_("operand mismatch -- `%s'"), str);
a06ea964
NC
5697 if (verbose_error_p)
5698 {
5699 /* We will try to correct the erroneous instruction and also provide
5700 more information e.g. all other valid variants.
5701
5702 The string representation of the corrected instruction and other
5703 valid variants are generated by
5704
5705 1) obtaining the intermediate representation of the erroneous
5706 instruction;
5707 2) manipulating the IR, e.g. replacing the operand qualifier;
5708 3) printing out the instruction by calling the printer functions
5709 shared with the disassembler.
5710
5711 The limitation of this method is that the exact input assembly
5712 line cannot be accurately reproduced in some cases, for example an
5713 optional operand present in the actual assembly line will be
5714 omitted in the output; likewise for the optional syntax rules,
5715 e.g. the # before the immediate. Another limitation is that the
5716 assembly symbols and relocation operations in the assembly line
5717 currently cannot be printed out in the error report. Last but not
5718 least, when there is other error(s) co-exist with this error, the
5719 'corrected' instruction may be still incorrect, e.g. given
5720 'ldnp h0,h1,[x0,#6]!'
5721 this diagnosis will provide the version:
5722 'ldnp s0,s1,[x0,#6]!'
5723 which is still not right. */
5724 size_t len = strlen (get_mnemonic_name (str));
5725 int i, qlf_idx;
5b7c81bd 5726 bool result;
08d3b0cc 5727 char buf[2048];
a06ea964
NC
5728 aarch64_inst *inst_base = &inst.base;
5729 const aarch64_opnd_qualifier_seq_t *qualifiers_list;
5730
5731 /* Init inst. */
5732 reset_aarch64_instruction (&inst);
5733 inst_base->opcode = opcode;
5734
5735 /* Reset the error report so that there is no side effect on the
5736 following operand parsing. */
5737 init_operand_error_report ();
5738
5739 /* Fill inst. */
5740 result = parse_operands (str + len, opcode)
5741 && programmer_friendly_fixup (&inst);
5742 gas_assert (result);
5743 result = aarch64_opcode_encode (opcode, inst_base, &inst_base->value,
7e84b55d 5744 NULL, NULL, insn_sequence);
a06ea964
NC
5745 gas_assert (!result);
5746
5747 /* Find the most matched qualifier sequence. */
5748 qlf_idx = find_best_match (inst_base, opcode->qualifiers_list);
5749 gas_assert (qlf_idx > -1);
5750
5751 /* Assign the qualifiers. */
5752 assign_qualifier_sequence (inst_base,
5753 opcode->qualifiers_list[qlf_idx]);
5754
5755 /* Print the hint. */
5756 output_info (_(" did you mean this?"));
08d3b0cc 5757 snprintf (buf, sizeof (buf), "\t%s", get_mnemonic_name (str));
a06ea964
NC
5758 print_operands (buf, opcode, inst_base->operands);
5759 output_info (_(" %s"), buf);
5760
5761 /* Print out other variant(s) if there is any. */
5762 if (qlf_idx != 0 ||
5763 !empty_qualifier_sequence_p (opcode->qualifiers_list[1]))
5764 output_info (_(" other valid variant(s):"));
5765
5766 /* For each pattern. */
5767 qualifiers_list = opcode->qualifiers_list;
5768 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
5769 {
5770 /* Most opcodes has much fewer patterns in the list.
5771 First NIL qualifier indicates the end in the list. */
535b785f 5772 if (empty_qualifier_sequence_p (*qualifiers_list))
a06ea964
NC
5773 break;
5774
5775 if (i != qlf_idx)
5776 {
5777 /* Mnemonics name. */
08d3b0cc 5778 snprintf (buf, sizeof (buf), "\t%s", get_mnemonic_name (str));
a06ea964
NC
5779
5780 /* Assign the qualifiers. */
5781 assign_qualifier_sequence (inst_base, *qualifiers_list);
5782
5783 /* Print instruction. */
5784 print_operands (buf, opcode, inst_base->operands);
5785
5786 output_info (_(" %s"), buf);
5787 }
5788 }
5789 }
5790 break;
5791
01a4d082
PW
5792 case AARCH64_OPDE_UNTIED_IMMS:
5793 handler (_("operand %d must have the same immediate value "
5794 "as operand 1 -- `%s'"),
5795 detail->index + 1, str);
5796 break;
5797
0c608d6b 5798 case AARCH64_OPDE_UNTIED_OPERAND:
7d02540a 5799 handler (_("operand %d must be the same register as operand 1 -- `%s'"),
01a4d082 5800 detail->index + 1, str);
0c608d6b
RS
5801 break;
5802
859f51df
RS
5803 case AARCH64_OPDE_INVALID_REGNO:
5804 handler (_("%s%d-%s%d expected at operand %d -- `%s'"),
5805 detail->data[0].s, detail->data[1].i,
5806 detail->data[0].s, detail->data[2].i, idx + 1, str);
5807 break;
5808
a06ea964 5809 case AARCH64_OPDE_OUT_OF_RANGE:
63eff947 5810 if (detail->data[0].i != detail->data[1].i)
7d02540a 5811 handler (_("%s out of range %d to %d at operand %d -- `%s'"),
e426521e 5812 msg ? msg : _("immediate value"),
63eff947 5813 detail->data[0].i, detail->data[1].i, idx + 1, str);
f5555712 5814 else
7d02540a 5815 handler (_("%s must be %d at operand %d -- `%s'"),
e426521e 5816 msg ? msg : _("immediate value"),
63eff947 5817 detail->data[0].i, idx + 1, str);
a06ea964
NC
5818 break;
5819
e2dc4040
RS
5820 case AARCH64_OPDE_INVALID_VG_SIZE:
5821 if (detail->data[0].i == 0)
5822 handler (_("unexpected vector group size at operand %d -- `%s'"),
5823 idx + 1, str);
5824 else
5825 handler (_("operand %d must have a vector group size of %d -- `%s'"),
5826 idx + 1, detail->data[0].i, str);
5827 break;
5828
f5b57fea 5829 case AARCH64_OPDE_REG_LIST_LENGTH:
38c5aa5e 5830 if (detail->data[0].i == (1 << 1))
8c2216b2 5831 handler (_("expected a single-register list at operand %d -- `%s'"),
7d02540a 5832 idx + 1, str);
38c5aa5e 5833 else if ((detail->data[0].i & -detail->data[0].i) == detail->data[0].i)
8c2216b2 5834 handler (_("expected a list of %d registers at operand %d -- `%s'"),
38c5aa5e 5835 get_log2 (detail->data[0].i), idx + 1, str);
b408ebbf
RS
5836 else if (detail->data[0].i == 0x14)
5837 handler (_("expected a list of %d or %d registers at"
5838 " operand %d -- `%s'"),
5839 2, 4, idx + 1, str);
38c5aa5e
RS
5840 else
5841 handler (_("invalid number of registers in the list"
5842 " at operand %d -- `%s'"), idx + 1, str);
a06ea964
NC
5843 break;
5844
f5b57fea
RS
5845 case AARCH64_OPDE_REG_LIST_STRIDE:
5846 if (detail->data[0].i == (1 << 1))
5847 handler (_("the register list must have a stride of %d"
5848 " at operand %d -- `%s'"), 1, idx + 1, str);
b408ebbf
RS
5849 else if (detail->data[0].i == 0x12 || detail->data[0].i == 0x102)
5850 handler (_("the register list must have a stride of %d or %d"
5851 " at operand %d -- `%s`"), 1,
5852 detail->data[0].i == 0x12 ? 4 : 8, idx + 1, str);
f5b57fea
RS
5853 else
5854 handler (_("invalid register stride at operand %d -- `%s'"),
5855 idx + 1, str);
5856 break;
5857
a06ea964 5858 case AARCH64_OPDE_UNALIGNED:
7d02540a
TC
5859 handler (_("immediate value must be a multiple of "
5860 "%d at operand %d -- `%s'"),
63eff947 5861 detail->data[0].i, idx + 1, str);
a06ea964
NC
5862 break;
5863
5864 default:
5865 gas_assert (0);
5866 break;
5867 }
5868}
5869
1be1148d
RS
5870/* Return true if the presence of error A against an instruction means
5871 that error B should not be reported. This is only used as a first pass,
5872 to pick the kind of error that we should report. */
5873
5874static bool
5875better_error_p (operand_error_record *a, operand_error_record *b)
5876{
5877 /* For errors reported during parsing, prefer errors that relate to
5878 later operands, since that implies that the earlier operands were
5879 syntactically valid.
5880
5881 For example, if we see a register R instead of an immediate in
5882 operand N, we'll report that as a recoverable "immediate operand
5883 required" error. This is because there is often another opcode
5884 entry that accepts a register operand N, and any errors about R
5885 should be reported against the register forms of the instruction.
5886 But if no such register form exists, the recoverable error should
5887 still win over a syntax error against operand N-1.
5888
5889 For these purposes, count an error reported at the end of the
5890 assembly string as equivalent to an error reported against the
5891 final operand. This means that opcode entries that expect more
5892 operands win over "unexpected characters following instruction". */
5893 if (a->detail.kind <= AARCH64_OPDE_FATAL_SYNTAX_ERROR
5894 && b->detail.kind <= AARCH64_OPDE_FATAL_SYNTAX_ERROR)
5895 {
5896 int a_index = (a->detail.index < 0
5897 ? aarch64_num_of_operands (a->opcode) - 1
5898 : a->detail.index);
5899 int b_index = (b->detail.index < 0
5900 ? aarch64_num_of_operands (b->opcode) - 1
5901 : b->detail.index);
5902 if (a_index != b_index)
5903 return a_index > b_index;
5904 }
5905 return operand_error_higher_severity_p (a->detail.kind, b->detail.kind);
5906}
5907
a06ea964
NC
5908/* Process and output the error message about the operand mismatching.
5909
5910 When this function is called, the operand error information had
5911 been collected for an assembly line and there will be multiple
33eaf5de 5912 errors in the case of multiple instruction templates; output the
7d02540a
TC
5913 error message that most closely describes the problem.
5914
5915 The errors to be printed can be filtered on printing all errors
5916 or only non-fatal errors. This distinction has to be made because
5917 the error buffer may already be filled with fatal errors we don't want to
5918 print due to the different instruction templates. */
a06ea964
NC
5919
5920static void
5b7c81bd 5921output_operand_error_report (char *str, bool non_fatal_only)
a06ea964 5922{
a06ea964
NC
5923 enum aarch64_operand_error_kind kind;
5924 operand_error_record *curr;
5925 operand_error_record *head = operand_error_report.head;
1be1148d 5926 operand_error_record *record;
a06ea964
NC
5927
5928 /* No error to report. */
5929 if (head == NULL)
5930 return;
5931
5932 gas_assert (head != NULL && operand_error_report.tail != NULL);
5933
5934 /* Only one error. */
5935 if (head == operand_error_report.tail)
5936 {
7d02540a
TC
5937 /* If the only error is a non-fatal one and we don't want to print it,
5938 just exit. */
5939 if (!non_fatal_only || head->detail.non_fatal)
5940 {
5941 DEBUG_TRACE ("single opcode entry with error kind: %s",
5942 operand_mismatch_kind_names[head->detail.kind]);
5943 output_operand_error_record (head, str);
5944 }
a06ea964
NC
5945 return;
5946 }
5947
5948 /* Find the error kind of the highest severity. */
33eaf5de 5949 DEBUG_TRACE ("multiple opcode entries with error kind");
1be1148d 5950 record = NULL;
a06ea964
NC
5951 for (curr = head; curr != NULL; curr = curr->next)
5952 {
5953 gas_assert (curr->detail.kind != AARCH64_OPDE_NIL);
e426521e
RS
5954 if (curr->detail.kind == AARCH64_OPDE_SYNTAX_ERROR)
5955 {
5956 DEBUG_TRACE ("\t%s [%x, %x, %x]",
5957 operand_mismatch_kind_names[curr->detail.kind],
5958 curr->detail.data[0].i, curr->detail.data[1].i,
5959 curr->detail.data[2].i);
5960 }
f5b57fea
RS
5961 else if (curr->detail.kind == AARCH64_OPDE_REG_LIST_LENGTH
5962 || curr->detail.kind == AARCH64_OPDE_REG_LIST_STRIDE)
38c5aa5e
RS
5963 {
5964 DEBUG_TRACE ("\t%s [%x]",
5965 operand_mismatch_kind_names[curr->detail.kind],
5966 curr->detail.data[0].i);
5967 }
e426521e
RS
5968 else
5969 {
5970 DEBUG_TRACE ("\t%s", operand_mismatch_kind_names[curr->detail.kind]);
5971 }
1be1148d
RS
5972 if ((!non_fatal_only || curr->detail.non_fatal)
5973 && (!record || better_error_p (curr, record)))
5974 record = curr;
a06ea964 5975 }
a68f4cd2 5976
1be1148d 5977 kind = (record ? record->detail.kind : AARCH64_OPDE_NIL);
a68f4cd2 5978 gas_assert (kind != AARCH64_OPDE_NIL || non_fatal_only);
a06ea964
NC
5979
5980 /* Pick up one of errors of KIND to report. */
1be1148d 5981 record = NULL;
a06ea964
NC
5982 for (curr = head; curr != NULL; curr = curr->next)
5983 {
7d02540a
TC
5984 /* If we don't want to print non-fatal errors then don't consider them
5985 at all. */
5986 if (curr->detail.kind != kind
af81c43b 5987 || (non_fatal_only && !curr->detail.non_fatal))
a06ea964
NC
5988 continue;
5989 /* If there are multiple errors, pick up the one with the highest
5990 mismatching operand index. In the case of multiple errors with
5991 the equally highest operand index, pick up the first one or the
5992 first one with non-NULL error message. */
e426521e
RS
5993 if (!record || curr->detail.index > record->detail.index)
5994 record = curr;
5995 else if (curr->detail.index == record->detail.index
5996 && !record->detail.error)
a06ea964 5997 {
e426521e
RS
5998 if (curr->detail.error)
5999 record = curr;
6000 else if (kind == AARCH64_OPDE_SYNTAX_ERROR)
6001 {
6002 record->detail.data[0].i |= curr->detail.data[0].i;
6003 record->detail.data[1].i |= curr->detail.data[1].i;
6004 record->detail.data[2].i |= curr->detail.data[2].i;
6005 DEBUG_TRACE ("\t--> %s [%x, %x, %x]",
6006 operand_mismatch_kind_names[kind],
6007 curr->detail.data[0].i, curr->detail.data[1].i,
6008 curr->detail.data[2].i);
6009 }
f5b57fea
RS
6010 else if (kind == AARCH64_OPDE_REG_LIST_LENGTH
6011 || kind == AARCH64_OPDE_REG_LIST_STRIDE)
38c5aa5e
RS
6012 {
6013 record->detail.data[0].i |= curr->detail.data[0].i;
6014 DEBUG_TRACE ("\t--> %s [%x]",
6015 operand_mismatch_kind_names[kind],
6016 curr->detail.data[0].i);
6017 }
b5b4f665
RS
6018 /* Pick the variant with the cloest match. */
6019 else if (kind == AARCH64_OPDE_INVALID_VARIANT
6020 && record->detail.data[0].i > curr->detail.data[0].i)
6021 record = curr;
a06ea964
NC
6022 }
6023 }
6024
7d02540a
TC
6025 /* The way errors are collected in the back-end is a bit non-intuitive. But
6026 essentially, because each operand template is tried recursively you may
6027 always have errors collected from the previous tried OPND. These are
6028 usually skipped if there is one successful match. However now with the
6029 non-fatal errors we have to ignore those previously collected hard errors
6030 when we're only interested in printing the non-fatal ones. This condition
6031 prevents us from printing errors that are not appropriate, since we did
6032 match a condition, but it also has warnings that it wants to print. */
6033 if (non_fatal_only && !record)
6034 return;
6035
e426521e 6036 gas_assert (record);
a06ea964 6037 DEBUG_TRACE ("Pick up error kind %s to report",
e426521e 6038 operand_mismatch_kind_names[kind]);
a06ea964
NC
6039
6040 /* Output. */
6041 output_operand_error_record (record, str);
6042}
6043\f
6044/* Write an AARCH64 instruction to buf - always little-endian. */
6045static void
6046put_aarch64_insn (char *buf, uint32_t insn)
6047{
6048 unsigned char *where = (unsigned char *) buf;
6049 where[0] = insn;
6050 where[1] = insn >> 8;
6051 where[2] = insn >> 16;
6052 where[3] = insn >> 24;
6053}
6054
6055static uint32_t
6056get_aarch64_insn (char *buf)
6057{
6058 unsigned char *where = (unsigned char *) buf;
6059 uint32_t result;
4f7cc141
AM
6060 result = ((where[0] | (where[1] << 8) | (where[2] << 16)
6061 | ((uint32_t) where[3] << 24)));
a06ea964
NC
6062 return result;
6063}
6064
6065static void
6066output_inst (struct aarch64_inst *new_inst)
6067{
6068 char *to = NULL;
6069
6070 to = frag_more (INSN_SIZE);
6071
6072 frag_now->tc_frag_data.recorded = 1;
6073
6074 put_aarch64_insn (to, inst.base.value);
6075
6076 if (inst.reloc.type != BFD_RELOC_UNUSED)
6077 {
6078 fixS *fixp = fix_new_aarch64 (frag_now, to - frag_now->fr_literal,
6079 INSN_SIZE, &inst.reloc.exp,
6080 inst.reloc.pc_rel,
6081 inst.reloc.type);
6082 DEBUG_TRACE ("Prepared relocation fix up");
6083 /* Don't check the addend value against the instruction size,
6084 that's the job of our code in md_apply_fix(). */
6085 fixp->fx_no_overflow = 1;
6086 if (new_inst != NULL)
6087 fixp->tc_fix_data.inst = new_inst;
6088 if (aarch64_gas_internal_fixup_p ())
6089 {
6090 gas_assert (inst.reloc.opnd != AARCH64_OPND_NIL);
6091 fixp->tc_fix_data.opnd = inst.reloc.opnd;
6092 fixp->fx_addnumber = inst.reloc.flags;
6093 }
6094 }
6095
6096 dwarf2_emit_insn (INSN_SIZE);
6097}
6098
6099/* Link together opcodes of the same name. */
6100
6101struct templates
6102{
6c2ede01 6103 const aarch64_opcode *opcode;
a06ea964
NC
6104 struct templates *next;
6105};
6106
6107typedef struct templates templates;
6108
6109static templates *
6110lookup_mnemonic (const char *start, int len)
6111{
6112 templates *templ = NULL;
6113
629310ab 6114 templ = str_hash_find_n (aarch64_ops_hsh, start, len);
a06ea964
NC
6115 return templ;
6116}
6117
6118/* Subroutine of md_assemble, responsible for looking up the primary
2dd3146b
RS
6119 opcode from the mnemonic the user wrote. BASE points to the beginning
6120 of the mnemonic, DOT points to the first '.' within the mnemonic
6121 (if any) and END points to the end of the mnemonic. */
a06ea964
NC
6122
6123static templates *
2dd3146b 6124opcode_lookup (char *base, char *dot, char *end)
a06ea964 6125{
a06ea964
NC
6126 const aarch64_cond *cond;
6127 char condname[16];
6128 int len;
6129
2dd3146b 6130 if (dot == end)
a06ea964
NC
6131 return 0;
6132
6133 inst.cond = COND_ALWAYS;
6134
6135 /* Handle a possible condition. */
bb7eff52 6136 if (dot)
a06ea964 6137 {
629310ab 6138 cond = str_hash_find_n (aarch64_cond_hsh, dot + 1, end - dot - 1);
2dd3146b
RS
6139 if (!cond)
6140 return 0;
6141 inst.cond = cond->value;
bb7eff52 6142 len = dot - base;
a06ea964
NC
6143 }
6144 else
2dd3146b 6145 len = end - base;
a06ea964
NC
6146
6147 if (inst.cond == COND_ALWAYS)
6148 {
6149 /* Look for unaffixed mnemonic. */
6150 return lookup_mnemonic (base, len);
6151 }
6152 else if (len <= 13)
6153 {
6154 /* append ".c" to mnemonic if conditional */
6155 memcpy (condname, base, len);
6156 memcpy (condname + len, ".c", 2);
6157 base = condname;
6158 len += 2;
6159 return lookup_mnemonic (base, len);
6160 }
6161
6162 return NULL;
6163}
6164
a06ea964
NC
6165/* Process an optional operand that is found omitted from the assembly line.
6166 Fill *OPERAND for such an operand of type TYPE. OPCODE points to the
6167 instruction's opcode entry while IDX is the index of this omitted operand.
6168 */
6169
6170static void
6171process_omitted_operand (enum aarch64_opnd type, const aarch64_opcode *opcode,
6172 int idx, aarch64_opnd_info *operand)
6173{
6174 aarch64_insn default_value = get_optional_operand_default_value (opcode);
6175 gas_assert (optional_operand_p (opcode, idx));
6176 gas_assert (!operand->present);
6177
6178 switch (type)
6179 {
6180 case AARCH64_OPND_Rd:
6181 case AARCH64_OPND_Rn:
6182 case AARCH64_OPND_Rm:
6183 case AARCH64_OPND_Rt:
6184 case AARCH64_OPND_Rt2:
8edca81e 6185 case AARCH64_OPND_Rt_LS64:
bd7ceb8d 6186 case AARCH64_OPND_Rt_SP:
a06ea964
NC
6187 case AARCH64_OPND_Rs:
6188 case AARCH64_OPND_Ra:
6189 case AARCH64_OPND_Rt_SYS:
6190 case AARCH64_OPND_Rd_SP:
6191 case AARCH64_OPND_Rn_SP:
c84364ec 6192 case AARCH64_OPND_Rm_SP:
a06ea964
NC
6193 case AARCH64_OPND_Fd:
6194 case AARCH64_OPND_Fn:
6195 case AARCH64_OPND_Fm:
6196 case AARCH64_OPND_Fa:
6197 case AARCH64_OPND_Ft:
6198 case AARCH64_OPND_Ft2:
6199 case AARCH64_OPND_Sd:
6200 case AARCH64_OPND_Sn:
6201 case AARCH64_OPND_Sm:
f42f1a1d 6202 case AARCH64_OPND_Va:
a06ea964
NC
6203 case AARCH64_OPND_Vd:
6204 case AARCH64_OPND_Vn:
6205 case AARCH64_OPND_Vm:
6206 case AARCH64_OPND_VdD1:
6207 case AARCH64_OPND_VnD1:
6208 operand->reg.regno = default_value;
6209 break;
f89c290e
VDN
6210 case AARCH64_OPND_PAIRREG_OR_XZR:
6211 if (inst.base.operands[idx - 1].reg.regno == 0x1f)
6212 {
6213 operand->reg.regno = 0x1f;
6214 break;
6215 }
6216 operand->reg.regno = inst.base.operands[idx - 1].reg.regno + 1;
6217 break;
6218 case AARCH64_OPND_PAIRREG:
6219 operand->reg.regno = inst.base.operands[idx - 1].reg.regno + 1;
6220 break;
a06ea964
NC
6221
6222 case AARCH64_OPND_Ed:
6223 case AARCH64_OPND_En:
6224 case AARCH64_OPND_Em:
369c9167 6225 case AARCH64_OPND_Em16:
f42f1a1d 6226 case AARCH64_OPND_SM3_IMM2:
a06ea964
NC
6227 operand->reglane.regno = default_value;
6228 break;
6229
6230 case AARCH64_OPND_IDX:
6231 case AARCH64_OPND_BIT_NUM:
6232 case AARCH64_OPND_IMMR:
6233 case AARCH64_OPND_IMMS:
6234 case AARCH64_OPND_SHLL_IMM:
6235 case AARCH64_OPND_IMM_VLSL:
6236 case AARCH64_OPND_IMM_VLSR:
6237 case AARCH64_OPND_CCMP_IMM:
6238 case AARCH64_OPND_FBITS:
6239 case AARCH64_OPND_UIMM4:
6240 case AARCH64_OPND_UIMM3_OP1:
6241 case AARCH64_OPND_UIMM3_OP2:
6242 case AARCH64_OPND_IMM:
f42f1a1d 6243 case AARCH64_OPND_IMM_2:
a06ea964
NC
6244 case AARCH64_OPND_WIDTH:
6245 case AARCH64_OPND_UIMM7:
6246 case AARCH64_OPND_NZCV:
245d2e3f
RS
6247 case AARCH64_OPND_SVE_PATTERN:
6248 case AARCH64_OPND_SVE_PRFOP:
a06ea964
NC
6249 operand->imm.value = default_value;
6250 break;
6251
2442d846
RS
6252 case AARCH64_OPND_SVE_PATTERN_SCALED:
6253 operand->imm.value = default_value;
6254 operand->shifter.kind = AARCH64_MOD_MUL;
6255 operand->shifter.amount = 1;
6256 break;
6257
a06ea964
NC
6258 case AARCH64_OPND_EXCEPTION:
6259 inst.reloc.type = BFD_RELOC_UNUSED;
6260 break;
6261
6262 case AARCH64_OPND_BARRIER_ISB:
6263 operand->barrier = aarch64_barrier_options + default_value;
ff605452
SD
6264 break;
6265
6266 case AARCH64_OPND_BTI_TARGET:
6267 operand->hint_option = aarch64_hint_options + default_value;
6268 break;
a06ea964
NC
6269
6270 default:
6271 break;
6272 }
6273}
6274
6275/* Process the relocation type for move wide instructions.
6276 Return TRUE on success; otherwise return FALSE. */
6277
5b7c81bd 6278static bool
a06ea964
NC
6279process_movw_reloc_info (void)
6280{
6281 int is32;
6282 unsigned shift;
6283
6284 is32 = inst.base.operands[0].qualifier == AARCH64_OPND_QLF_W ? 1 : 0;
6285
6286 if (inst.base.opcode->op == OP_MOVK)
6287 switch (inst.reloc.type)
6288 {
6289 case BFD_RELOC_AARCH64_MOVW_G0_S:
6290 case BFD_RELOC_AARCH64_MOVW_G1_S:
6291 case BFD_RELOC_AARCH64_MOVW_G2_S:
32247401
RL
6292 case BFD_RELOC_AARCH64_MOVW_PREL_G0:
6293 case BFD_RELOC_AARCH64_MOVW_PREL_G1:
6294 case BFD_RELOC_AARCH64_MOVW_PREL_G2:
6295 case BFD_RELOC_AARCH64_MOVW_PREL_G3:
1aa66fb1 6296 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 6297 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
a06ea964 6298 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
a06ea964
NC
6299 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
6300 set_syntax_error
6301 (_("the specified relocation type is not allowed for MOVK"));
5b7c81bd 6302 return false;
a06ea964
NC
6303 default:
6304 break;
6305 }
6306
6307 switch (inst.reloc.type)
6308 {
6309 case BFD_RELOC_AARCH64_MOVW_G0:
a06ea964 6310 case BFD_RELOC_AARCH64_MOVW_G0_NC:
f09c556a 6311 case BFD_RELOC_AARCH64_MOVW_G0_S:
ca632371 6312 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
32247401
RL
6313 case BFD_RELOC_AARCH64_MOVW_PREL_G0:
6314 case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
43a357f9 6315 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
3e8286c0 6316 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
3b957e5b 6317 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
49df5539
JW
6318 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
6319 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
a06ea964
NC
6320 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
6321 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6322 shift = 0;
6323 break;
6324 case BFD_RELOC_AARCH64_MOVW_G1:
a06ea964 6325 case BFD_RELOC_AARCH64_MOVW_G1_NC:
f09c556a 6326 case BFD_RELOC_AARCH64_MOVW_G1_S:
654248e7 6327 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
32247401
RL
6328 case BFD_RELOC_AARCH64_MOVW_PREL_G1:
6329 case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
43a357f9 6330 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
1aa66fb1 6331 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
3b957e5b 6332 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
49df5539
JW
6333 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
6334 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
a06ea964
NC
6335 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
6336 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6337 shift = 16;
6338 break;
6339 case BFD_RELOC_AARCH64_MOVW_G2:
a06ea964 6340 case BFD_RELOC_AARCH64_MOVW_G2_NC:
f09c556a 6341 case BFD_RELOC_AARCH64_MOVW_G2_S:
32247401
RL
6342 case BFD_RELOC_AARCH64_MOVW_PREL_G2:
6343 case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
49df5539 6344 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964
NC
6345 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
6346 if (is32)
6347 {
6348 set_fatal_syntax_error
6349 (_("the specified relocation type is not allowed for 32-bit "
6350 "register"));
5b7c81bd 6351 return false;
a06ea964
NC
6352 }
6353 shift = 32;
6354 break;
6355 case BFD_RELOC_AARCH64_MOVW_G3:
32247401 6356 case BFD_RELOC_AARCH64_MOVW_PREL_G3:
a06ea964
NC
6357 if (is32)
6358 {
6359 set_fatal_syntax_error
6360 (_("the specified relocation type is not allowed for 32-bit "
6361 "register"));
5b7c81bd 6362 return false;
a06ea964
NC
6363 }
6364 shift = 48;
6365 break;
6366 default:
6367 /* More cases should be added when more MOVW-related relocation types
6368 are supported in GAS. */
6369 gas_assert (aarch64_gas_internal_fixup_p ());
6370 /* The shift amount should have already been set by the parser. */
5b7c81bd 6371 return true;
a06ea964
NC
6372 }
6373 inst.base.operands[1].shifter.amount = shift;
5b7c81bd 6374 return true;
a06ea964
NC
6375}
6376
a06ea964
NC
6377/* Determine and return the real reloc type code for an instruction
6378 with the pseudo reloc type code BFD_RELOC_AARCH64_LDST_LO12. */
6379
6380static inline bfd_reloc_code_real_type
6381ldst_lo12_determine_real_reloc_type (void)
6382{
96cbfd9f 6383 unsigned logsz, max_logsz;
a06ea964
NC
6384 enum aarch64_opnd_qualifier opd0_qlf = inst.base.operands[0].qualifier;
6385 enum aarch64_opnd_qualifier opd1_qlf = inst.base.operands[1].qualifier;
6386
84f1b9fb 6387 const bfd_reloc_code_real_type reloc_ldst_lo12[5][5] = {
4c562523
JW
6388 {
6389 BFD_RELOC_AARCH64_LDST8_LO12,
6390 BFD_RELOC_AARCH64_LDST16_LO12,
6391 BFD_RELOC_AARCH64_LDST32_LO12,
6392 BFD_RELOC_AARCH64_LDST64_LO12,
a06ea964 6393 BFD_RELOC_AARCH64_LDST128_LO12
4c562523
JW
6394 },
6395 {
6396 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12,
6397 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12,
6398 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12,
6399 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12,
6400 BFD_RELOC_AARCH64_NONE
6401 },
6402 {
6403 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC,
6404 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC,
6405 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC,
6406 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC,
6407 BFD_RELOC_AARCH64_NONE
84f1b9fb
RL
6408 },
6409 {
6410 BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12,
6411 BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12,
6412 BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12,
6413 BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12,
6414 BFD_RELOC_AARCH64_NONE
6415 },
6416 {
6417 BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC,
6418 BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC,
6419 BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC,
6420 BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC,
6421 BFD_RELOC_AARCH64_NONE
4c562523 6422 }
a06ea964
NC
6423 };
6424
4c562523
JW
6425 gas_assert (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
6426 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
6427 || (inst.reloc.type
84f1b9fb
RL
6428 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC)
6429 || (inst.reloc.type
6430 == BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12)
6431 || (inst.reloc.type
6432 == BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12_NC));
a06ea964
NC
6433 gas_assert (inst.base.opcode->operands[1] == AARCH64_OPND_ADDR_UIMM12);
6434
6435 if (opd1_qlf == AARCH64_OPND_QLF_NIL)
6436 opd1_qlf =
6437 aarch64_get_expected_qualifier (inst.base.opcode->qualifiers_list,
6438 1, opd0_qlf, 0);
6439 gas_assert (opd1_qlf != AARCH64_OPND_QLF_NIL);
6440
38c5aa5e 6441 logsz = get_log2 (aarch64_get_qualifier_esize (opd1_qlf));
96cbfd9f 6442
4c562523 6443 if (inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
84f1b9fb
RL
6444 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC
6445 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12
6446 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12_NC)
96cbfd9f 6447 max_logsz = 3;
4c562523 6448 else
96cbfd9f
NC
6449 max_logsz = 4;
6450
6451 if (logsz > max_logsz)
6452 {
6453 /* SEE PR 27904 for an example of this. */
6454 set_fatal_syntax_error
6455 (_("relocation qualifier does not match instruction size"));
6456 return BFD_RELOC_AARCH64_NONE;
6457 }
a06ea964 6458
4c562523 6459 /* In reloc.c, these pseudo relocation types should be defined in similar
33eaf5de 6460 order as above reloc_ldst_lo12 array. Because the array index calculation
4c562523
JW
6461 below relies on this. */
6462 return reloc_ldst_lo12[inst.reloc.type - BFD_RELOC_AARCH64_LDST_LO12][logsz];
a06ea964
NC
6463}
6464
99e01a66
RS
6465/* Check whether a register list REGINFO is valid. The registers have type
6466 REG_TYPE and must be numbered in increasing order (modulo the register
6467 bank size). They must have a consistent stride.
a06ea964 6468
f5b57fea 6469 Return true if the list is valid, describing it in LIST if so. */
a06ea964 6470
5b7c81bd 6471static bool
99e01a66
RS
6472reg_list_valid_p (uint32_t reginfo, struct aarch64_reglist *list,
6473 aarch64_reg_type reg_type)
a06ea964 6474{
99e01a66
RS
6475 uint32_t i, nb_regs, prev_regno, incr, mask;
6476 mask = reg_type_mask (reg_type);
a06ea964
NC
6477
6478 nb_regs = 1 + (reginfo & 0x3);
6479 reginfo >>= 2;
6480 prev_regno = reginfo & 0x1f;
f5b57fea
RS
6481 incr = 1;
6482
6483 list->first_regno = prev_regno;
6484 list->num_regs = nb_regs;
a06ea964
NC
6485
6486 for (i = 1; i < nb_regs; ++i)
6487 {
f5b57fea 6488 uint32_t curr_regno, curr_incr;
a06ea964
NC
6489 reginfo >>= 5;
6490 curr_regno = reginfo & 0x1f;
99e01a66 6491 curr_incr = (curr_regno - prev_regno) & mask;
f5b57fea
RS
6492 if (curr_incr == 0)
6493 return false;
6494 else if (i == 1)
6495 incr = curr_incr;
6496 else if (curr_incr != incr)
5b7c81bd 6497 return false;
a06ea964
NC
6498 prev_regno = curr_regno;
6499 }
6500
f5b57fea 6501 list->stride = incr;
5b7c81bd 6502 return true;
a06ea964
NC
6503}
6504
6505/* Generic instruction operand parser. This does no encoding and no
6506 semantic validation; it merely squirrels values away in the inst
6507 structure. Returns TRUE or FALSE depending on whether the
6508 specified grammar matched. */
6509
5b7c81bd 6510static bool
a06ea964
NC
6511parse_operands (char *str, const aarch64_opcode *opcode)
6512{
6513 int i;
6514 char *backtrack_pos = 0;
6515 const enum aarch64_opnd *operands = opcode->operands;
5517af82 6516 const uint64_t flags = opcode->flags;
1799c0d0 6517 aarch64_reg_type imm_reg_type;
a06ea964
NC
6518
6519 clear_error ();
6520 skip_whitespace (str);
6521
4abb672a 6522 if (AARCH64_CPU_HAS_FEATURE (*opcode->avariant, SME2))
6efa6601 6523 imm_reg_type = REG_TYPE_R_ZR_SP_BHSDQ_VZP_PN;
4abb672a
RS
6524 else if (AARCH64_CPU_HAS_FEATURE (*opcode->avariant, SVE)
6525 || AARCH64_CPU_HAS_FEATURE (*opcode->avariant, SVE2))
2f4e3a2c 6526 imm_reg_type = REG_TYPE_R_ZR_SP_BHSDQ_VZP;
c0890d26 6527 else
2f4e3a2c 6528 imm_reg_type = REG_TYPE_R_ZR_BHSDQ_V;
1799c0d0 6529
a06ea964
NC
6530 for (i = 0; operands[i] != AARCH64_OPND_NIL; i++)
6531 {
6532 int64_t val;
e1b988bb 6533 const reg_entry *reg;
a06ea964 6534 int comma_skipped_p = 0;
8f9a77af 6535 struct vector_type_el vectype;
4df068de 6536 aarch64_opnd_qualifier_t qualifier, base_qualifier, offset_qualifier;
a06ea964 6537 aarch64_opnd_info *info = &inst.base.operands[i];
f11ad6bc 6538 aarch64_reg_type reg_type;
a06ea964
NC
6539
6540 DEBUG_TRACE ("parse operand %d", i);
6541
6542 /* Assign the operand code. */
6543 info->type = operands[i];
6544
6545 if (optional_operand_p (opcode, i))
6546 {
6547 /* Remember where we are in case we need to backtrack. */
6548 gas_assert (!backtrack_pos);
6549 backtrack_pos = str;
6550 }
6551
33eaf5de 6552 /* Expect comma between operands; the backtrack mechanism will take
a06ea964
NC
6553 care of cases of omitted optional operand. */
6554 if (i > 0 && ! skip_past_char (&str, ','))
6555 {
6556 set_syntax_error (_("comma expected between operands"));
6557 goto failure;
6558 }
6559 else
6560 comma_skipped_p = 1;
6561
6562 switch (operands[i])
6563 {
6564 case AARCH64_OPND_Rd:
6565 case AARCH64_OPND_Rn:
6566 case AARCH64_OPND_Rm:
6567 case AARCH64_OPND_Rt:
6568 case AARCH64_OPND_Rt2:
6c0ecdba 6569 case AARCH64_OPND_X16:
a06ea964
NC
6570 case AARCH64_OPND_Rs:
6571 case AARCH64_OPND_Ra:
8edca81e 6572 case AARCH64_OPND_Rt_LS64:
a06ea964 6573 case AARCH64_OPND_Rt_SYS:
ee804238 6574 case AARCH64_OPND_PAIRREG:
d30eb38d 6575 case AARCH64_OPND_PAIRREG_OR_XZR:
047cd301 6576 case AARCH64_OPND_SVE_Rm:
2f4e3a2c 6577 po_int_fp_reg_or_fail (REG_TYPE_R_ZR);
8edca81e 6578
4a3e3e22
PW
6579 /* In LS64 load/store instructions Rt register number must be even
6580 and <=22. */
8edca81e 6581 if (operands[i] == AARCH64_OPND_Rt_LS64)
8edca81e 6582 {
6c0ecdba
SP
6583 /* We've already checked if this is valid register.
6584 This will check if register number (Rt) is not undefined for
6585 LS64 instructions:
6586 if Rt<4:3> == '11' || Rt<0> == '1' then UNDEFINED. */
6587 if ((info->reg.regno & 0x18) == 0x18
6588 || (info->reg.regno & 0x01) == 0x01)
6589 {
6590 set_syntax_error
6591 (_("invalid Rt register number in 64-byte load/store"));
6592 goto failure;
6593 }
6594 }
6595 else if (operands[i] == AARCH64_OPND_X16)
6596 {
6597 if (info->reg.regno != 16)
6598 {
6599 goto failure;
6600 }
8edca81e 6601 }
a06ea964
NC
6602 break;
6603
6604 case AARCH64_OPND_Rd_SP:
6605 case AARCH64_OPND_Rn_SP:
bd7ceb8d 6606 case AARCH64_OPND_Rt_SP:
047cd301 6607 case AARCH64_OPND_SVE_Rn_SP:
c84364ec 6608 case AARCH64_OPND_Rm_SP:
30ba1d7e 6609 po_int_fp_reg_or_fail (REG_TYPE_R_SP);
a06ea964
NC
6610 break;
6611
6612 case AARCH64_OPND_Rm_EXT:
6613 case AARCH64_OPND_Rm_SFT:
6614 po_misc_or_fail (parse_shifter_operand
6615 (&str, info, (operands[i] == AARCH64_OPND_Rm_EXT
6616 ? SHIFTED_ARITH_IMM
6617 : SHIFTED_LOGIC_IMM)));
6618 if (!info->shifter.operator_present)
6619 {
6620 /* Default to LSL if not present. Libopcodes prefers shifter
6621 kind to be explicit. */
6622 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
6623 info->shifter.kind = AARCH64_MOD_LSL;
6624 /* For Rm_EXT, libopcodes will carry out further check on whether
6625 or not stack pointer is used in the instruction (Recall that
6626 "the extend operator is not optional unless at least one of
6627 "Rd" or "Rn" is '11111' (i.e. WSP)"). */
6628 }
6629 break;
6630
4792a423
YK
6631 case AARCH64_OPND_Rm_LSL:
6632 po_misc_or_fail (parse_reg_lsl_shifter_operand (&str, info));
6633 if (!info->shifter.operator_present)
6634 {
6635 /* Default to LSL #0 if not present. */
6636 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
6637 info->shifter.kind = AARCH64_MOD_LSL;
6638 info->shifter.amount = 0;
6639 }
6640 break;
6641
a06ea964
NC
6642 case AARCH64_OPND_Fd:
6643 case AARCH64_OPND_Fn:
6644 case AARCH64_OPND_Fm:
6645 case AARCH64_OPND_Fa:
6646 case AARCH64_OPND_Ft:
6647 case AARCH64_OPND_Ft2:
6648 case AARCH64_OPND_Sd:
6649 case AARCH64_OPND_Sn:
6650 case AARCH64_OPND_Sm:
047cd301
RS
6651 case AARCH64_OPND_SVE_VZn:
6652 case AARCH64_OPND_SVE_Vd:
6653 case AARCH64_OPND_SVE_Vm:
6654 case AARCH64_OPND_SVE_Vn:
30ba1d7e 6655 po_int_fp_reg_or_fail (REG_TYPE_BHSDQ);
a06ea964
NC
6656 break;
6657
f11ad6bc
RS
6658 case AARCH64_OPND_SVE_Pd:
6659 case AARCH64_OPND_SVE_Pg3:
6660 case AARCH64_OPND_SVE_Pg4_5:
6661 case AARCH64_OPND_SVE_Pg4_10:
6662 case AARCH64_OPND_SVE_Pg4_16:
6663 case AARCH64_OPND_SVE_Pm:
6664 case AARCH64_OPND_SVE_Pn:
6665 case AARCH64_OPND_SVE_Pt:
971eda73 6666 case AARCH64_OPND_SME_Pm:
2f4e3a2c 6667 reg_type = REG_TYPE_P;
f11ad6bc
RS
6668 goto vector_reg;
6669
6670 case AARCH64_OPND_SVE_Za_5:
6671 case AARCH64_OPND_SVE_Za_16:
6672 case AARCH64_OPND_SVE_Zd:
6673 case AARCH64_OPND_SVE_Zm_5:
6674 case AARCH64_OPND_SVE_Zm_16:
6675 case AARCH64_OPND_SVE_Zn:
6676 case AARCH64_OPND_SVE_Zt:
e87ff672 6677 case AARCH64_OPND_SME_Zm:
2f4e3a2c 6678 reg_type = REG_TYPE_Z;
f11ad6bc
RS
6679 goto vector_reg;
6680
503fae12
RS
6681 case AARCH64_OPND_SVE_PNd:
6682 case AARCH64_OPND_SVE_PNg4_10:
6683 case AARCH64_OPND_SVE_PNn:
6684 case AARCH64_OPND_SVE_PNt:
99e01a66 6685 case AARCH64_OPND_SME_PNd3:
b408ebbf 6686 case AARCH64_OPND_SME_PNg3:
99e01a66 6687 case AARCH64_OPND_SME_PNn:
503fae12
RS
6688 reg_type = REG_TYPE_PN;
6689 goto vector_reg;
6690
f42f1a1d 6691 case AARCH64_OPND_Va:
a06ea964
NC
6692 case AARCH64_OPND_Vd:
6693 case AARCH64_OPND_Vn:
6694 case AARCH64_OPND_Vm:
2f4e3a2c 6695 reg_type = REG_TYPE_V;
f11ad6bc 6696 vector_reg:
074c89d2
RS
6697 reg = aarch64_reg_parse (&str, reg_type, &vectype);
6698 if (!reg)
e426521e 6699 goto failure;
a06ea964
NC
6700 if (vectype.defined & NTA_HASINDEX)
6701 goto failure;
6702
074c89d2 6703 info->reg.regno = reg->number;
503fae12
RS
6704 if ((reg_type == REG_TYPE_P
6705 || reg_type == REG_TYPE_PN
6706 || reg_type == REG_TYPE_Z)
f11ad6bc 6707 && vectype.type == NT_invtype)
2f4e3a2c 6708 /* Unqualified P and Z registers are allowed in certain
f11ad6bc
RS
6709 contexts. Rely on F_STRICT qualifier checking to catch
6710 invalid uses. */
6711 info->qualifier = AARCH64_OPND_QLF_NIL;
6712 else
6713 {
6714 info->qualifier = vectype_to_qualifier (&vectype);
6715 if (info->qualifier == AARCH64_OPND_QLF_NIL)
6716 goto failure;
6717 }
a06ea964
NC
6718 break;
6719
6720 case AARCH64_OPND_VdD1:
6721 case AARCH64_OPND_VnD1:
2f4e3a2c 6722 reg = aarch64_reg_parse (&str, REG_TYPE_V, &vectype);
074c89d2 6723 if (!reg)
e426521e 6724 goto failure;
a06ea964
NC
6725 if (vectype.type != NT_d || vectype.index != 1)
6726 {
6727 set_fatal_syntax_error
6728 (_("the top half of a 128-bit FP/SIMD register is expected"));
6729 goto failure;
6730 }
074c89d2 6731 info->reg.regno = reg->number;
a06ea964
NC
6732 /* N.B: VdD1 and VnD1 are treated as an fp or advsimd scalar register
6733 here; it is correct for the purpose of encoding/decoding since
6734 only the register number is explicitly encoded in the related
6735 instructions, although this appears a bit hacky. */
6736 info->qualifier = AARCH64_OPND_QLF_S_D;
6737 break;
6738
582e12bf
RS
6739 case AARCH64_OPND_SVE_Zm3_INDEX:
6740 case AARCH64_OPND_SVE_Zm3_22_INDEX:
dfc12f9f 6741 case AARCH64_OPND_SVE_Zm3_19_INDEX:
116adc27 6742 case AARCH64_OPND_SVE_Zm3_11_INDEX:
31e36ab3 6743 case AARCH64_OPND_SVE_Zm4_11_INDEX:
582e12bf 6744 case AARCH64_OPND_SVE_Zm4_INDEX:
f11ad6bc 6745 case AARCH64_OPND_SVE_Zn_INDEX:
39092c7a
SP
6746 case AARCH64_OPND_SVE_Zm_imm4:
6747 case AARCH64_OPND_SVE_Zn_5_INDEX:
80752eb0
RS
6748 case AARCH64_OPND_SME_Zm_INDEX1:
6749 case AARCH64_OPND_SME_Zm_INDEX2:
a8cb21aa 6750 case AARCH64_OPND_SME_Zm_INDEX3_1:
ed429b33
RS
6751 case AARCH64_OPND_SME_Zm_INDEX3_2:
6752 case AARCH64_OPND_SME_Zm_INDEX3_10:
a8cb21aa
RS
6753 case AARCH64_OPND_SME_Zm_INDEX4_1:
6754 case AARCH64_OPND_SME_Zm_INDEX4_10:
cbd11b88
RS
6755 case AARCH64_OPND_SME_Zn_INDEX1_16:
6756 case AARCH64_OPND_SME_Zn_INDEX2_15:
6757 case AARCH64_OPND_SME_Zn_INDEX2_16:
6758 case AARCH64_OPND_SME_Zn_INDEX3_14:
6759 case AARCH64_OPND_SME_Zn_INDEX3_15:
6760 case AARCH64_OPND_SME_Zn_INDEX4_14:
2f4e3a2c 6761 reg_type = REG_TYPE_Z;
f11ad6bc
RS
6762 goto vector_reg_index;
6763
a06ea964
NC
6764 case AARCH64_OPND_Ed:
6765 case AARCH64_OPND_En:
6766 case AARCH64_OPND_Em:
369c9167 6767 case AARCH64_OPND_Em16:
f42f1a1d 6768 case AARCH64_OPND_SM3_IMM2:
2f4e3a2c 6769 reg_type = REG_TYPE_V;
f11ad6bc 6770 vector_reg_index:
074c89d2
RS
6771 reg = aarch64_reg_parse (&str, reg_type, &vectype);
6772 if (!reg)
e426521e 6773 goto failure;
cbd11b88 6774 if (!(vectype.defined & NTA_HASINDEX))
a06ea964
NC
6775 goto failure;
6776
cbd11b88
RS
6777 if (reg->type == REG_TYPE_Z && vectype.type == NT_invtype)
6778 /* Unqualified Zn[index] is allowed in LUTI2 instructions. */
6779 info->qualifier = AARCH64_OPND_QLF_NIL;
6780 else
6781 {
6782 if (vectype.type == NT_invtype)
6783 goto failure;
6784 info->qualifier = vectype_to_qualifier (&vectype);
6785 if (info->qualifier == AARCH64_OPND_QLF_NIL)
6786 goto failure;
6787 }
6788
074c89d2 6789 info->reglane.regno = reg->number;
a06ea964 6790 info->reglane.index = vectype.index;
a06ea964
NC
6791 break;
6792
f11ad6bc
RS
6793 case AARCH64_OPND_SVE_ZnxN:
6794 case AARCH64_OPND_SVE_ZtxN:
d8773a8a
RS
6795 case AARCH64_OPND_SME_Zdnx2:
6796 case AARCH64_OPND_SME_Zdnx4:
b33f1bcd
SP
6797 case AARCH64_OPND_SME_Zt2:
6798 case AARCH64_OPND_SME_Zt3:
6799 case AARCH64_OPND_SME_Zt4:
99e01a66
RS
6800 case AARCH64_OPND_SME_Zmx2:
6801 case AARCH64_OPND_SME_Zmx4:
d8773a8a
RS
6802 case AARCH64_OPND_SME_Znx2:
6803 case AARCH64_OPND_SME_Znx4:
b408ebbf
RS
6804 case AARCH64_OPND_SME_Ztx2_STRIDED:
6805 case AARCH64_OPND_SME_Ztx4_STRIDED:
2f4e3a2c 6806 reg_type = REG_TYPE_Z;
f11ad6bc
RS
6807 goto vector_reg_list;
6808
99e01a66
RS
6809 case AARCH64_OPND_SME_Pdx2:
6810 case AARCH64_OPND_SME_PdxN:
6811 reg_type = REG_TYPE_P;
6812 goto vector_reg_list;
6813
a06ea964
NC
6814 case AARCH64_OPND_LVn:
6815 case AARCH64_OPND_LVt:
6816 case AARCH64_OPND_LVt_AL:
6817 case AARCH64_OPND_LEt:
2f4e3a2c 6818 reg_type = REG_TYPE_V;
f11ad6bc 6819 vector_reg_list:
2f4e3a2c 6820 if (reg_type == REG_TYPE_Z
f11ad6bc
RS
6821 && get_opcode_dependent_value (opcode) == 1
6822 && *str != '{')
a06ea964 6823 {
074c89d2
RS
6824 reg = aarch64_reg_parse (&str, reg_type, &vectype);
6825 if (!reg)
e426521e 6826 goto failure;
074c89d2 6827 info->reglist.first_regno = reg->number;
f11ad6bc 6828 info->reglist.num_regs = 1;
f5b57fea 6829 info->reglist.stride = 1;
f11ad6bc
RS
6830 }
6831 else
6832 {
6833 val = parse_vector_reg_list (&str, reg_type, &vectype);
6834 if (val == PARSE_FAIL)
6835 goto failure;
163b2c58 6836
99e01a66 6837 if (! reg_list_valid_p (val, &info->reglist, reg_type))
f11ad6bc
RS
6838 {
6839 set_fatal_syntax_error (_("invalid register list"));
6840 goto failure;
6841 }
163b2c58 6842
e87ff672 6843 if ((int) vectype.width > 0 && *str != ',')
163b2c58
BW
6844 {
6845 set_fatal_syntax_error
6846 (_("expected element type rather than vector type"));
6847 goto failure;
6848 }
a06ea964 6849 }
a06ea964
NC
6850 if (operands[i] == AARCH64_OPND_LEt)
6851 {
6852 if (!(vectype.defined & NTA_HASINDEX))
6853 goto failure;
6854 info->reglist.has_index = 1;
6855 info->reglist.index = vectype.index;
6856 }
f11ad6bc
RS
6857 else
6858 {
6859 if (vectype.defined & NTA_HASINDEX)
6860 goto failure;
6861 if (!(vectype.defined & NTA_HASTYPE))
6862 {
99e01a66 6863 if (reg_type == REG_TYPE_Z || reg_type == REG_TYPE_P)
f11ad6bc
RS
6864 set_fatal_syntax_error (_("missing type suffix"));
6865 goto failure;
6866 }
6867 }
a06ea964
NC
6868 info->qualifier = vectype_to_qualifier (&vectype);
6869 if (info->qualifier == AARCH64_OPND_QLF_NIL)
6870 goto failure;
6871 break;
6872
a6a51754
RL
6873 case AARCH64_OPND_CRn:
6874 case AARCH64_OPND_CRm:
a06ea964 6875 {
a6a51754
RL
6876 char prefix = *(str++);
6877 if (prefix != 'c' && prefix != 'C')
6878 goto failure;
6879
6880 po_imm_nc_or_fail ();
5517af82
VDN
6881 if (flags & F_OPD_NARROW)
6882 {
6883 if ((operands[i] == AARCH64_OPND_CRn)
6884 && (val < 8 || val > 9))
6885 {
6886 set_fatal_syntax_error (_(N_ ("C8 - C9 expected")));
6887 goto failure;
6888 }
6889 else if ((operands[i] == AARCH64_OPND_CRm)
6890 && (val > 7))
6891 {
6892 set_fatal_syntax_error (_(N_ ("C0 - C7 expected")));
6893 goto failure;
6894 }
6895 }
6896 else if (val > 15)
a6a51754
RL
6897 {
6898 set_fatal_syntax_error (_(N_ ("C0 - C15 expected")));
6899 goto failure;
6900 }
6901 info->qualifier = AARCH64_OPND_QLF_CR;
6902 info->imm.value = val;
6903 break;
a06ea964 6904 }
a06ea964
NC
6905
6906 case AARCH64_OPND_SHLL_IMM:
6907 case AARCH64_OPND_IMM_VLSR:
6908 po_imm_or_fail (1, 64);
6909 info->imm.value = val;
6910 break;
6911
6912 case AARCH64_OPND_CCMP_IMM:
e950b345 6913 case AARCH64_OPND_SIMM5:
a06ea964 6914 case AARCH64_OPND_FBITS:
b83b4b13 6915 case AARCH64_OPND_TME_UIMM16:
a06ea964 6916 case AARCH64_OPND_UIMM4:
193614f2
SD
6917 case AARCH64_OPND_UIMM4_ADDG:
6918 case AARCH64_OPND_UIMM10:
a06ea964
NC
6919 case AARCH64_OPND_UIMM3_OP1:
6920 case AARCH64_OPND_UIMM3_OP2:
6921 case AARCH64_OPND_IMM_VLSL:
6922 case AARCH64_OPND_IMM:
f42f1a1d 6923 case AARCH64_OPND_IMM_2:
a06ea964 6924 case AARCH64_OPND_WIDTH:
e950b345
RS
6925 case AARCH64_OPND_SVE_INV_LIMM:
6926 case AARCH64_OPND_SVE_LIMM:
6927 case AARCH64_OPND_SVE_LIMM_MOV:
6928 case AARCH64_OPND_SVE_SHLIMM_PRED:
6929 case AARCH64_OPND_SVE_SHLIMM_UNPRED:
28ed815a 6930 case AARCH64_OPND_SVE_SHLIMM_UNPRED_22:
6efa6601
RS
6931 case AARCH64_OPND_SME_SHRIMM4:
6932 case AARCH64_OPND_SME_SHRIMM5:
e950b345
RS
6933 case AARCH64_OPND_SVE_SHRIMM_PRED:
6934 case AARCH64_OPND_SVE_SHRIMM_UNPRED:
3c17238b 6935 case AARCH64_OPND_SVE_SHRIMM_UNPRED_22:
e950b345
RS
6936 case AARCH64_OPND_SVE_SIMM5:
6937 case AARCH64_OPND_SVE_SIMM5B:
6938 case AARCH64_OPND_SVE_SIMM6:
6939 case AARCH64_OPND_SVE_SIMM8:
6940 case AARCH64_OPND_SVE_UIMM3:
6941 case AARCH64_OPND_SVE_UIMM7:
6942 case AARCH64_OPND_SVE_UIMM8:
6943 case AARCH64_OPND_SVE_UIMM8_53:
c2c4ff8d
SN
6944 case AARCH64_OPND_IMM_ROT1:
6945 case AARCH64_OPND_IMM_ROT2:
6946 case AARCH64_OPND_IMM_ROT3:
582e12bf
RS
6947 case AARCH64_OPND_SVE_IMM_ROT1:
6948 case AARCH64_OPND_SVE_IMM_ROT2:
adccc507 6949 case AARCH64_OPND_SVE_IMM_ROT3:
1f7b42d5
AV
6950 case AARCH64_OPND_CSSC_SIMM8:
6951 case AARCH64_OPND_CSSC_UIMM8:
a06ea964
NC
6952 po_imm_nc_or_fail ();
6953 info->imm.value = val;
6954 break;
6955
e950b345
RS
6956 case AARCH64_OPND_SVE_AIMM:
6957 case AARCH64_OPND_SVE_ASIMM:
6958 po_imm_nc_or_fail ();
6959 info->imm.value = val;
6960 skip_whitespace (str);
6961 if (skip_past_comma (&str))
6962 po_misc_or_fail (parse_shift (&str, info, SHIFTED_LSL));
6963 else
6964 inst.base.operands[i].shifter.kind = AARCH64_MOD_LSL;
6965 break;
6966
245d2e3f
RS
6967 case AARCH64_OPND_SVE_PATTERN:
6968 po_enum_or_fail (aarch64_sve_pattern_array);
6969 info->imm.value = val;
6970 break;
6971
2442d846
RS
6972 case AARCH64_OPND_SVE_PATTERN_SCALED:
6973 po_enum_or_fail (aarch64_sve_pattern_array);
6974 info->imm.value = val;
6975 if (skip_past_comma (&str)
6976 && !parse_shift (&str, info, SHIFTED_MUL))
6977 goto failure;
6978 if (!info->shifter.operator_present)
6979 {
6980 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
6981 info->shifter.kind = AARCH64_MOD_MUL;
6982 info->shifter.amount = 1;
6983 }
6984 break;
6985
245d2e3f
RS
6986 case AARCH64_OPND_SVE_PRFOP:
6987 po_enum_or_fail (aarch64_sve_prfop_array);
6988 info->imm.value = val;
6989 break;
6990
a06ea964
NC
6991 case AARCH64_OPND_UIMM7:
6992 po_imm_or_fail (0, 127);
6993 info->imm.value = val;
6994 break;
6995
6996 case AARCH64_OPND_IDX:
f42f1a1d 6997 case AARCH64_OPND_MASK:
a06ea964
NC
6998 case AARCH64_OPND_BIT_NUM:
6999 case AARCH64_OPND_IMMR:
7000 case AARCH64_OPND_IMMS:
7001 po_imm_or_fail (0, 63);
7002 info->imm.value = val;
7003 break;
7004
7005 case AARCH64_OPND_IMM0:
7006 po_imm_nc_or_fail ();
7007 if (val != 0)
7008 {
7009 set_fatal_syntax_error (_("immediate zero expected"));
7010 goto failure;
7011 }
7012 info->imm.value = 0;
7013 break;
7014
7015 case AARCH64_OPND_FPIMM0:
7016 {
7017 int qfloat;
5b7c81bd 7018 bool res1 = false, res2 = false;
a06ea964
NC
7019 /* N.B. -0.0 will be rejected; although -0.0 shouldn't be rejected,
7020 it is probably not worth the effort to support it. */
5b7c81bd 7021 if (!(res1 = parse_aarch64_imm_float (&str, &qfloat, false,
1799c0d0 7022 imm_reg_type))
6a9deabe
RS
7023 && (error_p ()
7024 || !(res2 = parse_constant_immediate (&str, &val,
7025 imm_reg_type))))
a06ea964
NC
7026 goto failure;
7027 if ((res1 && qfloat == 0) || (res2 && val == 0))
7028 {
7029 info->imm.value = 0;
7030 info->imm.is_fp = 1;
7031 break;
7032 }
7033 set_fatal_syntax_error (_("immediate zero expected"));
7034 goto failure;
7035 }
7036
7037 case AARCH64_OPND_IMM_MOV:
7038 {
7039 char *saved = str;
2f4e3a2c
RS
7040 if (reg_name_p (str, REG_TYPE_R_ZR_SP)
7041 || reg_name_p (str, REG_TYPE_V))
a06ea964
NC
7042 goto failure;
7043 str = saved;
eac4eb8e 7044 po_misc_or_fail (aarch64_get_expression (&inst.reloc.exp, &str,
c1723a81 7045 GE_OPT_PREFIX, REJECT_ABSENT));
a06ea964
NC
7046 /* The MOV immediate alias will be fixed up by fix_mov_imm_insn
7047 later. fix_mov_imm_insn will try to determine a machine
7048 instruction (MOVZ, MOVN or ORR) for it and will issue an error
7049 message if the immediate cannot be moved by a single
7050 instruction. */
7051 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
7052 inst.base.operands[i].skip = 1;
7053 }
7054 break;
7055
7056 case AARCH64_OPND_SIMD_IMM:
7057 case AARCH64_OPND_SIMD_IMM_SFT:
1799c0d0 7058 if (! parse_big_immediate (&str, &val, imm_reg_type))
a06ea964
NC
7059 goto failure;
7060 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7061 /* addr_off_p */ 0,
7062 /* need_libopcodes_p */ 1,
7063 /* skip_p */ 1);
7064 /* Parse shift.
7065 N.B. although AARCH64_OPND_SIMD_IMM doesn't permit any
7066 shift, we don't check it here; we leave the checking to
7067 the libopcodes (operand_general_constraint_met_p). By
7068 doing this, we achieve better diagnostics. */
7069 if (skip_past_comma (&str)
7070 && ! parse_shift (&str, info, SHIFTED_LSL_MSL))
7071 goto failure;
7072 if (!info->shifter.operator_present
7073 && info->type == AARCH64_OPND_SIMD_IMM_SFT)
7074 {
7075 /* Default to LSL if not present. Libopcodes prefers shifter
7076 kind to be explicit. */
7077 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
7078 info->shifter.kind = AARCH64_MOD_LSL;
7079 }
7080 break;
7081
7082 case AARCH64_OPND_FPIMM:
7083 case AARCH64_OPND_SIMD_FPIMM:
165d4950 7084 case AARCH64_OPND_SVE_FPIMM8:
a06ea964
NC
7085 {
7086 int qfloat;
5b7c81bd 7087 bool dp_p;
165d4950
RS
7088
7089 dp_p = double_precision_operand_p (&inst.base.operands[0]);
6a9deabe 7090 if (!parse_aarch64_imm_float (&str, &qfloat, dp_p, imm_reg_type)
874d7e6e 7091 || !aarch64_imm_float_p (qfloat))
a06ea964 7092 {
6a9deabe
RS
7093 if (!error_p ())
7094 set_fatal_syntax_error (_("invalid floating-point"
7095 " constant"));
a06ea964
NC
7096 goto failure;
7097 }
7098 inst.base.operands[i].imm.value = encode_imm_float_bits (qfloat);
7099 inst.base.operands[i].imm.is_fp = 1;
7100 }
7101 break;
7102
165d4950
RS
7103 case AARCH64_OPND_SVE_I1_HALF_ONE:
7104 case AARCH64_OPND_SVE_I1_HALF_TWO:
7105 case AARCH64_OPND_SVE_I1_ZERO_ONE:
7106 {
7107 int qfloat;
5b7c81bd 7108 bool dp_p;
165d4950
RS
7109
7110 dp_p = double_precision_operand_p (&inst.base.operands[0]);
7111 if (!parse_aarch64_imm_float (&str, &qfloat, dp_p, imm_reg_type))
7112 {
7113 if (!error_p ())
7114 set_fatal_syntax_error (_("invalid floating-point"
7115 " constant"));
7116 goto failure;
7117 }
7118 inst.base.operands[i].imm.value = qfloat;
7119 inst.base.operands[i].imm.is_fp = 1;
7120 }
7121 break;
7122
a06ea964
NC
7123 case AARCH64_OPND_LIMM:
7124 po_misc_or_fail (parse_shifter_operand (&str, info,
7125 SHIFTED_LOGIC_IMM));
7126 if (info->shifter.operator_present)
7127 {
7128 set_fatal_syntax_error
7129 (_("shift not allowed for bitmask immediate"));
7130 goto failure;
7131 }
7132 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7133 /* addr_off_p */ 0,
7134 /* need_libopcodes_p */ 1,
7135 /* skip_p */ 1);
7136 break;
7137
7138 case AARCH64_OPND_AIMM:
7139 if (opcode->op == OP_ADD)
7140 /* ADD may have relocation types. */
7141 po_misc_or_fail (parse_shifter_operand_reloc (&str, info,
7142 SHIFTED_ARITH_IMM));
7143 else
7144 po_misc_or_fail (parse_shifter_operand (&str, info,
7145 SHIFTED_ARITH_IMM));
7146 switch (inst.reloc.type)
7147 {
7148 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
7149 info->shifter.amount = 12;
7150 break;
7151 case BFD_RELOC_UNUSED:
7152 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
7153 if (info->shifter.kind != AARCH64_MOD_NONE)
7154 inst.reloc.flags = FIXUP_F_HAS_EXPLICIT_SHIFT;
7155 inst.reloc.pc_rel = 0;
7156 break;
7157 default:
7158 break;
7159 }
7160 info->imm.value = 0;
7161 if (!info->shifter.operator_present)
7162 {
7163 /* Default to LSL if not present. Libopcodes prefers shifter
7164 kind to be explicit. */
7165 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
7166 info->shifter.kind = AARCH64_MOD_LSL;
7167 }
7168 break;
7169
7170 case AARCH64_OPND_HALF:
7171 {
7172 /* #<imm16> or relocation. */
7173 int internal_fixup_p;
7174 po_misc_or_fail (parse_half (&str, &internal_fixup_p));
7175 if (internal_fixup_p)
7176 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
7177 skip_whitespace (str);
7178 if (skip_past_comma (&str))
7179 {
7180 /* {, LSL #<shift>} */
7181 if (! aarch64_gas_internal_fixup_p ())
7182 {
7183 set_fatal_syntax_error (_("can't mix relocation modifier "
7184 "with explicit shift"));
7185 goto failure;
7186 }
7187 po_misc_or_fail (parse_shift (&str, info, SHIFTED_LSL));
7188 }
7189 else
7190 inst.base.operands[i].shifter.amount = 0;
7191 inst.base.operands[i].shifter.kind = AARCH64_MOD_LSL;
7192 inst.base.operands[i].imm.value = 0;
7193 if (! process_movw_reloc_info ())
7194 goto failure;
7195 }
7196 break;
7197
7198 case AARCH64_OPND_EXCEPTION:
09c1e68a 7199 case AARCH64_OPND_UNDEFINED:
1799c0d0
RS
7200 po_misc_or_fail (parse_immediate_expression (&str, &inst.reloc.exp,
7201 imm_reg_type));
a06ea964
NC
7202 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7203 /* addr_off_p */ 0,
7204 /* need_libopcodes_p */ 0,
7205 /* skip_p */ 1);
7206 break;
7207
7208 case AARCH64_OPND_NZCV:
7209 {
629310ab 7210 const asm_nzcv *nzcv = str_hash_find_n (aarch64_nzcv_hsh, str, 4);
a06ea964
NC
7211 if (nzcv != NULL)
7212 {
7213 str += 4;
7214 info->imm.value = nzcv->value;
7215 break;
7216 }
7217 po_imm_or_fail (0, 15);
7218 info->imm.value = val;
7219 }
7220 break;
7221
7222 case AARCH64_OPND_COND:
68a64283 7223 case AARCH64_OPND_COND1:
bb7eff52
RS
7224 {
7225 char *start = str;
7226 do
7227 str++;
7228 while (ISALPHA (*str));
629310ab 7229 info->cond = str_hash_find_n (aarch64_cond_hsh, start, str - start);
bb7eff52
RS
7230 if (info->cond == NULL)
7231 {
7232 set_syntax_error (_("invalid condition"));
7233 goto failure;
7234 }
7235 else if (operands[i] == AARCH64_OPND_COND1
7236 && (info->cond->value & 0xe) == 0xe)
7237 {
7238 /* Do not allow AL or NV. */
7239 set_default_error ();
7240 goto failure;
7241 }
7242 }
a06ea964
NC
7243 break;
7244
7245 case AARCH64_OPND_ADDR_ADRP:
7246 po_misc_or_fail (parse_adrp (&str));
7247 /* Clear the value as operand needs to be relocated. */
7248 info->imm.value = 0;
7249 break;
7250
7251 case AARCH64_OPND_ADDR_PCREL14:
7252 case AARCH64_OPND_ADDR_PCREL19:
7253 case AARCH64_OPND_ADDR_PCREL21:
7254 case AARCH64_OPND_ADDR_PCREL26:
73866052 7255 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
7256 if (!info->addr.pcrel)
7257 {
7258 set_syntax_error (_("invalid pc-relative address"));
7259 goto failure;
7260 }
7261 if (inst.gen_lit_pool
7262 && (opcode->iclass != loadlit || opcode->op == OP_PRFM_LIT))
7263 {
7264 /* Only permit "=value" in the literal load instructions.
7265 The literal will be generated by programmer_friendly_fixup. */
7266 set_syntax_error (_("invalid use of \"=immediate\""));
7267 goto failure;
7268 }
7269 if (inst.reloc.exp.X_op == O_symbol && find_reloc_table_entry (&str))
7270 {
7271 set_syntax_error (_("unrecognized relocation suffix"));
7272 goto failure;
7273 }
7274 if (inst.reloc.exp.X_op == O_constant && !inst.gen_lit_pool)
7275 {
7276 info->imm.value = inst.reloc.exp.X_add_number;
7277 inst.reloc.type = BFD_RELOC_UNUSED;
7278 }
7279 else
7280 {
7281 info->imm.value = 0;
f41aef5f
RE
7282 if (inst.reloc.type == BFD_RELOC_UNUSED)
7283 switch (opcode->iclass)
7284 {
7285 case compbranch:
7286 case condbranch:
7287 /* e.g. CBZ or B.COND */
7288 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
7289 inst.reloc.type = BFD_RELOC_AARCH64_BRANCH19;
7290 break;
7291 case testbranch:
7292 /* e.g. TBZ */
7293 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL14);
7294 inst.reloc.type = BFD_RELOC_AARCH64_TSTBR14;
7295 break;
7296 case branch_imm:
7297 /* e.g. B or BL */
7298 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL26);
7299 inst.reloc.type =
7300 (opcode->op == OP_BL) ? BFD_RELOC_AARCH64_CALL26
7301 : BFD_RELOC_AARCH64_JUMP26;
7302 break;
7303 case loadlit:
7304 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
7305 inst.reloc.type = BFD_RELOC_AARCH64_LD_LO19_PCREL;
7306 break;
7307 case pcreladdr:
7308 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL21);
7309 inst.reloc.type = BFD_RELOC_AARCH64_ADR_LO21_PCREL;
7310 break;
7311 default:
7312 gas_assert (0);
7313 abort ();
7314 }
a06ea964
NC
7315 inst.reloc.pc_rel = 1;
7316 }
7317 break;
51bb8593
VDN
7318 case AARCH64_OPND_RCPC3_ADDR_PREIND_WB:
7319 case AARCH64_OPND_RCPC3_ADDR_POSTIND:
7320 po_misc_or_fail (parse_address (&str, info));
7321 if (info->addr.writeback)
7322 {
7323 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7324 /* addr_off_p */ 1,
7325 /* need_libopcodes_p */ 1,
7326 /* skip_p */ 0);
7327 break;
7328 }
7329 set_syntax_error (_("invalid addressing mode"));
7330 goto failure;
7331 case AARCH64_OPND_RCPC3_ADDR_OPT_PREIND_WB:
7332 case AARCH64_OPND_RCPC3_ADDR_OPT_POSTIND:
7333 {
7334 char *start = str;
7335 /* First use the normal address-parsing routines, to get
7336 the usual syntax errors. */
7337 po_misc_or_fail (parse_address (&str, info));
7338 if ((operands[i] == AARCH64_OPND_RCPC3_ADDR_OPT_PREIND_WB
7339 && info->addr.writeback && info->addr.preind)
7340 || (operands[i] == AARCH64_OPND_RCPC3_ADDR_OPT_POSTIND
7341 && info->addr.writeback && info->addr.postind))
7342 {
7343 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7344 /* addr_off_p */ 1,
7345 /* need_libopcodes_p */ 1,
7346 /* skip_p */ 0);
a06ea964 7347
51bb8593
VDN
7348 break;
7349 }
7350 if (info->addr.pcrel || info->addr.offset.is_reg
7351 || !info->addr.preind || info->addr.postind
7352 || info->addr.writeback)
7353 {
7354 set_syntax_error (_("invalid addressing mode"));
7355 goto failure;
7356 }
7357 /* Then retry, matching the specific syntax of these addresses. */
7358 str = start;
7359 po_char_or_fail ('[');
7360 po_reg_or_fail (REG_TYPE_R64_SP);
7361 po_char_or_fail (']');
7362 break;
7363 }
a06ea964
NC
7364 case AARCH64_OPND_ADDR_SIMPLE:
7365 case AARCH64_OPND_SIMD_ADDR_SIMPLE:
e1b988bb
RS
7366 {
7367 /* [<Xn|SP>{, #<simm>}] */
7368 char *start = str;
7369 /* First use the normal address-parsing routines, to get
7370 the usual syntax errors. */
73866052 7371 po_misc_or_fail (parse_address (&str, info));
e1b988bb
RS
7372 if (info->addr.pcrel || info->addr.offset.is_reg
7373 || !info->addr.preind || info->addr.postind
550fd7bf 7374 || info->addr.writeback)
e1b988bb
RS
7375 {
7376 set_syntax_error (_("invalid addressing mode"));
7377 goto failure;
7378 }
7379
7380 /* Then retry, matching the specific syntax of these addresses. */
7381 str = start;
7382 po_char_or_fail ('[');
7383 po_reg_or_fail (REG_TYPE_R64_SP);
7384 /* Accept optional ", #0". */
7385 if (operands[i] == AARCH64_OPND_ADDR_SIMPLE
7386 && skip_past_char (&str, ','))
7387 {
7388 skip_past_char (&str, '#');
7389 if (! skip_past_char (&str, '0'))
7390 {
7391 set_fatal_syntax_error
7392 (_("the optional immediate offset can only be 0"));
7393 goto failure;
7394 }
7395 }
7396 po_char_or_fail (']');
7397 break;
7398 }
a06ea964
NC
7399
7400 case AARCH64_OPND_ADDR_REGOFF:
7401 /* [<Xn|SP>, <R><m>{, <extend> {<amount>}}] */
73866052 7402 po_misc_or_fail (parse_address (&str, info));
4df068de 7403 regoff_addr:
a06ea964
NC
7404 if (info->addr.pcrel || !info->addr.offset.is_reg
7405 || !info->addr.preind || info->addr.postind
7406 || info->addr.writeback)
7407 {
7408 set_syntax_error (_("invalid addressing mode"));
7409 goto failure;
7410 }
7411 if (!info->shifter.operator_present)
7412 {
7413 /* Default to LSL if not present. Libopcodes prefers shifter
7414 kind to be explicit. */
7415 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
7416 info->shifter.kind = AARCH64_MOD_LSL;
7417 }
7418 /* Qualifier to be deduced by libopcodes. */
7419 break;
7420
7421 case AARCH64_OPND_ADDR_SIMM7:
73866052 7422 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
7423 if (info->addr.pcrel || info->addr.offset.is_reg
7424 || (!info->addr.preind && !info->addr.postind))
7425 {
7426 set_syntax_error (_("invalid addressing mode"));
7427 goto failure;
7428 }
73866052
RS
7429 if (inst.reloc.type != BFD_RELOC_UNUSED)
7430 {
7431 set_syntax_error (_("relocation not allowed"));
7432 goto failure;
7433 }
a06ea964
NC
7434 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7435 /* addr_off_p */ 1,
7436 /* need_libopcodes_p */ 1,
7437 /* skip_p */ 0);
7438 break;
7439
7440 case AARCH64_OPND_ADDR_SIMM9:
7441 case AARCH64_OPND_ADDR_SIMM9_2:
fb3265b3
SD
7442 case AARCH64_OPND_ADDR_SIMM11:
7443 case AARCH64_OPND_ADDR_SIMM13:
73866052 7444 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
7445 if (info->addr.pcrel || info->addr.offset.is_reg
7446 || (!info->addr.preind && !info->addr.postind)
7447 || (operands[i] == AARCH64_OPND_ADDR_SIMM9_2
7448 && info->addr.writeback))
7449 {
7450 set_syntax_error (_("invalid addressing mode"));
7451 goto failure;
7452 }
7453 if (inst.reloc.type != BFD_RELOC_UNUSED)
7454 {
7455 set_syntax_error (_("relocation not allowed"));
7456 goto failure;
7457 }
7458 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7459 /* addr_off_p */ 1,
7460 /* need_libopcodes_p */ 1,
7461 /* skip_p */ 0);
7462 break;
7463
3f06e550 7464 case AARCH64_OPND_ADDR_SIMM10:
f42f1a1d 7465 case AARCH64_OPND_ADDR_OFFSET:
3f06e550
SN
7466 po_misc_or_fail (parse_address (&str, info));
7467 if (info->addr.pcrel || info->addr.offset.is_reg
7468 || !info->addr.preind || info->addr.postind)
7469 {
7470 set_syntax_error (_("invalid addressing mode"));
7471 goto failure;
7472 }
7473 if (inst.reloc.type != BFD_RELOC_UNUSED)
7474 {
7475 set_syntax_error (_("relocation not allowed"));
7476 goto failure;
7477 }
7478 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7479 /* addr_off_p */ 1,
7480 /* need_libopcodes_p */ 1,
7481 /* skip_p */ 0);
7482 break;
7483
51bb8593
VDN
7484 case AARCH64_OPND_RCPC3_ADDR_OFFSET:
7485 po_misc_or_fail (parse_address (&str, info));
7486 if (info->addr.pcrel || info->addr.offset.is_reg
7487 || !info->addr.preind || info->addr.postind
7488 || info->addr.writeback)
7489 {
7490 set_syntax_error (_("invalid addressing mode"));
7491 goto failure;
7492 }
7493 if (inst.reloc.type != BFD_RELOC_UNUSED)
7494 {
7495 set_syntax_error (_("relocation not allowed"));
7496 goto failure;
7497 }
7498 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
7499 /* addr_off_p */ 1,
7500 /* need_libopcodes_p */ 1,
7501 /* skip_p */ 0);
7502 break;
7503
a06ea964 7504 case AARCH64_OPND_ADDR_UIMM12:
73866052 7505 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
7506 if (info->addr.pcrel || info->addr.offset.is_reg
7507 || !info->addr.preind || info->addr.writeback)
7508 {
7509 set_syntax_error (_("invalid addressing mode"));
7510 goto failure;
7511 }
7512 if (inst.reloc.type == BFD_RELOC_UNUSED)
7513 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
4c562523
JW
7514 else if (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
7515 || (inst.reloc.type
7516 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12)
7517 || (inst.reloc.type
84f1b9fb
RL
7518 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC)
7519 || (inst.reloc.type
7520 == BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12)
7521 || (inst.reloc.type
7522 == BFD_RELOC_AARCH64_TLSLE_LDST_TPREL_LO12_NC))
a06ea964
NC
7523 inst.reloc.type = ldst_lo12_determine_real_reloc_type ();
7524 /* Leave qualifier to be determined by libopcodes. */
7525 break;
7526
7527 case AARCH64_OPND_SIMD_ADDR_POST:
7528 /* [<Xn|SP>], <Xm|#<amount>> */
73866052 7529 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
7530 if (!info->addr.postind || !info->addr.writeback)
7531 {
7532 set_syntax_error (_("invalid addressing mode"));
7533 goto failure;
7534 }
7535 if (!info->addr.offset.is_reg)
7536 {
7537 if (inst.reloc.exp.X_op == O_constant)
7538 info->addr.offset.imm = inst.reloc.exp.X_add_number;
7539 else
7540 {
7541 set_fatal_syntax_error
ab3b8fcf 7542 (_("writeback value must be an immediate constant"));
a06ea964
NC
7543 goto failure;
7544 }
7545 }
7546 /* No qualifier. */
7547 break;
7548
3dd032c5
PW
7549 case AARCH64_OPND_SME_SM_ZA:
7550 /* { SM | ZA } */
7551 if ((val = parse_sme_sm_za (&str)) == PARSE_FAIL)
7552 {
7553 set_syntax_error (_("unknown or missing PSTATE field name"));
7554 goto failure;
7555 }
7556 info->reg.regno = val;
7557 break;
7558
d3de0860 7559 case AARCH64_OPND_SME_PnT_Wm_imm:
2f4e3a2c 7560 if (!parse_dual_indexed_reg (&str, REG_TYPE_P,
b9ca3896 7561 &info->indexed_za, &qualifier, 0))
1d597b88
RS
7562 goto failure;
7563 info->qualifier = qualifier;
7564 break;
d3de0860 7565
582e12bf 7566 case AARCH64_OPND_SVE_ADDR_RI_S4x16:
8382113f 7567 case AARCH64_OPND_SVE_ADDR_RI_S4x32:
98907a70 7568 case AARCH64_OPND_SVE_ADDR_RI_S4xVL:
01a4d082 7569 case AARCH64_OPND_SME_ADDR_RI_U4xVL:
98907a70
RS
7570 case AARCH64_OPND_SVE_ADDR_RI_S4x2xVL:
7571 case AARCH64_OPND_SVE_ADDR_RI_S4x3xVL:
7572 case AARCH64_OPND_SVE_ADDR_RI_S4x4xVL:
7573 case AARCH64_OPND_SVE_ADDR_RI_S6xVL:
7574 case AARCH64_OPND_SVE_ADDR_RI_S9xVL:
4df068de
RS
7575 case AARCH64_OPND_SVE_ADDR_RI_U6:
7576 case AARCH64_OPND_SVE_ADDR_RI_U6x2:
7577 case AARCH64_OPND_SVE_ADDR_RI_U6x4:
7578 case AARCH64_OPND_SVE_ADDR_RI_U6x8:
98907a70
RS
7579 /* [X<n>{, #imm, MUL VL}]
7580 [X<n>{, #imm}]
4df068de
RS
7581 but recognizing SVE registers. */
7582 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7583 &offset_qualifier));
7584 if (base_qualifier != AARCH64_OPND_QLF_X)
7585 {
7586 set_syntax_error (_("invalid addressing mode"));
7587 goto failure;
7588 }
7589 sve_regimm:
7590 if (info->addr.pcrel || info->addr.offset.is_reg
7591 || !info->addr.preind || info->addr.writeback)
7592 {
7593 set_syntax_error (_("invalid addressing mode"));
7594 goto failure;
7595 }
7596 if (inst.reloc.type != BFD_RELOC_UNUSED
7597 || inst.reloc.exp.X_op != O_constant)
7598 {
7599 /* Make sure this has priority over
7600 "invalid addressing mode". */
7601 set_fatal_syntax_error (_("constant offset required"));
7602 goto failure;
7603 }
7604 info->addr.offset.imm = inst.reloc.exp.X_add_number;
7605 break;
7606
c8d59609
NC
7607 case AARCH64_OPND_SVE_ADDR_R:
7608 /* [<Xn|SP>{, <R><m>}]
7609 but recognizing SVE registers. */
7610 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7611 &offset_qualifier));
7612 if (offset_qualifier == AARCH64_OPND_QLF_NIL)
7613 {
7614 offset_qualifier = AARCH64_OPND_QLF_X;
7615 info->addr.offset.is_reg = 1;
7616 info->addr.offset.regno = 31;
7617 }
7618 else if (base_qualifier != AARCH64_OPND_QLF_X
7619 || offset_qualifier != AARCH64_OPND_QLF_X)
7620 {
7621 set_syntax_error (_("invalid addressing mode"));
7622 goto failure;
7623 }
7624 goto regoff_addr;
01a4d082 7625
4df068de
RS
7626 case AARCH64_OPND_SVE_ADDR_RR:
7627 case AARCH64_OPND_SVE_ADDR_RR_LSL1:
7628 case AARCH64_OPND_SVE_ADDR_RR_LSL2:
7629 case AARCH64_OPND_SVE_ADDR_RR_LSL3:
01a4d082 7630 case AARCH64_OPND_SVE_ADDR_RR_LSL4:
4df068de
RS
7631 case AARCH64_OPND_SVE_ADDR_RX:
7632 case AARCH64_OPND_SVE_ADDR_RX_LSL1:
7633 case AARCH64_OPND_SVE_ADDR_RX_LSL2:
7634 case AARCH64_OPND_SVE_ADDR_RX_LSL3:
7635 /* [<Xn|SP>, <R><m>{, lsl #<amount>}]
7636 but recognizing SVE registers. */
7637 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7638 &offset_qualifier));
7639 if (base_qualifier != AARCH64_OPND_QLF_X
7640 || offset_qualifier != AARCH64_OPND_QLF_X)
7641 {
7642 set_syntax_error (_("invalid addressing mode"));
7643 goto failure;
7644 }
7645 goto regoff_addr;
7646
7647 case AARCH64_OPND_SVE_ADDR_RZ:
7648 case AARCH64_OPND_SVE_ADDR_RZ_LSL1:
7649 case AARCH64_OPND_SVE_ADDR_RZ_LSL2:
7650 case AARCH64_OPND_SVE_ADDR_RZ_LSL3:
7651 case AARCH64_OPND_SVE_ADDR_RZ_XTW_14:
7652 case AARCH64_OPND_SVE_ADDR_RZ_XTW_22:
7653 case AARCH64_OPND_SVE_ADDR_RZ_XTW1_14:
7654 case AARCH64_OPND_SVE_ADDR_RZ_XTW1_22:
7655 case AARCH64_OPND_SVE_ADDR_RZ_XTW2_14:
7656 case AARCH64_OPND_SVE_ADDR_RZ_XTW2_22:
7657 case AARCH64_OPND_SVE_ADDR_RZ_XTW3_14:
7658 case AARCH64_OPND_SVE_ADDR_RZ_XTW3_22:
7659 /* [<Xn|SP>, Z<m>.D{, LSL #<amount>}]
7660 [<Xn|SP>, Z<m>.<T>, <extend> {#<amount>}] */
7661 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7662 &offset_qualifier));
7663 if (base_qualifier != AARCH64_OPND_QLF_X
7664 || (offset_qualifier != AARCH64_OPND_QLF_S_S
7665 && offset_qualifier != AARCH64_OPND_QLF_S_D))
7666 {
7667 set_syntax_error (_("invalid addressing mode"));
7668 goto failure;
7669 }
7670 info->qualifier = offset_qualifier;
7671 goto regoff_addr;
7672
c469c864
MM
7673 case AARCH64_OPND_SVE_ADDR_ZX:
7674 /* [Zn.<T>{, <Xm>}]. */
7675 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7676 &offset_qualifier));
7677 /* Things to check:
7678 base_qualifier either S_S or S_D
7679 offset_qualifier must be X
7680 */
7681 if ((base_qualifier != AARCH64_OPND_QLF_S_S
7682 && base_qualifier != AARCH64_OPND_QLF_S_D)
7683 || offset_qualifier != AARCH64_OPND_QLF_X)
7684 {
7685 set_syntax_error (_("invalid addressing mode"));
7686 goto failure;
7687 }
7688 info->qualifier = base_qualifier;
7689 if (!info->addr.offset.is_reg || info->addr.pcrel
7690 || !info->addr.preind || info->addr.writeback
7691 || info->shifter.operator_present != 0)
7692 {
7693 set_syntax_error (_("invalid addressing mode"));
7694 goto failure;
7695 }
7696 info->shifter.kind = AARCH64_MOD_LSL;
7697 break;
7698
7699
4df068de
RS
7700 case AARCH64_OPND_SVE_ADDR_ZI_U5:
7701 case AARCH64_OPND_SVE_ADDR_ZI_U5x2:
7702 case AARCH64_OPND_SVE_ADDR_ZI_U5x4:
7703 case AARCH64_OPND_SVE_ADDR_ZI_U5x8:
7704 /* [Z<n>.<T>{, #imm}] */
7705 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7706 &offset_qualifier));
7707 if (base_qualifier != AARCH64_OPND_QLF_S_S
7708 && base_qualifier != AARCH64_OPND_QLF_S_D)
7709 {
7710 set_syntax_error (_("invalid addressing mode"));
7711 goto failure;
7712 }
7713 info->qualifier = base_qualifier;
7714 goto sve_regimm;
7715
7716 case AARCH64_OPND_SVE_ADDR_ZZ_LSL:
7717 case AARCH64_OPND_SVE_ADDR_ZZ_SXTW:
7718 case AARCH64_OPND_SVE_ADDR_ZZ_UXTW:
7719 /* [Z<n>.<T>, Z<m>.<T>{, LSL #<amount>}]
7720 [Z<n>.D, Z<m>.D, <extend> {#<amount>}]
7721
7722 We don't reject:
7723
7724 [Z<n>.S, Z<m>.S, <extend> {#<amount>}]
7725
7726 here since we get better error messages by leaving it to
7727 the qualifier checking routines. */
7728 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
7729 &offset_qualifier));
7730 if ((base_qualifier != AARCH64_OPND_QLF_S_S
7731 && base_qualifier != AARCH64_OPND_QLF_S_D)
7732 || offset_qualifier != base_qualifier)
7733 {
7734 set_syntax_error (_("invalid addressing mode"));
7735 goto failure;
7736 }
7737 info->qualifier = base_qualifier;
7738 goto regoff_addr;
a06ea964 7739 case AARCH64_OPND_SYSREG:
9af8f671 7740 case AARCH64_OPND_SYSREG128:
7d02540a 7741 {
9af8f671 7742 bool sysreg128_p = operands[i] == AARCH64_OPND_SYSREG128;
7d02540a
TC
7743 uint32_t sysreg_flags;
7744 if ((val = parse_sys_reg (&str, aarch64_sys_regs_hsh, 1, 0,
9af8f671
VDN
7745 &sysreg_flags,
7746 sysreg128_p)) == PARSE_FAIL)
7d02540a
TC
7747 {
7748 set_syntax_error (_("unknown or missing system register name"));
7749 goto failure;
7750 }
7751 inst.base.operands[i].sysreg.value = val;
7752 inst.base.operands[i].sysreg.flags = sysreg_flags;
7753 break;
7754 }
a06ea964
NC
7755
7756 case AARCH64_OPND_PSTATEFIELD:
3dd032c5
PW
7757 {
7758 uint32_t sysreg_flags;
7759 if ((val = parse_sys_reg (&str, aarch64_pstatefield_hsh, 0, 1,
9af8f671 7760 &sysreg_flags, false)) == PARSE_FAIL)
3dd032c5
PW
7761 {
7762 set_syntax_error (_("unknown or missing PSTATE field name"));
7763 goto failure;
7764 }
7765 inst.base.operands[i].pstatefield = val;
7766 inst.base.operands[i].sysreg.flags = sysreg_flags;
7767 break;
7768 }
a06ea964
NC
7769
7770 case AARCH64_OPND_SYSREG_IC:
7771 inst.base.operands[i].sysins_op =
0796bfa4 7772 parse_sys_ins_reg (&str, aarch64_sys_regs_ic_hsh, false);
a06ea964 7773 goto sys_reg_ins;
2ac435d4 7774
a06ea964
NC
7775 case AARCH64_OPND_SYSREG_DC:
7776 inst.base.operands[i].sysins_op =
0796bfa4 7777 parse_sys_ins_reg (&str, aarch64_sys_regs_dc_hsh, false);
a06ea964 7778 goto sys_reg_ins;
2ac435d4 7779
a06ea964
NC
7780 case AARCH64_OPND_SYSREG_AT:
7781 inst.base.operands[i].sysins_op =
0796bfa4 7782 parse_sys_ins_reg (&str, aarch64_sys_regs_at_hsh, false);
a06ea964 7783 goto sys_reg_ins;
2ac435d4
SD
7784
7785 case AARCH64_OPND_SYSREG_SR:
7786 inst.base.operands[i].sysins_op =
0796bfa4 7787 parse_sys_ins_reg (&str, aarch64_sys_regs_sr_hsh, false);
2ac435d4
SD
7788 goto sys_reg_ins;
7789
a06ea964 7790 case AARCH64_OPND_SYSREG_TLBI:
0796bfa4
AC
7791 inst.base.operands[i].sysins_op =
7792 parse_sys_ins_reg (&str, aarch64_sys_regs_tlbi_hsh, false);
7793 goto sys_reg_ins;
7794
a9e2cefd 7795 case AARCH64_OPND_SYSREG_TLBIP:
a06ea964 7796 inst.base.operands[i].sysins_op =
0796bfa4 7797 parse_sys_ins_reg (&str, aarch64_sys_regs_tlbi_hsh, true);
dc1e8a47 7798 sys_reg_ins:
a06ea964
NC
7799 if (inst.base.operands[i].sysins_op == NULL)
7800 {
7801 set_fatal_syntax_error ( _("unknown or missing operation name"));
7802 goto failure;
7803 }
7804 break;
7805
7806 case AARCH64_OPND_BARRIER:
7807 case AARCH64_OPND_BARRIER_ISB:
7808 val = parse_barrier (&str);
7809 if (val != PARSE_FAIL
7810 && operands[i] == AARCH64_OPND_BARRIER_ISB && val != 0xf)
7811 {
7812 /* ISB only accepts options name 'sy'. */
7813 set_syntax_error
7814 (_("the specified option is not accepted in ISB"));
7815 /* Turn off backtrack as this optional operand is present. */
7816 backtrack_pos = 0;
7817 goto failure;
7818 }
fd195909
PW
7819 if (val != PARSE_FAIL
7820 && operands[i] == AARCH64_OPND_BARRIER)
7821 {
7822 /* Regular barriers accept options CRm (C0-C15).
7823 DSB nXS barrier variant accepts values > 15. */
59d5613e
PW
7824 if (val < 0 || val > 15)
7825 {
7826 set_syntax_error (_("the specified option is not accepted in DSB"));
7827 goto failure;
7828 }
fd195909 7829 }
a06ea964
NC
7830 /* This is an extension to accept a 0..15 immediate. */
7831 if (val == PARSE_FAIL)
7832 po_imm_or_fail (0, 15);
7833 info->barrier = aarch64_barrier_options + val;
7834 break;
7835
fd195909
PW
7836 case AARCH64_OPND_BARRIER_DSB_NXS:
7837 val = parse_barrier (&str);
7838 if (val != PARSE_FAIL)
7839 {
7840 /* DSB nXS barrier variant accept only <option>nXS qualifiers. */
7841 if (!(val == 16 || val == 20 || val == 24 || val == 28))
7842 {
7843 set_syntax_error (_("the specified option is not accepted in DSB"));
7844 /* Turn off backtrack as this optional operand is present. */
7845 backtrack_pos = 0;
7846 goto failure;
7847 }
7848 }
7849 else
7850 {
7851 /* DSB nXS barrier variant accept 5-bit unsigned immediate, with
7852 possible values 16, 20, 24 or 28 , encoded as val<3:2>. */
7853 if (! parse_constant_immediate (&str, &val, imm_reg_type))
7854 goto failure;
7855 if (!(val == 16 || val == 20 || val == 24 || val == 28))
7856 {
7857 set_syntax_error (_("immediate value must be 16, 20, 24, 28"));
7858 goto failure;
7859 }
7860 }
7861 /* Option index is encoded as 2-bit value in val<3:2>. */
7862 val = (val >> 2) - 4;
7863 info->barrier = aarch64_barrier_dsb_nxs_options + val;
7864 break;
7865
a06ea964
NC
7866 case AARCH64_OPND_PRFOP:
7867 val = parse_pldop (&str);
7868 /* This is an extension to accept a 0..31 immediate. */
7869 if (val == PARSE_FAIL)
7870 po_imm_or_fail (0, 31);
7871 inst.base.operands[i].prfop = aarch64_prfops + val;
7872 break;
7873
8ff42920
RS
7874 case AARCH64_OPND_RPRFMOP:
7875 po_enum_or_fail (aarch64_rprfmop_array);
7876 info->imm.value = val;
7877 break;
7878
1e6f4800 7879 case AARCH64_OPND_BARRIER_PSB:
c58f84d8 7880 if (!parse_hint_opt (opcode->name, &str, &(info->hint_option)))
1e6f4800
MW
7881 goto failure;
7882 break;
7883
cbd11b88
RS
7884 case AARCH64_OPND_SME_ZT0:
7885 po_reg_or_fail (REG_TYPE_ZT0);
7886 break;
7887
7888 case AARCH64_OPND_SME_ZT0_INDEX:
7889 reg = aarch64_reg_parse (&str, REG_TYPE_ZT0, &vectype);
7890 if (!reg || vectype.type != NT_invtype)
7891 goto failure;
7892 if (!(vectype.defined & NTA_HASINDEX))
7893 {
7894 set_syntax_error (_("missing register index"));
7895 goto failure;
7896 }
7897 info->imm.value = vectype.index;
7898 break;
7899
7900 case AARCH64_OPND_SME_ZT0_LIST:
7901 if (*str != '{')
7902 {
7903 set_expected_reglist_error (REG_TYPE_ZT0, parse_reg (&str));
7904 goto failure;
7905 }
7906 str++;
7907 if (!parse_typed_reg (&str, REG_TYPE_ZT0, &vectype, PTR_IN_REGLIST))
7908 goto failure;
7909 if (*str != '}')
7910 {
7911 set_syntax_error (_("expected '}' after ZT0"));
7912 goto failure;
7913 }
7914 str++;
7915 break;
7916
99e01a66
RS
7917 case AARCH64_OPND_SME_PNn3_INDEX1:
7918 case AARCH64_OPND_SME_PNn3_INDEX2:
7919 reg = aarch64_reg_parse (&str, REG_TYPE_PN, &vectype);
7920 if (!reg)
7921 goto failure;
7922 if (!(vectype.defined & NTA_HASINDEX))
7923 {
7924 set_syntax_error (_("missing register index"));
7925 goto failure;
7926 }
7927 info->reglane.regno = reg->number;
7928 info->reglane.index = vectype.index;
7929 if (vectype.type == NT_invtype)
7930 info->qualifier = AARCH64_OPND_QLF_NIL;
7931 else
7932 info->qualifier = vectype_to_qualifier (&vectype);
7933 break;
7934
c58f84d8
SP
7935 case AARCH64_OPND_BARRIER_GCSB:
7936 if (!parse_hint_opt (opcode->name, &str, &(info->hint_option)))
7937 goto failure;
7938 break;
7939
ff605452 7940 case AARCH64_OPND_BTI_TARGET:
c58f84d8 7941 if (!parse_hint_opt (opcode->name, &str, &(info->hint_option)))
ff605452
SD
7942 goto failure;
7943 break;
7944
971eda73
PW
7945 case AARCH64_OPND_SME_ZAda_2b:
7946 case AARCH64_OPND_SME_ZAda_3b:
b9ca3896 7947 reg = parse_reg_with_qual (&str, REG_TYPE_ZAT, &qualifier, 0);
8cde0b98 7948 if (!reg)
971eda73 7949 goto failure;
8cde0b98 7950 info->reg.regno = reg->number;
971eda73
PW
7951 info->qualifier = qualifier;
7952 break;
7953
7bb5f07c 7954 case AARCH64_OPND_SME_ZA_HV_idx_src:
d8773a8a 7955 case AARCH64_OPND_SME_ZA_HV_idx_srcxN:
7bb5f07c 7956 case AARCH64_OPND_SME_ZA_HV_idx_dest:
d8773a8a 7957 case AARCH64_OPND_SME_ZA_HV_idx_destxN:
01a4d082 7958 case AARCH64_OPND_SME_ZA_HV_idx_ldstr:
1d597b88
RS
7959 if (operands[i] == AARCH64_OPND_SME_ZA_HV_idx_ldstr
7960 ? !parse_sme_za_hv_tiles_operand_with_braces (&str,
7961 &info->indexed_za,
7962 &qualifier)
9d862382 7963 : !parse_dual_indexed_reg (&str, REG_TYPE_ZATHV,
b9ca3896 7964 &info->indexed_za, &qualifier, 0))
1d597b88
RS
7965 goto failure;
7966 info->qualifier = qualifier;
7967 break;
7bb5f07c 7968
1d597b88
RS
7969 case AARCH64_OPND_SME_list_of_64bit_tiles:
7970 val = parse_sme_list_of_64bit_tiles (&str);
7971 if (val == PARSE_FAIL)
7972 goto failure;
7973 info->imm.value = val;
7974 break;
1cad938d 7975
a8cb21aa 7976 case AARCH64_OPND_SME_ZA_array_off1x4:
ed429b33 7977 case AARCH64_OPND_SME_ZA_array_off2x2:
a8cb21aa 7978 case AARCH64_OPND_SME_ZA_array_off2x4:
d8773a8a
RS
7979 case AARCH64_OPND_SME_ZA_array_off3_0:
7980 case AARCH64_OPND_SME_ZA_array_off3_5:
ed429b33 7981 case AARCH64_OPND_SME_ZA_array_off3x2:
90cd80f8 7982 case AARCH64_OPND_SME_ZA_array_off4:
9d862382 7983 if (!parse_dual_indexed_reg (&str, REG_TYPE_ZA,
b9ca3896 7984 &info->indexed_za, &qualifier, 0))
1d597b88 7985 goto failure;
9d862382 7986 info->qualifier = qualifier;
1d597b88 7987 break;
01a4d082 7988
89e06ec1
SP
7989 case AARCH64_OPND_SME_ZA_array_vrsb_1:
7990 case AARCH64_OPND_SME_ZA_array_vrsh_1:
7991 case AARCH64_OPND_SME_ZA_array_vrss_1:
7992 case AARCH64_OPND_SME_ZA_array_vrsd_1:
7993 case AARCH64_OPND_SME_ZA_array_vrsb_2:
7994 case AARCH64_OPND_SME_ZA_array_vrsh_2:
7995 case AARCH64_OPND_SME_ZA_array_vrss_2:
7996 case AARCH64_OPND_SME_ZA_array_vrsd_2:
7997 if (!parse_dual_indexed_reg (&str, REG_TYPE_ZATHV,
7998 &info->indexed_za, &qualifier, 0))
7999 goto failure;
8000 info->qualifier = qualifier;
8001 break;
8002
8003
99e01a66
RS
8004 case AARCH64_OPND_SME_VLxN_10:
8005 case AARCH64_OPND_SME_VLxN_13:
8006 po_strict_enum_or_fail (aarch64_sme_vlxn_array);
8007 info->imm.value = val;
8008 break;
8009
6327658e
RS
8010 case AARCH64_OPND_MOPS_ADDR_Rd:
8011 case AARCH64_OPND_MOPS_ADDR_Rs:
8012 po_char_or_fail ('[');
8013 if (!parse_x0_to_x30 (&str, info))
8014 goto failure;
8015 po_char_or_fail (']');
8016 po_char_or_fail ('!');
8017 break;
8018
8019 case AARCH64_OPND_MOPS_WB_Rn:
8020 if (!parse_x0_to_x30 (&str, info))
8021 goto failure;
8022 po_char_or_fail ('!');
8023 break;
8024
6219f9da
VDN
8025 case AARCH64_OPND_LSE128_Rt:
8026 case AARCH64_OPND_LSE128_Rt2:
8027 po_int_fp_reg_or_fail (REG_TYPE_R_64);
8028 break;
8029
a06ea964
NC
8030 default:
8031 as_fatal (_("unhandled operand code %d"), operands[i]);
8032 }
8033
8034 /* If we get here, this operand was successfully parsed. */
8035 inst.base.operands[i].present = 1;
f89c290e
VDN
8036
8037 /* As instructions can have multiple optional operands, it is imporant to
8038 reset the backtrack_pos variable once we finish processing an operand
8039 successfully. */
8040 backtrack_pos = 0;
8041
a06ea964
NC
8042 continue;
8043
dc1e8a47 8044 failure:
a06ea964
NC
8045 /* The parse routine should already have set the error, but in case
8046 not, set a default one here. */
8047 if (! error_p ())
8048 set_default_error ();
8049
8050 if (! backtrack_pos)
8051 goto parse_operands_return;
8052
f4c51f60
JW
8053 {
8054 /* We reach here because this operand is marked as optional, and
8055 either no operand was supplied or the operand was supplied but it
8056 was syntactically incorrect. In the latter case we report an
8057 error. In the former case we perform a few more checks before
8058 dropping through to the code to insert the default operand. */
8059
8060 char *tmp = backtrack_pos;
8061 char endchar = END_OF_INSN;
8062
f4c51f60
JW
8063 skip_past_char (&tmp, ',');
8064
8065 if (*tmp != endchar)
8066 /* The user has supplied an operand in the wrong format. */
8067 goto parse_operands_return;
8068
8069 /* Make sure there is not a comma before the optional operand.
8070 For example the fifth operand of 'sys' is optional:
8071
8072 sys #0,c0,c0,#0, <--- wrong
8073 sys #0,c0,c0,#0 <--- correct. */
8074 if (comma_skipped_p && i && endchar == END_OF_INSN)
8075 {
8076 set_fatal_syntax_error
8077 (_("unexpected comma before the omitted optional operand"));
8078 goto parse_operands_return;
8079 }
8080 }
8081
a06ea964
NC
8082 /* Reaching here means we are dealing with an optional operand that is
8083 omitted from the assembly line. */
8084 gas_assert (optional_operand_p (opcode, i));
8085 info->present = 0;
8086 process_omitted_operand (operands[i], opcode, i, info);
8087
8088 /* Try again, skipping the optional operand at backtrack_pos. */
8089 str = backtrack_pos;
8090 backtrack_pos = 0;
8091
a06ea964
NC
8092 /* Clear any error record after the omitted optional operand has been
8093 successfully handled. */
8094 clear_error ();
8095 }
8096
8097 /* Check if we have parsed all the operands. */
8098 if (*str != '\0' && ! error_p ())
8099 {
8100 /* Set I to the index of the last present operand; this is
8101 for the purpose of diagnostics. */
8102 for (i -= 1; i >= 0 && !inst.base.operands[i].present; --i)
8103 ;
8104 set_fatal_syntax_error
8105 (_("unexpected characters following instruction"));
8106 }
8107
dc1e8a47 8108 parse_operands_return:
a06ea964
NC
8109
8110 if (error_p ())
8111 {
3e4525ca 8112 inst.parsing_error.index = i;
a06ea964 8113 DEBUG_TRACE ("parsing FAIL: %s - %s",
3e4525ca
RS
8114 operand_mismatch_kind_names[inst.parsing_error.kind],
8115 inst.parsing_error.error);
a06ea964
NC
8116 /* Record the operand error properly; this is useful when there
8117 are multiple instruction templates for a mnemonic name, so that
8118 later on, we can select the error that most closely describes
8119 the problem. */
3e4525ca 8120 record_operand_error_info (opcode, &inst.parsing_error);
5b7c81bd 8121 return false;
a06ea964
NC
8122 }
8123 else
8124 {
8125 DEBUG_TRACE ("parsing SUCCESS");
5b7c81bd 8126 return true;
a06ea964
NC
8127 }
8128}
8129
8130/* It does some fix-up to provide some programmer friendly feature while
8131 keeping the libopcodes happy, i.e. libopcodes only accepts
8132 the preferred architectural syntax.
8133 Return FALSE if there is any failure; otherwise return TRUE. */
8134
5b7c81bd 8135static bool
a06ea964
NC
8136programmer_friendly_fixup (aarch64_instruction *instr)
8137{
8138 aarch64_inst *base = &instr->base;
8139 const aarch64_opcode *opcode = base->opcode;
8140 enum aarch64_op op = opcode->op;
8141 aarch64_opnd_info *operands = base->operands;
8142
8143 DEBUG_TRACE ("enter");
8144
8145 switch (opcode->iclass)
8146 {
8147 case testbranch:
8148 /* TBNZ Xn|Wn, #uimm6, label
8149 Test and Branch Not Zero: conditionally jumps to label if bit number
8150 uimm6 in register Xn is not zero. The bit number implies the width of
8151 the register, which may be written and should be disassembled as Wn if
8152 uimm is less than 32. */
8153 if (operands[0].qualifier == AARCH64_OPND_QLF_W)
8154 {
8155 if (operands[1].imm.value >= 32)
8156 {
8157 record_operand_out_of_range_error (opcode, 1, _("immediate value"),
8158 0, 31);
5b7c81bd 8159 return false;
a06ea964
NC
8160 }
8161 operands[0].qualifier = AARCH64_OPND_QLF_X;
8162 }
8163 break;
8164 case loadlit:
8165 /* LDR Wt, label | =value
8166 As a convenience assemblers will typically permit the notation
8167 "=value" in conjunction with the pc-relative literal load instructions
8168 to automatically place an immediate value or symbolic address in a
8169 nearby literal pool and generate a hidden label which references it.
8170 ISREG has been set to 0 in the case of =value. */
8171 if (instr->gen_lit_pool
8172 && (op == OP_LDR_LIT || op == OP_LDRV_LIT || op == OP_LDRSW_LIT))
8173 {
8174 int size = aarch64_get_qualifier_esize (operands[0].qualifier);
8175 if (op == OP_LDRSW_LIT)
8176 size = 4;
8177 if (instr->reloc.exp.X_op != O_constant
67a32447 8178 && instr->reloc.exp.X_op != O_big
a06ea964
NC
8179 && instr->reloc.exp.X_op != O_symbol)
8180 {
8181 record_operand_error (opcode, 1,
8182 AARCH64_OPDE_FATAL_SYNTAX_ERROR,
8183 _("constant expression expected"));
5b7c81bd 8184 return false;
a06ea964
NC
8185 }
8186 if (! add_to_lit_pool (&instr->reloc.exp, size))
8187 {
8188 record_operand_error (opcode, 1,
8189 AARCH64_OPDE_OTHER_ERROR,
8190 _("literal pool insertion failed"));
5b7c81bd 8191 return false;
a06ea964
NC
8192 }
8193 }
8194 break;
a06ea964
NC
8195 case log_shift:
8196 case bitfield:
8197 /* UXT[BHW] Wd, Wn
8198 Unsigned Extend Byte|Halfword|Word: UXT[BH] is architectural alias
8199 for UBFM Wd,Wn,#0,#7|15, while UXTW is pseudo instruction which is
8200 encoded using ORR Wd, WZR, Wn (MOV Wd,Wn).
8201 A programmer-friendly assembler should accept a destination Xd in
8202 place of Wd, however that is not the preferred form for disassembly.
8203 */
8204 if ((op == OP_UXTB || op == OP_UXTH || op == OP_UXTW)
8205 && operands[1].qualifier == AARCH64_OPND_QLF_W
8206 && operands[0].qualifier == AARCH64_OPND_QLF_X)
8207 operands[0].qualifier = AARCH64_OPND_QLF_W;
8208 break;
8209
8210 case addsub_ext:
8211 {
8212 /* In the 64-bit form, the final register operand is written as Wm
8213 for all but the (possibly omitted) UXTX/LSL and SXTX
8214 operators.
8215 As a programmer-friendly assembler, we accept e.g.
8216 ADDS <Xd>, <Xn|SP>, <Xm>{, UXTB {#<amount>}} and change it to
8217 ADDS <Xd>, <Xn|SP>, <Wm>{, UXTB {#<amount>}}. */
8218 int idx = aarch64_operand_index (opcode->operands,
8219 AARCH64_OPND_Rm_EXT);
8220 gas_assert (idx == 1 || idx == 2);
8221 if (operands[0].qualifier == AARCH64_OPND_QLF_X
8222 && operands[idx].qualifier == AARCH64_OPND_QLF_X
8223 && operands[idx].shifter.kind != AARCH64_MOD_LSL
8224 && operands[idx].shifter.kind != AARCH64_MOD_UXTX
8225 && operands[idx].shifter.kind != AARCH64_MOD_SXTX)
8226 operands[idx].qualifier = AARCH64_OPND_QLF_W;
8227 }
8228 break;
8229
8230 default:
8231 break;
8232 }
8233
8234 DEBUG_TRACE ("exit with SUCCESS");
5b7c81bd 8235 return true;
a06ea964
NC
8236}
8237
5c47e525 8238/* Check for loads and stores that will cause unpredictable behavior. */
54a28c4c
JW
8239
8240static void
8241warn_unpredictable_ldst (aarch64_instruction *instr, char *str)
8242{
8243 aarch64_inst *base = &instr->base;
8244 const aarch64_opcode *opcode = base->opcode;
8245 const aarch64_opnd_info *opnds = base->operands;
8246 switch (opcode->iclass)
8247 {
8248 case ldst_pos:
8249 case ldst_imm9:
3f06e550 8250 case ldst_imm10:
54a28c4c
JW
8251 case ldst_unscaled:
8252 case ldst_unpriv:
5c47e525
RE
8253 /* Loading/storing the base register is unpredictable if writeback. */
8254 if ((aarch64_get_operand_class (opnds[0].type)
8255 == AARCH64_OPND_CLASS_INT_REG)
8256 && opnds[0].reg.regno == opnds[1].addr.base_regno
4bf8c6e8 8257 && opnds[1].addr.base_regno != REG_SP
69105ce4
SD
8258 /* Exempt STG/STZG/ST2G/STZ2G. */
8259 && !(opnds[1].type == AARCH64_OPND_ADDR_SIMM13)
54a28c4c 8260 && opnds[1].addr.writeback)
5c47e525 8261 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
54a28c4c 8262 break;
503ba600 8263
54a28c4c
JW
8264 case ldstpair_off:
8265 case ldstnapair_offs:
8266 case ldstpair_indexed:
5c47e525
RE
8267 /* Loading/storing the base register is unpredictable if writeback. */
8268 if ((aarch64_get_operand_class (opnds[0].type)
8269 == AARCH64_OPND_CLASS_INT_REG)
8270 && (opnds[0].reg.regno == opnds[2].addr.base_regno
8271 || opnds[1].reg.regno == opnds[2].addr.base_regno)
4bf8c6e8 8272 && opnds[2].addr.base_regno != REG_SP
fb3265b3
SD
8273 /* Exempt STGP. */
8274 && !(opnds[2].type == AARCH64_OPND_ADDR_SIMM11)
54a28c4c 8275 && opnds[2].addr.writeback)
5c47e525
RE
8276 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
8277 /* Load operations must load different registers. */
54a28c4c
JW
8278 if ((opcode->opcode & (1 << 22))
8279 && opnds[0].reg.regno == opnds[1].reg.regno)
8280 as_warn (_("unpredictable load of register pair -- `%s'"), str);
8281 break;
ee943970
RR
8282
8283 case ldstexcl:
ee943970
RR
8284 if ((aarch64_get_operand_class (opnds[0].type)
8285 == AARCH64_OPND_CLASS_INT_REG)
8286 && (aarch64_get_operand_class (opnds[1].type)
dd170203
TB
8287 == AARCH64_OPND_CLASS_INT_REG))
8288 {
8289 if ((opcode->opcode & (1 << 22)))
8290 {
8291 /* It is unpredictable if load-exclusive pair with Rt == Rt2. */
8292 if ((opcode->opcode & (1 << 21))
8293 && opnds[0].reg.regno == opnds[1].reg.regno)
8294 as_warn (_("unpredictable load of register pair -- `%s'"), str);
8295 }
8296 else
8297 {
8298 /* Store-Exclusive is unpredictable if Rt == Rs. */
8299 if (opnds[0].reg.regno == opnds[1].reg.regno)
8300 as_warn
8301 (_("unpredictable: identical transfer and status registers"
8302 " --`%s'"),str);
ee943970 8303
dd170203
TB
8304 if (opnds[0].reg.regno == opnds[2].reg.regno)
8305 {
8306 if (!(opcode->opcode & (1 << 21)))
8307 /* Store-Exclusive is unpredictable if Rn == Rs. */
8308 as_warn
8309 (_("unpredictable: identical base and status registers"
8310 " --`%s'"),str);
8311 else
8312 /* Store-Exclusive pair is unpredictable if Rt2 == Rs. */
8313 as_warn
8314 (_("unpredictable: "
8315 "identical transfer and status registers"
8316 " --`%s'"),str);
8317 }
8318
8319 /* Store-Exclusive pair is unpredictable if Rn == Rs. */
8320 if ((opcode->opcode & (1 << 21))
8321 && opnds[0].reg.regno == opnds[3].reg.regno
8322 && opnds[3].reg.regno != REG_SP)
8323 as_warn (_("unpredictable: identical base and status registers"
8324 " --`%s'"),str);
8325 }
8326 }
ee943970
RR
8327 break;
8328
54a28c4c
JW
8329 default:
8330 break;
8331 }
8332}
8333
4f5d2536
TC
8334static void
8335force_automatic_sequence_close (void)
8336{
a346bb24
RS
8337 struct aarch64_segment_info_type *tc_seg_info;
8338
8339 tc_seg_info = &seg_info (now_seg)->tc_segment_info_data;
8340 if (tc_seg_info->insn_sequence.instr)
4f5d2536 8341 {
a346bb24
RS
8342 as_warn_where (tc_seg_info->last_file, tc_seg_info->last_line,
8343 _("previous `%s' sequence has not been closed"),
8344 tc_seg_info->insn_sequence.instr->opcode->name);
8345 init_insn_sequence (NULL, &tc_seg_info->insn_sequence);
4f5d2536
TC
8346 }
8347}
8348
a06ea964
NC
8349/* A wrapper function to interface with libopcodes on encoding and
8350 record the error message if there is any.
8351
8352 Return TRUE on success; otherwise return FALSE. */
8353
5b7c81bd 8354static bool
a06ea964
NC
8355do_encode (const aarch64_opcode *opcode, aarch64_inst *instr,
8356 aarch64_insn *code)
8357{
8358 aarch64_operand_error error_info;
7d02540a 8359 memset (&error_info, '\0', sizeof (error_info));
a06ea964 8360 error_info.kind = AARCH64_OPDE_NIL;
7e84b55d 8361 if (aarch64_opcode_encode (opcode, instr, code, NULL, &error_info, insn_sequence)
7d02540a 8362 && !error_info.non_fatal)
5b7c81bd 8363 return true;
7d02540a
TC
8364
8365 gas_assert (error_info.kind != AARCH64_OPDE_NIL);
8366 record_operand_error_info (opcode, &error_info);
8367 return error_info.non_fatal;
a06ea964
NC
8368}
8369
8370#ifdef DEBUG_AARCH64
8371static inline void
8372dump_opcode_operands (const aarch64_opcode *opcode)
8373{
8374 int i = 0;
8375 while (opcode->operands[i] != AARCH64_OPND_NIL)
8376 {
8377 aarch64_verbose ("\t\t opnd%d: %s", i,
8378 aarch64_get_operand_name (opcode->operands[i])[0] != '\0'
8379 ? aarch64_get_operand_name (opcode->operands[i])
8380 : aarch64_get_operand_desc (opcode->operands[i]));
8381 ++i;
8382 }
8383}
8384#endif /* DEBUG_AARCH64 */
8385
8386/* This is the guts of the machine-dependent assembler. STR points to a
8387 machine dependent instruction. This function is supposed to emit
8388 the frags/bytes it assembles to. */
8389
8390void
8391md_assemble (char *str)
8392{
a06ea964 8393 templates *template;
6c2ede01 8394 const aarch64_opcode *opcode;
a346bb24 8395 struct aarch64_segment_info_type *tc_seg_info;
a06ea964
NC
8396 aarch64_inst *inst_base;
8397 unsigned saved_cond;
8398
8399 /* Align the previous label if needed. */
8400 if (last_label_seen != NULL)
8401 {
8402 symbol_set_frag (last_label_seen, frag_now);
8403 S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
8404 S_SET_SEGMENT (last_label_seen, now_seg);
8405 }
8406
7e84b55d 8407 /* Update the current insn_sequence from the segment. */
a346bb24
RS
8408 tc_seg_info = &seg_info (now_seg)->tc_segment_info_data;
8409 insn_sequence = &tc_seg_info->insn_sequence;
8410 tc_seg_info->last_file = as_where (&tc_seg_info->last_line);
7e84b55d 8411
a06ea964
NC
8412 inst.reloc.type = BFD_RELOC_UNUSED;
8413
8414 DEBUG_TRACE ("\n\n");
8415 DEBUG_TRACE ("==============================");
8416 DEBUG_TRACE ("Enter md_assemble with %s", str);
8417
2dd3146b
RS
8418 /* Scan up to the end of the mnemonic, which must end in whitespace,
8419 '.', or end of string. */
8420 char *p = str;
8421 char *dot = 0;
8422 for (; is_part_of_name (*p); p++)
8423 if (*p == '.' && !dot)
8424 dot = p;
8425
8426 if (p == str)
8427 {
8428 as_bad (_("unknown mnemonic -- `%s'"), str);
8429 return;
8430 }
8431
8432 if (!dot && create_register_alias (str, p))
8433 return;
8434
8435 template = opcode_lookup (str, dot, p);
a06ea964
NC
8436 if (!template)
8437 {
2dd3146b
RS
8438 as_bad (_("unknown mnemonic `%s' -- `%s'"), get_mnemonic_name (str),
8439 str);
a06ea964
NC
8440 return;
8441 }
8442
8443 skip_whitespace (p);
8444 if (*p == ',')
8445 {
8446 as_bad (_("unexpected comma after the mnemonic name `%s' -- `%s'"),
8447 get_mnemonic_name (str), str);
8448 return;
8449 }
8450
8451 init_operand_error_report ();
8452
eb9d6cc9
RL
8453 /* Sections are assumed to start aligned. In executable section, there is no
8454 MAP_DATA symbol pending. So we only align the address during
8455 MAP_DATA --> MAP_INSN transition.
8456 For other sections, this is not guaranteed. */
8457 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
8458 if (!need_pass_2 && subseg_text_p (now_seg) && mapstate == MAP_DATA)
8459 frag_align_code (2, 0);
8460
a06ea964
NC
8461 saved_cond = inst.cond;
8462 reset_aarch64_instruction (&inst);
8463 inst.cond = saved_cond;
8464
8465 /* Iterate through all opcode entries with the same mnemonic name. */
8466 do
8467 {
8468 opcode = template->opcode;
8469
8470 DEBUG_TRACE ("opcode %s found", opcode->name);
8471#ifdef DEBUG_AARCH64
8472 if (debug_dump)
8473 dump_opcode_operands (opcode);
8474#endif /* DEBUG_AARCH64 */
8475
a06ea964
NC
8476 mapping_state (MAP_INSN);
8477
8478 inst_base = &inst.base;
8479 inst_base->opcode = opcode;
8480
8481 /* Truly conditionally executed instructions, e.g. b.cond. */
8482 if (opcode->flags & F_COND)
8483 {
8484 gas_assert (inst.cond != COND_ALWAYS);
8485 inst_base->cond = get_cond_from_value (inst.cond);
8486 DEBUG_TRACE ("condition found %s", inst_base->cond->names[0]);
8487 }
8488 else if (inst.cond != COND_ALWAYS)
8489 {
8490 /* It shouldn't arrive here, where the assembly looks like a
8491 conditional instruction but the found opcode is unconditional. */
8492 gas_assert (0);
8493 continue;
8494 }
8495
8496 if (parse_operands (p, opcode)
8497 && programmer_friendly_fixup (&inst)
8498 && do_encode (inst_base->opcode, &inst.base, &inst_base->value))
8499 {
3f06bfce 8500 /* Check that this instruction is supported for this CPU. */
199cfcc4 8501 if (!aarch64_cpu_supports_inst_p (cpu_variant, inst_base))
3f06bfce
YZ
8502 {
8503 as_bad (_("selected processor does not support `%s'"), str);
8504 return;
8505 }
8506
54a28c4c
JW
8507 warn_unpredictable_ldst (&inst, str);
8508
a06ea964
NC
8509 if (inst.reloc.type == BFD_RELOC_UNUSED
8510 || !inst.reloc.need_libopcodes_p)
8511 output_inst (NULL);
8512 else
8513 {
8514 /* If there is relocation generated for the instruction,
8515 store the instruction information for the future fix-up. */
8516 struct aarch64_inst *copy;
8517 gas_assert (inst.reloc.type != BFD_RELOC_UNUSED);
325801bd 8518 copy = XNEW (struct aarch64_inst);
a06ea964
NC
8519 memcpy (copy, &inst.base, sizeof (struct aarch64_inst));
8520 output_inst (copy);
8521 }
7d02540a
TC
8522
8523 /* Issue non-fatal messages if any. */
5b7c81bd 8524 output_operand_error_report (str, true);
a06ea964
NC
8525 return;
8526 }
8527
8528 template = template->next;
8529 if (template != NULL)
8530 {
8531 reset_aarch64_instruction (&inst);
8532 inst.cond = saved_cond;
8533 }
8534 }
8535 while (template != NULL);
8536
8537 /* Issue the error messages if any. */
5b7c81bd 8538 output_operand_error_report (str, false);
a06ea964
NC
8539}
8540
8541/* Various frobbings of labels and their addresses. */
8542
8543void
8544aarch64_start_line_hook (void)
8545{
8546 last_label_seen = NULL;
8547}
8548
8549void
8550aarch64_frob_label (symbolS * sym)
8551{
8552 last_label_seen = sym;
8553
8554 dwarf2_emit_label (sym);
8555}
8556
4f5d2536
TC
8557void
8558aarch64_frob_section (asection *sec ATTRIBUTE_UNUSED)
8559{
8560 /* Check to see if we have a block to close. */
8561 force_automatic_sequence_close ();
8562}
8563
a06ea964
NC
8564int
8565aarch64_data_in_code (void)
8566{
d34049e8 8567 if (startswith (input_line_pointer + 1, "data:"))
a06ea964
NC
8568 {
8569 *input_line_pointer = '/';
8570 input_line_pointer += 5;
8571 *input_line_pointer = 0;
8572 return 1;
8573 }
8574
8575 return 0;
8576}
8577
8578char *
8579aarch64_canonicalize_symbol_name (char *name)
8580{
8581 int len;
8582
8583 if ((len = strlen (name)) > 5 && streq (name + len - 5, "/data"))
8584 *(name + len - 5) = 0;
8585
8586 return name;
8587}
8588\f
8589/* Table of all register names defined by default. The user can
8590 define additional names with .req. Note that all register names
8591 should appear in both upper and lowercase variants. Some registers
8592 also have mixed-case names. */
8593
5b7c81bd
AM
8594#define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, true }
8595#define REGDEF_ALIAS(s, n, t) { #s, n, REG_TYPE_##t, false}
a06ea964 8596#define REGNUM(p,n,t) REGDEF(p##n, n, t)
7bb5f07c 8597#define REGNUMS(p,n,s,t) REGDEF(p##n##s, n, t)
f11ad6bc 8598#define REGSET16(p,t) \
a06ea964
NC
8599 REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
8600 REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
8601 REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
f11ad6bc 8602 REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
7bb5f07c
PW
8603#define REGSET16S(p,s,t) \
8604 REGNUMS(p, 0,s,t), REGNUMS(p, 1,s,t), REGNUMS(p, 2,s,t), REGNUMS(p, 3,s,t), \
8605 REGNUMS(p, 4,s,t), REGNUMS(p, 5,s,t), REGNUMS(p, 6,s,t), REGNUMS(p, 7,s,t), \
8606 REGNUMS(p, 8,s,t), REGNUMS(p, 9,s,t), REGNUMS(p,10,s,t), REGNUMS(p,11,s,t), \
8607 REGNUMS(p,12,s,t), REGNUMS(p,13,s,t), REGNUMS(p,14,s,t), REGNUMS(p,15,s,t)
f11ad6bc
RS
8608#define REGSET31(p,t) \
8609 REGSET16(p, t), \
a06ea964
NC
8610 REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
8611 REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
8612 REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
8613 REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t)
8614#define REGSET(p,t) \
8615 REGSET31(p,t), REGNUM(p,31,t)
8616
8617/* These go into aarch64_reg_hsh hash-table. */
8618static const reg_entry reg_names[] = {
8619 /* Integer registers. */
8620 REGSET31 (x, R_64), REGSET31 (X, R_64),
8621 REGSET31 (w, R_32), REGSET31 (W, R_32),
8622
8975f864 8623 REGDEF_ALIAS (ip0, 16, R_64), REGDEF_ALIAS (IP0, 16, R_64),
f10e937a 8624 REGDEF_ALIAS (ip1, 17, R_64), REGDEF_ALIAS (IP1, 17, R_64),
8975f864
RR
8625 REGDEF_ALIAS (fp, 29, R_64), REGDEF_ALIAS (FP, 29, R_64),
8626 REGDEF_ALIAS (lr, 30, R_64), REGDEF_ALIAS (LR, 30, R_64),
a06ea964
NC
8627 REGDEF (wsp, 31, SP_32), REGDEF (WSP, 31, SP_32),
8628 REGDEF (sp, 31, SP_64), REGDEF (SP, 31, SP_64),
8629
2f4e3a2c
RS
8630 REGDEF (wzr, 31, ZR_32), REGDEF (WZR, 31, ZR_32),
8631 REGDEF (xzr, 31, ZR_64), REGDEF (XZR, 31, ZR_64),
a06ea964 8632
a06ea964
NC
8633 /* Floating-point single precision registers. */
8634 REGSET (s, FP_S), REGSET (S, FP_S),
8635
8636 /* Floating-point double precision registers. */
8637 REGSET (d, FP_D), REGSET (D, FP_D),
8638
8639 /* Floating-point half precision registers. */
8640 REGSET (h, FP_H), REGSET (H, FP_H),
8641
8642 /* Floating-point byte precision registers. */
8643 REGSET (b, FP_B), REGSET (B, FP_B),
8644
8645 /* Floating-point quad precision registers. */
8646 REGSET (q, FP_Q), REGSET (Q, FP_Q),
8647
8648 /* FP/SIMD registers. */
2f4e3a2c 8649 REGSET (v, V), REGSET (V, V),
f11ad6bc
RS
8650
8651 /* SVE vector registers. */
2f4e3a2c 8652 REGSET (z, Z), REGSET (Z, Z),
f11ad6bc 8653
503fae12 8654 /* SVE predicate(-as-mask) registers. */
2f4e3a2c 8655 REGSET16 (p, P), REGSET16 (P, P),
971eda73 8656
503fae12
RS
8657 /* SVE predicate-as-counter registers. */
8658 REGSET16 (pn, PN), REGSET16 (PN, PN),
8659
e9e1ddbb
RS
8660 /* SME ZA. We model this as a register because it acts syntactically
8661 like ZA0H, supporting qualifier suffixes and indexing. */
8662 REGDEF (za, 0, ZA), REGDEF (ZA, 0, ZA),
8663
971eda73 8664 /* SME ZA tile registers. */
fd855fbb 8665 REGSET16 (za, ZAT), REGSET16 (ZA, ZAT),
7bb5f07c
PW
8666
8667 /* SME ZA tile registers (horizontal slice). */
fd855fbb 8668 REGSET16S (za, h, ZATH), REGSET16S (ZA, H, ZATH),
7bb5f07c
PW
8669
8670 /* SME ZA tile registers (vertical slice). */
cbd11b88
RS
8671 REGSET16S (za, v, ZATV), REGSET16S (ZA, V, ZATV),
8672
8673 /* SME2 ZT0. */
8674 REGDEF (zt0, 0, ZT0), REGDEF (ZT0, 0, ZT0)
a06ea964
NC
8675};
8676
8677#undef REGDEF
8975f864 8678#undef REGDEF_ALIAS
a06ea964 8679#undef REGNUM
f11ad6bc
RS
8680#undef REGSET16
8681#undef REGSET31
a06ea964
NC
8682#undef REGSET
8683
8684#define N 1
8685#define n 0
8686#define Z 1
8687#define z 0
8688#define C 1
8689#define c 0
8690#define V 1
8691#define v 0
8692#define B(a,b,c,d) (((a) << 3) | ((b) << 2) | ((c) << 1) | (d))
8693static const asm_nzcv nzcv_names[] = {
8694 {"nzcv", B (n, z, c, v)},
8695 {"nzcV", B (n, z, c, V)},
8696 {"nzCv", B (n, z, C, v)},
8697 {"nzCV", B (n, z, C, V)},
8698 {"nZcv", B (n, Z, c, v)},
8699 {"nZcV", B (n, Z, c, V)},
8700 {"nZCv", B (n, Z, C, v)},
8701 {"nZCV", B (n, Z, C, V)},
8702 {"Nzcv", B (N, z, c, v)},
8703 {"NzcV", B (N, z, c, V)},
8704 {"NzCv", B (N, z, C, v)},
8705 {"NzCV", B (N, z, C, V)},
8706 {"NZcv", B (N, Z, c, v)},
8707 {"NZcV", B (N, Z, c, V)},
8708 {"NZCv", B (N, Z, C, v)},
8709 {"NZCV", B (N, Z, C, V)}
8710};
8711
8712#undef N
8713#undef n
8714#undef Z
8715#undef z
8716#undef C
8717#undef c
8718#undef V
8719#undef v
8720#undef B
8721\f
8722/* MD interface: bits in the object file. */
8723
8724/* Turn an integer of n bytes (in val) into a stream of bytes appropriate
8725 for use in the a.out file, and stores them in the array pointed to by buf.
8726 This knows about the endian-ness of the target machine and does
8727 THE RIGHT THING, whatever it is. Possible values for n are 1 (byte)
8728 2 (short) and 4 (long) Floating numbers are put out as a series of
8729 LITTLENUMS (shorts, here at least). */
8730
8731void
8732md_number_to_chars (char *buf, valueT val, int n)
8733{
8734 if (target_big_endian)
8735 number_to_chars_bigendian (buf, val, n);
8736 else
8737 number_to_chars_littleendian (buf, val, n);
8738}
8739
8740/* MD interface: Sections. */
8741
8742/* Estimate the size of a frag before relaxing. Assume everything fits in
8743 4 bytes. */
8744
8745int
8746md_estimate_size_before_relax (fragS * fragp, segT segtype ATTRIBUTE_UNUSED)
8747{
8748 fragp->fr_var = 4;
8749 return 4;
8750}
8751
8752/* Round up a section size to the appropriate boundary. */
8753
8754valueT
8755md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
8756{
8757 return size;
8758}
8759
8760/* This is called from HANDLE_ALIGN in write.c. Fill in the contents
f803aa8e
DPT
8761 of an rs_align_code fragment.
8762
8763 Here we fill the frag with the appropriate info for padding the
8764 output stream. The resulting frag will consist of a fixed (fr_fix)
8765 and of a repeating (fr_var) part.
8766
8767 The fixed content is always emitted before the repeating content and
8768 these two parts are used as follows in constructing the output:
8769 - the fixed part will be used to align to a valid instruction word
8770 boundary, in case that we start at a misaligned address; as no
8771 executable instruction can live at the misaligned location, we
8772 simply fill with zeros;
8773 - the variable part will be used to cover the remaining padding and
8774 we fill using the AArch64 NOP instruction.
8775
8776 Note that the size of a RS_ALIGN_CODE fragment is always 7 to provide
8777 enough storage space for up to 3 bytes for padding the back to a valid
8778 instruction alignment and exactly 4 bytes to store the NOP pattern. */
a06ea964
NC
8779
8780void
8781aarch64_handle_align (fragS * fragP)
8782{
8783 /* NOP = d503201f */
8784 /* AArch64 instructions are always little-endian. */
d9235011 8785 static unsigned char const aarch64_noop[4] = { 0x1f, 0x20, 0x03, 0xd5 };
a06ea964
NC
8786
8787 int bytes, fix, noop_size;
8788 char *p;
a06ea964
NC
8789
8790 if (fragP->fr_type != rs_align_code)
8791 return;
8792
8793 bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
8794 p = fragP->fr_literal + fragP->fr_fix;
a06ea964
NC
8795
8796#ifdef OBJ_ELF
8797 gas_assert (fragP->tc_frag_data.recorded);
8798#endif
8799
a06ea964 8800 noop_size = sizeof (aarch64_noop);
a06ea964 8801
f803aa8e
DPT
8802 fix = bytes & (noop_size - 1);
8803 if (fix)
a06ea964 8804 {
c60b3806 8805#if defined OBJ_ELF || defined OBJ_COFF
a06ea964
NC
8806 insert_data_mapping_symbol (MAP_INSN, fragP->fr_fix, fragP, fix);
8807#endif
8808 memset (p, 0, fix);
8809 p += fix;
f803aa8e 8810 fragP->fr_fix += fix;
a06ea964
NC
8811 }
8812
f803aa8e
DPT
8813 if (noop_size)
8814 memcpy (p, aarch64_noop, noop_size);
8815 fragP->fr_var = noop_size;
a06ea964
NC
8816}
8817
8818/* Perform target specific initialisation of a frag.
8819 Note - despite the name this initialisation is not done when the frag
8820 is created, but only when its type is assigned. A frag can be created
8821 and used a long time before its type is set, so beware of assuming that
33eaf5de 8822 this initialisation is performed first. */
a06ea964
NC
8823
8824#ifndef OBJ_ELF
8825void
8826aarch64_init_frag (fragS * fragP ATTRIBUTE_UNUSED,
8827 int max_chars ATTRIBUTE_UNUSED)
8828{
8829}
8830
8831#else /* OBJ_ELF is defined. */
8832void
8833aarch64_init_frag (fragS * fragP, int max_chars)
8834{
8835 /* Record a mapping symbol for alignment frags. We will delete this
8836 later if the alignment ends up empty. */
8837 if (!fragP->tc_frag_data.recorded)
c7ad08e6
RL
8838 fragP->tc_frag_data.recorded = 1;
8839
e8d84ca1
NC
8840 /* PR 21809: Do not set a mapping state for debug sections
8841 - it just confuses other tools. */
fd361982 8842 if (bfd_section_flags (now_seg) & SEC_DEBUGGING)
e8d84ca1
NC
8843 return;
8844
c7ad08e6 8845 switch (fragP->fr_type)
a06ea964 8846 {
c7ad08e6
RL
8847 case rs_align_test:
8848 case rs_fill:
8849 mapping_state_2 (MAP_DATA, max_chars);
8850 break;
7ea12e5c
NC
8851 case rs_align:
8852 /* PR 20364: We can get alignment frags in code sections,
8853 so do not just assume that we should use the MAP_DATA state. */
8854 mapping_state_2 (subseg_text_p (now_seg) ? MAP_INSN : MAP_DATA, max_chars);
8855 break;
c7ad08e6
RL
8856 case rs_align_code:
8857 mapping_state_2 (MAP_INSN, max_chars);
8858 break;
8859 default:
8860 break;
a06ea964
NC
8861 }
8862}
b52c4ee4 8863
3e3e792a 8864/* Whether SFrame stack trace info is supported. */
b52c4ee4
IB
8865
8866bool
8867aarch64_support_sframe_p (void)
8868{
8869 /* At this time, SFrame is supported for aarch64 only. */
8870 return (aarch64_abi == AARCH64_ABI_LP64);
8871}
8872
8873/* Specify if RA tracking is needed. */
8874
8875bool
8876aarch64_sframe_ra_tracking_p (void)
8877{
8878 return true;
8879}
8880
8881/* Specify the fixed offset to recover RA from CFA.
8882 (useful only when RA tracking is not needed). */
8883
8884offsetT
8885aarch64_sframe_cfa_ra_offset (void)
8886{
8887 return (offsetT) SFRAME_CFA_FIXED_RA_INVALID;
8888}
8889
8890/* Get the abi/arch indentifier for SFrame. */
8891
8892unsigned char
8893aarch64_sframe_get_abi_arch (void)
8894{
8895 unsigned char sframe_abi_arch = 0;
8896
8897 if (aarch64_support_sframe_p ())
8898 {
8899 sframe_abi_arch = target_big_endian
8900 ? SFRAME_ABI_AARCH64_ENDIAN_BIG
8901 : SFRAME_ABI_AARCH64_ENDIAN_LITTLE;
8902 }
8903
8904 return sframe_abi_arch;
8905}
8906
c60b3806 8907#endif /* OBJ_ELF */
a06ea964
NC
8908\f
8909/* Initialize the DWARF-2 unwind information for this procedure. */
8910
8911void
8912tc_aarch64_frame_initial_instructions (void)
8913{
8914 cfi_add_CFA_def_cfa (REG_SP, 0);
8915}
a06ea964
NC
8916
8917/* Convert REGNAME to a DWARF-2 register number. */
8918
8919int
8920tc_aarch64_regname_to_dw2regnum (char *regname)
8921{
8922 const reg_entry *reg = parse_reg (&regname);
8923 if (reg == NULL)
8924 return -1;
8925
8926 switch (reg->type)
8927 {
8928 case REG_TYPE_SP_32:
8929 case REG_TYPE_SP_64:
8930 case REG_TYPE_R_32:
8931 case REG_TYPE_R_64:
a2cac51c
RH
8932 return reg->number;
8933
a06ea964
NC
8934 case REG_TYPE_FP_B:
8935 case REG_TYPE_FP_H:
8936 case REG_TYPE_FP_S:
8937 case REG_TYPE_FP_D:
8938 case REG_TYPE_FP_Q:
a2cac51c
RH
8939 return reg->number + 64;
8940
a06ea964
NC
8941 default:
8942 break;
8943 }
8944 return -1;
8945}
8946
cec5225b
YZ
8947/* Implement DWARF2_ADDR_SIZE. */
8948
8949int
8950aarch64_dwarf2_addr_size (void)
8951{
cec5225b
YZ
8952 if (ilp32_p)
8953 return 4;
c60b3806
JT
8954 else if (llp64_p)
8955 return 8;
cec5225b
YZ
8956 return bfd_arch_bits_per_address (stdoutput) / 8;
8957}
8958
a06ea964
NC
8959/* MD interface: Symbol and relocation handling. */
8960
8961/* Return the address within the segment that a PC-relative fixup is
8962 relative to. For AArch64 PC-relative fixups applied to instructions
8963 are generally relative to the location plus AARCH64_PCREL_OFFSET bytes. */
8964
8965long
8966md_pcrel_from_section (fixS * fixP, segT seg)
8967{
8968 offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
8969
8970 /* If this is pc-relative and we are going to emit a relocation
8971 then we just want to put out any pipeline compensation that the linker
8972 will need. Otherwise we want to use the calculated base. */
8973 if (fixP->fx_pcrel
8974 && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
8975 || aarch64_force_relocation (fixP)))
8976 base = 0;
8977
8978 /* AArch64 should be consistent for all pc-relative relocations. */
8979 return base + AARCH64_PCREL_OFFSET;
8980}
8981
8982/* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
8983 Otherwise we have no need to default values of symbols. */
8984
8985symbolS *
8986md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
8987{
8988#ifdef OBJ_ELF
8989 if (name[0] == '_' && name[1] == 'G'
8990 && streq (name, GLOBAL_OFFSET_TABLE_NAME))
8991 {
8992 if (!GOT_symbol)
8993 {
8994 if (symbol_find (name))
8995 as_bad (_("GOT already in the symbol table"));
8996
8997 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 8998 &zero_address_frag, 0);
a06ea964
NC
8999 }
9000
9001 return GOT_symbol;
9002 }
9003#endif
9004
9005 return 0;
9006}
9007
9008/* Return non-zero if the indicated VALUE has overflowed the maximum
9009 range expressible by a unsigned number with the indicated number of
9010 BITS. */
9011
5b7c81bd 9012static bool
a06ea964
NC
9013unsigned_overflow (valueT value, unsigned bits)
9014{
9015 valueT lim;
9016 if (bits >= sizeof (valueT) * 8)
5b7c81bd 9017 return false;
a06ea964
NC
9018 lim = (valueT) 1 << bits;
9019 return (value >= lim);
9020}
9021
9022
9023/* Return non-zero if the indicated VALUE has overflowed the maximum
9024 range expressible by an signed number with the indicated number of
9025 BITS. */
9026
5b7c81bd 9027static bool
a06ea964
NC
9028signed_overflow (offsetT value, unsigned bits)
9029{
9030 offsetT lim;
9031 if (bits >= sizeof (offsetT) * 8)
5b7c81bd 9032 return false;
a06ea964
NC
9033 lim = (offsetT) 1 << (bits - 1);
9034 return (value < -lim || value >= lim);
9035}
9036
9037/* Given an instruction in *INST, which is expected to be a scaled, 12-bit,
9038 unsigned immediate offset load/store instruction, try to encode it as
9039 an unscaled, 9-bit, signed immediate offset load/store instruction.
9040 Return TRUE if it is successful; otherwise return FALSE.
9041
9042 As a programmer-friendly assembler, LDUR/STUR instructions can be generated
9043 in response to the standard LDR/STR mnemonics when the immediate offset is
9044 unambiguous, i.e. when it is negative or unaligned. */
9045
5b7c81bd 9046static bool
a06ea964
NC
9047try_to_encode_as_unscaled_ldst (aarch64_inst *instr)
9048{
9049 int idx;
9050 enum aarch64_op new_op;
9051 const aarch64_opcode *new_opcode;
9052
9053 gas_assert (instr->opcode->iclass == ldst_pos);
9054
9055 switch (instr->opcode->op)
9056 {
9057 case OP_LDRB_POS:new_op = OP_LDURB; break;
9058 case OP_STRB_POS: new_op = OP_STURB; break;
9059 case OP_LDRSB_POS: new_op = OP_LDURSB; break;
9060 case OP_LDRH_POS: new_op = OP_LDURH; break;
9061 case OP_STRH_POS: new_op = OP_STURH; break;
9062 case OP_LDRSH_POS: new_op = OP_LDURSH; break;
9063 case OP_LDR_POS: new_op = OP_LDUR; break;
9064 case OP_STR_POS: new_op = OP_STUR; break;
9065 case OP_LDRF_POS: new_op = OP_LDURV; break;
9066 case OP_STRF_POS: new_op = OP_STURV; break;
9067 case OP_LDRSW_POS: new_op = OP_LDURSW; break;
9068 case OP_PRFM_POS: new_op = OP_PRFUM; break;
9069 default: new_op = OP_NIL; break;
9070 }
9071
9072 if (new_op == OP_NIL)
5b7c81bd 9073 return false;
a06ea964
NC
9074
9075 new_opcode = aarch64_get_opcode (new_op);
9076 gas_assert (new_opcode != NULL);
9077
9078 DEBUG_TRACE ("Check programmer-friendly STURB/LDURB -> STRB/LDRB: %d == %d",
9079 instr->opcode->op, new_opcode->op);
9080
9081 aarch64_replace_opcode (instr, new_opcode);
9082
9083 /* Clear up the ADDR_SIMM9's qualifier; otherwise the
9084 qualifier matching may fail because the out-of-date qualifier will
9085 prevent the operand being updated with a new and correct qualifier. */
9086 idx = aarch64_operand_index (instr->opcode->operands,
9087 AARCH64_OPND_ADDR_SIMM9);
9088 gas_assert (idx == 1);
9089 instr->operands[idx].qualifier = AARCH64_OPND_QLF_NIL;
9090
9091 DEBUG_TRACE ("Found LDURB entry to encode programmer-friendly LDRB");
9092
7e84b55d
TC
9093 if (!aarch64_opcode_encode (instr->opcode, instr, &instr->value, NULL, NULL,
9094 insn_sequence))
5b7c81bd 9095 return false;
a06ea964 9096
5b7c81bd 9097 return true;
a06ea964
NC
9098}
9099
9100/* Called by fix_insn to fix a MOV immediate alias instruction.
9101
9102 Operand for a generic move immediate instruction, which is an alias
9103 instruction that generates a single MOVZ, MOVN or ORR instruction to loads
9104 a 32-bit/64-bit immediate value into general register. An assembler error
9105 shall result if the immediate cannot be created by a single one of these
9106 instructions. If there is a choice, then to ensure reversability an
9107 assembler must prefer a MOVZ to MOVN, and MOVZ or MOVN to ORR. */
9108
9109static void
9110fix_mov_imm_insn (fixS *fixP, char *buf, aarch64_inst *instr, offsetT value)
9111{
9112 const aarch64_opcode *opcode;
9113
9114 /* Need to check if the destination is SP/ZR. The check has to be done
9115 before any aarch64_replace_opcode. */
9116 int try_mov_wide_p = !aarch64_stack_pointer_p (&instr->operands[0]);
9117 int try_mov_bitmask_p = !aarch64_zero_register_p (&instr->operands[0]);
9118
9119 instr->operands[1].imm.value = value;
9120 instr->operands[1].skip = 0;
9121
9122 if (try_mov_wide_p)
9123 {
9124 /* Try the MOVZ alias. */
9125 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDE);
9126 aarch64_replace_opcode (instr, opcode);
9127 if (aarch64_opcode_encode (instr->opcode, instr,
7e84b55d 9128 &instr->value, NULL, NULL, insn_sequence))
a06ea964
NC
9129 {
9130 put_aarch64_insn (buf, instr->value);
9131 return;
9132 }
9133 /* Try the MOVK alias. */
9134 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDEN);
9135 aarch64_replace_opcode (instr, opcode);
9136 if (aarch64_opcode_encode (instr->opcode, instr,
7e84b55d 9137 &instr->value, NULL, NULL, insn_sequence))
a06ea964
NC
9138 {
9139 put_aarch64_insn (buf, instr->value);
9140 return;
9141 }
9142 }
9143
9144 if (try_mov_bitmask_p)
9145 {
9146 /* Try the ORR alias. */
9147 opcode = aarch64_get_opcode (OP_MOV_IMM_LOG);
9148 aarch64_replace_opcode (instr, opcode);
9149 if (aarch64_opcode_encode (instr->opcode, instr,
7e84b55d 9150 &instr->value, NULL, NULL, insn_sequence))
a06ea964
NC
9151 {
9152 put_aarch64_insn (buf, instr->value);
9153 return;
9154 }
9155 }
9156
9157 as_bad_where (fixP->fx_file, fixP->fx_line,
9158 _("immediate cannot be moved by a single instruction"));
9159}
9160
9161/* An instruction operand which is immediate related may have symbol used
9162 in the assembly, e.g.
9163
9164 mov w0, u32
9165 .set u32, 0x00ffff00
9166
9167 At the time when the assembly instruction is parsed, a referenced symbol,
9168 like 'u32' in the above example may not have been seen; a fixS is created
9169 in such a case and is handled here after symbols have been resolved.
9170 Instruction is fixed up with VALUE using the information in *FIXP plus
9171 extra information in FLAGS.
9172
9173 This function is called by md_apply_fix to fix up instructions that need
9174 a fix-up described above but does not involve any linker-time relocation. */
9175
9176static void
9177fix_insn (fixS *fixP, uint32_t flags, offsetT value)
9178{
9179 int idx;
9180 uint32_t insn;
9181 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
9182 enum aarch64_opnd opnd = fixP->tc_fix_data.opnd;
9183 aarch64_inst *new_inst = fixP->tc_fix_data.inst;
9184
9185 if (new_inst)
9186 {
9187 /* Now the instruction is about to be fixed-up, so the operand that
9188 was previously marked as 'ignored' needs to be unmarked in order
9189 to get the encoding done properly. */
9190 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
9191 new_inst->operands[idx].skip = 0;
9192 }
9193
9194 gas_assert (opnd != AARCH64_OPND_NIL);
9195
9196 switch (opnd)
9197 {
9198 case AARCH64_OPND_EXCEPTION:
09c1e68a 9199 case AARCH64_OPND_UNDEFINED:
a06ea964
NC
9200 if (unsigned_overflow (value, 16))
9201 as_bad_where (fixP->fx_file, fixP->fx_line,
9202 _("immediate out of range"));
9203 insn = get_aarch64_insn (buf);
09c1e68a 9204 insn |= (opnd == AARCH64_OPND_EXCEPTION) ? encode_svc_imm (value) : value;
a06ea964
NC
9205 put_aarch64_insn (buf, insn);
9206 break;
9207
9208 case AARCH64_OPND_AIMM:
9209 /* ADD or SUB with immediate.
9210 NOTE this assumes we come here with a add/sub shifted reg encoding
9211 3 322|2222|2 2 2 21111 111111
9212 1 098|7654|3 2 1 09876 543210 98765 43210
9213 0b000000 sf 000|1011|shift 0 Rm imm6 Rn Rd ADD
9214 2b000000 sf 010|1011|shift 0 Rm imm6 Rn Rd ADDS
9215 4b000000 sf 100|1011|shift 0 Rm imm6 Rn Rd SUB
9216 6b000000 sf 110|1011|shift 0 Rm imm6 Rn Rd SUBS
9217 ->
9218 3 322|2222|2 2 221111111111
9219 1 098|7654|3 2 109876543210 98765 43210
9220 11000000 sf 001|0001|shift imm12 Rn Rd ADD
9221 31000000 sf 011|0001|shift imm12 Rn Rd ADDS
9222 51000000 sf 101|0001|shift imm12 Rn Rd SUB
9223 71000000 sf 111|0001|shift imm12 Rn Rd SUBS
9224 Fields sf Rn Rd are already set. */
9225 insn = get_aarch64_insn (buf);
9226 if (value < 0)
9227 {
9228 /* Add <-> sub. */
9229 insn = reencode_addsub_switch_add_sub (insn);
9230 value = -value;
9231 }
9232
9233 if ((flags & FIXUP_F_HAS_EXPLICIT_SHIFT) == 0
9234 && unsigned_overflow (value, 12))
9235 {
9236 /* Try to shift the value by 12 to make it fit. */
9237 if (((value >> 12) << 12) == value
9238 && ! unsigned_overflow (value, 12 + 12))
9239 {
9240 value >>= 12;
9241 insn |= encode_addsub_imm_shift_amount (1);
9242 }
9243 }
9244
9245 if (unsigned_overflow (value, 12))
9246 as_bad_where (fixP->fx_file, fixP->fx_line,
9247 _("immediate out of range"));
9248
9249 insn |= encode_addsub_imm (value);
9250
9251 put_aarch64_insn (buf, insn);
9252 break;
9253
9254 case AARCH64_OPND_SIMD_IMM:
9255 case AARCH64_OPND_SIMD_IMM_SFT:
9256 case AARCH64_OPND_LIMM:
9257 /* Bit mask immediate. */
9258 gas_assert (new_inst != NULL);
9259 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
9260 new_inst->operands[idx].imm.value = value;
9261 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
7e84b55d 9262 &new_inst->value, NULL, NULL, insn_sequence))
a06ea964
NC
9263 put_aarch64_insn (buf, new_inst->value);
9264 else
9265 as_bad_where (fixP->fx_file, fixP->fx_line,
9266 _("invalid immediate"));
9267 break;
9268
9269 case AARCH64_OPND_HALF:
9270 /* 16-bit unsigned immediate. */
9271 if (unsigned_overflow (value, 16))
9272 as_bad_where (fixP->fx_file, fixP->fx_line,
9273 _("immediate out of range"));
9274 insn = get_aarch64_insn (buf);
9275 insn |= encode_movw_imm (value & 0xffff);
9276 put_aarch64_insn (buf, insn);
9277 break;
9278
9279 case AARCH64_OPND_IMM_MOV:
9280 /* Operand for a generic move immediate instruction, which is
9281 an alias instruction that generates a single MOVZ, MOVN or ORR
9282 instruction to loads a 32-bit/64-bit immediate value into general
9283 register. An assembler error shall result if the immediate cannot be
9284 created by a single one of these instructions. If there is a choice,
9285 then to ensure reversability an assembler must prefer a MOVZ to MOVN,
9286 and MOVZ or MOVN to ORR. */
9287 gas_assert (new_inst != NULL);
9288 fix_mov_imm_insn (fixP, buf, new_inst, value);
9289 break;
9290
9291 case AARCH64_OPND_ADDR_SIMM7:
9292 case AARCH64_OPND_ADDR_SIMM9:
9293 case AARCH64_OPND_ADDR_SIMM9_2:
3f06e550 9294 case AARCH64_OPND_ADDR_SIMM10:
a06ea964 9295 case AARCH64_OPND_ADDR_UIMM12:
fb3265b3
SD
9296 case AARCH64_OPND_ADDR_SIMM11:
9297 case AARCH64_OPND_ADDR_SIMM13:
a06ea964
NC
9298 /* Immediate offset in an address. */
9299 insn = get_aarch64_insn (buf);
9300
9301 gas_assert (new_inst != NULL && new_inst->value == insn);
9302 gas_assert (new_inst->opcode->operands[1] == opnd
9303 || new_inst->opcode->operands[2] == opnd);
9304
9305 /* Get the index of the address operand. */
9306 if (new_inst->opcode->operands[1] == opnd)
9307 /* e.g. STR <Xt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]. */
9308 idx = 1;
9309 else
9310 /* e.g. LDP <Qt1>, <Qt2>, [<Xn|SP>{, #<imm>}]. */
9311 idx = 2;
9312
9313 /* Update the resolved offset value. */
9314 new_inst->operands[idx].addr.offset.imm = value;
9315
9316 /* Encode/fix-up. */
9317 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
7e84b55d 9318 &new_inst->value, NULL, NULL, insn_sequence))
a06ea964
NC
9319 {
9320 put_aarch64_insn (buf, new_inst->value);
9321 break;
9322 }
9323 else if (new_inst->opcode->iclass == ldst_pos
9324 && try_to_encode_as_unscaled_ldst (new_inst))
9325 {
9326 put_aarch64_insn (buf, new_inst->value);
9327 break;
9328 }
9329
9330 as_bad_where (fixP->fx_file, fixP->fx_line,
9331 _("immediate offset out of range"));
9332 break;
9333
9334 default:
9335 gas_assert (0);
9336 as_fatal (_("unhandled operand code %d"), opnd);
9337 }
9338}
9339
9340/* Apply a fixup (fixP) to segment data, once it has been determined
9341 by our caller that we have all the info we need to fix it up.
9342
9343 Parameter valP is the pointer to the value of the bits. */
9344
9345void
9346md_apply_fix (fixS * fixP, valueT * valP, segT seg)
9347{
9348 offsetT value = *valP;
9349 uint32_t insn;
9350 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
9351 int scale;
9352 unsigned flags = fixP->fx_addnumber;
9353
9354 DEBUG_TRACE ("\n\n");
9355 DEBUG_TRACE ("~~~~~~~~~~~~~~~~~~~~~~~~~");
9356 DEBUG_TRACE ("Enter md_apply_fix");
9357
9358 gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
9359
9360 /* Note whether this will delete the relocation. */
9361
c1723a81
JB
9362 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel
9363 && aarch64_force_reloc (fixP->fx_r_type) <= 0)
a06ea964
NC
9364 fixP->fx_done = 1;
9365
9366 /* Process the relocations. */
9367 switch (fixP->fx_r_type)
9368 {
9369 case BFD_RELOC_NONE:
9370 /* This will need to go in the object file. */
9371 fixP->fx_done = 0;
9372 break;
9373
9374 case BFD_RELOC_8:
9375 case BFD_RELOC_8_PCREL:
9376 if (fixP->fx_done || !seg->use_rela_p)
9377 md_number_to_chars (buf, value, 1);
9378 break;
9379
9380 case BFD_RELOC_16:
9381 case BFD_RELOC_16_PCREL:
9382 if (fixP->fx_done || !seg->use_rela_p)
9383 md_number_to_chars (buf, value, 2);
9384 break;
9385
9386 case BFD_RELOC_32:
9387 case BFD_RELOC_32_PCREL:
9388 if (fixP->fx_done || !seg->use_rela_p)
9389 md_number_to_chars (buf, value, 4);
9390 break;
9391
9392 case BFD_RELOC_64:
9393 case BFD_RELOC_64_PCREL:
9394 if (fixP->fx_done || !seg->use_rela_p)
9395 md_number_to_chars (buf, value, 8);
9396 break;
9397
9398 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
9399 /* We claim that these fixups have been processed here, even if
9400 in fact we generate an error because we do not have a reloc
9401 for them, so tc_gen_reloc() will reject them. */
9402 fixP->fx_done = 1;
9403 if (fixP->fx_addsy && !S_IS_DEFINED (fixP->fx_addsy))
9404 {
9405 as_bad_where (fixP->fx_file, fixP->fx_line,
9406 _("undefined symbol %s used as an immediate value"),
9407 S_GET_NAME (fixP->fx_addsy));
9408 goto apply_fix_return;
9409 }
9410 fix_insn (fixP, flags, value);
9411 break;
9412
9413 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
a06ea964
NC
9414 if (fixP->fx_done || !seg->use_rela_p)
9415 {
89d2a2a3
MS
9416 if (value & 3)
9417 as_bad_where (fixP->fx_file, fixP->fx_line,
9418 _("pc-relative load offset not word aligned"));
9419 if (signed_overflow (value, 21))
9420 as_bad_where (fixP->fx_file, fixP->fx_line,
9421 _("pc-relative load offset out of range"));
a06ea964
NC
9422 insn = get_aarch64_insn (buf);
9423 insn |= encode_ld_lit_ofs_19 (value >> 2);
9424 put_aarch64_insn (buf, insn);
9425 }
9426 break;
9427
9428 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
a06ea964
NC
9429 if (fixP->fx_done || !seg->use_rela_p)
9430 {
89d2a2a3
MS
9431 if (signed_overflow (value, 21))
9432 as_bad_where (fixP->fx_file, fixP->fx_line,
9433 _("pc-relative address offset out of range"));
a06ea964
NC
9434 insn = get_aarch64_insn (buf);
9435 insn |= encode_adr_imm (value);
9436 put_aarch64_insn (buf, insn);
9437 }
9438 break;
9439
9440 case BFD_RELOC_AARCH64_BRANCH19:
a06ea964
NC
9441 if (fixP->fx_done || !seg->use_rela_p)
9442 {
89d2a2a3
MS
9443 if (value & 3)
9444 as_bad_where (fixP->fx_file, fixP->fx_line,
9445 _("conditional branch target not word aligned"));
9446 if (signed_overflow (value, 21))
9447 as_bad_where (fixP->fx_file, fixP->fx_line,
9448 _("conditional branch out of range"));
a06ea964
NC
9449 insn = get_aarch64_insn (buf);
9450 insn |= encode_cond_branch_ofs_19 (value >> 2);
9451 put_aarch64_insn (buf, insn);
9452 }
9453 break;
9454
9455 case BFD_RELOC_AARCH64_TSTBR14:
a06ea964
NC
9456 if (fixP->fx_done || !seg->use_rela_p)
9457 {
89d2a2a3
MS
9458 if (value & 3)
9459 as_bad_where (fixP->fx_file, fixP->fx_line,
9460 _("conditional branch target not word aligned"));
9461 if (signed_overflow (value, 16))
9462 as_bad_where (fixP->fx_file, fixP->fx_line,
9463 _("conditional branch out of range"));
a06ea964
NC
9464 insn = get_aarch64_insn (buf);
9465 insn |= encode_tst_branch_ofs_14 (value >> 2);
9466 put_aarch64_insn (buf, insn);
9467 }
9468 break;
9469
a06ea964 9470 case BFD_RELOC_AARCH64_CALL26:
f09c556a 9471 case BFD_RELOC_AARCH64_JUMP26:
a06ea964
NC
9472 if (fixP->fx_done || !seg->use_rela_p)
9473 {
89d2a2a3
MS
9474 if (value & 3)
9475 as_bad_where (fixP->fx_file, fixP->fx_line,
9476 _("branch target not word aligned"));
9477 if (signed_overflow (value, 28))
9478 as_bad_where (fixP->fx_file, fixP->fx_line,
9479 _("branch out of range"));
a06ea964
NC
9480 insn = get_aarch64_insn (buf);
9481 insn |= encode_branch_ofs_26 (value >> 2);
9482 put_aarch64_insn (buf, insn);
9483 }
9484 break;
9485
9486 case BFD_RELOC_AARCH64_MOVW_G0:
a06ea964 9487 case BFD_RELOC_AARCH64_MOVW_G0_NC:
f09c556a 9488 case BFD_RELOC_AARCH64_MOVW_G0_S:
ca632371 9489 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
32247401
RL
9490 case BFD_RELOC_AARCH64_MOVW_PREL_G0:
9491 case BFD_RELOC_AARCH64_MOVW_PREL_G0_NC:
a06ea964
NC
9492 scale = 0;
9493 goto movw_common;
9494 case BFD_RELOC_AARCH64_MOVW_G1:
a06ea964 9495 case BFD_RELOC_AARCH64_MOVW_G1_NC:
f09c556a 9496 case BFD_RELOC_AARCH64_MOVW_G1_S:
654248e7 9497 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
32247401
RL
9498 case BFD_RELOC_AARCH64_MOVW_PREL_G1:
9499 case BFD_RELOC_AARCH64_MOVW_PREL_G1_NC:
a06ea964
NC
9500 scale = 16;
9501 goto movw_common;
43a357f9
RL
9502 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
9503 scale = 0;
9504 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9505 /* Should always be exported to object file, see
9506 aarch64_force_relocation(). */
9507 gas_assert (!fixP->fx_done);
9508 gas_assert (seg->use_rela_p);
9509 goto movw_common;
9510 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
9511 scale = 16;
9512 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9513 /* Should always be exported to object file, see
9514 aarch64_force_relocation(). */
9515 gas_assert (!fixP->fx_done);
9516 gas_assert (seg->use_rela_p);
9517 goto movw_common;
a06ea964 9518 case BFD_RELOC_AARCH64_MOVW_G2:
a06ea964 9519 case BFD_RELOC_AARCH64_MOVW_G2_NC:
f09c556a 9520 case BFD_RELOC_AARCH64_MOVW_G2_S:
32247401
RL
9521 case BFD_RELOC_AARCH64_MOVW_PREL_G2:
9522 case BFD_RELOC_AARCH64_MOVW_PREL_G2_NC:
a06ea964
NC
9523 scale = 32;
9524 goto movw_common;
9525 case BFD_RELOC_AARCH64_MOVW_G3:
32247401 9526 case BFD_RELOC_AARCH64_MOVW_PREL_G3:
a06ea964
NC
9527 scale = 48;
9528 movw_common:
9529 if (fixP->fx_done || !seg->use_rela_p)
9530 {
9531 insn = get_aarch64_insn (buf);
9532
9533 if (!fixP->fx_done)
9534 {
9535 /* REL signed addend must fit in 16 bits */
9536 if (signed_overflow (value, 16))
9537 as_bad_where (fixP->fx_file, fixP->fx_line,
9538 _("offset out of range"));
9539 }
9540 else
9541 {
9542 /* Check for overflow and scale. */
9543 switch (fixP->fx_r_type)
9544 {
9545 case BFD_RELOC_AARCH64_MOVW_G0:
9546 case BFD_RELOC_AARCH64_MOVW_G1:
9547 case BFD_RELOC_AARCH64_MOVW_G2:
9548 case BFD_RELOC_AARCH64_MOVW_G3:
654248e7 9549 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
43a357f9 9550 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964
NC
9551 if (unsigned_overflow (value, scale + 16))
9552 as_bad_where (fixP->fx_file, fixP->fx_line,
9553 _("unsigned value out of range"));
9554 break;
9555 case BFD_RELOC_AARCH64_MOVW_G0_S:
9556 case BFD_RELOC_AARCH64_MOVW_G1_S:
9557 case BFD_RELOC_AARCH64_MOVW_G2_S:
32247401
RL
9558 case BFD_RELOC_AARCH64_MOVW_PREL_G0:
9559 case BFD_RELOC_AARCH64_MOVW_PREL_G1:
9560 case BFD_RELOC_AARCH64_MOVW_PREL_G2:
a06ea964
NC
9561 /* NOTE: We can only come here with movz or movn. */
9562 if (signed_overflow (value, scale + 16))
9563 as_bad_where (fixP->fx_file, fixP->fx_line,
9564 _("signed value out of range"));
9565 if (value < 0)
9566 {
9567 /* Force use of MOVN. */
9568 value = ~value;
9569 insn = reencode_movzn_to_movn (insn);
9570 }
9571 else
9572 {
9573 /* Force use of MOVZ. */
9574 insn = reencode_movzn_to_movz (insn);
9575 }
9576 break;
9577 default:
9578 /* Unchecked relocations. */
9579 break;
9580 }
9581 value >>= scale;
9582 }
9583
9584 /* Insert value into MOVN/MOVZ/MOVK instruction. */
9585 insn |= encode_movw_imm (value & 0xffff);
9586
9587 put_aarch64_insn (buf, insn);
9588 }
9589 break;
9590
a6bb11b2
YZ
9591 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
9592 fixP->fx_r_type = (ilp32_p
9593 ? BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC
9594 : BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
9595 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9596 /* Should always be exported to object file, see
9597 aarch64_force_relocation(). */
9598 gas_assert (!fixP->fx_done);
9599 gas_assert (seg->use_rela_p);
9600 break;
9601
9602 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
9603 fixP->fx_r_type = (ilp32_p
9604 ? BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
f955cccf 9605 : BFD_RELOC_AARCH64_TLSDESC_LD64_LO12);
a6bb11b2
YZ
9606 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9607 /* Should always be exported to object file, see
9608 aarch64_force_relocation(). */
9609 gas_assert (!fixP->fx_done);
9610 gas_assert (seg->use_rela_p);
9611 break;
9612
f955cccf 9613 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
2c0a3565 9614 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 9615 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
2c0a3565 9616 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
f955cccf 9617 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
1ada945d 9618 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
a06ea964 9619 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
2c0a3565 9620 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 9621 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
3e8286c0 9622 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
1aa66fb1 9623 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 9624 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 9625 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
2c0a3565 9626 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 9627 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
9628 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
9629 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
49df5539 9630 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
70151fb5 9631 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
13289c10 9632 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
a12fad50 9633 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
1107e076 9634 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6c37fedc 9635 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4c562523
JW
9636 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
9637 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
9638 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
9639 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
9640 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
9641 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
9642 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
9643 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
49df5539
JW
9644 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
9645 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
9646 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
9647 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
9648 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
84f1b9fb
RL
9649 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12:
9650 case BFD_RELOC_AARCH64_TLSLE_LDST16_TPREL_LO12_NC:
9651 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12:
9652 case BFD_RELOC_AARCH64_TLSLE_LDST32_TPREL_LO12_NC:
9653 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12:
9654 case BFD_RELOC_AARCH64_TLSLE_LDST64_TPREL_LO12_NC:
9655 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12:
9656 case BFD_RELOC_AARCH64_TLSLE_LDST8_TPREL_LO12_NC:
a06ea964 9657 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
2c0a3565 9658 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
a06ea964 9659 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
a06ea964
NC
9660 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
9661 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
2c0a3565
MS
9662 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
9663 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
9664 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
a06ea964
NC
9665 S_SET_THREAD_LOCAL (fixP->fx_addsy);
9666 /* Should always be exported to object file, see
9667 aarch64_force_relocation(). */
9668 gas_assert (!fixP->fx_done);
9669 gas_assert (seg->use_rela_p);
9670 break;
9671
a6bb11b2
YZ
9672 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
9673 /* Should always be exported to object file, see
9674 aarch64_force_relocation(). */
9675 fixP->fx_r_type = (ilp32_p
9676 ? BFD_RELOC_AARCH64_LD32_GOT_LO12_NC
9677 : BFD_RELOC_AARCH64_LD64_GOT_LO12_NC);
9678 gas_assert (!fixP->fx_done);
9679 gas_assert (seg->use_rela_p);
9680 break;
9681
a06ea964 9682 case BFD_RELOC_AARCH64_ADD_LO12:
f09c556a
JW
9683 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
9684 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
9685 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
9686 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
9687 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3d715ce4 9688 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
87f5fbcc 9689 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
a921b5bd 9690 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
f09c556a
JW
9691 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
9692 case BFD_RELOC_AARCH64_LDST128_LO12:
a06ea964
NC
9693 case BFD_RELOC_AARCH64_LDST16_LO12:
9694 case BFD_RELOC_AARCH64_LDST32_LO12:
9695 case BFD_RELOC_AARCH64_LDST64_LO12:
f09c556a 9696 case BFD_RELOC_AARCH64_LDST8_LO12:
a06ea964
NC
9697 /* Should always be exported to object file, see
9698 aarch64_force_relocation(). */
9699 gas_assert (!fixP->fx_done);
9700 gas_assert (seg->use_rela_p);
9701 break;
9702
9703 case BFD_RELOC_AARCH64_TLSDESC_ADD:
a06ea964 9704 case BFD_RELOC_AARCH64_TLSDESC_CALL:
f09c556a 9705 case BFD_RELOC_AARCH64_TLSDESC_LDR:
a06ea964
NC
9706 break;
9707
b97e87cc
NC
9708 case BFD_RELOC_UNUSED:
9709 /* An error will already have been reported. */
9710 break;
9711
b152649d 9712 case BFD_RELOC_RVA:
528e4f46 9713 case BFD_RELOC_32_SECREL:
6f4a117f 9714 case BFD_RELOC_16_SECIDX:
b152649d
MH
9715 break;
9716
a06ea964
NC
9717 default:
9718 as_bad_where (fixP->fx_file, fixP->fx_line,
9719 _("unexpected %s fixup"),
9720 bfd_get_reloc_code_name (fixP->fx_r_type));
9721 break;
9722 }
9723
dc1e8a47 9724 apply_fix_return:
a06ea964
NC
9725 /* Free the allocated the struct aarch64_inst.
9726 N.B. currently there are very limited number of fix-up types actually use
9727 this field, so the impact on the performance should be minimal . */
9fbb53c7 9728 free (fixP->tc_fix_data.inst);
a06ea964
NC
9729
9730 return;
9731}
9732
9733/* Translate internal representation of relocation info to BFD target
9734 format. */
9735
9736arelent *
9737tc_gen_reloc (asection * section, fixS * fixp)
9738{
9739 arelent *reloc;
9740 bfd_reloc_code_real_type code;
9741
325801bd 9742 reloc = XNEW (arelent);
a06ea964 9743
325801bd 9744 reloc->sym_ptr_ptr = XNEW (asymbol *);
a06ea964
NC
9745 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
9746 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
9747
9748 if (fixp->fx_pcrel)
9749 {
9750 if (section->use_rela_p)
9751 fixp->fx_offset -= md_pcrel_from_section (fixp, section);
9752 else
9753 fixp->fx_offset = reloc->address;
9754 }
9755 reloc->addend = fixp->fx_offset;
9756
9757 code = fixp->fx_r_type;
9758 switch (code)
9759 {
9760 case BFD_RELOC_16:
9761 if (fixp->fx_pcrel)
9762 code = BFD_RELOC_16_PCREL;
9763 break;
9764
9765 case BFD_RELOC_32:
9766 if (fixp->fx_pcrel)
9767 code = BFD_RELOC_32_PCREL;
9768 break;
9769
9770 case BFD_RELOC_64:
9771 if (fixp->fx_pcrel)
9772 code = BFD_RELOC_64_PCREL;
9773 break;
9774
9775 default:
9776 break;
9777 }
9778
9779 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
9780 if (reloc->howto == NULL)
9781 {
9782 as_bad_where (fixp->fx_file, fixp->fx_line,
9783 _
9784 ("cannot represent %s relocation in this object file format"),
9785 bfd_get_reloc_code_name (code));
9786 return NULL;
9787 }
9788
9789 return reloc;
9790}
9791
9792/* This fix_new is called by cons via TC_CONS_FIX_NEW. */
9793
9794void
9795cons_fix_new_aarch64 (fragS * frag, int where, int size, expressionS * exp)
9796{
9797 bfd_reloc_code_real_type type;
9798 int pcrel = 0;
9799
528e4f46
MH
9800#ifdef TE_PE
9801 if (exp->X_op == O_secrel)
a06ea964 9802 {
528e4f46
MH
9803 exp->X_op = O_symbol;
9804 type = BFD_RELOC_32_SECREL;
a06ea964 9805 }
6f4a117f
MH
9806 else if (exp->X_op == O_secidx)
9807 {
9808 exp->X_op = O_symbol;
9809 type = BFD_RELOC_16_SECIDX;
9810 }
528e4f46
MH
9811 else
9812 {
9813#endif
9814 /* Pick a reloc.
9815 FIXME: @@ Should look at CPU word size. */
9816 switch (size)
9817 {
9818 case 1:
9819 type = BFD_RELOC_8;
9820 break;
9821 case 2:
9822 type = BFD_RELOC_16;
9823 break;
9824 case 4:
9825 type = BFD_RELOC_32;
9826 break;
9827 case 8:
9828 type = BFD_RELOC_64;
9829 break;
9830 default:
9831 as_bad (_("cannot do %u-byte relocation"), size);
9832 type = BFD_RELOC_UNUSED;
9833 break;
9834 }
9835#ifdef TE_PE
9836 }
9837#endif
a06ea964
NC
9838
9839 fix_new_exp (frag, where, (int) size, exp, pcrel, type);
9840}
9841
3c0367d0
JW
9842/* Implement md_after_parse_args. This is the earliest time we need to decide
9843 ABI. If no -mabi specified, the ABI will be decided by target triplet. */
9844
9845void
9846aarch64_after_parse_args (void)
9847{
9848 if (aarch64_abi != AARCH64_ABI_NONE)
9849 return;
9850
c60b3806 9851#ifdef OBJ_ELF
3c0367d0
JW
9852 /* DEFAULT_ARCH will have ":32" extension if it's configured for ILP32. */
9853 if (strlen (default_arch) > 7 && strcmp (default_arch + 7, ":32") == 0)
9854 aarch64_abi = AARCH64_ABI_ILP32;
9855 else
9856 aarch64_abi = AARCH64_ABI_LP64;
c60b3806
JT
9857#else
9858 aarch64_abi = AARCH64_ABI_LLP64;
9859#endif
3c0367d0
JW
9860}
9861
c60b3806 9862#ifdef OBJ_ELF
a06ea964
NC
9863const char *
9864elf64_aarch64_target_format (void)
9865{
12400dcc
AM
9866#ifdef TE_CLOUDABI
9867 /* FIXME: What to do for ilp32_p ? */
9868 if (target_big_endian)
9869 return "elf64-bigaarch64-cloudabi";
9870 else
9871 return "elf64-littleaarch64-cloudabi";
9872#else
a06ea964 9873 if (target_big_endian)
cec5225b 9874 return ilp32_p ? "elf32-bigaarch64" : "elf64-bigaarch64";
a06ea964 9875 else
cec5225b 9876 return ilp32_p ? "elf32-littleaarch64" : "elf64-littleaarch64";
12400dcc 9877#endif
a06ea964
NC
9878}
9879
9880void
9881aarch64elf_frob_symbol (symbolS * symp, int *puntp)
9882{
9883 elf_frob_symbol (symp, puntp);
9884}
c60b3806
JT
9885#elif defined OBJ_COFF
9886const char *
9887coff_aarch64_target_format (void)
9888{
9889 return "pe-aarch64-little";
9890}
a06ea964
NC
9891#endif
9892
9893/* MD interface: Finalization. */
9894
9895/* A good place to do this, although this was probably not intended
9896 for this kind of use. We need to dump the literal pool before
9897 references are made to a null symbol pointer. */
9898
9899void
9900aarch64_cleanup (void)
9901{
9902 literal_pool *pool;
9903
9904 for (pool = list_of_pools; pool; pool = pool->next)
9905 {
9906 /* Put it at the end of the relevant section. */
9907 subseg_set (pool->section, pool->sub_section);
9908 s_ltorg (0);
9909 }
9910}
9911
9912#ifdef OBJ_ELF
9913/* Remove any excess mapping symbols generated for alignment frags in
9914 SEC. We may have created a mapping symbol before a zero byte
9915 alignment; remove it if there's a mapping symbol after the
9916 alignment. */
9917static void
9918check_mapping_symbols (bfd * abfd ATTRIBUTE_UNUSED, asection * sec,
9919 void *dummy ATTRIBUTE_UNUSED)
9920{
9921 segment_info_type *seginfo = seg_info (sec);
9922 fragS *fragp;
9923
9924 if (seginfo == NULL || seginfo->frchainP == NULL)
9925 return;
9926
9927 for (fragp = seginfo->frchainP->frch_root;
9928 fragp != NULL; fragp = fragp->fr_next)
9929 {
9930 symbolS *sym = fragp->tc_frag_data.last_map;
9931 fragS *next = fragp->fr_next;
9932
9933 /* Variable-sized frags have been converted to fixed size by
9934 this point. But if this was variable-sized to start with,
9935 there will be a fixed-size frag after it. So don't handle
9936 next == NULL. */
9937 if (sym == NULL || next == NULL)
9938 continue;
9939
9940 if (S_GET_VALUE (sym) < next->fr_address)
9941 /* Not at the end of this frag. */
9942 continue;
9943 know (S_GET_VALUE (sym) == next->fr_address);
9944
9945 do
9946 {
9947 if (next->tc_frag_data.first_map != NULL)
9948 {
9949 /* Next frag starts with a mapping symbol. Discard this
9950 one. */
9951 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
9952 break;
9953 }
9954
9955 if (next->fr_next == NULL)
9956 {
9957 /* This mapping symbol is at the end of the section. Discard
9958 it. */
9959 know (next->fr_fix == 0 && next->fr_var == 0);
9960 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
9961 break;
9962 }
9963
9964 /* As long as we have empty frags without any mapping symbols,
9965 keep looking. */
9966 /* If the next frag is non-empty and does not start with a
9967 mapping symbol, then this mapping symbol is required. */
9968 if (next->fr_address != next->fr_next->fr_address)
9969 break;
9970
9971 next = next->fr_next;
9972 }
9973 while (next != NULL);
9974 }
9975}
9976#endif
9977
9978/* Adjust the symbol table. */
9979
9980void
9981aarch64_adjust_symtab (void)
9982{
9983#ifdef OBJ_ELF
9984 /* Remove any overlapping mapping symbols generated by alignment frags. */
9985 bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
9986 /* Now do generic ELF adjustments. */
9987 elf_adjust_symtab ();
9988#endif
9989}
9990
9991static void
629310ab 9992checked_hash_insert (htab_t table, const char *key, void *value)
a06ea964 9993{
fe0e921f 9994 str_hash_insert (table, key, value, 0);
a06ea964
NC
9995}
9996
fa63795f 9997static void
629310ab 9998sysreg_hash_insert (htab_t table, const char *key, void *value)
fa63795f
AC
9999{
10000 gas_assert (strlen (key) < AARCH64_MAX_SYSREG_NAME_LEN);
10001 checked_hash_insert (table, key, value);
10002}
10003
a06ea964
NC
10004static void
10005fill_instruction_hash_table (void)
10006{
6c2ede01 10007 const aarch64_opcode *opcode = aarch64_opcode_table;
a06ea964
NC
10008
10009 while (opcode->name != NULL)
10010 {
10011 templates *templ, *new_templ;
629310ab 10012 templ = str_hash_find (aarch64_ops_hsh, opcode->name);
a06ea964 10013
add39d23 10014 new_templ = XNEW (templates);
a06ea964
NC
10015 new_templ->opcode = opcode;
10016 new_templ->next = NULL;
10017
10018 if (!templ)
10019 checked_hash_insert (aarch64_ops_hsh, opcode->name, (void *) new_templ);
10020 else
10021 {
10022 new_templ->next = templ->next;
10023 templ->next = new_templ;
10024 }
10025 ++opcode;
10026 }
10027}
10028
10029static inline void
10030convert_to_upper (char *dst, const char *src, size_t num)
10031{
10032 unsigned int i;
10033 for (i = 0; i < num && *src != '\0'; ++i, ++dst, ++src)
10034 *dst = TOUPPER (*src);
10035 *dst = '\0';
10036}
10037
10038/* Assume STR point to a lower-case string, allocate, convert and return
10039 the corresponding upper-case string. */
10040static inline const char*
10041get_upper_str (const char *str)
10042{
10043 char *ret;
10044 size_t len = strlen (str);
325801bd 10045 ret = XNEWVEC (char, len + 1);
a06ea964
NC
10046 convert_to_upper (ret, str, len);
10047 return ret;
10048}
10049
10050/* MD interface: Initialization. */
10051
10052void
10053md_begin (void)
10054{
10055 unsigned mach;
10056 unsigned int i;
10057
f16c3d4f
AM
10058 aarch64_ops_hsh = str_htab_create ();
10059 aarch64_cond_hsh = str_htab_create ();
10060 aarch64_shift_hsh = str_htab_create ();
10061 aarch64_sys_regs_hsh = str_htab_create ();
10062 aarch64_pstatefield_hsh = str_htab_create ();
10063 aarch64_sys_regs_ic_hsh = str_htab_create ();
10064 aarch64_sys_regs_dc_hsh = str_htab_create ();
10065 aarch64_sys_regs_at_hsh = str_htab_create ();
10066 aarch64_sys_regs_tlbi_hsh = str_htab_create ();
10067 aarch64_sys_regs_sr_hsh = str_htab_create ();
10068 aarch64_reg_hsh = str_htab_create ();
10069 aarch64_barrier_opt_hsh = str_htab_create ();
10070 aarch64_nzcv_hsh = str_htab_create ();
10071 aarch64_pldop_hsh = str_htab_create ();
10072 aarch64_hint_opt_hsh = str_htab_create ();
a06ea964
NC
10073
10074 fill_instruction_hash_table ();
10075
10076 for (i = 0; aarch64_sys_regs[i].name != NULL; ++i)
fa63795f 10077 sysreg_hash_insert (aarch64_sys_regs_hsh, aarch64_sys_regs[i].name,
a06ea964
NC
10078 (void *) (aarch64_sys_regs + i));
10079
10080 for (i = 0; aarch64_pstatefields[i].name != NULL; ++i)
fa63795f 10081 sysreg_hash_insert (aarch64_pstatefield_hsh,
a06ea964
NC
10082 aarch64_pstatefields[i].name,
10083 (void *) (aarch64_pstatefields + i));
10084
875880c6 10085 for (i = 0; aarch64_sys_regs_ic[i].name != NULL; i++)
fa63795f 10086 sysreg_hash_insert (aarch64_sys_regs_ic_hsh,
875880c6 10087 aarch64_sys_regs_ic[i].name,
a06ea964
NC
10088 (void *) (aarch64_sys_regs_ic + i));
10089
875880c6 10090 for (i = 0; aarch64_sys_regs_dc[i].name != NULL; i++)
fa63795f 10091 sysreg_hash_insert (aarch64_sys_regs_dc_hsh,
875880c6 10092 aarch64_sys_regs_dc[i].name,
a06ea964
NC
10093 (void *) (aarch64_sys_regs_dc + i));
10094
875880c6 10095 for (i = 0; aarch64_sys_regs_at[i].name != NULL; i++)
fa63795f 10096 sysreg_hash_insert (aarch64_sys_regs_at_hsh,
875880c6 10097 aarch64_sys_regs_at[i].name,
a06ea964
NC
10098 (void *) (aarch64_sys_regs_at + i));
10099
875880c6 10100 for (i = 0; aarch64_sys_regs_tlbi[i].name != NULL; i++)
fa63795f 10101 sysreg_hash_insert (aarch64_sys_regs_tlbi_hsh,
875880c6 10102 aarch64_sys_regs_tlbi[i].name,
a06ea964
NC
10103 (void *) (aarch64_sys_regs_tlbi + i));
10104
2ac435d4 10105 for (i = 0; aarch64_sys_regs_sr[i].name != NULL; i++)
fa63795f 10106 sysreg_hash_insert (aarch64_sys_regs_sr_hsh,
2ac435d4
SD
10107 aarch64_sys_regs_sr[i].name,
10108 (void *) (aarch64_sys_regs_sr + i));
10109
a06ea964
NC
10110 for (i = 0; i < ARRAY_SIZE (reg_names); i++)
10111 checked_hash_insert (aarch64_reg_hsh, reg_names[i].name,
10112 (void *) (reg_names + i));
10113
10114 for (i = 0; i < ARRAY_SIZE (nzcv_names); i++)
10115 checked_hash_insert (aarch64_nzcv_hsh, nzcv_names[i].template,
10116 (void *) (nzcv_names + i));
10117
10118 for (i = 0; aarch64_operand_modifiers[i].name != NULL; i++)
10119 {
10120 const char *name = aarch64_operand_modifiers[i].name;
10121 checked_hash_insert (aarch64_shift_hsh, name,
10122 (void *) (aarch64_operand_modifiers + i));
10123 /* Also hash the name in the upper case. */
10124 checked_hash_insert (aarch64_shift_hsh, get_upper_str (name),
10125 (void *) (aarch64_operand_modifiers + i));
10126 }
10127
10128 for (i = 0; i < ARRAY_SIZE (aarch64_conds); i++)
10129 {
10130 unsigned int j;
10131 /* A condition code may have alias(es), e.g. "cc", "lo" and "ul" are
10132 the same condition code. */
10133 for (j = 0; j < ARRAY_SIZE (aarch64_conds[i].names); ++j)
10134 {
10135 const char *name = aarch64_conds[i].names[j];
10136 if (name == NULL)
10137 break;
10138 checked_hash_insert (aarch64_cond_hsh, name,
10139 (void *) (aarch64_conds + i));
10140 /* Also hash the name in the upper case. */
10141 checked_hash_insert (aarch64_cond_hsh, get_upper_str (name),
10142 (void *) (aarch64_conds + i));
10143 }
10144 }
10145
10146 for (i = 0; i < ARRAY_SIZE (aarch64_barrier_options); i++)
10147 {
10148 const char *name = aarch64_barrier_options[i].name;
10149 /* Skip xx00 - the unallocated values of option. */
10150 if ((i & 0x3) == 0)
10151 continue;
10152 checked_hash_insert (aarch64_barrier_opt_hsh, name,
10153 (void *) (aarch64_barrier_options + i));
10154 /* Also hash the name in the upper case. */
10155 checked_hash_insert (aarch64_barrier_opt_hsh, get_upper_str (name),
10156 (void *) (aarch64_barrier_options + i));
10157 }
10158
fd195909
PW
10159 for (i = 0; i < ARRAY_SIZE (aarch64_barrier_dsb_nxs_options); i++)
10160 {
10161 const char *name = aarch64_barrier_dsb_nxs_options[i].name;
10162 checked_hash_insert (aarch64_barrier_opt_hsh, name,
10163 (void *) (aarch64_barrier_dsb_nxs_options + i));
10164 /* Also hash the name in the upper case. */
10165 checked_hash_insert (aarch64_barrier_opt_hsh, get_upper_str (name),
10166 (void *) (aarch64_barrier_dsb_nxs_options + i));
10167 }
10168
a06ea964
NC
10169 for (i = 0; i < ARRAY_SIZE (aarch64_prfops); i++)
10170 {
10171 const char* name = aarch64_prfops[i].name;
a1ccaec9
YZ
10172 /* Skip the unallocated hint encodings. */
10173 if (name == NULL)
a06ea964
NC
10174 continue;
10175 checked_hash_insert (aarch64_pldop_hsh, name,
10176 (void *) (aarch64_prfops + i));
10177 /* Also hash the name in the upper case. */
10178 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
10179 (void *) (aarch64_prfops + i));
10180 }
10181
1e6f4800
MW
10182 for (i = 0; aarch64_hint_options[i].name != NULL; i++)
10183 {
10184 const char* name = aarch64_hint_options[i].name;
0a821c4f 10185 const char* upper_name = get_upper_str(name);
1e6f4800
MW
10186
10187 checked_hash_insert (aarch64_hint_opt_hsh, name,
10188 (void *) (aarch64_hint_options + i));
0a821c4f
AP
10189
10190 /* Also hash the name in the upper case if not the same. */
10191 if (strcmp (name, upper_name) != 0)
10192 checked_hash_insert (aarch64_hint_opt_hsh, upper_name,
10193 (void *) (aarch64_hint_options + i));
1e6f4800
MW
10194 }
10195
a06ea964
NC
10196 /* Set the cpu variant based on the command-line options. */
10197 if (!mcpu_cpu_opt)
10198 mcpu_cpu_opt = march_cpu_opt;
10199
10200 if (!mcpu_cpu_opt)
10201 mcpu_cpu_opt = &cpu_default;
10202
10203 cpu_variant = *mcpu_cpu_opt;
10204
10205 /* Record the CPU type. */
c60b3806
JT
10206 if(ilp32_p)
10207 mach = bfd_mach_aarch64_ilp32;
10208 else if (llp64_p)
10209 mach = bfd_mach_aarch64_llp64;
10210 else
10211 mach = bfd_mach_aarch64;
a06ea964
NC
10212
10213 bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
b52c4ee4
IB
10214#ifdef OBJ_ELF
10215 /* FIXME - is there a better way to do it ? */
10216 aarch64_sframe_cfa_sp_reg = 31;
10217 aarch64_sframe_cfa_fp_reg = 29; /* x29. */
10218 aarch64_sframe_cfa_ra_reg = 30;
10219#endif
a06ea964
NC
10220}
10221
10222/* Command line processing. */
10223
10224const char *md_shortopts = "m:";
10225
10226#ifdef AARCH64_BI_ENDIAN
10227#define OPTION_EB (OPTION_MD_BASE + 0)
10228#define OPTION_EL (OPTION_MD_BASE + 1)
10229#else
10230#if TARGET_BYTES_BIG_ENDIAN
10231#define OPTION_EB (OPTION_MD_BASE + 0)
10232#else
10233#define OPTION_EL (OPTION_MD_BASE + 1)
10234#endif
10235#endif
10236
10237struct option md_longopts[] = {
10238#ifdef OPTION_EB
10239 {"EB", no_argument, NULL, OPTION_EB},
10240#endif
10241#ifdef OPTION_EL
10242 {"EL", no_argument, NULL, OPTION_EL},
10243#endif
10244 {NULL, no_argument, NULL, 0}
10245};
10246
10247size_t md_longopts_size = sizeof (md_longopts);
10248
10249struct aarch64_option_table
10250{
e0471c16
TS
10251 const char *option; /* Option name to match. */
10252 const char *help; /* Help information. */
a06ea964
NC
10253 int *var; /* Variable to change. */
10254 int value; /* What to change it to. */
10255 char *deprecated; /* If non-null, print this message. */
10256};
10257
10258static struct aarch64_option_table aarch64_opts[] = {
10259 {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
10260 {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
10261 NULL},
10262#ifdef DEBUG_AARCH64
10263 {"mdebug-dump", N_("temporary switch for dumping"), &debug_dump, 1, NULL},
10264#endif /* DEBUG_AARCH64 */
10265 {"mverbose-error", N_("output verbose error messages"), &verbose_error_p, 1,
10266 NULL},
a52e6fd3
YZ
10267 {"mno-verbose-error", N_("do not output verbose error messages"),
10268 &verbose_error_p, 0, NULL},
a06ea964
NC
10269 {NULL, NULL, NULL, 0, NULL}
10270};
10271
10272struct aarch64_cpu_option_table
10273{
e0471c16 10274 const char *name;
a06ea964
NC
10275 const aarch64_feature_set value;
10276 /* The canonical name of the CPU, or NULL to use NAME converted to upper
10277 case. */
10278 const char *canonical_name;
10279};
10280
10281/* This list should, at a minimum, contain all the cpu names
10282 recognized by GCC. */
10283static const struct aarch64_cpu_option_table aarch64_cpus[] = {
4abb672a
RS
10284 {"all", AARCH64_ALL_FEATURES, NULL},
10285 {"cortex-a34", AARCH64_CPU_FEATURES (V8A, 1, CRC), "Cortex-A34"},
10286 {"cortex-a35", AARCH64_CPU_FEATURES (V8A, 1, CRC), "Cortex-A35"},
10287 {"cortex-a53", AARCH64_CPU_FEATURES (V8A, 1, CRC), "Cortex-A53"},
10288 {"cortex-a57", AARCH64_CPU_FEATURES (V8A, 1, CRC), "Cortex-A57"},
10289 {"cortex-a72", AARCH64_CPU_FEATURES (V8A, 1, CRC), "Cortex-A72"},
10290 {"cortex-a73", AARCH64_CPU_FEATURES (V8A, 1, CRC), "Cortex-A73"},
10291 {"cortex-a55", AARCH64_CPU_FEATURES (V8_2A, 3, RCPC, F16, DOTPROD),
1e292627 10292 "Cortex-A55"},
4abb672a 10293 {"cortex-a75", AARCH64_CPU_FEATURES (V8_2A, 3, RCPC, F16, DOTPROD),
1e292627 10294 "Cortex-A75"},
4abb672a 10295 {"cortex-a76", AARCH64_CPU_FEATURES (V8_2A, 3, RCPC, F16, DOTPROD),
c2a0f929 10296 "Cortex-A76"},
4abb672a
RS
10297 {"cortex-a76ae", AARCH64_CPU_FEATURES (V8_2A, 4, F16, RCPC, DOTPROD,
10298 SSBS), "Cortex-A76AE"},
10299 {"cortex-a77", AARCH64_CPU_FEATURES (V8_2A, 4, F16, RCPC, DOTPROD,
10300 SSBS), "Cortex-A77"},
10301 {"cortex-a65", AARCH64_CPU_FEATURES (V8_2A, 4, F16, RCPC, DOTPROD,
10302 SSBS), "Cortex-A65"},
10303 {"cortex-a65ae", AARCH64_CPU_FEATURES (V8_2A, 4, F16, RCPC, DOTPROD,
10304 SSBS), "Cortex-A65AE"},
10305 {"cortex-a78", AARCH64_CPU_FEATURES (V8_2A, 5, F16, RCPC, DOTPROD,
10306 SSBS, PROFILE), "Cortex-A78"},
10307 {"cortex-a78ae", AARCH64_CPU_FEATURES (V8_2A, 5, F16, RCPC, DOTPROD,
10308 SSBS, PROFILE), "Cortex-A78AE"},
10309 {"cortex-a78c", AARCH64_CPU_FEATURES (V8_2A, 7, DOTPROD, F16, FLAGM,
d6a14e41 10310 PAUTH, PROFILE, RCPC, SSBS),
4abb672a
RS
10311 "Cortex-A78C"},
10312 {"cortex-a510", AARCH64_CPU_FEATURES (V9A, 4, BFLOAT16, I8MM, MEMTAG,
10313 SVE2_BITPERM), "Cortex-A510"},
10314 {"cortex-a520", AARCH64_CPU_FEATURES (V9_2A, 2, MEMTAG, SVE2_BITPERM),
10315 "Cortex-A520"},
10316 {"cortex-a710", AARCH64_CPU_FEATURES (V9A, 4, BFLOAT16, I8MM, MEMTAG,
10317 SVE2_BITPERM), "Cortex-A710"},
10318 {"cortex-a720", AARCH64_CPU_FEATURES (V9_2A, 3, MEMTAG, PROFILE,
10319 SVE2_BITPERM), "Cortex-A720"},
10320 {"ares", AARCH64_CPU_FEATURES (V8_2A, 4, RCPC, F16, DOTPROD,
10321 PROFILE), "Ares"},
10322 {"exynos-m1", AARCH64_CPU_FEATURES (V8A, 3, CRC, SHA2, AES),
10323 "Samsung Exynos M1"},
10324 {"falkor", AARCH64_CPU_FEATURES (V8A, 4, CRC, SHA2, AES, RDMA),
10325 "Qualcomm Falkor"},
10326 {"neoverse-e1", AARCH64_CPU_FEATURES (V8_2A, 4, RCPC, F16, DOTPROD,
10327 SSBS), "Neoverse E1"},
10328 {"neoverse-n1", AARCH64_CPU_FEATURES (V8_2A, 4, RCPC, F16, DOTPROD,
10329 PROFILE), "Neoverse N1"},
10330 {"neoverse-n2", AARCH64_CPU_FEATURES (V8_5A, 8, BFLOAT16, I8MM, F16,
10331 SVE, SVE2, SVE2_BITPERM, MEMTAG,
10332 RNG), "Neoverse N2"},
10333 {"neoverse-v1", AARCH64_CPU_FEATURES (V8_4A, 8, PROFILE, CVADP, SVE,
10334 SSBS, RNG, F16, BFLOAT16, I8MM),
10335 "Neoverse V1"},
10336 {"qdf24xx", AARCH64_CPU_FEATURES (V8A, 4, CRC, SHA2, AES, RDMA),
10337 "Qualcomm QDF24XX"},
10338 {"saphira", AARCH64_CPU_FEATURES (V8_4A, 3, SHA2, AES, PROFILE),
10339 "Qualcomm Saphira"},
10340 {"thunderx", AARCH64_CPU_FEATURES (V8A, 3, CRC, SHA2, AES),
10341 "Cavium ThunderX"},
10342 {"vulcan", AARCH64_CPU_FEATURES (V8_1A, 2, SHA2, AES),
10343 "Broadcom Vulcan"},
070cb956
PT
10344 /* The 'xgene-1' name is an older name for 'xgene1', which was used
10345 in earlier releases and is superseded by 'xgene1' in all
10346 tools. */
4abb672a
RS
10347 {"xgene-1", AARCH64_ARCH_FEATURES (V8A), "APM X-Gene 1"},
10348 {"xgene1", AARCH64_ARCH_FEATURES (V8A), "APM X-Gene 1"},
10349 {"xgene2", AARCH64_CPU_FEATURES (V8A, 1, CRC), "APM X-Gene 2"},
10350 {"cortex-r82", AARCH64_ARCH_FEATURES (V8R), "Cortex-R82"},
10351 {"cortex-x1", AARCH64_CPU_FEATURES (V8_2A, 5, F16, RCPC, DOTPROD,
10352 SSBS, PROFILE), "Cortex-X1"},
10353 {"cortex-x2", AARCH64_CPU_FEATURES (V9A, 4, BFLOAT16, I8MM, MEMTAG,
10354 SVE2_BITPERM), "Cortex-X2"},
528c1f2b
ML
10355 {"cortex-x3", AARCH64_CPU_FEATURES (V9A, 4, BFLOAT16, I8MM, MEMTAG,
10356 SVE2_BITPERM), "Cortex-X3"},
0515a7b6
SJ
10357 {"cortex-x4", AARCH64_CPU_FEATURES (V9_2A, 3, MEMTAG, PROFILE,
10358 SVE2_BITPERM), "Cortex-X4"},
4abb672a
RS
10359 {"generic", AARCH64_ARCH_FEATURES (V8A), NULL},
10360
10361 {NULL, AARCH64_NO_FEATURES, NULL}
a06ea964
NC
10362};
10363
10364struct aarch64_arch_option_table
10365{
e0471c16 10366 const char *name;
a06ea964
NC
10367 const aarch64_feature_set value;
10368};
10369
10370/* This list should, at a minimum, contain all the architecture names
10371 recognized by GCC. */
10372static const struct aarch64_arch_option_table aarch64_archs[] = {
4abb672a
RS
10373 {"all", AARCH64_ALL_FEATURES},
10374 {"armv8-a", AARCH64_ARCH_FEATURES (V8A)},
10375 {"armv8.1-a", AARCH64_ARCH_FEATURES (V8_1A)},
10376 {"armv8.2-a", AARCH64_ARCH_FEATURES (V8_2A)},
10377 {"armv8.3-a", AARCH64_ARCH_FEATURES (V8_3A)},
10378 {"armv8.4-a", AARCH64_ARCH_FEATURES (V8_4A)},
10379 {"armv8.5-a", AARCH64_ARCH_FEATURES (V8_5A)},
10380 {"armv8.6-a", AARCH64_ARCH_FEATURES (V8_6A)},
10381 {"armv8.7-a", AARCH64_ARCH_FEATURES (V8_7A)},
10382 {"armv8.8-a", AARCH64_ARCH_FEATURES (V8_8A)},
8cee11ca 10383 {"armv8.9-a", AARCH64_ARCH_FEATURES (V8_9A)},
4abb672a
RS
10384 {"armv8-r", AARCH64_ARCH_FEATURES (V8R)},
10385 {"armv9-a", AARCH64_ARCH_FEATURES (V9A)},
10386 {"armv9.1-a", AARCH64_ARCH_FEATURES (V9_1A)},
10387 {"armv9.2-a", AARCH64_ARCH_FEATURES (V9_2A)},
10388 {"armv9.3-a", AARCH64_ARCH_FEATURES (V9_3A)},
8cee11ca 10389 {"armv9.4-a", AARCH64_ARCH_FEATURES (V9_4A)},
4abb672a 10390 {NULL, AARCH64_NO_FEATURES}
a06ea964
NC
10391};
10392
10393/* ISA extensions. */
10394struct aarch64_option_cpu_value_table
10395{
e0471c16 10396 const char *name;
a06ea964 10397 const aarch64_feature_set value;
93d8990c 10398 const aarch64_feature_set require; /* Feature dependencies. */
a06ea964
NC
10399};
10400
53071aac
AC
10401/* There are currently many feature bits with no corresponding flag for features implied by FEAT_D128. We should combine or remove most of these in the future, but for now just make +d128 enable these dependencies as well. */
10402#define D128_FEATURE_DEPS AARCH64_FEATURES (8, LSE128, SCTLR2, FGT2, ATS1A, \
10403 AIE, S1PIE, S2PIE, TCR2)
10404
a06ea964 10405static const struct aarch64_option_cpu_value_table aarch64_features[] = {
4abb672a
RS
10406 {"crc", AARCH64_FEATURE (CRC), AARCH64_NO_FEATURES},
10407 {"crypto", AARCH64_FEATURES (2, AES, SHA2),
10408 AARCH64_FEATURE (SIMD)},
10409 {"fp", AARCH64_FEATURE (FP), AARCH64_NO_FEATURES},
10410 {"lse", AARCH64_FEATURE (LSE), AARCH64_NO_FEATURES},
79f1989e 10411 {"lse128", AARCH64_FEATURE (LSE128), AARCH64_FEATURE (LSE)},
4abb672a
RS
10412 {"simd", AARCH64_FEATURE (SIMD), AARCH64_FEATURE (FP)},
10413 {"pan", AARCH64_FEATURE (PAN), AARCH64_NO_FEATURES},
10414 {"lor", AARCH64_FEATURE (LOR), AARCH64_NO_FEATURES},
10415 {"ras", AARCH64_FEATURE (RAS), AARCH64_NO_FEATURES},
10416 {"rdma", AARCH64_FEATURE (RDMA), AARCH64_FEATURE (SIMD)},
c17c7aaf 10417 {"rdm", AARCH64_FEATURE (RDMA), AARCH64_FEATURE (SIMD)},
4abb672a
RS
10418 {"fp16", AARCH64_FEATURE (F16), AARCH64_FEATURE (FP)},
10419 {"fp16fml", AARCH64_FEATURE (F16_FML), AARCH64_FEATURE (F16)},
10420 {"profile", AARCH64_FEATURE (PROFILE), AARCH64_NO_FEATURES},
10421 {"sve", AARCH64_FEATURE (SVE), AARCH64_FEATURE (COMPNUM)},
10422 {"tme", AARCH64_FEATURE (TME), AARCH64_NO_FEATURES},
c7c16ea5
AC
10423 {"fcma", AARCH64_FEATURE (COMPNUM),
10424 AARCH64_FEATURES (2, F16, SIMD)},
4abb672a
RS
10425 {"compnum", AARCH64_FEATURE (COMPNUM),
10426 AARCH64_FEATURES (2, F16, SIMD)},
227af30e 10427 {"jscvt", AARCH64_FEATURE (JSCVT), AARCH64_FEATURE (FP)},
4abb672a 10428 {"rcpc", AARCH64_FEATURE (RCPC), AARCH64_NO_FEATURES},
36891070 10429 {"rcpc2", AARCH64_FEATURE (RCPC2), AARCH64_FEATURE (RCPC)},
4abb672a 10430 {"dotprod", AARCH64_FEATURE (DOTPROD), AARCH64_FEATURE (SIMD)},
16b41963 10431 {"sha2", AARCH64_FEATURE (SHA2), AARCH64_FEATURE (SIMD)},
ce9fad98 10432 {"frintts", AARCH64_FEATURE (FRINTTS), AARCH64_FEATURE (SIMD)},
4abb672a
RS
10433 {"sb", AARCH64_FEATURE (SB), AARCH64_NO_FEATURES},
10434 {"predres", AARCH64_FEATURE (PREDRES), AARCH64_NO_FEATURES},
88b5a8ae 10435 {"predres2", AARCH64_FEATURE (PREDRES2), AARCH64_FEATURE (PREDRES)},
4abb672a
RS
10436 {"aes", AARCH64_FEATURE (AES), AARCH64_FEATURE (SIMD)},
10437 {"sm4", AARCH64_FEATURE (SM4), AARCH64_FEATURE (SIMD)},
10438 {"sha3", AARCH64_FEATURE (SHA3), AARCH64_FEATURE (SHA2)},
10439 {"rng", AARCH64_FEATURE (RNG), AARCH64_NO_FEATURES},
10440 {"ssbs", AARCH64_FEATURE (SSBS), AARCH64_NO_FEATURES},
10441 {"memtag", AARCH64_FEATURE (MEMTAG), AARCH64_NO_FEATURES},
10442 {"sve2", AARCH64_FEATURE (SVE2), AARCH64_FEATURE (SVE)},
10443 {"sve2-sm4", AARCH64_FEATURE (SVE2_SM4),
10444 AARCH64_FEATURES (2, SVE2, SM4)},
10445 {"sve2-aes", AARCH64_FEATURE (SVE2_AES),
10446 AARCH64_FEATURES (2, SVE2, AES)},
10447 {"sve2-sha3", AARCH64_FEATURE (SVE2_SHA3),
10448 AARCH64_FEATURES (2, SVE2, SHA3)},
10449 {"sve2-bitperm", AARCH64_FEATURE (SVE2_BITPERM),
10450 AARCH64_FEATURE (SVE2)},
10451 {"sme", AARCH64_FEATURE (SME),
10452 AARCH64_FEATURES (2, SVE2, BFLOAT16)},
10453 {"sme-f64", AARCH64_FEATURE (SME_F64F64), AARCH64_FEATURE (SME)},
10454 {"sme-f64f64", AARCH64_FEATURE (SME_F64F64), AARCH64_FEATURE (SME)},
10455 {"sme-i64", AARCH64_FEATURE (SME_I16I64), AARCH64_FEATURE (SME)},
10456 {"sme-i16i64", AARCH64_FEATURE (SME_I16I64), AARCH64_FEATURE (SME)},
10457 {"sme2", AARCH64_FEATURE (SME2), AARCH64_FEATURE (SME)},
10458 {"bf16", AARCH64_FEATURE (BFLOAT16), AARCH64_FEATURE (FP)},
10459 {"i8mm", AARCH64_FEATURE (I8MM), AARCH64_FEATURE (SIMD)},
10460 {"f32mm", AARCH64_FEATURE (F32MM), AARCH64_FEATURE (SVE)},
10461 {"f64mm", AARCH64_FEATURE (F64MM), AARCH64_FEATURE (SVE)},
10462 {"ls64", AARCH64_FEATURE (LS64), AARCH64_NO_FEATURES},
10463 {"flagm", AARCH64_FEATURE (FLAGM), AARCH64_NO_FEATURES},
5329ef9b 10464 {"flagm2", AARCH64_FEATURE (FLAGMANIP), AARCH64_FEATURE (FLAGM)},
d6a14e41 10465 {"pauth", AARCH64_FEATURE (PAUTH), AARCH64_NO_FEATURES},
43291582 10466 {"xs", AARCH64_FEATURE (XS), AARCH64_NO_FEATURES},
59255bf7 10467 {"wfxt", AARCH64_FEATURE (WFXT), AARCH64_NO_FEATURES},
4abb672a
RS
10468 {"mops", AARCH64_FEATURE (MOPS), AARCH64_NO_FEATURES},
10469 {"hbc", AARCH64_FEATURE (HBC), AARCH64_NO_FEATURES},
10470 {"cssc", AARCH64_FEATURE (CSSC), AARCH64_NO_FEATURES},
6c0ecdba 10471 {"chk", AARCH64_FEATURE (CHK), AARCH64_NO_FEATURES},
f985c251 10472 {"gcs", AARCH64_FEATURE (GCS), AARCH64_NO_FEATURES},
9203a155 10473 {"the", AARCH64_FEATURE (THE), AARCH64_NO_FEATURES},
311276f1 10474 {"rasv2", AARCH64_FEATURE (RASv2), AARCH64_FEATURE (RAS)},
d645278c 10475 {"ite", AARCH64_FEATURE (ITE), AARCH64_NO_FEATURES},
53071aac 10476 {"d128", AARCH64_FEATURE (D128), D128_FEATURE_DEPS},
7e8d2d87 10477 {"b16b16", AARCH64_FEATURE (B16B16), AARCH64_FEATURE (SVE2)},
89e06ec1 10478 {"sme2p1", AARCH64_FEATURE (SME2p1), AARCH64_FEATURE (SME2)},
88601c2d 10479 {"sve2p1", AARCH64_FEATURE (SVE2p1), AARCH64_FEATURE (SVE2)},
9e263f69 10480 {"rcpc3", AARCH64_FEATURE (RCPC3), AARCH64_FEATURE (RCPC2)},
4792a423 10481 {"cpa", AARCH64_FEATURE (CPA), AARCH64_NO_FEATURES},
f3f34f2b 10482 {"faminmax", AARCH64_FEATURE (FAMINMAX), AARCH64_FEATURE (SIMD)},
4abb672a 10483 {NULL, AARCH64_NO_FEATURES, AARCH64_NO_FEATURES},
a06ea964
NC
10484};
10485
10486struct aarch64_long_option_table
10487{
e0471c16
TS
10488 const char *option; /* Substring to match. */
10489 const char *help; /* Help information. */
17b9d67d 10490 int (*func) (const char *subopt); /* Function to decode sub-option. */
a06ea964
NC
10491 char *deprecated; /* If non-null, print this message. */
10492};
10493
93d8990c
SN
10494/* Transitive closure of features depending on set. */
10495static aarch64_feature_set
10496aarch64_feature_disable_set (aarch64_feature_set set)
10497{
10498 const struct aarch64_option_cpu_value_table *opt;
4abb672a 10499 aarch64_feature_set prev = AARCH64_NO_FEATURES;
93d8990c 10500
4abb672a
RS
10501 while (!AARCH64_CPU_HAS_ALL_FEATURES (prev, set))
10502 {
10503 prev = set;
10504 for (opt = aarch64_features; opt->name != NULL; opt++)
10505 if (AARCH64_CPU_HAS_ANY_FEATURES (opt->require, set))
10506 AARCH64_MERGE_FEATURE_SETS (set, set, opt->value);
10507 }
93d8990c
SN
10508 return set;
10509}
10510
10511/* Transitive closure of dependencies of set. */
10512static aarch64_feature_set
10513aarch64_feature_enable_set (aarch64_feature_set set)
10514{
10515 const struct aarch64_option_cpu_value_table *opt;
4abb672a 10516 aarch64_feature_set prev = AARCH64_NO_FEATURES;
93d8990c 10517
4abb672a
RS
10518 while (!AARCH64_CPU_HAS_ALL_FEATURES (prev, set))
10519 {
10520 prev = set;
10521 for (opt = aarch64_features; opt->name != NULL; opt++)
10522 if (AARCH64_CPU_HAS_ALL_FEATURES (set, opt->value))
10523 AARCH64_MERGE_FEATURE_SETS (set, set, opt->require);
10524 }
93d8990c
SN
10525 return set;
10526}
10527
a06ea964 10528static int
82b8a785 10529aarch64_parse_features (const char *str, const aarch64_feature_set **opt_p,
5b7c81bd 10530 bool ext_only)
a06ea964
NC
10531{
10532 /* We insist on extensions being added before being removed. We achieve
10533 this by using the ADDING_VALUE variable to indicate whether we are
10534 adding an extension (1) or removing it (0) and only allowing it to
10535 change in the order -1 -> 1 -> 0. */
10536 int adding_value = -1;
325801bd 10537 aarch64_feature_set *ext_set = XNEW (aarch64_feature_set);
a06ea964
NC
10538
10539 /* Copy the feature set, so that we can modify it. */
10540 *ext_set = **opt_p;
10541 *opt_p = ext_set;
10542
10543 while (str != NULL && *str != 0)
10544 {
10545 const struct aarch64_option_cpu_value_table *opt;
82b8a785 10546 const char *ext = NULL;
a06ea964
NC
10547 int optlen;
10548
ae527cd8 10549 if (!ext_only)
a06ea964 10550 {
ae527cd8
JB
10551 if (*str != '+')
10552 {
10553 as_bad (_("invalid architectural extension"));
10554 return 0;
10555 }
a06ea964 10556
ae527cd8
JB
10557 ext = strchr (++str, '+');
10558 }
a06ea964
NC
10559
10560 if (ext != NULL)
10561 optlen = ext - str;
10562 else
10563 optlen = strlen (str);
10564
d34049e8 10565 if (optlen >= 2 && startswith (str, "no"))
a06ea964
NC
10566 {
10567 if (adding_value != 0)
10568 adding_value = 0;
10569 optlen -= 2;
10570 str += 2;
10571 }
10572 else if (optlen > 0)
10573 {
10574 if (adding_value == -1)
10575 adding_value = 1;
10576 else if (adding_value != 1)
10577 {
10578 as_bad (_("must specify extensions to add before specifying "
10579 "those to remove"));
5b7c81bd 10580 return false;
a06ea964
NC
10581 }
10582 }
10583
10584 if (optlen == 0)
10585 {
10586 as_bad (_("missing architectural extension"));
10587 return 0;
10588 }
10589
10590 gas_assert (adding_value != -1);
10591
10592 for (opt = aarch64_features; opt->name != NULL; opt++)
c17c7aaf
AC
10593 if (optlen == (int) strlen(opt->name)
10594 && strncmp (opt->name, str, optlen) == 0)
a06ea964 10595 {
93d8990c
SN
10596 aarch64_feature_set set;
10597
a06ea964
NC
10598 /* Add or remove the extension. */
10599 if (adding_value)
93d8990c
SN
10600 {
10601 set = aarch64_feature_enable_set (opt->value);
10602 AARCH64_MERGE_FEATURE_SETS (*ext_set, *ext_set, set);
10603 }
a06ea964 10604 else
93d8990c
SN
10605 {
10606 set = aarch64_feature_disable_set (opt->value);
4abb672a 10607 AARCH64_CLEAR_FEATURES (*ext_set, *ext_set, set);
93d8990c 10608 }
a06ea964
NC
10609 break;
10610 }
10611
10612 if (opt->name == NULL)
10613 {
10614 as_bad (_("unknown architectural extension `%s'"), str);
10615 return 0;
10616 }
10617
10618 str = ext;
10619 };
10620
10621 return 1;
10622}
10623
10624static int
17b9d67d 10625aarch64_parse_cpu (const char *str)
a06ea964
NC
10626{
10627 const struct aarch64_cpu_option_table *opt;
82b8a785 10628 const char *ext = strchr (str, '+');
a06ea964
NC
10629 size_t optlen;
10630
10631 if (ext != NULL)
10632 optlen = ext - str;
10633 else
10634 optlen = strlen (str);
10635
10636 if (optlen == 0)
10637 {
10638 as_bad (_("missing cpu name `%s'"), str);
10639 return 0;
10640 }
10641
10642 for (opt = aarch64_cpus; opt->name != NULL; opt++)
10643 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
10644 {
10645 mcpu_cpu_opt = &opt->value;
10646 if (ext != NULL)
5b7c81bd 10647 return aarch64_parse_features (ext, &mcpu_cpu_opt, false);
a06ea964
NC
10648
10649 return 1;
10650 }
10651
10652 as_bad (_("unknown cpu `%s'"), str);
10653 return 0;
10654}
10655
10656static int
17b9d67d 10657aarch64_parse_arch (const char *str)
a06ea964
NC
10658{
10659 const struct aarch64_arch_option_table *opt;
82b8a785 10660 const char *ext = strchr (str, '+');
a06ea964
NC
10661 size_t optlen;
10662
10663 if (ext != NULL)
10664 optlen = ext - str;
10665 else
10666 optlen = strlen (str);
10667
10668 if (optlen == 0)
10669 {
10670 as_bad (_("missing architecture name `%s'"), str);
10671 return 0;
10672 }
10673
10674 for (opt = aarch64_archs; opt->name != NULL; opt++)
10675 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
10676 {
10677 march_cpu_opt = &opt->value;
10678 if (ext != NULL)
5b7c81bd 10679 return aarch64_parse_features (ext, &march_cpu_opt, false);
a06ea964
NC
10680
10681 return 1;
10682 }
10683
10684 as_bad (_("unknown architecture `%s'\n"), str);
10685 return 0;
10686}
10687
69091a2c
YZ
10688/* ABIs. */
10689struct aarch64_option_abi_value_table
10690{
e0471c16 10691 const char *name;
69091a2c
YZ
10692 enum aarch64_abi_type value;
10693};
10694
10695static const struct aarch64_option_abi_value_table aarch64_abis[] = {
c60b3806 10696#ifdef OBJ_ELF
69091a2c
YZ
10697 {"ilp32", AARCH64_ABI_ILP32},
10698 {"lp64", AARCH64_ABI_LP64},
c60b3806
JT
10699#else
10700 {"llp64", AARCH64_ABI_LLP64},
10701#endif
69091a2c
YZ
10702};
10703
10704static int
17b9d67d 10705aarch64_parse_abi (const char *str)
69091a2c 10706{
5703197e 10707 unsigned int i;
69091a2c 10708
5703197e 10709 if (str[0] == '\0')
69091a2c
YZ
10710 {
10711 as_bad (_("missing abi name `%s'"), str);
10712 return 0;
10713 }
10714
5703197e
TS
10715 for (i = 0; i < ARRAY_SIZE (aarch64_abis); i++)
10716 if (strcmp (str, aarch64_abis[i].name) == 0)
69091a2c 10717 {
5703197e 10718 aarch64_abi = aarch64_abis[i].value;
69091a2c
YZ
10719 return 1;
10720 }
10721
10722 as_bad (_("unknown abi `%s'\n"), str);
10723 return 0;
10724}
10725
a06ea964 10726static struct aarch64_long_option_table aarch64_long_opts[] = {
69091a2c
YZ
10727 {"mabi=", N_("<abi name>\t specify for ABI <abi name>"),
10728 aarch64_parse_abi, NULL},
a06ea964
NC
10729 {"mcpu=", N_("<cpu name>\t assemble for CPU <cpu name>"),
10730 aarch64_parse_cpu, NULL},
10731 {"march=", N_("<arch name>\t assemble for architecture <arch name>"),
10732 aarch64_parse_arch, NULL},
10733 {NULL, NULL, 0, NULL}
10734};
10735
10736int
17b9d67d 10737md_parse_option (int c, const char *arg)
a06ea964
NC
10738{
10739 struct aarch64_option_table *opt;
10740 struct aarch64_long_option_table *lopt;
10741
10742 switch (c)
10743 {
10744#ifdef OPTION_EB
10745 case OPTION_EB:
10746 target_big_endian = 1;
10747 break;
10748#endif
10749
10750#ifdef OPTION_EL
10751 case OPTION_EL:
10752 target_big_endian = 0;
10753 break;
10754#endif
10755
10756 case 'a':
10757 /* Listing option. Just ignore these, we don't support additional
10758 ones. */
10759 return 0;
10760
10761 default:
10762 for (opt = aarch64_opts; opt->option != NULL; opt++)
10763 {
10764 if (c == opt->option[0]
10765 && ((arg == NULL && opt->option[1] == 0)
10766 || streq (arg, opt->option + 1)))
10767 {
10768 /* If the option is deprecated, tell the user. */
10769 if (opt->deprecated != NULL)
10770 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
10771 arg ? arg : "", _(opt->deprecated));
10772
10773 if (opt->var != NULL)
10774 *opt->var = opt->value;
10775
10776 return 1;
10777 }
10778 }
10779
10780 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
10781 {
10782 /* These options are expected to have an argument. */
10783 if (c == lopt->option[0]
10784 && arg != NULL
d34049e8 10785 && startswith (arg, lopt->option + 1))
a06ea964
NC
10786 {
10787 /* If the option is deprecated, tell the user. */
10788 if (lopt->deprecated != NULL)
10789 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
10790 _(lopt->deprecated));
10791
10792 /* Call the sup-option parser. */
10793 return lopt->func (arg + strlen (lopt->option) - 1);
10794 }
10795 }
10796
10797 return 0;
10798 }
10799
10800 return 1;
10801}
10802
10803void
10804md_show_usage (FILE * fp)
10805{
10806 struct aarch64_option_table *opt;
10807 struct aarch64_long_option_table *lopt;
10808
10809 fprintf (fp, _(" AArch64-specific assembler options:\n"));
10810
10811 for (opt = aarch64_opts; opt->option != NULL; opt++)
10812 if (opt->help != NULL)
10813 fprintf (fp, " -%-23s%s\n", opt->option, _(opt->help));
10814
10815 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
10816 if (lopt->help != NULL)
10817 fprintf (fp, " -%s%s\n", lopt->option, _(lopt->help));
10818
10819#ifdef OPTION_EB
10820 fprintf (fp, _("\
10821 -EB assemble code for a big-endian cpu\n"));
10822#endif
10823
10824#ifdef OPTION_EL
10825 fprintf (fp, _("\
10826 -EL assemble code for a little-endian cpu\n"));
10827#endif
10828}
10829
10830/* Parse a .cpu directive. */
10831
10832static void
10833s_aarch64_cpu (int ignored ATTRIBUTE_UNUSED)
10834{
10835 const struct aarch64_cpu_option_table *opt;
10836 char saved_char;
10837 char *name;
10838 char *ext;
10839 size_t optlen;
10840
10841 name = input_line_pointer;
e8f20526 10842 input_line_pointer = find_end_of_line (input_line_pointer, flag_m68k_mri);
a06ea964
NC
10843 saved_char = *input_line_pointer;
10844 *input_line_pointer = 0;
10845
10846 ext = strchr (name, '+');
10847
10848 if (ext != NULL)
10849 optlen = ext - name;
10850 else
10851 optlen = strlen (name);
10852
10853 /* Skip the first "all" entry. */
10854 for (opt = aarch64_cpus + 1; opt->name != NULL; opt++)
10855 if (strlen (opt->name) == optlen
10856 && strncmp (name, opt->name, optlen) == 0)
10857 {
10858 mcpu_cpu_opt = &opt->value;
10859 if (ext != NULL)
5b7c81bd 10860 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, false))
a06ea964
NC
10861 return;
10862
10863 cpu_variant = *mcpu_cpu_opt;
10864
10865 *input_line_pointer = saved_char;
10866 demand_empty_rest_of_line ();
10867 return;
10868 }
10869 as_bad (_("unknown cpu `%s'"), name);
10870 *input_line_pointer = saved_char;
10871 ignore_rest_of_line ();
10872}
10873
10874
10875/* Parse a .arch directive. */
10876
10877static void
10878s_aarch64_arch (int ignored ATTRIBUTE_UNUSED)
10879{
10880 const struct aarch64_arch_option_table *opt;
10881 char saved_char;
10882 char *name;
10883 char *ext;
10884 size_t optlen;
10885
10886 name = input_line_pointer;
e8f20526 10887 input_line_pointer = find_end_of_line (input_line_pointer, flag_m68k_mri);
a06ea964
NC
10888 saved_char = *input_line_pointer;
10889 *input_line_pointer = 0;
10890
10891 ext = strchr (name, '+');
10892
10893 if (ext != NULL)
10894 optlen = ext - name;
10895 else
10896 optlen = strlen (name);
10897
10898 /* Skip the first "all" entry. */
10899 for (opt = aarch64_archs + 1; opt->name != NULL; opt++)
10900 if (strlen (opt->name) == optlen
10901 && strncmp (name, opt->name, optlen) == 0)
10902 {
10903 mcpu_cpu_opt = &opt->value;
10904 if (ext != NULL)
5b7c81bd 10905 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, false))
a06ea964
NC
10906 return;
10907
10908 cpu_variant = *mcpu_cpu_opt;
10909
10910 *input_line_pointer = saved_char;
10911 demand_empty_rest_of_line ();
10912 return;
10913 }
10914
10915 as_bad (_("unknown architecture `%s'\n"), name);
10916 *input_line_pointer = saved_char;
10917 ignore_rest_of_line ();
10918}
10919
ae527cd8
JB
10920/* Parse a .arch_extension directive. */
10921
10922static void
10923s_aarch64_arch_extension (int ignored ATTRIBUTE_UNUSED)
10924{
10925 char saved_char;
e8f20526 10926 char *ext = input_line_pointer;
ae527cd8 10927
e8f20526 10928 input_line_pointer = find_end_of_line (input_line_pointer, flag_m68k_mri);
ae527cd8
JB
10929 saved_char = *input_line_pointer;
10930 *input_line_pointer = 0;
10931
5b7c81bd 10932 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, true))
ae527cd8
JB
10933 return;
10934
10935 cpu_variant = *mcpu_cpu_opt;
10936
10937 *input_line_pointer = saved_char;
10938 demand_empty_rest_of_line ();
10939}
10940
a06ea964
NC
10941/* Copy symbol information. */
10942
10943void
10944aarch64_copy_symbol_attributes (symbolS * dest, symbolS * src)
10945{
10946 AARCH64_GET_FLAG (dest) = AARCH64_GET_FLAG (src);
10947}
0b4eac57
SN
10948
10949#ifdef OBJ_ELF
10950/* Same as elf_copy_symbol_attributes, but without copying st_other.
10951 This is needed so AArch64 specific st_other values can be independently
10952 specified for an IFUNC resolver (that is called by the dynamic linker)
10953 and the symbol it resolves (aliased to the resolver). In particular,
10954 if a function symbol has special st_other value set via directives,
10955 then attaching an IFUNC resolver to that symbol should not override
10956 the st_other setting. Requiring the directive on the IFUNC resolver
10957 symbol would be unexpected and problematic in C code, where the two
10958 symbols appear as two independent function declarations. */
10959
10960void
10961aarch64_elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
10962{
10963 struct elf_obj_sy *srcelf = symbol_get_obj (src);
10964 struct elf_obj_sy *destelf = symbol_get_obj (dest);
a3a7f5e1
FS
10965 /* If size is unset, copy size from src. Because we don't track whether
10966 .size has been used, we can't differentiate .size dest, 0 from the case
10967 where dest's size is unset. */
10968 if (!destelf->size && S_GET_SIZE (dest) == 0)
0b4eac57 10969 {
a3a7f5e1
FS
10970 if (srcelf->size)
10971 {
10972 destelf->size = XNEW (expressionS);
10973 *destelf->size = *srcelf->size;
10974 }
10975 S_SET_SIZE (dest, S_GET_SIZE (src));
0b4eac57 10976 }
0b4eac57
SN
10977}
10978#endif