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