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