Add support for V_4B so we can properly reject it.
[deliverable/binutils-gdb.git] / gas / config / tc-aarch64.c
CommitLineData
a06ea964
NC
1/* tc-aarch64.c -- Assemble for the AArch64 ISA
2
2571583a 3 Copyright (C) 2009-2017 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
a06ea964
NC
2580 /* Get to the page containing GOT entry for a symbol. */
2581 {"got", 1,
6f4a313b 2582 0, /* adr_type */
a06ea964
NC
2583 BFD_RELOC_AARCH64_ADR_GOT_PAGE,
2584 0,
2585 0,
74ad790c 2586 0,
4aa2c5e2
MS
2587 BFD_RELOC_AARCH64_GOT_LD_PREL19},
2588
a06ea964
NC
2589 /* 12 bit offset into the page containing GOT entry for that symbol. */
2590 {"got_lo12", 0,
6f4a313b 2591 0, /* adr_type */
a06ea964
NC
2592 0,
2593 0,
2594 0,
74ad790c
MS
2595 BFD_RELOC_AARCH64_LD_GOT_LO12_NC,
2596 0},
a06ea964 2597
ca632371
RL
2598 /* 0-15 bits of address/value: MOVk, no check. */
2599 {"gotoff_g0_nc", 0,
2600 0, /* adr_type */
2601 0,
2602 BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC,
2603 0,
2604 0,
2605 0},
2606
654248e7
RL
2607 /* Most significant bits 16-31 of address/value: MOVZ. */
2608 {"gotoff_g1", 0,
2609 0, /* adr_type */
2610 0,
2611 BFD_RELOC_AARCH64_MOVW_GOTOFF_G1,
2612 0,
2613 0,
2614 0},
2615
87f5fbcc
RL
2616 /* 15 bit offset into the page containing GOT entry for that symbol. */
2617 {"gotoff_lo15", 0,
2618 0, /* adr_type */
2619 0,
2620 0,
2621 0,
2622 BFD_RELOC_AARCH64_LD64_GOTOFF_LO15,
2623 0},
2624
3b957e5b
RL
2625 /* Get to the page containing GOT TLS entry for a symbol */
2626 {"gottprel_g0_nc", 0,
2627 0, /* adr_type */
2628 0,
2629 BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC,
2630 0,
2631 0,
2632 0},
2633
2634 /* Get to the page containing GOT TLS entry for a symbol */
2635 {"gottprel_g1", 0,
2636 0, /* adr_type */
2637 0,
2638 BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1,
2639 0,
2640 0,
2641 0},
2642
a06ea964
NC
2643 /* Get to the page containing GOT TLS entry for a symbol */
2644 {"tlsgd", 0,
3c12b054 2645 BFD_RELOC_AARCH64_TLSGD_ADR_PREL21, /* adr_type */
a06ea964
NC
2646 BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21,
2647 0,
2648 0,
74ad790c 2649 0,
a06ea964
NC
2650 0},
2651
2652 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2653 {"tlsgd_lo12", 0,
6f4a313b 2654 0, /* adr_type */
a06ea964
NC
2655 0,
2656 0,
2657 BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC,
74ad790c 2658 0,
a06ea964
NC
2659 0},
2660
3e8286c0
RL
2661 /* Lower 16 bits address/value: MOVk. */
2662 {"tlsgd_g0_nc", 0,
2663 0, /* adr_type */
2664 0,
2665 BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC,
2666 0,
2667 0,
2668 0},
2669
1aa66fb1
RL
2670 /* Most significant bits 16-31 of address/value: MOVZ. */
2671 {"tlsgd_g1", 0,
2672 0, /* adr_type */
2673 0,
2674 BFD_RELOC_AARCH64_TLSGD_MOVW_G1,
2675 0,
2676 0,
2677 0},
2678
a06ea964
NC
2679 /* Get to the page containing GOT TLS entry for a symbol */
2680 {"tlsdesc", 0,
389b8029 2681 BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21, /* adr_type */
418009c2 2682 BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21,
a06ea964
NC
2683 0,
2684 0,
74ad790c 2685 0,
1ada945d 2686 BFD_RELOC_AARCH64_TLSDESC_LD_PREL19},
a06ea964
NC
2687
2688 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2689 {"tlsdesc_lo12", 0,
6f4a313b 2690 0, /* adr_type */
a06ea964
NC
2691 0,
2692 0,
f955cccf 2693 BFD_RELOC_AARCH64_TLSDESC_ADD_LO12,
74ad790c
MS
2694 BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC,
2695 0},
a06ea964 2696
6c37fedc
JW
2697 /* Get to the page containing GOT TLS entry for a symbol.
2698 The same as GD, we allocate two consecutive GOT slots
2699 for module index and module offset, the only difference
33eaf5de 2700 with GD is the module offset should be initialized to
6c37fedc
JW
2701 zero without any outstanding runtime relocation. */
2702 {"tlsldm", 0,
2703 BFD_RELOC_AARCH64_TLSLD_ADR_PREL21, /* adr_type */
1107e076 2704 BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21,
6c37fedc
JW
2705 0,
2706 0,
2707 0,
2708 0},
2709
a12fad50
JW
2710 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2711 {"tlsldm_lo12_nc", 0,
2712 0, /* adr_type */
2713 0,
2714 0,
2715 BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC,
2716 0,
2717 0},
2718
70151fb5
JW
2719 /* 12 bit offset into the module TLS base address. */
2720 {"dtprel_lo12", 0,
2721 0, /* adr_type */
2722 0,
2723 0,
2724 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12,
4c562523 2725 BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12,
70151fb5
JW
2726 0},
2727
13289c10
JW
2728 /* Same as dtprel_lo12, no overflow check. */
2729 {"dtprel_lo12_nc", 0,
2730 0, /* adr_type */
2731 0,
2732 0,
2733 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC,
4c562523 2734 BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC,
13289c10
JW
2735 0},
2736
49df5539
JW
2737 /* bits[23:12] of offset to the module TLS base address. */
2738 {"dtprel_hi12", 0,
2739 0, /* adr_type */
2740 0,
2741 0,
2742 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12,
2743 0,
2744 0},
2745
2746 /* bits[15:0] of offset to the module TLS base address. */
2747 {"dtprel_g0", 0,
2748 0, /* adr_type */
2749 0,
2750 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0,
2751 0,
2752 0,
2753 0},
2754
2755 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0. */
2756 {"dtprel_g0_nc", 0,
2757 0, /* adr_type */
2758 0,
2759 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC,
2760 0,
2761 0,
2762 0},
2763
2764 /* bits[31:16] of offset to the module TLS base address. */
2765 {"dtprel_g1", 0,
2766 0, /* adr_type */
2767 0,
2768 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1,
2769 0,
2770 0,
2771 0},
2772
2773 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1. */
2774 {"dtprel_g1_nc", 0,
2775 0, /* adr_type */
2776 0,
2777 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC,
2778 0,
2779 0,
2780 0},
2781
2782 /* bits[47:32] of offset to the module TLS base address. */
2783 {"dtprel_g2", 0,
2784 0, /* adr_type */
2785 0,
2786 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2,
2787 0,
2788 0,
2789 0},
2790
43a357f9
RL
2791 /* Lower 16 bit offset into GOT entry for a symbol */
2792 {"tlsdesc_off_g0_nc", 0,
2793 0, /* adr_type */
2794 0,
2795 BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC,
2796 0,
2797 0,
2798 0},
2799
2800 /* Higher 16 bit offset into GOT entry for a symbol */
2801 {"tlsdesc_off_g1", 0,
2802 0, /* adr_type */
2803 0,
2804 BFD_RELOC_AARCH64_TLSDESC_OFF_G1,
2805 0,
2806 0,
2807 0},
2808
a06ea964
NC
2809 /* Get to the page containing GOT TLS entry for a symbol */
2810 {"gottprel", 0,
6f4a313b 2811 0, /* adr_type */
a06ea964
NC
2812 BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,
2813 0,
2814 0,
74ad790c 2815 0,
043bf05a 2816 BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19},
a06ea964
NC
2817
2818 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2819 {"gottprel_lo12", 0,
6f4a313b 2820 0, /* adr_type */
a06ea964
NC
2821 0,
2822 0,
2823 0,
74ad790c
MS
2824 BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC,
2825 0},
a06ea964
NC
2826
2827 /* Get tp offset for a symbol. */
2828 {"tprel", 0,
6f4a313b 2829 0, /* adr_type */
a06ea964
NC
2830 0,
2831 0,
2832 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12,
74ad790c 2833 0,
a06ea964
NC
2834 0},
2835
2836 /* Get tp offset for a symbol. */
2837 {"tprel_lo12", 0,
6f4a313b 2838 0, /* adr_type */
a06ea964
NC
2839 0,
2840 0,
2841 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12,
74ad790c 2842 0,
a06ea964
NC
2843 0},
2844
2845 /* Get tp offset for a symbol. */
2846 {"tprel_hi12", 0,
6f4a313b 2847 0, /* adr_type */
a06ea964
NC
2848 0,
2849 0,
2850 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12,
74ad790c 2851 0,
a06ea964
NC
2852 0},
2853
2854 /* Get tp offset for a symbol. */
2855 {"tprel_lo12_nc", 0,
6f4a313b 2856 0, /* adr_type */
a06ea964
NC
2857 0,
2858 0,
2859 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC,
74ad790c 2860 0,
a06ea964
NC
2861 0},
2862
2863 /* Most significant bits 32-47 of address/value: MOVZ. */
2864 {"tprel_g2", 0,
6f4a313b 2865 0, /* adr_type */
a06ea964
NC
2866 0,
2867 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2,
2868 0,
74ad790c 2869 0,
a06ea964
NC
2870 0},
2871
2872 /* Most significant bits 16-31 of address/value: MOVZ. */
2873 {"tprel_g1", 0,
6f4a313b 2874 0, /* adr_type */
a06ea964
NC
2875 0,
2876 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1,
2877 0,
74ad790c 2878 0,
a06ea964
NC
2879 0},
2880
2881 /* Most significant bits 16-31 of address/value: MOVZ, no check. */
2882 {"tprel_g1_nc", 0,
6f4a313b 2883 0, /* adr_type */
a06ea964
NC
2884 0,
2885 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC,
2886 0,
74ad790c 2887 0,
a06ea964
NC
2888 0},
2889
2890 /* Most significant bits 0-15 of address/value: MOVZ. */
2891 {"tprel_g0", 0,
6f4a313b 2892 0, /* adr_type */
a06ea964
NC
2893 0,
2894 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0,
2895 0,
74ad790c 2896 0,
a06ea964
NC
2897 0},
2898
2899 /* Most significant bits 0-15 of address/value: MOVZ, no check. */
2900 {"tprel_g0_nc", 0,
6f4a313b 2901 0, /* adr_type */
a06ea964
NC
2902 0,
2903 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC,
2904 0,
74ad790c 2905 0,
a06ea964 2906 0},
a921b5bd
JW
2907
2908 /* 15bit offset from got entry to base address of GOT table. */
2909 {"gotpage_lo15", 0,
2910 0,
2911 0,
2912 0,
2913 0,
2914 BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15,
2915 0},
3d715ce4
JW
2916
2917 /* 14bit offset from got entry to base address of GOT table. */
2918 {"gotpage_lo14", 0,
2919 0,
2920 0,
2921 0,
2922 0,
2923 BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14,
2924 0},
a06ea964
NC
2925};
2926
2927/* Given the address of a pointer pointing to the textual name of a
2928 relocation as may appear in assembler source, attempt to find its
2929 details in reloc_table. The pointer will be updated to the character
2930 after the trailing colon. On failure, NULL will be returned;
2931 otherwise return the reloc_table_entry. */
2932
2933static struct reloc_table_entry *
2934find_reloc_table_entry (char **str)
2935{
2936 unsigned int i;
2937 for (i = 0; i < ARRAY_SIZE (reloc_table); i++)
2938 {
2939 int length = strlen (reloc_table[i].name);
2940
2941 if (strncasecmp (reloc_table[i].name, *str, length) == 0
2942 && (*str)[length] == ':')
2943 {
2944 *str += (length + 1);
2945 return &reloc_table[i];
2946 }
2947 }
2948
2949 return NULL;
2950}
2951
2952/* Mode argument to parse_shift and parser_shifter_operand. */
2953enum parse_shift_mode
2954{
98907a70 2955 SHIFTED_NONE, /* no shifter allowed */
a06ea964
NC
2956 SHIFTED_ARITH_IMM, /* "rn{,lsl|lsr|asl|asr|uxt|sxt #n}" or
2957 "#imm{,lsl #n}" */
2958 SHIFTED_LOGIC_IMM, /* "rn{,lsl|lsr|asl|asr|ror #n}" or
2959 "#imm" */
2960 SHIFTED_LSL, /* bare "lsl #n" */
2442d846 2961 SHIFTED_MUL, /* bare "mul #n" */
a06ea964 2962 SHIFTED_LSL_MSL, /* "lsl|msl #n" */
98907a70 2963 SHIFTED_MUL_VL, /* "mul vl" */
a06ea964
NC
2964 SHIFTED_REG_OFFSET /* [su]xtw|sxtx {#n} or lsl #n */
2965};
2966
2967/* Parse a <shift> operator on an AArch64 data processing instruction.
2968 Return TRUE on success; otherwise return FALSE. */
2969static bfd_boolean
2970parse_shift (char **str, aarch64_opnd_info *operand, enum parse_shift_mode mode)
2971{
2972 const struct aarch64_name_value_pair *shift_op;
2973 enum aarch64_modifier_kind kind;
2974 expressionS exp;
2975 int exp_has_prefix;
2976 char *s = *str;
2977 char *p = s;
2978
2979 for (p = *str; ISALPHA (*p); p++)
2980 ;
2981
2982 if (p == *str)
2983 {
2984 set_syntax_error (_("shift expression expected"));
2985 return FALSE;
2986 }
2987
2988 shift_op = hash_find_n (aarch64_shift_hsh, *str, p - *str);
2989
2990 if (shift_op == NULL)
2991 {
2992 set_syntax_error (_("shift operator expected"));
2993 return FALSE;
2994 }
2995
2996 kind = aarch64_get_operand_modifier (shift_op);
2997
2998 if (kind == AARCH64_MOD_MSL && mode != SHIFTED_LSL_MSL)
2999 {
3000 set_syntax_error (_("invalid use of 'MSL'"));
3001 return FALSE;
3002 }
3003
2442d846 3004 if (kind == AARCH64_MOD_MUL
98907a70
RS
3005 && mode != SHIFTED_MUL
3006 && mode != SHIFTED_MUL_VL)
2442d846
RS
3007 {
3008 set_syntax_error (_("invalid use of 'MUL'"));
3009 return FALSE;
3010 }
3011
a06ea964
NC
3012 switch (mode)
3013 {
3014 case SHIFTED_LOGIC_IMM:
535b785f 3015 if (aarch64_extend_operator_p (kind))
a06ea964
NC
3016 {
3017 set_syntax_error (_("extending shift is not permitted"));
3018 return FALSE;
3019 }
3020 break;
3021
3022 case SHIFTED_ARITH_IMM:
3023 if (kind == AARCH64_MOD_ROR)
3024 {
3025 set_syntax_error (_("'ROR' shift is not permitted"));
3026 return FALSE;
3027 }
3028 break;
3029
3030 case SHIFTED_LSL:
3031 if (kind != AARCH64_MOD_LSL)
3032 {
3033 set_syntax_error (_("only 'LSL' shift is permitted"));
3034 return FALSE;
3035 }
3036 break;
3037
2442d846
RS
3038 case SHIFTED_MUL:
3039 if (kind != AARCH64_MOD_MUL)
3040 {
3041 set_syntax_error (_("only 'MUL' is permitted"));
3042 return FALSE;
3043 }
3044 break;
3045
98907a70
RS
3046 case SHIFTED_MUL_VL:
3047 /* "MUL VL" consists of two separate tokens. Require the first
3048 token to be "MUL" and look for a following "VL". */
3049 if (kind == AARCH64_MOD_MUL)
3050 {
3051 skip_whitespace (p);
3052 if (strncasecmp (p, "vl", 2) == 0 && !ISALPHA (p[2]))
3053 {
3054 p += 2;
3055 kind = AARCH64_MOD_MUL_VL;
3056 break;
3057 }
3058 }
3059 set_syntax_error (_("only 'MUL VL' is permitted"));
3060 return FALSE;
3061
a06ea964
NC
3062 case SHIFTED_REG_OFFSET:
3063 if (kind != AARCH64_MOD_UXTW && kind != AARCH64_MOD_LSL
3064 && kind != AARCH64_MOD_SXTW && kind != AARCH64_MOD_SXTX)
3065 {
3066 set_fatal_syntax_error
3067 (_("invalid shift for the register offset addressing mode"));
3068 return FALSE;
3069 }
3070 break;
3071
3072 case SHIFTED_LSL_MSL:
3073 if (kind != AARCH64_MOD_LSL && kind != AARCH64_MOD_MSL)
3074 {
3075 set_syntax_error (_("invalid shift operator"));
3076 return FALSE;
3077 }
3078 break;
3079
3080 default:
3081 abort ();
3082 }
3083
3084 /* Whitespace can appear here if the next thing is a bare digit. */
3085 skip_whitespace (p);
3086
3087 /* Parse shift amount. */
3088 exp_has_prefix = 0;
98907a70 3089 if ((mode == SHIFTED_REG_OFFSET && *p == ']') || kind == AARCH64_MOD_MUL_VL)
a06ea964
NC
3090 exp.X_op = O_absent;
3091 else
3092 {
3093 if (is_immediate_prefix (*p))
3094 {
3095 p++;
3096 exp_has_prefix = 1;
3097 }
3098 my_get_expression (&exp, &p, GE_NO_PREFIX, 0);
3099 }
98907a70
RS
3100 if (kind == AARCH64_MOD_MUL_VL)
3101 /* For consistency, give MUL VL the same shift amount as an implicit
3102 MUL #1. */
3103 operand->shifter.amount = 1;
3104 else if (exp.X_op == O_absent)
a06ea964 3105 {
535b785f 3106 if (!aarch64_extend_operator_p (kind) || exp_has_prefix)
a06ea964
NC
3107 {
3108 set_syntax_error (_("missing shift amount"));
3109 return FALSE;
3110 }
3111 operand->shifter.amount = 0;
3112 }
3113 else if (exp.X_op != O_constant)
3114 {
3115 set_syntax_error (_("constant shift amount required"));
3116 return FALSE;
3117 }
2442d846
RS
3118 /* For parsing purposes, MUL #n has no inherent range. The range
3119 depends on the operand and will be checked by operand-specific
3120 routines. */
3121 else if (kind != AARCH64_MOD_MUL
3122 && (exp.X_add_number < 0 || exp.X_add_number > 63))
a06ea964
NC
3123 {
3124 set_fatal_syntax_error (_("shift amount out of range 0 to 63"));
3125 return FALSE;
3126 }
3127 else
3128 {
3129 operand->shifter.amount = exp.X_add_number;
3130 operand->shifter.amount_present = 1;
3131 }
3132
3133 operand->shifter.operator_present = 1;
3134 operand->shifter.kind = kind;
3135
3136 *str = p;
3137 return TRUE;
3138}
3139
3140/* Parse a <shifter_operand> for a data processing instruction:
3141
3142 #<immediate>
3143 #<immediate>, LSL #imm
3144
3145 Validation of immediate operands is deferred to md_apply_fix.
3146
3147 Return TRUE on success; otherwise return FALSE. */
3148
3149static bfd_boolean
3150parse_shifter_operand_imm (char **str, aarch64_opnd_info *operand,
3151 enum parse_shift_mode mode)
3152{
3153 char *p;
3154
3155 if (mode != SHIFTED_ARITH_IMM && mode != SHIFTED_LOGIC_IMM)
3156 return FALSE;
3157
3158 p = *str;
3159
3160 /* Accept an immediate expression. */
3161 if (! my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX, 1))
3162 return FALSE;
3163
3164 /* Accept optional LSL for arithmetic immediate values. */
3165 if (mode == SHIFTED_ARITH_IMM && skip_past_comma (&p))
3166 if (! parse_shift (&p, operand, SHIFTED_LSL))
3167 return FALSE;
3168
3169 /* Not accept any shifter for logical immediate values. */
3170 if (mode == SHIFTED_LOGIC_IMM && skip_past_comma (&p)
3171 && parse_shift (&p, operand, mode))
3172 {
3173 set_syntax_error (_("unexpected shift operator"));
3174 return FALSE;
3175 }
3176
3177 *str = p;
3178 return TRUE;
3179}
3180
3181/* Parse a <shifter_operand> for a data processing instruction:
3182
3183 <Rm>
3184 <Rm>, <shift>
3185 #<immediate>
3186 #<immediate>, LSL #imm
3187
3188 where <shift> is handled by parse_shift above, and the last two
3189 cases are handled by the function above.
3190
3191 Validation of immediate operands is deferred to md_apply_fix.
3192
3193 Return TRUE on success; otherwise return FALSE. */
3194
3195static bfd_boolean
3196parse_shifter_operand (char **str, aarch64_opnd_info *operand,
3197 enum parse_shift_mode mode)
3198{
e1b988bb
RS
3199 const reg_entry *reg;
3200 aarch64_opnd_qualifier_t qualifier;
a06ea964
NC
3201 enum aarch64_operand_class opd_class
3202 = aarch64_get_operand_class (operand->type);
3203
e1b988bb
RS
3204 reg = aarch64_reg_parse_32_64 (str, &qualifier);
3205 if (reg)
a06ea964
NC
3206 {
3207 if (opd_class == AARCH64_OPND_CLASS_IMMEDIATE)
3208 {
3209 set_syntax_error (_("unexpected register in the immediate operand"));
3210 return FALSE;
3211 }
3212
e1b988bb 3213 if (!aarch64_check_reg_type (reg, REG_TYPE_R_Z))
a06ea964 3214 {
e1b988bb 3215 set_syntax_error (_(get_reg_expected_msg (REG_TYPE_R_Z)));
a06ea964
NC
3216 return FALSE;
3217 }
3218
e1b988bb
RS
3219 operand->reg.regno = reg->number;
3220 operand->qualifier = qualifier;
a06ea964
NC
3221
3222 /* Accept optional shift operation on register. */
3223 if (! skip_past_comma (str))
3224 return TRUE;
3225
3226 if (! parse_shift (str, operand, mode))
3227 return FALSE;
3228
3229 return TRUE;
3230 }
3231 else if (opd_class == AARCH64_OPND_CLASS_MODIFIED_REG)
3232 {
3233 set_syntax_error
3234 (_("integer register expected in the extended/shifted operand "
3235 "register"));
3236 return FALSE;
3237 }
3238
3239 /* We have a shifted immediate variable. */
3240 return parse_shifter_operand_imm (str, operand, mode);
3241}
3242
3243/* Return TRUE on success; return FALSE otherwise. */
3244
3245static bfd_boolean
3246parse_shifter_operand_reloc (char **str, aarch64_opnd_info *operand,
3247 enum parse_shift_mode mode)
3248{
3249 char *p = *str;
3250
3251 /* Determine if we have the sequence of characters #: or just :
3252 coming next. If we do, then we check for a :rello: relocation
3253 modifier. If we don't, punt the whole lot to
3254 parse_shifter_operand. */
3255
3256 if ((p[0] == '#' && p[1] == ':') || p[0] == ':')
3257 {
3258 struct reloc_table_entry *entry;
3259
3260 if (p[0] == '#')
3261 p += 2;
3262 else
3263 p++;
3264 *str = p;
3265
3266 /* Try to parse a relocation. Anything else is an error. */
3267 if (!(entry = find_reloc_table_entry (str)))
3268 {
3269 set_syntax_error (_("unknown relocation modifier"));
3270 return FALSE;
3271 }
3272
3273 if (entry->add_type == 0)
3274 {
3275 set_syntax_error
3276 (_("this relocation modifier is not allowed on this instruction"));
3277 return FALSE;
3278 }
3279
3280 /* Save str before we decompose it. */
3281 p = *str;
3282
3283 /* Next, we parse the expression. */
3284 if (! my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX, 1))
3285 return FALSE;
3286
3287 /* Record the relocation type (use the ADD variant here). */
3288 inst.reloc.type = entry->add_type;
3289 inst.reloc.pc_rel = entry->pc_rel;
3290
3291 /* If str is empty, we've reached the end, stop here. */
3292 if (**str == '\0')
3293 return TRUE;
3294
55d9b4c1 3295 /* Otherwise, we have a shifted reloc modifier, so rewind to
a06ea964
NC
3296 recover the variable name and continue parsing for the shifter. */
3297 *str = p;
3298 return parse_shifter_operand_imm (str, operand, mode);
3299 }
3300
3301 return parse_shifter_operand (str, operand, mode);
3302}
3303
3304/* Parse all forms of an address expression. Information is written
3305 to *OPERAND and/or inst.reloc.
3306
3307 The A64 instruction set has the following addressing modes:
3308
3309 Offset
4df068de
RS
3310 [base] // in SIMD ld/st structure
3311 [base{,#0}] // in ld/st exclusive
a06ea964
NC
3312 [base{,#imm}]
3313 [base,Xm{,LSL #imm}]
3314 [base,Xm,SXTX {#imm}]
3315 [base,Wm,(S|U)XTW {#imm}]
3316 Pre-indexed
3317 [base,#imm]!
3318 Post-indexed
3319 [base],#imm
4df068de 3320 [base],Xm // in SIMD ld/st structure
a06ea964
NC
3321 PC-relative (literal)
3322 label
4df068de 3323 SVE:
98907a70 3324 [base,#imm,MUL VL]
4df068de
RS
3325 [base,Zm.D{,LSL #imm}]
3326 [base,Zm.S,(S|U)XTW {#imm}]
3327 [base,Zm.D,(S|U)XTW {#imm}] // ignores top 32 bits of Zm.D elements
3328 [Zn.S,#imm]
3329 [Zn.D,#imm]
3330 [Zn.S,Zm.S{,LSL #imm}] // in ADR
3331 [Zn.D,Zm.D{,LSL #imm}] // in ADR
3332 [Zn.D,Zm.D,(S|U)XTW {#imm}] // in ADR
a06ea964
NC
3333
3334 (As a convenience, the notation "=immediate" is permitted in conjunction
3335 with the pc-relative literal load instructions to automatically place an
3336 immediate value or symbolic address in a nearby literal pool and generate
3337 a hidden label which references it.)
3338
3339 Upon a successful parsing, the address structure in *OPERAND will be
3340 filled in the following way:
3341
3342 .base_regno = <base>
3343 .offset.is_reg // 1 if the offset is a register
3344 .offset.imm = <imm>
3345 .offset.regno = <Rm>
3346
3347 For different addressing modes defined in the A64 ISA:
3348
3349 Offset
3350 .pcrel=0; .preind=1; .postind=0; .writeback=0
3351 Pre-indexed
3352 .pcrel=0; .preind=1; .postind=0; .writeback=1
3353 Post-indexed
3354 .pcrel=0; .preind=0; .postind=1; .writeback=1
3355 PC-relative (literal)
3356 .pcrel=1; .preind=1; .postind=0; .writeback=0
3357
3358 The shift/extension information, if any, will be stored in .shifter.
4df068de
RS
3359 The base and offset qualifiers will be stored in *BASE_QUALIFIER and
3360 *OFFSET_QUALIFIER respectively, with NIL being used if there's no
3361 corresponding register.
a06ea964 3362
4df068de 3363 BASE_TYPE says which types of base register should be accepted and
98907a70
RS
3364 OFFSET_TYPE says the same for offset registers. IMM_SHIFT_MODE
3365 is the type of shifter that is allowed for immediate offsets,
3366 or SHIFTED_NONE if none.
3367
3368 In all other respects, it is the caller's responsibility to check
3369 for addressing modes not supported by the instruction, and to set
3370 inst.reloc.type. */
a06ea964
NC
3371
3372static bfd_boolean
4df068de
RS
3373parse_address_main (char **str, aarch64_opnd_info *operand,
3374 aarch64_opnd_qualifier_t *base_qualifier,
3375 aarch64_opnd_qualifier_t *offset_qualifier,
98907a70
RS
3376 aarch64_reg_type base_type, aarch64_reg_type offset_type,
3377 enum parse_shift_mode imm_shift_mode)
a06ea964
NC
3378{
3379 char *p = *str;
e1b988bb 3380 const reg_entry *reg;
a06ea964
NC
3381 expressionS *exp = &inst.reloc.exp;
3382
4df068de
RS
3383 *base_qualifier = AARCH64_OPND_QLF_NIL;
3384 *offset_qualifier = AARCH64_OPND_QLF_NIL;
a06ea964
NC
3385 if (! skip_past_char (&p, '['))
3386 {
3387 /* =immediate or label. */
3388 operand->addr.pcrel = 1;
3389 operand->addr.preind = 1;
3390
f41aef5f
RE
3391 /* #:<reloc_op>:<symbol> */
3392 skip_past_char (&p, '#');
73866052 3393 if (skip_past_char (&p, ':'))
f41aef5f 3394 {
6f4a313b 3395 bfd_reloc_code_real_type ty;
f41aef5f
RE
3396 struct reloc_table_entry *entry;
3397
3398 /* Try to parse a relocation modifier. Anything else is
3399 an error. */
3400 entry = find_reloc_table_entry (&p);
3401 if (! entry)
3402 {
3403 set_syntax_error (_("unknown relocation modifier"));
3404 return FALSE;
3405 }
3406
6f4a313b
MS
3407 switch (operand->type)
3408 {
3409 case AARCH64_OPND_ADDR_PCREL21:
3410 /* adr */
3411 ty = entry->adr_type;
3412 break;
3413
3414 default:
74ad790c 3415 ty = entry->ld_literal_type;
6f4a313b
MS
3416 break;
3417 }
3418
3419 if (ty == 0)
f41aef5f
RE
3420 {
3421 set_syntax_error
3422 (_("this relocation modifier is not allowed on this "
3423 "instruction"));
3424 return FALSE;
3425 }
3426
3427 /* #:<reloc_op>: */
3428 if (! my_get_expression (exp, &p, GE_NO_PREFIX, 1))
3429 {
3430 set_syntax_error (_("invalid relocation expression"));
3431 return FALSE;
3432 }
a06ea964 3433
f41aef5f 3434 /* #:<reloc_op>:<expr> */
6f4a313b
MS
3435 /* Record the relocation type. */
3436 inst.reloc.type = ty;
f41aef5f
RE
3437 inst.reloc.pc_rel = entry->pc_rel;
3438 }
3439 else
a06ea964 3440 {
f41aef5f
RE
3441
3442 if (skip_past_char (&p, '='))
3443 /* =immediate; need to generate the literal in the literal pool. */
3444 inst.gen_lit_pool = 1;
3445
3446 if (!my_get_expression (exp, &p, GE_NO_PREFIX, 1))
3447 {
3448 set_syntax_error (_("invalid address"));
3449 return FALSE;
3450 }
a06ea964
NC
3451 }
3452
3453 *str = p;
3454 return TRUE;
3455 }
3456
3457 /* [ */
3458
4df068de
RS
3459 reg = aarch64_addr_reg_parse (&p, base_type, base_qualifier);
3460 if (!reg || !aarch64_check_reg_type (reg, base_type))
a06ea964 3461 {
4df068de 3462 set_syntax_error (_(get_reg_expected_msg (base_type)));
a06ea964
NC
3463 return FALSE;
3464 }
e1b988bb 3465 operand->addr.base_regno = reg->number;
a06ea964
NC
3466
3467 /* [Xn */
3468 if (skip_past_comma (&p))
3469 {
3470 /* [Xn, */
3471 operand->addr.preind = 1;
3472
4df068de 3473 reg = aarch64_addr_reg_parse (&p, offset_type, offset_qualifier);
e1b988bb 3474 if (reg)
a06ea964 3475 {
4df068de 3476 if (!aarch64_check_reg_type (reg, offset_type))
e1b988bb 3477 {
4df068de 3478 set_syntax_error (_(get_reg_expected_msg (offset_type)));
e1b988bb
RS
3479 return FALSE;
3480 }
3481
a06ea964 3482 /* [Xn,Rm */
e1b988bb 3483 operand->addr.offset.regno = reg->number;
a06ea964
NC
3484 operand->addr.offset.is_reg = 1;
3485 /* Shifted index. */
3486 if (skip_past_comma (&p))
3487 {
3488 /* [Xn,Rm, */
3489 if (! parse_shift (&p, operand, SHIFTED_REG_OFFSET))
3490 /* Use the diagnostics set in parse_shift, so not set new
3491 error message here. */
3492 return FALSE;
3493 }
3494 /* We only accept:
3495 [base,Xm{,LSL #imm}]
3496 [base,Xm,SXTX {#imm}]
3497 [base,Wm,(S|U)XTW {#imm}] */
3498 if (operand->shifter.kind == AARCH64_MOD_NONE
3499 || operand->shifter.kind == AARCH64_MOD_LSL
3500 || operand->shifter.kind == AARCH64_MOD_SXTX)
3501 {
4df068de 3502 if (*offset_qualifier == AARCH64_OPND_QLF_W)
a06ea964
NC
3503 {
3504 set_syntax_error (_("invalid use of 32-bit register offset"));
3505 return FALSE;
3506 }
4df068de
RS
3507 if (aarch64_get_qualifier_esize (*base_qualifier)
3508 != aarch64_get_qualifier_esize (*offset_qualifier))
3509 {
3510 set_syntax_error (_("offset has different size from base"));
3511 return FALSE;
3512 }
a06ea964 3513 }
4df068de 3514 else if (*offset_qualifier == AARCH64_OPND_QLF_X)
a06ea964
NC
3515 {
3516 set_syntax_error (_("invalid use of 64-bit register offset"));
3517 return FALSE;
3518 }
3519 }
3520 else
3521 {
3522 /* [Xn,#:<reloc_op>:<symbol> */
3523 skip_past_char (&p, '#');
73866052 3524 if (skip_past_char (&p, ':'))
a06ea964
NC
3525 {
3526 struct reloc_table_entry *entry;
3527
3528 /* Try to parse a relocation modifier. Anything else is
3529 an error. */
3530 if (!(entry = find_reloc_table_entry (&p)))
3531 {
3532 set_syntax_error (_("unknown relocation modifier"));
3533 return FALSE;
3534 }
3535
3536 if (entry->ldst_type == 0)
3537 {
3538 set_syntax_error
3539 (_("this relocation modifier is not allowed on this "
3540 "instruction"));
3541 return FALSE;
3542 }
3543
3544 /* [Xn,#:<reloc_op>: */
3545 /* We now have the group relocation table entry corresponding to
3546 the name in the assembler source. Next, we parse the
3547 expression. */
3548 if (! my_get_expression (exp, &p, GE_NO_PREFIX, 1))
3549 {
3550 set_syntax_error (_("invalid relocation expression"));
3551 return FALSE;
3552 }
3553
3554 /* [Xn,#:<reloc_op>:<expr> */
3555 /* Record the load/store relocation type. */
3556 inst.reloc.type = entry->ldst_type;
3557 inst.reloc.pc_rel = entry->pc_rel;
3558 }
98907a70 3559 else
a06ea964 3560 {
98907a70
RS
3561 if (! my_get_expression (exp, &p, GE_OPT_PREFIX, 1))
3562 {
3563 set_syntax_error (_("invalid expression in the address"));
3564 return FALSE;
3565 }
3566 /* [Xn,<expr> */
3567 if (imm_shift_mode != SHIFTED_NONE && skip_past_comma (&p))
3568 /* [Xn,<expr>,<shifter> */
3569 if (! parse_shift (&p, operand, imm_shift_mode))
3570 return FALSE;
a06ea964 3571 }
a06ea964
NC
3572 }
3573 }
3574
3575 if (! skip_past_char (&p, ']'))
3576 {
3577 set_syntax_error (_("']' expected"));
3578 return FALSE;
3579 }
3580
3581 if (skip_past_char (&p, '!'))
3582 {
3583 if (operand->addr.preind && operand->addr.offset.is_reg)
3584 {
3585 set_syntax_error (_("register offset not allowed in pre-indexed "
3586 "addressing mode"));
3587 return FALSE;
3588 }
3589 /* [Xn]! */
3590 operand->addr.writeback = 1;
3591 }
3592 else if (skip_past_comma (&p))
3593 {
3594 /* [Xn], */
3595 operand->addr.postind = 1;
3596 operand->addr.writeback = 1;
3597
3598 if (operand->addr.preind)
3599 {
3600 set_syntax_error (_("cannot combine pre- and post-indexing"));
3601 return FALSE;
3602 }
3603
4df068de 3604 reg = aarch64_reg_parse_32_64 (&p, offset_qualifier);
73866052 3605 if (reg)
a06ea964
NC
3606 {
3607 /* [Xn],Xm */
e1b988bb 3608 if (!aarch64_check_reg_type (reg, REG_TYPE_R_64))
a06ea964 3609 {
e1b988bb 3610 set_syntax_error (_(get_reg_expected_msg (REG_TYPE_R_64)));
a06ea964
NC
3611 return FALSE;
3612 }
e1b988bb
RS
3613
3614 operand->addr.offset.regno = reg->number;
a06ea964
NC
3615 operand->addr.offset.is_reg = 1;
3616 }
3617 else if (! my_get_expression (exp, &p, GE_OPT_PREFIX, 1))
3618 {
3619 /* [Xn],#expr */
3620 set_syntax_error (_("invalid expression in the address"));
3621 return FALSE;
3622 }
3623 }
3624
3625 /* If at this point neither .preind nor .postind is set, we have a
3626 bare [Rn]{!}; reject [Rn]! but accept [Rn] as a shorthand for [Rn,#0]. */
3627 if (operand->addr.preind == 0 && operand->addr.postind == 0)
3628 {
3629 if (operand->addr.writeback)
3630 {
3631 /* Reject [Rn]! */
3632 set_syntax_error (_("missing offset in the pre-indexed address"));
3633 return FALSE;
3634 }
3635 operand->addr.preind = 1;
3636 inst.reloc.exp.X_op = O_constant;
3637 inst.reloc.exp.X_add_number = 0;
3638 }
3639
3640 *str = p;
3641 return TRUE;
3642}
3643
73866052
RS
3644/* Parse a base AArch64 address (as opposed to an SVE one). Return TRUE
3645 on success. */
a06ea964 3646static bfd_boolean
73866052 3647parse_address (char **str, aarch64_opnd_info *operand)
a06ea964 3648{
4df068de
RS
3649 aarch64_opnd_qualifier_t base_qualifier, offset_qualifier;
3650 return parse_address_main (str, operand, &base_qualifier, &offset_qualifier,
98907a70 3651 REG_TYPE_R64_SP, REG_TYPE_R_Z, SHIFTED_NONE);
4df068de
RS
3652}
3653
98907a70 3654/* Parse an address in which SVE vector registers and MUL VL are allowed.
4df068de
RS
3655 The arguments have the same meaning as for parse_address_main.
3656 Return TRUE on success. */
3657static bfd_boolean
3658parse_sve_address (char **str, aarch64_opnd_info *operand,
3659 aarch64_opnd_qualifier_t *base_qualifier,
3660 aarch64_opnd_qualifier_t *offset_qualifier)
3661{
3662 return parse_address_main (str, operand, base_qualifier, offset_qualifier,
98907a70
RS
3663 REG_TYPE_SVE_BASE, REG_TYPE_SVE_OFFSET,
3664 SHIFTED_MUL_VL);
a06ea964
NC
3665}
3666
3667/* Parse an operand for a MOVZ, MOVN or MOVK instruction.
3668 Return TRUE on success; otherwise return FALSE. */
3669static bfd_boolean
3670parse_half (char **str, int *internal_fixup_p)
3671{
671eeb28 3672 char *p = *str;
a06ea964 3673
a06ea964
NC
3674 skip_past_char (&p, '#');
3675
3676 gas_assert (internal_fixup_p);
3677 *internal_fixup_p = 0;
3678
3679 if (*p == ':')
3680 {
3681 struct reloc_table_entry *entry;
3682
3683 /* Try to parse a relocation. Anything else is an error. */
3684 ++p;
3685 if (!(entry = find_reloc_table_entry (&p)))
3686 {
3687 set_syntax_error (_("unknown relocation modifier"));
3688 return FALSE;
3689 }
3690
3691 if (entry->movw_type == 0)
3692 {
3693 set_syntax_error
3694 (_("this relocation modifier is not allowed on this instruction"));
3695 return FALSE;
3696 }
3697
3698 inst.reloc.type = entry->movw_type;
3699 }
3700 else
3701 *internal_fixup_p = 1;
3702
a06ea964
NC
3703 if (! my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX, 1))
3704 return FALSE;
3705
3706 *str = p;
3707 return TRUE;
3708}
3709
3710/* Parse an operand for an ADRP instruction:
3711 ADRP <Xd>, <label>
3712 Return TRUE on success; otherwise return FALSE. */
3713
3714static bfd_boolean
3715parse_adrp (char **str)
3716{
3717 char *p;
3718
3719 p = *str;
3720 if (*p == ':')
3721 {
3722 struct reloc_table_entry *entry;
3723
3724 /* Try to parse a relocation. Anything else is an error. */
3725 ++p;
3726 if (!(entry = find_reloc_table_entry (&p)))
3727 {
3728 set_syntax_error (_("unknown relocation modifier"));
3729 return FALSE;
3730 }
3731
3732 if (entry->adrp_type == 0)
3733 {
3734 set_syntax_error
3735 (_("this relocation modifier is not allowed on this instruction"));
3736 return FALSE;
3737 }
3738
3739 inst.reloc.type = entry->adrp_type;
3740 }
3741 else
3742 inst.reloc.type = BFD_RELOC_AARCH64_ADR_HI21_PCREL;
3743
3744 inst.reloc.pc_rel = 1;
3745
3746 if (! my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX, 1))
3747 return FALSE;
3748
3749 *str = p;
3750 return TRUE;
3751}
3752
3753/* Miscellaneous. */
3754
245d2e3f
RS
3755/* Parse a symbolic operand such as "pow2" at *STR. ARRAY is an array
3756 of SIZE tokens in which index I gives the token for field value I,
3757 or is null if field value I is invalid. REG_TYPE says which register
3758 names should be treated as registers rather than as symbolic immediates.
3759
3760 Return true on success, moving *STR past the operand and storing the
3761 field value in *VAL. */
3762
3763static int
3764parse_enum_string (char **str, int64_t *val, const char *const *array,
3765 size_t size, aarch64_reg_type reg_type)
3766{
3767 expressionS exp;
3768 char *p, *q;
3769 size_t i;
3770
3771 /* Match C-like tokens. */
3772 p = q = *str;
3773 while (ISALNUM (*q))
3774 q++;
3775
3776 for (i = 0; i < size; ++i)
3777 if (array[i]
3778 && strncasecmp (array[i], p, q - p) == 0
3779 && array[i][q - p] == 0)
3780 {
3781 *val = i;
3782 *str = q;
3783 return TRUE;
3784 }
3785
3786 if (!parse_immediate_expression (&p, &exp, reg_type))
3787 return FALSE;
3788
3789 if (exp.X_op == O_constant
3790 && (uint64_t) exp.X_add_number < size)
3791 {
3792 *val = exp.X_add_number;
3793 *str = p;
3794 return TRUE;
3795 }
3796
3797 /* Use the default error for this operand. */
3798 return FALSE;
3799}
3800
a06ea964
NC
3801/* Parse an option for a preload instruction. Returns the encoding for the
3802 option, or PARSE_FAIL. */
3803
3804static int
3805parse_pldop (char **str)
3806{
3807 char *p, *q;
3808 const struct aarch64_name_value_pair *o;
3809
3810 p = q = *str;
3811 while (ISALNUM (*q))
3812 q++;
3813
3814 o = hash_find_n (aarch64_pldop_hsh, p, q - p);
3815 if (!o)
3816 return PARSE_FAIL;
3817
3818 *str = q;
3819 return o->value;
3820}
3821
3822/* Parse an option for a barrier instruction. Returns the encoding for the
3823 option, or PARSE_FAIL. */
3824
3825static int
3826parse_barrier (char **str)
3827{
3828 char *p, *q;
3829 const asm_barrier_opt *o;
3830
3831 p = q = *str;
3832 while (ISALPHA (*q))
3833 q++;
3834
3835 o = hash_find_n (aarch64_barrier_opt_hsh, p, q - p);
3836 if (!o)
3837 return PARSE_FAIL;
3838
3839 *str = q;
3840 return o->value;
3841}
3842
1e6f4800
MW
3843/* Parse an operand for a PSB barrier. Set *HINT_OPT to the hint-option record
3844 return 0 if successful. Otherwise return PARSE_FAIL. */
3845
3846static int
3847parse_barrier_psb (char **str,
3848 const struct aarch64_name_value_pair ** hint_opt)
3849{
3850 char *p, *q;
3851 const struct aarch64_name_value_pair *o;
3852
3853 p = q = *str;
3854 while (ISALPHA (*q))
3855 q++;
3856
3857 o = hash_find_n (aarch64_hint_opt_hsh, p, q - p);
3858 if (!o)
3859 {
3860 set_fatal_syntax_error
3861 ( _("unknown or missing option to PSB"));
3862 return PARSE_FAIL;
3863 }
3864
3865 if (o->value != 0x11)
3866 {
3867 /* PSB only accepts option name 'CSYNC'. */
3868 set_syntax_error
3869 (_("the specified option is not accepted for PSB"));
3870 return PARSE_FAIL;
3871 }
3872
3873 *str = q;
3874 *hint_opt = o;
3875 return 0;
3876}
3877
a06ea964 3878/* Parse a system register or a PSTATE field name for an MSR/MRS instruction.
a203d9b7 3879 Returns the encoding for the option, or PARSE_FAIL.
a06ea964
NC
3880
3881 If IMPLE_DEFINED_P is non-zero, the function will also try to parse the
72ca8fad
MW
3882 implementation defined system register name S<op0>_<op1>_<Cn>_<Cm>_<op2>.
3883
3884 If PSTATEFIELD_P is non-zero, the function will parse the name as a PSTATE
3885 field, otherwise as a system register.
3886*/
a06ea964
NC
3887
3888static int
72ca8fad
MW
3889parse_sys_reg (char **str, struct hash_control *sys_regs,
3890 int imple_defined_p, int pstatefield_p)
a06ea964
NC
3891{
3892 char *p, *q;
3893 char buf[32];
49eec193 3894 const aarch64_sys_reg *o;
a06ea964
NC
3895 int value;
3896
3897 p = buf;
3898 for (q = *str; ISALNUM (*q) || *q == '_'; q++)
3899 if (p < buf + 31)
3900 *p++ = TOLOWER (*q);
3901 *p = '\0';
3902 /* Assert that BUF be large enough. */
3903 gas_assert (p - buf == q - *str);
3904
3905 o = hash_find (sys_regs, buf);
3906 if (!o)
3907 {
3908 if (!imple_defined_p)
3909 return PARSE_FAIL;
3910 else
3911 {
df7b4545 3912 /* Parse S<op0>_<op1>_<Cn>_<Cm>_<op2>. */
a06ea964 3913 unsigned int op0, op1, cn, cm, op2;
df7b4545
JW
3914
3915 if (sscanf (buf, "s%u_%u_c%u_c%u_%u", &op0, &op1, &cn, &cm, &op2)
3916 != 5)
a06ea964 3917 return PARSE_FAIL;
df7b4545 3918 if (op0 > 3 || op1 > 7 || cn > 15 || cm > 15 || op2 > 7)
a06ea964
NC
3919 return PARSE_FAIL;
3920 value = (op0 << 14) | (op1 << 11) | (cn << 7) | (cm << 3) | op2;
3921 }
3922 }
3923 else
49eec193 3924 {
72ca8fad
MW
3925 if (pstatefield_p && !aarch64_pstatefield_supported_p (cpu_variant, o))
3926 as_bad (_("selected processor does not support PSTATE field "
3927 "name '%s'"), buf);
3928 if (!pstatefield_p && !aarch64_sys_reg_supported_p (cpu_variant, o))
3929 as_bad (_("selected processor does not support system register "
3930 "name '%s'"), buf);
9a73e520 3931 if (aarch64_sys_reg_deprecated_p (o))
49eec193 3932 as_warn (_("system register name '%s' is deprecated and may be "
72ca8fad 3933 "removed in a future release"), buf);
49eec193
YZ
3934 value = o->value;
3935 }
a06ea964
NC
3936
3937 *str = q;
3938 return value;
3939}
3940
3941/* Parse a system reg for ic/dc/at/tlbi instructions. Returns the table entry
3942 for the option, or NULL. */
3943
3944static const aarch64_sys_ins_reg *
3945parse_sys_ins_reg (char **str, struct hash_control *sys_ins_regs)
3946{
3947 char *p, *q;
3948 char buf[32];
3949 const aarch64_sys_ins_reg *o;
3950
3951 p = buf;
3952 for (q = *str; ISALNUM (*q) || *q == '_'; q++)
3953 if (p < buf + 31)
3954 *p++ = TOLOWER (*q);
3955 *p = '\0';
3956
3957 o = hash_find (sys_ins_regs, buf);
3958 if (!o)
3959 return NULL;
3960
d6bf7ce6
MW
3961 if (!aarch64_sys_ins_reg_supported_p (cpu_variant, o))
3962 as_bad (_("selected processor does not support system register "
3963 "name '%s'"), buf);
3964
a06ea964
NC
3965 *str = q;
3966 return o;
3967}
3968\f
3969#define po_char_or_fail(chr) do { \
3970 if (! skip_past_char (&str, chr)) \
3971 goto failure; \
3972} while (0)
3973
3974#define po_reg_or_fail(regtype) do { \
3975 val = aarch64_reg_parse (&str, regtype, &rtype, NULL); \
3976 if (val == PARSE_FAIL) \
3977 { \
3978 set_default_error (); \
3979 goto failure; \
3980 } \
3981 } while (0)
3982
e1b988bb
RS
3983#define po_int_reg_or_fail(reg_type) do { \
3984 reg = aarch64_reg_parse_32_64 (&str, &qualifier); \
3985 if (!reg || !aarch64_check_reg_type (reg, reg_type)) \
a06ea964
NC
3986 { \
3987 set_default_error (); \
3988 goto failure; \
3989 } \
e1b988bb
RS
3990 info->reg.regno = reg->number; \
3991 info->qualifier = qualifier; \
a06ea964
NC
3992 } while (0)
3993
3994#define po_imm_nc_or_fail() do { \
1799c0d0 3995 if (! parse_constant_immediate (&str, &val, imm_reg_type)) \
a06ea964
NC
3996 goto failure; \
3997 } while (0)
3998
3999#define po_imm_or_fail(min, max) do { \
1799c0d0 4000 if (! parse_constant_immediate (&str, &val, imm_reg_type)) \
a06ea964
NC
4001 goto failure; \
4002 if (val < min || val > max) \
4003 { \
4004 set_fatal_syntax_error (_("immediate value out of range "\
4005#min " to "#max)); \
4006 goto failure; \
4007 } \
4008 } while (0)
4009
245d2e3f
RS
4010#define po_enum_or_fail(array) do { \
4011 if (!parse_enum_string (&str, &val, array, \
4012 ARRAY_SIZE (array), imm_reg_type)) \
4013 goto failure; \
4014 } while (0)
4015
a06ea964
NC
4016#define po_misc_or_fail(expr) do { \
4017 if (!expr) \
4018 goto failure; \
4019 } while (0)
4020\f
4021/* encode the 12-bit imm field of Add/sub immediate */
4022static inline uint32_t
4023encode_addsub_imm (uint32_t imm)
4024{
4025 return imm << 10;
4026}
4027
4028/* encode the shift amount field of Add/sub immediate */
4029static inline uint32_t
4030encode_addsub_imm_shift_amount (uint32_t cnt)
4031{
4032 return cnt << 22;
4033}
4034
4035
4036/* encode the imm field of Adr instruction */
4037static inline uint32_t
4038encode_adr_imm (uint32_t imm)
4039{
4040 return (((imm & 0x3) << 29) /* [1:0] -> [30:29] */
4041 | ((imm & (0x7ffff << 2)) << 3)); /* [20:2] -> [23:5] */
4042}
4043
4044/* encode the immediate field of Move wide immediate */
4045static inline uint32_t
4046encode_movw_imm (uint32_t imm)
4047{
4048 return imm << 5;
4049}
4050
4051/* encode the 26-bit offset of unconditional branch */
4052static inline uint32_t
4053encode_branch_ofs_26 (uint32_t ofs)
4054{
4055 return ofs & ((1 << 26) - 1);
4056}
4057
4058/* encode the 19-bit offset of conditional branch and compare & branch */
4059static inline uint32_t
4060encode_cond_branch_ofs_19 (uint32_t ofs)
4061{
4062 return (ofs & ((1 << 19) - 1)) << 5;
4063}
4064
4065/* encode the 19-bit offset of ld literal */
4066static inline uint32_t
4067encode_ld_lit_ofs_19 (uint32_t ofs)
4068{
4069 return (ofs & ((1 << 19) - 1)) << 5;
4070}
4071
4072/* Encode the 14-bit offset of test & branch. */
4073static inline uint32_t
4074encode_tst_branch_ofs_14 (uint32_t ofs)
4075{
4076 return (ofs & ((1 << 14) - 1)) << 5;
4077}
4078
4079/* Encode the 16-bit imm field of svc/hvc/smc. */
4080static inline uint32_t
4081encode_svc_imm (uint32_t imm)
4082{
4083 return imm << 5;
4084}
4085
4086/* Reencode add(s) to sub(s), or sub(s) to add(s). */
4087static inline uint32_t
4088reencode_addsub_switch_add_sub (uint32_t opcode)
4089{
4090 return opcode ^ (1 << 30);
4091}
4092
4093static inline uint32_t
4094reencode_movzn_to_movz (uint32_t opcode)
4095{
4096 return opcode | (1 << 30);
4097}
4098
4099static inline uint32_t
4100reencode_movzn_to_movn (uint32_t opcode)
4101{
4102 return opcode & ~(1 << 30);
4103}
4104
4105/* Overall per-instruction processing. */
4106
4107/* We need to be able to fix up arbitrary expressions in some statements.
4108 This is so that we can handle symbols that are an arbitrary distance from
4109 the pc. The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
4110 which returns part of an address in a form which will be valid for
4111 a data instruction. We do this by pushing the expression into a symbol
4112 in the expr_section, and creating a fix for that. */
4113
4114static fixS *
4115fix_new_aarch64 (fragS * frag,
4116 int where,
4117 short int size, expressionS * exp, int pc_rel, int reloc)
4118{
4119 fixS *new_fix;
4120
4121 switch (exp->X_op)
4122 {
4123 case O_constant:
4124 case O_symbol:
4125 case O_add:
4126 case O_subtract:
4127 new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
4128 break;
4129
4130 default:
4131 new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
4132 pc_rel, reloc);
4133 break;
4134 }
4135 return new_fix;
4136}
4137\f
4138/* Diagnostics on operands errors. */
4139
a52e6fd3
YZ
4140/* By default, output verbose error message.
4141 Disable the verbose error message by -mno-verbose-error. */
4142static int verbose_error_p = 1;
a06ea964
NC
4143
4144#ifdef DEBUG_AARCH64
4145/* N.B. this is only for the purpose of debugging. */
4146const char* operand_mismatch_kind_names[] =
4147{
4148 "AARCH64_OPDE_NIL",
4149 "AARCH64_OPDE_RECOVERABLE",
4150 "AARCH64_OPDE_SYNTAX_ERROR",
4151 "AARCH64_OPDE_FATAL_SYNTAX_ERROR",
4152 "AARCH64_OPDE_INVALID_VARIANT",
4153 "AARCH64_OPDE_OUT_OF_RANGE",
4154 "AARCH64_OPDE_UNALIGNED",
4155 "AARCH64_OPDE_REG_LIST",
4156 "AARCH64_OPDE_OTHER_ERROR",
4157};
4158#endif /* DEBUG_AARCH64 */
4159
4160/* Return TRUE if LHS is of higher severity than RHS, otherwise return FALSE.
4161
4162 When multiple errors of different kinds are found in the same assembly
4163 line, only the error of the highest severity will be picked up for
4164 issuing the diagnostics. */
4165
4166static inline bfd_boolean
4167operand_error_higher_severity_p (enum aarch64_operand_error_kind lhs,
4168 enum aarch64_operand_error_kind rhs)
4169{
4170 gas_assert (AARCH64_OPDE_RECOVERABLE > AARCH64_OPDE_NIL);
4171 gas_assert (AARCH64_OPDE_SYNTAX_ERROR > AARCH64_OPDE_RECOVERABLE);
4172 gas_assert (AARCH64_OPDE_FATAL_SYNTAX_ERROR > AARCH64_OPDE_SYNTAX_ERROR);
4173 gas_assert (AARCH64_OPDE_INVALID_VARIANT > AARCH64_OPDE_FATAL_SYNTAX_ERROR);
4174 gas_assert (AARCH64_OPDE_OUT_OF_RANGE > AARCH64_OPDE_INVALID_VARIANT);
4175 gas_assert (AARCH64_OPDE_UNALIGNED > AARCH64_OPDE_OUT_OF_RANGE);
4176 gas_assert (AARCH64_OPDE_REG_LIST > AARCH64_OPDE_UNALIGNED);
4177 gas_assert (AARCH64_OPDE_OTHER_ERROR > AARCH64_OPDE_REG_LIST);
4178 return lhs > rhs;
4179}
4180
4181/* Helper routine to get the mnemonic name from the assembly instruction
4182 line; should only be called for the diagnosis purpose, as there is
4183 string copy operation involved, which may affect the runtime
4184 performance if used in elsewhere. */
4185
4186static const char*
4187get_mnemonic_name (const char *str)
4188{
4189 static char mnemonic[32];
4190 char *ptr;
4191
4192 /* Get the first 15 bytes and assume that the full name is included. */
4193 strncpy (mnemonic, str, 31);
4194 mnemonic[31] = '\0';
4195
4196 /* Scan up to the end of the mnemonic, which must end in white space,
4197 '.', or end of string. */
4198 for (ptr = mnemonic; is_part_of_name(*ptr); ++ptr)
4199 ;
4200
4201 *ptr = '\0';
4202
4203 /* Append '...' to the truncated long name. */
4204 if (ptr - mnemonic == 31)
4205 mnemonic[28] = mnemonic[29] = mnemonic[30] = '.';
4206
4207 return mnemonic;
4208}
4209
4210static void
4211reset_aarch64_instruction (aarch64_instruction *instruction)
4212{
4213 memset (instruction, '\0', sizeof (aarch64_instruction));
4214 instruction->reloc.type = BFD_RELOC_UNUSED;
4215}
4216
33eaf5de 4217/* Data structures storing one user error in the assembly code related to
a06ea964
NC
4218 operands. */
4219
4220struct operand_error_record
4221{
4222 const aarch64_opcode *opcode;
4223 aarch64_operand_error detail;
4224 struct operand_error_record *next;
4225};
4226
4227typedef struct operand_error_record operand_error_record;
4228
4229struct operand_errors
4230{
4231 operand_error_record *head;
4232 operand_error_record *tail;
4233};
4234
4235typedef struct operand_errors operand_errors;
4236
4237/* Top-level data structure reporting user errors for the current line of
4238 the assembly code.
4239 The way md_assemble works is that all opcodes sharing the same mnemonic
4240 name are iterated to find a match to the assembly line. In this data
4241 structure, each of the such opcodes will have one operand_error_record
4242 allocated and inserted. In other words, excessive errors related with
4243 a single opcode are disregarded. */
4244operand_errors operand_error_report;
4245
4246/* Free record nodes. */
4247static operand_error_record *free_opnd_error_record_nodes = NULL;
4248
4249/* Initialize the data structure that stores the operand mismatch
4250 information on assembling one line of the assembly code. */
4251static void
4252init_operand_error_report (void)
4253{
4254 if (operand_error_report.head != NULL)
4255 {
4256 gas_assert (operand_error_report.tail != NULL);
4257 operand_error_report.tail->next = free_opnd_error_record_nodes;
4258 free_opnd_error_record_nodes = operand_error_report.head;
4259 operand_error_report.head = NULL;
4260 operand_error_report.tail = NULL;
4261 return;
4262 }
4263 gas_assert (operand_error_report.tail == NULL);
4264}
4265
4266/* Return TRUE if some operand error has been recorded during the
4267 parsing of the current assembly line using the opcode *OPCODE;
4268 otherwise return FALSE. */
4269static inline bfd_boolean
4270opcode_has_operand_error_p (const aarch64_opcode *opcode)
4271{
4272 operand_error_record *record = operand_error_report.head;
4273 return record && record->opcode == opcode;
4274}
4275
4276/* Add the error record *NEW_RECORD to operand_error_report. The record's
4277 OPCODE field is initialized with OPCODE.
4278 N.B. only one record for each opcode, i.e. the maximum of one error is
4279 recorded for each instruction template. */
4280
4281static void
4282add_operand_error_record (const operand_error_record* new_record)
4283{
4284 const aarch64_opcode *opcode = new_record->opcode;
4285 operand_error_record* record = operand_error_report.head;
4286
4287 /* The record may have been created for this opcode. If not, we need
4288 to prepare one. */
4289 if (! opcode_has_operand_error_p (opcode))
4290 {
4291 /* Get one empty record. */
4292 if (free_opnd_error_record_nodes == NULL)
4293 {
325801bd 4294 record = XNEW (operand_error_record);
a06ea964
NC
4295 }
4296 else
4297 {
4298 record = free_opnd_error_record_nodes;
4299 free_opnd_error_record_nodes = record->next;
4300 }
4301 record->opcode = opcode;
4302 /* Insert at the head. */
4303 record->next = operand_error_report.head;
4304 operand_error_report.head = record;
4305 if (operand_error_report.tail == NULL)
4306 operand_error_report.tail = record;
4307 }
4308 else if (record->detail.kind != AARCH64_OPDE_NIL
4309 && record->detail.index <= new_record->detail.index
4310 && operand_error_higher_severity_p (record->detail.kind,
4311 new_record->detail.kind))
4312 {
4313 /* In the case of multiple errors found on operands related with a
4314 single opcode, only record the error of the leftmost operand and
4315 only if the error is of higher severity. */
4316 DEBUG_TRACE ("error %s on operand %d not added to the report due to"
4317 " the existing error %s on operand %d",
4318 operand_mismatch_kind_names[new_record->detail.kind],
4319 new_record->detail.index,
4320 operand_mismatch_kind_names[record->detail.kind],
4321 record->detail.index);
4322 return;
4323 }
4324
4325 record->detail = new_record->detail;
4326}
4327
4328static inline void
4329record_operand_error_info (const aarch64_opcode *opcode,
4330 aarch64_operand_error *error_info)
4331{
4332 operand_error_record record;
4333 record.opcode = opcode;
4334 record.detail = *error_info;
4335 add_operand_error_record (&record);
4336}
4337
4338/* Record an error of kind KIND and, if ERROR is not NULL, of the detailed
4339 error message *ERROR, for operand IDX (count from 0). */
4340
4341static void
4342record_operand_error (const aarch64_opcode *opcode, int idx,
4343 enum aarch64_operand_error_kind kind,
4344 const char* error)
4345{
4346 aarch64_operand_error info;
4347 memset(&info, 0, sizeof (info));
4348 info.index = idx;
4349 info.kind = kind;
4350 info.error = error;
4351 record_operand_error_info (opcode, &info);
4352}
4353
4354static void
4355record_operand_error_with_data (const aarch64_opcode *opcode, int idx,
4356 enum aarch64_operand_error_kind kind,
4357 const char* error, const int *extra_data)
4358{
4359 aarch64_operand_error info;
4360 info.index = idx;
4361 info.kind = kind;
4362 info.error = error;
4363 info.data[0] = extra_data[0];
4364 info.data[1] = extra_data[1];
4365 info.data[2] = extra_data[2];
4366 record_operand_error_info (opcode, &info);
4367}
4368
4369static void
4370record_operand_out_of_range_error (const aarch64_opcode *opcode, int idx,
4371 const char* error, int lower_bound,
4372 int upper_bound)
4373{
4374 int data[3] = {lower_bound, upper_bound, 0};
4375 record_operand_error_with_data (opcode, idx, AARCH64_OPDE_OUT_OF_RANGE,
4376 error, data);
4377}
4378
4379/* Remove the operand error record for *OPCODE. */
4380static void ATTRIBUTE_UNUSED
4381remove_operand_error_record (const aarch64_opcode *opcode)
4382{
4383 if (opcode_has_operand_error_p (opcode))
4384 {
4385 operand_error_record* record = operand_error_report.head;
4386 gas_assert (record != NULL && operand_error_report.tail != NULL);
4387 operand_error_report.head = record->next;
4388 record->next = free_opnd_error_record_nodes;
4389 free_opnd_error_record_nodes = record;
4390 if (operand_error_report.head == NULL)
4391 {
4392 gas_assert (operand_error_report.tail == record);
4393 operand_error_report.tail = NULL;
4394 }
4395 }
4396}
4397
4398/* Given the instruction in *INSTR, return the index of the best matched
4399 qualifier sequence in the list (an array) headed by QUALIFIERS_LIST.
4400
4401 Return -1 if there is no qualifier sequence; return the first match
4402 if there is multiple matches found. */
4403
4404static int
4405find_best_match (const aarch64_inst *instr,
4406 const aarch64_opnd_qualifier_seq_t *qualifiers_list)
4407{
4408 int i, num_opnds, max_num_matched, idx;
4409
4410 num_opnds = aarch64_num_of_operands (instr->opcode);
4411 if (num_opnds == 0)
4412 {
4413 DEBUG_TRACE ("no operand");
4414 return -1;
4415 }
4416
4417 max_num_matched = 0;
4989adac 4418 idx = 0;
a06ea964
NC
4419
4420 /* For each pattern. */
4421 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
4422 {
4423 int j, num_matched;
4424 const aarch64_opnd_qualifier_t *qualifiers = *qualifiers_list;
4425
4426 /* Most opcodes has much fewer patterns in the list. */
535b785f 4427 if (empty_qualifier_sequence_p (qualifiers))
a06ea964
NC
4428 {
4429 DEBUG_TRACE_IF (i == 0, "empty list of qualifier sequence");
a06ea964
NC
4430 break;
4431 }
4432
4433 for (j = 0, num_matched = 0; j < num_opnds; ++j, ++qualifiers)
4434 if (*qualifiers == instr->operands[j].qualifier)
4435 ++num_matched;
4436
4437 if (num_matched > max_num_matched)
4438 {
4439 max_num_matched = num_matched;
4440 idx = i;
4441 }
4442 }
4443
4444 DEBUG_TRACE ("return with %d", idx);
4445 return idx;
4446}
4447
33eaf5de 4448/* Assign qualifiers in the qualifier sequence (headed by QUALIFIERS) to the
a06ea964
NC
4449 corresponding operands in *INSTR. */
4450
4451static inline void
4452assign_qualifier_sequence (aarch64_inst *instr,
4453 const aarch64_opnd_qualifier_t *qualifiers)
4454{
4455 int i = 0;
4456 int num_opnds = aarch64_num_of_operands (instr->opcode);
4457 gas_assert (num_opnds);
4458 for (i = 0; i < num_opnds; ++i, ++qualifiers)
4459 instr->operands[i].qualifier = *qualifiers;
4460}
4461
4462/* Print operands for the diagnosis purpose. */
4463
4464static void
4465print_operands (char *buf, const aarch64_opcode *opcode,
4466 const aarch64_opnd_info *opnds)
4467{
4468 int i;
4469
4470 for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
4471 {
08d3b0cc 4472 char str[128];
a06ea964
NC
4473
4474 /* We regard the opcode operand info more, however we also look into
4475 the inst->operands to support the disassembling of the optional
4476 operand.
4477 The two operand code should be the same in all cases, apart from
4478 when the operand can be optional. */
4479 if (opcode->operands[i] == AARCH64_OPND_NIL
4480 || opnds[i].type == AARCH64_OPND_NIL)
4481 break;
4482
4483 /* Generate the operand string in STR. */
08d3b0cc 4484 aarch64_print_operand (str, sizeof (str), 0, opcode, opnds, i, NULL, NULL);
a06ea964
NC
4485
4486 /* Delimiter. */
4487 if (str[0] != '\0')
ad43e107 4488 strcat (buf, i == 0 ? " " : ", ");
a06ea964
NC
4489
4490 /* Append the operand string. */
4491 strcat (buf, str);
4492 }
4493}
4494
4495/* Send to stderr a string as information. */
4496
4497static void
4498output_info (const char *format, ...)
4499{
3b4dbbbf 4500 const char *file;
a06ea964
NC
4501 unsigned int line;
4502 va_list args;
4503
3b4dbbbf 4504 file = as_where (&line);
a06ea964
NC
4505 if (file)
4506 {
4507 if (line != 0)
4508 fprintf (stderr, "%s:%u: ", file, line);
4509 else
4510 fprintf (stderr, "%s: ", file);
4511 }
4512 fprintf (stderr, _("Info: "));
4513 va_start (args, format);
4514 vfprintf (stderr, format, args);
4515 va_end (args);
4516 (void) putc ('\n', stderr);
4517}
4518
4519/* Output one operand error record. */
4520
4521static void
4522output_operand_error_record (const operand_error_record *record, char *str)
4523{
28f013d5
JB
4524 const aarch64_operand_error *detail = &record->detail;
4525 int idx = detail->index;
a06ea964 4526 const aarch64_opcode *opcode = record->opcode;
28f013d5 4527 enum aarch64_opnd opd_code = (idx >= 0 ? opcode->operands[idx]
a06ea964 4528 : AARCH64_OPND_NIL);
a06ea964
NC
4529
4530 switch (detail->kind)
4531 {
4532 case AARCH64_OPDE_NIL:
4533 gas_assert (0);
4534 break;
4535
4536 case AARCH64_OPDE_SYNTAX_ERROR:
4537 case AARCH64_OPDE_RECOVERABLE:
4538 case AARCH64_OPDE_FATAL_SYNTAX_ERROR:
4539 case AARCH64_OPDE_OTHER_ERROR:
a06ea964
NC
4540 /* Use the prepared error message if there is, otherwise use the
4541 operand description string to describe the error. */
4542 if (detail->error != NULL)
4543 {
28f013d5 4544 if (idx < 0)
a06ea964
NC
4545 as_bad (_("%s -- `%s'"), detail->error, str);
4546 else
4547 as_bad (_("%s at operand %d -- `%s'"),
28f013d5 4548 detail->error, idx + 1, str);
a06ea964
NC
4549 }
4550 else
28f013d5
JB
4551 {
4552 gas_assert (idx >= 0);
ab3b8fcf 4553 as_bad (_("operand %d must be %s -- `%s'"), idx + 1,
a06ea964 4554 aarch64_get_operand_desc (opd_code), str);
28f013d5 4555 }
a06ea964
NC
4556 break;
4557
4558 case AARCH64_OPDE_INVALID_VARIANT:
4559 as_bad (_("operand mismatch -- `%s'"), str);
4560 if (verbose_error_p)
4561 {
4562 /* We will try to correct the erroneous instruction and also provide
4563 more information e.g. all other valid variants.
4564
4565 The string representation of the corrected instruction and other
4566 valid variants are generated by
4567
4568 1) obtaining the intermediate representation of the erroneous
4569 instruction;
4570 2) manipulating the IR, e.g. replacing the operand qualifier;
4571 3) printing out the instruction by calling the printer functions
4572 shared with the disassembler.
4573
4574 The limitation of this method is that the exact input assembly
4575 line cannot be accurately reproduced in some cases, for example an
4576 optional operand present in the actual assembly line will be
4577 omitted in the output; likewise for the optional syntax rules,
4578 e.g. the # before the immediate. Another limitation is that the
4579 assembly symbols and relocation operations in the assembly line
4580 currently cannot be printed out in the error report. Last but not
4581 least, when there is other error(s) co-exist with this error, the
4582 'corrected' instruction may be still incorrect, e.g. given
4583 'ldnp h0,h1,[x0,#6]!'
4584 this diagnosis will provide the version:
4585 'ldnp s0,s1,[x0,#6]!'
4586 which is still not right. */
4587 size_t len = strlen (get_mnemonic_name (str));
4588 int i, qlf_idx;
4589 bfd_boolean result;
08d3b0cc 4590 char buf[2048];
a06ea964
NC
4591 aarch64_inst *inst_base = &inst.base;
4592 const aarch64_opnd_qualifier_seq_t *qualifiers_list;
4593
4594 /* Init inst. */
4595 reset_aarch64_instruction (&inst);
4596 inst_base->opcode = opcode;
4597
4598 /* Reset the error report so that there is no side effect on the
4599 following operand parsing. */
4600 init_operand_error_report ();
4601
4602 /* Fill inst. */
4603 result = parse_operands (str + len, opcode)
4604 && programmer_friendly_fixup (&inst);
4605 gas_assert (result);
4606 result = aarch64_opcode_encode (opcode, inst_base, &inst_base->value,
4607 NULL, NULL);
4608 gas_assert (!result);
4609
4610 /* Find the most matched qualifier sequence. */
4611 qlf_idx = find_best_match (inst_base, opcode->qualifiers_list);
4612 gas_assert (qlf_idx > -1);
4613
4614 /* Assign the qualifiers. */
4615 assign_qualifier_sequence (inst_base,
4616 opcode->qualifiers_list[qlf_idx]);
4617
4618 /* Print the hint. */
4619 output_info (_(" did you mean this?"));
08d3b0cc 4620 snprintf (buf, sizeof (buf), "\t%s", get_mnemonic_name (str));
a06ea964
NC
4621 print_operands (buf, opcode, inst_base->operands);
4622 output_info (_(" %s"), buf);
4623
4624 /* Print out other variant(s) if there is any. */
4625 if (qlf_idx != 0 ||
4626 !empty_qualifier_sequence_p (opcode->qualifiers_list[1]))
4627 output_info (_(" other valid variant(s):"));
4628
4629 /* For each pattern. */
4630 qualifiers_list = opcode->qualifiers_list;
4631 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
4632 {
4633 /* Most opcodes has much fewer patterns in the list.
4634 First NIL qualifier indicates the end in the list. */
535b785f 4635 if (empty_qualifier_sequence_p (*qualifiers_list))
a06ea964
NC
4636 break;
4637
4638 if (i != qlf_idx)
4639 {
4640 /* Mnemonics name. */
08d3b0cc 4641 snprintf (buf, sizeof (buf), "\t%s", get_mnemonic_name (str));
a06ea964
NC
4642
4643 /* Assign the qualifiers. */
4644 assign_qualifier_sequence (inst_base, *qualifiers_list);
4645
4646 /* Print instruction. */
4647 print_operands (buf, opcode, inst_base->operands);
4648
4649 output_info (_(" %s"), buf);
4650 }
4651 }
4652 }
4653 break;
4654
0c608d6b
RS
4655 case AARCH64_OPDE_UNTIED_OPERAND:
4656 as_bad (_("operand %d must be the same register as operand 1 -- `%s'"),
4657 detail->index + 1, str);
4658 break;
4659
a06ea964 4660 case AARCH64_OPDE_OUT_OF_RANGE:
f5555712
YZ
4661 if (detail->data[0] != detail->data[1])
4662 as_bad (_("%s out of range %d to %d at operand %d -- `%s'"),
4663 detail->error ? detail->error : _("immediate value"),
28f013d5 4664 detail->data[0], detail->data[1], idx + 1, str);
f5555712 4665 else
ab3b8fcf 4666 as_bad (_("%s must be %d at operand %d -- `%s'"),
f5555712 4667 detail->error ? detail->error : _("immediate value"),
28f013d5 4668 detail->data[0], idx + 1, str);
a06ea964
NC
4669 break;
4670
4671 case AARCH64_OPDE_REG_LIST:
4672 if (detail->data[0] == 1)
4673 as_bad (_("invalid number of registers in the list; "
4674 "only 1 register is expected at operand %d -- `%s'"),
28f013d5 4675 idx + 1, str);
a06ea964
NC
4676 else
4677 as_bad (_("invalid number of registers in the list; "
4678 "%d registers are expected at operand %d -- `%s'"),
28f013d5 4679 detail->data[0], idx + 1, str);
a06ea964
NC
4680 break;
4681
4682 case AARCH64_OPDE_UNALIGNED:
ab3b8fcf 4683 as_bad (_("immediate value must be a multiple of "
a06ea964 4684 "%d at operand %d -- `%s'"),
28f013d5 4685 detail->data[0], idx + 1, str);
a06ea964
NC
4686 break;
4687
4688 default:
4689 gas_assert (0);
4690 break;
4691 }
4692}
4693
4694/* Process and output the error message about the operand mismatching.
4695
4696 When this function is called, the operand error information had
4697 been collected for an assembly line and there will be multiple
33eaf5de 4698 errors in the case of multiple instruction templates; output the
a06ea964
NC
4699 error message that most closely describes the problem. */
4700
4701static void
4702output_operand_error_report (char *str)
4703{
4704 int largest_error_pos;
4705 const char *msg = NULL;
4706 enum aarch64_operand_error_kind kind;
4707 operand_error_record *curr;
4708 operand_error_record *head = operand_error_report.head;
4709 operand_error_record *record = NULL;
4710
4711 /* No error to report. */
4712 if (head == NULL)
4713 return;
4714
4715 gas_assert (head != NULL && operand_error_report.tail != NULL);
4716
4717 /* Only one error. */
4718 if (head == operand_error_report.tail)
4719 {
4720 DEBUG_TRACE ("single opcode entry with error kind: %s",
4721 operand_mismatch_kind_names[head->detail.kind]);
4722 output_operand_error_record (head, str);
4723 return;
4724 }
4725
4726 /* Find the error kind of the highest severity. */
33eaf5de 4727 DEBUG_TRACE ("multiple opcode entries with error kind");
a06ea964
NC
4728 kind = AARCH64_OPDE_NIL;
4729 for (curr = head; curr != NULL; curr = curr->next)
4730 {
4731 gas_assert (curr->detail.kind != AARCH64_OPDE_NIL);
4732 DEBUG_TRACE ("\t%s", operand_mismatch_kind_names[curr->detail.kind]);
4733 if (operand_error_higher_severity_p (curr->detail.kind, kind))
4734 kind = curr->detail.kind;
4735 }
4736 gas_assert (kind != AARCH64_OPDE_NIL);
4737
4738 /* Pick up one of errors of KIND to report. */
4739 largest_error_pos = -2; /* Index can be -1 which means unknown index. */
4740 for (curr = head; curr != NULL; curr = curr->next)
4741 {
4742 if (curr->detail.kind != kind)
4743 continue;
4744 /* If there are multiple errors, pick up the one with the highest
4745 mismatching operand index. In the case of multiple errors with
4746 the equally highest operand index, pick up the first one or the
4747 first one with non-NULL error message. */
4748 if (curr->detail.index > largest_error_pos
4749 || (curr->detail.index == largest_error_pos && msg == NULL
4750 && curr->detail.error != NULL))
4751 {
4752 largest_error_pos = curr->detail.index;
4753 record = curr;
4754 msg = record->detail.error;
4755 }
4756 }
4757
4758 gas_assert (largest_error_pos != -2 && record != NULL);
4759 DEBUG_TRACE ("Pick up error kind %s to report",
4760 operand_mismatch_kind_names[record->detail.kind]);
4761
4762 /* Output. */
4763 output_operand_error_record (record, str);
4764}
4765\f
4766/* Write an AARCH64 instruction to buf - always little-endian. */
4767static void
4768put_aarch64_insn (char *buf, uint32_t insn)
4769{
4770 unsigned char *where = (unsigned char *) buf;
4771 where[0] = insn;
4772 where[1] = insn >> 8;
4773 where[2] = insn >> 16;
4774 where[3] = insn >> 24;
4775}
4776
4777static uint32_t
4778get_aarch64_insn (char *buf)
4779{
4780 unsigned char *where = (unsigned char *) buf;
4781 uint32_t result;
4782 result = (where[0] | (where[1] << 8) | (where[2] << 16) | (where[3] << 24));
4783 return result;
4784}
4785
4786static void
4787output_inst (struct aarch64_inst *new_inst)
4788{
4789 char *to = NULL;
4790
4791 to = frag_more (INSN_SIZE);
4792
4793 frag_now->tc_frag_data.recorded = 1;
4794
4795 put_aarch64_insn (to, inst.base.value);
4796
4797 if (inst.reloc.type != BFD_RELOC_UNUSED)
4798 {
4799 fixS *fixp = fix_new_aarch64 (frag_now, to - frag_now->fr_literal,
4800 INSN_SIZE, &inst.reloc.exp,
4801 inst.reloc.pc_rel,
4802 inst.reloc.type);
4803 DEBUG_TRACE ("Prepared relocation fix up");
4804 /* Don't check the addend value against the instruction size,
4805 that's the job of our code in md_apply_fix(). */
4806 fixp->fx_no_overflow = 1;
4807 if (new_inst != NULL)
4808 fixp->tc_fix_data.inst = new_inst;
4809 if (aarch64_gas_internal_fixup_p ())
4810 {
4811 gas_assert (inst.reloc.opnd != AARCH64_OPND_NIL);
4812 fixp->tc_fix_data.opnd = inst.reloc.opnd;
4813 fixp->fx_addnumber = inst.reloc.flags;
4814 }
4815 }
4816
4817 dwarf2_emit_insn (INSN_SIZE);
4818}
4819
4820/* Link together opcodes of the same name. */
4821
4822struct templates
4823{
4824 aarch64_opcode *opcode;
4825 struct templates *next;
4826};
4827
4828typedef struct templates templates;
4829
4830static templates *
4831lookup_mnemonic (const char *start, int len)
4832{
4833 templates *templ = NULL;
4834
4835 templ = hash_find_n (aarch64_ops_hsh, start, len);
4836 return templ;
4837}
4838
4839/* Subroutine of md_assemble, responsible for looking up the primary
4840 opcode from the mnemonic the user wrote. STR points to the
4841 beginning of the mnemonic. */
4842
4843static templates *
4844opcode_lookup (char **str)
4845{
bb7eff52 4846 char *end, *base, *dot;
a06ea964
NC
4847 const aarch64_cond *cond;
4848 char condname[16];
4849 int len;
4850
4851 /* Scan up to the end of the mnemonic, which must end in white space,
4852 '.', or end of string. */
bb7eff52 4853 dot = 0;
a06ea964 4854 for (base = end = *str; is_part_of_name(*end); end++)
bb7eff52
RS
4855 if (*end == '.' && !dot)
4856 dot = end;
a06ea964 4857
bb7eff52 4858 if (end == base || dot == base)
a06ea964
NC
4859 return 0;
4860
4861 inst.cond = COND_ALWAYS;
4862
4863 /* Handle a possible condition. */
bb7eff52 4864 if (dot)
a06ea964 4865 {
bb7eff52 4866 cond = hash_find_n (aarch64_cond_hsh, dot + 1, end - dot - 1);
a06ea964
NC
4867 if (cond)
4868 {
4869 inst.cond = cond->value;
bb7eff52 4870 *str = end;
a06ea964
NC
4871 }
4872 else
4873 {
bb7eff52 4874 *str = dot;
a06ea964
NC
4875 return 0;
4876 }
bb7eff52 4877 len = dot - base;
a06ea964
NC
4878 }
4879 else
bb7eff52
RS
4880 {
4881 *str = end;
4882 len = end - base;
4883 }
a06ea964
NC
4884
4885 if (inst.cond == COND_ALWAYS)
4886 {
4887 /* Look for unaffixed mnemonic. */
4888 return lookup_mnemonic (base, len);
4889 }
4890 else if (len <= 13)
4891 {
4892 /* append ".c" to mnemonic if conditional */
4893 memcpy (condname, base, len);
4894 memcpy (condname + len, ".c", 2);
4895 base = condname;
4896 len += 2;
4897 return lookup_mnemonic (base, len);
4898 }
4899
4900 return NULL;
4901}
4902
8f9a77af
RS
4903/* Internal helper routine converting a vector_type_el structure *VECTYPE
4904 to a corresponding operand qualifier. */
a06ea964
NC
4905
4906static inline aarch64_opnd_qualifier_t
8f9a77af 4907vectype_to_qualifier (const struct vector_type_el *vectype)
a06ea964 4908{
f06935a5 4909 /* Element size in bytes indexed by vector_el_type. */
a06ea964
NC
4910 const unsigned char ele_size[5]
4911 = {1, 2, 4, 8, 16};
65f2205d
MW
4912 const unsigned int ele_base [5] =
4913 {
a3b3345a 4914 AARCH64_OPND_QLF_V_4B,
3067d3b9 4915 AARCH64_OPND_QLF_V_2H,
65f2205d
MW
4916 AARCH64_OPND_QLF_V_2S,
4917 AARCH64_OPND_QLF_V_1D,
4918 AARCH64_OPND_QLF_V_1Q
4919 };
a06ea964
NC
4920
4921 if (!vectype->defined || vectype->type == NT_invtype)
4922 goto vectype_conversion_fail;
4923
d50c751e
RS
4924 if (vectype->type == NT_zero)
4925 return AARCH64_OPND_QLF_P_Z;
4926 if (vectype->type == NT_merge)
4927 return AARCH64_OPND_QLF_P_M;
4928
a06ea964
NC
4929 gas_assert (vectype->type >= NT_b && vectype->type <= NT_q);
4930
f11ad6bc 4931 if (vectype->defined & (NTA_HASINDEX | NTA_HASVARWIDTH))
a06ea964
NC
4932 /* Vector element register. */
4933 return AARCH64_OPND_QLF_S_B + vectype->type;
4934 else
4935 {
4936 /* Vector register. */
4937 int reg_size = ele_size[vectype->type] * vectype->width;
4938 unsigned offset;
65f2205d 4939 unsigned shift;
3067d3b9 4940 if (reg_size != 16 && reg_size != 8 && reg_size != 4)
a06ea964 4941 goto vectype_conversion_fail;
65f2205d
MW
4942
4943 /* The conversion is by calculating the offset from the base operand
4944 qualifier for the vector type. The operand qualifiers are regular
4945 enough that the offset can established by shifting the vector width by
4946 a vector-type dependent amount. */
4947 shift = 0;
4948 if (vectype->type == NT_b)
a3b3345a 4949 shift = 3;
3067d3b9 4950 else if (vectype->type == NT_h || vectype->type == NT_s)
65f2205d
MW
4951 shift = 2;
4952 else if (vectype->type >= NT_d)
4953 shift = 1;
4954 else
4955 gas_assert (0);
4956
4957 offset = ele_base [vectype->type] + (vectype->width >> shift);
a3b3345a 4958 gas_assert (AARCH64_OPND_QLF_V_4B <= offset
65f2205d
MW
4959 && offset <= AARCH64_OPND_QLF_V_1Q);
4960 return offset;
a06ea964
NC
4961 }
4962
4963vectype_conversion_fail:
4964 first_error (_("bad vector arrangement type"));
4965 return AARCH64_OPND_QLF_NIL;
4966}
4967
4968/* Process an optional operand that is found omitted from the assembly line.
4969 Fill *OPERAND for such an operand of type TYPE. OPCODE points to the
4970 instruction's opcode entry while IDX is the index of this omitted operand.
4971 */
4972
4973static void
4974process_omitted_operand (enum aarch64_opnd type, const aarch64_opcode *opcode,
4975 int idx, aarch64_opnd_info *operand)
4976{
4977 aarch64_insn default_value = get_optional_operand_default_value (opcode);
4978 gas_assert (optional_operand_p (opcode, idx));
4979 gas_assert (!operand->present);
4980
4981 switch (type)
4982 {
4983 case AARCH64_OPND_Rd:
4984 case AARCH64_OPND_Rn:
4985 case AARCH64_OPND_Rm:
4986 case AARCH64_OPND_Rt:
4987 case AARCH64_OPND_Rt2:
4988 case AARCH64_OPND_Rs:
4989 case AARCH64_OPND_Ra:
4990 case AARCH64_OPND_Rt_SYS:
4991 case AARCH64_OPND_Rd_SP:
4992 case AARCH64_OPND_Rn_SP:
c84364ec 4993 case AARCH64_OPND_Rm_SP:
a06ea964
NC
4994 case AARCH64_OPND_Fd:
4995 case AARCH64_OPND_Fn:
4996 case AARCH64_OPND_Fm:
4997 case AARCH64_OPND_Fa:
4998 case AARCH64_OPND_Ft:
4999 case AARCH64_OPND_Ft2:
5000 case AARCH64_OPND_Sd:
5001 case AARCH64_OPND_Sn:
5002 case AARCH64_OPND_Sm:
f42f1a1d 5003 case AARCH64_OPND_Va:
a06ea964
NC
5004 case AARCH64_OPND_Vd:
5005 case AARCH64_OPND_Vn:
5006 case AARCH64_OPND_Vm:
5007 case AARCH64_OPND_VdD1:
5008 case AARCH64_OPND_VnD1:
5009 operand->reg.regno = default_value;
5010 break;
5011
5012 case AARCH64_OPND_Ed:
5013 case AARCH64_OPND_En:
5014 case AARCH64_OPND_Em:
f42f1a1d 5015 case AARCH64_OPND_SM3_IMM2:
a06ea964
NC
5016 operand->reglane.regno = default_value;
5017 break;
5018
5019 case AARCH64_OPND_IDX:
5020 case AARCH64_OPND_BIT_NUM:
5021 case AARCH64_OPND_IMMR:
5022 case AARCH64_OPND_IMMS:
5023 case AARCH64_OPND_SHLL_IMM:
5024 case AARCH64_OPND_IMM_VLSL:
5025 case AARCH64_OPND_IMM_VLSR:
5026 case AARCH64_OPND_CCMP_IMM:
5027 case AARCH64_OPND_FBITS:
5028 case AARCH64_OPND_UIMM4:
5029 case AARCH64_OPND_UIMM3_OP1:
5030 case AARCH64_OPND_UIMM3_OP2:
5031 case AARCH64_OPND_IMM:
f42f1a1d 5032 case AARCH64_OPND_IMM_2:
a06ea964
NC
5033 case AARCH64_OPND_WIDTH:
5034 case AARCH64_OPND_UIMM7:
5035 case AARCH64_OPND_NZCV:
245d2e3f
RS
5036 case AARCH64_OPND_SVE_PATTERN:
5037 case AARCH64_OPND_SVE_PRFOP:
a06ea964
NC
5038 operand->imm.value = default_value;
5039 break;
5040
2442d846
RS
5041 case AARCH64_OPND_SVE_PATTERN_SCALED:
5042 operand->imm.value = default_value;
5043 operand->shifter.kind = AARCH64_MOD_MUL;
5044 operand->shifter.amount = 1;
5045 break;
5046
a06ea964
NC
5047 case AARCH64_OPND_EXCEPTION:
5048 inst.reloc.type = BFD_RELOC_UNUSED;
5049 break;
5050
5051 case AARCH64_OPND_BARRIER_ISB:
5052 operand->barrier = aarch64_barrier_options + default_value;
5053
5054 default:
5055 break;
5056 }
5057}
5058
5059/* Process the relocation type for move wide instructions.
5060 Return TRUE on success; otherwise return FALSE. */
5061
5062static bfd_boolean
5063process_movw_reloc_info (void)
5064{
5065 int is32;
5066 unsigned shift;
5067
5068 is32 = inst.base.operands[0].qualifier == AARCH64_OPND_QLF_W ? 1 : 0;
5069
5070 if (inst.base.opcode->op == OP_MOVK)
5071 switch (inst.reloc.type)
5072 {
5073 case BFD_RELOC_AARCH64_MOVW_G0_S:
5074 case BFD_RELOC_AARCH64_MOVW_G1_S:
5075 case BFD_RELOC_AARCH64_MOVW_G2_S:
1aa66fb1 5076 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 5077 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
a06ea964 5078 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
a06ea964
NC
5079 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
5080 set_syntax_error
5081 (_("the specified relocation type is not allowed for MOVK"));
5082 return FALSE;
5083 default:
5084 break;
5085 }
5086
5087 switch (inst.reloc.type)
5088 {
5089 case BFD_RELOC_AARCH64_MOVW_G0:
a06ea964 5090 case BFD_RELOC_AARCH64_MOVW_G0_NC:
f09c556a 5091 case BFD_RELOC_AARCH64_MOVW_G0_S:
ca632371 5092 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
43a357f9 5093 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
3e8286c0 5094 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
3b957e5b 5095 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
49df5539
JW
5096 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
5097 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
a06ea964
NC
5098 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
5099 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5100 shift = 0;
5101 break;
5102 case BFD_RELOC_AARCH64_MOVW_G1:
a06ea964 5103 case BFD_RELOC_AARCH64_MOVW_G1_NC:
f09c556a 5104 case BFD_RELOC_AARCH64_MOVW_G1_S:
654248e7 5105 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
43a357f9 5106 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
1aa66fb1 5107 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
3b957e5b 5108 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
49df5539
JW
5109 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
5110 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
a06ea964
NC
5111 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
5112 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5113 shift = 16;
5114 break;
5115 case BFD_RELOC_AARCH64_MOVW_G2:
a06ea964 5116 case BFD_RELOC_AARCH64_MOVW_G2_NC:
f09c556a 5117 case BFD_RELOC_AARCH64_MOVW_G2_S:
49df5539 5118 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964
NC
5119 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
5120 if (is32)
5121 {
5122 set_fatal_syntax_error
5123 (_("the specified relocation type is not allowed for 32-bit "
5124 "register"));
5125 return FALSE;
5126 }
5127 shift = 32;
5128 break;
5129 case BFD_RELOC_AARCH64_MOVW_G3:
5130 if (is32)
5131 {
5132 set_fatal_syntax_error
5133 (_("the specified relocation type is not allowed for 32-bit "
5134 "register"));
5135 return FALSE;
5136 }
5137 shift = 48;
5138 break;
5139 default:
5140 /* More cases should be added when more MOVW-related relocation types
5141 are supported in GAS. */
5142 gas_assert (aarch64_gas_internal_fixup_p ());
5143 /* The shift amount should have already been set by the parser. */
5144 return TRUE;
5145 }
5146 inst.base.operands[1].shifter.amount = shift;
5147 return TRUE;
5148}
5149
33eaf5de 5150/* A primitive log calculator. */
a06ea964
NC
5151
5152static inline unsigned int
5153get_logsz (unsigned int size)
5154{
5155 const unsigned char ls[16] =
5156 {0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
5157 if (size > 16)
5158 {
5159 gas_assert (0);
5160 return -1;
5161 }
5162 gas_assert (ls[size - 1] != (unsigned char)-1);
5163 return ls[size - 1];
5164}
5165
5166/* Determine and return the real reloc type code for an instruction
5167 with the pseudo reloc type code BFD_RELOC_AARCH64_LDST_LO12. */
5168
5169static inline bfd_reloc_code_real_type
5170ldst_lo12_determine_real_reloc_type (void)
5171{
4c562523 5172 unsigned logsz;
a06ea964
NC
5173 enum aarch64_opnd_qualifier opd0_qlf = inst.base.operands[0].qualifier;
5174 enum aarch64_opnd_qualifier opd1_qlf = inst.base.operands[1].qualifier;
5175
4c562523
JW
5176 const bfd_reloc_code_real_type reloc_ldst_lo12[3][5] = {
5177 {
5178 BFD_RELOC_AARCH64_LDST8_LO12,
5179 BFD_RELOC_AARCH64_LDST16_LO12,
5180 BFD_RELOC_AARCH64_LDST32_LO12,
5181 BFD_RELOC_AARCH64_LDST64_LO12,
a06ea964 5182 BFD_RELOC_AARCH64_LDST128_LO12
4c562523
JW
5183 },
5184 {
5185 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12,
5186 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12,
5187 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12,
5188 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12,
5189 BFD_RELOC_AARCH64_NONE
5190 },
5191 {
5192 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC,
5193 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC,
5194 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC,
5195 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC,
5196 BFD_RELOC_AARCH64_NONE
5197 }
a06ea964
NC
5198 };
5199
4c562523
JW
5200 gas_assert (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
5201 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
5202 || (inst.reloc.type
5203 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC));
a06ea964
NC
5204 gas_assert (inst.base.opcode->operands[1] == AARCH64_OPND_ADDR_UIMM12);
5205
5206 if (opd1_qlf == AARCH64_OPND_QLF_NIL)
5207 opd1_qlf =
5208 aarch64_get_expected_qualifier (inst.base.opcode->qualifiers_list,
5209 1, opd0_qlf, 0);
5210 gas_assert (opd1_qlf != AARCH64_OPND_QLF_NIL);
5211
5212 logsz = get_logsz (aarch64_get_qualifier_esize (opd1_qlf));
4c562523
JW
5213 if (inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
5214 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC)
5215 gas_assert (logsz <= 3);
5216 else
5217 gas_assert (logsz <= 4);
a06ea964 5218
4c562523 5219 /* In reloc.c, these pseudo relocation types should be defined in similar
33eaf5de 5220 order as above reloc_ldst_lo12 array. Because the array index calculation
4c562523
JW
5221 below relies on this. */
5222 return reloc_ldst_lo12[inst.reloc.type - BFD_RELOC_AARCH64_LDST_LO12][logsz];
a06ea964
NC
5223}
5224
5225/* Check whether a register list REGINFO is valid. The registers must be
5226 numbered in increasing order (modulo 32), in increments of one or two.
5227
5228 If ACCEPT_ALTERNATE is non-zero, the register numbers should be in
5229 increments of two.
5230
5231 Return FALSE if such a register list is invalid, otherwise return TRUE. */
5232
5233static bfd_boolean
5234reg_list_valid_p (uint32_t reginfo, int accept_alternate)
5235{
5236 uint32_t i, nb_regs, prev_regno, incr;
5237
5238 nb_regs = 1 + (reginfo & 0x3);
5239 reginfo >>= 2;
5240 prev_regno = reginfo & 0x1f;
5241 incr = accept_alternate ? 2 : 1;
5242
5243 for (i = 1; i < nb_regs; ++i)
5244 {
5245 uint32_t curr_regno;
5246 reginfo >>= 5;
5247 curr_regno = reginfo & 0x1f;
5248 if (curr_regno != ((prev_regno + incr) & 0x1f))
5249 return FALSE;
5250 prev_regno = curr_regno;
5251 }
5252
5253 return TRUE;
5254}
5255
5256/* Generic instruction operand parser. This does no encoding and no
5257 semantic validation; it merely squirrels values away in the inst
5258 structure. Returns TRUE or FALSE depending on whether the
5259 specified grammar matched. */
5260
5261static bfd_boolean
5262parse_operands (char *str, const aarch64_opcode *opcode)
5263{
5264 int i;
5265 char *backtrack_pos = 0;
5266 const enum aarch64_opnd *operands = opcode->operands;
1799c0d0 5267 aarch64_reg_type imm_reg_type;
a06ea964
NC
5268
5269 clear_error ();
5270 skip_whitespace (str);
5271
c0890d26 5272 if (AARCH64_CPU_HAS_FEATURE (AARCH64_FEATURE_SVE, *opcode->avariant))
5b2b928e 5273 imm_reg_type = REG_TYPE_R_Z_SP_BHSDQ_VZP;
c0890d26
RS
5274 else
5275 imm_reg_type = REG_TYPE_R_Z_BHSDQ_V;
1799c0d0 5276
a06ea964
NC
5277 for (i = 0; operands[i] != AARCH64_OPND_NIL; i++)
5278 {
5279 int64_t val;
e1b988bb 5280 const reg_entry *reg;
a06ea964
NC
5281 int comma_skipped_p = 0;
5282 aarch64_reg_type rtype;
8f9a77af 5283 struct vector_type_el vectype;
4df068de 5284 aarch64_opnd_qualifier_t qualifier, base_qualifier, offset_qualifier;
a06ea964 5285 aarch64_opnd_info *info = &inst.base.operands[i];
f11ad6bc 5286 aarch64_reg_type reg_type;
a06ea964
NC
5287
5288 DEBUG_TRACE ("parse operand %d", i);
5289
5290 /* Assign the operand code. */
5291 info->type = operands[i];
5292
5293 if (optional_operand_p (opcode, i))
5294 {
5295 /* Remember where we are in case we need to backtrack. */
5296 gas_assert (!backtrack_pos);
5297 backtrack_pos = str;
5298 }
5299
33eaf5de 5300 /* Expect comma between operands; the backtrack mechanism will take
a06ea964
NC
5301 care of cases of omitted optional operand. */
5302 if (i > 0 && ! skip_past_char (&str, ','))
5303 {
5304 set_syntax_error (_("comma expected between operands"));
5305 goto failure;
5306 }
5307 else
5308 comma_skipped_p = 1;
5309
5310 switch (operands[i])
5311 {
5312 case AARCH64_OPND_Rd:
5313 case AARCH64_OPND_Rn:
5314 case AARCH64_OPND_Rm:
5315 case AARCH64_OPND_Rt:
5316 case AARCH64_OPND_Rt2:
5317 case AARCH64_OPND_Rs:
5318 case AARCH64_OPND_Ra:
5319 case AARCH64_OPND_Rt_SYS:
ee804238 5320 case AARCH64_OPND_PAIRREG:
047cd301 5321 case AARCH64_OPND_SVE_Rm:
e1b988bb 5322 po_int_reg_or_fail (REG_TYPE_R_Z);
a06ea964
NC
5323 break;
5324
5325 case AARCH64_OPND_Rd_SP:
5326 case AARCH64_OPND_Rn_SP:
047cd301 5327 case AARCH64_OPND_SVE_Rn_SP:
c84364ec 5328 case AARCH64_OPND_Rm_SP:
e1b988bb 5329 po_int_reg_or_fail (REG_TYPE_R_SP);
a06ea964
NC
5330 break;
5331
5332 case AARCH64_OPND_Rm_EXT:
5333 case AARCH64_OPND_Rm_SFT:
5334 po_misc_or_fail (parse_shifter_operand
5335 (&str, info, (operands[i] == AARCH64_OPND_Rm_EXT
5336 ? SHIFTED_ARITH_IMM
5337 : SHIFTED_LOGIC_IMM)));
5338 if (!info->shifter.operator_present)
5339 {
5340 /* Default to LSL if not present. Libopcodes prefers shifter
5341 kind to be explicit. */
5342 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5343 info->shifter.kind = AARCH64_MOD_LSL;
5344 /* For Rm_EXT, libopcodes will carry out further check on whether
5345 or not stack pointer is used in the instruction (Recall that
5346 "the extend operator is not optional unless at least one of
5347 "Rd" or "Rn" is '11111' (i.e. WSP)"). */
5348 }
5349 break;
5350
5351 case AARCH64_OPND_Fd:
5352 case AARCH64_OPND_Fn:
5353 case AARCH64_OPND_Fm:
5354 case AARCH64_OPND_Fa:
5355 case AARCH64_OPND_Ft:
5356 case AARCH64_OPND_Ft2:
5357 case AARCH64_OPND_Sd:
5358 case AARCH64_OPND_Sn:
5359 case AARCH64_OPND_Sm:
047cd301
RS
5360 case AARCH64_OPND_SVE_VZn:
5361 case AARCH64_OPND_SVE_Vd:
5362 case AARCH64_OPND_SVE_Vm:
5363 case AARCH64_OPND_SVE_Vn:
a06ea964
NC
5364 val = aarch64_reg_parse (&str, REG_TYPE_BHSDQ, &rtype, NULL);
5365 if (val == PARSE_FAIL)
5366 {
5367 first_error (_(get_reg_expected_msg (REG_TYPE_BHSDQ)));
5368 goto failure;
5369 }
5370 gas_assert (rtype >= REG_TYPE_FP_B && rtype <= REG_TYPE_FP_Q);
5371
5372 info->reg.regno = val;
5373 info->qualifier = AARCH64_OPND_QLF_S_B + (rtype - REG_TYPE_FP_B);
5374 break;
5375
f11ad6bc
RS
5376 case AARCH64_OPND_SVE_Pd:
5377 case AARCH64_OPND_SVE_Pg3:
5378 case AARCH64_OPND_SVE_Pg4_5:
5379 case AARCH64_OPND_SVE_Pg4_10:
5380 case AARCH64_OPND_SVE_Pg4_16:
5381 case AARCH64_OPND_SVE_Pm:
5382 case AARCH64_OPND_SVE_Pn:
5383 case AARCH64_OPND_SVE_Pt:
5384 reg_type = REG_TYPE_PN;
5385 goto vector_reg;
5386
5387 case AARCH64_OPND_SVE_Za_5:
5388 case AARCH64_OPND_SVE_Za_16:
5389 case AARCH64_OPND_SVE_Zd:
5390 case AARCH64_OPND_SVE_Zm_5:
5391 case AARCH64_OPND_SVE_Zm_16:
5392 case AARCH64_OPND_SVE_Zn:
5393 case AARCH64_OPND_SVE_Zt:
5394 reg_type = REG_TYPE_ZN;
5395 goto vector_reg;
5396
f42f1a1d 5397 case AARCH64_OPND_Va:
a06ea964
NC
5398 case AARCH64_OPND_Vd:
5399 case AARCH64_OPND_Vn:
5400 case AARCH64_OPND_Vm:
f11ad6bc
RS
5401 reg_type = REG_TYPE_VN;
5402 vector_reg:
5403 val = aarch64_reg_parse (&str, reg_type, NULL, &vectype);
a06ea964
NC
5404 if (val == PARSE_FAIL)
5405 {
f11ad6bc 5406 first_error (_(get_reg_expected_msg (reg_type)));
a06ea964
NC
5407 goto failure;
5408 }
5409 if (vectype.defined & NTA_HASINDEX)
5410 goto failure;
5411
5412 info->reg.regno = val;
f11ad6bc
RS
5413 if ((reg_type == REG_TYPE_PN || reg_type == REG_TYPE_ZN)
5414 && vectype.type == NT_invtype)
5415 /* Unqualified Pn and Zn registers are allowed in certain
5416 contexts. Rely on F_STRICT qualifier checking to catch
5417 invalid uses. */
5418 info->qualifier = AARCH64_OPND_QLF_NIL;
5419 else
5420 {
5421 info->qualifier = vectype_to_qualifier (&vectype);
5422 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5423 goto failure;
5424 }
a06ea964
NC
5425 break;
5426
5427 case AARCH64_OPND_VdD1:
5428 case AARCH64_OPND_VnD1:
5429 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5430 if (val == PARSE_FAIL)
5431 {
5432 set_first_syntax_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5433 goto failure;
5434 }
5435 if (vectype.type != NT_d || vectype.index != 1)
5436 {
5437 set_fatal_syntax_error
5438 (_("the top half of a 128-bit FP/SIMD register is expected"));
5439 goto failure;
5440 }
5441 info->reg.regno = val;
5442 /* N.B: VdD1 and VnD1 are treated as an fp or advsimd scalar register
5443 here; it is correct for the purpose of encoding/decoding since
5444 only the register number is explicitly encoded in the related
5445 instructions, although this appears a bit hacky. */
5446 info->qualifier = AARCH64_OPND_QLF_S_D;
5447 break;
5448
582e12bf
RS
5449 case AARCH64_OPND_SVE_Zm3_INDEX:
5450 case AARCH64_OPND_SVE_Zm3_22_INDEX:
5451 case AARCH64_OPND_SVE_Zm4_INDEX:
f11ad6bc
RS
5452 case AARCH64_OPND_SVE_Zn_INDEX:
5453 reg_type = REG_TYPE_ZN;
5454 goto vector_reg_index;
5455
a06ea964
NC
5456 case AARCH64_OPND_Ed:
5457 case AARCH64_OPND_En:
5458 case AARCH64_OPND_Em:
f42f1a1d 5459 case AARCH64_OPND_SM3_IMM2:
f11ad6bc
RS
5460 reg_type = REG_TYPE_VN;
5461 vector_reg_index:
5462 val = aarch64_reg_parse (&str, reg_type, NULL, &vectype);
a06ea964
NC
5463 if (val == PARSE_FAIL)
5464 {
f11ad6bc 5465 first_error (_(get_reg_expected_msg (reg_type)));
a06ea964
NC
5466 goto failure;
5467 }
5468 if (vectype.type == NT_invtype || !(vectype.defined & NTA_HASINDEX))
5469 goto failure;
5470
5471 info->reglane.regno = val;
5472 info->reglane.index = vectype.index;
5473 info->qualifier = vectype_to_qualifier (&vectype);
5474 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5475 goto failure;
5476 break;
5477
f11ad6bc
RS
5478 case AARCH64_OPND_SVE_ZnxN:
5479 case AARCH64_OPND_SVE_ZtxN:
5480 reg_type = REG_TYPE_ZN;
5481 goto vector_reg_list;
5482
a06ea964
NC
5483 case AARCH64_OPND_LVn:
5484 case AARCH64_OPND_LVt:
5485 case AARCH64_OPND_LVt_AL:
5486 case AARCH64_OPND_LEt:
f11ad6bc
RS
5487 reg_type = REG_TYPE_VN;
5488 vector_reg_list:
5489 if (reg_type == REG_TYPE_ZN
5490 && get_opcode_dependent_value (opcode) == 1
5491 && *str != '{')
a06ea964 5492 {
f11ad6bc
RS
5493 val = aarch64_reg_parse (&str, reg_type, NULL, &vectype);
5494 if (val == PARSE_FAIL)
5495 {
5496 first_error (_(get_reg_expected_msg (reg_type)));
5497 goto failure;
5498 }
5499 info->reglist.first_regno = val;
5500 info->reglist.num_regs = 1;
5501 }
5502 else
5503 {
5504 val = parse_vector_reg_list (&str, reg_type, &vectype);
5505 if (val == PARSE_FAIL)
5506 goto failure;
5507 if (! reg_list_valid_p (val, /* accept_alternate */ 0))
5508 {
5509 set_fatal_syntax_error (_("invalid register list"));
5510 goto failure;
5511 }
5512 info->reglist.first_regno = (val >> 2) & 0x1f;
5513 info->reglist.num_regs = (val & 0x3) + 1;
a06ea964 5514 }
a06ea964
NC
5515 if (operands[i] == AARCH64_OPND_LEt)
5516 {
5517 if (!(vectype.defined & NTA_HASINDEX))
5518 goto failure;
5519 info->reglist.has_index = 1;
5520 info->reglist.index = vectype.index;
5521 }
f11ad6bc
RS
5522 else
5523 {
5524 if (vectype.defined & NTA_HASINDEX)
5525 goto failure;
5526 if (!(vectype.defined & NTA_HASTYPE))
5527 {
5528 if (reg_type == REG_TYPE_ZN)
5529 set_fatal_syntax_error (_("missing type suffix"));
5530 goto failure;
5531 }
5532 }
a06ea964
NC
5533 info->qualifier = vectype_to_qualifier (&vectype);
5534 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5535 goto failure;
5536 break;
5537
a6a51754
RL
5538 case AARCH64_OPND_CRn:
5539 case AARCH64_OPND_CRm:
a06ea964 5540 {
a6a51754
RL
5541 char prefix = *(str++);
5542 if (prefix != 'c' && prefix != 'C')
5543 goto failure;
5544
5545 po_imm_nc_or_fail ();
5546 if (val > 15)
5547 {
5548 set_fatal_syntax_error (_(N_ ("C0 - C15 expected")));
5549 goto failure;
5550 }
5551 info->qualifier = AARCH64_OPND_QLF_CR;
5552 info->imm.value = val;
5553 break;
a06ea964 5554 }
a06ea964
NC
5555
5556 case AARCH64_OPND_SHLL_IMM:
5557 case AARCH64_OPND_IMM_VLSR:
5558 po_imm_or_fail (1, 64);
5559 info->imm.value = val;
5560 break;
5561
5562 case AARCH64_OPND_CCMP_IMM:
e950b345 5563 case AARCH64_OPND_SIMM5:
a06ea964
NC
5564 case AARCH64_OPND_FBITS:
5565 case AARCH64_OPND_UIMM4:
5566 case AARCH64_OPND_UIMM3_OP1:
5567 case AARCH64_OPND_UIMM3_OP2:
5568 case AARCH64_OPND_IMM_VLSL:
5569 case AARCH64_OPND_IMM:
f42f1a1d 5570 case AARCH64_OPND_IMM_2:
a06ea964 5571 case AARCH64_OPND_WIDTH:
e950b345
RS
5572 case AARCH64_OPND_SVE_INV_LIMM:
5573 case AARCH64_OPND_SVE_LIMM:
5574 case AARCH64_OPND_SVE_LIMM_MOV:
5575 case AARCH64_OPND_SVE_SHLIMM_PRED:
5576 case AARCH64_OPND_SVE_SHLIMM_UNPRED:
5577 case AARCH64_OPND_SVE_SHRIMM_PRED:
5578 case AARCH64_OPND_SVE_SHRIMM_UNPRED:
5579 case AARCH64_OPND_SVE_SIMM5:
5580 case AARCH64_OPND_SVE_SIMM5B:
5581 case AARCH64_OPND_SVE_SIMM6:
5582 case AARCH64_OPND_SVE_SIMM8:
5583 case AARCH64_OPND_SVE_UIMM3:
5584 case AARCH64_OPND_SVE_UIMM7:
5585 case AARCH64_OPND_SVE_UIMM8:
5586 case AARCH64_OPND_SVE_UIMM8_53:
c2c4ff8d
SN
5587 case AARCH64_OPND_IMM_ROT1:
5588 case AARCH64_OPND_IMM_ROT2:
5589 case AARCH64_OPND_IMM_ROT3:
582e12bf
RS
5590 case AARCH64_OPND_SVE_IMM_ROT1:
5591 case AARCH64_OPND_SVE_IMM_ROT2:
a06ea964
NC
5592 po_imm_nc_or_fail ();
5593 info->imm.value = val;
5594 break;
5595
e950b345
RS
5596 case AARCH64_OPND_SVE_AIMM:
5597 case AARCH64_OPND_SVE_ASIMM:
5598 po_imm_nc_or_fail ();
5599 info->imm.value = val;
5600 skip_whitespace (str);
5601 if (skip_past_comma (&str))
5602 po_misc_or_fail (parse_shift (&str, info, SHIFTED_LSL));
5603 else
5604 inst.base.operands[i].shifter.kind = AARCH64_MOD_LSL;
5605 break;
5606
245d2e3f
RS
5607 case AARCH64_OPND_SVE_PATTERN:
5608 po_enum_or_fail (aarch64_sve_pattern_array);
5609 info->imm.value = val;
5610 break;
5611
2442d846
RS
5612 case AARCH64_OPND_SVE_PATTERN_SCALED:
5613 po_enum_or_fail (aarch64_sve_pattern_array);
5614 info->imm.value = val;
5615 if (skip_past_comma (&str)
5616 && !parse_shift (&str, info, SHIFTED_MUL))
5617 goto failure;
5618 if (!info->shifter.operator_present)
5619 {
5620 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5621 info->shifter.kind = AARCH64_MOD_MUL;
5622 info->shifter.amount = 1;
5623 }
5624 break;
5625
245d2e3f
RS
5626 case AARCH64_OPND_SVE_PRFOP:
5627 po_enum_or_fail (aarch64_sve_prfop_array);
5628 info->imm.value = val;
5629 break;
5630
a06ea964
NC
5631 case AARCH64_OPND_UIMM7:
5632 po_imm_or_fail (0, 127);
5633 info->imm.value = val;
5634 break;
5635
5636 case AARCH64_OPND_IDX:
f42f1a1d 5637 case AARCH64_OPND_MASK:
a06ea964
NC
5638 case AARCH64_OPND_BIT_NUM:
5639 case AARCH64_OPND_IMMR:
5640 case AARCH64_OPND_IMMS:
5641 po_imm_or_fail (0, 63);
5642 info->imm.value = val;
5643 break;
5644
5645 case AARCH64_OPND_IMM0:
5646 po_imm_nc_or_fail ();
5647 if (val != 0)
5648 {
5649 set_fatal_syntax_error (_("immediate zero expected"));
5650 goto failure;
5651 }
5652 info->imm.value = 0;
5653 break;
5654
5655 case AARCH64_OPND_FPIMM0:
5656 {
5657 int qfloat;
5658 bfd_boolean res1 = FALSE, res2 = FALSE;
5659 /* N.B. -0.0 will be rejected; although -0.0 shouldn't be rejected,
5660 it is probably not worth the effort to support it. */
1799c0d0
RS
5661 if (!(res1 = parse_aarch64_imm_float (&str, &qfloat, FALSE,
5662 imm_reg_type))
6a9deabe
RS
5663 && (error_p ()
5664 || !(res2 = parse_constant_immediate (&str, &val,
5665 imm_reg_type))))
a06ea964
NC
5666 goto failure;
5667 if ((res1 && qfloat == 0) || (res2 && val == 0))
5668 {
5669 info->imm.value = 0;
5670 info->imm.is_fp = 1;
5671 break;
5672 }
5673 set_fatal_syntax_error (_("immediate zero expected"));
5674 goto failure;
5675 }
5676
5677 case AARCH64_OPND_IMM_MOV:
5678 {
5679 char *saved = str;
8db49cc2
WN
5680 if (reg_name_p (str, REG_TYPE_R_Z_SP) ||
5681 reg_name_p (str, REG_TYPE_VN))
a06ea964
NC
5682 goto failure;
5683 str = saved;
5684 po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
5685 GE_OPT_PREFIX, 1));
5686 /* The MOV immediate alias will be fixed up by fix_mov_imm_insn
5687 later. fix_mov_imm_insn will try to determine a machine
5688 instruction (MOVZ, MOVN or ORR) for it and will issue an error
5689 message if the immediate cannot be moved by a single
5690 instruction. */
5691 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
5692 inst.base.operands[i].skip = 1;
5693 }
5694 break;
5695
5696 case AARCH64_OPND_SIMD_IMM:
5697 case AARCH64_OPND_SIMD_IMM_SFT:
1799c0d0 5698 if (! parse_big_immediate (&str, &val, imm_reg_type))
a06ea964
NC
5699 goto failure;
5700 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5701 /* addr_off_p */ 0,
5702 /* need_libopcodes_p */ 1,
5703 /* skip_p */ 1);
5704 /* Parse shift.
5705 N.B. although AARCH64_OPND_SIMD_IMM doesn't permit any
5706 shift, we don't check it here; we leave the checking to
5707 the libopcodes (operand_general_constraint_met_p). By
5708 doing this, we achieve better diagnostics. */
5709 if (skip_past_comma (&str)
5710 && ! parse_shift (&str, info, SHIFTED_LSL_MSL))
5711 goto failure;
5712 if (!info->shifter.operator_present
5713 && info->type == AARCH64_OPND_SIMD_IMM_SFT)
5714 {
5715 /* Default to LSL if not present. Libopcodes prefers shifter
5716 kind to be explicit. */
5717 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5718 info->shifter.kind = AARCH64_MOD_LSL;
5719 }
5720 break;
5721
5722 case AARCH64_OPND_FPIMM:
5723 case AARCH64_OPND_SIMD_FPIMM:
165d4950 5724 case AARCH64_OPND_SVE_FPIMM8:
a06ea964
NC
5725 {
5726 int qfloat;
165d4950
RS
5727 bfd_boolean dp_p;
5728
5729 dp_p = double_precision_operand_p (&inst.base.operands[0]);
6a9deabe 5730 if (!parse_aarch64_imm_float (&str, &qfloat, dp_p, imm_reg_type)
874d7e6e 5731 || !aarch64_imm_float_p (qfloat))
a06ea964 5732 {
6a9deabe
RS
5733 if (!error_p ())
5734 set_fatal_syntax_error (_("invalid floating-point"
5735 " constant"));
a06ea964
NC
5736 goto failure;
5737 }
5738 inst.base.operands[i].imm.value = encode_imm_float_bits (qfloat);
5739 inst.base.operands[i].imm.is_fp = 1;
5740 }
5741 break;
5742
165d4950
RS
5743 case AARCH64_OPND_SVE_I1_HALF_ONE:
5744 case AARCH64_OPND_SVE_I1_HALF_TWO:
5745 case AARCH64_OPND_SVE_I1_ZERO_ONE:
5746 {
5747 int qfloat;
5748 bfd_boolean dp_p;
5749
5750 dp_p = double_precision_operand_p (&inst.base.operands[0]);
5751 if (!parse_aarch64_imm_float (&str, &qfloat, dp_p, imm_reg_type))
5752 {
5753 if (!error_p ())
5754 set_fatal_syntax_error (_("invalid floating-point"
5755 " constant"));
5756 goto failure;
5757 }
5758 inst.base.operands[i].imm.value = qfloat;
5759 inst.base.operands[i].imm.is_fp = 1;
5760 }
5761 break;
5762
a06ea964
NC
5763 case AARCH64_OPND_LIMM:
5764 po_misc_or_fail (parse_shifter_operand (&str, info,
5765 SHIFTED_LOGIC_IMM));
5766 if (info->shifter.operator_present)
5767 {
5768 set_fatal_syntax_error
5769 (_("shift not allowed for bitmask immediate"));
5770 goto failure;
5771 }
5772 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5773 /* addr_off_p */ 0,
5774 /* need_libopcodes_p */ 1,
5775 /* skip_p */ 1);
5776 break;
5777
5778 case AARCH64_OPND_AIMM:
5779 if (opcode->op == OP_ADD)
5780 /* ADD may have relocation types. */
5781 po_misc_or_fail (parse_shifter_operand_reloc (&str, info,
5782 SHIFTED_ARITH_IMM));
5783 else
5784 po_misc_or_fail (parse_shifter_operand (&str, info,
5785 SHIFTED_ARITH_IMM));
5786 switch (inst.reloc.type)
5787 {
5788 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5789 info->shifter.amount = 12;
5790 break;
5791 case BFD_RELOC_UNUSED:
5792 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
5793 if (info->shifter.kind != AARCH64_MOD_NONE)
5794 inst.reloc.flags = FIXUP_F_HAS_EXPLICIT_SHIFT;
5795 inst.reloc.pc_rel = 0;
5796 break;
5797 default:
5798 break;
5799 }
5800 info->imm.value = 0;
5801 if (!info->shifter.operator_present)
5802 {
5803 /* Default to LSL if not present. Libopcodes prefers shifter
5804 kind to be explicit. */
5805 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5806 info->shifter.kind = AARCH64_MOD_LSL;
5807 }
5808 break;
5809
5810 case AARCH64_OPND_HALF:
5811 {
5812 /* #<imm16> or relocation. */
5813 int internal_fixup_p;
5814 po_misc_or_fail (parse_half (&str, &internal_fixup_p));
5815 if (internal_fixup_p)
5816 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
5817 skip_whitespace (str);
5818 if (skip_past_comma (&str))
5819 {
5820 /* {, LSL #<shift>} */
5821 if (! aarch64_gas_internal_fixup_p ())
5822 {
5823 set_fatal_syntax_error (_("can't mix relocation modifier "
5824 "with explicit shift"));
5825 goto failure;
5826 }
5827 po_misc_or_fail (parse_shift (&str, info, SHIFTED_LSL));
5828 }
5829 else
5830 inst.base.operands[i].shifter.amount = 0;
5831 inst.base.operands[i].shifter.kind = AARCH64_MOD_LSL;
5832 inst.base.operands[i].imm.value = 0;
5833 if (! process_movw_reloc_info ())
5834 goto failure;
5835 }
5836 break;
5837
5838 case AARCH64_OPND_EXCEPTION:
1799c0d0
RS
5839 po_misc_or_fail (parse_immediate_expression (&str, &inst.reloc.exp,
5840 imm_reg_type));
a06ea964
NC
5841 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5842 /* addr_off_p */ 0,
5843 /* need_libopcodes_p */ 0,
5844 /* skip_p */ 1);
5845 break;
5846
5847 case AARCH64_OPND_NZCV:
5848 {
5849 const asm_nzcv *nzcv = hash_find_n (aarch64_nzcv_hsh, str, 4);
5850 if (nzcv != NULL)
5851 {
5852 str += 4;
5853 info->imm.value = nzcv->value;
5854 break;
5855 }
5856 po_imm_or_fail (0, 15);
5857 info->imm.value = val;
5858 }
5859 break;
5860
5861 case AARCH64_OPND_COND:
68a64283 5862 case AARCH64_OPND_COND1:
bb7eff52
RS
5863 {
5864 char *start = str;
5865 do
5866 str++;
5867 while (ISALPHA (*str));
5868 info->cond = hash_find_n (aarch64_cond_hsh, start, str - start);
5869 if (info->cond == NULL)
5870 {
5871 set_syntax_error (_("invalid condition"));
5872 goto failure;
5873 }
5874 else if (operands[i] == AARCH64_OPND_COND1
5875 && (info->cond->value & 0xe) == 0xe)
5876 {
5877 /* Do not allow AL or NV. */
5878 set_default_error ();
5879 goto failure;
5880 }
5881 }
a06ea964
NC
5882 break;
5883
5884 case AARCH64_OPND_ADDR_ADRP:
5885 po_misc_or_fail (parse_adrp (&str));
5886 /* Clear the value as operand needs to be relocated. */
5887 info->imm.value = 0;
5888 break;
5889
5890 case AARCH64_OPND_ADDR_PCREL14:
5891 case AARCH64_OPND_ADDR_PCREL19:
5892 case AARCH64_OPND_ADDR_PCREL21:
5893 case AARCH64_OPND_ADDR_PCREL26:
73866052 5894 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5895 if (!info->addr.pcrel)
5896 {
5897 set_syntax_error (_("invalid pc-relative address"));
5898 goto failure;
5899 }
5900 if (inst.gen_lit_pool
5901 && (opcode->iclass != loadlit || opcode->op == OP_PRFM_LIT))
5902 {
5903 /* Only permit "=value" in the literal load instructions.
5904 The literal will be generated by programmer_friendly_fixup. */
5905 set_syntax_error (_("invalid use of \"=immediate\""));
5906 goto failure;
5907 }
5908 if (inst.reloc.exp.X_op == O_symbol && find_reloc_table_entry (&str))
5909 {
5910 set_syntax_error (_("unrecognized relocation suffix"));
5911 goto failure;
5912 }
5913 if (inst.reloc.exp.X_op == O_constant && !inst.gen_lit_pool)
5914 {
5915 info->imm.value = inst.reloc.exp.X_add_number;
5916 inst.reloc.type = BFD_RELOC_UNUSED;
5917 }
5918 else
5919 {
5920 info->imm.value = 0;
f41aef5f
RE
5921 if (inst.reloc.type == BFD_RELOC_UNUSED)
5922 switch (opcode->iclass)
5923 {
5924 case compbranch:
5925 case condbranch:
5926 /* e.g. CBZ or B.COND */
5927 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
5928 inst.reloc.type = BFD_RELOC_AARCH64_BRANCH19;
5929 break;
5930 case testbranch:
5931 /* e.g. TBZ */
5932 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL14);
5933 inst.reloc.type = BFD_RELOC_AARCH64_TSTBR14;
5934 break;
5935 case branch_imm:
5936 /* e.g. B or BL */
5937 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL26);
5938 inst.reloc.type =
5939 (opcode->op == OP_BL) ? BFD_RELOC_AARCH64_CALL26
5940 : BFD_RELOC_AARCH64_JUMP26;
5941 break;
5942 case loadlit:
5943 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
5944 inst.reloc.type = BFD_RELOC_AARCH64_LD_LO19_PCREL;
5945 break;
5946 case pcreladdr:
5947 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL21);
5948 inst.reloc.type = BFD_RELOC_AARCH64_ADR_LO21_PCREL;
5949 break;
5950 default:
5951 gas_assert (0);
5952 abort ();
5953 }
a06ea964
NC
5954 inst.reloc.pc_rel = 1;
5955 }
5956 break;
5957
5958 case AARCH64_OPND_ADDR_SIMPLE:
5959 case AARCH64_OPND_SIMD_ADDR_SIMPLE:
e1b988bb
RS
5960 {
5961 /* [<Xn|SP>{, #<simm>}] */
5962 char *start = str;
5963 /* First use the normal address-parsing routines, to get
5964 the usual syntax errors. */
73866052 5965 po_misc_or_fail (parse_address (&str, info));
e1b988bb
RS
5966 if (info->addr.pcrel || info->addr.offset.is_reg
5967 || !info->addr.preind || info->addr.postind
5968 || info->addr.writeback)
5969 {
5970 set_syntax_error (_("invalid addressing mode"));
5971 goto failure;
5972 }
5973
5974 /* Then retry, matching the specific syntax of these addresses. */
5975 str = start;
5976 po_char_or_fail ('[');
5977 po_reg_or_fail (REG_TYPE_R64_SP);
5978 /* Accept optional ", #0". */
5979 if (operands[i] == AARCH64_OPND_ADDR_SIMPLE
5980 && skip_past_char (&str, ','))
5981 {
5982 skip_past_char (&str, '#');
5983 if (! skip_past_char (&str, '0'))
5984 {
5985 set_fatal_syntax_error
5986 (_("the optional immediate offset can only be 0"));
5987 goto failure;
5988 }
5989 }
5990 po_char_or_fail (']');
5991 break;
5992 }
a06ea964
NC
5993
5994 case AARCH64_OPND_ADDR_REGOFF:
5995 /* [<Xn|SP>, <R><m>{, <extend> {<amount>}}] */
73866052 5996 po_misc_or_fail (parse_address (&str, info));
4df068de 5997 regoff_addr:
a06ea964
NC
5998 if (info->addr.pcrel || !info->addr.offset.is_reg
5999 || !info->addr.preind || info->addr.postind
6000 || info->addr.writeback)
6001 {
6002 set_syntax_error (_("invalid addressing mode"));
6003 goto failure;
6004 }
6005 if (!info->shifter.operator_present)
6006 {
6007 /* Default to LSL if not present. Libopcodes prefers shifter
6008 kind to be explicit. */
6009 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
6010 info->shifter.kind = AARCH64_MOD_LSL;
6011 }
6012 /* Qualifier to be deduced by libopcodes. */
6013 break;
6014
6015 case AARCH64_OPND_ADDR_SIMM7:
73866052 6016 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
6017 if (info->addr.pcrel || info->addr.offset.is_reg
6018 || (!info->addr.preind && !info->addr.postind))
6019 {
6020 set_syntax_error (_("invalid addressing mode"));
6021 goto failure;
6022 }
73866052
RS
6023 if (inst.reloc.type != BFD_RELOC_UNUSED)
6024 {
6025 set_syntax_error (_("relocation not allowed"));
6026 goto failure;
6027 }
a06ea964
NC
6028 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
6029 /* addr_off_p */ 1,
6030 /* need_libopcodes_p */ 1,
6031 /* skip_p */ 0);
6032 break;
6033
6034 case AARCH64_OPND_ADDR_SIMM9:
6035 case AARCH64_OPND_ADDR_SIMM9_2:
73866052 6036 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
6037 if (info->addr.pcrel || info->addr.offset.is_reg
6038 || (!info->addr.preind && !info->addr.postind)
6039 || (operands[i] == AARCH64_OPND_ADDR_SIMM9_2
6040 && info->addr.writeback))
6041 {
6042 set_syntax_error (_("invalid addressing mode"));
6043 goto failure;
6044 }
6045 if (inst.reloc.type != BFD_RELOC_UNUSED)
6046 {
6047 set_syntax_error (_("relocation not allowed"));
6048 goto failure;
6049 }
6050 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
6051 /* addr_off_p */ 1,
6052 /* need_libopcodes_p */ 1,
6053 /* skip_p */ 0);
6054 break;
6055
3f06e550 6056 case AARCH64_OPND_ADDR_SIMM10:
f42f1a1d 6057 case AARCH64_OPND_ADDR_OFFSET:
3f06e550
SN
6058 po_misc_or_fail (parse_address (&str, info));
6059 if (info->addr.pcrel || info->addr.offset.is_reg
6060 || !info->addr.preind || info->addr.postind)
6061 {
6062 set_syntax_error (_("invalid addressing mode"));
6063 goto failure;
6064 }
6065 if (inst.reloc.type != BFD_RELOC_UNUSED)
6066 {
6067 set_syntax_error (_("relocation not allowed"));
6068 goto failure;
6069 }
6070 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
6071 /* addr_off_p */ 1,
6072 /* need_libopcodes_p */ 1,
6073 /* skip_p */ 0);
6074 break;
6075
a06ea964 6076 case AARCH64_OPND_ADDR_UIMM12:
73866052 6077 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
6078 if (info->addr.pcrel || info->addr.offset.is_reg
6079 || !info->addr.preind || info->addr.writeback)
6080 {
6081 set_syntax_error (_("invalid addressing mode"));
6082 goto failure;
6083 }
6084 if (inst.reloc.type == BFD_RELOC_UNUSED)
6085 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
4c562523
JW
6086 else if (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
6087 || (inst.reloc.type
6088 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12)
6089 || (inst.reloc.type
6090 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC))
a06ea964
NC
6091 inst.reloc.type = ldst_lo12_determine_real_reloc_type ();
6092 /* Leave qualifier to be determined by libopcodes. */
6093 break;
6094
6095 case AARCH64_OPND_SIMD_ADDR_POST:
6096 /* [<Xn|SP>], <Xm|#<amount>> */
73866052 6097 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
6098 if (!info->addr.postind || !info->addr.writeback)
6099 {
6100 set_syntax_error (_("invalid addressing mode"));
6101 goto failure;
6102 }
6103 if (!info->addr.offset.is_reg)
6104 {
6105 if (inst.reloc.exp.X_op == O_constant)
6106 info->addr.offset.imm = inst.reloc.exp.X_add_number;
6107 else
6108 {
6109 set_fatal_syntax_error
ab3b8fcf 6110 (_("writeback value must be an immediate constant"));
a06ea964
NC
6111 goto failure;
6112 }
6113 }
6114 /* No qualifier. */
6115 break;
6116
582e12bf 6117 case AARCH64_OPND_SVE_ADDR_RI_S4x16:
98907a70
RS
6118 case AARCH64_OPND_SVE_ADDR_RI_S4xVL:
6119 case AARCH64_OPND_SVE_ADDR_RI_S4x2xVL:
6120 case AARCH64_OPND_SVE_ADDR_RI_S4x3xVL:
6121 case AARCH64_OPND_SVE_ADDR_RI_S4x4xVL:
6122 case AARCH64_OPND_SVE_ADDR_RI_S6xVL:
6123 case AARCH64_OPND_SVE_ADDR_RI_S9xVL:
4df068de
RS
6124 case AARCH64_OPND_SVE_ADDR_RI_U6:
6125 case AARCH64_OPND_SVE_ADDR_RI_U6x2:
6126 case AARCH64_OPND_SVE_ADDR_RI_U6x4:
6127 case AARCH64_OPND_SVE_ADDR_RI_U6x8:
98907a70
RS
6128 /* [X<n>{, #imm, MUL VL}]
6129 [X<n>{, #imm}]
4df068de
RS
6130 but recognizing SVE registers. */
6131 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
6132 &offset_qualifier));
6133 if (base_qualifier != AARCH64_OPND_QLF_X)
6134 {
6135 set_syntax_error (_("invalid addressing mode"));
6136 goto failure;
6137 }
6138 sve_regimm:
6139 if (info->addr.pcrel || info->addr.offset.is_reg
6140 || !info->addr.preind || info->addr.writeback)
6141 {
6142 set_syntax_error (_("invalid addressing mode"));
6143 goto failure;
6144 }
6145 if (inst.reloc.type != BFD_RELOC_UNUSED
6146 || inst.reloc.exp.X_op != O_constant)
6147 {
6148 /* Make sure this has priority over
6149 "invalid addressing mode". */
6150 set_fatal_syntax_error (_("constant offset required"));
6151 goto failure;
6152 }
6153 info->addr.offset.imm = inst.reloc.exp.X_add_number;
6154 break;
6155
6156 case AARCH64_OPND_SVE_ADDR_RR:
6157 case AARCH64_OPND_SVE_ADDR_RR_LSL1:
6158 case AARCH64_OPND_SVE_ADDR_RR_LSL2:
6159 case AARCH64_OPND_SVE_ADDR_RR_LSL3:
6160 case AARCH64_OPND_SVE_ADDR_RX:
6161 case AARCH64_OPND_SVE_ADDR_RX_LSL1:
6162 case AARCH64_OPND_SVE_ADDR_RX_LSL2:
6163 case AARCH64_OPND_SVE_ADDR_RX_LSL3:
6164 /* [<Xn|SP>, <R><m>{, lsl #<amount>}]
6165 but recognizing SVE registers. */
6166 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
6167 &offset_qualifier));
6168 if (base_qualifier != AARCH64_OPND_QLF_X
6169 || offset_qualifier != AARCH64_OPND_QLF_X)
6170 {
6171 set_syntax_error (_("invalid addressing mode"));
6172 goto failure;
6173 }
6174 goto regoff_addr;
6175
6176 case AARCH64_OPND_SVE_ADDR_RZ:
6177 case AARCH64_OPND_SVE_ADDR_RZ_LSL1:
6178 case AARCH64_OPND_SVE_ADDR_RZ_LSL2:
6179 case AARCH64_OPND_SVE_ADDR_RZ_LSL3:
6180 case AARCH64_OPND_SVE_ADDR_RZ_XTW_14:
6181 case AARCH64_OPND_SVE_ADDR_RZ_XTW_22:
6182 case AARCH64_OPND_SVE_ADDR_RZ_XTW1_14:
6183 case AARCH64_OPND_SVE_ADDR_RZ_XTW1_22:
6184 case AARCH64_OPND_SVE_ADDR_RZ_XTW2_14:
6185 case AARCH64_OPND_SVE_ADDR_RZ_XTW2_22:
6186 case AARCH64_OPND_SVE_ADDR_RZ_XTW3_14:
6187 case AARCH64_OPND_SVE_ADDR_RZ_XTW3_22:
6188 /* [<Xn|SP>, Z<m>.D{, LSL #<amount>}]
6189 [<Xn|SP>, Z<m>.<T>, <extend> {#<amount>}] */
6190 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
6191 &offset_qualifier));
6192 if (base_qualifier != AARCH64_OPND_QLF_X
6193 || (offset_qualifier != AARCH64_OPND_QLF_S_S
6194 && offset_qualifier != AARCH64_OPND_QLF_S_D))
6195 {
6196 set_syntax_error (_("invalid addressing mode"));
6197 goto failure;
6198 }
6199 info->qualifier = offset_qualifier;
6200 goto regoff_addr;
6201
6202 case AARCH64_OPND_SVE_ADDR_ZI_U5:
6203 case AARCH64_OPND_SVE_ADDR_ZI_U5x2:
6204 case AARCH64_OPND_SVE_ADDR_ZI_U5x4:
6205 case AARCH64_OPND_SVE_ADDR_ZI_U5x8:
6206 /* [Z<n>.<T>{, #imm}] */
6207 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
6208 &offset_qualifier));
6209 if (base_qualifier != AARCH64_OPND_QLF_S_S
6210 && base_qualifier != AARCH64_OPND_QLF_S_D)
6211 {
6212 set_syntax_error (_("invalid addressing mode"));
6213 goto failure;
6214 }
6215 info->qualifier = base_qualifier;
6216 goto sve_regimm;
6217
6218 case AARCH64_OPND_SVE_ADDR_ZZ_LSL:
6219 case AARCH64_OPND_SVE_ADDR_ZZ_SXTW:
6220 case AARCH64_OPND_SVE_ADDR_ZZ_UXTW:
6221 /* [Z<n>.<T>, Z<m>.<T>{, LSL #<amount>}]
6222 [Z<n>.D, Z<m>.D, <extend> {#<amount>}]
6223
6224 We don't reject:
6225
6226 [Z<n>.S, Z<m>.S, <extend> {#<amount>}]
6227
6228 here since we get better error messages by leaving it to
6229 the qualifier checking routines. */
6230 po_misc_or_fail (parse_sve_address (&str, info, &base_qualifier,
6231 &offset_qualifier));
6232 if ((base_qualifier != AARCH64_OPND_QLF_S_S
6233 && base_qualifier != AARCH64_OPND_QLF_S_D)
6234 || offset_qualifier != base_qualifier)
6235 {
6236 set_syntax_error (_("invalid addressing mode"));
6237 goto failure;
6238 }
6239 info->qualifier = base_qualifier;
6240 goto regoff_addr;
6241
a06ea964 6242 case AARCH64_OPND_SYSREG:
72ca8fad 6243 if ((val = parse_sys_reg (&str, aarch64_sys_regs_hsh, 1, 0))
a203d9b7 6244 == PARSE_FAIL)
a06ea964 6245 {
a203d9b7
YZ
6246 set_syntax_error (_("unknown or missing system register name"));
6247 goto failure;
a06ea964 6248 }
a203d9b7 6249 inst.base.operands[i].sysreg = val;
a06ea964
NC
6250 break;
6251
6252 case AARCH64_OPND_PSTATEFIELD:
72ca8fad 6253 if ((val = parse_sys_reg (&str, aarch64_pstatefield_hsh, 0, 1))
a3251895 6254 == PARSE_FAIL)
a06ea964
NC
6255 {
6256 set_syntax_error (_("unknown or missing PSTATE field name"));
6257 goto failure;
6258 }
6259 inst.base.operands[i].pstatefield = val;
6260 break;
6261
6262 case AARCH64_OPND_SYSREG_IC:
6263 inst.base.operands[i].sysins_op =
6264 parse_sys_ins_reg (&str, aarch64_sys_regs_ic_hsh);
6265 goto sys_reg_ins;
6266 case AARCH64_OPND_SYSREG_DC:
6267 inst.base.operands[i].sysins_op =
6268 parse_sys_ins_reg (&str, aarch64_sys_regs_dc_hsh);
6269 goto sys_reg_ins;
6270 case AARCH64_OPND_SYSREG_AT:
6271 inst.base.operands[i].sysins_op =
6272 parse_sys_ins_reg (&str, aarch64_sys_regs_at_hsh);
6273 goto sys_reg_ins;
6274 case AARCH64_OPND_SYSREG_TLBI:
6275 inst.base.operands[i].sysins_op =
6276 parse_sys_ins_reg (&str, aarch64_sys_regs_tlbi_hsh);
6277sys_reg_ins:
6278 if (inst.base.operands[i].sysins_op == NULL)
6279 {
6280 set_fatal_syntax_error ( _("unknown or missing operation name"));
6281 goto failure;
6282 }
6283 break;
6284
6285 case AARCH64_OPND_BARRIER:
6286 case AARCH64_OPND_BARRIER_ISB:
6287 val = parse_barrier (&str);
6288 if (val != PARSE_FAIL
6289 && operands[i] == AARCH64_OPND_BARRIER_ISB && val != 0xf)
6290 {
6291 /* ISB only accepts options name 'sy'. */
6292 set_syntax_error
6293 (_("the specified option is not accepted in ISB"));
6294 /* Turn off backtrack as this optional operand is present. */
6295 backtrack_pos = 0;
6296 goto failure;
6297 }
6298 /* This is an extension to accept a 0..15 immediate. */
6299 if (val == PARSE_FAIL)
6300 po_imm_or_fail (0, 15);
6301 info->barrier = aarch64_barrier_options + val;
6302 break;
6303
6304 case AARCH64_OPND_PRFOP:
6305 val = parse_pldop (&str);
6306 /* This is an extension to accept a 0..31 immediate. */
6307 if (val == PARSE_FAIL)
6308 po_imm_or_fail (0, 31);
6309 inst.base.operands[i].prfop = aarch64_prfops + val;
6310 break;
6311
1e6f4800
MW
6312 case AARCH64_OPND_BARRIER_PSB:
6313 val = parse_barrier_psb (&str, &(info->hint_option));
6314 if (val == PARSE_FAIL)
6315 goto failure;
6316 break;
6317
a06ea964
NC
6318 default:
6319 as_fatal (_("unhandled operand code %d"), operands[i]);
6320 }
6321
6322 /* If we get here, this operand was successfully parsed. */
6323 inst.base.operands[i].present = 1;
6324 continue;
6325
6326failure:
6327 /* The parse routine should already have set the error, but in case
6328 not, set a default one here. */
6329 if (! error_p ())
6330 set_default_error ();
6331
6332 if (! backtrack_pos)
6333 goto parse_operands_return;
6334
f4c51f60
JW
6335 {
6336 /* We reach here because this operand is marked as optional, and
6337 either no operand was supplied or the operand was supplied but it
6338 was syntactically incorrect. In the latter case we report an
6339 error. In the former case we perform a few more checks before
6340 dropping through to the code to insert the default operand. */
6341
6342 char *tmp = backtrack_pos;
6343 char endchar = END_OF_INSN;
6344
6345 if (i != (aarch64_num_of_operands (opcode) - 1))
6346 endchar = ',';
6347 skip_past_char (&tmp, ',');
6348
6349 if (*tmp != endchar)
6350 /* The user has supplied an operand in the wrong format. */
6351 goto parse_operands_return;
6352
6353 /* Make sure there is not a comma before the optional operand.
6354 For example the fifth operand of 'sys' is optional:
6355
6356 sys #0,c0,c0,#0, <--- wrong
6357 sys #0,c0,c0,#0 <--- correct. */
6358 if (comma_skipped_p && i && endchar == END_OF_INSN)
6359 {
6360 set_fatal_syntax_error
6361 (_("unexpected comma before the omitted optional operand"));
6362 goto parse_operands_return;
6363 }
6364 }
6365
a06ea964
NC
6366 /* Reaching here means we are dealing with an optional operand that is
6367 omitted from the assembly line. */
6368 gas_assert (optional_operand_p (opcode, i));
6369 info->present = 0;
6370 process_omitted_operand (operands[i], opcode, i, info);
6371
6372 /* Try again, skipping the optional operand at backtrack_pos. */
6373 str = backtrack_pos;
6374 backtrack_pos = 0;
6375
a06ea964
NC
6376 /* Clear any error record after the omitted optional operand has been
6377 successfully handled. */
6378 clear_error ();
6379 }
6380
6381 /* Check if we have parsed all the operands. */
6382 if (*str != '\0' && ! error_p ())
6383 {
6384 /* Set I to the index of the last present operand; this is
6385 for the purpose of diagnostics. */
6386 for (i -= 1; i >= 0 && !inst.base.operands[i].present; --i)
6387 ;
6388 set_fatal_syntax_error
6389 (_("unexpected characters following instruction"));
6390 }
6391
6392parse_operands_return:
6393
6394 if (error_p ())
6395 {
6396 DEBUG_TRACE ("parsing FAIL: %s - %s",
6397 operand_mismatch_kind_names[get_error_kind ()],
6398 get_error_message ());
6399 /* Record the operand error properly; this is useful when there
6400 are multiple instruction templates for a mnemonic name, so that
6401 later on, we can select the error that most closely describes
6402 the problem. */
6403 record_operand_error (opcode, i, get_error_kind (),
6404 get_error_message ());
6405 return FALSE;
6406 }
6407 else
6408 {
6409 DEBUG_TRACE ("parsing SUCCESS");
6410 return TRUE;
6411 }
6412}
6413
6414/* It does some fix-up to provide some programmer friendly feature while
6415 keeping the libopcodes happy, i.e. libopcodes only accepts
6416 the preferred architectural syntax.
6417 Return FALSE if there is any failure; otherwise return TRUE. */
6418
6419static bfd_boolean
6420programmer_friendly_fixup (aarch64_instruction *instr)
6421{
6422 aarch64_inst *base = &instr->base;
6423 const aarch64_opcode *opcode = base->opcode;
6424 enum aarch64_op op = opcode->op;
6425 aarch64_opnd_info *operands = base->operands;
6426
6427 DEBUG_TRACE ("enter");
6428
6429 switch (opcode->iclass)
6430 {
6431 case testbranch:
6432 /* TBNZ Xn|Wn, #uimm6, label
6433 Test and Branch Not Zero: conditionally jumps to label if bit number
6434 uimm6 in register Xn is not zero. The bit number implies the width of
6435 the register, which may be written and should be disassembled as Wn if
6436 uimm is less than 32. */
6437 if (operands[0].qualifier == AARCH64_OPND_QLF_W)
6438 {
6439 if (operands[1].imm.value >= 32)
6440 {
6441 record_operand_out_of_range_error (opcode, 1, _("immediate value"),
6442 0, 31);
6443 return FALSE;
6444 }
6445 operands[0].qualifier = AARCH64_OPND_QLF_X;
6446 }
6447 break;
6448 case loadlit:
6449 /* LDR Wt, label | =value
6450 As a convenience assemblers will typically permit the notation
6451 "=value" in conjunction with the pc-relative literal load instructions
6452 to automatically place an immediate value or symbolic address in a
6453 nearby literal pool and generate a hidden label which references it.
6454 ISREG has been set to 0 in the case of =value. */
6455 if (instr->gen_lit_pool
6456 && (op == OP_LDR_LIT || op == OP_LDRV_LIT || op == OP_LDRSW_LIT))
6457 {
6458 int size = aarch64_get_qualifier_esize (operands[0].qualifier);
6459 if (op == OP_LDRSW_LIT)
6460 size = 4;
6461 if (instr->reloc.exp.X_op != O_constant
67a32447 6462 && instr->reloc.exp.X_op != O_big
a06ea964
NC
6463 && instr->reloc.exp.X_op != O_symbol)
6464 {
6465 record_operand_error (opcode, 1,
6466 AARCH64_OPDE_FATAL_SYNTAX_ERROR,
6467 _("constant expression expected"));
6468 return FALSE;
6469 }
6470 if (! add_to_lit_pool (&instr->reloc.exp, size))
6471 {
6472 record_operand_error (opcode, 1,
6473 AARCH64_OPDE_OTHER_ERROR,
6474 _("literal pool insertion failed"));
6475 return FALSE;
6476 }
6477 }
6478 break;
a06ea964
NC
6479 case log_shift:
6480 case bitfield:
6481 /* UXT[BHW] Wd, Wn
6482 Unsigned Extend Byte|Halfword|Word: UXT[BH] is architectural alias
6483 for UBFM Wd,Wn,#0,#7|15, while UXTW is pseudo instruction which is
6484 encoded using ORR Wd, WZR, Wn (MOV Wd,Wn).
6485 A programmer-friendly assembler should accept a destination Xd in
6486 place of Wd, however that is not the preferred form for disassembly.
6487 */
6488 if ((op == OP_UXTB || op == OP_UXTH || op == OP_UXTW)
6489 && operands[1].qualifier == AARCH64_OPND_QLF_W
6490 && operands[0].qualifier == AARCH64_OPND_QLF_X)
6491 operands[0].qualifier = AARCH64_OPND_QLF_W;
6492 break;
6493
6494 case addsub_ext:
6495 {
6496 /* In the 64-bit form, the final register operand is written as Wm
6497 for all but the (possibly omitted) UXTX/LSL and SXTX
6498 operators.
6499 As a programmer-friendly assembler, we accept e.g.
6500 ADDS <Xd>, <Xn|SP>, <Xm>{, UXTB {#<amount>}} and change it to
6501 ADDS <Xd>, <Xn|SP>, <Wm>{, UXTB {#<amount>}}. */
6502 int idx = aarch64_operand_index (opcode->operands,
6503 AARCH64_OPND_Rm_EXT);
6504 gas_assert (idx == 1 || idx == 2);
6505 if (operands[0].qualifier == AARCH64_OPND_QLF_X
6506 && operands[idx].qualifier == AARCH64_OPND_QLF_X
6507 && operands[idx].shifter.kind != AARCH64_MOD_LSL
6508 && operands[idx].shifter.kind != AARCH64_MOD_UXTX
6509 && operands[idx].shifter.kind != AARCH64_MOD_SXTX)
6510 operands[idx].qualifier = AARCH64_OPND_QLF_W;
6511 }
6512 break;
6513
6514 default:
6515 break;
6516 }
6517
6518 DEBUG_TRACE ("exit with SUCCESS");
6519 return TRUE;
6520}
6521
5c47e525 6522/* Check for loads and stores that will cause unpredictable behavior. */
54a28c4c
JW
6523
6524static void
6525warn_unpredictable_ldst (aarch64_instruction *instr, char *str)
6526{
6527 aarch64_inst *base = &instr->base;
6528 const aarch64_opcode *opcode = base->opcode;
6529 const aarch64_opnd_info *opnds = base->operands;
6530 switch (opcode->iclass)
6531 {
6532 case ldst_pos:
6533 case ldst_imm9:
3f06e550 6534 case ldst_imm10:
54a28c4c
JW
6535 case ldst_unscaled:
6536 case ldst_unpriv:
5c47e525
RE
6537 /* Loading/storing the base register is unpredictable if writeback. */
6538 if ((aarch64_get_operand_class (opnds[0].type)
6539 == AARCH64_OPND_CLASS_INT_REG)
6540 && opnds[0].reg.regno == opnds[1].addr.base_regno
4bf8c6e8 6541 && opnds[1].addr.base_regno != REG_SP
54a28c4c 6542 && opnds[1].addr.writeback)
5c47e525 6543 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
54a28c4c
JW
6544 break;
6545 case ldstpair_off:
6546 case ldstnapair_offs:
6547 case ldstpair_indexed:
5c47e525
RE
6548 /* Loading/storing the base register is unpredictable if writeback. */
6549 if ((aarch64_get_operand_class (opnds[0].type)
6550 == AARCH64_OPND_CLASS_INT_REG)
6551 && (opnds[0].reg.regno == opnds[2].addr.base_regno
6552 || opnds[1].reg.regno == opnds[2].addr.base_regno)
4bf8c6e8 6553 && opnds[2].addr.base_regno != REG_SP
54a28c4c 6554 && opnds[2].addr.writeback)
5c47e525
RE
6555 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
6556 /* Load operations must load different registers. */
54a28c4c
JW
6557 if ((opcode->opcode & (1 << 22))
6558 && opnds[0].reg.regno == opnds[1].reg.regno)
6559 as_warn (_("unpredictable load of register pair -- `%s'"), str);
6560 break;
6561 default:
6562 break;
6563 }
6564}
6565
a06ea964
NC
6566/* A wrapper function to interface with libopcodes on encoding and
6567 record the error message if there is any.
6568
6569 Return TRUE on success; otherwise return FALSE. */
6570
6571static bfd_boolean
6572do_encode (const aarch64_opcode *opcode, aarch64_inst *instr,
6573 aarch64_insn *code)
6574{
6575 aarch64_operand_error error_info;
6576 error_info.kind = AARCH64_OPDE_NIL;
6577 if (aarch64_opcode_encode (opcode, instr, code, NULL, &error_info))
6578 return TRUE;
6579 else
6580 {
6581 gas_assert (error_info.kind != AARCH64_OPDE_NIL);
6582 record_operand_error_info (opcode, &error_info);
6583 return FALSE;
6584 }
6585}
6586
6587#ifdef DEBUG_AARCH64
6588static inline void
6589dump_opcode_operands (const aarch64_opcode *opcode)
6590{
6591 int i = 0;
6592 while (opcode->operands[i] != AARCH64_OPND_NIL)
6593 {
6594 aarch64_verbose ("\t\t opnd%d: %s", i,
6595 aarch64_get_operand_name (opcode->operands[i])[0] != '\0'
6596 ? aarch64_get_operand_name (opcode->operands[i])
6597 : aarch64_get_operand_desc (opcode->operands[i]));
6598 ++i;
6599 }
6600}
6601#endif /* DEBUG_AARCH64 */
6602
6603/* This is the guts of the machine-dependent assembler. STR points to a
6604 machine dependent instruction. This function is supposed to emit
6605 the frags/bytes it assembles to. */
6606
6607void
6608md_assemble (char *str)
6609{
6610 char *p = str;
6611 templates *template;
6612 aarch64_opcode *opcode;
6613 aarch64_inst *inst_base;
6614 unsigned saved_cond;
6615
6616 /* Align the previous label if needed. */
6617 if (last_label_seen != NULL)
6618 {
6619 symbol_set_frag (last_label_seen, frag_now);
6620 S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
6621 S_SET_SEGMENT (last_label_seen, now_seg);
6622 }
6623
6624 inst.reloc.type = BFD_RELOC_UNUSED;
6625
6626 DEBUG_TRACE ("\n\n");
6627 DEBUG_TRACE ("==============================");
6628 DEBUG_TRACE ("Enter md_assemble with %s", str);
6629
6630 template = opcode_lookup (&p);
6631 if (!template)
6632 {
6633 /* It wasn't an instruction, but it might be a register alias of
6634 the form alias .req reg directive. */
6635 if (!create_register_alias (str, p))
6636 as_bad (_("unknown mnemonic `%s' -- `%s'"), get_mnemonic_name (str),
6637 str);
6638 return;
6639 }
6640
6641 skip_whitespace (p);
6642 if (*p == ',')
6643 {
6644 as_bad (_("unexpected comma after the mnemonic name `%s' -- `%s'"),
6645 get_mnemonic_name (str), str);
6646 return;
6647 }
6648
6649 init_operand_error_report ();
6650
eb9d6cc9
RL
6651 /* Sections are assumed to start aligned. In executable section, there is no
6652 MAP_DATA symbol pending. So we only align the address during
6653 MAP_DATA --> MAP_INSN transition.
6654 For other sections, this is not guaranteed. */
6655 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
6656 if (!need_pass_2 && subseg_text_p (now_seg) && mapstate == MAP_DATA)
6657 frag_align_code (2, 0);
6658
a06ea964
NC
6659 saved_cond = inst.cond;
6660 reset_aarch64_instruction (&inst);
6661 inst.cond = saved_cond;
6662
6663 /* Iterate through all opcode entries with the same mnemonic name. */
6664 do
6665 {
6666 opcode = template->opcode;
6667
6668 DEBUG_TRACE ("opcode %s found", opcode->name);
6669#ifdef DEBUG_AARCH64
6670 if (debug_dump)
6671 dump_opcode_operands (opcode);
6672#endif /* DEBUG_AARCH64 */
6673
a06ea964
NC
6674 mapping_state (MAP_INSN);
6675
6676 inst_base = &inst.base;
6677 inst_base->opcode = opcode;
6678
6679 /* Truly conditionally executed instructions, e.g. b.cond. */
6680 if (opcode->flags & F_COND)
6681 {
6682 gas_assert (inst.cond != COND_ALWAYS);
6683 inst_base->cond = get_cond_from_value (inst.cond);
6684 DEBUG_TRACE ("condition found %s", inst_base->cond->names[0]);
6685 }
6686 else if (inst.cond != COND_ALWAYS)
6687 {
6688 /* It shouldn't arrive here, where the assembly looks like a
6689 conditional instruction but the found opcode is unconditional. */
6690 gas_assert (0);
6691 continue;
6692 }
6693
6694 if (parse_operands (p, opcode)
6695 && programmer_friendly_fixup (&inst)
6696 && do_encode (inst_base->opcode, &inst.base, &inst_base->value))
6697 {
3f06bfce
YZ
6698 /* Check that this instruction is supported for this CPU. */
6699 if (!opcode->avariant
93d8990c 6700 || !AARCH64_CPU_HAS_ALL_FEATURES (cpu_variant, *opcode->avariant))
3f06bfce
YZ
6701 {
6702 as_bad (_("selected processor does not support `%s'"), str);
6703 return;
6704 }
6705
54a28c4c
JW
6706 warn_unpredictable_ldst (&inst, str);
6707
a06ea964
NC
6708 if (inst.reloc.type == BFD_RELOC_UNUSED
6709 || !inst.reloc.need_libopcodes_p)
6710 output_inst (NULL);
6711 else
6712 {
6713 /* If there is relocation generated for the instruction,
6714 store the instruction information for the future fix-up. */
6715 struct aarch64_inst *copy;
6716 gas_assert (inst.reloc.type != BFD_RELOC_UNUSED);
325801bd 6717 copy = XNEW (struct aarch64_inst);
a06ea964
NC
6718 memcpy (copy, &inst.base, sizeof (struct aarch64_inst));
6719 output_inst (copy);
6720 }
6721 return;
6722 }
6723
6724 template = template->next;
6725 if (template != NULL)
6726 {
6727 reset_aarch64_instruction (&inst);
6728 inst.cond = saved_cond;
6729 }
6730 }
6731 while (template != NULL);
6732
6733 /* Issue the error messages if any. */
6734 output_operand_error_report (str);
6735}
6736
6737/* Various frobbings of labels and their addresses. */
6738
6739void
6740aarch64_start_line_hook (void)
6741{
6742 last_label_seen = NULL;
6743}
6744
6745void
6746aarch64_frob_label (symbolS * sym)
6747{
6748 last_label_seen = sym;
6749
6750 dwarf2_emit_label (sym);
6751}
6752
6753int
6754aarch64_data_in_code (void)
6755{
6756 if (!strncmp (input_line_pointer + 1, "data:", 5))
6757 {
6758 *input_line_pointer = '/';
6759 input_line_pointer += 5;
6760 *input_line_pointer = 0;
6761 return 1;
6762 }
6763
6764 return 0;
6765}
6766
6767char *
6768aarch64_canonicalize_symbol_name (char *name)
6769{
6770 int len;
6771
6772 if ((len = strlen (name)) > 5 && streq (name + len - 5, "/data"))
6773 *(name + len - 5) = 0;
6774
6775 return name;
6776}
6777\f
6778/* Table of all register names defined by default. The user can
6779 define additional names with .req. Note that all register names
6780 should appear in both upper and lowercase variants. Some registers
6781 also have mixed-case names. */
6782
6783#define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
8975f864 6784#define REGDEF_ALIAS(s, n, t) { #s, n, REG_TYPE_##t, FALSE}
a06ea964 6785#define REGNUM(p,n,t) REGDEF(p##n, n, t)
f11ad6bc 6786#define REGSET16(p,t) \
a06ea964
NC
6787 REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
6788 REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
6789 REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
f11ad6bc
RS
6790 REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
6791#define REGSET31(p,t) \
6792 REGSET16(p, t), \
a06ea964
NC
6793 REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
6794 REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
6795 REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
6796 REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t)
6797#define REGSET(p,t) \
6798 REGSET31(p,t), REGNUM(p,31,t)
6799
6800/* These go into aarch64_reg_hsh hash-table. */
6801static const reg_entry reg_names[] = {
6802 /* Integer registers. */
6803 REGSET31 (x, R_64), REGSET31 (X, R_64),
6804 REGSET31 (w, R_32), REGSET31 (W, R_32),
6805
8975f864 6806 REGDEF_ALIAS (ip0, 16, R_64), REGDEF_ALIAS (IP0, 16, R_64),
f10e937a 6807 REGDEF_ALIAS (ip1, 17, R_64), REGDEF_ALIAS (IP1, 17, R_64),
8975f864
RR
6808 REGDEF_ALIAS (fp, 29, R_64), REGDEF_ALIAS (FP, 29, R_64),
6809 REGDEF_ALIAS (lr, 30, R_64), REGDEF_ALIAS (LR, 30, R_64),
a06ea964
NC
6810 REGDEF (wsp, 31, SP_32), REGDEF (WSP, 31, SP_32),
6811 REGDEF (sp, 31, SP_64), REGDEF (SP, 31, SP_64),
6812
6813 REGDEF (wzr, 31, Z_32), REGDEF (WZR, 31, Z_32),
6814 REGDEF (xzr, 31, Z_64), REGDEF (XZR, 31, Z_64),
6815
a06ea964
NC
6816 /* Floating-point single precision registers. */
6817 REGSET (s, FP_S), REGSET (S, FP_S),
6818
6819 /* Floating-point double precision registers. */
6820 REGSET (d, FP_D), REGSET (D, FP_D),
6821
6822 /* Floating-point half precision registers. */
6823 REGSET (h, FP_H), REGSET (H, FP_H),
6824
6825 /* Floating-point byte precision registers. */
6826 REGSET (b, FP_B), REGSET (B, FP_B),
6827
6828 /* Floating-point quad precision registers. */
6829 REGSET (q, FP_Q), REGSET (Q, FP_Q),
6830
6831 /* FP/SIMD registers. */
6832 REGSET (v, VN), REGSET (V, VN),
f11ad6bc
RS
6833
6834 /* SVE vector registers. */
6835 REGSET (z, ZN), REGSET (Z, ZN),
6836
6837 /* SVE predicate registers. */
6838 REGSET16 (p, PN), REGSET16 (P, PN)
a06ea964
NC
6839};
6840
6841#undef REGDEF
8975f864 6842#undef REGDEF_ALIAS
a06ea964 6843#undef REGNUM
f11ad6bc
RS
6844#undef REGSET16
6845#undef REGSET31
a06ea964
NC
6846#undef REGSET
6847
6848#define N 1
6849#define n 0
6850#define Z 1
6851#define z 0
6852#define C 1
6853#define c 0
6854#define V 1
6855#define v 0
6856#define B(a,b,c,d) (((a) << 3) | ((b) << 2) | ((c) << 1) | (d))
6857static const asm_nzcv nzcv_names[] = {
6858 {"nzcv", B (n, z, c, v)},
6859 {"nzcV", B (n, z, c, V)},
6860 {"nzCv", B (n, z, C, v)},
6861 {"nzCV", B (n, z, C, V)},
6862 {"nZcv", B (n, Z, c, v)},
6863 {"nZcV", B (n, Z, c, V)},
6864 {"nZCv", B (n, Z, C, v)},
6865 {"nZCV", B (n, Z, C, V)},
6866 {"Nzcv", B (N, z, c, v)},
6867 {"NzcV", B (N, z, c, V)},
6868 {"NzCv", B (N, z, C, v)},
6869 {"NzCV", B (N, z, C, V)},
6870 {"NZcv", B (N, Z, c, v)},
6871 {"NZcV", B (N, Z, c, V)},
6872 {"NZCv", B (N, Z, C, v)},
6873 {"NZCV", B (N, Z, C, V)}
6874};
6875
6876#undef N
6877#undef n
6878#undef Z
6879#undef z
6880#undef C
6881#undef c
6882#undef V
6883#undef v
6884#undef B
6885\f
6886/* MD interface: bits in the object file. */
6887
6888/* Turn an integer of n bytes (in val) into a stream of bytes appropriate
6889 for use in the a.out file, and stores them in the array pointed to by buf.
6890 This knows about the endian-ness of the target machine and does
6891 THE RIGHT THING, whatever it is. Possible values for n are 1 (byte)
6892 2 (short) and 4 (long) Floating numbers are put out as a series of
6893 LITTLENUMS (shorts, here at least). */
6894
6895void
6896md_number_to_chars (char *buf, valueT val, int n)
6897{
6898 if (target_big_endian)
6899 number_to_chars_bigendian (buf, val, n);
6900 else
6901 number_to_chars_littleendian (buf, val, n);
6902}
6903
6904/* MD interface: Sections. */
6905
6906/* Estimate the size of a frag before relaxing. Assume everything fits in
6907 4 bytes. */
6908
6909int
6910md_estimate_size_before_relax (fragS * fragp, segT segtype ATTRIBUTE_UNUSED)
6911{
6912 fragp->fr_var = 4;
6913 return 4;
6914}
6915
6916/* Round up a section size to the appropriate boundary. */
6917
6918valueT
6919md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
6920{
6921 return size;
6922}
6923
6924/* This is called from HANDLE_ALIGN in write.c. Fill in the contents
f803aa8e
DPT
6925 of an rs_align_code fragment.
6926
6927 Here we fill the frag with the appropriate info for padding the
6928 output stream. The resulting frag will consist of a fixed (fr_fix)
6929 and of a repeating (fr_var) part.
6930
6931 The fixed content is always emitted before the repeating content and
6932 these two parts are used as follows in constructing the output:
6933 - the fixed part will be used to align to a valid instruction word
6934 boundary, in case that we start at a misaligned address; as no
6935 executable instruction can live at the misaligned location, we
6936 simply fill with zeros;
6937 - the variable part will be used to cover the remaining padding and
6938 we fill using the AArch64 NOP instruction.
6939
6940 Note that the size of a RS_ALIGN_CODE fragment is always 7 to provide
6941 enough storage space for up to 3 bytes for padding the back to a valid
6942 instruction alignment and exactly 4 bytes to store the NOP pattern. */
a06ea964
NC
6943
6944void
6945aarch64_handle_align (fragS * fragP)
6946{
6947 /* NOP = d503201f */
6948 /* AArch64 instructions are always little-endian. */
d9235011 6949 static unsigned char const aarch64_noop[4] = { 0x1f, 0x20, 0x03, 0xd5 };
a06ea964
NC
6950
6951 int bytes, fix, noop_size;
6952 char *p;
a06ea964
NC
6953
6954 if (fragP->fr_type != rs_align_code)
6955 return;
6956
6957 bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
6958 p = fragP->fr_literal + fragP->fr_fix;
a06ea964
NC
6959
6960#ifdef OBJ_ELF
6961 gas_assert (fragP->tc_frag_data.recorded);
6962#endif
6963
a06ea964 6964 noop_size = sizeof (aarch64_noop);
a06ea964 6965
f803aa8e
DPT
6966 fix = bytes & (noop_size - 1);
6967 if (fix)
a06ea964 6968 {
a06ea964
NC
6969#ifdef OBJ_ELF
6970 insert_data_mapping_symbol (MAP_INSN, fragP->fr_fix, fragP, fix);
6971#endif
6972 memset (p, 0, fix);
6973 p += fix;
f803aa8e 6974 fragP->fr_fix += fix;
a06ea964
NC
6975 }
6976
f803aa8e
DPT
6977 if (noop_size)
6978 memcpy (p, aarch64_noop, noop_size);
6979 fragP->fr_var = noop_size;
a06ea964
NC
6980}
6981
6982/* Perform target specific initialisation of a frag.
6983 Note - despite the name this initialisation is not done when the frag
6984 is created, but only when its type is assigned. A frag can be created
6985 and used a long time before its type is set, so beware of assuming that
33eaf5de 6986 this initialisation is performed first. */
a06ea964
NC
6987
6988#ifndef OBJ_ELF
6989void
6990aarch64_init_frag (fragS * fragP ATTRIBUTE_UNUSED,
6991 int max_chars ATTRIBUTE_UNUSED)
6992{
6993}
6994
6995#else /* OBJ_ELF is defined. */
6996void
6997aarch64_init_frag (fragS * fragP, int max_chars)
6998{
6999 /* Record a mapping symbol for alignment frags. We will delete this
7000 later if the alignment ends up empty. */
7001 if (!fragP->tc_frag_data.recorded)
c7ad08e6
RL
7002 fragP->tc_frag_data.recorded = 1;
7003
e8d84ca1
NC
7004 /* PR 21809: Do not set a mapping state for debug sections
7005 - it just confuses other tools. */
7006 if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
7007 return;
7008
c7ad08e6 7009 switch (fragP->fr_type)
a06ea964 7010 {
c7ad08e6
RL
7011 case rs_align_test:
7012 case rs_fill:
7013 mapping_state_2 (MAP_DATA, max_chars);
7014 break;
7ea12e5c
NC
7015 case rs_align:
7016 /* PR 20364: We can get alignment frags in code sections,
7017 so do not just assume that we should use the MAP_DATA state. */
7018 mapping_state_2 (subseg_text_p (now_seg) ? MAP_INSN : MAP_DATA, max_chars);
7019 break;
c7ad08e6
RL
7020 case rs_align_code:
7021 mapping_state_2 (MAP_INSN, max_chars);
7022 break;
7023 default:
7024 break;
a06ea964
NC
7025 }
7026}
7027\f
7028/* Initialize the DWARF-2 unwind information for this procedure. */
7029
7030void
7031tc_aarch64_frame_initial_instructions (void)
7032{
7033 cfi_add_CFA_def_cfa (REG_SP, 0);
7034}
7035#endif /* OBJ_ELF */
7036
7037/* Convert REGNAME to a DWARF-2 register number. */
7038
7039int
7040tc_aarch64_regname_to_dw2regnum (char *regname)
7041{
7042 const reg_entry *reg = parse_reg (&regname);
7043 if (reg == NULL)
7044 return -1;
7045
7046 switch (reg->type)
7047 {
7048 case REG_TYPE_SP_32:
7049 case REG_TYPE_SP_64:
7050 case REG_TYPE_R_32:
7051 case REG_TYPE_R_64:
a2cac51c
RH
7052 return reg->number;
7053
a06ea964
NC
7054 case REG_TYPE_FP_B:
7055 case REG_TYPE_FP_H:
7056 case REG_TYPE_FP_S:
7057 case REG_TYPE_FP_D:
7058 case REG_TYPE_FP_Q:
a2cac51c
RH
7059 return reg->number + 64;
7060
a06ea964
NC
7061 default:
7062 break;
7063 }
7064 return -1;
7065}
7066
cec5225b
YZ
7067/* Implement DWARF2_ADDR_SIZE. */
7068
7069int
7070aarch64_dwarf2_addr_size (void)
7071{
7072#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7073 if (ilp32_p)
7074 return 4;
7075#endif
7076 return bfd_arch_bits_per_address (stdoutput) / 8;
7077}
7078
a06ea964
NC
7079/* MD interface: Symbol and relocation handling. */
7080
7081/* Return the address within the segment that a PC-relative fixup is
7082 relative to. For AArch64 PC-relative fixups applied to instructions
7083 are generally relative to the location plus AARCH64_PCREL_OFFSET bytes. */
7084
7085long
7086md_pcrel_from_section (fixS * fixP, segT seg)
7087{
7088 offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
7089
7090 /* If this is pc-relative and we are going to emit a relocation
7091 then we just want to put out any pipeline compensation that the linker
7092 will need. Otherwise we want to use the calculated base. */
7093 if (fixP->fx_pcrel
7094 && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
7095 || aarch64_force_relocation (fixP)))
7096 base = 0;
7097
7098 /* AArch64 should be consistent for all pc-relative relocations. */
7099 return base + AARCH64_PCREL_OFFSET;
7100}
7101
7102/* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
7103 Otherwise we have no need to default values of symbols. */
7104
7105symbolS *
7106md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
7107{
7108#ifdef OBJ_ELF
7109 if (name[0] == '_' && name[1] == 'G'
7110 && streq (name, GLOBAL_OFFSET_TABLE_NAME))
7111 {
7112 if (!GOT_symbol)
7113 {
7114 if (symbol_find (name))
7115 as_bad (_("GOT already in the symbol table"));
7116
7117 GOT_symbol = symbol_new (name, undefined_section,
7118 (valueT) 0, &zero_address_frag);
7119 }
7120
7121 return GOT_symbol;
7122 }
7123#endif
7124
7125 return 0;
7126}
7127
7128/* Return non-zero if the indicated VALUE has overflowed the maximum
7129 range expressible by a unsigned number with the indicated number of
7130 BITS. */
7131
7132static bfd_boolean
7133unsigned_overflow (valueT value, unsigned bits)
7134{
7135 valueT lim;
7136 if (bits >= sizeof (valueT) * 8)
7137 return FALSE;
7138 lim = (valueT) 1 << bits;
7139 return (value >= lim);
7140}
7141
7142
7143/* Return non-zero if the indicated VALUE has overflowed the maximum
7144 range expressible by an signed number with the indicated number of
7145 BITS. */
7146
7147static bfd_boolean
7148signed_overflow (offsetT value, unsigned bits)
7149{
7150 offsetT lim;
7151 if (bits >= sizeof (offsetT) * 8)
7152 return FALSE;
7153 lim = (offsetT) 1 << (bits - 1);
7154 return (value < -lim || value >= lim);
7155}
7156
7157/* Given an instruction in *INST, which is expected to be a scaled, 12-bit,
7158 unsigned immediate offset load/store instruction, try to encode it as
7159 an unscaled, 9-bit, signed immediate offset load/store instruction.
7160 Return TRUE if it is successful; otherwise return FALSE.
7161
7162 As a programmer-friendly assembler, LDUR/STUR instructions can be generated
7163 in response to the standard LDR/STR mnemonics when the immediate offset is
7164 unambiguous, i.e. when it is negative or unaligned. */
7165
7166static bfd_boolean
7167try_to_encode_as_unscaled_ldst (aarch64_inst *instr)
7168{
7169 int idx;
7170 enum aarch64_op new_op;
7171 const aarch64_opcode *new_opcode;
7172
7173 gas_assert (instr->opcode->iclass == ldst_pos);
7174
7175 switch (instr->opcode->op)
7176 {
7177 case OP_LDRB_POS:new_op = OP_LDURB; break;
7178 case OP_STRB_POS: new_op = OP_STURB; break;
7179 case OP_LDRSB_POS: new_op = OP_LDURSB; break;
7180 case OP_LDRH_POS: new_op = OP_LDURH; break;
7181 case OP_STRH_POS: new_op = OP_STURH; break;
7182 case OP_LDRSH_POS: new_op = OP_LDURSH; break;
7183 case OP_LDR_POS: new_op = OP_LDUR; break;
7184 case OP_STR_POS: new_op = OP_STUR; break;
7185 case OP_LDRF_POS: new_op = OP_LDURV; break;
7186 case OP_STRF_POS: new_op = OP_STURV; break;
7187 case OP_LDRSW_POS: new_op = OP_LDURSW; break;
7188 case OP_PRFM_POS: new_op = OP_PRFUM; break;
7189 default: new_op = OP_NIL; break;
7190 }
7191
7192 if (new_op == OP_NIL)
7193 return FALSE;
7194
7195 new_opcode = aarch64_get_opcode (new_op);
7196 gas_assert (new_opcode != NULL);
7197
7198 DEBUG_TRACE ("Check programmer-friendly STURB/LDURB -> STRB/LDRB: %d == %d",
7199 instr->opcode->op, new_opcode->op);
7200
7201 aarch64_replace_opcode (instr, new_opcode);
7202
7203 /* Clear up the ADDR_SIMM9's qualifier; otherwise the
7204 qualifier matching may fail because the out-of-date qualifier will
7205 prevent the operand being updated with a new and correct qualifier. */
7206 idx = aarch64_operand_index (instr->opcode->operands,
7207 AARCH64_OPND_ADDR_SIMM9);
7208 gas_assert (idx == 1);
7209 instr->operands[idx].qualifier = AARCH64_OPND_QLF_NIL;
7210
7211 DEBUG_TRACE ("Found LDURB entry to encode programmer-friendly LDRB");
7212
7213 if (!aarch64_opcode_encode (instr->opcode, instr, &instr->value, NULL, NULL))
7214 return FALSE;
7215
7216 return TRUE;
7217}
7218
7219/* Called by fix_insn to fix a MOV immediate alias instruction.
7220
7221 Operand for a generic move immediate instruction, which is an alias
7222 instruction that generates a single MOVZ, MOVN or ORR instruction to loads
7223 a 32-bit/64-bit immediate value into general register. An assembler error
7224 shall result if the immediate cannot be created by a single one of these
7225 instructions. If there is a choice, then to ensure reversability an
7226 assembler must prefer a MOVZ to MOVN, and MOVZ or MOVN to ORR. */
7227
7228static void
7229fix_mov_imm_insn (fixS *fixP, char *buf, aarch64_inst *instr, offsetT value)
7230{
7231 const aarch64_opcode *opcode;
7232
7233 /* Need to check if the destination is SP/ZR. The check has to be done
7234 before any aarch64_replace_opcode. */
7235 int try_mov_wide_p = !aarch64_stack_pointer_p (&instr->operands[0]);
7236 int try_mov_bitmask_p = !aarch64_zero_register_p (&instr->operands[0]);
7237
7238 instr->operands[1].imm.value = value;
7239 instr->operands[1].skip = 0;
7240
7241 if (try_mov_wide_p)
7242 {
7243 /* Try the MOVZ alias. */
7244 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDE);
7245 aarch64_replace_opcode (instr, opcode);
7246 if (aarch64_opcode_encode (instr->opcode, instr,
7247 &instr->value, NULL, NULL))
7248 {
7249 put_aarch64_insn (buf, instr->value);
7250 return;
7251 }
7252 /* Try the MOVK alias. */
7253 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDEN);
7254 aarch64_replace_opcode (instr, opcode);
7255 if (aarch64_opcode_encode (instr->opcode, instr,
7256 &instr->value, NULL, NULL))
7257 {
7258 put_aarch64_insn (buf, instr->value);
7259 return;
7260 }
7261 }
7262
7263 if (try_mov_bitmask_p)
7264 {
7265 /* Try the ORR alias. */
7266 opcode = aarch64_get_opcode (OP_MOV_IMM_LOG);
7267 aarch64_replace_opcode (instr, opcode);
7268 if (aarch64_opcode_encode (instr->opcode, instr,
7269 &instr->value, NULL, NULL))
7270 {
7271 put_aarch64_insn (buf, instr->value);
7272 return;
7273 }
7274 }
7275
7276 as_bad_where (fixP->fx_file, fixP->fx_line,
7277 _("immediate cannot be moved by a single instruction"));
7278}
7279
7280/* An instruction operand which is immediate related may have symbol used
7281 in the assembly, e.g.
7282
7283 mov w0, u32
7284 .set u32, 0x00ffff00
7285
7286 At the time when the assembly instruction is parsed, a referenced symbol,
7287 like 'u32' in the above example may not have been seen; a fixS is created
7288 in such a case and is handled here after symbols have been resolved.
7289 Instruction is fixed up with VALUE using the information in *FIXP plus
7290 extra information in FLAGS.
7291
7292 This function is called by md_apply_fix to fix up instructions that need
7293 a fix-up described above but does not involve any linker-time relocation. */
7294
7295static void
7296fix_insn (fixS *fixP, uint32_t flags, offsetT value)
7297{
7298 int idx;
7299 uint32_t insn;
7300 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
7301 enum aarch64_opnd opnd = fixP->tc_fix_data.opnd;
7302 aarch64_inst *new_inst = fixP->tc_fix_data.inst;
7303
7304 if (new_inst)
7305 {
7306 /* Now the instruction is about to be fixed-up, so the operand that
7307 was previously marked as 'ignored' needs to be unmarked in order
7308 to get the encoding done properly. */
7309 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
7310 new_inst->operands[idx].skip = 0;
7311 }
7312
7313 gas_assert (opnd != AARCH64_OPND_NIL);
7314
7315 switch (opnd)
7316 {
7317 case AARCH64_OPND_EXCEPTION:
7318 if (unsigned_overflow (value, 16))
7319 as_bad_where (fixP->fx_file, fixP->fx_line,
7320 _("immediate out of range"));
7321 insn = get_aarch64_insn (buf);
7322 insn |= encode_svc_imm (value);
7323 put_aarch64_insn (buf, insn);
7324 break;
7325
7326 case AARCH64_OPND_AIMM:
7327 /* ADD or SUB with immediate.
7328 NOTE this assumes we come here with a add/sub shifted reg encoding
7329 3 322|2222|2 2 2 21111 111111
7330 1 098|7654|3 2 1 09876 543210 98765 43210
7331 0b000000 sf 000|1011|shift 0 Rm imm6 Rn Rd ADD
7332 2b000000 sf 010|1011|shift 0 Rm imm6 Rn Rd ADDS
7333 4b000000 sf 100|1011|shift 0 Rm imm6 Rn Rd SUB
7334 6b000000 sf 110|1011|shift 0 Rm imm6 Rn Rd SUBS
7335 ->
7336 3 322|2222|2 2 221111111111
7337 1 098|7654|3 2 109876543210 98765 43210
7338 11000000 sf 001|0001|shift imm12 Rn Rd ADD
7339 31000000 sf 011|0001|shift imm12 Rn Rd ADDS
7340 51000000 sf 101|0001|shift imm12 Rn Rd SUB
7341 71000000 sf 111|0001|shift imm12 Rn Rd SUBS
7342 Fields sf Rn Rd are already set. */
7343 insn = get_aarch64_insn (buf);
7344 if (value < 0)
7345 {
7346 /* Add <-> sub. */
7347 insn = reencode_addsub_switch_add_sub (insn);
7348 value = -value;
7349 }
7350
7351 if ((flags & FIXUP_F_HAS_EXPLICIT_SHIFT) == 0
7352 && unsigned_overflow (value, 12))
7353 {
7354 /* Try to shift the value by 12 to make it fit. */
7355 if (((value >> 12) << 12) == value
7356 && ! unsigned_overflow (value, 12 + 12))
7357 {
7358 value >>= 12;
7359 insn |= encode_addsub_imm_shift_amount (1);
7360 }
7361 }
7362
7363 if (unsigned_overflow (value, 12))
7364 as_bad_where (fixP->fx_file, fixP->fx_line,
7365 _("immediate out of range"));
7366
7367 insn |= encode_addsub_imm (value);
7368
7369 put_aarch64_insn (buf, insn);
7370 break;
7371
7372 case AARCH64_OPND_SIMD_IMM:
7373 case AARCH64_OPND_SIMD_IMM_SFT:
7374 case AARCH64_OPND_LIMM:
7375 /* Bit mask immediate. */
7376 gas_assert (new_inst != NULL);
7377 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
7378 new_inst->operands[idx].imm.value = value;
7379 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
7380 &new_inst->value, NULL, NULL))
7381 put_aarch64_insn (buf, new_inst->value);
7382 else
7383 as_bad_where (fixP->fx_file, fixP->fx_line,
7384 _("invalid immediate"));
7385 break;
7386
7387 case AARCH64_OPND_HALF:
7388 /* 16-bit unsigned immediate. */
7389 if (unsigned_overflow (value, 16))
7390 as_bad_where (fixP->fx_file, fixP->fx_line,
7391 _("immediate out of range"));
7392 insn = get_aarch64_insn (buf);
7393 insn |= encode_movw_imm (value & 0xffff);
7394 put_aarch64_insn (buf, insn);
7395 break;
7396
7397 case AARCH64_OPND_IMM_MOV:
7398 /* Operand for a generic move immediate instruction, which is
7399 an alias instruction that generates a single MOVZ, MOVN or ORR
7400 instruction to loads a 32-bit/64-bit immediate value into general
7401 register. An assembler error shall result if the immediate cannot be
7402 created by a single one of these instructions. If there is a choice,
7403 then to ensure reversability an assembler must prefer a MOVZ to MOVN,
7404 and MOVZ or MOVN to ORR. */
7405 gas_assert (new_inst != NULL);
7406 fix_mov_imm_insn (fixP, buf, new_inst, value);
7407 break;
7408
7409 case AARCH64_OPND_ADDR_SIMM7:
7410 case AARCH64_OPND_ADDR_SIMM9:
7411 case AARCH64_OPND_ADDR_SIMM9_2:
3f06e550 7412 case AARCH64_OPND_ADDR_SIMM10:
a06ea964
NC
7413 case AARCH64_OPND_ADDR_UIMM12:
7414 /* Immediate offset in an address. */
7415 insn = get_aarch64_insn (buf);
7416
7417 gas_assert (new_inst != NULL && new_inst->value == insn);
7418 gas_assert (new_inst->opcode->operands[1] == opnd
7419 || new_inst->opcode->operands[2] == opnd);
7420
7421 /* Get the index of the address operand. */
7422 if (new_inst->opcode->operands[1] == opnd)
7423 /* e.g. STR <Xt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]. */
7424 idx = 1;
7425 else
7426 /* e.g. LDP <Qt1>, <Qt2>, [<Xn|SP>{, #<imm>}]. */
7427 idx = 2;
7428
7429 /* Update the resolved offset value. */
7430 new_inst->operands[idx].addr.offset.imm = value;
7431
7432 /* Encode/fix-up. */
7433 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
7434 &new_inst->value, NULL, NULL))
7435 {
7436 put_aarch64_insn (buf, new_inst->value);
7437 break;
7438 }
7439 else if (new_inst->opcode->iclass == ldst_pos
7440 && try_to_encode_as_unscaled_ldst (new_inst))
7441 {
7442 put_aarch64_insn (buf, new_inst->value);
7443 break;
7444 }
7445
7446 as_bad_where (fixP->fx_file, fixP->fx_line,
7447 _("immediate offset out of range"));
7448 break;
7449
7450 default:
7451 gas_assert (0);
7452 as_fatal (_("unhandled operand code %d"), opnd);
7453 }
7454}
7455
7456/* Apply a fixup (fixP) to segment data, once it has been determined
7457 by our caller that we have all the info we need to fix it up.
7458
7459 Parameter valP is the pointer to the value of the bits. */
7460
7461void
7462md_apply_fix (fixS * fixP, valueT * valP, segT seg)
7463{
7464 offsetT value = *valP;
7465 uint32_t insn;
7466 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
7467 int scale;
7468 unsigned flags = fixP->fx_addnumber;
7469
7470 DEBUG_TRACE ("\n\n");
7471 DEBUG_TRACE ("~~~~~~~~~~~~~~~~~~~~~~~~~");
7472 DEBUG_TRACE ("Enter md_apply_fix");
7473
7474 gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
7475
7476 /* Note whether this will delete the relocation. */
7477
7478 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
7479 fixP->fx_done = 1;
7480
7481 /* Process the relocations. */
7482 switch (fixP->fx_r_type)
7483 {
7484 case BFD_RELOC_NONE:
7485 /* This will need to go in the object file. */
7486 fixP->fx_done = 0;
7487 break;
7488
7489 case BFD_RELOC_8:
7490 case BFD_RELOC_8_PCREL:
7491 if (fixP->fx_done || !seg->use_rela_p)
7492 md_number_to_chars (buf, value, 1);
7493 break;
7494
7495 case BFD_RELOC_16:
7496 case BFD_RELOC_16_PCREL:
7497 if (fixP->fx_done || !seg->use_rela_p)
7498 md_number_to_chars (buf, value, 2);
7499 break;
7500
7501 case BFD_RELOC_32:
7502 case BFD_RELOC_32_PCREL:
7503 if (fixP->fx_done || !seg->use_rela_p)
7504 md_number_to_chars (buf, value, 4);
7505 break;
7506
7507 case BFD_RELOC_64:
7508 case BFD_RELOC_64_PCREL:
7509 if (fixP->fx_done || !seg->use_rela_p)
7510 md_number_to_chars (buf, value, 8);
7511 break;
7512
7513 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
7514 /* We claim that these fixups have been processed here, even if
7515 in fact we generate an error because we do not have a reloc
7516 for them, so tc_gen_reloc() will reject them. */
7517 fixP->fx_done = 1;
7518 if (fixP->fx_addsy && !S_IS_DEFINED (fixP->fx_addsy))
7519 {
7520 as_bad_where (fixP->fx_file, fixP->fx_line,
7521 _("undefined symbol %s used as an immediate value"),
7522 S_GET_NAME (fixP->fx_addsy));
7523 goto apply_fix_return;
7524 }
7525 fix_insn (fixP, flags, value);
7526 break;
7527
7528 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
a06ea964
NC
7529 if (fixP->fx_done || !seg->use_rela_p)
7530 {
89d2a2a3
MS
7531 if (value & 3)
7532 as_bad_where (fixP->fx_file, fixP->fx_line,
7533 _("pc-relative load offset not word aligned"));
7534 if (signed_overflow (value, 21))
7535 as_bad_where (fixP->fx_file, fixP->fx_line,
7536 _("pc-relative load offset out of range"));
a06ea964
NC
7537 insn = get_aarch64_insn (buf);
7538 insn |= encode_ld_lit_ofs_19 (value >> 2);
7539 put_aarch64_insn (buf, insn);
7540 }
7541 break;
7542
7543 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
a06ea964
NC
7544 if (fixP->fx_done || !seg->use_rela_p)
7545 {
89d2a2a3
MS
7546 if (signed_overflow (value, 21))
7547 as_bad_where (fixP->fx_file, fixP->fx_line,
7548 _("pc-relative address offset out of range"));
a06ea964
NC
7549 insn = get_aarch64_insn (buf);
7550 insn |= encode_adr_imm (value);
7551 put_aarch64_insn (buf, insn);
7552 }
7553 break;
7554
7555 case BFD_RELOC_AARCH64_BRANCH19:
a06ea964
NC
7556 if (fixP->fx_done || !seg->use_rela_p)
7557 {
89d2a2a3
MS
7558 if (value & 3)
7559 as_bad_where (fixP->fx_file, fixP->fx_line,
7560 _("conditional branch target not word aligned"));
7561 if (signed_overflow (value, 21))
7562 as_bad_where (fixP->fx_file, fixP->fx_line,
7563 _("conditional branch out of range"));
a06ea964
NC
7564 insn = get_aarch64_insn (buf);
7565 insn |= encode_cond_branch_ofs_19 (value >> 2);
7566 put_aarch64_insn (buf, insn);
7567 }
7568 break;
7569
7570 case BFD_RELOC_AARCH64_TSTBR14:
a06ea964
NC
7571 if (fixP->fx_done || !seg->use_rela_p)
7572 {
89d2a2a3
MS
7573 if (value & 3)
7574 as_bad_where (fixP->fx_file, fixP->fx_line,
7575 _("conditional branch target not word aligned"));
7576 if (signed_overflow (value, 16))
7577 as_bad_where (fixP->fx_file, fixP->fx_line,
7578 _("conditional branch out of range"));
a06ea964
NC
7579 insn = get_aarch64_insn (buf);
7580 insn |= encode_tst_branch_ofs_14 (value >> 2);
7581 put_aarch64_insn (buf, insn);
7582 }
7583 break;
7584
a06ea964 7585 case BFD_RELOC_AARCH64_CALL26:
f09c556a 7586 case BFD_RELOC_AARCH64_JUMP26:
a06ea964
NC
7587 if (fixP->fx_done || !seg->use_rela_p)
7588 {
89d2a2a3
MS
7589 if (value & 3)
7590 as_bad_where (fixP->fx_file, fixP->fx_line,
7591 _("branch target not word aligned"));
7592 if (signed_overflow (value, 28))
7593 as_bad_where (fixP->fx_file, fixP->fx_line,
7594 _("branch out of range"));
a06ea964
NC
7595 insn = get_aarch64_insn (buf);
7596 insn |= encode_branch_ofs_26 (value >> 2);
7597 put_aarch64_insn (buf, insn);
7598 }
7599 break;
7600
7601 case BFD_RELOC_AARCH64_MOVW_G0:
a06ea964 7602 case BFD_RELOC_AARCH64_MOVW_G0_NC:
f09c556a 7603 case BFD_RELOC_AARCH64_MOVW_G0_S:
ca632371 7604 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
a06ea964
NC
7605 scale = 0;
7606 goto movw_common;
7607 case BFD_RELOC_AARCH64_MOVW_G1:
a06ea964 7608 case BFD_RELOC_AARCH64_MOVW_G1_NC:
f09c556a 7609 case BFD_RELOC_AARCH64_MOVW_G1_S:
654248e7 7610 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
a06ea964
NC
7611 scale = 16;
7612 goto movw_common;
43a357f9
RL
7613 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7614 scale = 0;
7615 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7616 /* Should always be exported to object file, see
7617 aarch64_force_relocation(). */
7618 gas_assert (!fixP->fx_done);
7619 gas_assert (seg->use_rela_p);
7620 goto movw_common;
7621 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7622 scale = 16;
7623 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7624 /* Should always be exported to object file, see
7625 aarch64_force_relocation(). */
7626 gas_assert (!fixP->fx_done);
7627 gas_assert (seg->use_rela_p);
7628 goto movw_common;
a06ea964 7629 case BFD_RELOC_AARCH64_MOVW_G2:
a06ea964 7630 case BFD_RELOC_AARCH64_MOVW_G2_NC:
f09c556a 7631 case BFD_RELOC_AARCH64_MOVW_G2_S:
a06ea964
NC
7632 scale = 32;
7633 goto movw_common;
7634 case BFD_RELOC_AARCH64_MOVW_G3:
7635 scale = 48;
7636 movw_common:
7637 if (fixP->fx_done || !seg->use_rela_p)
7638 {
7639 insn = get_aarch64_insn (buf);
7640
7641 if (!fixP->fx_done)
7642 {
7643 /* REL signed addend must fit in 16 bits */
7644 if (signed_overflow (value, 16))
7645 as_bad_where (fixP->fx_file, fixP->fx_line,
7646 _("offset out of range"));
7647 }
7648 else
7649 {
7650 /* Check for overflow and scale. */
7651 switch (fixP->fx_r_type)
7652 {
7653 case BFD_RELOC_AARCH64_MOVW_G0:
7654 case BFD_RELOC_AARCH64_MOVW_G1:
7655 case BFD_RELOC_AARCH64_MOVW_G2:
7656 case BFD_RELOC_AARCH64_MOVW_G3:
654248e7 7657 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
43a357f9 7658 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964
NC
7659 if (unsigned_overflow (value, scale + 16))
7660 as_bad_where (fixP->fx_file, fixP->fx_line,
7661 _("unsigned value out of range"));
7662 break;
7663 case BFD_RELOC_AARCH64_MOVW_G0_S:
7664 case BFD_RELOC_AARCH64_MOVW_G1_S:
7665 case BFD_RELOC_AARCH64_MOVW_G2_S:
7666 /* NOTE: We can only come here with movz or movn. */
7667 if (signed_overflow (value, scale + 16))
7668 as_bad_where (fixP->fx_file, fixP->fx_line,
7669 _("signed value out of range"));
7670 if (value < 0)
7671 {
7672 /* Force use of MOVN. */
7673 value = ~value;
7674 insn = reencode_movzn_to_movn (insn);
7675 }
7676 else
7677 {
7678 /* Force use of MOVZ. */
7679 insn = reencode_movzn_to_movz (insn);
7680 }
7681 break;
7682 default:
7683 /* Unchecked relocations. */
7684 break;
7685 }
7686 value >>= scale;
7687 }
7688
7689 /* Insert value into MOVN/MOVZ/MOVK instruction. */
7690 insn |= encode_movw_imm (value & 0xffff);
7691
7692 put_aarch64_insn (buf, insn);
7693 }
7694 break;
7695
a6bb11b2
YZ
7696 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
7697 fixP->fx_r_type = (ilp32_p
7698 ? BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC
7699 : BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7700 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7701 /* Should always be exported to object file, see
7702 aarch64_force_relocation(). */
7703 gas_assert (!fixP->fx_done);
7704 gas_assert (seg->use_rela_p);
7705 break;
7706
7707 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
7708 fixP->fx_r_type = (ilp32_p
7709 ? BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
f955cccf 7710 : BFD_RELOC_AARCH64_TLSDESC_LD64_LO12);
a6bb11b2
YZ
7711 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7712 /* Should always be exported to object file, see
7713 aarch64_force_relocation(). */
7714 gas_assert (!fixP->fx_done);
7715 gas_assert (seg->use_rela_p);
7716 break;
7717
f955cccf 7718 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
2c0a3565 7719 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 7720 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
2c0a3565 7721 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
f955cccf 7722 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
1ada945d 7723 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
a06ea964 7724 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
2c0a3565 7725 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 7726 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
3e8286c0 7727 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
1aa66fb1 7728 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 7729 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 7730 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
2c0a3565 7731 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 7732 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
7733 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7734 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
49df5539 7735 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
70151fb5 7736 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
13289c10 7737 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
a12fad50 7738 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
1107e076 7739 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6c37fedc 7740 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4c562523
JW
7741 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7742 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7743 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7744 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7745 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7746 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7747 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7748 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
49df5539
JW
7749 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7750 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7751 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7752 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7753 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964 7754 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
2c0a3565 7755 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
a06ea964 7756 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
a06ea964
NC
7757 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7758 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
2c0a3565
MS
7759 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7760 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7761 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
a06ea964
NC
7762 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7763 /* Should always be exported to object file, see
7764 aarch64_force_relocation(). */
7765 gas_assert (!fixP->fx_done);
7766 gas_assert (seg->use_rela_p);
7767 break;
7768
a6bb11b2
YZ
7769 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
7770 /* Should always be exported to object file, see
7771 aarch64_force_relocation(). */
7772 fixP->fx_r_type = (ilp32_p
7773 ? BFD_RELOC_AARCH64_LD32_GOT_LO12_NC
7774 : BFD_RELOC_AARCH64_LD64_GOT_LO12_NC);
7775 gas_assert (!fixP->fx_done);
7776 gas_assert (seg->use_rela_p);
7777 break;
7778
a06ea964 7779 case BFD_RELOC_AARCH64_ADD_LO12:
f09c556a
JW
7780 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7781 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7782 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7783 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7784 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3d715ce4 7785 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
87f5fbcc 7786 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
a921b5bd 7787 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
f09c556a
JW
7788 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7789 case BFD_RELOC_AARCH64_LDST128_LO12:
a06ea964
NC
7790 case BFD_RELOC_AARCH64_LDST16_LO12:
7791 case BFD_RELOC_AARCH64_LDST32_LO12:
7792 case BFD_RELOC_AARCH64_LDST64_LO12:
f09c556a 7793 case BFD_RELOC_AARCH64_LDST8_LO12:
a06ea964
NC
7794 /* Should always be exported to object file, see
7795 aarch64_force_relocation(). */
7796 gas_assert (!fixP->fx_done);
7797 gas_assert (seg->use_rela_p);
7798 break;
7799
7800 case BFD_RELOC_AARCH64_TLSDESC_ADD:
a06ea964 7801 case BFD_RELOC_AARCH64_TLSDESC_CALL:
f09c556a 7802 case BFD_RELOC_AARCH64_TLSDESC_LDR:
a06ea964
NC
7803 break;
7804
b97e87cc
NC
7805 case BFD_RELOC_UNUSED:
7806 /* An error will already have been reported. */
7807 break;
7808
a06ea964
NC
7809 default:
7810 as_bad_where (fixP->fx_file, fixP->fx_line,
7811 _("unexpected %s fixup"),
7812 bfd_get_reloc_code_name (fixP->fx_r_type));
7813 break;
7814 }
7815
7816apply_fix_return:
7817 /* Free the allocated the struct aarch64_inst.
7818 N.B. currently there are very limited number of fix-up types actually use
7819 this field, so the impact on the performance should be minimal . */
7820 if (fixP->tc_fix_data.inst != NULL)
7821 free (fixP->tc_fix_data.inst);
7822
7823 return;
7824}
7825
7826/* Translate internal representation of relocation info to BFD target
7827 format. */
7828
7829arelent *
7830tc_gen_reloc (asection * section, fixS * fixp)
7831{
7832 arelent *reloc;
7833 bfd_reloc_code_real_type code;
7834
325801bd 7835 reloc = XNEW (arelent);
a06ea964 7836
325801bd 7837 reloc->sym_ptr_ptr = XNEW (asymbol *);
a06ea964
NC
7838 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
7839 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7840
7841 if (fixp->fx_pcrel)
7842 {
7843 if (section->use_rela_p)
7844 fixp->fx_offset -= md_pcrel_from_section (fixp, section);
7845 else
7846 fixp->fx_offset = reloc->address;
7847 }
7848 reloc->addend = fixp->fx_offset;
7849
7850 code = fixp->fx_r_type;
7851 switch (code)
7852 {
7853 case BFD_RELOC_16:
7854 if (fixp->fx_pcrel)
7855 code = BFD_RELOC_16_PCREL;
7856 break;
7857
7858 case BFD_RELOC_32:
7859 if (fixp->fx_pcrel)
7860 code = BFD_RELOC_32_PCREL;
7861 break;
7862
7863 case BFD_RELOC_64:
7864 if (fixp->fx_pcrel)
7865 code = BFD_RELOC_64_PCREL;
7866 break;
7867
7868 default:
7869 break;
7870 }
7871
7872 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
7873 if (reloc->howto == NULL)
7874 {
7875 as_bad_where (fixp->fx_file, fixp->fx_line,
7876 _
7877 ("cannot represent %s relocation in this object file format"),
7878 bfd_get_reloc_code_name (code));
7879 return NULL;
7880 }
7881
7882 return reloc;
7883}
7884
7885/* This fix_new is called by cons via TC_CONS_FIX_NEW. */
7886
7887void
7888cons_fix_new_aarch64 (fragS * frag, int where, int size, expressionS * exp)
7889{
7890 bfd_reloc_code_real_type type;
7891 int pcrel = 0;
7892
7893 /* Pick a reloc.
7894 FIXME: @@ Should look at CPU word size. */
7895 switch (size)
7896 {
7897 case 1:
7898 type = BFD_RELOC_8;
7899 break;
7900 case 2:
7901 type = BFD_RELOC_16;
7902 break;
7903 case 4:
7904 type = BFD_RELOC_32;
7905 break;
7906 case 8:
7907 type = BFD_RELOC_64;
7908 break;
7909 default:
7910 as_bad (_("cannot do %u-byte relocation"), size);
7911 type = BFD_RELOC_UNUSED;
7912 break;
7913 }
7914
7915 fix_new_exp (frag, where, (int) size, exp, pcrel, type);
7916}
7917
7918int
7919aarch64_force_relocation (struct fix *fixp)
7920{
7921 switch (fixp->fx_r_type)
7922 {
7923 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
7924 /* Perform these "immediate" internal relocations
7925 even if the symbol is extern or weak. */
7926 return 0;
7927
a6bb11b2 7928 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
f09c556a
JW
7929 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
7930 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
a6bb11b2
YZ
7931 /* Pseudo relocs that need to be fixed up according to
7932 ilp32_p. */
7933 return 0;
7934
2c0a3565
MS
7935 case BFD_RELOC_AARCH64_ADD_LO12:
7936 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7937 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7938 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7939 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7940 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3d715ce4 7941 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
87f5fbcc 7942 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
a921b5bd 7943 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
2c0a3565
MS
7944 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7945 case BFD_RELOC_AARCH64_LDST128_LO12:
7946 case BFD_RELOC_AARCH64_LDST16_LO12:
7947 case BFD_RELOC_AARCH64_LDST32_LO12:
7948 case BFD_RELOC_AARCH64_LDST64_LO12:
7949 case BFD_RELOC_AARCH64_LDST8_LO12:
f955cccf 7950 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12:
2c0a3565 7951 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 7952 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
2c0a3565 7953 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
f955cccf 7954 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12:
1ada945d 7955 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
43a357f9
RL
7956 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7957 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964 7958 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
2c0a3565 7959 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 7960 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
3e8286c0 7961 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
1aa66fb1 7962 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 7963 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 7964 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
2c0a3565 7965 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 7966 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
7967 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7968 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7969 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
70151fb5 7970 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
13289c10 7971 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
a12fad50 7972 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
1107e076 7973 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6c37fedc 7974 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4c562523
JW
7975 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7976 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7977 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7978 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7979 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7980 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7981 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7982 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
49df5539
JW
7983 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7984 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7985 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7986 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7987 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964 7988 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
2c0a3565 7989 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
a06ea964 7990 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
a06ea964
NC
7991 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7992 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
2c0a3565
MS
7993 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7994 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7995 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
a06ea964
NC
7996 /* Always leave these relocations for the linker. */
7997 return 1;
7998
7999 default:
8000 break;
8001 }
8002
8003 return generic_force_reloc (fixp);
8004}
8005
8006#ifdef OBJ_ELF
8007
3c0367d0
JW
8008/* Implement md_after_parse_args. This is the earliest time we need to decide
8009 ABI. If no -mabi specified, the ABI will be decided by target triplet. */
8010
8011void
8012aarch64_after_parse_args (void)
8013{
8014 if (aarch64_abi != AARCH64_ABI_NONE)
8015 return;
8016
8017 /* DEFAULT_ARCH will have ":32" extension if it's configured for ILP32. */
8018 if (strlen (default_arch) > 7 && strcmp (default_arch + 7, ":32") == 0)
8019 aarch64_abi = AARCH64_ABI_ILP32;
8020 else
8021 aarch64_abi = AARCH64_ABI_LP64;
8022}
8023
a06ea964
NC
8024const char *
8025elf64_aarch64_target_format (void)
8026{
a75cf613
ES
8027 if (strcmp (TARGET_OS, "cloudabi") == 0)
8028 {
8029 /* FIXME: What to do for ilp32_p ? */
8030 return target_big_endian ? "elf64-bigaarch64-cloudabi" : "elf64-littleaarch64-cloudabi";
8031 }
a06ea964 8032 if (target_big_endian)
cec5225b 8033 return ilp32_p ? "elf32-bigaarch64" : "elf64-bigaarch64";
a06ea964 8034 else
cec5225b 8035 return ilp32_p ? "elf32-littleaarch64" : "elf64-littleaarch64";
a06ea964
NC
8036}
8037
8038void
8039aarch64elf_frob_symbol (symbolS * symp, int *puntp)
8040{
8041 elf_frob_symbol (symp, puntp);
8042}
8043#endif
8044
8045/* MD interface: Finalization. */
8046
8047/* A good place to do this, although this was probably not intended
8048 for this kind of use. We need to dump the literal pool before
8049 references are made to a null symbol pointer. */
8050
8051void
8052aarch64_cleanup (void)
8053{
8054 literal_pool *pool;
8055
8056 for (pool = list_of_pools; pool; pool = pool->next)
8057 {
8058 /* Put it at the end of the relevant section. */
8059 subseg_set (pool->section, pool->sub_section);
8060 s_ltorg (0);
8061 }
8062}
8063
8064#ifdef OBJ_ELF
8065/* Remove any excess mapping symbols generated for alignment frags in
8066 SEC. We may have created a mapping symbol before a zero byte
8067 alignment; remove it if there's a mapping symbol after the
8068 alignment. */
8069static void
8070check_mapping_symbols (bfd * abfd ATTRIBUTE_UNUSED, asection * sec,
8071 void *dummy ATTRIBUTE_UNUSED)
8072{
8073 segment_info_type *seginfo = seg_info (sec);
8074 fragS *fragp;
8075
8076 if (seginfo == NULL || seginfo->frchainP == NULL)
8077 return;
8078
8079 for (fragp = seginfo->frchainP->frch_root;
8080 fragp != NULL; fragp = fragp->fr_next)
8081 {
8082 symbolS *sym = fragp->tc_frag_data.last_map;
8083 fragS *next = fragp->fr_next;
8084
8085 /* Variable-sized frags have been converted to fixed size by
8086 this point. But if this was variable-sized to start with,
8087 there will be a fixed-size frag after it. So don't handle
8088 next == NULL. */
8089 if (sym == NULL || next == NULL)
8090 continue;
8091
8092 if (S_GET_VALUE (sym) < next->fr_address)
8093 /* Not at the end of this frag. */
8094 continue;
8095 know (S_GET_VALUE (sym) == next->fr_address);
8096
8097 do
8098 {
8099 if (next->tc_frag_data.first_map != NULL)
8100 {
8101 /* Next frag starts with a mapping symbol. Discard this
8102 one. */
8103 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
8104 break;
8105 }
8106
8107 if (next->fr_next == NULL)
8108 {
8109 /* This mapping symbol is at the end of the section. Discard
8110 it. */
8111 know (next->fr_fix == 0 && next->fr_var == 0);
8112 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
8113 break;
8114 }
8115
8116 /* As long as we have empty frags without any mapping symbols,
8117 keep looking. */
8118 /* If the next frag is non-empty and does not start with a
8119 mapping symbol, then this mapping symbol is required. */
8120 if (next->fr_address != next->fr_next->fr_address)
8121 break;
8122
8123 next = next->fr_next;
8124 }
8125 while (next != NULL);
8126 }
8127}
8128#endif
8129
8130/* Adjust the symbol table. */
8131
8132void
8133aarch64_adjust_symtab (void)
8134{
8135#ifdef OBJ_ELF
8136 /* Remove any overlapping mapping symbols generated by alignment frags. */
8137 bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
8138 /* Now do generic ELF adjustments. */
8139 elf_adjust_symtab ();
8140#endif
8141}
8142
8143static void
8144checked_hash_insert (struct hash_control *table, const char *key, void *value)
8145{
8146 const char *hash_err;
8147
8148 hash_err = hash_insert (table, key, value);
8149 if (hash_err)
8150 printf ("Internal Error: Can't hash %s\n", key);
8151}
8152
8153static void
8154fill_instruction_hash_table (void)
8155{
8156 aarch64_opcode *opcode = aarch64_opcode_table;
8157
8158 while (opcode->name != NULL)
8159 {
8160 templates *templ, *new_templ;
8161 templ = hash_find (aarch64_ops_hsh, opcode->name);
8162
add39d23 8163 new_templ = XNEW (templates);
a06ea964
NC
8164 new_templ->opcode = opcode;
8165 new_templ->next = NULL;
8166
8167 if (!templ)
8168 checked_hash_insert (aarch64_ops_hsh, opcode->name, (void *) new_templ);
8169 else
8170 {
8171 new_templ->next = templ->next;
8172 templ->next = new_templ;
8173 }
8174 ++opcode;
8175 }
8176}
8177
8178static inline void
8179convert_to_upper (char *dst, const char *src, size_t num)
8180{
8181 unsigned int i;
8182 for (i = 0; i < num && *src != '\0'; ++i, ++dst, ++src)
8183 *dst = TOUPPER (*src);
8184 *dst = '\0';
8185}
8186
8187/* Assume STR point to a lower-case string, allocate, convert and return
8188 the corresponding upper-case string. */
8189static inline const char*
8190get_upper_str (const char *str)
8191{
8192 char *ret;
8193 size_t len = strlen (str);
325801bd 8194 ret = XNEWVEC (char, len + 1);
a06ea964
NC
8195 convert_to_upper (ret, str, len);
8196 return ret;
8197}
8198
8199/* MD interface: Initialization. */
8200
8201void
8202md_begin (void)
8203{
8204 unsigned mach;
8205 unsigned int i;
8206
8207 if ((aarch64_ops_hsh = hash_new ()) == NULL
8208 || (aarch64_cond_hsh = hash_new ()) == NULL
8209 || (aarch64_shift_hsh = hash_new ()) == NULL
8210 || (aarch64_sys_regs_hsh = hash_new ()) == NULL
8211 || (aarch64_pstatefield_hsh = hash_new ()) == NULL
8212 || (aarch64_sys_regs_ic_hsh = hash_new ()) == NULL
8213 || (aarch64_sys_regs_dc_hsh = hash_new ()) == NULL
8214 || (aarch64_sys_regs_at_hsh = hash_new ()) == NULL
8215 || (aarch64_sys_regs_tlbi_hsh = hash_new ()) == NULL
8216 || (aarch64_reg_hsh = hash_new ()) == NULL
8217 || (aarch64_barrier_opt_hsh = hash_new ()) == NULL
8218 || (aarch64_nzcv_hsh = hash_new ()) == NULL
1e6f4800
MW
8219 || (aarch64_pldop_hsh = hash_new ()) == NULL
8220 || (aarch64_hint_opt_hsh = hash_new ()) == NULL)
a06ea964
NC
8221 as_fatal (_("virtual memory exhausted"));
8222
8223 fill_instruction_hash_table ();
8224
8225 for (i = 0; aarch64_sys_regs[i].name != NULL; ++i)
8226 checked_hash_insert (aarch64_sys_regs_hsh, aarch64_sys_regs[i].name,
8227 (void *) (aarch64_sys_regs + i));
8228
8229 for (i = 0; aarch64_pstatefields[i].name != NULL; ++i)
8230 checked_hash_insert (aarch64_pstatefield_hsh,
8231 aarch64_pstatefields[i].name,
8232 (void *) (aarch64_pstatefields + i));
8233
875880c6 8234 for (i = 0; aarch64_sys_regs_ic[i].name != NULL; i++)
a06ea964 8235 checked_hash_insert (aarch64_sys_regs_ic_hsh,
875880c6 8236 aarch64_sys_regs_ic[i].name,
a06ea964
NC
8237 (void *) (aarch64_sys_regs_ic + i));
8238
875880c6 8239 for (i = 0; aarch64_sys_regs_dc[i].name != NULL; i++)
a06ea964 8240 checked_hash_insert (aarch64_sys_regs_dc_hsh,
875880c6 8241 aarch64_sys_regs_dc[i].name,
a06ea964
NC
8242 (void *) (aarch64_sys_regs_dc + i));
8243
875880c6 8244 for (i = 0; aarch64_sys_regs_at[i].name != NULL; i++)
a06ea964 8245 checked_hash_insert (aarch64_sys_regs_at_hsh,
875880c6 8246 aarch64_sys_regs_at[i].name,
a06ea964
NC
8247 (void *) (aarch64_sys_regs_at + i));
8248
875880c6 8249 for (i = 0; aarch64_sys_regs_tlbi[i].name != NULL; i++)
a06ea964 8250 checked_hash_insert (aarch64_sys_regs_tlbi_hsh,
875880c6 8251 aarch64_sys_regs_tlbi[i].name,
a06ea964
NC
8252 (void *) (aarch64_sys_regs_tlbi + i));
8253
8254 for (i = 0; i < ARRAY_SIZE (reg_names); i++)
8255 checked_hash_insert (aarch64_reg_hsh, reg_names[i].name,
8256 (void *) (reg_names + i));
8257
8258 for (i = 0; i < ARRAY_SIZE (nzcv_names); i++)
8259 checked_hash_insert (aarch64_nzcv_hsh, nzcv_names[i].template,
8260 (void *) (nzcv_names + i));
8261
8262 for (i = 0; aarch64_operand_modifiers[i].name != NULL; i++)
8263 {
8264 const char *name = aarch64_operand_modifiers[i].name;
8265 checked_hash_insert (aarch64_shift_hsh, name,
8266 (void *) (aarch64_operand_modifiers + i));
8267 /* Also hash the name in the upper case. */
8268 checked_hash_insert (aarch64_shift_hsh, get_upper_str (name),
8269 (void *) (aarch64_operand_modifiers + i));
8270 }
8271
8272 for (i = 0; i < ARRAY_SIZE (aarch64_conds); i++)
8273 {
8274 unsigned int j;
8275 /* A condition code may have alias(es), e.g. "cc", "lo" and "ul" are
8276 the same condition code. */
8277 for (j = 0; j < ARRAY_SIZE (aarch64_conds[i].names); ++j)
8278 {
8279 const char *name = aarch64_conds[i].names[j];
8280 if (name == NULL)
8281 break;
8282 checked_hash_insert (aarch64_cond_hsh, name,
8283 (void *) (aarch64_conds + i));
8284 /* Also hash the name in the upper case. */
8285 checked_hash_insert (aarch64_cond_hsh, get_upper_str (name),
8286 (void *) (aarch64_conds + i));
8287 }
8288 }
8289
8290 for (i = 0; i < ARRAY_SIZE (aarch64_barrier_options); i++)
8291 {
8292 const char *name = aarch64_barrier_options[i].name;
8293 /* Skip xx00 - the unallocated values of option. */
8294 if ((i & 0x3) == 0)
8295 continue;
8296 checked_hash_insert (aarch64_barrier_opt_hsh, name,
8297 (void *) (aarch64_barrier_options + i));
8298 /* Also hash the name in the upper case. */
8299 checked_hash_insert (aarch64_barrier_opt_hsh, get_upper_str (name),
8300 (void *) (aarch64_barrier_options + i));
8301 }
8302
8303 for (i = 0; i < ARRAY_SIZE (aarch64_prfops); i++)
8304 {
8305 const char* name = aarch64_prfops[i].name;
a1ccaec9
YZ
8306 /* Skip the unallocated hint encodings. */
8307 if (name == NULL)
a06ea964
NC
8308 continue;
8309 checked_hash_insert (aarch64_pldop_hsh, name,
8310 (void *) (aarch64_prfops + i));
8311 /* Also hash the name in the upper case. */
8312 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
8313 (void *) (aarch64_prfops + i));
8314 }
8315
1e6f4800
MW
8316 for (i = 0; aarch64_hint_options[i].name != NULL; i++)
8317 {
8318 const char* name = aarch64_hint_options[i].name;
8319
8320 checked_hash_insert (aarch64_hint_opt_hsh, name,
8321 (void *) (aarch64_hint_options + i));
8322 /* Also hash the name in the upper case. */
8323 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
8324 (void *) (aarch64_hint_options + i));
8325 }
8326
a06ea964
NC
8327 /* Set the cpu variant based on the command-line options. */
8328 if (!mcpu_cpu_opt)
8329 mcpu_cpu_opt = march_cpu_opt;
8330
8331 if (!mcpu_cpu_opt)
8332 mcpu_cpu_opt = &cpu_default;
8333
8334 cpu_variant = *mcpu_cpu_opt;
8335
8336 /* Record the CPU type. */
cec5225b 8337 mach = ilp32_p ? bfd_mach_aarch64_ilp32 : bfd_mach_aarch64;
a06ea964
NC
8338
8339 bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
8340}
8341
8342/* Command line processing. */
8343
8344const char *md_shortopts = "m:";
8345
8346#ifdef AARCH64_BI_ENDIAN
8347#define OPTION_EB (OPTION_MD_BASE + 0)
8348#define OPTION_EL (OPTION_MD_BASE + 1)
8349#else
8350#if TARGET_BYTES_BIG_ENDIAN
8351#define OPTION_EB (OPTION_MD_BASE + 0)
8352#else
8353#define OPTION_EL (OPTION_MD_BASE + 1)
8354#endif
8355#endif
8356
8357struct option md_longopts[] = {
8358#ifdef OPTION_EB
8359 {"EB", no_argument, NULL, OPTION_EB},
8360#endif
8361#ifdef OPTION_EL
8362 {"EL", no_argument, NULL, OPTION_EL},
8363#endif
8364 {NULL, no_argument, NULL, 0}
8365};
8366
8367size_t md_longopts_size = sizeof (md_longopts);
8368
8369struct aarch64_option_table
8370{
e0471c16
TS
8371 const char *option; /* Option name to match. */
8372 const char *help; /* Help information. */
a06ea964
NC
8373 int *var; /* Variable to change. */
8374 int value; /* What to change it to. */
8375 char *deprecated; /* If non-null, print this message. */
8376};
8377
8378static struct aarch64_option_table aarch64_opts[] = {
8379 {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
8380 {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
8381 NULL},
8382#ifdef DEBUG_AARCH64
8383 {"mdebug-dump", N_("temporary switch for dumping"), &debug_dump, 1, NULL},
8384#endif /* DEBUG_AARCH64 */
8385 {"mverbose-error", N_("output verbose error messages"), &verbose_error_p, 1,
8386 NULL},
a52e6fd3
YZ
8387 {"mno-verbose-error", N_("do not output verbose error messages"),
8388 &verbose_error_p, 0, NULL},
a06ea964
NC
8389 {NULL, NULL, NULL, 0, NULL}
8390};
8391
8392struct aarch64_cpu_option_table
8393{
e0471c16 8394 const char *name;
a06ea964
NC
8395 const aarch64_feature_set value;
8396 /* The canonical name of the CPU, or NULL to use NAME converted to upper
8397 case. */
8398 const char *canonical_name;
8399};
8400
8401/* This list should, at a minimum, contain all the cpu names
8402 recognized by GCC. */
8403static const struct aarch64_cpu_option_table aarch64_cpus[] = {
8404 {"all", AARCH64_ANY, NULL},
9c352f1c
JG
8405 {"cortex-a35", AARCH64_FEATURE (AARCH64_ARCH_V8,
8406 AARCH64_FEATURE_CRC), "Cortex-A35"},
aa31c464
JW
8407 {"cortex-a53", AARCH64_FEATURE (AARCH64_ARCH_V8,
8408 AARCH64_FEATURE_CRC), "Cortex-A53"},
8409 {"cortex-a57", AARCH64_FEATURE (AARCH64_ARCH_V8,
8410 AARCH64_FEATURE_CRC), "Cortex-A57"},
2abdd192
JW
8411 {"cortex-a72", AARCH64_FEATURE (AARCH64_ARCH_V8,
8412 AARCH64_FEATURE_CRC), "Cortex-A72"},
1aa70332
KT
8413 {"cortex-a73", AARCH64_FEATURE (AARCH64_ARCH_V8,
8414 AARCH64_FEATURE_CRC), "Cortex-A73"},
1e292627 8415 {"cortex-a55", AARCH64_FEATURE (AARCH64_ARCH_V8_2,
1c5c938a 8416 AARCH64_FEATURE_RCPC | AARCH64_FEATURE_F16 | AARCH64_FEATURE_DOTPROD),
1e292627
JG
8417 "Cortex-A55"},
8418 {"cortex-a75", AARCH64_FEATURE (AARCH64_ARCH_V8_2,
1c5c938a 8419 AARCH64_FEATURE_RCPC | AARCH64_FEATURE_F16 | AARCH64_FEATURE_DOTPROD),
1e292627 8420 "Cortex-A75"},
2412d878
EM
8421 {"exynos-m1", AARCH64_FEATURE (AARCH64_ARCH_V8,
8422 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
8423 "Samsung Exynos M1"},
2fe9c2a0 8424 {"falkor", AARCH64_FEATURE (AARCH64_ARCH_V8,
e58ff055
JW
8425 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO
8426 | AARCH64_FEATURE_RDMA),
2fe9c2a0 8427 "Qualcomm Falkor"},
6b21c2bf 8428 {"qdf24xx", AARCH64_FEATURE (AARCH64_ARCH_V8,
e58ff055
JW
8429 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO
8430 | AARCH64_FEATURE_RDMA),
6b21c2bf 8431 "Qualcomm QDF24XX"},
7605d944
SP
8432 {"saphira", AARCH64_FEATURE (AARCH64_ARCH_V8_3,
8433 AARCH64_FEATURE_CRYPTO | AARCH64_FEATURE_PROFILE),
8434 "Qualcomm Saphira"},
faade851
JW
8435 {"thunderx", AARCH64_FEATURE (AARCH64_ARCH_V8,
8436 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
8437 "Cavium ThunderX"},
9f99c22e
VP
8438 {"vulcan", AARCH64_FEATURE (AARCH64_ARCH_V8_1,
8439 AARCH64_FEATURE_CRYPTO),
0a8be2fe 8440 "Broadcom Vulcan"},
070cb956
PT
8441 /* The 'xgene-1' name is an older name for 'xgene1', which was used
8442 in earlier releases and is superseded by 'xgene1' in all
8443 tools. */
9877c63c 8444 {"xgene-1", AARCH64_ARCH_V8, "APM X-Gene 1"},
070cb956 8445 {"xgene1", AARCH64_ARCH_V8, "APM X-Gene 1"},
aa31c464
JW
8446 {"xgene2", AARCH64_FEATURE (AARCH64_ARCH_V8,
8447 AARCH64_FEATURE_CRC), "APM X-Gene 2"},
a06ea964
NC
8448 {"generic", AARCH64_ARCH_V8, NULL},
8449
a06ea964
NC
8450 {NULL, AARCH64_ARCH_NONE, NULL}
8451};
8452
8453struct aarch64_arch_option_table
8454{
e0471c16 8455 const char *name;
a06ea964
NC
8456 const aarch64_feature_set value;
8457};
8458
8459/* This list should, at a minimum, contain all the architecture names
8460 recognized by GCC. */
8461static const struct aarch64_arch_option_table aarch64_archs[] = {
8462 {"all", AARCH64_ANY},
5a1ad39d 8463 {"armv8-a", AARCH64_ARCH_V8},
88f0ea34 8464 {"armv8.1-a", AARCH64_ARCH_V8_1},
acb787b0 8465 {"armv8.2-a", AARCH64_ARCH_V8_2},
1924ff75 8466 {"armv8.3-a", AARCH64_ARCH_V8_3},
b6b9ca0c 8467 {"armv8.4-a", AARCH64_ARCH_V8_4},
a06ea964
NC
8468 {NULL, AARCH64_ARCH_NONE}
8469};
8470
8471/* ISA extensions. */
8472struct aarch64_option_cpu_value_table
8473{
e0471c16 8474 const char *name;
a06ea964 8475 const aarch64_feature_set value;
93d8990c 8476 const aarch64_feature_set require; /* Feature dependencies. */
a06ea964
NC
8477};
8478
8479static const struct aarch64_option_cpu_value_table aarch64_features[] = {
93d8990c
SN
8480 {"crc", AARCH64_FEATURE (AARCH64_FEATURE_CRC, 0),
8481 AARCH64_ARCH_NONE},
c0e7cef7
NC
8482 {"crypto", AARCH64_FEATURE (AARCH64_FEATURE_CRYPTO
8483 | AARCH64_FEATURE_AES
8484 | AARCH64_FEATURE_SHA2, 0),
fa09f4ea 8485 AARCH64_FEATURE (AARCH64_FEATURE_SIMD, 0)},
93d8990c
SN
8486 {"fp", AARCH64_FEATURE (AARCH64_FEATURE_FP, 0),
8487 AARCH64_ARCH_NONE},
8488 {"lse", AARCH64_FEATURE (AARCH64_FEATURE_LSE, 0),
8489 AARCH64_ARCH_NONE},
8490 {"simd", AARCH64_FEATURE (AARCH64_FEATURE_SIMD, 0),
fa09f4ea 8491 AARCH64_FEATURE (AARCH64_FEATURE_FP, 0)},
93d8990c
SN
8492 {"pan", AARCH64_FEATURE (AARCH64_FEATURE_PAN, 0),
8493 AARCH64_ARCH_NONE},
8494 {"lor", AARCH64_FEATURE (AARCH64_FEATURE_LOR, 0),
8495 AARCH64_ARCH_NONE},
8496 {"ras", AARCH64_FEATURE (AARCH64_FEATURE_RAS, 0),
8497 AARCH64_ARCH_NONE},
8498 {"rdma", AARCH64_FEATURE (AARCH64_FEATURE_RDMA, 0),
8499 AARCH64_FEATURE (AARCH64_FEATURE_SIMD, 0)},
8500 {"fp16", AARCH64_FEATURE (AARCH64_FEATURE_F16, 0),
8501 AARCH64_FEATURE (AARCH64_FEATURE_FP, 0)},
d0f7791c
TC
8502 {"fp16fml", AARCH64_FEATURE (AARCH64_FEATURE_F16_FML, 0),
8503 AARCH64_FEATURE (AARCH64_FEATURE_FP
8504 | AARCH64_FEATURE_F16, 0)},
93d8990c
SN
8505 {"profile", AARCH64_FEATURE (AARCH64_FEATURE_PROFILE, 0),
8506 AARCH64_ARCH_NONE},
c0890d26 8507 {"sve", AARCH64_FEATURE (AARCH64_FEATURE_SVE, 0),
582e12bf
RS
8508 AARCH64_FEATURE (AARCH64_FEATURE_F16
8509 | AARCH64_FEATURE_SIMD
8510 | AARCH64_FEATURE_COMPNUM, 0)},
f482d304
RS
8511 {"compnum", AARCH64_FEATURE (AARCH64_FEATURE_COMPNUM, 0),
8512 AARCH64_FEATURE (AARCH64_FEATURE_F16
8513 | AARCH64_FEATURE_SIMD, 0)},
d74d4880
SN
8514 {"rcpc", AARCH64_FEATURE (AARCH64_FEATURE_RCPC, 0),
8515 AARCH64_ARCH_NONE},
65a55fbb
TC
8516 {"dotprod", AARCH64_FEATURE (AARCH64_FEATURE_DOTPROD, 0),
8517 AARCH64_ARCH_NONE},
c0e7cef7
NC
8518 {"sha2", AARCH64_FEATURE (AARCH64_FEATURE_SHA2, 0),
8519 AARCH64_ARCH_NONE},
8520 {"aes", AARCH64_FEATURE (AARCH64_FEATURE_AES, 0),
8521 AARCH64_ARCH_NONE},
b6b9ca0c
TC
8522 {"sm4", AARCH64_FEATURE (AARCH64_FEATURE_SM4, 0),
8523 AARCH64_ARCH_NONE},
8524 {"sha3", AARCH64_FEATURE (AARCH64_FEATURE_SHA2
8525 | AARCH64_FEATURE_SHA3, 0),
8526 AARCH64_ARCH_NONE},
93d8990c 8527 {NULL, AARCH64_ARCH_NONE, AARCH64_ARCH_NONE},
a06ea964
NC
8528};
8529
8530struct aarch64_long_option_table
8531{
e0471c16
TS
8532 const char *option; /* Substring to match. */
8533 const char *help; /* Help information. */
17b9d67d 8534 int (*func) (const char *subopt); /* Function to decode sub-option. */
a06ea964
NC
8535 char *deprecated; /* If non-null, print this message. */
8536};
8537
93d8990c
SN
8538/* Transitive closure of features depending on set. */
8539static aarch64_feature_set
8540aarch64_feature_disable_set (aarch64_feature_set set)
8541{
8542 const struct aarch64_option_cpu_value_table *opt;
8543 aarch64_feature_set prev = 0;
8544
8545 while (prev != set) {
8546 prev = set;
8547 for (opt = aarch64_features; opt->name != NULL; opt++)
8548 if (AARCH64_CPU_HAS_ANY_FEATURES (opt->require, set))
8549 AARCH64_MERGE_FEATURE_SETS (set, set, opt->value);
8550 }
8551 return set;
8552}
8553
8554/* Transitive closure of dependencies of set. */
8555static aarch64_feature_set
8556aarch64_feature_enable_set (aarch64_feature_set set)
8557{
8558 const struct aarch64_option_cpu_value_table *opt;
8559 aarch64_feature_set prev = 0;
8560
8561 while (prev != set) {
8562 prev = set;
8563 for (opt = aarch64_features; opt->name != NULL; opt++)
8564 if (AARCH64_CPU_HAS_FEATURE (set, opt->value))
8565 AARCH64_MERGE_FEATURE_SETS (set, set, opt->require);
8566 }
8567 return set;
8568}
8569
a06ea964 8570static int
82b8a785 8571aarch64_parse_features (const char *str, const aarch64_feature_set **opt_p,
ae527cd8 8572 bfd_boolean ext_only)
a06ea964
NC
8573{
8574 /* We insist on extensions being added before being removed. We achieve
8575 this by using the ADDING_VALUE variable to indicate whether we are
8576 adding an extension (1) or removing it (0) and only allowing it to
8577 change in the order -1 -> 1 -> 0. */
8578 int adding_value = -1;
325801bd 8579 aarch64_feature_set *ext_set = XNEW (aarch64_feature_set);
a06ea964
NC
8580
8581 /* Copy the feature set, so that we can modify it. */
8582 *ext_set = **opt_p;
8583 *opt_p = ext_set;
8584
8585 while (str != NULL && *str != 0)
8586 {
8587 const struct aarch64_option_cpu_value_table *opt;
82b8a785 8588 const char *ext = NULL;
a06ea964
NC
8589 int optlen;
8590
ae527cd8 8591 if (!ext_only)
a06ea964 8592 {
ae527cd8
JB
8593 if (*str != '+')
8594 {
8595 as_bad (_("invalid architectural extension"));
8596 return 0;
8597 }
a06ea964 8598
ae527cd8
JB
8599 ext = strchr (++str, '+');
8600 }
a06ea964
NC
8601
8602 if (ext != NULL)
8603 optlen = ext - str;
8604 else
8605 optlen = strlen (str);
8606
8607 if (optlen >= 2 && strncmp (str, "no", 2) == 0)
8608 {
8609 if (adding_value != 0)
8610 adding_value = 0;
8611 optlen -= 2;
8612 str += 2;
8613 }
8614 else if (optlen > 0)
8615 {
8616 if (adding_value == -1)
8617 adding_value = 1;
8618 else if (adding_value != 1)
8619 {
8620 as_bad (_("must specify extensions to add before specifying "
8621 "those to remove"));
8622 return FALSE;
8623 }
8624 }
8625
8626 if (optlen == 0)
8627 {
8628 as_bad (_("missing architectural extension"));
8629 return 0;
8630 }
8631
8632 gas_assert (adding_value != -1);
8633
8634 for (opt = aarch64_features; opt->name != NULL; opt++)
8635 if (strncmp (opt->name, str, optlen) == 0)
8636 {
93d8990c
SN
8637 aarch64_feature_set set;
8638
a06ea964
NC
8639 /* Add or remove the extension. */
8640 if (adding_value)
93d8990c
SN
8641 {
8642 set = aarch64_feature_enable_set (opt->value);
8643 AARCH64_MERGE_FEATURE_SETS (*ext_set, *ext_set, set);
8644 }
a06ea964 8645 else
93d8990c
SN
8646 {
8647 set = aarch64_feature_disable_set (opt->value);
8648 AARCH64_CLEAR_FEATURE (*ext_set, *ext_set, set);
8649 }
a06ea964
NC
8650 break;
8651 }
8652
8653 if (opt->name == NULL)
8654 {
8655 as_bad (_("unknown architectural extension `%s'"), str);
8656 return 0;
8657 }
8658
8659 str = ext;
8660 };
8661
8662 return 1;
8663}
8664
8665static int
17b9d67d 8666aarch64_parse_cpu (const char *str)
a06ea964
NC
8667{
8668 const struct aarch64_cpu_option_table *opt;
82b8a785 8669 const char *ext = strchr (str, '+');
a06ea964
NC
8670 size_t optlen;
8671
8672 if (ext != NULL)
8673 optlen = ext - str;
8674 else
8675 optlen = strlen (str);
8676
8677 if (optlen == 0)
8678 {
8679 as_bad (_("missing cpu name `%s'"), str);
8680 return 0;
8681 }
8682
8683 for (opt = aarch64_cpus; opt->name != NULL; opt++)
8684 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
8685 {
8686 mcpu_cpu_opt = &opt->value;
8687 if (ext != NULL)
ae527cd8 8688 return aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE);
a06ea964
NC
8689
8690 return 1;
8691 }
8692
8693 as_bad (_("unknown cpu `%s'"), str);
8694 return 0;
8695}
8696
8697static int
17b9d67d 8698aarch64_parse_arch (const char *str)
a06ea964
NC
8699{
8700 const struct aarch64_arch_option_table *opt;
82b8a785 8701 const char *ext = strchr (str, '+');
a06ea964
NC
8702 size_t optlen;
8703
8704 if (ext != NULL)
8705 optlen = ext - str;
8706 else
8707 optlen = strlen (str);
8708
8709 if (optlen == 0)
8710 {
8711 as_bad (_("missing architecture name `%s'"), str);
8712 return 0;
8713 }
8714
8715 for (opt = aarch64_archs; opt->name != NULL; opt++)
8716 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
8717 {
8718 march_cpu_opt = &opt->value;
8719 if (ext != NULL)
ae527cd8 8720 return aarch64_parse_features (ext, &march_cpu_opt, FALSE);
a06ea964
NC
8721
8722 return 1;
8723 }
8724
8725 as_bad (_("unknown architecture `%s'\n"), str);
8726 return 0;
8727}
8728
69091a2c
YZ
8729/* ABIs. */
8730struct aarch64_option_abi_value_table
8731{
e0471c16 8732 const char *name;
69091a2c
YZ
8733 enum aarch64_abi_type value;
8734};
8735
8736static const struct aarch64_option_abi_value_table aarch64_abis[] = {
8737 {"ilp32", AARCH64_ABI_ILP32},
8738 {"lp64", AARCH64_ABI_LP64},
69091a2c
YZ
8739};
8740
8741static int
17b9d67d 8742aarch64_parse_abi (const char *str)
69091a2c 8743{
5703197e 8744 unsigned int i;
69091a2c 8745
5703197e 8746 if (str[0] == '\0')
69091a2c
YZ
8747 {
8748 as_bad (_("missing abi name `%s'"), str);
8749 return 0;
8750 }
8751
5703197e
TS
8752 for (i = 0; i < ARRAY_SIZE (aarch64_abis); i++)
8753 if (strcmp (str, aarch64_abis[i].name) == 0)
69091a2c 8754 {
5703197e 8755 aarch64_abi = aarch64_abis[i].value;
69091a2c
YZ
8756 return 1;
8757 }
8758
8759 as_bad (_("unknown abi `%s'\n"), str);
8760 return 0;
8761}
8762
a06ea964 8763static struct aarch64_long_option_table aarch64_long_opts[] = {
69091a2c
YZ
8764#ifdef OBJ_ELF
8765 {"mabi=", N_("<abi name>\t specify for ABI <abi name>"),
8766 aarch64_parse_abi, NULL},
8767#endif /* OBJ_ELF */
a06ea964
NC
8768 {"mcpu=", N_("<cpu name>\t assemble for CPU <cpu name>"),
8769 aarch64_parse_cpu, NULL},
8770 {"march=", N_("<arch name>\t assemble for architecture <arch name>"),
8771 aarch64_parse_arch, NULL},
8772 {NULL, NULL, 0, NULL}
8773};
8774
8775int
17b9d67d 8776md_parse_option (int c, const char *arg)
a06ea964
NC
8777{
8778 struct aarch64_option_table *opt;
8779 struct aarch64_long_option_table *lopt;
8780
8781 switch (c)
8782 {
8783#ifdef OPTION_EB
8784 case OPTION_EB:
8785 target_big_endian = 1;
8786 break;
8787#endif
8788
8789#ifdef OPTION_EL
8790 case OPTION_EL:
8791 target_big_endian = 0;
8792 break;
8793#endif
8794
8795 case 'a':
8796 /* Listing option. Just ignore these, we don't support additional
8797 ones. */
8798 return 0;
8799
8800 default:
8801 for (opt = aarch64_opts; opt->option != NULL; opt++)
8802 {
8803 if (c == opt->option[0]
8804 && ((arg == NULL && opt->option[1] == 0)
8805 || streq (arg, opt->option + 1)))
8806 {
8807 /* If the option is deprecated, tell the user. */
8808 if (opt->deprecated != NULL)
8809 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
8810 arg ? arg : "", _(opt->deprecated));
8811
8812 if (opt->var != NULL)
8813 *opt->var = opt->value;
8814
8815 return 1;
8816 }
8817 }
8818
8819 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
8820 {
8821 /* These options are expected to have an argument. */
8822 if (c == lopt->option[0]
8823 && arg != NULL
8824 && strncmp (arg, lopt->option + 1,
8825 strlen (lopt->option + 1)) == 0)
8826 {
8827 /* If the option is deprecated, tell the user. */
8828 if (lopt->deprecated != NULL)
8829 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
8830 _(lopt->deprecated));
8831
8832 /* Call the sup-option parser. */
8833 return lopt->func (arg + strlen (lopt->option) - 1);
8834 }
8835 }
8836
8837 return 0;
8838 }
8839
8840 return 1;
8841}
8842
8843void
8844md_show_usage (FILE * fp)
8845{
8846 struct aarch64_option_table *opt;
8847 struct aarch64_long_option_table *lopt;
8848
8849 fprintf (fp, _(" AArch64-specific assembler options:\n"));
8850
8851 for (opt = aarch64_opts; opt->option != NULL; opt++)
8852 if (opt->help != NULL)
8853 fprintf (fp, " -%-23s%s\n", opt->option, _(opt->help));
8854
8855 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
8856 if (lopt->help != NULL)
8857 fprintf (fp, " -%s%s\n", lopt->option, _(lopt->help));
8858
8859#ifdef OPTION_EB
8860 fprintf (fp, _("\
8861 -EB assemble code for a big-endian cpu\n"));
8862#endif
8863
8864#ifdef OPTION_EL
8865 fprintf (fp, _("\
8866 -EL assemble code for a little-endian cpu\n"));
8867#endif
8868}
8869
8870/* Parse a .cpu directive. */
8871
8872static void
8873s_aarch64_cpu (int ignored ATTRIBUTE_UNUSED)
8874{
8875 const struct aarch64_cpu_option_table *opt;
8876 char saved_char;
8877 char *name;
8878 char *ext;
8879 size_t optlen;
8880
8881 name = input_line_pointer;
8882 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8883 input_line_pointer++;
8884 saved_char = *input_line_pointer;
8885 *input_line_pointer = 0;
8886
8887 ext = strchr (name, '+');
8888
8889 if (ext != NULL)
8890 optlen = ext - name;
8891 else
8892 optlen = strlen (name);
8893
8894 /* Skip the first "all" entry. */
8895 for (opt = aarch64_cpus + 1; opt->name != NULL; opt++)
8896 if (strlen (opt->name) == optlen
8897 && strncmp (name, opt->name, optlen) == 0)
8898 {
8899 mcpu_cpu_opt = &opt->value;
8900 if (ext != NULL)
ae527cd8 8901 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE))
a06ea964
NC
8902 return;
8903
8904 cpu_variant = *mcpu_cpu_opt;
8905
8906 *input_line_pointer = saved_char;
8907 demand_empty_rest_of_line ();
8908 return;
8909 }
8910 as_bad (_("unknown cpu `%s'"), name);
8911 *input_line_pointer = saved_char;
8912 ignore_rest_of_line ();
8913}
8914
8915
8916/* Parse a .arch directive. */
8917
8918static void
8919s_aarch64_arch (int ignored ATTRIBUTE_UNUSED)
8920{
8921 const struct aarch64_arch_option_table *opt;
8922 char saved_char;
8923 char *name;
8924 char *ext;
8925 size_t optlen;
8926
8927 name = input_line_pointer;
8928 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8929 input_line_pointer++;
8930 saved_char = *input_line_pointer;
8931 *input_line_pointer = 0;
8932
8933 ext = strchr (name, '+');
8934
8935 if (ext != NULL)
8936 optlen = ext - name;
8937 else
8938 optlen = strlen (name);
8939
8940 /* Skip the first "all" entry. */
8941 for (opt = aarch64_archs + 1; opt->name != NULL; opt++)
8942 if (strlen (opt->name) == optlen
8943 && strncmp (name, opt->name, optlen) == 0)
8944 {
8945 mcpu_cpu_opt = &opt->value;
8946 if (ext != NULL)
ae527cd8 8947 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE))
a06ea964
NC
8948 return;
8949
8950 cpu_variant = *mcpu_cpu_opt;
8951
8952 *input_line_pointer = saved_char;
8953 demand_empty_rest_of_line ();
8954 return;
8955 }
8956
8957 as_bad (_("unknown architecture `%s'\n"), name);
8958 *input_line_pointer = saved_char;
8959 ignore_rest_of_line ();
8960}
8961
ae527cd8
JB
8962/* Parse a .arch_extension directive. */
8963
8964static void
8965s_aarch64_arch_extension (int ignored ATTRIBUTE_UNUSED)
8966{
8967 char saved_char;
8968 char *ext = input_line_pointer;;
8969
8970 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8971 input_line_pointer++;
8972 saved_char = *input_line_pointer;
8973 *input_line_pointer = 0;
8974
8975 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, TRUE))
8976 return;
8977
8978 cpu_variant = *mcpu_cpu_opt;
8979
8980 *input_line_pointer = saved_char;
8981 demand_empty_rest_of_line ();
8982}
8983
a06ea964
NC
8984/* Copy symbol information. */
8985
8986void
8987aarch64_copy_symbol_attributes (symbolS * dest, symbolS * src)
8988{
8989 AARCH64_GET_FLAG (dest) = AARCH64_GET_FLAG (src);
8990}
This page took 0.989684 seconds and 4 git commands to generate.