1 /* tc-aarch64.c -- Assemble for the AArch64 ISA
3 Copyright (C) 2009-2016 Free Software Foundation, Inc.
4 Contributed by ARM Ltd.
6 This file is part of GAS.
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.
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.
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/>. */
25 #include "bfd_stdint.h"
27 #include "safe-ctype.h"
32 #include "elf/aarch64.h"
33 #include "dw2gencfi.h"
36 #include "dwarf2dbg.h"
38 /* Types of processor to assemble for. */
40 #define CPU_DEFAULT AARCH64_ARCH_V8
43 #define streq(a, b) (strcmp (a, b) == 0)
45 #define END_OF_INSN '\0'
47 static aarch64_feature_set cpu_variant;
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
52 static const aarch64_feature_set *mcpu_cpu_opt = NULL;
53 static const aarch64_feature_set *march_cpu_opt = NULL;
55 /* Constants for known architecture features. */
56 static const aarch64_feature_set cpu_default = CPU_DEFAULT;
59 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
60 static symbolS *GOT_symbol;
62 /* Which ABI to use. */
69 /* AArch64 ABI for the output file. */
70 static enum aarch64_abi_type aarch64_abi = AARCH64_ABI_LP64;
72 /* When non-zero, program to a 32-bit model, in which the C data types
73 int, long and all pointer types are 32-bit objects (ILP32); or to a
74 64-bit model, in which the C int type is 32-bits but the C long type
75 and all pointer types are 64-bit objects (LP64). */
76 #define ilp32_p (aarch64_abi == AARCH64_ABI_ILP32)
89 /* Bits for DEFINED field in neon_type_el. */
91 #define NTA_HASINDEX 2
95 enum neon_el_type type;
96 unsigned char defined;
101 #define FIXUP_F_HAS_EXPLICIT_SHIFT 0x00000001
105 bfd_reloc_code_real_type type;
108 enum aarch64_opnd opnd;
110 unsigned need_libopcodes_p : 1;
113 struct aarch64_instruction
115 /* libopcodes structure for instruction intermediate representation. */
117 /* Record assembly errors found during the parsing. */
120 enum aarch64_operand_error_kind kind;
123 /* The condition that appears in the assembly line. */
125 /* Relocation information (including the GAS internal fixup). */
127 /* Need to generate an immediate in the literal pool. */
128 unsigned gen_lit_pool : 1;
131 typedef struct aarch64_instruction aarch64_instruction;
133 static aarch64_instruction inst;
135 static bfd_boolean parse_operands (char *, const aarch64_opcode *);
136 static bfd_boolean programmer_friendly_fixup (aarch64_instruction *);
138 /* Diagnostics inline function utilites.
140 These are lightweight utlities which should only be called by parse_operands
141 and other parsers. GAS processes each assembly line by parsing it against
142 instruction template(s), in the case of multiple templates (for the same
143 mnemonic name), those templates are tried one by one until one succeeds or
144 all fail. An assembly line may fail a few templates before being
145 successfully parsed; an error saved here in most cases is not a user error
146 but an error indicating the current template is not the right template.
147 Therefore it is very important that errors can be saved at a low cost during
148 the parsing; we don't want to slow down the whole parsing by recording
149 non-user errors in detail.
151 Remember that the objective is to help GAS pick up the most approapriate
152 error message in the case of multiple templates, e.g. FMOV which has 8
158 inst.parsing_error.kind = AARCH64_OPDE_NIL;
159 inst.parsing_error.error = NULL;
162 static inline bfd_boolean
165 return inst.parsing_error.kind != AARCH64_OPDE_NIL;
168 static inline const char *
169 get_error_message (void)
171 return inst.parsing_error.error;
174 static inline enum aarch64_operand_error_kind
175 get_error_kind (void)
177 return inst.parsing_error.kind;
181 set_error (enum aarch64_operand_error_kind kind, const char *error)
183 inst.parsing_error.kind = kind;
184 inst.parsing_error.error = error;
188 set_recoverable_error (const char *error)
190 set_error (AARCH64_OPDE_RECOVERABLE, error);
193 /* Use the DESC field of the corresponding aarch64_operand entry to compose
194 the error message. */
196 set_default_error (void)
198 set_error (AARCH64_OPDE_SYNTAX_ERROR, NULL);
202 set_syntax_error (const char *error)
204 set_error (AARCH64_OPDE_SYNTAX_ERROR, error);
208 set_first_syntax_error (const char *error)
211 set_error (AARCH64_OPDE_SYNTAX_ERROR, error);
215 set_fatal_syntax_error (const char *error)
217 set_error (AARCH64_OPDE_FATAL_SYNTAX_ERROR, error);
220 /* Number of littlenums required to hold an extended precision number. */
221 #define MAX_LITTLENUMS 6
223 /* Return value for certain parsers when the parsing fails; those parsers
224 return the information of the parsed result, e.g. register number, on
226 #define PARSE_FAIL -1
228 /* This is an invalid condition code that means no conditional field is
230 #define COND_ALWAYS 0x10
234 const char *template;
240 const char *template;
247 bfd_reloc_code_real_type reloc;
250 /* Structure for a hash table entry for a register. */
254 unsigned char number;
256 unsigned char builtin;
259 /* Macros to define the register types and masks for the purpose
262 #undef AARCH64_REG_TYPES
263 #define AARCH64_REG_TYPES \
264 BASIC_REG_TYPE(R_32) /* w[0-30] */ \
265 BASIC_REG_TYPE(R_64) /* x[0-30] */ \
266 BASIC_REG_TYPE(SP_32) /* wsp */ \
267 BASIC_REG_TYPE(SP_64) /* sp */ \
268 BASIC_REG_TYPE(Z_32) /* wzr */ \
269 BASIC_REG_TYPE(Z_64) /* xzr */ \
270 BASIC_REG_TYPE(FP_B) /* b[0-31] *//* NOTE: keep FP_[BHSDQ] consecutive! */\
271 BASIC_REG_TYPE(FP_H) /* h[0-31] */ \
272 BASIC_REG_TYPE(FP_S) /* s[0-31] */ \
273 BASIC_REG_TYPE(FP_D) /* d[0-31] */ \
274 BASIC_REG_TYPE(FP_Q) /* q[0-31] */ \
275 BASIC_REG_TYPE(CN) /* c[0-7] */ \
276 BASIC_REG_TYPE(VN) /* v[0-31] */ \
277 /* Typecheck: any 64-bit int reg (inc SP exc XZR) */ \
278 MULTI_REG_TYPE(R64_SP, REG_TYPE(R_64) | REG_TYPE(SP_64)) \
279 /* Typecheck: any int (inc {W}SP inc [WX]ZR) */ \
280 MULTI_REG_TYPE(R_Z_SP, REG_TYPE(R_32) | REG_TYPE(R_64) \
281 | REG_TYPE(SP_32) | REG_TYPE(SP_64) \
282 | REG_TYPE(Z_32) | REG_TYPE(Z_64)) \
283 /* Typecheck: any [BHSDQ]P FP. */ \
284 MULTI_REG_TYPE(BHSDQ, REG_TYPE(FP_B) | REG_TYPE(FP_H) \
285 | REG_TYPE(FP_S) | REG_TYPE(FP_D) | REG_TYPE(FP_Q)) \
286 /* Typecheck: any int or [BHSDQ]P FP or V reg (exc SP inc [WX]ZR) */ \
287 MULTI_REG_TYPE(R_Z_BHSDQ_V, REG_TYPE(R_32) | REG_TYPE(R_64) \
288 | REG_TYPE(Z_32) | REG_TYPE(Z_64) | REG_TYPE(VN) \
289 | REG_TYPE(FP_B) | REG_TYPE(FP_H) \
290 | REG_TYPE(FP_S) | REG_TYPE(FP_D) | REG_TYPE(FP_Q)) \
291 /* Any integer register; used for error messages only. */ \
292 MULTI_REG_TYPE(R_N, REG_TYPE(R_32) | REG_TYPE(R_64) \
293 | REG_TYPE(SP_32) | REG_TYPE(SP_64) \
294 | REG_TYPE(Z_32) | REG_TYPE(Z_64)) \
295 /* Pseudo type to mark the end of the enumerator sequence. */ \
298 #undef BASIC_REG_TYPE
299 #define BASIC_REG_TYPE(T) REG_TYPE_##T,
300 #undef MULTI_REG_TYPE
301 #define MULTI_REG_TYPE(T,V) BASIC_REG_TYPE(T)
303 /* Register type enumerators. */
306 /* A list of REG_TYPE_*. */
310 #undef BASIC_REG_TYPE
311 #define BASIC_REG_TYPE(T) 1 << REG_TYPE_##T,
313 #define REG_TYPE(T) (1 << REG_TYPE_##T)
314 #undef MULTI_REG_TYPE
315 #define MULTI_REG_TYPE(T,V) V,
317 /* Values indexed by aarch64_reg_type to assist the type checking. */
318 static const unsigned reg_type_masks[] =
323 #undef BASIC_REG_TYPE
325 #undef MULTI_REG_TYPE
326 #undef AARCH64_REG_TYPES
328 /* Diagnostics used when we don't get a register of the expected type.
329 Note: this has to synchronized with aarch64_reg_type definitions
332 get_reg_expected_msg (aarch64_reg_type reg_type)
339 msg = N_("integer 32-bit register expected");
342 msg = N_("integer 64-bit register expected");
345 msg = N_("integer register expected");
347 case REG_TYPE_R_Z_SP:
348 msg = N_("integer, zero or SP register expected");
351 msg = N_("8-bit SIMD scalar register expected");
354 msg = N_("16-bit SIMD scalar or floating-point half precision "
355 "register expected");
358 msg = N_("32-bit SIMD scalar or floating-point single precision "
359 "register expected");
362 msg = N_("64-bit SIMD scalar or floating-point double precision "
363 "register expected");
366 msg = N_("128-bit SIMD scalar or floating-point quad precision "
367 "register expected");
370 msg = N_("C0 - C15 expected");
372 case REG_TYPE_R_Z_BHSDQ_V:
373 msg = N_("register expected");
375 case REG_TYPE_BHSDQ: /* any [BHSDQ]P FP */
376 msg = N_("SIMD scalar or floating-point register expected");
378 case REG_TYPE_VN: /* any V reg */
379 msg = N_("vector register expected");
382 as_fatal (_("invalid register type %d"), reg_type);
387 /* Some well known registers that we refer to directly elsewhere. */
390 /* Instructions take 4 bytes in the object file. */
393 /* Define some common error messages. */
394 #define BAD_SP _("SP not allowed here")
396 static struct hash_control *aarch64_ops_hsh;
397 static struct hash_control *aarch64_cond_hsh;
398 static struct hash_control *aarch64_shift_hsh;
399 static struct hash_control *aarch64_sys_regs_hsh;
400 static struct hash_control *aarch64_pstatefield_hsh;
401 static struct hash_control *aarch64_sys_regs_ic_hsh;
402 static struct hash_control *aarch64_sys_regs_dc_hsh;
403 static struct hash_control *aarch64_sys_regs_at_hsh;
404 static struct hash_control *aarch64_sys_regs_tlbi_hsh;
405 static struct hash_control *aarch64_reg_hsh;
406 static struct hash_control *aarch64_barrier_opt_hsh;
407 static struct hash_control *aarch64_nzcv_hsh;
408 static struct hash_control *aarch64_pldop_hsh;
409 static struct hash_control *aarch64_hint_opt_hsh;
411 /* Stuff needed to resolve the label ambiguity
420 static symbolS *last_label_seen;
422 /* Literal pool structure. Held on a per-section
423 and per-sub-section basis. */
425 #define MAX_LITERAL_POOL_SIZE 1024
426 typedef struct literal_expression
429 /* If exp.op == O_big then this bignum holds a copy of the global bignum value. */
430 LITTLENUM_TYPE * bignum;
431 } literal_expression;
433 typedef struct literal_pool
435 literal_expression literals[MAX_LITERAL_POOL_SIZE];
436 unsigned int next_free_entry;
442 struct literal_pool *next;
445 /* Pointer to a linked list of literal pools. */
446 static literal_pool *list_of_pools = NULL;
450 /* This array holds the chars that always start a comment. If the
451 pre-processor is disabled, these aren't very useful. */
452 const char comment_chars[] = "";
454 /* This array holds the chars that only start a comment at the beginning of
455 a line. If the line seems to have the form '# 123 filename'
456 .line and .file directives will appear in the pre-processed output. */
457 /* Note that input_file.c hand checks for '#' at the beginning of the
458 first line of the input file. This is because the compiler outputs
459 #NO_APP at the beginning of its output. */
460 /* Also note that comments like this one will always work. */
461 const char line_comment_chars[] = "#";
463 const char line_separator_chars[] = ";";
465 /* Chars that can be used to separate mant
466 from exp in floating point numbers. */
467 const char EXP_CHARS[] = "eE";
469 /* Chars that mean this number is a floating point constant. */
473 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
475 /* Prefix character that indicates the start of an immediate value. */
476 #define is_immediate_prefix(C) ((C) == '#')
478 /* Separator character handling. */
480 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
482 static inline bfd_boolean
483 skip_past_char (char **str, char c)
494 #define skip_past_comma(str) skip_past_char (str, ',')
496 /* Arithmetic expressions (possibly involving symbols). */
498 static bfd_boolean in_my_get_expression_p = FALSE;
500 /* Third argument to my_get_expression. */
501 #define GE_NO_PREFIX 0
502 #define GE_OPT_PREFIX 1
504 /* Return TRUE if the string pointed by *STR is successfully parsed
505 as an valid expression; *EP will be filled with the information of
506 such an expression. Otherwise return FALSE. */
509 my_get_expression (expressionS * ep, char **str, int prefix_mode,
514 int prefix_present_p = 0;
521 if (is_immediate_prefix (**str))
524 prefix_present_p = 1;
531 memset (ep, 0, sizeof (expressionS));
533 save_in = input_line_pointer;
534 input_line_pointer = *str;
535 in_my_get_expression_p = TRUE;
536 seg = expression (ep);
537 in_my_get_expression_p = FALSE;
539 if (ep->X_op == O_illegal || (reject_absent && ep->X_op == O_absent))
541 /* We found a bad expression in md_operand(). */
542 *str = input_line_pointer;
543 input_line_pointer = save_in;
544 if (prefix_present_p && ! error_p ())
545 set_fatal_syntax_error (_("bad expression"));
547 set_first_syntax_error (_("bad expression"));
552 if (seg != absolute_section
553 && seg != text_section
554 && seg != data_section
555 && seg != bss_section && seg != undefined_section)
557 set_syntax_error (_("bad segment"));
558 *str = input_line_pointer;
559 input_line_pointer = save_in;
566 *str = input_line_pointer;
567 input_line_pointer = save_in;
571 /* Turn a string in input_line_pointer into a floating point constant
572 of type TYPE, and store the appropriate bytes in *LITP. The number
573 of LITTLENUMS emitted is stored in *SIZEP. An error message is
574 returned, or NULL on OK. */
577 md_atof (int type, char *litP, int *sizeP)
579 return ieee_md_atof (type, litP, sizeP, target_big_endian);
582 /* We handle all bad expressions here, so that we can report the faulty
583 instruction in the error message. */
585 md_operand (expressionS * exp)
587 if (in_my_get_expression_p)
588 exp->X_op = O_illegal;
591 /* Immediate values. */
593 /* Errors may be set multiple times during parsing or bit encoding
594 (particularly in the Neon bits), but usually the earliest error which is set
595 will be the most meaningful. Avoid overwriting it with later (cascading)
596 errors by calling this function. */
599 first_error (const char *error)
602 set_syntax_error (error);
605 /* Similiar to first_error, but this function accepts formatted error
608 first_error_fmt (const char *format, ...)
613 /* N.B. this single buffer will not cause error messages for different
614 instructions to pollute each other; this is because at the end of
615 processing of each assembly line, error message if any will be
616 collected by as_bad. */
617 static char buffer[size];
621 int ret ATTRIBUTE_UNUSED;
622 va_start (args, format);
623 ret = vsnprintf (buffer, size, format, args);
624 know (ret <= size - 1 && ret >= 0);
626 set_syntax_error (buffer);
630 /* Register parsing. */
632 /* Generic register parser which is called by other specialized
634 CCP points to what should be the beginning of a register name.
635 If it is indeed a valid register name, advance CCP over it and
636 return the reg_entry structure; otherwise return NULL.
637 It does not issue diagnostics. */
640 parse_reg (char **ccp)
646 #ifdef REGISTER_PREFIX
647 if (*start != REGISTER_PREFIX)
653 if (!ISALPHA (*p) || !is_name_beginner (*p))
658 while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
660 reg = (reg_entry *) hash_find_n (aarch64_reg_hsh, start, p - start);
669 /* Return TRUE if REG->TYPE is a valid type of TYPE; otherwise
672 aarch64_check_reg_type (const reg_entry *reg, aarch64_reg_type type)
674 if (reg->type == type)
679 case REG_TYPE_R64_SP: /* 64-bit integer reg (inc SP exc XZR). */
680 case REG_TYPE_R_Z_SP: /* Integer reg (inc {X}SP inc [WX]ZR). */
681 case REG_TYPE_R_Z_BHSDQ_V: /* Any register apart from Cn. */
682 case REG_TYPE_BHSDQ: /* Any [BHSDQ]P FP or SIMD scalar register. */
683 case REG_TYPE_VN: /* Vector register. */
684 gas_assert (reg->type < REG_TYPE_MAX && type < REG_TYPE_MAX);
685 return ((reg_type_masks[reg->type] & reg_type_masks[type])
686 == reg_type_masks[reg->type]);
688 as_fatal ("unhandled type %d", type);
693 /* Parse a register and return PARSE_FAIL if the register is not of type R_Z_SP.
694 Return the register number otherwise. *ISREG32 is set to one if the
695 register is 32-bit wide; *ISREGZERO is set to one if the register is
696 of type Z_32 or Z_64.
697 Note that this function does not issue any diagnostics. */
700 aarch64_reg_parse_32_64 (char **ccp, int reject_sp, int reject_rz,
701 int *isreg32, int *isregzero)
704 const reg_entry *reg = parse_reg (&str);
709 if (! aarch64_check_reg_type (reg, REG_TYPE_R_Z_SP))
718 *isreg32 = reg->type == REG_TYPE_SP_32;
723 *isreg32 = reg->type == REG_TYPE_R_32;
730 *isreg32 = reg->type == REG_TYPE_Z_32;
742 /* Parse the qualifier of a SIMD vector register or a SIMD vector element.
743 Fill in *PARSED_TYPE and return TRUE if the parsing succeeds;
744 otherwise return FALSE.
746 Accept only one occurrence of:
747 8b 16b 2h 4h 8h 2s 4s 1d 2d
750 parse_neon_type_for_operand (struct neon_type_el *parsed_type, char **str)
754 unsigned element_size;
755 enum neon_el_type type;
765 width = strtoul (ptr, &ptr, 10);
766 if (width != 1 && width != 2 && width != 4 && width != 8 && width != 16)
768 first_error_fmt (_("bad size %d in vector width specifier"), width);
773 switch (TOLOWER (*ptr))
801 first_error_fmt (_("unexpected character `%c' in element size"), *ptr);
803 first_error (_("missing element size"));
806 if (width != 0 && width * element_size != 64 && width * element_size != 128
807 && !(width == 2 && element_size == 16))
810 ("invalid element size %d and vector size combination %c"),
816 parsed_type->type = type;
817 parsed_type->width = width;
824 /* Parse a single type, e.g. ".8b", leading period included.
825 Only applicable to Vn registers.
827 Return TRUE on success; otherwise return FALSE. */
829 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
835 if (! parse_neon_type_for_operand (vectype, &str))
837 first_error (_("vector type expected"));
849 /* Parse a register of the type TYPE.
851 Return PARSE_FAIL if the string pointed by *CCP is not a valid register
852 name or the parsed register is not of TYPE.
854 Otherwise return the register number, and optionally fill in the actual
855 type of the register in *RTYPE when multiple alternatives were given, and
856 return the register shape and element index information in *TYPEINFO.
858 IN_REG_LIST should be set with TRUE if the caller is parsing a register
862 parse_typed_reg (char **ccp, aarch64_reg_type type, aarch64_reg_type *rtype,
863 struct neon_type_el *typeinfo, bfd_boolean in_reg_list)
866 const reg_entry *reg = parse_reg (&str);
867 struct neon_type_el atype;
868 struct neon_type_el parsetype;
869 bfd_boolean is_typed_vecreg = FALSE;
872 atype.type = NT_invtype;
880 set_default_error ();
884 if (! aarch64_check_reg_type (reg, type))
886 DEBUG_TRACE ("reg type check failed");
887 set_default_error ();
892 if (type == REG_TYPE_VN
893 && parse_neon_operand_type (&parsetype, &str))
895 /* Register if of the form Vn.[bhsdq]. */
896 is_typed_vecreg = TRUE;
898 if (parsetype.width == 0)
899 /* Expect index. In the new scheme we cannot have
900 Vn.[bhsdq] represent a scalar. Therefore any
901 Vn.[bhsdq] should have an index following it.
902 Except in reglists ofcourse. */
903 atype.defined |= NTA_HASINDEX;
905 atype.defined |= NTA_HASTYPE;
907 atype.type = parsetype.type;
908 atype.width = parsetype.width;
911 if (skip_past_char (&str, '['))
915 /* Reject Sn[index] syntax. */
916 if (!is_typed_vecreg)
918 first_error (_("this type of register can't be indexed"));
922 if (in_reg_list == TRUE)
924 first_error (_("index not allowed inside register list"));
928 atype.defined |= NTA_HASINDEX;
930 my_get_expression (&exp, &str, GE_NO_PREFIX, 1);
932 if (exp.X_op != O_constant)
934 first_error (_("constant expression required"));
938 if (! skip_past_char (&str, ']'))
941 atype.index = exp.X_add_number;
943 else if (!in_reg_list && (atype.defined & NTA_HASINDEX) != 0)
945 /* Indexed vector register expected. */
946 first_error (_("indexed vector register expected"));
950 /* A vector reg Vn should be typed or indexed. */
951 if (type == REG_TYPE_VN && atype.defined == 0)
953 first_error (_("invalid use of vector register"));
969 Return the register number on success; return PARSE_FAIL otherwise.
971 If RTYPE is not NULL, return in *RTYPE the (possibly restricted) type of
972 the register (e.g. NEON double or quad reg when either has been requested).
974 If this is a NEON vector register with additional type information, fill
975 in the struct pointed to by VECTYPE (if non-NULL).
977 This parser does not handle register list. */
980 aarch64_reg_parse (char **ccp, aarch64_reg_type type,
981 aarch64_reg_type *rtype, struct neon_type_el *vectype)
983 struct neon_type_el atype;
985 int reg = parse_typed_reg (&str, type, rtype, &atype,
986 /*in_reg_list= */ FALSE);
988 if (reg == PARSE_FAIL)
999 static inline bfd_boolean
1000 eq_neon_type_el (struct neon_type_el e1, struct neon_type_el e2)
1004 && e1.defined == e2.defined
1005 && e1.width == e2.width && e1.index == e2.index;
1008 /* This function parses the NEON register list. On success, it returns
1009 the parsed register list information in the following encoded format:
1011 bit 18-22 | 13-17 | 7-11 | 2-6 | 0-1
1012 4th regno | 3rd regno | 2nd regno | 1st regno | num_of_reg
1014 The information of the register shape and/or index is returned in
1017 It returns PARSE_FAIL if the register list is invalid.
1019 The list contains one to four registers.
1020 Each register can be one of:
1023 All <T> should be identical.
1024 All <index> should be identical.
1025 There are restrictions on <Vt> numbers which are checked later
1026 (by reg_list_valid_p). */
1029 parse_neon_reg_list (char **ccp, struct neon_type_el *vectype)
1033 struct neon_type_el typeinfo, typeinfo_first;
1038 bfd_boolean error = FALSE;
1039 bfd_boolean expect_index = FALSE;
1043 set_syntax_error (_("expecting {"));
1049 typeinfo_first.defined = 0;
1050 typeinfo_first.type = NT_invtype;
1051 typeinfo_first.width = -1;
1052 typeinfo_first.index = 0;
1061 str++; /* skip over '-' */
1064 val = parse_typed_reg (&str, REG_TYPE_VN, NULL, &typeinfo,
1065 /*in_reg_list= */ TRUE);
1066 if (val == PARSE_FAIL)
1068 set_first_syntax_error (_("invalid vector register in list"));
1072 /* reject [bhsd]n */
1073 if (typeinfo.defined == 0)
1075 set_first_syntax_error (_("invalid scalar register in list"));
1080 if (typeinfo.defined & NTA_HASINDEX)
1081 expect_index = TRUE;
1085 if (val < val_range)
1087 set_first_syntax_error
1088 (_("invalid range in vector register list"));
1097 typeinfo_first = typeinfo;
1098 else if (! eq_neon_type_el (typeinfo_first, typeinfo))
1100 set_first_syntax_error
1101 (_("type mismatch in vector register list"));
1106 for (i = val_range; i <= val; i++)
1108 ret_val |= i << (5 * nb_regs);
1113 while (skip_past_comma (&str) || (in_range = 1, *str == '-'));
1115 skip_whitespace (str);
1118 set_first_syntax_error (_("end of vector register list not found"));
1123 skip_whitespace (str);
1127 if (skip_past_char (&str, '['))
1131 my_get_expression (&exp, &str, GE_NO_PREFIX, 1);
1132 if (exp.X_op != O_constant)
1134 set_first_syntax_error (_("constant expression required."));
1137 if (! skip_past_char (&str, ']'))
1140 typeinfo_first.index = exp.X_add_number;
1144 set_first_syntax_error (_("expected index"));
1151 set_first_syntax_error (_("too many registers in vector register list"));
1154 else if (nb_regs == 0)
1156 set_first_syntax_error (_("empty vector register list"));
1162 *vectype = typeinfo_first;
1164 return error ? PARSE_FAIL : (ret_val << 2) | (nb_regs - 1);
1167 /* Directives: register aliases. */
1170 insert_reg_alias (char *str, int number, aarch64_reg_type type)
1175 if ((new = hash_find (aarch64_reg_hsh, str)) != 0)
1178 as_warn (_("ignoring attempt to redefine built-in register '%s'"),
1181 /* Only warn about a redefinition if it's not defined as the
1183 else if (new->number != number || new->type != type)
1184 as_warn (_("ignoring redefinition of register alias '%s'"), str);
1189 name = xstrdup (str);
1190 new = XNEW (reg_entry);
1193 new->number = number;
1195 new->builtin = FALSE;
1197 if (hash_insert (aarch64_reg_hsh, name, (void *) new))
1203 /* Look for the .req directive. This is of the form:
1205 new_register_name .req existing_register_name
1207 If we find one, or if it looks sufficiently like one that we want to
1208 handle any error here, return TRUE. Otherwise return FALSE. */
1211 create_register_alias (char *newname, char *p)
1213 const reg_entry *old;
1214 char *oldname, *nbuf;
1217 /* The input scrubber ensures that whitespace after the mnemonic is
1218 collapsed to single spaces. */
1220 if (strncmp (oldname, " .req ", 6) != 0)
1224 if (*oldname == '\0')
1227 old = hash_find (aarch64_reg_hsh, oldname);
1230 as_warn (_("unknown register '%s' -- .req ignored"), oldname);
1234 /* If TC_CASE_SENSITIVE is defined, then newname already points to
1235 the desired alias name, and p points to its end. If not, then
1236 the desired alias name is in the global original_case_string. */
1237 #ifdef TC_CASE_SENSITIVE
1240 newname = original_case_string;
1241 nlen = strlen (newname);
1244 nbuf = xmemdup0 (newname, nlen);
1246 /* Create aliases under the new name as stated; an all-lowercase
1247 version of the new name; and an all-uppercase version of the new
1249 if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
1251 for (p = nbuf; *p; p++)
1254 if (strncmp (nbuf, newname, nlen))
1256 /* If this attempt to create an additional alias fails, do not bother
1257 trying to create the all-lower case alias. We will fail and issue
1258 a second, duplicate error message. This situation arises when the
1259 programmer does something like:
1262 The second .req creates the "Foo" alias but then fails to create
1263 the artificial FOO alias because it has already been created by the
1265 if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
1272 for (p = nbuf; *p; p++)
1275 if (strncmp (nbuf, newname, nlen))
1276 insert_reg_alias (nbuf, old->number, old->type);
1283 /* Should never be called, as .req goes between the alias and the
1284 register name, not at the beginning of the line. */
1286 s_req (int a ATTRIBUTE_UNUSED)
1288 as_bad (_("invalid syntax for .req directive"));
1291 /* The .unreq directive deletes an alias which was previously defined
1292 by .req. For example:
1298 s_unreq (int a ATTRIBUTE_UNUSED)
1303 name = input_line_pointer;
1305 while (*input_line_pointer != 0
1306 && *input_line_pointer != ' ' && *input_line_pointer != '\n')
1307 ++input_line_pointer;
1309 saved_char = *input_line_pointer;
1310 *input_line_pointer = 0;
1313 as_bad (_("invalid syntax for .unreq directive"));
1316 reg_entry *reg = hash_find (aarch64_reg_hsh, name);
1319 as_bad (_("unknown register alias '%s'"), name);
1320 else if (reg->builtin)
1321 as_warn (_("ignoring attempt to undefine built-in register '%s'"),
1328 hash_delete (aarch64_reg_hsh, name, FALSE);
1329 free ((char *) reg->name);
1332 /* Also locate the all upper case and all lower case versions.
1333 Do not complain if we cannot find one or the other as it
1334 was probably deleted above. */
1336 nbuf = strdup (name);
1337 for (p = nbuf; *p; p++)
1339 reg = hash_find (aarch64_reg_hsh, nbuf);
1342 hash_delete (aarch64_reg_hsh, nbuf, FALSE);
1343 free ((char *) reg->name);
1347 for (p = nbuf; *p; p++)
1349 reg = hash_find (aarch64_reg_hsh, nbuf);
1352 hash_delete (aarch64_reg_hsh, nbuf, FALSE);
1353 free ((char *) reg->name);
1361 *input_line_pointer = saved_char;
1362 demand_empty_rest_of_line ();
1365 /* Directives: Instruction set selection. */
1368 /* This code is to handle mapping symbols as defined in the ARM AArch64 ELF
1369 spec. (See "Mapping symbols", section 4.5.4, ARM AAELF64 version 0.05).
1370 Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
1371 and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped. */
1373 /* Create a new mapping symbol for the transition to STATE. */
1376 make_mapping_symbol (enum mstate state, valueT value, fragS * frag)
1379 const char *symname;
1386 type = BSF_NO_FLAGS;
1390 type = BSF_NO_FLAGS;
1396 symbolP = symbol_new (symname, now_seg, value, frag);
1397 symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
1399 /* Save the mapping symbols for future reference. Also check that
1400 we do not place two mapping symbols at the same offset within a
1401 frag. We'll handle overlap between frags in
1402 check_mapping_symbols.
1404 If .fill or other data filling directive generates zero sized data,
1405 the mapping symbol for the following code will have the same value
1406 as the one generated for the data filling directive. In this case,
1407 we replace the old symbol with the new one at the same address. */
1410 if (frag->tc_frag_data.first_map != NULL)
1412 know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
1413 symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP,
1416 frag->tc_frag_data.first_map = symbolP;
1418 if (frag->tc_frag_data.last_map != NULL)
1420 know (S_GET_VALUE (frag->tc_frag_data.last_map) <=
1421 S_GET_VALUE (symbolP));
1422 if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
1423 symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP,
1426 frag->tc_frag_data.last_map = symbolP;
1429 /* We must sometimes convert a region marked as code to data during
1430 code alignment, if an odd number of bytes have to be padded. The
1431 code mapping symbol is pushed to an aligned address. */
1434 insert_data_mapping_symbol (enum mstate state,
1435 valueT value, fragS * frag, offsetT bytes)
1437 /* If there was already a mapping symbol, remove it. */
1438 if (frag->tc_frag_data.last_map != NULL
1439 && S_GET_VALUE (frag->tc_frag_data.last_map) ==
1440 frag->fr_address + value)
1442 symbolS *symp = frag->tc_frag_data.last_map;
1446 know (frag->tc_frag_data.first_map == symp);
1447 frag->tc_frag_data.first_map = NULL;
1449 frag->tc_frag_data.last_map = NULL;
1450 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1453 make_mapping_symbol (MAP_DATA, value, frag);
1454 make_mapping_symbol (state, value + bytes, frag);
1457 static void mapping_state_2 (enum mstate state, int max_chars);
1459 /* Set the mapping state to STATE. Only call this when about to
1460 emit some STATE bytes to the file. */
1463 mapping_state (enum mstate state)
1465 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
1467 if (state == MAP_INSN)
1468 /* AArch64 instructions require 4-byte alignment. When emitting
1469 instructions into any section, record the appropriate section
1471 record_alignment (now_seg, 2);
1473 if (mapstate == state)
1474 /* The mapping symbol has already been emitted.
1475 There is nothing else to do. */
1478 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
1479 if (TRANSITION (MAP_UNDEFINED, MAP_DATA) && !subseg_text_p (now_seg))
1480 /* Emit MAP_DATA within executable section in order. Otherwise, it will be
1481 evaluated later in the next else. */
1483 else if (TRANSITION (MAP_UNDEFINED, MAP_INSN))
1485 /* Only add the symbol if the offset is > 0:
1486 if we're at the first frag, check it's size > 0;
1487 if we're not at the first frag, then for sure
1488 the offset is > 0. */
1489 struct frag *const frag_first = seg_info (now_seg)->frchainP->frch_root;
1490 const int add_symbol = (frag_now != frag_first)
1491 || (frag_now_fix () > 0);
1494 make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
1498 mapping_state_2 (state, 0);
1501 /* Same as mapping_state, but MAX_CHARS bytes have already been
1502 allocated. Put the mapping symbol that far back. */
1505 mapping_state_2 (enum mstate state, int max_chars)
1507 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
1509 if (!SEG_NORMAL (now_seg))
1512 if (mapstate == state)
1513 /* The mapping symbol has already been emitted.
1514 There is nothing else to do. */
1517 seg_info (now_seg)->tc_segment_info_data.mapstate = state;
1518 make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
1521 #define mapping_state(x) /* nothing */
1522 #define mapping_state_2(x, y) /* nothing */
1525 /* Directives: sectioning and alignment. */
1528 s_bss (int ignore ATTRIBUTE_UNUSED)
1530 /* We don't support putting frags in the BSS segment, we fake it by
1531 marking in_bss, then looking at s_skip for clues. */
1532 subseg_set (bss_section, 0);
1533 demand_empty_rest_of_line ();
1534 mapping_state (MAP_DATA);
1538 s_even (int ignore ATTRIBUTE_UNUSED)
1540 /* Never make frag if expect extra pass. */
1542 frag_align (1, 0, 0);
1544 record_alignment (now_seg, 1);
1546 demand_empty_rest_of_line ();
1549 /* Directives: Literal pools. */
1551 static literal_pool *
1552 find_literal_pool (int size)
1556 for (pool = list_of_pools; pool != NULL; pool = pool->next)
1558 if (pool->section == now_seg
1559 && pool->sub_section == now_subseg && pool->size == size)
1566 static literal_pool *
1567 find_or_make_literal_pool (int size)
1569 /* Next literal pool ID number. */
1570 static unsigned int latest_pool_num = 1;
1573 pool = find_literal_pool (size);
1577 /* Create a new pool. */
1578 pool = XNEW (literal_pool);
1582 /* Currently we always put the literal pool in the current text
1583 section. If we were generating "small" model code where we
1584 knew that all code and initialised data was within 1MB then
1585 we could output literals to mergeable, read-only data
1588 pool->next_free_entry = 0;
1589 pool->section = now_seg;
1590 pool->sub_section = now_subseg;
1592 pool->next = list_of_pools;
1593 pool->symbol = NULL;
1595 /* Add it to the list. */
1596 list_of_pools = pool;
1599 /* New pools, and emptied pools, will have a NULL symbol. */
1600 if (pool->symbol == NULL)
1602 pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
1603 (valueT) 0, &zero_address_frag);
1604 pool->id = latest_pool_num++;
1611 /* Add the literal of size SIZE in *EXP to the relevant literal pool.
1612 Return TRUE on success, otherwise return FALSE. */
1614 add_to_lit_pool (expressionS *exp, int size)
1619 pool = find_or_make_literal_pool (size);
1621 /* Check if this literal value is already in the pool. */
1622 for (entry = 0; entry < pool->next_free_entry; entry++)
1624 expressionS * litexp = & pool->literals[entry].exp;
1626 if ((litexp->X_op == exp->X_op)
1627 && (exp->X_op == O_constant)
1628 && (litexp->X_add_number == exp->X_add_number)
1629 && (litexp->X_unsigned == exp->X_unsigned))
1632 if ((litexp->X_op == exp->X_op)
1633 && (exp->X_op == O_symbol)
1634 && (litexp->X_add_number == exp->X_add_number)
1635 && (litexp->X_add_symbol == exp->X_add_symbol)
1636 && (litexp->X_op_symbol == exp->X_op_symbol))
1640 /* Do we need to create a new entry? */
1641 if (entry == pool->next_free_entry)
1643 if (entry >= MAX_LITERAL_POOL_SIZE)
1645 set_syntax_error (_("literal pool overflow"));
1649 pool->literals[entry].exp = *exp;
1650 pool->next_free_entry += 1;
1651 if (exp->X_op == O_big)
1653 /* PR 16688: Bignums are held in a single global array. We must
1654 copy and preserve that value now, before it is overwritten. */
1655 pool->literals[entry].bignum = XNEWVEC (LITTLENUM_TYPE,
1657 memcpy (pool->literals[entry].bignum, generic_bignum,
1658 CHARS_PER_LITTLENUM * exp->X_add_number);
1661 pool->literals[entry].bignum = NULL;
1664 exp->X_op = O_symbol;
1665 exp->X_add_number = ((int) entry) * size;
1666 exp->X_add_symbol = pool->symbol;
1671 /* Can't use symbol_new here, so have to create a symbol and then at
1672 a later date assign it a value. Thats what these functions do. */
1675 symbol_locate (symbolS * symbolP,
1676 const char *name,/* It is copied, the caller can modify. */
1677 segT segment, /* Segment identifier (SEG_<something>). */
1678 valueT valu, /* Symbol value. */
1679 fragS * frag) /* Associated fragment. */
1682 char *preserved_copy_of_name;
1684 name_length = strlen (name) + 1; /* +1 for \0. */
1685 obstack_grow (¬es, name, name_length);
1686 preserved_copy_of_name = obstack_finish (¬es);
1688 #ifdef tc_canonicalize_symbol_name
1689 preserved_copy_of_name =
1690 tc_canonicalize_symbol_name (preserved_copy_of_name);
1693 S_SET_NAME (symbolP, preserved_copy_of_name);
1695 S_SET_SEGMENT (symbolP, segment);
1696 S_SET_VALUE (symbolP, valu);
1697 symbol_clear_list_pointers (symbolP);
1699 symbol_set_frag (symbolP, frag);
1701 /* Link to end of symbol chain. */
1703 extern int symbol_table_frozen;
1705 if (symbol_table_frozen)
1709 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
1711 obj_symbol_new_hook (symbolP);
1713 #ifdef tc_symbol_new_hook
1714 tc_symbol_new_hook (symbolP);
1718 verify_symbol_chain (symbol_rootP, symbol_lastP);
1719 #endif /* DEBUG_SYMS */
1724 s_ltorg (int ignored ATTRIBUTE_UNUSED)
1731 for (align = 2; align <= 4; align++)
1733 int size = 1 << align;
1735 pool = find_literal_pool (size);
1736 if (pool == NULL || pool->symbol == NULL || pool->next_free_entry == 0)
1739 mapping_state (MAP_DATA);
1741 /* Align pool as you have word accesses.
1742 Only make a frag if we have to. */
1744 frag_align (align, 0, 0);
1746 record_alignment (now_seg, align);
1748 sprintf (sym_name, "$$lit_\002%x", pool->id);
1750 symbol_locate (pool->symbol, sym_name, now_seg,
1751 (valueT) frag_now_fix (), frag_now);
1752 symbol_table_insert (pool->symbol);
1754 for (entry = 0; entry < pool->next_free_entry; entry++)
1756 expressionS * exp = & pool->literals[entry].exp;
1758 if (exp->X_op == O_big)
1760 /* PR 16688: Restore the global bignum value. */
1761 gas_assert (pool->literals[entry].bignum != NULL);
1762 memcpy (generic_bignum, pool->literals[entry].bignum,
1763 CHARS_PER_LITTLENUM * exp->X_add_number);
1766 /* First output the expression in the instruction to the pool. */
1767 emit_expr (exp, size); /* .word|.xword */
1769 if (exp->X_op == O_big)
1771 free (pool->literals[entry].bignum);
1772 pool->literals[entry].bignum = NULL;
1776 /* Mark the pool as empty. */
1777 pool->next_free_entry = 0;
1778 pool->symbol = NULL;
1783 /* Forward declarations for functions below, in the MD interface
1785 static fixS *fix_new_aarch64 (fragS *, int, short, expressionS *, int, int);
1786 static struct reloc_table_entry * find_reloc_table_entry (char **);
1788 /* Directives: Data. */
1789 /* N.B. the support for relocation suffix in this directive needs to be
1790 implemented properly. */
1793 s_aarch64_elf_cons (int nbytes)
1797 #ifdef md_flush_pending_output
1798 md_flush_pending_output ();
1801 if (is_it_end_of_statement ())
1803 demand_empty_rest_of_line ();
1807 #ifdef md_cons_align
1808 md_cons_align (nbytes);
1811 mapping_state (MAP_DATA);
1814 struct reloc_table_entry *reloc;
1818 if (exp.X_op != O_symbol)
1819 emit_expr (&exp, (unsigned int) nbytes);
1822 skip_past_char (&input_line_pointer, '#');
1823 if (skip_past_char (&input_line_pointer, ':'))
1825 reloc = find_reloc_table_entry (&input_line_pointer);
1827 as_bad (_("unrecognized relocation suffix"));
1829 as_bad (_("unimplemented relocation suffix"));
1830 ignore_rest_of_line ();
1834 emit_expr (&exp, (unsigned int) nbytes);
1837 while (*input_line_pointer++ == ',');
1839 /* Put terminator back into stream. */
1840 input_line_pointer--;
1841 demand_empty_rest_of_line ();
1844 #endif /* OBJ_ELF */
1846 /* Output a 32-bit word, but mark as an instruction. */
1849 s_aarch64_inst (int ignored ATTRIBUTE_UNUSED)
1853 #ifdef md_flush_pending_output
1854 md_flush_pending_output ();
1857 if (is_it_end_of_statement ())
1859 demand_empty_rest_of_line ();
1863 /* Sections are assumed to start aligned. In executable section, there is no
1864 MAP_DATA symbol pending. So we only align the address during
1865 MAP_DATA --> MAP_INSN transition.
1866 For other sections, this is not guaranteed. */
1867 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
1868 if (!need_pass_2 && subseg_text_p (now_seg) && mapstate == MAP_DATA)
1869 frag_align_code (2, 0);
1872 mapping_state (MAP_INSN);
1878 if (exp.X_op != O_constant)
1880 as_bad (_("constant expression required"));
1881 ignore_rest_of_line ();
1885 if (target_big_endian)
1887 unsigned int val = exp.X_add_number;
1888 exp.X_add_number = SWAP_32 (val);
1890 emit_expr (&exp, 4);
1892 while (*input_line_pointer++ == ',');
1894 /* Put terminator back into stream. */
1895 input_line_pointer--;
1896 demand_empty_rest_of_line ();
1900 /* Emit BFD_RELOC_AARCH64_TLSDESC_ADD on the next ADD instruction. */
1903 s_tlsdescadd (int ignored ATTRIBUTE_UNUSED)
1909 fix_new_aarch64 (frag_now, frag_more (0) - frag_now->fr_literal, 4, &exp, 0,
1910 BFD_RELOC_AARCH64_TLSDESC_ADD);
1912 demand_empty_rest_of_line ();
1915 /* Emit BFD_RELOC_AARCH64_TLSDESC_CALL on the next BLR instruction. */
1918 s_tlsdesccall (int ignored ATTRIBUTE_UNUSED)
1922 /* Since we're just labelling the code, there's no need to define a
1925 /* Make sure there is enough room in this frag for the following
1926 blr. This trick only works if the blr follows immediately after
1927 the .tlsdesc directive. */
1929 fix_new_aarch64 (frag_now, frag_more (0) - frag_now->fr_literal, 4, &exp, 0,
1930 BFD_RELOC_AARCH64_TLSDESC_CALL);
1932 demand_empty_rest_of_line ();
1935 /* Emit BFD_RELOC_AARCH64_TLSDESC_LDR on the next LDR instruction. */
1938 s_tlsdescldr (int ignored ATTRIBUTE_UNUSED)
1944 fix_new_aarch64 (frag_now, frag_more (0) - frag_now->fr_literal, 4, &exp, 0,
1945 BFD_RELOC_AARCH64_TLSDESC_LDR);
1947 demand_empty_rest_of_line ();
1949 #endif /* OBJ_ELF */
1951 static void s_aarch64_arch (int);
1952 static void s_aarch64_cpu (int);
1953 static void s_aarch64_arch_extension (int);
1955 /* This table describes all the machine specific pseudo-ops the assembler
1956 has to support. The fields are:
1957 pseudo-op name without dot
1958 function to call to execute this pseudo-op
1959 Integer arg to pass to the function. */
1961 const pseudo_typeS md_pseudo_table[] = {
1962 /* Never called because '.req' does not start a line. */
1964 {"unreq", s_unreq, 0},
1966 {"even", s_even, 0},
1967 {"ltorg", s_ltorg, 0},
1968 {"pool", s_ltorg, 0},
1969 {"cpu", s_aarch64_cpu, 0},
1970 {"arch", s_aarch64_arch, 0},
1971 {"arch_extension", s_aarch64_arch_extension, 0},
1972 {"inst", s_aarch64_inst, 0},
1974 {"tlsdescadd", s_tlsdescadd, 0},
1975 {"tlsdesccall", s_tlsdesccall, 0},
1976 {"tlsdescldr", s_tlsdescldr, 0},
1977 {"word", s_aarch64_elf_cons, 4},
1978 {"long", s_aarch64_elf_cons, 4},
1979 {"xword", s_aarch64_elf_cons, 8},
1980 {"dword", s_aarch64_elf_cons, 8},
1986 /* Check whether STR points to a register name followed by a comma or the
1987 end of line; REG_TYPE indicates which register types are checked
1988 against. Return TRUE if STR is such a register name; otherwise return
1989 FALSE. The function does not intend to produce any diagnostics, but since
1990 the register parser aarch64_reg_parse, which is called by this function,
1991 does produce diagnostics, we call clear_error to clear any diagnostics
1992 that may be generated by aarch64_reg_parse.
1993 Also, the function returns FALSE directly if there is any user error
1994 present at the function entry. This prevents the existing diagnostics
1995 state from being spoiled.
1996 The function currently serves parse_constant_immediate and
1997 parse_big_immediate only. */
1999 reg_name_p (char *str, aarch64_reg_type reg_type)
2003 /* Prevent the diagnostics state from being spoiled. */
2007 reg = aarch64_reg_parse (&str, reg_type, NULL, NULL);
2009 /* Clear the parsing error that may be set by the reg parser. */
2012 if (reg == PARSE_FAIL)
2015 skip_whitespace (str);
2016 if (*str == ',' || is_end_of_line[(unsigned int) *str])
2022 /* Parser functions used exclusively in instruction operands. */
2024 /* Parse an immediate expression which may not be constant.
2026 To prevent the expression parser from pushing a register name
2027 into the symbol table as an undefined symbol, firstly a check is
2028 done to find out whether STR is a valid register name followed
2029 by a comma or the end of line. Return FALSE if STR is such a
2033 parse_immediate_expression (char **str, expressionS *exp)
2035 if (reg_name_p (*str, REG_TYPE_R_Z_BHSDQ_V))
2037 set_recoverable_error (_("immediate operand required"));
2041 my_get_expression (exp, str, GE_OPT_PREFIX, 1);
2043 if (exp->X_op == O_absent)
2045 set_fatal_syntax_error (_("missing immediate expression"));
2052 /* Constant immediate-value read function for use in insn parsing.
2053 STR points to the beginning of the immediate (with the optional
2054 leading #); *VAL receives the value.
2056 Return TRUE on success; otherwise return FALSE. */
2059 parse_constant_immediate (char **str, int64_t * val)
2063 if (! parse_immediate_expression (str, &exp))
2066 if (exp.X_op != O_constant)
2068 set_syntax_error (_("constant expression required"));
2072 *val = exp.X_add_number;
2077 encode_imm_float_bits (uint32_t imm)
2079 return ((imm >> 19) & 0x7f) /* b[25:19] -> b[6:0] */
2080 | ((imm >> (31 - 7)) & 0x80); /* b[31] -> b[7] */
2083 /* Return TRUE if the single-precision floating-point value encoded in IMM
2084 can be expressed in the AArch64 8-bit signed floating-point format with
2085 3-bit exponent and normalized 4 bits of precision; in other words, the
2086 floating-point value must be expressable as
2087 (+/-) n / 16 * power (2, r)
2088 where n and r are integers such that 16 <= n <=31 and -3 <= r <= 4. */
2091 aarch64_imm_float_p (uint32_t imm)
2093 /* If a single-precision floating-point value has the following bit
2094 pattern, it can be expressed in the AArch64 8-bit floating-point
2097 3 32222222 2221111111111
2098 1 09876543 21098765432109876543210
2099 n Eeeeeexx xxxx0000000000000000000
2101 where n, e and each x are either 0 or 1 independently, with
2106 /* Prepare the pattern for 'Eeeeee'. */
2107 if (((imm >> 30) & 0x1) == 0)
2108 pattern = 0x3e000000;
2110 pattern = 0x40000000;
2112 return (imm & 0x7ffff) == 0 /* lower 19 bits are 0. */
2113 && ((imm & 0x7e000000) == pattern); /* bits 25 - 29 == ~ bit 30. */
2116 /* Like aarch64_imm_float_p but for a double-precision floating-point value.
2118 Return TRUE if the value encoded in IMM can be expressed in the AArch64
2119 8-bit signed floating-point format with 3-bit exponent and normalized 4
2120 bits of precision (i.e. can be used in an FMOV instruction); return the
2121 equivalent single-precision encoding in *FPWORD.
2123 Otherwise return FALSE. */
2126 aarch64_double_precision_fmovable (uint64_t imm, uint32_t *fpword)
2128 /* If a double-precision floating-point value has the following bit
2129 pattern, it can be expressed in the AArch64 8-bit floating-point
2132 6 66655555555 554444444...21111111111
2133 3 21098765432 109876543...098765432109876543210
2134 n Eeeeeeeeexx xxxx00000...000000000000000000000
2136 where n, e and each x are either 0 or 1 independently, with
2140 uint32_t high32 = imm >> 32;
2142 /* Lower 32 bits need to be 0s. */
2143 if ((imm & 0xffffffff) != 0)
2146 /* Prepare the pattern for 'Eeeeeeeee'. */
2147 if (((high32 >> 30) & 0x1) == 0)
2148 pattern = 0x3fc00000;
2150 pattern = 0x40000000;
2152 if ((high32 & 0xffff) == 0 /* bits 32 - 47 are 0. */
2153 && (high32 & 0x7fc00000) == pattern) /* bits 54 - 61 == ~ bit 62. */
2155 /* Convert to the single-precision encoding.
2157 n Eeeeeeeeexx xxxx00000...000000000000000000000
2159 n Eeeeeexx xxxx0000000000000000000. */
2160 *fpword = ((high32 & 0xfe000000) /* nEeeeee. */
2161 | (((high32 >> 16) & 0x3f) << 19)); /* xxxxxx. */
2168 /* Parse a floating-point immediate. Return TRUE on success and return the
2169 value in *IMMED in the format of IEEE754 single-precision encoding.
2170 *CCP points to the start of the string; DP_P is TRUE when the immediate
2171 is expected to be in double-precision (N.B. this only matters when
2172 hexadecimal representation is involved).
2174 N.B. 0.0 is accepted by this function. */
2177 parse_aarch64_imm_float (char **ccp, int *immed, bfd_boolean dp_p)
2181 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2182 int found_fpchar = 0;
2184 unsigned fpword = 0;
2185 bfd_boolean hex_p = FALSE;
2187 skip_past_char (&str, '#');
2190 skip_whitespace (fpnum);
2192 if (strncmp (fpnum, "0x", 2) == 0)
2194 /* Support the hexadecimal representation of the IEEE754 encoding.
2195 Double-precision is expected when DP_P is TRUE, otherwise the
2196 representation should be in single-precision. */
2197 if (! parse_constant_immediate (&str, &val))
2202 if (! aarch64_double_precision_fmovable (val, &fpword))
2205 else if ((uint64_t) val > 0xffffffff)
2214 /* We must not accidentally parse an integer as a floating-point number.
2215 Make sure that the value we parse is not an integer by checking for
2216 special characters '.' or 'e'. */
2217 for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
2218 if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
2232 if ((str = atof_ieee (str, 's', words)) == NULL)
2235 /* Our FP word must be 32 bits (single-precision FP). */
2236 for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
2238 fpword <<= LITTLENUM_NUMBER_OF_BITS;
2243 if (aarch64_imm_float_p (fpword) || (fpword & 0x7fffffff) == 0)
2251 set_fatal_syntax_error (_("invalid floating-point constant"));
2255 /* Less-generic immediate-value read function with the possibility of loading
2256 a big (64-bit) immediate, as required by AdvSIMD Modified immediate
2259 To prevent the expression parser from pushing a register name into the
2260 symbol table as an undefined symbol, a check is firstly done to find
2261 out whether STR is a valid register name followed by a comma or the end
2262 of line. Return FALSE if STR is such a register. */
2265 parse_big_immediate (char **str, int64_t *imm)
2269 if (reg_name_p (ptr, REG_TYPE_R_Z_BHSDQ_V))
2271 set_syntax_error (_("immediate operand required"));
2275 my_get_expression (&inst.reloc.exp, &ptr, GE_OPT_PREFIX, 1);
2277 if (inst.reloc.exp.X_op == O_constant)
2278 *imm = inst.reloc.exp.X_add_number;
2285 /* Set operand IDX of the *INSTR that needs a GAS internal fixup.
2286 if NEED_LIBOPCODES is non-zero, the fixup will need
2287 assistance from the libopcodes. */
2290 aarch64_set_gas_internal_fixup (struct reloc *reloc,
2291 const aarch64_opnd_info *operand,
2292 int need_libopcodes_p)
2294 reloc->type = BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP;
2295 reloc->opnd = operand->type;
2296 if (need_libopcodes_p)
2297 reloc->need_libopcodes_p = 1;
2300 /* Return TRUE if the instruction needs to be fixed up later internally by
2301 the GAS; otherwise return FALSE. */
2303 static inline bfd_boolean
2304 aarch64_gas_internal_fixup_p (void)
2306 return inst.reloc.type == BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP;
2309 /* Assign the immediate value to the relavant field in *OPERAND if
2310 RELOC->EXP is a constant expression; otherwise, flag that *OPERAND
2311 needs an internal fixup in a later stage.
2312 ADDR_OFF_P determines whether it is the field ADDR.OFFSET.IMM or
2313 IMM.VALUE that may get assigned with the constant. */
2315 assign_imm_if_const_or_fixup_later (struct reloc *reloc,
2316 aarch64_opnd_info *operand,
2318 int need_libopcodes_p,
2321 if (reloc->exp.X_op == O_constant)
2324 operand->addr.offset.imm = reloc->exp.X_add_number;
2326 operand->imm.value = reloc->exp.X_add_number;
2327 reloc->type = BFD_RELOC_UNUSED;
2331 aarch64_set_gas_internal_fixup (reloc, operand, need_libopcodes_p);
2332 /* Tell libopcodes to ignore this operand or not. This is helpful
2333 when one of the operands needs to be fixed up later but we need
2334 libopcodes to check the other operands. */
2335 operand->skip = skip_p;
2339 /* Relocation modifiers. Each entry in the table contains the textual
2340 name for the relocation which may be placed before a symbol used as
2341 a load/store offset, or add immediate. It must be surrounded by a
2342 leading and trailing colon, for example:
2344 ldr x0, [x1, #:rello:varsym]
2345 add x0, x1, #:rello:varsym */
2347 struct reloc_table_entry
2351 bfd_reloc_code_real_type adr_type;
2352 bfd_reloc_code_real_type adrp_type;
2353 bfd_reloc_code_real_type movw_type;
2354 bfd_reloc_code_real_type add_type;
2355 bfd_reloc_code_real_type ldst_type;
2356 bfd_reloc_code_real_type ld_literal_type;
2359 static struct reloc_table_entry reloc_table[] = {
2360 /* Low 12 bits of absolute address: ADD/i and LDR/STR */
2365 BFD_RELOC_AARCH64_ADD_LO12,
2366 BFD_RELOC_AARCH64_LDST_LO12,
2369 /* Higher 21 bits of pc-relative page offset: ADRP */
2372 BFD_RELOC_AARCH64_ADR_HI21_PCREL,
2378 /* Higher 21 bits of pc-relative page offset: ADRP, no check */
2381 BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL,
2387 /* Most significant bits 0-15 of unsigned address/value: MOVZ */
2391 BFD_RELOC_AARCH64_MOVW_G0,
2396 /* Most significant bits 0-15 of signed address/value: MOVN/Z */
2400 BFD_RELOC_AARCH64_MOVW_G0_S,
2405 /* Less significant bits 0-15 of address/value: MOVK, no check */
2409 BFD_RELOC_AARCH64_MOVW_G0_NC,
2414 /* Most significant bits 16-31 of unsigned address/value: MOVZ */
2418 BFD_RELOC_AARCH64_MOVW_G1,
2423 /* Most significant bits 16-31 of signed address/value: MOVN/Z */
2427 BFD_RELOC_AARCH64_MOVW_G1_S,
2432 /* Less significant bits 16-31 of address/value: MOVK, no check */
2436 BFD_RELOC_AARCH64_MOVW_G1_NC,
2441 /* Most significant bits 32-47 of unsigned address/value: MOVZ */
2445 BFD_RELOC_AARCH64_MOVW_G2,
2450 /* Most significant bits 32-47 of signed address/value: MOVN/Z */
2454 BFD_RELOC_AARCH64_MOVW_G2_S,
2459 /* Less significant bits 32-47 of address/value: MOVK, no check */
2463 BFD_RELOC_AARCH64_MOVW_G2_NC,
2468 /* Most significant bits 48-63 of signed/unsigned address/value: MOVZ */
2472 BFD_RELOC_AARCH64_MOVW_G3,
2477 /* Get to the page containing GOT entry for a symbol. */
2480 BFD_RELOC_AARCH64_ADR_GOT_PAGE,
2484 BFD_RELOC_AARCH64_GOT_LD_PREL19},
2486 /* 12 bit offset into the page containing GOT entry for that symbol. */
2492 BFD_RELOC_AARCH64_LD_GOT_LO12_NC,
2495 /* 0-15 bits of address/value: MOVk, no check. */
2499 BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC,
2504 /* Most significant bits 16-31 of address/value: MOVZ. */
2508 BFD_RELOC_AARCH64_MOVW_GOTOFF_G1,
2513 /* 15 bit offset into the page containing GOT entry for that symbol. */
2519 BFD_RELOC_AARCH64_LD64_GOTOFF_LO15,
2522 /* Get to the page containing GOT TLS entry for a symbol */
2523 {"gottprel_g0_nc", 0,
2526 BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC,
2531 /* Get to the page containing GOT TLS entry for a symbol */
2535 BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1,
2540 /* Get to the page containing GOT TLS entry for a symbol */
2542 BFD_RELOC_AARCH64_TLSGD_ADR_PREL21, /* adr_type */
2543 BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21,
2549 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2554 BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC,
2558 /* Lower 16 bits address/value: MOVk. */
2562 BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC,
2567 /* Most significant bits 16-31 of address/value: MOVZ. */
2571 BFD_RELOC_AARCH64_TLSGD_MOVW_G1,
2576 /* Get to the page containing GOT TLS entry for a symbol */
2578 BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21, /* adr_type */
2579 BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21,
2583 BFD_RELOC_AARCH64_TLSDESC_LD_PREL19},
2585 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2590 BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC,
2591 BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC,
2594 /* Get to the page containing GOT TLS entry for a symbol.
2595 The same as GD, we allocate two consecutive GOT slots
2596 for module index and module offset, the only difference
2597 with GD is the module offset should be intialized to
2598 zero without any outstanding runtime relocation. */
2600 BFD_RELOC_AARCH64_TLSLD_ADR_PREL21, /* adr_type */
2601 BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21,
2607 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2608 {"tlsldm_lo12_nc", 0,
2612 BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC,
2616 /* 12 bit offset into the module TLS base address. */
2621 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12,
2622 BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12,
2625 /* Same as dtprel_lo12, no overflow check. */
2626 {"dtprel_lo12_nc", 0,
2630 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC,
2631 BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC,
2634 /* bits[23:12] of offset to the module TLS base address. */
2639 BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12,
2643 /* bits[15:0] of offset to the module TLS base address. */
2647 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0,
2652 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0. */
2656 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC,
2661 /* bits[31:16] of offset to the module TLS base address. */
2665 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1,
2670 /* No overflow check version of BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1. */
2674 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC,
2679 /* bits[47:32] of offset to the module TLS base address. */
2683 BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2,
2688 /* Lower 16 bit offset into GOT entry for a symbol */
2689 {"tlsdesc_off_g0_nc", 0,
2692 BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC,
2697 /* Higher 16 bit offset into GOT entry for a symbol */
2698 {"tlsdesc_off_g1", 0,
2701 BFD_RELOC_AARCH64_TLSDESC_OFF_G1,
2706 /* Get to the page containing GOT TLS entry for a symbol */
2709 BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21,
2713 BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19},
2715 /* 12 bit offset into the page containing GOT TLS entry for a symbol */
2716 {"gottprel_lo12", 0,
2721 BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC,
2724 /* Get tp offset for a symbol. */
2729 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12,
2733 /* Get tp offset for a symbol. */
2738 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12,
2742 /* Get tp offset for a symbol. */
2747 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12,
2751 /* Get tp offset for a symbol. */
2752 {"tprel_lo12_nc", 0,
2756 BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC,
2760 /* Most significant bits 32-47 of address/value: MOVZ. */
2764 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2,
2769 /* Most significant bits 16-31 of address/value: MOVZ. */
2773 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1,
2778 /* Most significant bits 16-31 of address/value: MOVZ, no check. */
2782 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC,
2787 /* Most significant bits 0-15 of address/value: MOVZ. */
2791 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0,
2796 /* Most significant bits 0-15 of address/value: MOVZ, no check. */
2800 BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC,
2805 /* 15bit offset from got entry to base address of GOT table. */
2811 BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15,
2814 /* 14bit offset from got entry to base address of GOT table. */
2820 BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14,
2824 /* Given the address of a pointer pointing to the textual name of a
2825 relocation as may appear in assembler source, attempt to find its
2826 details in reloc_table. The pointer will be updated to the character
2827 after the trailing colon. On failure, NULL will be returned;
2828 otherwise return the reloc_table_entry. */
2830 static struct reloc_table_entry *
2831 find_reloc_table_entry (char **str)
2834 for (i = 0; i < ARRAY_SIZE (reloc_table); i++)
2836 int length = strlen (reloc_table[i].name);
2838 if (strncasecmp (reloc_table[i].name, *str, length) == 0
2839 && (*str)[length] == ':')
2841 *str += (length + 1);
2842 return &reloc_table[i];
2849 /* Mode argument to parse_shift and parser_shifter_operand. */
2850 enum parse_shift_mode
2852 SHIFTED_ARITH_IMM, /* "rn{,lsl|lsr|asl|asr|uxt|sxt #n}" or
2854 SHIFTED_LOGIC_IMM, /* "rn{,lsl|lsr|asl|asr|ror #n}" or
2856 SHIFTED_LSL, /* bare "lsl #n" */
2857 SHIFTED_LSL_MSL, /* "lsl|msl #n" */
2858 SHIFTED_REG_OFFSET /* [su]xtw|sxtx {#n} or lsl #n */
2861 /* Parse a <shift> operator on an AArch64 data processing instruction.
2862 Return TRUE on success; otherwise return FALSE. */
2864 parse_shift (char **str, aarch64_opnd_info *operand, enum parse_shift_mode mode)
2866 const struct aarch64_name_value_pair *shift_op;
2867 enum aarch64_modifier_kind kind;
2873 for (p = *str; ISALPHA (*p); p++)
2878 set_syntax_error (_("shift expression expected"));
2882 shift_op = hash_find_n (aarch64_shift_hsh, *str, p - *str);
2884 if (shift_op == NULL)
2886 set_syntax_error (_("shift operator expected"));
2890 kind = aarch64_get_operand_modifier (shift_op);
2892 if (kind == AARCH64_MOD_MSL && mode != SHIFTED_LSL_MSL)
2894 set_syntax_error (_("invalid use of 'MSL'"));
2900 case SHIFTED_LOGIC_IMM:
2901 if (aarch64_extend_operator_p (kind) == TRUE)
2903 set_syntax_error (_("extending shift is not permitted"));
2908 case SHIFTED_ARITH_IMM:
2909 if (kind == AARCH64_MOD_ROR)
2911 set_syntax_error (_("'ROR' shift is not permitted"));
2917 if (kind != AARCH64_MOD_LSL)
2919 set_syntax_error (_("only 'LSL' shift is permitted"));
2924 case SHIFTED_REG_OFFSET:
2925 if (kind != AARCH64_MOD_UXTW && kind != AARCH64_MOD_LSL
2926 && kind != AARCH64_MOD_SXTW && kind != AARCH64_MOD_SXTX)
2928 set_fatal_syntax_error
2929 (_("invalid shift for the register offset addressing mode"));
2934 case SHIFTED_LSL_MSL:
2935 if (kind != AARCH64_MOD_LSL && kind != AARCH64_MOD_MSL)
2937 set_syntax_error (_("invalid shift operator"));
2946 /* Whitespace can appear here if the next thing is a bare digit. */
2947 skip_whitespace (p);
2949 /* Parse shift amount. */
2951 if (mode == SHIFTED_REG_OFFSET && *p == ']')
2952 exp.X_op = O_absent;
2955 if (is_immediate_prefix (*p))
2960 my_get_expression (&exp, &p, GE_NO_PREFIX, 0);
2962 if (exp.X_op == O_absent)
2964 if (aarch64_extend_operator_p (kind) == FALSE || exp_has_prefix)
2966 set_syntax_error (_("missing shift amount"));
2969 operand->shifter.amount = 0;
2971 else if (exp.X_op != O_constant)
2973 set_syntax_error (_("constant shift amount required"));
2976 else if (exp.X_add_number < 0 || exp.X_add_number > 63)
2978 set_fatal_syntax_error (_("shift amount out of range 0 to 63"));
2983 operand->shifter.amount = exp.X_add_number;
2984 operand->shifter.amount_present = 1;
2987 operand->shifter.operator_present = 1;
2988 operand->shifter.kind = kind;
2994 /* Parse a <shifter_operand> for a data processing instruction:
2997 #<immediate>, LSL #imm
2999 Validation of immediate operands is deferred to md_apply_fix.
3001 Return TRUE on success; otherwise return FALSE. */
3004 parse_shifter_operand_imm (char **str, aarch64_opnd_info *operand,
3005 enum parse_shift_mode mode)
3009 if (mode != SHIFTED_ARITH_IMM && mode != SHIFTED_LOGIC_IMM)
3014 /* Accept an immediate expression. */
3015 if (! my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX, 1))
3018 /* Accept optional LSL for arithmetic immediate values. */
3019 if (mode == SHIFTED_ARITH_IMM && skip_past_comma (&p))
3020 if (! parse_shift (&p, operand, SHIFTED_LSL))
3023 /* Not accept any shifter for logical immediate values. */
3024 if (mode == SHIFTED_LOGIC_IMM && skip_past_comma (&p)
3025 && parse_shift (&p, operand, mode))
3027 set_syntax_error (_("unexpected shift operator"));
3035 /* Parse a <shifter_operand> for a data processing instruction:
3040 #<immediate>, LSL #imm
3042 where <shift> is handled by parse_shift above, and the last two
3043 cases are handled by the function above.
3045 Validation of immediate operands is deferred to md_apply_fix.
3047 Return TRUE on success; otherwise return FALSE. */
3050 parse_shifter_operand (char **str, aarch64_opnd_info *operand,
3051 enum parse_shift_mode mode)
3054 int isreg32, isregzero;
3055 enum aarch64_operand_class opd_class
3056 = aarch64_get_operand_class (operand->type);
3059 aarch64_reg_parse_32_64 (str, 0, 0, &isreg32, &isregzero)) != PARSE_FAIL)
3061 if (opd_class == AARCH64_OPND_CLASS_IMMEDIATE)
3063 set_syntax_error (_("unexpected register in the immediate operand"));
3067 if (!isregzero && reg == REG_SP)
3069 set_syntax_error (BAD_SP);
3073 operand->reg.regno = reg;
3074 operand->qualifier = isreg32 ? AARCH64_OPND_QLF_W : AARCH64_OPND_QLF_X;
3076 /* Accept optional shift operation on register. */
3077 if (! skip_past_comma (str))
3080 if (! parse_shift (str, operand, mode))
3085 else if (opd_class == AARCH64_OPND_CLASS_MODIFIED_REG)
3088 (_("integer register expected in the extended/shifted operand "
3093 /* We have a shifted immediate variable. */
3094 return parse_shifter_operand_imm (str, operand, mode);
3097 /* Return TRUE on success; return FALSE otherwise. */
3100 parse_shifter_operand_reloc (char **str, aarch64_opnd_info *operand,
3101 enum parse_shift_mode mode)
3105 /* Determine if we have the sequence of characters #: or just :
3106 coming next. If we do, then we check for a :rello: relocation
3107 modifier. If we don't, punt the whole lot to
3108 parse_shifter_operand. */
3110 if ((p[0] == '#' && p[1] == ':') || p[0] == ':')
3112 struct reloc_table_entry *entry;
3120 /* Try to parse a relocation. Anything else is an error. */
3121 if (!(entry = find_reloc_table_entry (str)))
3123 set_syntax_error (_("unknown relocation modifier"));
3127 if (entry->add_type == 0)
3130 (_("this relocation modifier is not allowed on this instruction"));
3134 /* Save str before we decompose it. */
3137 /* Next, we parse the expression. */
3138 if (! my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX, 1))
3141 /* Record the relocation type (use the ADD variant here). */
3142 inst.reloc.type = entry->add_type;
3143 inst.reloc.pc_rel = entry->pc_rel;
3145 /* If str is empty, we've reached the end, stop here. */
3149 /* Otherwise, we have a shifted reloc modifier, so rewind to
3150 recover the variable name and continue parsing for the shifter. */
3152 return parse_shifter_operand_imm (str, operand, mode);
3155 return parse_shifter_operand (str, operand, mode);
3158 /* Parse all forms of an address expression. Information is written
3159 to *OPERAND and/or inst.reloc.
3161 The A64 instruction set has the following addressing modes:
3164 [base] // in SIMD ld/st structure
3165 [base{,#0}] // in ld/st exclusive
3167 [base,Xm{,LSL #imm}]
3168 [base,Xm,SXTX {#imm}]
3169 [base,Wm,(S|U)XTW {#imm}]
3174 [base],Xm // in SIMD ld/st structure
3175 PC-relative (literal)
3179 (As a convenience, the notation "=immediate" is permitted in conjunction
3180 with the pc-relative literal load instructions to automatically place an
3181 immediate value or symbolic address in a nearby literal pool and generate
3182 a hidden label which references it.)
3184 Upon a successful parsing, the address structure in *OPERAND will be
3185 filled in the following way:
3187 .base_regno = <base>
3188 .offset.is_reg // 1 if the offset is a register
3190 .offset.regno = <Rm>
3192 For different addressing modes defined in the A64 ISA:
3195 .pcrel=0; .preind=1; .postind=0; .writeback=0
3197 .pcrel=0; .preind=1; .postind=0; .writeback=1
3199 .pcrel=0; .preind=0; .postind=1; .writeback=1
3200 PC-relative (literal)
3201 .pcrel=1; .preind=1; .postind=0; .writeback=0
3203 The shift/extension information, if any, will be stored in .shifter.
3205 It is the caller's responsibility to check for addressing modes not
3206 supported by the instruction, and to set inst.reloc.type. */
3209 parse_address_main (char **str, aarch64_opnd_info *operand, int reloc,
3210 int accept_reg_post_index)
3214 int isreg32, isregzero;
3215 expressionS *exp = &inst.reloc.exp;
3217 if (! skip_past_char (&p, '['))
3219 /* =immediate or label. */
3220 operand->addr.pcrel = 1;
3221 operand->addr.preind = 1;
3223 /* #:<reloc_op>:<symbol> */
3224 skip_past_char (&p, '#');
3225 if (reloc && skip_past_char (&p, ':'))
3227 bfd_reloc_code_real_type ty;
3228 struct reloc_table_entry *entry;
3230 /* Try to parse a relocation modifier. Anything else is
3232 entry = find_reloc_table_entry (&p);
3235 set_syntax_error (_("unknown relocation modifier"));
3239 switch (operand->type)
3241 case AARCH64_OPND_ADDR_PCREL21:
3243 ty = entry->adr_type;
3247 ty = entry->ld_literal_type;
3254 (_("this relocation modifier is not allowed on this "
3260 if (! my_get_expression (exp, &p, GE_NO_PREFIX, 1))
3262 set_syntax_error (_("invalid relocation expression"));
3266 /* #:<reloc_op>:<expr> */
3267 /* Record the relocation type. */
3268 inst.reloc.type = ty;
3269 inst.reloc.pc_rel = entry->pc_rel;
3274 if (skip_past_char (&p, '='))
3275 /* =immediate; need to generate the literal in the literal pool. */
3276 inst.gen_lit_pool = 1;
3278 if (!my_get_expression (exp, &p, GE_NO_PREFIX, 1))
3280 set_syntax_error (_("invalid address"));
3291 /* Accept SP and reject ZR */
3292 reg = aarch64_reg_parse_32_64 (&p, 0, 1, &isreg32, &isregzero);
3293 if (reg == PARSE_FAIL || isreg32)
3295 set_syntax_error (_(get_reg_expected_msg (REG_TYPE_R_64)));
3298 operand->addr.base_regno = reg;
3301 if (skip_past_comma (&p))
3304 operand->addr.preind = 1;
3306 /* Reject SP and accept ZR */
3307 reg = aarch64_reg_parse_32_64 (&p, 1, 0, &isreg32, &isregzero);
3308 if (reg != PARSE_FAIL)
3311 operand->addr.offset.regno = reg;
3312 operand->addr.offset.is_reg = 1;
3313 /* Shifted index. */
3314 if (skip_past_comma (&p))
3317 if (! parse_shift (&p, operand, SHIFTED_REG_OFFSET))
3318 /* Use the diagnostics set in parse_shift, so not set new
3319 error message here. */
3323 [base,Xm{,LSL #imm}]
3324 [base,Xm,SXTX {#imm}]
3325 [base,Wm,(S|U)XTW {#imm}] */
3326 if (operand->shifter.kind == AARCH64_MOD_NONE
3327 || operand->shifter.kind == AARCH64_MOD_LSL
3328 || operand->shifter.kind == AARCH64_MOD_SXTX)
3332 set_syntax_error (_("invalid use of 32-bit register offset"));
3338 set_syntax_error (_("invalid use of 64-bit register offset"));
3344 /* [Xn,#:<reloc_op>:<symbol> */
3345 skip_past_char (&p, '#');
3346 if (reloc && skip_past_char (&p, ':'))
3348 struct reloc_table_entry *entry;
3350 /* Try to parse a relocation modifier. Anything else is
3352 if (!(entry = find_reloc_table_entry (&p)))
3354 set_syntax_error (_("unknown relocation modifier"));
3358 if (entry->ldst_type == 0)
3361 (_("this relocation modifier is not allowed on this "
3366 /* [Xn,#:<reloc_op>: */
3367 /* We now have the group relocation table entry corresponding to
3368 the name in the assembler source. Next, we parse the
3370 if (! my_get_expression (exp, &p, GE_NO_PREFIX, 1))
3372 set_syntax_error (_("invalid relocation expression"));
3376 /* [Xn,#:<reloc_op>:<expr> */
3377 /* Record the load/store relocation type. */
3378 inst.reloc.type = entry->ldst_type;
3379 inst.reloc.pc_rel = entry->pc_rel;
3381 else if (! my_get_expression (exp, &p, GE_OPT_PREFIX, 1))
3383 set_syntax_error (_("invalid expression in the address"));
3390 if (! skip_past_char (&p, ']'))
3392 set_syntax_error (_("']' expected"));
3396 if (skip_past_char (&p, '!'))
3398 if (operand->addr.preind && operand->addr.offset.is_reg)
3400 set_syntax_error (_("register offset not allowed in pre-indexed "
3401 "addressing mode"));
3405 operand->addr.writeback = 1;
3407 else if (skip_past_comma (&p))
3410 operand->addr.postind = 1;
3411 operand->addr.writeback = 1;
3413 if (operand->addr.preind)
3415 set_syntax_error (_("cannot combine pre- and post-indexing"));
3419 if (accept_reg_post_index
3420 && (reg = aarch64_reg_parse_32_64 (&p, 1, 1, &isreg32,
3421 &isregzero)) != PARSE_FAIL)
3426 set_syntax_error (_("invalid 32-bit register offset"));
3429 operand->addr.offset.regno = reg;
3430 operand->addr.offset.is_reg = 1;
3432 else if (! my_get_expression (exp, &p, GE_OPT_PREFIX, 1))
3435 set_syntax_error (_("invalid expression in the address"));
3440 /* If at this point neither .preind nor .postind is set, we have a
3441 bare [Rn]{!}; reject [Rn]! but accept [Rn] as a shorthand for [Rn,#0]. */
3442 if (operand->addr.preind == 0 && operand->addr.postind == 0)
3444 if (operand->addr.writeback)
3447 set_syntax_error (_("missing offset in the pre-indexed address"));
3450 operand->addr.preind = 1;
3451 inst.reloc.exp.X_op = O_constant;
3452 inst.reloc.exp.X_add_number = 0;
3459 /* Return TRUE on success; otherwise return FALSE. */
3461 parse_address (char **str, aarch64_opnd_info *operand,
3462 int accept_reg_post_index)
3464 return parse_address_main (str, operand, 0, accept_reg_post_index);
3467 /* Return TRUE on success; otherwise return FALSE. */
3469 parse_address_reloc (char **str, aarch64_opnd_info *operand)
3471 return parse_address_main (str, operand, 1, 0);
3474 /* Parse an operand for a MOVZ, MOVN or MOVK instruction.
3475 Return TRUE on success; otherwise return FALSE. */
3477 parse_half (char **str, int *internal_fixup_p)
3481 skip_past_char (&p, '#');
3483 gas_assert (internal_fixup_p);
3484 *internal_fixup_p = 0;
3488 struct reloc_table_entry *entry;
3490 /* Try to parse a relocation. Anything else is an error. */
3492 if (!(entry = find_reloc_table_entry (&p)))
3494 set_syntax_error (_("unknown relocation modifier"));
3498 if (entry->movw_type == 0)
3501 (_("this relocation modifier is not allowed on this instruction"));
3505 inst.reloc.type = entry->movw_type;
3508 *internal_fixup_p = 1;
3510 if (! my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX, 1))
3517 /* Parse an operand for an ADRP instruction:
3519 Return TRUE on success; otherwise return FALSE. */
3522 parse_adrp (char **str)
3529 struct reloc_table_entry *entry;
3531 /* Try to parse a relocation. Anything else is an error. */
3533 if (!(entry = find_reloc_table_entry (&p)))
3535 set_syntax_error (_("unknown relocation modifier"));
3539 if (entry->adrp_type == 0)
3542 (_("this relocation modifier is not allowed on this instruction"));
3546 inst.reloc.type = entry->adrp_type;
3549 inst.reloc.type = BFD_RELOC_AARCH64_ADR_HI21_PCREL;
3551 inst.reloc.pc_rel = 1;
3553 if (! my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX, 1))
3560 /* Miscellaneous. */
3562 /* Parse an option for a preload instruction. Returns the encoding for the
3563 option, or PARSE_FAIL. */
3566 parse_pldop (char **str)
3569 const struct aarch64_name_value_pair *o;
3572 while (ISALNUM (*q))
3575 o = hash_find_n (aarch64_pldop_hsh, p, q - p);
3583 /* Parse an option for a barrier instruction. Returns the encoding for the
3584 option, or PARSE_FAIL. */
3587 parse_barrier (char **str)
3590 const asm_barrier_opt *o;
3593 while (ISALPHA (*q))
3596 o = hash_find_n (aarch64_barrier_opt_hsh, p, q - p);
3604 /* Parse an operand for a PSB barrier. Set *HINT_OPT to the hint-option record
3605 return 0 if successful. Otherwise return PARSE_FAIL. */
3608 parse_barrier_psb (char **str,
3609 const struct aarch64_name_value_pair ** hint_opt)
3612 const struct aarch64_name_value_pair *o;
3615 while (ISALPHA (*q))
3618 o = hash_find_n (aarch64_hint_opt_hsh, p, q - p);
3621 set_fatal_syntax_error
3622 ( _("unknown or missing option to PSB"));
3626 if (o->value != 0x11)
3628 /* PSB only accepts option name 'CSYNC'. */
3630 (_("the specified option is not accepted for PSB"));
3639 /* Parse a system register or a PSTATE field name for an MSR/MRS instruction.
3640 Returns the encoding for the option, or PARSE_FAIL.
3642 If IMPLE_DEFINED_P is non-zero, the function will also try to parse the
3643 implementation defined system register name S<op0>_<op1>_<Cn>_<Cm>_<op2>.
3645 If PSTATEFIELD_P is non-zero, the function will parse the name as a PSTATE
3646 field, otherwise as a system register.
3650 parse_sys_reg (char **str, struct hash_control *sys_regs,
3651 int imple_defined_p, int pstatefield_p)
3655 const aarch64_sys_reg *o;
3659 for (q = *str; ISALNUM (*q) || *q == '_'; q++)
3661 *p++ = TOLOWER (*q);
3663 /* Assert that BUF be large enough. */
3664 gas_assert (p - buf == q - *str);
3666 o = hash_find (sys_regs, buf);
3669 if (!imple_defined_p)
3673 /* Parse S<op0>_<op1>_<Cn>_<Cm>_<op2>. */
3674 unsigned int op0, op1, cn, cm, op2;
3676 if (sscanf (buf, "s%u_%u_c%u_c%u_%u", &op0, &op1, &cn, &cm, &op2)
3679 if (op0 > 3 || op1 > 7 || cn > 15 || cm > 15 || op2 > 7)
3681 value = (op0 << 14) | (op1 << 11) | (cn << 7) | (cm << 3) | op2;
3686 if (pstatefield_p && !aarch64_pstatefield_supported_p (cpu_variant, o))
3687 as_bad (_("selected processor does not support PSTATE field "
3689 if (!pstatefield_p && !aarch64_sys_reg_supported_p (cpu_variant, o))
3690 as_bad (_("selected processor does not support system register "
3692 if (aarch64_sys_reg_deprecated_p (o))
3693 as_warn (_("system register name '%s' is deprecated and may be "
3694 "removed in a future release"), buf);
3702 /* Parse a system reg for ic/dc/at/tlbi instructions. Returns the table entry
3703 for the option, or NULL. */
3705 static const aarch64_sys_ins_reg *
3706 parse_sys_ins_reg (char **str, struct hash_control *sys_ins_regs)
3710 const aarch64_sys_ins_reg *o;
3713 for (q = *str; ISALNUM (*q) || *q == '_'; q++)
3715 *p++ = TOLOWER (*q);
3718 o = hash_find (sys_ins_regs, buf);
3722 if (!aarch64_sys_ins_reg_supported_p (cpu_variant, o))
3723 as_bad (_("selected processor does not support system register "
3730 #define po_char_or_fail(chr) do { \
3731 if (! skip_past_char (&str, chr)) \
3735 #define po_reg_or_fail(regtype) do { \
3736 val = aarch64_reg_parse (&str, regtype, &rtype, NULL); \
3737 if (val == PARSE_FAIL) \
3739 set_default_error (); \
3744 #define po_int_reg_or_fail(reject_sp, reject_rz) do { \
3745 val = aarch64_reg_parse_32_64 (&str, reject_sp, reject_rz, \
3746 &isreg32, &isregzero); \
3747 if (val == PARSE_FAIL) \
3749 set_default_error (); \
3752 info->reg.regno = val; \
3754 info->qualifier = AARCH64_OPND_QLF_W; \
3756 info->qualifier = AARCH64_OPND_QLF_X; \
3759 #define po_imm_nc_or_fail() do { \
3760 if (! parse_constant_immediate (&str, &val)) \
3764 #define po_imm_or_fail(min, max) do { \
3765 if (! parse_constant_immediate (&str, &val)) \
3767 if (val < min || val > max) \
3769 set_fatal_syntax_error (_("immediate value out of range "\
3770 #min " to "#max)); \
3775 #define po_misc_or_fail(expr) do { \
3780 /* encode the 12-bit imm field of Add/sub immediate */
3781 static inline uint32_t
3782 encode_addsub_imm (uint32_t imm)
3787 /* encode the shift amount field of Add/sub immediate */
3788 static inline uint32_t
3789 encode_addsub_imm_shift_amount (uint32_t cnt)
3795 /* encode the imm field of Adr instruction */
3796 static inline uint32_t
3797 encode_adr_imm (uint32_t imm)
3799 return (((imm & 0x3) << 29) /* [1:0] -> [30:29] */
3800 | ((imm & (0x7ffff << 2)) << 3)); /* [20:2] -> [23:5] */
3803 /* encode the immediate field of Move wide immediate */
3804 static inline uint32_t
3805 encode_movw_imm (uint32_t imm)
3810 /* encode the 26-bit offset of unconditional branch */
3811 static inline uint32_t
3812 encode_branch_ofs_26 (uint32_t ofs)
3814 return ofs & ((1 << 26) - 1);
3817 /* encode the 19-bit offset of conditional branch and compare & branch */
3818 static inline uint32_t
3819 encode_cond_branch_ofs_19 (uint32_t ofs)
3821 return (ofs & ((1 << 19) - 1)) << 5;
3824 /* encode the 19-bit offset of ld literal */
3825 static inline uint32_t
3826 encode_ld_lit_ofs_19 (uint32_t ofs)
3828 return (ofs & ((1 << 19) - 1)) << 5;
3831 /* Encode the 14-bit offset of test & branch. */
3832 static inline uint32_t
3833 encode_tst_branch_ofs_14 (uint32_t ofs)
3835 return (ofs & ((1 << 14) - 1)) << 5;
3838 /* Encode the 16-bit imm field of svc/hvc/smc. */
3839 static inline uint32_t
3840 encode_svc_imm (uint32_t imm)
3845 /* Reencode add(s) to sub(s), or sub(s) to add(s). */
3846 static inline uint32_t
3847 reencode_addsub_switch_add_sub (uint32_t opcode)
3849 return opcode ^ (1 << 30);
3852 static inline uint32_t
3853 reencode_movzn_to_movz (uint32_t opcode)
3855 return opcode | (1 << 30);
3858 static inline uint32_t
3859 reencode_movzn_to_movn (uint32_t opcode)
3861 return opcode & ~(1 << 30);
3864 /* Overall per-instruction processing. */
3866 /* We need to be able to fix up arbitrary expressions in some statements.
3867 This is so that we can handle symbols that are an arbitrary distance from
3868 the pc. The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
3869 which returns part of an address in a form which will be valid for
3870 a data instruction. We do this by pushing the expression into a symbol
3871 in the expr_section, and creating a fix for that. */
3874 fix_new_aarch64 (fragS * frag,
3876 short int size, expressionS * exp, int pc_rel, int reloc)
3886 new_fix = fix_new_exp (frag, where, size, exp, pc_rel, reloc);
3890 new_fix = fix_new (frag, where, size, make_expr_symbol (exp), 0,
3897 /* Diagnostics on operands errors. */
3899 /* By default, output verbose error message.
3900 Disable the verbose error message by -mno-verbose-error. */
3901 static int verbose_error_p = 1;
3903 #ifdef DEBUG_AARCH64
3904 /* N.B. this is only for the purpose of debugging. */
3905 const char* operand_mismatch_kind_names[] =
3908 "AARCH64_OPDE_RECOVERABLE",
3909 "AARCH64_OPDE_SYNTAX_ERROR",
3910 "AARCH64_OPDE_FATAL_SYNTAX_ERROR",
3911 "AARCH64_OPDE_INVALID_VARIANT",
3912 "AARCH64_OPDE_OUT_OF_RANGE",
3913 "AARCH64_OPDE_UNALIGNED",
3914 "AARCH64_OPDE_REG_LIST",
3915 "AARCH64_OPDE_OTHER_ERROR",
3917 #endif /* DEBUG_AARCH64 */
3919 /* Return TRUE if LHS is of higher severity than RHS, otherwise return FALSE.
3921 When multiple errors of different kinds are found in the same assembly
3922 line, only the error of the highest severity will be picked up for
3923 issuing the diagnostics. */
3925 static inline bfd_boolean
3926 operand_error_higher_severity_p (enum aarch64_operand_error_kind lhs,
3927 enum aarch64_operand_error_kind rhs)
3929 gas_assert (AARCH64_OPDE_RECOVERABLE > AARCH64_OPDE_NIL);
3930 gas_assert (AARCH64_OPDE_SYNTAX_ERROR > AARCH64_OPDE_RECOVERABLE);
3931 gas_assert (AARCH64_OPDE_FATAL_SYNTAX_ERROR > AARCH64_OPDE_SYNTAX_ERROR);
3932 gas_assert (AARCH64_OPDE_INVALID_VARIANT > AARCH64_OPDE_FATAL_SYNTAX_ERROR);
3933 gas_assert (AARCH64_OPDE_OUT_OF_RANGE > AARCH64_OPDE_INVALID_VARIANT);
3934 gas_assert (AARCH64_OPDE_UNALIGNED > AARCH64_OPDE_OUT_OF_RANGE);
3935 gas_assert (AARCH64_OPDE_REG_LIST > AARCH64_OPDE_UNALIGNED);
3936 gas_assert (AARCH64_OPDE_OTHER_ERROR > AARCH64_OPDE_REG_LIST);
3940 /* Helper routine to get the mnemonic name from the assembly instruction
3941 line; should only be called for the diagnosis purpose, as there is
3942 string copy operation involved, which may affect the runtime
3943 performance if used in elsewhere. */
3946 get_mnemonic_name (const char *str)
3948 static char mnemonic[32];
3951 /* Get the first 15 bytes and assume that the full name is included. */
3952 strncpy (mnemonic, str, 31);
3953 mnemonic[31] = '\0';
3955 /* Scan up to the end of the mnemonic, which must end in white space,
3956 '.', or end of string. */
3957 for (ptr = mnemonic; is_part_of_name(*ptr); ++ptr)
3962 /* Append '...' to the truncated long name. */
3963 if (ptr - mnemonic == 31)
3964 mnemonic[28] = mnemonic[29] = mnemonic[30] = '.';
3970 reset_aarch64_instruction (aarch64_instruction *instruction)
3972 memset (instruction, '\0', sizeof (aarch64_instruction));
3973 instruction->reloc.type = BFD_RELOC_UNUSED;
3976 /* Data strutures storing one user error in the assembly code related to
3979 struct operand_error_record
3981 const aarch64_opcode *opcode;
3982 aarch64_operand_error detail;
3983 struct operand_error_record *next;
3986 typedef struct operand_error_record operand_error_record;
3988 struct operand_errors
3990 operand_error_record *head;
3991 operand_error_record *tail;
3994 typedef struct operand_errors operand_errors;
3996 /* Top-level data structure reporting user errors for the current line of
3998 The way md_assemble works is that all opcodes sharing the same mnemonic
3999 name are iterated to find a match to the assembly line. In this data
4000 structure, each of the such opcodes will have one operand_error_record
4001 allocated and inserted. In other words, excessive errors related with
4002 a single opcode are disregarded. */
4003 operand_errors operand_error_report;
4005 /* Free record nodes. */
4006 static operand_error_record *free_opnd_error_record_nodes = NULL;
4008 /* Initialize the data structure that stores the operand mismatch
4009 information on assembling one line of the assembly code. */
4011 init_operand_error_report (void)
4013 if (operand_error_report.head != NULL)
4015 gas_assert (operand_error_report.tail != NULL);
4016 operand_error_report.tail->next = free_opnd_error_record_nodes;
4017 free_opnd_error_record_nodes = operand_error_report.head;
4018 operand_error_report.head = NULL;
4019 operand_error_report.tail = NULL;
4022 gas_assert (operand_error_report.tail == NULL);
4025 /* Return TRUE if some operand error has been recorded during the
4026 parsing of the current assembly line using the opcode *OPCODE;
4027 otherwise return FALSE. */
4028 static inline bfd_boolean
4029 opcode_has_operand_error_p (const aarch64_opcode *opcode)
4031 operand_error_record *record = operand_error_report.head;
4032 return record && record->opcode == opcode;
4035 /* Add the error record *NEW_RECORD to operand_error_report. The record's
4036 OPCODE field is initialized with OPCODE.
4037 N.B. only one record for each opcode, i.e. the maximum of one error is
4038 recorded for each instruction template. */
4041 add_operand_error_record (const operand_error_record* new_record)
4043 const aarch64_opcode *opcode = new_record->opcode;
4044 operand_error_record* record = operand_error_report.head;
4046 /* The record may have been created for this opcode. If not, we need
4048 if (! opcode_has_operand_error_p (opcode))
4050 /* Get one empty record. */
4051 if (free_opnd_error_record_nodes == NULL)
4053 record = XNEW (operand_error_record);
4057 record = free_opnd_error_record_nodes;
4058 free_opnd_error_record_nodes = record->next;
4060 record->opcode = opcode;
4061 /* Insert at the head. */
4062 record->next = operand_error_report.head;
4063 operand_error_report.head = record;
4064 if (operand_error_report.tail == NULL)
4065 operand_error_report.tail = record;
4067 else if (record->detail.kind != AARCH64_OPDE_NIL
4068 && record->detail.index <= new_record->detail.index
4069 && operand_error_higher_severity_p (record->detail.kind,
4070 new_record->detail.kind))
4072 /* In the case of multiple errors found on operands related with a
4073 single opcode, only record the error of the leftmost operand and
4074 only if the error is of higher severity. */
4075 DEBUG_TRACE ("error %s on operand %d not added to the report due to"
4076 " the existing error %s on operand %d",
4077 operand_mismatch_kind_names[new_record->detail.kind],
4078 new_record->detail.index,
4079 operand_mismatch_kind_names[record->detail.kind],
4080 record->detail.index);
4084 record->detail = new_record->detail;
4088 record_operand_error_info (const aarch64_opcode *opcode,
4089 aarch64_operand_error *error_info)
4091 operand_error_record record;
4092 record.opcode = opcode;
4093 record.detail = *error_info;
4094 add_operand_error_record (&record);
4097 /* Record an error of kind KIND and, if ERROR is not NULL, of the detailed
4098 error message *ERROR, for operand IDX (count from 0). */
4101 record_operand_error (const aarch64_opcode *opcode, int idx,
4102 enum aarch64_operand_error_kind kind,
4105 aarch64_operand_error info;
4106 memset(&info, 0, sizeof (info));
4110 record_operand_error_info (opcode, &info);
4114 record_operand_error_with_data (const aarch64_opcode *opcode, int idx,
4115 enum aarch64_operand_error_kind kind,
4116 const char* error, const int *extra_data)
4118 aarch64_operand_error info;
4122 info.data[0] = extra_data[0];
4123 info.data[1] = extra_data[1];
4124 info.data[2] = extra_data[2];
4125 record_operand_error_info (opcode, &info);
4129 record_operand_out_of_range_error (const aarch64_opcode *opcode, int idx,
4130 const char* error, int lower_bound,
4133 int data[3] = {lower_bound, upper_bound, 0};
4134 record_operand_error_with_data (opcode, idx, AARCH64_OPDE_OUT_OF_RANGE,
4138 /* Remove the operand error record for *OPCODE. */
4139 static void ATTRIBUTE_UNUSED
4140 remove_operand_error_record (const aarch64_opcode *opcode)
4142 if (opcode_has_operand_error_p (opcode))
4144 operand_error_record* record = operand_error_report.head;
4145 gas_assert (record != NULL && operand_error_report.tail != NULL);
4146 operand_error_report.head = record->next;
4147 record->next = free_opnd_error_record_nodes;
4148 free_opnd_error_record_nodes = record;
4149 if (operand_error_report.head == NULL)
4151 gas_assert (operand_error_report.tail == record);
4152 operand_error_report.tail = NULL;
4157 /* Given the instruction in *INSTR, return the index of the best matched
4158 qualifier sequence in the list (an array) headed by QUALIFIERS_LIST.
4160 Return -1 if there is no qualifier sequence; return the first match
4161 if there is multiple matches found. */
4164 find_best_match (const aarch64_inst *instr,
4165 const aarch64_opnd_qualifier_seq_t *qualifiers_list)
4167 int i, num_opnds, max_num_matched, idx;
4169 num_opnds = aarch64_num_of_operands (instr->opcode);
4172 DEBUG_TRACE ("no operand");
4176 max_num_matched = 0;
4179 /* For each pattern. */
4180 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
4183 const aarch64_opnd_qualifier_t *qualifiers = *qualifiers_list;
4185 /* Most opcodes has much fewer patterns in the list. */
4186 if (empty_qualifier_sequence_p (qualifiers) == TRUE)
4188 DEBUG_TRACE_IF (i == 0, "empty list of qualifier sequence");
4189 if (i != 0 && idx == -1)
4190 /* If nothing has been matched, return the 1st sequence. */
4195 for (j = 0, num_matched = 0; j < num_opnds; ++j, ++qualifiers)
4196 if (*qualifiers == instr->operands[j].qualifier)
4199 if (num_matched > max_num_matched)
4201 max_num_matched = num_matched;
4206 DEBUG_TRACE ("return with %d", idx);
4210 /* Assign qualifiers in the qualifier seqence (headed by QUALIFIERS) to the
4211 corresponding operands in *INSTR. */
4214 assign_qualifier_sequence (aarch64_inst *instr,
4215 const aarch64_opnd_qualifier_t *qualifiers)
4218 int num_opnds = aarch64_num_of_operands (instr->opcode);
4219 gas_assert (num_opnds);
4220 for (i = 0; i < num_opnds; ++i, ++qualifiers)
4221 instr->operands[i].qualifier = *qualifiers;
4224 /* Print operands for the diagnosis purpose. */
4227 print_operands (char *buf, const aarch64_opcode *opcode,
4228 const aarch64_opnd_info *opnds)
4232 for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
4234 const size_t size = 128;
4237 /* We regard the opcode operand info more, however we also look into
4238 the inst->operands to support the disassembling of the optional
4240 The two operand code should be the same in all cases, apart from
4241 when the operand can be optional. */
4242 if (opcode->operands[i] == AARCH64_OPND_NIL
4243 || opnds[i].type == AARCH64_OPND_NIL)
4246 /* Generate the operand string in STR. */
4247 aarch64_print_operand (str, size, 0, opcode, opnds, i, NULL, NULL);
4251 strcat (buf, i == 0 ? " " : ",");
4253 /* Append the operand string. */
4258 /* Send to stderr a string as information. */
4261 output_info (const char *format, ...)
4267 file = as_where (&line);
4271 fprintf (stderr, "%s:%u: ", file, line);
4273 fprintf (stderr, "%s: ", file);
4275 fprintf (stderr, _("Info: "));
4276 va_start (args, format);
4277 vfprintf (stderr, format, args);
4279 (void) putc ('\n', stderr);
4282 /* Output one operand error record. */
4285 output_operand_error_record (const operand_error_record *record, char *str)
4287 const aarch64_operand_error *detail = &record->detail;
4288 int idx = detail->index;
4289 const aarch64_opcode *opcode = record->opcode;
4290 enum aarch64_opnd opd_code = (idx >= 0 ? opcode->operands[idx]
4291 : AARCH64_OPND_NIL);
4293 switch (detail->kind)
4295 case AARCH64_OPDE_NIL:
4299 case AARCH64_OPDE_SYNTAX_ERROR:
4300 case AARCH64_OPDE_RECOVERABLE:
4301 case AARCH64_OPDE_FATAL_SYNTAX_ERROR:
4302 case AARCH64_OPDE_OTHER_ERROR:
4303 /* Use the prepared error message if there is, otherwise use the
4304 operand description string to describe the error. */
4305 if (detail->error != NULL)
4308 as_bad (_("%s -- `%s'"), detail->error, str);
4310 as_bad (_("%s at operand %d -- `%s'"),
4311 detail->error, idx + 1, str);
4315 gas_assert (idx >= 0);
4316 as_bad (_("operand %d should be %s -- `%s'"), idx + 1,
4317 aarch64_get_operand_desc (opd_code), str);
4321 case AARCH64_OPDE_INVALID_VARIANT:
4322 as_bad (_("operand mismatch -- `%s'"), str);
4323 if (verbose_error_p)
4325 /* We will try to correct the erroneous instruction and also provide
4326 more information e.g. all other valid variants.
4328 The string representation of the corrected instruction and other
4329 valid variants are generated by
4331 1) obtaining the intermediate representation of the erroneous
4333 2) manipulating the IR, e.g. replacing the operand qualifier;
4334 3) printing out the instruction by calling the printer functions
4335 shared with the disassembler.
4337 The limitation of this method is that the exact input assembly
4338 line cannot be accurately reproduced in some cases, for example an
4339 optional operand present in the actual assembly line will be
4340 omitted in the output; likewise for the optional syntax rules,
4341 e.g. the # before the immediate. Another limitation is that the
4342 assembly symbols and relocation operations in the assembly line
4343 currently cannot be printed out in the error report. Last but not
4344 least, when there is other error(s) co-exist with this error, the
4345 'corrected' instruction may be still incorrect, e.g. given
4346 'ldnp h0,h1,[x0,#6]!'
4347 this diagnosis will provide the version:
4348 'ldnp s0,s1,[x0,#6]!'
4349 which is still not right. */
4350 size_t len = strlen (get_mnemonic_name (str));
4353 const size_t size = 2048;
4355 aarch64_inst *inst_base = &inst.base;
4356 const aarch64_opnd_qualifier_seq_t *qualifiers_list;
4359 reset_aarch64_instruction (&inst);
4360 inst_base->opcode = opcode;
4362 /* Reset the error report so that there is no side effect on the
4363 following operand parsing. */
4364 init_operand_error_report ();
4367 result = parse_operands (str + len, opcode)
4368 && programmer_friendly_fixup (&inst);
4369 gas_assert (result);
4370 result = aarch64_opcode_encode (opcode, inst_base, &inst_base->value,
4372 gas_assert (!result);
4374 /* Find the most matched qualifier sequence. */
4375 qlf_idx = find_best_match (inst_base, opcode->qualifiers_list);
4376 gas_assert (qlf_idx > -1);
4378 /* Assign the qualifiers. */
4379 assign_qualifier_sequence (inst_base,
4380 opcode->qualifiers_list[qlf_idx]);
4382 /* Print the hint. */
4383 output_info (_(" did you mean this?"));
4384 snprintf (buf, size, "\t%s", get_mnemonic_name (str));
4385 print_operands (buf, opcode, inst_base->operands);
4386 output_info (_(" %s"), buf);
4388 /* Print out other variant(s) if there is any. */
4390 !empty_qualifier_sequence_p (opcode->qualifiers_list[1]))
4391 output_info (_(" other valid variant(s):"));
4393 /* For each pattern. */
4394 qualifiers_list = opcode->qualifiers_list;
4395 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
4397 /* Most opcodes has much fewer patterns in the list.
4398 First NIL qualifier indicates the end in the list. */
4399 if (empty_qualifier_sequence_p (*qualifiers_list) == TRUE)
4404 /* Mnemonics name. */
4405 snprintf (buf, size, "\t%s", get_mnemonic_name (str));
4407 /* Assign the qualifiers. */
4408 assign_qualifier_sequence (inst_base, *qualifiers_list);
4410 /* Print instruction. */
4411 print_operands (buf, opcode, inst_base->operands);
4413 output_info (_(" %s"), buf);
4419 case AARCH64_OPDE_OUT_OF_RANGE:
4420 if (detail->data[0] != detail->data[1])
4421 as_bad (_("%s out of range %d to %d at operand %d -- `%s'"),
4422 detail->error ? detail->error : _("immediate value"),
4423 detail->data[0], detail->data[1], idx + 1, str);
4425 as_bad (_("%s expected to be %d at operand %d -- `%s'"),
4426 detail->error ? detail->error : _("immediate value"),
4427 detail->data[0], idx + 1, str);
4430 case AARCH64_OPDE_REG_LIST:
4431 if (detail->data[0] == 1)
4432 as_bad (_("invalid number of registers in the list; "
4433 "only 1 register is expected at operand %d -- `%s'"),
4436 as_bad (_("invalid number of registers in the list; "
4437 "%d registers are expected at operand %d -- `%s'"),
4438 detail->data[0], idx + 1, str);
4441 case AARCH64_OPDE_UNALIGNED:
4442 as_bad (_("immediate value should be a multiple of "
4443 "%d at operand %d -- `%s'"),
4444 detail->data[0], idx + 1, str);
4453 /* Process and output the error message about the operand mismatching.
4455 When this function is called, the operand error information had
4456 been collected for an assembly line and there will be multiple
4457 errors in the case of mulitple instruction templates; output the
4458 error message that most closely describes the problem. */
4461 output_operand_error_report (char *str)
4463 int largest_error_pos;
4464 const char *msg = NULL;
4465 enum aarch64_operand_error_kind kind;
4466 operand_error_record *curr;
4467 operand_error_record *head = operand_error_report.head;
4468 operand_error_record *record = NULL;
4470 /* No error to report. */
4474 gas_assert (head != NULL && operand_error_report.tail != NULL);
4476 /* Only one error. */
4477 if (head == operand_error_report.tail)
4479 DEBUG_TRACE ("single opcode entry with error kind: %s",
4480 operand_mismatch_kind_names[head->detail.kind]);
4481 output_operand_error_record (head, str);
4485 /* Find the error kind of the highest severity. */
4486 DEBUG_TRACE ("multiple opcode entres with error kind");
4487 kind = AARCH64_OPDE_NIL;
4488 for (curr = head; curr != NULL; curr = curr->next)
4490 gas_assert (curr->detail.kind != AARCH64_OPDE_NIL);
4491 DEBUG_TRACE ("\t%s", operand_mismatch_kind_names[curr->detail.kind]);
4492 if (operand_error_higher_severity_p (curr->detail.kind, kind))
4493 kind = curr->detail.kind;
4495 gas_assert (kind != AARCH64_OPDE_NIL);
4497 /* Pick up one of errors of KIND to report. */
4498 largest_error_pos = -2; /* Index can be -1 which means unknown index. */
4499 for (curr = head; curr != NULL; curr = curr->next)
4501 if (curr->detail.kind != kind)
4503 /* If there are multiple errors, pick up the one with the highest
4504 mismatching operand index. In the case of multiple errors with
4505 the equally highest operand index, pick up the first one or the
4506 first one with non-NULL error message. */
4507 if (curr->detail.index > largest_error_pos
4508 || (curr->detail.index == largest_error_pos && msg == NULL
4509 && curr->detail.error != NULL))
4511 largest_error_pos = curr->detail.index;
4513 msg = record->detail.error;
4517 gas_assert (largest_error_pos != -2 && record != NULL);
4518 DEBUG_TRACE ("Pick up error kind %s to report",
4519 operand_mismatch_kind_names[record->detail.kind]);
4522 output_operand_error_record (record, str);
4525 /* Write an AARCH64 instruction to buf - always little-endian. */
4527 put_aarch64_insn (char *buf, uint32_t insn)
4529 unsigned char *where = (unsigned char *) buf;
4531 where[1] = insn >> 8;
4532 where[2] = insn >> 16;
4533 where[3] = insn >> 24;
4537 get_aarch64_insn (char *buf)
4539 unsigned char *where = (unsigned char *) buf;
4541 result = (where[0] | (where[1] << 8) | (where[2] << 16) | (where[3] << 24));
4546 output_inst (struct aarch64_inst *new_inst)
4550 to = frag_more (INSN_SIZE);
4552 frag_now->tc_frag_data.recorded = 1;
4554 put_aarch64_insn (to, inst.base.value);
4556 if (inst.reloc.type != BFD_RELOC_UNUSED)
4558 fixS *fixp = fix_new_aarch64 (frag_now, to - frag_now->fr_literal,
4559 INSN_SIZE, &inst.reloc.exp,
4562 DEBUG_TRACE ("Prepared relocation fix up");
4563 /* Don't check the addend value against the instruction size,
4564 that's the job of our code in md_apply_fix(). */
4565 fixp->fx_no_overflow = 1;
4566 if (new_inst != NULL)
4567 fixp->tc_fix_data.inst = new_inst;
4568 if (aarch64_gas_internal_fixup_p ())
4570 gas_assert (inst.reloc.opnd != AARCH64_OPND_NIL);
4571 fixp->tc_fix_data.opnd = inst.reloc.opnd;
4572 fixp->fx_addnumber = inst.reloc.flags;
4576 dwarf2_emit_insn (INSN_SIZE);
4579 /* Link together opcodes of the same name. */
4583 aarch64_opcode *opcode;
4584 struct templates *next;
4587 typedef struct templates templates;
4590 lookup_mnemonic (const char *start, int len)
4592 templates *templ = NULL;
4594 templ = hash_find_n (aarch64_ops_hsh, start, len);
4598 /* Subroutine of md_assemble, responsible for looking up the primary
4599 opcode from the mnemonic the user wrote. STR points to the
4600 beginning of the mnemonic. */
4603 opcode_lookup (char **str)
4606 const aarch64_cond *cond;
4610 /* Scan up to the end of the mnemonic, which must end in white space,
4611 '.', or end of string. */
4612 for (base = end = *str; is_part_of_name(*end); end++)
4619 inst.cond = COND_ALWAYS;
4621 /* Handle a possible condition. */
4624 cond = hash_find_n (aarch64_cond_hsh, end + 1, 2);
4627 inst.cond = cond->value;
4641 if (inst.cond == COND_ALWAYS)
4643 /* Look for unaffixed mnemonic. */
4644 return lookup_mnemonic (base, len);
4648 /* append ".c" to mnemonic if conditional */
4649 memcpy (condname, base, len);
4650 memcpy (condname + len, ".c", 2);
4653 return lookup_mnemonic (base, len);
4659 /* Internal helper routine converting a vector neon_type_el structure
4660 *VECTYPE to a corresponding operand qualifier. */
4662 static inline aarch64_opnd_qualifier_t
4663 vectype_to_qualifier (const struct neon_type_el *vectype)
4665 /* Element size in bytes indexed by neon_el_type. */
4666 const unsigned char ele_size[5]
4668 const unsigned int ele_base [5] =
4670 AARCH64_OPND_QLF_V_8B,
4671 AARCH64_OPND_QLF_V_2H,
4672 AARCH64_OPND_QLF_V_2S,
4673 AARCH64_OPND_QLF_V_1D,
4674 AARCH64_OPND_QLF_V_1Q
4677 if (!vectype->defined || vectype->type == NT_invtype)
4678 goto vectype_conversion_fail;
4680 gas_assert (vectype->type >= NT_b && vectype->type <= NT_q);
4682 if (vectype->defined & NTA_HASINDEX)
4683 /* Vector element register. */
4684 return AARCH64_OPND_QLF_S_B + vectype->type;
4687 /* Vector register. */
4688 int reg_size = ele_size[vectype->type] * vectype->width;
4691 if (reg_size != 16 && reg_size != 8 && reg_size != 4)
4692 goto vectype_conversion_fail;
4694 /* The conversion is by calculating the offset from the base operand
4695 qualifier for the vector type. The operand qualifiers are regular
4696 enough that the offset can established by shifting the vector width by
4697 a vector-type dependent amount. */
4699 if (vectype->type == NT_b)
4701 else if (vectype->type == NT_h || vectype->type == NT_s)
4703 else if (vectype->type >= NT_d)
4708 offset = ele_base [vectype->type] + (vectype->width >> shift);
4709 gas_assert (AARCH64_OPND_QLF_V_8B <= offset
4710 && offset <= AARCH64_OPND_QLF_V_1Q);
4714 vectype_conversion_fail:
4715 first_error (_("bad vector arrangement type"));
4716 return AARCH64_OPND_QLF_NIL;
4719 /* Process an optional operand that is found omitted from the assembly line.
4720 Fill *OPERAND for such an operand of type TYPE. OPCODE points to the
4721 instruction's opcode entry while IDX is the index of this omitted operand.
4725 process_omitted_operand (enum aarch64_opnd type, const aarch64_opcode *opcode,
4726 int idx, aarch64_opnd_info *operand)
4728 aarch64_insn default_value = get_optional_operand_default_value (opcode);
4729 gas_assert (optional_operand_p (opcode, idx));
4730 gas_assert (!operand->present);
4734 case AARCH64_OPND_Rd:
4735 case AARCH64_OPND_Rn:
4736 case AARCH64_OPND_Rm:
4737 case AARCH64_OPND_Rt:
4738 case AARCH64_OPND_Rt2:
4739 case AARCH64_OPND_Rs:
4740 case AARCH64_OPND_Ra:
4741 case AARCH64_OPND_Rt_SYS:
4742 case AARCH64_OPND_Rd_SP:
4743 case AARCH64_OPND_Rn_SP:
4744 case AARCH64_OPND_Fd:
4745 case AARCH64_OPND_Fn:
4746 case AARCH64_OPND_Fm:
4747 case AARCH64_OPND_Fa:
4748 case AARCH64_OPND_Ft:
4749 case AARCH64_OPND_Ft2:
4750 case AARCH64_OPND_Sd:
4751 case AARCH64_OPND_Sn:
4752 case AARCH64_OPND_Sm:
4753 case AARCH64_OPND_Vd:
4754 case AARCH64_OPND_Vn:
4755 case AARCH64_OPND_Vm:
4756 case AARCH64_OPND_VdD1:
4757 case AARCH64_OPND_VnD1:
4758 operand->reg.regno = default_value;
4761 case AARCH64_OPND_Ed:
4762 case AARCH64_OPND_En:
4763 case AARCH64_OPND_Em:
4764 operand->reglane.regno = default_value;
4767 case AARCH64_OPND_IDX:
4768 case AARCH64_OPND_BIT_NUM:
4769 case AARCH64_OPND_IMMR:
4770 case AARCH64_OPND_IMMS:
4771 case AARCH64_OPND_SHLL_IMM:
4772 case AARCH64_OPND_IMM_VLSL:
4773 case AARCH64_OPND_IMM_VLSR:
4774 case AARCH64_OPND_CCMP_IMM:
4775 case AARCH64_OPND_FBITS:
4776 case AARCH64_OPND_UIMM4:
4777 case AARCH64_OPND_UIMM3_OP1:
4778 case AARCH64_OPND_UIMM3_OP2:
4779 case AARCH64_OPND_IMM:
4780 case AARCH64_OPND_WIDTH:
4781 case AARCH64_OPND_UIMM7:
4782 case AARCH64_OPND_NZCV:
4783 operand->imm.value = default_value;
4786 case AARCH64_OPND_EXCEPTION:
4787 inst.reloc.type = BFD_RELOC_UNUSED;
4790 case AARCH64_OPND_BARRIER_ISB:
4791 operand->barrier = aarch64_barrier_options + default_value;
4798 /* Process the relocation type for move wide instructions.
4799 Return TRUE on success; otherwise return FALSE. */
4802 process_movw_reloc_info (void)
4807 is32 = inst.base.operands[0].qualifier == AARCH64_OPND_QLF_W ? 1 : 0;
4809 if (inst.base.opcode->op == OP_MOVK)
4810 switch (inst.reloc.type)
4812 case BFD_RELOC_AARCH64_MOVW_G0_S:
4813 case BFD_RELOC_AARCH64_MOVW_G1_S:
4814 case BFD_RELOC_AARCH64_MOVW_G2_S:
4815 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4816 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4817 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4818 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4820 (_("the specified relocation type is not allowed for MOVK"));
4826 switch (inst.reloc.type)
4828 case BFD_RELOC_AARCH64_MOVW_G0:
4829 case BFD_RELOC_AARCH64_MOVW_G0_NC:
4830 case BFD_RELOC_AARCH64_MOVW_G0_S:
4831 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
4832 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
4833 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
4834 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
4835 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
4836 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
4837 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4838 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4841 case BFD_RELOC_AARCH64_MOVW_G1:
4842 case BFD_RELOC_AARCH64_MOVW_G1_NC:
4843 case BFD_RELOC_AARCH64_MOVW_G1_S:
4844 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
4845 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
4846 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
4847 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
4848 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
4849 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
4850 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4851 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4854 case BFD_RELOC_AARCH64_MOVW_G2:
4855 case BFD_RELOC_AARCH64_MOVW_G2_NC:
4856 case BFD_RELOC_AARCH64_MOVW_G2_S:
4857 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
4858 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4861 set_fatal_syntax_error
4862 (_("the specified relocation type is not allowed for 32-bit "
4868 case BFD_RELOC_AARCH64_MOVW_G3:
4871 set_fatal_syntax_error
4872 (_("the specified relocation type is not allowed for 32-bit "
4879 /* More cases should be added when more MOVW-related relocation types
4880 are supported in GAS. */
4881 gas_assert (aarch64_gas_internal_fixup_p ());
4882 /* The shift amount should have already been set by the parser. */
4885 inst.base.operands[1].shifter.amount = shift;
4889 /* A primitive log caculator. */
4891 static inline unsigned int
4892 get_logsz (unsigned int size)
4894 const unsigned char ls[16] =
4895 {0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
4901 gas_assert (ls[size - 1] != (unsigned char)-1);
4902 return ls[size - 1];
4905 /* Determine and return the real reloc type code for an instruction
4906 with the pseudo reloc type code BFD_RELOC_AARCH64_LDST_LO12. */
4908 static inline bfd_reloc_code_real_type
4909 ldst_lo12_determine_real_reloc_type (void)
4912 enum aarch64_opnd_qualifier opd0_qlf = inst.base.operands[0].qualifier;
4913 enum aarch64_opnd_qualifier opd1_qlf = inst.base.operands[1].qualifier;
4915 const bfd_reloc_code_real_type reloc_ldst_lo12[3][5] = {
4917 BFD_RELOC_AARCH64_LDST8_LO12,
4918 BFD_RELOC_AARCH64_LDST16_LO12,
4919 BFD_RELOC_AARCH64_LDST32_LO12,
4920 BFD_RELOC_AARCH64_LDST64_LO12,
4921 BFD_RELOC_AARCH64_LDST128_LO12
4924 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12,
4925 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12,
4926 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12,
4927 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12,
4928 BFD_RELOC_AARCH64_NONE
4931 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC,
4932 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC,
4933 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC,
4934 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC,
4935 BFD_RELOC_AARCH64_NONE
4939 gas_assert (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
4940 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
4942 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC));
4943 gas_assert (inst.base.opcode->operands[1] == AARCH64_OPND_ADDR_UIMM12);
4945 if (opd1_qlf == AARCH64_OPND_QLF_NIL)
4947 aarch64_get_expected_qualifier (inst.base.opcode->qualifiers_list,
4949 gas_assert (opd1_qlf != AARCH64_OPND_QLF_NIL);
4951 logsz = get_logsz (aarch64_get_qualifier_esize (opd1_qlf));
4952 if (inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
4953 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC)
4954 gas_assert (logsz <= 3);
4956 gas_assert (logsz <= 4);
4958 /* In reloc.c, these pseudo relocation types should be defined in similar
4959 order as above reloc_ldst_lo12 array. Because the array index calcuation
4960 below relies on this. */
4961 return reloc_ldst_lo12[inst.reloc.type - BFD_RELOC_AARCH64_LDST_LO12][logsz];
4964 /* Check whether a register list REGINFO is valid. The registers must be
4965 numbered in increasing order (modulo 32), in increments of one or two.
4967 If ACCEPT_ALTERNATE is non-zero, the register numbers should be in
4970 Return FALSE if such a register list is invalid, otherwise return TRUE. */
4973 reg_list_valid_p (uint32_t reginfo, int accept_alternate)
4975 uint32_t i, nb_regs, prev_regno, incr;
4977 nb_regs = 1 + (reginfo & 0x3);
4979 prev_regno = reginfo & 0x1f;
4980 incr = accept_alternate ? 2 : 1;
4982 for (i = 1; i < nb_regs; ++i)
4984 uint32_t curr_regno;
4986 curr_regno = reginfo & 0x1f;
4987 if (curr_regno != ((prev_regno + incr) & 0x1f))
4989 prev_regno = curr_regno;
4995 /* Generic instruction operand parser. This does no encoding and no
4996 semantic validation; it merely squirrels values away in the inst
4997 structure. Returns TRUE or FALSE depending on whether the
4998 specified grammar matched. */
5001 parse_operands (char *str, const aarch64_opcode *opcode)
5004 char *backtrack_pos = 0;
5005 const enum aarch64_opnd *operands = opcode->operands;
5008 skip_whitespace (str);
5010 for (i = 0; operands[i] != AARCH64_OPND_NIL; i++)
5013 int isreg32, isregzero;
5014 int comma_skipped_p = 0;
5015 aarch64_reg_type rtype;
5016 struct neon_type_el vectype;
5017 aarch64_opnd_info *info = &inst.base.operands[i];
5019 DEBUG_TRACE ("parse operand %d", i);
5021 /* Assign the operand code. */
5022 info->type = operands[i];
5024 if (optional_operand_p (opcode, i))
5026 /* Remember where we are in case we need to backtrack. */
5027 gas_assert (!backtrack_pos);
5028 backtrack_pos = str;
5031 /* Expect comma between operands; the backtrack mechanizm will take
5032 care of cases of omitted optional operand. */
5033 if (i > 0 && ! skip_past_char (&str, ','))
5035 set_syntax_error (_("comma expected between operands"));
5039 comma_skipped_p = 1;
5041 switch (operands[i])
5043 case AARCH64_OPND_Rd:
5044 case AARCH64_OPND_Rn:
5045 case AARCH64_OPND_Rm:
5046 case AARCH64_OPND_Rt:
5047 case AARCH64_OPND_Rt2:
5048 case AARCH64_OPND_Rs:
5049 case AARCH64_OPND_Ra:
5050 case AARCH64_OPND_Rt_SYS:
5051 case AARCH64_OPND_PAIRREG:
5052 po_int_reg_or_fail (1, 0);
5055 case AARCH64_OPND_Rd_SP:
5056 case AARCH64_OPND_Rn_SP:
5057 po_int_reg_or_fail (0, 1);
5060 case AARCH64_OPND_Rm_EXT:
5061 case AARCH64_OPND_Rm_SFT:
5062 po_misc_or_fail (parse_shifter_operand
5063 (&str, info, (operands[i] == AARCH64_OPND_Rm_EXT
5065 : SHIFTED_LOGIC_IMM)));
5066 if (!info->shifter.operator_present)
5068 /* Default to LSL if not present. Libopcodes prefers shifter
5069 kind to be explicit. */
5070 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5071 info->shifter.kind = AARCH64_MOD_LSL;
5072 /* For Rm_EXT, libopcodes will carry out further check on whether
5073 or not stack pointer is used in the instruction (Recall that
5074 "the extend operator is not optional unless at least one of
5075 "Rd" or "Rn" is '11111' (i.e. WSP)"). */
5079 case AARCH64_OPND_Fd:
5080 case AARCH64_OPND_Fn:
5081 case AARCH64_OPND_Fm:
5082 case AARCH64_OPND_Fa:
5083 case AARCH64_OPND_Ft:
5084 case AARCH64_OPND_Ft2:
5085 case AARCH64_OPND_Sd:
5086 case AARCH64_OPND_Sn:
5087 case AARCH64_OPND_Sm:
5088 val = aarch64_reg_parse (&str, REG_TYPE_BHSDQ, &rtype, NULL);
5089 if (val == PARSE_FAIL)
5091 first_error (_(get_reg_expected_msg (REG_TYPE_BHSDQ)));
5094 gas_assert (rtype >= REG_TYPE_FP_B && rtype <= REG_TYPE_FP_Q);
5096 info->reg.regno = val;
5097 info->qualifier = AARCH64_OPND_QLF_S_B + (rtype - REG_TYPE_FP_B);
5100 case AARCH64_OPND_Vd:
5101 case AARCH64_OPND_Vn:
5102 case AARCH64_OPND_Vm:
5103 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5104 if (val == PARSE_FAIL)
5106 first_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5109 if (vectype.defined & NTA_HASINDEX)
5112 info->reg.regno = val;
5113 info->qualifier = vectype_to_qualifier (&vectype);
5114 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5118 case AARCH64_OPND_VdD1:
5119 case AARCH64_OPND_VnD1:
5120 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5121 if (val == PARSE_FAIL)
5123 set_first_syntax_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5126 if (vectype.type != NT_d || vectype.index != 1)
5128 set_fatal_syntax_error
5129 (_("the top half of a 128-bit FP/SIMD register is expected"));
5132 info->reg.regno = val;
5133 /* N.B: VdD1 and VnD1 are treated as an fp or advsimd scalar register
5134 here; it is correct for the purpose of encoding/decoding since
5135 only the register number is explicitly encoded in the related
5136 instructions, although this appears a bit hacky. */
5137 info->qualifier = AARCH64_OPND_QLF_S_D;
5140 case AARCH64_OPND_Ed:
5141 case AARCH64_OPND_En:
5142 case AARCH64_OPND_Em:
5143 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5144 if (val == PARSE_FAIL)
5146 first_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5149 if (vectype.type == NT_invtype || !(vectype.defined & NTA_HASINDEX))
5152 info->reglane.regno = val;
5153 info->reglane.index = vectype.index;
5154 info->qualifier = vectype_to_qualifier (&vectype);
5155 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5159 case AARCH64_OPND_LVn:
5160 case AARCH64_OPND_LVt:
5161 case AARCH64_OPND_LVt_AL:
5162 case AARCH64_OPND_LEt:
5163 if ((val = parse_neon_reg_list (&str, &vectype)) == PARSE_FAIL)
5165 if (! reg_list_valid_p (val, /* accept_alternate */ 0))
5167 set_fatal_syntax_error (_("invalid register list"));
5170 info->reglist.first_regno = (val >> 2) & 0x1f;
5171 info->reglist.num_regs = (val & 0x3) + 1;
5172 if (operands[i] == AARCH64_OPND_LEt)
5174 if (!(vectype.defined & NTA_HASINDEX))
5176 info->reglist.has_index = 1;
5177 info->reglist.index = vectype.index;
5179 else if (!(vectype.defined & NTA_HASTYPE))
5181 info->qualifier = vectype_to_qualifier (&vectype);
5182 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5186 case AARCH64_OPND_Cn:
5187 case AARCH64_OPND_Cm:
5188 po_reg_or_fail (REG_TYPE_CN);
5191 set_fatal_syntax_error (_(get_reg_expected_msg (REG_TYPE_CN)));
5194 inst.base.operands[i].reg.regno = val;
5197 case AARCH64_OPND_SHLL_IMM:
5198 case AARCH64_OPND_IMM_VLSR:
5199 po_imm_or_fail (1, 64);
5200 info->imm.value = val;
5203 case AARCH64_OPND_CCMP_IMM:
5204 case AARCH64_OPND_FBITS:
5205 case AARCH64_OPND_UIMM4:
5206 case AARCH64_OPND_UIMM3_OP1:
5207 case AARCH64_OPND_UIMM3_OP2:
5208 case AARCH64_OPND_IMM_VLSL:
5209 case AARCH64_OPND_IMM:
5210 case AARCH64_OPND_WIDTH:
5211 po_imm_nc_or_fail ();
5212 info->imm.value = val;
5215 case AARCH64_OPND_UIMM7:
5216 po_imm_or_fail (0, 127);
5217 info->imm.value = val;
5220 case AARCH64_OPND_IDX:
5221 case AARCH64_OPND_BIT_NUM:
5222 case AARCH64_OPND_IMMR:
5223 case AARCH64_OPND_IMMS:
5224 po_imm_or_fail (0, 63);
5225 info->imm.value = val;
5228 case AARCH64_OPND_IMM0:
5229 po_imm_nc_or_fail ();
5232 set_fatal_syntax_error (_("immediate zero expected"));
5235 info->imm.value = 0;
5238 case AARCH64_OPND_FPIMM0:
5241 bfd_boolean res1 = FALSE, res2 = FALSE;
5242 /* N.B. -0.0 will be rejected; although -0.0 shouldn't be rejected,
5243 it is probably not worth the effort to support it. */
5244 if (!(res1 = parse_aarch64_imm_float (&str, &qfloat, FALSE))
5245 && !(res2 = parse_constant_immediate (&str, &val)))
5247 if ((res1 && qfloat == 0) || (res2 && val == 0))
5249 info->imm.value = 0;
5250 info->imm.is_fp = 1;
5253 set_fatal_syntax_error (_("immediate zero expected"));
5257 case AARCH64_OPND_IMM_MOV:
5260 if (reg_name_p (str, REG_TYPE_R_Z_SP) ||
5261 reg_name_p (str, REG_TYPE_VN))
5264 po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
5266 /* The MOV immediate alias will be fixed up by fix_mov_imm_insn
5267 later. fix_mov_imm_insn will try to determine a machine
5268 instruction (MOVZ, MOVN or ORR) for it and will issue an error
5269 message if the immediate cannot be moved by a single
5271 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
5272 inst.base.operands[i].skip = 1;
5276 case AARCH64_OPND_SIMD_IMM:
5277 case AARCH64_OPND_SIMD_IMM_SFT:
5278 if (! parse_big_immediate (&str, &val))
5280 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5282 /* need_libopcodes_p */ 1,
5285 N.B. although AARCH64_OPND_SIMD_IMM doesn't permit any
5286 shift, we don't check it here; we leave the checking to
5287 the libopcodes (operand_general_constraint_met_p). By
5288 doing this, we achieve better diagnostics. */
5289 if (skip_past_comma (&str)
5290 && ! parse_shift (&str, info, SHIFTED_LSL_MSL))
5292 if (!info->shifter.operator_present
5293 && info->type == AARCH64_OPND_SIMD_IMM_SFT)
5295 /* Default to LSL if not present. Libopcodes prefers shifter
5296 kind to be explicit. */
5297 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5298 info->shifter.kind = AARCH64_MOD_LSL;
5302 case AARCH64_OPND_FPIMM:
5303 case AARCH64_OPND_SIMD_FPIMM:
5307 = (aarch64_get_qualifier_esize (inst.base.operands[0].qualifier)
5309 if (! parse_aarch64_imm_float (&str, &qfloat, dp_p))
5313 set_fatal_syntax_error (_("invalid floating-point constant"));
5316 inst.base.operands[i].imm.value = encode_imm_float_bits (qfloat);
5317 inst.base.operands[i].imm.is_fp = 1;
5321 case AARCH64_OPND_LIMM:
5322 po_misc_or_fail (parse_shifter_operand (&str, info,
5323 SHIFTED_LOGIC_IMM));
5324 if (info->shifter.operator_present)
5326 set_fatal_syntax_error
5327 (_("shift not allowed for bitmask immediate"));
5330 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5332 /* need_libopcodes_p */ 1,
5336 case AARCH64_OPND_AIMM:
5337 if (opcode->op == OP_ADD)
5338 /* ADD may have relocation types. */
5339 po_misc_or_fail (parse_shifter_operand_reloc (&str, info,
5340 SHIFTED_ARITH_IMM));
5342 po_misc_or_fail (parse_shifter_operand (&str, info,
5343 SHIFTED_ARITH_IMM));
5344 switch (inst.reloc.type)
5346 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5347 info->shifter.amount = 12;
5349 case BFD_RELOC_UNUSED:
5350 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
5351 if (info->shifter.kind != AARCH64_MOD_NONE)
5352 inst.reloc.flags = FIXUP_F_HAS_EXPLICIT_SHIFT;
5353 inst.reloc.pc_rel = 0;
5358 info->imm.value = 0;
5359 if (!info->shifter.operator_present)
5361 /* Default to LSL if not present. Libopcodes prefers shifter
5362 kind to be explicit. */
5363 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5364 info->shifter.kind = AARCH64_MOD_LSL;
5368 case AARCH64_OPND_HALF:
5370 /* #<imm16> or relocation. */
5371 int internal_fixup_p;
5372 po_misc_or_fail (parse_half (&str, &internal_fixup_p));
5373 if (internal_fixup_p)
5374 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
5375 skip_whitespace (str);
5376 if (skip_past_comma (&str))
5378 /* {, LSL #<shift>} */
5379 if (! aarch64_gas_internal_fixup_p ())
5381 set_fatal_syntax_error (_("can't mix relocation modifier "
5382 "with explicit shift"));
5385 po_misc_or_fail (parse_shift (&str, info, SHIFTED_LSL));
5388 inst.base.operands[i].shifter.amount = 0;
5389 inst.base.operands[i].shifter.kind = AARCH64_MOD_LSL;
5390 inst.base.operands[i].imm.value = 0;
5391 if (! process_movw_reloc_info ())
5396 case AARCH64_OPND_EXCEPTION:
5397 po_misc_or_fail (parse_immediate_expression (&str, &inst.reloc.exp));
5398 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5400 /* need_libopcodes_p */ 0,
5404 case AARCH64_OPND_NZCV:
5406 const asm_nzcv *nzcv = hash_find_n (aarch64_nzcv_hsh, str, 4);
5410 info->imm.value = nzcv->value;
5413 po_imm_or_fail (0, 15);
5414 info->imm.value = val;
5418 case AARCH64_OPND_COND:
5419 case AARCH64_OPND_COND1:
5420 info->cond = hash_find_n (aarch64_cond_hsh, str, 2);
5422 if (info->cond == NULL)
5424 set_syntax_error (_("invalid condition"));
5427 else if (operands[i] == AARCH64_OPND_COND1
5428 && (info->cond->value & 0xe) == 0xe)
5430 /* Not allow AL or NV. */
5431 set_default_error ();
5436 case AARCH64_OPND_ADDR_ADRP:
5437 po_misc_or_fail (parse_adrp (&str));
5438 /* Clear the value as operand needs to be relocated. */
5439 info->imm.value = 0;
5442 case AARCH64_OPND_ADDR_PCREL14:
5443 case AARCH64_OPND_ADDR_PCREL19:
5444 case AARCH64_OPND_ADDR_PCREL21:
5445 case AARCH64_OPND_ADDR_PCREL26:
5446 po_misc_or_fail (parse_address_reloc (&str, info));
5447 if (!info->addr.pcrel)
5449 set_syntax_error (_("invalid pc-relative address"));
5452 if (inst.gen_lit_pool
5453 && (opcode->iclass != loadlit || opcode->op == OP_PRFM_LIT))
5455 /* Only permit "=value" in the literal load instructions.
5456 The literal will be generated by programmer_friendly_fixup. */
5457 set_syntax_error (_("invalid use of \"=immediate\""));
5460 if (inst.reloc.exp.X_op == O_symbol && find_reloc_table_entry (&str))
5462 set_syntax_error (_("unrecognized relocation suffix"));
5465 if (inst.reloc.exp.X_op == O_constant && !inst.gen_lit_pool)
5467 info->imm.value = inst.reloc.exp.X_add_number;
5468 inst.reloc.type = BFD_RELOC_UNUSED;
5472 info->imm.value = 0;
5473 if (inst.reloc.type == BFD_RELOC_UNUSED)
5474 switch (opcode->iclass)
5478 /* e.g. CBZ or B.COND */
5479 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
5480 inst.reloc.type = BFD_RELOC_AARCH64_BRANCH19;
5484 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL14);
5485 inst.reloc.type = BFD_RELOC_AARCH64_TSTBR14;
5489 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL26);
5491 (opcode->op == OP_BL) ? BFD_RELOC_AARCH64_CALL26
5492 : BFD_RELOC_AARCH64_JUMP26;
5495 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
5496 inst.reloc.type = BFD_RELOC_AARCH64_LD_LO19_PCREL;
5499 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL21);
5500 inst.reloc.type = BFD_RELOC_AARCH64_ADR_LO21_PCREL;
5506 inst.reloc.pc_rel = 1;
5510 case AARCH64_OPND_ADDR_SIMPLE:
5511 case AARCH64_OPND_SIMD_ADDR_SIMPLE:
5512 /* [<Xn|SP>{, #<simm>}] */
5513 po_char_or_fail ('[');
5514 po_reg_or_fail (REG_TYPE_R64_SP);
5515 /* Accept optional ", #0". */
5516 if (operands[i] == AARCH64_OPND_ADDR_SIMPLE
5517 && skip_past_char (&str, ','))
5519 skip_past_char (&str, '#');
5520 if (! skip_past_char (&str, '0'))
5522 set_fatal_syntax_error
5523 (_("the optional immediate offset can only be 0"));
5527 po_char_or_fail (']');
5528 info->addr.base_regno = val;
5531 case AARCH64_OPND_ADDR_REGOFF:
5532 /* [<Xn|SP>, <R><m>{, <extend> {<amount>}}] */
5533 po_misc_or_fail (parse_address (&str, info, 0));
5534 if (info->addr.pcrel || !info->addr.offset.is_reg
5535 || !info->addr.preind || info->addr.postind
5536 || info->addr.writeback)
5538 set_syntax_error (_("invalid addressing mode"));
5541 if (!info->shifter.operator_present)
5543 /* Default to LSL if not present. Libopcodes prefers shifter
5544 kind to be explicit. */
5545 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5546 info->shifter.kind = AARCH64_MOD_LSL;
5548 /* Qualifier to be deduced by libopcodes. */
5551 case AARCH64_OPND_ADDR_SIMM7:
5552 po_misc_or_fail (parse_address (&str, info, 0));
5553 if (info->addr.pcrel || info->addr.offset.is_reg
5554 || (!info->addr.preind && !info->addr.postind))
5556 set_syntax_error (_("invalid addressing mode"));
5559 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5561 /* need_libopcodes_p */ 1,
5565 case AARCH64_OPND_ADDR_SIMM9:
5566 case AARCH64_OPND_ADDR_SIMM9_2:
5567 po_misc_or_fail (parse_address_reloc (&str, info));
5568 if (info->addr.pcrel || info->addr.offset.is_reg
5569 || (!info->addr.preind && !info->addr.postind)
5570 || (operands[i] == AARCH64_OPND_ADDR_SIMM9_2
5571 && info->addr.writeback))
5573 set_syntax_error (_("invalid addressing mode"));
5576 if (inst.reloc.type != BFD_RELOC_UNUSED)
5578 set_syntax_error (_("relocation not allowed"));
5581 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5583 /* need_libopcodes_p */ 1,
5587 case AARCH64_OPND_ADDR_UIMM12:
5588 po_misc_or_fail (parse_address_reloc (&str, info));
5589 if (info->addr.pcrel || info->addr.offset.is_reg
5590 || !info->addr.preind || info->addr.writeback)
5592 set_syntax_error (_("invalid addressing mode"));
5595 if (inst.reloc.type == BFD_RELOC_UNUSED)
5596 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
5597 else if (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
5599 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12)
5601 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC))
5602 inst.reloc.type = ldst_lo12_determine_real_reloc_type ();
5603 /* Leave qualifier to be determined by libopcodes. */
5606 case AARCH64_OPND_SIMD_ADDR_POST:
5607 /* [<Xn|SP>], <Xm|#<amount>> */
5608 po_misc_or_fail (parse_address (&str, info, 1));
5609 if (!info->addr.postind || !info->addr.writeback)
5611 set_syntax_error (_("invalid addressing mode"));
5614 if (!info->addr.offset.is_reg)
5616 if (inst.reloc.exp.X_op == O_constant)
5617 info->addr.offset.imm = inst.reloc.exp.X_add_number;
5620 set_fatal_syntax_error
5621 (_("writeback value should be an immediate constant"));
5628 case AARCH64_OPND_SYSREG:
5629 if ((val = parse_sys_reg (&str, aarch64_sys_regs_hsh, 1, 0))
5632 set_syntax_error (_("unknown or missing system register name"));
5635 inst.base.operands[i].sysreg = val;
5638 case AARCH64_OPND_PSTATEFIELD:
5639 if ((val = parse_sys_reg (&str, aarch64_pstatefield_hsh, 0, 1))
5642 set_syntax_error (_("unknown or missing PSTATE field name"));
5645 inst.base.operands[i].pstatefield = val;
5648 case AARCH64_OPND_SYSREG_IC:
5649 inst.base.operands[i].sysins_op =
5650 parse_sys_ins_reg (&str, aarch64_sys_regs_ic_hsh);
5652 case AARCH64_OPND_SYSREG_DC:
5653 inst.base.operands[i].sysins_op =
5654 parse_sys_ins_reg (&str, aarch64_sys_regs_dc_hsh);
5656 case AARCH64_OPND_SYSREG_AT:
5657 inst.base.operands[i].sysins_op =
5658 parse_sys_ins_reg (&str, aarch64_sys_regs_at_hsh);
5660 case AARCH64_OPND_SYSREG_TLBI:
5661 inst.base.operands[i].sysins_op =
5662 parse_sys_ins_reg (&str, aarch64_sys_regs_tlbi_hsh);
5664 if (inst.base.operands[i].sysins_op == NULL)
5666 set_fatal_syntax_error ( _("unknown or missing operation name"));
5671 case AARCH64_OPND_BARRIER:
5672 case AARCH64_OPND_BARRIER_ISB:
5673 val = parse_barrier (&str);
5674 if (val != PARSE_FAIL
5675 && operands[i] == AARCH64_OPND_BARRIER_ISB && val != 0xf)
5677 /* ISB only accepts options name 'sy'. */
5679 (_("the specified option is not accepted in ISB"));
5680 /* Turn off backtrack as this optional operand is present. */
5684 /* This is an extension to accept a 0..15 immediate. */
5685 if (val == PARSE_FAIL)
5686 po_imm_or_fail (0, 15);
5687 info->barrier = aarch64_barrier_options + val;
5690 case AARCH64_OPND_PRFOP:
5691 val = parse_pldop (&str);
5692 /* This is an extension to accept a 0..31 immediate. */
5693 if (val == PARSE_FAIL)
5694 po_imm_or_fail (0, 31);
5695 inst.base.operands[i].prfop = aarch64_prfops + val;
5698 case AARCH64_OPND_BARRIER_PSB:
5699 val = parse_barrier_psb (&str, &(info->hint_option));
5700 if (val == PARSE_FAIL)
5705 as_fatal (_("unhandled operand code %d"), operands[i]);
5708 /* If we get here, this operand was successfully parsed. */
5709 inst.base.operands[i].present = 1;
5713 /* The parse routine should already have set the error, but in case
5714 not, set a default one here. */
5716 set_default_error ();
5718 if (! backtrack_pos)
5719 goto parse_operands_return;
5722 /* We reach here because this operand is marked as optional, and
5723 either no operand was supplied or the operand was supplied but it
5724 was syntactically incorrect. In the latter case we report an
5725 error. In the former case we perform a few more checks before
5726 dropping through to the code to insert the default operand. */
5728 char *tmp = backtrack_pos;
5729 char endchar = END_OF_INSN;
5731 if (i != (aarch64_num_of_operands (opcode) - 1))
5733 skip_past_char (&tmp, ',');
5735 if (*tmp != endchar)
5736 /* The user has supplied an operand in the wrong format. */
5737 goto parse_operands_return;
5739 /* Make sure there is not a comma before the optional operand.
5740 For example the fifth operand of 'sys' is optional:
5742 sys #0,c0,c0,#0, <--- wrong
5743 sys #0,c0,c0,#0 <--- correct. */
5744 if (comma_skipped_p && i && endchar == END_OF_INSN)
5746 set_fatal_syntax_error
5747 (_("unexpected comma before the omitted optional operand"));
5748 goto parse_operands_return;
5752 /* Reaching here means we are dealing with an optional operand that is
5753 omitted from the assembly line. */
5754 gas_assert (optional_operand_p (opcode, i));
5756 process_omitted_operand (operands[i], opcode, i, info);
5758 /* Try again, skipping the optional operand at backtrack_pos. */
5759 str = backtrack_pos;
5762 /* Clear any error record after the omitted optional operand has been
5763 successfully handled. */
5767 /* Check if we have parsed all the operands. */
5768 if (*str != '\0' && ! error_p ())
5770 /* Set I to the index of the last present operand; this is
5771 for the purpose of diagnostics. */
5772 for (i -= 1; i >= 0 && !inst.base.operands[i].present; --i)
5774 set_fatal_syntax_error
5775 (_("unexpected characters following instruction"));
5778 parse_operands_return:
5782 DEBUG_TRACE ("parsing FAIL: %s - %s",
5783 operand_mismatch_kind_names[get_error_kind ()],
5784 get_error_message ());
5785 /* Record the operand error properly; this is useful when there
5786 are multiple instruction templates for a mnemonic name, so that
5787 later on, we can select the error that most closely describes
5789 record_operand_error (opcode, i, get_error_kind (),
5790 get_error_message ());
5795 DEBUG_TRACE ("parsing SUCCESS");
5800 /* It does some fix-up to provide some programmer friendly feature while
5801 keeping the libopcodes happy, i.e. libopcodes only accepts
5802 the preferred architectural syntax.
5803 Return FALSE if there is any failure; otherwise return TRUE. */
5806 programmer_friendly_fixup (aarch64_instruction *instr)
5808 aarch64_inst *base = &instr->base;
5809 const aarch64_opcode *opcode = base->opcode;
5810 enum aarch64_op op = opcode->op;
5811 aarch64_opnd_info *operands = base->operands;
5813 DEBUG_TRACE ("enter");
5815 switch (opcode->iclass)
5818 /* TBNZ Xn|Wn, #uimm6, label
5819 Test and Branch Not Zero: conditionally jumps to label if bit number
5820 uimm6 in register Xn is not zero. The bit number implies the width of
5821 the register, which may be written and should be disassembled as Wn if
5822 uimm is less than 32. */
5823 if (operands[0].qualifier == AARCH64_OPND_QLF_W)
5825 if (operands[1].imm.value >= 32)
5827 record_operand_out_of_range_error (opcode, 1, _("immediate value"),
5831 operands[0].qualifier = AARCH64_OPND_QLF_X;
5835 /* LDR Wt, label | =value
5836 As a convenience assemblers will typically permit the notation
5837 "=value" in conjunction with the pc-relative literal load instructions
5838 to automatically place an immediate value or symbolic address in a
5839 nearby literal pool and generate a hidden label which references it.
5840 ISREG has been set to 0 in the case of =value. */
5841 if (instr->gen_lit_pool
5842 && (op == OP_LDR_LIT || op == OP_LDRV_LIT || op == OP_LDRSW_LIT))
5844 int size = aarch64_get_qualifier_esize (operands[0].qualifier);
5845 if (op == OP_LDRSW_LIT)
5847 if (instr->reloc.exp.X_op != O_constant
5848 && instr->reloc.exp.X_op != O_big
5849 && instr->reloc.exp.X_op != O_symbol)
5851 record_operand_error (opcode, 1,
5852 AARCH64_OPDE_FATAL_SYNTAX_ERROR,
5853 _("constant expression expected"));
5856 if (! add_to_lit_pool (&instr->reloc.exp, size))
5858 record_operand_error (opcode, 1,
5859 AARCH64_OPDE_OTHER_ERROR,
5860 _("literal pool insertion failed"));
5868 Unsigned Extend Byte|Halfword|Word: UXT[BH] is architectural alias
5869 for UBFM Wd,Wn,#0,#7|15, while UXTW is pseudo instruction which is
5870 encoded using ORR Wd, WZR, Wn (MOV Wd,Wn).
5871 A programmer-friendly assembler should accept a destination Xd in
5872 place of Wd, however that is not the preferred form for disassembly.
5874 if ((op == OP_UXTB || op == OP_UXTH || op == OP_UXTW)
5875 && operands[1].qualifier == AARCH64_OPND_QLF_W
5876 && operands[0].qualifier == AARCH64_OPND_QLF_X)
5877 operands[0].qualifier = AARCH64_OPND_QLF_W;
5882 /* In the 64-bit form, the final register operand is written as Wm
5883 for all but the (possibly omitted) UXTX/LSL and SXTX
5885 As a programmer-friendly assembler, we accept e.g.
5886 ADDS <Xd>, <Xn|SP>, <Xm>{, UXTB {#<amount>}} and change it to
5887 ADDS <Xd>, <Xn|SP>, <Wm>{, UXTB {#<amount>}}. */
5888 int idx = aarch64_operand_index (opcode->operands,
5889 AARCH64_OPND_Rm_EXT);
5890 gas_assert (idx == 1 || idx == 2);
5891 if (operands[0].qualifier == AARCH64_OPND_QLF_X
5892 && operands[idx].qualifier == AARCH64_OPND_QLF_X
5893 && operands[idx].shifter.kind != AARCH64_MOD_LSL
5894 && operands[idx].shifter.kind != AARCH64_MOD_UXTX
5895 && operands[idx].shifter.kind != AARCH64_MOD_SXTX)
5896 operands[idx].qualifier = AARCH64_OPND_QLF_W;
5904 DEBUG_TRACE ("exit with SUCCESS");
5908 /* Check for loads and stores that will cause unpredictable behavior. */
5911 warn_unpredictable_ldst (aarch64_instruction *instr, char *str)
5913 aarch64_inst *base = &instr->base;
5914 const aarch64_opcode *opcode = base->opcode;
5915 const aarch64_opnd_info *opnds = base->operands;
5916 switch (opcode->iclass)
5922 /* Loading/storing the base register is unpredictable if writeback. */
5923 if ((aarch64_get_operand_class (opnds[0].type)
5924 == AARCH64_OPND_CLASS_INT_REG)
5925 && opnds[0].reg.regno == opnds[1].addr.base_regno
5926 && opnds[1].addr.base_regno != REG_SP
5927 && opnds[1].addr.writeback)
5928 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
5931 case ldstnapair_offs:
5932 case ldstpair_indexed:
5933 /* Loading/storing the base register is unpredictable if writeback. */
5934 if ((aarch64_get_operand_class (opnds[0].type)
5935 == AARCH64_OPND_CLASS_INT_REG)
5936 && (opnds[0].reg.regno == opnds[2].addr.base_regno
5937 || opnds[1].reg.regno == opnds[2].addr.base_regno)
5938 && opnds[2].addr.base_regno != REG_SP
5939 && opnds[2].addr.writeback)
5940 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
5941 /* Load operations must load different registers. */
5942 if ((opcode->opcode & (1 << 22))
5943 && opnds[0].reg.regno == opnds[1].reg.regno)
5944 as_warn (_("unpredictable load of register pair -- `%s'"), str);
5951 /* A wrapper function to interface with libopcodes on encoding and
5952 record the error message if there is any.
5954 Return TRUE on success; otherwise return FALSE. */
5957 do_encode (const aarch64_opcode *opcode, aarch64_inst *instr,
5960 aarch64_operand_error error_info;
5961 error_info.kind = AARCH64_OPDE_NIL;
5962 if (aarch64_opcode_encode (opcode, instr, code, NULL, &error_info))
5966 gas_assert (error_info.kind != AARCH64_OPDE_NIL);
5967 record_operand_error_info (opcode, &error_info);
5972 #ifdef DEBUG_AARCH64
5974 dump_opcode_operands (const aarch64_opcode *opcode)
5977 while (opcode->operands[i] != AARCH64_OPND_NIL)
5979 aarch64_verbose ("\t\t opnd%d: %s", i,
5980 aarch64_get_operand_name (opcode->operands[i])[0] != '\0'
5981 ? aarch64_get_operand_name (opcode->operands[i])
5982 : aarch64_get_operand_desc (opcode->operands[i]));
5986 #endif /* DEBUG_AARCH64 */
5988 /* This is the guts of the machine-dependent assembler. STR points to a
5989 machine dependent instruction. This function is supposed to emit
5990 the frags/bytes it assembles to. */
5993 md_assemble (char *str)
5996 templates *template;
5997 aarch64_opcode *opcode;
5998 aarch64_inst *inst_base;
5999 unsigned saved_cond;
6001 /* Align the previous label if needed. */
6002 if (last_label_seen != NULL)
6004 symbol_set_frag (last_label_seen, frag_now);
6005 S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
6006 S_SET_SEGMENT (last_label_seen, now_seg);
6009 inst.reloc.type = BFD_RELOC_UNUSED;
6011 DEBUG_TRACE ("\n\n");
6012 DEBUG_TRACE ("==============================");
6013 DEBUG_TRACE ("Enter md_assemble with %s", str);
6015 template = opcode_lookup (&p);
6018 /* It wasn't an instruction, but it might be a register alias of
6019 the form alias .req reg directive. */
6020 if (!create_register_alias (str, p))
6021 as_bad (_("unknown mnemonic `%s' -- `%s'"), get_mnemonic_name (str),
6026 skip_whitespace (p);
6029 as_bad (_("unexpected comma after the mnemonic name `%s' -- `%s'"),
6030 get_mnemonic_name (str), str);
6034 init_operand_error_report ();
6036 /* Sections are assumed to start aligned. In executable section, there is no
6037 MAP_DATA symbol pending. So we only align the address during
6038 MAP_DATA --> MAP_INSN transition.
6039 For other sections, this is not guaranteed. */
6040 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
6041 if (!need_pass_2 && subseg_text_p (now_seg) && mapstate == MAP_DATA)
6042 frag_align_code (2, 0);
6044 saved_cond = inst.cond;
6045 reset_aarch64_instruction (&inst);
6046 inst.cond = saved_cond;
6048 /* Iterate through all opcode entries with the same mnemonic name. */
6051 opcode = template->opcode;
6053 DEBUG_TRACE ("opcode %s found", opcode->name);
6054 #ifdef DEBUG_AARCH64
6056 dump_opcode_operands (opcode);
6057 #endif /* DEBUG_AARCH64 */
6059 mapping_state (MAP_INSN);
6061 inst_base = &inst.base;
6062 inst_base->opcode = opcode;
6064 /* Truly conditionally executed instructions, e.g. b.cond. */
6065 if (opcode->flags & F_COND)
6067 gas_assert (inst.cond != COND_ALWAYS);
6068 inst_base->cond = get_cond_from_value (inst.cond);
6069 DEBUG_TRACE ("condition found %s", inst_base->cond->names[0]);
6071 else if (inst.cond != COND_ALWAYS)
6073 /* It shouldn't arrive here, where the assembly looks like a
6074 conditional instruction but the found opcode is unconditional. */
6079 if (parse_operands (p, opcode)
6080 && programmer_friendly_fixup (&inst)
6081 && do_encode (inst_base->opcode, &inst.base, &inst_base->value))
6083 /* Check that this instruction is supported for this CPU. */
6084 if (!opcode->avariant
6085 || !AARCH64_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant))
6087 as_bad (_("selected processor does not support `%s'"), str);
6091 warn_unpredictable_ldst (&inst, str);
6093 if (inst.reloc.type == BFD_RELOC_UNUSED
6094 || !inst.reloc.need_libopcodes_p)
6098 /* If there is relocation generated for the instruction,
6099 store the instruction information for the future fix-up. */
6100 struct aarch64_inst *copy;
6101 gas_assert (inst.reloc.type != BFD_RELOC_UNUSED);
6102 copy = XNEW (struct aarch64_inst);
6103 memcpy (copy, &inst.base, sizeof (struct aarch64_inst));
6109 template = template->next;
6110 if (template != NULL)
6112 reset_aarch64_instruction (&inst);
6113 inst.cond = saved_cond;
6116 while (template != NULL);
6118 /* Issue the error messages if any. */
6119 output_operand_error_report (str);
6122 /* Various frobbings of labels and their addresses. */
6125 aarch64_start_line_hook (void)
6127 last_label_seen = NULL;
6131 aarch64_frob_label (symbolS * sym)
6133 last_label_seen = sym;
6135 dwarf2_emit_label (sym);
6139 aarch64_data_in_code (void)
6141 if (!strncmp (input_line_pointer + 1, "data:", 5))
6143 *input_line_pointer = '/';
6144 input_line_pointer += 5;
6145 *input_line_pointer = 0;
6153 aarch64_canonicalize_symbol_name (char *name)
6157 if ((len = strlen (name)) > 5 && streq (name + len - 5, "/data"))
6158 *(name + len - 5) = 0;
6163 /* Table of all register names defined by default. The user can
6164 define additional names with .req. Note that all register names
6165 should appear in both upper and lowercase variants. Some registers
6166 also have mixed-case names. */
6168 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
6169 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
6170 #define REGSET31(p,t) \
6171 REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
6172 REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
6173 REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
6174 REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t), \
6175 REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
6176 REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
6177 REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
6178 REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t)
6179 #define REGSET(p,t) \
6180 REGSET31(p,t), REGNUM(p,31,t)
6182 /* These go into aarch64_reg_hsh hash-table. */
6183 static const reg_entry reg_names[] = {
6184 /* Integer registers. */
6185 REGSET31 (x, R_64), REGSET31 (X, R_64),
6186 REGSET31 (w, R_32), REGSET31 (W, R_32),
6188 REGDEF (wsp, 31, SP_32), REGDEF (WSP, 31, SP_32),
6189 REGDEF (sp, 31, SP_64), REGDEF (SP, 31, SP_64),
6191 REGDEF (wzr, 31, Z_32), REGDEF (WZR, 31, Z_32),
6192 REGDEF (xzr, 31, Z_64), REGDEF (XZR, 31, Z_64),
6194 /* Coprocessor register numbers. */
6195 REGSET (c, CN), REGSET (C, CN),
6197 /* Floating-point single precision registers. */
6198 REGSET (s, FP_S), REGSET (S, FP_S),
6200 /* Floating-point double precision registers. */
6201 REGSET (d, FP_D), REGSET (D, FP_D),
6203 /* Floating-point half precision registers. */
6204 REGSET (h, FP_H), REGSET (H, FP_H),
6206 /* Floating-point byte precision registers. */
6207 REGSET (b, FP_B), REGSET (B, FP_B),
6209 /* Floating-point quad precision registers. */
6210 REGSET (q, FP_Q), REGSET (Q, FP_Q),
6212 /* FP/SIMD registers. */
6213 REGSET (v, VN), REGSET (V, VN),
6228 #define B(a,b,c,d) (((a) << 3) | ((b) << 2) | ((c) << 1) | (d))
6229 static const asm_nzcv nzcv_names[] = {
6230 {"nzcv", B (n, z, c, v)},
6231 {"nzcV", B (n, z, c, V)},
6232 {"nzCv", B (n, z, C, v)},
6233 {"nzCV", B (n, z, C, V)},
6234 {"nZcv", B (n, Z, c, v)},
6235 {"nZcV", B (n, Z, c, V)},
6236 {"nZCv", B (n, Z, C, v)},
6237 {"nZCV", B (n, Z, C, V)},
6238 {"Nzcv", B (N, z, c, v)},
6239 {"NzcV", B (N, z, c, V)},
6240 {"NzCv", B (N, z, C, v)},
6241 {"NzCV", B (N, z, C, V)},
6242 {"NZcv", B (N, Z, c, v)},
6243 {"NZcV", B (N, Z, c, V)},
6244 {"NZCv", B (N, Z, C, v)},
6245 {"NZCV", B (N, Z, C, V)}
6258 /* MD interface: bits in the object file. */
6260 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
6261 for use in the a.out file, and stores them in the array pointed to by buf.
6262 This knows about the endian-ness of the target machine and does
6263 THE RIGHT THING, whatever it is. Possible values for n are 1 (byte)
6264 2 (short) and 4 (long) Floating numbers are put out as a series of
6265 LITTLENUMS (shorts, here at least). */
6268 md_number_to_chars (char *buf, valueT val, int n)
6270 if (target_big_endian)
6271 number_to_chars_bigendian (buf, val, n);
6273 number_to_chars_littleendian (buf, val, n);
6276 /* MD interface: Sections. */
6278 /* Estimate the size of a frag before relaxing. Assume everything fits in
6282 md_estimate_size_before_relax (fragS * fragp, segT segtype ATTRIBUTE_UNUSED)
6288 /* Round up a section size to the appropriate boundary. */
6291 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
6296 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
6297 of an rs_align_code fragment.
6299 Here we fill the frag with the appropriate info for padding the
6300 output stream. The resulting frag will consist of a fixed (fr_fix)
6301 and of a repeating (fr_var) part.
6303 The fixed content is always emitted before the repeating content and
6304 these two parts are used as follows in constructing the output:
6305 - the fixed part will be used to align to a valid instruction word
6306 boundary, in case that we start at a misaligned address; as no
6307 executable instruction can live at the misaligned location, we
6308 simply fill with zeros;
6309 - the variable part will be used to cover the remaining padding and
6310 we fill using the AArch64 NOP instruction.
6312 Note that the size of a RS_ALIGN_CODE fragment is always 7 to provide
6313 enough storage space for up to 3 bytes for padding the back to a valid
6314 instruction alignment and exactly 4 bytes to store the NOP pattern. */
6317 aarch64_handle_align (fragS * fragP)
6319 /* NOP = d503201f */
6320 /* AArch64 instructions are always little-endian. */
6321 static unsigned char const aarch64_noop[4] = { 0x1f, 0x20, 0x03, 0xd5 };
6323 int bytes, fix, noop_size;
6326 if (fragP->fr_type != rs_align_code)
6329 bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
6330 p = fragP->fr_literal + fragP->fr_fix;
6333 gas_assert (fragP->tc_frag_data.recorded);
6336 noop_size = sizeof (aarch64_noop);
6338 fix = bytes & (noop_size - 1);
6342 insert_data_mapping_symbol (MAP_INSN, fragP->fr_fix, fragP, fix);
6346 fragP->fr_fix += fix;
6350 memcpy (p, aarch64_noop, noop_size);
6351 fragP->fr_var = noop_size;
6354 /* Perform target specific initialisation of a frag.
6355 Note - despite the name this initialisation is not done when the frag
6356 is created, but only when its type is assigned. A frag can be created
6357 and used a long time before its type is set, so beware of assuming that
6358 this initialisationis performed first. */
6362 aarch64_init_frag (fragS * fragP ATTRIBUTE_UNUSED,
6363 int max_chars ATTRIBUTE_UNUSED)
6367 #else /* OBJ_ELF is defined. */
6369 aarch64_init_frag (fragS * fragP, int max_chars)
6371 /* Record a mapping symbol for alignment frags. We will delete this
6372 later if the alignment ends up empty. */
6373 if (!fragP->tc_frag_data.recorded)
6374 fragP->tc_frag_data.recorded = 1;
6376 switch (fragP->fr_type)
6381 mapping_state_2 (MAP_DATA, max_chars);
6384 mapping_state_2 (MAP_INSN, max_chars);
6391 /* Initialize the DWARF-2 unwind information for this procedure. */
6394 tc_aarch64_frame_initial_instructions (void)
6396 cfi_add_CFA_def_cfa (REG_SP, 0);
6398 #endif /* OBJ_ELF */
6400 /* Convert REGNAME to a DWARF-2 register number. */
6403 tc_aarch64_regname_to_dw2regnum (char *regname)
6405 const reg_entry *reg = parse_reg (®name);
6411 case REG_TYPE_SP_32:
6412 case REG_TYPE_SP_64:
6422 return reg->number + 64;
6430 /* Implement DWARF2_ADDR_SIZE. */
6433 aarch64_dwarf2_addr_size (void)
6435 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
6439 return bfd_arch_bits_per_address (stdoutput) / 8;
6442 /* MD interface: Symbol and relocation handling. */
6444 /* Return the address within the segment that a PC-relative fixup is
6445 relative to. For AArch64 PC-relative fixups applied to instructions
6446 are generally relative to the location plus AARCH64_PCREL_OFFSET bytes. */
6449 md_pcrel_from_section (fixS * fixP, segT seg)
6451 offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
6453 /* If this is pc-relative and we are going to emit a relocation
6454 then we just want to put out any pipeline compensation that the linker
6455 will need. Otherwise we want to use the calculated base. */
6457 && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
6458 || aarch64_force_relocation (fixP)))
6461 /* AArch64 should be consistent for all pc-relative relocations. */
6462 return base + AARCH64_PCREL_OFFSET;
6465 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
6466 Otherwise we have no need to default values of symbols. */
6469 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
6472 if (name[0] == '_' && name[1] == 'G'
6473 && streq (name, GLOBAL_OFFSET_TABLE_NAME))
6477 if (symbol_find (name))
6478 as_bad (_("GOT already in the symbol table"));
6480 GOT_symbol = symbol_new (name, undefined_section,
6481 (valueT) 0, &zero_address_frag);
6491 /* Return non-zero if the indicated VALUE has overflowed the maximum
6492 range expressible by a unsigned number with the indicated number of
6496 unsigned_overflow (valueT value, unsigned bits)
6499 if (bits >= sizeof (valueT) * 8)
6501 lim = (valueT) 1 << bits;
6502 return (value >= lim);
6506 /* Return non-zero if the indicated VALUE has overflowed the maximum
6507 range expressible by an signed number with the indicated number of
6511 signed_overflow (offsetT value, unsigned bits)
6514 if (bits >= sizeof (offsetT) * 8)
6516 lim = (offsetT) 1 << (bits - 1);
6517 return (value < -lim || value >= lim);
6520 /* Given an instruction in *INST, which is expected to be a scaled, 12-bit,
6521 unsigned immediate offset load/store instruction, try to encode it as
6522 an unscaled, 9-bit, signed immediate offset load/store instruction.
6523 Return TRUE if it is successful; otherwise return FALSE.
6525 As a programmer-friendly assembler, LDUR/STUR instructions can be generated
6526 in response to the standard LDR/STR mnemonics when the immediate offset is
6527 unambiguous, i.e. when it is negative or unaligned. */
6530 try_to_encode_as_unscaled_ldst (aarch64_inst *instr)
6533 enum aarch64_op new_op;
6534 const aarch64_opcode *new_opcode;
6536 gas_assert (instr->opcode->iclass == ldst_pos);
6538 switch (instr->opcode->op)
6540 case OP_LDRB_POS:new_op = OP_LDURB; break;
6541 case OP_STRB_POS: new_op = OP_STURB; break;
6542 case OP_LDRSB_POS: new_op = OP_LDURSB; break;
6543 case OP_LDRH_POS: new_op = OP_LDURH; break;
6544 case OP_STRH_POS: new_op = OP_STURH; break;
6545 case OP_LDRSH_POS: new_op = OP_LDURSH; break;
6546 case OP_LDR_POS: new_op = OP_LDUR; break;
6547 case OP_STR_POS: new_op = OP_STUR; break;
6548 case OP_LDRF_POS: new_op = OP_LDURV; break;
6549 case OP_STRF_POS: new_op = OP_STURV; break;
6550 case OP_LDRSW_POS: new_op = OP_LDURSW; break;
6551 case OP_PRFM_POS: new_op = OP_PRFUM; break;
6552 default: new_op = OP_NIL; break;
6555 if (new_op == OP_NIL)
6558 new_opcode = aarch64_get_opcode (new_op);
6559 gas_assert (new_opcode != NULL);
6561 DEBUG_TRACE ("Check programmer-friendly STURB/LDURB -> STRB/LDRB: %d == %d",
6562 instr->opcode->op, new_opcode->op);
6564 aarch64_replace_opcode (instr, new_opcode);
6566 /* Clear up the ADDR_SIMM9's qualifier; otherwise the
6567 qualifier matching may fail because the out-of-date qualifier will
6568 prevent the operand being updated with a new and correct qualifier. */
6569 idx = aarch64_operand_index (instr->opcode->operands,
6570 AARCH64_OPND_ADDR_SIMM9);
6571 gas_assert (idx == 1);
6572 instr->operands[idx].qualifier = AARCH64_OPND_QLF_NIL;
6574 DEBUG_TRACE ("Found LDURB entry to encode programmer-friendly LDRB");
6576 if (!aarch64_opcode_encode (instr->opcode, instr, &instr->value, NULL, NULL))
6582 /* Called by fix_insn to fix a MOV immediate alias instruction.
6584 Operand for a generic move immediate instruction, which is an alias
6585 instruction that generates a single MOVZ, MOVN or ORR instruction to loads
6586 a 32-bit/64-bit immediate value into general register. An assembler error
6587 shall result if the immediate cannot be created by a single one of these
6588 instructions. If there is a choice, then to ensure reversability an
6589 assembler must prefer a MOVZ to MOVN, and MOVZ or MOVN to ORR. */
6592 fix_mov_imm_insn (fixS *fixP, char *buf, aarch64_inst *instr, offsetT value)
6594 const aarch64_opcode *opcode;
6596 /* Need to check if the destination is SP/ZR. The check has to be done
6597 before any aarch64_replace_opcode. */
6598 int try_mov_wide_p = !aarch64_stack_pointer_p (&instr->operands[0]);
6599 int try_mov_bitmask_p = !aarch64_zero_register_p (&instr->operands[0]);
6601 instr->operands[1].imm.value = value;
6602 instr->operands[1].skip = 0;
6606 /* Try the MOVZ alias. */
6607 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDE);
6608 aarch64_replace_opcode (instr, opcode);
6609 if (aarch64_opcode_encode (instr->opcode, instr,
6610 &instr->value, NULL, NULL))
6612 put_aarch64_insn (buf, instr->value);
6615 /* Try the MOVK alias. */
6616 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDEN);
6617 aarch64_replace_opcode (instr, opcode);
6618 if (aarch64_opcode_encode (instr->opcode, instr,
6619 &instr->value, NULL, NULL))
6621 put_aarch64_insn (buf, instr->value);
6626 if (try_mov_bitmask_p)
6628 /* Try the ORR alias. */
6629 opcode = aarch64_get_opcode (OP_MOV_IMM_LOG);
6630 aarch64_replace_opcode (instr, opcode);
6631 if (aarch64_opcode_encode (instr->opcode, instr,
6632 &instr->value, NULL, NULL))
6634 put_aarch64_insn (buf, instr->value);
6639 as_bad_where (fixP->fx_file, fixP->fx_line,
6640 _("immediate cannot be moved by a single instruction"));
6643 /* An instruction operand which is immediate related may have symbol used
6644 in the assembly, e.g.
6647 .set u32, 0x00ffff00
6649 At the time when the assembly instruction is parsed, a referenced symbol,
6650 like 'u32' in the above example may not have been seen; a fixS is created
6651 in such a case and is handled here after symbols have been resolved.
6652 Instruction is fixed up with VALUE using the information in *FIXP plus
6653 extra information in FLAGS.
6655 This function is called by md_apply_fix to fix up instructions that need
6656 a fix-up described above but does not involve any linker-time relocation. */
6659 fix_insn (fixS *fixP, uint32_t flags, offsetT value)
6663 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
6664 enum aarch64_opnd opnd = fixP->tc_fix_data.opnd;
6665 aarch64_inst *new_inst = fixP->tc_fix_data.inst;
6669 /* Now the instruction is about to be fixed-up, so the operand that
6670 was previously marked as 'ignored' needs to be unmarked in order
6671 to get the encoding done properly. */
6672 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
6673 new_inst->operands[idx].skip = 0;
6676 gas_assert (opnd != AARCH64_OPND_NIL);
6680 case AARCH64_OPND_EXCEPTION:
6681 if (unsigned_overflow (value, 16))
6682 as_bad_where (fixP->fx_file, fixP->fx_line,
6683 _("immediate out of range"));
6684 insn = get_aarch64_insn (buf);
6685 insn |= encode_svc_imm (value);
6686 put_aarch64_insn (buf, insn);
6689 case AARCH64_OPND_AIMM:
6690 /* ADD or SUB with immediate.
6691 NOTE this assumes we come here with a add/sub shifted reg encoding
6692 3 322|2222|2 2 2 21111 111111
6693 1 098|7654|3 2 1 09876 543210 98765 43210
6694 0b000000 sf 000|1011|shift 0 Rm imm6 Rn Rd ADD
6695 2b000000 sf 010|1011|shift 0 Rm imm6 Rn Rd ADDS
6696 4b000000 sf 100|1011|shift 0 Rm imm6 Rn Rd SUB
6697 6b000000 sf 110|1011|shift 0 Rm imm6 Rn Rd SUBS
6699 3 322|2222|2 2 221111111111
6700 1 098|7654|3 2 109876543210 98765 43210
6701 11000000 sf 001|0001|shift imm12 Rn Rd ADD
6702 31000000 sf 011|0001|shift imm12 Rn Rd ADDS
6703 51000000 sf 101|0001|shift imm12 Rn Rd SUB
6704 71000000 sf 111|0001|shift imm12 Rn Rd SUBS
6705 Fields sf Rn Rd are already set. */
6706 insn = get_aarch64_insn (buf);
6710 insn = reencode_addsub_switch_add_sub (insn);
6714 if ((flags & FIXUP_F_HAS_EXPLICIT_SHIFT) == 0
6715 && unsigned_overflow (value, 12))
6717 /* Try to shift the value by 12 to make it fit. */
6718 if (((value >> 12) << 12) == value
6719 && ! unsigned_overflow (value, 12 + 12))
6722 insn |= encode_addsub_imm_shift_amount (1);
6726 if (unsigned_overflow (value, 12))
6727 as_bad_where (fixP->fx_file, fixP->fx_line,
6728 _("immediate out of range"));
6730 insn |= encode_addsub_imm (value);
6732 put_aarch64_insn (buf, insn);
6735 case AARCH64_OPND_SIMD_IMM:
6736 case AARCH64_OPND_SIMD_IMM_SFT:
6737 case AARCH64_OPND_LIMM:
6738 /* Bit mask immediate. */
6739 gas_assert (new_inst != NULL);
6740 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
6741 new_inst->operands[idx].imm.value = value;
6742 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
6743 &new_inst->value, NULL, NULL))
6744 put_aarch64_insn (buf, new_inst->value);
6746 as_bad_where (fixP->fx_file, fixP->fx_line,
6747 _("invalid immediate"));
6750 case AARCH64_OPND_HALF:
6751 /* 16-bit unsigned immediate. */
6752 if (unsigned_overflow (value, 16))
6753 as_bad_where (fixP->fx_file, fixP->fx_line,
6754 _("immediate out of range"));
6755 insn = get_aarch64_insn (buf);
6756 insn |= encode_movw_imm (value & 0xffff);
6757 put_aarch64_insn (buf, insn);
6760 case AARCH64_OPND_IMM_MOV:
6761 /* Operand for a generic move immediate instruction, which is
6762 an alias instruction that generates a single MOVZ, MOVN or ORR
6763 instruction to loads a 32-bit/64-bit immediate value into general
6764 register. An assembler error shall result if the immediate cannot be
6765 created by a single one of these instructions. If there is a choice,
6766 then to ensure reversability an assembler must prefer a MOVZ to MOVN,
6767 and MOVZ or MOVN to ORR. */
6768 gas_assert (new_inst != NULL);
6769 fix_mov_imm_insn (fixP, buf, new_inst, value);
6772 case AARCH64_OPND_ADDR_SIMM7:
6773 case AARCH64_OPND_ADDR_SIMM9:
6774 case AARCH64_OPND_ADDR_SIMM9_2:
6775 case AARCH64_OPND_ADDR_UIMM12:
6776 /* Immediate offset in an address. */
6777 insn = get_aarch64_insn (buf);
6779 gas_assert (new_inst != NULL && new_inst->value == insn);
6780 gas_assert (new_inst->opcode->operands[1] == opnd
6781 || new_inst->opcode->operands[2] == opnd);
6783 /* Get the index of the address operand. */
6784 if (new_inst->opcode->operands[1] == opnd)
6785 /* e.g. STR <Xt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]. */
6788 /* e.g. LDP <Qt1>, <Qt2>, [<Xn|SP>{, #<imm>}]. */
6791 /* Update the resolved offset value. */
6792 new_inst->operands[idx].addr.offset.imm = value;
6794 /* Encode/fix-up. */
6795 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
6796 &new_inst->value, NULL, NULL))
6798 put_aarch64_insn (buf, new_inst->value);
6801 else if (new_inst->opcode->iclass == ldst_pos
6802 && try_to_encode_as_unscaled_ldst (new_inst))
6804 put_aarch64_insn (buf, new_inst->value);
6808 as_bad_where (fixP->fx_file, fixP->fx_line,
6809 _("immediate offset out of range"));
6814 as_fatal (_("unhandled operand code %d"), opnd);
6818 /* Apply a fixup (fixP) to segment data, once it has been determined
6819 by our caller that we have all the info we need to fix it up.
6821 Parameter valP is the pointer to the value of the bits. */
6824 md_apply_fix (fixS * fixP, valueT * valP, segT seg)
6826 offsetT value = *valP;
6828 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
6830 unsigned flags = fixP->fx_addnumber;
6832 DEBUG_TRACE ("\n\n");
6833 DEBUG_TRACE ("~~~~~~~~~~~~~~~~~~~~~~~~~");
6834 DEBUG_TRACE ("Enter md_apply_fix");
6836 gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
6838 /* Note whether this will delete the relocation. */
6840 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
6843 /* Process the relocations. */
6844 switch (fixP->fx_r_type)
6846 case BFD_RELOC_NONE:
6847 /* This will need to go in the object file. */
6852 case BFD_RELOC_8_PCREL:
6853 if (fixP->fx_done || !seg->use_rela_p)
6854 md_number_to_chars (buf, value, 1);
6858 case BFD_RELOC_16_PCREL:
6859 if (fixP->fx_done || !seg->use_rela_p)
6860 md_number_to_chars (buf, value, 2);
6864 case BFD_RELOC_32_PCREL:
6865 if (fixP->fx_done || !seg->use_rela_p)
6866 md_number_to_chars (buf, value, 4);
6870 case BFD_RELOC_64_PCREL:
6871 if (fixP->fx_done || !seg->use_rela_p)
6872 md_number_to_chars (buf, value, 8);
6875 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
6876 /* We claim that these fixups have been processed here, even if
6877 in fact we generate an error because we do not have a reloc
6878 for them, so tc_gen_reloc() will reject them. */
6880 if (fixP->fx_addsy && !S_IS_DEFINED (fixP->fx_addsy))
6882 as_bad_where (fixP->fx_file, fixP->fx_line,
6883 _("undefined symbol %s used as an immediate value"),
6884 S_GET_NAME (fixP->fx_addsy));
6885 goto apply_fix_return;
6887 fix_insn (fixP, flags, value);
6890 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
6891 if (fixP->fx_done || !seg->use_rela_p)
6894 as_bad_where (fixP->fx_file, fixP->fx_line,
6895 _("pc-relative load offset not word aligned"));
6896 if (signed_overflow (value, 21))
6897 as_bad_where (fixP->fx_file, fixP->fx_line,
6898 _("pc-relative load offset out of range"));
6899 insn = get_aarch64_insn (buf);
6900 insn |= encode_ld_lit_ofs_19 (value >> 2);
6901 put_aarch64_insn (buf, insn);
6905 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
6906 if (fixP->fx_done || !seg->use_rela_p)
6908 if (signed_overflow (value, 21))
6909 as_bad_where (fixP->fx_file, fixP->fx_line,
6910 _("pc-relative address offset out of range"));
6911 insn = get_aarch64_insn (buf);
6912 insn |= encode_adr_imm (value);
6913 put_aarch64_insn (buf, insn);
6917 case BFD_RELOC_AARCH64_BRANCH19:
6918 if (fixP->fx_done || !seg->use_rela_p)
6921 as_bad_where (fixP->fx_file, fixP->fx_line,
6922 _("conditional branch target not word aligned"));
6923 if (signed_overflow (value, 21))
6924 as_bad_where (fixP->fx_file, fixP->fx_line,
6925 _("conditional branch out of range"));
6926 insn = get_aarch64_insn (buf);
6927 insn |= encode_cond_branch_ofs_19 (value >> 2);
6928 put_aarch64_insn (buf, insn);
6932 case BFD_RELOC_AARCH64_TSTBR14:
6933 if (fixP->fx_done || !seg->use_rela_p)
6936 as_bad_where (fixP->fx_file, fixP->fx_line,
6937 _("conditional branch target not word aligned"));
6938 if (signed_overflow (value, 16))
6939 as_bad_where (fixP->fx_file, fixP->fx_line,
6940 _("conditional branch out of range"));
6941 insn = get_aarch64_insn (buf);
6942 insn |= encode_tst_branch_ofs_14 (value >> 2);
6943 put_aarch64_insn (buf, insn);
6947 case BFD_RELOC_AARCH64_CALL26:
6948 case BFD_RELOC_AARCH64_JUMP26:
6949 if (fixP->fx_done || !seg->use_rela_p)
6952 as_bad_where (fixP->fx_file, fixP->fx_line,
6953 _("branch target not word aligned"));
6954 if (signed_overflow (value, 28))
6955 as_bad_where (fixP->fx_file, fixP->fx_line,
6956 _("branch out of range"));
6957 insn = get_aarch64_insn (buf);
6958 insn |= encode_branch_ofs_26 (value >> 2);
6959 put_aarch64_insn (buf, insn);
6963 case BFD_RELOC_AARCH64_MOVW_G0:
6964 case BFD_RELOC_AARCH64_MOVW_G0_NC:
6965 case BFD_RELOC_AARCH64_MOVW_G0_S:
6966 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
6969 case BFD_RELOC_AARCH64_MOVW_G1:
6970 case BFD_RELOC_AARCH64_MOVW_G1_NC:
6971 case BFD_RELOC_AARCH64_MOVW_G1_S:
6972 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
6975 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6977 S_SET_THREAD_LOCAL (fixP->fx_addsy);
6978 /* Should always be exported to object file, see
6979 aarch64_force_relocation(). */
6980 gas_assert (!fixP->fx_done);
6981 gas_assert (seg->use_rela_p);
6983 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6985 S_SET_THREAD_LOCAL (fixP->fx_addsy);
6986 /* Should always be exported to object file, see
6987 aarch64_force_relocation(). */
6988 gas_assert (!fixP->fx_done);
6989 gas_assert (seg->use_rela_p);
6991 case BFD_RELOC_AARCH64_MOVW_G2:
6992 case BFD_RELOC_AARCH64_MOVW_G2_NC:
6993 case BFD_RELOC_AARCH64_MOVW_G2_S:
6996 case BFD_RELOC_AARCH64_MOVW_G3:
6999 if (fixP->fx_done || !seg->use_rela_p)
7001 insn = get_aarch64_insn (buf);
7005 /* REL signed addend must fit in 16 bits */
7006 if (signed_overflow (value, 16))
7007 as_bad_where (fixP->fx_file, fixP->fx_line,
7008 _("offset out of range"));
7012 /* Check for overflow and scale. */
7013 switch (fixP->fx_r_type)
7015 case BFD_RELOC_AARCH64_MOVW_G0:
7016 case BFD_RELOC_AARCH64_MOVW_G1:
7017 case BFD_RELOC_AARCH64_MOVW_G2:
7018 case BFD_RELOC_AARCH64_MOVW_G3:
7019 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
7020 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7021 if (unsigned_overflow (value, scale + 16))
7022 as_bad_where (fixP->fx_file, fixP->fx_line,
7023 _("unsigned value out of range"));
7025 case BFD_RELOC_AARCH64_MOVW_G0_S:
7026 case BFD_RELOC_AARCH64_MOVW_G1_S:
7027 case BFD_RELOC_AARCH64_MOVW_G2_S:
7028 /* NOTE: We can only come here with movz or movn. */
7029 if (signed_overflow (value, scale + 16))
7030 as_bad_where (fixP->fx_file, fixP->fx_line,
7031 _("signed value out of range"));
7034 /* Force use of MOVN. */
7036 insn = reencode_movzn_to_movn (insn);
7040 /* Force use of MOVZ. */
7041 insn = reencode_movzn_to_movz (insn);
7045 /* Unchecked relocations. */
7051 /* Insert value into MOVN/MOVZ/MOVK instruction. */
7052 insn |= encode_movw_imm (value & 0xffff);
7054 put_aarch64_insn (buf, insn);
7058 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
7059 fixP->fx_r_type = (ilp32_p
7060 ? BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC
7061 : BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7062 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7063 /* Should always be exported to object file, see
7064 aarch64_force_relocation(). */
7065 gas_assert (!fixP->fx_done);
7066 gas_assert (seg->use_rela_p);
7069 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
7070 fixP->fx_r_type = (ilp32_p
7071 ? BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
7072 : BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC);
7073 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7074 /* Should always be exported to object file, see
7075 aarch64_force_relocation(). */
7076 gas_assert (!fixP->fx_done);
7077 gas_assert (seg->use_rela_p);
7080 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
7081 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7082 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7083 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7084 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
7085 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7086 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7087 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7088 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7089 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7090 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7091 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7092 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7093 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7094 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7095 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7096 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7097 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
7098 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
7099 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7100 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7101 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7102 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7103 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7104 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7105 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7106 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7107 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7108 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7109 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7110 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
7111 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7112 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7113 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7114 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7115 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
7116 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
7117 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
7118 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7119 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7120 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7121 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7122 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7123 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
7124 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7125 /* Should always be exported to object file, see
7126 aarch64_force_relocation(). */
7127 gas_assert (!fixP->fx_done);
7128 gas_assert (seg->use_rela_p);
7131 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
7132 /* Should always be exported to object file, see
7133 aarch64_force_relocation(). */
7134 fixP->fx_r_type = (ilp32_p
7135 ? BFD_RELOC_AARCH64_LD32_GOT_LO12_NC
7136 : BFD_RELOC_AARCH64_LD64_GOT_LO12_NC);
7137 gas_assert (!fixP->fx_done);
7138 gas_assert (seg->use_rela_p);
7141 case BFD_RELOC_AARCH64_ADD_LO12:
7142 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7143 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7144 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7145 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7146 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7147 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7148 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7149 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7150 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7151 case BFD_RELOC_AARCH64_LDST128_LO12:
7152 case BFD_RELOC_AARCH64_LDST16_LO12:
7153 case BFD_RELOC_AARCH64_LDST32_LO12:
7154 case BFD_RELOC_AARCH64_LDST64_LO12:
7155 case BFD_RELOC_AARCH64_LDST8_LO12:
7156 /* Should always be exported to object file, see
7157 aarch64_force_relocation(). */
7158 gas_assert (!fixP->fx_done);
7159 gas_assert (seg->use_rela_p);
7162 case BFD_RELOC_AARCH64_TLSDESC_ADD:
7163 case BFD_RELOC_AARCH64_TLSDESC_CALL:
7164 case BFD_RELOC_AARCH64_TLSDESC_LDR:
7167 case BFD_RELOC_UNUSED:
7168 /* An error will already have been reported. */
7172 as_bad_where (fixP->fx_file, fixP->fx_line,
7173 _("unexpected %s fixup"),
7174 bfd_get_reloc_code_name (fixP->fx_r_type));
7179 /* Free the allocated the struct aarch64_inst.
7180 N.B. currently there are very limited number of fix-up types actually use
7181 this field, so the impact on the performance should be minimal . */
7182 if (fixP->tc_fix_data.inst != NULL)
7183 free (fixP->tc_fix_data.inst);
7188 /* Translate internal representation of relocation info to BFD target
7192 tc_gen_reloc (asection * section, fixS * fixp)
7195 bfd_reloc_code_real_type code;
7197 reloc = XNEW (arelent);
7199 reloc->sym_ptr_ptr = XNEW (asymbol *);
7200 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
7201 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7205 if (section->use_rela_p)
7206 fixp->fx_offset -= md_pcrel_from_section (fixp, section);
7208 fixp->fx_offset = reloc->address;
7210 reloc->addend = fixp->fx_offset;
7212 code = fixp->fx_r_type;
7217 code = BFD_RELOC_16_PCREL;
7222 code = BFD_RELOC_32_PCREL;
7227 code = BFD_RELOC_64_PCREL;
7234 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
7235 if (reloc->howto == NULL)
7237 as_bad_where (fixp->fx_file, fixp->fx_line,
7239 ("cannot represent %s relocation in this object file format"),
7240 bfd_get_reloc_code_name (code));
7247 /* This fix_new is called by cons via TC_CONS_FIX_NEW. */
7250 cons_fix_new_aarch64 (fragS * frag, int where, int size, expressionS * exp)
7252 bfd_reloc_code_real_type type;
7256 FIXME: @@ Should look at CPU word size. */
7263 type = BFD_RELOC_16;
7266 type = BFD_RELOC_32;
7269 type = BFD_RELOC_64;
7272 as_bad (_("cannot do %u-byte relocation"), size);
7273 type = BFD_RELOC_UNUSED;
7277 fix_new_exp (frag, where, (int) size, exp, pcrel, type);
7281 aarch64_force_relocation (struct fix *fixp)
7283 switch (fixp->fx_r_type)
7285 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
7286 /* Perform these "immediate" internal relocations
7287 even if the symbol is extern or weak. */
7290 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
7291 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
7292 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
7293 /* Pseudo relocs that need to be fixed up according to
7297 case BFD_RELOC_AARCH64_ADD_LO12:
7298 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7299 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7300 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7301 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7302 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
7303 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
7304 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
7305 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
7306 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7307 case BFD_RELOC_AARCH64_LDST128_LO12:
7308 case BFD_RELOC_AARCH64_LDST16_LO12:
7309 case BFD_RELOC_AARCH64_LDST32_LO12:
7310 case BFD_RELOC_AARCH64_LDST64_LO12:
7311 case BFD_RELOC_AARCH64_LDST8_LO12:
7312 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
7313 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
7314 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
7315 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7316 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
7317 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
7318 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7319 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
7320 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
7321 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
7322 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
7323 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
7324 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
7325 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7326 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
7327 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7328 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
7329 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7330 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7331 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
7332 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
7333 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7334 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
7335 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
7336 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
7337 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7338 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7339 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7340 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7341 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7342 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7343 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7344 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
7345 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7346 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7347 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7348 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7349 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
7350 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
7351 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
7352 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7353 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7354 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7355 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7356 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7357 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
7358 /* Always leave these relocations for the linker. */
7365 return generic_force_reloc (fixp);
7371 elf64_aarch64_target_format (void)
7373 if (strcmp (TARGET_OS, "cloudabi") == 0)
7375 /* FIXME: What to do for ilp32_p ? */
7376 return target_big_endian ? "elf64-bigaarch64-cloudabi" : "elf64-littleaarch64-cloudabi";
7378 if (target_big_endian)
7379 return ilp32_p ? "elf32-bigaarch64" : "elf64-bigaarch64";
7381 return ilp32_p ? "elf32-littleaarch64" : "elf64-littleaarch64";
7385 aarch64elf_frob_symbol (symbolS * symp, int *puntp)
7387 elf_frob_symbol (symp, puntp);
7391 /* MD interface: Finalization. */
7393 /* A good place to do this, although this was probably not intended
7394 for this kind of use. We need to dump the literal pool before
7395 references are made to a null symbol pointer. */
7398 aarch64_cleanup (void)
7402 for (pool = list_of_pools; pool; pool = pool->next)
7404 /* Put it at the end of the relevant section. */
7405 subseg_set (pool->section, pool->sub_section);
7411 /* Remove any excess mapping symbols generated for alignment frags in
7412 SEC. We may have created a mapping symbol before a zero byte
7413 alignment; remove it if there's a mapping symbol after the
7416 check_mapping_symbols (bfd * abfd ATTRIBUTE_UNUSED, asection * sec,
7417 void *dummy ATTRIBUTE_UNUSED)
7419 segment_info_type *seginfo = seg_info (sec);
7422 if (seginfo == NULL || seginfo->frchainP == NULL)
7425 for (fragp = seginfo->frchainP->frch_root;
7426 fragp != NULL; fragp = fragp->fr_next)
7428 symbolS *sym = fragp->tc_frag_data.last_map;
7429 fragS *next = fragp->fr_next;
7431 /* Variable-sized frags have been converted to fixed size by
7432 this point. But if this was variable-sized to start with,
7433 there will be a fixed-size frag after it. So don't handle
7435 if (sym == NULL || next == NULL)
7438 if (S_GET_VALUE (sym) < next->fr_address)
7439 /* Not at the end of this frag. */
7441 know (S_GET_VALUE (sym) == next->fr_address);
7445 if (next->tc_frag_data.first_map != NULL)
7447 /* Next frag starts with a mapping symbol. Discard this
7449 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
7453 if (next->fr_next == NULL)
7455 /* This mapping symbol is at the end of the section. Discard
7457 know (next->fr_fix == 0 && next->fr_var == 0);
7458 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
7462 /* As long as we have empty frags without any mapping symbols,
7464 /* If the next frag is non-empty and does not start with a
7465 mapping symbol, then this mapping symbol is required. */
7466 if (next->fr_address != next->fr_next->fr_address)
7469 next = next->fr_next;
7471 while (next != NULL);
7476 /* Adjust the symbol table. */
7479 aarch64_adjust_symtab (void)
7482 /* Remove any overlapping mapping symbols generated by alignment frags. */
7483 bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
7484 /* Now do generic ELF adjustments. */
7485 elf_adjust_symtab ();
7490 checked_hash_insert (struct hash_control *table, const char *key, void *value)
7492 const char *hash_err;
7494 hash_err = hash_insert (table, key, value);
7496 printf ("Internal Error: Can't hash %s\n", key);
7500 fill_instruction_hash_table (void)
7502 aarch64_opcode *opcode = aarch64_opcode_table;
7504 while (opcode->name != NULL)
7506 templates *templ, *new_templ;
7507 templ = hash_find (aarch64_ops_hsh, opcode->name);
7509 new_templ = XNEW (templates);
7510 new_templ->opcode = opcode;
7511 new_templ->next = NULL;
7514 checked_hash_insert (aarch64_ops_hsh, opcode->name, (void *) new_templ);
7517 new_templ->next = templ->next;
7518 templ->next = new_templ;
7525 convert_to_upper (char *dst, const char *src, size_t num)
7528 for (i = 0; i < num && *src != '\0'; ++i, ++dst, ++src)
7529 *dst = TOUPPER (*src);
7533 /* Assume STR point to a lower-case string, allocate, convert and return
7534 the corresponding upper-case string. */
7535 static inline const char*
7536 get_upper_str (const char *str)
7539 size_t len = strlen (str);
7540 ret = XNEWVEC (char, len + 1);
7541 convert_to_upper (ret, str, len);
7545 /* MD interface: Initialization. */
7553 if ((aarch64_ops_hsh = hash_new ()) == NULL
7554 || (aarch64_cond_hsh = hash_new ()) == NULL
7555 || (aarch64_shift_hsh = hash_new ()) == NULL
7556 || (aarch64_sys_regs_hsh = hash_new ()) == NULL
7557 || (aarch64_pstatefield_hsh = hash_new ()) == NULL
7558 || (aarch64_sys_regs_ic_hsh = hash_new ()) == NULL
7559 || (aarch64_sys_regs_dc_hsh = hash_new ()) == NULL
7560 || (aarch64_sys_regs_at_hsh = hash_new ()) == NULL
7561 || (aarch64_sys_regs_tlbi_hsh = hash_new ()) == NULL
7562 || (aarch64_reg_hsh = hash_new ()) == NULL
7563 || (aarch64_barrier_opt_hsh = hash_new ()) == NULL
7564 || (aarch64_nzcv_hsh = hash_new ()) == NULL
7565 || (aarch64_pldop_hsh = hash_new ()) == NULL
7566 || (aarch64_hint_opt_hsh = hash_new ()) == NULL)
7567 as_fatal (_("virtual memory exhausted"));
7569 fill_instruction_hash_table ();
7571 for (i = 0; aarch64_sys_regs[i].name != NULL; ++i)
7572 checked_hash_insert (aarch64_sys_regs_hsh, aarch64_sys_regs[i].name,
7573 (void *) (aarch64_sys_regs + i));
7575 for (i = 0; aarch64_pstatefields[i].name != NULL; ++i)
7576 checked_hash_insert (aarch64_pstatefield_hsh,
7577 aarch64_pstatefields[i].name,
7578 (void *) (aarch64_pstatefields + i));
7580 for (i = 0; aarch64_sys_regs_ic[i].name != NULL; i++)
7581 checked_hash_insert (aarch64_sys_regs_ic_hsh,
7582 aarch64_sys_regs_ic[i].name,
7583 (void *) (aarch64_sys_regs_ic + i));
7585 for (i = 0; aarch64_sys_regs_dc[i].name != NULL; i++)
7586 checked_hash_insert (aarch64_sys_regs_dc_hsh,
7587 aarch64_sys_regs_dc[i].name,
7588 (void *) (aarch64_sys_regs_dc + i));
7590 for (i = 0; aarch64_sys_regs_at[i].name != NULL; i++)
7591 checked_hash_insert (aarch64_sys_regs_at_hsh,
7592 aarch64_sys_regs_at[i].name,
7593 (void *) (aarch64_sys_regs_at + i));
7595 for (i = 0; aarch64_sys_regs_tlbi[i].name != NULL; i++)
7596 checked_hash_insert (aarch64_sys_regs_tlbi_hsh,
7597 aarch64_sys_regs_tlbi[i].name,
7598 (void *) (aarch64_sys_regs_tlbi + i));
7600 for (i = 0; i < ARRAY_SIZE (reg_names); i++)
7601 checked_hash_insert (aarch64_reg_hsh, reg_names[i].name,
7602 (void *) (reg_names + i));
7604 for (i = 0; i < ARRAY_SIZE (nzcv_names); i++)
7605 checked_hash_insert (aarch64_nzcv_hsh, nzcv_names[i].template,
7606 (void *) (nzcv_names + i));
7608 for (i = 0; aarch64_operand_modifiers[i].name != NULL; i++)
7610 const char *name = aarch64_operand_modifiers[i].name;
7611 checked_hash_insert (aarch64_shift_hsh, name,
7612 (void *) (aarch64_operand_modifiers + i));
7613 /* Also hash the name in the upper case. */
7614 checked_hash_insert (aarch64_shift_hsh, get_upper_str (name),
7615 (void *) (aarch64_operand_modifiers + i));
7618 for (i = 0; i < ARRAY_SIZE (aarch64_conds); i++)
7621 /* A condition code may have alias(es), e.g. "cc", "lo" and "ul" are
7622 the same condition code. */
7623 for (j = 0; j < ARRAY_SIZE (aarch64_conds[i].names); ++j)
7625 const char *name = aarch64_conds[i].names[j];
7628 checked_hash_insert (aarch64_cond_hsh, name,
7629 (void *) (aarch64_conds + i));
7630 /* Also hash the name in the upper case. */
7631 checked_hash_insert (aarch64_cond_hsh, get_upper_str (name),
7632 (void *) (aarch64_conds + i));
7636 for (i = 0; i < ARRAY_SIZE (aarch64_barrier_options); i++)
7638 const char *name = aarch64_barrier_options[i].name;
7639 /* Skip xx00 - the unallocated values of option. */
7642 checked_hash_insert (aarch64_barrier_opt_hsh, name,
7643 (void *) (aarch64_barrier_options + i));
7644 /* Also hash the name in the upper case. */
7645 checked_hash_insert (aarch64_barrier_opt_hsh, get_upper_str (name),
7646 (void *) (aarch64_barrier_options + i));
7649 for (i = 0; i < ARRAY_SIZE (aarch64_prfops); i++)
7651 const char* name = aarch64_prfops[i].name;
7652 /* Skip the unallocated hint encodings. */
7655 checked_hash_insert (aarch64_pldop_hsh, name,
7656 (void *) (aarch64_prfops + i));
7657 /* Also hash the name in the upper case. */
7658 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
7659 (void *) (aarch64_prfops + i));
7662 for (i = 0; aarch64_hint_options[i].name != NULL; i++)
7664 const char* name = aarch64_hint_options[i].name;
7666 checked_hash_insert (aarch64_hint_opt_hsh, name,
7667 (void *) (aarch64_hint_options + i));
7668 /* Also hash the name in the upper case. */
7669 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
7670 (void *) (aarch64_hint_options + i));
7673 /* Set the cpu variant based on the command-line options. */
7675 mcpu_cpu_opt = march_cpu_opt;
7678 mcpu_cpu_opt = &cpu_default;
7680 cpu_variant = *mcpu_cpu_opt;
7682 /* Record the CPU type. */
7683 mach = ilp32_p ? bfd_mach_aarch64_ilp32 : bfd_mach_aarch64;
7685 bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
7688 /* Command line processing. */
7690 const char *md_shortopts = "m:";
7692 #ifdef AARCH64_BI_ENDIAN
7693 #define OPTION_EB (OPTION_MD_BASE + 0)
7694 #define OPTION_EL (OPTION_MD_BASE + 1)
7696 #if TARGET_BYTES_BIG_ENDIAN
7697 #define OPTION_EB (OPTION_MD_BASE + 0)
7699 #define OPTION_EL (OPTION_MD_BASE + 1)
7703 struct option md_longopts[] = {
7705 {"EB", no_argument, NULL, OPTION_EB},
7708 {"EL", no_argument, NULL, OPTION_EL},
7710 {NULL, no_argument, NULL, 0}
7713 size_t md_longopts_size = sizeof (md_longopts);
7715 struct aarch64_option_table
7717 const char *option; /* Option name to match. */
7718 const char *help; /* Help information. */
7719 int *var; /* Variable to change. */
7720 int value; /* What to change it to. */
7721 char *deprecated; /* If non-null, print this message. */
7724 static struct aarch64_option_table aarch64_opts[] = {
7725 {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
7726 {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
7728 #ifdef DEBUG_AARCH64
7729 {"mdebug-dump", N_("temporary switch for dumping"), &debug_dump, 1, NULL},
7730 #endif /* DEBUG_AARCH64 */
7731 {"mverbose-error", N_("output verbose error messages"), &verbose_error_p, 1,
7733 {"mno-verbose-error", N_("do not output verbose error messages"),
7734 &verbose_error_p, 0, NULL},
7735 {NULL, NULL, NULL, 0, NULL}
7738 struct aarch64_cpu_option_table
7741 const aarch64_feature_set value;
7742 /* The canonical name of the CPU, or NULL to use NAME converted to upper
7744 const char *canonical_name;
7747 /* This list should, at a minimum, contain all the cpu names
7748 recognized by GCC. */
7749 static const struct aarch64_cpu_option_table aarch64_cpus[] = {
7750 {"all", AARCH64_ANY, NULL},
7751 {"cortex-a35", AARCH64_FEATURE (AARCH64_ARCH_V8,
7752 AARCH64_FEATURE_CRC), "Cortex-A35"},
7753 {"cortex-a53", AARCH64_FEATURE (AARCH64_ARCH_V8,
7754 AARCH64_FEATURE_CRC), "Cortex-A53"},
7755 {"cortex-a57", AARCH64_FEATURE (AARCH64_ARCH_V8,
7756 AARCH64_FEATURE_CRC), "Cortex-A57"},
7757 {"cortex-a72", AARCH64_FEATURE (AARCH64_ARCH_V8,
7758 AARCH64_FEATURE_CRC), "Cortex-A72"},
7759 {"cortex-a73", AARCH64_FEATURE (AARCH64_ARCH_V8,
7760 AARCH64_FEATURE_CRC), "Cortex-A73"},
7761 {"exynos-m1", AARCH64_FEATURE (AARCH64_ARCH_V8,
7762 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
7763 "Samsung Exynos M1"},
7764 {"qdf24xx", AARCH64_FEATURE (AARCH64_ARCH_V8,
7765 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
7766 "Qualcomm QDF24XX"},
7767 {"thunderx", AARCH64_FEATURE (AARCH64_ARCH_V8,
7768 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
7770 /* The 'xgene-1' name is an older name for 'xgene1', which was used
7771 in earlier releases and is superseded by 'xgene1' in all
7773 {"xgene-1", AARCH64_ARCH_V8, "APM X-Gene 1"},
7774 {"xgene1", AARCH64_ARCH_V8, "APM X-Gene 1"},
7775 {"xgene2", AARCH64_FEATURE (AARCH64_ARCH_V8,
7776 AARCH64_FEATURE_CRC), "APM X-Gene 2"},
7777 {"generic", AARCH64_ARCH_V8, NULL},
7779 {NULL, AARCH64_ARCH_NONE, NULL}
7782 struct aarch64_arch_option_table
7785 const aarch64_feature_set value;
7788 /* This list should, at a minimum, contain all the architecture names
7789 recognized by GCC. */
7790 static const struct aarch64_arch_option_table aarch64_archs[] = {
7791 {"all", AARCH64_ANY},
7792 {"armv8-a", AARCH64_ARCH_V8},
7793 {"armv8.1-a", AARCH64_ARCH_V8_1},
7794 {"armv8.2-a", AARCH64_ARCH_V8_2},
7795 {NULL, AARCH64_ARCH_NONE}
7798 /* ISA extensions. */
7799 struct aarch64_option_cpu_value_table
7802 const aarch64_feature_set value;
7805 static const struct aarch64_option_cpu_value_table aarch64_features[] = {
7806 {"crc", AARCH64_FEATURE (AARCH64_FEATURE_CRC, 0)},
7807 {"crypto", AARCH64_FEATURE (AARCH64_FEATURE_CRYPTO, 0)},
7808 {"fp", AARCH64_FEATURE (AARCH64_FEATURE_FP, 0)},
7809 {"lse", AARCH64_FEATURE (AARCH64_FEATURE_LSE, 0)},
7810 {"simd", AARCH64_FEATURE (AARCH64_FEATURE_SIMD, 0)},
7811 {"pan", AARCH64_FEATURE (AARCH64_FEATURE_PAN, 0)},
7812 {"lor", AARCH64_FEATURE (AARCH64_FEATURE_LOR, 0)},
7813 {"ras", AARCH64_FEATURE (AARCH64_FEATURE_RAS, 0)},
7814 {"rdma", AARCH64_FEATURE (AARCH64_FEATURE_SIMD
7815 | AARCH64_FEATURE_RDMA, 0)},
7816 {"fp16", AARCH64_FEATURE (AARCH64_FEATURE_F16
7817 | AARCH64_FEATURE_FP, 0)},
7818 {"profile", AARCH64_FEATURE (AARCH64_FEATURE_PROFILE, 0)},
7819 {NULL, AARCH64_ARCH_NONE}
7822 struct aarch64_long_option_table
7824 const char *option; /* Substring to match. */
7825 const char *help; /* Help information. */
7826 int (*func) (const char *subopt); /* Function to decode sub-option. */
7827 char *deprecated; /* If non-null, print this message. */
7831 aarch64_parse_features (const char *str, const aarch64_feature_set **opt_p,
7832 bfd_boolean ext_only)
7834 /* We insist on extensions being added before being removed. We achieve
7835 this by using the ADDING_VALUE variable to indicate whether we are
7836 adding an extension (1) or removing it (0) and only allowing it to
7837 change in the order -1 -> 1 -> 0. */
7838 int adding_value = -1;
7839 aarch64_feature_set *ext_set = XNEW (aarch64_feature_set);
7841 /* Copy the feature set, so that we can modify it. */
7845 while (str != NULL && *str != 0)
7847 const struct aarch64_option_cpu_value_table *opt;
7848 const char *ext = NULL;
7855 as_bad (_("invalid architectural extension"));
7859 ext = strchr (++str, '+');
7865 optlen = strlen (str);
7867 if (optlen >= 2 && strncmp (str, "no", 2) == 0)
7869 if (adding_value != 0)
7874 else if (optlen > 0)
7876 if (adding_value == -1)
7878 else if (adding_value != 1)
7880 as_bad (_("must specify extensions to add before specifying "
7881 "those to remove"));
7888 as_bad (_("missing architectural extension"));
7892 gas_assert (adding_value != -1);
7894 for (opt = aarch64_features; opt->name != NULL; opt++)
7895 if (strncmp (opt->name, str, optlen) == 0)
7897 /* Add or remove the extension. */
7899 AARCH64_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->value);
7901 AARCH64_CLEAR_FEATURE (*ext_set, *ext_set, opt->value);
7905 if (opt->name == NULL)
7907 as_bad (_("unknown architectural extension `%s'"), str);
7918 aarch64_parse_cpu (const char *str)
7920 const struct aarch64_cpu_option_table *opt;
7921 const char *ext = strchr (str, '+');
7927 optlen = strlen (str);
7931 as_bad (_("missing cpu name `%s'"), str);
7935 for (opt = aarch64_cpus; opt->name != NULL; opt++)
7936 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
7938 mcpu_cpu_opt = &opt->value;
7940 return aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE);
7945 as_bad (_("unknown cpu `%s'"), str);
7950 aarch64_parse_arch (const char *str)
7952 const struct aarch64_arch_option_table *opt;
7953 const char *ext = strchr (str, '+');
7959 optlen = strlen (str);
7963 as_bad (_("missing architecture name `%s'"), str);
7967 for (opt = aarch64_archs; opt->name != NULL; opt++)
7968 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
7970 march_cpu_opt = &opt->value;
7972 return aarch64_parse_features (ext, &march_cpu_opt, FALSE);
7977 as_bad (_("unknown architecture `%s'\n"), str);
7982 struct aarch64_option_abi_value_table
7985 enum aarch64_abi_type value;
7988 static const struct aarch64_option_abi_value_table aarch64_abis[] = {
7989 {"ilp32", AARCH64_ABI_ILP32},
7990 {"lp64", AARCH64_ABI_LP64},
7995 aarch64_parse_abi (const char *str)
7997 const struct aarch64_option_abi_value_table *opt;
7998 size_t optlen = strlen (str);
8002 as_bad (_("missing abi name `%s'"), str);
8006 for (opt = aarch64_abis; opt->name != NULL; opt++)
8007 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
8009 aarch64_abi = opt->value;
8013 as_bad (_("unknown abi `%s'\n"), str);
8017 static struct aarch64_long_option_table aarch64_long_opts[] = {
8019 {"mabi=", N_("<abi name>\t specify for ABI <abi name>"),
8020 aarch64_parse_abi, NULL},
8021 #endif /* OBJ_ELF */
8022 {"mcpu=", N_("<cpu name>\t assemble for CPU <cpu name>"),
8023 aarch64_parse_cpu, NULL},
8024 {"march=", N_("<arch name>\t assemble for architecture <arch name>"),
8025 aarch64_parse_arch, NULL},
8026 {NULL, NULL, 0, NULL}
8030 md_parse_option (int c, const char *arg)
8032 struct aarch64_option_table *opt;
8033 struct aarch64_long_option_table *lopt;
8039 target_big_endian = 1;
8045 target_big_endian = 0;
8050 /* Listing option. Just ignore these, we don't support additional
8055 for (opt = aarch64_opts; opt->option != NULL; opt++)
8057 if (c == opt->option[0]
8058 && ((arg == NULL && opt->option[1] == 0)
8059 || streq (arg, opt->option + 1)))
8061 /* If the option is deprecated, tell the user. */
8062 if (opt->deprecated != NULL)
8063 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
8064 arg ? arg : "", _(opt->deprecated));
8066 if (opt->var != NULL)
8067 *opt->var = opt->value;
8073 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
8075 /* These options are expected to have an argument. */
8076 if (c == lopt->option[0]
8078 && strncmp (arg, lopt->option + 1,
8079 strlen (lopt->option + 1)) == 0)
8081 /* If the option is deprecated, tell the user. */
8082 if (lopt->deprecated != NULL)
8083 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
8084 _(lopt->deprecated));
8086 /* Call the sup-option parser. */
8087 return lopt->func (arg + strlen (lopt->option) - 1);
8098 md_show_usage (FILE * fp)
8100 struct aarch64_option_table *opt;
8101 struct aarch64_long_option_table *lopt;
8103 fprintf (fp, _(" AArch64-specific assembler options:\n"));
8105 for (opt = aarch64_opts; opt->option != NULL; opt++)
8106 if (opt->help != NULL)
8107 fprintf (fp, " -%-23s%s\n", opt->option, _(opt->help));
8109 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
8110 if (lopt->help != NULL)
8111 fprintf (fp, " -%s%s\n", lopt->option, _(lopt->help));
8115 -EB assemble code for a big-endian cpu\n"));
8120 -EL assemble code for a little-endian cpu\n"));
8124 /* Parse a .cpu directive. */
8127 s_aarch64_cpu (int ignored ATTRIBUTE_UNUSED)
8129 const struct aarch64_cpu_option_table *opt;
8135 name = input_line_pointer;
8136 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8137 input_line_pointer++;
8138 saved_char = *input_line_pointer;
8139 *input_line_pointer = 0;
8141 ext = strchr (name, '+');
8144 optlen = ext - name;
8146 optlen = strlen (name);
8148 /* Skip the first "all" entry. */
8149 for (opt = aarch64_cpus + 1; opt->name != NULL; opt++)
8150 if (strlen (opt->name) == optlen
8151 && strncmp (name, opt->name, optlen) == 0)
8153 mcpu_cpu_opt = &opt->value;
8155 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE))
8158 cpu_variant = *mcpu_cpu_opt;
8160 *input_line_pointer = saved_char;
8161 demand_empty_rest_of_line ();
8164 as_bad (_("unknown cpu `%s'"), name);
8165 *input_line_pointer = saved_char;
8166 ignore_rest_of_line ();
8170 /* Parse a .arch directive. */
8173 s_aarch64_arch (int ignored ATTRIBUTE_UNUSED)
8175 const struct aarch64_arch_option_table *opt;
8181 name = input_line_pointer;
8182 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8183 input_line_pointer++;
8184 saved_char = *input_line_pointer;
8185 *input_line_pointer = 0;
8187 ext = strchr (name, '+');
8190 optlen = ext - name;
8192 optlen = strlen (name);
8194 /* Skip the first "all" entry. */
8195 for (opt = aarch64_archs + 1; opt->name != NULL; opt++)
8196 if (strlen (opt->name) == optlen
8197 && strncmp (name, opt->name, optlen) == 0)
8199 mcpu_cpu_opt = &opt->value;
8201 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE))
8204 cpu_variant = *mcpu_cpu_opt;
8206 *input_line_pointer = saved_char;
8207 demand_empty_rest_of_line ();
8211 as_bad (_("unknown architecture `%s'\n"), name);
8212 *input_line_pointer = saved_char;
8213 ignore_rest_of_line ();
8216 /* Parse a .arch_extension directive. */
8219 s_aarch64_arch_extension (int ignored ATTRIBUTE_UNUSED)
8222 char *ext = input_line_pointer;;
8224 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8225 input_line_pointer++;
8226 saved_char = *input_line_pointer;
8227 *input_line_pointer = 0;
8229 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, TRUE))
8232 cpu_variant = *mcpu_cpu_opt;
8234 *input_line_pointer = saved_char;
8235 demand_empty_rest_of_line ();
8238 /* Copy symbol information. */
8241 aarch64_copy_symbol_attributes (symbolS * dest, symbolS * src)
8243 AARCH64_GET_FLAG (dest) = AARCH64_GET_FLAG (src);