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