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