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