Add generic and ARM specific support for half-precision IEEE 754 floating point numbe...
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright (C) 1994-2019 Free Software Foundation, Inc.
3    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
4         Modified by David Taylor (dtaylor@armltd.co.uk)
5         Cirrus coprocessor mods by Aldy Hernandez (aldyh@redhat.com)
6         Cirrus coprocessor fixes by Petko Manolov (petkan@nucleusys.com)
7         Cirrus coprocessor fixes by Vladimir Ivanov (vladitx@nucleusys.com)
8
9    This file is part of GAS, the GNU Assembler.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24    02110-1301, USA.  */
25
26 #include "as.h"
27 #include <limits.h>
28 #include <stdarg.h>
29 #define  NO_RELOC 0
30 #include "safe-ctype.h"
31 #include "subsegs.h"
32 #include "obstack.h"
33 #include "libiberty.h"
34 #include "opcode/arm.h"
35
36 #ifdef OBJ_ELF
37 #include "elf/arm.h"
38 #include "dw2gencfi.h"
39 #endif
40
41 #include "dwarf2dbg.h"
42
43 #ifdef OBJ_ELF
44 /* Must be at least the size of the largest unwind opcode (currently two).  */
45 #define ARM_OPCODE_CHUNK_SIZE 8
46
47 /* This structure holds the unwinding state.  */
48
49 static struct
50 {
51   symbolS *       proc_start;
52   symbolS *       table_entry;
53   symbolS *       personality_routine;
54   int             personality_index;
55   /* The segment containing the function.  */
56   segT            saved_seg;
57   subsegT         saved_subseg;
58   /* Opcodes generated from this function.  */
59   unsigned char * opcodes;
60   int             opcode_count;
61   int             opcode_alloc;
62   /* The number of bytes pushed to the stack.  */
63   offsetT         frame_size;
64   /* We don't add stack adjustment opcodes immediately so that we can merge
65      multiple adjustments.  We can also omit the final adjustment
66      when using a frame pointer.  */
67   offsetT         pending_offset;
68   /* These two fields are set by both unwind_movsp and unwind_setfp.  They
69      hold the reg+offset to use when restoring sp from a frame pointer.  */
70   offsetT         fp_offset;
71   int             fp_reg;
72   /* Nonzero if an unwind_setfp directive has been seen.  */
73   unsigned        fp_used:1;
74   /* Nonzero if the last opcode restores sp from fp_reg.  */
75   unsigned        sp_restored:1;
76 } unwind;
77
78 /* Whether --fdpic was given.  */
79 static int arm_fdpic;
80
81 #endif /* OBJ_ELF */
82
83 /* Results from operand parsing worker functions.  */
84
85 typedef enum
86 {
87   PARSE_OPERAND_SUCCESS,
88   PARSE_OPERAND_FAIL,
89   PARSE_OPERAND_FAIL_NO_BACKTRACK
90 } parse_operand_result;
91
92 enum arm_float_abi
93 {
94   ARM_FLOAT_ABI_HARD,
95   ARM_FLOAT_ABI_SOFTFP,
96   ARM_FLOAT_ABI_SOFT
97 };
98
99 /* Types of processor to assemble for.  */
100 #ifndef CPU_DEFAULT
101 /* The code that was here used to select a default CPU depending on compiler
102    pre-defines which were only present when doing native builds, thus
103    changing gas' default behaviour depending upon the build host.
104
105    If you have a target that requires a default CPU option then the you
106    should define CPU_DEFAULT here.  */
107 #endif
108
109 #ifndef FPU_DEFAULT
110 # ifdef TE_LINUX
111 #  define FPU_DEFAULT FPU_ARCH_FPA
112 # elif defined (TE_NetBSD)
113 #  ifdef OBJ_ELF
114 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
115 #  else
116     /* Legacy a.out format.  */
117 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
118 #  endif
119 # elif defined (TE_VXWORKS)
120 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
121 # else
122    /* For backwards compatibility, default to FPA.  */
123 #  define FPU_DEFAULT FPU_ARCH_FPA
124 # endif
125 #endif /* ifndef FPU_DEFAULT */
126
127 #define streq(a, b)           (strcmp (a, b) == 0)
128
129 /* Current set of feature bits available (CPU+FPU).  Different from
130    selected_cpu + selected_fpu in case of autodetection since the CPU
131    feature bits are then all set.  */
132 static arm_feature_set cpu_variant;
133 /* Feature bits used in each execution state.  Used to set build attribute
134    (in particular Tag_*_ISA_use) in CPU autodetection mode.  */
135 static arm_feature_set arm_arch_used;
136 static arm_feature_set thumb_arch_used;
137
138 /* Flags stored in private area of BFD structure.  */
139 static int uses_apcs_26      = FALSE;
140 static int atpcs             = FALSE;
141 static int support_interwork = FALSE;
142 static int uses_apcs_float   = FALSE;
143 static int pic_code          = FALSE;
144 static int fix_v4bx          = FALSE;
145 /* Warn on using deprecated features.  */
146 static int warn_on_deprecated = TRUE;
147
148 /* Understand CodeComposer Studio assembly syntax.  */
149 bfd_boolean codecomposer_syntax = FALSE;
150
151 /* Variables that we set while parsing command-line options.  Once all
152    options have been read we re-process these values to set the real
153    assembly flags.  */
154
155 /* CPU and FPU feature bits set for legacy CPU and FPU options (eg. -marm1
156    instead of -mcpu=arm1).  */
157 static const arm_feature_set *legacy_cpu = NULL;
158 static const arm_feature_set *legacy_fpu = NULL;
159
160 /* CPU, extension and FPU feature bits selected by -mcpu.  */
161 static const arm_feature_set *mcpu_cpu_opt = NULL;
162 static arm_feature_set *mcpu_ext_opt = NULL;
163 static const arm_feature_set *mcpu_fpu_opt = NULL;
164
165 /* CPU, extension and FPU feature bits selected by -march.  */
166 static const arm_feature_set *march_cpu_opt = NULL;
167 static arm_feature_set *march_ext_opt = NULL;
168 static const arm_feature_set *march_fpu_opt = NULL;
169
170 /* Feature bits selected by -mfpu.  */
171 static const arm_feature_set *mfpu_opt = NULL;
172
173 /* Constants for known architecture features.  */
174 static const arm_feature_set fpu_default = FPU_DEFAULT;
175 static const arm_feature_set fpu_arch_vfp_v1 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V1;
176 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
177 static const arm_feature_set fpu_arch_vfp_v3 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V3;
178 static const arm_feature_set fpu_arch_neon_v1 ATTRIBUTE_UNUSED = FPU_ARCH_NEON_V1;
179 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
180 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
181 #ifdef OBJ_ELF
182 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
183 #endif
184 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
185
186 #ifdef CPU_DEFAULT
187 static const arm_feature_set cpu_default = CPU_DEFAULT;
188 #endif
189
190 static const arm_feature_set arm_ext_v1 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
191 static const arm_feature_set arm_ext_v2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V2);
192 static const arm_feature_set arm_ext_v2s = ARM_FEATURE_CORE_LOW (ARM_EXT_V2S);
193 static const arm_feature_set arm_ext_v3 = ARM_FEATURE_CORE_LOW (ARM_EXT_V3);
194 static const arm_feature_set arm_ext_v3m = ARM_FEATURE_CORE_LOW (ARM_EXT_V3M);
195 static const arm_feature_set arm_ext_v4 = ARM_FEATURE_CORE_LOW (ARM_EXT_V4);
196 static const arm_feature_set arm_ext_v4t = ARM_FEATURE_CORE_LOW (ARM_EXT_V4T);
197 static const arm_feature_set arm_ext_v5 = ARM_FEATURE_CORE_LOW (ARM_EXT_V5);
198 static const arm_feature_set arm_ext_v4t_5 =
199   ARM_FEATURE_CORE_LOW (ARM_EXT_V4T | ARM_EXT_V5);
200 static const arm_feature_set arm_ext_v5t = ARM_FEATURE_CORE_LOW (ARM_EXT_V5T);
201 static const arm_feature_set arm_ext_v5e = ARM_FEATURE_CORE_LOW (ARM_EXT_V5E);
202 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP);
203 static const arm_feature_set arm_ext_v5j = ARM_FEATURE_CORE_LOW (ARM_EXT_V5J);
204 static const arm_feature_set arm_ext_v6 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6);
205 static const arm_feature_set arm_ext_v6k = ARM_FEATURE_CORE_LOW (ARM_EXT_V6K);
206 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6T2);
207 /* Only for compatability of hint instructions.  */
208 static const arm_feature_set arm_ext_v6k_v6t2 =
209   ARM_FEATURE_CORE_LOW (ARM_EXT_V6K | ARM_EXT_V6T2);
210 static const arm_feature_set arm_ext_v6_notm =
211   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_NOTM);
212 static const arm_feature_set arm_ext_v6_dsp =
213   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_DSP);
214 static const arm_feature_set arm_ext_barrier =
215   ARM_FEATURE_CORE_LOW (ARM_EXT_BARRIER);
216 static const arm_feature_set arm_ext_msr =
217   ARM_FEATURE_CORE_LOW (ARM_EXT_THUMB_MSR);
218 static const arm_feature_set arm_ext_div = ARM_FEATURE_CORE_LOW (ARM_EXT_DIV);
219 static const arm_feature_set arm_ext_v7 = ARM_FEATURE_CORE_LOW (ARM_EXT_V7);
220 static const arm_feature_set arm_ext_v7a = ARM_FEATURE_CORE_LOW (ARM_EXT_V7A);
221 static const arm_feature_set arm_ext_v7r = ARM_FEATURE_CORE_LOW (ARM_EXT_V7R);
222 #ifdef OBJ_ELF
223 static const arm_feature_set ATTRIBUTE_UNUSED arm_ext_v7m = ARM_FEATURE_CORE_LOW (ARM_EXT_V7M);
224 #endif
225 static const arm_feature_set arm_ext_v8 = ARM_FEATURE_CORE_LOW (ARM_EXT_V8);
226 static const arm_feature_set arm_ext_m =
227   ARM_FEATURE_CORE (ARM_EXT_V6M | ARM_EXT_V7M,
228                     ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
229 static const arm_feature_set arm_ext_mp = ARM_FEATURE_CORE_LOW (ARM_EXT_MP);
230 static const arm_feature_set arm_ext_sec = ARM_FEATURE_CORE_LOW (ARM_EXT_SEC);
231 static const arm_feature_set arm_ext_os = ARM_FEATURE_CORE_LOW (ARM_EXT_OS);
232 static const arm_feature_set arm_ext_adiv = ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV);
233 static const arm_feature_set arm_ext_virt = ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT);
234 static const arm_feature_set arm_ext_pan = ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN);
235 static const arm_feature_set arm_ext_v8m = ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M);
236 static const arm_feature_set arm_ext_v8m_main =
237   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M_MAIN);
238 static const arm_feature_set arm_ext_v8_1m_main =
239 ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_1M_MAIN);
240 /* Instructions in ARMv8-M only found in M profile architectures.  */
241 static const arm_feature_set arm_ext_v8m_m_only =
242   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
243 static const arm_feature_set arm_ext_v6t2_v8m =
244   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V6T2_V8M);
245 /* Instructions shared between ARMv8-A and ARMv8-M.  */
246 static const arm_feature_set arm_ext_atomics =
247   ARM_FEATURE_CORE_HIGH (ARM_EXT2_ATOMICS);
248 #ifdef OBJ_ELF
249 /* DSP instructions Tag_DSP_extension refers to.  */
250 static const arm_feature_set arm_ext_dsp =
251   ARM_FEATURE_CORE_LOW (ARM_EXT_V5E | ARM_EXT_V5ExP | ARM_EXT_V6_DSP);
252 #endif
253 static const arm_feature_set arm_ext_ras =
254   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS);
255 /* FP16 instructions.  */
256 static const arm_feature_set arm_ext_fp16 =
257   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST);
258 static const arm_feature_set arm_ext_fp16_fml =
259   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_FML);
260 static const arm_feature_set arm_ext_v8_2 =
261   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_2A);
262 static const arm_feature_set arm_ext_v8_3 =
263   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_3A);
264 static const arm_feature_set arm_ext_sb =
265   ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB);
266 static const arm_feature_set arm_ext_predres =
267   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES);
268
269 static const arm_feature_set arm_arch_any = ARM_ANY;
270 #ifdef OBJ_ELF
271 static const arm_feature_set fpu_any = FPU_ANY;
272 #endif
273 static const arm_feature_set arm_arch_full ATTRIBUTE_UNUSED = ARM_FEATURE (-1, -1, -1);
274 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
275 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
276
277 static const arm_feature_set arm_cext_iwmmxt2 =
278   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2);
279 static const arm_feature_set arm_cext_iwmmxt =
280   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT);
281 static const arm_feature_set arm_cext_xscale =
282   ARM_FEATURE_COPROC (ARM_CEXT_XSCALE);
283 static const arm_feature_set arm_cext_maverick =
284   ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK);
285 static const arm_feature_set fpu_fpa_ext_v1 =
286   ARM_FEATURE_COPROC (FPU_FPA_EXT_V1);
287 static const arm_feature_set fpu_fpa_ext_v2 =
288   ARM_FEATURE_COPROC (FPU_FPA_EXT_V2);
289 static const arm_feature_set fpu_vfp_ext_v1xd =
290   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1xD);
291 static const arm_feature_set fpu_vfp_ext_v1 =
292   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1);
293 static const arm_feature_set fpu_vfp_ext_v2 =
294   ARM_FEATURE_COPROC (FPU_VFP_EXT_V2);
295 static const arm_feature_set fpu_vfp_ext_v3xd =
296   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3xD);
297 static const arm_feature_set fpu_vfp_ext_v3 =
298   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3);
299 static const arm_feature_set fpu_vfp_ext_d32 =
300   ARM_FEATURE_COPROC (FPU_VFP_EXT_D32);
301 static const arm_feature_set fpu_neon_ext_v1 =
302   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1);
303 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
304   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
305 static const arm_feature_set mve_ext =
306   ARM_FEATURE_COPROC (FPU_MVE);
307 static const arm_feature_set mve_fp_ext =
308   ARM_FEATURE_COPROC (FPU_MVE_FP);
309 #ifdef OBJ_ELF
310 static const arm_feature_set fpu_vfp_fp16 =
311   ARM_FEATURE_COPROC (FPU_VFP_EXT_FP16);
312 static const arm_feature_set fpu_neon_ext_fma =
313   ARM_FEATURE_COPROC (FPU_NEON_EXT_FMA);
314 #endif
315 static const arm_feature_set fpu_vfp_ext_fma =
316   ARM_FEATURE_COPROC (FPU_VFP_EXT_FMA);
317 static const arm_feature_set fpu_vfp_ext_armv8 =
318   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8);
319 static const arm_feature_set fpu_vfp_ext_armv8xd =
320   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8xD);
321 static const arm_feature_set fpu_neon_ext_armv8 =
322   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8);
323 static const arm_feature_set fpu_crypto_ext_armv8 =
324   ARM_FEATURE_COPROC (FPU_CRYPTO_EXT_ARMV8);
325 static const arm_feature_set crc_ext_armv8 =
326   ARM_FEATURE_COPROC (CRC_EXT_ARMV8);
327 static const arm_feature_set fpu_neon_ext_v8_1 =
328   ARM_FEATURE_COPROC (FPU_NEON_EXT_RDMA);
329 static const arm_feature_set fpu_neon_ext_dotprod =
330   ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD);
331
332 static int mfloat_abi_opt = -1;
333 /* Architecture feature bits selected by the last -mcpu/-march or .cpu/.arch
334    directive.  */
335 static arm_feature_set selected_arch = ARM_ARCH_NONE;
336 /* Extension feature bits selected by the last -mcpu/-march or .arch_extension
337    directive.  */
338 static arm_feature_set selected_ext = ARM_ARCH_NONE;
339 /* Feature bits selected by the last -mcpu/-march or by the combination of the
340    last .cpu/.arch directive .arch_extension directives since that
341    directive.  */
342 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
343 /* FPU feature bits selected by the last -mfpu or .fpu directive.  */
344 static arm_feature_set selected_fpu = FPU_NONE;
345 /* Feature bits selected by the last .object_arch directive.  */
346 static arm_feature_set selected_object_arch = ARM_ARCH_NONE;
347 /* Must be long enough to hold any of the names in arm_cpus.  */
348 static char selected_cpu_name[20];
349
350 extern FLONUM_TYPE generic_floating_point_number;
351
352 /* Return if no cpu was selected on command-line.  */
353 static bfd_boolean
354 no_cpu_selected (void)
355 {
356   return ARM_FEATURE_EQUAL (selected_cpu, arm_arch_none);
357 }
358
359 #ifdef OBJ_ELF
360 # ifdef EABI_DEFAULT
361 static int meabi_flags = EABI_DEFAULT;
362 # else
363 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
364 # endif
365
366 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
367
368 bfd_boolean
369 arm_is_eabi (void)
370 {
371   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
372 }
373 #endif
374
375 #ifdef OBJ_ELF
376 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
377 symbolS * GOT_symbol;
378 #endif
379
380 /* 0: assemble for ARM,
381    1: assemble for Thumb,
382    2: assemble for Thumb even though target CPU does not support thumb
383       instructions.  */
384 static int thumb_mode = 0;
385 /* A value distinct from the possible values for thumb_mode that we
386    can use to record whether thumb_mode has been copied into the
387    tc_frag_data field of a frag.  */
388 #define MODE_RECORDED (1 << 4)
389
390 /* Specifies the intrinsic IT insn behavior mode.  */
391 enum implicit_it_mode
392 {
393   IMPLICIT_IT_MODE_NEVER  = 0x00,
394   IMPLICIT_IT_MODE_ARM    = 0x01,
395   IMPLICIT_IT_MODE_THUMB  = 0x02,
396   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
397 };
398 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
399
400 /* If unified_syntax is true, we are processing the new unified
401    ARM/Thumb syntax.  Important differences from the old ARM mode:
402
403      - Immediate operands do not require a # prefix.
404      - Conditional affixes always appear at the end of the
405        instruction.  (For backward compatibility, those instructions
406        that formerly had them in the middle, continue to accept them
407        there.)
408      - The IT instruction may appear, and if it does is validated
409        against subsequent conditional affixes.  It does not generate
410        machine code.
411
412    Important differences from the old Thumb mode:
413
414      - Immediate operands do not require a # prefix.
415      - Most of the V6T2 instructions are only available in unified mode.
416      - The .N and .W suffixes are recognized and honored (it is an error
417        if they cannot be honored).
418      - All instructions set the flags if and only if they have an 's' affix.
419      - Conditional affixes may be used.  They are validated against
420        preceding IT instructions.  Unlike ARM mode, you cannot use a
421        conditional affix except in the scope of an IT instruction.  */
422
423 static bfd_boolean unified_syntax = FALSE;
424
425 /* An immediate operand can start with #, and ld*, st*, pld operands
426    can contain [ and ].  We need to tell APP not to elide whitespace
427    before a [, which can appear as the first operand for pld.
428    Likewise, a { can appear as the first operand for push, pop, vld*, etc.  */
429 const char arm_symbol_chars[] = "#[]{}";
430
431 enum neon_el_type
432 {
433   NT_invtype,
434   NT_untyped,
435   NT_integer,
436   NT_float,
437   NT_poly,
438   NT_signed,
439   NT_unsigned
440 };
441
442 struct neon_type_el
443 {
444   enum neon_el_type type;
445   unsigned size;
446 };
447
448 #define NEON_MAX_TYPE_ELS 4
449
450 struct neon_type
451 {
452   struct neon_type_el el[NEON_MAX_TYPE_ELS];
453   unsigned elems;
454 };
455
456 enum pred_instruction_type
457 {
458    OUTSIDE_PRED_INSN,
459    INSIDE_VPT_INSN,
460    INSIDE_IT_INSN,
461    INSIDE_IT_LAST_INSN,
462    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
463                               if inside, should be the last one.  */
464    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
465                               i.e. BKPT and NOP.  */
466    IT_INSN,                /* The IT insn has been parsed.  */
467    VPT_INSN,               /* The VPT/VPST insn has been parsed.  */
468    MVE_OUTSIDE_PRED_INSN , /* Instruction to indicate a MVE instruction without
469                               a predication code.  */
470    MVE_UNPREDICABLE_INSN   /* MVE instruction that is non-predicable.  */
471 };
472
473 /* The maximum number of operands we need.  */
474 #define ARM_IT_MAX_OPERANDS 6
475 #define ARM_IT_MAX_RELOCS 3
476
477 struct arm_it
478 {
479   const char *  error;
480   unsigned long instruction;
481   int           size;
482   int           size_req;
483   int           cond;
484   /* "uncond_value" is set to the value in place of the conditional field in
485      unconditional versions of the instruction, or -1 if nothing is
486      appropriate.  */
487   int           uncond_value;
488   struct neon_type vectype;
489   /* This does not indicate an actual NEON instruction, only that
490      the mnemonic accepts neon-style type suffixes.  */
491   int           is_neon;
492   /* Set to the opcode if the instruction needs relaxation.
493      Zero if the instruction is not relaxed.  */
494   unsigned long relax;
495   struct
496   {
497     bfd_reloc_code_real_type type;
498     expressionS              exp;
499     int                      pc_rel;
500   } relocs[ARM_IT_MAX_RELOCS];
501
502   enum pred_instruction_type pred_insn_type;
503
504   struct
505   {
506     unsigned reg;
507     signed int imm;
508     struct neon_type_el vectype;
509     unsigned present    : 1;  /* Operand present.  */
510     unsigned isreg      : 1;  /* Operand was a register.  */
511     unsigned immisreg   : 2;  /* .imm field is a second register.
512                                  0: imm, 1: gpr, 2: MVE Q-register.  */
513     unsigned isscalar   : 2;  /* Operand is a (SIMD) scalar:
514                                  0) not scalar,
515                                  1) Neon scalar,
516                                  2) MVE scalar.  */
517     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
518     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
519     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
520        instructions. This allows us to disambiguate ARM <-> vector insns.  */
521     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
522     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
523     unsigned isquad     : 1;  /* Operand is SIMD quad register.  */
524     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
525     unsigned iszr       : 1;  /* Operand is ZR register.  */
526     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
527     unsigned writeback  : 1;  /* Operand has trailing !  */
528     unsigned preind     : 1;  /* Preindexed address.  */
529     unsigned postind    : 1;  /* Postindexed address.  */
530     unsigned negative   : 1;  /* Index register was negated.  */
531     unsigned shifted    : 1;  /* Shift applied to operation.  */
532     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
533   } operands[ARM_IT_MAX_OPERANDS];
534 };
535
536 static struct arm_it inst;
537
538 #define NUM_FLOAT_VALS 8
539
540 const char * fp_const[] =
541 {
542   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
543 };
544
545 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
546
547 #define FAIL    (-1)
548 #define SUCCESS (0)
549
550 #define SUFF_S 1
551 #define SUFF_D 2
552 #define SUFF_E 3
553 #define SUFF_P 4
554
555 #define CP_T_X   0x00008000
556 #define CP_T_Y   0x00400000
557
558 #define CONDS_BIT        0x00100000
559 #define LOAD_BIT         0x00100000
560
561 #define DOUBLE_LOAD_FLAG 0x00000001
562
563 struct asm_cond
564 {
565   const char *   template_name;
566   unsigned long  value;
567 };
568
569 #define COND_ALWAYS 0xE
570
571 struct asm_psr
572 {
573   const char *   template_name;
574   unsigned long  field;
575 };
576
577 struct asm_barrier_opt
578 {
579   const char *    template_name;
580   unsigned long   value;
581   const arm_feature_set arch;
582 };
583
584 /* The bit that distinguishes CPSR and SPSR.  */
585 #define SPSR_BIT   (1 << 22)
586
587 /* The individual PSR flag bits.  */
588 #define PSR_c   (1 << 16)
589 #define PSR_x   (1 << 17)
590 #define PSR_s   (1 << 18)
591 #define PSR_f   (1 << 19)
592
593 struct reloc_entry
594 {
595   const char *              name;
596   bfd_reloc_code_real_type  reloc;
597 };
598
599 enum vfp_reg_pos
600 {
601   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
602   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
603 };
604
605 enum vfp_ldstm_type
606 {
607   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
608 };
609
610 /* Bits for DEFINED field in neon_typed_alias.  */
611 #define NTA_HASTYPE  1
612 #define NTA_HASINDEX 2
613
614 struct neon_typed_alias
615 {
616   unsigned char        defined;
617   unsigned char        index;
618   struct neon_type_el  eltype;
619 };
620
621 /* ARM register categories.  This includes coprocessor numbers and various
622    architecture extensions' registers.  Each entry should have an error message
623    in reg_expected_msgs below.  */
624 enum arm_reg_type
625 {
626   REG_TYPE_RN,
627   REG_TYPE_CP,
628   REG_TYPE_CN,
629   REG_TYPE_FN,
630   REG_TYPE_VFS,
631   REG_TYPE_VFD,
632   REG_TYPE_NQ,
633   REG_TYPE_VFSD,
634   REG_TYPE_NDQ,
635   REG_TYPE_NSD,
636   REG_TYPE_NSDQ,
637   REG_TYPE_VFC,
638   REG_TYPE_MVF,
639   REG_TYPE_MVD,
640   REG_TYPE_MVFX,
641   REG_TYPE_MVDX,
642   REG_TYPE_MVAX,
643   REG_TYPE_MQ,
644   REG_TYPE_DSPSC,
645   REG_TYPE_MMXWR,
646   REG_TYPE_MMXWC,
647   REG_TYPE_MMXWCG,
648   REG_TYPE_XSCALE,
649   REG_TYPE_RNB,
650   REG_TYPE_ZR
651 };
652
653 /* Structure for a hash table entry for a register.
654    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
655    information which states whether a vector type or index is specified (for a
656    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
657 struct reg_entry
658 {
659   const char *               name;
660   unsigned int               number;
661   unsigned char              type;
662   unsigned char              builtin;
663   struct neon_typed_alias *  neon;
664 };
665
666 /* Diagnostics used when we don't get a register of the expected type.  */
667 const char * const reg_expected_msgs[] =
668 {
669   [REG_TYPE_RN]     = N_("ARM register expected"),
670   [REG_TYPE_CP]     = N_("bad or missing co-processor number"),
671   [REG_TYPE_CN]     = N_("co-processor register expected"),
672   [REG_TYPE_FN]     = N_("FPA register expected"),
673   [REG_TYPE_VFS]    = N_("VFP single precision register expected"),
674   [REG_TYPE_VFD]    = N_("VFP/Neon double precision register expected"),
675   [REG_TYPE_NQ]     = N_("Neon quad precision register expected"),
676   [REG_TYPE_VFSD]   = N_("VFP single or double precision register expected"),
677   [REG_TYPE_NDQ]    = N_("Neon double or quad precision register expected"),
678   [REG_TYPE_NSD]    = N_("Neon single or double precision register expected"),
679   [REG_TYPE_NSDQ]   = N_("VFP single, double or Neon quad precision register"
680                          " expected"),
681   [REG_TYPE_VFC]    = N_("VFP system register expected"),
682   [REG_TYPE_MVF]    = N_("Maverick MVF register expected"),
683   [REG_TYPE_MVD]    = N_("Maverick MVD register expected"),
684   [REG_TYPE_MVFX]   = N_("Maverick MVFX register expected"),
685   [REG_TYPE_MVDX]   = N_("Maverick MVDX register expected"),
686   [REG_TYPE_MVAX]   = N_("Maverick MVAX register expected"),
687   [REG_TYPE_DSPSC]  = N_("Maverick DSPSC register expected"),
688   [REG_TYPE_MMXWR]  = N_("iWMMXt data register expected"),
689   [REG_TYPE_MMXWC]  = N_("iWMMXt control register expected"),
690   [REG_TYPE_MMXWCG] = N_("iWMMXt scalar register expected"),
691   [REG_TYPE_XSCALE] = N_("XScale accumulator register expected"),
692   [REG_TYPE_MQ]     = N_("MVE vector register expected"),
693   [REG_TYPE_RNB]    = N_("")
694 };
695
696 /* Some well known registers that we refer to directly elsewhere.  */
697 #define REG_R12 12
698 #define REG_SP  13
699 #define REG_LR  14
700 #define REG_PC  15
701
702 /* ARM instructions take 4bytes in the object file, Thumb instructions
703    take 2:  */
704 #define INSN_SIZE       4
705
706 struct asm_opcode
707 {
708   /* Basic string to match.  */
709   const char * template_name;
710
711   /* Parameters to instruction.  */
712   unsigned int operands[8];
713
714   /* Conditional tag - see opcode_lookup.  */
715   unsigned int tag : 4;
716
717   /* Basic instruction code.  */
718   unsigned int avalue;
719
720   /* Thumb-format instruction code.  */
721   unsigned int tvalue;
722
723   /* Which architecture variant provides this instruction.  */
724   const arm_feature_set * avariant;
725   const arm_feature_set * tvariant;
726
727   /* Function to call to encode instruction in ARM format.  */
728   void (* aencode) (void);
729
730   /* Function to call to encode instruction in Thumb format.  */
731   void (* tencode) (void);
732
733   /* Indicates whether this instruction may be vector predicated.  */
734   unsigned int mayBeVecPred : 1;
735 };
736
737 /* Defines for various bits that we will want to toggle.  */
738 #define INST_IMMEDIATE  0x02000000
739 #define OFFSET_REG      0x02000000
740 #define HWOFFSET_IMM    0x00400000
741 #define SHIFT_BY_REG    0x00000010
742 #define PRE_INDEX       0x01000000
743 #define INDEX_UP        0x00800000
744 #define WRITE_BACK      0x00200000
745 #define LDM_TYPE_2_OR_3 0x00400000
746 #define CPSI_MMOD       0x00020000
747
748 #define LITERAL_MASK    0xf000f000
749 #define OPCODE_MASK     0xfe1fffff
750 #define V4_STR_BIT      0x00000020
751 #define VLDR_VMOV_SAME  0x0040f000
752
753 #define T2_SUBS_PC_LR   0xf3de8f00
754
755 #define DATA_OP_SHIFT   21
756 #define SBIT_SHIFT      20
757
758 #define T2_OPCODE_MASK  0xfe1fffff
759 #define T2_DATA_OP_SHIFT 21
760 #define T2_SBIT_SHIFT    20
761
762 #define A_COND_MASK         0xf0000000
763 #define A_PUSH_POP_OP_MASK  0x0fff0000
764
765 /* Opcodes for pushing/poping registers to/from the stack.  */
766 #define A1_OPCODE_PUSH    0x092d0000
767 #define A2_OPCODE_PUSH    0x052d0004
768 #define A2_OPCODE_POP     0x049d0004
769
770 /* Codes to distinguish the arithmetic instructions.  */
771 #define OPCODE_AND      0
772 #define OPCODE_EOR      1
773 #define OPCODE_SUB      2
774 #define OPCODE_RSB      3
775 #define OPCODE_ADD      4
776 #define OPCODE_ADC      5
777 #define OPCODE_SBC      6
778 #define OPCODE_RSC      7
779 #define OPCODE_TST      8
780 #define OPCODE_TEQ      9
781 #define OPCODE_CMP      10
782 #define OPCODE_CMN      11
783 #define OPCODE_ORR      12
784 #define OPCODE_MOV      13
785 #define OPCODE_BIC      14
786 #define OPCODE_MVN      15
787
788 #define T2_OPCODE_AND   0
789 #define T2_OPCODE_BIC   1
790 #define T2_OPCODE_ORR   2
791 #define T2_OPCODE_ORN   3
792 #define T2_OPCODE_EOR   4
793 #define T2_OPCODE_ADD   8
794 #define T2_OPCODE_ADC   10
795 #define T2_OPCODE_SBC   11
796 #define T2_OPCODE_SUB   13
797 #define T2_OPCODE_RSB   14
798
799 #define T_OPCODE_MUL 0x4340
800 #define T_OPCODE_TST 0x4200
801 #define T_OPCODE_CMN 0x42c0
802 #define T_OPCODE_NEG 0x4240
803 #define T_OPCODE_MVN 0x43c0
804
805 #define T_OPCODE_ADD_R3 0x1800
806 #define T_OPCODE_SUB_R3 0x1a00
807 #define T_OPCODE_ADD_HI 0x4400
808 #define T_OPCODE_ADD_ST 0xb000
809 #define T_OPCODE_SUB_ST 0xb080
810 #define T_OPCODE_ADD_SP 0xa800
811 #define T_OPCODE_ADD_PC 0xa000
812 #define T_OPCODE_ADD_I8 0x3000
813 #define T_OPCODE_SUB_I8 0x3800
814 #define T_OPCODE_ADD_I3 0x1c00
815 #define T_OPCODE_SUB_I3 0x1e00
816
817 #define T_OPCODE_ASR_R  0x4100
818 #define T_OPCODE_LSL_R  0x4080
819 #define T_OPCODE_LSR_R  0x40c0
820 #define T_OPCODE_ROR_R  0x41c0
821 #define T_OPCODE_ASR_I  0x1000
822 #define T_OPCODE_LSL_I  0x0000
823 #define T_OPCODE_LSR_I  0x0800
824
825 #define T_OPCODE_MOV_I8 0x2000
826 #define T_OPCODE_CMP_I8 0x2800
827 #define T_OPCODE_CMP_LR 0x4280
828 #define T_OPCODE_MOV_HR 0x4600
829 #define T_OPCODE_CMP_HR 0x4500
830
831 #define T_OPCODE_LDR_PC 0x4800
832 #define T_OPCODE_LDR_SP 0x9800
833 #define T_OPCODE_STR_SP 0x9000
834 #define T_OPCODE_LDR_IW 0x6800
835 #define T_OPCODE_STR_IW 0x6000
836 #define T_OPCODE_LDR_IH 0x8800
837 #define T_OPCODE_STR_IH 0x8000
838 #define T_OPCODE_LDR_IB 0x7800
839 #define T_OPCODE_STR_IB 0x7000
840 #define T_OPCODE_LDR_RW 0x5800
841 #define T_OPCODE_STR_RW 0x5000
842 #define T_OPCODE_LDR_RH 0x5a00
843 #define T_OPCODE_STR_RH 0x5200
844 #define T_OPCODE_LDR_RB 0x5c00
845 #define T_OPCODE_STR_RB 0x5400
846
847 #define T_OPCODE_PUSH   0xb400
848 #define T_OPCODE_POP    0xbc00
849
850 #define T_OPCODE_BRANCH 0xe000
851
852 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
853 #define THUMB_PP_PC_LR 0x0100
854 #define THUMB_LOAD_BIT 0x0800
855 #define THUMB2_LOAD_BIT 0x00100000
856
857 #define BAD_SYNTAX      _("syntax error")
858 #define BAD_ARGS        _("bad arguments to instruction")
859 #define BAD_SP          _("r13 not allowed here")
860 #define BAD_PC          _("r15 not allowed here")
861 #define BAD_ODD         _("Odd register not allowed here")
862 #define BAD_EVEN        _("Even register not allowed here")
863 #define BAD_COND        _("instruction cannot be conditional")
864 #define BAD_OVERLAP     _("registers may not be the same")
865 #define BAD_HIREG       _("lo register required")
866 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
867 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode")
868 #define BAD_BRANCH      _("branch must be last instruction in IT block")
869 #define BAD_BRANCH_OFF  _("branch out of range or not a multiple of 2")
870 #define BAD_NOT_IT      _("instruction not allowed in IT block")
871 #define BAD_NOT_VPT     _("instruction missing MVE vector predication code")
872 #define BAD_FPU         _("selected FPU does not support instruction")
873 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
874 #define BAD_OUT_VPT     \
875         _("vector predicated instruction should be in VPT/VPST block")
876 #define BAD_IT_COND     _("incorrect condition in IT block")
877 #define BAD_VPT_COND    _("incorrect condition in VPT/VPST block")
878 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
879 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
880 #define BAD_PC_ADDRESSING \
881         _("cannot use register index with PC-relative addressing")
882 #define BAD_PC_WRITEBACK \
883         _("cannot use writeback with PC-relative addressing")
884 #define BAD_RANGE       _("branch out of range")
885 #define BAD_FP16        _("selected processor does not support fp16 instruction")
886 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
887 #define THUMB1_RELOC_ONLY  _("relocation valid in thumb1 code only")
888 #define MVE_NOT_IT      _("Warning: instruction is UNPREDICTABLE in an IT " \
889                           "block")
890 #define MVE_NOT_VPT     _("Warning: instruction is UNPREDICTABLE in a VPT " \
891                           "block")
892 #define MVE_BAD_PC      _("Warning: instruction is UNPREDICTABLE with PC" \
893                           " operand")
894 #define MVE_BAD_SP      _("Warning: instruction is UNPREDICTABLE with SP" \
895                           " operand")
896 #define BAD_SIMD_TYPE   _("bad type in SIMD instruction")
897 #define BAD_MVE_AUTO    \
898   _("GAS auto-detection mode and -march=all is deprecated for MVE, please" \
899     " use a valid -march or -mcpu option.")
900 #define BAD_MVE_SRCDEST _("Warning: 32-bit element size and same destination "\
901                           "and source operands makes instruction UNPREDICTABLE")
902 #define BAD_EL_TYPE     _("bad element type for instruction")
903 #define MVE_BAD_QREG    _("MVE vector register Q[0..7] expected")
904
905 static struct hash_control * arm_ops_hsh;
906 static struct hash_control * arm_cond_hsh;
907 static struct hash_control * arm_vcond_hsh;
908 static struct hash_control * arm_shift_hsh;
909 static struct hash_control * arm_psr_hsh;
910 static struct hash_control * arm_v7m_psr_hsh;
911 static struct hash_control * arm_reg_hsh;
912 static struct hash_control * arm_reloc_hsh;
913 static struct hash_control * arm_barrier_opt_hsh;
914
915 /* Stuff needed to resolve the label ambiguity
916    As:
917      ...
918      label:   <insn>
919    may differ from:
920      ...
921      label:
922               <insn>  */
923
924 symbolS *  last_label_seen;
925 static int label_is_thumb_function_name = FALSE;
926
927 /* Literal pool structure.  Held on a per-section
928    and per-sub-section basis.  */
929
930 #define MAX_LITERAL_POOL_SIZE 1024
931 typedef struct literal_pool
932 {
933   expressionS            literals [MAX_LITERAL_POOL_SIZE];
934   unsigned int           next_free_entry;
935   unsigned int           id;
936   symbolS *              symbol;
937   segT                   section;
938   subsegT                sub_section;
939 #ifdef OBJ_ELF
940   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
941 #endif
942   struct literal_pool *  next;
943   unsigned int           alignment;
944 } literal_pool;
945
946 /* Pointer to a linked list of literal pools.  */
947 literal_pool * list_of_pools = NULL;
948
949 typedef enum asmfunc_states
950 {
951   OUTSIDE_ASMFUNC,
952   WAITING_ASMFUNC_NAME,
953   WAITING_ENDASMFUNC
954 } asmfunc_states;
955
956 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
957
958 #ifdef OBJ_ELF
959 #  define now_pred seg_info (now_seg)->tc_segment_info_data.current_pred
960 #else
961 static struct current_pred now_pred;
962 #endif
963
964 static inline int
965 now_pred_compatible (int cond)
966 {
967   return (cond & ~1) == (now_pred.cc & ~1);
968 }
969
970 static inline int
971 conditional_insn (void)
972 {
973   return inst.cond != COND_ALWAYS;
974 }
975
976 static int in_pred_block (void);
977
978 static int handle_pred_state (void);
979
980 static void force_automatic_it_block_close (void);
981
982 static void it_fsm_post_encode (void);
983
984 #define set_pred_insn_type(type)                        \
985   do                                            \
986     {                                           \
987       inst.pred_insn_type = type;                       \
988       if (handle_pred_state () == FAIL)         \
989         return;                                 \
990     }                                           \
991   while (0)
992
993 #define set_pred_insn_type_nonvoid(type, failret) \
994   do                                            \
995     {                                           \
996       inst.pred_insn_type = type;                       \
997       if (handle_pred_state () == FAIL)         \
998         return failret;                         \
999     }                                           \
1000   while(0)
1001
1002 #define set_pred_insn_type_last()                               \
1003   do                                                    \
1004     {                                                   \
1005       if (inst.cond == COND_ALWAYS)                     \
1006         set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);    \
1007       else                                              \
1008         set_pred_insn_type (INSIDE_IT_LAST_INSN);               \
1009     }                                                   \
1010   while (0)
1011
1012 /* Toggle value[pos].  */
1013 #define TOGGLE_BIT(value, pos) (value ^ (1 << pos))
1014
1015 /* Pure syntax.  */
1016
1017 /* This array holds the chars that always start a comment.  If the
1018    pre-processor is disabled, these aren't very useful.  */
1019 char arm_comment_chars[] = "@";
1020
1021 /* This array holds the chars that only start a comment at the beginning of
1022    a line.  If the line seems to have the form '# 123 filename'
1023    .line and .file directives will appear in the pre-processed output.  */
1024 /* Note that input_file.c hand checks for '#' at the beginning of the
1025    first line of the input file.  This is because the compiler outputs
1026    #NO_APP at the beginning of its output.  */
1027 /* Also note that comments like this one will always work.  */
1028 const char line_comment_chars[] = "#";
1029
1030 char arm_line_separator_chars[] = ";";
1031
1032 /* Chars that can be used to separate mant
1033    from exp in floating point numbers.  */
1034 const char EXP_CHARS[] = "eE";
1035
1036 /* Chars that mean this number is a floating point constant.  */
1037 /* As in 0f12.456  */
1038 /* or    0d1.2345e12  */
1039
1040 const char FLT_CHARS[] = "rRsSfFdDxXeEpPHh";
1041
1042 /* Prefix characters that indicate the start of an immediate
1043    value.  */
1044 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
1045
1046 /* Separator character handling.  */
1047
1048 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
1049
1050 enum fp_16bit_format
1051 {
1052   ARM_FP16_FORMAT_IEEE          = 0x1,
1053   ARM_FP16_FORMAT_ALTERNATIVE   = 0x2,
1054   ARM_FP16_FORMAT_DEFAULT       = 0x3
1055 };
1056
1057 static enum fp_16bit_format fp16_format = ARM_FP16_FORMAT_DEFAULT;
1058
1059
1060 static inline int
1061 skip_past_char (char ** str, char c)
1062 {
1063   /* PR gas/14987: Allow for whitespace before the expected character.  */
1064   skip_whitespace (*str);
1065
1066   if (**str == c)
1067     {
1068       (*str)++;
1069       return SUCCESS;
1070     }
1071   else
1072     return FAIL;
1073 }
1074
1075 #define skip_past_comma(str) skip_past_char (str, ',')
1076
1077 /* Arithmetic expressions (possibly involving symbols).  */
1078
1079 /* Return TRUE if anything in the expression is a bignum.  */
1080
1081 static bfd_boolean
1082 walk_no_bignums (symbolS * sp)
1083 {
1084   if (symbol_get_value_expression (sp)->X_op == O_big)
1085     return TRUE;
1086
1087   if (symbol_get_value_expression (sp)->X_add_symbol)
1088     {
1089       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
1090               || (symbol_get_value_expression (sp)->X_op_symbol
1091                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
1092     }
1093
1094   return FALSE;
1095 }
1096
1097 static bfd_boolean in_my_get_expression = FALSE;
1098
1099 /* Third argument to my_get_expression.  */
1100 #define GE_NO_PREFIX 0
1101 #define GE_IMM_PREFIX 1
1102 #define GE_OPT_PREFIX 2
1103 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
1104    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
1105 #define GE_OPT_PREFIX_BIG 3
1106
1107 static int
1108 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1109 {
1110   char * save_in;
1111
1112   /* In unified syntax, all prefixes are optional.  */
1113   if (unified_syntax)
1114     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1115                   : GE_OPT_PREFIX;
1116
1117   switch (prefix_mode)
1118     {
1119     case GE_NO_PREFIX: break;
1120     case GE_IMM_PREFIX:
1121       if (!is_immediate_prefix (**str))
1122         {
1123           inst.error = _("immediate expression requires a # prefix");
1124           return FAIL;
1125         }
1126       (*str)++;
1127       break;
1128     case GE_OPT_PREFIX:
1129     case GE_OPT_PREFIX_BIG:
1130       if (is_immediate_prefix (**str))
1131         (*str)++;
1132       break;
1133     default:
1134       abort ();
1135     }
1136
1137   memset (ep, 0, sizeof (expressionS));
1138
1139   save_in = input_line_pointer;
1140   input_line_pointer = *str;
1141   in_my_get_expression = TRUE;
1142   expression (ep);
1143   in_my_get_expression = FALSE;
1144
1145   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1146     {
1147       /* We found a bad or missing expression in md_operand().  */
1148       *str = input_line_pointer;
1149       input_line_pointer = save_in;
1150       if (inst.error == NULL)
1151         inst.error = (ep->X_op == O_absent
1152                       ? _("missing expression") :_("bad expression"));
1153       return 1;
1154     }
1155
1156   /* Get rid of any bignums now, so that we don't generate an error for which
1157      we can't establish a line number later on.  Big numbers are never valid
1158      in instructions, which is where this routine is always called.  */
1159   if (prefix_mode != GE_OPT_PREFIX_BIG
1160       && (ep->X_op == O_big
1161           || (ep->X_add_symbol
1162               && (walk_no_bignums (ep->X_add_symbol)
1163                   || (ep->X_op_symbol
1164                       && walk_no_bignums (ep->X_op_symbol))))))
1165     {
1166       inst.error = _("invalid constant");
1167       *str = input_line_pointer;
1168       input_line_pointer = save_in;
1169       return 1;
1170     }
1171
1172   *str = input_line_pointer;
1173   input_line_pointer = save_in;
1174   return SUCCESS;
1175 }
1176
1177 /* Turn a string in input_line_pointer into a floating point constant
1178    of type TYPE, and store the appropriate bytes in *LITP.  The number
1179    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1180    returned, or NULL on OK.
1181
1182    Note that fp constants aren't represent in the normal way on the ARM.
1183    In big endian mode, things are as expected.  However, in little endian
1184    mode fp constants are big-endian word-wise, and little-endian byte-wise
1185    within the words.  For example, (double) 1.1 in big endian mode is
1186    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1187    the byte sequence 99 99 f1 3f 9a 99 99 99.
1188
1189    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1190
1191 const char *
1192 md_atof (int type, char * litP, int * sizeP)
1193 {
1194   int prec;
1195   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1196   char *t;
1197   int i;
1198
1199   switch (type)
1200     {
1201     case 'H':
1202     case 'h':
1203       prec = 1;
1204       break;
1205
1206     case 'f':
1207     case 'F':
1208     case 's':
1209     case 'S':
1210       prec = 2;
1211       break;
1212
1213     case 'd':
1214     case 'D':
1215     case 'r':
1216     case 'R':
1217       prec = 4;
1218       break;
1219
1220     case 'x':
1221     case 'X':
1222       prec = 5;
1223       break;
1224
1225     case 'p':
1226     case 'P':
1227       prec = 5;
1228       break;
1229
1230     default:
1231       *sizeP = 0;
1232       return _("Unrecognized or unsupported floating point constant");
1233     }
1234
1235   t = atof_ieee (input_line_pointer, type, words);
1236   if (t)
1237     input_line_pointer = t;
1238   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1239
1240   if (target_big_endian)
1241     {
1242       for (i = 0; i < prec; i++)
1243         {
1244           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1245           litP += sizeof (LITTLENUM_TYPE);
1246         }
1247     }
1248   else
1249     {
1250       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1251         for (i = prec - 1; i >= 0; i--)
1252           {
1253             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1254             litP += sizeof (LITTLENUM_TYPE);
1255           }
1256       else
1257         /* For a 4 byte float the order of elements in `words' is 1 0.
1258            For an 8 byte float the order is 1 0 3 2.  */
1259         for (i = 0; i < prec; i += 2)
1260           {
1261             md_number_to_chars (litP, (valueT) words[i + 1],
1262                                 sizeof (LITTLENUM_TYPE));
1263             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1264                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1265             litP += 2 * sizeof (LITTLENUM_TYPE);
1266           }
1267     }
1268
1269   return NULL;
1270 }
1271
1272 /* We handle all bad expressions here, so that we can report the faulty
1273    instruction in the error message.  */
1274
1275 void
1276 md_operand (expressionS * exp)
1277 {
1278   if (in_my_get_expression)
1279     exp->X_op = O_illegal;
1280 }
1281
1282 /* Immediate values.  */
1283
1284 #ifdef OBJ_ELF
1285 /* Generic immediate-value read function for use in directives.
1286    Accepts anything that 'expression' can fold to a constant.
1287    *val receives the number.  */
1288
1289 static int
1290 immediate_for_directive (int *val)
1291 {
1292   expressionS exp;
1293   exp.X_op = O_illegal;
1294
1295   if (is_immediate_prefix (*input_line_pointer))
1296     {
1297       input_line_pointer++;
1298       expression (&exp);
1299     }
1300
1301   if (exp.X_op != O_constant)
1302     {
1303       as_bad (_("expected #constant"));
1304       ignore_rest_of_line ();
1305       return FAIL;
1306     }
1307   *val = exp.X_add_number;
1308   return SUCCESS;
1309 }
1310 #endif
1311
1312 /* Register parsing.  */
1313
1314 /* Generic register parser.  CCP points to what should be the
1315    beginning of a register name.  If it is indeed a valid register
1316    name, advance CCP over it and return the reg_entry structure;
1317    otherwise return NULL.  Does not issue diagnostics.  */
1318
1319 static struct reg_entry *
1320 arm_reg_parse_multi (char **ccp)
1321 {
1322   char *start = *ccp;
1323   char *p;
1324   struct reg_entry *reg;
1325
1326   skip_whitespace (start);
1327
1328 #ifdef REGISTER_PREFIX
1329   if (*start != REGISTER_PREFIX)
1330     return NULL;
1331   start++;
1332 #endif
1333 #ifdef OPTIONAL_REGISTER_PREFIX
1334   if (*start == OPTIONAL_REGISTER_PREFIX)
1335     start++;
1336 #endif
1337
1338   p = start;
1339   if (!ISALPHA (*p) || !is_name_beginner (*p))
1340     return NULL;
1341
1342   do
1343     p++;
1344   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1345
1346   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1347
1348   if (!reg)
1349     return NULL;
1350
1351   *ccp = p;
1352   return reg;
1353 }
1354
1355 static int
1356 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1357                     enum arm_reg_type type)
1358 {
1359   /* Alternative syntaxes are accepted for a few register classes.  */
1360   switch (type)
1361     {
1362     case REG_TYPE_MVF:
1363     case REG_TYPE_MVD:
1364     case REG_TYPE_MVFX:
1365     case REG_TYPE_MVDX:
1366       /* Generic coprocessor register names are allowed for these.  */
1367       if (reg && reg->type == REG_TYPE_CN)
1368         return reg->number;
1369       break;
1370
1371     case REG_TYPE_CP:
1372       /* For backward compatibility, a bare number is valid here.  */
1373       {
1374         unsigned long processor = strtoul (start, ccp, 10);
1375         if (*ccp != start && processor <= 15)
1376           return processor;
1377       }
1378       /* Fall through.  */
1379
1380     case REG_TYPE_MMXWC:
1381       /* WC includes WCG.  ??? I'm not sure this is true for all
1382          instructions that take WC registers.  */
1383       if (reg && reg->type == REG_TYPE_MMXWCG)
1384         return reg->number;
1385       break;
1386
1387     default:
1388       break;
1389     }
1390
1391   return FAIL;
1392 }
1393
1394 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1395    return value is the register number or FAIL.  */
1396
1397 static int
1398 arm_reg_parse (char **ccp, enum arm_reg_type type)
1399 {
1400   char *start = *ccp;
1401   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1402   int ret;
1403
1404   /* Do not allow a scalar (reg+index) to parse as a register.  */
1405   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1406     return FAIL;
1407
1408   if (reg && reg->type == type)
1409     return reg->number;
1410
1411   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1412     return ret;
1413
1414   *ccp = start;
1415   return FAIL;
1416 }
1417
1418 /* Parse a Neon type specifier. *STR should point at the leading '.'
1419    character. Does no verification at this stage that the type fits the opcode
1420    properly. E.g.,
1421
1422      .i32.i32.s16
1423      .s32.f32
1424      .u16
1425
1426    Can all be legally parsed by this function.
1427
1428    Fills in neon_type struct pointer with parsed information, and updates STR
1429    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1430    type, FAIL if not.  */
1431
1432 static int
1433 parse_neon_type (struct neon_type *type, char **str)
1434 {
1435   char *ptr = *str;
1436
1437   if (type)
1438     type->elems = 0;
1439
1440   while (type->elems < NEON_MAX_TYPE_ELS)
1441     {
1442       enum neon_el_type thistype = NT_untyped;
1443       unsigned thissize = -1u;
1444
1445       if (*ptr != '.')
1446         break;
1447
1448       ptr++;
1449
1450       /* Just a size without an explicit type.  */
1451       if (ISDIGIT (*ptr))
1452         goto parsesize;
1453
1454       switch (TOLOWER (*ptr))
1455         {
1456         case 'i': thistype = NT_integer; break;
1457         case 'f': thistype = NT_float; break;
1458         case 'p': thistype = NT_poly; break;
1459         case 's': thistype = NT_signed; break;
1460         case 'u': thistype = NT_unsigned; break;
1461         case 'd':
1462           thistype = NT_float;
1463           thissize = 64;
1464           ptr++;
1465           goto done;
1466         default:
1467           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1468           return FAIL;
1469         }
1470
1471       ptr++;
1472
1473       /* .f is an abbreviation for .f32.  */
1474       if (thistype == NT_float && !ISDIGIT (*ptr))
1475         thissize = 32;
1476       else
1477         {
1478         parsesize:
1479           thissize = strtoul (ptr, &ptr, 10);
1480
1481           if (thissize != 8 && thissize != 16 && thissize != 32
1482               && thissize != 64)
1483             {
1484               as_bad (_("bad size %d in type specifier"), thissize);
1485               return FAIL;
1486             }
1487         }
1488
1489       done:
1490       if (type)
1491         {
1492           type->el[type->elems].type = thistype;
1493           type->el[type->elems].size = thissize;
1494           type->elems++;
1495         }
1496     }
1497
1498   /* Empty/missing type is not a successful parse.  */
1499   if (type->elems == 0)
1500     return FAIL;
1501
1502   *str = ptr;
1503
1504   return SUCCESS;
1505 }
1506
1507 /* Errors may be set multiple times during parsing or bit encoding
1508    (particularly in the Neon bits), but usually the earliest error which is set
1509    will be the most meaningful. Avoid overwriting it with later (cascading)
1510    errors by calling this function.  */
1511
1512 static void
1513 first_error (const char *err)
1514 {
1515   if (!inst.error)
1516     inst.error = err;
1517 }
1518
1519 /* Parse a single type, e.g. ".s32", leading period included.  */
1520 static int
1521 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1522 {
1523   char *str = *ccp;
1524   struct neon_type optype;
1525
1526   if (*str == '.')
1527     {
1528       if (parse_neon_type (&optype, &str) == SUCCESS)
1529         {
1530           if (optype.elems == 1)
1531             *vectype = optype.el[0];
1532           else
1533             {
1534               first_error (_("only one type should be specified for operand"));
1535               return FAIL;
1536             }
1537         }
1538       else
1539         {
1540           first_error (_("vector type expected"));
1541           return FAIL;
1542         }
1543     }
1544   else
1545     return FAIL;
1546
1547   *ccp = str;
1548
1549   return SUCCESS;
1550 }
1551
1552 /* Special meanings for indices (which have a range of 0-7), which will fit into
1553    a 4-bit integer.  */
1554
1555 #define NEON_ALL_LANES          15
1556 #define NEON_INTERLEAVE_LANES   14
1557
1558 /* Record a use of the given feature.  */
1559 static void
1560 record_feature_use (const arm_feature_set *feature)
1561 {
1562   if (thumb_mode)
1563     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
1564   else
1565     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
1566 }
1567
1568 /* If the given feature available in the selected CPU, mark it as used.
1569    Returns TRUE iff feature is available.  */
1570 static bfd_boolean
1571 mark_feature_used (const arm_feature_set *feature)
1572 {
1573
1574   /* Do not support the use of MVE only instructions when in auto-detection or
1575      -march=all.  */
1576   if (((feature == &mve_ext) || (feature == &mve_fp_ext))
1577       && ARM_CPU_IS_ANY (cpu_variant))
1578     {
1579       first_error (BAD_MVE_AUTO);
1580       return FALSE;
1581     }
1582   /* Ensure the option is valid on the current architecture.  */
1583   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
1584     return FALSE;
1585
1586   /* Add the appropriate architecture feature for the barrier option used.
1587      */
1588   record_feature_use (feature);
1589
1590   return TRUE;
1591 }
1592
1593 /* Parse either a register or a scalar, with an optional type. Return the
1594    register number, and optionally fill in the actual type of the register
1595    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1596    type/index information in *TYPEINFO.  */
1597
1598 static int
1599 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1600                            enum arm_reg_type *rtype,
1601                            struct neon_typed_alias *typeinfo)
1602 {
1603   char *str = *ccp;
1604   struct reg_entry *reg = arm_reg_parse_multi (&str);
1605   struct neon_typed_alias atype;
1606   struct neon_type_el parsetype;
1607
1608   atype.defined = 0;
1609   atype.index = -1;
1610   atype.eltype.type = NT_invtype;
1611   atype.eltype.size = -1;
1612
1613   /* Try alternate syntax for some types of register. Note these are mutually
1614      exclusive with the Neon syntax extensions.  */
1615   if (reg == NULL)
1616     {
1617       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1618       if (altreg != FAIL)
1619         *ccp = str;
1620       if (typeinfo)
1621         *typeinfo = atype;
1622       return altreg;
1623     }
1624
1625   /* Undo polymorphism when a set of register types may be accepted.  */
1626   if ((type == REG_TYPE_NDQ
1627        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1628       || (type == REG_TYPE_VFSD
1629           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1630       || (type == REG_TYPE_NSDQ
1631           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1632               || reg->type == REG_TYPE_NQ))
1633       || (type == REG_TYPE_NSD
1634           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1635       || (type == REG_TYPE_MMXWC
1636           && (reg->type == REG_TYPE_MMXWCG)))
1637     type = (enum arm_reg_type) reg->type;
1638
1639   if (type == REG_TYPE_MQ)
1640     {
1641       if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1642         return FAIL;
1643
1644       if (!reg || reg->type != REG_TYPE_NQ)
1645         return FAIL;
1646
1647       if (reg->number > 14 && !mark_feature_used (&fpu_vfp_ext_d32))
1648         {
1649           first_error (_("expected MVE register [q0..q7]"));
1650           return FAIL;
1651         }
1652       type = REG_TYPE_NQ;
1653     }
1654   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
1655            && (type == REG_TYPE_NQ))
1656     return FAIL;
1657
1658
1659   if (type != reg->type)
1660     return FAIL;
1661
1662   if (reg->neon)
1663     atype = *reg->neon;
1664
1665   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1666     {
1667       if ((atype.defined & NTA_HASTYPE) != 0)
1668         {
1669           first_error (_("can't redefine type for operand"));
1670           return FAIL;
1671         }
1672       atype.defined |= NTA_HASTYPE;
1673       atype.eltype = parsetype;
1674     }
1675
1676   if (skip_past_char (&str, '[') == SUCCESS)
1677     {
1678       if (type != REG_TYPE_VFD
1679           && !(type == REG_TYPE_VFS
1680                && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_2))
1681           && !(type == REG_TYPE_NQ
1682                && ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)))
1683         {
1684           if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1685             first_error (_("only D and Q registers may be indexed"));
1686           else
1687             first_error (_("only D registers may be indexed"));
1688           return FAIL;
1689         }
1690
1691       if ((atype.defined & NTA_HASINDEX) != 0)
1692         {
1693           first_error (_("can't change index for operand"));
1694           return FAIL;
1695         }
1696
1697       atype.defined |= NTA_HASINDEX;
1698
1699       if (skip_past_char (&str, ']') == SUCCESS)
1700         atype.index = NEON_ALL_LANES;
1701       else
1702         {
1703           expressionS exp;
1704
1705           my_get_expression (&exp, &str, GE_NO_PREFIX);
1706
1707           if (exp.X_op != O_constant)
1708             {
1709               first_error (_("constant expression required"));
1710               return FAIL;
1711             }
1712
1713           if (skip_past_char (&str, ']') == FAIL)
1714             return FAIL;
1715
1716           atype.index = exp.X_add_number;
1717         }
1718     }
1719
1720   if (typeinfo)
1721     *typeinfo = atype;
1722
1723   if (rtype)
1724     *rtype = type;
1725
1726   *ccp = str;
1727
1728   return reg->number;
1729 }
1730
1731 /* Like arm_reg_parse, but also allow the following extra features:
1732     - If RTYPE is non-zero, return the (possibly restricted) type of the
1733       register (e.g. Neon double or quad reg when either has been requested).
1734     - If this is a Neon vector type with additional type information, fill
1735       in the struct pointed to by VECTYPE (if non-NULL).
1736    This function will fault on encountering a scalar.  */
1737
1738 static int
1739 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1740                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1741 {
1742   struct neon_typed_alias atype;
1743   char *str = *ccp;
1744   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1745
1746   if (reg == FAIL)
1747     return FAIL;
1748
1749   /* Do not allow regname(... to parse as a register.  */
1750   if (*str == '(')
1751     return FAIL;
1752
1753   /* Do not allow a scalar (reg+index) to parse as a register.  */
1754   if ((atype.defined & NTA_HASINDEX) != 0)
1755     {
1756       first_error (_("register operand expected, but got scalar"));
1757       return FAIL;
1758     }
1759
1760   if (vectype)
1761     *vectype = atype.eltype;
1762
1763   *ccp = str;
1764
1765   return reg;
1766 }
1767
1768 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1769 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1770
1771 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1772    have enough information to be able to do a good job bounds-checking. So, we
1773    just do easy checks here, and do further checks later.  */
1774
1775 static int
1776 parse_scalar (char **ccp, int elsize, struct neon_type_el *type, enum
1777               arm_reg_type reg_type)
1778 {
1779   int reg;
1780   char *str = *ccp;
1781   struct neon_typed_alias atype;
1782   unsigned reg_size;
1783
1784   reg = parse_typed_reg_or_scalar (&str, reg_type, NULL, &atype);
1785
1786   switch (reg_type)
1787     {
1788     case REG_TYPE_VFS:
1789       reg_size = 32;
1790       break;
1791     case REG_TYPE_VFD:
1792       reg_size = 64;
1793       break;
1794     case REG_TYPE_MQ:
1795       reg_size = 128;
1796       break;
1797     default:
1798       gas_assert (0);
1799       return FAIL;
1800     }
1801
1802   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1803     return FAIL;
1804
1805   if (reg_type != REG_TYPE_MQ && atype.index == NEON_ALL_LANES)
1806     {
1807       first_error (_("scalar must have an index"));
1808       return FAIL;
1809     }
1810   else if (atype.index >= reg_size / elsize)
1811     {
1812       first_error (_("scalar index out of range"));
1813       return FAIL;
1814     }
1815
1816   if (type)
1817     *type = atype.eltype;
1818
1819   *ccp = str;
1820
1821   return reg * 16 + atype.index;
1822 }
1823
1824 /* Types of registers in a list.  */
1825
1826 enum reg_list_els
1827 {
1828   REGLIST_RN,
1829   REGLIST_CLRM,
1830   REGLIST_VFP_S,
1831   REGLIST_VFP_S_VPR,
1832   REGLIST_VFP_D,
1833   REGLIST_VFP_D_VPR,
1834   REGLIST_NEON_D
1835 };
1836
1837 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1838
1839 static long
1840 parse_reg_list (char ** strp, enum reg_list_els etype)
1841 {
1842   char *str = *strp;
1843   long range = 0;
1844   int another_range;
1845
1846   gas_assert (etype == REGLIST_RN || etype == REGLIST_CLRM);
1847
1848   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1849   do
1850     {
1851       skip_whitespace (str);
1852
1853       another_range = 0;
1854
1855       if (*str == '{')
1856         {
1857           int in_range = 0;
1858           int cur_reg = -1;
1859
1860           str++;
1861           do
1862             {
1863               int reg;
1864               const char apsr_str[] = "apsr";
1865               int apsr_str_len = strlen (apsr_str);
1866
1867               reg = arm_reg_parse (&str, REGLIST_RN);
1868               if (etype == REGLIST_CLRM)
1869                 {
1870                   if (reg == REG_SP || reg == REG_PC)
1871                     reg = FAIL;
1872                   else if (reg == FAIL
1873                            && !strncasecmp (str, apsr_str, apsr_str_len)
1874                            && !ISALPHA (*(str + apsr_str_len)))
1875                     {
1876                       reg = 15;
1877                       str += apsr_str_len;
1878                     }
1879
1880                   if (reg == FAIL)
1881                     {
1882                       first_error (_("r0-r12, lr or APSR expected"));
1883                       return FAIL;
1884                     }
1885                 }
1886               else /* etype == REGLIST_RN.  */
1887                 {
1888                   if (reg == FAIL)
1889                     {
1890                       first_error (_(reg_expected_msgs[REGLIST_RN]));
1891                       return FAIL;
1892                     }
1893                 }
1894
1895               if (in_range)
1896                 {
1897                   int i;
1898
1899                   if (reg <= cur_reg)
1900                     {
1901                       first_error (_("bad range in register list"));
1902                       return FAIL;
1903                     }
1904
1905                   for (i = cur_reg + 1; i < reg; i++)
1906                     {
1907                       if (range & (1 << i))
1908                         as_tsktsk
1909                           (_("Warning: duplicated register (r%d) in register list"),
1910                            i);
1911                       else
1912                         range |= 1 << i;
1913                     }
1914                   in_range = 0;
1915                 }
1916
1917               if (range & (1 << reg))
1918                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1919                            reg);
1920               else if (reg <= cur_reg)
1921                 as_tsktsk (_("Warning: register range not in ascending order"));
1922
1923               range |= 1 << reg;
1924               cur_reg = reg;
1925             }
1926           while (skip_past_comma (&str) != FAIL
1927                  || (in_range = 1, *str++ == '-'));
1928           str--;
1929
1930           if (skip_past_char (&str, '}') == FAIL)
1931             {
1932               first_error (_("missing `}'"));
1933               return FAIL;
1934             }
1935         }
1936       else if (etype == REGLIST_RN)
1937         {
1938           expressionS exp;
1939
1940           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1941             return FAIL;
1942
1943           if (exp.X_op == O_constant)
1944             {
1945               if (exp.X_add_number
1946                   != (exp.X_add_number & 0x0000ffff))
1947                 {
1948                   inst.error = _("invalid register mask");
1949                   return FAIL;
1950                 }
1951
1952               if ((range & exp.X_add_number) != 0)
1953                 {
1954                   int regno = range & exp.X_add_number;
1955
1956                   regno &= -regno;
1957                   regno = (1 << regno) - 1;
1958                   as_tsktsk
1959                     (_("Warning: duplicated register (r%d) in register list"),
1960                      regno);
1961                 }
1962
1963               range |= exp.X_add_number;
1964             }
1965           else
1966             {
1967               if (inst.relocs[0].type != 0)
1968                 {
1969                   inst.error = _("expression too complex");
1970                   return FAIL;
1971                 }
1972
1973               memcpy (&inst.relocs[0].exp, &exp, sizeof (expressionS));
1974               inst.relocs[0].type = BFD_RELOC_ARM_MULTI;
1975               inst.relocs[0].pc_rel = 0;
1976             }
1977         }
1978
1979       if (*str == '|' || *str == '+')
1980         {
1981           str++;
1982           another_range = 1;
1983         }
1984     }
1985   while (another_range);
1986
1987   *strp = str;
1988   return range;
1989 }
1990
1991 /* Parse a VFP register list.  If the string is invalid return FAIL.
1992    Otherwise return the number of registers, and set PBASE to the first
1993    register.  Parses registers of type ETYPE.
1994    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1995      - Q registers can be used to specify pairs of D registers
1996      - { } can be omitted from around a singleton register list
1997          FIXME: This is not implemented, as it would require backtracking in
1998          some cases, e.g.:
1999            vtbl.8 d3,d4,d5
2000          This could be done (the meaning isn't really ambiguous), but doesn't
2001          fit in well with the current parsing framework.
2002      - 32 D registers may be used (also true for VFPv3).
2003    FIXME: Types are ignored in these register lists, which is probably a
2004    bug.  */
2005
2006 static int
2007 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype,
2008                     bfd_boolean *partial_match)
2009 {
2010   char *str = *ccp;
2011   int base_reg;
2012   int new_base;
2013   enum arm_reg_type regtype = (enum arm_reg_type) 0;
2014   int max_regs = 0;
2015   int count = 0;
2016   int warned = 0;
2017   unsigned long mask = 0;
2018   int i;
2019   bfd_boolean vpr_seen = FALSE;
2020   bfd_boolean expect_vpr =
2021     (etype == REGLIST_VFP_S_VPR) || (etype == REGLIST_VFP_D_VPR);
2022
2023   if (skip_past_char (&str, '{') == FAIL)
2024     {
2025       inst.error = _("expecting {");
2026       return FAIL;
2027     }
2028
2029   switch (etype)
2030     {
2031     case REGLIST_VFP_S:
2032     case REGLIST_VFP_S_VPR:
2033       regtype = REG_TYPE_VFS;
2034       max_regs = 32;
2035       break;
2036
2037     case REGLIST_VFP_D:
2038     case REGLIST_VFP_D_VPR:
2039       regtype = REG_TYPE_VFD;
2040       break;
2041
2042     case REGLIST_NEON_D:
2043       regtype = REG_TYPE_NDQ;
2044       break;
2045
2046     default:
2047       gas_assert (0);
2048     }
2049
2050   if (etype != REGLIST_VFP_S && etype != REGLIST_VFP_S_VPR)
2051     {
2052       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
2053       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
2054         {
2055           max_regs = 32;
2056           if (thumb_mode)
2057             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
2058                                     fpu_vfp_ext_d32);
2059           else
2060             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
2061                                     fpu_vfp_ext_d32);
2062         }
2063       else
2064         max_regs = 16;
2065     }
2066
2067   base_reg = max_regs;
2068   *partial_match = FALSE;
2069
2070   do
2071     {
2072       int setmask = 1, addregs = 1;
2073       const char vpr_str[] = "vpr";
2074       int vpr_str_len = strlen (vpr_str);
2075
2076       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
2077
2078       if (expect_vpr)
2079         {
2080           if (new_base == FAIL
2081               && !strncasecmp (str, vpr_str, vpr_str_len)
2082               && !ISALPHA (*(str + vpr_str_len))
2083               && !vpr_seen)
2084             {
2085               vpr_seen = TRUE;
2086               str += vpr_str_len;
2087               if (count == 0)
2088                 base_reg = 0; /* Canonicalize VPR only on d0 with 0 regs.  */
2089             }
2090           else if (vpr_seen)
2091             {
2092               first_error (_("VPR expected last"));
2093               return FAIL;
2094             }
2095           else if (new_base == FAIL)
2096             {
2097               if (regtype == REG_TYPE_VFS)
2098                 first_error (_("VFP single precision register or VPR "
2099                                "expected"));
2100               else /* regtype == REG_TYPE_VFD.  */
2101                 first_error (_("VFP/Neon double precision register or VPR "
2102                                "expected"));
2103               return FAIL;
2104             }
2105         }
2106       else if (new_base == FAIL)
2107         {
2108           first_error (_(reg_expected_msgs[regtype]));
2109           return FAIL;
2110         }
2111
2112       *partial_match = TRUE;
2113       if (vpr_seen)
2114         continue;
2115
2116       if (new_base >= max_regs)
2117         {
2118           first_error (_("register out of range in list"));
2119           return FAIL;
2120         }
2121
2122       /* Note: a value of 2 * n is returned for the register Q<n>.  */
2123       if (regtype == REG_TYPE_NQ)
2124         {
2125           setmask = 3;
2126           addregs = 2;
2127         }
2128
2129       if (new_base < base_reg)
2130         base_reg = new_base;
2131
2132       if (mask & (setmask << new_base))
2133         {
2134           first_error (_("invalid register list"));
2135           return FAIL;
2136         }
2137
2138       if ((mask >> new_base) != 0 && ! warned && !vpr_seen)
2139         {
2140           as_tsktsk (_("register list not in ascending order"));
2141           warned = 1;
2142         }
2143
2144       mask |= setmask << new_base;
2145       count += addregs;
2146
2147       if (*str == '-') /* We have the start of a range expression */
2148         {
2149           int high_range;
2150
2151           str++;
2152
2153           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
2154               == FAIL)
2155             {
2156               inst.error = gettext (reg_expected_msgs[regtype]);
2157               return FAIL;
2158             }
2159
2160           if (high_range >= max_regs)
2161             {
2162               first_error (_("register out of range in list"));
2163               return FAIL;
2164             }
2165
2166           if (regtype == REG_TYPE_NQ)
2167             high_range = high_range + 1;
2168
2169           if (high_range <= new_base)
2170             {
2171               inst.error = _("register range not in ascending order");
2172               return FAIL;
2173             }
2174
2175           for (new_base += addregs; new_base <= high_range; new_base += addregs)
2176             {
2177               if (mask & (setmask << new_base))
2178                 {
2179                   inst.error = _("invalid register list");
2180                   return FAIL;
2181                 }
2182
2183               mask |= setmask << new_base;
2184               count += addregs;
2185             }
2186         }
2187     }
2188   while (skip_past_comma (&str) != FAIL);
2189
2190   str++;
2191
2192   /* Sanity check -- should have raised a parse error above.  */
2193   if ((!vpr_seen && count == 0) || count > max_regs)
2194     abort ();
2195
2196   *pbase = base_reg;
2197
2198   if (expect_vpr && !vpr_seen)
2199     {
2200       first_error (_("VPR expected last"));
2201       return FAIL;
2202     }
2203
2204   /* Final test -- the registers must be consecutive.  */
2205   mask >>= base_reg;
2206   for (i = 0; i < count; i++)
2207     {
2208       if ((mask & (1u << i)) == 0)
2209         {
2210           inst.error = _("non-contiguous register range");
2211           return FAIL;
2212         }
2213     }
2214
2215   *ccp = str;
2216
2217   return count;
2218 }
2219
2220 /* True if two alias types are the same.  */
2221
2222 static bfd_boolean
2223 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
2224 {
2225   if (!a && !b)
2226     return TRUE;
2227
2228   if (!a || !b)
2229     return FALSE;
2230
2231   if (a->defined != b->defined)
2232     return FALSE;
2233
2234   if ((a->defined & NTA_HASTYPE) != 0
2235       && (a->eltype.type != b->eltype.type
2236           || a->eltype.size != b->eltype.size))
2237     return FALSE;
2238
2239   if ((a->defined & NTA_HASINDEX) != 0
2240       && (a->index != b->index))
2241     return FALSE;
2242
2243   return TRUE;
2244 }
2245
2246 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
2247    The base register is put in *PBASE.
2248    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
2249    the return value.
2250    The register stride (minus one) is put in bit 4 of the return value.
2251    Bits [6:5] encode the list length (minus one).
2252    The type of the list elements is put in *ELTYPE, if non-NULL.  */
2253
2254 #define NEON_LANE(X)            ((X) & 0xf)
2255 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
2256 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2257
2258 static int
2259 parse_neon_el_struct_list (char **str, unsigned *pbase,
2260                            int mve,
2261                            struct neon_type_el *eltype)
2262 {
2263   char *ptr = *str;
2264   int base_reg = -1;
2265   int reg_incr = -1;
2266   int count = 0;
2267   int lane = -1;
2268   int leading_brace = 0;
2269   enum arm_reg_type rtype = REG_TYPE_NDQ;
2270   const char *const incr_error = mve ? _("register stride must be 1") :
2271     _("register stride must be 1 or 2");
2272   const char *const type_error = _("mismatched element/structure types in list");
2273   struct neon_typed_alias firsttype;
2274   firsttype.defined = 0;
2275   firsttype.eltype.type = NT_invtype;
2276   firsttype.eltype.size = -1;
2277   firsttype.index = -1;
2278
2279   if (skip_past_char (&ptr, '{') == SUCCESS)
2280     leading_brace = 1;
2281
2282   do
2283     {
2284       struct neon_typed_alias atype;
2285       if (mve)
2286         rtype = REG_TYPE_MQ;
2287       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2288
2289       if (getreg == FAIL)
2290         {
2291           first_error (_(reg_expected_msgs[rtype]));
2292           return FAIL;
2293         }
2294
2295       if (base_reg == -1)
2296         {
2297           base_reg = getreg;
2298           if (rtype == REG_TYPE_NQ)
2299             {
2300               reg_incr = 1;
2301             }
2302           firsttype = atype;
2303         }
2304       else if (reg_incr == -1)
2305         {
2306           reg_incr = getreg - base_reg;
2307           if (reg_incr < 1 || reg_incr > 2)
2308             {
2309               first_error (_(incr_error));
2310               return FAIL;
2311             }
2312         }
2313       else if (getreg != base_reg + reg_incr * count)
2314         {
2315           first_error (_(incr_error));
2316           return FAIL;
2317         }
2318
2319       if (! neon_alias_types_same (&atype, &firsttype))
2320         {
2321           first_error (_(type_error));
2322           return FAIL;
2323         }
2324
2325       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2326          modes.  */
2327       if (ptr[0] == '-')
2328         {
2329           struct neon_typed_alias htype;
2330           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2331           if (lane == -1)
2332             lane = NEON_INTERLEAVE_LANES;
2333           else if (lane != NEON_INTERLEAVE_LANES)
2334             {
2335               first_error (_(type_error));
2336               return FAIL;
2337             }
2338           if (reg_incr == -1)
2339             reg_incr = 1;
2340           else if (reg_incr != 1)
2341             {
2342               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2343               return FAIL;
2344             }
2345           ptr++;
2346           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2347           if (hireg == FAIL)
2348             {
2349               first_error (_(reg_expected_msgs[rtype]));
2350               return FAIL;
2351             }
2352           if (! neon_alias_types_same (&htype, &firsttype))
2353             {
2354               first_error (_(type_error));
2355               return FAIL;
2356             }
2357           count += hireg + dregs - getreg;
2358           continue;
2359         }
2360
2361       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2362       if (rtype == REG_TYPE_NQ)
2363         {
2364           count += 2;
2365           continue;
2366         }
2367
2368       if ((atype.defined & NTA_HASINDEX) != 0)
2369         {
2370           if (lane == -1)
2371             lane = atype.index;
2372           else if (lane != atype.index)
2373             {
2374               first_error (_(type_error));
2375               return FAIL;
2376             }
2377         }
2378       else if (lane == -1)
2379         lane = NEON_INTERLEAVE_LANES;
2380       else if (lane != NEON_INTERLEAVE_LANES)
2381         {
2382           first_error (_(type_error));
2383           return FAIL;
2384         }
2385       count++;
2386     }
2387   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2388
2389   /* No lane set by [x]. We must be interleaving structures.  */
2390   if (lane == -1)
2391     lane = NEON_INTERLEAVE_LANES;
2392
2393   /* Sanity check.  */
2394   if (lane == -1 || base_reg == -1 || count < 1 || (!mve && count > 4)
2395       || (count > 1 && reg_incr == -1))
2396     {
2397       first_error (_("error parsing element/structure list"));
2398       return FAIL;
2399     }
2400
2401   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2402     {
2403       first_error (_("expected }"));
2404       return FAIL;
2405     }
2406
2407   if (reg_incr == -1)
2408     reg_incr = 1;
2409
2410   if (eltype)
2411     *eltype = firsttype.eltype;
2412
2413   *pbase = base_reg;
2414   *str = ptr;
2415
2416   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2417 }
2418
2419 /* Parse an explicit relocation suffix on an expression.  This is
2420    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2421    arm_reloc_hsh contains no entries, so this function can only
2422    succeed if there is no () after the word.  Returns -1 on error,
2423    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2424
2425 static int
2426 parse_reloc (char **str)
2427 {
2428   struct reloc_entry *r;
2429   char *p, *q;
2430
2431   if (**str != '(')
2432     return BFD_RELOC_UNUSED;
2433
2434   p = *str + 1;
2435   q = p;
2436
2437   while (*q && *q != ')' && *q != ',')
2438     q++;
2439   if (*q != ')')
2440     return -1;
2441
2442   if ((r = (struct reloc_entry *)
2443        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2444     return -1;
2445
2446   *str = q + 1;
2447   return r->reloc;
2448 }
2449
2450 /* Directives: register aliases.  */
2451
2452 static struct reg_entry *
2453 insert_reg_alias (char *str, unsigned number, int type)
2454 {
2455   struct reg_entry *new_reg;
2456   const char *name;
2457
2458   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2459     {
2460       if (new_reg->builtin)
2461         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2462
2463       /* Only warn about a redefinition if it's not defined as the
2464          same register.  */
2465       else if (new_reg->number != number || new_reg->type != type)
2466         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2467
2468       return NULL;
2469     }
2470
2471   name = xstrdup (str);
2472   new_reg = XNEW (struct reg_entry);
2473
2474   new_reg->name = name;
2475   new_reg->number = number;
2476   new_reg->type = type;
2477   new_reg->builtin = FALSE;
2478   new_reg->neon = NULL;
2479
2480   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2481     abort ();
2482
2483   return new_reg;
2484 }
2485
2486 static void
2487 insert_neon_reg_alias (char *str, int number, int type,
2488                        struct neon_typed_alias *atype)
2489 {
2490   struct reg_entry *reg = insert_reg_alias (str, number, type);
2491
2492   if (!reg)
2493     {
2494       first_error (_("attempt to redefine typed alias"));
2495       return;
2496     }
2497
2498   if (atype)
2499     {
2500       reg->neon = XNEW (struct neon_typed_alias);
2501       *reg->neon = *atype;
2502     }
2503 }
2504
2505 /* Look for the .req directive.  This is of the form:
2506
2507         new_register_name .req existing_register_name
2508
2509    If we find one, or if it looks sufficiently like one that we want to
2510    handle any error here, return TRUE.  Otherwise return FALSE.  */
2511
2512 static bfd_boolean
2513 create_register_alias (char * newname, char *p)
2514 {
2515   struct reg_entry *old;
2516   char *oldname, *nbuf;
2517   size_t nlen;
2518
2519   /* The input scrubber ensures that whitespace after the mnemonic is
2520      collapsed to single spaces.  */
2521   oldname = p;
2522   if (strncmp (oldname, " .req ", 6) != 0)
2523     return FALSE;
2524
2525   oldname += 6;
2526   if (*oldname == '\0')
2527     return FALSE;
2528
2529   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2530   if (!old)
2531     {
2532       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2533       return TRUE;
2534     }
2535
2536   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2537      the desired alias name, and p points to its end.  If not, then
2538      the desired alias name is in the global original_case_string.  */
2539 #ifdef TC_CASE_SENSITIVE
2540   nlen = p - newname;
2541 #else
2542   newname = original_case_string;
2543   nlen = strlen (newname);
2544 #endif
2545
2546   nbuf = xmemdup0 (newname, nlen);
2547
2548   /* Create aliases under the new name as stated; an all-lowercase
2549      version of the new name; and an all-uppercase version of the new
2550      name.  */
2551   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2552     {
2553       for (p = nbuf; *p; p++)
2554         *p = TOUPPER (*p);
2555
2556       if (strncmp (nbuf, newname, nlen))
2557         {
2558           /* If this attempt to create an additional alias fails, do not bother
2559              trying to create the all-lower case alias.  We will fail and issue
2560              a second, duplicate error message.  This situation arises when the
2561              programmer does something like:
2562                foo .req r0
2563                Foo .req r1
2564              The second .req creates the "Foo" alias but then fails to create
2565              the artificial FOO alias because it has already been created by the
2566              first .req.  */
2567           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2568             {
2569               free (nbuf);
2570               return TRUE;
2571             }
2572         }
2573
2574       for (p = nbuf; *p; p++)
2575         *p = TOLOWER (*p);
2576
2577       if (strncmp (nbuf, newname, nlen))
2578         insert_reg_alias (nbuf, old->number, old->type);
2579     }
2580
2581   free (nbuf);
2582   return TRUE;
2583 }
2584
2585 /* Create a Neon typed/indexed register alias using directives, e.g.:
2586      X .dn d5.s32[1]
2587      Y .qn 6.s16
2588      Z .dn d7
2589      T .dn Z[0]
2590    These typed registers can be used instead of the types specified after the
2591    Neon mnemonic, so long as all operands given have types. Types can also be
2592    specified directly, e.g.:
2593      vadd d0.s32, d1.s32, d2.s32  */
2594
2595 static bfd_boolean
2596 create_neon_reg_alias (char *newname, char *p)
2597 {
2598   enum arm_reg_type basetype;
2599   struct reg_entry *basereg;
2600   struct reg_entry mybasereg;
2601   struct neon_type ntype;
2602   struct neon_typed_alias typeinfo;
2603   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2604   int namelen;
2605
2606   typeinfo.defined = 0;
2607   typeinfo.eltype.type = NT_invtype;
2608   typeinfo.eltype.size = -1;
2609   typeinfo.index = -1;
2610
2611   nameend = p;
2612
2613   if (strncmp (p, " .dn ", 5) == 0)
2614     basetype = REG_TYPE_VFD;
2615   else if (strncmp (p, " .qn ", 5) == 0)
2616     basetype = REG_TYPE_NQ;
2617   else
2618     return FALSE;
2619
2620   p += 5;
2621
2622   if (*p == '\0')
2623     return FALSE;
2624
2625   basereg = arm_reg_parse_multi (&p);
2626
2627   if (basereg && basereg->type != basetype)
2628     {
2629       as_bad (_("bad type for register"));
2630       return FALSE;
2631     }
2632
2633   if (basereg == NULL)
2634     {
2635       expressionS exp;
2636       /* Try parsing as an integer.  */
2637       my_get_expression (&exp, &p, GE_NO_PREFIX);
2638       if (exp.X_op != O_constant)
2639         {
2640           as_bad (_("expression must be constant"));
2641           return FALSE;
2642         }
2643       basereg = &mybasereg;
2644       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2645                                                   : exp.X_add_number;
2646       basereg->neon = 0;
2647     }
2648
2649   if (basereg->neon)
2650     typeinfo = *basereg->neon;
2651
2652   if (parse_neon_type (&ntype, &p) == SUCCESS)
2653     {
2654       /* We got a type.  */
2655       if (typeinfo.defined & NTA_HASTYPE)
2656         {
2657           as_bad (_("can't redefine the type of a register alias"));
2658           return FALSE;
2659         }
2660
2661       typeinfo.defined |= NTA_HASTYPE;
2662       if (ntype.elems != 1)
2663         {
2664           as_bad (_("you must specify a single type only"));
2665           return FALSE;
2666         }
2667       typeinfo.eltype = ntype.el[0];
2668     }
2669
2670   if (skip_past_char (&p, '[') == SUCCESS)
2671     {
2672       expressionS exp;
2673       /* We got a scalar index.  */
2674
2675       if (typeinfo.defined & NTA_HASINDEX)
2676         {
2677           as_bad (_("can't redefine the index of a scalar alias"));
2678           return FALSE;
2679         }
2680
2681       my_get_expression (&exp, &p, GE_NO_PREFIX);
2682
2683       if (exp.X_op != O_constant)
2684         {
2685           as_bad (_("scalar index must be constant"));
2686           return FALSE;
2687         }
2688
2689       typeinfo.defined |= NTA_HASINDEX;
2690       typeinfo.index = exp.X_add_number;
2691
2692       if (skip_past_char (&p, ']') == FAIL)
2693         {
2694           as_bad (_("expecting ]"));
2695           return FALSE;
2696         }
2697     }
2698
2699   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2700      the desired alias name, and p points to its end.  If not, then
2701      the desired alias name is in the global original_case_string.  */
2702 #ifdef TC_CASE_SENSITIVE
2703   namelen = nameend - newname;
2704 #else
2705   newname = original_case_string;
2706   namelen = strlen (newname);
2707 #endif
2708
2709   namebuf = xmemdup0 (newname, namelen);
2710
2711   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2712                          typeinfo.defined != 0 ? &typeinfo : NULL);
2713
2714   /* Insert name in all uppercase.  */
2715   for (p = namebuf; *p; p++)
2716     *p = TOUPPER (*p);
2717
2718   if (strncmp (namebuf, newname, namelen))
2719     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2720                            typeinfo.defined != 0 ? &typeinfo : NULL);
2721
2722   /* Insert name in all lowercase.  */
2723   for (p = namebuf; *p; p++)
2724     *p = TOLOWER (*p);
2725
2726   if (strncmp (namebuf, newname, namelen))
2727     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2728                            typeinfo.defined != 0 ? &typeinfo : NULL);
2729
2730   free (namebuf);
2731   return TRUE;
2732 }
2733
2734 /* Should never be called, as .req goes between the alias and the
2735    register name, not at the beginning of the line.  */
2736
2737 static void
2738 s_req (int a ATTRIBUTE_UNUSED)
2739 {
2740   as_bad (_("invalid syntax for .req directive"));
2741 }
2742
2743 static void
2744 s_dn (int a ATTRIBUTE_UNUSED)
2745 {
2746   as_bad (_("invalid syntax for .dn directive"));
2747 }
2748
2749 static void
2750 s_qn (int a ATTRIBUTE_UNUSED)
2751 {
2752   as_bad (_("invalid syntax for .qn directive"));
2753 }
2754
2755 /* The .unreq directive deletes an alias which was previously defined
2756    by .req.  For example:
2757
2758        my_alias .req r11
2759        .unreq my_alias    */
2760
2761 static void
2762 s_unreq (int a ATTRIBUTE_UNUSED)
2763 {
2764   char * name;
2765   char saved_char;
2766
2767   name = input_line_pointer;
2768
2769   while (*input_line_pointer != 0
2770          && *input_line_pointer != ' '
2771          && *input_line_pointer != '\n')
2772     ++input_line_pointer;
2773
2774   saved_char = *input_line_pointer;
2775   *input_line_pointer = 0;
2776
2777   if (!*name)
2778     as_bad (_("invalid syntax for .unreq directive"));
2779   else
2780     {
2781       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2782                                                               name);
2783
2784       if (!reg)
2785         as_bad (_("unknown register alias '%s'"), name);
2786       else if (reg->builtin)
2787         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2788                  name);
2789       else
2790         {
2791           char * p;
2792           char * nbuf;
2793
2794           hash_delete (arm_reg_hsh, name, FALSE);
2795           free ((char *) reg->name);
2796           if (reg->neon)
2797             free (reg->neon);
2798           free (reg);
2799
2800           /* Also locate the all upper case and all lower case versions.
2801              Do not complain if we cannot find one or the other as it
2802              was probably deleted above.  */
2803
2804           nbuf = strdup (name);
2805           for (p = nbuf; *p; p++)
2806             *p = TOUPPER (*p);
2807           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2808           if (reg)
2809             {
2810               hash_delete (arm_reg_hsh, nbuf, FALSE);
2811               free ((char *) reg->name);
2812               if (reg->neon)
2813                 free (reg->neon);
2814               free (reg);
2815             }
2816
2817           for (p = nbuf; *p; p++)
2818             *p = TOLOWER (*p);
2819           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2820           if (reg)
2821             {
2822               hash_delete (arm_reg_hsh, nbuf, FALSE);
2823               free ((char *) reg->name);
2824               if (reg->neon)
2825                 free (reg->neon);
2826               free (reg);
2827             }
2828
2829           free (nbuf);
2830         }
2831     }
2832
2833   *input_line_pointer = saved_char;
2834   demand_empty_rest_of_line ();
2835 }
2836
2837 /* Directives: Instruction set selection.  */
2838
2839 #ifdef OBJ_ELF
2840 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2841    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2842    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2843    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2844
2845 /* Create a new mapping symbol for the transition to STATE.  */
2846
2847 static void
2848 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2849 {
2850   symbolS * symbolP;
2851   const char * symname;
2852   int type;
2853
2854   switch (state)
2855     {
2856     case MAP_DATA:
2857       symname = "$d";
2858       type = BSF_NO_FLAGS;
2859       break;
2860     case MAP_ARM:
2861       symname = "$a";
2862       type = BSF_NO_FLAGS;
2863       break;
2864     case MAP_THUMB:
2865       symname = "$t";
2866       type = BSF_NO_FLAGS;
2867       break;
2868     default:
2869       abort ();
2870     }
2871
2872   symbolP = symbol_new (symname, now_seg, value, frag);
2873   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2874
2875   switch (state)
2876     {
2877     case MAP_ARM:
2878       THUMB_SET_FUNC (symbolP, 0);
2879       ARM_SET_THUMB (symbolP, 0);
2880       ARM_SET_INTERWORK (symbolP, support_interwork);
2881       break;
2882
2883     case MAP_THUMB:
2884       THUMB_SET_FUNC (symbolP, 1);
2885       ARM_SET_THUMB (symbolP, 1);
2886       ARM_SET_INTERWORK (symbolP, support_interwork);
2887       break;
2888
2889     case MAP_DATA:
2890     default:
2891       break;
2892     }
2893
2894   /* Save the mapping symbols for future reference.  Also check that
2895      we do not place two mapping symbols at the same offset within a
2896      frag.  We'll handle overlap between frags in
2897      check_mapping_symbols.
2898
2899      If .fill or other data filling directive generates zero sized data,
2900      the mapping symbol for the following code will have the same value
2901      as the one generated for the data filling directive.  In this case,
2902      we replace the old symbol with the new one at the same address.  */
2903   if (value == 0)
2904     {
2905       if (frag->tc_frag_data.first_map != NULL)
2906         {
2907           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2908           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2909         }
2910       frag->tc_frag_data.first_map = symbolP;
2911     }
2912   if (frag->tc_frag_data.last_map != NULL)
2913     {
2914       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2915       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2916         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2917     }
2918   frag->tc_frag_data.last_map = symbolP;
2919 }
2920
2921 /* We must sometimes convert a region marked as code to data during
2922    code alignment, if an odd number of bytes have to be padded.  The
2923    code mapping symbol is pushed to an aligned address.  */
2924
2925 static void
2926 insert_data_mapping_symbol (enum mstate state,
2927                             valueT value, fragS *frag, offsetT bytes)
2928 {
2929   /* If there was already a mapping symbol, remove it.  */
2930   if (frag->tc_frag_data.last_map != NULL
2931       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2932     {
2933       symbolS *symp = frag->tc_frag_data.last_map;
2934
2935       if (value == 0)
2936         {
2937           know (frag->tc_frag_data.first_map == symp);
2938           frag->tc_frag_data.first_map = NULL;
2939         }
2940       frag->tc_frag_data.last_map = NULL;
2941       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2942     }
2943
2944   make_mapping_symbol (MAP_DATA, value, frag);
2945   make_mapping_symbol (state, value + bytes, frag);
2946 }
2947
2948 static void mapping_state_2 (enum mstate state, int max_chars);
2949
2950 /* Set the mapping state to STATE.  Only call this when about to
2951    emit some STATE bytes to the file.  */
2952
2953 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2954 void
2955 mapping_state (enum mstate state)
2956 {
2957   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2958
2959   if (mapstate == state)
2960     /* The mapping symbol has already been emitted.
2961        There is nothing else to do.  */
2962     return;
2963
2964   if (state == MAP_ARM || state == MAP_THUMB)
2965     /*  PR gas/12931
2966         All ARM instructions require 4-byte alignment.
2967         (Almost) all Thumb instructions require 2-byte alignment.
2968
2969         When emitting instructions into any section, mark the section
2970         appropriately.
2971
2972         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2973         but themselves require 2-byte alignment; this applies to some
2974         PC- relative forms.  However, these cases will involve implicit
2975         literal pool generation or an explicit .align >=2, both of
2976         which will cause the section to me marked with sufficient
2977         alignment.  Thus, we don't handle those cases here.  */
2978     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2979
2980   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2981     /* This case will be evaluated later.  */
2982     return;
2983
2984   mapping_state_2 (state, 0);
2985 }
2986
2987 /* Same as mapping_state, but MAX_CHARS bytes have already been
2988    allocated.  Put the mapping symbol that far back.  */
2989
2990 static void
2991 mapping_state_2 (enum mstate state, int max_chars)
2992 {
2993   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2994
2995   if (!SEG_NORMAL (now_seg))
2996     return;
2997
2998   if (mapstate == state)
2999     /* The mapping symbol has already been emitted.
3000        There is nothing else to do.  */
3001     return;
3002
3003   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
3004           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
3005     {
3006       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
3007       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
3008
3009       if (add_symbol)
3010         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
3011     }
3012
3013   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
3014   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
3015 }
3016 #undef TRANSITION
3017 #else
3018 #define mapping_state(x) ((void)0)
3019 #define mapping_state_2(x, y) ((void)0)
3020 #endif
3021
3022 /* Find the real, Thumb encoded start of a Thumb function.  */
3023
3024 #ifdef OBJ_COFF
3025 static symbolS *
3026 find_real_start (symbolS * symbolP)
3027 {
3028   char *       real_start;
3029   const char * name = S_GET_NAME (symbolP);
3030   symbolS *    new_target;
3031
3032   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
3033 #define STUB_NAME ".real_start_of"
3034
3035   if (name == NULL)
3036     abort ();
3037
3038   /* The compiler may generate BL instructions to local labels because
3039      it needs to perform a branch to a far away location. These labels
3040      do not have a corresponding ".real_start_of" label.  We check
3041      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
3042      the ".real_start_of" convention for nonlocal branches.  */
3043   if (S_IS_LOCAL (symbolP) || name[0] == '.')
3044     return symbolP;
3045
3046   real_start = concat (STUB_NAME, name, NULL);
3047   new_target = symbol_find (real_start);
3048   free (real_start);
3049
3050   if (new_target == NULL)
3051     {
3052       as_warn (_("Failed to find real start of function: %s\n"), name);
3053       new_target = symbolP;
3054     }
3055
3056   return new_target;
3057 }
3058 #endif
3059
3060 static void
3061 opcode_select (int width)
3062 {
3063   switch (width)
3064     {
3065     case 16:
3066       if (! thumb_mode)
3067         {
3068           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
3069             as_bad (_("selected processor does not support THUMB opcodes"));
3070
3071           thumb_mode = 1;
3072           /* No need to force the alignment, since we will have been
3073              coming from ARM mode, which is word-aligned.  */
3074           record_alignment (now_seg, 1);
3075         }
3076       break;
3077
3078     case 32:
3079       if (thumb_mode)
3080         {
3081           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
3082             as_bad (_("selected processor does not support ARM opcodes"));
3083
3084           thumb_mode = 0;
3085
3086           if (!need_pass_2)
3087             frag_align (2, 0, 0);
3088
3089           record_alignment (now_seg, 1);
3090         }
3091       break;
3092
3093     default:
3094       as_bad (_("invalid instruction size selected (%d)"), width);
3095     }
3096 }
3097
3098 static void
3099 s_arm (int ignore ATTRIBUTE_UNUSED)
3100 {
3101   opcode_select (32);
3102   demand_empty_rest_of_line ();
3103 }
3104
3105 static void
3106 s_thumb (int ignore ATTRIBUTE_UNUSED)
3107 {
3108   opcode_select (16);
3109   demand_empty_rest_of_line ();
3110 }
3111
3112 static void
3113 s_code (int unused ATTRIBUTE_UNUSED)
3114 {
3115   int temp;
3116
3117   temp = get_absolute_expression ();
3118   switch (temp)
3119     {
3120     case 16:
3121     case 32:
3122       opcode_select (temp);
3123       break;
3124
3125     default:
3126       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
3127     }
3128 }
3129
3130 static void
3131 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
3132 {
3133   /* If we are not already in thumb mode go into it, EVEN if
3134      the target processor does not support thumb instructions.
3135      This is used by gcc/config/arm/lib1funcs.asm for example
3136      to compile interworking support functions even if the
3137      target processor should not support interworking.  */
3138   if (! thumb_mode)
3139     {
3140       thumb_mode = 2;
3141       record_alignment (now_seg, 1);
3142     }
3143
3144   demand_empty_rest_of_line ();
3145 }
3146
3147 static void
3148 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
3149 {
3150   s_thumb (0);
3151
3152   /* The following label is the name/address of the start of a Thumb function.
3153      We need to know this for the interworking support.  */
3154   label_is_thumb_function_name = TRUE;
3155 }
3156
3157 /* Perform a .set directive, but also mark the alias as
3158    being a thumb function.  */
3159
3160 static void
3161 s_thumb_set (int equiv)
3162 {
3163   /* XXX the following is a duplicate of the code for s_set() in read.c
3164      We cannot just call that code as we need to get at the symbol that
3165      is created.  */
3166   char *    name;
3167   char      delim;
3168   char *    end_name;
3169   symbolS * symbolP;
3170
3171   /* Especial apologies for the random logic:
3172      This just grew, and could be parsed much more simply!
3173      Dean - in haste.  */
3174   delim     = get_symbol_name (& name);
3175   end_name  = input_line_pointer;
3176   (void) restore_line_pointer (delim);
3177
3178   if (*input_line_pointer != ',')
3179     {
3180       *end_name = 0;
3181       as_bad (_("expected comma after name \"%s\""), name);
3182       *end_name = delim;
3183       ignore_rest_of_line ();
3184       return;
3185     }
3186
3187   input_line_pointer++;
3188   *end_name = 0;
3189
3190   if (name[0] == '.' && name[1] == '\0')
3191     {
3192       /* XXX - this should not happen to .thumb_set.  */
3193       abort ();
3194     }
3195
3196   if ((symbolP = symbol_find (name)) == NULL
3197       && (symbolP = md_undefined_symbol (name)) == NULL)
3198     {
3199 #ifndef NO_LISTING
3200       /* When doing symbol listings, play games with dummy fragments living
3201          outside the normal fragment chain to record the file and line info
3202          for this symbol.  */
3203       if (listing & LISTING_SYMBOLS)
3204         {
3205           extern struct list_info_struct * listing_tail;
3206           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
3207
3208           memset (dummy_frag, 0, sizeof (fragS));
3209           dummy_frag->fr_type = rs_fill;
3210           dummy_frag->line = listing_tail;
3211           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
3212           dummy_frag->fr_symbol = symbolP;
3213         }
3214       else
3215 #endif
3216         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
3217
3218 #ifdef OBJ_COFF
3219       /* "set" symbols are local unless otherwise specified.  */
3220       SF_SET_LOCAL (symbolP);
3221 #endif /* OBJ_COFF  */
3222     }                           /* Make a new symbol.  */
3223
3224   symbol_table_insert (symbolP);
3225
3226   * end_name = delim;
3227
3228   if (equiv
3229       && S_IS_DEFINED (symbolP)
3230       && S_GET_SEGMENT (symbolP) != reg_section)
3231     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
3232
3233   pseudo_set (symbolP);
3234
3235   demand_empty_rest_of_line ();
3236
3237   /* XXX Now we come to the Thumb specific bit of code.  */
3238
3239   THUMB_SET_FUNC (symbolP, 1);
3240   ARM_SET_THUMB (symbolP, 1);
3241 #if defined OBJ_ELF || defined OBJ_COFF
3242   ARM_SET_INTERWORK (symbolP, support_interwork);
3243 #endif
3244 }
3245
3246 /* Directives: Mode selection.  */
3247
3248 /* .syntax [unified|divided] - choose the new unified syntax
3249    (same for Arm and Thumb encoding, modulo slight differences in what
3250    can be represented) or the old divergent syntax for each mode.  */
3251 static void
3252 s_syntax (int unused ATTRIBUTE_UNUSED)
3253 {
3254   char *name, delim;
3255
3256   delim = get_symbol_name (& name);
3257
3258   if (!strcasecmp (name, "unified"))
3259     unified_syntax = TRUE;
3260   else if (!strcasecmp (name, "divided"))
3261     unified_syntax = FALSE;
3262   else
3263     {
3264       as_bad (_("unrecognized syntax mode \"%s\""), name);
3265       return;
3266     }
3267   (void) restore_line_pointer (delim);
3268   demand_empty_rest_of_line ();
3269 }
3270
3271 /* Directives: sectioning and alignment.  */
3272
3273 static void
3274 s_bss (int ignore ATTRIBUTE_UNUSED)
3275 {
3276   /* We don't support putting frags in the BSS segment, we fake it by
3277      marking in_bss, then looking at s_skip for clues.  */
3278   subseg_set (bss_section, 0);
3279   demand_empty_rest_of_line ();
3280
3281 #ifdef md_elf_section_change_hook
3282   md_elf_section_change_hook ();
3283 #endif
3284 }
3285
3286 static void
3287 s_even (int ignore ATTRIBUTE_UNUSED)
3288 {
3289   /* Never make frag if expect extra pass.  */
3290   if (!need_pass_2)
3291     frag_align (1, 0, 0);
3292
3293   record_alignment (now_seg, 1);
3294
3295   demand_empty_rest_of_line ();
3296 }
3297
3298 /* Directives: CodeComposer Studio.  */
3299
3300 /*  .ref  (for CodeComposer Studio syntax only).  */
3301 static void
3302 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3303 {
3304   if (codecomposer_syntax)
3305     ignore_rest_of_line ();
3306   else
3307     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3308 }
3309
3310 /*  If name is not NULL, then it is used for marking the beginning of a
3311     function, whereas if it is NULL then it means the function end.  */
3312 static void
3313 asmfunc_debug (const char * name)
3314 {
3315   static const char * last_name = NULL;
3316
3317   if (name != NULL)
3318     {
3319       gas_assert (last_name == NULL);
3320       last_name = name;
3321
3322       if (debug_type == DEBUG_STABS)
3323          stabs_generate_asm_func (name, name);
3324     }
3325   else
3326     {
3327       gas_assert (last_name != NULL);
3328
3329       if (debug_type == DEBUG_STABS)
3330         stabs_generate_asm_endfunc (last_name, last_name);
3331
3332       last_name = NULL;
3333     }
3334 }
3335
3336 static void
3337 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3338 {
3339   if (codecomposer_syntax)
3340     {
3341       switch (asmfunc_state)
3342         {
3343         case OUTSIDE_ASMFUNC:
3344           asmfunc_state = WAITING_ASMFUNC_NAME;
3345           break;
3346
3347         case WAITING_ASMFUNC_NAME:
3348           as_bad (_(".asmfunc repeated."));
3349           break;
3350
3351         case WAITING_ENDASMFUNC:
3352           as_bad (_(".asmfunc without function."));
3353           break;
3354         }
3355       demand_empty_rest_of_line ();
3356     }
3357   else
3358     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3359 }
3360
3361 static void
3362 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3363 {
3364   if (codecomposer_syntax)
3365     {
3366       switch (asmfunc_state)
3367         {
3368         case OUTSIDE_ASMFUNC:
3369           as_bad (_(".endasmfunc without a .asmfunc."));
3370           break;
3371
3372         case WAITING_ASMFUNC_NAME:
3373           as_bad (_(".endasmfunc without function."));
3374           break;
3375
3376         case WAITING_ENDASMFUNC:
3377           asmfunc_state = OUTSIDE_ASMFUNC;
3378           asmfunc_debug (NULL);
3379           break;
3380         }
3381       demand_empty_rest_of_line ();
3382     }
3383   else
3384     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3385 }
3386
3387 static void
3388 s_ccs_def (int name)
3389 {
3390   if (codecomposer_syntax)
3391     s_globl (name);
3392   else
3393     as_bad (_(".def pseudo-op only available with -mccs flag."));
3394 }
3395
3396 /* Directives: Literal pools.  */
3397
3398 static literal_pool *
3399 find_literal_pool (void)
3400 {
3401   literal_pool * pool;
3402
3403   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3404     {
3405       if (pool->section == now_seg
3406           && pool->sub_section == now_subseg)
3407         break;
3408     }
3409
3410   return pool;
3411 }
3412
3413 static literal_pool *
3414 find_or_make_literal_pool (void)
3415 {
3416   /* Next literal pool ID number.  */
3417   static unsigned int latest_pool_num = 1;
3418   literal_pool *      pool;
3419
3420   pool = find_literal_pool ();
3421
3422   if (pool == NULL)
3423     {
3424       /* Create a new pool.  */
3425       pool = XNEW (literal_pool);
3426       if (! pool)
3427         return NULL;
3428
3429       pool->next_free_entry = 0;
3430       pool->section         = now_seg;
3431       pool->sub_section     = now_subseg;
3432       pool->next            = list_of_pools;
3433       pool->symbol          = NULL;
3434       pool->alignment       = 2;
3435
3436       /* Add it to the list.  */
3437       list_of_pools = pool;
3438     }
3439
3440   /* New pools, and emptied pools, will have a NULL symbol.  */
3441   if (pool->symbol == NULL)
3442     {
3443       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3444                                     (valueT) 0, &zero_address_frag);
3445       pool->id = latest_pool_num ++;
3446     }
3447
3448   /* Done.  */
3449   return pool;
3450 }
3451
3452 /* Add the literal in the global 'inst'
3453    structure to the relevant literal pool.  */
3454
3455 static int
3456 add_to_lit_pool (unsigned int nbytes)
3457 {
3458 #define PADDING_SLOT 0x1
3459 #define LIT_ENTRY_SIZE_MASK 0xFF
3460   literal_pool * pool;
3461   unsigned int entry, pool_size = 0;
3462   bfd_boolean padding_slot_p = FALSE;
3463   unsigned imm1 = 0;
3464   unsigned imm2 = 0;
3465
3466   if (nbytes == 8)
3467     {
3468       imm1 = inst.operands[1].imm;
3469       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3470                : inst.relocs[0].exp.X_unsigned ? 0
3471                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3472       if (target_big_endian)
3473         {
3474           imm1 = imm2;
3475           imm2 = inst.operands[1].imm;
3476         }
3477     }
3478
3479   pool = find_or_make_literal_pool ();
3480
3481   /* Check if this literal value is already in the pool.  */
3482   for (entry = 0; entry < pool->next_free_entry; entry ++)
3483     {
3484       if (nbytes == 4)
3485         {
3486           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3487               && (inst.relocs[0].exp.X_op == O_constant)
3488               && (pool->literals[entry].X_add_number
3489                   == inst.relocs[0].exp.X_add_number)
3490               && (pool->literals[entry].X_md == nbytes)
3491               && (pool->literals[entry].X_unsigned
3492                   == inst.relocs[0].exp.X_unsigned))
3493             break;
3494
3495           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3496               && (inst.relocs[0].exp.X_op == O_symbol)
3497               && (pool->literals[entry].X_add_number
3498                   == inst.relocs[0].exp.X_add_number)
3499               && (pool->literals[entry].X_add_symbol
3500                   == inst.relocs[0].exp.X_add_symbol)
3501               && (pool->literals[entry].X_op_symbol
3502                   == inst.relocs[0].exp.X_op_symbol)
3503               && (pool->literals[entry].X_md == nbytes))
3504             break;
3505         }
3506       else if ((nbytes == 8)
3507                && !(pool_size & 0x7)
3508                && ((entry + 1) != pool->next_free_entry)
3509                && (pool->literals[entry].X_op == O_constant)
3510                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3511                && (pool->literals[entry].X_unsigned
3512                    == inst.relocs[0].exp.X_unsigned)
3513                && (pool->literals[entry + 1].X_op == O_constant)
3514                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3515                && (pool->literals[entry + 1].X_unsigned
3516                    == inst.relocs[0].exp.X_unsigned))
3517         break;
3518
3519       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3520       if (padding_slot_p && (nbytes == 4))
3521         break;
3522
3523       pool_size += 4;
3524     }
3525
3526   /* Do we need to create a new entry?  */
3527   if (entry == pool->next_free_entry)
3528     {
3529       if (entry >= MAX_LITERAL_POOL_SIZE)
3530         {
3531           inst.error = _("literal pool overflow");
3532           return FAIL;
3533         }
3534
3535       if (nbytes == 8)
3536         {
3537           /* For 8-byte entries, we align to an 8-byte boundary,
3538              and split it into two 4-byte entries, because on 32-bit
3539              host, 8-byte constants are treated as big num, thus
3540              saved in "generic_bignum" which will be overwritten
3541              by later assignments.
3542
3543              We also need to make sure there is enough space for
3544              the split.
3545
3546              We also check to make sure the literal operand is a
3547              constant number.  */
3548           if (!(inst.relocs[0].exp.X_op == O_constant
3549                 || inst.relocs[0].exp.X_op == O_big))
3550             {
3551               inst.error = _("invalid type for literal pool");
3552               return FAIL;
3553             }
3554           else if (pool_size & 0x7)
3555             {
3556               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3557                 {
3558                   inst.error = _("literal pool overflow");
3559                   return FAIL;
3560                 }
3561
3562               pool->literals[entry] = inst.relocs[0].exp;
3563               pool->literals[entry].X_op = O_constant;
3564               pool->literals[entry].X_add_number = 0;
3565               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3566               pool->next_free_entry += 1;
3567               pool_size += 4;
3568             }
3569           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3570             {
3571               inst.error = _("literal pool overflow");
3572               return FAIL;
3573             }
3574
3575           pool->literals[entry] = inst.relocs[0].exp;
3576           pool->literals[entry].X_op = O_constant;
3577           pool->literals[entry].X_add_number = imm1;
3578           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3579           pool->literals[entry++].X_md = 4;
3580           pool->literals[entry] = inst.relocs[0].exp;
3581           pool->literals[entry].X_op = O_constant;
3582           pool->literals[entry].X_add_number = imm2;
3583           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3584           pool->literals[entry].X_md = 4;
3585           pool->alignment = 3;
3586           pool->next_free_entry += 1;
3587         }
3588       else
3589         {
3590           pool->literals[entry] = inst.relocs[0].exp;
3591           pool->literals[entry].X_md = 4;
3592         }
3593
3594 #ifdef OBJ_ELF
3595       /* PR ld/12974: Record the location of the first source line to reference
3596          this entry in the literal pool.  If it turns out during linking that the
3597          symbol does not exist we will be able to give an accurate line number for
3598          the (first use of the) missing reference.  */
3599       if (debug_type == DEBUG_DWARF2)
3600         dwarf2_where (pool->locs + entry);
3601 #endif
3602       pool->next_free_entry += 1;
3603     }
3604   else if (padding_slot_p)
3605     {
3606       pool->literals[entry] = inst.relocs[0].exp;
3607       pool->literals[entry].X_md = nbytes;
3608     }
3609
3610   inst.relocs[0].exp.X_op             = O_symbol;
3611   inst.relocs[0].exp.X_add_number = pool_size;
3612   inst.relocs[0].exp.X_add_symbol = pool->symbol;
3613
3614   return SUCCESS;
3615 }
3616
3617 bfd_boolean
3618 tc_start_label_without_colon (void)
3619 {
3620   bfd_boolean ret = TRUE;
3621
3622   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3623     {
3624       const char *label = input_line_pointer;
3625
3626       while (!is_end_of_line[(int) label[-1]])
3627         --label;
3628
3629       if (*label == '.')
3630         {
3631           as_bad (_("Invalid label '%s'"), label);
3632           ret = FALSE;
3633         }
3634
3635       asmfunc_debug (label);
3636
3637       asmfunc_state = WAITING_ENDASMFUNC;
3638     }
3639
3640   return ret;
3641 }
3642
3643 /* Can't use symbol_new here, so have to create a symbol and then at
3644    a later date assign it a value. That's what these functions do.  */
3645
3646 static void
3647 symbol_locate (symbolS *    symbolP,
3648                const char * name,       /* It is copied, the caller can modify.  */
3649                segT         segment,    /* Segment identifier (SEG_<something>).  */
3650                valueT       valu,       /* Symbol value.  */
3651                fragS *      frag)       /* Associated fragment.  */
3652 {
3653   size_t name_length;
3654   char * preserved_copy_of_name;
3655
3656   name_length = strlen (name) + 1;   /* +1 for \0.  */
3657   obstack_grow (&notes, name, name_length);
3658   preserved_copy_of_name = (char *) obstack_finish (&notes);
3659
3660 #ifdef tc_canonicalize_symbol_name
3661   preserved_copy_of_name =
3662     tc_canonicalize_symbol_name (preserved_copy_of_name);
3663 #endif
3664
3665   S_SET_NAME (symbolP, preserved_copy_of_name);
3666
3667   S_SET_SEGMENT (symbolP, segment);
3668   S_SET_VALUE (symbolP, valu);
3669   symbol_clear_list_pointers (symbolP);
3670
3671   symbol_set_frag (symbolP, frag);
3672
3673   /* Link to end of symbol chain.  */
3674   {
3675     extern int symbol_table_frozen;
3676
3677     if (symbol_table_frozen)
3678       abort ();
3679   }
3680
3681   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3682
3683   obj_symbol_new_hook (symbolP);
3684
3685 #ifdef tc_symbol_new_hook
3686   tc_symbol_new_hook (symbolP);
3687 #endif
3688
3689 #ifdef DEBUG_SYMS
3690   verify_symbol_chain (symbol_rootP, symbol_lastP);
3691 #endif /* DEBUG_SYMS  */
3692 }
3693
3694 static void
3695 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3696 {
3697   unsigned int entry;
3698   literal_pool * pool;
3699   char sym_name[20];
3700
3701   pool = find_literal_pool ();
3702   if (pool == NULL
3703       || pool->symbol == NULL
3704       || pool->next_free_entry == 0)
3705     return;
3706
3707   /* Align pool as you have word accesses.
3708      Only make a frag if we have to.  */
3709   if (!need_pass_2)
3710     frag_align (pool->alignment, 0, 0);
3711
3712   record_alignment (now_seg, 2);
3713
3714 #ifdef OBJ_ELF
3715   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3716   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3717 #endif
3718   sprintf (sym_name, "$$lit_\002%x", pool->id);
3719
3720   symbol_locate (pool->symbol, sym_name, now_seg,
3721                  (valueT) frag_now_fix (), frag_now);
3722   symbol_table_insert (pool->symbol);
3723
3724   ARM_SET_THUMB (pool->symbol, thumb_mode);
3725
3726 #if defined OBJ_COFF || defined OBJ_ELF
3727   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3728 #endif
3729
3730   for (entry = 0; entry < pool->next_free_entry; entry ++)
3731     {
3732 #ifdef OBJ_ELF
3733       if (debug_type == DEBUG_DWARF2)
3734         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3735 #endif
3736       /* First output the expression in the instruction to the pool.  */
3737       emit_expr (&(pool->literals[entry]),
3738                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3739     }
3740
3741   /* Mark the pool as empty.  */
3742   pool->next_free_entry = 0;
3743   pool->symbol = NULL;
3744 }
3745
3746 #ifdef OBJ_ELF
3747 /* Forward declarations for functions below, in the MD interface
3748    section.  */
3749 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3750 static valueT create_unwind_entry (int);
3751 static void start_unwind_section (const segT, int);
3752 static void add_unwind_opcode (valueT, int);
3753 static void flush_pending_unwind (void);
3754
3755 /* Directives: Data.  */
3756
3757 static void
3758 s_arm_elf_cons (int nbytes)
3759 {
3760   expressionS exp;
3761
3762 #ifdef md_flush_pending_output
3763   md_flush_pending_output ();
3764 #endif
3765
3766   if (is_it_end_of_statement ())
3767     {
3768       demand_empty_rest_of_line ();
3769       return;
3770     }
3771
3772 #ifdef md_cons_align
3773   md_cons_align (nbytes);
3774 #endif
3775
3776   mapping_state (MAP_DATA);
3777   do
3778     {
3779       int reloc;
3780       char *base = input_line_pointer;
3781
3782       expression (& exp);
3783
3784       if (exp.X_op != O_symbol)
3785         emit_expr (&exp, (unsigned int) nbytes);
3786       else
3787         {
3788           char *before_reloc = input_line_pointer;
3789           reloc = parse_reloc (&input_line_pointer);
3790           if (reloc == -1)
3791             {
3792               as_bad (_("unrecognized relocation suffix"));
3793               ignore_rest_of_line ();
3794               return;
3795             }
3796           else if (reloc == BFD_RELOC_UNUSED)
3797             emit_expr (&exp, (unsigned int) nbytes);
3798           else
3799             {
3800               reloc_howto_type *howto = (reloc_howto_type *)
3801                   bfd_reloc_type_lookup (stdoutput,
3802                                          (bfd_reloc_code_real_type) reloc);
3803               int size = bfd_get_reloc_size (howto);
3804
3805               if (reloc == BFD_RELOC_ARM_PLT32)
3806                 {
3807                   as_bad (_("(plt) is only valid on branch targets"));
3808                   reloc = BFD_RELOC_UNUSED;
3809                   size = 0;
3810                 }
3811
3812               if (size > nbytes)
3813                 as_bad (ngettext ("%s relocations do not fit in %d byte",
3814                                   "%s relocations do not fit in %d bytes",
3815                                   nbytes),
3816                         howto->name, nbytes);
3817               else
3818                 {
3819                   /* We've parsed an expression stopping at O_symbol.
3820                      But there may be more expression left now that we
3821                      have parsed the relocation marker.  Parse it again.
3822                      XXX Surely there is a cleaner way to do this.  */
3823                   char *p = input_line_pointer;
3824                   int offset;
3825                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3826
3827                   memcpy (save_buf, base, input_line_pointer - base);
3828                   memmove (base + (input_line_pointer - before_reloc),
3829                            base, before_reloc - base);
3830
3831                   input_line_pointer = base + (input_line_pointer-before_reloc);
3832                   expression (&exp);
3833                   memcpy (base, save_buf, p - base);
3834
3835                   offset = nbytes - size;
3836                   p = frag_more (nbytes);
3837                   memset (p, 0, nbytes);
3838                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3839                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3840                   free (save_buf);
3841                 }
3842             }
3843         }
3844     }
3845   while (*input_line_pointer++ == ',');
3846
3847   /* Put terminator back into stream.  */
3848   input_line_pointer --;
3849   demand_empty_rest_of_line ();
3850 }
3851
3852 /* Emit an expression containing a 32-bit thumb instruction.
3853    Implementation based on put_thumb32_insn.  */
3854
3855 static void
3856 emit_thumb32_expr (expressionS * exp)
3857 {
3858   expressionS exp_high = *exp;
3859
3860   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3861   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3862   exp->X_add_number &= 0xffff;
3863   emit_expr (exp, (unsigned int) THUMB_SIZE);
3864 }
3865
3866 /*  Guess the instruction size based on the opcode.  */
3867
3868 static int
3869 thumb_insn_size (int opcode)
3870 {
3871   if ((unsigned int) opcode < 0xe800u)
3872     return 2;
3873   else if ((unsigned int) opcode >= 0xe8000000u)
3874     return 4;
3875   else
3876     return 0;
3877 }
3878
3879 static bfd_boolean
3880 emit_insn (expressionS *exp, int nbytes)
3881 {
3882   int size = 0;
3883
3884   if (exp->X_op == O_constant)
3885     {
3886       size = nbytes;
3887
3888       if (size == 0)
3889         size = thumb_insn_size (exp->X_add_number);
3890
3891       if (size != 0)
3892         {
3893           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3894             {
3895               as_bad (_(".inst.n operand too big. "\
3896                         "Use .inst.w instead"));
3897               size = 0;
3898             }
3899           else
3900             {
3901               if (now_pred.state == AUTOMATIC_PRED_BLOCK)
3902                 set_pred_insn_type_nonvoid (OUTSIDE_PRED_INSN, 0);
3903               else
3904                 set_pred_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3905
3906               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3907                 emit_thumb32_expr (exp);
3908               else
3909                 emit_expr (exp, (unsigned int) size);
3910
3911               it_fsm_post_encode ();
3912             }
3913         }
3914       else
3915         as_bad (_("cannot determine Thumb instruction size. "   \
3916                   "Use .inst.n/.inst.w instead"));
3917     }
3918   else
3919     as_bad (_("constant expression required"));
3920
3921   return (size != 0);
3922 }
3923
3924 /* Like s_arm_elf_cons but do not use md_cons_align and
3925    set the mapping state to MAP_ARM/MAP_THUMB.  */
3926
3927 static void
3928 s_arm_elf_inst (int nbytes)
3929 {
3930   if (is_it_end_of_statement ())
3931     {
3932       demand_empty_rest_of_line ();
3933       return;
3934     }
3935
3936   /* Calling mapping_state () here will not change ARM/THUMB,
3937      but will ensure not to be in DATA state.  */
3938
3939   if (thumb_mode)
3940     mapping_state (MAP_THUMB);
3941   else
3942     {
3943       if (nbytes != 0)
3944         {
3945           as_bad (_("width suffixes are invalid in ARM mode"));
3946           ignore_rest_of_line ();
3947           return;
3948         }
3949
3950       nbytes = 4;
3951
3952       mapping_state (MAP_ARM);
3953     }
3954
3955   do
3956     {
3957       expressionS exp;
3958
3959       expression (& exp);
3960
3961       if (! emit_insn (& exp, nbytes))
3962         {
3963           ignore_rest_of_line ();
3964           return;
3965         }
3966     }
3967   while (*input_line_pointer++ == ',');
3968
3969   /* Put terminator back into stream.  */
3970   input_line_pointer --;
3971   demand_empty_rest_of_line ();
3972 }
3973
3974 /* Parse a .rel31 directive.  */
3975
3976 static void
3977 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3978 {
3979   expressionS exp;
3980   char *p;
3981   valueT highbit;
3982
3983   highbit = 0;
3984   if (*input_line_pointer == '1')
3985     highbit = 0x80000000;
3986   else if (*input_line_pointer != '0')
3987     as_bad (_("expected 0 or 1"));
3988
3989   input_line_pointer++;
3990   if (*input_line_pointer != ',')
3991     as_bad (_("missing comma"));
3992   input_line_pointer++;
3993
3994 #ifdef md_flush_pending_output
3995   md_flush_pending_output ();
3996 #endif
3997
3998 #ifdef md_cons_align
3999   md_cons_align (4);
4000 #endif
4001
4002   mapping_state (MAP_DATA);
4003
4004   expression (&exp);
4005
4006   p = frag_more (4);
4007   md_number_to_chars (p, highbit, 4);
4008   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
4009                BFD_RELOC_ARM_PREL31);
4010
4011   demand_empty_rest_of_line ();
4012 }
4013
4014 /* Directives: AEABI stack-unwind tables.  */
4015
4016 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
4017
4018 static void
4019 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
4020 {
4021   demand_empty_rest_of_line ();
4022   if (unwind.proc_start)
4023     {
4024       as_bad (_("duplicate .fnstart directive"));
4025       return;
4026     }
4027
4028   /* Mark the start of the function.  */
4029   unwind.proc_start = expr_build_dot ();
4030
4031   /* Reset the rest of the unwind info.  */
4032   unwind.opcode_count = 0;
4033   unwind.table_entry = NULL;
4034   unwind.personality_routine = NULL;
4035   unwind.personality_index = -1;
4036   unwind.frame_size = 0;
4037   unwind.fp_offset = 0;
4038   unwind.fp_reg = REG_SP;
4039   unwind.fp_used = 0;
4040   unwind.sp_restored = 0;
4041 }
4042
4043
4044 /* Parse a handlerdata directive.  Creates the exception handling table entry
4045    for the function.  */
4046
4047 static void
4048 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
4049 {
4050   demand_empty_rest_of_line ();
4051   if (!unwind.proc_start)
4052     as_bad (MISSING_FNSTART);
4053
4054   if (unwind.table_entry)
4055     as_bad (_("duplicate .handlerdata directive"));
4056
4057   create_unwind_entry (1);
4058 }
4059
4060 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
4061
4062 static void
4063 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
4064 {
4065   long where;
4066   char *ptr;
4067   valueT val;
4068   unsigned int marked_pr_dependency;
4069
4070   demand_empty_rest_of_line ();
4071
4072   if (!unwind.proc_start)
4073     {
4074       as_bad (_(".fnend directive without .fnstart"));
4075       return;
4076     }
4077
4078   /* Add eh table entry.  */
4079   if (unwind.table_entry == NULL)
4080     val = create_unwind_entry (0);
4081   else
4082     val = 0;
4083
4084   /* Add index table entry.  This is two words.  */
4085   start_unwind_section (unwind.saved_seg, 1);
4086   frag_align (2, 0, 0);
4087   record_alignment (now_seg, 2);
4088
4089   ptr = frag_more (8);
4090   memset (ptr, 0, 8);
4091   where = frag_now_fix () - 8;
4092
4093   /* Self relative offset of the function start.  */
4094   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
4095            BFD_RELOC_ARM_PREL31);
4096
4097   /* Indicate dependency on EHABI-defined personality routines to the
4098      linker, if it hasn't been done already.  */
4099   marked_pr_dependency
4100     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
4101   if (unwind.personality_index >= 0 && unwind.personality_index < 3
4102       && !(marked_pr_dependency & (1 << unwind.personality_index)))
4103     {
4104       static const char *const name[] =
4105         {
4106           "__aeabi_unwind_cpp_pr0",
4107           "__aeabi_unwind_cpp_pr1",
4108           "__aeabi_unwind_cpp_pr2"
4109         };
4110       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
4111       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
4112       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
4113         |= 1 << unwind.personality_index;
4114     }
4115
4116   if (val)
4117     /* Inline exception table entry.  */
4118     md_number_to_chars (ptr + 4, val, 4);
4119   else
4120     /* Self relative offset of the table entry.  */
4121     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
4122              BFD_RELOC_ARM_PREL31);
4123
4124   /* Restore the original section.  */
4125   subseg_set (unwind.saved_seg, unwind.saved_subseg);
4126
4127   unwind.proc_start = NULL;
4128 }
4129
4130
4131 /* Parse an unwind_cantunwind directive.  */
4132
4133 static void
4134 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
4135 {
4136   demand_empty_rest_of_line ();
4137   if (!unwind.proc_start)
4138     as_bad (MISSING_FNSTART);
4139
4140   if (unwind.personality_routine || unwind.personality_index != -1)
4141     as_bad (_("personality routine specified for cantunwind frame"));
4142
4143   unwind.personality_index = -2;
4144 }
4145
4146
4147 /* Parse a personalityindex directive.  */
4148
4149 static void
4150 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
4151 {
4152   expressionS exp;
4153
4154   if (!unwind.proc_start)
4155     as_bad (MISSING_FNSTART);
4156
4157   if (unwind.personality_routine || unwind.personality_index != -1)
4158     as_bad (_("duplicate .personalityindex directive"));
4159
4160   expression (&exp);
4161
4162   if (exp.X_op != O_constant
4163       || exp.X_add_number < 0 || exp.X_add_number > 15)
4164     {
4165       as_bad (_("bad personality routine number"));
4166       ignore_rest_of_line ();
4167       return;
4168     }
4169
4170   unwind.personality_index = exp.X_add_number;
4171
4172   demand_empty_rest_of_line ();
4173 }
4174
4175
4176 /* Parse a personality directive.  */
4177
4178 static void
4179 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
4180 {
4181   char *name, *p, c;
4182
4183   if (!unwind.proc_start)
4184     as_bad (MISSING_FNSTART);
4185
4186   if (unwind.personality_routine || unwind.personality_index != -1)
4187     as_bad (_("duplicate .personality directive"));
4188
4189   c = get_symbol_name (& name);
4190   p = input_line_pointer;
4191   if (c == '"')
4192     ++ input_line_pointer;
4193   unwind.personality_routine = symbol_find_or_make (name);
4194   *p = c;
4195   demand_empty_rest_of_line ();
4196 }
4197
4198
4199 /* Parse a directive saving core registers.  */
4200
4201 static void
4202 s_arm_unwind_save_core (void)
4203 {
4204   valueT op;
4205   long range;
4206   int n;
4207
4208   range = parse_reg_list (&input_line_pointer, REGLIST_RN);
4209   if (range == FAIL)
4210     {
4211       as_bad (_("expected register list"));
4212       ignore_rest_of_line ();
4213       return;
4214     }
4215
4216   demand_empty_rest_of_line ();
4217
4218   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
4219      into .unwind_save {..., sp...}.  We aren't bothered about the value of
4220      ip because it is clobbered by calls.  */
4221   if (unwind.sp_restored && unwind.fp_reg == 12
4222       && (range & 0x3000) == 0x1000)
4223     {
4224       unwind.opcode_count--;
4225       unwind.sp_restored = 0;
4226       range = (range | 0x2000) & ~0x1000;
4227       unwind.pending_offset = 0;
4228     }
4229
4230   /* Pop r4-r15.  */
4231   if (range & 0xfff0)
4232     {
4233       /* See if we can use the short opcodes.  These pop a block of up to 8
4234          registers starting with r4, plus maybe r14.  */
4235       for (n = 0; n < 8; n++)
4236         {
4237           /* Break at the first non-saved register.      */
4238           if ((range & (1 << (n + 4))) == 0)
4239             break;
4240         }
4241       /* See if there are any other bits set.  */
4242       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
4243         {
4244           /* Use the long form.  */
4245           op = 0x8000 | ((range >> 4) & 0xfff);
4246           add_unwind_opcode (op, 2);
4247         }
4248       else
4249         {
4250           /* Use the short form.  */
4251           if (range & 0x4000)
4252             op = 0xa8; /* Pop r14.      */
4253           else
4254             op = 0xa0; /* Do not pop r14.  */
4255           op |= (n - 1);
4256           add_unwind_opcode (op, 1);
4257         }
4258     }
4259
4260   /* Pop r0-r3.  */
4261   if (range & 0xf)
4262     {
4263       op = 0xb100 | (range & 0xf);
4264       add_unwind_opcode (op, 2);
4265     }
4266
4267   /* Record the number of bytes pushed.  */
4268   for (n = 0; n < 16; n++)
4269     {
4270       if (range & (1 << n))
4271         unwind.frame_size += 4;
4272     }
4273 }
4274
4275
4276 /* Parse a directive saving FPA registers.  */
4277
4278 static void
4279 s_arm_unwind_save_fpa (int reg)
4280 {
4281   expressionS exp;
4282   int num_regs;
4283   valueT op;
4284
4285   /* Get Number of registers to transfer.  */
4286   if (skip_past_comma (&input_line_pointer) != FAIL)
4287     expression (&exp);
4288   else
4289     exp.X_op = O_illegal;
4290
4291   if (exp.X_op != O_constant)
4292     {
4293       as_bad (_("expected , <constant>"));
4294       ignore_rest_of_line ();
4295       return;
4296     }
4297
4298   num_regs = exp.X_add_number;
4299
4300   if (num_regs < 1 || num_regs > 4)
4301     {
4302       as_bad (_("number of registers must be in the range [1:4]"));
4303       ignore_rest_of_line ();
4304       return;
4305     }
4306
4307   demand_empty_rest_of_line ();
4308
4309   if (reg == 4)
4310     {
4311       /* Short form.  */
4312       op = 0xb4 | (num_regs - 1);
4313       add_unwind_opcode (op, 1);
4314     }
4315   else
4316     {
4317       /* Long form.  */
4318       op = 0xc800 | (reg << 4) | (num_regs - 1);
4319       add_unwind_opcode (op, 2);
4320     }
4321   unwind.frame_size += num_regs * 12;
4322 }
4323
4324
4325 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4326
4327 static void
4328 s_arm_unwind_save_vfp_armv6 (void)
4329 {
4330   int count;
4331   unsigned int start;
4332   valueT op;
4333   int num_vfpv3_regs = 0;
4334   int num_regs_below_16;
4335   bfd_boolean partial_match;
4336
4337   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D,
4338                               &partial_match);
4339   if (count == FAIL)
4340     {
4341       as_bad (_("expected register list"));
4342       ignore_rest_of_line ();
4343       return;
4344     }
4345
4346   demand_empty_rest_of_line ();
4347
4348   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4349      than FSTMX/FLDMX-style ones).  */
4350
4351   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4352   if (start >= 16)
4353     num_vfpv3_regs = count;
4354   else if (start + count > 16)
4355     num_vfpv3_regs = start + count - 16;
4356
4357   if (num_vfpv3_regs > 0)
4358     {
4359       int start_offset = start > 16 ? start - 16 : 0;
4360       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4361       add_unwind_opcode (op, 2);
4362     }
4363
4364   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4365   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4366   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4367   if (num_regs_below_16 > 0)
4368     {
4369       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4370       add_unwind_opcode (op, 2);
4371     }
4372
4373   unwind.frame_size += count * 8;
4374 }
4375
4376
4377 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4378
4379 static void
4380 s_arm_unwind_save_vfp (void)
4381 {
4382   int count;
4383   unsigned int reg;
4384   valueT op;
4385   bfd_boolean partial_match;
4386
4387   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D,
4388                               &partial_match);
4389   if (count == FAIL)
4390     {
4391       as_bad (_("expected register list"));
4392       ignore_rest_of_line ();
4393       return;
4394     }
4395
4396   demand_empty_rest_of_line ();
4397
4398   if (reg == 8)
4399     {
4400       /* Short form.  */
4401       op = 0xb8 | (count - 1);
4402       add_unwind_opcode (op, 1);
4403     }
4404   else
4405     {
4406       /* Long form.  */
4407       op = 0xb300 | (reg << 4) | (count - 1);
4408       add_unwind_opcode (op, 2);
4409     }
4410   unwind.frame_size += count * 8 + 4;
4411 }
4412
4413
4414 /* Parse a directive saving iWMMXt data registers.  */
4415
4416 static void
4417 s_arm_unwind_save_mmxwr (void)
4418 {
4419   int reg;
4420   int hi_reg;
4421   int i;
4422   unsigned mask = 0;
4423   valueT op;
4424
4425   if (*input_line_pointer == '{')
4426     input_line_pointer++;
4427
4428   do
4429     {
4430       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4431
4432       if (reg == FAIL)
4433         {
4434           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4435           goto error;
4436         }
4437
4438       if (mask >> reg)
4439         as_tsktsk (_("register list not in ascending order"));
4440       mask |= 1 << reg;
4441
4442       if (*input_line_pointer == '-')
4443         {
4444           input_line_pointer++;
4445           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4446           if (hi_reg == FAIL)
4447             {
4448               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4449               goto error;
4450             }
4451           else if (reg >= hi_reg)
4452             {
4453               as_bad (_("bad register range"));
4454               goto error;
4455             }
4456           for (; reg < hi_reg; reg++)
4457             mask |= 1 << reg;
4458         }
4459     }
4460   while (skip_past_comma (&input_line_pointer) != FAIL);
4461
4462   skip_past_char (&input_line_pointer, '}');
4463
4464   demand_empty_rest_of_line ();
4465
4466   /* Generate any deferred opcodes because we're going to be looking at
4467      the list.  */
4468   flush_pending_unwind ();
4469
4470   for (i = 0; i < 16; i++)
4471     {
4472       if (mask & (1 << i))
4473         unwind.frame_size += 8;
4474     }
4475
4476   /* Attempt to combine with a previous opcode.  We do this because gcc
4477      likes to output separate unwind directives for a single block of
4478      registers.  */
4479   if (unwind.opcode_count > 0)
4480     {
4481       i = unwind.opcodes[unwind.opcode_count - 1];
4482       if ((i & 0xf8) == 0xc0)
4483         {
4484           i &= 7;
4485           /* Only merge if the blocks are contiguous.  */
4486           if (i < 6)
4487             {
4488               if ((mask & 0xfe00) == (1 << 9))
4489                 {
4490                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4491                   unwind.opcode_count--;
4492                 }
4493             }
4494           else if (i == 6 && unwind.opcode_count >= 2)
4495             {
4496               i = unwind.opcodes[unwind.opcode_count - 2];
4497               reg = i >> 4;
4498               i &= 0xf;
4499
4500               op = 0xffff << (reg - 1);
4501               if (reg > 0
4502                   && ((mask & op) == (1u << (reg - 1))))
4503                 {
4504                   op = (1 << (reg + i + 1)) - 1;
4505                   op &= ~((1 << reg) - 1);
4506                   mask |= op;
4507                   unwind.opcode_count -= 2;
4508                 }
4509             }
4510         }
4511     }
4512
4513   hi_reg = 15;
4514   /* We want to generate opcodes in the order the registers have been
4515      saved, ie. descending order.  */
4516   for (reg = 15; reg >= -1; reg--)
4517     {
4518       /* Save registers in blocks.  */
4519       if (reg < 0
4520           || !(mask & (1 << reg)))
4521         {
4522           /* We found an unsaved reg.  Generate opcodes to save the
4523              preceding block.   */
4524           if (reg != hi_reg)
4525             {
4526               if (reg == 9)
4527                 {
4528                   /* Short form.  */
4529                   op = 0xc0 | (hi_reg - 10);
4530                   add_unwind_opcode (op, 1);
4531                 }
4532               else
4533                 {
4534                   /* Long form.  */
4535                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4536                   add_unwind_opcode (op, 2);
4537                 }
4538             }
4539           hi_reg = reg - 1;
4540         }
4541     }
4542
4543   return;
4544 error:
4545   ignore_rest_of_line ();
4546 }
4547
4548 static void
4549 s_arm_unwind_save_mmxwcg (void)
4550 {
4551   int reg;
4552   int hi_reg;
4553   unsigned mask = 0;
4554   valueT op;
4555
4556   if (*input_line_pointer == '{')
4557     input_line_pointer++;
4558
4559   skip_whitespace (input_line_pointer);
4560
4561   do
4562     {
4563       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4564
4565       if (reg == FAIL)
4566         {
4567           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4568           goto error;
4569         }
4570
4571       reg -= 8;
4572       if (mask >> reg)
4573         as_tsktsk (_("register list not in ascending order"));
4574       mask |= 1 << reg;
4575
4576       if (*input_line_pointer == '-')
4577         {
4578           input_line_pointer++;
4579           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4580           if (hi_reg == FAIL)
4581             {
4582               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4583               goto error;
4584             }
4585           else if (reg >= hi_reg)
4586             {
4587               as_bad (_("bad register range"));
4588               goto error;
4589             }
4590           for (; reg < hi_reg; reg++)
4591             mask |= 1 << reg;
4592         }
4593     }
4594   while (skip_past_comma (&input_line_pointer) != FAIL);
4595
4596   skip_past_char (&input_line_pointer, '}');
4597
4598   demand_empty_rest_of_line ();
4599
4600   /* Generate any deferred opcodes because we're going to be looking at
4601      the list.  */
4602   flush_pending_unwind ();
4603
4604   for (reg = 0; reg < 16; reg++)
4605     {
4606       if (mask & (1 << reg))
4607         unwind.frame_size += 4;
4608     }
4609   op = 0xc700 | mask;
4610   add_unwind_opcode (op, 2);
4611   return;
4612 error:
4613   ignore_rest_of_line ();
4614 }
4615
4616
4617 /* Parse an unwind_save directive.
4618    If the argument is non-zero, this is a .vsave directive.  */
4619
4620 static void
4621 s_arm_unwind_save (int arch_v6)
4622 {
4623   char *peek;
4624   struct reg_entry *reg;
4625   bfd_boolean had_brace = FALSE;
4626
4627   if (!unwind.proc_start)
4628     as_bad (MISSING_FNSTART);
4629
4630   /* Figure out what sort of save we have.  */
4631   peek = input_line_pointer;
4632
4633   if (*peek == '{')
4634     {
4635       had_brace = TRUE;
4636       peek++;
4637     }
4638
4639   reg = arm_reg_parse_multi (&peek);
4640
4641   if (!reg)
4642     {
4643       as_bad (_("register expected"));
4644       ignore_rest_of_line ();
4645       return;
4646     }
4647
4648   switch (reg->type)
4649     {
4650     case REG_TYPE_FN:
4651       if (had_brace)
4652         {
4653           as_bad (_("FPA .unwind_save does not take a register list"));
4654           ignore_rest_of_line ();
4655           return;
4656         }
4657       input_line_pointer = peek;
4658       s_arm_unwind_save_fpa (reg->number);
4659       return;
4660
4661     case REG_TYPE_RN:
4662       s_arm_unwind_save_core ();
4663       return;
4664
4665     case REG_TYPE_VFD:
4666       if (arch_v6)
4667         s_arm_unwind_save_vfp_armv6 ();
4668       else
4669         s_arm_unwind_save_vfp ();
4670       return;
4671
4672     case REG_TYPE_MMXWR:
4673       s_arm_unwind_save_mmxwr ();
4674       return;
4675
4676     case REG_TYPE_MMXWCG:
4677       s_arm_unwind_save_mmxwcg ();
4678       return;
4679
4680     default:
4681       as_bad (_(".unwind_save does not support this kind of register"));
4682       ignore_rest_of_line ();
4683     }
4684 }
4685
4686
4687 /* Parse an unwind_movsp directive.  */
4688
4689 static void
4690 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4691 {
4692   int reg;
4693   valueT op;
4694   int offset;
4695
4696   if (!unwind.proc_start)
4697     as_bad (MISSING_FNSTART);
4698
4699   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4700   if (reg == FAIL)
4701     {
4702       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4703       ignore_rest_of_line ();
4704       return;
4705     }
4706
4707   /* Optional constant.  */
4708   if (skip_past_comma (&input_line_pointer) != FAIL)
4709     {
4710       if (immediate_for_directive (&offset) == FAIL)
4711         return;
4712     }
4713   else
4714     offset = 0;
4715
4716   demand_empty_rest_of_line ();
4717
4718   if (reg == REG_SP || reg == REG_PC)
4719     {
4720       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4721       return;
4722     }
4723
4724   if (unwind.fp_reg != REG_SP)
4725     as_bad (_("unexpected .unwind_movsp directive"));
4726
4727   /* Generate opcode to restore the value.  */
4728   op = 0x90 | reg;
4729   add_unwind_opcode (op, 1);
4730
4731   /* Record the information for later.  */
4732   unwind.fp_reg = reg;
4733   unwind.fp_offset = unwind.frame_size - offset;
4734   unwind.sp_restored = 1;
4735 }
4736
4737 /* Parse an unwind_pad directive.  */
4738
4739 static void
4740 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4741 {
4742   int offset;
4743
4744   if (!unwind.proc_start)
4745     as_bad (MISSING_FNSTART);
4746
4747   if (immediate_for_directive (&offset) == FAIL)
4748     return;
4749
4750   if (offset & 3)
4751     {
4752       as_bad (_("stack increment must be multiple of 4"));
4753       ignore_rest_of_line ();
4754       return;
4755     }
4756
4757   /* Don't generate any opcodes, just record the details for later.  */
4758   unwind.frame_size += offset;
4759   unwind.pending_offset += offset;
4760
4761   demand_empty_rest_of_line ();
4762 }
4763
4764 /* Parse an unwind_setfp directive.  */
4765
4766 static void
4767 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4768 {
4769   int sp_reg;
4770   int fp_reg;
4771   int offset;
4772
4773   if (!unwind.proc_start)
4774     as_bad (MISSING_FNSTART);
4775
4776   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4777   if (skip_past_comma (&input_line_pointer) == FAIL)
4778     sp_reg = FAIL;
4779   else
4780     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4781
4782   if (fp_reg == FAIL || sp_reg == FAIL)
4783     {
4784       as_bad (_("expected <reg>, <reg>"));
4785       ignore_rest_of_line ();
4786       return;
4787     }
4788
4789   /* Optional constant.  */
4790   if (skip_past_comma (&input_line_pointer) != FAIL)
4791     {
4792       if (immediate_for_directive (&offset) == FAIL)
4793         return;
4794     }
4795   else
4796     offset = 0;
4797
4798   demand_empty_rest_of_line ();
4799
4800   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4801     {
4802       as_bad (_("register must be either sp or set by a previous"
4803                 "unwind_movsp directive"));
4804       return;
4805     }
4806
4807   /* Don't generate any opcodes, just record the information for later.  */
4808   unwind.fp_reg = fp_reg;
4809   unwind.fp_used = 1;
4810   if (sp_reg == REG_SP)
4811     unwind.fp_offset = unwind.frame_size - offset;
4812   else
4813     unwind.fp_offset -= offset;
4814 }
4815
4816 /* Parse an unwind_raw directive.  */
4817
4818 static void
4819 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4820 {
4821   expressionS exp;
4822   /* This is an arbitrary limit.         */
4823   unsigned char op[16];
4824   int count;
4825
4826   if (!unwind.proc_start)
4827     as_bad (MISSING_FNSTART);
4828
4829   expression (&exp);
4830   if (exp.X_op == O_constant
4831       && skip_past_comma (&input_line_pointer) != FAIL)
4832     {
4833       unwind.frame_size += exp.X_add_number;
4834       expression (&exp);
4835     }
4836   else
4837     exp.X_op = O_illegal;
4838
4839   if (exp.X_op != O_constant)
4840     {
4841       as_bad (_("expected <offset>, <opcode>"));
4842       ignore_rest_of_line ();
4843       return;
4844     }
4845
4846   count = 0;
4847
4848   /* Parse the opcode.  */
4849   for (;;)
4850     {
4851       if (count >= 16)
4852         {
4853           as_bad (_("unwind opcode too long"));
4854           ignore_rest_of_line ();
4855         }
4856       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4857         {
4858           as_bad (_("invalid unwind opcode"));
4859           ignore_rest_of_line ();
4860           return;
4861         }
4862       op[count++] = exp.X_add_number;
4863
4864       /* Parse the next byte.  */
4865       if (skip_past_comma (&input_line_pointer) == FAIL)
4866         break;
4867
4868       expression (&exp);
4869     }
4870
4871   /* Add the opcode bytes in reverse order.  */
4872   while (count--)
4873     add_unwind_opcode (op[count], 1);
4874
4875   demand_empty_rest_of_line ();
4876 }
4877
4878
4879 /* Parse a .eabi_attribute directive.  */
4880
4881 static void
4882 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4883 {
4884   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4885
4886   if (tag >= 0 && tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4887     attributes_set_explicitly[tag] = 1;
4888 }
4889
4890 /* Emit a tls fix for the symbol.  */
4891
4892 static void
4893 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4894 {
4895   char *p;
4896   expressionS exp;
4897 #ifdef md_flush_pending_output
4898   md_flush_pending_output ();
4899 #endif
4900
4901 #ifdef md_cons_align
4902   md_cons_align (4);
4903 #endif
4904
4905   /* Since we're just labelling the code, there's no need to define a
4906      mapping symbol.  */
4907   expression (&exp);
4908   p = obstack_next_free (&frchain_now->frch_obstack);
4909   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4910                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4911                : BFD_RELOC_ARM_TLS_DESCSEQ);
4912 }
4913 #endif /* OBJ_ELF */
4914
4915 static void s_arm_arch (int);
4916 static void s_arm_object_arch (int);
4917 static void s_arm_cpu (int);
4918 static void s_arm_fpu (int);
4919 static void s_arm_arch_extension (int);
4920
4921 #ifdef TE_PE
4922
4923 static void
4924 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4925 {
4926   expressionS exp;
4927
4928   do
4929     {
4930       expression (&exp);
4931       if (exp.X_op == O_symbol)
4932         exp.X_op = O_secrel;
4933
4934       emit_expr (&exp, 4);
4935     }
4936   while (*input_line_pointer++ == ',');
4937
4938   input_line_pointer--;
4939   demand_empty_rest_of_line ();
4940 }
4941 #endif /* TE_PE */
4942
4943 int
4944 arm_is_largest_exponent_ok (int precision)
4945 {
4946   /* precision == 1 ensures that this will only return
4947      true for 16 bit floats.  */
4948   return (precision == 1) && (fp16_format == ARM_FP16_FORMAT_ALTERNATIVE);
4949 }
4950
4951 static void
4952 set_fp16_format (int dummy ATTRIBUTE_UNUSED)
4953 {
4954   char saved_char;
4955   char* name;
4956   enum fp_16bit_format new_format;
4957
4958   new_format = ARM_FP16_FORMAT_DEFAULT;
4959
4960   name = input_line_pointer;
4961   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
4962     input_line_pointer++;
4963
4964   saved_char = *input_line_pointer;
4965   *input_line_pointer = 0;
4966
4967   if (strcasecmp (name, "ieee") == 0)
4968     new_format = ARM_FP16_FORMAT_IEEE;
4969   else if (strcasecmp (name, "alternative") == 0)
4970     new_format = ARM_FP16_FORMAT_ALTERNATIVE;
4971   else
4972     {
4973       as_bad (_("unrecognised float16 format \"%s\""), name);
4974       goto cleanup;
4975     }
4976
4977   /* Only set fp16_format if it is still the default (aka not already
4978      been set yet).  */
4979   if (fp16_format == ARM_FP16_FORMAT_DEFAULT)
4980     fp16_format = new_format;
4981   else
4982     {
4983       if (new_format != fp16_format)
4984         as_warn (_("float16 format cannot be set more than once, ignoring."));
4985     }
4986
4987 cleanup:
4988   *input_line_pointer = saved_char;
4989   ignore_rest_of_line ();
4990 }
4991
4992 /* This table describes all the machine specific pseudo-ops the assembler
4993    has to support.  The fields are:
4994      pseudo-op name without dot
4995      function to call to execute this pseudo-op
4996      Integer arg to pass to the function.  */
4997
4998 const pseudo_typeS md_pseudo_table[] =
4999 {
5000   /* Never called because '.req' does not start a line.  */
5001   { "req",         s_req,         0 },
5002   /* Following two are likewise never called.  */
5003   { "dn",          s_dn,          0 },
5004   { "qn",          s_qn,          0 },
5005   { "unreq",       s_unreq,       0 },
5006   { "bss",         s_bss,         0 },
5007   { "align",       s_align_ptwo,  2 },
5008   { "arm",         s_arm,         0 },
5009   { "thumb",       s_thumb,       0 },
5010   { "code",        s_code,        0 },
5011   { "force_thumb", s_force_thumb, 0 },
5012   { "thumb_func",  s_thumb_func,  0 },
5013   { "thumb_set",   s_thumb_set,   0 },
5014   { "even",        s_even,        0 },
5015   { "ltorg",       s_ltorg,       0 },
5016   { "pool",        s_ltorg,       0 },
5017   { "syntax",      s_syntax,      0 },
5018   { "cpu",         s_arm_cpu,     0 },
5019   { "arch",        s_arm_arch,    0 },
5020   { "object_arch", s_arm_object_arch,   0 },
5021   { "fpu",         s_arm_fpu,     0 },
5022   { "arch_extension", s_arm_arch_extension, 0 },
5023 #ifdef OBJ_ELF
5024   { "word",             s_arm_elf_cons, 4 },
5025   { "long",             s_arm_elf_cons, 4 },
5026   { "inst.n",           s_arm_elf_inst, 2 },
5027   { "inst.w",           s_arm_elf_inst, 4 },
5028   { "inst",             s_arm_elf_inst, 0 },
5029   { "rel31",            s_arm_rel31,      0 },
5030   { "fnstart",          s_arm_unwind_fnstart,   0 },
5031   { "fnend",            s_arm_unwind_fnend,     0 },
5032   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
5033   { "personality",      s_arm_unwind_personality, 0 },
5034   { "personalityindex", s_arm_unwind_personalityindex, 0 },
5035   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
5036   { "save",             s_arm_unwind_save,      0 },
5037   { "vsave",            s_arm_unwind_save,      1 },
5038   { "movsp",            s_arm_unwind_movsp,     0 },
5039   { "pad",              s_arm_unwind_pad,       0 },
5040   { "setfp",            s_arm_unwind_setfp,     0 },
5041   { "unwind_raw",       s_arm_unwind_raw,       0 },
5042   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
5043   { "tlsdescseq",       s_arm_tls_descseq,      0 },
5044 #else
5045   { "word",        cons, 4},
5046
5047   /* These are used for dwarf.  */
5048   {"2byte", cons, 2},
5049   {"4byte", cons, 4},
5050   {"8byte", cons, 8},
5051   /* These are used for dwarf2.  */
5052   { "file", dwarf2_directive_file, 0 },
5053   { "loc",  dwarf2_directive_loc,  0 },
5054   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
5055 #endif
5056   { "extend",      float_cons, 'x' },
5057   { "ldouble",     float_cons, 'x' },
5058   { "packed",      float_cons, 'p' },
5059 #ifdef TE_PE
5060   {"secrel32", pe_directive_secrel, 0},
5061 #endif
5062
5063   /* These are for compatibility with CodeComposer Studio.  */
5064   {"ref",          s_ccs_ref,        0},
5065   {"def",          s_ccs_def,        0},
5066   {"asmfunc",      s_ccs_asmfunc,    0},
5067   {"endasmfunc",   s_ccs_endasmfunc, 0},
5068
5069   {"float16", float_cons, 'h' },
5070   {"float16_format", set_fp16_format, 0 },
5071
5072   { 0, 0, 0 }
5073 };
5074
5075 /* Parser functions used exclusively in instruction operands.  */
5076
5077 /* Generic immediate-value read function for use in insn parsing.
5078    STR points to the beginning of the immediate (the leading #);
5079    VAL receives the value; if the value is outside [MIN, MAX]
5080    issue an error.  PREFIX_OPT is true if the immediate prefix is
5081    optional.  */
5082
5083 static int
5084 parse_immediate (char **str, int *val, int min, int max,
5085                  bfd_boolean prefix_opt)
5086 {
5087   expressionS exp;
5088
5089   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
5090   if (exp.X_op != O_constant)
5091     {
5092       inst.error = _("constant expression required");
5093       return FAIL;
5094     }
5095
5096   if (exp.X_add_number < min || exp.X_add_number > max)
5097     {
5098       inst.error = _("immediate value out of range");
5099       return FAIL;
5100     }
5101
5102   *val = exp.X_add_number;
5103   return SUCCESS;
5104 }
5105
5106 /* Less-generic immediate-value read function with the possibility of loading a
5107    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
5108    instructions. Puts the result directly in inst.operands[i].  */
5109
5110 static int
5111 parse_big_immediate (char **str, int i, expressionS *in_exp,
5112                      bfd_boolean allow_symbol_p)
5113 {
5114   expressionS exp;
5115   expressionS *exp_p = in_exp ? in_exp : &exp;
5116   char *ptr = *str;
5117
5118   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
5119
5120   if (exp_p->X_op == O_constant)
5121     {
5122       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
5123       /* If we're on a 64-bit host, then a 64-bit number can be returned using
5124          O_constant.  We have to be careful not to break compilation for
5125          32-bit X_add_number, though.  */
5126       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
5127         {
5128           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
5129           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
5130                                   & 0xffffffff);
5131           inst.operands[i].regisimm = 1;
5132         }
5133     }
5134   else if (exp_p->X_op == O_big
5135            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
5136     {
5137       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
5138
5139       /* Bignums have their least significant bits in
5140          generic_bignum[0]. Make sure we put 32 bits in imm and
5141          32 bits in reg,  in a (hopefully) portable way.  */
5142       gas_assert (parts != 0);
5143
5144       /* Make sure that the number is not too big.
5145          PR 11972: Bignums can now be sign-extended to the
5146          size of a .octa so check that the out of range bits
5147          are all zero or all one.  */
5148       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
5149         {
5150           LITTLENUM_TYPE m = -1;
5151
5152           if (generic_bignum[parts * 2] != 0
5153               && generic_bignum[parts * 2] != m)
5154             return FAIL;
5155
5156           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
5157             if (generic_bignum[j] != generic_bignum[j-1])
5158               return FAIL;
5159         }
5160
5161       inst.operands[i].imm = 0;
5162       for (j = 0; j < parts; j++, idx++)
5163         inst.operands[i].imm |= generic_bignum[idx]
5164                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5165       inst.operands[i].reg = 0;
5166       for (j = 0; j < parts; j++, idx++)
5167         inst.operands[i].reg |= generic_bignum[idx]
5168                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5169       inst.operands[i].regisimm = 1;
5170     }
5171   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
5172     return FAIL;
5173
5174   *str = ptr;
5175
5176   return SUCCESS;
5177 }
5178
5179 /* Returns the pseudo-register number of an FPA immediate constant,
5180    or FAIL if there isn't a valid constant here.  */
5181
5182 static int
5183 parse_fpa_immediate (char ** str)
5184 {
5185   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5186   char *         save_in;
5187   expressionS    exp;
5188   int            i;
5189   int            j;
5190
5191   /* First try and match exact strings, this is to guarantee
5192      that some formats will work even for cross assembly.  */
5193
5194   for (i = 0; fp_const[i]; i++)
5195     {
5196       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
5197         {
5198           char *start = *str;
5199
5200           *str += strlen (fp_const[i]);
5201           if (is_end_of_line[(unsigned char) **str])
5202             return i + 8;
5203           *str = start;
5204         }
5205     }
5206
5207   /* Just because we didn't get a match doesn't mean that the constant
5208      isn't valid, just that it is in a format that we don't
5209      automatically recognize.  Try parsing it with the standard
5210      expression routines.  */
5211
5212   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
5213
5214   /* Look for a raw floating point number.  */
5215   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
5216       && is_end_of_line[(unsigned char) *save_in])
5217     {
5218       for (i = 0; i < NUM_FLOAT_VALS; i++)
5219         {
5220           for (j = 0; j < MAX_LITTLENUMS; j++)
5221             {
5222               if (words[j] != fp_values[i][j])
5223                 break;
5224             }
5225
5226           if (j == MAX_LITTLENUMS)
5227             {
5228               *str = save_in;
5229               return i + 8;
5230             }
5231         }
5232     }
5233
5234   /* Try and parse a more complex expression, this will probably fail
5235      unless the code uses a floating point prefix (eg "0f").  */
5236   save_in = input_line_pointer;
5237   input_line_pointer = *str;
5238   if (expression (&exp) == absolute_section
5239       && exp.X_op == O_big
5240       && exp.X_add_number < 0)
5241     {
5242       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
5243          Ditto for 15.  */
5244 #define X_PRECISION 5
5245 #define E_PRECISION 15L
5246       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
5247         {
5248           for (i = 0; i < NUM_FLOAT_VALS; i++)
5249             {
5250               for (j = 0; j < MAX_LITTLENUMS; j++)
5251                 {
5252                   if (words[j] != fp_values[i][j])
5253                     break;
5254                 }
5255
5256               if (j == MAX_LITTLENUMS)
5257                 {
5258                   *str = input_line_pointer;
5259                   input_line_pointer = save_in;
5260                   return i + 8;
5261                 }
5262             }
5263         }
5264     }
5265
5266   *str = input_line_pointer;
5267   input_line_pointer = save_in;
5268   inst.error = _("invalid FPA immediate expression");
5269   return FAIL;
5270 }
5271
5272 /* Returns 1 if a number has "quarter-precision" float format
5273    0baBbbbbbc defgh000 00000000 00000000.  */
5274
5275 static int
5276 is_quarter_float (unsigned imm)
5277 {
5278   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
5279   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
5280 }
5281
5282
5283 /* Detect the presence of a floating point or integer zero constant,
5284    i.e. #0.0 or #0.  */
5285
5286 static bfd_boolean
5287 parse_ifimm_zero (char **in)
5288 {
5289   int error_code;
5290
5291   if (!is_immediate_prefix (**in))
5292     {
5293       /* In unified syntax, all prefixes are optional.  */
5294       if (!unified_syntax)
5295         return FALSE;
5296     }
5297   else
5298     ++*in;
5299
5300   /* Accept #0x0 as a synonym for #0.  */
5301   if (strncmp (*in, "0x", 2) == 0)
5302     {
5303       int val;
5304       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
5305         return FALSE;
5306       return TRUE;
5307     }
5308
5309   error_code = atof_generic (in, ".", EXP_CHARS,
5310                              &generic_floating_point_number);
5311
5312   if (!error_code
5313       && generic_floating_point_number.sign == '+'
5314       && (generic_floating_point_number.low
5315           > generic_floating_point_number.leader))
5316     return TRUE;
5317
5318   return FALSE;
5319 }
5320
5321 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5322    0baBbbbbbc defgh000 00000000 00000000.
5323    The zero and minus-zero cases need special handling, since they can't be
5324    encoded in the "quarter-precision" float format, but can nonetheless be
5325    loaded as integer constants.  */
5326
5327 static unsigned
5328 parse_qfloat_immediate (char **ccp, int *immed)
5329 {
5330   char *str = *ccp;
5331   char *fpnum;
5332   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5333   int found_fpchar = 0;
5334
5335   skip_past_char (&str, '#');
5336
5337   /* We must not accidentally parse an integer as a floating-point number. Make
5338      sure that the value we parse is not an integer by checking for special
5339      characters '.' or 'e'.
5340      FIXME: This is a horrible hack, but doing better is tricky because type
5341      information isn't in a very usable state at parse time.  */
5342   fpnum = str;
5343   skip_whitespace (fpnum);
5344
5345   if (strncmp (fpnum, "0x", 2) == 0)
5346     return FAIL;
5347   else
5348     {
5349       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5350         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5351           {
5352             found_fpchar = 1;
5353             break;
5354           }
5355
5356       if (!found_fpchar)
5357         return FAIL;
5358     }
5359
5360   if ((str = atof_ieee (str, 's', words)) != NULL)
5361     {
5362       unsigned fpword = 0;
5363       int i;
5364
5365       /* Our FP word must be 32 bits (single-precision FP).  */
5366       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5367         {
5368           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5369           fpword |= words[i];
5370         }
5371
5372       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5373         *immed = fpword;
5374       else
5375         return FAIL;
5376
5377       *ccp = str;
5378
5379       return SUCCESS;
5380     }
5381
5382   return FAIL;
5383 }
5384
5385 /* Shift operands.  */
5386 enum shift_kind
5387 {
5388   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX, SHIFT_UXTW
5389 };
5390
5391 struct asm_shift_name
5392 {
5393   const char      *name;
5394   enum shift_kind  kind;
5395 };
5396
5397 /* Third argument to parse_shift.  */
5398 enum parse_shift_mode
5399 {
5400   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5401   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5402   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5403   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5404   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5405   SHIFT_UXTW_IMMEDIATE          /* Shift must be UXTW immediate.  */
5406 };
5407
5408 /* Parse a <shift> specifier on an ARM data processing instruction.
5409    This has three forms:
5410
5411      (LSL|LSR|ASL|ASR|ROR) Rs
5412      (LSL|LSR|ASL|ASR|ROR) #imm
5413      RRX
5414
5415    Note that ASL is assimilated to LSL in the instruction encoding, and
5416    RRX to ROR #0 (which cannot be written as such).  */
5417
5418 static int
5419 parse_shift (char **str, int i, enum parse_shift_mode mode)
5420 {
5421   const struct asm_shift_name *shift_name;
5422   enum shift_kind shift;
5423   char *s = *str;
5424   char *p = s;
5425   int reg;
5426
5427   for (p = *str; ISALPHA (*p); p++)
5428     ;
5429
5430   if (p == *str)
5431     {
5432       inst.error = _("shift expression expected");
5433       return FAIL;
5434     }
5435
5436   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5437                                                             p - *str);
5438
5439   if (shift_name == NULL)
5440     {
5441       inst.error = _("shift expression expected");
5442       return FAIL;
5443     }
5444
5445   shift = shift_name->kind;
5446
5447   switch (mode)
5448     {
5449     case NO_SHIFT_RESTRICT:
5450     case SHIFT_IMMEDIATE:
5451       if (shift == SHIFT_UXTW)
5452         {
5453           inst.error = _("'UXTW' not allowed here");
5454           return FAIL;
5455         }
5456       break;
5457
5458     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5459       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5460         {
5461           inst.error = _("'LSL' or 'ASR' required");
5462           return FAIL;
5463         }
5464       break;
5465
5466     case SHIFT_LSL_IMMEDIATE:
5467       if (shift != SHIFT_LSL)
5468         {
5469           inst.error = _("'LSL' required");
5470           return FAIL;
5471         }
5472       break;
5473
5474     case SHIFT_ASR_IMMEDIATE:
5475       if (shift != SHIFT_ASR)
5476         {
5477           inst.error = _("'ASR' required");
5478           return FAIL;
5479         }
5480       break;
5481     case SHIFT_UXTW_IMMEDIATE:
5482       if (shift != SHIFT_UXTW)
5483         {
5484           inst.error = _("'UXTW' required");
5485           return FAIL;
5486         }
5487       break;
5488
5489     default: abort ();
5490     }
5491
5492   if (shift != SHIFT_RRX)
5493     {
5494       /* Whitespace can appear here if the next thing is a bare digit.  */
5495       skip_whitespace (p);
5496
5497       if (mode == NO_SHIFT_RESTRICT
5498           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5499         {
5500           inst.operands[i].imm = reg;
5501           inst.operands[i].immisreg = 1;
5502         }
5503       else if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5504         return FAIL;
5505     }
5506   inst.operands[i].shift_kind = shift;
5507   inst.operands[i].shifted = 1;
5508   *str = p;
5509   return SUCCESS;
5510 }
5511
5512 /* Parse a <shifter_operand> for an ARM data processing instruction:
5513
5514       #<immediate>
5515       #<immediate>, <rotate>
5516       <Rm>
5517       <Rm>, <shift>
5518
5519    where <shift> is defined by parse_shift above, and <rotate> is a
5520    multiple of 2 between 0 and 30.  Validation of immediate operands
5521    is deferred to md_apply_fix.  */
5522
5523 static int
5524 parse_shifter_operand (char **str, int i)
5525 {
5526   int value;
5527   expressionS exp;
5528
5529   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5530     {
5531       inst.operands[i].reg = value;
5532       inst.operands[i].isreg = 1;
5533
5534       /* parse_shift will override this if appropriate */
5535       inst.relocs[0].exp.X_op = O_constant;
5536       inst.relocs[0].exp.X_add_number = 0;
5537
5538       if (skip_past_comma (str) == FAIL)
5539         return SUCCESS;
5540
5541       /* Shift operation on register.  */
5542       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5543     }
5544
5545   if (my_get_expression (&inst.relocs[0].exp, str, GE_IMM_PREFIX))
5546     return FAIL;
5547
5548   if (skip_past_comma (str) == SUCCESS)
5549     {
5550       /* #x, y -- ie explicit rotation by Y.  */
5551       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5552         return FAIL;
5553
5554       if (exp.X_op != O_constant || inst.relocs[0].exp.X_op != O_constant)
5555         {
5556           inst.error = _("constant expression expected");
5557           return FAIL;
5558         }
5559
5560       value = exp.X_add_number;
5561       if (value < 0 || value > 30 || value % 2 != 0)
5562         {
5563           inst.error = _("invalid rotation");
5564           return FAIL;
5565         }
5566       if (inst.relocs[0].exp.X_add_number < 0
5567           || inst.relocs[0].exp.X_add_number > 255)
5568         {
5569           inst.error = _("invalid constant");
5570           return FAIL;
5571         }
5572
5573       /* Encode as specified.  */
5574       inst.operands[i].imm = inst.relocs[0].exp.X_add_number | value << 7;
5575       return SUCCESS;
5576     }
5577
5578   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
5579   inst.relocs[0].pc_rel = 0;
5580   return SUCCESS;
5581 }
5582
5583 /* Group relocation information.  Each entry in the table contains the
5584    textual name of the relocation as may appear in assembler source
5585    and must end with a colon.
5586    Along with this textual name are the relocation codes to be used if
5587    the corresponding instruction is an ALU instruction (ADD or SUB only),
5588    an LDR, an LDRS, or an LDC.  */
5589
5590 struct group_reloc_table_entry
5591 {
5592   const char *name;
5593   int alu_code;
5594   int ldr_code;
5595   int ldrs_code;
5596   int ldc_code;
5597 };
5598
5599 typedef enum
5600 {
5601   /* Varieties of non-ALU group relocation.  */
5602
5603   GROUP_LDR,
5604   GROUP_LDRS,
5605   GROUP_LDC,
5606   GROUP_MVE
5607 } group_reloc_type;
5608
5609 static struct group_reloc_table_entry group_reloc_table[] =
5610   { /* Program counter relative: */
5611     { "pc_g0_nc",
5612       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5613       0,                                /* LDR */
5614       0,                                /* LDRS */
5615       0 },                              /* LDC */
5616     { "pc_g0",
5617       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5618       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5619       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5620       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5621     { "pc_g1_nc",
5622       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5623       0,                                /* LDR */
5624       0,                                /* LDRS */
5625       0 },                              /* LDC */
5626     { "pc_g1",
5627       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5628       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5629       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5630       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5631     { "pc_g2",
5632       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5633       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5634       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5635       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5636     /* Section base relative */
5637     { "sb_g0_nc",
5638       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5639       0,                                /* LDR */
5640       0,                                /* LDRS */
5641       0 },                              /* LDC */
5642     { "sb_g0",
5643       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5644       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5645       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5646       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5647     { "sb_g1_nc",
5648       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5649       0,                                /* LDR */
5650       0,                                /* LDRS */
5651       0 },                              /* LDC */
5652     { "sb_g1",
5653       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5654       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5655       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5656       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5657     { "sb_g2",
5658       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5659       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5660       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5661       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5662     /* Absolute thumb alu relocations.  */
5663     { "lower0_7",
5664       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5665       0,                                /* LDR.  */
5666       0,                                /* LDRS.  */
5667       0 },                              /* LDC.  */
5668     { "lower8_15",
5669       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5670       0,                                /* LDR.  */
5671       0,                                /* LDRS.  */
5672       0 },                              /* LDC.  */
5673     { "upper0_7",
5674       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5675       0,                                /* LDR.  */
5676       0,                                /* LDRS.  */
5677       0 },                              /* LDC.  */
5678     { "upper8_15",
5679       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5680       0,                                /* LDR.  */
5681       0,                                /* LDRS.  */
5682       0 } };                            /* LDC.  */
5683
5684 /* Given the address of a pointer pointing to the textual name of a group
5685    relocation as may appear in assembler source, attempt to find its details
5686    in group_reloc_table.  The pointer will be updated to the character after
5687    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5688    otherwise.  On success, *entry will be updated to point at the relevant
5689    group_reloc_table entry. */
5690
5691 static int
5692 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5693 {
5694   unsigned int i;
5695   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5696     {
5697       int length = strlen (group_reloc_table[i].name);
5698
5699       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5700           && (*str)[length] == ':')
5701         {
5702           *out = &group_reloc_table[i];
5703           *str += (length + 1);
5704           return SUCCESS;
5705         }
5706     }
5707
5708   return FAIL;
5709 }
5710
5711 /* Parse a <shifter_operand> for an ARM data processing instruction
5712    (as for parse_shifter_operand) where group relocations are allowed:
5713
5714       #<immediate>
5715       #<immediate>, <rotate>
5716       #:<group_reloc>:<expression>
5717       <Rm>
5718       <Rm>, <shift>
5719
5720    where <group_reloc> is one of the strings defined in group_reloc_table.
5721    The hashes are optional.
5722
5723    Everything else is as for parse_shifter_operand.  */
5724
5725 static parse_operand_result
5726 parse_shifter_operand_group_reloc (char **str, int i)
5727 {
5728   /* Determine if we have the sequence of characters #: or just :
5729      coming next.  If we do, then we check for a group relocation.
5730      If we don't, punt the whole lot to parse_shifter_operand.  */
5731
5732   if (((*str)[0] == '#' && (*str)[1] == ':')
5733       || (*str)[0] == ':')
5734     {
5735       struct group_reloc_table_entry *entry;
5736
5737       if ((*str)[0] == '#')
5738         (*str) += 2;
5739       else
5740         (*str)++;
5741
5742       /* Try to parse a group relocation.  Anything else is an error.  */
5743       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5744         {
5745           inst.error = _("unknown group relocation");
5746           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5747         }
5748
5749       /* We now have the group relocation table entry corresponding to
5750          the name in the assembler source.  Next, we parse the expression.  */
5751       if (my_get_expression (&inst.relocs[0].exp, str, GE_NO_PREFIX))
5752         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5753
5754       /* Record the relocation type (always the ALU variant here).  */
5755       inst.relocs[0].type = (bfd_reloc_code_real_type) entry->alu_code;
5756       gas_assert (inst.relocs[0].type != 0);
5757
5758       return PARSE_OPERAND_SUCCESS;
5759     }
5760   else
5761     return parse_shifter_operand (str, i) == SUCCESS
5762            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5763
5764   /* Never reached.  */
5765 }
5766
5767 /* Parse a Neon alignment expression.  Information is written to
5768    inst.operands[i].  We assume the initial ':' has been skipped.
5769
5770    align        .imm = align << 8, .immisalign=1, .preind=0  */
5771 static parse_operand_result
5772 parse_neon_alignment (char **str, int i)
5773 {
5774   char *p = *str;
5775   expressionS exp;
5776
5777   my_get_expression (&exp, &p, GE_NO_PREFIX);
5778
5779   if (exp.X_op != O_constant)
5780     {
5781       inst.error = _("alignment must be constant");
5782       return PARSE_OPERAND_FAIL;
5783     }
5784
5785   inst.operands[i].imm = exp.X_add_number << 8;
5786   inst.operands[i].immisalign = 1;
5787   /* Alignments are not pre-indexes.  */
5788   inst.operands[i].preind = 0;
5789
5790   *str = p;
5791   return PARSE_OPERAND_SUCCESS;
5792 }
5793
5794 /* Parse all forms of an ARM address expression.  Information is written
5795    to inst.operands[i] and/or inst.relocs[0].
5796
5797    Preindexed addressing (.preind=1):
5798
5799    [Rn, #offset]       .reg=Rn .relocs[0].exp=offset
5800    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5801    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5802                        .shift_kind=shift .relocs[0].exp=shift_imm
5803
5804    These three may have a trailing ! which causes .writeback to be set also.
5805
5806    Postindexed addressing (.postind=1, .writeback=1):
5807
5808    [Rn], #offset       .reg=Rn .relocs[0].exp=offset
5809    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5810    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5811                        .shift_kind=shift .relocs[0].exp=shift_imm
5812
5813    Unindexed addressing (.preind=0, .postind=0):
5814
5815    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5816
5817    Other:
5818
5819    [Rn]{!}             shorthand for [Rn,#0]{!}
5820    =immediate          .isreg=0 .relocs[0].exp=immediate
5821    label               .reg=PC .relocs[0].pc_rel=1 .relocs[0].exp=label
5822
5823   It is the caller's responsibility to check for addressing modes not
5824   supported by the instruction, and to set inst.relocs[0].type.  */
5825
5826 static parse_operand_result
5827 parse_address_main (char **str, int i, int group_relocations,
5828                     group_reloc_type group_type)
5829 {
5830   char *p = *str;
5831   int reg;
5832
5833   if (skip_past_char (&p, '[') == FAIL)
5834     {
5835       if (skip_past_char (&p, '=') == FAIL)
5836         {
5837           /* Bare address - translate to PC-relative offset.  */
5838           inst.relocs[0].pc_rel = 1;
5839           inst.operands[i].reg = REG_PC;
5840           inst.operands[i].isreg = 1;
5841           inst.operands[i].preind = 1;
5842
5843           if (my_get_expression (&inst.relocs[0].exp, &p, GE_OPT_PREFIX_BIG))
5844             return PARSE_OPERAND_FAIL;
5845         }
5846       else if (parse_big_immediate (&p, i, &inst.relocs[0].exp,
5847                                     /*allow_symbol_p=*/TRUE))
5848         return PARSE_OPERAND_FAIL;
5849
5850       *str = p;
5851       return PARSE_OPERAND_SUCCESS;
5852     }
5853
5854   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5855   skip_whitespace (p);
5856
5857   if (group_type == GROUP_MVE)
5858     {
5859       enum arm_reg_type rtype = REG_TYPE_MQ;
5860       struct neon_type_el et;
5861       if ((reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5862         {
5863           inst.operands[i].isquad = 1;
5864         }
5865       else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5866         {
5867           inst.error = BAD_ADDR_MODE;
5868           return PARSE_OPERAND_FAIL;
5869         }
5870     }
5871   else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5872     {
5873       if (group_type == GROUP_MVE)
5874         inst.error = BAD_ADDR_MODE;
5875       else
5876         inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5877       return PARSE_OPERAND_FAIL;
5878     }
5879   inst.operands[i].reg = reg;
5880   inst.operands[i].isreg = 1;
5881
5882   if (skip_past_comma (&p) == SUCCESS)
5883     {
5884       inst.operands[i].preind = 1;
5885
5886       if (*p == '+') p++;
5887       else if (*p == '-') p++, inst.operands[i].negative = 1;
5888
5889       enum arm_reg_type rtype = REG_TYPE_MQ;
5890       struct neon_type_el et;
5891       if (group_type == GROUP_MVE
5892           && (reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5893         {
5894           inst.operands[i].immisreg = 2;
5895           inst.operands[i].imm = reg;
5896
5897           if (skip_past_comma (&p) == SUCCESS)
5898             {
5899               if (parse_shift (&p, i, SHIFT_UXTW_IMMEDIATE) == SUCCESS)
5900                 {
5901                   inst.operands[i].imm |= inst.relocs[0].exp.X_add_number << 5;
5902                   inst.relocs[0].exp.X_add_number = 0;
5903                 }
5904               else
5905                 return PARSE_OPERAND_FAIL;
5906             }
5907         }
5908       else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5909         {
5910           inst.operands[i].imm = reg;
5911           inst.operands[i].immisreg = 1;
5912
5913           if (skip_past_comma (&p) == SUCCESS)
5914             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5915               return PARSE_OPERAND_FAIL;
5916         }
5917       else if (skip_past_char (&p, ':') == SUCCESS)
5918         {
5919           /* FIXME: '@' should be used here, but it's filtered out by generic
5920              code before we get to see it here. This may be subject to
5921              change.  */
5922           parse_operand_result result = parse_neon_alignment (&p, i);
5923
5924           if (result != PARSE_OPERAND_SUCCESS)
5925             return result;
5926         }
5927       else
5928         {
5929           if (inst.operands[i].negative)
5930             {
5931               inst.operands[i].negative = 0;
5932               p--;
5933             }
5934
5935           if (group_relocations
5936               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5937             {
5938               struct group_reloc_table_entry *entry;
5939
5940               /* Skip over the #: or : sequence.  */
5941               if (*p == '#')
5942                 p += 2;
5943               else
5944                 p++;
5945
5946               /* Try to parse a group relocation.  Anything else is an
5947                  error.  */
5948               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5949                 {
5950                   inst.error = _("unknown group relocation");
5951                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5952                 }
5953
5954               /* We now have the group relocation table entry corresponding to
5955                  the name in the assembler source.  Next, we parse the
5956                  expression.  */
5957               if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
5958                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5959
5960               /* Record the relocation type.  */
5961               switch (group_type)
5962                 {
5963                   case GROUP_LDR:
5964                     inst.relocs[0].type
5965                         = (bfd_reloc_code_real_type) entry->ldr_code;
5966                     break;
5967
5968                   case GROUP_LDRS:
5969                     inst.relocs[0].type
5970                         = (bfd_reloc_code_real_type) entry->ldrs_code;
5971                     break;
5972
5973                   case GROUP_LDC:
5974                     inst.relocs[0].type
5975                         = (bfd_reloc_code_real_type) entry->ldc_code;
5976                     break;
5977
5978                   default:
5979                     gas_assert (0);
5980                 }
5981
5982               if (inst.relocs[0].type == 0)
5983                 {
5984                   inst.error = _("this group relocation is not allowed on this instruction");
5985                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5986                 }
5987             }
5988           else
5989             {
5990               char *q = p;
5991
5992               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5993                 return PARSE_OPERAND_FAIL;
5994               /* If the offset is 0, find out if it's a +0 or -0.  */
5995               if (inst.relocs[0].exp.X_op == O_constant
5996                   && inst.relocs[0].exp.X_add_number == 0)
5997                 {
5998                   skip_whitespace (q);
5999                   if (*q == '#')
6000                     {
6001                       q++;
6002                       skip_whitespace (q);
6003                     }
6004                   if (*q == '-')
6005                     inst.operands[i].negative = 1;
6006                 }
6007             }
6008         }
6009     }
6010   else if (skip_past_char (&p, ':') == SUCCESS)
6011     {
6012       /* FIXME: '@' should be used here, but it's filtered out by generic code
6013          before we get to see it here. This may be subject to change.  */
6014       parse_operand_result result = parse_neon_alignment (&p, i);
6015
6016       if (result != PARSE_OPERAND_SUCCESS)
6017         return result;
6018     }
6019
6020   if (skip_past_char (&p, ']') == FAIL)
6021     {
6022       inst.error = _("']' expected");
6023       return PARSE_OPERAND_FAIL;
6024     }
6025
6026   if (skip_past_char (&p, '!') == SUCCESS)
6027     inst.operands[i].writeback = 1;
6028
6029   else if (skip_past_comma (&p) == SUCCESS)
6030     {
6031       if (skip_past_char (&p, '{') == SUCCESS)
6032         {
6033           /* [Rn], {expr} - unindexed, with option */
6034           if (parse_immediate (&p, &inst.operands[i].imm,
6035                                0, 255, TRUE) == FAIL)
6036             return PARSE_OPERAND_FAIL;
6037
6038           if (skip_past_char (&p, '}') == FAIL)
6039             {
6040               inst.error = _("'}' expected at end of 'option' field");
6041               return PARSE_OPERAND_FAIL;
6042             }
6043           if (inst.operands[i].preind)
6044             {
6045               inst.error = _("cannot combine index with option");
6046               return PARSE_OPERAND_FAIL;
6047             }
6048           *str = p;
6049           return PARSE_OPERAND_SUCCESS;
6050         }
6051       else
6052         {
6053           inst.operands[i].postind = 1;
6054           inst.operands[i].writeback = 1;
6055
6056           if (inst.operands[i].preind)
6057             {
6058               inst.error = _("cannot combine pre- and post-indexing");
6059               return PARSE_OPERAND_FAIL;
6060             }
6061
6062           if (*p == '+') p++;
6063           else if (*p == '-') p++, inst.operands[i].negative = 1;
6064
6065           enum arm_reg_type rtype = REG_TYPE_MQ;
6066           struct neon_type_el et;
6067           if (group_type == GROUP_MVE
6068               && (reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
6069             {
6070               inst.operands[i].immisreg = 2;
6071               inst.operands[i].imm = reg;
6072             }
6073           else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
6074             {
6075               /* We might be using the immediate for alignment already. If we
6076                  are, OR the register number into the low-order bits.  */
6077               if (inst.operands[i].immisalign)
6078                 inst.operands[i].imm |= reg;
6079               else
6080                 inst.operands[i].imm = reg;
6081               inst.operands[i].immisreg = 1;
6082
6083               if (skip_past_comma (&p) == SUCCESS)
6084                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
6085                   return PARSE_OPERAND_FAIL;
6086             }
6087           else
6088             {
6089               char *q = p;
6090
6091               if (inst.operands[i].negative)
6092                 {
6093                   inst.operands[i].negative = 0;
6094                   p--;
6095                 }
6096               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
6097                 return PARSE_OPERAND_FAIL;
6098               /* If the offset is 0, find out if it's a +0 or -0.  */
6099               if (inst.relocs[0].exp.X_op == O_constant
6100                   && inst.relocs[0].exp.X_add_number == 0)
6101                 {
6102                   skip_whitespace (q);
6103                   if (*q == '#')
6104                     {
6105                       q++;
6106                       skip_whitespace (q);
6107                     }
6108                   if (*q == '-')
6109                     inst.operands[i].negative = 1;
6110                 }
6111             }
6112         }
6113     }
6114
6115   /* If at this point neither .preind nor .postind is set, we have a
6116      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
6117   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
6118     {
6119       inst.operands[i].preind = 1;
6120       inst.relocs[0].exp.X_op = O_constant;
6121       inst.relocs[0].exp.X_add_number = 0;
6122     }
6123   *str = p;
6124   return PARSE_OPERAND_SUCCESS;
6125 }
6126
6127 static int
6128 parse_address (char **str, int i)
6129 {
6130   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
6131          ? SUCCESS : FAIL;
6132 }
6133
6134 static parse_operand_result
6135 parse_address_group_reloc (char **str, int i, group_reloc_type type)
6136 {
6137   return parse_address_main (str, i, 1, type);
6138 }
6139
6140 /* Parse an operand for a MOVW or MOVT instruction.  */
6141 static int
6142 parse_half (char **str)
6143 {
6144   char * p;
6145
6146   p = *str;
6147   skip_past_char (&p, '#');
6148   if (strncasecmp (p, ":lower16:", 9) == 0)
6149     inst.relocs[0].type = BFD_RELOC_ARM_MOVW;
6150   else if (strncasecmp (p, ":upper16:", 9) == 0)
6151     inst.relocs[0].type = BFD_RELOC_ARM_MOVT;
6152
6153   if (inst.relocs[0].type != BFD_RELOC_UNUSED)
6154     {
6155       p += 9;
6156       skip_whitespace (p);
6157     }
6158
6159   if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
6160     return FAIL;
6161
6162   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
6163     {
6164       if (inst.relocs[0].exp.X_op != O_constant)
6165         {
6166           inst.error = _("constant expression expected");
6167           return FAIL;
6168         }
6169       if (inst.relocs[0].exp.X_add_number < 0
6170           || inst.relocs[0].exp.X_add_number > 0xffff)
6171         {
6172           inst.error = _("immediate value out of range");
6173           return FAIL;
6174         }
6175     }
6176   *str = p;
6177   return SUCCESS;
6178 }
6179
6180 /* Miscellaneous. */
6181
6182 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
6183    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
6184 static int
6185 parse_psr (char **str, bfd_boolean lhs)
6186 {
6187   char *p;
6188   unsigned long psr_field;
6189   const struct asm_psr *psr;
6190   char *start;
6191   bfd_boolean is_apsr = FALSE;
6192   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
6193
6194   /* PR gas/12698:  If the user has specified -march=all then m_profile will
6195      be TRUE, but we want to ignore it in this case as we are building for any
6196      CPU type, including non-m variants.  */
6197   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
6198     m_profile = FALSE;
6199
6200   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
6201      feature for ease of use and backwards compatibility.  */
6202   p = *str;
6203   if (strncasecmp (p, "SPSR", 4) == 0)
6204     {
6205       if (m_profile)
6206         goto unsupported_psr;
6207
6208       psr_field = SPSR_BIT;
6209     }
6210   else if (strncasecmp (p, "CPSR", 4) == 0)
6211     {
6212       if (m_profile)
6213         goto unsupported_psr;
6214
6215       psr_field = 0;
6216     }
6217   else if (strncasecmp (p, "APSR", 4) == 0)
6218     {
6219       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
6220          and ARMv7-R architecture CPUs.  */
6221       is_apsr = TRUE;
6222       psr_field = 0;
6223     }
6224   else if (m_profile)
6225     {
6226       start = p;
6227       do
6228         p++;
6229       while (ISALNUM (*p) || *p == '_');
6230
6231       if (strncasecmp (start, "iapsr", 5) == 0
6232           || strncasecmp (start, "eapsr", 5) == 0
6233           || strncasecmp (start, "xpsr", 4) == 0
6234           || strncasecmp (start, "psr", 3) == 0)
6235         p = start + strcspn (start, "rR") + 1;
6236
6237       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
6238                                                   p - start);
6239
6240       if (!psr)
6241         return FAIL;
6242
6243       /* If APSR is being written, a bitfield may be specified.  Note that
6244          APSR itself is handled above.  */
6245       if (psr->field <= 3)
6246         {
6247           psr_field = psr->field;
6248           is_apsr = TRUE;
6249           goto check_suffix;
6250         }
6251
6252       *str = p;
6253       /* M-profile MSR instructions have the mask field set to "10", except
6254          *PSR variants which modify APSR, which may use a different mask (and
6255          have been handled already).  Do that by setting the PSR_f field
6256          here.  */
6257       return psr->field | (lhs ? PSR_f : 0);
6258     }
6259   else
6260     goto unsupported_psr;
6261
6262   p += 4;
6263 check_suffix:
6264   if (*p == '_')
6265     {
6266       /* A suffix follows.  */
6267       p++;
6268       start = p;
6269
6270       do
6271         p++;
6272       while (ISALNUM (*p) || *p == '_');
6273
6274       if (is_apsr)
6275         {
6276           /* APSR uses a notation for bits, rather than fields.  */
6277           unsigned int nzcvq_bits = 0;
6278           unsigned int g_bit = 0;
6279           char *bit;
6280
6281           for (bit = start; bit != p; bit++)
6282             {
6283               switch (TOLOWER (*bit))
6284                 {
6285                 case 'n':
6286                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
6287                   break;
6288
6289                 case 'z':
6290                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
6291                   break;
6292
6293                 case 'c':
6294                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
6295                   break;
6296
6297                 case 'v':
6298                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
6299                   break;
6300
6301                 case 'q':
6302                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
6303                   break;
6304
6305                 case 'g':
6306                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
6307                   break;
6308
6309                 default:
6310                   inst.error = _("unexpected bit specified after APSR");
6311                   return FAIL;
6312                 }
6313             }
6314
6315           if (nzcvq_bits == 0x1f)
6316             psr_field |= PSR_f;
6317
6318           if (g_bit == 0x1)
6319             {
6320               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
6321                 {
6322                   inst.error = _("selected processor does not "
6323                                  "support DSP extension");
6324                   return FAIL;
6325                 }
6326
6327               psr_field |= PSR_s;
6328             }
6329
6330           if ((nzcvq_bits & 0x20) != 0
6331               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
6332               || (g_bit & 0x2) != 0)
6333             {
6334               inst.error = _("bad bitmask specified after APSR");
6335               return FAIL;
6336             }
6337         }
6338       else
6339         {
6340           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
6341                                                       p - start);
6342           if (!psr)
6343             goto error;
6344
6345           psr_field |= psr->field;
6346         }
6347     }
6348   else
6349     {
6350       if (ISALNUM (*p))
6351         goto error;    /* Garbage after "[CS]PSR".  */
6352
6353       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
6354          is deprecated, but allow it anyway.  */
6355       if (is_apsr && lhs)
6356         {
6357           psr_field |= PSR_f;
6358           as_tsktsk (_("writing to APSR without specifying a bitmask is "
6359                        "deprecated"));
6360         }
6361       else if (!m_profile)
6362         /* These bits are never right for M-profile devices: don't set them
6363            (only code paths which read/write APSR reach here).  */
6364         psr_field |= (PSR_c | PSR_f);
6365     }
6366   *str = p;
6367   return psr_field;
6368
6369  unsupported_psr:
6370   inst.error = _("selected processor does not support requested special "
6371                  "purpose register");
6372   return FAIL;
6373
6374  error:
6375   inst.error = _("flag for {c}psr instruction expected");
6376   return FAIL;
6377 }
6378
6379 static int
6380 parse_sys_vldr_vstr (char **str)
6381 {
6382   unsigned i;
6383   int val = FAIL;
6384   struct {
6385     const char *name;
6386     int regl;
6387     int regh;
6388   } sysregs[] = {
6389     {"FPSCR",           0x1, 0x0},
6390     {"FPSCR_nzcvqc",    0x2, 0x0},
6391     {"VPR",             0x4, 0x1},
6392     {"P0",              0x5, 0x1},
6393     {"FPCXTNS",         0x6, 0x1},
6394     {"FPCXTS",          0x7, 0x1}
6395   };
6396   char *op_end = strchr (*str, ',');
6397   size_t op_strlen = op_end - *str;
6398
6399   for (i = 0; i < sizeof (sysregs) / sizeof (sysregs[0]); i++)
6400     {
6401       if (!strncmp (*str, sysregs[i].name, op_strlen))
6402         {
6403           val = sysregs[i].regl | (sysregs[i].regh << 3);
6404           *str = op_end;
6405           break;
6406         }
6407     }
6408
6409   return val;
6410 }
6411
6412 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
6413    value suitable for splatting into the AIF field of the instruction.  */
6414
6415 static int
6416 parse_cps_flags (char **str)
6417 {
6418   int val = 0;
6419   int saw_a_flag = 0;
6420   char *s = *str;
6421
6422   for (;;)
6423     switch (*s++)
6424       {
6425       case '\0': case ',':
6426         goto done;
6427
6428       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6429       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6430       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6431
6432       default:
6433         inst.error = _("unrecognized CPS flag");
6434         return FAIL;
6435       }
6436
6437  done:
6438   if (saw_a_flag == 0)
6439     {
6440       inst.error = _("missing CPS flags");
6441       return FAIL;
6442     }
6443
6444   *str = s - 1;
6445   return val;
6446 }
6447
6448 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6449    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6450
6451 static int
6452 parse_endian_specifier (char **str)
6453 {
6454   int little_endian;
6455   char *s = *str;
6456
6457   if (strncasecmp (s, "BE", 2))
6458     little_endian = 0;
6459   else if (strncasecmp (s, "LE", 2))
6460     little_endian = 1;
6461   else
6462     {
6463       inst.error = _("valid endian specifiers are be or le");
6464       return FAIL;
6465     }
6466
6467   if (ISALNUM (s[2]) || s[2] == '_')
6468     {
6469       inst.error = _("valid endian specifiers are be or le");
6470       return FAIL;
6471     }
6472
6473   *str = s + 2;
6474   return little_endian;
6475 }
6476
6477 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6478    value suitable for poking into the rotate field of an sxt or sxta
6479    instruction, or FAIL on error.  */
6480
6481 static int
6482 parse_ror (char **str)
6483 {
6484   int rot;
6485   char *s = *str;
6486
6487   if (strncasecmp (s, "ROR", 3) == 0)
6488     s += 3;
6489   else
6490     {
6491       inst.error = _("missing rotation field after comma");
6492       return FAIL;
6493     }
6494
6495   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6496     return FAIL;
6497
6498   switch (rot)
6499     {
6500     case  0: *str = s; return 0x0;
6501     case  8: *str = s; return 0x1;
6502     case 16: *str = s; return 0x2;
6503     case 24: *str = s; return 0x3;
6504
6505     default:
6506       inst.error = _("rotation can only be 0, 8, 16, or 24");
6507       return FAIL;
6508     }
6509 }
6510
6511 /* Parse a conditional code (from conds[] below).  The value returned is in the
6512    range 0 .. 14, or FAIL.  */
6513 static int
6514 parse_cond (char **str)
6515 {
6516   char *q;
6517   const struct asm_cond *c;
6518   int n;
6519   /* Condition codes are always 2 characters, so matching up to
6520      3 characters is sufficient.  */
6521   char cond[3];
6522
6523   q = *str;
6524   n = 0;
6525   while (ISALPHA (*q) && n < 3)
6526     {
6527       cond[n] = TOLOWER (*q);
6528       q++;
6529       n++;
6530     }
6531
6532   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6533   if (!c)
6534     {
6535       inst.error = _("condition required");
6536       return FAIL;
6537     }
6538
6539   *str = q;
6540   return c->value;
6541 }
6542
6543 /* Parse an option for a barrier instruction.  Returns the encoding for the
6544    option, or FAIL.  */
6545 static int
6546 parse_barrier (char **str)
6547 {
6548   char *p, *q;
6549   const struct asm_barrier_opt *o;
6550
6551   p = q = *str;
6552   while (ISALPHA (*q))
6553     q++;
6554
6555   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6556                                                     q - p);
6557   if (!o)
6558     return FAIL;
6559
6560   if (!mark_feature_used (&o->arch))
6561     return FAIL;
6562
6563   *str = q;
6564   return o->value;
6565 }
6566
6567 /* Parse the operands of a table branch instruction.  Similar to a memory
6568    operand.  */
6569 static int
6570 parse_tb (char **str)
6571 {
6572   char * p = *str;
6573   int reg;
6574
6575   if (skip_past_char (&p, '[') == FAIL)
6576     {
6577       inst.error = _("'[' expected");
6578       return FAIL;
6579     }
6580
6581   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6582     {
6583       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6584       return FAIL;
6585     }
6586   inst.operands[0].reg = reg;
6587
6588   if (skip_past_comma (&p) == FAIL)
6589     {
6590       inst.error = _("',' expected");
6591       return FAIL;
6592     }
6593
6594   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6595     {
6596       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6597       return FAIL;
6598     }
6599   inst.operands[0].imm = reg;
6600
6601   if (skip_past_comma (&p) == SUCCESS)
6602     {
6603       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6604         return FAIL;
6605       if (inst.relocs[0].exp.X_add_number != 1)
6606         {
6607           inst.error = _("invalid shift");
6608           return FAIL;
6609         }
6610       inst.operands[0].shifted = 1;
6611     }
6612
6613   if (skip_past_char (&p, ']') == FAIL)
6614     {
6615       inst.error = _("']' expected");
6616       return FAIL;
6617     }
6618   *str = p;
6619   return SUCCESS;
6620 }
6621
6622 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6623    information on the types the operands can take and how they are encoded.
6624    Up to four operands may be read; this function handles setting the
6625    ".present" field for each read operand itself.
6626    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6627    else returns FAIL.  */
6628
6629 static int
6630 parse_neon_mov (char **str, int *which_operand)
6631 {
6632   int i = *which_operand, val;
6633   enum arm_reg_type rtype;
6634   char *ptr = *str;
6635   struct neon_type_el optype;
6636
6637    if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6638     {
6639       /* Cases 17 or 19.  */
6640       inst.operands[i].reg = val;
6641       inst.operands[i].isvec = 1;
6642       inst.operands[i].isscalar = 2;
6643       inst.operands[i].vectype = optype;
6644       inst.operands[i++].present = 1;
6645
6646       if (skip_past_comma (&ptr) == FAIL)
6647         goto wanted_comma;
6648
6649       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6650         {
6651           /* Case 17: VMOV<c>.<dt> <Qd[idx]>, <Rt>  */
6652           inst.operands[i].reg = val;
6653           inst.operands[i].isreg = 1;
6654           inst.operands[i].present = 1;
6655         }
6656       else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6657         {
6658           /* Case 19: VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>  */
6659           inst.operands[i].reg = val;
6660           inst.operands[i].isvec = 1;
6661           inst.operands[i].isscalar = 2;
6662           inst.operands[i].vectype = optype;
6663           inst.operands[i++].present = 1;
6664
6665           if (skip_past_comma (&ptr) == FAIL)
6666             goto wanted_comma;
6667
6668           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6669             goto wanted_arm;
6670
6671           inst.operands[i].reg = val;
6672           inst.operands[i].isreg = 1;
6673           inst.operands[i++].present = 1;
6674
6675           if (skip_past_comma (&ptr) == FAIL)
6676             goto wanted_comma;
6677
6678           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6679             goto wanted_arm;
6680
6681           inst.operands[i].reg = val;
6682           inst.operands[i].isreg = 1;
6683           inst.operands[i].present = 1;
6684         }
6685       else
6686         {
6687           first_error (_("expected ARM or MVE vector register"));
6688           return FAIL;
6689         }
6690     }
6691    else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_VFD)) != FAIL)
6692     {
6693       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6694       inst.operands[i].reg = val;
6695       inst.operands[i].isscalar = 1;
6696       inst.operands[i].vectype = optype;
6697       inst.operands[i++].present = 1;
6698
6699       if (skip_past_comma (&ptr) == FAIL)
6700         goto wanted_comma;
6701
6702       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6703         goto wanted_arm;
6704
6705       inst.operands[i].reg = val;
6706       inst.operands[i].isreg = 1;
6707       inst.operands[i].present = 1;
6708     }
6709   else if (((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6710             != FAIL)
6711            || ((val = arm_typed_reg_parse (&ptr, REG_TYPE_MQ, &rtype, &optype))
6712                != FAIL))
6713     {
6714       /* Cases 0, 1, 2, 3, 5 (D only).  */
6715       if (skip_past_comma (&ptr) == FAIL)
6716         goto wanted_comma;
6717
6718       inst.operands[i].reg = val;
6719       inst.operands[i].isreg = 1;
6720       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6721       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6722       inst.operands[i].isvec = 1;
6723       inst.operands[i].vectype = optype;
6724       inst.operands[i++].present = 1;
6725
6726       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6727         {
6728           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6729              Case 13: VMOV <Sd>, <Rm>  */
6730           inst.operands[i].reg = val;
6731           inst.operands[i].isreg = 1;
6732           inst.operands[i].present = 1;
6733
6734           if (rtype == REG_TYPE_NQ)
6735             {
6736               first_error (_("can't use Neon quad register here"));
6737               return FAIL;
6738             }
6739           else if (rtype != REG_TYPE_VFS)
6740             {
6741               i++;
6742               if (skip_past_comma (&ptr) == FAIL)
6743                 goto wanted_comma;
6744               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6745                 goto wanted_arm;
6746               inst.operands[i].reg = val;
6747               inst.operands[i].isreg = 1;
6748               inst.operands[i].present = 1;
6749             }
6750         }
6751       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6752                                            &optype)) != FAIL)
6753         {
6754           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6755              Case 1: VMOV<c><q> <Dd>, <Dm>
6756              Case 8: VMOV.F32 <Sd>, <Sm>
6757              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6758
6759           inst.operands[i].reg = val;
6760           inst.operands[i].isreg = 1;
6761           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6762           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6763           inst.operands[i].isvec = 1;
6764           inst.operands[i].vectype = optype;
6765           inst.operands[i].present = 1;
6766
6767           if (skip_past_comma (&ptr) == SUCCESS)
6768             {
6769               /* Case 15.  */
6770               i++;
6771
6772               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6773                 goto wanted_arm;
6774
6775               inst.operands[i].reg = val;
6776               inst.operands[i].isreg = 1;
6777               inst.operands[i++].present = 1;
6778
6779               if (skip_past_comma (&ptr) == FAIL)
6780                 goto wanted_comma;
6781
6782               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6783                 goto wanted_arm;
6784
6785               inst.operands[i].reg = val;
6786               inst.operands[i].isreg = 1;
6787               inst.operands[i].present = 1;
6788             }
6789         }
6790       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6791           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6792              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6793              Case 10: VMOV.F32 <Sd>, #<imm>
6794              Case 11: VMOV.F64 <Dd>, #<imm>  */
6795         inst.operands[i].immisfloat = 1;
6796       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6797                == SUCCESS)
6798           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6799              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6800         ;
6801       else
6802         {
6803           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6804           return FAIL;
6805         }
6806     }
6807   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6808     {
6809       /* Cases 6, 7, 16, 18.  */
6810       inst.operands[i].reg = val;
6811       inst.operands[i].isreg = 1;
6812       inst.operands[i++].present = 1;
6813
6814       if (skip_past_comma (&ptr) == FAIL)
6815         goto wanted_comma;
6816
6817       if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6818         {
6819           /* Case 18: VMOV<c>.<dt> <Rt>, <Qn[idx]>  */
6820           inst.operands[i].reg = val;
6821           inst.operands[i].isscalar = 2;
6822           inst.operands[i].present = 1;
6823           inst.operands[i].vectype = optype;
6824         }
6825       else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_VFD)) != FAIL)
6826         {
6827           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6828           inst.operands[i].reg = val;
6829           inst.operands[i].isscalar = 1;
6830           inst.operands[i].present = 1;
6831           inst.operands[i].vectype = optype;
6832         }
6833       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6834         {
6835           inst.operands[i].reg = val;
6836           inst.operands[i].isreg = 1;
6837           inst.operands[i++].present = 1;
6838
6839           if (skip_past_comma (&ptr) == FAIL)
6840             goto wanted_comma;
6841
6842           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6843               != FAIL)
6844             {
6845               /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6846
6847               inst.operands[i].reg = val;
6848               inst.operands[i].isreg = 1;
6849               inst.operands[i].isvec = 1;
6850               inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6851               inst.operands[i].vectype = optype;
6852               inst.operands[i].present = 1;
6853
6854               if (rtype == REG_TYPE_VFS)
6855                 {
6856                   /* Case 14.  */
6857                   i++;
6858                   if (skip_past_comma (&ptr) == FAIL)
6859                     goto wanted_comma;
6860                   if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6861                                                   &optype)) == FAIL)
6862                     {
6863                       first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6864                       return FAIL;
6865                     }
6866                   inst.operands[i].reg = val;
6867                   inst.operands[i].isreg = 1;
6868                   inst.operands[i].isvec = 1;
6869                   inst.operands[i].issingle = 1;
6870                   inst.operands[i].vectype = optype;
6871                   inst.operands[i].present = 1;
6872                 }
6873             }
6874           else
6875             {
6876               if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ))
6877                        != FAIL)
6878                 {
6879                   /* Case 16: VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>  */
6880                   inst.operands[i].reg = val;
6881                   inst.operands[i].isvec = 1;
6882                   inst.operands[i].isscalar = 2;
6883                   inst.operands[i].vectype = optype;
6884                   inst.operands[i++].present = 1;
6885
6886                   if (skip_past_comma (&ptr) == FAIL)
6887                     goto wanted_comma;
6888
6889                   if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ))
6890                       == FAIL)
6891                     {
6892                       first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
6893                       return FAIL;
6894                     }
6895                   inst.operands[i].reg = val;
6896                   inst.operands[i].isvec = 1;
6897                   inst.operands[i].isscalar = 2;
6898                   inst.operands[i].vectype = optype;
6899                   inst.operands[i].present = 1;
6900                 }
6901               else
6902                 {
6903                   first_error (_("VFP single, double or MVE vector register"
6904                                " expected"));
6905                   return FAIL;
6906                 }
6907             }
6908         }
6909       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6910                != FAIL)
6911         {
6912           /* Case 13.  */
6913           inst.operands[i].reg = val;
6914           inst.operands[i].isreg = 1;
6915           inst.operands[i].isvec = 1;
6916           inst.operands[i].issingle = 1;
6917           inst.operands[i].vectype = optype;
6918           inst.operands[i].present = 1;
6919         }
6920     }
6921   else
6922     {
6923       first_error (_("parse error"));
6924       return FAIL;
6925     }
6926
6927   /* Successfully parsed the operands. Update args.  */
6928   *which_operand = i;
6929   *str = ptr;
6930   return SUCCESS;
6931
6932  wanted_comma:
6933   first_error (_("expected comma"));
6934   return FAIL;
6935
6936  wanted_arm:
6937   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6938   return FAIL;
6939 }
6940
6941 /* Use this macro when the operand constraints are different
6942    for ARM and THUMB (e.g. ldrd).  */
6943 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6944         ((arm_operand) | ((thumb_operand) << 16))
6945
6946 /* Matcher codes for parse_operands.  */
6947 enum operand_parse_code
6948 {
6949   OP_stop,      /* end of line */
6950
6951   OP_RR,        /* ARM register */
6952   OP_RRnpc,     /* ARM register, not r15 */
6953   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6954   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6955   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6956                    optional trailing ! */
6957   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6958   OP_RCP,       /* Coprocessor number */
6959   OP_RCN,       /* Coprocessor register */
6960   OP_RF,        /* FPA register */
6961   OP_RVS,       /* VFP single precision register */
6962   OP_RVD,       /* VFP double precision register (0..15) */
6963   OP_RND,       /* Neon double precision register (0..31) */
6964   OP_RNDMQ,     /* Neon double precision (0..31) or MVE vector register.  */
6965   OP_RNDMQR,    /* Neon double precision (0..31), MVE vector or ARM register.
6966                  */
6967   OP_RNQ,       /* Neon quad precision register */
6968   OP_RNQMQ,     /* Neon quad or MVE vector register.  */
6969   OP_RVSD,      /* VFP single or double precision register */
6970   OP_RVSD_COND, /* VFP single, double precision register or condition code.  */
6971   OP_RVSDMQ,    /* VFP single, double precision or MVE vector register.  */
6972   OP_RNSD,      /* Neon single or double precision register */
6973   OP_RNDQ,      /* Neon double or quad precision register */
6974   OP_RNDQMQ,     /* Neon double, quad or MVE vector register.  */
6975   OP_RNDQMQR,   /* Neon double, quad, MVE vector or ARM register.  */
6976   OP_RNSDQ,     /* Neon single, double or quad precision register */
6977   OP_RNSC,      /* Neon scalar D[X] */
6978   OP_RVC,       /* VFP control register */
6979   OP_RMF,       /* Maverick F register */
6980   OP_RMD,       /* Maverick D register */
6981   OP_RMFX,      /* Maverick FX register */
6982   OP_RMDX,      /* Maverick DX register */
6983   OP_RMAX,      /* Maverick AX register */
6984   OP_RMDS,      /* Maverick DSPSC register */
6985   OP_RIWR,      /* iWMMXt wR register */
6986   OP_RIWC,      /* iWMMXt wC register */
6987   OP_RIWG,      /* iWMMXt wCG register */
6988   OP_RXA,       /* XScale accumulator register */
6989
6990   OP_RNSDQMQ,   /* Neon single, double or quad register or MVE vector register
6991                  */
6992   OP_RNSDQMQR,  /* Neon single, double or quad register, MVE vector register or
6993                    GPR (no SP/SP)  */
6994   OP_RMQ,       /* MVE vector register.  */
6995   OP_RMQRZ,     /* MVE vector or ARM register including ZR.  */
6996   OP_RMQRR,     /* MVE vector or ARM register.  */
6997
6998   /* New operands for Armv8.1-M Mainline.  */
6999   OP_LR,        /* ARM LR register */
7000   OP_RRe,       /* ARM register, only even numbered.  */
7001   OP_RRo,       /* ARM register, only odd numbered, not r13 or r15.  */
7002   OP_RRnpcsp_I32, /* ARM register (no BadReg) or literal 1 .. 32 */
7003   OP_RR_ZR,     /* ARM register or ZR but no PC */
7004
7005   OP_REGLST,    /* ARM register list */
7006   OP_CLRMLST,   /* CLRM register list */
7007   OP_VRSLST,    /* VFP single-precision register list */
7008   OP_VRDLST,    /* VFP double-precision register list */
7009   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
7010   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
7011   OP_NSTRLST,   /* Neon element/structure list */
7012   OP_VRSDVLST,  /* VFP single or double-precision register list and VPR */
7013   OP_MSTRLST2,  /* MVE vector list with two elements.  */
7014   OP_MSTRLST4,  /* MVE vector list with four elements.  */
7015
7016   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
7017   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
7018   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
7019   OP_RSVDMQ_FI0, /* VFP S, D, MVE vector register or floating point immediate
7020                     zero.  */
7021   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
7022   OP_RNSD_RNSC, /* Neon S or D reg, or Neon scalar.  */
7023   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
7024   OP_RNSDQ_RNSC_MQ, /* Vector S, D or Q reg, Neon scalar or MVE vector register.
7025                      */
7026   OP_RNSDQ_RNSC_MQ_RR, /* Vector S, D or Q reg, or MVE vector reg , or Neon
7027                           scalar, or ARM register.  */
7028   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
7029   OP_RNDQ_RNSC_RR, /* Neon D or Q reg, Neon scalar, or ARM register.  */
7030   OP_RNDQMQ_RNSC_RR, /* Neon D or Q reg, Neon scalar, MVE vector or ARM
7031                         register.  */
7032   OP_RNDQMQ_RNSC, /* Neon D, Q or MVE vector reg, or Neon scalar.  */
7033   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
7034   OP_VMOV,      /* Neon VMOV operands.  */
7035   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
7036   /* Neon D, Q or MVE vector register, or big immediate for logic and VMVN.  */
7037   OP_RNDQMQ_Ibig,
7038   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
7039   OP_RNDQMQ_I63b_RR, /* Neon D or Q reg, immediate for shift, MVE vector or
7040                         ARM register.  */
7041   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
7042   OP_VLDR,      /* VLDR operand.  */
7043
7044   OP_I0,        /* immediate zero */
7045   OP_I7,        /* immediate value 0 .. 7 */
7046   OP_I15,       /*                 0 .. 15 */
7047   OP_I16,       /*                 1 .. 16 */
7048   OP_I16z,      /*                 0 .. 16 */
7049   OP_I31,       /*                 0 .. 31 */
7050   OP_I31w,      /*                 0 .. 31, optional trailing ! */
7051   OP_I32,       /*                 1 .. 32 */
7052   OP_I32z,      /*                 0 .. 32 */
7053   OP_I63,       /*                 0 .. 63 */
7054   OP_I63s,      /*               -64 .. 63 */
7055   OP_I64,       /*                 1 .. 64 */
7056   OP_I64z,      /*                 0 .. 64 */
7057   OP_I255,      /*                 0 .. 255 */
7058
7059   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
7060   OP_I7b,       /*                             0 .. 7 */
7061   OP_I15b,      /*                             0 .. 15 */
7062   OP_I31b,      /*                             0 .. 31 */
7063
7064   OP_SH,        /* shifter operand */
7065   OP_SHG,       /* shifter operand with possible group relocation */
7066   OP_ADDR,      /* Memory address expression (any mode) */
7067   OP_ADDRMVE,   /* Memory address expression for MVE's VSTR/VLDR.  */
7068   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
7069   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
7070   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
7071   OP_EXP,       /* arbitrary expression */
7072   OP_EXPi,      /* same, with optional immediate prefix */
7073   OP_EXPr,      /* same, with optional relocation suffix */
7074   OP_EXPs,      /* same, with optional non-first operand relocation suffix */
7075   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
7076   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
7077   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
7078
7079   OP_CPSF,      /* CPS flags */
7080   OP_ENDI,      /* Endianness specifier */
7081   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
7082   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
7083   OP_COND,      /* conditional code */
7084   OP_TB,        /* Table branch.  */
7085
7086   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
7087
7088   OP_RRnpc_I0,  /* ARM register or literal 0 */
7089   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
7090   OP_RR_EXi,    /* ARM register or expression with imm prefix */
7091   OP_RF_IF,     /* FPA register or immediate */
7092   OP_RIWR_RIWC, /* iWMMXt R or C reg */
7093   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
7094
7095   /* Optional operands.  */
7096   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
7097   OP_oI31b,      /*                             0 .. 31 */
7098   OP_oI32b,      /*                             1 .. 32 */
7099   OP_oI32z,      /*                             0 .. 32 */
7100   OP_oIffffb,    /*                             0 .. 65535 */
7101   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
7102
7103   OP_oRR,        /* ARM register */
7104   OP_oLR,        /* ARM LR register */
7105   OP_oRRnpc,     /* ARM register, not the PC */
7106   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
7107   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
7108   OP_oRND,       /* Optional Neon double precision register */
7109   OP_oRNQ,       /* Optional Neon quad precision register */
7110   OP_oRNDQMQ,     /* Optional Neon double, quad or MVE vector register.  */
7111   OP_oRNDQ,      /* Optional Neon double or quad precision register */
7112   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
7113   OP_oRNSDQMQ,   /* Optional single, double or quad register or MVE vector
7114                     register.  */
7115   OP_oSHll,      /* LSL immediate */
7116   OP_oSHar,      /* ASR immediate */
7117   OP_oSHllar,    /* LSL or ASR immediate */
7118   OP_oROR,       /* ROR 0/8/16/24 */
7119   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
7120
7121   OP_oRMQRZ,    /* optional MVE vector or ARM register including ZR.  */
7122
7123   /* Some pre-defined mixed (ARM/THUMB) operands.  */
7124   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
7125   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
7126   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
7127
7128   OP_FIRST_OPTIONAL = OP_oI7b
7129 };
7130
7131 /* Generic instruction operand parser.  This does no encoding and no
7132    semantic validation; it merely squirrels values away in the inst
7133    structure.  Returns SUCCESS or FAIL depending on whether the
7134    specified grammar matched.  */
7135 static int
7136 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
7137 {
7138   unsigned const int *upat = pattern;
7139   char *backtrack_pos = 0;
7140   const char *backtrack_error = 0;
7141   int i, val = 0, backtrack_index = 0;
7142   enum arm_reg_type rtype;
7143   parse_operand_result result;
7144   unsigned int op_parse_code;
7145   bfd_boolean partial_match;
7146
7147 #define po_char_or_fail(chr)                    \
7148   do                                            \
7149     {                                           \
7150       if (skip_past_char (&str, chr) == FAIL)   \
7151         goto bad_args;                          \
7152     }                                           \
7153   while (0)
7154
7155 #define po_reg_or_fail(regtype)                                 \
7156   do                                                            \
7157     {                                                           \
7158       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7159                                  & inst.operands[i].vectype);   \
7160       if (val == FAIL)                                          \
7161         {                                                       \
7162           first_error (_(reg_expected_msgs[regtype]));          \
7163           goto failure;                                         \
7164         }                                                       \
7165       inst.operands[i].reg = val;                               \
7166       inst.operands[i].isreg = 1;                               \
7167       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7168       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7169       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7170                              || rtype == REG_TYPE_VFD           \
7171                              || rtype == REG_TYPE_NQ);          \
7172       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7173     }                                                           \
7174   while (0)
7175
7176 #define po_reg_or_goto(regtype, label)                          \
7177   do                                                            \
7178     {                                                           \
7179       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7180                                  & inst.operands[i].vectype);   \
7181       if (val == FAIL)                                          \
7182         goto label;                                             \
7183                                                                 \
7184       inst.operands[i].reg = val;                               \
7185       inst.operands[i].isreg = 1;                               \
7186       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7187       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7188       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7189                              || rtype == REG_TYPE_VFD           \
7190                              || rtype == REG_TYPE_NQ);          \
7191       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7192     }                                                           \
7193   while (0)
7194
7195 #define po_imm_or_fail(min, max, popt)                          \
7196   do                                                            \
7197     {                                                           \
7198       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
7199         goto failure;                                           \
7200       inst.operands[i].imm = val;                               \
7201     }                                                           \
7202   while (0)
7203
7204 #define po_scalar_or_goto(elsz, label, reg_type)                        \
7205   do                                                                    \
7206     {                                                                   \
7207       val = parse_scalar (& str, elsz, & inst.operands[i].vectype,      \
7208                           reg_type);                                    \
7209       if (val == FAIL)                                                  \
7210         goto label;                                                     \
7211       inst.operands[i].reg = val;                                       \
7212       inst.operands[i].isscalar = 1;                                    \
7213     }                                                                   \
7214   while (0)
7215
7216 #define po_misc_or_fail(expr)                   \
7217   do                                            \
7218     {                                           \
7219       if (expr)                                 \
7220         goto failure;                           \
7221     }                                           \
7222   while (0)
7223
7224 #define po_misc_or_fail_no_backtrack(expr)              \
7225   do                                                    \
7226     {                                                   \
7227       result = expr;                                    \
7228       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
7229         backtrack_pos = 0;                              \
7230       if (result != PARSE_OPERAND_SUCCESS)              \
7231         goto failure;                                   \
7232     }                                                   \
7233   while (0)
7234
7235 #define po_barrier_or_imm(str)                             \
7236   do                                                       \
7237     {                                                      \
7238       val = parse_barrier (&str);                          \
7239       if (val == FAIL && ! ISALPHA (*str))                 \
7240         goto immediate;                                    \
7241       if (val == FAIL                                      \
7242           /* ISB can only take SY as an option.  */        \
7243           || ((inst.instruction & 0xf0) == 0x60            \
7244                && val != 0xf))                             \
7245         {                                                  \
7246            inst.error = _("invalid barrier type");         \
7247            backtrack_pos = 0;                              \
7248            goto failure;                                   \
7249         }                                                  \
7250     }                                                      \
7251   while (0)
7252
7253   skip_whitespace (str);
7254
7255   for (i = 0; upat[i] != OP_stop; i++)
7256     {
7257       op_parse_code = upat[i];
7258       if (op_parse_code >= 1<<16)
7259         op_parse_code = thumb ? (op_parse_code >> 16)
7260                                 : (op_parse_code & ((1<<16)-1));
7261
7262       if (op_parse_code >= OP_FIRST_OPTIONAL)
7263         {
7264           /* Remember where we are in case we need to backtrack.  */
7265           backtrack_pos = str;
7266           backtrack_error = inst.error;
7267           backtrack_index = i;
7268         }
7269
7270       if (i > 0 && (i > 1 || inst.operands[0].present))
7271         po_char_or_fail (',');
7272
7273       switch (op_parse_code)
7274         {
7275           /* Registers */
7276         case OP_oRRnpc:
7277         case OP_oRRnpcsp:
7278         case OP_RRnpc:
7279         case OP_RRnpcsp:
7280         case OP_oRR:
7281         case OP_RRe:
7282         case OP_RRo:
7283         case OP_LR:
7284         case OP_oLR:
7285         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
7286         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
7287         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
7288         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
7289         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
7290         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
7291         case OP_oRND:
7292         case OP_RNDMQR:
7293           po_reg_or_goto (REG_TYPE_RN, try_rndmq);
7294           break;
7295         try_rndmq:
7296         case OP_RNDMQ:
7297           po_reg_or_goto (REG_TYPE_MQ, try_rnd);
7298           break;
7299         try_rnd:
7300         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
7301         case OP_RVC:
7302           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
7303           break;
7304           /* Also accept generic coprocessor regs for unknown registers.  */
7305           coproc_reg:
7306           po_reg_or_goto (REG_TYPE_CN, vpr_po);
7307           break;
7308           /* Also accept P0 or p0 for VPR.P0.  Since P0 is already an
7309              existing register with a value of 0, this seems like the
7310              best way to parse P0.  */
7311           vpr_po:
7312           if (strncasecmp (str, "P0", 2) == 0)
7313             {
7314               str += 2;
7315               inst.operands[i].isreg = 1;
7316               inst.operands[i].reg = 13;
7317             }
7318           else
7319             goto failure;
7320           break;
7321         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
7322         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
7323         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
7324         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
7325         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
7326         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
7327         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
7328         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
7329         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
7330         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
7331         case OP_oRNQ:
7332         case OP_RNQMQ:
7333           po_reg_or_goto (REG_TYPE_MQ, try_nq);
7334           break;
7335         try_nq:
7336         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
7337         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
7338         case OP_RNDQMQR:
7339           po_reg_or_goto (REG_TYPE_RN, try_rndqmq);
7340           break;
7341         try_rndqmq:
7342         case OP_oRNDQMQ:
7343         case OP_RNDQMQ:
7344           po_reg_or_goto (REG_TYPE_MQ, try_rndq);
7345           break;
7346         try_rndq:
7347         case OP_oRNDQ:
7348         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
7349         case OP_RVSDMQ:
7350           po_reg_or_goto (REG_TYPE_MQ, try_rvsd);
7351           break;
7352         try_rvsd:
7353         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
7354         case OP_RVSD_COND:
7355           po_reg_or_goto (REG_TYPE_VFSD, try_cond);
7356           break;
7357         case OP_oRNSDQ:
7358         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
7359         case OP_RNSDQMQR:
7360           po_reg_or_goto (REG_TYPE_RN, try_mq);
7361           break;
7362           try_mq:
7363         case OP_oRNSDQMQ:
7364         case OP_RNSDQMQ:
7365           po_reg_or_goto (REG_TYPE_MQ, try_nsdq2);
7366           break;
7367           try_nsdq2:
7368           po_reg_or_fail (REG_TYPE_NSDQ);
7369           inst.error = 0;
7370           break;
7371         case OP_RMQRR:
7372           po_reg_or_goto (REG_TYPE_RN, try_rmq);
7373           break;
7374         try_rmq:
7375         case OP_RMQ:
7376           po_reg_or_fail (REG_TYPE_MQ);
7377           break;
7378         /* Neon scalar. Using an element size of 8 means that some invalid
7379            scalars are accepted here, so deal with those in later code.  */
7380         case OP_RNSC:  po_scalar_or_goto (8, failure, REG_TYPE_VFD);    break;
7381
7382         case OP_RNDQ_I0:
7383           {
7384             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
7385             break;
7386             try_imm0:
7387             po_imm_or_fail (0, 0, TRUE);
7388           }
7389           break;
7390
7391         case OP_RVSD_I0:
7392           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
7393           break;
7394
7395         case OP_RSVDMQ_FI0:
7396           po_reg_or_goto (REG_TYPE_MQ, try_rsvd_fi0);
7397           break;
7398         try_rsvd_fi0:
7399         case OP_RSVD_FI0:
7400           {
7401             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
7402             break;
7403             try_ifimm0:
7404             if (parse_ifimm_zero (&str))
7405               inst.operands[i].imm = 0;
7406             else
7407             {
7408               inst.error
7409                 = _("only floating point zero is allowed as immediate value");
7410               goto failure;
7411             }
7412           }
7413           break;
7414
7415         case OP_RR_RNSC:
7416           {
7417             po_scalar_or_goto (8, try_rr, REG_TYPE_VFD);
7418             break;
7419             try_rr:
7420             po_reg_or_fail (REG_TYPE_RN);
7421           }
7422           break;
7423
7424         case OP_RNSDQ_RNSC_MQ_RR:
7425           po_reg_or_goto (REG_TYPE_RN, try_rnsdq_rnsc_mq);
7426           break;
7427         try_rnsdq_rnsc_mq:
7428         case OP_RNSDQ_RNSC_MQ:
7429           po_reg_or_goto (REG_TYPE_MQ, try_rnsdq_rnsc);
7430           break;
7431         try_rnsdq_rnsc:
7432         case OP_RNSDQ_RNSC:
7433           {
7434             po_scalar_or_goto (8, try_nsdq, REG_TYPE_VFD);
7435             inst.error = 0;
7436             break;
7437             try_nsdq:
7438             po_reg_or_fail (REG_TYPE_NSDQ);
7439             inst.error = 0;
7440           }
7441           break;
7442
7443         case OP_RNSD_RNSC:
7444           {
7445             po_scalar_or_goto (8, try_s_scalar, REG_TYPE_VFD);
7446             break;
7447             try_s_scalar:
7448             po_scalar_or_goto (4, try_nsd, REG_TYPE_VFS);
7449             break;
7450             try_nsd:
7451             po_reg_or_fail (REG_TYPE_NSD);
7452           }
7453           break;
7454
7455         case OP_RNDQMQ_RNSC_RR:
7456           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc_rr);
7457           break;
7458         try_rndq_rnsc_rr:
7459         case OP_RNDQ_RNSC_RR:
7460           po_reg_or_goto (REG_TYPE_RN, try_rndq_rnsc);
7461           break;
7462         case OP_RNDQMQ_RNSC:
7463           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc);
7464           break;
7465         try_rndq_rnsc:
7466         case OP_RNDQ_RNSC:
7467           {
7468             po_scalar_or_goto (8, try_ndq, REG_TYPE_VFD);
7469             break;
7470             try_ndq:
7471             po_reg_or_fail (REG_TYPE_NDQ);
7472           }
7473           break;
7474
7475         case OP_RND_RNSC:
7476           {
7477             po_scalar_or_goto (8, try_vfd, REG_TYPE_VFD);
7478             break;
7479             try_vfd:
7480             po_reg_or_fail (REG_TYPE_VFD);
7481           }
7482           break;
7483
7484         case OP_VMOV:
7485           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
7486              not careful then bad things might happen.  */
7487           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
7488           break;
7489
7490         case OP_RNDQMQ_Ibig:
7491           po_reg_or_goto (REG_TYPE_MQ, try_rndq_ibig);
7492           break;
7493         try_rndq_ibig:
7494         case OP_RNDQ_Ibig:
7495           {
7496             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
7497             break;
7498             try_immbig:
7499             /* There's a possibility of getting a 64-bit immediate here, so
7500                we need special handling.  */
7501             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
7502                 == FAIL)
7503               {
7504                 inst.error = _("immediate value is out of range");
7505                 goto failure;
7506               }
7507           }
7508           break;
7509
7510         case OP_RNDQMQ_I63b_RR:
7511           po_reg_or_goto (REG_TYPE_MQ, try_rndq_i63b_rr);
7512           break;
7513         try_rndq_i63b_rr:
7514           po_reg_or_goto (REG_TYPE_RN, try_rndq_i63b);
7515           break;
7516         try_rndq_i63b:
7517         case OP_RNDQ_I63b:
7518           {
7519             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
7520             break;
7521             try_shimm:
7522             po_imm_or_fail (0, 63, TRUE);
7523           }
7524           break;
7525
7526         case OP_RRnpcb:
7527           po_char_or_fail ('[');
7528           po_reg_or_fail  (REG_TYPE_RN);
7529           po_char_or_fail (']');
7530           break;
7531
7532         case OP_RRnpctw:
7533         case OP_RRw:
7534         case OP_oRRw:
7535           po_reg_or_fail (REG_TYPE_RN);
7536           if (skip_past_char (&str, '!') == SUCCESS)
7537             inst.operands[i].writeback = 1;
7538           break;
7539
7540           /* Immediates */
7541         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
7542         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
7543         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
7544         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
7545         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
7546         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
7547         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
7548         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
7549         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
7550         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
7551         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
7552         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
7553
7554         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
7555         case OP_oI7b:
7556         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
7557         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
7558         case OP_oI31b:
7559         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
7560         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
7561         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
7562         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
7563
7564           /* Immediate variants */
7565         case OP_oI255c:
7566           po_char_or_fail ('{');
7567           po_imm_or_fail (0, 255, TRUE);
7568           po_char_or_fail ('}');
7569           break;
7570
7571         case OP_I31w:
7572           /* The expression parser chokes on a trailing !, so we have
7573              to find it first and zap it.  */
7574           {
7575             char *s = str;
7576             while (*s && *s != ',')
7577               s++;
7578             if (s[-1] == '!')
7579               {
7580                 s[-1] = '\0';
7581                 inst.operands[i].writeback = 1;
7582               }
7583             po_imm_or_fail (0, 31, TRUE);
7584             if (str == s - 1)
7585               str = s;
7586           }
7587           break;
7588
7589           /* Expressions */
7590         case OP_EXPi:   EXPi:
7591           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7592                                               GE_OPT_PREFIX));
7593           break;
7594
7595         case OP_EXP:
7596           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7597                                               GE_NO_PREFIX));
7598           break;
7599
7600         case OP_EXPr:   EXPr:
7601           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7602                                               GE_NO_PREFIX));
7603           if (inst.relocs[0].exp.X_op == O_symbol)
7604             {
7605               val = parse_reloc (&str);
7606               if (val == -1)
7607                 {
7608                   inst.error = _("unrecognized relocation suffix");
7609                   goto failure;
7610                 }
7611               else if (val != BFD_RELOC_UNUSED)
7612                 {
7613                   inst.operands[i].imm = val;
7614                   inst.operands[i].hasreloc = 1;
7615                 }
7616             }
7617           break;
7618
7619         case OP_EXPs:
7620           po_misc_or_fail (my_get_expression (&inst.relocs[i].exp, &str,
7621                                               GE_NO_PREFIX));
7622           if (inst.relocs[i].exp.X_op == O_symbol)
7623             {
7624               inst.operands[i].hasreloc = 1;
7625             }
7626           else if (inst.relocs[i].exp.X_op == O_constant)
7627             {
7628               inst.operands[i].imm = inst.relocs[i].exp.X_add_number;
7629               inst.operands[i].hasreloc = 0;
7630             }
7631           break;
7632
7633           /* Operand for MOVW or MOVT.  */
7634         case OP_HALF:
7635           po_misc_or_fail (parse_half (&str));
7636           break;
7637
7638           /* Register or expression.  */
7639         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7640         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7641
7642           /* Register or immediate.  */
7643         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7644         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7645
7646         case OP_RRnpcsp_I32: po_reg_or_goto (REG_TYPE_RN, I32); break;
7647         I32:                 po_imm_or_fail (1, 32, FALSE);     break;
7648
7649         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7650         IF:
7651           if (!is_immediate_prefix (*str))
7652             goto bad_args;
7653           str++;
7654           val = parse_fpa_immediate (&str);
7655           if (val == FAIL)
7656             goto failure;
7657           /* FPA immediates are encoded as registers 8-15.
7658              parse_fpa_immediate has already applied the offset.  */
7659           inst.operands[i].reg = val;
7660           inst.operands[i].isreg = 1;
7661           break;
7662
7663         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7664         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7665
7666           /* Two kinds of register.  */
7667         case OP_RIWR_RIWC:
7668           {
7669             struct reg_entry *rege = arm_reg_parse_multi (&str);
7670             if (!rege
7671                 || (rege->type != REG_TYPE_MMXWR
7672                     && rege->type != REG_TYPE_MMXWC
7673                     && rege->type != REG_TYPE_MMXWCG))
7674               {
7675                 inst.error = _("iWMMXt data or control register expected");
7676                 goto failure;
7677               }
7678             inst.operands[i].reg = rege->number;
7679             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7680           }
7681           break;
7682
7683         case OP_RIWC_RIWG:
7684           {
7685             struct reg_entry *rege = arm_reg_parse_multi (&str);
7686             if (!rege
7687                 || (rege->type != REG_TYPE_MMXWC
7688                     && rege->type != REG_TYPE_MMXWCG))
7689               {
7690                 inst.error = _("iWMMXt control register expected");
7691                 goto failure;
7692               }
7693             inst.operands[i].reg = rege->number;
7694             inst.operands[i].isreg = 1;
7695           }
7696           break;
7697
7698           /* Misc */
7699         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7700         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7701         case OP_oROR:    val = parse_ror (&str);                break;
7702         try_cond:
7703         case OP_COND:    val = parse_cond (&str);               break;
7704         case OP_oBARRIER_I15:
7705           po_barrier_or_imm (str); break;
7706           immediate:
7707           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7708             goto failure;
7709           break;
7710
7711         case OP_wPSR:
7712         case OP_rPSR:
7713           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7714           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7715             {
7716               inst.error = _("Banked registers are not available with this "
7717                              "architecture.");
7718               goto failure;
7719             }
7720           break;
7721           try_psr:
7722           val = parse_psr (&str, op_parse_code == OP_wPSR);
7723           break;
7724
7725         case OP_VLDR:
7726           po_reg_or_goto (REG_TYPE_VFSD, try_sysreg);
7727           break;
7728         try_sysreg:
7729           val = parse_sys_vldr_vstr (&str);
7730           break;
7731
7732         case OP_APSR_RR:
7733           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7734           break;
7735           try_apsr:
7736           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7737              instruction).  */
7738           if (strncasecmp (str, "APSR_", 5) == 0)
7739             {
7740               unsigned found = 0;
7741               str += 5;
7742               while (found < 15)
7743                 switch (*str++)
7744                   {
7745                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7746                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7747                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7748                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7749                   default: found = 16;
7750                   }
7751               if (found != 15)
7752                 goto failure;
7753               inst.operands[i].isvec = 1;
7754               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7755               inst.operands[i].reg = REG_PC;
7756             }
7757           else
7758             goto failure;
7759           break;
7760
7761         case OP_TB:
7762           po_misc_or_fail (parse_tb (&str));
7763           break;
7764
7765           /* Register lists.  */
7766         case OP_REGLST:
7767           val = parse_reg_list (&str, REGLIST_RN);
7768           if (*str == '^')
7769             {
7770               inst.operands[i].writeback = 1;
7771               str++;
7772             }
7773           break;
7774
7775         case OP_CLRMLST:
7776           val = parse_reg_list (&str, REGLIST_CLRM);
7777           break;
7778
7779         case OP_VRSLST:
7780           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S,
7781                                     &partial_match);
7782           break;
7783
7784         case OP_VRDLST:
7785           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D,
7786                                     &partial_match);
7787           break;
7788
7789         case OP_VRSDLST:
7790           /* Allow Q registers too.  */
7791           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7792                                     REGLIST_NEON_D, &partial_match);
7793           if (val == FAIL)
7794             {
7795               inst.error = NULL;
7796               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7797                                         REGLIST_VFP_S, &partial_match);
7798               inst.operands[i].issingle = 1;
7799             }
7800           break;
7801
7802         case OP_VRSDVLST:
7803           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7804                                     REGLIST_VFP_D_VPR, &partial_match);
7805           if (val == FAIL && !partial_match)
7806             {
7807               inst.error = NULL;
7808               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7809                                         REGLIST_VFP_S_VPR, &partial_match);
7810               inst.operands[i].issingle = 1;
7811             }
7812           break;
7813
7814         case OP_NRDLST:
7815           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7816                                     REGLIST_NEON_D, &partial_match);
7817           break;
7818
7819         case OP_MSTRLST4:
7820         case OP_MSTRLST2:
7821           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7822                                            1, &inst.operands[i].vectype);
7823           if (val != (((op_parse_code == OP_MSTRLST2) ? 3 : 7) << 5 | 0xe))
7824             goto failure;
7825           break;
7826         case OP_NSTRLST:
7827           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7828                                            0, &inst.operands[i].vectype);
7829           break;
7830
7831           /* Addressing modes */
7832         case OP_ADDRMVE:
7833           po_misc_or_fail (parse_address_group_reloc (&str, i, GROUP_MVE));
7834           break;
7835
7836         case OP_ADDR:
7837           po_misc_or_fail (parse_address (&str, i));
7838           break;
7839
7840         case OP_ADDRGLDR:
7841           po_misc_or_fail_no_backtrack (
7842             parse_address_group_reloc (&str, i, GROUP_LDR));
7843           break;
7844
7845         case OP_ADDRGLDRS:
7846           po_misc_or_fail_no_backtrack (
7847             parse_address_group_reloc (&str, i, GROUP_LDRS));
7848           break;
7849
7850         case OP_ADDRGLDC:
7851           po_misc_or_fail_no_backtrack (
7852             parse_address_group_reloc (&str, i, GROUP_LDC));
7853           break;
7854
7855         case OP_SH:
7856           po_misc_or_fail (parse_shifter_operand (&str, i));
7857           break;
7858
7859         case OP_SHG:
7860           po_misc_or_fail_no_backtrack (
7861             parse_shifter_operand_group_reloc (&str, i));
7862           break;
7863
7864         case OP_oSHll:
7865           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7866           break;
7867
7868         case OP_oSHar:
7869           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7870           break;
7871
7872         case OP_oSHllar:
7873           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7874           break;
7875
7876         case OP_RMQRZ:
7877         case OP_oRMQRZ:
7878           po_reg_or_goto (REG_TYPE_MQ, try_rr_zr);
7879           break;
7880
7881         case OP_RR_ZR:
7882         try_rr_zr:
7883           po_reg_or_goto (REG_TYPE_RN, ZR);
7884           break;
7885         ZR:
7886           po_reg_or_fail (REG_TYPE_ZR);
7887           break;
7888
7889         default:
7890           as_fatal (_("unhandled operand code %d"), op_parse_code);
7891         }
7892
7893       /* Various value-based sanity checks and shared operations.  We
7894          do not signal immediate failures for the register constraints;
7895          this allows a syntax error to take precedence.  */
7896       switch (op_parse_code)
7897         {
7898         case OP_oRRnpc:
7899         case OP_RRnpc:
7900         case OP_RRnpcb:
7901         case OP_RRw:
7902         case OP_oRRw:
7903         case OP_RRnpc_I0:
7904           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7905             inst.error = BAD_PC;
7906           break;
7907
7908         case OP_oRRnpcsp:
7909         case OP_RRnpcsp:
7910         case OP_RRnpcsp_I32:
7911           if (inst.operands[i].isreg)
7912             {
7913               if (inst.operands[i].reg == REG_PC)
7914                 inst.error = BAD_PC;
7915               else if (inst.operands[i].reg == REG_SP
7916                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7917                           relaxed since ARMv8-A.  */
7918                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7919                 {
7920                   gas_assert (thumb);
7921                   inst.error = BAD_SP;
7922                 }
7923             }
7924           break;
7925
7926         case OP_RRnpctw:
7927           if (inst.operands[i].isreg
7928               && inst.operands[i].reg == REG_PC
7929               && (inst.operands[i].writeback || thumb))
7930             inst.error = BAD_PC;
7931           break;
7932
7933         case OP_RVSD_COND:
7934         case OP_VLDR:
7935           if (inst.operands[i].isreg)
7936             break;
7937         /* fall through.  */
7938
7939         case OP_CPSF:
7940         case OP_ENDI:
7941         case OP_oROR:
7942         case OP_wPSR:
7943         case OP_rPSR:
7944         case OP_COND:
7945         case OP_oBARRIER_I15:
7946         case OP_REGLST:
7947         case OP_CLRMLST:
7948         case OP_VRSLST:
7949         case OP_VRDLST:
7950         case OP_VRSDLST:
7951         case OP_VRSDVLST:
7952         case OP_NRDLST:
7953         case OP_NSTRLST:
7954         case OP_MSTRLST2:
7955         case OP_MSTRLST4:
7956           if (val == FAIL)
7957             goto failure;
7958           inst.operands[i].imm = val;
7959           break;
7960
7961         case OP_LR:
7962         case OP_oLR:
7963           if (inst.operands[i].reg != REG_LR)
7964             inst.error = _("operand must be LR register");
7965           break;
7966
7967         case OP_RMQRZ:
7968         case OP_oRMQRZ:
7969         case OP_RR_ZR:
7970           if (!inst.operands[i].iszr && inst.operands[i].reg == REG_PC)
7971             inst.error = BAD_PC;
7972           break;
7973
7974         case OP_RRe:
7975           if (inst.operands[i].isreg
7976               && (inst.operands[i].reg & 0x00000001) != 0)
7977             inst.error = BAD_ODD;
7978           break;
7979
7980         case OP_RRo:
7981           if (inst.operands[i].isreg)
7982             {
7983               if ((inst.operands[i].reg & 0x00000001) != 1)
7984                 inst.error = BAD_EVEN;
7985               else if (inst.operands[i].reg == REG_SP)
7986                 as_tsktsk (MVE_BAD_SP);
7987               else if (inst.operands[i].reg == REG_PC)
7988                 inst.error = BAD_PC;
7989             }
7990           break;
7991
7992         default:
7993           break;
7994         }
7995
7996       /* If we get here, this operand was successfully parsed.  */
7997       inst.operands[i].present = 1;
7998       continue;
7999
8000     bad_args:
8001       inst.error = BAD_ARGS;
8002
8003     failure:
8004       if (!backtrack_pos)
8005         {
8006           /* The parse routine should already have set inst.error, but set a
8007              default here just in case.  */
8008           if (!inst.error)
8009             inst.error = BAD_SYNTAX;
8010           return FAIL;
8011         }
8012
8013       /* Do not backtrack over a trailing optional argument that
8014          absorbed some text.  We will only fail again, with the
8015          'garbage following instruction' error message, which is
8016          probably less helpful than the current one.  */
8017       if (backtrack_index == i && backtrack_pos != str
8018           && upat[i+1] == OP_stop)
8019         {
8020           if (!inst.error)
8021             inst.error = BAD_SYNTAX;
8022           return FAIL;
8023         }
8024
8025       /* Try again, skipping the optional argument at backtrack_pos.  */
8026       str = backtrack_pos;
8027       inst.error = backtrack_error;
8028       inst.operands[backtrack_index].present = 0;
8029       i = backtrack_index;
8030       backtrack_pos = 0;
8031     }
8032
8033   /* Check that we have parsed all the arguments.  */
8034   if (*str != '\0' && !inst.error)
8035     inst.error = _("garbage following instruction");
8036
8037   return inst.error ? FAIL : SUCCESS;
8038 }
8039
8040 #undef po_char_or_fail
8041 #undef po_reg_or_fail
8042 #undef po_reg_or_goto
8043 #undef po_imm_or_fail
8044 #undef po_scalar_or_fail
8045 #undef po_barrier_or_imm
8046
8047 /* Shorthand macro for instruction encoding functions issuing errors.  */
8048 #define constraint(expr, err)                   \
8049   do                                            \
8050     {                                           \
8051       if (expr)                                 \
8052         {                                       \
8053           inst.error = err;                     \
8054           return;                               \
8055         }                                       \
8056     }                                           \
8057   while (0)
8058
8059 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
8060    instructions are unpredictable if these registers are used.  This
8061    is the BadReg predicate in ARM's Thumb-2 documentation.
8062
8063    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
8064    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
8065 #define reject_bad_reg(reg)                                     \
8066   do                                                            \
8067    if (reg == REG_PC)                                           \
8068      {                                                          \
8069        inst.error = BAD_PC;                                     \
8070        return;                                                  \
8071      }                                                          \
8072    else if (reg == REG_SP                                       \
8073             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
8074      {                                                          \
8075        inst.error = BAD_SP;                                     \
8076        return;                                                  \
8077      }                                                          \
8078   while (0)
8079
8080 /* If REG is R13 (the stack pointer), warn that its use is
8081    deprecated.  */
8082 #define warn_deprecated_sp(reg)                 \
8083   do                                            \
8084     if (warn_on_deprecated && reg == REG_SP)    \
8085        as_tsktsk (_("use of r13 is deprecated"));       \
8086   while (0)
8087
8088 /* Functions for operand encoding.  ARM, then Thumb.  */
8089
8090 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
8091
8092 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
8093
8094    The only binary encoding difference is the Coprocessor number.  Coprocessor
8095    9 is used for half-precision calculations or conversions.  The format of the
8096    instruction is the same as the equivalent Coprocessor 10 instruction that
8097    exists for Single-Precision operation.  */
8098
8099 static void
8100 do_scalar_fp16_v82_encode (void)
8101 {
8102   if (inst.cond < COND_ALWAYS)
8103     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
8104                " the behaviour is UNPREDICTABLE"));
8105   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
8106               _(BAD_FP16));
8107
8108   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
8109   mark_feature_used (&arm_ext_fp16);
8110 }
8111
8112 /* If VAL can be encoded in the immediate field of an ARM instruction,
8113    return the encoded form.  Otherwise, return FAIL.  */
8114
8115 static unsigned int
8116 encode_arm_immediate (unsigned int val)
8117 {
8118   unsigned int a, i;
8119
8120   if (val <= 0xff)
8121     return val;
8122
8123   for (i = 2; i < 32; i += 2)
8124     if ((a = rotate_left (val, i)) <= 0xff)
8125       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
8126
8127   return FAIL;
8128 }
8129
8130 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
8131    return the encoded form.  Otherwise, return FAIL.  */
8132 static unsigned int
8133 encode_thumb32_immediate (unsigned int val)
8134 {
8135   unsigned int a, i;
8136
8137   if (val <= 0xff)
8138     return val;
8139
8140   for (i = 1; i <= 24; i++)
8141     {
8142       a = val >> i;
8143       if ((val & ~(0xff << i)) == 0)
8144         return ((val >> i) & 0x7f) | ((32 - i) << 7);
8145     }
8146
8147   a = val & 0xff;
8148   if (val == ((a << 16) | a))
8149     return 0x100 | a;
8150   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
8151     return 0x300 | a;
8152
8153   a = val & 0xff00;
8154   if (val == ((a << 16) | a))
8155     return 0x200 | (a >> 8);
8156
8157   return FAIL;
8158 }
8159 /* Encode a VFP SP or DP register number into inst.instruction.  */
8160
8161 static void
8162 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
8163 {
8164   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
8165       && reg > 15)
8166     {
8167       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
8168         {
8169           if (thumb_mode)
8170             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8171                                     fpu_vfp_ext_d32);
8172           else
8173             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
8174                                     fpu_vfp_ext_d32);
8175         }
8176       else
8177         {
8178           first_error (_("D register out of range for selected VFP version"));
8179           return;
8180         }
8181     }
8182
8183   switch (pos)
8184     {
8185     case VFP_REG_Sd:
8186       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
8187       break;
8188
8189     case VFP_REG_Sn:
8190       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
8191       break;
8192
8193     case VFP_REG_Sm:
8194       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
8195       break;
8196
8197     case VFP_REG_Dd:
8198       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
8199       break;
8200
8201     case VFP_REG_Dn:
8202       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
8203       break;
8204
8205     case VFP_REG_Dm:
8206       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
8207       break;
8208
8209     default:
8210       abort ();
8211     }
8212 }
8213
8214 /* Encode a <shift> in an ARM-format instruction.  The immediate,
8215    if any, is handled by md_apply_fix.   */
8216 static void
8217 encode_arm_shift (int i)
8218 {
8219   /* register-shifted register.  */
8220   if (inst.operands[i].immisreg)
8221     {
8222       int op_index;
8223       for (op_index = 0; op_index <= i; ++op_index)
8224         {
8225           /* Check the operand only when it's presented.  In pre-UAL syntax,
8226              if the destination register is the same as the first operand, two
8227              register form of the instruction can be used.  */
8228           if (inst.operands[op_index].present && inst.operands[op_index].isreg
8229               && inst.operands[op_index].reg == REG_PC)
8230             as_warn (UNPRED_REG ("r15"));
8231         }
8232
8233       if (inst.operands[i].imm == REG_PC)
8234         as_warn (UNPRED_REG ("r15"));
8235     }
8236
8237   if (inst.operands[i].shift_kind == SHIFT_RRX)
8238     inst.instruction |= SHIFT_ROR << 5;
8239   else
8240     {
8241       inst.instruction |= inst.operands[i].shift_kind << 5;
8242       if (inst.operands[i].immisreg)
8243         {
8244           inst.instruction |= SHIFT_BY_REG;
8245           inst.instruction |= inst.operands[i].imm << 8;
8246         }
8247       else
8248         inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8249     }
8250 }
8251
8252 static void
8253 encode_arm_shifter_operand (int i)
8254 {
8255   if (inst.operands[i].isreg)
8256     {
8257       inst.instruction |= inst.operands[i].reg;
8258       encode_arm_shift (i);
8259     }
8260   else
8261     {
8262       inst.instruction |= INST_IMMEDIATE;
8263       if (inst.relocs[0].type != BFD_RELOC_ARM_IMMEDIATE)
8264         inst.instruction |= inst.operands[i].imm;
8265     }
8266 }
8267
8268 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
8269 static void
8270 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
8271 {
8272   /* PR 14260:
8273      Generate an error if the operand is not a register.  */
8274   constraint (!inst.operands[i].isreg,
8275               _("Instruction does not support =N addresses"));
8276
8277   inst.instruction |= inst.operands[i].reg << 16;
8278
8279   if (inst.operands[i].preind)
8280     {
8281       if (is_t)
8282         {
8283           inst.error = _("instruction does not accept preindexed addressing");
8284           return;
8285         }
8286       inst.instruction |= PRE_INDEX;
8287       if (inst.operands[i].writeback)
8288         inst.instruction |= WRITE_BACK;
8289
8290     }
8291   else if (inst.operands[i].postind)
8292     {
8293       gas_assert (inst.operands[i].writeback);
8294       if (is_t)
8295         inst.instruction |= WRITE_BACK;
8296     }
8297   else /* unindexed - only for coprocessor */
8298     {
8299       inst.error = _("instruction does not accept unindexed addressing");
8300       return;
8301     }
8302
8303   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
8304       && (((inst.instruction & 0x000f0000) >> 16)
8305           == ((inst.instruction & 0x0000f000) >> 12)))
8306     as_warn ((inst.instruction & LOAD_BIT)
8307              ? _("destination register same as write-back base")
8308              : _("source register same as write-back base"));
8309 }
8310
8311 /* inst.operands[i] was set up by parse_address.  Encode it into an
8312    ARM-format mode 2 load or store instruction.  If is_t is true,
8313    reject forms that cannot be used with a T instruction (i.e. not
8314    post-indexed).  */
8315 static void
8316 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
8317 {
8318   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
8319
8320   encode_arm_addr_mode_common (i, is_t);
8321
8322   if (inst.operands[i].immisreg)
8323     {
8324       constraint ((inst.operands[i].imm == REG_PC
8325                    || (is_pc && inst.operands[i].writeback)),
8326                   BAD_PC_ADDRESSING);
8327       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
8328       inst.instruction |= inst.operands[i].imm;
8329       if (!inst.operands[i].negative)
8330         inst.instruction |= INDEX_UP;
8331       if (inst.operands[i].shifted)
8332         {
8333           if (inst.operands[i].shift_kind == SHIFT_RRX)
8334             inst.instruction |= SHIFT_ROR << 5;
8335           else
8336             {
8337               inst.instruction |= inst.operands[i].shift_kind << 5;
8338               inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8339             }
8340         }
8341     }
8342   else /* immediate offset in inst.relocs[0] */
8343     {
8344       if (is_pc && !inst.relocs[0].pc_rel)
8345         {
8346           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
8347
8348           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
8349              cannot use PC in addressing.
8350              PC cannot be used in writeback addressing, either.  */
8351           constraint ((is_t || inst.operands[i].writeback),
8352                       BAD_PC_ADDRESSING);
8353
8354           /* Use of PC in str is deprecated for ARMv7.  */
8355           if (warn_on_deprecated
8356               && !is_load
8357               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
8358             as_tsktsk (_("use of PC in this instruction is deprecated"));
8359         }
8360
8361       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8362         {
8363           /* Prefer + for zero encoded value.  */
8364           if (!inst.operands[i].negative)
8365             inst.instruction |= INDEX_UP;
8366           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM;
8367         }
8368     }
8369 }
8370
8371 /* inst.operands[i] was set up by parse_address.  Encode it into an
8372    ARM-format mode 3 load or store instruction.  Reject forms that
8373    cannot be used with such instructions.  If is_t is true, reject
8374    forms that cannot be used with a T instruction (i.e. not
8375    post-indexed).  */
8376 static void
8377 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
8378 {
8379   if (inst.operands[i].immisreg && inst.operands[i].shifted)
8380     {
8381       inst.error = _("instruction does not accept scaled register index");
8382       return;
8383     }
8384
8385   encode_arm_addr_mode_common (i, is_t);
8386
8387   if (inst.operands[i].immisreg)
8388     {
8389       constraint ((inst.operands[i].imm == REG_PC
8390                    || (is_t && inst.operands[i].reg == REG_PC)),
8391                   BAD_PC_ADDRESSING);
8392       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
8393                   BAD_PC_WRITEBACK);
8394       inst.instruction |= inst.operands[i].imm;
8395       if (!inst.operands[i].negative)
8396         inst.instruction |= INDEX_UP;
8397     }
8398   else /* immediate offset in inst.relocs[0] */
8399     {
8400       constraint ((inst.operands[i].reg == REG_PC && !inst.relocs[0].pc_rel
8401                    && inst.operands[i].writeback),
8402                   BAD_PC_WRITEBACK);
8403       inst.instruction |= HWOFFSET_IMM;
8404       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8405         {
8406           /* Prefer + for zero encoded value.  */
8407           if (!inst.operands[i].negative)
8408             inst.instruction |= INDEX_UP;
8409
8410           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM8;
8411         }
8412     }
8413 }
8414
8415 /* Write immediate bits [7:0] to the following locations:
8416
8417   |28/24|23     19|18 16|15                    4|3     0|
8418   |  a  |x x x x x|b c d|x x x x x x x x x x x x|e f g h|
8419
8420   This function is used by VMOV/VMVN/VORR/VBIC.  */
8421
8422 static void
8423 neon_write_immbits (unsigned immbits)
8424 {
8425   inst.instruction |= immbits & 0xf;
8426   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
8427   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
8428 }
8429
8430 /* Invert low-order SIZE bits of XHI:XLO.  */
8431
8432 static void
8433 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
8434 {
8435   unsigned immlo = xlo ? *xlo : 0;
8436   unsigned immhi = xhi ? *xhi : 0;
8437
8438   switch (size)
8439     {
8440     case 8:
8441       immlo = (~immlo) & 0xff;
8442       break;
8443
8444     case 16:
8445       immlo = (~immlo) & 0xffff;
8446       break;
8447
8448     case 64:
8449       immhi = (~immhi) & 0xffffffff;
8450       /* fall through.  */
8451
8452     case 32:
8453       immlo = (~immlo) & 0xffffffff;
8454       break;
8455
8456     default:
8457       abort ();
8458     }
8459
8460   if (xlo)
8461     *xlo = immlo;
8462
8463   if (xhi)
8464     *xhi = immhi;
8465 }
8466
8467 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
8468    A, B, C, D.  */
8469
8470 static int
8471 neon_bits_same_in_bytes (unsigned imm)
8472 {
8473   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
8474          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
8475          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
8476          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
8477 }
8478
8479 /* For immediate of above form, return 0bABCD.  */
8480
8481 static unsigned
8482 neon_squash_bits (unsigned imm)
8483 {
8484   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
8485          | ((imm & 0x01000000) >> 21);
8486 }
8487
8488 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
8489
8490 static unsigned
8491 neon_qfloat_bits (unsigned imm)
8492 {
8493   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
8494 }
8495
8496 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
8497    the instruction. *OP is passed as the initial value of the op field, and
8498    may be set to a different value depending on the constant (i.e.
8499    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
8500    MVN).  If the immediate looks like a repeated pattern then also
8501    try smaller element sizes.  */
8502
8503 static int
8504 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
8505                          unsigned *immbits, int *op, int size,
8506                          enum neon_el_type type)
8507 {
8508   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
8509      float.  */
8510   if (type == NT_float && !float_p)
8511     return FAIL;
8512
8513   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
8514     {
8515       if (size != 32 || *op == 1)
8516         return FAIL;
8517       *immbits = neon_qfloat_bits (immlo);
8518       return 0xf;
8519     }
8520
8521   if (size == 64)
8522     {
8523       if (neon_bits_same_in_bytes (immhi)
8524           && neon_bits_same_in_bytes (immlo))
8525         {
8526           if (*op == 1)
8527             return FAIL;
8528           *immbits = (neon_squash_bits (immhi) << 4)
8529                      | neon_squash_bits (immlo);
8530           *op = 1;
8531           return 0xe;
8532         }
8533
8534       if (immhi != immlo)
8535         return FAIL;
8536     }
8537
8538   if (size >= 32)
8539     {
8540       if (immlo == (immlo & 0x000000ff))
8541         {
8542           *immbits = immlo;
8543           return 0x0;
8544         }
8545       else if (immlo == (immlo & 0x0000ff00))
8546         {
8547           *immbits = immlo >> 8;
8548           return 0x2;
8549         }
8550       else if (immlo == (immlo & 0x00ff0000))
8551         {
8552           *immbits = immlo >> 16;
8553           return 0x4;
8554         }
8555       else if (immlo == (immlo & 0xff000000))
8556         {
8557           *immbits = immlo >> 24;
8558           return 0x6;
8559         }
8560       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
8561         {
8562           *immbits = (immlo >> 8) & 0xff;
8563           return 0xc;
8564         }
8565       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
8566         {
8567           *immbits = (immlo >> 16) & 0xff;
8568           return 0xd;
8569         }
8570
8571       if ((immlo & 0xffff) != (immlo >> 16))
8572         return FAIL;
8573       immlo &= 0xffff;
8574     }
8575
8576   if (size >= 16)
8577     {
8578       if (immlo == (immlo & 0x000000ff))
8579         {
8580           *immbits = immlo;
8581           return 0x8;
8582         }
8583       else if (immlo == (immlo & 0x0000ff00))
8584         {
8585           *immbits = immlo >> 8;
8586           return 0xa;
8587         }
8588
8589       if ((immlo & 0xff) != (immlo >> 8))
8590         return FAIL;
8591       immlo &= 0xff;
8592     }
8593
8594   if (immlo == (immlo & 0x000000ff))
8595     {
8596       /* Don't allow MVN with 8-bit immediate.  */
8597       if (*op == 1)
8598         return FAIL;
8599       *immbits = immlo;
8600       return 0xe;
8601     }
8602
8603   return FAIL;
8604 }
8605
8606 #if defined BFD_HOST_64_BIT
8607 /* Returns TRUE if double precision value V may be cast
8608    to single precision without loss of accuracy.  */
8609
8610 static bfd_boolean
8611 is_double_a_single (bfd_int64_t v)
8612 {
8613   int exp = (int)((v >> 52) & 0x7FF);
8614   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8615
8616   return (exp == 0 || exp == 0x7FF
8617           || (exp >= 1023 - 126 && exp <= 1023 + 127))
8618     && (mantissa & 0x1FFFFFFFl) == 0;
8619 }
8620
8621 /* Returns a double precision value casted to single precision
8622    (ignoring the least significant bits in exponent and mantissa).  */
8623
8624 static int
8625 double_to_single (bfd_int64_t v)
8626 {
8627   int sign = (int) ((v >> 63) & 1l);
8628   int exp = (int) ((v >> 52) & 0x7FF);
8629   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8630
8631   if (exp == 0x7FF)
8632     exp = 0xFF;
8633   else
8634     {
8635       exp = exp - 1023 + 127;
8636       if (exp >= 0xFF)
8637         {
8638           /* Infinity.  */
8639           exp = 0x7F;
8640           mantissa = 0;
8641         }
8642       else if (exp < 0)
8643         {
8644           /* No denormalized numbers.  */
8645           exp = 0;
8646           mantissa = 0;
8647         }
8648     }
8649   mantissa >>= 29;
8650   return (sign << 31) | (exp << 23) | mantissa;
8651 }
8652 #endif /* BFD_HOST_64_BIT */
8653
8654 enum lit_type
8655 {
8656   CONST_THUMB,
8657   CONST_ARM,
8658   CONST_VEC
8659 };
8660
8661 static void do_vfp_nsyn_opcode (const char *);
8662
8663 /* inst.relocs[0].exp describes an "=expr" load pseudo-operation.
8664    Determine whether it can be performed with a move instruction; if
8665    it can, convert inst.instruction to that move instruction and
8666    return TRUE; if it can't, convert inst.instruction to a literal-pool
8667    load and return FALSE.  If this is not a valid thing to do in the
8668    current context, set inst.error and return TRUE.
8669
8670    inst.operands[i] describes the destination register.  */
8671
8672 static bfd_boolean
8673 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
8674 {
8675   unsigned long tbit;
8676   bfd_boolean thumb_p = (t == CONST_THUMB);
8677   bfd_boolean arm_p   = (t == CONST_ARM);
8678
8679   if (thumb_p)
8680     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
8681   else
8682     tbit = LOAD_BIT;
8683
8684   if ((inst.instruction & tbit) == 0)
8685     {
8686       inst.error = _("invalid pseudo operation");
8687       return TRUE;
8688     }
8689
8690   if (inst.relocs[0].exp.X_op != O_constant
8691       && inst.relocs[0].exp.X_op != O_symbol
8692       && inst.relocs[0].exp.X_op != O_big)
8693     {
8694       inst.error = _("constant expression expected");
8695       return TRUE;
8696     }
8697
8698   if (inst.relocs[0].exp.X_op == O_constant
8699       || inst.relocs[0].exp.X_op == O_big)
8700     {
8701 #if defined BFD_HOST_64_BIT
8702       bfd_int64_t v;
8703 #else
8704       offsetT v;
8705 #endif
8706       if (inst.relocs[0].exp.X_op == O_big)
8707         {
8708           LITTLENUM_TYPE w[X_PRECISION];
8709           LITTLENUM_TYPE * l;
8710
8711           if (inst.relocs[0].exp.X_add_number == -1)
8712             {
8713               gen_to_words (w, X_PRECISION, E_PRECISION);
8714               l = w;
8715               /* FIXME: Should we check words w[2..5] ?  */
8716             }
8717           else
8718             l = generic_bignum;
8719
8720 #if defined BFD_HOST_64_BIT
8721           v =
8722             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8723                   << LITTLENUM_NUMBER_OF_BITS)
8724                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8725                 << LITTLENUM_NUMBER_OF_BITS)
8726                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8727               << LITTLENUM_NUMBER_OF_BITS)
8728              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8729 #else
8730           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8731             |  (l[0] & LITTLENUM_MASK);
8732 #endif
8733         }
8734       else
8735         v = inst.relocs[0].exp.X_add_number;
8736
8737       if (!inst.operands[i].issingle)
8738         {
8739           if (thumb_p)
8740             {
8741               /* LDR should not use lead in a flag-setting instruction being
8742                  chosen so we do not check whether movs can be used.  */
8743
8744               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8745                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8746                   && inst.operands[i].reg != 13
8747                   && inst.operands[i].reg != 15)
8748                 {
8749                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8750                      movw instruction.  */
8751                   unsigned int newimm;
8752                   bfd_boolean isNegated;
8753
8754                   newimm = encode_thumb32_immediate (v);
8755                   if (newimm != (unsigned int) FAIL)
8756                     isNegated = FALSE;
8757                   else
8758                     {
8759                       newimm = encode_thumb32_immediate (~v);
8760                       if (newimm != (unsigned int) FAIL)
8761                         isNegated = TRUE;
8762                     }
8763
8764                   /* The number can be loaded with a mov.w or mvn
8765                      instruction.  */
8766                   if (newimm != (unsigned int) FAIL
8767                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8768                     {
8769                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8770                                           | (inst.operands[i].reg << 8));
8771                       /* Change to MOVN.  */
8772                       inst.instruction |= (isNegated ? 0x200000 : 0);
8773                       inst.instruction |= (newimm & 0x800) << 15;
8774                       inst.instruction |= (newimm & 0x700) << 4;
8775                       inst.instruction |= (newimm & 0x0ff);
8776                       return TRUE;
8777                     }
8778                   /* The number can be loaded with a movw instruction.  */
8779                   else if ((v & ~0xFFFF) == 0
8780                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8781                     {
8782                       int imm = v & 0xFFFF;
8783
8784                       inst.instruction = 0xf2400000;  /* MOVW.  */
8785                       inst.instruction |= (inst.operands[i].reg << 8);
8786                       inst.instruction |= (imm & 0xf000) << 4;
8787                       inst.instruction |= (imm & 0x0800) << 15;
8788                       inst.instruction |= (imm & 0x0700) << 4;
8789                       inst.instruction |= (imm & 0x00ff);
8790                       /*  In case this replacement is being done on Armv8-M
8791                           Baseline we need to make sure to disable the
8792                           instruction size check, as otherwise GAS will reject
8793                           the use of this T32 instruction.  */
8794                       inst.size_req = 0;
8795                       return TRUE;
8796                     }
8797                 }
8798             }
8799           else if (arm_p)
8800             {
8801               int value = encode_arm_immediate (v);
8802
8803               if (value != FAIL)
8804                 {
8805                   /* This can be done with a mov instruction.  */
8806                   inst.instruction &= LITERAL_MASK;
8807                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8808                   inst.instruction |= value & 0xfff;
8809                   return TRUE;
8810                 }
8811
8812               value = encode_arm_immediate (~ v);
8813               if (value != FAIL)
8814                 {
8815                   /* This can be done with a mvn instruction.  */
8816                   inst.instruction &= LITERAL_MASK;
8817                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8818                   inst.instruction |= value & 0xfff;
8819                   return TRUE;
8820                 }
8821             }
8822           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8823             {
8824               int op = 0;
8825               unsigned immbits = 0;
8826               unsigned immlo = inst.operands[1].imm;
8827               unsigned immhi = inst.operands[1].regisimm
8828                 ? inst.operands[1].reg
8829                 : inst.relocs[0].exp.X_unsigned
8830                 ? 0
8831                 : ((bfd_int64_t)((int) immlo)) >> 32;
8832               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8833                                                    &op, 64, NT_invtype);
8834
8835               if (cmode == FAIL)
8836                 {
8837                   neon_invert_size (&immlo, &immhi, 64);
8838                   op = !op;
8839                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8840                                                    &op, 64, NT_invtype);
8841                 }
8842
8843               if (cmode != FAIL)
8844                 {
8845                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8846                     | (1 << 23)
8847                     | (cmode << 8)
8848                     | (op << 5)
8849                     | (1 << 4);
8850
8851                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8852                   if (thumb_mode)
8853                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8854                   else
8855                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8856                   neon_write_immbits (immbits);
8857                   return TRUE;
8858                 }
8859             }
8860         }
8861
8862       if (t == CONST_VEC)
8863         {
8864           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8865           if (inst.operands[i].issingle
8866               && is_quarter_float (inst.operands[1].imm)
8867               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8868             {
8869               inst.operands[1].imm =
8870                 neon_qfloat_bits (v);
8871               do_vfp_nsyn_opcode ("fconsts");
8872               return TRUE;
8873             }
8874
8875           /* If our host does not support a 64-bit type then we cannot perform
8876              the following optimization.  This mean that there will be a
8877              discrepancy between the output produced by an assembler built for
8878              a 32-bit-only host and the output produced from a 64-bit host, but
8879              this cannot be helped.  */
8880 #if defined BFD_HOST_64_BIT
8881           else if (!inst.operands[1].issingle
8882                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8883             {
8884               if (is_double_a_single (v)
8885                   && is_quarter_float (double_to_single (v)))
8886                 {
8887                   inst.operands[1].imm =
8888                     neon_qfloat_bits (double_to_single (v));
8889                   do_vfp_nsyn_opcode ("fconstd");
8890                   return TRUE;
8891                 }
8892             }
8893 #endif
8894         }
8895     }
8896
8897   if (add_to_lit_pool ((!inst.operands[i].isvec
8898                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8899     return TRUE;
8900
8901   inst.operands[1].reg = REG_PC;
8902   inst.operands[1].isreg = 1;
8903   inst.operands[1].preind = 1;
8904   inst.relocs[0].pc_rel = 1;
8905   inst.relocs[0].type = (thumb_p
8906                      ? BFD_RELOC_ARM_THUMB_OFFSET
8907                      : (mode_3
8908                         ? BFD_RELOC_ARM_HWLITERAL
8909                         : BFD_RELOC_ARM_LITERAL));
8910   return FALSE;
8911 }
8912
8913 /* inst.operands[i] was set up by parse_address.  Encode it into an
8914    ARM-format instruction.  Reject all forms which cannot be encoded
8915    into a coprocessor load/store instruction.  If wb_ok is false,
8916    reject use of writeback; if unind_ok is false, reject use of
8917    unindexed addressing.  If reloc_override is not 0, use it instead
8918    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8919    (in which case it is preserved).  */
8920
8921 static int
8922 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8923 {
8924   if (!inst.operands[i].isreg)
8925     {
8926       /* PR 18256 */
8927       if (! inst.operands[0].isvec)
8928         {
8929           inst.error = _("invalid co-processor operand");
8930           return FAIL;
8931         }
8932       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8933         return SUCCESS;
8934     }
8935
8936   inst.instruction |= inst.operands[i].reg << 16;
8937
8938   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8939
8940   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8941     {
8942       gas_assert (!inst.operands[i].writeback);
8943       if (!unind_ok)
8944         {
8945           inst.error = _("instruction does not support unindexed addressing");
8946           return FAIL;
8947         }
8948       inst.instruction |= inst.operands[i].imm;
8949       inst.instruction |= INDEX_UP;
8950       return SUCCESS;
8951     }
8952
8953   if (inst.operands[i].preind)
8954     inst.instruction |= PRE_INDEX;
8955
8956   if (inst.operands[i].writeback)
8957     {
8958       if (inst.operands[i].reg == REG_PC)
8959         {
8960           inst.error = _("pc may not be used with write-back");
8961           return FAIL;
8962         }
8963       if (!wb_ok)
8964         {
8965           inst.error = _("instruction does not support writeback");
8966           return FAIL;
8967         }
8968       inst.instruction |= WRITE_BACK;
8969     }
8970
8971   if (reloc_override)
8972     inst.relocs[0].type = (bfd_reloc_code_real_type) reloc_override;
8973   else if ((inst.relocs[0].type < BFD_RELOC_ARM_ALU_PC_G0_NC
8974             || inst.relocs[0].type > BFD_RELOC_ARM_LDC_SB_G2)
8975            && inst.relocs[0].type != BFD_RELOC_ARM_LDR_PC_G0)
8976     {
8977       if (thumb_mode)
8978         inst.relocs[0].type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8979       else
8980         inst.relocs[0].type = BFD_RELOC_ARM_CP_OFF_IMM;
8981     }
8982
8983   /* Prefer + for zero encoded value.  */
8984   if (!inst.operands[i].negative)
8985     inst.instruction |= INDEX_UP;
8986
8987   return SUCCESS;
8988 }
8989
8990 /* Functions for instruction encoding, sorted by sub-architecture.
8991    First some generics; their names are taken from the conventional
8992    bit positions for register arguments in ARM format instructions.  */
8993
8994 static void
8995 do_noargs (void)
8996 {
8997 }
8998
8999 static void
9000 do_rd (void)
9001 {
9002   inst.instruction |= inst.operands[0].reg << 12;
9003 }
9004
9005 static void
9006 do_rn (void)
9007 {
9008   inst.instruction |= inst.operands[0].reg << 16;
9009 }
9010
9011 static void
9012 do_rd_rm (void)
9013 {
9014   inst.instruction |= inst.operands[0].reg << 12;
9015   inst.instruction |= inst.operands[1].reg;
9016 }
9017
9018 static void
9019 do_rm_rn (void)
9020 {
9021   inst.instruction |= inst.operands[0].reg;
9022   inst.instruction |= inst.operands[1].reg << 16;
9023 }
9024
9025 static void
9026 do_rd_rn (void)
9027 {
9028   inst.instruction |= inst.operands[0].reg << 12;
9029   inst.instruction |= inst.operands[1].reg << 16;
9030 }
9031
9032 static void
9033 do_rn_rd (void)
9034 {
9035   inst.instruction |= inst.operands[0].reg << 16;
9036   inst.instruction |= inst.operands[1].reg << 12;
9037 }
9038
9039 static void
9040 do_tt (void)
9041 {
9042   inst.instruction |= inst.operands[0].reg << 8;
9043   inst.instruction |= inst.operands[1].reg << 16;
9044 }
9045
9046 static bfd_boolean
9047 check_obsolete (const arm_feature_set *feature, const char *msg)
9048 {
9049   if (ARM_CPU_IS_ANY (cpu_variant))
9050     {
9051       as_tsktsk ("%s", msg);
9052       return TRUE;
9053     }
9054   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
9055     {
9056       as_bad ("%s", msg);
9057       return TRUE;
9058     }
9059
9060   return FALSE;
9061 }
9062
9063 static void
9064 do_rd_rm_rn (void)
9065 {
9066   unsigned Rn = inst.operands[2].reg;
9067   /* Enforce restrictions on SWP instruction.  */
9068   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
9069     {
9070       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
9071                   _("Rn must not overlap other operands"));
9072
9073       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
9074        */
9075       if (!check_obsolete (&arm_ext_v8,
9076                            _("swp{b} use is obsoleted for ARMv8 and later"))
9077           && warn_on_deprecated
9078           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
9079         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
9080     }
9081
9082   inst.instruction |= inst.operands[0].reg << 12;
9083   inst.instruction |= inst.operands[1].reg;
9084   inst.instruction |= Rn << 16;
9085 }
9086
9087 static void
9088 do_rd_rn_rm (void)
9089 {
9090   inst.instruction |= inst.operands[0].reg << 12;
9091   inst.instruction |= inst.operands[1].reg << 16;
9092   inst.instruction |= inst.operands[2].reg;
9093 }
9094
9095 static void
9096 do_rm_rd_rn (void)
9097 {
9098   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
9099   constraint (((inst.relocs[0].exp.X_op != O_constant
9100                 && inst.relocs[0].exp.X_op != O_illegal)
9101                || inst.relocs[0].exp.X_add_number != 0),
9102               BAD_ADDR_MODE);
9103   inst.instruction |= inst.operands[0].reg;
9104   inst.instruction |= inst.operands[1].reg << 12;
9105   inst.instruction |= inst.operands[2].reg << 16;
9106 }
9107
9108 static void
9109 do_imm0 (void)
9110 {
9111   inst.instruction |= inst.operands[0].imm;
9112 }
9113
9114 static void
9115 do_rd_cpaddr (void)
9116 {
9117   inst.instruction |= inst.operands[0].reg << 12;
9118   encode_arm_cp_address (1, TRUE, TRUE, 0);
9119 }
9120
9121 /* ARM instructions, in alphabetical order by function name (except
9122    that wrapper functions appear immediately after the function they
9123    wrap).  */
9124
9125 /* This is a pseudo-op of the form "adr rd, label" to be converted
9126    into a relative address of the form "add rd, pc, #label-.-8".  */
9127
9128 static void
9129 do_adr (void)
9130 {
9131   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9132
9133   /* Frag hacking will turn this into a sub instruction if the offset turns
9134      out to be negative.  */
9135   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
9136   inst.relocs[0].pc_rel = 1;
9137   inst.relocs[0].exp.X_add_number -= 8;
9138
9139   if (support_interwork
9140       && inst.relocs[0].exp.X_op == O_symbol
9141       && inst.relocs[0].exp.X_add_symbol != NULL
9142       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9143       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9144     inst.relocs[0].exp.X_add_number |= 1;
9145 }
9146
9147 /* This is a pseudo-op of the form "adrl rd, label" to be converted
9148    into a relative address of the form:
9149    add rd, pc, #low(label-.-8)"
9150    add rd, rd, #high(label-.-8)"  */
9151
9152 static void
9153 do_adrl (void)
9154 {
9155   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9156
9157   /* Frag hacking will turn this into a sub instruction if the offset turns
9158      out to be negative.  */
9159   inst.relocs[0].type          = BFD_RELOC_ARM_ADRL_IMMEDIATE;
9160   inst.relocs[0].pc_rel        = 1;
9161   inst.size                    = INSN_SIZE * 2;
9162   inst.relocs[0].exp.X_add_number -= 8;
9163
9164   if (support_interwork
9165       && inst.relocs[0].exp.X_op == O_symbol
9166       && inst.relocs[0].exp.X_add_symbol != NULL
9167       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9168       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9169     inst.relocs[0].exp.X_add_number |= 1;
9170 }
9171
9172 static void
9173 do_arit (void)
9174 {
9175   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9176               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9177               THUMB1_RELOC_ONLY);
9178   if (!inst.operands[1].present)
9179     inst.operands[1].reg = inst.operands[0].reg;
9180   inst.instruction |= inst.operands[0].reg << 12;
9181   inst.instruction |= inst.operands[1].reg << 16;
9182   encode_arm_shifter_operand (2);
9183 }
9184
9185 static void
9186 do_barrier (void)
9187 {
9188   if (inst.operands[0].present)
9189     inst.instruction |= inst.operands[0].imm;
9190   else
9191     inst.instruction |= 0xf;
9192 }
9193
9194 static void
9195 do_bfc (void)
9196 {
9197   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9198   constraint (msb > 32, _("bit-field extends past end of register"));
9199   /* The instruction encoding stores the LSB and MSB,
9200      not the LSB and width.  */
9201   inst.instruction |= inst.operands[0].reg << 12;
9202   inst.instruction |= inst.operands[1].imm << 7;
9203   inst.instruction |= (msb - 1) << 16;
9204 }
9205
9206 static void
9207 do_bfi (void)
9208 {
9209   unsigned int msb;
9210
9211   /* #0 in second position is alternative syntax for bfc, which is
9212      the same instruction but with REG_PC in the Rm field.  */
9213   if (!inst.operands[1].isreg)
9214     inst.operands[1].reg = REG_PC;
9215
9216   msb = inst.operands[2].imm + inst.operands[3].imm;
9217   constraint (msb > 32, _("bit-field extends past end of register"));
9218   /* The instruction encoding stores the LSB and MSB,
9219      not the LSB and width.  */
9220   inst.instruction |= inst.operands[0].reg << 12;
9221   inst.instruction |= inst.operands[1].reg;
9222   inst.instruction |= inst.operands[2].imm << 7;
9223   inst.instruction |= (msb - 1) << 16;
9224 }
9225
9226 static void
9227 do_bfx (void)
9228 {
9229   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9230               _("bit-field extends past end of register"));
9231   inst.instruction |= inst.operands[0].reg << 12;
9232   inst.instruction |= inst.operands[1].reg;
9233   inst.instruction |= inst.operands[2].imm << 7;
9234   inst.instruction |= (inst.operands[3].imm - 1) << 16;
9235 }
9236
9237 /* ARM V5 breakpoint instruction (argument parse)
9238      BKPT <16 bit unsigned immediate>
9239      Instruction is not conditional.
9240         The bit pattern given in insns[] has the COND_ALWAYS condition,
9241         and it is an error if the caller tried to override that.  */
9242
9243 static void
9244 do_bkpt (void)
9245 {
9246   /* Top 12 of 16 bits to bits 19:8.  */
9247   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
9248
9249   /* Bottom 4 of 16 bits to bits 3:0.  */
9250   inst.instruction |= inst.operands[0].imm & 0xf;
9251 }
9252
9253 static void
9254 encode_branch (int default_reloc)
9255 {
9256   if (inst.operands[0].hasreloc)
9257     {
9258       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
9259                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
9260                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
9261       inst.relocs[0].type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
9262         ? BFD_RELOC_ARM_PLT32
9263         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
9264     }
9265   else
9266     inst.relocs[0].type = (bfd_reloc_code_real_type) default_reloc;
9267   inst.relocs[0].pc_rel = 1;
9268 }
9269
9270 static void
9271 do_branch (void)
9272 {
9273 #ifdef OBJ_ELF
9274   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9275     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9276   else
9277 #endif
9278     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9279 }
9280
9281 static void
9282 do_bl (void)
9283 {
9284 #ifdef OBJ_ELF
9285   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9286     {
9287       if (inst.cond == COND_ALWAYS)
9288         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
9289       else
9290         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9291     }
9292   else
9293 #endif
9294     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9295 }
9296
9297 /* ARM V5 branch-link-exchange instruction (argument parse)
9298      BLX <target_addr>          ie BLX(1)
9299      BLX{<condition>} <Rm>      ie BLX(2)
9300    Unfortunately, there are two different opcodes for this mnemonic.
9301    So, the insns[].value is not used, and the code here zaps values
9302         into inst.instruction.
9303    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
9304
9305 static void
9306 do_blx (void)
9307 {
9308   if (inst.operands[0].isreg)
9309     {
9310       /* Arg is a register; the opcode provided by insns[] is correct.
9311          It is not illegal to do "blx pc", just useless.  */
9312       if (inst.operands[0].reg == REG_PC)
9313         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
9314
9315       inst.instruction |= inst.operands[0].reg;
9316     }
9317   else
9318     {
9319       /* Arg is an address; this instruction cannot be executed
9320          conditionally, and the opcode must be adjusted.
9321          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
9322          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
9323       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9324       inst.instruction = 0xfa000000;
9325       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
9326     }
9327 }
9328
9329 static void
9330 do_bx (void)
9331 {
9332   bfd_boolean want_reloc;
9333
9334   if (inst.operands[0].reg == REG_PC)
9335     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
9336
9337   inst.instruction |= inst.operands[0].reg;
9338   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
9339      it is for ARMv4t or earlier.  */
9340   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
9341   if (!ARM_FEATURE_ZERO (selected_object_arch)
9342       && !ARM_CPU_HAS_FEATURE (selected_object_arch, arm_ext_v5))
9343       want_reloc = TRUE;
9344
9345 #ifdef OBJ_ELF
9346   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
9347 #endif
9348     want_reloc = FALSE;
9349
9350   if (want_reloc)
9351     inst.relocs[0].type = BFD_RELOC_ARM_V4BX;
9352 }
9353
9354
9355 /* ARM v5TEJ.  Jump to Jazelle code.  */
9356
9357 static void
9358 do_bxj (void)
9359 {
9360   if (inst.operands[0].reg == REG_PC)
9361     as_tsktsk (_("use of r15 in bxj is not really useful"));
9362
9363   inst.instruction |= inst.operands[0].reg;
9364 }
9365
9366 /* Co-processor data operation:
9367       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
9368       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
9369 static void
9370 do_cdp (void)
9371 {
9372   inst.instruction |= inst.operands[0].reg << 8;
9373   inst.instruction |= inst.operands[1].imm << 20;
9374   inst.instruction |= inst.operands[2].reg << 12;
9375   inst.instruction |= inst.operands[3].reg << 16;
9376   inst.instruction |= inst.operands[4].reg;
9377   inst.instruction |= inst.operands[5].imm << 5;
9378 }
9379
9380 static void
9381 do_cmp (void)
9382 {
9383   inst.instruction |= inst.operands[0].reg << 16;
9384   encode_arm_shifter_operand (1);
9385 }
9386
9387 /* Transfer between coprocessor and ARM registers.
9388    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
9389    MRC2
9390    MCR{cond}
9391    MCR2
9392
9393    No special properties.  */
9394
9395 struct deprecated_coproc_regs_s
9396 {
9397   unsigned cp;
9398   int opc1;
9399   unsigned crn;
9400   unsigned crm;
9401   int opc2;
9402   arm_feature_set deprecated;
9403   arm_feature_set obsoleted;
9404   const char *dep_msg;
9405   const char *obs_msg;
9406 };
9407
9408 #define DEPR_ACCESS_V8 \
9409   N_("This coprocessor register access is deprecated in ARMv8")
9410
9411 /* Table of all deprecated coprocessor registers.  */
9412 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
9413 {
9414     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
9415      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9416      DEPR_ACCESS_V8, NULL},
9417     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
9418      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9419      DEPR_ACCESS_V8, NULL},
9420     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
9421      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9422      DEPR_ACCESS_V8, NULL},
9423     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
9424      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9425      DEPR_ACCESS_V8, NULL},
9426     {14, 6, 0,  0, 0,                                   /* TEECR.  */
9427      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9428      DEPR_ACCESS_V8, NULL},
9429 };
9430
9431 #undef DEPR_ACCESS_V8
9432
9433 static const size_t deprecated_coproc_reg_count =
9434   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
9435
9436 static void
9437 do_co_reg (void)
9438 {
9439   unsigned Rd;
9440   size_t i;
9441
9442   Rd = inst.operands[2].reg;
9443   if (thumb_mode)
9444     {
9445       if (inst.instruction == 0xee000010
9446           || inst.instruction == 0xfe000010)
9447         /* MCR, MCR2  */
9448         reject_bad_reg (Rd);
9449       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9450         /* MRC, MRC2  */
9451         constraint (Rd == REG_SP, BAD_SP);
9452     }
9453   else
9454     {
9455       /* MCR */
9456       if (inst.instruction == 0xe000010)
9457         constraint (Rd == REG_PC, BAD_PC);
9458     }
9459
9460     for (i = 0; i < deprecated_coproc_reg_count; ++i)
9461       {
9462         const struct deprecated_coproc_regs_s *r =
9463           deprecated_coproc_regs + i;
9464
9465         if (inst.operands[0].reg == r->cp
9466             && inst.operands[1].imm == r->opc1
9467             && inst.operands[3].reg == r->crn
9468             && inst.operands[4].reg == r->crm
9469             && inst.operands[5].imm == r->opc2)
9470           {
9471             if (! ARM_CPU_IS_ANY (cpu_variant)
9472                 && warn_on_deprecated
9473                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
9474               as_tsktsk ("%s", r->dep_msg);
9475           }
9476       }
9477
9478   inst.instruction |= inst.operands[0].reg << 8;
9479   inst.instruction |= inst.operands[1].imm << 21;
9480   inst.instruction |= Rd << 12;
9481   inst.instruction |= inst.operands[3].reg << 16;
9482   inst.instruction |= inst.operands[4].reg;
9483   inst.instruction |= inst.operands[5].imm << 5;
9484 }
9485
9486 /* Transfer between coprocessor register and pair of ARM registers.
9487    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
9488    MCRR2
9489    MRRC{cond}
9490    MRRC2
9491
9492    Two XScale instructions are special cases of these:
9493
9494      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
9495      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
9496
9497    Result unpredictable if Rd or Rn is R15.  */
9498
9499 static void
9500 do_co_reg2c (void)
9501 {
9502   unsigned Rd, Rn;
9503
9504   Rd = inst.operands[2].reg;
9505   Rn = inst.operands[3].reg;
9506
9507   if (thumb_mode)
9508     {
9509       reject_bad_reg (Rd);
9510       reject_bad_reg (Rn);
9511     }
9512   else
9513     {
9514       constraint (Rd == REG_PC, BAD_PC);
9515       constraint (Rn == REG_PC, BAD_PC);
9516     }
9517
9518   /* Only check the MRRC{2} variants.  */
9519   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
9520     {
9521        /* If Rd == Rn, error that the operation is
9522           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
9523        constraint (Rd == Rn, BAD_OVERLAP);
9524     }
9525
9526   inst.instruction |= inst.operands[0].reg << 8;
9527   inst.instruction |= inst.operands[1].imm << 4;
9528   inst.instruction |= Rd << 12;
9529   inst.instruction |= Rn << 16;
9530   inst.instruction |= inst.operands[4].reg;
9531 }
9532
9533 static void
9534 do_cpsi (void)
9535 {
9536   inst.instruction |= inst.operands[0].imm << 6;
9537   if (inst.operands[1].present)
9538     {
9539       inst.instruction |= CPSI_MMOD;
9540       inst.instruction |= inst.operands[1].imm;
9541     }
9542 }
9543
9544 static void
9545 do_dbg (void)
9546 {
9547   inst.instruction |= inst.operands[0].imm;
9548 }
9549
9550 static void
9551 do_div (void)
9552 {
9553   unsigned Rd, Rn, Rm;
9554
9555   Rd = inst.operands[0].reg;
9556   Rn = (inst.operands[1].present
9557         ? inst.operands[1].reg : Rd);
9558   Rm = inst.operands[2].reg;
9559
9560   constraint ((Rd == REG_PC), BAD_PC);
9561   constraint ((Rn == REG_PC), BAD_PC);
9562   constraint ((Rm == REG_PC), BAD_PC);
9563
9564   inst.instruction |= Rd << 16;
9565   inst.instruction |= Rn << 0;
9566   inst.instruction |= Rm << 8;
9567 }
9568
9569 static void
9570 do_it (void)
9571 {
9572   /* There is no IT instruction in ARM mode.  We
9573      process it to do the validation as if in
9574      thumb mode, just in case the code gets
9575      assembled for thumb using the unified syntax.  */
9576
9577   inst.size = 0;
9578   if (unified_syntax)
9579     {
9580       set_pred_insn_type (IT_INSN);
9581       now_pred.mask = (inst.instruction & 0xf) | 0x10;
9582       now_pred.cc = inst.operands[0].imm;
9583     }
9584 }
9585
9586 /* If there is only one register in the register list,
9587    then return its register number.  Otherwise return -1.  */
9588 static int
9589 only_one_reg_in_list (int range)
9590 {
9591   int i = ffs (range) - 1;
9592   return (i > 15 || range != (1 << i)) ? -1 : i;
9593 }
9594
9595 static void
9596 encode_ldmstm(int from_push_pop_mnem)
9597 {
9598   int base_reg = inst.operands[0].reg;
9599   int range = inst.operands[1].imm;
9600   int one_reg;
9601
9602   inst.instruction |= base_reg << 16;
9603   inst.instruction |= range;
9604
9605   if (inst.operands[1].writeback)
9606     inst.instruction |= LDM_TYPE_2_OR_3;
9607
9608   if (inst.operands[0].writeback)
9609     {
9610       inst.instruction |= WRITE_BACK;
9611       /* Check for unpredictable uses of writeback.  */
9612       if (inst.instruction & LOAD_BIT)
9613         {
9614           /* Not allowed in LDM type 2.  */
9615           if ((inst.instruction & LDM_TYPE_2_OR_3)
9616               && ((range & (1 << REG_PC)) == 0))
9617             as_warn (_("writeback of base register is UNPREDICTABLE"));
9618           /* Only allowed if base reg not in list for other types.  */
9619           else if (range & (1 << base_reg))
9620             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
9621         }
9622       else /* STM.  */
9623         {
9624           /* Not allowed for type 2.  */
9625           if (inst.instruction & LDM_TYPE_2_OR_3)
9626             as_warn (_("writeback of base register is UNPREDICTABLE"));
9627           /* Only allowed if base reg not in list, or first in list.  */
9628           else if ((range & (1 << base_reg))
9629                    && (range & ((1 << base_reg) - 1)))
9630             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
9631         }
9632     }
9633
9634   /* If PUSH/POP has only one register, then use the A2 encoding.  */
9635   one_reg = only_one_reg_in_list (range);
9636   if (from_push_pop_mnem && one_reg >= 0)
9637     {
9638       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
9639
9640       if (is_push && one_reg == 13 /* SP */)
9641         /* PR 22483: The A2 encoding cannot be used when
9642            pushing the stack pointer as this is UNPREDICTABLE.  */
9643         return;
9644
9645       inst.instruction &= A_COND_MASK;
9646       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
9647       inst.instruction |= one_reg << 12;
9648     }
9649 }
9650
9651 static void
9652 do_ldmstm (void)
9653 {
9654   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
9655 }
9656
9657 /* ARMv5TE load-consecutive (argument parse)
9658    Mode is like LDRH.
9659
9660      LDRccD R, mode
9661      STRccD R, mode.  */
9662
9663 static void
9664 do_ldrd (void)
9665 {
9666   constraint (inst.operands[0].reg % 2 != 0,
9667               _("first transfer register must be even"));
9668   constraint (inst.operands[1].present
9669               && inst.operands[1].reg != inst.operands[0].reg + 1,
9670               _("can only transfer two consecutive registers"));
9671   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9672   constraint (!inst.operands[2].isreg, _("'[' expected"));
9673
9674   if (!inst.operands[1].present)
9675     inst.operands[1].reg = inst.operands[0].reg + 1;
9676
9677   /* encode_arm_addr_mode_3 will diagnose overlap between the base
9678      register and the first register written; we have to diagnose
9679      overlap between the base and the second register written here.  */
9680
9681   if (inst.operands[2].reg == inst.operands[1].reg
9682       && (inst.operands[2].writeback || inst.operands[2].postind))
9683     as_warn (_("base register written back, and overlaps "
9684                "second transfer register"));
9685
9686   if (!(inst.instruction & V4_STR_BIT))
9687     {
9688       /* For an index-register load, the index register must not overlap the
9689         destination (even if not write-back).  */
9690       if (inst.operands[2].immisreg
9691               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
9692               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
9693         as_warn (_("index register overlaps transfer register"));
9694     }
9695   inst.instruction |= inst.operands[0].reg << 12;
9696   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
9697 }
9698
9699 static void
9700 do_ldrex (void)
9701 {
9702   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
9703               || inst.operands[1].postind || inst.operands[1].writeback
9704               || inst.operands[1].immisreg || inst.operands[1].shifted
9705               || inst.operands[1].negative
9706               /* This can arise if the programmer has written
9707                    strex rN, rM, foo
9708                  or if they have mistakenly used a register name as the last
9709                  operand,  eg:
9710                    strex rN, rM, rX
9711                  It is very difficult to distinguish between these two cases
9712                  because "rX" might actually be a label. ie the register
9713                  name has been occluded by a symbol of the same name. So we
9714                  just generate a general 'bad addressing mode' type error
9715                  message and leave it up to the programmer to discover the
9716                  true cause and fix their mistake.  */
9717               || (inst.operands[1].reg == REG_PC),
9718               BAD_ADDR_MODE);
9719
9720   constraint (inst.relocs[0].exp.X_op != O_constant
9721               || inst.relocs[0].exp.X_add_number != 0,
9722               _("offset must be zero in ARM encoding"));
9723
9724   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
9725
9726   inst.instruction |= inst.operands[0].reg << 12;
9727   inst.instruction |= inst.operands[1].reg << 16;
9728   inst.relocs[0].type = BFD_RELOC_UNUSED;
9729 }
9730
9731 static void
9732 do_ldrexd (void)
9733 {
9734   constraint (inst.operands[0].reg % 2 != 0,
9735               _("even register required"));
9736   constraint (inst.operands[1].present
9737               && inst.operands[1].reg != inst.operands[0].reg + 1,
9738               _("can only load two consecutive registers"));
9739   /* If op 1 were present and equal to PC, this function wouldn't
9740      have been called in the first place.  */
9741   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9742
9743   inst.instruction |= inst.operands[0].reg << 12;
9744   inst.instruction |= inst.operands[2].reg << 16;
9745 }
9746
9747 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9748    which is not a multiple of four is UNPREDICTABLE.  */
9749 static void
9750 check_ldr_r15_aligned (void)
9751 {
9752   constraint (!(inst.operands[1].immisreg)
9753               && (inst.operands[0].reg == REG_PC
9754               && inst.operands[1].reg == REG_PC
9755               && (inst.relocs[0].exp.X_add_number & 0x3)),
9756               _("ldr to register 15 must be 4-byte aligned"));
9757 }
9758
9759 static void
9760 do_ldst (void)
9761 {
9762   inst.instruction |= inst.operands[0].reg << 12;
9763   if (!inst.operands[1].isreg)
9764     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9765       return;
9766   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9767   check_ldr_r15_aligned ();
9768 }
9769
9770 static void
9771 do_ldstt (void)
9772 {
9773   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9774      reject [Rn,...].  */
9775   if (inst.operands[1].preind)
9776     {
9777       constraint (inst.relocs[0].exp.X_op != O_constant
9778                   || inst.relocs[0].exp.X_add_number != 0,
9779                   _("this instruction requires a post-indexed address"));
9780
9781       inst.operands[1].preind = 0;
9782       inst.operands[1].postind = 1;
9783       inst.operands[1].writeback = 1;
9784     }
9785   inst.instruction |= inst.operands[0].reg << 12;
9786   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9787 }
9788
9789 /* Halfword and signed-byte load/store operations.  */
9790
9791 static void
9792 do_ldstv4 (void)
9793 {
9794   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9795   inst.instruction |= inst.operands[0].reg << 12;
9796   if (!inst.operands[1].isreg)
9797     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9798       return;
9799   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9800 }
9801
9802 static void
9803 do_ldsttv4 (void)
9804 {
9805   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9806      reject [Rn,...].  */
9807   if (inst.operands[1].preind)
9808     {
9809       constraint (inst.relocs[0].exp.X_op != O_constant
9810                   || inst.relocs[0].exp.X_add_number != 0,
9811                   _("this instruction requires a post-indexed address"));
9812
9813       inst.operands[1].preind = 0;
9814       inst.operands[1].postind = 1;
9815       inst.operands[1].writeback = 1;
9816     }
9817   inst.instruction |= inst.operands[0].reg << 12;
9818   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9819 }
9820
9821 /* Co-processor register load/store.
9822    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9823 static void
9824 do_lstc (void)
9825 {
9826   inst.instruction |= inst.operands[0].reg << 8;
9827   inst.instruction |= inst.operands[1].reg << 12;
9828   encode_arm_cp_address (2, TRUE, TRUE, 0);
9829 }
9830
9831 static void
9832 do_mlas (void)
9833 {
9834   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9835   if (inst.operands[0].reg == inst.operands[1].reg
9836       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9837       && !(inst.instruction & 0x00400000))
9838     as_tsktsk (_("Rd and Rm should be different in mla"));
9839
9840   inst.instruction |= inst.operands[0].reg << 16;
9841   inst.instruction |= inst.operands[1].reg;
9842   inst.instruction |= inst.operands[2].reg << 8;
9843   inst.instruction |= inst.operands[3].reg << 12;
9844 }
9845
9846 static void
9847 do_mov (void)
9848 {
9849   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9850               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9851               THUMB1_RELOC_ONLY);
9852   inst.instruction |= inst.operands[0].reg << 12;
9853   encode_arm_shifter_operand (1);
9854 }
9855
9856 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9857 static void
9858 do_mov16 (void)
9859 {
9860   bfd_vma imm;
9861   bfd_boolean top;
9862
9863   top = (inst.instruction & 0x00400000) != 0;
9864   constraint (top && inst.relocs[0].type == BFD_RELOC_ARM_MOVW,
9865               _(":lower16: not allowed in this instruction"));
9866   constraint (!top && inst.relocs[0].type == BFD_RELOC_ARM_MOVT,
9867               _(":upper16: not allowed in this instruction"));
9868   inst.instruction |= inst.operands[0].reg << 12;
9869   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
9870     {
9871       imm = inst.relocs[0].exp.X_add_number;
9872       /* The value is in two pieces: 0:11, 16:19.  */
9873       inst.instruction |= (imm & 0x00000fff);
9874       inst.instruction |= (imm & 0x0000f000) << 4;
9875     }
9876 }
9877
9878 static int
9879 do_vfp_nsyn_mrs (void)
9880 {
9881   if (inst.operands[0].isvec)
9882     {
9883       if (inst.operands[1].reg != 1)
9884         first_error (_("operand 1 must be FPSCR"));
9885       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9886       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9887       do_vfp_nsyn_opcode ("fmstat");
9888     }
9889   else if (inst.operands[1].isvec)
9890     do_vfp_nsyn_opcode ("fmrx");
9891   else
9892     return FAIL;
9893
9894   return SUCCESS;
9895 }
9896
9897 static int
9898 do_vfp_nsyn_msr (void)
9899 {
9900   if (inst.operands[0].isvec)
9901     do_vfp_nsyn_opcode ("fmxr");
9902   else
9903     return FAIL;
9904
9905   return SUCCESS;
9906 }
9907
9908 static void
9909 do_vmrs (void)
9910 {
9911   unsigned Rt = inst.operands[0].reg;
9912
9913   if (thumb_mode && Rt == REG_SP)
9914     {
9915       inst.error = BAD_SP;
9916       return;
9917     }
9918
9919   switch (inst.operands[1].reg)
9920     {
9921     /* MVFR2 is only valid for Armv8-A.  */
9922     case 5:
9923       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9924                   _(BAD_FPU));
9925       break;
9926
9927     /* Check for new Armv8.1-M Mainline changes to <spec_reg>.  */
9928     case 1: /* fpscr.  */
9929       constraint (!(ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9930                     || ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9931                   _(BAD_FPU));
9932       break;
9933
9934     case 14: /* fpcxt_ns.  */
9935     case 15: /* fpcxt_s.  */
9936       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main),
9937                   _("selected processor does not support instruction"));
9938       break;
9939
9940     case  2: /* fpscr_nzcvqc.  */
9941     case 12: /* vpr.  */
9942     case 13: /* p0.  */
9943       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main)
9944                   || (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9945                       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9946                   _("selected processor does not support instruction"));
9947       if (inst.operands[0].reg != 2
9948           && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
9949         as_warn (_("accessing MVE system register without MVE is UNPREDICTABLE"));
9950       break;
9951
9952     default:
9953       break;
9954     }
9955
9956   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9957   if (!inst.operands[0].isvec && Rt == REG_PC)
9958     {
9959       inst.error = BAD_PC;
9960       return;
9961     }
9962
9963   /* If we get through parsing the register name, we just insert the number
9964      generated into the instruction without further validation.  */
9965   inst.instruction |= (inst.operands[1].reg << 16);
9966   inst.instruction |= (Rt << 12);
9967 }
9968
9969 static void
9970 do_vmsr (void)
9971 {
9972   unsigned Rt = inst.operands[1].reg;
9973
9974   if (thumb_mode)
9975     reject_bad_reg (Rt);
9976   else if (Rt == REG_PC)
9977     {
9978       inst.error = BAD_PC;
9979       return;
9980     }
9981
9982   switch (inst.operands[0].reg)
9983     {
9984     /* MVFR2 is only valid for Armv8-A.  */
9985     case 5:
9986       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9987                   _(BAD_FPU));
9988       break;
9989
9990     /* Check for new Armv8.1-M Mainline changes to <spec_reg>.  */
9991     case  1: /* fpcr.  */
9992       constraint (!(ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9993                     || ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9994                   _(BAD_FPU));
9995       break;
9996
9997     case 14: /* fpcxt_ns.  */
9998     case 15: /* fpcxt_s.  */
9999       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main),
10000                   _("selected processor does not support instruction"));
10001       break;
10002
10003     case  2: /* fpscr_nzcvqc.  */
10004     case 12: /* vpr.  */
10005     case 13: /* p0.  */
10006       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main)
10007                   || (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
10008                       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
10009                   _("selected processor does not support instruction"));
10010       if (inst.operands[0].reg != 2
10011           && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
10012         as_warn (_("accessing MVE system register without MVE is UNPREDICTABLE"));
10013       break;
10014
10015     default:
10016       break;
10017     }
10018
10019   /* If we get through parsing the register name, we just insert the number
10020      generated into the instruction without further validation.  */
10021   inst.instruction |= (inst.operands[0].reg << 16);
10022   inst.instruction |= (Rt << 12);
10023 }
10024
10025 static void
10026 do_mrs (void)
10027 {
10028   unsigned br;
10029
10030   if (do_vfp_nsyn_mrs () == SUCCESS)
10031     return;
10032
10033   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10034   inst.instruction |= inst.operands[0].reg << 12;
10035
10036   if (inst.operands[1].isreg)
10037     {
10038       br = inst.operands[1].reg;
10039       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
10040         as_bad (_("bad register for mrs"));
10041     }
10042   else
10043     {
10044       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
10045       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
10046                   != (PSR_c|PSR_f),
10047                   _("'APSR', 'CPSR' or 'SPSR' expected"));
10048       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
10049     }
10050
10051   inst.instruction |= br;
10052 }
10053
10054 /* Two possible forms:
10055       "{C|S}PSR_<field>, Rm",
10056       "{C|S}PSR_f, #expression".  */
10057
10058 static void
10059 do_msr (void)
10060 {
10061   if (do_vfp_nsyn_msr () == SUCCESS)
10062     return;
10063
10064   inst.instruction |= inst.operands[0].imm;
10065   if (inst.operands[1].isreg)
10066     inst.instruction |= inst.operands[1].reg;
10067   else
10068     {
10069       inst.instruction |= INST_IMMEDIATE;
10070       inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
10071       inst.relocs[0].pc_rel = 0;
10072     }
10073 }
10074
10075 static void
10076 do_mul (void)
10077 {
10078   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
10079
10080   if (!inst.operands[2].present)
10081     inst.operands[2].reg = inst.operands[0].reg;
10082   inst.instruction |= inst.operands[0].reg << 16;
10083   inst.instruction |= inst.operands[1].reg;
10084   inst.instruction |= inst.operands[2].reg << 8;
10085
10086   if (inst.operands[0].reg == inst.operands[1].reg
10087       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
10088     as_tsktsk (_("Rd and Rm should be different in mul"));
10089 }
10090
10091 /* Long Multiply Parser
10092    UMULL RdLo, RdHi, Rm, Rs
10093    SMULL RdLo, RdHi, Rm, Rs
10094    UMLAL RdLo, RdHi, Rm, Rs
10095    SMLAL RdLo, RdHi, Rm, Rs.  */
10096
10097 static void
10098 do_mull (void)
10099 {
10100   inst.instruction |= inst.operands[0].reg << 12;
10101   inst.instruction |= inst.operands[1].reg << 16;
10102   inst.instruction |= inst.operands[2].reg;
10103   inst.instruction |= inst.operands[3].reg << 8;
10104
10105   /* rdhi and rdlo must be different.  */
10106   if (inst.operands[0].reg == inst.operands[1].reg)
10107     as_tsktsk (_("rdhi and rdlo must be different"));
10108
10109   /* rdhi, rdlo and rm must all be different before armv6.  */
10110   if ((inst.operands[0].reg == inst.operands[2].reg
10111       || inst.operands[1].reg == inst.operands[2].reg)
10112       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
10113     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
10114 }
10115
10116 static void
10117 do_nop (void)
10118 {
10119   if (inst.operands[0].present
10120       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
10121     {
10122       /* Architectural NOP hints are CPSR sets with no bits selected.  */
10123       inst.instruction &= 0xf0000000;
10124       inst.instruction |= 0x0320f000;
10125       if (inst.operands[0].present)
10126         inst.instruction |= inst.operands[0].imm;
10127     }
10128 }
10129
10130 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
10131    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
10132    Condition defaults to COND_ALWAYS.
10133    Error if Rd, Rn or Rm are R15.  */
10134
10135 static void
10136 do_pkhbt (void)
10137 {
10138   inst.instruction |= inst.operands[0].reg << 12;
10139   inst.instruction |= inst.operands[1].reg << 16;
10140   inst.instruction |= inst.operands[2].reg;
10141   if (inst.operands[3].present)
10142     encode_arm_shift (3);
10143 }
10144
10145 /* ARM V6 PKHTB (Argument Parse).  */
10146
10147 static void
10148 do_pkhtb (void)
10149 {
10150   if (!inst.operands[3].present)
10151     {
10152       /* If the shift specifier is omitted, turn the instruction
10153          into pkhbt rd, rm, rn. */
10154       inst.instruction &= 0xfff00010;
10155       inst.instruction |= inst.operands[0].reg << 12;
10156       inst.instruction |= inst.operands[1].reg;
10157       inst.instruction |= inst.operands[2].reg << 16;
10158     }
10159   else
10160     {
10161       inst.instruction |= inst.operands[0].reg << 12;
10162       inst.instruction |= inst.operands[1].reg << 16;
10163       inst.instruction |= inst.operands[2].reg;
10164       encode_arm_shift (3);
10165     }
10166 }
10167
10168 /* ARMv5TE: Preload-Cache
10169    MP Extensions: Preload for write
10170
10171     PLD(W) <addr_mode>
10172
10173   Syntactically, like LDR with B=1, W=0, L=1.  */
10174
10175 static void
10176 do_pld (void)
10177 {
10178   constraint (!inst.operands[0].isreg,
10179               _("'[' expected after PLD mnemonic"));
10180   constraint (inst.operands[0].postind,
10181               _("post-indexed expression used in preload instruction"));
10182   constraint (inst.operands[0].writeback,
10183               _("writeback used in preload instruction"));
10184   constraint (!inst.operands[0].preind,
10185               _("unindexed addressing used in preload instruction"));
10186   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10187 }
10188
10189 /* ARMv7: PLI <addr_mode>  */
10190 static void
10191 do_pli (void)
10192 {
10193   constraint (!inst.operands[0].isreg,
10194               _("'[' expected after PLI mnemonic"));
10195   constraint (inst.operands[0].postind,
10196               _("post-indexed expression used in preload instruction"));
10197   constraint (inst.operands[0].writeback,
10198               _("writeback used in preload instruction"));
10199   constraint (!inst.operands[0].preind,
10200               _("unindexed addressing used in preload instruction"));
10201   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10202   inst.instruction &= ~PRE_INDEX;
10203 }
10204
10205 static void
10206 do_push_pop (void)
10207 {
10208   constraint (inst.operands[0].writeback,
10209               _("push/pop do not support {reglist}^"));
10210   inst.operands[1] = inst.operands[0];
10211   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
10212   inst.operands[0].isreg = 1;
10213   inst.operands[0].writeback = 1;
10214   inst.operands[0].reg = REG_SP;
10215   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
10216 }
10217
10218 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
10219    word at the specified address and the following word
10220    respectively.
10221    Unconditionally executed.
10222    Error if Rn is R15.  */
10223
10224 static void
10225 do_rfe (void)
10226 {
10227   inst.instruction |= inst.operands[0].reg << 16;
10228   if (inst.operands[0].writeback)
10229     inst.instruction |= WRITE_BACK;
10230 }
10231
10232 /* ARM V6 ssat (argument parse).  */
10233
10234 static void
10235 do_ssat (void)
10236 {
10237   inst.instruction |= inst.operands[0].reg << 12;
10238   inst.instruction |= (inst.operands[1].imm - 1) << 16;
10239   inst.instruction |= inst.operands[2].reg;
10240
10241   if (inst.operands[3].present)
10242     encode_arm_shift (3);
10243 }
10244
10245 /* ARM V6 usat (argument parse).  */
10246
10247 static void
10248 do_usat (void)
10249 {
10250   inst.instruction |= inst.operands[0].reg << 12;
10251   inst.instruction |= inst.operands[1].imm << 16;
10252   inst.instruction |= inst.operands[2].reg;
10253
10254   if (inst.operands[3].present)
10255     encode_arm_shift (3);
10256 }
10257
10258 /* ARM V6 ssat16 (argument parse).  */
10259
10260 static void
10261 do_ssat16 (void)
10262 {
10263   inst.instruction |= inst.operands[0].reg << 12;
10264   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
10265   inst.instruction |= inst.operands[2].reg;
10266 }
10267
10268 static void
10269 do_usat16 (void)
10270 {
10271   inst.instruction |= inst.operands[0].reg << 12;
10272   inst.instruction |= inst.operands[1].imm << 16;
10273   inst.instruction |= inst.operands[2].reg;
10274 }
10275
10276 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
10277    preserving the other bits.
10278
10279    setend <endian_specifier>, where <endian_specifier> is either
10280    BE or LE.  */
10281
10282 static void
10283 do_setend (void)
10284 {
10285   if (warn_on_deprecated
10286       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10287       as_tsktsk (_("setend use is deprecated for ARMv8"));
10288
10289   if (inst.operands[0].imm)
10290     inst.instruction |= 0x200;
10291 }
10292
10293 static void
10294 do_shift (void)
10295 {
10296   unsigned int Rm = (inst.operands[1].present
10297                      ? inst.operands[1].reg
10298                      : inst.operands[0].reg);
10299
10300   inst.instruction |= inst.operands[0].reg << 12;
10301   inst.instruction |= Rm;
10302   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
10303     {
10304       inst.instruction |= inst.operands[2].reg << 8;
10305       inst.instruction |= SHIFT_BY_REG;
10306       /* PR 12854: Error on extraneous shifts.  */
10307       constraint (inst.operands[2].shifted,
10308                   _("extraneous shift as part of operand to shift insn"));
10309     }
10310   else
10311     inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
10312 }
10313
10314 static void
10315 do_smc (void)
10316 {
10317   unsigned int value = inst.relocs[0].exp.X_add_number;
10318   constraint (value > 0xf, _("immediate too large (bigger than 0xF)"));
10319
10320   inst.relocs[0].type = BFD_RELOC_ARM_SMC;
10321   inst.relocs[0].pc_rel = 0;
10322 }
10323
10324 static void
10325 do_hvc (void)
10326 {
10327   inst.relocs[0].type = BFD_RELOC_ARM_HVC;
10328   inst.relocs[0].pc_rel = 0;
10329 }
10330
10331 static void
10332 do_swi (void)
10333 {
10334   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
10335   inst.relocs[0].pc_rel = 0;
10336 }
10337
10338 static void
10339 do_setpan (void)
10340 {
10341   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10342               _("selected processor does not support SETPAN instruction"));
10343
10344   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
10345 }
10346
10347 static void
10348 do_t_setpan (void)
10349 {
10350   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10351               _("selected processor does not support SETPAN instruction"));
10352
10353   inst.instruction |= (inst.operands[0].imm << 3);
10354 }
10355
10356 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
10357    SMLAxy{cond} Rd,Rm,Rs,Rn
10358    SMLAWy{cond} Rd,Rm,Rs,Rn
10359    Error if any register is R15.  */
10360
10361 static void
10362 do_smla (void)
10363 {
10364   inst.instruction |= inst.operands[0].reg << 16;
10365   inst.instruction |= inst.operands[1].reg;
10366   inst.instruction |= inst.operands[2].reg << 8;
10367   inst.instruction |= inst.operands[3].reg << 12;
10368 }
10369
10370 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
10371    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
10372    Error if any register is R15.
10373    Warning if Rdlo == Rdhi.  */
10374
10375 static void
10376 do_smlal (void)
10377 {
10378   inst.instruction |= inst.operands[0].reg << 12;
10379   inst.instruction |= inst.operands[1].reg << 16;
10380   inst.instruction |= inst.operands[2].reg;
10381   inst.instruction |= inst.operands[3].reg << 8;
10382
10383   if (inst.operands[0].reg == inst.operands[1].reg)
10384     as_tsktsk (_("rdhi and rdlo must be different"));
10385 }
10386
10387 /* ARM V5E (El Segundo) signed-multiply (argument parse)
10388    SMULxy{cond} Rd,Rm,Rs
10389    Error if any register is R15.  */
10390
10391 static void
10392 do_smul (void)
10393 {
10394   inst.instruction |= inst.operands[0].reg << 16;
10395   inst.instruction |= inst.operands[1].reg;
10396   inst.instruction |= inst.operands[2].reg << 8;
10397 }
10398
10399 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
10400    the same for both ARM and Thumb-2.  */
10401
10402 static void
10403 do_srs (void)
10404 {
10405   int reg;
10406
10407   if (inst.operands[0].present)
10408     {
10409       reg = inst.operands[0].reg;
10410       constraint (reg != REG_SP, _("SRS base register must be r13"));
10411     }
10412   else
10413     reg = REG_SP;
10414
10415   inst.instruction |= reg << 16;
10416   inst.instruction |= inst.operands[1].imm;
10417   if (inst.operands[0].writeback || inst.operands[1].writeback)
10418     inst.instruction |= WRITE_BACK;
10419 }
10420
10421 /* ARM V6 strex (argument parse).  */
10422
10423 static void
10424 do_strex (void)
10425 {
10426   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10427               || inst.operands[2].postind || inst.operands[2].writeback
10428               || inst.operands[2].immisreg || inst.operands[2].shifted
10429               || inst.operands[2].negative
10430               /* See comment in do_ldrex().  */
10431               || (inst.operands[2].reg == REG_PC),
10432               BAD_ADDR_MODE);
10433
10434   constraint (inst.operands[0].reg == inst.operands[1].reg
10435               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10436
10437   constraint (inst.relocs[0].exp.X_op != O_constant
10438               || inst.relocs[0].exp.X_add_number != 0,
10439               _("offset must be zero in ARM encoding"));
10440
10441   inst.instruction |= inst.operands[0].reg << 12;
10442   inst.instruction |= inst.operands[1].reg;
10443   inst.instruction |= inst.operands[2].reg << 16;
10444   inst.relocs[0].type = BFD_RELOC_UNUSED;
10445 }
10446
10447 static void
10448 do_t_strexbh (void)
10449 {
10450   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10451               || inst.operands[2].postind || inst.operands[2].writeback
10452               || inst.operands[2].immisreg || inst.operands[2].shifted
10453               || inst.operands[2].negative,
10454               BAD_ADDR_MODE);
10455
10456   constraint (inst.operands[0].reg == inst.operands[1].reg
10457               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10458
10459   do_rm_rd_rn ();
10460 }
10461
10462 static void
10463 do_strexd (void)
10464 {
10465   constraint (inst.operands[1].reg % 2 != 0,
10466               _("even register required"));
10467   constraint (inst.operands[2].present
10468               && inst.operands[2].reg != inst.operands[1].reg + 1,
10469               _("can only store two consecutive registers"));
10470   /* If op 2 were present and equal to PC, this function wouldn't
10471      have been called in the first place.  */
10472   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
10473
10474   constraint (inst.operands[0].reg == inst.operands[1].reg
10475               || inst.operands[0].reg == inst.operands[1].reg + 1
10476               || inst.operands[0].reg == inst.operands[3].reg,
10477               BAD_OVERLAP);
10478
10479   inst.instruction |= inst.operands[0].reg << 12;
10480   inst.instruction |= inst.operands[1].reg;
10481   inst.instruction |= inst.operands[3].reg << 16;
10482 }
10483
10484 /* ARM V8 STRL.  */
10485 static void
10486 do_stlex (void)
10487 {
10488   constraint (inst.operands[0].reg == inst.operands[1].reg
10489               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10490
10491   do_rd_rm_rn ();
10492 }
10493
10494 static void
10495 do_t_stlex (void)
10496 {
10497   constraint (inst.operands[0].reg == inst.operands[1].reg
10498               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10499
10500   do_rm_rd_rn ();
10501 }
10502
10503 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
10504    extends it to 32-bits, and adds the result to a value in another
10505    register.  You can specify a rotation by 0, 8, 16, or 24 bits
10506    before extracting the 16-bit value.
10507    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
10508    Condition defaults to COND_ALWAYS.
10509    Error if any register uses R15.  */
10510
10511 static void
10512 do_sxtah (void)
10513 {
10514   inst.instruction |= inst.operands[0].reg << 12;
10515   inst.instruction |= inst.operands[1].reg << 16;
10516   inst.instruction |= inst.operands[2].reg;
10517   inst.instruction |= inst.operands[3].imm << 10;
10518 }
10519
10520 /* ARM V6 SXTH.
10521
10522    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
10523    Condition defaults to COND_ALWAYS.
10524    Error if any register uses R15.  */
10525
10526 static void
10527 do_sxth (void)
10528 {
10529   inst.instruction |= inst.operands[0].reg << 12;
10530   inst.instruction |= inst.operands[1].reg;
10531   inst.instruction |= inst.operands[2].imm << 10;
10532 }
10533 \f
10534 /* VFP instructions.  In a logical order: SP variant first, monad
10535    before dyad, arithmetic then move then load/store.  */
10536
10537 static void
10538 do_vfp_sp_monadic (void)
10539 {
10540   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10541               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10542               _(BAD_FPU));
10543
10544   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10545   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10546 }
10547
10548 static void
10549 do_vfp_sp_dyadic (void)
10550 {
10551   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10552   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10553   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10554 }
10555
10556 static void
10557 do_vfp_sp_compare_z (void)
10558 {
10559   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10560 }
10561
10562 static void
10563 do_vfp_dp_sp_cvt (void)
10564 {
10565   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10566   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10567 }
10568
10569 static void
10570 do_vfp_sp_dp_cvt (void)
10571 {
10572   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10573   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10574 }
10575
10576 static void
10577 do_vfp_reg_from_sp (void)
10578 {
10579   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10580              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10581              _(BAD_FPU));
10582
10583   inst.instruction |= inst.operands[0].reg << 12;
10584   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10585 }
10586
10587 static void
10588 do_vfp_reg2_from_sp2 (void)
10589 {
10590   constraint (inst.operands[2].imm != 2,
10591               _("only two consecutive VFP SP registers allowed here"));
10592   inst.instruction |= inst.operands[0].reg << 12;
10593   inst.instruction |= inst.operands[1].reg << 16;
10594   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10595 }
10596
10597 static void
10598 do_vfp_sp_from_reg (void)
10599 {
10600   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10601              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10602              _(BAD_FPU));
10603
10604   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
10605   inst.instruction |= inst.operands[1].reg << 12;
10606 }
10607
10608 static void
10609 do_vfp_sp2_from_reg2 (void)
10610 {
10611   constraint (inst.operands[0].imm != 2,
10612               _("only two consecutive VFP SP registers allowed here"));
10613   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
10614   inst.instruction |= inst.operands[1].reg << 12;
10615   inst.instruction |= inst.operands[2].reg << 16;
10616 }
10617
10618 static void
10619 do_vfp_sp_ldst (void)
10620 {
10621   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10622   encode_arm_cp_address (1, FALSE, TRUE, 0);
10623 }
10624
10625 static void
10626 do_vfp_dp_ldst (void)
10627 {
10628   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10629   encode_arm_cp_address (1, FALSE, TRUE, 0);
10630 }
10631
10632
10633 static void
10634 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
10635 {
10636   if (inst.operands[0].writeback)
10637     inst.instruction |= WRITE_BACK;
10638   else
10639     constraint (ldstm_type != VFP_LDSTMIA,
10640                 _("this addressing mode requires base-register writeback"));
10641   inst.instruction |= inst.operands[0].reg << 16;
10642   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
10643   inst.instruction |= inst.operands[1].imm;
10644 }
10645
10646 static void
10647 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
10648 {
10649   int count;
10650
10651   if (inst.operands[0].writeback)
10652     inst.instruction |= WRITE_BACK;
10653   else
10654     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
10655                 _("this addressing mode requires base-register writeback"));
10656
10657   inst.instruction |= inst.operands[0].reg << 16;
10658   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10659
10660   count = inst.operands[1].imm << 1;
10661   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
10662     count += 1;
10663
10664   inst.instruction |= count;
10665 }
10666
10667 static void
10668 do_vfp_sp_ldstmia (void)
10669 {
10670   vfp_sp_ldstm (VFP_LDSTMIA);
10671 }
10672
10673 static void
10674 do_vfp_sp_ldstmdb (void)
10675 {
10676   vfp_sp_ldstm (VFP_LDSTMDB);
10677 }
10678
10679 static void
10680 do_vfp_dp_ldstmia (void)
10681 {
10682   vfp_dp_ldstm (VFP_LDSTMIA);
10683 }
10684
10685 static void
10686 do_vfp_dp_ldstmdb (void)
10687 {
10688   vfp_dp_ldstm (VFP_LDSTMDB);
10689 }
10690
10691 static void
10692 do_vfp_xp_ldstmia (void)
10693 {
10694   vfp_dp_ldstm (VFP_LDSTMIAX);
10695 }
10696
10697 static void
10698 do_vfp_xp_ldstmdb (void)
10699 {
10700   vfp_dp_ldstm (VFP_LDSTMDBX);
10701 }
10702
10703 static void
10704 do_vfp_dp_rd_rm (void)
10705 {
10706   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
10707               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10708               _(BAD_FPU));
10709
10710   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10711   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10712 }
10713
10714 static void
10715 do_vfp_dp_rn_rd (void)
10716 {
10717   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
10718   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10719 }
10720
10721 static void
10722 do_vfp_dp_rd_rn (void)
10723 {
10724   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10725   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10726 }
10727
10728 static void
10729 do_vfp_dp_rd_rn_rm (void)
10730 {
10731   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10732               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10733               _(BAD_FPU));
10734
10735   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10736   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10737   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
10738 }
10739
10740 static void
10741 do_vfp_dp_rd (void)
10742 {
10743   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10744 }
10745
10746 static void
10747 do_vfp_dp_rm_rd_rn (void)
10748 {
10749   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10750               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10751               _(BAD_FPU));
10752
10753   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
10754   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10755   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
10756 }
10757
10758 /* VFPv3 instructions.  */
10759 static void
10760 do_vfp_sp_const (void)
10761 {
10762   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10763   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10764   inst.instruction |= (inst.operands[1].imm & 0x0f);
10765 }
10766
10767 static void
10768 do_vfp_dp_const (void)
10769 {
10770   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10771   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10772   inst.instruction |= (inst.operands[1].imm & 0x0f);
10773 }
10774
10775 static void
10776 vfp_conv (int srcsize)
10777 {
10778   int immbits = srcsize - inst.operands[1].imm;
10779
10780   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
10781     {
10782       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
10783          i.e. immbits must be in range 0 - 16.  */
10784       inst.error = _("immediate value out of range, expected range [0, 16]");
10785       return;
10786     }
10787   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
10788     {
10789       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
10790          i.e. immbits must be in range 0 - 31.  */
10791       inst.error = _("immediate value out of range, expected range [1, 32]");
10792       return;
10793     }
10794
10795   inst.instruction |= (immbits & 1) << 5;
10796   inst.instruction |= (immbits >> 1);
10797 }
10798
10799 static void
10800 do_vfp_sp_conv_16 (void)
10801 {
10802   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10803   vfp_conv (16);
10804 }
10805
10806 static void
10807 do_vfp_dp_conv_16 (void)
10808 {
10809   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10810   vfp_conv (16);
10811 }
10812
10813 static void
10814 do_vfp_sp_conv_32 (void)
10815 {
10816   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10817   vfp_conv (32);
10818 }
10819
10820 static void
10821 do_vfp_dp_conv_32 (void)
10822 {
10823   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10824   vfp_conv (32);
10825 }
10826 \f
10827 /* FPA instructions.  Also in a logical order.  */
10828
10829 static void
10830 do_fpa_cmp (void)
10831 {
10832   inst.instruction |= inst.operands[0].reg << 16;
10833   inst.instruction |= inst.operands[1].reg;
10834 }
10835
10836 static void
10837 do_fpa_ldmstm (void)
10838 {
10839   inst.instruction |= inst.operands[0].reg << 12;
10840   switch (inst.operands[1].imm)
10841     {
10842     case 1: inst.instruction |= CP_T_X;          break;
10843     case 2: inst.instruction |= CP_T_Y;          break;
10844     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10845     case 4:                                      break;
10846     default: abort ();
10847     }
10848
10849   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10850     {
10851       /* The instruction specified "ea" or "fd", so we can only accept
10852          [Rn]{!}.  The instruction does not really support stacking or
10853          unstacking, so we have to emulate these by setting appropriate
10854          bits and offsets.  */
10855       constraint (inst.relocs[0].exp.X_op != O_constant
10856                   || inst.relocs[0].exp.X_add_number != 0,
10857                   _("this instruction does not support indexing"));
10858
10859       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10860         inst.relocs[0].exp.X_add_number = 12 * inst.operands[1].imm;
10861
10862       if (!(inst.instruction & INDEX_UP))
10863         inst.relocs[0].exp.X_add_number = -inst.relocs[0].exp.X_add_number;
10864
10865       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10866         {
10867           inst.operands[2].preind = 0;
10868           inst.operands[2].postind = 1;
10869         }
10870     }
10871
10872   encode_arm_cp_address (2, TRUE, TRUE, 0);
10873 }
10874 \f
10875 /* iWMMXt instructions: strictly in alphabetical order.  */
10876
10877 static void
10878 do_iwmmxt_tandorc (void)
10879 {
10880   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10881 }
10882
10883 static void
10884 do_iwmmxt_textrc (void)
10885 {
10886   inst.instruction |= inst.operands[0].reg << 12;
10887   inst.instruction |= inst.operands[1].imm;
10888 }
10889
10890 static void
10891 do_iwmmxt_textrm (void)
10892 {
10893   inst.instruction |= inst.operands[0].reg << 12;
10894   inst.instruction |= inst.operands[1].reg << 16;
10895   inst.instruction |= inst.operands[2].imm;
10896 }
10897
10898 static void
10899 do_iwmmxt_tinsr (void)
10900 {
10901   inst.instruction |= inst.operands[0].reg << 16;
10902   inst.instruction |= inst.operands[1].reg << 12;
10903   inst.instruction |= inst.operands[2].imm;
10904 }
10905
10906 static void
10907 do_iwmmxt_tmia (void)
10908 {
10909   inst.instruction |= inst.operands[0].reg << 5;
10910   inst.instruction |= inst.operands[1].reg;
10911   inst.instruction |= inst.operands[2].reg << 12;
10912 }
10913
10914 static void
10915 do_iwmmxt_waligni (void)
10916 {
10917   inst.instruction |= inst.operands[0].reg << 12;
10918   inst.instruction |= inst.operands[1].reg << 16;
10919   inst.instruction |= inst.operands[2].reg;
10920   inst.instruction |= inst.operands[3].imm << 20;
10921 }
10922
10923 static void
10924 do_iwmmxt_wmerge (void)
10925 {
10926   inst.instruction |= inst.operands[0].reg << 12;
10927   inst.instruction |= inst.operands[1].reg << 16;
10928   inst.instruction |= inst.operands[2].reg;
10929   inst.instruction |= inst.operands[3].imm << 21;
10930 }
10931
10932 static void
10933 do_iwmmxt_wmov (void)
10934 {
10935   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10936   inst.instruction |= inst.operands[0].reg << 12;
10937   inst.instruction |= inst.operands[1].reg << 16;
10938   inst.instruction |= inst.operands[1].reg;
10939 }
10940
10941 static void
10942 do_iwmmxt_wldstbh (void)
10943 {
10944   int reloc;
10945   inst.instruction |= inst.operands[0].reg << 12;
10946   if (thumb_mode)
10947     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10948   else
10949     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10950   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10951 }
10952
10953 static void
10954 do_iwmmxt_wldstw (void)
10955 {
10956   /* RIWR_RIWC clears .isreg for a control register.  */
10957   if (!inst.operands[0].isreg)
10958     {
10959       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10960       inst.instruction |= 0xf0000000;
10961     }
10962
10963   inst.instruction |= inst.operands[0].reg << 12;
10964   encode_arm_cp_address (1, TRUE, TRUE, 0);
10965 }
10966
10967 static void
10968 do_iwmmxt_wldstd (void)
10969 {
10970   inst.instruction |= inst.operands[0].reg << 12;
10971   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10972       && inst.operands[1].immisreg)
10973     {
10974       inst.instruction &= ~0x1a000ff;
10975       inst.instruction |= (0xfU << 28);
10976       if (inst.operands[1].preind)
10977         inst.instruction |= PRE_INDEX;
10978       if (!inst.operands[1].negative)
10979         inst.instruction |= INDEX_UP;
10980       if (inst.operands[1].writeback)
10981         inst.instruction |= WRITE_BACK;
10982       inst.instruction |= inst.operands[1].reg << 16;
10983       inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
10984       inst.instruction |= inst.operands[1].imm;
10985     }
10986   else
10987     encode_arm_cp_address (1, TRUE, FALSE, 0);
10988 }
10989
10990 static void
10991 do_iwmmxt_wshufh (void)
10992 {
10993   inst.instruction |= inst.operands[0].reg << 12;
10994   inst.instruction |= inst.operands[1].reg << 16;
10995   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10996   inst.instruction |= (inst.operands[2].imm & 0x0f);
10997 }
10998
10999 static void
11000 do_iwmmxt_wzero (void)
11001 {
11002   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
11003   inst.instruction |= inst.operands[0].reg;
11004   inst.instruction |= inst.operands[0].reg << 12;
11005   inst.instruction |= inst.operands[0].reg << 16;
11006 }
11007
11008 static void
11009 do_iwmmxt_wrwrwr_or_imm5 (void)
11010 {
11011   if (inst.operands[2].isreg)
11012     do_rd_rn_rm ();
11013   else {
11014     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
11015                 _("immediate operand requires iWMMXt2"));
11016     do_rd_rn ();
11017     if (inst.operands[2].imm == 0)
11018       {
11019         switch ((inst.instruction >> 20) & 0xf)
11020           {
11021           case 4:
11022           case 5:
11023           case 6:
11024           case 7:
11025             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
11026             inst.operands[2].imm = 16;
11027             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
11028             break;
11029           case 8:
11030           case 9:
11031           case 10:
11032           case 11:
11033             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
11034             inst.operands[2].imm = 32;
11035             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
11036             break;
11037           case 12:
11038           case 13:
11039           case 14:
11040           case 15:
11041             {
11042               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
11043               unsigned long wrn;
11044               wrn = (inst.instruction >> 16) & 0xf;
11045               inst.instruction &= 0xff0fff0f;
11046               inst.instruction |= wrn;
11047               /* Bail out here; the instruction is now assembled.  */
11048               return;
11049             }
11050           }
11051       }
11052     /* Map 32 -> 0, etc.  */
11053     inst.operands[2].imm &= 0x1f;
11054     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
11055   }
11056 }
11057 \f
11058 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
11059    operations first, then control, shift, and load/store.  */
11060
11061 /* Insns like "foo X,Y,Z".  */
11062
11063 static void
11064 do_mav_triple (void)
11065 {
11066   inst.instruction |= inst.operands[0].reg << 16;
11067   inst.instruction |= inst.operands[1].reg;
11068   inst.instruction |= inst.operands[2].reg << 12;
11069 }
11070
11071 /* Insns like "foo W,X,Y,Z".
11072     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
11073
11074 static void
11075 do_mav_quad (void)
11076 {
11077   inst.instruction |= inst.operands[0].reg << 5;
11078   inst.instruction |= inst.operands[1].reg << 12;
11079   inst.instruction |= inst.operands[2].reg << 16;
11080   inst.instruction |= inst.operands[3].reg;
11081 }
11082
11083 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
11084 static void
11085 do_mav_dspsc (void)
11086 {
11087   inst.instruction |= inst.operands[1].reg << 12;
11088 }
11089
11090 /* Maverick shift immediate instructions.
11091    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
11092    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
11093
11094 static void
11095 do_mav_shift (void)
11096 {
11097   int imm = inst.operands[2].imm;
11098
11099   inst.instruction |= inst.operands[0].reg << 12;
11100   inst.instruction |= inst.operands[1].reg << 16;
11101
11102   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
11103      Bits 5-7 of the insn should have bits 4-6 of the immediate.
11104      Bit 4 should be 0.  */
11105   imm = (imm & 0xf) | ((imm & 0x70) << 1);
11106
11107   inst.instruction |= imm;
11108 }
11109 \f
11110 /* XScale instructions.  Also sorted arithmetic before move.  */
11111
11112 /* Xscale multiply-accumulate (argument parse)
11113      MIAcc   acc0,Rm,Rs
11114      MIAPHcc acc0,Rm,Rs
11115      MIAxycc acc0,Rm,Rs.  */
11116
11117 static void
11118 do_xsc_mia (void)
11119 {
11120   inst.instruction |= inst.operands[1].reg;
11121   inst.instruction |= inst.operands[2].reg << 12;
11122 }
11123
11124 /* Xscale move-accumulator-register (argument parse)
11125
11126      MARcc   acc0,RdLo,RdHi.  */
11127
11128 static void
11129 do_xsc_mar (void)
11130 {
11131   inst.instruction |= inst.operands[1].reg << 12;
11132   inst.instruction |= inst.operands[2].reg << 16;
11133 }
11134
11135 /* Xscale move-register-accumulator (argument parse)
11136
11137      MRAcc   RdLo,RdHi,acc0.  */
11138
11139 static void
11140 do_xsc_mra (void)
11141 {
11142   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
11143   inst.instruction |= inst.operands[0].reg << 12;
11144   inst.instruction |= inst.operands[1].reg << 16;
11145 }
11146 \f
11147 /* Encoding functions relevant only to Thumb.  */
11148
11149 /* inst.operands[i] is a shifted-register operand; encode
11150    it into inst.instruction in the format used by Thumb32.  */
11151
11152 static void
11153 encode_thumb32_shifted_operand (int i)
11154 {
11155   unsigned int value = inst.relocs[0].exp.X_add_number;
11156   unsigned int shift = inst.operands[i].shift_kind;
11157
11158   constraint (inst.operands[i].immisreg,
11159               _("shift by register not allowed in thumb mode"));
11160   inst.instruction |= inst.operands[i].reg;
11161   if (shift == SHIFT_RRX)
11162     inst.instruction |= SHIFT_ROR << 4;
11163   else
11164     {
11165       constraint (inst.relocs[0].exp.X_op != O_constant,
11166                   _("expression too complex"));
11167
11168       constraint (value > 32
11169                   || (value == 32 && (shift == SHIFT_LSL
11170                                       || shift == SHIFT_ROR)),
11171                   _("shift expression is too large"));
11172
11173       if (value == 0)
11174         shift = SHIFT_LSL;
11175       else if (value == 32)
11176         value = 0;
11177
11178       inst.instruction |= shift << 4;
11179       inst.instruction |= (value & 0x1c) << 10;
11180       inst.instruction |= (value & 0x03) << 6;
11181     }
11182 }
11183
11184
11185 /* inst.operands[i] was set up by parse_address.  Encode it into a
11186    Thumb32 format load or store instruction.  Reject forms that cannot
11187    be used with such instructions.  If is_t is true, reject forms that
11188    cannot be used with a T instruction; if is_d is true, reject forms
11189    that cannot be used with a D instruction.  If it is a store insn,
11190    reject PC in Rn.  */
11191
11192 static void
11193 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
11194 {
11195   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
11196
11197   constraint (!inst.operands[i].isreg,
11198               _("Instruction does not support =N addresses"));
11199
11200   inst.instruction |= inst.operands[i].reg << 16;
11201   if (inst.operands[i].immisreg)
11202     {
11203       constraint (is_pc, BAD_PC_ADDRESSING);
11204       constraint (is_t || is_d, _("cannot use register index with this instruction"));
11205       constraint (inst.operands[i].negative,
11206                   _("Thumb does not support negative register indexing"));
11207       constraint (inst.operands[i].postind,
11208                   _("Thumb does not support register post-indexing"));
11209       constraint (inst.operands[i].writeback,
11210                   _("Thumb does not support register indexing with writeback"));
11211       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
11212                   _("Thumb supports only LSL in shifted register indexing"));
11213
11214       inst.instruction |= inst.operands[i].imm;
11215       if (inst.operands[i].shifted)
11216         {
11217           constraint (inst.relocs[0].exp.X_op != O_constant,
11218                       _("expression too complex"));
11219           constraint (inst.relocs[0].exp.X_add_number < 0
11220                       || inst.relocs[0].exp.X_add_number > 3,
11221                       _("shift out of range"));
11222           inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
11223         }
11224       inst.relocs[0].type = BFD_RELOC_UNUSED;
11225     }
11226   else if (inst.operands[i].preind)
11227     {
11228       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
11229       constraint (is_t && inst.operands[i].writeback,
11230                   _("cannot use writeback with this instruction"));
11231       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
11232                   BAD_PC_ADDRESSING);
11233
11234       if (is_d)
11235         {
11236           inst.instruction |= 0x01000000;
11237           if (inst.operands[i].writeback)
11238             inst.instruction |= 0x00200000;
11239         }
11240       else
11241         {
11242           inst.instruction |= 0x00000c00;
11243           if (inst.operands[i].writeback)
11244             inst.instruction |= 0x00000100;
11245         }
11246       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11247     }
11248   else if (inst.operands[i].postind)
11249     {
11250       gas_assert (inst.operands[i].writeback);
11251       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
11252       constraint (is_t, _("cannot use post-indexing with this instruction"));
11253
11254       if (is_d)
11255         inst.instruction |= 0x00200000;
11256       else
11257         inst.instruction |= 0x00000900;
11258       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11259     }
11260   else /* unindexed - only for coprocessor */
11261     inst.error = _("instruction does not accept unindexed addressing");
11262 }
11263
11264 /* Table of Thumb instructions which exist in 16- and/or 32-bit
11265    encodings (the latter only in post-V6T2 cores).  The index is the
11266    value used in the insns table below.  When there is more than one
11267    possible 16-bit encoding for the instruction, this table always
11268    holds variant (1).
11269    Also contains several pseudo-instructions used during relaxation.  */
11270 #define T16_32_TAB                              \
11271   X(_adc,   4140, eb400000),                    \
11272   X(_adcs,  4140, eb500000),                    \
11273   X(_add,   1c00, eb000000),                    \
11274   X(_adds,  1c00, eb100000),                    \
11275   X(_addi,  0000, f1000000),                    \
11276   X(_addis, 0000, f1100000),                    \
11277   X(_add_pc,000f, f20f0000),                    \
11278   X(_add_sp,000d, f10d0000),                    \
11279   X(_adr,   000f, f20f0000),                    \
11280   X(_and,   4000, ea000000),                    \
11281   X(_ands,  4000, ea100000),                    \
11282   X(_asr,   1000, fa40f000),                    \
11283   X(_asrs,  1000, fa50f000),                    \
11284   X(_b,     e000, f000b000),                    \
11285   X(_bcond, d000, f0008000),                    \
11286   X(_bf,    0000, f040e001),                    \
11287   X(_bfcsel,0000, f000e001),                    \
11288   X(_bfx,   0000, f060e001),                    \
11289   X(_bfl,   0000, f000c001),                    \
11290   X(_bflx,  0000, f070e001),                    \
11291   X(_bic,   4380, ea200000),                    \
11292   X(_bics,  4380, ea300000),                    \
11293   X(_cinc,  0000, ea509000),                    \
11294   X(_cinv,  0000, ea50a000),                    \
11295   X(_cmn,   42c0, eb100f00),                    \
11296   X(_cmp,   2800, ebb00f00),                    \
11297   X(_cneg,  0000, ea50b000),                    \
11298   X(_cpsie, b660, f3af8400),                    \
11299   X(_cpsid, b670, f3af8600),                    \
11300   X(_cpy,   4600, ea4f0000),                    \
11301   X(_csel,  0000, ea508000),                    \
11302   X(_cset,  0000, ea5f900f),                    \
11303   X(_csetm, 0000, ea5fa00f),                    \
11304   X(_csinc, 0000, ea509000),                    \
11305   X(_csinv, 0000, ea50a000),                    \
11306   X(_csneg, 0000, ea50b000),                    \
11307   X(_dec_sp,80dd, f1ad0d00),                    \
11308   X(_dls,   0000, f040e001),                    \
11309   X(_dlstp, 0000, f000e001),                    \
11310   X(_eor,   4040, ea800000),                    \
11311   X(_eors,  4040, ea900000),                    \
11312   X(_inc_sp,00dd, f10d0d00),                    \
11313   X(_lctp,  0000, f00fe001),                    \
11314   X(_ldmia, c800, e8900000),                    \
11315   X(_ldr,   6800, f8500000),                    \
11316   X(_ldrb,  7800, f8100000),                    \
11317   X(_ldrh,  8800, f8300000),                    \
11318   X(_ldrsb, 5600, f9100000),                    \
11319   X(_ldrsh, 5e00, f9300000),                    \
11320   X(_ldr_pc,4800, f85f0000),                    \
11321   X(_ldr_pc2,4800, f85f0000),                   \
11322   X(_ldr_sp,9800, f85d0000),                    \
11323   X(_le,    0000, f00fc001),                    \
11324   X(_letp,  0000, f01fc001),                    \
11325   X(_lsl,   0000, fa00f000),                    \
11326   X(_lsls,  0000, fa10f000),                    \
11327   X(_lsr,   0800, fa20f000),                    \
11328   X(_lsrs,  0800, fa30f000),                    \
11329   X(_mov,   2000, ea4f0000),                    \
11330   X(_movs,  2000, ea5f0000),                    \
11331   X(_mul,   4340, fb00f000),                     \
11332   X(_muls,  4340, ffffffff), /* no 32b muls */  \
11333   X(_mvn,   43c0, ea6f0000),                    \
11334   X(_mvns,  43c0, ea7f0000),                    \
11335   X(_neg,   4240, f1c00000), /* rsb #0 */       \
11336   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
11337   X(_orr,   4300, ea400000),                    \
11338   X(_orrs,  4300, ea500000),                    \
11339   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
11340   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
11341   X(_rev,   ba00, fa90f080),                    \
11342   X(_rev16, ba40, fa90f090),                    \
11343   X(_revsh, bac0, fa90f0b0),                    \
11344   X(_ror,   41c0, fa60f000),                    \
11345   X(_rors,  41c0, fa70f000),                    \
11346   X(_sbc,   4180, eb600000),                    \
11347   X(_sbcs,  4180, eb700000),                    \
11348   X(_stmia, c000, e8800000),                    \
11349   X(_str,   6000, f8400000),                    \
11350   X(_strb,  7000, f8000000),                    \
11351   X(_strh,  8000, f8200000),                    \
11352   X(_str_sp,9000, f84d0000),                    \
11353   X(_sub,   1e00, eba00000),                    \
11354   X(_subs,  1e00, ebb00000),                    \
11355   X(_subi,  8000, f1a00000),                    \
11356   X(_subis, 8000, f1b00000),                    \
11357   X(_sxtb,  b240, fa4ff080),                    \
11358   X(_sxth,  b200, fa0ff080),                    \
11359   X(_tst,   4200, ea100f00),                    \
11360   X(_uxtb,  b2c0, fa5ff080),                    \
11361   X(_uxth,  b280, fa1ff080),                    \
11362   X(_nop,   bf00, f3af8000),                    \
11363   X(_yield, bf10, f3af8001),                    \
11364   X(_wfe,   bf20, f3af8002),                    \
11365   X(_wfi,   bf30, f3af8003),                    \
11366   X(_wls,   0000, f040c001),                    \
11367   X(_wlstp, 0000, f000c001),                    \
11368   X(_sev,   bf40, f3af8004),                    \
11369   X(_sevl,  bf50, f3af8005),                    \
11370   X(_udf,   de00, f7f0a000)
11371
11372 /* To catch errors in encoding functions, the codes are all offset by
11373    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
11374    as 16-bit instructions.  */
11375 #define X(a,b,c) T_MNEM##a
11376 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
11377 #undef X
11378
11379 #define X(a,b,c) 0x##b
11380 static const unsigned short thumb_op16[] = { T16_32_TAB };
11381 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
11382 #undef X
11383
11384 #define X(a,b,c) 0x##c
11385 static const unsigned int thumb_op32[] = { T16_32_TAB };
11386 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
11387 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
11388 #undef X
11389 #undef T16_32_TAB
11390
11391 /* Thumb instruction encoders, in alphabetical order.  */
11392
11393 /* ADDW or SUBW.  */
11394
11395 static void
11396 do_t_add_sub_w (void)
11397 {
11398   int Rd, Rn;
11399
11400   Rd = inst.operands[0].reg;
11401   Rn = inst.operands[1].reg;
11402
11403   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
11404      is the SP-{plus,minus}-immediate form of the instruction.  */
11405   if (Rn == REG_SP)
11406     constraint (Rd == REG_PC, BAD_PC);
11407   else
11408     reject_bad_reg (Rd);
11409
11410   inst.instruction |= (Rn << 16) | (Rd << 8);
11411   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11412 }
11413
11414 /* Parse an add or subtract instruction.  We get here with inst.instruction
11415    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
11416
11417 static void
11418 do_t_add_sub (void)
11419 {
11420   int Rd, Rs, Rn;
11421
11422   Rd = inst.operands[0].reg;
11423   Rs = (inst.operands[1].present
11424         ? inst.operands[1].reg    /* Rd, Rs, foo */
11425         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11426
11427   if (Rd == REG_PC)
11428     set_pred_insn_type_last ();
11429
11430   if (unified_syntax)
11431     {
11432       bfd_boolean flags;
11433       bfd_boolean narrow;
11434       int opcode;
11435
11436       flags = (inst.instruction == T_MNEM_adds
11437                || inst.instruction == T_MNEM_subs);
11438       if (flags)
11439         narrow = !in_pred_block ();
11440       else
11441         narrow = in_pred_block ();
11442       if (!inst.operands[2].isreg)
11443         {
11444           int add;
11445
11446           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11447             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11448
11449           add = (inst.instruction == T_MNEM_add
11450                  || inst.instruction == T_MNEM_adds);
11451           opcode = 0;
11452           if (inst.size_req != 4)
11453             {
11454               /* Attempt to use a narrow opcode, with relaxation if
11455                  appropriate.  */
11456               if (Rd == REG_SP && Rs == REG_SP && !flags)
11457                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
11458               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
11459                 opcode = T_MNEM_add_sp;
11460               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
11461                 opcode = T_MNEM_add_pc;
11462               else if (Rd <= 7 && Rs <= 7 && narrow)
11463                 {
11464                   if (flags)
11465                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
11466                   else
11467                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
11468                 }
11469               if (opcode)
11470                 {
11471                   inst.instruction = THUMB_OP16(opcode);
11472                   inst.instruction |= (Rd << 4) | Rs;
11473                   if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11474                       || (inst.relocs[0].type
11475                           > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC))
11476                   {
11477                     if (inst.size_req == 2)
11478                       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11479                     else
11480                       inst.relax = opcode;
11481                   }
11482                 }
11483               else
11484                 constraint (inst.size_req == 2, BAD_HIREG);
11485             }
11486           if (inst.size_req == 4
11487               || (inst.size_req != 2 && !opcode))
11488             {
11489               constraint ((inst.relocs[0].type
11490                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
11491                           && (inst.relocs[0].type
11492                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
11493                           THUMB1_RELOC_ONLY);
11494               if (Rd == REG_PC)
11495                 {
11496                   constraint (add, BAD_PC);
11497                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
11498                              _("only SUBS PC, LR, #const allowed"));
11499                   constraint (inst.relocs[0].exp.X_op != O_constant,
11500                               _("expression too complex"));
11501                   constraint (inst.relocs[0].exp.X_add_number < 0
11502                               || inst.relocs[0].exp.X_add_number > 0xff,
11503                              _("immediate value out of range"));
11504                   inst.instruction = T2_SUBS_PC_LR
11505                                      | inst.relocs[0].exp.X_add_number;
11506                   inst.relocs[0].type = BFD_RELOC_UNUSED;
11507                   return;
11508                 }
11509               else if (Rs == REG_PC)
11510                 {
11511                   /* Always use addw/subw.  */
11512                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
11513                   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11514                 }
11515               else
11516                 {
11517                   inst.instruction = THUMB_OP32 (inst.instruction);
11518                   inst.instruction = (inst.instruction & 0xe1ffffff)
11519                                      | 0x10000000;
11520                   if (flags)
11521                     inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11522                   else
11523                     inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_IMM;
11524                 }
11525               inst.instruction |= Rd << 8;
11526               inst.instruction |= Rs << 16;
11527             }
11528         }
11529       else
11530         {
11531           unsigned int value = inst.relocs[0].exp.X_add_number;
11532           unsigned int shift = inst.operands[2].shift_kind;
11533
11534           Rn = inst.operands[2].reg;
11535           /* See if we can do this with a 16-bit instruction.  */
11536           if (!inst.operands[2].shifted && inst.size_req != 4)
11537             {
11538               if (Rd > 7 || Rs > 7 || Rn > 7)
11539                 narrow = FALSE;
11540
11541               if (narrow)
11542                 {
11543                   inst.instruction = ((inst.instruction == T_MNEM_adds
11544                                        || inst.instruction == T_MNEM_add)
11545                                       ? T_OPCODE_ADD_R3
11546                                       : T_OPCODE_SUB_R3);
11547                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11548                   return;
11549                 }
11550
11551               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
11552                 {
11553                   /* Thumb-1 cores (except v6-M) require at least one high
11554                      register in a narrow non flag setting add.  */
11555                   if (Rd > 7 || Rn > 7
11556                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
11557                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
11558                     {
11559                       if (Rd == Rn)
11560                         {
11561                           Rn = Rs;
11562                           Rs = Rd;
11563                         }
11564                       inst.instruction = T_OPCODE_ADD_HI;
11565                       inst.instruction |= (Rd & 8) << 4;
11566                       inst.instruction |= (Rd & 7);
11567                       inst.instruction |= Rn << 3;
11568                       return;
11569                     }
11570                 }
11571             }
11572
11573           constraint (Rd == REG_PC, BAD_PC);
11574           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11575             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11576           constraint (Rs == REG_PC, BAD_PC);
11577           reject_bad_reg (Rn);
11578
11579           /* If we get here, it can't be done in 16 bits.  */
11580           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
11581                       _("shift must be constant"));
11582           inst.instruction = THUMB_OP32 (inst.instruction);
11583           inst.instruction |= Rd << 8;
11584           inst.instruction |= Rs << 16;
11585           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
11586                       _("shift value over 3 not allowed in thumb mode"));
11587           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
11588                       _("only LSL shift allowed in thumb mode"));
11589           encode_thumb32_shifted_operand (2);
11590         }
11591     }
11592   else
11593     {
11594       constraint (inst.instruction == T_MNEM_adds
11595                   || inst.instruction == T_MNEM_subs,
11596                   BAD_THUMB32);
11597
11598       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
11599         {
11600           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
11601                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
11602                       BAD_HIREG);
11603
11604           inst.instruction = (inst.instruction == T_MNEM_add
11605                               ? 0x0000 : 0x8000);
11606           inst.instruction |= (Rd << 4) | Rs;
11607           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11608           return;
11609         }
11610
11611       Rn = inst.operands[2].reg;
11612       constraint (inst.operands[2].shifted, _("unshifted register required"));
11613
11614       /* We now have Rd, Rs, and Rn set to registers.  */
11615       if (Rd > 7 || Rs > 7 || Rn > 7)
11616         {
11617           /* Can't do this for SUB.      */
11618           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
11619           inst.instruction = T_OPCODE_ADD_HI;
11620           inst.instruction |= (Rd & 8) << 4;
11621           inst.instruction |= (Rd & 7);
11622           if (Rs == Rd)
11623             inst.instruction |= Rn << 3;
11624           else if (Rn == Rd)
11625             inst.instruction |= Rs << 3;
11626           else
11627             constraint (1, _("dest must overlap one source register"));
11628         }
11629       else
11630         {
11631           inst.instruction = (inst.instruction == T_MNEM_add
11632                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
11633           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11634         }
11635     }
11636 }
11637
11638 static void
11639 do_t_adr (void)
11640 {
11641   unsigned Rd;
11642
11643   Rd = inst.operands[0].reg;
11644   reject_bad_reg (Rd);
11645
11646   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
11647     {
11648       /* Defer to section relaxation.  */
11649       inst.relax = inst.instruction;
11650       inst.instruction = THUMB_OP16 (inst.instruction);
11651       inst.instruction |= Rd << 4;
11652     }
11653   else if (unified_syntax && inst.size_req != 2)
11654     {
11655       /* Generate a 32-bit opcode.  */
11656       inst.instruction = THUMB_OP32 (inst.instruction);
11657       inst.instruction |= Rd << 8;
11658       inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_PC12;
11659       inst.relocs[0].pc_rel = 1;
11660     }
11661   else
11662     {
11663       /* Generate a 16-bit opcode.  */
11664       inst.instruction = THUMB_OP16 (inst.instruction);
11665       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11666       inst.relocs[0].exp.X_add_number -= 4; /* PC relative adjust.  */
11667       inst.relocs[0].pc_rel = 1;
11668       inst.instruction |= Rd << 4;
11669     }
11670
11671   if (inst.relocs[0].exp.X_op == O_symbol
11672       && inst.relocs[0].exp.X_add_symbol != NULL
11673       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11674       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11675     inst.relocs[0].exp.X_add_number += 1;
11676 }
11677
11678 /* Arithmetic instructions for which there is just one 16-bit
11679    instruction encoding, and it allows only two low registers.
11680    For maximal compatibility with ARM syntax, we allow three register
11681    operands even when Thumb-32 instructions are not available, as long
11682    as the first two are identical.  For instance, both "sbc r0,r1" and
11683    "sbc r0,r0,r1" are allowed.  */
11684 static void
11685 do_t_arit3 (void)
11686 {
11687   int Rd, Rs, Rn;
11688
11689   Rd = inst.operands[0].reg;
11690   Rs = (inst.operands[1].present
11691         ? inst.operands[1].reg    /* Rd, Rs, foo */
11692         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11693   Rn = inst.operands[2].reg;
11694
11695   reject_bad_reg (Rd);
11696   reject_bad_reg (Rs);
11697   if (inst.operands[2].isreg)
11698     reject_bad_reg (Rn);
11699
11700   if (unified_syntax)
11701     {
11702       if (!inst.operands[2].isreg)
11703         {
11704           /* For an immediate, we always generate a 32-bit opcode;
11705              section relaxation will shrink it later if possible.  */
11706           inst.instruction = THUMB_OP32 (inst.instruction);
11707           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11708           inst.instruction |= Rd << 8;
11709           inst.instruction |= Rs << 16;
11710           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11711         }
11712       else
11713         {
11714           bfd_boolean narrow;
11715
11716           /* See if we can do this with a 16-bit instruction.  */
11717           if (THUMB_SETS_FLAGS (inst.instruction))
11718             narrow = !in_pred_block ();
11719           else
11720             narrow = in_pred_block ();
11721
11722           if (Rd > 7 || Rn > 7 || Rs > 7)
11723             narrow = FALSE;
11724           if (inst.operands[2].shifted)
11725             narrow = FALSE;
11726           if (inst.size_req == 4)
11727             narrow = FALSE;
11728
11729           if (narrow
11730               && Rd == Rs)
11731             {
11732               inst.instruction = THUMB_OP16 (inst.instruction);
11733               inst.instruction |= Rd;
11734               inst.instruction |= Rn << 3;
11735               return;
11736             }
11737
11738           /* If we get here, it can't be done in 16 bits.  */
11739           constraint (inst.operands[2].shifted
11740                       && inst.operands[2].immisreg,
11741                       _("shift must be constant"));
11742           inst.instruction = THUMB_OP32 (inst.instruction);
11743           inst.instruction |= Rd << 8;
11744           inst.instruction |= Rs << 16;
11745           encode_thumb32_shifted_operand (2);
11746         }
11747     }
11748   else
11749     {
11750       /* On its face this is a lie - the instruction does set the
11751          flags.  However, the only supported mnemonic in this mode
11752          says it doesn't.  */
11753       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11754
11755       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11756                   _("unshifted register required"));
11757       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11758       constraint (Rd != Rs,
11759                   _("dest and source1 must be the same register"));
11760
11761       inst.instruction = THUMB_OP16 (inst.instruction);
11762       inst.instruction |= Rd;
11763       inst.instruction |= Rn << 3;
11764     }
11765 }
11766
11767 /* Similarly, but for instructions where the arithmetic operation is
11768    commutative, so we can allow either of them to be different from
11769    the destination operand in a 16-bit instruction.  For instance, all
11770    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
11771    accepted.  */
11772 static void
11773 do_t_arit3c (void)
11774 {
11775   int Rd, Rs, Rn;
11776
11777   Rd = inst.operands[0].reg;
11778   Rs = (inst.operands[1].present
11779         ? inst.operands[1].reg    /* Rd, Rs, foo */
11780         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11781   Rn = inst.operands[2].reg;
11782
11783   reject_bad_reg (Rd);
11784   reject_bad_reg (Rs);
11785   if (inst.operands[2].isreg)
11786     reject_bad_reg (Rn);
11787
11788   if (unified_syntax)
11789     {
11790       if (!inst.operands[2].isreg)
11791         {
11792           /* For an immediate, we always generate a 32-bit opcode;
11793              section relaxation will shrink it later if possible.  */
11794           inst.instruction = THUMB_OP32 (inst.instruction);
11795           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11796           inst.instruction |= Rd << 8;
11797           inst.instruction |= Rs << 16;
11798           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11799         }
11800       else
11801         {
11802           bfd_boolean narrow;
11803
11804           /* See if we can do this with a 16-bit instruction.  */
11805           if (THUMB_SETS_FLAGS (inst.instruction))
11806             narrow = !in_pred_block ();
11807           else
11808             narrow = in_pred_block ();
11809
11810           if (Rd > 7 || Rn > 7 || Rs > 7)
11811             narrow = FALSE;
11812           if (inst.operands[2].shifted)
11813             narrow = FALSE;
11814           if (inst.size_req == 4)
11815             narrow = FALSE;
11816
11817           if (narrow)
11818             {
11819               if (Rd == Rs)
11820                 {
11821                   inst.instruction = THUMB_OP16 (inst.instruction);
11822                   inst.instruction |= Rd;
11823                   inst.instruction |= Rn << 3;
11824                   return;
11825                 }
11826               if (Rd == Rn)
11827                 {
11828                   inst.instruction = THUMB_OP16 (inst.instruction);
11829                   inst.instruction |= Rd;
11830                   inst.instruction |= Rs << 3;
11831                   return;
11832                 }
11833             }
11834
11835           /* If we get here, it can't be done in 16 bits.  */
11836           constraint (inst.operands[2].shifted
11837                       && inst.operands[2].immisreg,
11838                       _("shift must be constant"));
11839           inst.instruction = THUMB_OP32 (inst.instruction);
11840           inst.instruction |= Rd << 8;
11841           inst.instruction |= Rs << 16;
11842           encode_thumb32_shifted_operand (2);
11843         }
11844     }
11845   else
11846     {
11847       /* On its face this is a lie - the instruction does set the
11848          flags.  However, the only supported mnemonic in this mode
11849          says it doesn't.  */
11850       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11851
11852       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11853                   _("unshifted register required"));
11854       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11855
11856       inst.instruction = THUMB_OP16 (inst.instruction);
11857       inst.instruction |= Rd;
11858
11859       if (Rd == Rs)
11860         inst.instruction |= Rn << 3;
11861       else if (Rd == Rn)
11862         inst.instruction |= Rs << 3;
11863       else
11864         constraint (1, _("dest must overlap one source register"));
11865     }
11866 }
11867
11868 static void
11869 do_t_bfc (void)
11870 {
11871   unsigned Rd;
11872   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11873   constraint (msb > 32, _("bit-field extends past end of register"));
11874   /* The instruction encoding stores the LSB and MSB,
11875      not the LSB and width.  */
11876   Rd = inst.operands[0].reg;
11877   reject_bad_reg (Rd);
11878   inst.instruction |= Rd << 8;
11879   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11880   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11881   inst.instruction |= msb - 1;
11882 }
11883
11884 static void
11885 do_t_bfi (void)
11886 {
11887   int Rd, Rn;
11888   unsigned int msb;
11889
11890   Rd = inst.operands[0].reg;
11891   reject_bad_reg (Rd);
11892
11893   /* #0 in second position is alternative syntax for bfc, which is
11894      the same instruction but with REG_PC in the Rm field.  */
11895   if (!inst.operands[1].isreg)
11896     Rn = REG_PC;
11897   else
11898     {
11899       Rn = inst.operands[1].reg;
11900       reject_bad_reg (Rn);
11901     }
11902
11903   msb = inst.operands[2].imm + inst.operands[3].imm;
11904   constraint (msb > 32, _("bit-field extends past end of register"));
11905   /* The instruction encoding stores the LSB and MSB,
11906      not the LSB and width.  */
11907   inst.instruction |= Rd << 8;
11908   inst.instruction |= Rn << 16;
11909   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11910   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11911   inst.instruction |= msb - 1;
11912 }
11913
11914 static void
11915 do_t_bfx (void)
11916 {
11917   unsigned Rd, Rn;
11918
11919   Rd = inst.operands[0].reg;
11920   Rn = inst.operands[1].reg;
11921
11922   reject_bad_reg (Rd);
11923   reject_bad_reg (Rn);
11924
11925   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11926               _("bit-field extends past end of register"));
11927   inst.instruction |= Rd << 8;
11928   inst.instruction |= Rn << 16;
11929   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11930   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11931   inst.instruction |= inst.operands[3].imm - 1;
11932 }
11933
11934 /* ARM V5 Thumb BLX (argument parse)
11935         BLX <target_addr>       which is BLX(1)
11936         BLX <Rm>                which is BLX(2)
11937    Unfortunately, there are two different opcodes for this mnemonic.
11938    So, the insns[].value is not used, and the code here zaps values
11939         into inst.instruction.
11940
11941    ??? How to take advantage of the additional two bits of displacement
11942    available in Thumb32 mode?  Need new relocation?  */
11943
11944 static void
11945 do_t_blx (void)
11946 {
11947   set_pred_insn_type_last ();
11948
11949   if (inst.operands[0].isreg)
11950     {
11951       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11952       /* We have a register, so this is BLX(2).  */
11953       inst.instruction |= inst.operands[0].reg << 3;
11954     }
11955   else
11956     {
11957       /* No register.  This must be BLX(1).  */
11958       inst.instruction = 0xf000e800;
11959       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11960     }
11961 }
11962
11963 static void
11964 do_t_branch (void)
11965 {
11966   int opcode;
11967   int cond;
11968   bfd_reloc_code_real_type reloc;
11969
11970   cond = inst.cond;
11971   set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);
11972
11973   if (in_pred_block ())
11974     {
11975       /* Conditional branches inside IT blocks are encoded as unconditional
11976          branches.  */
11977       cond = COND_ALWAYS;
11978     }
11979   else
11980     cond = inst.cond;
11981
11982   if (cond != COND_ALWAYS)
11983     opcode = T_MNEM_bcond;
11984   else
11985     opcode = inst.instruction;
11986
11987   if (unified_syntax
11988       && (inst.size_req == 4
11989           || (inst.size_req != 2
11990               && (inst.operands[0].hasreloc
11991                   || inst.relocs[0].exp.X_op == O_constant))))
11992     {
11993       inst.instruction = THUMB_OP32(opcode);
11994       if (cond == COND_ALWAYS)
11995         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11996       else
11997         {
11998           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11999                       _("selected architecture does not support "
12000                         "wide conditional branch instruction"));
12001
12002           gas_assert (cond != 0xF);
12003           inst.instruction |= cond << 22;
12004           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
12005         }
12006     }
12007   else
12008     {
12009       inst.instruction = THUMB_OP16(opcode);
12010       if (cond == COND_ALWAYS)
12011         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
12012       else
12013         {
12014           inst.instruction |= cond << 8;
12015           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
12016         }
12017       /* Allow section relaxation.  */
12018       if (unified_syntax && inst.size_req != 2)
12019         inst.relax = opcode;
12020     }
12021   inst.relocs[0].type = reloc;
12022   inst.relocs[0].pc_rel = 1;
12023 }
12024
12025 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
12026    between the two is the maximum immediate allowed - which is passed in
12027    RANGE.  */
12028 static void
12029 do_t_bkpt_hlt1 (int range)
12030 {
12031   constraint (inst.cond != COND_ALWAYS,
12032               _("instruction is always unconditional"));
12033   if (inst.operands[0].present)
12034     {
12035       constraint (inst.operands[0].imm > range,
12036                   _("immediate value out of range"));
12037       inst.instruction |= inst.operands[0].imm;
12038     }
12039
12040   set_pred_insn_type (NEUTRAL_IT_INSN);
12041 }
12042
12043 static void
12044 do_t_hlt (void)
12045 {
12046   do_t_bkpt_hlt1 (63);
12047 }
12048
12049 static void
12050 do_t_bkpt (void)
12051 {
12052   do_t_bkpt_hlt1 (255);
12053 }
12054
12055 static void
12056 do_t_branch23 (void)
12057 {
12058   set_pred_insn_type_last ();
12059   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
12060
12061   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
12062      this file.  We used to simply ignore the PLT reloc type here --
12063      the branch encoding is now needed to deal with TLSCALL relocs.
12064      So if we see a PLT reloc now, put it back to how it used to be to
12065      keep the preexisting behaviour.  */
12066   if (inst.relocs[0].type == BFD_RELOC_ARM_PLT32)
12067     inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH23;
12068
12069 #if defined(OBJ_COFF)
12070   /* If the destination of the branch is a defined symbol which does not have
12071      the THUMB_FUNC attribute, then we must be calling a function which has
12072      the (interfacearm) attribute.  We look for the Thumb entry point to that
12073      function and change the branch to refer to that function instead.  */
12074   if (   inst.relocs[0].exp.X_op == O_symbol
12075       && inst.relocs[0].exp.X_add_symbol != NULL
12076       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
12077       && ! THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
12078     inst.relocs[0].exp.X_add_symbol
12079       = find_real_start (inst.relocs[0].exp.X_add_symbol);
12080 #endif
12081 }
12082
12083 static void
12084 do_t_bx (void)
12085 {
12086   set_pred_insn_type_last ();
12087   inst.instruction |= inst.operands[0].reg << 3;
12088   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
12089      should cause the alignment to be checked once it is known.  This is
12090      because BX PC only works if the instruction is word aligned.  */
12091 }
12092
12093 static void
12094 do_t_bxj (void)
12095 {
12096   int Rm;
12097
12098   set_pred_insn_type_last ();
12099   Rm = inst.operands[0].reg;
12100   reject_bad_reg (Rm);
12101   inst.instruction |= Rm << 16;
12102 }
12103
12104 static void
12105 do_t_clz (void)
12106 {
12107   unsigned Rd;
12108   unsigned Rm;
12109
12110   Rd = inst.operands[0].reg;
12111   Rm = inst.operands[1].reg;
12112
12113   reject_bad_reg (Rd);
12114   reject_bad_reg (Rm);
12115
12116   inst.instruction |= Rd << 8;
12117   inst.instruction |= Rm << 16;
12118   inst.instruction |= Rm;
12119 }
12120
12121 /* For the Armv8.1-M conditional instructions.  */
12122 static void
12123 do_t_cond (void)
12124 {
12125   unsigned Rd, Rn, Rm;
12126   signed int cond;
12127
12128   constraint (inst.cond != COND_ALWAYS, BAD_COND);
12129
12130   Rd = inst.operands[0].reg;
12131   switch (inst.instruction)
12132     {
12133       case T_MNEM_csinc:
12134       case T_MNEM_csinv:
12135       case T_MNEM_csneg:
12136       case T_MNEM_csel:
12137         Rn = inst.operands[1].reg;
12138         Rm = inst.operands[2].reg;
12139         cond = inst.operands[3].imm;
12140         constraint (Rn == REG_SP, BAD_SP);
12141         constraint (Rm == REG_SP, BAD_SP);
12142         break;
12143
12144       case T_MNEM_cinc:
12145       case T_MNEM_cinv:
12146       case T_MNEM_cneg:
12147         Rn = inst.operands[1].reg;
12148         cond = inst.operands[2].imm;
12149         /* Invert the last bit to invert the cond.  */
12150         cond = TOGGLE_BIT (cond, 0);
12151         constraint (Rn == REG_SP, BAD_SP);
12152         Rm = Rn;
12153         break;
12154
12155       case T_MNEM_csetm:
12156       case T_MNEM_cset:
12157         cond = inst.operands[1].imm;
12158         /* Invert the last bit to invert the cond.  */
12159         cond = TOGGLE_BIT (cond, 0);
12160         Rn = REG_PC;
12161         Rm = REG_PC;
12162         break;
12163
12164       default: abort ();
12165     }
12166
12167   set_pred_insn_type (OUTSIDE_PRED_INSN);
12168   inst.instruction = THUMB_OP32 (inst.instruction);
12169   inst.instruction |= Rd << 8;
12170   inst.instruction |= Rn << 16;
12171   inst.instruction |= Rm;
12172   inst.instruction |= cond << 4;
12173 }
12174
12175 static void
12176 do_t_csdb (void)
12177 {
12178   set_pred_insn_type (OUTSIDE_PRED_INSN);
12179 }
12180
12181 static void
12182 do_t_cps (void)
12183 {
12184   set_pred_insn_type (OUTSIDE_PRED_INSN);
12185   inst.instruction |= inst.operands[0].imm;
12186 }
12187
12188 static void
12189 do_t_cpsi (void)
12190 {
12191   set_pred_insn_type (OUTSIDE_PRED_INSN);
12192   if (unified_syntax
12193       && (inst.operands[1].present || inst.size_req == 4)
12194       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
12195     {
12196       unsigned int imod = (inst.instruction & 0x0030) >> 4;
12197       inst.instruction = 0xf3af8000;
12198       inst.instruction |= imod << 9;
12199       inst.instruction |= inst.operands[0].imm << 5;
12200       if (inst.operands[1].present)
12201         inst.instruction |= 0x100 | inst.operands[1].imm;
12202     }
12203   else
12204     {
12205       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
12206                   && (inst.operands[0].imm & 4),
12207                   _("selected processor does not support 'A' form "
12208                     "of this instruction"));
12209       constraint (inst.operands[1].present || inst.size_req == 4,
12210                   _("Thumb does not support the 2-argument "
12211                     "form of this instruction"));
12212       inst.instruction |= inst.operands[0].imm;
12213     }
12214 }
12215
12216 /* THUMB CPY instruction (argument parse).  */
12217
12218 static void
12219 do_t_cpy (void)
12220 {
12221   if (inst.size_req == 4)
12222     {
12223       inst.instruction = THUMB_OP32 (T_MNEM_mov);
12224       inst.instruction |= inst.operands[0].reg << 8;
12225       inst.instruction |= inst.operands[1].reg;
12226     }
12227   else
12228     {
12229       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
12230       inst.instruction |= (inst.operands[0].reg & 0x7);
12231       inst.instruction |= inst.operands[1].reg << 3;
12232     }
12233 }
12234
12235 static void
12236 do_t_cbz (void)
12237 {
12238   set_pred_insn_type (OUTSIDE_PRED_INSN);
12239   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12240   inst.instruction |= inst.operands[0].reg;
12241   inst.relocs[0].pc_rel = 1;
12242   inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH7;
12243 }
12244
12245 static void
12246 do_t_dbg (void)
12247 {
12248   inst.instruction |= inst.operands[0].imm;
12249 }
12250
12251 static void
12252 do_t_div (void)
12253 {
12254   unsigned Rd, Rn, Rm;
12255
12256   Rd = inst.operands[0].reg;
12257   Rn = (inst.operands[1].present
12258         ? inst.operands[1].reg : Rd);
12259   Rm = inst.operands[2].reg;
12260
12261   reject_bad_reg (Rd);
12262   reject_bad_reg (Rn);
12263   reject_bad_reg (Rm);
12264
12265   inst.instruction |= Rd << 8;
12266   inst.instruction |= Rn << 16;
12267   inst.instruction |= Rm;
12268 }
12269
12270 static void
12271 do_t_hint (void)
12272 {
12273   if (unified_syntax && inst.size_req == 4)
12274     inst.instruction = THUMB_OP32 (inst.instruction);
12275   else
12276     inst.instruction = THUMB_OP16 (inst.instruction);
12277 }
12278
12279 static void
12280 do_t_it (void)
12281 {
12282   unsigned int cond = inst.operands[0].imm;
12283
12284   set_pred_insn_type (IT_INSN);
12285   now_pred.mask = (inst.instruction & 0xf) | 0x10;
12286   now_pred.cc = cond;
12287   now_pred.warn_deprecated = FALSE;
12288   now_pred.type = SCALAR_PRED;
12289
12290   /* If the condition is a negative condition, invert the mask.  */
12291   if ((cond & 0x1) == 0x0)
12292     {
12293       unsigned int mask = inst.instruction & 0x000f;
12294
12295       if ((mask & 0x7) == 0)
12296         {
12297           /* No conversion needed.  */
12298           now_pred.block_length = 1;
12299         }
12300       else if ((mask & 0x3) == 0)
12301         {
12302           mask ^= 0x8;
12303           now_pred.block_length = 2;
12304         }
12305       else if ((mask & 0x1) == 0)
12306         {
12307           mask ^= 0xC;
12308           now_pred.block_length = 3;
12309         }
12310       else
12311         {
12312           mask ^= 0xE;
12313           now_pred.block_length = 4;
12314         }
12315
12316       inst.instruction &= 0xfff0;
12317       inst.instruction |= mask;
12318     }
12319
12320   inst.instruction |= cond << 4;
12321 }
12322
12323 /* Helper function used for both push/pop and ldm/stm.  */
12324 static void
12325 encode_thumb2_multi (bfd_boolean do_io, int base, unsigned mask,
12326                      bfd_boolean writeback)
12327 {
12328   bfd_boolean load, store;
12329
12330   gas_assert (base != -1 || !do_io);
12331   load = do_io && ((inst.instruction & (1 << 20)) != 0);
12332   store = do_io && !load;
12333
12334   if (mask & (1 << 13))
12335     inst.error =  _("SP not allowed in register list");
12336
12337   if (do_io && (mask & (1 << base)) != 0
12338       && writeback)
12339     inst.error = _("having the base register in the register list when "
12340                    "using write back is UNPREDICTABLE");
12341
12342   if (load)
12343     {
12344       if (mask & (1 << 15))
12345         {
12346           if (mask & (1 << 14))
12347             inst.error = _("LR and PC should not both be in register list");
12348           else
12349             set_pred_insn_type_last ();
12350         }
12351     }
12352   else if (store)
12353     {
12354       if (mask & (1 << 15))
12355         inst.error = _("PC not allowed in register list");
12356     }
12357
12358   if (do_io && ((mask & (mask - 1)) == 0))
12359     {
12360       /* Single register transfers implemented as str/ldr.  */
12361       if (writeback)
12362         {
12363           if (inst.instruction & (1 << 23))
12364             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
12365           else
12366             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
12367         }
12368       else
12369         {
12370           if (inst.instruction & (1 << 23))
12371             inst.instruction = 0x00800000; /* ia -> [base] */
12372           else
12373             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
12374         }
12375
12376       inst.instruction |= 0xf8400000;
12377       if (load)
12378         inst.instruction |= 0x00100000;
12379
12380       mask = ffs (mask) - 1;
12381       mask <<= 12;
12382     }
12383   else if (writeback)
12384     inst.instruction |= WRITE_BACK;
12385
12386   inst.instruction |= mask;
12387   if (do_io)
12388     inst.instruction |= base << 16;
12389 }
12390
12391 static void
12392 do_t_ldmstm (void)
12393 {
12394   /* This really doesn't seem worth it.  */
12395   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
12396               _("expression too complex"));
12397   constraint (inst.operands[1].writeback,
12398               _("Thumb load/store multiple does not support {reglist}^"));
12399
12400   if (unified_syntax)
12401     {
12402       bfd_boolean narrow;
12403       unsigned mask;
12404
12405       narrow = FALSE;
12406       /* See if we can use a 16-bit instruction.  */
12407       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
12408           && inst.size_req != 4
12409           && !(inst.operands[1].imm & ~0xff))
12410         {
12411           mask = 1 << inst.operands[0].reg;
12412
12413           if (inst.operands[0].reg <= 7)
12414             {
12415               if (inst.instruction == T_MNEM_stmia
12416                   ? inst.operands[0].writeback
12417                   : (inst.operands[0].writeback
12418                      == !(inst.operands[1].imm & mask)))
12419                 {
12420                   if (inst.instruction == T_MNEM_stmia
12421                       && (inst.operands[1].imm & mask)
12422                       && (inst.operands[1].imm & (mask - 1)))
12423                     as_warn (_("value stored for r%d is UNKNOWN"),
12424                              inst.operands[0].reg);
12425
12426                   inst.instruction = THUMB_OP16 (inst.instruction);
12427                   inst.instruction |= inst.operands[0].reg << 8;
12428                   inst.instruction |= inst.operands[1].imm;
12429                   narrow = TRUE;
12430                 }
12431               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12432                 {
12433                   /* This means 1 register in reg list one of 3 situations:
12434                      1. Instruction is stmia, but without writeback.
12435                      2. lmdia without writeback, but with Rn not in
12436                         reglist.
12437                      3. ldmia with writeback, but with Rn in reglist.
12438                      Case 3 is UNPREDICTABLE behaviour, so we handle
12439                      case 1 and 2 which can be converted into a 16-bit
12440                      str or ldr. The SP cases are handled below.  */
12441                   unsigned long opcode;
12442                   /* First, record an error for Case 3.  */
12443                   if (inst.operands[1].imm & mask
12444                       && inst.operands[0].writeback)
12445                     inst.error =
12446                         _("having the base register in the register list when "
12447                           "using write back is UNPREDICTABLE");
12448
12449                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
12450                                                              : T_MNEM_ldr);
12451                   inst.instruction = THUMB_OP16 (opcode);
12452                   inst.instruction |= inst.operands[0].reg << 3;
12453                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
12454                   narrow = TRUE;
12455                 }
12456             }
12457           else if (inst.operands[0] .reg == REG_SP)
12458             {
12459               if (inst.operands[0].writeback)
12460                 {
12461                   inst.instruction =
12462                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12463                                     ? T_MNEM_push : T_MNEM_pop);
12464                   inst.instruction |= inst.operands[1].imm;
12465                   narrow = TRUE;
12466                 }
12467               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12468                 {
12469                   inst.instruction =
12470                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12471                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
12472                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
12473                   narrow = TRUE;
12474                 }
12475             }
12476         }
12477
12478       if (!narrow)
12479         {
12480           if (inst.instruction < 0xffff)
12481             inst.instruction = THUMB_OP32 (inst.instruction);
12482
12483           encode_thumb2_multi (TRUE /* do_io */, inst.operands[0].reg,
12484                                inst.operands[1].imm,
12485                                inst.operands[0].writeback);
12486         }
12487     }
12488   else
12489     {
12490       constraint (inst.operands[0].reg > 7
12491                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
12492       constraint (inst.instruction != T_MNEM_ldmia
12493                   && inst.instruction != T_MNEM_stmia,
12494                   _("Thumb-2 instruction only valid in unified syntax"));
12495       if (inst.instruction == T_MNEM_stmia)
12496         {
12497           if (!inst.operands[0].writeback)
12498             as_warn (_("this instruction will write back the base register"));
12499           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
12500               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
12501             as_warn (_("value stored for r%d is UNKNOWN"),
12502                      inst.operands[0].reg);
12503         }
12504       else
12505         {
12506           if (!inst.operands[0].writeback
12507               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
12508             as_warn (_("this instruction will write back the base register"));
12509           else if (inst.operands[0].writeback
12510                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
12511             as_warn (_("this instruction will not write back the base register"));
12512         }
12513
12514       inst.instruction = THUMB_OP16 (inst.instruction);
12515       inst.instruction |= inst.operands[0].reg << 8;
12516       inst.instruction |= inst.operands[1].imm;
12517     }
12518 }
12519
12520 static void
12521 do_t_ldrex (void)
12522 {
12523   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
12524               || inst.operands[1].postind || inst.operands[1].writeback
12525               || inst.operands[1].immisreg || inst.operands[1].shifted
12526               || inst.operands[1].negative,
12527               BAD_ADDR_MODE);
12528
12529   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
12530
12531   inst.instruction |= inst.operands[0].reg << 12;
12532   inst.instruction |= inst.operands[1].reg << 16;
12533   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
12534 }
12535
12536 static void
12537 do_t_ldrexd (void)
12538 {
12539   if (!inst.operands[1].present)
12540     {
12541       constraint (inst.operands[0].reg == REG_LR,
12542                   _("r14 not allowed as first register "
12543                     "when second register is omitted"));
12544       inst.operands[1].reg = inst.operands[0].reg + 1;
12545     }
12546   constraint (inst.operands[0].reg == inst.operands[1].reg,
12547               BAD_OVERLAP);
12548
12549   inst.instruction |= inst.operands[0].reg << 12;
12550   inst.instruction |= inst.operands[1].reg << 8;
12551   inst.instruction |= inst.operands[2].reg << 16;
12552 }
12553
12554 static void
12555 do_t_ldst (void)
12556 {
12557   unsigned long opcode;
12558   int Rn;
12559
12560   if (inst.operands[0].isreg
12561       && !inst.operands[0].preind
12562       && inst.operands[0].reg == REG_PC)
12563     set_pred_insn_type_last ();
12564
12565   opcode = inst.instruction;
12566   if (unified_syntax)
12567     {
12568       if (!inst.operands[1].isreg)
12569         {
12570           if (opcode <= 0xffff)
12571             inst.instruction = THUMB_OP32 (opcode);
12572           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12573             return;
12574         }
12575       if (inst.operands[1].isreg
12576           && !inst.operands[1].writeback
12577           && !inst.operands[1].shifted && !inst.operands[1].postind
12578           && !inst.operands[1].negative && inst.operands[0].reg <= 7
12579           && opcode <= 0xffff
12580           && inst.size_req != 4)
12581         {
12582           /* Insn may have a 16-bit form.  */
12583           Rn = inst.operands[1].reg;
12584           if (inst.operands[1].immisreg)
12585             {
12586               inst.instruction = THUMB_OP16 (opcode);
12587               /* [Rn, Rik] */
12588               if (Rn <= 7 && inst.operands[1].imm <= 7)
12589                 goto op16;
12590               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
12591                 reject_bad_reg (inst.operands[1].imm);
12592             }
12593           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
12594                     && opcode != T_MNEM_ldrsb)
12595                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
12596                    || (Rn == REG_SP && opcode == T_MNEM_str))
12597             {
12598               /* [Rn, #const] */
12599               if (Rn > 7)
12600                 {
12601                   if (Rn == REG_PC)
12602                     {
12603                       if (inst.relocs[0].pc_rel)
12604                         opcode = T_MNEM_ldr_pc2;
12605                       else
12606                         opcode = T_MNEM_ldr_pc;
12607                     }
12608                   else
12609                     {
12610                       if (opcode == T_MNEM_ldr)
12611                         opcode = T_MNEM_ldr_sp;
12612                       else
12613                         opcode = T_MNEM_str_sp;
12614                     }
12615                   inst.instruction = inst.operands[0].reg << 8;
12616                 }
12617               else
12618                 {
12619                   inst.instruction = inst.operands[0].reg;
12620                   inst.instruction |= inst.operands[1].reg << 3;
12621                 }
12622               inst.instruction |= THUMB_OP16 (opcode);
12623               if (inst.size_req == 2)
12624                 inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12625               else
12626                 inst.relax = opcode;
12627               return;
12628             }
12629         }
12630       /* Definitely a 32-bit variant.  */
12631
12632       /* Warning for Erratum 752419.  */
12633       if (opcode == T_MNEM_ldr
12634           && inst.operands[0].reg == REG_SP
12635           && inst.operands[1].writeback == 1
12636           && !inst.operands[1].immisreg)
12637         {
12638           if (no_cpu_selected ()
12639               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
12640                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
12641                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
12642             as_warn (_("This instruction may be unpredictable "
12643                        "if executed on M-profile cores "
12644                        "with interrupts enabled."));
12645         }
12646
12647       /* Do some validations regarding addressing modes.  */
12648       if (inst.operands[1].immisreg)
12649         reject_bad_reg (inst.operands[1].imm);
12650
12651       constraint (inst.operands[1].writeback == 1
12652                   && inst.operands[0].reg == inst.operands[1].reg,
12653                   BAD_OVERLAP);
12654
12655       inst.instruction = THUMB_OP32 (opcode);
12656       inst.instruction |= inst.operands[0].reg << 12;
12657       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
12658       check_ldr_r15_aligned ();
12659       return;
12660     }
12661
12662   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12663
12664   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
12665     {
12666       /* Only [Rn,Rm] is acceptable.  */
12667       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
12668       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
12669                   || inst.operands[1].postind || inst.operands[1].shifted
12670                   || inst.operands[1].negative,
12671                   _("Thumb does not support this addressing mode"));
12672       inst.instruction = THUMB_OP16 (inst.instruction);
12673       goto op16;
12674     }
12675
12676   inst.instruction = THUMB_OP16 (inst.instruction);
12677   if (!inst.operands[1].isreg)
12678     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12679       return;
12680
12681   constraint (!inst.operands[1].preind
12682               || inst.operands[1].shifted
12683               || inst.operands[1].writeback,
12684               _("Thumb does not support this addressing mode"));
12685   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
12686     {
12687       constraint (inst.instruction & 0x0600,
12688                   _("byte or halfword not valid for base register"));
12689       constraint (inst.operands[1].reg == REG_PC
12690                   && !(inst.instruction & THUMB_LOAD_BIT),
12691                   _("r15 based store not allowed"));
12692       constraint (inst.operands[1].immisreg,
12693                   _("invalid base register for register offset"));
12694
12695       if (inst.operands[1].reg == REG_PC)
12696         inst.instruction = T_OPCODE_LDR_PC;
12697       else if (inst.instruction & THUMB_LOAD_BIT)
12698         inst.instruction = T_OPCODE_LDR_SP;
12699       else
12700         inst.instruction = T_OPCODE_STR_SP;
12701
12702       inst.instruction |= inst.operands[0].reg << 8;
12703       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12704       return;
12705     }
12706
12707   constraint (inst.operands[1].reg > 7, BAD_HIREG);
12708   if (!inst.operands[1].immisreg)
12709     {
12710       /* Immediate offset.  */
12711       inst.instruction |= inst.operands[0].reg;
12712       inst.instruction |= inst.operands[1].reg << 3;
12713       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12714       return;
12715     }
12716
12717   /* Register offset.  */
12718   constraint (inst.operands[1].imm > 7, BAD_HIREG);
12719   constraint (inst.operands[1].negative,
12720               _("Thumb does not support this addressing mode"));
12721
12722  op16:
12723   switch (inst.instruction)
12724     {
12725     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
12726     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
12727     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
12728     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
12729     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
12730     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
12731     case 0x5600 /* ldrsb */:
12732     case 0x5e00 /* ldrsh */: break;
12733     default: abort ();
12734     }
12735
12736   inst.instruction |= inst.operands[0].reg;
12737   inst.instruction |= inst.operands[1].reg << 3;
12738   inst.instruction |= inst.operands[1].imm << 6;
12739 }
12740
12741 static void
12742 do_t_ldstd (void)
12743 {
12744   if (!inst.operands[1].present)
12745     {
12746       inst.operands[1].reg = inst.operands[0].reg + 1;
12747       constraint (inst.operands[0].reg == REG_LR,
12748                   _("r14 not allowed here"));
12749       constraint (inst.operands[0].reg == REG_R12,
12750                   _("r12 not allowed here"));
12751     }
12752
12753   if (inst.operands[2].writeback
12754       && (inst.operands[0].reg == inst.operands[2].reg
12755       || inst.operands[1].reg == inst.operands[2].reg))
12756     as_warn (_("base register written back, and overlaps "
12757                "one of transfer registers"));
12758
12759   inst.instruction |= inst.operands[0].reg << 12;
12760   inst.instruction |= inst.operands[1].reg << 8;
12761   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
12762 }
12763
12764 static void
12765 do_t_ldstt (void)
12766 {
12767   inst.instruction |= inst.operands[0].reg << 12;
12768   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
12769 }
12770
12771 static void
12772 do_t_mla (void)
12773 {
12774   unsigned Rd, Rn, Rm, Ra;
12775
12776   Rd = inst.operands[0].reg;
12777   Rn = inst.operands[1].reg;
12778   Rm = inst.operands[2].reg;
12779   Ra = inst.operands[3].reg;
12780
12781   reject_bad_reg (Rd);
12782   reject_bad_reg (Rn);
12783   reject_bad_reg (Rm);
12784   reject_bad_reg (Ra);
12785
12786   inst.instruction |= Rd << 8;
12787   inst.instruction |= Rn << 16;
12788   inst.instruction |= Rm;
12789   inst.instruction |= Ra << 12;
12790 }
12791
12792 static void
12793 do_t_mlal (void)
12794 {
12795   unsigned RdLo, RdHi, Rn, Rm;
12796
12797   RdLo = inst.operands[0].reg;
12798   RdHi = inst.operands[1].reg;
12799   Rn = inst.operands[2].reg;
12800   Rm = inst.operands[3].reg;
12801
12802   reject_bad_reg (RdLo);
12803   reject_bad_reg (RdHi);
12804   reject_bad_reg (Rn);
12805   reject_bad_reg (Rm);
12806
12807   inst.instruction |= RdLo << 12;
12808   inst.instruction |= RdHi << 8;
12809   inst.instruction |= Rn << 16;
12810   inst.instruction |= Rm;
12811 }
12812
12813 static void
12814 do_t_mov_cmp (void)
12815 {
12816   unsigned Rn, Rm;
12817
12818   Rn = inst.operands[0].reg;
12819   Rm = inst.operands[1].reg;
12820
12821   if (Rn == REG_PC)
12822     set_pred_insn_type_last ();
12823
12824   if (unified_syntax)
12825     {
12826       int r0off = (inst.instruction == T_MNEM_mov
12827                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
12828       unsigned long opcode;
12829       bfd_boolean narrow;
12830       bfd_boolean low_regs;
12831
12832       low_regs = (Rn <= 7 && Rm <= 7);
12833       opcode = inst.instruction;
12834       if (in_pred_block ())
12835         narrow = opcode != T_MNEM_movs;
12836       else
12837         narrow = opcode != T_MNEM_movs || low_regs;
12838       if (inst.size_req == 4
12839           || inst.operands[1].shifted)
12840         narrow = FALSE;
12841
12842       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
12843       if (opcode == T_MNEM_movs && inst.operands[1].isreg
12844           && !inst.operands[1].shifted
12845           && Rn == REG_PC
12846           && Rm == REG_LR)
12847         {
12848           inst.instruction = T2_SUBS_PC_LR;
12849           return;
12850         }
12851
12852       if (opcode == T_MNEM_cmp)
12853         {
12854           constraint (Rn == REG_PC, BAD_PC);
12855           if (narrow)
12856             {
12857               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
12858                  but valid.  */
12859               warn_deprecated_sp (Rm);
12860               /* R15 was documented as a valid choice for Rm in ARMv6,
12861                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
12862                  tools reject R15, so we do too.  */
12863               constraint (Rm == REG_PC, BAD_PC);
12864             }
12865           else
12866             reject_bad_reg (Rm);
12867         }
12868       else if (opcode == T_MNEM_mov
12869                || opcode == T_MNEM_movs)
12870         {
12871           if (inst.operands[1].isreg)
12872             {
12873               if (opcode == T_MNEM_movs)
12874                 {
12875                   reject_bad_reg (Rn);
12876                   reject_bad_reg (Rm);
12877                 }
12878               else if (narrow)
12879                 {
12880                   /* This is mov.n.  */
12881                   if ((Rn == REG_SP || Rn == REG_PC)
12882                       && (Rm == REG_SP || Rm == REG_PC))
12883                     {
12884                       as_tsktsk (_("Use of r%u as a source register is "
12885                                  "deprecated when r%u is the destination "
12886                                  "register."), Rm, Rn);
12887                     }
12888                 }
12889               else
12890                 {
12891                   /* This is mov.w.  */
12892                   constraint (Rn == REG_PC, BAD_PC);
12893                   constraint (Rm == REG_PC, BAD_PC);
12894                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12895                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
12896                 }
12897             }
12898           else
12899             reject_bad_reg (Rn);
12900         }
12901
12902       if (!inst.operands[1].isreg)
12903         {
12904           /* Immediate operand.  */
12905           if (!in_pred_block () && opcode == T_MNEM_mov)
12906             narrow = 0;
12907           if (low_regs && narrow)
12908             {
12909               inst.instruction = THUMB_OP16 (opcode);
12910               inst.instruction |= Rn << 8;
12911               if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12912                   || inst.relocs[0].type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12913                 {
12914                   if (inst.size_req == 2)
12915                     inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12916                   else
12917                     inst.relax = opcode;
12918                 }
12919             }
12920           else
12921             {
12922               constraint ((inst.relocs[0].type
12923                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
12924                           && (inst.relocs[0].type
12925                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
12926                           THUMB1_RELOC_ONLY);
12927
12928               inst.instruction = THUMB_OP32 (inst.instruction);
12929               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12930               inst.instruction |= Rn << r0off;
12931               inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12932             }
12933         }
12934       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12935                && (inst.instruction == T_MNEM_mov
12936                    || inst.instruction == T_MNEM_movs))
12937         {
12938           /* Register shifts are encoded as separate shift instructions.  */
12939           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12940
12941           if (in_pred_block ())
12942             narrow = !flags;
12943           else
12944             narrow = flags;
12945
12946           if (inst.size_req == 4)
12947             narrow = FALSE;
12948
12949           if (!low_regs || inst.operands[1].imm > 7)
12950             narrow = FALSE;
12951
12952           if (Rn != Rm)
12953             narrow = FALSE;
12954
12955           switch (inst.operands[1].shift_kind)
12956             {
12957             case SHIFT_LSL:
12958               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12959               break;
12960             case SHIFT_ASR:
12961               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12962               break;
12963             case SHIFT_LSR:
12964               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12965               break;
12966             case SHIFT_ROR:
12967               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12968               break;
12969             default:
12970               abort ();
12971             }
12972
12973           inst.instruction = opcode;
12974           if (narrow)
12975             {
12976               inst.instruction |= Rn;
12977               inst.instruction |= inst.operands[1].imm << 3;
12978             }
12979           else
12980             {
12981               if (flags)
12982                 inst.instruction |= CONDS_BIT;
12983
12984               inst.instruction |= Rn << 8;
12985               inst.instruction |= Rm << 16;
12986               inst.instruction |= inst.operands[1].imm;
12987             }
12988         }
12989       else if (!narrow)
12990         {
12991           /* Some mov with immediate shift have narrow variants.
12992              Register shifts are handled above.  */
12993           if (low_regs && inst.operands[1].shifted
12994               && (inst.instruction == T_MNEM_mov
12995                   || inst.instruction == T_MNEM_movs))
12996             {
12997               if (in_pred_block ())
12998                 narrow = (inst.instruction == T_MNEM_mov);
12999               else
13000                 narrow = (inst.instruction == T_MNEM_movs);
13001             }
13002
13003           if (narrow)
13004             {
13005               switch (inst.operands[1].shift_kind)
13006                 {
13007                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13008                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13009                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13010                 default: narrow = FALSE; break;
13011                 }
13012             }
13013
13014           if (narrow)
13015             {
13016               inst.instruction |= Rn;
13017               inst.instruction |= Rm << 3;
13018               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13019             }
13020           else
13021             {
13022               inst.instruction = THUMB_OP32 (inst.instruction);
13023               inst.instruction |= Rn << r0off;
13024               encode_thumb32_shifted_operand (1);
13025             }
13026         }
13027       else
13028         switch (inst.instruction)
13029           {
13030           case T_MNEM_mov:
13031             /* In v4t or v5t a move of two lowregs produces unpredictable
13032                results. Don't allow this.  */
13033             if (low_regs)
13034               {
13035                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
13036                             "MOV Rd, Rs with two low registers is not "
13037                             "permitted on this architecture");
13038                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
13039                                         arm_ext_v6);
13040               }
13041
13042             inst.instruction = T_OPCODE_MOV_HR;
13043             inst.instruction |= (Rn & 0x8) << 4;
13044             inst.instruction |= (Rn & 0x7);
13045             inst.instruction |= Rm << 3;
13046             break;
13047
13048           case T_MNEM_movs:
13049             /* We know we have low registers at this point.
13050                Generate LSLS Rd, Rs, #0.  */
13051             inst.instruction = T_OPCODE_LSL_I;
13052             inst.instruction |= Rn;
13053             inst.instruction |= Rm << 3;
13054             break;
13055
13056           case T_MNEM_cmp:
13057             if (low_regs)
13058               {
13059                 inst.instruction = T_OPCODE_CMP_LR;
13060                 inst.instruction |= Rn;
13061                 inst.instruction |= Rm << 3;
13062               }
13063             else
13064               {
13065                 inst.instruction = T_OPCODE_CMP_HR;
13066                 inst.instruction |= (Rn & 0x8) << 4;
13067                 inst.instruction |= (Rn & 0x7);
13068                 inst.instruction |= Rm << 3;
13069               }
13070             break;
13071           }
13072       return;
13073     }
13074
13075   inst.instruction = THUMB_OP16 (inst.instruction);
13076
13077   /* PR 10443: Do not silently ignore shifted operands.  */
13078   constraint (inst.operands[1].shifted,
13079               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
13080
13081   if (inst.operands[1].isreg)
13082     {
13083       if (Rn < 8 && Rm < 8)
13084         {
13085           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
13086              since a MOV instruction produces unpredictable results.  */
13087           if (inst.instruction == T_OPCODE_MOV_I8)
13088             inst.instruction = T_OPCODE_ADD_I3;
13089           else
13090             inst.instruction = T_OPCODE_CMP_LR;
13091
13092           inst.instruction |= Rn;
13093           inst.instruction |= Rm << 3;
13094         }
13095       else
13096         {
13097           if (inst.instruction == T_OPCODE_MOV_I8)
13098             inst.instruction = T_OPCODE_MOV_HR;
13099           else
13100             inst.instruction = T_OPCODE_CMP_HR;
13101           do_t_cpy ();
13102         }
13103     }
13104   else
13105     {
13106       constraint (Rn > 7,
13107                   _("only lo regs allowed with immediate"));
13108       inst.instruction |= Rn << 8;
13109       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
13110     }
13111 }
13112
13113 static void
13114 do_t_mov16 (void)
13115 {
13116   unsigned Rd;
13117   bfd_vma imm;
13118   bfd_boolean top;
13119
13120   top = (inst.instruction & 0x00800000) != 0;
13121   if (inst.relocs[0].type == BFD_RELOC_ARM_MOVW)
13122     {
13123       constraint (top, _(":lower16: not allowed in this instruction"));
13124       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVW;
13125     }
13126   else if (inst.relocs[0].type == BFD_RELOC_ARM_MOVT)
13127     {
13128       constraint (!top, _(":upper16: not allowed in this instruction"));
13129       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVT;
13130     }
13131
13132   Rd = inst.operands[0].reg;
13133   reject_bad_reg (Rd);
13134
13135   inst.instruction |= Rd << 8;
13136   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
13137     {
13138       imm = inst.relocs[0].exp.X_add_number;
13139       inst.instruction |= (imm & 0xf000) << 4;
13140       inst.instruction |= (imm & 0x0800) << 15;
13141       inst.instruction |= (imm & 0x0700) << 4;
13142       inst.instruction |= (imm & 0x00ff);
13143     }
13144 }
13145
13146 static void
13147 do_t_mvn_tst (void)
13148 {
13149   unsigned Rn, Rm;
13150
13151   Rn = inst.operands[0].reg;
13152   Rm = inst.operands[1].reg;
13153
13154   if (inst.instruction == T_MNEM_cmp
13155       || inst.instruction == T_MNEM_cmn)
13156     constraint (Rn == REG_PC, BAD_PC);
13157   else
13158     reject_bad_reg (Rn);
13159   reject_bad_reg (Rm);
13160
13161   if (unified_syntax)
13162     {
13163       int r0off = (inst.instruction == T_MNEM_mvn
13164                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
13165       bfd_boolean narrow;
13166
13167       if (inst.size_req == 4
13168           || inst.instruction > 0xffff
13169           || inst.operands[1].shifted
13170           || Rn > 7 || Rm > 7)
13171         narrow = FALSE;
13172       else if (inst.instruction == T_MNEM_cmn
13173                || inst.instruction == T_MNEM_tst)
13174         narrow = TRUE;
13175       else if (THUMB_SETS_FLAGS (inst.instruction))
13176         narrow = !in_pred_block ();
13177       else
13178         narrow = in_pred_block ();
13179
13180       if (!inst.operands[1].isreg)
13181         {
13182           /* For an immediate, we always generate a 32-bit opcode;
13183              section relaxation will shrink it later if possible.  */
13184           if (inst.instruction < 0xffff)
13185             inst.instruction = THUMB_OP32 (inst.instruction);
13186           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13187           inst.instruction |= Rn << r0off;
13188           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13189         }
13190       else
13191         {
13192           /* See if we can do this with a 16-bit instruction.  */
13193           if (narrow)
13194             {
13195               inst.instruction = THUMB_OP16 (inst.instruction);
13196               inst.instruction |= Rn;
13197               inst.instruction |= Rm << 3;
13198             }
13199           else
13200             {
13201               constraint (inst.operands[1].shifted
13202                           && inst.operands[1].immisreg,
13203                           _("shift must be constant"));
13204               if (inst.instruction < 0xffff)
13205                 inst.instruction = THUMB_OP32 (inst.instruction);
13206               inst.instruction |= Rn << r0off;
13207               encode_thumb32_shifted_operand (1);
13208             }
13209         }
13210     }
13211   else
13212     {
13213       constraint (inst.instruction > 0xffff
13214                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
13215       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
13216                   _("unshifted register required"));
13217       constraint (Rn > 7 || Rm > 7,
13218                   BAD_HIREG);
13219
13220       inst.instruction = THUMB_OP16 (inst.instruction);
13221       inst.instruction |= Rn;
13222       inst.instruction |= Rm << 3;
13223     }
13224 }
13225
13226 static void
13227 do_t_mrs (void)
13228 {
13229   unsigned Rd;
13230
13231   if (do_vfp_nsyn_mrs () == SUCCESS)
13232     return;
13233
13234   Rd = inst.operands[0].reg;
13235   reject_bad_reg (Rd);
13236   inst.instruction |= Rd << 8;
13237
13238   if (inst.operands[1].isreg)
13239     {
13240       unsigned br = inst.operands[1].reg;
13241       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
13242         as_bad (_("bad register for mrs"));
13243
13244       inst.instruction |= br & (0xf << 16);
13245       inst.instruction |= (br & 0x300) >> 4;
13246       inst.instruction |= (br & SPSR_BIT) >> 2;
13247     }
13248   else
13249     {
13250       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13251
13252       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13253         {
13254           /* PR gas/12698:  The constraint is only applied for m_profile.
13255              If the user has specified -march=all, we want to ignore it as
13256              we are building for any CPU type, including non-m variants.  */
13257           bfd_boolean m_profile =
13258             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13259           constraint ((flags != 0) && m_profile, _("selected processor does "
13260                                                    "not support requested special purpose register"));
13261         }
13262       else
13263         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
13264            devices).  */
13265         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
13266                     _("'APSR', 'CPSR' or 'SPSR' expected"));
13267
13268       inst.instruction |= (flags & SPSR_BIT) >> 2;
13269       inst.instruction |= inst.operands[1].imm & 0xff;
13270       inst.instruction |= 0xf0000;
13271     }
13272 }
13273
13274 static void
13275 do_t_msr (void)
13276 {
13277   int flags;
13278   unsigned Rn;
13279
13280   if (do_vfp_nsyn_msr () == SUCCESS)
13281     return;
13282
13283   constraint (!inst.operands[1].isreg,
13284               _("Thumb encoding does not support an immediate here"));
13285
13286   if (inst.operands[0].isreg)
13287     flags = (int)(inst.operands[0].reg);
13288   else
13289     flags = inst.operands[0].imm;
13290
13291   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13292     {
13293       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13294
13295       /* PR gas/12698:  The constraint is only applied for m_profile.
13296          If the user has specified -march=all, we want to ignore it as
13297          we are building for any CPU type, including non-m variants.  */
13298       bfd_boolean m_profile =
13299         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13300       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13301            && (bits & ~(PSR_s | PSR_f)) != 0)
13302           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13303               && bits != PSR_f)) && m_profile,
13304           _("selected processor does not support requested special "
13305             "purpose register"));
13306     }
13307   else
13308      constraint ((flags & 0xff) != 0, _("selected processor does not support "
13309                  "requested special purpose register"));
13310
13311   Rn = inst.operands[1].reg;
13312   reject_bad_reg (Rn);
13313
13314   inst.instruction |= (flags & SPSR_BIT) >> 2;
13315   inst.instruction |= (flags & 0xf0000) >> 8;
13316   inst.instruction |= (flags & 0x300) >> 4;
13317   inst.instruction |= (flags & 0xff);
13318   inst.instruction |= Rn << 16;
13319 }
13320
13321 static void
13322 do_t_mul (void)
13323 {
13324   bfd_boolean narrow;
13325   unsigned Rd, Rn, Rm;
13326
13327   if (!inst.operands[2].present)
13328     inst.operands[2].reg = inst.operands[0].reg;
13329
13330   Rd = inst.operands[0].reg;
13331   Rn = inst.operands[1].reg;
13332   Rm = inst.operands[2].reg;
13333
13334   if (unified_syntax)
13335     {
13336       if (inst.size_req == 4
13337           || (Rd != Rn
13338               && Rd != Rm)
13339           || Rn > 7
13340           || Rm > 7)
13341         narrow = FALSE;
13342       else if (inst.instruction == T_MNEM_muls)
13343         narrow = !in_pred_block ();
13344       else
13345         narrow = in_pred_block ();
13346     }
13347   else
13348     {
13349       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
13350       constraint (Rn > 7 || Rm > 7,
13351                   BAD_HIREG);
13352       narrow = TRUE;
13353     }
13354
13355   if (narrow)
13356     {
13357       /* 16-bit MULS/Conditional MUL.  */
13358       inst.instruction = THUMB_OP16 (inst.instruction);
13359       inst.instruction |= Rd;
13360
13361       if (Rd == Rn)
13362         inst.instruction |= Rm << 3;
13363       else if (Rd == Rm)
13364         inst.instruction |= Rn << 3;
13365       else
13366         constraint (1, _("dest must overlap one source register"));
13367     }
13368   else
13369     {
13370       constraint (inst.instruction != T_MNEM_mul,
13371                   _("Thumb-2 MUL must not set flags"));
13372       /* 32-bit MUL.  */
13373       inst.instruction = THUMB_OP32 (inst.instruction);
13374       inst.instruction |= Rd << 8;
13375       inst.instruction |= Rn << 16;
13376       inst.instruction |= Rm << 0;
13377
13378       reject_bad_reg (Rd);
13379       reject_bad_reg (Rn);
13380       reject_bad_reg (Rm);
13381     }
13382 }
13383
13384 static void
13385 do_t_mull (void)
13386 {
13387   unsigned RdLo, RdHi, Rn, Rm;
13388
13389   RdLo = inst.operands[0].reg;
13390   RdHi = inst.operands[1].reg;
13391   Rn = inst.operands[2].reg;
13392   Rm = inst.operands[3].reg;
13393
13394   reject_bad_reg (RdLo);
13395   reject_bad_reg (RdHi);
13396   reject_bad_reg (Rn);
13397   reject_bad_reg (Rm);
13398
13399   inst.instruction |= RdLo << 12;
13400   inst.instruction |= RdHi << 8;
13401   inst.instruction |= Rn << 16;
13402   inst.instruction |= Rm;
13403
13404  if (RdLo == RdHi)
13405     as_tsktsk (_("rdhi and rdlo must be different"));
13406 }
13407
13408 static void
13409 do_t_nop (void)
13410 {
13411   set_pred_insn_type (NEUTRAL_IT_INSN);
13412
13413   if (unified_syntax)
13414     {
13415       if (inst.size_req == 4 || inst.operands[0].imm > 15)
13416         {
13417           inst.instruction = THUMB_OP32 (inst.instruction);
13418           inst.instruction |= inst.operands[0].imm;
13419         }
13420       else
13421         {
13422           /* PR9722: Check for Thumb2 availability before
13423              generating a thumb2 nop instruction.  */
13424           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
13425             {
13426               inst.instruction = THUMB_OP16 (inst.instruction);
13427               inst.instruction |= inst.operands[0].imm << 4;
13428             }
13429           else
13430             inst.instruction = 0x46c0;
13431         }
13432     }
13433   else
13434     {
13435       constraint (inst.operands[0].present,
13436                   _("Thumb does not support NOP with hints"));
13437       inst.instruction = 0x46c0;
13438     }
13439 }
13440
13441 static void
13442 do_t_neg (void)
13443 {
13444   if (unified_syntax)
13445     {
13446       bfd_boolean narrow;
13447
13448       if (THUMB_SETS_FLAGS (inst.instruction))
13449         narrow = !in_pred_block ();
13450       else
13451         narrow = in_pred_block ();
13452       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13453         narrow = FALSE;
13454       if (inst.size_req == 4)
13455         narrow = FALSE;
13456
13457       if (!narrow)
13458         {
13459           inst.instruction = THUMB_OP32 (inst.instruction);
13460           inst.instruction |= inst.operands[0].reg << 8;
13461           inst.instruction |= inst.operands[1].reg << 16;
13462         }
13463       else
13464         {
13465           inst.instruction = THUMB_OP16 (inst.instruction);
13466           inst.instruction |= inst.operands[0].reg;
13467           inst.instruction |= inst.operands[1].reg << 3;
13468         }
13469     }
13470   else
13471     {
13472       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
13473                   BAD_HIREG);
13474       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13475
13476       inst.instruction = THUMB_OP16 (inst.instruction);
13477       inst.instruction |= inst.operands[0].reg;
13478       inst.instruction |= inst.operands[1].reg << 3;
13479     }
13480 }
13481
13482 static void
13483 do_t_orn (void)
13484 {
13485   unsigned Rd, Rn;
13486
13487   Rd = inst.operands[0].reg;
13488   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
13489
13490   reject_bad_reg (Rd);
13491   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
13492   reject_bad_reg (Rn);
13493
13494   inst.instruction |= Rd << 8;
13495   inst.instruction |= Rn << 16;
13496
13497   if (!inst.operands[2].isreg)
13498     {
13499       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13500       inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13501     }
13502   else
13503     {
13504       unsigned Rm;
13505
13506       Rm = inst.operands[2].reg;
13507       reject_bad_reg (Rm);
13508
13509       constraint (inst.operands[2].shifted
13510                   && inst.operands[2].immisreg,
13511                   _("shift must be constant"));
13512       encode_thumb32_shifted_operand (2);
13513     }
13514 }
13515
13516 static void
13517 do_t_pkhbt (void)
13518 {
13519   unsigned Rd, Rn, Rm;
13520
13521   Rd = inst.operands[0].reg;
13522   Rn = inst.operands[1].reg;
13523   Rm = inst.operands[2].reg;
13524
13525   reject_bad_reg (Rd);
13526   reject_bad_reg (Rn);
13527   reject_bad_reg (Rm);
13528
13529   inst.instruction |= Rd << 8;
13530   inst.instruction |= Rn << 16;
13531   inst.instruction |= Rm;
13532   if (inst.operands[3].present)
13533     {
13534       unsigned int val = inst.relocs[0].exp.X_add_number;
13535       constraint (inst.relocs[0].exp.X_op != O_constant,
13536                   _("expression too complex"));
13537       inst.instruction |= (val & 0x1c) << 10;
13538       inst.instruction |= (val & 0x03) << 6;
13539     }
13540 }
13541
13542 static void
13543 do_t_pkhtb (void)
13544 {
13545   if (!inst.operands[3].present)
13546     {
13547       unsigned Rtmp;
13548
13549       inst.instruction &= ~0x00000020;
13550
13551       /* PR 10168.  Swap the Rm and Rn registers.  */
13552       Rtmp = inst.operands[1].reg;
13553       inst.operands[1].reg = inst.operands[2].reg;
13554       inst.operands[2].reg = Rtmp;
13555     }
13556   do_t_pkhbt ();
13557 }
13558
13559 static void
13560 do_t_pld (void)
13561 {
13562   if (inst.operands[0].immisreg)
13563     reject_bad_reg (inst.operands[0].imm);
13564
13565   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
13566 }
13567
13568 static void
13569 do_t_push_pop (void)
13570 {
13571   unsigned mask;
13572
13573   constraint (inst.operands[0].writeback,
13574               _("push/pop do not support {reglist}^"));
13575   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
13576               _("expression too complex"));
13577
13578   mask = inst.operands[0].imm;
13579   if (inst.size_req != 4 && (mask & ~0xff) == 0)
13580     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
13581   else if (inst.size_req != 4
13582            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
13583                                        ? REG_LR : REG_PC)))
13584     {
13585       inst.instruction = THUMB_OP16 (inst.instruction);
13586       inst.instruction |= THUMB_PP_PC_LR;
13587       inst.instruction |= mask & 0xff;
13588     }
13589   else if (unified_syntax)
13590     {
13591       inst.instruction = THUMB_OP32 (inst.instruction);
13592       encode_thumb2_multi (TRUE /* do_io */, 13, mask, TRUE);
13593     }
13594   else
13595     {
13596       inst.error = _("invalid register list to push/pop instruction");
13597       return;
13598     }
13599 }
13600
13601 static void
13602 do_t_clrm (void)
13603 {
13604   if (unified_syntax)
13605     encode_thumb2_multi (FALSE /* do_io */, -1, inst.operands[0].imm, FALSE);
13606   else
13607     {
13608       inst.error = _("invalid register list to push/pop instruction");
13609       return;
13610     }
13611 }
13612
13613 static void
13614 do_t_vscclrm (void)
13615 {
13616   if (inst.operands[0].issingle)
13617     {
13618       inst.instruction |= (inst.operands[0].reg & 0x1) << 22;
13619       inst.instruction |= (inst.operands[0].reg & 0x1e) << 11;
13620       inst.instruction |= inst.operands[0].imm;
13621     }
13622   else
13623     {
13624       inst.instruction |= (inst.operands[0].reg & 0x10) << 18;
13625       inst.instruction |= (inst.operands[0].reg & 0xf) << 12;
13626       inst.instruction |= 1 << 8;
13627       inst.instruction |= inst.operands[0].imm << 1;
13628     }
13629 }
13630
13631 static void
13632 do_t_rbit (void)
13633 {
13634   unsigned Rd, Rm;
13635
13636   Rd = inst.operands[0].reg;
13637   Rm = inst.operands[1].reg;
13638
13639   reject_bad_reg (Rd);
13640   reject_bad_reg (Rm);
13641
13642   inst.instruction |= Rd << 8;
13643   inst.instruction |= Rm << 16;
13644   inst.instruction |= Rm;
13645 }
13646
13647 static void
13648 do_t_rev (void)
13649 {
13650   unsigned Rd, Rm;
13651
13652   Rd = inst.operands[0].reg;
13653   Rm = inst.operands[1].reg;
13654
13655   reject_bad_reg (Rd);
13656   reject_bad_reg (Rm);
13657
13658   if (Rd <= 7 && Rm <= 7
13659       && inst.size_req != 4)
13660     {
13661       inst.instruction = THUMB_OP16 (inst.instruction);
13662       inst.instruction |= Rd;
13663       inst.instruction |= Rm << 3;
13664     }
13665   else if (unified_syntax)
13666     {
13667       inst.instruction = THUMB_OP32 (inst.instruction);
13668       inst.instruction |= Rd << 8;
13669       inst.instruction |= Rm << 16;
13670       inst.instruction |= Rm;
13671     }
13672   else
13673     inst.error = BAD_HIREG;
13674 }
13675
13676 static void
13677 do_t_rrx (void)
13678 {
13679   unsigned Rd, Rm;
13680
13681   Rd = inst.operands[0].reg;
13682   Rm = inst.operands[1].reg;
13683
13684   reject_bad_reg (Rd);
13685   reject_bad_reg (Rm);
13686
13687   inst.instruction |= Rd << 8;
13688   inst.instruction |= Rm;
13689 }
13690
13691 static void
13692 do_t_rsb (void)
13693 {
13694   unsigned Rd, Rs;
13695
13696   Rd = inst.operands[0].reg;
13697   Rs = (inst.operands[1].present
13698         ? inst.operands[1].reg    /* Rd, Rs, foo */
13699         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
13700
13701   reject_bad_reg (Rd);
13702   reject_bad_reg (Rs);
13703   if (inst.operands[2].isreg)
13704     reject_bad_reg (inst.operands[2].reg);
13705
13706   inst.instruction |= Rd << 8;
13707   inst.instruction |= Rs << 16;
13708   if (!inst.operands[2].isreg)
13709     {
13710       bfd_boolean narrow;
13711
13712       if ((inst.instruction & 0x00100000) != 0)
13713         narrow = !in_pred_block ();
13714       else
13715         narrow = in_pred_block ();
13716
13717       if (Rd > 7 || Rs > 7)
13718         narrow = FALSE;
13719
13720       if (inst.size_req == 4 || !unified_syntax)
13721         narrow = FALSE;
13722
13723       if (inst.relocs[0].exp.X_op != O_constant
13724           || inst.relocs[0].exp.X_add_number != 0)
13725         narrow = FALSE;
13726
13727       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
13728          relaxation, but it doesn't seem worth the hassle.  */
13729       if (narrow)
13730         {
13731           inst.relocs[0].type = BFD_RELOC_UNUSED;
13732           inst.instruction = THUMB_OP16 (T_MNEM_negs);
13733           inst.instruction |= Rs << 3;
13734           inst.instruction |= Rd;
13735         }
13736       else
13737         {
13738           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13739           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13740         }
13741     }
13742   else
13743     encode_thumb32_shifted_operand (2);
13744 }
13745
13746 static void
13747 do_t_setend (void)
13748 {
13749   if (warn_on_deprecated
13750       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13751       as_tsktsk (_("setend use is deprecated for ARMv8"));
13752
13753   set_pred_insn_type (OUTSIDE_PRED_INSN);
13754   if (inst.operands[0].imm)
13755     inst.instruction |= 0x8;
13756 }
13757
13758 static void
13759 do_t_shift (void)
13760 {
13761   if (!inst.operands[1].present)
13762     inst.operands[1].reg = inst.operands[0].reg;
13763
13764   if (unified_syntax)
13765     {
13766       bfd_boolean narrow;
13767       int shift_kind;
13768
13769       switch (inst.instruction)
13770         {
13771         case T_MNEM_asr:
13772         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
13773         case T_MNEM_lsl:
13774         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
13775         case T_MNEM_lsr:
13776         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
13777         case T_MNEM_ror:
13778         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
13779         default: abort ();
13780         }
13781
13782       if (THUMB_SETS_FLAGS (inst.instruction))
13783         narrow = !in_pred_block ();
13784       else
13785         narrow = in_pred_block ();
13786       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13787         narrow = FALSE;
13788       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
13789         narrow = FALSE;
13790       if (inst.operands[2].isreg
13791           && (inst.operands[1].reg != inst.operands[0].reg
13792               || inst.operands[2].reg > 7))
13793         narrow = FALSE;
13794       if (inst.size_req == 4)
13795         narrow = FALSE;
13796
13797       reject_bad_reg (inst.operands[0].reg);
13798       reject_bad_reg (inst.operands[1].reg);
13799
13800       if (!narrow)
13801         {
13802           if (inst.operands[2].isreg)
13803             {
13804               reject_bad_reg (inst.operands[2].reg);
13805               inst.instruction = THUMB_OP32 (inst.instruction);
13806               inst.instruction |= inst.operands[0].reg << 8;
13807               inst.instruction |= inst.operands[1].reg << 16;
13808               inst.instruction |= inst.operands[2].reg;
13809
13810               /* PR 12854: Error on extraneous shifts.  */
13811               constraint (inst.operands[2].shifted,
13812                           _("extraneous shift as part of operand to shift insn"));
13813             }
13814           else
13815             {
13816               inst.operands[1].shifted = 1;
13817               inst.operands[1].shift_kind = shift_kind;
13818               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
13819                                              ? T_MNEM_movs : T_MNEM_mov);
13820               inst.instruction |= inst.operands[0].reg << 8;
13821               encode_thumb32_shifted_operand (1);
13822               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
13823               inst.relocs[0].type = BFD_RELOC_UNUSED;
13824             }
13825         }
13826       else
13827         {
13828           if (inst.operands[2].isreg)
13829             {
13830               switch (shift_kind)
13831                 {
13832                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
13833                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
13834                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
13835                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
13836                 default: abort ();
13837                 }
13838
13839               inst.instruction |= inst.operands[0].reg;
13840               inst.instruction |= inst.operands[2].reg << 3;
13841
13842               /* PR 12854: Error on extraneous shifts.  */
13843               constraint (inst.operands[2].shifted,
13844                           _("extraneous shift as part of operand to shift insn"));
13845             }
13846           else
13847             {
13848               switch (shift_kind)
13849                 {
13850                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13851                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13852                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13853                 default: abort ();
13854                 }
13855               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13856               inst.instruction |= inst.operands[0].reg;
13857               inst.instruction |= inst.operands[1].reg << 3;
13858             }
13859         }
13860     }
13861   else
13862     {
13863       constraint (inst.operands[0].reg > 7
13864                   || inst.operands[1].reg > 7, BAD_HIREG);
13865       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13866
13867       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
13868         {
13869           constraint (inst.operands[2].reg > 7, BAD_HIREG);
13870           constraint (inst.operands[0].reg != inst.operands[1].reg,
13871                       _("source1 and dest must be same register"));
13872
13873           switch (inst.instruction)
13874             {
13875             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
13876             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
13877             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
13878             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
13879             default: abort ();
13880             }
13881
13882           inst.instruction |= inst.operands[0].reg;
13883           inst.instruction |= inst.operands[2].reg << 3;
13884
13885           /* PR 12854: Error on extraneous shifts.  */
13886           constraint (inst.operands[2].shifted,
13887                       _("extraneous shift as part of operand to shift insn"));
13888         }
13889       else
13890         {
13891           switch (inst.instruction)
13892             {
13893             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
13894             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
13895             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
13896             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
13897             default: abort ();
13898             }
13899           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13900           inst.instruction |= inst.operands[0].reg;
13901           inst.instruction |= inst.operands[1].reg << 3;
13902         }
13903     }
13904 }
13905
13906 static void
13907 do_t_simd (void)
13908 {
13909   unsigned Rd, Rn, Rm;
13910
13911   Rd = inst.operands[0].reg;
13912   Rn = inst.operands[1].reg;
13913   Rm = inst.operands[2].reg;
13914
13915   reject_bad_reg (Rd);
13916   reject_bad_reg (Rn);
13917   reject_bad_reg (Rm);
13918
13919   inst.instruction |= Rd << 8;
13920   inst.instruction |= Rn << 16;
13921   inst.instruction |= Rm;
13922 }
13923
13924 static void
13925 do_t_simd2 (void)
13926 {
13927   unsigned Rd, Rn, Rm;
13928
13929   Rd = inst.operands[0].reg;
13930   Rm = inst.operands[1].reg;
13931   Rn = inst.operands[2].reg;
13932
13933   reject_bad_reg (Rd);
13934   reject_bad_reg (Rn);
13935   reject_bad_reg (Rm);
13936
13937   inst.instruction |= Rd << 8;
13938   inst.instruction |= Rn << 16;
13939   inst.instruction |= Rm;
13940 }
13941
13942 static void
13943 do_t_smc (void)
13944 {
13945   unsigned int value = inst.relocs[0].exp.X_add_number;
13946   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13947               _("SMC is not permitted on this architecture"));
13948   constraint (inst.relocs[0].exp.X_op != O_constant,
13949               _("expression too complex"));
13950   constraint (value > 0xf, _("immediate too large (bigger than 0xF)"));
13951
13952   inst.relocs[0].type = BFD_RELOC_UNUSED;
13953   inst.instruction |= (value & 0x000f) << 16;
13954
13955   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13956   set_pred_insn_type_last ();
13957 }
13958
13959 static void
13960 do_t_hvc (void)
13961 {
13962   unsigned int value = inst.relocs[0].exp.X_add_number;
13963
13964   inst.relocs[0].type = BFD_RELOC_UNUSED;
13965   inst.instruction |= (value & 0x0fff);
13966   inst.instruction |= (value & 0xf000) << 4;
13967 }
13968
13969 static void
13970 do_t_ssat_usat (int bias)
13971 {
13972   unsigned Rd, Rn;
13973
13974   Rd = inst.operands[0].reg;
13975   Rn = inst.operands[2].reg;
13976
13977   reject_bad_reg (Rd);
13978   reject_bad_reg (Rn);
13979
13980   inst.instruction |= Rd << 8;
13981   inst.instruction |= inst.operands[1].imm - bias;
13982   inst.instruction |= Rn << 16;
13983
13984   if (inst.operands[3].present)
13985     {
13986       offsetT shift_amount = inst.relocs[0].exp.X_add_number;
13987
13988       inst.relocs[0].type = BFD_RELOC_UNUSED;
13989
13990       constraint (inst.relocs[0].exp.X_op != O_constant,
13991                   _("expression too complex"));
13992
13993       if (shift_amount != 0)
13994         {
13995           constraint (shift_amount > 31,
13996                       _("shift expression is too large"));
13997
13998           if (inst.operands[3].shift_kind == SHIFT_ASR)
13999             inst.instruction |= 0x00200000;  /* sh bit.  */
14000
14001           inst.instruction |= (shift_amount & 0x1c) << 10;
14002           inst.instruction |= (shift_amount & 0x03) << 6;
14003         }
14004     }
14005 }
14006
14007 static void
14008 do_t_ssat (void)
14009 {
14010   do_t_ssat_usat (1);
14011 }
14012
14013 static void
14014 do_t_ssat16 (void)
14015 {
14016   unsigned Rd, Rn;
14017
14018   Rd = inst.operands[0].reg;
14019   Rn = inst.operands[2].reg;
14020
14021   reject_bad_reg (Rd);
14022   reject_bad_reg (Rn);
14023
14024   inst.instruction |= Rd << 8;
14025   inst.instruction |= inst.operands[1].imm - 1;
14026   inst.instruction |= Rn << 16;
14027 }
14028
14029 static void
14030 do_t_strex (void)
14031 {
14032   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
14033               || inst.operands[2].postind || inst.operands[2].writeback
14034               || inst.operands[2].immisreg || inst.operands[2].shifted
14035               || inst.operands[2].negative,
14036               BAD_ADDR_MODE);
14037
14038   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
14039
14040   inst.instruction |= inst.operands[0].reg << 8;
14041   inst.instruction |= inst.operands[1].reg << 12;
14042   inst.instruction |= inst.operands[2].reg << 16;
14043   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
14044 }
14045
14046 static void
14047 do_t_strexd (void)
14048 {
14049   if (!inst.operands[2].present)
14050     inst.operands[2].reg = inst.operands[1].reg + 1;
14051
14052   constraint (inst.operands[0].reg == inst.operands[1].reg
14053               || inst.operands[0].reg == inst.operands[2].reg
14054               || inst.operands[0].reg == inst.operands[3].reg,
14055               BAD_OVERLAP);
14056
14057   inst.instruction |= inst.operands[0].reg;
14058   inst.instruction |= inst.operands[1].reg << 12;
14059   inst.instruction |= inst.operands[2].reg << 8;
14060   inst.instruction |= inst.operands[3].reg << 16;
14061 }
14062
14063 static void
14064 do_t_sxtah (void)
14065 {
14066   unsigned Rd, Rn, Rm;
14067
14068   Rd = inst.operands[0].reg;
14069   Rn = inst.operands[1].reg;
14070   Rm = inst.operands[2].reg;
14071
14072   reject_bad_reg (Rd);
14073   reject_bad_reg (Rn);
14074   reject_bad_reg (Rm);
14075
14076   inst.instruction |= Rd << 8;
14077   inst.instruction |= Rn << 16;
14078   inst.instruction |= Rm;
14079   inst.instruction |= inst.operands[3].imm << 4;
14080 }
14081
14082 static void
14083 do_t_sxth (void)
14084 {
14085   unsigned Rd, Rm;
14086
14087   Rd = inst.operands[0].reg;
14088   Rm = inst.operands[1].reg;
14089
14090   reject_bad_reg (Rd);
14091   reject_bad_reg (Rm);
14092
14093   if (inst.instruction <= 0xffff
14094       && inst.size_req != 4
14095       && Rd <= 7 && Rm <= 7
14096       && (!inst.operands[2].present || inst.operands[2].imm == 0))
14097     {
14098       inst.instruction = THUMB_OP16 (inst.instruction);
14099       inst.instruction |= Rd;
14100       inst.instruction |= Rm << 3;
14101     }
14102   else if (unified_syntax)
14103     {
14104       if (inst.instruction <= 0xffff)
14105         inst.instruction = THUMB_OP32 (inst.instruction);
14106       inst.instruction |= Rd << 8;
14107       inst.instruction |= Rm;
14108       inst.instruction |= inst.operands[2].imm << 4;
14109     }
14110   else
14111     {
14112       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
14113                   _("Thumb encoding does not support rotation"));
14114       constraint (1, BAD_HIREG);
14115     }
14116 }
14117
14118 static void
14119 do_t_swi (void)
14120 {
14121   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
14122 }
14123
14124 static void
14125 do_t_tb (void)
14126 {
14127   unsigned Rn, Rm;
14128   int half;
14129
14130   half = (inst.instruction & 0x10) != 0;
14131   set_pred_insn_type_last ();
14132   constraint (inst.operands[0].immisreg,
14133               _("instruction requires register index"));
14134
14135   Rn = inst.operands[0].reg;
14136   Rm = inst.operands[0].imm;
14137
14138   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
14139     constraint (Rn == REG_SP, BAD_SP);
14140   reject_bad_reg (Rm);
14141
14142   constraint (!half && inst.operands[0].shifted,
14143               _("instruction does not allow shifted index"));
14144   inst.instruction |= (Rn << 16) | Rm;
14145 }
14146
14147 static void
14148 do_t_udf (void)
14149 {
14150   if (!inst.operands[0].present)
14151     inst.operands[0].imm = 0;
14152
14153   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
14154     {
14155       constraint (inst.size_req == 2,
14156                   _("immediate value out of range"));
14157       inst.instruction = THUMB_OP32 (inst.instruction);
14158       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
14159       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
14160     }
14161   else
14162     {
14163       inst.instruction = THUMB_OP16 (inst.instruction);
14164       inst.instruction |= inst.operands[0].imm;
14165     }
14166
14167   set_pred_insn_type (NEUTRAL_IT_INSN);
14168 }
14169
14170
14171 static void
14172 do_t_usat (void)
14173 {
14174   do_t_ssat_usat (0);
14175 }
14176
14177 static void
14178 do_t_usat16 (void)
14179 {
14180   unsigned Rd, Rn;
14181
14182   Rd = inst.operands[0].reg;
14183   Rn = inst.operands[2].reg;
14184
14185   reject_bad_reg (Rd);
14186   reject_bad_reg (Rn);
14187
14188   inst.instruction |= Rd << 8;
14189   inst.instruction |= inst.operands[1].imm;
14190   inst.instruction |= Rn << 16;
14191 }
14192
14193 /* Checking the range of the branch offset (VAL) with NBITS bits
14194    and IS_SIGNED signedness.  Also checks the LSB to be 0.  */
14195 static int
14196 v8_1_branch_value_check (int val, int nbits, int is_signed)
14197 {
14198   gas_assert (nbits > 0 && nbits <= 32);
14199   if (is_signed)
14200     {
14201       int cmp = (1 << (nbits - 1));
14202       if ((val < -cmp) || (val >= cmp) || (val & 0x01))
14203         return FAIL;
14204     }
14205   else
14206     {
14207       if ((val <= 0) || (val >= (1 << nbits)) || (val & 0x1))
14208         return FAIL;
14209     }
14210     return SUCCESS;
14211 }
14212
14213 /* For branches in Armv8.1-M Mainline.  */
14214 static void
14215 do_t_branch_future (void)
14216 {
14217   unsigned long insn = inst.instruction;
14218
14219   inst.instruction = THUMB_OP32 (inst.instruction);
14220   if (inst.operands[0].hasreloc == 0)
14221     {
14222       if (v8_1_branch_value_check (inst.operands[0].imm, 5, FALSE) == FAIL)
14223         as_bad (BAD_BRANCH_OFF);
14224
14225       inst.instruction |= ((inst.operands[0].imm & 0x1f) >> 1) << 23;
14226     }
14227   else
14228     {
14229       inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH5;
14230       inst.relocs[0].pc_rel = 1;
14231     }
14232
14233   switch (insn)
14234     {
14235       case T_MNEM_bf:
14236         if (inst.operands[1].hasreloc == 0)
14237           {
14238             int val = inst.operands[1].imm;
14239             if (v8_1_branch_value_check (inst.operands[1].imm, 17, TRUE) == FAIL)
14240               as_bad (BAD_BRANCH_OFF);
14241
14242             int immA = (val & 0x0001f000) >> 12;
14243             int immB = (val & 0x00000ffc) >> 2;
14244             int immC = (val & 0x00000002) >> 1;
14245             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14246           }
14247         else
14248           {
14249             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF17;
14250             inst.relocs[1].pc_rel = 1;
14251           }
14252         break;
14253
14254       case T_MNEM_bfl:
14255         if (inst.operands[1].hasreloc == 0)
14256           {
14257             int val = inst.operands[1].imm;
14258             if (v8_1_branch_value_check (inst.operands[1].imm, 19, TRUE) == FAIL)
14259               as_bad (BAD_BRANCH_OFF);
14260
14261             int immA = (val & 0x0007f000) >> 12;
14262             int immB = (val & 0x00000ffc) >> 2;
14263             int immC = (val & 0x00000002) >> 1;
14264             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14265           }
14266           else
14267           {
14268             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF19;
14269             inst.relocs[1].pc_rel = 1;
14270           }
14271         break;
14272
14273       case T_MNEM_bfcsel:
14274         /* Operand 1.  */
14275         if (inst.operands[1].hasreloc == 0)
14276           {
14277             int val = inst.operands[1].imm;
14278             int immA = (val & 0x00001000) >> 12;
14279             int immB = (val & 0x00000ffc) >> 2;
14280             int immC = (val & 0x00000002) >> 1;
14281             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14282           }
14283           else
14284           {
14285             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF13;
14286             inst.relocs[1].pc_rel = 1;
14287           }
14288
14289         /* Operand 2.  */
14290         if (inst.operands[2].hasreloc == 0)
14291           {
14292               constraint ((inst.operands[0].hasreloc != 0), BAD_ARGS);
14293               int val2 = inst.operands[2].imm;
14294               int val0 = inst.operands[0].imm & 0x1f;
14295               int diff = val2 - val0;
14296               if (diff == 4)
14297                 inst.instruction |= 1 << 17; /* T bit.  */
14298               else if (diff != 2)
14299                 as_bad (_("out of range label-relative fixup value"));
14300           }
14301         else
14302           {
14303               constraint ((inst.operands[0].hasreloc == 0), BAD_ARGS);
14304               inst.relocs[2].type = BFD_RELOC_THUMB_PCREL_BFCSEL;
14305               inst.relocs[2].pc_rel = 1;
14306           }
14307
14308         /* Operand 3.  */
14309         constraint (inst.cond != COND_ALWAYS, BAD_COND);
14310         inst.instruction |= (inst.operands[3].imm & 0xf) << 18;
14311         break;
14312
14313       case T_MNEM_bfx:
14314       case T_MNEM_bflx:
14315         inst.instruction |= inst.operands[1].reg << 16;
14316         break;
14317
14318       default: abort ();
14319     }
14320 }
14321
14322 /* Helper function for do_t_loloop to handle relocations.  */
14323 static void
14324 v8_1_loop_reloc (int is_le)
14325 {
14326   if (inst.relocs[0].exp.X_op == O_constant)
14327     {
14328       int value = inst.relocs[0].exp.X_add_number;
14329       value = (is_le) ? -value : value;
14330
14331       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
14332         as_bad (BAD_BRANCH_OFF);
14333
14334       int imml, immh;
14335
14336       immh = (value & 0x00000ffc) >> 2;
14337       imml = (value & 0x00000002) >> 1;
14338
14339       inst.instruction |= (imml << 11) | (immh << 1);
14340     }
14341   else
14342     {
14343       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_LOOP12;
14344       inst.relocs[0].pc_rel = 1;
14345     }
14346 }
14347
14348 /* For shifts in MVE.  */
14349 static void
14350 do_mve_scalar_shift (void)
14351 {
14352   if (!inst.operands[2].present)
14353     {
14354       inst.operands[2] = inst.operands[1];
14355       inst.operands[1].reg = 0xf;
14356     }
14357
14358   inst.instruction |= inst.operands[0].reg << 16;
14359   inst.instruction |= inst.operands[1].reg << 8;
14360
14361   if (inst.operands[2].isreg)
14362     {
14363       /* Assuming Rm is already checked not to be 11x1.  */
14364       constraint (inst.operands[2].reg == inst.operands[0].reg, BAD_OVERLAP);
14365       constraint (inst.operands[2].reg == inst.operands[1].reg, BAD_OVERLAP);
14366       inst.instruction |= inst.operands[2].reg << 12;
14367     }
14368   else
14369     {
14370       /* Assuming imm is already checked as [1,32].  */
14371       unsigned int value = inst.operands[2].imm;
14372       inst.instruction |= (value & 0x1c) << 10;
14373       inst.instruction |= (value & 0x03) << 6;
14374       /* Change last 4 bits from 0xd to 0xf.  */
14375       inst.instruction |= 0x2;
14376     }
14377 }
14378
14379 /* MVE instruction encoder helpers.  */
14380 #define M_MNEM_vabav    0xee800f01
14381 #define M_MNEM_vmladav    0xeef00e00
14382 #define M_MNEM_vmladava   0xeef00e20
14383 #define M_MNEM_vmladavx   0xeef01e00
14384 #define M_MNEM_vmladavax  0xeef01e20
14385 #define M_MNEM_vmlsdav    0xeef00e01
14386 #define M_MNEM_vmlsdava   0xeef00e21
14387 #define M_MNEM_vmlsdavx   0xeef01e01
14388 #define M_MNEM_vmlsdavax  0xeef01e21
14389 #define M_MNEM_vmullt   0xee011e00
14390 #define M_MNEM_vmullb   0xee010e00
14391 #define M_MNEM_vst20    0xfc801e00
14392 #define M_MNEM_vst21    0xfc801e20
14393 #define M_MNEM_vst40    0xfc801e01
14394 #define M_MNEM_vst41    0xfc801e21
14395 #define M_MNEM_vst42    0xfc801e41
14396 #define M_MNEM_vst43    0xfc801e61
14397 #define M_MNEM_vld20    0xfc901e00
14398 #define M_MNEM_vld21    0xfc901e20
14399 #define M_MNEM_vld40    0xfc901e01
14400 #define M_MNEM_vld41    0xfc901e21
14401 #define M_MNEM_vld42    0xfc901e41
14402 #define M_MNEM_vld43    0xfc901e61
14403 #define M_MNEM_vstrb    0xec000e00
14404 #define M_MNEM_vstrh    0xec000e10
14405 #define M_MNEM_vstrw    0xec000e40
14406 #define M_MNEM_vstrd    0xec000e50
14407 #define M_MNEM_vldrb    0xec100e00
14408 #define M_MNEM_vldrh    0xec100e10
14409 #define M_MNEM_vldrw    0xec100e40
14410 #define M_MNEM_vldrd    0xec100e50
14411 #define M_MNEM_vmovlt   0xeea01f40
14412 #define M_MNEM_vmovlb   0xeea00f40
14413 #define M_MNEM_vmovnt   0xfe311e81
14414 #define M_MNEM_vmovnb   0xfe310e81
14415 #define M_MNEM_vadc     0xee300f00
14416 #define M_MNEM_vadci    0xee301f00
14417 #define M_MNEM_vbrsr    0xfe011e60
14418 #define M_MNEM_vaddlv   0xee890f00
14419 #define M_MNEM_vaddlva  0xee890f20
14420 #define M_MNEM_vaddv    0xeef10f00
14421 #define M_MNEM_vaddva   0xeef10f20
14422 #define M_MNEM_vddup    0xee011f6e
14423 #define M_MNEM_vdwdup   0xee011f60
14424 #define M_MNEM_vidup    0xee010f6e
14425 #define M_MNEM_viwdup   0xee010f60
14426 #define M_MNEM_vmaxv    0xeee20f00
14427 #define M_MNEM_vmaxav   0xeee00f00
14428 #define M_MNEM_vminv    0xeee20f80
14429 #define M_MNEM_vminav   0xeee00f80
14430 #define M_MNEM_vmlaldav   0xee800e00
14431 #define M_MNEM_vmlaldava  0xee800e20
14432 #define M_MNEM_vmlaldavx  0xee801e00
14433 #define M_MNEM_vmlaldavax 0xee801e20
14434 #define M_MNEM_vmlsldav   0xee800e01
14435 #define M_MNEM_vmlsldava  0xee800e21
14436 #define M_MNEM_vmlsldavx  0xee801e01
14437 #define M_MNEM_vmlsldavax 0xee801e21
14438 #define M_MNEM_vrmlaldavhx  0xee801f00
14439 #define M_MNEM_vrmlaldavhax 0xee801f20
14440 #define M_MNEM_vrmlsldavh   0xfe800e01
14441 #define M_MNEM_vrmlsldavha  0xfe800e21
14442 #define M_MNEM_vrmlsldavhx  0xfe801e01
14443 #define M_MNEM_vrmlsldavhax 0xfe801e21
14444 #define M_MNEM_vqmovnt    0xee331e01
14445 #define M_MNEM_vqmovnb    0xee330e01
14446 #define M_MNEM_vqmovunt   0xee311e81
14447 #define M_MNEM_vqmovunb   0xee310e81
14448 #define M_MNEM_vshrnt       0xee801fc1
14449 #define M_MNEM_vshrnb       0xee800fc1
14450 #define M_MNEM_vrshrnt      0xfe801fc1
14451 #define M_MNEM_vqshrnt      0xee801f40
14452 #define M_MNEM_vqshrnb      0xee800f40
14453 #define M_MNEM_vqshrunt     0xee801fc0
14454 #define M_MNEM_vqshrunb     0xee800fc0
14455 #define M_MNEM_vrshrnb      0xfe800fc1
14456 #define M_MNEM_vqrshrnt     0xee801f41
14457 #define M_MNEM_vqrshrnb     0xee800f41
14458 #define M_MNEM_vqrshrunt    0xfe801fc0
14459 #define M_MNEM_vqrshrunb    0xfe800fc0
14460
14461 /* Neon instruction encoder helpers.  */
14462
14463 /* Encodings for the different types for various Neon opcodes.  */
14464
14465 /* An "invalid" code for the following tables.  */
14466 #define N_INV -1u
14467
14468 struct neon_tab_entry
14469 {
14470   unsigned integer;
14471   unsigned float_or_poly;
14472   unsigned scalar_or_imm;
14473 };
14474
14475 /* Map overloaded Neon opcodes to their respective encodings.  */
14476 #define NEON_ENC_TAB                                    \
14477   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
14478   X(vabdl,      0x0800700, N_INV,     N_INV),           \
14479   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
14480   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
14481   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
14482   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
14483   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
14484   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
14485   X(vaddl,      0x0800000, N_INV,     N_INV),           \
14486   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
14487   X(vsubl,      0x0800200, N_INV,     N_INV),           \
14488   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
14489   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
14490   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
14491   /* Register variants of the following two instructions are encoded as
14492      vcge / vcgt with the operands reversed.  */        \
14493   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
14494   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
14495   X(vfma,       N_INV, 0x0000c10, N_INV),               \
14496   X(vfms,       N_INV, 0x0200c10, N_INV),               \
14497   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
14498   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
14499   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
14500   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
14501   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
14502   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
14503   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
14504   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
14505   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
14506   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
14507   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
14508   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
14509   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
14510   X(vshl,       0x0000400, N_INV,     0x0800510),       \
14511   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
14512   X(vand,       0x0000110, N_INV,     0x0800030),       \
14513   X(vbic,       0x0100110, N_INV,     0x0800030),       \
14514   X(veor,       0x1000110, N_INV,     N_INV),           \
14515   X(vorn,       0x0300110, N_INV,     0x0800010),       \
14516   X(vorr,       0x0200110, N_INV,     0x0800010),       \
14517   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
14518   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
14519   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
14520   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
14521   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
14522   X(vst1,       0x0000000, 0x0800000, N_INV),           \
14523   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
14524   X(vst2,       0x0000100, 0x0800100, N_INV),           \
14525   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
14526   X(vst3,       0x0000200, 0x0800200, N_INV),           \
14527   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
14528   X(vst4,       0x0000300, 0x0800300, N_INV),           \
14529   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
14530   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
14531   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
14532   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
14533   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
14534   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
14535   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
14536   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
14537   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
14538   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
14539   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
14540   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
14541   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
14542   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
14543   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
14544   X(vselge,     0xe200a00, N_INV,     N_INV),           \
14545   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
14546   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
14547   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
14548   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
14549   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
14550   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
14551   X(aes,        0x3b00300, N_INV,     N_INV),           \
14552   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
14553   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
14554   X(sha2op,     0x3ba0380, N_INV,     N_INV)
14555
14556 enum neon_opc
14557 {
14558 #define X(OPC,I,F,S) N_MNEM_##OPC
14559 NEON_ENC_TAB
14560 #undef X
14561 };
14562
14563 static const struct neon_tab_entry neon_enc_tab[] =
14564 {
14565 #define X(OPC,I,F,S) { (I), (F), (S) }
14566 NEON_ENC_TAB
14567 #undef X
14568 };
14569
14570 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
14571 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14572 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
14573 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14574 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14575 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14576 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14577 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14578 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14579 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14580 #define NEON_ENC_SINGLE_(X) \
14581   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
14582 #define NEON_ENC_DOUBLE_(X) \
14583   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
14584 #define NEON_ENC_FPV8_(X) \
14585   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
14586
14587 #define NEON_ENCODE(type, inst)                                 \
14588   do                                                            \
14589     {                                                           \
14590       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
14591       inst.is_neon = 1;                                         \
14592     }                                                           \
14593   while (0)
14594
14595 #define check_neon_suffixes                                             \
14596   do                                                                    \
14597     {                                                                   \
14598       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
14599         {                                                               \
14600           as_bad (_("invalid neon suffix for non neon instruction"));   \
14601           return;                                                       \
14602         }                                                               \
14603     }                                                                   \
14604   while (0)
14605
14606 /* Define shapes for instruction operands. The following mnemonic characters
14607    are used in this table:
14608
14609      F - VFP S<n> register
14610      D - Neon D<n> register
14611      Q - Neon Q<n> register
14612      I - Immediate
14613      S - Scalar
14614      R - ARM register
14615      L - D<n> register list
14616
14617    This table is used to generate various data:
14618      - enumerations of the form NS_DDR to be used as arguments to
14619        neon_select_shape.
14620      - a table classifying shapes into single, double, quad, mixed.
14621      - a table used to drive neon_select_shape.  */
14622
14623 #define NEON_SHAPE_DEF                  \
14624   X(4, (R, R, Q, Q), QUAD),             \
14625   X(4, (Q, R, R, I), QUAD),             \
14626   X(4, (R, R, S, S), QUAD),             \
14627   X(4, (S, S, R, R), QUAD),             \
14628   X(3, (Q, R, I), QUAD),                \
14629   X(3, (I, Q, Q), QUAD),                \
14630   X(3, (I, Q, R), QUAD),                \
14631   X(3, (R, Q, Q), QUAD),                \
14632   X(3, (D, D, D), DOUBLE),              \
14633   X(3, (Q, Q, Q), QUAD),                \
14634   X(3, (D, D, I), DOUBLE),              \
14635   X(3, (Q, Q, I), QUAD),                \
14636   X(3, (D, D, S), DOUBLE),              \
14637   X(3, (Q, Q, S), QUAD),                \
14638   X(3, (Q, Q, R), QUAD),                \
14639   X(3, (R, R, Q), QUAD),                \
14640   X(2, (R, Q),    QUAD),                \
14641   X(2, (D, D), DOUBLE),                 \
14642   X(2, (Q, Q), QUAD),                   \
14643   X(2, (D, S), DOUBLE),                 \
14644   X(2, (Q, S), QUAD),                   \
14645   X(2, (D, R), DOUBLE),                 \
14646   X(2, (Q, R), QUAD),                   \
14647   X(2, (D, I), DOUBLE),                 \
14648   X(2, (Q, I), QUAD),                   \
14649   X(3, (D, L, D), DOUBLE),              \
14650   X(2, (D, Q), MIXED),                  \
14651   X(2, (Q, D), MIXED),                  \
14652   X(3, (D, Q, I), MIXED),               \
14653   X(3, (Q, D, I), MIXED),               \
14654   X(3, (Q, D, D), MIXED),               \
14655   X(3, (D, Q, Q), MIXED),               \
14656   X(3, (Q, Q, D), MIXED),               \
14657   X(3, (Q, D, S), MIXED),               \
14658   X(3, (D, Q, S), MIXED),               \
14659   X(4, (D, D, D, I), DOUBLE),           \
14660   X(4, (Q, Q, Q, I), QUAD),             \
14661   X(4, (D, D, S, I), DOUBLE),           \
14662   X(4, (Q, Q, S, I), QUAD),             \
14663   X(2, (F, F), SINGLE),                 \
14664   X(3, (F, F, F), SINGLE),              \
14665   X(2, (F, I), SINGLE),                 \
14666   X(2, (F, D), MIXED),                  \
14667   X(2, (D, F), MIXED),                  \
14668   X(3, (F, F, I), MIXED),               \
14669   X(4, (R, R, F, F), SINGLE),           \
14670   X(4, (F, F, R, R), SINGLE),           \
14671   X(3, (D, R, R), DOUBLE),              \
14672   X(3, (R, R, D), DOUBLE),              \
14673   X(2, (S, R), SINGLE),                 \
14674   X(2, (R, S), SINGLE),                 \
14675   X(2, (F, R), SINGLE),                 \
14676   X(2, (R, F), SINGLE),                 \
14677 /* Used for MVE tail predicated loop instructions.  */\
14678   X(2, (R, R), QUAD),                   \
14679 /* Half float shape supported so far.  */\
14680   X (2, (H, D), MIXED),                 \
14681   X (2, (D, H), MIXED),                 \
14682   X (2, (H, F), MIXED),                 \
14683   X (2, (F, H), MIXED),                 \
14684   X (2, (H, H), HALF),                  \
14685   X (2, (H, R), HALF),                  \
14686   X (2, (R, H), HALF),                  \
14687   X (2, (H, I), HALF),                  \
14688   X (3, (H, H, H), HALF),               \
14689   X (3, (H, F, I), MIXED),              \
14690   X (3, (F, H, I), MIXED),              \
14691   X (3, (D, H, H), MIXED),              \
14692   X (3, (D, H, S), MIXED)
14693
14694 #define S2(A,B)         NS_##A##B
14695 #define S3(A,B,C)       NS_##A##B##C
14696 #define S4(A,B,C,D)     NS_##A##B##C##D
14697
14698 #define X(N, L, C) S##N L
14699
14700 enum neon_shape
14701 {
14702   NEON_SHAPE_DEF,
14703   NS_NULL
14704 };
14705
14706 #undef X
14707 #undef S2
14708 #undef S3
14709 #undef S4
14710
14711 enum neon_shape_class
14712 {
14713   SC_HALF,
14714   SC_SINGLE,
14715   SC_DOUBLE,
14716   SC_QUAD,
14717   SC_MIXED
14718 };
14719
14720 #define X(N, L, C) SC_##C
14721
14722 static enum neon_shape_class neon_shape_class[] =
14723 {
14724   NEON_SHAPE_DEF
14725 };
14726
14727 #undef X
14728
14729 enum neon_shape_el
14730 {
14731   SE_H,
14732   SE_F,
14733   SE_D,
14734   SE_Q,
14735   SE_I,
14736   SE_S,
14737   SE_R,
14738   SE_L
14739 };
14740
14741 /* Register widths of above.  */
14742 static unsigned neon_shape_el_size[] =
14743 {
14744   16,
14745   32,
14746   64,
14747   128,
14748   0,
14749   32,
14750   32,
14751   0
14752 };
14753
14754 struct neon_shape_info
14755 {
14756   unsigned els;
14757   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
14758 };
14759
14760 #define S2(A,B)         { SE_##A, SE_##B }
14761 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
14762 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
14763
14764 #define X(N, L, C) { N, S##N L }
14765
14766 static struct neon_shape_info neon_shape_tab[] =
14767 {
14768   NEON_SHAPE_DEF
14769 };
14770
14771 #undef X
14772 #undef S2
14773 #undef S3
14774 #undef S4
14775
14776 /* Bit masks used in type checking given instructions.
14777   'N_EQK' means the type must be the same as (or based on in some way) the key
14778    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
14779    set, various other bits can be set as well in order to modify the meaning of
14780    the type constraint.  */
14781
14782 enum neon_type_mask
14783 {
14784   N_S8   = 0x0000001,
14785   N_S16  = 0x0000002,
14786   N_S32  = 0x0000004,
14787   N_S64  = 0x0000008,
14788   N_U8   = 0x0000010,
14789   N_U16  = 0x0000020,
14790   N_U32  = 0x0000040,
14791   N_U64  = 0x0000080,
14792   N_I8   = 0x0000100,
14793   N_I16  = 0x0000200,
14794   N_I32  = 0x0000400,
14795   N_I64  = 0x0000800,
14796   N_8    = 0x0001000,
14797   N_16   = 0x0002000,
14798   N_32   = 0x0004000,
14799   N_64   = 0x0008000,
14800   N_P8   = 0x0010000,
14801   N_P16  = 0x0020000,
14802   N_F16  = 0x0040000,
14803   N_F32  = 0x0080000,
14804   N_F64  = 0x0100000,
14805   N_P64  = 0x0200000,
14806   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
14807   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
14808   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
14809   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
14810   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
14811   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
14812   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
14813   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
14814   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
14815   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
14816   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
14817   N_UTYP = 0,
14818   N_MAX_NONSPECIAL = N_P64
14819 };
14820
14821 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
14822
14823 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
14824 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14825 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
14826 #define N_S_32     (N_S8 | N_S16 | N_S32)
14827 #define N_F_16_32  (N_F16 | N_F32)
14828 #define N_SUF_32   (N_SU_32 | N_F_16_32)
14829 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
14830 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
14831 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
14832 #define N_I_MVE    (N_I8 | N_I16 | N_I32)
14833 #define N_F_MVE    (N_F16 | N_F32)
14834 #define N_SU_MVE   (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14835
14836 /* Pass this as the first type argument to neon_check_type to ignore types
14837    altogether.  */
14838 #define N_IGNORE_TYPE (N_KEY | N_EQK)
14839
14840 /* Select a "shape" for the current instruction (describing register types or
14841    sizes) from a list of alternatives. Return NS_NULL if the current instruction
14842    doesn't fit. For non-polymorphic shapes, checking is usually done as a
14843    function of operand parsing, so this function doesn't need to be called.
14844    Shapes should be listed in order of decreasing length.  */
14845
14846 static enum neon_shape
14847 neon_select_shape (enum neon_shape shape, ...)
14848 {
14849   va_list ap;
14850   enum neon_shape first_shape = shape;
14851
14852   /* Fix missing optional operands. FIXME: we don't know at this point how
14853      many arguments we should have, so this makes the assumption that we have
14854      > 1. This is true of all current Neon opcodes, I think, but may not be
14855      true in the future.  */
14856   if (!inst.operands[1].present)
14857     inst.operands[1] = inst.operands[0];
14858
14859   va_start (ap, shape);
14860
14861   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
14862     {
14863       unsigned j;
14864       int matches = 1;
14865
14866       for (j = 0; j < neon_shape_tab[shape].els; j++)
14867         {
14868           if (!inst.operands[j].present)
14869             {
14870               matches = 0;
14871               break;
14872             }
14873
14874           switch (neon_shape_tab[shape].el[j])
14875             {
14876               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
14877                  a VFP single precision register operand, it's essentially
14878                  means only half of the register is used.
14879
14880                  If the type specifier is given after the mnemonics, the
14881                  information is stored in inst.vectype.  If the type specifier
14882                  is given after register operand, the information is stored
14883                  in inst.operands[].vectype.
14884
14885                  When there is only one type specifier, and all the register
14886                  operands are the same type of hardware register, the type
14887                  specifier applies to all register operands.
14888
14889                  If no type specifier is given, the shape is inferred from
14890                  operand information.
14891
14892                  for example:
14893                  vadd.f16 s0, s1, s2:           NS_HHH
14894                  vabs.f16 s0, s1:               NS_HH
14895                  vmov.f16 s0, r1:               NS_HR
14896                  vmov.f16 r0, s1:               NS_RH
14897                  vcvt.f16 r0, s1:               NS_RH
14898                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
14899                  vcvt.f16.s32   s2, s2:         NS_HF
14900               */
14901             case SE_H:
14902               if (!(inst.operands[j].isreg
14903                     && inst.operands[j].isvec
14904                     && inst.operands[j].issingle
14905                     && !inst.operands[j].isquad
14906                     && ((inst.vectype.elems == 1
14907                          && inst.vectype.el[0].size == 16)
14908                         || (inst.vectype.elems > 1
14909                             && inst.vectype.el[j].size == 16)
14910                         || (inst.vectype.elems == 0
14911                             && inst.operands[j].vectype.type != NT_invtype
14912                             && inst.operands[j].vectype.size == 16))))
14913                 matches = 0;
14914               break;
14915
14916             case SE_F:
14917               if (!(inst.operands[j].isreg
14918                     && inst.operands[j].isvec
14919                     && inst.operands[j].issingle
14920                     && !inst.operands[j].isquad
14921                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
14922                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
14923                         || (inst.vectype.elems == 0
14924                             && (inst.operands[j].vectype.size == 32
14925                                 || inst.operands[j].vectype.type == NT_invtype)))))
14926                 matches = 0;
14927               break;
14928
14929             case SE_D:
14930               if (!(inst.operands[j].isreg
14931                     && inst.operands[j].isvec
14932                     && !inst.operands[j].isquad
14933                     && !inst.operands[j].issingle))
14934                 matches = 0;
14935               break;
14936
14937             case SE_R:
14938               if (!(inst.operands[j].isreg
14939                     && !inst.operands[j].isvec))
14940                 matches = 0;
14941               break;
14942
14943             case SE_Q:
14944               if (!(inst.operands[j].isreg
14945                     && inst.operands[j].isvec
14946                     && inst.operands[j].isquad
14947                     && !inst.operands[j].issingle))
14948                 matches = 0;
14949               break;
14950
14951             case SE_I:
14952               if (!(!inst.operands[j].isreg
14953                     && !inst.operands[j].isscalar))
14954                 matches = 0;
14955               break;
14956
14957             case SE_S:
14958               if (!(!inst.operands[j].isreg
14959                     && inst.operands[j].isscalar))
14960                 matches = 0;
14961               break;
14962
14963             case SE_L:
14964               break;
14965             }
14966           if (!matches)
14967             break;
14968         }
14969       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
14970         /* We've matched all the entries in the shape table, and we don't
14971            have any left over operands which have not been matched.  */
14972         break;
14973     }
14974
14975   va_end (ap);
14976
14977   if (shape == NS_NULL && first_shape != NS_NULL)
14978     first_error (_("invalid instruction shape"));
14979
14980   return shape;
14981 }
14982
14983 /* True if SHAPE is predominantly a quadword operation (most of the time, this
14984    means the Q bit should be set).  */
14985
14986 static int
14987 neon_quad (enum neon_shape shape)
14988 {
14989   return neon_shape_class[shape] == SC_QUAD;
14990 }
14991
14992 static void
14993 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
14994                        unsigned *g_size)
14995 {
14996   /* Allow modification to be made to types which are constrained to be
14997      based on the key element, based on bits set alongside N_EQK.  */
14998   if ((typebits & N_EQK) != 0)
14999     {
15000       if ((typebits & N_HLF) != 0)
15001         *g_size /= 2;
15002       else if ((typebits & N_DBL) != 0)
15003         *g_size *= 2;
15004       if ((typebits & N_SGN) != 0)
15005         *g_type = NT_signed;
15006       else if ((typebits & N_UNS) != 0)
15007         *g_type = NT_unsigned;
15008       else if ((typebits & N_INT) != 0)
15009         *g_type = NT_integer;
15010       else if ((typebits & N_FLT) != 0)
15011         *g_type = NT_float;
15012       else if ((typebits & N_SIZ) != 0)
15013         *g_type = NT_untyped;
15014     }
15015 }
15016
15017 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
15018    operand type, i.e. the single type specified in a Neon instruction when it
15019    is the only one given.  */
15020
15021 static struct neon_type_el
15022 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
15023 {
15024   struct neon_type_el dest = *key;
15025
15026   gas_assert ((thisarg & N_EQK) != 0);
15027
15028   neon_modify_type_size (thisarg, &dest.type, &dest.size);
15029
15030   return dest;
15031 }
15032
15033 /* Convert Neon type and size into compact bitmask representation.  */
15034
15035 static enum neon_type_mask
15036 type_chk_of_el_type (enum neon_el_type type, unsigned size)
15037 {
15038   switch (type)
15039     {
15040     case NT_untyped:
15041       switch (size)
15042         {
15043         case 8:  return N_8;
15044         case 16: return N_16;
15045         case 32: return N_32;
15046         case 64: return N_64;
15047         default: ;
15048         }
15049       break;
15050
15051     case NT_integer:
15052       switch (size)
15053         {
15054         case 8:  return N_I8;
15055         case 16: return N_I16;
15056         case 32: return N_I32;
15057         case 64: return N_I64;
15058         default: ;
15059         }
15060       break;
15061
15062     case NT_float:
15063       switch (size)
15064         {
15065         case 16: return N_F16;
15066         case 32: return N_F32;
15067         case 64: return N_F64;
15068         default: ;
15069         }
15070       break;
15071
15072     case NT_poly:
15073       switch (size)
15074         {
15075         case 8:  return N_P8;
15076         case 16: return N_P16;
15077         case 64: return N_P64;
15078         default: ;
15079         }
15080       break;
15081
15082     case NT_signed:
15083       switch (size)
15084         {
15085         case 8:  return N_S8;
15086         case 16: return N_S16;
15087         case 32: return N_S32;
15088         case 64: return N_S64;
15089         default: ;
15090         }
15091       break;
15092
15093     case NT_unsigned:
15094       switch (size)
15095         {
15096         case 8:  return N_U8;
15097         case 16: return N_U16;
15098         case 32: return N_U32;
15099         case 64: return N_U64;
15100         default: ;
15101         }
15102       break;
15103
15104     default: ;
15105     }
15106
15107   return N_UTYP;
15108 }
15109
15110 /* Convert compact Neon bitmask type representation to a type and size. Only
15111    handles the case where a single bit is set in the mask.  */
15112
15113 static int
15114 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
15115                      enum neon_type_mask mask)
15116 {
15117   if ((mask & N_EQK) != 0)
15118     return FAIL;
15119
15120   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
15121     *size = 8;
15122   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
15123     *size = 16;
15124   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
15125     *size = 32;
15126   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
15127     *size = 64;
15128   else
15129     return FAIL;
15130
15131   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
15132     *type = NT_signed;
15133   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
15134     *type = NT_unsigned;
15135   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
15136     *type = NT_integer;
15137   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
15138     *type = NT_untyped;
15139   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
15140     *type = NT_poly;
15141   else if ((mask & (N_F_ALL)) != 0)
15142     *type = NT_float;
15143   else
15144     return FAIL;
15145
15146   return SUCCESS;
15147 }
15148
15149 /* Modify a bitmask of allowed types. This is only needed for type
15150    relaxation.  */
15151
15152 static unsigned
15153 modify_types_allowed (unsigned allowed, unsigned mods)
15154 {
15155   unsigned size;
15156   enum neon_el_type type;
15157   unsigned destmask;
15158   int i;
15159
15160   destmask = 0;
15161
15162   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
15163     {
15164       if (el_type_of_type_chk (&type, &size,
15165                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
15166         {
15167           neon_modify_type_size (mods, &type, &size);
15168           destmask |= type_chk_of_el_type (type, size);
15169         }
15170     }
15171
15172   return destmask;
15173 }
15174
15175 /* Check type and return type classification.
15176    The manual states (paraphrase): If one datatype is given, it indicates the
15177    type given in:
15178     - the second operand, if there is one
15179     - the operand, if there is no second operand
15180     - the result, if there are no operands.
15181    This isn't quite good enough though, so we use a concept of a "key" datatype
15182    which is set on a per-instruction basis, which is the one which matters when
15183    only one data type is written.
15184    Note: this function has side-effects (e.g. filling in missing operands). All
15185    Neon instructions should call it before performing bit encoding.  */
15186
15187 static struct neon_type_el
15188 neon_check_type (unsigned els, enum neon_shape ns, ...)
15189 {
15190   va_list ap;
15191   unsigned i, pass, key_el = 0;
15192   unsigned types[NEON_MAX_TYPE_ELS];
15193   enum neon_el_type k_type = NT_invtype;
15194   unsigned k_size = -1u;
15195   struct neon_type_el badtype = {NT_invtype, -1};
15196   unsigned key_allowed = 0;
15197
15198   /* Optional registers in Neon instructions are always (not) in operand 1.
15199      Fill in the missing operand here, if it was omitted.  */
15200   if (els > 1 && !inst.operands[1].present)
15201     inst.operands[1] = inst.operands[0];
15202
15203   /* Suck up all the varargs.  */
15204   va_start (ap, ns);
15205   for (i = 0; i < els; i++)
15206     {
15207       unsigned thisarg = va_arg (ap, unsigned);
15208       if (thisarg == N_IGNORE_TYPE)
15209         {
15210           va_end (ap);
15211           return badtype;
15212         }
15213       types[i] = thisarg;
15214       if ((thisarg & N_KEY) != 0)
15215         key_el = i;
15216     }
15217   va_end (ap);
15218
15219   if (inst.vectype.elems > 0)
15220     for (i = 0; i < els; i++)
15221       if (inst.operands[i].vectype.type != NT_invtype)
15222         {
15223           first_error (_("types specified in both the mnemonic and operands"));
15224           return badtype;
15225         }
15226
15227   /* Duplicate inst.vectype elements here as necessary.
15228      FIXME: No idea if this is exactly the same as the ARM assembler,
15229      particularly when an insn takes one register and one non-register
15230      operand. */
15231   if (inst.vectype.elems == 1 && els > 1)
15232     {
15233       unsigned j;
15234       inst.vectype.elems = els;
15235       inst.vectype.el[key_el] = inst.vectype.el[0];
15236       for (j = 0; j < els; j++)
15237         if (j != key_el)
15238           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
15239                                                   types[j]);
15240     }
15241   else if (inst.vectype.elems == 0 && els > 0)
15242     {
15243       unsigned j;
15244       /* No types were given after the mnemonic, so look for types specified
15245          after each operand. We allow some flexibility here; as long as the
15246          "key" operand has a type, we can infer the others.  */
15247       for (j = 0; j < els; j++)
15248         if (inst.operands[j].vectype.type != NT_invtype)
15249           inst.vectype.el[j] = inst.operands[j].vectype;
15250
15251       if (inst.operands[key_el].vectype.type != NT_invtype)
15252         {
15253           for (j = 0; j < els; j++)
15254             if (inst.operands[j].vectype.type == NT_invtype)
15255               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
15256                                                       types[j]);
15257         }
15258       else
15259         {
15260           first_error (_("operand types can't be inferred"));
15261           return badtype;
15262         }
15263     }
15264   else if (inst.vectype.elems != els)
15265     {
15266       first_error (_("type specifier has the wrong number of parts"));
15267       return badtype;
15268     }
15269
15270   for (pass = 0; pass < 2; pass++)
15271     {
15272       for (i = 0; i < els; i++)
15273         {
15274           unsigned thisarg = types[i];
15275           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
15276             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
15277           enum neon_el_type g_type = inst.vectype.el[i].type;
15278           unsigned g_size = inst.vectype.el[i].size;
15279
15280           /* Decay more-specific signed & unsigned types to sign-insensitive
15281              integer types if sign-specific variants are unavailable.  */
15282           if ((g_type == NT_signed || g_type == NT_unsigned)
15283               && (types_allowed & N_SU_ALL) == 0)
15284             g_type = NT_integer;
15285
15286           /* If only untyped args are allowed, decay any more specific types to
15287              them. Some instructions only care about signs for some element
15288              sizes, so handle that properly.  */
15289           if (((types_allowed & N_UNT) == 0)
15290               && ((g_size == 8 && (types_allowed & N_8) != 0)
15291                   || (g_size == 16 && (types_allowed & N_16) != 0)
15292                   || (g_size == 32 && (types_allowed & N_32) != 0)
15293                   || (g_size == 64 && (types_allowed & N_64) != 0)))
15294             g_type = NT_untyped;
15295
15296           if (pass == 0)
15297             {
15298               if ((thisarg & N_KEY) != 0)
15299                 {
15300                   k_type = g_type;
15301                   k_size = g_size;
15302                   key_allowed = thisarg & ~N_KEY;
15303
15304                   /* Check architecture constraint on FP16 extension.  */
15305                   if (k_size == 16
15306                       && k_type == NT_float
15307                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15308                     {
15309                       inst.error = _(BAD_FP16);
15310                       return badtype;
15311                     }
15312                 }
15313             }
15314           else
15315             {
15316               if ((thisarg & N_VFP) != 0)
15317                 {
15318                   enum neon_shape_el regshape;
15319                   unsigned regwidth, match;
15320
15321                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
15322                   if (ns == NS_NULL)
15323                     {
15324                       first_error (_("invalid instruction shape"));
15325                       return badtype;
15326                     }
15327                   regshape = neon_shape_tab[ns].el[i];
15328                   regwidth = neon_shape_el_size[regshape];
15329
15330                   /* In VFP mode, operands must match register widths. If we
15331                      have a key operand, use its width, else use the width of
15332                      the current operand.  */
15333                   if (k_size != -1u)
15334                     match = k_size;
15335                   else
15336                     match = g_size;
15337
15338                   /* FP16 will use a single precision register.  */
15339                   if (regwidth == 32 && match == 16)
15340                     {
15341                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15342                         match = regwidth;
15343                       else
15344                         {
15345                           inst.error = _(BAD_FP16);
15346                           return badtype;
15347                         }
15348                     }
15349
15350                   if (regwidth != match)
15351                     {
15352                       first_error (_("operand size must match register width"));
15353                       return badtype;
15354                     }
15355                 }
15356
15357               if ((thisarg & N_EQK) == 0)
15358                 {
15359                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
15360
15361                   if ((given_type & types_allowed) == 0)
15362                     {
15363                       first_error (BAD_SIMD_TYPE);
15364                       return badtype;
15365                     }
15366                 }
15367               else
15368                 {
15369                   enum neon_el_type mod_k_type = k_type;
15370                   unsigned mod_k_size = k_size;
15371                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
15372                   if (g_type != mod_k_type || g_size != mod_k_size)
15373                     {
15374                       first_error (_("inconsistent types in Neon instruction"));
15375                       return badtype;
15376                     }
15377                 }
15378             }
15379         }
15380     }
15381
15382   return inst.vectype.el[key_el];
15383 }
15384
15385 /* Neon-style VFP instruction forwarding.  */
15386
15387 /* Thumb VFP instructions have 0xE in the condition field.  */
15388
15389 static void
15390 do_vfp_cond_or_thumb (void)
15391 {
15392   inst.is_neon = 1;
15393
15394   if (thumb_mode)
15395     inst.instruction |= 0xe0000000;
15396   else
15397     inst.instruction |= inst.cond << 28;
15398 }
15399
15400 /* Look up and encode a simple mnemonic, for use as a helper function for the
15401    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
15402    etc.  It is assumed that operand parsing has already been done, and that the
15403    operands are in the form expected by the given opcode (this isn't necessarily
15404    the same as the form in which they were parsed, hence some massaging must
15405    take place before this function is called).
15406    Checks current arch version against that in the looked-up opcode.  */
15407
15408 static void
15409 do_vfp_nsyn_opcode (const char *opname)
15410 {
15411   const struct asm_opcode *opcode;
15412
15413   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
15414
15415   if (!opcode)
15416     abort ();
15417
15418   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
15419                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
15420               _(BAD_FPU));
15421
15422   inst.is_neon = 1;
15423
15424   if (thumb_mode)
15425     {
15426       inst.instruction = opcode->tvalue;
15427       opcode->tencode ();
15428     }
15429   else
15430     {
15431       inst.instruction = (inst.cond << 28) | opcode->avalue;
15432       opcode->aencode ();
15433     }
15434 }
15435
15436 static void
15437 do_vfp_nsyn_add_sub (enum neon_shape rs)
15438 {
15439   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
15440
15441   if (rs == NS_FFF || rs == NS_HHH)
15442     {
15443       if (is_add)
15444         do_vfp_nsyn_opcode ("fadds");
15445       else
15446         do_vfp_nsyn_opcode ("fsubs");
15447
15448       /* ARMv8.2 fp16 instruction.  */
15449       if (rs == NS_HHH)
15450         do_scalar_fp16_v82_encode ();
15451     }
15452   else
15453     {
15454       if (is_add)
15455         do_vfp_nsyn_opcode ("faddd");
15456       else
15457         do_vfp_nsyn_opcode ("fsubd");
15458     }
15459 }
15460
15461 /* Check operand types to see if this is a VFP instruction, and if so call
15462    PFN ().  */
15463
15464 static int
15465 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
15466 {
15467   enum neon_shape rs;
15468   struct neon_type_el et;
15469
15470   switch (args)
15471     {
15472     case 2:
15473       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15474       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15475       break;
15476
15477     case 3:
15478       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15479       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15480                             N_F_ALL | N_KEY | N_VFP);
15481       break;
15482
15483     default:
15484       abort ();
15485     }
15486
15487   if (et.type != NT_invtype)
15488     {
15489       pfn (rs);
15490       return SUCCESS;
15491     }
15492
15493   inst.error = NULL;
15494   return FAIL;
15495 }
15496
15497 static void
15498 do_vfp_nsyn_mla_mls (enum neon_shape rs)
15499 {
15500   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
15501
15502   if (rs == NS_FFF || rs == NS_HHH)
15503     {
15504       if (is_mla)
15505         do_vfp_nsyn_opcode ("fmacs");
15506       else
15507         do_vfp_nsyn_opcode ("fnmacs");
15508
15509       /* ARMv8.2 fp16 instruction.  */
15510       if (rs == NS_HHH)
15511         do_scalar_fp16_v82_encode ();
15512     }
15513   else
15514     {
15515       if (is_mla)
15516         do_vfp_nsyn_opcode ("fmacd");
15517       else
15518         do_vfp_nsyn_opcode ("fnmacd");
15519     }
15520 }
15521
15522 static void
15523 do_vfp_nsyn_fma_fms (enum neon_shape rs)
15524 {
15525   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
15526
15527   if (rs == NS_FFF || rs == NS_HHH)
15528     {
15529       if (is_fma)
15530         do_vfp_nsyn_opcode ("ffmas");
15531       else
15532         do_vfp_nsyn_opcode ("ffnmas");
15533
15534       /* ARMv8.2 fp16 instruction.  */
15535       if (rs == NS_HHH)
15536         do_scalar_fp16_v82_encode ();
15537     }
15538   else
15539     {
15540       if (is_fma)
15541         do_vfp_nsyn_opcode ("ffmad");
15542       else
15543         do_vfp_nsyn_opcode ("ffnmad");
15544     }
15545 }
15546
15547 static void
15548 do_vfp_nsyn_mul (enum neon_shape rs)
15549 {
15550   if (rs == NS_FFF || rs == NS_HHH)
15551     {
15552       do_vfp_nsyn_opcode ("fmuls");
15553
15554       /* ARMv8.2 fp16 instruction.  */
15555       if (rs == NS_HHH)
15556         do_scalar_fp16_v82_encode ();
15557     }
15558   else
15559     do_vfp_nsyn_opcode ("fmuld");
15560 }
15561
15562 static void
15563 do_vfp_nsyn_abs_neg (enum neon_shape rs)
15564 {
15565   int is_neg = (inst.instruction & 0x80) != 0;
15566   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
15567
15568   if (rs == NS_FF || rs == NS_HH)
15569     {
15570       if (is_neg)
15571         do_vfp_nsyn_opcode ("fnegs");
15572       else
15573         do_vfp_nsyn_opcode ("fabss");
15574
15575       /* ARMv8.2 fp16 instruction.  */
15576       if (rs == NS_HH)
15577         do_scalar_fp16_v82_encode ();
15578     }
15579   else
15580     {
15581       if (is_neg)
15582         do_vfp_nsyn_opcode ("fnegd");
15583       else
15584         do_vfp_nsyn_opcode ("fabsd");
15585     }
15586 }
15587
15588 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
15589    insns belong to Neon, and are handled elsewhere.  */
15590
15591 static void
15592 do_vfp_nsyn_ldm_stm (int is_dbmode)
15593 {
15594   int is_ldm = (inst.instruction & (1 << 20)) != 0;
15595   if (is_ldm)
15596     {
15597       if (is_dbmode)
15598         do_vfp_nsyn_opcode ("fldmdbs");
15599       else
15600         do_vfp_nsyn_opcode ("fldmias");
15601     }
15602   else
15603     {
15604       if (is_dbmode)
15605         do_vfp_nsyn_opcode ("fstmdbs");
15606       else
15607         do_vfp_nsyn_opcode ("fstmias");
15608     }
15609 }
15610
15611 static void
15612 do_vfp_nsyn_sqrt (void)
15613 {
15614   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15615   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15616
15617   if (rs == NS_FF || rs == NS_HH)
15618     {
15619       do_vfp_nsyn_opcode ("fsqrts");
15620
15621       /* ARMv8.2 fp16 instruction.  */
15622       if (rs == NS_HH)
15623         do_scalar_fp16_v82_encode ();
15624     }
15625   else
15626     do_vfp_nsyn_opcode ("fsqrtd");
15627 }
15628
15629 static void
15630 do_vfp_nsyn_div (void)
15631 {
15632   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15633   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15634                    N_F_ALL | N_KEY | N_VFP);
15635
15636   if (rs == NS_FFF || rs == NS_HHH)
15637     {
15638       do_vfp_nsyn_opcode ("fdivs");
15639
15640       /* ARMv8.2 fp16 instruction.  */
15641       if (rs == NS_HHH)
15642         do_scalar_fp16_v82_encode ();
15643     }
15644   else
15645     do_vfp_nsyn_opcode ("fdivd");
15646 }
15647
15648 static void
15649 do_vfp_nsyn_nmul (void)
15650 {
15651   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15652   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15653                    N_F_ALL | N_KEY | N_VFP);
15654
15655   if (rs == NS_FFF || rs == NS_HHH)
15656     {
15657       NEON_ENCODE (SINGLE, inst);
15658       do_vfp_sp_dyadic ();
15659
15660       /* ARMv8.2 fp16 instruction.  */
15661       if (rs == NS_HHH)
15662         do_scalar_fp16_v82_encode ();
15663     }
15664   else
15665     {
15666       NEON_ENCODE (DOUBLE, inst);
15667       do_vfp_dp_rd_rn_rm ();
15668     }
15669   do_vfp_cond_or_thumb ();
15670
15671 }
15672
15673 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
15674    (0, 1, 2, 3).  */
15675
15676 static unsigned
15677 neon_logbits (unsigned x)
15678 {
15679   return ffs (x) - 4;
15680 }
15681
15682 #define LOW4(R) ((R) & 0xf)
15683 #define HI1(R) (((R) >> 4) & 1)
15684
15685 static unsigned
15686 mve_get_vcmp_vpt_cond (struct neon_type_el et)
15687 {
15688   switch (et.type)
15689     {
15690     default:
15691       first_error (BAD_EL_TYPE);
15692       return 0;
15693     case NT_float:
15694       switch (inst.operands[0].imm)
15695         {
15696         default:
15697           first_error (_("invalid condition"));
15698           return 0;
15699         case 0x0:
15700           /* eq.  */
15701           return 0;
15702         case 0x1:
15703           /* ne.  */
15704           return 1;
15705         case 0xa:
15706           /* ge/  */
15707           return 4;
15708         case 0xb:
15709           /* lt.  */
15710           return 5;
15711         case 0xc:
15712           /* gt.  */
15713           return 6;
15714         case 0xd:
15715           /* le.  */
15716           return 7;
15717         }
15718     case NT_integer:
15719       /* only accept eq and ne.  */
15720       if (inst.operands[0].imm > 1)
15721         {
15722           first_error (_("invalid condition"));
15723           return 0;
15724         }
15725       return inst.operands[0].imm;
15726     case NT_unsigned:
15727       if (inst.operands[0].imm == 0x2)
15728         return 2;
15729       else if (inst.operands[0].imm == 0x8)
15730         return 3;
15731       else
15732         {
15733           first_error (_("invalid condition"));
15734           return 0;
15735         }
15736     case NT_signed:
15737       switch (inst.operands[0].imm)
15738         {
15739           default:
15740             first_error (_("invalid condition"));
15741             return 0;
15742           case 0xa:
15743             /* ge.  */
15744             return 4;
15745           case 0xb:
15746             /* lt.  */
15747             return 5;
15748           case 0xc:
15749             /* gt.  */
15750             return 6;
15751           case 0xd:
15752             /* le.  */
15753             return 7;
15754         }
15755     }
15756   /* Should be unreachable.  */
15757   abort ();
15758 }
15759
15760 static void
15761 do_mve_vpt (void)
15762 {
15763   /* We are dealing with a vector predicated block.  */
15764   if (inst.operands[0].present)
15765     {
15766       enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15767       struct neon_type_el et
15768         = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15769                            N_EQK);
15770
15771       unsigned fcond = mve_get_vcmp_vpt_cond (et);
15772
15773       constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15774
15775       if (et.type == NT_invtype)
15776         return;
15777
15778       if (et.type == NT_float)
15779         {
15780           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15781                       BAD_FPU);
15782           constraint (et.size != 16 && et.size != 32, BAD_EL_TYPE);
15783           inst.instruction |= (et.size == 16) << 28;
15784           inst.instruction |= 0x3 << 20;
15785         }
15786       else
15787         {
15788           constraint (et.size != 8 && et.size != 16 && et.size != 32,
15789                       BAD_EL_TYPE);
15790           inst.instruction |= 1 << 28;
15791           inst.instruction |= neon_logbits (et.size) << 20;
15792         }
15793
15794       if (inst.operands[2].isquad)
15795         {
15796           inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15797           inst.instruction |= LOW4 (inst.operands[2].reg);
15798           inst.instruction |= (fcond & 0x2) >> 1;
15799         }
15800       else
15801         {
15802           if (inst.operands[2].reg == REG_SP)
15803             as_tsktsk (MVE_BAD_SP);
15804           inst.instruction |= 1 << 6;
15805           inst.instruction |= (fcond & 0x2) << 4;
15806           inst.instruction |= inst.operands[2].reg;
15807         }
15808       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15809       inst.instruction |= (fcond & 0x4) << 10;
15810       inst.instruction |= (fcond & 0x1) << 7;
15811
15812     }
15813     set_pred_insn_type (VPT_INSN);
15814     now_pred.cc = 0;
15815     now_pred.mask = ((inst.instruction & 0x00400000) >> 19)
15816                     | ((inst.instruction & 0xe000) >> 13);
15817     now_pred.warn_deprecated = FALSE;
15818     now_pred.type = VECTOR_PRED;
15819     inst.is_neon = 1;
15820 }
15821
15822 static void
15823 do_mve_vcmp (void)
15824 {
15825   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
15826   if (!inst.operands[1].isreg || !inst.operands[1].isquad)
15827     first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
15828   if (!inst.operands[2].present)
15829     first_error (_("MVE vector or ARM register expected"));
15830   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15831
15832   /* Deal with 'else' conditional MVE's vcmp, it will be parsed as vcmpe.  */
15833   if ((inst.instruction & 0xffffffff) == N_MNEM_vcmpe
15834       && inst.operands[1].isquad)
15835     {
15836       inst.instruction = N_MNEM_vcmp;
15837       inst.cond = 0x10;
15838     }
15839
15840   if (inst.cond > COND_ALWAYS)
15841     inst.pred_insn_type = INSIDE_VPT_INSN;
15842   else
15843     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15844
15845   enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15846   struct neon_type_el et
15847     = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15848                        N_EQK);
15849
15850   constraint (rs == NS_IQR && inst.operands[2].reg == REG_PC
15851               && !inst.operands[2].iszr, BAD_PC);
15852
15853   unsigned fcond = mve_get_vcmp_vpt_cond (et);
15854
15855   inst.instruction = 0xee010f00;
15856   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15857   inst.instruction |= (fcond & 0x4) << 10;
15858   inst.instruction |= (fcond & 0x1) << 7;
15859   if (et.type == NT_float)
15860     {
15861       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15862                   BAD_FPU);
15863       inst.instruction |= (et.size == 16) << 28;
15864       inst.instruction |= 0x3 << 20;
15865     }
15866   else
15867     {
15868       inst.instruction |= 1 << 28;
15869       inst.instruction |= neon_logbits (et.size) << 20;
15870     }
15871   if (inst.operands[2].isquad)
15872     {
15873       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15874       inst.instruction |= (fcond & 0x2) >> 1;
15875       inst.instruction |= LOW4 (inst.operands[2].reg);
15876     }
15877   else
15878     {
15879       if (inst.operands[2].reg == REG_SP)
15880         as_tsktsk (MVE_BAD_SP);
15881       inst.instruction |= 1 << 6;
15882       inst.instruction |= (fcond & 0x2) << 4;
15883       inst.instruction |= inst.operands[2].reg;
15884     }
15885
15886   inst.is_neon = 1;
15887   return;
15888 }
15889
15890 static void
15891 do_mve_vmaxa_vmina (void)
15892 {
15893   if (inst.cond > COND_ALWAYS)
15894     inst.pred_insn_type = INSIDE_VPT_INSN;
15895   else
15896     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15897
15898   enum neon_shape rs = neon_select_shape (NS_QQ, NS_NULL);
15899   struct neon_type_el et
15900     = neon_check_type (2, rs, N_EQK, N_KEY | N_S8 | N_S16 | N_S32);
15901
15902   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15903   inst.instruction |= neon_logbits (et.size) << 18;
15904   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15905   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15906   inst.instruction |= LOW4 (inst.operands[1].reg);
15907   inst.is_neon = 1;
15908 }
15909
15910 static void
15911 do_mve_vfmas (void)
15912 {
15913   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
15914   struct neon_type_el et
15915     = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK, N_EQK);
15916
15917   if (inst.cond > COND_ALWAYS)
15918     inst.pred_insn_type = INSIDE_VPT_INSN;
15919   else
15920     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15921
15922   if (inst.operands[2].reg == REG_SP)
15923     as_tsktsk (MVE_BAD_SP);
15924   else if (inst.operands[2].reg == REG_PC)
15925     as_tsktsk (MVE_BAD_PC);
15926
15927   inst.instruction |= (et.size == 16) << 28;
15928   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15929   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15930   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15931   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15932   inst.instruction |= inst.operands[2].reg;
15933   inst.is_neon = 1;
15934 }
15935
15936 static void
15937 do_mve_viddup (void)
15938 {
15939   if (inst.cond > COND_ALWAYS)
15940     inst.pred_insn_type = INSIDE_VPT_INSN;
15941   else
15942     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15943
15944   unsigned imm = inst.relocs[0].exp.X_add_number;
15945   constraint (imm != 1 && imm != 2 && imm != 4 && imm != 8,
15946               _("immediate must be either 1, 2, 4 or 8"));
15947
15948   enum neon_shape rs;
15949   struct neon_type_el et;
15950   unsigned Rm;
15951   if (inst.instruction == M_MNEM_vddup || inst.instruction == M_MNEM_vidup)
15952     {
15953       rs = neon_select_shape (NS_QRI, NS_NULL);
15954       et = neon_check_type (2, rs, N_KEY | N_U8 | N_U16 | N_U32, N_EQK);
15955       Rm = 7;
15956     }
15957   else
15958     {
15959       constraint ((inst.operands[2].reg % 2) != 1, BAD_EVEN);
15960       if (inst.operands[2].reg == REG_SP)
15961         as_tsktsk (MVE_BAD_SP);
15962       else if (inst.operands[2].reg == REG_PC)
15963         first_error (BAD_PC);
15964
15965       rs = neon_select_shape (NS_QRRI, NS_NULL);
15966       et = neon_check_type (3, rs, N_KEY | N_U8 | N_U16 | N_U32, N_EQK, N_EQK);
15967       Rm = inst.operands[2].reg >> 1;
15968     }
15969   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15970   inst.instruction |= neon_logbits (et.size) << 20;
15971   inst.instruction |= inst.operands[1].reg << 16;
15972   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15973   inst.instruction |= (imm > 2) << 7;
15974   inst.instruction |= Rm << 1;
15975   inst.instruction |= (imm == 2 || imm == 8);
15976   inst.is_neon = 1;
15977 }
15978
15979 static void
15980 do_mve_vmlas (void)
15981 {
15982   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
15983   struct neon_type_el et
15984     = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
15985
15986   if (inst.operands[2].reg == REG_PC)
15987     as_tsktsk (MVE_BAD_PC);
15988   else if (inst.operands[2].reg == REG_SP)
15989     as_tsktsk (MVE_BAD_SP);
15990
15991   if (inst.cond > COND_ALWAYS)
15992     inst.pred_insn_type = INSIDE_VPT_INSN;
15993   else
15994     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15995
15996   inst.instruction |= (et.type == NT_unsigned) << 28;
15997   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15998   inst.instruction |= neon_logbits (et.size) << 20;
15999   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16000   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16001   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16002   inst.instruction |= inst.operands[2].reg;
16003   inst.is_neon = 1;
16004 }
16005
16006 static void
16007 do_mve_vshll (void)
16008 {
16009   struct neon_type_el et
16010     = neon_check_type (2, NS_QQI, N_EQK, N_S8 | N_U8 | N_S16 | N_U16 | N_KEY);
16011
16012   if (inst.cond > COND_ALWAYS)
16013     inst.pred_insn_type = INSIDE_VPT_INSN;
16014   else
16015     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16016
16017   int imm = inst.operands[2].imm;
16018   constraint (imm < 1 || (unsigned)imm > et.size,
16019               _("immediate value out of range"));
16020
16021   if ((unsigned)imm == et.size)
16022     {
16023       inst.instruction |= neon_logbits (et.size) << 18;
16024       inst.instruction |= 0x110001;
16025     }
16026   else
16027     {
16028       inst.instruction |= (et.size + imm) << 16;
16029       inst.instruction |= 0x800140;
16030     }
16031
16032   inst.instruction |= (et.type == NT_unsigned) << 28;
16033   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16034   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16035   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16036   inst.instruction |= LOW4 (inst.operands[1].reg);
16037   inst.is_neon = 1;
16038 }
16039
16040 static void
16041 do_mve_vshlc (void)
16042 {
16043   if (inst.cond > COND_ALWAYS)
16044     inst.pred_insn_type = INSIDE_VPT_INSN;
16045   else
16046     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16047
16048   if (inst.operands[1].reg == REG_PC)
16049     as_tsktsk (MVE_BAD_PC);
16050   else if (inst.operands[1].reg == REG_SP)
16051     as_tsktsk (MVE_BAD_SP);
16052
16053   int imm = inst.operands[2].imm;
16054   constraint (imm < 1 || imm > 32, _("immediate value out of range"));
16055
16056   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16057   inst.instruction |= (imm & 0x1f) << 16;
16058   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16059   inst.instruction |= inst.operands[1].reg;
16060   inst.is_neon = 1;
16061 }
16062
16063 static void
16064 do_mve_vshrn (void)
16065 {
16066   unsigned types;
16067   switch (inst.instruction)
16068     {
16069     case M_MNEM_vshrnt:
16070     case M_MNEM_vshrnb:
16071     case M_MNEM_vrshrnt:
16072     case M_MNEM_vrshrnb:
16073       types = N_I16 | N_I32;
16074       break;
16075     case M_MNEM_vqshrnt:
16076     case M_MNEM_vqshrnb:
16077     case M_MNEM_vqrshrnt:
16078     case M_MNEM_vqrshrnb:
16079       types = N_U16 | N_U32 | N_S16 | N_S32;
16080       break;
16081     case M_MNEM_vqshrunt:
16082     case M_MNEM_vqshrunb:
16083     case M_MNEM_vqrshrunt:
16084     case M_MNEM_vqrshrunb:
16085       types = N_S16 | N_S32;
16086       break;
16087     default:
16088       abort ();
16089     }
16090
16091   struct neon_type_el et = neon_check_type (2, NS_QQI, N_EQK, types | N_KEY);
16092
16093   if (inst.cond > COND_ALWAYS)
16094     inst.pred_insn_type = INSIDE_VPT_INSN;
16095   else
16096     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16097
16098   unsigned Qd = inst.operands[0].reg;
16099   unsigned Qm = inst.operands[1].reg;
16100   unsigned imm = inst.operands[2].imm;
16101   constraint (imm < 1 || ((unsigned) imm) > (et.size / 2),
16102               et.size == 16
16103               ? _("immediate operand expected in the range [1,8]")
16104               : _("immediate operand expected in the range [1,16]"));
16105
16106   inst.instruction |= (et.type == NT_unsigned) << 28;
16107   inst.instruction |= HI1 (Qd) << 22;
16108   inst.instruction |= (et.size - imm) << 16;
16109   inst.instruction |= LOW4 (Qd) << 12;
16110   inst.instruction |= HI1 (Qm) << 5;
16111   inst.instruction |= LOW4 (Qm);
16112   inst.is_neon = 1;
16113 }
16114
16115 static void
16116 do_mve_vqmovn (void)
16117 {
16118   struct neon_type_el et;
16119   if (inst.instruction == M_MNEM_vqmovnt
16120      || inst.instruction == M_MNEM_vqmovnb)
16121     et = neon_check_type (2, NS_QQ, N_EQK,
16122                           N_U16 | N_U32 | N_S16 | N_S32 | N_KEY);
16123   else
16124     et = neon_check_type (2, NS_QQ, N_EQK, N_S16 | N_S32 | N_KEY);
16125
16126   if (inst.cond > COND_ALWAYS)
16127     inst.pred_insn_type = INSIDE_VPT_INSN;
16128   else
16129     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16130
16131   inst.instruction |= (et.type == NT_unsigned) << 28;
16132   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16133   inst.instruction |= (et.size == 32) << 18;
16134   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16135   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16136   inst.instruction |= LOW4 (inst.operands[1].reg);
16137   inst.is_neon = 1;
16138 }
16139
16140 static void
16141 do_mve_vpsel (void)
16142 {
16143   neon_select_shape (NS_QQQ, NS_NULL);
16144
16145   if (inst.cond > COND_ALWAYS)
16146     inst.pred_insn_type = INSIDE_VPT_INSN;
16147   else
16148     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16149
16150   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16151   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16152   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16153   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16154   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16155   inst.instruction |= LOW4 (inst.operands[2].reg);
16156   inst.is_neon = 1;
16157 }
16158
16159 static void
16160 do_mve_vpnot (void)
16161 {
16162   if (inst.cond > COND_ALWAYS)
16163     inst.pred_insn_type = INSIDE_VPT_INSN;
16164   else
16165     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16166 }
16167
16168 static void
16169 do_mve_vmaxnma_vminnma (void)
16170 {
16171   enum neon_shape rs = neon_select_shape (NS_QQ, NS_NULL);
16172   struct neon_type_el et
16173     = neon_check_type (2, rs, N_EQK, N_F_MVE | N_KEY);
16174
16175   if (inst.cond > COND_ALWAYS)
16176     inst.pred_insn_type = INSIDE_VPT_INSN;
16177   else
16178     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16179
16180   inst.instruction |= (et.size == 16) << 28;
16181   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16182   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16183   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16184   inst.instruction |= LOW4 (inst.operands[1].reg);
16185   inst.is_neon = 1;
16186 }
16187
16188 static void
16189 do_mve_vcmul (void)
16190 {
16191   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
16192   struct neon_type_el et
16193     = neon_check_type (3, rs, N_EQK, N_EQK, N_F_MVE | N_KEY);
16194
16195   if (inst.cond > COND_ALWAYS)
16196     inst.pred_insn_type = INSIDE_VPT_INSN;
16197   else
16198     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16199
16200   unsigned rot = inst.relocs[0].exp.X_add_number;
16201   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
16202               _("immediate out of range"));
16203
16204   if (et.size == 32 && (inst.operands[0].reg == inst.operands[1].reg
16205                         || inst.operands[0].reg == inst.operands[2].reg))
16206     as_tsktsk (BAD_MVE_SRCDEST);
16207
16208   inst.instruction |= (et.size == 32) << 28;
16209   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16210   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16211   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16212   inst.instruction |= (rot > 90) << 12;
16213   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16214   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16215   inst.instruction |= LOW4 (inst.operands[2].reg);
16216   inst.instruction |= (rot == 90 || rot == 270);
16217   inst.is_neon = 1;
16218 }
16219
16220 /* To handle the Low Overhead Loop instructions
16221    in Armv8.1-M Mainline and MVE.  */
16222 static void
16223 do_t_loloop (void)
16224 {
16225   unsigned long insn = inst.instruction;
16226
16227   inst.instruction = THUMB_OP32 (inst.instruction);
16228
16229   if (insn == T_MNEM_lctp)
16230     return;
16231
16232   set_pred_insn_type (MVE_OUTSIDE_PRED_INSN);
16233
16234   if (insn == T_MNEM_wlstp || insn == T_MNEM_dlstp)
16235     {
16236       struct neon_type_el et
16237        = neon_check_type (2, NS_RR, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16238       inst.instruction |= neon_logbits (et.size) << 20;
16239       inst.is_neon = 1;
16240     }
16241
16242   switch (insn)
16243     {
16244     case T_MNEM_letp:
16245       constraint (!inst.operands[0].present,
16246                   _("expected LR"));
16247       /* fall through.  */
16248     case T_MNEM_le:
16249       /* le <label>.  */
16250       if (!inst.operands[0].present)
16251        inst.instruction |= 1 << 21;
16252
16253       v8_1_loop_reloc (TRUE);
16254       break;
16255
16256     case T_MNEM_wls:
16257     case T_MNEM_wlstp:
16258       v8_1_loop_reloc (FALSE);
16259       /* fall through.  */
16260     case T_MNEM_dlstp:
16261     case T_MNEM_dls:
16262       constraint (inst.operands[1].isreg != 1, BAD_ARGS);
16263
16264       if (insn == T_MNEM_wlstp || insn == T_MNEM_dlstp)
16265        constraint (inst.operands[1].reg == REG_PC, BAD_PC);
16266       else if (inst.operands[1].reg == REG_PC)
16267        as_tsktsk (MVE_BAD_PC);
16268       if (inst.operands[1].reg == REG_SP)
16269        as_tsktsk (MVE_BAD_SP);
16270
16271       inst.instruction |= (inst.operands[1].reg << 16);
16272       break;
16273
16274     default:
16275       abort ();
16276     }
16277 }
16278
16279
16280 static void
16281 do_vfp_nsyn_cmp (void)
16282 {
16283   enum neon_shape rs;
16284   if (!inst.operands[0].isreg)
16285     {
16286       do_mve_vcmp ();
16287       return;
16288     }
16289   else
16290     {
16291       constraint (inst.operands[2].present, BAD_SYNTAX);
16292       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd),
16293                   BAD_FPU);
16294     }
16295
16296   if (inst.operands[1].isreg)
16297     {
16298       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
16299       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
16300
16301       if (rs == NS_FF || rs == NS_HH)
16302         {
16303           NEON_ENCODE (SINGLE, inst);
16304           do_vfp_sp_monadic ();
16305         }
16306       else
16307         {
16308           NEON_ENCODE (DOUBLE, inst);
16309           do_vfp_dp_rd_rm ();
16310         }
16311     }
16312   else
16313     {
16314       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
16315       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
16316
16317       switch (inst.instruction & 0x0fffffff)
16318         {
16319         case N_MNEM_vcmp:
16320           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
16321           break;
16322         case N_MNEM_vcmpe:
16323           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
16324           break;
16325         default:
16326           abort ();
16327         }
16328
16329       if (rs == NS_FI || rs == NS_HI)
16330         {
16331           NEON_ENCODE (SINGLE, inst);
16332           do_vfp_sp_compare_z ();
16333         }
16334       else
16335         {
16336           NEON_ENCODE (DOUBLE, inst);
16337           do_vfp_dp_rd ();
16338         }
16339     }
16340   do_vfp_cond_or_thumb ();
16341
16342   /* ARMv8.2 fp16 instruction.  */
16343   if (rs == NS_HI || rs == NS_HH)
16344     do_scalar_fp16_v82_encode ();
16345 }
16346
16347 static void
16348 nsyn_insert_sp (void)
16349 {
16350   inst.operands[1] = inst.operands[0];
16351   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
16352   inst.operands[0].reg = REG_SP;
16353   inst.operands[0].isreg = 1;
16354   inst.operands[0].writeback = 1;
16355   inst.operands[0].present = 1;
16356 }
16357
16358 static void
16359 do_vfp_nsyn_push (void)
16360 {
16361   nsyn_insert_sp ();
16362
16363   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16364               _("register list must contain at least 1 and at most 16 "
16365                 "registers"));
16366
16367   if (inst.operands[1].issingle)
16368     do_vfp_nsyn_opcode ("fstmdbs");
16369   else
16370     do_vfp_nsyn_opcode ("fstmdbd");
16371 }
16372
16373 static void
16374 do_vfp_nsyn_pop (void)
16375 {
16376   nsyn_insert_sp ();
16377
16378   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16379               _("register list must contain at least 1 and at most 16 "
16380                 "registers"));
16381
16382   if (inst.operands[1].issingle)
16383     do_vfp_nsyn_opcode ("fldmias");
16384   else
16385     do_vfp_nsyn_opcode ("fldmiad");
16386 }
16387
16388 /* Fix up Neon data-processing instructions, ORing in the correct bits for
16389    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
16390
16391 static void
16392 neon_dp_fixup (struct arm_it* insn)
16393 {
16394   unsigned int i = insn->instruction;
16395   insn->is_neon = 1;
16396
16397   if (thumb_mode)
16398     {
16399       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
16400       if (i & (1 << 24))
16401         i |= 1 << 28;
16402
16403       i &= ~(1 << 24);
16404
16405       i |= 0xef000000;
16406     }
16407   else
16408     i |= 0xf2000000;
16409
16410   insn->instruction = i;
16411 }
16412
16413 static void
16414 mve_encode_qqr (int size, int U, int fp)
16415 {
16416   if (inst.operands[2].reg == REG_SP)
16417     as_tsktsk (MVE_BAD_SP);
16418   else if (inst.operands[2].reg == REG_PC)
16419     as_tsktsk (MVE_BAD_PC);
16420
16421   if (fp)
16422     {
16423       /* vadd.  */
16424       if (((unsigned)inst.instruction) == 0xd00)
16425         inst.instruction = 0xee300f40;
16426       /* vsub.  */
16427       else if (((unsigned)inst.instruction) == 0x200d00)
16428         inst.instruction = 0xee301f40;
16429       /* vmul.  */
16430       else if (((unsigned)inst.instruction) == 0x1000d10)
16431         inst.instruction = 0xee310e60;
16432
16433       /* Setting size which is 1 for F16 and 0 for F32.  */
16434       inst.instruction |= (size == 16) << 28;
16435     }
16436   else
16437     {
16438       /* vadd.  */
16439       if (((unsigned)inst.instruction) == 0x800)
16440         inst.instruction = 0xee010f40;
16441       /* vsub.  */
16442       else if (((unsigned)inst.instruction) == 0x1000800)
16443         inst.instruction = 0xee011f40;
16444       /* vhadd.  */
16445       else if (((unsigned)inst.instruction) == 0)
16446         inst.instruction = 0xee000f40;
16447       /* vhsub.  */
16448       else if (((unsigned)inst.instruction) == 0x200)
16449         inst.instruction = 0xee001f40;
16450       /* vmla.  */
16451       else if (((unsigned)inst.instruction) == 0x900)
16452         inst.instruction = 0xee010e40;
16453       /* vmul.  */
16454       else if (((unsigned)inst.instruction) == 0x910)
16455         inst.instruction = 0xee011e60;
16456       /* vqadd.  */
16457       else if (((unsigned)inst.instruction) == 0x10)
16458         inst.instruction = 0xee000f60;
16459       /* vqsub.  */
16460       else if (((unsigned)inst.instruction) == 0x210)
16461         inst.instruction = 0xee001f60;
16462       /* vqrdmlah.  */
16463       else if (((unsigned)inst.instruction) == 0x3000b10)
16464         inst.instruction = 0xee000e40;
16465       /* vqdmulh.  */
16466       else if (((unsigned)inst.instruction) == 0x0000b00)
16467         inst.instruction = 0xee010e60;
16468       /* vqrdmulh.  */
16469       else if (((unsigned)inst.instruction) == 0x1000b00)
16470         inst.instruction = 0xfe010e60;
16471
16472       /* Set U-bit.  */
16473       inst.instruction |= U << 28;
16474
16475       /* Setting bits for size.  */
16476       inst.instruction |= neon_logbits (size) << 20;
16477     }
16478   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16479   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16480   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16481   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16482   inst.instruction |= inst.operands[2].reg;
16483   inst.is_neon = 1;
16484 }
16485
16486 static void
16487 mve_encode_rqq (unsigned bit28, unsigned size)
16488 {
16489   inst.instruction |= bit28 << 28;
16490   inst.instruction |= neon_logbits (size) << 20;
16491   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16492   inst.instruction |= inst.operands[0].reg << 12;
16493   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16494   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16495   inst.instruction |= LOW4 (inst.operands[2].reg);
16496   inst.is_neon = 1;
16497 }
16498
16499 static void
16500 mve_encode_qqq (int ubit, int size)
16501 {
16502
16503   inst.instruction |= (ubit != 0) << 28;
16504   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16505   inst.instruction |= neon_logbits (size) << 20;
16506   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16507   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16508   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16509   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16510   inst.instruction |= LOW4 (inst.operands[2].reg);
16511
16512   inst.is_neon = 1;
16513 }
16514
16515 static void
16516 mve_encode_rq (unsigned bit28, unsigned size)
16517 {
16518   inst.instruction |= bit28 << 28;
16519   inst.instruction |= neon_logbits (size) << 18;
16520   inst.instruction |= inst.operands[0].reg << 12;
16521   inst.instruction |= LOW4 (inst.operands[1].reg);
16522   inst.is_neon = 1;
16523 }
16524
16525 static void
16526 mve_encode_rrqq (unsigned U, unsigned size)
16527 {
16528   constraint (inst.operands[3].reg > 14, MVE_BAD_QREG);
16529
16530   inst.instruction |= U << 28;
16531   inst.instruction |= (inst.operands[1].reg >> 1) << 20;
16532   inst.instruction |= LOW4 (inst.operands[2].reg) << 16;
16533   inst.instruction |= (size == 32) << 16;
16534   inst.instruction |= inst.operands[0].reg << 12;
16535   inst.instruction |= HI1 (inst.operands[2].reg) << 7;
16536   inst.instruction |= inst.operands[3].reg;
16537   inst.is_neon = 1;
16538 }
16539
16540 /* Encode insns with bit pattern:
16541
16542   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16543   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
16544
16545   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
16546   different meaning for some instruction.  */
16547
16548 static void
16549 neon_three_same (int isquad, int ubit, int size)
16550 {
16551   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16552   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16553   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16554   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16555   inst.instruction |= LOW4 (inst.operands[2].reg);
16556   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16557   inst.instruction |= (isquad != 0) << 6;
16558   inst.instruction |= (ubit != 0) << 24;
16559   if (size != -1)
16560     inst.instruction |= neon_logbits (size) << 20;
16561
16562   neon_dp_fixup (&inst);
16563 }
16564
16565 /* Encode instructions of the form:
16566
16567   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
16568   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
16569
16570   Don't write size if SIZE == -1.  */
16571
16572 static void
16573 neon_two_same (int qbit, int ubit, int size)
16574 {
16575   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16576   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16577   inst.instruction |= LOW4 (inst.operands[1].reg);
16578   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16579   inst.instruction |= (qbit != 0) << 6;
16580   inst.instruction |= (ubit != 0) << 24;
16581
16582   if (size != -1)
16583     inst.instruction |= neon_logbits (size) << 18;
16584
16585   neon_dp_fixup (&inst);
16586 }
16587
16588 enum vfp_or_neon_is_neon_bits
16589 {
16590 NEON_CHECK_CC = 1,
16591 NEON_CHECK_ARCH = 2,
16592 NEON_CHECK_ARCH8 = 4
16593 };
16594
16595 /* Call this function if an instruction which may have belonged to the VFP or
16596  Neon instruction sets, but turned out to be a Neon instruction (due to the
16597  operand types involved, etc.). We have to check and/or fix-up a couple of
16598  things:
16599
16600    - Make sure the user hasn't attempted to make a Neon instruction
16601      conditional.
16602    - Alter the value in the condition code field if necessary.
16603    - Make sure that the arch supports Neon instructions.
16604
16605  Which of these operations take place depends on bits from enum
16606  vfp_or_neon_is_neon_bits.
16607
16608  WARNING: This function has side effects! If NEON_CHECK_CC is used and the
16609  current instruction's condition is COND_ALWAYS, the condition field is
16610  changed to inst.uncond_value.  This is necessary because instructions shared
16611  between VFP and Neon may be conditional for the VFP variants only, and the
16612  unconditional Neon version must have, e.g., 0xF in the condition field.  */
16613
16614 static int
16615 vfp_or_neon_is_neon (unsigned check)
16616 {
16617 /* Conditions are always legal in Thumb mode (IT blocks).  */
16618 if (!thumb_mode && (check & NEON_CHECK_CC))
16619   {
16620     if (inst.cond != COND_ALWAYS)
16621       {
16622         first_error (_(BAD_COND));
16623         return FAIL;
16624       }
16625     if (inst.uncond_value != -1)
16626       inst.instruction |= inst.uncond_value << 28;
16627   }
16628
16629
16630   if (((check & NEON_CHECK_ARCH) && !mark_feature_used (&fpu_neon_ext_v1))
16631       || ((check & NEON_CHECK_ARCH8)
16632           && !mark_feature_used (&fpu_neon_ext_armv8)))
16633     {
16634       first_error (_(BAD_FPU));
16635       return FAIL;
16636     }
16637
16638 return SUCCESS;
16639 }
16640
16641
16642 /* Return TRUE if the SIMD instruction is available for the current
16643    cpu_variant.  FP is set to TRUE if this is a SIMD floating-point
16644    instruction.  CHECK contains th.  CHECK contains the set of bits to pass to
16645    vfp_or_neon_is_neon for the NEON specific checks.  */
16646
16647 static bfd_boolean
16648 check_simd_pred_availability (int fp, unsigned check)
16649 {
16650 if (inst.cond > COND_ALWAYS)
16651   {
16652     if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16653       {
16654         inst.error = BAD_FPU;
16655         return FALSE;
16656       }
16657     inst.pred_insn_type = INSIDE_VPT_INSN;
16658   }
16659 else if (inst.cond < COND_ALWAYS)
16660   {
16661     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16662       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16663     else if (vfp_or_neon_is_neon (check) == FAIL)
16664       return FALSE;
16665   }
16666 else
16667   {
16668     if (!ARM_CPU_HAS_FEATURE (cpu_variant, fp ? mve_fp_ext : mve_ext)
16669         && vfp_or_neon_is_neon (check) == FAIL)
16670       return FALSE;
16671
16672     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16673       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16674   }
16675 return TRUE;
16676 }
16677
16678 /* Neon instruction encoders, in approximate order of appearance.  */
16679
16680 static void
16681 do_neon_dyadic_i_su (void)
16682 {
16683   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16684    return;
16685
16686   enum neon_shape rs;
16687   struct neon_type_el et;
16688   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16689     rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16690   else
16691     rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16692
16693   et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_32 | N_KEY);
16694
16695
16696   if (rs != NS_QQR)
16697     neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16698   else
16699     mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
16700 }
16701
16702 static void
16703 do_neon_dyadic_i64_su (void)
16704 {
16705   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
16706     return;
16707   enum neon_shape rs;
16708   struct neon_type_el et;
16709   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16710     {
16711       rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
16712       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16713     }
16714   else
16715     {
16716       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16717       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_ALL | N_KEY);
16718     }
16719   if (rs == NS_QQR)
16720     mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
16721   else
16722     neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16723 }
16724
16725 static void
16726 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
16727                 unsigned immbits)
16728 {
16729   unsigned size = et.size >> 3;
16730   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16731   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16732   inst.instruction |= LOW4 (inst.operands[1].reg);
16733   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16734   inst.instruction |= (isquad != 0) << 6;
16735   inst.instruction |= immbits << 16;
16736   inst.instruction |= (size >> 3) << 7;
16737   inst.instruction |= (size & 0x7) << 19;
16738   if (write_ubit)
16739     inst.instruction |= (uval != 0) << 24;
16740
16741   neon_dp_fixup (&inst);
16742 }
16743
16744 static void
16745 do_neon_shl (void)
16746 {
16747   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16748    return;
16749
16750   if (!inst.operands[2].isreg)
16751     {
16752       enum neon_shape rs;
16753       struct neon_type_el et;
16754       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16755         {
16756           rs = neon_select_shape (NS_QQI, NS_NULL);
16757           et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_MVE);
16758         }
16759       else
16760         {
16761           rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16762           et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
16763         }
16764       int imm = inst.operands[2].imm;
16765
16766       constraint (imm < 0 || (unsigned)imm >= et.size,
16767                   _("immediate out of range for shift"));
16768       NEON_ENCODE (IMMED, inst);
16769       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
16770     }
16771   else
16772     {
16773       enum neon_shape rs;
16774       struct neon_type_el et;
16775       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16776         {
16777           rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16778           et = neon_check_type (3, rs, N_EQK, N_SU_MVE | N_KEY, N_EQK | N_EQK);
16779         }
16780       else
16781         {
16782           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16783           et = neon_check_type (3, rs, N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
16784         }
16785
16786
16787       if (rs == NS_QQR)
16788         {
16789           constraint (inst.operands[0].reg != inst.operands[1].reg,
16790                        _("invalid instruction shape"));
16791           if (inst.operands[2].reg == REG_SP)
16792             as_tsktsk (MVE_BAD_SP);
16793           else if (inst.operands[2].reg == REG_PC)
16794             as_tsktsk (MVE_BAD_PC);
16795
16796           inst.instruction = 0xee311e60;
16797           inst.instruction |= (et.type == NT_unsigned) << 28;
16798           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16799           inst.instruction |= neon_logbits (et.size) << 18;
16800           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16801           inst.instruction |= inst.operands[2].reg;
16802           inst.is_neon = 1;
16803         }
16804       else
16805         {
16806           unsigned int tmp;
16807
16808           /* VSHL/VQSHL 3-register variants have syntax such as:
16809                vshl.xx Dd, Dm, Dn
16810              whereas other 3-register operations encoded by neon_three_same have
16811              syntax like:
16812                vadd.xx Dd, Dn, Dm
16813              (i.e. with Dn & Dm reversed). Swap operands[1].reg and
16814              operands[2].reg here.  */
16815           tmp = inst.operands[2].reg;
16816           inst.operands[2].reg = inst.operands[1].reg;
16817           inst.operands[1].reg = tmp;
16818           NEON_ENCODE (INTEGER, inst);
16819           neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16820         }
16821     }
16822 }
16823
16824 static void
16825 do_neon_qshl (void)
16826 {
16827   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16828    return;
16829
16830   if (!inst.operands[2].isreg)
16831     {
16832       enum neon_shape rs;
16833       struct neon_type_el et;
16834       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16835         {
16836           rs = neon_select_shape (NS_QQI, NS_NULL);
16837           et = neon_check_type (2, rs, N_EQK, N_KEY | N_SU_MVE);
16838         }
16839       else
16840         {
16841           rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16842           et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16843         }
16844       int imm = inst.operands[2].imm;
16845
16846       constraint (imm < 0 || (unsigned)imm >= et.size,
16847                   _("immediate out of range for shift"));
16848       NEON_ENCODE (IMMED, inst);
16849       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
16850     }
16851   else
16852     {
16853       enum neon_shape rs;
16854       struct neon_type_el et;
16855
16856       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16857         {
16858           rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16859           et = neon_check_type (3, rs, N_EQK, N_SU_MVE | N_KEY, N_EQK | N_EQK);
16860         }
16861       else
16862         {
16863           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16864           et = neon_check_type (3, rs, N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
16865         }
16866
16867       if (rs == NS_QQR)
16868         {
16869           constraint (inst.operands[0].reg != inst.operands[1].reg,
16870                        _("invalid instruction shape"));
16871           if (inst.operands[2].reg == REG_SP)
16872             as_tsktsk (MVE_BAD_SP);
16873           else if (inst.operands[2].reg == REG_PC)
16874             as_tsktsk (MVE_BAD_PC);
16875
16876           inst.instruction = 0xee311ee0;
16877           inst.instruction |= (et.type == NT_unsigned) << 28;
16878           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16879           inst.instruction |= neon_logbits (et.size) << 18;
16880           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16881           inst.instruction |= inst.operands[2].reg;
16882           inst.is_neon = 1;
16883         }
16884       else
16885         {
16886           unsigned int tmp;
16887
16888           /* See note in do_neon_shl.  */
16889           tmp = inst.operands[2].reg;
16890           inst.operands[2].reg = inst.operands[1].reg;
16891           inst.operands[1].reg = tmp;
16892           NEON_ENCODE (INTEGER, inst);
16893           neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16894         }
16895     }
16896 }
16897
16898 static void
16899 do_neon_rshl (void)
16900 {
16901   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16902    return;
16903
16904   enum neon_shape rs;
16905   struct neon_type_el et;
16906   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16907     {
16908       rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
16909       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16910     }
16911   else
16912     {
16913       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16914       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_ALL | N_KEY);
16915     }
16916
16917   unsigned int tmp;
16918
16919   if (rs == NS_QQR)
16920     {
16921       if (inst.operands[2].reg == REG_PC)
16922         as_tsktsk (MVE_BAD_PC);
16923       else if (inst.operands[2].reg == REG_SP)
16924         as_tsktsk (MVE_BAD_SP);
16925
16926       constraint (inst.operands[0].reg != inst.operands[1].reg,
16927                   _("invalid instruction shape"));
16928
16929       if (inst.instruction == 0x0000510)
16930         /* We are dealing with vqrshl.  */
16931         inst.instruction = 0xee331ee0;
16932       else
16933         /* We are dealing with vrshl.  */
16934         inst.instruction = 0xee331e60;
16935
16936       inst.instruction |= (et.type == NT_unsigned) << 28;
16937       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16938       inst.instruction |= neon_logbits (et.size) << 18;
16939       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16940       inst.instruction |= inst.operands[2].reg;
16941       inst.is_neon = 1;
16942     }
16943   else
16944     {
16945       tmp = inst.operands[2].reg;
16946       inst.operands[2].reg = inst.operands[1].reg;
16947       inst.operands[1].reg = tmp;
16948       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16949     }
16950 }
16951
16952 static int
16953 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
16954 {
16955   /* Handle .I8 pseudo-instructions.  */
16956   if (size == 8)
16957     {
16958       /* Unfortunately, this will make everything apart from zero out-of-range.
16959          FIXME is this the intended semantics? There doesn't seem much point in
16960          accepting .I8 if so.  */
16961       immediate |= immediate << 8;
16962       size = 16;
16963     }
16964
16965   if (size >= 32)
16966     {
16967       if (immediate == (immediate & 0x000000ff))
16968         {
16969           *immbits = immediate;
16970           return 0x1;
16971         }
16972       else if (immediate == (immediate & 0x0000ff00))
16973         {
16974           *immbits = immediate >> 8;
16975           return 0x3;
16976         }
16977       else if (immediate == (immediate & 0x00ff0000))
16978         {
16979           *immbits = immediate >> 16;
16980           return 0x5;
16981         }
16982       else if (immediate == (immediate & 0xff000000))
16983         {
16984           *immbits = immediate >> 24;
16985           return 0x7;
16986         }
16987       if ((immediate & 0xffff) != (immediate >> 16))
16988         goto bad_immediate;
16989       immediate &= 0xffff;
16990     }
16991
16992   if (immediate == (immediate & 0x000000ff))
16993     {
16994       *immbits = immediate;
16995       return 0x9;
16996     }
16997   else if (immediate == (immediate & 0x0000ff00))
16998     {
16999       *immbits = immediate >> 8;
17000       return 0xb;
17001     }
17002
17003   bad_immediate:
17004   first_error (_("immediate value out of range"));
17005   return FAIL;
17006 }
17007
17008 static void
17009 do_neon_logic (void)
17010 {
17011   if (inst.operands[2].present && inst.operands[2].isreg)
17012     {
17013       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17014       if (rs == NS_QQQ
17015           && !check_simd_pred_availability (FALSE,
17016                                             NEON_CHECK_ARCH | NEON_CHECK_CC))
17017         return;
17018       else if (rs != NS_QQQ
17019                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
17020         first_error (BAD_FPU);
17021
17022       neon_check_type (3, rs, N_IGNORE_TYPE);
17023       /* U bit and size field were set as part of the bitmask.  */
17024       NEON_ENCODE (INTEGER, inst);
17025       neon_three_same (neon_quad (rs), 0, -1);
17026     }
17027   else
17028     {
17029       const int three_ops_form = (inst.operands[2].present
17030                                   && !inst.operands[2].isreg);
17031       const int immoperand = (three_ops_form ? 2 : 1);
17032       enum neon_shape rs = (three_ops_form
17033                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
17034                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
17035       /* Because neon_select_shape makes the second operand a copy of the first
17036          if the second operand is not present.  */
17037       if (rs == NS_QQI
17038           && !check_simd_pred_availability (FALSE,
17039                                             NEON_CHECK_ARCH | NEON_CHECK_CC))
17040         return;
17041       else if (rs != NS_QQI
17042                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
17043         first_error (BAD_FPU);
17044
17045       struct neon_type_el et;
17046       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17047         et = neon_check_type (2, rs, N_I32 | N_I16 | N_KEY, N_EQK);
17048       else
17049         et = neon_check_type (2, rs, N_I8 | N_I16 | N_I32 | N_I64 | N_F32
17050                               | N_KEY, N_EQK);
17051
17052       if (et.type == NT_invtype)
17053         return;
17054       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
17055       unsigned immbits;
17056       int cmode;
17057
17058
17059       if (three_ops_form)
17060         constraint (inst.operands[0].reg != inst.operands[1].reg,
17061                     _("first and second operands shall be the same register"));
17062
17063       NEON_ENCODE (IMMED, inst);
17064
17065       immbits = inst.operands[immoperand].imm;
17066       if (et.size == 64)
17067         {
17068           /* .i64 is a pseudo-op, so the immediate must be a repeating
17069              pattern.  */
17070           if (immbits != (inst.operands[immoperand].regisimm ?
17071                           inst.operands[immoperand].reg : 0))
17072             {
17073               /* Set immbits to an invalid constant.  */
17074               immbits = 0xdeadbeef;
17075             }
17076         }
17077
17078       switch (opcode)
17079         {
17080         case N_MNEM_vbic:
17081           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17082           break;
17083
17084         case N_MNEM_vorr:
17085           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17086           break;
17087
17088         case N_MNEM_vand:
17089           /* Pseudo-instruction for VBIC.  */
17090           neon_invert_size (&immbits, 0, et.size);
17091           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17092           break;
17093
17094         case N_MNEM_vorn:
17095           /* Pseudo-instruction for VORR.  */
17096           neon_invert_size (&immbits, 0, et.size);
17097           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17098           break;
17099
17100         default:
17101           abort ();
17102         }
17103
17104       if (cmode == FAIL)
17105         return;
17106
17107       inst.instruction |= neon_quad (rs) << 6;
17108       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17109       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17110       inst.instruction |= cmode << 8;
17111       neon_write_immbits (immbits);
17112
17113       neon_dp_fixup (&inst);
17114     }
17115 }
17116
17117 static void
17118 do_neon_bitfield (void)
17119 {
17120   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17121   neon_check_type (3, rs, N_IGNORE_TYPE);
17122   neon_three_same (neon_quad (rs), 0, -1);
17123 }
17124
17125 static void
17126 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
17127                   unsigned destbits)
17128 {
17129   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17130   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
17131                                             types | N_KEY);
17132   if (et.type == NT_float)
17133     {
17134       NEON_ENCODE (FLOAT, inst);
17135       if (rs == NS_QQR)
17136         mve_encode_qqr (et.size, 0, 1);
17137       else
17138         neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
17139     }
17140   else
17141     {
17142       NEON_ENCODE (INTEGER, inst);
17143       if (rs == NS_QQR)
17144         mve_encode_qqr (et.size, et.type == ubit_meaning, 0);
17145       else
17146         neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
17147     }
17148 }
17149
17150
17151 static void
17152 do_neon_dyadic_if_su_d (void)
17153 {
17154   /* This version only allow D registers, but that constraint is enforced during
17155      operand parsing so we don't need to do anything extra here.  */
17156   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
17157 }
17158
17159 static void
17160 do_neon_dyadic_if_i_d (void)
17161 {
17162   /* The "untyped" case can't happen. Do this to stop the "U" bit being
17163      affected if we specify unsigned args.  */
17164   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17165 }
17166
17167 static void
17168 do_mve_vstr_vldr_QI (int size, int elsize, int load)
17169 {
17170   constraint (size < 32, BAD_ADDR_MODE);
17171   constraint (size != elsize, BAD_EL_TYPE);
17172   constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17173   constraint (!inst.operands[1].preind, BAD_ADDR_MODE);
17174   constraint (load && inst.operands[0].reg == inst.operands[1].reg,
17175               _("destination register and offset register may not be the"
17176                 " same"));
17177
17178   int imm = inst.relocs[0].exp.X_add_number;
17179   int add = 1;
17180   if (imm < 0)
17181     {
17182       add = 0;
17183       imm = -imm;
17184     }
17185   constraint ((imm % (size / 8) != 0)
17186               || imm > (0x7f << neon_logbits (size)),
17187               (size == 32) ? _("immediate must be a multiple of 4 in the"
17188                                " range of +/-[0,508]")
17189                            : _("immediate must be a multiple of 8 in the"
17190                                " range of +/-[0,1016]"));
17191   inst.instruction |= 0x11 << 24;
17192   inst.instruction |= add << 23;
17193   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17194   inst.instruction |= inst.operands[1].writeback << 21;
17195   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17196   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17197   inst.instruction |= 1 << 12;
17198   inst.instruction |= (size == 64) << 8;
17199   inst.instruction &= 0xffffff00;
17200   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17201   inst.instruction |= imm >> neon_logbits (size);
17202 }
17203
17204 static void
17205 do_mve_vstr_vldr_RQ (int size, int elsize, int load)
17206 {
17207     unsigned os = inst.operands[1].imm >> 5;
17208     constraint (os != 0 && size == 8,
17209                 _("can not shift offsets when accessing less than half-word"));
17210     constraint (os && os != neon_logbits (size),
17211                 _("shift immediate must be 1, 2 or 3 for half-word, word"
17212                   " or double-word accesses respectively"));
17213     if (inst.operands[1].reg == REG_PC)
17214       as_tsktsk (MVE_BAD_PC);
17215
17216     switch (size)
17217       {
17218       case 8:
17219         constraint (elsize >= 64, BAD_EL_TYPE);
17220         break;
17221       case 16:
17222         constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
17223         break;
17224       case 32:
17225       case 64:
17226         constraint (elsize != size, BAD_EL_TYPE);
17227         break;
17228       default:
17229         break;
17230       }
17231     constraint (inst.operands[1].writeback || !inst.operands[1].preind,
17232                 BAD_ADDR_MODE);
17233     if (load)
17234       {
17235         constraint (inst.operands[0].reg == (inst.operands[1].imm & 0x1f),
17236                     _("destination register and offset register may not be"
17237                     " the same"));
17238         constraint (size == elsize && inst.vectype.el[0].type != NT_unsigned,
17239                     BAD_EL_TYPE);
17240         constraint (inst.vectype.el[0].type != NT_unsigned
17241                     && inst.vectype.el[0].type != NT_signed, BAD_EL_TYPE);
17242         inst.instruction |= (inst.vectype.el[0].type == NT_unsigned) << 28;
17243       }
17244     else
17245       {
17246         constraint (inst.vectype.el[0].type != NT_untyped, BAD_EL_TYPE);
17247       }
17248
17249     inst.instruction |= 1 << 23;
17250     inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17251     inst.instruction |= inst.operands[1].reg << 16;
17252     inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17253     inst.instruction |= neon_logbits (elsize) << 7;
17254     inst.instruction |= HI1 (inst.operands[1].imm) << 5;
17255     inst.instruction |= LOW4 (inst.operands[1].imm);
17256     inst.instruction |= !!os;
17257 }
17258
17259 static void
17260 do_mve_vstr_vldr_RI (int size, int elsize, int load)
17261 {
17262   enum neon_el_type type = inst.vectype.el[0].type;
17263
17264   constraint (size >= 64, BAD_ADDR_MODE);
17265   switch (size)
17266     {
17267     case 16:
17268       constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
17269       break;
17270     case 32:
17271       constraint (elsize != size, BAD_EL_TYPE);
17272       break;
17273     default:
17274       break;
17275     }
17276   if (load)
17277     {
17278       constraint (elsize != size && type != NT_unsigned
17279                   && type != NT_signed, BAD_EL_TYPE);
17280     }
17281   else
17282     {
17283       constraint (elsize != size && type != NT_untyped, BAD_EL_TYPE);
17284     }
17285
17286   int imm = inst.relocs[0].exp.X_add_number;
17287   int add = 1;
17288   if (imm < 0)
17289     {
17290       add = 0;
17291       imm = -imm;
17292     }
17293
17294   if ((imm % (size / 8) != 0) || imm > (0x7f << neon_logbits (size)))
17295     {
17296       switch (size)
17297         {
17298         case 8:
17299           constraint (1, _("immediate must be in the range of +/-[0,127]"));
17300           break;
17301         case 16:
17302           constraint (1, _("immediate must be a multiple of 2 in the"
17303                            " range of +/-[0,254]"));
17304           break;
17305         case 32:
17306           constraint (1, _("immediate must be a multiple of 4 in the"
17307                            " range of +/-[0,508]"));
17308           break;
17309         }
17310     }
17311
17312   if (size != elsize)
17313     {
17314       constraint (inst.operands[1].reg > 7, BAD_HIREG);
17315       constraint (inst.operands[0].reg > 14,
17316                   _("MVE vector register in the range [Q0..Q7] expected"));
17317       inst.instruction |= (load && type == NT_unsigned) << 28;
17318       inst.instruction |= (size == 16) << 19;
17319       inst.instruction |= neon_logbits (elsize) << 7;
17320     }
17321   else
17322     {
17323       if (inst.operands[1].reg == REG_PC)
17324         as_tsktsk (MVE_BAD_PC);
17325       else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
17326         as_tsktsk (MVE_BAD_SP);
17327       inst.instruction |= 1 << 12;
17328       inst.instruction |= neon_logbits (size) << 7;
17329     }
17330   inst.instruction |= inst.operands[1].preind << 24;
17331   inst.instruction |= add << 23;
17332   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17333   inst.instruction |= inst.operands[1].writeback << 21;
17334   inst.instruction |= inst.operands[1].reg << 16;
17335   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17336   inst.instruction &= 0xffffff80;
17337   inst.instruction |= imm >> neon_logbits (size);
17338
17339 }
17340
17341 static void
17342 do_mve_vstr_vldr (void)
17343 {
17344   unsigned size;
17345   int load = 0;
17346
17347   if (inst.cond > COND_ALWAYS)
17348     inst.pred_insn_type = INSIDE_VPT_INSN;
17349   else
17350     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17351
17352   switch (inst.instruction)
17353     {
17354     default:
17355       gas_assert (0);
17356       break;
17357     case M_MNEM_vldrb:
17358       load = 1;
17359       /* fall through.  */
17360     case M_MNEM_vstrb:
17361       size = 8;
17362       break;
17363     case M_MNEM_vldrh:
17364       load = 1;
17365       /* fall through.  */
17366     case M_MNEM_vstrh:
17367       size = 16;
17368       break;
17369     case M_MNEM_vldrw:
17370       load = 1;
17371       /* fall through.  */
17372     case M_MNEM_vstrw:
17373       size = 32;
17374       break;
17375     case M_MNEM_vldrd:
17376       load = 1;
17377       /* fall through.  */
17378     case M_MNEM_vstrd:
17379       size = 64;
17380       break;
17381     }
17382   unsigned elsize = inst.vectype.el[0].size;
17383
17384   if (inst.operands[1].isquad)
17385     {
17386       /* We are dealing with [Q, imm]{!} cases.  */
17387       do_mve_vstr_vldr_QI (size, elsize, load);
17388     }
17389   else
17390     {
17391       if (inst.operands[1].immisreg == 2)
17392         {
17393           /* We are dealing with [R, Q, {UXTW #os}] cases.  */
17394           do_mve_vstr_vldr_RQ (size, elsize, load);
17395         }
17396       else if (!inst.operands[1].immisreg)
17397         {
17398           /* We are dealing with [R, Imm]{!}/[R], Imm cases.  */
17399           do_mve_vstr_vldr_RI (size, elsize, load);
17400         }
17401       else
17402         constraint (1, BAD_ADDR_MODE);
17403     }
17404
17405   inst.is_neon = 1;
17406 }
17407
17408 static void
17409 do_mve_vst_vld (void)
17410 {
17411   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17412     return;
17413
17414   constraint (!inst.operands[1].preind || inst.relocs[0].exp.X_add_symbol != 0
17415               || inst.relocs[0].exp.X_add_number != 0
17416               || inst.operands[1].immisreg != 0,
17417               BAD_ADDR_MODE);
17418   constraint (inst.vectype.el[0].size > 32, BAD_EL_TYPE);
17419   if (inst.operands[1].reg == REG_PC)
17420     as_tsktsk (MVE_BAD_PC);
17421   else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
17422     as_tsktsk (MVE_BAD_SP);
17423
17424
17425   /* These instructions are one of the "exceptions" mentioned in
17426      handle_pred_state.  They are MVE instructions that are not VPT compatible
17427      and do not accept a VPT code, thus appending such a code is a syntax
17428      error.  */
17429   if (inst.cond > COND_ALWAYS)
17430     first_error (BAD_SYNTAX);
17431   /* If we append a scalar condition code we can set this to
17432      MVE_OUTSIDE_PRED_INSN as it will also lead to a syntax error.  */
17433   else if (inst.cond < COND_ALWAYS)
17434     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17435   else
17436     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
17437
17438   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17439   inst.instruction |= inst.operands[1].writeback << 21;
17440   inst.instruction |= inst.operands[1].reg << 16;
17441   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17442   inst.instruction |= neon_logbits (inst.vectype.el[0].size) << 7;
17443   inst.is_neon = 1;
17444 }
17445
17446 static void
17447 do_mve_vaddlv (void)
17448 {
17449   enum neon_shape rs = neon_select_shape (NS_RRQ, NS_NULL);
17450   struct neon_type_el et
17451     = neon_check_type (3, rs, N_EQK, N_EQK, N_S32 | N_U32 | N_KEY);
17452
17453   if (et.type == NT_invtype)
17454     first_error (BAD_EL_TYPE);
17455
17456   if (inst.cond > COND_ALWAYS)
17457     inst.pred_insn_type = INSIDE_VPT_INSN;
17458   else
17459     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17460
17461   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
17462
17463   inst.instruction |= (et.type == NT_unsigned) << 28;
17464   inst.instruction |= inst.operands[1].reg << 19;
17465   inst.instruction |= inst.operands[0].reg << 12;
17466   inst.instruction |= inst.operands[2].reg;
17467   inst.is_neon = 1;
17468 }
17469
17470 static void
17471 do_neon_dyadic_if_su (void)
17472 {
17473   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17474   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
17475                                             N_SUF_32 | N_KEY);
17476
17477   constraint ((inst.instruction == ((unsigned) N_MNEM_vmax)
17478                || inst.instruction == ((unsigned) N_MNEM_vmin))
17479               && et.type == NT_float
17480               && !ARM_CPU_HAS_FEATURE (cpu_variant,fpu_neon_ext_v1), BAD_FPU);
17481
17482   if (!check_simd_pred_availability (et.type == NT_float,
17483                                      NEON_CHECK_ARCH | NEON_CHECK_CC))
17484     return;
17485
17486   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
17487 }
17488
17489 static void
17490 do_neon_addsub_if_i (void)
17491 {
17492   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
17493       && try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
17494     return;
17495
17496   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17497   struct neon_type_el et = neon_check_type (3, rs, N_EQK,
17498                                             N_EQK, N_IF_32 | N_I64 | N_KEY);
17499
17500   constraint (rs == NS_QQR && et.size == 64, BAD_FPU);
17501   /* If we are parsing Q registers and the element types match MVE, which NEON
17502      also supports, then we must check whether this is an instruction that can
17503      be used by both MVE/NEON.  This distinction can be made based on whether
17504      they are predicated or not.  */
17505   if ((rs == NS_QQQ || rs == NS_QQR) && et.size != 64)
17506     {
17507       if (!check_simd_pred_availability (et.type == NT_float,
17508                                          NEON_CHECK_ARCH | NEON_CHECK_CC))
17509         return;
17510     }
17511   else
17512     {
17513       /* If they are either in a D register or are using an unsupported.  */
17514       if (rs != NS_QQR
17515           && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17516         return;
17517     }
17518
17519   /* The "untyped" case can't happen. Do this to stop the "U" bit being
17520      affected if we specify unsigned args.  */
17521   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
17522 }
17523
17524 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
17525    result to be:
17526      V<op> A,B     (A is operand 0, B is operand 2)
17527    to mean:
17528      V<op> A,B,A
17529    not:
17530      V<op> A,B,B
17531    so handle that case specially.  */
17532
17533 static void
17534 neon_exchange_operands (void)
17535 {
17536   if (inst.operands[1].present)
17537     {
17538       void *scratch = xmalloc (sizeof (inst.operands[0]));
17539
17540       /* Swap operands[1] and operands[2].  */
17541       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
17542       inst.operands[1] = inst.operands[2];
17543       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
17544       free (scratch);
17545     }
17546   else
17547     {
17548       inst.operands[1] = inst.operands[2];
17549       inst.operands[2] = inst.operands[0];
17550     }
17551 }
17552
17553 static void
17554 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
17555 {
17556   if (inst.operands[2].isreg)
17557     {
17558       if (invert)
17559         neon_exchange_operands ();
17560       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
17561     }
17562   else
17563     {
17564       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17565       struct neon_type_el et = neon_check_type (2, rs,
17566         N_EQK | N_SIZ, immtypes | N_KEY);
17567
17568       NEON_ENCODE (IMMED, inst);
17569       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17570       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17571       inst.instruction |= LOW4 (inst.operands[1].reg);
17572       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17573       inst.instruction |= neon_quad (rs) << 6;
17574       inst.instruction |= (et.type == NT_float) << 10;
17575       inst.instruction |= neon_logbits (et.size) << 18;
17576
17577       neon_dp_fixup (&inst);
17578     }
17579 }
17580
17581 static void
17582 do_neon_cmp (void)
17583 {
17584   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
17585 }
17586
17587 static void
17588 do_neon_cmp_inv (void)
17589 {
17590   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
17591 }
17592
17593 static void
17594 do_neon_ceq (void)
17595 {
17596   neon_compare (N_IF_32, N_IF_32, FALSE);
17597 }
17598
17599 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
17600    scalars, which are encoded in 5 bits, M : Rm.
17601    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
17602    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
17603    index in M.
17604
17605    Dot Product instructions are similar to multiply instructions except elsize
17606    should always be 32.
17607
17608    This function translates SCALAR, which is GAS's internal encoding of indexed
17609    scalar register, to raw encoding.  There is also register and index range
17610    check based on ELSIZE.  */
17611
17612 static unsigned
17613 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
17614 {
17615   unsigned regno = NEON_SCALAR_REG (scalar);
17616   unsigned elno = NEON_SCALAR_INDEX (scalar);
17617
17618   switch (elsize)
17619     {
17620     case 16:
17621       if (regno > 7 || elno > 3)
17622         goto bad_scalar;
17623       return regno | (elno << 3);
17624
17625     case 32:
17626       if (regno > 15 || elno > 1)
17627         goto bad_scalar;
17628       return regno | (elno << 4);
17629
17630     default:
17631     bad_scalar:
17632       first_error (_("scalar out of range for multiply instruction"));
17633     }
17634
17635   return 0;
17636 }
17637
17638 /* Encode multiply / multiply-accumulate scalar instructions.  */
17639
17640 static void
17641 neon_mul_mac (struct neon_type_el et, int ubit)
17642 {
17643   unsigned scalar;
17644
17645   /* Give a more helpful error message if we have an invalid type.  */
17646   if (et.type == NT_invtype)
17647     return;
17648
17649   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
17650   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17651   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17652   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17653   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17654   inst.instruction |= LOW4 (scalar);
17655   inst.instruction |= HI1 (scalar) << 5;
17656   inst.instruction |= (et.type == NT_float) << 8;
17657   inst.instruction |= neon_logbits (et.size) << 20;
17658   inst.instruction |= (ubit != 0) << 24;
17659
17660   neon_dp_fixup (&inst);
17661 }
17662
17663 static void
17664 do_neon_mac_maybe_scalar (void)
17665 {
17666   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
17667     return;
17668
17669   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17670     return;
17671
17672   if (inst.operands[2].isscalar)
17673     {
17674       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17675       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17676       struct neon_type_el et = neon_check_type (3, rs,
17677         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
17678       NEON_ENCODE (SCALAR, inst);
17679       neon_mul_mac (et, neon_quad (rs));
17680     }
17681   else if (!inst.operands[2].isvec)
17682     {
17683       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17684
17685       enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17686       neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
17687
17688       neon_dyadic_misc (NT_unsigned, N_SU_MVE, 0);
17689     }
17690   else
17691     {
17692       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17693       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
17694          affected if we specify unsigned args.  */
17695       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17696     }
17697 }
17698
17699 static void
17700 do_neon_fmac (void)
17701 {
17702   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_fma)
17703       && try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
17704     return;
17705
17706   if (!check_simd_pred_availability (TRUE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17707     return;
17708
17709   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
17710     {
17711       enum neon_shape rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
17712       struct neon_type_el et = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK,
17713                                                 N_EQK);
17714
17715       if (rs == NS_QQR)
17716         {
17717           if (inst.operands[2].reg == REG_SP)
17718             as_tsktsk (MVE_BAD_SP);
17719           else if (inst.operands[2].reg == REG_PC)
17720             as_tsktsk (MVE_BAD_PC);
17721
17722           inst.instruction = 0xee310e40;
17723           inst.instruction |= (et.size == 16) << 28;
17724           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17725           inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17726           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17727           inst.instruction |= HI1 (inst.operands[1].reg) << 6;
17728           inst.instruction |= inst.operands[2].reg;
17729           inst.is_neon = 1;
17730           return;
17731         }
17732     }
17733   else
17734     {
17735       constraint (!inst.operands[2].isvec, BAD_FPU);
17736     }
17737
17738   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17739 }
17740
17741 static void
17742 do_neon_tst (void)
17743 {
17744   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17745   struct neon_type_el et = neon_check_type (3, rs,
17746     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
17747   neon_three_same (neon_quad (rs), 0, et.size);
17748 }
17749
17750 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
17751    same types as the MAC equivalents. The polynomial type for this instruction
17752    is encoded the same as the integer type.  */
17753
17754 static void
17755 do_neon_mul (void)
17756 {
17757   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
17758     return;
17759
17760   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17761     return;
17762
17763   if (inst.operands[2].isscalar)
17764     {
17765       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17766       do_neon_mac_maybe_scalar ();
17767     }
17768   else
17769     {
17770       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17771         {
17772           enum neon_shape rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
17773           struct neon_type_el et
17774             = neon_check_type (3, rs, N_EQK, N_EQK, N_I_MVE | N_F_MVE | N_KEY);
17775           if (et.type == NT_float)
17776             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
17777                         BAD_FPU);
17778
17779           neon_dyadic_misc (NT_float, N_I_MVE | N_F_MVE, 0);
17780         }
17781       else
17782         {
17783           constraint (!inst.operands[2].isvec, BAD_FPU);
17784           neon_dyadic_misc (NT_poly,
17785                             N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
17786         }
17787     }
17788 }
17789
17790 static void
17791 do_neon_qdmulh (void)
17792 {
17793   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
17794    return;
17795
17796   if (inst.operands[2].isscalar)
17797     {
17798       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17799       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17800       struct neon_type_el et = neon_check_type (3, rs,
17801         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17802       NEON_ENCODE (SCALAR, inst);
17803       neon_mul_mac (et, neon_quad (rs));
17804     }
17805   else
17806     {
17807       enum neon_shape rs;
17808       struct neon_type_el et;
17809       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17810         {
17811           rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
17812           et = neon_check_type (3, rs,
17813             N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17814         }
17815       else
17816         {
17817           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17818           et = neon_check_type (3, rs,
17819             N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17820         }
17821
17822       NEON_ENCODE (INTEGER, inst);
17823       if (rs == NS_QQR)
17824         mve_encode_qqr (et.size, 0, 0);
17825       else
17826         /* The U bit (rounding) comes from bit mask.  */
17827         neon_three_same (neon_quad (rs), 0, et.size);
17828     }
17829 }
17830
17831 static void
17832 do_mve_vaddv (void)
17833 {
17834   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
17835   struct neon_type_el et
17836     = neon_check_type (2, rs, N_EQK,  N_SU_32 | N_KEY);
17837
17838   if (et.type == NT_invtype)
17839     first_error (BAD_EL_TYPE);
17840
17841   if (inst.cond > COND_ALWAYS)
17842     inst.pred_insn_type = INSIDE_VPT_INSN;
17843   else
17844     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17845
17846   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
17847
17848   mve_encode_rq (et.type == NT_unsigned, et.size);
17849 }
17850
17851 static void
17852 do_mve_vhcadd (void)
17853 {
17854   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
17855   struct neon_type_el et
17856     = neon_check_type (3, rs, N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17857
17858   if (inst.cond > COND_ALWAYS)
17859     inst.pred_insn_type = INSIDE_VPT_INSN;
17860   else
17861     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17862
17863   unsigned rot = inst.relocs[0].exp.X_add_number;
17864   constraint (rot != 90 && rot != 270, _("immediate out of range"));
17865
17866   if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
17867     as_tsktsk (_("Warning: 32-bit element size and same first and third "
17868                  "operand makes instruction UNPREDICTABLE"));
17869
17870   mve_encode_qqq (0, et.size);
17871   inst.instruction |= (rot == 270) << 12;
17872   inst.is_neon = 1;
17873 }
17874
17875 static void
17876 do_mve_vqdmull (void)
17877 {
17878   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
17879   struct neon_type_el et
17880     = neon_check_type (3, rs, N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17881
17882   if (et.size == 32
17883       && (inst.operands[0].reg == inst.operands[1].reg
17884           || (rs == NS_QQQ && inst.operands[0].reg == inst.operands[2].reg)))
17885     as_tsktsk (BAD_MVE_SRCDEST);
17886
17887   if (inst.cond > COND_ALWAYS)
17888     inst.pred_insn_type = INSIDE_VPT_INSN;
17889   else
17890     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17891
17892   if (rs == NS_QQQ)
17893     {
17894       mve_encode_qqq (et.size == 32, 64);
17895       inst.instruction |= 1;
17896     }
17897   else
17898     {
17899       mve_encode_qqr (64, et.size == 32, 0);
17900       inst.instruction |= 0x3 << 5;
17901     }
17902 }
17903
17904 static void
17905 do_mve_vadc (void)
17906 {
17907   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17908   struct neon_type_el et
17909     = neon_check_type (3, rs, N_KEY | N_I32, N_EQK, N_EQK);
17910
17911   if (et.type == NT_invtype)
17912     first_error (BAD_EL_TYPE);
17913
17914   if (inst.cond > COND_ALWAYS)
17915     inst.pred_insn_type = INSIDE_VPT_INSN;
17916   else
17917     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17918
17919   mve_encode_qqq (0, 64);
17920 }
17921
17922 static void
17923 do_mve_vbrsr (void)
17924 {
17925   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17926   struct neon_type_el et
17927     = neon_check_type (3, rs, N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
17928
17929   if (inst.cond > COND_ALWAYS)
17930     inst.pred_insn_type = INSIDE_VPT_INSN;
17931   else
17932     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17933
17934   mve_encode_qqr (et.size, 0, 0);
17935 }
17936
17937 static void
17938 do_mve_vsbc (void)
17939 {
17940   neon_check_type (3, NS_QQQ, N_EQK, N_EQK, N_I32 | N_KEY);
17941
17942   if (inst.cond > COND_ALWAYS)
17943     inst.pred_insn_type = INSIDE_VPT_INSN;
17944   else
17945     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17946
17947   mve_encode_qqq (1, 64);
17948 }
17949
17950 static void
17951 do_mve_vmulh (void)
17952 {
17953   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17954   struct neon_type_el et
17955     = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
17956
17957   if (inst.cond > COND_ALWAYS)
17958     inst.pred_insn_type = INSIDE_VPT_INSN;
17959   else
17960     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17961
17962   mve_encode_qqq (et.type == NT_unsigned, et.size);
17963 }
17964
17965 static void
17966 do_mve_vqdmlah (void)
17967 {
17968   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17969   struct neon_type_el et
17970     = neon_check_type (3, rs, N_EQK, N_EQK, N_S_32 | N_KEY);
17971
17972   if (inst.cond > COND_ALWAYS)
17973     inst.pred_insn_type = INSIDE_VPT_INSN;
17974   else
17975     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17976
17977   mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
17978 }
17979
17980 static void
17981 do_mve_vqdmladh (void)
17982 {
17983   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17984   struct neon_type_el et
17985     = neon_check_type (3, rs, N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17986
17987   if (inst.cond > COND_ALWAYS)
17988     inst.pred_insn_type = INSIDE_VPT_INSN;
17989   else
17990     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17991
17992   mve_encode_qqq (0, et.size);
17993 }
17994
17995
17996 static void
17997 do_mve_vmull (void)
17998 {
17999
18000   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_DDS,
18001                                           NS_QQS, NS_QQQ, NS_QQR, NS_NULL);
18002   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
18003       && inst.cond == COND_ALWAYS
18004       && ((unsigned)inst.instruction) == M_MNEM_vmullt)
18005     {
18006       if (rs == NS_QQQ)
18007         {
18008
18009           struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
18010                                                     N_SUF_32 | N_F64 | N_P8
18011                                                     | N_P16 | N_I_MVE | N_KEY);
18012           if (((et.type == NT_poly) && et.size == 8
18013                && ARM_CPU_IS_ANY (cpu_variant))
18014               || (et.type == NT_integer) || (et.type == NT_float))
18015             goto neon_vmul;
18016         }
18017       else
18018         goto neon_vmul;
18019     }
18020
18021   constraint (rs != NS_QQQ, BAD_FPU);
18022   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
18023                                             N_SU_32 | N_P8 | N_P16 | N_KEY);
18024
18025   /* We are dealing with MVE's vmullt.  */
18026   if (et.size == 32
18027       && (inst.operands[0].reg == inst.operands[1].reg
18028           || inst.operands[0].reg == inst.operands[2].reg))
18029     as_tsktsk (BAD_MVE_SRCDEST);
18030
18031   if (inst.cond > COND_ALWAYS)
18032     inst.pred_insn_type = INSIDE_VPT_INSN;
18033   else
18034     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18035
18036   if (et.type == NT_poly)
18037     mve_encode_qqq (neon_logbits (et.size), 64);
18038   else
18039     mve_encode_qqq (et.type == NT_unsigned, et.size);
18040
18041   return;
18042
18043 neon_vmul:
18044   inst.instruction = N_MNEM_vmul;
18045   inst.cond = 0xb;
18046   if (thumb_mode)
18047     inst.pred_insn_type = INSIDE_IT_INSN;
18048   do_neon_mul ();
18049 }
18050
18051 static void
18052 do_mve_vabav (void)
18053 {
18054   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
18055
18056   if (rs == NS_NULL)
18057     return;
18058
18059   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18060     return;
18061
18062   struct neon_type_el et = neon_check_type (2, NS_NULL, N_EQK, N_KEY | N_S8
18063                                             | N_S16 | N_S32 | N_U8 | N_U16
18064                                             | N_U32);
18065
18066   if (inst.cond > COND_ALWAYS)
18067     inst.pred_insn_type = INSIDE_VPT_INSN;
18068   else
18069     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18070
18071   mve_encode_rqq (et.type == NT_unsigned, et.size);
18072 }
18073
18074 static void
18075 do_mve_vmladav (void)
18076 {
18077   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
18078   struct neon_type_el et = neon_check_type (3, rs,
18079                                             N_EQK, N_EQK, N_SU_MVE | N_KEY);
18080
18081   if (et.type == NT_unsigned
18082       && (inst.instruction == M_MNEM_vmladavx
18083           || inst.instruction == M_MNEM_vmladavax
18084           || inst.instruction == M_MNEM_vmlsdav
18085           || inst.instruction == M_MNEM_vmlsdava
18086           || inst.instruction == M_MNEM_vmlsdavx
18087           || inst.instruction == M_MNEM_vmlsdavax))
18088     first_error (BAD_SIMD_TYPE);
18089
18090   constraint (inst.operands[2].reg > 14,
18091               _("MVE vector register in the range [Q0..Q7] expected"));
18092
18093   if (inst.cond > COND_ALWAYS)
18094     inst.pred_insn_type = INSIDE_VPT_INSN;
18095   else
18096     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18097
18098   if (inst.instruction == M_MNEM_vmlsdav
18099       || inst.instruction == M_MNEM_vmlsdava
18100       || inst.instruction == M_MNEM_vmlsdavx
18101       || inst.instruction == M_MNEM_vmlsdavax)
18102     inst.instruction |= (et.size == 8) << 28;
18103   else
18104     inst.instruction |= (et.size == 8) << 8;
18105
18106   mve_encode_rqq (et.type == NT_unsigned, 64);
18107   inst.instruction |= (et.size == 32) << 16;
18108 }
18109
18110 static void
18111 do_mve_vmlaldav (void)
18112 {
18113   enum neon_shape rs = neon_select_shape (NS_RRQQ, NS_NULL);
18114   struct neon_type_el et
18115     = neon_check_type (4, rs, N_EQK, N_EQK, N_EQK,
18116                        N_S16 | N_S32 | N_U16 | N_U32 | N_KEY);
18117
18118   if (et.type == NT_unsigned
18119       && (inst.instruction == M_MNEM_vmlsldav
18120           || inst.instruction == M_MNEM_vmlsldava
18121           || inst.instruction == M_MNEM_vmlsldavx
18122           || inst.instruction == M_MNEM_vmlsldavax))
18123     first_error (BAD_SIMD_TYPE);
18124
18125   if (inst.cond > COND_ALWAYS)
18126     inst.pred_insn_type = INSIDE_VPT_INSN;
18127   else
18128     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18129
18130   mve_encode_rrqq (et.type == NT_unsigned, et.size);
18131 }
18132
18133 static void
18134 do_mve_vrmlaldavh (void)
18135 {
18136   struct neon_type_el et;
18137   if (inst.instruction == M_MNEM_vrmlsldavh
18138      || inst.instruction == M_MNEM_vrmlsldavha
18139      || inst.instruction == M_MNEM_vrmlsldavhx
18140      || inst.instruction == M_MNEM_vrmlsldavhax)
18141     {
18142       et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK, N_S32 | N_KEY);
18143       if (inst.operands[1].reg == REG_SP)
18144         as_tsktsk (MVE_BAD_SP);
18145     }
18146   else
18147     {
18148       if (inst.instruction == M_MNEM_vrmlaldavhx
18149           || inst.instruction == M_MNEM_vrmlaldavhax)
18150         et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK, N_S32 | N_KEY);
18151       else
18152         et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK,
18153                               N_U32 | N_S32 | N_KEY);
18154       /* vrmlaldavh's encoding with SP as the second, odd, GPR operand may alias
18155          with vmax/min instructions, making the use of SP in assembly really
18156          nonsensical, so instead of issuing a warning like we do for other uses
18157          of SP for the odd register operand we error out.  */
18158       constraint (inst.operands[1].reg == REG_SP, BAD_SP);
18159     }
18160
18161   /* Make sure we still check the second operand is an odd one and that PC is
18162      disallowed.  This because we are parsing for any GPR operand, to be able
18163      to distinguish between giving a warning or an error for SP as described
18164      above.  */
18165   constraint ((inst.operands[1].reg % 2) != 1, BAD_EVEN);
18166   constraint (inst.operands[1].reg == REG_PC, BAD_PC);
18167
18168   if (inst.cond > COND_ALWAYS)
18169     inst.pred_insn_type = INSIDE_VPT_INSN;
18170   else
18171     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18172
18173   mve_encode_rrqq (et.type == NT_unsigned, 0);
18174 }
18175
18176
18177 static void
18178 do_mve_vmaxnmv (void)
18179 {
18180   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
18181   struct neon_type_el et
18182     = neon_check_type (2, rs, N_EQK, N_F_MVE | N_KEY);
18183
18184   if (inst.cond > COND_ALWAYS)
18185     inst.pred_insn_type = INSIDE_VPT_INSN;
18186   else
18187     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18188
18189   if (inst.operands[0].reg == REG_SP)
18190     as_tsktsk (MVE_BAD_SP);
18191   else if (inst.operands[0].reg == REG_PC)
18192     as_tsktsk (MVE_BAD_PC);
18193
18194   mve_encode_rq (et.size == 16, 64);
18195 }
18196
18197 static void
18198 do_mve_vmaxv (void)
18199 {
18200   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
18201   struct neon_type_el et;
18202
18203   if (inst.instruction == M_MNEM_vmaxv || inst.instruction == M_MNEM_vminv)
18204     et = neon_check_type (2, rs, N_EQK, N_SU_MVE | N_KEY);
18205   else
18206     et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18207
18208   if (inst.cond > COND_ALWAYS)
18209     inst.pred_insn_type = INSIDE_VPT_INSN;
18210   else
18211     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18212
18213   if (inst.operands[0].reg == REG_SP)
18214     as_tsktsk (MVE_BAD_SP);
18215   else if (inst.operands[0].reg == REG_PC)
18216     as_tsktsk (MVE_BAD_PC);
18217
18218   mve_encode_rq (et.type == NT_unsigned, et.size);
18219 }
18220
18221
18222 static void
18223 do_neon_qrdmlah (void)
18224 {
18225   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18226    return;
18227   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18228     {
18229       /* Check we're on the correct architecture.  */
18230       if (!mark_feature_used (&fpu_neon_ext_armv8))
18231         inst.error
18232           = _("instruction form not available on this architecture.");
18233       else if (!mark_feature_used (&fpu_neon_ext_v8_1))
18234         {
18235           as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
18236           record_feature_use (&fpu_neon_ext_v8_1);
18237         }
18238         if (inst.operands[2].isscalar)
18239           {
18240             enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
18241             struct neon_type_el et = neon_check_type (3, rs,
18242               N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
18243             NEON_ENCODE (SCALAR, inst);
18244             neon_mul_mac (et, neon_quad (rs));
18245           }
18246         else
18247           {
18248             enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18249             struct neon_type_el et = neon_check_type (3, rs,
18250               N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
18251             NEON_ENCODE (INTEGER, inst);
18252             /* The U bit (rounding) comes from bit mask.  */
18253             neon_three_same (neon_quad (rs), 0, et.size);
18254           }
18255     }
18256   else
18257     {
18258       enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
18259       struct neon_type_el et
18260         = neon_check_type (3, rs, N_EQK, N_EQK, N_S_32 | N_KEY);
18261
18262       NEON_ENCODE (INTEGER, inst);
18263       mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
18264     }
18265 }
18266
18267 static void
18268 do_neon_fcmp_absolute (void)
18269 {
18270   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18271   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
18272                                             N_F_16_32 | N_KEY);
18273   /* Size field comes from bit mask.  */
18274   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
18275 }
18276
18277 static void
18278 do_neon_fcmp_absolute_inv (void)
18279 {
18280   neon_exchange_operands ();
18281   do_neon_fcmp_absolute ();
18282 }
18283
18284 static void
18285 do_neon_step (void)
18286 {
18287   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18288   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
18289                                             N_F_16_32 | N_KEY);
18290   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
18291 }
18292
18293 static void
18294 do_neon_abs_neg (void)
18295 {
18296   enum neon_shape rs;
18297   struct neon_type_el et;
18298
18299   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
18300     return;
18301
18302   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18303   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
18304
18305   if (!check_simd_pred_availability (et.type == NT_float,
18306                                      NEON_CHECK_ARCH | NEON_CHECK_CC))
18307     return;
18308
18309   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18310   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18311   inst.instruction |= LOW4 (inst.operands[1].reg);
18312   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18313   inst.instruction |= neon_quad (rs) << 6;
18314   inst.instruction |= (et.type == NT_float) << 10;
18315   inst.instruction |= neon_logbits (et.size) << 18;
18316
18317   neon_dp_fixup (&inst);
18318 }
18319
18320 static void
18321 do_neon_sli (void)
18322 {
18323   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18324     return;
18325
18326   enum neon_shape rs;
18327   struct neon_type_el et;
18328   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18329     {
18330       rs = neon_select_shape (NS_QQI, NS_NULL);
18331       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_KEY);
18332     }
18333   else
18334     {
18335       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18336       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18337     }
18338
18339
18340   int imm = inst.operands[2].imm;
18341   constraint (imm < 0 || (unsigned)imm >= et.size,
18342               _("immediate out of range for insert"));
18343   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
18344 }
18345
18346 static void
18347 do_neon_sri (void)
18348 {
18349   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18350     return;
18351
18352   enum neon_shape rs;
18353   struct neon_type_el et;
18354   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18355     {
18356       rs = neon_select_shape (NS_QQI, NS_NULL);
18357       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_KEY);
18358     }
18359   else
18360     {
18361       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18362       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18363     }
18364
18365   int imm = inst.operands[2].imm;
18366   constraint (imm < 1 || (unsigned)imm > et.size,
18367               _("immediate out of range for insert"));
18368   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
18369 }
18370
18371 static void
18372 do_neon_qshlu_imm (void)
18373 {
18374   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18375     return;
18376
18377   enum neon_shape rs;
18378   struct neon_type_el et;
18379   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18380     {
18381       rs = neon_select_shape (NS_QQI, NS_NULL);
18382       et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18383     }
18384   else
18385     {
18386       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18387       et = neon_check_type (2, rs, N_EQK | N_UNS,
18388                             N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
18389     }
18390
18391   int imm = inst.operands[2].imm;
18392   constraint (imm < 0 || (unsigned)imm >= et.size,
18393               _("immediate out of range for shift"));
18394   /* Only encodes the 'U present' variant of the instruction.
18395      In this case, signed types have OP (bit 8) set to 0.
18396      Unsigned types have OP set to 1.  */
18397   inst.instruction |= (et.type == NT_unsigned) << 8;
18398   /* The rest of the bits are the same as other immediate shifts.  */
18399   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
18400 }
18401
18402 static void
18403 do_neon_qmovn (void)
18404 {
18405   struct neon_type_el et = neon_check_type (2, NS_DQ,
18406     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
18407   /* Saturating move where operands can be signed or unsigned, and the
18408      destination has the same signedness.  */
18409   NEON_ENCODE (INTEGER, inst);
18410   if (et.type == NT_unsigned)
18411     inst.instruction |= 0xc0;
18412   else
18413     inst.instruction |= 0x80;
18414   neon_two_same (0, 1, et.size / 2);
18415 }
18416
18417 static void
18418 do_neon_qmovun (void)
18419 {
18420   struct neon_type_el et = neon_check_type (2, NS_DQ,
18421     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
18422   /* Saturating move with unsigned results. Operands must be signed.  */
18423   NEON_ENCODE (INTEGER, inst);
18424   neon_two_same (0, 1, et.size / 2);
18425 }
18426
18427 static void
18428 do_neon_rshift_sat_narrow (void)
18429 {
18430   /* FIXME: Types for narrowing. If operands are signed, results can be signed
18431      or unsigned. If operands are unsigned, results must also be unsigned.  */
18432   struct neon_type_el et = neon_check_type (2, NS_DQI,
18433     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
18434   int imm = inst.operands[2].imm;
18435   /* This gets the bounds check, size encoding and immediate bits calculation
18436      right.  */
18437   et.size /= 2;
18438
18439   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
18440      VQMOVN.I<size> <Dd>, <Qm>.  */
18441   if (imm == 0)
18442     {
18443       inst.operands[2].present = 0;
18444       inst.instruction = N_MNEM_vqmovn;
18445       do_neon_qmovn ();
18446       return;
18447     }
18448
18449   constraint (imm < 1 || (unsigned)imm > et.size,
18450               _("immediate out of range"));
18451   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
18452 }
18453
18454 static void
18455 do_neon_rshift_sat_narrow_u (void)
18456 {
18457   /* FIXME: Types for narrowing. If operands are signed, results can be signed
18458      or unsigned. If operands are unsigned, results must also be unsigned.  */
18459   struct neon_type_el et = neon_check_type (2, NS_DQI,
18460     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
18461   int imm = inst.operands[2].imm;
18462   /* This gets the bounds check, size encoding and immediate bits calculation
18463      right.  */
18464   et.size /= 2;
18465
18466   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
18467      VQMOVUN.I<size> <Dd>, <Qm>.  */
18468   if (imm == 0)
18469     {
18470       inst.operands[2].present = 0;
18471       inst.instruction = N_MNEM_vqmovun;
18472       do_neon_qmovun ();
18473       return;
18474     }
18475
18476   constraint (imm < 1 || (unsigned)imm > et.size,
18477               _("immediate out of range"));
18478   /* FIXME: The manual is kind of unclear about what value U should have in
18479      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
18480      must be 1.  */
18481   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
18482 }
18483
18484 static void
18485 do_neon_movn (void)
18486 {
18487   struct neon_type_el et = neon_check_type (2, NS_DQ,
18488     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
18489   NEON_ENCODE (INTEGER, inst);
18490   neon_two_same (0, 1, et.size / 2);
18491 }
18492
18493 static void
18494 do_neon_rshift_narrow (void)
18495 {
18496   struct neon_type_el et = neon_check_type (2, NS_DQI,
18497     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
18498   int imm = inst.operands[2].imm;
18499   /* This gets the bounds check, size encoding and immediate bits calculation
18500      right.  */
18501   et.size /= 2;
18502
18503   /* If immediate is zero then we are a pseudo-instruction for
18504      VMOVN.I<size> <Dd>, <Qm>  */
18505   if (imm == 0)
18506     {
18507       inst.operands[2].present = 0;
18508       inst.instruction = N_MNEM_vmovn;
18509       do_neon_movn ();
18510       return;
18511     }
18512
18513   constraint (imm < 1 || (unsigned)imm > et.size,
18514               _("immediate out of range for narrowing operation"));
18515   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
18516 }
18517
18518 static void
18519 do_neon_shll (void)
18520 {
18521   /* FIXME: Type checking when lengthening.  */
18522   struct neon_type_el et = neon_check_type (2, NS_QDI,
18523     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
18524   unsigned imm = inst.operands[2].imm;
18525
18526   if (imm == et.size)
18527     {
18528       /* Maximum shift variant.  */
18529       NEON_ENCODE (INTEGER, inst);
18530       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18531       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18532       inst.instruction |= LOW4 (inst.operands[1].reg);
18533       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18534       inst.instruction |= neon_logbits (et.size) << 18;
18535
18536       neon_dp_fixup (&inst);
18537     }
18538   else
18539     {
18540       /* A more-specific type check for non-max versions.  */
18541       et = neon_check_type (2, NS_QDI,
18542         N_EQK | N_DBL, N_SU_32 | N_KEY);
18543       NEON_ENCODE (IMMED, inst);
18544       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
18545     }
18546 }
18547
18548 /* Check the various types for the VCVT instruction, and return which version
18549    the current instruction is.  */
18550
18551 #define CVT_FLAVOUR_VAR                                                       \
18552   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
18553   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
18554   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
18555   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
18556   /* Half-precision conversions.  */                                          \
18557   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
18558   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
18559   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
18560   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
18561   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
18562   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
18563   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
18564      Compared with single/double precision variants, only the co-processor    \
18565      field is different, so the encoding flow is reused here.  */             \
18566   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
18567   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
18568   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
18569   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
18570   /* VFP instructions.  */                                                    \
18571   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
18572   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
18573   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
18574   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
18575   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
18576   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
18577   /* VFP instructions with bitshift.  */                                      \
18578   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
18579   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
18580   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
18581   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
18582   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
18583   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
18584   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
18585   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
18586
18587 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
18588   neon_cvt_flavour_##C,
18589
18590 /* The different types of conversions we can do.  */
18591 enum neon_cvt_flavour
18592 {
18593   CVT_FLAVOUR_VAR
18594   neon_cvt_flavour_invalid,
18595   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
18596 };
18597
18598 #undef CVT_VAR
18599
18600 static enum neon_cvt_flavour
18601 get_neon_cvt_flavour (enum neon_shape rs)
18602 {
18603 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
18604   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
18605   if (et.type != NT_invtype)                            \
18606     {                                                   \
18607       inst.error = NULL;                                \
18608       return (neon_cvt_flavour_##C);                    \
18609     }
18610
18611   struct neon_type_el et;
18612   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
18613                         || rs == NS_FF) ? N_VFP : 0;
18614   /* The instruction versions which take an immediate take one register
18615      argument, which is extended to the width of the full register. Thus the
18616      "source" and "destination" registers must have the same width.  Hack that
18617      here by making the size equal to the key (wider, in this case) operand.  */
18618   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
18619
18620   CVT_FLAVOUR_VAR;
18621
18622   return neon_cvt_flavour_invalid;
18623 #undef CVT_VAR
18624 }
18625
18626 enum neon_cvt_mode
18627 {
18628   neon_cvt_mode_a,
18629   neon_cvt_mode_n,
18630   neon_cvt_mode_p,
18631   neon_cvt_mode_m,
18632   neon_cvt_mode_z,
18633   neon_cvt_mode_x,
18634   neon_cvt_mode_r
18635 };
18636
18637 /* Neon-syntax VFP conversions.  */
18638
18639 static void
18640 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
18641 {
18642   const char *opname = 0;
18643
18644   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
18645       || rs == NS_FHI || rs == NS_HFI)
18646     {
18647       /* Conversions with immediate bitshift.  */
18648       const char *enc[] =
18649         {
18650 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
18651           CVT_FLAVOUR_VAR
18652           NULL
18653 #undef CVT_VAR
18654         };
18655
18656       if (flavour < (int) ARRAY_SIZE (enc))
18657         {
18658           opname = enc[flavour];
18659           constraint (inst.operands[0].reg != inst.operands[1].reg,
18660                       _("operands 0 and 1 must be the same register"));
18661           inst.operands[1] = inst.operands[2];
18662           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
18663         }
18664     }
18665   else
18666     {
18667       /* Conversions without bitshift.  */
18668       const char *enc[] =
18669         {
18670 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
18671           CVT_FLAVOUR_VAR
18672           NULL
18673 #undef CVT_VAR
18674         };
18675
18676       if (flavour < (int) ARRAY_SIZE (enc))
18677         opname = enc[flavour];
18678     }
18679
18680   if (opname)
18681     do_vfp_nsyn_opcode (opname);
18682
18683   /* ARMv8.2 fp16 VCVT instruction.  */
18684   if (flavour == neon_cvt_flavour_s32_f16
18685       || flavour == neon_cvt_flavour_u32_f16
18686       || flavour == neon_cvt_flavour_f16_u32
18687       || flavour == neon_cvt_flavour_f16_s32)
18688     do_scalar_fp16_v82_encode ();
18689 }
18690
18691 static void
18692 do_vfp_nsyn_cvtz (void)
18693 {
18694   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
18695   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
18696   const char *enc[] =
18697     {
18698 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
18699       CVT_FLAVOUR_VAR
18700       NULL
18701 #undef CVT_VAR
18702     };
18703
18704   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
18705     do_vfp_nsyn_opcode (enc[flavour]);
18706 }
18707
18708 static void
18709 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
18710                       enum neon_cvt_mode mode)
18711 {
18712   int sz, op;
18713   int rm;
18714
18715   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
18716      D register operands.  */
18717   if (flavour == neon_cvt_flavour_s32_f64
18718       || flavour == neon_cvt_flavour_u32_f64)
18719     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
18720                 _(BAD_FPU));
18721
18722   if (flavour == neon_cvt_flavour_s32_f16
18723       || flavour == neon_cvt_flavour_u32_f16)
18724     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
18725                 _(BAD_FP16));
18726
18727   set_pred_insn_type (OUTSIDE_PRED_INSN);
18728
18729   switch (flavour)
18730     {
18731     case neon_cvt_flavour_s32_f64:
18732       sz = 1;
18733       op = 1;
18734       break;
18735     case neon_cvt_flavour_s32_f32:
18736       sz = 0;
18737       op = 1;
18738       break;
18739     case neon_cvt_flavour_s32_f16:
18740       sz = 0;
18741       op = 1;
18742       break;
18743     case neon_cvt_flavour_u32_f64:
18744       sz = 1;
18745       op = 0;
18746       break;
18747     case neon_cvt_flavour_u32_f32:
18748       sz = 0;
18749       op = 0;
18750       break;
18751     case neon_cvt_flavour_u32_f16:
18752       sz = 0;
18753       op = 0;
18754       break;
18755     default:
18756       first_error (_("invalid instruction shape"));
18757       return;
18758     }
18759
18760   switch (mode)
18761     {
18762     case neon_cvt_mode_a: rm = 0; break;
18763     case neon_cvt_mode_n: rm = 1; break;
18764     case neon_cvt_mode_p: rm = 2; break;
18765     case neon_cvt_mode_m: rm = 3; break;
18766     default: first_error (_("invalid rounding mode")); return;
18767     }
18768
18769   NEON_ENCODE (FPV8, inst);
18770   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
18771   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
18772   inst.instruction |= sz << 8;
18773
18774   /* ARMv8.2 fp16 VCVT instruction.  */
18775   if (flavour == neon_cvt_flavour_s32_f16
18776       ||flavour == neon_cvt_flavour_u32_f16)
18777     do_scalar_fp16_v82_encode ();
18778   inst.instruction |= op << 7;
18779   inst.instruction |= rm << 16;
18780   inst.instruction |= 0xf0000000;
18781   inst.is_neon = TRUE;
18782 }
18783
18784 static void
18785 do_neon_cvt_1 (enum neon_cvt_mode mode)
18786 {
18787   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
18788                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
18789                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
18790                                           NS_NULL);
18791   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
18792
18793   if (flavour == neon_cvt_flavour_invalid)
18794     return;
18795
18796   /* PR11109: Handle round-to-zero for VCVT conversions.  */
18797   if (mode == neon_cvt_mode_z
18798       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
18799       && (flavour == neon_cvt_flavour_s16_f16
18800           || flavour == neon_cvt_flavour_u16_f16
18801           || flavour == neon_cvt_flavour_s32_f32
18802           || flavour == neon_cvt_flavour_u32_f32
18803           || flavour == neon_cvt_flavour_s32_f64
18804           || flavour == neon_cvt_flavour_u32_f64)
18805       && (rs == NS_FD || rs == NS_FF))
18806     {
18807       do_vfp_nsyn_cvtz ();
18808       return;
18809     }
18810
18811   /* ARMv8.2 fp16 VCVT conversions.  */
18812   if (mode == neon_cvt_mode_z
18813       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
18814       && (flavour == neon_cvt_flavour_s32_f16
18815           || flavour == neon_cvt_flavour_u32_f16)
18816       && (rs == NS_FH))
18817     {
18818       do_vfp_nsyn_cvtz ();
18819       do_scalar_fp16_v82_encode ();
18820       return;
18821     }
18822
18823   /* VFP rather than Neon conversions.  */
18824   if (flavour >= neon_cvt_flavour_first_fp)
18825     {
18826       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
18827         do_vfp_nsyn_cvt (rs, flavour);
18828       else
18829         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
18830
18831       return;
18832     }
18833
18834   switch (rs)
18835     {
18836     case NS_QQI:
18837       if (mode == neon_cvt_mode_z
18838           && (flavour == neon_cvt_flavour_f16_s16
18839               || flavour == neon_cvt_flavour_f16_u16
18840               || flavour == neon_cvt_flavour_s16_f16
18841               || flavour == neon_cvt_flavour_u16_f16
18842               || flavour == neon_cvt_flavour_f32_u32
18843               || flavour == neon_cvt_flavour_f32_s32
18844               || flavour == neon_cvt_flavour_s32_f32
18845               || flavour == neon_cvt_flavour_u32_f32))
18846         {
18847           if (!check_simd_pred_availability (TRUE,
18848                                              NEON_CHECK_CC | NEON_CHECK_ARCH))
18849             return;
18850         }
18851       else if (mode == neon_cvt_mode_n)
18852         {
18853           /* We are dealing with vcvt with the 'ne' condition.  */
18854           inst.cond = 0x1;
18855           inst.instruction = N_MNEM_vcvt;
18856           do_neon_cvt_1 (neon_cvt_mode_z);
18857           return;
18858         }
18859       /* fall through.  */
18860     case NS_DDI:
18861       {
18862         unsigned immbits;
18863         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
18864                              0x0000100, 0x1000100, 0x0, 0x1000000};
18865
18866         if ((rs != NS_QQI || !ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18867             && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
18868             return;
18869
18870         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18871           {
18872             constraint (inst.operands[2].present && inst.operands[2].imm == 0,
18873                         _("immediate value out of range"));
18874             switch (flavour)
18875               {
18876                 case neon_cvt_flavour_f16_s16:
18877                 case neon_cvt_flavour_f16_u16:
18878                 case neon_cvt_flavour_s16_f16:
18879                 case neon_cvt_flavour_u16_f16:
18880                   constraint (inst.operands[2].imm > 16,
18881                               _("immediate value out of range"));
18882                   break;
18883                 case neon_cvt_flavour_f32_u32:
18884                 case neon_cvt_flavour_f32_s32:
18885                 case neon_cvt_flavour_s32_f32:
18886                 case neon_cvt_flavour_u32_f32:
18887                   constraint (inst.operands[2].imm > 32,
18888                               _("immediate value out of range"));
18889                   break;
18890                 default:
18891                   inst.error = BAD_FPU;
18892                   return;
18893               }
18894           }
18895
18896         /* Fixed-point conversion with #0 immediate is encoded as an
18897            integer conversion.  */
18898         if (inst.operands[2].present && inst.operands[2].imm == 0)
18899           goto int_encode;
18900         NEON_ENCODE (IMMED, inst);
18901         if (flavour != neon_cvt_flavour_invalid)
18902           inst.instruction |= enctab[flavour];
18903         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18904         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18905         inst.instruction |= LOW4 (inst.operands[1].reg);
18906         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18907         inst.instruction |= neon_quad (rs) << 6;
18908         inst.instruction |= 1 << 21;
18909         if (flavour < neon_cvt_flavour_s16_f16)
18910           {
18911             inst.instruction |= 1 << 21;
18912             immbits = 32 - inst.operands[2].imm;
18913             inst.instruction |= immbits << 16;
18914           }
18915         else
18916           {
18917             inst.instruction |= 3 << 20;
18918             immbits = 16 - inst.operands[2].imm;
18919             inst.instruction |= immbits << 16;
18920             inst.instruction &= ~(1 << 9);
18921           }
18922
18923         neon_dp_fixup (&inst);
18924       }
18925       break;
18926
18927     case NS_QQ:
18928       if ((mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
18929            || mode == neon_cvt_mode_m || mode == neon_cvt_mode_p)
18930           && (flavour == neon_cvt_flavour_s16_f16
18931               || flavour == neon_cvt_flavour_u16_f16
18932               || flavour == neon_cvt_flavour_s32_f32
18933               || flavour == neon_cvt_flavour_u32_f32))
18934         {
18935           if (!check_simd_pred_availability (TRUE,
18936                                              NEON_CHECK_CC | NEON_CHECK_ARCH8))
18937             return;
18938         }
18939       else if (mode == neon_cvt_mode_z
18940                && (flavour == neon_cvt_flavour_f16_s16
18941                    || flavour == neon_cvt_flavour_f16_u16
18942                    || flavour == neon_cvt_flavour_s16_f16
18943                    || flavour == neon_cvt_flavour_u16_f16
18944                    || flavour == neon_cvt_flavour_f32_u32
18945                    || flavour == neon_cvt_flavour_f32_s32
18946                    || flavour == neon_cvt_flavour_s32_f32
18947                    || flavour == neon_cvt_flavour_u32_f32))
18948         {
18949           if (!check_simd_pred_availability (TRUE,
18950                                              NEON_CHECK_CC | NEON_CHECK_ARCH))
18951             return;
18952         }
18953       /* fall through.  */
18954     case NS_DD:
18955       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
18956         {
18957
18958           NEON_ENCODE (FLOAT, inst);
18959           if (!check_simd_pred_availability (TRUE,
18960                                              NEON_CHECK_CC | NEON_CHECK_ARCH8))
18961             return;
18962
18963           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18964           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18965           inst.instruction |= LOW4 (inst.operands[1].reg);
18966           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18967           inst.instruction |= neon_quad (rs) << 6;
18968           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
18969                                || flavour == neon_cvt_flavour_u32_f32) << 7;
18970           inst.instruction |= mode << 8;
18971           if (flavour == neon_cvt_flavour_u16_f16
18972               || flavour == neon_cvt_flavour_s16_f16)
18973             /* Mask off the original size bits and reencode them.  */
18974             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
18975
18976           if (thumb_mode)
18977             inst.instruction |= 0xfc000000;
18978           else
18979             inst.instruction |= 0xf0000000;
18980         }
18981       else
18982         {
18983     int_encode:
18984           {
18985             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
18986                                   0x100, 0x180, 0x0, 0x080};
18987
18988             NEON_ENCODE (INTEGER, inst);
18989
18990           if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18991             {
18992               if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
18993                 return;
18994             }
18995
18996             if (flavour != neon_cvt_flavour_invalid)
18997               inst.instruction |= enctab[flavour];
18998
18999             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19000             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19001             inst.instruction |= LOW4 (inst.operands[1].reg);
19002             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19003             inst.instruction |= neon_quad (rs) << 6;
19004             if (flavour >= neon_cvt_flavour_s16_f16
19005                 && flavour <= neon_cvt_flavour_f16_u16)
19006               /* Half precision.  */
19007               inst.instruction |= 1 << 18;
19008             else
19009               inst.instruction |= 2 << 18;
19010
19011             neon_dp_fixup (&inst);
19012           }
19013         }
19014       break;
19015
19016     /* Half-precision conversions for Advanced SIMD -- neon.  */
19017     case NS_QD:
19018     case NS_DQ:
19019       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
19020         return;
19021
19022       if ((rs == NS_DQ)
19023           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
19024           {
19025             as_bad (_("operand size must match register width"));
19026             break;
19027           }
19028
19029       if ((rs == NS_QD)
19030           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
19031           {
19032             as_bad (_("operand size must match register width"));
19033             break;
19034           }
19035
19036       if (rs == NS_DQ)
19037         inst.instruction = 0x3b60600;
19038       else
19039         inst.instruction = 0x3b60700;
19040
19041       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19042       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19043       inst.instruction |= LOW4 (inst.operands[1].reg);
19044       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19045       neon_dp_fixup (&inst);
19046       break;
19047
19048     default:
19049       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
19050       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
19051         do_vfp_nsyn_cvt (rs, flavour);
19052       else
19053         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
19054     }
19055 }
19056
19057 static void
19058 do_neon_cvtr (void)
19059 {
19060   do_neon_cvt_1 (neon_cvt_mode_x);
19061 }
19062
19063 static void
19064 do_neon_cvt (void)
19065 {
19066   do_neon_cvt_1 (neon_cvt_mode_z);
19067 }
19068
19069 static void
19070 do_neon_cvta (void)
19071 {
19072   do_neon_cvt_1 (neon_cvt_mode_a);
19073 }
19074
19075 static void
19076 do_neon_cvtn (void)
19077 {
19078   do_neon_cvt_1 (neon_cvt_mode_n);
19079 }
19080
19081 static void
19082 do_neon_cvtp (void)
19083 {
19084   do_neon_cvt_1 (neon_cvt_mode_p);
19085 }
19086
19087 static void
19088 do_neon_cvtm (void)
19089 {
19090   do_neon_cvt_1 (neon_cvt_mode_m);
19091 }
19092
19093 static void
19094 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
19095 {
19096   if (is_double)
19097     mark_feature_used (&fpu_vfp_ext_armv8);
19098
19099   encode_arm_vfp_reg (inst.operands[0].reg,
19100                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
19101   encode_arm_vfp_reg (inst.operands[1].reg,
19102                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
19103   inst.instruction |= to ? 0x10000 : 0;
19104   inst.instruction |= t ? 0x80 : 0;
19105   inst.instruction |= is_double ? 0x100 : 0;
19106   do_vfp_cond_or_thumb ();
19107 }
19108
19109 static void
19110 do_neon_cvttb_1 (bfd_boolean t)
19111 {
19112   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
19113                                           NS_DF, NS_DH, NS_QQ, NS_QQI, NS_NULL);
19114
19115   if (rs == NS_NULL)
19116     return;
19117   else if (rs == NS_QQ || rs == NS_QQI)
19118     {
19119       int single_to_half = 0;
19120       if (!check_simd_pred_availability (TRUE, NEON_CHECK_ARCH))
19121         return;
19122
19123       enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
19124
19125       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19126           && (flavour ==  neon_cvt_flavour_u16_f16
19127               || flavour ==  neon_cvt_flavour_s16_f16
19128               || flavour ==  neon_cvt_flavour_f16_s16
19129               || flavour ==  neon_cvt_flavour_f16_u16
19130               || flavour ==  neon_cvt_flavour_u32_f32
19131               || flavour ==  neon_cvt_flavour_s32_f32
19132               || flavour ==  neon_cvt_flavour_f32_s32
19133               || flavour ==  neon_cvt_flavour_f32_u32))
19134         {
19135           inst.cond = 0xf;
19136           inst.instruction = N_MNEM_vcvt;
19137           set_pred_insn_type (INSIDE_VPT_INSN);
19138           do_neon_cvt_1 (neon_cvt_mode_z);
19139           return;
19140         }
19141       else if (rs == NS_QQ && flavour == neon_cvt_flavour_f32_f16)
19142         single_to_half = 1;
19143       else if (rs == NS_QQ && flavour != neon_cvt_flavour_f16_f32)
19144         {
19145           first_error (BAD_FPU);
19146           return;
19147         }
19148
19149       inst.instruction = 0xee3f0e01;
19150       inst.instruction |= single_to_half << 28;
19151       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19152       inst.instruction |= LOW4 (inst.operands[0].reg) << 13;
19153       inst.instruction |= t << 12;
19154       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19155       inst.instruction |= LOW4 (inst.operands[1].reg) << 1;
19156       inst.is_neon = 1;
19157     }
19158   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
19159     {
19160       inst.error = NULL;
19161       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
19162     }
19163   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
19164     {
19165       inst.error = NULL;
19166       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
19167     }
19168   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
19169     {
19170       /* The VCVTB and VCVTT instructions with D-register operands
19171          don't work for SP only targets.  */
19172       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19173                   _(BAD_FPU));
19174
19175       inst.error = NULL;
19176       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
19177     }
19178   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
19179     {
19180       /* The VCVTB and VCVTT instructions with D-register operands
19181          don't work for SP only targets.  */
19182       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19183                   _(BAD_FPU));
19184
19185       inst.error = NULL;
19186       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
19187     }
19188   else
19189     return;
19190 }
19191
19192 static void
19193 do_neon_cvtb (void)
19194 {
19195   do_neon_cvttb_1 (FALSE);
19196 }
19197
19198
19199 static void
19200 do_neon_cvtt (void)
19201 {
19202   do_neon_cvttb_1 (TRUE);
19203 }
19204
19205 static void
19206 neon_move_immediate (void)
19207 {
19208   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
19209   struct neon_type_el et = neon_check_type (2, rs,
19210     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
19211   unsigned immlo, immhi = 0, immbits;
19212   int op, cmode, float_p;
19213
19214   constraint (et.type == NT_invtype,
19215               _("operand size must be specified for immediate VMOV"));
19216
19217   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
19218   op = (inst.instruction & (1 << 5)) != 0;
19219
19220   immlo = inst.operands[1].imm;
19221   if (inst.operands[1].regisimm)
19222     immhi = inst.operands[1].reg;
19223
19224   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
19225               _("immediate has bits set outside the operand size"));
19226
19227   float_p = inst.operands[1].immisfloat;
19228
19229   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
19230                                         et.size, et.type)) == FAIL)
19231     {
19232       /* Invert relevant bits only.  */
19233       neon_invert_size (&immlo, &immhi, et.size);
19234       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
19235          with one or the other; those cases are caught by
19236          neon_cmode_for_move_imm.  */
19237       op = !op;
19238       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
19239                                             &op, et.size, et.type)) == FAIL)
19240         {
19241           first_error (_("immediate out of range"));
19242           return;
19243         }
19244     }
19245
19246   inst.instruction &= ~(1 << 5);
19247   inst.instruction |= op << 5;
19248
19249   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19250   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19251   inst.instruction |= neon_quad (rs) << 6;
19252   inst.instruction |= cmode << 8;
19253
19254   neon_write_immbits (immbits);
19255 }
19256
19257 static void
19258 do_neon_mvn (void)
19259 {
19260   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
19261     return;
19262
19263   if (inst.operands[1].isreg)
19264     {
19265       enum neon_shape rs;
19266       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19267         rs = neon_select_shape (NS_QQ, NS_NULL);
19268       else
19269         rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19270
19271       NEON_ENCODE (INTEGER, inst);
19272       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19273       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19274       inst.instruction |= LOW4 (inst.operands[1].reg);
19275       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19276       inst.instruction |= neon_quad (rs) << 6;
19277     }
19278   else
19279     {
19280       NEON_ENCODE (IMMED, inst);
19281       neon_move_immediate ();
19282     }
19283
19284   neon_dp_fixup (&inst);
19285
19286   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19287     {
19288       constraint (!inst.operands[1].isreg && !inst.operands[0].isquad, BAD_FPU);
19289       constraint ((inst.instruction & 0xd00) == 0xd00,
19290                   _("immediate value out of range"));
19291     }
19292 }
19293
19294 /* Encode instructions of form:
19295
19296   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
19297   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
19298
19299 static void
19300 neon_mixed_length (struct neon_type_el et, unsigned size)
19301 {
19302   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19303   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19304   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19305   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19306   inst.instruction |= LOW4 (inst.operands[2].reg);
19307   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19308   inst.instruction |= (et.type == NT_unsigned) << 24;
19309   inst.instruction |= neon_logbits (size) << 20;
19310
19311   neon_dp_fixup (&inst);
19312 }
19313
19314 static void
19315 do_neon_dyadic_long (void)
19316 {
19317   enum neon_shape rs = neon_select_shape (NS_QDD, NS_QQQ, NS_QQR, NS_NULL);
19318   if (rs == NS_QDD)
19319     {
19320       if (vfp_or_neon_is_neon (NEON_CHECK_ARCH | NEON_CHECK_CC) == FAIL)
19321         return;
19322
19323       NEON_ENCODE (INTEGER, inst);
19324       /* FIXME: Type checking for lengthening op.  */
19325       struct neon_type_el et = neon_check_type (3, NS_QDD,
19326         N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
19327       neon_mixed_length (et, et.size);
19328     }
19329   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19330            && (inst.cond == 0xf || inst.cond == 0x10))
19331     {
19332       /* If parsing for MVE, vaddl/vsubl/vabdl{e,t} can only be vadd/vsub/vabd
19333          in an IT block with le/lt conditions.  */
19334
19335       if (inst.cond == 0xf)
19336         inst.cond = 0xb;
19337       else if (inst.cond == 0x10)
19338         inst.cond = 0xd;
19339
19340       inst.pred_insn_type = INSIDE_IT_INSN;
19341
19342       if (inst.instruction == N_MNEM_vaddl)
19343         {
19344           inst.instruction = N_MNEM_vadd;
19345           do_neon_addsub_if_i ();
19346         }
19347       else if (inst.instruction == N_MNEM_vsubl)
19348         {
19349           inst.instruction = N_MNEM_vsub;
19350           do_neon_addsub_if_i ();
19351         }
19352       else if (inst.instruction == N_MNEM_vabdl)
19353         {
19354           inst.instruction = N_MNEM_vabd;
19355           do_neon_dyadic_if_su ();
19356         }
19357     }
19358   else
19359     first_error (BAD_FPU);
19360 }
19361
19362 static void
19363 do_neon_abal (void)
19364 {
19365   struct neon_type_el et = neon_check_type (3, NS_QDD,
19366     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
19367   neon_mixed_length (et, et.size);
19368 }
19369
19370 static void
19371 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
19372 {
19373   if (inst.operands[2].isscalar)
19374     {
19375       struct neon_type_el et = neon_check_type (3, NS_QDS,
19376         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
19377       NEON_ENCODE (SCALAR, inst);
19378       neon_mul_mac (et, et.type == NT_unsigned);
19379     }
19380   else
19381     {
19382       struct neon_type_el et = neon_check_type (3, NS_QDD,
19383         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
19384       NEON_ENCODE (INTEGER, inst);
19385       neon_mixed_length (et, et.size);
19386     }
19387 }
19388
19389 static void
19390 do_neon_mac_maybe_scalar_long (void)
19391 {
19392   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
19393 }
19394
19395 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
19396    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
19397
19398 static unsigned
19399 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
19400 {
19401   unsigned regno = NEON_SCALAR_REG (scalar);
19402   unsigned elno = NEON_SCALAR_INDEX (scalar);
19403
19404   if (quad_p)
19405     {
19406       if (regno > 7 || elno > 3)
19407         goto bad_scalar;
19408
19409       return ((regno & 0x7)
19410               | ((elno & 0x1) << 3)
19411               | (((elno >> 1) & 0x1) << 5));
19412     }
19413   else
19414     {
19415       if (regno > 15 || elno > 1)
19416         goto bad_scalar;
19417
19418       return (((regno & 0x1) << 5)
19419               | ((regno >> 1) & 0x7)
19420               | ((elno & 0x1) << 3));
19421     }
19422
19423 bad_scalar:
19424   first_error (_("scalar out of range for multiply instruction"));
19425   return 0;
19426 }
19427
19428 static void
19429 do_neon_fmac_maybe_scalar_long (int subtype)
19430 {
19431   enum neon_shape rs;
19432   int high8;
19433   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
19434      field (bits[21:20]) has different meaning.  For scalar index variant, it's
19435      used to differentiate add and subtract, otherwise it's with fixed value
19436      0x2.  */
19437   int size = -1;
19438
19439   if (inst.cond != COND_ALWAYS)
19440     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
19441                "behaviour is UNPREDICTABLE"));
19442
19443   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
19444               _(BAD_FP16));
19445
19446   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
19447               _(BAD_FPU));
19448
19449   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
19450      be a scalar index register.  */
19451   if (inst.operands[2].isscalar)
19452     {
19453       high8 = 0xfe000000;
19454       if (subtype)
19455         size = 16;
19456       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
19457     }
19458   else
19459     {
19460       high8 = 0xfc000000;
19461       size = 32;
19462       if (subtype)
19463         inst.instruction |= (0x1 << 23);
19464       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
19465     }
19466
19467   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
19468
19469   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
19470      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
19471      so we simply pass -1 as size.  */
19472   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
19473   neon_three_same (quad_p, 0, size);
19474
19475   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
19476   inst.instruction &= 0x00ffffff;
19477   inst.instruction |= high8;
19478
19479 #define LOW1(R) ((R) & 0x1)
19480 #define HI4(R) (((R) >> 1) & 0xf)
19481   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
19482      whether the instruction is in Q form and whether Vm is a scalar indexed
19483      operand.  */
19484   if (inst.operands[2].isscalar)
19485     {
19486       unsigned rm
19487         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
19488       inst.instruction &= 0xffffffd0;
19489       inst.instruction |= rm;
19490
19491       if (!quad_p)
19492         {
19493           /* Redo Rn as well.  */
19494           inst.instruction &= 0xfff0ff7f;
19495           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
19496           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
19497         }
19498     }
19499   else if (!quad_p)
19500     {
19501       /* Redo Rn and Rm.  */
19502       inst.instruction &= 0xfff0ff50;
19503       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
19504       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
19505       inst.instruction |= HI4 (inst.operands[2].reg);
19506       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
19507     }
19508 }
19509
19510 static void
19511 do_neon_vfmal (void)
19512 {
19513   return do_neon_fmac_maybe_scalar_long (0);
19514 }
19515
19516 static void
19517 do_neon_vfmsl (void)
19518 {
19519   return do_neon_fmac_maybe_scalar_long (1);
19520 }
19521
19522 static void
19523 do_neon_dyadic_wide (void)
19524 {
19525   struct neon_type_el et = neon_check_type (3, NS_QQD,
19526     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
19527   neon_mixed_length (et, et.size);
19528 }
19529
19530 static void
19531 do_neon_dyadic_narrow (void)
19532 {
19533   struct neon_type_el et = neon_check_type (3, NS_QDD,
19534     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
19535   /* Operand sign is unimportant, and the U bit is part of the opcode,
19536      so force the operand type to integer.  */
19537   et.type = NT_integer;
19538   neon_mixed_length (et, et.size / 2);
19539 }
19540
19541 static void
19542 do_neon_mul_sat_scalar_long (void)
19543 {
19544   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
19545 }
19546
19547 static void
19548 do_neon_vmull (void)
19549 {
19550   if (inst.operands[2].isscalar)
19551     do_neon_mac_maybe_scalar_long ();
19552   else
19553     {
19554       struct neon_type_el et = neon_check_type (3, NS_QDD,
19555         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
19556
19557       if (et.type == NT_poly)
19558         NEON_ENCODE (POLY, inst);
19559       else
19560         NEON_ENCODE (INTEGER, inst);
19561
19562       /* For polynomial encoding the U bit must be zero, and the size must
19563          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
19564          obviously, as 0b10).  */
19565       if (et.size == 64)
19566         {
19567           /* Check we're on the correct architecture.  */
19568           if (!mark_feature_used (&fpu_crypto_ext_armv8))
19569             inst.error =
19570               _("Instruction form not available on this architecture.");
19571
19572           et.size = 32;
19573         }
19574
19575       neon_mixed_length (et, et.size);
19576     }
19577 }
19578
19579 static void
19580 do_neon_ext (void)
19581 {
19582   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
19583   struct neon_type_el et = neon_check_type (3, rs,
19584     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
19585   unsigned imm = (inst.operands[3].imm * et.size) / 8;
19586
19587   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
19588               _("shift out of range"));
19589   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19590   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19591   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19592   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19593   inst.instruction |= LOW4 (inst.operands[2].reg);
19594   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19595   inst.instruction |= neon_quad (rs) << 6;
19596   inst.instruction |= imm << 8;
19597
19598   neon_dp_fixup (&inst);
19599 }
19600
19601 static void
19602 do_neon_rev (void)
19603 {
19604   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
19605    return;
19606
19607   enum neon_shape rs;
19608   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19609     rs = neon_select_shape (NS_QQ, NS_NULL);
19610   else
19611     rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19612
19613   struct neon_type_el et = neon_check_type (2, rs,
19614     N_EQK, N_8 | N_16 | N_32 | N_KEY);
19615
19616   unsigned op = (inst.instruction >> 7) & 3;
19617   /* N (width of reversed regions) is encoded as part of the bitmask. We
19618      extract it here to check the elements to be reversed are smaller.
19619      Otherwise we'd get a reserved instruction.  */
19620   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
19621
19622   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext) && elsize == 64
19623       && inst.operands[0].reg == inst.operands[1].reg)
19624     as_tsktsk (_("Warning: 64-bit element size and same destination and source"
19625                  " operands makes instruction UNPREDICTABLE"));
19626
19627   gas_assert (elsize != 0);
19628   constraint (et.size >= elsize,
19629               _("elements must be smaller than reversal region"));
19630   neon_two_same (neon_quad (rs), 1, et.size);
19631 }
19632
19633 static void
19634 do_neon_dup (void)
19635 {
19636   if (inst.operands[1].isscalar)
19637     {
19638       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1),
19639                   BAD_FPU);
19640       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
19641       struct neon_type_el et = neon_check_type (2, rs,
19642         N_EQK, N_8 | N_16 | N_32 | N_KEY);
19643       unsigned sizebits = et.size >> 3;
19644       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
19645       int logsize = neon_logbits (et.size);
19646       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
19647
19648       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
19649         return;
19650
19651       NEON_ENCODE (SCALAR, inst);
19652       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19653       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19654       inst.instruction |= LOW4 (dm);
19655       inst.instruction |= HI1 (dm) << 5;
19656       inst.instruction |= neon_quad (rs) << 6;
19657       inst.instruction |= x << 17;
19658       inst.instruction |= sizebits << 16;
19659
19660       neon_dp_fixup (&inst);
19661     }
19662   else
19663     {
19664       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
19665       struct neon_type_el et = neon_check_type (2, rs,
19666         N_8 | N_16 | N_32 | N_KEY, N_EQK);
19667       if (rs == NS_QR)
19668         {
19669           if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH))
19670             return;
19671         }
19672       else
19673         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1),
19674                     BAD_FPU);
19675
19676       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19677         {
19678           if (inst.operands[1].reg == REG_SP)
19679             as_tsktsk (MVE_BAD_SP);
19680           else if (inst.operands[1].reg == REG_PC)
19681             as_tsktsk (MVE_BAD_PC);
19682         }
19683
19684       /* Duplicate ARM register to lanes of vector.  */
19685       NEON_ENCODE (ARMREG, inst);
19686       switch (et.size)
19687         {
19688         case 8:  inst.instruction |= 0x400000; break;
19689         case 16: inst.instruction |= 0x000020; break;
19690         case 32: inst.instruction |= 0x000000; break;
19691         default: break;
19692         }
19693       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
19694       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
19695       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
19696       inst.instruction |= neon_quad (rs) << 21;
19697       /* The encoding for this instruction is identical for the ARM and Thumb
19698          variants, except for the condition field.  */
19699       do_vfp_cond_or_thumb ();
19700     }
19701 }
19702
19703 static void
19704 do_mve_mov (int toQ)
19705 {
19706   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19707     return;
19708   if (inst.cond > COND_ALWAYS)
19709     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
19710
19711   unsigned Rt = 0, Rt2 = 1, Q0 = 2, Q1 = 3;
19712   if (toQ)
19713     {
19714       Q0 = 0;
19715       Q1 = 1;
19716       Rt = 2;
19717       Rt2 = 3;
19718     }
19719
19720   constraint (inst.operands[Q0].reg != inst.operands[Q1].reg + 2,
19721               _("Index one must be [2,3] and index two must be two less than"
19722                 " index one."));
19723   constraint (inst.operands[Rt].reg == inst.operands[Rt2].reg,
19724               _("General purpose registers may not be the same"));
19725   constraint (inst.operands[Rt].reg == REG_SP
19726               || inst.operands[Rt2].reg == REG_SP,
19727               BAD_SP);
19728   constraint (inst.operands[Rt].reg == REG_PC
19729               || inst.operands[Rt2].reg == REG_PC,
19730               BAD_PC);
19731
19732   inst.instruction = 0xec000f00;
19733   inst.instruction |= HI1 (inst.operands[Q1].reg / 32) << 23;
19734   inst.instruction |= !!toQ << 20;
19735   inst.instruction |= inst.operands[Rt2].reg << 16;
19736   inst.instruction |= LOW4 (inst.operands[Q1].reg / 32) << 13;
19737   inst.instruction |= (inst.operands[Q1].reg % 4) << 4;
19738   inst.instruction |= inst.operands[Rt].reg;
19739 }
19740
19741 static void
19742 do_mve_movn (void)
19743 {
19744   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19745     return;
19746
19747   if (inst.cond > COND_ALWAYS)
19748     inst.pred_insn_type = INSIDE_VPT_INSN;
19749   else
19750     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
19751
19752   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_I16 | N_I32
19753                                             | N_KEY);
19754
19755   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19756   inst.instruction |= (neon_logbits (et.size) - 1) << 18;
19757   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19758   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19759   inst.instruction |= LOW4 (inst.operands[1].reg);
19760   inst.is_neon = 1;
19761
19762 }
19763
19764 /* VMOV has particularly many variations. It can be one of:
19765      0. VMOV<c><q> <Qd>, <Qm>
19766      1. VMOV<c><q> <Dd>, <Dm>
19767    (Register operations, which are VORR with Rm = Rn.)
19768      2. VMOV<c><q>.<dt> <Qd>, #<imm>
19769      3. VMOV<c><q>.<dt> <Dd>, #<imm>
19770    (Immediate loads.)
19771      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
19772    (ARM register to scalar.)
19773      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
19774    (Two ARM registers to vector.)
19775      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
19776    (Scalar to ARM register.)
19777      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
19778    (Vector to two ARM registers.)
19779      8. VMOV.F32 <Sd>, <Sm>
19780      9. VMOV.F64 <Dd>, <Dm>
19781    (VFP register moves.)
19782     10. VMOV.F32 <Sd>, #imm
19783     11. VMOV.F64 <Dd>, #imm
19784    (VFP float immediate load.)
19785     12. VMOV <Rd>, <Sm>
19786    (VFP single to ARM reg.)
19787     13. VMOV <Sd>, <Rm>
19788    (ARM reg to VFP single.)
19789     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
19790    (Two ARM regs to two VFP singles.)
19791     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
19792    (Two VFP singles to two ARM regs.)
19793    16. VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>
19794    17. VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>
19795    18. VMOV<c>.<dt> <Rt>, <Qn[idx]>
19796    19. VMOV<c>.<dt> <Qd[idx]>, <Rt>
19797
19798    These cases can be disambiguated using neon_select_shape, except cases 1/9
19799    and 3/11 which depend on the operand type too.
19800
19801    All the encoded bits are hardcoded by this function.
19802
19803    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
19804    Cases 5, 7 may be used with VFPv2 and above.
19805
19806    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
19807    can specify a type where it doesn't make sense to, and is ignored).  */
19808
19809 static void
19810 do_neon_mov (void)
19811 {
19812   enum neon_shape rs = neon_select_shape (NS_RRSS, NS_SSRR, NS_RRFF, NS_FFRR,
19813                                           NS_DRR, NS_RRD, NS_QQ, NS_DD, NS_QI,
19814                                           NS_DI, NS_SR, NS_RS, NS_FF, NS_FI,
19815                                           NS_RF, NS_FR, NS_HR, NS_RH, NS_HI,
19816                                           NS_NULL);
19817   struct neon_type_el et;
19818   const char *ldconst = 0;
19819
19820   switch (rs)
19821     {
19822     case NS_DD:  /* case 1/9.  */
19823       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
19824       /* It is not an error here if no type is given.  */
19825       inst.error = NULL;
19826       if (et.type == NT_float && et.size == 64)
19827         {
19828           do_vfp_nsyn_opcode ("fcpyd");
19829           break;
19830         }
19831       /* fall through.  */
19832
19833     case NS_QQ:  /* case 0/1.  */
19834       {
19835         if (!check_simd_pred_availability (FALSE,
19836                                            NEON_CHECK_CC | NEON_CHECK_ARCH))
19837           return;
19838         /* The architecture manual I have doesn't explicitly state which
19839            value the U bit should have for register->register moves, but
19840            the equivalent VORR instruction has U = 0, so do that.  */
19841         inst.instruction = 0x0200110;
19842         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19843         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19844         inst.instruction |= LOW4 (inst.operands[1].reg);
19845         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19846         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19847         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19848         inst.instruction |= neon_quad (rs) << 6;
19849
19850         neon_dp_fixup (&inst);
19851       }
19852       break;
19853
19854     case NS_DI:  /* case 3/11.  */
19855       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
19856       inst.error = NULL;
19857       if (et.type == NT_float && et.size == 64)
19858         {
19859           /* case 11 (fconstd).  */
19860           ldconst = "fconstd";
19861           goto encode_fconstd;
19862         }
19863       /* fall through.  */
19864
19865     case NS_QI:  /* case 2/3.  */
19866       if (!check_simd_pred_availability (FALSE,
19867                                          NEON_CHECK_CC | NEON_CHECK_ARCH))
19868         return;
19869       inst.instruction = 0x0800010;
19870       neon_move_immediate ();
19871       neon_dp_fixup (&inst);
19872       break;
19873
19874     case NS_SR:  /* case 4.  */
19875       {
19876         unsigned bcdebits = 0;
19877         int logsize;
19878         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
19879         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
19880
19881         /* .<size> is optional here, defaulting to .32. */
19882         if (inst.vectype.elems == 0
19883             && inst.operands[0].vectype.type == NT_invtype
19884             && inst.operands[1].vectype.type == NT_invtype)
19885           {
19886             inst.vectype.el[0].type = NT_untyped;
19887             inst.vectype.el[0].size = 32;
19888             inst.vectype.elems = 1;
19889           }
19890
19891         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
19892         logsize = neon_logbits (et.size);
19893
19894         if (et.size != 32)
19895           {
19896             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19897                 && vfp_or_neon_is_neon (NEON_CHECK_ARCH) == FAIL)
19898               return;
19899           }
19900         else
19901           {
19902             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
19903                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19904                         _(BAD_FPU));
19905           }
19906
19907         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19908           {
19909             if (inst.operands[1].reg == REG_SP)
19910               as_tsktsk (MVE_BAD_SP);
19911             else if (inst.operands[1].reg == REG_PC)
19912               as_tsktsk (MVE_BAD_PC);
19913           }
19914         unsigned size = inst.operands[0].isscalar == 1 ? 64 : 128;
19915
19916         constraint (et.type == NT_invtype, _("bad type for scalar"));
19917         constraint (x >= size / et.size, _("scalar index out of range"));
19918
19919
19920         switch (et.size)
19921           {
19922           case 8:  bcdebits = 0x8; break;
19923           case 16: bcdebits = 0x1; break;
19924           case 32: bcdebits = 0x0; break;
19925           default: ;
19926           }
19927
19928         bcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
19929
19930         inst.instruction = 0xe000b10;
19931         do_vfp_cond_or_thumb ();
19932         inst.instruction |= LOW4 (dn) << 16;
19933         inst.instruction |= HI1 (dn) << 7;
19934         inst.instruction |= inst.operands[1].reg << 12;
19935         inst.instruction |= (bcdebits & 3) << 5;
19936         inst.instruction |= ((bcdebits >> 2) & 3) << 21;
19937         inst.instruction |= (x >> (3-logsize)) << 16;
19938       }
19939       break;
19940
19941     case NS_DRR:  /* case 5 (fmdrr).  */
19942       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
19943                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19944                   _(BAD_FPU));
19945
19946       inst.instruction = 0xc400b10;
19947       do_vfp_cond_or_thumb ();
19948       inst.instruction |= LOW4 (inst.operands[0].reg);
19949       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
19950       inst.instruction |= inst.operands[1].reg << 12;
19951       inst.instruction |= inst.operands[2].reg << 16;
19952       break;
19953
19954     case NS_RS:  /* case 6.  */
19955       {
19956         unsigned logsize;
19957         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
19958         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
19959         unsigned abcdebits = 0;
19960
19961         /* .<dt> is optional here, defaulting to .32. */
19962         if (inst.vectype.elems == 0
19963             && inst.operands[0].vectype.type == NT_invtype
19964             && inst.operands[1].vectype.type == NT_invtype)
19965           {
19966             inst.vectype.el[0].type = NT_untyped;
19967             inst.vectype.el[0].size = 32;
19968             inst.vectype.elems = 1;
19969           }
19970
19971         et = neon_check_type (2, NS_NULL,
19972                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
19973         logsize = neon_logbits (et.size);
19974
19975         if (et.size != 32)
19976           {
19977             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19978                 && vfp_or_neon_is_neon (NEON_CHECK_CC
19979                                         | NEON_CHECK_ARCH) == FAIL)
19980               return;
19981           }
19982         else
19983           {
19984             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
19985                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19986                         _(BAD_FPU));
19987           }
19988
19989         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19990           {
19991             if (inst.operands[0].reg == REG_SP)
19992               as_tsktsk (MVE_BAD_SP);
19993             else if (inst.operands[0].reg == REG_PC)
19994               as_tsktsk (MVE_BAD_PC);
19995           }
19996
19997         unsigned size = inst.operands[1].isscalar == 1 ? 64 : 128;
19998
19999         constraint (et.type == NT_invtype, _("bad type for scalar"));
20000         constraint (x >= size / et.size, _("scalar index out of range"));
20001
20002         switch (et.size)
20003           {
20004           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
20005           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
20006           case 32: abcdebits = 0x00; break;
20007           default: ;
20008           }
20009
20010         abcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
20011         inst.instruction = 0xe100b10;
20012         do_vfp_cond_or_thumb ();
20013         inst.instruction |= LOW4 (dn) << 16;
20014         inst.instruction |= HI1 (dn) << 7;
20015         inst.instruction |= inst.operands[0].reg << 12;
20016         inst.instruction |= (abcdebits & 3) << 5;
20017         inst.instruction |= (abcdebits >> 2) << 21;
20018         inst.instruction |= (x >> (3-logsize)) << 16;
20019       }
20020       break;
20021
20022     case NS_RRD:  /* case 7 (fmrrd).  */
20023       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20024                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20025                   _(BAD_FPU));
20026
20027       inst.instruction = 0xc500b10;
20028       do_vfp_cond_or_thumb ();
20029       inst.instruction |= inst.operands[0].reg << 12;
20030       inst.instruction |= inst.operands[1].reg << 16;
20031       inst.instruction |= LOW4 (inst.operands[2].reg);
20032       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
20033       break;
20034
20035     case NS_FF:  /* case 8 (fcpys).  */
20036       do_vfp_nsyn_opcode ("fcpys");
20037       break;
20038
20039     case NS_HI:
20040     case NS_FI:  /* case 10 (fconsts).  */
20041       ldconst = "fconsts";
20042     encode_fconstd:
20043       if (!inst.operands[1].immisfloat)
20044         {
20045           unsigned new_imm;
20046           /* Immediate has to fit in 8 bits so float is enough.  */
20047           float imm = (float) inst.operands[1].imm;
20048           memcpy (&new_imm, &imm, sizeof (float));
20049           /* But the assembly may have been written to provide an integer
20050              bit pattern that equates to a float, so check that the
20051              conversion has worked.  */
20052           if (is_quarter_float (new_imm))
20053             {
20054               if (is_quarter_float (inst.operands[1].imm))
20055                 as_warn (_("immediate constant is valid both as a bit-pattern and a floating point value (using the fp value)"));
20056
20057               inst.operands[1].imm = new_imm;
20058               inst.operands[1].immisfloat = 1;
20059             }
20060         }
20061
20062       if (is_quarter_float (inst.operands[1].imm))
20063         {
20064           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
20065           do_vfp_nsyn_opcode (ldconst);
20066
20067           /* ARMv8.2 fp16 vmov.f16 instruction.  */
20068           if (rs == NS_HI)
20069             do_scalar_fp16_v82_encode ();
20070         }
20071       else
20072         first_error (_("immediate out of range"));
20073       break;
20074
20075     case NS_RH:
20076     case NS_RF:  /* case 12 (fmrs).  */
20077       do_vfp_nsyn_opcode ("fmrs");
20078       /* ARMv8.2 fp16 vmov.f16 instruction.  */
20079       if (rs == NS_RH)
20080         do_scalar_fp16_v82_encode ();
20081       break;
20082
20083     case NS_HR:
20084     case NS_FR:  /* case 13 (fmsr).  */
20085       do_vfp_nsyn_opcode ("fmsr");
20086       /* ARMv8.2 fp16 vmov.f16 instruction.  */
20087       if (rs == NS_HR)
20088         do_scalar_fp16_v82_encode ();
20089       break;
20090
20091     case NS_RRSS:
20092       do_mve_mov (0);
20093       break;
20094     case NS_SSRR:
20095       do_mve_mov (1);
20096       break;
20097
20098     /* The encoders for the fmrrs and fmsrr instructions expect three operands
20099        (one of which is a list), but we have parsed four.  Do some fiddling to
20100        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
20101        expect.  */
20102     case NS_RRFF:  /* case 14 (fmrrs).  */
20103       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20104                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20105                   _(BAD_FPU));
20106       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
20107                   _("VFP registers must be adjacent"));
20108       inst.operands[2].imm = 2;
20109       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
20110       do_vfp_nsyn_opcode ("fmrrs");
20111       break;
20112
20113     case NS_FFRR:  /* case 15 (fmsrr).  */
20114       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20115                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20116                   _(BAD_FPU));
20117       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
20118                   _("VFP registers must be adjacent"));
20119       inst.operands[1] = inst.operands[2];
20120       inst.operands[2] = inst.operands[3];
20121       inst.operands[0].imm = 2;
20122       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
20123       do_vfp_nsyn_opcode ("fmsrr");
20124       break;
20125
20126     case NS_NULL:
20127       /* neon_select_shape has determined that the instruction
20128          shape is wrong and has already set the error message.  */
20129       break;
20130
20131     default:
20132       abort ();
20133     }
20134 }
20135
20136 static void
20137 do_mve_movl (void)
20138 {
20139   if (!(inst.operands[0].present && inst.operands[0].isquad
20140       && inst.operands[1].present && inst.operands[1].isquad
20141       && !inst.operands[2].present))
20142     {
20143       inst.instruction = 0;
20144       inst.cond = 0xb;
20145       if (thumb_mode)
20146         set_pred_insn_type (INSIDE_IT_INSN);
20147       do_neon_mov ();
20148       return;
20149     }
20150
20151   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20152     return;
20153
20154   if (inst.cond != COND_ALWAYS)
20155     inst.pred_insn_type = INSIDE_VPT_INSN;
20156
20157   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_S8 | N_U8
20158                                             | N_S16 | N_U16 | N_KEY);
20159
20160   inst.instruction |= (et.type == NT_unsigned) << 28;
20161   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20162   inst.instruction |= (neon_logbits (et.size) + 1) << 19;
20163   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20164   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
20165   inst.instruction |= LOW4 (inst.operands[1].reg);
20166   inst.is_neon = 1;
20167 }
20168
20169 static void
20170 do_neon_rshift_round_imm (void)
20171 {
20172   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20173    return;
20174
20175   enum neon_shape rs;
20176   struct neon_type_el et;
20177
20178   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20179     {
20180       rs = neon_select_shape (NS_QQI, NS_NULL);
20181       et = neon_check_type (2, rs, N_EQK, N_SU_MVE | N_KEY);
20182     }
20183   else
20184     {
20185       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
20186       et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
20187     }
20188   int imm = inst.operands[2].imm;
20189
20190   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
20191   if (imm == 0)
20192     {
20193       inst.operands[2].present = 0;
20194       do_neon_mov ();
20195       return;
20196     }
20197
20198   constraint (imm < 1 || (unsigned)imm > et.size,
20199               _("immediate out of range for shift"));
20200   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
20201                   et.size - imm);
20202 }
20203
20204 static void
20205 do_neon_movhf (void)
20206 {
20207   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
20208   constraint (rs != NS_HH, _("invalid suffix"));
20209
20210   if (inst.cond != COND_ALWAYS)
20211     {
20212       if (thumb_mode)
20213         {
20214           as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
20215                      " the behaviour is UNPREDICTABLE"));
20216         }
20217       else
20218         {
20219           inst.error = BAD_COND;
20220           return;
20221         }
20222     }
20223
20224   do_vfp_sp_monadic ();
20225
20226   inst.is_neon = 1;
20227   inst.instruction |= 0xf0000000;
20228 }
20229
20230 static void
20231 do_neon_movl (void)
20232 {
20233   struct neon_type_el et = neon_check_type (2, NS_QD,
20234     N_EQK | N_DBL, N_SU_32 | N_KEY);
20235   unsigned sizebits = et.size >> 3;
20236   inst.instruction |= sizebits << 19;
20237   neon_two_same (0, et.type == NT_unsigned, -1);
20238 }
20239
20240 static void
20241 do_neon_trn (void)
20242 {
20243   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20244   struct neon_type_el et = neon_check_type (2, rs,
20245     N_EQK, N_8 | N_16 | N_32 | N_KEY);
20246   NEON_ENCODE (INTEGER, inst);
20247   neon_two_same (neon_quad (rs), 1, et.size);
20248 }
20249
20250 static void
20251 do_neon_zip_uzp (void)
20252 {
20253   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20254   struct neon_type_el et = neon_check_type (2, rs,
20255     N_EQK, N_8 | N_16 | N_32 | N_KEY);
20256   if (rs == NS_DD && et.size == 32)
20257     {
20258       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
20259       inst.instruction = N_MNEM_vtrn;
20260       do_neon_trn ();
20261       return;
20262     }
20263   neon_two_same (neon_quad (rs), 1, et.size);
20264 }
20265
20266 static void
20267 do_neon_sat_abs_neg (void)
20268 {
20269   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
20270     return;
20271
20272   enum neon_shape rs;
20273   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20274     rs = neon_select_shape (NS_QQ, NS_NULL);
20275   else
20276     rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20277   struct neon_type_el et = neon_check_type (2, rs,
20278     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
20279   neon_two_same (neon_quad (rs), 1, et.size);
20280 }
20281
20282 static void
20283 do_neon_pair_long (void)
20284 {
20285   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20286   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
20287   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
20288   inst.instruction |= (et.type == NT_unsigned) << 7;
20289   neon_two_same (neon_quad (rs), 1, et.size);
20290 }
20291
20292 static void
20293 do_neon_recip_est (void)
20294 {
20295   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20296   struct neon_type_el et = neon_check_type (2, rs,
20297     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
20298   inst.instruction |= (et.type == NT_float) << 8;
20299   neon_two_same (neon_quad (rs), 1, et.size);
20300 }
20301
20302 static void
20303 do_neon_cls (void)
20304 {
20305   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20306     return;
20307
20308   enum neon_shape rs;
20309   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20310    rs = neon_select_shape (NS_QQ, NS_NULL);
20311   else
20312    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20313
20314   struct neon_type_el et = neon_check_type (2, rs,
20315     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
20316   neon_two_same (neon_quad (rs), 1, et.size);
20317 }
20318
20319 static void
20320 do_neon_clz (void)
20321 {
20322   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20323     return;
20324
20325   enum neon_shape rs;
20326   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20327    rs = neon_select_shape (NS_QQ, NS_NULL);
20328   else
20329    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20330
20331   struct neon_type_el et = neon_check_type (2, rs,
20332     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
20333   neon_two_same (neon_quad (rs), 1, et.size);
20334 }
20335
20336 static void
20337 do_neon_cnt (void)
20338 {
20339   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20340   struct neon_type_el et = neon_check_type (2, rs,
20341     N_EQK | N_INT, N_8 | N_KEY);
20342   neon_two_same (neon_quad (rs), 1, et.size);
20343 }
20344
20345 static void
20346 do_neon_swp (void)
20347 {
20348   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20349   neon_two_same (neon_quad (rs), 1, -1);
20350 }
20351
20352 static void
20353 do_neon_tbl_tbx (void)
20354 {
20355   unsigned listlenbits;
20356   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
20357
20358   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
20359     {
20360       first_error (_("bad list length for table lookup"));
20361       return;
20362     }
20363
20364   listlenbits = inst.operands[1].imm - 1;
20365   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20366   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20367   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
20368   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
20369   inst.instruction |= LOW4 (inst.operands[2].reg);
20370   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
20371   inst.instruction |= listlenbits << 8;
20372
20373   neon_dp_fixup (&inst);
20374 }
20375
20376 static void
20377 do_neon_ldm_stm (void)
20378 {
20379   /* P, U and L bits are part of bitmask.  */
20380   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
20381   unsigned offsetbits = inst.operands[1].imm * 2;
20382
20383   if (inst.operands[1].issingle)
20384     {
20385       do_vfp_nsyn_ldm_stm (is_dbmode);
20386       return;
20387     }
20388
20389   constraint (is_dbmode && !inst.operands[0].writeback,
20390               _("writeback (!) must be used for VLDMDB and VSTMDB"));
20391
20392   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
20393               _("register list must contain at least 1 and at most 16 "
20394                 "registers"));
20395
20396   inst.instruction |= inst.operands[0].reg << 16;
20397   inst.instruction |= inst.operands[0].writeback << 21;
20398   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
20399   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
20400
20401   inst.instruction |= offsetbits;
20402
20403   do_vfp_cond_or_thumb ();
20404 }
20405
20406 static void
20407 do_neon_ldr_str (void)
20408 {
20409   int is_ldr = (inst.instruction & (1 << 20)) != 0;
20410
20411   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
20412      And is UNPREDICTABLE in thumb mode.  */
20413   if (!is_ldr
20414       && inst.operands[1].reg == REG_PC
20415       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
20416     {
20417       if (thumb_mode)
20418         inst.error = _("Use of PC here is UNPREDICTABLE");
20419       else if (warn_on_deprecated)
20420         as_tsktsk (_("Use of PC here is deprecated"));
20421     }
20422
20423   if (inst.operands[0].issingle)
20424     {
20425       if (is_ldr)
20426         do_vfp_nsyn_opcode ("flds");
20427       else
20428         do_vfp_nsyn_opcode ("fsts");
20429
20430       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
20431       if (inst.vectype.el[0].size == 16)
20432         do_scalar_fp16_v82_encode ();
20433     }
20434   else
20435     {
20436       if (is_ldr)
20437         do_vfp_nsyn_opcode ("fldd");
20438       else
20439         do_vfp_nsyn_opcode ("fstd");
20440     }
20441 }
20442
20443 static void
20444 do_t_vldr_vstr_sysreg (void)
20445 {
20446   int fp_vldr_bitno = 20, sysreg_vldr_bitno = 20;
20447   bfd_boolean is_vldr = ((inst.instruction & (1 << fp_vldr_bitno)) != 0);
20448
20449   /* Use of PC is UNPREDICTABLE.  */
20450   if (inst.operands[1].reg == REG_PC)
20451     inst.error = _("Use of PC here is UNPREDICTABLE");
20452
20453   if (inst.operands[1].immisreg)
20454     inst.error = _("instruction does not accept register index");
20455
20456   if (!inst.operands[1].isreg)
20457     inst.error = _("instruction does not accept PC-relative addressing");
20458
20459   if (abs (inst.operands[1].imm) >= (1 << 7))
20460     inst.error = _("immediate value out of range");
20461
20462   inst.instruction = 0xec000f80;
20463   if (is_vldr)
20464     inst.instruction |= 1 << sysreg_vldr_bitno;
20465   encode_arm_cp_address (1, TRUE, FALSE, BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM);
20466   inst.instruction |= (inst.operands[0].imm & 0x7) << 13;
20467   inst.instruction |= (inst.operands[0].imm & 0x8) << 19;
20468 }
20469
20470 static void
20471 do_vldr_vstr (void)
20472 {
20473   bfd_boolean sysreg_op = !inst.operands[0].isreg;
20474
20475   /* VLDR/VSTR (System Register).  */
20476   if (sysreg_op)
20477     {
20478       if (!mark_feature_used (&arm_ext_v8_1m_main))
20479         as_bad (_("Instruction not permitted on this architecture"));
20480
20481       do_t_vldr_vstr_sysreg ();
20482     }
20483   /* VLDR/VSTR.  */
20484   else
20485     {
20486       if (!mark_feature_used (&fpu_vfp_ext_v1xd))
20487         as_bad (_("Instruction not permitted on this architecture"));
20488       do_neon_ldr_str ();
20489     }
20490 }
20491
20492 /* "interleave" version also handles non-interleaving register VLD1/VST1
20493    instructions.  */
20494
20495 static void
20496 do_neon_ld_st_interleave (void)
20497 {
20498   struct neon_type_el et = neon_check_type (1, NS_NULL,
20499                                             N_8 | N_16 | N_32 | N_64);
20500   unsigned alignbits = 0;
20501   unsigned idx;
20502   /* The bits in this table go:
20503      0: register stride of one (0) or two (1)
20504      1,2: register list length, minus one (1, 2, 3, 4).
20505      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
20506      We use -1 for invalid entries.  */
20507   const int typetable[] =
20508     {
20509       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
20510        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
20511        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
20512        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
20513     };
20514   int typebits;
20515
20516   if (et.type == NT_invtype)
20517     return;
20518
20519   if (inst.operands[1].immisalign)
20520     switch (inst.operands[1].imm >> 8)
20521       {
20522       case 64: alignbits = 1; break;
20523       case 128:
20524         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
20525             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
20526           goto bad_alignment;
20527         alignbits = 2;
20528         break;
20529       case 256:
20530         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
20531           goto bad_alignment;
20532         alignbits = 3;
20533         break;
20534       default:
20535       bad_alignment:
20536         first_error (_("bad alignment"));
20537         return;
20538       }
20539
20540   inst.instruction |= alignbits << 4;
20541   inst.instruction |= neon_logbits (et.size) << 6;
20542
20543   /* Bits [4:6] of the immediate in a list specifier encode register stride
20544      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
20545      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
20546      up the right value for "type" in a table based on this value and the given
20547      list style, then stick it back.  */
20548   idx = ((inst.operands[0].imm >> 4) & 7)
20549         | (((inst.instruction >> 8) & 3) << 3);
20550
20551   typebits = typetable[idx];
20552
20553   constraint (typebits == -1, _("bad list type for instruction"));
20554   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
20555               BAD_EL_TYPE);
20556
20557   inst.instruction &= ~0xf00;
20558   inst.instruction |= typebits << 8;
20559 }
20560
20561 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
20562    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
20563    otherwise. The variable arguments are a list of pairs of legal (size, align)
20564    values, terminated with -1.  */
20565
20566 static int
20567 neon_alignment_bit (int size, int align, int *do_alignment, ...)
20568 {
20569   va_list ap;
20570   int result = FAIL, thissize, thisalign;
20571
20572   if (!inst.operands[1].immisalign)
20573     {
20574       *do_alignment = 0;
20575       return SUCCESS;
20576     }
20577
20578   va_start (ap, do_alignment);
20579
20580   do
20581     {
20582       thissize = va_arg (ap, int);
20583       if (thissize == -1)
20584         break;
20585       thisalign = va_arg (ap, int);
20586
20587       if (size == thissize && align == thisalign)
20588         result = SUCCESS;
20589     }
20590   while (result != SUCCESS);
20591
20592   va_end (ap);
20593
20594   if (result == SUCCESS)
20595     *do_alignment = 1;
20596   else
20597     first_error (_("unsupported alignment for instruction"));
20598
20599   return result;
20600 }
20601
20602 static void
20603 do_neon_ld_st_lane (void)
20604 {
20605   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
20606   int align_good, do_alignment = 0;
20607   int logsize = neon_logbits (et.size);
20608   int align = inst.operands[1].imm >> 8;
20609   int n = (inst.instruction >> 8) & 3;
20610   int max_el = 64 / et.size;
20611
20612   if (et.type == NT_invtype)
20613     return;
20614
20615   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
20616               _("bad list length"));
20617   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
20618               _("scalar index out of range"));
20619   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
20620               && et.size == 8,
20621               _("stride of 2 unavailable when element size is 8"));
20622
20623   switch (n)
20624     {
20625     case 0:  /* VLD1 / VST1.  */
20626       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
20627                                        32, 32, -1);
20628       if (align_good == FAIL)
20629         return;
20630       if (do_alignment)
20631         {
20632           unsigned alignbits = 0;
20633           switch (et.size)
20634             {
20635             case 16: alignbits = 0x1; break;
20636             case 32: alignbits = 0x3; break;
20637             default: ;
20638             }
20639           inst.instruction |= alignbits << 4;
20640         }
20641       break;
20642
20643     case 1:  /* VLD2 / VST2.  */
20644       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
20645                       16, 32, 32, 64, -1);
20646       if (align_good == FAIL)
20647         return;
20648       if (do_alignment)
20649         inst.instruction |= 1 << 4;
20650       break;
20651
20652     case 2:  /* VLD3 / VST3.  */
20653       constraint (inst.operands[1].immisalign,
20654                   _("can't use alignment with this instruction"));
20655       break;
20656
20657     case 3:  /* VLD4 / VST4.  */
20658       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
20659                                        16, 64, 32, 64, 32, 128, -1);
20660       if (align_good == FAIL)
20661         return;
20662       if (do_alignment)
20663         {
20664           unsigned alignbits = 0;
20665           switch (et.size)
20666             {
20667             case 8:  alignbits = 0x1; break;
20668             case 16: alignbits = 0x1; break;
20669             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
20670             default: ;
20671             }
20672           inst.instruction |= alignbits << 4;
20673         }
20674       break;
20675
20676     default: ;
20677     }
20678
20679   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
20680   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20681     inst.instruction |= 1 << (4 + logsize);
20682
20683   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
20684   inst.instruction |= logsize << 10;
20685 }
20686
20687 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
20688
20689 static void
20690 do_neon_ld_dup (void)
20691 {
20692   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
20693   int align_good, do_alignment = 0;
20694
20695   if (et.type == NT_invtype)
20696     return;
20697
20698   switch ((inst.instruction >> 8) & 3)
20699     {
20700     case 0:  /* VLD1.  */
20701       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
20702       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
20703                                        &do_alignment, 16, 16, 32, 32, -1);
20704       if (align_good == FAIL)
20705         return;
20706       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
20707         {
20708         case 1: break;
20709         case 2: inst.instruction |= 1 << 5; break;
20710         default: first_error (_("bad list length")); return;
20711         }
20712       inst.instruction |= neon_logbits (et.size) << 6;
20713       break;
20714
20715     case 1:  /* VLD2.  */
20716       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
20717                                        &do_alignment, 8, 16, 16, 32, 32, 64,
20718                                        -1);
20719       if (align_good == FAIL)
20720         return;
20721       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
20722                   _("bad list length"));
20723       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20724         inst.instruction |= 1 << 5;
20725       inst.instruction |= neon_logbits (et.size) << 6;
20726       break;
20727
20728     case 2:  /* VLD3.  */
20729       constraint (inst.operands[1].immisalign,
20730                   _("can't use alignment with this instruction"));
20731       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
20732                   _("bad list length"));
20733       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20734         inst.instruction |= 1 << 5;
20735       inst.instruction |= neon_logbits (et.size) << 6;
20736       break;
20737
20738     case 3:  /* VLD4.  */
20739       {
20740         int align = inst.operands[1].imm >> 8;
20741         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
20742                                          16, 64, 32, 64, 32, 128, -1);
20743         if (align_good == FAIL)
20744           return;
20745         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
20746                     _("bad list length"));
20747         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20748           inst.instruction |= 1 << 5;
20749         if (et.size == 32 && align == 128)
20750           inst.instruction |= 0x3 << 6;
20751         else
20752           inst.instruction |= neon_logbits (et.size) << 6;
20753       }
20754       break;
20755
20756     default: ;
20757     }
20758
20759   inst.instruction |= do_alignment << 4;
20760 }
20761
20762 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
20763    apart from bits [11:4].  */
20764
20765 static void
20766 do_neon_ldx_stx (void)
20767 {
20768   if (inst.operands[1].isreg)
20769     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
20770
20771   switch (NEON_LANE (inst.operands[0].imm))
20772     {
20773     case NEON_INTERLEAVE_LANES:
20774       NEON_ENCODE (INTERLV, inst);
20775       do_neon_ld_st_interleave ();
20776       break;
20777
20778     case NEON_ALL_LANES:
20779       NEON_ENCODE (DUP, inst);
20780       if (inst.instruction == N_INV)
20781         {
20782           first_error ("only loads support such operands");
20783           break;
20784         }
20785       do_neon_ld_dup ();
20786       break;
20787
20788     default:
20789       NEON_ENCODE (LANE, inst);
20790       do_neon_ld_st_lane ();
20791     }
20792
20793   /* L bit comes from bit mask.  */
20794   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20795   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20796   inst.instruction |= inst.operands[1].reg << 16;
20797
20798   if (inst.operands[1].postind)
20799     {
20800       int postreg = inst.operands[1].imm & 0xf;
20801       constraint (!inst.operands[1].immisreg,
20802                   _("post-index must be a register"));
20803       constraint (postreg == 0xd || postreg == 0xf,
20804                   _("bad register for post-index"));
20805       inst.instruction |= postreg;
20806     }
20807   else
20808     {
20809       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
20810       constraint (inst.relocs[0].exp.X_op != O_constant
20811                   || inst.relocs[0].exp.X_add_number != 0,
20812                   BAD_ADDR_MODE);
20813
20814       if (inst.operands[1].writeback)
20815         {
20816           inst.instruction |= 0xd;
20817         }
20818       else
20819         inst.instruction |= 0xf;
20820     }
20821
20822   if (thumb_mode)
20823     inst.instruction |= 0xf9000000;
20824   else
20825     inst.instruction |= 0xf4000000;
20826 }
20827
20828 /* FP v8.  */
20829 static void
20830 do_vfp_nsyn_fpv8 (enum neon_shape rs)
20831 {
20832   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
20833      D register operands.  */
20834   if (neon_shape_class[rs] == SC_DOUBLE)
20835     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20836                 _(BAD_FPU));
20837
20838   NEON_ENCODE (FPV8, inst);
20839
20840   if (rs == NS_FFF || rs == NS_HHH)
20841     {
20842       do_vfp_sp_dyadic ();
20843
20844       /* ARMv8.2 fp16 instruction.  */
20845       if (rs == NS_HHH)
20846         do_scalar_fp16_v82_encode ();
20847     }
20848   else
20849     do_vfp_dp_rd_rn_rm ();
20850
20851   if (rs == NS_DDD)
20852     inst.instruction |= 0x100;
20853
20854   inst.instruction |= 0xf0000000;
20855 }
20856
20857 static void
20858 do_vsel (void)
20859 {
20860   set_pred_insn_type (OUTSIDE_PRED_INSN);
20861
20862   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
20863     first_error (_("invalid instruction shape"));
20864 }
20865
20866 static void
20867 do_vmaxnm (void)
20868 {
20869   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20870     set_pred_insn_type (OUTSIDE_PRED_INSN);
20871
20872   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
20873     return;
20874
20875   if (!check_simd_pred_availability (TRUE, NEON_CHECK_CC | NEON_CHECK_ARCH8))
20876     return;
20877
20878   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
20879 }
20880
20881 static void
20882 do_vrint_1 (enum neon_cvt_mode mode)
20883 {
20884   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
20885   struct neon_type_el et;
20886
20887   if (rs == NS_NULL)
20888     return;
20889
20890   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
20891      D register operands.  */
20892   if (neon_shape_class[rs] == SC_DOUBLE)
20893     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20894                 _(BAD_FPU));
20895
20896   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
20897                         | N_VFP);
20898   if (et.type != NT_invtype)
20899     {
20900       /* VFP encodings.  */
20901       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
20902           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
20903         set_pred_insn_type (OUTSIDE_PRED_INSN);
20904
20905       NEON_ENCODE (FPV8, inst);
20906       if (rs == NS_FF || rs == NS_HH)
20907         do_vfp_sp_monadic ();
20908       else
20909         do_vfp_dp_rd_rm ();
20910
20911       switch (mode)
20912         {
20913         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
20914         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
20915         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
20916         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
20917         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
20918         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
20919         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
20920         default: abort ();
20921         }
20922
20923       inst.instruction |= (rs == NS_DD) << 8;
20924       do_vfp_cond_or_thumb ();
20925
20926       /* ARMv8.2 fp16 vrint instruction.  */
20927       if (rs == NS_HH)
20928       do_scalar_fp16_v82_encode ();
20929     }
20930   else
20931     {
20932       /* Neon encodings (or something broken...).  */
20933       inst.error = NULL;
20934       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
20935
20936       if (et.type == NT_invtype)
20937         return;
20938
20939       if (!check_simd_pred_availability (TRUE,
20940                                          NEON_CHECK_CC | NEON_CHECK_ARCH8))
20941         return;
20942
20943       NEON_ENCODE (FLOAT, inst);
20944
20945       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20946       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20947       inst.instruction |= LOW4 (inst.operands[1].reg);
20948       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
20949       inst.instruction |= neon_quad (rs) << 6;
20950       /* Mask off the original size bits and reencode them.  */
20951       inst.instruction = ((inst.instruction & 0xfff3ffff)
20952                           | neon_logbits (et.size) << 18);
20953
20954       switch (mode)
20955         {
20956         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
20957         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
20958         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
20959         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
20960         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
20961         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
20962         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
20963         default: abort ();
20964         }
20965
20966       if (thumb_mode)
20967         inst.instruction |= 0xfc000000;
20968       else
20969         inst.instruction |= 0xf0000000;
20970     }
20971 }
20972
20973 static void
20974 do_vrintx (void)
20975 {
20976   do_vrint_1 (neon_cvt_mode_x);
20977 }
20978
20979 static void
20980 do_vrintz (void)
20981 {
20982   do_vrint_1 (neon_cvt_mode_z);
20983 }
20984
20985 static void
20986 do_vrintr (void)
20987 {
20988   do_vrint_1 (neon_cvt_mode_r);
20989 }
20990
20991 static void
20992 do_vrinta (void)
20993 {
20994   do_vrint_1 (neon_cvt_mode_a);
20995 }
20996
20997 static void
20998 do_vrintn (void)
20999 {
21000   do_vrint_1 (neon_cvt_mode_n);
21001 }
21002
21003 static void
21004 do_vrintp (void)
21005 {
21006   do_vrint_1 (neon_cvt_mode_p);
21007 }
21008
21009 static void
21010 do_vrintm (void)
21011 {
21012   do_vrint_1 (neon_cvt_mode_m);
21013 }
21014
21015 static unsigned
21016 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
21017 {
21018   unsigned regno = NEON_SCALAR_REG (opnd);
21019   unsigned elno = NEON_SCALAR_INDEX (opnd);
21020
21021   if (elsize == 16 && elno < 2 && regno < 16)
21022     return regno | (elno << 4);
21023   else if (elsize == 32 && elno == 0)
21024     return regno;
21025
21026   first_error (_("scalar out of range"));
21027   return 0;
21028 }
21029
21030 static void
21031 do_vcmla (void)
21032 {
21033   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext)
21034               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
21035                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
21036   constraint (inst.relocs[0].exp.X_op != O_constant,
21037               _("expression too complex"));
21038   unsigned rot = inst.relocs[0].exp.X_add_number;
21039   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
21040               _("immediate out of range"));
21041   rot /= 90;
21042
21043   if (!check_simd_pred_availability (TRUE,
21044                                      NEON_CHECK_ARCH8 | NEON_CHECK_CC))
21045     return;
21046
21047   if (inst.operands[2].isscalar)
21048     {
21049       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
21050         first_error (_("invalid instruction shape"));
21051       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
21052       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
21053                                        N_KEY | N_F16 | N_F32).size;
21054       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
21055       inst.is_neon = 1;
21056       inst.instruction = 0xfe000800;
21057       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21058       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21059       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
21060       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
21061       inst.instruction |= LOW4 (m);
21062       inst.instruction |= HI1 (m) << 5;
21063       inst.instruction |= neon_quad (rs) << 6;
21064       inst.instruction |= rot << 20;
21065       inst.instruction |= (size == 32) << 23;
21066     }
21067   else
21068     {
21069       enum neon_shape rs;
21070       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
21071         rs = neon_select_shape (NS_QQQI, NS_NULL);
21072       else
21073         rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
21074
21075       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
21076                                        N_KEY | N_F16 | N_F32).size;
21077       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext) && size == 32
21078           && (inst.operands[0].reg == inst.operands[1].reg
21079               || inst.operands[0].reg == inst.operands[2].reg))
21080         as_tsktsk (BAD_MVE_SRCDEST);
21081
21082       neon_three_same (neon_quad (rs), 0, -1);
21083       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
21084       inst.instruction |= 0xfc200800;
21085       inst.instruction |= rot << 23;
21086       inst.instruction |= (size == 32) << 20;
21087     }
21088 }
21089
21090 static void
21091 do_vcadd (void)
21092 {
21093   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
21094               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
21095                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
21096   constraint (inst.relocs[0].exp.X_op != O_constant,
21097               _("expression too complex"));
21098
21099   unsigned rot = inst.relocs[0].exp.X_add_number;
21100   constraint (rot != 90 && rot != 270, _("immediate out of range"));
21101   enum neon_shape rs;
21102   struct neon_type_el et;
21103   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
21104     {
21105       rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
21106       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32);
21107     }
21108   else
21109     {
21110       rs = neon_select_shape (NS_QQQI, NS_NULL);
21111       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32 | N_I8
21112                             | N_I16 | N_I32);
21113       if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
21114         as_tsktsk (_("Warning: 32-bit element size and same first and third "
21115                      "operand makes instruction UNPREDICTABLE"));
21116     }
21117
21118   if (et.type == NT_invtype)
21119     return;
21120
21121   if (!check_simd_pred_availability (et.type == NT_float,
21122                                      NEON_CHECK_ARCH8 | NEON_CHECK_CC))
21123     return;
21124
21125   if (et.type == NT_float)
21126     {
21127       neon_three_same (neon_quad (rs), 0, -1);
21128       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
21129       inst.instruction |= 0xfc800800;
21130       inst.instruction |= (rot == 270) << 24;
21131       inst.instruction |= (et.size == 32) << 20;
21132     }
21133   else
21134     {
21135       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
21136       inst.instruction = 0xfe000f00;
21137       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21138       inst.instruction |= neon_logbits (et.size) << 20;
21139       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
21140       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21141       inst.instruction |= (rot == 270) << 12;
21142       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
21143       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
21144       inst.instruction |= LOW4 (inst.operands[2].reg);
21145       inst.is_neon = 1;
21146     }
21147 }
21148
21149 /* Dot Product instructions encoding support.  */
21150
21151 static void
21152 do_neon_dotproduct (int unsigned_p)
21153 {
21154   enum neon_shape rs;
21155   unsigned scalar_oprd2 = 0;
21156   int high8;
21157
21158   if (inst.cond != COND_ALWAYS)
21159     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
21160                "is UNPREDICTABLE"));
21161
21162   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
21163               _(BAD_FPU));
21164
21165   /* Dot Product instructions are in three-same D/Q register format or the third
21166      operand can be a scalar index register.  */
21167   if (inst.operands[2].isscalar)
21168     {
21169       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
21170       high8 = 0xfe000000;
21171       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
21172     }
21173   else
21174     {
21175       high8 = 0xfc000000;
21176       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
21177     }
21178
21179   if (unsigned_p)
21180     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
21181   else
21182     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
21183
21184   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
21185      Product instruction, so we pass 0 as the "ubit" parameter.  And the
21186      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
21187   neon_three_same (neon_quad (rs), 0, 32);
21188
21189   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
21190      different NEON three-same encoding.  */
21191   inst.instruction &= 0x00ffffff;
21192   inst.instruction |= high8;
21193   /* Encode 'U' bit which indicates signedness.  */
21194   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
21195   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
21196      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
21197      the instruction encoding.  */
21198   if (inst.operands[2].isscalar)
21199     {
21200       inst.instruction &= 0xffffffd0;
21201       inst.instruction |= LOW4 (scalar_oprd2);
21202       inst.instruction |= HI1 (scalar_oprd2) << 5;
21203     }
21204 }
21205
21206 /* Dot Product instructions for signed integer.  */
21207
21208 static void
21209 do_neon_dotproduct_s (void)
21210 {
21211   return do_neon_dotproduct (0);
21212 }
21213
21214 /* Dot Product instructions for unsigned integer.  */
21215
21216 static void
21217 do_neon_dotproduct_u (void)
21218 {
21219   return do_neon_dotproduct (1);
21220 }
21221
21222 /* Crypto v1 instructions.  */
21223 static void
21224 do_crypto_2op_1 (unsigned elttype, int op)
21225 {
21226   set_pred_insn_type (OUTSIDE_PRED_INSN);
21227
21228   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
21229       == NT_invtype)
21230     return;
21231
21232   inst.error = NULL;
21233
21234   NEON_ENCODE (INTEGER, inst);
21235   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21236   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21237   inst.instruction |= LOW4 (inst.operands[1].reg);
21238   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
21239   if (op != -1)
21240     inst.instruction |= op << 6;
21241
21242   if (thumb_mode)
21243     inst.instruction |= 0xfc000000;
21244   else
21245     inst.instruction |= 0xf0000000;
21246 }
21247
21248 static void
21249 do_crypto_3op_1 (int u, int op)
21250 {
21251   set_pred_insn_type (OUTSIDE_PRED_INSN);
21252
21253   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
21254                        N_32 | N_UNT | N_KEY).type == NT_invtype)
21255     return;
21256
21257   inst.error = NULL;
21258
21259   NEON_ENCODE (INTEGER, inst);
21260   neon_three_same (1, u, 8 << op);
21261 }
21262
21263 static void
21264 do_aese (void)
21265 {
21266   do_crypto_2op_1 (N_8, 0);
21267 }
21268
21269 static void
21270 do_aesd (void)
21271 {
21272   do_crypto_2op_1 (N_8, 1);
21273 }
21274
21275 static void
21276 do_aesmc (void)
21277 {
21278   do_crypto_2op_1 (N_8, 2);
21279 }
21280
21281 static void
21282 do_aesimc (void)
21283 {
21284   do_crypto_2op_1 (N_8, 3);
21285 }
21286
21287 static void
21288 do_sha1c (void)
21289 {
21290   do_crypto_3op_1 (0, 0);
21291 }
21292
21293 static void
21294 do_sha1p (void)
21295 {
21296   do_crypto_3op_1 (0, 1);
21297 }
21298
21299 static void
21300 do_sha1m (void)
21301 {
21302   do_crypto_3op_1 (0, 2);
21303 }
21304
21305 static void
21306 do_sha1su0 (void)
21307 {
21308   do_crypto_3op_1 (0, 3);
21309 }
21310
21311 static void
21312 do_sha256h (void)
21313 {
21314   do_crypto_3op_1 (1, 0);
21315 }
21316
21317 static void
21318 do_sha256h2 (void)
21319 {
21320   do_crypto_3op_1 (1, 1);
21321 }
21322
21323 static void
21324 do_sha256su1 (void)
21325 {
21326   do_crypto_3op_1 (1, 2);
21327 }
21328
21329 static void
21330 do_sha1h (void)
21331 {
21332   do_crypto_2op_1 (N_32, -1);
21333 }
21334
21335 static void
21336 do_sha1su1 (void)
21337 {
21338   do_crypto_2op_1 (N_32, 0);
21339 }
21340
21341 static void
21342 do_sha256su0 (void)
21343 {
21344   do_crypto_2op_1 (N_32, 1);
21345 }
21346
21347 static void
21348 do_crc32_1 (unsigned int poly, unsigned int sz)
21349 {
21350   unsigned int Rd = inst.operands[0].reg;
21351   unsigned int Rn = inst.operands[1].reg;
21352   unsigned int Rm = inst.operands[2].reg;
21353
21354   set_pred_insn_type (OUTSIDE_PRED_INSN);
21355   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
21356   inst.instruction |= LOW4 (Rn) << 16;
21357   inst.instruction |= LOW4 (Rm);
21358   inst.instruction |= sz << (thumb_mode ? 4 : 21);
21359   inst.instruction |= poly << (thumb_mode ? 20 : 9);
21360
21361   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
21362     as_warn (UNPRED_REG ("r15"));
21363 }
21364
21365 static void
21366 do_crc32b (void)
21367 {
21368   do_crc32_1 (0, 0);
21369 }
21370
21371 static void
21372 do_crc32h (void)
21373 {
21374   do_crc32_1 (0, 1);
21375 }
21376
21377 static void
21378 do_crc32w (void)
21379 {
21380   do_crc32_1 (0, 2);
21381 }
21382
21383 static void
21384 do_crc32cb (void)
21385 {
21386   do_crc32_1 (1, 0);
21387 }
21388
21389 static void
21390 do_crc32ch (void)
21391 {
21392   do_crc32_1 (1, 1);
21393 }
21394
21395 static void
21396 do_crc32cw (void)
21397 {
21398   do_crc32_1 (1, 2);
21399 }
21400
21401 static void
21402 do_vjcvt (void)
21403 {
21404   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
21405               _(BAD_FPU));
21406   neon_check_type (2, NS_FD, N_S32, N_F64);
21407   do_vfp_sp_dp_cvt ();
21408   do_vfp_cond_or_thumb ();
21409 }
21410
21411 \f
21412 /* Overall per-instruction processing.  */
21413
21414 /* We need to be able to fix up arbitrary expressions in some statements.
21415    This is so that we can handle symbols that are an arbitrary distance from
21416    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
21417    which returns part of an address in a form which will be valid for
21418    a data instruction.  We do this by pushing the expression into a symbol
21419    in the expr_section, and creating a fix for that.  */
21420
21421 static void
21422 fix_new_arm (fragS *       frag,
21423              int           where,
21424              short int     size,
21425              expressionS * exp,
21426              int           pc_rel,
21427              int           reloc)
21428 {
21429   fixS *           new_fix;
21430
21431   switch (exp->X_op)
21432     {
21433     case O_constant:
21434       if (pc_rel)
21435         {
21436           /* Create an absolute valued symbol, so we have something to
21437              refer to in the object file.  Unfortunately for us, gas's
21438              generic expression parsing will already have folded out
21439              any use of .set foo/.type foo %function that may have
21440              been used to set type information of the target location,
21441              that's being specified symbolically.  We have to presume
21442              the user knows what they are doing.  */
21443           char name[16 + 8];
21444           symbolS *symbol;
21445
21446           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
21447
21448           symbol = symbol_find_or_make (name);
21449           S_SET_SEGMENT (symbol, absolute_section);
21450           symbol_set_frag (symbol, &zero_address_frag);
21451           S_SET_VALUE (symbol, exp->X_add_number);
21452           exp->X_op = O_symbol;
21453           exp->X_add_symbol = symbol;
21454           exp->X_add_number = 0;
21455         }
21456       /* FALLTHROUGH */
21457     case O_symbol:
21458     case O_add:
21459     case O_subtract:
21460       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
21461                              (enum bfd_reloc_code_real) reloc);
21462       break;
21463
21464     default:
21465       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
21466                                   pc_rel, (enum bfd_reloc_code_real) reloc);
21467       break;
21468     }
21469
21470   /* Mark whether the fix is to a THUMB instruction, or an ARM
21471      instruction.  */
21472   new_fix->tc_fix_data = thumb_mode;
21473 }
21474
21475 /* Create a frg for an instruction requiring relaxation.  */
21476 static void
21477 output_relax_insn (void)
21478 {
21479   char * to;
21480   symbolS *sym;
21481   int offset;
21482
21483   /* The size of the instruction is unknown, so tie the debug info to the
21484      start of the instruction.  */
21485   dwarf2_emit_insn (0);
21486
21487   switch (inst.relocs[0].exp.X_op)
21488     {
21489     case O_symbol:
21490       sym = inst.relocs[0].exp.X_add_symbol;
21491       offset = inst.relocs[0].exp.X_add_number;
21492       break;
21493     case O_constant:
21494       sym = NULL;
21495       offset = inst.relocs[0].exp.X_add_number;
21496       break;
21497     default:
21498       sym = make_expr_symbol (&inst.relocs[0].exp);
21499       offset = 0;
21500       break;
21501   }
21502   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
21503                  inst.relax, sym, offset, NULL/*offset, opcode*/);
21504   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
21505 }
21506
21507 /* Write a 32-bit thumb instruction to buf.  */
21508 static void
21509 put_thumb32_insn (char * buf, unsigned long insn)
21510 {
21511   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
21512   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
21513 }
21514
21515 static void
21516 output_inst (const char * str)
21517 {
21518   char * to = NULL;
21519
21520   if (inst.error)
21521     {
21522       as_bad ("%s -- `%s'", inst.error, str);
21523       return;
21524     }
21525   if (inst.relax)
21526     {
21527       output_relax_insn ();
21528       return;
21529     }
21530   if (inst.size == 0)
21531     return;
21532
21533   to = frag_more (inst.size);
21534   /* PR 9814: Record the thumb mode into the current frag so that we know
21535      what type of NOP padding to use, if necessary.  We override any previous
21536      setting so that if the mode has changed then the NOPS that we use will
21537      match the encoding of the last instruction in the frag.  */
21538   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21539
21540   if (thumb_mode && (inst.size > THUMB_SIZE))
21541     {
21542       gas_assert (inst.size == (2 * THUMB_SIZE));
21543       put_thumb32_insn (to, inst.instruction);
21544     }
21545   else if (inst.size > INSN_SIZE)
21546     {
21547       gas_assert (inst.size == (2 * INSN_SIZE));
21548       md_number_to_chars (to, inst.instruction, INSN_SIZE);
21549       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
21550     }
21551   else
21552     md_number_to_chars (to, inst.instruction, inst.size);
21553
21554   int r;
21555   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
21556     {
21557       if (inst.relocs[r].type != BFD_RELOC_UNUSED)
21558         fix_new_arm (frag_now, to - frag_now->fr_literal,
21559                      inst.size, & inst.relocs[r].exp, inst.relocs[r].pc_rel,
21560                      inst.relocs[r].type);
21561     }
21562
21563   dwarf2_emit_insn (inst.size);
21564 }
21565
21566 static char *
21567 output_it_inst (int cond, int mask, char * to)
21568 {
21569   unsigned long instruction = 0xbf00;
21570
21571   mask &= 0xf;
21572   instruction |= mask;
21573   instruction |= cond << 4;
21574
21575   if (to == NULL)
21576     {
21577       to = frag_more (2);
21578 #ifdef OBJ_ELF
21579       dwarf2_emit_insn (2);
21580 #endif
21581     }
21582
21583   md_number_to_chars (to, instruction, 2);
21584
21585   return to;
21586 }
21587
21588 /* Tag values used in struct asm_opcode's tag field.  */
21589 enum opcode_tag
21590 {
21591   OT_unconditional,     /* Instruction cannot be conditionalized.
21592                            The ARM condition field is still 0xE.  */
21593   OT_unconditionalF,    /* Instruction cannot be conditionalized
21594                            and carries 0xF in its ARM condition field.  */
21595   OT_csuffix,           /* Instruction takes a conditional suffix.  */
21596   OT_csuffixF,          /* Some forms of the instruction take a scalar
21597                            conditional suffix, others place 0xF where the
21598                            condition field would be, others take a vector
21599                            conditional suffix.  */
21600   OT_cinfix3,           /* Instruction takes a conditional infix,
21601                            beginning at character index 3.  (In
21602                            unified mode, it becomes a suffix.)  */
21603   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
21604                             tsts, cmps, cmns, and teqs. */
21605   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
21606                            character index 3, even in unified mode.  Used for
21607                            legacy instructions where suffix and infix forms
21608                            may be ambiguous.  */
21609   OT_csuf_or_in3,       /* Instruction takes either a conditional
21610                            suffix or an infix at character index 3.  */
21611   OT_odd_infix_unc,     /* This is the unconditional variant of an
21612                            instruction that takes a conditional infix
21613                            at an unusual position.  In unified mode,
21614                            this variant will accept a suffix.  */
21615   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
21616                            are the conditional variants of instructions that
21617                            take conditional infixes in unusual positions.
21618                            The infix appears at character index
21619                            (tag - OT_odd_infix_0).  These are not accepted
21620                            in unified mode.  */
21621 };
21622
21623 /* Subroutine of md_assemble, responsible for looking up the primary
21624    opcode from the mnemonic the user wrote.  STR points to the
21625    beginning of the mnemonic.
21626
21627    This is not simply a hash table lookup, because of conditional
21628    variants.  Most instructions have conditional variants, which are
21629    expressed with a _conditional affix_ to the mnemonic.  If we were
21630    to encode each conditional variant as a literal string in the opcode
21631    table, it would have approximately 20,000 entries.
21632
21633    Most mnemonics take this affix as a suffix, and in unified syntax,
21634    'most' is upgraded to 'all'.  However, in the divided syntax, some
21635    instructions take the affix as an infix, notably the s-variants of
21636    the arithmetic instructions.  Of those instructions, all but six
21637    have the infix appear after the third character of the mnemonic.
21638
21639    Accordingly, the algorithm for looking up primary opcodes given
21640    an identifier is:
21641
21642    1. Look up the identifier in the opcode table.
21643       If we find a match, go to step U.
21644
21645    2. Look up the last two characters of the identifier in the
21646       conditions table.  If we find a match, look up the first N-2
21647       characters of the identifier in the opcode table.  If we
21648       find a match, go to step CE.
21649
21650    3. Look up the fourth and fifth characters of the identifier in
21651       the conditions table.  If we find a match, extract those
21652       characters from the identifier, and look up the remaining
21653       characters in the opcode table.  If we find a match, go
21654       to step CM.
21655
21656    4. Fail.
21657
21658    U. Examine the tag field of the opcode structure, in case this is
21659       one of the six instructions with its conditional infix in an
21660       unusual place.  If it is, the tag tells us where to find the
21661       infix; look it up in the conditions table and set inst.cond
21662       accordingly.  Otherwise, this is an unconditional instruction.
21663       Again set inst.cond accordingly.  Return the opcode structure.
21664
21665   CE. Examine the tag field to make sure this is an instruction that
21666       should receive a conditional suffix.  If it is not, fail.
21667       Otherwise, set inst.cond from the suffix we already looked up,
21668       and return the opcode structure.
21669
21670   CM. Examine the tag field to make sure this is an instruction that
21671       should receive a conditional infix after the third character.
21672       If it is not, fail.  Otherwise, undo the edits to the current
21673       line of input and proceed as for case CE.  */
21674
21675 static const struct asm_opcode *
21676 opcode_lookup (char **str)
21677 {
21678   char *end, *base;
21679   char *affix;
21680   const struct asm_opcode *opcode;
21681   const struct asm_cond *cond;
21682   char save[2];
21683
21684   /* Scan up to the end of the mnemonic, which must end in white space,
21685      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
21686   for (base = end = *str; *end != '\0'; end++)
21687     if (*end == ' ' || *end == '.')
21688       break;
21689
21690   if (end == base)
21691     return NULL;
21692
21693   /* Handle a possible width suffix and/or Neon type suffix.  */
21694   if (end[0] == '.')
21695     {
21696       int offset = 2;
21697
21698       /* The .w and .n suffixes are only valid if the unified syntax is in
21699          use.  */
21700       if (unified_syntax && end[1] == 'w')
21701         inst.size_req = 4;
21702       else if (unified_syntax && end[1] == 'n')
21703         inst.size_req = 2;
21704       else
21705         offset = 0;
21706
21707       inst.vectype.elems = 0;
21708
21709       *str = end + offset;
21710
21711       if (end[offset] == '.')
21712         {
21713           /* See if we have a Neon type suffix (possible in either unified or
21714              non-unified ARM syntax mode).  */
21715           if (parse_neon_type (&inst.vectype, str) == FAIL)
21716             return NULL;
21717         }
21718       else if (end[offset] != '\0' && end[offset] != ' ')
21719         return NULL;
21720     }
21721   else
21722     *str = end;
21723
21724   /* Look for unaffixed or special-case affixed mnemonic.  */
21725   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21726                                                     end - base);
21727   if (opcode)
21728     {
21729       /* step U */
21730       if (opcode->tag < OT_odd_infix_0)
21731         {
21732           inst.cond = COND_ALWAYS;
21733           return opcode;
21734         }
21735
21736       if (warn_on_deprecated && unified_syntax)
21737         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
21738       affix = base + (opcode->tag - OT_odd_infix_0);
21739       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21740       gas_assert (cond);
21741
21742       inst.cond = cond->value;
21743       return opcode;
21744     }
21745  if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
21746    {
21747     /* Cannot have a conditional suffix on a mnemonic of less than a character.
21748      */
21749     if (end - base < 2)
21750       return NULL;
21751      affix = end - 1;
21752      cond = (const struct asm_cond *) hash_find_n (arm_vcond_hsh, affix, 1);
21753      opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21754                                                       affix - base);
21755      /* If this opcode can not be vector predicated then don't accept it with a
21756         vector predication code.  */
21757      if (opcode && !opcode->mayBeVecPred)
21758        opcode = NULL;
21759    }
21760   if (!opcode || !cond)
21761     {
21762       /* Cannot have a conditional suffix on a mnemonic of less than two
21763          characters.  */
21764       if (end - base < 3)
21765         return NULL;
21766
21767       /* Look for suffixed mnemonic.  */
21768       affix = end - 2;
21769       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21770       opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21771                                                         affix - base);
21772     }
21773
21774   if (opcode && cond)
21775     {
21776       /* step CE */
21777       switch (opcode->tag)
21778         {
21779         case OT_cinfix3_legacy:
21780           /* Ignore conditional suffixes matched on infix only mnemonics.  */
21781           break;
21782
21783         case OT_cinfix3:
21784         case OT_cinfix3_deprecated:
21785         case OT_odd_infix_unc:
21786           if (!unified_syntax)
21787             return NULL;
21788           /* Fall through.  */
21789
21790         case OT_csuffix:
21791         case OT_csuffixF:
21792         case OT_csuf_or_in3:
21793           inst.cond = cond->value;
21794           return opcode;
21795
21796         case OT_unconditional:
21797         case OT_unconditionalF:
21798           if (thumb_mode)
21799             inst.cond = cond->value;
21800           else
21801             {
21802               /* Delayed diagnostic.  */
21803               inst.error = BAD_COND;
21804               inst.cond = COND_ALWAYS;
21805             }
21806           return opcode;
21807
21808         default:
21809           return NULL;
21810         }
21811     }
21812
21813   /* Cannot have a usual-position infix on a mnemonic of less than
21814      six characters (five would be a suffix).  */
21815   if (end - base < 6)
21816     return NULL;
21817
21818   /* Look for infixed mnemonic in the usual position.  */
21819   affix = base + 3;
21820   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21821   if (!cond)
21822     return NULL;
21823
21824   memcpy (save, affix, 2);
21825   memmove (affix, affix + 2, (end - affix) - 2);
21826   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21827                                                     (end - base) - 2);
21828   memmove (affix + 2, affix, (end - affix) - 2);
21829   memcpy (affix, save, 2);
21830
21831   if (opcode
21832       && (opcode->tag == OT_cinfix3
21833           || opcode->tag == OT_cinfix3_deprecated
21834           || opcode->tag == OT_csuf_or_in3
21835           || opcode->tag == OT_cinfix3_legacy))
21836     {
21837       /* Step CM.  */
21838       if (warn_on_deprecated && unified_syntax
21839           && (opcode->tag == OT_cinfix3
21840               || opcode->tag == OT_cinfix3_deprecated))
21841         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
21842
21843       inst.cond = cond->value;
21844       return opcode;
21845     }
21846
21847   return NULL;
21848 }
21849
21850 /* This function generates an initial IT instruction, leaving its block
21851    virtually open for the new instructions. Eventually,
21852    the mask will be updated by now_pred_add_mask () each time
21853    a new instruction needs to be included in the IT block.
21854    Finally, the block is closed with close_automatic_it_block ().
21855    The block closure can be requested either from md_assemble (),
21856    a tencode (), or due to a label hook.  */
21857
21858 static void
21859 new_automatic_it_block (int cond)
21860 {
21861   now_pred.state = AUTOMATIC_PRED_BLOCK;
21862   now_pred.mask = 0x18;
21863   now_pred.cc = cond;
21864   now_pred.block_length = 1;
21865   mapping_state (MAP_THUMB);
21866   now_pred.insn = output_it_inst (cond, now_pred.mask, NULL);
21867   now_pred.warn_deprecated = FALSE;
21868   now_pred.insn_cond = TRUE;
21869 }
21870
21871 /* Close an automatic IT block.
21872    See comments in new_automatic_it_block ().  */
21873
21874 static void
21875 close_automatic_it_block (void)
21876 {
21877   now_pred.mask = 0x10;
21878   now_pred.block_length = 0;
21879 }
21880
21881 /* Update the mask of the current automatically-generated IT
21882    instruction. See comments in new_automatic_it_block ().  */
21883
21884 static void
21885 now_pred_add_mask (int cond)
21886 {
21887 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
21888 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
21889                                               | ((bitvalue) << (nbit)))
21890   const int resulting_bit = (cond & 1);
21891
21892   now_pred.mask &= 0xf;
21893   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
21894                                    resulting_bit,
21895                                   (5 - now_pred.block_length));
21896   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
21897                                    1,
21898                                    ((5 - now_pred.block_length) - 1));
21899   output_it_inst (now_pred.cc, now_pred.mask, now_pred.insn);
21900
21901 #undef CLEAR_BIT
21902 #undef SET_BIT_VALUE
21903 }
21904
21905 /* The IT blocks handling machinery is accessed through the these functions:
21906      it_fsm_pre_encode ()               from md_assemble ()
21907      set_pred_insn_type ()              optional, from the tencode functions
21908      set_pred_insn_type_last ()         ditto
21909      in_pred_block ()                   ditto
21910      it_fsm_post_encode ()              from md_assemble ()
21911      force_automatic_it_block_close ()  from label handling functions
21912
21913    Rationale:
21914      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
21915         initializing the IT insn type with a generic initial value depending
21916         on the inst.condition.
21917      2) During the tencode function, two things may happen:
21918         a) The tencode function overrides the IT insn type by
21919            calling either set_pred_insn_type (type) or
21920            set_pred_insn_type_last ().
21921         b) The tencode function queries the IT block state by
21922            calling in_pred_block () (i.e. to determine narrow/not narrow mode).
21923
21924         Both set_pred_insn_type and in_pred_block run the internal FSM state
21925         handling function (handle_pred_state), because: a) setting the IT insn
21926         type may incur in an invalid state (exiting the function),
21927         and b) querying the state requires the FSM to be updated.
21928         Specifically we want to avoid creating an IT block for conditional
21929         branches, so it_fsm_pre_encode is actually a guess and we can't
21930         determine whether an IT block is required until the tencode () routine
21931         has decided what type of instruction this actually it.
21932         Because of this, if set_pred_insn_type and in_pred_block have to be
21933         used, set_pred_insn_type has to be called first.
21934
21935         set_pred_insn_type_last () is a wrapper of set_pred_insn_type (type),
21936         that determines the insn IT type depending on the inst.cond code.
21937         When a tencode () routine encodes an instruction that can be
21938         either outside an IT block, or, in the case of being inside, has to be
21939         the last one, set_pred_insn_type_last () will determine the proper
21940         IT instruction type based on the inst.cond code. Otherwise,
21941         set_pred_insn_type can be called for overriding that logic or
21942         for covering other cases.
21943
21944         Calling handle_pred_state () may not transition the IT block state to
21945         OUTSIDE_PRED_BLOCK immediately, since the (current) state could be
21946         still queried. Instead, if the FSM determines that the state should
21947         be transitioned to OUTSIDE_PRED_BLOCK, a flag is marked to be closed
21948         after the tencode () function: that's what it_fsm_post_encode () does.
21949
21950         Since in_pred_block () calls the state handling function to get an
21951         updated state, an error may occur (due to invalid insns combination).
21952         In that case, inst.error is set.
21953         Therefore, inst.error has to be checked after the execution of
21954         the tencode () routine.
21955
21956      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
21957         any pending state change (if any) that didn't take place in
21958         handle_pred_state () as explained above.  */
21959
21960 static void
21961 it_fsm_pre_encode (void)
21962 {
21963   if (inst.cond != COND_ALWAYS)
21964     inst.pred_insn_type =  INSIDE_IT_INSN;
21965   else
21966     inst.pred_insn_type = OUTSIDE_PRED_INSN;
21967
21968   now_pred.state_handled = 0;
21969 }
21970
21971 /* IT state FSM handling function.  */
21972 /* MVE instructions and non-MVE instructions are handled differently because of
21973    the introduction of VPT blocks.
21974    Specifications say that any non-MVE instruction inside a VPT block is
21975    UNPREDICTABLE, with the exception of the BKPT instruction.  Whereas most MVE
21976    instructions are deemed to be UNPREDICTABLE if inside an IT block.  For the
21977    few exceptions we have MVE_UNPREDICABLE_INSN.
21978    The error messages provided depending on the different combinations possible
21979    are described in the cases below:
21980    For 'most' MVE instructions:
21981    1) In an IT block, with an IT code: syntax error
21982    2) In an IT block, with a VPT code: error: must be in a VPT block
21983    3) In an IT block, with no code: warning: UNPREDICTABLE
21984    4) In a VPT block, with an IT code: syntax error
21985    5) In a VPT block, with a VPT code: OK!
21986    6) In a VPT block, with no code: error: missing code
21987    7) Outside a pred block, with an IT code: error: syntax error
21988    8) Outside a pred block, with a VPT code: error: should be in a VPT block
21989    9) Outside a pred block, with no code: OK!
21990    For non-MVE instructions:
21991    10) In an IT block, with an IT code: OK!
21992    11) In an IT block, with a VPT code: syntax error
21993    12) In an IT block, with no code: error: missing code
21994    13) In a VPT block, with an IT code: error: should be in an IT block
21995    14) In a VPT block, with a VPT code: syntax error
21996    15) In a VPT block, with no code: UNPREDICTABLE
21997    16) Outside a pred block, with an IT code: error: should be in an IT block
21998    17) Outside a pred block, with a VPT code: syntax error
21999    18) Outside a pred block, with no code: OK!
22000  */
22001
22002
22003 static int
22004 handle_pred_state (void)
22005 {
22006   now_pred.state_handled = 1;
22007   now_pred.insn_cond = FALSE;
22008
22009   switch (now_pred.state)
22010     {
22011     case OUTSIDE_PRED_BLOCK:
22012       switch (inst.pred_insn_type)
22013         {
22014         case MVE_UNPREDICABLE_INSN:
22015         case MVE_OUTSIDE_PRED_INSN:
22016           if (inst.cond < COND_ALWAYS)
22017             {
22018               /* Case 7: Outside a pred block, with an IT code: error: syntax
22019                  error.  */
22020               inst.error = BAD_SYNTAX;
22021               return FAIL;
22022             }
22023           /* Case 9:  Outside a pred block, with no code: OK!  */
22024           break;
22025         case OUTSIDE_PRED_INSN:
22026           if (inst.cond > COND_ALWAYS)
22027             {
22028               /* Case 17:  Outside a pred block, with a VPT code: syntax error.
22029                */
22030               inst.error = BAD_SYNTAX;
22031               return FAIL;
22032             }
22033           /* Case 18: Outside a pred block, with no code: OK!  */
22034           break;
22035
22036         case INSIDE_VPT_INSN:
22037           /* Case 8: Outside a pred block, with a VPT code: error: should be in
22038              a VPT block.  */
22039           inst.error = BAD_OUT_VPT;
22040           return FAIL;
22041
22042         case INSIDE_IT_INSN:
22043         case INSIDE_IT_LAST_INSN:
22044           if (inst.cond < COND_ALWAYS)
22045             {
22046               /* Case 16: Outside a pred block, with an IT code: error: should
22047                  be in an IT block.  */
22048               if (thumb_mode == 0)
22049                 {
22050                   if (unified_syntax
22051                       && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
22052                     as_tsktsk (_("Warning: conditional outside an IT block"\
22053                                  " for Thumb."));
22054                 }
22055               else
22056                 {
22057                   if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
22058                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
22059                     {
22060                       /* Automatically generate the IT instruction.  */
22061                       new_automatic_it_block (inst.cond);
22062                       if (inst.pred_insn_type == INSIDE_IT_LAST_INSN)
22063                         close_automatic_it_block ();
22064                     }
22065                   else
22066                     {
22067                       inst.error = BAD_OUT_IT;
22068                       return FAIL;
22069                     }
22070                 }
22071               break;
22072             }
22073           else if (inst.cond > COND_ALWAYS)
22074             {
22075               /* Case 17: Outside a pred block, with a VPT code: syntax error.
22076                */
22077               inst.error = BAD_SYNTAX;
22078               return FAIL;
22079             }
22080           else
22081             gas_assert (0);
22082         case IF_INSIDE_IT_LAST_INSN:
22083         case NEUTRAL_IT_INSN:
22084           break;
22085
22086         case VPT_INSN:
22087           if (inst.cond != COND_ALWAYS)
22088             first_error (BAD_SYNTAX);
22089           now_pred.state = MANUAL_PRED_BLOCK;
22090           now_pred.block_length = 0;
22091           now_pred.type = VECTOR_PRED;
22092           now_pred.cc = 0;
22093           break;
22094         case IT_INSN:
22095           now_pred.state = MANUAL_PRED_BLOCK;
22096           now_pred.block_length = 0;
22097           now_pred.type = SCALAR_PRED;
22098           break;
22099         }
22100       break;
22101
22102     case AUTOMATIC_PRED_BLOCK:
22103       /* Three things may happen now:
22104          a) We should increment current it block size;
22105          b) We should close current it block (closing insn or 4 insns);
22106          c) We should close current it block and start a new one (due
22107          to incompatible conditions or
22108          4 insns-length block reached).  */
22109
22110       switch (inst.pred_insn_type)
22111         {
22112         case INSIDE_VPT_INSN:
22113         case VPT_INSN:
22114         case MVE_UNPREDICABLE_INSN:
22115         case MVE_OUTSIDE_PRED_INSN:
22116           gas_assert (0);
22117         case OUTSIDE_PRED_INSN:
22118           /* The closure of the block shall happen immediately,
22119              so any in_pred_block () call reports the block as closed.  */
22120           force_automatic_it_block_close ();
22121           break;
22122
22123         case INSIDE_IT_INSN:
22124         case INSIDE_IT_LAST_INSN:
22125         case IF_INSIDE_IT_LAST_INSN:
22126           now_pred.block_length++;
22127
22128           if (now_pred.block_length > 4
22129               || !now_pred_compatible (inst.cond))
22130             {
22131               force_automatic_it_block_close ();
22132               if (inst.pred_insn_type != IF_INSIDE_IT_LAST_INSN)
22133                 new_automatic_it_block (inst.cond);
22134             }
22135           else
22136             {
22137               now_pred.insn_cond = TRUE;
22138               now_pred_add_mask (inst.cond);
22139             }
22140
22141           if (now_pred.state == AUTOMATIC_PRED_BLOCK
22142               && (inst.pred_insn_type == INSIDE_IT_LAST_INSN
22143                   || inst.pred_insn_type == IF_INSIDE_IT_LAST_INSN))
22144             close_automatic_it_block ();
22145           break;
22146
22147         case NEUTRAL_IT_INSN:
22148           now_pred.block_length++;
22149           now_pred.insn_cond = TRUE;
22150
22151           if (now_pred.block_length > 4)
22152             force_automatic_it_block_close ();
22153           else
22154             now_pred_add_mask (now_pred.cc & 1);
22155           break;
22156
22157         case IT_INSN:
22158           close_automatic_it_block ();
22159           now_pred.state = MANUAL_PRED_BLOCK;
22160           break;
22161         }
22162       break;
22163
22164     case MANUAL_PRED_BLOCK:
22165       {
22166         int cond, is_last;
22167         if (now_pred.type == SCALAR_PRED)
22168           {
22169             /* Check conditional suffixes.  */
22170             cond = now_pred.cc ^ ((now_pred.mask >> 4) & 1) ^ 1;
22171             now_pred.mask <<= 1;
22172             now_pred.mask &= 0x1f;
22173             is_last = (now_pred.mask == 0x10);
22174           }
22175         else
22176           {
22177             now_pred.cc ^= (now_pred.mask >> 4);
22178             cond = now_pred.cc + 0xf;
22179             now_pred.mask <<= 1;
22180             now_pred.mask &= 0x1f;
22181             is_last = now_pred.mask == 0x10;
22182           }
22183         now_pred.insn_cond = TRUE;
22184
22185         switch (inst.pred_insn_type)
22186           {
22187           case OUTSIDE_PRED_INSN:
22188             if (now_pred.type == SCALAR_PRED)
22189               {
22190                 if (inst.cond == COND_ALWAYS)
22191                   {
22192                     /* Case 12: In an IT block, with no code: error: missing
22193                        code.  */
22194                     inst.error = BAD_NOT_IT;
22195                     return FAIL;
22196                   }
22197                 else if (inst.cond > COND_ALWAYS)
22198                   {
22199                     /* Case 11: In an IT block, with a VPT code: syntax error.
22200                      */
22201                     inst.error = BAD_SYNTAX;
22202                     return FAIL;
22203                   }
22204                 else if (thumb_mode)
22205                   {
22206                     /* This is for some special cases where a non-MVE
22207                        instruction is not allowed in an IT block, such as cbz,
22208                        but are put into one with a condition code.
22209                        You could argue this should be a syntax error, but we
22210                        gave the 'not allowed in IT block' diagnostic in the
22211                        past so we will keep doing so.  */
22212                     inst.error = BAD_NOT_IT;
22213                     return FAIL;
22214                   }
22215                 break;
22216               }
22217             else
22218               {
22219                 /* Case 15: In a VPT block, with no code: UNPREDICTABLE.  */
22220                 as_tsktsk (MVE_NOT_VPT);
22221                 return SUCCESS;
22222               }
22223           case MVE_OUTSIDE_PRED_INSN:
22224             if (now_pred.type == SCALAR_PRED)
22225               {
22226                 if (inst.cond == COND_ALWAYS)
22227                   {
22228                     /* Case 3: In an IT block, with no code: warning:
22229                        UNPREDICTABLE.  */
22230                     as_tsktsk (MVE_NOT_IT);
22231                     return SUCCESS;
22232                   }
22233                 else if (inst.cond < COND_ALWAYS)
22234                   {
22235                     /* Case 1: In an IT block, with an IT code: syntax error.
22236                      */
22237                     inst.error = BAD_SYNTAX;
22238                     return FAIL;
22239                   }
22240                 else
22241                   gas_assert (0);
22242               }
22243             else
22244               {
22245                 if (inst.cond < COND_ALWAYS)
22246                   {
22247                     /* Case 4: In a VPT block, with an IT code: syntax error.
22248                      */
22249                     inst.error = BAD_SYNTAX;
22250                     return FAIL;
22251                   }
22252                 else if (inst.cond == COND_ALWAYS)
22253                   {
22254                     /* Case 6: In a VPT block, with no code: error: missing
22255                        code.  */
22256                     inst.error = BAD_NOT_VPT;
22257                     return FAIL;
22258                   }
22259                 else
22260                   {
22261                     gas_assert (0);
22262                   }
22263               }
22264           case MVE_UNPREDICABLE_INSN:
22265             as_tsktsk (now_pred.type == SCALAR_PRED ? MVE_NOT_IT : MVE_NOT_VPT);
22266             return SUCCESS;
22267           case INSIDE_IT_INSN:
22268             if (inst.cond > COND_ALWAYS)
22269               {
22270                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
22271                 /* Case 14: In a VPT block, with a VPT code: syntax error.  */
22272                 inst.error = BAD_SYNTAX;
22273                 return FAIL;
22274               }
22275             else if (now_pred.type == SCALAR_PRED)
22276               {
22277                 /* Case 10: In an IT block, with an IT code: OK!  */
22278                 if (cond != inst.cond)
22279                   {
22280                     inst.error = now_pred.type == SCALAR_PRED ? BAD_IT_COND :
22281                       BAD_VPT_COND;
22282                     return FAIL;
22283                   }
22284               }
22285             else
22286               {
22287                 /* Case 13: In a VPT block, with an IT code: error: should be
22288                    in an IT block.  */
22289                 inst.error = BAD_OUT_IT;
22290                 return FAIL;
22291               }
22292             break;
22293
22294           case INSIDE_VPT_INSN:
22295             if (now_pred.type == SCALAR_PRED)
22296               {
22297                 /* Case 2: In an IT block, with a VPT code: error: must be in a
22298                    VPT block.  */
22299                 inst.error = BAD_OUT_VPT;
22300                 return FAIL;
22301               }
22302             /* Case 5:  In a VPT block, with a VPT code: OK!  */
22303             else if (cond != inst.cond)
22304               {
22305                 inst.error = BAD_VPT_COND;
22306                 return FAIL;
22307               }
22308             break;
22309           case INSIDE_IT_LAST_INSN:
22310           case IF_INSIDE_IT_LAST_INSN:
22311             if (now_pred.type == VECTOR_PRED || inst.cond > COND_ALWAYS)
22312               {
22313                 /* Case 4: In a VPT block, with an IT code: syntax error.  */
22314                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
22315                 inst.error = BAD_SYNTAX;
22316                 return FAIL;
22317               }
22318             else if (cond != inst.cond)
22319               {
22320                 inst.error = BAD_IT_COND;
22321                 return FAIL;
22322               }
22323             if (!is_last)
22324               {
22325                 inst.error = BAD_BRANCH;
22326                 return FAIL;
22327               }
22328             break;
22329
22330           case NEUTRAL_IT_INSN:
22331             /* The BKPT instruction is unconditional even in a IT or VPT
22332                block.  */
22333             break;
22334
22335           case IT_INSN:
22336             if (now_pred.type == SCALAR_PRED)
22337               {
22338                 inst.error = BAD_IT_IT;
22339                 return FAIL;
22340               }
22341             /* fall through.  */
22342           case VPT_INSN:
22343             if (inst.cond == COND_ALWAYS)
22344               {
22345                 /* Executing a VPT/VPST instruction inside an IT block or a
22346                    VPT/VPST/IT instruction inside a VPT block is UNPREDICTABLE.
22347                  */
22348                 if (now_pred.type == SCALAR_PRED)
22349                   as_tsktsk (MVE_NOT_IT);
22350                 else
22351                   as_tsktsk (MVE_NOT_VPT);
22352                 return SUCCESS;
22353               }
22354             else
22355               {
22356                 /* VPT/VPST do not accept condition codes.  */
22357                 inst.error = BAD_SYNTAX;
22358                 return FAIL;
22359               }
22360           }
22361         }
22362       break;
22363     }
22364
22365   return SUCCESS;
22366 }
22367
22368 struct depr_insn_mask
22369 {
22370   unsigned long pattern;
22371   unsigned long mask;
22372   const char* description;
22373 };
22374
22375 /* List of 16-bit instruction patterns deprecated in an IT block in
22376    ARMv8.  */
22377 static const struct depr_insn_mask depr_it_insns[] = {
22378   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
22379   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
22380   { 0xa000, 0xb800, N_("ADR") },
22381   { 0x4800, 0xf800, N_("Literal loads") },
22382   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
22383   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
22384   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
22385      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
22386   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
22387   { 0, 0, NULL }
22388 };
22389
22390 static void
22391 it_fsm_post_encode (void)
22392 {
22393   int is_last;
22394
22395   if (!now_pred.state_handled)
22396     handle_pred_state ();
22397
22398   if (now_pred.insn_cond
22399       && !now_pred.warn_deprecated
22400       && warn_on_deprecated
22401       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8)
22402       && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_m))
22403     {
22404       if (inst.instruction >= 0x10000)
22405         {
22406           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
22407                      "performance deprecated in ARMv8-A and ARMv8-R"));
22408           now_pred.warn_deprecated = TRUE;
22409         }
22410       else
22411         {
22412           const struct depr_insn_mask *p = depr_it_insns;
22413
22414           while (p->mask != 0)
22415             {
22416               if ((inst.instruction & p->mask) == p->pattern)
22417                 {
22418                   as_tsktsk (_("IT blocks containing 16-bit Thumb "
22419                                "instructions of the following class are "
22420                                "performance deprecated in ARMv8-A and "
22421                                "ARMv8-R: %s"), p->description);
22422                   now_pred.warn_deprecated = TRUE;
22423                   break;
22424                 }
22425
22426               ++p;
22427             }
22428         }
22429
22430       if (now_pred.block_length > 1)
22431         {
22432           as_tsktsk (_("IT blocks containing more than one conditional "
22433                      "instruction are performance deprecated in ARMv8-A and "
22434                      "ARMv8-R"));
22435           now_pred.warn_deprecated = TRUE;
22436         }
22437     }
22438
22439     is_last = (now_pred.mask == 0x10);
22440     if (is_last)
22441       {
22442         now_pred.state = OUTSIDE_PRED_BLOCK;
22443         now_pred.mask = 0;
22444       }
22445 }
22446
22447 static void
22448 force_automatic_it_block_close (void)
22449 {
22450   if (now_pred.state == AUTOMATIC_PRED_BLOCK)
22451     {
22452       close_automatic_it_block ();
22453       now_pred.state = OUTSIDE_PRED_BLOCK;
22454       now_pred.mask = 0;
22455     }
22456 }
22457
22458 static int
22459 in_pred_block (void)
22460 {
22461   if (!now_pred.state_handled)
22462     handle_pred_state ();
22463
22464   return now_pred.state != OUTSIDE_PRED_BLOCK;
22465 }
22466
22467 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
22468    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
22469    here, hence the "known" in the function name.  */
22470
22471 static bfd_boolean
22472 known_t32_only_insn (const struct asm_opcode *opcode)
22473 {
22474   /* Original Thumb-1 wide instruction.  */
22475   if (opcode->tencode == do_t_blx
22476       || opcode->tencode == do_t_branch23
22477       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
22478       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
22479     return TRUE;
22480
22481   /* Wide-only instruction added to ARMv8-M Baseline.  */
22482   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
22483       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
22484       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
22485       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
22486     return TRUE;
22487
22488   return FALSE;
22489 }
22490
22491 /* Whether wide instruction variant can be used if available for a valid OPCODE
22492    in ARCH.  */
22493
22494 static bfd_boolean
22495 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
22496 {
22497   if (known_t32_only_insn (opcode))
22498     return TRUE;
22499
22500   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
22501      of variant T3 of B.W is checked in do_t_branch.  */
22502   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
22503       && opcode->tencode == do_t_branch)
22504     return TRUE;
22505
22506   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
22507   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
22508       && opcode->tencode == do_t_mov_cmp
22509       /* Make sure CMP instruction is not affected.  */
22510       && opcode->aencode == do_mov)
22511     return TRUE;
22512
22513   /* Wide instruction variants of all instructions with narrow *and* wide
22514      variants become available with ARMv6t2.  Other opcodes are either
22515      narrow-only or wide-only and are thus available if OPCODE is valid.  */
22516   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
22517     return TRUE;
22518
22519   /* OPCODE with narrow only instruction variant or wide variant not
22520      available.  */
22521   return FALSE;
22522 }
22523
22524 void
22525 md_assemble (char *str)
22526 {
22527   char *p = str;
22528   const struct asm_opcode * opcode;
22529
22530   /* Align the previous label if needed.  */
22531   if (last_label_seen != NULL)
22532     {
22533       symbol_set_frag (last_label_seen, frag_now);
22534       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
22535       S_SET_SEGMENT (last_label_seen, now_seg);
22536     }
22537
22538   memset (&inst, '\0', sizeof (inst));
22539   int r;
22540   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
22541     inst.relocs[r].type = BFD_RELOC_UNUSED;
22542
22543   opcode = opcode_lookup (&p);
22544   if (!opcode)
22545     {
22546       /* It wasn't an instruction, but it might be a register alias of
22547          the form alias .req reg, or a Neon .dn/.qn directive.  */
22548       if (! create_register_alias (str, p)
22549           && ! create_neon_reg_alias (str, p))
22550         as_bad (_("bad instruction `%s'"), str);
22551
22552       return;
22553     }
22554
22555   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
22556     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
22557
22558   /* The value which unconditional instructions should have in place of the
22559      condition field.  */
22560   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
22561
22562   if (thumb_mode)
22563     {
22564       arm_feature_set variant;
22565
22566       variant = cpu_variant;
22567       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
22568       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
22569         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
22570       /* Check that this instruction is supported for this CPU.  */
22571       if (!opcode->tvariant
22572           || (thumb_mode == 1
22573               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
22574         {
22575           if (opcode->tencode == do_t_swi)
22576             as_bad (_("SVC is not permitted on this architecture"));
22577           else
22578             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
22579           return;
22580         }
22581       if (inst.cond != COND_ALWAYS && !unified_syntax
22582           && opcode->tencode != do_t_branch)
22583         {
22584           as_bad (_("Thumb does not support conditional execution"));
22585           return;
22586         }
22587
22588       /* Two things are addressed here:
22589          1) Implicit require narrow instructions on Thumb-1.
22590             This avoids relaxation accidentally introducing Thumb-2
22591             instructions.
22592          2) Reject wide instructions in non Thumb-2 cores.
22593
22594          Only instructions with narrow and wide variants need to be handled
22595          but selecting all non wide-only instructions is easier.  */
22596       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
22597           && !t32_insn_ok (variant, opcode))
22598         {
22599           if (inst.size_req == 0)
22600             inst.size_req = 2;
22601           else if (inst.size_req == 4)
22602             {
22603               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
22604                 as_bad (_("selected processor does not support 32bit wide "
22605                           "variant of instruction `%s'"), str);
22606               else
22607                 as_bad (_("selected processor does not support `%s' in "
22608                           "Thumb-2 mode"), str);
22609               return;
22610             }
22611         }
22612
22613       inst.instruction = opcode->tvalue;
22614
22615       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
22616         {
22617           /* Prepare the pred_insn_type for those encodings that don't set
22618              it.  */
22619           it_fsm_pre_encode ();
22620
22621           opcode->tencode ();
22622
22623           it_fsm_post_encode ();
22624         }
22625
22626       if (!(inst.error || inst.relax))
22627         {
22628           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
22629           inst.size = (inst.instruction > 0xffff ? 4 : 2);
22630           if (inst.size_req && inst.size_req != inst.size)
22631             {
22632               as_bad (_("cannot honor width suffix -- `%s'"), str);
22633               return;
22634             }
22635         }
22636
22637       /* Something has gone badly wrong if we try to relax a fixed size
22638          instruction.  */
22639       gas_assert (inst.size_req == 0 || !inst.relax);
22640
22641       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
22642                               *opcode->tvariant);
22643       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
22644          set those bits when Thumb-2 32-bit instructions are seen.  The impact
22645          of relaxable instructions will be considered later after we finish all
22646          relaxation.  */
22647       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
22648         variant = arm_arch_none;
22649       else
22650         variant = cpu_variant;
22651       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
22652         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
22653                                 arm_ext_v6t2);
22654
22655       check_neon_suffixes;
22656
22657       if (!inst.error)
22658         {
22659           mapping_state (MAP_THUMB);
22660         }
22661     }
22662   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
22663     {
22664       bfd_boolean is_bx;
22665
22666       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
22667       is_bx = (opcode->aencode == do_bx);
22668
22669       /* Check that this instruction is supported for this CPU.  */
22670       if (!(is_bx && fix_v4bx)
22671           && !(opcode->avariant &&
22672                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
22673         {
22674           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
22675           return;
22676         }
22677       if (inst.size_req)
22678         {
22679           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
22680           return;
22681         }
22682
22683       inst.instruction = opcode->avalue;
22684       if (opcode->tag == OT_unconditionalF)
22685         inst.instruction |= 0xFU << 28;
22686       else
22687         inst.instruction |= inst.cond << 28;
22688       inst.size = INSN_SIZE;
22689       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
22690         {
22691           it_fsm_pre_encode ();
22692           opcode->aencode ();
22693           it_fsm_post_encode ();
22694         }
22695       /* Arm mode bx is marked as both v4T and v5 because it's still required
22696          on a hypothetical non-thumb v5 core.  */
22697       if (is_bx)
22698         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
22699       else
22700         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
22701                                 *opcode->avariant);
22702
22703       check_neon_suffixes;
22704
22705       if (!inst.error)
22706         {
22707           mapping_state (MAP_ARM);
22708         }
22709     }
22710   else
22711     {
22712       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
22713                 "-- `%s'"), str);
22714       return;
22715     }
22716   output_inst (str);
22717 }
22718
22719 static void
22720 check_pred_blocks_finished (void)
22721 {
22722 #ifdef OBJ_ELF
22723   asection *sect;
22724
22725   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
22726     if (seg_info (sect)->tc_segment_info_data.current_pred.state
22727         == MANUAL_PRED_BLOCK)
22728       {
22729         if (now_pred.type == SCALAR_PRED)
22730           as_warn (_("section '%s' finished with an open IT block."),
22731                    sect->name);
22732         else
22733           as_warn (_("section '%s' finished with an open VPT/VPST block."),
22734                    sect->name);
22735       }
22736 #else
22737   if (now_pred.state == MANUAL_PRED_BLOCK)
22738     {
22739       if (now_pred.type == SCALAR_PRED)
22740        as_warn (_("file finished with an open IT block."));
22741       else
22742         as_warn (_("file finished with an open VPT/VPST block."));
22743     }
22744 #endif
22745 }
22746
22747 /* Various frobbings of labels and their addresses.  */
22748
22749 void
22750 arm_start_line_hook (void)
22751 {
22752   last_label_seen = NULL;
22753 }
22754
22755 void
22756 arm_frob_label (symbolS * sym)
22757 {
22758   last_label_seen = sym;
22759
22760   ARM_SET_THUMB (sym, thumb_mode);
22761
22762 #if defined OBJ_COFF || defined OBJ_ELF
22763   ARM_SET_INTERWORK (sym, support_interwork);
22764 #endif
22765
22766   force_automatic_it_block_close ();
22767
22768   /* Note - do not allow local symbols (.Lxxx) to be labelled
22769      as Thumb functions.  This is because these labels, whilst
22770      they exist inside Thumb code, are not the entry points for
22771      possible ARM->Thumb calls.  Also, these labels can be used
22772      as part of a computed goto or switch statement.  eg gcc
22773      can generate code that looks like this:
22774
22775                 ldr  r2, [pc, .Laaa]
22776                 lsl  r3, r3, #2
22777                 ldr  r2, [r3, r2]
22778                 mov  pc, r2
22779
22780        .Lbbb:  .word .Lxxx
22781        .Lccc:  .word .Lyyy
22782        ..etc...
22783        .Laaa:   .word Lbbb
22784
22785      The first instruction loads the address of the jump table.
22786      The second instruction converts a table index into a byte offset.
22787      The third instruction gets the jump address out of the table.
22788      The fourth instruction performs the jump.
22789
22790      If the address stored at .Laaa is that of a symbol which has the
22791      Thumb_Func bit set, then the linker will arrange for this address
22792      to have the bottom bit set, which in turn would mean that the
22793      address computation performed by the third instruction would end
22794      up with the bottom bit set.  Since the ARM is capable of unaligned
22795      word loads, the instruction would then load the incorrect address
22796      out of the jump table, and chaos would ensue.  */
22797   if (label_is_thumb_function_name
22798       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
22799       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
22800     {
22801       /* When the address of a Thumb function is taken the bottom
22802          bit of that address should be set.  This will allow
22803          interworking between Arm and Thumb functions to work
22804          correctly.  */
22805
22806       THUMB_SET_FUNC (sym, 1);
22807
22808       label_is_thumb_function_name = FALSE;
22809     }
22810
22811   dwarf2_emit_label (sym);
22812 }
22813
22814 bfd_boolean
22815 arm_data_in_code (void)
22816 {
22817   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
22818     {
22819       *input_line_pointer = '/';
22820       input_line_pointer += 5;
22821       *input_line_pointer = 0;
22822       return TRUE;
22823     }
22824
22825   return FALSE;
22826 }
22827
22828 char *
22829 arm_canonicalize_symbol_name (char * name)
22830 {
22831   int len;
22832
22833   if (thumb_mode && (len = strlen (name)) > 5
22834       && streq (name + len - 5, "/data"))
22835     *(name + len - 5) = 0;
22836
22837   return name;
22838 }
22839 \f
22840 /* Table of all register names defined by default.  The user can
22841    define additional names with .req.  Note that all register names
22842    should appear in both upper and lowercase variants.  Some registers
22843    also have mixed-case names.  */
22844
22845 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
22846 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
22847 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
22848 #define REGSET(p,t) \
22849   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
22850   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
22851   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
22852   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
22853 #define REGSETH(p,t) \
22854   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
22855   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
22856   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
22857   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
22858 #define REGSET2(p,t) \
22859   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
22860   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
22861   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
22862   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
22863 #define SPLRBANK(base,bank,t) \
22864   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
22865   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
22866   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
22867   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
22868   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
22869   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
22870
22871 static const struct reg_entry reg_names[] =
22872 {
22873   /* ARM integer registers.  */
22874   REGSET(r, RN), REGSET(R, RN),
22875
22876   /* ATPCS synonyms.  */
22877   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
22878   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
22879   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
22880
22881   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
22882   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
22883   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
22884
22885   /* Well-known aliases.  */
22886   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
22887   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
22888
22889   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
22890   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
22891
22892   /* Defining the new Zero register from ARMv8.1-M.  */
22893   REGDEF(zr,15,ZR),
22894   REGDEF(ZR,15,ZR),
22895
22896   /* Coprocessor numbers.  */
22897   REGSET(p, CP), REGSET(P, CP),
22898
22899   /* Coprocessor register numbers.  The "cr" variants are for backward
22900      compatibility.  */
22901   REGSET(c,  CN), REGSET(C, CN),
22902   REGSET(cr, CN), REGSET(CR, CN),
22903
22904   /* ARM banked registers.  */
22905   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
22906   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
22907   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
22908   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
22909   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
22910   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
22911   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
22912
22913   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
22914   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
22915   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
22916   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
22917   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
22918   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
22919   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
22920   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
22921
22922   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
22923   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
22924   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
22925   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
22926   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
22927   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
22928   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
22929   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
22930   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
22931
22932   /* FPA registers.  */
22933   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
22934   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
22935
22936   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
22937   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
22938
22939   /* VFP SP registers.  */
22940   REGSET(s,VFS),  REGSET(S,VFS),
22941   REGSETH(s,VFS), REGSETH(S,VFS),
22942
22943   /* VFP DP Registers.  */
22944   REGSET(d,VFD),  REGSET(D,VFD),
22945   /* Extra Neon DP registers.  */
22946   REGSETH(d,VFD), REGSETH(D,VFD),
22947
22948   /* Neon QP registers.  */
22949   REGSET2(q,NQ),  REGSET2(Q,NQ),
22950
22951   /* VFP control registers.  */
22952   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
22953   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
22954   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
22955   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
22956   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
22957   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
22958   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
22959   REGDEF(fpscr_nzcvqc,2,VFC), REGDEF(FPSCR_nzcvqc,2,VFC),
22960   REGDEF(vpr,12,VFC), REGDEF(VPR,12,VFC),
22961   REGDEF(fpcxt_ns,14,VFC), REGDEF(FPCXT_NS,14,VFC),
22962   REGDEF(fpcxt_s,15,VFC), REGDEF(FPCXT_S,15,VFC),
22963
22964   /* Maverick DSP coprocessor registers.  */
22965   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
22966   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
22967
22968   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
22969   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
22970   REGDEF(dspsc,0,DSPSC),
22971
22972   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
22973   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
22974   REGDEF(DSPSC,0,DSPSC),
22975
22976   /* iWMMXt data registers - p0, c0-15.  */
22977   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
22978
22979   /* iWMMXt control registers - p1, c0-3.  */
22980   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
22981   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
22982   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
22983   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
22984
22985   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
22986   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
22987   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
22988   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
22989   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
22990
22991   /* XScale accumulator registers.  */
22992   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
22993 };
22994 #undef REGDEF
22995 #undef REGNUM
22996 #undef REGSET
22997
22998 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
22999    within psr_required_here.  */
23000 static const struct asm_psr psrs[] =
23001 {
23002   /* Backward compatibility notation.  Note that "all" is no longer
23003      truly all possible PSR bits.  */
23004   {"all",  PSR_c | PSR_f},
23005   {"flg",  PSR_f},
23006   {"ctl",  PSR_c},
23007
23008   /* Individual flags.  */
23009   {"f",    PSR_f},
23010   {"c",    PSR_c},
23011   {"x",    PSR_x},
23012   {"s",    PSR_s},
23013
23014   /* Combinations of flags.  */
23015   {"fs",   PSR_f | PSR_s},
23016   {"fx",   PSR_f | PSR_x},
23017   {"fc",   PSR_f | PSR_c},
23018   {"sf",   PSR_s | PSR_f},
23019   {"sx",   PSR_s | PSR_x},
23020   {"sc",   PSR_s | PSR_c},
23021   {"xf",   PSR_x | PSR_f},
23022   {"xs",   PSR_x | PSR_s},
23023   {"xc",   PSR_x | PSR_c},
23024   {"cf",   PSR_c | PSR_f},
23025   {"cs",   PSR_c | PSR_s},
23026   {"cx",   PSR_c | PSR_x},
23027   {"fsx",  PSR_f | PSR_s | PSR_x},
23028   {"fsc",  PSR_f | PSR_s | PSR_c},
23029   {"fxs",  PSR_f | PSR_x | PSR_s},
23030   {"fxc",  PSR_f | PSR_x | PSR_c},
23031   {"fcs",  PSR_f | PSR_c | PSR_s},
23032   {"fcx",  PSR_f | PSR_c | PSR_x},
23033   {"sfx",  PSR_s | PSR_f | PSR_x},
23034   {"sfc",  PSR_s | PSR_f | PSR_c},
23035   {"sxf",  PSR_s | PSR_x | PSR_f},
23036   {"sxc",  PSR_s | PSR_x | PSR_c},
23037   {"scf",  PSR_s | PSR_c | PSR_f},
23038   {"scx",  PSR_s | PSR_c | PSR_x},
23039   {"xfs",  PSR_x | PSR_f | PSR_s},
23040   {"xfc",  PSR_x | PSR_f | PSR_c},
23041   {"xsf",  PSR_x | PSR_s | PSR_f},
23042   {"xsc",  PSR_x | PSR_s | PSR_c},
23043   {"xcf",  PSR_x | PSR_c | PSR_f},
23044   {"xcs",  PSR_x | PSR_c | PSR_s},
23045   {"cfs",  PSR_c | PSR_f | PSR_s},
23046   {"cfx",  PSR_c | PSR_f | PSR_x},
23047   {"csf",  PSR_c | PSR_s | PSR_f},
23048   {"csx",  PSR_c | PSR_s | PSR_x},
23049   {"cxf",  PSR_c | PSR_x | PSR_f},
23050   {"cxs",  PSR_c | PSR_x | PSR_s},
23051   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
23052   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
23053   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
23054   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
23055   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
23056   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
23057   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
23058   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
23059   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
23060   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
23061   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
23062   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
23063   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
23064   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
23065   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
23066   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
23067   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
23068   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
23069   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
23070   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
23071   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
23072   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
23073   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
23074   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
23075 };
23076
23077 /* Table of V7M psr names.  */
23078 static const struct asm_psr v7m_psrs[] =
23079 {
23080   {"apsr",         0x0 }, {"APSR",         0x0 },
23081   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
23082   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
23083   {"psr",          0x3 }, {"PSR",          0x3 },
23084   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
23085   {"ipsr",         0x5 }, {"IPSR",         0x5 },
23086   {"epsr",         0x6 }, {"EPSR",         0x6 },
23087   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
23088   {"msp",          0x8 }, {"MSP",          0x8 },
23089   {"psp",          0x9 }, {"PSP",          0x9 },
23090   {"msplim",       0xa }, {"MSPLIM",       0xa },
23091   {"psplim",       0xb }, {"PSPLIM",       0xb },
23092   {"primask",      0x10}, {"PRIMASK",      0x10},
23093   {"basepri",      0x11}, {"BASEPRI",      0x11},
23094   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
23095   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
23096   {"control",      0x14}, {"CONTROL",      0x14},
23097   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
23098   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
23099   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
23100   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
23101   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
23102   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
23103   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
23104   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
23105   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
23106 };
23107
23108 /* Table of all shift-in-operand names.  */
23109 static const struct asm_shift_name shift_names [] =
23110 {
23111   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
23112   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
23113   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
23114   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
23115   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
23116   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX },
23117   { "uxtw", SHIFT_UXTW}, { "UXTW", SHIFT_UXTW}
23118 };
23119
23120 /* Table of all explicit relocation names.  */
23121 #ifdef OBJ_ELF
23122 static struct reloc_entry reloc_names[] =
23123 {
23124   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
23125   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
23126   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
23127   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
23128   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
23129   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
23130   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
23131   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
23132   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
23133   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
23134   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
23135   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
23136   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
23137         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
23138   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
23139         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
23140   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
23141         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ},
23142   { "gotfuncdesc", BFD_RELOC_ARM_GOTFUNCDESC },
23143         { "GOTFUNCDESC", BFD_RELOC_ARM_GOTFUNCDESC },
23144   { "gotofffuncdesc", BFD_RELOC_ARM_GOTOFFFUNCDESC },
23145         { "GOTOFFFUNCDESC", BFD_RELOC_ARM_GOTOFFFUNCDESC },
23146   { "funcdesc", BFD_RELOC_ARM_FUNCDESC },
23147         { "FUNCDESC", BFD_RELOC_ARM_FUNCDESC },
23148    { "tlsgd_fdpic", BFD_RELOC_ARM_TLS_GD32_FDPIC },      { "TLSGD_FDPIC", BFD_RELOC_ARM_TLS_GD32_FDPIC },
23149    { "tlsldm_fdpic", BFD_RELOC_ARM_TLS_LDM32_FDPIC },    { "TLSLDM_FDPIC", BFD_RELOC_ARM_TLS_LDM32_FDPIC },
23150    { "gottpoff_fdpic", BFD_RELOC_ARM_TLS_IE32_FDPIC },   { "GOTTPOFF_FDIC", BFD_RELOC_ARM_TLS_IE32_FDPIC },
23151 };
23152 #endif
23153
23154 /* Table of all conditional affixes.  */
23155 static const struct asm_cond conds[] =
23156 {
23157   {"eq", 0x0},
23158   {"ne", 0x1},
23159   {"cs", 0x2}, {"hs", 0x2},
23160   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
23161   {"mi", 0x4},
23162   {"pl", 0x5},
23163   {"vs", 0x6},
23164   {"vc", 0x7},
23165   {"hi", 0x8},
23166   {"ls", 0x9},
23167   {"ge", 0xa},
23168   {"lt", 0xb},
23169   {"gt", 0xc},
23170   {"le", 0xd},
23171   {"al", 0xe}
23172 };
23173 static const struct asm_cond vconds[] =
23174 {
23175     {"t", 0xf},
23176     {"e", 0x10}
23177 };
23178
23179 #define UL_BARRIER(L,U,CODE,FEAT) \
23180   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
23181   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
23182
23183 static struct asm_barrier_opt barrier_opt_names[] =
23184 {
23185   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
23186   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
23187   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
23188   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
23189   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
23190   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
23191   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
23192   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
23193   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
23194   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
23195   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
23196   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
23197   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
23198   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
23199   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
23200   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
23201 };
23202
23203 #undef UL_BARRIER
23204
23205 /* Table of ARM-format instructions.    */
23206
23207 /* Macros for gluing together operand strings.  N.B. In all cases
23208    other than OPS0, the trailing OP_stop comes from default
23209    zero-initialization of the unspecified elements of the array.  */
23210 #define OPS0()            { OP_stop, }
23211 #define OPS1(a)           { OP_##a, }
23212 #define OPS2(a,b)         { OP_##a,OP_##b, }
23213 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
23214 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
23215 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
23216 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
23217
23218 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
23219    This is useful when mixing operands for ARM and THUMB, i.e. using the
23220    MIX_ARM_THUMB_OPERANDS macro.
23221    In order to use these macros, prefix the number of operands with _
23222    e.g. _3.  */
23223 #define OPS_1(a)           { a, }
23224 #define OPS_2(a,b)         { a,b, }
23225 #define OPS_3(a,b,c)       { a,b,c, }
23226 #define OPS_4(a,b,c,d)     { a,b,c,d, }
23227 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
23228 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
23229
23230 /* These macros abstract out the exact format of the mnemonic table and
23231    save some repeated characters.  */
23232
23233 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
23234 #define TxCE(mnem, op, top, nops, ops, ae, te) \
23235   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
23236     THUMB_VARIANT, do_##ae, do_##te, 0 }
23237
23238 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
23239    a T_MNEM_xyz enumerator.  */
23240 #define TCE(mnem, aop, top, nops, ops, ae, te) \
23241       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
23242 #define tCE(mnem, aop, top, nops, ops, ae, te) \
23243       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23244
23245 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
23246    infix after the third character.  */
23247 #define TxC3(mnem, op, top, nops, ops, ae, te) \
23248   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
23249     THUMB_VARIANT, do_##ae, do_##te, 0 }
23250 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
23251   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
23252     THUMB_VARIANT, do_##ae, do_##te, 0 }
23253 #define TC3(mnem, aop, top, nops, ops, ae, te) \
23254       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
23255 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
23256       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
23257 #define tC3(mnem, aop, top, nops, ops, ae, te) \
23258       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23259 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
23260       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23261
23262 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
23263    field is still 0xE.  Many of the Thumb variants can be executed
23264    conditionally, so this is checked separately.  */
23265 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
23266   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
23267     THUMB_VARIANT, do_##ae, do_##te, 0 }
23268
23269 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
23270    Used by mnemonics that have very minimal differences in the encoding for
23271    ARM and Thumb variants and can be handled in a common function.  */
23272 #define TUEc(mnem, op, top, nops, ops, en) \
23273   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
23274     THUMB_VARIANT, do_##en, do_##en, 0 }
23275
23276 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
23277    condition code field.  */
23278 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
23279   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
23280     THUMB_VARIANT, do_##ae, do_##te, 0 }
23281
23282 /* ARM-only variants of all the above.  */
23283 #define CE(mnem,  op, nops, ops, ae)    \
23284   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23285
23286 #define C3(mnem, op, nops, ops, ae)     \
23287   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23288
23289 /* Thumb-only variants of TCE and TUE.  */
23290 #define ToC(mnem, top, nops, ops, te) \
23291   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
23292     do_##te, 0 }
23293
23294 #define ToU(mnem, top, nops, ops, te) \
23295   { mnem, OPS##nops ops, OT_unconditional, 0x0, 0x##top, 0, THUMB_VARIANT, \
23296     NULL, do_##te, 0 }
23297
23298 /* T_MNEM_xyz enumerator variants of ToC.  */
23299 #define toC(mnem, top, nops, ops, te) \
23300   { mnem, OPS##nops ops, OT_csuffix, 0x0, T_MNEM##top, 0, THUMB_VARIANT, NULL, \
23301     do_##te, 0 }
23302
23303 /* T_MNEM_xyz enumerator variants of ToU.  */
23304 #define toU(mnem, top, nops, ops, te) \
23305   { mnem, OPS##nops ops, OT_unconditional, 0x0, T_MNEM##top, 0, THUMB_VARIANT, \
23306     NULL, do_##te, 0 }
23307
23308 /* Legacy mnemonics that always have conditional infix after the third
23309    character.  */
23310 #define CL(mnem, op, nops, ops, ae)     \
23311   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
23312     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23313
23314 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
23315 #define cCE(mnem,  op, nops, ops, ae)   \
23316   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23317
23318 /* mov instructions that are shared between coprocessor and MVE.  */
23319 #define mcCE(mnem,  op, nops, ops, ae)  \
23320   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##ae, 0 }
23321
23322 /* Legacy coprocessor instructions where conditional infix and conditional
23323    suffix are ambiguous.  For consistency this includes all FPA instructions,
23324    not just the potentially ambiguous ones.  */
23325 #define cCL(mnem, op, nops, ops, ae)    \
23326   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
23327     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23328
23329 /* Coprocessor, takes either a suffix or a position-3 infix
23330    (for an FPA corner case). */
23331 #define C3E(mnem, op, nops, ops, ae) \
23332   { mnem, OPS##nops ops, OT_csuf_or_in3, \
23333     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23334
23335 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
23336   { m1 #m2 m3, OPS##nops ops, \
23337     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
23338     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23339
23340 #define CM(m1, m2, op, nops, ops, ae)   \
23341   xCM_ (m1,   , m2, op, nops, ops, ae), \
23342   xCM_ (m1, eq, m2, op, nops, ops, ae), \
23343   xCM_ (m1, ne, m2, op, nops, ops, ae), \
23344   xCM_ (m1, cs, m2, op, nops, ops, ae), \
23345   xCM_ (m1, hs, m2, op, nops, ops, ae), \
23346   xCM_ (m1, cc, m2, op, nops, ops, ae), \
23347   xCM_ (m1, ul, m2, op, nops, ops, ae), \
23348   xCM_ (m1, lo, m2, op, nops, ops, ae), \
23349   xCM_ (m1, mi, m2, op, nops, ops, ae), \
23350   xCM_ (m1, pl, m2, op, nops, ops, ae), \
23351   xCM_ (m1, vs, m2, op, nops, ops, ae), \
23352   xCM_ (m1, vc, m2, op, nops, ops, ae), \
23353   xCM_ (m1, hi, m2, op, nops, ops, ae), \
23354   xCM_ (m1, ls, m2, op, nops, ops, ae), \
23355   xCM_ (m1, ge, m2, op, nops, ops, ae), \
23356   xCM_ (m1, lt, m2, op, nops, ops, ae), \
23357   xCM_ (m1, gt, m2, op, nops, ops, ae), \
23358   xCM_ (m1, le, m2, op, nops, ops, ae), \
23359   xCM_ (m1, al, m2, op, nops, ops, ae)
23360
23361 #define UE(mnem, op, nops, ops, ae)     \
23362   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23363
23364 #define UF(mnem, op, nops, ops, ae)     \
23365   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23366
23367 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
23368    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
23369    use the same encoding function for each.  */
23370 #define NUF(mnem, op, nops, ops, enc)                                   \
23371   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
23372     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
23373
23374 /* Neon data processing, version which indirects through neon_enc_tab for
23375    the various overloaded versions of opcodes.  */
23376 #define nUF(mnem, op, nops, ops, enc)                                   \
23377   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
23378     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
23379
23380 /* Neon insn with conditional suffix for the ARM version, non-overloaded
23381    version.  */
23382 #define NCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
23383   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
23384     THUMB_VARIANT, do_##enc, do_##enc, mve_p }
23385
23386 #define NCE(mnem, op, nops, ops, enc)                                   \
23387    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
23388
23389 #define NCEF(mnem, op, nops, ops, enc)                                  \
23390     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
23391
23392 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
23393 #define nCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
23394   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
23395     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, mve_p }
23396
23397 #define nCE(mnem, op, nops, ops, enc)                                   \
23398    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
23399
23400 #define nCEF(mnem, op, nops, ops, enc)                                  \
23401     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
23402
23403 /*   */
23404 #define mCEF(mnem, op, nops, ops, enc)                          \
23405   { #mnem, OPS##nops ops, OT_csuffixF, M_MNEM##op, M_MNEM##op,  \
23406     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23407
23408
23409 /* nCEF but for MVE predicated instructions.  */
23410 #define mnCEF(mnem, op, nops, ops, enc)                                 \
23411     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
23412
23413 /* nCE but for MVE predicated instructions.  */
23414 #define mnCE(mnem, op, nops, ops, enc)                                  \
23415    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
23416
23417 /* NUF but for potentially MVE predicated instructions.  */
23418 #define MNUF(mnem, op, nops, ops, enc)                                  \
23419   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
23420     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23421
23422 /* nUF but for potentially MVE predicated instructions.  */
23423 #define mnUF(mnem, op, nops, ops, enc)                                  \
23424   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
23425     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23426
23427 /* ToC but for potentially MVE predicated instructions.  */
23428 #define mToC(mnem, top, nops, ops, te) \
23429   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
23430     do_##te, 1 }
23431
23432 /* NCE but for MVE predicated instructions.  */
23433 #define MNCE(mnem, op, nops, ops, enc)                                  \
23434    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
23435
23436 /* NCEF but for MVE predicated instructions.  */
23437 #define MNCEF(mnem, op, nops, ops, enc)                                 \
23438     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
23439 #define do_0 0
23440
23441 static const struct asm_opcode insns[] =
23442 {
23443 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
23444 #define THUMB_VARIANT  & arm_ext_v4t
23445  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
23446  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
23447  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
23448  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
23449  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
23450  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
23451  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
23452  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
23453  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
23454  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
23455  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
23456  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
23457  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
23458  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
23459  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
23460  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
23461
23462  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
23463     for setting PSR flag bits.  They are obsolete in V6 and do not
23464     have Thumb equivalents. */
23465  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
23466  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
23467   CL("tstp",    110f000,           2, (RR, SH),      cmp),
23468  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
23469  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
23470   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
23471  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
23472  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
23473   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
23474
23475  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
23476  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
23477  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
23478  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
23479
23480  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
23481  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
23482  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
23483                                                                 OP_RRnpc),
23484                                         OP_ADDRGLDR),ldst, t_ldst),
23485  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
23486
23487  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23488  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23489  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23490  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23491  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23492  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23493
23494  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
23495  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
23496
23497   /* Pseudo ops.  */
23498  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
23499   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
23500  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
23501  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
23502
23503   /* Thumb-compatibility pseudo ops.  */
23504  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
23505  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
23506  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
23507  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
23508  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
23509  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
23510  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
23511  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
23512  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
23513  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
23514  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
23515  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
23516
23517  /* These may simplify to neg.  */
23518  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
23519  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
23520
23521 #undef THUMB_VARIANT
23522 #define THUMB_VARIANT  & arm_ext_os
23523
23524  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
23525  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
23526
23527 #undef  THUMB_VARIANT
23528 #define THUMB_VARIANT  & arm_ext_v6
23529
23530  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
23531
23532  /* V1 instructions with no Thumb analogue prior to V6T2.  */
23533 #undef  THUMB_VARIANT
23534 #define THUMB_VARIANT  & arm_ext_v6t2
23535
23536  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
23537  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
23538   CL("teqp",    130f000,           2, (RR, SH),      cmp),
23539
23540  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23541  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23542  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
23543  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23544
23545  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23546  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23547
23548  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23549  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23550
23551  /* V1 instructions with no Thumb analogue at all.  */
23552   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
23553   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
23554
23555   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
23556   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
23557   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
23558   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
23559   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
23560   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
23561   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
23562   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
23563
23564 #undef  ARM_VARIANT
23565 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
23566 #undef  THUMB_VARIANT
23567 #define THUMB_VARIANT  & arm_ext_v4t
23568
23569  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
23570  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
23571
23572 #undef  THUMB_VARIANT
23573 #define THUMB_VARIANT  & arm_ext_v6t2
23574
23575  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
23576   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
23577
23578   /* Generic coprocessor instructions.  */
23579  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
23580  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23581  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23582  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23583  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23584  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23585  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
23586
23587 #undef  ARM_VARIANT
23588 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
23589
23590   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
23591   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
23592
23593 #undef  ARM_VARIANT
23594 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
23595 #undef  THUMB_VARIANT
23596 #define THUMB_VARIANT  & arm_ext_msr
23597
23598  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
23599  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
23600
23601 #undef  ARM_VARIANT
23602 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
23603 #undef  THUMB_VARIANT
23604 #define THUMB_VARIANT  & arm_ext_v6t2
23605
23606  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23607   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23608  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23609   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23610  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23611   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23612  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23613   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23614
23615 #undef  ARM_VARIANT
23616 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
23617 #undef  THUMB_VARIANT
23618 #define THUMB_VARIANT  & arm_ext_v4t
23619
23620  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23621  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23622  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23623  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23624  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23625  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23626
23627 #undef  ARM_VARIANT
23628 #define ARM_VARIANT  & arm_ext_v4t_5
23629
23630   /* ARM Architecture 4T.  */
23631   /* Note: bx (and blx) are required on V5, even if the processor does
23632      not support Thumb.  */
23633  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
23634
23635 #undef  ARM_VARIANT
23636 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
23637 #undef  THUMB_VARIANT
23638 #define THUMB_VARIANT  & arm_ext_v5t
23639
23640   /* Note: blx has 2 variants; the .value coded here is for
23641      BLX(2).  Only this variant has conditional execution.  */
23642  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
23643  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
23644
23645 #undef  THUMB_VARIANT
23646 #define THUMB_VARIANT  & arm_ext_v6t2
23647
23648  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
23649  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23650  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
23651  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23652  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
23653  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
23654  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23655  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23656
23657 #undef  ARM_VARIANT
23658 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
23659 #undef  THUMB_VARIANT
23660 #define THUMB_VARIANT  & arm_ext_v5exp
23661
23662  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23663  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23664  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23665  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23666
23667  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23668  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23669
23670  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23671  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23672  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23673  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23674
23675  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23676  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23677  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23678  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23679
23680  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23681  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23682
23683  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23684  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23685  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23686  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23687
23688 #undef  ARM_VARIANT
23689 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
23690 #undef  THUMB_VARIANT
23691 #define THUMB_VARIANT  & arm_ext_v6t2
23692
23693  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
23694  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
23695      ldrd, t_ldstd),
23696  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
23697                                        ADDRGLDRS), ldrd, t_ldstd),
23698
23699  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23700  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23701
23702 #undef  ARM_VARIANT
23703 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
23704
23705  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
23706
23707 #undef  ARM_VARIANT
23708 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
23709 #undef  THUMB_VARIANT
23710 #define THUMB_VARIANT  & arm_ext_v6
23711
23712  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
23713  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
23714  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23715  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23716  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23717  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23718  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23719  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23720  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23721  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
23722
23723 #undef  THUMB_VARIANT
23724 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23725
23726  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
23727  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23728                                       strex,  t_strex),
23729 #undef  THUMB_VARIANT
23730 #define THUMB_VARIANT  & arm_ext_v6t2
23731
23732  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23733  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23734
23735  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
23736  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
23737
23738 /*  ARM V6 not included in V7M.  */
23739 #undef  THUMB_VARIANT
23740 #define THUMB_VARIANT  & arm_ext_v6_notm
23741  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23742  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23743   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
23744   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
23745  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
23746  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23747   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
23748  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
23749   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
23750  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23751  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23752  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23753   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
23754   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
23755   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
23756   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
23757  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
23758  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
23759  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
23760
23761 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
23762 #undef  THUMB_VARIANT
23763 #define THUMB_VARIANT  & arm_ext_v6_dsp
23764  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
23765  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
23766  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23767  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23768  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23769  /* Old name for QASX.  */
23770  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23771  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23772  /* Old name for QSAX.  */
23773  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23774  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23775  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23776  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23777  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23778  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23779  /* Old name for SASX.  */
23780  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23781  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23782  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23783  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23784  /* Old name for SHASX.  */
23785  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23786  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23787  /* Old name for SHSAX.  */
23788  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23789  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23790  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23791  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23792  /* Old name for SSAX.  */
23793  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23794  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23795  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23796  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23797  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23798  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23799  /* Old name for UASX.  */
23800  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23801  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23802  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23803  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23804  /* Old name for UHASX.  */
23805  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23806  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23807  /* Old name for UHSAX.  */
23808  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23809  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23810  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23811  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23812  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23813  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23814  /* Old name for UQASX.  */
23815  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23816  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23817  /* Old name for UQSAX.  */
23818  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23819  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23820  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23821  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23822  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23823  /* Old name for USAX.  */
23824  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23825  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23826  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23827  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23828  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23829  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
23830  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23831  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23832  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23833  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
23834  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23835  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23836  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23837  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23838  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23839  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23840  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23841  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23842  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23843  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23844  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23845  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23846  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23847  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23848  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23849  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23850  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23851  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23852  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23853  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
23854  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
23855  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
23856  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
23857  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
23858
23859 #undef  ARM_VARIANT
23860 #define ARM_VARIANT   & arm_ext_v6k_v6t2
23861 #undef  THUMB_VARIANT
23862 #define THUMB_VARIANT & arm_ext_v6k_v6t2
23863
23864  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
23865  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
23866  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
23867  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
23868
23869 #undef  THUMB_VARIANT
23870 #define THUMB_VARIANT  & arm_ext_v6_notm
23871  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
23872                                       ldrexd, t_ldrexd),
23873  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
23874                                        RRnpcb), strexd, t_strexd),
23875
23876 #undef  THUMB_VARIANT
23877 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23878  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
23879      rd_rn,  rd_rn),
23880  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
23881      rd_rn,  rd_rn),
23882  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23883      strex, t_strexbh),
23884  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23885      strex, t_strexbh),
23886  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
23887
23888 #undef  ARM_VARIANT
23889 #define ARM_VARIANT    & arm_ext_sec
23890 #undef  THUMB_VARIANT
23891 #define THUMB_VARIANT  & arm_ext_sec
23892
23893  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
23894
23895 #undef  ARM_VARIANT
23896 #define ARM_VARIANT    & arm_ext_virt
23897 #undef  THUMB_VARIANT
23898 #define THUMB_VARIANT    & arm_ext_virt
23899
23900  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
23901  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
23902
23903 #undef  ARM_VARIANT
23904 #define ARM_VARIANT    & arm_ext_pan
23905 #undef  THUMB_VARIANT
23906 #define THUMB_VARIANT  & arm_ext_pan
23907
23908  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
23909
23910 #undef  ARM_VARIANT
23911 #define ARM_VARIANT    & arm_ext_v6t2
23912 #undef  THUMB_VARIANT
23913 #define THUMB_VARIANT  & arm_ext_v6t2
23914
23915  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
23916  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
23917  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
23918  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
23919
23920  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
23921  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
23922
23923  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23924  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23925  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23926  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23927
23928 #undef  ARM_VARIANT
23929 #define ARM_VARIANT    & arm_ext_v3
23930 #undef  THUMB_VARIANT
23931 #define THUMB_VARIANT  & arm_ext_v6t2
23932
23933  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
23934  TUF("ssbb",    57ff040, f3bf8f40, 0, (), noargs, t_csdb),
23935  TUF("pssbb",   57ff044, f3bf8f44, 0, (), noargs, t_csdb),
23936
23937 #undef  ARM_VARIANT
23938 #define ARM_VARIANT    & arm_ext_v6t2
23939 #undef  THUMB_VARIANT
23940 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23941  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
23942  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
23943
23944  /* Thumb-only instructions.  */
23945 #undef  ARM_VARIANT
23946 #define ARM_VARIANT NULL
23947   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
23948   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
23949
23950  /* ARM does not really have an IT instruction, so always allow it.
23951     The opcode is copied from Thumb in order to allow warnings in
23952     -mimplicit-it=[never | arm] modes.  */
23953 #undef  ARM_VARIANT
23954 #define ARM_VARIANT  & arm_ext_v1
23955 #undef  THUMB_VARIANT
23956 #define THUMB_VARIANT  & arm_ext_v6t2
23957
23958  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
23959  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
23960  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
23961  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
23962  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
23963  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
23964  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
23965  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
23966  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
23967  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
23968  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
23969  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
23970  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
23971  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
23972  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
23973  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
23974  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
23975  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
23976
23977  /* Thumb2 only instructions.  */
23978 #undef  ARM_VARIANT
23979 #define ARM_VARIANT  NULL
23980
23981  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
23982  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
23983  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
23984  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
23985  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
23986  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
23987
23988  /* Hardware division instructions.  */
23989 #undef  ARM_VARIANT
23990 #define ARM_VARIANT    & arm_ext_adiv
23991 #undef  THUMB_VARIANT
23992 #define THUMB_VARIANT  & arm_ext_div
23993
23994  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
23995  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
23996
23997  /* ARM V6M/V7 instructions.  */
23998 #undef  ARM_VARIANT
23999 #define ARM_VARIANT    & arm_ext_barrier
24000 #undef  THUMB_VARIANT
24001 #define THUMB_VARIANT  & arm_ext_barrier
24002
24003  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
24004  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
24005  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
24006
24007  /* ARM V7 instructions.  */
24008 #undef  ARM_VARIANT
24009 #define ARM_VARIANT    & arm_ext_v7
24010 #undef  THUMB_VARIANT
24011 #define THUMB_VARIANT  & arm_ext_v7
24012
24013  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
24014  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
24015
24016 #undef  ARM_VARIANT
24017 #define ARM_VARIANT    & arm_ext_mp
24018 #undef  THUMB_VARIANT
24019 #define THUMB_VARIANT  & arm_ext_mp
24020
24021  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
24022
24023  /* AArchv8 instructions.  */
24024 #undef  ARM_VARIANT
24025 #define ARM_VARIANT   & arm_ext_v8
24026
24027 /* Instructions shared between armv8-a and armv8-m.  */
24028 #undef  THUMB_VARIANT
24029 #define THUMB_VARIANT & arm_ext_atomics
24030
24031  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24032  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24033  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24034  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24035  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24036  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24037  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24038  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
24039  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24040  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
24041                                                         stlex,  t_stlex),
24042  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
24043                                                         stlex, t_stlex),
24044  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
24045                                                         stlex, t_stlex),
24046 #undef  THUMB_VARIANT
24047 #define THUMB_VARIANT & arm_ext_v8
24048
24049  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
24050  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
24051                                                         ldrexd, t_ldrexd),
24052  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
24053                                                         strexd, t_strexd),
24054
24055 /* Defined in V8 but is in undefined encoding space for earlier
24056    architectures.  However earlier architectures are required to treat
24057    this instuction as a semihosting trap as well.  Hence while not explicitly
24058    defined as such, it is in fact correct to define the instruction for all
24059    architectures.  */
24060 #undef  THUMB_VARIANT
24061 #define THUMB_VARIANT  & arm_ext_v1
24062 #undef  ARM_VARIANT
24063 #define ARM_VARIANT  & arm_ext_v1
24064  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
24065
24066  /* ARMv8 T32 only.  */
24067 #undef  ARM_VARIANT
24068 #define ARM_VARIANT  NULL
24069  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
24070  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
24071  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
24072
24073   /* FP for ARMv8.  */
24074 #undef  ARM_VARIANT
24075 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
24076 #undef  THUMB_VARIANT
24077 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
24078
24079   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
24080   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
24081   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
24082   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
24083   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
24084   mnCE(vrintz, _vrintr, 2, (RNSDQMQ, oRNSDQMQ),         vrintz),
24085   mnCE(vrintx, _vrintr, 2, (RNSDQMQ, oRNSDQMQ),         vrintx),
24086   mnUF(vrinta, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrinta),
24087   mnUF(vrintn, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintn),
24088   mnUF(vrintp, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintp),
24089   mnUF(vrintm, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintm),
24090
24091   /* Crypto v1 extensions.  */
24092 #undef  ARM_VARIANT
24093 #define ARM_VARIANT & fpu_crypto_ext_armv8
24094 #undef  THUMB_VARIANT
24095 #define THUMB_VARIANT & fpu_crypto_ext_armv8
24096
24097   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
24098   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
24099   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
24100   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
24101   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
24102   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
24103   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
24104   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
24105   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
24106   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
24107   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
24108   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
24109   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
24110   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
24111
24112 #undef  ARM_VARIANT
24113 #define ARM_VARIANT   & crc_ext_armv8
24114 #undef  THUMB_VARIANT
24115 #define THUMB_VARIANT & crc_ext_armv8
24116   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
24117   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
24118   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
24119   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
24120   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
24121   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
24122
24123  /* ARMv8.2 RAS extension.  */
24124 #undef  ARM_VARIANT
24125 #define ARM_VARIANT   & arm_ext_ras
24126 #undef  THUMB_VARIANT
24127 #define THUMB_VARIANT & arm_ext_ras
24128  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
24129
24130 #undef  ARM_VARIANT
24131 #define ARM_VARIANT   & arm_ext_v8_3
24132 #undef  THUMB_VARIANT
24133 #define THUMB_VARIANT & arm_ext_v8_3
24134  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
24135
24136 #undef  ARM_VARIANT
24137 #define ARM_VARIANT   & fpu_neon_ext_dotprod
24138 #undef  THUMB_VARIANT
24139 #define THUMB_VARIANT & fpu_neon_ext_dotprod
24140  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
24141  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
24142
24143 #undef  ARM_VARIANT
24144 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
24145 #undef  THUMB_VARIANT
24146 #define THUMB_VARIANT NULL
24147
24148  cCE("wfs",     e200110, 1, (RR),            rd),
24149  cCE("rfs",     e300110, 1, (RR),            rd),
24150  cCE("wfc",     e400110, 1, (RR),            rd),
24151  cCE("rfc",     e500110, 1, (RR),            rd),
24152
24153  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24154  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24155  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24156  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24157
24158  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24159  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24160  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24161  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24162
24163  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
24164  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
24165  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
24166  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
24167  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
24168  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
24169  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
24170  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
24171  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
24172  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
24173  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
24174  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
24175
24176  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
24177  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
24178  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
24179  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
24180  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
24181  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
24182  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
24183  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
24184  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
24185  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
24186  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
24187  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
24188
24189  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
24190  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
24191  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
24192  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
24193  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
24194  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
24195  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
24196  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
24197  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
24198  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
24199  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
24200  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
24201
24202  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
24203  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
24204  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
24205  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
24206  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
24207  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
24208  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
24209  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
24210  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
24211  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
24212  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
24213  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
24214
24215  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
24216  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
24217  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
24218  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
24219  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
24220  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
24221  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
24222  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
24223  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
24224  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
24225  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
24226  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
24227
24228  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
24229  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
24230  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
24231  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
24232  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
24233  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
24234  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
24235  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
24236  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
24237  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
24238  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
24239  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
24240
24241  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
24242  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
24243  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
24244  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
24245  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
24246  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
24247  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
24248  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
24249  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
24250  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
24251  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
24252  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
24253
24254  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
24255  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
24256  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
24257  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
24258  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
24259  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
24260  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
24261  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
24262  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
24263  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
24264  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
24265  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
24266
24267  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
24268  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
24269  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
24270  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
24271  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
24272  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
24273  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
24274  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
24275  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
24276  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
24277  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
24278  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
24279
24280  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
24281  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
24282  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
24283  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
24284  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
24285  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
24286  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
24287  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
24288  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
24289  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
24290  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
24291  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
24292
24293  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
24294  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
24295  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
24296  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
24297  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
24298  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
24299  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
24300  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
24301  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
24302  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
24303  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
24304  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
24305
24306  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
24307  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
24308  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
24309  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
24310  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
24311  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
24312  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
24313  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
24314  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
24315  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
24316  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
24317  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
24318
24319  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
24320  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
24321  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
24322  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
24323  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
24324  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
24325  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
24326  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
24327  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
24328  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
24329  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
24330  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
24331
24332  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
24333  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
24334  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
24335  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
24336  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
24337  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
24338  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
24339  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
24340  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
24341  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
24342  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
24343  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
24344
24345  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
24346  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
24347  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
24348  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
24349  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
24350  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
24351  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
24352  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
24353  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
24354  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
24355  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
24356  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
24357
24358  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
24359  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
24360  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
24361  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
24362  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
24363  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
24364  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
24365  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
24366  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
24367  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
24368  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
24369  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
24370
24371  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
24372  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
24373  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
24374  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
24375  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
24376  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24377  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24378  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24379  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
24380  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
24381  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
24382  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
24383
24384  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
24385  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
24386  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
24387  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
24388  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
24389  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24390  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24391  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24392  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
24393  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
24394  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
24395  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
24396
24397  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
24398  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
24399  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
24400  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
24401  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
24402  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24403  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24404  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24405  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
24406  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
24407  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
24408  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
24409
24410  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
24411  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
24412  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
24413  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
24414  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
24415  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24416  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24417  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24418  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
24419  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
24420  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
24421  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
24422
24423  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
24424  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
24425  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
24426  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
24427  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
24428  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24429  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24430  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24431  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
24432  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
24433  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
24434  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
24435
24436  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
24437  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
24438  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
24439  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
24440  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
24441  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24442  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24443  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24444  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
24445  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
24446  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
24447  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
24448
24449  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
24450  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
24451  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
24452  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
24453  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
24454  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24455  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24456  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24457  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
24458  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
24459  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
24460  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
24461
24462  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
24463  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
24464  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
24465  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
24466  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
24467  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24468  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24469  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24470  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
24471  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
24472  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
24473  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
24474
24475  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
24476  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
24477  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
24478  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
24479  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
24480  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24481  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24482  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24483  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
24484  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
24485  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
24486  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
24487
24488  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
24489  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
24490  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
24491  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
24492  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
24493  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24494  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24495  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24496  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
24497  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
24498  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
24499  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
24500
24501  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24502  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24503  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24504  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24505  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24506  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24507  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24508  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24509  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24510  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24511  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24512  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24513
24514  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24515  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24516  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24517  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24518  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24519  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24520  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24521  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24522  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24523  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24524  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24525  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24526
24527  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24528  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24529  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24530  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24531  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24532  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24533  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24534  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24535  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24536  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24537  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24538  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24539
24540  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
24541  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
24542  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
24543  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
24544
24545  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
24546  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
24547  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
24548  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
24549  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
24550  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
24551  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
24552  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
24553  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
24554  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
24555  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
24556  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
24557
24558   /* The implementation of the FIX instruction is broken on some
24559      assemblers, in that it accepts a precision specifier as well as a
24560      rounding specifier, despite the fact that this is meaningless.
24561      To be more compatible, we accept it as well, though of course it
24562      does not set any bits.  */
24563  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
24564  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
24565  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
24566  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
24567  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
24568  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
24569  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
24570  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
24571  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
24572  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
24573  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
24574  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
24575  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
24576
24577   /* Instructions that were new with the real FPA, call them V2.  */
24578 #undef  ARM_VARIANT
24579 #define ARM_VARIANT  & fpu_fpa_ext_v2
24580
24581  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24582  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24583  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24584  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24585  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24586  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24587
24588 #undef  ARM_VARIANT
24589 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
24590 #undef THUMB_VARIANT
24591 #define THUMB_VARIANT  & arm_ext_v6t2
24592  mcCE(vmrs,     ef00a10, 2, (APSR_RR, RVC),   vmrs),
24593  mcCE(vmsr,     ee00a10, 2, (RVC, RR),        vmsr),
24594 #undef THUMB_VARIANT
24595
24596   /* Moves and type conversions.  */
24597  cCE("fmstat",  ef1fa10, 0, (),               noargs),
24598  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24599  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
24600  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
24601  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24602  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
24603  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24604  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
24605  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
24606
24607   /* Memory operations.  */
24608  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
24609  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
24610  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24611  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24612  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24613  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24614  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24615  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24616  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24617  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24618  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24619  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24620  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24621  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24622  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24623  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24624  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24625  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24626
24627   /* Monadic operations.  */
24628  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24629  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
24630  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24631
24632   /* Dyadic operations.  */
24633  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24634  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24635  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24636  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24637  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24638  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24639  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24640  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24641  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24642
24643   /* Comparisons.  */
24644  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
24645  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
24646  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24647  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
24648
24649  /* Double precision load/store are still present on single precision
24650     implementations.  */
24651  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
24652  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
24653  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24654  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24655  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24656  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24657  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24658  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24659  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24660  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24661
24662 #undef  ARM_VARIANT
24663 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
24664
24665   /* Moves and type conversions.  */
24666  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24667  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24668  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
24669  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
24670  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
24671  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
24672  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24673  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24674  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24675  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24676  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24677  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24678
24679   /* Monadic operations.  */
24680  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24681  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24682  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24683
24684   /* Dyadic operations.  */
24685  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24686  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24687  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24688  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24689  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24690  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24691  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24692  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24693  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24694
24695   /* Comparisons.  */
24696  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24697  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
24698  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24699  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
24700
24701 /* Instructions which may belong to either the Neon or VFP instruction sets.
24702    Individual encoder functions perform additional architecture checks.  */
24703 #undef  ARM_VARIANT
24704 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
24705 #undef  THUMB_VARIANT
24706 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
24707
24708   /* These mnemonics are unique to VFP.  */
24709  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
24710  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
24711  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24712  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24713  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24714  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
24715  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
24716  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
24717
24718   /* Mnemonics shared by Neon and VFP.  */
24719  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
24720
24721  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24722  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24723  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24724  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24725  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24726  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24727
24728  mnCEF(vcvt,     _vcvt,   3, (RNSDQMQ, RNSDQMQ, oI32z), neon_cvt),
24729  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
24730  MNCEF(vcvtb,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtb),
24731  MNCEF(vcvtt,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtt),
24732
24733
24734   /* NOTE: All VMOV encoding is special-cased!  */
24735  NCE(vmovq,     0,       1, (VMOV), neon_mov),
24736
24737 #undef  THUMB_VARIANT
24738 /* Could be either VLDR/VSTR or VLDR/VSTR (system register) which are guarded
24739    by different feature bits.  Since we are setting the Thumb guard, we can
24740    require Thumb-1 which makes it a nop guard and set the right feature bit in
24741    do_vldr_vstr ().  */
24742 #define THUMB_VARIANT  & arm_ext_v4t
24743  NCE(vldr,      d100b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
24744  NCE(vstr,      d000b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
24745
24746 #undef  ARM_VARIANT
24747 #define ARM_VARIANT    & arm_ext_fp16
24748 #undef  THUMB_VARIANT
24749 #define THUMB_VARIANT  & arm_ext_fp16
24750  /* New instructions added from v8.2, allowing the extraction and insertion of
24751     the upper 16 bits of a 32-bit vector register.  */
24752  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
24753  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
24754
24755  /* New backported fma/fms instructions optional in v8.2.  */
24756  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
24757  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
24758
24759 #undef  THUMB_VARIANT
24760 #define THUMB_VARIANT  & fpu_neon_ext_v1
24761 #undef  ARM_VARIANT
24762 #define ARM_VARIANT    & fpu_neon_ext_v1
24763
24764   /* Data processing with three registers of the same length.  */
24765   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
24766  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
24767  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
24768  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24769  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24770  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24771   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
24772  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
24773  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
24774  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
24775  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
24776   /* If not immediate, fall back to neon_dyadic_i64_su.
24777      shl should accept I8 I16 I32 I64,
24778      qshl should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
24779  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl),
24780  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl),
24781   /* Logic ops, types optional & ignored.  */
24782  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24783  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24784  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24785  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24786  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
24787   /* Bitfield ops, untyped.  */
24788  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24789  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24790  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24791  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24792  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24793  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24794   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
24795  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24796  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24797  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24798   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
24799      back to neon_dyadic_if_su.  */
24800  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
24801  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
24802  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
24803  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
24804  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
24805  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
24806  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
24807  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
24808   /* Comparison. Type I8 I16 I32 F32.  */
24809  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
24810  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
24811   /* As above, D registers only.  */
24812  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
24813  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
24814   /* Int and float variants, signedness unimportant.  */
24815  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
24816  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
24817  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
24818   /* Add/sub take types I8 I16 I32 I64 F32.  */
24819  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
24820  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
24821   /* vtst takes sizes 8, 16, 32.  */
24822  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
24823  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
24824   /* VMUL takes I8 I16 I32 F32 P8.  */
24825  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
24826   /* VQD{R}MULH takes S16 S32.  */
24827  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
24828  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
24829  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
24830  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
24831  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
24832  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
24833  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
24834  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
24835  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
24836  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
24837  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
24838  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
24839  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
24840  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
24841  /* ARM v8.1 extension.  */
24842  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
24843  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
24844  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
24845
24846   /* Two address, int/float. Types S8 S16 S32 F32.  */
24847  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
24848  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
24849
24850   /* Data processing with two registers and a shift amount.  */
24851   /* Right shifts, and variants with rounding.
24852      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
24853  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
24854  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
24855  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
24856  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
24857  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
24858  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
24859   /* Shift and insert. Sizes accepted 8 16 32 64.  */
24860  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
24861  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
24862   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
24863  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
24864   /* Right shift immediate, saturating & narrowing, with rounding variants.
24865      Types accepted S16 S32 S64 U16 U32 U64.  */
24866  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
24867  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
24868   /* As above, unsigned. Types accepted S16 S32 S64.  */
24869  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
24870  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
24871   /* Right shift narrowing. Types accepted I16 I32 I64.  */
24872  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
24873  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
24874   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
24875  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
24876   /* CVT with optional immediate for fixed-point variant.  */
24877  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
24878
24879  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
24880
24881   /* Data processing, three registers of different lengths.  */
24882   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
24883  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
24884   /* If not scalar, fall back to neon_dyadic_long.
24885      Vector types as above, scalar types S16 S32 U16 U32.  */
24886  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
24887  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
24888   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
24889  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
24890  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
24891   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
24892  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24893  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24894  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24895  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24896   /* Saturating doubling multiplies. Types S16 S32.  */
24897  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24898  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24899  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24900   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
24901      S16 S32 U16 U32.  */
24902  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
24903
24904   /* Extract. Size 8.  */
24905  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
24906  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
24907
24908   /* Two registers, miscellaneous.  */
24909   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
24910  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
24911  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
24912  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
24913   /* Vector replicate. Sizes 8 16 32.  */
24914  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
24915   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
24916  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
24917   /* VMOVN. Types I16 I32 I64.  */
24918  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
24919   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
24920  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
24921   /* VQMOVUN. Types S16 S32 S64.  */
24922  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
24923   /* VZIP / VUZP. Sizes 8 16 32.  */
24924  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
24925  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
24926  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
24927  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
24928   /* VQABS / VQNEG. Types S8 S16 S32.  */
24929  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
24930  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
24931   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
24932  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
24933  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
24934  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
24935  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
24936   /* Reciprocal estimates.  Types U32 F16 F32.  */
24937  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
24938  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
24939  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
24940  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
24941   /* VCLS. Types S8 S16 S32.  */
24942  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
24943   /* VCLZ. Types I8 I16 I32.  */
24944  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
24945   /* VCNT. Size 8.  */
24946  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
24947  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
24948   /* Two address, untyped.  */
24949  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
24950  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
24951   /* VTRN. Sizes 8 16 32.  */
24952  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
24953  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
24954
24955   /* Table lookup. Size 8.  */
24956  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
24957  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
24958
24959 #undef  THUMB_VARIANT
24960 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
24961 #undef  ARM_VARIANT
24962 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
24963
24964   /* Neon element/structure load/store.  */
24965  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24966  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24967  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24968  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24969  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24970  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24971  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24972  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
24973
24974 #undef  THUMB_VARIANT
24975 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
24976 #undef  ARM_VARIANT
24977 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
24978  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
24979  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
24980  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
24981  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
24982  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
24983  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
24984  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
24985  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
24986  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
24987
24988 #undef  THUMB_VARIANT
24989 #define THUMB_VARIANT  & fpu_vfp_ext_v3
24990 #undef  ARM_VARIANT
24991 #define ARM_VARIANT    & fpu_vfp_ext_v3
24992
24993  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
24994  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
24995  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
24996  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
24997  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
24998  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
24999  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25000  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25001  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25002
25003 #undef  ARM_VARIANT
25004 #define ARM_VARIANT    & fpu_vfp_ext_fma
25005 #undef  THUMB_VARIANT
25006 #define THUMB_VARIANT  & fpu_vfp_ext_fma
25007  /* Mnemonics shared by Neon, VFP and MVE.  These are included in the
25008     VFP FMA variant; NEON and VFP FMA always includes the NEON
25009     FMA instructions.  */
25010  mnCEF(vfma,     _vfma,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR), neon_fmac),
25011  mnCEF(vfms,     _vfms,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ),  neon_fmac),
25012
25013  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
25014     the v form should always be used.  */
25015  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
25016  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
25017  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
25018  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
25019  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
25020  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
25021
25022 #undef THUMB_VARIANT
25023 #undef  ARM_VARIANT
25024 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
25025
25026  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25027  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25028  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25029  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25030  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25031  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25032  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
25033  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
25034
25035 #undef  ARM_VARIANT
25036 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
25037
25038  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
25039  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
25040  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
25041  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
25042  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
25043  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
25044  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
25045  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
25046  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
25047  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25048  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25049  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25050  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25051  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25052  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25053  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25054  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25055  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25056  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
25057  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
25058  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25059  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25060  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25061  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25062  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25063  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25064  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
25065  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
25066  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
25067  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
25068  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
25069  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
25070  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
25071  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
25072  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
25073  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
25074  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
25075  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25076  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25077  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25078  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25079  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25080  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25081  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25082  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25083  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25084  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
25085  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25086  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25087  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25088  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25089  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25090  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25091  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25092  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25093  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25094  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25095  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25096  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25097  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25098  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25099  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25100  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25101  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25102  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25103  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25104  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25105  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25106  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
25107  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
25108  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25109  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25110  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25111  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25112  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25113  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25114  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25115  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25116  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25117  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25118  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25119  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25120  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25121  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25122  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25123  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25124  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25125  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25126  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
25127  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25128  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25129  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25130  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25131  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25132  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25133  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25134  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25135  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25136  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25137  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25138  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25139  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25140  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25141  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25142  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25143  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25144  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25145  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25146  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25147  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25148  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
25149  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25150  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25151  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25152  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25153  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25154  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25155  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25156  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25157  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25158  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25159  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25160  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25161  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25162  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25163  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25164  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25165  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25166  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25167  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25168  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25169  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
25170  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
25171  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25172  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25173  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25174  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25175  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25176  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25177  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25178  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25179  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25180  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
25181  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
25182  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
25183  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
25184  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
25185  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
25186  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25187  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25188  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25189  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
25190  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
25191  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
25192  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
25193  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
25194  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
25195  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25196  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25197  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25198  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25199  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
25200
25201 #undef  ARM_VARIANT
25202 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
25203
25204  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
25205  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
25206  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
25207  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
25208  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
25209  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
25210  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25211  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25212  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25213  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25214  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25215  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25216  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25217  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25218  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25219  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25220  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25221  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25222  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25223  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25224  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
25225  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25226  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25227  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25228  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25229  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25230  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25231  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25232  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25233  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25234  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25235  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25236  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25237  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25238  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25239  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25240  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25241  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25242  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25243  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25244  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25245  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25246  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25247  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25248  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25249  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25250  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25251  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25252  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25253  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25254  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25255  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25256  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25257  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25258  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25259  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25260  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25261
25262 #undef  ARM_VARIANT
25263 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
25264
25265  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
25266  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
25267  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
25268  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
25269  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
25270  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
25271  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
25272  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
25273  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
25274  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
25275  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
25276  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
25277  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
25278  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
25279  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
25280  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
25281  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
25282  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
25283  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
25284  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
25285  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
25286  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
25287  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
25288  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
25289  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
25290  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
25291  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
25292  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
25293  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
25294  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
25295  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
25296  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
25297  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
25298  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
25299  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
25300  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
25301  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
25302  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
25303  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
25304  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
25305  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
25306  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
25307  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
25308  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
25309  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
25310  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
25311  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
25312  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
25313  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
25314  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
25315  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
25316  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
25317  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
25318  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
25319  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
25320  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
25321  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
25322  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
25323  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
25324  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
25325  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
25326  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
25327  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
25328  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
25329  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25330  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25331  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25332  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25333  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25334  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25335  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25336  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25337  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
25338  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
25339  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
25340  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
25341
25342  /* ARMv8.5-A instructions.  */
25343 #undef  ARM_VARIANT
25344 #define ARM_VARIANT   & arm_ext_sb
25345 #undef  THUMB_VARIANT
25346 #define THUMB_VARIANT & arm_ext_sb
25347  TUF("sb", 57ff070, f3bf8f70, 0, (), noargs, noargs),
25348
25349 #undef  ARM_VARIANT
25350 #define ARM_VARIANT   & arm_ext_predres
25351 #undef  THUMB_VARIANT
25352 #define THUMB_VARIANT & arm_ext_predres
25353  CE("cfprctx", e070f93, 1, (RRnpc), rd),
25354  CE("dvprctx", e070fb3, 1, (RRnpc), rd),
25355  CE("cpprctx", e070ff3, 1, (RRnpc), rd),
25356
25357  /* ARMv8-M instructions.  */
25358 #undef  ARM_VARIANT
25359 #define ARM_VARIANT NULL
25360 #undef  THUMB_VARIANT
25361 #define THUMB_VARIANT & arm_ext_v8m
25362  ToU("sg",    e97fe97f, 0, (),             noargs),
25363  ToC("blxns", 4784,     1, (RRnpc),        t_blx),
25364  ToC("bxns",  4704,     1, (RRnpc),        t_bx),
25365  ToC("tt",    e840f000, 2, (RRnpc, RRnpc), tt),
25366  ToC("ttt",   e840f040, 2, (RRnpc, RRnpc), tt),
25367  ToC("tta",   e840f080, 2, (RRnpc, RRnpc), tt),
25368  ToC("ttat",  e840f0c0, 2, (RRnpc, RRnpc), tt),
25369
25370  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
25371     instructions behave as nop if no VFP is present.  */
25372 #undef  THUMB_VARIANT
25373 #define THUMB_VARIANT & arm_ext_v8m_main
25374  ToC("vlldm", ec300a00, 1, (RRnpc), rn),
25375  ToC("vlstm", ec200a00, 1, (RRnpc), rn),
25376
25377  /* Armv8.1-M Mainline instructions.  */
25378 #undef  THUMB_VARIANT
25379 #define THUMB_VARIANT & arm_ext_v8_1m_main
25380  toU("cinc",  _cinc,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25381  toU("cinv",  _cinv,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25382  toU("cneg",  _cneg,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25383  toU("csel",  _csel,  4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25384  toU("csetm", _csetm, 2, (RRnpcsp, COND),               t_cond),
25385  toU("cset",  _cset,  2, (RRnpcsp, COND),               t_cond),
25386  toU("csinc", _csinc, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25387  toU("csinv", _csinv, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25388  toU("csneg", _csneg, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25389
25390  toC("bf",     _bf,     2, (EXPs, EXPs),             t_branch_future),
25391  toU("bfcsel", _bfcsel, 4, (EXPs, EXPs, EXPs, COND), t_branch_future),
25392  toC("bfx",    _bfx,    2, (EXPs, RRnpcsp),          t_branch_future),
25393  toC("bfl",    _bfl,    2, (EXPs, EXPs),             t_branch_future),
25394  toC("bflx",   _bflx,   2, (EXPs, RRnpcsp),          t_branch_future),
25395
25396  toU("dls", _dls, 2, (LR, RRnpcsp),      t_loloop),
25397  toU("wls", _wls, 3, (LR, RRnpcsp, EXP), t_loloop),
25398  toU("le",  _le,  2, (oLR, EXP),         t_loloop),
25399
25400  ToC("clrm",    e89f0000, 1, (CLRMLST),  t_clrm),
25401  ToC("vscclrm", ec9f0a00, 1, (VRSDVLST), t_vscclrm),
25402
25403 #undef  THUMB_VARIANT
25404 #define THUMB_VARIANT & mve_ext
25405  ToC("lsll",    ea50010d, 3, (RRe, RRo, RRnpcsp_I32), mve_scalar_shift),
25406  ToC("lsrl",    ea50011f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25407  ToC("asrl",    ea50012d, 3, (RRe, RRo, RRnpcsp_I32), mve_scalar_shift),
25408  ToC("uqrshll", ea51010d, 3, (RRe, RRo, RRnpcsp),     mve_scalar_shift),
25409  ToC("sqrshrl", ea51012d, 3, (RRe, RRo, RRnpcsp),     mve_scalar_shift),
25410  ToC("uqshll",  ea51010f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25411  ToC("urshrl",  ea51011f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25412  ToC("srshrl",  ea51012f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25413  ToC("sqshll",  ea51013f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25414  ToC("uqrshl",  ea500f0d, 2, (RRnpcsp, RRnpcsp),      mve_scalar_shift),
25415  ToC("sqrshr",  ea500f2d, 2, (RRnpcsp, RRnpcsp),      mve_scalar_shift),
25416  ToC("uqshl",   ea500f0f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25417  ToC("urshr",   ea500f1f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25418  ToC("srshr",   ea500f2f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25419  ToC("sqshl",   ea500f3f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25420
25421  ToC("vpt",     ee410f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25422  ToC("vptt",    ee018f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25423  ToC("vpte",    ee418f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25424  ToC("vpttt",   ee014f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25425  ToC("vptte",   ee01cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25426  ToC("vptet",   ee41cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25427  ToC("vptee",   ee414f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25428  ToC("vptttt",  ee012f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25429  ToC("vpttte",  ee016f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25430  ToC("vpttet",  ee01ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25431  ToC("vpttee",  ee01af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25432  ToC("vptett",  ee41af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25433  ToC("vptete",  ee41ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25434  ToC("vpteet",  ee416f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25435  ToC("vpteee",  ee412f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25436
25437  ToC("vpst",    fe710f4d, 0, (), mve_vpt),
25438  ToC("vpstt",   fe318f4d, 0, (), mve_vpt),
25439  ToC("vpste",   fe718f4d, 0, (), mve_vpt),
25440  ToC("vpsttt",  fe314f4d, 0, (), mve_vpt),
25441  ToC("vpstte",  fe31cf4d, 0, (), mve_vpt),
25442  ToC("vpstet",  fe71cf4d, 0, (), mve_vpt),
25443  ToC("vpstee",  fe714f4d, 0, (), mve_vpt),
25444  ToC("vpstttt", fe312f4d, 0, (), mve_vpt),
25445  ToC("vpsttte", fe316f4d, 0, (), mve_vpt),
25446  ToC("vpsttet", fe31ef4d, 0, (), mve_vpt),
25447  ToC("vpsttee", fe31af4d, 0, (), mve_vpt),
25448  ToC("vpstett", fe71af4d, 0, (), mve_vpt),
25449  ToC("vpstete", fe71ef4d, 0, (), mve_vpt),
25450  ToC("vpsteet", fe716f4d, 0, (), mve_vpt),
25451  ToC("vpsteee", fe712f4d, 0, (), mve_vpt),
25452
25453  /* MVE and MVE FP only.  */
25454  mToC("vhcadd", ee000f00,   4, (RMQ, RMQ, RMQ, EXPi),             mve_vhcadd),
25455  mCEF(vadc,     _vadc,      3, (RMQ, RMQ, RMQ),                   mve_vadc),
25456  mCEF(vadci,    _vadci,     3, (RMQ, RMQ, RMQ),                   mve_vadc),
25457  mToC("vsbc",   fe300f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
25458  mToC("vsbci",  fe301f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
25459  mCEF(vmullb,   _vmullb,    3, (RMQ, RMQ, RMQ),                   mve_vmull),
25460  mCEF(vabav,    _vabav,     3, (RRnpcsp, RMQ, RMQ),               mve_vabav),
25461  mCEF(vmladav,    _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25462  mCEF(vmladava,   _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25463  mCEF(vmladavx,   _vmladavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25464  mCEF(vmladavax,  _vmladavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
25465  mCEF(vmlav,      _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25466  mCEF(vmlava,     _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25467  mCEF(vmlsdav,    _vmlsdav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25468  mCEF(vmlsdava,   _vmlsdava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25469  mCEF(vmlsdavx,   _vmlsdavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25470  mCEF(vmlsdavax,  _vmlsdavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
25471
25472  mCEF(vst20,    _vst20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25473  mCEF(vst21,    _vst21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25474  mCEF(vst40,    _vst40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25475  mCEF(vst41,    _vst41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25476  mCEF(vst42,    _vst42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25477  mCEF(vst43,    _vst43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25478  mCEF(vld20,    _vld20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25479  mCEF(vld21,    _vld21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25480  mCEF(vld40,    _vld40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25481  mCEF(vld41,    _vld41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25482  mCEF(vld42,    _vld42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25483  mCEF(vld43,    _vld43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25484  mCEF(vstrb,    _vstrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25485  mCEF(vstrh,    _vstrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25486  mCEF(vstrw,    _vstrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25487  mCEF(vstrd,    _vstrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25488  mCEF(vldrb,    _vldrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25489  mCEF(vldrh,    _vldrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25490  mCEF(vldrw,    _vldrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25491  mCEF(vldrd,    _vldrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25492
25493  mCEF(vmovnt,   _vmovnt,    2, (RMQ, RMQ),                        mve_movn),
25494  mCEF(vmovnb,   _vmovnb,    2, (RMQ, RMQ),                        mve_movn),
25495  mCEF(vbrsr,    _vbrsr,     3, (RMQ, RMQ, RR),                    mve_vbrsr),
25496  mCEF(vaddlv,   _vaddlv,    3, (RRe, RRo, RMQ),                   mve_vaddlv),
25497  mCEF(vaddlva,  _vaddlva,   3, (RRe, RRo, RMQ),                   mve_vaddlv),
25498  mCEF(vaddv,    _vaddv,     2, (RRe, RMQ),                        mve_vaddv),
25499  mCEF(vaddva,   _vaddva,    2, (RRe, RMQ),                        mve_vaddv),
25500  mCEF(vddup,    _vddup,     3, (RMQ, RRe, EXPi),                  mve_viddup),
25501  mCEF(vdwdup,   _vdwdup,    4, (RMQ, RRe, RR, EXPi),              mve_viddup),
25502  mCEF(vidup,    _vidup,     3, (RMQ, RRe, EXPi),                  mve_viddup),
25503  mCEF(viwdup,   _viwdup,    4, (RMQ, RRe, RR, EXPi),              mve_viddup),
25504  mToC("vmaxa",  ee330e81,   2, (RMQ, RMQ),                        mve_vmaxa_vmina),
25505  mToC("vmina",  ee331e81,   2, (RMQ, RMQ),                        mve_vmaxa_vmina),
25506  mCEF(vmaxv,    _vmaxv,   2, (RR, RMQ),                           mve_vmaxv),
25507  mCEF(vmaxav,   _vmaxav,  2, (RR, RMQ),                           mve_vmaxv),
25508  mCEF(vminv,    _vminv,   2, (RR, RMQ),                           mve_vmaxv),
25509  mCEF(vminav,   _vminav,  2, (RR, RMQ),                           mve_vmaxv),
25510
25511  mCEF(vmlaldav,   _vmlaldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25512  mCEF(vmlaldava,  _vmlaldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25513  mCEF(vmlaldavx,  _vmlaldavx,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25514  mCEF(vmlaldavax, _vmlaldavax,  4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25515  mCEF(vmlalv,     _vmlaldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25516  mCEF(vmlalva,    _vmlaldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25517  mCEF(vmlsldav,   _vmlsldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25518  mCEF(vmlsldava,  _vmlsldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25519  mCEF(vmlsldavx,  _vmlsldavx,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25520  mCEF(vmlsldavax, _vmlsldavax,  4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25521  mToC("vrmlaldavh", ee800f00,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25522  mToC("vrmlaldavha",ee800f20,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25523  mCEF(vrmlaldavhx,  _vrmlaldavhx,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25524  mCEF(vrmlaldavhax, _vrmlaldavhax, 4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25525  mToC("vrmlalvh",   ee800f00,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25526  mToC("vrmlalvha",  ee800f20,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25527  mCEF(vrmlsldavh,   _vrmlsldavh,   4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25528  mCEF(vrmlsldavha,  _vrmlsldavha,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25529  mCEF(vrmlsldavhx,  _vrmlsldavhx,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25530  mCEF(vrmlsldavhax, _vrmlsldavhax, 4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25531
25532  mToC("vmlas",    ee011e40,     3, (RMQ, RMQ, RR),              mve_vmlas),
25533  mToC("vmulh",    ee010e01,     3, (RMQ, RMQ, RMQ),             mve_vmulh),
25534  mToC("vrmulh",   ee011e01,     3, (RMQ, RMQ, RMQ),             mve_vmulh),
25535  mToC("vpnot",    fe310f4d,     0, (),                          mve_vpnot),
25536  mToC("vpsel",    fe310f01,     3, (RMQ, RMQ, RMQ),             mve_vpsel),
25537
25538  mToC("vqdmladh",  ee000e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25539  mToC("vqdmladhx", ee001e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25540  mToC("vqrdmladh", ee000e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25541  mToC("vqrdmladhx",ee001e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25542  mToC("vqdmlsdh",  fe000e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25543  mToC("vqdmlsdhx", fe001e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25544  mToC("vqrdmlsdh", fe000e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25545  mToC("vqrdmlsdhx",fe001e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25546  mToC("vqdmlah",   ee000e60,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25547  mToC("vqdmlash",  ee001e60,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25548  mToC("vqrdmlash", ee001e40,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25549  mToC("vqdmullt",  ee301f00,    3, (RMQ, RMQ, RMQRR),           mve_vqdmull),
25550  mToC("vqdmullb",  ee300f00,    3, (RMQ, RMQ, RMQRR),           mve_vqdmull),
25551  mCEF(vqmovnt,    _vqmovnt,     2, (RMQ, RMQ),                  mve_vqmovn),
25552  mCEF(vqmovnb,    _vqmovnb,     2, (RMQ, RMQ),                  mve_vqmovn),
25553  mCEF(vqmovunt,   _vqmovunt,    2, (RMQ, RMQ),                  mve_vqmovn),
25554  mCEF(vqmovunb,   _vqmovunb,    2, (RMQ, RMQ),                  mve_vqmovn),
25555
25556  mCEF(vshrnt,     _vshrnt,      3, (RMQ, RMQ, I32z),    mve_vshrn),
25557  mCEF(vshrnb,     _vshrnb,      3, (RMQ, RMQ, I32z),    mve_vshrn),
25558  mCEF(vrshrnt,    _vrshrnt,     3, (RMQ, RMQ, I32z),    mve_vshrn),
25559  mCEF(vrshrnb,    _vrshrnb,     3, (RMQ, RMQ, I32z),    mve_vshrn),
25560  mCEF(vqshrnt,    _vqrshrnt,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25561  mCEF(vqshrnb,    _vqrshrnb,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25562  mCEF(vqshrunt,   _vqrshrunt,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25563  mCEF(vqshrunb,   _vqrshrunb,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25564  mCEF(vqrshrnt,   _vqrshrnt,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25565  mCEF(vqrshrnb,   _vqrshrnb,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25566  mCEF(vqrshrunt,  _vqrshrunt,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25567  mCEF(vqrshrunb,  _vqrshrunb,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25568
25569  mToC("vshlc",      eea00fc0,      3, (RMQ, RR, I32z),      mve_vshlc),
25570  mToC("vshllt",     ee201e00,      3, (RMQ, RMQ, I32),      mve_vshll),
25571  mToC("vshllb",     ee200e00,      3, (RMQ, RMQ, I32),      mve_vshll),
25572
25573  toU("dlstp",   _dlstp, 2, (LR, RR),      t_loloop),
25574  toU("wlstp",   _wlstp, 3, (LR, RR, EXP), t_loloop),
25575  toU("letp",    _letp,  2, (LR, EXP),     t_loloop),
25576  toU("lctp",    _lctp,  0, (),            t_loloop),
25577
25578 #undef THUMB_VARIANT
25579 #define THUMB_VARIANT & mve_fp_ext
25580  mToC("vcmul", ee300e00,   4, (RMQ, RMQ, RMQ, EXPi),              mve_vcmul),
25581  mToC("vfmas", ee311e40,   3, (RMQ, RMQ, RR),                     mve_vfmas),
25582  mToC("vmaxnma", ee3f0e81, 2, (RMQ, RMQ),                         mve_vmaxnma_vminnma),
25583  mToC("vminnma", ee3f1e81, 2, (RMQ, RMQ),                         mve_vmaxnma_vminnma),
25584  mToC("vmaxnmv", eeee0f00, 2, (RR, RMQ),                          mve_vmaxnmv),
25585  mToC("vmaxnmav",eeec0f00, 2, (RR, RMQ),                          mve_vmaxnmv),
25586  mToC("vminnmv", eeee0f80, 2, (RR, RMQ),                          mve_vmaxnmv),
25587  mToC("vminnmav",eeec0f80, 2, (RR, RMQ),                          mve_vmaxnmv),
25588
25589 #undef  ARM_VARIANT
25590 #define ARM_VARIANT  & fpu_vfp_ext_v1
25591 #undef  THUMB_VARIANT
25592 #define THUMB_VARIANT  & arm_ext_v6t2
25593  mnCEF(vmla,     _vmla,    3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ_RR), neon_mac_maybe_scalar),
25594  mnCEF(vmul,     _vmul,    3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ_RR), neon_mul),
25595
25596  mcCE(fcpyd,    eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
25597
25598 #undef  ARM_VARIANT
25599 #define ARM_VARIANT  & fpu_vfp_ext_v1xd
25600
25601  MNCE(vmov,   0,        1, (VMOV),            neon_mov),
25602  mcCE(fmrs,     e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
25603  mcCE(fmsr,     e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
25604  mcCE(fcpys,    eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
25605
25606  mCEF(vmullt, _vmullt,  3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ),  mve_vmull),
25607  mnCEF(vadd,  _vadd,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
25608  mnCEF(vsub,  _vsub,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
25609
25610  MNCEF(vabs,  1b10300,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
25611  MNCEF(vneg,  1b10380,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
25612
25613  mCEF(vmovlt, _vmovlt,  1, (VMOV),              mve_movl),
25614  mCEF(vmovlb, _vmovlb,  1, (VMOV),              mve_movl),
25615
25616  mnCE(vcmp,      _vcmp,    3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
25617  mnCE(vcmpe,     _vcmpe,   3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
25618
25619 #undef  ARM_VARIANT
25620 #define ARM_VARIANT  & fpu_vfp_ext_v2
25621
25622  mcCE(fmsrr,    c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
25623  mcCE(fmrrs,    c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
25624  mcCE(fmdrr,    c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
25625  mcCE(fmrrd,    c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
25626
25627 #undef  ARM_VARIANT
25628 #define ARM_VARIANT    & fpu_vfp_ext_armv8xd
25629  mnUF(vcvta,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvta),
25630  mnUF(vcvtp,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtp),
25631  mnUF(vcvtn,  _vcvta,  3, (RNSDQMQ, oRNSDQMQ, oI32z),   neon_cvtn),
25632  mnUF(vcvtm,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtm),
25633  mnUF(vmaxnm, _vmaxnm, 3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ), vmaxnm),
25634  mnUF(vminnm, _vminnm, 3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ), vmaxnm),
25635
25636 #undef  ARM_VARIANT
25637 #define ARM_VARIANT & fpu_neon_ext_v1
25638  mnUF(vabd,      _vabd,           3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25639  mnUF(vabdl,     _vabdl,          3, (RNQMQ, RNDMQ, RNDMQ),   neon_dyadic_long),
25640  mnUF(vaddl,     _vaddl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
25641  mnUF(vsubl,     _vsubl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
25642  mnUF(vand,      _vand,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25643  mnUF(vbic,      _vbic,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25644  mnUF(vorr,      _vorr,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25645  mnUF(vorn,      _vorn,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25646  mnUF(veor,      _veor,           3, (RNDQMQ, oRNDQMQ, RNDQMQ),      neon_logic),
25647  MNUF(vcls,      1b00400,         2, (RNDQMQ, RNDQMQ),               neon_cls),
25648  MNUF(vclz,      1b00480,         2, (RNDQMQ, RNDQMQ),               neon_clz),
25649  mnCE(vdup,      _vdup,           2, (RNDQMQ, RR_RNSC),              neon_dup),
25650  MNUF(vhadd,     00000000,        3, (RNDQMQ, oRNDQMQ, RNDQMQR),  neon_dyadic_i_su),
25651  MNUF(vrhadd,    00000100,        3, (RNDQMQ, oRNDQMQ, RNDQMQ),   neon_dyadic_i_su),
25652  MNUF(vhsub,     00000200,        3, (RNDQMQ, oRNDQMQ, RNDQMQR),  neon_dyadic_i_su),
25653  mnUF(vmin,      _vmin,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25654  mnUF(vmax,      _vmax,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25655  MNUF(vqadd,     0000010,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_dyadic_i64_su),
25656  MNUF(vqsub,     0000210,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_dyadic_i64_su),
25657  mnUF(vmvn,      _vmvn,    2, (RNDQMQ, RNDQMQ_Ibig), neon_mvn),
25658  MNUF(vqabs,     1b00700,  2, (RNDQMQ, RNDQMQ),     neon_sat_abs_neg),
25659  MNUF(vqneg,     1b00780,  2, (RNDQMQ, RNDQMQ),     neon_sat_abs_neg),
25660  mnUF(vqrdmlah,  _vqrdmlah,3, (RNDQMQ, oRNDQMQ, RNDQ_RNSC_RR), neon_qrdmlah),
25661  mnUF(vqdmulh,   _vqdmulh, 3, (RNDQMQ, oRNDQMQ, RNDQMQ_RNSC_RR), neon_qdmulh),
25662  mnUF(vqrdmulh,  _vqrdmulh,3, (RNDQMQ, oRNDQMQ, RNDQMQ_RNSC_RR), neon_qdmulh),
25663  MNUF(vqrshl,    0000510,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_rshl),
25664  MNUF(vrshl,     0000500,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_rshl),
25665  MNUF(vshr,      0800010,  3, (RNDQMQ, oRNDQMQ, I64z), neon_rshift_round_imm),
25666  MNUF(vrshr,     0800210,  3, (RNDQMQ, oRNDQMQ, I64z), neon_rshift_round_imm),
25667  MNUF(vsli,      1800510,  3, (RNDQMQ, oRNDQMQ, I63),  neon_sli),
25668  MNUF(vsri,      1800410,  3, (RNDQMQ, oRNDQMQ, I64z), neon_sri),
25669  MNUF(vrev64,    1b00000,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25670  MNUF(vrev32,    1b00080,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25671  MNUF(vrev16,    1b00100,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25672  mnUF(vshl,      _vshl,    3, (RNDQMQ, oRNDQMQ, RNDQMQ_I63b_RR), neon_shl),
25673  mnUF(vqshl,     _vqshl,   3, (RNDQMQ, oRNDQMQ, RNDQMQ_I63b_RR), neon_qshl),
25674  MNUF(vqshlu,    1800610,  3, (RNDQMQ, oRNDQMQ, I63),            neon_qshlu_imm),
25675
25676 #undef  ARM_VARIANT
25677 #define ARM_VARIANT & arm_ext_v8_3
25678 #undef  THUMB_VARIANT
25679 #define THUMB_VARIANT & arm_ext_v6t2_v8m
25680  MNUF (vcadd, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ, EXPi), vcadd),
25681  MNUF (vcmla, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ_RNSC, EXPi), vcmla),
25682 };
25683 #undef ARM_VARIANT
25684 #undef THUMB_VARIANT
25685 #undef TCE
25686 #undef TUE
25687 #undef TUF
25688 #undef TCC
25689 #undef cCE
25690 #undef cCL
25691 #undef C3E
25692 #undef C3
25693 #undef CE
25694 #undef CM
25695 #undef CL
25696 #undef UE
25697 #undef UF
25698 #undef UT
25699 #undef NUF
25700 #undef nUF
25701 #undef NCE
25702 #undef nCE
25703 #undef OPS0
25704 #undef OPS1
25705 #undef OPS2
25706 #undef OPS3
25707 #undef OPS4
25708 #undef OPS5
25709 #undef OPS6
25710 #undef do_0
25711 #undef ToC
25712 #undef toC
25713 #undef ToU
25714 #undef toU
25715 \f
25716 /* MD interface: bits in the object file.  */
25717
25718 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
25719    for use in the a.out file, and stores them in the array pointed to by buf.
25720    This knows about the endian-ness of the target machine and does
25721    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
25722    2 (short) and 4 (long)  Floating numbers are put out as a series of
25723    LITTLENUMS (shorts, here at least).  */
25724
25725 void
25726 md_number_to_chars (char * buf, valueT val, int n)
25727 {
25728   if (target_big_endian)
25729     number_to_chars_bigendian (buf, val, n);
25730   else
25731     number_to_chars_littleendian (buf, val, n);
25732 }
25733
25734 static valueT
25735 md_chars_to_number (char * buf, int n)
25736 {
25737   valueT result = 0;
25738   unsigned char * where = (unsigned char *) buf;
25739
25740   if (target_big_endian)
25741     {
25742       while (n--)
25743         {
25744           result <<= 8;
25745           result |= (*where++ & 255);
25746         }
25747     }
25748   else
25749     {
25750       while (n--)
25751         {
25752           result <<= 8;
25753           result |= (where[n] & 255);
25754         }
25755     }
25756
25757   return result;
25758 }
25759
25760 /* MD interface: Sections.  */
25761
25762 /* Calculate the maximum variable size (i.e., excluding fr_fix)
25763    that an rs_machine_dependent frag may reach.  */
25764
25765 unsigned int
25766 arm_frag_max_var (fragS *fragp)
25767 {
25768   /* We only use rs_machine_dependent for variable-size Thumb instructions,
25769      which are either THUMB_SIZE (2) or INSN_SIZE (4).
25770
25771      Note that we generate relaxable instructions even for cases that don't
25772      really need it, like an immediate that's a trivial constant.  So we're
25773      overestimating the instruction size for some of those cases.  Rather
25774      than putting more intelligence here, it would probably be better to
25775      avoid generating a relaxation frag in the first place when it can be
25776      determined up front that a short instruction will suffice.  */
25777
25778   gas_assert (fragp->fr_type == rs_machine_dependent);
25779   return INSN_SIZE;
25780 }
25781
25782 /* Estimate the size of a frag before relaxing.  Assume everything fits in
25783    2 bytes.  */
25784
25785 int
25786 md_estimate_size_before_relax (fragS * fragp,
25787                                segT    segtype ATTRIBUTE_UNUSED)
25788 {
25789   fragp->fr_var = 2;
25790   return 2;
25791 }
25792
25793 /* Convert a machine dependent frag.  */
25794
25795 void
25796 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
25797 {
25798   unsigned long insn;
25799   unsigned long old_op;
25800   char *buf;
25801   expressionS exp;
25802   fixS *fixp;
25803   int reloc_type;
25804   int pc_rel;
25805   int opcode;
25806
25807   buf = fragp->fr_literal + fragp->fr_fix;
25808
25809   old_op = bfd_get_16(abfd, buf);
25810   if (fragp->fr_symbol)
25811     {
25812       exp.X_op = O_symbol;
25813       exp.X_add_symbol = fragp->fr_symbol;
25814     }
25815   else
25816     {
25817       exp.X_op = O_constant;
25818     }
25819   exp.X_add_number = fragp->fr_offset;
25820   opcode = fragp->fr_subtype;
25821   switch (opcode)
25822     {
25823     case T_MNEM_ldr_pc:
25824     case T_MNEM_ldr_pc2:
25825     case T_MNEM_ldr_sp:
25826     case T_MNEM_str_sp:
25827     case T_MNEM_ldr:
25828     case T_MNEM_ldrb:
25829     case T_MNEM_ldrh:
25830     case T_MNEM_str:
25831     case T_MNEM_strb:
25832     case T_MNEM_strh:
25833       if (fragp->fr_var == 4)
25834         {
25835           insn = THUMB_OP32 (opcode);
25836           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
25837             {
25838               insn |= (old_op & 0x700) << 4;
25839             }
25840           else
25841             {
25842               insn |= (old_op & 7) << 12;
25843               insn |= (old_op & 0x38) << 13;
25844             }
25845           insn |= 0x00000c00;
25846           put_thumb32_insn (buf, insn);
25847           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
25848         }
25849       else
25850         {
25851           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
25852         }
25853       pc_rel = (opcode == T_MNEM_ldr_pc2);
25854       break;
25855     case T_MNEM_adr:
25856       if (fragp->fr_var == 4)
25857         {
25858           insn = THUMB_OP32 (opcode);
25859           insn |= (old_op & 0xf0) << 4;
25860           put_thumb32_insn (buf, insn);
25861           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
25862         }
25863       else
25864         {
25865           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25866           exp.X_add_number -= 4;
25867         }
25868       pc_rel = 1;
25869       break;
25870     case T_MNEM_mov:
25871     case T_MNEM_movs:
25872     case T_MNEM_cmp:
25873     case T_MNEM_cmn:
25874       if (fragp->fr_var == 4)
25875         {
25876           int r0off = (opcode == T_MNEM_mov
25877                        || opcode == T_MNEM_movs) ? 0 : 8;
25878           insn = THUMB_OP32 (opcode);
25879           insn = (insn & 0xe1ffffff) | 0x10000000;
25880           insn |= (old_op & 0x700) << r0off;
25881           put_thumb32_insn (buf, insn);
25882           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
25883         }
25884       else
25885         {
25886           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
25887         }
25888       pc_rel = 0;
25889       break;
25890     case T_MNEM_b:
25891       if (fragp->fr_var == 4)
25892         {
25893           insn = THUMB_OP32(opcode);
25894           put_thumb32_insn (buf, insn);
25895           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
25896         }
25897       else
25898         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
25899       pc_rel = 1;
25900       break;
25901     case T_MNEM_bcond:
25902       if (fragp->fr_var == 4)
25903         {
25904           insn = THUMB_OP32(opcode);
25905           insn |= (old_op & 0xf00) << 14;
25906           put_thumb32_insn (buf, insn);
25907           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
25908         }
25909       else
25910         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
25911       pc_rel = 1;
25912       break;
25913     case T_MNEM_add_sp:
25914     case T_MNEM_add_pc:
25915     case T_MNEM_inc_sp:
25916     case T_MNEM_dec_sp:
25917       if (fragp->fr_var == 4)
25918         {
25919           /* ??? Choose between add and addw.  */
25920           insn = THUMB_OP32 (opcode);
25921           insn |= (old_op & 0xf0) << 4;
25922           put_thumb32_insn (buf, insn);
25923           if (opcode == T_MNEM_add_pc)
25924             reloc_type = BFD_RELOC_ARM_T32_IMM12;
25925           else
25926             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
25927         }
25928       else
25929         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25930       pc_rel = 0;
25931       break;
25932
25933     case T_MNEM_addi:
25934     case T_MNEM_addis:
25935     case T_MNEM_subi:
25936     case T_MNEM_subis:
25937       if (fragp->fr_var == 4)
25938         {
25939           insn = THUMB_OP32 (opcode);
25940           insn |= (old_op & 0xf0) << 4;
25941           insn |= (old_op & 0xf) << 16;
25942           put_thumb32_insn (buf, insn);
25943           if (insn & (1 << 20))
25944             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
25945           else
25946             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
25947         }
25948       else
25949         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25950       pc_rel = 0;
25951       break;
25952     default:
25953       abort ();
25954     }
25955   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
25956                       (enum bfd_reloc_code_real) reloc_type);
25957   fixp->fx_file = fragp->fr_file;
25958   fixp->fx_line = fragp->fr_line;
25959   fragp->fr_fix += fragp->fr_var;
25960
25961   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
25962   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
25963       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
25964     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
25965 }
25966
25967 /* Return the size of a relaxable immediate operand instruction.
25968    SHIFT and SIZE specify the form of the allowable immediate.  */
25969 static int
25970 relax_immediate (fragS *fragp, int size, int shift)
25971 {
25972   offsetT offset;
25973   offsetT mask;
25974   offsetT low;
25975
25976   /* ??? Should be able to do better than this.  */
25977   if (fragp->fr_symbol)
25978     return 4;
25979
25980   low = (1 << shift) - 1;
25981   mask = (1 << (shift + size)) - (1 << shift);
25982   offset = fragp->fr_offset;
25983   /* Force misaligned offsets to 32-bit variant.  */
25984   if (offset & low)
25985     return 4;
25986   if (offset & ~mask)
25987     return 4;
25988   return 2;
25989 }
25990
25991 /* Get the address of a symbol during relaxation.  */
25992 static addressT
25993 relaxed_symbol_addr (fragS *fragp, long stretch)
25994 {
25995   fragS *sym_frag;
25996   addressT addr;
25997   symbolS *sym;
25998
25999   sym = fragp->fr_symbol;
26000   sym_frag = symbol_get_frag (sym);
26001   know (S_GET_SEGMENT (sym) != absolute_section
26002         || sym_frag == &zero_address_frag);
26003   addr = S_GET_VALUE (sym) + fragp->fr_offset;
26004
26005   /* If frag has yet to be reached on this pass, assume it will
26006      move by STRETCH just as we did.  If this is not so, it will
26007      be because some frag between grows, and that will force
26008      another pass.  */
26009
26010   if (stretch != 0
26011       && sym_frag->relax_marker != fragp->relax_marker)
26012     {
26013       fragS *f;
26014
26015       /* Adjust stretch for any alignment frag.  Note that if have
26016          been expanding the earlier code, the symbol may be
26017          defined in what appears to be an earlier frag.  FIXME:
26018          This doesn't handle the fr_subtype field, which specifies
26019          a maximum number of bytes to skip when doing an
26020          alignment.  */
26021       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
26022         {
26023           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
26024             {
26025               if (stretch < 0)
26026                 stretch = - ((- stretch)
26027                              & ~ ((1 << (int) f->fr_offset) - 1));
26028               else
26029                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
26030               if (stretch == 0)
26031                 break;
26032             }
26033         }
26034       if (f != NULL)
26035         addr += stretch;
26036     }
26037
26038   return addr;
26039 }
26040
26041 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
26042    load.  */
26043 static int
26044 relax_adr (fragS *fragp, asection *sec, long stretch)
26045 {
26046   addressT addr;
26047   offsetT val;
26048
26049   /* Assume worst case for symbols not known to be in the same section.  */
26050   if (fragp->fr_symbol == NULL
26051       || !S_IS_DEFINED (fragp->fr_symbol)
26052       || sec != S_GET_SEGMENT (fragp->fr_symbol)
26053       || S_IS_WEAK (fragp->fr_symbol))
26054     return 4;
26055
26056   val = relaxed_symbol_addr (fragp, stretch);
26057   addr = fragp->fr_address + fragp->fr_fix;
26058   addr = (addr + 4) & ~3;
26059   /* Force misaligned targets to 32-bit variant.  */
26060   if (val & 3)
26061     return 4;
26062   val -= addr;
26063   if (val < 0 || val > 1020)
26064     return 4;
26065   return 2;
26066 }
26067
26068 /* Return the size of a relaxable add/sub immediate instruction.  */
26069 static int
26070 relax_addsub (fragS *fragp, asection *sec)
26071 {
26072   char *buf;
26073   int op;
26074
26075   buf = fragp->fr_literal + fragp->fr_fix;
26076   op = bfd_get_16(sec->owner, buf);
26077   if ((op & 0xf) == ((op >> 4) & 0xf))
26078     return relax_immediate (fragp, 8, 0);
26079   else
26080     return relax_immediate (fragp, 3, 0);
26081 }
26082
26083 /* Return TRUE iff the definition of symbol S could be pre-empted
26084    (overridden) at link or load time.  */
26085 static bfd_boolean
26086 symbol_preemptible (symbolS *s)
26087 {
26088   /* Weak symbols can always be pre-empted.  */
26089   if (S_IS_WEAK (s))
26090     return TRUE;
26091
26092   /* Non-global symbols cannot be pre-empted. */
26093   if (! S_IS_EXTERNAL (s))
26094     return FALSE;
26095
26096 #ifdef OBJ_ELF
26097   /* In ELF, a global symbol can be marked protected, or private.  In that
26098      case it can't be pre-empted (other definitions in the same link unit
26099      would violate the ODR).  */
26100   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
26101     return FALSE;
26102 #endif
26103
26104   /* Other global symbols might be pre-empted.  */
26105   return TRUE;
26106 }
26107
26108 /* Return the size of a relaxable branch instruction.  BITS is the
26109    size of the offset field in the narrow instruction.  */
26110
26111 static int
26112 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
26113 {
26114   addressT addr;
26115   offsetT val;
26116   offsetT limit;
26117
26118   /* Assume worst case for symbols not known to be in the same section.  */
26119   if (!S_IS_DEFINED (fragp->fr_symbol)
26120       || sec != S_GET_SEGMENT (fragp->fr_symbol)
26121       || S_IS_WEAK (fragp->fr_symbol))
26122     return 4;
26123
26124 #ifdef OBJ_ELF
26125   /* A branch to a function in ARM state will require interworking.  */
26126   if (S_IS_DEFINED (fragp->fr_symbol)
26127       && ARM_IS_FUNC (fragp->fr_symbol))
26128       return 4;
26129 #endif
26130
26131   if (symbol_preemptible (fragp->fr_symbol))
26132     return 4;
26133
26134   val = relaxed_symbol_addr (fragp, stretch);
26135   addr = fragp->fr_address + fragp->fr_fix + 4;
26136   val -= addr;
26137
26138   /* Offset is a signed value *2 */
26139   limit = 1 << bits;
26140   if (val >= limit || val < -limit)
26141     return 4;
26142   return 2;
26143 }
26144
26145
26146 /* Relax a machine dependent frag.  This returns the amount by which
26147    the current size of the frag should change.  */
26148
26149 int
26150 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
26151 {
26152   int oldsize;
26153   int newsize;
26154
26155   oldsize = fragp->fr_var;
26156   switch (fragp->fr_subtype)
26157     {
26158     case T_MNEM_ldr_pc2:
26159       newsize = relax_adr (fragp, sec, stretch);
26160       break;
26161     case T_MNEM_ldr_pc:
26162     case T_MNEM_ldr_sp:
26163     case T_MNEM_str_sp:
26164       newsize = relax_immediate (fragp, 8, 2);
26165       break;
26166     case T_MNEM_ldr:
26167     case T_MNEM_str:
26168       newsize = relax_immediate (fragp, 5, 2);
26169       break;
26170     case T_MNEM_ldrh:
26171     case T_MNEM_strh:
26172       newsize = relax_immediate (fragp, 5, 1);
26173       break;
26174     case T_MNEM_ldrb:
26175     case T_MNEM_strb:
26176       newsize = relax_immediate (fragp, 5, 0);
26177       break;
26178     case T_MNEM_adr:
26179       newsize = relax_adr (fragp, sec, stretch);
26180       break;
26181     case T_MNEM_mov:
26182     case T_MNEM_movs:
26183     case T_MNEM_cmp:
26184     case T_MNEM_cmn:
26185       newsize = relax_immediate (fragp, 8, 0);
26186       break;
26187     case T_MNEM_b:
26188       newsize = relax_branch (fragp, sec, 11, stretch);
26189       break;
26190     case T_MNEM_bcond:
26191       newsize = relax_branch (fragp, sec, 8, stretch);
26192       break;
26193     case T_MNEM_add_sp:
26194     case T_MNEM_add_pc:
26195       newsize = relax_immediate (fragp, 8, 2);
26196       break;
26197     case T_MNEM_inc_sp:
26198     case T_MNEM_dec_sp:
26199       newsize = relax_immediate (fragp, 7, 2);
26200       break;
26201     case T_MNEM_addi:
26202     case T_MNEM_addis:
26203     case T_MNEM_subi:
26204     case T_MNEM_subis:
26205       newsize = relax_addsub (fragp, sec);
26206       break;
26207     default:
26208       abort ();
26209     }
26210
26211   fragp->fr_var = newsize;
26212   /* Freeze wide instructions that are at or before the same location as
26213      in the previous pass.  This avoids infinite loops.
26214      Don't freeze them unconditionally because targets may be artificially
26215      misaligned by the expansion of preceding frags.  */
26216   if (stretch <= 0 && newsize > 2)
26217     {
26218       md_convert_frag (sec->owner, sec, fragp);
26219       frag_wane (fragp);
26220     }
26221
26222   return newsize - oldsize;
26223 }
26224
26225 /* Round up a section size to the appropriate boundary.  */
26226
26227 valueT
26228 md_section_align (segT   segment ATTRIBUTE_UNUSED,
26229                   valueT size)
26230 {
26231   return size;
26232 }
26233
26234 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
26235    of an rs_align_code fragment.  */
26236
26237 void
26238 arm_handle_align (fragS * fragP)
26239 {
26240   static unsigned char const arm_noop[2][2][4] =
26241     {
26242       {  /* ARMv1 */
26243         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
26244         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
26245       },
26246       {  /* ARMv6k */
26247         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
26248         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
26249       },
26250     };
26251   static unsigned char const thumb_noop[2][2][2] =
26252     {
26253       {  /* Thumb-1 */
26254         {0xc0, 0x46},  /* LE */
26255         {0x46, 0xc0},  /* BE */
26256       },
26257       {  /* Thumb-2 */
26258         {0x00, 0xbf},  /* LE */
26259         {0xbf, 0x00}   /* BE */
26260       }
26261     };
26262   static unsigned char const wide_thumb_noop[2][4] =
26263     {  /* Wide Thumb-2 */
26264       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
26265       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
26266     };
26267
26268   unsigned bytes, fix, noop_size;
26269   char * p;
26270   const unsigned char * noop;
26271   const unsigned char *narrow_noop = NULL;
26272 #ifdef OBJ_ELF
26273   enum mstate state;
26274 #endif
26275
26276   if (fragP->fr_type != rs_align_code)
26277     return;
26278
26279   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
26280   p = fragP->fr_literal + fragP->fr_fix;
26281   fix = 0;
26282
26283   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
26284     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
26285
26286   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
26287
26288   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
26289     {
26290       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
26291                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
26292         {
26293           narrow_noop = thumb_noop[1][target_big_endian];
26294           noop = wide_thumb_noop[target_big_endian];
26295         }
26296       else
26297         noop = thumb_noop[0][target_big_endian];
26298       noop_size = 2;
26299 #ifdef OBJ_ELF
26300       state = MAP_THUMB;
26301 #endif
26302     }
26303   else
26304     {
26305       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
26306                                            ? selected_cpu : arm_arch_none,
26307                                            arm_ext_v6k) != 0]
26308                      [target_big_endian];
26309       noop_size = 4;
26310 #ifdef OBJ_ELF
26311       state = MAP_ARM;
26312 #endif
26313     }
26314
26315   fragP->fr_var = noop_size;
26316
26317   if (bytes & (noop_size - 1))
26318     {
26319       fix = bytes & (noop_size - 1);
26320 #ifdef OBJ_ELF
26321       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
26322 #endif
26323       memset (p, 0, fix);
26324       p += fix;
26325       bytes -= fix;
26326     }
26327
26328   if (narrow_noop)
26329     {
26330       if (bytes & noop_size)
26331         {
26332           /* Insert a narrow noop.  */
26333           memcpy (p, narrow_noop, noop_size);
26334           p += noop_size;
26335           bytes -= noop_size;
26336           fix += noop_size;
26337         }
26338
26339       /* Use wide noops for the remainder */
26340       noop_size = 4;
26341     }
26342
26343   while (bytes >= noop_size)
26344     {
26345       memcpy (p, noop, noop_size);
26346       p += noop_size;
26347       bytes -= noop_size;
26348       fix += noop_size;
26349     }
26350
26351   fragP->fr_fix += fix;
26352 }
26353
26354 /* Called from md_do_align.  Used to create an alignment
26355    frag in a code section.  */
26356
26357 void
26358 arm_frag_align_code (int n, int max)
26359 {
26360   char * p;
26361
26362   /* We assume that there will never be a requirement
26363      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
26364   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
26365     {
26366       char err_msg[128];
26367
26368       sprintf (err_msg,
26369         _("alignments greater than %d bytes not supported in .text sections."),
26370         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
26371       as_fatal ("%s", err_msg);
26372     }
26373
26374   p = frag_var (rs_align_code,
26375                 MAX_MEM_FOR_RS_ALIGN_CODE,
26376                 1,
26377                 (relax_substateT) max,
26378                 (symbolS *) NULL,
26379                 (offsetT) n,
26380                 (char *) NULL);
26381   *p = 0;
26382 }
26383
26384 /* Perform target specific initialisation of a frag.
26385    Note - despite the name this initialisation is not done when the frag
26386    is created, but only when its type is assigned.  A frag can be created
26387    and used a long time before its type is set, so beware of assuming that
26388    this initialisation is performed first.  */
26389
26390 #ifndef OBJ_ELF
26391 void
26392 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
26393 {
26394   /* Record whether this frag is in an ARM or a THUMB area.  */
26395   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
26396 }
26397
26398 #else /* OBJ_ELF is defined.  */
26399 void
26400 arm_init_frag (fragS * fragP, int max_chars)
26401 {
26402   bfd_boolean frag_thumb_mode;
26403
26404   /* If the current ARM vs THUMB mode has not already
26405      been recorded into this frag then do so now.  */
26406   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
26407     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
26408
26409   /* PR 21809: Do not set a mapping state for debug sections
26410      - it just confuses other tools.  */
26411   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
26412     return;
26413
26414   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
26415
26416   /* Record a mapping symbol for alignment frags.  We will delete this
26417      later if the alignment ends up empty.  */
26418   switch (fragP->fr_type)
26419     {
26420     case rs_align:
26421     case rs_align_test:
26422     case rs_fill:
26423       mapping_state_2 (MAP_DATA, max_chars);
26424       break;
26425     case rs_align_code:
26426       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
26427       break;
26428     default:
26429       break;
26430     }
26431 }
26432
26433 /* When we change sections we need to issue a new mapping symbol.  */
26434
26435 void
26436 arm_elf_change_section (void)
26437 {
26438   /* Link an unlinked unwind index table section to the .text section.  */
26439   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
26440       && elf_linked_to_section (now_seg) == NULL)
26441     elf_linked_to_section (now_seg) = text_section;
26442 }
26443
26444 int
26445 arm_elf_section_type (const char * str, size_t len)
26446 {
26447   if (len == 5 && strncmp (str, "exidx", 5) == 0)
26448     return SHT_ARM_EXIDX;
26449
26450   return -1;
26451 }
26452 \f
26453 /* Code to deal with unwinding tables.  */
26454
26455 static void add_unwind_adjustsp (offsetT);
26456
26457 /* Generate any deferred unwind frame offset.  */
26458
26459 static void
26460 flush_pending_unwind (void)
26461 {
26462   offsetT offset;
26463
26464   offset = unwind.pending_offset;
26465   unwind.pending_offset = 0;
26466   if (offset != 0)
26467     add_unwind_adjustsp (offset);
26468 }
26469
26470 /* Add an opcode to this list for this function.  Two-byte opcodes should
26471    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
26472    order.  */
26473
26474 static void
26475 add_unwind_opcode (valueT op, int length)
26476 {
26477   /* Add any deferred stack adjustment.  */
26478   if (unwind.pending_offset)
26479     flush_pending_unwind ();
26480
26481   unwind.sp_restored = 0;
26482
26483   if (unwind.opcode_count + length > unwind.opcode_alloc)
26484     {
26485       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
26486       if (unwind.opcodes)
26487         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
26488                                      unwind.opcode_alloc);
26489       else
26490         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
26491     }
26492   while (length > 0)
26493     {
26494       length--;
26495       unwind.opcodes[unwind.opcode_count] = op & 0xff;
26496       op >>= 8;
26497       unwind.opcode_count++;
26498     }
26499 }
26500
26501 /* Add unwind opcodes to adjust the stack pointer.  */
26502
26503 static void
26504 add_unwind_adjustsp (offsetT offset)
26505 {
26506   valueT op;
26507
26508   if (offset > 0x200)
26509     {
26510       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
26511       char bytes[5];
26512       int n;
26513       valueT o;
26514
26515       /* Long form: 0xb2, uleb128.  */
26516       /* This might not fit in a word so add the individual bytes,
26517          remembering the list is built in reverse order.  */
26518       o = (valueT) ((offset - 0x204) >> 2);
26519       if (o == 0)
26520         add_unwind_opcode (0, 1);
26521
26522       /* Calculate the uleb128 encoding of the offset.  */
26523       n = 0;
26524       while (o)
26525         {
26526           bytes[n] = o & 0x7f;
26527           o >>= 7;
26528           if (o)
26529             bytes[n] |= 0x80;
26530           n++;
26531         }
26532       /* Add the insn.  */
26533       for (; n; n--)
26534         add_unwind_opcode (bytes[n - 1], 1);
26535       add_unwind_opcode (0xb2, 1);
26536     }
26537   else if (offset > 0x100)
26538     {
26539       /* Two short opcodes.  */
26540       add_unwind_opcode (0x3f, 1);
26541       op = (offset - 0x104) >> 2;
26542       add_unwind_opcode (op, 1);
26543     }
26544   else if (offset > 0)
26545     {
26546       /* Short opcode.  */
26547       op = (offset - 4) >> 2;
26548       add_unwind_opcode (op, 1);
26549     }
26550   else if (offset < 0)
26551     {
26552       offset = -offset;
26553       while (offset > 0x100)
26554         {
26555           add_unwind_opcode (0x7f, 1);
26556           offset -= 0x100;
26557         }
26558       op = ((offset - 4) >> 2) | 0x40;
26559       add_unwind_opcode (op, 1);
26560     }
26561 }
26562
26563 /* Finish the list of unwind opcodes for this function.  */
26564
26565 static void
26566 finish_unwind_opcodes (void)
26567 {
26568   valueT op;
26569
26570   if (unwind.fp_used)
26571     {
26572       /* Adjust sp as necessary.  */
26573       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
26574       flush_pending_unwind ();
26575
26576       /* After restoring sp from the frame pointer.  */
26577       op = 0x90 | unwind.fp_reg;
26578       add_unwind_opcode (op, 1);
26579     }
26580   else
26581     flush_pending_unwind ();
26582 }
26583
26584
26585 /* Start an exception table entry.  If idx is nonzero this is an index table
26586    entry.  */
26587
26588 static void
26589 start_unwind_section (const segT text_seg, int idx)
26590 {
26591   const char * text_name;
26592   const char * prefix;
26593   const char * prefix_once;
26594   const char * group_name;
26595   char * sec_name;
26596   int type;
26597   int flags;
26598   int linkonce;
26599
26600   if (idx)
26601     {
26602       prefix = ELF_STRING_ARM_unwind;
26603       prefix_once = ELF_STRING_ARM_unwind_once;
26604       type = SHT_ARM_EXIDX;
26605     }
26606   else
26607     {
26608       prefix = ELF_STRING_ARM_unwind_info;
26609       prefix_once = ELF_STRING_ARM_unwind_info_once;
26610       type = SHT_PROGBITS;
26611     }
26612
26613   text_name = segment_name (text_seg);
26614   if (streq (text_name, ".text"))
26615     text_name = "";
26616
26617   if (strncmp (text_name, ".gnu.linkonce.t.",
26618                strlen (".gnu.linkonce.t.")) == 0)
26619     {
26620       prefix = prefix_once;
26621       text_name += strlen (".gnu.linkonce.t.");
26622     }
26623
26624   sec_name = concat (prefix, text_name, (char *) NULL);
26625
26626   flags = SHF_ALLOC;
26627   linkonce = 0;
26628   group_name = 0;
26629
26630   /* Handle COMDAT group.  */
26631   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
26632     {
26633       group_name = elf_group_name (text_seg);
26634       if (group_name == NULL)
26635         {
26636           as_bad (_("Group section `%s' has no group signature"),
26637                   segment_name (text_seg));
26638           ignore_rest_of_line ();
26639           return;
26640         }
26641       flags |= SHF_GROUP;
26642       linkonce = 1;
26643     }
26644
26645   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
26646                           linkonce, 0);
26647
26648   /* Set the section link for index tables.  */
26649   if (idx)
26650     elf_linked_to_section (now_seg) = text_seg;
26651 }
26652
26653
26654 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
26655    personality routine data.  Returns zero, or the index table value for
26656    an inline entry.  */
26657
26658 static valueT
26659 create_unwind_entry (int have_data)
26660 {
26661   int size;
26662   addressT where;
26663   char *ptr;
26664   /* The current word of data.  */
26665   valueT data;
26666   /* The number of bytes left in this word.  */
26667   int n;
26668
26669   finish_unwind_opcodes ();
26670
26671   /* Remember the current text section.  */
26672   unwind.saved_seg = now_seg;
26673   unwind.saved_subseg = now_subseg;
26674
26675   start_unwind_section (now_seg, 0);
26676
26677   if (unwind.personality_routine == NULL)
26678     {
26679       if (unwind.personality_index == -2)
26680         {
26681           if (have_data)
26682             as_bad (_("handlerdata in cantunwind frame"));
26683           return 1; /* EXIDX_CANTUNWIND.  */
26684         }
26685
26686       /* Use a default personality routine if none is specified.  */
26687       if (unwind.personality_index == -1)
26688         {
26689           if (unwind.opcode_count > 3)
26690             unwind.personality_index = 1;
26691           else
26692             unwind.personality_index = 0;
26693         }
26694
26695       /* Space for the personality routine entry.  */
26696       if (unwind.personality_index == 0)
26697         {
26698           if (unwind.opcode_count > 3)
26699             as_bad (_("too many unwind opcodes for personality routine 0"));
26700
26701           if (!have_data)
26702             {
26703               /* All the data is inline in the index table.  */
26704               data = 0x80;
26705               n = 3;
26706               while (unwind.opcode_count > 0)
26707                 {
26708                   unwind.opcode_count--;
26709                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
26710                   n--;
26711                 }
26712
26713               /* Pad with "finish" opcodes.  */
26714               while (n--)
26715                 data = (data << 8) | 0xb0;
26716
26717               return data;
26718             }
26719           size = 0;
26720         }
26721       else
26722         /* We get two opcodes "free" in the first word.  */
26723         size = unwind.opcode_count - 2;
26724     }
26725   else
26726     {
26727       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
26728       if (unwind.personality_index != -1)
26729         {
26730           as_bad (_("attempt to recreate an unwind entry"));
26731           return 1;
26732         }
26733
26734       /* An extra byte is required for the opcode count.        */
26735       size = unwind.opcode_count + 1;
26736     }
26737
26738   size = (size + 3) >> 2;
26739   if (size > 0xff)
26740     as_bad (_("too many unwind opcodes"));
26741
26742   frag_align (2, 0, 0);
26743   record_alignment (now_seg, 2);
26744   unwind.table_entry = expr_build_dot ();
26745
26746   /* Allocate the table entry.  */
26747   ptr = frag_more ((size << 2) + 4);
26748   /* PR 13449: Zero the table entries in case some of them are not used.  */
26749   memset (ptr, 0, (size << 2) + 4);
26750   where = frag_now_fix () - ((size << 2) + 4);
26751
26752   switch (unwind.personality_index)
26753     {
26754     case -1:
26755       /* ??? Should this be a PLT generating relocation?  */
26756       /* Custom personality routine.  */
26757       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
26758                BFD_RELOC_ARM_PREL31);
26759
26760       where += 4;
26761       ptr += 4;
26762
26763       /* Set the first byte to the number of additional words.  */
26764       data = size > 0 ? size - 1 : 0;
26765       n = 3;
26766       break;
26767
26768     /* ABI defined personality routines.  */
26769     case 0:
26770       /* Three opcodes bytes are packed into the first word.  */
26771       data = 0x80;
26772       n = 3;
26773       break;
26774
26775     case 1:
26776     case 2:
26777       /* The size and first two opcode bytes go in the first word.  */
26778       data = ((0x80 + unwind.personality_index) << 8) | size;
26779       n = 2;
26780       break;
26781
26782     default:
26783       /* Should never happen.  */
26784       abort ();
26785     }
26786
26787   /* Pack the opcodes into words (MSB first), reversing the list at the same
26788      time.  */
26789   while (unwind.opcode_count > 0)
26790     {
26791       if (n == 0)
26792         {
26793           md_number_to_chars (ptr, data, 4);
26794           ptr += 4;
26795           n = 4;
26796           data = 0;
26797         }
26798       unwind.opcode_count--;
26799       n--;
26800       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
26801     }
26802
26803   /* Finish off the last word.  */
26804   if (n < 4)
26805     {
26806       /* Pad with "finish" opcodes.  */
26807       while (n--)
26808         data = (data << 8) | 0xb0;
26809
26810       md_number_to_chars (ptr, data, 4);
26811     }
26812
26813   if (!have_data)
26814     {
26815       /* Add an empty descriptor if there is no user-specified data.   */
26816       ptr = frag_more (4);
26817       md_number_to_chars (ptr, 0, 4);
26818     }
26819
26820   return 0;
26821 }
26822
26823
26824 /* Initialize the DWARF-2 unwind information for this procedure.  */
26825
26826 void
26827 tc_arm_frame_initial_instructions (void)
26828 {
26829   cfi_add_CFA_def_cfa (REG_SP, 0);
26830 }
26831 #endif /* OBJ_ELF */
26832
26833 /* Convert REGNAME to a DWARF-2 register number.  */
26834
26835 int
26836 tc_arm_regname_to_dw2regnum (char *regname)
26837 {
26838   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
26839   if (reg != FAIL)
26840     return reg;
26841
26842   /* PR 16694: Allow VFP registers as well.  */
26843   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
26844   if (reg != FAIL)
26845     return 64 + reg;
26846
26847   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
26848   if (reg != FAIL)
26849     return reg + 256;
26850
26851   return FAIL;
26852 }
26853
26854 #ifdef TE_PE
26855 void
26856 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
26857 {
26858   expressionS exp;
26859
26860   exp.X_op = O_secrel;
26861   exp.X_add_symbol = symbol;
26862   exp.X_add_number = 0;
26863   emit_expr (&exp, size);
26864 }
26865 #endif
26866
26867 /* MD interface: Symbol and relocation handling.  */
26868
26869 /* Return the address within the segment that a PC-relative fixup is
26870    relative to.  For ARM, PC-relative fixups applied to instructions
26871    are generally relative to the location of the fixup plus 8 bytes.
26872    Thumb branches are offset by 4, and Thumb loads relative to PC
26873    require special handling.  */
26874
26875 long
26876 md_pcrel_from_section (fixS * fixP, segT seg)
26877 {
26878   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
26879
26880   /* If this is pc-relative and we are going to emit a relocation
26881      then we just want to put out any pipeline compensation that the linker
26882      will need.  Otherwise we want to use the calculated base.
26883      For WinCE we skip the bias for externals as well, since this
26884      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
26885   if (fixP->fx_pcrel
26886       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
26887           || (arm_force_relocation (fixP)
26888 #ifdef TE_WINCE
26889               && !S_IS_EXTERNAL (fixP->fx_addsy)
26890 #endif
26891               )))
26892     base = 0;
26893
26894
26895   switch (fixP->fx_r_type)
26896     {
26897       /* PC relative addressing on the Thumb is slightly odd as the
26898          bottom two bits of the PC are forced to zero for the
26899          calculation.  This happens *after* application of the
26900          pipeline offset.  However, Thumb adrl already adjusts for
26901          this, so we need not do it again.  */
26902     case BFD_RELOC_ARM_THUMB_ADD:
26903       return base & ~3;
26904
26905     case BFD_RELOC_ARM_THUMB_OFFSET:
26906     case BFD_RELOC_ARM_T32_OFFSET_IMM:
26907     case BFD_RELOC_ARM_T32_ADD_PC12:
26908     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
26909       return (base + 4) & ~3;
26910
26911       /* Thumb branches are simply offset by +4.  */
26912     case BFD_RELOC_THUMB_PCREL_BRANCH5:
26913     case BFD_RELOC_THUMB_PCREL_BRANCH7:
26914     case BFD_RELOC_THUMB_PCREL_BRANCH9:
26915     case BFD_RELOC_THUMB_PCREL_BRANCH12:
26916     case BFD_RELOC_THUMB_PCREL_BRANCH20:
26917     case BFD_RELOC_THUMB_PCREL_BRANCH25:
26918     case BFD_RELOC_THUMB_PCREL_BFCSEL:
26919     case BFD_RELOC_ARM_THUMB_BF17:
26920     case BFD_RELOC_ARM_THUMB_BF19:
26921     case BFD_RELOC_ARM_THUMB_BF13:
26922     case BFD_RELOC_ARM_THUMB_LOOP12:
26923       return base + 4;
26924
26925     case BFD_RELOC_THUMB_PCREL_BRANCH23:
26926       if (fixP->fx_addsy
26927           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26928           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26929           && ARM_IS_FUNC (fixP->fx_addsy)
26930           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26931         base = fixP->fx_where + fixP->fx_frag->fr_address;
26932        return base + 4;
26933
26934       /* BLX is like branches above, but forces the low two bits of PC to
26935          zero.  */
26936     case BFD_RELOC_THUMB_PCREL_BLX:
26937       if (fixP->fx_addsy
26938           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26939           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26940           && THUMB_IS_FUNC (fixP->fx_addsy)
26941           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26942         base = fixP->fx_where + fixP->fx_frag->fr_address;
26943       return (base + 4) & ~3;
26944
26945       /* ARM mode branches are offset by +8.  However, the Windows CE
26946          loader expects the relocation not to take this into account.  */
26947     case BFD_RELOC_ARM_PCREL_BLX:
26948       if (fixP->fx_addsy
26949           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26950           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26951           && ARM_IS_FUNC (fixP->fx_addsy)
26952           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26953         base = fixP->fx_where + fixP->fx_frag->fr_address;
26954       return base + 8;
26955
26956     case BFD_RELOC_ARM_PCREL_CALL:
26957       if (fixP->fx_addsy
26958           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26959           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26960           && THUMB_IS_FUNC (fixP->fx_addsy)
26961           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26962         base = fixP->fx_where + fixP->fx_frag->fr_address;
26963       return base + 8;
26964
26965     case BFD_RELOC_ARM_PCREL_BRANCH:
26966     case BFD_RELOC_ARM_PCREL_JUMP:
26967     case BFD_RELOC_ARM_PLT32:
26968 #ifdef TE_WINCE
26969       /* When handling fixups immediately, because we have already
26970          discovered the value of a symbol, or the address of the frag involved
26971          we must account for the offset by +8, as the OS loader will never see the reloc.
26972          see fixup_segment() in write.c
26973          The S_IS_EXTERNAL test handles the case of global symbols.
26974          Those need the calculated base, not just the pipe compensation the linker will need.  */
26975       if (fixP->fx_pcrel
26976           && fixP->fx_addsy != NULL
26977           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26978           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
26979         return base + 8;
26980       return base;
26981 #else
26982       return base + 8;
26983 #endif
26984
26985
26986       /* ARM mode loads relative to PC are also offset by +8.  Unlike
26987          branches, the Windows CE loader *does* expect the relocation
26988          to take this into account.  */
26989     case BFD_RELOC_ARM_OFFSET_IMM:
26990     case BFD_RELOC_ARM_OFFSET_IMM8:
26991     case BFD_RELOC_ARM_HWLITERAL:
26992     case BFD_RELOC_ARM_LITERAL:
26993     case BFD_RELOC_ARM_CP_OFF_IMM:
26994       return base + 8;
26995
26996
26997       /* Other PC-relative relocations are un-offset.  */
26998     default:
26999       return base;
27000     }
27001 }
27002
27003 static bfd_boolean flag_warn_syms = TRUE;
27004
27005 bfd_boolean
27006 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
27007 {
27008   /* PR 18347 - Warn if the user attempts to create a symbol with the same
27009      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
27010      does mean that the resulting code might be very confusing to the reader.
27011      Also this warning can be triggered if the user omits an operand before
27012      an immediate address, eg:
27013
27014        LDR =foo
27015
27016      GAS treats this as an assignment of the value of the symbol foo to a
27017      symbol LDR, and so (without this code) it will not issue any kind of
27018      warning or error message.
27019
27020      Note - ARM instructions are case-insensitive but the strings in the hash
27021      table are all stored in lower case, so we must first ensure that name is
27022      lower case too.  */
27023   if (flag_warn_syms && arm_ops_hsh)
27024     {
27025       char * nbuf = strdup (name);
27026       char * p;
27027
27028       for (p = nbuf; *p; p++)
27029         *p = TOLOWER (*p);
27030       if (hash_find (arm_ops_hsh, nbuf) != NULL)
27031         {
27032           static struct hash_control * already_warned = NULL;
27033
27034           if (already_warned == NULL)
27035             already_warned = hash_new ();
27036           /* Only warn about the symbol once.  To keep the code
27037              simple we let hash_insert do the lookup for us.  */
27038           if (hash_insert (already_warned, nbuf, NULL) == NULL)
27039             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
27040         }
27041       else
27042         free (nbuf);
27043     }
27044
27045   return FALSE;
27046 }
27047
27048 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
27049    Otherwise we have no need to default values of symbols.  */
27050
27051 symbolS *
27052 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
27053 {
27054 #ifdef OBJ_ELF
27055   if (name[0] == '_' && name[1] == 'G'
27056       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
27057     {
27058       if (!GOT_symbol)
27059         {
27060           if (symbol_find (name))
27061             as_bad (_("GOT already in the symbol table"));
27062
27063           GOT_symbol = symbol_new (name, undefined_section,
27064                                    (valueT) 0, & zero_address_frag);
27065         }
27066
27067       return GOT_symbol;
27068     }
27069 #endif
27070
27071   return NULL;
27072 }
27073
27074 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
27075    computed as two separate immediate values, added together.  We
27076    already know that this value cannot be computed by just one ARM
27077    instruction.  */
27078
27079 static unsigned int
27080 validate_immediate_twopart (unsigned int   val,
27081                             unsigned int * highpart)
27082 {
27083   unsigned int a;
27084   unsigned int i;
27085
27086   for (i = 0; i < 32; i += 2)
27087     if (((a = rotate_left (val, i)) & 0xff) != 0)
27088       {
27089         if (a & 0xff00)
27090           {
27091             if (a & ~ 0xffff)
27092               continue;
27093             * highpart = (a  >> 8) | ((i + 24) << 7);
27094           }
27095         else if (a & 0xff0000)
27096           {
27097             if (a & 0xff000000)
27098               continue;
27099             * highpart = (a >> 16) | ((i + 16) << 7);
27100           }
27101         else
27102           {
27103             gas_assert (a & 0xff000000);
27104             * highpart = (a >> 24) | ((i + 8) << 7);
27105           }
27106
27107         return (a & 0xff) | (i << 7);
27108       }
27109
27110   return FAIL;
27111 }
27112
27113 static int
27114 validate_offset_imm (unsigned int val, int hwse)
27115 {
27116   if ((hwse && val > 255) || val > 4095)
27117     return FAIL;
27118   return val;
27119 }
27120
27121 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
27122    negative immediate constant by altering the instruction.  A bit of
27123    a hack really.
27124         MOV <-> MVN
27125         AND <-> BIC
27126         ADC <-> SBC
27127         by inverting the second operand, and
27128         ADD <-> SUB
27129         CMP <-> CMN
27130         by negating the second operand.  */
27131
27132 static int
27133 negate_data_op (unsigned long * instruction,
27134                 unsigned long   value)
27135 {
27136   int op, new_inst;
27137   unsigned long negated, inverted;
27138
27139   negated = encode_arm_immediate (-value);
27140   inverted = encode_arm_immediate (~value);
27141
27142   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
27143   switch (op)
27144     {
27145       /* First negates.  */
27146     case OPCODE_SUB:             /* ADD <-> SUB  */
27147       new_inst = OPCODE_ADD;
27148       value = negated;
27149       break;
27150
27151     case OPCODE_ADD:
27152       new_inst = OPCODE_SUB;
27153       value = negated;
27154       break;
27155
27156     case OPCODE_CMP:             /* CMP <-> CMN  */
27157       new_inst = OPCODE_CMN;
27158       value = negated;
27159       break;
27160
27161     case OPCODE_CMN:
27162       new_inst = OPCODE_CMP;
27163       value = negated;
27164       break;
27165
27166       /* Now Inverted ops.  */
27167     case OPCODE_MOV:             /* MOV <-> MVN  */
27168       new_inst = OPCODE_MVN;
27169       value = inverted;
27170       break;
27171
27172     case OPCODE_MVN:
27173       new_inst = OPCODE_MOV;
27174       value = inverted;
27175       break;
27176
27177     case OPCODE_AND:             /* AND <-> BIC  */
27178       new_inst = OPCODE_BIC;
27179       value = inverted;
27180       break;
27181
27182     case OPCODE_BIC:
27183       new_inst = OPCODE_AND;
27184       value = inverted;
27185       break;
27186
27187     case OPCODE_ADC:              /* ADC <-> SBC  */
27188       new_inst = OPCODE_SBC;
27189       value = inverted;
27190       break;
27191
27192     case OPCODE_SBC:
27193       new_inst = OPCODE_ADC;
27194       value = inverted;
27195       break;
27196
27197       /* We cannot do anything.  */
27198     default:
27199       return FAIL;
27200     }
27201
27202   if (value == (unsigned) FAIL)
27203     return FAIL;
27204
27205   *instruction &= OPCODE_MASK;
27206   *instruction |= new_inst << DATA_OP_SHIFT;
27207   return value;
27208 }
27209
27210 /* Like negate_data_op, but for Thumb-2.   */
27211
27212 static unsigned int
27213 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
27214 {
27215   int op, new_inst;
27216   int rd;
27217   unsigned int negated, inverted;
27218
27219   negated = encode_thumb32_immediate (-value);
27220   inverted = encode_thumb32_immediate (~value);
27221
27222   rd = (*instruction >> 8) & 0xf;
27223   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
27224   switch (op)
27225     {
27226       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
27227     case T2_OPCODE_SUB:
27228       new_inst = T2_OPCODE_ADD;
27229       value = negated;
27230       break;
27231
27232     case T2_OPCODE_ADD:
27233       new_inst = T2_OPCODE_SUB;
27234       value = negated;
27235       break;
27236
27237       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
27238     case T2_OPCODE_ORR:
27239       new_inst = T2_OPCODE_ORN;
27240       value = inverted;
27241       break;
27242
27243     case T2_OPCODE_ORN:
27244       new_inst = T2_OPCODE_ORR;
27245       value = inverted;
27246       break;
27247
27248       /* AND <-> BIC.  TST has no inverted equivalent.  */
27249     case T2_OPCODE_AND:
27250       new_inst = T2_OPCODE_BIC;
27251       if (rd == 15)
27252         value = FAIL;
27253       else
27254         value = inverted;
27255       break;
27256
27257     case T2_OPCODE_BIC:
27258       new_inst = T2_OPCODE_AND;
27259       value = inverted;
27260       break;
27261
27262       /* ADC <-> SBC  */
27263     case T2_OPCODE_ADC:
27264       new_inst = T2_OPCODE_SBC;
27265       value = inverted;
27266       break;
27267
27268     case T2_OPCODE_SBC:
27269       new_inst = T2_OPCODE_ADC;
27270       value = inverted;
27271       break;
27272
27273       /* We cannot do anything.  */
27274     default:
27275       return FAIL;
27276     }
27277
27278   if (value == (unsigned int)FAIL)
27279     return FAIL;
27280
27281   *instruction &= T2_OPCODE_MASK;
27282   *instruction |= new_inst << T2_DATA_OP_SHIFT;
27283   return value;
27284 }
27285
27286 /* Read a 32-bit thumb instruction from buf.  */
27287
27288 static unsigned long
27289 get_thumb32_insn (char * buf)
27290 {
27291   unsigned long insn;
27292   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
27293   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27294
27295   return insn;
27296 }
27297
27298 /* We usually want to set the low bit on the address of thumb function
27299    symbols.  In particular .word foo - . should have the low bit set.
27300    Generic code tries to fold the difference of two symbols to
27301    a constant.  Prevent this and force a relocation when the first symbols
27302    is a thumb function.  */
27303
27304 bfd_boolean
27305 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
27306 {
27307   if (op == O_subtract
27308       && l->X_op == O_symbol
27309       && r->X_op == O_symbol
27310       && THUMB_IS_FUNC (l->X_add_symbol))
27311     {
27312       l->X_op = O_subtract;
27313       l->X_op_symbol = r->X_add_symbol;
27314       l->X_add_number -= r->X_add_number;
27315       return TRUE;
27316     }
27317
27318   /* Process as normal.  */
27319   return FALSE;
27320 }
27321
27322 /* Encode Thumb2 unconditional branches and calls. The encoding
27323    for the 2 are identical for the immediate values.  */
27324
27325 static void
27326 encode_thumb2_b_bl_offset (char * buf, offsetT value)
27327 {
27328 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
27329   offsetT newval;
27330   offsetT newval2;
27331   addressT S, I1, I2, lo, hi;
27332
27333   S = (value >> 24) & 0x01;
27334   I1 = (value >> 23) & 0x01;
27335   I2 = (value >> 22) & 0x01;
27336   hi = (value >> 12) & 0x3ff;
27337   lo = (value >> 1) & 0x7ff;
27338   newval   = md_chars_to_number (buf, THUMB_SIZE);
27339   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27340   newval  |= (S << 10) | hi;
27341   newval2 &=  ~T2I1I2MASK;
27342   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
27343   md_number_to_chars (buf, newval, THUMB_SIZE);
27344   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
27345 }
27346
27347 void
27348 md_apply_fix (fixS *    fixP,
27349                valueT * valP,
27350                segT     seg)
27351 {
27352   offsetT        value = * valP;
27353   offsetT        newval;
27354   unsigned int   newimm;
27355   unsigned long  temp;
27356   int            sign;
27357   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
27358
27359   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
27360
27361   /* Note whether this will delete the relocation.  */
27362
27363   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
27364     fixP->fx_done = 1;
27365
27366   /* On a 64-bit host, silently truncate 'value' to 32 bits for
27367      consistency with the behaviour on 32-bit hosts.  Remember value
27368      for emit_reloc.  */
27369   value &= 0xffffffff;
27370   value ^= 0x80000000;
27371   value -= 0x80000000;
27372
27373   *valP = value;
27374   fixP->fx_addnumber = value;
27375
27376   /* Same treatment for fixP->fx_offset.  */
27377   fixP->fx_offset &= 0xffffffff;
27378   fixP->fx_offset ^= 0x80000000;
27379   fixP->fx_offset -= 0x80000000;
27380
27381   switch (fixP->fx_r_type)
27382     {
27383     case BFD_RELOC_NONE:
27384       /* This will need to go in the object file.  */
27385       fixP->fx_done = 0;
27386       break;
27387
27388     case BFD_RELOC_ARM_IMMEDIATE:
27389       /* We claim that this fixup has been processed here,
27390          even if in fact we generate an error because we do
27391          not have a reloc for it, so tc_gen_reloc will reject it.  */
27392       fixP->fx_done = 1;
27393
27394       if (fixP->fx_addsy)
27395         {
27396           const char *msg = 0;
27397
27398           if (! S_IS_DEFINED (fixP->fx_addsy))
27399             msg = _("undefined symbol %s used as an immediate value");
27400           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
27401             msg = _("symbol %s is in a different section");
27402           else if (S_IS_WEAK (fixP->fx_addsy))
27403             msg = _("symbol %s is weak and may be overridden later");
27404
27405           if (msg)
27406             {
27407               as_bad_where (fixP->fx_file, fixP->fx_line,
27408                             msg, S_GET_NAME (fixP->fx_addsy));
27409               break;
27410             }
27411         }
27412
27413       temp = md_chars_to_number (buf, INSN_SIZE);
27414
27415       /* If the offset is negative, we should use encoding A2 for ADR.  */
27416       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
27417         newimm = negate_data_op (&temp, value);
27418       else
27419         {
27420           newimm = encode_arm_immediate (value);
27421
27422           /* If the instruction will fail, see if we can fix things up by
27423              changing the opcode.  */
27424           if (newimm == (unsigned int) FAIL)
27425             newimm = negate_data_op (&temp, value);
27426           /* MOV accepts both ARM modified immediate (A1 encoding) and
27427              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
27428              When disassembling, MOV is preferred when there is no encoding
27429              overlap.  */
27430           if (newimm == (unsigned int) FAIL
27431               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
27432               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
27433               && !((temp >> SBIT_SHIFT) & 0x1)
27434               && value >= 0 && value <= 0xffff)
27435             {
27436               /* Clear bits[23:20] to change encoding from A1 to A2.  */
27437               temp &= 0xff0fffff;
27438               /* Encoding high 4bits imm.  Code below will encode the remaining
27439                  low 12bits.  */
27440               temp |= (value & 0x0000f000) << 4;
27441               newimm = value & 0x00000fff;
27442             }
27443         }
27444
27445       if (newimm == (unsigned int) FAIL)
27446         {
27447           as_bad_where (fixP->fx_file, fixP->fx_line,
27448                         _("invalid constant (%lx) after fixup"),
27449                         (unsigned long) value);
27450           break;
27451         }
27452
27453       newimm |= (temp & 0xfffff000);
27454       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
27455       break;
27456
27457     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
27458       {
27459         unsigned int highpart = 0;
27460         unsigned int newinsn  = 0xe1a00000; /* nop.  */
27461
27462         if (fixP->fx_addsy)
27463           {
27464             const char *msg = 0;
27465
27466             if (! S_IS_DEFINED (fixP->fx_addsy))
27467               msg = _("undefined symbol %s used as an immediate value");
27468             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
27469               msg = _("symbol %s is in a different section");
27470             else if (S_IS_WEAK (fixP->fx_addsy))
27471               msg = _("symbol %s is weak and may be overridden later");
27472
27473             if (msg)
27474               {
27475                 as_bad_where (fixP->fx_file, fixP->fx_line,
27476                               msg, S_GET_NAME (fixP->fx_addsy));
27477                 break;
27478               }
27479           }
27480
27481         newimm = encode_arm_immediate (value);
27482         temp = md_chars_to_number (buf, INSN_SIZE);
27483
27484         /* If the instruction will fail, see if we can fix things up by
27485            changing the opcode.  */
27486         if (newimm == (unsigned int) FAIL
27487             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
27488           {
27489             /* No ?  OK - try using two ADD instructions to generate
27490                the value.  */
27491             newimm = validate_immediate_twopart (value, & highpart);
27492
27493             /* Yes - then make sure that the second instruction is
27494                also an add.  */
27495             if (newimm != (unsigned int) FAIL)
27496               newinsn = temp;
27497             /* Still No ?  Try using a negated value.  */
27498             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
27499               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
27500             /* Otherwise - give up.  */
27501             else
27502               {
27503                 as_bad_where (fixP->fx_file, fixP->fx_line,
27504                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
27505                               (long) value);
27506                 break;
27507               }
27508
27509             /* Replace the first operand in the 2nd instruction (which
27510                is the PC) with the destination register.  We have
27511                already added in the PC in the first instruction and we
27512                do not want to do it again.  */
27513             newinsn &= ~ 0xf0000;
27514             newinsn |= ((newinsn & 0x0f000) << 4);
27515           }
27516
27517         newimm |= (temp & 0xfffff000);
27518         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
27519
27520         highpart |= (newinsn & 0xfffff000);
27521         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
27522       }
27523       break;
27524
27525     case BFD_RELOC_ARM_OFFSET_IMM:
27526       if (!fixP->fx_done && seg->use_rela_p)
27527         value = 0;
27528       /* Fall through.  */
27529
27530     case BFD_RELOC_ARM_LITERAL:
27531       sign = value > 0;
27532
27533       if (value < 0)
27534         value = - value;
27535
27536       if (validate_offset_imm (value, 0) == FAIL)
27537         {
27538           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
27539             as_bad_where (fixP->fx_file, fixP->fx_line,
27540                           _("invalid literal constant: pool needs to be closer"));
27541           else
27542             as_bad_where (fixP->fx_file, fixP->fx_line,
27543                           _("bad immediate value for offset (%ld)"),
27544                           (long) value);
27545           break;
27546         }
27547
27548       newval = md_chars_to_number (buf, INSN_SIZE);
27549       if (value == 0)
27550         newval &= 0xfffff000;
27551       else
27552         {
27553           newval &= 0xff7ff000;
27554           newval |= value | (sign ? INDEX_UP : 0);
27555         }
27556       md_number_to_chars (buf, newval, INSN_SIZE);
27557       break;
27558
27559     case BFD_RELOC_ARM_OFFSET_IMM8:
27560     case BFD_RELOC_ARM_HWLITERAL:
27561       sign = value > 0;
27562
27563       if (value < 0)
27564         value = - value;
27565
27566       if (validate_offset_imm (value, 1) == FAIL)
27567         {
27568           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
27569             as_bad_where (fixP->fx_file, fixP->fx_line,
27570                           _("invalid literal constant: pool needs to be closer"));
27571           else
27572             as_bad_where (fixP->fx_file, fixP->fx_line,
27573                           _("bad immediate value for 8-bit offset (%ld)"),
27574                           (long) value);
27575           break;
27576         }
27577
27578       newval = md_chars_to_number (buf, INSN_SIZE);
27579       if (value == 0)
27580         newval &= 0xfffff0f0;
27581       else
27582         {
27583           newval &= 0xff7ff0f0;
27584           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
27585         }
27586       md_number_to_chars (buf, newval, INSN_SIZE);
27587       break;
27588
27589     case BFD_RELOC_ARM_T32_OFFSET_U8:
27590       if (value < 0 || value > 1020 || value % 4 != 0)
27591         as_bad_where (fixP->fx_file, fixP->fx_line,
27592                       _("bad immediate value for offset (%ld)"), (long) value);
27593       value /= 4;
27594
27595       newval = md_chars_to_number (buf+2, THUMB_SIZE);
27596       newval |= value;
27597       md_number_to_chars (buf+2, newval, THUMB_SIZE);
27598       break;
27599
27600     case BFD_RELOC_ARM_T32_OFFSET_IMM:
27601       /* This is a complicated relocation used for all varieties of Thumb32
27602          load/store instruction with immediate offset:
27603
27604          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
27605                                                    *4, optional writeback(W)
27606                                                    (doubleword load/store)
27607
27608          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
27609          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
27610          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
27611          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
27612          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
27613
27614          Uppercase letters indicate bits that are already encoded at
27615          this point.  Lowercase letters are our problem.  For the
27616          second block of instructions, the secondary opcode nybble
27617          (bits 8..11) is present, and bit 23 is zero, even if this is
27618          a PC-relative operation.  */
27619       newval = md_chars_to_number (buf, THUMB_SIZE);
27620       newval <<= 16;
27621       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
27622
27623       if ((newval & 0xf0000000) == 0xe0000000)
27624         {
27625           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
27626           if (value >= 0)
27627             newval |= (1 << 23);
27628           else
27629             value = -value;
27630           if (value % 4 != 0)
27631             {
27632               as_bad_where (fixP->fx_file, fixP->fx_line,
27633                             _("offset not a multiple of 4"));
27634               break;
27635             }
27636           value /= 4;
27637           if (value > 0xff)
27638             {
27639               as_bad_where (fixP->fx_file, fixP->fx_line,
27640                             _("offset out of range"));
27641               break;
27642             }
27643           newval &= ~0xff;
27644         }
27645       else if ((newval & 0x000f0000) == 0x000f0000)
27646         {
27647           /* PC-relative, 12-bit offset.  */
27648           if (value >= 0)
27649             newval |= (1 << 23);
27650           else
27651             value = -value;
27652           if (value > 0xfff)
27653             {
27654               as_bad_where (fixP->fx_file, fixP->fx_line,
27655                             _("offset out of range"));
27656               break;
27657             }
27658           newval &= ~0xfff;
27659         }
27660       else if ((newval & 0x00000100) == 0x00000100)
27661         {
27662           /* Writeback: 8-bit, +/- offset.  */
27663           if (value >= 0)
27664             newval |= (1 << 9);
27665           else
27666             value = -value;
27667           if (value > 0xff)
27668             {
27669               as_bad_where (fixP->fx_file, fixP->fx_line,
27670                             _("offset out of range"));
27671               break;
27672             }
27673           newval &= ~0xff;
27674         }
27675       else if ((newval & 0x00000f00) == 0x00000e00)
27676         {
27677           /* T-instruction: positive 8-bit offset.  */
27678           if (value < 0 || value > 0xff)
27679             {
27680               as_bad_where (fixP->fx_file, fixP->fx_line,
27681                             _("offset out of range"));
27682               break;
27683             }
27684           newval &= ~0xff;
27685           newval |= value;
27686         }
27687       else
27688         {
27689           /* Positive 12-bit or negative 8-bit offset.  */
27690           int limit;
27691           if (value >= 0)
27692             {
27693               newval |= (1 << 23);
27694               limit = 0xfff;
27695             }
27696           else
27697             {
27698               value = -value;
27699               limit = 0xff;
27700             }
27701           if (value > limit)
27702             {
27703               as_bad_where (fixP->fx_file, fixP->fx_line,
27704                             _("offset out of range"));
27705               break;
27706             }
27707           newval &= ~limit;
27708         }
27709
27710       newval |= value;
27711       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
27712       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
27713       break;
27714
27715     case BFD_RELOC_ARM_SHIFT_IMM:
27716       newval = md_chars_to_number (buf, INSN_SIZE);
27717       if (((unsigned long) value) > 32
27718           || (value == 32
27719               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
27720         {
27721           as_bad_where (fixP->fx_file, fixP->fx_line,
27722                         _("shift expression is too large"));
27723           break;
27724         }
27725
27726       if (value == 0)
27727         /* Shifts of zero must be done as lsl.  */
27728         newval &= ~0x60;
27729       else if (value == 32)
27730         value = 0;
27731       newval &= 0xfffff07f;
27732       newval |= (value & 0x1f) << 7;
27733       md_number_to_chars (buf, newval, INSN_SIZE);
27734       break;
27735
27736     case BFD_RELOC_ARM_T32_IMMEDIATE:
27737     case BFD_RELOC_ARM_T32_ADD_IMM:
27738     case BFD_RELOC_ARM_T32_IMM12:
27739     case BFD_RELOC_ARM_T32_ADD_PC12:
27740       /* We claim that this fixup has been processed here,
27741          even if in fact we generate an error because we do
27742          not have a reloc for it, so tc_gen_reloc will reject it.  */
27743       fixP->fx_done = 1;
27744
27745       if (fixP->fx_addsy
27746           && ! S_IS_DEFINED (fixP->fx_addsy))
27747         {
27748           as_bad_where (fixP->fx_file, fixP->fx_line,
27749                         _("undefined symbol %s used as an immediate value"),
27750                         S_GET_NAME (fixP->fx_addsy));
27751           break;
27752         }
27753
27754       newval = md_chars_to_number (buf, THUMB_SIZE);
27755       newval <<= 16;
27756       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
27757
27758       newimm = FAIL;
27759       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
27760            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
27761               Thumb2 modified immediate encoding (T2).  */
27762            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
27763           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
27764         {
27765           newimm = encode_thumb32_immediate (value);
27766           if (newimm == (unsigned int) FAIL)
27767             newimm = thumb32_negate_data_op (&newval, value);
27768         }
27769       if (newimm == (unsigned int) FAIL)
27770         {
27771           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
27772             {
27773               /* Turn add/sum into addw/subw.  */
27774               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
27775                 newval = (newval & 0xfeffffff) | 0x02000000;
27776               /* No flat 12-bit imm encoding for addsw/subsw.  */
27777               if ((newval & 0x00100000) == 0)
27778                 {
27779                   /* 12 bit immediate for addw/subw.  */
27780                   if (value < 0)
27781                     {
27782                       value = -value;
27783                       newval ^= 0x00a00000;
27784                     }
27785                   if (value > 0xfff)
27786                     newimm = (unsigned int) FAIL;
27787                   else
27788                     newimm = value;
27789                 }
27790             }
27791           else
27792             {
27793               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
27794                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
27795                  disassembling, MOV is preferred when there is no encoding
27796                  overlap.  */
27797               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
27798                   /* NOTE: MOV uses the ORR opcode in Thumb 2 mode
27799                      but with the Rn field [19:16] set to 1111.  */
27800                   && (((newval >> 16) & 0xf) == 0xf)
27801                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
27802                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
27803                   && value >= 0 && value <= 0xffff)
27804                 {
27805                   /* Toggle bit[25] to change encoding from T2 to T3.  */
27806                   newval ^= 1 << 25;
27807                   /* Clear bits[19:16].  */
27808                   newval &= 0xfff0ffff;
27809                   /* Encoding high 4bits imm.  Code below will encode the
27810                      remaining low 12bits.  */
27811                   newval |= (value & 0x0000f000) << 4;
27812                   newimm = value & 0x00000fff;
27813                 }
27814             }
27815         }
27816
27817       if (newimm == (unsigned int)FAIL)
27818         {
27819           as_bad_where (fixP->fx_file, fixP->fx_line,
27820                         _("invalid constant (%lx) after fixup"),
27821                         (unsigned long) value);
27822           break;
27823         }
27824
27825       newval |= (newimm & 0x800) << 15;
27826       newval |= (newimm & 0x700) << 4;
27827       newval |= (newimm & 0x0ff);
27828
27829       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
27830       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
27831       break;
27832
27833     case BFD_RELOC_ARM_SMC:
27834       if (((unsigned long) value) > 0xf)
27835         as_bad_where (fixP->fx_file, fixP->fx_line,
27836                       _("invalid smc expression"));
27837
27838       newval = md_chars_to_number (buf, INSN_SIZE);
27839       newval |= (value & 0xf);
27840       md_number_to_chars (buf, newval, INSN_SIZE);
27841       break;
27842
27843     case BFD_RELOC_ARM_HVC:
27844       if (((unsigned long) value) > 0xffff)
27845         as_bad_where (fixP->fx_file, fixP->fx_line,
27846                       _("invalid hvc expression"));
27847       newval = md_chars_to_number (buf, INSN_SIZE);
27848       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
27849       md_number_to_chars (buf, newval, INSN_SIZE);
27850       break;
27851
27852     case BFD_RELOC_ARM_SWI:
27853       if (fixP->tc_fix_data != 0)
27854         {
27855           if (((unsigned long) value) > 0xff)
27856             as_bad_where (fixP->fx_file, fixP->fx_line,
27857                           _("invalid swi expression"));
27858           newval = md_chars_to_number (buf, THUMB_SIZE);
27859           newval |= value;
27860           md_number_to_chars (buf, newval, THUMB_SIZE);
27861         }
27862       else
27863         {
27864           if (((unsigned long) value) > 0x00ffffff)
27865             as_bad_where (fixP->fx_file, fixP->fx_line,
27866                           _("invalid swi expression"));
27867           newval = md_chars_to_number (buf, INSN_SIZE);
27868           newval |= value;
27869           md_number_to_chars (buf, newval, INSN_SIZE);
27870         }
27871       break;
27872
27873     case BFD_RELOC_ARM_MULTI:
27874       if (((unsigned long) value) > 0xffff)
27875         as_bad_where (fixP->fx_file, fixP->fx_line,
27876                       _("invalid expression in load/store multiple"));
27877       newval = value | md_chars_to_number (buf, INSN_SIZE);
27878       md_number_to_chars (buf, newval, INSN_SIZE);
27879       break;
27880
27881 #ifdef OBJ_ELF
27882     case BFD_RELOC_ARM_PCREL_CALL:
27883
27884       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27885           && fixP->fx_addsy
27886           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27887           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27888           && THUMB_IS_FUNC (fixP->fx_addsy))
27889         /* Flip the bl to blx. This is a simple flip
27890            bit here because we generate PCREL_CALL for
27891            unconditional bls.  */
27892         {
27893           newval = md_chars_to_number (buf, INSN_SIZE);
27894           newval = newval | 0x10000000;
27895           md_number_to_chars (buf, newval, INSN_SIZE);
27896           temp = 1;
27897           fixP->fx_done = 1;
27898         }
27899       else
27900         temp = 3;
27901       goto arm_branch_common;
27902
27903     case BFD_RELOC_ARM_PCREL_JUMP:
27904       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27905           && fixP->fx_addsy
27906           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27907           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27908           && THUMB_IS_FUNC (fixP->fx_addsy))
27909         {
27910           /* This would map to a bl<cond>, b<cond>,
27911              b<always> to a Thumb function. We
27912              need to force a relocation for this particular
27913              case.  */
27914           newval = md_chars_to_number (buf, INSN_SIZE);
27915           fixP->fx_done = 0;
27916         }
27917       /* Fall through.  */
27918
27919     case BFD_RELOC_ARM_PLT32:
27920 #endif
27921     case BFD_RELOC_ARM_PCREL_BRANCH:
27922       temp = 3;
27923       goto arm_branch_common;
27924
27925     case BFD_RELOC_ARM_PCREL_BLX:
27926
27927       temp = 1;
27928       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27929           && fixP->fx_addsy
27930           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27931           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27932           && ARM_IS_FUNC (fixP->fx_addsy))
27933         {
27934           /* Flip the blx to a bl and warn.  */
27935           const char *name = S_GET_NAME (fixP->fx_addsy);
27936           newval = 0xeb000000;
27937           as_warn_where (fixP->fx_file, fixP->fx_line,
27938                          _("blx to '%s' an ARM ISA state function changed to bl"),
27939                           name);
27940           md_number_to_chars (buf, newval, INSN_SIZE);
27941           temp = 3;
27942           fixP->fx_done = 1;
27943         }
27944
27945 #ifdef OBJ_ELF
27946        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
27947          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
27948 #endif
27949
27950     arm_branch_common:
27951       /* We are going to store value (shifted right by two) in the
27952          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
27953          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
27954          also be clear.  */
27955       if (value & temp)
27956         as_bad_where (fixP->fx_file, fixP->fx_line,
27957                       _("misaligned branch destination"));
27958       if ((value & (offsetT)0xfe000000) != (offsetT)0
27959           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
27960         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
27961
27962       if (fixP->fx_done || !seg->use_rela_p)
27963         {
27964           newval = md_chars_to_number (buf, INSN_SIZE);
27965           newval |= (value >> 2) & 0x00ffffff;
27966           /* Set the H bit on BLX instructions.  */
27967           if (temp == 1)
27968             {
27969               if (value & 2)
27970                 newval |= 0x01000000;
27971               else
27972                 newval &= ~0x01000000;
27973             }
27974           md_number_to_chars (buf, newval, INSN_SIZE);
27975         }
27976       break;
27977
27978     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
27979       /* CBZ can only branch forward.  */
27980
27981       /* Attempts to use CBZ to branch to the next instruction
27982          (which, strictly speaking, are prohibited) will be turned into
27983          no-ops.
27984
27985          FIXME: It may be better to remove the instruction completely and
27986          perform relaxation.  */
27987       if (value == -2)
27988         {
27989           newval = md_chars_to_number (buf, THUMB_SIZE);
27990           newval = 0xbf00; /* NOP encoding T1 */
27991           md_number_to_chars (buf, newval, THUMB_SIZE);
27992         }
27993       else
27994         {
27995           if (value & ~0x7e)
27996             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
27997
27998           if (fixP->fx_done || !seg->use_rela_p)
27999             {
28000               newval = md_chars_to_number (buf, THUMB_SIZE);
28001               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
28002               md_number_to_chars (buf, newval, THUMB_SIZE);
28003             }
28004         }
28005       break;
28006
28007     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
28008       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
28009         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28010
28011       if (fixP->fx_done || !seg->use_rela_p)
28012         {
28013           newval = md_chars_to_number (buf, THUMB_SIZE);
28014           newval |= (value & 0x1ff) >> 1;
28015           md_number_to_chars (buf, newval, THUMB_SIZE);
28016         }
28017       break;
28018
28019     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
28020       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
28021         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28022
28023       if (fixP->fx_done || !seg->use_rela_p)
28024         {
28025           newval = md_chars_to_number (buf, THUMB_SIZE);
28026           newval |= (value & 0xfff) >> 1;
28027           md_number_to_chars (buf, newval, THUMB_SIZE);
28028         }
28029       break;
28030
28031     case BFD_RELOC_THUMB_PCREL_BRANCH20:
28032       if (fixP->fx_addsy
28033           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28034           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28035           && ARM_IS_FUNC (fixP->fx_addsy)
28036           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
28037         {
28038           /* Force a relocation for a branch 20 bits wide.  */
28039           fixP->fx_done = 0;
28040         }
28041       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
28042         as_bad_where (fixP->fx_file, fixP->fx_line,
28043                       _("conditional branch out of range"));
28044
28045       if (fixP->fx_done || !seg->use_rela_p)
28046         {
28047           offsetT newval2;
28048           addressT S, J1, J2, lo, hi;
28049
28050           S  = (value & 0x00100000) >> 20;
28051           J2 = (value & 0x00080000) >> 19;
28052           J1 = (value & 0x00040000) >> 18;
28053           hi = (value & 0x0003f000) >> 12;
28054           lo = (value & 0x00000ffe) >> 1;
28055
28056           newval   = md_chars_to_number (buf, THUMB_SIZE);
28057           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28058           newval  |= (S << 10) | hi;
28059           newval2 |= (J1 << 13) | (J2 << 11) | lo;
28060           md_number_to_chars (buf, newval, THUMB_SIZE);
28061           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28062         }
28063       break;
28064
28065     case BFD_RELOC_THUMB_PCREL_BLX:
28066       /* If there is a blx from a thumb state function to
28067          another thumb function flip this to a bl and warn
28068          about it.  */
28069
28070       if (fixP->fx_addsy
28071           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28072           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28073           && THUMB_IS_FUNC (fixP->fx_addsy))
28074         {
28075           const char *name = S_GET_NAME (fixP->fx_addsy);
28076           as_warn_where (fixP->fx_file, fixP->fx_line,
28077                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
28078                          name);
28079           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28080           newval = newval | 0x1000;
28081           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
28082           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
28083           fixP->fx_done = 1;
28084         }
28085
28086
28087       goto thumb_bl_common;
28088
28089     case BFD_RELOC_THUMB_PCREL_BRANCH23:
28090       /* A bl from Thumb state ISA to an internal ARM state function
28091          is converted to a blx.  */
28092       if (fixP->fx_addsy
28093           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28094           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28095           && ARM_IS_FUNC (fixP->fx_addsy)
28096           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
28097         {
28098           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28099           newval = newval & ~0x1000;
28100           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
28101           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
28102           fixP->fx_done = 1;
28103         }
28104
28105     thumb_bl_common:
28106
28107       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
28108         /* For a BLX instruction, make sure that the relocation is rounded up
28109            to a word boundary.  This follows the semantics of the instruction
28110            which specifies that bit 1 of the target address will come from bit
28111            1 of the base address.  */
28112         value = (value + 3) & ~ 3;
28113
28114 #ifdef OBJ_ELF
28115        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
28116            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
28117          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
28118 #endif
28119
28120       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
28121         {
28122           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
28123             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28124           else if ((value & ~0x1ffffff)
28125                    && ((value & ~0x1ffffff) != ~0x1ffffff))
28126             as_bad_where (fixP->fx_file, fixP->fx_line,
28127                           _("Thumb2 branch out of range"));
28128         }
28129
28130       if (fixP->fx_done || !seg->use_rela_p)
28131         encode_thumb2_b_bl_offset (buf, value);
28132
28133       break;
28134
28135     case BFD_RELOC_THUMB_PCREL_BRANCH25:
28136       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
28137         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28138
28139       if (fixP->fx_done || !seg->use_rela_p)
28140           encode_thumb2_b_bl_offset (buf, value);
28141
28142       break;
28143
28144     case BFD_RELOC_8:
28145       if (fixP->fx_done || !seg->use_rela_p)
28146         *buf = value;
28147       break;
28148
28149     case BFD_RELOC_16:
28150       if (fixP->fx_done || !seg->use_rela_p)
28151         md_number_to_chars (buf, value, 2);
28152       break;
28153
28154 #ifdef OBJ_ELF
28155     case BFD_RELOC_ARM_TLS_CALL:
28156     case BFD_RELOC_ARM_THM_TLS_CALL:
28157     case BFD_RELOC_ARM_TLS_DESCSEQ:
28158     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
28159     case BFD_RELOC_ARM_TLS_GOTDESC:
28160     case BFD_RELOC_ARM_TLS_GD32:
28161     case BFD_RELOC_ARM_TLS_LE32:
28162     case BFD_RELOC_ARM_TLS_IE32:
28163     case BFD_RELOC_ARM_TLS_LDM32:
28164     case BFD_RELOC_ARM_TLS_LDO32:
28165       S_SET_THREAD_LOCAL (fixP->fx_addsy);
28166       break;
28167
28168       /* Same handling as above, but with the arm_fdpic guard.  */
28169     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
28170     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
28171     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
28172       if (arm_fdpic)
28173         {
28174           S_SET_THREAD_LOCAL (fixP->fx_addsy);
28175         }
28176       else
28177         {
28178           as_bad_where (fixP->fx_file, fixP->fx_line,
28179                         _("Relocation supported only in FDPIC mode"));
28180         }
28181       break;
28182
28183     case BFD_RELOC_ARM_GOT32:
28184     case BFD_RELOC_ARM_GOTOFF:
28185       break;
28186
28187     case BFD_RELOC_ARM_GOT_PREL:
28188       if (fixP->fx_done || !seg->use_rela_p)
28189         md_number_to_chars (buf, value, 4);
28190       break;
28191
28192     case BFD_RELOC_ARM_TARGET2:
28193       /* TARGET2 is not partial-inplace, so we need to write the
28194          addend here for REL targets, because it won't be written out
28195          during reloc processing later.  */
28196       if (fixP->fx_done || !seg->use_rela_p)
28197         md_number_to_chars (buf, fixP->fx_offset, 4);
28198       break;
28199
28200       /* Relocations for FDPIC.  */
28201     case BFD_RELOC_ARM_GOTFUNCDESC:
28202     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
28203     case BFD_RELOC_ARM_FUNCDESC:
28204       if (arm_fdpic)
28205         {
28206           if (fixP->fx_done || !seg->use_rela_p)
28207             md_number_to_chars (buf, 0, 4);
28208         }
28209       else
28210         {
28211           as_bad_where (fixP->fx_file, fixP->fx_line,
28212                         _("Relocation supported only in FDPIC mode"));
28213       }
28214       break;
28215 #endif
28216
28217     case BFD_RELOC_RVA:
28218     case BFD_RELOC_32:
28219     case BFD_RELOC_ARM_TARGET1:
28220     case BFD_RELOC_ARM_ROSEGREL32:
28221     case BFD_RELOC_ARM_SBREL32:
28222     case BFD_RELOC_32_PCREL:
28223 #ifdef TE_PE
28224     case BFD_RELOC_32_SECREL:
28225 #endif
28226       if (fixP->fx_done || !seg->use_rela_p)
28227 #ifdef TE_WINCE
28228         /* For WinCE we only do this for pcrel fixups.  */
28229         if (fixP->fx_done || fixP->fx_pcrel)
28230 #endif
28231           md_number_to_chars (buf, value, 4);
28232       break;
28233
28234 #ifdef OBJ_ELF
28235     case BFD_RELOC_ARM_PREL31:
28236       if (fixP->fx_done || !seg->use_rela_p)
28237         {
28238           newval = md_chars_to_number (buf, 4) & 0x80000000;
28239           if ((value ^ (value >> 1)) & 0x40000000)
28240             {
28241               as_bad_where (fixP->fx_file, fixP->fx_line,
28242                             _("rel31 relocation overflow"));
28243             }
28244           newval |= value & 0x7fffffff;
28245           md_number_to_chars (buf, newval, 4);
28246         }
28247       break;
28248 #endif
28249
28250     case BFD_RELOC_ARM_CP_OFF_IMM:
28251     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
28252     case BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM:
28253       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
28254         newval = md_chars_to_number (buf, INSN_SIZE);
28255       else
28256         newval = get_thumb32_insn (buf);
28257       if ((newval & 0x0f200f00) == 0x0d000900)
28258         {
28259           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
28260              has permitted values that are multiples of 2, in the range 0
28261              to 510.  */
28262           if (value < -510 || value > 510 || (value & 1))
28263             as_bad_where (fixP->fx_file, fixP->fx_line,
28264                           _("co-processor offset out of range"));
28265         }
28266       else if ((newval & 0xfe001f80) == 0xec000f80)
28267         {
28268           if (value < -511 || value > 512 || (value & 3))
28269             as_bad_where (fixP->fx_file, fixP->fx_line,
28270                           _("co-processor offset out of range"));
28271         }
28272       else if (value < -1023 || value > 1023 || (value & 3))
28273         as_bad_where (fixP->fx_file, fixP->fx_line,
28274                       _("co-processor offset out of range"));
28275     cp_off_common:
28276       sign = value > 0;
28277       if (value < 0)
28278         value = -value;
28279       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28280           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
28281         newval = md_chars_to_number (buf, INSN_SIZE);
28282       else
28283         newval = get_thumb32_insn (buf);
28284       if (value == 0)
28285         {
28286           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
28287             newval &= 0xffffff80;
28288           else
28289             newval &= 0xffffff00;
28290         }
28291       else
28292         {
28293           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
28294             newval &= 0xff7fff80;
28295           else
28296             newval &= 0xff7fff00;
28297           if ((newval & 0x0f200f00) == 0x0d000900)
28298             {
28299               /* This is a fp16 vstr/vldr.
28300
28301                  It requires the immediate offset in the instruction is shifted
28302                  left by 1 to be a half-word offset.
28303
28304                  Here, left shift by 1 first, and later right shift by 2
28305                  should get the right offset.  */
28306               value <<= 1;
28307             }
28308           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
28309         }
28310       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28311           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
28312         md_number_to_chars (buf, newval, INSN_SIZE);
28313       else
28314         put_thumb32_insn (buf, newval);
28315       break;
28316
28317     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
28318     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
28319       if (value < -255 || value > 255)
28320         as_bad_where (fixP->fx_file, fixP->fx_line,
28321                       _("co-processor offset out of range"));
28322       value *= 4;
28323       goto cp_off_common;
28324
28325     case BFD_RELOC_ARM_THUMB_OFFSET:
28326       newval = md_chars_to_number (buf, THUMB_SIZE);
28327       /* Exactly what ranges, and where the offset is inserted depends
28328          on the type of instruction, we can establish this from the
28329          top 4 bits.  */
28330       switch (newval >> 12)
28331         {
28332         case 4: /* PC load.  */
28333           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
28334              forced to zero for these loads; md_pcrel_from has already
28335              compensated for this.  */
28336           if (value & 3)
28337             as_bad_where (fixP->fx_file, fixP->fx_line,
28338                           _("invalid offset, target not word aligned (0x%08lX)"),
28339                           (((unsigned long) fixP->fx_frag->fr_address
28340                             + (unsigned long) fixP->fx_where) & ~3)
28341                           + (unsigned long) value);
28342
28343           if (value & ~0x3fc)
28344             as_bad_where (fixP->fx_file, fixP->fx_line,
28345                           _("invalid offset, value too big (0x%08lX)"),
28346                           (long) value);
28347
28348           newval |= value >> 2;
28349           break;
28350
28351         case 9: /* SP load/store.  */
28352           if (value & ~0x3fc)
28353             as_bad_where (fixP->fx_file, fixP->fx_line,
28354                           _("invalid offset, value too big (0x%08lX)"),
28355                           (long) value);
28356           newval |= value >> 2;
28357           break;
28358
28359         case 6: /* Word load/store.  */
28360           if (value & ~0x7c)
28361             as_bad_where (fixP->fx_file, fixP->fx_line,
28362                           _("invalid offset, value too big (0x%08lX)"),
28363                           (long) value);
28364           newval |= value << 4; /* 6 - 2.  */
28365           break;
28366
28367         case 7: /* Byte load/store.  */
28368           if (value & ~0x1f)
28369             as_bad_where (fixP->fx_file, fixP->fx_line,
28370                           _("invalid offset, value too big (0x%08lX)"),
28371                           (long) value);
28372           newval |= value << 6;
28373           break;
28374
28375         case 8: /* Halfword load/store.  */
28376           if (value & ~0x3e)
28377             as_bad_where (fixP->fx_file, fixP->fx_line,
28378                           _("invalid offset, value too big (0x%08lX)"),
28379                           (long) value);
28380           newval |= value << 5; /* 6 - 1.  */
28381           break;
28382
28383         default:
28384           as_bad_where (fixP->fx_file, fixP->fx_line,
28385                         "Unable to process relocation for thumb opcode: %lx",
28386                         (unsigned long) newval);
28387           break;
28388         }
28389       md_number_to_chars (buf, newval, THUMB_SIZE);
28390       break;
28391
28392     case BFD_RELOC_ARM_THUMB_ADD:
28393       /* This is a complicated relocation, since we use it for all of
28394          the following immediate relocations:
28395
28396             3bit ADD/SUB
28397             8bit ADD/SUB
28398             9bit ADD/SUB SP word-aligned
28399            10bit ADD PC/SP word-aligned
28400
28401          The type of instruction being processed is encoded in the
28402          instruction field:
28403
28404            0x8000  SUB
28405            0x00F0  Rd
28406            0x000F  Rs
28407       */
28408       newval = md_chars_to_number (buf, THUMB_SIZE);
28409       {
28410         int rd = (newval >> 4) & 0xf;
28411         int rs = newval & 0xf;
28412         int subtract = !!(newval & 0x8000);
28413
28414         /* Check for HI regs, only very restricted cases allowed:
28415            Adjusting SP, and using PC or SP to get an address.  */
28416         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
28417             || (rs > 7 && rs != REG_SP && rs != REG_PC))
28418           as_bad_where (fixP->fx_file, fixP->fx_line,
28419                         _("invalid Hi register with immediate"));
28420
28421         /* If value is negative, choose the opposite instruction.  */
28422         if (value < 0)
28423           {
28424             value = -value;
28425             subtract = !subtract;
28426             if (value < 0)
28427               as_bad_where (fixP->fx_file, fixP->fx_line,
28428                             _("immediate value out of range"));
28429           }
28430
28431         if (rd == REG_SP)
28432           {
28433             if (value & ~0x1fc)
28434               as_bad_where (fixP->fx_file, fixP->fx_line,
28435                             _("invalid immediate for stack address calculation"));
28436             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
28437             newval |= value >> 2;
28438           }
28439         else if (rs == REG_PC || rs == REG_SP)
28440           {
28441             /* PR gas/18541.  If the addition is for a defined symbol
28442                within range of an ADR instruction then accept it.  */
28443             if (subtract
28444                 && value == 4
28445                 && fixP->fx_addsy != NULL)
28446               {
28447                 subtract = 0;
28448
28449                 if (! S_IS_DEFINED (fixP->fx_addsy)
28450                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
28451                     || S_IS_WEAK (fixP->fx_addsy))
28452                   {
28453                     as_bad_where (fixP->fx_file, fixP->fx_line,
28454                                   _("address calculation needs a strongly defined nearby symbol"));
28455                   }
28456                 else
28457                   {
28458                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
28459
28460                     /* Round up to the next 4-byte boundary.  */
28461                     if (v & 3)
28462                       v = (v + 3) & ~ 3;
28463                     else
28464                       v += 4;
28465                     v = S_GET_VALUE (fixP->fx_addsy) - v;
28466
28467                     if (v & ~0x3fc)
28468                       {
28469                         as_bad_where (fixP->fx_file, fixP->fx_line,
28470                                       _("symbol too far away"));
28471                       }
28472                     else
28473                       {
28474                         fixP->fx_done = 1;
28475                         value = v;
28476                       }
28477                   }
28478               }
28479
28480             if (subtract || value & ~0x3fc)
28481               as_bad_where (fixP->fx_file, fixP->fx_line,
28482                             _("invalid immediate for address calculation (value = 0x%08lX)"),
28483                             (unsigned long) (subtract ? - value : value));
28484             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
28485             newval |= rd << 8;
28486             newval |= value >> 2;
28487           }
28488         else if (rs == rd)
28489           {
28490             if (value & ~0xff)
28491               as_bad_where (fixP->fx_file, fixP->fx_line,
28492                             _("immediate value out of range"));
28493             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
28494             newval |= (rd << 8) | value;
28495           }
28496         else
28497           {
28498             if (value & ~0x7)
28499               as_bad_where (fixP->fx_file, fixP->fx_line,
28500                             _("immediate value out of range"));
28501             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
28502             newval |= rd | (rs << 3) | (value << 6);
28503           }
28504       }
28505       md_number_to_chars (buf, newval, THUMB_SIZE);
28506       break;
28507
28508     case BFD_RELOC_ARM_THUMB_IMM:
28509       newval = md_chars_to_number (buf, THUMB_SIZE);
28510       if (value < 0 || value > 255)
28511         as_bad_where (fixP->fx_file, fixP->fx_line,
28512                       _("invalid immediate: %ld is out of range"),
28513                       (long) value);
28514       newval |= value;
28515       md_number_to_chars (buf, newval, THUMB_SIZE);
28516       break;
28517
28518     case BFD_RELOC_ARM_THUMB_SHIFT:
28519       /* 5bit shift value (0..32).  LSL cannot take 32.  */
28520       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
28521       temp = newval & 0xf800;
28522       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
28523         as_bad_where (fixP->fx_file, fixP->fx_line,
28524                       _("invalid shift value: %ld"), (long) value);
28525       /* Shifts of zero must be encoded as LSL.  */
28526       if (value == 0)
28527         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
28528       /* Shifts of 32 are encoded as zero.  */
28529       else if (value == 32)
28530         value = 0;
28531       newval |= value << 6;
28532       md_number_to_chars (buf, newval, THUMB_SIZE);
28533       break;
28534
28535     case BFD_RELOC_VTABLE_INHERIT:
28536     case BFD_RELOC_VTABLE_ENTRY:
28537       fixP->fx_done = 0;
28538       return;
28539
28540     case BFD_RELOC_ARM_MOVW:
28541     case BFD_RELOC_ARM_MOVT:
28542     case BFD_RELOC_ARM_THUMB_MOVW:
28543     case BFD_RELOC_ARM_THUMB_MOVT:
28544       if (fixP->fx_done || !seg->use_rela_p)
28545         {
28546           /* REL format relocations are limited to a 16-bit addend.  */
28547           if (!fixP->fx_done)
28548             {
28549               if (value < -0x8000 || value > 0x7fff)
28550                   as_bad_where (fixP->fx_file, fixP->fx_line,
28551                                 _("offset out of range"));
28552             }
28553           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
28554                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
28555             {
28556               value >>= 16;
28557             }
28558
28559           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
28560               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
28561             {
28562               newval = get_thumb32_insn (buf);
28563               newval &= 0xfbf08f00;
28564               newval |= (value & 0xf000) << 4;
28565               newval |= (value & 0x0800) << 15;
28566               newval |= (value & 0x0700) << 4;
28567               newval |= (value & 0x00ff);
28568               put_thumb32_insn (buf, newval);
28569             }
28570           else
28571             {
28572               newval = md_chars_to_number (buf, 4);
28573               newval &= 0xfff0f000;
28574               newval |= value & 0x0fff;
28575               newval |= (value & 0xf000) << 4;
28576               md_number_to_chars (buf, newval, 4);
28577             }
28578         }
28579       return;
28580
28581    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
28582    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
28583    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
28584    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
28585       gas_assert (!fixP->fx_done);
28586       {
28587         bfd_vma insn;
28588         bfd_boolean is_mov;
28589         bfd_vma encoded_addend = value;
28590
28591         /* Check that addend can be encoded in instruction.  */
28592         if (!seg->use_rela_p && (value < 0 || value > 255))
28593           as_bad_where (fixP->fx_file, fixP->fx_line,
28594                         _("the offset 0x%08lX is not representable"),
28595                         (unsigned long) encoded_addend);
28596
28597         /* Extract the instruction.  */
28598         insn = md_chars_to_number (buf, THUMB_SIZE);
28599         is_mov = (insn & 0xf800) == 0x2000;
28600
28601         /* Encode insn.  */
28602         if (is_mov)
28603           {
28604             if (!seg->use_rela_p)
28605               insn |= encoded_addend;
28606           }
28607         else
28608           {
28609             int rd, rs;
28610
28611             /* Extract the instruction.  */
28612              /* Encoding is the following
28613                 0x8000  SUB
28614                 0x00F0  Rd
28615                 0x000F  Rs
28616              */
28617              /* The following conditions must be true :
28618                 - ADD
28619                 - Rd == Rs
28620                 - Rd <= 7
28621              */
28622             rd = (insn >> 4) & 0xf;
28623             rs = insn & 0xf;
28624             if ((insn & 0x8000) || (rd != rs) || rd > 7)
28625               as_bad_where (fixP->fx_file, fixP->fx_line,
28626                         _("Unable to process relocation for thumb opcode: %lx"),
28627                         (unsigned long) insn);
28628
28629             /* Encode as ADD immediate8 thumb 1 code.  */
28630             insn = 0x3000 | (rd << 8);
28631
28632             /* Place the encoded addend into the first 8 bits of the
28633                instruction.  */
28634             if (!seg->use_rela_p)
28635               insn |= encoded_addend;
28636           }
28637
28638         /* Update the instruction.  */
28639         md_number_to_chars (buf, insn, THUMB_SIZE);
28640       }
28641       break;
28642
28643    case BFD_RELOC_ARM_ALU_PC_G0_NC:
28644    case BFD_RELOC_ARM_ALU_PC_G0:
28645    case BFD_RELOC_ARM_ALU_PC_G1_NC:
28646    case BFD_RELOC_ARM_ALU_PC_G1:
28647    case BFD_RELOC_ARM_ALU_PC_G2:
28648    case BFD_RELOC_ARM_ALU_SB_G0_NC:
28649    case BFD_RELOC_ARM_ALU_SB_G0:
28650    case BFD_RELOC_ARM_ALU_SB_G1_NC:
28651    case BFD_RELOC_ARM_ALU_SB_G1:
28652    case BFD_RELOC_ARM_ALU_SB_G2:
28653      gas_assert (!fixP->fx_done);
28654      if (!seg->use_rela_p)
28655        {
28656          bfd_vma insn;
28657          bfd_vma encoded_addend;
28658          bfd_vma addend_abs = llabs (value);
28659
28660          /* Check that the absolute value of the addend can be
28661             expressed as an 8-bit constant plus a rotation.  */
28662          encoded_addend = encode_arm_immediate (addend_abs);
28663          if (encoded_addend == (unsigned int) FAIL)
28664            as_bad_where (fixP->fx_file, fixP->fx_line,
28665                          _("the offset 0x%08lX is not representable"),
28666                          (unsigned long) addend_abs);
28667
28668          /* Extract the instruction.  */
28669          insn = md_chars_to_number (buf, INSN_SIZE);
28670
28671          /* If the addend is positive, use an ADD instruction.
28672             Otherwise use a SUB.  Take care not to destroy the S bit.  */
28673          insn &= 0xff1fffff;
28674          if (value < 0)
28675            insn |= 1 << 22;
28676          else
28677            insn |= 1 << 23;
28678
28679          /* Place the encoded addend into the first 12 bits of the
28680             instruction.  */
28681          insn &= 0xfffff000;
28682          insn |= encoded_addend;
28683
28684          /* Update the instruction.  */
28685          md_number_to_chars (buf, insn, INSN_SIZE);
28686        }
28687      break;
28688
28689     case BFD_RELOC_ARM_LDR_PC_G0:
28690     case BFD_RELOC_ARM_LDR_PC_G1:
28691     case BFD_RELOC_ARM_LDR_PC_G2:
28692     case BFD_RELOC_ARM_LDR_SB_G0:
28693     case BFD_RELOC_ARM_LDR_SB_G1:
28694     case BFD_RELOC_ARM_LDR_SB_G2:
28695       gas_assert (!fixP->fx_done);
28696       if (!seg->use_rela_p)
28697         {
28698           bfd_vma insn;
28699           bfd_vma addend_abs = llabs (value);
28700
28701           /* Check that the absolute value of the addend can be
28702              encoded in 12 bits.  */
28703           if (addend_abs >= 0x1000)
28704             as_bad_where (fixP->fx_file, fixP->fx_line,
28705                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
28706                           (unsigned long) addend_abs);
28707
28708           /* Extract the instruction.  */
28709           insn = md_chars_to_number (buf, INSN_SIZE);
28710
28711           /* If the addend is negative, clear bit 23 of the instruction.
28712              Otherwise set it.  */
28713           if (value < 0)
28714             insn &= ~(1 << 23);
28715           else
28716             insn |= 1 << 23;
28717
28718           /* Place the absolute value of the addend into the first 12 bits
28719              of the instruction.  */
28720           insn &= 0xfffff000;
28721           insn |= addend_abs;
28722
28723           /* Update the instruction.  */
28724           md_number_to_chars (buf, insn, INSN_SIZE);
28725         }
28726       break;
28727
28728     case BFD_RELOC_ARM_LDRS_PC_G0:
28729     case BFD_RELOC_ARM_LDRS_PC_G1:
28730     case BFD_RELOC_ARM_LDRS_PC_G2:
28731     case BFD_RELOC_ARM_LDRS_SB_G0:
28732     case BFD_RELOC_ARM_LDRS_SB_G1:
28733     case BFD_RELOC_ARM_LDRS_SB_G2:
28734       gas_assert (!fixP->fx_done);
28735       if (!seg->use_rela_p)
28736         {
28737           bfd_vma insn;
28738           bfd_vma addend_abs = llabs (value);
28739
28740           /* Check that the absolute value of the addend can be
28741              encoded in 8 bits.  */
28742           if (addend_abs >= 0x100)
28743             as_bad_where (fixP->fx_file, fixP->fx_line,
28744                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
28745                           (unsigned long) addend_abs);
28746
28747           /* Extract the instruction.  */
28748           insn = md_chars_to_number (buf, INSN_SIZE);
28749
28750           /* If the addend is negative, clear bit 23 of the instruction.
28751              Otherwise set it.  */
28752           if (value < 0)
28753             insn &= ~(1 << 23);
28754           else
28755             insn |= 1 << 23;
28756
28757           /* Place the first four bits of the absolute value of the addend
28758              into the first 4 bits of the instruction, and the remaining
28759              four into bits 8 .. 11.  */
28760           insn &= 0xfffff0f0;
28761           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
28762
28763           /* Update the instruction.  */
28764           md_number_to_chars (buf, insn, INSN_SIZE);
28765         }
28766       break;
28767
28768     case BFD_RELOC_ARM_LDC_PC_G0:
28769     case BFD_RELOC_ARM_LDC_PC_G1:
28770     case BFD_RELOC_ARM_LDC_PC_G2:
28771     case BFD_RELOC_ARM_LDC_SB_G0:
28772     case BFD_RELOC_ARM_LDC_SB_G1:
28773     case BFD_RELOC_ARM_LDC_SB_G2:
28774       gas_assert (!fixP->fx_done);
28775       if (!seg->use_rela_p)
28776         {
28777           bfd_vma insn;
28778           bfd_vma addend_abs = llabs (value);
28779
28780           /* Check that the absolute value of the addend is a multiple of
28781              four and, when divided by four, fits in 8 bits.  */
28782           if (addend_abs & 0x3)
28783             as_bad_where (fixP->fx_file, fixP->fx_line,
28784                           _("bad offset 0x%08lX (must be word-aligned)"),
28785                           (unsigned long) addend_abs);
28786
28787           if ((addend_abs >> 2) > 0xff)
28788             as_bad_where (fixP->fx_file, fixP->fx_line,
28789                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
28790                           (unsigned long) addend_abs);
28791
28792           /* Extract the instruction.  */
28793           insn = md_chars_to_number (buf, INSN_SIZE);
28794
28795           /* If the addend is negative, clear bit 23 of the instruction.
28796              Otherwise set it.  */
28797           if (value < 0)
28798             insn &= ~(1 << 23);
28799           else
28800             insn |= 1 << 23;
28801
28802           /* Place the addend (divided by four) into the first eight
28803              bits of the instruction.  */
28804           insn &= 0xfffffff0;
28805           insn |= addend_abs >> 2;
28806
28807           /* Update the instruction.  */
28808           md_number_to_chars (buf, insn, INSN_SIZE);
28809         }
28810       break;
28811
28812     case BFD_RELOC_THUMB_PCREL_BRANCH5:
28813       if (fixP->fx_addsy
28814           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28815           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28816           && ARM_IS_FUNC (fixP->fx_addsy)
28817           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28818         {
28819           /* Force a relocation for a branch 5 bits wide.  */
28820           fixP->fx_done = 0;
28821         }
28822       if (v8_1_branch_value_check (value, 5, FALSE) == FAIL)
28823         as_bad_where (fixP->fx_file, fixP->fx_line,
28824                       BAD_BRANCH_OFF);
28825
28826       if (fixP->fx_done || !seg->use_rela_p)
28827         {
28828           addressT boff = value >> 1;
28829
28830           newval  = md_chars_to_number (buf, THUMB_SIZE);
28831           newval |= (boff << 7);
28832           md_number_to_chars (buf, newval, THUMB_SIZE);
28833         }
28834       break;
28835
28836     case BFD_RELOC_THUMB_PCREL_BFCSEL:
28837       if (fixP->fx_addsy
28838           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28839           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28840           && ARM_IS_FUNC (fixP->fx_addsy)
28841           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28842         {
28843           fixP->fx_done = 0;
28844         }
28845       if ((value & ~0x7f) && ((value & ~0x3f) != ~0x3f))
28846         as_bad_where (fixP->fx_file, fixP->fx_line,
28847                       _("branch out of range"));
28848
28849       if (fixP->fx_done || !seg->use_rela_p)
28850         {
28851           newval  = md_chars_to_number (buf, THUMB_SIZE);
28852
28853           addressT boff = ((newval & 0x0780) >> 7) << 1;
28854           addressT diff = value - boff;
28855
28856           if (diff == 4)
28857             {
28858               newval |= 1 << 1; /* T bit.  */
28859             }
28860           else if (diff != 2)
28861             {
28862               as_bad_where (fixP->fx_file, fixP->fx_line,
28863                             _("out of range label-relative fixup value"));
28864             }
28865           md_number_to_chars (buf, newval, THUMB_SIZE);
28866         }
28867       break;
28868
28869     case BFD_RELOC_ARM_THUMB_BF17:
28870       if (fixP->fx_addsy
28871           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28872           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28873           && ARM_IS_FUNC (fixP->fx_addsy)
28874           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28875         {
28876           /* Force a relocation for a branch 17 bits wide.  */
28877           fixP->fx_done = 0;
28878         }
28879
28880       if (v8_1_branch_value_check (value, 17, TRUE) == FAIL)
28881         as_bad_where (fixP->fx_file, fixP->fx_line,
28882                       BAD_BRANCH_OFF);
28883
28884       if (fixP->fx_done || !seg->use_rela_p)
28885         {
28886           offsetT newval2;
28887           addressT immA, immB, immC;
28888
28889           immA = (value & 0x0001f000) >> 12;
28890           immB = (value & 0x00000ffc) >> 2;
28891           immC = (value & 0x00000002) >> 1;
28892
28893           newval   = md_chars_to_number (buf, THUMB_SIZE);
28894           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28895           newval  |= immA;
28896           newval2 |= (immC << 11) | (immB << 1);
28897           md_number_to_chars (buf, newval, THUMB_SIZE);
28898           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28899         }
28900       break;
28901
28902     case BFD_RELOC_ARM_THUMB_BF19:
28903       if (fixP->fx_addsy
28904           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28905           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28906           && ARM_IS_FUNC (fixP->fx_addsy)
28907           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28908         {
28909           /* Force a relocation for a branch 19 bits wide.  */
28910           fixP->fx_done = 0;
28911         }
28912
28913       if (v8_1_branch_value_check (value, 19, TRUE) == FAIL)
28914         as_bad_where (fixP->fx_file, fixP->fx_line,
28915                       BAD_BRANCH_OFF);
28916
28917       if (fixP->fx_done || !seg->use_rela_p)
28918         {
28919           offsetT newval2;
28920           addressT immA, immB, immC;
28921
28922           immA = (value & 0x0007f000) >> 12;
28923           immB = (value & 0x00000ffc) >> 2;
28924           immC = (value & 0x00000002) >> 1;
28925
28926           newval   = md_chars_to_number (buf, THUMB_SIZE);
28927           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28928           newval  |= immA;
28929           newval2 |= (immC << 11) | (immB << 1);
28930           md_number_to_chars (buf, newval, THUMB_SIZE);
28931           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28932         }
28933       break;
28934
28935     case BFD_RELOC_ARM_THUMB_BF13:
28936       if (fixP->fx_addsy
28937           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28938           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28939           && ARM_IS_FUNC (fixP->fx_addsy)
28940           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28941         {
28942           /* Force a relocation for a branch 13 bits wide.  */
28943           fixP->fx_done = 0;
28944         }
28945
28946       if (v8_1_branch_value_check (value, 13, TRUE) == FAIL)
28947         as_bad_where (fixP->fx_file, fixP->fx_line,
28948                       BAD_BRANCH_OFF);
28949
28950       if (fixP->fx_done || !seg->use_rela_p)
28951         {
28952           offsetT newval2;
28953           addressT immA, immB, immC;
28954
28955           immA = (value & 0x00001000) >> 12;
28956           immB = (value & 0x00000ffc) >> 2;
28957           immC = (value & 0x00000002) >> 1;
28958
28959           newval   = md_chars_to_number (buf, THUMB_SIZE);
28960           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28961           newval  |= immA;
28962           newval2 |= (immC << 11) | (immB << 1);
28963           md_number_to_chars (buf, newval, THUMB_SIZE);
28964           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28965         }
28966       break;
28967
28968     case BFD_RELOC_ARM_THUMB_LOOP12:
28969       if (fixP->fx_addsy
28970           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28971           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28972           && ARM_IS_FUNC (fixP->fx_addsy)
28973           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28974         {
28975           /* Force a relocation for a branch 12 bits wide.  */
28976           fixP->fx_done = 0;
28977         }
28978
28979       bfd_vma insn = get_thumb32_insn (buf);
28980       /* le lr, <label>, le <label> or letp lr, <label> */
28981       if (((insn & 0xffffffff) == 0xf00fc001)
28982           || ((insn & 0xffffffff) == 0xf02fc001)
28983           || ((insn & 0xffffffff) == 0xf01fc001))
28984         value = -value;
28985
28986       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
28987         as_bad_where (fixP->fx_file, fixP->fx_line,
28988                       BAD_BRANCH_OFF);
28989       if (fixP->fx_done || !seg->use_rela_p)
28990         {
28991           addressT imml, immh;
28992
28993           immh = (value & 0x00000ffc) >> 2;
28994           imml = (value & 0x00000002) >> 1;
28995
28996           newval  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28997           newval |= (imml << 11) | (immh << 1);
28998           md_number_to_chars (buf + THUMB_SIZE, newval, THUMB_SIZE);
28999         }
29000       break;
29001
29002     case BFD_RELOC_ARM_V4BX:
29003       /* This will need to go in the object file.  */
29004       fixP->fx_done = 0;
29005       break;
29006
29007     case BFD_RELOC_UNUSED:
29008     default:
29009       as_bad_where (fixP->fx_file, fixP->fx_line,
29010                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
29011     }
29012 }
29013
29014 /* Translate internal representation of relocation info to BFD target
29015    format.  */
29016
29017 arelent *
29018 tc_gen_reloc (asection *section, fixS *fixp)
29019 {
29020   arelent * reloc;
29021   bfd_reloc_code_real_type code;
29022
29023   reloc = XNEW (arelent);
29024
29025   reloc->sym_ptr_ptr = XNEW (asymbol *);
29026   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
29027   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
29028
29029   if (fixp->fx_pcrel)
29030     {
29031       if (section->use_rela_p)
29032         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
29033       else
29034         fixp->fx_offset = reloc->address;
29035     }
29036   reloc->addend = fixp->fx_offset;
29037
29038   switch (fixp->fx_r_type)
29039     {
29040     case BFD_RELOC_8:
29041       if (fixp->fx_pcrel)
29042         {
29043           code = BFD_RELOC_8_PCREL;
29044           break;
29045         }
29046       /* Fall through.  */
29047
29048     case BFD_RELOC_16:
29049       if (fixp->fx_pcrel)
29050         {
29051           code = BFD_RELOC_16_PCREL;
29052           break;
29053         }
29054       /* Fall through.  */
29055
29056     case BFD_RELOC_32:
29057       if (fixp->fx_pcrel)
29058         {
29059           code = BFD_RELOC_32_PCREL;
29060           break;
29061         }
29062       /* Fall through.  */
29063
29064     case BFD_RELOC_ARM_MOVW:
29065       if (fixp->fx_pcrel)
29066         {
29067           code = BFD_RELOC_ARM_MOVW_PCREL;
29068           break;
29069         }
29070       /* Fall through.  */
29071
29072     case BFD_RELOC_ARM_MOVT:
29073       if (fixp->fx_pcrel)
29074         {
29075           code = BFD_RELOC_ARM_MOVT_PCREL;
29076           break;
29077         }
29078       /* Fall through.  */
29079
29080     case BFD_RELOC_ARM_THUMB_MOVW:
29081       if (fixp->fx_pcrel)
29082         {
29083           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
29084           break;
29085         }
29086       /* Fall through.  */
29087
29088     case BFD_RELOC_ARM_THUMB_MOVT:
29089       if (fixp->fx_pcrel)
29090         {
29091           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
29092           break;
29093         }
29094       /* Fall through.  */
29095
29096     case BFD_RELOC_NONE:
29097     case BFD_RELOC_ARM_PCREL_BRANCH:
29098     case BFD_RELOC_ARM_PCREL_BLX:
29099     case BFD_RELOC_RVA:
29100     case BFD_RELOC_THUMB_PCREL_BRANCH7:
29101     case BFD_RELOC_THUMB_PCREL_BRANCH9:
29102     case BFD_RELOC_THUMB_PCREL_BRANCH12:
29103     case BFD_RELOC_THUMB_PCREL_BRANCH20:
29104     case BFD_RELOC_THUMB_PCREL_BRANCH23:
29105     case BFD_RELOC_THUMB_PCREL_BRANCH25:
29106     case BFD_RELOC_VTABLE_ENTRY:
29107     case BFD_RELOC_VTABLE_INHERIT:
29108 #ifdef TE_PE
29109     case BFD_RELOC_32_SECREL:
29110 #endif
29111       code = fixp->fx_r_type;
29112       break;
29113
29114     case BFD_RELOC_THUMB_PCREL_BLX:
29115 #ifdef OBJ_ELF
29116       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
29117         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
29118       else
29119 #endif
29120         code = BFD_RELOC_THUMB_PCREL_BLX;
29121       break;
29122
29123     case BFD_RELOC_ARM_LITERAL:
29124     case BFD_RELOC_ARM_HWLITERAL:
29125       /* If this is called then the a literal has
29126          been referenced across a section boundary.  */
29127       as_bad_where (fixp->fx_file, fixp->fx_line,
29128                     _("literal referenced across section boundary"));
29129       return NULL;
29130
29131 #ifdef OBJ_ELF
29132     case BFD_RELOC_ARM_TLS_CALL:
29133     case BFD_RELOC_ARM_THM_TLS_CALL:
29134     case BFD_RELOC_ARM_TLS_DESCSEQ:
29135     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
29136     case BFD_RELOC_ARM_GOT32:
29137     case BFD_RELOC_ARM_GOTOFF:
29138     case BFD_RELOC_ARM_GOT_PREL:
29139     case BFD_RELOC_ARM_PLT32:
29140     case BFD_RELOC_ARM_TARGET1:
29141     case BFD_RELOC_ARM_ROSEGREL32:
29142     case BFD_RELOC_ARM_SBREL32:
29143     case BFD_RELOC_ARM_PREL31:
29144     case BFD_RELOC_ARM_TARGET2:
29145     case BFD_RELOC_ARM_TLS_LDO32:
29146     case BFD_RELOC_ARM_PCREL_CALL:
29147     case BFD_RELOC_ARM_PCREL_JUMP:
29148     case BFD_RELOC_ARM_ALU_PC_G0_NC:
29149     case BFD_RELOC_ARM_ALU_PC_G0:
29150     case BFD_RELOC_ARM_ALU_PC_G1_NC:
29151     case BFD_RELOC_ARM_ALU_PC_G1:
29152     case BFD_RELOC_ARM_ALU_PC_G2:
29153     case BFD_RELOC_ARM_LDR_PC_G0:
29154     case BFD_RELOC_ARM_LDR_PC_G1:
29155     case BFD_RELOC_ARM_LDR_PC_G2:
29156     case BFD_RELOC_ARM_LDRS_PC_G0:
29157     case BFD_RELOC_ARM_LDRS_PC_G1:
29158     case BFD_RELOC_ARM_LDRS_PC_G2:
29159     case BFD_RELOC_ARM_LDC_PC_G0:
29160     case BFD_RELOC_ARM_LDC_PC_G1:
29161     case BFD_RELOC_ARM_LDC_PC_G2:
29162     case BFD_RELOC_ARM_ALU_SB_G0_NC:
29163     case BFD_RELOC_ARM_ALU_SB_G0:
29164     case BFD_RELOC_ARM_ALU_SB_G1_NC:
29165     case BFD_RELOC_ARM_ALU_SB_G1:
29166     case BFD_RELOC_ARM_ALU_SB_G2:
29167     case BFD_RELOC_ARM_LDR_SB_G0:
29168     case BFD_RELOC_ARM_LDR_SB_G1:
29169     case BFD_RELOC_ARM_LDR_SB_G2:
29170     case BFD_RELOC_ARM_LDRS_SB_G0:
29171     case BFD_RELOC_ARM_LDRS_SB_G1:
29172     case BFD_RELOC_ARM_LDRS_SB_G2:
29173     case BFD_RELOC_ARM_LDC_SB_G0:
29174     case BFD_RELOC_ARM_LDC_SB_G1:
29175     case BFD_RELOC_ARM_LDC_SB_G2:
29176     case BFD_RELOC_ARM_V4BX:
29177     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
29178     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
29179     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
29180     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
29181     case BFD_RELOC_ARM_GOTFUNCDESC:
29182     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
29183     case BFD_RELOC_ARM_FUNCDESC:
29184     case BFD_RELOC_ARM_THUMB_BF17:
29185     case BFD_RELOC_ARM_THUMB_BF19:
29186     case BFD_RELOC_ARM_THUMB_BF13:
29187       code = fixp->fx_r_type;
29188       break;
29189
29190     case BFD_RELOC_ARM_TLS_GOTDESC:
29191     case BFD_RELOC_ARM_TLS_GD32:
29192     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
29193     case BFD_RELOC_ARM_TLS_LE32:
29194     case BFD_RELOC_ARM_TLS_IE32:
29195     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
29196     case BFD_RELOC_ARM_TLS_LDM32:
29197     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
29198       /* BFD will include the symbol's address in the addend.
29199          But we don't want that, so subtract it out again here.  */
29200       if (!S_IS_COMMON (fixp->fx_addsy))
29201         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
29202       code = fixp->fx_r_type;
29203       break;
29204 #endif
29205
29206     case BFD_RELOC_ARM_IMMEDIATE:
29207       as_bad_where (fixp->fx_file, fixp->fx_line,
29208                     _("internal relocation (type: IMMEDIATE) not fixed up"));
29209       return NULL;
29210
29211     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
29212       as_bad_where (fixp->fx_file, fixp->fx_line,
29213                     _("ADRL used for a symbol not defined in the same file"));
29214       return NULL;
29215
29216     case BFD_RELOC_THUMB_PCREL_BRANCH5:
29217     case BFD_RELOC_THUMB_PCREL_BFCSEL:
29218     case BFD_RELOC_ARM_THUMB_LOOP12:
29219       as_bad_where (fixp->fx_file, fixp->fx_line,
29220                     _("%s used for a symbol not defined in the same file"),
29221                     bfd_get_reloc_code_name (fixp->fx_r_type));
29222       return NULL;
29223
29224     case BFD_RELOC_ARM_OFFSET_IMM:
29225       if (section->use_rela_p)
29226         {
29227           code = fixp->fx_r_type;
29228           break;
29229         }
29230
29231       if (fixp->fx_addsy != NULL
29232           && !S_IS_DEFINED (fixp->fx_addsy)
29233           && S_IS_LOCAL (fixp->fx_addsy))
29234         {
29235           as_bad_where (fixp->fx_file, fixp->fx_line,
29236                         _("undefined local label `%s'"),
29237                         S_GET_NAME (fixp->fx_addsy));
29238           return NULL;
29239         }
29240
29241       as_bad_where (fixp->fx_file, fixp->fx_line,
29242                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
29243       return NULL;
29244
29245     default:
29246       {
29247         const char * type;
29248
29249         switch (fixp->fx_r_type)
29250           {
29251           case BFD_RELOC_NONE:             type = "NONE";         break;
29252           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
29253           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
29254           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
29255           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
29256           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
29257           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
29258           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
29259           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
29260           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
29261           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
29262           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
29263           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
29264           default:                         type = _("<unknown>"); break;
29265           }
29266         as_bad_where (fixp->fx_file, fixp->fx_line,
29267                       _("cannot represent %s relocation in this object file format"),
29268                       type);
29269         return NULL;
29270       }
29271     }
29272
29273 #ifdef OBJ_ELF
29274   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
29275       && GOT_symbol
29276       && fixp->fx_addsy == GOT_symbol)
29277     {
29278       code = BFD_RELOC_ARM_GOTPC;
29279       reloc->addend = fixp->fx_offset = reloc->address;
29280     }
29281 #endif
29282
29283   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
29284
29285   if (reloc->howto == NULL)
29286     {
29287       as_bad_where (fixp->fx_file, fixp->fx_line,
29288                     _("cannot represent %s relocation in this object file format"),
29289                     bfd_get_reloc_code_name (code));
29290       return NULL;
29291     }
29292
29293   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
29294      vtable entry to be used in the relocation's section offset.  */
29295   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
29296     reloc->address = fixp->fx_offset;
29297
29298   return reloc;
29299 }
29300
29301 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
29302
29303 void
29304 cons_fix_new_arm (fragS *       frag,
29305                   int           where,
29306                   int           size,
29307                   expressionS * exp,
29308                   bfd_reloc_code_real_type reloc)
29309 {
29310   int pcrel = 0;
29311
29312   /* Pick a reloc.
29313      FIXME: @@ Should look at CPU word size.  */
29314   switch (size)
29315     {
29316     case 1:
29317       reloc = BFD_RELOC_8;
29318       break;
29319     case 2:
29320       reloc = BFD_RELOC_16;
29321       break;
29322     case 4:
29323     default:
29324       reloc = BFD_RELOC_32;
29325       break;
29326     case 8:
29327       reloc = BFD_RELOC_64;
29328       break;
29329     }
29330
29331 #ifdef TE_PE
29332   if (exp->X_op == O_secrel)
29333   {
29334     exp->X_op = O_symbol;
29335     reloc = BFD_RELOC_32_SECREL;
29336   }
29337 #endif
29338
29339   fix_new_exp (frag, where, size, exp, pcrel, reloc);
29340 }
29341
29342 #if defined (OBJ_COFF)
29343 void
29344 arm_validate_fix (fixS * fixP)
29345 {
29346   /* If the destination of the branch is a defined symbol which does not have
29347      the THUMB_FUNC attribute, then we must be calling a function which has
29348      the (interfacearm) attribute.  We look for the Thumb entry point to that
29349      function and change the branch to refer to that function instead.  */
29350   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
29351       && fixP->fx_addsy != NULL
29352       && S_IS_DEFINED (fixP->fx_addsy)
29353       && ! THUMB_IS_FUNC (fixP->fx_addsy))
29354     {
29355       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
29356     }
29357 }
29358 #endif
29359
29360
29361 int
29362 arm_force_relocation (struct fix * fixp)
29363 {
29364 #if defined (OBJ_COFF) && defined (TE_PE)
29365   if (fixp->fx_r_type == BFD_RELOC_RVA)
29366     return 1;
29367 #endif
29368
29369   /* In case we have a call or a branch to a function in ARM ISA mode from
29370      a thumb function or vice-versa force the relocation. These relocations
29371      are cleared off for some cores that might have blx and simple transformations
29372      are possible.  */
29373
29374 #ifdef OBJ_ELF
29375   switch (fixp->fx_r_type)
29376     {
29377     case BFD_RELOC_ARM_PCREL_JUMP:
29378     case BFD_RELOC_ARM_PCREL_CALL:
29379     case BFD_RELOC_THUMB_PCREL_BLX:
29380       if (THUMB_IS_FUNC (fixp->fx_addsy))
29381         return 1;
29382       break;
29383
29384     case BFD_RELOC_ARM_PCREL_BLX:
29385     case BFD_RELOC_THUMB_PCREL_BRANCH25:
29386     case BFD_RELOC_THUMB_PCREL_BRANCH20:
29387     case BFD_RELOC_THUMB_PCREL_BRANCH23:
29388       if (ARM_IS_FUNC (fixp->fx_addsy))
29389         return 1;
29390       break;
29391
29392     default:
29393       break;
29394     }
29395 #endif
29396
29397   /* Resolve these relocations even if the symbol is extern or weak.
29398      Technically this is probably wrong due to symbol preemption.
29399      In practice these relocations do not have enough range to be useful
29400      at dynamic link time, and some code (e.g. in the Linux kernel)
29401      expects these references to be resolved.  */
29402   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
29403       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
29404       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
29405       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
29406       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
29407       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
29408       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
29409       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
29410       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
29411       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
29412       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
29413       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
29414       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
29415       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
29416     return 0;
29417
29418   /* Always leave these relocations for the linker.  */
29419   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
29420        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
29421       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
29422     return 1;
29423
29424   /* Always generate relocations against function symbols.  */
29425   if (fixp->fx_r_type == BFD_RELOC_32
29426       && fixp->fx_addsy
29427       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
29428     return 1;
29429
29430   return generic_force_reloc (fixp);
29431 }
29432
29433 #if defined (OBJ_ELF) || defined (OBJ_COFF)
29434 /* Relocations against function names must be left unadjusted,
29435    so that the linker can use this information to generate interworking
29436    stubs.  The MIPS version of this function
29437    also prevents relocations that are mips-16 specific, but I do not
29438    know why it does this.
29439
29440    FIXME:
29441    There is one other problem that ought to be addressed here, but
29442    which currently is not:  Taking the address of a label (rather
29443    than a function) and then later jumping to that address.  Such
29444    addresses also ought to have their bottom bit set (assuming that
29445    they reside in Thumb code), but at the moment they will not.  */
29446
29447 bfd_boolean
29448 arm_fix_adjustable (fixS * fixP)
29449 {
29450   if (fixP->fx_addsy == NULL)
29451     return 1;
29452
29453   /* Preserve relocations against symbols with function type.  */
29454   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
29455     return FALSE;
29456
29457   if (THUMB_IS_FUNC (fixP->fx_addsy)
29458       && fixP->fx_subsy == NULL)
29459     return FALSE;
29460
29461   /* We need the symbol name for the VTABLE entries.  */
29462   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
29463       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
29464     return FALSE;
29465
29466   /* Don't allow symbols to be discarded on GOT related relocs.  */
29467   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
29468       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
29469       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
29470       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
29471       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32_FDPIC
29472       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
29473       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
29474       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32_FDPIC
29475       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
29476       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32_FDPIC
29477       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
29478       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
29479       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
29480       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
29481       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
29482       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
29483       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
29484     return FALSE;
29485
29486   /* Similarly for group relocations.  */
29487   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
29488        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
29489       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
29490     return FALSE;
29491
29492   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
29493   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
29494       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
29495       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
29496       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
29497       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
29498       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
29499       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
29500       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
29501     return FALSE;
29502
29503   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
29504      offsets, so keep these symbols.  */
29505   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
29506       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
29507     return FALSE;
29508
29509   return TRUE;
29510 }
29511 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
29512
29513 #ifdef OBJ_ELF
29514 const char *
29515 elf32_arm_target_format (void)
29516 {
29517 #ifdef TE_SYMBIAN
29518   return (target_big_endian
29519           ? "elf32-bigarm-symbian"
29520           : "elf32-littlearm-symbian");
29521 #elif defined (TE_VXWORKS)
29522   return (target_big_endian
29523           ? "elf32-bigarm-vxworks"
29524           : "elf32-littlearm-vxworks");
29525 #elif defined (TE_NACL)
29526   return (target_big_endian
29527           ? "elf32-bigarm-nacl"
29528           : "elf32-littlearm-nacl");
29529 #else
29530   if (arm_fdpic)
29531     {
29532       if (target_big_endian)
29533         return "elf32-bigarm-fdpic";
29534       else
29535         return "elf32-littlearm-fdpic";
29536     }
29537   else
29538     {
29539       if (target_big_endian)
29540         return "elf32-bigarm";
29541       else
29542         return "elf32-littlearm";
29543     }
29544 #endif
29545 }
29546
29547 void
29548 armelf_frob_symbol (symbolS * symp,
29549                     int *     puntp)
29550 {
29551   elf_frob_symbol (symp, puntp);
29552 }
29553 #endif
29554
29555 /* MD interface: Finalization.  */
29556
29557 void
29558 arm_cleanup (void)
29559 {
29560   literal_pool * pool;
29561
29562   /* Ensure that all the predication blocks are properly closed.  */
29563   check_pred_blocks_finished ();
29564
29565   for (pool = list_of_pools; pool; pool = pool->next)
29566     {
29567       /* Put it at the end of the relevant section.  */
29568       subseg_set (pool->section, pool->sub_section);
29569 #ifdef OBJ_ELF
29570       arm_elf_change_section ();
29571 #endif
29572       s_ltorg (0);
29573     }
29574 }
29575
29576 #ifdef OBJ_ELF
29577 /* Remove any excess mapping symbols generated for alignment frags in
29578    SEC.  We may have created a mapping symbol before a zero byte
29579    alignment; remove it if there's a mapping symbol after the
29580    alignment.  */
29581 static void
29582 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
29583                        void *dummy ATTRIBUTE_UNUSED)
29584 {
29585   segment_info_type *seginfo = seg_info (sec);
29586   fragS *fragp;
29587
29588   if (seginfo == NULL || seginfo->frchainP == NULL)
29589     return;
29590
29591   for (fragp = seginfo->frchainP->frch_root;
29592        fragp != NULL;
29593        fragp = fragp->fr_next)
29594     {
29595       symbolS *sym = fragp->tc_frag_data.last_map;
29596       fragS *next = fragp->fr_next;
29597
29598       /* Variable-sized frags have been converted to fixed size by
29599          this point.  But if this was variable-sized to start with,
29600          there will be a fixed-size frag after it.  So don't handle
29601          next == NULL.  */
29602       if (sym == NULL || next == NULL)
29603         continue;
29604
29605       if (S_GET_VALUE (sym) < next->fr_address)
29606         /* Not at the end of this frag.  */
29607         continue;
29608       know (S_GET_VALUE (sym) == next->fr_address);
29609
29610       do
29611         {
29612           if (next->tc_frag_data.first_map != NULL)
29613             {
29614               /* Next frag starts with a mapping symbol.  Discard this
29615                  one.  */
29616               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
29617               break;
29618             }
29619
29620           if (next->fr_next == NULL)
29621             {
29622               /* This mapping symbol is at the end of the section.  Discard
29623                  it.  */
29624               know (next->fr_fix == 0 && next->fr_var == 0);
29625               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
29626               break;
29627             }
29628
29629           /* As long as we have empty frags without any mapping symbols,
29630              keep looking.  */
29631           /* If the next frag is non-empty and does not start with a
29632              mapping symbol, then this mapping symbol is required.  */
29633           if (next->fr_address != next->fr_next->fr_address)
29634             break;
29635
29636           next = next->fr_next;
29637         }
29638       while (next != NULL);
29639     }
29640 }
29641 #endif
29642
29643 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
29644    ARM ones.  */
29645
29646 void
29647 arm_adjust_symtab (void)
29648 {
29649 #ifdef OBJ_COFF
29650   symbolS * sym;
29651
29652   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
29653     {
29654       if (ARM_IS_THUMB (sym))
29655         {
29656           if (THUMB_IS_FUNC (sym))
29657             {
29658               /* Mark the symbol as a Thumb function.  */
29659               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
29660                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
29661                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
29662
29663               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
29664                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
29665               else
29666                 as_bad (_("%s: unexpected function type: %d"),
29667                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
29668             }
29669           else switch (S_GET_STORAGE_CLASS (sym))
29670             {
29671             case C_EXT:
29672               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
29673               break;
29674             case C_STAT:
29675               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
29676               break;
29677             case C_LABEL:
29678               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
29679               break;
29680             default:
29681               /* Do nothing.  */
29682               break;
29683             }
29684         }
29685
29686       if (ARM_IS_INTERWORK (sym))
29687         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
29688     }
29689 #endif
29690 #ifdef OBJ_ELF
29691   symbolS * sym;
29692   char      bind;
29693
29694   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
29695     {
29696       if (ARM_IS_THUMB (sym))
29697         {
29698           elf_symbol_type * elf_sym;
29699
29700           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
29701           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
29702
29703           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
29704                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
29705             {
29706               /* If it's a .thumb_func, declare it as so,
29707                  otherwise tag label as .code 16.  */
29708               if (THUMB_IS_FUNC (sym))
29709                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
29710                                          ST_BRANCH_TO_THUMB);
29711               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
29712                 elf_sym->internal_elf_sym.st_info =
29713                   ELF_ST_INFO (bind, STT_ARM_16BIT);
29714             }
29715         }
29716     }
29717
29718   /* Remove any overlapping mapping symbols generated by alignment frags.  */
29719   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
29720   /* Now do generic ELF adjustments.  */
29721   elf_adjust_symtab ();
29722 #endif
29723 }
29724
29725 /* MD interface: Initialization.  */
29726
29727 static void
29728 set_constant_flonums (void)
29729 {
29730   int i;
29731
29732   for (i = 0; i < NUM_FLOAT_VALS; i++)
29733     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
29734       abort ();
29735 }
29736
29737 /* Auto-select Thumb mode if it's the only available instruction set for the
29738    given architecture.  */
29739
29740 static void
29741 autoselect_thumb_from_cpu_variant (void)
29742 {
29743   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
29744     opcode_select (16);
29745 }
29746
29747 void
29748 md_begin (void)
29749 {
29750   unsigned mach;
29751   unsigned int i;
29752
29753   if (   (arm_ops_hsh = hash_new ()) == NULL
29754       || (arm_cond_hsh = hash_new ()) == NULL
29755       || (arm_vcond_hsh = hash_new ()) == NULL
29756       || (arm_shift_hsh = hash_new ()) == NULL
29757       || (arm_psr_hsh = hash_new ()) == NULL
29758       || (arm_v7m_psr_hsh = hash_new ()) == NULL
29759       || (arm_reg_hsh = hash_new ()) == NULL
29760       || (arm_reloc_hsh = hash_new ()) == NULL
29761       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
29762     as_fatal (_("virtual memory exhausted"));
29763
29764   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
29765     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
29766   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
29767     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
29768   for (i = 0; i < sizeof (vconds) / sizeof (struct asm_cond); i++)
29769     hash_insert (arm_vcond_hsh, vconds[i].template_name, (void *) (vconds + i));
29770   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
29771     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
29772   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
29773     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
29774   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
29775     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
29776                  (void *) (v7m_psrs + i));
29777   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
29778     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
29779   for (i = 0;
29780        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
29781        i++)
29782     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
29783                  (void *) (barrier_opt_names + i));
29784 #ifdef OBJ_ELF
29785   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
29786     {
29787       struct reloc_entry * entry = reloc_names + i;
29788
29789       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
29790         /* This makes encode_branch() use the EABI versions of this relocation.  */
29791         entry->reloc = BFD_RELOC_UNUSED;
29792
29793       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
29794     }
29795 #endif
29796
29797   set_constant_flonums ();
29798
29799   /* Set the cpu variant based on the command-line options.  We prefer
29800      -mcpu= over -march= if both are set (as for GCC); and we prefer
29801      -mfpu= over any other way of setting the floating point unit.
29802      Use of legacy options with new options are faulted.  */
29803   if (legacy_cpu)
29804     {
29805       if (mcpu_cpu_opt || march_cpu_opt)
29806         as_bad (_("use of old and new-style options to set CPU type"));
29807
29808       selected_arch = *legacy_cpu;
29809     }
29810   else if (mcpu_cpu_opt)
29811     {
29812       selected_arch = *mcpu_cpu_opt;
29813       selected_ext = *mcpu_ext_opt;
29814     }
29815   else if (march_cpu_opt)
29816     {
29817       selected_arch = *march_cpu_opt;
29818       selected_ext = *march_ext_opt;
29819     }
29820   ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
29821
29822   if (legacy_fpu)
29823     {
29824       if (mfpu_opt)
29825         as_bad (_("use of old and new-style options to set FPU type"));
29826
29827       selected_fpu = *legacy_fpu;
29828     }
29829   else if (mfpu_opt)
29830     selected_fpu = *mfpu_opt;
29831   else
29832     {
29833 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
29834         || defined (TE_NetBSD) || defined (TE_VXWORKS))
29835       /* Some environments specify a default FPU.  If they don't, infer it
29836          from the processor.  */
29837       if (mcpu_fpu_opt)
29838         selected_fpu = *mcpu_fpu_opt;
29839       else if (march_fpu_opt)
29840         selected_fpu = *march_fpu_opt;
29841 #else
29842       selected_fpu = fpu_default;
29843 #endif
29844     }
29845
29846   if (ARM_FEATURE_ZERO (selected_fpu))
29847     {
29848       if (!no_cpu_selected ())
29849         selected_fpu = fpu_default;
29850       else
29851         selected_fpu = fpu_arch_fpa;
29852     }
29853
29854 #ifdef CPU_DEFAULT
29855   if (ARM_FEATURE_ZERO (selected_arch))
29856     {
29857       selected_arch = cpu_default;
29858       selected_cpu = selected_arch;
29859     }
29860   ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29861 #else
29862   /*  Autodection of feature mode: allow all features in cpu_variant but leave
29863       selected_cpu unset.  It will be set in aeabi_set_public_attributes ()
29864       after all instruction have been processed and we can decide what CPU
29865       should be selected.  */
29866   if (ARM_FEATURE_ZERO (selected_arch))
29867     ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
29868   else
29869     ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29870 #endif
29871
29872   autoselect_thumb_from_cpu_variant ();
29873
29874   arm_arch_used = thumb_arch_used = arm_arch_none;
29875
29876 #if defined OBJ_COFF || defined OBJ_ELF
29877   {
29878     unsigned int flags = 0;
29879
29880 #if defined OBJ_ELF
29881     flags = meabi_flags;
29882
29883     switch (meabi_flags)
29884       {
29885       case EF_ARM_EABI_UNKNOWN:
29886 #endif
29887         /* Set the flags in the private structure.  */
29888         if (uses_apcs_26)      flags |= F_APCS26;
29889         if (support_interwork) flags |= F_INTERWORK;
29890         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
29891         if (pic_code)          flags |= F_PIC;
29892         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
29893           flags |= F_SOFT_FLOAT;
29894
29895         switch (mfloat_abi_opt)
29896           {
29897           case ARM_FLOAT_ABI_SOFT:
29898           case ARM_FLOAT_ABI_SOFTFP:
29899             flags |= F_SOFT_FLOAT;
29900             break;
29901
29902           case ARM_FLOAT_ABI_HARD:
29903             if (flags & F_SOFT_FLOAT)
29904               as_bad (_("hard-float conflicts with specified fpu"));
29905             break;
29906           }
29907
29908         /* Using pure-endian doubles (even if soft-float).      */
29909         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
29910           flags |= F_VFP_FLOAT;
29911
29912 #if defined OBJ_ELF
29913         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
29914             flags |= EF_ARM_MAVERICK_FLOAT;
29915         break;
29916
29917       case EF_ARM_EABI_VER4:
29918       case EF_ARM_EABI_VER5:
29919         /* No additional flags to set.  */
29920         break;
29921
29922       default:
29923         abort ();
29924       }
29925 #endif
29926     bfd_set_private_flags (stdoutput, flags);
29927
29928     /* We have run out flags in the COFF header to encode the
29929        status of ATPCS support, so instead we create a dummy,
29930        empty, debug section called .arm.atpcs.  */
29931     if (atpcs)
29932       {
29933         asection * sec;
29934
29935         sec = bfd_make_section (stdoutput, ".arm.atpcs");
29936
29937         if (sec != NULL)
29938           {
29939             bfd_set_section_flags
29940               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
29941             bfd_set_section_size (stdoutput, sec, 0);
29942             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
29943           }
29944       }
29945   }
29946 #endif
29947
29948   /* Record the CPU type as well.  */
29949   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
29950     mach = bfd_mach_arm_iWMMXt2;
29951   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
29952     mach = bfd_mach_arm_iWMMXt;
29953   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
29954     mach = bfd_mach_arm_XScale;
29955   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
29956     mach = bfd_mach_arm_ep9312;
29957   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
29958     mach = bfd_mach_arm_5TE;
29959   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
29960     {
29961       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
29962         mach = bfd_mach_arm_5T;
29963       else
29964         mach = bfd_mach_arm_5;
29965     }
29966   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
29967     {
29968       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
29969         mach = bfd_mach_arm_4T;
29970       else
29971         mach = bfd_mach_arm_4;
29972     }
29973   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
29974     mach = bfd_mach_arm_3M;
29975   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
29976     mach = bfd_mach_arm_3;
29977   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
29978     mach = bfd_mach_arm_2a;
29979   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
29980     mach = bfd_mach_arm_2;
29981   else
29982     mach = bfd_mach_arm_unknown;
29983
29984   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
29985 }
29986
29987 /* Command line processing.  */
29988
29989 /* md_parse_option
29990       Invocation line includes a switch not recognized by the base assembler.
29991       See if it's a processor-specific option.
29992
29993       This routine is somewhat complicated by the need for backwards
29994       compatibility (since older releases of gcc can't be changed).
29995       The new options try to make the interface as compatible as
29996       possible with GCC.
29997
29998       New options (supported) are:
29999
30000               -mcpu=<cpu name>           Assemble for selected processor
30001               -march=<architecture name> Assemble for selected architecture
30002               -mfpu=<fpu architecture>   Assemble for selected FPU.
30003               -EB/-mbig-endian           Big-endian
30004               -EL/-mlittle-endian        Little-endian
30005               -k                         Generate PIC code
30006               -mthumb                    Start in Thumb mode
30007               -mthumb-interwork          Code supports ARM/Thumb interworking
30008
30009               -m[no-]warn-deprecated     Warn about deprecated features
30010               -m[no-]warn-syms           Warn when symbols match instructions
30011
30012       For now we will also provide support for:
30013
30014               -mapcs-32                  32-bit Program counter
30015               -mapcs-26                  26-bit Program counter
30016               -macps-float               Floats passed in FP registers
30017               -mapcs-reentrant           Reentrant code
30018               -matpcs
30019       (sometime these will probably be replaced with -mapcs=<list of options>
30020       and -matpcs=<list of options>)
30021
30022       The remaining options are only supported for back-wards compatibility.
30023       Cpu variants, the arm part is optional:
30024               -m[arm]1                Currently not supported.
30025               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
30026               -m[arm]3                Arm 3 processor
30027               -m[arm]6[xx],           Arm 6 processors
30028               -m[arm]7[xx][t][[d]m]   Arm 7 processors
30029               -m[arm]8[10]            Arm 8 processors
30030               -m[arm]9[20][tdmi]      Arm 9 processors
30031               -mstrongarm[110[0]]     StrongARM processors
30032               -mxscale                XScale processors
30033               -m[arm]v[2345[t[e]]]    Arm architectures
30034               -mall                   All (except the ARM1)
30035       FP variants:
30036               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
30037               -mfpe-old               (No float load/store multiples)
30038               -mvfpxd                 VFP Single precision
30039               -mvfp                   All VFP
30040               -mno-fpu                Disable all floating point instructions
30041
30042       The following CPU names are recognized:
30043               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
30044               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
30045               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
30046               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
30047               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
30048               arm10t arm10e, arm1020t, arm1020e, arm10200e,
30049               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
30050
30051       */
30052
30053 const char * md_shortopts = "m:k";
30054
30055 #ifdef ARM_BI_ENDIAN
30056 #define OPTION_EB (OPTION_MD_BASE + 0)
30057 #define OPTION_EL (OPTION_MD_BASE + 1)
30058 #else
30059 #if TARGET_BYTES_BIG_ENDIAN
30060 #define OPTION_EB (OPTION_MD_BASE + 0)
30061 #else
30062 #define OPTION_EL (OPTION_MD_BASE + 1)
30063 #endif
30064 #endif
30065 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
30066 #define OPTION_FDPIC (OPTION_MD_BASE + 3)
30067
30068 struct option md_longopts[] =
30069 {
30070 #ifdef OPTION_EB
30071   {"EB", no_argument, NULL, OPTION_EB},
30072 #endif
30073 #ifdef OPTION_EL
30074   {"EL", no_argument, NULL, OPTION_EL},
30075 #endif
30076   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
30077 #ifdef OBJ_ELF
30078   {"fdpic", no_argument, NULL, OPTION_FDPIC},
30079 #endif
30080   {NULL, no_argument, NULL, 0}
30081 };
30082
30083 size_t md_longopts_size = sizeof (md_longopts);
30084
30085 struct arm_option_table
30086 {
30087   const char *  option;         /* Option name to match.  */
30088   const char *  help;           /* Help information.  */
30089   int *         var;            /* Variable to change.  */
30090   int           value;          /* What to change it to.  */
30091   const char *  deprecated;     /* If non-null, print this message.  */
30092 };
30093
30094 struct arm_option_table arm_opts[] =
30095 {
30096   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
30097   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
30098   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
30099    &support_interwork, 1, NULL},
30100   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
30101   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
30102   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
30103    1, NULL},
30104   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
30105   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
30106   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
30107   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
30108    NULL},
30109
30110   /* These are recognized by the assembler, but have no affect on code.  */
30111   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
30112   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
30113
30114   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
30115   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
30116    &warn_on_deprecated, 0, NULL},
30117   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
30118   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
30119   {NULL, NULL, NULL, 0, NULL}
30120 };
30121
30122 struct arm_legacy_option_table
30123 {
30124   const char *              option;             /* Option name to match.  */
30125   const arm_feature_set **  var;                /* Variable to change.  */
30126   const arm_feature_set     value;              /* What to change it to.  */
30127   const char *              deprecated;         /* If non-null, print this message.  */
30128 };
30129
30130 const struct arm_legacy_option_table arm_legacy_opts[] =
30131 {
30132   /* DON'T add any new processors to this list -- we want the whole list
30133      to go away...  Add them to the processors table instead.  */
30134   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
30135   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
30136   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
30137   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
30138   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
30139   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
30140   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
30141   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
30142   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
30143   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
30144   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
30145   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
30146   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
30147   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
30148   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
30149   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
30150   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
30151   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
30152   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
30153   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
30154   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
30155   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
30156   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
30157   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
30158   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
30159   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
30160   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
30161   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
30162   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
30163   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
30164   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
30165   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
30166   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
30167   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
30168   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
30169   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
30170   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
30171   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
30172   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
30173   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
30174   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
30175   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
30176   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
30177   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
30178   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
30179   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
30180   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30181   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30182   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30183   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30184   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
30185   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
30186   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
30187   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
30188   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
30189   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
30190   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
30191   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
30192   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
30193   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
30194   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
30195   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
30196   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
30197   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
30198   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
30199   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
30200   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
30201   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
30202   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
30203   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
30204    N_("use -mcpu=strongarm110")},
30205   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
30206    N_("use -mcpu=strongarm1100")},
30207   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
30208    N_("use -mcpu=strongarm1110")},
30209   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
30210   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
30211   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
30212
30213   /* Architecture variants -- don't add any more to this list either.  */
30214   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
30215   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
30216   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
30217   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
30218   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
30219   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
30220   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
30221   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
30222   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
30223   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
30224   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
30225   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
30226   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
30227   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
30228   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
30229   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
30230   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
30231   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
30232
30233   /* Floating point variants -- don't add any more to this list either.  */
30234   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
30235   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
30236   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
30237   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
30238    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
30239
30240   {NULL, NULL, ARM_ARCH_NONE, NULL}
30241 };
30242
30243 struct arm_cpu_option_table
30244 {
30245   const char *           name;
30246   size_t                 name_len;
30247   const arm_feature_set  value;
30248   const arm_feature_set  ext;
30249   /* For some CPUs we assume an FPU unless the user explicitly sets
30250      -mfpu=...  */
30251   const arm_feature_set  default_fpu;
30252   /* The canonical name of the CPU, or NULL to use NAME converted to upper
30253      case.  */
30254   const char *           canonical_name;
30255 };
30256
30257 /* This list should, at a minimum, contain all the cpu names
30258    recognized by GCC.  */
30259 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
30260
30261 static const struct arm_cpu_option_table arm_cpus[] =
30262 {
30263   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
30264                ARM_ARCH_NONE,
30265                FPU_ARCH_FPA),
30266   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
30267                ARM_ARCH_NONE,
30268                FPU_ARCH_FPA),
30269   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
30270                ARM_ARCH_NONE,
30271                FPU_ARCH_FPA),
30272   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
30273                ARM_ARCH_NONE,
30274                FPU_ARCH_FPA),
30275   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
30276                ARM_ARCH_NONE,
30277                FPU_ARCH_FPA),
30278   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
30279                ARM_ARCH_NONE,
30280                FPU_ARCH_FPA),
30281   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
30282                ARM_ARCH_NONE,
30283                FPU_ARCH_FPA),
30284   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
30285                ARM_ARCH_NONE,
30286                FPU_ARCH_FPA),
30287   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
30288                ARM_ARCH_NONE,
30289                FPU_ARCH_FPA),
30290   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
30291                ARM_ARCH_NONE,
30292                FPU_ARCH_FPA),
30293   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
30294                ARM_ARCH_NONE,
30295                FPU_ARCH_FPA),
30296   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
30297                ARM_ARCH_NONE,
30298                FPU_ARCH_FPA),
30299   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
30300                ARM_ARCH_NONE,
30301                FPU_ARCH_FPA),
30302   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
30303                ARM_ARCH_NONE,
30304                FPU_ARCH_FPA),
30305   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
30306                ARM_ARCH_NONE,
30307                FPU_ARCH_FPA),
30308   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
30309                ARM_ARCH_NONE,
30310                FPU_ARCH_FPA),
30311   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
30312                ARM_ARCH_NONE,
30313                FPU_ARCH_FPA),
30314   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
30315                ARM_ARCH_NONE,
30316                FPU_ARCH_FPA),
30317   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
30318                ARM_ARCH_NONE,
30319                FPU_ARCH_FPA),
30320   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
30321                ARM_ARCH_NONE,
30322                FPU_ARCH_FPA),
30323   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
30324                ARM_ARCH_NONE,
30325                FPU_ARCH_FPA),
30326   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
30327                ARM_ARCH_NONE,
30328                FPU_ARCH_FPA),
30329   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
30330                ARM_ARCH_NONE,
30331                FPU_ARCH_FPA),
30332   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
30333                ARM_ARCH_NONE,
30334                FPU_ARCH_FPA),
30335   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
30336                ARM_ARCH_NONE,
30337                FPU_ARCH_FPA),
30338   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
30339                ARM_ARCH_NONE,
30340                FPU_ARCH_FPA),
30341   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
30342                ARM_ARCH_NONE,
30343                FPU_ARCH_FPA),
30344   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
30345                ARM_ARCH_NONE,
30346                FPU_ARCH_FPA),
30347   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
30348                ARM_ARCH_NONE,
30349                FPU_ARCH_FPA),
30350   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
30351                ARM_ARCH_NONE,
30352                FPU_ARCH_FPA),
30353   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
30354                ARM_ARCH_NONE,
30355                FPU_ARCH_FPA),
30356   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
30357                ARM_ARCH_NONE,
30358                FPU_ARCH_FPA),
30359   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
30360                ARM_ARCH_NONE,
30361                FPU_ARCH_FPA),
30362   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
30363                ARM_ARCH_NONE,
30364                FPU_ARCH_FPA),
30365   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
30366                ARM_ARCH_NONE,
30367                FPU_ARCH_FPA),
30368   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
30369                ARM_ARCH_NONE,
30370                FPU_ARCH_FPA),
30371   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
30372                ARM_ARCH_NONE,
30373                FPU_ARCH_FPA),
30374   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
30375                ARM_ARCH_NONE,
30376                FPU_ARCH_FPA),
30377   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
30378                ARM_ARCH_NONE,
30379                FPU_ARCH_FPA),
30380   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
30381                ARM_ARCH_NONE,
30382                FPU_ARCH_FPA),
30383   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
30384                ARM_ARCH_NONE,
30385                FPU_ARCH_FPA),
30386   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
30387                ARM_ARCH_NONE,
30388                FPU_ARCH_FPA),
30389   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
30390                ARM_ARCH_NONE,
30391                FPU_ARCH_FPA),
30392   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
30393                ARM_ARCH_NONE,
30394                FPU_ARCH_FPA),
30395   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
30396                ARM_ARCH_NONE,
30397                FPU_ARCH_FPA),
30398   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
30399                ARM_ARCH_NONE,
30400                FPU_ARCH_FPA),
30401
30402   /* For V5 or later processors we default to using VFP; but the user
30403      should really set the FPU type explicitly.  */
30404   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
30405                ARM_ARCH_NONE,
30406                FPU_ARCH_VFP_V2),
30407   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
30408                ARM_ARCH_NONE,
30409                FPU_ARCH_VFP_V2),
30410   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
30411                ARM_ARCH_NONE,
30412                FPU_ARCH_VFP_V2),
30413   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
30414                ARM_ARCH_NONE,
30415                FPU_ARCH_VFP_V2),
30416   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
30417                ARM_ARCH_NONE,
30418                FPU_ARCH_VFP_V2),
30419   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
30420                ARM_ARCH_NONE,
30421                FPU_ARCH_VFP_V2),
30422   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
30423                ARM_ARCH_NONE,
30424                FPU_ARCH_VFP_V2),
30425   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
30426                ARM_ARCH_NONE,
30427                FPU_ARCH_VFP_V2),
30428   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
30429                ARM_ARCH_NONE,
30430                FPU_ARCH_VFP_V2),
30431   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
30432                ARM_ARCH_NONE,
30433                FPU_ARCH_VFP_V2),
30434   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
30435                ARM_ARCH_NONE,
30436                FPU_ARCH_VFP_V2),
30437   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
30438                ARM_ARCH_NONE,
30439                FPU_ARCH_VFP_V2),
30440   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
30441                ARM_ARCH_NONE,
30442                FPU_ARCH_VFP_V1),
30443   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
30444                ARM_ARCH_NONE,
30445                FPU_ARCH_VFP_V1),
30446   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
30447                ARM_ARCH_NONE,
30448                FPU_ARCH_VFP_V2),
30449   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
30450                ARM_ARCH_NONE,
30451                FPU_ARCH_VFP_V2),
30452   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
30453                ARM_ARCH_NONE,
30454                FPU_ARCH_VFP_V1),
30455   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
30456                ARM_ARCH_NONE,
30457                FPU_ARCH_VFP_V2),
30458   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
30459                ARM_ARCH_NONE,
30460                FPU_ARCH_VFP_V2),
30461   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
30462                ARM_ARCH_NONE,
30463                FPU_ARCH_VFP_V2),
30464   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
30465                ARM_ARCH_NONE,
30466                FPU_ARCH_VFP_V2),
30467   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
30468                ARM_ARCH_NONE,
30469                FPU_ARCH_VFP_V2),
30470   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
30471                ARM_ARCH_NONE,
30472                FPU_ARCH_VFP_V2),
30473   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
30474                ARM_ARCH_NONE,
30475                FPU_ARCH_VFP_V2),
30476   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
30477                ARM_ARCH_NONE,
30478                FPU_ARCH_VFP_V2),
30479   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
30480                ARM_ARCH_NONE,
30481                FPU_ARCH_VFP_V2),
30482   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
30483                ARM_ARCH_NONE,
30484                FPU_NONE),
30485   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
30486                ARM_ARCH_NONE,
30487                FPU_NONE),
30488   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
30489                ARM_ARCH_NONE,
30490                FPU_ARCH_VFP_V2),
30491   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
30492                ARM_ARCH_NONE,
30493                FPU_ARCH_VFP_V2),
30494   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
30495                ARM_ARCH_NONE,
30496                FPU_ARCH_VFP_V2),
30497   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
30498                ARM_ARCH_NONE,
30499                FPU_NONE),
30500   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
30501                ARM_ARCH_NONE,
30502                FPU_NONE),
30503   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
30504                ARM_ARCH_NONE,
30505                FPU_ARCH_VFP_V2),
30506   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
30507                ARM_ARCH_NONE,
30508                FPU_NONE),
30509   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
30510                ARM_ARCH_NONE,
30511                FPU_ARCH_VFP_V2),
30512   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
30513                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30514                FPU_NONE),
30515   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
30516                ARM_ARCH_NONE,
30517                FPU_ARCH_NEON_VFP_V4),
30518   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
30519                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
30520                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
30521   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
30522                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30523                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
30524   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
30525                ARM_ARCH_NONE,
30526                FPU_ARCH_NEON_VFP_V4),
30527   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
30528                ARM_ARCH_NONE,
30529                FPU_ARCH_NEON_VFP_V4),
30530   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
30531                ARM_ARCH_NONE,
30532                FPU_ARCH_NEON_VFP_V4),
30533   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
30534                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30535                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30536   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
30537                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30538                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30539   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
30540                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30541                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30542   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
30543                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30544                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30545   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
30546                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30547                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30548   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
30549               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30550               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30551   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
30552               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30553               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30554   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
30555                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30556                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30557   ARM_CPU_OPT ("cortex-a76",    "Cortex-A76",          ARM_ARCH_V8_2A,
30558                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30559                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30560   ARM_CPU_OPT ("ares",    "Ares",              ARM_ARCH_V8_2A,
30561                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30562                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30563   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
30564                ARM_ARCH_NONE,
30565                FPU_NONE),
30566   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
30567                ARM_ARCH_NONE,
30568                FPU_ARCH_VFP_V3D16),
30569   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
30570                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30571                FPU_NONE),
30572   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
30573                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30574                FPU_ARCH_VFP_V3D16),
30575   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
30576                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30577                FPU_ARCH_VFP_V3D16),
30578   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
30579               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30580               FPU_ARCH_NEON_VFP_ARMV8),
30581   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
30582                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30583                FPU_NONE),
30584   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
30585                ARM_ARCH_NONE,
30586                FPU_NONE),
30587   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
30588                ARM_ARCH_NONE,
30589                FPU_NONE),
30590   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
30591                ARM_ARCH_NONE,
30592                FPU_NONE),
30593   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
30594                ARM_ARCH_NONE,
30595                FPU_NONE),
30596   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
30597                ARM_ARCH_NONE,
30598                FPU_NONE),
30599   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
30600                ARM_ARCH_NONE,
30601                FPU_NONE),
30602   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
30603                ARM_ARCH_NONE,
30604                FPU_NONE),
30605   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
30606                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30607                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30608   ARM_CPU_OPT ("neoverse-n1",    "Neoverse N1",        ARM_ARCH_V8_2A,
30609                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30610                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30611   /* ??? XSCALE is really an architecture.  */
30612   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
30613                ARM_ARCH_NONE,
30614                FPU_ARCH_VFP_V2),
30615
30616   /* ??? iwmmxt is not a processor.  */
30617   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
30618                ARM_ARCH_NONE,
30619                FPU_ARCH_VFP_V2),
30620   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
30621                ARM_ARCH_NONE,
30622                FPU_ARCH_VFP_V2),
30623   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
30624                ARM_ARCH_NONE,
30625                FPU_ARCH_VFP_V2),
30626
30627   /* Maverick.  */
30628   ARM_CPU_OPT ("ep9312",          "ARM920T",
30629                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
30630                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
30631
30632   /* Marvell processors.  */
30633   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
30634                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30635                FPU_ARCH_VFP_V3D16),
30636   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
30637                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30638                FPU_ARCH_NEON_VFP_V4),
30639
30640   /* APM X-Gene family.  */
30641   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
30642                ARM_ARCH_NONE,
30643                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30644   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
30645                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30646                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30647
30648   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
30649 };
30650 #undef ARM_CPU_OPT
30651
30652 struct arm_ext_table
30653 {
30654   const char *            name;
30655   size_t                  name_len;
30656   const arm_feature_set   merge;
30657   const arm_feature_set   clear;
30658 };
30659
30660 struct arm_arch_option_table
30661 {
30662   const char *                  name;
30663   size_t                        name_len;
30664   const arm_feature_set         value;
30665   const arm_feature_set         default_fpu;
30666   const struct arm_ext_table *  ext_table;
30667 };
30668
30669 /* Used to add support for +E and +noE extension.  */
30670 #define ARM_EXT(E, M, C) { E, sizeof (E) - 1, M, C }
30671 /* Used to add support for a +E extension.  */
30672 #define ARM_ADD(E, M) { E, sizeof(E) - 1, M, ARM_ARCH_NONE }
30673 /* Used to add support for a +noE extension.  */
30674 #define ARM_REMOVE(E, C) { E, sizeof(E) -1, ARM_ARCH_NONE, C }
30675
30676 #define ALL_FP ARM_FEATURE (0, ARM_EXT2_FP16_INST | ARM_EXT2_FP16_FML, \
30677                             ~0 & ~FPU_ENDIAN_PURE)
30678
30679 static const struct arm_ext_table armv5te_ext_table[] =
30680 {
30681   ARM_EXT ("fp", FPU_ARCH_VFP_V2, ALL_FP),
30682   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30683 };
30684
30685 static const struct arm_ext_table armv7_ext_table[] =
30686 {
30687   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30688   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30689 };
30690
30691 static const struct arm_ext_table armv7ve_ext_table[] =
30692 {
30693   ARM_EXT ("fp", FPU_ARCH_VFP_V4D16, ALL_FP),
30694   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16),
30695   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
30696   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30697   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
30698   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),  /* Alias for +fp.  */
30699   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
30700
30701   ARM_EXT ("simd", FPU_ARCH_NEON_VFP_V4,
30702            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
30703
30704   /* Aliases for +simd.  */
30705   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
30706
30707   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30708   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30709   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
30710
30711   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30712 };
30713
30714 static const struct arm_ext_table armv7a_ext_table[] =
30715 {
30716   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30717   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
30718   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
30719   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30720   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
30721   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),
30722   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
30723
30724   ARM_EXT ("simd", FPU_ARCH_VFP_V3_PLUS_NEON_V1,
30725            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
30726
30727   /* Aliases for +simd.  */
30728   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30729   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30730
30731   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
30732   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
30733
30734   ARM_ADD ("mp", ARM_FEATURE_CORE_LOW (ARM_EXT_MP)),
30735   ARM_ADD ("sec", ARM_FEATURE_CORE_LOW (ARM_EXT_SEC)),
30736   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30737 };
30738
30739 static const struct arm_ext_table armv7r_ext_table[] =
30740 {
30741   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V3xD),
30742   ARM_ADD ("vfpv3xd", FPU_ARCH_VFP_V3xD), /* Alias for +fp.sp.  */
30743   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30744   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
30745   ARM_ADD ("vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16),
30746   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30747   ARM_EXT ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
30748            ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV)),
30749   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30750 };
30751
30752 static const struct arm_ext_table armv7em_ext_table[] =
30753 {
30754   ARM_EXT ("fp", FPU_ARCH_VFP_V4_SP_D16, ALL_FP),
30755   /* Alias for +fp, used to be known as fpv4-sp-d16.  */
30756   ARM_ADD ("vfpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16),
30757   ARM_ADD ("fpv5", FPU_ARCH_VFP_V5_SP_D16),
30758   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
30759   ARM_ADD ("fpv5-d16", FPU_ARCH_VFP_V5D16),
30760   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30761 };
30762
30763 static const struct arm_ext_table armv8a_ext_table[] =
30764 {
30765   ARM_ADD ("crc", ARCH_CRC_ARMV8),
30766   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
30767   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30768            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30769
30770   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30771      should use the +simd option to turn on FP.  */
30772   ARM_REMOVE ("fp", ALL_FP),
30773   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30774   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30775   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30776 };
30777
30778
30779 static const struct arm_ext_table armv81a_ext_table[] =
30780 {
30781   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
30782   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
30783            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30784
30785   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30786      should use the +simd option to turn on FP.  */
30787   ARM_REMOVE ("fp", ALL_FP),
30788   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30789   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30790   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30791 };
30792
30793 static const struct arm_ext_table armv82a_ext_table[] =
30794 {
30795   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
30796   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_2_FP16),
30797   ARM_ADD ("fp16fml", FPU_ARCH_NEON_VFP_ARMV8_2_FP16FML),
30798   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
30799            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30800   ARM_ADD ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30801
30802   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30803      should use the +simd option to turn on FP.  */
30804   ARM_REMOVE ("fp", ALL_FP),
30805   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30806   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30807   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30808 };
30809
30810 static const struct arm_ext_table armv84a_ext_table[] =
30811 {
30812   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30813   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
30814   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
30815            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30816
30817   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30818      should use the +simd option to turn on FP.  */
30819   ARM_REMOVE ("fp", ALL_FP),
30820   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30821   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30822   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30823 };
30824
30825 static const struct arm_ext_table armv85a_ext_table[] =
30826 {
30827   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30828   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
30829   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
30830            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30831
30832   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30833      should use the +simd option to turn on FP.  */
30834   ARM_REMOVE ("fp", ALL_FP),
30835   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30836 };
30837
30838 static const struct arm_ext_table armv8m_main_ext_table[] =
30839 {
30840   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30841                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
30842   ARM_EXT ("fp", FPU_ARCH_VFP_V5_SP_D16, ALL_FP),
30843   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
30844   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30845 };
30846
30847 static const struct arm_ext_table armv8_1m_main_ext_table[] =
30848 {
30849   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30850                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
30851   ARM_EXT ("fp",
30852            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30853                         FPU_VFP_V5_SP_D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA),
30854            ALL_FP),
30855   ARM_ADD ("fp.dp",
30856            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30857                         FPU_VFP_V5D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
30858   ARM_EXT ("mve", ARM_FEATURE_COPROC (FPU_MVE),
30859            ARM_FEATURE_COPROC (FPU_MVE | FPU_MVE_FP)),
30860   ARM_ADD ("mve.fp",
30861            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30862                         FPU_MVE | FPU_MVE_FP | FPU_VFP_V5_SP_D16 |
30863                         FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
30864   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30865 };
30866
30867 static const struct arm_ext_table armv8r_ext_table[] =
30868 {
30869   ARM_ADD ("crc", ARCH_CRC_ARMV8),
30870   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
30871   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30872            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30873   ARM_REMOVE ("fp", ALL_FP),
30874   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V5_SP_D16),
30875   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30876 };
30877
30878 /* This list should, at a minimum, contain all the architecture names
30879    recognized by GCC.  */
30880 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF, NULL }
30881 #define ARM_ARCH_OPT2(N, V, DF, ext) \
30882   { N, sizeof (N) - 1, V, DF, ext##_ext_table }
30883
30884 static const struct arm_arch_option_table arm_archs[] =
30885 {
30886   ARM_ARCH_OPT ("all",            ARM_ANY,              FPU_ARCH_FPA),
30887   ARM_ARCH_OPT ("armv1",          ARM_ARCH_V1,          FPU_ARCH_FPA),
30888   ARM_ARCH_OPT ("armv2",          ARM_ARCH_V2,          FPU_ARCH_FPA),
30889   ARM_ARCH_OPT ("armv2a",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
30890   ARM_ARCH_OPT ("armv2s",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
30891   ARM_ARCH_OPT ("armv3",          ARM_ARCH_V3,          FPU_ARCH_FPA),
30892   ARM_ARCH_OPT ("armv3m",         ARM_ARCH_V3M,         FPU_ARCH_FPA),
30893   ARM_ARCH_OPT ("armv4",          ARM_ARCH_V4,          FPU_ARCH_FPA),
30894   ARM_ARCH_OPT ("armv4xm",        ARM_ARCH_V4xM,        FPU_ARCH_FPA),
30895   ARM_ARCH_OPT ("armv4t",         ARM_ARCH_V4T,         FPU_ARCH_FPA),
30896   ARM_ARCH_OPT ("armv4txm",       ARM_ARCH_V4TxM,       FPU_ARCH_FPA),
30897   ARM_ARCH_OPT ("armv5",          ARM_ARCH_V5,          FPU_ARCH_VFP),
30898   ARM_ARCH_OPT ("armv5t",         ARM_ARCH_V5T,         FPU_ARCH_VFP),
30899   ARM_ARCH_OPT ("armv5txm",       ARM_ARCH_V5TxM,       FPU_ARCH_VFP),
30900   ARM_ARCH_OPT2 ("armv5te",       ARM_ARCH_V5TE,        FPU_ARCH_VFP,   armv5te),
30901   ARM_ARCH_OPT2 ("armv5texp",     ARM_ARCH_V5TExP,      FPU_ARCH_VFP, armv5te),
30902   ARM_ARCH_OPT2 ("armv5tej",      ARM_ARCH_V5TEJ,       FPU_ARCH_VFP,   armv5te),
30903   ARM_ARCH_OPT2 ("armv6",         ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
30904   ARM_ARCH_OPT2 ("armv6j",        ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
30905   ARM_ARCH_OPT2 ("armv6k",        ARM_ARCH_V6K,         FPU_ARCH_VFP,   armv5te),
30906   ARM_ARCH_OPT2 ("armv6z",        ARM_ARCH_V6Z,         FPU_ARCH_VFP,   armv5te),
30907   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
30908      kept to preserve existing behaviour.  */
30909   ARM_ARCH_OPT2 ("armv6kz",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
30910   ARM_ARCH_OPT2 ("armv6zk",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
30911   ARM_ARCH_OPT2 ("armv6t2",       ARM_ARCH_V6T2,        FPU_ARCH_VFP,   armv5te),
30912   ARM_ARCH_OPT2 ("armv6kt2",      ARM_ARCH_V6KT2,       FPU_ARCH_VFP,   armv5te),
30913   ARM_ARCH_OPT2 ("armv6zt2",      ARM_ARCH_V6ZT2,       FPU_ARCH_VFP,   armv5te),
30914   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
30915      kept to preserve existing behaviour.  */
30916   ARM_ARCH_OPT2 ("armv6kzt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
30917   ARM_ARCH_OPT2 ("armv6zkt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
30918   ARM_ARCH_OPT ("armv6-m",        ARM_ARCH_V6M,         FPU_ARCH_VFP),
30919   ARM_ARCH_OPT ("armv6s-m",       ARM_ARCH_V6SM,        FPU_ARCH_VFP),
30920   ARM_ARCH_OPT2 ("armv7",         ARM_ARCH_V7,          FPU_ARCH_VFP, armv7),
30921   /* The official spelling of the ARMv7 profile variants is the dashed form.
30922      Accept the non-dashed form for compatibility with old toolchains.  */
30923   ARM_ARCH_OPT2 ("armv7a",        ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
30924   ARM_ARCH_OPT2 ("armv7ve",       ARM_ARCH_V7VE,        FPU_ARCH_VFP, armv7ve),
30925   ARM_ARCH_OPT2 ("armv7r",        ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
30926   ARM_ARCH_OPT ("armv7m",         ARM_ARCH_V7M,         FPU_ARCH_VFP),
30927   ARM_ARCH_OPT2 ("armv7-a",       ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
30928   ARM_ARCH_OPT2 ("armv7-r",       ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
30929   ARM_ARCH_OPT ("armv7-m",        ARM_ARCH_V7M,         FPU_ARCH_VFP),
30930   ARM_ARCH_OPT2 ("armv7e-m",      ARM_ARCH_V7EM,        FPU_ARCH_VFP, armv7em),
30931   ARM_ARCH_OPT ("armv8-m.base",   ARM_ARCH_V8M_BASE,    FPU_ARCH_VFP),
30932   ARM_ARCH_OPT2 ("armv8-m.main",  ARM_ARCH_V8M_MAIN,    FPU_ARCH_VFP,
30933                  armv8m_main),
30934   ARM_ARCH_OPT2 ("armv8.1-m.main", ARM_ARCH_V8_1M_MAIN, FPU_ARCH_VFP,
30935                  armv8_1m_main),
30936   ARM_ARCH_OPT2 ("armv8-a",       ARM_ARCH_V8A,         FPU_ARCH_VFP, armv8a),
30937   ARM_ARCH_OPT2 ("armv8.1-a",     ARM_ARCH_V8_1A,       FPU_ARCH_VFP, armv81a),
30938   ARM_ARCH_OPT2 ("armv8.2-a",     ARM_ARCH_V8_2A,       FPU_ARCH_VFP, armv82a),
30939   ARM_ARCH_OPT2 ("armv8.3-a",     ARM_ARCH_V8_3A,       FPU_ARCH_VFP, armv82a),
30940   ARM_ARCH_OPT2 ("armv8-r",       ARM_ARCH_V8R,         FPU_ARCH_VFP, armv8r),
30941   ARM_ARCH_OPT2 ("armv8.4-a",     ARM_ARCH_V8_4A,       FPU_ARCH_VFP, armv84a),
30942   ARM_ARCH_OPT2 ("armv8.5-a",     ARM_ARCH_V8_5A,       FPU_ARCH_VFP, armv85a),
30943   ARM_ARCH_OPT ("xscale",         ARM_ARCH_XSCALE,      FPU_ARCH_VFP),
30944   ARM_ARCH_OPT ("iwmmxt",         ARM_ARCH_IWMMXT,      FPU_ARCH_VFP),
30945   ARM_ARCH_OPT ("iwmmxt2",        ARM_ARCH_IWMMXT2,     FPU_ARCH_VFP),
30946   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
30947 };
30948 #undef ARM_ARCH_OPT
30949
30950 /* ISA extensions in the co-processor and main instruction set space.  */
30951
30952 struct arm_option_extension_value_table
30953 {
30954   const char *           name;
30955   size_t                 name_len;
30956   const arm_feature_set  merge_value;
30957   const arm_feature_set  clear_value;
30958   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
30959      indicates that an extension is available for all architectures while
30960      ARM_ANY marks an empty entry.  */
30961   const arm_feature_set  allowed_archs[2];
30962 };
30963
30964 /* The following table must be in alphabetical order with a NULL last entry.  */
30965
30966 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
30967 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
30968
30969 /* DEPRECATED: Refrain from using this table to add any new extensions, instead
30970    use the context sensitive approach using arm_ext_table's.  */
30971 static const struct arm_option_extension_value_table arm_extensions[] =
30972 {
30973   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30974                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
30975   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30976                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
30977                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
30978   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
30979                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
30980                           ARM_ARCH_V8_2A),
30981   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30982                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30983                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
30984   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
30985                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
30986   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30987                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30988                         ARM_ARCH_V8_2A),
30989   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
30990                                                   | ARM_EXT2_FP16_FML),
30991                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
30992                                                   | ARM_EXT2_FP16_FML),
30993                            ARM_ARCH_V8_2A),
30994   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
30995                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
30996                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
30997                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
30998   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
30999      Thumb divide instruction.  Due to this having the same name as the
31000      previous entry, this will be ignored when doing command-line parsing and
31001      only considered by build attribute selection code.  */
31002   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
31003                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
31004                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
31005   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
31006                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
31007   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
31008                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
31009   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
31010                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
31011   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
31012                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
31013                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
31014                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
31015   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
31016                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
31017                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
31018   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
31019                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
31020                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31021   ARM_EXT_OPT ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
31022                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
31023                         ARM_ARCH_V8A),
31024   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
31025                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
31026                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31027   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
31028                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
31029                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31030   ARM_EXT_OPT ("sb",    ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
31031                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
31032                         ARM_ARCH_V8A),
31033   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
31034                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
31035                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
31036                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
31037   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
31038                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
31039                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31040   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
31041                                      | ARM_EXT_DIV),
31042                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
31043                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
31044   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
31045                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
31046   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
31047 };
31048 #undef ARM_EXT_OPT
31049
31050 /* ISA floating-point and Advanced SIMD extensions.  */
31051 struct arm_option_fpu_value_table
31052 {
31053   const char *           name;
31054   const arm_feature_set  value;
31055 };
31056
31057 /* This list should, at a minimum, contain all the fpu names
31058    recognized by GCC.  */
31059 static const struct arm_option_fpu_value_table arm_fpus[] =
31060 {
31061   {"softfpa",           FPU_NONE},
31062   {"fpe",               FPU_ARCH_FPE},
31063   {"fpe2",              FPU_ARCH_FPE},
31064   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
31065   {"fpa",               FPU_ARCH_FPA},
31066   {"fpa10",             FPU_ARCH_FPA},
31067   {"fpa11",             FPU_ARCH_FPA},
31068   {"arm7500fe",         FPU_ARCH_FPA},
31069   {"softvfp",           FPU_ARCH_VFP},
31070   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
31071   {"vfp",               FPU_ARCH_VFP_V2},
31072   {"vfp9",              FPU_ARCH_VFP_V2},
31073   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
31074   {"vfp10",             FPU_ARCH_VFP_V2},
31075   {"vfp10-r0",          FPU_ARCH_VFP_V1},
31076   {"vfpxd",             FPU_ARCH_VFP_V1xD},
31077   {"vfpv2",             FPU_ARCH_VFP_V2},
31078   {"vfpv3",             FPU_ARCH_VFP_V3},
31079   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
31080   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
31081   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
31082   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
31083   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
31084   {"arm1020t",          FPU_ARCH_VFP_V1},
31085   {"arm1020e",          FPU_ARCH_VFP_V2},
31086   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
31087   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
31088   {"maverick",          FPU_ARCH_MAVERICK},
31089   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
31090   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
31091   {"neon-fp16",         FPU_ARCH_NEON_FP16},
31092   {"vfpv4",             FPU_ARCH_VFP_V4},
31093   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
31094   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
31095   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
31096   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
31097   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
31098   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
31099   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
31100   {"crypto-neon-fp-armv8",
31101                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
31102   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
31103   {"crypto-neon-fp-armv8.1",
31104                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
31105   {NULL,                ARM_ARCH_NONE}
31106 };
31107
31108 struct arm_option_value_table
31109 {
31110   const char *name;
31111   long value;
31112 };
31113
31114 static const struct arm_option_value_table arm_float_abis[] =
31115 {
31116   {"hard",      ARM_FLOAT_ABI_HARD},
31117   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
31118   {"soft",      ARM_FLOAT_ABI_SOFT},
31119   {NULL,        0}
31120 };
31121
31122 #ifdef OBJ_ELF
31123 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
31124 static const struct arm_option_value_table arm_eabis[] =
31125 {
31126   {"gnu",       EF_ARM_EABI_UNKNOWN},
31127   {"4",         EF_ARM_EABI_VER4},
31128   {"5",         EF_ARM_EABI_VER5},
31129   {NULL,        0}
31130 };
31131 #endif
31132
31133 struct arm_long_option_table
31134 {
31135   const char * option;                  /* Substring to match.  */
31136   const char * help;                    /* Help information.  */
31137   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
31138   const char * deprecated;              /* If non-null, print this message.  */
31139 };
31140
31141 static bfd_boolean
31142 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
31143                      arm_feature_set *ext_set,
31144                      const struct arm_ext_table *ext_table)
31145 {
31146   /* We insist on extensions being specified in alphabetical order, and with
31147      extensions being added before being removed.  We achieve this by having
31148      the global ARM_EXTENSIONS table in alphabetical order, and using the
31149      ADDING_VALUE variable to indicate whether we are adding an extension (1)
31150      or removing it (0) and only allowing it to change in the order
31151      -1 -> 1 -> 0.  */
31152   const struct arm_option_extension_value_table * opt = NULL;
31153   const arm_feature_set arm_any = ARM_ANY;
31154   int adding_value = -1;
31155
31156   while (str != NULL && *str != 0)
31157     {
31158       const char *ext;
31159       size_t len;
31160
31161       if (*str != '+')
31162         {
31163           as_bad (_("invalid architectural extension"));
31164           return FALSE;
31165         }
31166
31167       str++;
31168       ext = strchr (str, '+');
31169
31170       if (ext != NULL)
31171         len = ext - str;
31172       else
31173         len = strlen (str);
31174
31175       if (len >= 2 && strncmp (str, "no", 2) == 0)
31176         {
31177           if (adding_value != 0)
31178             {
31179               adding_value = 0;
31180               opt = arm_extensions;
31181             }
31182
31183           len -= 2;
31184           str += 2;
31185         }
31186       else if (len > 0)
31187         {
31188           if (adding_value == -1)
31189             {
31190               adding_value = 1;
31191               opt = arm_extensions;
31192             }
31193           else if (adding_value != 1)
31194             {
31195               as_bad (_("must specify extensions to add before specifying "
31196                         "those to remove"));
31197               return FALSE;
31198             }
31199         }
31200
31201       if (len == 0)
31202         {
31203           as_bad (_("missing architectural extension"));
31204           return FALSE;
31205         }
31206
31207       gas_assert (adding_value != -1);
31208       gas_assert (opt != NULL);
31209
31210       if (ext_table != NULL)
31211         {
31212           const struct arm_ext_table * ext_opt = ext_table;
31213           bfd_boolean found = FALSE;
31214           for (; ext_opt->name != NULL; ext_opt++)
31215             if (ext_opt->name_len == len
31216                 && strncmp (ext_opt->name, str, len) == 0)
31217               {
31218                 if (adding_value)
31219                   {
31220                     if (ARM_FEATURE_ZERO (ext_opt->merge))
31221                         /* TODO: Option not supported.  When we remove the
31222                            legacy table this case should error out.  */
31223                         continue;
31224
31225                     ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, ext_opt->merge);
31226                   }
31227                 else
31228                   {
31229                     if (ARM_FEATURE_ZERO (ext_opt->clear))
31230                         /* TODO: Option not supported.  When we remove the
31231                            legacy table this case should error out.  */
31232                         continue;
31233                     ARM_CLEAR_FEATURE (*ext_set, *ext_set, ext_opt->clear);
31234                   }
31235                 found = TRUE;
31236                 break;
31237               }
31238           if (found)
31239             {
31240               str = ext;
31241               continue;
31242             }
31243         }
31244
31245       /* Scan over the options table trying to find an exact match. */
31246       for (; opt->name != NULL; opt++)
31247         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31248           {
31249             int i, nb_allowed_archs =
31250               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
31251             /* Check we can apply the extension to this architecture.  */
31252             for (i = 0; i < nb_allowed_archs; i++)
31253               {
31254                 /* Empty entry.  */
31255                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
31256                   continue;
31257                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
31258                   break;
31259               }
31260             if (i == nb_allowed_archs)
31261               {
31262                 as_bad (_("extension does not apply to the base architecture"));
31263                 return FALSE;
31264               }
31265
31266             /* Add or remove the extension.  */
31267             if (adding_value)
31268               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
31269             else
31270               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
31271
31272             /* Allowing Thumb division instructions for ARMv7 in autodetection
31273                rely on this break so that duplicate extensions (extensions
31274                with the same name as a previous extension in the list) are not
31275                considered for command-line parsing.  */
31276             break;
31277           }
31278
31279       if (opt->name == NULL)
31280         {
31281           /* Did we fail to find an extension because it wasn't specified in
31282              alphabetical order, or because it does not exist?  */
31283
31284           for (opt = arm_extensions; opt->name != NULL; opt++)
31285             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31286               break;
31287
31288           if (opt->name == NULL)
31289             as_bad (_("unknown architectural extension `%s'"), str);
31290           else
31291             as_bad (_("architectural extensions must be specified in "
31292                       "alphabetical order"));
31293
31294           return FALSE;
31295         }
31296       else
31297         {
31298           /* We should skip the extension we've just matched the next time
31299              round.  */
31300           opt++;
31301         }
31302
31303       str = ext;
31304     };
31305
31306   return TRUE;
31307 }
31308
31309 static bfd_boolean
31310 arm_parse_fp16_opt (const char *str)
31311 {
31312   if (strcasecmp (str, "ieee") == 0)
31313     fp16_format = ARM_FP16_FORMAT_IEEE;
31314   else if (strcasecmp (str, "alternative") == 0)
31315     fp16_format = ARM_FP16_FORMAT_ALTERNATIVE;
31316   else
31317     {
31318       as_bad (_("unrecognised float16 format \"%s\""), str);
31319       return FALSE;
31320     }
31321
31322   return TRUE;
31323 }
31324
31325 static bfd_boolean
31326 arm_parse_cpu (const char *str)
31327 {
31328   const struct arm_cpu_option_table *opt;
31329   const char *ext = strchr (str, '+');
31330   size_t len;
31331
31332   if (ext != NULL)
31333     len = ext - str;
31334   else
31335     len = strlen (str);
31336
31337   if (len == 0)
31338     {
31339       as_bad (_("missing cpu name `%s'"), str);
31340       return FALSE;
31341     }
31342
31343   for (opt = arm_cpus; opt->name != NULL; opt++)
31344     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31345       {
31346         mcpu_cpu_opt = &opt->value;
31347         if (mcpu_ext_opt == NULL)
31348           mcpu_ext_opt = XNEW (arm_feature_set);
31349         *mcpu_ext_opt = opt->ext;
31350         mcpu_fpu_opt = &opt->default_fpu;
31351         if (opt->canonical_name)
31352           {
31353             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
31354             strcpy (selected_cpu_name, opt->canonical_name);
31355           }
31356         else
31357           {
31358             size_t i;
31359
31360             if (len >= sizeof selected_cpu_name)
31361               len = (sizeof selected_cpu_name) - 1;
31362
31363             for (i = 0; i < len; i++)
31364               selected_cpu_name[i] = TOUPPER (opt->name[i]);
31365             selected_cpu_name[i] = 0;
31366           }
31367
31368         if (ext != NULL)
31369           return arm_parse_extension (ext, mcpu_cpu_opt, mcpu_ext_opt, NULL);
31370
31371         return TRUE;
31372       }
31373
31374   as_bad (_("unknown cpu `%s'"), str);
31375   return FALSE;
31376 }
31377
31378 static bfd_boolean
31379 arm_parse_arch (const char *str)
31380 {
31381   const struct arm_arch_option_table *opt;
31382   const char *ext = strchr (str, '+');
31383   size_t len;
31384
31385   if (ext != NULL)
31386     len = ext - str;
31387   else
31388     len = strlen (str);
31389
31390   if (len == 0)
31391     {
31392       as_bad (_("missing architecture name `%s'"), str);
31393       return FALSE;
31394     }
31395
31396   for (opt = arm_archs; opt->name != NULL; opt++)
31397     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31398       {
31399         march_cpu_opt = &opt->value;
31400         if (march_ext_opt == NULL)
31401           march_ext_opt = XNEW (arm_feature_set);
31402         *march_ext_opt = arm_arch_none;
31403         march_fpu_opt = &opt->default_fpu;
31404         strcpy (selected_cpu_name, opt->name);
31405
31406         if (ext != NULL)
31407           return arm_parse_extension (ext, march_cpu_opt, march_ext_opt,
31408                                       opt->ext_table);
31409
31410         return TRUE;
31411       }
31412
31413   as_bad (_("unknown architecture `%s'\n"), str);
31414   return FALSE;
31415 }
31416
31417 static bfd_boolean
31418 arm_parse_fpu (const char * str)
31419 {
31420   const struct arm_option_fpu_value_table * opt;
31421
31422   for (opt = arm_fpus; opt->name != NULL; opt++)
31423     if (streq (opt->name, str))
31424       {
31425         mfpu_opt = &opt->value;
31426         return TRUE;
31427       }
31428
31429   as_bad (_("unknown floating point format `%s'\n"), str);
31430   return FALSE;
31431 }
31432
31433 static bfd_boolean
31434 arm_parse_float_abi (const char * str)
31435 {
31436   const struct arm_option_value_table * opt;
31437
31438   for (opt = arm_float_abis; opt->name != NULL; opt++)
31439     if (streq (opt->name, str))
31440       {
31441         mfloat_abi_opt = opt->value;
31442         return TRUE;
31443       }
31444
31445   as_bad (_("unknown floating point abi `%s'\n"), str);
31446   return FALSE;
31447 }
31448
31449 #ifdef OBJ_ELF
31450 static bfd_boolean
31451 arm_parse_eabi (const char * str)
31452 {
31453   const struct arm_option_value_table *opt;
31454
31455   for (opt = arm_eabis; opt->name != NULL; opt++)
31456     if (streq (opt->name, str))
31457       {
31458         meabi_flags = opt->value;
31459         return TRUE;
31460       }
31461   as_bad (_("unknown EABI `%s'\n"), str);
31462   return FALSE;
31463 }
31464 #endif
31465
31466 static bfd_boolean
31467 arm_parse_it_mode (const char * str)
31468 {
31469   bfd_boolean ret = TRUE;
31470
31471   if (streq ("arm", str))
31472     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
31473   else if (streq ("thumb", str))
31474     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
31475   else if (streq ("always", str))
31476     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
31477   else if (streq ("never", str))
31478     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
31479   else
31480     {
31481       as_bad (_("unknown implicit IT mode `%s', should be "\
31482                 "arm, thumb, always, or never."), str);
31483       ret = FALSE;
31484     }
31485
31486   return ret;
31487 }
31488
31489 static bfd_boolean
31490 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
31491 {
31492   codecomposer_syntax = TRUE;
31493   arm_comment_chars[0] = ';';
31494   arm_line_separator_chars[0] = 0;
31495   return TRUE;
31496 }
31497
31498 struct arm_long_option_table arm_long_opts[] =
31499 {
31500   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
31501    arm_parse_cpu, NULL},
31502   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
31503    arm_parse_arch, NULL},
31504   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
31505    arm_parse_fpu, NULL},
31506   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
31507    arm_parse_float_abi, NULL},
31508 #ifdef OBJ_ELF
31509   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
31510    arm_parse_eabi, NULL},
31511 #endif
31512   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
31513    arm_parse_it_mode, NULL},
31514   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
31515    arm_ccs_mode, NULL},
31516   {"mfp16-format=",
31517    N_("[ieee|alternative]\n\
31518                           set the encoding for half precision floating point "
31519                           "numbers to IEEE\n\
31520                           or Arm alternative format."),
31521    arm_parse_fp16_opt, NULL },
31522   {NULL, NULL, 0, NULL}
31523 };
31524
31525 int
31526 md_parse_option (int c, const char * arg)
31527 {
31528   struct arm_option_table *opt;
31529   const struct arm_legacy_option_table *fopt;
31530   struct arm_long_option_table *lopt;
31531
31532   switch (c)
31533     {
31534 #ifdef OPTION_EB
31535     case OPTION_EB:
31536       target_big_endian = 1;
31537       break;
31538 #endif
31539
31540 #ifdef OPTION_EL
31541     case OPTION_EL:
31542       target_big_endian = 0;
31543       break;
31544 #endif
31545
31546     case OPTION_FIX_V4BX:
31547       fix_v4bx = TRUE;
31548       break;
31549
31550 #ifdef OBJ_ELF
31551     case OPTION_FDPIC:
31552       arm_fdpic = TRUE;
31553       break;
31554 #endif /* OBJ_ELF */
31555
31556     case 'a':
31557       /* Listing option.  Just ignore these, we don't support additional
31558          ones.  */
31559       return 0;
31560
31561     default:
31562       for (opt = arm_opts; opt->option != NULL; opt++)
31563         {
31564           if (c == opt->option[0]
31565               && ((arg == NULL && opt->option[1] == 0)
31566                   || streq (arg, opt->option + 1)))
31567             {
31568               /* If the option is deprecated, tell the user.  */
31569               if (warn_on_deprecated && opt->deprecated != NULL)
31570                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
31571                            arg ? arg : "", _(opt->deprecated));
31572
31573               if (opt->var != NULL)
31574                 *opt->var = opt->value;
31575
31576               return 1;
31577             }
31578         }
31579
31580       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
31581         {
31582           if (c == fopt->option[0]
31583               && ((arg == NULL && fopt->option[1] == 0)
31584                   || streq (arg, fopt->option + 1)))
31585             {
31586               /* If the option is deprecated, tell the user.  */
31587               if (warn_on_deprecated && fopt->deprecated != NULL)
31588                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
31589                            arg ? arg : "", _(fopt->deprecated));
31590
31591               if (fopt->var != NULL)
31592                 *fopt->var = &fopt->value;
31593
31594               return 1;
31595             }
31596         }
31597
31598       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
31599         {
31600           /* These options are expected to have an argument.  */
31601           if (c == lopt->option[0]
31602               && arg != NULL
31603               && strncmp (arg, lopt->option + 1,
31604                           strlen (lopt->option + 1)) == 0)
31605             {
31606               /* If the option is deprecated, tell the user.  */
31607               if (warn_on_deprecated && lopt->deprecated != NULL)
31608                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
31609                            _(lopt->deprecated));
31610
31611               /* Call the sup-option parser.  */
31612               return lopt->func (arg + strlen (lopt->option) - 1);
31613             }
31614         }
31615
31616       return 0;
31617     }
31618
31619   return 1;
31620 }
31621
31622 void
31623 md_show_usage (FILE * fp)
31624 {
31625   struct arm_option_table *opt;
31626   struct arm_long_option_table *lopt;
31627
31628   fprintf (fp, _(" ARM-specific assembler options:\n"));
31629
31630   for (opt = arm_opts; opt->option != NULL; opt++)
31631     if (opt->help != NULL)
31632       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
31633
31634   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
31635     if (lopt->help != NULL)
31636       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
31637
31638 #ifdef OPTION_EB
31639   fprintf (fp, _("\
31640   -EB                     assemble code for a big-endian cpu\n"));
31641 #endif
31642
31643 #ifdef OPTION_EL
31644   fprintf (fp, _("\
31645   -EL                     assemble code for a little-endian cpu\n"));
31646 #endif
31647
31648   fprintf (fp, _("\
31649   --fix-v4bx              Allow BX in ARMv4 code\n"));
31650
31651 #ifdef OBJ_ELF
31652   fprintf (fp, _("\
31653   --fdpic                 generate an FDPIC object file\n"));
31654 #endif /* OBJ_ELF */
31655 }
31656
31657 #ifdef OBJ_ELF
31658
31659 typedef struct
31660 {
31661   int val;
31662   arm_feature_set flags;
31663 } cpu_arch_ver_table;
31664
31665 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
31666    chronologically for architectures, with an exception for ARMv6-M and
31667    ARMv6S-M due to legacy reasons.  No new architecture should have a
31668    special case.  This allows for build attribute selection results to be
31669    stable when new architectures are added.  */
31670 static const cpu_arch_ver_table cpu_arch_ver[] =
31671 {
31672     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V1},
31673     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2},
31674     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2S},
31675     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3},
31676     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3M},
31677     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4xM},
31678     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4},
31679     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4TxM},
31680     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4T},
31681     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5xM},
31682     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5},
31683     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5TxM},
31684     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5T},
31685     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TExP},
31686     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TE},
31687     {TAG_CPU_ARCH_V5TEJ,      ARM_ARCH_V5TEJ},
31688     {TAG_CPU_ARCH_V6,         ARM_ARCH_V6},
31689     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6Z},
31690     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6KZ},
31691     {TAG_CPU_ARCH_V6K,        ARM_ARCH_V6K},
31692     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6T2},
31693     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KT2},
31694     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6ZT2},
31695     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KZT2},
31696
31697     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
31698        always selected build attributes to match those of ARMv6-M
31699        (resp. ARMv6S-M).  However, due to these architectures being a strict
31700        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
31701        would be selected when fully respecting chronology of architectures.
31702        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
31703        move them before ARMv7 architectures.  */
31704     {TAG_CPU_ARCH_V6_M,       ARM_ARCH_V6M},
31705     {TAG_CPU_ARCH_V6S_M,      ARM_ARCH_V6SM},
31706
31707     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7},
31708     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7A},
31709     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7R},
31710     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7M},
31711     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7VE},
31712     {TAG_CPU_ARCH_V7E_M,      ARM_ARCH_V7EM},
31713     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8A},
31714     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_1A},
31715     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_2A},
31716     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_3A},
31717     {TAG_CPU_ARCH_V8M_BASE,   ARM_ARCH_V8M_BASE},
31718     {TAG_CPU_ARCH_V8M_MAIN,   ARM_ARCH_V8M_MAIN},
31719     {TAG_CPU_ARCH_V8R,        ARM_ARCH_V8R},
31720     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_4A},
31721     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_5A},
31722     {TAG_CPU_ARCH_V8_1M_MAIN, ARM_ARCH_V8_1M_MAIN},
31723     {-1,                      ARM_ARCH_NONE}
31724 };
31725
31726 /* Set an attribute if it has not already been set by the user.  */
31727
31728 static void
31729 aeabi_set_attribute_int (int tag, int value)
31730 {
31731   if (tag < 1
31732       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
31733       || !attributes_set_explicitly[tag])
31734     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
31735 }
31736
31737 static void
31738 aeabi_set_attribute_string (int tag, const char *value)
31739 {
31740   if (tag < 1
31741       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
31742       || !attributes_set_explicitly[tag])
31743     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
31744 }
31745
31746 /* Return whether features in the *NEEDED feature set are available via
31747    extensions for the architecture whose feature set is *ARCH_FSET.  */
31748
31749 static bfd_boolean
31750 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
31751                             const arm_feature_set *needed)
31752 {
31753   int i, nb_allowed_archs;
31754   arm_feature_set ext_fset;
31755   const struct arm_option_extension_value_table *opt;
31756
31757   ext_fset = arm_arch_none;
31758   for (opt = arm_extensions; opt->name != NULL; opt++)
31759     {
31760       /* Extension does not provide any feature we need.  */
31761       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
31762         continue;
31763
31764       nb_allowed_archs =
31765         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
31766       for (i = 0; i < nb_allowed_archs; i++)
31767         {
31768           /* Empty entry.  */
31769           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
31770             break;
31771
31772           /* Extension is available, add it.  */
31773           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
31774             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
31775         }
31776     }
31777
31778   /* Can we enable all features in *needed?  */
31779   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
31780 }
31781
31782 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
31783    a given architecture feature set *ARCH_EXT_FSET including extension feature
31784    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
31785    - if true, check for an exact match of the architecture modulo extensions;
31786    - otherwise, select build attribute value of the first superset
31787      architecture released so that results remains stable when new architectures
31788      are added.
31789    For -march/-mcpu=all the build attribute value of the most featureful
31790    architecture is returned.  Tag_CPU_arch_profile result is returned in
31791    PROFILE.  */
31792
31793 static int
31794 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
31795                               const arm_feature_set *ext_fset,
31796                               char *profile, int exact_match)
31797 {
31798   arm_feature_set arch_fset;
31799   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
31800
31801   /* Select most featureful architecture with all its extensions if building
31802      for -march=all as the feature sets used to set build attributes.  */
31803   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
31804     {
31805       /* Force revisiting of decision for each new architecture.  */
31806       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8_1M_MAIN);
31807       *profile = 'A';
31808       return TAG_CPU_ARCH_V8;
31809     }
31810
31811   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
31812
31813   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
31814     {
31815       arm_feature_set known_arch_fset;
31816
31817       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
31818       if (exact_match)
31819         {
31820           /* Base architecture match user-specified architecture and
31821              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
31822           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
31823             {
31824               p_ver_ret = p_ver;
31825               goto found;
31826             }
31827           /* Base architecture match user-specified architecture only
31828              (eg. ARMv6-M in the same case as above).  Record it in case we
31829              find a match with above condition.  */
31830           else if (p_ver_ret == NULL
31831                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
31832             p_ver_ret = p_ver;
31833         }
31834       else
31835         {
31836
31837           /* Architecture has all features wanted.  */
31838           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
31839             {
31840               arm_feature_set added_fset;
31841
31842               /* Compute features added by this architecture over the one
31843                  recorded in p_ver_ret.  */
31844               if (p_ver_ret != NULL)
31845                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
31846                                    p_ver_ret->flags);
31847               /* First architecture that match incl. with extensions, or the
31848                  only difference in features over the recorded match is
31849                  features that were optional and are now mandatory.  */
31850               if (p_ver_ret == NULL
31851                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
31852                 {
31853                   p_ver_ret = p_ver;
31854                   goto found;
31855                 }
31856             }
31857           else if (p_ver_ret == NULL)
31858             {
31859               arm_feature_set needed_ext_fset;
31860
31861               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
31862
31863               /* Architecture has all features needed when using some
31864                  extensions.  Record it and continue searching in case there
31865                  exist an architecture providing all needed features without
31866                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
31867                  OS extension).  */
31868               if (have_ext_for_needed_feat_p (&known_arch_fset,
31869                                               &needed_ext_fset))
31870                 p_ver_ret = p_ver;
31871             }
31872         }
31873     }
31874
31875   if (p_ver_ret == NULL)
31876     return -1;
31877
31878 found:
31879   /* Tag_CPU_arch_profile.  */
31880   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
31881       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
31882       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
31883           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
31884     *profile = 'A';
31885   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
31886     *profile = 'R';
31887   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
31888     *profile = 'M';
31889   else
31890     *profile = '\0';
31891   return p_ver_ret->val;
31892 }
31893
31894 /* Set the public EABI object attributes.  */
31895
31896 static void
31897 aeabi_set_public_attributes (void)
31898 {
31899   char profile = '\0';
31900   int arch = -1;
31901   int virt_sec = 0;
31902   int fp16_optional = 0;
31903   int skip_exact_match = 0;
31904   arm_feature_set flags, flags_arch, flags_ext;
31905
31906   /* Autodetection mode, choose the architecture based the instructions
31907      actually used.  */
31908   if (no_cpu_selected ())
31909     {
31910       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
31911
31912       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
31913         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
31914
31915       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
31916         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
31917
31918       /* Code run during relaxation relies on selected_cpu being set.  */
31919       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
31920       flags_ext = arm_arch_none;
31921       ARM_CLEAR_FEATURE (selected_arch, flags_arch, flags_ext);
31922       selected_ext = flags_ext;
31923       selected_cpu = flags;
31924     }
31925   /* Otherwise, choose the architecture based on the capabilities of the
31926      requested cpu.  */
31927   else
31928     {
31929       ARM_MERGE_FEATURE_SETS (flags_arch, selected_arch, selected_ext);
31930       ARM_CLEAR_FEATURE (flags_arch, flags_arch, fpu_any);
31931       flags_ext = selected_ext;
31932       flags = selected_cpu;
31933     }
31934   ARM_MERGE_FEATURE_SETS (flags, flags, selected_fpu);
31935
31936   /* Allow the user to override the reported architecture.  */
31937   if (!ARM_FEATURE_ZERO (selected_object_arch))
31938     {
31939       ARM_CLEAR_FEATURE (flags_arch, selected_object_arch, fpu_any);
31940       flags_ext = arm_arch_none;
31941     }
31942   else
31943     skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
31944
31945   /* When this function is run again after relaxation has happened there is no
31946      way to determine whether an architecture or CPU was specified by the user:
31947      - selected_cpu is set above for relaxation to work;
31948      - march_cpu_opt is not set if only -mcpu or .cpu is used;
31949      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
31950      Therefore, if not in -march=all case we first try an exact match and fall
31951      back to autodetection.  */
31952   if (!skip_exact_match)
31953     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
31954   if (arch == -1)
31955     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
31956   if (arch == -1)
31957     as_bad (_("no architecture contains all the instructions used\n"));
31958
31959   /* Tag_CPU_name.  */
31960   if (selected_cpu_name[0])
31961     {
31962       char *q;
31963
31964       q = selected_cpu_name;
31965       if (strncmp (q, "armv", 4) == 0)
31966         {
31967           int i;
31968
31969           q += 4;
31970           for (i = 0; q[i]; i++)
31971             q[i] = TOUPPER (q[i]);
31972         }
31973       aeabi_set_attribute_string (Tag_CPU_name, q);
31974     }
31975
31976   /* Tag_CPU_arch.  */
31977   aeabi_set_attribute_int (Tag_CPU_arch, arch);
31978
31979   /* Tag_CPU_arch_profile.  */
31980   if (profile != '\0')
31981     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
31982
31983   /* Tag_DSP_extension.  */
31984   if (ARM_CPU_HAS_FEATURE (selected_ext, arm_ext_dsp))
31985     aeabi_set_attribute_int (Tag_DSP_extension, 1);
31986
31987   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
31988   /* Tag_ARM_ISA_use.  */
31989   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
31990       || ARM_FEATURE_ZERO (flags_arch))
31991     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
31992
31993   /* Tag_THUMB_ISA_use.  */
31994   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
31995       || ARM_FEATURE_ZERO (flags_arch))
31996     {
31997       int thumb_isa_use;
31998
31999       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
32000           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
32001         thumb_isa_use = 3;
32002       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
32003         thumb_isa_use = 2;
32004       else
32005         thumb_isa_use = 1;
32006       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
32007     }
32008
32009   /* Tag_VFP_arch.  */
32010   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
32011     aeabi_set_attribute_int (Tag_VFP_arch,
32012                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
32013                              ? 7 : 8);
32014   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
32015     aeabi_set_attribute_int (Tag_VFP_arch,
32016                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
32017                              ? 5 : 6);
32018   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
32019     {
32020       fp16_optional = 1;
32021       aeabi_set_attribute_int (Tag_VFP_arch, 3);
32022     }
32023   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
32024     {
32025       aeabi_set_attribute_int (Tag_VFP_arch, 4);
32026       fp16_optional = 1;
32027     }
32028   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
32029     aeabi_set_attribute_int (Tag_VFP_arch, 2);
32030   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
32031            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
32032     aeabi_set_attribute_int (Tag_VFP_arch, 1);
32033
32034   /* Tag_ABI_HardFP_use.  */
32035   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
32036       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
32037     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
32038
32039   /* Tag_WMMX_arch.  */
32040   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
32041     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
32042   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
32043     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
32044
32045   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
32046   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
32047     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
32048   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
32049     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
32050   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
32051     {
32052       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
32053         {
32054           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
32055         }
32056       else
32057         {
32058           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
32059           fp16_optional = 1;
32060         }
32061     }
32062
32063   if (ARM_CPU_HAS_FEATURE (flags, mve_fp_ext))
32064     aeabi_set_attribute_int (Tag_MVE_arch, 2);
32065   else if (ARM_CPU_HAS_FEATURE (flags, mve_ext))
32066     aeabi_set_attribute_int (Tag_MVE_arch, 1);
32067
32068   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
32069   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
32070     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
32071
32072   /* Tag_DIV_use.
32073
32074      We set Tag_DIV_use to two when integer divide instructions have been used
32075      in ARM state, or when Thumb integer divide instructions have been used,
32076      but we have no architecture profile set, nor have we any ARM instructions.
32077
32078      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
32079      by the base architecture.
32080
32081      For new architectures we will have to check these tests.  */
32082   gas_assert (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
32083   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
32084       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
32085     aeabi_set_attribute_int (Tag_DIV_use, 0);
32086   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
32087            || (profile == '\0'
32088                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
32089                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
32090     aeabi_set_attribute_int (Tag_DIV_use, 2);
32091
32092   /* Tag_MP_extension_use.  */
32093   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
32094     aeabi_set_attribute_int (Tag_MPextension_use, 1);
32095
32096   /* Tag Virtualization_use.  */
32097   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
32098     virt_sec |= 1;
32099   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
32100     virt_sec |= 2;
32101   if (virt_sec != 0)
32102     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
32103
32104   if (fp16_format != ARM_FP16_FORMAT_DEFAULT)
32105     aeabi_set_attribute_int (Tag_ABI_FP_16bit_format, fp16_format);
32106 }
32107
32108 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
32109    finished and free extension feature bits which will not be used anymore.  */
32110
32111 void
32112 arm_md_post_relax (void)
32113 {
32114   aeabi_set_public_attributes ();
32115   XDELETE (mcpu_ext_opt);
32116   mcpu_ext_opt = NULL;
32117   XDELETE (march_ext_opt);
32118   march_ext_opt = NULL;
32119 }
32120
32121 /* Add the default contents for the .ARM.attributes section.  */
32122
32123 void
32124 arm_md_end (void)
32125 {
32126   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
32127     return;
32128
32129   aeabi_set_public_attributes ();
32130 }
32131 #endif /* OBJ_ELF */
32132
32133 /* Parse a .cpu directive.  */
32134
32135 static void
32136 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
32137 {
32138   const struct arm_cpu_option_table *opt;
32139   char *name;
32140   char saved_char;
32141
32142   name = input_line_pointer;
32143   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32144     input_line_pointer++;
32145   saved_char = *input_line_pointer;
32146   *input_line_pointer = 0;
32147
32148   /* Skip the first "all" entry.  */
32149   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
32150     if (streq (opt->name, name))
32151       {
32152         selected_arch = opt->value;
32153         selected_ext = opt->ext;
32154         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
32155         if (opt->canonical_name)
32156           strcpy (selected_cpu_name, opt->canonical_name);
32157         else
32158           {
32159             int i;
32160             for (i = 0; opt->name[i]; i++)
32161               selected_cpu_name[i] = TOUPPER (opt->name[i]);
32162
32163             selected_cpu_name[i] = 0;
32164           }
32165         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32166
32167         *input_line_pointer = saved_char;
32168         demand_empty_rest_of_line ();
32169         return;
32170       }
32171   as_bad (_("unknown cpu `%s'"), name);
32172   *input_line_pointer = saved_char;
32173   ignore_rest_of_line ();
32174 }
32175
32176 /* Parse a .arch directive.  */
32177
32178 static void
32179 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
32180 {
32181   const struct arm_arch_option_table *opt;
32182   char saved_char;
32183   char *name;
32184
32185   name = input_line_pointer;
32186   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32187     input_line_pointer++;
32188   saved_char = *input_line_pointer;
32189   *input_line_pointer = 0;
32190
32191   /* Skip the first "all" entry.  */
32192   for (opt = arm_archs + 1; opt->name != NULL; opt++)
32193     if (streq (opt->name, name))
32194       {
32195         selected_arch = opt->value;
32196         selected_ext = arm_arch_none;
32197         selected_cpu = selected_arch;
32198         strcpy (selected_cpu_name, opt->name);
32199         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32200         *input_line_pointer = saved_char;
32201         demand_empty_rest_of_line ();
32202         return;
32203       }
32204
32205   as_bad (_("unknown architecture `%s'\n"), name);
32206   *input_line_pointer = saved_char;
32207   ignore_rest_of_line ();
32208 }
32209
32210 /* Parse a .object_arch directive.  */
32211
32212 static void
32213 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
32214 {
32215   const struct arm_arch_option_table *opt;
32216   char saved_char;
32217   char *name;
32218
32219   name = input_line_pointer;
32220   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32221     input_line_pointer++;
32222   saved_char = *input_line_pointer;
32223   *input_line_pointer = 0;
32224
32225   /* Skip the first "all" entry.  */
32226   for (opt = arm_archs + 1; opt->name != NULL; opt++)
32227     if (streq (opt->name, name))
32228       {
32229         selected_object_arch = opt->value;
32230         *input_line_pointer = saved_char;
32231         demand_empty_rest_of_line ();
32232         return;
32233       }
32234
32235   as_bad (_("unknown architecture `%s'\n"), name);
32236   *input_line_pointer = saved_char;
32237   ignore_rest_of_line ();
32238 }
32239
32240 /* Parse a .arch_extension directive.  */
32241
32242 static void
32243 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
32244 {
32245   const struct arm_option_extension_value_table *opt;
32246   char saved_char;
32247   char *name;
32248   int adding_value = 1;
32249
32250   name = input_line_pointer;
32251   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32252     input_line_pointer++;
32253   saved_char = *input_line_pointer;
32254   *input_line_pointer = 0;
32255
32256   if (strlen (name) >= 2
32257       && strncmp (name, "no", 2) == 0)
32258     {
32259       adding_value = 0;
32260       name += 2;
32261     }
32262
32263   for (opt = arm_extensions; opt->name != NULL; opt++)
32264     if (streq (opt->name, name))
32265       {
32266         int i, nb_allowed_archs =
32267           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
32268         for (i = 0; i < nb_allowed_archs; i++)
32269           {
32270             /* Empty entry.  */
32271             if (ARM_CPU_IS_ANY (opt->allowed_archs[i]))
32272               continue;
32273             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], selected_arch))
32274               break;
32275           }
32276
32277         if (i == nb_allowed_archs)
32278           {
32279             as_bad (_("architectural extension `%s' is not allowed for the "
32280                       "current base architecture"), name);
32281             break;
32282           }
32283
32284         if (adding_value)
32285           ARM_MERGE_FEATURE_SETS (selected_ext, selected_ext,
32286                                   opt->merge_value);
32287         else
32288           ARM_CLEAR_FEATURE (selected_ext, selected_ext, opt->clear_value);
32289
32290         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
32291         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32292         *input_line_pointer = saved_char;
32293         demand_empty_rest_of_line ();
32294         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
32295            on this return so that duplicate extensions (extensions with the
32296            same name as a previous extension in the list) are not considered
32297            for command-line parsing.  */
32298         return;
32299       }
32300
32301   if (opt->name == NULL)
32302     as_bad (_("unknown architecture extension `%s'\n"), name);
32303
32304   *input_line_pointer = saved_char;
32305   ignore_rest_of_line ();
32306 }
32307
32308 /* Parse a .fpu directive.  */
32309
32310 static void
32311 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
32312 {
32313   const struct arm_option_fpu_value_table *opt;
32314   char saved_char;
32315   char *name;
32316
32317   name = input_line_pointer;
32318   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32319     input_line_pointer++;
32320   saved_char = *input_line_pointer;
32321   *input_line_pointer = 0;
32322
32323   for (opt = arm_fpus; opt->name != NULL; opt++)
32324     if (streq (opt->name, name))
32325       {
32326         selected_fpu = opt->value;
32327 #ifndef CPU_DEFAULT
32328         if (no_cpu_selected ())
32329           ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
32330         else
32331 #endif
32332           ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32333         *input_line_pointer = saved_char;
32334         demand_empty_rest_of_line ();
32335         return;
32336       }
32337
32338   as_bad (_("unknown floating point format `%s'\n"), name);
32339   *input_line_pointer = saved_char;
32340   ignore_rest_of_line ();
32341 }
32342
32343 /* Copy symbol information.  */
32344
32345 void
32346 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
32347 {
32348   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
32349 }
32350
32351 #ifdef OBJ_ELF
32352 /* Given a symbolic attribute NAME, return the proper integer value.
32353    Returns -1 if the attribute is not known.  */
32354
32355 int
32356 arm_convert_symbolic_attribute (const char *name)
32357 {
32358   static const struct
32359   {
32360     const char * name;
32361     const int    tag;
32362   }
32363   attribute_table[] =
32364     {
32365       /* When you modify this table you should
32366          also modify the list in doc/c-arm.texi.  */
32367 #define T(tag) {#tag, tag}
32368       T (Tag_CPU_raw_name),
32369       T (Tag_CPU_name),
32370       T (Tag_CPU_arch),
32371       T (Tag_CPU_arch_profile),
32372       T (Tag_ARM_ISA_use),
32373       T (Tag_THUMB_ISA_use),
32374       T (Tag_FP_arch),
32375       T (Tag_VFP_arch),
32376       T (Tag_WMMX_arch),
32377       T (Tag_Advanced_SIMD_arch),
32378       T (Tag_PCS_config),
32379       T (Tag_ABI_PCS_R9_use),
32380       T (Tag_ABI_PCS_RW_data),
32381       T (Tag_ABI_PCS_RO_data),
32382       T (Tag_ABI_PCS_GOT_use),
32383       T (Tag_ABI_PCS_wchar_t),
32384       T (Tag_ABI_FP_rounding),
32385       T (Tag_ABI_FP_denormal),
32386       T (Tag_ABI_FP_exceptions),
32387       T (Tag_ABI_FP_user_exceptions),
32388       T (Tag_ABI_FP_number_model),
32389       T (Tag_ABI_align_needed),
32390       T (Tag_ABI_align8_needed),
32391       T (Tag_ABI_align_preserved),
32392       T (Tag_ABI_align8_preserved),
32393       T (Tag_ABI_enum_size),
32394       T (Tag_ABI_HardFP_use),
32395       T (Tag_ABI_VFP_args),
32396       T (Tag_ABI_WMMX_args),
32397       T (Tag_ABI_optimization_goals),
32398       T (Tag_ABI_FP_optimization_goals),
32399       T (Tag_compatibility),
32400       T (Tag_CPU_unaligned_access),
32401       T (Tag_FP_HP_extension),
32402       T (Tag_VFP_HP_extension),
32403       T (Tag_ABI_FP_16bit_format),
32404       T (Tag_MPextension_use),
32405       T (Tag_DIV_use),
32406       T (Tag_nodefaults),
32407       T (Tag_also_compatible_with),
32408       T (Tag_conformance),
32409       T (Tag_T2EE_use),
32410       T (Tag_Virtualization_use),
32411       T (Tag_DSP_extension),
32412       T (Tag_MVE_arch),
32413       /* We deliberately do not include Tag_MPextension_use_legacy.  */
32414 #undef T
32415     };
32416   unsigned int i;
32417
32418   if (name == NULL)
32419     return -1;
32420
32421   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
32422     if (streq (name, attribute_table[i].name))
32423       return attribute_table[i].tag;
32424
32425   return -1;
32426 }
32427
32428 /* Apply sym value for relocations only in the case that they are for
32429    local symbols in the same segment as the fixup and you have the
32430    respective architectural feature for blx and simple switches.  */
32431
32432 int
32433 arm_apply_sym_value (struct fix * fixP, segT this_seg)
32434 {
32435   if (fixP->fx_addsy
32436       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
32437       /* PR 17444: If the local symbol is in a different section then a reloc
32438          will always be generated for it, so applying the symbol value now
32439          will result in a double offset being stored in the relocation.  */
32440       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
32441       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
32442     {
32443       switch (fixP->fx_r_type)
32444         {
32445         case BFD_RELOC_ARM_PCREL_BLX:
32446         case BFD_RELOC_THUMB_PCREL_BRANCH23:
32447           if (ARM_IS_FUNC (fixP->fx_addsy))
32448             return 1;
32449           break;
32450
32451         case BFD_RELOC_ARM_PCREL_CALL:
32452         case BFD_RELOC_THUMB_PCREL_BLX:
32453           if (THUMB_IS_FUNC (fixP->fx_addsy))
32454             return 1;
32455           break;
32456
32457         default:
32458           break;
32459         }
32460
32461     }
32462   return 0;
32463 }
32464 #endif /* OBJ_ELF */