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