Modify the ARM encoding and decoding of SQRSHRL and UQRSHLL MVE instructions.
[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_I48_I64,   /*                 48 or 64 */
7054   OP_I63,       /*                 0 .. 63 */
7055   OP_I63s,      /*               -64 .. 63 */
7056   OP_I64,       /*                 1 .. 64 */
7057   OP_I64z,      /*                 0 .. 64 */
7058   OP_I255,      /*                 0 .. 255 */
7059
7060   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
7061   OP_I7b,       /*                             0 .. 7 */
7062   OP_I15b,      /*                             0 .. 15 */
7063   OP_I31b,      /*                             0 .. 31 */
7064
7065   OP_SH,        /* shifter operand */
7066   OP_SHG,       /* shifter operand with possible group relocation */
7067   OP_ADDR,      /* Memory address expression (any mode) */
7068   OP_ADDRMVE,   /* Memory address expression for MVE's VSTR/VLDR.  */
7069   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
7070   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
7071   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
7072   OP_EXP,       /* arbitrary expression */
7073   OP_EXPi,      /* same, with optional immediate prefix */
7074   OP_EXPr,      /* same, with optional relocation suffix */
7075   OP_EXPs,      /* same, with optional non-first operand relocation suffix */
7076   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
7077   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
7078   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
7079
7080   OP_CPSF,      /* CPS flags */
7081   OP_ENDI,      /* Endianness specifier */
7082   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
7083   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
7084   OP_COND,      /* conditional code */
7085   OP_TB,        /* Table branch.  */
7086
7087   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
7088
7089   OP_RRnpc_I0,  /* ARM register or literal 0 */
7090   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
7091   OP_RR_EXi,    /* ARM register or expression with imm prefix */
7092   OP_RF_IF,     /* FPA register or immediate */
7093   OP_RIWR_RIWC, /* iWMMXt R or C reg */
7094   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
7095
7096   /* Optional operands.  */
7097   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
7098   OP_oI31b,      /*                             0 .. 31 */
7099   OP_oI32b,      /*                             1 .. 32 */
7100   OP_oI32z,      /*                             0 .. 32 */
7101   OP_oIffffb,    /*                             0 .. 65535 */
7102   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
7103
7104   OP_oRR,        /* ARM register */
7105   OP_oLR,        /* ARM LR register */
7106   OP_oRRnpc,     /* ARM register, not the PC */
7107   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
7108   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
7109   OP_oRND,       /* Optional Neon double precision register */
7110   OP_oRNQ,       /* Optional Neon quad precision register */
7111   OP_oRNDQMQ,     /* Optional Neon double, quad or MVE vector register.  */
7112   OP_oRNDQ,      /* Optional Neon double or quad precision register */
7113   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
7114   OP_oRNSDQMQ,   /* Optional single, double or quad register or MVE vector
7115                     register.  */
7116   OP_oSHll,      /* LSL immediate */
7117   OP_oSHar,      /* ASR immediate */
7118   OP_oSHllar,    /* LSL or ASR immediate */
7119   OP_oROR,       /* ROR 0/8/16/24 */
7120   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
7121
7122   OP_oRMQRZ,    /* optional MVE vector or ARM register including ZR.  */
7123
7124   /* Some pre-defined mixed (ARM/THUMB) operands.  */
7125   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
7126   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
7127   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
7128
7129   OP_FIRST_OPTIONAL = OP_oI7b
7130 };
7131
7132 /* Generic instruction operand parser.  This does no encoding and no
7133    semantic validation; it merely squirrels values away in the inst
7134    structure.  Returns SUCCESS or FAIL depending on whether the
7135    specified grammar matched.  */
7136 static int
7137 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
7138 {
7139   unsigned const int *upat = pattern;
7140   char *backtrack_pos = 0;
7141   const char *backtrack_error = 0;
7142   int i, val = 0, backtrack_index = 0;
7143   enum arm_reg_type rtype;
7144   parse_operand_result result;
7145   unsigned int op_parse_code;
7146   bfd_boolean partial_match;
7147
7148 #define po_char_or_fail(chr)                    \
7149   do                                            \
7150     {                                           \
7151       if (skip_past_char (&str, chr) == FAIL)   \
7152         goto bad_args;                          \
7153     }                                           \
7154   while (0)
7155
7156 #define po_reg_or_fail(regtype)                                 \
7157   do                                                            \
7158     {                                                           \
7159       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7160                                  & inst.operands[i].vectype);   \
7161       if (val == FAIL)                                          \
7162         {                                                       \
7163           first_error (_(reg_expected_msgs[regtype]));          \
7164           goto failure;                                         \
7165         }                                                       \
7166       inst.operands[i].reg = val;                               \
7167       inst.operands[i].isreg = 1;                               \
7168       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7169       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7170       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7171                              || rtype == REG_TYPE_VFD           \
7172                              || rtype == REG_TYPE_NQ);          \
7173       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7174     }                                                           \
7175   while (0)
7176
7177 #define po_reg_or_goto(regtype, label)                          \
7178   do                                                            \
7179     {                                                           \
7180       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7181                                  & inst.operands[i].vectype);   \
7182       if (val == FAIL)                                          \
7183         goto label;                                             \
7184                                                                 \
7185       inst.operands[i].reg = val;                               \
7186       inst.operands[i].isreg = 1;                               \
7187       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7188       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7189       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7190                              || rtype == REG_TYPE_VFD           \
7191                              || rtype == REG_TYPE_NQ);          \
7192       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7193     }                                                           \
7194   while (0)
7195
7196 #define po_imm_or_fail(min, max, popt)                          \
7197   do                                                            \
7198     {                                                           \
7199       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
7200         goto failure;                                           \
7201       inst.operands[i].imm = val;                               \
7202     }                                                           \
7203   while (0)
7204
7205 #define po_imm1_or_imm2_or_fail(imm1, imm2, popt)               \
7206   do                                                            \
7207     {                                                           \
7208       expressionS exp;                                          \
7209       my_get_expression (&exp, &str, popt);                     \
7210       if (exp.X_op != O_constant)                               \
7211         {                                                       \
7212           inst.error = _("constant expression required");       \
7213           goto failure;                                         \
7214         }                                                       \
7215       if (exp.X_add_number != imm1 && exp.X_add_number != imm2) \
7216         {                                                       \
7217           inst.error = _("immediate value 48 or 64 expected");  \
7218           goto failure;                                         \
7219         }                                                       \
7220       inst.operands[i].imm = exp.X_add_number;                  \
7221     }                                                           \
7222   while (0)
7223
7224 #define po_scalar_or_goto(elsz, label, reg_type)                        \
7225   do                                                                    \
7226     {                                                                   \
7227       val = parse_scalar (& str, elsz, & inst.operands[i].vectype,      \
7228                           reg_type);                                    \
7229       if (val == FAIL)                                                  \
7230         goto label;                                                     \
7231       inst.operands[i].reg = val;                                       \
7232       inst.operands[i].isscalar = 1;                                    \
7233     }                                                                   \
7234   while (0)
7235
7236 #define po_misc_or_fail(expr)                   \
7237   do                                            \
7238     {                                           \
7239       if (expr)                                 \
7240         goto failure;                           \
7241     }                                           \
7242   while (0)
7243
7244 #define po_misc_or_fail_no_backtrack(expr)              \
7245   do                                                    \
7246     {                                                   \
7247       result = expr;                                    \
7248       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
7249         backtrack_pos = 0;                              \
7250       if (result != PARSE_OPERAND_SUCCESS)              \
7251         goto failure;                                   \
7252     }                                                   \
7253   while (0)
7254
7255 #define po_barrier_or_imm(str)                             \
7256   do                                                       \
7257     {                                                      \
7258       val = parse_barrier (&str);                          \
7259       if (val == FAIL && ! ISALPHA (*str))                 \
7260         goto immediate;                                    \
7261       if (val == FAIL                                      \
7262           /* ISB can only take SY as an option.  */        \
7263           || ((inst.instruction & 0xf0) == 0x60            \
7264                && val != 0xf))                             \
7265         {                                                  \
7266            inst.error = _("invalid barrier type");         \
7267            backtrack_pos = 0;                              \
7268            goto failure;                                   \
7269         }                                                  \
7270     }                                                      \
7271   while (0)
7272
7273   skip_whitespace (str);
7274
7275   for (i = 0; upat[i] != OP_stop; i++)
7276     {
7277       op_parse_code = upat[i];
7278       if (op_parse_code >= 1<<16)
7279         op_parse_code = thumb ? (op_parse_code >> 16)
7280                                 : (op_parse_code & ((1<<16)-1));
7281
7282       if (op_parse_code >= OP_FIRST_OPTIONAL)
7283         {
7284           /* Remember where we are in case we need to backtrack.  */
7285           backtrack_pos = str;
7286           backtrack_error = inst.error;
7287           backtrack_index = i;
7288         }
7289
7290       if (i > 0 && (i > 1 || inst.operands[0].present))
7291         po_char_or_fail (',');
7292
7293       switch (op_parse_code)
7294         {
7295           /* Registers */
7296         case OP_oRRnpc:
7297         case OP_oRRnpcsp:
7298         case OP_RRnpc:
7299         case OP_RRnpcsp:
7300         case OP_oRR:
7301         case OP_RRe:
7302         case OP_RRo:
7303         case OP_LR:
7304         case OP_oLR:
7305         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
7306         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
7307         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
7308         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
7309         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
7310         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
7311         case OP_oRND:
7312         case OP_RNDMQR:
7313           po_reg_or_goto (REG_TYPE_RN, try_rndmq);
7314           break;
7315         try_rndmq:
7316         case OP_RNDMQ:
7317           po_reg_or_goto (REG_TYPE_MQ, try_rnd);
7318           break;
7319         try_rnd:
7320         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
7321         case OP_RVC:
7322           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
7323           break;
7324           /* Also accept generic coprocessor regs for unknown registers.  */
7325           coproc_reg:
7326           po_reg_or_goto (REG_TYPE_CN, vpr_po);
7327           break;
7328           /* Also accept P0 or p0 for VPR.P0.  Since P0 is already an
7329              existing register with a value of 0, this seems like the
7330              best way to parse P0.  */
7331           vpr_po:
7332           if (strncasecmp (str, "P0", 2) == 0)
7333             {
7334               str += 2;
7335               inst.operands[i].isreg = 1;
7336               inst.operands[i].reg = 13;
7337             }
7338           else
7339             goto failure;
7340           break;
7341         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
7342         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
7343         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
7344         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
7345         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
7346         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
7347         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
7348         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
7349         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
7350         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
7351         case OP_oRNQ:
7352         case OP_RNQMQ:
7353           po_reg_or_goto (REG_TYPE_MQ, try_nq);
7354           break;
7355         try_nq:
7356         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
7357         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
7358         case OP_RNDQMQR:
7359           po_reg_or_goto (REG_TYPE_RN, try_rndqmq);
7360           break;
7361         try_rndqmq:
7362         case OP_oRNDQMQ:
7363         case OP_RNDQMQ:
7364           po_reg_or_goto (REG_TYPE_MQ, try_rndq);
7365           break;
7366         try_rndq:
7367         case OP_oRNDQ:
7368         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
7369         case OP_RVSDMQ:
7370           po_reg_or_goto (REG_TYPE_MQ, try_rvsd);
7371           break;
7372         try_rvsd:
7373         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
7374         case OP_RVSD_COND:
7375           po_reg_or_goto (REG_TYPE_VFSD, try_cond);
7376           break;
7377         case OP_oRNSDQ:
7378         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
7379         case OP_RNSDQMQR:
7380           po_reg_or_goto (REG_TYPE_RN, try_mq);
7381           break;
7382           try_mq:
7383         case OP_oRNSDQMQ:
7384         case OP_RNSDQMQ:
7385           po_reg_or_goto (REG_TYPE_MQ, try_nsdq2);
7386           break;
7387           try_nsdq2:
7388           po_reg_or_fail (REG_TYPE_NSDQ);
7389           inst.error = 0;
7390           break;
7391         case OP_RMQRR:
7392           po_reg_or_goto (REG_TYPE_RN, try_rmq);
7393           break;
7394         try_rmq:
7395         case OP_RMQ:
7396           po_reg_or_fail (REG_TYPE_MQ);
7397           break;
7398         /* Neon scalar. Using an element size of 8 means that some invalid
7399            scalars are accepted here, so deal with those in later code.  */
7400         case OP_RNSC:  po_scalar_or_goto (8, failure, REG_TYPE_VFD);    break;
7401
7402         case OP_RNDQ_I0:
7403           {
7404             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
7405             break;
7406             try_imm0:
7407             po_imm_or_fail (0, 0, TRUE);
7408           }
7409           break;
7410
7411         case OP_RVSD_I0:
7412           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
7413           break;
7414
7415         case OP_RSVDMQ_FI0:
7416           po_reg_or_goto (REG_TYPE_MQ, try_rsvd_fi0);
7417           break;
7418         try_rsvd_fi0:
7419         case OP_RSVD_FI0:
7420           {
7421             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
7422             break;
7423             try_ifimm0:
7424             if (parse_ifimm_zero (&str))
7425               inst.operands[i].imm = 0;
7426             else
7427             {
7428               inst.error
7429                 = _("only floating point zero is allowed as immediate value");
7430               goto failure;
7431             }
7432           }
7433           break;
7434
7435         case OP_RR_RNSC:
7436           {
7437             po_scalar_or_goto (8, try_rr, REG_TYPE_VFD);
7438             break;
7439             try_rr:
7440             po_reg_or_fail (REG_TYPE_RN);
7441           }
7442           break;
7443
7444         case OP_RNSDQ_RNSC_MQ_RR:
7445           po_reg_or_goto (REG_TYPE_RN, try_rnsdq_rnsc_mq);
7446           break;
7447         try_rnsdq_rnsc_mq:
7448         case OP_RNSDQ_RNSC_MQ:
7449           po_reg_or_goto (REG_TYPE_MQ, try_rnsdq_rnsc);
7450           break;
7451         try_rnsdq_rnsc:
7452         case OP_RNSDQ_RNSC:
7453           {
7454             po_scalar_or_goto (8, try_nsdq, REG_TYPE_VFD);
7455             inst.error = 0;
7456             break;
7457             try_nsdq:
7458             po_reg_or_fail (REG_TYPE_NSDQ);
7459             inst.error = 0;
7460           }
7461           break;
7462
7463         case OP_RNSD_RNSC:
7464           {
7465             po_scalar_or_goto (8, try_s_scalar, REG_TYPE_VFD);
7466             break;
7467             try_s_scalar:
7468             po_scalar_or_goto (4, try_nsd, REG_TYPE_VFS);
7469             break;
7470             try_nsd:
7471             po_reg_or_fail (REG_TYPE_NSD);
7472           }
7473           break;
7474
7475         case OP_RNDQMQ_RNSC_RR:
7476           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc_rr);
7477           break;
7478         try_rndq_rnsc_rr:
7479         case OP_RNDQ_RNSC_RR:
7480           po_reg_or_goto (REG_TYPE_RN, try_rndq_rnsc);
7481           break;
7482         case OP_RNDQMQ_RNSC:
7483           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc);
7484           break;
7485         try_rndq_rnsc:
7486         case OP_RNDQ_RNSC:
7487           {
7488             po_scalar_or_goto (8, try_ndq, REG_TYPE_VFD);
7489             break;
7490             try_ndq:
7491             po_reg_or_fail (REG_TYPE_NDQ);
7492           }
7493           break;
7494
7495         case OP_RND_RNSC:
7496           {
7497             po_scalar_or_goto (8, try_vfd, REG_TYPE_VFD);
7498             break;
7499             try_vfd:
7500             po_reg_or_fail (REG_TYPE_VFD);
7501           }
7502           break;
7503
7504         case OP_VMOV:
7505           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
7506              not careful then bad things might happen.  */
7507           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
7508           break;
7509
7510         case OP_RNDQMQ_Ibig:
7511           po_reg_or_goto (REG_TYPE_MQ, try_rndq_ibig);
7512           break;
7513         try_rndq_ibig:
7514         case OP_RNDQ_Ibig:
7515           {
7516             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
7517             break;
7518             try_immbig:
7519             /* There's a possibility of getting a 64-bit immediate here, so
7520                we need special handling.  */
7521             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
7522                 == FAIL)
7523               {
7524                 inst.error = _("immediate value is out of range");
7525                 goto failure;
7526               }
7527           }
7528           break;
7529
7530         case OP_RNDQMQ_I63b_RR:
7531           po_reg_or_goto (REG_TYPE_MQ, try_rndq_i63b_rr);
7532           break;
7533         try_rndq_i63b_rr:
7534           po_reg_or_goto (REG_TYPE_RN, try_rndq_i63b);
7535           break;
7536         try_rndq_i63b:
7537         case OP_RNDQ_I63b:
7538           {
7539             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
7540             break;
7541             try_shimm:
7542             po_imm_or_fail (0, 63, TRUE);
7543           }
7544           break;
7545
7546         case OP_RRnpcb:
7547           po_char_or_fail ('[');
7548           po_reg_or_fail  (REG_TYPE_RN);
7549           po_char_or_fail (']');
7550           break;
7551
7552         case OP_RRnpctw:
7553         case OP_RRw:
7554         case OP_oRRw:
7555           po_reg_or_fail (REG_TYPE_RN);
7556           if (skip_past_char (&str, '!') == SUCCESS)
7557             inst.operands[i].writeback = 1;
7558           break;
7559
7560           /* Immediates */
7561         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
7562         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
7563         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
7564         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
7565         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
7566         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
7567         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
7568         case OP_I48_I64: po_imm1_or_imm2_or_fail (48, 64, FALSE); break;
7569         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
7570         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
7571         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
7572         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
7573         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
7574
7575         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
7576         case OP_oI7b:
7577         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
7578         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
7579         case OP_oI31b:
7580         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
7581         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
7582         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
7583         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
7584
7585           /* Immediate variants */
7586         case OP_oI255c:
7587           po_char_or_fail ('{');
7588           po_imm_or_fail (0, 255, TRUE);
7589           po_char_or_fail ('}');
7590           break;
7591
7592         case OP_I31w:
7593           /* The expression parser chokes on a trailing !, so we have
7594              to find it first and zap it.  */
7595           {
7596             char *s = str;
7597             while (*s && *s != ',')
7598               s++;
7599             if (s[-1] == '!')
7600               {
7601                 s[-1] = '\0';
7602                 inst.operands[i].writeback = 1;
7603               }
7604             po_imm_or_fail (0, 31, TRUE);
7605             if (str == s - 1)
7606               str = s;
7607           }
7608           break;
7609
7610           /* Expressions */
7611         case OP_EXPi:   EXPi:
7612           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7613                                               GE_OPT_PREFIX));
7614           break;
7615
7616         case OP_EXP:
7617           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7618                                               GE_NO_PREFIX));
7619           break;
7620
7621         case OP_EXPr:   EXPr:
7622           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7623                                               GE_NO_PREFIX));
7624           if (inst.relocs[0].exp.X_op == O_symbol)
7625             {
7626               val = parse_reloc (&str);
7627               if (val == -1)
7628                 {
7629                   inst.error = _("unrecognized relocation suffix");
7630                   goto failure;
7631                 }
7632               else if (val != BFD_RELOC_UNUSED)
7633                 {
7634                   inst.operands[i].imm = val;
7635                   inst.operands[i].hasreloc = 1;
7636                 }
7637             }
7638           break;
7639
7640         case OP_EXPs:
7641           po_misc_or_fail (my_get_expression (&inst.relocs[i].exp, &str,
7642                                               GE_NO_PREFIX));
7643           if (inst.relocs[i].exp.X_op == O_symbol)
7644             {
7645               inst.operands[i].hasreloc = 1;
7646             }
7647           else if (inst.relocs[i].exp.X_op == O_constant)
7648             {
7649               inst.operands[i].imm = inst.relocs[i].exp.X_add_number;
7650               inst.operands[i].hasreloc = 0;
7651             }
7652           break;
7653
7654           /* Operand for MOVW or MOVT.  */
7655         case OP_HALF:
7656           po_misc_or_fail (parse_half (&str));
7657           break;
7658
7659           /* Register or expression.  */
7660         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7661         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7662
7663           /* Register or immediate.  */
7664         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7665         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7666
7667         case OP_RRnpcsp_I32: po_reg_or_goto (REG_TYPE_RN, I32); break;
7668         I32:                 po_imm_or_fail (1, 32, FALSE);     break;
7669
7670         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7671         IF:
7672           if (!is_immediate_prefix (*str))
7673             goto bad_args;
7674           str++;
7675           val = parse_fpa_immediate (&str);
7676           if (val == FAIL)
7677             goto failure;
7678           /* FPA immediates are encoded as registers 8-15.
7679              parse_fpa_immediate has already applied the offset.  */
7680           inst.operands[i].reg = val;
7681           inst.operands[i].isreg = 1;
7682           break;
7683
7684         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7685         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7686
7687           /* Two kinds of register.  */
7688         case OP_RIWR_RIWC:
7689           {
7690             struct reg_entry *rege = arm_reg_parse_multi (&str);
7691             if (!rege
7692                 || (rege->type != REG_TYPE_MMXWR
7693                     && rege->type != REG_TYPE_MMXWC
7694                     && rege->type != REG_TYPE_MMXWCG))
7695               {
7696                 inst.error = _("iWMMXt data or control register expected");
7697                 goto failure;
7698               }
7699             inst.operands[i].reg = rege->number;
7700             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7701           }
7702           break;
7703
7704         case OP_RIWC_RIWG:
7705           {
7706             struct reg_entry *rege = arm_reg_parse_multi (&str);
7707             if (!rege
7708                 || (rege->type != REG_TYPE_MMXWC
7709                     && rege->type != REG_TYPE_MMXWCG))
7710               {
7711                 inst.error = _("iWMMXt control register expected");
7712                 goto failure;
7713               }
7714             inst.operands[i].reg = rege->number;
7715             inst.operands[i].isreg = 1;
7716           }
7717           break;
7718
7719           /* Misc */
7720         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7721         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7722         case OP_oROR:    val = parse_ror (&str);                break;
7723         try_cond:
7724         case OP_COND:    val = parse_cond (&str);               break;
7725         case OP_oBARRIER_I15:
7726           po_barrier_or_imm (str); break;
7727           immediate:
7728           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7729             goto failure;
7730           break;
7731
7732         case OP_wPSR:
7733         case OP_rPSR:
7734           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7735           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7736             {
7737               inst.error = _("Banked registers are not available with this "
7738                              "architecture.");
7739               goto failure;
7740             }
7741           break;
7742           try_psr:
7743           val = parse_psr (&str, op_parse_code == OP_wPSR);
7744           break;
7745
7746         case OP_VLDR:
7747           po_reg_or_goto (REG_TYPE_VFSD, try_sysreg);
7748           break;
7749         try_sysreg:
7750           val = parse_sys_vldr_vstr (&str);
7751           break;
7752
7753         case OP_APSR_RR:
7754           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7755           break;
7756           try_apsr:
7757           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7758              instruction).  */
7759           if (strncasecmp (str, "APSR_", 5) == 0)
7760             {
7761               unsigned found = 0;
7762               str += 5;
7763               while (found < 15)
7764                 switch (*str++)
7765                   {
7766                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7767                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7768                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7769                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7770                   default: found = 16;
7771                   }
7772               if (found != 15)
7773                 goto failure;
7774               inst.operands[i].isvec = 1;
7775               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7776               inst.operands[i].reg = REG_PC;
7777             }
7778           else
7779             goto failure;
7780           break;
7781
7782         case OP_TB:
7783           po_misc_or_fail (parse_tb (&str));
7784           break;
7785
7786           /* Register lists.  */
7787         case OP_REGLST:
7788           val = parse_reg_list (&str, REGLIST_RN);
7789           if (*str == '^')
7790             {
7791               inst.operands[i].writeback = 1;
7792               str++;
7793             }
7794           break;
7795
7796         case OP_CLRMLST:
7797           val = parse_reg_list (&str, REGLIST_CLRM);
7798           break;
7799
7800         case OP_VRSLST:
7801           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S,
7802                                     &partial_match);
7803           break;
7804
7805         case OP_VRDLST:
7806           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D,
7807                                     &partial_match);
7808           break;
7809
7810         case OP_VRSDLST:
7811           /* Allow Q registers too.  */
7812           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7813                                     REGLIST_NEON_D, &partial_match);
7814           if (val == FAIL)
7815             {
7816               inst.error = NULL;
7817               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7818                                         REGLIST_VFP_S, &partial_match);
7819               inst.operands[i].issingle = 1;
7820             }
7821           break;
7822
7823         case OP_VRSDVLST:
7824           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7825                                     REGLIST_VFP_D_VPR, &partial_match);
7826           if (val == FAIL && !partial_match)
7827             {
7828               inst.error = NULL;
7829               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7830                                         REGLIST_VFP_S_VPR, &partial_match);
7831               inst.operands[i].issingle = 1;
7832             }
7833           break;
7834
7835         case OP_NRDLST:
7836           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7837                                     REGLIST_NEON_D, &partial_match);
7838           break;
7839
7840         case OP_MSTRLST4:
7841         case OP_MSTRLST2:
7842           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7843                                            1, &inst.operands[i].vectype);
7844           if (val != (((op_parse_code == OP_MSTRLST2) ? 3 : 7) << 5 | 0xe))
7845             goto failure;
7846           break;
7847         case OP_NSTRLST:
7848           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7849                                            0, &inst.operands[i].vectype);
7850           break;
7851
7852           /* Addressing modes */
7853         case OP_ADDRMVE:
7854           po_misc_or_fail (parse_address_group_reloc (&str, i, GROUP_MVE));
7855           break;
7856
7857         case OP_ADDR:
7858           po_misc_or_fail (parse_address (&str, i));
7859           break;
7860
7861         case OP_ADDRGLDR:
7862           po_misc_or_fail_no_backtrack (
7863             parse_address_group_reloc (&str, i, GROUP_LDR));
7864           break;
7865
7866         case OP_ADDRGLDRS:
7867           po_misc_or_fail_no_backtrack (
7868             parse_address_group_reloc (&str, i, GROUP_LDRS));
7869           break;
7870
7871         case OP_ADDRGLDC:
7872           po_misc_or_fail_no_backtrack (
7873             parse_address_group_reloc (&str, i, GROUP_LDC));
7874           break;
7875
7876         case OP_SH:
7877           po_misc_or_fail (parse_shifter_operand (&str, i));
7878           break;
7879
7880         case OP_SHG:
7881           po_misc_or_fail_no_backtrack (
7882             parse_shifter_operand_group_reloc (&str, i));
7883           break;
7884
7885         case OP_oSHll:
7886           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7887           break;
7888
7889         case OP_oSHar:
7890           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7891           break;
7892
7893         case OP_oSHllar:
7894           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7895           break;
7896
7897         case OP_RMQRZ:
7898         case OP_oRMQRZ:
7899           po_reg_or_goto (REG_TYPE_MQ, try_rr_zr);
7900           break;
7901
7902         case OP_RR_ZR:
7903         try_rr_zr:
7904           po_reg_or_goto (REG_TYPE_RN, ZR);
7905           break;
7906         ZR:
7907           po_reg_or_fail (REG_TYPE_ZR);
7908           break;
7909
7910         default:
7911           as_fatal (_("unhandled operand code %d"), op_parse_code);
7912         }
7913
7914       /* Various value-based sanity checks and shared operations.  We
7915          do not signal immediate failures for the register constraints;
7916          this allows a syntax error to take precedence.  */
7917       switch (op_parse_code)
7918         {
7919         case OP_oRRnpc:
7920         case OP_RRnpc:
7921         case OP_RRnpcb:
7922         case OP_RRw:
7923         case OP_oRRw:
7924         case OP_RRnpc_I0:
7925           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7926             inst.error = BAD_PC;
7927           break;
7928
7929         case OP_oRRnpcsp:
7930         case OP_RRnpcsp:
7931         case OP_RRnpcsp_I32:
7932           if (inst.operands[i].isreg)
7933             {
7934               if (inst.operands[i].reg == REG_PC)
7935                 inst.error = BAD_PC;
7936               else if (inst.operands[i].reg == REG_SP
7937                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7938                           relaxed since ARMv8-A.  */
7939                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7940                 {
7941                   gas_assert (thumb);
7942                   inst.error = BAD_SP;
7943                 }
7944             }
7945           break;
7946
7947         case OP_RRnpctw:
7948           if (inst.operands[i].isreg
7949               && inst.operands[i].reg == REG_PC
7950               && (inst.operands[i].writeback || thumb))
7951             inst.error = BAD_PC;
7952           break;
7953
7954         case OP_RVSD_COND:
7955         case OP_VLDR:
7956           if (inst.operands[i].isreg)
7957             break;
7958         /* fall through.  */
7959
7960         case OP_CPSF:
7961         case OP_ENDI:
7962         case OP_oROR:
7963         case OP_wPSR:
7964         case OP_rPSR:
7965         case OP_COND:
7966         case OP_oBARRIER_I15:
7967         case OP_REGLST:
7968         case OP_CLRMLST:
7969         case OP_VRSLST:
7970         case OP_VRDLST:
7971         case OP_VRSDLST:
7972         case OP_VRSDVLST:
7973         case OP_NRDLST:
7974         case OP_NSTRLST:
7975         case OP_MSTRLST2:
7976         case OP_MSTRLST4:
7977           if (val == FAIL)
7978             goto failure;
7979           inst.operands[i].imm = val;
7980           break;
7981
7982         case OP_LR:
7983         case OP_oLR:
7984           if (inst.operands[i].reg != REG_LR)
7985             inst.error = _("operand must be LR register");
7986           break;
7987
7988         case OP_RMQRZ:
7989         case OP_oRMQRZ:
7990         case OP_RR_ZR:
7991           if (!inst.operands[i].iszr && inst.operands[i].reg == REG_PC)
7992             inst.error = BAD_PC;
7993           break;
7994
7995         case OP_RRe:
7996           if (inst.operands[i].isreg
7997               && (inst.operands[i].reg & 0x00000001) != 0)
7998             inst.error = BAD_ODD;
7999           break;
8000
8001         case OP_RRo:
8002           if (inst.operands[i].isreg)
8003             {
8004               if ((inst.operands[i].reg & 0x00000001) != 1)
8005                 inst.error = BAD_EVEN;
8006               else if (inst.operands[i].reg == REG_SP)
8007                 as_tsktsk (MVE_BAD_SP);
8008               else if (inst.operands[i].reg == REG_PC)
8009                 inst.error = BAD_PC;
8010             }
8011           break;
8012
8013         default:
8014           break;
8015         }
8016
8017       /* If we get here, this operand was successfully parsed.  */
8018       inst.operands[i].present = 1;
8019       continue;
8020
8021     bad_args:
8022       inst.error = BAD_ARGS;
8023
8024     failure:
8025       if (!backtrack_pos)
8026         {
8027           /* The parse routine should already have set inst.error, but set a
8028              default here just in case.  */
8029           if (!inst.error)
8030             inst.error = BAD_SYNTAX;
8031           return FAIL;
8032         }
8033
8034       /* Do not backtrack over a trailing optional argument that
8035          absorbed some text.  We will only fail again, with the
8036          'garbage following instruction' error message, which is
8037          probably less helpful than the current one.  */
8038       if (backtrack_index == i && backtrack_pos != str
8039           && upat[i+1] == OP_stop)
8040         {
8041           if (!inst.error)
8042             inst.error = BAD_SYNTAX;
8043           return FAIL;
8044         }
8045
8046       /* Try again, skipping the optional argument at backtrack_pos.  */
8047       str = backtrack_pos;
8048       inst.error = backtrack_error;
8049       inst.operands[backtrack_index].present = 0;
8050       i = backtrack_index;
8051       backtrack_pos = 0;
8052     }
8053
8054   /* Check that we have parsed all the arguments.  */
8055   if (*str != '\0' && !inst.error)
8056     inst.error = _("garbage following instruction");
8057
8058   return inst.error ? FAIL : SUCCESS;
8059 }
8060
8061 #undef po_char_or_fail
8062 #undef po_reg_or_fail
8063 #undef po_reg_or_goto
8064 #undef po_imm_or_fail
8065 #undef po_scalar_or_fail
8066 #undef po_barrier_or_imm
8067
8068 /* Shorthand macro for instruction encoding functions issuing errors.  */
8069 #define constraint(expr, err)                   \
8070   do                                            \
8071     {                                           \
8072       if (expr)                                 \
8073         {                                       \
8074           inst.error = err;                     \
8075           return;                               \
8076         }                                       \
8077     }                                           \
8078   while (0)
8079
8080 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
8081    instructions are unpredictable if these registers are used.  This
8082    is the BadReg predicate in ARM's Thumb-2 documentation.
8083
8084    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
8085    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
8086 #define reject_bad_reg(reg)                                     \
8087   do                                                            \
8088    if (reg == REG_PC)                                           \
8089      {                                                          \
8090        inst.error = BAD_PC;                                     \
8091        return;                                                  \
8092      }                                                          \
8093    else if (reg == REG_SP                                       \
8094             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
8095      {                                                          \
8096        inst.error = BAD_SP;                                     \
8097        return;                                                  \
8098      }                                                          \
8099   while (0)
8100
8101 /* If REG is R13 (the stack pointer), warn that its use is
8102    deprecated.  */
8103 #define warn_deprecated_sp(reg)                 \
8104   do                                            \
8105     if (warn_on_deprecated && reg == REG_SP)    \
8106        as_tsktsk (_("use of r13 is deprecated"));       \
8107   while (0)
8108
8109 /* Functions for operand encoding.  ARM, then Thumb.  */
8110
8111 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
8112
8113 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
8114
8115    The only binary encoding difference is the Coprocessor number.  Coprocessor
8116    9 is used for half-precision calculations or conversions.  The format of the
8117    instruction is the same as the equivalent Coprocessor 10 instruction that
8118    exists for Single-Precision operation.  */
8119
8120 static void
8121 do_scalar_fp16_v82_encode (void)
8122 {
8123   if (inst.cond < COND_ALWAYS)
8124     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
8125                " the behaviour is UNPREDICTABLE"));
8126   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
8127               _(BAD_FP16));
8128
8129   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
8130   mark_feature_used (&arm_ext_fp16);
8131 }
8132
8133 /* If VAL can be encoded in the immediate field of an ARM instruction,
8134    return the encoded form.  Otherwise, return FAIL.  */
8135
8136 static unsigned int
8137 encode_arm_immediate (unsigned int val)
8138 {
8139   unsigned int a, i;
8140
8141   if (val <= 0xff)
8142     return val;
8143
8144   for (i = 2; i < 32; i += 2)
8145     if ((a = rotate_left (val, i)) <= 0xff)
8146       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
8147
8148   return FAIL;
8149 }
8150
8151 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
8152    return the encoded form.  Otherwise, return FAIL.  */
8153 static unsigned int
8154 encode_thumb32_immediate (unsigned int val)
8155 {
8156   unsigned int a, i;
8157
8158   if (val <= 0xff)
8159     return val;
8160
8161   for (i = 1; i <= 24; i++)
8162     {
8163       a = val >> i;
8164       if ((val & ~(0xff << i)) == 0)
8165         return ((val >> i) & 0x7f) | ((32 - i) << 7);
8166     }
8167
8168   a = val & 0xff;
8169   if (val == ((a << 16) | a))
8170     return 0x100 | a;
8171   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
8172     return 0x300 | a;
8173
8174   a = val & 0xff00;
8175   if (val == ((a << 16) | a))
8176     return 0x200 | (a >> 8);
8177
8178   return FAIL;
8179 }
8180 /* Encode a VFP SP or DP register number into inst.instruction.  */
8181
8182 static void
8183 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
8184 {
8185   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
8186       && reg > 15)
8187     {
8188       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
8189         {
8190           if (thumb_mode)
8191             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8192                                     fpu_vfp_ext_d32);
8193           else
8194             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
8195                                     fpu_vfp_ext_d32);
8196         }
8197       else
8198         {
8199           first_error (_("D register out of range for selected VFP version"));
8200           return;
8201         }
8202     }
8203
8204   switch (pos)
8205     {
8206     case VFP_REG_Sd:
8207       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
8208       break;
8209
8210     case VFP_REG_Sn:
8211       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
8212       break;
8213
8214     case VFP_REG_Sm:
8215       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
8216       break;
8217
8218     case VFP_REG_Dd:
8219       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
8220       break;
8221
8222     case VFP_REG_Dn:
8223       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
8224       break;
8225
8226     case VFP_REG_Dm:
8227       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
8228       break;
8229
8230     default:
8231       abort ();
8232     }
8233 }
8234
8235 /* Encode a <shift> in an ARM-format instruction.  The immediate,
8236    if any, is handled by md_apply_fix.   */
8237 static void
8238 encode_arm_shift (int i)
8239 {
8240   /* register-shifted register.  */
8241   if (inst.operands[i].immisreg)
8242     {
8243       int op_index;
8244       for (op_index = 0; op_index <= i; ++op_index)
8245         {
8246           /* Check the operand only when it's presented.  In pre-UAL syntax,
8247              if the destination register is the same as the first operand, two
8248              register form of the instruction can be used.  */
8249           if (inst.operands[op_index].present && inst.operands[op_index].isreg
8250               && inst.operands[op_index].reg == REG_PC)
8251             as_warn (UNPRED_REG ("r15"));
8252         }
8253
8254       if (inst.operands[i].imm == REG_PC)
8255         as_warn (UNPRED_REG ("r15"));
8256     }
8257
8258   if (inst.operands[i].shift_kind == SHIFT_RRX)
8259     inst.instruction |= SHIFT_ROR << 5;
8260   else
8261     {
8262       inst.instruction |= inst.operands[i].shift_kind << 5;
8263       if (inst.operands[i].immisreg)
8264         {
8265           inst.instruction |= SHIFT_BY_REG;
8266           inst.instruction |= inst.operands[i].imm << 8;
8267         }
8268       else
8269         inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8270     }
8271 }
8272
8273 static void
8274 encode_arm_shifter_operand (int i)
8275 {
8276   if (inst.operands[i].isreg)
8277     {
8278       inst.instruction |= inst.operands[i].reg;
8279       encode_arm_shift (i);
8280     }
8281   else
8282     {
8283       inst.instruction |= INST_IMMEDIATE;
8284       if (inst.relocs[0].type != BFD_RELOC_ARM_IMMEDIATE)
8285         inst.instruction |= inst.operands[i].imm;
8286     }
8287 }
8288
8289 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
8290 static void
8291 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
8292 {
8293   /* PR 14260:
8294      Generate an error if the operand is not a register.  */
8295   constraint (!inst.operands[i].isreg,
8296               _("Instruction does not support =N addresses"));
8297
8298   inst.instruction |= inst.operands[i].reg << 16;
8299
8300   if (inst.operands[i].preind)
8301     {
8302       if (is_t)
8303         {
8304           inst.error = _("instruction does not accept preindexed addressing");
8305           return;
8306         }
8307       inst.instruction |= PRE_INDEX;
8308       if (inst.operands[i].writeback)
8309         inst.instruction |= WRITE_BACK;
8310
8311     }
8312   else if (inst.operands[i].postind)
8313     {
8314       gas_assert (inst.operands[i].writeback);
8315       if (is_t)
8316         inst.instruction |= WRITE_BACK;
8317     }
8318   else /* unindexed - only for coprocessor */
8319     {
8320       inst.error = _("instruction does not accept unindexed addressing");
8321       return;
8322     }
8323
8324   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
8325       && (((inst.instruction & 0x000f0000) >> 16)
8326           == ((inst.instruction & 0x0000f000) >> 12)))
8327     as_warn ((inst.instruction & LOAD_BIT)
8328              ? _("destination register same as write-back base")
8329              : _("source register same as write-back base"));
8330 }
8331
8332 /* inst.operands[i] was set up by parse_address.  Encode it into an
8333    ARM-format mode 2 load or store instruction.  If is_t is true,
8334    reject forms that cannot be used with a T instruction (i.e. not
8335    post-indexed).  */
8336 static void
8337 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
8338 {
8339   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
8340
8341   encode_arm_addr_mode_common (i, is_t);
8342
8343   if (inst.operands[i].immisreg)
8344     {
8345       constraint ((inst.operands[i].imm == REG_PC
8346                    || (is_pc && inst.operands[i].writeback)),
8347                   BAD_PC_ADDRESSING);
8348       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
8349       inst.instruction |= inst.operands[i].imm;
8350       if (!inst.operands[i].negative)
8351         inst.instruction |= INDEX_UP;
8352       if (inst.operands[i].shifted)
8353         {
8354           if (inst.operands[i].shift_kind == SHIFT_RRX)
8355             inst.instruction |= SHIFT_ROR << 5;
8356           else
8357             {
8358               inst.instruction |= inst.operands[i].shift_kind << 5;
8359               inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8360             }
8361         }
8362     }
8363   else /* immediate offset in inst.relocs[0] */
8364     {
8365       if (is_pc && !inst.relocs[0].pc_rel)
8366         {
8367           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
8368
8369           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
8370              cannot use PC in addressing.
8371              PC cannot be used in writeback addressing, either.  */
8372           constraint ((is_t || inst.operands[i].writeback),
8373                       BAD_PC_ADDRESSING);
8374
8375           /* Use of PC in str is deprecated for ARMv7.  */
8376           if (warn_on_deprecated
8377               && !is_load
8378               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
8379             as_tsktsk (_("use of PC in this instruction is deprecated"));
8380         }
8381
8382       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8383         {
8384           /* Prefer + for zero encoded value.  */
8385           if (!inst.operands[i].negative)
8386             inst.instruction |= INDEX_UP;
8387           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM;
8388         }
8389     }
8390 }
8391
8392 /* inst.operands[i] was set up by parse_address.  Encode it into an
8393    ARM-format mode 3 load or store instruction.  Reject forms that
8394    cannot be used with such instructions.  If is_t is true, reject
8395    forms that cannot be used with a T instruction (i.e. not
8396    post-indexed).  */
8397 static void
8398 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
8399 {
8400   if (inst.operands[i].immisreg && inst.operands[i].shifted)
8401     {
8402       inst.error = _("instruction does not accept scaled register index");
8403       return;
8404     }
8405
8406   encode_arm_addr_mode_common (i, is_t);
8407
8408   if (inst.operands[i].immisreg)
8409     {
8410       constraint ((inst.operands[i].imm == REG_PC
8411                    || (is_t && inst.operands[i].reg == REG_PC)),
8412                   BAD_PC_ADDRESSING);
8413       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
8414                   BAD_PC_WRITEBACK);
8415       inst.instruction |= inst.operands[i].imm;
8416       if (!inst.operands[i].negative)
8417         inst.instruction |= INDEX_UP;
8418     }
8419   else /* immediate offset in inst.relocs[0] */
8420     {
8421       constraint ((inst.operands[i].reg == REG_PC && !inst.relocs[0].pc_rel
8422                    && inst.operands[i].writeback),
8423                   BAD_PC_WRITEBACK);
8424       inst.instruction |= HWOFFSET_IMM;
8425       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8426         {
8427           /* Prefer + for zero encoded value.  */
8428           if (!inst.operands[i].negative)
8429             inst.instruction |= INDEX_UP;
8430
8431           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM8;
8432         }
8433     }
8434 }
8435
8436 /* Write immediate bits [7:0] to the following locations:
8437
8438   |28/24|23     19|18 16|15                    4|3     0|
8439   |  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|
8440
8441   This function is used by VMOV/VMVN/VORR/VBIC.  */
8442
8443 static void
8444 neon_write_immbits (unsigned immbits)
8445 {
8446   inst.instruction |= immbits & 0xf;
8447   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
8448   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
8449 }
8450
8451 /* Invert low-order SIZE bits of XHI:XLO.  */
8452
8453 static void
8454 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
8455 {
8456   unsigned immlo = xlo ? *xlo : 0;
8457   unsigned immhi = xhi ? *xhi : 0;
8458
8459   switch (size)
8460     {
8461     case 8:
8462       immlo = (~immlo) & 0xff;
8463       break;
8464
8465     case 16:
8466       immlo = (~immlo) & 0xffff;
8467       break;
8468
8469     case 64:
8470       immhi = (~immhi) & 0xffffffff;
8471       /* fall through.  */
8472
8473     case 32:
8474       immlo = (~immlo) & 0xffffffff;
8475       break;
8476
8477     default:
8478       abort ();
8479     }
8480
8481   if (xlo)
8482     *xlo = immlo;
8483
8484   if (xhi)
8485     *xhi = immhi;
8486 }
8487
8488 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
8489    A, B, C, D.  */
8490
8491 static int
8492 neon_bits_same_in_bytes (unsigned imm)
8493 {
8494   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
8495          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
8496          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
8497          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
8498 }
8499
8500 /* For immediate of above form, return 0bABCD.  */
8501
8502 static unsigned
8503 neon_squash_bits (unsigned imm)
8504 {
8505   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
8506          | ((imm & 0x01000000) >> 21);
8507 }
8508
8509 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
8510
8511 static unsigned
8512 neon_qfloat_bits (unsigned imm)
8513 {
8514   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
8515 }
8516
8517 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
8518    the instruction. *OP is passed as the initial value of the op field, and
8519    may be set to a different value depending on the constant (i.e.
8520    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
8521    MVN).  If the immediate looks like a repeated pattern then also
8522    try smaller element sizes.  */
8523
8524 static int
8525 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
8526                          unsigned *immbits, int *op, int size,
8527                          enum neon_el_type type)
8528 {
8529   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
8530      float.  */
8531   if (type == NT_float && !float_p)
8532     return FAIL;
8533
8534   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
8535     {
8536       if (size != 32 || *op == 1)
8537         return FAIL;
8538       *immbits = neon_qfloat_bits (immlo);
8539       return 0xf;
8540     }
8541
8542   if (size == 64)
8543     {
8544       if (neon_bits_same_in_bytes (immhi)
8545           && neon_bits_same_in_bytes (immlo))
8546         {
8547           if (*op == 1)
8548             return FAIL;
8549           *immbits = (neon_squash_bits (immhi) << 4)
8550                      | neon_squash_bits (immlo);
8551           *op = 1;
8552           return 0xe;
8553         }
8554
8555       if (immhi != immlo)
8556         return FAIL;
8557     }
8558
8559   if (size >= 32)
8560     {
8561       if (immlo == (immlo & 0x000000ff))
8562         {
8563           *immbits = immlo;
8564           return 0x0;
8565         }
8566       else if (immlo == (immlo & 0x0000ff00))
8567         {
8568           *immbits = immlo >> 8;
8569           return 0x2;
8570         }
8571       else if (immlo == (immlo & 0x00ff0000))
8572         {
8573           *immbits = immlo >> 16;
8574           return 0x4;
8575         }
8576       else if (immlo == (immlo & 0xff000000))
8577         {
8578           *immbits = immlo >> 24;
8579           return 0x6;
8580         }
8581       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
8582         {
8583           *immbits = (immlo >> 8) & 0xff;
8584           return 0xc;
8585         }
8586       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
8587         {
8588           *immbits = (immlo >> 16) & 0xff;
8589           return 0xd;
8590         }
8591
8592       if ((immlo & 0xffff) != (immlo >> 16))
8593         return FAIL;
8594       immlo &= 0xffff;
8595     }
8596
8597   if (size >= 16)
8598     {
8599       if (immlo == (immlo & 0x000000ff))
8600         {
8601           *immbits = immlo;
8602           return 0x8;
8603         }
8604       else if (immlo == (immlo & 0x0000ff00))
8605         {
8606           *immbits = immlo >> 8;
8607           return 0xa;
8608         }
8609
8610       if ((immlo & 0xff) != (immlo >> 8))
8611         return FAIL;
8612       immlo &= 0xff;
8613     }
8614
8615   if (immlo == (immlo & 0x000000ff))
8616     {
8617       /* Don't allow MVN with 8-bit immediate.  */
8618       if (*op == 1)
8619         return FAIL;
8620       *immbits = immlo;
8621       return 0xe;
8622     }
8623
8624   return FAIL;
8625 }
8626
8627 #if defined BFD_HOST_64_BIT
8628 /* Returns TRUE if double precision value V may be cast
8629    to single precision without loss of accuracy.  */
8630
8631 static bfd_boolean
8632 is_double_a_single (bfd_int64_t v)
8633 {
8634   int exp = (int)((v >> 52) & 0x7FF);
8635   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8636
8637   return (exp == 0 || exp == 0x7FF
8638           || (exp >= 1023 - 126 && exp <= 1023 + 127))
8639     && (mantissa & 0x1FFFFFFFl) == 0;
8640 }
8641
8642 /* Returns a double precision value casted to single precision
8643    (ignoring the least significant bits in exponent and mantissa).  */
8644
8645 static int
8646 double_to_single (bfd_int64_t v)
8647 {
8648   int sign = (int) ((v >> 63) & 1l);
8649   int exp = (int) ((v >> 52) & 0x7FF);
8650   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8651
8652   if (exp == 0x7FF)
8653     exp = 0xFF;
8654   else
8655     {
8656       exp = exp - 1023 + 127;
8657       if (exp >= 0xFF)
8658         {
8659           /* Infinity.  */
8660           exp = 0x7F;
8661           mantissa = 0;
8662         }
8663       else if (exp < 0)
8664         {
8665           /* No denormalized numbers.  */
8666           exp = 0;
8667           mantissa = 0;
8668         }
8669     }
8670   mantissa >>= 29;
8671   return (sign << 31) | (exp << 23) | mantissa;
8672 }
8673 #endif /* BFD_HOST_64_BIT */
8674
8675 enum lit_type
8676 {
8677   CONST_THUMB,
8678   CONST_ARM,
8679   CONST_VEC
8680 };
8681
8682 static void do_vfp_nsyn_opcode (const char *);
8683
8684 /* inst.relocs[0].exp describes an "=expr" load pseudo-operation.
8685    Determine whether it can be performed with a move instruction; if
8686    it can, convert inst.instruction to that move instruction and
8687    return TRUE; if it can't, convert inst.instruction to a literal-pool
8688    load and return FALSE.  If this is not a valid thing to do in the
8689    current context, set inst.error and return TRUE.
8690
8691    inst.operands[i] describes the destination register.  */
8692
8693 static bfd_boolean
8694 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
8695 {
8696   unsigned long tbit;
8697   bfd_boolean thumb_p = (t == CONST_THUMB);
8698   bfd_boolean arm_p   = (t == CONST_ARM);
8699
8700   if (thumb_p)
8701     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
8702   else
8703     tbit = LOAD_BIT;
8704
8705   if ((inst.instruction & tbit) == 0)
8706     {
8707       inst.error = _("invalid pseudo operation");
8708       return TRUE;
8709     }
8710
8711   if (inst.relocs[0].exp.X_op != O_constant
8712       && inst.relocs[0].exp.X_op != O_symbol
8713       && inst.relocs[0].exp.X_op != O_big)
8714     {
8715       inst.error = _("constant expression expected");
8716       return TRUE;
8717     }
8718
8719   if (inst.relocs[0].exp.X_op == O_constant
8720       || inst.relocs[0].exp.X_op == O_big)
8721     {
8722 #if defined BFD_HOST_64_BIT
8723       bfd_int64_t v;
8724 #else
8725       offsetT v;
8726 #endif
8727       if (inst.relocs[0].exp.X_op == O_big)
8728         {
8729           LITTLENUM_TYPE w[X_PRECISION];
8730           LITTLENUM_TYPE * l;
8731
8732           if (inst.relocs[0].exp.X_add_number == -1)
8733             {
8734               gen_to_words (w, X_PRECISION, E_PRECISION);
8735               l = w;
8736               /* FIXME: Should we check words w[2..5] ?  */
8737             }
8738           else
8739             l = generic_bignum;
8740
8741 #if defined BFD_HOST_64_BIT
8742           v =
8743             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8744                   << LITTLENUM_NUMBER_OF_BITS)
8745                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8746                 << LITTLENUM_NUMBER_OF_BITS)
8747                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8748               << LITTLENUM_NUMBER_OF_BITS)
8749              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8750 #else
8751           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8752             |  (l[0] & LITTLENUM_MASK);
8753 #endif
8754         }
8755       else
8756         v = inst.relocs[0].exp.X_add_number;
8757
8758       if (!inst.operands[i].issingle)
8759         {
8760           if (thumb_p)
8761             {
8762               /* LDR should not use lead in a flag-setting instruction being
8763                  chosen so we do not check whether movs can be used.  */
8764
8765               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8766                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8767                   && inst.operands[i].reg != 13
8768                   && inst.operands[i].reg != 15)
8769                 {
8770                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8771                      movw instruction.  */
8772                   unsigned int newimm;
8773                   bfd_boolean isNegated;
8774
8775                   newimm = encode_thumb32_immediate (v);
8776                   if (newimm != (unsigned int) FAIL)
8777                     isNegated = FALSE;
8778                   else
8779                     {
8780                       newimm = encode_thumb32_immediate (~v);
8781                       if (newimm != (unsigned int) FAIL)
8782                         isNegated = TRUE;
8783                     }
8784
8785                   /* The number can be loaded with a mov.w or mvn
8786                      instruction.  */
8787                   if (newimm != (unsigned int) FAIL
8788                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8789                     {
8790                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8791                                           | (inst.operands[i].reg << 8));
8792                       /* Change to MOVN.  */
8793                       inst.instruction |= (isNegated ? 0x200000 : 0);
8794                       inst.instruction |= (newimm & 0x800) << 15;
8795                       inst.instruction |= (newimm & 0x700) << 4;
8796                       inst.instruction |= (newimm & 0x0ff);
8797                       return TRUE;
8798                     }
8799                   /* The number can be loaded with a movw instruction.  */
8800                   else if ((v & ~0xFFFF) == 0
8801                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8802                     {
8803                       int imm = v & 0xFFFF;
8804
8805                       inst.instruction = 0xf2400000;  /* MOVW.  */
8806                       inst.instruction |= (inst.operands[i].reg << 8);
8807                       inst.instruction |= (imm & 0xf000) << 4;
8808                       inst.instruction |= (imm & 0x0800) << 15;
8809                       inst.instruction |= (imm & 0x0700) << 4;
8810                       inst.instruction |= (imm & 0x00ff);
8811                       /*  In case this replacement is being done on Armv8-M
8812                           Baseline we need to make sure to disable the
8813                           instruction size check, as otherwise GAS will reject
8814                           the use of this T32 instruction.  */
8815                       inst.size_req = 0;
8816                       return TRUE;
8817                     }
8818                 }
8819             }
8820           else if (arm_p)
8821             {
8822               int value = encode_arm_immediate (v);
8823
8824               if (value != FAIL)
8825                 {
8826                   /* This can be done with a mov instruction.  */
8827                   inst.instruction &= LITERAL_MASK;
8828                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8829                   inst.instruction |= value & 0xfff;
8830                   return TRUE;
8831                 }
8832
8833               value = encode_arm_immediate (~ v);
8834               if (value != FAIL)
8835                 {
8836                   /* This can be done with a mvn instruction.  */
8837                   inst.instruction &= LITERAL_MASK;
8838                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8839                   inst.instruction |= value & 0xfff;
8840                   return TRUE;
8841                 }
8842             }
8843           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8844             {
8845               int op = 0;
8846               unsigned immbits = 0;
8847               unsigned immlo = inst.operands[1].imm;
8848               unsigned immhi = inst.operands[1].regisimm
8849                 ? inst.operands[1].reg
8850                 : inst.relocs[0].exp.X_unsigned
8851                 ? 0
8852                 : ((bfd_int64_t)((int) immlo)) >> 32;
8853               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8854                                                    &op, 64, NT_invtype);
8855
8856               if (cmode == FAIL)
8857                 {
8858                   neon_invert_size (&immlo, &immhi, 64);
8859                   op = !op;
8860                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8861                                                    &op, 64, NT_invtype);
8862                 }
8863
8864               if (cmode != FAIL)
8865                 {
8866                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8867                     | (1 << 23)
8868                     | (cmode << 8)
8869                     | (op << 5)
8870                     | (1 << 4);
8871
8872                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8873                   if (thumb_mode)
8874                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8875                   else
8876                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8877                   neon_write_immbits (immbits);
8878                   return TRUE;
8879                 }
8880             }
8881         }
8882
8883       if (t == CONST_VEC)
8884         {
8885           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8886           if (inst.operands[i].issingle
8887               && is_quarter_float (inst.operands[1].imm)
8888               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8889             {
8890               inst.operands[1].imm =
8891                 neon_qfloat_bits (v);
8892               do_vfp_nsyn_opcode ("fconsts");
8893               return TRUE;
8894             }
8895
8896           /* If our host does not support a 64-bit type then we cannot perform
8897              the following optimization.  This mean that there will be a
8898              discrepancy between the output produced by an assembler built for
8899              a 32-bit-only host and the output produced from a 64-bit host, but
8900              this cannot be helped.  */
8901 #if defined BFD_HOST_64_BIT
8902           else if (!inst.operands[1].issingle
8903                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8904             {
8905               if (is_double_a_single (v)
8906                   && is_quarter_float (double_to_single (v)))
8907                 {
8908                   inst.operands[1].imm =
8909                     neon_qfloat_bits (double_to_single (v));
8910                   do_vfp_nsyn_opcode ("fconstd");
8911                   return TRUE;
8912                 }
8913             }
8914 #endif
8915         }
8916     }
8917
8918   if (add_to_lit_pool ((!inst.operands[i].isvec
8919                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8920     return TRUE;
8921
8922   inst.operands[1].reg = REG_PC;
8923   inst.operands[1].isreg = 1;
8924   inst.operands[1].preind = 1;
8925   inst.relocs[0].pc_rel = 1;
8926   inst.relocs[0].type = (thumb_p
8927                      ? BFD_RELOC_ARM_THUMB_OFFSET
8928                      : (mode_3
8929                         ? BFD_RELOC_ARM_HWLITERAL
8930                         : BFD_RELOC_ARM_LITERAL));
8931   return FALSE;
8932 }
8933
8934 /* inst.operands[i] was set up by parse_address.  Encode it into an
8935    ARM-format instruction.  Reject all forms which cannot be encoded
8936    into a coprocessor load/store instruction.  If wb_ok is false,
8937    reject use of writeback; if unind_ok is false, reject use of
8938    unindexed addressing.  If reloc_override is not 0, use it instead
8939    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8940    (in which case it is preserved).  */
8941
8942 static int
8943 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8944 {
8945   if (!inst.operands[i].isreg)
8946     {
8947       /* PR 18256 */
8948       if (! inst.operands[0].isvec)
8949         {
8950           inst.error = _("invalid co-processor operand");
8951           return FAIL;
8952         }
8953       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8954         return SUCCESS;
8955     }
8956
8957   inst.instruction |= inst.operands[i].reg << 16;
8958
8959   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8960
8961   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8962     {
8963       gas_assert (!inst.operands[i].writeback);
8964       if (!unind_ok)
8965         {
8966           inst.error = _("instruction does not support unindexed addressing");
8967           return FAIL;
8968         }
8969       inst.instruction |= inst.operands[i].imm;
8970       inst.instruction |= INDEX_UP;
8971       return SUCCESS;
8972     }
8973
8974   if (inst.operands[i].preind)
8975     inst.instruction |= PRE_INDEX;
8976
8977   if (inst.operands[i].writeback)
8978     {
8979       if (inst.operands[i].reg == REG_PC)
8980         {
8981           inst.error = _("pc may not be used with write-back");
8982           return FAIL;
8983         }
8984       if (!wb_ok)
8985         {
8986           inst.error = _("instruction does not support writeback");
8987           return FAIL;
8988         }
8989       inst.instruction |= WRITE_BACK;
8990     }
8991
8992   if (reloc_override)
8993     inst.relocs[0].type = (bfd_reloc_code_real_type) reloc_override;
8994   else if ((inst.relocs[0].type < BFD_RELOC_ARM_ALU_PC_G0_NC
8995             || inst.relocs[0].type > BFD_RELOC_ARM_LDC_SB_G2)
8996            && inst.relocs[0].type != BFD_RELOC_ARM_LDR_PC_G0)
8997     {
8998       if (thumb_mode)
8999         inst.relocs[0].type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
9000       else
9001         inst.relocs[0].type = BFD_RELOC_ARM_CP_OFF_IMM;
9002     }
9003
9004   /* Prefer + for zero encoded value.  */
9005   if (!inst.operands[i].negative)
9006     inst.instruction |= INDEX_UP;
9007
9008   return SUCCESS;
9009 }
9010
9011 /* Functions for instruction encoding, sorted by sub-architecture.
9012    First some generics; their names are taken from the conventional
9013    bit positions for register arguments in ARM format instructions.  */
9014
9015 static void
9016 do_noargs (void)
9017 {
9018 }
9019
9020 static void
9021 do_rd (void)
9022 {
9023   inst.instruction |= inst.operands[0].reg << 12;
9024 }
9025
9026 static void
9027 do_rn (void)
9028 {
9029   inst.instruction |= inst.operands[0].reg << 16;
9030 }
9031
9032 static void
9033 do_rd_rm (void)
9034 {
9035   inst.instruction |= inst.operands[0].reg << 12;
9036   inst.instruction |= inst.operands[1].reg;
9037 }
9038
9039 static void
9040 do_rm_rn (void)
9041 {
9042   inst.instruction |= inst.operands[0].reg;
9043   inst.instruction |= inst.operands[1].reg << 16;
9044 }
9045
9046 static void
9047 do_rd_rn (void)
9048 {
9049   inst.instruction |= inst.operands[0].reg << 12;
9050   inst.instruction |= inst.operands[1].reg << 16;
9051 }
9052
9053 static void
9054 do_rn_rd (void)
9055 {
9056   inst.instruction |= inst.operands[0].reg << 16;
9057   inst.instruction |= inst.operands[1].reg << 12;
9058 }
9059
9060 static void
9061 do_tt (void)
9062 {
9063   inst.instruction |= inst.operands[0].reg << 8;
9064   inst.instruction |= inst.operands[1].reg << 16;
9065 }
9066
9067 static bfd_boolean
9068 check_obsolete (const arm_feature_set *feature, const char *msg)
9069 {
9070   if (ARM_CPU_IS_ANY (cpu_variant))
9071     {
9072       as_tsktsk ("%s", msg);
9073       return TRUE;
9074     }
9075   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
9076     {
9077       as_bad ("%s", msg);
9078       return TRUE;
9079     }
9080
9081   return FALSE;
9082 }
9083
9084 static void
9085 do_rd_rm_rn (void)
9086 {
9087   unsigned Rn = inst.operands[2].reg;
9088   /* Enforce restrictions on SWP instruction.  */
9089   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
9090     {
9091       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
9092                   _("Rn must not overlap other operands"));
9093
9094       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
9095        */
9096       if (!check_obsolete (&arm_ext_v8,
9097                            _("swp{b} use is obsoleted for ARMv8 and later"))
9098           && warn_on_deprecated
9099           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
9100         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
9101     }
9102
9103   inst.instruction |= inst.operands[0].reg << 12;
9104   inst.instruction |= inst.operands[1].reg;
9105   inst.instruction |= Rn << 16;
9106 }
9107
9108 static void
9109 do_rd_rn_rm (void)
9110 {
9111   inst.instruction |= inst.operands[0].reg << 12;
9112   inst.instruction |= inst.operands[1].reg << 16;
9113   inst.instruction |= inst.operands[2].reg;
9114 }
9115
9116 static void
9117 do_rm_rd_rn (void)
9118 {
9119   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
9120   constraint (((inst.relocs[0].exp.X_op != O_constant
9121                 && inst.relocs[0].exp.X_op != O_illegal)
9122                || inst.relocs[0].exp.X_add_number != 0),
9123               BAD_ADDR_MODE);
9124   inst.instruction |= inst.operands[0].reg;
9125   inst.instruction |= inst.operands[1].reg << 12;
9126   inst.instruction |= inst.operands[2].reg << 16;
9127 }
9128
9129 static void
9130 do_imm0 (void)
9131 {
9132   inst.instruction |= inst.operands[0].imm;
9133 }
9134
9135 static void
9136 do_rd_cpaddr (void)
9137 {
9138   inst.instruction |= inst.operands[0].reg << 12;
9139   encode_arm_cp_address (1, TRUE, TRUE, 0);
9140 }
9141
9142 /* ARM instructions, in alphabetical order by function name (except
9143    that wrapper functions appear immediately after the function they
9144    wrap).  */
9145
9146 /* This is a pseudo-op of the form "adr rd, label" to be converted
9147    into a relative address of the form "add rd, pc, #label-.-8".  */
9148
9149 static void
9150 do_adr (void)
9151 {
9152   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9153
9154   /* Frag hacking will turn this into a sub instruction if the offset turns
9155      out to be negative.  */
9156   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
9157   inst.relocs[0].pc_rel = 1;
9158   inst.relocs[0].exp.X_add_number -= 8;
9159
9160   if (support_interwork
9161       && inst.relocs[0].exp.X_op == O_symbol
9162       && inst.relocs[0].exp.X_add_symbol != NULL
9163       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9164       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9165     inst.relocs[0].exp.X_add_number |= 1;
9166 }
9167
9168 /* This is a pseudo-op of the form "adrl rd, label" to be converted
9169    into a relative address of the form:
9170    add rd, pc, #low(label-.-8)"
9171    add rd, rd, #high(label-.-8)"  */
9172
9173 static void
9174 do_adrl (void)
9175 {
9176   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9177
9178   /* Frag hacking will turn this into a sub instruction if the offset turns
9179      out to be negative.  */
9180   inst.relocs[0].type          = BFD_RELOC_ARM_ADRL_IMMEDIATE;
9181   inst.relocs[0].pc_rel        = 1;
9182   inst.size                    = INSN_SIZE * 2;
9183   inst.relocs[0].exp.X_add_number -= 8;
9184
9185   if (support_interwork
9186       && inst.relocs[0].exp.X_op == O_symbol
9187       && inst.relocs[0].exp.X_add_symbol != NULL
9188       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9189       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9190     inst.relocs[0].exp.X_add_number |= 1;
9191 }
9192
9193 static void
9194 do_arit (void)
9195 {
9196   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9197               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9198               THUMB1_RELOC_ONLY);
9199   if (!inst.operands[1].present)
9200     inst.operands[1].reg = inst.operands[0].reg;
9201   inst.instruction |= inst.operands[0].reg << 12;
9202   inst.instruction |= inst.operands[1].reg << 16;
9203   encode_arm_shifter_operand (2);
9204 }
9205
9206 static void
9207 do_barrier (void)
9208 {
9209   if (inst.operands[0].present)
9210     inst.instruction |= inst.operands[0].imm;
9211   else
9212     inst.instruction |= 0xf;
9213 }
9214
9215 static void
9216 do_bfc (void)
9217 {
9218   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9219   constraint (msb > 32, _("bit-field extends past end of register"));
9220   /* The instruction encoding stores the LSB and MSB,
9221      not the LSB and width.  */
9222   inst.instruction |= inst.operands[0].reg << 12;
9223   inst.instruction |= inst.operands[1].imm << 7;
9224   inst.instruction |= (msb - 1) << 16;
9225 }
9226
9227 static void
9228 do_bfi (void)
9229 {
9230   unsigned int msb;
9231
9232   /* #0 in second position is alternative syntax for bfc, which is
9233      the same instruction but with REG_PC in the Rm field.  */
9234   if (!inst.operands[1].isreg)
9235     inst.operands[1].reg = REG_PC;
9236
9237   msb = inst.operands[2].imm + inst.operands[3].imm;
9238   constraint (msb > 32, _("bit-field extends past end of register"));
9239   /* The instruction encoding stores the LSB and MSB,
9240      not the LSB and width.  */
9241   inst.instruction |= inst.operands[0].reg << 12;
9242   inst.instruction |= inst.operands[1].reg;
9243   inst.instruction |= inst.operands[2].imm << 7;
9244   inst.instruction |= (msb - 1) << 16;
9245 }
9246
9247 static void
9248 do_bfx (void)
9249 {
9250   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9251               _("bit-field extends past end of register"));
9252   inst.instruction |= inst.operands[0].reg << 12;
9253   inst.instruction |= inst.operands[1].reg;
9254   inst.instruction |= inst.operands[2].imm << 7;
9255   inst.instruction |= (inst.operands[3].imm - 1) << 16;
9256 }
9257
9258 /* ARM V5 breakpoint instruction (argument parse)
9259      BKPT <16 bit unsigned immediate>
9260      Instruction is not conditional.
9261         The bit pattern given in insns[] has the COND_ALWAYS condition,
9262         and it is an error if the caller tried to override that.  */
9263
9264 static void
9265 do_bkpt (void)
9266 {
9267   /* Top 12 of 16 bits to bits 19:8.  */
9268   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
9269
9270   /* Bottom 4 of 16 bits to bits 3:0.  */
9271   inst.instruction |= inst.operands[0].imm & 0xf;
9272 }
9273
9274 static void
9275 encode_branch (int default_reloc)
9276 {
9277   if (inst.operands[0].hasreloc)
9278     {
9279       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
9280                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
9281                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
9282       inst.relocs[0].type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
9283         ? BFD_RELOC_ARM_PLT32
9284         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
9285     }
9286   else
9287     inst.relocs[0].type = (bfd_reloc_code_real_type) default_reloc;
9288   inst.relocs[0].pc_rel = 1;
9289 }
9290
9291 static void
9292 do_branch (void)
9293 {
9294 #ifdef OBJ_ELF
9295   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9296     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9297   else
9298 #endif
9299     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9300 }
9301
9302 static void
9303 do_bl (void)
9304 {
9305 #ifdef OBJ_ELF
9306   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9307     {
9308       if (inst.cond == COND_ALWAYS)
9309         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
9310       else
9311         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9312     }
9313   else
9314 #endif
9315     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9316 }
9317
9318 /* ARM V5 branch-link-exchange instruction (argument parse)
9319      BLX <target_addr>          ie BLX(1)
9320      BLX{<condition>} <Rm>      ie BLX(2)
9321    Unfortunately, there are two different opcodes for this mnemonic.
9322    So, the insns[].value is not used, and the code here zaps values
9323         into inst.instruction.
9324    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
9325
9326 static void
9327 do_blx (void)
9328 {
9329   if (inst.operands[0].isreg)
9330     {
9331       /* Arg is a register; the opcode provided by insns[] is correct.
9332          It is not illegal to do "blx pc", just useless.  */
9333       if (inst.operands[0].reg == REG_PC)
9334         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
9335
9336       inst.instruction |= inst.operands[0].reg;
9337     }
9338   else
9339     {
9340       /* Arg is an address; this instruction cannot be executed
9341          conditionally, and the opcode must be adjusted.
9342          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
9343          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
9344       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9345       inst.instruction = 0xfa000000;
9346       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
9347     }
9348 }
9349
9350 static void
9351 do_bx (void)
9352 {
9353   bfd_boolean want_reloc;
9354
9355   if (inst.operands[0].reg == REG_PC)
9356     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
9357
9358   inst.instruction |= inst.operands[0].reg;
9359   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
9360      it is for ARMv4t or earlier.  */
9361   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
9362   if (!ARM_FEATURE_ZERO (selected_object_arch)
9363       && !ARM_CPU_HAS_FEATURE (selected_object_arch, arm_ext_v5))
9364       want_reloc = TRUE;
9365
9366 #ifdef OBJ_ELF
9367   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
9368 #endif
9369     want_reloc = FALSE;
9370
9371   if (want_reloc)
9372     inst.relocs[0].type = BFD_RELOC_ARM_V4BX;
9373 }
9374
9375
9376 /* ARM v5TEJ.  Jump to Jazelle code.  */
9377
9378 static void
9379 do_bxj (void)
9380 {
9381   if (inst.operands[0].reg == REG_PC)
9382     as_tsktsk (_("use of r15 in bxj is not really useful"));
9383
9384   inst.instruction |= inst.operands[0].reg;
9385 }
9386
9387 /* Co-processor data operation:
9388       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
9389       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
9390 static void
9391 do_cdp (void)
9392 {
9393   inst.instruction |= inst.operands[0].reg << 8;
9394   inst.instruction |= inst.operands[1].imm << 20;
9395   inst.instruction |= inst.operands[2].reg << 12;
9396   inst.instruction |= inst.operands[3].reg << 16;
9397   inst.instruction |= inst.operands[4].reg;
9398   inst.instruction |= inst.operands[5].imm << 5;
9399 }
9400
9401 static void
9402 do_cmp (void)
9403 {
9404   inst.instruction |= inst.operands[0].reg << 16;
9405   encode_arm_shifter_operand (1);
9406 }
9407
9408 /* Transfer between coprocessor and ARM registers.
9409    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
9410    MRC2
9411    MCR{cond}
9412    MCR2
9413
9414    No special properties.  */
9415
9416 struct deprecated_coproc_regs_s
9417 {
9418   unsigned cp;
9419   int opc1;
9420   unsigned crn;
9421   unsigned crm;
9422   int opc2;
9423   arm_feature_set deprecated;
9424   arm_feature_set obsoleted;
9425   const char *dep_msg;
9426   const char *obs_msg;
9427 };
9428
9429 #define DEPR_ACCESS_V8 \
9430   N_("This coprocessor register access is deprecated in ARMv8")
9431
9432 /* Table of all deprecated coprocessor registers.  */
9433 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
9434 {
9435     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
9436      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9437      DEPR_ACCESS_V8, NULL},
9438     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
9439      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9440      DEPR_ACCESS_V8, NULL},
9441     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
9442      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9443      DEPR_ACCESS_V8, NULL},
9444     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
9445      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9446      DEPR_ACCESS_V8, NULL},
9447     {14, 6, 0,  0, 0,                                   /* TEECR.  */
9448      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9449      DEPR_ACCESS_V8, NULL},
9450 };
9451
9452 #undef DEPR_ACCESS_V8
9453
9454 static const size_t deprecated_coproc_reg_count =
9455   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
9456
9457 static void
9458 do_co_reg (void)
9459 {
9460   unsigned Rd;
9461   size_t i;
9462
9463   Rd = inst.operands[2].reg;
9464   if (thumb_mode)
9465     {
9466       if (inst.instruction == 0xee000010
9467           || inst.instruction == 0xfe000010)
9468         /* MCR, MCR2  */
9469         reject_bad_reg (Rd);
9470       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9471         /* MRC, MRC2  */
9472         constraint (Rd == REG_SP, BAD_SP);
9473     }
9474   else
9475     {
9476       /* MCR */
9477       if (inst.instruction == 0xe000010)
9478         constraint (Rd == REG_PC, BAD_PC);
9479     }
9480
9481     for (i = 0; i < deprecated_coproc_reg_count; ++i)
9482       {
9483         const struct deprecated_coproc_regs_s *r =
9484           deprecated_coproc_regs + i;
9485
9486         if (inst.operands[0].reg == r->cp
9487             && inst.operands[1].imm == r->opc1
9488             && inst.operands[3].reg == r->crn
9489             && inst.operands[4].reg == r->crm
9490             && inst.operands[5].imm == r->opc2)
9491           {
9492             if (! ARM_CPU_IS_ANY (cpu_variant)
9493                 && warn_on_deprecated
9494                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
9495               as_tsktsk ("%s", r->dep_msg);
9496           }
9497       }
9498
9499   inst.instruction |= inst.operands[0].reg << 8;
9500   inst.instruction |= inst.operands[1].imm << 21;
9501   inst.instruction |= Rd << 12;
9502   inst.instruction |= inst.operands[3].reg << 16;
9503   inst.instruction |= inst.operands[4].reg;
9504   inst.instruction |= inst.operands[5].imm << 5;
9505 }
9506
9507 /* Transfer between coprocessor register and pair of ARM registers.
9508    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
9509    MCRR2
9510    MRRC{cond}
9511    MRRC2
9512
9513    Two XScale instructions are special cases of these:
9514
9515      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
9516      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
9517
9518    Result unpredictable if Rd or Rn is R15.  */
9519
9520 static void
9521 do_co_reg2c (void)
9522 {
9523   unsigned Rd, Rn;
9524
9525   Rd = inst.operands[2].reg;
9526   Rn = inst.operands[3].reg;
9527
9528   if (thumb_mode)
9529     {
9530       reject_bad_reg (Rd);
9531       reject_bad_reg (Rn);
9532     }
9533   else
9534     {
9535       constraint (Rd == REG_PC, BAD_PC);
9536       constraint (Rn == REG_PC, BAD_PC);
9537     }
9538
9539   /* Only check the MRRC{2} variants.  */
9540   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
9541     {
9542        /* If Rd == Rn, error that the operation is
9543           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
9544        constraint (Rd == Rn, BAD_OVERLAP);
9545     }
9546
9547   inst.instruction |= inst.operands[0].reg << 8;
9548   inst.instruction |= inst.operands[1].imm << 4;
9549   inst.instruction |= Rd << 12;
9550   inst.instruction |= Rn << 16;
9551   inst.instruction |= inst.operands[4].reg;
9552 }
9553
9554 static void
9555 do_cpsi (void)
9556 {
9557   inst.instruction |= inst.operands[0].imm << 6;
9558   if (inst.operands[1].present)
9559     {
9560       inst.instruction |= CPSI_MMOD;
9561       inst.instruction |= inst.operands[1].imm;
9562     }
9563 }
9564
9565 static void
9566 do_dbg (void)
9567 {
9568   inst.instruction |= inst.operands[0].imm;
9569 }
9570
9571 static void
9572 do_div (void)
9573 {
9574   unsigned Rd, Rn, Rm;
9575
9576   Rd = inst.operands[0].reg;
9577   Rn = (inst.operands[1].present
9578         ? inst.operands[1].reg : Rd);
9579   Rm = inst.operands[2].reg;
9580
9581   constraint ((Rd == REG_PC), BAD_PC);
9582   constraint ((Rn == REG_PC), BAD_PC);
9583   constraint ((Rm == REG_PC), BAD_PC);
9584
9585   inst.instruction |= Rd << 16;
9586   inst.instruction |= Rn << 0;
9587   inst.instruction |= Rm << 8;
9588 }
9589
9590 static void
9591 do_it (void)
9592 {
9593   /* There is no IT instruction in ARM mode.  We
9594      process it to do the validation as if in
9595      thumb mode, just in case the code gets
9596      assembled for thumb using the unified syntax.  */
9597
9598   inst.size = 0;
9599   if (unified_syntax)
9600     {
9601       set_pred_insn_type (IT_INSN);
9602       now_pred.mask = (inst.instruction & 0xf) | 0x10;
9603       now_pred.cc = inst.operands[0].imm;
9604     }
9605 }
9606
9607 /* If there is only one register in the register list,
9608    then return its register number.  Otherwise return -1.  */
9609 static int
9610 only_one_reg_in_list (int range)
9611 {
9612   int i = ffs (range) - 1;
9613   return (i > 15 || range != (1 << i)) ? -1 : i;
9614 }
9615
9616 static void
9617 encode_ldmstm(int from_push_pop_mnem)
9618 {
9619   int base_reg = inst.operands[0].reg;
9620   int range = inst.operands[1].imm;
9621   int one_reg;
9622
9623   inst.instruction |= base_reg << 16;
9624   inst.instruction |= range;
9625
9626   if (inst.operands[1].writeback)
9627     inst.instruction |= LDM_TYPE_2_OR_3;
9628
9629   if (inst.operands[0].writeback)
9630     {
9631       inst.instruction |= WRITE_BACK;
9632       /* Check for unpredictable uses of writeback.  */
9633       if (inst.instruction & LOAD_BIT)
9634         {
9635           /* Not allowed in LDM type 2.  */
9636           if ((inst.instruction & LDM_TYPE_2_OR_3)
9637               && ((range & (1 << REG_PC)) == 0))
9638             as_warn (_("writeback of base register is UNPREDICTABLE"));
9639           /* Only allowed if base reg not in list for other types.  */
9640           else if (range & (1 << base_reg))
9641             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
9642         }
9643       else /* STM.  */
9644         {
9645           /* Not allowed for type 2.  */
9646           if (inst.instruction & LDM_TYPE_2_OR_3)
9647             as_warn (_("writeback of base register is UNPREDICTABLE"));
9648           /* Only allowed if base reg not in list, or first in list.  */
9649           else if ((range & (1 << base_reg))
9650                    && (range & ((1 << base_reg) - 1)))
9651             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
9652         }
9653     }
9654
9655   /* If PUSH/POP has only one register, then use the A2 encoding.  */
9656   one_reg = only_one_reg_in_list (range);
9657   if (from_push_pop_mnem && one_reg >= 0)
9658     {
9659       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
9660
9661       if (is_push && one_reg == 13 /* SP */)
9662         /* PR 22483: The A2 encoding cannot be used when
9663            pushing the stack pointer as this is UNPREDICTABLE.  */
9664         return;
9665
9666       inst.instruction &= A_COND_MASK;
9667       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
9668       inst.instruction |= one_reg << 12;
9669     }
9670 }
9671
9672 static void
9673 do_ldmstm (void)
9674 {
9675   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
9676 }
9677
9678 /* ARMv5TE load-consecutive (argument parse)
9679    Mode is like LDRH.
9680
9681      LDRccD R, mode
9682      STRccD R, mode.  */
9683
9684 static void
9685 do_ldrd (void)
9686 {
9687   constraint (inst.operands[0].reg % 2 != 0,
9688               _("first transfer register must be even"));
9689   constraint (inst.operands[1].present
9690               && inst.operands[1].reg != inst.operands[0].reg + 1,
9691               _("can only transfer two consecutive registers"));
9692   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9693   constraint (!inst.operands[2].isreg, _("'[' expected"));
9694
9695   if (!inst.operands[1].present)
9696     inst.operands[1].reg = inst.operands[0].reg + 1;
9697
9698   /* encode_arm_addr_mode_3 will diagnose overlap between the base
9699      register and the first register written; we have to diagnose
9700      overlap between the base and the second register written here.  */
9701
9702   if (inst.operands[2].reg == inst.operands[1].reg
9703       && (inst.operands[2].writeback || inst.operands[2].postind))
9704     as_warn (_("base register written back, and overlaps "
9705                "second transfer register"));
9706
9707   if (!(inst.instruction & V4_STR_BIT))
9708     {
9709       /* For an index-register load, the index register must not overlap the
9710         destination (even if not write-back).  */
9711       if (inst.operands[2].immisreg
9712               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
9713               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
9714         as_warn (_("index register overlaps transfer register"));
9715     }
9716   inst.instruction |= inst.operands[0].reg << 12;
9717   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
9718 }
9719
9720 static void
9721 do_ldrex (void)
9722 {
9723   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
9724               || inst.operands[1].postind || inst.operands[1].writeback
9725               || inst.operands[1].immisreg || inst.operands[1].shifted
9726               || inst.operands[1].negative
9727               /* This can arise if the programmer has written
9728                    strex rN, rM, foo
9729                  or if they have mistakenly used a register name as the last
9730                  operand,  eg:
9731                    strex rN, rM, rX
9732                  It is very difficult to distinguish between these two cases
9733                  because "rX" might actually be a label. ie the register
9734                  name has been occluded by a symbol of the same name. So we
9735                  just generate a general 'bad addressing mode' type error
9736                  message and leave it up to the programmer to discover the
9737                  true cause and fix their mistake.  */
9738               || (inst.operands[1].reg == REG_PC),
9739               BAD_ADDR_MODE);
9740
9741   constraint (inst.relocs[0].exp.X_op != O_constant
9742               || inst.relocs[0].exp.X_add_number != 0,
9743               _("offset must be zero in ARM encoding"));
9744
9745   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
9746
9747   inst.instruction |= inst.operands[0].reg << 12;
9748   inst.instruction |= inst.operands[1].reg << 16;
9749   inst.relocs[0].type = BFD_RELOC_UNUSED;
9750 }
9751
9752 static void
9753 do_ldrexd (void)
9754 {
9755   constraint (inst.operands[0].reg % 2 != 0,
9756               _("even register required"));
9757   constraint (inst.operands[1].present
9758               && inst.operands[1].reg != inst.operands[0].reg + 1,
9759               _("can only load two consecutive registers"));
9760   /* If op 1 were present and equal to PC, this function wouldn't
9761      have been called in the first place.  */
9762   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9763
9764   inst.instruction |= inst.operands[0].reg << 12;
9765   inst.instruction |= inst.operands[2].reg << 16;
9766 }
9767
9768 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9769    which is not a multiple of four is UNPREDICTABLE.  */
9770 static void
9771 check_ldr_r15_aligned (void)
9772 {
9773   constraint (!(inst.operands[1].immisreg)
9774               && (inst.operands[0].reg == REG_PC
9775               && inst.operands[1].reg == REG_PC
9776               && (inst.relocs[0].exp.X_add_number & 0x3)),
9777               _("ldr to register 15 must be 4-byte aligned"));
9778 }
9779
9780 static void
9781 do_ldst (void)
9782 {
9783   inst.instruction |= inst.operands[0].reg << 12;
9784   if (!inst.operands[1].isreg)
9785     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9786       return;
9787   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9788   check_ldr_r15_aligned ();
9789 }
9790
9791 static void
9792 do_ldstt (void)
9793 {
9794   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9795      reject [Rn,...].  */
9796   if (inst.operands[1].preind)
9797     {
9798       constraint (inst.relocs[0].exp.X_op != O_constant
9799                   || inst.relocs[0].exp.X_add_number != 0,
9800                   _("this instruction requires a post-indexed address"));
9801
9802       inst.operands[1].preind = 0;
9803       inst.operands[1].postind = 1;
9804       inst.operands[1].writeback = 1;
9805     }
9806   inst.instruction |= inst.operands[0].reg << 12;
9807   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9808 }
9809
9810 /* Halfword and signed-byte load/store operations.  */
9811
9812 static void
9813 do_ldstv4 (void)
9814 {
9815   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9816   inst.instruction |= inst.operands[0].reg << 12;
9817   if (!inst.operands[1].isreg)
9818     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9819       return;
9820   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9821 }
9822
9823 static void
9824 do_ldsttv4 (void)
9825 {
9826   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9827      reject [Rn,...].  */
9828   if (inst.operands[1].preind)
9829     {
9830       constraint (inst.relocs[0].exp.X_op != O_constant
9831                   || inst.relocs[0].exp.X_add_number != 0,
9832                   _("this instruction requires a post-indexed address"));
9833
9834       inst.operands[1].preind = 0;
9835       inst.operands[1].postind = 1;
9836       inst.operands[1].writeback = 1;
9837     }
9838   inst.instruction |= inst.operands[0].reg << 12;
9839   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9840 }
9841
9842 /* Co-processor register load/store.
9843    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9844 static void
9845 do_lstc (void)
9846 {
9847   inst.instruction |= inst.operands[0].reg << 8;
9848   inst.instruction |= inst.operands[1].reg << 12;
9849   encode_arm_cp_address (2, TRUE, TRUE, 0);
9850 }
9851
9852 static void
9853 do_mlas (void)
9854 {
9855   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9856   if (inst.operands[0].reg == inst.operands[1].reg
9857       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9858       && !(inst.instruction & 0x00400000))
9859     as_tsktsk (_("Rd and Rm should be different in mla"));
9860
9861   inst.instruction |= inst.operands[0].reg << 16;
9862   inst.instruction |= inst.operands[1].reg;
9863   inst.instruction |= inst.operands[2].reg << 8;
9864   inst.instruction |= inst.operands[3].reg << 12;
9865 }
9866
9867 static void
9868 do_mov (void)
9869 {
9870   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9871               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9872               THUMB1_RELOC_ONLY);
9873   inst.instruction |= inst.operands[0].reg << 12;
9874   encode_arm_shifter_operand (1);
9875 }
9876
9877 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9878 static void
9879 do_mov16 (void)
9880 {
9881   bfd_vma imm;
9882   bfd_boolean top;
9883
9884   top = (inst.instruction & 0x00400000) != 0;
9885   constraint (top && inst.relocs[0].type == BFD_RELOC_ARM_MOVW,
9886               _(":lower16: not allowed in this instruction"));
9887   constraint (!top && inst.relocs[0].type == BFD_RELOC_ARM_MOVT,
9888               _(":upper16: not allowed in this instruction"));
9889   inst.instruction |= inst.operands[0].reg << 12;
9890   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
9891     {
9892       imm = inst.relocs[0].exp.X_add_number;
9893       /* The value is in two pieces: 0:11, 16:19.  */
9894       inst.instruction |= (imm & 0x00000fff);
9895       inst.instruction |= (imm & 0x0000f000) << 4;
9896     }
9897 }
9898
9899 static int
9900 do_vfp_nsyn_mrs (void)
9901 {
9902   if (inst.operands[0].isvec)
9903     {
9904       if (inst.operands[1].reg != 1)
9905         first_error (_("operand 1 must be FPSCR"));
9906       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9907       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9908       do_vfp_nsyn_opcode ("fmstat");
9909     }
9910   else if (inst.operands[1].isvec)
9911     do_vfp_nsyn_opcode ("fmrx");
9912   else
9913     return FAIL;
9914
9915   return SUCCESS;
9916 }
9917
9918 static int
9919 do_vfp_nsyn_msr (void)
9920 {
9921   if (inst.operands[0].isvec)
9922     do_vfp_nsyn_opcode ("fmxr");
9923   else
9924     return FAIL;
9925
9926   return SUCCESS;
9927 }
9928
9929 static void
9930 do_vmrs (void)
9931 {
9932   unsigned Rt = inst.operands[0].reg;
9933
9934   if (thumb_mode && Rt == REG_SP)
9935     {
9936       inst.error = BAD_SP;
9937       return;
9938     }
9939
9940   switch (inst.operands[1].reg)
9941     {
9942     /* MVFR2 is only valid for Armv8-A.  */
9943     case 5:
9944       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9945                   _(BAD_FPU));
9946       break;
9947
9948     /* Check for new Armv8.1-M Mainline changes to <spec_reg>.  */
9949     case 1: /* fpscr.  */
9950       constraint (!(ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9951                     || ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9952                   _(BAD_FPU));
9953       break;
9954
9955     case 14: /* fpcxt_ns.  */
9956     case 15: /* fpcxt_s.  */
9957       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main),
9958                   _("selected processor does not support instruction"));
9959       break;
9960
9961     case  2: /* fpscr_nzcvqc.  */
9962     case 12: /* vpr.  */
9963     case 13: /* p0.  */
9964       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main)
9965                   || (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9966                       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9967                   _("selected processor does not support instruction"));
9968       if (inst.operands[0].reg != 2
9969           && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
9970         as_warn (_("accessing MVE system register without MVE is UNPREDICTABLE"));
9971       break;
9972
9973     default:
9974       break;
9975     }
9976
9977   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9978   if (!inst.operands[0].isvec && Rt == REG_PC)
9979     {
9980       inst.error = BAD_PC;
9981       return;
9982     }
9983
9984   /* If we get through parsing the register name, we just insert the number
9985      generated into the instruction without further validation.  */
9986   inst.instruction |= (inst.operands[1].reg << 16);
9987   inst.instruction |= (Rt << 12);
9988 }
9989
9990 static void
9991 do_vmsr (void)
9992 {
9993   unsigned Rt = inst.operands[1].reg;
9994
9995   if (thumb_mode)
9996     reject_bad_reg (Rt);
9997   else if (Rt == REG_PC)
9998     {
9999       inst.error = BAD_PC;
10000       return;
10001     }
10002
10003   switch (inst.operands[0].reg)
10004     {
10005     /* MVFR2 is only valid for Armv8-A.  */
10006     case 5:
10007       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
10008                   _(BAD_FPU));
10009       break;
10010
10011     /* Check for new Armv8.1-M Mainline changes to <spec_reg>.  */
10012     case  1: /* fpcr.  */
10013       constraint (!(ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
10014                     || ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
10015                   _(BAD_FPU));
10016       break;
10017
10018     case 14: /* fpcxt_ns.  */
10019     case 15: /* fpcxt_s.  */
10020       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main),
10021                   _("selected processor does not support instruction"));
10022       break;
10023
10024     case  2: /* fpscr_nzcvqc.  */
10025     case 12: /* vpr.  */
10026     case 13: /* p0.  */
10027       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main)
10028                   || (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
10029                       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
10030                   _("selected processor does not support instruction"));
10031       if (inst.operands[0].reg != 2
10032           && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
10033         as_warn (_("accessing MVE system register without MVE is UNPREDICTABLE"));
10034       break;
10035
10036     default:
10037       break;
10038     }
10039
10040   /* If we get through parsing the register name, we just insert the number
10041      generated into the instruction without further validation.  */
10042   inst.instruction |= (inst.operands[0].reg << 16);
10043   inst.instruction |= (Rt << 12);
10044 }
10045
10046 static void
10047 do_mrs (void)
10048 {
10049   unsigned br;
10050
10051   if (do_vfp_nsyn_mrs () == SUCCESS)
10052     return;
10053
10054   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10055   inst.instruction |= inst.operands[0].reg << 12;
10056
10057   if (inst.operands[1].isreg)
10058     {
10059       br = inst.operands[1].reg;
10060       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
10061         as_bad (_("bad register for mrs"));
10062     }
10063   else
10064     {
10065       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
10066       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
10067                   != (PSR_c|PSR_f),
10068                   _("'APSR', 'CPSR' or 'SPSR' expected"));
10069       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
10070     }
10071
10072   inst.instruction |= br;
10073 }
10074
10075 /* Two possible forms:
10076       "{C|S}PSR_<field>, Rm",
10077       "{C|S}PSR_f, #expression".  */
10078
10079 static void
10080 do_msr (void)
10081 {
10082   if (do_vfp_nsyn_msr () == SUCCESS)
10083     return;
10084
10085   inst.instruction |= inst.operands[0].imm;
10086   if (inst.operands[1].isreg)
10087     inst.instruction |= inst.operands[1].reg;
10088   else
10089     {
10090       inst.instruction |= INST_IMMEDIATE;
10091       inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
10092       inst.relocs[0].pc_rel = 0;
10093     }
10094 }
10095
10096 static void
10097 do_mul (void)
10098 {
10099   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
10100
10101   if (!inst.operands[2].present)
10102     inst.operands[2].reg = inst.operands[0].reg;
10103   inst.instruction |= inst.operands[0].reg << 16;
10104   inst.instruction |= inst.operands[1].reg;
10105   inst.instruction |= inst.operands[2].reg << 8;
10106
10107   if (inst.operands[0].reg == inst.operands[1].reg
10108       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
10109     as_tsktsk (_("Rd and Rm should be different in mul"));
10110 }
10111
10112 /* Long Multiply Parser
10113    UMULL RdLo, RdHi, Rm, Rs
10114    SMULL RdLo, RdHi, Rm, Rs
10115    UMLAL RdLo, RdHi, Rm, Rs
10116    SMLAL RdLo, RdHi, Rm, Rs.  */
10117
10118 static void
10119 do_mull (void)
10120 {
10121   inst.instruction |= inst.operands[0].reg << 12;
10122   inst.instruction |= inst.operands[1].reg << 16;
10123   inst.instruction |= inst.operands[2].reg;
10124   inst.instruction |= inst.operands[3].reg << 8;
10125
10126   /* rdhi and rdlo must be different.  */
10127   if (inst.operands[0].reg == inst.operands[1].reg)
10128     as_tsktsk (_("rdhi and rdlo must be different"));
10129
10130   /* rdhi, rdlo and rm must all be different before armv6.  */
10131   if ((inst.operands[0].reg == inst.operands[2].reg
10132       || inst.operands[1].reg == inst.operands[2].reg)
10133       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
10134     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
10135 }
10136
10137 static void
10138 do_nop (void)
10139 {
10140   if (inst.operands[0].present
10141       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
10142     {
10143       /* Architectural NOP hints are CPSR sets with no bits selected.  */
10144       inst.instruction &= 0xf0000000;
10145       inst.instruction |= 0x0320f000;
10146       if (inst.operands[0].present)
10147         inst.instruction |= inst.operands[0].imm;
10148     }
10149 }
10150
10151 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
10152    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
10153    Condition defaults to COND_ALWAYS.
10154    Error if Rd, Rn or Rm are R15.  */
10155
10156 static void
10157 do_pkhbt (void)
10158 {
10159   inst.instruction |= inst.operands[0].reg << 12;
10160   inst.instruction |= inst.operands[1].reg << 16;
10161   inst.instruction |= inst.operands[2].reg;
10162   if (inst.operands[3].present)
10163     encode_arm_shift (3);
10164 }
10165
10166 /* ARM V6 PKHTB (Argument Parse).  */
10167
10168 static void
10169 do_pkhtb (void)
10170 {
10171   if (!inst.operands[3].present)
10172     {
10173       /* If the shift specifier is omitted, turn the instruction
10174          into pkhbt rd, rm, rn. */
10175       inst.instruction &= 0xfff00010;
10176       inst.instruction |= inst.operands[0].reg << 12;
10177       inst.instruction |= inst.operands[1].reg;
10178       inst.instruction |= inst.operands[2].reg << 16;
10179     }
10180   else
10181     {
10182       inst.instruction |= inst.operands[0].reg << 12;
10183       inst.instruction |= inst.operands[1].reg << 16;
10184       inst.instruction |= inst.operands[2].reg;
10185       encode_arm_shift (3);
10186     }
10187 }
10188
10189 /* ARMv5TE: Preload-Cache
10190    MP Extensions: Preload for write
10191
10192     PLD(W) <addr_mode>
10193
10194   Syntactically, like LDR with B=1, W=0, L=1.  */
10195
10196 static void
10197 do_pld (void)
10198 {
10199   constraint (!inst.operands[0].isreg,
10200               _("'[' expected after PLD mnemonic"));
10201   constraint (inst.operands[0].postind,
10202               _("post-indexed expression used in preload instruction"));
10203   constraint (inst.operands[0].writeback,
10204               _("writeback used in preload instruction"));
10205   constraint (!inst.operands[0].preind,
10206               _("unindexed addressing used in preload instruction"));
10207   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10208 }
10209
10210 /* ARMv7: PLI <addr_mode>  */
10211 static void
10212 do_pli (void)
10213 {
10214   constraint (!inst.operands[0].isreg,
10215               _("'[' expected after PLI mnemonic"));
10216   constraint (inst.operands[0].postind,
10217               _("post-indexed expression used in preload instruction"));
10218   constraint (inst.operands[0].writeback,
10219               _("writeback used in preload instruction"));
10220   constraint (!inst.operands[0].preind,
10221               _("unindexed addressing used in preload instruction"));
10222   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10223   inst.instruction &= ~PRE_INDEX;
10224 }
10225
10226 static void
10227 do_push_pop (void)
10228 {
10229   constraint (inst.operands[0].writeback,
10230               _("push/pop do not support {reglist}^"));
10231   inst.operands[1] = inst.operands[0];
10232   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
10233   inst.operands[0].isreg = 1;
10234   inst.operands[0].writeback = 1;
10235   inst.operands[0].reg = REG_SP;
10236   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
10237 }
10238
10239 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
10240    word at the specified address and the following word
10241    respectively.
10242    Unconditionally executed.
10243    Error if Rn is R15.  */
10244
10245 static void
10246 do_rfe (void)
10247 {
10248   inst.instruction |= inst.operands[0].reg << 16;
10249   if (inst.operands[0].writeback)
10250     inst.instruction |= WRITE_BACK;
10251 }
10252
10253 /* ARM V6 ssat (argument parse).  */
10254
10255 static void
10256 do_ssat (void)
10257 {
10258   inst.instruction |= inst.operands[0].reg << 12;
10259   inst.instruction |= (inst.operands[1].imm - 1) << 16;
10260   inst.instruction |= inst.operands[2].reg;
10261
10262   if (inst.operands[3].present)
10263     encode_arm_shift (3);
10264 }
10265
10266 /* ARM V6 usat (argument parse).  */
10267
10268 static void
10269 do_usat (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   if (inst.operands[3].present)
10276     encode_arm_shift (3);
10277 }
10278
10279 /* ARM V6 ssat16 (argument parse).  */
10280
10281 static void
10282 do_ssat16 (void)
10283 {
10284   inst.instruction |= inst.operands[0].reg << 12;
10285   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
10286   inst.instruction |= inst.operands[2].reg;
10287 }
10288
10289 static void
10290 do_usat16 (void)
10291 {
10292   inst.instruction |= inst.operands[0].reg << 12;
10293   inst.instruction |= inst.operands[1].imm << 16;
10294   inst.instruction |= inst.operands[2].reg;
10295 }
10296
10297 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
10298    preserving the other bits.
10299
10300    setend <endian_specifier>, where <endian_specifier> is either
10301    BE or LE.  */
10302
10303 static void
10304 do_setend (void)
10305 {
10306   if (warn_on_deprecated
10307       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10308       as_tsktsk (_("setend use is deprecated for ARMv8"));
10309
10310   if (inst.operands[0].imm)
10311     inst.instruction |= 0x200;
10312 }
10313
10314 static void
10315 do_shift (void)
10316 {
10317   unsigned int Rm = (inst.operands[1].present
10318                      ? inst.operands[1].reg
10319                      : inst.operands[0].reg);
10320
10321   inst.instruction |= inst.operands[0].reg << 12;
10322   inst.instruction |= Rm;
10323   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
10324     {
10325       inst.instruction |= inst.operands[2].reg << 8;
10326       inst.instruction |= SHIFT_BY_REG;
10327       /* PR 12854: Error on extraneous shifts.  */
10328       constraint (inst.operands[2].shifted,
10329                   _("extraneous shift as part of operand to shift insn"));
10330     }
10331   else
10332     inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
10333 }
10334
10335 static void
10336 do_smc (void)
10337 {
10338   unsigned int value = inst.relocs[0].exp.X_add_number;
10339   constraint (value > 0xf, _("immediate too large (bigger than 0xF)"));
10340
10341   inst.relocs[0].type = BFD_RELOC_ARM_SMC;
10342   inst.relocs[0].pc_rel = 0;
10343 }
10344
10345 static void
10346 do_hvc (void)
10347 {
10348   inst.relocs[0].type = BFD_RELOC_ARM_HVC;
10349   inst.relocs[0].pc_rel = 0;
10350 }
10351
10352 static void
10353 do_swi (void)
10354 {
10355   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
10356   inst.relocs[0].pc_rel = 0;
10357 }
10358
10359 static void
10360 do_setpan (void)
10361 {
10362   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10363               _("selected processor does not support SETPAN instruction"));
10364
10365   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
10366 }
10367
10368 static void
10369 do_t_setpan (void)
10370 {
10371   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10372               _("selected processor does not support SETPAN instruction"));
10373
10374   inst.instruction |= (inst.operands[0].imm << 3);
10375 }
10376
10377 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
10378    SMLAxy{cond} Rd,Rm,Rs,Rn
10379    SMLAWy{cond} Rd,Rm,Rs,Rn
10380    Error if any register is R15.  */
10381
10382 static void
10383 do_smla (void)
10384 {
10385   inst.instruction |= inst.operands[0].reg << 16;
10386   inst.instruction |= inst.operands[1].reg;
10387   inst.instruction |= inst.operands[2].reg << 8;
10388   inst.instruction |= inst.operands[3].reg << 12;
10389 }
10390
10391 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
10392    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
10393    Error if any register is R15.
10394    Warning if Rdlo == Rdhi.  */
10395
10396 static void
10397 do_smlal (void)
10398 {
10399   inst.instruction |= inst.operands[0].reg << 12;
10400   inst.instruction |= inst.operands[1].reg << 16;
10401   inst.instruction |= inst.operands[2].reg;
10402   inst.instruction |= inst.operands[3].reg << 8;
10403
10404   if (inst.operands[0].reg == inst.operands[1].reg)
10405     as_tsktsk (_("rdhi and rdlo must be different"));
10406 }
10407
10408 /* ARM V5E (El Segundo) signed-multiply (argument parse)
10409    SMULxy{cond} Rd,Rm,Rs
10410    Error if any register is R15.  */
10411
10412 static void
10413 do_smul (void)
10414 {
10415   inst.instruction |= inst.operands[0].reg << 16;
10416   inst.instruction |= inst.operands[1].reg;
10417   inst.instruction |= inst.operands[2].reg << 8;
10418 }
10419
10420 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
10421    the same for both ARM and Thumb-2.  */
10422
10423 static void
10424 do_srs (void)
10425 {
10426   int reg;
10427
10428   if (inst.operands[0].present)
10429     {
10430       reg = inst.operands[0].reg;
10431       constraint (reg != REG_SP, _("SRS base register must be r13"));
10432     }
10433   else
10434     reg = REG_SP;
10435
10436   inst.instruction |= reg << 16;
10437   inst.instruction |= inst.operands[1].imm;
10438   if (inst.operands[0].writeback || inst.operands[1].writeback)
10439     inst.instruction |= WRITE_BACK;
10440 }
10441
10442 /* ARM V6 strex (argument parse).  */
10443
10444 static void
10445 do_strex (void)
10446 {
10447   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10448               || inst.operands[2].postind || inst.operands[2].writeback
10449               || inst.operands[2].immisreg || inst.operands[2].shifted
10450               || inst.operands[2].negative
10451               /* See comment in do_ldrex().  */
10452               || (inst.operands[2].reg == REG_PC),
10453               BAD_ADDR_MODE);
10454
10455   constraint (inst.operands[0].reg == inst.operands[1].reg
10456               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10457
10458   constraint (inst.relocs[0].exp.X_op != O_constant
10459               || inst.relocs[0].exp.X_add_number != 0,
10460               _("offset must be zero in ARM encoding"));
10461
10462   inst.instruction |= inst.operands[0].reg << 12;
10463   inst.instruction |= inst.operands[1].reg;
10464   inst.instruction |= inst.operands[2].reg << 16;
10465   inst.relocs[0].type = BFD_RELOC_UNUSED;
10466 }
10467
10468 static void
10469 do_t_strexbh (void)
10470 {
10471   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10472               || inst.operands[2].postind || inst.operands[2].writeback
10473               || inst.operands[2].immisreg || inst.operands[2].shifted
10474               || inst.operands[2].negative,
10475               BAD_ADDR_MODE);
10476
10477   constraint (inst.operands[0].reg == inst.operands[1].reg
10478               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10479
10480   do_rm_rd_rn ();
10481 }
10482
10483 static void
10484 do_strexd (void)
10485 {
10486   constraint (inst.operands[1].reg % 2 != 0,
10487               _("even register required"));
10488   constraint (inst.operands[2].present
10489               && inst.operands[2].reg != inst.operands[1].reg + 1,
10490               _("can only store two consecutive registers"));
10491   /* If op 2 were present and equal to PC, this function wouldn't
10492      have been called in the first place.  */
10493   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
10494
10495   constraint (inst.operands[0].reg == inst.operands[1].reg
10496               || inst.operands[0].reg == inst.operands[1].reg + 1
10497               || inst.operands[0].reg == inst.operands[3].reg,
10498               BAD_OVERLAP);
10499
10500   inst.instruction |= inst.operands[0].reg << 12;
10501   inst.instruction |= inst.operands[1].reg;
10502   inst.instruction |= inst.operands[3].reg << 16;
10503 }
10504
10505 /* ARM V8 STRL.  */
10506 static void
10507 do_stlex (void)
10508 {
10509   constraint (inst.operands[0].reg == inst.operands[1].reg
10510               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10511
10512   do_rd_rm_rn ();
10513 }
10514
10515 static void
10516 do_t_stlex (void)
10517 {
10518   constraint (inst.operands[0].reg == inst.operands[1].reg
10519               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10520
10521   do_rm_rd_rn ();
10522 }
10523
10524 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
10525    extends it to 32-bits, and adds the result to a value in another
10526    register.  You can specify a rotation by 0, 8, 16, or 24 bits
10527    before extracting the 16-bit value.
10528    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
10529    Condition defaults to COND_ALWAYS.
10530    Error if any register uses R15.  */
10531
10532 static void
10533 do_sxtah (void)
10534 {
10535   inst.instruction |= inst.operands[0].reg << 12;
10536   inst.instruction |= inst.operands[1].reg << 16;
10537   inst.instruction |= inst.operands[2].reg;
10538   inst.instruction |= inst.operands[3].imm << 10;
10539 }
10540
10541 /* ARM V6 SXTH.
10542
10543    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
10544    Condition defaults to COND_ALWAYS.
10545    Error if any register uses R15.  */
10546
10547 static void
10548 do_sxth (void)
10549 {
10550   inst.instruction |= inst.operands[0].reg << 12;
10551   inst.instruction |= inst.operands[1].reg;
10552   inst.instruction |= inst.operands[2].imm << 10;
10553 }
10554 \f
10555 /* VFP instructions.  In a logical order: SP variant first, monad
10556    before dyad, arithmetic then move then load/store.  */
10557
10558 static void
10559 do_vfp_sp_monadic (void)
10560 {
10561   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10562               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10563               _(BAD_FPU));
10564
10565   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10566   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10567 }
10568
10569 static void
10570 do_vfp_sp_dyadic (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_Sn);
10574   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10575 }
10576
10577 static void
10578 do_vfp_sp_compare_z (void)
10579 {
10580   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10581 }
10582
10583 static void
10584 do_vfp_dp_sp_cvt (void)
10585 {
10586   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10587   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10588 }
10589
10590 static void
10591 do_vfp_sp_dp_cvt (void)
10592 {
10593   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10594   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10595 }
10596
10597 static void
10598 do_vfp_reg_from_sp (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   inst.instruction |= inst.operands[0].reg << 12;
10605   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10606 }
10607
10608 static void
10609 do_vfp_reg2_from_sp2 (void)
10610 {
10611   constraint (inst.operands[2].imm != 2,
10612               _("only two consecutive VFP SP registers allowed here"));
10613   inst.instruction |= inst.operands[0].reg << 12;
10614   inst.instruction |= inst.operands[1].reg << 16;
10615   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10616 }
10617
10618 static void
10619 do_vfp_sp_from_reg (void)
10620 {
10621   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10622              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10623              _(BAD_FPU));
10624
10625   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
10626   inst.instruction |= inst.operands[1].reg << 12;
10627 }
10628
10629 static void
10630 do_vfp_sp2_from_reg2 (void)
10631 {
10632   constraint (inst.operands[0].imm != 2,
10633               _("only two consecutive VFP SP registers allowed here"));
10634   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
10635   inst.instruction |= inst.operands[1].reg << 12;
10636   inst.instruction |= inst.operands[2].reg << 16;
10637 }
10638
10639 static void
10640 do_vfp_sp_ldst (void)
10641 {
10642   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10643   encode_arm_cp_address (1, FALSE, TRUE, 0);
10644 }
10645
10646 static void
10647 do_vfp_dp_ldst (void)
10648 {
10649   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10650   encode_arm_cp_address (1, FALSE, TRUE, 0);
10651 }
10652
10653
10654 static void
10655 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
10656 {
10657   if (inst.operands[0].writeback)
10658     inst.instruction |= WRITE_BACK;
10659   else
10660     constraint (ldstm_type != VFP_LDSTMIA,
10661                 _("this addressing mode requires base-register writeback"));
10662   inst.instruction |= inst.operands[0].reg << 16;
10663   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
10664   inst.instruction |= inst.operands[1].imm;
10665 }
10666
10667 static void
10668 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
10669 {
10670   int count;
10671
10672   if (inst.operands[0].writeback)
10673     inst.instruction |= WRITE_BACK;
10674   else
10675     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
10676                 _("this addressing mode requires base-register writeback"));
10677
10678   inst.instruction |= inst.operands[0].reg << 16;
10679   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10680
10681   count = inst.operands[1].imm << 1;
10682   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
10683     count += 1;
10684
10685   inst.instruction |= count;
10686 }
10687
10688 static void
10689 do_vfp_sp_ldstmia (void)
10690 {
10691   vfp_sp_ldstm (VFP_LDSTMIA);
10692 }
10693
10694 static void
10695 do_vfp_sp_ldstmdb (void)
10696 {
10697   vfp_sp_ldstm (VFP_LDSTMDB);
10698 }
10699
10700 static void
10701 do_vfp_dp_ldstmia (void)
10702 {
10703   vfp_dp_ldstm (VFP_LDSTMIA);
10704 }
10705
10706 static void
10707 do_vfp_dp_ldstmdb (void)
10708 {
10709   vfp_dp_ldstm (VFP_LDSTMDB);
10710 }
10711
10712 static void
10713 do_vfp_xp_ldstmia (void)
10714 {
10715   vfp_dp_ldstm (VFP_LDSTMIAX);
10716 }
10717
10718 static void
10719 do_vfp_xp_ldstmdb (void)
10720 {
10721   vfp_dp_ldstm (VFP_LDSTMDBX);
10722 }
10723
10724 static void
10725 do_vfp_dp_rd_rm (void)
10726 {
10727   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
10728               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10729               _(BAD_FPU));
10730
10731   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10732   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10733 }
10734
10735 static void
10736 do_vfp_dp_rn_rd (void)
10737 {
10738   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
10739   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10740 }
10741
10742 static void
10743 do_vfp_dp_rd_rn (void)
10744 {
10745   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10746   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10747 }
10748
10749 static void
10750 do_vfp_dp_rd_rn_rm (void)
10751 {
10752   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10753               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10754               _(BAD_FPU));
10755
10756   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10757   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10758   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
10759 }
10760
10761 static void
10762 do_vfp_dp_rd (void)
10763 {
10764   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10765 }
10766
10767 static void
10768 do_vfp_dp_rm_rd_rn (void)
10769 {
10770   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10771               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10772               _(BAD_FPU));
10773
10774   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
10775   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10776   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
10777 }
10778
10779 /* VFPv3 instructions.  */
10780 static void
10781 do_vfp_sp_const (void)
10782 {
10783   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10784   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10785   inst.instruction |= (inst.operands[1].imm & 0x0f);
10786 }
10787
10788 static void
10789 do_vfp_dp_const (void)
10790 {
10791   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10792   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10793   inst.instruction |= (inst.operands[1].imm & 0x0f);
10794 }
10795
10796 static void
10797 vfp_conv (int srcsize)
10798 {
10799   int immbits = srcsize - inst.operands[1].imm;
10800
10801   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
10802     {
10803       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
10804          i.e. immbits must be in range 0 - 16.  */
10805       inst.error = _("immediate value out of range, expected range [0, 16]");
10806       return;
10807     }
10808   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
10809     {
10810       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
10811          i.e. immbits must be in range 0 - 31.  */
10812       inst.error = _("immediate value out of range, expected range [1, 32]");
10813       return;
10814     }
10815
10816   inst.instruction |= (immbits & 1) << 5;
10817   inst.instruction |= (immbits >> 1);
10818 }
10819
10820 static void
10821 do_vfp_sp_conv_16 (void)
10822 {
10823   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10824   vfp_conv (16);
10825 }
10826
10827 static void
10828 do_vfp_dp_conv_16 (void)
10829 {
10830   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10831   vfp_conv (16);
10832 }
10833
10834 static void
10835 do_vfp_sp_conv_32 (void)
10836 {
10837   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10838   vfp_conv (32);
10839 }
10840
10841 static void
10842 do_vfp_dp_conv_32 (void)
10843 {
10844   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10845   vfp_conv (32);
10846 }
10847 \f
10848 /* FPA instructions.  Also in a logical order.  */
10849
10850 static void
10851 do_fpa_cmp (void)
10852 {
10853   inst.instruction |= inst.operands[0].reg << 16;
10854   inst.instruction |= inst.operands[1].reg;
10855 }
10856
10857 static void
10858 do_fpa_ldmstm (void)
10859 {
10860   inst.instruction |= inst.operands[0].reg << 12;
10861   switch (inst.operands[1].imm)
10862     {
10863     case 1: inst.instruction |= CP_T_X;          break;
10864     case 2: inst.instruction |= CP_T_Y;          break;
10865     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10866     case 4:                                      break;
10867     default: abort ();
10868     }
10869
10870   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10871     {
10872       /* The instruction specified "ea" or "fd", so we can only accept
10873          [Rn]{!}.  The instruction does not really support stacking or
10874          unstacking, so we have to emulate these by setting appropriate
10875          bits and offsets.  */
10876       constraint (inst.relocs[0].exp.X_op != O_constant
10877                   || inst.relocs[0].exp.X_add_number != 0,
10878                   _("this instruction does not support indexing"));
10879
10880       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10881         inst.relocs[0].exp.X_add_number = 12 * inst.operands[1].imm;
10882
10883       if (!(inst.instruction & INDEX_UP))
10884         inst.relocs[0].exp.X_add_number = -inst.relocs[0].exp.X_add_number;
10885
10886       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10887         {
10888           inst.operands[2].preind = 0;
10889           inst.operands[2].postind = 1;
10890         }
10891     }
10892
10893   encode_arm_cp_address (2, TRUE, TRUE, 0);
10894 }
10895 \f
10896 /* iWMMXt instructions: strictly in alphabetical order.  */
10897
10898 static void
10899 do_iwmmxt_tandorc (void)
10900 {
10901   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10902 }
10903
10904 static void
10905 do_iwmmxt_textrc (void)
10906 {
10907   inst.instruction |= inst.operands[0].reg << 12;
10908   inst.instruction |= inst.operands[1].imm;
10909 }
10910
10911 static void
10912 do_iwmmxt_textrm (void)
10913 {
10914   inst.instruction |= inst.operands[0].reg << 12;
10915   inst.instruction |= inst.operands[1].reg << 16;
10916   inst.instruction |= inst.operands[2].imm;
10917 }
10918
10919 static void
10920 do_iwmmxt_tinsr (void)
10921 {
10922   inst.instruction |= inst.operands[0].reg << 16;
10923   inst.instruction |= inst.operands[1].reg << 12;
10924   inst.instruction |= inst.operands[2].imm;
10925 }
10926
10927 static void
10928 do_iwmmxt_tmia (void)
10929 {
10930   inst.instruction |= inst.operands[0].reg << 5;
10931   inst.instruction |= inst.operands[1].reg;
10932   inst.instruction |= inst.operands[2].reg << 12;
10933 }
10934
10935 static void
10936 do_iwmmxt_waligni (void)
10937 {
10938   inst.instruction |= inst.operands[0].reg << 12;
10939   inst.instruction |= inst.operands[1].reg << 16;
10940   inst.instruction |= inst.operands[2].reg;
10941   inst.instruction |= inst.operands[3].imm << 20;
10942 }
10943
10944 static void
10945 do_iwmmxt_wmerge (void)
10946 {
10947   inst.instruction |= inst.operands[0].reg << 12;
10948   inst.instruction |= inst.operands[1].reg << 16;
10949   inst.instruction |= inst.operands[2].reg;
10950   inst.instruction |= inst.operands[3].imm << 21;
10951 }
10952
10953 static void
10954 do_iwmmxt_wmov (void)
10955 {
10956   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10957   inst.instruction |= inst.operands[0].reg << 12;
10958   inst.instruction |= inst.operands[1].reg << 16;
10959   inst.instruction |= inst.operands[1].reg;
10960 }
10961
10962 static void
10963 do_iwmmxt_wldstbh (void)
10964 {
10965   int reloc;
10966   inst.instruction |= inst.operands[0].reg << 12;
10967   if (thumb_mode)
10968     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10969   else
10970     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10971   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10972 }
10973
10974 static void
10975 do_iwmmxt_wldstw (void)
10976 {
10977   /* RIWR_RIWC clears .isreg for a control register.  */
10978   if (!inst.operands[0].isreg)
10979     {
10980       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10981       inst.instruction |= 0xf0000000;
10982     }
10983
10984   inst.instruction |= inst.operands[0].reg << 12;
10985   encode_arm_cp_address (1, TRUE, TRUE, 0);
10986 }
10987
10988 static void
10989 do_iwmmxt_wldstd (void)
10990 {
10991   inst.instruction |= inst.operands[0].reg << 12;
10992   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10993       && inst.operands[1].immisreg)
10994     {
10995       inst.instruction &= ~0x1a000ff;
10996       inst.instruction |= (0xfU << 28);
10997       if (inst.operands[1].preind)
10998         inst.instruction |= PRE_INDEX;
10999       if (!inst.operands[1].negative)
11000         inst.instruction |= INDEX_UP;
11001       if (inst.operands[1].writeback)
11002         inst.instruction |= WRITE_BACK;
11003       inst.instruction |= inst.operands[1].reg << 16;
11004       inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
11005       inst.instruction |= inst.operands[1].imm;
11006     }
11007   else
11008     encode_arm_cp_address (1, TRUE, FALSE, 0);
11009 }
11010
11011 static void
11012 do_iwmmxt_wshufh (void)
11013 {
11014   inst.instruction |= inst.operands[0].reg << 12;
11015   inst.instruction |= inst.operands[1].reg << 16;
11016   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
11017   inst.instruction |= (inst.operands[2].imm & 0x0f);
11018 }
11019
11020 static void
11021 do_iwmmxt_wzero (void)
11022 {
11023   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
11024   inst.instruction |= inst.operands[0].reg;
11025   inst.instruction |= inst.operands[0].reg << 12;
11026   inst.instruction |= inst.operands[0].reg << 16;
11027 }
11028
11029 static void
11030 do_iwmmxt_wrwrwr_or_imm5 (void)
11031 {
11032   if (inst.operands[2].isreg)
11033     do_rd_rn_rm ();
11034   else {
11035     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
11036                 _("immediate operand requires iWMMXt2"));
11037     do_rd_rn ();
11038     if (inst.operands[2].imm == 0)
11039       {
11040         switch ((inst.instruction >> 20) & 0xf)
11041           {
11042           case 4:
11043           case 5:
11044           case 6:
11045           case 7:
11046             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
11047             inst.operands[2].imm = 16;
11048             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
11049             break;
11050           case 8:
11051           case 9:
11052           case 10:
11053           case 11:
11054             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
11055             inst.operands[2].imm = 32;
11056             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
11057             break;
11058           case 12:
11059           case 13:
11060           case 14:
11061           case 15:
11062             {
11063               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
11064               unsigned long wrn;
11065               wrn = (inst.instruction >> 16) & 0xf;
11066               inst.instruction &= 0xff0fff0f;
11067               inst.instruction |= wrn;
11068               /* Bail out here; the instruction is now assembled.  */
11069               return;
11070             }
11071           }
11072       }
11073     /* Map 32 -> 0, etc.  */
11074     inst.operands[2].imm &= 0x1f;
11075     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
11076   }
11077 }
11078 \f
11079 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
11080    operations first, then control, shift, and load/store.  */
11081
11082 /* Insns like "foo X,Y,Z".  */
11083
11084 static void
11085 do_mav_triple (void)
11086 {
11087   inst.instruction |= inst.operands[0].reg << 16;
11088   inst.instruction |= inst.operands[1].reg;
11089   inst.instruction |= inst.operands[2].reg << 12;
11090 }
11091
11092 /* Insns like "foo W,X,Y,Z".
11093     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
11094
11095 static void
11096 do_mav_quad (void)
11097 {
11098   inst.instruction |= inst.operands[0].reg << 5;
11099   inst.instruction |= inst.operands[1].reg << 12;
11100   inst.instruction |= inst.operands[2].reg << 16;
11101   inst.instruction |= inst.operands[3].reg;
11102 }
11103
11104 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
11105 static void
11106 do_mav_dspsc (void)
11107 {
11108   inst.instruction |= inst.operands[1].reg << 12;
11109 }
11110
11111 /* Maverick shift immediate instructions.
11112    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
11113    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
11114
11115 static void
11116 do_mav_shift (void)
11117 {
11118   int imm = inst.operands[2].imm;
11119
11120   inst.instruction |= inst.operands[0].reg << 12;
11121   inst.instruction |= inst.operands[1].reg << 16;
11122
11123   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
11124      Bits 5-7 of the insn should have bits 4-6 of the immediate.
11125      Bit 4 should be 0.  */
11126   imm = (imm & 0xf) | ((imm & 0x70) << 1);
11127
11128   inst.instruction |= imm;
11129 }
11130 \f
11131 /* XScale instructions.  Also sorted arithmetic before move.  */
11132
11133 /* Xscale multiply-accumulate (argument parse)
11134      MIAcc   acc0,Rm,Rs
11135      MIAPHcc acc0,Rm,Rs
11136      MIAxycc acc0,Rm,Rs.  */
11137
11138 static void
11139 do_xsc_mia (void)
11140 {
11141   inst.instruction |= inst.operands[1].reg;
11142   inst.instruction |= inst.operands[2].reg << 12;
11143 }
11144
11145 /* Xscale move-accumulator-register (argument parse)
11146
11147      MARcc   acc0,RdLo,RdHi.  */
11148
11149 static void
11150 do_xsc_mar (void)
11151 {
11152   inst.instruction |= inst.operands[1].reg << 12;
11153   inst.instruction |= inst.operands[2].reg << 16;
11154 }
11155
11156 /* Xscale move-register-accumulator (argument parse)
11157
11158      MRAcc   RdLo,RdHi,acc0.  */
11159
11160 static void
11161 do_xsc_mra (void)
11162 {
11163   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
11164   inst.instruction |= inst.operands[0].reg << 12;
11165   inst.instruction |= inst.operands[1].reg << 16;
11166 }
11167 \f
11168 /* Encoding functions relevant only to Thumb.  */
11169
11170 /* inst.operands[i] is a shifted-register operand; encode
11171    it into inst.instruction in the format used by Thumb32.  */
11172
11173 static void
11174 encode_thumb32_shifted_operand (int i)
11175 {
11176   unsigned int value = inst.relocs[0].exp.X_add_number;
11177   unsigned int shift = inst.operands[i].shift_kind;
11178
11179   constraint (inst.operands[i].immisreg,
11180               _("shift by register not allowed in thumb mode"));
11181   inst.instruction |= inst.operands[i].reg;
11182   if (shift == SHIFT_RRX)
11183     inst.instruction |= SHIFT_ROR << 4;
11184   else
11185     {
11186       constraint (inst.relocs[0].exp.X_op != O_constant,
11187                   _("expression too complex"));
11188
11189       constraint (value > 32
11190                   || (value == 32 && (shift == SHIFT_LSL
11191                                       || shift == SHIFT_ROR)),
11192                   _("shift expression is too large"));
11193
11194       if (value == 0)
11195         shift = SHIFT_LSL;
11196       else if (value == 32)
11197         value = 0;
11198
11199       inst.instruction |= shift << 4;
11200       inst.instruction |= (value & 0x1c) << 10;
11201       inst.instruction |= (value & 0x03) << 6;
11202     }
11203 }
11204
11205
11206 /* inst.operands[i] was set up by parse_address.  Encode it into a
11207    Thumb32 format load or store instruction.  Reject forms that cannot
11208    be used with such instructions.  If is_t is true, reject forms that
11209    cannot be used with a T instruction; if is_d is true, reject forms
11210    that cannot be used with a D instruction.  If it is a store insn,
11211    reject PC in Rn.  */
11212
11213 static void
11214 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
11215 {
11216   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
11217
11218   constraint (!inst.operands[i].isreg,
11219               _("Instruction does not support =N addresses"));
11220
11221   inst.instruction |= inst.operands[i].reg << 16;
11222   if (inst.operands[i].immisreg)
11223     {
11224       constraint (is_pc, BAD_PC_ADDRESSING);
11225       constraint (is_t || is_d, _("cannot use register index with this instruction"));
11226       constraint (inst.operands[i].negative,
11227                   _("Thumb does not support negative register indexing"));
11228       constraint (inst.operands[i].postind,
11229                   _("Thumb does not support register post-indexing"));
11230       constraint (inst.operands[i].writeback,
11231                   _("Thumb does not support register indexing with writeback"));
11232       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
11233                   _("Thumb supports only LSL in shifted register indexing"));
11234
11235       inst.instruction |= inst.operands[i].imm;
11236       if (inst.operands[i].shifted)
11237         {
11238           constraint (inst.relocs[0].exp.X_op != O_constant,
11239                       _("expression too complex"));
11240           constraint (inst.relocs[0].exp.X_add_number < 0
11241                       || inst.relocs[0].exp.X_add_number > 3,
11242                       _("shift out of range"));
11243           inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
11244         }
11245       inst.relocs[0].type = BFD_RELOC_UNUSED;
11246     }
11247   else if (inst.operands[i].preind)
11248     {
11249       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
11250       constraint (is_t && inst.operands[i].writeback,
11251                   _("cannot use writeback with this instruction"));
11252       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
11253                   BAD_PC_ADDRESSING);
11254
11255       if (is_d)
11256         {
11257           inst.instruction |= 0x01000000;
11258           if (inst.operands[i].writeback)
11259             inst.instruction |= 0x00200000;
11260         }
11261       else
11262         {
11263           inst.instruction |= 0x00000c00;
11264           if (inst.operands[i].writeback)
11265             inst.instruction |= 0x00000100;
11266         }
11267       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11268     }
11269   else if (inst.operands[i].postind)
11270     {
11271       gas_assert (inst.operands[i].writeback);
11272       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
11273       constraint (is_t, _("cannot use post-indexing with this instruction"));
11274
11275       if (is_d)
11276         inst.instruction |= 0x00200000;
11277       else
11278         inst.instruction |= 0x00000900;
11279       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11280     }
11281   else /* unindexed - only for coprocessor */
11282     inst.error = _("instruction does not accept unindexed addressing");
11283 }
11284
11285 /* Table of Thumb instructions which exist in 16- and/or 32-bit
11286    encodings (the latter only in post-V6T2 cores).  The index is the
11287    value used in the insns table below.  When there is more than one
11288    possible 16-bit encoding for the instruction, this table always
11289    holds variant (1).
11290    Also contains several pseudo-instructions used during relaxation.  */
11291 #define T16_32_TAB                              \
11292   X(_adc,   4140, eb400000),                    \
11293   X(_adcs,  4140, eb500000),                    \
11294   X(_add,   1c00, eb000000),                    \
11295   X(_adds,  1c00, eb100000),                    \
11296   X(_addi,  0000, f1000000),                    \
11297   X(_addis, 0000, f1100000),                    \
11298   X(_add_pc,000f, f20f0000),                    \
11299   X(_add_sp,000d, f10d0000),                    \
11300   X(_adr,   000f, f20f0000),                    \
11301   X(_and,   4000, ea000000),                    \
11302   X(_ands,  4000, ea100000),                    \
11303   X(_asr,   1000, fa40f000),                    \
11304   X(_asrs,  1000, fa50f000),                    \
11305   X(_b,     e000, f000b000),                    \
11306   X(_bcond, d000, f0008000),                    \
11307   X(_bf,    0000, f040e001),                    \
11308   X(_bfcsel,0000, f000e001),                    \
11309   X(_bfx,   0000, f060e001),                    \
11310   X(_bfl,   0000, f000c001),                    \
11311   X(_bflx,  0000, f070e001),                    \
11312   X(_bic,   4380, ea200000),                    \
11313   X(_bics,  4380, ea300000),                    \
11314   X(_cinc,  0000, ea509000),                    \
11315   X(_cinv,  0000, ea50a000),                    \
11316   X(_cmn,   42c0, eb100f00),                    \
11317   X(_cmp,   2800, ebb00f00),                    \
11318   X(_cneg,  0000, ea50b000),                    \
11319   X(_cpsie, b660, f3af8400),                    \
11320   X(_cpsid, b670, f3af8600),                    \
11321   X(_cpy,   4600, ea4f0000),                    \
11322   X(_csel,  0000, ea508000),                    \
11323   X(_cset,  0000, ea5f900f),                    \
11324   X(_csetm, 0000, ea5fa00f),                    \
11325   X(_csinc, 0000, ea509000),                    \
11326   X(_csinv, 0000, ea50a000),                    \
11327   X(_csneg, 0000, ea50b000),                    \
11328   X(_dec_sp,80dd, f1ad0d00),                    \
11329   X(_dls,   0000, f040e001),                    \
11330   X(_dlstp, 0000, f000e001),                    \
11331   X(_eor,   4040, ea800000),                    \
11332   X(_eors,  4040, ea900000),                    \
11333   X(_inc_sp,00dd, f10d0d00),                    \
11334   X(_lctp,  0000, f00fe001),                    \
11335   X(_ldmia, c800, e8900000),                    \
11336   X(_ldr,   6800, f8500000),                    \
11337   X(_ldrb,  7800, f8100000),                    \
11338   X(_ldrh,  8800, f8300000),                    \
11339   X(_ldrsb, 5600, f9100000),                    \
11340   X(_ldrsh, 5e00, f9300000),                    \
11341   X(_ldr_pc,4800, f85f0000),                    \
11342   X(_ldr_pc2,4800, f85f0000),                   \
11343   X(_ldr_sp,9800, f85d0000),                    \
11344   X(_le,    0000, f00fc001),                    \
11345   X(_letp,  0000, f01fc001),                    \
11346   X(_lsl,   0000, fa00f000),                    \
11347   X(_lsls,  0000, fa10f000),                    \
11348   X(_lsr,   0800, fa20f000),                    \
11349   X(_lsrs,  0800, fa30f000),                    \
11350   X(_mov,   2000, ea4f0000),                    \
11351   X(_movs,  2000, ea5f0000),                    \
11352   X(_mul,   4340, fb00f000),                     \
11353   X(_muls,  4340, ffffffff), /* no 32b muls */  \
11354   X(_mvn,   43c0, ea6f0000),                    \
11355   X(_mvns,  43c0, ea7f0000),                    \
11356   X(_neg,   4240, f1c00000), /* rsb #0 */       \
11357   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
11358   X(_orr,   4300, ea400000),                    \
11359   X(_orrs,  4300, ea500000),                    \
11360   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
11361   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
11362   X(_rev,   ba00, fa90f080),                    \
11363   X(_rev16, ba40, fa90f090),                    \
11364   X(_revsh, bac0, fa90f0b0),                    \
11365   X(_ror,   41c0, fa60f000),                    \
11366   X(_rors,  41c0, fa70f000),                    \
11367   X(_sbc,   4180, eb600000),                    \
11368   X(_sbcs,  4180, eb700000),                    \
11369   X(_stmia, c000, e8800000),                    \
11370   X(_str,   6000, f8400000),                    \
11371   X(_strb,  7000, f8000000),                    \
11372   X(_strh,  8000, f8200000),                    \
11373   X(_str_sp,9000, f84d0000),                    \
11374   X(_sub,   1e00, eba00000),                    \
11375   X(_subs,  1e00, ebb00000),                    \
11376   X(_subi,  8000, f1a00000),                    \
11377   X(_subis, 8000, f1b00000),                    \
11378   X(_sxtb,  b240, fa4ff080),                    \
11379   X(_sxth,  b200, fa0ff080),                    \
11380   X(_tst,   4200, ea100f00),                    \
11381   X(_uxtb,  b2c0, fa5ff080),                    \
11382   X(_uxth,  b280, fa1ff080),                    \
11383   X(_nop,   bf00, f3af8000),                    \
11384   X(_yield, bf10, f3af8001),                    \
11385   X(_wfe,   bf20, f3af8002),                    \
11386   X(_wfi,   bf30, f3af8003),                    \
11387   X(_wls,   0000, f040c001),                    \
11388   X(_wlstp, 0000, f000c001),                    \
11389   X(_sev,   bf40, f3af8004),                    \
11390   X(_sevl,  bf50, f3af8005),                    \
11391   X(_udf,   de00, f7f0a000)
11392
11393 /* To catch errors in encoding functions, the codes are all offset by
11394    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
11395    as 16-bit instructions.  */
11396 #define X(a,b,c) T_MNEM##a
11397 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
11398 #undef X
11399
11400 #define X(a,b,c) 0x##b
11401 static const unsigned short thumb_op16[] = { T16_32_TAB };
11402 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
11403 #undef X
11404
11405 #define X(a,b,c) 0x##c
11406 static const unsigned int thumb_op32[] = { T16_32_TAB };
11407 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
11408 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
11409 #undef X
11410 #undef T16_32_TAB
11411
11412 /* Thumb instruction encoders, in alphabetical order.  */
11413
11414 /* ADDW or SUBW.  */
11415
11416 static void
11417 do_t_add_sub_w (void)
11418 {
11419   int Rd, Rn;
11420
11421   Rd = inst.operands[0].reg;
11422   Rn = inst.operands[1].reg;
11423
11424   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
11425      is the SP-{plus,minus}-immediate form of the instruction.  */
11426   if (Rn == REG_SP)
11427     constraint (Rd == REG_PC, BAD_PC);
11428   else
11429     reject_bad_reg (Rd);
11430
11431   inst.instruction |= (Rn << 16) | (Rd << 8);
11432   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11433 }
11434
11435 /* Parse an add or subtract instruction.  We get here with inst.instruction
11436    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
11437
11438 static void
11439 do_t_add_sub (void)
11440 {
11441   int Rd, Rs, Rn;
11442
11443   Rd = inst.operands[0].reg;
11444   Rs = (inst.operands[1].present
11445         ? inst.operands[1].reg    /* Rd, Rs, foo */
11446         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11447
11448   if (Rd == REG_PC)
11449     set_pred_insn_type_last ();
11450
11451   if (unified_syntax)
11452     {
11453       bfd_boolean flags;
11454       bfd_boolean narrow;
11455       int opcode;
11456
11457       flags = (inst.instruction == T_MNEM_adds
11458                || inst.instruction == T_MNEM_subs);
11459       if (flags)
11460         narrow = !in_pred_block ();
11461       else
11462         narrow = in_pred_block ();
11463       if (!inst.operands[2].isreg)
11464         {
11465           int add;
11466
11467           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11468             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11469
11470           add = (inst.instruction == T_MNEM_add
11471                  || inst.instruction == T_MNEM_adds);
11472           opcode = 0;
11473           if (inst.size_req != 4)
11474             {
11475               /* Attempt to use a narrow opcode, with relaxation if
11476                  appropriate.  */
11477               if (Rd == REG_SP && Rs == REG_SP && !flags)
11478                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
11479               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
11480                 opcode = T_MNEM_add_sp;
11481               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
11482                 opcode = T_MNEM_add_pc;
11483               else if (Rd <= 7 && Rs <= 7 && narrow)
11484                 {
11485                   if (flags)
11486                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
11487                   else
11488                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
11489                 }
11490               if (opcode)
11491                 {
11492                   inst.instruction = THUMB_OP16(opcode);
11493                   inst.instruction |= (Rd << 4) | Rs;
11494                   if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11495                       || (inst.relocs[0].type
11496                           > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC))
11497                   {
11498                     if (inst.size_req == 2)
11499                       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11500                     else
11501                       inst.relax = opcode;
11502                   }
11503                 }
11504               else
11505                 constraint (inst.size_req == 2, BAD_HIREG);
11506             }
11507           if (inst.size_req == 4
11508               || (inst.size_req != 2 && !opcode))
11509             {
11510               constraint ((inst.relocs[0].type
11511                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
11512                           && (inst.relocs[0].type
11513                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
11514                           THUMB1_RELOC_ONLY);
11515               if (Rd == REG_PC)
11516                 {
11517                   constraint (add, BAD_PC);
11518                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
11519                              _("only SUBS PC, LR, #const allowed"));
11520                   constraint (inst.relocs[0].exp.X_op != O_constant,
11521                               _("expression too complex"));
11522                   constraint (inst.relocs[0].exp.X_add_number < 0
11523                               || inst.relocs[0].exp.X_add_number > 0xff,
11524                              _("immediate value out of range"));
11525                   inst.instruction = T2_SUBS_PC_LR
11526                                      | inst.relocs[0].exp.X_add_number;
11527                   inst.relocs[0].type = BFD_RELOC_UNUSED;
11528                   return;
11529                 }
11530               else if (Rs == REG_PC)
11531                 {
11532                   /* Always use addw/subw.  */
11533                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
11534                   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11535                 }
11536               else
11537                 {
11538                   inst.instruction = THUMB_OP32 (inst.instruction);
11539                   inst.instruction = (inst.instruction & 0xe1ffffff)
11540                                      | 0x10000000;
11541                   if (flags)
11542                     inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11543                   else
11544                     inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_IMM;
11545                 }
11546               inst.instruction |= Rd << 8;
11547               inst.instruction |= Rs << 16;
11548             }
11549         }
11550       else
11551         {
11552           unsigned int value = inst.relocs[0].exp.X_add_number;
11553           unsigned int shift = inst.operands[2].shift_kind;
11554
11555           Rn = inst.operands[2].reg;
11556           /* See if we can do this with a 16-bit instruction.  */
11557           if (!inst.operands[2].shifted && inst.size_req != 4)
11558             {
11559               if (Rd > 7 || Rs > 7 || Rn > 7)
11560                 narrow = FALSE;
11561
11562               if (narrow)
11563                 {
11564                   inst.instruction = ((inst.instruction == T_MNEM_adds
11565                                        || inst.instruction == T_MNEM_add)
11566                                       ? T_OPCODE_ADD_R3
11567                                       : T_OPCODE_SUB_R3);
11568                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11569                   return;
11570                 }
11571
11572               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
11573                 {
11574                   /* Thumb-1 cores (except v6-M) require at least one high
11575                      register in a narrow non flag setting add.  */
11576                   if (Rd > 7 || Rn > 7
11577                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
11578                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
11579                     {
11580                       if (Rd == Rn)
11581                         {
11582                           Rn = Rs;
11583                           Rs = Rd;
11584                         }
11585                       inst.instruction = T_OPCODE_ADD_HI;
11586                       inst.instruction |= (Rd & 8) << 4;
11587                       inst.instruction |= (Rd & 7);
11588                       inst.instruction |= Rn << 3;
11589                       return;
11590                     }
11591                 }
11592             }
11593
11594           constraint (Rd == REG_PC, BAD_PC);
11595           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11596             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11597           constraint (Rs == REG_PC, BAD_PC);
11598           reject_bad_reg (Rn);
11599
11600           /* If we get here, it can't be done in 16 bits.  */
11601           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
11602                       _("shift must be constant"));
11603           inst.instruction = THUMB_OP32 (inst.instruction);
11604           inst.instruction |= Rd << 8;
11605           inst.instruction |= Rs << 16;
11606           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
11607                       _("shift value over 3 not allowed in thumb mode"));
11608           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
11609                       _("only LSL shift allowed in thumb mode"));
11610           encode_thumb32_shifted_operand (2);
11611         }
11612     }
11613   else
11614     {
11615       constraint (inst.instruction == T_MNEM_adds
11616                   || inst.instruction == T_MNEM_subs,
11617                   BAD_THUMB32);
11618
11619       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
11620         {
11621           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
11622                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
11623                       BAD_HIREG);
11624
11625           inst.instruction = (inst.instruction == T_MNEM_add
11626                               ? 0x0000 : 0x8000);
11627           inst.instruction |= (Rd << 4) | Rs;
11628           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11629           return;
11630         }
11631
11632       Rn = inst.operands[2].reg;
11633       constraint (inst.operands[2].shifted, _("unshifted register required"));
11634
11635       /* We now have Rd, Rs, and Rn set to registers.  */
11636       if (Rd > 7 || Rs > 7 || Rn > 7)
11637         {
11638           /* Can't do this for SUB.      */
11639           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
11640           inst.instruction = T_OPCODE_ADD_HI;
11641           inst.instruction |= (Rd & 8) << 4;
11642           inst.instruction |= (Rd & 7);
11643           if (Rs == Rd)
11644             inst.instruction |= Rn << 3;
11645           else if (Rn == Rd)
11646             inst.instruction |= Rs << 3;
11647           else
11648             constraint (1, _("dest must overlap one source register"));
11649         }
11650       else
11651         {
11652           inst.instruction = (inst.instruction == T_MNEM_add
11653                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
11654           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11655         }
11656     }
11657 }
11658
11659 static void
11660 do_t_adr (void)
11661 {
11662   unsigned Rd;
11663
11664   Rd = inst.operands[0].reg;
11665   reject_bad_reg (Rd);
11666
11667   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
11668     {
11669       /* Defer to section relaxation.  */
11670       inst.relax = inst.instruction;
11671       inst.instruction = THUMB_OP16 (inst.instruction);
11672       inst.instruction |= Rd << 4;
11673     }
11674   else if (unified_syntax && inst.size_req != 2)
11675     {
11676       /* Generate a 32-bit opcode.  */
11677       inst.instruction = THUMB_OP32 (inst.instruction);
11678       inst.instruction |= Rd << 8;
11679       inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_PC12;
11680       inst.relocs[0].pc_rel = 1;
11681     }
11682   else
11683     {
11684       /* Generate a 16-bit opcode.  */
11685       inst.instruction = THUMB_OP16 (inst.instruction);
11686       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11687       inst.relocs[0].exp.X_add_number -= 4; /* PC relative adjust.  */
11688       inst.relocs[0].pc_rel = 1;
11689       inst.instruction |= Rd << 4;
11690     }
11691
11692   if (inst.relocs[0].exp.X_op == O_symbol
11693       && inst.relocs[0].exp.X_add_symbol != NULL
11694       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11695       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11696     inst.relocs[0].exp.X_add_number += 1;
11697 }
11698
11699 /* Arithmetic instructions for which there is just one 16-bit
11700    instruction encoding, and it allows only two low registers.
11701    For maximal compatibility with ARM syntax, we allow three register
11702    operands even when Thumb-32 instructions are not available, as long
11703    as the first two are identical.  For instance, both "sbc r0,r1" and
11704    "sbc r0,r0,r1" are allowed.  */
11705 static void
11706 do_t_arit3 (void)
11707 {
11708   int Rd, Rs, Rn;
11709
11710   Rd = inst.operands[0].reg;
11711   Rs = (inst.operands[1].present
11712         ? inst.operands[1].reg    /* Rd, Rs, foo */
11713         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11714   Rn = inst.operands[2].reg;
11715
11716   reject_bad_reg (Rd);
11717   reject_bad_reg (Rs);
11718   if (inst.operands[2].isreg)
11719     reject_bad_reg (Rn);
11720
11721   if (unified_syntax)
11722     {
11723       if (!inst.operands[2].isreg)
11724         {
11725           /* For an immediate, we always generate a 32-bit opcode;
11726              section relaxation will shrink it later if possible.  */
11727           inst.instruction = THUMB_OP32 (inst.instruction);
11728           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11729           inst.instruction |= Rd << 8;
11730           inst.instruction |= Rs << 16;
11731           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11732         }
11733       else
11734         {
11735           bfd_boolean narrow;
11736
11737           /* See if we can do this with a 16-bit instruction.  */
11738           if (THUMB_SETS_FLAGS (inst.instruction))
11739             narrow = !in_pred_block ();
11740           else
11741             narrow = in_pred_block ();
11742
11743           if (Rd > 7 || Rn > 7 || Rs > 7)
11744             narrow = FALSE;
11745           if (inst.operands[2].shifted)
11746             narrow = FALSE;
11747           if (inst.size_req == 4)
11748             narrow = FALSE;
11749
11750           if (narrow
11751               && Rd == Rs)
11752             {
11753               inst.instruction = THUMB_OP16 (inst.instruction);
11754               inst.instruction |= Rd;
11755               inst.instruction |= Rn << 3;
11756               return;
11757             }
11758
11759           /* If we get here, it can't be done in 16 bits.  */
11760           constraint (inst.operands[2].shifted
11761                       && inst.operands[2].immisreg,
11762                       _("shift must be constant"));
11763           inst.instruction = THUMB_OP32 (inst.instruction);
11764           inst.instruction |= Rd << 8;
11765           inst.instruction |= Rs << 16;
11766           encode_thumb32_shifted_operand (2);
11767         }
11768     }
11769   else
11770     {
11771       /* On its face this is a lie - the instruction does set the
11772          flags.  However, the only supported mnemonic in this mode
11773          says it doesn't.  */
11774       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11775
11776       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11777                   _("unshifted register required"));
11778       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11779       constraint (Rd != Rs,
11780                   _("dest and source1 must be the same register"));
11781
11782       inst.instruction = THUMB_OP16 (inst.instruction);
11783       inst.instruction |= Rd;
11784       inst.instruction |= Rn << 3;
11785     }
11786 }
11787
11788 /* Similarly, but for instructions where the arithmetic operation is
11789    commutative, so we can allow either of them to be different from
11790    the destination operand in a 16-bit instruction.  For instance, all
11791    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
11792    accepted.  */
11793 static void
11794 do_t_arit3c (void)
11795 {
11796   int Rd, Rs, Rn;
11797
11798   Rd = inst.operands[0].reg;
11799   Rs = (inst.operands[1].present
11800         ? inst.operands[1].reg    /* Rd, Rs, foo */
11801         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11802   Rn = inst.operands[2].reg;
11803
11804   reject_bad_reg (Rd);
11805   reject_bad_reg (Rs);
11806   if (inst.operands[2].isreg)
11807     reject_bad_reg (Rn);
11808
11809   if (unified_syntax)
11810     {
11811       if (!inst.operands[2].isreg)
11812         {
11813           /* For an immediate, we always generate a 32-bit opcode;
11814              section relaxation will shrink it later if possible.  */
11815           inst.instruction = THUMB_OP32 (inst.instruction);
11816           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11817           inst.instruction |= Rd << 8;
11818           inst.instruction |= Rs << 16;
11819           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11820         }
11821       else
11822         {
11823           bfd_boolean narrow;
11824
11825           /* See if we can do this with a 16-bit instruction.  */
11826           if (THUMB_SETS_FLAGS (inst.instruction))
11827             narrow = !in_pred_block ();
11828           else
11829             narrow = in_pred_block ();
11830
11831           if (Rd > 7 || Rn > 7 || Rs > 7)
11832             narrow = FALSE;
11833           if (inst.operands[2].shifted)
11834             narrow = FALSE;
11835           if (inst.size_req == 4)
11836             narrow = FALSE;
11837
11838           if (narrow)
11839             {
11840               if (Rd == Rs)
11841                 {
11842                   inst.instruction = THUMB_OP16 (inst.instruction);
11843                   inst.instruction |= Rd;
11844                   inst.instruction |= Rn << 3;
11845                   return;
11846                 }
11847               if (Rd == Rn)
11848                 {
11849                   inst.instruction = THUMB_OP16 (inst.instruction);
11850                   inst.instruction |= Rd;
11851                   inst.instruction |= Rs << 3;
11852                   return;
11853                 }
11854             }
11855
11856           /* If we get here, it can't be done in 16 bits.  */
11857           constraint (inst.operands[2].shifted
11858                       && inst.operands[2].immisreg,
11859                       _("shift must be constant"));
11860           inst.instruction = THUMB_OP32 (inst.instruction);
11861           inst.instruction |= Rd << 8;
11862           inst.instruction |= Rs << 16;
11863           encode_thumb32_shifted_operand (2);
11864         }
11865     }
11866   else
11867     {
11868       /* On its face this is a lie - the instruction does set the
11869          flags.  However, the only supported mnemonic in this mode
11870          says it doesn't.  */
11871       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11872
11873       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11874                   _("unshifted register required"));
11875       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11876
11877       inst.instruction = THUMB_OP16 (inst.instruction);
11878       inst.instruction |= Rd;
11879
11880       if (Rd == Rs)
11881         inst.instruction |= Rn << 3;
11882       else if (Rd == Rn)
11883         inst.instruction |= Rs << 3;
11884       else
11885         constraint (1, _("dest must overlap one source register"));
11886     }
11887 }
11888
11889 static void
11890 do_t_bfc (void)
11891 {
11892   unsigned Rd;
11893   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11894   constraint (msb > 32, _("bit-field extends past end of register"));
11895   /* The instruction encoding stores the LSB and MSB,
11896      not the LSB and width.  */
11897   Rd = inst.operands[0].reg;
11898   reject_bad_reg (Rd);
11899   inst.instruction |= Rd << 8;
11900   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11901   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11902   inst.instruction |= msb - 1;
11903 }
11904
11905 static void
11906 do_t_bfi (void)
11907 {
11908   int Rd, Rn;
11909   unsigned int msb;
11910
11911   Rd = inst.operands[0].reg;
11912   reject_bad_reg (Rd);
11913
11914   /* #0 in second position is alternative syntax for bfc, which is
11915      the same instruction but with REG_PC in the Rm field.  */
11916   if (!inst.operands[1].isreg)
11917     Rn = REG_PC;
11918   else
11919     {
11920       Rn = inst.operands[1].reg;
11921       reject_bad_reg (Rn);
11922     }
11923
11924   msb = inst.operands[2].imm + inst.operands[3].imm;
11925   constraint (msb > 32, _("bit-field extends past end of register"));
11926   /* The instruction encoding stores the LSB and MSB,
11927      not the LSB and width.  */
11928   inst.instruction |= Rd << 8;
11929   inst.instruction |= Rn << 16;
11930   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11931   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11932   inst.instruction |= msb - 1;
11933 }
11934
11935 static void
11936 do_t_bfx (void)
11937 {
11938   unsigned Rd, Rn;
11939
11940   Rd = inst.operands[0].reg;
11941   Rn = inst.operands[1].reg;
11942
11943   reject_bad_reg (Rd);
11944   reject_bad_reg (Rn);
11945
11946   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11947               _("bit-field extends past end of register"));
11948   inst.instruction |= Rd << 8;
11949   inst.instruction |= Rn << 16;
11950   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11951   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11952   inst.instruction |= inst.operands[3].imm - 1;
11953 }
11954
11955 /* ARM V5 Thumb BLX (argument parse)
11956         BLX <target_addr>       which is BLX(1)
11957         BLX <Rm>                which is BLX(2)
11958    Unfortunately, there are two different opcodes for this mnemonic.
11959    So, the insns[].value is not used, and the code here zaps values
11960         into inst.instruction.
11961
11962    ??? How to take advantage of the additional two bits of displacement
11963    available in Thumb32 mode?  Need new relocation?  */
11964
11965 static void
11966 do_t_blx (void)
11967 {
11968   set_pred_insn_type_last ();
11969
11970   if (inst.operands[0].isreg)
11971     {
11972       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11973       /* We have a register, so this is BLX(2).  */
11974       inst.instruction |= inst.operands[0].reg << 3;
11975     }
11976   else
11977     {
11978       /* No register.  This must be BLX(1).  */
11979       inst.instruction = 0xf000e800;
11980       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11981     }
11982 }
11983
11984 static void
11985 do_t_branch (void)
11986 {
11987   int opcode;
11988   int cond;
11989   bfd_reloc_code_real_type reloc;
11990
11991   cond = inst.cond;
11992   set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);
11993
11994   if (in_pred_block ())
11995     {
11996       /* Conditional branches inside IT blocks are encoded as unconditional
11997          branches.  */
11998       cond = COND_ALWAYS;
11999     }
12000   else
12001     cond = inst.cond;
12002
12003   if (cond != COND_ALWAYS)
12004     opcode = T_MNEM_bcond;
12005   else
12006     opcode = inst.instruction;
12007
12008   if (unified_syntax
12009       && (inst.size_req == 4
12010           || (inst.size_req != 2
12011               && (inst.operands[0].hasreloc
12012                   || inst.relocs[0].exp.X_op == O_constant))))
12013     {
12014       inst.instruction = THUMB_OP32(opcode);
12015       if (cond == COND_ALWAYS)
12016         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
12017       else
12018         {
12019           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
12020                       _("selected architecture does not support "
12021                         "wide conditional branch instruction"));
12022
12023           gas_assert (cond != 0xF);
12024           inst.instruction |= cond << 22;
12025           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
12026         }
12027     }
12028   else
12029     {
12030       inst.instruction = THUMB_OP16(opcode);
12031       if (cond == COND_ALWAYS)
12032         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
12033       else
12034         {
12035           inst.instruction |= cond << 8;
12036           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
12037         }
12038       /* Allow section relaxation.  */
12039       if (unified_syntax && inst.size_req != 2)
12040         inst.relax = opcode;
12041     }
12042   inst.relocs[0].type = reloc;
12043   inst.relocs[0].pc_rel = 1;
12044 }
12045
12046 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
12047    between the two is the maximum immediate allowed - which is passed in
12048    RANGE.  */
12049 static void
12050 do_t_bkpt_hlt1 (int range)
12051 {
12052   constraint (inst.cond != COND_ALWAYS,
12053               _("instruction is always unconditional"));
12054   if (inst.operands[0].present)
12055     {
12056       constraint (inst.operands[0].imm > range,
12057                   _("immediate value out of range"));
12058       inst.instruction |= inst.operands[0].imm;
12059     }
12060
12061   set_pred_insn_type (NEUTRAL_IT_INSN);
12062 }
12063
12064 static void
12065 do_t_hlt (void)
12066 {
12067   do_t_bkpt_hlt1 (63);
12068 }
12069
12070 static void
12071 do_t_bkpt (void)
12072 {
12073   do_t_bkpt_hlt1 (255);
12074 }
12075
12076 static void
12077 do_t_branch23 (void)
12078 {
12079   set_pred_insn_type_last ();
12080   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
12081
12082   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
12083      this file.  We used to simply ignore the PLT reloc type here --
12084      the branch encoding is now needed to deal with TLSCALL relocs.
12085      So if we see a PLT reloc now, put it back to how it used to be to
12086      keep the preexisting behaviour.  */
12087   if (inst.relocs[0].type == BFD_RELOC_ARM_PLT32)
12088     inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH23;
12089
12090 #if defined(OBJ_COFF)
12091   /* If the destination of the branch is a defined symbol which does not have
12092      the THUMB_FUNC attribute, then we must be calling a function which has
12093      the (interfacearm) attribute.  We look for the Thumb entry point to that
12094      function and change the branch to refer to that function instead.  */
12095   if (   inst.relocs[0].exp.X_op == O_symbol
12096       && inst.relocs[0].exp.X_add_symbol != NULL
12097       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
12098       && ! THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
12099     inst.relocs[0].exp.X_add_symbol
12100       = find_real_start (inst.relocs[0].exp.X_add_symbol);
12101 #endif
12102 }
12103
12104 static void
12105 do_t_bx (void)
12106 {
12107   set_pred_insn_type_last ();
12108   inst.instruction |= inst.operands[0].reg << 3;
12109   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
12110      should cause the alignment to be checked once it is known.  This is
12111      because BX PC only works if the instruction is word aligned.  */
12112 }
12113
12114 static void
12115 do_t_bxj (void)
12116 {
12117   int Rm;
12118
12119   set_pred_insn_type_last ();
12120   Rm = inst.operands[0].reg;
12121   reject_bad_reg (Rm);
12122   inst.instruction |= Rm << 16;
12123 }
12124
12125 static void
12126 do_t_clz (void)
12127 {
12128   unsigned Rd;
12129   unsigned Rm;
12130
12131   Rd = inst.operands[0].reg;
12132   Rm = inst.operands[1].reg;
12133
12134   reject_bad_reg (Rd);
12135   reject_bad_reg (Rm);
12136
12137   inst.instruction |= Rd << 8;
12138   inst.instruction |= Rm << 16;
12139   inst.instruction |= Rm;
12140 }
12141
12142 /* For the Armv8.1-M conditional instructions.  */
12143 static void
12144 do_t_cond (void)
12145 {
12146   unsigned Rd, Rn, Rm;
12147   signed int cond;
12148
12149   constraint (inst.cond != COND_ALWAYS, BAD_COND);
12150
12151   Rd = inst.operands[0].reg;
12152   switch (inst.instruction)
12153     {
12154       case T_MNEM_csinc:
12155       case T_MNEM_csinv:
12156       case T_MNEM_csneg:
12157       case T_MNEM_csel:
12158         Rn = inst.operands[1].reg;
12159         Rm = inst.operands[2].reg;
12160         cond = inst.operands[3].imm;
12161         constraint (Rn == REG_SP, BAD_SP);
12162         constraint (Rm == REG_SP, BAD_SP);
12163         break;
12164
12165       case T_MNEM_cinc:
12166       case T_MNEM_cinv:
12167       case T_MNEM_cneg:
12168         Rn = inst.operands[1].reg;
12169         cond = inst.operands[2].imm;
12170         /* Invert the last bit to invert the cond.  */
12171         cond = TOGGLE_BIT (cond, 0);
12172         constraint (Rn == REG_SP, BAD_SP);
12173         Rm = Rn;
12174         break;
12175
12176       case T_MNEM_csetm:
12177       case T_MNEM_cset:
12178         cond = inst.operands[1].imm;
12179         /* Invert the last bit to invert the cond.  */
12180         cond = TOGGLE_BIT (cond, 0);
12181         Rn = REG_PC;
12182         Rm = REG_PC;
12183         break;
12184
12185       default: abort ();
12186     }
12187
12188   set_pred_insn_type (OUTSIDE_PRED_INSN);
12189   inst.instruction = THUMB_OP32 (inst.instruction);
12190   inst.instruction |= Rd << 8;
12191   inst.instruction |= Rn << 16;
12192   inst.instruction |= Rm;
12193   inst.instruction |= cond << 4;
12194 }
12195
12196 static void
12197 do_t_csdb (void)
12198 {
12199   set_pred_insn_type (OUTSIDE_PRED_INSN);
12200 }
12201
12202 static void
12203 do_t_cps (void)
12204 {
12205   set_pred_insn_type (OUTSIDE_PRED_INSN);
12206   inst.instruction |= inst.operands[0].imm;
12207 }
12208
12209 static void
12210 do_t_cpsi (void)
12211 {
12212   set_pred_insn_type (OUTSIDE_PRED_INSN);
12213   if (unified_syntax
12214       && (inst.operands[1].present || inst.size_req == 4)
12215       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
12216     {
12217       unsigned int imod = (inst.instruction & 0x0030) >> 4;
12218       inst.instruction = 0xf3af8000;
12219       inst.instruction |= imod << 9;
12220       inst.instruction |= inst.operands[0].imm << 5;
12221       if (inst.operands[1].present)
12222         inst.instruction |= 0x100 | inst.operands[1].imm;
12223     }
12224   else
12225     {
12226       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
12227                   && (inst.operands[0].imm & 4),
12228                   _("selected processor does not support 'A' form "
12229                     "of this instruction"));
12230       constraint (inst.operands[1].present || inst.size_req == 4,
12231                   _("Thumb does not support the 2-argument "
12232                     "form of this instruction"));
12233       inst.instruction |= inst.operands[0].imm;
12234     }
12235 }
12236
12237 /* THUMB CPY instruction (argument parse).  */
12238
12239 static void
12240 do_t_cpy (void)
12241 {
12242   if (inst.size_req == 4)
12243     {
12244       inst.instruction = THUMB_OP32 (T_MNEM_mov);
12245       inst.instruction |= inst.operands[0].reg << 8;
12246       inst.instruction |= inst.operands[1].reg;
12247     }
12248   else
12249     {
12250       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
12251       inst.instruction |= (inst.operands[0].reg & 0x7);
12252       inst.instruction |= inst.operands[1].reg << 3;
12253     }
12254 }
12255
12256 static void
12257 do_t_cbz (void)
12258 {
12259   set_pred_insn_type (OUTSIDE_PRED_INSN);
12260   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12261   inst.instruction |= inst.operands[0].reg;
12262   inst.relocs[0].pc_rel = 1;
12263   inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH7;
12264 }
12265
12266 static void
12267 do_t_dbg (void)
12268 {
12269   inst.instruction |= inst.operands[0].imm;
12270 }
12271
12272 static void
12273 do_t_div (void)
12274 {
12275   unsigned Rd, Rn, Rm;
12276
12277   Rd = inst.operands[0].reg;
12278   Rn = (inst.operands[1].present
12279         ? inst.operands[1].reg : Rd);
12280   Rm = inst.operands[2].reg;
12281
12282   reject_bad_reg (Rd);
12283   reject_bad_reg (Rn);
12284   reject_bad_reg (Rm);
12285
12286   inst.instruction |= Rd << 8;
12287   inst.instruction |= Rn << 16;
12288   inst.instruction |= Rm;
12289 }
12290
12291 static void
12292 do_t_hint (void)
12293 {
12294   if (unified_syntax && inst.size_req == 4)
12295     inst.instruction = THUMB_OP32 (inst.instruction);
12296   else
12297     inst.instruction = THUMB_OP16 (inst.instruction);
12298 }
12299
12300 static void
12301 do_t_it (void)
12302 {
12303   unsigned int cond = inst.operands[0].imm;
12304
12305   set_pred_insn_type (IT_INSN);
12306   now_pred.mask = (inst.instruction & 0xf) | 0x10;
12307   now_pred.cc = cond;
12308   now_pred.warn_deprecated = FALSE;
12309   now_pred.type = SCALAR_PRED;
12310
12311   /* If the condition is a negative condition, invert the mask.  */
12312   if ((cond & 0x1) == 0x0)
12313     {
12314       unsigned int mask = inst.instruction & 0x000f;
12315
12316       if ((mask & 0x7) == 0)
12317         {
12318           /* No conversion needed.  */
12319           now_pred.block_length = 1;
12320         }
12321       else if ((mask & 0x3) == 0)
12322         {
12323           mask ^= 0x8;
12324           now_pred.block_length = 2;
12325         }
12326       else if ((mask & 0x1) == 0)
12327         {
12328           mask ^= 0xC;
12329           now_pred.block_length = 3;
12330         }
12331       else
12332         {
12333           mask ^= 0xE;
12334           now_pred.block_length = 4;
12335         }
12336
12337       inst.instruction &= 0xfff0;
12338       inst.instruction |= mask;
12339     }
12340
12341   inst.instruction |= cond << 4;
12342 }
12343
12344 /* Helper function used for both push/pop and ldm/stm.  */
12345 static void
12346 encode_thumb2_multi (bfd_boolean do_io, int base, unsigned mask,
12347                      bfd_boolean writeback)
12348 {
12349   bfd_boolean load, store;
12350
12351   gas_assert (base != -1 || !do_io);
12352   load = do_io && ((inst.instruction & (1 << 20)) != 0);
12353   store = do_io && !load;
12354
12355   if (mask & (1 << 13))
12356     inst.error =  _("SP not allowed in register list");
12357
12358   if (do_io && (mask & (1 << base)) != 0
12359       && writeback)
12360     inst.error = _("having the base register in the register list when "
12361                    "using write back is UNPREDICTABLE");
12362
12363   if (load)
12364     {
12365       if (mask & (1 << 15))
12366         {
12367           if (mask & (1 << 14))
12368             inst.error = _("LR and PC should not both be in register list");
12369           else
12370             set_pred_insn_type_last ();
12371         }
12372     }
12373   else if (store)
12374     {
12375       if (mask & (1 << 15))
12376         inst.error = _("PC not allowed in register list");
12377     }
12378
12379   if (do_io && ((mask & (mask - 1)) == 0))
12380     {
12381       /* Single register transfers implemented as str/ldr.  */
12382       if (writeback)
12383         {
12384           if (inst.instruction & (1 << 23))
12385             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
12386           else
12387             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
12388         }
12389       else
12390         {
12391           if (inst.instruction & (1 << 23))
12392             inst.instruction = 0x00800000; /* ia -> [base] */
12393           else
12394             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
12395         }
12396
12397       inst.instruction |= 0xf8400000;
12398       if (load)
12399         inst.instruction |= 0x00100000;
12400
12401       mask = ffs (mask) - 1;
12402       mask <<= 12;
12403     }
12404   else if (writeback)
12405     inst.instruction |= WRITE_BACK;
12406
12407   inst.instruction |= mask;
12408   if (do_io)
12409     inst.instruction |= base << 16;
12410 }
12411
12412 static void
12413 do_t_ldmstm (void)
12414 {
12415   /* This really doesn't seem worth it.  */
12416   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
12417               _("expression too complex"));
12418   constraint (inst.operands[1].writeback,
12419               _("Thumb load/store multiple does not support {reglist}^"));
12420
12421   if (unified_syntax)
12422     {
12423       bfd_boolean narrow;
12424       unsigned mask;
12425
12426       narrow = FALSE;
12427       /* See if we can use a 16-bit instruction.  */
12428       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
12429           && inst.size_req != 4
12430           && !(inst.operands[1].imm & ~0xff))
12431         {
12432           mask = 1 << inst.operands[0].reg;
12433
12434           if (inst.operands[0].reg <= 7)
12435             {
12436               if (inst.instruction == T_MNEM_stmia
12437                   ? inst.operands[0].writeback
12438                   : (inst.operands[0].writeback
12439                      == !(inst.operands[1].imm & mask)))
12440                 {
12441                   if (inst.instruction == T_MNEM_stmia
12442                       && (inst.operands[1].imm & mask)
12443                       && (inst.operands[1].imm & (mask - 1)))
12444                     as_warn (_("value stored for r%d is UNKNOWN"),
12445                              inst.operands[0].reg);
12446
12447                   inst.instruction = THUMB_OP16 (inst.instruction);
12448                   inst.instruction |= inst.operands[0].reg << 8;
12449                   inst.instruction |= inst.operands[1].imm;
12450                   narrow = TRUE;
12451                 }
12452               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12453                 {
12454                   /* This means 1 register in reg list one of 3 situations:
12455                      1. Instruction is stmia, but without writeback.
12456                      2. lmdia without writeback, but with Rn not in
12457                         reglist.
12458                      3. ldmia with writeback, but with Rn in reglist.
12459                      Case 3 is UNPREDICTABLE behaviour, so we handle
12460                      case 1 and 2 which can be converted into a 16-bit
12461                      str or ldr. The SP cases are handled below.  */
12462                   unsigned long opcode;
12463                   /* First, record an error for Case 3.  */
12464                   if (inst.operands[1].imm & mask
12465                       && inst.operands[0].writeback)
12466                     inst.error =
12467                         _("having the base register in the register list when "
12468                           "using write back is UNPREDICTABLE");
12469
12470                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
12471                                                              : T_MNEM_ldr);
12472                   inst.instruction = THUMB_OP16 (opcode);
12473                   inst.instruction |= inst.operands[0].reg << 3;
12474                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
12475                   narrow = TRUE;
12476                 }
12477             }
12478           else if (inst.operands[0] .reg == REG_SP)
12479             {
12480               if (inst.operands[0].writeback)
12481                 {
12482                   inst.instruction =
12483                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12484                                     ? T_MNEM_push : T_MNEM_pop);
12485                   inst.instruction |= inst.operands[1].imm;
12486                   narrow = TRUE;
12487                 }
12488               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12489                 {
12490                   inst.instruction =
12491                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12492                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
12493                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
12494                   narrow = TRUE;
12495                 }
12496             }
12497         }
12498
12499       if (!narrow)
12500         {
12501           if (inst.instruction < 0xffff)
12502             inst.instruction = THUMB_OP32 (inst.instruction);
12503
12504           encode_thumb2_multi (TRUE /* do_io */, inst.operands[0].reg,
12505                                inst.operands[1].imm,
12506                                inst.operands[0].writeback);
12507         }
12508     }
12509   else
12510     {
12511       constraint (inst.operands[0].reg > 7
12512                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
12513       constraint (inst.instruction != T_MNEM_ldmia
12514                   && inst.instruction != T_MNEM_stmia,
12515                   _("Thumb-2 instruction only valid in unified syntax"));
12516       if (inst.instruction == T_MNEM_stmia)
12517         {
12518           if (!inst.operands[0].writeback)
12519             as_warn (_("this instruction will write back the base register"));
12520           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
12521               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
12522             as_warn (_("value stored for r%d is UNKNOWN"),
12523                      inst.operands[0].reg);
12524         }
12525       else
12526         {
12527           if (!inst.operands[0].writeback
12528               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
12529             as_warn (_("this instruction will write back the base register"));
12530           else if (inst.operands[0].writeback
12531                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
12532             as_warn (_("this instruction will not write back the base register"));
12533         }
12534
12535       inst.instruction = THUMB_OP16 (inst.instruction);
12536       inst.instruction |= inst.operands[0].reg << 8;
12537       inst.instruction |= inst.operands[1].imm;
12538     }
12539 }
12540
12541 static void
12542 do_t_ldrex (void)
12543 {
12544   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
12545               || inst.operands[1].postind || inst.operands[1].writeback
12546               || inst.operands[1].immisreg || inst.operands[1].shifted
12547               || inst.operands[1].negative,
12548               BAD_ADDR_MODE);
12549
12550   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
12551
12552   inst.instruction |= inst.operands[0].reg << 12;
12553   inst.instruction |= inst.operands[1].reg << 16;
12554   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
12555 }
12556
12557 static void
12558 do_t_ldrexd (void)
12559 {
12560   if (!inst.operands[1].present)
12561     {
12562       constraint (inst.operands[0].reg == REG_LR,
12563                   _("r14 not allowed as first register "
12564                     "when second register is omitted"));
12565       inst.operands[1].reg = inst.operands[0].reg + 1;
12566     }
12567   constraint (inst.operands[0].reg == inst.operands[1].reg,
12568               BAD_OVERLAP);
12569
12570   inst.instruction |= inst.operands[0].reg << 12;
12571   inst.instruction |= inst.operands[1].reg << 8;
12572   inst.instruction |= inst.operands[2].reg << 16;
12573 }
12574
12575 static void
12576 do_t_ldst (void)
12577 {
12578   unsigned long opcode;
12579   int Rn;
12580
12581   if (inst.operands[0].isreg
12582       && !inst.operands[0].preind
12583       && inst.operands[0].reg == REG_PC)
12584     set_pred_insn_type_last ();
12585
12586   opcode = inst.instruction;
12587   if (unified_syntax)
12588     {
12589       if (!inst.operands[1].isreg)
12590         {
12591           if (opcode <= 0xffff)
12592             inst.instruction = THUMB_OP32 (opcode);
12593           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12594             return;
12595         }
12596       if (inst.operands[1].isreg
12597           && !inst.operands[1].writeback
12598           && !inst.operands[1].shifted && !inst.operands[1].postind
12599           && !inst.operands[1].negative && inst.operands[0].reg <= 7
12600           && opcode <= 0xffff
12601           && inst.size_req != 4)
12602         {
12603           /* Insn may have a 16-bit form.  */
12604           Rn = inst.operands[1].reg;
12605           if (inst.operands[1].immisreg)
12606             {
12607               inst.instruction = THUMB_OP16 (opcode);
12608               /* [Rn, Rik] */
12609               if (Rn <= 7 && inst.operands[1].imm <= 7)
12610                 goto op16;
12611               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
12612                 reject_bad_reg (inst.operands[1].imm);
12613             }
12614           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
12615                     && opcode != T_MNEM_ldrsb)
12616                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
12617                    || (Rn == REG_SP && opcode == T_MNEM_str))
12618             {
12619               /* [Rn, #const] */
12620               if (Rn > 7)
12621                 {
12622                   if (Rn == REG_PC)
12623                     {
12624                       if (inst.relocs[0].pc_rel)
12625                         opcode = T_MNEM_ldr_pc2;
12626                       else
12627                         opcode = T_MNEM_ldr_pc;
12628                     }
12629                   else
12630                     {
12631                       if (opcode == T_MNEM_ldr)
12632                         opcode = T_MNEM_ldr_sp;
12633                       else
12634                         opcode = T_MNEM_str_sp;
12635                     }
12636                   inst.instruction = inst.operands[0].reg << 8;
12637                 }
12638               else
12639                 {
12640                   inst.instruction = inst.operands[0].reg;
12641                   inst.instruction |= inst.operands[1].reg << 3;
12642                 }
12643               inst.instruction |= THUMB_OP16 (opcode);
12644               if (inst.size_req == 2)
12645                 inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12646               else
12647                 inst.relax = opcode;
12648               return;
12649             }
12650         }
12651       /* Definitely a 32-bit variant.  */
12652
12653       /* Warning for Erratum 752419.  */
12654       if (opcode == T_MNEM_ldr
12655           && inst.operands[0].reg == REG_SP
12656           && inst.operands[1].writeback == 1
12657           && !inst.operands[1].immisreg)
12658         {
12659           if (no_cpu_selected ()
12660               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
12661                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
12662                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
12663             as_warn (_("This instruction may be unpredictable "
12664                        "if executed on M-profile cores "
12665                        "with interrupts enabled."));
12666         }
12667
12668       /* Do some validations regarding addressing modes.  */
12669       if (inst.operands[1].immisreg)
12670         reject_bad_reg (inst.operands[1].imm);
12671
12672       constraint (inst.operands[1].writeback == 1
12673                   && inst.operands[0].reg == inst.operands[1].reg,
12674                   BAD_OVERLAP);
12675
12676       inst.instruction = THUMB_OP32 (opcode);
12677       inst.instruction |= inst.operands[0].reg << 12;
12678       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
12679       check_ldr_r15_aligned ();
12680       return;
12681     }
12682
12683   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12684
12685   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
12686     {
12687       /* Only [Rn,Rm] is acceptable.  */
12688       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
12689       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
12690                   || inst.operands[1].postind || inst.operands[1].shifted
12691                   || inst.operands[1].negative,
12692                   _("Thumb does not support this addressing mode"));
12693       inst.instruction = THUMB_OP16 (inst.instruction);
12694       goto op16;
12695     }
12696
12697   inst.instruction = THUMB_OP16 (inst.instruction);
12698   if (!inst.operands[1].isreg)
12699     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12700       return;
12701
12702   constraint (!inst.operands[1].preind
12703               || inst.operands[1].shifted
12704               || inst.operands[1].writeback,
12705               _("Thumb does not support this addressing mode"));
12706   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
12707     {
12708       constraint (inst.instruction & 0x0600,
12709                   _("byte or halfword not valid for base register"));
12710       constraint (inst.operands[1].reg == REG_PC
12711                   && !(inst.instruction & THUMB_LOAD_BIT),
12712                   _("r15 based store not allowed"));
12713       constraint (inst.operands[1].immisreg,
12714                   _("invalid base register for register offset"));
12715
12716       if (inst.operands[1].reg == REG_PC)
12717         inst.instruction = T_OPCODE_LDR_PC;
12718       else if (inst.instruction & THUMB_LOAD_BIT)
12719         inst.instruction = T_OPCODE_LDR_SP;
12720       else
12721         inst.instruction = T_OPCODE_STR_SP;
12722
12723       inst.instruction |= inst.operands[0].reg << 8;
12724       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12725       return;
12726     }
12727
12728   constraint (inst.operands[1].reg > 7, BAD_HIREG);
12729   if (!inst.operands[1].immisreg)
12730     {
12731       /* Immediate offset.  */
12732       inst.instruction |= inst.operands[0].reg;
12733       inst.instruction |= inst.operands[1].reg << 3;
12734       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12735       return;
12736     }
12737
12738   /* Register offset.  */
12739   constraint (inst.operands[1].imm > 7, BAD_HIREG);
12740   constraint (inst.operands[1].negative,
12741               _("Thumb does not support this addressing mode"));
12742
12743  op16:
12744   switch (inst.instruction)
12745     {
12746     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
12747     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
12748     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
12749     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
12750     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
12751     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
12752     case 0x5600 /* ldrsb */:
12753     case 0x5e00 /* ldrsh */: break;
12754     default: abort ();
12755     }
12756
12757   inst.instruction |= inst.operands[0].reg;
12758   inst.instruction |= inst.operands[1].reg << 3;
12759   inst.instruction |= inst.operands[1].imm << 6;
12760 }
12761
12762 static void
12763 do_t_ldstd (void)
12764 {
12765   if (!inst.operands[1].present)
12766     {
12767       inst.operands[1].reg = inst.operands[0].reg + 1;
12768       constraint (inst.operands[0].reg == REG_LR,
12769                   _("r14 not allowed here"));
12770       constraint (inst.operands[0].reg == REG_R12,
12771                   _("r12 not allowed here"));
12772     }
12773
12774   if (inst.operands[2].writeback
12775       && (inst.operands[0].reg == inst.operands[2].reg
12776       || inst.operands[1].reg == inst.operands[2].reg))
12777     as_warn (_("base register written back, and overlaps "
12778                "one of transfer registers"));
12779
12780   inst.instruction |= inst.operands[0].reg << 12;
12781   inst.instruction |= inst.operands[1].reg << 8;
12782   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
12783 }
12784
12785 static void
12786 do_t_ldstt (void)
12787 {
12788   inst.instruction |= inst.operands[0].reg << 12;
12789   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
12790 }
12791
12792 static void
12793 do_t_mla (void)
12794 {
12795   unsigned Rd, Rn, Rm, Ra;
12796
12797   Rd = inst.operands[0].reg;
12798   Rn = inst.operands[1].reg;
12799   Rm = inst.operands[2].reg;
12800   Ra = inst.operands[3].reg;
12801
12802   reject_bad_reg (Rd);
12803   reject_bad_reg (Rn);
12804   reject_bad_reg (Rm);
12805   reject_bad_reg (Ra);
12806
12807   inst.instruction |= Rd << 8;
12808   inst.instruction |= Rn << 16;
12809   inst.instruction |= Rm;
12810   inst.instruction |= Ra << 12;
12811 }
12812
12813 static void
12814 do_t_mlal (void)
12815 {
12816   unsigned RdLo, RdHi, Rn, Rm;
12817
12818   RdLo = inst.operands[0].reg;
12819   RdHi = inst.operands[1].reg;
12820   Rn = inst.operands[2].reg;
12821   Rm = inst.operands[3].reg;
12822
12823   reject_bad_reg (RdLo);
12824   reject_bad_reg (RdHi);
12825   reject_bad_reg (Rn);
12826   reject_bad_reg (Rm);
12827
12828   inst.instruction |= RdLo << 12;
12829   inst.instruction |= RdHi << 8;
12830   inst.instruction |= Rn << 16;
12831   inst.instruction |= Rm;
12832 }
12833
12834 static void
12835 do_t_mov_cmp (void)
12836 {
12837   unsigned Rn, Rm;
12838
12839   Rn = inst.operands[0].reg;
12840   Rm = inst.operands[1].reg;
12841
12842   if (Rn == REG_PC)
12843     set_pred_insn_type_last ();
12844
12845   if (unified_syntax)
12846     {
12847       int r0off = (inst.instruction == T_MNEM_mov
12848                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
12849       unsigned long opcode;
12850       bfd_boolean narrow;
12851       bfd_boolean low_regs;
12852
12853       low_regs = (Rn <= 7 && Rm <= 7);
12854       opcode = inst.instruction;
12855       if (in_pred_block ())
12856         narrow = opcode != T_MNEM_movs;
12857       else
12858         narrow = opcode != T_MNEM_movs || low_regs;
12859       if (inst.size_req == 4
12860           || inst.operands[1].shifted)
12861         narrow = FALSE;
12862
12863       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
12864       if (opcode == T_MNEM_movs && inst.operands[1].isreg
12865           && !inst.operands[1].shifted
12866           && Rn == REG_PC
12867           && Rm == REG_LR)
12868         {
12869           inst.instruction = T2_SUBS_PC_LR;
12870           return;
12871         }
12872
12873       if (opcode == T_MNEM_cmp)
12874         {
12875           constraint (Rn == REG_PC, BAD_PC);
12876           if (narrow)
12877             {
12878               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
12879                  but valid.  */
12880               warn_deprecated_sp (Rm);
12881               /* R15 was documented as a valid choice for Rm in ARMv6,
12882                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
12883                  tools reject R15, so we do too.  */
12884               constraint (Rm == REG_PC, BAD_PC);
12885             }
12886           else
12887             reject_bad_reg (Rm);
12888         }
12889       else if (opcode == T_MNEM_mov
12890                || opcode == T_MNEM_movs)
12891         {
12892           if (inst.operands[1].isreg)
12893             {
12894               if (opcode == T_MNEM_movs)
12895                 {
12896                   reject_bad_reg (Rn);
12897                   reject_bad_reg (Rm);
12898                 }
12899               else if (narrow)
12900                 {
12901                   /* This is mov.n.  */
12902                   if ((Rn == REG_SP || Rn == REG_PC)
12903                       && (Rm == REG_SP || Rm == REG_PC))
12904                     {
12905                       as_tsktsk (_("Use of r%u as a source register is "
12906                                  "deprecated when r%u is the destination "
12907                                  "register."), Rm, Rn);
12908                     }
12909                 }
12910               else
12911                 {
12912                   /* This is mov.w.  */
12913                   constraint (Rn == REG_PC, BAD_PC);
12914                   constraint (Rm == REG_PC, BAD_PC);
12915                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12916                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
12917                 }
12918             }
12919           else
12920             reject_bad_reg (Rn);
12921         }
12922
12923       if (!inst.operands[1].isreg)
12924         {
12925           /* Immediate operand.  */
12926           if (!in_pred_block () && opcode == T_MNEM_mov)
12927             narrow = 0;
12928           if (low_regs && narrow)
12929             {
12930               inst.instruction = THUMB_OP16 (opcode);
12931               inst.instruction |= Rn << 8;
12932               if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12933                   || inst.relocs[0].type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12934                 {
12935                   if (inst.size_req == 2)
12936                     inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12937                   else
12938                     inst.relax = opcode;
12939                 }
12940             }
12941           else
12942             {
12943               constraint ((inst.relocs[0].type
12944                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
12945                           && (inst.relocs[0].type
12946                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
12947                           THUMB1_RELOC_ONLY);
12948
12949               inst.instruction = THUMB_OP32 (inst.instruction);
12950               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12951               inst.instruction |= Rn << r0off;
12952               inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12953             }
12954         }
12955       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12956                && (inst.instruction == T_MNEM_mov
12957                    || inst.instruction == T_MNEM_movs))
12958         {
12959           /* Register shifts are encoded as separate shift instructions.  */
12960           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12961
12962           if (in_pred_block ())
12963             narrow = !flags;
12964           else
12965             narrow = flags;
12966
12967           if (inst.size_req == 4)
12968             narrow = FALSE;
12969
12970           if (!low_regs || inst.operands[1].imm > 7)
12971             narrow = FALSE;
12972
12973           if (Rn != Rm)
12974             narrow = FALSE;
12975
12976           switch (inst.operands[1].shift_kind)
12977             {
12978             case SHIFT_LSL:
12979               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12980               break;
12981             case SHIFT_ASR:
12982               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12983               break;
12984             case SHIFT_LSR:
12985               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12986               break;
12987             case SHIFT_ROR:
12988               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12989               break;
12990             default:
12991               abort ();
12992             }
12993
12994           inst.instruction = opcode;
12995           if (narrow)
12996             {
12997               inst.instruction |= Rn;
12998               inst.instruction |= inst.operands[1].imm << 3;
12999             }
13000           else
13001             {
13002               if (flags)
13003                 inst.instruction |= CONDS_BIT;
13004
13005               inst.instruction |= Rn << 8;
13006               inst.instruction |= Rm << 16;
13007               inst.instruction |= inst.operands[1].imm;
13008             }
13009         }
13010       else if (!narrow)
13011         {
13012           /* Some mov with immediate shift have narrow variants.
13013              Register shifts are handled above.  */
13014           if (low_regs && inst.operands[1].shifted
13015               && (inst.instruction == T_MNEM_mov
13016                   || inst.instruction == T_MNEM_movs))
13017             {
13018               if (in_pred_block ())
13019                 narrow = (inst.instruction == T_MNEM_mov);
13020               else
13021                 narrow = (inst.instruction == T_MNEM_movs);
13022             }
13023
13024           if (narrow)
13025             {
13026               switch (inst.operands[1].shift_kind)
13027                 {
13028                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13029                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13030                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13031                 default: narrow = FALSE; break;
13032                 }
13033             }
13034
13035           if (narrow)
13036             {
13037               inst.instruction |= Rn;
13038               inst.instruction |= Rm << 3;
13039               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13040             }
13041           else
13042             {
13043               inst.instruction = THUMB_OP32 (inst.instruction);
13044               inst.instruction |= Rn << r0off;
13045               encode_thumb32_shifted_operand (1);
13046             }
13047         }
13048       else
13049         switch (inst.instruction)
13050           {
13051           case T_MNEM_mov:
13052             /* In v4t or v5t a move of two lowregs produces unpredictable
13053                results. Don't allow this.  */
13054             if (low_regs)
13055               {
13056                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
13057                             "MOV Rd, Rs with two low registers is not "
13058                             "permitted on this architecture");
13059                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
13060                                         arm_ext_v6);
13061               }
13062
13063             inst.instruction = T_OPCODE_MOV_HR;
13064             inst.instruction |= (Rn & 0x8) << 4;
13065             inst.instruction |= (Rn & 0x7);
13066             inst.instruction |= Rm << 3;
13067             break;
13068
13069           case T_MNEM_movs:
13070             /* We know we have low registers at this point.
13071                Generate LSLS Rd, Rs, #0.  */
13072             inst.instruction = T_OPCODE_LSL_I;
13073             inst.instruction |= Rn;
13074             inst.instruction |= Rm << 3;
13075             break;
13076
13077           case T_MNEM_cmp:
13078             if (low_regs)
13079               {
13080                 inst.instruction = T_OPCODE_CMP_LR;
13081                 inst.instruction |= Rn;
13082                 inst.instruction |= Rm << 3;
13083               }
13084             else
13085               {
13086                 inst.instruction = T_OPCODE_CMP_HR;
13087                 inst.instruction |= (Rn & 0x8) << 4;
13088                 inst.instruction |= (Rn & 0x7);
13089                 inst.instruction |= Rm << 3;
13090               }
13091             break;
13092           }
13093       return;
13094     }
13095
13096   inst.instruction = THUMB_OP16 (inst.instruction);
13097
13098   /* PR 10443: Do not silently ignore shifted operands.  */
13099   constraint (inst.operands[1].shifted,
13100               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
13101
13102   if (inst.operands[1].isreg)
13103     {
13104       if (Rn < 8 && Rm < 8)
13105         {
13106           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
13107              since a MOV instruction produces unpredictable results.  */
13108           if (inst.instruction == T_OPCODE_MOV_I8)
13109             inst.instruction = T_OPCODE_ADD_I3;
13110           else
13111             inst.instruction = T_OPCODE_CMP_LR;
13112
13113           inst.instruction |= Rn;
13114           inst.instruction |= Rm << 3;
13115         }
13116       else
13117         {
13118           if (inst.instruction == T_OPCODE_MOV_I8)
13119             inst.instruction = T_OPCODE_MOV_HR;
13120           else
13121             inst.instruction = T_OPCODE_CMP_HR;
13122           do_t_cpy ();
13123         }
13124     }
13125   else
13126     {
13127       constraint (Rn > 7,
13128                   _("only lo regs allowed with immediate"));
13129       inst.instruction |= Rn << 8;
13130       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
13131     }
13132 }
13133
13134 static void
13135 do_t_mov16 (void)
13136 {
13137   unsigned Rd;
13138   bfd_vma imm;
13139   bfd_boolean top;
13140
13141   top = (inst.instruction & 0x00800000) != 0;
13142   if (inst.relocs[0].type == BFD_RELOC_ARM_MOVW)
13143     {
13144       constraint (top, _(":lower16: not allowed in this instruction"));
13145       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVW;
13146     }
13147   else if (inst.relocs[0].type == BFD_RELOC_ARM_MOVT)
13148     {
13149       constraint (!top, _(":upper16: not allowed in this instruction"));
13150       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVT;
13151     }
13152
13153   Rd = inst.operands[0].reg;
13154   reject_bad_reg (Rd);
13155
13156   inst.instruction |= Rd << 8;
13157   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
13158     {
13159       imm = inst.relocs[0].exp.X_add_number;
13160       inst.instruction |= (imm & 0xf000) << 4;
13161       inst.instruction |= (imm & 0x0800) << 15;
13162       inst.instruction |= (imm & 0x0700) << 4;
13163       inst.instruction |= (imm & 0x00ff);
13164     }
13165 }
13166
13167 static void
13168 do_t_mvn_tst (void)
13169 {
13170   unsigned Rn, Rm;
13171
13172   Rn = inst.operands[0].reg;
13173   Rm = inst.operands[1].reg;
13174
13175   if (inst.instruction == T_MNEM_cmp
13176       || inst.instruction == T_MNEM_cmn)
13177     constraint (Rn == REG_PC, BAD_PC);
13178   else
13179     reject_bad_reg (Rn);
13180   reject_bad_reg (Rm);
13181
13182   if (unified_syntax)
13183     {
13184       int r0off = (inst.instruction == T_MNEM_mvn
13185                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
13186       bfd_boolean narrow;
13187
13188       if (inst.size_req == 4
13189           || inst.instruction > 0xffff
13190           || inst.operands[1].shifted
13191           || Rn > 7 || Rm > 7)
13192         narrow = FALSE;
13193       else if (inst.instruction == T_MNEM_cmn
13194                || inst.instruction == T_MNEM_tst)
13195         narrow = TRUE;
13196       else if (THUMB_SETS_FLAGS (inst.instruction))
13197         narrow = !in_pred_block ();
13198       else
13199         narrow = in_pred_block ();
13200
13201       if (!inst.operands[1].isreg)
13202         {
13203           /* For an immediate, we always generate a 32-bit opcode;
13204              section relaxation will shrink it later if possible.  */
13205           if (inst.instruction < 0xffff)
13206             inst.instruction = THUMB_OP32 (inst.instruction);
13207           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13208           inst.instruction |= Rn << r0off;
13209           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13210         }
13211       else
13212         {
13213           /* See if we can do this with a 16-bit instruction.  */
13214           if (narrow)
13215             {
13216               inst.instruction = THUMB_OP16 (inst.instruction);
13217               inst.instruction |= Rn;
13218               inst.instruction |= Rm << 3;
13219             }
13220           else
13221             {
13222               constraint (inst.operands[1].shifted
13223                           && inst.operands[1].immisreg,
13224                           _("shift must be constant"));
13225               if (inst.instruction < 0xffff)
13226                 inst.instruction = THUMB_OP32 (inst.instruction);
13227               inst.instruction |= Rn << r0off;
13228               encode_thumb32_shifted_operand (1);
13229             }
13230         }
13231     }
13232   else
13233     {
13234       constraint (inst.instruction > 0xffff
13235                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
13236       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
13237                   _("unshifted register required"));
13238       constraint (Rn > 7 || Rm > 7,
13239                   BAD_HIREG);
13240
13241       inst.instruction = THUMB_OP16 (inst.instruction);
13242       inst.instruction |= Rn;
13243       inst.instruction |= Rm << 3;
13244     }
13245 }
13246
13247 static void
13248 do_t_mrs (void)
13249 {
13250   unsigned Rd;
13251
13252   if (do_vfp_nsyn_mrs () == SUCCESS)
13253     return;
13254
13255   Rd = inst.operands[0].reg;
13256   reject_bad_reg (Rd);
13257   inst.instruction |= Rd << 8;
13258
13259   if (inst.operands[1].isreg)
13260     {
13261       unsigned br = inst.operands[1].reg;
13262       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
13263         as_bad (_("bad register for mrs"));
13264
13265       inst.instruction |= br & (0xf << 16);
13266       inst.instruction |= (br & 0x300) >> 4;
13267       inst.instruction |= (br & SPSR_BIT) >> 2;
13268     }
13269   else
13270     {
13271       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13272
13273       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13274         {
13275           /* PR gas/12698:  The constraint is only applied for m_profile.
13276              If the user has specified -march=all, we want to ignore it as
13277              we are building for any CPU type, including non-m variants.  */
13278           bfd_boolean m_profile =
13279             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13280           constraint ((flags != 0) && m_profile, _("selected processor does "
13281                                                    "not support requested special purpose register"));
13282         }
13283       else
13284         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
13285            devices).  */
13286         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
13287                     _("'APSR', 'CPSR' or 'SPSR' expected"));
13288
13289       inst.instruction |= (flags & SPSR_BIT) >> 2;
13290       inst.instruction |= inst.operands[1].imm & 0xff;
13291       inst.instruction |= 0xf0000;
13292     }
13293 }
13294
13295 static void
13296 do_t_msr (void)
13297 {
13298   int flags;
13299   unsigned Rn;
13300
13301   if (do_vfp_nsyn_msr () == SUCCESS)
13302     return;
13303
13304   constraint (!inst.operands[1].isreg,
13305               _("Thumb encoding does not support an immediate here"));
13306
13307   if (inst.operands[0].isreg)
13308     flags = (int)(inst.operands[0].reg);
13309   else
13310     flags = inst.operands[0].imm;
13311
13312   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13313     {
13314       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13315
13316       /* PR gas/12698:  The constraint is only applied for m_profile.
13317          If the user has specified -march=all, we want to ignore it as
13318          we are building for any CPU type, including non-m variants.  */
13319       bfd_boolean m_profile =
13320         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13321       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13322            && (bits & ~(PSR_s | PSR_f)) != 0)
13323           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13324               && bits != PSR_f)) && m_profile,
13325           _("selected processor does not support requested special "
13326             "purpose register"));
13327     }
13328   else
13329      constraint ((flags & 0xff) != 0, _("selected processor does not support "
13330                  "requested special purpose register"));
13331
13332   Rn = inst.operands[1].reg;
13333   reject_bad_reg (Rn);
13334
13335   inst.instruction |= (flags & SPSR_BIT) >> 2;
13336   inst.instruction |= (flags & 0xf0000) >> 8;
13337   inst.instruction |= (flags & 0x300) >> 4;
13338   inst.instruction |= (flags & 0xff);
13339   inst.instruction |= Rn << 16;
13340 }
13341
13342 static void
13343 do_t_mul (void)
13344 {
13345   bfd_boolean narrow;
13346   unsigned Rd, Rn, Rm;
13347
13348   if (!inst.operands[2].present)
13349     inst.operands[2].reg = inst.operands[0].reg;
13350
13351   Rd = inst.operands[0].reg;
13352   Rn = inst.operands[1].reg;
13353   Rm = inst.operands[2].reg;
13354
13355   if (unified_syntax)
13356     {
13357       if (inst.size_req == 4
13358           || (Rd != Rn
13359               && Rd != Rm)
13360           || Rn > 7
13361           || Rm > 7)
13362         narrow = FALSE;
13363       else if (inst.instruction == T_MNEM_muls)
13364         narrow = !in_pred_block ();
13365       else
13366         narrow = in_pred_block ();
13367     }
13368   else
13369     {
13370       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
13371       constraint (Rn > 7 || Rm > 7,
13372                   BAD_HIREG);
13373       narrow = TRUE;
13374     }
13375
13376   if (narrow)
13377     {
13378       /* 16-bit MULS/Conditional MUL.  */
13379       inst.instruction = THUMB_OP16 (inst.instruction);
13380       inst.instruction |= Rd;
13381
13382       if (Rd == Rn)
13383         inst.instruction |= Rm << 3;
13384       else if (Rd == Rm)
13385         inst.instruction |= Rn << 3;
13386       else
13387         constraint (1, _("dest must overlap one source register"));
13388     }
13389   else
13390     {
13391       constraint (inst.instruction != T_MNEM_mul,
13392                   _("Thumb-2 MUL must not set flags"));
13393       /* 32-bit MUL.  */
13394       inst.instruction = THUMB_OP32 (inst.instruction);
13395       inst.instruction |= Rd << 8;
13396       inst.instruction |= Rn << 16;
13397       inst.instruction |= Rm << 0;
13398
13399       reject_bad_reg (Rd);
13400       reject_bad_reg (Rn);
13401       reject_bad_reg (Rm);
13402     }
13403 }
13404
13405 static void
13406 do_t_mull (void)
13407 {
13408   unsigned RdLo, RdHi, Rn, Rm;
13409
13410   RdLo = inst.operands[0].reg;
13411   RdHi = inst.operands[1].reg;
13412   Rn = inst.operands[2].reg;
13413   Rm = inst.operands[3].reg;
13414
13415   reject_bad_reg (RdLo);
13416   reject_bad_reg (RdHi);
13417   reject_bad_reg (Rn);
13418   reject_bad_reg (Rm);
13419
13420   inst.instruction |= RdLo << 12;
13421   inst.instruction |= RdHi << 8;
13422   inst.instruction |= Rn << 16;
13423   inst.instruction |= Rm;
13424
13425  if (RdLo == RdHi)
13426     as_tsktsk (_("rdhi and rdlo must be different"));
13427 }
13428
13429 static void
13430 do_t_nop (void)
13431 {
13432   set_pred_insn_type (NEUTRAL_IT_INSN);
13433
13434   if (unified_syntax)
13435     {
13436       if (inst.size_req == 4 || inst.operands[0].imm > 15)
13437         {
13438           inst.instruction = THUMB_OP32 (inst.instruction);
13439           inst.instruction |= inst.operands[0].imm;
13440         }
13441       else
13442         {
13443           /* PR9722: Check for Thumb2 availability before
13444              generating a thumb2 nop instruction.  */
13445           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
13446             {
13447               inst.instruction = THUMB_OP16 (inst.instruction);
13448               inst.instruction |= inst.operands[0].imm << 4;
13449             }
13450           else
13451             inst.instruction = 0x46c0;
13452         }
13453     }
13454   else
13455     {
13456       constraint (inst.operands[0].present,
13457                   _("Thumb does not support NOP with hints"));
13458       inst.instruction = 0x46c0;
13459     }
13460 }
13461
13462 static void
13463 do_t_neg (void)
13464 {
13465   if (unified_syntax)
13466     {
13467       bfd_boolean narrow;
13468
13469       if (THUMB_SETS_FLAGS (inst.instruction))
13470         narrow = !in_pred_block ();
13471       else
13472         narrow = in_pred_block ();
13473       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13474         narrow = FALSE;
13475       if (inst.size_req == 4)
13476         narrow = FALSE;
13477
13478       if (!narrow)
13479         {
13480           inst.instruction = THUMB_OP32 (inst.instruction);
13481           inst.instruction |= inst.operands[0].reg << 8;
13482           inst.instruction |= inst.operands[1].reg << 16;
13483         }
13484       else
13485         {
13486           inst.instruction = THUMB_OP16 (inst.instruction);
13487           inst.instruction |= inst.operands[0].reg;
13488           inst.instruction |= inst.operands[1].reg << 3;
13489         }
13490     }
13491   else
13492     {
13493       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
13494                   BAD_HIREG);
13495       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13496
13497       inst.instruction = THUMB_OP16 (inst.instruction);
13498       inst.instruction |= inst.operands[0].reg;
13499       inst.instruction |= inst.operands[1].reg << 3;
13500     }
13501 }
13502
13503 static void
13504 do_t_orn (void)
13505 {
13506   unsigned Rd, Rn;
13507
13508   Rd = inst.operands[0].reg;
13509   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
13510
13511   reject_bad_reg (Rd);
13512   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
13513   reject_bad_reg (Rn);
13514
13515   inst.instruction |= Rd << 8;
13516   inst.instruction |= Rn << 16;
13517
13518   if (!inst.operands[2].isreg)
13519     {
13520       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13521       inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13522     }
13523   else
13524     {
13525       unsigned Rm;
13526
13527       Rm = inst.operands[2].reg;
13528       reject_bad_reg (Rm);
13529
13530       constraint (inst.operands[2].shifted
13531                   && inst.operands[2].immisreg,
13532                   _("shift must be constant"));
13533       encode_thumb32_shifted_operand (2);
13534     }
13535 }
13536
13537 static void
13538 do_t_pkhbt (void)
13539 {
13540   unsigned Rd, Rn, Rm;
13541
13542   Rd = inst.operands[0].reg;
13543   Rn = inst.operands[1].reg;
13544   Rm = inst.operands[2].reg;
13545
13546   reject_bad_reg (Rd);
13547   reject_bad_reg (Rn);
13548   reject_bad_reg (Rm);
13549
13550   inst.instruction |= Rd << 8;
13551   inst.instruction |= Rn << 16;
13552   inst.instruction |= Rm;
13553   if (inst.operands[3].present)
13554     {
13555       unsigned int val = inst.relocs[0].exp.X_add_number;
13556       constraint (inst.relocs[0].exp.X_op != O_constant,
13557                   _("expression too complex"));
13558       inst.instruction |= (val & 0x1c) << 10;
13559       inst.instruction |= (val & 0x03) << 6;
13560     }
13561 }
13562
13563 static void
13564 do_t_pkhtb (void)
13565 {
13566   if (!inst.operands[3].present)
13567     {
13568       unsigned Rtmp;
13569
13570       inst.instruction &= ~0x00000020;
13571
13572       /* PR 10168.  Swap the Rm and Rn registers.  */
13573       Rtmp = inst.operands[1].reg;
13574       inst.operands[1].reg = inst.operands[2].reg;
13575       inst.operands[2].reg = Rtmp;
13576     }
13577   do_t_pkhbt ();
13578 }
13579
13580 static void
13581 do_t_pld (void)
13582 {
13583   if (inst.operands[0].immisreg)
13584     reject_bad_reg (inst.operands[0].imm);
13585
13586   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
13587 }
13588
13589 static void
13590 do_t_push_pop (void)
13591 {
13592   unsigned mask;
13593
13594   constraint (inst.operands[0].writeback,
13595               _("push/pop do not support {reglist}^"));
13596   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
13597               _("expression too complex"));
13598
13599   mask = inst.operands[0].imm;
13600   if (inst.size_req != 4 && (mask & ~0xff) == 0)
13601     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
13602   else if (inst.size_req != 4
13603            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
13604                                        ? REG_LR : REG_PC)))
13605     {
13606       inst.instruction = THUMB_OP16 (inst.instruction);
13607       inst.instruction |= THUMB_PP_PC_LR;
13608       inst.instruction |= mask & 0xff;
13609     }
13610   else if (unified_syntax)
13611     {
13612       inst.instruction = THUMB_OP32 (inst.instruction);
13613       encode_thumb2_multi (TRUE /* do_io */, 13, mask, TRUE);
13614     }
13615   else
13616     {
13617       inst.error = _("invalid register list to push/pop instruction");
13618       return;
13619     }
13620 }
13621
13622 static void
13623 do_t_clrm (void)
13624 {
13625   if (unified_syntax)
13626     encode_thumb2_multi (FALSE /* do_io */, -1, inst.operands[0].imm, FALSE);
13627   else
13628     {
13629       inst.error = _("invalid register list to push/pop instruction");
13630       return;
13631     }
13632 }
13633
13634 static void
13635 do_t_vscclrm (void)
13636 {
13637   if (inst.operands[0].issingle)
13638     {
13639       inst.instruction |= (inst.operands[0].reg & 0x1) << 22;
13640       inst.instruction |= (inst.operands[0].reg & 0x1e) << 11;
13641       inst.instruction |= inst.operands[0].imm;
13642     }
13643   else
13644     {
13645       inst.instruction |= (inst.operands[0].reg & 0x10) << 18;
13646       inst.instruction |= (inst.operands[0].reg & 0xf) << 12;
13647       inst.instruction |= 1 << 8;
13648       inst.instruction |= inst.operands[0].imm << 1;
13649     }
13650 }
13651
13652 static void
13653 do_t_rbit (void)
13654 {
13655   unsigned Rd, Rm;
13656
13657   Rd = inst.operands[0].reg;
13658   Rm = inst.operands[1].reg;
13659
13660   reject_bad_reg (Rd);
13661   reject_bad_reg (Rm);
13662
13663   inst.instruction |= Rd << 8;
13664   inst.instruction |= Rm << 16;
13665   inst.instruction |= Rm;
13666 }
13667
13668 static void
13669 do_t_rev (void)
13670 {
13671   unsigned Rd, Rm;
13672
13673   Rd = inst.operands[0].reg;
13674   Rm = inst.operands[1].reg;
13675
13676   reject_bad_reg (Rd);
13677   reject_bad_reg (Rm);
13678
13679   if (Rd <= 7 && Rm <= 7
13680       && inst.size_req != 4)
13681     {
13682       inst.instruction = THUMB_OP16 (inst.instruction);
13683       inst.instruction |= Rd;
13684       inst.instruction |= Rm << 3;
13685     }
13686   else if (unified_syntax)
13687     {
13688       inst.instruction = THUMB_OP32 (inst.instruction);
13689       inst.instruction |= Rd << 8;
13690       inst.instruction |= Rm << 16;
13691       inst.instruction |= Rm;
13692     }
13693   else
13694     inst.error = BAD_HIREG;
13695 }
13696
13697 static void
13698 do_t_rrx (void)
13699 {
13700   unsigned Rd, Rm;
13701
13702   Rd = inst.operands[0].reg;
13703   Rm = inst.operands[1].reg;
13704
13705   reject_bad_reg (Rd);
13706   reject_bad_reg (Rm);
13707
13708   inst.instruction |= Rd << 8;
13709   inst.instruction |= Rm;
13710 }
13711
13712 static void
13713 do_t_rsb (void)
13714 {
13715   unsigned Rd, Rs;
13716
13717   Rd = inst.operands[0].reg;
13718   Rs = (inst.operands[1].present
13719         ? inst.operands[1].reg    /* Rd, Rs, foo */
13720         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
13721
13722   reject_bad_reg (Rd);
13723   reject_bad_reg (Rs);
13724   if (inst.operands[2].isreg)
13725     reject_bad_reg (inst.operands[2].reg);
13726
13727   inst.instruction |= Rd << 8;
13728   inst.instruction |= Rs << 16;
13729   if (!inst.operands[2].isreg)
13730     {
13731       bfd_boolean narrow;
13732
13733       if ((inst.instruction & 0x00100000) != 0)
13734         narrow = !in_pred_block ();
13735       else
13736         narrow = in_pred_block ();
13737
13738       if (Rd > 7 || Rs > 7)
13739         narrow = FALSE;
13740
13741       if (inst.size_req == 4 || !unified_syntax)
13742         narrow = FALSE;
13743
13744       if (inst.relocs[0].exp.X_op != O_constant
13745           || inst.relocs[0].exp.X_add_number != 0)
13746         narrow = FALSE;
13747
13748       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
13749          relaxation, but it doesn't seem worth the hassle.  */
13750       if (narrow)
13751         {
13752           inst.relocs[0].type = BFD_RELOC_UNUSED;
13753           inst.instruction = THUMB_OP16 (T_MNEM_negs);
13754           inst.instruction |= Rs << 3;
13755           inst.instruction |= Rd;
13756         }
13757       else
13758         {
13759           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13760           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13761         }
13762     }
13763   else
13764     encode_thumb32_shifted_operand (2);
13765 }
13766
13767 static void
13768 do_t_setend (void)
13769 {
13770   if (warn_on_deprecated
13771       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13772       as_tsktsk (_("setend use is deprecated for ARMv8"));
13773
13774   set_pred_insn_type (OUTSIDE_PRED_INSN);
13775   if (inst.operands[0].imm)
13776     inst.instruction |= 0x8;
13777 }
13778
13779 static void
13780 do_t_shift (void)
13781 {
13782   if (!inst.operands[1].present)
13783     inst.operands[1].reg = inst.operands[0].reg;
13784
13785   if (unified_syntax)
13786     {
13787       bfd_boolean narrow;
13788       int shift_kind;
13789
13790       switch (inst.instruction)
13791         {
13792         case T_MNEM_asr:
13793         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
13794         case T_MNEM_lsl:
13795         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
13796         case T_MNEM_lsr:
13797         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
13798         case T_MNEM_ror:
13799         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
13800         default: abort ();
13801         }
13802
13803       if (THUMB_SETS_FLAGS (inst.instruction))
13804         narrow = !in_pred_block ();
13805       else
13806         narrow = in_pred_block ();
13807       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13808         narrow = FALSE;
13809       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
13810         narrow = FALSE;
13811       if (inst.operands[2].isreg
13812           && (inst.operands[1].reg != inst.operands[0].reg
13813               || inst.operands[2].reg > 7))
13814         narrow = FALSE;
13815       if (inst.size_req == 4)
13816         narrow = FALSE;
13817
13818       reject_bad_reg (inst.operands[0].reg);
13819       reject_bad_reg (inst.operands[1].reg);
13820
13821       if (!narrow)
13822         {
13823           if (inst.operands[2].isreg)
13824             {
13825               reject_bad_reg (inst.operands[2].reg);
13826               inst.instruction = THUMB_OP32 (inst.instruction);
13827               inst.instruction |= inst.operands[0].reg << 8;
13828               inst.instruction |= inst.operands[1].reg << 16;
13829               inst.instruction |= inst.operands[2].reg;
13830
13831               /* PR 12854: Error on extraneous shifts.  */
13832               constraint (inst.operands[2].shifted,
13833                           _("extraneous shift as part of operand to shift insn"));
13834             }
13835           else
13836             {
13837               inst.operands[1].shifted = 1;
13838               inst.operands[1].shift_kind = shift_kind;
13839               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
13840                                              ? T_MNEM_movs : T_MNEM_mov);
13841               inst.instruction |= inst.operands[0].reg << 8;
13842               encode_thumb32_shifted_operand (1);
13843               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
13844               inst.relocs[0].type = BFD_RELOC_UNUSED;
13845             }
13846         }
13847       else
13848         {
13849           if (inst.operands[2].isreg)
13850             {
13851               switch (shift_kind)
13852                 {
13853                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
13854                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
13855                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
13856                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
13857                 default: abort ();
13858                 }
13859
13860               inst.instruction |= inst.operands[0].reg;
13861               inst.instruction |= inst.operands[2].reg << 3;
13862
13863               /* PR 12854: Error on extraneous shifts.  */
13864               constraint (inst.operands[2].shifted,
13865                           _("extraneous shift as part of operand to shift insn"));
13866             }
13867           else
13868             {
13869               switch (shift_kind)
13870                 {
13871                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13872                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13873                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13874                 default: abort ();
13875                 }
13876               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13877               inst.instruction |= inst.operands[0].reg;
13878               inst.instruction |= inst.operands[1].reg << 3;
13879             }
13880         }
13881     }
13882   else
13883     {
13884       constraint (inst.operands[0].reg > 7
13885                   || inst.operands[1].reg > 7, BAD_HIREG);
13886       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13887
13888       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
13889         {
13890           constraint (inst.operands[2].reg > 7, BAD_HIREG);
13891           constraint (inst.operands[0].reg != inst.operands[1].reg,
13892                       _("source1 and dest must be same register"));
13893
13894           switch (inst.instruction)
13895             {
13896             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
13897             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
13898             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
13899             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
13900             default: abort ();
13901             }
13902
13903           inst.instruction |= inst.operands[0].reg;
13904           inst.instruction |= inst.operands[2].reg << 3;
13905
13906           /* PR 12854: Error on extraneous shifts.  */
13907           constraint (inst.operands[2].shifted,
13908                       _("extraneous shift as part of operand to shift insn"));
13909         }
13910       else
13911         {
13912           switch (inst.instruction)
13913             {
13914             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
13915             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
13916             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
13917             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
13918             default: abort ();
13919             }
13920           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13921           inst.instruction |= inst.operands[0].reg;
13922           inst.instruction |= inst.operands[1].reg << 3;
13923         }
13924     }
13925 }
13926
13927 static void
13928 do_t_simd (void)
13929 {
13930   unsigned Rd, Rn, Rm;
13931
13932   Rd = inst.operands[0].reg;
13933   Rn = inst.operands[1].reg;
13934   Rm = inst.operands[2].reg;
13935
13936   reject_bad_reg (Rd);
13937   reject_bad_reg (Rn);
13938   reject_bad_reg (Rm);
13939
13940   inst.instruction |= Rd << 8;
13941   inst.instruction |= Rn << 16;
13942   inst.instruction |= Rm;
13943 }
13944
13945 static void
13946 do_t_simd2 (void)
13947 {
13948   unsigned Rd, Rn, Rm;
13949
13950   Rd = inst.operands[0].reg;
13951   Rm = inst.operands[1].reg;
13952   Rn = inst.operands[2].reg;
13953
13954   reject_bad_reg (Rd);
13955   reject_bad_reg (Rn);
13956   reject_bad_reg (Rm);
13957
13958   inst.instruction |= Rd << 8;
13959   inst.instruction |= Rn << 16;
13960   inst.instruction |= Rm;
13961 }
13962
13963 static void
13964 do_t_smc (void)
13965 {
13966   unsigned int value = inst.relocs[0].exp.X_add_number;
13967   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13968               _("SMC is not permitted on this architecture"));
13969   constraint (inst.relocs[0].exp.X_op != O_constant,
13970               _("expression too complex"));
13971   constraint (value > 0xf, _("immediate too large (bigger than 0xF)"));
13972
13973   inst.relocs[0].type = BFD_RELOC_UNUSED;
13974   inst.instruction |= (value & 0x000f) << 16;
13975
13976   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13977   set_pred_insn_type_last ();
13978 }
13979
13980 static void
13981 do_t_hvc (void)
13982 {
13983   unsigned int value = inst.relocs[0].exp.X_add_number;
13984
13985   inst.relocs[0].type = BFD_RELOC_UNUSED;
13986   inst.instruction |= (value & 0x0fff);
13987   inst.instruction |= (value & 0xf000) << 4;
13988 }
13989
13990 static void
13991 do_t_ssat_usat (int bias)
13992 {
13993   unsigned Rd, Rn;
13994
13995   Rd = inst.operands[0].reg;
13996   Rn = inst.operands[2].reg;
13997
13998   reject_bad_reg (Rd);
13999   reject_bad_reg (Rn);
14000
14001   inst.instruction |= Rd << 8;
14002   inst.instruction |= inst.operands[1].imm - bias;
14003   inst.instruction |= Rn << 16;
14004
14005   if (inst.operands[3].present)
14006     {
14007       offsetT shift_amount = inst.relocs[0].exp.X_add_number;
14008
14009       inst.relocs[0].type = BFD_RELOC_UNUSED;
14010
14011       constraint (inst.relocs[0].exp.X_op != O_constant,
14012                   _("expression too complex"));
14013
14014       if (shift_amount != 0)
14015         {
14016           constraint (shift_amount > 31,
14017                       _("shift expression is too large"));
14018
14019           if (inst.operands[3].shift_kind == SHIFT_ASR)
14020             inst.instruction |= 0x00200000;  /* sh bit.  */
14021
14022           inst.instruction |= (shift_amount & 0x1c) << 10;
14023           inst.instruction |= (shift_amount & 0x03) << 6;
14024         }
14025     }
14026 }
14027
14028 static void
14029 do_t_ssat (void)
14030 {
14031   do_t_ssat_usat (1);
14032 }
14033
14034 static void
14035 do_t_ssat16 (void)
14036 {
14037   unsigned Rd, Rn;
14038
14039   Rd = inst.operands[0].reg;
14040   Rn = inst.operands[2].reg;
14041
14042   reject_bad_reg (Rd);
14043   reject_bad_reg (Rn);
14044
14045   inst.instruction |= Rd << 8;
14046   inst.instruction |= inst.operands[1].imm - 1;
14047   inst.instruction |= Rn << 16;
14048 }
14049
14050 static void
14051 do_t_strex (void)
14052 {
14053   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
14054               || inst.operands[2].postind || inst.operands[2].writeback
14055               || inst.operands[2].immisreg || inst.operands[2].shifted
14056               || inst.operands[2].negative,
14057               BAD_ADDR_MODE);
14058
14059   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
14060
14061   inst.instruction |= inst.operands[0].reg << 8;
14062   inst.instruction |= inst.operands[1].reg << 12;
14063   inst.instruction |= inst.operands[2].reg << 16;
14064   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
14065 }
14066
14067 static void
14068 do_t_strexd (void)
14069 {
14070   if (!inst.operands[2].present)
14071     inst.operands[2].reg = inst.operands[1].reg + 1;
14072
14073   constraint (inst.operands[0].reg == inst.operands[1].reg
14074               || inst.operands[0].reg == inst.operands[2].reg
14075               || inst.operands[0].reg == inst.operands[3].reg,
14076               BAD_OVERLAP);
14077
14078   inst.instruction |= inst.operands[0].reg;
14079   inst.instruction |= inst.operands[1].reg << 12;
14080   inst.instruction |= inst.operands[2].reg << 8;
14081   inst.instruction |= inst.operands[3].reg << 16;
14082 }
14083
14084 static void
14085 do_t_sxtah (void)
14086 {
14087   unsigned Rd, Rn, Rm;
14088
14089   Rd = inst.operands[0].reg;
14090   Rn = inst.operands[1].reg;
14091   Rm = inst.operands[2].reg;
14092
14093   reject_bad_reg (Rd);
14094   reject_bad_reg (Rn);
14095   reject_bad_reg (Rm);
14096
14097   inst.instruction |= Rd << 8;
14098   inst.instruction |= Rn << 16;
14099   inst.instruction |= Rm;
14100   inst.instruction |= inst.operands[3].imm << 4;
14101 }
14102
14103 static void
14104 do_t_sxth (void)
14105 {
14106   unsigned Rd, Rm;
14107
14108   Rd = inst.operands[0].reg;
14109   Rm = inst.operands[1].reg;
14110
14111   reject_bad_reg (Rd);
14112   reject_bad_reg (Rm);
14113
14114   if (inst.instruction <= 0xffff
14115       && inst.size_req != 4
14116       && Rd <= 7 && Rm <= 7
14117       && (!inst.operands[2].present || inst.operands[2].imm == 0))
14118     {
14119       inst.instruction = THUMB_OP16 (inst.instruction);
14120       inst.instruction |= Rd;
14121       inst.instruction |= Rm << 3;
14122     }
14123   else if (unified_syntax)
14124     {
14125       if (inst.instruction <= 0xffff)
14126         inst.instruction = THUMB_OP32 (inst.instruction);
14127       inst.instruction |= Rd << 8;
14128       inst.instruction |= Rm;
14129       inst.instruction |= inst.operands[2].imm << 4;
14130     }
14131   else
14132     {
14133       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
14134                   _("Thumb encoding does not support rotation"));
14135       constraint (1, BAD_HIREG);
14136     }
14137 }
14138
14139 static void
14140 do_t_swi (void)
14141 {
14142   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
14143 }
14144
14145 static void
14146 do_t_tb (void)
14147 {
14148   unsigned Rn, Rm;
14149   int half;
14150
14151   half = (inst.instruction & 0x10) != 0;
14152   set_pred_insn_type_last ();
14153   constraint (inst.operands[0].immisreg,
14154               _("instruction requires register index"));
14155
14156   Rn = inst.operands[0].reg;
14157   Rm = inst.operands[0].imm;
14158
14159   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
14160     constraint (Rn == REG_SP, BAD_SP);
14161   reject_bad_reg (Rm);
14162
14163   constraint (!half && inst.operands[0].shifted,
14164               _("instruction does not allow shifted index"));
14165   inst.instruction |= (Rn << 16) | Rm;
14166 }
14167
14168 static void
14169 do_t_udf (void)
14170 {
14171   if (!inst.operands[0].present)
14172     inst.operands[0].imm = 0;
14173
14174   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
14175     {
14176       constraint (inst.size_req == 2,
14177                   _("immediate value out of range"));
14178       inst.instruction = THUMB_OP32 (inst.instruction);
14179       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
14180       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
14181     }
14182   else
14183     {
14184       inst.instruction = THUMB_OP16 (inst.instruction);
14185       inst.instruction |= inst.operands[0].imm;
14186     }
14187
14188   set_pred_insn_type (NEUTRAL_IT_INSN);
14189 }
14190
14191
14192 static void
14193 do_t_usat (void)
14194 {
14195   do_t_ssat_usat (0);
14196 }
14197
14198 static void
14199 do_t_usat16 (void)
14200 {
14201   unsigned Rd, Rn;
14202
14203   Rd = inst.operands[0].reg;
14204   Rn = inst.operands[2].reg;
14205
14206   reject_bad_reg (Rd);
14207   reject_bad_reg (Rn);
14208
14209   inst.instruction |= Rd << 8;
14210   inst.instruction |= inst.operands[1].imm;
14211   inst.instruction |= Rn << 16;
14212 }
14213
14214 /* Checking the range of the branch offset (VAL) with NBITS bits
14215    and IS_SIGNED signedness.  Also checks the LSB to be 0.  */
14216 static int
14217 v8_1_branch_value_check (int val, int nbits, int is_signed)
14218 {
14219   gas_assert (nbits > 0 && nbits <= 32);
14220   if (is_signed)
14221     {
14222       int cmp = (1 << (nbits - 1));
14223       if ((val < -cmp) || (val >= cmp) || (val & 0x01))
14224         return FAIL;
14225     }
14226   else
14227     {
14228       if ((val <= 0) || (val >= (1 << nbits)) || (val & 0x1))
14229         return FAIL;
14230     }
14231     return SUCCESS;
14232 }
14233
14234 /* For branches in Armv8.1-M Mainline.  */
14235 static void
14236 do_t_branch_future (void)
14237 {
14238   unsigned long insn = inst.instruction;
14239
14240   inst.instruction = THUMB_OP32 (inst.instruction);
14241   if (inst.operands[0].hasreloc == 0)
14242     {
14243       if (v8_1_branch_value_check (inst.operands[0].imm, 5, FALSE) == FAIL)
14244         as_bad (BAD_BRANCH_OFF);
14245
14246       inst.instruction |= ((inst.operands[0].imm & 0x1f) >> 1) << 23;
14247     }
14248   else
14249     {
14250       inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH5;
14251       inst.relocs[0].pc_rel = 1;
14252     }
14253
14254   switch (insn)
14255     {
14256       case T_MNEM_bf:
14257         if (inst.operands[1].hasreloc == 0)
14258           {
14259             int val = inst.operands[1].imm;
14260             if (v8_1_branch_value_check (inst.operands[1].imm, 17, TRUE) == FAIL)
14261               as_bad (BAD_BRANCH_OFF);
14262
14263             int immA = (val & 0x0001f000) >> 12;
14264             int immB = (val & 0x00000ffc) >> 2;
14265             int immC = (val & 0x00000002) >> 1;
14266             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14267           }
14268         else
14269           {
14270             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF17;
14271             inst.relocs[1].pc_rel = 1;
14272           }
14273         break;
14274
14275       case T_MNEM_bfl:
14276         if (inst.operands[1].hasreloc == 0)
14277           {
14278             int val = inst.operands[1].imm;
14279             if (v8_1_branch_value_check (inst.operands[1].imm, 19, TRUE) == FAIL)
14280               as_bad (BAD_BRANCH_OFF);
14281
14282             int immA = (val & 0x0007f000) >> 12;
14283             int immB = (val & 0x00000ffc) >> 2;
14284             int immC = (val & 0x00000002) >> 1;
14285             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14286           }
14287           else
14288           {
14289             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF19;
14290             inst.relocs[1].pc_rel = 1;
14291           }
14292         break;
14293
14294       case T_MNEM_bfcsel:
14295         /* Operand 1.  */
14296         if (inst.operands[1].hasreloc == 0)
14297           {
14298             int val = inst.operands[1].imm;
14299             int immA = (val & 0x00001000) >> 12;
14300             int immB = (val & 0x00000ffc) >> 2;
14301             int immC = (val & 0x00000002) >> 1;
14302             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14303           }
14304           else
14305           {
14306             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF13;
14307             inst.relocs[1].pc_rel = 1;
14308           }
14309
14310         /* Operand 2.  */
14311         if (inst.operands[2].hasreloc == 0)
14312           {
14313               constraint ((inst.operands[0].hasreloc != 0), BAD_ARGS);
14314               int val2 = inst.operands[2].imm;
14315               int val0 = inst.operands[0].imm & 0x1f;
14316               int diff = val2 - val0;
14317               if (diff == 4)
14318                 inst.instruction |= 1 << 17; /* T bit.  */
14319               else if (diff != 2)
14320                 as_bad (_("out of range label-relative fixup value"));
14321           }
14322         else
14323           {
14324               constraint ((inst.operands[0].hasreloc == 0), BAD_ARGS);
14325               inst.relocs[2].type = BFD_RELOC_THUMB_PCREL_BFCSEL;
14326               inst.relocs[2].pc_rel = 1;
14327           }
14328
14329         /* Operand 3.  */
14330         constraint (inst.cond != COND_ALWAYS, BAD_COND);
14331         inst.instruction |= (inst.operands[3].imm & 0xf) << 18;
14332         break;
14333
14334       case T_MNEM_bfx:
14335       case T_MNEM_bflx:
14336         inst.instruction |= inst.operands[1].reg << 16;
14337         break;
14338
14339       default: abort ();
14340     }
14341 }
14342
14343 /* Helper function for do_t_loloop to handle relocations.  */
14344 static void
14345 v8_1_loop_reloc (int is_le)
14346 {
14347   if (inst.relocs[0].exp.X_op == O_constant)
14348     {
14349       int value = inst.relocs[0].exp.X_add_number;
14350       value = (is_le) ? -value : value;
14351
14352       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
14353         as_bad (BAD_BRANCH_OFF);
14354
14355       int imml, immh;
14356
14357       immh = (value & 0x00000ffc) >> 2;
14358       imml = (value & 0x00000002) >> 1;
14359
14360       inst.instruction |= (imml << 11) | (immh << 1);
14361     }
14362   else
14363     {
14364       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_LOOP12;
14365       inst.relocs[0].pc_rel = 1;
14366     }
14367 }
14368
14369 /* For shifts with four operands in MVE.  */
14370 static void
14371 do_mve_scalar_shift1 (void)
14372 {
14373   unsigned int value = inst.operands[2].imm;
14374
14375   inst.instruction |= inst.operands[0].reg << 16;
14376   inst.instruction |= inst.operands[1].reg << 8;
14377
14378   /* Setting the bit for saturation.  */
14379   inst.instruction |= ((value == 64) ? 0: 1) << 7;
14380
14381   /* Assuming Rm is already checked not to be 11x1.  */
14382   constraint (inst.operands[3].reg == inst.operands[0].reg, BAD_OVERLAP);
14383   constraint (inst.operands[3].reg == inst.operands[1].reg, BAD_OVERLAP);
14384   inst.instruction |= inst.operands[3].reg << 12;
14385 }
14386
14387 /* For shifts in MVE.  */
14388 static void
14389 do_mve_scalar_shift (void)
14390 {
14391   if (!inst.operands[2].present)
14392     {
14393       inst.operands[2] = inst.operands[1];
14394       inst.operands[1].reg = 0xf;
14395     }
14396
14397   inst.instruction |= inst.operands[0].reg << 16;
14398   inst.instruction |= inst.operands[1].reg << 8;
14399
14400   if (inst.operands[2].isreg)
14401     {
14402       /* Assuming Rm is already checked not to be 11x1.  */
14403       constraint (inst.operands[2].reg == inst.operands[0].reg, BAD_OVERLAP);
14404       constraint (inst.operands[2].reg == inst.operands[1].reg, BAD_OVERLAP);
14405       inst.instruction |= inst.operands[2].reg << 12;
14406     }
14407   else
14408     {
14409       /* Assuming imm is already checked as [1,32].  */
14410       unsigned int value = inst.operands[2].imm;
14411       inst.instruction |= (value & 0x1c) << 10;
14412       inst.instruction |= (value & 0x03) << 6;
14413       /* Change last 4 bits from 0xd to 0xf.  */
14414       inst.instruction |= 0x2;
14415     }
14416 }
14417
14418 /* MVE instruction encoder helpers.  */
14419 #define M_MNEM_vabav    0xee800f01
14420 #define M_MNEM_vmladav    0xeef00e00
14421 #define M_MNEM_vmladava   0xeef00e20
14422 #define M_MNEM_vmladavx   0xeef01e00
14423 #define M_MNEM_vmladavax  0xeef01e20
14424 #define M_MNEM_vmlsdav    0xeef00e01
14425 #define M_MNEM_vmlsdava   0xeef00e21
14426 #define M_MNEM_vmlsdavx   0xeef01e01
14427 #define M_MNEM_vmlsdavax  0xeef01e21
14428 #define M_MNEM_vmullt   0xee011e00
14429 #define M_MNEM_vmullb   0xee010e00
14430 #define M_MNEM_vst20    0xfc801e00
14431 #define M_MNEM_vst21    0xfc801e20
14432 #define M_MNEM_vst40    0xfc801e01
14433 #define M_MNEM_vst41    0xfc801e21
14434 #define M_MNEM_vst42    0xfc801e41
14435 #define M_MNEM_vst43    0xfc801e61
14436 #define M_MNEM_vld20    0xfc901e00
14437 #define M_MNEM_vld21    0xfc901e20
14438 #define M_MNEM_vld40    0xfc901e01
14439 #define M_MNEM_vld41    0xfc901e21
14440 #define M_MNEM_vld42    0xfc901e41
14441 #define M_MNEM_vld43    0xfc901e61
14442 #define M_MNEM_vstrb    0xec000e00
14443 #define M_MNEM_vstrh    0xec000e10
14444 #define M_MNEM_vstrw    0xec000e40
14445 #define M_MNEM_vstrd    0xec000e50
14446 #define M_MNEM_vldrb    0xec100e00
14447 #define M_MNEM_vldrh    0xec100e10
14448 #define M_MNEM_vldrw    0xec100e40
14449 #define M_MNEM_vldrd    0xec100e50
14450 #define M_MNEM_vmovlt   0xeea01f40
14451 #define M_MNEM_vmovlb   0xeea00f40
14452 #define M_MNEM_vmovnt   0xfe311e81
14453 #define M_MNEM_vmovnb   0xfe310e81
14454 #define M_MNEM_vadc     0xee300f00
14455 #define M_MNEM_vadci    0xee301f00
14456 #define M_MNEM_vbrsr    0xfe011e60
14457 #define M_MNEM_vaddlv   0xee890f00
14458 #define M_MNEM_vaddlva  0xee890f20
14459 #define M_MNEM_vaddv    0xeef10f00
14460 #define M_MNEM_vaddva   0xeef10f20
14461 #define M_MNEM_vddup    0xee011f6e
14462 #define M_MNEM_vdwdup   0xee011f60
14463 #define M_MNEM_vidup    0xee010f6e
14464 #define M_MNEM_viwdup   0xee010f60
14465 #define M_MNEM_vmaxv    0xeee20f00
14466 #define M_MNEM_vmaxav   0xeee00f00
14467 #define M_MNEM_vminv    0xeee20f80
14468 #define M_MNEM_vminav   0xeee00f80
14469 #define M_MNEM_vmlaldav   0xee800e00
14470 #define M_MNEM_vmlaldava  0xee800e20
14471 #define M_MNEM_vmlaldavx  0xee801e00
14472 #define M_MNEM_vmlaldavax 0xee801e20
14473 #define M_MNEM_vmlsldav   0xee800e01
14474 #define M_MNEM_vmlsldava  0xee800e21
14475 #define M_MNEM_vmlsldavx  0xee801e01
14476 #define M_MNEM_vmlsldavax 0xee801e21
14477 #define M_MNEM_vrmlaldavhx  0xee801f00
14478 #define M_MNEM_vrmlaldavhax 0xee801f20
14479 #define M_MNEM_vrmlsldavh   0xfe800e01
14480 #define M_MNEM_vrmlsldavha  0xfe800e21
14481 #define M_MNEM_vrmlsldavhx  0xfe801e01
14482 #define M_MNEM_vrmlsldavhax 0xfe801e21
14483 #define M_MNEM_vqmovnt    0xee331e01
14484 #define M_MNEM_vqmovnb    0xee330e01
14485 #define M_MNEM_vqmovunt   0xee311e81
14486 #define M_MNEM_vqmovunb   0xee310e81
14487 #define M_MNEM_vshrnt       0xee801fc1
14488 #define M_MNEM_vshrnb       0xee800fc1
14489 #define M_MNEM_vrshrnt      0xfe801fc1
14490 #define M_MNEM_vqshrnt      0xee801f40
14491 #define M_MNEM_vqshrnb      0xee800f40
14492 #define M_MNEM_vqshrunt     0xee801fc0
14493 #define M_MNEM_vqshrunb     0xee800fc0
14494 #define M_MNEM_vrshrnb      0xfe800fc1
14495 #define M_MNEM_vqrshrnt     0xee801f41
14496 #define M_MNEM_vqrshrnb     0xee800f41
14497 #define M_MNEM_vqrshrunt    0xfe801fc0
14498 #define M_MNEM_vqrshrunb    0xfe800fc0
14499
14500 /* Neon instruction encoder helpers.  */
14501
14502 /* Encodings for the different types for various Neon opcodes.  */
14503
14504 /* An "invalid" code for the following tables.  */
14505 #define N_INV -1u
14506
14507 struct neon_tab_entry
14508 {
14509   unsigned integer;
14510   unsigned float_or_poly;
14511   unsigned scalar_or_imm;
14512 };
14513
14514 /* Map overloaded Neon opcodes to their respective encodings.  */
14515 #define NEON_ENC_TAB                                    \
14516   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
14517   X(vabdl,      0x0800700, N_INV,     N_INV),           \
14518   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
14519   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
14520   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
14521   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
14522   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
14523   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
14524   X(vaddl,      0x0800000, N_INV,     N_INV),           \
14525   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
14526   X(vsubl,      0x0800200, N_INV,     N_INV),           \
14527   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
14528   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
14529   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
14530   /* Register variants of the following two instructions are encoded as
14531      vcge / vcgt with the operands reversed.  */        \
14532   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
14533   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
14534   X(vfma,       N_INV, 0x0000c10, N_INV),               \
14535   X(vfms,       N_INV, 0x0200c10, N_INV),               \
14536   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
14537   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
14538   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
14539   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
14540   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
14541   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
14542   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
14543   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
14544   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
14545   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
14546   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
14547   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
14548   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
14549   X(vshl,       0x0000400, N_INV,     0x0800510),       \
14550   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
14551   X(vand,       0x0000110, N_INV,     0x0800030),       \
14552   X(vbic,       0x0100110, N_INV,     0x0800030),       \
14553   X(veor,       0x1000110, N_INV,     N_INV),           \
14554   X(vorn,       0x0300110, N_INV,     0x0800010),       \
14555   X(vorr,       0x0200110, N_INV,     0x0800010),       \
14556   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
14557   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
14558   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
14559   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
14560   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
14561   X(vst1,       0x0000000, 0x0800000, N_INV),           \
14562   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
14563   X(vst2,       0x0000100, 0x0800100, N_INV),           \
14564   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
14565   X(vst3,       0x0000200, 0x0800200, N_INV),           \
14566   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
14567   X(vst4,       0x0000300, 0x0800300, N_INV),           \
14568   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
14569   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
14570   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
14571   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
14572   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
14573   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
14574   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
14575   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
14576   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
14577   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
14578   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
14579   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
14580   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
14581   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
14582   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
14583   X(vselge,     0xe200a00, N_INV,     N_INV),           \
14584   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
14585   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
14586   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
14587   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
14588   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
14589   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
14590   X(aes,        0x3b00300, N_INV,     N_INV),           \
14591   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
14592   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
14593   X(sha2op,     0x3ba0380, N_INV,     N_INV)
14594
14595 enum neon_opc
14596 {
14597 #define X(OPC,I,F,S) N_MNEM_##OPC
14598 NEON_ENC_TAB
14599 #undef X
14600 };
14601
14602 static const struct neon_tab_entry neon_enc_tab[] =
14603 {
14604 #define X(OPC,I,F,S) { (I), (F), (S) }
14605 NEON_ENC_TAB
14606 #undef X
14607 };
14608
14609 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
14610 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14611 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
14612 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14613 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14614 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14615 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14616 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14617 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14618 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14619 #define NEON_ENC_SINGLE_(X) \
14620   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
14621 #define NEON_ENC_DOUBLE_(X) \
14622   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
14623 #define NEON_ENC_FPV8_(X) \
14624   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
14625
14626 #define NEON_ENCODE(type, inst)                                 \
14627   do                                                            \
14628     {                                                           \
14629       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
14630       inst.is_neon = 1;                                         \
14631     }                                                           \
14632   while (0)
14633
14634 #define check_neon_suffixes                                             \
14635   do                                                                    \
14636     {                                                                   \
14637       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
14638         {                                                               \
14639           as_bad (_("invalid neon suffix for non neon instruction"));   \
14640           return;                                                       \
14641         }                                                               \
14642     }                                                                   \
14643   while (0)
14644
14645 /* Define shapes for instruction operands. The following mnemonic characters
14646    are used in this table:
14647
14648      F - VFP S<n> register
14649      D - Neon D<n> register
14650      Q - Neon Q<n> register
14651      I - Immediate
14652      S - Scalar
14653      R - ARM register
14654      L - D<n> register list
14655
14656    This table is used to generate various data:
14657      - enumerations of the form NS_DDR to be used as arguments to
14658        neon_select_shape.
14659      - a table classifying shapes into single, double, quad, mixed.
14660      - a table used to drive neon_select_shape.  */
14661
14662 #define NEON_SHAPE_DEF                  \
14663   X(4, (R, R, Q, Q), QUAD),             \
14664   X(4, (Q, R, R, I), QUAD),             \
14665   X(4, (R, R, S, S), QUAD),             \
14666   X(4, (S, S, R, R), QUAD),             \
14667   X(3, (Q, R, I), QUAD),                \
14668   X(3, (I, Q, Q), QUAD),                \
14669   X(3, (I, Q, R), QUAD),                \
14670   X(3, (R, Q, Q), QUAD),                \
14671   X(3, (D, D, D), DOUBLE),              \
14672   X(3, (Q, Q, Q), QUAD),                \
14673   X(3, (D, D, I), DOUBLE),              \
14674   X(3, (Q, Q, I), QUAD),                \
14675   X(3, (D, D, S), DOUBLE),              \
14676   X(3, (Q, Q, S), QUAD),                \
14677   X(3, (Q, Q, R), QUAD),                \
14678   X(3, (R, R, Q), QUAD),                \
14679   X(2, (R, Q),    QUAD),                \
14680   X(2, (D, D), DOUBLE),                 \
14681   X(2, (Q, Q), QUAD),                   \
14682   X(2, (D, S), DOUBLE),                 \
14683   X(2, (Q, S), QUAD),                   \
14684   X(2, (D, R), DOUBLE),                 \
14685   X(2, (Q, R), QUAD),                   \
14686   X(2, (D, I), DOUBLE),                 \
14687   X(2, (Q, I), QUAD),                   \
14688   X(3, (D, L, D), DOUBLE),              \
14689   X(2, (D, Q), MIXED),                  \
14690   X(2, (Q, D), MIXED),                  \
14691   X(3, (D, Q, I), MIXED),               \
14692   X(3, (Q, D, I), MIXED),               \
14693   X(3, (Q, D, D), MIXED),               \
14694   X(3, (D, Q, Q), MIXED),               \
14695   X(3, (Q, Q, D), MIXED),               \
14696   X(3, (Q, D, S), MIXED),               \
14697   X(3, (D, Q, S), MIXED),               \
14698   X(4, (D, D, D, I), DOUBLE),           \
14699   X(4, (Q, Q, Q, I), QUAD),             \
14700   X(4, (D, D, S, I), DOUBLE),           \
14701   X(4, (Q, Q, S, I), QUAD),             \
14702   X(2, (F, F), SINGLE),                 \
14703   X(3, (F, F, F), SINGLE),              \
14704   X(2, (F, I), SINGLE),                 \
14705   X(2, (F, D), MIXED),                  \
14706   X(2, (D, F), MIXED),                  \
14707   X(3, (F, F, I), MIXED),               \
14708   X(4, (R, R, F, F), SINGLE),           \
14709   X(4, (F, F, R, R), SINGLE),           \
14710   X(3, (D, R, R), DOUBLE),              \
14711   X(3, (R, R, D), DOUBLE),              \
14712   X(2, (S, R), SINGLE),                 \
14713   X(2, (R, S), SINGLE),                 \
14714   X(2, (F, R), SINGLE),                 \
14715   X(2, (R, F), SINGLE),                 \
14716 /* Used for MVE tail predicated loop instructions.  */\
14717   X(2, (R, R), QUAD),                   \
14718 /* Half float shape supported so far.  */\
14719   X (2, (H, D), MIXED),                 \
14720   X (2, (D, H), MIXED),                 \
14721   X (2, (H, F), MIXED),                 \
14722   X (2, (F, H), MIXED),                 \
14723   X (2, (H, H), HALF),                  \
14724   X (2, (H, R), HALF),                  \
14725   X (2, (R, H), HALF),                  \
14726   X (2, (H, I), HALF),                  \
14727   X (3, (H, H, H), HALF),               \
14728   X (3, (H, F, I), MIXED),              \
14729   X (3, (F, H, I), MIXED),              \
14730   X (3, (D, H, H), MIXED),              \
14731   X (3, (D, H, S), MIXED)
14732
14733 #define S2(A,B)         NS_##A##B
14734 #define S3(A,B,C)       NS_##A##B##C
14735 #define S4(A,B,C,D)     NS_##A##B##C##D
14736
14737 #define X(N, L, C) S##N L
14738
14739 enum neon_shape
14740 {
14741   NEON_SHAPE_DEF,
14742   NS_NULL
14743 };
14744
14745 #undef X
14746 #undef S2
14747 #undef S3
14748 #undef S4
14749
14750 enum neon_shape_class
14751 {
14752   SC_HALF,
14753   SC_SINGLE,
14754   SC_DOUBLE,
14755   SC_QUAD,
14756   SC_MIXED
14757 };
14758
14759 #define X(N, L, C) SC_##C
14760
14761 static enum neon_shape_class neon_shape_class[] =
14762 {
14763   NEON_SHAPE_DEF
14764 };
14765
14766 #undef X
14767
14768 enum neon_shape_el
14769 {
14770   SE_H,
14771   SE_F,
14772   SE_D,
14773   SE_Q,
14774   SE_I,
14775   SE_S,
14776   SE_R,
14777   SE_L
14778 };
14779
14780 /* Register widths of above.  */
14781 static unsigned neon_shape_el_size[] =
14782 {
14783   16,
14784   32,
14785   64,
14786   128,
14787   0,
14788   32,
14789   32,
14790   0
14791 };
14792
14793 struct neon_shape_info
14794 {
14795   unsigned els;
14796   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
14797 };
14798
14799 #define S2(A,B)         { SE_##A, SE_##B }
14800 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
14801 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
14802
14803 #define X(N, L, C) { N, S##N L }
14804
14805 static struct neon_shape_info neon_shape_tab[] =
14806 {
14807   NEON_SHAPE_DEF
14808 };
14809
14810 #undef X
14811 #undef S2
14812 #undef S3
14813 #undef S4
14814
14815 /* Bit masks used in type checking given instructions.
14816   'N_EQK' means the type must be the same as (or based on in some way) the key
14817    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
14818    set, various other bits can be set as well in order to modify the meaning of
14819    the type constraint.  */
14820
14821 enum neon_type_mask
14822 {
14823   N_S8   = 0x0000001,
14824   N_S16  = 0x0000002,
14825   N_S32  = 0x0000004,
14826   N_S64  = 0x0000008,
14827   N_U8   = 0x0000010,
14828   N_U16  = 0x0000020,
14829   N_U32  = 0x0000040,
14830   N_U64  = 0x0000080,
14831   N_I8   = 0x0000100,
14832   N_I16  = 0x0000200,
14833   N_I32  = 0x0000400,
14834   N_I64  = 0x0000800,
14835   N_8    = 0x0001000,
14836   N_16   = 0x0002000,
14837   N_32   = 0x0004000,
14838   N_64   = 0x0008000,
14839   N_P8   = 0x0010000,
14840   N_P16  = 0x0020000,
14841   N_F16  = 0x0040000,
14842   N_F32  = 0x0080000,
14843   N_F64  = 0x0100000,
14844   N_P64  = 0x0200000,
14845   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
14846   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
14847   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
14848   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
14849   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
14850   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
14851   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
14852   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
14853   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
14854   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
14855   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
14856   N_UTYP = 0,
14857   N_MAX_NONSPECIAL = N_P64
14858 };
14859
14860 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
14861
14862 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
14863 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14864 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
14865 #define N_S_32     (N_S8 | N_S16 | N_S32)
14866 #define N_F_16_32  (N_F16 | N_F32)
14867 #define N_SUF_32   (N_SU_32 | N_F_16_32)
14868 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
14869 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
14870 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
14871 #define N_I_MVE    (N_I8 | N_I16 | N_I32)
14872 #define N_F_MVE    (N_F16 | N_F32)
14873 #define N_SU_MVE   (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14874
14875 /* Pass this as the first type argument to neon_check_type to ignore types
14876    altogether.  */
14877 #define N_IGNORE_TYPE (N_KEY | N_EQK)
14878
14879 /* Select a "shape" for the current instruction (describing register types or
14880    sizes) from a list of alternatives. Return NS_NULL if the current instruction
14881    doesn't fit. For non-polymorphic shapes, checking is usually done as a
14882    function of operand parsing, so this function doesn't need to be called.
14883    Shapes should be listed in order of decreasing length.  */
14884
14885 static enum neon_shape
14886 neon_select_shape (enum neon_shape shape, ...)
14887 {
14888   va_list ap;
14889   enum neon_shape first_shape = shape;
14890
14891   /* Fix missing optional operands. FIXME: we don't know at this point how
14892      many arguments we should have, so this makes the assumption that we have
14893      > 1. This is true of all current Neon opcodes, I think, but may not be
14894      true in the future.  */
14895   if (!inst.operands[1].present)
14896     inst.operands[1] = inst.operands[0];
14897
14898   va_start (ap, shape);
14899
14900   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
14901     {
14902       unsigned j;
14903       int matches = 1;
14904
14905       for (j = 0; j < neon_shape_tab[shape].els; j++)
14906         {
14907           if (!inst.operands[j].present)
14908             {
14909               matches = 0;
14910               break;
14911             }
14912
14913           switch (neon_shape_tab[shape].el[j])
14914             {
14915               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
14916                  a VFP single precision register operand, it's essentially
14917                  means only half of the register is used.
14918
14919                  If the type specifier is given after the mnemonics, the
14920                  information is stored in inst.vectype.  If the type specifier
14921                  is given after register operand, the information is stored
14922                  in inst.operands[].vectype.
14923
14924                  When there is only one type specifier, and all the register
14925                  operands are the same type of hardware register, the type
14926                  specifier applies to all register operands.
14927
14928                  If no type specifier is given, the shape is inferred from
14929                  operand information.
14930
14931                  for example:
14932                  vadd.f16 s0, s1, s2:           NS_HHH
14933                  vabs.f16 s0, s1:               NS_HH
14934                  vmov.f16 s0, r1:               NS_HR
14935                  vmov.f16 r0, s1:               NS_RH
14936                  vcvt.f16 r0, s1:               NS_RH
14937                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
14938                  vcvt.f16.s32   s2, s2:         NS_HF
14939               */
14940             case SE_H:
14941               if (!(inst.operands[j].isreg
14942                     && inst.operands[j].isvec
14943                     && inst.operands[j].issingle
14944                     && !inst.operands[j].isquad
14945                     && ((inst.vectype.elems == 1
14946                          && inst.vectype.el[0].size == 16)
14947                         || (inst.vectype.elems > 1
14948                             && inst.vectype.el[j].size == 16)
14949                         || (inst.vectype.elems == 0
14950                             && inst.operands[j].vectype.type != NT_invtype
14951                             && inst.operands[j].vectype.size == 16))))
14952                 matches = 0;
14953               break;
14954
14955             case SE_F:
14956               if (!(inst.operands[j].isreg
14957                     && inst.operands[j].isvec
14958                     && inst.operands[j].issingle
14959                     && !inst.operands[j].isquad
14960                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
14961                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
14962                         || (inst.vectype.elems == 0
14963                             && (inst.operands[j].vectype.size == 32
14964                                 || inst.operands[j].vectype.type == NT_invtype)))))
14965                 matches = 0;
14966               break;
14967
14968             case SE_D:
14969               if (!(inst.operands[j].isreg
14970                     && inst.operands[j].isvec
14971                     && !inst.operands[j].isquad
14972                     && !inst.operands[j].issingle))
14973                 matches = 0;
14974               break;
14975
14976             case SE_R:
14977               if (!(inst.operands[j].isreg
14978                     && !inst.operands[j].isvec))
14979                 matches = 0;
14980               break;
14981
14982             case SE_Q:
14983               if (!(inst.operands[j].isreg
14984                     && inst.operands[j].isvec
14985                     && inst.operands[j].isquad
14986                     && !inst.operands[j].issingle))
14987                 matches = 0;
14988               break;
14989
14990             case SE_I:
14991               if (!(!inst.operands[j].isreg
14992                     && !inst.operands[j].isscalar))
14993                 matches = 0;
14994               break;
14995
14996             case SE_S:
14997               if (!(!inst.operands[j].isreg
14998                     && inst.operands[j].isscalar))
14999                 matches = 0;
15000               break;
15001
15002             case SE_L:
15003               break;
15004             }
15005           if (!matches)
15006             break;
15007         }
15008       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
15009         /* We've matched all the entries in the shape table, and we don't
15010            have any left over operands which have not been matched.  */
15011         break;
15012     }
15013
15014   va_end (ap);
15015
15016   if (shape == NS_NULL && first_shape != NS_NULL)
15017     first_error (_("invalid instruction shape"));
15018
15019   return shape;
15020 }
15021
15022 /* True if SHAPE is predominantly a quadword operation (most of the time, this
15023    means the Q bit should be set).  */
15024
15025 static int
15026 neon_quad (enum neon_shape shape)
15027 {
15028   return neon_shape_class[shape] == SC_QUAD;
15029 }
15030
15031 static void
15032 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
15033                        unsigned *g_size)
15034 {
15035   /* Allow modification to be made to types which are constrained to be
15036      based on the key element, based on bits set alongside N_EQK.  */
15037   if ((typebits & N_EQK) != 0)
15038     {
15039       if ((typebits & N_HLF) != 0)
15040         *g_size /= 2;
15041       else if ((typebits & N_DBL) != 0)
15042         *g_size *= 2;
15043       if ((typebits & N_SGN) != 0)
15044         *g_type = NT_signed;
15045       else if ((typebits & N_UNS) != 0)
15046         *g_type = NT_unsigned;
15047       else if ((typebits & N_INT) != 0)
15048         *g_type = NT_integer;
15049       else if ((typebits & N_FLT) != 0)
15050         *g_type = NT_float;
15051       else if ((typebits & N_SIZ) != 0)
15052         *g_type = NT_untyped;
15053     }
15054 }
15055
15056 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
15057    operand type, i.e. the single type specified in a Neon instruction when it
15058    is the only one given.  */
15059
15060 static struct neon_type_el
15061 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
15062 {
15063   struct neon_type_el dest = *key;
15064
15065   gas_assert ((thisarg & N_EQK) != 0);
15066
15067   neon_modify_type_size (thisarg, &dest.type, &dest.size);
15068
15069   return dest;
15070 }
15071
15072 /* Convert Neon type and size into compact bitmask representation.  */
15073
15074 static enum neon_type_mask
15075 type_chk_of_el_type (enum neon_el_type type, unsigned size)
15076 {
15077   switch (type)
15078     {
15079     case NT_untyped:
15080       switch (size)
15081         {
15082         case 8:  return N_8;
15083         case 16: return N_16;
15084         case 32: return N_32;
15085         case 64: return N_64;
15086         default: ;
15087         }
15088       break;
15089
15090     case NT_integer:
15091       switch (size)
15092         {
15093         case 8:  return N_I8;
15094         case 16: return N_I16;
15095         case 32: return N_I32;
15096         case 64: return N_I64;
15097         default: ;
15098         }
15099       break;
15100
15101     case NT_float:
15102       switch (size)
15103         {
15104         case 16: return N_F16;
15105         case 32: return N_F32;
15106         case 64: return N_F64;
15107         default: ;
15108         }
15109       break;
15110
15111     case NT_poly:
15112       switch (size)
15113         {
15114         case 8:  return N_P8;
15115         case 16: return N_P16;
15116         case 64: return N_P64;
15117         default: ;
15118         }
15119       break;
15120
15121     case NT_signed:
15122       switch (size)
15123         {
15124         case 8:  return N_S8;
15125         case 16: return N_S16;
15126         case 32: return N_S32;
15127         case 64: return N_S64;
15128         default: ;
15129         }
15130       break;
15131
15132     case NT_unsigned:
15133       switch (size)
15134         {
15135         case 8:  return N_U8;
15136         case 16: return N_U16;
15137         case 32: return N_U32;
15138         case 64: return N_U64;
15139         default: ;
15140         }
15141       break;
15142
15143     default: ;
15144     }
15145
15146   return N_UTYP;
15147 }
15148
15149 /* Convert compact Neon bitmask type representation to a type and size. Only
15150    handles the case where a single bit is set in the mask.  */
15151
15152 static int
15153 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
15154                      enum neon_type_mask mask)
15155 {
15156   if ((mask & N_EQK) != 0)
15157     return FAIL;
15158
15159   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
15160     *size = 8;
15161   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
15162     *size = 16;
15163   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
15164     *size = 32;
15165   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
15166     *size = 64;
15167   else
15168     return FAIL;
15169
15170   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
15171     *type = NT_signed;
15172   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
15173     *type = NT_unsigned;
15174   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
15175     *type = NT_integer;
15176   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
15177     *type = NT_untyped;
15178   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
15179     *type = NT_poly;
15180   else if ((mask & (N_F_ALL)) != 0)
15181     *type = NT_float;
15182   else
15183     return FAIL;
15184
15185   return SUCCESS;
15186 }
15187
15188 /* Modify a bitmask of allowed types. This is only needed for type
15189    relaxation.  */
15190
15191 static unsigned
15192 modify_types_allowed (unsigned allowed, unsigned mods)
15193 {
15194   unsigned size;
15195   enum neon_el_type type;
15196   unsigned destmask;
15197   int i;
15198
15199   destmask = 0;
15200
15201   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
15202     {
15203       if (el_type_of_type_chk (&type, &size,
15204                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
15205         {
15206           neon_modify_type_size (mods, &type, &size);
15207           destmask |= type_chk_of_el_type (type, size);
15208         }
15209     }
15210
15211   return destmask;
15212 }
15213
15214 /* Check type and return type classification.
15215    The manual states (paraphrase): If one datatype is given, it indicates the
15216    type given in:
15217     - the second operand, if there is one
15218     - the operand, if there is no second operand
15219     - the result, if there are no operands.
15220    This isn't quite good enough though, so we use a concept of a "key" datatype
15221    which is set on a per-instruction basis, which is the one which matters when
15222    only one data type is written.
15223    Note: this function has side-effects (e.g. filling in missing operands). All
15224    Neon instructions should call it before performing bit encoding.  */
15225
15226 static struct neon_type_el
15227 neon_check_type (unsigned els, enum neon_shape ns, ...)
15228 {
15229   va_list ap;
15230   unsigned i, pass, key_el = 0;
15231   unsigned types[NEON_MAX_TYPE_ELS];
15232   enum neon_el_type k_type = NT_invtype;
15233   unsigned k_size = -1u;
15234   struct neon_type_el badtype = {NT_invtype, -1};
15235   unsigned key_allowed = 0;
15236
15237   /* Optional registers in Neon instructions are always (not) in operand 1.
15238      Fill in the missing operand here, if it was omitted.  */
15239   if (els > 1 && !inst.operands[1].present)
15240     inst.operands[1] = inst.operands[0];
15241
15242   /* Suck up all the varargs.  */
15243   va_start (ap, ns);
15244   for (i = 0; i < els; i++)
15245     {
15246       unsigned thisarg = va_arg (ap, unsigned);
15247       if (thisarg == N_IGNORE_TYPE)
15248         {
15249           va_end (ap);
15250           return badtype;
15251         }
15252       types[i] = thisarg;
15253       if ((thisarg & N_KEY) != 0)
15254         key_el = i;
15255     }
15256   va_end (ap);
15257
15258   if (inst.vectype.elems > 0)
15259     for (i = 0; i < els; i++)
15260       if (inst.operands[i].vectype.type != NT_invtype)
15261         {
15262           first_error (_("types specified in both the mnemonic and operands"));
15263           return badtype;
15264         }
15265
15266   /* Duplicate inst.vectype elements here as necessary.
15267      FIXME: No idea if this is exactly the same as the ARM assembler,
15268      particularly when an insn takes one register and one non-register
15269      operand. */
15270   if (inst.vectype.elems == 1 && els > 1)
15271     {
15272       unsigned j;
15273       inst.vectype.elems = els;
15274       inst.vectype.el[key_el] = inst.vectype.el[0];
15275       for (j = 0; j < els; j++)
15276         if (j != key_el)
15277           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
15278                                                   types[j]);
15279     }
15280   else if (inst.vectype.elems == 0 && els > 0)
15281     {
15282       unsigned j;
15283       /* No types were given after the mnemonic, so look for types specified
15284          after each operand. We allow some flexibility here; as long as the
15285          "key" operand has a type, we can infer the others.  */
15286       for (j = 0; j < els; j++)
15287         if (inst.operands[j].vectype.type != NT_invtype)
15288           inst.vectype.el[j] = inst.operands[j].vectype;
15289
15290       if (inst.operands[key_el].vectype.type != NT_invtype)
15291         {
15292           for (j = 0; j < els; j++)
15293             if (inst.operands[j].vectype.type == NT_invtype)
15294               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
15295                                                       types[j]);
15296         }
15297       else
15298         {
15299           first_error (_("operand types can't be inferred"));
15300           return badtype;
15301         }
15302     }
15303   else if (inst.vectype.elems != els)
15304     {
15305       first_error (_("type specifier has the wrong number of parts"));
15306       return badtype;
15307     }
15308
15309   for (pass = 0; pass < 2; pass++)
15310     {
15311       for (i = 0; i < els; i++)
15312         {
15313           unsigned thisarg = types[i];
15314           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
15315             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
15316           enum neon_el_type g_type = inst.vectype.el[i].type;
15317           unsigned g_size = inst.vectype.el[i].size;
15318
15319           /* Decay more-specific signed & unsigned types to sign-insensitive
15320              integer types if sign-specific variants are unavailable.  */
15321           if ((g_type == NT_signed || g_type == NT_unsigned)
15322               && (types_allowed & N_SU_ALL) == 0)
15323             g_type = NT_integer;
15324
15325           /* If only untyped args are allowed, decay any more specific types to
15326              them. Some instructions only care about signs for some element
15327              sizes, so handle that properly.  */
15328           if (((types_allowed & N_UNT) == 0)
15329               && ((g_size == 8 && (types_allowed & N_8) != 0)
15330                   || (g_size == 16 && (types_allowed & N_16) != 0)
15331                   || (g_size == 32 && (types_allowed & N_32) != 0)
15332                   || (g_size == 64 && (types_allowed & N_64) != 0)))
15333             g_type = NT_untyped;
15334
15335           if (pass == 0)
15336             {
15337               if ((thisarg & N_KEY) != 0)
15338                 {
15339                   k_type = g_type;
15340                   k_size = g_size;
15341                   key_allowed = thisarg & ~N_KEY;
15342
15343                   /* Check architecture constraint on FP16 extension.  */
15344                   if (k_size == 16
15345                       && k_type == NT_float
15346                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15347                     {
15348                       inst.error = _(BAD_FP16);
15349                       return badtype;
15350                     }
15351                 }
15352             }
15353           else
15354             {
15355               if ((thisarg & N_VFP) != 0)
15356                 {
15357                   enum neon_shape_el regshape;
15358                   unsigned regwidth, match;
15359
15360                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
15361                   if (ns == NS_NULL)
15362                     {
15363                       first_error (_("invalid instruction shape"));
15364                       return badtype;
15365                     }
15366                   regshape = neon_shape_tab[ns].el[i];
15367                   regwidth = neon_shape_el_size[regshape];
15368
15369                   /* In VFP mode, operands must match register widths. If we
15370                      have a key operand, use its width, else use the width of
15371                      the current operand.  */
15372                   if (k_size != -1u)
15373                     match = k_size;
15374                   else
15375                     match = g_size;
15376
15377                   /* FP16 will use a single precision register.  */
15378                   if (regwidth == 32 && match == 16)
15379                     {
15380                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15381                         match = regwidth;
15382                       else
15383                         {
15384                           inst.error = _(BAD_FP16);
15385                           return badtype;
15386                         }
15387                     }
15388
15389                   if (regwidth != match)
15390                     {
15391                       first_error (_("operand size must match register width"));
15392                       return badtype;
15393                     }
15394                 }
15395
15396               if ((thisarg & N_EQK) == 0)
15397                 {
15398                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
15399
15400                   if ((given_type & types_allowed) == 0)
15401                     {
15402                       first_error (BAD_SIMD_TYPE);
15403                       return badtype;
15404                     }
15405                 }
15406               else
15407                 {
15408                   enum neon_el_type mod_k_type = k_type;
15409                   unsigned mod_k_size = k_size;
15410                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
15411                   if (g_type != mod_k_type || g_size != mod_k_size)
15412                     {
15413                       first_error (_("inconsistent types in Neon instruction"));
15414                       return badtype;
15415                     }
15416                 }
15417             }
15418         }
15419     }
15420
15421   return inst.vectype.el[key_el];
15422 }
15423
15424 /* Neon-style VFP instruction forwarding.  */
15425
15426 /* Thumb VFP instructions have 0xE in the condition field.  */
15427
15428 static void
15429 do_vfp_cond_or_thumb (void)
15430 {
15431   inst.is_neon = 1;
15432
15433   if (thumb_mode)
15434     inst.instruction |= 0xe0000000;
15435   else
15436     inst.instruction |= inst.cond << 28;
15437 }
15438
15439 /* Look up and encode a simple mnemonic, for use as a helper function for the
15440    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
15441    etc.  It is assumed that operand parsing has already been done, and that the
15442    operands are in the form expected by the given opcode (this isn't necessarily
15443    the same as the form in which they were parsed, hence some massaging must
15444    take place before this function is called).
15445    Checks current arch version against that in the looked-up opcode.  */
15446
15447 static void
15448 do_vfp_nsyn_opcode (const char *opname)
15449 {
15450   const struct asm_opcode *opcode;
15451
15452   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
15453
15454   if (!opcode)
15455     abort ();
15456
15457   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
15458                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
15459               _(BAD_FPU));
15460
15461   inst.is_neon = 1;
15462
15463   if (thumb_mode)
15464     {
15465       inst.instruction = opcode->tvalue;
15466       opcode->tencode ();
15467     }
15468   else
15469     {
15470       inst.instruction = (inst.cond << 28) | opcode->avalue;
15471       opcode->aencode ();
15472     }
15473 }
15474
15475 static void
15476 do_vfp_nsyn_add_sub (enum neon_shape rs)
15477 {
15478   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
15479
15480   if (rs == NS_FFF || rs == NS_HHH)
15481     {
15482       if (is_add)
15483         do_vfp_nsyn_opcode ("fadds");
15484       else
15485         do_vfp_nsyn_opcode ("fsubs");
15486
15487       /* ARMv8.2 fp16 instruction.  */
15488       if (rs == NS_HHH)
15489         do_scalar_fp16_v82_encode ();
15490     }
15491   else
15492     {
15493       if (is_add)
15494         do_vfp_nsyn_opcode ("faddd");
15495       else
15496         do_vfp_nsyn_opcode ("fsubd");
15497     }
15498 }
15499
15500 /* Check operand types to see if this is a VFP instruction, and if so call
15501    PFN ().  */
15502
15503 static int
15504 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
15505 {
15506   enum neon_shape rs;
15507   struct neon_type_el et;
15508
15509   switch (args)
15510     {
15511     case 2:
15512       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15513       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15514       break;
15515
15516     case 3:
15517       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15518       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15519                             N_F_ALL | N_KEY | N_VFP);
15520       break;
15521
15522     default:
15523       abort ();
15524     }
15525
15526   if (et.type != NT_invtype)
15527     {
15528       pfn (rs);
15529       return SUCCESS;
15530     }
15531
15532   inst.error = NULL;
15533   return FAIL;
15534 }
15535
15536 static void
15537 do_vfp_nsyn_mla_mls (enum neon_shape rs)
15538 {
15539   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
15540
15541   if (rs == NS_FFF || rs == NS_HHH)
15542     {
15543       if (is_mla)
15544         do_vfp_nsyn_opcode ("fmacs");
15545       else
15546         do_vfp_nsyn_opcode ("fnmacs");
15547
15548       /* ARMv8.2 fp16 instruction.  */
15549       if (rs == NS_HHH)
15550         do_scalar_fp16_v82_encode ();
15551     }
15552   else
15553     {
15554       if (is_mla)
15555         do_vfp_nsyn_opcode ("fmacd");
15556       else
15557         do_vfp_nsyn_opcode ("fnmacd");
15558     }
15559 }
15560
15561 static void
15562 do_vfp_nsyn_fma_fms (enum neon_shape rs)
15563 {
15564   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
15565
15566   if (rs == NS_FFF || rs == NS_HHH)
15567     {
15568       if (is_fma)
15569         do_vfp_nsyn_opcode ("ffmas");
15570       else
15571         do_vfp_nsyn_opcode ("ffnmas");
15572
15573       /* ARMv8.2 fp16 instruction.  */
15574       if (rs == NS_HHH)
15575         do_scalar_fp16_v82_encode ();
15576     }
15577   else
15578     {
15579       if (is_fma)
15580         do_vfp_nsyn_opcode ("ffmad");
15581       else
15582         do_vfp_nsyn_opcode ("ffnmad");
15583     }
15584 }
15585
15586 static void
15587 do_vfp_nsyn_mul (enum neon_shape rs)
15588 {
15589   if (rs == NS_FFF || rs == NS_HHH)
15590     {
15591       do_vfp_nsyn_opcode ("fmuls");
15592
15593       /* ARMv8.2 fp16 instruction.  */
15594       if (rs == NS_HHH)
15595         do_scalar_fp16_v82_encode ();
15596     }
15597   else
15598     do_vfp_nsyn_opcode ("fmuld");
15599 }
15600
15601 static void
15602 do_vfp_nsyn_abs_neg (enum neon_shape rs)
15603 {
15604   int is_neg = (inst.instruction & 0x80) != 0;
15605   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
15606
15607   if (rs == NS_FF || rs == NS_HH)
15608     {
15609       if (is_neg)
15610         do_vfp_nsyn_opcode ("fnegs");
15611       else
15612         do_vfp_nsyn_opcode ("fabss");
15613
15614       /* ARMv8.2 fp16 instruction.  */
15615       if (rs == NS_HH)
15616         do_scalar_fp16_v82_encode ();
15617     }
15618   else
15619     {
15620       if (is_neg)
15621         do_vfp_nsyn_opcode ("fnegd");
15622       else
15623         do_vfp_nsyn_opcode ("fabsd");
15624     }
15625 }
15626
15627 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
15628    insns belong to Neon, and are handled elsewhere.  */
15629
15630 static void
15631 do_vfp_nsyn_ldm_stm (int is_dbmode)
15632 {
15633   int is_ldm = (inst.instruction & (1 << 20)) != 0;
15634   if (is_ldm)
15635     {
15636       if (is_dbmode)
15637         do_vfp_nsyn_opcode ("fldmdbs");
15638       else
15639         do_vfp_nsyn_opcode ("fldmias");
15640     }
15641   else
15642     {
15643       if (is_dbmode)
15644         do_vfp_nsyn_opcode ("fstmdbs");
15645       else
15646         do_vfp_nsyn_opcode ("fstmias");
15647     }
15648 }
15649
15650 static void
15651 do_vfp_nsyn_sqrt (void)
15652 {
15653   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15654   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15655
15656   if (rs == NS_FF || rs == NS_HH)
15657     {
15658       do_vfp_nsyn_opcode ("fsqrts");
15659
15660       /* ARMv8.2 fp16 instruction.  */
15661       if (rs == NS_HH)
15662         do_scalar_fp16_v82_encode ();
15663     }
15664   else
15665     do_vfp_nsyn_opcode ("fsqrtd");
15666 }
15667
15668 static void
15669 do_vfp_nsyn_div (void)
15670 {
15671   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15672   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15673                    N_F_ALL | N_KEY | N_VFP);
15674
15675   if (rs == NS_FFF || rs == NS_HHH)
15676     {
15677       do_vfp_nsyn_opcode ("fdivs");
15678
15679       /* ARMv8.2 fp16 instruction.  */
15680       if (rs == NS_HHH)
15681         do_scalar_fp16_v82_encode ();
15682     }
15683   else
15684     do_vfp_nsyn_opcode ("fdivd");
15685 }
15686
15687 static void
15688 do_vfp_nsyn_nmul (void)
15689 {
15690   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15691   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15692                    N_F_ALL | N_KEY | N_VFP);
15693
15694   if (rs == NS_FFF || rs == NS_HHH)
15695     {
15696       NEON_ENCODE (SINGLE, inst);
15697       do_vfp_sp_dyadic ();
15698
15699       /* ARMv8.2 fp16 instruction.  */
15700       if (rs == NS_HHH)
15701         do_scalar_fp16_v82_encode ();
15702     }
15703   else
15704     {
15705       NEON_ENCODE (DOUBLE, inst);
15706       do_vfp_dp_rd_rn_rm ();
15707     }
15708   do_vfp_cond_or_thumb ();
15709
15710 }
15711
15712 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
15713    (0, 1, 2, 3).  */
15714
15715 static unsigned
15716 neon_logbits (unsigned x)
15717 {
15718   return ffs (x) - 4;
15719 }
15720
15721 #define LOW4(R) ((R) & 0xf)
15722 #define HI1(R) (((R) >> 4) & 1)
15723
15724 static unsigned
15725 mve_get_vcmp_vpt_cond (struct neon_type_el et)
15726 {
15727   switch (et.type)
15728     {
15729     default:
15730       first_error (BAD_EL_TYPE);
15731       return 0;
15732     case NT_float:
15733       switch (inst.operands[0].imm)
15734         {
15735         default:
15736           first_error (_("invalid condition"));
15737           return 0;
15738         case 0x0:
15739           /* eq.  */
15740           return 0;
15741         case 0x1:
15742           /* ne.  */
15743           return 1;
15744         case 0xa:
15745           /* ge/  */
15746           return 4;
15747         case 0xb:
15748           /* lt.  */
15749           return 5;
15750         case 0xc:
15751           /* gt.  */
15752           return 6;
15753         case 0xd:
15754           /* le.  */
15755           return 7;
15756         }
15757     case NT_integer:
15758       /* only accept eq and ne.  */
15759       if (inst.operands[0].imm > 1)
15760         {
15761           first_error (_("invalid condition"));
15762           return 0;
15763         }
15764       return inst.operands[0].imm;
15765     case NT_unsigned:
15766       if (inst.operands[0].imm == 0x2)
15767         return 2;
15768       else if (inst.operands[0].imm == 0x8)
15769         return 3;
15770       else
15771         {
15772           first_error (_("invalid condition"));
15773           return 0;
15774         }
15775     case NT_signed:
15776       switch (inst.operands[0].imm)
15777         {
15778           default:
15779             first_error (_("invalid condition"));
15780             return 0;
15781           case 0xa:
15782             /* ge.  */
15783             return 4;
15784           case 0xb:
15785             /* lt.  */
15786             return 5;
15787           case 0xc:
15788             /* gt.  */
15789             return 6;
15790           case 0xd:
15791             /* le.  */
15792             return 7;
15793         }
15794     }
15795   /* Should be unreachable.  */
15796   abort ();
15797 }
15798
15799 static void
15800 do_mve_vpt (void)
15801 {
15802   /* We are dealing with a vector predicated block.  */
15803   if (inst.operands[0].present)
15804     {
15805       enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15806       struct neon_type_el et
15807         = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15808                            N_EQK);
15809
15810       unsigned fcond = mve_get_vcmp_vpt_cond (et);
15811
15812       constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15813
15814       if (et.type == NT_invtype)
15815         return;
15816
15817       if (et.type == NT_float)
15818         {
15819           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15820                       BAD_FPU);
15821           constraint (et.size != 16 && et.size != 32, BAD_EL_TYPE);
15822           inst.instruction |= (et.size == 16) << 28;
15823           inst.instruction |= 0x3 << 20;
15824         }
15825       else
15826         {
15827           constraint (et.size != 8 && et.size != 16 && et.size != 32,
15828                       BAD_EL_TYPE);
15829           inst.instruction |= 1 << 28;
15830           inst.instruction |= neon_logbits (et.size) << 20;
15831         }
15832
15833       if (inst.operands[2].isquad)
15834         {
15835           inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15836           inst.instruction |= LOW4 (inst.operands[2].reg);
15837           inst.instruction |= (fcond & 0x2) >> 1;
15838         }
15839       else
15840         {
15841           if (inst.operands[2].reg == REG_SP)
15842             as_tsktsk (MVE_BAD_SP);
15843           inst.instruction |= 1 << 6;
15844           inst.instruction |= (fcond & 0x2) << 4;
15845           inst.instruction |= inst.operands[2].reg;
15846         }
15847       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15848       inst.instruction |= (fcond & 0x4) << 10;
15849       inst.instruction |= (fcond & 0x1) << 7;
15850
15851     }
15852     set_pred_insn_type (VPT_INSN);
15853     now_pred.cc = 0;
15854     now_pred.mask = ((inst.instruction & 0x00400000) >> 19)
15855                     | ((inst.instruction & 0xe000) >> 13);
15856     now_pred.warn_deprecated = FALSE;
15857     now_pred.type = VECTOR_PRED;
15858     inst.is_neon = 1;
15859 }
15860
15861 static void
15862 do_mve_vcmp (void)
15863 {
15864   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
15865   if (!inst.operands[1].isreg || !inst.operands[1].isquad)
15866     first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
15867   if (!inst.operands[2].present)
15868     first_error (_("MVE vector or ARM register expected"));
15869   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15870
15871   /* Deal with 'else' conditional MVE's vcmp, it will be parsed as vcmpe.  */
15872   if ((inst.instruction & 0xffffffff) == N_MNEM_vcmpe
15873       && inst.operands[1].isquad)
15874     {
15875       inst.instruction = N_MNEM_vcmp;
15876       inst.cond = 0x10;
15877     }
15878
15879   if (inst.cond > COND_ALWAYS)
15880     inst.pred_insn_type = INSIDE_VPT_INSN;
15881   else
15882     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15883
15884   enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15885   struct neon_type_el et
15886     = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15887                        N_EQK);
15888
15889   constraint (rs == NS_IQR && inst.operands[2].reg == REG_PC
15890               && !inst.operands[2].iszr, BAD_PC);
15891
15892   unsigned fcond = mve_get_vcmp_vpt_cond (et);
15893
15894   inst.instruction = 0xee010f00;
15895   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15896   inst.instruction |= (fcond & 0x4) << 10;
15897   inst.instruction |= (fcond & 0x1) << 7;
15898   if (et.type == NT_float)
15899     {
15900       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15901                   BAD_FPU);
15902       inst.instruction |= (et.size == 16) << 28;
15903       inst.instruction |= 0x3 << 20;
15904     }
15905   else
15906     {
15907       inst.instruction |= 1 << 28;
15908       inst.instruction |= neon_logbits (et.size) << 20;
15909     }
15910   if (inst.operands[2].isquad)
15911     {
15912       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15913       inst.instruction |= (fcond & 0x2) >> 1;
15914       inst.instruction |= LOW4 (inst.operands[2].reg);
15915     }
15916   else
15917     {
15918       if (inst.operands[2].reg == REG_SP)
15919         as_tsktsk (MVE_BAD_SP);
15920       inst.instruction |= 1 << 6;
15921       inst.instruction |= (fcond & 0x2) << 4;
15922       inst.instruction |= inst.operands[2].reg;
15923     }
15924
15925   inst.is_neon = 1;
15926   return;
15927 }
15928
15929 static void
15930 do_mve_vmaxa_vmina (void)
15931 {
15932   if (inst.cond > COND_ALWAYS)
15933     inst.pred_insn_type = INSIDE_VPT_INSN;
15934   else
15935     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15936
15937   enum neon_shape rs = neon_select_shape (NS_QQ, NS_NULL);
15938   struct neon_type_el et
15939     = neon_check_type (2, rs, N_EQK, N_KEY | N_S8 | N_S16 | N_S32);
15940
15941   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15942   inst.instruction |= neon_logbits (et.size) << 18;
15943   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15944   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15945   inst.instruction |= LOW4 (inst.operands[1].reg);
15946   inst.is_neon = 1;
15947 }
15948
15949 static void
15950 do_mve_vfmas (void)
15951 {
15952   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
15953   struct neon_type_el et
15954     = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK, N_EQK);
15955
15956   if (inst.cond > COND_ALWAYS)
15957     inst.pred_insn_type = INSIDE_VPT_INSN;
15958   else
15959     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15960
15961   if (inst.operands[2].reg == REG_SP)
15962     as_tsktsk (MVE_BAD_SP);
15963   else if (inst.operands[2].reg == REG_PC)
15964     as_tsktsk (MVE_BAD_PC);
15965
15966   inst.instruction |= (et.size == 16) << 28;
15967   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15968   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15969   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15970   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15971   inst.instruction |= inst.operands[2].reg;
15972   inst.is_neon = 1;
15973 }
15974
15975 static void
15976 do_mve_viddup (void)
15977 {
15978   if (inst.cond > COND_ALWAYS)
15979     inst.pred_insn_type = INSIDE_VPT_INSN;
15980   else
15981     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15982
15983   unsigned imm = inst.relocs[0].exp.X_add_number;
15984   constraint (imm != 1 && imm != 2 && imm != 4 && imm != 8,
15985               _("immediate must be either 1, 2, 4 or 8"));
15986
15987   enum neon_shape rs;
15988   struct neon_type_el et;
15989   unsigned Rm;
15990   if (inst.instruction == M_MNEM_vddup || inst.instruction == M_MNEM_vidup)
15991     {
15992       rs = neon_select_shape (NS_QRI, NS_NULL);
15993       et = neon_check_type (2, rs, N_KEY | N_U8 | N_U16 | N_U32, N_EQK);
15994       Rm = 7;
15995     }
15996   else
15997     {
15998       constraint ((inst.operands[2].reg % 2) != 1, BAD_EVEN);
15999       if (inst.operands[2].reg == REG_SP)
16000         as_tsktsk (MVE_BAD_SP);
16001       else if (inst.operands[2].reg == REG_PC)
16002         first_error (BAD_PC);
16003
16004       rs = neon_select_shape (NS_QRRI, NS_NULL);
16005       et = neon_check_type (3, rs, N_KEY | N_U8 | N_U16 | N_U32, N_EQK, N_EQK);
16006       Rm = inst.operands[2].reg >> 1;
16007     }
16008   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16009   inst.instruction |= neon_logbits (et.size) << 20;
16010   inst.instruction |= inst.operands[1].reg << 16;
16011   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16012   inst.instruction |= (imm > 2) << 7;
16013   inst.instruction |= Rm << 1;
16014   inst.instruction |= (imm == 2 || imm == 8);
16015   inst.is_neon = 1;
16016 }
16017
16018 static void
16019 do_mve_vmlas (void)
16020 {
16021   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
16022   struct neon_type_el et
16023     = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16024
16025   if (inst.operands[2].reg == REG_PC)
16026     as_tsktsk (MVE_BAD_PC);
16027   else if (inst.operands[2].reg == REG_SP)
16028     as_tsktsk (MVE_BAD_SP);
16029
16030   if (inst.cond > COND_ALWAYS)
16031     inst.pred_insn_type = INSIDE_VPT_INSN;
16032   else
16033     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16034
16035   inst.instruction |= (et.type == NT_unsigned) << 28;
16036   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16037   inst.instruction |= neon_logbits (et.size) << 20;
16038   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16039   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16040   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16041   inst.instruction |= inst.operands[2].reg;
16042   inst.is_neon = 1;
16043 }
16044
16045 static void
16046 do_mve_vshll (void)
16047 {
16048   struct neon_type_el et
16049     = neon_check_type (2, NS_QQI, N_EQK, N_S8 | N_U8 | N_S16 | N_U16 | N_KEY);
16050
16051   if (inst.cond > COND_ALWAYS)
16052     inst.pred_insn_type = INSIDE_VPT_INSN;
16053   else
16054     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16055
16056   int imm = inst.operands[2].imm;
16057   constraint (imm < 1 || (unsigned)imm > et.size,
16058               _("immediate value out of range"));
16059
16060   if ((unsigned)imm == et.size)
16061     {
16062       inst.instruction |= neon_logbits (et.size) << 18;
16063       inst.instruction |= 0x110001;
16064     }
16065   else
16066     {
16067       inst.instruction |= (et.size + imm) << 16;
16068       inst.instruction |= 0x800140;
16069     }
16070
16071   inst.instruction |= (et.type == NT_unsigned) << 28;
16072   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16073   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16074   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16075   inst.instruction |= LOW4 (inst.operands[1].reg);
16076   inst.is_neon = 1;
16077 }
16078
16079 static void
16080 do_mve_vshlc (void)
16081 {
16082   if (inst.cond > COND_ALWAYS)
16083     inst.pred_insn_type = INSIDE_VPT_INSN;
16084   else
16085     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16086
16087   if (inst.operands[1].reg == REG_PC)
16088     as_tsktsk (MVE_BAD_PC);
16089   else if (inst.operands[1].reg == REG_SP)
16090     as_tsktsk (MVE_BAD_SP);
16091
16092   int imm = inst.operands[2].imm;
16093   constraint (imm < 1 || imm > 32, _("immediate value out of range"));
16094
16095   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16096   inst.instruction |= (imm & 0x1f) << 16;
16097   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16098   inst.instruction |= inst.operands[1].reg;
16099   inst.is_neon = 1;
16100 }
16101
16102 static void
16103 do_mve_vshrn (void)
16104 {
16105   unsigned types;
16106   switch (inst.instruction)
16107     {
16108     case M_MNEM_vshrnt:
16109     case M_MNEM_vshrnb:
16110     case M_MNEM_vrshrnt:
16111     case M_MNEM_vrshrnb:
16112       types = N_I16 | N_I32;
16113       break;
16114     case M_MNEM_vqshrnt:
16115     case M_MNEM_vqshrnb:
16116     case M_MNEM_vqrshrnt:
16117     case M_MNEM_vqrshrnb:
16118       types = N_U16 | N_U32 | N_S16 | N_S32;
16119       break;
16120     case M_MNEM_vqshrunt:
16121     case M_MNEM_vqshrunb:
16122     case M_MNEM_vqrshrunt:
16123     case M_MNEM_vqrshrunb:
16124       types = N_S16 | N_S32;
16125       break;
16126     default:
16127       abort ();
16128     }
16129
16130   struct neon_type_el et = neon_check_type (2, NS_QQI, N_EQK, types | N_KEY);
16131
16132   if (inst.cond > COND_ALWAYS)
16133     inst.pred_insn_type = INSIDE_VPT_INSN;
16134   else
16135     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16136
16137   unsigned Qd = inst.operands[0].reg;
16138   unsigned Qm = inst.operands[1].reg;
16139   unsigned imm = inst.operands[2].imm;
16140   constraint (imm < 1 || ((unsigned) imm) > (et.size / 2),
16141               et.size == 16
16142               ? _("immediate operand expected in the range [1,8]")
16143               : _("immediate operand expected in the range [1,16]"));
16144
16145   inst.instruction |= (et.type == NT_unsigned) << 28;
16146   inst.instruction |= HI1 (Qd) << 22;
16147   inst.instruction |= (et.size - imm) << 16;
16148   inst.instruction |= LOW4 (Qd) << 12;
16149   inst.instruction |= HI1 (Qm) << 5;
16150   inst.instruction |= LOW4 (Qm);
16151   inst.is_neon = 1;
16152 }
16153
16154 static void
16155 do_mve_vqmovn (void)
16156 {
16157   struct neon_type_el et;
16158   if (inst.instruction == M_MNEM_vqmovnt
16159      || inst.instruction == M_MNEM_vqmovnb)
16160     et = neon_check_type (2, NS_QQ, N_EQK,
16161                           N_U16 | N_U32 | N_S16 | N_S32 | N_KEY);
16162   else
16163     et = neon_check_type (2, NS_QQ, N_EQK, N_S16 | N_S32 | N_KEY);
16164
16165   if (inst.cond > COND_ALWAYS)
16166     inst.pred_insn_type = INSIDE_VPT_INSN;
16167   else
16168     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16169
16170   inst.instruction |= (et.type == NT_unsigned) << 28;
16171   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16172   inst.instruction |= (et.size == 32) << 18;
16173   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16174   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16175   inst.instruction |= LOW4 (inst.operands[1].reg);
16176   inst.is_neon = 1;
16177 }
16178
16179 static void
16180 do_mve_vpsel (void)
16181 {
16182   neon_select_shape (NS_QQQ, NS_NULL);
16183
16184   if (inst.cond > COND_ALWAYS)
16185     inst.pred_insn_type = INSIDE_VPT_INSN;
16186   else
16187     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16188
16189   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16190   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16191   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16192   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16193   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16194   inst.instruction |= LOW4 (inst.operands[2].reg);
16195   inst.is_neon = 1;
16196 }
16197
16198 static void
16199 do_mve_vpnot (void)
16200 {
16201   if (inst.cond > COND_ALWAYS)
16202     inst.pred_insn_type = INSIDE_VPT_INSN;
16203   else
16204     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16205 }
16206
16207 static void
16208 do_mve_vmaxnma_vminnma (void)
16209 {
16210   enum neon_shape rs = neon_select_shape (NS_QQ, NS_NULL);
16211   struct neon_type_el et
16212     = neon_check_type (2, rs, N_EQK, N_F_MVE | N_KEY);
16213
16214   if (inst.cond > COND_ALWAYS)
16215     inst.pred_insn_type = INSIDE_VPT_INSN;
16216   else
16217     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16218
16219   inst.instruction |= (et.size == 16) << 28;
16220   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16221   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16222   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16223   inst.instruction |= LOW4 (inst.operands[1].reg);
16224   inst.is_neon = 1;
16225 }
16226
16227 static void
16228 do_mve_vcmul (void)
16229 {
16230   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
16231   struct neon_type_el et
16232     = neon_check_type (3, rs, N_EQK, N_EQK, N_F_MVE | N_KEY);
16233
16234   if (inst.cond > COND_ALWAYS)
16235     inst.pred_insn_type = INSIDE_VPT_INSN;
16236   else
16237     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16238
16239   unsigned rot = inst.relocs[0].exp.X_add_number;
16240   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
16241               _("immediate out of range"));
16242
16243   if (et.size == 32 && (inst.operands[0].reg == inst.operands[1].reg
16244                         || inst.operands[0].reg == inst.operands[2].reg))
16245     as_tsktsk (BAD_MVE_SRCDEST);
16246
16247   inst.instruction |= (et.size == 32) << 28;
16248   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16249   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16250   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16251   inst.instruction |= (rot > 90) << 12;
16252   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16253   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16254   inst.instruction |= LOW4 (inst.operands[2].reg);
16255   inst.instruction |= (rot == 90 || rot == 270);
16256   inst.is_neon = 1;
16257 }
16258
16259 /* To handle the Low Overhead Loop instructions
16260    in Armv8.1-M Mainline and MVE.  */
16261 static void
16262 do_t_loloop (void)
16263 {
16264   unsigned long insn = inst.instruction;
16265
16266   inst.instruction = THUMB_OP32 (inst.instruction);
16267
16268   if (insn == T_MNEM_lctp)
16269     return;
16270
16271   set_pred_insn_type (MVE_OUTSIDE_PRED_INSN);
16272
16273   if (insn == T_MNEM_wlstp || insn == T_MNEM_dlstp)
16274     {
16275       struct neon_type_el et
16276        = neon_check_type (2, NS_RR, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16277       inst.instruction |= neon_logbits (et.size) << 20;
16278       inst.is_neon = 1;
16279     }
16280
16281   switch (insn)
16282     {
16283     case T_MNEM_letp:
16284       constraint (!inst.operands[0].present,
16285                   _("expected LR"));
16286       /* fall through.  */
16287     case T_MNEM_le:
16288       /* le <label>.  */
16289       if (!inst.operands[0].present)
16290        inst.instruction |= 1 << 21;
16291
16292       v8_1_loop_reloc (TRUE);
16293       break;
16294
16295     case T_MNEM_wls:
16296     case T_MNEM_wlstp:
16297       v8_1_loop_reloc (FALSE);
16298       /* fall through.  */
16299     case T_MNEM_dlstp:
16300     case T_MNEM_dls:
16301       constraint (inst.operands[1].isreg != 1, BAD_ARGS);
16302
16303       if (insn == T_MNEM_wlstp || insn == T_MNEM_dlstp)
16304        constraint (inst.operands[1].reg == REG_PC, BAD_PC);
16305       else if (inst.operands[1].reg == REG_PC)
16306        as_tsktsk (MVE_BAD_PC);
16307       if (inst.operands[1].reg == REG_SP)
16308        as_tsktsk (MVE_BAD_SP);
16309
16310       inst.instruction |= (inst.operands[1].reg << 16);
16311       break;
16312
16313     default:
16314       abort ();
16315     }
16316 }
16317
16318
16319 static void
16320 do_vfp_nsyn_cmp (void)
16321 {
16322   enum neon_shape rs;
16323   if (!inst.operands[0].isreg)
16324     {
16325       do_mve_vcmp ();
16326       return;
16327     }
16328   else
16329     {
16330       constraint (inst.operands[2].present, BAD_SYNTAX);
16331       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd),
16332                   BAD_FPU);
16333     }
16334
16335   if (inst.operands[1].isreg)
16336     {
16337       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
16338       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
16339
16340       if (rs == NS_FF || rs == NS_HH)
16341         {
16342           NEON_ENCODE (SINGLE, inst);
16343           do_vfp_sp_monadic ();
16344         }
16345       else
16346         {
16347           NEON_ENCODE (DOUBLE, inst);
16348           do_vfp_dp_rd_rm ();
16349         }
16350     }
16351   else
16352     {
16353       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
16354       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
16355
16356       switch (inst.instruction & 0x0fffffff)
16357         {
16358         case N_MNEM_vcmp:
16359           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
16360           break;
16361         case N_MNEM_vcmpe:
16362           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
16363           break;
16364         default:
16365           abort ();
16366         }
16367
16368       if (rs == NS_FI || rs == NS_HI)
16369         {
16370           NEON_ENCODE (SINGLE, inst);
16371           do_vfp_sp_compare_z ();
16372         }
16373       else
16374         {
16375           NEON_ENCODE (DOUBLE, inst);
16376           do_vfp_dp_rd ();
16377         }
16378     }
16379   do_vfp_cond_or_thumb ();
16380
16381   /* ARMv8.2 fp16 instruction.  */
16382   if (rs == NS_HI || rs == NS_HH)
16383     do_scalar_fp16_v82_encode ();
16384 }
16385
16386 static void
16387 nsyn_insert_sp (void)
16388 {
16389   inst.operands[1] = inst.operands[0];
16390   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
16391   inst.operands[0].reg = REG_SP;
16392   inst.operands[0].isreg = 1;
16393   inst.operands[0].writeback = 1;
16394   inst.operands[0].present = 1;
16395 }
16396
16397 static void
16398 do_vfp_nsyn_push (void)
16399 {
16400   nsyn_insert_sp ();
16401
16402   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16403               _("register list must contain at least 1 and at most 16 "
16404                 "registers"));
16405
16406   if (inst.operands[1].issingle)
16407     do_vfp_nsyn_opcode ("fstmdbs");
16408   else
16409     do_vfp_nsyn_opcode ("fstmdbd");
16410 }
16411
16412 static void
16413 do_vfp_nsyn_pop (void)
16414 {
16415   nsyn_insert_sp ();
16416
16417   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16418               _("register list must contain at least 1 and at most 16 "
16419                 "registers"));
16420
16421   if (inst.operands[1].issingle)
16422     do_vfp_nsyn_opcode ("fldmias");
16423   else
16424     do_vfp_nsyn_opcode ("fldmiad");
16425 }
16426
16427 /* Fix up Neon data-processing instructions, ORing in the correct bits for
16428    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
16429
16430 static void
16431 neon_dp_fixup (struct arm_it* insn)
16432 {
16433   unsigned int i = insn->instruction;
16434   insn->is_neon = 1;
16435
16436   if (thumb_mode)
16437     {
16438       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
16439       if (i & (1 << 24))
16440         i |= 1 << 28;
16441
16442       i &= ~(1 << 24);
16443
16444       i |= 0xef000000;
16445     }
16446   else
16447     i |= 0xf2000000;
16448
16449   insn->instruction = i;
16450 }
16451
16452 static void
16453 mve_encode_qqr (int size, int U, int fp)
16454 {
16455   if (inst.operands[2].reg == REG_SP)
16456     as_tsktsk (MVE_BAD_SP);
16457   else if (inst.operands[2].reg == REG_PC)
16458     as_tsktsk (MVE_BAD_PC);
16459
16460   if (fp)
16461     {
16462       /* vadd.  */
16463       if (((unsigned)inst.instruction) == 0xd00)
16464         inst.instruction = 0xee300f40;
16465       /* vsub.  */
16466       else if (((unsigned)inst.instruction) == 0x200d00)
16467         inst.instruction = 0xee301f40;
16468       /* vmul.  */
16469       else if (((unsigned)inst.instruction) == 0x1000d10)
16470         inst.instruction = 0xee310e60;
16471
16472       /* Setting size which is 1 for F16 and 0 for F32.  */
16473       inst.instruction |= (size == 16) << 28;
16474     }
16475   else
16476     {
16477       /* vadd.  */
16478       if (((unsigned)inst.instruction) == 0x800)
16479         inst.instruction = 0xee010f40;
16480       /* vsub.  */
16481       else if (((unsigned)inst.instruction) == 0x1000800)
16482         inst.instruction = 0xee011f40;
16483       /* vhadd.  */
16484       else if (((unsigned)inst.instruction) == 0)
16485         inst.instruction = 0xee000f40;
16486       /* vhsub.  */
16487       else if (((unsigned)inst.instruction) == 0x200)
16488         inst.instruction = 0xee001f40;
16489       /* vmla.  */
16490       else if (((unsigned)inst.instruction) == 0x900)
16491         inst.instruction = 0xee010e40;
16492       /* vmul.  */
16493       else if (((unsigned)inst.instruction) == 0x910)
16494         inst.instruction = 0xee011e60;
16495       /* vqadd.  */
16496       else if (((unsigned)inst.instruction) == 0x10)
16497         inst.instruction = 0xee000f60;
16498       /* vqsub.  */
16499       else if (((unsigned)inst.instruction) == 0x210)
16500         inst.instruction = 0xee001f60;
16501       /* vqrdmlah.  */
16502       else if (((unsigned)inst.instruction) == 0x3000b10)
16503         inst.instruction = 0xee000e40;
16504       /* vqdmulh.  */
16505       else if (((unsigned)inst.instruction) == 0x0000b00)
16506         inst.instruction = 0xee010e60;
16507       /* vqrdmulh.  */
16508       else if (((unsigned)inst.instruction) == 0x1000b00)
16509         inst.instruction = 0xfe010e60;
16510
16511       /* Set U-bit.  */
16512       inst.instruction |= U << 28;
16513
16514       /* Setting bits for size.  */
16515       inst.instruction |= neon_logbits (size) << 20;
16516     }
16517   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16518   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16519   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16520   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16521   inst.instruction |= inst.operands[2].reg;
16522   inst.is_neon = 1;
16523 }
16524
16525 static void
16526 mve_encode_rqq (unsigned bit28, unsigned size)
16527 {
16528   inst.instruction |= bit28 << 28;
16529   inst.instruction |= neon_logbits (size) << 20;
16530   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16531   inst.instruction |= inst.operands[0].reg << 12;
16532   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16533   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16534   inst.instruction |= LOW4 (inst.operands[2].reg);
16535   inst.is_neon = 1;
16536 }
16537
16538 static void
16539 mve_encode_qqq (int ubit, int size)
16540 {
16541
16542   inst.instruction |= (ubit != 0) << 28;
16543   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16544   inst.instruction |= neon_logbits (size) << 20;
16545   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16546   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16547   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16548   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16549   inst.instruction |= LOW4 (inst.operands[2].reg);
16550
16551   inst.is_neon = 1;
16552 }
16553
16554 static void
16555 mve_encode_rq (unsigned bit28, unsigned size)
16556 {
16557   inst.instruction |= bit28 << 28;
16558   inst.instruction |= neon_logbits (size) << 18;
16559   inst.instruction |= inst.operands[0].reg << 12;
16560   inst.instruction |= LOW4 (inst.operands[1].reg);
16561   inst.is_neon = 1;
16562 }
16563
16564 static void
16565 mve_encode_rrqq (unsigned U, unsigned size)
16566 {
16567   constraint (inst.operands[3].reg > 14, MVE_BAD_QREG);
16568
16569   inst.instruction |= U << 28;
16570   inst.instruction |= (inst.operands[1].reg >> 1) << 20;
16571   inst.instruction |= LOW4 (inst.operands[2].reg) << 16;
16572   inst.instruction |= (size == 32) << 16;
16573   inst.instruction |= inst.operands[0].reg << 12;
16574   inst.instruction |= HI1 (inst.operands[2].reg) << 7;
16575   inst.instruction |= inst.operands[3].reg;
16576   inst.is_neon = 1;
16577 }
16578
16579 /* Encode insns with bit pattern:
16580
16581   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16582   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
16583
16584   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
16585   different meaning for some instruction.  */
16586
16587 static void
16588 neon_three_same (int isquad, int ubit, int size)
16589 {
16590   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16591   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16592   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16593   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16594   inst.instruction |= LOW4 (inst.operands[2].reg);
16595   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16596   inst.instruction |= (isquad != 0) << 6;
16597   inst.instruction |= (ubit != 0) << 24;
16598   if (size != -1)
16599     inst.instruction |= neon_logbits (size) << 20;
16600
16601   neon_dp_fixup (&inst);
16602 }
16603
16604 /* Encode instructions of the form:
16605
16606   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
16607   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
16608
16609   Don't write size if SIZE == -1.  */
16610
16611 static void
16612 neon_two_same (int qbit, int ubit, int size)
16613 {
16614   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16615   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16616   inst.instruction |= LOW4 (inst.operands[1].reg);
16617   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16618   inst.instruction |= (qbit != 0) << 6;
16619   inst.instruction |= (ubit != 0) << 24;
16620
16621   if (size != -1)
16622     inst.instruction |= neon_logbits (size) << 18;
16623
16624   neon_dp_fixup (&inst);
16625 }
16626
16627 enum vfp_or_neon_is_neon_bits
16628 {
16629 NEON_CHECK_CC = 1,
16630 NEON_CHECK_ARCH = 2,
16631 NEON_CHECK_ARCH8 = 4
16632 };
16633
16634 /* Call this function if an instruction which may have belonged to the VFP or
16635  Neon instruction sets, but turned out to be a Neon instruction (due to the
16636  operand types involved, etc.). We have to check and/or fix-up a couple of
16637  things:
16638
16639    - Make sure the user hasn't attempted to make a Neon instruction
16640      conditional.
16641    - Alter the value in the condition code field if necessary.
16642    - Make sure that the arch supports Neon instructions.
16643
16644  Which of these operations take place depends on bits from enum
16645  vfp_or_neon_is_neon_bits.
16646
16647  WARNING: This function has side effects! If NEON_CHECK_CC is used and the
16648  current instruction's condition is COND_ALWAYS, the condition field is
16649  changed to inst.uncond_value.  This is necessary because instructions shared
16650  between VFP and Neon may be conditional for the VFP variants only, and the
16651  unconditional Neon version must have, e.g., 0xF in the condition field.  */
16652
16653 static int
16654 vfp_or_neon_is_neon (unsigned check)
16655 {
16656 /* Conditions are always legal in Thumb mode (IT blocks).  */
16657 if (!thumb_mode && (check & NEON_CHECK_CC))
16658   {
16659     if (inst.cond != COND_ALWAYS)
16660       {
16661         first_error (_(BAD_COND));
16662         return FAIL;
16663       }
16664     if (inst.uncond_value != -1)
16665       inst.instruction |= inst.uncond_value << 28;
16666   }
16667
16668
16669   if (((check & NEON_CHECK_ARCH) && !mark_feature_used (&fpu_neon_ext_v1))
16670       || ((check & NEON_CHECK_ARCH8)
16671           && !mark_feature_used (&fpu_neon_ext_armv8)))
16672     {
16673       first_error (_(BAD_FPU));
16674       return FAIL;
16675     }
16676
16677 return SUCCESS;
16678 }
16679
16680
16681 /* Return TRUE if the SIMD instruction is available for the current
16682    cpu_variant.  FP is set to TRUE if this is a SIMD floating-point
16683    instruction.  CHECK contains th.  CHECK contains the set of bits to pass to
16684    vfp_or_neon_is_neon for the NEON specific checks.  */
16685
16686 static bfd_boolean
16687 check_simd_pred_availability (int fp, unsigned check)
16688 {
16689 if (inst.cond > COND_ALWAYS)
16690   {
16691     if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16692       {
16693         inst.error = BAD_FPU;
16694         return FALSE;
16695       }
16696     inst.pred_insn_type = INSIDE_VPT_INSN;
16697   }
16698 else if (inst.cond < COND_ALWAYS)
16699   {
16700     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16701       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16702     else if (vfp_or_neon_is_neon (check) == FAIL)
16703       return FALSE;
16704   }
16705 else
16706   {
16707     if (!ARM_CPU_HAS_FEATURE (cpu_variant, fp ? mve_fp_ext : mve_ext)
16708         && vfp_or_neon_is_neon (check) == FAIL)
16709       return FALSE;
16710
16711     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16712       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16713   }
16714 return TRUE;
16715 }
16716
16717 /* Neon instruction encoders, in approximate order of appearance.  */
16718
16719 static void
16720 do_neon_dyadic_i_su (void)
16721 {
16722   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16723    return;
16724
16725   enum neon_shape rs;
16726   struct neon_type_el et;
16727   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16728     rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16729   else
16730     rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16731
16732   et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_32 | N_KEY);
16733
16734
16735   if (rs != NS_QQR)
16736     neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16737   else
16738     mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
16739 }
16740
16741 static void
16742 do_neon_dyadic_i64_su (void)
16743 {
16744   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
16745     return;
16746   enum neon_shape rs;
16747   struct neon_type_el et;
16748   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16749     {
16750       rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
16751       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16752     }
16753   else
16754     {
16755       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16756       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_ALL | N_KEY);
16757     }
16758   if (rs == NS_QQR)
16759     mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
16760   else
16761     neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16762 }
16763
16764 static void
16765 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
16766                 unsigned immbits)
16767 {
16768   unsigned size = et.size >> 3;
16769   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16770   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16771   inst.instruction |= LOW4 (inst.operands[1].reg);
16772   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16773   inst.instruction |= (isquad != 0) << 6;
16774   inst.instruction |= immbits << 16;
16775   inst.instruction |= (size >> 3) << 7;
16776   inst.instruction |= (size & 0x7) << 19;
16777   if (write_ubit)
16778     inst.instruction |= (uval != 0) << 24;
16779
16780   neon_dp_fixup (&inst);
16781 }
16782
16783 static void
16784 do_neon_shl (void)
16785 {
16786   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16787    return;
16788
16789   if (!inst.operands[2].isreg)
16790     {
16791       enum neon_shape rs;
16792       struct neon_type_el et;
16793       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16794         {
16795           rs = neon_select_shape (NS_QQI, NS_NULL);
16796           et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_MVE);
16797         }
16798       else
16799         {
16800           rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16801           et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
16802         }
16803       int imm = inst.operands[2].imm;
16804
16805       constraint (imm < 0 || (unsigned)imm >= et.size,
16806                   _("immediate out of range for shift"));
16807       NEON_ENCODE (IMMED, inst);
16808       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
16809     }
16810   else
16811     {
16812       enum neon_shape rs;
16813       struct neon_type_el et;
16814       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16815         {
16816           rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16817           et = neon_check_type (3, rs, N_EQK, N_SU_MVE | N_KEY, N_EQK | N_EQK);
16818         }
16819       else
16820         {
16821           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16822           et = neon_check_type (3, rs, N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
16823         }
16824
16825
16826       if (rs == NS_QQR)
16827         {
16828           constraint (inst.operands[0].reg != inst.operands[1].reg,
16829                        _("invalid instruction shape"));
16830           if (inst.operands[2].reg == REG_SP)
16831             as_tsktsk (MVE_BAD_SP);
16832           else if (inst.operands[2].reg == REG_PC)
16833             as_tsktsk (MVE_BAD_PC);
16834
16835           inst.instruction = 0xee311e60;
16836           inst.instruction |= (et.type == NT_unsigned) << 28;
16837           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16838           inst.instruction |= neon_logbits (et.size) << 18;
16839           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16840           inst.instruction |= inst.operands[2].reg;
16841           inst.is_neon = 1;
16842         }
16843       else
16844         {
16845           unsigned int tmp;
16846
16847           /* VSHL/VQSHL 3-register variants have syntax such as:
16848                vshl.xx Dd, Dm, Dn
16849              whereas other 3-register operations encoded by neon_three_same have
16850              syntax like:
16851                vadd.xx Dd, Dn, Dm
16852              (i.e. with Dn & Dm reversed). Swap operands[1].reg and
16853              operands[2].reg here.  */
16854           tmp = inst.operands[2].reg;
16855           inst.operands[2].reg = inst.operands[1].reg;
16856           inst.operands[1].reg = tmp;
16857           NEON_ENCODE (INTEGER, inst);
16858           neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16859         }
16860     }
16861 }
16862
16863 static void
16864 do_neon_qshl (void)
16865 {
16866   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16867    return;
16868
16869   if (!inst.operands[2].isreg)
16870     {
16871       enum neon_shape rs;
16872       struct neon_type_el et;
16873       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16874         {
16875           rs = neon_select_shape (NS_QQI, NS_NULL);
16876           et = neon_check_type (2, rs, N_EQK, N_KEY | N_SU_MVE);
16877         }
16878       else
16879         {
16880           rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16881           et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16882         }
16883       int imm = inst.operands[2].imm;
16884
16885       constraint (imm < 0 || (unsigned)imm >= et.size,
16886                   _("immediate out of range for shift"));
16887       NEON_ENCODE (IMMED, inst);
16888       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
16889     }
16890   else
16891     {
16892       enum neon_shape rs;
16893       struct neon_type_el et;
16894
16895       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16896         {
16897           rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16898           et = neon_check_type (3, rs, N_EQK, N_SU_MVE | N_KEY, N_EQK | N_EQK);
16899         }
16900       else
16901         {
16902           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16903           et = neon_check_type (3, rs, N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
16904         }
16905
16906       if (rs == NS_QQR)
16907         {
16908           constraint (inst.operands[0].reg != inst.operands[1].reg,
16909                        _("invalid instruction shape"));
16910           if (inst.operands[2].reg == REG_SP)
16911             as_tsktsk (MVE_BAD_SP);
16912           else if (inst.operands[2].reg == REG_PC)
16913             as_tsktsk (MVE_BAD_PC);
16914
16915           inst.instruction = 0xee311ee0;
16916           inst.instruction |= (et.type == NT_unsigned) << 28;
16917           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16918           inst.instruction |= neon_logbits (et.size) << 18;
16919           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16920           inst.instruction |= inst.operands[2].reg;
16921           inst.is_neon = 1;
16922         }
16923       else
16924         {
16925           unsigned int tmp;
16926
16927           /* See note in do_neon_shl.  */
16928           tmp = inst.operands[2].reg;
16929           inst.operands[2].reg = inst.operands[1].reg;
16930           inst.operands[1].reg = tmp;
16931           NEON_ENCODE (INTEGER, inst);
16932           neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16933         }
16934     }
16935 }
16936
16937 static void
16938 do_neon_rshl (void)
16939 {
16940   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16941    return;
16942
16943   enum neon_shape rs;
16944   struct neon_type_el et;
16945   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16946     {
16947       rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
16948       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16949     }
16950   else
16951     {
16952       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16953       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_ALL | N_KEY);
16954     }
16955
16956   unsigned int tmp;
16957
16958   if (rs == NS_QQR)
16959     {
16960       if (inst.operands[2].reg == REG_PC)
16961         as_tsktsk (MVE_BAD_PC);
16962       else if (inst.operands[2].reg == REG_SP)
16963         as_tsktsk (MVE_BAD_SP);
16964
16965       constraint (inst.operands[0].reg != inst.operands[1].reg,
16966                   _("invalid instruction shape"));
16967
16968       if (inst.instruction == 0x0000510)
16969         /* We are dealing with vqrshl.  */
16970         inst.instruction = 0xee331ee0;
16971       else
16972         /* We are dealing with vrshl.  */
16973         inst.instruction = 0xee331e60;
16974
16975       inst.instruction |= (et.type == NT_unsigned) << 28;
16976       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16977       inst.instruction |= neon_logbits (et.size) << 18;
16978       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16979       inst.instruction |= inst.operands[2].reg;
16980       inst.is_neon = 1;
16981     }
16982   else
16983     {
16984       tmp = inst.operands[2].reg;
16985       inst.operands[2].reg = inst.operands[1].reg;
16986       inst.operands[1].reg = tmp;
16987       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16988     }
16989 }
16990
16991 static int
16992 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
16993 {
16994   /* Handle .I8 pseudo-instructions.  */
16995   if (size == 8)
16996     {
16997       /* Unfortunately, this will make everything apart from zero out-of-range.
16998          FIXME is this the intended semantics? There doesn't seem much point in
16999          accepting .I8 if so.  */
17000       immediate |= immediate << 8;
17001       size = 16;
17002     }
17003
17004   if (size >= 32)
17005     {
17006       if (immediate == (immediate & 0x000000ff))
17007         {
17008           *immbits = immediate;
17009           return 0x1;
17010         }
17011       else if (immediate == (immediate & 0x0000ff00))
17012         {
17013           *immbits = immediate >> 8;
17014           return 0x3;
17015         }
17016       else if (immediate == (immediate & 0x00ff0000))
17017         {
17018           *immbits = immediate >> 16;
17019           return 0x5;
17020         }
17021       else if (immediate == (immediate & 0xff000000))
17022         {
17023           *immbits = immediate >> 24;
17024           return 0x7;
17025         }
17026       if ((immediate & 0xffff) != (immediate >> 16))
17027         goto bad_immediate;
17028       immediate &= 0xffff;
17029     }
17030
17031   if (immediate == (immediate & 0x000000ff))
17032     {
17033       *immbits = immediate;
17034       return 0x9;
17035     }
17036   else if (immediate == (immediate & 0x0000ff00))
17037     {
17038       *immbits = immediate >> 8;
17039       return 0xb;
17040     }
17041
17042   bad_immediate:
17043   first_error (_("immediate value out of range"));
17044   return FAIL;
17045 }
17046
17047 static void
17048 do_neon_logic (void)
17049 {
17050   if (inst.operands[2].present && inst.operands[2].isreg)
17051     {
17052       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17053       if (rs == NS_QQQ
17054           && !check_simd_pred_availability (FALSE,
17055                                             NEON_CHECK_ARCH | NEON_CHECK_CC))
17056         return;
17057       else if (rs != NS_QQQ
17058                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
17059         first_error (BAD_FPU);
17060
17061       neon_check_type (3, rs, N_IGNORE_TYPE);
17062       /* U bit and size field were set as part of the bitmask.  */
17063       NEON_ENCODE (INTEGER, inst);
17064       neon_three_same (neon_quad (rs), 0, -1);
17065     }
17066   else
17067     {
17068       const int three_ops_form = (inst.operands[2].present
17069                                   && !inst.operands[2].isreg);
17070       const int immoperand = (three_ops_form ? 2 : 1);
17071       enum neon_shape rs = (three_ops_form
17072                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
17073                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
17074       /* Because neon_select_shape makes the second operand a copy of the first
17075          if the second operand is not present.  */
17076       if (rs == NS_QQI
17077           && !check_simd_pred_availability (FALSE,
17078                                             NEON_CHECK_ARCH | NEON_CHECK_CC))
17079         return;
17080       else if (rs != NS_QQI
17081                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
17082         first_error (BAD_FPU);
17083
17084       struct neon_type_el et;
17085       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17086         et = neon_check_type (2, rs, N_I32 | N_I16 | N_KEY, N_EQK);
17087       else
17088         et = neon_check_type (2, rs, N_I8 | N_I16 | N_I32 | N_I64 | N_F32
17089                               | N_KEY, N_EQK);
17090
17091       if (et.type == NT_invtype)
17092         return;
17093       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
17094       unsigned immbits;
17095       int cmode;
17096
17097
17098       if (three_ops_form)
17099         constraint (inst.operands[0].reg != inst.operands[1].reg,
17100                     _("first and second operands shall be the same register"));
17101
17102       NEON_ENCODE (IMMED, inst);
17103
17104       immbits = inst.operands[immoperand].imm;
17105       if (et.size == 64)
17106         {
17107           /* .i64 is a pseudo-op, so the immediate must be a repeating
17108              pattern.  */
17109           if (immbits != (inst.operands[immoperand].regisimm ?
17110                           inst.operands[immoperand].reg : 0))
17111             {
17112               /* Set immbits to an invalid constant.  */
17113               immbits = 0xdeadbeef;
17114             }
17115         }
17116
17117       switch (opcode)
17118         {
17119         case N_MNEM_vbic:
17120           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17121           break;
17122
17123         case N_MNEM_vorr:
17124           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17125           break;
17126
17127         case N_MNEM_vand:
17128           /* Pseudo-instruction for VBIC.  */
17129           neon_invert_size (&immbits, 0, et.size);
17130           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17131           break;
17132
17133         case N_MNEM_vorn:
17134           /* Pseudo-instruction for VORR.  */
17135           neon_invert_size (&immbits, 0, et.size);
17136           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17137           break;
17138
17139         default:
17140           abort ();
17141         }
17142
17143       if (cmode == FAIL)
17144         return;
17145
17146       inst.instruction |= neon_quad (rs) << 6;
17147       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17148       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17149       inst.instruction |= cmode << 8;
17150       neon_write_immbits (immbits);
17151
17152       neon_dp_fixup (&inst);
17153     }
17154 }
17155
17156 static void
17157 do_neon_bitfield (void)
17158 {
17159   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17160   neon_check_type (3, rs, N_IGNORE_TYPE);
17161   neon_three_same (neon_quad (rs), 0, -1);
17162 }
17163
17164 static void
17165 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
17166                   unsigned destbits)
17167 {
17168   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17169   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
17170                                             types | N_KEY);
17171   if (et.type == NT_float)
17172     {
17173       NEON_ENCODE (FLOAT, inst);
17174       if (rs == NS_QQR)
17175         mve_encode_qqr (et.size, 0, 1);
17176       else
17177         neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
17178     }
17179   else
17180     {
17181       NEON_ENCODE (INTEGER, inst);
17182       if (rs == NS_QQR)
17183         mve_encode_qqr (et.size, et.type == ubit_meaning, 0);
17184       else
17185         neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
17186     }
17187 }
17188
17189
17190 static void
17191 do_neon_dyadic_if_su_d (void)
17192 {
17193   /* This version only allow D registers, but that constraint is enforced during
17194      operand parsing so we don't need to do anything extra here.  */
17195   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
17196 }
17197
17198 static void
17199 do_neon_dyadic_if_i_d (void)
17200 {
17201   /* The "untyped" case can't happen. Do this to stop the "U" bit being
17202      affected if we specify unsigned args.  */
17203   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17204 }
17205
17206 static void
17207 do_mve_vstr_vldr_QI (int size, int elsize, int load)
17208 {
17209   constraint (size < 32, BAD_ADDR_MODE);
17210   constraint (size != elsize, BAD_EL_TYPE);
17211   constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17212   constraint (!inst.operands[1].preind, BAD_ADDR_MODE);
17213   constraint (load && inst.operands[0].reg == inst.operands[1].reg,
17214               _("destination register and offset register may not be the"
17215                 " same"));
17216
17217   int imm = inst.relocs[0].exp.X_add_number;
17218   int add = 1;
17219   if (imm < 0)
17220     {
17221       add = 0;
17222       imm = -imm;
17223     }
17224   constraint ((imm % (size / 8) != 0)
17225               || imm > (0x7f << neon_logbits (size)),
17226               (size == 32) ? _("immediate must be a multiple of 4 in the"
17227                                " range of +/-[0,508]")
17228                            : _("immediate must be a multiple of 8 in the"
17229                                " range of +/-[0,1016]"));
17230   inst.instruction |= 0x11 << 24;
17231   inst.instruction |= add << 23;
17232   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17233   inst.instruction |= inst.operands[1].writeback << 21;
17234   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17235   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17236   inst.instruction |= 1 << 12;
17237   inst.instruction |= (size == 64) << 8;
17238   inst.instruction &= 0xffffff00;
17239   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17240   inst.instruction |= imm >> neon_logbits (size);
17241 }
17242
17243 static void
17244 do_mve_vstr_vldr_RQ (int size, int elsize, int load)
17245 {
17246     unsigned os = inst.operands[1].imm >> 5;
17247     constraint (os != 0 && size == 8,
17248                 _("can not shift offsets when accessing less than half-word"));
17249     constraint (os && os != neon_logbits (size),
17250                 _("shift immediate must be 1, 2 or 3 for half-word, word"
17251                   " or double-word accesses respectively"));
17252     if (inst.operands[1].reg == REG_PC)
17253       as_tsktsk (MVE_BAD_PC);
17254
17255     switch (size)
17256       {
17257       case 8:
17258         constraint (elsize >= 64, BAD_EL_TYPE);
17259         break;
17260       case 16:
17261         constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
17262         break;
17263       case 32:
17264       case 64:
17265         constraint (elsize != size, BAD_EL_TYPE);
17266         break;
17267       default:
17268         break;
17269       }
17270     constraint (inst.operands[1].writeback || !inst.operands[1].preind,
17271                 BAD_ADDR_MODE);
17272     if (load)
17273       {
17274         constraint (inst.operands[0].reg == (inst.operands[1].imm & 0x1f),
17275                     _("destination register and offset register may not be"
17276                     " the same"));
17277         constraint (size == elsize && inst.vectype.el[0].type != NT_unsigned,
17278                     BAD_EL_TYPE);
17279         constraint (inst.vectype.el[0].type != NT_unsigned
17280                     && inst.vectype.el[0].type != NT_signed, BAD_EL_TYPE);
17281         inst.instruction |= (inst.vectype.el[0].type == NT_unsigned) << 28;
17282       }
17283     else
17284       {
17285         constraint (inst.vectype.el[0].type != NT_untyped, BAD_EL_TYPE);
17286       }
17287
17288     inst.instruction |= 1 << 23;
17289     inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17290     inst.instruction |= inst.operands[1].reg << 16;
17291     inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17292     inst.instruction |= neon_logbits (elsize) << 7;
17293     inst.instruction |= HI1 (inst.operands[1].imm) << 5;
17294     inst.instruction |= LOW4 (inst.operands[1].imm);
17295     inst.instruction |= !!os;
17296 }
17297
17298 static void
17299 do_mve_vstr_vldr_RI (int size, int elsize, int load)
17300 {
17301   enum neon_el_type type = inst.vectype.el[0].type;
17302
17303   constraint (size >= 64, BAD_ADDR_MODE);
17304   switch (size)
17305     {
17306     case 16:
17307       constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
17308       break;
17309     case 32:
17310       constraint (elsize != size, BAD_EL_TYPE);
17311       break;
17312     default:
17313       break;
17314     }
17315   if (load)
17316     {
17317       constraint (elsize != size && type != NT_unsigned
17318                   && type != NT_signed, BAD_EL_TYPE);
17319     }
17320   else
17321     {
17322       constraint (elsize != size && type != NT_untyped, BAD_EL_TYPE);
17323     }
17324
17325   int imm = inst.relocs[0].exp.X_add_number;
17326   int add = 1;
17327   if (imm < 0)
17328     {
17329       add = 0;
17330       imm = -imm;
17331     }
17332
17333   if ((imm % (size / 8) != 0) || imm > (0x7f << neon_logbits (size)))
17334     {
17335       switch (size)
17336         {
17337         case 8:
17338           constraint (1, _("immediate must be in the range of +/-[0,127]"));
17339           break;
17340         case 16:
17341           constraint (1, _("immediate must be a multiple of 2 in the"
17342                            " range of +/-[0,254]"));
17343           break;
17344         case 32:
17345           constraint (1, _("immediate must be a multiple of 4 in the"
17346                            " range of +/-[0,508]"));
17347           break;
17348         }
17349     }
17350
17351   if (size != elsize)
17352     {
17353       constraint (inst.operands[1].reg > 7, BAD_HIREG);
17354       constraint (inst.operands[0].reg > 14,
17355                   _("MVE vector register in the range [Q0..Q7] expected"));
17356       inst.instruction |= (load && type == NT_unsigned) << 28;
17357       inst.instruction |= (size == 16) << 19;
17358       inst.instruction |= neon_logbits (elsize) << 7;
17359     }
17360   else
17361     {
17362       if (inst.operands[1].reg == REG_PC)
17363         as_tsktsk (MVE_BAD_PC);
17364       else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
17365         as_tsktsk (MVE_BAD_SP);
17366       inst.instruction |= 1 << 12;
17367       inst.instruction |= neon_logbits (size) << 7;
17368     }
17369   inst.instruction |= inst.operands[1].preind << 24;
17370   inst.instruction |= add << 23;
17371   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17372   inst.instruction |= inst.operands[1].writeback << 21;
17373   inst.instruction |= inst.operands[1].reg << 16;
17374   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17375   inst.instruction &= 0xffffff80;
17376   inst.instruction |= imm >> neon_logbits (size);
17377
17378 }
17379
17380 static void
17381 do_mve_vstr_vldr (void)
17382 {
17383   unsigned size;
17384   int load = 0;
17385
17386   if (inst.cond > COND_ALWAYS)
17387     inst.pred_insn_type = INSIDE_VPT_INSN;
17388   else
17389     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17390
17391   switch (inst.instruction)
17392     {
17393     default:
17394       gas_assert (0);
17395       break;
17396     case M_MNEM_vldrb:
17397       load = 1;
17398       /* fall through.  */
17399     case M_MNEM_vstrb:
17400       size = 8;
17401       break;
17402     case M_MNEM_vldrh:
17403       load = 1;
17404       /* fall through.  */
17405     case M_MNEM_vstrh:
17406       size = 16;
17407       break;
17408     case M_MNEM_vldrw:
17409       load = 1;
17410       /* fall through.  */
17411     case M_MNEM_vstrw:
17412       size = 32;
17413       break;
17414     case M_MNEM_vldrd:
17415       load = 1;
17416       /* fall through.  */
17417     case M_MNEM_vstrd:
17418       size = 64;
17419       break;
17420     }
17421   unsigned elsize = inst.vectype.el[0].size;
17422
17423   if (inst.operands[1].isquad)
17424     {
17425       /* We are dealing with [Q, imm]{!} cases.  */
17426       do_mve_vstr_vldr_QI (size, elsize, load);
17427     }
17428   else
17429     {
17430       if (inst.operands[1].immisreg == 2)
17431         {
17432           /* We are dealing with [R, Q, {UXTW #os}] cases.  */
17433           do_mve_vstr_vldr_RQ (size, elsize, load);
17434         }
17435       else if (!inst.operands[1].immisreg)
17436         {
17437           /* We are dealing with [R, Imm]{!}/[R], Imm cases.  */
17438           do_mve_vstr_vldr_RI (size, elsize, load);
17439         }
17440       else
17441         constraint (1, BAD_ADDR_MODE);
17442     }
17443
17444   inst.is_neon = 1;
17445 }
17446
17447 static void
17448 do_mve_vst_vld (void)
17449 {
17450   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17451     return;
17452
17453   constraint (!inst.operands[1].preind || inst.relocs[0].exp.X_add_symbol != 0
17454               || inst.relocs[0].exp.X_add_number != 0
17455               || inst.operands[1].immisreg != 0,
17456               BAD_ADDR_MODE);
17457   constraint (inst.vectype.el[0].size > 32, BAD_EL_TYPE);
17458   if (inst.operands[1].reg == REG_PC)
17459     as_tsktsk (MVE_BAD_PC);
17460   else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
17461     as_tsktsk (MVE_BAD_SP);
17462
17463
17464   /* These instructions are one of the "exceptions" mentioned in
17465      handle_pred_state.  They are MVE instructions that are not VPT compatible
17466      and do not accept a VPT code, thus appending such a code is a syntax
17467      error.  */
17468   if (inst.cond > COND_ALWAYS)
17469     first_error (BAD_SYNTAX);
17470   /* If we append a scalar condition code we can set this to
17471      MVE_OUTSIDE_PRED_INSN as it will also lead to a syntax error.  */
17472   else if (inst.cond < COND_ALWAYS)
17473     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17474   else
17475     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
17476
17477   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17478   inst.instruction |= inst.operands[1].writeback << 21;
17479   inst.instruction |= inst.operands[1].reg << 16;
17480   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17481   inst.instruction |= neon_logbits (inst.vectype.el[0].size) << 7;
17482   inst.is_neon = 1;
17483 }
17484
17485 static void
17486 do_mve_vaddlv (void)
17487 {
17488   enum neon_shape rs = neon_select_shape (NS_RRQ, NS_NULL);
17489   struct neon_type_el et
17490     = neon_check_type (3, rs, N_EQK, N_EQK, N_S32 | N_U32 | N_KEY);
17491
17492   if (et.type == NT_invtype)
17493     first_error (BAD_EL_TYPE);
17494
17495   if (inst.cond > COND_ALWAYS)
17496     inst.pred_insn_type = INSIDE_VPT_INSN;
17497   else
17498     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17499
17500   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
17501
17502   inst.instruction |= (et.type == NT_unsigned) << 28;
17503   inst.instruction |= inst.operands[1].reg << 19;
17504   inst.instruction |= inst.operands[0].reg << 12;
17505   inst.instruction |= inst.operands[2].reg;
17506   inst.is_neon = 1;
17507 }
17508
17509 static void
17510 do_neon_dyadic_if_su (void)
17511 {
17512   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17513   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
17514                                             N_SUF_32 | N_KEY);
17515
17516   constraint ((inst.instruction == ((unsigned) N_MNEM_vmax)
17517                || inst.instruction == ((unsigned) N_MNEM_vmin))
17518               && et.type == NT_float
17519               && !ARM_CPU_HAS_FEATURE (cpu_variant,fpu_neon_ext_v1), BAD_FPU);
17520
17521   if (!check_simd_pred_availability (et.type == NT_float,
17522                                      NEON_CHECK_ARCH | NEON_CHECK_CC))
17523     return;
17524
17525   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
17526 }
17527
17528 static void
17529 do_neon_addsub_if_i (void)
17530 {
17531   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
17532       && try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
17533     return;
17534
17535   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17536   struct neon_type_el et = neon_check_type (3, rs, N_EQK,
17537                                             N_EQK, N_IF_32 | N_I64 | N_KEY);
17538
17539   constraint (rs == NS_QQR && et.size == 64, BAD_FPU);
17540   /* If we are parsing Q registers and the element types match MVE, which NEON
17541      also supports, then we must check whether this is an instruction that can
17542      be used by both MVE/NEON.  This distinction can be made based on whether
17543      they are predicated or not.  */
17544   if ((rs == NS_QQQ || rs == NS_QQR) && et.size != 64)
17545     {
17546       if (!check_simd_pred_availability (et.type == NT_float,
17547                                          NEON_CHECK_ARCH | NEON_CHECK_CC))
17548         return;
17549     }
17550   else
17551     {
17552       /* If they are either in a D register or are using an unsupported.  */
17553       if (rs != NS_QQR
17554           && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17555         return;
17556     }
17557
17558   /* The "untyped" case can't happen. Do this to stop the "U" bit being
17559      affected if we specify unsigned args.  */
17560   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
17561 }
17562
17563 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
17564    result to be:
17565      V<op> A,B     (A is operand 0, B is operand 2)
17566    to mean:
17567      V<op> A,B,A
17568    not:
17569      V<op> A,B,B
17570    so handle that case specially.  */
17571
17572 static void
17573 neon_exchange_operands (void)
17574 {
17575   if (inst.operands[1].present)
17576     {
17577       void *scratch = xmalloc (sizeof (inst.operands[0]));
17578
17579       /* Swap operands[1] and operands[2].  */
17580       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
17581       inst.operands[1] = inst.operands[2];
17582       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
17583       free (scratch);
17584     }
17585   else
17586     {
17587       inst.operands[1] = inst.operands[2];
17588       inst.operands[2] = inst.operands[0];
17589     }
17590 }
17591
17592 static void
17593 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
17594 {
17595   if (inst.operands[2].isreg)
17596     {
17597       if (invert)
17598         neon_exchange_operands ();
17599       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
17600     }
17601   else
17602     {
17603       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17604       struct neon_type_el et = neon_check_type (2, rs,
17605         N_EQK | N_SIZ, immtypes | N_KEY);
17606
17607       NEON_ENCODE (IMMED, inst);
17608       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17609       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17610       inst.instruction |= LOW4 (inst.operands[1].reg);
17611       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17612       inst.instruction |= neon_quad (rs) << 6;
17613       inst.instruction |= (et.type == NT_float) << 10;
17614       inst.instruction |= neon_logbits (et.size) << 18;
17615
17616       neon_dp_fixup (&inst);
17617     }
17618 }
17619
17620 static void
17621 do_neon_cmp (void)
17622 {
17623   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
17624 }
17625
17626 static void
17627 do_neon_cmp_inv (void)
17628 {
17629   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
17630 }
17631
17632 static void
17633 do_neon_ceq (void)
17634 {
17635   neon_compare (N_IF_32, N_IF_32, FALSE);
17636 }
17637
17638 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
17639    scalars, which are encoded in 5 bits, M : Rm.
17640    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
17641    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
17642    index in M.
17643
17644    Dot Product instructions are similar to multiply instructions except elsize
17645    should always be 32.
17646
17647    This function translates SCALAR, which is GAS's internal encoding of indexed
17648    scalar register, to raw encoding.  There is also register and index range
17649    check based on ELSIZE.  */
17650
17651 static unsigned
17652 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
17653 {
17654   unsigned regno = NEON_SCALAR_REG (scalar);
17655   unsigned elno = NEON_SCALAR_INDEX (scalar);
17656
17657   switch (elsize)
17658     {
17659     case 16:
17660       if (regno > 7 || elno > 3)
17661         goto bad_scalar;
17662       return regno | (elno << 3);
17663
17664     case 32:
17665       if (regno > 15 || elno > 1)
17666         goto bad_scalar;
17667       return regno | (elno << 4);
17668
17669     default:
17670     bad_scalar:
17671       first_error (_("scalar out of range for multiply instruction"));
17672     }
17673
17674   return 0;
17675 }
17676
17677 /* Encode multiply / multiply-accumulate scalar instructions.  */
17678
17679 static void
17680 neon_mul_mac (struct neon_type_el et, int ubit)
17681 {
17682   unsigned scalar;
17683
17684   /* Give a more helpful error message if we have an invalid type.  */
17685   if (et.type == NT_invtype)
17686     return;
17687
17688   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
17689   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17690   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17691   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17692   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17693   inst.instruction |= LOW4 (scalar);
17694   inst.instruction |= HI1 (scalar) << 5;
17695   inst.instruction |= (et.type == NT_float) << 8;
17696   inst.instruction |= neon_logbits (et.size) << 20;
17697   inst.instruction |= (ubit != 0) << 24;
17698
17699   neon_dp_fixup (&inst);
17700 }
17701
17702 static void
17703 do_neon_mac_maybe_scalar (void)
17704 {
17705   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
17706     return;
17707
17708   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17709     return;
17710
17711   if (inst.operands[2].isscalar)
17712     {
17713       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17714       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17715       struct neon_type_el et = neon_check_type (3, rs,
17716         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
17717       NEON_ENCODE (SCALAR, inst);
17718       neon_mul_mac (et, neon_quad (rs));
17719     }
17720   else if (!inst.operands[2].isvec)
17721     {
17722       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17723
17724       enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17725       neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
17726
17727       neon_dyadic_misc (NT_unsigned, N_SU_MVE, 0);
17728     }
17729   else
17730     {
17731       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17732       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
17733          affected if we specify unsigned args.  */
17734       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17735     }
17736 }
17737
17738 static void
17739 do_neon_fmac (void)
17740 {
17741   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_fma)
17742       && try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
17743     return;
17744
17745   if (!check_simd_pred_availability (TRUE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17746     return;
17747
17748   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
17749     {
17750       enum neon_shape rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
17751       struct neon_type_el et = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK,
17752                                                 N_EQK);
17753
17754       if (rs == NS_QQR)
17755         {
17756           if (inst.operands[2].reg == REG_SP)
17757             as_tsktsk (MVE_BAD_SP);
17758           else if (inst.operands[2].reg == REG_PC)
17759             as_tsktsk (MVE_BAD_PC);
17760
17761           inst.instruction = 0xee310e40;
17762           inst.instruction |= (et.size == 16) << 28;
17763           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17764           inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17765           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17766           inst.instruction |= HI1 (inst.operands[1].reg) << 6;
17767           inst.instruction |= inst.operands[2].reg;
17768           inst.is_neon = 1;
17769           return;
17770         }
17771     }
17772   else
17773     {
17774       constraint (!inst.operands[2].isvec, BAD_FPU);
17775     }
17776
17777   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17778 }
17779
17780 static void
17781 do_neon_tst (void)
17782 {
17783   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17784   struct neon_type_el et = neon_check_type (3, rs,
17785     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
17786   neon_three_same (neon_quad (rs), 0, et.size);
17787 }
17788
17789 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
17790    same types as the MAC equivalents. The polynomial type for this instruction
17791    is encoded the same as the integer type.  */
17792
17793 static void
17794 do_neon_mul (void)
17795 {
17796   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
17797     return;
17798
17799   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17800     return;
17801
17802   if (inst.operands[2].isscalar)
17803     {
17804       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17805       do_neon_mac_maybe_scalar ();
17806     }
17807   else
17808     {
17809       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17810         {
17811           enum neon_shape rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
17812           struct neon_type_el et
17813             = neon_check_type (3, rs, N_EQK, N_EQK, N_I_MVE | N_F_MVE | N_KEY);
17814           if (et.type == NT_float)
17815             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
17816                         BAD_FPU);
17817
17818           neon_dyadic_misc (NT_float, N_I_MVE | N_F_MVE, 0);
17819         }
17820       else
17821         {
17822           constraint (!inst.operands[2].isvec, BAD_FPU);
17823           neon_dyadic_misc (NT_poly,
17824                             N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
17825         }
17826     }
17827 }
17828
17829 static void
17830 do_neon_qdmulh (void)
17831 {
17832   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
17833    return;
17834
17835   if (inst.operands[2].isscalar)
17836     {
17837       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17838       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17839       struct neon_type_el et = neon_check_type (3, rs,
17840         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17841       NEON_ENCODE (SCALAR, inst);
17842       neon_mul_mac (et, neon_quad (rs));
17843     }
17844   else
17845     {
17846       enum neon_shape rs;
17847       struct neon_type_el et;
17848       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17849         {
17850           rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
17851           et = neon_check_type (3, rs,
17852             N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17853         }
17854       else
17855         {
17856           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17857           et = neon_check_type (3, rs,
17858             N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17859         }
17860
17861       NEON_ENCODE (INTEGER, inst);
17862       if (rs == NS_QQR)
17863         mve_encode_qqr (et.size, 0, 0);
17864       else
17865         /* The U bit (rounding) comes from bit mask.  */
17866         neon_three_same (neon_quad (rs), 0, et.size);
17867     }
17868 }
17869
17870 static void
17871 do_mve_vaddv (void)
17872 {
17873   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
17874   struct neon_type_el et
17875     = neon_check_type (2, rs, N_EQK,  N_SU_32 | N_KEY);
17876
17877   if (et.type == NT_invtype)
17878     first_error (BAD_EL_TYPE);
17879
17880   if (inst.cond > COND_ALWAYS)
17881     inst.pred_insn_type = INSIDE_VPT_INSN;
17882   else
17883     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17884
17885   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
17886
17887   mve_encode_rq (et.type == NT_unsigned, et.size);
17888 }
17889
17890 static void
17891 do_mve_vhcadd (void)
17892 {
17893   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
17894   struct neon_type_el et
17895     = neon_check_type (3, rs, N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17896
17897   if (inst.cond > COND_ALWAYS)
17898     inst.pred_insn_type = INSIDE_VPT_INSN;
17899   else
17900     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17901
17902   unsigned rot = inst.relocs[0].exp.X_add_number;
17903   constraint (rot != 90 && rot != 270, _("immediate out of range"));
17904
17905   if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
17906     as_tsktsk (_("Warning: 32-bit element size and same first and third "
17907                  "operand makes instruction UNPREDICTABLE"));
17908
17909   mve_encode_qqq (0, et.size);
17910   inst.instruction |= (rot == 270) << 12;
17911   inst.is_neon = 1;
17912 }
17913
17914 static void
17915 do_mve_vqdmull (void)
17916 {
17917   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
17918   struct neon_type_el et
17919     = neon_check_type (3, rs, N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17920
17921   if (et.size == 32
17922       && (inst.operands[0].reg == inst.operands[1].reg
17923           || (rs == NS_QQQ && inst.operands[0].reg == inst.operands[2].reg)))
17924     as_tsktsk (BAD_MVE_SRCDEST);
17925
17926   if (inst.cond > COND_ALWAYS)
17927     inst.pred_insn_type = INSIDE_VPT_INSN;
17928   else
17929     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17930
17931   if (rs == NS_QQQ)
17932     {
17933       mve_encode_qqq (et.size == 32, 64);
17934       inst.instruction |= 1;
17935     }
17936   else
17937     {
17938       mve_encode_qqr (64, et.size == 32, 0);
17939       inst.instruction |= 0x3 << 5;
17940     }
17941 }
17942
17943 static void
17944 do_mve_vadc (void)
17945 {
17946   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17947   struct neon_type_el et
17948     = neon_check_type (3, rs, N_KEY | N_I32, N_EQK, N_EQK);
17949
17950   if (et.type == NT_invtype)
17951     first_error (BAD_EL_TYPE);
17952
17953   if (inst.cond > COND_ALWAYS)
17954     inst.pred_insn_type = INSIDE_VPT_INSN;
17955   else
17956     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17957
17958   mve_encode_qqq (0, 64);
17959 }
17960
17961 static void
17962 do_mve_vbrsr (void)
17963 {
17964   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17965   struct neon_type_el et
17966     = neon_check_type (3, rs, N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
17967
17968   if (inst.cond > COND_ALWAYS)
17969     inst.pred_insn_type = INSIDE_VPT_INSN;
17970   else
17971     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17972
17973   mve_encode_qqr (et.size, 0, 0);
17974 }
17975
17976 static void
17977 do_mve_vsbc (void)
17978 {
17979   neon_check_type (3, NS_QQQ, N_EQK, N_EQK, N_I32 | N_KEY);
17980
17981   if (inst.cond > COND_ALWAYS)
17982     inst.pred_insn_type = INSIDE_VPT_INSN;
17983   else
17984     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17985
17986   mve_encode_qqq (1, 64);
17987 }
17988
17989 static void
17990 do_mve_vmulh (void)
17991 {
17992   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17993   struct neon_type_el et
17994     = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
17995
17996   if (inst.cond > COND_ALWAYS)
17997     inst.pred_insn_type = INSIDE_VPT_INSN;
17998   else
17999     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18000
18001   mve_encode_qqq (et.type == NT_unsigned, et.size);
18002 }
18003
18004 static void
18005 do_mve_vqdmlah (void)
18006 {
18007   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
18008   struct neon_type_el et
18009     = neon_check_type (3, rs, N_EQK, N_EQK, N_S_32 | N_KEY);
18010
18011   if (inst.cond > COND_ALWAYS)
18012     inst.pred_insn_type = INSIDE_VPT_INSN;
18013   else
18014     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18015
18016   mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
18017 }
18018
18019 static void
18020 do_mve_vqdmladh (void)
18021 {
18022   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
18023   struct neon_type_el et
18024     = neon_check_type (3, rs, N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18025
18026   if (inst.cond > COND_ALWAYS)
18027     inst.pred_insn_type = INSIDE_VPT_INSN;
18028   else
18029     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18030
18031   mve_encode_qqq (0, et.size);
18032 }
18033
18034
18035 static void
18036 do_mve_vmull (void)
18037 {
18038
18039   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_DDS,
18040                                           NS_QQS, NS_QQQ, NS_QQR, NS_NULL);
18041   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
18042       && inst.cond == COND_ALWAYS
18043       && ((unsigned)inst.instruction) == M_MNEM_vmullt)
18044     {
18045       if (rs == NS_QQQ)
18046         {
18047
18048           struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
18049                                                     N_SUF_32 | N_F64 | N_P8
18050                                                     | N_P16 | N_I_MVE | N_KEY);
18051           if (((et.type == NT_poly) && et.size == 8
18052                && ARM_CPU_IS_ANY (cpu_variant))
18053               || (et.type == NT_integer) || (et.type == NT_float))
18054             goto neon_vmul;
18055         }
18056       else
18057         goto neon_vmul;
18058     }
18059
18060   constraint (rs != NS_QQQ, BAD_FPU);
18061   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
18062                                             N_SU_32 | N_P8 | N_P16 | N_KEY);
18063
18064   /* We are dealing with MVE's vmullt.  */
18065   if (et.size == 32
18066       && (inst.operands[0].reg == inst.operands[1].reg
18067           || inst.operands[0].reg == inst.operands[2].reg))
18068     as_tsktsk (BAD_MVE_SRCDEST);
18069
18070   if (inst.cond > COND_ALWAYS)
18071     inst.pred_insn_type = INSIDE_VPT_INSN;
18072   else
18073     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18074
18075   if (et.type == NT_poly)
18076     mve_encode_qqq (neon_logbits (et.size), 64);
18077   else
18078     mve_encode_qqq (et.type == NT_unsigned, et.size);
18079
18080   return;
18081
18082 neon_vmul:
18083   inst.instruction = N_MNEM_vmul;
18084   inst.cond = 0xb;
18085   if (thumb_mode)
18086     inst.pred_insn_type = INSIDE_IT_INSN;
18087   do_neon_mul ();
18088 }
18089
18090 static void
18091 do_mve_vabav (void)
18092 {
18093   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
18094
18095   if (rs == NS_NULL)
18096     return;
18097
18098   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18099     return;
18100
18101   struct neon_type_el et = neon_check_type (2, NS_NULL, N_EQK, N_KEY | N_S8
18102                                             | N_S16 | N_S32 | N_U8 | N_U16
18103                                             | N_U32);
18104
18105   if (inst.cond > COND_ALWAYS)
18106     inst.pred_insn_type = INSIDE_VPT_INSN;
18107   else
18108     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18109
18110   mve_encode_rqq (et.type == NT_unsigned, et.size);
18111 }
18112
18113 static void
18114 do_mve_vmladav (void)
18115 {
18116   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
18117   struct neon_type_el et = neon_check_type (3, rs,
18118                                             N_EQK, N_EQK, N_SU_MVE | N_KEY);
18119
18120   if (et.type == NT_unsigned
18121       && (inst.instruction == M_MNEM_vmladavx
18122           || inst.instruction == M_MNEM_vmladavax
18123           || inst.instruction == M_MNEM_vmlsdav
18124           || inst.instruction == M_MNEM_vmlsdava
18125           || inst.instruction == M_MNEM_vmlsdavx
18126           || inst.instruction == M_MNEM_vmlsdavax))
18127     first_error (BAD_SIMD_TYPE);
18128
18129   constraint (inst.operands[2].reg > 14,
18130               _("MVE vector register in the range [Q0..Q7] expected"));
18131
18132   if (inst.cond > COND_ALWAYS)
18133     inst.pred_insn_type = INSIDE_VPT_INSN;
18134   else
18135     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18136
18137   if (inst.instruction == M_MNEM_vmlsdav
18138       || inst.instruction == M_MNEM_vmlsdava
18139       || inst.instruction == M_MNEM_vmlsdavx
18140       || inst.instruction == M_MNEM_vmlsdavax)
18141     inst.instruction |= (et.size == 8) << 28;
18142   else
18143     inst.instruction |= (et.size == 8) << 8;
18144
18145   mve_encode_rqq (et.type == NT_unsigned, 64);
18146   inst.instruction |= (et.size == 32) << 16;
18147 }
18148
18149 static void
18150 do_mve_vmlaldav (void)
18151 {
18152   enum neon_shape rs = neon_select_shape (NS_RRQQ, NS_NULL);
18153   struct neon_type_el et
18154     = neon_check_type (4, rs, N_EQK, N_EQK, N_EQK,
18155                        N_S16 | N_S32 | N_U16 | N_U32 | N_KEY);
18156
18157   if (et.type == NT_unsigned
18158       && (inst.instruction == M_MNEM_vmlsldav
18159           || inst.instruction == M_MNEM_vmlsldava
18160           || inst.instruction == M_MNEM_vmlsldavx
18161           || inst.instruction == M_MNEM_vmlsldavax))
18162     first_error (BAD_SIMD_TYPE);
18163
18164   if (inst.cond > COND_ALWAYS)
18165     inst.pred_insn_type = INSIDE_VPT_INSN;
18166   else
18167     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18168
18169   mve_encode_rrqq (et.type == NT_unsigned, et.size);
18170 }
18171
18172 static void
18173 do_mve_vrmlaldavh (void)
18174 {
18175   struct neon_type_el et;
18176   if (inst.instruction == M_MNEM_vrmlsldavh
18177      || inst.instruction == M_MNEM_vrmlsldavha
18178      || inst.instruction == M_MNEM_vrmlsldavhx
18179      || inst.instruction == M_MNEM_vrmlsldavhax)
18180     {
18181       et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK, N_S32 | N_KEY);
18182       if (inst.operands[1].reg == REG_SP)
18183         as_tsktsk (MVE_BAD_SP);
18184     }
18185   else
18186     {
18187       if (inst.instruction == M_MNEM_vrmlaldavhx
18188           || inst.instruction == M_MNEM_vrmlaldavhax)
18189         et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK, N_S32 | N_KEY);
18190       else
18191         et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK,
18192                               N_U32 | N_S32 | N_KEY);
18193       /* vrmlaldavh's encoding with SP as the second, odd, GPR operand may alias
18194          with vmax/min instructions, making the use of SP in assembly really
18195          nonsensical, so instead of issuing a warning like we do for other uses
18196          of SP for the odd register operand we error out.  */
18197       constraint (inst.operands[1].reg == REG_SP, BAD_SP);
18198     }
18199
18200   /* Make sure we still check the second operand is an odd one and that PC is
18201      disallowed.  This because we are parsing for any GPR operand, to be able
18202      to distinguish between giving a warning or an error for SP as described
18203      above.  */
18204   constraint ((inst.operands[1].reg % 2) != 1, BAD_EVEN);
18205   constraint (inst.operands[1].reg == REG_PC, BAD_PC);
18206
18207   if (inst.cond > COND_ALWAYS)
18208     inst.pred_insn_type = INSIDE_VPT_INSN;
18209   else
18210     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18211
18212   mve_encode_rrqq (et.type == NT_unsigned, 0);
18213 }
18214
18215
18216 static void
18217 do_mve_vmaxnmv (void)
18218 {
18219   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
18220   struct neon_type_el et
18221     = neon_check_type (2, rs, N_EQK, N_F_MVE | N_KEY);
18222
18223   if (inst.cond > COND_ALWAYS)
18224     inst.pred_insn_type = INSIDE_VPT_INSN;
18225   else
18226     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18227
18228   if (inst.operands[0].reg == REG_SP)
18229     as_tsktsk (MVE_BAD_SP);
18230   else if (inst.operands[0].reg == REG_PC)
18231     as_tsktsk (MVE_BAD_PC);
18232
18233   mve_encode_rq (et.size == 16, 64);
18234 }
18235
18236 static void
18237 do_mve_vmaxv (void)
18238 {
18239   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
18240   struct neon_type_el et;
18241
18242   if (inst.instruction == M_MNEM_vmaxv || inst.instruction == M_MNEM_vminv)
18243     et = neon_check_type (2, rs, N_EQK, N_SU_MVE | N_KEY);
18244   else
18245     et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18246
18247   if (inst.cond > COND_ALWAYS)
18248     inst.pred_insn_type = INSIDE_VPT_INSN;
18249   else
18250     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18251
18252   if (inst.operands[0].reg == REG_SP)
18253     as_tsktsk (MVE_BAD_SP);
18254   else if (inst.operands[0].reg == REG_PC)
18255     as_tsktsk (MVE_BAD_PC);
18256
18257   mve_encode_rq (et.type == NT_unsigned, et.size);
18258 }
18259
18260
18261 static void
18262 do_neon_qrdmlah (void)
18263 {
18264   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18265    return;
18266   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18267     {
18268       /* Check we're on the correct architecture.  */
18269       if (!mark_feature_used (&fpu_neon_ext_armv8))
18270         inst.error
18271           = _("instruction form not available on this architecture.");
18272       else if (!mark_feature_used (&fpu_neon_ext_v8_1))
18273         {
18274           as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
18275           record_feature_use (&fpu_neon_ext_v8_1);
18276         }
18277         if (inst.operands[2].isscalar)
18278           {
18279             enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
18280             struct neon_type_el et = neon_check_type (3, rs,
18281               N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
18282             NEON_ENCODE (SCALAR, inst);
18283             neon_mul_mac (et, neon_quad (rs));
18284           }
18285         else
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,
18289               N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
18290             NEON_ENCODE (INTEGER, inst);
18291             /* The U bit (rounding) comes from bit mask.  */
18292             neon_three_same (neon_quad (rs), 0, et.size);
18293           }
18294     }
18295   else
18296     {
18297       enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
18298       struct neon_type_el et
18299         = neon_check_type (3, rs, N_EQK, N_EQK, N_S_32 | N_KEY);
18300
18301       NEON_ENCODE (INTEGER, inst);
18302       mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
18303     }
18304 }
18305
18306 static void
18307 do_neon_fcmp_absolute (void)
18308 {
18309   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18310   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
18311                                             N_F_16_32 | N_KEY);
18312   /* Size field comes from bit mask.  */
18313   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
18314 }
18315
18316 static void
18317 do_neon_fcmp_absolute_inv (void)
18318 {
18319   neon_exchange_operands ();
18320   do_neon_fcmp_absolute ();
18321 }
18322
18323 static void
18324 do_neon_step (void)
18325 {
18326   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18327   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
18328                                             N_F_16_32 | N_KEY);
18329   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
18330 }
18331
18332 static void
18333 do_neon_abs_neg (void)
18334 {
18335   enum neon_shape rs;
18336   struct neon_type_el et;
18337
18338   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
18339     return;
18340
18341   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18342   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
18343
18344   if (!check_simd_pred_availability (et.type == NT_float,
18345                                      NEON_CHECK_ARCH | NEON_CHECK_CC))
18346     return;
18347
18348   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18349   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18350   inst.instruction |= LOW4 (inst.operands[1].reg);
18351   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18352   inst.instruction |= neon_quad (rs) << 6;
18353   inst.instruction |= (et.type == NT_float) << 10;
18354   inst.instruction |= neon_logbits (et.size) << 18;
18355
18356   neon_dp_fixup (&inst);
18357 }
18358
18359 static void
18360 do_neon_sli (void)
18361 {
18362   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18363     return;
18364
18365   enum neon_shape rs;
18366   struct neon_type_el et;
18367   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18368     {
18369       rs = neon_select_shape (NS_QQI, NS_NULL);
18370       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_KEY);
18371     }
18372   else
18373     {
18374       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18375       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18376     }
18377
18378
18379   int imm = inst.operands[2].imm;
18380   constraint (imm < 0 || (unsigned)imm >= et.size,
18381               _("immediate out of range for insert"));
18382   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
18383 }
18384
18385 static void
18386 do_neon_sri (void)
18387 {
18388   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18389     return;
18390
18391   enum neon_shape rs;
18392   struct neon_type_el et;
18393   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18394     {
18395       rs = neon_select_shape (NS_QQI, NS_NULL);
18396       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_KEY);
18397     }
18398   else
18399     {
18400       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18401       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18402     }
18403
18404   int imm = inst.operands[2].imm;
18405   constraint (imm < 1 || (unsigned)imm > et.size,
18406               _("immediate out of range for insert"));
18407   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
18408 }
18409
18410 static void
18411 do_neon_qshlu_imm (void)
18412 {
18413   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18414     return;
18415
18416   enum neon_shape rs;
18417   struct neon_type_el et;
18418   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18419     {
18420       rs = neon_select_shape (NS_QQI, NS_NULL);
18421       et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18422     }
18423   else
18424     {
18425       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18426       et = neon_check_type (2, rs, N_EQK | N_UNS,
18427                             N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
18428     }
18429
18430   int imm = inst.operands[2].imm;
18431   constraint (imm < 0 || (unsigned)imm >= et.size,
18432               _("immediate out of range for shift"));
18433   /* Only encodes the 'U present' variant of the instruction.
18434      In this case, signed types have OP (bit 8) set to 0.
18435      Unsigned types have OP set to 1.  */
18436   inst.instruction |= (et.type == NT_unsigned) << 8;
18437   /* The rest of the bits are the same as other immediate shifts.  */
18438   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
18439 }
18440
18441 static void
18442 do_neon_qmovn (void)
18443 {
18444   struct neon_type_el et = neon_check_type (2, NS_DQ,
18445     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
18446   /* Saturating move where operands can be signed or unsigned, and the
18447      destination has the same signedness.  */
18448   NEON_ENCODE (INTEGER, inst);
18449   if (et.type == NT_unsigned)
18450     inst.instruction |= 0xc0;
18451   else
18452     inst.instruction |= 0x80;
18453   neon_two_same (0, 1, et.size / 2);
18454 }
18455
18456 static void
18457 do_neon_qmovun (void)
18458 {
18459   struct neon_type_el et = neon_check_type (2, NS_DQ,
18460     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
18461   /* Saturating move with unsigned results. Operands must be signed.  */
18462   NEON_ENCODE (INTEGER, inst);
18463   neon_two_same (0, 1, et.size / 2);
18464 }
18465
18466 static void
18467 do_neon_rshift_sat_narrow (void)
18468 {
18469   /* FIXME: Types for narrowing. If operands are signed, results can be signed
18470      or unsigned. If operands are unsigned, results must also be unsigned.  */
18471   struct neon_type_el et = neon_check_type (2, NS_DQI,
18472     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
18473   int imm = inst.operands[2].imm;
18474   /* This gets the bounds check, size encoding and immediate bits calculation
18475      right.  */
18476   et.size /= 2;
18477
18478   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
18479      VQMOVN.I<size> <Dd>, <Qm>.  */
18480   if (imm == 0)
18481     {
18482       inst.operands[2].present = 0;
18483       inst.instruction = N_MNEM_vqmovn;
18484       do_neon_qmovn ();
18485       return;
18486     }
18487
18488   constraint (imm < 1 || (unsigned)imm > et.size,
18489               _("immediate out of range"));
18490   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
18491 }
18492
18493 static void
18494 do_neon_rshift_sat_narrow_u (void)
18495 {
18496   /* FIXME: Types for narrowing. If operands are signed, results can be signed
18497      or unsigned. If operands are unsigned, results must also be unsigned.  */
18498   struct neon_type_el et = neon_check_type (2, NS_DQI,
18499     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
18500   int imm = inst.operands[2].imm;
18501   /* This gets the bounds check, size encoding and immediate bits calculation
18502      right.  */
18503   et.size /= 2;
18504
18505   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
18506      VQMOVUN.I<size> <Dd>, <Qm>.  */
18507   if (imm == 0)
18508     {
18509       inst.operands[2].present = 0;
18510       inst.instruction = N_MNEM_vqmovun;
18511       do_neon_qmovun ();
18512       return;
18513     }
18514
18515   constraint (imm < 1 || (unsigned)imm > et.size,
18516               _("immediate out of range"));
18517   /* FIXME: The manual is kind of unclear about what value U should have in
18518      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
18519      must be 1.  */
18520   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
18521 }
18522
18523 static void
18524 do_neon_movn (void)
18525 {
18526   struct neon_type_el et = neon_check_type (2, NS_DQ,
18527     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
18528   NEON_ENCODE (INTEGER, inst);
18529   neon_two_same (0, 1, et.size / 2);
18530 }
18531
18532 static void
18533 do_neon_rshift_narrow (void)
18534 {
18535   struct neon_type_el et = neon_check_type (2, NS_DQI,
18536     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
18537   int imm = inst.operands[2].imm;
18538   /* This gets the bounds check, size encoding and immediate bits calculation
18539      right.  */
18540   et.size /= 2;
18541
18542   /* If immediate is zero then we are a pseudo-instruction for
18543      VMOVN.I<size> <Dd>, <Qm>  */
18544   if (imm == 0)
18545     {
18546       inst.operands[2].present = 0;
18547       inst.instruction = N_MNEM_vmovn;
18548       do_neon_movn ();
18549       return;
18550     }
18551
18552   constraint (imm < 1 || (unsigned)imm > et.size,
18553               _("immediate out of range for narrowing operation"));
18554   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
18555 }
18556
18557 static void
18558 do_neon_shll (void)
18559 {
18560   /* FIXME: Type checking when lengthening.  */
18561   struct neon_type_el et = neon_check_type (2, NS_QDI,
18562     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
18563   unsigned imm = inst.operands[2].imm;
18564
18565   if (imm == et.size)
18566     {
18567       /* Maximum shift variant.  */
18568       NEON_ENCODE (INTEGER, inst);
18569       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18570       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18571       inst.instruction |= LOW4 (inst.operands[1].reg);
18572       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18573       inst.instruction |= neon_logbits (et.size) << 18;
18574
18575       neon_dp_fixup (&inst);
18576     }
18577   else
18578     {
18579       /* A more-specific type check for non-max versions.  */
18580       et = neon_check_type (2, NS_QDI,
18581         N_EQK | N_DBL, N_SU_32 | N_KEY);
18582       NEON_ENCODE (IMMED, inst);
18583       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
18584     }
18585 }
18586
18587 /* Check the various types for the VCVT instruction, and return which version
18588    the current instruction is.  */
18589
18590 #define CVT_FLAVOUR_VAR                                                       \
18591   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
18592   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
18593   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
18594   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
18595   /* Half-precision conversions.  */                                          \
18596   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
18597   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
18598   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
18599   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
18600   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
18601   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
18602   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
18603      Compared with single/double precision variants, only the co-processor    \
18604      field is different, so the encoding flow is reused here.  */             \
18605   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
18606   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
18607   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
18608   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
18609   /* VFP instructions.  */                                                    \
18610   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
18611   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
18612   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
18613   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
18614   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
18615   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
18616   /* VFP instructions with bitshift.  */                                      \
18617   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
18618   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
18619   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
18620   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
18621   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
18622   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
18623   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
18624   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
18625
18626 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
18627   neon_cvt_flavour_##C,
18628
18629 /* The different types of conversions we can do.  */
18630 enum neon_cvt_flavour
18631 {
18632   CVT_FLAVOUR_VAR
18633   neon_cvt_flavour_invalid,
18634   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
18635 };
18636
18637 #undef CVT_VAR
18638
18639 static enum neon_cvt_flavour
18640 get_neon_cvt_flavour (enum neon_shape rs)
18641 {
18642 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
18643   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
18644   if (et.type != NT_invtype)                            \
18645     {                                                   \
18646       inst.error = NULL;                                \
18647       return (neon_cvt_flavour_##C);                    \
18648     }
18649
18650   struct neon_type_el et;
18651   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
18652                         || rs == NS_FF) ? N_VFP : 0;
18653   /* The instruction versions which take an immediate take one register
18654      argument, which is extended to the width of the full register. Thus the
18655      "source" and "destination" registers must have the same width.  Hack that
18656      here by making the size equal to the key (wider, in this case) operand.  */
18657   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
18658
18659   CVT_FLAVOUR_VAR;
18660
18661   return neon_cvt_flavour_invalid;
18662 #undef CVT_VAR
18663 }
18664
18665 enum neon_cvt_mode
18666 {
18667   neon_cvt_mode_a,
18668   neon_cvt_mode_n,
18669   neon_cvt_mode_p,
18670   neon_cvt_mode_m,
18671   neon_cvt_mode_z,
18672   neon_cvt_mode_x,
18673   neon_cvt_mode_r
18674 };
18675
18676 /* Neon-syntax VFP conversions.  */
18677
18678 static void
18679 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
18680 {
18681   const char *opname = 0;
18682
18683   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
18684       || rs == NS_FHI || rs == NS_HFI)
18685     {
18686       /* Conversions with immediate bitshift.  */
18687       const char *enc[] =
18688         {
18689 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
18690           CVT_FLAVOUR_VAR
18691           NULL
18692 #undef CVT_VAR
18693         };
18694
18695       if (flavour < (int) ARRAY_SIZE (enc))
18696         {
18697           opname = enc[flavour];
18698           constraint (inst.operands[0].reg != inst.operands[1].reg,
18699                       _("operands 0 and 1 must be the same register"));
18700           inst.operands[1] = inst.operands[2];
18701           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
18702         }
18703     }
18704   else
18705     {
18706       /* Conversions without bitshift.  */
18707       const char *enc[] =
18708         {
18709 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
18710           CVT_FLAVOUR_VAR
18711           NULL
18712 #undef CVT_VAR
18713         };
18714
18715       if (flavour < (int) ARRAY_SIZE (enc))
18716         opname = enc[flavour];
18717     }
18718
18719   if (opname)
18720     do_vfp_nsyn_opcode (opname);
18721
18722   /* ARMv8.2 fp16 VCVT instruction.  */
18723   if (flavour == neon_cvt_flavour_s32_f16
18724       || flavour == neon_cvt_flavour_u32_f16
18725       || flavour == neon_cvt_flavour_f16_u32
18726       || flavour == neon_cvt_flavour_f16_s32)
18727     do_scalar_fp16_v82_encode ();
18728 }
18729
18730 static void
18731 do_vfp_nsyn_cvtz (void)
18732 {
18733   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
18734   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
18735   const char *enc[] =
18736     {
18737 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
18738       CVT_FLAVOUR_VAR
18739       NULL
18740 #undef CVT_VAR
18741     };
18742
18743   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
18744     do_vfp_nsyn_opcode (enc[flavour]);
18745 }
18746
18747 static void
18748 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
18749                       enum neon_cvt_mode mode)
18750 {
18751   int sz, op;
18752   int rm;
18753
18754   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
18755      D register operands.  */
18756   if (flavour == neon_cvt_flavour_s32_f64
18757       || flavour == neon_cvt_flavour_u32_f64)
18758     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
18759                 _(BAD_FPU));
18760
18761   if (flavour == neon_cvt_flavour_s32_f16
18762       || flavour == neon_cvt_flavour_u32_f16)
18763     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
18764                 _(BAD_FP16));
18765
18766   set_pred_insn_type (OUTSIDE_PRED_INSN);
18767
18768   switch (flavour)
18769     {
18770     case neon_cvt_flavour_s32_f64:
18771       sz = 1;
18772       op = 1;
18773       break;
18774     case neon_cvt_flavour_s32_f32:
18775       sz = 0;
18776       op = 1;
18777       break;
18778     case neon_cvt_flavour_s32_f16:
18779       sz = 0;
18780       op = 1;
18781       break;
18782     case neon_cvt_flavour_u32_f64:
18783       sz = 1;
18784       op = 0;
18785       break;
18786     case neon_cvt_flavour_u32_f32:
18787       sz = 0;
18788       op = 0;
18789       break;
18790     case neon_cvt_flavour_u32_f16:
18791       sz = 0;
18792       op = 0;
18793       break;
18794     default:
18795       first_error (_("invalid instruction shape"));
18796       return;
18797     }
18798
18799   switch (mode)
18800     {
18801     case neon_cvt_mode_a: rm = 0; break;
18802     case neon_cvt_mode_n: rm = 1; break;
18803     case neon_cvt_mode_p: rm = 2; break;
18804     case neon_cvt_mode_m: rm = 3; break;
18805     default: first_error (_("invalid rounding mode")); return;
18806     }
18807
18808   NEON_ENCODE (FPV8, inst);
18809   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
18810   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
18811   inst.instruction |= sz << 8;
18812
18813   /* ARMv8.2 fp16 VCVT instruction.  */
18814   if (flavour == neon_cvt_flavour_s32_f16
18815       ||flavour == neon_cvt_flavour_u32_f16)
18816     do_scalar_fp16_v82_encode ();
18817   inst.instruction |= op << 7;
18818   inst.instruction |= rm << 16;
18819   inst.instruction |= 0xf0000000;
18820   inst.is_neon = TRUE;
18821 }
18822
18823 static void
18824 do_neon_cvt_1 (enum neon_cvt_mode mode)
18825 {
18826   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
18827                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
18828                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
18829                                           NS_NULL);
18830   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
18831
18832   if (flavour == neon_cvt_flavour_invalid)
18833     return;
18834
18835   /* PR11109: Handle round-to-zero for VCVT conversions.  */
18836   if (mode == neon_cvt_mode_z
18837       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
18838       && (flavour == neon_cvt_flavour_s16_f16
18839           || flavour == neon_cvt_flavour_u16_f16
18840           || flavour == neon_cvt_flavour_s32_f32
18841           || flavour == neon_cvt_flavour_u32_f32
18842           || flavour == neon_cvt_flavour_s32_f64
18843           || flavour == neon_cvt_flavour_u32_f64)
18844       && (rs == NS_FD || rs == NS_FF))
18845     {
18846       do_vfp_nsyn_cvtz ();
18847       return;
18848     }
18849
18850   /* ARMv8.2 fp16 VCVT conversions.  */
18851   if (mode == neon_cvt_mode_z
18852       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
18853       && (flavour == neon_cvt_flavour_s32_f16
18854           || flavour == neon_cvt_flavour_u32_f16)
18855       && (rs == NS_FH))
18856     {
18857       do_vfp_nsyn_cvtz ();
18858       do_scalar_fp16_v82_encode ();
18859       return;
18860     }
18861
18862   /* VFP rather than Neon conversions.  */
18863   if (flavour >= neon_cvt_flavour_first_fp)
18864     {
18865       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
18866         do_vfp_nsyn_cvt (rs, flavour);
18867       else
18868         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
18869
18870       return;
18871     }
18872
18873   switch (rs)
18874     {
18875     case NS_QQI:
18876       if (mode == neon_cvt_mode_z
18877           && (flavour == neon_cvt_flavour_f16_s16
18878               || flavour == neon_cvt_flavour_f16_u16
18879               || flavour == neon_cvt_flavour_s16_f16
18880               || flavour == neon_cvt_flavour_u16_f16
18881               || flavour == neon_cvt_flavour_f32_u32
18882               || flavour == neon_cvt_flavour_f32_s32
18883               || flavour == neon_cvt_flavour_s32_f32
18884               || flavour == neon_cvt_flavour_u32_f32))
18885         {
18886           if (!check_simd_pred_availability (TRUE,
18887                                              NEON_CHECK_CC | NEON_CHECK_ARCH))
18888             return;
18889         }
18890       else if (mode == neon_cvt_mode_n)
18891         {
18892           /* We are dealing with vcvt with the 'ne' condition.  */
18893           inst.cond = 0x1;
18894           inst.instruction = N_MNEM_vcvt;
18895           do_neon_cvt_1 (neon_cvt_mode_z);
18896           return;
18897         }
18898       /* fall through.  */
18899     case NS_DDI:
18900       {
18901         unsigned immbits;
18902         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
18903                              0x0000100, 0x1000100, 0x0, 0x1000000};
18904
18905         if ((rs != NS_QQI || !ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18906             && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
18907             return;
18908
18909         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18910           {
18911             constraint (inst.operands[2].present && inst.operands[2].imm == 0,
18912                         _("immediate value out of range"));
18913             switch (flavour)
18914               {
18915                 case neon_cvt_flavour_f16_s16:
18916                 case neon_cvt_flavour_f16_u16:
18917                 case neon_cvt_flavour_s16_f16:
18918                 case neon_cvt_flavour_u16_f16:
18919                   constraint (inst.operands[2].imm > 16,
18920                               _("immediate value out of range"));
18921                   break;
18922                 case neon_cvt_flavour_f32_u32:
18923                 case neon_cvt_flavour_f32_s32:
18924                 case neon_cvt_flavour_s32_f32:
18925                 case neon_cvt_flavour_u32_f32:
18926                   constraint (inst.operands[2].imm > 32,
18927                               _("immediate value out of range"));
18928                   break;
18929                 default:
18930                   inst.error = BAD_FPU;
18931                   return;
18932               }
18933           }
18934
18935         /* Fixed-point conversion with #0 immediate is encoded as an
18936            integer conversion.  */
18937         if (inst.operands[2].present && inst.operands[2].imm == 0)
18938           goto int_encode;
18939         NEON_ENCODE (IMMED, inst);
18940         if (flavour != neon_cvt_flavour_invalid)
18941           inst.instruction |= enctab[flavour];
18942         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18943         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18944         inst.instruction |= LOW4 (inst.operands[1].reg);
18945         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18946         inst.instruction |= neon_quad (rs) << 6;
18947         inst.instruction |= 1 << 21;
18948         if (flavour < neon_cvt_flavour_s16_f16)
18949           {
18950             inst.instruction |= 1 << 21;
18951             immbits = 32 - inst.operands[2].imm;
18952             inst.instruction |= immbits << 16;
18953           }
18954         else
18955           {
18956             inst.instruction |= 3 << 20;
18957             immbits = 16 - inst.operands[2].imm;
18958             inst.instruction |= immbits << 16;
18959             inst.instruction &= ~(1 << 9);
18960           }
18961
18962         neon_dp_fixup (&inst);
18963       }
18964       break;
18965
18966     case NS_QQ:
18967       if ((mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
18968            || mode == neon_cvt_mode_m || mode == neon_cvt_mode_p)
18969           && (flavour == neon_cvt_flavour_s16_f16
18970               || flavour == neon_cvt_flavour_u16_f16
18971               || flavour == neon_cvt_flavour_s32_f32
18972               || flavour == neon_cvt_flavour_u32_f32))
18973         {
18974           if (!check_simd_pred_availability (TRUE,
18975                                              NEON_CHECK_CC | NEON_CHECK_ARCH8))
18976             return;
18977         }
18978       else if (mode == neon_cvt_mode_z
18979                && (flavour == neon_cvt_flavour_f16_s16
18980                    || flavour == neon_cvt_flavour_f16_u16
18981                    || flavour == neon_cvt_flavour_s16_f16
18982                    || flavour == neon_cvt_flavour_u16_f16
18983                    || flavour == neon_cvt_flavour_f32_u32
18984                    || flavour == neon_cvt_flavour_f32_s32
18985                    || flavour == neon_cvt_flavour_s32_f32
18986                    || flavour == neon_cvt_flavour_u32_f32))
18987         {
18988           if (!check_simd_pred_availability (TRUE,
18989                                              NEON_CHECK_CC | NEON_CHECK_ARCH))
18990             return;
18991         }
18992       /* fall through.  */
18993     case NS_DD:
18994       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
18995         {
18996
18997           NEON_ENCODE (FLOAT, inst);
18998           if (!check_simd_pred_availability (TRUE,
18999                                              NEON_CHECK_CC | NEON_CHECK_ARCH8))
19000             return;
19001
19002           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19003           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19004           inst.instruction |= LOW4 (inst.operands[1].reg);
19005           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19006           inst.instruction |= neon_quad (rs) << 6;
19007           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
19008                                || flavour == neon_cvt_flavour_u32_f32) << 7;
19009           inst.instruction |= mode << 8;
19010           if (flavour == neon_cvt_flavour_u16_f16
19011               || flavour == neon_cvt_flavour_s16_f16)
19012             /* Mask off the original size bits and reencode them.  */
19013             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
19014
19015           if (thumb_mode)
19016             inst.instruction |= 0xfc000000;
19017           else
19018             inst.instruction |= 0xf0000000;
19019         }
19020       else
19021         {
19022     int_encode:
19023           {
19024             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
19025                                   0x100, 0x180, 0x0, 0x080};
19026
19027             NEON_ENCODE (INTEGER, inst);
19028
19029           if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
19030             {
19031               if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
19032                 return;
19033             }
19034
19035             if (flavour != neon_cvt_flavour_invalid)
19036               inst.instruction |= enctab[flavour];
19037
19038             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19039             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19040             inst.instruction |= LOW4 (inst.operands[1].reg);
19041             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19042             inst.instruction |= neon_quad (rs) << 6;
19043             if (flavour >= neon_cvt_flavour_s16_f16
19044                 && flavour <= neon_cvt_flavour_f16_u16)
19045               /* Half precision.  */
19046               inst.instruction |= 1 << 18;
19047             else
19048               inst.instruction |= 2 << 18;
19049
19050             neon_dp_fixup (&inst);
19051           }
19052         }
19053       break;
19054
19055     /* Half-precision conversions for Advanced SIMD -- neon.  */
19056     case NS_QD:
19057     case NS_DQ:
19058       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
19059         return;
19060
19061       if ((rs == NS_DQ)
19062           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
19063           {
19064             as_bad (_("operand size must match register width"));
19065             break;
19066           }
19067
19068       if ((rs == NS_QD)
19069           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
19070           {
19071             as_bad (_("operand size must match register width"));
19072             break;
19073           }
19074
19075       if (rs == NS_DQ)
19076         inst.instruction = 0x3b60600;
19077       else
19078         inst.instruction = 0x3b60700;
19079
19080       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19081       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19082       inst.instruction |= LOW4 (inst.operands[1].reg);
19083       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19084       neon_dp_fixup (&inst);
19085       break;
19086
19087     default:
19088       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
19089       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
19090         do_vfp_nsyn_cvt (rs, flavour);
19091       else
19092         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
19093     }
19094 }
19095
19096 static void
19097 do_neon_cvtr (void)
19098 {
19099   do_neon_cvt_1 (neon_cvt_mode_x);
19100 }
19101
19102 static void
19103 do_neon_cvt (void)
19104 {
19105   do_neon_cvt_1 (neon_cvt_mode_z);
19106 }
19107
19108 static void
19109 do_neon_cvta (void)
19110 {
19111   do_neon_cvt_1 (neon_cvt_mode_a);
19112 }
19113
19114 static void
19115 do_neon_cvtn (void)
19116 {
19117   do_neon_cvt_1 (neon_cvt_mode_n);
19118 }
19119
19120 static void
19121 do_neon_cvtp (void)
19122 {
19123   do_neon_cvt_1 (neon_cvt_mode_p);
19124 }
19125
19126 static void
19127 do_neon_cvtm (void)
19128 {
19129   do_neon_cvt_1 (neon_cvt_mode_m);
19130 }
19131
19132 static void
19133 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
19134 {
19135   if (is_double)
19136     mark_feature_used (&fpu_vfp_ext_armv8);
19137
19138   encode_arm_vfp_reg (inst.operands[0].reg,
19139                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
19140   encode_arm_vfp_reg (inst.operands[1].reg,
19141                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
19142   inst.instruction |= to ? 0x10000 : 0;
19143   inst.instruction |= t ? 0x80 : 0;
19144   inst.instruction |= is_double ? 0x100 : 0;
19145   do_vfp_cond_or_thumb ();
19146 }
19147
19148 static void
19149 do_neon_cvttb_1 (bfd_boolean t)
19150 {
19151   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
19152                                           NS_DF, NS_DH, NS_QQ, NS_QQI, NS_NULL);
19153
19154   if (rs == NS_NULL)
19155     return;
19156   else if (rs == NS_QQ || rs == NS_QQI)
19157     {
19158       int single_to_half = 0;
19159       if (!check_simd_pred_availability (TRUE, NEON_CHECK_ARCH))
19160         return;
19161
19162       enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
19163
19164       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19165           && (flavour ==  neon_cvt_flavour_u16_f16
19166               || flavour ==  neon_cvt_flavour_s16_f16
19167               || flavour ==  neon_cvt_flavour_f16_s16
19168               || flavour ==  neon_cvt_flavour_f16_u16
19169               || flavour ==  neon_cvt_flavour_u32_f32
19170               || flavour ==  neon_cvt_flavour_s32_f32
19171               || flavour ==  neon_cvt_flavour_f32_s32
19172               || flavour ==  neon_cvt_flavour_f32_u32))
19173         {
19174           inst.cond = 0xf;
19175           inst.instruction = N_MNEM_vcvt;
19176           set_pred_insn_type (INSIDE_VPT_INSN);
19177           do_neon_cvt_1 (neon_cvt_mode_z);
19178           return;
19179         }
19180       else if (rs == NS_QQ && flavour == neon_cvt_flavour_f32_f16)
19181         single_to_half = 1;
19182       else if (rs == NS_QQ && flavour != neon_cvt_flavour_f16_f32)
19183         {
19184           first_error (BAD_FPU);
19185           return;
19186         }
19187
19188       inst.instruction = 0xee3f0e01;
19189       inst.instruction |= single_to_half << 28;
19190       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19191       inst.instruction |= LOW4 (inst.operands[0].reg) << 13;
19192       inst.instruction |= t << 12;
19193       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19194       inst.instruction |= LOW4 (inst.operands[1].reg) << 1;
19195       inst.is_neon = 1;
19196     }
19197   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
19198     {
19199       inst.error = NULL;
19200       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
19201     }
19202   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
19203     {
19204       inst.error = NULL;
19205       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
19206     }
19207   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
19208     {
19209       /* The VCVTB and VCVTT instructions with D-register operands
19210          don't work for SP only targets.  */
19211       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19212                   _(BAD_FPU));
19213
19214       inst.error = NULL;
19215       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
19216     }
19217   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
19218     {
19219       /* The VCVTB and VCVTT instructions with D-register operands
19220          don't work for SP only targets.  */
19221       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19222                   _(BAD_FPU));
19223
19224       inst.error = NULL;
19225       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
19226     }
19227   else
19228     return;
19229 }
19230
19231 static void
19232 do_neon_cvtb (void)
19233 {
19234   do_neon_cvttb_1 (FALSE);
19235 }
19236
19237
19238 static void
19239 do_neon_cvtt (void)
19240 {
19241   do_neon_cvttb_1 (TRUE);
19242 }
19243
19244 static void
19245 neon_move_immediate (void)
19246 {
19247   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
19248   struct neon_type_el et = neon_check_type (2, rs,
19249     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
19250   unsigned immlo, immhi = 0, immbits;
19251   int op, cmode, float_p;
19252
19253   constraint (et.type == NT_invtype,
19254               _("operand size must be specified for immediate VMOV"));
19255
19256   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
19257   op = (inst.instruction & (1 << 5)) != 0;
19258
19259   immlo = inst.operands[1].imm;
19260   if (inst.operands[1].regisimm)
19261     immhi = inst.operands[1].reg;
19262
19263   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
19264               _("immediate has bits set outside the operand size"));
19265
19266   float_p = inst.operands[1].immisfloat;
19267
19268   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
19269                                         et.size, et.type)) == FAIL)
19270     {
19271       /* Invert relevant bits only.  */
19272       neon_invert_size (&immlo, &immhi, et.size);
19273       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
19274          with one or the other; those cases are caught by
19275          neon_cmode_for_move_imm.  */
19276       op = !op;
19277       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
19278                                             &op, et.size, et.type)) == FAIL)
19279         {
19280           first_error (_("immediate out of range"));
19281           return;
19282         }
19283     }
19284
19285   inst.instruction &= ~(1 << 5);
19286   inst.instruction |= op << 5;
19287
19288   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19289   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19290   inst.instruction |= neon_quad (rs) << 6;
19291   inst.instruction |= cmode << 8;
19292
19293   neon_write_immbits (immbits);
19294 }
19295
19296 static void
19297 do_neon_mvn (void)
19298 {
19299   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
19300     return;
19301
19302   if (inst.operands[1].isreg)
19303     {
19304       enum neon_shape rs;
19305       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19306         rs = neon_select_shape (NS_QQ, NS_NULL);
19307       else
19308         rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19309
19310       NEON_ENCODE (INTEGER, inst);
19311       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19312       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19313       inst.instruction |= LOW4 (inst.operands[1].reg);
19314       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19315       inst.instruction |= neon_quad (rs) << 6;
19316     }
19317   else
19318     {
19319       NEON_ENCODE (IMMED, inst);
19320       neon_move_immediate ();
19321     }
19322
19323   neon_dp_fixup (&inst);
19324
19325   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19326     {
19327       constraint (!inst.operands[1].isreg && !inst.operands[0].isquad, BAD_FPU);
19328       constraint ((inst.instruction & 0xd00) == 0xd00,
19329                   _("immediate value out of range"));
19330     }
19331 }
19332
19333 /* Encode instructions of form:
19334
19335   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
19336   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
19337
19338 static void
19339 neon_mixed_length (struct neon_type_el et, unsigned size)
19340 {
19341   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19342   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19343   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19344   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19345   inst.instruction |= LOW4 (inst.operands[2].reg);
19346   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19347   inst.instruction |= (et.type == NT_unsigned) << 24;
19348   inst.instruction |= neon_logbits (size) << 20;
19349
19350   neon_dp_fixup (&inst);
19351 }
19352
19353 static void
19354 do_neon_dyadic_long (void)
19355 {
19356   enum neon_shape rs = neon_select_shape (NS_QDD, NS_QQQ, NS_QQR, NS_NULL);
19357   if (rs == NS_QDD)
19358     {
19359       if (vfp_or_neon_is_neon (NEON_CHECK_ARCH | NEON_CHECK_CC) == FAIL)
19360         return;
19361
19362       NEON_ENCODE (INTEGER, inst);
19363       /* FIXME: Type checking for lengthening op.  */
19364       struct neon_type_el et = neon_check_type (3, NS_QDD,
19365         N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
19366       neon_mixed_length (et, et.size);
19367     }
19368   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19369            && (inst.cond == 0xf || inst.cond == 0x10))
19370     {
19371       /* If parsing for MVE, vaddl/vsubl/vabdl{e,t} can only be vadd/vsub/vabd
19372          in an IT block with le/lt conditions.  */
19373
19374       if (inst.cond == 0xf)
19375         inst.cond = 0xb;
19376       else if (inst.cond == 0x10)
19377         inst.cond = 0xd;
19378
19379       inst.pred_insn_type = INSIDE_IT_INSN;
19380
19381       if (inst.instruction == N_MNEM_vaddl)
19382         {
19383           inst.instruction = N_MNEM_vadd;
19384           do_neon_addsub_if_i ();
19385         }
19386       else if (inst.instruction == N_MNEM_vsubl)
19387         {
19388           inst.instruction = N_MNEM_vsub;
19389           do_neon_addsub_if_i ();
19390         }
19391       else if (inst.instruction == N_MNEM_vabdl)
19392         {
19393           inst.instruction = N_MNEM_vabd;
19394           do_neon_dyadic_if_su ();
19395         }
19396     }
19397   else
19398     first_error (BAD_FPU);
19399 }
19400
19401 static void
19402 do_neon_abal (void)
19403 {
19404   struct neon_type_el et = neon_check_type (3, NS_QDD,
19405     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
19406   neon_mixed_length (et, et.size);
19407 }
19408
19409 static void
19410 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
19411 {
19412   if (inst.operands[2].isscalar)
19413     {
19414       struct neon_type_el et = neon_check_type (3, NS_QDS,
19415         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
19416       NEON_ENCODE (SCALAR, inst);
19417       neon_mul_mac (et, et.type == NT_unsigned);
19418     }
19419   else
19420     {
19421       struct neon_type_el et = neon_check_type (3, NS_QDD,
19422         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
19423       NEON_ENCODE (INTEGER, inst);
19424       neon_mixed_length (et, et.size);
19425     }
19426 }
19427
19428 static void
19429 do_neon_mac_maybe_scalar_long (void)
19430 {
19431   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
19432 }
19433
19434 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
19435    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
19436
19437 static unsigned
19438 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
19439 {
19440   unsigned regno = NEON_SCALAR_REG (scalar);
19441   unsigned elno = NEON_SCALAR_INDEX (scalar);
19442
19443   if (quad_p)
19444     {
19445       if (regno > 7 || elno > 3)
19446         goto bad_scalar;
19447
19448       return ((regno & 0x7)
19449               | ((elno & 0x1) << 3)
19450               | (((elno >> 1) & 0x1) << 5));
19451     }
19452   else
19453     {
19454       if (regno > 15 || elno > 1)
19455         goto bad_scalar;
19456
19457       return (((regno & 0x1) << 5)
19458               | ((regno >> 1) & 0x7)
19459               | ((elno & 0x1) << 3));
19460     }
19461
19462 bad_scalar:
19463   first_error (_("scalar out of range for multiply instruction"));
19464   return 0;
19465 }
19466
19467 static void
19468 do_neon_fmac_maybe_scalar_long (int subtype)
19469 {
19470   enum neon_shape rs;
19471   int high8;
19472   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
19473      field (bits[21:20]) has different meaning.  For scalar index variant, it's
19474      used to differentiate add and subtract, otherwise it's with fixed value
19475      0x2.  */
19476   int size = -1;
19477
19478   if (inst.cond != COND_ALWAYS)
19479     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
19480                "behaviour is UNPREDICTABLE"));
19481
19482   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
19483               _(BAD_FP16));
19484
19485   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
19486               _(BAD_FPU));
19487
19488   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
19489      be a scalar index register.  */
19490   if (inst.operands[2].isscalar)
19491     {
19492       high8 = 0xfe000000;
19493       if (subtype)
19494         size = 16;
19495       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
19496     }
19497   else
19498     {
19499       high8 = 0xfc000000;
19500       size = 32;
19501       if (subtype)
19502         inst.instruction |= (0x1 << 23);
19503       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
19504     }
19505
19506   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
19507
19508   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
19509      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
19510      so we simply pass -1 as size.  */
19511   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
19512   neon_three_same (quad_p, 0, size);
19513
19514   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
19515   inst.instruction &= 0x00ffffff;
19516   inst.instruction |= high8;
19517
19518 #define LOW1(R) ((R) & 0x1)
19519 #define HI4(R) (((R) >> 1) & 0xf)
19520   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
19521      whether the instruction is in Q form and whether Vm is a scalar indexed
19522      operand.  */
19523   if (inst.operands[2].isscalar)
19524     {
19525       unsigned rm
19526         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
19527       inst.instruction &= 0xffffffd0;
19528       inst.instruction |= rm;
19529
19530       if (!quad_p)
19531         {
19532           /* Redo Rn as well.  */
19533           inst.instruction &= 0xfff0ff7f;
19534           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
19535           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
19536         }
19537     }
19538   else if (!quad_p)
19539     {
19540       /* Redo Rn and Rm.  */
19541       inst.instruction &= 0xfff0ff50;
19542       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
19543       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
19544       inst.instruction |= HI4 (inst.operands[2].reg);
19545       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
19546     }
19547 }
19548
19549 static void
19550 do_neon_vfmal (void)
19551 {
19552   return do_neon_fmac_maybe_scalar_long (0);
19553 }
19554
19555 static void
19556 do_neon_vfmsl (void)
19557 {
19558   return do_neon_fmac_maybe_scalar_long (1);
19559 }
19560
19561 static void
19562 do_neon_dyadic_wide (void)
19563 {
19564   struct neon_type_el et = neon_check_type (3, NS_QQD,
19565     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
19566   neon_mixed_length (et, et.size);
19567 }
19568
19569 static void
19570 do_neon_dyadic_narrow (void)
19571 {
19572   struct neon_type_el et = neon_check_type (3, NS_QDD,
19573     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
19574   /* Operand sign is unimportant, and the U bit is part of the opcode,
19575      so force the operand type to integer.  */
19576   et.type = NT_integer;
19577   neon_mixed_length (et, et.size / 2);
19578 }
19579
19580 static void
19581 do_neon_mul_sat_scalar_long (void)
19582 {
19583   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
19584 }
19585
19586 static void
19587 do_neon_vmull (void)
19588 {
19589   if (inst.operands[2].isscalar)
19590     do_neon_mac_maybe_scalar_long ();
19591   else
19592     {
19593       struct neon_type_el et = neon_check_type (3, NS_QDD,
19594         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
19595
19596       if (et.type == NT_poly)
19597         NEON_ENCODE (POLY, inst);
19598       else
19599         NEON_ENCODE (INTEGER, inst);
19600
19601       /* For polynomial encoding the U bit must be zero, and the size must
19602          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
19603          obviously, as 0b10).  */
19604       if (et.size == 64)
19605         {
19606           /* Check we're on the correct architecture.  */
19607           if (!mark_feature_used (&fpu_crypto_ext_armv8))
19608             inst.error =
19609               _("Instruction form not available on this architecture.");
19610
19611           et.size = 32;
19612         }
19613
19614       neon_mixed_length (et, et.size);
19615     }
19616 }
19617
19618 static void
19619 do_neon_ext (void)
19620 {
19621   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
19622   struct neon_type_el et = neon_check_type (3, rs,
19623     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
19624   unsigned imm = (inst.operands[3].imm * et.size) / 8;
19625
19626   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
19627               _("shift out of range"));
19628   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19629   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19630   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19631   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19632   inst.instruction |= LOW4 (inst.operands[2].reg);
19633   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19634   inst.instruction |= neon_quad (rs) << 6;
19635   inst.instruction |= imm << 8;
19636
19637   neon_dp_fixup (&inst);
19638 }
19639
19640 static void
19641 do_neon_rev (void)
19642 {
19643   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
19644    return;
19645
19646   enum neon_shape rs;
19647   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19648     rs = neon_select_shape (NS_QQ, NS_NULL);
19649   else
19650     rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19651
19652   struct neon_type_el et = neon_check_type (2, rs,
19653     N_EQK, N_8 | N_16 | N_32 | N_KEY);
19654
19655   unsigned op = (inst.instruction >> 7) & 3;
19656   /* N (width of reversed regions) is encoded as part of the bitmask. We
19657      extract it here to check the elements to be reversed are smaller.
19658      Otherwise we'd get a reserved instruction.  */
19659   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
19660
19661   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext) && elsize == 64
19662       && inst.operands[0].reg == inst.operands[1].reg)
19663     as_tsktsk (_("Warning: 64-bit element size and same destination and source"
19664                  " operands makes instruction UNPREDICTABLE"));
19665
19666   gas_assert (elsize != 0);
19667   constraint (et.size >= elsize,
19668               _("elements must be smaller than reversal region"));
19669   neon_two_same (neon_quad (rs), 1, et.size);
19670 }
19671
19672 static void
19673 do_neon_dup (void)
19674 {
19675   if (inst.operands[1].isscalar)
19676     {
19677       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1),
19678                   BAD_FPU);
19679       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
19680       struct neon_type_el et = neon_check_type (2, rs,
19681         N_EQK, N_8 | N_16 | N_32 | N_KEY);
19682       unsigned sizebits = et.size >> 3;
19683       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
19684       int logsize = neon_logbits (et.size);
19685       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
19686
19687       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
19688         return;
19689
19690       NEON_ENCODE (SCALAR, inst);
19691       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19692       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19693       inst.instruction |= LOW4 (dm);
19694       inst.instruction |= HI1 (dm) << 5;
19695       inst.instruction |= neon_quad (rs) << 6;
19696       inst.instruction |= x << 17;
19697       inst.instruction |= sizebits << 16;
19698
19699       neon_dp_fixup (&inst);
19700     }
19701   else
19702     {
19703       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
19704       struct neon_type_el et = neon_check_type (2, rs,
19705         N_8 | N_16 | N_32 | N_KEY, N_EQK);
19706       if (rs == NS_QR)
19707         {
19708           if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH))
19709             return;
19710         }
19711       else
19712         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1),
19713                     BAD_FPU);
19714
19715       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19716         {
19717           if (inst.operands[1].reg == REG_SP)
19718             as_tsktsk (MVE_BAD_SP);
19719           else if (inst.operands[1].reg == REG_PC)
19720             as_tsktsk (MVE_BAD_PC);
19721         }
19722
19723       /* Duplicate ARM register to lanes of vector.  */
19724       NEON_ENCODE (ARMREG, inst);
19725       switch (et.size)
19726         {
19727         case 8:  inst.instruction |= 0x400000; break;
19728         case 16: inst.instruction |= 0x000020; break;
19729         case 32: inst.instruction |= 0x000000; break;
19730         default: break;
19731         }
19732       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
19733       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
19734       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
19735       inst.instruction |= neon_quad (rs) << 21;
19736       /* The encoding for this instruction is identical for the ARM and Thumb
19737          variants, except for the condition field.  */
19738       do_vfp_cond_or_thumb ();
19739     }
19740 }
19741
19742 static void
19743 do_mve_mov (int toQ)
19744 {
19745   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19746     return;
19747   if (inst.cond > COND_ALWAYS)
19748     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
19749
19750   unsigned Rt = 0, Rt2 = 1, Q0 = 2, Q1 = 3;
19751   if (toQ)
19752     {
19753       Q0 = 0;
19754       Q1 = 1;
19755       Rt = 2;
19756       Rt2 = 3;
19757     }
19758
19759   constraint (inst.operands[Q0].reg != inst.operands[Q1].reg + 2,
19760               _("Index one must be [2,3] and index two must be two less than"
19761                 " index one."));
19762   constraint (inst.operands[Rt].reg == inst.operands[Rt2].reg,
19763               _("General purpose registers may not be the same"));
19764   constraint (inst.operands[Rt].reg == REG_SP
19765               || inst.operands[Rt2].reg == REG_SP,
19766               BAD_SP);
19767   constraint (inst.operands[Rt].reg == REG_PC
19768               || inst.operands[Rt2].reg == REG_PC,
19769               BAD_PC);
19770
19771   inst.instruction = 0xec000f00;
19772   inst.instruction |= HI1 (inst.operands[Q1].reg / 32) << 23;
19773   inst.instruction |= !!toQ << 20;
19774   inst.instruction |= inst.operands[Rt2].reg << 16;
19775   inst.instruction |= LOW4 (inst.operands[Q1].reg / 32) << 13;
19776   inst.instruction |= (inst.operands[Q1].reg % 4) << 4;
19777   inst.instruction |= inst.operands[Rt].reg;
19778 }
19779
19780 static void
19781 do_mve_movn (void)
19782 {
19783   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19784     return;
19785
19786   if (inst.cond > COND_ALWAYS)
19787     inst.pred_insn_type = INSIDE_VPT_INSN;
19788   else
19789     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
19790
19791   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_I16 | N_I32
19792                                             | N_KEY);
19793
19794   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19795   inst.instruction |= (neon_logbits (et.size) - 1) << 18;
19796   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19797   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19798   inst.instruction |= LOW4 (inst.operands[1].reg);
19799   inst.is_neon = 1;
19800
19801 }
19802
19803 /* VMOV has particularly many variations. It can be one of:
19804      0. VMOV<c><q> <Qd>, <Qm>
19805      1. VMOV<c><q> <Dd>, <Dm>
19806    (Register operations, which are VORR with Rm = Rn.)
19807      2. VMOV<c><q>.<dt> <Qd>, #<imm>
19808      3. VMOV<c><q>.<dt> <Dd>, #<imm>
19809    (Immediate loads.)
19810      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
19811    (ARM register to scalar.)
19812      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
19813    (Two ARM registers to vector.)
19814      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
19815    (Scalar to ARM register.)
19816      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
19817    (Vector to two ARM registers.)
19818      8. VMOV.F32 <Sd>, <Sm>
19819      9. VMOV.F64 <Dd>, <Dm>
19820    (VFP register moves.)
19821     10. VMOV.F32 <Sd>, #imm
19822     11. VMOV.F64 <Dd>, #imm
19823    (VFP float immediate load.)
19824     12. VMOV <Rd>, <Sm>
19825    (VFP single to ARM reg.)
19826     13. VMOV <Sd>, <Rm>
19827    (ARM reg to VFP single.)
19828     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
19829    (Two ARM regs to two VFP singles.)
19830     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
19831    (Two VFP singles to two ARM regs.)
19832    16. VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>
19833    17. VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>
19834    18. VMOV<c>.<dt> <Rt>, <Qn[idx]>
19835    19. VMOV<c>.<dt> <Qd[idx]>, <Rt>
19836
19837    These cases can be disambiguated using neon_select_shape, except cases 1/9
19838    and 3/11 which depend on the operand type too.
19839
19840    All the encoded bits are hardcoded by this function.
19841
19842    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
19843    Cases 5, 7 may be used with VFPv2 and above.
19844
19845    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
19846    can specify a type where it doesn't make sense to, and is ignored).  */
19847
19848 static void
19849 do_neon_mov (void)
19850 {
19851   enum neon_shape rs = neon_select_shape (NS_RRSS, NS_SSRR, NS_RRFF, NS_FFRR,
19852                                           NS_DRR, NS_RRD, NS_QQ, NS_DD, NS_QI,
19853                                           NS_DI, NS_SR, NS_RS, NS_FF, NS_FI,
19854                                           NS_RF, NS_FR, NS_HR, NS_RH, NS_HI,
19855                                           NS_NULL);
19856   struct neon_type_el et;
19857   const char *ldconst = 0;
19858
19859   switch (rs)
19860     {
19861     case NS_DD:  /* case 1/9.  */
19862       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
19863       /* It is not an error here if no type is given.  */
19864       inst.error = NULL;
19865       if (et.type == NT_float && et.size == 64)
19866         {
19867           do_vfp_nsyn_opcode ("fcpyd");
19868           break;
19869         }
19870       /* fall through.  */
19871
19872     case NS_QQ:  /* case 0/1.  */
19873       {
19874         if (!check_simd_pred_availability (FALSE,
19875                                            NEON_CHECK_CC | NEON_CHECK_ARCH))
19876           return;
19877         /* The architecture manual I have doesn't explicitly state which
19878            value the U bit should have for register->register moves, but
19879            the equivalent VORR instruction has U = 0, so do that.  */
19880         inst.instruction = 0x0200110;
19881         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19882         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19883         inst.instruction |= LOW4 (inst.operands[1].reg);
19884         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19885         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19886         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19887         inst.instruction |= neon_quad (rs) << 6;
19888
19889         neon_dp_fixup (&inst);
19890       }
19891       break;
19892
19893     case NS_DI:  /* case 3/11.  */
19894       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
19895       inst.error = NULL;
19896       if (et.type == NT_float && et.size == 64)
19897         {
19898           /* case 11 (fconstd).  */
19899           ldconst = "fconstd";
19900           goto encode_fconstd;
19901         }
19902       /* fall through.  */
19903
19904     case NS_QI:  /* case 2/3.  */
19905       if (!check_simd_pred_availability (FALSE,
19906                                          NEON_CHECK_CC | NEON_CHECK_ARCH))
19907         return;
19908       inst.instruction = 0x0800010;
19909       neon_move_immediate ();
19910       neon_dp_fixup (&inst);
19911       break;
19912
19913     case NS_SR:  /* case 4.  */
19914       {
19915         unsigned bcdebits = 0;
19916         int logsize;
19917         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
19918         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
19919
19920         /* .<size> is optional here, defaulting to .32. */
19921         if (inst.vectype.elems == 0
19922             && inst.operands[0].vectype.type == NT_invtype
19923             && inst.operands[1].vectype.type == NT_invtype)
19924           {
19925             inst.vectype.el[0].type = NT_untyped;
19926             inst.vectype.el[0].size = 32;
19927             inst.vectype.elems = 1;
19928           }
19929
19930         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
19931         logsize = neon_logbits (et.size);
19932
19933         if (et.size != 32)
19934           {
19935             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19936                 && vfp_or_neon_is_neon (NEON_CHECK_ARCH) == FAIL)
19937               return;
19938           }
19939         else
19940           {
19941             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
19942                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19943                         _(BAD_FPU));
19944           }
19945
19946         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19947           {
19948             if (inst.operands[1].reg == REG_SP)
19949               as_tsktsk (MVE_BAD_SP);
19950             else if (inst.operands[1].reg == REG_PC)
19951               as_tsktsk (MVE_BAD_PC);
19952           }
19953         unsigned size = inst.operands[0].isscalar == 1 ? 64 : 128;
19954
19955         constraint (et.type == NT_invtype, _("bad type for scalar"));
19956         constraint (x >= size / et.size, _("scalar index out of range"));
19957
19958
19959         switch (et.size)
19960           {
19961           case 8:  bcdebits = 0x8; break;
19962           case 16: bcdebits = 0x1; break;
19963           case 32: bcdebits = 0x0; break;
19964           default: ;
19965           }
19966
19967         bcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
19968
19969         inst.instruction = 0xe000b10;
19970         do_vfp_cond_or_thumb ();
19971         inst.instruction |= LOW4 (dn) << 16;
19972         inst.instruction |= HI1 (dn) << 7;
19973         inst.instruction |= inst.operands[1].reg << 12;
19974         inst.instruction |= (bcdebits & 3) << 5;
19975         inst.instruction |= ((bcdebits >> 2) & 3) << 21;
19976         inst.instruction |= (x >> (3-logsize)) << 16;
19977       }
19978       break;
19979
19980     case NS_DRR:  /* case 5 (fmdrr).  */
19981       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
19982                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19983                   _(BAD_FPU));
19984
19985       inst.instruction = 0xc400b10;
19986       do_vfp_cond_or_thumb ();
19987       inst.instruction |= LOW4 (inst.operands[0].reg);
19988       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
19989       inst.instruction |= inst.operands[1].reg << 12;
19990       inst.instruction |= inst.operands[2].reg << 16;
19991       break;
19992
19993     case NS_RS:  /* case 6.  */
19994       {
19995         unsigned logsize;
19996         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
19997         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
19998         unsigned abcdebits = 0;
19999
20000         /* .<dt> is optional here, defaulting to .32. */
20001         if (inst.vectype.elems == 0
20002             && inst.operands[0].vectype.type == NT_invtype
20003             && inst.operands[1].vectype.type == NT_invtype)
20004           {
20005             inst.vectype.el[0].type = NT_untyped;
20006             inst.vectype.el[0].size = 32;
20007             inst.vectype.elems = 1;
20008           }
20009
20010         et = neon_check_type (2, NS_NULL,
20011                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
20012         logsize = neon_logbits (et.size);
20013
20014         if (et.size != 32)
20015           {
20016             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
20017                 && vfp_or_neon_is_neon (NEON_CHECK_CC
20018                                         | NEON_CHECK_ARCH) == FAIL)
20019               return;
20020           }
20021         else
20022           {
20023             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
20024                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20025                         _(BAD_FPU));
20026           }
20027
20028         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20029           {
20030             if (inst.operands[0].reg == REG_SP)
20031               as_tsktsk (MVE_BAD_SP);
20032             else if (inst.operands[0].reg == REG_PC)
20033               as_tsktsk (MVE_BAD_PC);
20034           }
20035
20036         unsigned size = inst.operands[1].isscalar == 1 ? 64 : 128;
20037
20038         constraint (et.type == NT_invtype, _("bad type for scalar"));
20039         constraint (x >= size / et.size, _("scalar index out of range"));
20040
20041         switch (et.size)
20042           {
20043           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
20044           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
20045           case 32: abcdebits = 0x00; break;
20046           default: ;
20047           }
20048
20049         abcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
20050         inst.instruction = 0xe100b10;
20051         do_vfp_cond_or_thumb ();
20052         inst.instruction |= LOW4 (dn) << 16;
20053         inst.instruction |= HI1 (dn) << 7;
20054         inst.instruction |= inst.operands[0].reg << 12;
20055         inst.instruction |= (abcdebits & 3) << 5;
20056         inst.instruction |= (abcdebits >> 2) << 21;
20057         inst.instruction |= (x >> (3-logsize)) << 16;
20058       }
20059       break;
20060
20061     case NS_RRD:  /* case 7 (fmrrd).  */
20062       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20063                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20064                   _(BAD_FPU));
20065
20066       inst.instruction = 0xc500b10;
20067       do_vfp_cond_or_thumb ();
20068       inst.instruction |= inst.operands[0].reg << 12;
20069       inst.instruction |= inst.operands[1].reg << 16;
20070       inst.instruction |= LOW4 (inst.operands[2].reg);
20071       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
20072       break;
20073
20074     case NS_FF:  /* case 8 (fcpys).  */
20075       do_vfp_nsyn_opcode ("fcpys");
20076       break;
20077
20078     case NS_HI:
20079     case NS_FI:  /* case 10 (fconsts).  */
20080       ldconst = "fconsts";
20081     encode_fconstd:
20082       if (!inst.operands[1].immisfloat)
20083         {
20084           unsigned new_imm;
20085           /* Immediate has to fit in 8 bits so float is enough.  */
20086           float imm = (float) inst.operands[1].imm;
20087           memcpy (&new_imm, &imm, sizeof (float));
20088           /* But the assembly may have been written to provide an integer
20089              bit pattern that equates to a float, so check that the
20090              conversion has worked.  */
20091           if (is_quarter_float (new_imm))
20092             {
20093               if (is_quarter_float (inst.operands[1].imm))
20094                 as_warn (_("immediate constant is valid both as a bit-pattern and a floating point value (using the fp value)"));
20095
20096               inst.operands[1].imm = new_imm;
20097               inst.operands[1].immisfloat = 1;
20098             }
20099         }
20100
20101       if (is_quarter_float (inst.operands[1].imm))
20102         {
20103           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
20104           do_vfp_nsyn_opcode (ldconst);
20105
20106           /* ARMv8.2 fp16 vmov.f16 instruction.  */
20107           if (rs == NS_HI)
20108             do_scalar_fp16_v82_encode ();
20109         }
20110       else
20111         first_error (_("immediate out of range"));
20112       break;
20113
20114     case NS_RH:
20115     case NS_RF:  /* case 12 (fmrs).  */
20116       do_vfp_nsyn_opcode ("fmrs");
20117       /* ARMv8.2 fp16 vmov.f16 instruction.  */
20118       if (rs == NS_RH)
20119         do_scalar_fp16_v82_encode ();
20120       break;
20121
20122     case NS_HR:
20123     case NS_FR:  /* case 13 (fmsr).  */
20124       do_vfp_nsyn_opcode ("fmsr");
20125       /* ARMv8.2 fp16 vmov.f16 instruction.  */
20126       if (rs == NS_HR)
20127         do_scalar_fp16_v82_encode ();
20128       break;
20129
20130     case NS_RRSS:
20131       do_mve_mov (0);
20132       break;
20133     case NS_SSRR:
20134       do_mve_mov (1);
20135       break;
20136
20137     /* The encoders for the fmrrs and fmsrr instructions expect three operands
20138        (one of which is a list), but we have parsed four.  Do some fiddling to
20139        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
20140        expect.  */
20141     case NS_RRFF:  /* case 14 (fmrrs).  */
20142       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20143                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20144                   _(BAD_FPU));
20145       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
20146                   _("VFP registers must be adjacent"));
20147       inst.operands[2].imm = 2;
20148       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
20149       do_vfp_nsyn_opcode ("fmrrs");
20150       break;
20151
20152     case NS_FFRR:  /* case 15 (fmsrr).  */
20153       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20154                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20155                   _(BAD_FPU));
20156       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
20157                   _("VFP registers must be adjacent"));
20158       inst.operands[1] = inst.operands[2];
20159       inst.operands[2] = inst.operands[3];
20160       inst.operands[0].imm = 2;
20161       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
20162       do_vfp_nsyn_opcode ("fmsrr");
20163       break;
20164
20165     case NS_NULL:
20166       /* neon_select_shape has determined that the instruction
20167          shape is wrong and has already set the error message.  */
20168       break;
20169
20170     default:
20171       abort ();
20172     }
20173 }
20174
20175 static void
20176 do_mve_movl (void)
20177 {
20178   if (!(inst.operands[0].present && inst.operands[0].isquad
20179       && inst.operands[1].present && inst.operands[1].isquad
20180       && !inst.operands[2].present))
20181     {
20182       inst.instruction = 0;
20183       inst.cond = 0xb;
20184       if (thumb_mode)
20185         set_pred_insn_type (INSIDE_IT_INSN);
20186       do_neon_mov ();
20187       return;
20188     }
20189
20190   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20191     return;
20192
20193   if (inst.cond != COND_ALWAYS)
20194     inst.pred_insn_type = INSIDE_VPT_INSN;
20195
20196   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_S8 | N_U8
20197                                             | N_S16 | N_U16 | N_KEY);
20198
20199   inst.instruction |= (et.type == NT_unsigned) << 28;
20200   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20201   inst.instruction |= (neon_logbits (et.size) + 1) << 19;
20202   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20203   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
20204   inst.instruction |= LOW4 (inst.operands[1].reg);
20205   inst.is_neon = 1;
20206 }
20207
20208 static void
20209 do_neon_rshift_round_imm (void)
20210 {
20211   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20212    return;
20213
20214   enum neon_shape rs;
20215   struct neon_type_el et;
20216
20217   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20218     {
20219       rs = neon_select_shape (NS_QQI, NS_NULL);
20220       et = neon_check_type (2, rs, N_EQK, N_SU_MVE | N_KEY);
20221     }
20222   else
20223     {
20224       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
20225       et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
20226     }
20227   int imm = inst.operands[2].imm;
20228
20229   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
20230   if (imm == 0)
20231     {
20232       inst.operands[2].present = 0;
20233       do_neon_mov ();
20234       return;
20235     }
20236
20237   constraint (imm < 1 || (unsigned)imm > et.size,
20238               _("immediate out of range for shift"));
20239   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
20240                   et.size - imm);
20241 }
20242
20243 static void
20244 do_neon_movhf (void)
20245 {
20246   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
20247   constraint (rs != NS_HH, _("invalid suffix"));
20248
20249   if (inst.cond != COND_ALWAYS)
20250     {
20251       if (thumb_mode)
20252         {
20253           as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
20254                      " the behaviour is UNPREDICTABLE"));
20255         }
20256       else
20257         {
20258           inst.error = BAD_COND;
20259           return;
20260         }
20261     }
20262
20263   do_vfp_sp_monadic ();
20264
20265   inst.is_neon = 1;
20266   inst.instruction |= 0xf0000000;
20267 }
20268
20269 static void
20270 do_neon_movl (void)
20271 {
20272   struct neon_type_el et = neon_check_type (2, NS_QD,
20273     N_EQK | N_DBL, N_SU_32 | N_KEY);
20274   unsigned sizebits = et.size >> 3;
20275   inst.instruction |= sizebits << 19;
20276   neon_two_same (0, et.type == NT_unsigned, -1);
20277 }
20278
20279 static void
20280 do_neon_trn (void)
20281 {
20282   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20283   struct neon_type_el et = neon_check_type (2, rs,
20284     N_EQK, N_8 | N_16 | N_32 | N_KEY);
20285   NEON_ENCODE (INTEGER, inst);
20286   neon_two_same (neon_quad (rs), 1, et.size);
20287 }
20288
20289 static void
20290 do_neon_zip_uzp (void)
20291 {
20292   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20293   struct neon_type_el et = neon_check_type (2, rs,
20294     N_EQK, N_8 | N_16 | N_32 | N_KEY);
20295   if (rs == NS_DD && et.size == 32)
20296     {
20297       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
20298       inst.instruction = N_MNEM_vtrn;
20299       do_neon_trn ();
20300       return;
20301     }
20302   neon_two_same (neon_quad (rs), 1, et.size);
20303 }
20304
20305 static void
20306 do_neon_sat_abs_neg (void)
20307 {
20308   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
20309     return;
20310
20311   enum neon_shape rs;
20312   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20313     rs = neon_select_shape (NS_QQ, NS_NULL);
20314   else
20315     rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20316   struct neon_type_el et = neon_check_type (2, rs,
20317     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
20318   neon_two_same (neon_quad (rs), 1, et.size);
20319 }
20320
20321 static void
20322 do_neon_pair_long (void)
20323 {
20324   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20325   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
20326   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
20327   inst.instruction |= (et.type == NT_unsigned) << 7;
20328   neon_two_same (neon_quad (rs), 1, et.size);
20329 }
20330
20331 static void
20332 do_neon_recip_est (void)
20333 {
20334   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20335   struct neon_type_el et = neon_check_type (2, rs,
20336     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
20337   inst.instruction |= (et.type == NT_float) << 8;
20338   neon_two_same (neon_quad (rs), 1, et.size);
20339 }
20340
20341 static void
20342 do_neon_cls (void)
20343 {
20344   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20345     return;
20346
20347   enum neon_shape rs;
20348   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20349    rs = neon_select_shape (NS_QQ, NS_NULL);
20350   else
20351    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20352
20353   struct neon_type_el et = neon_check_type (2, rs,
20354     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
20355   neon_two_same (neon_quad (rs), 1, et.size);
20356 }
20357
20358 static void
20359 do_neon_clz (void)
20360 {
20361   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20362     return;
20363
20364   enum neon_shape rs;
20365   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20366    rs = neon_select_shape (NS_QQ, NS_NULL);
20367   else
20368    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20369
20370   struct neon_type_el et = neon_check_type (2, rs,
20371     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
20372   neon_two_same (neon_quad (rs), 1, et.size);
20373 }
20374
20375 static void
20376 do_neon_cnt (void)
20377 {
20378   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20379   struct neon_type_el et = neon_check_type (2, rs,
20380     N_EQK | N_INT, N_8 | N_KEY);
20381   neon_two_same (neon_quad (rs), 1, et.size);
20382 }
20383
20384 static void
20385 do_neon_swp (void)
20386 {
20387   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20388   neon_two_same (neon_quad (rs), 1, -1);
20389 }
20390
20391 static void
20392 do_neon_tbl_tbx (void)
20393 {
20394   unsigned listlenbits;
20395   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
20396
20397   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
20398     {
20399       first_error (_("bad list length for table lookup"));
20400       return;
20401     }
20402
20403   listlenbits = inst.operands[1].imm - 1;
20404   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20405   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20406   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
20407   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
20408   inst.instruction |= LOW4 (inst.operands[2].reg);
20409   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
20410   inst.instruction |= listlenbits << 8;
20411
20412   neon_dp_fixup (&inst);
20413 }
20414
20415 static void
20416 do_neon_ldm_stm (void)
20417 {
20418   /* P, U and L bits are part of bitmask.  */
20419   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
20420   unsigned offsetbits = inst.operands[1].imm * 2;
20421
20422   if (inst.operands[1].issingle)
20423     {
20424       do_vfp_nsyn_ldm_stm (is_dbmode);
20425       return;
20426     }
20427
20428   constraint (is_dbmode && !inst.operands[0].writeback,
20429               _("writeback (!) must be used for VLDMDB and VSTMDB"));
20430
20431   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
20432               _("register list must contain at least 1 and at most 16 "
20433                 "registers"));
20434
20435   inst.instruction |= inst.operands[0].reg << 16;
20436   inst.instruction |= inst.operands[0].writeback << 21;
20437   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
20438   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
20439
20440   inst.instruction |= offsetbits;
20441
20442   do_vfp_cond_or_thumb ();
20443 }
20444
20445 static void
20446 do_neon_ldr_str (void)
20447 {
20448   int is_ldr = (inst.instruction & (1 << 20)) != 0;
20449
20450   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
20451      And is UNPREDICTABLE in thumb mode.  */
20452   if (!is_ldr
20453       && inst.operands[1].reg == REG_PC
20454       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
20455     {
20456       if (thumb_mode)
20457         inst.error = _("Use of PC here is UNPREDICTABLE");
20458       else if (warn_on_deprecated)
20459         as_tsktsk (_("Use of PC here is deprecated"));
20460     }
20461
20462   if (inst.operands[0].issingle)
20463     {
20464       if (is_ldr)
20465         do_vfp_nsyn_opcode ("flds");
20466       else
20467         do_vfp_nsyn_opcode ("fsts");
20468
20469       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
20470       if (inst.vectype.el[0].size == 16)
20471         do_scalar_fp16_v82_encode ();
20472     }
20473   else
20474     {
20475       if (is_ldr)
20476         do_vfp_nsyn_opcode ("fldd");
20477       else
20478         do_vfp_nsyn_opcode ("fstd");
20479     }
20480 }
20481
20482 static void
20483 do_t_vldr_vstr_sysreg (void)
20484 {
20485   int fp_vldr_bitno = 20, sysreg_vldr_bitno = 20;
20486   bfd_boolean is_vldr = ((inst.instruction & (1 << fp_vldr_bitno)) != 0);
20487
20488   /* Use of PC is UNPREDICTABLE.  */
20489   if (inst.operands[1].reg == REG_PC)
20490     inst.error = _("Use of PC here is UNPREDICTABLE");
20491
20492   if (inst.operands[1].immisreg)
20493     inst.error = _("instruction does not accept register index");
20494
20495   if (!inst.operands[1].isreg)
20496     inst.error = _("instruction does not accept PC-relative addressing");
20497
20498   if (abs (inst.operands[1].imm) >= (1 << 7))
20499     inst.error = _("immediate value out of range");
20500
20501   inst.instruction = 0xec000f80;
20502   if (is_vldr)
20503     inst.instruction |= 1 << sysreg_vldr_bitno;
20504   encode_arm_cp_address (1, TRUE, FALSE, BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM);
20505   inst.instruction |= (inst.operands[0].imm & 0x7) << 13;
20506   inst.instruction |= (inst.operands[0].imm & 0x8) << 19;
20507 }
20508
20509 static void
20510 do_vldr_vstr (void)
20511 {
20512   bfd_boolean sysreg_op = !inst.operands[0].isreg;
20513
20514   /* VLDR/VSTR (System Register).  */
20515   if (sysreg_op)
20516     {
20517       if (!mark_feature_used (&arm_ext_v8_1m_main))
20518         as_bad (_("Instruction not permitted on this architecture"));
20519
20520       do_t_vldr_vstr_sysreg ();
20521     }
20522   /* VLDR/VSTR.  */
20523   else
20524     {
20525       if (!mark_feature_used (&fpu_vfp_ext_v1xd))
20526         as_bad (_("Instruction not permitted on this architecture"));
20527       do_neon_ldr_str ();
20528     }
20529 }
20530
20531 /* "interleave" version also handles non-interleaving register VLD1/VST1
20532    instructions.  */
20533
20534 static void
20535 do_neon_ld_st_interleave (void)
20536 {
20537   struct neon_type_el et = neon_check_type (1, NS_NULL,
20538                                             N_8 | N_16 | N_32 | N_64);
20539   unsigned alignbits = 0;
20540   unsigned idx;
20541   /* The bits in this table go:
20542      0: register stride of one (0) or two (1)
20543      1,2: register list length, minus one (1, 2, 3, 4).
20544      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
20545      We use -1 for invalid entries.  */
20546   const int typetable[] =
20547     {
20548       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
20549        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
20550        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
20551        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
20552     };
20553   int typebits;
20554
20555   if (et.type == NT_invtype)
20556     return;
20557
20558   if (inst.operands[1].immisalign)
20559     switch (inst.operands[1].imm >> 8)
20560       {
20561       case 64: alignbits = 1; break;
20562       case 128:
20563         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
20564             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
20565           goto bad_alignment;
20566         alignbits = 2;
20567         break;
20568       case 256:
20569         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
20570           goto bad_alignment;
20571         alignbits = 3;
20572         break;
20573       default:
20574       bad_alignment:
20575         first_error (_("bad alignment"));
20576         return;
20577       }
20578
20579   inst.instruction |= alignbits << 4;
20580   inst.instruction |= neon_logbits (et.size) << 6;
20581
20582   /* Bits [4:6] of the immediate in a list specifier encode register stride
20583      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
20584      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
20585      up the right value for "type" in a table based on this value and the given
20586      list style, then stick it back.  */
20587   idx = ((inst.operands[0].imm >> 4) & 7)
20588         | (((inst.instruction >> 8) & 3) << 3);
20589
20590   typebits = typetable[idx];
20591
20592   constraint (typebits == -1, _("bad list type for instruction"));
20593   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
20594               BAD_EL_TYPE);
20595
20596   inst.instruction &= ~0xf00;
20597   inst.instruction |= typebits << 8;
20598 }
20599
20600 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
20601    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
20602    otherwise. The variable arguments are a list of pairs of legal (size, align)
20603    values, terminated with -1.  */
20604
20605 static int
20606 neon_alignment_bit (int size, int align, int *do_alignment, ...)
20607 {
20608   va_list ap;
20609   int result = FAIL, thissize, thisalign;
20610
20611   if (!inst.operands[1].immisalign)
20612     {
20613       *do_alignment = 0;
20614       return SUCCESS;
20615     }
20616
20617   va_start (ap, do_alignment);
20618
20619   do
20620     {
20621       thissize = va_arg (ap, int);
20622       if (thissize == -1)
20623         break;
20624       thisalign = va_arg (ap, int);
20625
20626       if (size == thissize && align == thisalign)
20627         result = SUCCESS;
20628     }
20629   while (result != SUCCESS);
20630
20631   va_end (ap);
20632
20633   if (result == SUCCESS)
20634     *do_alignment = 1;
20635   else
20636     first_error (_("unsupported alignment for instruction"));
20637
20638   return result;
20639 }
20640
20641 static void
20642 do_neon_ld_st_lane (void)
20643 {
20644   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
20645   int align_good, do_alignment = 0;
20646   int logsize = neon_logbits (et.size);
20647   int align = inst.operands[1].imm >> 8;
20648   int n = (inst.instruction >> 8) & 3;
20649   int max_el = 64 / et.size;
20650
20651   if (et.type == NT_invtype)
20652     return;
20653
20654   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
20655               _("bad list length"));
20656   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
20657               _("scalar index out of range"));
20658   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
20659               && et.size == 8,
20660               _("stride of 2 unavailable when element size is 8"));
20661
20662   switch (n)
20663     {
20664     case 0:  /* VLD1 / VST1.  */
20665       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
20666                                        32, 32, -1);
20667       if (align_good == FAIL)
20668         return;
20669       if (do_alignment)
20670         {
20671           unsigned alignbits = 0;
20672           switch (et.size)
20673             {
20674             case 16: alignbits = 0x1; break;
20675             case 32: alignbits = 0x3; break;
20676             default: ;
20677             }
20678           inst.instruction |= alignbits << 4;
20679         }
20680       break;
20681
20682     case 1:  /* VLD2 / VST2.  */
20683       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
20684                       16, 32, 32, 64, -1);
20685       if (align_good == FAIL)
20686         return;
20687       if (do_alignment)
20688         inst.instruction |= 1 << 4;
20689       break;
20690
20691     case 2:  /* VLD3 / VST3.  */
20692       constraint (inst.operands[1].immisalign,
20693                   _("can't use alignment with this instruction"));
20694       break;
20695
20696     case 3:  /* VLD4 / VST4.  */
20697       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
20698                                        16, 64, 32, 64, 32, 128, -1);
20699       if (align_good == FAIL)
20700         return;
20701       if (do_alignment)
20702         {
20703           unsigned alignbits = 0;
20704           switch (et.size)
20705             {
20706             case 8:  alignbits = 0x1; break;
20707             case 16: alignbits = 0x1; break;
20708             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
20709             default: ;
20710             }
20711           inst.instruction |= alignbits << 4;
20712         }
20713       break;
20714
20715     default: ;
20716     }
20717
20718   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
20719   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20720     inst.instruction |= 1 << (4 + logsize);
20721
20722   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
20723   inst.instruction |= logsize << 10;
20724 }
20725
20726 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
20727
20728 static void
20729 do_neon_ld_dup (void)
20730 {
20731   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
20732   int align_good, do_alignment = 0;
20733
20734   if (et.type == NT_invtype)
20735     return;
20736
20737   switch ((inst.instruction >> 8) & 3)
20738     {
20739     case 0:  /* VLD1.  */
20740       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
20741       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
20742                                        &do_alignment, 16, 16, 32, 32, -1);
20743       if (align_good == FAIL)
20744         return;
20745       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
20746         {
20747         case 1: break;
20748         case 2: inst.instruction |= 1 << 5; break;
20749         default: first_error (_("bad list length")); return;
20750         }
20751       inst.instruction |= neon_logbits (et.size) << 6;
20752       break;
20753
20754     case 1:  /* VLD2.  */
20755       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
20756                                        &do_alignment, 8, 16, 16, 32, 32, 64,
20757                                        -1);
20758       if (align_good == FAIL)
20759         return;
20760       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
20761                   _("bad list length"));
20762       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20763         inst.instruction |= 1 << 5;
20764       inst.instruction |= neon_logbits (et.size) << 6;
20765       break;
20766
20767     case 2:  /* VLD3.  */
20768       constraint (inst.operands[1].immisalign,
20769                   _("can't use alignment with this instruction"));
20770       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
20771                   _("bad list length"));
20772       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20773         inst.instruction |= 1 << 5;
20774       inst.instruction |= neon_logbits (et.size) << 6;
20775       break;
20776
20777     case 3:  /* VLD4.  */
20778       {
20779         int align = inst.operands[1].imm >> 8;
20780         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
20781                                          16, 64, 32, 64, 32, 128, -1);
20782         if (align_good == FAIL)
20783           return;
20784         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
20785                     _("bad list length"));
20786         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20787           inst.instruction |= 1 << 5;
20788         if (et.size == 32 && align == 128)
20789           inst.instruction |= 0x3 << 6;
20790         else
20791           inst.instruction |= neon_logbits (et.size) << 6;
20792       }
20793       break;
20794
20795     default: ;
20796     }
20797
20798   inst.instruction |= do_alignment << 4;
20799 }
20800
20801 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
20802    apart from bits [11:4].  */
20803
20804 static void
20805 do_neon_ldx_stx (void)
20806 {
20807   if (inst.operands[1].isreg)
20808     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
20809
20810   switch (NEON_LANE (inst.operands[0].imm))
20811     {
20812     case NEON_INTERLEAVE_LANES:
20813       NEON_ENCODE (INTERLV, inst);
20814       do_neon_ld_st_interleave ();
20815       break;
20816
20817     case NEON_ALL_LANES:
20818       NEON_ENCODE (DUP, inst);
20819       if (inst.instruction == N_INV)
20820         {
20821           first_error ("only loads support such operands");
20822           break;
20823         }
20824       do_neon_ld_dup ();
20825       break;
20826
20827     default:
20828       NEON_ENCODE (LANE, inst);
20829       do_neon_ld_st_lane ();
20830     }
20831
20832   /* L bit comes from bit mask.  */
20833   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20834   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20835   inst.instruction |= inst.operands[1].reg << 16;
20836
20837   if (inst.operands[1].postind)
20838     {
20839       int postreg = inst.operands[1].imm & 0xf;
20840       constraint (!inst.operands[1].immisreg,
20841                   _("post-index must be a register"));
20842       constraint (postreg == 0xd || postreg == 0xf,
20843                   _("bad register for post-index"));
20844       inst.instruction |= postreg;
20845     }
20846   else
20847     {
20848       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
20849       constraint (inst.relocs[0].exp.X_op != O_constant
20850                   || inst.relocs[0].exp.X_add_number != 0,
20851                   BAD_ADDR_MODE);
20852
20853       if (inst.operands[1].writeback)
20854         {
20855           inst.instruction |= 0xd;
20856         }
20857       else
20858         inst.instruction |= 0xf;
20859     }
20860
20861   if (thumb_mode)
20862     inst.instruction |= 0xf9000000;
20863   else
20864     inst.instruction |= 0xf4000000;
20865 }
20866
20867 /* FP v8.  */
20868 static void
20869 do_vfp_nsyn_fpv8 (enum neon_shape rs)
20870 {
20871   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
20872      D register operands.  */
20873   if (neon_shape_class[rs] == SC_DOUBLE)
20874     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20875                 _(BAD_FPU));
20876
20877   NEON_ENCODE (FPV8, inst);
20878
20879   if (rs == NS_FFF || rs == NS_HHH)
20880     {
20881       do_vfp_sp_dyadic ();
20882
20883       /* ARMv8.2 fp16 instruction.  */
20884       if (rs == NS_HHH)
20885         do_scalar_fp16_v82_encode ();
20886     }
20887   else
20888     do_vfp_dp_rd_rn_rm ();
20889
20890   if (rs == NS_DDD)
20891     inst.instruction |= 0x100;
20892
20893   inst.instruction |= 0xf0000000;
20894 }
20895
20896 static void
20897 do_vsel (void)
20898 {
20899   set_pred_insn_type (OUTSIDE_PRED_INSN);
20900
20901   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
20902     first_error (_("invalid instruction shape"));
20903 }
20904
20905 static void
20906 do_vmaxnm (void)
20907 {
20908   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20909     set_pred_insn_type (OUTSIDE_PRED_INSN);
20910
20911   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
20912     return;
20913
20914   if (!check_simd_pred_availability (TRUE, NEON_CHECK_CC | NEON_CHECK_ARCH8))
20915     return;
20916
20917   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
20918 }
20919
20920 static void
20921 do_vrint_1 (enum neon_cvt_mode mode)
20922 {
20923   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
20924   struct neon_type_el et;
20925
20926   if (rs == NS_NULL)
20927     return;
20928
20929   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
20930      D register operands.  */
20931   if (neon_shape_class[rs] == SC_DOUBLE)
20932     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20933                 _(BAD_FPU));
20934
20935   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
20936                         | N_VFP);
20937   if (et.type != NT_invtype)
20938     {
20939       /* VFP encodings.  */
20940       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
20941           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
20942         set_pred_insn_type (OUTSIDE_PRED_INSN);
20943
20944       NEON_ENCODE (FPV8, inst);
20945       if (rs == NS_FF || rs == NS_HH)
20946         do_vfp_sp_monadic ();
20947       else
20948         do_vfp_dp_rd_rm ();
20949
20950       switch (mode)
20951         {
20952         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
20953         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
20954         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
20955         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
20956         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
20957         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
20958         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
20959         default: abort ();
20960         }
20961
20962       inst.instruction |= (rs == NS_DD) << 8;
20963       do_vfp_cond_or_thumb ();
20964
20965       /* ARMv8.2 fp16 vrint instruction.  */
20966       if (rs == NS_HH)
20967       do_scalar_fp16_v82_encode ();
20968     }
20969   else
20970     {
20971       /* Neon encodings (or something broken...).  */
20972       inst.error = NULL;
20973       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
20974
20975       if (et.type == NT_invtype)
20976         return;
20977
20978       if (!check_simd_pred_availability (TRUE,
20979                                          NEON_CHECK_CC | NEON_CHECK_ARCH8))
20980         return;
20981
20982       NEON_ENCODE (FLOAT, inst);
20983
20984       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20985       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20986       inst.instruction |= LOW4 (inst.operands[1].reg);
20987       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
20988       inst.instruction |= neon_quad (rs) << 6;
20989       /* Mask off the original size bits and reencode them.  */
20990       inst.instruction = ((inst.instruction & 0xfff3ffff)
20991                           | neon_logbits (et.size) << 18);
20992
20993       switch (mode)
20994         {
20995         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
20996         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
20997         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
20998         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
20999         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
21000         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
21001         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
21002         default: abort ();
21003         }
21004
21005       if (thumb_mode)
21006         inst.instruction |= 0xfc000000;
21007       else
21008         inst.instruction |= 0xf0000000;
21009     }
21010 }
21011
21012 static void
21013 do_vrintx (void)
21014 {
21015   do_vrint_1 (neon_cvt_mode_x);
21016 }
21017
21018 static void
21019 do_vrintz (void)
21020 {
21021   do_vrint_1 (neon_cvt_mode_z);
21022 }
21023
21024 static void
21025 do_vrintr (void)
21026 {
21027   do_vrint_1 (neon_cvt_mode_r);
21028 }
21029
21030 static void
21031 do_vrinta (void)
21032 {
21033   do_vrint_1 (neon_cvt_mode_a);
21034 }
21035
21036 static void
21037 do_vrintn (void)
21038 {
21039   do_vrint_1 (neon_cvt_mode_n);
21040 }
21041
21042 static void
21043 do_vrintp (void)
21044 {
21045   do_vrint_1 (neon_cvt_mode_p);
21046 }
21047
21048 static void
21049 do_vrintm (void)
21050 {
21051   do_vrint_1 (neon_cvt_mode_m);
21052 }
21053
21054 static unsigned
21055 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
21056 {
21057   unsigned regno = NEON_SCALAR_REG (opnd);
21058   unsigned elno = NEON_SCALAR_INDEX (opnd);
21059
21060   if (elsize == 16 && elno < 2 && regno < 16)
21061     return regno | (elno << 4);
21062   else if (elsize == 32 && elno == 0)
21063     return regno;
21064
21065   first_error (_("scalar out of range"));
21066   return 0;
21067 }
21068
21069 static void
21070 do_vcmla (void)
21071 {
21072   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext)
21073               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
21074                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
21075   constraint (inst.relocs[0].exp.X_op != O_constant,
21076               _("expression too complex"));
21077   unsigned rot = inst.relocs[0].exp.X_add_number;
21078   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
21079               _("immediate out of range"));
21080   rot /= 90;
21081
21082   if (!check_simd_pred_availability (TRUE,
21083                                      NEON_CHECK_ARCH8 | NEON_CHECK_CC))
21084     return;
21085
21086   if (inst.operands[2].isscalar)
21087     {
21088       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
21089         first_error (_("invalid instruction shape"));
21090       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
21091       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
21092                                        N_KEY | N_F16 | N_F32).size;
21093       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
21094       inst.is_neon = 1;
21095       inst.instruction = 0xfe000800;
21096       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21097       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21098       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
21099       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
21100       inst.instruction |= LOW4 (m);
21101       inst.instruction |= HI1 (m) << 5;
21102       inst.instruction |= neon_quad (rs) << 6;
21103       inst.instruction |= rot << 20;
21104       inst.instruction |= (size == 32) << 23;
21105     }
21106   else
21107     {
21108       enum neon_shape rs;
21109       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
21110         rs = neon_select_shape (NS_QQQI, NS_NULL);
21111       else
21112         rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
21113
21114       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
21115                                        N_KEY | N_F16 | N_F32).size;
21116       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext) && size == 32
21117           && (inst.operands[0].reg == inst.operands[1].reg
21118               || inst.operands[0].reg == inst.operands[2].reg))
21119         as_tsktsk (BAD_MVE_SRCDEST);
21120
21121       neon_three_same (neon_quad (rs), 0, -1);
21122       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
21123       inst.instruction |= 0xfc200800;
21124       inst.instruction |= rot << 23;
21125       inst.instruction |= (size == 32) << 20;
21126     }
21127 }
21128
21129 static void
21130 do_vcadd (void)
21131 {
21132   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
21133               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
21134                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
21135   constraint (inst.relocs[0].exp.X_op != O_constant,
21136               _("expression too complex"));
21137
21138   unsigned rot = inst.relocs[0].exp.X_add_number;
21139   constraint (rot != 90 && rot != 270, _("immediate out of range"));
21140   enum neon_shape rs;
21141   struct neon_type_el et;
21142   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
21143     {
21144       rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
21145       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32);
21146     }
21147   else
21148     {
21149       rs = neon_select_shape (NS_QQQI, NS_NULL);
21150       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32 | N_I8
21151                             | N_I16 | N_I32);
21152       if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
21153         as_tsktsk (_("Warning: 32-bit element size and same first and third "
21154                      "operand makes instruction UNPREDICTABLE"));
21155     }
21156
21157   if (et.type == NT_invtype)
21158     return;
21159
21160   if (!check_simd_pred_availability (et.type == NT_float,
21161                                      NEON_CHECK_ARCH8 | NEON_CHECK_CC))
21162     return;
21163
21164   if (et.type == NT_float)
21165     {
21166       neon_three_same (neon_quad (rs), 0, -1);
21167       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
21168       inst.instruction |= 0xfc800800;
21169       inst.instruction |= (rot == 270) << 24;
21170       inst.instruction |= (et.size == 32) << 20;
21171     }
21172   else
21173     {
21174       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
21175       inst.instruction = 0xfe000f00;
21176       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21177       inst.instruction |= neon_logbits (et.size) << 20;
21178       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
21179       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21180       inst.instruction |= (rot == 270) << 12;
21181       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
21182       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
21183       inst.instruction |= LOW4 (inst.operands[2].reg);
21184       inst.is_neon = 1;
21185     }
21186 }
21187
21188 /* Dot Product instructions encoding support.  */
21189
21190 static void
21191 do_neon_dotproduct (int unsigned_p)
21192 {
21193   enum neon_shape rs;
21194   unsigned scalar_oprd2 = 0;
21195   int high8;
21196
21197   if (inst.cond != COND_ALWAYS)
21198     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
21199                "is UNPREDICTABLE"));
21200
21201   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
21202               _(BAD_FPU));
21203
21204   /* Dot Product instructions are in three-same D/Q register format or the third
21205      operand can be a scalar index register.  */
21206   if (inst.operands[2].isscalar)
21207     {
21208       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
21209       high8 = 0xfe000000;
21210       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
21211     }
21212   else
21213     {
21214       high8 = 0xfc000000;
21215       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
21216     }
21217
21218   if (unsigned_p)
21219     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
21220   else
21221     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
21222
21223   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
21224      Product instruction, so we pass 0 as the "ubit" parameter.  And the
21225      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
21226   neon_three_same (neon_quad (rs), 0, 32);
21227
21228   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
21229      different NEON three-same encoding.  */
21230   inst.instruction &= 0x00ffffff;
21231   inst.instruction |= high8;
21232   /* Encode 'U' bit which indicates signedness.  */
21233   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
21234   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
21235      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
21236      the instruction encoding.  */
21237   if (inst.operands[2].isscalar)
21238     {
21239       inst.instruction &= 0xffffffd0;
21240       inst.instruction |= LOW4 (scalar_oprd2);
21241       inst.instruction |= HI1 (scalar_oprd2) << 5;
21242     }
21243 }
21244
21245 /* Dot Product instructions for signed integer.  */
21246
21247 static void
21248 do_neon_dotproduct_s (void)
21249 {
21250   return do_neon_dotproduct (0);
21251 }
21252
21253 /* Dot Product instructions for unsigned integer.  */
21254
21255 static void
21256 do_neon_dotproduct_u (void)
21257 {
21258   return do_neon_dotproduct (1);
21259 }
21260
21261 /* Crypto v1 instructions.  */
21262 static void
21263 do_crypto_2op_1 (unsigned elttype, int op)
21264 {
21265   set_pred_insn_type (OUTSIDE_PRED_INSN);
21266
21267   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
21268       == NT_invtype)
21269     return;
21270
21271   inst.error = NULL;
21272
21273   NEON_ENCODE (INTEGER, inst);
21274   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21275   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21276   inst.instruction |= LOW4 (inst.operands[1].reg);
21277   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
21278   if (op != -1)
21279     inst.instruction |= op << 6;
21280
21281   if (thumb_mode)
21282     inst.instruction |= 0xfc000000;
21283   else
21284     inst.instruction |= 0xf0000000;
21285 }
21286
21287 static void
21288 do_crypto_3op_1 (int u, int op)
21289 {
21290   set_pred_insn_type (OUTSIDE_PRED_INSN);
21291
21292   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
21293                        N_32 | N_UNT | N_KEY).type == NT_invtype)
21294     return;
21295
21296   inst.error = NULL;
21297
21298   NEON_ENCODE (INTEGER, inst);
21299   neon_three_same (1, u, 8 << op);
21300 }
21301
21302 static void
21303 do_aese (void)
21304 {
21305   do_crypto_2op_1 (N_8, 0);
21306 }
21307
21308 static void
21309 do_aesd (void)
21310 {
21311   do_crypto_2op_1 (N_8, 1);
21312 }
21313
21314 static void
21315 do_aesmc (void)
21316 {
21317   do_crypto_2op_1 (N_8, 2);
21318 }
21319
21320 static void
21321 do_aesimc (void)
21322 {
21323   do_crypto_2op_1 (N_8, 3);
21324 }
21325
21326 static void
21327 do_sha1c (void)
21328 {
21329   do_crypto_3op_1 (0, 0);
21330 }
21331
21332 static void
21333 do_sha1p (void)
21334 {
21335   do_crypto_3op_1 (0, 1);
21336 }
21337
21338 static void
21339 do_sha1m (void)
21340 {
21341   do_crypto_3op_1 (0, 2);
21342 }
21343
21344 static void
21345 do_sha1su0 (void)
21346 {
21347   do_crypto_3op_1 (0, 3);
21348 }
21349
21350 static void
21351 do_sha256h (void)
21352 {
21353   do_crypto_3op_1 (1, 0);
21354 }
21355
21356 static void
21357 do_sha256h2 (void)
21358 {
21359   do_crypto_3op_1 (1, 1);
21360 }
21361
21362 static void
21363 do_sha256su1 (void)
21364 {
21365   do_crypto_3op_1 (1, 2);
21366 }
21367
21368 static void
21369 do_sha1h (void)
21370 {
21371   do_crypto_2op_1 (N_32, -1);
21372 }
21373
21374 static void
21375 do_sha1su1 (void)
21376 {
21377   do_crypto_2op_1 (N_32, 0);
21378 }
21379
21380 static void
21381 do_sha256su0 (void)
21382 {
21383   do_crypto_2op_1 (N_32, 1);
21384 }
21385
21386 static void
21387 do_crc32_1 (unsigned int poly, unsigned int sz)
21388 {
21389   unsigned int Rd = inst.operands[0].reg;
21390   unsigned int Rn = inst.operands[1].reg;
21391   unsigned int Rm = inst.operands[2].reg;
21392
21393   set_pred_insn_type (OUTSIDE_PRED_INSN);
21394   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
21395   inst.instruction |= LOW4 (Rn) << 16;
21396   inst.instruction |= LOW4 (Rm);
21397   inst.instruction |= sz << (thumb_mode ? 4 : 21);
21398   inst.instruction |= poly << (thumb_mode ? 20 : 9);
21399
21400   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
21401     as_warn (UNPRED_REG ("r15"));
21402 }
21403
21404 static void
21405 do_crc32b (void)
21406 {
21407   do_crc32_1 (0, 0);
21408 }
21409
21410 static void
21411 do_crc32h (void)
21412 {
21413   do_crc32_1 (0, 1);
21414 }
21415
21416 static void
21417 do_crc32w (void)
21418 {
21419   do_crc32_1 (0, 2);
21420 }
21421
21422 static void
21423 do_crc32cb (void)
21424 {
21425   do_crc32_1 (1, 0);
21426 }
21427
21428 static void
21429 do_crc32ch (void)
21430 {
21431   do_crc32_1 (1, 1);
21432 }
21433
21434 static void
21435 do_crc32cw (void)
21436 {
21437   do_crc32_1 (1, 2);
21438 }
21439
21440 static void
21441 do_vjcvt (void)
21442 {
21443   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
21444               _(BAD_FPU));
21445   neon_check_type (2, NS_FD, N_S32, N_F64);
21446   do_vfp_sp_dp_cvt ();
21447   do_vfp_cond_or_thumb ();
21448 }
21449
21450 \f
21451 /* Overall per-instruction processing.  */
21452
21453 /* We need to be able to fix up arbitrary expressions in some statements.
21454    This is so that we can handle symbols that are an arbitrary distance from
21455    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
21456    which returns part of an address in a form which will be valid for
21457    a data instruction.  We do this by pushing the expression into a symbol
21458    in the expr_section, and creating a fix for that.  */
21459
21460 static void
21461 fix_new_arm (fragS *       frag,
21462              int           where,
21463              short int     size,
21464              expressionS * exp,
21465              int           pc_rel,
21466              int           reloc)
21467 {
21468   fixS *           new_fix;
21469
21470   switch (exp->X_op)
21471     {
21472     case O_constant:
21473       if (pc_rel)
21474         {
21475           /* Create an absolute valued symbol, so we have something to
21476              refer to in the object file.  Unfortunately for us, gas's
21477              generic expression parsing will already have folded out
21478              any use of .set foo/.type foo %function that may have
21479              been used to set type information of the target location,
21480              that's being specified symbolically.  We have to presume
21481              the user knows what they are doing.  */
21482           char name[16 + 8];
21483           symbolS *symbol;
21484
21485           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
21486
21487           symbol = symbol_find_or_make (name);
21488           S_SET_SEGMENT (symbol, absolute_section);
21489           symbol_set_frag (symbol, &zero_address_frag);
21490           S_SET_VALUE (symbol, exp->X_add_number);
21491           exp->X_op = O_symbol;
21492           exp->X_add_symbol = symbol;
21493           exp->X_add_number = 0;
21494         }
21495       /* FALLTHROUGH */
21496     case O_symbol:
21497     case O_add:
21498     case O_subtract:
21499       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
21500                              (enum bfd_reloc_code_real) reloc);
21501       break;
21502
21503     default:
21504       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
21505                                   pc_rel, (enum bfd_reloc_code_real) reloc);
21506       break;
21507     }
21508
21509   /* Mark whether the fix is to a THUMB instruction, or an ARM
21510      instruction.  */
21511   new_fix->tc_fix_data = thumb_mode;
21512 }
21513
21514 /* Create a frg for an instruction requiring relaxation.  */
21515 static void
21516 output_relax_insn (void)
21517 {
21518   char * to;
21519   symbolS *sym;
21520   int offset;
21521
21522   /* The size of the instruction is unknown, so tie the debug info to the
21523      start of the instruction.  */
21524   dwarf2_emit_insn (0);
21525
21526   switch (inst.relocs[0].exp.X_op)
21527     {
21528     case O_symbol:
21529       sym = inst.relocs[0].exp.X_add_symbol;
21530       offset = inst.relocs[0].exp.X_add_number;
21531       break;
21532     case O_constant:
21533       sym = NULL;
21534       offset = inst.relocs[0].exp.X_add_number;
21535       break;
21536     default:
21537       sym = make_expr_symbol (&inst.relocs[0].exp);
21538       offset = 0;
21539       break;
21540   }
21541   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
21542                  inst.relax, sym, offset, NULL/*offset, opcode*/);
21543   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
21544 }
21545
21546 /* Write a 32-bit thumb instruction to buf.  */
21547 static void
21548 put_thumb32_insn (char * buf, unsigned long insn)
21549 {
21550   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
21551   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
21552 }
21553
21554 static void
21555 output_inst (const char * str)
21556 {
21557   char * to = NULL;
21558
21559   if (inst.error)
21560     {
21561       as_bad ("%s -- `%s'", inst.error, str);
21562       return;
21563     }
21564   if (inst.relax)
21565     {
21566       output_relax_insn ();
21567       return;
21568     }
21569   if (inst.size == 0)
21570     return;
21571
21572   to = frag_more (inst.size);
21573   /* PR 9814: Record the thumb mode into the current frag so that we know
21574      what type of NOP padding to use, if necessary.  We override any previous
21575      setting so that if the mode has changed then the NOPS that we use will
21576      match the encoding of the last instruction in the frag.  */
21577   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21578
21579   if (thumb_mode && (inst.size > THUMB_SIZE))
21580     {
21581       gas_assert (inst.size == (2 * THUMB_SIZE));
21582       put_thumb32_insn (to, inst.instruction);
21583     }
21584   else if (inst.size > INSN_SIZE)
21585     {
21586       gas_assert (inst.size == (2 * INSN_SIZE));
21587       md_number_to_chars (to, inst.instruction, INSN_SIZE);
21588       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
21589     }
21590   else
21591     md_number_to_chars (to, inst.instruction, inst.size);
21592
21593   int r;
21594   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
21595     {
21596       if (inst.relocs[r].type != BFD_RELOC_UNUSED)
21597         fix_new_arm (frag_now, to - frag_now->fr_literal,
21598                      inst.size, & inst.relocs[r].exp, inst.relocs[r].pc_rel,
21599                      inst.relocs[r].type);
21600     }
21601
21602   dwarf2_emit_insn (inst.size);
21603 }
21604
21605 static char *
21606 output_it_inst (int cond, int mask, char * to)
21607 {
21608   unsigned long instruction = 0xbf00;
21609
21610   mask &= 0xf;
21611   instruction |= mask;
21612   instruction |= cond << 4;
21613
21614   if (to == NULL)
21615     {
21616       to = frag_more (2);
21617 #ifdef OBJ_ELF
21618       dwarf2_emit_insn (2);
21619 #endif
21620     }
21621
21622   md_number_to_chars (to, instruction, 2);
21623
21624   return to;
21625 }
21626
21627 /* Tag values used in struct asm_opcode's tag field.  */
21628 enum opcode_tag
21629 {
21630   OT_unconditional,     /* Instruction cannot be conditionalized.
21631                            The ARM condition field is still 0xE.  */
21632   OT_unconditionalF,    /* Instruction cannot be conditionalized
21633                            and carries 0xF in its ARM condition field.  */
21634   OT_csuffix,           /* Instruction takes a conditional suffix.  */
21635   OT_csuffixF,          /* Some forms of the instruction take a scalar
21636                            conditional suffix, others place 0xF where the
21637                            condition field would be, others take a vector
21638                            conditional suffix.  */
21639   OT_cinfix3,           /* Instruction takes a conditional infix,
21640                            beginning at character index 3.  (In
21641                            unified mode, it becomes a suffix.)  */
21642   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
21643                             tsts, cmps, cmns, and teqs. */
21644   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
21645                            character index 3, even in unified mode.  Used for
21646                            legacy instructions where suffix and infix forms
21647                            may be ambiguous.  */
21648   OT_csuf_or_in3,       /* Instruction takes either a conditional
21649                            suffix or an infix at character index 3.  */
21650   OT_odd_infix_unc,     /* This is the unconditional variant of an
21651                            instruction that takes a conditional infix
21652                            at an unusual position.  In unified mode,
21653                            this variant will accept a suffix.  */
21654   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
21655                            are the conditional variants of instructions that
21656                            take conditional infixes in unusual positions.
21657                            The infix appears at character index
21658                            (tag - OT_odd_infix_0).  These are not accepted
21659                            in unified mode.  */
21660 };
21661
21662 /* Subroutine of md_assemble, responsible for looking up the primary
21663    opcode from the mnemonic the user wrote.  STR points to the
21664    beginning of the mnemonic.
21665
21666    This is not simply a hash table lookup, because of conditional
21667    variants.  Most instructions have conditional variants, which are
21668    expressed with a _conditional affix_ to the mnemonic.  If we were
21669    to encode each conditional variant as a literal string in the opcode
21670    table, it would have approximately 20,000 entries.
21671
21672    Most mnemonics take this affix as a suffix, and in unified syntax,
21673    'most' is upgraded to 'all'.  However, in the divided syntax, some
21674    instructions take the affix as an infix, notably the s-variants of
21675    the arithmetic instructions.  Of those instructions, all but six
21676    have the infix appear after the third character of the mnemonic.
21677
21678    Accordingly, the algorithm for looking up primary opcodes given
21679    an identifier is:
21680
21681    1. Look up the identifier in the opcode table.
21682       If we find a match, go to step U.
21683
21684    2. Look up the last two characters of the identifier in the
21685       conditions table.  If we find a match, look up the first N-2
21686       characters of the identifier in the opcode table.  If we
21687       find a match, go to step CE.
21688
21689    3. Look up the fourth and fifth characters of the identifier in
21690       the conditions table.  If we find a match, extract those
21691       characters from the identifier, and look up the remaining
21692       characters in the opcode table.  If we find a match, go
21693       to step CM.
21694
21695    4. Fail.
21696
21697    U. Examine the tag field of the opcode structure, in case this is
21698       one of the six instructions with its conditional infix in an
21699       unusual place.  If it is, the tag tells us where to find the
21700       infix; look it up in the conditions table and set inst.cond
21701       accordingly.  Otherwise, this is an unconditional instruction.
21702       Again set inst.cond accordingly.  Return the opcode structure.
21703
21704   CE. Examine the tag field to make sure this is an instruction that
21705       should receive a conditional suffix.  If it is not, fail.
21706       Otherwise, set inst.cond from the suffix we already looked up,
21707       and return the opcode structure.
21708
21709   CM. Examine the tag field to make sure this is an instruction that
21710       should receive a conditional infix after the third character.
21711       If it is not, fail.  Otherwise, undo the edits to the current
21712       line of input and proceed as for case CE.  */
21713
21714 static const struct asm_opcode *
21715 opcode_lookup (char **str)
21716 {
21717   char *end, *base;
21718   char *affix;
21719   const struct asm_opcode *opcode;
21720   const struct asm_cond *cond;
21721   char save[2];
21722
21723   /* Scan up to the end of the mnemonic, which must end in white space,
21724      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
21725   for (base = end = *str; *end != '\0'; end++)
21726     if (*end == ' ' || *end == '.')
21727       break;
21728
21729   if (end == base)
21730     return NULL;
21731
21732   /* Handle a possible width suffix and/or Neon type suffix.  */
21733   if (end[0] == '.')
21734     {
21735       int offset = 2;
21736
21737       /* The .w and .n suffixes are only valid if the unified syntax is in
21738          use.  */
21739       if (unified_syntax && end[1] == 'w')
21740         inst.size_req = 4;
21741       else if (unified_syntax && end[1] == 'n')
21742         inst.size_req = 2;
21743       else
21744         offset = 0;
21745
21746       inst.vectype.elems = 0;
21747
21748       *str = end + offset;
21749
21750       if (end[offset] == '.')
21751         {
21752           /* See if we have a Neon type suffix (possible in either unified or
21753              non-unified ARM syntax mode).  */
21754           if (parse_neon_type (&inst.vectype, str) == FAIL)
21755             return NULL;
21756         }
21757       else if (end[offset] != '\0' && end[offset] != ' ')
21758         return NULL;
21759     }
21760   else
21761     *str = end;
21762
21763   /* Look for unaffixed or special-case affixed mnemonic.  */
21764   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21765                                                     end - base);
21766   if (opcode)
21767     {
21768       /* step U */
21769       if (opcode->tag < OT_odd_infix_0)
21770         {
21771           inst.cond = COND_ALWAYS;
21772           return opcode;
21773         }
21774
21775       if (warn_on_deprecated && unified_syntax)
21776         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
21777       affix = base + (opcode->tag - OT_odd_infix_0);
21778       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21779       gas_assert (cond);
21780
21781       inst.cond = cond->value;
21782       return opcode;
21783     }
21784  if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
21785    {
21786     /* Cannot have a conditional suffix on a mnemonic of less than a character.
21787      */
21788     if (end - base < 2)
21789       return NULL;
21790      affix = end - 1;
21791      cond = (const struct asm_cond *) hash_find_n (arm_vcond_hsh, affix, 1);
21792      opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21793                                                       affix - base);
21794      /* If this opcode can not be vector predicated then don't accept it with a
21795         vector predication code.  */
21796      if (opcode && !opcode->mayBeVecPred)
21797        opcode = NULL;
21798    }
21799   if (!opcode || !cond)
21800     {
21801       /* Cannot have a conditional suffix on a mnemonic of less than two
21802          characters.  */
21803       if (end - base < 3)
21804         return NULL;
21805
21806       /* Look for suffixed mnemonic.  */
21807       affix = end - 2;
21808       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21809       opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21810                                                         affix - base);
21811     }
21812
21813   if (opcode && cond)
21814     {
21815       /* step CE */
21816       switch (opcode->tag)
21817         {
21818         case OT_cinfix3_legacy:
21819           /* Ignore conditional suffixes matched on infix only mnemonics.  */
21820           break;
21821
21822         case OT_cinfix3:
21823         case OT_cinfix3_deprecated:
21824         case OT_odd_infix_unc:
21825           if (!unified_syntax)
21826             return NULL;
21827           /* Fall through.  */
21828
21829         case OT_csuffix:
21830         case OT_csuffixF:
21831         case OT_csuf_or_in3:
21832           inst.cond = cond->value;
21833           return opcode;
21834
21835         case OT_unconditional:
21836         case OT_unconditionalF:
21837           if (thumb_mode)
21838             inst.cond = cond->value;
21839           else
21840             {
21841               /* Delayed diagnostic.  */
21842               inst.error = BAD_COND;
21843               inst.cond = COND_ALWAYS;
21844             }
21845           return opcode;
21846
21847         default:
21848           return NULL;
21849         }
21850     }
21851
21852   /* Cannot have a usual-position infix on a mnemonic of less than
21853      six characters (five would be a suffix).  */
21854   if (end - base < 6)
21855     return NULL;
21856
21857   /* Look for infixed mnemonic in the usual position.  */
21858   affix = base + 3;
21859   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21860   if (!cond)
21861     return NULL;
21862
21863   memcpy (save, affix, 2);
21864   memmove (affix, affix + 2, (end - affix) - 2);
21865   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21866                                                     (end - base) - 2);
21867   memmove (affix + 2, affix, (end - affix) - 2);
21868   memcpy (affix, save, 2);
21869
21870   if (opcode
21871       && (opcode->tag == OT_cinfix3
21872           || opcode->tag == OT_cinfix3_deprecated
21873           || opcode->tag == OT_csuf_or_in3
21874           || opcode->tag == OT_cinfix3_legacy))
21875     {
21876       /* Step CM.  */
21877       if (warn_on_deprecated && unified_syntax
21878           && (opcode->tag == OT_cinfix3
21879               || opcode->tag == OT_cinfix3_deprecated))
21880         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
21881
21882       inst.cond = cond->value;
21883       return opcode;
21884     }
21885
21886   return NULL;
21887 }
21888
21889 /* This function generates an initial IT instruction, leaving its block
21890    virtually open for the new instructions. Eventually,
21891    the mask will be updated by now_pred_add_mask () each time
21892    a new instruction needs to be included in the IT block.
21893    Finally, the block is closed with close_automatic_it_block ().
21894    The block closure can be requested either from md_assemble (),
21895    a tencode (), or due to a label hook.  */
21896
21897 static void
21898 new_automatic_it_block (int cond)
21899 {
21900   now_pred.state = AUTOMATIC_PRED_BLOCK;
21901   now_pred.mask = 0x18;
21902   now_pred.cc = cond;
21903   now_pred.block_length = 1;
21904   mapping_state (MAP_THUMB);
21905   now_pred.insn = output_it_inst (cond, now_pred.mask, NULL);
21906   now_pred.warn_deprecated = FALSE;
21907   now_pred.insn_cond = TRUE;
21908 }
21909
21910 /* Close an automatic IT block.
21911    See comments in new_automatic_it_block ().  */
21912
21913 static void
21914 close_automatic_it_block (void)
21915 {
21916   now_pred.mask = 0x10;
21917   now_pred.block_length = 0;
21918 }
21919
21920 /* Update the mask of the current automatically-generated IT
21921    instruction. See comments in new_automatic_it_block ().  */
21922
21923 static void
21924 now_pred_add_mask (int cond)
21925 {
21926 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
21927 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
21928                                               | ((bitvalue) << (nbit)))
21929   const int resulting_bit = (cond & 1);
21930
21931   now_pred.mask &= 0xf;
21932   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
21933                                    resulting_bit,
21934                                   (5 - now_pred.block_length));
21935   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
21936                                    1,
21937                                    ((5 - now_pred.block_length) - 1));
21938   output_it_inst (now_pred.cc, now_pred.mask, now_pred.insn);
21939
21940 #undef CLEAR_BIT
21941 #undef SET_BIT_VALUE
21942 }
21943
21944 /* The IT blocks handling machinery is accessed through the these functions:
21945      it_fsm_pre_encode ()               from md_assemble ()
21946      set_pred_insn_type ()              optional, from the tencode functions
21947      set_pred_insn_type_last ()         ditto
21948      in_pred_block ()                   ditto
21949      it_fsm_post_encode ()              from md_assemble ()
21950      force_automatic_it_block_close ()  from label handling functions
21951
21952    Rationale:
21953      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
21954         initializing the IT insn type with a generic initial value depending
21955         on the inst.condition.
21956      2) During the tencode function, two things may happen:
21957         a) The tencode function overrides the IT insn type by
21958            calling either set_pred_insn_type (type) or
21959            set_pred_insn_type_last ().
21960         b) The tencode function queries the IT block state by
21961            calling in_pred_block () (i.e. to determine narrow/not narrow mode).
21962
21963         Both set_pred_insn_type and in_pred_block run the internal FSM state
21964         handling function (handle_pred_state), because: a) setting the IT insn
21965         type may incur in an invalid state (exiting the function),
21966         and b) querying the state requires the FSM to be updated.
21967         Specifically we want to avoid creating an IT block for conditional
21968         branches, so it_fsm_pre_encode is actually a guess and we can't
21969         determine whether an IT block is required until the tencode () routine
21970         has decided what type of instruction this actually it.
21971         Because of this, if set_pred_insn_type and in_pred_block have to be
21972         used, set_pred_insn_type has to be called first.
21973
21974         set_pred_insn_type_last () is a wrapper of set_pred_insn_type (type),
21975         that determines the insn IT type depending on the inst.cond code.
21976         When a tencode () routine encodes an instruction that can be
21977         either outside an IT block, or, in the case of being inside, has to be
21978         the last one, set_pred_insn_type_last () will determine the proper
21979         IT instruction type based on the inst.cond code. Otherwise,
21980         set_pred_insn_type can be called for overriding that logic or
21981         for covering other cases.
21982
21983         Calling handle_pred_state () may not transition the IT block state to
21984         OUTSIDE_PRED_BLOCK immediately, since the (current) state could be
21985         still queried. Instead, if the FSM determines that the state should
21986         be transitioned to OUTSIDE_PRED_BLOCK, a flag is marked to be closed
21987         after the tencode () function: that's what it_fsm_post_encode () does.
21988
21989         Since in_pred_block () calls the state handling function to get an
21990         updated state, an error may occur (due to invalid insns combination).
21991         In that case, inst.error is set.
21992         Therefore, inst.error has to be checked after the execution of
21993         the tencode () routine.
21994
21995      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
21996         any pending state change (if any) that didn't take place in
21997         handle_pred_state () as explained above.  */
21998
21999 static void
22000 it_fsm_pre_encode (void)
22001 {
22002   if (inst.cond != COND_ALWAYS)
22003     inst.pred_insn_type =  INSIDE_IT_INSN;
22004   else
22005     inst.pred_insn_type = OUTSIDE_PRED_INSN;
22006
22007   now_pred.state_handled = 0;
22008 }
22009
22010 /* IT state FSM handling function.  */
22011 /* MVE instructions and non-MVE instructions are handled differently because of
22012    the introduction of VPT blocks.
22013    Specifications say that any non-MVE instruction inside a VPT block is
22014    UNPREDICTABLE, with the exception of the BKPT instruction.  Whereas most MVE
22015    instructions are deemed to be UNPREDICTABLE if inside an IT block.  For the
22016    few exceptions we have MVE_UNPREDICABLE_INSN.
22017    The error messages provided depending on the different combinations possible
22018    are described in the cases below:
22019    For 'most' MVE instructions:
22020    1) In an IT block, with an IT code: syntax error
22021    2) In an IT block, with a VPT code: error: must be in a VPT block
22022    3) In an IT block, with no code: warning: UNPREDICTABLE
22023    4) In a VPT block, with an IT code: syntax error
22024    5) In a VPT block, with a VPT code: OK!
22025    6) In a VPT block, with no code: error: missing code
22026    7) Outside a pred block, with an IT code: error: syntax error
22027    8) Outside a pred block, with a VPT code: error: should be in a VPT block
22028    9) Outside a pred block, with no code: OK!
22029    For non-MVE instructions:
22030    10) In an IT block, with an IT code: OK!
22031    11) In an IT block, with a VPT code: syntax error
22032    12) In an IT block, with no code: error: missing code
22033    13) In a VPT block, with an IT code: error: should be in an IT block
22034    14) In a VPT block, with a VPT code: syntax error
22035    15) In a VPT block, with no code: UNPREDICTABLE
22036    16) Outside a pred block, with an IT code: error: should be in an IT block
22037    17) Outside a pred block, with a VPT code: syntax error
22038    18) Outside a pred block, with no code: OK!
22039  */
22040
22041
22042 static int
22043 handle_pred_state (void)
22044 {
22045   now_pred.state_handled = 1;
22046   now_pred.insn_cond = FALSE;
22047
22048   switch (now_pred.state)
22049     {
22050     case OUTSIDE_PRED_BLOCK:
22051       switch (inst.pred_insn_type)
22052         {
22053         case MVE_UNPREDICABLE_INSN:
22054         case MVE_OUTSIDE_PRED_INSN:
22055           if (inst.cond < COND_ALWAYS)
22056             {
22057               /* Case 7: Outside a pred block, with an IT code: error: syntax
22058                  error.  */
22059               inst.error = BAD_SYNTAX;
22060               return FAIL;
22061             }
22062           /* Case 9:  Outside a pred block, with no code: OK!  */
22063           break;
22064         case OUTSIDE_PRED_INSN:
22065           if (inst.cond > COND_ALWAYS)
22066             {
22067               /* Case 17:  Outside a pred block, with a VPT code: syntax error.
22068                */
22069               inst.error = BAD_SYNTAX;
22070               return FAIL;
22071             }
22072           /* Case 18: Outside a pred block, with no code: OK!  */
22073           break;
22074
22075         case INSIDE_VPT_INSN:
22076           /* Case 8: Outside a pred block, with a VPT code: error: should be in
22077              a VPT block.  */
22078           inst.error = BAD_OUT_VPT;
22079           return FAIL;
22080
22081         case INSIDE_IT_INSN:
22082         case INSIDE_IT_LAST_INSN:
22083           if (inst.cond < COND_ALWAYS)
22084             {
22085               /* Case 16: Outside a pred block, with an IT code: error: should
22086                  be in an IT block.  */
22087               if (thumb_mode == 0)
22088                 {
22089                   if (unified_syntax
22090                       && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
22091                     as_tsktsk (_("Warning: conditional outside an IT block"\
22092                                  " for Thumb."));
22093                 }
22094               else
22095                 {
22096                   if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
22097                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
22098                     {
22099                       /* Automatically generate the IT instruction.  */
22100                       new_automatic_it_block (inst.cond);
22101                       if (inst.pred_insn_type == INSIDE_IT_LAST_INSN)
22102                         close_automatic_it_block ();
22103                     }
22104                   else
22105                     {
22106                       inst.error = BAD_OUT_IT;
22107                       return FAIL;
22108                     }
22109                 }
22110               break;
22111             }
22112           else if (inst.cond > COND_ALWAYS)
22113             {
22114               /* Case 17: Outside a pred block, with a VPT code: syntax error.
22115                */
22116               inst.error = BAD_SYNTAX;
22117               return FAIL;
22118             }
22119           else
22120             gas_assert (0);
22121         case IF_INSIDE_IT_LAST_INSN:
22122         case NEUTRAL_IT_INSN:
22123           break;
22124
22125         case VPT_INSN:
22126           if (inst.cond != COND_ALWAYS)
22127             first_error (BAD_SYNTAX);
22128           now_pred.state = MANUAL_PRED_BLOCK;
22129           now_pred.block_length = 0;
22130           now_pred.type = VECTOR_PRED;
22131           now_pred.cc = 0;
22132           break;
22133         case IT_INSN:
22134           now_pred.state = MANUAL_PRED_BLOCK;
22135           now_pred.block_length = 0;
22136           now_pred.type = SCALAR_PRED;
22137           break;
22138         }
22139       break;
22140
22141     case AUTOMATIC_PRED_BLOCK:
22142       /* Three things may happen now:
22143          a) We should increment current it block size;
22144          b) We should close current it block (closing insn or 4 insns);
22145          c) We should close current it block and start a new one (due
22146          to incompatible conditions or
22147          4 insns-length block reached).  */
22148
22149       switch (inst.pred_insn_type)
22150         {
22151         case INSIDE_VPT_INSN:
22152         case VPT_INSN:
22153         case MVE_UNPREDICABLE_INSN:
22154         case MVE_OUTSIDE_PRED_INSN:
22155           gas_assert (0);
22156         case OUTSIDE_PRED_INSN:
22157           /* The closure of the block shall happen immediately,
22158              so any in_pred_block () call reports the block as closed.  */
22159           force_automatic_it_block_close ();
22160           break;
22161
22162         case INSIDE_IT_INSN:
22163         case INSIDE_IT_LAST_INSN:
22164         case IF_INSIDE_IT_LAST_INSN:
22165           now_pred.block_length++;
22166
22167           if (now_pred.block_length > 4
22168               || !now_pred_compatible (inst.cond))
22169             {
22170               force_automatic_it_block_close ();
22171               if (inst.pred_insn_type != IF_INSIDE_IT_LAST_INSN)
22172                 new_automatic_it_block (inst.cond);
22173             }
22174           else
22175             {
22176               now_pred.insn_cond = TRUE;
22177               now_pred_add_mask (inst.cond);
22178             }
22179
22180           if (now_pred.state == AUTOMATIC_PRED_BLOCK
22181               && (inst.pred_insn_type == INSIDE_IT_LAST_INSN
22182                   || inst.pred_insn_type == IF_INSIDE_IT_LAST_INSN))
22183             close_automatic_it_block ();
22184           break;
22185
22186         case NEUTRAL_IT_INSN:
22187           now_pred.block_length++;
22188           now_pred.insn_cond = TRUE;
22189
22190           if (now_pred.block_length > 4)
22191             force_automatic_it_block_close ();
22192           else
22193             now_pred_add_mask (now_pred.cc & 1);
22194           break;
22195
22196         case IT_INSN:
22197           close_automatic_it_block ();
22198           now_pred.state = MANUAL_PRED_BLOCK;
22199           break;
22200         }
22201       break;
22202
22203     case MANUAL_PRED_BLOCK:
22204       {
22205         int cond, is_last;
22206         if (now_pred.type == SCALAR_PRED)
22207           {
22208             /* Check conditional suffixes.  */
22209             cond = now_pred.cc ^ ((now_pred.mask >> 4) & 1) ^ 1;
22210             now_pred.mask <<= 1;
22211             now_pred.mask &= 0x1f;
22212             is_last = (now_pred.mask == 0x10);
22213           }
22214         else
22215           {
22216             now_pred.cc ^= (now_pred.mask >> 4);
22217             cond = now_pred.cc + 0xf;
22218             now_pred.mask <<= 1;
22219             now_pred.mask &= 0x1f;
22220             is_last = now_pred.mask == 0x10;
22221           }
22222         now_pred.insn_cond = TRUE;
22223
22224         switch (inst.pred_insn_type)
22225           {
22226           case OUTSIDE_PRED_INSN:
22227             if (now_pred.type == SCALAR_PRED)
22228               {
22229                 if (inst.cond == COND_ALWAYS)
22230                   {
22231                     /* Case 12: In an IT block, with no code: error: missing
22232                        code.  */
22233                     inst.error = BAD_NOT_IT;
22234                     return FAIL;
22235                   }
22236                 else if (inst.cond > COND_ALWAYS)
22237                   {
22238                     /* Case 11: In an IT block, with a VPT code: syntax error.
22239                      */
22240                     inst.error = BAD_SYNTAX;
22241                     return FAIL;
22242                   }
22243                 else if (thumb_mode)
22244                   {
22245                     /* This is for some special cases where a non-MVE
22246                        instruction is not allowed in an IT block, such as cbz,
22247                        but are put into one with a condition code.
22248                        You could argue this should be a syntax error, but we
22249                        gave the 'not allowed in IT block' diagnostic in the
22250                        past so we will keep doing so.  */
22251                     inst.error = BAD_NOT_IT;
22252                     return FAIL;
22253                   }
22254                 break;
22255               }
22256             else
22257               {
22258                 /* Case 15: In a VPT block, with no code: UNPREDICTABLE.  */
22259                 as_tsktsk (MVE_NOT_VPT);
22260                 return SUCCESS;
22261               }
22262           case MVE_OUTSIDE_PRED_INSN:
22263             if (now_pred.type == SCALAR_PRED)
22264               {
22265                 if (inst.cond == COND_ALWAYS)
22266                   {
22267                     /* Case 3: In an IT block, with no code: warning:
22268                        UNPREDICTABLE.  */
22269                     as_tsktsk (MVE_NOT_IT);
22270                     return SUCCESS;
22271                   }
22272                 else if (inst.cond < COND_ALWAYS)
22273                   {
22274                     /* Case 1: In an IT block, with an IT code: syntax error.
22275                      */
22276                     inst.error = BAD_SYNTAX;
22277                     return FAIL;
22278                   }
22279                 else
22280                   gas_assert (0);
22281               }
22282             else
22283               {
22284                 if (inst.cond < COND_ALWAYS)
22285                   {
22286                     /* Case 4: In a VPT block, with an IT code: syntax error.
22287                      */
22288                     inst.error = BAD_SYNTAX;
22289                     return FAIL;
22290                   }
22291                 else if (inst.cond == COND_ALWAYS)
22292                   {
22293                     /* Case 6: In a VPT block, with no code: error: missing
22294                        code.  */
22295                     inst.error = BAD_NOT_VPT;
22296                     return FAIL;
22297                   }
22298                 else
22299                   {
22300                     gas_assert (0);
22301                   }
22302               }
22303           case MVE_UNPREDICABLE_INSN:
22304             as_tsktsk (now_pred.type == SCALAR_PRED ? MVE_NOT_IT : MVE_NOT_VPT);
22305             return SUCCESS;
22306           case INSIDE_IT_INSN:
22307             if (inst.cond > COND_ALWAYS)
22308               {
22309                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
22310                 /* Case 14: In a VPT block, with a VPT code: syntax error.  */
22311                 inst.error = BAD_SYNTAX;
22312                 return FAIL;
22313               }
22314             else if (now_pred.type == SCALAR_PRED)
22315               {
22316                 /* Case 10: In an IT block, with an IT code: OK!  */
22317                 if (cond != inst.cond)
22318                   {
22319                     inst.error = now_pred.type == SCALAR_PRED ? BAD_IT_COND :
22320                       BAD_VPT_COND;
22321                     return FAIL;
22322                   }
22323               }
22324             else
22325               {
22326                 /* Case 13: In a VPT block, with an IT code: error: should be
22327                    in an IT block.  */
22328                 inst.error = BAD_OUT_IT;
22329                 return FAIL;
22330               }
22331             break;
22332
22333           case INSIDE_VPT_INSN:
22334             if (now_pred.type == SCALAR_PRED)
22335               {
22336                 /* Case 2: In an IT block, with a VPT code: error: must be in a
22337                    VPT block.  */
22338                 inst.error = BAD_OUT_VPT;
22339                 return FAIL;
22340               }
22341             /* Case 5:  In a VPT block, with a VPT code: OK!  */
22342             else if (cond != inst.cond)
22343               {
22344                 inst.error = BAD_VPT_COND;
22345                 return FAIL;
22346               }
22347             break;
22348           case INSIDE_IT_LAST_INSN:
22349           case IF_INSIDE_IT_LAST_INSN:
22350             if (now_pred.type == VECTOR_PRED || inst.cond > COND_ALWAYS)
22351               {
22352                 /* Case 4: In a VPT block, with an IT code: syntax error.  */
22353                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
22354                 inst.error = BAD_SYNTAX;
22355                 return FAIL;
22356               }
22357             else if (cond != inst.cond)
22358               {
22359                 inst.error = BAD_IT_COND;
22360                 return FAIL;
22361               }
22362             if (!is_last)
22363               {
22364                 inst.error = BAD_BRANCH;
22365                 return FAIL;
22366               }
22367             break;
22368
22369           case NEUTRAL_IT_INSN:
22370             /* The BKPT instruction is unconditional even in a IT or VPT
22371                block.  */
22372             break;
22373
22374           case IT_INSN:
22375             if (now_pred.type == SCALAR_PRED)
22376               {
22377                 inst.error = BAD_IT_IT;
22378                 return FAIL;
22379               }
22380             /* fall through.  */
22381           case VPT_INSN:
22382             if (inst.cond == COND_ALWAYS)
22383               {
22384                 /* Executing a VPT/VPST instruction inside an IT block or a
22385                    VPT/VPST/IT instruction inside a VPT block is UNPREDICTABLE.
22386                  */
22387                 if (now_pred.type == SCALAR_PRED)
22388                   as_tsktsk (MVE_NOT_IT);
22389                 else
22390                   as_tsktsk (MVE_NOT_VPT);
22391                 return SUCCESS;
22392               }
22393             else
22394               {
22395                 /* VPT/VPST do not accept condition codes.  */
22396                 inst.error = BAD_SYNTAX;
22397                 return FAIL;
22398               }
22399           }
22400         }
22401       break;
22402     }
22403
22404   return SUCCESS;
22405 }
22406
22407 struct depr_insn_mask
22408 {
22409   unsigned long pattern;
22410   unsigned long mask;
22411   const char* description;
22412 };
22413
22414 /* List of 16-bit instruction patterns deprecated in an IT block in
22415    ARMv8.  */
22416 static const struct depr_insn_mask depr_it_insns[] = {
22417   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
22418   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
22419   { 0xa000, 0xb800, N_("ADR") },
22420   { 0x4800, 0xf800, N_("Literal loads") },
22421   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
22422   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
22423   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
22424      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
22425   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
22426   { 0, 0, NULL }
22427 };
22428
22429 static void
22430 it_fsm_post_encode (void)
22431 {
22432   int is_last;
22433
22434   if (!now_pred.state_handled)
22435     handle_pred_state ();
22436
22437   if (now_pred.insn_cond
22438       && !now_pred.warn_deprecated
22439       && warn_on_deprecated
22440       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8)
22441       && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_m))
22442     {
22443       if (inst.instruction >= 0x10000)
22444         {
22445           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
22446                      "performance deprecated in ARMv8-A and ARMv8-R"));
22447           now_pred.warn_deprecated = TRUE;
22448         }
22449       else
22450         {
22451           const struct depr_insn_mask *p = depr_it_insns;
22452
22453           while (p->mask != 0)
22454             {
22455               if ((inst.instruction & p->mask) == p->pattern)
22456                 {
22457                   as_tsktsk (_("IT blocks containing 16-bit Thumb "
22458                                "instructions of the following class are "
22459                                "performance deprecated in ARMv8-A and "
22460                                "ARMv8-R: %s"), p->description);
22461                   now_pred.warn_deprecated = TRUE;
22462                   break;
22463                 }
22464
22465               ++p;
22466             }
22467         }
22468
22469       if (now_pred.block_length > 1)
22470         {
22471           as_tsktsk (_("IT blocks containing more than one conditional "
22472                      "instruction are performance deprecated in ARMv8-A and "
22473                      "ARMv8-R"));
22474           now_pred.warn_deprecated = TRUE;
22475         }
22476     }
22477
22478     is_last = (now_pred.mask == 0x10);
22479     if (is_last)
22480       {
22481         now_pred.state = OUTSIDE_PRED_BLOCK;
22482         now_pred.mask = 0;
22483       }
22484 }
22485
22486 static void
22487 force_automatic_it_block_close (void)
22488 {
22489   if (now_pred.state == AUTOMATIC_PRED_BLOCK)
22490     {
22491       close_automatic_it_block ();
22492       now_pred.state = OUTSIDE_PRED_BLOCK;
22493       now_pred.mask = 0;
22494     }
22495 }
22496
22497 static int
22498 in_pred_block (void)
22499 {
22500   if (!now_pred.state_handled)
22501     handle_pred_state ();
22502
22503   return now_pred.state != OUTSIDE_PRED_BLOCK;
22504 }
22505
22506 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
22507    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
22508    here, hence the "known" in the function name.  */
22509
22510 static bfd_boolean
22511 known_t32_only_insn (const struct asm_opcode *opcode)
22512 {
22513   /* Original Thumb-1 wide instruction.  */
22514   if (opcode->tencode == do_t_blx
22515       || opcode->tencode == do_t_branch23
22516       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
22517       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
22518     return TRUE;
22519
22520   /* Wide-only instruction added to ARMv8-M Baseline.  */
22521   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
22522       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
22523       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
22524       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
22525     return TRUE;
22526
22527   return FALSE;
22528 }
22529
22530 /* Whether wide instruction variant can be used if available for a valid OPCODE
22531    in ARCH.  */
22532
22533 static bfd_boolean
22534 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
22535 {
22536   if (known_t32_only_insn (opcode))
22537     return TRUE;
22538
22539   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
22540      of variant T3 of B.W is checked in do_t_branch.  */
22541   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
22542       && opcode->tencode == do_t_branch)
22543     return TRUE;
22544
22545   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
22546   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
22547       && opcode->tencode == do_t_mov_cmp
22548       /* Make sure CMP instruction is not affected.  */
22549       && opcode->aencode == do_mov)
22550     return TRUE;
22551
22552   /* Wide instruction variants of all instructions with narrow *and* wide
22553      variants become available with ARMv6t2.  Other opcodes are either
22554      narrow-only or wide-only and are thus available if OPCODE is valid.  */
22555   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
22556     return TRUE;
22557
22558   /* OPCODE with narrow only instruction variant or wide variant not
22559      available.  */
22560   return FALSE;
22561 }
22562
22563 void
22564 md_assemble (char *str)
22565 {
22566   char *p = str;
22567   const struct asm_opcode * opcode;
22568
22569   /* Align the previous label if needed.  */
22570   if (last_label_seen != NULL)
22571     {
22572       symbol_set_frag (last_label_seen, frag_now);
22573       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
22574       S_SET_SEGMENT (last_label_seen, now_seg);
22575     }
22576
22577   memset (&inst, '\0', sizeof (inst));
22578   int r;
22579   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
22580     inst.relocs[r].type = BFD_RELOC_UNUSED;
22581
22582   opcode = opcode_lookup (&p);
22583   if (!opcode)
22584     {
22585       /* It wasn't an instruction, but it might be a register alias of
22586          the form alias .req reg, or a Neon .dn/.qn directive.  */
22587       if (! create_register_alias (str, p)
22588           && ! create_neon_reg_alias (str, p))
22589         as_bad (_("bad instruction `%s'"), str);
22590
22591       return;
22592     }
22593
22594   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
22595     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
22596
22597   /* The value which unconditional instructions should have in place of the
22598      condition field.  */
22599   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
22600
22601   if (thumb_mode)
22602     {
22603       arm_feature_set variant;
22604
22605       variant = cpu_variant;
22606       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
22607       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
22608         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
22609       /* Check that this instruction is supported for this CPU.  */
22610       if (!opcode->tvariant
22611           || (thumb_mode == 1
22612               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
22613         {
22614           if (opcode->tencode == do_t_swi)
22615             as_bad (_("SVC is not permitted on this architecture"));
22616           else
22617             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
22618           return;
22619         }
22620       if (inst.cond != COND_ALWAYS && !unified_syntax
22621           && opcode->tencode != do_t_branch)
22622         {
22623           as_bad (_("Thumb does not support conditional execution"));
22624           return;
22625         }
22626
22627       /* Two things are addressed here:
22628          1) Implicit require narrow instructions on Thumb-1.
22629             This avoids relaxation accidentally introducing Thumb-2
22630             instructions.
22631          2) Reject wide instructions in non Thumb-2 cores.
22632
22633          Only instructions with narrow and wide variants need to be handled
22634          but selecting all non wide-only instructions is easier.  */
22635       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
22636           && !t32_insn_ok (variant, opcode))
22637         {
22638           if (inst.size_req == 0)
22639             inst.size_req = 2;
22640           else if (inst.size_req == 4)
22641             {
22642               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
22643                 as_bad (_("selected processor does not support 32bit wide "
22644                           "variant of instruction `%s'"), str);
22645               else
22646                 as_bad (_("selected processor does not support `%s' in "
22647                           "Thumb-2 mode"), str);
22648               return;
22649             }
22650         }
22651
22652       inst.instruction = opcode->tvalue;
22653
22654       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
22655         {
22656           /* Prepare the pred_insn_type for those encodings that don't set
22657              it.  */
22658           it_fsm_pre_encode ();
22659
22660           opcode->tencode ();
22661
22662           it_fsm_post_encode ();
22663         }
22664
22665       if (!(inst.error || inst.relax))
22666         {
22667           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
22668           inst.size = (inst.instruction > 0xffff ? 4 : 2);
22669           if (inst.size_req && inst.size_req != inst.size)
22670             {
22671               as_bad (_("cannot honor width suffix -- `%s'"), str);
22672               return;
22673             }
22674         }
22675
22676       /* Something has gone badly wrong if we try to relax a fixed size
22677          instruction.  */
22678       gas_assert (inst.size_req == 0 || !inst.relax);
22679
22680       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
22681                               *opcode->tvariant);
22682       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
22683          set those bits when Thumb-2 32-bit instructions are seen.  The impact
22684          of relaxable instructions will be considered later after we finish all
22685          relaxation.  */
22686       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
22687         variant = arm_arch_none;
22688       else
22689         variant = cpu_variant;
22690       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
22691         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
22692                                 arm_ext_v6t2);
22693
22694       check_neon_suffixes;
22695
22696       if (!inst.error)
22697         {
22698           mapping_state (MAP_THUMB);
22699         }
22700     }
22701   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
22702     {
22703       bfd_boolean is_bx;
22704
22705       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
22706       is_bx = (opcode->aencode == do_bx);
22707
22708       /* Check that this instruction is supported for this CPU.  */
22709       if (!(is_bx && fix_v4bx)
22710           && !(opcode->avariant &&
22711                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
22712         {
22713           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
22714           return;
22715         }
22716       if (inst.size_req)
22717         {
22718           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
22719           return;
22720         }
22721
22722       inst.instruction = opcode->avalue;
22723       if (opcode->tag == OT_unconditionalF)
22724         inst.instruction |= 0xFU << 28;
22725       else
22726         inst.instruction |= inst.cond << 28;
22727       inst.size = INSN_SIZE;
22728       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
22729         {
22730           it_fsm_pre_encode ();
22731           opcode->aencode ();
22732           it_fsm_post_encode ();
22733         }
22734       /* Arm mode bx is marked as both v4T and v5 because it's still required
22735          on a hypothetical non-thumb v5 core.  */
22736       if (is_bx)
22737         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
22738       else
22739         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
22740                                 *opcode->avariant);
22741
22742       check_neon_suffixes;
22743
22744       if (!inst.error)
22745         {
22746           mapping_state (MAP_ARM);
22747         }
22748     }
22749   else
22750     {
22751       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
22752                 "-- `%s'"), str);
22753       return;
22754     }
22755   output_inst (str);
22756 }
22757
22758 static void
22759 check_pred_blocks_finished (void)
22760 {
22761 #ifdef OBJ_ELF
22762   asection *sect;
22763
22764   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
22765     if (seg_info (sect)->tc_segment_info_data.current_pred.state
22766         == MANUAL_PRED_BLOCK)
22767       {
22768         if (now_pred.type == SCALAR_PRED)
22769           as_warn (_("section '%s' finished with an open IT block."),
22770                    sect->name);
22771         else
22772           as_warn (_("section '%s' finished with an open VPT/VPST block."),
22773                    sect->name);
22774       }
22775 #else
22776   if (now_pred.state == MANUAL_PRED_BLOCK)
22777     {
22778       if (now_pred.type == SCALAR_PRED)
22779        as_warn (_("file finished with an open IT block."));
22780       else
22781         as_warn (_("file finished with an open VPT/VPST block."));
22782     }
22783 #endif
22784 }
22785
22786 /* Various frobbings of labels and their addresses.  */
22787
22788 void
22789 arm_start_line_hook (void)
22790 {
22791   last_label_seen = NULL;
22792 }
22793
22794 void
22795 arm_frob_label (symbolS * sym)
22796 {
22797   last_label_seen = sym;
22798
22799   ARM_SET_THUMB (sym, thumb_mode);
22800
22801 #if defined OBJ_COFF || defined OBJ_ELF
22802   ARM_SET_INTERWORK (sym, support_interwork);
22803 #endif
22804
22805   force_automatic_it_block_close ();
22806
22807   /* Note - do not allow local symbols (.Lxxx) to be labelled
22808      as Thumb functions.  This is because these labels, whilst
22809      they exist inside Thumb code, are not the entry points for
22810      possible ARM->Thumb calls.  Also, these labels can be used
22811      as part of a computed goto or switch statement.  eg gcc
22812      can generate code that looks like this:
22813
22814                 ldr  r2, [pc, .Laaa]
22815                 lsl  r3, r3, #2
22816                 ldr  r2, [r3, r2]
22817                 mov  pc, r2
22818
22819        .Lbbb:  .word .Lxxx
22820        .Lccc:  .word .Lyyy
22821        ..etc...
22822        .Laaa:   .word Lbbb
22823
22824      The first instruction loads the address of the jump table.
22825      The second instruction converts a table index into a byte offset.
22826      The third instruction gets the jump address out of the table.
22827      The fourth instruction performs the jump.
22828
22829      If the address stored at .Laaa is that of a symbol which has the
22830      Thumb_Func bit set, then the linker will arrange for this address
22831      to have the bottom bit set, which in turn would mean that the
22832      address computation performed by the third instruction would end
22833      up with the bottom bit set.  Since the ARM is capable of unaligned
22834      word loads, the instruction would then load the incorrect address
22835      out of the jump table, and chaos would ensue.  */
22836   if (label_is_thumb_function_name
22837       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
22838       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
22839     {
22840       /* When the address of a Thumb function is taken the bottom
22841          bit of that address should be set.  This will allow
22842          interworking between Arm and Thumb functions to work
22843          correctly.  */
22844
22845       THUMB_SET_FUNC (sym, 1);
22846
22847       label_is_thumb_function_name = FALSE;
22848     }
22849
22850   dwarf2_emit_label (sym);
22851 }
22852
22853 bfd_boolean
22854 arm_data_in_code (void)
22855 {
22856   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
22857     {
22858       *input_line_pointer = '/';
22859       input_line_pointer += 5;
22860       *input_line_pointer = 0;
22861       return TRUE;
22862     }
22863
22864   return FALSE;
22865 }
22866
22867 char *
22868 arm_canonicalize_symbol_name (char * name)
22869 {
22870   int len;
22871
22872   if (thumb_mode && (len = strlen (name)) > 5
22873       && streq (name + len - 5, "/data"))
22874     *(name + len - 5) = 0;
22875
22876   return name;
22877 }
22878 \f
22879 /* Table of all register names defined by default.  The user can
22880    define additional names with .req.  Note that all register names
22881    should appear in both upper and lowercase variants.  Some registers
22882    also have mixed-case names.  */
22883
22884 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
22885 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
22886 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
22887 #define REGSET(p,t) \
22888   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
22889   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
22890   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
22891   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
22892 #define REGSETH(p,t) \
22893   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
22894   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
22895   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
22896   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
22897 #define REGSET2(p,t) \
22898   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
22899   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
22900   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
22901   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
22902 #define SPLRBANK(base,bank,t) \
22903   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
22904   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
22905   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
22906   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
22907   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
22908   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
22909
22910 static const struct reg_entry reg_names[] =
22911 {
22912   /* ARM integer registers.  */
22913   REGSET(r, RN), REGSET(R, RN),
22914
22915   /* ATPCS synonyms.  */
22916   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
22917   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
22918   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
22919
22920   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
22921   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
22922   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
22923
22924   /* Well-known aliases.  */
22925   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
22926   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
22927
22928   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
22929   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
22930
22931   /* Defining the new Zero register from ARMv8.1-M.  */
22932   REGDEF(zr,15,ZR),
22933   REGDEF(ZR,15,ZR),
22934
22935   /* Coprocessor numbers.  */
22936   REGSET(p, CP), REGSET(P, CP),
22937
22938   /* Coprocessor register numbers.  The "cr" variants are for backward
22939      compatibility.  */
22940   REGSET(c,  CN), REGSET(C, CN),
22941   REGSET(cr, CN), REGSET(CR, CN),
22942
22943   /* ARM banked registers.  */
22944   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
22945   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
22946   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
22947   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
22948   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
22949   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
22950   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
22951
22952   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
22953   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
22954   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
22955   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
22956   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
22957   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
22958   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
22959   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
22960
22961   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
22962   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
22963   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
22964   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
22965   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
22966   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
22967   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
22968   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
22969   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
22970
22971   /* FPA registers.  */
22972   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
22973   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
22974
22975   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
22976   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
22977
22978   /* VFP SP registers.  */
22979   REGSET(s,VFS),  REGSET(S,VFS),
22980   REGSETH(s,VFS), REGSETH(S,VFS),
22981
22982   /* VFP DP Registers.  */
22983   REGSET(d,VFD),  REGSET(D,VFD),
22984   /* Extra Neon DP registers.  */
22985   REGSETH(d,VFD), REGSETH(D,VFD),
22986
22987   /* Neon QP registers.  */
22988   REGSET2(q,NQ),  REGSET2(Q,NQ),
22989
22990   /* VFP control registers.  */
22991   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
22992   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
22993   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
22994   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
22995   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
22996   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
22997   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
22998   REGDEF(fpscr_nzcvqc,2,VFC), REGDEF(FPSCR_nzcvqc,2,VFC),
22999   REGDEF(vpr,12,VFC), REGDEF(VPR,12,VFC),
23000   REGDEF(fpcxt_ns,14,VFC), REGDEF(FPCXT_NS,14,VFC),
23001   REGDEF(fpcxt_s,15,VFC), REGDEF(FPCXT_S,15,VFC),
23002
23003   /* Maverick DSP coprocessor registers.  */
23004   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
23005   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
23006
23007   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
23008   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
23009   REGDEF(dspsc,0,DSPSC),
23010
23011   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
23012   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
23013   REGDEF(DSPSC,0,DSPSC),
23014
23015   /* iWMMXt data registers - p0, c0-15.  */
23016   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
23017
23018   /* iWMMXt control registers - p1, c0-3.  */
23019   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
23020   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
23021   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
23022   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
23023
23024   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
23025   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
23026   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
23027   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
23028   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
23029
23030   /* XScale accumulator registers.  */
23031   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
23032 };
23033 #undef REGDEF
23034 #undef REGNUM
23035 #undef REGSET
23036
23037 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
23038    within psr_required_here.  */
23039 static const struct asm_psr psrs[] =
23040 {
23041   /* Backward compatibility notation.  Note that "all" is no longer
23042      truly all possible PSR bits.  */
23043   {"all",  PSR_c | PSR_f},
23044   {"flg",  PSR_f},
23045   {"ctl",  PSR_c},
23046
23047   /* Individual flags.  */
23048   {"f",    PSR_f},
23049   {"c",    PSR_c},
23050   {"x",    PSR_x},
23051   {"s",    PSR_s},
23052
23053   /* Combinations of flags.  */
23054   {"fs",   PSR_f | PSR_s},
23055   {"fx",   PSR_f | PSR_x},
23056   {"fc",   PSR_f | PSR_c},
23057   {"sf",   PSR_s | PSR_f},
23058   {"sx",   PSR_s | PSR_x},
23059   {"sc",   PSR_s | PSR_c},
23060   {"xf",   PSR_x | PSR_f},
23061   {"xs",   PSR_x | PSR_s},
23062   {"xc",   PSR_x | PSR_c},
23063   {"cf",   PSR_c | PSR_f},
23064   {"cs",   PSR_c | PSR_s},
23065   {"cx",   PSR_c | PSR_x},
23066   {"fsx",  PSR_f | PSR_s | PSR_x},
23067   {"fsc",  PSR_f | PSR_s | PSR_c},
23068   {"fxs",  PSR_f | PSR_x | PSR_s},
23069   {"fxc",  PSR_f | PSR_x | PSR_c},
23070   {"fcs",  PSR_f | PSR_c | PSR_s},
23071   {"fcx",  PSR_f | PSR_c | PSR_x},
23072   {"sfx",  PSR_s | PSR_f | PSR_x},
23073   {"sfc",  PSR_s | PSR_f | PSR_c},
23074   {"sxf",  PSR_s | PSR_x | PSR_f},
23075   {"sxc",  PSR_s | PSR_x | PSR_c},
23076   {"scf",  PSR_s | PSR_c | PSR_f},
23077   {"scx",  PSR_s | PSR_c | PSR_x},
23078   {"xfs",  PSR_x | PSR_f | PSR_s},
23079   {"xfc",  PSR_x | PSR_f | PSR_c},
23080   {"xsf",  PSR_x | PSR_s | PSR_f},
23081   {"xsc",  PSR_x | PSR_s | PSR_c},
23082   {"xcf",  PSR_x | PSR_c | PSR_f},
23083   {"xcs",  PSR_x | PSR_c | PSR_s},
23084   {"cfs",  PSR_c | PSR_f | PSR_s},
23085   {"cfx",  PSR_c | PSR_f | PSR_x},
23086   {"csf",  PSR_c | PSR_s | PSR_f},
23087   {"csx",  PSR_c | PSR_s | PSR_x},
23088   {"cxf",  PSR_c | PSR_x | PSR_f},
23089   {"cxs",  PSR_c | PSR_x | PSR_s},
23090   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
23091   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
23092   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
23093   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
23094   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
23095   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
23096   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
23097   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
23098   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
23099   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
23100   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
23101   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
23102   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
23103   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
23104   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
23105   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
23106   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
23107   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
23108   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
23109   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
23110   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
23111   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
23112   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
23113   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
23114 };
23115
23116 /* Table of V7M psr names.  */
23117 static const struct asm_psr v7m_psrs[] =
23118 {
23119   {"apsr",         0x0 }, {"APSR",         0x0 },
23120   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
23121   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
23122   {"psr",          0x3 }, {"PSR",          0x3 },
23123   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
23124   {"ipsr",         0x5 }, {"IPSR",         0x5 },
23125   {"epsr",         0x6 }, {"EPSR",         0x6 },
23126   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
23127   {"msp",          0x8 }, {"MSP",          0x8 },
23128   {"psp",          0x9 }, {"PSP",          0x9 },
23129   {"msplim",       0xa }, {"MSPLIM",       0xa },
23130   {"psplim",       0xb }, {"PSPLIM",       0xb },
23131   {"primask",      0x10}, {"PRIMASK",      0x10},
23132   {"basepri",      0x11}, {"BASEPRI",      0x11},
23133   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
23134   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
23135   {"control",      0x14}, {"CONTROL",      0x14},
23136   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
23137   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
23138   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
23139   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
23140   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
23141   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
23142   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
23143   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
23144   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
23145 };
23146
23147 /* Table of all shift-in-operand names.  */
23148 static const struct asm_shift_name shift_names [] =
23149 {
23150   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
23151   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
23152   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
23153   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
23154   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
23155   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX },
23156   { "uxtw", SHIFT_UXTW}, { "UXTW", SHIFT_UXTW}
23157 };
23158
23159 /* Table of all explicit relocation names.  */
23160 #ifdef OBJ_ELF
23161 static struct reloc_entry reloc_names[] =
23162 {
23163   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
23164   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
23165   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
23166   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
23167   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
23168   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
23169   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
23170   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
23171   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
23172   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
23173   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
23174   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
23175   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
23176         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
23177   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
23178         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
23179   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
23180         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ},
23181   { "gotfuncdesc", BFD_RELOC_ARM_GOTFUNCDESC },
23182         { "GOTFUNCDESC", BFD_RELOC_ARM_GOTFUNCDESC },
23183   { "gotofffuncdesc", BFD_RELOC_ARM_GOTOFFFUNCDESC },
23184         { "GOTOFFFUNCDESC", BFD_RELOC_ARM_GOTOFFFUNCDESC },
23185   { "funcdesc", BFD_RELOC_ARM_FUNCDESC },
23186         { "FUNCDESC", BFD_RELOC_ARM_FUNCDESC },
23187    { "tlsgd_fdpic", BFD_RELOC_ARM_TLS_GD32_FDPIC },      { "TLSGD_FDPIC", BFD_RELOC_ARM_TLS_GD32_FDPIC },
23188    { "tlsldm_fdpic", BFD_RELOC_ARM_TLS_LDM32_FDPIC },    { "TLSLDM_FDPIC", BFD_RELOC_ARM_TLS_LDM32_FDPIC },
23189    { "gottpoff_fdpic", BFD_RELOC_ARM_TLS_IE32_FDPIC },   { "GOTTPOFF_FDIC", BFD_RELOC_ARM_TLS_IE32_FDPIC },
23190 };
23191 #endif
23192
23193 /* Table of all conditional affixes.  */
23194 static const struct asm_cond conds[] =
23195 {
23196   {"eq", 0x0},
23197   {"ne", 0x1},
23198   {"cs", 0x2}, {"hs", 0x2},
23199   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
23200   {"mi", 0x4},
23201   {"pl", 0x5},
23202   {"vs", 0x6},
23203   {"vc", 0x7},
23204   {"hi", 0x8},
23205   {"ls", 0x9},
23206   {"ge", 0xa},
23207   {"lt", 0xb},
23208   {"gt", 0xc},
23209   {"le", 0xd},
23210   {"al", 0xe}
23211 };
23212 static const struct asm_cond vconds[] =
23213 {
23214     {"t", 0xf},
23215     {"e", 0x10}
23216 };
23217
23218 #define UL_BARRIER(L,U,CODE,FEAT) \
23219   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
23220   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
23221
23222 static struct asm_barrier_opt barrier_opt_names[] =
23223 {
23224   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
23225   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
23226   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
23227   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
23228   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
23229   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
23230   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
23231   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
23232   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
23233   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
23234   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
23235   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
23236   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
23237   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
23238   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
23239   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
23240 };
23241
23242 #undef UL_BARRIER
23243
23244 /* Table of ARM-format instructions.    */
23245
23246 /* Macros for gluing together operand strings.  N.B. In all cases
23247    other than OPS0, the trailing OP_stop comes from default
23248    zero-initialization of the unspecified elements of the array.  */
23249 #define OPS0()            { OP_stop, }
23250 #define OPS1(a)           { OP_##a, }
23251 #define OPS2(a,b)         { OP_##a,OP_##b, }
23252 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
23253 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
23254 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
23255 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
23256
23257 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
23258    This is useful when mixing operands for ARM and THUMB, i.e. using the
23259    MIX_ARM_THUMB_OPERANDS macro.
23260    In order to use these macros, prefix the number of operands with _
23261    e.g. _3.  */
23262 #define OPS_1(a)           { a, }
23263 #define OPS_2(a,b)         { a,b, }
23264 #define OPS_3(a,b,c)       { a,b,c, }
23265 #define OPS_4(a,b,c,d)     { a,b,c,d, }
23266 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
23267 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
23268
23269 /* These macros abstract out the exact format of the mnemonic table and
23270    save some repeated characters.  */
23271
23272 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
23273 #define TxCE(mnem, op, top, nops, ops, ae, te) \
23274   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
23275     THUMB_VARIANT, do_##ae, do_##te, 0 }
23276
23277 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
23278    a T_MNEM_xyz enumerator.  */
23279 #define TCE(mnem, aop, top, nops, ops, ae, te) \
23280       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
23281 #define tCE(mnem, aop, top, nops, ops, ae, te) \
23282       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23283
23284 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
23285    infix after the third character.  */
23286 #define TxC3(mnem, op, top, nops, ops, ae, te) \
23287   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
23288     THUMB_VARIANT, do_##ae, do_##te, 0 }
23289 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
23290   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
23291     THUMB_VARIANT, do_##ae, do_##te, 0 }
23292 #define TC3(mnem, aop, top, nops, ops, ae, te) \
23293       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
23294 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
23295       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
23296 #define tC3(mnem, aop, top, nops, ops, ae, te) \
23297       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23298 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
23299       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23300
23301 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
23302    field is still 0xE.  Many of the Thumb variants can be executed
23303    conditionally, so this is checked separately.  */
23304 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
23305   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
23306     THUMB_VARIANT, do_##ae, do_##te, 0 }
23307
23308 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
23309    Used by mnemonics that have very minimal differences in the encoding for
23310    ARM and Thumb variants and can be handled in a common function.  */
23311 #define TUEc(mnem, op, top, nops, ops, en) \
23312   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
23313     THUMB_VARIANT, do_##en, do_##en, 0 }
23314
23315 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
23316    condition code field.  */
23317 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
23318   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
23319     THUMB_VARIANT, do_##ae, do_##te, 0 }
23320
23321 /* ARM-only variants of all the above.  */
23322 #define CE(mnem,  op, nops, ops, ae)    \
23323   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23324
23325 #define C3(mnem, op, nops, ops, ae)     \
23326   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23327
23328 /* Thumb-only variants of TCE and TUE.  */
23329 #define ToC(mnem, top, nops, ops, te) \
23330   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
23331     do_##te, 0 }
23332
23333 #define ToU(mnem, top, nops, ops, te) \
23334   { mnem, OPS##nops ops, OT_unconditional, 0x0, 0x##top, 0, THUMB_VARIANT, \
23335     NULL, do_##te, 0 }
23336
23337 /* T_MNEM_xyz enumerator variants of ToC.  */
23338 #define toC(mnem, top, nops, ops, te) \
23339   { mnem, OPS##nops ops, OT_csuffix, 0x0, T_MNEM##top, 0, THUMB_VARIANT, NULL, \
23340     do_##te, 0 }
23341
23342 /* T_MNEM_xyz enumerator variants of ToU.  */
23343 #define toU(mnem, top, nops, ops, te) \
23344   { mnem, OPS##nops ops, OT_unconditional, 0x0, T_MNEM##top, 0, THUMB_VARIANT, \
23345     NULL, do_##te, 0 }
23346
23347 /* Legacy mnemonics that always have conditional infix after the third
23348    character.  */
23349 #define CL(mnem, op, nops, ops, ae)     \
23350   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
23351     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23352
23353 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
23354 #define cCE(mnem,  op, nops, ops, ae)   \
23355   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23356
23357 /* mov instructions that are shared between coprocessor and MVE.  */
23358 #define mcCE(mnem,  op, nops, ops, ae)  \
23359   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##ae, 0 }
23360
23361 /* Legacy coprocessor instructions where conditional infix and conditional
23362    suffix are ambiguous.  For consistency this includes all FPA instructions,
23363    not just the potentially ambiguous ones.  */
23364 #define cCL(mnem, op, nops, ops, ae)    \
23365   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
23366     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23367
23368 /* Coprocessor, takes either a suffix or a position-3 infix
23369    (for an FPA corner case). */
23370 #define C3E(mnem, op, nops, ops, ae) \
23371   { mnem, OPS##nops ops, OT_csuf_or_in3, \
23372     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23373
23374 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
23375   { m1 #m2 m3, OPS##nops ops, \
23376     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
23377     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23378
23379 #define CM(m1, m2, op, nops, ops, ae)   \
23380   xCM_ (m1,   , m2, op, nops, ops, ae), \
23381   xCM_ (m1, eq, m2, op, nops, ops, ae), \
23382   xCM_ (m1, ne, m2, op, nops, ops, ae), \
23383   xCM_ (m1, cs, m2, op, nops, ops, ae), \
23384   xCM_ (m1, hs, m2, op, nops, ops, ae), \
23385   xCM_ (m1, cc, m2, op, nops, ops, ae), \
23386   xCM_ (m1, ul, m2, op, nops, ops, ae), \
23387   xCM_ (m1, lo, m2, op, nops, ops, ae), \
23388   xCM_ (m1, mi, m2, op, nops, ops, ae), \
23389   xCM_ (m1, pl, m2, op, nops, ops, ae), \
23390   xCM_ (m1, vs, m2, op, nops, ops, ae), \
23391   xCM_ (m1, vc, m2, op, nops, ops, ae), \
23392   xCM_ (m1, hi, m2, op, nops, ops, ae), \
23393   xCM_ (m1, ls, m2, op, nops, ops, ae), \
23394   xCM_ (m1, ge, m2, op, nops, ops, ae), \
23395   xCM_ (m1, lt, m2, op, nops, ops, ae), \
23396   xCM_ (m1, gt, m2, op, nops, ops, ae), \
23397   xCM_ (m1, le, m2, op, nops, ops, ae), \
23398   xCM_ (m1, al, m2, op, nops, ops, ae)
23399
23400 #define UE(mnem, op, nops, ops, ae)     \
23401   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23402
23403 #define UF(mnem, op, nops, ops, ae)     \
23404   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23405
23406 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
23407    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
23408    use the same encoding function for each.  */
23409 #define NUF(mnem, op, nops, ops, enc)                                   \
23410   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
23411     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
23412
23413 /* Neon data processing, version which indirects through neon_enc_tab for
23414    the various overloaded versions of opcodes.  */
23415 #define nUF(mnem, op, nops, ops, enc)                                   \
23416   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
23417     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
23418
23419 /* Neon insn with conditional suffix for the ARM version, non-overloaded
23420    version.  */
23421 #define NCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
23422   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
23423     THUMB_VARIANT, do_##enc, do_##enc, mve_p }
23424
23425 #define NCE(mnem, op, nops, ops, enc)                                   \
23426    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
23427
23428 #define NCEF(mnem, op, nops, ops, enc)                                  \
23429     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
23430
23431 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
23432 #define nCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
23433   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
23434     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, mve_p }
23435
23436 #define nCE(mnem, op, nops, ops, enc)                                   \
23437    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
23438
23439 #define nCEF(mnem, op, nops, ops, enc)                                  \
23440     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
23441
23442 /*   */
23443 #define mCEF(mnem, op, nops, ops, enc)                          \
23444   { #mnem, OPS##nops ops, OT_csuffixF, M_MNEM##op, M_MNEM##op,  \
23445     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23446
23447
23448 /* nCEF but for MVE predicated instructions.  */
23449 #define mnCEF(mnem, op, nops, ops, enc)                                 \
23450     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
23451
23452 /* nCE but for MVE predicated instructions.  */
23453 #define mnCE(mnem, op, nops, ops, enc)                                  \
23454    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
23455
23456 /* NUF but for potentially MVE predicated instructions.  */
23457 #define MNUF(mnem, op, nops, ops, enc)                                  \
23458   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
23459     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23460
23461 /* nUF but for potentially MVE predicated instructions.  */
23462 #define mnUF(mnem, op, nops, ops, enc)                                  \
23463   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
23464     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23465
23466 /* ToC but for potentially MVE predicated instructions.  */
23467 #define mToC(mnem, top, nops, ops, te) \
23468   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
23469     do_##te, 1 }
23470
23471 /* NCE but for MVE predicated instructions.  */
23472 #define MNCE(mnem, op, nops, ops, enc)                                  \
23473    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
23474
23475 /* NCEF but for MVE predicated instructions.  */
23476 #define MNCEF(mnem, op, nops, ops, enc)                                 \
23477     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
23478 #define do_0 0
23479
23480 static const struct asm_opcode insns[] =
23481 {
23482 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
23483 #define THUMB_VARIANT  & arm_ext_v4t
23484  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
23485  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
23486  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
23487  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
23488  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
23489  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
23490  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
23491  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
23492  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
23493  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
23494  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
23495  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
23496  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
23497  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
23498  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
23499  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
23500
23501  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
23502     for setting PSR flag bits.  They are obsolete in V6 and do not
23503     have Thumb equivalents. */
23504  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
23505  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
23506   CL("tstp",    110f000,           2, (RR, SH),      cmp),
23507  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
23508  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
23509   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
23510  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
23511  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
23512   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
23513
23514  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
23515  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
23516  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
23517  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
23518
23519  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
23520  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
23521  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
23522                                                                 OP_RRnpc),
23523                                         OP_ADDRGLDR),ldst, t_ldst),
23524  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
23525
23526  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23527  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23528  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23529  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23530  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23531  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23532
23533  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
23534  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
23535
23536   /* Pseudo ops.  */
23537  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
23538   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
23539  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
23540  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
23541
23542   /* Thumb-compatibility pseudo ops.  */
23543  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
23544  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
23545  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
23546  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
23547  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
23548  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
23549  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
23550  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
23551  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
23552  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
23553  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
23554  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
23555
23556  /* These may simplify to neg.  */
23557  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
23558  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
23559
23560 #undef THUMB_VARIANT
23561 #define THUMB_VARIANT  & arm_ext_os
23562
23563  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
23564  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
23565
23566 #undef  THUMB_VARIANT
23567 #define THUMB_VARIANT  & arm_ext_v6
23568
23569  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
23570
23571  /* V1 instructions with no Thumb analogue prior to V6T2.  */
23572 #undef  THUMB_VARIANT
23573 #define THUMB_VARIANT  & arm_ext_v6t2
23574
23575  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
23576  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
23577   CL("teqp",    130f000,           2, (RR, SH),      cmp),
23578
23579  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23580  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23581  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
23582  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23583
23584  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23585  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23586
23587  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23588  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23589
23590  /* V1 instructions with no Thumb analogue at all.  */
23591   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
23592   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
23593
23594   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
23595   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
23596   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
23597   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
23598   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
23599   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
23600   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
23601   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
23602
23603 #undef  ARM_VARIANT
23604 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
23605 #undef  THUMB_VARIANT
23606 #define THUMB_VARIANT  & arm_ext_v4t
23607
23608  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
23609  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
23610
23611 #undef  THUMB_VARIANT
23612 #define THUMB_VARIANT  & arm_ext_v6t2
23613
23614  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
23615   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
23616
23617   /* Generic coprocessor instructions.  */
23618  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
23619  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23620  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23621  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23622  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23623  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23624  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
23625
23626 #undef  ARM_VARIANT
23627 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
23628
23629   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
23630   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
23631
23632 #undef  ARM_VARIANT
23633 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
23634 #undef  THUMB_VARIANT
23635 #define THUMB_VARIANT  & arm_ext_msr
23636
23637  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
23638  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
23639
23640 #undef  ARM_VARIANT
23641 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
23642 #undef  THUMB_VARIANT
23643 #define THUMB_VARIANT  & arm_ext_v6t2
23644
23645  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23646   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23647  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23648   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23649  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23650   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23651  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23652   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23653
23654 #undef  ARM_VARIANT
23655 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
23656 #undef  THUMB_VARIANT
23657 #define THUMB_VARIANT  & arm_ext_v4t
23658
23659  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23660  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23661  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23662  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23663  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23664  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23665
23666 #undef  ARM_VARIANT
23667 #define ARM_VARIANT  & arm_ext_v4t_5
23668
23669   /* ARM Architecture 4T.  */
23670   /* Note: bx (and blx) are required on V5, even if the processor does
23671      not support Thumb.  */
23672  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
23673
23674 #undef  ARM_VARIANT
23675 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
23676 #undef  THUMB_VARIANT
23677 #define THUMB_VARIANT  & arm_ext_v5t
23678
23679   /* Note: blx has 2 variants; the .value coded here is for
23680      BLX(2).  Only this variant has conditional execution.  */
23681  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
23682  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
23683
23684 #undef  THUMB_VARIANT
23685 #define THUMB_VARIANT  & arm_ext_v6t2
23686
23687  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
23688  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23689  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
23690  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23691  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
23692  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
23693  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23694  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23695
23696 #undef  ARM_VARIANT
23697 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
23698 #undef  THUMB_VARIANT
23699 #define THUMB_VARIANT  & arm_ext_v5exp
23700
23701  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23702  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23703  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23704  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23705
23706  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23707  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23708
23709  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23710  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23711  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23712  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23713
23714  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23715  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23716  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23717  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23718
23719  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23720  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23721
23722  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23723  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23724  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23725  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23726
23727 #undef  ARM_VARIANT
23728 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
23729 #undef  THUMB_VARIANT
23730 #define THUMB_VARIANT  & arm_ext_v6t2
23731
23732  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
23733  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
23734      ldrd, t_ldstd),
23735  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
23736                                        ADDRGLDRS), ldrd, t_ldstd),
23737
23738  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23739  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23740
23741 #undef  ARM_VARIANT
23742 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
23743
23744  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
23745
23746 #undef  ARM_VARIANT
23747 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
23748 #undef  THUMB_VARIANT
23749 #define THUMB_VARIANT  & arm_ext_v6
23750
23751  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
23752  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
23753  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23754  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23755  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23756  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23757  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23758  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23759  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23760  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
23761
23762 #undef  THUMB_VARIANT
23763 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23764
23765  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
23766  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23767                                       strex,  t_strex),
23768 #undef  THUMB_VARIANT
23769 #define THUMB_VARIANT  & arm_ext_v6t2
23770
23771  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23772  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23773
23774  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
23775  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
23776
23777 /*  ARM V6 not included in V7M.  */
23778 #undef  THUMB_VARIANT
23779 #define THUMB_VARIANT  & arm_ext_v6_notm
23780  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23781  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23782   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
23783   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
23784  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
23785  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23786   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
23787  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
23788   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
23789  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23790  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23791  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23792   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
23793   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
23794   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
23795   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
23796  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
23797  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
23798  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
23799
23800 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
23801 #undef  THUMB_VARIANT
23802 #define THUMB_VARIANT  & arm_ext_v6_dsp
23803  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
23804  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
23805  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23806  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23807  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23808  /* Old name for QASX.  */
23809  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23810  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23811  /* Old name for QSAX.  */
23812  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23813  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23814  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23815  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23816  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23817  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23818  /* Old name for SASX.  */
23819  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23820  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23821  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23822  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23823  /* Old name for SHASX.  */
23824  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23825  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23826  /* Old name for SHSAX.  */
23827  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23828  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23829  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23830  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23831  /* Old name for SSAX.  */
23832  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23833  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23834  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23835  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23836  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23837  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23838  /* Old name for UASX.  */
23839  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23840  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23841  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23842  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23843  /* Old name for UHASX.  */
23844  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23845  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23846  /* Old name for UHSAX.  */
23847  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23848  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23849  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23850  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23851  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23852  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23853  /* Old name for UQASX.  */
23854  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23855  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23856  /* Old name for UQSAX.  */
23857  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23858  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23859  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23860  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23861  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23862  /* Old name for USAX.  */
23863  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23864  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23865  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23866  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23867  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23868  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
23869  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23870  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23871  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23872  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
23873  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23874  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23875  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23876  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23877  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23878  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23879  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23880  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23881  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23882  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23883  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23884  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23885  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23886  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23887  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23888  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23889  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23890  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23891  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23892  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
23893  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
23894  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
23895  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
23896  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
23897
23898 #undef  ARM_VARIANT
23899 #define ARM_VARIANT   & arm_ext_v6k_v6t2
23900 #undef  THUMB_VARIANT
23901 #define THUMB_VARIANT & arm_ext_v6k_v6t2
23902
23903  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
23904  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
23905  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
23906  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
23907
23908 #undef  THUMB_VARIANT
23909 #define THUMB_VARIANT  & arm_ext_v6_notm
23910  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
23911                                       ldrexd, t_ldrexd),
23912  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
23913                                        RRnpcb), strexd, t_strexd),
23914
23915 #undef  THUMB_VARIANT
23916 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23917  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
23918      rd_rn,  rd_rn),
23919  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
23920      rd_rn,  rd_rn),
23921  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23922      strex, t_strexbh),
23923  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23924      strex, t_strexbh),
23925  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
23926
23927 #undef  ARM_VARIANT
23928 #define ARM_VARIANT    & arm_ext_sec
23929 #undef  THUMB_VARIANT
23930 #define THUMB_VARIANT  & arm_ext_sec
23931
23932  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
23933
23934 #undef  ARM_VARIANT
23935 #define ARM_VARIANT    & arm_ext_virt
23936 #undef  THUMB_VARIANT
23937 #define THUMB_VARIANT    & arm_ext_virt
23938
23939  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
23940  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
23941
23942 #undef  ARM_VARIANT
23943 #define ARM_VARIANT    & arm_ext_pan
23944 #undef  THUMB_VARIANT
23945 #define THUMB_VARIANT  & arm_ext_pan
23946
23947  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
23948
23949 #undef  ARM_VARIANT
23950 #define ARM_VARIANT    & arm_ext_v6t2
23951 #undef  THUMB_VARIANT
23952 #define THUMB_VARIANT  & arm_ext_v6t2
23953
23954  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
23955  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
23956  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
23957  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
23958
23959  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
23960  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
23961
23962  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23963  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23964  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23965  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23966
23967 #undef  ARM_VARIANT
23968 #define ARM_VARIANT    & arm_ext_v3
23969 #undef  THUMB_VARIANT
23970 #define THUMB_VARIANT  & arm_ext_v6t2
23971
23972  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
23973  TUF("ssbb",    57ff040, f3bf8f40, 0, (), noargs, t_csdb),
23974  TUF("pssbb",   57ff044, f3bf8f44, 0, (), noargs, t_csdb),
23975
23976 #undef  ARM_VARIANT
23977 #define ARM_VARIANT    & arm_ext_v6t2
23978 #undef  THUMB_VARIANT
23979 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23980  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
23981  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
23982
23983  /* Thumb-only instructions.  */
23984 #undef  ARM_VARIANT
23985 #define ARM_VARIANT NULL
23986   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
23987   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
23988
23989  /* ARM does not really have an IT instruction, so always allow it.
23990     The opcode is copied from Thumb in order to allow warnings in
23991     -mimplicit-it=[never | arm] modes.  */
23992 #undef  ARM_VARIANT
23993 #define ARM_VARIANT  & arm_ext_v1
23994 #undef  THUMB_VARIANT
23995 #define THUMB_VARIANT  & arm_ext_v6t2
23996
23997  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
23998  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
23999  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
24000  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
24001  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
24002  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
24003  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
24004  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
24005  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
24006  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
24007  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
24008  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
24009  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
24010  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
24011  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
24012  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
24013  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
24014  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
24015
24016  /* Thumb2 only instructions.  */
24017 #undef  ARM_VARIANT
24018 #define ARM_VARIANT  NULL
24019
24020  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
24021  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
24022  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
24023  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
24024  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
24025  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
24026
24027  /* Hardware division instructions.  */
24028 #undef  ARM_VARIANT
24029 #define ARM_VARIANT    & arm_ext_adiv
24030 #undef  THUMB_VARIANT
24031 #define THUMB_VARIANT  & arm_ext_div
24032
24033  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
24034  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
24035
24036  /* ARM V6M/V7 instructions.  */
24037 #undef  ARM_VARIANT
24038 #define ARM_VARIANT    & arm_ext_barrier
24039 #undef  THUMB_VARIANT
24040 #define THUMB_VARIANT  & arm_ext_barrier
24041
24042  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
24043  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
24044  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
24045
24046  /* ARM V7 instructions.  */
24047 #undef  ARM_VARIANT
24048 #define ARM_VARIANT    & arm_ext_v7
24049 #undef  THUMB_VARIANT
24050 #define THUMB_VARIANT  & arm_ext_v7
24051
24052  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
24053  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
24054
24055 #undef  ARM_VARIANT
24056 #define ARM_VARIANT    & arm_ext_mp
24057 #undef  THUMB_VARIANT
24058 #define THUMB_VARIANT  & arm_ext_mp
24059
24060  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
24061
24062  /* AArchv8 instructions.  */
24063 #undef  ARM_VARIANT
24064 #define ARM_VARIANT   & arm_ext_v8
24065
24066 /* Instructions shared between armv8-a and armv8-m.  */
24067 #undef  THUMB_VARIANT
24068 #define THUMB_VARIANT & arm_ext_atomics
24069
24070  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24071  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24072  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24073  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24074  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24075  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24076  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24077  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
24078  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24079  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
24080                                                         stlex,  t_stlex),
24081  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
24082                                                         stlex, t_stlex),
24083  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
24084                                                         stlex, t_stlex),
24085 #undef  THUMB_VARIANT
24086 #define THUMB_VARIANT & arm_ext_v8
24087
24088  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
24089  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
24090                                                         ldrexd, t_ldrexd),
24091  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
24092                                                         strexd, t_strexd),
24093
24094 /* Defined in V8 but is in undefined encoding space for earlier
24095    architectures.  However earlier architectures are required to treat
24096    this instuction as a semihosting trap as well.  Hence while not explicitly
24097    defined as such, it is in fact correct to define the instruction for all
24098    architectures.  */
24099 #undef  THUMB_VARIANT
24100 #define THUMB_VARIANT  & arm_ext_v1
24101 #undef  ARM_VARIANT
24102 #define ARM_VARIANT  & arm_ext_v1
24103  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
24104
24105  /* ARMv8 T32 only.  */
24106 #undef  ARM_VARIANT
24107 #define ARM_VARIANT  NULL
24108  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
24109  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
24110  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
24111
24112   /* FP for ARMv8.  */
24113 #undef  ARM_VARIANT
24114 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
24115 #undef  THUMB_VARIANT
24116 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
24117
24118   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
24119   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
24120   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
24121   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
24122   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
24123   mnCE(vrintz, _vrintr, 2, (RNSDQMQ, oRNSDQMQ),         vrintz),
24124   mnCE(vrintx, _vrintr, 2, (RNSDQMQ, oRNSDQMQ),         vrintx),
24125   mnUF(vrinta, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrinta),
24126   mnUF(vrintn, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintn),
24127   mnUF(vrintp, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintp),
24128   mnUF(vrintm, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintm),
24129
24130   /* Crypto v1 extensions.  */
24131 #undef  ARM_VARIANT
24132 #define ARM_VARIANT & fpu_crypto_ext_armv8
24133 #undef  THUMB_VARIANT
24134 #define THUMB_VARIANT & fpu_crypto_ext_armv8
24135
24136   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
24137   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
24138   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
24139   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
24140   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
24141   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
24142   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
24143   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
24144   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
24145   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
24146   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
24147   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
24148   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
24149   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
24150
24151 #undef  ARM_VARIANT
24152 #define ARM_VARIANT   & crc_ext_armv8
24153 #undef  THUMB_VARIANT
24154 #define THUMB_VARIANT & crc_ext_armv8
24155   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
24156   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
24157   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
24158   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
24159   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
24160   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
24161
24162  /* ARMv8.2 RAS extension.  */
24163 #undef  ARM_VARIANT
24164 #define ARM_VARIANT   & arm_ext_ras
24165 #undef  THUMB_VARIANT
24166 #define THUMB_VARIANT & arm_ext_ras
24167  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
24168
24169 #undef  ARM_VARIANT
24170 #define ARM_VARIANT   & arm_ext_v8_3
24171 #undef  THUMB_VARIANT
24172 #define THUMB_VARIANT & arm_ext_v8_3
24173  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
24174
24175 #undef  ARM_VARIANT
24176 #define ARM_VARIANT   & fpu_neon_ext_dotprod
24177 #undef  THUMB_VARIANT
24178 #define THUMB_VARIANT & fpu_neon_ext_dotprod
24179  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
24180  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
24181
24182 #undef  ARM_VARIANT
24183 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
24184 #undef  THUMB_VARIANT
24185 #define THUMB_VARIANT NULL
24186
24187  cCE("wfs",     e200110, 1, (RR),            rd),
24188  cCE("rfs",     e300110, 1, (RR),            rd),
24189  cCE("wfc",     e400110, 1, (RR),            rd),
24190  cCE("rfc",     e500110, 1, (RR),            rd),
24191
24192  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24193  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24194  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24195  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24196
24197  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24198  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24199  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24200  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24201
24202  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
24203  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
24204  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
24205  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
24206  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
24207  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
24208  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
24209  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
24210  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
24211  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
24212  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
24213  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
24214
24215  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
24216  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
24217  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
24218  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
24219  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
24220  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
24221  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
24222  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
24223  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
24224  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
24225  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
24226  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
24227
24228  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
24229  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
24230  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
24231  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
24232  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
24233  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
24234  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
24235  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
24236  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
24237  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
24238  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
24239  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
24240
24241  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
24242  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
24243  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
24244  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
24245  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
24246  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
24247  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
24248  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
24249  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
24250  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
24251  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
24252  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
24253
24254  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
24255  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
24256  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
24257  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
24258  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
24259  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
24260  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
24261  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
24262  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
24263  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
24264  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
24265  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
24266
24267  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
24268  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
24269  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
24270  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
24271  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
24272  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
24273  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
24274  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
24275  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
24276  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
24277  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
24278  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
24279
24280  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
24281  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
24282  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
24283  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
24284  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
24285  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
24286  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
24287  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
24288  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
24289  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
24290  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
24291  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
24292
24293  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
24294  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
24295  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
24296  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
24297  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
24298  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
24299  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
24300  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
24301  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
24302  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
24303  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
24304  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
24305
24306  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
24307  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
24308  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
24309  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
24310  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
24311  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
24312  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
24313  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
24314  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
24315  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
24316  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
24317  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
24318
24319  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
24320  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
24321  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
24322  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
24323  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
24324  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
24325  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
24326  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
24327  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
24328  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
24329  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
24330  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
24331
24332  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
24333  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
24334  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
24335  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
24336  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
24337  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
24338  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
24339  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
24340  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
24341  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
24342  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
24343  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
24344
24345  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
24346  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
24347  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
24348  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
24349  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
24350  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
24351  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
24352  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
24353  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
24354  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
24355  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
24356  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
24357
24358  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
24359  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
24360  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
24361  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
24362  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
24363  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
24364  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
24365  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
24366  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
24367  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
24368  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
24369  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
24370
24371  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
24372  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
24373  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
24374  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
24375  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
24376  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
24377  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
24378  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
24379  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
24380  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
24381  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
24382  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
24383
24384  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
24385  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
24386  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
24387  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
24388  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
24389  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
24390  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
24391  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
24392  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
24393  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
24394  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
24395  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
24396
24397  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
24398  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
24399  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
24400  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
24401  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
24402  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
24403  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
24404  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
24405  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
24406  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
24407  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
24408  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
24409
24410  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
24411  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
24412  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
24413  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
24414  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
24415  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24416  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24417  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24418  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
24419  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
24420  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
24421  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
24422
24423  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
24424  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
24425  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
24426  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
24427  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
24428  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24429  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24430  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24431  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
24432  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
24433  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
24434  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
24435
24436  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
24437  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
24438  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
24439  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
24440  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
24441  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24442  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24443  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24444  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
24445  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
24446  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
24447  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
24448
24449  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
24450  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
24451  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
24452  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
24453  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
24454  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24455  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24456  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24457  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
24458  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
24459  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
24460  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
24461
24462  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
24463  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
24464  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
24465  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
24466  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
24467  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24468  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24469  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24470  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
24471  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
24472  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
24473  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
24474
24475  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
24476  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
24477  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
24478  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
24479  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
24480  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24481  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24482  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24483  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
24484  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
24485  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
24486  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
24487
24488  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
24489  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
24490  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
24491  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
24492  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
24493  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24494  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24495  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24496  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
24497  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
24498  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
24499  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
24500
24501  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
24502  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
24503  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
24504  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
24505  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
24506  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24507  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24508  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24509  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
24510  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
24511  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
24512  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
24513
24514  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
24515  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
24516  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
24517  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
24518  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
24519  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24520  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24521  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24522  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
24523  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
24524  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
24525  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
24526
24527  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
24528  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
24529  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
24530  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
24531  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
24532  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24533  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24534  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24535  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
24536  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
24537  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
24538  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
24539
24540  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24541  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24542  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24543  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24544  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24545  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24546  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24547  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24548  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24549  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24550  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24551  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24552
24553  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24554  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24555  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24556  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24557  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24558  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24559  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24560  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24561  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24562  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24563  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24564  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24565
24566  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24567  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24568  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24569  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24570  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24571  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24572  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24573  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24574  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24575  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24576  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24577  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24578
24579  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
24580  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
24581  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
24582  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
24583
24584  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
24585  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
24586  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
24587  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
24588  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
24589  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
24590  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
24591  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
24592  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
24593  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
24594  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
24595  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
24596
24597   /* The implementation of the FIX instruction is broken on some
24598      assemblers, in that it accepts a precision specifier as well as a
24599      rounding specifier, despite the fact that this is meaningless.
24600      To be more compatible, we accept it as well, though of course it
24601      does not set any bits.  */
24602  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
24603  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
24604  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
24605  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
24606  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
24607  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
24608  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
24609  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
24610  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
24611  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
24612  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
24613  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
24614  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
24615
24616   /* Instructions that were new with the real FPA, call them V2.  */
24617 #undef  ARM_VARIANT
24618 #define ARM_VARIANT  & fpu_fpa_ext_v2
24619
24620  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24621  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24622  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24623  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24624  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24625  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24626
24627 #undef  ARM_VARIANT
24628 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
24629 #undef THUMB_VARIANT
24630 #define THUMB_VARIANT  & arm_ext_v6t2
24631  mcCE(vmrs,     ef00a10, 2, (APSR_RR, RVC),   vmrs),
24632  mcCE(vmsr,     ee00a10, 2, (RVC, RR),        vmsr),
24633 #undef THUMB_VARIANT
24634
24635   /* Moves and type conversions.  */
24636  cCE("fmstat",  ef1fa10, 0, (),               noargs),
24637  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24638  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
24639  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
24640  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24641  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
24642  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24643  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
24644  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
24645
24646   /* Memory operations.  */
24647  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
24648  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
24649  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24650  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24651  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24652  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24653  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24654  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24655  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24656  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24657  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24658  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24659  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24660  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24661  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24662  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24663  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24664  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24665
24666   /* Monadic operations.  */
24667  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24668  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
24669  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24670
24671   /* Dyadic operations.  */
24672  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24673  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24674  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24675  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24676  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24677  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24678  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24679  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24680  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24681
24682   /* Comparisons.  */
24683  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
24684  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
24685  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24686  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
24687
24688  /* Double precision load/store are still present on single precision
24689     implementations.  */
24690  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
24691  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
24692  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24693  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24694  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24695  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24696  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24697  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24698  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24699  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24700
24701 #undef  ARM_VARIANT
24702 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
24703
24704   /* Moves and type conversions.  */
24705  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24706  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24707  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
24708  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
24709  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
24710  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
24711  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24712  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24713  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24714  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24715  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24716  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24717
24718   /* Monadic operations.  */
24719  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24720  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24721  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24722
24723   /* Dyadic operations.  */
24724  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24725  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24726  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24727  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24728  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24729  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24730  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24731  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24732  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24733
24734   /* Comparisons.  */
24735  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24736  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
24737  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24738  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
24739
24740 /* Instructions which may belong to either the Neon or VFP instruction sets.
24741    Individual encoder functions perform additional architecture checks.  */
24742 #undef  ARM_VARIANT
24743 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
24744 #undef  THUMB_VARIANT
24745 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
24746
24747   /* These mnemonics are unique to VFP.  */
24748  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
24749  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
24750  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24751  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24752  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24753  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
24754  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
24755  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
24756
24757   /* Mnemonics shared by Neon and VFP.  */
24758  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
24759
24760  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24761  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24762  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24763  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24764  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24765  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24766
24767  mnCEF(vcvt,     _vcvt,   3, (RNSDQMQ, RNSDQMQ, oI32z), neon_cvt),
24768  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
24769  MNCEF(vcvtb,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtb),
24770  MNCEF(vcvtt,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtt),
24771
24772
24773   /* NOTE: All VMOV encoding is special-cased!  */
24774  NCE(vmovq,     0,       1, (VMOV), neon_mov),
24775
24776 #undef  THUMB_VARIANT
24777 /* Could be either VLDR/VSTR or VLDR/VSTR (system register) which are guarded
24778    by different feature bits.  Since we are setting the Thumb guard, we can
24779    require Thumb-1 which makes it a nop guard and set the right feature bit in
24780    do_vldr_vstr ().  */
24781 #define THUMB_VARIANT  & arm_ext_v4t
24782  NCE(vldr,      d100b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
24783  NCE(vstr,      d000b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
24784
24785 #undef  ARM_VARIANT
24786 #define ARM_VARIANT    & arm_ext_fp16
24787 #undef  THUMB_VARIANT
24788 #define THUMB_VARIANT  & arm_ext_fp16
24789  /* New instructions added from v8.2, allowing the extraction and insertion of
24790     the upper 16 bits of a 32-bit vector register.  */
24791  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
24792  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
24793
24794  /* New backported fma/fms instructions optional in v8.2.  */
24795  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
24796  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
24797
24798 #undef  THUMB_VARIANT
24799 #define THUMB_VARIANT  & fpu_neon_ext_v1
24800 #undef  ARM_VARIANT
24801 #define ARM_VARIANT    & fpu_neon_ext_v1
24802
24803   /* Data processing with three registers of the same length.  */
24804   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
24805  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
24806  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
24807  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24808  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24809  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24810   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
24811  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
24812  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
24813  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
24814  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
24815   /* If not immediate, fall back to neon_dyadic_i64_su.
24816      shl should accept I8 I16 I32 I64,
24817      qshl should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
24818  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl),
24819  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl),
24820   /* Logic ops, types optional & ignored.  */
24821  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24822  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24823  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24824  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24825  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
24826   /* Bitfield ops, untyped.  */
24827  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24828  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24829  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24830  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24831  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24832  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24833   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
24834  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24835  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24836  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24837   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
24838      back to neon_dyadic_if_su.  */
24839  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
24840  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
24841  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
24842  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
24843  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
24844  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
24845  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
24846  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
24847   /* Comparison. Type I8 I16 I32 F32.  */
24848  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
24849  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
24850   /* As above, D registers only.  */
24851  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
24852  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
24853   /* Int and float variants, signedness unimportant.  */
24854  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
24855  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
24856  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
24857   /* Add/sub take types I8 I16 I32 I64 F32.  */
24858  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
24859  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
24860   /* vtst takes sizes 8, 16, 32.  */
24861  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
24862  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
24863   /* VMUL takes I8 I16 I32 F32 P8.  */
24864  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
24865   /* VQD{R}MULH takes S16 S32.  */
24866  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
24867  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
24868  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
24869  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
24870  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
24871  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
24872  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
24873  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
24874  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
24875  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
24876  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
24877  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
24878  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
24879  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
24880  /* ARM v8.1 extension.  */
24881  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
24882  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
24883  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
24884
24885   /* Two address, int/float. Types S8 S16 S32 F32.  */
24886  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
24887  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
24888
24889   /* Data processing with two registers and a shift amount.  */
24890   /* Right shifts, and variants with rounding.
24891      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
24892  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
24893  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
24894  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
24895  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
24896  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
24897  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
24898   /* Shift and insert. Sizes accepted 8 16 32 64.  */
24899  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
24900  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
24901   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
24902  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
24903   /* Right shift immediate, saturating & narrowing, with rounding variants.
24904      Types accepted S16 S32 S64 U16 U32 U64.  */
24905  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
24906  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
24907   /* As above, unsigned. Types accepted S16 S32 S64.  */
24908  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
24909  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
24910   /* Right shift narrowing. Types accepted I16 I32 I64.  */
24911  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
24912  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
24913   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
24914  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
24915   /* CVT with optional immediate for fixed-point variant.  */
24916  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
24917
24918  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
24919
24920   /* Data processing, three registers of different lengths.  */
24921   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
24922  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
24923   /* If not scalar, fall back to neon_dyadic_long.
24924      Vector types as above, scalar types S16 S32 U16 U32.  */
24925  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
24926  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
24927   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
24928  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
24929  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
24930   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
24931  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24932  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24933  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24934  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24935   /* Saturating doubling multiplies. Types S16 S32.  */
24936  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24937  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24938  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24939   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
24940      S16 S32 U16 U32.  */
24941  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
24942
24943   /* Extract. Size 8.  */
24944  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
24945  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
24946
24947   /* Two registers, miscellaneous.  */
24948   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
24949  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
24950  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
24951  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
24952   /* Vector replicate. Sizes 8 16 32.  */
24953  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
24954   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
24955  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
24956   /* VMOVN. Types I16 I32 I64.  */
24957  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
24958   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
24959  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
24960   /* VQMOVUN. Types S16 S32 S64.  */
24961  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
24962   /* VZIP / VUZP. Sizes 8 16 32.  */
24963  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
24964  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
24965  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
24966  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
24967   /* VQABS / VQNEG. Types S8 S16 S32.  */
24968  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
24969  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
24970   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
24971  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
24972  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
24973  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
24974  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
24975   /* Reciprocal estimates.  Types U32 F16 F32.  */
24976  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
24977  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
24978  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
24979  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
24980   /* VCLS. Types S8 S16 S32.  */
24981  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
24982   /* VCLZ. Types I8 I16 I32.  */
24983  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
24984   /* VCNT. Size 8.  */
24985  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
24986  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
24987   /* Two address, untyped.  */
24988  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
24989  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
24990   /* VTRN. Sizes 8 16 32.  */
24991  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
24992  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
24993
24994   /* Table lookup. Size 8.  */
24995  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
24996  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
24997
24998 #undef  THUMB_VARIANT
24999 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
25000 #undef  ARM_VARIANT
25001 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
25002
25003   /* Neon element/structure load/store.  */
25004  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25005  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25006  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25007  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25008  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25009  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25010  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25011  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25012
25013 #undef  THUMB_VARIANT
25014 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
25015 #undef  ARM_VARIANT
25016 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
25017  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
25018  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25019  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25020  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25021  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25022  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25023  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25024  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25025  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25026
25027 #undef  THUMB_VARIANT
25028 #define THUMB_VARIANT  & fpu_vfp_ext_v3
25029 #undef  ARM_VARIANT
25030 #define ARM_VARIANT    & fpu_vfp_ext_v3
25031
25032  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
25033  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25034  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25035  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25036  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25037  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25038  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25039  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25040  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25041
25042 #undef  ARM_VARIANT
25043 #define ARM_VARIANT    & fpu_vfp_ext_fma
25044 #undef  THUMB_VARIANT
25045 #define THUMB_VARIANT  & fpu_vfp_ext_fma
25046  /* Mnemonics shared by Neon, VFP and MVE.  These are included in the
25047     VFP FMA variant; NEON and VFP FMA always includes the NEON
25048     FMA instructions.  */
25049  mnCEF(vfma,     _vfma,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR), neon_fmac),
25050  mnCEF(vfms,     _vfms,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ),  neon_fmac),
25051
25052  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
25053     the v form should always be used.  */
25054  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
25055  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
25056  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
25057  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
25058  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
25059  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
25060
25061 #undef THUMB_VARIANT
25062 #undef  ARM_VARIANT
25063 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
25064
25065  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25066  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25067  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25068  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25069  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25070  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25071  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
25072  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
25073
25074 #undef  ARM_VARIANT
25075 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
25076
25077  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
25078  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
25079  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
25080  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
25081  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
25082  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
25083  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
25084  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
25085  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
25086  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25087  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25088  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25089  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25090  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25091  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25092  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25093  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25094  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25095  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
25096  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
25097  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25098  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25099  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25100  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25101  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25102  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25103  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
25104  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
25105  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
25106  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
25107  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
25108  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
25109  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
25110  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
25111  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
25112  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
25113  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
25114  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25115  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25116  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25117  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25118  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25119  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25120  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25121  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25122  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25123  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
25124  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25125  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25126  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25127  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25128  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25129  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25130  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25131  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25132  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25133  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25134  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25135  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25136  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25137  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25138  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25139  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25140  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25141  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25142  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25143  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25144  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25145  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
25146  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
25147  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25148  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25149  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25150  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25151  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25152  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25153  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25154  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25155  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25156  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25157  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25158  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25159  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25160  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25161  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25162  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25163  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25164  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25165  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
25166  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25167  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25168  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25169  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25170  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25171  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25172  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25173  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25174  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25175  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25176  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25177  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25178  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25179  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25180  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25181  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25182  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25183  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25184  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25185  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25186  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25187  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
25188  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25189  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25190  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25191  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25192  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25193  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25194  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25195  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25196  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25197  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25198  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25199  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25200  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25201  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25202  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25203  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25204  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25205  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25206  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25207  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25208  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
25209  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
25210  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25211  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25212  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25213  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25214  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25215  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25216  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25217  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25218  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25219  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
25220  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
25221  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
25222  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
25223  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
25224  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
25225  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25226  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25227  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25228  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
25229  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
25230  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
25231  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
25232  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
25233  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
25234  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25235  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25236  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25237  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25238  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
25239
25240 #undef  ARM_VARIANT
25241 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
25242
25243  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
25244  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
25245  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
25246  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
25247  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
25248  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
25249  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25250  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25251  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25252  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25253  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25254  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25255  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25256  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25257  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25258  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25259  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25260  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25261  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25262  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25263  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
25264  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25265  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25266  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25267  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25268  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25269  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25270  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25271  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25272  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25273  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25274  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25275  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25276  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25277  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25278  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25279  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25280  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25281  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25282  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25283  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25284  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25285  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25286  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25287  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25288  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25289  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25290  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25291  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25292  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25293  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25294  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25295  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25296  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25297  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25298  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25299  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25300
25301 #undef  ARM_VARIANT
25302 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
25303
25304  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
25305  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
25306  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
25307  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
25308  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
25309  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
25310  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
25311  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
25312  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
25313  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
25314  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
25315  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
25316  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
25317  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
25318  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
25319  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
25320  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
25321  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
25322  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
25323  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
25324  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
25325  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
25326  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
25327  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
25328  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
25329  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
25330  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
25331  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
25332  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
25333  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
25334  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
25335  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
25336  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
25337  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
25338  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
25339  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
25340  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
25341  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
25342  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
25343  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
25344  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
25345  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
25346  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
25347  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
25348  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
25349  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
25350  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
25351  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
25352  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
25353  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
25354  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
25355  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
25356  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
25357  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
25358  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
25359  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
25360  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
25361  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
25362  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
25363  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
25364  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
25365  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
25366  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
25367  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
25368  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25369  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25370  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25371  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25372  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25373  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25374  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25375  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25376  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
25377  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
25378  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
25379  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
25380
25381  /* ARMv8.5-A instructions.  */
25382 #undef  ARM_VARIANT
25383 #define ARM_VARIANT   & arm_ext_sb
25384 #undef  THUMB_VARIANT
25385 #define THUMB_VARIANT & arm_ext_sb
25386  TUF("sb", 57ff070, f3bf8f70, 0, (), noargs, noargs),
25387
25388 #undef  ARM_VARIANT
25389 #define ARM_VARIANT   & arm_ext_predres
25390 #undef  THUMB_VARIANT
25391 #define THUMB_VARIANT & arm_ext_predres
25392  CE("cfprctx", e070f93, 1, (RRnpc), rd),
25393  CE("dvprctx", e070fb3, 1, (RRnpc), rd),
25394  CE("cpprctx", e070ff3, 1, (RRnpc), rd),
25395
25396  /* ARMv8-M instructions.  */
25397 #undef  ARM_VARIANT
25398 #define ARM_VARIANT NULL
25399 #undef  THUMB_VARIANT
25400 #define THUMB_VARIANT & arm_ext_v8m
25401  ToU("sg",    e97fe97f, 0, (),             noargs),
25402  ToC("blxns", 4784,     1, (RRnpc),        t_blx),
25403  ToC("bxns",  4704,     1, (RRnpc),        t_bx),
25404  ToC("tt",    e840f000, 2, (RRnpc, RRnpc), tt),
25405  ToC("ttt",   e840f040, 2, (RRnpc, RRnpc), tt),
25406  ToC("tta",   e840f080, 2, (RRnpc, RRnpc), tt),
25407  ToC("ttat",  e840f0c0, 2, (RRnpc, RRnpc), tt),
25408
25409  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
25410     instructions behave as nop if no VFP is present.  */
25411 #undef  THUMB_VARIANT
25412 #define THUMB_VARIANT & arm_ext_v8m_main
25413  ToC("vlldm", ec300a00, 1, (RRnpc), rn),
25414  ToC("vlstm", ec200a00, 1, (RRnpc), rn),
25415
25416  /* Armv8.1-M Mainline instructions.  */
25417 #undef  THUMB_VARIANT
25418 #define THUMB_VARIANT & arm_ext_v8_1m_main
25419  toU("cinc",  _cinc,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25420  toU("cinv",  _cinv,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25421  toU("cneg",  _cneg,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25422  toU("csel",  _csel,  4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25423  toU("csetm", _csetm, 2, (RRnpcsp, COND),               t_cond),
25424  toU("cset",  _cset,  2, (RRnpcsp, COND),               t_cond),
25425  toU("csinc", _csinc, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25426  toU("csinv", _csinv, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25427  toU("csneg", _csneg, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25428
25429  toC("bf",     _bf,     2, (EXPs, EXPs),             t_branch_future),
25430  toU("bfcsel", _bfcsel, 4, (EXPs, EXPs, EXPs, COND), t_branch_future),
25431  toC("bfx",    _bfx,    2, (EXPs, RRnpcsp),          t_branch_future),
25432  toC("bfl",    _bfl,    2, (EXPs, EXPs),             t_branch_future),
25433  toC("bflx",   _bflx,   2, (EXPs, RRnpcsp),          t_branch_future),
25434
25435  toU("dls", _dls, 2, (LR, RRnpcsp),      t_loloop),
25436  toU("wls", _wls, 3, (LR, RRnpcsp, EXP), t_loloop),
25437  toU("le",  _le,  2, (oLR, EXP),         t_loloop),
25438
25439  ToC("clrm",    e89f0000, 1, (CLRMLST),  t_clrm),
25440  ToC("vscclrm", ec9f0a00, 1, (VRSDVLST), t_vscclrm),
25441
25442 #undef  THUMB_VARIANT
25443 #define THUMB_VARIANT & mve_ext
25444  ToC("lsll",    ea50010d, 3, (RRe, RRo, RRnpcsp_I32), mve_scalar_shift),
25445  ToC("lsrl",    ea50011f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25446  ToC("asrl",    ea50012d, 3, (RRe, RRo, RRnpcsp_I32), mve_scalar_shift),
25447  ToC("uqrshll", ea51010d, 4, (RRe, RRo, I48_I64, RRnpcsp), mve_scalar_shift1),
25448  ToC("sqrshrl", ea51012d, 4, (RRe, RRo, I48_I64, RRnpcsp), mve_scalar_shift1),
25449  ToC("uqshll",  ea51010f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25450  ToC("urshrl",  ea51011f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25451  ToC("srshrl",  ea51012f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25452  ToC("sqshll",  ea51013f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25453  ToC("uqrshl",  ea500f0d, 2, (RRnpcsp, RRnpcsp),      mve_scalar_shift),
25454  ToC("sqrshr",  ea500f2d, 2, (RRnpcsp, RRnpcsp),      mve_scalar_shift),
25455  ToC("uqshl",   ea500f0f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25456  ToC("urshr",   ea500f1f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25457  ToC("srshr",   ea500f2f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25458  ToC("sqshl",   ea500f3f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25459
25460  ToC("vpt",     ee410f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25461  ToC("vptt",    ee018f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25462  ToC("vpte",    ee418f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25463  ToC("vpttt",   ee014f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25464  ToC("vptte",   ee01cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25465  ToC("vptet",   ee41cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25466  ToC("vptee",   ee414f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25467  ToC("vptttt",  ee012f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25468  ToC("vpttte",  ee016f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25469  ToC("vpttet",  ee01ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25470  ToC("vpttee",  ee01af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25471  ToC("vptett",  ee41af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25472  ToC("vptete",  ee41ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25473  ToC("vpteet",  ee416f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25474  ToC("vpteee",  ee412f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25475
25476  ToC("vpst",    fe710f4d, 0, (), mve_vpt),
25477  ToC("vpstt",   fe318f4d, 0, (), mve_vpt),
25478  ToC("vpste",   fe718f4d, 0, (), mve_vpt),
25479  ToC("vpsttt",  fe314f4d, 0, (), mve_vpt),
25480  ToC("vpstte",  fe31cf4d, 0, (), mve_vpt),
25481  ToC("vpstet",  fe71cf4d, 0, (), mve_vpt),
25482  ToC("vpstee",  fe714f4d, 0, (), mve_vpt),
25483  ToC("vpstttt", fe312f4d, 0, (), mve_vpt),
25484  ToC("vpsttte", fe316f4d, 0, (), mve_vpt),
25485  ToC("vpsttet", fe31ef4d, 0, (), mve_vpt),
25486  ToC("vpsttee", fe31af4d, 0, (), mve_vpt),
25487  ToC("vpstett", fe71af4d, 0, (), mve_vpt),
25488  ToC("vpstete", fe71ef4d, 0, (), mve_vpt),
25489  ToC("vpsteet", fe716f4d, 0, (), mve_vpt),
25490  ToC("vpsteee", fe712f4d, 0, (), mve_vpt),
25491
25492  /* MVE and MVE FP only.  */
25493  mToC("vhcadd", ee000f00,   4, (RMQ, RMQ, RMQ, EXPi),             mve_vhcadd),
25494  mCEF(vadc,     _vadc,      3, (RMQ, RMQ, RMQ),                   mve_vadc),
25495  mCEF(vadci,    _vadci,     3, (RMQ, RMQ, RMQ),                   mve_vadc),
25496  mToC("vsbc",   fe300f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
25497  mToC("vsbci",  fe301f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
25498  mCEF(vmullb,   _vmullb,    3, (RMQ, RMQ, RMQ),                   mve_vmull),
25499  mCEF(vabav,    _vabav,     3, (RRnpcsp, RMQ, RMQ),               mve_vabav),
25500  mCEF(vmladav,    _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25501  mCEF(vmladava,   _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25502  mCEF(vmladavx,   _vmladavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25503  mCEF(vmladavax,  _vmladavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
25504  mCEF(vmlav,      _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25505  mCEF(vmlava,     _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25506  mCEF(vmlsdav,    _vmlsdav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25507  mCEF(vmlsdava,   _vmlsdava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25508  mCEF(vmlsdavx,   _vmlsdavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25509  mCEF(vmlsdavax,  _vmlsdavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
25510
25511  mCEF(vst20,    _vst20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25512  mCEF(vst21,    _vst21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25513  mCEF(vst40,    _vst40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25514  mCEF(vst41,    _vst41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25515  mCEF(vst42,    _vst42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25516  mCEF(vst43,    _vst43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25517  mCEF(vld20,    _vld20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25518  mCEF(vld21,    _vld21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25519  mCEF(vld40,    _vld40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25520  mCEF(vld41,    _vld41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25521  mCEF(vld42,    _vld42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25522  mCEF(vld43,    _vld43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25523  mCEF(vstrb,    _vstrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25524  mCEF(vstrh,    _vstrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25525  mCEF(vstrw,    _vstrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25526  mCEF(vstrd,    _vstrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25527  mCEF(vldrb,    _vldrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25528  mCEF(vldrh,    _vldrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25529  mCEF(vldrw,    _vldrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25530  mCEF(vldrd,    _vldrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25531
25532  mCEF(vmovnt,   _vmovnt,    2, (RMQ, RMQ),                        mve_movn),
25533  mCEF(vmovnb,   _vmovnb,    2, (RMQ, RMQ),                        mve_movn),
25534  mCEF(vbrsr,    _vbrsr,     3, (RMQ, RMQ, RR),                    mve_vbrsr),
25535  mCEF(vaddlv,   _vaddlv,    3, (RRe, RRo, RMQ),                   mve_vaddlv),
25536  mCEF(vaddlva,  _vaddlva,   3, (RRe, RRo, RMQ),                   mve_vaddlv),
25537  mCEF(vaddv,    _vaddv,     2, (RRe, RMQ),                        mve_vaddv),
25538  mCEF(vaddva,   _vaddva,    2, (RRe, RMQ),                        mve_vaddv),
25539  mCEF(vddup,    _vddup,     3, (RMQ, RRe, EXPi),                  mve_viddup),
25540  mCEF(vdwdup,   _vdwdup,    4, (RMQ, RRe, RR, EXPi),              mve_viddup),
25541  mCEF(vidup,    _vidup,     3, (RMQ, RRe, EXPi),                  mve_viddup),
25542  mCEF(viwdup,   _viwdup,    4, (RMQ, RRe, RR, EXPi),              mve_viddup),
25543  mToC("vmaxa",  ee330e81,   2, (RMQ, RMQ),                        mve_vmaxa_vmina),
25544  mToC("vmina",  ee331e81,   2, (RMQ, RMQ),                        mve_vmaxa_vmina),
25545  mCEF(vmaxv,    _vmaxv,   2, (RR, RMQ),                           mve_vmaxv),
25546  mCEF(vmaxav,   _vmaxav,  2, (RR, RMQ),                           mve_vmaxv),
25547  mCEF(vminv,    _vminv,   2, (RR, RMQ),                           mve_vmaxv),
25548  mCEF(vminav,   _vminav,  2, (RR, RMQ),                           mve_vmaxv),
25549
25550  mCEF(vmlaldav,   _vmlaldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25551  mCEF(vmlaldava,  _vmlaldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25552  mCEF(vmlaldavx,  _vmlaldavx,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25553  mCEF(vmlaldavax, _vmlaldavax,  4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25554  mCEF(vmlalv,     _vmlaldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25555  mCEF(vmlalva,    _vmlaldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25556  mCEF(vmlsldav,   _vmlsldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25557  mCEF(vmlsldava,  _vmlsldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25558  mCEF(vmlsldavx,  _vmlsldavx,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25559  mCEF(vmlsldavax, _vmlsldavax,  4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25560  mToC("vrmlaldavh", ee800f00,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25561  mToC("vrmlaldavha",ee800f20,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25562  mCEF(vrmlaldavhx,  _vrmlaldavhx,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25563  mCEF(vrmlaldavhax, _vrmlaldavhax, 4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25564  mToC("vrmlalvh",   ee800f00,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25565  mToC("vrmlalvha",  ee800f20,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25566  mCEF(vrmlsldavh,   _vrmlsldavh,   4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25567  mCEF(vrmlsldavha,  _vrmlsldavha,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25568  mCEF(vrmlsldavhx,  _vrmlsldavhx,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25569  mCEF(vrmlsldavhax, _vrmlsldavhax, 4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25570
25571  mToC("vmlas",    ee011e40,     3, (RMQ, RMQ, RR),              mve_vmlas),
25572  mToC("vmulh",    ee010e01,     3, (RMQ, RMQ, RMQ),             mve_vmulh),
25573  mToC("vrmulh",   ee011e01,     3, (RMQ, RMQ, RMQ),             mve_vmulh),
25574  mToC("vpnot",    fe310f4d,     0, (),                          mve_vpnot),
25575  mToC("vpsel",    fe310f01,     3, (RMQ, RMQ, RMQ),             mve_vpsel),
25576
25577  mToC("vqdmladh",  ee000e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25578  mToC("vqdmladhx", ee001e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25579  mToC("vqrdmladh", ee000e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25580  mToC("vqrdmladhx",ee001e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25581  mToC("vqdmlsdh",  fe000e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25582  mToC("vqdmlsdhx", fe001e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25583  mToC("vqrdmlsdh", fe000e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25584  mToC("vqrdmlsdhx",fe001e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25585  mToC("vqdmlah",   ee000e60,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25586  mToC("vqdmlash",  ee001e60,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25587  mToC("vqrdmlash", ee001e40,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25588  mToC("vqdmullt",  ee301f00,    3, (RMQ, RMQ, RMQRR),           mve_vqdmull),
25589  mToC("vqdmullb",  ee300f00,    3, (RMQ, RMQ, RMQRR),           mve_vqdmull),
25590  mCEF(vqmovnt,    _vqmovnt,     2, (RMQ, RMQ),                  mve_vqmovn),
25591  mCEF(vqmovnb,    _vqmovnb,     2, (RMQ, RMQ),                  mve_vqmovn),
25592  mCEF(vqmovunt,   _vqmovunt,    2, (RMQ, RMQ),                  mve_vqmovn),
25593  mCEF(vqmovunb,   _vqmovunb,    2, (RMQ, RMQ),                  mve_vqmovn),
25594
25595  mCEF(vshrnt,     _vshrnt,      3, (RMQ, RMQ, I32z),    mve_vshrn),
25596  mCEF(vshrnb,     _vshrnb,      3, (RMQ, RMQ, I32z),    mve_vshrn),
25597  mCEF(vrshrnt,    _vrshrnt,     3, (RMQ, RMQ, I32z),    mve_vshrn),
25598  mCEF(vrshrnb,    _vrshrnb,     3, (RMQ, RMQ, I32z),    mve_vshrn),
25599  mCEF(vqshrnt,    _vqrshrnt,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25600  mCEF(vqshrnb,    _vqrshrnb,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25601  mCEF(vqshrunt,   _vqrshrunt,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25602  mCEF(vqshrunb,   _vqrshrunb,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25603  mCEF(vqrshrnt,   _vqrshrnt,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25604  mCEF(vqrshrnb,   _vqrshrnb,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25605  mCEF(vqrshrunt,  _vqrshrunt,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25606  mCEF(vqrshrunb,  _vqrshrunb,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25607
25608  mToC("vshlc",      eea00fc0,      3, (RMQ, RR, I32z),      mve_vshlc),
25609  mToC("vshllt",     ee201e00,      3, (RMQ, RMQ, I32),      mve_vshll),
25610  mToC("vshllb",     ee200e00,      3, (RMQ, RMQ, I32),      mve_vshll),
25611
25612  toU("dlstp",   _dlstp, 2, (LR, RR),      t_loloop),
25613  toU("wlstp",   _wlstp, 3, (LR, RR, EXP), t_loloop),
25614  toU("letp",    _letp,  2, (LR, EXP),     t_loloop),
25615  toU("lctp",    _lctp,  0, (),            t_loloop),
25616
25617 #undef THUMB_VARIANT
25618 #define THUMB_VARIANT & mve_fp_ext
25619  mToC("vcmul", ee300e00,   4, (RMQ, RMQ, RMQ, EXPi),              mve_vcmul),
25620  mToC("vfmas", ee311e40,   3, (RMQ, RMQ, RR),                     mve_vfmas),
25621  mToC("vmaxnma", ee3f0e81, 2, (RMQ, RMQ),                         mve_vmaxnma_vminnma),
25622  mToC("vminnma", ee3f1e81, 2, (RMQ, RMQ),                         mve_vmaxnma_vminnma),
25623  mToC("vmaxnmv", eeee0f00, 2, (RR, RMQ),                          mve_vmaxnmv),
25624  mToC("vmaxnmav",eeec0f00, 2, (RR, RMQ),                          mve_vmaxnmv),
25625  mToC("vminnmv", eeee0f80, 2, (RR, RMQ),                          mve_vmaxnmv),
25626  mToC("vminnmav",eeec0f80, 2, (RR, RMQ),                          mve_vmaxnmv),
25627
25628 #undef  ARM_VARIANT
25629 #define ARM_VARIANT  & fpu_vfp_ext_v1
25630 #undef  THUMB_VARIANT
25631 #define THUMB_VARIANT  & arm_ext_v6t2
25632  mnCEF(vmla,     _vmla,    3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ_RR), neon_mac_maybe_scalar),
25633  mnCEF(vmul,     _vmul,    3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ_RR), neon_mul),
25634
25635  mcCE(fcpyd,    eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
25636
25637 #undef  ARM_VARIANT
25638 #define ARM_VARIANT  & fpu_vfp_ext_v1xd
25639
25640  MNCE(vmov,   0,        1, (VMOV),            neon_mov),
25641  mcCE(fmrs,     e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
25642  mcCE(fmsr,     e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
25643  mcCE(fcpys,    eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
25644
25645  mCEF(vmullt, _vmullt,  3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ),  mve_vmull),
25646  mnCEF(vadd,  _vadd,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
25647  mnCEF(vsub,  _vsub,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
25648
25649  MNCEF(vabs,  1b10300,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
25650  MNCEF(vneg,  1b10380,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
25651
25652  mCEF(vmovlt, _vmovlt,  1, (VMOV),              mve_movl),
25653  mCEF(vmovlb, _vmovlb,  1, (VMOV),              mve_movl),
25654
25655  mnCE(vcmp,      _vcmp,    3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
25656  mnCE(vcmpe,     _vcmpe,   3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
25657
25658 #undef  ARM_VARIANT
25659 #define ARM_VARIANT  & fpu_vfp_ext_v2
25660
25661  mcCE(fmsrr,    c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
25662  mcCE(fmrrs,    c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
25663  mcCE(fmdrr,    c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
25664  mcCE(fmrrd,    c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
25665
25666 #undef  ARM_VARIANT
25667 #define ARM_VARIANT    & fpu_vfp_ext_armv8xd
25668  mnUF(vcvta,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvta),
25669  mnUF(vcvtp,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtp),
25670  mnUF(vcvtn,  _vcvta,  3, (RNSDQMQ, oRNSDQMQ, oI32z),   neon_cvtn),
25671  mnUF(vcvtm,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtm),
25672  mnUF(vmaxnm, _vmaxnm, 3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ), vmaxnm),
25673  mnUF(vminnm, _vminnm, 3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ), vmaxnm),
25674
25675 #undef  ARM_VARIANT
25676 #define ARM_VARIANT & fpu_neon_ext_v1
25677  mnUF(vabd,      _vabd,           3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25678  mnUF(vabdl,     _vabdl,          3, (RNQMQ, RNDMQ, RNDMQ),   neon_dyadic_long),
25679  mnUF(vaddl,     _vaddl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
25680  mnUF(vsubl,     _vsubl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
25681  mnUF(vand,      _vand,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25682  mnUF(vbic,      _vbic,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25683  mnUF(vorr,      _vorr,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25684  mnUF(vorn,      _vorn,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25685  mnUF(veor,      _veor,           3, (RNDQMQ, oRNDQMQ, RNDQMQ),      neon_logic),
25686  MNUF(vcls,      1b00400,         2, (RNDQMQ, RNDQMQ),               neon_cls),
25687  MNUF(vclz,      1b00480,         2, (RNDQMQ, RNDQMQ),               neon_clz),
25688  mnCE(vdup,      _vdup,           2, (RNDQMQ, RR_RNSC),              neon_dup),
25689  MNUF(vhadd,     00000000,        3, (RNDQMQ, oRNDQMQ, RNDQMQR),  neon_dyadic_i_su),
25690  MNUF(vrhadd,    00000100,        3, (RNDQMQ, oRNDQMQ, RNDQMQ),   neon_dyadic_i_su),
25691  MNUF(vhsub,     00000200,        3, (RNDQMQ, oRNDQMQ, RNDQMQR),  neon_dyadic_i_su),
25692  mnUF(vmin,      _vmin,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25693  mnUF(vmax,      _vmax,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25694  MNUF(vqadd,     0000010,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_dyadic_i64_su),
25695  MNUF(vqsub,     0000210,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_dyadic_i64_su),
25696  mnUF(vmvn,      _vmvn,    2, (RNDQMQ, RNDQMQ_Ibig), neon_mvn),
25697  MNUF(vqabs,     1b00700,  2, (RNDQMQ, RNDQMQ),     neon_sat_abs_neg),
25698  MNUF(vqneg,     1b00780,  2, (RNDQMQ, RNDQMQ),     neon_sat_abs_neg),
25699  mnUF(vqrdmlah,  _vqrdmlah,3, (RNDQMQ, oRNDQMQ, RNDQ_RNSC_RR), neon_qrdmlah),
25700  mnUF(vqdmulh,   _vqdmulh, 3, (RNDQMQ, oRNDQMQ, RNDQMQ_RNSC_RR), neon_qdmulh),
25701  mnUF(vqrdmulh,  _vqrdmulh,3, (RNDQMQ, oRNDQMQ, RNDQMQ_RNSC_RR), neon_qdmulh),
25702  MNUF(vqrshl,    0000510,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_rshl),
25703  MNUF(vrshl,     0000500,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_rshl),
25704  MNUF(vshr,      0800010,  3, (RNDQMQ, oRNDQMQ, I64z), neon_rshift_round_imm),
25705  MNUF(vrshr,     0800210,  3, (RNDQMQ, oRNDQMQ, I64z), neon_rshift_round_imm),
25706  MNUF(vsli,      1800510,  3, (RNDQMQ, oRNDQMQ, I63),  neon_sli),
25707  MNUF(vsri,      1800410,  3, (RNDQMQ, oRNDQMQ, I64z), neon_sri),
25708  MNUF(vrev64,    1b00000,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25709  MNUF(vrev32,    1b00080,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25710  MNUF(vrev16,    1b00100,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25711  mnUF(vshl,      _vshl,    3, (RNDQMQ, oRNDQMQ, RNDQMQ_I63b_RR), neon_shl),
25712  mnUF(vqshl,     _vqshl,   3, (RNDQMQ, oRNDQMQ, RNDQMQ_I63b_RR), neon_qshl),
25713  MNUF(vqshlu,    1800610,  3, (RNDQMQ, oRNDQMQ, I63),            neon_qshlu_imm),
25714
25715 #undef  ARM_VARIANT
25716 #define ARM_VARIANT & arm_ext_v8_3
25717 #undef  THUMB_VARIANT
25718 #define THUMB_VARIANT & arm_ext_v6t2_v8m
25719  MNUF (vcadd, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ, EXPi), vcadd),
25720  MNUF (vcmla, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ_RNSC, EXPi), vcmla),
25721 };
25722 #undef ARM_VARIANT
25723 #undef THUMB_VARIANT
25724 #undef TCE
25725 #undef TUE
25726 #undef TUF
25727 #undef TCC
25728 #undef cCE
25729 #undef cCL
25730 #undef C3E
25731 #undef C3
25732 #undef CE
25733 #undef CM
25734 #undef CL
25735 #undef UE
25736 #undef UF
25737 #undef UT
25738 #undef NUF
25739 #undef nUF
25740 #undef NCE
25741 #undef nCE
25742 #undef OPS0
25743 #undef OPS1
25744 #undef OPS2
25745 #undef OPS3
25746 #undef OPS4
25747 #undef OPS5
25748 #undef OPS6
25749 #undef do_0
25750 #undef ToC
25751 #undef toC
25752 #undef ToU
25753 #undef toU
25754 \f
25755 /* MD interface: bits in the object file.  */
25756
25757 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
25758    for use in the a.out file, and stores them in the array pointed to by buf.
25759    This knows about the endian-ness of the target machine and does
25760    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
25761    2 (short) and 4 (long)  Floating numbers are put out as a series of
25762    LITTLENUMS (shorts, here at least).  */
25763
25764 void
25765 md_number_to_chars (char * buf, valueT val, int n)
25766 {
25767   if (target_big_endian)
25768     number_to_chars_bigendian (buf, val, n);
25769   else
25770     number_to_chars_littleendian (buf, val, n);
25771 }
25772
25773 static valueT
25774 md_chars_to_number (char * buf, int n)
25775 {
25776   valueT result = 0;
25777   unsigned char * where = (unsigned char *) buf;
25778
25779   if (target_big_endian)
25780     {
25781       while (n--)
25782         {
25783           result <<= 8;
25784           result |= (*where++ & 255);
25785         }
25786     }
25787   else
25788     {
25789       while (n--)
25790         {
25791           result <<= 8;
25792           result |= (where[n] & 255);
25793         }
25794     }
25795
25796   return result;
25797 }
25798
25799 /* MD interface: Sections.  */
25800
25801 /* Calculate the maximum variable size (i.e., excluding fr_fix)
25802    that an rs_machine_dependent frag may reach.  */
25803
25804 unsigned int
25805 arm_frag_max_var (fragS *fragp)
25806 {
25807   /* We only use rs_machine_dependent for variable-size Thumb instructions,
25808      which are either THUMB_SIZE (2) or INSN_SIZE (4).
25809
25810      Note that we generate relaxable instructions even for cases that don't
25811      really need it, like an immediate that's a trivial constant.  So we're
25812      overestimating the instruction size for some of those cases.  Rather
25813      than putting more intelligence here, it would probably be better to
25814      avoid generating a relaxation frag in the first place when it can be
25815      determined up front that a short instruction will suffice.  */
25816
25817   gas_assert (fragp->fr_type == rs_machine_dependent);
25818   return INSN_SIZE;
25819 }
25820
25821 /* Estimate the size of a frag before relaxing.  Assume everything fits in
25822    2 bytes.  */
25823
25824 int
25825 md_estimate_size_before_relax (fragS * fragp,
25826                                segT    segtype ATTRIBUTE_UNUSED)
25827 {
25828   fragp->fr_var = 2;
25829   return 2;
25830 }
25831
25832 /* Convert a machine dependent frag.  */
25833
25834 void
25835 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
25836 {
25837   unsigned long insn;
25838   unsigned long old_op;
25839   char *buf;
25840   expressionS exp;
25841   fixS *fixp;
25842   int reloc_type;
25843   int pc_rel;
25844   int opcode;
25845
25846   buf = fragp->fr_literal + fragp->fr_fix;
25847
25848   old_op = bfd_get_16(abfd, buf);
25849   if (fragp->fr_symbol)
25850     {
25851       exp.X_op = O_symbol;
25852       exp.X_add_symbol = fragp->fr_symbol;
25853     }
25854   else
25855     {
25856       exp.X_op = O_constant;
25857     }
25858   exp.X_add_number = fragp->fr_offset;
25859   opcode = fragp->fr_subtype;
25860   switch (opcode)
25861     {
25862     case T_MNEM_ldr_pc:
25863     case T_MNEM_ldr_pc2:
25864     case T_MNEM_ldr_sp:
25865     case T_MNEM_str_sp:
25866     case T_MNEM_ldr:
25867     case T_MNEM_ldrb:
25868     case T_MNEM_ldrh:
25869     case T_MNEM_str:
25870     case T_MNEM_strb:
25871     case T_MNEM_strh:
25872       if (fragp->fr_var == 4)
25873         {
25874           insn = THUMB_OP32 (opcode);
25875           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
25876             {
25877               insn |= (old_op & 0x700) << 4;
25878             }
25879           else
25880             {
25881               insn |= (old_op & 7) << 12;
25882               insn |= (old_op & 0x38) << 13;
25883             }
25884           insn |= 0x00000c00;
25885           put_thumb32_insn (buf, insn);
25886           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
25887         }
25888       else
25889         {
25890           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
25891         }
25892       pc_rel = (opcode == T_MNEM_ldr_pc2);
25893       break;
25894     case T_MNEM_adr:
25895       if (fragp->fr_var == 4)
25896         {
25897           insn = THUMB_OP32 (opcode);
25898           insn |= (old_op & 0xf0) << 4;
25899           put_thumb32_insn (buf, insn);
25900           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
25901         }
25902       else
25903         {
25904           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25905           exp.X_add_number -= 4;
25906         }
25907       pc_rel = 1;
25908       break;
25909     case T_MNEM_mov:
25910     case T_MNEM_movs:
25911     case T_MNEM_cmp:
25912     case T_MNEM_cmn:
25913       if (fragp->fr_var == 4)
25914         {
25915           int r0off = (opcode == T_MNEM_mov
25916                        || opcode == T_MNEM_movs) ? 0 : 8;
25917           insn = THUMB_OP32 (opcode);
25918           insn = (insn & 0xe1ffffff) | 0x10000000;
25919           insn |= (old_op & 0x700) << r0off;
25920           put_thumb32_insn (buf, insn);
25921           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
25922         }
25923       else
25924         {
25925           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
25926         }
25927       pc_rel = 0;
25928       break;
25929     case T_MNEM_b:
25930       if (fragp->fr_var == 4)
25931         {
25932           insn = THUMB_OP32(opcode);
25933           put_thumb32_insn (buf, insn);
25934           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
25935         }
25936       else
25937         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
25938       pc_rel = 1;
25939       break;
25940     case T_MNEM_bcond:
25941       if (fragp->fr_var == 4)
25942         {
25943           insn = THUMB_OP32(opcode);
25944           insn |= (old_op & 0xf00) << 14;
25945           put_thumb32_insn (buf, insn);
25946           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
25947         }
25948       else
25949         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
25950       pc_rel = 1;
25951       break;
25952     case T_MNEM_add_sp:
25953     case T_MNEM_add_pc:
25954     case T_MNEM_inc_sp:
25955     case T_MNEM_dec_sp:
25956       if (fragp->fr_var == 4)
25957         {
25958           /* ??? Choose between add and addw.  */
25959           insn = THUMB_OP32 (opcode);
25960           insn |= (old_op & 0xf0) << 4;
25961           put_thumb32_insn (buf, insn);
25962           if (opcode == T_MNEM_add_pc)
25963             reloc_type = BFD_RELOC_ARM_T32_IMM12;
25964           else
25965             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
25966         }
25967       else
25968         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25969       pc_rel = 0;
25970       break;
25971
25972     case T_MNEM_addi:
25973     case T_MNEM_addis:
25974     case T_MNEM_subi:
25975     case T_MNEM_subis:
25976       if (fragp->fr_var == 4)
25977         {
25978           insn = THUMB_OP32 (opcode);
25979           insn |= (old_op & 0xf0) << 4;
25980           insn |= (old_op & 0xf) << 16;
25981           put_thumb32_insn (buf, insn);
25982           if (insn & (1 << 20))
25983             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
25984           else
25985             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
25986         }
25987       else
25988         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25989       pc_rel = 0;
25990       break;
25991     default:
25992       abort ();
25993     }
25994   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
25995                       (enum bfd_reloc_code_real) reloc_type);
25996   fixp->fx_file = fragp->fr_file;
25997   fixp->fx_line = fragp->fr_line;
25998   fragp->fr_fix += fragp->fr_var;
25999
26000   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
26001   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
26002       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
26003     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
26004 }
26005
26006 /* Return the size of a relaxable immediate operand instruction.
26007    SHIFT and SIZE specify the form of the allowable immediate.  */
26008 static int
26009 relax_immediate (fragS *fragp, int size, int shift)
26010 {
26011   offsetT offset;
26012   offsetT mask;
26013   offsetT low;
26014
26015   /* ??? Should be able to do better than this.  */
26016   if (fragp->fr_symbol)
26017     return 4;
26018
26019   low = (1 << shift) - 1;
26020   mask = (1 << (shift + size)) - (1 << shift);
26021   offset = fragp->fr_offset;
26022   /* Force misaligned offsets to 32-bit variant.  */
26023   if (offset & low)
26024     return 4;
26025   if (offset & ~mask)
26026     return 4;
26027   return 2;
26028 }
26029
26030 /* Get the address of a symbol during relaxation.  */
26031 static addressT
26032 relaxed_symbol_addr (fragS *fragp, long stretch)
26033 {
26034   fragS *sym_frag;
26035   addressT addr;
26036   symbolS *sym;
26037
26038   sym = fragp->fr_symbol;
26039   sym_frag = symbol_get_frag (sym);
26040   know (S_GET_SEGMENT (sym) != absolute_section
26041         || sym_frag == &zero_address_frag);
26042   addr = S_GET_VALUE (sym) + fragp->fr_offset;
26043
26044   /* If frag has yet to be reached on this pass, assume it will
26045      move by STRETCH just as we did.  If this is not so, it will
26046      be because some frag between grows, and that will force
26047      another pass.  */
26048
26049   if (stretch != 0
26050       && sym_frag->relax_marker != fragp->relax_marker)
26051     {
26052       fragS *f;
26053
26054       /* Adjust stretch for any alignment frag.  Note that if have
26055          been expanding the earlier code, the symbol may be
26056          defined in what appears to be an earlier frag.  FIXME:
26057          This doesn't handle the fr_subtype field, which specifies
26058          a maximum number of bytes to skip when doing an
26059          alignment.  */
26060       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
26061         {
26062           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
26063             {
26064               if (stretch < 0)
26065                 stretch = - ((- stretch)
26066                              & ~ ((1 << (int) f->fr_offset) - 1));
26067               else
26068                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
26069               if (stretch == 0)
26070                 break;
26071             }
26072         }
26073       if (f != NULL)
26074         addr += stretch;
26075     }
26076
26077   return addr;
26078 }
26079
26080 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
26081    load.  */
26082 static int
26083 relax_adr (fragS *fragp, asection *sec, long stretch)
26084 {
26085   addressT addr;
26086   offsetT val;
26087
26088   /* Assume worst case for symbols not known to be in the same section.  */
26089   if (fragp->fr_symbol == NULL
26090       || !S_IS_DEFINED (fragp->fr_symbol)
26091       || sec != S_GET_SEGMENT (fragp->fr_symbol)
26092       || S_IS_WEAK (fragp->fr_symbol))
26093     return 4;
26094
26095   val = relaxed_symbol_addr (fragp, stretch);
26096   addr = fragp->fr_address + fragp->fr_fix;
26097   addr = (addr + 4) & ~3;
26098   /* Force misaligned targets to 32-bit variant.  */
26099   if (val & 3)
26100     return 4;
26101   val -= addr;
26102   if (val < 0 || val > 1020)
26103     return 4;
26104   return 2;
26105 }
26106
26107 /* Return the size of a relaxable add/sub immediate instruction.  */
26108 static int
26109 relax_addsub (fragS *fragp, asection *sec)
26110 {
26111   char *buf;
26112   int op;
26113
26114   buf = fragp->fr_literal + fragp->fr_fix;
26115   op = bfd_get_16(sec->owner, buf);
26116   if ((op & 0xf) == ((op >> 4) & 0xf))
26117     return relax_immediate (fragp, 8, 0);
26118   else
26119     return relax_immediate (fragp, 3, 0);
26120 }
26121
26122 /* Return TRUE iff the definition of symbol S could be pre-empted
26123    (overridden) at link or load time.  */
26124 static bfd_boolean
26125 symbol_preemptible (symbolS *s)
26126 {
26127   /* Weak symbols can always be pre-empted.  */
26128   if (S_IS_WEAK (s))
26129     return TRUE;
26130
26131   /* Non-global symbols cannot be pre-empted. */
26132   if (! S_IS_EXTERNAL (s))
26133     return FALSE;
26134
26135 #ifdef OBJ_ELF
26136   /* In ELF, a global symbol can be marked protected, or private.  In that
26137      case it can't be pre-empted (other definitions in the same link unit
26138      would violate the ODR).  */
26139   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
26140     return FALSE;
26141 #endif
26142
26143   /* Other global symbols might be pre-empted.  */
26144   return TRUE;
26145 }
26146
26147 /* Return the size of a relaxable branch instruction.  BITS is the
26148    size of the offset field in the narrow instruction.  */
26149
26150 static int
26151 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
26152 {
26153   addressT addr;
26154   offsetT val;
26155   offsetT limit;
26156
26157   /* Assume worst case for symbols not known to be in the same section.  */
26158   if (!S_IS_DEFINED (fragp->fr_symbol)
26159       || sec != S_GET_SEGMENT (fragp->fr_symbol)
26160       || S_IS_WEAK (fragp->fr_symbol))
26161     return 4;
26162
26163 #ifdef OBJ_ELF
26164   /* A branch to a function in ARM state will require interworking.  */
26165   if (S_IS_DEFINED (fragp->fr_symbol)
26166       && ARM_IS_FUNC (fragp->fr_symbol))
26167       return 4;
26168 #endif
26169
26170   if (symbol_preemptible (fragp->fr_symbol))
26171     return 4;
26172
26173   val = relaxed_symbol_addr (fragp, stretch);
26174   addr = fragp->fr_address + fragp->fr_fix + 4;
26175   val -= addr;
26176
26177   /* Offset is a signed value *2 */
26178   limit = 1 << bits;
26179   if (val >= limit || val < -limit)
26180     return 4;
26181   return 2;
26182 }
26183
26184
26185 /* Relax a machine dependent frag.  This returns the amount by which
26186    the current size of the frag should change.  */
26187
26188 int
26189 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
26190 {
26191   int oldsize;
26192   int newsize;
26193
26194   oldsize = fragp->fr_var;
26195   switch (fragp->fr_subtype)
26196     {
26197     case T_MNEM_ldr_pc2:
26198       newsize = relax_adr (fragp, sec, stretch);
26199       break;
26200     case T_MNEM_ldr_pc:
26201     case T_MNEM_ldr_sp:
26202     case T_MNEM_str_sp:
26203       newsize = relax_immediate (fragp, 8, 2);
26204       break;
26205     case T_MNEM_ldr:
26206     case T_MNEM_str:
26207       newsize = relax_immediate (fragp, 5, 2);
26208       break;
26209     case T_MNEM_ldrh:
26210     case T_MNEM_strh:
26211       newsize = relax_immediate (fragp, 5, 1);
26212       break;
26213     case T_MNEM_ldrb:
26214     case T_MNEM_strb:
26215       newsize = relax_immediate (fragp, 5, 0);
26216       break;
26217     case T_MNEM_adr:
26218       newsize = relax_adr (fragp, sec, stretch);
26219       break;
26220     case T_MNEM_mov:
26221     case T_MNEM_movs:
26222     case T_MNEM_cmp:
26223     case T_MNEM_cmn:
26224       newsize = relax_immediate (fragp, 8, 0);
26225       break;
26226     case T_MNEM_b:
26227       newsize = relax_branch (fragp, sec, 11, stretch);
26228       break;
26229     case T_MNEM_bcond:
26230       newsize = relax_branch (fragp, sec, 8, stretch);
26231       break;
26232     case T_MNEM_add_sp:
26233     case T_MNEM_add_pc:
26234       newsize = relax_immediate (fragp, 8, 2);
26235       break;
26236     case T_MNEM_inc_sp:
26237     case T_MNEM_dec_sp:
26238       newsize = relax_immediate (fragp, 7, 2);
26239       break;
26240     case T_MNEM_addi:
26241     case T_MNEM_addis:
26242     case T_MNEM_subi:
26243     case T_MNEM_subis:
26244       newsize = relax_addsub (fragp, sec);
26245       break;
26246     default:
26247       abort ();
26248     }
26249
26250   fragp->fr_var = newsize;
26251   /* Freeze wide instructions that are at or before the same location as
26252      in the previous pass.  This avoids infinite loops.
26253      Don't freeze them unconditionally because targets may be artificially
26254      misaligned by the expansion of preceding frags.  */
26255   if (stretch <= 0 && newsize > 2)
26256     {
26257       md_convert_frag (sec->owner, sec, fragp);
26258       frag_wane (fragp);
26259     }
26260
26261   return newsize - oldsize;
26262 }
26263
26264 /* Round up a section size to the appropriate boundary.  */
26265
26266 valueT
26267 md_section_align (segT   segment ATTRIBUTE_UNUSED,
26268                   valueT size)
26269 {
26270   return size;
26271 }
26272
26273 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
26274    of an rs_align_code fragment.  */
26275
26276 void
26277 arm_handle_align (fragS * fragP)
26278 {
26279   static unsigned char const arm_noop[2][2][4] =
26280     {
26281       {  /* ARMv1 */
26282         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
26283         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
26284       },
26285       {  /* ARMv6k */
26286         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
26287         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
26288       },
26289     };
26290   static unsigned char const thumb_noop[2][2][2] =
26291     {
26292       {  /* Thumb-1 */
26293         {0xc0, 0x46},  /* LE */
26294         {0x46, 0xc0},  /* BE */
26295       },
26296       {  /* Thumb-2 */
26297         {0x00, 0xbf},  /* LE */
26298         {0xbf, 0x00}   /* BE */
26299       }
26300     };
26301   static unsigned char const wide_thumb_noop[2][4] =
26302     {  /* Wide Thumb-2 */
26303       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
26304       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
26305     };
26306
26307   unsigned bytes, fix, noop_size;
26308   char * p;
26309   const unsigned char * noop;
26310   const unsigned char *narrow_noop = NULL;
26311 #ifdef OBJ_ELF
26312   enum mstate state;
26313 #endif
26314
26315   if (fragP->fr_type != rs_align_code)
26316     return;
26317
26318   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
26319   p = fragP->fr_literal + fragP->fr_fix;
26320   fix = 0;
26321
26322   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
26323     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
26324
26325   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
26326
26327   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
26328     {
26329       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
26330                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
26331         {
26332           narrow_noop = thumb_noop[1][target_big_endian];
26333           noop = wide_thumb_noop[target_big_endian];
26334         }
26335       else
26336         noop = thumb_noop[0][target_big_endian];
26337       noop_size = 2;
26338 #ifdef OBJ_ELF
26339       state = MAP_THUMB;
26340 #endif
26341     }
26342   else
26343     {
26344       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
26345                                            ? selected_cpu : arm_arch_none,
26346                                            arm_ext_v6k) != 0]
26347                      [target_big_endian];
26348       noop_size = 4;
26349 #ifdef OBJ_ELF
26350       state = MAP_ARM;
26351 #endif
26352     }
26353
26354   fragP->fr_var = noop_size;
26355
26356   if (bytes & (noop_size - 1))
26357     {
26358       fix = bytes & (noop_size - 1);
26359 #ifdef OBJ_ELF
26360       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
26361 #endif
26362       memset (p, 0, fix);
26363       p += fix;
26364       bytes -= fix;
26365     }
26366
26367   if (narrow_noop)
26368     {
26369       if (bytes & noop_size)
26370         {
26371           /* Insert a narrow noop.  */
26372           memcpy (p, narrow_noop, noop_size);
26373           p += noop_size;
26374           bytes -= noop_size;
26375           fix += noop_size;
26376         }
26377
26378       /* Use wide noops for the remainder */
26379       noop_size = 4;
26380     }
26381
26382   while (bytes >= noop_size)
26383     {
26384       memcpy (p, noop, noop_size);
26385       p += noop_size;
26386       bytes -= noop_size;
26387       fix += noop_size;
26388     }
26389
26390   fragP->fr_fix += fix;
26391 }
26392
26393 /* Called from md_do_align.  Used to create an alignment
26394    frag in a code section.  */
26395
26396 void
26397 arm_frag_align_code (int n, int max)
26398 {
26399   char * p;
26400
26401   /* We assume that there will never be a requirement
26402      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
26403   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
26404     {
26405       char err_msg[128];
26406
26407       sprintf (err_msg,
26408         _("alignments greater than %d bytes not supported in .text sections."),
26409         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
26410       as_fatal ("%s", err_msg);
26411     }
26412
26413   p = frag_var (rs_align_code,
26414                 MAX_MEM_FOR_RS_ALIGN_CODE,
26415                 1,
26416                 (relax_substateT) max,
26417                 (symbolS *) NULL,
26418                 (offsetT) n,
26419                 (char *) NULL);
26420   *p = 0;
26421 }
26422
26423 /* Perform target specific initialisation of a frag.
26424    Note - despite the name this initialisation is not done when the frag
26425    is created, but only when its type is assigned.  A frag can be created
26426    and used a long time before its type is set, so beware of assuming that
26427    this initialisation is performed first.  */
26428
26429 #ifndef OBJ_ELF
26430 void
26431 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
26432 {
26433   /* Record whether this frag is in an ARM or a THUMB area.  */
26434   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
26435 }
26436
26437 #else /* OBJ_ELF is defined.  */
26438 void
26439 arm_init_frag (fragS * fragP, int max_chars)
26440 {
26441   bfd_boolean frag_thumb_mode;
26442
26443   /* If the current ARM vs THUMB mode has not already
26444      been recorded into this frag then do so now.  */
26445   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
26446     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
26447
26448   /* PR 21809: Do not set a mapping state for debug sections
26449      - it just confuses other tools.  */
26450   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
26451     return;
26452
26453   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
26454
26455   /* Record a mapping symbol for alignment frags.  We will delete this
26456      later if the alignment ends up empty.  */
26457   switch (fragP->fr_type)
26458     {
26459     case rs_align:
26460     case rs_align_test:
26461     case rs_fill:
26462       mapping_state_2 (MAP_DATA, max_chars);
26463       break;
26464     case rs_align_code:
26465       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
26466       break;
26467     default:
26468       break;
26469     }
26470 }
26471
26472 /* When we change sections we need to issue a new mapping symbol.  */
26473
26474 void
26475 arm_elf_change_section (void)
26476 {
26477   /* Link an unlinked unwind index table section to the .text section.  */
26478   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
26479       && elf_linked_to_section (now_seg) == NULL)
26480     elf_linked_to_section (now_seg) = text_section;
26481 }
26482
26483 int
26484 arm_elf_section_type (const char * str, size_t len)
26485 {
26486   if (len == 5 && strncmp (str, "exidx", 5) == 0)
26487     return SHT_ARM_EXIDX;
26488
26489   return -1;
26490 }
26491 \f
26492 /* Code to deal with unwinding tables.  */
26493
26494 static void add_unwind_adjustsp (offsetT);
26495
26496 /* Generate any deferred unwind frame offset.  */
26497
26498 static void
26499 flush_pending_unwind (void)
26500 {
26501   offsetT offset;
26502
26503   offset = unwind.pending_offset;
26504   unwind.pending_offset = 0;
26505   if (offset != 0)
26506     add_unwind_adjustsp (offset);
26507 }
26508
26509 /* Add an opcode to this list for this function.  Two-byte opcodes should
26510    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
26511    order.  */
26512
26513 static void
26514 add_unwind_opcode (valueT op, int length)
26515 {
26516   /* Add any deferred stack adjustment.  */
26517   if (unwind.pending_offset)
26518     flush_pending_unwind ();
26519
26520   unwind.sp_restored = 0;
26521
26522   if (unwind.opcode_count + length > unwind.opcode_alloc)
26523     {
26524       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
26525       if (unwind.opcodes)
26526         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
26527                                      unwind.opcode_alloc);
26528       else
26529         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
26530     }
26531   while (length > 0)
26532     {
26533       length--;
26534       unwind.opcodes[unwind.opcode_count] = op & 0xff;
26535       op >>= 8;
26536       unwind.opcode_count++;
26537     }
26538 }
26539
26540 /* Add unwind opcodes to adjust the stack pointer.  */
26541
26542 static void
26543 add_unwind_adjustsp (offsetT offset)
26544 {
26545   valueT op;
26546
26547   if (offset > 0x200)
26548     {
26549       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
26550       char bytes[5];
26551       int n;
26552       valueT o;
26553
26554       /* Long form: 0xb2, uleb128.  */
26555       /* This might not fit in a word so add the individual bytes,
26556          remembering the list is built in reverse order.  */
26557       o = (valueT) ((offset - 0x204) >> 2);
26558       if (o == 0)
26559         add_unwind_opcode (0, 1);
26560
26561       /* Calculate the uleb128 encoding of the offset.  */
26562       n = 0;
26563       while (o)
26564         {
26565           bytes[n] = o & 0x7f;
26566           o >>= 7;
26567           if (o)
26568             bytes[n] |= 0x80;
26569           n++;
26570         }
26571       /* Add the insn.  */
26572       for (; n; n--)
26573         add_unwind_opcode (bytes[n - 1], 1);
26574       add_unwind_opcode (0xb2, 1);
26575     }
26576   else if (offset > 0x100)
26577     {
26578       /* Two short opcodes.  */
26579       add_unwind_opcode (0x3f, 1);
26580       op = (offset - 0x104) >> 2;
26581       add_unwind_opcode (op, 1);
26582     }
26583   else if (offset > 0)
26584     {
26585       /* Short opcode.  */
26586       op = (offset - 4) >> 2;
26587       add_unwind_opcode (op, 1);
26588     }
26589   else if (offset < 0)
26590     {
26591       offset = -offset;
26592       while (offset > 0x100)
26593         {
26594           add_unwind_opcode (0x7f, 1);
26595           offset -= 0x100;
26596         }
26597       op = ((offset - 4) >> 2) | 0x40;
26598       add_unwind_opcode (op, 1);
26599     }
26600 }
26601
26602 /* Finish the list of unwind opcodes for this function.  */
26603
26604 static void
26605 finish_unwind_opcodes (void)
26606 {
26607   valueT op;
26608
26609   if (unwind.fp_used)
26610     {
26611       /* Adjust sp as necessary.  */
26612       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
26613       flush_pending_unwind ();
26614
26615       /* After restoring sp from the frame pointer.  */
26616       op = 0x90 | unwind.fp_reg;
26617       add_unwind_opcode (op, 1);
26618     }
26619   else
26620     flush_pending_unwind ();
26621 }
26622
26623
26624 /* Start an exception table entry.  If idx is nonzero this is an index table
26625    entry.  */
26626
26627 static void
26628 start_unwind_section (const segT text_seg, int idx)
26629 {
26630   const char * text_name;
26631   const char * prefix;
26632   const char * prefix_once;
26633   const char * group_name;
26634   char * sec_name;
26635   int type;
26636   int flags;
26637   int linkonce;
26638
26639   if (idx)
26640     {
26641       prefix = ELF_STRING_ARM_unwind;
26642       prefix_once = ELF_STRING_ARM_unwind_once;
26643       type = SHT_ARM_EXIDX;
26644     }
26645   else
26646     {
26647       prefix = ELF_STRING_ARM_unwind_info;
26648       prefix_once = ELF_STRING_ARM_unwind_info_once;
26649       type = SHT_PROGBITS;
26650     }
26651
26652   text_name = segment_name (text_seg);
26653   if (streq (text_name, ".text"))
26654     text_name = "";
26655
26656   if (strncmp (text_name, ".gnu.linkonce.t.",
26657                strlen (".gnu.linkonce.t.")) == 0)
26658     {
26659       prefix = prefix_once;
26660       text_name += strlen (".gnu.linkonce.t.");
26661     }
26662
26663   sec_name = concat (prefix, text_name, (char *) NULL);
26664
26665   flags = SHF_ALLOC;
26666   linkonce = 0;
26667   group_name = 0;
26668
26669   /* Handle COMDAT group.  */
26670   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
26671     {
26672       group_name = elf_group_name (text_seg);
26673       if (group_name == NULL)
26674         {
26675           as_bad (_("Group section `%s' has no group signature"),
26676                   segment_name (text_seg));
26677           ignore_rest_of_line ();
26678           return;
26679         }
26680       flags |= SHF_GROUP;
26681       linkonce = 1;
26682     }
26683
26684   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
26685                           linkonce, 0);
26686
26687   /* Set the section link for index tables.  */
26688   if (idx)
26689     elf_linked_to_section (now_seg) = text_seg;
26690 }
26691
26692
26693 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
26694    personality routine data.  Returns zero, or the index table value for
26695    an inline entry.  */
26696
26697 static valueT
26698 create_unwind_entry (int have_data)
26699 {
26700   int size;
26701   addressT where;
26702   char *ptr;
26703   /* The current word of data.  */
26704   valueT data;
26705   /* The number of bytes left in this word.  */
26706   int n;
26707
26708   finish_unwind_opcodes ();
26709
26710   /* Remember the current text section.  */
26711   unwind.saved_seg = now_seg;
26712   unwind.saved_subseg = now_subseg;
26713
26714   start_unwind_section (now_seg, 0);
26715
26716   if (unwind.personality_routine == NULL)
26717     {
26718       if (unwind.personality_index == -2)
26719         {
26720           if (have_data)
26721             as_bad (_("handlerdata in cantunwind frame"));
26722           return 1; /* EXIDX_CANTUNWIND.  */
26723         }
26724
26725       /* Use a default personality routine if none is specified.  */
26726       if (unwind.personality_index == -1)
26727         {
26728           if (unwind.opcode_count > 3)
26729             unwind.personality_index = 1;
26730           else
26731             unwind.personality_index = 0;
26732         }
26733
26734       /* Space for the personality routine entry.  */
26735       if (unwind.personality_index == 0)
26736         {
26737           if (unwind.opcode_count > 3)
26738             as_bad (_("too many unwind opcodes for personality routine 0"));
26739
26740           if (!have_data)
26741             {
26742               /* All the data is inline in the index table.  */
26743               data = 0x80;
26744               n = 3;
26745               while (unwind.opcode_count > 0)
26746                 {
26747                   unwind.opcode_count--;
26748                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
26749                   n--;
26750                 }
26751
26752               /* Pad with "finish" opcodes.  */
26753               while (n--)
26754                 data = (data << 8) | 0xb0;
26755
26756               return data;
26757             }
26758           size = 0;
26759         }
26760       else
26761         /* We get two opcodes "free" in the first word.  */
26762         size = unwind.opcode_count - 2;
26763     }
26764   else
26765     {
26766       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
26767       if (unwind.personality_index != -1)
26768         {
26769           as_bad (_("attempt to recreate an unwind entry"));
26770           return 1;
26771         }
26772
26773       /* An extra byte is required for the opcode count.        */
26774       size = unwind.opcode_count + 1;
26775     }
26776
26777   size = (size + 3) >> 2;
26778   if (size > 0xff)
26779     as_bad (_("too many unwind opcodes"));
26780
26781   frag_align (2, 0, 0);
26782   record_alignment (now_seg, 2);
26783   unwind.table_entry = expr_build_dot ();
26784
26785   /* Allocate the table entry.  */
26786   ptr = frag_more ((size << 2) + 4);
26787   /* PR 13449: Zero the table entries in case some of them are not used.  */
26788   memset (ptr, 0, (size << 2) + 4);
26789   where = frag_now_fix () - ((size << 2) + 4);
26790
26791   switch (unwind.personality_index)
26792     {
26793     case -1:
26794       /* ??? Should this be a PLT generating relocation?  */
26795       /* Custom personality routine.  */
26796       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
26797                BFD_RELOC_ARM_PREL31);
26798
26799       where += 4;
26800       ptr += 4;
26801
26802       /* Set the first byte to the number of additional words.  */
26803       data = size > 0 ? size - 1 : 0;
26804       n = 3;
26805       break;
26806
26807     /* ABI defined personality routines.  */
26808     case 0:
26809       /* Three opcodes bytes are packed into the first word.  */
26810       data = 0x80;
26811       n = 3;
26812       break;
26813
26814     case 1:
26815     case 2:
26816       /* The size and first two opcode bytes go in the first word.  */
26817       data = ((0x80 + unwind.personality_index) << 8) | size;
26818       n = 2;
26819       break;
26820
26821     default:
26822       /* Should never happen.  */
26823       abort ();
26824     }
26825
26826   /* Pack the opcodes into words (MSB first), reversing the list at the same
26827      time.  */
26828   while (unwind.opcode_count > 0)
26829     {
26830       if (n == 0)
26831         {
26832           md_number_to_chars (ptr, data, 4);
26833           ptr += 4;
26834           n = 4;
26835           data = 0;
26836         }
26837       unwind.opcode_count--;
26838       n--;
26839       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
26840     }
26841
26842   /* Finish off the last word.  */
26843   if (n < 4)
26844     {
26845       /* Pad with "finish" opcodes.  */
26846       while (n--)
26847         data = (data << 8) | 0xb0;
26848
26849       md_number_to_chars (ptr, data, 4);
26850     }
26851
26852   if (!have_data)
26853     {
26854       /* Add an empty descriptor if there is no user-specified data.   */
26855       ptr = frag_more (4);
26856       md_number_to_chars (ptr, 0, 4);
26857     }
26858
26859   return 0;
26860 }
26861
26862
26863 /* Initialize the DWARF-2 unwind information for this procedure.  */
26864
26865 void
26866 tc_arm_frame_initial_instructions (void)
26867 {
26868   cfi_add_CFA_def_cfa (REG_SP, 0);
26869 }
26870 #endif /* OBJ_ELF */
26871
26872 /* Convert REGNAME to a DWARF-2 register number.  */
26873
26874 int
26875 tc_arm_regname_to_dw2regnum (char *regname)
26876 {
26877   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
26878   if (reg != FAIL)
26879     return reg;
26880
26881   /* PR 16694: Allow VFP registers as well.  */
26882   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
26883   if (reg != FAIL)
26884     return 64 + reg;
26885
26886   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
26887   if (reg != FAIL)
26888     return reg + 256;
26889
26890   return FAIL;
26891 }
26892
26893 #ifdef TE_PE
26894 void
26895 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
26896 {
26897   expressionS exp;
26898
26899   exp.X_op = O_secrel;
26900   exp.X_add_symbol = symbol;
26901   exp.X_add_number = 0;
26902   emit_expr (&exp, size);
26903 }
26904 #endif
26905
26906 /* MD interface: Symbol and relocation handling.  */
26907
26908 /* Return the address within the segment that a PC-relative fixup is
26909    relative to.  For ARM, PC-relative fixups applied to instructions
26910    are generally relative to the location of the fixup plus 8 bytes.
26911    Thumb branches are offset by 4, and Thumb loads relative to PC
26912    require special handling.  */
26913
26914 long
26915 md_pcrel_from_section (fixS * fixP, segT seg)
26916 {
26917   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
26918
26919   /* If this is pc-relative and we are going to emit a relocation
26920      then we just want to put out any pipeline compensation that the linker
26921      will need.  Otherwise we want to use the calculated base.
26922      For WinCE we skip the bias for externals as well, since this
26923      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
26924   if (fixP->fx_pcrel
26925       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
26926           || (arm_force_relocation (fixP)
26927 #ifdef TE_WINCE
26928               && !S_IS_EXTERNAL (fixP->fx_addsy)
26929 #endif
26930               )))
26931     base = 0;
26932
26933
26934   switch (fixP->fx_r_type)
26935     {
26936       /* PC relative addressing on the Thumb is slightly odd as the
26937          bottom two bits of the PC are forced to zero for the
26938          calculation.  This happens *after* application of the
26939          pipeline offset.  However, Thumb adrl already adjusts for
26940          this, so we need not do it again.  */
26941     case BFD_RELOC_ARM_THUMB_ADD:
26942       return base & ~3;
26943
26944     case BFD_RELOC_ARM_THUMB_OFFSET:
26945     case BFD_RELOC_ARM_T32_OFFSET_IMM:
26946     case BFD_RELOC_ARM_T32_ADD_PC12:
26947     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
26948       return (base + 4) & ~3;
26949
26950       /* Thumb branches are simply offset by +4.  */
26951     case BFD_RELOC_THUMB_PCREL_BRANCH5:
26952     case BFD_RELOC_THUMB_PCREL_BRANCH7:
26953     case BFD_RELOC_THUMB_PCREL_BRANCH9:
26954     case BFD_RELOC_THUMB_PCREL_BRANCH12:
26955     case BFD_RELOC_THUMB_PCREL_BRANCH20:
26956     case BFD_RELOC_THUMB_PCREL_BRANCH25:
26957     case BFD_RELOC_THUMB_PCREL_BFCSEL:
26958     case BFD_RELOC_ARM_THUMB_BF17:
26959     case BFD_RELOC_ARM_THUMB_BF19:
26960     case BFD_RELOC_ARM_THUMB_BF13:
26961     case BFD_RELOC_ARM_THUMB_LOOP12:
26962       return base + 4;
26963
26964     case BFD_RELOC_THUMB_PCREL_BRANCH23:
26965       if (fixP->fx_addsy
26966           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26967           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26968           && ARM_IS_FUNC (fixP->fx_addsy)
26969           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26970         base = fixP->fx_where + fixP->fx_frag->fr_address;
26971        return base + 4;
26972
26973       /* BLX is like branches above, but forces the low two bits of PC to
26974          zero.  */
26975     case BFD_RELOC_THUMB_PCREL_BLX:
26976       if (fixP->fx_addsy
26977           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26978           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26979           && THUMB_IS_FUNC (fixP->fx_addsy)
26980           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26981         base = fixP->fx_where + fixP->fx_frag->fr_address;
26982       return (base + 4) & ~3;
26983
26984       /* ARM mode branches are offset by +8.  However, the Windows CE
26985          loader expects the relocation not to take this into account.  */
26986     case BFD_RELOC_ARM_PCREL_BLX:
26987       if (fixP->fx_addsy
26988           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26989           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26990           && ARM_IS_FUNC (fixP->fx_addsy)
26991           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26992         base = fixP->fx_where + fixP->fx_frag->fr_address;
26993       return base + 8;
26994
26995     case BFD_RELOC_ARM_PCREL_CALL:
26996       if (fixP->fx_addsy
26997           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26998           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26999           && THUMB_IS_FUNC (fixP->fx_addsy)
27000           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
27001         base = fixP->fx_where + fixP->fx_frag->fr_address;
27002       return base + 8;
27003
27004     case BFD_RELOC_ARM_PCREL_BRANCH:
27005     case BFD_RELOC_ARM_PCREL_JUMP:
27006     case BFD_RELOC_ARM_PLT32:
27007 #ifdef TE_WINCE
27008       /* When handling fixups immediately, because we have already
27009          discovered the value of a symbol, or the address of the frag involved
27010          we must account for the offset by +8, as the OS loader will never see the reloc.
27011          see fixup_segment() in write.c
27012          The S_IS_EXTERNAL test handles the case of global symbols.
27013          Those need the calculated base, not just the pipe compensation the linker will need.  */
27014       if (fixP->fx_pcrel
27015           && fixP->fx_addsy != NULL
27016           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27017           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
27018         return base + 8;
27019       return base;
27020 #else
27021       return base + 8;
27022 #endif
27023
27024
27025       /* ARM mode loads relative to PC are also offset by +8.  Unlike
27026          branches, the Windows CE loader *does* expect the relocation
27027          to take this into account.  */
27028     case BFD_RELOC_ARM_OFFSET_IMM:
27029     case BFD_RELOC_ARM_OFFSET_IMM8:
27030     case BFD_RELOC_ARM_HWLITERAL:
27031     case BFD_RELOC_ARM_LITERAL:
27032     case BFD_RELOC_ARM_CP_OFF_IMM:
27033       return base + 8;
27034
27035
27036       /* Other PC-relative relocations are un-offset.  */
27037     default:
27038       return base;
27039     }
27040 }
27041
27042 static bfd_boolean flag_warn_syms = TRUE;
27043
27044 bfd_boolean
27045 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
27046 {
27047   /* PR 18347 - Warn if the user attempts to create a symbol with the same
27048      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
27049      does mean that the resulting code might be very confusing to the reader.
27050      Also this warning can be triggered if the user omits an operand before
27051      an immediate address, eg:
27052
27053        LDR =foo
27054
27055      GAS treats this as an assignment of the value of the symbol foo to a
27056      symbol LDR, and so (without this code) it will not issue any kind of
27057      warning or error message.
27058
27059      Note - ARM instructions are case-insensitive but the strings in the hash
27060      table are all stored in lower case, so we must first ensure that name is
27061      lower case too.  */
27062   if (flag_warn_syms && arm_ops_hsh)
27063     {
27064       char * nbuf = strdup (name);
27065       char * p;
27066
27067       for (p = nbuf; *p; p++)
27068         *p = TOLOWER (*p);
27069       if (hash_find (arm_ops_hsh, nbuf) != NULL)
27070         {
27071           static struct hash_control * already_warned = NULL;
27072
27073           if (already_warned == NULL)
27074             already_warned = hash_new ();
27075           /* Only warn about the symbol once.  To keep the code
27076              simple we let hash_insert do the lookup for us.  */
27077           if (hash_insert (already_warned, nbuf, NULL) == NULL)
27078             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
27079         }
27080       else
27081         free (nbuf);
27082     }
27083
27084   return FALSE;
27085 }
27086
27087 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
27088    Otherwise we have no need to default values of symbols.  */
27089
27090 symbolS *
27091 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
27092 {
27093 #ifdef OBJ_ELF
27094   if (name[0] == '_' && name[1] == 'G'
27095       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
27096     {
27097       if (!GOT_symbol)
27098         {
27099           if (symbol_find (name))
27100             as_bad (_("GOT already in the symbol table"));
27101
27102           GOT_symbol = symbol_new (name, undefined_section,
27103                                    (valueT) 0, & zero_address_frag);
27104         }
27105
27106       return GOT_symbol;
27107     }
27108 #endif
27109
27110   return NULL;
27111 }
27112
27113 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
27114    computed as two separate immediate values, added together.  We
27115    already know that this value cannot be computed by just one ARM
27116    instruction.  */
27117
27118 static unsigned int
27119 validate_immediate_twopart (unsigned int   val,
27120                             unsigned int * highpart)
27121 {
27122   unsigned int a;
27123   unsigned int i;
27124
27125   for (i = 0; i < 32; i += 2)
27126     if (((a = rotate_left (val, i)) & 0xff) != 0)
27127       {
27128         if (a & 0xff00)
27129           {
27130             if (a & ~ 0xffff)
27131               continue;
27132             * highpart = (a  >> 8) | ((i + 24) << 7);
27133           }
27134         else if (a & 0xff0000)
27135           {
27136             if (a & 0xff000000)
27137               continue;
27138             * highpart = (a >> 16) | ((i + 16) << 7);
27139           }
27140         else
27141           {
27142             gas_assert (a & 0xff000000);
27143             * highpart = (a >> 24) | ((i + 8) << 7);
27144           }
27145
27146         return (a & 0xff) | (i << 7);
27147       }
27148
27149   return FAIL;
27150 }
27151
27152 static int
27153 validate_offset_imm (unsigned int val, int hwse)
27154 {
27155   if ((hwse && val > 255) || val > 4095)
27156     return FAIL;
27157   return val;
27158 }
27159
27160 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
27161    negative immediate constant by altering the instruction.  A bit of
27162    a hack really.
27163         MOV <-> MVN
27164         AND <-> BIC
27165         ADC <-> SBC
27166         by inverting the second operand, and
27167         ADD <-> SUB
27168         CMP <-> CMN
27169         by negating the second operand.  */
27170
27171 static int
27172 negate_data_op (unsigned long * instruction,
27173                 unsigned long   value)
27174 {
27175   int op, new_inst;
27176   unsigned long negated, inverted;
27177
27178   negated = encode_arm_immediate (-value);
27179   inverted = encode_arm_immediate (~value);
27180
27181   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
27182   switch (op)
27183     {
27184       /* First negates.  */
27185     case OPCODE_SUB:             /* ADD <-> SUB  */
27186       new_inst = OPCODE_ADD;
27187       value = negated;
27188       break;
27189
27190     case OPCODE_ADD:
27191       new_inst = OPCODE_SUB;
27192       value = negated;
27193       break;
27194
27195     case OPCODE_CMP:             /* CMP <-> CMN  */
27196       new_inst = OPCODE_CMN;
27197       value = negated;
27198       break;
27199
27200     case OPCODE_CMN:
27201       new_inst = OPCODE_CMP;
27202       value = negated;
27203       break;
27204
27205       /* Now Inverted ops.  */
27206     case OPCODE_MOV:             /* MOV <-> MVN  */
27207       new_inst = OPCODE_MVN;
27208       value = inverted;
27209       break;
27210
27211     case OPCODE_MVN:
27212       new_inst = OPCODE_MOV;
27213       value = inverted;
27214       break;
27215
27216     case OPCODE_AND:             /* AND <-> BIC  */
27217       new_inst = OPCODE_BIC;
27218       value = inverted;
27219       break;
27220
27221     case OPCODE_BIC:
27222       new_inst = OPCODE_AND;
27223       value = inverted;
27224       break;
27225
27226     case OPCODE_ADC:              /* ADC <-> SBC  */
27227       new_inst = OPCODE_SBC;
27228       value = inverted;
27229       break;
27230
27231     case OPCODE_SBC:
27232       new_inst = OPCODE_ADC;
27233       value = inverted;
27234       break;
27235
27236       /* We cannot do anything.  */
27237     default:
27238       return FAIL;
27239     }
27240
27241   if (value == (unsigned) FAIL)
27242     return FAIL;
27243
27244   *instruction &= OPCODE_MASK;
27245   *instruction |= new_inst << DATA_OP_SHIFT;
27246   return value;
27247 }
27248
27249 /* Like negate_data_op, but for Thumb-2.   */
27250
27251 static unsigned int
27252 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
27253 {
27254   int op, new_inst;
27255   int rd;
27256   unsigned int negated, inverted;
27257
27258   negated = encode_thumb32_immediate (-value);
27259   inverted = encode_thumb32_immediate (~value);
27260
27261   rd = (*instruction >> 8) & 0xf;
27262   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
27263   switch (op)
27264     {
27265       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
27266     case T2_OPCODE_SUB:
27267       new_inst = T2_OPCODE_ADD;
27268       value = negated;
27269       break;
27270
27271     case T2_OPCODE_ADD:
27272       new_inst = T2_OPCODE_SUB;
27273       value = negated;
27274       break;
27275
27276       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
27277     case T2_OPCODE_ORR:
27278       new_inst = T2_OPCODE_ORN;
27279       value = inverted;
27280       break;
27281
27282     case T2_OPCODE_ORN:
27283       new_inst = T2_OPCODE_ORR;
27284       value = inverted;
27285       break;
27286
27287       /* AND <-> BIC.  TST has no inverted equivalent.  */
27288     case T2_OPCODE_AND:
27289       new_inst = T2_OPCODE_BIC;
27290       if (rd == 15)
27291         value = FAIL;
27292       else
27293         value = inverted;
27294       break;
27295
27296     case T2_OPCODE_BIC:
27297       new_inst = T2_OPCODE_AND;
27298       value = inverted;
27299       break;
27300
27301       /* ADC <-> SBC  */
27302     case T2_OPCODE_ADC:
27303       new_inst = T2_OPCODE_SBC;
27304       value = inverted;
27305       break;
27306
27307     case T2_OPCODE_SBC:
27308       new_inst = T2_OPCODE_ADC;
27309       value = inverted;
27310       break;
27311
27312       /* We cannot do anything.  */
27313     default:
27314       return FAIL;
27315     }
27316
27317   if (value == (unsigned int)FAIL)
27318     return FAIL;
27319
27320   *instruction &= T2_OPCODE_MASK;
27321   *instruction |= new_inst << T2_DATA_OP_SHIFT;
27322   return value;
27323 }
27324
27325 /* Read a 32-bit thumb instruction from buf.  */
27326
27327 static unsigned long
27328 get_thumb32_insn (char * buf)
27329 {
27330   unsigned long insn;
27331   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
27332   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27333
27334   return insn;
27335 }
27336
27337 /* We usually want to set the low bit on the address of thumb function
27338    symbols.  In particular .word foo - . should have the low bit set.
27339    Generic code tries to fold the difference of two symbols to
27340    a constant.  Prevent this and force a relocation when the first symbols
27341    is a thumb function.  */
27342
27343 bfd_boolean
27344 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
27345 {
27346   if (op == O_subtract
27347       && l->X_op == O_symbol
27348       && r->X_op == O_symbol
27349       && THUMB_IS_FUNC (l->X_add_symbol))
27350     {
27351       l->X_op = O_subtract;
27352       l->X_op_symbol = r->X_add_symbol;
27353       l->X_add_number -= r->X_add_number;
27354       return TRUE;
27355     }
27356
27357   /* Process as normal.  */
27358   return FALSE;
27359 }
27360
27361 /* Encode Thumb2 unconditional branches and calls. The encoding
27362    for the 2 are identical for the immediate values.  */
27363
27364 static void
27365 encode_thumb2_b_bl_offset (char * buf, offsetT value)
27366 {
27367 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
27368   offsetT newval;
27369   offsetT newval2;
27370   addressT S, I1, I2, lo, hi;
27371
27372   S = (value >> 24) & 0x01;
27373   I1 = (value >> 23) & 0x01;
27374   I2 = (value >> 22) & 0x01;
27375   hi = (value >> 12) & 0x3ff;
27376   lo = (value >> 1) & 0x7ff;
27377   newval   = md_chars_to_number (buf, THUMB_SIZE);
27378   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27379   newval  |= (S << 10) | hi;
27380   newval2 &=  ~T2I1I2MASK;
27381   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
27382   md_number_to_chars (buf, newval, THUMB_SIZE);
27383   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
27384 }
27385
27386 void
27387 md_apply_fix (fixS *    fixP,
27388                valueT * valP,
27389                segT     seg)
27390 {
27391   offsetT        value = * valP;
27392   offsetT        newval;
27393   unsigned int   newimm;
27394   unsigned long  temp;
27395   int            sign;
27396   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
27397
27398   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
27399
27400   /* Note whether this will delete the relocation.  */
27401
27402   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
27403     fixP->fx_done = 1;
27404
27405   /* On a 64-bit host, silently truncate 'value' to 32 bits for
27406      consistency with the behaviour on 32-bit hosts.  Remember value
27407      for emit_reloc.  */
27408   value &= 0xffffffff;
27409   value ^= 0x80000000;
27410   value -= 0x80000000;
27411
27412   *valP = value;
27413   fixP->fx_addnumber = value;
27414
27415   /* Same treatment for fixP->fx_offset.  */
27416   fixP->fx_offset &= 0xffffffff;
27417   fixP->fx_offset ^= 0x80000000;
27418   fixP->fx_offset -= 0x80000000;
27419
27420   switch (fixP->fx_r_type)
27421     {
27422     case BFD_RELOC_NONE:
27423       /* This will need to go in the object file.  */
27424       fixP->fx_done = 0;
27425       break;
27426
27427     case BFD_RELOC_ARM_IMMEDIATE:
27428       /* We claim that this fixup has been processed here,
27429          even if in fact we generate an error because we do
27430          not have a reloc for it, so tc_gen_reloc will reject it.  */
27431       fixP->fx_done = 1;
27432
27433       if (fixP->fx_addsy)
27434         {
27435           const char *msg = 0;
27436
27437           if (! S_IS_DEFINED (fixP->fx_addsy))
27438             msg = _("undefined symbol %s used as an immediate value");
27439           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
27440             msg = _("symbol %s is in a different section");
27441           else if (S_IS_WEAK (fixP->fx_addsy))
27442             msg = _("symbol %s is weak and may be overridden later");
27443
27444           if (msg)
27445             {
27446               as_bad_where (fixP->fx_file, fixP->fx_line,
27447                             msg, S_GET_NAME (fixP->fx_addsy));
27448               break;
27449             }
27450         }
27451
27452       temp = md_chars_to_number (buf, INSN_SIZE);
27453
27454       /* If the offset is negative, we should use encoding A2 for ADR.  */
27455       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
27456         newimm = negate_data_op (&temp, value);
27457       else
27458         {
27459           newimm = encode_arm_immediate (value);
27460
27461           /* If the instruction will fail, see if we can fix things up by
27462              changing the opcode.  */
27463           if (newimm == (unsigned int) FAIL)
27464             newimm = negate_data_op (&temp, value);
27465           /* MOV accepts both ARM modified immediate (A1 encoding) and
27466              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
27467              When disassembling, MOV is preferred when there is no encoding
27468              overlap.  */
27469           if (newimm == (unsigned int) FAIL
27470               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
27471               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
27472               && !((temp >> SBIT_SHIFT) & 0x1)
27473               && value >= 0 && value <= 0xffff)
27474             {
27475               /* Clear bits[23:20] to change encoding from A1 to A2.  */
27476               temp &= 0xff0fffff;
27477               /* Encoding high 4bits imm.  Code below will encode the remaining
27478                  low 12bits.  */
27479               temp |= (value & 0x0000f000) << 4;
27480               newimm = value & 0x00000fff;
27481             }
27482         }
27483
27484       if (newimm == (unsigned int) FAIL)
27485         {
27486           as_bad_where (fixP->fx_file, fixP->fx_line,
27487                         _("invalid constant (%lx) after fixup"),
27488                         (unsigned long) value);
27489           break;
27490         }
27491
27492       newimm |= (temp & 0xfffff000);
27493       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
27494       break;
27495
27496     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
27497       {
27498         unsigned int highpart = 0;
27499         unsigned int newinsn  = 0xe1a00000; /* nop.  */
27500
27501         if (fixP->fx_addsy)
27502           {
27503             const char *msg = 0;
27504
27505             if (! S_IS_DEFINED (fixP->fx_addsy))
27506               msg = _("undefined symbol %s used as an immediate value");
27507             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
27508               msg = _("symbol %s is in a different section");
27509             else if (S_IS_WEAK (fixP->fx_addsy))
27510               msg = _("symbol %s is weak and may be overridden later");
27511
27512             if (msg)
27513               {
27514                 as_bad_where (fixP->fx_file, fixP->fx_line,
27515                               msg, S_GET_NAME (fixP->fx_addsy));
27516                 break;
27517               }
27518           }
27519
27520         newimm = encode_arm_immediate (value);
27521         temp = md_chars_to_number (buf, INSN_SIZE);
27522
27523         /* If the instruction will fail, see if we can fix things up by
27524            changing the opcode.  */
27525         if (newimm == (unsigned int) FAIL
27526             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
27527           {
27528             /* No ?  OK - try using two ADD instructions to generate
27529                the value.  */
27530             newimm = validate_immediate_twopart (value, & highpart);
27531
27532             /* Yes - then make sure that the second instruction is
27533                also an add.  */
27534             if (newimm != (unsigned int) FAIL)
27535               newinsn = temp;
27536             /* Still No ?  Try using a negated value.  */
27537             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
27538               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
27539             /* Otherwise - give up.  */
27540             else
27541               {
27542                 as_bad_where (fixP->fx_file, fixP->fx_line,
27543                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
27544                               (long) value);
27545                 break;
27546               }
27547
27548             /* Replace the first operand in the 2nd instruction (which
27549                is the PC) with the destination register.  We have
27550                already added in the PC in the first instruction and we
27551                do not want to do it again.  */
27552             newinsn &= ~ 0xf0000;
27553             newinsn |= ((newinsn & 0x0f000) << 4);
27554           }
27555
27556         newimm |= (temp & 0xfffff000);
27557         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
27558
27559         highpart |= (newinsn & 0xfffff000);
27560         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
27561       }
27562       break;
27563
27564     case BFD_RELOC_ARM_OFFSET_IMM:
27565       if (!fixP->fx_done && seg->use_rela_p)
27566         value = 0;
27567       /* Fall through.  */
27568
27569     case BFD_RELOC_ARM_LITERAL:
27570       sign = value > 0;
27571
27572       if (value < 0)
27573         value = - value;
27574
27575       if (validate_offset_imm (value, 0) == FAIL)
27576         {
27577           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
27578             as_bad_where (fixP->fx_file, fixP->fx_line,
27579                           _("invalid literal constant: pool needs to be closer"));
27580           else
27581             as_bad_where (fixP->fx_file, fixP->fx_line,
27582                           _("bad immediate value for offset (%ld)"),
27583                           (long) value);
27584           break;
27585         }
27586
27587       newval = md_chars_to_number (buf, INSN_SIZE);
27588       if (value == 0)
27589         newval &= 0xfffff000;
27590       else
27591         {
27592           newval &= 0xff7ff000;
27593           newval |= value | (sign ? INDEX_UP : 0);
27594         }
27595       md_number_to_chars (buf, newval, INSN_SIZE);
27596       break;
27597
27598     case BFD_RELOC_ARM_OFFSET_IMM8:
27599     case BFD_RELOC_ARM_HWLITERAL:
27600       sign = value > 0;
27601
27602       if (value < 0)
27603         value = - value;
27604
27605       if (validate_offset_imm (value, 1) == FAIL)
27606         {
27607           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
27608             as_bad_where (fixP->fx_file, fixP->fx_line,
27609                           _("invalid literal constant: pool needs to be closer"));
27610           else
27611             as_bad_where (fixP->fx_file, fixP->fx_line,
27612                           _("bad immediate value for 8-bit offset (%ld)"),
27613                           (long) value);
27614           break;
27615         }
27616
27617       newval = md_chars_to_number (buf, INSN_SIZE);
27618       if (value == 0)
27619         newval &= 0xfffff0f0;
27620       else
27621         {
27622           newval &= 0xff7ff0f0;
27623           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
27624         }
27625       md_number_to_chars (buf, newval, INSN_SIZE);
27626       break;
27627
27628     case BFD_RELOC_ARM_T32_OFFSET_U8:
27629       if (value < 0 || value > 1020 || value % 4 != 0)
27630         as_bad_where (fixP->fx_file, fixP->fx_line,
27631                       _("bad immediate value for offset (%ld)"), (long) value);
27632       value /= 4;
27633
27634       newval = md_chars_to_number (buf+2, THUMB_SIZE);
27635       newval |= value;
27636       md_number_to_chars (buf+2, newval, THUMB_SIZE);
27637       break;
27638
27639     case BFD_RELOC_ARM_T32_OFFSET_IMM:
27640       /* This is a complicated relocation used for all varieties of Thumb32
27641          load/store instruction with immediate offset:
27642
27643          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
27644                                                    *4, optional writeback(W)
27645                                                    (doubleword load/store)
27646
27647          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
27648          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
27649          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
27650          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
27651          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
27652
27653          Uppercase letters indicate bits that are already encoded at
27654          this point.  Lowercase letters are our problem.  For the
27655          second block of instructions, the secondary opcode nybble
27656          (bits 8..11) is present, and bit 23 is zero, even if this is
27657          a PC-relative operation.  */
27658       newval = md_chars_to_number (buf, THUMB_SIZE);
27659       newval <<= 16;
27660       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
27661
27662       if ((newval & 0xf0000000) == 0xe0000000)
27663         {
27664           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
27665           if (value >= 0)
27666             newval |= (1 << 23);
27667           else
27668             value = -value;
27669           if (value % 4 != 0)
27670             {
27671               as_bad_where (fixP->fx_file, fixP->fx_line,
27672                             _("offset not a multiple of 4"));
27673               break;
27674             }
27675           value /= 4;
27676           if (value > 0xff)
27677             {
27678               as_bad_where (fixP->fx_file, fixP->fx_line,
27679                             _("offset out of range"));
27680               break;
27681             }
27682           newval &= ~0xff;
27683         }
27684       else if ((newval & 0x000f0000) == 0x000f0000)
27685         {
27686           /* PC-relative, 12-bit offset.  */
27687           if (value >= 0)
27688             newval |= (1 << 23);
27689           else
27690             value = -value;
27691           if (value > 0xfff)
27692             {
27693               as_bad_where (fixP->fx_file, fixP->fx_line,
27694                             _("offset out of range"));
27695               break;
27696             }
27697           newval &= ~0xfff;
27698         }
27699       else if ((newval & 0x00000100) == 0x00000100)
27700         {
27701           /* Writeback: 8-bit, +/- offset.  */
27702           if (value >= 0)
27703             newval |= (1 << 9);
27704           else
27705             value = -value;
27706           if (value > 0xff)
27707             {
27708               as_bad_where (fixP->fx_file, fixP->fx_line,
27709                             _("offset out of range"));
27710               break;
27711             }
27712           newval &= ~0xff;
27713         }
27714       else if ((newval & 0x00000f00) == 0x00000e00)
27715         {
27716           /* T-instruction: positive 8-bit offset.  */
27717           if (value < 0 || value > 0xff)
27718             {
27719               as_bad_where (fixP->fx_file, fixP->fx_line,
27720                             _("offset out of range"));
27721               break;
27722             }
27723           newval &= ~0xff;
27724           newval |= value;
27725         }
27726       else
27727         {
27728           /* Positive 12-bit or negative 8-bit offset.  */
27729           int limit;
27730           if (value >= 0)
27731             {
27732               newval |= (1 << 23);
27733               limit = 0xfff;
27734             }
27735           else
27736             {
27737               value = -value;
27738               limit = 0xff;
27739             }
27740           if (value > limit)
27741             {
27742               as_bad_where (fixP->fx_file, fixP->fx_line,
27743                             _("offset out of range"));
27744               break;
27745             }
27746           newval &= ~limit;
27747         }
27748
27749       newval |= value;
27750       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
27751       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
27752       break;
27753
27754     case BFD_RELOC_ARM_SHIFT_IMM:
27755       newval = md_chars_to_number (buf, INSN_SIZE);
27756       if (((unsigned long) value) > 32
27757           || (value == 32
27758               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
27759         {
27760           as_bad_where (fixP->fx_file, fixP->fx_line,
27761                         _("shift expression is too large"));
27762           break;
27763         }
27764
27765       if (value == 0)
27766         /* Shifts of zero must be done as lsl.  */
27767         newval &= ~0x60;
27768       else if (value == 32)
27769         value = 0;
27770       newval &= 0xfffff07f;
27771       newval |= (value & 0x1f) << 7;
27772       md_number_to_chars (buf, newval, INSN_SIZE);
27773       break;
27774
27775     case BFD_RELOC_ARM_T32_IMMEDIATE:
27776     case BFD_RELOC_ARM_T32_ADD_IMM:
27777     case BFD_RELOC_ARM_T32_IMM12:
27778     case BFD_RELOC_ARM_T32_ADD_PC12:
27779       /* We claim that this fixup has been processed here,
27780          even if in fact we generate an error because we do
27781          not have a reloc for it, so tc_gen_reloc will reject it.  */
27782       fixP->fx_done = 1;
27783
27784       if (fixP->fx_addsy
27785           && ! S_IS_DEFINED (fixP->fx_addsy))
27786         {
27787           as_bad_where (fixP->fx_file, fixP->fx_line,
27788                         _("undefined symbol %s used as an immediate value"),
27789                         S_GET_NAME (fixP->fx_addsy));
27790           break;
27791         }
27792
27793       newval = md_chars_to_number (buf, THUMB_SIZE);
27794       newval <<= 16;
27795       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
27796
27797       newimm = FAIL;
27798       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
27799            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
27800               Thumb2 modified immediate encoding (T2).  */
27801            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
27802           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
27803         {
27804           newimm = encode_thumb32_immediate (value);
27805           if (newimm == (unsigned int) FAIL)
27806             newimm = thumb32_negate_data_op (&newval, value);
27807         }
27808       if (newimm == (unsigned int) FAIL)
27809         {
27810           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
27811             {
27812               /* Turn add/sum into addw/subw.  */
27813               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
27814                 newval = (newval & 0xfeffffff) | 0x02000000;
27815               /* No flat 12-bit imm encoding for addsw/subsw.  */
27816               if ((newval & 0x00100000) == 0)
27817                 {
27818                   /* 12 bit immediate for addw/subw.  */
27819                   if (value < 0)
27820                     {
27821                       value = -value;
27822                       newval ^= 0x00a00000;
27823                     }
27824                   if (value > 0xfff)
27825                     newimm = (unsigned int) FAIL;
27826                   else
27827                     newimm = value;
27828                 }
27829             }
27830           else
27831             {
27832               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
27833                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
27834                  disassembling, MOV is preferred when there is no encoding
27835                  overlap.  */
27836               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
27837                   /* NOTE: MOV uses the ORR opcode in Thumb 2 mode
27838                      but with the Rn field [19:16] set to 1111.  */
27839                   && (((newval >> 16) & 0xf) == 0xf)
27840                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
27841                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
27842                   && value >= 0 && value <= 0xffff)
27843                 {
27844                   /* Toggle bit[25] to change encoding from T2 to T3.  */
27845                   newval ^= 1 << 25;
27846                   /* Clear bits[19:16].  */
27847                   newval &= 0xfff0ffff;
27848                   /* Encoding high 4bits imm.  Code below will encode the
27849                      remaining low 12bits.  */
27850                   newval |= (value & 0x0000f000) << 4;
27851                   newimm = value & 0x00000fff;
27852                 }
27853             }
27854         }
27855
27856       if (newimm == (unsigned int)FAIL)
27857         {
27858           as_bad_where (fixP->fx_file, fixP->fx_line,
27859                         _("invalid constant (%lx) after fixup"),
27860                         (unsigned long) value);
27861           break;
27862         }
27863
27864       newval |= (newimm & 0x800) << 15;
27865       newval |= (newimm & 0x700) << 4;
27866       newval |= (newimm & 0x0ff);
27867
27868       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
27869       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
27870       break;
27871
27872     case BFD_RELOC_ARM_SMC:
27873       if (((unsigned long) value) > 0xf)
27874         as_bad_where (fixP->fx_file, fixP->fx_line,
27875                       _("invalid smc expression"));
27876
27877       newval = md_chars_to_number (buf, INSN_SIZE);
27878       newval |= (value & 0xf);
27879       md_number_to_chars (buf, newval, INSN_SIZE);
27880       break;
27881
27882     case BFD_RELOC_ARM_HVC:
27883       if (((unsigned long) value) > 0xffff)
27884         as_bad_where (fixP->fx_file, fixP->fx_line,
27885                       _("invalid hvc expression"));
27886       newval = md_chars_to_number (buf, INSN_SIZE);
27887       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
27888       md_number_to_chars (buf, newval, INSN_SIZE);
27889       break;
27890
27891     case BFD_RELOC_ARM_SWI:
27892       if (fixP->tc_fix_data != 0)
27893         {
27894           if (((unsigned long) value) > 0xff)
27895             as_bad_where (fixP->fx_file, fixP->fx_line,
27896                           _("invalid swi expression"));
27897           newval = md_chars_to_number (buf, THUMB_SIZE);
27898           newval |= value;
27899           md_number_to_chars (buf, newval, THUMB_SIZE);
27900         }
27901       else
27902         {
27903           if (((unsigned long) value) > 0x00ffffff)
27904             as_bad_where (fixP->fx_file, fixP->fx_line,
27905                           _("invalid swi expression"));
27906           newval = md_chars_to_number (buf, INSN_SIZE);
27907           newval |= value;
27908           md_number_to_chars (buf, newval, INSN_SIZE);
27909         }
27910       break;
27911
27912     case BFD_RELOC_ARM_MULTI:
27913       if (((unsigned long) value) > 0xffff)
27914         as_bad_where (fixP->fx_file, fixP->fx_line,
27915                       _("invalid expression in load/store multiple"));
27916       newval = value | md_chars_to_number (buf, INSN_SIZE);
27917       md_number_to_chars (buf, newval, INSN_SIZE);
27918       break;
27919
27920 #ifdef OBJ_ELF
27921     case BFD_RELOC_ARM_PCREL_CALL:
27922
27923       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27924           && fixP->fx_addsy
27925           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27926           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27927           && THUMB_IS_FUNC (fixP->fx_addsy))
27928         /* Flip the bl to blx. This is a simple flip
27929            bit here because we generate PCREL_CALL for
27930            unconditional bls.  */
27931         {
27932           newval = md_chars_to_number (buf, INSN_SIZE);
27933           newval = newval | 0x10000000;
27934           md_number_to_chars (buf, newval, INSN_SIZE);
27935           temp = 1;
27936           fixP->fx_done = 1;
27937         }
27938       else
27939         temp = 3;
27940       goto arm_branch_common;
27941
27942     case BFD_RELOC_ARM_PCREL_JUMP:
27943       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27944           && fixP->fx_addsy
27945           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27946           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27947           && THUMB_IS_FUNC (fixP->fx_addsy))
27948         {
27949           /* This would map to a bl<cond>, b<cond>,
27950              b<always> to a Thumb function. We
27951              need to force a relocation for this particular
27952              case.  */
27953           newval = md_chars_to_number (buf, INSN_SIZE);
27954           fixP->fx_done = 0;
27955         }
27956       /* Fall through.  */
27957
27958     case BFD_RELOC_ARM_PLT32:
27959 #endif
27960     case BFD_RELOC_ARM_PCREL_BRANCH:
27961       temp = 3;
27962       goto arm_branch_common;
27963
27964     case BFD_RELOC_ARM_PCREL_BLX:
27965
27966       temp = 1;
27967       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27968           && fixP->fx_addsy
27969           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27970           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27971           && ARM_IS_FUNC (fixP->fx_addsy))
27972         {
27973           /* Flip the blx to a bl and warn.  */
27974           const char *name = S_GET_NAME (fixP->fx_addsy);
27975           newval = 0xeb000000;
27976           as_warn_where (fixP->fx_file, fixP->fx_line,
27977                          _("blx to '%s' an ARM ISA state function changed to bl"),
27978                           name);
27979           md_number_to_chars (buf, newval, INSN_SIZE);
27980           temp = 3;
27981           fixP->fx_done = 1;
27982         }
27983
27984 #ifdef OBJ_ELF
27985        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
27986          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
27987 #endif
27988
27989     arm_branch_common:
27990       /* We are going to store value (shifted right by two) in the
27991          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
27992          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
27993          also be clear.  */
27994       if (value & temp)
27995         as_bad_where (fixP->fx_file, fixP->fx_line,
27996                       _("misaligned branch destination"));
27997       if ((value & (offsetT)0xfe000000) != (offsetT)0
27998           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
27999         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28000
28001       if (fixP->fx_done || !seg->use_rela_p)
28002         {
28003           newval = md_chars_to_number (buf, INSN_SIZE);
28004           newval |= (value >> 2) & 0x00ffffff;
28005           /* Set the H bit on BLX instructions.  */
28006           if (temp == 1)
28007             {
28008               if (value & 2)
28009                 newval |= 0x01000000;
28010               else
28011                 newval &= ~0x01000000;
28012             }
28013           md_number_to_chars (buf, newval, INSN_SIZE);
28014         }
28015       break;
28016
28017     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
28018       /* CBZ can only branch forward.  */
28019
28020       /* Attempts to use CBZ to branch to the next instruction
28021          (which, strictly speaking, are prohibited) will be turned into
28022          no-ops.
28023
28024          FIXME: It may be better to remove the instruction completely and
28025          perform relaxation.  */
28026       if (value == -2)
28027         {
28028           newval = md_chars_to_number (buf, THUMB_SIZE);
28029           newval = 0xbf00; /* NOP encoding T1 */
28030           md_number_to_chars (buf, newval, THUMB_SIZE);
28031         }
28032       else
28033         {
28034           if (value & ~0x7e)
28035             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28036
28037           if (fixP->fx_done || !seg->use_rela_p)
28038             {
28039               newval = md_chars_to_number (buf, THUMB_SIZE);
28040               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
28041               md_number_to_chars (buf, newval, THUMB_SIZE);
28042             }
28043         }
28044       break;
28045
28046     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
28047       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
28048         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28049
28050       if (fixP->fx_done || !seg->use_rela_p)
28051         {
28052           newval = md_chars_to_number (buf, THUMB_SIZE);
28053           newval |= (value & 0x1ff) >> 1;
28054           md_number_to_chars (buf, newval, THUMB_SIZE);
28055         }
28056       break;
28057
28058     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
28059       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
28060         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28061
28062       if (fixP->fx_done || !seg->use_rela_p)
28063         {
28064           newval = md_chars_to_number (buf, THUMB_SIZE);
28065           newval |= (value & 0xfff) >> 1;
28066           md_number_to_chars (buf, newval, THUMB_SIZE);
28067         }
28068       break;
28069
28070     case BFD_RELOC_THUMB_PCREL_BRANCH20:
28071       if (fixP->fx_addsy
28072           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28073           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28074           && ARM_IS_FUNC (fixP->fx_addsy)
28075           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
28076         {
28077           /* Force a relocation for a branch 20 bits wide.  */
28078           fixP->fx_done = 0;
28079         }
28080       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
28081         as_bad_where (fixP->fx_file, fixP->fx_line,
28082                       _("conditional branch out of range"));
28083
28084       if (fixP->fx_done || !seg->use_rela_p)
28085         {
28086           offsetT newval2;
28087           addressT S, J1, J2, lo, hi;
28088
28089           S  = (value & 0x00100000) >> 20;
28090           J2 = (value & 0x00080000) >> 19;
28091           J1 = (value & 0x00040000) >> 18;
28092           hi = (value & 0x0003f000) >> 12;
28093           lo = (value & 0x00000ffe) >> 1;
28094
28095           newval   = md_chars_to_number (buf, THUMB_SIZE);
28096           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28097           newval  |= (S << 10) | hi;
28098           newval2 |= (J1 << 13) | (J2 << 11) | lo;
28099           md_number_to_chars (buf, newval, THUMB_SIZE);
28100           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28101         }
28102       break;
28103
28104     case BFD_RELOC_THUMB_PCREL_BLX:
28105       /* If there is a blx from a thumb state function to
28106          another thumb function flip this to a bl and warn
28107          about it.  */
28108
28109       if (fixP->fx_addsy
28110           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28111           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28112           && THUMB_IS_FUNC (fixP->fx_addsy))
28113         {
28114           const char *name = S_GET_NAME (fixP->fx_addsy);
28115           as_warn_where (fixP->fx_file, fixP->fx_line,
28116                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
28117                          name);
28118           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28119           newval = newval | 0x1000;
28120           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
28121           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
28122           fixP->fx_done = 1;
28123         }
28124
28125
28126       goto thumb_bl_common;
28127
28128     case BFD_RELOC_THUMB_PCREL_BRANCH23:
28129       /* A bl from Thumb state ISA to an internal ARM state function
28130          is converted to a blx.  */
28131       if (fixP->fx_addsy
28132           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28133           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28134           && ARM_IS_FUNC (fixP->fx_addsy)
28135           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
28136         {
28137           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28138           newval = newval & ~0x1000;
28139           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
28140           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
28141           fixP->fx_done = 1;
28142         }
28143
28144     thumb_bl_common:
28145
28146       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
28147         /* For a BLX instruction, make sure that the relocation is rounded up
28148            to a word boundary.  This follows the semantics of the instruction
28149            which specifies that bit 1 of the target address will come from bit
28150            1 of the base address.  */
28151         value = (value + 3) & ~ 3;
28152
28153 #ifdef OBJ_ELF
28154        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
28155            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
28156          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
28157 #endif
28158
28159       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
28160         {
28161           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
28162             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28163           else if ((value & ~0x1ffffff)
28164                    && ((value & ~0x1ffffff) != ~0x1ffffff))
28165             as_bad_where (fixP->fx_file, fixP->fx_line,
28166                           _("Thumb2 branch out of range"));
28167         }
28168
28169       if (fixP->fx_done || !seg->use_rela_p)
28170         encode_thumb2_b_bl_offset (buf, value);
28171
28172       break;
28173
28174     case BFD_RELOC_THUMB_PCREL_BRANCH25:
28175       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
28176         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28177
28178       if (fixP->fx_done || !seg->use_rela_p)
28179           encode_thumb2_b_bl_offset (buf, value);
28180
28181       break;
28182
28183     case BFD_RELOC_8:
28184       if (fixP->fx_done || !seg->use_rela_p)
28185         *buf = value;
28186       break;
28187
28188     case BFD_RELOC_16:
28189       if (fixP->fx_done || !seg->use_rela_p)
28190         md_number_to_chars (buf, value, 2);
28191       break;
28192
28193 #ifdef OBJ_ELF
28194     case BFD_RELOC_ARM_TLS_CALL:
28195     case BFD_RELOC_ARM_THM_TLS_CALL:
28196     case BFD_RELOC_ARM_TLS_DESCSEQ:
28197     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
28198     case BFD_RELOC_ARM_TLS_GOTDESC:
28199     case BFD_RELOC_ARM_TLS_GD32:
28200     case BFD_RELOC_ARM_TLS_LE32:
28201     case BFD_RELOC_ARM_TLS_IE32:
28202     case BFD_RELOC_ARM_TLS_LDM32:
28203     case BFD_RELOC_ARM_TLS_LDO32:
28204       S_SET_THREAD_LOCAL (fixP->fx_addsy);
28205       break;
28206
28207       /* Same handling as above, but with the arm_fdpic guard.  */
28208     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
28209     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
28210     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
28211       if (arm_fdpic)
28212         {
28213           S_SET_THREAD_LOCAL (fixP->fx_addsy);
28214         }
28215       else
28216         {
28217           as_bad_where (fixP->fx_file, fixP->fx_line,
28218                         _("Relocation supported only in FDPIC mode"));
28219         }
28220       break;
28221
28222     case BFD_RELOC_ARM_GOT32:
28223     case BFD_RELOC_ARM_GOTOFF:
28224       break;
28225
28226     case BFD_RELOC_ARM_GOT_PREL:
28227       if (fixP->fx_done || !seg->use_rela_p)
28228         md_number_to_chars (buf, value, 4);
28229       break;
28230
28231     case BFD_RELOC_ARM_TARGET2:
28232       /* TARGET2 is not partial-inplace, so we need to write the
28233          addend here for REL targets, because it won't be written out
28234          during reloc processing later.  */
28235       if (fixP->fx_done || !seg->use_rela_p)
28236         md_number_to_chars (buf, fixP->fx_offset, 4);
28237       break;
28238
28239       /* Relocations for FDPIC.  */
28240     case BFD_RELOC_ARM_GOTFUNCDESC:
28241     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
28242     case BFD_RELOC_ARM_FUNCDESC:
28243       if (arm_fdpic)
28244         {
28245           if (fixP->fx_done || !seg->use_rela_p)
28246             md_number_to_chars (buf, 0, 4);
28247         }
28248       else
28249         {
28250           as_bad_where (fixP->fx_file, fixP->fx_line,
28251                         _("Relocation supported only in FDPIC mode"));
28252       }
28253       break;
28254 #endif
28255
28256     case BFD_RELOC_RVA:
28257     case BFD_RELOC_32:
28258     case BFD_RELOC_ARM_TARGET1:
28259     case BFD_RELOC_ARM_ROSEGREL32:
28260     case BFD_RELOC_ARM_SBREL32:
28261     case BFD_RELOC_32_PCREL:
28262 #ifdef TE_PE
28263     case BFD_RELOC_32_SECREL:
28264 #endif
28265       if (fixP->fx_done || !seg->use_rela_p)
28266 #ifdef TE_WINCE
28267         /* For WinCE we only do this for pcrel fixups.  */
28268         if (fixP->fx_done || fixP->fx_pcrel)
28269 #endif
28270           md_number_to_chars (buf, value, 4);
28271       break;
28272
28273 #ifdef OBJ_ELF
28274     case BFD_RELOC_ARM_PREL31:
28275       if (fixP->fx_done || !seg->use_rela_p)
28276         {
28277           newval = md_chars_to_number (buf, 4) & 0x80000000;
28278           if ((value ^ (value >> 1)) & 0x40000000)
28279             {
28280               as_bad_where (fixP->fx_file, fixP->fx_line,
28281                             _("rel31 relocation overflow"));
28282             }
28283           newval |= value & 0x7fffffff;
28284           md_number_to_chars (buf, newval, 4);
28285         }
28286       break;
28287 #endif
28288
28289     case BFD_RELOC_ARM_CP_OFF_IMM:
28290     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
28291     case BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM:
28292       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
28293         newval = md_chars_to_number (buf, INSN_SIZE);
28294       else
28295         newval = get_thumb32_insn (buf);
28296       if ((newval & 0x0f200f00) == 0x0d000900)
28297         {
28298           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
28299              has permitted values that are multiples of 2, in the range 0
28300              to 510.  */
28301           if (value < -510 || value > 510 || (value & 1))
28302             as_bad_where (fixP->fx_file, fixP->fx_line,
28303                           _("co-processor offset out of range"));
28304         }
28305       else if ((newval & 0xfe001f80) == 0xec000f80)
28306         {
28307           if (value < -511 || value > 512 || (value & 3))
28308             as_bad_where (fixP->fx_file, fixP->fx_line,
28309                           _("co-processor offset out of range"));
28310         }
28311       else if (value < -1023 || value > 1023 || (value & 3))
28312         as_bad_where (fixP->fx_file, fixP->fx_line,
28313                       _("co-processor offset out of range"));
28314     cp_off_common:
28315       sign = value > 0;
28316       if (value < 0)
28317         value = -value;
28318       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28319           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
28320         newval = md_chars_to_number (buf, INSN_SIZE);
28321       else
28322         newval = get_thumb32_insn (buf);
28323       if (value == 0)
28324         {
28325           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
28326             newval &= 0xffffff80;
28327           else
28328             newval &= 0xffffff00;
28329         }
28330       else
28331         {
28332           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
28333             newval &= 0xff7fff80;
28334           else
28335             newval &= 0xff7fff00;
28336           if ((newval & 0x0f200f00) == 0x0d000900)
28337             {
28338               /* This is a fp16 vstr/vldr.
28339
28340                  It requires the immediate offset in the instruction is shifted
28341                  left by 1 to be a half-word offset.
28342
28343                  Here, left shift by 1 first, and later right shift by 2
28344                  should get the right offset.  */
28345               value <<= 1;
28346             }
28347           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
28348         }
28349       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28350           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
28351         md_number_to_chars (buf, newval, INSN_SIZE);
28352       else
28353         put_thumb32_insn (buf, newval);
28354       break;
28355
28356     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
28357     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
28358       if (value < -255 || value > 255)
28359         as_bad_where (fixP->fx_file, fixP->fx_line,
28360                       _("co-processor offset out of range"));
28361       value *= 4;
28362       goto cp_off_common;
28363
28364     case BFD_RELOC_ARM_THUMB_OFFSET:
28365       newval = md_chars_to_number (buf, THUMB_SIZE);
28366       /* Exactly what ranges, and where the offset is inserted depends
28367          on the type of instruction, we can establish this from the
28368          top 4 bits.  */
28369       switch (newval >> 12)
28370         {
28371         case 4: /* PC load.  */
28372           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
28373              forced to zero for these loads; md_pcrel_from has already
28374              compensated for this.  */
28375           if (value & 3)
28376             as_bad_where (fixP->fx_file, fixP->fx_line,
28377                           _("invalid offset, target not word aligned (0x%08lX)"),
28378                           (((unsigned long) fixP->fx_frag->fr_address
28379                             + (unsigned long) fixP->fx_where) & ~3)
28380                           + (unsigned long) value);
28381
28382           if (value & ~0x3fc)
28383             as_bad_where (fixP->fx_file, fixP->fx_line,
28384                           _("invalid offset, value too big (0x%08lX)"),
28385                           (long) value);
28386
28387           newval |= value >> 2;
28388           break;
28389
28390         case 9: /* SP load/store.  */
28391           if (value & ~0x3fc)
28392             as_bad_where (fixP->fx_file, fixP->fx_line,
28393                           _("invalid offset, value too big (0x%08lX)"),
28394                           (long) value);
28395           newval |= value >> 2;
28396           break;
28397
28398         case 6: /* Word load/store.  */
28399           if (value & ~0x7c)
28400             as_bad_where (fixP->fx_file, fixP->fx_line,
28401                           _("invalid offset, value too big (0x%08lX)"),
28402                           (long) value);
28403           newval |= value << 4; /* 6 - 2.  */
28404           break;
28405
28406         case 7: /* Byte load/store.  */
28407           if (value & ~0x1f)
28408             as_bad_where (fixP->fx_file, fixP->fx_line,
28409                           _("invalid offset, value too big (0x%08lX)"),
28410                           (long) value);
28411           newval |= value << 6;
28412           break;
28413
28414         case 8: /* Halfword load/store.  */
28415           if (value & ~0x3e)
28416             as_bad_where (fixP->fx_file, fixP->fx_line,
28417                           _("invalid offset, value too big (0x%08lX)"),
28418                           (long) value);
28419           newval |= value << 5; /* 6 - 1.  */
28420           break;
28421
28422         default:
28423           as_bad_where (fixP->fx_file, fixP->fx_line,
28424                         "Unable to process relocation for thumb opcode: %lx",
28425                         (unsigned long) newval);
28426           break;
28427         }
28428       md_number_to_chars (buf, newval, THUMB_SIZE);
28429       break;
28430
28431     case BFD_RELOC_ARM_THUMB_ADD:
28432       /* This is a complicated relocation, since we use it for all of
28433          the following immediate relocations:
28434
28435             3bit ADD/SUB
28436             8bit ADD/SUB
28437             9bit ADD/SUB SP word-aligned
28438            10bit ADD PC/SP word-aligned
28439
28440          The type of instruction being processed is encoded in the
28441          instruction field:
28442
28443            0x8000  SUB
28444            0x00F0  Rd
28445            0x000F  Rs
28446       */
28447       newval = md_chars_to_number (buf, THUMB_SIZE);
28448       {
28449         int rd = (newval >> 4) & 0xf;
28450         int rs = newval & 0xf;
28451         int subtract = !!(newval & 0x8000);
28452
28453         /* Check for HI regs, only very restricted cases allowed:
28454            Adjusting SP, and using PC or SP to get an address.  */
28455         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
28456             || (rs > 7 && rs != REG_SP && rs != REG_PC))
28457           as_bad_where (fixP->fx_file, fixP->fx_line,
28458                         _("invalid Hi register with immediate"));
28459
28460         /* If value is negative, choose the opposite instruction.  */
28461         if (value < 0)
28462           {
28463             value = -value;
28464             subtract = !subtract;
28465             if (value < 0)
28466               as_bad_where (fixP->fx_file, fixP->fx_line,
28467                             _("immediate value out of range"));
28468           }
28469
28470         if (rd == REG_SP)
28471           {
28472             if (value & ~0x1fc)
28473               as_bad_where (fixP->fx_file, fixP->fx_line,
28474                             _("invalid immediate for stack address calculation"));
28475             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
28476             newval |= value >> 2;
28477           }
28478         else if (rs == REG_PC || rs == REG_SP)
28479           {
28480             /* PR gas/18541.  If the addition is for a defined symbol
28481                within range of an ADR instruction then accept it.  */
28482             if (subtract
28483                 && value == 4
28484                 && fixP->fx_addsy != NULL)
28485               {
28486                 subtract = 0;
28487
28488                 if (! S_IS_DEFINED (fixP->fx_addsy)
28489                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
28490                     || S_IS_WEAK (fixP->fx_addsy))
28491                   {
28492                     as_bad_where (fixP->fx_file, fixP->fx_line,
28493                                   _("address calculation needs a strongly defined nearby symbol"));
28494                   }
28495                 else
28496                   {
28497                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
28498
28499                     /* Round up to the next 4-byte boundary.  */
28500                     if (v & 3)
28501                       v = (v + 3) & ~ 3;
28502                     else
28503                       v += 4;
28504                     v = S_GET_VALUE (fixP->fx_addsy) - v;
28505
28506                     if (v & ~0x3fc)
28507                       {
28508                         as_bad_where (fixP->fx_file, fixP->fx_line,
28509                                       _("symbol too far away"));
28510                       }
28511                     else
28512                       {
28513                         fixP->fx_done = 1;
28514                         value = v;
28515                       }
28516                   }
28517               }
28518
28519             if (subtract || value & ~0x3fc)
28520               as_bad_where (fixP->fx_file, fixP->fx_line,
28521                             _("invalid immediate for address calculation (value = 0x%08lX)"),
28522                             (unsigned long) (subtract ? - value : value));
28523             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
28524             newval |= rd << 8;
28525             newval |= value >> 2;
28526           }
28527         else if (rs == rd)
28528           {
28529             if (value & ~0xff)
28530               as_bad_where (fixP->fx_file, fixP->fx_line,
28531                             _("immediate value out of range"));
28532             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
28533             newval |= (rd << 8) | value;
28534           }
28535         else
28536           {
28537             if (value & ~0x7)
28538               as_bad_where (fixP->fx_file, fixP->fx_line,
28539                             _("immediate value out of range"));
28540             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
28541             newval |= rd | (rs << 3) | (value << 6);
28542           }
28543       }
28544       md_number_to_chars (buf, newval, THUMB_SIZE);
28545       break;
28546
28547     case BFD_RELOC_ARM_THUMB_IMM:
28548       newval = md_chars_to_number (buf, THUMB_SIZE);
28549       if (value < 0 || value > 255)
28550         as_bad_where (fixP->fx_file, fixP->fx_line,
28551                       _("invalid immediate: %ld is out of range"),
28552                       (long) value);
28553       newval |= value;
28554       md_number_to_chars (buf, newval, THUMB_SIZE);
28555       break;
28556
28557     case BFD_RELOC_ARM_THUMB_SHIFT:
28558       /* 5bit shift value (0..32).  LSL cannot take 32.  */
28559       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
28560       temp = newval & 0xf800;
28561       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
28562         as_bad_where (fixP->fx_file, fixP->fx_line,
28563                       _("invalid shift value: %ld"), (long) value);
28564       /* Shifts of zero must be encoded as LSL.  */
28565       if (value == 0)
28566         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
28567       /* Shifts of 32 are encoded as zero.  */
28568       else if (value == 32)
28569         value = 0;
28570       newval |= value << 6;
28571       md_number_to_chars (buf, newval, THUMB_SIZE);
28572       break;
28573
28574     case BFD_RELOC_VTABLE_INHERIT:
28575     case BFD_RELOC_VTABLE_ENTRY:
28576       fixP->fx_done = 0;
28577       return;
28578
28579     case BFD_RELOC_ARM_MOVW:
28580     case BFD_RELOC_ARM_MOVT:
28581     case BFD_RELOC_ARM_THUMB_MOVW:
28582     case BFD_RELOC_ARM_THUMB_MOVT:
28583       if (fixP->fx_done || !seg->use_rela_p)
28584         {
28585           /* REL format relocations are limited to a 16-bit addend.  */
28586           if (!fixP->fx_done)
28587             {
28588               if (value < -0x8000 || value > 0x7fff)
28589                   as_bad_where (fixP->fx_file, fixP->fx_line,
28590                                 _("offset out of range"));
28591             }
28592           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
28593                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
28594             {
28595               value >>= 16;
28596             }
28597
28598           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
28599               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
28600             {
28601               newval = get_thumb32_insn (buf);
28602               newval &= 0xfbf08f00;
28603               newval |= (value & 0xf000) << 4;
28604               newval |= (value & 0x0800) << 15;
28605               newval |= (value & 0x0700) << 4;
28606               newval |= (value & 0x00ff);
28607               put_thumb32_insn (buf, newval);
28608             }
28609           else
28610             {
28611               newval = md_chars_to_number (buf, 4);
28612               newval &= 0xfff0f000;
28613               newval |= value & 0x0fff;
28614               newval |= (value & 0xf000) << 4;
28615               md_number_to_chars (buf, newval, 4);
28616             }
28617         }
28618       return;
28619
28620    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
28621    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
28622    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
28623    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
28624       gas_assert (!fixP->fx_done);
28625       {
28626         bfd_vma insn;
28627         bfd_boolean is_mov;
28628         bfd_vma encoded_addend = value;
28629
28630         /* Check that addend can be encoded in instruction.  */
28631         if (!seg->use_rela_p && (value < 0 || value > 255))
28632           as_bad_where (fixP->fx_file, fixP->fx_line,
28633                         _("the offset 0x%08lX is not representable"),
28634                         (unsigned long) encoded_addend);
28635
28636         /* Extract the instruction.  */
28637         insn = md_chars_to_number (buf, THUMB_SIZE);
28638         is_mov = (insn & 0xf800) == 0x2000;
28639
28640         /* Encode insn.  */
28641         if (is_mov)
28642           {
28643             if (!seg->use_rela_p)
28644               insn |= encoded_addend;
28645           }
28646         else
28647           {
28648             int rd, rs;
28649
28650             /* Extract the instruction.  */
28651              /* Encoding is the following
28652                 0x8000  SUB
28653                 0x00F0  Rd
28654                 0x000F  Rs
28655              */
28656              /* The following conditions must be true :
28657                 - ADD
28658                 - Rd == Rs
28659                 - Rd <= 7
28660              */
28661             rd = (insn >> 4) & 0xf;
28662             rs = insn & 0xf;
28663             if ((insn & 0x8000) || (rd != rs) || rd > 7)
28664               as_bad_where (fixP->fx_file, fixP->fx_line,
28665                         _("Unable to process relocation for thumb opcode: %lx"),
28666                         (unsigned long) insn);
28667
28668             /* Encode as ADD immediate8 thumb 1 code.  */
28669             insn = 0x3000 | (rd << 8);
28670
28671             /* Place the encoded addend into the first 8 bits of the
28672                instruction.  */
28673             if (!seg->use_rela_p)
28674               insn |= encoded_addend;
28675           }
28676
28677         /* Update the instruction.  */
28678         md_number_to_chars (buf, insn, THUMB_SIZE);
28679       }
28680       break;
28681
28682    case BFD_RELOC_ARM_ALU_PC_G0_NC:
28683    case BFD_RELOC_ARM_ALU_PC_G0:
28684    case BFD_RELOC_ARM_ALU_PC_G1_NC:
28685    case BFD_RELOC_ARM_ALU_PC_G1:
28686    case BFD_RELOC_ARM_ALU_PC_G2:
28687    case BFD_RELOC_ARM_ALU_SB_G0_NC:
28688    case BFD_RELOC_ARM_ALU_SB_G0:
28689    case BFD_RELOC_ARM_ALU_SB_G1_NC:
28690    case BFD_RELOC_ARM_ALU_SB_G1:
28691    case BFD_RELOC_ARM_ALU_SB_G2:
28692      gas_assert (!fixP->fx_done);
28693      if (!seg->use_rela_p)
28694        {
28695          bfd_vma insn;
28696          bfd_vma encoded_addend;
28697          bfd_vma addend_abs = llabs (value);
28698
28699          /* Check that the absolute value of the addend can be
28700             expressed as an 8-bit constant plus a rotation.  */
28701          encoded_addend = encode_arm_immediate (addend_abs);
28702          if (encoded_addend == (unsigned int) FAIL)
28703            as_bad_where (fixP->fx_file, fixP->fx_line,
28704                          _("the offset 0x%08lX is not representable"),
28705                          (unsigned long) addend_abs);
28706
28707          /* Extract the instruction.  */
28708          insn = md_chars_to_number (buf, INSN_SIZE);
28709
28710          /* If the addend is positive, use an ADD instruction.
28711             Otherwise use a SUB.  Take care not to destroy the S bit.  */
28712          insn &= 0xff1fffff;
28713          if (value < 0)
28714            insn |= 1 << 22;
28715          else
28716            insn |= 1 << 23;
28717
28718          /* Place the encoded addend into the first 12 bits of the
28719             instruction.  */
28720          insn &= 0xfffff000;
28721          insn |= encoded_addend;
28722
28723          /* Update the instruction.  */
28724          md_number_to_chars (buf, insn, INSN_SIZE);
28725        }
28726      break;
28727
28728     case BFD_RELOC_ARM_LDR_PC_G0:
28729     case BFD_RELOC_ARM_LDR_PC_G1:
28730     case BFD_RELOC_ARM_LDR_PC_G2:
28731     case BFD_RELOC_ARM_LDR_SB_G0:
28732     case BFD_RELOC_ARM_LDR_SB_G1:
28733     case BFD_RELOC_ARM_LDR_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 12 bits.  */
28742           if (addend_abs >= 0x1000)
28743             as_bad_where (fixP->fx_file, fixP->fx_line,
28744                           _("bad offset 0x%08lX (only 12 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 absolute value of the addend into the first 12 bits
28758              of the instruction.  */
28759           insn &= 0xfffff000;
28760           insn |= addend_abs;
28761
28762           /* Update the instruction.  */
28763           md_number_to_chars (buf, insn, INSN_SIZE);
28764         }
28765       break;
28766
28767     case BFD_RELOC_ARM_LDRS_PC_G0:
28768     case BFD_RELOC_ARM_LDRS_PC_G1:
28769     case BFD_RELOC_ARM_LDRS_PC_G2:
28770     case BFD_RELOC_ARM_LDRS_SB_G0:
28771     case BFD_RELOC_ARM_LDRS_SB_G1:
28772     case BFD_RELOC_ARM_LDRS_SB_G2:
28773       gas_assert (!fixP->fx_done);
28774       if (!seg->use_rela_p)
28775         {
28776           bfd_vma insn;
28777           bfd_vma addend_abs = llabs (value);
28778
28779           /* Check that the absolute value of the addend can be
28780              encoded in 8 bits.  */
28781           if (addend_abs >= 0x100)
28782             as_bad_where (fixP->fx_file, fixP->fx_line,
28783                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
28784                           (unsigned long) addend_abs);
28785
28786           /* Extract the instruction.  */
28787           insn = md_chars_to_number (buf, INSN_SIZE);
28788
28789           /* If the addend is negative, clear bit 23 of the instruction.
28790              Otherwise set it.  */
28791           if (value < 0)
28792             insn &= ~(1 << 23);
28793           else
28794             insn |= 1 << 23;
28795
28796           /* Place the first four bits of the absolute value of the addend
28797              into the first 4 bits of the instruction, and the remaining
28798              four into bits 8 .. 11.  */
28799           insn &= 0xfffff0f0;
28800           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
28801
28802           /* Update the instruction.  */
28803           md_number_to_chars (buf, insn, INSN_SIZE);
28804         }
28805       break;
28806
28807     case BFD_RELOC_ARM_LDC_PC_G0:
28808     case BFD_RELOC_ARM_LDC_PC_G1:
28809     case BFD_RELOC_ARM_LDC_PC_G2:
28810     case BFD_RELOC_ARM_LDC_SB_G0:
28811     case BFD_RELOC_ARM_LDC_SB_G1:
28812     case BFD_RELOC_ARM_LDC_SB_G2:
28813       gas_assert (!fixP->fx_done);
28814       if (!seg->use_rela_p)
28815         {
28816           bfd_vma insn;
28817           bfd_vma addend_abs = llabs (value);
28818
28819           /* Check that the absolute value of the addend is a multiple of
28820              four and, when divided by four, fits in 8 bits.  */
28821           if (addend_abs & 0x3)
28822             as_bad_where (fixP->fx_file, fixP->fx_line,
28823                           _("bad offset 0x%08lX (must be word-aligned)"),
28824                           (unsigned long) addend_abs);
28825
28826           if ((addend_abs >> 2) > 0xff)
28827             as_bad_where (fixP->fx_file, fixP->fx_line,
28828                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
28829                           (unsigned long) addend_abs);
28830
28831           /* Extract the instruction.  */
28832           insn = md_chars_to_number (buf, INSN_SIZE);
28833
28834           /* If the addend is negative, clear bit 23 of the instruction.
28835              Otherwise set it.  */
28836           if (value < 0)
28837             insn &= ~(1 << 23);
28838           else
28839             insn |= 1 << 23;
28840
28841           /* Place the addend (divided by four) into the first eight
28842              bits of the instruction.  */
28843           insn &= 0xfffffff0;
28844           insn |= addend_abs >> 2;
28845
28846           /* Update the instruction.  */
28847           md_number_to_chars (buf, insn, INSN_SIZE);
28848         }
28849       break;
28850
28851     case BFD_RELOC_THUMB_PCREL_BRANCH5:
28852       if (fixP->fx_addsy
28853           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28854           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28855           && ARM_IS_FUNC (fixP->fx_addsy)
28856           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28857         {
28858           /* Force a relocation for a branch 5 bits wide.  */
28859           fixP->fx_done = 0;
28860         }
28861       if (v8_1_branch_value_check (value, 5, FALSE) == FAIL)
28862         as_bad_where (fixP->fx_file, fixP->fx_line,
28863                       BAD_BRANCH_OFF);
28864
28865       if (fixP->fx_done || !seg->use_rela_p)
28866         {
28867           addressT boff = value >> 1;
28868
28869           newval  = md_chars_to_number (buf, THUMB_SIZE);
28870           newval |= (boff << 7);
28871           md_number_to_chars (buf, newval, THUMB_SIZE);
28872         }
28873       break;
28874
28875     case BFD_RELOC_THUMB_PCREL_BFCSEL:
28876       if (fixP->fx_addsy
28877           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28878           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28879           && ARM_IS_FUNC (fixP->fx_addsy)
28880           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28881         {
28882           fixP->fx_done = 0;
28883         }
28884       if ((value & ~0x7f) && ((value & ~0x3f) != ~0x3f))
28885         as_bad_where (fixP->fx_file, fixP->fx_line,
28886                       _("branch out of range"));
28887
28888       if (fixP->fx_done || !seg->use_rela_p)
28889         {
28890           newval  = md_chars_to_number (buf, THUMB_SIZE);
28891
28892           addressT boff = ((newval & 0x0780) >> 7) << 1;
28893           addressT diff = value - boff;
28894
28895           if (diff == 4)
28896             {
28897               newval |= 1 << 1; /* T bit.  */
28898             }
28899           else if (diff != 2)
28900             {
28901               as_bad_where (fixP->fx_file, fixP->fx_line,
28902                             _("out of range label-relative fixup value"));
28903             }
28904           md_number_to_chars (buf, newval, THUMB_SIZE);
28905         }
28906       break;
28907
28908     case BFD_RELOC_ARM_THUMB_BF17:
28909       if (fixP->fx_addsy
28910           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28911           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28912           && ARM_IS_FUNC (fixP->fx_addsy)
28913           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28914         {
28915           /* Force a relocation for a branch 17 bits wide.  */
28916           fixP->fx_done = 0;
28917         }
28918
28919       if (v8_1_branch_value_check (value, 17, TRUE) == FAIL)
28920         as_bad_where (fixP->fx_file, fixP->fx_line,
28921                       BAD_BRANCH_OFF);
28922
28923       if (fixP->fx_done || !seg->use_rela_p)
28924         {
28925           offsetT newval2;
28926           addressT immA, immB, immC;
28927
28928           immA = (value & 0x0001f000) >> 12;
28929           immB = (value & 0x00000ffc) >> 2;
28930           immC = (value & 0x00000002) >> 1;
28931
28932           newval   = md_chars_to_number (buf, THUMB_SIZE);
28933           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28934           newval  |= immA;
28935           newval2 |= (immC << 11) | (immB << 1);
28936           md_number_to_chars (buf, newval, THUMB_SIZE);
28937           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28938         }
28939       break;
28940
28941     case BFD_RELOC_ARM_THUMB_BF19:
28942       if (fixP->fx_addsy
28943           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28944           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28945           && ARM_IS_FUNC (fixP->fx_addsy)
28946           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28947         {
28948           /* Force a relocation for a branch 19 bits wide.  */
28949           fixP->fx_done = 0;
28950         }
28951
28952       if (v8_1_branch_value_check (value, 19, TRUE) == FAIL)
28953         as_bad_where (fixP->fx_file, fixP->fx_line,
28954                       BAD_BRANCH_OFF);
28955
28956       if (fixP->fx_done || !seg->use_rela_p)
28957         {
28958           offsetT newval2;
28959           addressT immA, immB, immC;
28960
28961           immA = (value & 0x0007f000) >> 12;
28962           immB = (value & 0x00000ffc) >> 2;
28963           immC = (value & 0x00000002) >> 1;
28964
28965           newval   = md_chars_to_number (buf, THUMB_SIZE);
28966           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28967           newval  |= immA;
28968           newval2 |= (immC << 11) | (immB << 1);
28969           md_number_to_chars (buf, newval, THUMB_SIZE);
28970           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28971         }
28972       break;
28973
28974     case BFD_RELOC_ARM_THUMB_BF13:
28975       if (fixP->fx_addsy
28976           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28977           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28978           && ARM_IS_FUNC (fixP->fx_addsy)
28979           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28980         {
28981           /* Force a relocation for a branch 13 bits wide.  */
28982           fixP->fx_done = 0;
28983         }
28984
28985       if (v8_1_branch_value_check (value, 13, TRUE) == FAIL)
28986         as_bad_where (fixP->fx_file, fixP->fx_line,
28987                       BAD_BRANCH_OFF);
28988
28989       if (fixP->fx_done || !seg->use_rela_p)
28990         {
28991           offsetT newval2;
28992           addressT immA, immB, immC;
28993
28994           immA = (value & 0x00001000) >> 12;
28995           immB = (value & 0x00000ffc) >> 2;
28996           immC = (value & 0x00000002) >> 1;
28997
28998           newval   = md_chars_to_number (buf, THUMB_SIZE);
28999           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
29000           newval  |= immA;
29001           newval2 |= (immC << 11) | (immB << 1);
29002           md_number_to_chars (buf, newval, THUMB_SIZE);
29003           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
29004         }
29005       break;
29006
29007     case BFD_RELOC_ARM_THUMB_LOOP12:
29008       if (fixP->fx_addsy
29009           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
29010           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
29011           && ARM_IS_FUNC (fixP->fx_addsy)
29012           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
29013         {
29014           /* Force a relocation for a branch 12 bits wide.  */
29015           fixP->fx_done = 0;
29016         }
29017
29018       bfd_vma insn = get_thumb32_insn (buf);
29019       /* le lr, <label>, le <label> or letp lr, <label> */
29020       if (((insn & 0xffffffff) == 0xf00fc001)
29021           || ((insn & 0xffffffff) == 0xf02fc001)
29022           || ((insn & 0xffffffff) == 0xf01fc001))
29023         value = -value;
29024
29025       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
29026         as_bad_where (fixP->fx_file, fixP->fx_line,
29027                       BAD_BRANCH_OFF);
29028       if (fixP->fx_done || !seg->use_rela_p)
29029         {
29030           addressT imml, immh;
29031
29032           immh = (value & 0x00000ffc) >> 2;
29033           imml = (value & 0x00000002) >> 1;
29034
29035           newval  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
29036           newval |= (imml << 11) | (immh << 1);
29037           md_number_to_chars (buf + THUMB_SIZE, newval, THUMB_SIZE);
29038         }
29039       break;
29040
29041     case BFD_RELOC_ARM_V4BX:
29042       /* This will need to go in the object file.  */
29043       fixP->fx_done = 0;
29044       break;
29045
29046     case BFD_RELOC_UNUSED:
29047     default:
29048       as_bad_where (fixP->fx_file, fixP->fx_line,
29049                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
29050     }
29051 }
29052
29053 /* Translate internal representation of relocation info to BFD target
29054    format.  */
29055
29056 arelent *
29057 tc_gen_reloc (asection *section, fixS *fixp)
29058 {
29059   arelent * reloc;
29060   bfd_reloc_code_real_type code;
29061
29062   reloc = XNEW (arelent);
29063
29064   reloc->sym_ptr_ptr = XNEW (asymbol *);
29065   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
29066   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
29067
29068   if (fixp->fx_pcrel)
29069     {
29070       if (section->use_rela_p)
29071         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
29072       else
29073         fixp->fx_offset = reloc->address;
29074     }
29075   reloc->addend = fixp->fx_offset;
29076
29077   switch (fixp->fx_r_type)
29078     {
29079     case BFD_RELOC_8:
29080       if (fixp->fx_pcrel)
29081         {
29082           code = BFD_RELOC_8_PCREL;
29083           break;
29084         }
29085       /* Fall through.  */
29086
29087     case BFD_RELOC_16:
29088       if (fixp->fx_pcrel)
29089         {
29090           code = BFD_RELOC_16_PCREL;
29091           break;
29092         }
29093       /* Fall through.  */
29094
29095     case BFD_RELOC_32:
29096       if (fixp->fx_pcrel)
29097         {
29098           code = BFD_RELOC_32_PCREL;
29099           break;
29100         }
29101       /* Fall through.  */
29102
29103     case BFD_RELOC_ARM_MOVW:
29104       if (fixp->fx_pcrel)
29105         {
29106           code = BFD_RELOC_ARM_MOVW_PCREL;
29107           break;
29108         }
29109       /* Fall through.  */
29110
29111     case BFD_RELOC_ARM_MOVT:
29112       if (fixp->fx_pcrel)
29113         {
29114           code = BFD_RELOC_ARM_MOVT_PCREL;
29115           break;
29116         }
29117       /* Fall through.  */
29118
29119     case BFD_RELOC_ARM_THUMB_MOVW:
29120       if (fixp->fx_pcrel)
29121         {
29122           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
29123           break;
29124         }
29125       /* Fall through.  */
29126
29127     case BFD_RELOC_ARM_THUMB_MOVT:
29128       if (fixp->fx_pcrel)
29129         {
29130           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
29131           break;
29132         }
29133       /* Fall through.  */
29134
29135     case BFD_RELOC_NONE:
29136     case BFD_RELOC_ARM_PCREL_BRANCH:
29137     case BFD_RELOC_ARM_PCREL_BLX:
29138     case BFD_RELOC_RVA:
29139     case BFD_RELOC_THUMB_PCREL_BRANCH7:
29140     case BFD_RELOC_THUMB_PCREL_BRANCH9:
29141     case BFD_RELOC_THUMB_PCREL_BRANCH12:
29142     case BFD_RELOC_THUMB_PCREL_BRANCH20:
29143     case BFD_RELOC_THUMB_PCREL_BRANCH23:
29144     case BFD_RELOC_THUMB_PCREL_BRANCH25:
29145     case BFD_RELOC_VTABLE_ENTRY:
29146     case BFD_RELOC_VTABLE_INHERIT:
29147 #ifdef TE_PE
29148     case BFD_RELOC_32_SECREL:
29149 #endif
29150       code = fixp->fx_r_type;
29151       break;
29152
29153     case BFD_RELOC_THUMB_PCREL_BLX:
29154 #ifdef OBJ_ELF
29155       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
29156         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
29157       else
29158 #endif
29159         code = BFD_RELOC_THUMB_PCREL_BLX;
29160       break;
29161
29162     case BFD_RELOC_ARM_LITERAL:
29163     case BFD_RELOC_ARM_HWLITERAL:
29164       /* If this is called then the a literal has
29165          been referenced across a section boundary.  */
29166       as_bad_where (fixp->fx_file, fixp->fx_line,
29167                     _("literal referenced across section boundary"));
29168       return NULL;
29169
29170 #ifdef OBJ_ELF
29171     case BFD_RELOC_ARM_TLS_CALL:
29172     case BFD_RELOC_ARM_THM_TLS_CALL:
29173     case BFD_RELOC_ARM_TLS_DESCSEQ:
29174     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
29175     case BFD_RELOC_ARM_GOT32:
29176     case BFD_RELOC_ARM_GOTOFF:
29177     case BFD_RELOC_ARM_GOT_PREL:
29178     case BFD_RELOC_ARM_PLT32:
29179     case BFD_RELOC_ARM_TARGET1:
29180     case BFD_RELOC_ARM_ROSEGREL32:
29181     case BFD_RELOC_ARM_SBREL32:
29182     case BFD_RELOC_ARM_PREL31:
29183     case BFD_RELOC_ARM_TARGET2:
29184     case BFD_RELOC_ARM_TLS_LDO32:
29185     case BFD_RELOC_ARM_PCREL_CALL:
29186     case BFD_RELOC_ARM_PCREL_JUMP:
29187     case BFD_RELOC_ARM_ALU_PC_G0_NC:
29188     case BFD_RELOC_ARM_ALU_PC_G0:
29189     case BFD_RELOC_ARM_ALU_PC_G1_NC:
29190     case BFD_RELOC_ARM_ALU_PC_G1:
29191     case BFD_RELOC_ARM_ALU_PC_G2:
29192     case BFD_RELOC_ARM_LDR_PC_G0:
29193     case BFD_RELOC_ARM_LDR_PC_G1:
29194     case BFD_RELOC_ARM_LDR_PC_G2:
29195     case BFD_RELOC_ARM_LDRS_PC_G0:
29196     case BFD_RELOC_ARM_LDRS_PC_G1:
29197     case BFD_RELOC_ARM_LDRS_PC_G2:
29198     case BFD_RELOC_ARM_LDC_PC_G0:
29199     case BFD_RELOC_ARM_LDC_PC_G1:
29200     case BFD_RELOC_ARM_LDC_PC_G2:
29201     case BFD_RELOC_ARM_ALU_SB_G0_NC:
29202     case BFD_RELOC_ARM_ALU_SB_G0:
29203     case BFD_RELOC_ARM_ALU_SB_G1_NC:
29204     case BFD_RELOC_ARM_ALU_SB_G1:
29205     case BFD_RELOC_ARM_ALU_SB_G2:
29206     case BFD_RELOC_ARM_LDR_SB_G0:
29207     case BFD_RELOC_ARM_LDR_SB_G1:
29208     case BFD_RELOC_ARM_LDR_SB_G2:
29209     case BFD_RELOC_ARM_LDRS_SB_G0:
29210     case BFD_RELOC_ARM_LDRS_SB_G1:
29211     case BFD_RELOC_ARM_LDRS_SB_G2:
29212     case BFD_RELOC_ARM_LDC_SB_G0:
29213     case BFD_RELOC_ARM_LDC_SB_G1:
29214     case BFD_RELOC_ARM_LDC_SB_G2:
29215     case BFD_RELOC_ARM_V4BX:
29216     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
29217     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
29218     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
29219     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
29220     case BFD_RELOC_ARM_GOTFUNCDESC:
29221     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
29222     case BFD_RELOC_ARM_FUNCDESC:
29223     case BFD_RELOC_ARM_THUMB_BF17:
29224     case BFD_RELOC_ARM_THUMB_BF19:
29225     case BFD_RELOC_ARM_THUMB_BF13:
29226       code = fixp->fx_r_type;
29227       break;
29228
29229     case BFD_RELOC_ARM_TLS_GOTDESC:
29230     case BFD_RELOC_ARM_TLS_GD32:
29231     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
29232     case BFD_RELOC_ARM_TLS_LE32:
29233     case BFD_RELOC_ARM_TLS_IE32:
29234     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
29235     case BFD_RELOC_ARM_TLS_LDM32:
29236     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
29237       /* BFD will include the symbol's address in the addend.
29238          But we don't want that, so subtract it out again here.  */
29239       if (!S_IS_COMMON (fixp->fx_addsy))
29240         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
29241       code = fixp->fx_r_type;
29242       break;
29243 #endif
29244
29245     case BFD_RELOC_ARM_IMMEDIATE:
29246       as_bad_where (fixp->fx_file, fixp->fx_line,
29247                     _("internal relocation (type: IMMEDIATE) not fixed up"));
29248       return NULL;
29249
29250     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
29251       as_bad_where (fixp->fx_file, fixp->fx_line,
29252                     _("ADRL used for a symbol not defined in the same file"));
29253       return NULL;
29254
29255     case BFD_RELOC_THUMB_PCREL_BRANCH5:
29256     case BFD_RELOC_THUMB_PCREL_BFCSEL:
29257     case BFD_RELOC_ARM_THUMB_LOOP12:
29258       as_bad_where (fixp->fx_file, fixp->fx_line,
29259                     _("%s used for a symbol not defined in the same file"),
29260                     bfd_get_reloc_code_name (fixp->fx_r_type));
29261       return NULL;
29262
29263     case BFD_RELOC_ARM_OFFSET_IMM:
29264       if (section->use_rela_p)
29265         {
29266           code = fixp->fx_r_type;
29267           break;
29268         }
29269
29270       if (fixp->fx_addsy != NULL
29271           && !S_IS_DEFINED (fixp->fx_addsy)
29272           && S_IS_LOCAL (fixp->fx_addsy))
29273         {
29274           as_bad_where (fixp->fx_file, fixp->fx_line,
29275                         _("undefined local label `%s'"),
29276                         S_GET_NAME (fixp->fx_addsy));
29277           return NULL;
29278         }
29279
29280       as_bad_where (fixp->fx_file, fixp->fx_line,
29281                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
29282       return NULL;
29283
29284     default:
29285       {
29286         const char * type;
29287
29288         switch (fixp->fx_r_type)
29289           {
29290           case BFD_RELOC_NONE:             type = "NONE";         break;
29291           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
29292           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
29293           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
29294           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
29295           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
29296           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
29297           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
29298           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
29299           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
29300           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
29301           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
29302           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
29303           default:                         type = _("<unknown>"); break;
29304           }
29305         as_bad_where (fixp->fx_file, fixp->fx_line,
29306                       _("cannot represent %s relocation in this object file format"),
29307                       type);
29308         return NULL;
29309       }
29310     }
29311
29312 #ifdef OBJ_ELF
29313   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
29314       && GOT_symbol
29315       && fixp->fx_addsy == GOT_symbol)
29316     {
29317       code = BFD_RELOC_ARM_GOTPC;
29318       reloc->addend = fixp->fx_offset = reloc->address;
29319     }
29320 #endif
29321
29322   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
29323
29324   if (reloc->howto == NULL)
29325     {
29326       as_bad_where (fixp->fx_file, fixp->fx_line,
29327                     _("cannot represent %s relocation in this object file format"),
29328                     bfd_get_reloc_code_name (code));
29329       return NULL;
29330     }
29331
29332   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
29333      vtable entry to be used in the relocation's section offset.  */
29334   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
29335     reloc->address = fixp->fx_offset;
29336
29337   return reloc;
29338 }
29339
29340 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
29341
29342 void
29343 cons_fix_new_arm (fragS *       frag,
29344                   int           where,
29345                   int           size,
29346                   expressionS * exp,
29347                   bfd_reloc_code_real_type reloc)
29348 {
29349   int pcrel = 0;
29350
29351   /* Pick a reloc.
29352      FIXME: @@ Should look at CPU word size.  */
29353   switch (size)
29354     {
29355     case 1:
29356       reloc = BFD_RELOC_8;
29357       break;
29358     case 2:
29359       reloc = BFD_RELOC_16;
29360       break;
29361     case 4:
29362     default:
29363       reloc = BFD_RELOC_32;
29364       break;
29365     case 8:
29366       reloc = BFD_RELOC_64;
29367       break;
29368     }
29369
29370 #ifdef TE_PE
29371   if (exp->X_op == O_secrel)
29372   {
29373     exp->X_op = O_symbol;
29374     reloc = BFD_RELOC_32_SECREL;
29375   }
29376 #endif
29377
29378   fix_new_exp (frag, where, size, exp, pcrel, reloc);
29379 }
29380
29381 #if defined (OBJ_COFF)
29382 void
29383 arm_validate_fix (fixS * fixP)
29384 {
29385   /* If the destination of the branch is a defined symbol which does not have
29386      the THUMB_FUNC attribute, then we must be calling a function which has
29387      the (interfacearm) attribute.  We look for the Thumb entry point to that
29388      function and change the branch to refer to that function instead.  */
29389   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
29390       && fixP->fx_addsy != NULL
29391       && S_IS_DEFINED (fixP->fx_addsy)
29392       && ! THUMB_IS_FUNC (fixP->fx_addsy))
29393     {
29394       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
29395     }
29396 }
29397 #endif
29398
29399
29400 int
29401 arm_force_relocation (struct fix * fixp)
29402 {
29403 #if defined (OBJ_COFF) && defined (TE_PE)
29404   if (fixp->fx_r_type == BFD_RELOC_RVA)
29405     return 1;
29406 #endif
29407
29408   /* In case we have a call or a branch to a function in ARM ISA mode from
29409      a thumb function or vice-versa force the relocation. These relocations
29410      are cleared off for some cores that might have blx and simple transformations
29411      are possible.  */
29412
29413 #ifdef OBJ_ELF
29414   switch (fixp->fx_r_type)
29415     {
29416     case BFD_RELOC_ARM_PCREL_JUMP:
29417     case BFD_RELOC_ARM_PCREL_CALL:
29418     case BFD_RELOC_THUMB_PCREL_BLX:
29419       if (THUMB_IS_FUNC (fixp->fx_addsy))
29420         return 1;
29421       break;
29422
29423     case BFD_RELOC_ARM_PCREL_BLX:
29424     case BFD_RELOC_THUMB_PCREL_BRANCH25:
29425     case BFD_RELOC_THUMB_PCREL_BRANCH20:
29426     case BFD_RELOC_THUMB_PCREL_BRANCH23:
29427       if (ARM_IS_FUNC (fixp->fx_addsy))
29428         return 1;
29429       break;
29430
29431     default:
29432       break;
29433     }
29434 #endif
29435
29436   /* Resolve these relocations even if the symbol is extern or weak.
29437      Technically this is probably wrong due to symbol preemption.
29438      In practice these relocations do not have enough range to be useful
29439      at dynamic link time, and some code (e.g. in the Linux kernel)
29440      expects these references to be resolved.  */
29441   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
29442       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
29443       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
29444       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
29445       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
29446       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
29447       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
29448       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
29449       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
29450       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
29451       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
29452       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
29453       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
29454       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
29455     return 0;
29456
29457   /* Always leave these relocations for the linker.  */
29458   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
29459        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
29460       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
29461     return 1;
29462
29463   /* Always generate relocations against function symbols.  */
29464   if (fixp->fx_r_type == BFD_RELOC_32
29465       && fixp->fx_addsy
29466       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
29467     return 1;
29468
29469   return generic_force_reloc (fixp);
29470 }
29471
29472 #if defined (OBJ_ELF) || defined (OBJ_COFF)
29473 /* Relocations against function names must be left unadjusted,
29474    so that the linker can use this information to generate interworking
29475    stubs.  The MIPS version of this function
29476    also prevents relocations that are mips-16 specific, but I do not
29477    know why it does this.
29478
29479    FIXME:
29480    There is one other problem that ought to be addressed here, but
29481    which currently is not:  Taking the address of a label (rather
29482    than a function) and then later jumping to that address.  Such
29483    addresses also ought to have their bottom bit set (assuming that
29484    they reside in Thumb code), but at the moment they will not.  */
29485
29486 bfd_boolean
29487 arm_fix_adjustable (fixS * fixP)
29488 {
29489   if (fixP->fx_addsy == NULL)
29490     return 1;
29491
29492   /* Preserve relocations against symbols with function type.  */
29493   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
29494     return FALSE;
29495
29496   if (THUMB_IS_FUNC (fixP->fx_addsy)
29497       && fixP->fx_subsy == NULL)
29498     return FALSE;
29499
29500   /* We need the symbol name for the VTABLE entries.  */
29501   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
29502       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
29503     return FALSE;
29504
29505   /* Don't allow symbols to be discarded on GOT related relocs.  */
29506   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
29507       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
29508       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
29509       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
29510       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32_FDPIC
29511       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
29512       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
29513       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32_FDPIC
29514       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
29515       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32_FDPIC
29516       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
29517       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
29518       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
29519       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
29520       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
29521       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
29522       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
29523     return FALSE;
29524
29525   /* Similarly for group relocations.  */
29526   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
29527        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
29528       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
29529     return FALSE;
29530
29531   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
29532   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
29533       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
29534       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
29535       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
29536       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
29537       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
29538       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
29539       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
29540     return FALSE;
29541
29542   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
29543      offsets, so keep these symbols.  */
29544   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
29545       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
29546     return FALSE;
29547
29548   return TRUE;
29549 }
29550 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
29551
29552 #ifdef OBJ_ELF
29553 const char *
29554 elf32_arm_target_format (void)
29555 {
29556 #ifdef TE_SYMBIAN
29557   return (target_big_endian
29558           ? "elf32-bigarm-symbian"
29559           : "elf32-littlearm-symbian");
29560 #elif defined (TE_VXWORKS)
29561   return (target_big_endian
29562           ? "elf32-bigarm-vxworks"
29563           : "elf32-littlearm-vxworks");
29564 #elif defined (TE_NACL)
29565   return (target_big_endian
29566           ? "elf32-bigarm-nacl"
29567           : "elf32-littlearm-nacl");
29568 #else
29569   if (arm_fdpic)
29570     {
29571       if (target_big_endian)
29572         return "elf32-bigarm-fdpic";
29573       else
29574         return "elf32-littlearm-fdpic";
29575     }
29576   else
29577     {
29578       if (target_big_endian)
29579         return "elf32-bigarm";
29580       else
29581         return "elf32-littlearm";
29582     }
29583 #endif
29584 }
29585
29586 void
29587 armelf_frob_symbol (symbolS * symp,
29588                     int *     puntp)
29589 {
29590   elf_frob_symbol (symp, puntp);
29591 }
29592 #endif
29593
29594 /* MD interface: Finalization.  */
29595
29596 void
29597 arm_cleanup (void)
29598 {
29599   literal_pool * pool;
29600
29601   /* Ensure that all the predication blocks are properly closed.  */
29602   check_pred_blocks_finished ();
29603
29604   for (pool = list_of_pools; pool; pool = pool->next)
29605     {
29606       /* Put it at the end of the relevant section.  */
29607       subseg_set (pool->section, pool->sub_section);
29608 #ifdef OBJ_ELF
29609       arm_elf_change_section ();
29610 #endif
29611       s_ltorg (0);
29612     }
29613 }
29614
29615 #ifdef OBJ_ELF
29616 /* Remove any excess mapping symbols generated for alignment frags in
29617    SEC.  We may have created a mapping symbol before a zero byte
29618    alignment; remove it if there's a mapping symbol after the
29619    alignment.  */
29620 static void
29621 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
29622                        void *dummy ATTRIBUTE_UNUSED)
29623 {
29624   segment_info_type *seginfo = seg_info (sec);
29625   fragS *fragp;
29626
29627   if (seginfo == NULL || seginfo->frchainP == NULL)
29628     return;
29629
29630   for (fragp = seginfo->frchainP->frch_root;
29631        fragp != NULL;
29632        fragp = fragp->fr_next)
29633     {
29634       symbolS *sym = fragp->tc_frag_data.last_map;
29635       fragS *next = fragp->fr_next;
29636
29637       /* Variable-sized frags have been converted to fixed size by
29638          this point.  But if this was variable-sized to start with,
29639          there will be a fixed-size frag after it.  So don't handle
29640          next == NULL.  */
29641       if (sym == NULL || next == NULL)
29642         continue;
29643
29644       if (S_GET_VALUE (sym) < next->fr_address)
29645         /* Not at the end of this frag.  */
29646         continue;
29647       know (S_GET_VALUE (sym) == next->fr_address);
29648
29649       do
29650         {
29651           if (next->tc_frag_data.first_map != NULL)
29652             {
29653               /* Next frag starts with a mapping symbol.  Discard this
29654                  one.  */
29655               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
29656               break;
29657             }
29658
29659           if (next->fr_next == NULL)
29660             {
29661               /* This mapping symbol is at the end of the section.  Discard
29662                  it.  */
29663               know (next->fr_fix == 0 && next->fr_var == 0);
29664               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
29665               break;
29666             }
29667
29668           /* As long as we have empty frags without any mapping symbols,
29669              keep looking.  */
29670           /* If the next frag is non-empty and does not start with a
29671              mapping symbol, then this mapping symbol is required.  */
29672           if (next->fr_address != next->fr_next->fr_address)
29673             break;
29674
29675           next = next->fr_next;
29676         }
29677       while (next != NULL);
29678     }
29679 }
29680 #endif
29681
29682 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
29683    ARM ones.  */
29684
29685 void
29686 arm_adjust_symtab (void)
29687 {
29688 #ifdef OBJ_COFF
29689   symbolS * sym;
29690
29691   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
29692     {
29693       if (ARM_IS_THUMB (sym))
29694         {
29695           if (THUMB_IS_FUNC (sym))
29696             {
29697               /* Mark the symbol as a Thumb function.  */
29698               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
29699                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
29700                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
29701
29702               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
29703                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
29704               else
29705                 as_bad (_("%s: unexpected function type: %d"),
29706                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
29707             }
29708           else switch (S_GET_STORAGE_CLASS (sym))
29709             {
29710             case C_EXT:
29711               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
29712               break;
29713             case C_STAT:
29714               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
29715               break;
29716             case C_LABEL:
29717               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
29718               break;
29719             default:
29720               /* Do nothing.  */
29721               break;
29722             }
29723         }
29724
29725       if (ARM_IS_INTERWORK (sym))
29726         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
29727     }
29728 #endif
29729 #ifdef OBJ_ELF
29730   symbolS * sym;
29731   char      bind;
29732
29733   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
29734     {
29735       if (ARM_IS_THUMB (sym))
29736         {
29737           elf_symbol_type * elf_sym;
29738
29739           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
29740           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
29741
29742           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
29743                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
29744             {
29745               /* If it's a .thumb_func, declare it as so,
29746                  otherwise tag label as .code 16.  */
29747               if (THUMB_IS_FUNC (sym))
29748                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
29749                                          ST_BRANCH_TO_THUMB);
29750               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
29751                 elf_sym->internal_elf_sym.st_info =
29752                   ELF_ST_INFO (bind, STT_ARM_16BIT);
29753             }
29754         }
29755     }
29756
29757   /* Remove any overlapping mapping symbols generated by alignment frags.  */
29758   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
29759   /* Now do generic ELF adjustments.  */
29760   elf_adjust_symtab ();
29761 #endif
29762 }
29763
29764 /* MD interface: Initialization.  */
29765
29766 static void
29767 set_constant_flonums (void)
29768 {
29769   int i;
29770
29771   for (i = 0; i < NUM_FLOAT_VALS; i++)
29772     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
29773       abort ();
29774 }
29775
29776 /* Auto-select Thumb mode if it's the only available instruction set for the
29777    given architecture.  */
29778
29779 static void
29780 autoselect_thumb_from_cpu_variant (void)
29781 {
29782   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
29783     opcode_select (16);
29784 }
29785
29786 void
29787 md_begin (void)
29788 {
29789   unsigned mach;
29790   unsigned int i;
29791
29792   if (   (arm_ops_hsh = hash_new ()) == NULL
29793       || (arm_cond_hsh = hash_new ()) == NULL
29794       || (arm_vcond_hsh = hash_new ()) == NULL
29795       || (arm_shift_hsh = hash_new ()) == NULL
29796       || (arm_psr_hsh = hash_new ()) == NULL
29797       || (arm_v7m_psr_hsh = hash_new ()) == NULL
29798       || (arm_reg_hsh = hash_new ()) == NULL
29799       || (arm_reloc_hsh = hash_new ()) == NULL
29800       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
29801     as_fatal (_("virtual memory exhausted"));
29802
29803   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
29804     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
29805   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
29806     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
29807   for (i = 0; i < sizeof (vconds) / sizeof (struct asm_cond); i++)
29808     hash_insert (arm_vcond_hsh, vconds[i].template_name, (void *) (vconds + i));
29809   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
29810     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
29811   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
29812     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
29813   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
29814     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
29815                  (void *) (v7m_psrs + i));
29816   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
29817     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
29818   for (i = 0;
29819        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
29820        i++)
29821     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
29822                  (void *) (barrier_opt_names + i));
29823 #ifdef OBJ_ELF
29824   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
29825     {
29826       struct reloc_entry * entry = reloc_names + i;
29827
29828       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
29829         /* This makes encode_branch() use the EABI versions of this relocation.  */
29830         entry->reloc = BFD_RELOC_UNUSED;
29831
29832       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
29833     }
29834 #endif
29835
29836   set_constant_flonums ();
29837
29838   /* Set the cpu variant based on the command-line options.  We prefer
29839      -mcpu= over -march= if both are set (as for GCC); and we prefer
29840      -mfpu= over any other way of setting the floating point unit.
29841      Use of legacy options with new options are faulted.  */
29842   if (legacy_cpu)
29843     {
29844       if (mcpu_cpu_opt || march_cpu_opt)
29845         as_bad (_("use of old and new-style options to set CPU type"));
29846
29847       selected_arch = *legacy_cpu;
29848     }
29849   else if (mcpu_cpu_opt)
29850     {
29851       selected_arch = *mcpu_cpu_opt;
29852       selected_ext = *mcpu_ext_opt;
29853     }
29854   else if (march_cpu_opt)
29855     {
29856       selected_arch = *march_cpu_opt;
29857       selected_ext = *march_ext_opt;
29858     }
29859   ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
29860
29861   if (legacy_fpu)
29862     {
29863       if (mfpu_opt)
29864         as_bad (_("use of old and new-style options to set FPU type"));
29865
29866       selected_fpu = *legacy_fpu;
29867     }
29868   else if (mfpu_opt)
29869     selected_fpu = *mfpu_opt;
29870   else
29871     {
29872 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
29873         || defined (TE_NetBSD) || defined (TE_VXWORKS))
29874       /* Some environments specify a default FPU.  If they don't, infer it
29875          from the processor.  */
29876       if (mcpu_fpu_opt)
29877         selected_fpu = *mcpu_fpu_opt;
29878       else if (march_fpu_opt)
29879         selected_fpu = *march_fpu_opt;
29880 #else
29881       selected_fpu = fpu_default;
29882 #endif
29883     }
29884
29885   if (ARM_FEATURE_ZERO (selected_fpu))
29886     {
29887       if (!no_cpu_selected ())
29888         selected_fpu = fpu_default;
29889       else
29890         selected_fpu = fpu_arch_fpa;
29891     }
29892
29893 #ifdef CPU_DEFAULT
29894   if (ARM_FEATURE_ZERO (selected_arch))
29895     {
29896       selected_arch = cpu_default;
29897       selected_cpu = selected_arch;
29898     }
29899   ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29900 #else
29901   /*  Autodection of feature mode: allow all features in cpu_variant but leave
29902       selected_cpu unset.  It will be set in aeabi_set_public_attributes ()
29903       after all instruction have been processed and we can decide what CPU
29904       should be selected.  */
29905   if (ARM_FEATURE_ZERO (selected_arch))
29906     ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
29907   else
29908     ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29909 #endif
29910
29911   autoselect_thumb_from_cpu_variant ();
29912
29913   arm_arch_used = thumb_arch_used = arm_arch_none;
29914
29915 #if defined OBJ_COFF || defined OBJ_ELF
29916   {
29917     unsigned int flags = 0;
29918
29919 #if defined OBJ_ELF
29920     flags = meabi_flags;
29921
29922     switch (meabi_flags)
29923       {
29924       case EF_ARM_EABI_UNKNOWN:
29925 #endif
29926         /* Set the flags in the private structure.  */
29927         if (uses_apcs_26)      flags |= F_APCS26;
29928         if (support_interwork) flags |= F_INTERWORK;
29929         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
29930         if (pic_code)          flags |= F_PIC;
29931         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
29932           flags |= F_SOFT_FLOAT;
29933
29934         switch (mfloat_abi_opt)
29935           {
29936           case ARM_FLOAT_ABI_SOFT:
29937           case ARM_FLOAT_ABI_SOFTFP:
29938             flags |= F_SOFT_FLOAT;
29939             break;
29940
29941           case ARM_FLOAT_ABI_HARD:
29942             if (flags & F_SOFT_FLOAT)
29943               as_bad (_("hard-float conflicts with specified fpu"));
29944             break;
29945           }
29946
29947         /* Using pure-endian doubles (even if soft-float).      */
29948         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
29949           flags |= F_VFP_FLOAT;
29950
29951 #if defined OBJ_ELF
29952         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
29953             flags |= EF_ARM_MAVERICK_FLOAT;
29954         break;
29955
29956       case EF_ARM_EABI_VER4:
29957       case EF_ARM_EABI_VER5:
29958         /* No additional flags to set.  */
29959         break;
29960
29961       default:
29962         abort ();
29963       }
29964 #endif
29965     bfd_set_private_flags (stdoutput, flags);
29966
29967     /* We have run out flags in the COFF header to encode the
29968        status of ATPCS support, so instead we create a dummy,
29969        empty, debug section called .arm.atpcs.  */
29970     if (atpcs)
29971       {
29972         asection * sec;
29973
29974         sec = bfd_make_section (stdoutput, ".arm.atpcs");
29975
29976         if (sec != NULL)
29977           {
29978             bfd_set_section_flags
29979               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
29980             bfd_set_section_size (stdoutput, sec, 0);
29981             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
29982           }
29983       }
29984   }
29985 #endif
29986
29987   /* Record the CPU type as well.  */
29988   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
29989     mach = bfd_mach_arm_iWMMXt2;
29990   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
29991     mach = bfd_mach_arm_iWMMXt;
29992   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
29993     mach = bfd_mach_arm_XScale;
29994   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
29995     mach = bfd_mach_arm_ep9312;
29996   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
29997     mach = bfd_mach_arm_5TE;
29998   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
29999     {
30000       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
30001         mach = bfd_mach_arm_5T;
30002       else
30003         mach = bfd_mach_arm_5;
30004     }
30005   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
30006     {
30007       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
30008         mach = bfd_mach_arm_4T;
30009       else
30010         mach = bfd_mach_arm_4;
30011     }
30012   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
30013     mach = bfd_mach_arm_3M;
30014   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
30015     mach = bfd_mach_arm_3;
30016   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
30017     mach = bfd_mach_arm_2a;
30018   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
30019     mach = bfd_mach_arm_2;
30020   else
30021     mach = bfd_mach_arm_unknown;
30022
30023   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
30024 }
30025
30026 /* Command line processing.  */
30027
30028 /* md_parse_option
30029       Invocation line includes a switch not recognized by the base assembler.
30030       See if it's a processor-specific option.
30031
30032       This routine is somewhat complicated by the need for backwards
30033       compatibility (since older releases of gcc can't be changed).
30034       The new options try to make the interface as compatible as
30035       possible with GCC.
30036
30037       New options (supported) are:
30038
30039               -mcpu=<cpu name>           Assemble for selected processor
30040               -march=<architecture name> Assemble for selected architecture
30041               -mfpu=<fpu architecture>   Assemble for selected FPU.
30042               -EB/-mbig-endian           Big-endian
30043               -EL/-mlittle-endian        Little-endian
30044               -k                         Generate PIC code
30045               -mthumb                    Start in Thumb mode
30046               -mthumb-interwork          Code supports ARM/Thumb interworking
30047
30048               -m[no-]warn-deprecated     Warn about deprecated features
30049               -m[no-]warn-syms           Warn when symbols match instructions
30050
30051       For now we will also provide support for:
30052
30053               -mapcs-32                  32-bit Program counter
30054               -mapcs-26                  26-bit Program counter
30055               -macps-float               Floats passed in FP registers
30056               -mapcs-reentrant           Reentrant code
30057               -matpcs
30058       (sometime these will probably be replaced with -mapcs=<list of options>
30059       and -matpcs=<list of options>)
30060
30061       The remaining options are only supported for back-wards compatibility.
30062       Cpu variants, the arm part is optional:
30063               -m[arm]1                Currently not supported.
30064               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
30065               -m[arm]3                Arm 3 processor
30066               -m[arm]6[xx],           Arm 6 processors
30067               -m[arm]7[xx][t][[d]m]   Arm 7 processors
30068               -m[arm]8[10]            Arm 8 processors
30069               -m[arm]9[20][tdmi]      Arm 9 processors
30070               -mstrongarm[110[0]]     StrongARM processors
30071               -mxscale                XScale processors
30072               -m[arm]v[2345[t[e]]]    Arm architectures
30073               -mall                   All (except the ARM1)
30074       FP variants:
30075               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
30076               -mfpe-old               (No float load/store multiples)
30077               -mvfpxd                 VFP Single precision
30078               -mvfp                   All VFP
30079               -mno-fpu                Disable all floating point instructions
30080
30081       The following CPU names are recognized:
30082               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
30083               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
30084               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
30085               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
30086               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
30087               arm10t arm10e, arm1020t, arm1020e, arm10200e,
30088               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
30089
30090       */
30091
30092 const char * md_shortopts = "m:k";
30093
30094 #ifdef ARM_BI_ENDIAN
30095 #define OPTION_EB (OPTION_MD_BASE + 0)
30096 #define OPTION_EL (OPTION_MD_BASE + 1)
30097 #else
30098 #if TARGET_BYTES_BIG_ENDIAN
30099 #define OPTION_EB (OPTION_MD_BASE + 0)
30100 #else
30101 #define OPTION_EL (OPTION_MD_BASE + 1)
30102 #endif
30103 #endif
30104 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
30105 #define OPTION_FDPIC (OPTION_MD_BASE + 3)
30106
30107 struct option md_longopts[] =
30108 {
30109 #ifdef OPTION_EB
30110   {"EB", no_argument, NULL, OPTION_EB},
30111 #endif
30112 #ifdef OPTION_EL
30113   {"EL", no_argument, NULL, OPTION_EL},
30114 #endif
30115   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
30116 #ifdef OBJ_ELF
30117   {"fdpic", no_argument, NULL, OPTION_FDPIC},
30118 #endif
30119   {NULL, no_argument, NULL, 0}
30120 };
30121
30122 size_t md_longopts_size = sizeof (md_longopts);
30123
30124 struct arm_option_table
30125 {
30126   const char *  option;         /* Option name to match.  */
30127   const char *  help;           /* Help information.  */
30128   int *         var;            /* Variable to change.  */
30129   int           value;          /* What to change it to.  */
30130   const char *  deprecated;     /* If non-null, print this message.  */
30131 };
30132
30133 struct arm_option_table arm_opts[] =
30134 {
30135   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
30136   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
30137   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
30138    &support_interwork, 1, NULL},
30139   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
30140   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
30141   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
30142    1, NULL},
30143   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
30144   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
30145   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
30146   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
30147    NULL},
30148
30149   /* These are recognized by the assembler, but have no affect on code.  */
30150   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
30151   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
30152
30153   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
30154   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
30155    &warn_on_deprecated, 0, NULL},
30156   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
30157   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
30158   {NULL, NULL, NULL, 0, NULL}
30159 };
30160
30161 struct arm_legacy_option_table
30162 {
30163   const char *              option;             /* Option name to match.  */
30164   const arm_feature_set **  var;                /* Variable to change.  */
30165   const arm_feature_set     value;              /* What to change it to.  */
30166   const char *              deprecated;         /* If non-null, print this message.  */
30167 };
30168
30169 const struct arm_legacy_option_table arm_legacy_opts[] =
30170 {
30171   /* DON'T add any new processors to this list -- we want the whole list
30172      to go away...  Add them to the processors table instead.  */
30173   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
30174   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
30175   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
30176   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
30177   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
30178   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
30179   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
30180   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
30181   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
30182   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
30183   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
30184   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
30185   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
30186   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
30187   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
30188   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
30189   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
30190   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
30191   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
30192   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
30193   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
30194   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
30195   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
30196   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
30197   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
30198   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
30199   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
30200   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
30201   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
30202   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
30203   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
30204   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
30205   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
30206   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
30207   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
30208   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
30209   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
30210   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
30211   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
30212   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
30213   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
30214   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
30215   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
30216   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
30217   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
30218   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
30219   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30220   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30221   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30222   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30223   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
30224   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
30225   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
30226   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
30227   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
30228   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
30229   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
30230   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
30231   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
30232   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
30233   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
30234   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
30235   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
30236   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
30237   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
30238   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
30239   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
30240   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
30241   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
30242   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
30243    N_("use -mcpu=strongarm110")},
30244   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
30245    N_("use -mcpu=strongarm1100")},
30246   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
30247    N_("use -mcpu=strongarm1110")},
30248   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
30249   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
30250   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
30251
30252   /* Architecture variants -- don't add any more to this list either.  */
30253   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
30254   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
30255   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
30256   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
30257   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
30258   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
30259   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
30260   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
30261   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
30262   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
30263   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
30264   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
30265   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
30266   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
30267   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
30268   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
30269   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
30270   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
30271
30272   /* Floating point variants -- don't add any more to this list either.  */
30273   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
30274   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
30275   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
30276   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
30277    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
30278
30279   {NULL, NULL, ARM_ARCH_NONE, NULL}
30280 };
30281
30282 struct arm_cpu_option_table
30283 {
30284   const char *           name;
30285   size_t                 name_len;
30286   const arm_feature_set  value;
30287   const arm_feature_set  ext;
30288   /* For some CPUs we assume an FPU unless the user explicitly sets
30289      -mfpu=...  */
30290   const arm_feature_set  default_fpu;
30291   /* The canonical name of the CPU, or NULL to use NAME converted to upper
30292      case.  */
30293   const char *           canonical_name;
30294 };
30295
30296 /* This list should, at a minimum, contain all the cpu names
30297    recognized by GCC.  */
30298 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
30299
30300 static const struct arm_cpu_option_table arm_cpus[] =
30301 {
30302   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
30303                ARM_ARCH_NONE,
30304                FPU_ARCH_FPA),
30305   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
30306                ARM_ARCH_NONE,
30307                FPU_ARCH_FPA),
30308   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
30309                ARM_ARCH_NONE,
30310                FPU_ARCH_FPA),
30311   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
30312                ARM_ARCH_NONE,
30313                FPU_ARCH_FPA),
30314   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
30315                ARM_ARCH_NONE,
30316                FPU_ARCH_FPA),
30317   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
30318                ARM_ARCH_NONE,
30319                FPU_ARCH_FPA),
30320   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
30321                ARM_ARCH_NONE,
30322                FPU_ARCH_FPA),
30323   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
30324                ARM_ARCH_NONE,
30325                FPU_ARCH_FPA),
30326   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
30327                ARM_ARCH_NONE,
30328                FPU_ARCH_FPA),
30329   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
30330                ARM_ARCH_NONE,
30331                FPU_ARCH_FPA),
30332   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
30333                ARM_ARCH_NONE,
30334                FPU_ARCH_FPA),
30335   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
30336                ARM_ARCH_NONE,
30337                FPU_ARCH_FPA),
30338   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
30339                ARM_ARCH_NONE,
30340                FPU_ARCH_FPA),
30341   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
30342                ARM_ARCH_NONE,
30343                FPU_ARCH_FPA),
30344   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
30345                ARM_ARCH_NONE,
30346                FPU_ARCH_FPA),
30347   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
30348                ARM_ARCH_NONE,
30349                FPU_ARCH_FPA),
30350   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
30351                ARM_ARCH_NONE,
30352                FPU_ARCH_FPA),
30353   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
30354                ARM_ARCH_NONE,
30355                FPU_ARCH_FPA),
30356   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
30357                ARM_ARCH_NONE,
30358                FPU_ARCH_FPA),
30359   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
30360                ARM_ARCH_NONE,
30361                FPU_ARCH_FPA),
30362   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
30363                ARM_ARCH_NONE,
30364                FPU_ARCH_FPA),
30365   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
30366                ARM_ARCH_NONE,
30367                FPU_ARCH_FPA),
30368   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
30369                ARM_ARCH_NONE,
30370                FPU_ARCH_FPA),
30371   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
30372                ARM_ARCH_NONE,
30373                FPU_ARCH_FPA),
30374   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
30375                ARM_ARCH_NONE,
30376                FPU_ARCH_FPA),
30377   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
30378                ARM_ARCH_NONE,
30379                FPU_ARCH_FPA),
30380   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
30381                ARM_ARCH_NONE,
30382                FPU_ARCH_FPA),
30383   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
30384                ARM_ARCH_NONE,
30385                FPU_ARCH_FPA),
30386   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
30387                ARM_ARCH_NONE,
30388                FPU_ARCH_FPA),
30389   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
30390                ARM_ARCH_NONE,
30391                FPU_ARCH_FPA),
30392   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
30393                ARM_ARCH_NONE,
30394                FPU_ARCH_FPA),
30395   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
30396                ARM_ARCH_NONE,
30397                FPU_ARCH_FPA),
30398   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
30399                ARM_ARCH_NONE,
30400                FPU_ARCH_FPA),
30401   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
30402                ARM_ARCH_NONE,
30403                FPU_ARCH_FPA),
30404   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
30405                ARM_ARCH_NONE,
30406                FPU_ARCH_FPA),
30407   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
30408                ARM_ARCH_NONE,
30409                FPU_ARCH_FPA),
30410   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
30411                ARM_ARCH_NONE,
30412                FPU_ARCH_FPA),
30413   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
30414                ARM_ARCH_NONE,
30415                FPU_ARCH_FPA),
30416   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
30417                ARM_ARCH_NONE,
30418                FPU_ARCH_FPA),
30419   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
30420                ARM_ARCH_NONE,
30421                FPU_ARCH_FPA),
30422   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
30423                ARM_ARCH_NONE,
30424                FPU_ARCH_FPA),
30425   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
30426                ARM_ARCH_NONE,
30427                FPU_ARCH_FPA),
30428   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
30429                ARM_ARCH_NONE,
30430                FPU_ARCH_FPA),
30431   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
30432                ARM_ARCH_NONE,
30433                FPU_ARCH_FPA),
30434   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
30435                ARM_ARCH_NONE,
30436                FPU_ARCH_FPA),
30437   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
30438                ARM_ARCH_NONE,
30439                FPU_ARCH_FPA),
30440
30441   /* For V5 or later processors we default to using VFP; but the user
30442      should really set the FPU type explicitly.  */
30443   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
30444                ARM_ARCH_NONE,
30445                FPU_ARCH_VFP_V2),
30446   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
30447                ARM_ARCH_NONE,
30448                FPU_ARCH_VFP_V2),
30449   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
30450                ARM_ARCH_NONE,
30451                FPU_ARCH_VFP_V2),
30452   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
30453                ARM_ARCH_NONE,
30454                FPU_ARCH_VFP_V2),
30455   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
30456                ARM_ARCH_NONE,
30457                FPU_ARCH_VFP_V2),
30458   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
30459                ARM_ARCH_NONE,
30460                FPU_ARCH_VFP_V2),
30461   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
30462                ARM_ARCH_NONE,
30463                FPU_ARCH_VFP_V2),
30464   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
30465                ARM_ARCH_NONE,
30466                FPU_ARCH_VFP_V2),
30467   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
30468                ARM_ARCH_NONE,
30469                FPU_ARCH_VFP_V2),
30470   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
30471                ARM_ARCH_NONE,
30472                FPU_ARCH_VFP_V2),
30473   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
30474                ARM_ARCH_NONE,
30475                FPU_ARCH_VFP_V2),
30476   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
30477                ARM_ARCH_NONE,
30478                FPU_ARCH_VFP_V2),
30479   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
30480                ARM_ARCH_NONE,
30481                FPU_ARCH_VFP_V1),
30482   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
30483                ARM_ARCH_NONE,
30484                FPU_ARCH_VFP_V1),
30485   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
30486                ARM_ARCH_NONE,
30487                FPU_ARCH_VFP_V2),
30488   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
30489                ARM_ARCH_NONE,
30490                FPU_ARCH_VFP_V2),
30491   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
30492                ARM_ARCH_NONE,
30493                FPU_ARCH_VFP_V1),
30494   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
30495                ARM_ARCH_NONE,
30496                FPU_ARCH_VFP_V2),
30497   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
30498                ARM_ARCH_NONE,
30499                FPU_ARCH_VFP_V2),
30500   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
30501                ARM_ARCH_NONE,
30502                FPU_ARCH_VFP_V2),
30503   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
30504                ARM_ARCH_NONE,
30505                FPU_ARCH_VFP_V2),
30506   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
30507                ARM_ARCH_NONE,
30508                FPU_ARCH_VFP_V2),
30509   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
30510                ARM_ARCH_NONE,
30511                FPU_ARCH_VFP_V2),
30512   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
30513                ARM_ARCH_NONE,
30514                FPU_ARCH_VFP_V2),
30515   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
30516                ARM_ARCH_NONE,
30517                FPU_ARCH_VFP_V2),
30518   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
30519                ARM_ARCH_NONE,
30520                FPU_ARCH_VFP_V2),
30521   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
30522                ARM_ARCH_NONE,
30523                FPU_NONE),
30524   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
30525                ARM_ARCH_NONE,
30526                FPU_NONE),
30527   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
30528                ARM_ARCH_NONE,
30529                FPU_ARCH_VFP_V2),
30530   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
30531                ARM_ARCH_NONE,
30532                FPU_ARCH_VFP_V2),
30533   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
30534                ARM_ARCH_NONE,
30535                FPU_ARCH_VFP_V2),
30536   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
30537                ARM_ARCH_NONE,
30538                FPU_NONE),
30539   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
30540                ARM_ARCH_NONE,
30541                FPU_NONE),
30542   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
30543                ARM_ARCH_NONE,
30544                FPU_ARCH_VFP_V2),
30545   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
30546                ARM_ARCH_NONE,
30547                FPU_NONE),
30548   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
30549                ARM_ARCH_NONE,
30550                FPU_ARCH_VFP_V2),
30551   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
30552                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30553                FPU_NONE),
30554   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
30555                ARM_ARCH_NONE,
30556                FPU_ARCH_NEON_VFP_V4),
30557   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
30558                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
30559                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
30560   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
30561                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30562                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
30563   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
30564                ARM_ARCH_NONE,
30565                FPU_ARCH_NEON_VFP_V4),
30566   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
30567                ARM_ARCH_NONE,
30568                FPU_ARCH_NEON_VFP_V4),
30569   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
30570                ARM_ARCH_NONE,
30571                FPU_ARCH_NEON_VFP_V4),
30572   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
30573                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30574                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30575   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
30576                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30577                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30578   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
30579                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30580                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30581   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
30582                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30583                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30584   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
30585                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30586                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30587   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
30588               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30589               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30590   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
30591               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30592               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30593   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
30594                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30595                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30596   ARM_CPU_OPT ("cortex-a76",    "Cortex-A76",          ARM_ARCH_V8_2A,
30597                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30598                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30599   ARM_CPU_OPT ("ares",    "Ares",              ARM_ARCH_V8_2A,
30600                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30601                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30602   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
30603                ARM_ARCH_NONE,
30604                FPU_NONE),
30605   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
30606                ARM_ARCH_NONE,
30607                FPU_ARCH_VFP_V3D16),
30608   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
30609                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30610                FPU_NONE),
30611   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
30612                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30613                FPU_ARCH_VFP_V3D16),
30614   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
30615                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30616                FPU_ARCH_VFP_V3D16),
30617   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
30618               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30619               FPU_ARCH_NEON_VFP_ARMV8),
30620   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
30621                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30622                FPU_NONE),
30623   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
30624                ARM_ARCH_NONE,
30625                FPU_NONE),
30626   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
30627                ARM_ARCH_NONE,
30628                FPU_NONE),
30629   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
30630                ARM_ARCH_NONE,
30631                FPU_NONE),
30632   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
30633                ARM_ARCH_NONE,
30634                FPU_NONE),
30635   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
30636                ARM_ARCH_NONE,
30637                FPU_NONE),
30638   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
30639                ARM_ARCH_NONE,
30640                FPU_NONE),
30641   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
30642                ARM_ARCH_NONE,
30643                FPU_NONE),
30644   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
30645                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30646                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30647   ARM_CPU_OPT ("neoverse-n1",    "Neoverse N1",        ARM_ARCH_V8_2A,
30648                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30649                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30650   /* ??? XSCALE is really an architecture.  */
30651   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
30652                ARM_ARCH_NONE,
30653                FPU_ARCH_VFP_V2),
30654
30655   /* ??? iwmmxt is not a processor.  */
30656   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
30657                ARM_ARCH_NONE,
30658                FPU_ARCH_VFP_V2),
30659   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
30660                ARM_ARCH_NONE,
30661                FPU_ARCH_VFP_V2),
30662   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
30663                ARM_ARCH_NONE,
30664                FPU_ARCH_VFP_V2),
30665
30666   /* Maverick.  */
30667   ARM_CPU_OPT ("ep9312",          "ARM920T",
30668                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
30669                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
30670
30671   /* Marvell processors.  */
30672   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
30673                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30674                FPU_ARCH_VFP_V3D16),
30675   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
30676                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30677                FPU_ARCH_NEON_VFP_V4),
30678
30679   /* APM X-Gene family.  */
30680   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
30681                ARM_ARCH_NONE,
30682                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30683   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
30684                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30685                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30686
30687   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
30688 };
30689 #undef ARM_CPU_OPT
30690
30691 struct arm_ext_table
30692 {
30693   const char *            name;
30694   size_t                  name_len;
30695   const arm_feature_set   merge;
30696   const arm_feature_set   clear;
30697 };
30698
30699 struct arm_arch_option_table
30700 {
30701   const char *                  name;
30702   size_t                        name_len;
30703   const arm_feature_set         value;
30704   const arm_feature_set         default_fpu;
30705   const struct arm_ext_table *  ext_table;
30706 };
30707
30708 /* Used to add support for +E and +noE extension.  */
30709 #define ARM_EXT(E, M, C) { E, sizeof (E) - 1, M, C }
30710 /* Used to add support for a +E extension.  */
30711 #define ARM_ADD(E, M) { E, sizeof(E) - 1, M, ARM_ARCH_NONE }
30712 /* Used to add support for a +noE extension.  */
30713 #define ARM_REMOVE(E, C) { E, sizeof(E) -1, ARM_ARCH_NONE, C }
30714
30715 #define ALL_FP ARM_FEATURE (0, ARM_EXT2_FP16_INST | ARM_EXT2_FP16_FML, \
30716                             ~0 & ~FPU_ENDIAN_PURE)
30717
30718 static const struct arm_ext_table armv5te_ext_table[] =
30719 {
30720   ARM_EXT ("fp", FPU_ARCH_VFP_V2, ALL_FP),
30721   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30722 };
30723
30724 static const struct arm_ext_table armv7_ext_table[] =
30725 {
30726   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30727   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30728 };
30729
30730 static const struct arm_ext_table armv7ve_ext_table[] =
30731 {
30732   ARM_EXT ("fp", FPU_ARCH_VFP_V4D16, ALL_FP),
30733   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16),
30734   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
30735   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30736   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
30737   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),  /* Alias for +fp.  */
30738   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
30739
30740   ARM_EXT ("simd", FPU_ARCH_NEON_VFP_V4,
30741            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
30742
30743   /* Aliases for +simd.  */
30744   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
30745
30746   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30747   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30748   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
30749
30750   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30751 };
30752
30753 static const struct arm_ext_table armv7a_ext_table[] =
30754 {
30755   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30756   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
30757   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
30758   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30759   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
30760   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),
30761   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
30762
30763   ARM_EXT ("simd", FPU_ARCH_VFP_V3_PLUS_NEON_V1,
30764            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
30765
30766   /* Aliases for +simd.  */
30767   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30768   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30769
30770   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
30771   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
30772
30773   ARM_ADD ("mp", ARM_FEATURE_CORE_LOW (ARM_EXT_MP)),
30774   ARM_ADD ("sec", ARM_FEATURE_CORE_LOW (ARM_EXT_SEC)),
30775   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30776 };
30777
30778 static const struct arm_ext_table armv7r_ext_table[] =
30779 {
30780   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V3xD),
30781   ARM_ADD ("vfpv3xd", FPU_ARCH_VFP_V3xD), /* Alias for +fp.sp.  */
30782   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30783   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
30784   ARM_ADD ("vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16),
30785   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30786   ARM_EXT ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
30787            ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV)),
30788   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30789 };
30790
30791 static const struct arm_ext_table armv7em_ext_table[] =
30792 {
30793   ARM_EXT ("fp", FPU_ARCH_VFP_V4_SP_D16, ALL_FP),
30794   /* Alias for +fp, used to be known as fpv4-sp-d16.  */
30795   ARM_ADD ("vfpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16),
30796   ARM_ADD ("fpv5", FPU_ARCH_VFP_V5_SP_D16),
30797   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
30798   ARM_ADD ("fpv5-d16", FPU_ARCH_VFP_V5D16),
30799   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30800 };
30801
30802 static const struct arm_ext_table armv8a_ext_table[] =
30803 {
30804   ARM_ADD ("crc", ARCH_CRC_ARMV8),
30805   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
30806   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30807            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30808
30809   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30810      should use the +simd option to turn on FP.  */
30811   ARM_REMOVE ("fp", ALL_FP),
30812   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30813   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30814   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30815 };
30816
30817
30818 static const struct arm_ext_table armv81a_ext_table[] =
30819 {
30820   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
30821   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
30822            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30823
30824   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30825      should use the +simd option to turn on FP.  */
30826   ARM_REMOVE ("fp", ALL_FP),
30827   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30828   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30829   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30830 };
30831
30832 static const struct arm_ext_table armv82a_ext_table[] =
30833 {
30834   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
30835   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_2_FP16),
30836   ARM_ADD ("fp16fml", FPU_ARCH_NEON_VFP_ARMV8_2_FP16FML),
30837   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
30838            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30839   ARM_ADD ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30840
30841   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30842      should use the +simd option to turn on FP.  */
30843   ARM_REMOVE ("fp", ALL_FP),
30844   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30845   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30846   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30847 };
30848
30849 static const struct arm_ext_table armv84a_ext_table[] =
30850 {
30851   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30852   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
30853   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
30854            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30855
30856   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30857      should use the +simd option to turn on FP.  */
30858   ARM_REMOVE ("fp", ALL_FP),
30859   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30860   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30861   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30862 };
30863
30864 static const struct arm_ext_table armv85a_ext_table[] =
30865 {
30866   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30867   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
30868   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
30869            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30870
30871   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30872      should use the +simd option to turn on FP.  */
30873   ARM_REMOVE ("fp", ALL_FP),
30874   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30875 };
30876
30877 static const struct arm_ext_table armv8m_main_ext_table[] =
30878 {
30879   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30880                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
30881   ARM_EXT ("fp", FPU_ARCH_VFP_V5_SP_D16, ALL_FP),
30882   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
30883   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30884 };
30885
30886 static const struct arm_ext_table armv8_1m_main_ext_table[] =
30887 {
30888   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30889                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
30890   ARM_EXT ("fp",
30891            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30892                         FPU_VFP_V5_SP_D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA),
30893            ALL_FP),
30894   ARM_ADD ("fp.dp",
30895            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30896                         FPU_VFP_V5D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
30897   ARM_EXT ("mve", ARM_FEATURE_COPROC (FPU_MVE),
30898            ARM_FEATURE_COPROC (FPU_MVE | FPU_MVE_FP)),
30899   ARM_ADD ("mve.fp",
30900            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30901                         FPU_MVE | FPU_MVE_FP | FPU_VFP_V5_SP_D16 |
30902                         FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
30903   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30904 };
30905
30906 static const struct arm_ext_table armv8r_ext_table[] =
30907 {
30908   ARM_ADD ("crc", ARCH_CRC_ARMV8),
30909   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
30910   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30911            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30912   ARM_REMOVE ("fp", ALL_FP),
30913   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V5_SP_D16),
30914   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30915 };
30916
30917 /* This list should, at a minimum, contain all the architecture names
30918    recognized by GCC.  */
30919 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF, NULL }
30920 #define ARM_ARCH_OPT2(N, V, DF, ext) \
30921   { N, sizeof (N) - 1, V, DF, ext##_ext_table }
30922
30923 static const struct arm_arch_option_table arm_archs[] =
30924 {
30925   ARM_ARCH_OPT ("all",            ARM_ANY,              FPU_ARCH_FPA),
30926   ARM_ARCH_OPT ("armv1",          ARM_ARCH_V1,          FPU_ARCH_FPA),
30927   ARM_ARCH_OPT ("armv2",          ARM_ARCH_V2,          FPU_ARCH_FPA),
30928   ARM_ARCH_OPT ("armv2a",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
30929   ARM_ARCH_OPT ("armv2s",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
30930   ARM_ARCH_OPT ("armv3",          ARM_ARCH_V3,          FPU_ARCH_FPA),
30931   ARM_ARCH_OPT ("armv3m",         ARM_ARCH_V3M,         FPU_ARCH_FPA),
30932   ARM_ARCH_OPT ("armv4",          ARM_ARCH_V4,          FPU_ARCH_FPA),
30933   ARM_ARCH_OPT ("armv4xm",        ARM_ARCH_V4xM,        FPU_ARCH_FPA),
30934   ARM_ARCH_OPT ("armv4t",         ARM_ARCH_V4T,         FPU_ARCH_FPA),
30935   ARM_ARCH_OPT ("armv4txm",       ARM_ARCH_V4TxM,       FPU_ARCH_FPA),
30936   ARM_ARCH_OPT ("armv5",          ARM_ARCH_V5,          FPU_ARCH_VFP),
30937   ARM_ARCH_OPT ("armv5t",         ARM_ARCH_V5T,         FPU_ARCH_VFP),
30938   ARM_ARCH_OPT ("armv5txm",       ARM_ARCH_V5TxM,       FPU_ARCH_VFP),
30939   ARM_ARCH_OPT2 ("armv5te",       ARM_ARCH_V5TE,        FPU_ARCH_VFP,   armv5te),
30940   ARM_ARCH_OPT2 ("armv5texp",     ARM_ARCH_V5TExP,      FPU_ARCH_VFP, armv5te),
30941   ARM_ARCH_OPT2 ("armv5tej",      ARM_ARCH_V5TEJ,       FPU_ARCH_VFP,   armv5te),
30942   ARM_ARCH_OPT2 ("armv6",         ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
30943   ARM_ARCH_OPT2 ("armv6j",        ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
30944   ARM_ARCH_OPT2 ("armv6k",        ARM_ARCH_V6K,         FPU_ARCH_VFP,   armv5te),
30945   ARM_ARCH_OPT2 ("armv6z",        ARM_ARCH_V6Z,         FPU_ARCH_VFP,   armv5te),
30946   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
30947      kept to preserve existing behaviour.  */
30948   ARM_ARCH_OPT2 ("armv6kz",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
30949   ARM_ARCH_OPT2 ("armv6zk",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
30950   ARM_ARCH_OPT2 ("armv6t2",       ARM_ARCH_V6T2,        FPU_ARCH_VFP,   armv5te),
30951   ARM_ARCH_OPT2 ("armv6kt2",      ARM_ARCH_V6KT2,       FPU_ARCH_VFP,   armv5te),
30952   ARM_ARCH_OPT2 ("armv6zt2",      ARM_ARCH_V6ZT2,       FPU_ARCH_VFP,   armv5te),
30953   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
30954      kept to preserve existing behaviour.  */
30955   ARM_ARCH_OPT2 ("armv6kzt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
30956   ARM_ARCH_OPT2 ("armv6zkt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
30957   ARM_ARCH_OPT ("armv6-m",        ARM_ARCH_V6M,         FPU_ARCH_VFP),
30958   ARM_ARCH_OPT ("armv6s-m",       ARM_ARCH_V6SM,        FPU_ARCH_VFP),
30959   ARM_ARCH_OPT2 ("armv7",         ARM_ARCH_V7,          FPU_ARCH_VFP, armv7),
30960   /* The official spelling of the ARMv7 profile variants is the dashed form.
30961      Accept the non-dashed form for compatibility with old toolchains.  */
30962   ARM_ARCH_OPT2 ("armv7a",        ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
30963   ARM_ARCH_OPT2 ("armv7ve",       ARM_ARCH_V7VE,        FPU_ARCH_VFP, armv7ve),
30964   ARM_ARCH_OPT2 ("armv7r",        ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
30965   ARM_ARCH_OPT ("armv7m",         ARM_ARCH_V7M,         FPU_ARCH_VFP),
30966   ARM_ARCH_OPT2 ("armv7-a",       ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
30967   ARM_ARCH_OPT2 ("armv7-r",       ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
30968   ARM_ARCH_OPT ("armv7-m",        ARM_ARCH_V7M,         FPU_ARCH_VFP),
30969   ARM_ARCH_OPT2 ("armv7e-m",      ARM_ARCH_V7EM,        FPU_ARCH_VFP, armv7em),
30970   ARM_ARCH_OPT ("armv8-m.base",   ARM_ARCH_V8M_BASE,    FPU_ARCH_VFP),
30971   ARM_ARCH_OPT2 ("armv8-m.main",  ARM_ARCH_V8M_MAIN,    FPU_ARCH_VFP,
30972                  armv8m_main),
30973   ARM_ARCH_OPT2 ("armv8.1-m.main", ARM_ARCH_V8_1M_MAIN, FPU_ARCH_VFP,
30974                  armv8_1m_main),
30975   ARM_ARCH_OPT2 ("armv8-a",       ARM_ARCH_V8A,         FPU_ARCH_VFP, armv8a),
30976   ARM_ARCH_OPT2 ("armv8.1-a",     ARM_ARCH_V8_1A,       FPU_ARCH_VFP, armv81a),
30977   ARM_ARCH_OPT2 ("armv8.2-a",     ARM_ARCH_V8_2A,       FPU_ARCH_VFP, armv82a),
30978   ARM_ARCH_OPT2 ("armv8.3-a",     ARM_ARCH_V8_3A,       FPU_ARCH_VFP, armv82a),
30979   ARM_ARCH_OPT2 ("armv8-r",       ARM_ARCH_V8R,         FPU_ARCH_VFP, armv8r),
30980   ARM_ARCH_OPT2 ("armv8.4-a",     ARM_ARCH_V8_4A,       FPU_ARCH_VFP, armv84a),
30981   ARM_ARCH_OPT2 ("armv8.5-a",     ARM_ARCH_V8_5A,       FPU_ARCH_VFP, armv85a),
30982   ARM_ARCH_OPT ("xscale",         ARM_ARCH_XSCALE,      FPU_ARCH_VFP),
30983   ARM_ARCH_OPT ("iwmmxt",         ARM_ARCH_IWMMXT,      FPU_ARCH_VFP),
30984   ARM_ARCH_OPT ("iwmmxt2",        ARM_ARCH_IWMMXT2,     FPU_ARCH_VFP),
30985   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
30986 };
30987 #undef ARM_ARCH_OPT
30988
30989 /* ISA extensions in the co-processor and main instruction set space.  */
30990
30991 struct arm_option_extension_value_table
30992 {
30993   const char *           name;
30994   size_t                 name_len;
30995   const arm_feature_set  merge_value;
30996   const arm_feature_set  clear_value;
30997   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
30998      indicates that an extension is available for all architectures while
30999      ARM_ANY marks an empty entry.  */
31000   const arm_feature_set  allowed_archs[2];
31001 };
31002
31003 /* The following table must be in alphabetical order with a NULL last entry.  */
31004
31005 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
31006 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
31007
31008 /* DEPRECATED: Refrain from using this table to add any new extensions, instead
31009    use the context sensitive approach using arm_ext_table's.  */
31010 static const struct arm_option_extension_value_table arm_extensions[] =
31011 {
31012   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
31013                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31014   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
31015                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
31016                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31017   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
31018                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
31019                           ARM_ARCH_V8_2A),
31020   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
31021                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
31022                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
31023   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
31024                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31025   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
31026                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
31027                         ARM_ARCH_V8_2A),
31028   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
31029                                                   | ARM_EXT2_FP16_FML),
31030                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
31031                                                   | ARM_EXT2_FP16_FML),
31032                            ARM_ARCH_V8_2A),
31033   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
31034                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
31035                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
31036                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
31037   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
31038      Thumb divide instruction.  Due to this having the same name as the
31039      previous entry, this will be ignored when doing command-line parsing and
31040      only considered by build attribute selection code.  */
31041   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
31042                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
31043                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
31044   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
31045                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
31046   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
31047                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
31048   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
31049                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
31050   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
31051                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
31052                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
31053                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
31054   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
31055                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
31056                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
31057   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
31058                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
31059                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31060   ARM_EXT_OPT ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
31061                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
31062                         ARM_ARCH_V8A),
31063   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
31064                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
31065                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31066   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
31067                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
31068                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31069   ARM_EXT_OPT ("sb",    ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
31070                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
31071                         ARM_ARCH_V8A),
31072   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
31073                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
31074                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
31075                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
31076   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
31077                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
31078                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31079   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
31080                                      | ARM_EXT_DIV),
31081                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
31082                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
31083   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
31084                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
31085   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
31086 };
31087 #undef ARM_EXT_OPT
31088
31089 /* ISA floating-point and Advanced SIMD extensions.  */
31090 struct arm_option_fpu_value_table
31091 {
31092   const char *           name;
31093   const arm_feature_set  value;
31094 };
31095
31096 /* This list should, at a minimum, contain all the fpu names
31097    recognized by GCC.  */
31098 static const struct arm_option_fpu_value_table arm_fpus[] =
31099 {
31100   {"softfpa",           FPU_NONE},
31101   {"fpe",               FPU_ARCH_FPE},
31102   {"fpe2",              FPU_ARCH_FPE},
31103   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
31104   {"fpa",               FPU_ARCH_FPA},
31105   {"fpa10",             FPU_ARCH_FPA},
31106   {"fpa11",             FPU_ARCH_FPA},
31107   {"arm7500fe",         FPU_ARCH_FPA},
31108   {"softvfp",           FPU_ARCH_VFP},
31109   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
31110   {"vfp",               FPU_ARCH_VFP_V2},
31111   {"vfp9",              FPU_ARCH_VFP_V2},
31112   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
31113   {"vfp10",             FPU_ARCH_VFP_V2},
31114   {"vfp10-r0",          FPU_ARCH_VFP_V1},
31115   {"vfpxd",             FPU_ARCH_VFP_V1xD},
31116   {"vfpv2",             FPU_ARCH_VFP_V2},
31117   {"vfpv3",             FPU_ARCH_VFP_V3},
31118   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
31119   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
31120   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
31121   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
31122   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
31123   {"arm1020t",          FPU_ARCH_VFP_V1},
31124   {"arm1020e",          FPU_ARCH_VFP_V2},
31125   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
31126   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
31127   {"maverick",          FPU_ARCH_MAVERICK},
31128   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
31129   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
31130   {"neon-fp16",         FPU_ARCH_NEON_FP16},
31131   {"vfpv4",             FPU_ARCH_VFP_V4},
31132   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
31133   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
31134   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
31135   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
31136   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
31137   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
31138   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
31139   {"crypto-neon-fp-armv8",
31140                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
31141   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
31142   {"crypto-neon-fp-armv8.1",
31143                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
31144   {NULL,                ARM_ARCH_NONE}
31145 };
31146
31147 struct arm_option_value_table
31148 {
31149   const char *name;
31150   long value;
31151 };
31152
31153 static const struct arm_option_value_table arm_float_abis[] =
31154 {
31155   {"hard",      ARM_FLOAT_ABI_HARD},
31156   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
31157   {"soft",      ARM_FLOAT_ABI_SOFT},
31158   {NULL,        0}
31159 };
31160
31161 #ifdef OBJ_ELF
31162 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
31163 static const struct arm_option_value_table arm_eabis[] =
31164 {
31165   {"gnu",       EF_ARM_EABI_UNKNOWN},
31166   {"4",         EF_ARM_EABI_VER4},
31167   {"5",         EF_ARM_EABI_VER5},
31168   {NULL,        0}
31169 };
31170 #endif
31171
31172 struct arm_long_option_table
31173 {
31174   const char * option;                  /* Substring to match.  */
31175   const char * help;                    /* Help information.  */
31176   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
31177   const char * deprecated;              /* If non-null, print this message.  */
31178 };
31179
31180 static bfd_boolean
31181 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
31182                      arm_feature_set *ext_set,
31183                      const struct arm_ext_table *ext_table)
31184 {
31185   /* We insist on extensions being specified in alphabetical order, and with
31186      extensions being added before being removed.  We achieve this by having
31187      the global ARM_EXTENSIONS table in alphabetical order, and using the
31188      ADDING_VALUE variable to indicate whether we are adding an extension (1)
31189      or removing it (0) and only allowing it to change in the order
31190      -1 -> 1 -> 0.  */
31191   const struct arm_option_extension_value_table * opt = NULL;
31192   const arm_feature_set arm_any = ARM_ANY;
31193   int adding_value = -1;
31194
31195   while (str != NULL && *str != 0)
31196     {
31197       const char *ext;
31198       size_t len;
31199
31200       if (*str != '+')
31201         {
31202           as_bad (_("invalid architectural extension"));
31203           return FALSE;
31204         }
31205
31206       str++;
31207       ext = strchr (str, '+');
31208
31209       if (ext != NULL)
31210         len = ext - str;
31211       else
31212         len = strlen (str);
31213
31214       if (len >= 2 && strncmp (str, "no", 2) == 0)
31215         {
31216           if (adding_value != 0)
31217             {
31218               adding_value = 0;
31219               opt = arm_extensions;
31220             }
31221
31222           len -= 2;
31223           str += 2;
31224         }
31225       else if (len > 0)
31226         {
31227           if (adding_value == -1)
31228             {
31229               adding_value = 1;
31230               opt = arm_extensions;
31231             }
31232           else if (adding_value != 1)
31233             {
31234               as_bad (_("must specify extensions to add before specifying "
31235                         "those to remove"));
31236               return FALSE;
31237             }
31238         }
31239
31240       if (len == 0)
31241         {
31242           as_bad (_("missing architectural extension"));
31243           return FALSE;
31244         }
31245
31246       gas_assert (adding_value != -1);
31247       gas_assert (opt != NULL);
31248
31249       if (ext_table != NULL)
31250         {
31251           const struct arm_ext_table * ext_opt = ext_table;
31252           bfd_boolean found = FALSE;
31253           for (; ext_opt->name != NULL; ext_opt++)
31254             if (ext_opt->name_len == len
31255                 && strncmp (ext_opt->name, str, len) == 0)
31256               {
31257                 if (adding_value)
31258                   {
31259                     if (ARM_FEATURE_ZERO (ext_opt->merge))
31260                         /* TODO: Option not supported.  When we remove the
31261                            legacy table this case should error out.  */
31262                         continue;
31263
31264                     ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, ext_opt->merge);
31265                   }
31266                 else
31267                   {
31268                     if (ARM_FEATURE_ZERO (ext_opt->clear))
31269                         /* TODO: Option not supported.  When we remove the
31270                            legacy table this case should error out.  */
31271                         continue;
31272                     ARM_CLEAR_FEATURE (*ext_set, *ext_set, ext_opt->clear);
31273                   }
31274                 found = TRUE;
31275                 break;
31276               }
31277           if (found)
31278             {
31279               str = ext;
31280               continue;
31281             }
31282         }
31283
31284       /* Scan over the options table trying to find an exact match. */
31285       for (; opt->name != NULL; opt++)
31286         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31287           {
31288             int i, nb_allowed_archs =
31289               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
31290             /* Check we can apply the extension to this architecture.  */
31291             for (i = 0; i < nb_allowed_archs; i++)
31292               {
31293                 /* Empty entry.  */
31294                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
31295                   continue;
31296                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
31297                   break;
31298               }
31299             if (i == nb_allowed_archs)
31300               {
31301                 as_bad (_("extension does not apply to the base architecture"));
31302                 return FALSE;
31303               }
31304
31305             /* Add or remove the extension.  */
31306             if (adding_value)
31307               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
31308             else
31309               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
31310
31311             /* Allowing Thumb division instructions for ARMv7 in autodetection
31312                rely on this break so that duplicate extensions (extensions
31313                with the same name as a previous extension in the list) are not
31314                considered for command-line parsing.  */
31315             break;
31316           }
31317
31318       if (opt->name == NULL)
31319         {
31320           /* Did we fail to find an extension because it wasn't specified in
31321              alphabetical order, or because it does not exist?  */
31322
31323           for (opt = arm_extensions; opt->name != NULL; opt++)
31324             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31325               break;
31326
31327           if (opt->name == NULL)
31328             as_bad (_("unknown architectural extension `%s'"), str);
31329           else
31330             as_bad (_("architectural extensions must be specified in "
31331                       "alphabetical order"));
31332
31333           return FALSE;
31334         }
31335       else
31336         {
31337           /* We should skip the extension we've just matched the next time
31338              round.  */
31339           opt++;
31340         }
31341
31342       str = ext;
31343     };
31344
31345   return TRUE;
31346 }
31347
31348 static bfd_boolean
31349 arm_parse_fp16_opt (const char *str)
31350 {
31351   if (strcasecmp (str, "ieee") == 0)
31352     fp16_format = ARM_FP16_FORMAT_IEEE;
31353   else if (strcasecmp (str, "alternative") == 0)
31354     fp16_format = ARM_FP16_FORMAT_ALTERNATIVE;
31355   else
31356     {
31357       as_bad (_("unrecognised float16 format \"%s\""), str);
31358       return FALSE;
31359     }
31360
31361   return TRUE;
31362 }
31363
31364 static bfd_boolean
31365 arm_parse_cpu (const char *str)
31366 {
31367   const struct arm_cpu_option_table *opt;
31368   const char *ext = strchr (str, '+');
31369   size_t len;
31370
31371   if (ext != NULL)
31372     len = ext - str;
31373   else
31374     len = strlen (str);
31375
31376   if (len == 0)
31377     {
31378       as_bad (_("missing cpu name `%s'"), str);
31379       return FALSE;
31380     }
31381
31382   for (opt = arm_cpus; opt->name != NULL; opt++)
31383     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31384       {
31385         mcpu_cpu_opt = &opt->value;
31386         if (mcpu_ext_opt == NULL)
31387           mcpu_ext_opt = XNEW (arm_feature_set);
31388         *mcpu_ext_opt = opt->ext;
31389         mcpu_fpu_opt = &opt->default_fpu;
31390         if (opt->canonical_name)
31391           {
31392             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
31393             strcpy (selected_cpu_name, opt->canonical_name);
31394           }
31395         else
31396           {
31397             size_t i;
31398
31399             if (len >= sizeof selected_cpu_name)
31400               len = (sizeof selected_cpu_name) - 1;
31401
31402             for (i = 0; i < len; i++)
31403               selected_cpu_name[i] = TOUPPER (opt->name[i]);
31404             selected_cpu_name[i] = 0;
31405           }
31406
31407         if (ext != NULL)
31408           return arm_parse_extension (ext, mcpu_cpu_opt, mcpu_ext_opt, NULL);
31409
31410         return TRUE;
31411       }
31412
31413   as_bad (_("unknown cpu `%s'"), str);
31414   return FALSE;
31415 }
31416
31417 static bfd_boolean
31418 arm_parse_arch (const char *str)
31419 {
31420   const struct arm_arch_option_table *opt;
31421   const char *ext = strchr (str, '+');
31422   size_t len;
31423
31424   if (ext != NULL)
31425     len = ext - str;
31426   else
31427     len = strlen (str);
31428
31429   if (len == 0)
31430     {
31431       as_bad (_("missing architecture name `%s'"), str);
31432       return FALSE;
31433     }
31434
31435   for (opt = arm_archs; opt->name != NULL; opt++)
31436     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31437       {
31438         march_cpu_opt = &opt->value;
31439         if (march_ext_opt == NULL)
31440           march_ext_opt = XNEW (arm_feature_set);
31441         *march_ext_opt = arm_arch_none;
31442         march_fpu_opt = &opt->default_fpu;
31443         strcpy (selected_cpu_name, opt->name);
31444
31445         if (ext != NULL)
31446           return arm_parse_extension (ext, march_cpu_opt, march_ext_opt,
31447                                       opt->ext_table);
31448
31449         return TRUE;
31450       }
31451
31452   as_bad (_("unknown architecture `%s'\n"), str);
31453   return FALSE;
31454 }
31455
31456 static bfd_boolean
31457 arm_parse_fpu (const char * str)
31458 {
31459   const struct arm_option_fpu_value_table * opt;
31460
31461   for (opt = arm_fpus; opt->name != NULL; opt++)
31462     if (streq (opt->name, str))
31463       {
31464         mfpu_opt = &opt->value;
31465         return TRUE;
31466       }
31467
31468   as_bad (_("unknown floating point format `%s'\n"), str);
31469   return FALSE;
31470 }
31471
31472 static bfd_boolean
31473 arm_parse_float_abi (const char * str)
31474 {
31475   const struct arm_option_value_table * opt;
31476
31477   for (opt = arm_float_abis; opt->name != NULL; opt++)
31478     if (streq (opt->name, str))
31479       {
31480         mfloat_abi_opt = opt->value;
31481         return TRUE;
31482       }
31483
31484   as_bad (_("unknown floating point abi `%s'\n"), str);
31485   return FALSE;
31486 }
31487
31488 #ifdef OBJ_ELF
31489 static bfd_boolean
31490 arm_parse_eabi (const char * str)
31491 {
31492   const struct arm_option_value_table *opt;
31493
31494   for (opt = arm_eabis; opt->name != NULL; opt++)
31495     if (streq (opt->name, str))
31496       {
31497         meabi_flags = opt->value;
31498         return TRUE;
31499       }
31500   as_bad (_("unknown EABI `%s'\n"), str);
31501   return FALSE;
31502 }
31503 #endif
31504
31505 static bfd_boolean
31506 arm_parse_it_mode (const char * str)
31507 {
31508   bfd_boolean ret = TRUE;
31509
31510   if (streq ("arm", str))
31511     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
31512   else if (streq ("thumb", str))
31513     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
31514   else if (streq ("always", str))
31515     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
31516   else if (streq ("never", str))
31517     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
31518   else
31519     {
31520       as_bad (_("unknown implicit IT mode `%s', should be "\
31521                 "arm, thumb, always, or never."), str);
31522       ret = FALSE;
31523     }
31524
31525   return ret;
31526 }
31527
31528 static bfd_boolean
31529 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
31530 {
31531   codecomposer_syntax = TRUE;
31532   arm_comment_chars[0] = ';';
31533   arm_line_separator_chars[0] = 0;
31534   return TRUE;
31535 }
31536
31537 struct arm_long_option_table arm_long_opts[] =
31538 {
31539   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
31540    arm_parse_cpu, NULL},
31541   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
31542    arm_parse_arch, NULL},
31543   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
31544    arm_parse_fpu, NULL},
31545   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
31546    arm_parse_float_abi, NULL},
31547 #ifdef OBJ_ELF
31548   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
31549    arm_parse_eabi, NULL},
31550 #endif
31551   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
31552    arm_parse_it_mode, NULL},
31553   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
31554    arm_ccs_mode, NULL},
31555   {"mfp16-format=",
31556    N_("[ieee|alternative]\n\
31557                           set the encoding for half precision floating point "
31558                           "numbers to IEEE\n\
31559                           or Arm alternative format."),
31560    arm_parse_fp16_opt, NULL },
31561   {NULL, NULL, 0, NULL}
31562 };
31563
31564 int
31565 md_parse_option (int c, const char * arg)
31566 {
31567   struct arm_option_table *opt;
31568   const struct arm_legacy_option_table *fopt;
31569   struct arm_long_option_table *lopt;
31570
31571   switch (c)
31572     {
31573 #ifdef OPTION_EB
31574     case OPTION_EB:
31575       target_big_endian = 1;
31576       break;
31577 #endif
31578
31579 #ifdef OPTION_EL
31580     case OPTION_EL:
31581       target_big_endian = 0;
31582       break;
31583 #endif
31584
31585     case OPTION_FIX_V4BX:
31586       fix_v4bx = TRUE;
31587       break;
31588
31589 #ifdef OBJ_ELF
31590     case OPTION_FDPIC:
31591       arm_fdpic = TRUE;
31592       break;
31593 #endif /* OBJ_ELF */
31594
31595     case 'a':
31596       /* Listing option.  Just ignore these, we don't support additional
31597          ones.  */
31598       return 0;
31599
31600     default:
31601       for (opt = arm_opts; opt->option != NULL; opt++)
31602         {
31603           if (c == opt->option[0]
31604               && ((arg == NULL && opt->option[1] == 0)
31605                   || streq (arg, opt->option + 1)))
31606             {
31607               /* If the option is deprecated, tell the user.  */
31608               if (warn_on_deprecated && opt->deprecated != NULL)
31609                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
31610                            arg ? arg : "", _(opt->deprecated));
31611
31612               if (opt->var != NULL)
31613                 *opt->var = opt->value;
31614
31615               return 1;
31616             }
31617         }
31618
31619       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
31620         {
31621           if (c == fopt->option[0]
31622               && ((arg == NULL && fopt->option[1] == 0)
31623                   || streq (arg, fopt->option + 1)))
31624             {
31625               /* If the option is deprecated, tell the user.  */
31626               if (warn_on_deprecated && fopt->deprecated != NULL)
31627                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
31628                            arg ? arg : "", _(fopt->deprecated));
31629
31630               if (fopt->var != NULL)
31631                 *fopt->var = &fopt->value;
31632
31633               return 1;
31634             }
31635         }
31636
31637       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
31638         {
31639           /* These options are expected to have an argument.  */
31640           if (c == lopt->option[0]
31641               && arg != NULL
31642               && strncmp (arg, lopt->option + 1,
31643                           strlen (lopt->option + 1)) == 0)
31644             {
31645               /* If the option is deprecated, tell the user.  */
31646               if (warn_on_deprecated && lopt->deprecated != NULL)
31647                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
31648                            _(lopt->deprecated));
31649
31650               /* Call the sup-option parser.  */
31651               return lopt->func (arg + strlen (lopt->option) - 1);
31652             }
31653         }
31654
31655       return 0;
31656     }
31657
31658   return 1;
31659 }
31660
31661 void
31662 md_show_usage (FILE * fp)
31663 {
31664   struct arm_option_table *opt;
31665   struct arm_long_option_table *lopt;
31666
31667   fprintf (fp, _(" ARM-specific assembler options:\n"));
31668
31669   for (opt = arm_opts; opt->option != NULL; opt++)
31670     if (opt->help != NULL)
31671       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
31672
31673   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
31674     if (lopt->help != NULL)
31675       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
31676
31677 #ifdef OPTION_EB
31678   fprintf (fp, _("\
31679   -EB                     assemble code for a big-endian cpu\n"));
31680 #endif
31681
31682 #ifdef OPTION_EL
31683   fprintf (fp, _("\
31684   -EL                     assemble code for a little-endian cpu\n"));
31685 #endif
31686
31687   fprintf (fp, _("\
31688   --fix-v4bx              Allow BX in ARMv4 code\n"));
31689
31690 #ifdef OBJ_ELF
31691   fprintf (fp, _("\
31692   --fdpic                 generate an FDPIC object file\n"));
31693 #endif /* OBJ_ELF */
31694 }
31695
31696 #ifdef OBJ_ELF
31697
31698 typedef struct
31699 {
31700   int val;
31701   arm_feature_set flags;
31702 } cpu_arch_ver_table;
31703
31704 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
31705    chronologically for architectures, with an exception for ARMv6-M and
31706    ARMv6S-M due to legacy reasons.  No new architecture should have a
31707    special case.  This allows for build attribute selection results to be
31708    stable when new architectures are added.  */
31709 static const cpu_arch_ver_table cpu_arch_ver[] =
31710 {
31711     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V1},
31712     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2},
31713     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2S},
31714     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3},
31715     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3M},
31716     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4xM},
31717     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4},
31718     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4TxM},
31719     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4T},
31720     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5xM},
31721     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5},
31722     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5TxM},
31723     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5T},
31724     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TExP},
31725     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TE},
31726     {TAG_CPU_ARCH_V5TEJ,      ARM_ARCH_V5TEJ},
31727     {TAG_CPU_ARCH_V6,         ARM_ARCH_V6},
31728     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6Z},
31729     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6KZ},
31730     {TAG_CPU_ARCH_V6K,        ARM_ARCH_V6K},
31731     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6T2},
31732     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KT2},
31733     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6ZT2},
31734     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KZT2},
31735
31736     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
31737        always selected build attributes to match those of ARMv6-M
31738        (resp. ARMv6S-M).  However, due to these architectures being a strict
31739        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
31740        would be selected when fully respecting chronology of architectures.
31741        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
31742        move them before ARMv7 architectures.  */
31743     {TAG_CPU_ARCH_V6_M,       ARM_ARCH_V6M},
31744     {TAG_CPU_ARCH_V6S_M,      ARM_ARCH_V6SM},
31745
31746     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7},
31747     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7A},
31748     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7R},
31749     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7M},
31750     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7VE},
31751     {TAG_CPU_ARCH_V7E_M,      ARM_ARCH_V7EM},
31752     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8A},
31753     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_1A},
31754     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_2A},
31755     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_3A},
31756     {TAG_CPU_ARCH_V8M_BASE,   ARM_ARCH_V8M_BASE},
31757     {TAG_CPU_ARCH_V8M_MAIN,   ARM_ARCH_V8M_MAIN},
31758     {TAG_CPU_ARCH_V8R,        ARM_ARCH_V8R},
31759     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_4A},
31760     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_5A},
31761     {TAG_CPU_ARCH_V8_1M_MAIN, ARM_ARCH_V8_1M_MAIN},
31762     {-1,                      ARM_ARCH_NONE}
31763 };
31764
31765 /* Set an attribute if it has not already been set by the user.  */
31766
31767 static void
31768 aeabi_set_attribute_int (int tag, int value)
31769 {
31770   if (tag < 1
31771       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
31772       || !attributes_set_explicitly[tag])
31773     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
31774 }
31775
31776 static void
31777 aeabi_set_attribute_string (int tag, const char *value)
31778 {
31779   if (tag < 1
31780       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
31781       || !attributes_set_explicitly[tag])
31782     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
31783 }
31784
31785 /* Return whether features in the *NEEDED feature set are available via
31786    extensions for the architecture whose feature set is *ARCH_FSET.  */
31787
31788 static bfd_boolean
31789 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
31790                             const arm_feature_set *needed)
31791 {
31792   int i, nb_allowed_archs;
31793   arm_feature_set ext_fset;
31794   const struct arm_option_extension_value_table *opt;
31795
31796   ext_fset = arm_arch_none;
31797   for (opt = arm_extensions; opt->name != NULL; opt++)
31798     {
31799       /* Extension does not provide any feature we need.  */
31800       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
31801         continue;
31802
31803       nb_allowed_archs =
31804         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
31805       for (i = 0; i < nb_allowed_archs; i++)
31806         {
31807           /* Empty entry.  */
31808           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
31809             break;
31810
31811           /* Extension is available, add it.  */
31812           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
31813             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
31814         }
31815     }
31816
31817   /* Can we enable all features in *needed?  */
31818   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
31819 }
31820
31821 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
31822    a given architecture feature set *ARCH_EXT_FSET including extension feature
31823    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
31824    - if true, check for an exact match of the architecture modulo extensions;
31825    - otherwise, select build attribute value of the first superset
31826      architecture released so that results remains stable when new architectures
31827      are added.
31828    For -march/-mcpu=all the build attribute value of the most featureful
31829    architecture is returned.  Tag_CPU_arch_profile result is returned in
31830    PROFILE.  */
31831
31832 static int
31833 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
31834                               const arm_feature_set *ext_fset,
31835                               char *profile, int exact_match)
31836 {
31837   arm_feature_set arch_fset;
31838   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
31839
31840   /* Select most featureful architecture with all its extensions if building
31841      for -march=all as the feature sets used to set build attributes.  */
31842   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
31843     {
31844       /* Force revisiting of decision for each new architecture.  */
31845       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8_1M_MAIN);
31846       *profile = 'A';
31847       return TAG_CPU_ARCH_V8;
31848     }
31849
31850   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
31851
31852   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
31853     {
31854       arm_feature_set known_arch_fset;
31855
31856       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
31857       if (exact_match)
31858         {
31859           /* Base architecture match user-specified architecture and
31860              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
31861           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
31862             {
31863               p_ver_ret = p_ver;
31864               goto found;
31865             }
31866           /* Base architecture match user-specified architecture only
31867              (eg. ARMv6-M in the same case as above).  Record it in case we
31868              find a match with above condition.  */
31869           else if (p_ver_ret == NULL
31870                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
31871             p_ver_ret = p_ver;
31872         }
31873       else
31874         {
31875
31876           /* Architecture has all features wanted.  */
31877           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
31878             {
31879               arm_feature_set added_fset;
31880
31881               /* Compute features added by this architecture over the one
31882                  recorded in p_ver_ret.  */
31883               if (p_ver_ret != NULL)
31884                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
31885                                    p_ver_ret->flags);
31886               /* First architecture that match incl. with extensions, or the
31887                  only difference in features over the recorded match is
31888                  features that were optional and are now mandatory.  */
31889               if (p_ver_ret == NULL
31890                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
31891                 {
31892                   p_ver_ret = p_ver;
31893                   goto found;
31894                 }
31895             }
31896           else if (p_ver_ret == NULL)
31897             {
31898               arm_feature_set needed_ext_fset;
31899
31900               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
31901
31902               /* Architecture has all features needed when using some
31903                  extensions.  Record it and continue searching in case there
31904                  exist an architecture providing all needed features without
31905                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
31906                  OS extension).  */
31907               if (have_ext_for_needed_feat_p (&known_arch_fset,
31908                                               &needed_ext_fset))
31909                 p_ver_ret = p_ver;
31910             }
31911         }
31912     }
31913
31914   if (p_ver_ret == NULL)
31915     return -1;
31916
31917 found:
31918   /* Tag_CPU_arch_profile.  */
31919   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
31920       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
31921       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
31922           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
31923     *profile = 'A';
31924   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
31925     *profile = 'R';
31926   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
31927     *profile = 'M';
31928   else
31929     *profile = '\0';
31930   return p_ver_ret->val;
31931 }
31932
31933 /* Set the public EABI object attributes.  */
31934
31935 static void
31936 aeabi_set_public_attributes (void)
31937 {
31938   char profile = '\0';
31939   int arch = -1;
31940   int virt_sec = 0;
31941   int fp16_optional = 0;
31942   int skip_exact_match = 0;
31943   arm_feature_set flags, flags_arch, flags_ext;
31944
31945   /* Autodetection mode, choose the architecture based the instructions
31946      actually used.  */
31947   if (no_cpu_selected ())
31948     {
31949       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
31950
31951       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
31952         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
31953
31954       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
31955         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
31956
31957       /* Code run during relaxation relies on selected_cpu being set.  */
31958       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
31959       flags_ext = arm_arch_none;
31960       ARM_CLEAR_FEATURE (selected_arch, flags_arch, flags_ext);
31961       selected_ext = flags_ext;
31962       selected_cpu = flags;
31963     }
31964   /* Otherwise, choose the architecture based on the capabilities of the
31965      requested cpu.  */
31966   else
31967     {
31968       ARM_MERGE_FEATURE_SETS (flags_arch, selected_arch, selected_ext);
31969       ARM_CLEAR_FEATURE (flags_arch, flags_arch, fpu_any);
31970       flags_ext = selected_ext;
31971       flags = selected_cpu;
31972     }
31973   ARM_MERGE_FEATURE_SETS (flags, flags, selected_fpu);
31974
31975   /* Allow the user to override the reported architecture.  */
31976   if (!ARM_FEATURE_ZERO (selected_object_arch))
31977     {
31978       ARM_CLEAR_FEATURE (flags_arch, selected_object_arch, fpu_any);
31979       flags_ext = arm_arch_none;
31980     }
31981   else
31982     skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
31983
31984   /* When this function is run again after relaxation has happened there is no
31985      way to determine whether an architecture or CPU was specified by the user:
31986      - selected_cpu is set above for relaxation to work;
31987      - march_cpu_opt is not set if only -mcpu or .cpu is used;
31988      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
31989      Therefore, if not in -march=all case we first try an exact match and fall
31990      back to autodetection.  */
31991   if (!skip_exact_match)
31992     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
31993   if (arch == -1)
31994     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
31995   if (arch == -1)
31996     as_bad (_("no architecture contains all the instructions used\n"));
31997
31998   /* Tag_CPU_name.  */
31999   if (selected_cpu_name[0])
32000     {
32001       char *q;
32002
32003       q = selected_cpu_name;
32004       if (strncmp (q, "armv", 4) == 0)
32005         {
32006           int i;
32007
32008           q += 4;
32009           for (i = 0; q[i]; i++)
32010             q[i] = TOUPPER (q[i]);
32011         }
32012       aeabi_set_attribute_string (Tag_CPU_name, q);
32013     }
32014
32015   /* Tag_CPU_arch.  */
32016   aeabi_set_attribute_int (Tag_CPU_arch, arch);
32017
32018   /* Tag_CPU_arch_profile.  */
32019   if (profile != '\0')
32020     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
32021
32022   /* Tag_DSP_extension.  */
32023   if (ARM_CPU_HAS_FEATURE (selected_ext, arm_ext_dsp))
32024     aeabi_set_attribute_int (Tag_DSP_extension, 1);
32025
32026   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
32027   /* Tag_ARM_ISA_use.  */
32028   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
32029       || ARM_FEATURE_ZERO (flags_arch))
32030     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
32031
32032   /* Tag_THUMB_ISA_use.  */
32033   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
32034       || ARM_FEATURE_ZERO (flags_arch))
32035     {
32036       int thumb_isa_use;
32037
32038       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
32039           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
32040         thumb_isa_use = 3;
32041       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
32042         thumb_isa_use = 2;
32043       else
32044         thumb_isa_use = 1;
32045       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
32046     }
32047
32048   /* Tag_VFP_arch.  */
32049   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
32050     aeabi_set_attribute_int (Tag_VFP_arch,
32051                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
32052                              ? 7 : 8);
32053   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
32054     aeabi_set_attribute_int (Tag_VFP_arch,
32055                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
32056                              ? 5 : 6);
32057   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
32058     {
32059       fp16_optional = 1;
32060       aeabi_set_attribute_int (Tag_VFP_arch, 3);
32061     }
32062   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
32063     {
32064       aeabi_set_attribute_int (Tag_VFP_arch, 4);
32065       fp16_optional = 1;
32066     }
32067   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
32068     aeabi_set_attribute_int (Tag_VFP_arch, 2);
32069   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
32070            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
32071     aeabi_set_attribute_int (Tag_VFP_arch, 1);
32072
32073   /* Tag_ABI_HardFP_use.  */
32074   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
32075       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
32076     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
32077
32078   /* Tag_WMMX_arch.  */
32079   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
32080     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
32081   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
32082     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
32083
32084   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
32085   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
32086     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
32087   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
32088     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
32089   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
32090     {
32091       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
32092         {
32093           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
32094         }
32095       else
32096         {
32097           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
32098           fp16_optional = 1;
32099         }
32100     }
32101
32102   if (ARM_CPU_HAS_FEATURE (flags, mve_fp_ext))
32103     aeabi_set_attribute_int (Tag_MVE_arch, 2);
32104   else if (ARM_CPU_HAS_FEATURE (flags, mve_ext))
32105     aeabi_set_attribute_int (Tag_MVE_arch, 1);
32106
32107   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
32108   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
32109     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
32110
32111   /* Tag_DIV_use.
32112
32113      We set Tag_DIV_use to two when integer divide instructions have been used
32114      in ARM state, or when Thumb integer divide instructions have been used,
32115      but we have no architecture profile set, nor have we any ARM instructions.
32116
32117      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
32118      by the base architecture.
32119
32120      For new architectures we will have to check these tests.  */
32121   gas_assert (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
32122   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
32123       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
32124     aeabi_set_attribute_int (Tag_DIV_use, 0);
32125   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
32126            || (profile == '\0'
32127                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
32128                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
32129     aeabi_set_attribute_int (Tag_DIV_use, 2);
32130
32131   /* Tag_MP_extension_use.  */
32132   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
32133     aeabi_set_attribute_int (Tag_MPextension_use, 1);
32134
32135   /* Tag Virtualization_use.  */
32136   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
32137     virt_sec |= 1;
32138   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
32139     virt_sec |= 2;
32140   if (virt_sec != 0)
32141     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
32142
32143   if (fp16_format != ARM_FP16_FORMAT_DEFAULT)
32144     aeabi_set_attribute_int (Tag_ABI_FP_16bit_format, fp16_format);
32145 }
32146
32147 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
32148    finished and free extension feature bits which will not be used anymore.  */
32149
32150 void
32151 arm_md_post_relax (void)
32152 {
32153   aeabi_set_public_attributes ();
32154   XDELETE (mcpu_ext_opt);
32155   mcpu_ext_opt = NULL;
32156   XDELETE (march_ext_opt);
32157   march_ext_opt = NULL;
32158 }
32159
32160 /* Add the default contents for the .ARM.attributes section.  */
32161
32162 void
32163 arm_md_end (void)
32164 {
32165   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
32166     return;
32167
32168   aeabi_set_public_attributes ();
32169 }
32170 #endif /* OBJ_ELF */
32171
32172 /* Parse a .cpu directive.  */
32173
32174 static void
32175 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
32176 {
32177   const struct arm_cpu_option_table *opt;
32178   char *name;
32179   char saved_char;
32180
32181   name = input_line_pointer;
32182   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32183     input_line_pointer++;
32184   saved_char = *input_line_pointer;
32185   *input_line_pointer = 0;
32186
32187   /* Skip the first "all" entry.  */
32188   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
32189     if (streq (opt->name, name))
32190       {
32191         selected_arch = opt->value;
32192         selected_ext = opt->ext;
32193         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
32194         if (opt->canonical_name)
32195           strcpy (selected_cpu_name, opt->canonical_name);
32196         else
32197           {
32198             int i;
32199             for (i = 0; opt->name[i]; i++)
32200               selected_cpu_name[i] = TOUPPER (opt->name[i]);
32201
32202             selected_cpu_name[i] = 0;
32203           }
32204         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32205
32206         *input_line_pointer = saved_char;
32207         demand_empty_rest_of_line ();
32208         return;
32209       }
32210   as_bad (_("unknown cpu `%s'"), name);
32211   *input_line_pointer = saved_char;
32212   ignore_rest_of_line ();
32213 }
32214
32215 /* Parse a .arch directive.  */
32216
32217 static void
32218 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
32219 {
32220   const struct arm_arch_option_table *opt;
32221   char saved_char;
32222   char *name;
32223
32224   name = input_line_pointer;
32225   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32226     input_line_pointer++;
32227   saved_char = *input_line_pointer;
32228   *input_line_pointer = 0;
32229
32230   /* Skip the first "all" entry.  */
32231   for (opt = arm_archs + 1; opt->name != NULL; opt++)
32232     if (streq (opt->name, name))
32233       {
32234         selected_arch = opt->value;
32235         selected_ext = arm_arch_none;
32236         selected_cpu = selected_arch;
32237         strcpy (selected_cpu_name, opt->name);
32238         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32239         *input_line_pointer = saved_char;
32240         demand_empty_rest_of_line ();
32241         return;
32242       }
32243
32244   as_bad (_("unknown architecture `%s'\n"), name);
32245   *input_line_pointer = saved_char;
32246   ignore_rest_of_line ();
32247 }
32248
32249 /* Parse a .object_arch directive.  */
32250
32251 static void
32252 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
32253 {
32254   const struct arm_arch_option_table *opt;
32255   char saved_char;
32256   char *name;
32257
32258   name = input_line_pointer;
32259   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32260     input_line_pointer++;
32261   saved_char = *input_line_pointer;
32262   *input_line_pointer = 0;
32263
32264   /* Skip the first "all" entry.  */
32265   for (opt = arm_archs + 1; opt->name != NULL; opt++)
32266     if (streq (opt->name, name))
32267       {
32268         selected_object_arch = opt->value;
32269         *input_line_pointer = saved_char;
32270         demand_empty_rest_of_line ();
32271         return;
32272       }
32273
32274   as_bad (_("unknown architecture `%s'\n"), name);
32275   *input_line_pointer = saved_char;
32276   ignore_rest_of_line ();
32277 }
32278
32279 /* Parse a .arch_extension directive.  */
32280
32281 static void
32282 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
32283 {
32284   const struct arm_option_extension_value_table *opt;
32285   char saved_char;
32286   char *name;
32287   int adding_value = 1;
32288
32289   name = input_line_pointer;
32290   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32291     input_line_pointer++;
32292   saved_char = *input_line_pointer;
32293   *input_line_pointer = 0;
32294
32295   if (strlen (name) >= 2
32296       && strncmp (name, "no", 2) == 0)
32297     {
32298       adding_value = 0;
32299       name += 2;
32300     }
32301
32302   for (opt = arm_extensions; opt->name != NULL; opt++)
32303     if (streq (opt->name, name))
32304       {
32305         int i, nb_allowed_archs =
32306           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
32307         for (i = 0; i < nb_allowed_archs; i++)
32308           {
32309             /* Empty entry.  */
32310             if (ARM_CPU_IS_ANY (opt->allowed_archs[i]))
32311               continue;
32312             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], selected_arch))
32313               break;
32314           }
32315
32316         if (i == nb_allowed_archs)
32317           {
32318             as_bad (_("architectural extension `%s' is not allowed for the "
32319                       "current base architecture"), name);
32320             break;
32321           }
32322
32323         if (adding_value)
32324           ARM_MERGE_FEATURE_SETS (selected_ext, selected_ext,
32325                                   opt->merge_value);
32326         else
32327           ARM_CLEAR_FEATURE (selected_ext, selected_ext, opt->clear_value);
32328
32329         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
32330         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32331         *input_line_pointer = saved_char;
32332         demand_empty_rest_of_line ();
32333         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
32334            on this return so that duplicate extensions (extensions with the
32335            same name as a previous extension in the list) are not considered
32336            for command-line parsing.  */
32337         return;
32338       }
32339
32340   if (opt->name == NULL)
32341     as_bad (_("unknown architecture extension `%s'\n"), name);
32342
32343   *input_line_pointer = saved_char;
32344   ignore_rest_of_line ();
32345 }
32346
32347 /* Parse a .fpu directive.  */
32348
32349 static void
32350 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
32351 {
32352   const struct arm_option_fpu_value_table *opt;
32353   char saved_char;
32354   char *name;
32355
32356   name = input_line_pointer;
32357   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32358     input_line_pointer++;
32359   saved_char = *input_line_pointer;
32360   *input_line_pointer = 0;
32361
32362   for (opt = arm_fpus; opt->name != NULL; opt++)
32363     if (streq (opt->name, name))
32364       {
32365         selected_fpu = opt->value;
32366 #ifndef CPU_DEFAULT
32367         if (no_cpu_selected ())
32368           ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
32369         else
32370 #endif
32371           ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32372         *input_line_pointer = saved_char;
32373         demand_empty_rest_of_line ();
32374         return;
32375       }
32376
32377   as_bad (_("unknown floating point format `%s'\n"), name);
32378   *input_line_pointer = saved_char;
32379   ignore_rest_of_line ();
32380 }
32381
32382 /* Copy symbol information.  */
32383
32384 void
32385 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
32386 {
32387   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
32388 }
32389
32390 #ifdef OBJ_ELF
32391 /* Given a symbolic attribute NAME, return the proper integer value.
32392    Returns -1 if the attribute is not known.  */
32393
32394 int
32395 arm_convert_symbolic_attribute (const char *name)
32396 {
32397   static const struct
32398   {
32399     const char * name;
32400     const int    tag;
32401   }
32402   attribute_table[] =
32403     {
32404       /* When you modify this table you should
32405          also modify the list in doc/c-arm.texi.  */
32406 #define T(tag) {#tag, tag}
32407       T (Tag_CPU_raw_name),
32408       T (Tag_CPU_name),
32409       T (Tag_CPU_arch),
32410       T (Tag_CPU_arch_profile),
32411       T (Tag_ARM_ISA_use),
32412       T (Tag_THUMB_ISA_use),
32413       T (Tag_FP_arch),
32414       T (Tag_VFP_arch),
32415       T (Tag_WMMX_arch),
32416       T (Tag_Advanced_SIMD_arch),
32417       T (Tag_PCS_config),
32418       T (Tag_ABI_PCS_R9_use),
32419       T (Tag_ABI_PCS_RW_data),
32420       T (Tag_ABI_PCS_RO_data),
32421       T (Tag_ABI_PCS_GOT_use),
32422       T (Tag_ABI_PCS_wchar_t),
32423       T (Tag_ABI_FP_rounding),
32424       T (Tag_ABI_FP_denormal),
32425       T (Tag_ABI_FP_exceptions),
32426       T (Tag_ABI_FP_user_exceptions),
32427       T (Tag_ABI_FP_number_model),
32428       T (Tag_ABI_align_needed),
32429       T (Tag_ABI_align8_needed),
32430       T (Tag_ABI_align_preserved),
32431       T (Tag_ABI_align8_preserved),
32432       T (Tag_ABI_enum_size),
32433       T (Tag_ABI_HardFP_use),
32434       T (Tag_ABI_VFP_args),
32435       T (Tag_ABI_WMMX_args),
32436       T (Tag_ABI_optimization_goals),
32437       T (Tag_ABI_FP_optimization_goals),
32438       T (Tag_compatibility),
32439       T (Tag_CPU_unaligned_access),
32440       T (Tag_FP_HP_extension),
32441       T (Tag_VFP_HP_extension),
32442       T (Tag_ABI_FP_16bit_format),
32443       T (Tag_MPextension_use),
32444       T (Tag_DIV_use),
32445       T (Tag_nodefaults),
32446       T (Tag_also_compatible_with),
32447       T (Tag_conformance),
32448       T (Tag_T2EE_use),
32449       T (Tag_Virtualization_use),
32450       T (Tag_DSP_extension),
32451       T (Tag_MVE_arch),
32452       /* We deliberately do not include Tag_MPextension_use_legacy.  */
32453 #undef T
32454     };
32455   unsigned int i;
32456
32457   if (name == NULL)
32458     return -1;
32459
32460   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
32461     if (streq (name, attribute_table[i].name))
32462       return attribute_table[i].tag;
32463
32464   return -1;
32465 }
32466
32467 /* Apply sym value for relocations only in the case that they are for
32468    local symbols in the same segment as the fixup and you have the
32469    respective architectural feature for blx and simple switches.  */
32470
32471 int
32472 arm_apply_sym_value (struct fix * fixP, segT this_seg)
32473 {
32474   if (fixP->fx_addsy
32475       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
32476       /* PR 17444: If the local symbol is in a different section then a reloc
32477          will always be generated for it, so applying the symbol value now
32478          will result in a double offset being stored in the relocation.  */
32479       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
32480       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
32481     {
32482       switch (fixP->fx_r_type)
32483         {
32484         case BFD_RELOC_ARM_PCREL_BLX:
32485         case BFD_RELOC_THUMB_PCREL_BRANCH23:
32486           if (ARM_IS_FUNC (fixP->fx_addsy))
32487             return 1;
32488           break;
32489
32490         case BFD_RELOC_ARM_PCREL_CALL:
32491         case BFD_RELOC_THUMB_PCREL_BLX:
32492           if (THUMB_IS_FUNC (fixP->fx_addsy))
32493             return 1;
32494           break;
32495
32496         default:
32497           break;
32498         }
32499
32500     }
32501   return 0;
32502 }
32503 #endif /* OBJ_ELF */