[AArch64][SVE 12/32] Remove boolean parameters from parse_address_main
[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;
4137 idx = -1;
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");
4149 if (i != 0 && idx == -1)
4150 /* If nothing has been matched, return the 1st sequence. */
4151 idx = 0;
4152 break;
4153 }
4154
4155 for (j = 0, num_matched = 0; j < num_opnds; ++j, ++qualifiers)
4156 if (*qualifiers == instr->operands[j].qualifier)
4157 ++num_matched;
4158
4159 if (num_matched > max_num_matched)
4160 {
4161 max_num_matched = num_matched;
4162 idx = i;
4163 }
4164 }
4165
4166 DEBUG_TRACE ("return with %d", idx);
4167 return idx;
4168}
4169
4170/* Assign qualifiers in the qualifier seqence (headed by QUALIFIERS) to the
4171 corresponding operands in *INSTR. */
4172
4173static inline void
4174assign_qualifier_sequence (aarch64_inst *instr,
4175 const aarch64_opnd_qualifier_t *qualifiers)
4176{
4177 int i = 0;
4178 int num_opnds = aarch64_num_of_operands (instr->opcode);
4179 gas_assert (num_opnds);
4180 for (i = 0; i < num_opnds; ++i, ++qualifiers)
4181 instr->operands[i].qualifier = *qualifiers;
4182}
4183
4184/* Print operands for the diagnosis purpose. */
4185
4186static void
4187print_operands (char *buf, const aarch64_opcode *opcode,
4188 const aarch64_opnd_info *opnds)
4189{
4190 int i;
4191
4192 for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
4193 {
08d3b0cc 4194 char str[128];
a06ea964
NC
4195
4196 /* We regard the opcode operand info more, however we also look into
4197 the inst->operands to support the disassembling of the optional
4198 operand.
4199 The two operand code should be the same in all cases, apart from
4200 when the operand can be optional. */
4201 if (opcode->operands[i] == AARCH64_OPND_NIL
4202 || opnds[i].type == AARCH64_OPND_NIL)
4203 break;
4204
4205 /* Generate the operand string in STR. */
08d3b0cc 4206 aarch64_print_operand (str, sizeof (str), 0, opcode, opnds, i, NULL, NULL);
a06ea964
NC
4207
4208 /* Delimiter. */
4209 if (str[0] != '\0')
4210 strcat (buf, i == 0 ? " " : ",");
4211
4212 /* Append the operand string. */
4213 strcat (buf, str);
4214 }
4215}
4216
4217/* Send to stderr a string as information. */
4218
4219static void
4220output_info (const char *format, ...)
4221{
3b4dbbbf 4222 const char *file;
a06ea964
NC
4223 unsigned int line;
4224 va_list args;
4225
3b4dbbbf 4226 file = as_where (&line);
a06ea964
NC
4227 if (file)
4228 {
4229 if (line != 0)
4230 fprintf (stderr, "%s:%u: ", file, line);
4231 else
4232 fprintf (stderr, "%s: ", file);
4233 }
4234 fprintf (stderr, _("Info: "));
4235 va_start (args, format);
4236 vfprintf (stderr, format, args);
4237 va_end (args);
4238 (void) putc ('\n', stderr);
4239}
4240
4241/* Output one operand error record. */
4242
4243static void
4244output_operand_error_record (const operand_error_record *record, char *str)
4245{
28f013d5
JB
4246 const aarch64_operand_error *detail = &record->detail;
4247 int idx = detail->index;
a06ea964 4248 const aarch64_opcode *opcode = record->opcode;
28f013d5 4249 enum aarch64_opnd opd_code = (idx >= 0 ? opcode->operands[idx]
a06ea964 4250 : AARCH64_OPND_NIL);
a06ea964
NC
4251
4252 switch (detail->kind)
4253 {
4254 case AARCH64_OPDE_NIL:
4255 gas_assert (0);
4256 break;
4257
4258 case AARCH64_OPDE_SYNTAX_ERROR:
4259 case AARCH64_OPDE_RECOVERABLE:
4260 case AARCH64_OPDE_FATAL_SYNTAX_ERROR:
4261 case AARCH64_OPDE_OTHER_ERROR:
a06ea964
NC
4262 /* Use the prepared error message if there is, otherwise use the
4263 operand description string to describe the error. */
4264 if (detail->error != NULL)
4265 {
28f013d5 4266 if (idx < 0)
a06ea964
NC
4267 as_bad (_("%s -- `%s'"), detail->error, str);
4268 else
4269 as_bad (_("%s at operand %d -- `%s'"),
28f013d5 4270 detail->error, idx + 1, str);
a06ea964
NC
4271 }
4272 else
28f013d5
JB
4273 {
4274 gas_assert (idx >= 0);
4275 as_bad (_("operand %d should be %s -- `%s'"), idx + 1,
a06ea964 4276 aarch64_get_operand_desc (opd_code), str);
28f013d5 4277 }
a06ea964
NC
4278 break;
4279
4280 case AARCH64_OPDE_INVALID_VARIANT:
4281 as_bad (_("operand mismatch -- `%s'"), str);
4282 if (verbose_error_p)
4283 {
4284 /* We will try to correct the erroneous instruction and also provide
4285 more information e.g. all other valid variants.
4286
4287 The string representation of the corrected instruction and other
4288 valid variants are generated by
4289
4290 1) obtaining the intermediate representation of the erroneous
4291 instruction;
4292 2) manipulating the IR, e.g. replacing the operand qualifier;
4293 3) printing out the instruction by calling the printer functions
4294 shared with the disassembler.
4295
4296 The limitation of this method is that the exact input assembly
4297 line cannot be accurately reproduced in some cases, for example an
4298 optional operand present in the actual assembly line will be
4299 omitted in the output; likewise for the optional syntax rules,
4300 e.g. the # before the immediate. Another limitation is that the
4301 assembly symbols and relocation operations in the assembly line
4302 currently cannot be printed out in the error report. Last but not
4303 least, when there is other error(s) co-exist with this error, the
4304 'corrected' instruction may be still incorrect, e.g. given
4305 'ldnp h0,h1,[x0,#6]!'
4306 this diagnosis will provide the version:
4307 'ldnp s0,s1,[x0,#6]!'
4308 which is still not right. */
4309 size_t len = strlen (get_mnemonic_name (str));
4310 int i, qlf_idx;
4311 bfd_boolean result;
08d3b0cc 4312 char buf[2048];
a06ea964
NC
4313 aarch64_inst *inst_base = &inst.base;
4314 const aarch64_opnd_qualifier_seq_t *qualifiers_list;
4315
4316 /* Init inst. */
4317 reset_aarch64_instruction (&inst);
4318 inst_base->opcode = opcode;
4319
4320 /* Reset the error report so that there is no side effect on the
4321 following operand parsing. */
4322 init_operand_error_report ();
4323
4324 /* Fill inst. */
4325 result = parse_operands (str + len, opcode)
4326 && programmer_friendly_fixup (&inst);
4327 gas_assert (result);
4328 result = aarch64_opcode_encode (opcode, inst_base, &inst_base->value,
4329 NULL, NULL);
4330 gas_assert (!result);
4331
4332 /* Find the most matched qualifier sequence. */
4333 qlf_idx = find_best_match (inst_base, opcode->qualifiers_list);
4334 gas_assert (qlf_idx > -1);
4335
4336 /* Assign the qualifiers. */
4337 assign_qualifier_sequence (inst_base,
4338 opcode->qualifiers_list[qlf_idx]);
4339
4340 /* Print the hint. */
4341 output_info (_(" did you mean this?"));
08d3b0cc 4342 snprintf (buf, sizeof (buf), "\t%s", get_mnemonic_name (str));
a06ea964
NC
4343 print_operands (buf, opcode, inst_base->operands);
4344 output_info (_(" %s"), buf);
4345
4346 /* Print out other variant(s) if there is any. */
4347 if (qlf_idx != 0 ||
4348 !empty_qualifier_sequence_p (opcode->qualifiers_list[1]))
4349 output_info (_(" other valid variant(s):"));
4350
4351 /* For each pattern. */
4352 qualifiers_list = opcode->qualifiers_list;
4353 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
4354 {
4355 /* Most opcodes has much fewer patterns in the list.
4356 First NIL qualifier indicates the end in the list. */
4357 if (empty_qualifier_sequence_p (*qualifiers_list) == TRUE)
4358 break;
4359
4360 if (i != qlf_idx)
4361 {
4362 /* Mnemonics name. */
08d3b0cc 4363 snprintf (buf, sizeof (buf), "\t%s", get_mnemonic_name (str));
a06ea964
NC
4364
4365 /* Assign the qualifiers. */
4366 assign_qualifier_sequence (inst_base, *qualifiers_list);
4367
4368 /* Print instruction. */
4369 print_operands (buf, opcode, inst_base->operands);
4370
4371 output_info (_(" %s"), buf);
4372 }
4373 }
4374 }
4375 break;
4376
4377 case AARCH64_OPDE_OUT_OF_RANGE:
f5555712
YZ
4378 if (detail->data[0] != detail->data[1])
4379 as_bad (_("%s out of range %d to %d at operand %d -- `%s'"),
4380 detail->error ? detail->error : _("immediate value"),
28f013d5 4381 detail->data[0], detail->data[1], idx + 1, str);
f5555712
YZ
4382 else
4383 as_bad (_("%s expected to be %d at operand %d -- `%s'"),
4384 detail->error ? detail->error : _("immediate value"),
28f013d5 4385 detail->data[0], idx + 1, str);
a06ea964
NC
4386 break;
4387
4388 case AARCH64_OPDE_REG_LIST:
4389 if (detail->data[0] == 1)
4390 as_bad (_("invalid number of registers in the list; "
4391 "only 1 register is expected at operand %d -- `%s'"),
28f013d5 4392 idx + 1, str);
a06ea964
NC
4393 else
4394 as_bad (_("invalid number of registers in the list; "
4395 "%d registers are expected at operand %d -- `%s'"),
28f013d5 4396 detail->data[0], idx + 1, str);
a06ea964
NC
4397 break;
4398
4399 case AARCH64_OPDE_UNALIGNED:
4400 as_bad (_("immediate value should be a multiple of "
4401 "%d at operand %d -- `%s'"),
28f013d5 4402 detail->data[0], idx + 1, str);
a06ea964
NC
4403 break;
4404
4405 default:
4406 gas_assert (0);
4407 break;
4408 }
4409}
4410
4411/* Process and output the error message about the operand mismatching.
4412
4413 When this function is called, the operand error information had
4414 been collected for an assembly line and there will be multiple
4415 errors in the case of mulitple instruction templates; output the
4416 error message that most closely describes the problem. */
4417
4418static void
4419output_operand_error_report (char *str)
4420{
4421 int largest_error_pos;
4422 const char *msg = NULL;
4423 enum aarch64_operand_error_kind kind;
4424 operand_error_record *curr;
4425 operand_error_record *head = operand_error_report.head;
4426 operand_error_record *record = NULL;
4427
4428 /* No error to report. */
4429 if (head == NULL)
4430 return;
4431
4432 gas_assert (head != NULL && operand_error_report.tail != NULL);
4433
4434 /* Only one error. */
4435 if (head == operand_error_report.tail)
4436 {
4437 DEBUG_TRACE ("single opcode entry with error kind: %s",
4438 operand_mismatch_kind_names[head->detail.kind]);
4439 output_operand_error_record (head, str);
4440 return;
4441 }
4442
4443 /* Find the error kind of the highest severity. */
4444 DEBUG_TRACE ("multiple opcode entres with error kind");
4445 kind = AARCH64_OPDE_NIL;
4446 for (curr = head; curr != NULL; curr = curr->next)
4447 {
4448 gas_assert (curr->detail.kind != AARCH64_OPDE_NIL);
4449 DEBUG_TRACE ("\t%s", operand_mismatch_kind_names[curr->detail.kind]);
4450 if (operand_error_higher_severity_p (curr->detail.kind, kind))
4451 kind = curr->detail.kind;
4452 }
4453 gas_assert (kind != AARCH64_OPDE_NIL);
4454
4455 /* Pick up one of errors of KIND to report. */
4456 largest_error_pos = -2; /* Index can be -1 which means unknown index. */
4457 for (curr = head; curr != NULL; curr = curr->next)
4458 {
4459 if (curr->detail.kind != kind)
4460 continue;
4461 /* If there are multiple errors, pick up the one with the highest
4462 mismatching operand index. In the case of multiple errors with
4463 the equally highest operand index, pick up the first one or the
4464 first one with non-NULL error message. */
4465 if (curr->detail.index > largest_error_pos
4466 || (curr->detail.index == largest_error_pos && msg == NULL
4467 && curr->detail.error != NULL))
4468 {
4469 largest_error_pos = curr->detail.index;
4470 record = curr;
4471 msg = record->detail.error;
4472 }
4473 }
4474
4475 gas_assert (largest_error_pos != -2 && record != NULL);
4476 DEBUG_TRACE ("Pick up error kind %s to report",
4477 operand_mismatch_kind_names[record->detail.kind]);
4478
4479 /* Output. */
4480 output_operand_error_record (record, str);
4481}
4482\f
4483/* Write an AARCH64 instruction to buf - always little-endian. */
4484static void
4485put_aarch64_insn (char *buf, uint32_t insn)
4486{
4487 unsigned char *where = (unsigned char *) buf;
4488 where[0] = insn;
4489 where[1] = insn >> 8;
4490 where[2] = insn >> 16;
4491 where[3] = insn >> 24;
4492}
4493
4494static uint32_t
4495get_aarch64_insn (char *buf)
4496{
4497 unsigned char *where = (unsigned char *) buf;
4498 uint32_t result;
4499 result = (where[0] | (where[1] << 8) | (where[2] << 16) | (where[3] << 24));
4500 return result;
4501}
4502
4503static void
4504output_inst (struct aarch64_inst *new_inst)
4505{
4506 char *to = NULL;
4507
4508 to = frag_more (INSN_SIZE);
4509
4510 frag_now->tc_frag_data.recorded = 1;
4511
4512 put_aarch64_insn (to, inst.base.value);
4513
4514 if (inst.reloc.type != BFD_RELOC_UNUSED)
4515 {
4516 fixS *fixp = fix_new_aarch64 (frag_now, to - frag_now->fr_literal,
4517 INSN_SIZE, &inst.reloc.exp,
4518 inst.reloc.pc_rel,
4519 inst.reloc.type);
4520 DEBUG_TRACE ("Prepared relocation fix up");
4521 /* Don't check the addend value against the instruction size,
4522 that's the job of our code in md_apply_fix(). */
4523 fixp->fx_no_overflow = 1;
4524 if (new_inst != NULL)
4525 fixp->tc_fix_data.inst = new_inst;
4526 if (aarch64_gas_internal_fixup_p ())
4527 {
4528 gas_assert (inst.reloc.opnd != AARCH64_OPND_NIL);
4529 fixp->tc_fix_data.opnd = inst.reloc.opnd;
4530 fixp->fx_addnumber = inst.reloc.flags;
4531 }
4532 }
4533
4534 dwarf2_emit_insn (INSN_SIZE);
4535}
4536
4537/* Link together opcodes of the same name. */
4538
4539struct templates
4540{
4541 aarch64_opcode *opcode;
4542 struct templates *next;
4543};
4544
4545typedef struct templates templates;
4546
4547static templates *
4548lookup_mnemonic (const char *start, int len)
4549{
4550 templates *templ = NULL;
4551
4552 templ = hash_find_n (aarch64_ops_hsh, start, len);
4553 return templ;
4554}
4555
4556/* Subroutine of md_assemble, responsible for looking up the primary
4557 opcode from the mnemonic the user wrote. STR points to the
4558 beginning of the mnemonic. */
4559
4560static templates *
4561opcode_lookup (char **str)
4562{
4563 char *end, *base;
4564 const aarch64_cond *cond;
4565 char condname[16];
4566 int len;
4567
4568 /* Scan up to the end of the mnemonic, which must end in white space,
4569 '.', or end of string. */
4570 for (base = end = *str; is_part_of_name(*end); end++)
4571 if (*end == '.')
4572 break;
4573
4574 if (end == base)
4575 return 0;
4576
4577 inst.cond = COND_ALWAYS;
4578
4579 /* Handle a possible condition. */
4580 if (end[0] == '.')
4581 {
4582 cond = hash_find_n (aarch64_cond_hsh, end + 1, 2);
4583 if (cond)
4584 {
4585 inst.cond = cond->value;
4586 *str = end + 3;
4587 }
4588 else
4589 {
4590 *str = end;
4591 return 0;
4592 }
4593 }
4594 else
4595 *str = end;
4596
4597 len = end - base;
4598
4599 if (inst.cond == COND_ALWAYS)
4600 {
4601 /* Look for unaffixed mnemonic. */
4602 return lookup_mnemonic (base, len);
4603 }
4604 else if (len <= 13)
4605 {
4606 /* append ".c" to mnemonic if conditional */
4607 memcpy (condname, base, len);
4608 memcpy (condname + len, ".c", 2);
4609 base = condname;
4610 len += 2;
4611 return lookup_mnemonic (base, len);
4612 }
4613
4614 return NULL;
4615}
4616
8f9a77af
RS
4617/* Internal helper routine converting a vector_type_el structure *VECTYPE
4618 to a corresponding operand qualifier. */
a06ea964
NC
4619
4620static inline aarch64_opnd_qualifier_t
8f9a77af 4621vectype_to_qualifier (const struct vector_type_el *vectype)
a06ea964 4622{
f06935a5 4623 /* Element size in bytes indexed by vector_el_type. */
a06ea964
NC
4624 const unsigned char ele_size[5]
4625 = {1, 2, 4, 8, 16};
65f2205d
MW
4626 const unsigned int ele_base [5] =
4627 {
4628 AARCH64_OPND_QLF_V_8B,
3067d3b9 4629 AARCH64_OPND_QLF_V_2H,
65f2205d
MW
4630 AARCH64_OPND_QLF_V_2S,
4631 AARCH64_OPND_QLF_V_1D,
4632 AARCH64_OPND_QLF_V_1Q
4633 };
a06ea964
NC
4634
4635 if (!vectype->defined || vectype->type == NT_invtype)
4636 goto vectype_conversion_fail;
4637
4638 gas_assert (vectype->type >= NT_b && vectype->type <= NT_q);
4639
4640 if (vectype->defined & NTA_HASINDEX)
4641 /* Vector element register. */
4642 return AARCH64_OPND_QLF_S_B + vectype->type;
4643 else
4644 {
4645 /* Vector register. */
4646 int reg_size = ele_size[vectype->type] * vectype->width;
4647 unsigned offset;
65f2205d 4648 unsigned shift;
3067d3b9 4649 if (reg_size != 16 && reg_size != 8 && reg_size != 4)
a06ea964 4650 goto vectype_conversion_fail;
65f2205d
MW
4651
4652 /* The conversion is by calculating the offset from the base operand
4653 qualifier for the vector type. The operand qualifiers are regular
4654 enough that the offset can established by shifting the vector width by
4655 a vector-type dependent amount. */
4656 shift = 0;
4657 if (vectype->type == NT_b)
4658 shift = 4;
3067d3b9 4659 else if (vectype->type == NT_h || vectype->type == NT_s)
65f2205d
MW
4660 shift = 2;
4661 else if (vectype->type >= NT_d)
4662 shift = 1;
4663 else
4664 gas_assert (0);
4665
4666 offset = ele_base [vectype->type] + (vectype->width >> shift);
4667 gas_assert (AARCH64_OPND_QLF_V_8B <= offset
4668 && offset <= AARCH64_OPND_QLF_V_1Q);
4669 return offset;
a06ea964
NC
4670 }
4671
4672vectype_conversion_fail:
4673 first_error (_("bad vector arrangement type"));
4674 return AARCH64_OPND_QLF_NIL;
4675}
4676
4677/* Process an optional operand that is found omitted from the assembly line.
4678 Fill *OPERAND for such an operand of type TYPE. OPCODE points to the
4679 instruction's opcode entry while IDX is the index of this omitted operand.
4680 */
4681
4682static void
4683process_omitted_operand (enum aarch64_opnd type, const aarch64_opcode *opcode,
4684 int idx, aarch64_opnd_info *operand)
4685{
4686 aarch64_insn default_value = get_optional_operand_default_value (opcode);
4687 gas_assert (optional_operand_p (opcode, idx));
4688 gas_assert (!operand->present);
4689
4690 switch (type)
4691 {
4692 case AARCH64_OPND_Rd:
4693 case AARCH64_OPND_Rn:
4694 case AARCH64_OPND_Rm:
4695 case AARCH64_OPND_Rt:
4696 case AARCH64_OPND_Rt2:
4697 case AARCH64_OPND_Rs:
4698 case AARCH64_OPND_Ra:
4699 case AARCH64_OPND_Rt_SYS:
4700 case AARCH64_OPND_Rd_SP:
4701 case AARCH64_OPND_Rn_SP:
4702 case AARCH64_OPND_Fd:
4703 case AARCH64_OPND_Fn:
4704 case AARCH64_OPND_Fm:
4705 case AARCH64_OPND_Fa:
4706 case AARCH64_OPND_Ft:
4707 case AARCH64_OPND_Ft2:
4708 case AARCH64_OPND_Sd:
4709 case AARCH64_OPND_Sn:
4710 case AARCH64_OPND_Sm:
4711 case AARCH64_OPND_Vd:
4712 case AARCH64_OPND_Vn:
4713 case AARCH64_OPND_Vm:
4714 case AARCH64_OPND_VdD1:
4715 case AARCH64_OPND_VnD1:
4716 operand->reg.regno = default_value;
4717 break;
4718
4719 case AARCH64_OPND_Ed:
4720 case AARCH64_OPND_En:
4721 case AARCH64_OPND_Em:
4722 operand->reglane.regno = default_value;
4723 break;
4724
4725 case AARCH64_OPND_IDX:
4726 case AARCH64_OPND_BIT_NUM:
4727 case AARCH64_OPND_IMMR:
4728 case AARCH64_OPND_IMMS:
4729 case AARCH64_OPND_SHLL_IMM:
4730 case AARCH64_OPND_IMM_VLSL:
4731 case AARCH64_OPND_IMM_VLSR:
4732 case AARCH64_OPND_CCMP_IMM:
4733 case AARCH64_OPND_FBITS:
4734 case AARCH64_OPND_UIMM4:
4735 case AARCH64_OPND_UIMM3_OP1:
4736 case AARCH64_OPND_UIMM3_OP2:
4737 case AARCH64_OPND_IMM:
4738 case AARCH64_OPND_WIDTH:
4739 case AARCH64_OPND_UIMM7:
4740 case AARCH64_OPND_NZCV:
4741 operand->imm.value = default_value;
4742 break;
4743
4744 case AARCH64_OPND_EXCEPTION:
4745 inst.reloc.type = BFD_RELOC_UNUSED;
4746 break;
4747
4748 case AARCH64_OPND_BARRIER_ISB:
4749 operand->barrier = aarch64_barrier_options + default_value;
4750
4751 default:
4752 break;
4753 }
4754}
4755
4756/* Process the relocation type for move wide instructions.
4757 Return TRUE on success; otherwise return FALSE. */
4758
4759static bfd_boolean
4760process_movw_reloc_info (void)
4761{
4762 int is32;
4763 unsigned shift;
4764
4765 is32 = inst.base.operands[0].qualifier == AARCH64_OPND_QLF_W ? 1 : 0;
4766
4767 if (inst.base.opcode->op == OP_MOVK)
4768 switch (inst.reloc.type)
4769 {
4770 case BFD_RELOC_AARCH64_MOVW_G0_S:
4771 case BFD_RELOC_AARCH64_MOVW_G1_S:
4772 case BFD_RELOC_AARCH64_MOVW_G2_S:
1aa66fb1 4773 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 4774 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
a06ea964 4775 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
a06ea964
NC
4776 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4777 set_syntax_error
4778 (_("the specified relocation type is not allowed for MOVK"));
4779 return FALSE;
4780 default:
4781 break;
4782 }
4783
4784 switch (inst.reloc.type)
4785 {
4786 case BFD_RELOC_AARCH64_MOVW_G0:
a06ea964 4787 case BFD_RELOC_AARCH64_MOVW_G0_NC:
f09c556a 4788 case BFD_RELOC_AARCH64_MOVW_G0_S:
ca632371 4789 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
43a357f9 4790 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
3e8286c0 4791 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
3b957e5b 4792 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
49df5539
JW
4793 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
4794 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
a06ea964
NC
4795 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
4796 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4797 shift = 0;
4798 break;
4799 case BFD_RELOC_AARCH64_MOVW_G1:
a06ea964 4800 case BFD_RELOC_AARCH64_MOVW_G1_NC:
f09c556a 4801 case BFD_RELOC_AARCH64_MOVW_G1_S:
654248e7 4802 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
43a357f9 4803 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
1aa66fb1 4804 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
3b957e5b 4805 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
49df5539
JW
4806 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
4807 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
a06ea964
NC
4808 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
4809 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4810 shift = 16;
4811 break;
4812 case BFD_RELOC_AARCH64_MOVW_G2:
a06ea964 4813 case BFD_RELOC_AARCH64_MOVW_G2_NC:
f09c556a 4814 case BFD_RELOC_AARCH64_MOVW_G2_S:
49df5539 4815 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964
NC
4816 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
4817 if (is32)
4818 {
4819 set_fatal_syntax_error
4820 (_("the specified relocation type is not allowed for 32-bit "
4821 "register"));
4822 return FALSE;
4823 }
4824 shift = 32;
4825 break;
4826 case BFD_RELOC_AARCH64_MOVW_G3:
4827 if (is32)
4828 {
4829 set_fatal_syntax_error
4830 (_("the specified relocation type is not allowed for 32-bit "
4831 "register"));
4832 return FALSE;
4833 }
4834 shift = 48;
4835 break;
4836 default:
4837 /* More cases should be added when more MOVW-related relocation types
4838 are supported in GAS. */
4839 gas_assert (aarch64_gas_internal_fixup_p ());
4840 /* The shift amount should have already been set by the parser. */
4841 return TRUE;
4842 }
4843 inst.base.operands[1].shifter.amount = shift;
4844 return TRUE;
4845}
4846
4847/* A primitive log caculator. */
4848
4849static inline unsigned int
4850get_logsz (unsigned int size)
4851{
4852 const unsigned char ls[16] =
4853 {0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
4854 if (size > 16)
4855 {
4856 gas_assert (0);
4857 return -1;
4858 }
4859 gas_assert (ls[size - 1] != (unsigned char)-1);
4860 return ls[size - 1];
4861}
4862
4863/* Determine and return the real reloc type code for an instruction
4864 with the pseudo reloc type code BFD_RELOC_AARCH64_LDST_LO12. */
4865
4866static inline bfd_reloc_code_real_type
4867ldst_lo12_determine_real_reloc_type (void)
4868{
4c562523 4869 unsigned logsz;
a06ea964
NC
4870 enum aarch64_opnd_qualifier opd0_qlf = inst.base.operands[0].qualifier;
4871 enum aarch64_opnd_qualifier opd1_qlf = inst.base.operands[1].qualifier;
4872
4c562523
JW
4873 const bfd_reloc_code_real_type reloc_ldst_lo12[3][5] = {
4874 {
4875 BFD_RELOC_AARCH64_LDST8_LO12,
4876 BFD_RELOC_AARCH64_LDST16_LO12,
4877 BFD_RELOC_AARCH64_LDST32_LO12,
4878 BFD_RELOC_AARCH64_LDST64_LO12,
a06ea964 4879 BFD_RELOC_AARCH64_LDST128_LO12
4c562523
JW
4880 },
4881 {
4882 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12,
4883 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12,
4884 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12,
4885 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12,
4886 BFD_RELOC_AARCH64_NONE
4887 },
4888 {
4889 BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC,
4890 BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC,
4891 BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC,
4892 BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC,
4893 BFD_RELOC_AARCH64_NONE
4894 }
a06ea964
NC
4895 };
4896
4c562523
JW
4897 gas_assert (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
4898 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
4899 || (inst.reloc.type
4900 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC));
a06ea964
NC
4901 gas_assert (inst.base.opcode->operands[1] == AARCH64_OPND_ADDR_UIMM12);
4902
4903 if (opd1_qlf == AARCH64_OPND_QLF_NIL)
4904 opd1_qlf =
4905 aarch64_get_expected_qualifier (inst.base.opcode->qualifiers_list,
4906 1, opd0_qlf, 0);
4907 gas_assert (opd1_qlf != AARCH64_OPND_QLF_NIL);
4908
4909 logsz = get_logsz (aarch64_get_qualifier_esize (opd1_qlf));
4c562523
JW
4910 if (inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12
4911 || inst.reloc.type == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC)
4912 gas_assert (logsz <= 3);
4913 else
4914 gas_assert (logsz <= 4);
a06ea964 4915
4c562523
JW
4916 /* In reloc.c, these pseudo relocation types should be defined in similar
4917 order as above reloc_ldst_lo12 array. Because the array index calcuation
4918 below relies on this. */
4919 return reloc_ldst_lo12[inst.reloc.type - BFD_RELOC_AARCH64_LDST_LO12][logsz];
a06ea964
NC
4920}
4921
4922/* Check whether a register list REGINFO is valid. The registers must be
4923 numbered in increasing order (modulo 32), in increments of one or two.
4924
4925 If ACCEPT_ALTERNATE is non-zero, the register numbers should be in
4926 increments of two.
4927
4928 Return FALSE if such a register list is invalid, otherwise return TRUE. */
4929
4930static bfd_boolean
4931reg_list_valid_p (uint32_t reginfo, int accept_alternate)
4932{
4933 uint32_t i, nb_regs, prev_regno, incr;
4934
4935 nb_regs = 1 + (reginfo & 0x3);
4936 reginfo >>= 2;
4937 prev_regno = reginfo & 0x1f;
4938 incr = accept_alternate ? 2 : 1;
4939
4940 for (i = 1; i < nb_regs; ++i)
4941 {
4942 uint32_t curr_regno;
4943 reginfo >>= 5;
4944 curr_regno = reginfo & 0x1f;
4945 if (curr_regno != ((prev_regno + incr) & 0x1f))
4946 return FALSE;
4947 prev_regno = curr_regno;
4948 }
4949
4950 return TRUE;
4951}
4952
4953/* Generic instruction operand parser. This does no encoding and no
4954 semantic validation; it merely squirrels values away in the inst
4955 structure. Returns TRUE or FALSE depending on whether the
4956 specified grammar matched. */
4957
4958static bfd_boolean
4959parse_operands (char *str, const aarch64_opcode *opcode)
4960{
4961 int i;
4962 char *backtrack_pos = 0;
4963 const enum aarch64_opnd *operands = opcode->operands;
1799c0d0 4964 aarch64_reg_type imm_reg_type;
a06ea964
NC
4965
4966 clear_error ();
4967 skip_whitespace (str);
4968
1799c0d0
RS
4969 imm_reg_type = REG_TYPE_R_Z_BHSDQ_V;
4970
a06ea964
NC
4971 for (i = 0; operands[i] != AARCH64_OPND_NIL; i++)
4972 {
4973 int64_t val;
e1b988bb 4974 const reg_entry *reg;
a06ea964
NC
4975 int comma_skipped_p = 0;
4976 aarch64_reg_type rtype;
8f9a77af 4977 struct vector_type_el vectype;
e1b988bb 4978 aarch64_opnd_qualifier_t qualifier;
a06ea964
NC
4979 aarch64_opnd_info *info = &inst.base.operands[i];
4980
4981 DEBUG_TRACE ("parse operand %d", i);
4982
4983 /* Assign the operand code. */
4984 info->type = operands[i];
4985
4986 if (optional_operand_p (opcode, i))
4987 {
4988 /* Remember where we are in case we need to backtrack. */
4989 gas_assert (!backtrack_pos);
4990 backtrack_pos = str;
4991 }
4992
4993 /* Expect comma between operands; the backtrack mechanizm will take
4994 care of cases of omitted optional operand. */
4995 if (i > 0 && ! skip_past_char (&str, ','))
4996 {
4997 set_syntax_error (_("comma expected between operands"));
4998 goto failure;
4999 }
5000 else
5001 comma_skipped_p = 1;
5002
5003 switch (operands[i])
5004 {
5005 case AARCH64_OPND_Rd:
5006 case AARCH64_OPND_Rn:
5007 case AARCH64_OPND_Rm:
5008 case AARCH64_OPND_Rt:
5009 case AARCH64_OPND_Rt2:
5010 case AARCH64_OPND_Rs:
5011 case AARCH64_OPND_Ra:
5012 case AARCH64_OPND_Rt_SYS:
ee804238 5013 case AARCH64_OPND_PAIRREG:
e1b988bb 5014 po_int_reg_or_fail (REG_TYPE_R_Z);
a06ea964
NC
5015 break;
5016
5017 case AARCH64_OPND_Rd_SP:
5018 case AARCH64_OPND_Rn_SP:
e1b988bb 5019 po_int_reg_or_fail (REG_TYPE_R_SP);
a06ea964
NC
5020 break;
5021
5022 case AARCH64_OPND_Rm_EXT:
5023 case AARCH64_OPND_Rm_SFT:
5024 po_misc_or_fail (parse_shifter_operand
5025 (&str, info, (operands[i] == AARCH64_OPND_Rm_EXT
5026 ? SHIFTED_ARITH_IMM
5027 : SHIFTED_LOGIC_IMM)));
5028 if (!info->shifter.operator_present)
5029 {
5030 /* Default to LSL if not present. Libopcodes prefers shifter
5031 kind to be explicit. */
5032 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5033 info->shifter.kind = AARCH64_MOD_LSL;
5034 /* For Rm_EXT, libopcodes will carry out further check on whether
5035 or not stack pointer is used in the instruction (Recall that
5036 "the extend operator is not optional unless at least one of
5037 "Rd" or "Rn" is '11111' (i.e. WSP)"). */
5038 }
5039 break;
5040
5041 case AARCH64_OPND_Fd:
5042 case AARCH64_OPND_Fn:
5043 case AARCH64_OPND_Fm:
5044 case AARCH64_OPND_Fa:
5045 case AARCH64_OPND_Ft:
5046 case AARCH64_OPND_Ft2:
5047 case AARCH64_OPND_Sd:
5048 case AARCH64_OPND_Sn:
5049 case AARCH64_OPND_Sm:
5050 val = aarch64_reg_parse (&str, REG_TYPE_BHSDQ, &rtype, NULL);
5051 if (val == PARSE_FAIL)
5052 {
5053 first_error (_(get_reg_expected_msg (REG_TYPE_BHSDQ)));
5054 goto failure;
5055 }
5056 gas_assert (rtype >= REG_TYPE_FP_B && rtype <= REG_TYPE_FP_Q);
5057
5058 info->reg.regno = val;
5059 info->qualifier = AARCH64_OPND_QLF_S_B + (rtype - REG_TYPE_FP_B);
5060 break;
5061
5062 case AARCH64_OPND_Vd:
5063 case AARCH64_OPND_Vn:
5064 case AARCH64_OPND_Vm:
5065 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5066 if (val == PARSE_FAIL)
5067 {
5068 first_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5069 goto failure;
5070 }
5071 if (vectype.defined & NTA_HASINDEX)
5072 goto failure;
5073
5074 info->reg.regno = val;
5075 info->qualifier = vectype_to_qualifier (&vectype);
5076 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5077 goto failure;
5078 break;
5079
5080 case AARCH64_OPND_VdD1:
5081 case AARCH64_OPND_VnD1:
5082 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5083 if (val == PARSE_FAIL)
5084 {
5085 set_first_syntax_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5086 goto failure;
5087 }
5088 if (vectype.type != NT_d || vectype.index != 1)
5089 {
5090 set_fatal_syntax_error
5091 (_("the top half of a 128-bit FP/SIMD register is expected"));
5092 goto failure;
5093 }
5094 info->reg.regno = val;
5095 /* N.B: VdD1 and VnD1 are treated as an fp or advsimd scalar register
5096 here; it is correct for the purpose of encoding/decoding since
5097 only the register number is explicitly encoded in the related
5098 instructions, although this appears a bit hacky. */
5099 info->qualifier = AARCH64_OPND_QLF_S_D;
5100 break;
5101
5102 case AARCH64_OPND_Ed:
5103 case AARCH64_OPND_En:
5104 case AARCH64_OPND_Em:
5105 val = aarch64_reg_parse (&str, REG_TYPE_VN, NULL, &vectype);
5106 if (val == PARSE_FAIL)
5107 {
5108 first_error (_(get_reg_expected_msg (REG_TYPE_VN)));
5109 goto failure;
5110 }
5111 if (vectype.type == NT_invtype || !(vectype.defined & NTA_HASINDEX))
5112 goto failure;
5113
5114 info->reglane.regno = val;
5115 info->reglane.index = vectype.index;
5116 info->qualifier = vectype_to_qualifier (&vectype);
5117 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5118 goto failure;
5119 break;
5120
5121 case AARCH64_OPND_LVn:
5122 case AARCH64_OPND_LVt:
5123 case AARCH64_OPND_LVt_AL:
5124 case AARCH64_OPND_LEt:
10d76650
RS
5125 if ((val = parse_vector_reg_list (&str, REG_TYPE_VN,
5126 &vectype)) == PARSE_FAIL)
a06ea964
NC
5127 goto failure;
5128 if (! reg_list_valid_p (val, /* accept_alternate */ 0))
5129 {
5130 set_fatal_syntax_error (_("invalid register list"));
5131 goto failure;
5132 }
5133 info->reglist.first_regno = (val >> 2) & 0x1f;
5134 info->reglist.num_regs = (val & 0x3) + 1;
5135 if (operands[i] == AARCH64_OPND_LEt)
5136 {
5137 if (!(vectype.defined & NTA_HASINDEX))
5138 goto failure;
5139 info->reglist.has_index = 1;
5140 info->reglist.index = vectype.index;
5141 }
5142 else if (!(vectype.defined & NTA_HASTYPE))
5143 goto failure;
5144 info->qualifier = vectype_to_qualifier (&vectype);
5145 if (info->qualifier == AARCH64_OPND_QLF_NIL)
5146 goto failure;
5147 break;
5148
5149 case AARCH64_OPND_Cn:
5150 case AARCH64_OPND_Cm:
5151 po_reg_or_fail (REG_TYPE_CN);
5152 if (val > 15)
5153 {
5154 set_fatal_syntax_error (_(get_reg_expected_msg (REG_TYPE_CN)));
5155 goto failure;
5156 }
5157 inst.base.operands[i].reg.regno = val;
5158 break;
5159
5160 case AARCH64_OPND_SHLL_IMM:
5161 case AARCH64_OPND_IMM_VLSR:
5162 po_imm_or_fail (1, 64);
5163 info->imm.value = val;
5164 break;
5165
5166 case AARCH64_OPND_CCMP_IMM:
5167 case AARCH64_OPND_FBITS:
5168 case AARCH64_OPND_UIMM4:
5169 case AARCH64_OPND_UIMM3_OP1:
5170 case AARCH64_OPND_UIMM3_OP2:
5171 case AARCH64_OPND_IMM_VLSL:
5172 case AARCH64_OPND_IMM:
5173 case AARCH64_OPND_WIDTH:
5174 po_imm_nc_or_fail ();
5175 info->imm.value = val;
5176 break;
5177
5178 case AARCH64_OPND_UIMM7:
5179 po_imm_or_fail (0, 127);
5180 info->imm.value = val;
5181 break;
5182
5183 case AARCH64_OPND_IDX:
5184 case AARCH64_OPND_BIT_NUM:
5185 case AARCH64_OPND_IMMR:
5186 case AARCH64_OPND_IMMS:
5187 po_imm_or_fail (0, 63);
5188 info->imm.value = val;
5189 break;
5190
5191 case AARCH64_OPND_IMM0:
5192 po_imm_nc_or_fail ();
5193 if (val != 0)
5194 {
5195 set_fatal_syntax_error (_("immediate zero expected"));
5196 goto failure;
5197 }
5198 info->imm.value = 0;
5199 break;
5200
5201 case AARCH64_OPND_FPIMM0:
5202 {
5203 int qfloat;
5204 bfd_boolean res1 = FALSE, res2 = FALSE;
5205 /* N.B. -0.0 will be rejected; although -0.0 shouldn't be rejected,
5206 it is probably not worth the effort to support it. */
1799c0d0
RS
5207 if (!(res1 = parse_aarch64_imm_float (&str, &qfloat, FALSE,
5208 imm_reg_type))
6a9deabe
RS
5209 && (error_p ()
5210 || !(res2 = parse_constant_immediate (&str, &val,
5211 imm_reg_type))))
a06ea964
NC
5212 goto failure;
5213 if ((res1 && qfloat == 0) || (res2 && val == 0))
5214 {
5215 info->imm.value = 0;
5216 info->imm.is_fp = 1;
5217 break;
5218 }
5219 set_fatal_syntax_error (_("immediate zero expected"));
5220 goto failure;
5221 }
5222
5223 case AARCH64_OPND_IMM_MOV:
5224 {
5225 char *saved = str;
8db49cc2
WN
5226 if (reg_name_p (str, REG_TYPE_R_Z_SP) ||
5227 reg_name_p (str, REG_TYPE_VN))
a06ea964
NC
5228 goto failure;
5229 str = saved;
5230 po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
5231 GE_OPT_PREFIX, 1));
5232 /* The MOV immediate alias will be fixed up by fix_mov_imm_insn
5233 later. fix_mov_imm_insn will try to determine a machine
5234 instruction (MOVZ, MOVN or ORR) for it and will issue an error
5235 message if the immediate cannot be moved by a single
5236 instruction. */
5237 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
5238 inst.base.operands[i].skip = 1;
5239 }
5240 break;
5241
5242 case AARCH64_OPND_SIMD_IMM:
5243 case AARCH64_OPND_SIMD_IMM_SFT:
1799c0d0 5244 if (! parse_big_immediate (&str, &val, imm_reg_type))
a06ea964
NC
5245 goto failure;
5246 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5247 /* addr_off_p */ 0,
5248 /* need_libopcodes_p */ 1,
5249 /* skip_p */ 1);
5250 /* Parse shift.
5251 N.B. although AARCH64_OPND_SIMD_IMM doesn't permit any
5252 shift, we don't check it here; we leave the checking to
5253 the libopcodes (operand_general_constraint_met_p). By
5254 doing this, we achieve better diagnostics. */
5255 if (skip_past_comma (&str)
5256 && ! parse_shift (&str, info, SHIFTED_LSL_MSL))
5257 goto failure;
5258 if (!info->shifter.operator_present
5259 && info->type == AARCH64_OPND_SIMD_IMM_SFT)
5260 {
5261 /* Default to LSL if not present. Libopcodes prefers shifter
5262 kind to be explicit. */
5263 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5264 info->shifter.kind = AARCH64_MOD_LSL;
5265 }
5266 break;
5267
5268 case AARCH64_OPND_FPIMM:
5269 case AARCH64_OPND_SIMD_FPIMM:
5270 {
5271 int qfloat;
62b0d0d5
YZ
5272 bfd_boolean dp_p
5273 = (aarch64_get_qualifier_esize (inst.base.operands[0].qualifier)
5274 == 8);
6a9deabe 5275 if (!parse_aarch64_imm_float (&str, &qfloat, dp_p, imm_reg_type)
874d7e6e 5276 || !aarch64_imm_float_p (qfloat))
a06ea964 5277 {
6a9deabe
RS
5278 if (!error_p ())
5279 set_fatal_syntax_error (_("invalid floating-point"
5280 " constant"));
a06ea964
NC
5281 goto failure;
5282 }
5283 inst.base.operands[i].imm.value = encode_imm_float_bits (qfloat);
5284 inst.base.operands[i].imm.is_fp = 1;
5285 }
5286 break;
5287
5288 case AARCH64_OPND_LIMM:
5289 po_misc_or_fail (parse_shifter_operand (&str, info,
5290 SHIFTED_LOGIC_IMM));
5291 if (info->shifter.operator_present)
5292 {
5293 set_fatal_syntax_error
5294 (_("shift not allowed for bitmask immediate"));
5295 goto failure;
5296 }
5297 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5298 /* addr_off_p */ 0,
5299 /* need_libopcodes_p */ 1,
5300 /* skip_p */ 1);
5301 break;
5302
5303 case AARCH64_OPND_AIMM:
5304 if (opcode->op == OP_ADD)
5305 /* ADD may have relocation types. */
5306 po_misc_or_fail (parse_shifter_operand_reloc (&str, info,
5307 SHIFTED_ARITH_IMM));
5308 else
5309 po_misc_or_fail (parse_shifter_operand (&str, info,
5310 SHIFTED_ARITH_IMM));
5311 switch (inst.reloc.type)
5312 {
5313 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
5314 info->shifter.amount = 12;
5315 break;
5316 case BFD_RELOC_UNUSED:
5317 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
5318 if (info->shifter.kind != AARCH64_MOD_NONE)
5319 inst.reloc.flags = FIXUP_F_HAS_EXPLICIT_SHIFT;
5320 inst.reloc.pc_rel = 0;
5321 break;
5322 default:
5323 break;
5324 }
5325 info->imm.value = 0;
5326 if (!info->shifter.operator_present)
5327 {
5328 /* Default to LSL if not present. Libopcodes prefers shifter
5329 kind to be explicit. */
5330 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5331 info->shifter.kind = AARCH64_MOD_LSL;
5332 }
5333 break;
5334
5335 case AARCH64_OPND_HALF:
5336 {
5337 /* #<imm16> or relocation. */
5338 int internal_fixup_p;
5339 po_misc_or_fail (parse_half (&str, &internal_fixup_p));
5340 if (internal_fixup_p)
5341 aarch64_set_gas_internal_fixup (&inst.reloc, info, 0);
5342 skip_whitespace (str);
5343 if (skip_past_comma (&str))
5344 {
5345 /* {, LSL #<shift>} */
5346 if (! aarch64_gas_internal_fixup_p ())
5347 {
5348 set_fatal_syntax_error (_("can't mix relocation modifier "
5349 "with explicit shift"));
5350 goto failure;
5351 }
5352 po_misc_or_fail (parse_shift (&str, info, SHIFTED_LSL));
5353 }
5354 else
5355 inst.base.operands[i].shifter.amount = 0;
5356 inst.base.operands[i].shifter.kind = AARCH64_MOD_LSL;
5357 inst.base.operands[i].imm.value = 0;
5358 if (! process_movw_reloc_info ())
5359 goto failure;
5360 }
5361 break;
5362
5363 case AARCH64_OPND_EXCEPTION:
1799c0d0
RS
5364 po_misc_or_fail (parse_immediate_expression (&str, &inst.reloc.exp,
5365 imm_reg_type));
a06ea964
NC
5366 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5367 /* addr_off_p */ 0,
5368 /* need_libopcodes_p */ 0,
5369 /* skip_p */ 1);
5370 break;
5371
5372 case AARCH64_OPND_NZCV:
5373 {
5374 const asm_nzcv *nzcv = hash_find_n (aarch64_nzcv_hsh, str, 4);
5375 if (nzcv != NULL)
5376 {
5377 str += 4;
5378 info->imm.value = nzcv->value;
5379 break;
5380 }
5381 po_imm_or_fail (0, 15);
5382 info->imm.value = val;
5383 }
5384 break;
5385
5386 case AARCH64_OPND_COND:
68a64283 5387 case AARCH64_OPND_COND1:
a06ea964
NC
5388 info->cond = hash_find_n (aarch64_cond_hsh, str, 2);
5389 str += 2;
5390 if (info->cond == NULL)
5391 {
5392 set_syntax_error (_("invalid condition"));
5393 goto failure;
5394 }
68a64283
YZ
5395 else if (operands[i] == AARCH64_OPND_COND1
5396 && (info->cond->value & 0xe) == 0xe)
5397 {
5398 /* Not allow AL or NV. */
5399 set_default_error ();
5400 goto failure;
5401 }
a06ea964
NC
5402 break;
5403
5404 case AARCH64_OPND_ADDR_ADRP:
5405 po_misc_or_fail (parse_adrp (&str));
5406 /* Clear the value as operand needs to be relocated. */
5407 info->imm.value = 0;
5408 break;
5409
5410 case AARCH64_OPND_ADDR_PCREL14:
5411 case AARCH64_OPND_ADDR_PCREL19:
5412 case AARCH64_OPND_ADDR_PCREL21:
5413 case AARCH64_OPND_ADDR_PCREL26:
73866052 5414 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5415 if (!info->addr.pcrel)
5416 {
5417 set_syntax_error (_("invalid pc-relative address"));
5418 goto failure;
5419 }
5420 if (inst.gen_lit_pool
5421 && (opcode->iclass != loadlit || opcode->op == OP_PRFM_LIT))
5422 {
5423 /* Only permit "=value" in the literal load instructions.
5424 The literal will be generated by programmer_friendly_fixup. */
5425 set_syntax_error (_("invalid use of \"=immediate\""));
5426 goto failure;
5427 }
5428 if (inst.reloc.exp.X_op == O_symbol && find_reloc_table_entry (&str))
5429 {
5430 set_syntax_error (_("unrecognized relocation suffix"));
5431 goto failure;
5432 }
5433 if (inst.reloc.exp.X_op == O_constant && !inst.gen_lit_pool)
5434 {
5435 info->imm.value = inst.reloc.exp.X_add_number;
5436 inst.reloc.type = BFD_RELOC_UNUSED;
5437 }
5438 else
5439 {
5440 info->imm.value = 0;
f41aef5f
RE
5441 if (inst.reloc.type == BFD_RELOC_UNUSED)
5442 switch (opcode->iclass)
5443 {
5444 case compbranch:
5445 case condbranch:
5446 /* e.g. CBZ or B.COND */
5447 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
5448 inst.reloc.type = BFD_RELOC_AARCH64_BRANCH19;
5449 break;
5450 case testbranch:
5451 /* e.g. TBZ */
5452 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL14);
5453 inst.reloc.type = BFD_RELOC_AARCH64_TSTBR14;
5454 break;
5455 case branch_imm:
5456 /* e.g. B or BL */
5457 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL26);
5458 inst.reloc.type =
5459 (opcode->op == OP_BL) ? BFD_RELOC_AARCH64_CALL26
5460 : BFD_RELOC_AARCH64_JUMP26;
5461 break;
5462 case loadlit:
5463 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL19);
5464 inst.reloc.type = BFD_RELOC_AARCH64_LD_LO19_PCREL;
5465 break;
5466 case pcreladdr:
5467 gas_assert (operands[i] == AARCH64_OPND_ADDR_PCREL21);
5468 inst.reloc.type = BFD_RELOC_AARCH64_ADR_LO21_PCREL;
5469 break;
5470 default:
5471 gas_assert (0);
5472 abort ();
5473 }
a06ea964
NC
5474 inst.reloc.pc_rel = 1;
5475 }
5476 break;
5477
5478 case AARCH64_OPND_ADDR_SIMPLE:
5479 case AARCH64_OPND_SIMD_ADDR_SIMPLE:
e1b988bb
RS
5480 {
5481 /* [<Xn|SP>{, #<simm>}] */
5482 char *start = str;
5483 /* First use the normal address-parsing routines, to get
5484 the usual syntax errors. */
73866052 5485 po_misc_or_fail (parse_address (&str, info));
e1b988bb
RS
5486 if (info->addr.pcrel || info->addr.offset.is_reg
5487 || !info->addr.preind || info->addr.postind
5488 || info->addr.writeback)
5489 {
5490 set_syntax_error (_("invalid addressing mode"));
5491 goto failure;
5492 }
5493
5494 /* Then retry, matching the specific syntax of these addresses. */
5495 str = start;
5496 po_char_or_fail ('[');
5497 po_reg_or_fail (REG_TYPE_R64_SP);
5498 /* Accept optional ", #0". */
5499 if (operands[i] == AARCH64_OPND_ADDR_SIMPLE
5500 && skip_past_char (&str, ','))
5501 {
5502 skip_past_char (&str, '#');
5503 if (! skip_past_char (&str, '0'))
5504 {
5505 set_fatal_syntax_error
5506 (_("the optional immediate offset can only be 0"));
5507 goto failure;
5508 }
5509 }
5510 po_char_or_fail (']');
5511 break;
5512 }
a06ea964
NC
5513
5514 case AARCH64_OPND_ADDR_REGOFF:
5515 /* [<Xn|SP>, <R><m>{, <extend> {<amount>}}] */
73866052 5516 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5517 if (info->addr.pcrel || !info->addr.offset.is_reg
5518 || !info->addr.preind || info->addr.postind
5519 || info->addr.writeback)
5520 {
5521 set_syntax_error (_("invalid addressing mode"));
5522 goto failure;
5523 }
5524 if (!info->shifter.operator_present)
5525 {
5526 /* Default to LSL if not present. Libopcodes prefers shifter
5527 kind to be explicit. */
5528 gas_assert (info->shifter.kind == AARCH64_MOD_NONE);
5529 info->shifter.kind = AARCH64_MOD_LSL;
5530 }
5531 /* Qualifier to be deduced by libopcodes. */
5532 break;
5533
5534 case AARCH64_OPND_ADDR_SIMM7:
73866052 5535 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5536 if (info->addr.pcrel || info->addr.offset.is_reg
5537 || (!info->addr.preind && !info->addr.postind))
5538 {
5539 set_syntax_error (_("invalid addressing mode"));
5540 goto failure;
5541 }
73866052
RS
5542 if (inst.reloc.type != BFD_RELOC_UNUSED)
5543 {
5544 set_syntax_error (_("relocation not allowed"));
5545 goto failure;
5546 }
a06ea964
NC
5547 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5548 /* addr_off_p */ 1,
5549 /* need_libopcodes_p */ 1,
5550 /* skip_p */ 0);
5551 break;
5552
5553 case AARCH64_OPND_ADDR_SIMM9:
5554 case AARCH64_OPND_ADDR_SIMM9_2:
73866052 5555 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5556 if (info->addr.pcrel || info->addr.offset.is_reg
5557 || (!info->addr.preind && !info->addr.postind)
5558 || (operands[i] == AARCH64_OPND_ADDR_SIMM9_2
5559 && info->addr.writeback))
5560 {
5561 set_syntax_error (_("invalid addressing mode"));
5562 goto failure;
5563 }
5564 if (inst.reloc.type != BFD_RELOC_UNUSED)
5565 {
5566 set_syntax_error (_("relocation not allowed"));
5567 goto failure;
5568 }
5569 assign_imm_if_const_or_fixup_later (&inst.reloc, info,
5570 /* addr_off_p */ 1,
5571 /* need_libopcodes_p */ 1,
5572 /* skip_p */ 0);
5573 break;
5574
5575 case AARCH64_OPND_ADDR_UIMM12:
73866052 5576 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5577 if (info->addr.pcrel || info->addr.offset.is_reg
5578 || !info->addr.preind || info->addr.writeback)
5579 {
5580 set_syntax_error (_("invalid addressing mode"));
5581 goto failure;
5582 }
5583 if (inst.reloc.type == BFD_RELOC_UNUSED)
5584 aarch64_set_gas_internal_fixup (&inst.reloc, info, 1);
4c562523
JW
5585 else if (inst.reloc.type == BFD_RELOC_AARCH64_LDST_LO12
5586 || (inst.reloc.type
5587 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12)
5588 || (inst.reloc.type
5589 == BFD_RELOC_AARCH64_TLSLD_LDST_DTPREL_LO12_NC))
a06ea964
NC
5590 inst.reloc.type = ldst_lo12_determine_real_reloc_type ();
5591 /* Leave qualifier to be determined by libopcodes. */
5592 break;
5593
5594 case AARCH64_OPND_SIMD_ADDR_POST:
5595 /* [<Xn|SP>], <Xm|#<amount>> */
73866052 5596 po_misc_or_fail (parse_address (&str, info));
a06ea964
NC
5597 if (!info->addr.postind || !info->addr.writeback)
5598 {
5599 set_syntax_error (_("invalid addressing mode"));
5600 goto failure;
5601 }
5602 if (!info->addr.offset.is_reg)
5603 {
5604 if (inst.reloc.exp.X_op == O_constant)
5605 info->addr.offset.imm = inst.reloc.exp.X_add_number;
5606 else
5607 {
5608 set_fatal_syntax_error
5609 (_("writeback value should be an immediate constant"));
5610 goto failure;
5611 }
5612 }
5613 /* No qualifier. */
5614 break;
5615
5616 case AARCH64_OPND_SYSREG:
72ca8fad 5617 if ((val = parse_sys_reg (&str, aarch64_sys_regs_hsh, 1, 0))
a203d9b7 5618 == PARSE_FAIL)
a06ea964 5619 {
a203d9b7
YZ
5620 set_syntax_error (_("unknown or missing system register name"));
5621 goto failure;
a06ea964 5622 }
a203d9b7 5623 inst.base.operands[i].sysreg = val;
a06ea964
NC
5624 break;
5625
5626 case AARCH64_OPND_PSTATEFIELD:
72ca8fad 5627 if ((val = parse_sys_reg (&str, aarch64_pstatefield_hsh, 0, 1))
a3251895 5628 == PARSE_FAIL)
a06ea964
NC
5629 {
5630 set_syntax_error (_("unknown or missing PSTATE field name"));
5631 goto failure;
5632 }
5633 inst.base.operands[i].pstatefield = val;
5634 break;
5635
5636 case AARCH64_OPND_SYSREG_IC:
5637 inst.base.operands[i].sysins_op =
5638 parse_sys_ins_reg (&str, aarch64_sys_regs_ic_hsh);
5639 goto sys_reg_ins;
5640 case AARCH64_OPND_SYSREG_DC:
5641 inst.base.operands[i].sysins_op =
5642 parse_sys_ins_reg (&str, aarch64_sys_regs_dc_hsh);
5643 goto sys_reg_ins;
5644 case AARCH64_OPND_SYSREG_AT:
5645 inst.base.operands[i].sysins_op =
5646 parse_sys_ins_reg (&str, aarch64_sys_regs_at_hsh);
5647 goto sys_reg_ins;
5648 case AARCH64_OPND_SYSREG_TLBI:
5649 inst.base.operands[i].sysins_op =
5650 parse_sys_ins_reg (&str, aarch64_sys_regs_tlbi_hsh);
5651sys_reg_ins:
5652 if (inst.base.operands[i].sysins_op == NULL)
5653 {
5654 set_fatal_syntax_error ( _("unknown or missing operation name"));
5655 goto failure;
5656 }
5657 break;
5658
5659 case AARCH64_OPND_BARRIER:
5660 case AARCH64_OPND_BARRIER_ISB:
5661 val = parse_barrier (&str);
5662 if (val != PARSE_FAIL
5663 && operands[i] == AARCH64_OPND_BARRIER_ISB && val != 0xf)
5664 {
5665 /* ISB only accepts options name 'sy'. */
5666 set_syntax_error
5667 (_("the specified option is not accepted in ISB"));
5668 /* Turn off backtrack as this optional operand is present. */
5669 backtrack_pos = 0;
5670 goto failure;
5671 }
5672 /* This is an extension to accept a 0..15 immediate. */
5673 if (val == PARSE_FAIL)
5674 po_imm_or_fail (0, 15);
5675 info->barrier = aarch64_barrier_options + val;
5676 break;
5677
5678 case AARCH64_OPND_PRFOP:
5679 val = parse_pldop (&str);
5680 /* This is an extension to accept a 0..31 immediate. */
5681 if (val == PARSE_FAIL)
5682 po_imm_or_fail (0, 31);
5683 inst.base.operands[i].prfop = aarch64_prfops + val;
5684 break;
5685
1e6f4800
MW
5686 case AARCH64_OPND_BARRIER_PSB:
5687 val = parse_barrier_psb (&str, &(info->hint_option));
5688 if (val == PARSE_FAIL)
5689 goto failure;
5690 break;
5691
a06ea964
NC
5692 default:
5693 as_fatal (_("unhandled operand code %d"), operands[i]);
5694 }
5695
5696 /* If we get here, this operand was successfully parsed. */
5697 inst.base.operands[i].present = 1;
5698 continue;
5699
5700failure:
5701 /* The parse routine should already have set the error, but in case
5702 not, set a default one here. */
5703 if (! error_p ())
5704 set_default_error ();
5705
5706 if (! backtrack_pos)
5707 goto parse_operands_return;
5708
f4c51f60
JW
5709 {
5710 /* We reach here because this operand is marked as optional, and
5711 either no operand was supplied or the operand was supplied but it
5712 was syntactically incorrect. In the latter case we report an
5713 error. In the former case we perform a few more checks before
5714 dropping through to the code to insert the default operand. */
5715
5716 char *tmp = backtrack_pos;
5717 char endchar = END_OF_INSN;
5718
5719 if (i != (aarch64_num_of_operands (opcode) - 1))
5720 endchar = ',';
5721 skip_past_char (&tmp, ',');
5722
5723 if (*tmp != endchar)
5724 /* The user has supplied an operand in the wrong format. */
5725 goto parse_operands_return;
5726
5727 /* Make sure there is not a comma before the optional operand.
5728 For example the fifth operand of 'sys' is optional:
5729
5730 sys #0,c0,c0,#0, <--- wrong
5731 sys #0,c0,c0,#0 <--- correct. */
5732 if (comma_skipped_p && i && endchar == END_OF_INSN)
5733 {
5734 set_fatal_syntax_error
5735 (_("unexpected comma before the omitted optional operand"));
5736 goto parse_operands_return;
5737 }
5738 }
5739
a06ea964
NC
5740 /* Reaching here means we are dealing with an optional operand that is
5741 omitted from the assembly line. */
5742 gas_assert (optional_operand_p (opcode, i));
5743 info->present = 0;
5744 process_omitted_operand (operands[i], opcode, i, info);
5745
5746 /* Try again, skipping the optional operand at backtrack_pos. */
5747 str = backtrack_pos;
5748 backtrack_pos = 0;
5749
a06ea964
NC
5750 /* Clear any error record after the omitted optional operand has been
5751 successfully handled. */
5752 clear_error ();
5753 }
5754
5755 /* Check if we have parsed all the operands. */
5756 if (*str != '\0' && ! error_p ())
5757 {
5758 /* Set I to the index of the last present operand; this is
5759 for the purpose of diagnostics. */
5760 for (i -= 1; i >= 0 && !inst.base.operands[i].present; --i)
5761 ;
5762 set_fatal_syntax_error
5763 (_("unexpected characters following instruction"));
5764 }
5765
5766parse_operands_return:
5767
5768 if (error_p ())
5769 {
5770 DEBUG_TRACE ("parsing FAIL: %s - %s",
5771 operand_mismatch_kind_names[get_error_kind ()],
5772 get_error_message ());
5773 /* Record the operand error properly; this is useful when there
5774 are multiple instruction templates for a mnemonic name, so that
5775 later on, we can select the error that most closely describes
5776 the problem. */
5777 record_operand_error (opcode, i, get_error_kind (),
5778 get_error_message ());
5779 return FALSE;
5780 }
5781 else
5782 {
5783 DEBUG_TRACE ("parsing SUCCESS");
5784 return TRUE;
5785 }
5786}
5787
5788/* It does some fix-up to provide some programmer friendly feature while
5789 keeping the libopcodes happy, i.e. libopcodes only accepts
5790 the preferred architectural syntax.
5791 Return FALSE if there is any failure; otherwise return TRUE. */
5792
5793static bfd_boolean
5794programmer_friendly_fixup (aarch64_instruction *instr)
5795{
5796 aarch64_inst *base = &instr->base;
5797 const aarch64_opcode *opcode = base->opcode;
5798 enum aarch64_op op = opcode->op;
5799 aarch64_opnd_info *operands = base->operands;
5800
5801 DEBUG_TRACE ("enter");
5802
5803 switch (opcode->iclass)
5804 {
5805 case testbranch:
5806 /* TBNZ Xn|Wn, #uimm6, label
5807 Test and Branch Not Zero: conditionally jumps to label if bit number
5808 uimm6 in register Xn is not zero. The bit number implies the width of
5809 the register, which may be written and should be disassembled as Wn if
5810 uimm is less than 32. */
5811 if (operands[0].qualifier == AARCH64_OPND_QLF_W)
5812 {
5813 if (operands[1].imm.value >= 32)
5814 {
5815 record_operand_out_of_range_error (opcode, 1, _("immediate value"),
5816 0, 31);
5817 return FALSE;
5818 }
5819 operands[0].qualifier = AARCH64_OPND_QLF_X;
5820 }
5821 break;
5822 case loadlit:
5823 /* LDR Wt, label | =value
5824 As a convenience assemblers will typically permit the notation
5825 "=value" in conjunction with the pc-relative literal load instructions
5826 to automatically place an immediate value or symbolic address in a
5827 nearby literal pool and generate a hidden label which references it.
5828 ISREG has been set to 0 in the case of =value. */
5829 if (instr->gen_lit_pool
5830 && (op == OP_LDR_LIT || op == OP_LDRV_LIT || op == OP_LDRSW_LIT))
5831 {
5832 int size = aarch64_get_qualifier_esize (operands[0].qualifier);
5833 if (op == OP_LDRSW_LIT)
5834 size = 4;
5835 if (instr->reloc.exp.X_op != O_constant
67a32447 5836 && instr->reloc.exp.X_op != O_big
a06ea964
NC
5837 && instr->reloc.exp.X_op != O_symbol)
5838 {
5839 record_operand_error (opcode, 1,
5840 AARCH64_OPDE_FATAL_SYNTAX_ERROR,
5841 _("constant expression expected"));
5842 return FALSE;
5843 }
5844 if (! add_to_lit_pool (&instr->reloc.exp, size))
5845 {
5846 record_operand_error (opcode, 1,
5847 AARCH64_OPDE_OTHER_ERROR,
5848 _("literal pool insertion failed"));
5849 return FALSE;
5850 }
5851 }
5852 break;
a06ea964
NC
5853 case log_shift:
5854 case bitfield:
5855 /* UXT[BHW] Wd, Wn
5856 Unsigned Extend Byte|Halfword|Word: UXT[BH] is architectural alias
5857 for UBFM Wd,Wn,#0,#7|15, while UXTW is pseudo instruction which is
5858 encoded using ORR Wd, WZR, Wn (MOV Wd,Wn).
5859 A programmer-friendly assembler should accept a destination Xd in
5860 place of Wd, however that is not the preferred form for disassembly.
5861 */
5862 if ((op == OP_UXTB || op == OP_UXTH || op == OP_UXTW)
5863 && operands[1].qualifier == AARCH64_OPND_QLF_W
5864 && operands[0].qualifier == AARCH64_OPND_QLF_X)
5865 operands[0].qualifier = AARCH64_OPND_QLF_W;
5866 break;
5867
5868 case addsub_ext:
5869 {
5870 /* In the 64-bit form, the final register operand is written as Wm
5871 for all but the (possibly omitted) UXTX/LSL and SXTX
5872 operators.
5873 As a programmer-friendly assembler, we accept e.g.
5874 ADDS <Xd>, <Xn|SP>, <Xm>{, UXTB {#<amount>}} and change it to
5875 ADDS <Xd>, <Xn|SP>, <Wm>{, UXTB {#<amount>}}. */
5876 int idx = aarch64_operand_index (opcode->operands,
5877 AARCH64_OPND_Rm_EXT);
5878 gas_assert (idx == 1 || idx == 2);
5879 if (operands[0].qualifier == AARCH64_OPND_QLF_X
5880 && operands[idx].qualifier == AARCH64_OPND_QLF_X
5881 && operands[idx].shifter.kind != AARCH64_MOD_LSL
5882 && operands[idx].shifter.kind != AARCH64_MOD_UXTX
5883 && operands[idx].shifter.kind != AARCH64_MOD_SXTX)
5884 operands[idx].qualifier = AARCH64_OPND_QLF_W;
5885 }
5886 break;
5887
5888 default:
5889 break;
5890 }
5891
5892 DEBUG_TRACE ("exit with SUCCESS");
5893 return TRUE;
5894}
5895
5c47e525 5896/* Check for loads and stores that will cause unpredictable behavior. */
54a28c4c
JW
5897
5898static void
5899warn_unpredictable_ldst (aarch64_instruction *instr, char *str)
5900{
5901 aarch64_inst *base = &instr->base;
5902 const aarch64_opcode *opcode = base->opcode;
5903 const aarch64_opnd_info *opnds = base->operands;
5904 switch (opcode->iclass)
5905 {
5906 case ldst_pos:
5907 case ldst_imm9:
5908 case ldst_unscaled:
5909 case ldst_unpriv:
5c47e525
RE
5910 /* Loading/storing the base register is unpredictable if writeback. */
5911 if ((aarch64_get_operand_class (opnds[0].type)
5912 == AARCH64_OPND_CLASS_INT_REG)
5913 && opnds[0].reg.regno == opnds[1].addr.base_regno
4bf8c6e8 5914 && opnds[1].addr.base_regno != REG_SP
54a28c4c 5915 && opnds[1].addr.writeback)
5c47e525 5916 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
54a28c4c
JW
5917 break;
5918 case ldstpair_off:
5919 case ldstnapair_offs:
5920 case ldstpair_indexed:
5c47e525
RE
5921 /* Loading/storing the base register is unpredictable if writeback. */
5922 if ((aarch64_get_operand_class (opnds[0].type)
5923 == AARCH64_OPND_CLASS_INT_REG)
5924 && (opnds[0].reg.regno == opnds[2].addr.base_regno
5925 || opnds[1].reg.regno == opnds[2].addr.base_regno)
4bf8c6e8 5926 && opnds[2].addr.base_regno != REG_SP
54a28c4c 5927 && opnds[2].addr.writeback)
5c47e525
RE
5928 as_warn (_("unpredictable transfer with writeback -- `%s'"), str);
5929 /* Load operations must load different registers. */
54a28c4c
JW
5930 if ((opcode->opcode & (1 << 22))
5931 && opnds[0].reg.regno == opnds[1].reg.regno)
5932 as_warn (_("unpredictable load of register pair -- `%s'"), str);
5933 break;
5934 default:
5935 break;
5936 }
5937}
5938
a06ea964
NC
5939/* A wrapper function to interface with libopcodes on encoding and
5940 record the error message if there is any.
5941
5942 Return TRUE on success; otherwise return FALSE. */
5943
5944static bfd_boolean
5945do_encode (const aarch64_opcode *opcode, aarch64_inst *instr,
5946 aarch64_insn *code)
5947{
5948 aarch64_operand_error error_info;
5949 error_info.kind = AARCH64_OPDE_NIL;
5950 if (aarch64_opcode_encode (opcode, instr, code, NULL, &error_info))
5951 return TRUE;
5952 else
5953 {
5954 gas_assert (error_info.kind != AARCH64_OPDE_NIL);
5955 record_operand_error_info (opcode, &error_info);
5956 return FALSE;
5957 }
5958}
5959
5960#ifdef DEBUG_AARCH64
5961static inline void
5962dump_opcode_operands (const aarch64_opcode *opcode)
5963{
5964 int i = 0;
5965 while (opcode->operands[i] != AARCH64_OPND_NIL)
5966 {
5967 aarch64_verbose ("\t\t opnd%d: %s", i,
5968 aarch64_get_operand_name (opcode->operands[i])[0] != '\0'
5969 ? aarch64_get_operand_name (opcode->operands[i])
5970 : aarch64_get_operand_desc (opcode->operands[i]));
5971 ++i;
5972 }
5973}
5974#endif /* DEBUG_AARCH64 */
5975
5976/* This is the guts of the machine-dependent assembler. STR points to a
5977 machine dependent instruction. This function is supposed to emit
5978 the frags/bytes it assembles to. */
5979
5980void
5981md_assemble (char *str)
5982{
5983 char *p = str;
5984 templates *template;
5985 aarch64_opcode *opcode;
5986 aarch64_inst *inst_base;
5987 unsigned saved_cond;
5988
5989 /* Align the previous label if needed. */
5990 if (last_label_seen != NULL)
5991 {
5992 symbol_set_frag (last_label_seen, frag_now);
5993 S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
5994 S_SET_SEGMENT (last_label_seen, now_seg);
5995 }
5996
5997 inst.reloc.type = BFD_RELOC_UNUSED;
5998
5999 DEBUG_TRACE ("\n\n");
6000 DEBUG_TRACE ("==============================");
6001 DEBUG_TRACE ("Enter md_assemble with %s", str);
6002
6003 template = opcode_lookup (&p);
6004 if (!template)
6005 {
6006 /* It wasn't an instruction, but it might be a register alias of
6007 the form alias .req reg directive. */
6008 if (!create_register_alias (str, p))
6009 as_bad (_("unknown mnemonic `%s' -- `%s'"), get_mnemonic_name (str),
6010 str);
6011 return;
6012 }
6013
6014 skip_whitespace (p);
6015 if (*p == ',')
6016 {
6017 as_bad (_("unexpected comma after the mnemonic name `%s' -- `%s'"),
6018 get_mnemonic_name (str), str);
6019 return;
6020 }
6021
6022 init_operand_error_report ();
6023
eb9d6cc9
RL
6024 /* Sections are assumed to start aligned. In executable section, there is no
6025 MAP_DATA symbol pending. So we only align the address during
6026 MAP_DATA --> MAP_INSN transition.
6027 For other sections, this is not guaranteed. */
6028 enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
6029 if (!need_pass_2 && subseg_text_p (now_seg) && mapstate == MAP_DATA)
6030 frag_align_code (2, 0);
6031
a06ea964
NC
6032 saved_cond = inst.cond;
6033 reset_aarch64_instruction (&inst);
6034 inst.cond = saved_cond;
6035
6036 /* Iterate through all opcode entries with the same mnemonic name. */
6037 do
6038 {
6039 opcode = template->opcode;
6040
6041 DEBUG_TRACE ("opcode %s found", opcode->name);
6042#ifdef DEBUG_AARCH64
6043 if (debug_dump)
6044 dump_opcode_operands (opcode);
6045#endif /* DEBUG_AARCH64 */
6046
a06ea964
NC
6047 mapping_state (MAP_INSN);
6048
6049 inst_base = &inst.base;
6050 inst_base->opcode = opcode;
6051
6052 /* Truly conditionally executed instructions, e.g. b.cond. */
6053 if (opcode->flags & F_COND)
6054 {
6055 gas_assert (inst.cond != COND_ALWAYS);
6056 inst_base->cond = get_cond_from_value (inst.cond);
6057 DEBUG_TRACE ("condition found %s", inst_base->cond->names[0]);
6058 }
6059 else if (inst.cond != COND_ALWAYS)
6060 {
6061 /* It shouldn't arrive here, where the assembly looks like a
6062 conditional instruction but the found opcode is unconditional. */
6063 gas_assert (0);
6064 continue;
6065 }
6066
6067 if (parse_operands (p, opcode)
6068 && programmer_friendly_fixup (&inst)
6069 && do_encode (inst_base->opcode, &inst.base, &inst_base->value))
6070 {
3f06bfce
YZ
6071 /* Check that this instruction is supported for this CPU. */
6072 if (!opcode->avariant
93d8990c 6073 || !AARCH64_CPU_HAS_ALL_FEATURES (cpu_variant, *opcode->avariant))
3f06bfce
YZ
6074 {
6075 as_bad (_("selected processor does not support `%s'"), str);
6076 return;
6077 }
6078
54a28c4c
JW
6079 warn_unpredictable_ldst (&inst, str);
6080
a06ea964
NC
6081 if (inst.reloc.type == BFD_RELOC_UNUSED
6082 || !inst.reloc.need_libopcodes_p)
6083 output_inst (NULL);
6084 else
6085 {
6086 /* If there is relocation generated for the instruction,
6087 store the instruction information for the future fix-up. */
6088 struct aarch64_inst *copy;
6089 gas_assert (inst.reloc.type != BFD_RELOC_UNUSED);
325801bd 6090 copy = XNEW (struct aarch64_inst);
a06ea964
NC
6091 memcpy (copy, &inst.base, sizeof (struct aarch64_inst));
6092 output_inst (copy);
6093 }
6094 return;
6095 }
6096
6097 template = template->next;
6098 if (template != NULL)
6099 {
6100 reset_aarch64_instruction (&inst);
6101 inst.cond = saved_cond;
6102 }
6103 }
6104 while (template != NULL);
6105
6106 /* Issue the error messages if any. */
6107 output_operand_error_report (str);
6108}
6109
6110/* Various frobbings of labels and their addresses. */
6111
6112void
6113aarch64_start_line_hook (void)
6114{
6115 last_label_seen = NULL;
6116}
6117
6118void
6119aarch64_frob_label (symbolS * sym)
6120{
6121 last_label_seen = sym;
6122
6123 dwarf2_emit_label (sym);
6124}
6125
6126int
6127aarch64_data_in_code (void)
6128{
6129 if (!strncmp (input_line_pointer + 1, "data:", 5))
6130 {
6131 *input_line_pointer = '/';
6132 input_line_pointer += 5;
6133 *input_line_pointer = 0;
6134 return 1;
6135 }
6136
6137 return 0;
6138}
6139
6140char *
6141aarch64_canonicalize_symbol_name (char *name)
6142{
6143 int len;
6144
6145 if ((len = strlen (name)) > 5 && streq (name + len - 5, "/data"))
6146 *(name + len - 5) = 0;
6147
6148 return name;
6149}
6150\f
6151/* Table of all register names defined by default. The user can
6152 define additional names with .req. Note that all register names
6153 should appear in both upper and lowercase variants. Some registers
6154 also have mixed-case names. */
6155
6156#define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE }
6157#define REGNUM(p,n,t) REGDEF(p##n, n, t)
6158#define REGSET31(p,t) \
6159 REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
6160 REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
6161 REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
6162 REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t), \
6163 REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
6164 REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
6165 REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
6166 REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t)
6167#define REGSET(p,t) \
6168 REGSET31(p,t), REGNUM(p,31,t)
6169
6170/* These go into aarch64_reg_hsh hash-table. */
6171static const reg_entry reg_names[] = {
6172 /* Integer registers. */
6173 REGSET31 (x, R_64), REGSET31 (X, R_64),
6174 REGSET31 (w, R_32), REGSET31 (W, R_32),
6175
6176 REGDEF (wsp, 31, SP_32), REGDEF (WSP, 31, SP_32),
6177 REGDEF (sp, 31, SP_64), REGDEF (SP, 31, SP_64),
6178
6179 REGDEF (wzr, 31, Z_32), REGDEF (WZR, 31, Z_32),
6180 REGDEF (xzr, 31, Z_64), REGDEF (XZR, 31, Z_64),
6181
6182 /* Coprocessor register numbers. */
6183 REGSET (c, CN), REGSET (C, CN),
6184
6185 /* Floating-point single precision registers. */
6186 REGSET (s, FP_S), REGSET (S, FP_S),
6187
6188 /* Floating-point double precision registers. */
6189 REGSET (d, FP_D), REGSET (D, FP_D),
6190
6191 /* Floating-point half precision registers. */
6192 REGSET (h, FP_H), REGSET (H, FP_H),
6193
6194 /* Floating-point byte precision registers. */
6195 REGSET (b, FP_B), REGSET (B, FP_B),
6196
6197 /* Floating-point quad precision registers. */
6198 REGSET (q, FP_Q), REGSET (Q, FP_Q),
6199
6200 /* FP/SIMD registers. */
6201 REGSET (v, VN), REGSET (V, VN),
6202};
6203
6204#undef REGDEF
6205#undef REGNUM
6206#undef REGSET
6207
6208#define N 1
6209#define n 0
6210#define Z 1
6211#define z 0
6212#define C 1
6213#define c 0
6214#define V 1
6215#define v 0
6216#define B(a,b,c,d) (((a) << 3) | ((b) << 2) | ((c) << 1) | (d))
6217static const asm_nzcv nzcv_names[] = {
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 {"NZcV", B (N, Z, c, V)},
6232 {"NZCv", B (N, Z, C, v)},
6233 {"NZCV", B (N, Z, C, V)}
6234};
6235
6236#undef N
6237#undef n
6238#undef Z
6239#undef z
6240#undef C
6241#undef c
6242#undef V
6243#undef v
6244#undef B
6245\f
6246/* MD interface: bits in the object file. */
6247
6248/* Turn an integer of n bytes (in val) into a stream of bytes appropriate
6249 for use in the a.out file, and stores them in the array pointed to by buf.
6250 This knows about the endian-ness of the target machine and does
6251 THE RIGHT THING, whatever it is. Possible values for n are 1 (byte)
6252 2 (short) and 4 (long) Floating numbers are put out as a series of
6253 LITTLENUMS (shorts, here at least). */
6254
6255void
6256md_number_to_chars (char *buf, valueT val, int n)
6257{
6258 if (target_big_endian)
6259 number_to_chars_bigendian (buf, val, n);
6260 else
6261 number_to_chars_littleendian (buf, val, n);
6262}
6263
6264/* MD interface: Sections. */
6265
6266/* Estimate the size of a frag before relaxing. Assume everything fits in
6267 4 bytes. */
6268
6269int
6270md_estimate_size_before_relax (fragS * fragp, segT segtype ATTRIBUTE_UNUSED)
6271{
6272 fragp->fr_var = 4;
6273 return 4;
6274}
6275
6276/* Round up a section size to the appropriate boundary. */
6277
6278valueT
6279md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
6280{
6281 return size;
6282}
6283
6284/* This is called from HANDLE_ALIGN in write.c. Fill in the contents
f803aa8e
DPT
6285 of an rs_align_code fragment.
6286
6287 Here we fill the frag with the appropriate info for padding the
6288 output stream. The resulting frag will consist of a fixed (fr_fix)
6289 and of a repeating (fr_var) part.
6290
6291 The fixed content is always emitted before the repeating content and
6292 these two parts are used as follows in constructing the output:
6293 - the fixed part will be used to align to a valid instruction word
6294 boundary, in case that we start at a misaligned address; as no
6295 executable instruction can live at the misaligned location, we
6296 simply fill with zeros;
6297 - the variable part will be used to cover the remaining padding and
6298 we fill using the AArch64 NOP instruction.
6299
6300 Note that the size of a RS_ALIGN_CODE fragment is always 7 to provide
6301 enough storage space for up to 3 bytes for padding the back to a valid
6302 instruction alignment and exactly 4 bytes to store the NOP pattern. */
a06ea964
NC
6303
6304void
6305aarch64_handle_align (fragS * fragP)
6306{
6307 /* NOP = d503201f */
6308 /* AArch64 instructions are always little-endian. */
d9235011 6309 static unsigned char const aarch64_noop[4] = { 0x1f, 0x20, 0x03, 0xd5 };
a06ea964
NC
6310
6311 int bytes, fix, noop_size;
6312 char *p;
a06ea964
NC
6313
6314 if (fragP->fr_type != rs_align_code)
6315 return;
6316
6317 bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
6318 p = fragP->fr_literal + fragP->fr_fix;
a06ea964
NC
6319
6320#ifdef OBJ_ELF
6321 gas_assert (fragP->tc_frag_data.recorded);
6322#endif
6323
a06ea964 6324 noop_size = sizeof (aarch64_noop);
a06ea964 6325
f803aa8e
DPT
6326 fix = bytes & (noop_size - 1);
6327 if (fix)
a06ea964 6328 {
a06ea964
NC
6329#ifdef OBJ_ELF
6330 insert_data_mapping_symbol (MAP_INSN, fragP->fr_fix, fragP, fix);
6331#endif
6332 memset (p, 0, fix);
6333 p += fix;
f803aa8e 6334 fragP->fr_fix += fix;
a06ea964
NC
6335 }
6336
f803aa8e
DPT
6337 if (noop_size)
6338 memcpy (p, aarch64_noop, noop_size);
6339 fragP->fr_var = noop_size;
a06ea964
NC
6340}
6341
6342/* Perform target specific initialisation of a frag.
6343 Note - despite the name this initialisation is not done when the frag
6344 is created, but only when its type is assigned. A frag can be created
6345 and used a long time before its type is set, so beware of assuming that
6346 this initialisationis performed first. */
6347
6348#ifndef OBJ_ELF
6349void
6350aarch64_init_frag (fragS * fragP ATTRIBUTE_UNUSED,
6351 int max_chars ATTRIBUTE_UNUSED)
6352{
6353}
6354
6355#else /* OBJ_ELF is defined. */
6356void
6357aarch64_init_frag (fragS * fragP, int max_chars)
6358{
6359 /* Record a mapping symbol for alignment frags. We will delete this
6360 later if the alignment ends up empty. */
6361 if (!fragP->tc_frag_data.recorded)
c7ad08e6
RL
6362 fragP->tc_frag_data.recorded = 1;
6363
6364 switch (fragP->fr_type)
a06ea964 6365 {
c7ad08e6
RL
6366 case rs_align_test:
6367 case rs_fill:
6368 mapping_state_2 (MAP_DATA, max_chars);
6369 break;
7ea12e5c
NC
6370 case rs_align:
6371 /* PR 20364: We can get alignment frags in code sections,
6372 so do not just assume that we should use the MAP_DATA state. */
6373 mapping_state_2 (subseg_text_p (now_seg) ? MAP_INSN : MAP_DATA, max_chars);
6374 break;
c7ad08e6
RL
6375 case rs_align_code:
6376 mapping_state_2 (MAP_INSN, max_chars);
6377 break;
6378 default:
6379 break;
a06ea964
NC
6380 }
6381}
6382\f
6383/* Initialize the DWARF-2 unwind information for this procedure. */
6384
6385void
6386tc_aarch64_frame_initial_instructions (void)
6387{
6388 cfi_add_CFA_def_cfa (REG_SP, 0);
6389}
6390#endif /* OBJ_ELF */
6391
6392/* Convert REGNAME to a DWARF-2 register number. */
6393
6394int
6395tc_aarch64_regname_to_dw2regnum (char *regname)
6396{
6397 const reg_entry *reg = parse_reg (&regname);
6398 if (reg == NULL)
6399 return -1;
6400
6401 switch (reg->type)
6402 {
6403 case REG_TYPE_SP_32:
6404 case REG_TYPE_SP_64:
6405 case REG_TYPE_R_32:
6406 case REG_TYPE_R_64:
a2cac51c
RH
6407 return reg->number;
6408
a06ea964
NC
6409 case REG_TYPE_FP_B:
6410 case REG_TYPE_FP_H:
6411 case REG_TYPE_FP_S:
6412 case REG_TYPE_FP_D:
6413 case REG_TYPE_FP_Q:
a2cac51c
RH
6414 return reg->number + 64;
6415
a06ea964
NC
6416 default:
6417 break;
6418 }
6419 return -1;
6420}
6421
cec5225b
YZ
6422/* Implement DWARF2_ADDR_SIZE. */
6423
6424int
6425aarch64_dwarf2_addr_size (void)
6426{
6427#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
6428 if (ilp32_p)
6429 return 4;
6430#endif
6431 return bfd_arch_bits_per_address (stdoutput) / 8;
6432}
6433
a06ea964
NC
6434/* MD interface: Symbol and relocation handling. */
6435
6436/* Return the address within the segment that a PC-relative fixup is
6437 relative to. For AArch64 PC-relative fixups applied to instructions
6438 are generally relative to the location plus AARCH64_PCREL_OFFSET bytes. */
6439
6440long
6441md_pcrel_from_section (fixS * fixP, segT seg)
6442{
6443 offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
6444
6445 /* If this is pc-relative and we are going to emit a relocation
6446 then we just want to put out any pipeline compensation that the linker
6447 will need. Otherwise we want to use the calculated base. */
6448 if (fixP->fx_pcrel
6449 && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
6450 || aarch64_force_relocation (fixP)))
6451 base = 0;
6452
6453 /* AArch64 should be consistent for all pc-relative relocations. */
6454 return base + AARCH64_PCREL_OFFSET;
6455}
6456
6457/* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
6458 Otherwise we have no need to default values of symbols. */
6459
6460symbolS *
6461md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
6462{
6463#ifdef OBJ_ELF
6464 if (name[0] == '_' && name[1] == 'G'
6465 && streq (name, GLOBAL_OFFSET_TABLE_NAME))
6466 {
6467 if (!GOT_symbol)
6468 {
6469 if (symbol_find (name))
6470 as_bad (_("GOT already in the symbol table"));
6471
6472 GOT_symbol = symbol_new (name, undefined_section,
6473 (valueT) 0, &zero_address_frag);
6474 }
6475
6476 return GOT_symbol;
6477 }
6478#endif
6479
6480 return 0;
6481}
6482
6483/* Return non-zero if the indicated VALUE has overflowed the maximum
6484 range expressible by a unsigned number with the indicated number of
6485 BITS. */
6486
6487static bfd_boolean
6488unsigned_overflow (valueT value, unsigned bits)
6489{
6490 valueT lim;
6491 if (bits >= sizeof (valueT) * 8)
6492 return FALSE;
6493 lim = (valueT) 1 << bits;
6494 return (value >= lim);
6495}
6496
6497
6498/* Return non-zero if the indicated VALUE has overflowed the maximum
6499 range expressible by an signed number with the indicated number of
6500 BITS. */
6501
6502static bfd_boolean
6503signed_overflow (offsetT value, unsigned bits)
6504{
6505 offsetT lim;
6506 if (bits >= sizeof (offsetT) * 8)
6507 return FALSE;
6508 lim = (offsetT) 1 << (bits - 1);
6509 return (value < -lim || value >= lim);
6510}
6511
6512/* Given an instruction in *INST, which is expected to be a scaled, 12-bit,
6513 unsigned immediate offset load/store instruction, try to encode it as
6514 an unscaled, 9-bit, signed immediate offset load/store instruction.
6515 Return TRUE if it is successful; otherwise return FALSE.
6516
6517 As a programmer-friendly assembler, LDUR/STUR instructions can be generated
6518 in response to the standard LDR/STR mnemonics when the immediate offset is
6519 unambiguous, i.e. when it is negative or unaligned. */
6520
6521static bfd_boolean
6522try_to_encode_as_unscaled_ldst (aarch64_inst *instr)
6523{
6524 int idx;
6525 enum aarch64_op new_op;
6526 const aarch64_opcode *new_opcode;
6527
6528 gas_assert (instr->opcode->iclass == ldst_pos);
6529
6530 switch (instr->opcode->op)
6531 {
6532 case OP_LDRB_POS:new_op = OP_LDURB; break;
6533 case OP_STRB_POS: new_op = OP_STURB; break;
6534 case OP_LDRSB_POS: new_op = OP_LDURSB; break;
6535 case OP_LDRH_POS: new_op = OP_LDURH; break;
6536 case OP_STRH_POS: new_op = OP_STURH; break;
6537 case OP_LDRSH_POS: new_op = OP_LDURSH; break;
6538 case OP_LDR_POS: new_op = OP_LDUR; break;
6539 case OP_STR_POS: new_op = OP_STUR; break;
6540 case OP_LDRF_POS: new_op = OP_LDURV; break;
6541 case OP_STRF_POS: new_op = OP_STURV; break;
6542 case OP_LDRSW_POS: new_op = OP_LDURSW; break;
6543 case OP_PRFM_POS: new_op = OP_PRFUM; break;
6544 default: new_op = OP_NIL; break;
6545 }
6546
6547 if (new_op == OP_NIL)
6548 return FALSE;
6549
6550 new_opcode = aarch64_get_opcode (new_op);
6551 gas_assert (new_opcode != NULL);
6552
6553 DEBUG_TRACE ("Check programmer-friendly STURB/LDURB -> STRB/LDRB: %d == %d",
6554 instr->opcode->op, new_opcode->op);
6555
6556 aarch64_replace_opcode (instr, new_opcode);
6557
6558 /* Clear up the ADDR_SIMM9's qualifier; otherwise the
6559 qualifier matching may fail because the out-of-date qualifier will
6560 prevent the operand being updated with a new and correct qualifier. */
6561 idx = aarch64_operand_index (instr->opcode->operands,
6562 AARCH64_OPND_ADDR_SIMM9);
6563 gas_assert (idx == 1);
6564 instr->operands[idx].qualifier = AARCH64_OPND_QLF_NIL;
6565
6566 DEBUG_TRACE ("Found LDURB entry to encode programmer-friendly LDRB");
6567
6568 if (!aarch64_opcode_encode (instr->opcode, instr, &instr->value, NULL, NULL))
6569 return FALSE;
6570
6571 return TRUE;
6572}
6573
6574/* Called by fix_insn to fix a MOV immediate alias instruction.
6575
6576 Operand for a generic move immediate instruction, which is an alias
6577 instruction that generates a single MOVZ, MOVN or ORR instruction to loads
6578 a 32-bit/64-bit immediate value into general register. An assembler error
6579 shall result if the immediate cannot be created by a single one of these
6580 instructions. If there is a choice, then to ensure reversability an
6581 assembler must prefer a MOVZ to MOVN, and MOVZ or MOVN to ORR. */
6582
6583static void
6584fix_mov_imm_insn (fixS *fixP, char *buf, aarch64_inst *instr, offsetT value)
6585{
6586 const aarch64_opcode *opcode;
6587
6588 /* Need to check if the destination is SP/ZR. The check has to be done
6589 before any aarch64_replace_opcode. */
6590 int try_mov_wide_p = !aarch64_stack_pointer_p (&instr->operands[0]);
6591 int try_mov_bitmask_p = !aarch64_zero_register_p (&instr->operands[0]);
6592
6593 instr->operands[1].imm.value = value;
6594 instr->operands[1].skip = 0;
6595
6596 if (try_mov_wide_p)
6597 {
6598 /* Try the MOVZ alias. */
6599 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDE);
6600 aarch64_replace_opcode (instr, opcode);
6601 if (aarch64_opcode_encode (instr->opcode, instr,
6602 &instr->value, NULL, NULL))
6603 {
6604 put_aarch64_insn (buf, instr->value);
6605 return;
6606 }
6607 /* Try the MOVK alias. */
6608 opcode = aarch64_get_opcode (OP_MOV_IMM_WIDEN);
6609 aarch64_replace_opcode (instr, opcode);
6610 if (aarch64_opcode_encode (instr->opcode, instr,
6611 &instr->value, NULL, NULL))
6612 {
6613 put_aarch64_insn (buf, instr->value);
6614 return;
6615 }
6616 }
6617
6618 if (try_mov_bitmask_p)
6619 {
6620 /* Try the ORR alias. */
6621 opcode = aarch64_get_opcode (OP_MOV_IMM_LOG);
6622 aarch64_replace_opcode (instr, opcode);
6623 if (aarch64_opcode_encode (instr->opcode, instr,
6624 &instr->value, NULL, NULL))
6625 {
6626 put_aarch64_insn (buf, instr->value);
6627 return;
6628 }
6629 }
6630
6631 as_bad_where (fixP->fx_file, fixP->fx_line,
6632 _("immediate cannot be moved by a single instruction"));
6633}
6634
6635/* An instruction operand which is immediate related may have symbol used
6636 in the assembly, e.g.
6637
6638 mov w0, u32
6639 .set u32, 0x00ffff00
6640
6641 At the time when the assembly instruction is parsed, a referenced symbol,
6642 like 'u32' in the above example may not have been seen; a fixS is created
6643 in such a case and is handled here after symbols have been resolved.
6644 Instruction is fixed up with VALUE using the information in *FIXP plus
6645 extra information in FLAGS.
6646
6647 This function is called by md_apply_fix to fix up instructions that need
6648 a fix-up described above but does not involve any linker-time relocation. */
6649
6650static void
6651fix_insn (fixS *fixP, uint32_t flags, offsetT value)
6652{
6653 int idx;
6654 uint32_t insn;
6655 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
6656 enum aarch64_opnd opnd = fixP->tc_fix_data.opnd;
6657 aarch64_inst *new_inst = fixP->tc_fix_data.inst;
6658
6659 if (new_inst)
6660 {
6661 /* Now the instruction is about to be fixed-up, so the operand that
6662 was previously marked as 'ignored' needs to be unmarked in order
6663 to get the encoding done properly. */
6664 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
6665 new_inst->operands[idx].skip = 0;
6666 }
6667
6668 gas_assert (opnd != AARCH64_OPND_NIL);
6669
6670 switch (opnd)
6671 {
6672 case AARCH64_OPND_EXCEPTION:
6673 if (unsigned_overflow (value, 16))
6674 as_bad_where (fixP->fx_file, fixP->fx_line,
6675 _("immediate out of range"));
6676 insn = get_aarch64_insn (buf);
6677 insn |= encode_svc_imm (value);
6678 put_aarch64_insn (buf, insn);
6679 break;
6680
6681 case AARCH64_OPND_AIMM:
6682 /* ADD or SUB with immediate.
6683 NOTE this assumes we come here with a add/sub shifted reg encoding
6684 3 322|2222|2 2 2 21111 111111
6685 1 098|7654|3 2 1 09876 543210 98765 43210
6686 0b000000 sf 000|1011|shift 0 Rm imm6 Rn Rd ADD
6687 2b000000 sf 010|1011|shift 0 Rm imm6 Rn Rd ADDS
6688 4b000000 sf 100|1011|shift 0 Rm imm6 Rn Rd SUB
6689 6b000000 sf 110|1011|shift 0 Rm imm6 Rn Rd SUBS
6690 ->
6691 3 322|2222|2 2 221111111111
6692 1 098|7654|3 2 109876543210 98765 43210
6693 11000000 sf 001|0001|shift imm12 Rn Rd ADD
6694 31000000 sf 011|0001|shift imm12 Rn Rd ADDS
6695 51000000 sf 101|0001|shift imm12 Rn Rd SUB
6696 71000000 sf 111|0001|shift imm12 Rn Rd SUBS
6697 Fields sf Rn Rd are already set. */
6698 insn = get_aarch64_insn (buf);
6699 if (value < 0)
6700 {
6701 /* Add <-> sub. */
6702 insn = reencode_addsub_switch_add_sub (insn);
6703 value = -value;
6704 }
6705
6706 if ((flags & FIXUP_F_HAS_EXPLICIT_SHIFT) == 0
6707 && unsigned_overflow (value, 12))
6708 {
6709 /* Try to shift the value by 12 to make it fit. */
6710 if (((value >> 12) << 12) == value
6711 && ! unsigned_overflow (value, 12 + 12))
6712 {
6713 value >>= 12;
6714 insn |= encode_addsub_imm_shift_amount (1);
6715 }
6716 }
6717
6718 if (unsigned_overflow (value, 12))
6719 as_bad_where (fixP->fx_file, fixP->fx_line,
6720 _("immediate out of range"));
6721
6722 insn |= encode_addsub_imm (value);
6723
6724 put_aarch64_insn (buf, insn);
6725 break;
6726
6727 case AARCH64_OPND_SIMD_IMM:
6728 case AARCH64_OPND_SIMD_IMM_SFT:
6729 case AARCH64_OPND_LIMM:
6730 /* Bit mask immediate. */
6731 gas_assert (new_inst != NULL);
6732 idx = aarch64_operand_index (new_inst->opcode->operands, opnd);
6733 new_inst->operands[idx].imm.value = value;
6734 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
6735 &new_inst->value, NULL, NULL))
6736 put_aarch64_insn (buf, new_inst->value);
6737 else
6738 as_bad_where (fixP->fx_file, fixP->fx_line,
6739 _("invalid immediate"));
6740 break;
6741
6742 case AARCH64_OPND_HALF:
6743 /* 16-bit unsigned immediate. */
6744 if (unsigned_overflow (value, 16))
6745 as_bad_where (fixP->fx_file, fixP->fx_line,
6746 _("immediate out of range"));
6747 insn = get_aarch64_insn (buf);
6748 insn |= encode_movw_imm (value & 0xffff);
6749 put_aarch64_insn (buf, insn);
6750 break;
6751
6752 case AARCH64_OPND_IMM_MOV:
6753 /* Operand for a generic move immediate instruction, which is
6754 an alias instruction that generates a single MOVZ, MOVN or ORR
6755 instruction to loads a 32-bit/64-bit immediate value into general
6756 register. An assembler error shall result if the immediate cannot be
6757 created by a single one of these instructions. If there is a choice,
6758 then to ensure reversability an assembler must prefer a MOVZ to MOVN,
6759 and MOVZ or MOVN to ORR. */
6760 gas_assert (new_inst != NULL);
6761 fix_mov_imm_insn (fixP, buf, new_inst, value);
6762 break;
6763
6764 case AARCH64_OPND_ADDR_SIMM7:
6765 case AARCH64_OPND_ADDR_SIMM9:
6766 case AARCH64_OPND_ADDR_SIMM9_2:
6767 case AARCH64_OPND_ADDR_UIMM12:
6768 /* Immediate offset in an address. */
6769 insn = get_aarch64_insn (buf);
6770
6771 gas_assert (new_inst != NULL && new_inst->value == insn);
6772 gas_assert (new_inst->opcode->operands[1] == opnd
6773 || new_inst->opcode->operands[2] == opnd);
6774
6775 /* Get the index of the address operand. */
6776 if (new_inst->opcode->operands[1] == opnd)
6777 /* e.g. STR <Xt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]. */
6778 idx = 1;
6779 else
6780 /* e.g. LDP <Qt1>, <Qt2>, [<Xn|SP>{, #<imm>}]. */
6781 idx = 2;
6782
6783 /* Update the resolved offset value. */
6784 new_inst->operands[idx].addr.offset.imm = value;
6785
6786 /* Encode/fix-up. */
6787 if (aarch64_opcode_encode (new_inst->opcode, new_inst,
6788 &new_inst->value, NULL, NULL))
6789 {
6790 put_aarch64_insn (buf, new_inst->value);
6791 break;
6792 }
6793 else if (new_inst->opcode->iclass == ldst_pos
6794 && try_to_encode_as_unscaled_ldst (new_inst))
6795 {
6796 put_aarch64_insn (buf, new_inst->value);
6797 break;
6798 }
6799
6800 as_bad_where (fixP->fx_file, fixP->fx_line,
6801 _("immediate offset out of range"));
6802 break;
6803
6804 default:
6805 gas_assert (0);
6806 as_fatal (_("unhandled operand code %d"), opnd);
6807 }
6808}
6809
6810/* Apply a fixup (fixP) to segment data, once it has been determined
6811 by our caller that we have all the info we need to fix it up.
6812
6813 Parameter valP is the pointer to the value of the bits. */
6814
6815void
6816md_apply_fix (fixS * fixP, valueT * valP, segT seg)
6817{
6818 offsetT value = *valP;
6819 uint32_t insn;
6820 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
6821 int scale;
6822 unsigned flags = fixP->fx_addnumber;
6823
6824 DEBUG_TRACE ("\n\n");
6825 DEBUG_TRACE ("~~~~~~~~~~~~~~~~~~~~~~~~~");
6826 DEBUG_TRACE ("Enter md_apply_fix");
6827
6828 gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
6829
6830 /* Note whether this will delete the relocation. */
6831
6832 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
6833 fixP->fx_done = 1;
6834
6835 /* Process the relocations. */
6836 switch (fixP->fx_r_type)
6837 {
6838 case BFD_RELOC_NONE:
6839 /* This will need to go in the object file. */
6840 fixP->fx_done = 0;
6841 break;
6842
6843 case BFD_RELOC_8:
6844 case BFD_RELOC_8_PCREL:
6845 if (fixP->fx_done || !seg->use_rela_p)
6846 md_number_to_chars (buf, value, 1);
6847 break;
6848
6849 case BFD_RELOC_16:
6850 case BFD_RELOC_16_PCREL:
6851 if (fixP->fx_done || !seg->use_rela_p)
6852 md_number_to_chars (buf, value, 2);
6853 break;
6854
6855 case BFD_RELOC_32:
6856 case BFD_RELOC_32_PCREL:
6857 if (fixP->fx_done || !seg->use_rela_p)
6858 md_number_to_chars (buf, value, 4);
6859 break;
6860
6861 case BFD_RELOC_64:
6862 case BFD_RELOC_64_PCREL:
6863 if (fixP->fx_done || !seg->use_rela_p)
6864 md_number_to_chars (buf, value, 8);
6865 break;
6866
6867 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
6868 /* We claim that these fixups have been processed here, even if
6869 in fact we generate an error because we do not have a reloc
6870 for them, so tc_gen_reloc() will reject them. */
6871 fixP->fx_done = 1;
6872 if (fixP->fx_addsy && !S_IS_DEFINED (fixP->fx_addsy))
6873 {
6874 as_bad_where (fixP->fx_file, fixP->fx_line,
6875 _("undefined symbol %s used as an immediate value"),
6876 S_GET_NAME (fixP->fx_addsy));
6877 goto apply_fix_return;
6878 }
6879 fix_insn (fixP, flags, value);
6880 break;
6881
6882 case BFD_RELOC_AARCH64_LD_LO19_PCREL:
a06ea964
NC
6883 if (fixP->fx_done || !seg->use_rela_p)
6884 {
89d2a2a3
MS
6885 if (value & 3)
6886 as_bad_where (fixP->fx_file, fixP->fx_line,
6887 _("pc-relative load offset not word aligned"));
6888 if (signed_overflow (value, 21))
6889 as_bad_where (fixP->fx_file, fixP->fx_line,
6890 _("pc-relative load offset out of range"));
a06ea964
NC
6891 insn = get_aarch64_insn (buf);
6892 insn |= encode_ld_lit_ofs_19 (value >> 2);
6893 put_aarch64_insn (buf, insn);
6894 }
6895 break;
6896
6897 case BFD_RELOC_AARCH64_ADR_LO21_PCREL:
a06ea964
NC
6898 if (fixP->fx_done || !seg->use_rela_p)
6899 {
89d2a2a3
MS
6900 if (signed_overflow (value, 21))
6901 as_bad_where (fixP->fx_file, fixP->fx_line,
6902 _("pc-relative address offset out of range"));
a06ea964
NC
6903 insn = get_aarch64_insn (buf);
6904 insn |= encode_adr_imm (value);
6905 put_aarch64_insn (buf, insn);
6906 }
6907 break;
6908
6909 case BFD_RELOC_AARCH64_BRANCH19:
a06ea964
NC
6910 if (fixP->fx_done || !seg->use_rela_p)
6911 {
89d2a2a3
MS
6912 if (value & 3)
6913 as_bad_where (fixP->fx_file, fixP->fx_line,
6914 _("conditional branch target not word aligned"));
6915 if (signed_overflow (value, 21))
6916 as_bad_where (fixP->fx_file, fixP->fx_line,
6917 _("conditional branch out of range"));
a06ea964
NC
6918 insn = get_aarch64_insn (buf);
6919 insn |= encode_cond_branch_ofs_19 (value >> 2);
6920 put_aarch64_insn (buf, insn);
6921 }
6922 break;
6923
6924 case BFD_RELOC_AARCH64_TSTBR14:
a06ea964
NC
6925 if (fixP->fx_done || !seg->use_rela_p)
6926 {
89d2a2a3
MS
6927 if (value & 3)
6928 as_bad_where (fixP->fx_file, fixP->fx_line,
6929 _("conditional branch target not word aligned"));
6930 if (signed_overflow (value, 16))
6931 as_bad_where (fixP->fx_file, fixP->fx_line,
6932 _("conditional branch out of range"));
a06ea964
NC
6933 insn = get_aarch64_insn (buf);
6934 insn |= encode_tst_branch_ofs_14 (value >> 2);
6935 put_aarch64_insn (buf, insn);
6936 }
6937 break;
6938
a06ea964 6939 case BFD_RELOC_AARCH64_CALL26:
f09c556a 6940 case BFD_RELOC_AARCH64_JUMP26:
a06ea964
NC
6941 if (fixP->fx_done || !seg->use_rela_p)
6942 {
89d2a2a3
MS
6943 if (value & 3)
6944 as_bad_where (fixP->fx_file, fixP->fx_line,
6945 _("branch target not word aligned"));
6946 if (signed_overflow (value, 28))
6947 as_bad_where (fixP->fx_file, fixP->fx_line,
6948 _("branch out of range"));
a06ea964
NC
6949 insn = get_aarch64_insn (buf);
6950 insn |= encode_branch_ofs_26 (value >> 2);
6951 put_aarch64_insn (buf, insn);
6952 }
6953 break;
6954
6955 case BFD_RELOC_AARCH64_MOVW_G0:
a06ea964 6956 case BFD_RELOC_AARCH64_MOVW_G0_NC:
f09c556a 6957 case BFD_RELOC_AARCH64_MOVW_G0_S:
ca632371 6958 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G0_NC:
a06ea964
NC
6959 scale = 0;
6960 goto movw_common;
6961 case BFD_RELOC_AARCH64_MOVW_G1:
a06ea964 6962 case BFD_RELOC_AARCH64_MOVW_G1_NC:
f09c556a 6963 case BFD_RELOC_AARCH64_MOVW_G1_S:
654248e7 6964 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
a06ea964
NC
6965 scale = 16;
6966 goto movw_common;
43a357f9
RL
6967 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
6968 scale = 0;
6969 S_SET_THREAD_LOCAL (fixP->fx_addsy);
6970 /* Should always be exported to object file, see
6971 aarch64_force_relocation(). */
6972 gas_assert (!fixP->fx_done);
6973 gas_assert (seg->use_rela_p);
6974 goto movw_common;
6975 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
6976 scale = 16;
6977 S_SET_THREAD_LOCAL (fixP->fx_addsy);
6978 /* Should always be exported to object file, see
6979 aarch64_force_relocation(). */
6980 gas_assert (!fixP->fx_done);
6981 gas_assert (seg->use_rela_p);
6982 goto movw_common;
a06ea964 6983 case BFD_RELOC_AARCH64_MOVW_G2:
a06ea964 6984 case BFD_RELOC_AARCH64_MOVW_G2_NC:
f09c556a 6985 case BFD_RELOC_AARCH64_MOVW_G2_S:
a06ea964
NC
6986 scale = 32;
6987 goto movw_common;
6988 case BFD_RELOC_AARCH64_MOVW_G3:
6989 scale = 48;
6990 movw_common:
6991 if (fixP->fx_done || !seg->use_rela_p)
6992 {
6993 insn = get_aarch64_insn (buf);
6994
6995 if (!fixP->fx_done)
6996 {
6997 /* REL signed addend must fit in 16 bits */
6998 if (signed_overflow (value, 16))
6999 as_bad_where (fixP->fx_file, fixP->fx_line,
7000 _("offset out of range"));
7001 }
7002 else
7003 {
7004 /* Check for overflow and scale. */
7005 switch (fixP->fx_r_type)
7006 {
7007 case BFD_RELOC_AARCH64_MOVW_G0:
7008 case BFD_RELOC_AARCH64_MOVW_G1:
7009 case BFD_RELOC_AARCH64_MOVW_G2:
7010 case BFD_RELOC_AARCH64_MOVW_G3:
654248e7 7011 case BFD_RELOC_AARCH64_MOVW_GOTOFF_G1:
43a357f9 7012 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964
NC
7013 if (unsigned_overflow (value, scale + 16))
7014 as_bad_where (fixP->fx_file, fixP->fx_line,
7015 _("unsigned value out of range"));
7016 break;
7017 case BFD_RELOC_AARCH64_MOVW_G0_S:
7018 case BFD_RELOC_AARCH64_MOVW_G1_S:
7019 case BFD_RELOC_AARCH64_MOVW_G2_S:
7020 /* NOTE: We can only come here with movz or movn. */
7021 if (signed_overflow (value, scale + 16))
7022 as_bad_where (fixP->fx_file, fixP->fx_line,
7023 _("signed value out of range"));
7024 if (value < 0)
7025 {
7026 /* Force use of MOVN. */
7027 value = ~value;
7028 insn = reencode_movzn_to_movn (insn);
7029 }
7030 else
7031 {
7032 /* Force use of MOVZ. */
7033 insn = reencode_movzn_to_movz (insn);
7034 }
7035 break;
7036 default:
7037 /* Unchecked relocations. */
7038 break;
7039 }
7040 value >>= scale;
7041 }
7042
7043 /* Insert value into MOVN/MOVZ/MOVK instruction. */
7044 insn |= encode_movw_imm (value & 0xffff);
7045
7046 put_aarch64_insn (buf, insn);
7047 }
7048 break;
7049
a6bb11b2
YZ
7050 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
7051 fixP->fx_r_type = (ilp32_p
7052 ? BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC
7053 : BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7054 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7055 /* Should always be exported to object file, see
7056 aarch64_force_relocation(). */
7057 gas_assert (!fixP->fx_done);
7058 gas_assert (seg->use_rela_p);
7059 break;
7060
7061 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
7062 fixP->fx_r_type = (ilp32_p
7063 ? BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC
7064 : BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC);
7065 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7066 /* Should always be exported to object file, see
7067 aarch64_force_relocation(). */
7068 gas_assert (!fixP->fx_done);
7069 gas_assert (seg->use_rela_p);
7070 break;
7071
2c0a3565
MS
7072 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
7073 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 7074 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
2c0a3565
MS
7075 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7076 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
1ada945d 7077 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
a06ea964 7078 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
2c0a3565 7079 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 7080 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
3e8286c0 7081 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
1aa66fb1 7082 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 7083 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 7084 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
2c0a3565 7085 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 7086 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
7087 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7088 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
49df5539 7089 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
70151fb5 7090 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
13289c10 7091 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
a12fad50 7092 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
1107e076 7093 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6c37fedc 7094 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4c562523
JW
7095 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7096 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7097 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7098 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7099 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7100 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7101 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7102 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
49df5539
JW
7103 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7104 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7105 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7106 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7107 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964 7108 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
2c0a3565 7109 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
a06ea964 7110 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
a06ea964
NC
7111 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7112 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
2c0a3565
MS
7113 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7114 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7115 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
a06ea964
NC
7116 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7117 /* Should always be exported to object file, see
7118 aarch64_force_relocation(). */
7119 gas_assert (!fixP->fx_done);
7120 gas_assert (seg->use_rela_p);
7121 break;
7122
a6bb11b2
YZ
7123 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
7124 /* Should always be exported to object file, see
7125 aarch64_force_relocation(). */
7126 fixP->fx_r_type = (ilp32_p
7127 ? BFD_RELOC_AARCH64_LD32_GOT_LO12_NC
7128 : BFD_RELOC_AARCH64_LD64_GOT_LO12_NC);
7129 gas_assert (!fixP->fx_done);
7130 gas_assert (seg->use_rela_p);
7131 break;
7132
a06ea964 7133 case BFD_RELOC_AARCH64_ADD_LO12:
f09c556a
JW
7134 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7135 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7136 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7137 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7138 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3d715ce4 7139 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
87f5fbcc 7140 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
a921b5bd 7141 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
f09c556a
JW
7142 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7143 case BFD_RELOC_AARCH64_LDST128_LO12:
a06ea964
NC
7144 case BFD_RELOC_AARCH64_LDST16_LO12:
7145 case BFD_RELOC_AARCH64_LDST32_LO12:
7146 case BFD_RELOC_AARCH64_LDST64_LO12:
f09c556a 7147 case BFD_RELOC_AARCH64_LDST8_LO12:
a06ea964
NC
7148 /* Should always be exported to object file, see
7149 aarch64_force_relocation(). */
7150 gas_assert (!fixP->fx_done);
7151 gas_assert (seg->use_rela_p);
7152 break;
7153
7154 case BFD_RELOC_AARCH64_TLSDESC_ADD:
a06ea964 7155 case BFD_RELOC_AARCH64_TLSDESC_CALL:
f09c556a 7156 case BFD_RELOC_AARCH64_TLSDESC_LDR:
a06ea964
NC
7157 break;
7158
b97e87cc
NC
7159 case BFD_RELOC_UNUSED:
7160 /* An error will already have been reported. */
7161 break;
7162
a06ea964
NC
7163 default:
7164 as_bad_where (fixP->fx_file, fixP->fx_line,
7165 _("unexpected %s fixup"),
7166 bfd_get_reloc_code_name (fixP->fx_r_type));
7167 break;
7168 }
7169
7170apply_fix_return:
7171 /* Free the allocated the struct aarch64_inst.
7172 N.B. currently there are very limited number of fix-up types actually use
7173 this field, so the impact on the performance should be minimal . */
7174 if (fixP->tc_fix_data.inst != NULL)
7175 free (fixP->tc_fix_data.inst);
7176
7177 return;
7178}
7179
7180/* Translate internal representation of relocation info to BFD target
7181 format. */
7182
7183arelent *
7184tc_gen_reloc (asection * section, fixS * fixp)
7185{
7186 arelent *reloc;
7187 bfd_reloc_code_real_type code;
7188
325801bd 7189 reloc = XNEW (arelent);
a06ea964 7190
325801bd 7191 reloc->sym_ptr_ptr = XNEW (asymbol *);
a06ea964
NC
7192 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
7193 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7194
7195 if (fixp->fx_pcrel)
7196 {
7197 if (section->use_rela_p)
7198 fixp->fx_offset -= md_pcrel_from_section (fixp, section);
7199 else
7200 fixp->fx_offset = reloc->address;
7201 }
7202 reloc->addend = fixp->fx_offset;
7203
7204 code = fixp->fx_r_type;
7205 switch (code)
7206 {
7207 case BFD_RELOC_16:
7208 if (fixp->fx_pcrel)
7209 code = BFD_RELOC_16_PCREL;
7210 break;
7211
7212 case BFD_RELOC_32:
7213 if (fixp->fx_pcrel)
7214 code = BFD_RELOC_32_PCREL;
7215 break;
7216
7217 case BFD_RELOC_64:
7218 if (fixp->fx_pcrel)
7219 code = BFD_RELOC_64_PCREL;
7220 break;
7221
7222 default:
7223 break;
7224 }
7225
7226 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
7227 if (reloc->howto == NULL)
7228 {
7229 as_bad_where (fixp->fx_file, fixp->fx_line,
7230 _
7231 ("cannot represent %s relocation in this object file format"),
7232 bfd_get_reloc_code_name (code));
7233 return NULL;
7234 }
7235
7236 return reloc;
7237}
7238
7239/* This fix_new is called by cons via TC_CONS_FIX_NEW. */
7240
7241void
7242cons_fix_new_aarch64 (fragS * frag, int where, int size, expressionS * exp)
7243{
7244 bfd_reloc_code_real_type type;
7245 int pcrel = 0;
7246
7247 /* Pick a reloc.
7248 FIXME: @@ Should look at CPU word size. */
7249 switch (size)
7250 {
7251 case 1:
7252 type = BFD_RELOC_8;
7253 break;
7254 case 2:
7255 type = BFD_RELOC_16;
7256 break;
7257 case 4:
7258 type = BFD_RELOC_32;
7259 break;
7260 case 8:
7261 type = BFD_RELOC_64;
7262 break;
7263 default:
7264 as_bad (_("cannot do %u-byte relocation"), size);
7265 type = BFD_RELOC_UNUSED;
7266 break;
7267 }
7268
7269 fix_new_exp (frag, where, (int) size, exp, pcrel, type);
7270}
7271
7272int
7273aarch64_force_relocation (struct fix *fixp)
7274{
7275 switch (fixp->fx_r_type)
7276 {
7277 case BFD_RELOC_AARCH64_GAS_INTERNAL_FIXUP:
7278 /* Perform these "immediate" internal relocations
7279 even if the symbol is extern or weak. */
7280 return 0;
7281
a6bb11b2 7282 case BFD_RELOC_AARCH64_LD_GOT_LO12_NC:
f09c556a
JW
7283 case BFD_RELOC_AARCH64_TLSDESC_LD_LO12_NC:
7284 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_LO12_NC:
a6bb11b2
YZ
7285 /* Pseudo relocs that need to be fixed up according to
7286 ilp32_p. */
7287 return 0;
7288
2c0a3565
MS
7289 case BFD_RELOC_AARCH64_ADD_LO12:
7290 case BFD_RELOC_AARCH64_ADR_GOT_PAGE:
7291 case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL:
7292 case BFD_RELOC_AARCH64_ADR_HI21_PCREL:
7293 case BFD_RELOC_AARCH64_GOT_LD_PREL19:
7294 case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC:
3d715ce4 7295 case BFD_RELOC_AARCH64_LD32_GOTPAGE_LO14:
87f5fbcc 7296 case BFD_RELOC_AARCH64_LD64_GOTOFF_LO15:
a921b5bd 7297 case BFD_RELOC_AARCH64_LD64_GOTPAGE_LO15:
2c0a3565
MS
7298 case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC:
7299 case BFD_RELOC_AARCH64_LDST128_LO12:
7300 case BFD_RELOC_AARCH64_LDST16_LO12:
7301 case BFD_RELOC_AARCH64_LDST32_LO12:
7302 case BFD_RELOC_AARCH64_LDST64_LO12:
7303 case BFD_RELOC_AARCH64_LDST8_LO12:
7304 case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC:
7305 case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21:
389b8029 7306 case BFD_RELOC_AARCH64_TLSDESC_ADR_PREL21:
2c0a3565
MS
7307 case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC:
7308 case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC:
1ada945d 7309 case BFD_RELOC_AARCH64_TLSDESC_LD_PREL19:
43a357f9
RL
7310 case BFD_RELOC_AARCH64_TLSDESC_OFF_G0_NC:
7311 case BFD_RELOC_AARCH64_TLSDESC_OFF_G1:
a06ea964 7312 case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC:
2c0a3565 7313 case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21:
3c12b054 7314 case BFD_RELOC_AARCH64_TLSGD_ADR_PREL21:
3e8286c0 7315 case BFD_RELOC_AARCH64_TLSGD_MOVW_G0_NC:
1aa66fb1 7316 case BFD_RELOC_AARCH64_TLSGD_MOVW_G1:
a06ea964 7317 case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
a6bb11b2 7318 case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC:
2c0a3565 7319 case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
043bf05a 7320 case BFD_RELOC_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
3b957e5b
RL
7321 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
7322 case BFD_RELOC_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
7323 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_HI12:
70151fb5 7324 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12:
13289c10 7325 case BFD_RELOC_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
a12fad50 7326 case BFD_RELOC_AARCH64_TLSLD_ADD_LO12_NC:
1107e076 7327 case BFD_RELOC_AARCH64_TLSLD_ADR_PAGE21:
6c37fedc 7328 case BFD_RELOC_AARCH64_TLSLD_ADR_PREL21:
4c562523
JW
7329 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12:
7330 case BFD_RELOC_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC:
7331 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12:
7332 case BFD_RELOC_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC:
7333 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12:
7334 case BFD_RELOC_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC:
7335 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12:
7336 case BFD_RELOC_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC:
49df5539
JW
7337 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0:
7338 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7339 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1:
7340 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G1_NC:
7341 case BFD_RELOC_AARCH64_TLSLD_MOVW_DTPREL_G2:
a06ea964 7342 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12:
2c0a3565 7343 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12:
a06ea964 7344 case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
a06ea964
NC
7345 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0:
7346 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
2c0a3565
MS
7347 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1:
7348 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7349 case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2:
a06ea964
NC
7350 /* Always leave these relocations for the linker. */
7351 return 1;
7352
7353 default:
7354 break;
7355 }
7356
7357 return generic_force_reloc (fixp);
7358}
7359
7360#ifdef OBJ_ELF
7361
7362const char *
7363elf64_aarch64_target_format (void)
7364{
a75cf613
ES
7365 if (strcmp (TARGET_OS, "cloudabi") == 0)
7366 {
7367 /* FIXME: What to do for ilp32_p ? */
7368 return target_big_endian ? "elf64-bigaarch64-cloudabi" : "elf64-littleaarch64-cloudabi";
7369 }
a06ea964 7370 if (target_big_endian)
cec5225b 7371 return ilp32_p ? "elf32-bigaarch64" : "elf64-bigaarch64";
a06ea964 7372 else
cec5225b 7373 return ilp32_p ? "elf32-littleaarch64" : "elf64-littleaarch64";
a06ea964
NC
7374}
7375
7376void
7377aarch64elf_frob_symbol (symbolS * symp, int *puntp)
7378{
7379 elf_frob_symbol (symp, puntp);
7380}
7381#endif
7382
7383/* MD interface: Finalization. */
7384
7385/* A good place to do this, although this was probably not intended
7386 for this kind of use. We need to dump the literal pool before
7387 references are made to a null symbol pointer. */
7388
7389void
7390aarch64_cleanup (void)
7391{
7392 literal_pool *pool;
7393
7394 for (pool = list_of_pools; pool; pool = pool->next)
7395 {
7396 /* Put it at the end of the relevant section. */
7397 subseg_set (pool->section, pool->sub_section);
7398 s_ltorg (0);
7399 }
7400}
7401
7402#ifdef OBJ_ELF
7403/* Remove any excess mapping symbols generated for alignment frags in
7404 SEC. We may have created a mapping symbol before a zero byte
7405 alignment; remove it if there's a mapping symbol after the
7406 alignment. */
7407static void
7408check_mapping_symbols (bfd * abfd ATTRIBUTE_UNUSED, asection * sec,
7409 void *dummy ATTRIBUTE_UNUSED)
7410{
7411 segment_info_type *seginfo = seg_info (sec);
7412 fragS *fragp;
7413
7414 if (seginfo == NULL || seginfo->frchainP == NULL)
7415 return;
7416
7417 for (fragp = seginfo->frchainP->frch_root;
7418 fragp != NULL; fragp = fragp->fr_next)
7419 {
7420 symbolS *sym = fragp->tc_frag_data.last_map;
7421 fragS *next = fragp->fr_next;
7422
7423 /* Variable-sized frags have been converted to fixed size by
7424 this point. But if this was variable-sized to start with,
7425 there will be a fixed-size frag after it. So don't handle
7426 next == NULL. */
7427 if (sym == NULL || next == NULL)
7428 continue;
7429
7430 if (S_GET_VALUE (sym) < next->fr_address)
7431 /* Not at the end of this frag. */
7432 continue;
7433 know (S_GET_VALUE (sym) == next->fr_address);
7434
7435 do
7436 {
7437 if (next->tc_frag_data.first_map != NULL)
7438 {
7439 /* Next frag starts with a mapping symbol. Discard this
7440 one. */
7441 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
7442 break;
7443 }
7444
7445 if (next->fr_next == NULL)
7446 {
7447 /* This mapping symbol is at the end of the section. Discard
7448 it. */
7449 know (next->fr_fix == 0 && next->fr_var == 0);
7450 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
7451 break;
7452 }
7453
7454 /* As long as we have empty frags without any mapping symbols,
7455 keep looking. */
7456 /* If the next frag is non-empty and does not start with a
7457 mapping symbol, then this mapping symbol is required. */
7458 if (next->fr_address != next->fr_next->fr_address)
7459 break;
7460
7461 next = next->fr_next;
7462 }
7463 while (next != NULL);
7464 }
7465}
7466#endif
7467
7468/* Adjust the symbol table. */
7469
7470void
7471aarch64_adjust_symtab (void)
7472{
7473#ifdef OBJ_ELF
7474 /* Remove any overlapping mapping symbols generated by alignment frags. */
7475 bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
7476 /* Now do generic ELF adjustments. */
7477 elf_adjust_symtab ();
7478#endif
7479}
7480
7481static void
7482checked_hash_insert (struct hash_control *table, const char *key, void *value)
7483{
7484 const char *hash_err;
7485
7486 hash_err = hash_insert (table, key, value);
7487 if (hash_err)
7488 printf ("Internal Error: Can't hash %s\n", key);
7489}
7490
7491static void
7492fill_instruction_hash_table (void)
7493{
7494 aarch64_opcode *opcode = aarch64_opcode_table;
7495
7496 while (opcode->name != NULL)
7497 {
7498 templates *templ, *new_templ;
7499 templ = hash_find (aarch64_ops_hsh, opcode->name);
7500
add39d23 7501 new_templ = XNEW (templates);
a06ea964
NC
7502 new_templ->opcode = opcode;
7503 new_templ->next = NULL;
7504
7505 if (!templ)
7506 checked_hash_insert (aarch64_ops_hsh, opcode->name, (void *) new_templ);
7507 else
7508 {
7509 new_templ->next = templ->next;
7510 templ->next = new_templ;
7511 }
7512 ++opcode;
7513 }
7514}
7515
7516static inline void
7517convert_to_upper (char *dst, const char *src, size_t num)
7518{
7519 unsigned int i;
7520 for (i = 0; i < num && *src != '\0'; ++i, ++dst, ++src)
7521 *dst = TOUPPER (*src);
7522 *dst = '\0';
7523}
7524
7525/* Assume STR point to a lower-case string, allocate, convert and return
7526 the corresponding upper-case string. */
7527static inline const char*
7528get_upper_str (const char *str)
7529{
7530 char *ret;
7531 size_t len = strlen (str);
325801bd 7532 ret = XNEWVEC (char, len + 1);
a06ea964
NC
7533 convert_to_upper (ret, str, len);
7534 return ret;
7535}
7536
7537/* MD interface: Initialization. */
7538
7539void
7540md_begin (void)
7541{
7542 unsigned mach;
7543 unsigned int i;
7544
7545 if ((aarch64_ops_hsh = hash_new ()) == NULL
7546 || (aarch64_cond_hsh = hash_new ()) == NULL
7547 || (aarch64_shift_hsh = hash_new ()) == NULL
7548 || (aarch64_sys_regs_hsh = hash_new ()) == NULL
7549 || (aarch64_pstatefield_hsh = hash_new ()) == NULL
7550 || (aarch64_sys_regs_ic_hsh = hash_new ()) == NULL
7551 || (aarch64_sys_regs_dc_hsh = hash_new ()) == NULL
7552 || (aarch64_sys_regs_at_hsh = hash_new ()) == NULL
7553 || (aarch64_sys_regs_tlbi_hsh = hash_new ()) == NULL
7554 || (aarch64_reg_hsh = hash_new ()) == NULL
7555 || (aarch64_barrier_opt_hsh = hash_new ()) == NULL
7556 || (aarch64_nzcv_hsh = hash_new ()) == NULL
1e6f4800
MW
7557 || (aarch64_pldop_hsh = hash_new ()) == NULL
7558 || (aarch64_hint_opt_hsh = hash_new ()) == NULL)
a06ea964
NC
7559 as_fatal (_("virtual memory exhausted"));
7560
7561 fill_instruction_hash_table ();
7562
7563 for (i = 0; aarch64_sys_regs[i].name != NULL; ++i)
7564 checked_hash_insert (aarch64_sys_regs_hsh, aarch64_sys_regs[i].name,
7565 (void *) (aarch64_sys_regs + i));
7566
7567 for (i = 0; aarch64_pstatefields[i].name != NULL; ++i)
7568 checked_hash_insert (aarch64_pstatefield_hsh,
7569 aarch64_pstatefields[i].name,
7570 (void *) (aarch64_pstatefields + i));
7571
875880c6 7572 for (i = 0; aarch64_sys_regs_ic[i].name != NULL; i++)
a06ea964 7573 checked_hash_insert (aarch64_sys_regs_ic_hsh,
875880c6 7574 aarch64_sys_regs_ic[i].name,
a06ea964
NC
7575 (void *) (aarch64_sys_regs_ic + i));
7576
875880c6 7577 for (i = 0; aarch64_sys_regs_dc[i].name != NULL; i++)
a06ea964 7578 checked_hash_insert (aarch64_sys_regs_dc_hsh,
875880c6 7579 aarch64_sys_regs_dc[i].name,
a06ea964
NC
7580 (void *) (aarch64_sys_regs_dc + i));
7581
875880c6 7582 for (i = 0; aarch64_sys_regs_at[i].name != NULL; i++)
a06ea964 7583 checked_hash_insert (aarch64_sys_regs_at_hsh,
875880c6 7584 aarch64_sys_regs_at[i].name,
a06ea964
NC
7585 (void *) (aarch64_sys_regs_at + i));
7586
875880c6 7587 for (i = 0; aarch64_sys_regs_tlbi[i].name != NULL; i++)
a06ea964 7588 checked_hash_insert (aarch64_sys_regs_tlbi_hsh,
875880c6 7589 aarch64_sys_regs_tlbi[i].name,
a06ea964
NC
7590 (void *) (aarch64_sys_regs_tlbi + i));
7591
7592 for (i = 0; i < ARRAY_SIZE (reg_names); i++)
7593 checked_hash_insert (aarch64_reg_hsh, reg_names[i].name,
7594 (void *) (reg_names + i));
7595
7596 for (i = 0; i < ARRAY_SIZE (nzcv_names); i++)
7597 checked_hash_insert (aarch64_nzcv_hsh, nzcv_names[i].template,
7598 (void *) (nzcv_names + i));
7599
7600 for (i = 0; aarch64_operand_modifiers[i].name != NULL; i++)
7601 {
7602 const char *name = aarch64_operand_modifiers[i].name;
7603 checked_hash_insert (aarch64_shift_hsh, name,
7604 (void *) (aarch64_operand_modifiers + i));
7605 /* Also hash the name in the upper case. */
7606 checked_hash_insert (aarch64_shift_hsh, get_upper_str (name),
7607 (void *) (aarch64_operand_modifiers + i));
7608 }
7609
7610 for (i = 0; i < ARRAY_SIZE (aarch64_conds); i++)
7611 {
7612 unsigned int j;
7613 /* A condition code may have alias(es), e.g. "cc", "lo" and "ul" are
7614 the same condition code. */
7615 for (j = 0; j < ARRAY_SIZE (aarch64_conds[i].names); ++j)
7616 {
7617 const char *name = aarch64_conds[i].names[j];
7618 if (name == NULL)
7619 break;
7620 checked_hash_insert (aarch64_cond_hsh, name,
7621 (void *) (aarch64_conds + i));
7622 /* Also hash the name in the upper case. */
7623 checked_hash_insert (aarch64_cond_hsh, get_upper_str (name),
7624 (void *) (aarch64_conds + i));
7625 }
7626 }
7627
7628 for (i = 0; i < ARRAY_SIZE (aarch64_barrier_options); i++)
7629 {
7630 const char *name = aarch64_barrier_options[i].name;
7631 /* Skip xx00 - the unallocated values of option. */
7632 if ((i & 0x3) == 0)
7633 continue;
7634 checked_hash_insert (aarch64_barrier_opt_hsh, name,
7635 (void *) (aarch64_barrier_options + i));
7636 /* Also hash the name in the upper case. */
7637 checked_hash_insert (aarch64_barrier_opt_hsh, get_upper_str (name),
7638 (void *) (aarch64_barrier_options + i));
7639 }
7640
7641 for (i = 0; i < ARRAY_SIZE (aarch64_prfops); i++)
7642 {
7643 const char* name = aarch64_prfops[i].name;
a1ccaec9
YZ
7644 /* Skip the unallocated hint encodings. */
7645 if (name == NULL)
a06ea964
NC
7646 continue;
7647 checked_hash_insert (aarch64_pldop_hsh, name,
7648 (void *) (aarch64_prfops + i));
7649 /* Also hash the name in the upper case. */
7650 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
7651 (void *) (aarch64_prfops + i));
7652 }
7653
1e6f4800
MW
7654 for (i = 0; aarch64_hint_options[i].name != NULL; i++)
7655 {
7656 const char* name = aarch64_hint_options[i].name;
7657
7658 checked_hash_insert (aarch64_hint_opt_hsh, name,
7659 (void *) (aarch64_hint_options + i));
7660 /* Also hash the name in the upper case. */
7661 checked_hash_insert (aarch64_pldop_hsh, get_upper_str (name),
7662 (void *) (aarch64_hint_options + i));
7663 }
7664
a06ea964
NC
7665 /* Set the cpu variant based on the command-line options. */
7666 if (!mcpu_cpu_opt)
7667 mcpu_cpu_opt = march_cpu_opt;
7668
7669 if (!mcpu_cpu_opt)
7670 mcpu_cpu_opt = &cpu_default;
7671
7672 cpu_variant = *mcpu_cpu_opt;
7673
7674 /* Record the CPU type. */
cec5225b 7675 mach = ilp32_p ? bfd_mach_aarch64_ilp32 : bfd_mach_aarch64;
a06ea964
NC
7676
7677 bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
7678}
7679
7680/* Command line processing. */
7681
7682const char *md_shortopts = "m:";
7683
7684#ifdef AARCH64_BI_ENDIAN
7685#define OPTION_EB (OPTION_MD_BASE + 0)
7686#define OPTION_EL (OPTION_MD_BASE + 1)
7687#else
7688#if TARGET_BYTES_BIG_ENDIAN
7689#define OPTION_EB (OPTION_MD_BASE + 0)
7690#else
7691#define OPTION_EL (OPTION_MD_BASE + 1)
7692#endif
7693#endif
7694
7695struct option md_longopts[] = {
7696#ifdef OPTION_EB
7697 {"EB", no_argument, NULL, OPTION_EB},
7698#endif
7699#ifdef OPTION_EL
7700 {"EL", no_argument, NULL, OPTION_EL},
7701#endif
7702 {NULL, no_argument, NULL, 0}
7703};
7704
7705size_t md_longopts_size = sizeof (md_longopts);
7706
7707struct aarch64_option_table
7708{
e0471c16
TS
7709 const char *option; /* Option name to match. */
7710 const char *help; /* Help information. */
a06ea964
NC
7711 int *var; /* Variable to change. */
7712 int value; /* What to change it to. */
7713 char *deprecated; /* If non-null, print this message. */
7714};
7715
7716static struct aarch64_option_table aarch64_opts[] = {
7717 {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
7718 {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
7719 NULL},
7720#ifdef DEBUG_AARCH64
7721 {"mdebug-dump", N_("temporary switch for dumping"), &debug_dump, 1, NULL},
7722#endif /* DEBUG_AARCH64 */
7723 {"mverbose-error", N_("output verbose error messages"), &verbose_error_p, 1,
7724 NULL},
a52e6fd3
YZ
7725 {"mno-verbose-error", N_("do not output verbose error messages"),
7726 &verbose_error_p, 0, NULL},
a06ea964
NC
7727 {NULL, NULL, NULL, 0, NULL}
7728};
7729
7730struct aarch64_cpu_option_table
7731{
e0471c16 7732 const char *name;
a06ea964
NC
7733 const aarch64_feature_set value;
7734 /* The canonical name of the CPU, or NULL to use NAME converted to upper
7735 case. */
7736 const char *canonical_name;
7737};
7738
7739/* This list should, at a minimum, contain all the cpu names
7740 recognized by GCC. */
7741static const struct aarch64_cpu_option_table aarch64_cpus[] = {
7742 {"all", AARCH64_ANY, NULL},
9c352f1c
JG
7743 {"cortex-a35", AARCH64_FEATURE (AARCH64_ARCH_V8,
7744 AARCH64_FEATURE_CRC), "Cortex-A35"},
aa31c464
JW
7745 {"cortex-a53", AARCH64_FEATURE (AARCH64_ARCH_V8,
7746 AARCH64_FEATURE_CRC), "Cortex-A53"},
7747 {"cortex-a57", AARCH64_FEATURE (AARCH64_ARCH_V8,
7748 AARCH64_FEATURE_CRC), "Cortex-A57"},
2abdd192
JW
7749 {"cortex-a72", AARCH64_FEATURE (AARCH64_ARCH_V8,
7750 AARCH64_FEATURE_CRC), "Cortex-A72"},
1aa70332
KT
7751 {"cortex-a73", AARCH64_FEATURE (AARCH64_ARCH_V8,
7752 AARCH64_FEATURE_CRC), "Cortex-A73"},
2412d878
EM
7753 {"exynos-m1", AARCH64_FEATURE (AARCH64_ARCH_V8,
7754 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
7755 "Samsung Exynos M1"},
6b21c2bf
JW
7756 {"qdf24xx", AARCH64_FEATURE (AARCH64_ARCH_V8,
7757 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
7758 "Qualcomm QDF24XX"},
faade851
JW
7759 {"thunderx", AARCH64_FEATURE (AARCH64_ARCH_V8,
7760 AARCH64_FEATURE_CRC | AARCH64_FEATURE_CRYPTO),
7761 "Cavium ThunderX"},
9f99c22e
VP
7762 {"vulcan", AARCH64_FEATURE (AARCH64_ARCH_V8_1,
7763 AARCH64_FEATURE_CRYPTO),
0a8be2fe 7764 "Broadcom Vulcan"},
070cb956
PT
7765 /* The 'xgene-1' name is an older name for 'xgene1', which was used
7766 in earlier releases and is superseded by 'xgene1' in all
7767 tools. */
9877c63c 7768 {"xgene-1", AARCH64_ARCH_V8, "APM X-Gene 1"},
070cb956 7769 {"xgene1", AARCH64_ARCH_V8, "APM X-Gene 1"},
aa31c464
JW
7770 {"xgene2", AARCH64_FEATURE (AARCH64_ARCH_V8,
7771 AARCH64_FEATURE_CRC), "APM X-Gene 2"},
a06ea964
NC
7772 {"generic", AARCH64_ARCH_V8, NULL},
7773
a06ea964
NC
7774 {NULL, AARCH64_ARCH_NONE, NULL}
7775};
7776
7777struct aarch64_arch_option_table
7778{
e0471c16 7779 const char *name;
a06ea964
NC
7780 const aarch64_feature_set value;
7781};
7782
7783/* This list should, at a minimum, contain all the architecture names
7784 recognized by GCC. */
7785static const struct aarch64_arch_option_table aarch64_archs[] = {
7786 {"all", AARCH64_ANY},
5a1ad39d 7787 {"armv8-a", AARCH64_ARCH_V8},
88f0ea34 7788 {"armv8.1-a", AARCH64_ARCH_V8_1},
acb787b0 7789 {"armv8.2-a", AARCH64_ARCH_V8_2},
a06ea964
NC
7790 {NULL, AARCH64_ARCH_NONE}
7791};
7792
7793/* ISA extensions. */
7794struct aarch64_option_cpu_value_table
7795{
e0471c16 7796 const char *name;
a06ea964 7797 const aarch64_feature_set value;
93d8990c 7798 const aarch64_feature_set require; /* Feature dependencies. */
a06ea964
NC
7799};
7800
7801static const struct aarch64_option_cpu_value_table aarch64_features[] = {
93d8990c
SN
7802 {"crc", AARCH64_FEATURE (AARCH64_FEATURE_CRC, 0),
7803 AARCH64_ARCH_NONE},
7804 {"crypto", AARCH64_FEATURE (AARCH64_FEATURE_CRYPTO, 0),
7805 AARCH64_ARCH_NONE},
7806 {"fp", AARCH64_FEATURE (AARCH64_FEATURE_FP, 0),
7807 AARCH64_ARCH_NONE},
7808 {"lse", AARCH64_FEATURE (AARCH64_FEATURE_LSE, 0),
7809 AARCH64_ARCH_NONE},
7810 {"simd", AARCH64_FEATURE (AARCH64_FEATURE_SIMD, 0),
7811 AARCH64_ARCH_NONE},
7812 {"pan", AARCH64_FEATURE (AARCH64_FEATURE_PAN, 0),
7813 AARCH64_ARCH_NONE},
7814 {"lor", AARCH64_FEATURE (AARCH64_FEATURE_LOR, 0),
7815 AARCH64_ARCH_NONE},
7816 {"ras", AARCH64_FEATURE (AARCH64_FEATURE_RAS, 0),
7817 AARCH64_ARCH_NONE},
7818 {"rdma", AARCH64_FEATURE (AARCH64_FEATURE_RDMA, 0),
7819 AARCH64_FEATURE (AARCH64_FEATURE_SIMD, 0)},
7820 {"fp16", AARCH64_FEATURE (AARCH64_FEATURE_F16, 0),
7821 AARCH64_FEATURE (AARCH64_FEATURE_FP, 0)},
7822 {"profile", AARCH64_FEATURE (AARCH64_FEATURE_PROFILE, 0),
7823 AARCH64_ARCH_NONE},
7824 {NULL, AARCH64_ARCH_NONE, AARCH64_ARCH_NONE},
a06ea964
NC
7825};
7826
7827struct aarch64_long_option_table
7828{
e0471c16
TS
7829 const char *option; /* Substring to match. */
7830 const char *help; /* Help information. */
17b9d67d 7831 int (*func) (const char *subopt); /* Function to decode sub-option. */
a06ea964
NC
7832 char *deprecated; /* If non-null, print this message. */
7833};
7834
93d8990c
SN
7835/* Transitive closure of features depending on set. */
7836static aarch64_feature_set
7837aarch64_feature_disable_set (aarch64_feature_set set)
7838{
7839 const struct aarch64_option_cpu_value_table *opt;
7840 aarch64_feature_set prev = 0;
7841
7842 while (prev != set) {
7843 prev = set;
7844 for (opt = aarch64_features; opt->name != NULL; opt++)
7845 if (AARCH64_CPU_HAS_ANY_FEATURES (opt->require, set))
7846 AARCH64_MERGE_FEATURE_SETS (set, set, opt->value);
7847 }
7848 return set;
7849}
7850
7851/* Transitive closure of dependencies of set. */
7852static aarch64_feature_set
7853aarch64_feature_enable_set (aarch64_feature_set set)
7854{
7855 const struct aarch64_option_cpu_value_table *opt;
7856 aarch64_feature_set prev = 0;
7857
7858 while (prev != set) {
7859 prev = set;
7860 for (opt = aarch64_features; opt->name != NULL; opt++)
7861 if (AARCH64_CPU_HAS_FEATURE (set, opt->value))
7862 AARCH64_MERGE_FEATURE_SETS (set, set, opt->require);
7863 }
7864 return set;
7865}
7866
a06ea964 7867static int
82b8a785 7868aarch64_parse_features (const char *str, const aarch64_feature_set **opt_p,
ae527cd8 7869 bfd_boolean ext_only)
a06ea964
NC
7870{
7871 /* We insist on extensions being added before being removed. We achieve
7872 this by using the ADDING_VALUE variable to indicate whether we are
7873 adding an extension (1) or removing it (0) and only allowing it to
7874 change in the order -1 -> 1 -> 0. */
7875 int adding_value = -1;
325801bd 7876 aarch64_feature_set *ext_set = XNEW (aarch64_feature_set);
a06ea964
NC
7877
7878 /* Copy the feature set, so that we can modify it. */
7879 *ext_set = **opt_p;
7880 *opt_p = ext_set;
7881
7882 while (str != NULL && *str != 0)
7883 {
7884 const struct aarch64_option_cpu_value_table *opt;
82b8a785 7885 const char *ext = NULL;
a06ea964
NC
7886 int optlen;
7887
ae527cd8 7888 if (!ext_only)
a06ea964 7889 {
ae527cd8
JB
7890 if (*str != '+')
7891 {
7892 as_bad (_("invalid architectural extension"));
7893 return 0;
7894 }
a06ea964 7895
ae527cd8
JB
7896 ext = strchr (++str, '+');
7897 }
a06ea964
NC
7898
7899 if (ext != NULL)
7900 optlen = ext - str;
7901 else
7902 optlen = strlen (str);
7903
7904 if (optlen >= 2 && strncmp (str, "no", 2) == 0)
7905 {
7906 if (adding_value != 0)
7907 adding_value = 0;
7908 optlen -= 2;
7909 str += 2;
7910 }
7911 else if (optlen > 0)
7912 {
7913 if (adding_value == -1)
7914 adding_value = 1;
7915 else if (adding_value != 1)
7916 {
7917 as_bad (_("must specify extensions to add before specifying "
7918 "those to remove"));
7919 return FALSE;
7920 }
7921 }
7922
7923 if (optlen == 0)
7924 {
7925 as_bad (_("missing architectural extension"));
7926 return 0;
7927 }
7928
7929 gas_assert (adding_value != -1);
7930
7931 for (opt = aarch64_features; opt->name != NULL; opt++)
7932 if (strncmp (opt->name, str, optlen) == 0)
7933 {
93d8990c
SN
7934 aarch64_feature_set set;
7935
a06ea964
NC
7936 /* Add or remove the extension. */
7937 if (adding_value)
93d8990c
SN
7938 {
7939 set = aarch64_feature_enable_set (opt->value);
7940 AARCH64_MERGE_FEATURE_SETS (*ext_set, *ext_set, set);
7941 }
a06ea964 7942 else
93d8990c
SN
7943 {
7944 set = aarch64_feature_disable_set (opt->value);
7945 AARCH64_CLEAR_FEATURE (*ext_set, *ext_set, set);
7946 }
a06ea964
NC
7947 break;
7948 }
7949
7950 if (opt->name == NULL)
7951 {
7952 as_bad (_("unknown architectural extension `%s'"), str);
7953 return 0;
7954 }
7955
7956 str = ext;
7957 };
7958
7959 return 1;
7960}
7961
7962static int
17b9d67d 7963aarch64_parse_cpu (const char *str)
a06ea964
NC
7964{
7965 const struct aarch64_cpu_option_table *opt;
82b8a785 7966 const char *ext = strchr (str, '+');
a06ea964
NC
7967 size_t optlen;
7968
7969 if (ext != NULL)
7970 optlen = ext - str;
7971 else
7972 optlen = strlen (str);
7973
7974 if (optlen == 0)
7975 {
7976 as_bad (_("missing cpu name `%s'"), str);
7977 return 0;
7978 }
7979
7980 for (opt = aarch64_cpus; opt->name != NULL; opt++)
7981 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
7982 {
7983 mcpu_cpu_opt = &opt->value;
7984 if (ext != NULL)
ae527cd8 7985 return aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE);
a06ea964
NC
7986
7987 return 1;
7988 }
7989
7990 as_bad (_("unknown cpu `%s'"), str);
7991 return 0;
7992}
7993
7994static int
17b9d67d 7995aarch64_parse_arch (const char *str)
a06ea964
NC
7996{
7997 const struct aarch64_arch_option_table *opt;
82b8a785 7998 const char *ext = strchr (str, '+');
a06ea964
NC
7999 size_t optlen;
8000
8001 if (ext != NULL)
8002 optlen = ext - str;
8003 else
8004 optlen = strlen (str);
8005
8006 if (optlen == 0)
8007 {
8008 as_bad (_("missing architecture name `%s'"), str);
8009 return 0;
8010 }
8011
8012 for (opt = aarch64_archs; opt->name != NULL; opt++)
8013 if (strlen (opt->name) == optlen && strncmp (str, opt->name, optlen) == 0)
8014 {
8015 march_cpu_opt = &opt->value;
8016 if (ext != NULL)
ae527cd8 8017 return aarch64_parse_features (ext, &march_cpu_opt, FALSE);
a06ea964
NC
8018
8019 return 1;
8020 }
8021
8022 as_bad (_("unknown architecture `%s'\n"), str);
8023 return 0;
8024}
8025
69091a2c
YZ
8026/* ABIs. */
8027struct aarch64_option_abi_value_table
8028{
e0471c16 8029 const char *name;
69091a2c
YZ
8030 enum aarch64_abi_type value;
8031};
8032
8033static const struct aarch64_option_abi_value_table aarch64_abis[] = {
8034 {"ilp32", AARCH64_ABI_ILP32},
8035 {"lp64", AARCH64_ABI_LP64},
69091a2c
YZ
8036};
8037
8038static int
17b9d67d 8039aarch64_parse_abi (const char *str)
69091a2c 8040{
5703197e 8041 unsigned int i;
69091a2c 8042
5703197e 8043 if (str[0] == '\0')
69091a2c
YZ
8044 {
8045 as_bad (_("missing abi name `%s'"), str);
8046 return 0;
8047 }
8048
5703197e
TS
8049 for (i = 0; i < ARRAY_SIZE (aarch64_abis); i++)
8050 if (strcmp (str, aarch64_abis[i].name) == 0)
69091a2c 8051 {
5703197e 8052 aarch64_abi = aarch64_abis[i].value;
69091a2c
YZ
8053 return 1;
8054 }
8055
8056 as_bad (_("unknown abi `%s'\n"), str);
8057 return 0;
8058}
8059
a06ea964 8060static struct aarch64_long_option_table aarch64_long_opts[] = {
69091a2c
YZ
8061#ifdef OBJ_ELF
8062 {"mabi=", N_("<abi name>\t specify for ABI <abi name>"),
8063 aarch64_parse_abi, NULL},
8064#endif /* OBJ_ELF */
a06ea964
NC
8065 {"mcpu=", N_("<cpu name>\t assemble for CPU <cpu name>"),
8066 aarch64_parse_cpu, NULL},
8067 {"march=", N_("<arch name>\t assemble for architecture <arch name>"),
8068 aarch64_parse_arch, NULL},
8069 {NULL, NULL, 0, NULL}
8070};
8071
8072int
17b9d67d 8073md_parse_option (int c, const char *arg)
a06ea964
NC
8074{
8075 struct aarch64_option_table *opt;
8076 struct aarch64_long_option_table *lopt;
8077
8078 switch (c)
8079 {
8080#ifdef OPTION_EB
8081 case OPTION_EB:
8082 target_big_endian = 1;
8083 break;
8084#endif
8085
8086#ifdef OPTION_EL
8087 case OPTION_EL:
8088 target_big_endian = 0;
8089 break;
8090#endif
8091
8092 case 'a':
8093 /* Listing option. Just ignore these, we don't support additional
8094 ones. */
8095 return 0;
8096
8097 default:
8098 for (opt = aarch64_opts; opt->option != NULL; opt++)
8099 {
8100 if (c == opt->option[0]
8101 && ((arg == NULL && opt->option[1] == 0)
8102 || streq (arg, opt->option + 1)))
8103 {
8104 /* If the option is deprecated, tell the user. */
8105 if (opt->deprecated != NULL)
8106 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
8107 arg ? arg : "", _(opt->deprecated));
8108
8109 if (opt->var != NULL)
8110 *opt->var = opt->value;
8111
8112 return 1;
8113 }
8114 }
8115
8116 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
8117 {
8118 /* These options are expected to have an argument. */
8119 if (c == lopt->option[0]
8120 && arg != NULL
8121 && strncmp (arg, lopt->option + 1,
8122 strlen (lopt->option + 1)) == 0)
8123 {
8124 /* If the option is deprecated, tell the user. */
8125 if (lopt->deprecated != NULL)
8126 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
8127 _(lopt->deprecated));
8128
8129 /* Call the sup-option parser. */
8130 return lopt->func (arg + strlen (lopt->option) - 1);
8131 }
8132 }
8133
8134 return 0;
8135 }
8136
8137 return 1;
8138}
8139
8140void
8141md_show_usage (FILE * fp)
8142{
8143 struct aarch64_option_table *opt;
8144 struct aarch64_long_option_table *lopt;
8145
8146 fprintf (fp, _(" AArch64-specific assembler options:\n"));
8147
8148 for (opt = aarch64_opts; opt->option != NULL; opt++)
8149 if (opt->help != NULL)
8150 fprintf (fp, " -%-23s%s\n", opt->option, _(opt->help));
8151
8152 for (lopt = aarch64_long_opts; lopt->option != NULL; lopt++)
8153 if (lopt->help != NULL)
8154 fprintf (fp, " -%s%s\n", lopt->option, _(lopt->help));
8155
8156#ifdef OPTION_EB
8157 fprintf (fp, _("\
8158 -EB assemble code for a big-endian cpu\n"));
8159#endif
8160
8161#ifdef OPTION_EL
8162 fprintf (fp, _("\
8163 -EL assemble code for a little-endian cpu\n"));
8164#endif
8165}
8166
8167/* Parse a .cpu directive. */
8168
8169static void
8170s_aarch64_cpu (int ignored ATTRIBUTE_UNUSED)
8171{
8172 const struct aarch64_cpu_option_table *opt;
8173 char saved_char;
8174 char *name;
8175 char *ext;
8176 size_t optlen;
8177
8178 name = input_line_pointer;
8179 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8180 input_line_pointer++;
8181 saved_char = *input_line_pointer;
8182 *input_line_pointer = 0;
8183
8184 ext = strchr (name, '+');
8185
8186 if (ext != NULL)
8187 optlen = ext - name;
8188 else
8189 optlen = strlen (name);
8190
8191 /* Skip the first "all" entry. */
8192 for (opt = aarch64_cpus + 1; opt->name != NULL; opt++)
8193 if (strlen (opt->name) == optlen
8194 && strncmp (name, opt->name, optlen) == 0)
8195 {
8196 mcpu_cpu_opt = &opt->value;
8197 if (ext != NULL)
ae527cd8 8198 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE))
a06ea964
NC
8199 return;
8200
8201 cpu_variant = *mcpu_cpu_opt;
8202
8203 *input_line_pointer = saved_char;
8204 demand_empty_rest_of_line ();
8205 return;
8206 }
8207 as_bad (_("unknown cpu `%s'"), name);
8208 *input_line_pointer = saved_char;
8209 ignore_rest_of_line ();
8210}
8211
8212
8213/* Parse a .arch directive. */
8214
8215static void
8216s_aarch64_arch (int ignored ATTRIBUTE_UNUSED)
8217{
8218 const struct aarch64_arch_option_table *opt;
8219 char saved_char;
8220 char *name;
8221 char *ext;
8222 size_t optlen;
8223
8224 name = input_line_pointer;
8225 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8226 input_line_pointer++;
8227 saved_char = *input_line_pointer;
8228 *input_line_pointer = 0;
8229
8230 ext = strchr (name, '+');
8231
8232 if (ext != NULL)
8233 optlen = ext - name;
8234 else
8235 optlen = strlen (name);
8236
8237 /* Skip the first "all" entry. */
8238 for (opt = aarch64_archs + 1; opt->name != NULL; opt++)
8239 if (strlen (opt->name) == optlen
8240 && strncmp (name, opt->name, optlen) == 0)
8241 {
8242 mcpu_cpu_opt = &opt->value;
8243 if (ext != NULL)
ae527cd8 8244 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, FALSE))
a06ea964
NC
8245 return;
8246
8247 cpu_variant = *mcpu_cpu_opt;
8248
8249 *input_line_pointer = saved_char;
8250 demand_empty_rest_of_line ();
8251 return;
8252 }
8253
8254 as_bad (_("unknown architecture `%s'\n"), name);
8255 *input_line_pointer = saved_char;
8256 ignore_rest_of_line ();
8257}
8258
ae527cd8
JB
8259/* Parse a .arch_extension directive. */
8260
8261static void
8262s_aarch64_arch_extension (int ignored ATTRIBUTE_UNUSED)
8263{
8264 char saved_char;
8265 char *ext = input_line_pointer;;
8266
8267 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
8268 input_line_pointer++;
8269 saved_char = *input_line_pointer;
8270 *input_line_pointer = 0;
8271
8272 if (!aarch64_parse_features (ext, &mcpu_cpu_opt, TRUE))
8273 return;
8274
8275 cpu_variant = *mcpu_cpu_opt;
8276
8277 *input_line_pointer = saved_char;
8278 demand_empty_rest_of_line ();
8279}
8280
a06ea964
NC
8281/* Copy symbol information. */
8282
8283void
8284aarch64_copy_symbol_attributes (symbolS * dest, symbolS * src)
8285{
8286 AARCH64_GET_FLAG (dest) = AARCH64_GET_FLAG (src);
8287}
This page took 0.621489 seconds and 4 git commands to generate.