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