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