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