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