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