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