Arm: Add support for missing CPUs
[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 || prec == 1)
1241     for (i = 0; i < prec; i++)
1242       {
1243         md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1244         litP += sizeof (LITTLENUM_TYPE);
1245       }
1246   else if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1247     for (i = prec - 1; i >= 0; i--)
1248       {
1249         md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1250         litP += sizeof (LITTLENUM_TYPE);
1251       }
1252   else
1253     /* For a 4 byte float the order of elements in `words' is 1 0.
1254        For an 8 byte float the order is 1 0 3 2.  */
1255     for (i = 0; i < prec; i += 2)
1256       {
1257         md_number_to_chars (litP, (valueT) words[i + 1],
1258                             sizeof (LITTLENUM_TYPE));
1259         md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1260                             (valueT) words[i], sizeof (LITTLENUM_TYPE));
1261         litP += 2 * sizeof (LITTLENUM_TYPE);
1262       }
1263
1264   return NULL;
1265 }
1266
1267 /* We handle all bad expressions here, so that we can report the faulty
1268    instruction in the error message.  */
1269
1270 void
1271 md_operand (expressionS * exp)
1272 {
1273   if (in_my_get_expression)
1274     exp->X_op = O_illegal;
1275 }
1276
1277 /* Immediate values.  */
1278
1279 #ifdef OBJ_ELF
1280 /* Generic immediate-value read function for use in directives.
1281    Accepts anything that 'expression' can fold to a constant.
1282    *val receives the number.  */
1283
1284 static int
1285 immediate_for_directive (int *val)
1286 {
1287   expressionS exp;
1288   exp.X_op = O_illegal;
1289
1290   if (is_immediate_prefix (*input_line_pointer))
1291     {
1292       input_line_pointer++;
1293       expression (&exp);
1294     }
1295
1296   if (exp.X_op != O_constant)
1297     {
1298       as_bad (_("expected #constant"));
1299       ignore_rest_of_line ();
1300       return FAIL;
1301     }
1302   *val = exp.X_add_number;
1303   return SUCCESS;
1304 }
1305 #endif
1306
1307 /* Register parsing.  */
1308
1309 /* Generic register parser.  CCP points to what should be the
1310    beginning of a register name.  If it is indeed a valid register
1311    name, advance CCP over it and return the reg_entry structure;
1312    otherwise return NULL.  Does not issue diagnostics.  */
1313
1314 static struct reg_entry *
1315 arm_reg_parse_multi (char **ccp)
1316 {
1317   char *start = *ccp;
1318   char *p;
1319   struct reg_entry *reg;
1320
1321   skip_whitespace (start);
1322
1323 #ifdef REGISTER_PREFIX
1324   if (*start != REGISTER_PREFIX)
1325     return NULL;
1326   start++;
1327 #endif
1328 #ifdef OPTIONAL_REGISTER_PREFIX
1329   if (*start == OPTIONAL_REGISTER_PREFIX)
1330     start++;
1331 #endif
1332
1333   p = start;
1334   if (!ISALPHA (*p) || !is_name_beginner (*p))
1335     return NULL;
1336
1337   do
1338     p++;
1339   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1340
1341   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1342
1343   if (!reg)
1344     return NULL;
1345
1346   *ccp = p;
1347   return reg;
1348 }
1349
1350 static int
1351 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1352                     enum arm_reg_type type)
1353 {
1354   /* Alternative syntaxes are accepted for a few register classes.  */
1355   switch (type)
1356     {
1357     case REG_TYPE_MVF:
1358     case REG_TYPE_MVD:
1359     case REG_TYPE_MVFX:
1360     case REG_TYPE_MVDX:
1361       /* Generic coprocessor register names are allowed for these.  */
1362       if (reg && reg->type == REG_TYPE_CN)
1363         return reg->number;
1364       break;
1365
1366     case REG_TYPE_CP:
1367       /* For backward compatibility, a bare number is valid here.  */
1368       {
1369         unsigned long processor = strtoul (start, ccp, 10);
1370         if (*ccp != start && processor <= 15)
1371           return processor;
1372       }
1373       /* Fall through.  */
1374
1375     case REG_TYPE_MMXWC:
1376       /* WC includes WCG.  ??? I'm not sure this is true for all
1377          instructions that take WC registers.  */
1378       if (reg && reg->type == REG_TYPE_MMXWCG)
1379         return reg->number;
1380       break;
1381
1382     default:
1383       break;
1384     }
1385
1386   return FAIL;
1387 }
1388
1389 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1390    return value is the register number or FAIL.  */
1391
1392 static int
1393 arm_reg_parse (char **ccp, enum arm_reg_type type)
1394 {
1395   char *start = *ccp;
1396   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1397   int ret;
1398
1399   /* Do not allow a scalar (reg+index) to parse as a register.  */
1400   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1401     return FAIL;
1402
1403   if (reg && reg->type == type)
1404     return reg->number;
1405
1406   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1407     return ret;
1408
1409   *ccp = start;
1410   return FAIL;
1411 }
1412
1413 /* Parse a Neon type specifier. *STR should point at the leading '.'
1414    character. Does no verification at this stage that the type fits the opcode
1415    properly. E.g.,
1416
1417      .i32.i32.s16
1418      .s32.f32
1419      .u16
1420
1421    Can all be legally parsed by this function.
1422
1423    Fills in neon_type struct pointer with parsed information, and updates STR
1424    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1425    type, FAIL if not.  */
1426
1427 static int
1428 parse_neon_type (struct neon_type *type, char **str)
1429 {
1430   char *ptr = *str;
1431
1432   if (type)
1433     type->elems = 0;
1434
1435   while (type->elems < NEON_MAX_TYPE_ELS)
1436     {
1437       enum neon_el_type thistype = NT_untyped;
1438       unsigned thissize = -1u;
1439
1440       if (*ptr != '.')
1441         break;
1442
1443       ptr++;
1444
1445       /* Just a size without an explicit type.  */
1446       if (ISDIGIT (*ptr))
1447         goto parsesize;
1448
1449       switch (TOLOWER (*ptr))
1450         {
1451         case 'i': thistype = NT_integer; break;
1452         case 'f': thistype = NT_float; break;
1453         case 'p': thistype = NT_poly; break;
1454         case 's': thistype = NT_signed; break;
1455         case 'u': thistype = NT_unsigned; break;
1456         case 'd':
1457           thistype = NT_float;
1458           thissize = 64;
1459           ptr++;
1460           goto done;
1461         default:
1462           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1463           return FAIL;
1464         }
1465
1466       ptr++;
1467
1468       /* .f is an abbreviation for .f32.  */
1469       if (thistype == NT_float && !ISDIGIT (*ptr))
1470         thissize = 32;
1471       else
1472         {
1473         parsesize:
1474           thissize = strtoul (ptr, &ptr, 10);
1475
1476           if (thissize != 8 && thissize != 16 && thissize != 32
1477               && thissize != 64)
1478             {
1479               as_bad (_("bad size %d in type specifier"), thissize);
1480               return FAIL;
1481             }
1482         }
1483
1484       done:
1485       if (type)
1486         {
1487           type->el[type->elems].type = thistype;
1488           type->el[type->elems].size = thissize;
1489           type->elems++;
1490         }
1491     }
1492
1493   /* Empty/missing type is not a successful parse.  */
1494   if (type->elems == 0)
1495     return FAIL;
1496
1497   *str = ptr;
1498
1499   return SUCCESS;
1500 }
1501
1502 /* Errors may be set multiple times during parsing or bit encoding
1503    (particularly in the Neon bits), but usually the earliest error which is set
1504    will be the most meaningful. Avoid overwriting it with later (cascading)
1505    errors by calling this function.  */
1506
1507 static void
1508 first_error (const char *err)
1509 {
1510   if (!inst.error)
1511     inst.error = err;
1512 }
1513
1514 /* Parse a single type, e.g. ".s32", leading period included.  */
1515 static int
1516 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1517 {
1518   char *str = *ccp;
1519   struct neon_type optype;
1520
1521   if (*str == '.')
1522     {
1523       if (parse_neon_type (&optype, &str) == SUCCESS)
1524         {
1525           if (optype.elems == 1)
1526             *vectype = optype.el[0];
1527           else
1528             {
1529               first_error (_("only one type should be specified for operand"));
1530               return FAIL;
1531             }
1532         }
1533       else
1534         {
1535           first_error (_("vector type expected"));
1536           return FAIL;
1537         }
1538     }
1539   else
1540     return FAIL;
1541
1542   *ccp = str;
1543
1544   return SUCCESS;
1545 }
1546
1547 /* Special meanings for indices (which have a range of 0-7), which will fit into
1548    a 4-bit integer.  */
1549
1550 #define NEON_ALL_LANES          15
1551 #define NEON_INTERLEAVE_LANES   14
1552
1553 /* Record a use of the given feature.  */
1554 static void
1555 record_feature_use (const arm_feature_set *feature)
1556 {
1557   if (thumb_mode)
1558     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
1559   else
1560     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
1561 }
1562
1563 /* If the given feature available in the selected CPU, mark it as used.
1564    Returns TRUE iff feature is available.  */
1565 static bfd_boolean
1566 mark_feature_used (const arm_feature_set *feature)
1567 {
1568
1569   /* Do not support the use of MVE only instructions when in auto-detection or
1570      -march=all.  */
1571   if (((feature == &mve_ext) || (feature == &mve_fp_ext))
1572       && ARM_CPU_IS_ANY (cpu_variant))
1573     {
1574       first_error (BAD_MVE_AUTO);
1575       return FALSE;
1576     }
1577   /* Ensure the option is valid on the current architecture.  */
1578   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
1579     return FALSE;
1580
1581   /* Add the appropriate architecture feature for the barrier option used.
1582      */
1583   record_feature_use (feature);
1584
1585   return TRUE;
1586 }
1587
1588 /* Parse either a register or a scalar, with an optional type. Return the
1589    register number, and optionally fill in the actual type of the register
1590    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1591    type/index information in *TYPEINFO.  */
1592
1593 static int
1594 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1595                            enum arm_reg_type *rtype,
1596                            struct neon_typed_alias *typeinfo)
1597 {
1598   char *str = *ccp;
1599   struct reg_entry *reg = arm_reg_parse_multi (&str);
1600   struct neon_typed_alias atype;
1601   struct neon_type_el parsetype;
1602
1603   atype.defined = 0;
1604   atype.index = -1;
1605   atype.eltype.type = NT_invtype;
1606   atype.eltype.size = -1;
1607
1608   /* Try alternate syntax for some types of register. Note these are mutually
1609      exclusive with the Neon syntax extensions.  */
1610   if (reg == NULL)
1611     {
1612       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1613       if (altreg != FAIL)
1614         *ccp = str;
1615       if (typeinfo)
1616         *typeinfo = atype;
1617       return altreg;
1618     }
1619
1620   /* Undo polymorphism when a set of register types may be accepted.  */
1621   if ((type == REG_TYPE_NDQ
1622        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1623       || (type == REG_TYPE_VFSD
1624           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1625       || (type == REG_TYPE_NSDQ
1626           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1627               || reg->type == REG_TYPE_NQ))
1628       || (type == REG_TYPE_NSD
1629           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1630       || (type == REG_TYPE_MMXWC
1631           && (reg->type == REG_TYPE_MMXWCG)))
1632     type = (enum arm_reg_type) reg->type;
1633
1634   if (type == REG_TYPE_MQ)
1635     {
1636       if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1637         return FAIL;
1638
1639       if (!reg || reg->type != REG_TYPE_NQ)
1640         return FAIL;
1641
1642       if (reg->number > 14 && !mark_feature_used (&fpu_vfp_ext_d32))
1643         {
1644           first_error (_("expected MVE register [q0..q7]"));
1645           return FAIL;
1646         }
1647       type = REG_TYPE_NQ;
1648     }
1649   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
1650            && (type == REG_TYPE_NQ))
1651     return FAIL;
1652
1653
1654   if (type != reg->type)
1655     return FAIL;
1656
1657   if (reg->neon)
1658     atype = *reg->neon;
1659
1660   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1661     {
1662       if ((atype.defined & NTA_HASTYPE) != 0)
1663         {
1664           first_error (_("can't redefine type for operand"));
1665           return FAIL;
1666         }
1667       atype.defined |= NTA_HASTYPE;
1668       atype.eltype = parsetype;
1669     }
1670
1671   if (skip_past_char (&str, '[') == SUCCESS)
1672     {
1673       if (type != REG_TYPE_VFD
1674           && !(type == REG_TYPE_VFS
1675                && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_2))
1676           && !(type == REG_TYPE_NQ
1677                && ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)))
1678         {
1679           if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1680             first_error (_("only D and Q registers may be indexed"));
1681           else
1682             first_error (_("only D registers may be indexed"));
1683           return FAIL;
1684         }
1685
1686       if ((atype.defined & NTA_HASINDEX) != 0)
1687         {
1688           first_error (_("can't change index for operand"));
1689           return FAIL;
1690         }
1691
1692       atype.defined |= NTA_HASINDEX;
1693
1694       if (skip_past_char (&str, ']') == SUCCESS)
1695         atype.index = NEON_ALL_LANES;
1696       else
1697         {
1698           expressionS exp;
1699
1700           my_get_expression (&exp, &str, GE_NO_PREFIX);
1701
1702           if (exp.X_op != O_constant)
1703             {
1704               first_error (_("constant expression required"));
1705               return FAIL;
1706             }
1707
1708           if (skip_past_char (&str, ']') == FAIL)
1709             return FAIL;
1710
1711           atype.index = exp.X_add_number;
1712         }
1713     }
1714
1715   if (typeinfo)
1716     *typeinfo = atype;
1717
1718   if (rtype)
1719     *rtype = type;
1720
1721   *ccp = str;
1722
1723   return reg->number;
1724 }
1725
1726 /* Like arm_reg_parse, but also allow the following extra features:
1727     - If RTYPE is non-zero, return the (possibly restricted) type of the
1728       register (e.g. Neon double or quad reg when either has been requested).
1729     - If this is a Neon vector type with additional type information, fill
1730       in the struct pointed to by VECTYPE (if non-NULL).
1731    This function will fault on encountering a scalar.  */
1732
1733 static int
1734 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1735                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1736 {
1737   struct neon_typed_alias atype;
1738   char *str = *ccp;
1739   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1740
1741   if (reg == FAIL)
1742     return FAIL;
1743
1744   /* Do not allow regname(... to parse as a register.  */
1745   if (*str == '(')
1746     return FAIL;
1747
1748   /* Do not allow a scalar (reg+index) to parse as a register.  */
1749   if ((atype.defined & NTA_HASINDEX) != 0)
1750     {
1751       first_error (_("register operand expected, but got scalar"));
1752       return FAIL;
1753     }
1754
1755   if (vectype)
1756     *vectype = atype.eltype;
1757
1758   *ccp = str;
1759
1760   return reg;
1761 }
1762
1763 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1764 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1765
1766 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1767    have enough information to be able to do a good job bounds-checking. So, we
1768    just do easy checks here, and do further checks later.  */
1769
1770 static int
1771 parse_scalar (char **ccp, int elsize, struct neon_type_el *type, enum
1772               arm_reg_type reg_type)
1773 {
1774   int reg;
1775   char *str = *ccp;
1776   struct neon_typed_alias atype;
1777   unsigned reg_size;
1778
1779   reg = parse_typed_reg_or_scalar (&str, reg_type, NULL, &atype);
1780
1781   switch (reg_type)
1782     {
1783     case REG_TYPE_VFS:
1784       reg_size = 32;
1785       break;
1786     case REG_TYPE_VFD:
1787       reg_size = 64;
1788       break;
1789     case REG_TYPE_MQ:
1790       reg_size = 128;
1791       break;
1792     default:
1793       gas_assert (0);
1794       return FAIL;
1795     }
1796
1797   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1798     return FAIL;
1799
1800   if (reg_type != REG_TYPE_MQ && atype.index == NEON_ALL_LANES)
1801     {
1802       first_error (_("scalar must have an index"));
1803       return FAIL;
1804     }
1805   else if (atype.index >= reg_size / elsize)
1806     {
1807       first_error (_("scalar index out of range"));
1808       return FAIL;
1809     }
1810
1811   if (type)
1812     *type = atype.eltype;
1813
1814   *ccp = str;
1815
1816   return reg * 16 + atype.index;
1817 }
1818
1819 /* Types of registers in a list.  */
1820
1821 enum reg_list_els
1822 {
1823   REGLIST_RN,
1824   REGLIST_CLRM,
1825   REGLIST_VFP_S,
1826   REGLIST_VFP_S_VPR,
1827   REGLIST_VFP_D,
1828   REGLIST_VFP_D_VPR,
1829   REGLIST_NEON_D
1830 };
1831
1832 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1833
1834 static long
1835 parse_reg_list (char ** strp, enum reg_list_els etype)
1836 {
1837   char *str = *strp;
1838   long range = 0;
1839   int another_range;
1840
1841   gas_assert (etype == REGLIST_RN || etype == REGLIST_CLRM);
1842
1843   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1844   do
1845     {
1846       skip_whitespace (str);
1847
1848       another_range = 0;
1849
1850       if (*str == '{')
1851         {
1852           int in_range = 0;
1853           int cur_reg = -1;
1854
1855           str++;
1856           do
1857             {
1858               int reg;
1859               const char apsr_str[] = "apsr";
1860               int apsr_str_len = strlen (apsr_str);
1861
1862               reg = arm_reg_parse (&str, REGLIST_RN);
1863               if (etype == REGLIST_CLRM)
1864                 {
1865                   if (reg == REG_SP || reg == REG_PC)
1866                     reg = FAIL;
1867                   else if (reg == FAIL
1868                            && !strncasecmp (str, apsr_str, apsr_str_len)
1869                            && !ISALPHA (*(str + apsr_str_len)))
1870                     {
1871                       reg = 15;
1872                       str += apsr_str_len;
1873                     }
1874
1875                   if (reg == FAIL)
1876                     {
1877                       first_error (_("r0-r12, lr or APSR expected"));
1878                       return FAIL;
1879                     }
1880                 }
1881               else /* etype == REGLIST_RN.  */
1882                 {
1883                   if (reg == FAIL)
1884                     {
1885                       first_error (_(reg_expected_msgs[REGLIST_RN]));
1886                       return FAIL;
1887                     }
1888                 }
1889
1890               if (in_range)
1891                 {
1892                   int i;
1893
1894                   if (reg <= cur_reg)
1895                     {
1896                       first_error (_("bad range in register list"));
1897                       return FAIL;
1898                     }
1899
1900                   for (i = cur_reg + 1; i < reg; i++)
1901                     {
1902                       if (range & (1 << i))
1903                         as_tsktsk
1904                           (_("Warning: duplicated register (r%d) in register list"),
1905                            i);
1906                       else
1907                         range |= 1 << i;
1908                     }
1909                   in_range = 0;
1910                 }
1911
1912               if (range & (1 << reg))
1913                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1914                            reg);
1915               else if (reg <= cur_reg)
1916                 as_tsktsk (_("Warning: register range not in ascending order"));
1917
1918               range |= 1 << reg;
1919               cur_reg = reg;
1920             }
1921           while (skip_past_comma (&str) != FAIL
1922                  || (in_range = 1, *str++ == '-'));
1923           str--;
1924
1925           if (skip_past_char (&str, '}') == FAIL)
1926             {
1927               first_error (_("missing `}'"));
1928               return FAIL;
1929             }
1930         }
1931       else if (etype == REGLIST_RN)
1932         {
1933           expressionS exp;
1934
1935           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1936             return FAIL;
1937
1938           if (exp.X_op == O_constant)
1939             {
1940               if (exp.X_add_number
1941                   != (exp.X_add_number & 0x0000ffff))
1942                 {
1943                   inst.error = _("invalid register mask");
1944                   return FAIL;
1945                 }
1946
1947               if ((range & exp.X_add_number) != 0)
1948                 {
1949                   int regno = range & exp.X_add_number;
1950
1951                   regno &= -regno;
1952                   regno = (1 << regno) - 1;
1953                   as_tsktsk
1954                     (_("Warning: duplicated register (r%d) in register list"),
1955                      regno);
1956                 }
1957
1958               range |= exp.X_add_number;
1959             }
1960           else
1961             {
1962               if (inst.relocs[0].type != 0)
1963                 {
1964                   inst.error = _("expression too complex");
1965                   return FAIL;
1966                 }
1967
1968               memcpy (&inst.relocs[0].exp, &exp, sizeof (expressionS));
1969               inst.relocs[0].type = BFD_RELOC_ARM_MULTI;
1970               inst.relocs[0].pc_rel = 0;
1971             }
1972         }
1973
1974       if (*str == '|' || *str == '+')
1975         {
1976           str++;
1977           another_range = 1;
1978         }
1979     }
1980   while (another_range);
1981
1982   *strp = str;
1983   return range;
1984 }
1985
1986 /* Parse a VFP register list.  If the string is invalid return FAIL.
1987    Otherwise return the number of registers, and set PBASE to the first
1988    register.  Parses registers of type ETYPE.
1989    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1990      - Q registers can be used to specify pairs of D registers
1991      - { } can be omitted from around a singleton register list
1992          FIXME: This is not implemented, as it would require backtracking in
1993          some cases, e.g.:
1994            vtbl.8 d3,d4,d5
1995          This could be done (the meaning isn't really ambiguous), but doesn't
1996          fit in well with the current parsing framework.
1997      - 32 D registers may be used (also true for VFPv3).
1998    FIXME: Types are ignored in these register lists, which is probably a
1999    bug.  */
2000
2001 static int
2002 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype,
2003                     bfd_boolean *partial_match)
2004 {
2005   char *str = *ccp;
2006   int base_reg;
2007   int new_base;
2008   enum arm_reg_type regtype = (enum arm_reg_type) 0;
2009   int max_regs = 0;
2010   int count = 0;
2011   int warned = 0;
2012   unsigned long mask = 0;
2013   int i;
2014   bfd_boolean vpr_seen = FALSE;
2015   bfd_boolean expect_vpr =
2016     (etype == REGLIST_VFP_S_VPR) || (etype == REGLIST_VFP_D_VPR);
2017
2018   if (skip_past_char (&str, '{') == FAIL)
2019     {
2020       inst.error = _("expecting {");
2021       return FAIL;
2022     }
2023
2024   switch (etype)
2025     {
2026     case REGLIST_VFP_S:
2027     case REGLIST_VFP_S_VPR:
2028       regtype = REG_TYPE_VFS;
2029       max_regs = 32;
2030       break;
2031
2032     case REGLIST_VFP_D:
2033     case REGLIST_VFP_D_VPR:
2034       regtype = REG_TYPE_VFD;
2035       break;
2036
2037     case REGLIST_NEON_D:
2038       regtype = REG_TYPE_NDQ;
2039       break;
2040
2041     default:
2042       gas_assert (0);
2043     }
2044
2045   if (etype != REGLIST_VFP_S && etype != REGLIST_VFP_S_VPR)
2046     {
2047       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
2048       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
2049         {
2050           max_regs = 32;
2051           if (thumb_mode)
2052             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
2053                                     fpu_vfp_ext_d32);
2054           else
2055             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
2056                                     fpu_vfp_ext_d32);
2057         }
2058       else
2059         max_regs = 16;
2060     }
2061
2062   base_reg = max_regs;
2063   *partial_match = FALSE;
2064
2065   do
2066     {
2067       int setmask = 1, addregs = 1;
2068       const char vpr_str[] = "vpr";
2069       int vpr_str_len = strlen (vpr_str);
2070
2071       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
2072
2073       if (expect_vpr)
2074         {
2075           if (new_base == FAIL
2076               && !strncasecmp (str, vpr_str, vpr_str_len)
2077               && !ISALPHA (*(str + vpr_str_len))
2078               && !vpr_seen)
2079             {
2080               vpr_seen = TRUE;
2081               str += vpr_str_len;
2082               if (count == 0)
2083                 base_reg = 0; /* Canonicalize VPR only on d0 with 0 regs.  */
2084             }
2085           else if (vpr_seen)
2086             {
2087               first_error (_("VPR expected last"));
2088               return FAIL;
2089             }
2090           else if (new_base == FAIL)
2091             {
2092               if (regtype == REG_TYPE_VFS)
2093                 first_error (_("VFP single precision register or VPR "
2094                                "expected"));
2095               else /* regtype == REG_TYPE_VFD.  */
2096                 first_error (_("VFP/Neon double precision register or VPR "
2097                                "expected"));
2098               return FAIL;
2099             }
2100         }
2101       else if (new_base == FAIL)
2102         {
2103           first_error (_(reg_expected_msgs[regtype]));
2104           return FAIL;
2105         }
2106
2107       *partial_match = TRUE;
2108       if (vpr_seen)
2109         continue;
2110
2111       if (new_base >= max_regs)
2112         {
2113           first_error (_("register out of range in list"));
2114           return FAIL;
2115         }
2116
2117       /* Note: a value of 2 * n is returned for the register Q<n>.  */
2118       if (regtype == REG_TYPE_NQ)
2119         {
2120           setmask = 3;
2121           addregs = 2;
2122         }
2123
2124       if (new_base < base_reg)
2125         base_reg = new_base;
2126
2127       if (mask & (setmask << new_base))
2128         {
2129           first_error (_("invalid register list"));
2130           return FAIL;
2131         }
2132
2133       if ((mask >> new_base) != 0 && ! warned && !vpr_seen)
2134         {
2135           as_tsktsk (_("register list not in ascending order"));
2136           warned = 1;
2137         }
2138
2139       mask |= setmask << new_base;
2140       count += addregs;
2141
2142       if (*str == '-') /* We have the start of a range expression */
2143         {
2144           int high_range;
2145
2146           str++;
2147
2148           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
2149               == FAIL)
2150             {
2151               inst.error = gettext (reg_expected_msgs[regtype]);
2152               return FAIL;
2153             }
2154
2155           if (high_range >= max_regs)
2156             {
2157               first_error (_("register out of range in list"));
2158               return FAIL;
2159             }
2160
2161           if (regtype == REG_TYPE_NQ)
2162             high_range = high_range + 1;
2163
2164           if (high_range <= new_base)
2165             {
2166               inst.error = _("register range not in ascending order");
2167               return FAIL;
2168             }
2169
2170           for (new_base += addregs; new_base <= high_range; new_base += addregs)
2171             {
2172               if (mask & (setmask << new_base))
2173                 {
2174                   inst.error = _("invalid register list");
2175                   return FAIL;
2176                 }
2177
2178               mask |= setmask << new_base;
2179               count += addregs;
2180             }
2181         }
2182     }
2183   while (skip_past_comma (&str) != FAIL);
2184
2185   str++;
2186
2187   /* Sanity check -- should have raised a parse error above.  */
2188   if ((!vpr_seen && count == 0) || count > max_regs)
2189     abort ();
2190
2191   *pbase = base_reg;
2192
2193   if (expect_vpr && !vpr_seen)
2194     {
2195       first_error (_("VPR expected last"));
2196       return FAIL;
2197     }
2198
2199   /* Final test -- the registers must be consecutive.  */
2200   mask >>= base_reg;
2201   for (i = 0; i < count; i++)
2202     {
2203       if ((mask & (1u << i)) == 0)
2204         {
2205           inst.error = _("non-contiguous register range");
2206           return FAIL;
2207         }
2208     }
2209
2210   *ccp = str;
2211
2212   return count;
2213 }
2214
2215 /* True if two alias types are the same.  */
2216
2217 static bfd_boolean
2218 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
2219 {
2220   if (!a && !b)
2221     return TRUE;
2222
2223   if (!a || !b)
2224     return FALSE;
2225
2226   if (a->defined != b->defined)
2227     return FALSE;
2228
2229   if ((a->defined & NTA_HASTYPE) != 0
2230       && (a->eltype.type != b->eltype.type
2231           || a->eltype.size != b->eltype.size))
2232     return FALSE;
2233
2234   if ((a->defined & NTA_HASINDEX) != 0
2235       && (a->index != b->index))
2236     return FALSE;
2237
2238   return TRUE;
2239 }
2240
2241 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
2242    The base register is put in *PBASE.
2243    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
2244    the return value.
2245    The register stride (minus one) is put in bit 4 of the return value.
2246    Bits [6:5] encode the list length (minus one).
2247    The type of the list elements is put in *ELTYPE, if non-NULL.  */
2248
2249 #define NEON_LANE(X)            ((X) & 0xf)
2250 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
2251 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2252
2253 static int
2254 parse_neon_el_struct_list (char **str, unsigned *pbase,
2255                            int mve,
2256                            struct neon_type_el *eltype)
2257 {
2258   char *ptr = *str;
2259   int base_reg = -1;
2260   int reg_incr = -1;
2261   int count = 0;
2262   int lane = -1;
2263   int leading_brace = 0;
2264   enum arm_reg_type rtype = REG_TYPE_NDQ;
2265   const char *const incr_error = mve ? _("register stride must be 1") :
2266     _("register stride must be 1 or 2");
2267   const char *const type_error = _("mismatched element/structure types in list");
2268   struct neon_typed_alias firsttype;
2269   firsttype.defined = 0;
2270   firsttype.eltype.type = NT_invtype;
2271   firsttype.eltype.size = -1;
2272   firsttype.index = -1;
2273
2274   if (skip_past_char (&ptr, '{') == SUCCESS)
2275     leading_brace = 1;
2276
2277   do
2278     {
2279       struct neon_typed_alias atype;
2280       if (mve)
2281         rtype = REG_TYPE_MQ;
2282       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2283
2284       if (getreg == FAIL)
2285         {
2286           first_error (_(reg_expected_msgs[rtype]));
2287           return FAIL;
2288         }
2289
2290       if (base_reg == -1)
2291         {
2292           base_reg = getreg;
2293           if (rtype == REG_TYPE_NQ)
2294             {
2295               reg_incr = 1;
2296             }
2297           firsttype = atype;
2298         }
2299       else if (reg_incr == -1)
2300         {
2301           reg_incr = getreg - base_reg;
2302           if (reg_incr < 1 || reg_incr > 2)
2303             {
2304               first_error (_(incr_error));
2305               return FAIL;
2306             }
2307         }
2308       else if (getreg != base_reg + reg_incr * count)
2309         {
2310           first_error (_(incr_error));
2311           return FAIL;
2312         }
2313
2314       if (! neon_alias_types_same (&atype, &firsttype))
2315         {
2316           first_error (_(type_error));
2317           return FAIL;
2318         }
2319
2320       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2321          modes.  */
2322       if (ptr[0] == '-')
2323         {
2324           struct neon_typed_alias htype;
2325           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2326           if (lane == -1)
2327             lane = NEON_INTERLEAVE_LANES;
2328           else if (lane != NEON_INTERLEAVE_LANES)
2329             {
2330               first_error (_(type_error));
2331               return FAIL;
2332             }
2333           if (reg_incr == -1)
2334             reg_incr = 1;
2335           else if (reg_incr != 1)
2336             {
2337               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2338               return FAIL;
2339             }
2340           ptr++;
2341           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2342           if (hireg == FAIL)
2343             {
2344               first_error (_(reg_expected_msgs[rtype]));
2345               return FAIL;
2346             }
2347           if (! neon_alias_types_same (&htype, &firsttype))
2348             {
2349               first_error (_(type_error));
2350               return FAIL;
2351             }
2352           count += hireg + dregs - getreg;
2353           continue;
2354         }
2355
2356       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2357       if (rtype == REG_TYPE_NQ)
2358         {
2359           count += 2;
2360           continue;
2361         }
2362
2363       if ((atype.defined & NTA_HASINDEX) != 0)
2364         {
2365           if (lane == -1)
2366             lane = atype.index;
2367           else if (lane != atype.index)
2368             {
2369               first_error (_(type_error));
2370               return FAIL;
2371             }
2372         }
2373       else if (lane == -1)
2374         lane = NEON_INTERLEAVE_LANES;
2375       else if (lane != NEON_INTERLEAVE_LANES)
2376         {
2377           first_error (_(type_error));
2378           return FAIL;
2379         }
2380       count++;
2381     }
2382   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2383
2384   /* No lane set by [x]. We must be interleaving structures.  */
2385   if (lane == -1)
2386     lane = NEON_INTERLEAVE_LANES;
2387
2388   /* Sanity check.  */
2389   if (lane == -1 || base_reg == -1 || count < 1 || (!mve && count > 4)
2390       || (count > 1 && reg_incr == -1))
2391     {
2392       first_error (_("error parsing element/structure list"));
2393       return FAIL;
2394     }
2395
2396   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2397     {
2398       first_error (_("expected }"));
2399       return FAIL;
2400     }
2401
2402   if (reg_incr == -1)
2403     reg_incr = 1;
2404
2405   if (eltype)
2406     *eltype = firsttype.eltype;
2407
2408   *pbase = base_reg;
2409   *str = ptr;
2410
2411   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2412 }
2413
2414 /* Parse an explicit relocation suffix on an expression.  This is
2415    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2416    arm_reloc_hsh contains no entries, so this function can only
2417    succeed if there is no () after the word.  Returns -1 on error,
2418    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2419
2420 static int
2421 parse_reloc (char **str)
2422 {
2423   struct reloc_entry *r;
2424   char *p, *q;
2425
2426   if (**str != '(')
2427     return BFD_RELOC_UNUSED;
2428
2429   p = *str + 1;
2430   q = p;
2431
2432   while (*q && *q != ')' && *q != ',')
2433     q++;
2434   if (*q != ')')
2435     return -1;
2436
2437   if ((r = (struct reloc_entry *)
2438        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2439     return -1;
2440
2441   *str = q + 1;
2442   return r->reloc;
2443 }
2444
2445 /* Directives: register aliases.  */
2446
2447 static struct reg_entry *
2448 insert_reg_alias (char *str, unsigned number, int type)
2449 {
2450   struct reg_entry *new_reg;
2451   const char *name;
2452
2453   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2454     {
2455       if (new_reg->builtin)
2456         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2457
2458       /* Only warn about a redefinition if it's not defined as the
2459          same register.  */
2460       else if (new_reg->number != number || new_reg->type != type)
2461         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2462
2463       return NULL;
2464     }
2465
2466   name = xstrdup (str);
2467   new_reg = XNEW (struct reg_entry);
2468
2469   new_reg->name = name;
2470   new_reg->number = number;
2471   new_reg->type = type;
2472   new_reg->builtin = FALSE;
2473   new_reg->neon = NULL;
2474
2475   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2476     abort ();
2477
2478   return new_reg;
2479 }
2480
2481 static void
2482 insert_neon_reg_alias (char *str, int number, int type,
2483                        struct neon_typed_alias *atype)
2484 {
2485   struct reg_entry *reg = insert_reg_alias (str, number, type);
2486
2487   if (!reg)
2488     {
2489       first_error (_("attempt to redefine typed alias"));
2490       return;
2491     }
2492
2493   if (atype)
2494     {
2495       reg->neon = XNEW (struct neon_typed_alias);
2496       *reg->neon = *atype;
2497     }
2498 }
2499
2500 /* Look for the .req directive.  This is of the form:
2501
2502         new_register_name .req existing_register_name
2503
2504    If we find one, or if it looks sufficiently like one that we want to
2505    handle any error here, return TRUE.  Otherwise return FALSE.  */
2506
2507 static bfd_boolean
2508 create_register_alias (char * newname, char *p)
2509 {
2510   struct reg_entry *old;
2511   char *oldname, *nbuf;
2512   size_t nlen;
2513
2514   /* The input scrubber ensures that whitespace after the mnemonic is
2515      collapsed to single spaces.  */
2516   oldname = p;
2517   if (strncmp (oldname, " .req ", 6) != 0)
2518     return FALSE;
2519
2520   oldname += 6;
2521   if (*oldname == '\0')
2522     return FALSE;
2523
2524   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2525   if (!old)
2526     {
2527       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2528       return TRUE;
2529     }
2530
2531   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2532      the desired alias name, and p points to its end.  If not, then
2533      the desired alias name is in the global original_case_string.  */
2534 #ifdef TC_CASE_SENSITIVE
2535   nlen = p - newname;
2536 #else
2537   newname = original_case_string;
2538   nlen = strlen (newname);
2539 #endif
2540
2541   nbuf = xmemdup0 (newname, nlen);
2542
2543   /* Create aliases under the new name as stated; an all-lowercase
2544      version of the new name; and an all-uppercase version of the new
2545      name.  */
2546   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2547     {
2548       for (p = nbuf; *p; p++)
2549         *p = TOUPPER (*p);
2550
2551       if (strncmp (nbuf, newname, nlen))
2552         {
2553           /* If this attempt to create an additional alias fails, do not bother
2554              trying to create the all-lower case alias.  We will fail and issue
2555              a second, duplicate error message.  This situation arises when the
2556              programmer does something like:
2557                foo .req r0
2558                Foo .req r1
2559              The second .req creates the "Foo" alias but then fails to create
2560              the artificial FOO alias because it has already been created by the
2561              first .req.  */
2562           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2563             {
2564               free (nbuf);
2565               return TRUE;
2566             }
2567         }
2568
2569       for (p = nbuf; *p; p++)
2570         *p = TOLOWER (*p);
2571
2572       if (strncmp (nbuf, newname, nlen))
2573         insert_reg_alias (nbuf, old->number, old->type);
2574     }
2575
2576   free (nbuf);
2577   return TRUE;
2578 }
2579
2580 /* Create a Neon typed/indexed register alias using directives, e.g.:
2581      X .dn d5.s32[1]
2582      Y .qn 6.s16
2583      Z .dn d7
2584      T .dn Z[0]
2585    These typed registers can be used instead of the types specified after the
2586    Neon mnemonic, so long as all operands given have types. Types can also be
2587    specified directly, e.g.:
2588      vadd d0.s32, d1.s32, d2.s32  */
2589
2590 static bfd_boolean
2591 create_neon_reg_alias (char *newname, char *p)
2592 {
2593   enum arm_reg_type basetype;
2594   struct reg_entry *basereg;
2595   struct reg_entry mybasereg;
2596   struct neon_type ntype;
2597   struct neon_typed_alias typeinfo;
2598   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2599   int namelen;
2600
2601   typeinfo.defined = 0;
2602   typeinfo.eltype.type = NT_invtype;
2603   typeinfo.eltype.size = -1;
2604   typeinfo.index = -1;
2605
2606   nameend = p;
2607
2608   if (strncmp (p, " .dn ", 5) == 0)
2609     basetype = REG_TYPE_VFD;
2610   else if (strncmp (p, " .qn ", 5) == 0)
2611     basetype = REG_TYPE_NQ;
2612   else
2613     return FALSE;
2614
2615   p += 5;
2616
2617   if (*p == '\0')
2618     return FALSE;
2619
2620   basereg = arm_reg_parse_multi (&p);
2621
2622   if (basereg && basereg->type != basetype)
2623     {
2624       as_bad (_("bad type for register"));
2625       return FALSE;
2626     }
2627
2628   if (basereg == NULL)
2629     {
2630       expressionS exp;
2631       /* Try parsing as an integer.  */
2632       my_get_expression (&exp, &p, GE_NO_PREFIX);
2633       if (exp.X_op != O_constant)
2634         {
2635           as_bad (_("expression must be constant"));
2636           return FALSE;
2637         }
2638       basereg = &mybasereg;
2639       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2640                                                   : exp.X_add_number;
2641       basereg->neon = 0;
2642     }
2643
2644   if (basereg->neon)
2645     typeinfo = *basereg->neon;
2646
2647   if (parse_neon_type (&ntype, &p) == SUCCESS)
2648     {
2649       /* We got a type.  */
2650       if (typeinfo.defined & NTA_HASTYPE)
2651         {
2652           as_bad (_("can't redefine the type of a register alias"));
2653           return FALSE;
2654         }
2655
2656       typeinfo.defined |= NTA_HASTYPE;
2657       if (ntype.elems != 1)
2658         {
2659           as_bad (_("you must specify a single type only"));
2660           return FALSE;
2661         }
2662       typeinfo.eltype = ntype.el[0];
2663     }
2664
2665   if (skip_past_char (&p, '[') == SUCCESS)
2666     {
2667       expressionS exp;
2668       /* We got a scalar index.  */
2669
2670       if (typeinfo.defined & NTA_HASINDEX)
2671         {
2672           as_bad (_("can't redefine the index of a scalar alias"));
2673           return FALSE;
2674         }
2675
2676       my_get_expression (&exp, &p, GE_NO_PREFIX);
2677
2678       if (exp.X_op != O_constant)
2679         {
2680           as_bad (_("scalar index must be constant"));
2681           return FALSE;
2682         }
2683
2684       typeinfo.defined |= NTA_HASINDEX;
2685       typeinfo.index = exp.X_add_number;
2686
2687       if (skip_past_char (&p, ']') == FAIL)
2688         {
2689           as_bad (_("expecting ]"));
2690           return FALSE;
2691         }
2692     }
2693
2694   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2695      the desired alias name, and p points to its end.  If not, then
2696      the desired alias name is in the global original_case_string.  */
2697 #ifdef TC_CASE_SENSITIVE
2698   namelen = nameend - newname;
2699 #else
2700   newname = original_case_string;
2701   namelen = strlen (newname);
2702 #endif
2703
2704   namebuf = xmemdup0 (newname, namelen);
2705
2706   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2707                          typeinfo.defined != 0 ? &typeinfo : NULL);
2708
2709   /* Insert name in all uppercase.  */
2710   for (p = namebuf; *p; p++)
2711     *p = TOUPPER (*p);
2712
2713   if (strncmp (namebuf, newname, namelen))
2714     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2715                            typeinfo.defined != 0 ? &typeinfo : NULL);
2716
2717   /* Insert name in all lowercase.  */
2718   for (p = namebuf; *p; p++)
2719     *p = TOLOWER (*p);
2720
2721   if (strncmp (namebuf, newname, namelen))
2722     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2723                            typeinfo.defined != 0 ? &typeinfo : NULL);
2724
2725   free (namebuf);
2726   return TRUE;
2727 }
2728
2729 /* Should never be called, as .req goes between the alias and the
2730    register name, not at the beginning of the line.  */
2731
2732 static void
2733 s_req (int a ATTRIBUTE_UNUSED)
2734 {
2735   as_bad (_("invalid syntax for .req directive"));
2736 }
2737
2738 static void
2739 s_dn (int a ATTRIBUTE_UNUSED)
2740 {
2741   as_bad (_("invalid syntax for .dn directive"));
2742 }
2743
2744 static void
2745 s_qn (int a ATTRIBUTE_UNUSED)
2746 {
2747   as_bad (_("invalid syntax for .qn directive"));
2748 }
2749
2750 /* The .unreq directive deletes an alias which was previously defined
2751    by .req.  For example:
2752
2753        my_alias .req r11
2754        .unreq my_alias    */
2755
2756 static void
2757 s_unreq (int a ATTRIBUTE_UNUSED)
2758 {
2759   char * name;
2760   char saved_char;
2761
2762   name = input_line_pointer;
2763
2764   while (*input_line_pointer != 0
2765          && *input_line_pointer != ' '
2766          && *input_line_pointer != '\n')
2767     ++input_line_pointer;
2768
2769   saved_char = *input_line_pointer;
2770   *input_line_pointer = 0;
2771
2772   if (!*name)
2773     as_bad (_("invalid syntax for .unreq directive"));
2774   else
2775     {
2776       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2777                                                               name);
2778
2779       if (!reg)
2780         as_bad (_("unknown register alias '%s'"), name);
2781       else if (reg->builtin)
2782         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2783                  name);
2784       else
2785         {
2786           char * p;
2787           char * nbuf;
2788
2789           hash_delete (arm_reg_hsh, name, FALSE);
2790           free ((char *) reg->name);
2791           if (reg->neon)
2792             free (reg->neon);
2793           free (reg);
2794
2795           /* Also locate the all upper case and all lower case versions.
2796              Do not complain if we cannot find one or the other as it
2797              was probably deleted above.  */
2798
2799           nbuf = strdup (name);
2800           for (p = nbuf; *p; p++)
2801             *p = TOUPPER (*p);
2802           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2803           if (reg)
2804             {
2805               hash_delete (arm_reg_hsh, nbuf, FALSE);
2806               free ((char *) reg->name);
2807               if (reg->neon)
2808                 free (reg->neon);
2809               free (reg);
2810             }
2811
2812           for (p = nbuf; *p; p++)
2813             *p = TOLOWER (*p);
2814           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2815           if (reg)
2816             {
2817               hash_delete (arm_reg_hsh, nbuf, FALSE);
2818               free ((char *) reg->name);
2819               if (reg->neon)
2820                 free (reg->neon);
2821               free (reg);
2822             }
2823
2824           free (nbuf);
2825         }
2826     }
2827
2828   *input_line_pointer = saved_char;
2829   demand_empty_rest_of_line ();
2830 }
2831
2832 /* Directives: Instruction set selection.  */
2833
2834 #ifdef OBJ_ELF
2835 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2836    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2837    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2838    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2839
2840 /* Create a new mapping symbol for the transition to STATE.  */
2841
2842 static void
2843 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2844 {
2845   symbolS * symbolP;
2846   const char * symname;
2847   int type;
2848
2849   switch (state)
2850     {
2851     case MAP_DATA:
2852       symname = "$d";
2853       type = BSF_NO_FLAGS;
2854       break;
2855     case MAP_ARM:
2856       symname = "$a";
2857       type = BSF_NO_FLAGS;
2858       break;
2859     case MAP_THUMB:
2860       symname = "$t";
2861       type = BSF_NO_FLAGS;
2862       break;
2863     default:
2864       abort ();
2865     }
2866
2867   symbolP = symbol_new (symname, now_seg, value, frag);
2868   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2869
2870   switch (state)
2871     {
2872     case MAP_ARM:
2873       THUMB_SET_FUNC (symbolP, 0);
2874       ARM_SET_THUMB (symbolP, 0);
2875       ARM_SET_INTERWORK (symbolP, support_interwork);
2876       break;
2877
2878     case MAP_THUMB:
2879       THUMB_SET_FUNC (symbolP, 1);
2880       ARM_SET_THUMB (symbolP, 1);
2881       ARM_SET_INTERWORK (symbolP, support_interwork);
2882       break;
2883
2884     case MAP_DATA:
2885     default:
2886       break;
2887     }
2888
2889   /* Save the mapping symbols for future reference.  Also check that
2890      we do not place two mapping symbols at the same offset within a
2891      frag.  We'll handle overlap between frags in
2892      check_mapping_symbols.
2893
2894      If .fill or other data filling directive generates zero sized data,
2895      the mapping symbol for the following code will have the same value
2896      as the one generated for the data filling directive.  In this case,
2897      we replace the old symbol with the new one at the same address.  */
2898   if (value == 0)
2899     {
2900       if (frag->tc_frag_data.first_map != NULL)
2901         {
2902           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2903           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2904         }
2905       frag->tc_frag_data.first_map = symbolP;
2906     }
2907   if (frag->tc_frag_data.last_map != NULL)
2908     {
2909       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2910       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2911         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2912     }
2913   frag->tc_frag_data.last_map = symbolP;
2914 }
2915
2916 /* We must sometimes convert a region marked as code to data during
2917    code alignment, if an odd number of bytes have to be padded.  The
2918    code mapping symbol is pushed to an aligned address.  */
2919
2920 static void
2921 insert_data_mapping_symbol (enum mstate state,
2922                             valueT value, fragS *frag, offsetT bytes)
2923 {
2924   /* If there was already a mapping symbol, remove it.  */
2925   if (frag->tc_frag_data.last_map != NULL
2926       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2927     {
2928       symbolS *symp = frag->tc_frag_data.last_map;
2929
2930       if (value == 0)
2931         {
2932           know (frag->tc_frag_data.first_map == symp);
2933           frag->tc_frag_data.first_map = NULL;
2934         }
2935       frag->tc_frag_data.last_map = NULL;
2936       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2937     }
2938
2939   make_mapping_symbol (MAP_DATA, value, frag);
2940   make_mapping_symbol (state, value + bytes, frag);
2941 }
2942
2943 static void mapping_state_2 (enum mstate state, int max_chars);
2944
2945 /* Set the mapping state to STATE.  Only call this when about to
2946    emit some STATE bytes to the file.  */
2947
2948 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2949 void
2950 mapping_state (enum mstate state)
2951 {
2952   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2953
2954   if (mapstate == state)
2955     /* The mapping symbol has already been emitted.
2956        There is nothing else to do.  */
2957     return;
2958
2959   if (state == MAP_ARM || state == MAP_THUMB)
2960     /*  PR gas/12931
2961         All ARM instructions require 4-byte alignment.
2962         (Almost) all Thumb instructions require 2-byte alignment.
2963
2964         When emitting instructions into any section, mark the section
2965         appropriately.
2966
2967         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2968         but themselves require 2-byte alignment; this applies to some
2969         PC- relative forms.  However, these cases will involve implicit
2970         literal pool generation or an explicit .align >=2, both of
2971         which will cause the section to me marked with sufficient
2972         alignment.  Thus, we don't handle those cases here.  */
2973     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2974
2975   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2976     /* This case will be evaluated later.  */
2977     return;
2978
2979   mapping_state_2 (state, 0);
2980 }
2981
2982 /* Same as mapping_state, but MAX_CHARS bytes have already been
2983    allocated.  Put the mapping symbol that far back.  */
2984
2985 static void
2986 mapping_state_2 (enum mstate state, int max_chars)
2987 {
2988   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2989
2990   if (!SEG_NORMAL (now_seg))
2991     return;
2992
2993   if (mapstate == state)
2994     /* The mapping symbol has already been emitted.
2995        There is nothing else to do.  */
2996     return;
2997
2998   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2999           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
3000     {
3001       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
3002       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
3003
3004       if (add_symbol)
3005         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
3006     }
3007
3008   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
3009   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
3010 }
3011 #undef TRANSITION
3012 #else
3013 #define mapping_state(x) ((void)0)
3014 #define mapping_state_2(x, y) ((void)0)
3015 #endif
3016
3017 /* Find the real, Thumb encoded start of a Thumb function.  */
3018
3019 #ifdef OBJ_COFF
3020 static symbolS *
3021 find_real_start (symbolS * symbolP)
3022 {
3023   char *       real_start;
3024   const char * name = S_GET_NAME (symbolP);
3025   symbolS *    new_target;
3026
3027   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
3028 #define STUB_NAME ".real_start_of"
3029
3030   if (name == NULL)
3031     abort ();
3032
3033   /* The compiler may generate BL instructions to local labels because
3034      it needs to perform a branch to a far away location. These labels
3035      do not have a corresponding ".real_start_of" label.  We check
3036      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
3037      the ".real_start_of" convention for nonlocal branches.  */
3038   if (S_IS_LOCAL (symbolP) || name[0] == '.')
3039     return symbolP;
3040
3041   real_start = concat (STUB_NAME, name, NULL);
3042   new_target = symbol_find (real_start);
3043   free (real_start);
3044
3045   if (new_target == NULL)
3046     {
3047       as_warn (_("Failed to find real start of function: %s\n"), name);
3048       new_target = symbolP;
3049     }
3050
3051   return new_target;
3052 }
3053 #endif
3054
3055 static void
3056 opcode_select (int width)
3057 {
3058   switch (width)
3059     {
3060     case 16:
3061       if (! thumb_mode)
3062         {
3063           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
3064             as_bad (_("selected processor does not support THUMB opcodes"));
3065
3066           thumb_mode = 1;
3067           /* No need to force the alignment, since we will have been
3068              coming from ARM mode, which is word-aligned.  */
3069           record_alignment (now_seg, 1);
3070         }
3071       break;
3072
3073     case 32:
3074       if (thumb_mode)
3075         {
3076           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
3077             as_bad (_("selected processor does not support ARM opcodes"));
3078
3079           thumb_mode = 0;
3080
3081           if (!need_pass_2)
3082             frag_align (2, 0, 0);
3083
3084           record_alignment (now_seg, 1);
3085         }
3086       break;
3087
3088     default:
3089       as_bad (_("invalid instruction size selected (%d)"), width);
3090     }
3091 }
3092
3093 static void
3094 s_arm (int ignore ATTRIBUTE_UNUSED)
3095 {
3096   opcode_select (32);
3097   demand_empty_rest_of_line ();
3098 }
3099
3100 static void
3101 s_thumb (int ignore ATTRIBUTE_UNUSED)
3102 {
3103   opcode_select (16);
3104   demand_empty_rest_of_line ();
3105 }
3106
3107 static void
3108 s_code (int unused ATTRIBUTE_UNUSED)
3109 {
3110   int temp;
3111
3112   temp = get_absolute_expression ();
3113   switch (temp)
3114     {
3115     case 16:
3116     case 32:
3117       opcode_select (temp);
3118       break;
3119
3120     default:
3121       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
3122     }
3123 }
3124
3125 static void
3126 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
3127 {
3128   /* If we are not already in thumb mode go into it, EVEN if
3129      the target processor does not support thumb instructions.
3130      This is used by gcc/config/arm/lib1funcs.asm for example
3131      to compile interworking support functions even if the
3132      target processor should not support interworking.  */
3133   if (! thumb_mode)
3134     {
3135       thumb_mode = 2;
3136       record_alignment (now_seg, 1);
3137     }
3138
3139   demand_empty_rest_of_line ();
3140 }
3141
3142 static void
3143 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
3144 {
3145   s_thumb (0);
3146
3147   /* The following label is the name/address of the start of a Thumb function.
3148      We need to know this for the interworking support.  */
3149   label_is_thumb_function_name = TRUE;
3150 }
3151
3152 /* Perform a .set directive, but also mark the alias as
3153    being a thumb function.  */
3154
3155 static void
3156 s_thumb_set (int equiv)
3157 {
3158   /* XXX the following is a duplicate of the code for s_set() in read.c
3159      We cannot just call that code as we need to get at the symbol that
3160      is created.  */
3161   char *    name;
3162   char      delim;
3163   char *    end_name;
3164   symbolS * symbolP;
3165
3166   /* Especial apologies for the random logic:
3167      This just grew, and could be parsed much more simply!
3168      Dean - in haste.  */
3169   delim     = get_symbol_name (& name);
3170   end_name  = input_line_pointer;
3171   (void) restore_line_pointer (delim);
3172
3173   if (*input_line_pointer != ',')
3174     {
3175       *end_name = 0;
3176       as_bad (_("expected comma after name \"%s\""), name);
3177       *end_name = delim;
3178       ignore_rest_of_line ();
3179       return;
3180     }
3181
3182   input_line_pointer++;
3183   *end_name = 0;
3184
3185   if (name[0] == '.' && name[1] == '\0')
3186     {
3187       /* XXX - this should not happen to .thumb_set.  */
3188       abort ();
3189     }
3190
3191   if ((symbolP = symbol_find (name)) == NULL
3192       && (symbolP = md_undefined_symbol (name)) == NULL)
3193     {
3194 #ifndef NO_LISTING
3195       /* When doing symbol listings, play games with dummy fragments living
3196          outside the normal fragment chain to record the file and line info
3197          for this symbol.  */
3198       if (listing & LISTING_SYMBOLS)
3199         {
3200           extern struct list_info_struct * listing_tail;
3201           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
3202
3203           memset (dummy_frag, 0, sizeof (fragS));
3204           dummy_frag->fr_type = rs_fill;
3205           dummy_frag->line = listing_tail;
3206           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
3207           dummy_frag->fr_symbol = symbolP;
3208         }
3209       else
3210 #endif
3211         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
3212
3213 #ifdef OBJ_COFF
3214       /* "set" symbols are local unless otherwise specified.  */
3215       SF_SET_LOCAL (symbolP);
3216 #endif /* OBJ_COFF  */
3217     }                           /* Make a new symbol.  */
3218
3219   symbol_table_insert (symbolP);
3220
3221   * end_name = delim;
3222
3223   if (equiv
3224       && S_IS_DEFINED (symbolP)
3225       && S_GET_SEGMENT (symbolP) != reg_section)
3226     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
3227
3228   pseudo_set (symbolP);
3229
3230   demand_empty_rest_of_line ();
3231
3232   /* XXX Now we come to the Thumb specific bit of code.  */
3233
3234   THUMB_SET_FUNC (symbolP, 1);
3235   ARM_SET_THUMB (symbolP, 1);
3236 #if defined OBJ_ELF || defined OBJ_COFF
3237   ARM_SET_INTERWORK (symbolP, support_interwork);
3238 #endif
3239 }
3240
3241 /* Directives: Mode selection.  */
3242
3243 /* .syntax [unified|divided] - choose the new unified syntax
3244    (same for Arm and Thumb encoding, modulo slight differences in what
3245    can be represented) or the old divergent syntax for each mode.  */
3246 static void
3247 s_syntax (int unused ATTRIBUTE_UNUSED)
3248 {
3249   char *name, delim;
3250
3251   delim = get_symbol_name (& name);
3252
3253   if (!strcasecmp (name, "unified"))
3254     unified_syntax = TRUE;
3255   else if (!strcasecmp (name, "divided"))
3256     unified_syntax = FALSE;
3257   else
3258     {
3259       as_bad (_("unrecognized syntax mode \"%s\""), name);
3260       return;
3261     }
3262   (void) restore_line_pointer (delim);
3263   demand_empty_rest_of_line ();
3264 }
3265
3266 /* Directives: sectioning and alignment.  */
3267
3268 static void
3269 s_bss (int ignore ATTRIBUTE_UNUSED)
3270 {
3271   /* We don't support putting frags in the BSS segment, we fake it by
3272      marking in_bss, then looking at s_skip for clues.  */
3273   subseg_set (bss_section, 0);
3274   demand_empty_rest_of_line ();
3275
3276 #ifdef md_elf_section_change_hook
3277   md_elf_section_change_hook ();
3278 #endif
3279 }
3280
3281 static void
3282 s_even (int ignore ATTRIBUTE_UNUSED)
3283 {
3284   /* Never make frag if expect extra pass.  */
3285   if (!need_pass_2)
3286     frag_align (1, 0, 0);
3287
3288   record_alignment (now_seg, 1);
3289
3290   demand_empty_rest_of_line ();
3291 }
3292
3293 /* Directives: CodeComposer Studio.  */
3294
3295 /*  .ref  (for CodeComposer Studio syntax only).  */
3296 static void
3297 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3298 {
3299   if (codecomposer_syntax)
3300     ignore_rest_of_line ();
3301   else
3302     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3303 }
3304
3305 /*  If name is not NULL, then it is used for marking the beginning of a
3306     function, whereas if it is NULL then it means the function end.  */
3307 static void
3308 asmfunc_debug (const char * name)
3309 {
3310   static const char * last_name = NULL;
3311
3312   if (name != NULL)
3313     {
3314       gas_assert (last_name == NULL);
3315       last_name = name;
3316
3317       if (debug_type == DEBUG_STABS)
3318          stabs_generate_asm_func (name, name);
3319     }
3320   else
3321     {
3322       gas_assert (last_name != NULL);
3323
3324       if (debug_type == DEBUG_STABS)
3325         stabs_generate_asm_endfunc (last_name, last_name);
3326
3327       last_name = NULL;
3328     }
3329 }
3330
3331 static void
3332 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3333 {
3334   if (codecomposer_syntax)
3335     {
3336       switch (asmfunc_state)
3337         {
3338         case OUTSIDE_ASMFUNC:
3339           asmfunc_state = WAITING_ASMFUNC_NAME;
3340           break;
3341
3342         case WAITING_ASMFUNC_NAME:
3343           as_bad (_(".asmfunc repeated."));
3344           break;
3345
3346         case WAITING_ENDASMFUNC:
3347           as_bad (_(".asmfunc without function."));
3348           break;
3349         }
3350       demand_empty_rest_of_line ();
3351     }
3352   else
3353     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3354 }
3355
3356 static void
3357 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3358 {
3359   if (codecomposer_syntax)
3360     {
3361       switch (asmfunc_state)
3362         {
3363         case OUTSIDE_ASMFUNC:
3364           as_bad (_(".endasmfunc without a .asmfunc."));
3365           break;
3366
3367         case WAITING_ASMFUNC_NAME:
3368           as_bad (_(".endasmfunc without function."));
3369           break;
3370
3371         case WAITING_ENDASMFUNC:
3372           asmfunc_state = OUTSIDE_ASMFUNC;
3373           asmfunc_debug (NULL);
3374           break;
3375         }
3376       demand_empty_rest_of_line ();
3377     }
3378   else
3379     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3380 }
3381
3382 static void
3383 s_ccs_def (int name)
3384 {
3385   if (codecomposer_syntax)
3386     s_globl (name);
3387   else
3388     as_bad (_(".def pseudo-op only available with -mccs flag."));
3389 }
3390
3391 /* Directives: Literal pools.  */
3392
3393 static literal_pool *
3394 find_literal_pool (void)
3395 {
3396   literal_pool * pool;
3397
3398   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3399     {
3400       if (pool->section == now_seg
3401           && pool->sub_section == now_subseg)
3402         break;
3403     }
3404
3405   return pool;
3406 }
3407
3408 static literal_pool *
3409 find_or_make_literal_pool (void)
3410 {
3411   /* Next literal pool ID number.  */
3412   static unsigned int latest_pool_num = 1;
3413   literal_pool *      pool;
3414
3415   pool = find_literal_pool ();
3416
3417   if (pool == NULL)
3418     {
3419       /* Create a new pool.  */
3420       pool = XNEW (literal_pool);
3421       if (! pool)
3422         return NULL;
3423
3424       pool->next_free_entry = 0;
3425       pool->section         = now_seg;
3426       pool->sub_section     = now_subseg;
3427       pool->next            = list_of_pools;
3428       pool->symbol          = NULL;
3429       pool->alignment       = 2;
3430
3431       /* Add it to the list.  */
3432       list_of_pools = pool;
3433     }
3434
3435   /* New pools, and emptied pools, will have a NULL symbol.  */
3436   if (pool->symbol == NULL)
3437     {
3438       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3439                                     (valueT) 0, &zero_address_frag);
3440       pool->id = latest_pool_num ++;
3441     }
3442
3443   /* Done.  */
3444   return pool;
3445 }
3446
3447 /* Add the literal in the global 'inst'
3448    structure to the relevant literal pool.  */
3449
3450 static int
3451 add_to_lit_pool (unsigned int nbytes)
3452 {
3453 #define PADDING_SLOT 0x1
3454 #define LIT_ENTRY_SIZE_MASK 0xFF
3455   literal_pool * pool;
3456   unsigned int entry, pool_size = 0;
3457   bfd_boolean padding_slot_p = FALSE;
3458   unsigned imm1 = 0;
3459   unsigned imm2 = 0;
3460
3461   if (nbytes == 8)
3462     {
3463       imm1 = inst.operands[1].imm;
3464       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3465                : inst.relocs[0].exp.X_unsigned ? 0
3466                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3467       if (target_big_endian)
3468         {
3469           imm1 = imm2;
3470           imm2 = inst.operands[1].imm;
3471         }
3472     }
3473
3474   pool = find_or_make_literal_pool ();
3475
3476   /* Check if this literal value is already in the pool.  */
3477   for (entry = 0; entry < pool->next_free_entry; entry ++)
3478     {
3479       if (nbytes == 4)
3480         {
3481           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3482               && (inst.relocs[0].exp.X_op == O_constant)
3483               && (pool->literals[entry].X_add_number
3484                   == inst.relocs[0].exp.X_add_number)
3485               && (pool->literals[entry].X_md == nbytes)
3486               && (pool->literals[entry].X_unsigned
3487                   == inst.relocs[0].exp.X_unsigned))
3488             break;
3489
3490           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3491               && (inst.relocs[0].exp.X_op == O_symbol)
3492               && (pool->literals[entry].X_add_number
3493                   == inst.relocs[0].exp.X_add_number)
3494               && (pool->literals[entry].X_add_symbol
3495                   == inst.relocs[0].exp.X_add_symbol)
3496               && (pool->literals[entry].X_op_symbol
3497                   == inst.relocs[0].exp.X_op_symbol)
3498               && (pool->literals[entry].X_md == nbytes))
3499             break;
3500         }
3501       else if ((nbytes == 8)
3502                && !(pool_size & 0x7)
3503                && ((entry + 1) != pool->next_free_entry)
3504                && (pool->literals[entry].X_op == O_constant)
3505                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3506                && (pool->literals[entry].X_unsigned
3507                    == inst.relocs[0].exp.X_unsigned)
3508                && (pool->literals[entry + 1].X_op == O_constant)
3509                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3510                && (pool->literals[entry + 1].X_unsigned
3511                    == inst.relocs[0].exp.X_unsigned))
3512         break;
3513
3514       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3515       if (padding_slot_p && (nbytes == 4))
3516         break;
3517
3518       pool_size += 4;
3519     }
3520
3521   /* Do we need to create a new entry?  */
3522   if (entry == pool->next_free_entry)
3523     {
3524       if (entry >= MAX_LITERAL_POOL_SIZE)
3525         {
3526           inst.error = _("literal pool overflow");
3527           return FAIL;
3528         }
3529
3530       if (nbytes == 8)
3531         {
3532           /* For 8-byte entries, we align to an 8-byte boundary,
3533              and split it into two 4-byte entries, because on 32-bit
3534              host, 8-byte constants are treated as big num, thus
3535              saved in "generic_bignum" which will be overwritten
3536              by later assignments.
3537
3538              We also need to make sure there is enough space for
3539              the split.
3540
3541              We also check to make sure the literal operand is a
3542              constant number.  */
3543           if (!(inst.relocs[0].exp.X_op == O_constant
3544                 || inst.relocs[0].exp.X_op == O_big))
3545             {
3546               inst.error = _("invalid type for literal pool");
3547               return FAIL;
3548             }
3549           else if (pool_size & 0x7)
3550             {
3551               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3552                 {
3553                   inst.error = _("literal pool overflow");
3554                   return FAIL;
3555                 }
3556
3557               pool->literals[entry] = inst.relocs[0].exp;
3558               pool->literals[entry].X_op = O_constant;
3559               pool->literals[entry].X_add_number = 0;
3560               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3561               pool->next_free_entry += 1;
3562               pool_size += 4;
3563             }
3564           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3565             {
3566               inst.error = _("literal pool overflow");
3567               return FAIL;
3568             }
3569
3570           pool->literals[entry] = inst.relocs[0].exp;
3571           pool->literals[entry].X_op = O_constant;
3572           pool->literals[entry].X_add_number = imm1;
3573           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3574           pool->literals[entry++].X_md = 4;
3575           pool->literals[entry] = inst.relocs[0].exp;
3576           pool->literals[entry].X_op = O_constant;
3577           pool->literals[entry].X_add_number = imm2;
3578           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3579           pool->literals[entry].X_md = 4;
3580           pool->alignment = 3;
3581           pool->next_free_entry += 1;
3582         }
3583       else
3584         {
3585           pool->literals[entry] = inst.relocs[0].exp;
3586           pool->literals[entry].X_md = 4;
3587         }
3588
3589 #ifdef OBJ_ELF
3590       /* PR ld/12974: Record the location of the first source line to reference
3591          this entry in the literal pool.  If it turns out during linking that the
3592          symbol does not exist we will be able to give an accurate line number for
3593          the (first use of the) missing reference.  */
3594       if (debug_type == DEBUG_DWARF2)
3595         dwarf2_where (pool->locs + entry);
3596 #endif
3597       pool->next_free_entry += 1;
3598     }
3599   else if (padding_slot_p)
3600     {
3601       pool->literals[entry] = inst.relocs[0].exp;
3602       pool->literals[entry].X_md = nbytes;
3603     }
3604
3605   inst.relocs[0].exp.X_op             = O_symbol;
3606   inst.relocs[0].exp.X_add_number = pool_size;
3607   inst.relocs[0].exp.X_add_symbol = pool->symbol;
3608
3609   return SUCCESS;
3610 }
3611
3612 bfd_boolean
3613 tc_start_label_without_colon (void)
3614 {
3615   bfd_boolean ret = TRUE;
3616
3617   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3618     {
3619       const char *label = input_line_pointer;
3620
3621       while (!is_end_of_line[(int) label[-1]])
3622         --label;
3623
3624       if (*label == '.')
3625         {
3626           as_bad (_("Invalid label '%s'"), label);
3627           ret = FALSE;
3628         }
3629
3630       asmfunc_debug (label);
3631
3632       asmfunc_state = WAITING_ENDASMFUNC;
3633     }
3634
3635   return ret;
3636 }
3637
3638 /* Can't use symbol_new here, so have to create a symbol and then at
3639    a later date assign it a value. That's what these functions do.  */
3640
3641 static void
3642 symbol_locate (symbolS *    symbolP,
3643                const char * name,       /* It is copied, the caller can modify.  */
3644                segT         segment,    /* Segment identifier (SEG_<something>).  */
3645                valueT       valu,       /* Symbol value.  */
3646                fragS *      frag)       /* Associated fragment.  */
3647 {
3648   size_t name_length;
3649   char * preserved_copy_of_name;
3650
3651   name_length = strlen (name) + 1;   /* +1 for \0.  */
3652   obstack_grow (&notes, name, name_length);
3653   preserved_copy_of_name = (char *) obstack_finish (&notes);
3654
3655 #ifdef tc_canonicalize_symbol_name
3656   preserved_copy_of_name =
3657     tc_canonicalize_symbol_name (preserved_copy_of_name);
3658 #endif
3659
3660   S_SET_NAME (symbolP, preserved_copy_of_name);
3661
3662   S_SET_SEGMENT (symbolP, segment);
3663   S_SET_VALUE (symbolP, valu);
3664   symbol_clear_list_pointers (symbolP);
3665
3666   symbol_set_frag (symbolP, frag);
3667
3668   /* Link to end of symbol chain.  */
3669   {
3670     extern int symbol_table_frozen;
3671
3672     if (symbol_table_frozen)
3673       abort ();
3674   }
3675
3676   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3677
3678   obj_symbol_new_hook (symbolP);
3679
3680 #ifdef tc_symbol_new_hook
3681   tc_symbol_new_hook (symbolP);
3682 #endif
3683
3684 #ifdef DEBUG_SYMS
3685   verify_symbol_chain (symbol_rootP, symbol_lastP);
3686 #endif /* DEBUG_SYMS  */
3687 }
3688
3689 static void
3690 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3691 {
3692   unsigned int entry;
3693   literal_pool * pool;
3694   char sym_name[20];
3695
3696   pool = find_literal_pool ();
3697   if (pool == NULL
3698       || pool->symbol == NULL
3699       || pool->next_free_entry == 0)
3700     return;
3701
3702   /* Align pool as you have word accesses.
3703      Only make a frag if we have to.  */
3704   if (!need_pass_2)
3705     frag_align (pool->alignment, 0, 0);
3706
3707   record_alignment (now_seg, 2);
3708
3709 #ifdef OBJ_ELF
3710   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3711   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3712 #endif
3713   sprintf (sym_name, "$$lit_\002%x", pool->id);
3714
3715   symbol_locate (pool->symbol, sym_name, now_seg,
3716                  (valueT) frag_now_fix (), frag_now);
3717   symbol_table_insert (pool->symbol);
3718
3719   ARM_SET_THUMB (pool->symbol, thumb_mode);
3720
3721 #if defined OBJ_COFF || defined OBJ_ELF
3722   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3723 #endif
3724
3725   for (entry = 0; entry < pool->next_free_entry; entry ++)
3726     {
3727 #ifdef OBJ_ELF
3728       if (debug_type == DEBUG_DWARF2)
3729         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3730 #endif
3731       /* First output the expression in the instruction to the pool.  */
3732       emit_expr (&(pool->literals[entry]),
3733                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3734     }
3735
3736   /* Mark the pool as empty.  */
3737   pool->next_free_entry = 0;
3738   pool->symbol = NULL;
3739 }
3740
3741 #ifdef OBJ_ELF
3742 /* Forward declarations for functions below, in the MD interface
3743    section.  */
3744 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3745 static valueT create_unwind_entry (int);
3746 static void start_unwind_section (const segT, int);
3747 static void add_unwind_opcode (valueT, int);
3748 static void flush_pending_unwind (void);
3749
3750 /* Directives: Data.  */
3751
3752 static void
3753 s_arm_elf_cons (int nbytes)
3754 {
3755   expressionS exp;
3756
3757 #ifdef md_flush_pending_output
3758   md_flush_pending_output ();
3759 #endif
3760
3761   if (is_it_end_of_statement ())
3762     {
3763       demand_empty_rest_of_line ();
3764       return;
3765     }
3766
3767 #ifdef md_cons_align
3768   md_cons_align (nbytes);
3769 #endif
3770
3771   mapping_state (MAP_DATA);
3772   do
3773     {
3774       int reloc;
3775       char *base = input_line_pointer;
3776
3777       expression (& exp);
3778
3779       if (exp.X_op != O_symbol)
3780         emit_expr (&exp, (unsigned int) nbytes);
3781       else
3782         {
3783           char *before_reloc = input_line_pointer;
3784           reloc = parse_reloc (&input_line_pointer);
3785           if (reloc == -1)
3786             {
3787               as_bad (_("unrecognized relocation suffix"));
3788               ignore_rest_of_line ();
3789               return;
3790             }
3791           else if (reloc == BFD_RELOC_UNUSED)
3792             emit_expr (&exp, (unsigned int) nbytes);
3793           else
3794             {
3795               reloc_howto_type *howto = (reloc_howto_type *)
3796                   bfd_reloc_type_lookup (stdoutput,
3797                                          (bfd_reloc_code_real_type) reloc);
3798               int size = bfd_get_reloc_size (howto);
3799
3800               if (reloc == BFD_RELOC_ARM_PLT32)
3801                 {
3802                   as_bad (_("(plt) is only valid on branch targets"));
3803                   reloc = BFD_RELOC_UNUSED;
3804                   size = 0;
3805                 }
3806
3807               if (size > nbytes)
3808                 as_bad (ngettext ("%s relocations do not fit in %d byte",
3809                                   "%s relocations do not fit in %d bytes",
3810                                   nbytes),
3811                         howto->name, nbytes);
3812               else
3813                 {
3814                   /* We've parsed an expression stopping at O_symbol.
3815                      But there may be more expression left now that we
3816                      have parsed the relocation marker.  Parse it again.
3817                      XXX Surely there is a cleaner way to do this.  */
3818                   char *p = input_line_pointer;
3819                   int offset;
3820                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3821
3822                   memcpy (save_buf, base, input_line_pointer - base);
3823                   memmove (base + (input_line_pointer - before_reloc),
3824                            base, before_reloc - base);
3825
3826                   input_line_pointer = base + (input_line_pointer-before_reloc);
3827                   expression (&exp);
3828                   memcpy (base, save_buf, p - base);
3829
3830                   offset = nbytes - size;
3831                   p = frag_more (nbytes);
3832                   memset (p, 0, nbytes);
3833                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3834                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3835                   free (save_buf);
3836                 }
3837             }
3838         }
3839     }
3840   while (*input_line_pointer++ == ',');
3841
3842   /* Put terminator back into stream.  */
3843   input_line_pointer --;
3844   demand_empty_rest_of_line ();
3845 }
3846
3847 /* Emit an expression containing a 32-bit thumb instruction.
3848    Implementation based on put_thumb32_insn.  */
3849
3850 static void
3851 emit_thumb32_expr (expressionS * exp)
3852 {
3853   expressionS exp_high = *exp;
3854
3855   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3856   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3857   exp->X_add_number &= 0xffff;
3858   emit_expr (exp, (unsigned int) THUMB_SIZE);
3859 }
3860
3861 /*  Guess the instruction size based on the opcode.  */
3862
3863 static int
3864 thumb_insn_size (int opcode)
3865 {
3866   if ((unsigned int) opcode < 0xe800u)
3867     return 2;
3868   else if ((unsigned int) opcode >= 0xe8000000u)
3869     return 4;
3870   else
3871     return 0;
3872 }
3873
3874 static bfd_boolean
3875 emit_insn (expressionS *exp, int nbytes)
3876 {
3877   int size = 0;
3878
3879   if (exp->X_op == O_constant)
3880     {
3881       size = nbytes;
3882
3883       if (size == 0)
3884         size = thumb_insn_size (exp->X_add_number);
3885
3886       if (size != 0)
3887         {
3888           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3889             {
3890               as_bad (_(".inst.n operand too big. "\
3891                         "Use .inst.w instead"));
3892               size = 0;
3893             }
3894           else
3895             {
3896               if (now_pred.state == AUTOMATIC_PRED_BLOCK)
3897                 set_pred_insn_type_nonvoid (OUTSIDE_PRED_INSN, 0);
3898               else
3899                 set_pred_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3900
3901               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3902                 emit_thumb32_expr (exp);
3903               else
3904                 emit_expr (exp, (unsigned int) size);
3905
3906               it_fsm_post_encode ();
3907             }
3908         }
3909       else
3910         as_bad (_("cannot determine Thumb instruction size. "   \
3911                   "Use .inst.n/.inst.w instead"));
3912     }
3913   else
3914     as_bad (_("constant expression required"));
3915
3916   return (size != 0);
3917 }
3918
3919 /* Like s_arm_elf_cons but do not use md_cons_align and
3920    set the mapping state to MAP_ARM/MAP_THUMB.  */
3921
3922 static void
3923 s_arm_elf_inst (int nbytes)
3924 {
3925   if (is_it_end_of_statement ())
3926     {
3927       demand_empty_rest_of_line ();
3928       return;
3929     }
3930
3931   /* Calling mapping_state () here will not change ARM/THUMB,
3932      but will ensure not to be in DATA state.  */
3933
3934   if (thumb_mode)
3935     mapping_state (MAP_THUMB);
3936   else
3937     {
3938       if (nbytes != 0)
3939         {
3940           as_bad (_("width suffixes are invalid in ARM mode"));
3941           ignore_rest_of_line ();
3942           return;
3943         }
3944
3945       nbytes = 4;
3946
3947       mapping_state (MAP_ARM);
3948     }
3949
3950   do
3951     {
3952       expressionS exp;
3953
3954       expression (& exp);
3955
3956       if (! emit_insn (& exp, nbytes))
3957         {
3958           ignore_rest_of_line ();
3959           return;
3960         }
3961     }
3962   while (*input_line_pointer++ == ',');
3963
3964   /* Put terminator back into stream.  */
3965   input_line_pointer --;
3966   demand_empty_rest_of_line ();
3967 }
3968
3969 /* Parse a .rel31 directive.  */
3970
3971 static void
3972 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3973 {
3974   expressionS exp;
3975   char *p;
3976   valueT highbit;
3977
3978   highbit = 0;
3979   if (*input_line_pointer == '1')
3980     highbit = 0x80000000;
3981   else if (*input_line_pointer != '0')
3982     as_bad (_("expected 0 or 1"));
3983
3984   input_line_pointer++;
3985   if (*input_line_pointer != ',')
3986     as_bad (_("missing comma"));
3987   input_line_pointer++;
3988
3989 #ifdef md_flush_pending_output
3990   md_flush_pending_output ();
3991 #endif
3992
3993 #ifdef md_cons_align
3994   md_cons_align (4);
3995 #endif
3996
3997   mapping_state (MAP_DATA);
3998
3999   expression (&exp);
4000
4001   p = frag_more (4);
4002   md_number_to_chars (p, highbit, 4);
4003   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
4004                BFD_RELOC_ARM_PREL31);
4005
4006   demand_empty_rest_of_line ();
4007 }
4008
4009 /* Directives: AEABI stack-unwind tables.  */
4010
4011 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
4012
4013 static void
4014 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
4015 {
4016   demand_empty_rest_of_line ();
4017   if (unwind.proc_start)
4018     {
4019       as_bad (_("duplicate .fnstart directive"));
4020       return;
4021     }
4022
4023   /* Mark the start of the function.  */
4024   unwind.proc_start = expr_build_dot ();
4025
4026   /* Reset the rest of the unwind info.  */
4027   unwind.opcode_count = 0;
4028   unwind.table_entry = NULL;
4029   unwind.personality_routine = NULL;
4030   unwind.personality_index = -1;
4031   unwind.frame_size = 0;
4032   unwind.fp_offset = 0;
4033   unwind.fp_reg = REG_SP;
4034   unwind.fp_used = 0;
4035   unwind.sp_restored = 0;
4036 }
4037
4038
4039 /* Parse a handlerdata directive.  Creates the exception handling table entry
4040    for the function.  */
4041
4042 static void
4043 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
4044 {
4045   demand_empty_rest_of_line ();
4046   if (!unwind.proc_start)
4047     as_bad (MISSING_FNSTART);
4048
4049   if (unwind.table_entry)
4050     as_bad (_("duplicate .handlerdata directive"));
4051
4052   create_unwind_entry (1);
4053 }
4054
4055 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
4056
4057 static void
4058 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
4059 {
4060   long where;
4061   char *ptr;
4062   valueT val;
4063   unsigned int marked_pr_dependency;
4064
4065   demand_empty_rest_of_line ();
4066
4067   if (!unwind.proc_start)
4068     {
4069       as_bad (_(".fnend directive without .fnstart"));
4070       return;
4071     }
4072
4073   /* Add eh table entry.  */
4074   if (unwind.table_entry == NULL)
4075     val = create_unwind_entry (0);
4076   else
4077     val = 0;
4078
4079   /* Add index table entry.  This is two words.  */
4080   start_unwind_section (unwind.saved_seg, 1);
4081   frag_align (2, 0, 0);
4082   record_alignment (now_seg, 2);
4083
4084   ptr = frag_more (8);
4085   memset (ptr, 0, 8);
4086   where = frag_now_fix () - 8;
4087
4088   /* Self relative offset of the function start.  */
4089   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
4090            BFD_RELOC_ARM_PREL31);
4091
4092   /* Indicate dependency on EHABI-defined personality routines to the
4093      linker, if it hasn't been done already.  */
4094   marked_pr_dependency
4095     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
4096   if (unwind.personality_index >= 0 && unwind.personality_index < 3
4097       && !(marked_pr_dependency & (1 << unwind.personality_index)))
4098     {
4099       static const char *const name[] =
4100         {
4101           "__aeabi_unwind_cpp_pr0",
4102           "__aeabi_unwind_cpp_pr1",
4103           "__aeabi_unwind_cpp_pr2"
4104         };
4105       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
4106       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
4107       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
4108         |= 1 << unwind.personality_index;
4109     }
4110
4111   if (val)
4112     /* Inline exception table entry.  */
4113     md_number_to_chars (ptr + 4, val, 4);
4114   else
4115     /* Self relative offset of the table entry.  */
4116     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
4117              BFD_RELOC_ARM_PREL31);
4118
4119   /* Restore the original section.  */
4120   subseg_set (unwind.saved_seg, unwind.saved_subseg);
4121
4122   unwind.proc_start = NULL;
4123 }
4124
4125
4126 /* Parse an unwind_cantunwind directive.  */
4127
4128 static void
4129 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
4130 {
4131   demand_empty_rest_of_line ();
4132   if (!unwind.proc_start)
4133     as_bad (MISSING_FNSTART);
4134
4135   if (unwind.personality_routine || unwind.personality_index != -1)
4136     as_bad (_("personality routine specified for cantunwind frame"));
4137
4138   unwind.personality_index = -2;
4139 }
4140
4141
4142 /* Parse a personalityindex directive.  */
4143
4144 static void
4145 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
4146 {
4147   expressionS exp;
4148
4149   if (!unwind.proc_start)
4150     as_bad (MISSING_FNSTART);
4151
4152   if (unwind.personality_routine || unwind.personality_index != -1)
4153     as_bad (_("duplicate .personalityindex directive"));
4154
4155   expression (&exp);
4156
4157   if (exp.X_op != O_constant
4158       || exp.X_add_number < 0 || exp.X_add_number > 15)
4159     {
4160       as_bad (_("bad personality routine number"));
4161       ignore_rest_of_line ();
4162       return;
4163     }
4164
4165   unwind.personality_index = exp.X_add_number;
4166
4167   demand_empty_rest_of_line ();
4168 }
4169
4170
4171 /* Parse a personality directive.  */
4172
4173 static void
4174 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
4175 {
4176   char *name, *p, c;
4177
4178   if (!unwind.proc_start)
4179     as_bad (MISSING_FNSTART);
4180
4181   if (unwind.personality_routine || unwind.personality_index != -1)
4182     as_bad (_("duplicate .personality directive"));
4183
4184   c = get_symbol_name (& name);
4185   p = input_line_pointer;
4186   if (c == '"')
4187     ++ input_line_pointer;
4188   unwind.personality_routine = symbol_find_or_make (name);
4189   *p = c;
4190   demand_empty_rest_of_line ();
4191 }
4192
4193
4194 /* Parse a directive saving core registers.  */
4195
4196 static void
4197 s_arm_unwind_save_core (void)
4198 {
4199   valueT op;
4200   long range;
4201   int n;
4202
4203   range = parse_reg_list (&input_line_pointer, REGLIST_RN);
4204   if (range == FAIL)
4205     {
4206       as_bad (_("expected register list"));
4207       ignore_rest_of_line ();
4208       return;
4209     }
4210
4211   demand_empty_rest_of_line ();
4212
4213   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
4214      into .unwind_save {..., sp...}.  We aren't bothered about the value of
4215      ip because it is clobbered by calls.  */
4216   if (unwind.sp_restored && unwind.fp_reg == 12
4217       && (range & 0x3000) == 0x1000)
4218     {
4219       unwind.opcode_count--;
4220       unwind.sp_restored = 0;
4221       range = (range | 0x2000) & ~0x1000;
4222       unwind.pending_offset = 0;
4223     }
4224
4225   /* Pop r4-r15.  */
4226   if (range & 0xfff0)
4227     {
4228       /* See if we can use the short opcodes.  These pop a block of up to 8
4229          registers starting with r4, plus maybe r14.  */
4230       for (n = 0; n < 8; n++)
4231         {
4232           /* Break at the first non-saved register.      */
4233           if ((range & (1 << (n + 4))) == 0)
4234             break;
4235         }
4236       /* See if there are any other bits set.  */
4237       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
4238         {
4239           /* Use the long form.  */
4240           op = 0x8000 | ((range >> 4) & 0xfff);
4241           add_unwind_opcode (op, 2);
4242         }
4243       else
4244         {
4245           /* Use the short form.  */
4246           if (range & 0x4000)
4247             op = 0xa8; /* Pop r14.      */
4248           else
4249             op = 0xa0; /* Do not pop r14.  */
4250           op |= (n - 1);
4251           add_unwind_opcode (op, 1);
4252         }
4253     }
4254
4255   /* Pop r0-r3.  */
4256   if (range & 0xf)
4257     {
4258       op = 0xb100 | (range & 0xf);
4259       add_unwind_opcode (op, 2);
4260     }
4261
4262   /* Record the number of bytes pushed.  */
4263   for (n = 0; n < 16; n++)
4264     {
4265       if (range & (1 << n))
4266         unwind.frame_size += 4;
4267     }
4268 }
4269
4270
4271 /* Parse a directive saving FPA registers.  */
4272
4273 static void
4274 s_arm_unwind_save_fpa (int reg)
4275 {
4276   expressionS exp;
4277   int num_regs;
4278   valueT op;
4279
4280   /* Get Number of registers to transfer.  */
4281   if (skip_past_comma (&input_line_pointer) != FAIL)
4282     expression (&exp);
4283   else
4284     exp.X_op = O_illegal;
4285
4286   if (exp.X_op != O_constant)
4287     {
4288       as_bad (_("expected , <constant>"));
4289       ignore_rest_of_line ();
4290       return;
4291     }
4292
4293   num_regs = exp.X_add_number;
4294
4295   if (num_regs < 1 || num_regs > 4)
4296     {
4297       as_bad (_("number of registers must be in the range [1:4]"));
4298       ignore_rest_of_line ();
4299       return;
4300     }
4301
4302   demand_empty_rest_of_line ();
4303
4304   if (reg == 4)
4305     {
4306       /* Short form.  */
4307       op = 0xb4 | (num_regs - 1);
4308       add_unwind_opcode (op, 1);
4309     }
4310   else
4311     {
4312       /* Long form.  */
4313       op = 0xc800 | (reg << 4) | (num_regs - 1);
4314       add_unwind_opcode (op, 2);
4315     }
4316   unwind.frame_size += num_regs * 12;
4317 }
4318
4319
4320 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4321
4322 static void
4323 s_arm_unwind_save_vfp_armv6 (void)
4324 {
4325   int count;
4326   unsigned int start;
4327   valueT op;
4328   int num_vfpv3_regs = 0;
4329   int num_regs_below_16;
4330   bfd_boolean partial_match;
4331
4332   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D,
4333                               &partial_match);
4334   if (count == FAIL)
4335     {
4336       as_bad (_("expected register list"));
4337       ignore_rest_of_line ();
4338       return;
4339     }
4340
4341   demand_empty_rest_of_line ();
4342
4343   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4344      than FSTMX/FLDMX-style ones).  */
4345
4346   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4347   if (start >= 16)
4348     num_vfpv3_regs = count;
4349   else if (start + count > 16)
4350     num_vfpv3_regs = start + count - 16;
4351
4352   if (num_vfpv3_regs > 0)
4353     {
4354       int start_offset = start > 16 ? start - 16 : 0;
4355       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4356       add_unwind_opcode (op, 2);
4357     }
4358
4359   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4360   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4361   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4362   if (num_regs_below_16 > 0)
4363     {
4364       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4365       add_unwind_opcode (op, 2);
4366     }
4367
4368   unwind.frame_size += count * 8;
4369 }
4370
4371
4372 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4373
4374 static void
4375 s_arm_unwind_save_vfp (void)
4376 {
4377   int count;
4378   unsigned int reg;
4379   valueT op;
4380   bfd_boolean partial_match;
4381
4382   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D,
4383                               &partial_match);
4384   if (count == FAIL)
4385     {
4386       as_bad (_("expected register list"));
4387       ignore_rest_of_line ();
4388       return;
4389     }
4390
4391   demand_empty_rest_of_line ();
4392
4393   if (reg == 8)
4394     {
4395       /* Short form.  */
4396       op = 0xb8 | (count - 1);
4397       add_unwind_opcode (op, 1);
4398     }
4399   else
4400     {
4401       /* Long form.  */
4402       op = 0xb300 | (reg << 4) | (count - 1);
4403       add_unwind_opcode (op, 2);
4404     }
4405   unwind.frame_size += count * 8 + 4;
4406 }
4407
4408
4409 /* Parse a directive saving iWMMXt data registers.  */
4410
4411 static void
4412 s_arm_unwind_save_mmxwr (void)
4413 {
4414   int reg;
4415   int hi_reg;
4416   int i;
4417   unsigned mask = 0;
4418   valueT op;
4419
4420   if (*input_line_pointer == '{')
4421     input_line_pointer++;
4422
4423   do
4424     {
4425       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4426
4427       if (reg == FAIL)
4428         {
4429           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4430           goto error;
4431         }
4432
4433       if (mask >> reg)
4434         as_tsktsk (_("register list not in ascending order"));
4435       mask |= 1 << reg;
4436
4437       if (*input_line_pointer == '-')
4438         {
4439           input_line_pointer++;
4440           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4441           if (hi_reg == FAIL)
4442             {
4443               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4444               goto error;
4445             }
4446           else if (reg >= hi_reg)
4447             {
4448               as_bad (_("bad register range"));
4449               goto error;
4450             }
4451           for (; reg < hi_reg; reg++)
4452             mask |= 1 << reg;
4453         }
4454     }
4455   while (skip_past_comma (&input_line_pointer) != FAIL);
4456
4457   skip_past_char (&input_line_pointer, '}');
4458
4459   demand_empty_rest_of_line ();
4460
4461   /* Generate any deferred opcodes because we're going to be looking at
4462      the list.  */
4463   flush_pending_unwind ();
4464
4465   for (i = 0; i < 16; i++)
4466     {
4467       if (mask & (1 << i))
4468         unwind.frame_size += 8;
4469     }
4470
4471   /* Attempt to combine with a previous opcode.  We do this because gcc
4472      likes to output separate unwind directives for a single block of
4473      registers.  */
4474   if (unwind.opcode_count > 0)
4475     {
4476       i = unwind.opcodes[unwind.opcode_count - 1];
4477       if ((i & 0xf8) == 0xc0)
4478         {
4479           i &= 7;
4480           /* Only merge if the blocks are contiguous.  */
4481           if (i < 6)
4482             {
4483               if ((mask & 0xfe00) == (1 << 9))
4484                 {
4485                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4486                   unwind.opcode_count--;
4487                 }
4488             }
4489           else if (i == 6 && unwind.opcode_count >= 2)
4490             {
4491               i = unwind.opcodes[unwind.opcode_count - 2];
4492               reg = i >> 4;
4493               i &= 0xf;
4494
4495               op = 0xffff << (reg - 1);
4496               if (reg > 0
4497                   && ((mask & op) == (1u << (reg - 1))))
4498                 {
4499                   op = (1 << (reg + i + 1)) - 1;
4500                   op &= ~((1 << reg) - 1);
4501                   mask |= op;
4502                   unwind.opcode_count -= 2;
4503                 }
4504             }
4505         }
4506     }
4507
4508   hi_reg = 15;
4509   /* We want to generate opcodes in the order the registers have been
4510      saved, ie. descending order.  */
4511   for (reg = 15; reg >= -1; reg--)
4512     {
4513       /* Save registers in blocks.  */
4514       if (reg < 0
4515           || !(mask & (1 << reg)))
4516         {
4517           /* We found an unsaved reg.  Generate opcodes to save the
4518              preceding block.   */
4519           if (reg != hi_reg)
4520             {
4521               if (reg == 9)
4522                 {
4523                   /* Short form.  */
4524                   op = 0xc0 | (hi_reg - 10);
4525                   add_unwind_opcode (op, 1);
4526                 }
4527               else
4528                 {
4529                   /* Long form.  */
4530                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4531                   add_unwind_opcode (op, 2);
4532                 }
4533             }
4534           hi_reg = reg - 1;
4535         }
4536     }
4537
4538   return;
4539 error:
4540   ignore_rest_of_line ();
4541 }
4542
4543 static void
4544 s_arm_unwind_save_mmxwcg (void)
4545 {
4546   int reg;
4547   int hi_reg;
4548   unsigned mask = 0;
4549   valueT op;
4550
4551   if (*input_line_pointer == '{')
4552     input_line_pointer++;
4553
4554   skip_whitespace (input_line_pointer);
4555
4556   do
4557     {
4558       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4559
4560       if (reg == FAIL)
4561         {
4562           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4563           goto error;
4564         }
4565
4566       reg -= 8;
4567       if (mask >> reg)
4568         as_tsktsk (_("register list not in ascending order"));
4569       mask |= 1 << reg;
4570
4571       if (*input_line_pointer == '-')
4572         {
4573           input_line_pointer++;
4574           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4575           if (hi_reg == FAIL)
4576             {
4577               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4578               goto error;
4579             }
4580           else if (reg >= hi_reg)
4581             {
4582               as_bad (_("bad register range"));
4583               goto error;
4584             }
4585           for (; reg < hi_reg; reg++)
4586             mask |= 1 << reg;
4587         }
4588     }
4589   while (skip_past_comma (&input_line_pointer) != FAIL);
4590
4591   skip_past_char (&input_line_pointer, '}');
4592
4593   demand_empty_rest_of_line ();
4594
4595   /* Generate any deferred opcodes because we're going to be looking at
4596      the list.  */
4597   flush_pending_unwind ();
4598
4599   for (reg = 0; reg < 16; reg++)
4600     {
4601       if (mask & (1 << reg))
4602         unwind.frame_size += 4;
4603     }
4604   op = 0xc700 | mask;
4605   add_unwind_opcode (op, 2);
4606   return;
4607 error:
4608   ignore_rest_of_line ();
4609 }
4610
4611
4612 /* Parse an unwind_save directive.
4613    If the argument is non-zero, this is a .vsave directive.  */
4614
4615 static void
4616 s_arm_unwind_save (int arch_v6)
4617 {
4618   char *peek;
4619   struct reg_entry *reg;
4620   bfd_boolean had_brace = FALSE;
4621
4622   if (!unwind.proc_start)
4623     as_bad (MISSING_FNSTART);
4624
4625   /* Figure out what sort of save we have.  */
4626   peek = input_line_pointer;
4627
4628   if (*peek == '{')
4629     {
4630       had_brace = TRUE;
4631       peek++;
4632     }
4633
4634   reg = arm_reg_parse_multi (&peek);
4635
4636   if (!reg)
4637     {
4638       as_bad (_("register expected"));
4639       ignore_rest_of_line ();
4640       return;
4641     }
4642
4643   switch (reg->type)
4644     {
4645     case REG_TYPE_FN:
4646       if (had_brace)
4647         {
4648           as_bad (_("FPA .unwind_save does not take a register list"));
4649           ignore_rest_of_line ();
4650           return;
4651         }
4652       input_line_pointer = peek;
4653       s_arm_unwind_save_fpa (reg->number);
4654       return;
4655
4656     case REG_TYPE_RN:
4657       s_arm_unwind_save_core ();
4658       return;
4659
4660     case REG_TYPE_VFD:
4661       if (arch_v6)
4662         s_arm_unwind_save_vfp_armv6 ();
4663       else
4664         s_arm_unwind_save_vfp ();
4665       return;
4666
4667     case REG_TYPE_MMXWR:
4668       s_arm_unwind_save_mmxwr ();
4669       return;
4670
4671     case REG_TYPE_MMXWCG:
4672       s_arm_unwind_save_mmxwcg ();
4673       return;
4674
4675     default:
4676       as_bad (_(".unwind_save does not support this kind of register"));
4677       ignore_rest_of_line ();
4678     }
4679 }
4680
4681
4682 /* Parse an unwind_movsp directive.  */
4683
4684 static void
4685 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4686 {
4687   int reg;
4688   valueT op;
4689   int offset;
4690
4691   if (!unwind.proc_start)
4692     as_bad (MISSING_FNSTART);
4693
4694   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4695   if (reg == FAIL)
4696     {
4697       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4698       ignore_rest_of_line ();
4699       return;
4700     }
4701
4702   /* Optional constant.  */
4703   if (skip_past_comma (&input_line_pointer) != FAIL)
4704     {
4705       if (immediate_for_directive (&offset) == FAIL)
4706         return;
4707     }
4708   else
4709     offset = 0;
4710
4711   demand_empty_rest_of_line ();
4712
4713   if (reg == REG_SP || reg == REG_PC)
4714     {
4715       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4716       return;
4717     }
4718
4719   if (unwind.fp_reg != REG_SP)
4720     as_bad (_("unexpected .unwind_movsp directive"));
4721
4722   /* Generate opcode to restore the value.  */
4723   op = 0x90 | reg;
4724   add_unwind_opcode (op, 1);
4725
4726   /* Record the information for later.  */
4727   unwind.fp_reg = reg;
4728   unwind.fp_offset = unwind.frame_size - offset;
4729   unwind.sp_restored = 1;
4730 }
4731
4732 /* Parse an unwind_pad directive.  */
4733
4734 static void
4735 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4736 {
4737   int offset;
4738
4739   if (!unwind.proc_start)
4740     as_bad (MISSING_FNSTART);
4741
4742   if (immediate_for_directive (&offset) == FAIL)
4743     return;
4744
4745   if (offset & 3)
4746     {
4747       as_bad (_("stack increment must be multiple of 4"));
4748       ignore_rest_of_line ();
4749       return;
4750     }
4751
4752   /* Don't generate any opcodes, just record the details for later.  */
4753   unwind.frame_size += offset;
4754   unwind.pending_offset += offset;
4755
4756   demand_empty_rest_of_line ();
4757 }
4758
4759 /* Parse an unwind_setfp directive.  */
4760
4761 static void
4762 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4763 {
4764   int sp_reg;
4765   int fp_reg;
4766   int offset;
4767
4768   if (!unwind.proc_start)
4769     as_bad (MISSING_FNSTART);
4770
4771   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4772   if (skip_past_comma (&input_line_pointer) == FAIL)
4773     sp_reg = FAIL;
4774   else
4775     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4776
4777   if (fp_reg == FAIL || sp_reg == FAIL)
4778     {
4779       as_bad (_("expected <reg>, <reg>"));
4780       ignore_rest_of_line ();
4781       return;
4782     }
4783
4784   /* Optional constant.  */
4785   if (skip_past_comma (&input_line_pointer) != FAIL)
4786     {
4787       if (immediate_for_directive (&offset) == FAIL)
4788         return;
4789     }
4790   else
4791     offset = 0;
4792
4793   demand_empty_rest_of_line ();
4794
4795   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4796     {
4797       as_bad (_("register must be either sp or set by a previous"
4798                 "unwind_movsp directive"));
4799       return;
4800     }
4801
4802   /* Don't generate any opcodes, just record the information for later.  */
4803   unwind.fp_reg = fp_reg;
4804   unwind.fp_used = 1;
4805   if (sp_reg == REG_SP)
4806     unwind.fp_offset = unwind.frame_size - offset;
4807   else
4808     unwind.fp_offset -= offset;
4809 }
4810
4811 /* Parse an unwind_raw directive.  */
4812
4813 static void
4814 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4815 {
4816   expressionS exp;
4817   /* This is an arbitrary limit.         */
4818   unsigned char op[16];
4819   int count;
4820
4821   if (!unwind.proc_start)
4822     as_bad (MISSING_FNSTART);
4823
4824   expression (&exp);
4825   if (exp.X_op == O_constant
4826       && skip_past_comma (&input_line_pointer) != FAIL)
4827     {
4828       unwind.frame_size += exp.X_add_number;
4829       expression (&exp);
4830     }
4831   else
4832     exp.X_op = O_illegal;
4833
4834   if (exp.X_op != O_constant)
4835     {
4836       as_bad (_("expected <offset>, <opcode>"));
4837       ignore_rest_of_line ();
4838       return;
4839     }
4840
4841   count = 0;
4842
4843   /* Parse the opcode.  */
4844   for (;;)
4845     {
4846       if (count >= 16)
4847         {
4848           as_bad (_("unwind opcode too long"));
4849           ignore_rest_of_line ();
4850         }
4851       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4852         {
4853           as_bad (_("invalid unwind opcode"));
4854           ignore_rest_of_line ();
4855           return;
4856         }
4857       op[count++] = exp.X_add_number;
4858
4859       /* Parse the next byte.  */
4860       if (skip_past_comma (&input_line_pointer) == FAIL)
4861         break;
4862
4863       expression (&exp);
4864     }
4865
4866   /* Add the opcode bytes in reverse order.  */
4867   while (count--)
4868     add_unwind_opcode (op[count], 1);
4869
4870   demand_empty_rest_of_line ();
4871 }
4872
4873
4874 /* Parse a .eabi_attribute directive.  */
4875
4876 static void
4877 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4878 {
4879   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4880
4881   if (tag >= 0 && tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4882     attributes_set_explicitly[tag] = 1;
4883 }
4884
4885 /* Emit a tls fix for the symbol.  */
4886
4887 static void
4888 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4889 {
4890   char *p;
4891   expressionS exp;
4892 #ifdef md_flush_pending_output
4893   md_flush_pending_output ();
4894 #endif
4895
4896 #ifdef md_cons_align
4897   md_cons_align (4);
4898 #endif
4899
4900   /* Since we're just labelling the code, there's no need to define a
4901      mapping symbol.  */
4902   expression (&exp);
4903   p = obstack_next_free (&frchain_now->frch_obstack);
4904   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4905                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4906                : BFD_RELOC_ARM_TLS_DESCSEQ);
4907 }
4908 #endif /* OBJ_ELF */
4909
4910 static void s_arm_arch (int);
4911 static void s_arm_object_arch (int);
4912 static void s_arm_cpu (int);
4913 static void s_arm_fpu (int);
4914 static void s_arm_arch_extension (int);
4915
4916 #ifdef TE_PE
4917
4918 static void
4919 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4920 {
4921   expressionS exp;
4922
4923   do
4924     {
4925       expression (&exp);
4926       if (exp.X_op == O_symbol)
4927         exp.X_op = O_secrel;
4928
4929       emit_expr (&exp, 4);
4930     }
4931   while (*input_line_pointer++ == ',');
4932
4933   input_line_pointer--;
4934   demand_empty_rest_of_line ();
4935 }
4936 #endif /* TE_PE */
4937
4938 int
4939 arm_is_largest_exponent_ok (int precision)
4940 {
4941   /* precision == 1 ensures that this will only return
4942      true for 16 bit floats.  */
4943   return (precision == 1) && (fp16_format == ARM_FP16_FORMAT_ALTERNATIVE);
4944 }
4945
4946 static void
4947 set_fp16_format (int dummy ATTRIBUTE_UNUSED)
4948 {
4949   char saved_char;
4950   char* name;
4951   enum fp_16bit_format new_format;
4952
4953   new_format = ARM_FP16_FORMAT_DEFAULT;
4954
4955   name = input_line_pointer;
4956   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
4957     input_line_pointer++;
4958
4959   saved_char = *input_line_pointer;
4960   *input_line_pointer = 0;
4961
4962   if (strcasecmp (name, "ieee") == 0)
4963     new_format = ARM_FP16_FORMAT_IEEE;
4964   else if (strcasecmp (name, "alternative") == 0)
4965     new_format = ARM_FP16_FORMAT_ALTERNATIVE;
4966   else
4967     {
4968       as_bad (_("unrecognised float16 format \"%s\""), name);
4969       goto cleanup;
4970     }
4971
4972   /* Only set fp16_format if it is still the default (aka not already
4973      been set yet).  */
4974   if (fp16_format == ARM_FP16_FORMAT_DEFAULT)
4975     fp16_format = new_format;
4976   else
4977     {
4978       if (new_format != fp16_format)
4979         as_warn (_("float16 format cannot be set more than once, ignoring."));
4980     }
4981
4982 cleanup:
4983   *input_line_pointer = saved_char;
4984   ignore_rest_of_line ();
4985 }
4986
4987 /* This table describes all the machine specific pseudo-ops the assembler
4988    has to support.  The fields are:
4989      pseudo-op name without dot
4990      function to call to execute this pseudo-op
4991      Integer arg to pass to the function.  */
4992
4993 const pseudo_typeS md_pseudo_table[] =
4994 {
4995   /* Never called because '.req' does not start a line.  */
4996   { "req",         s_req,         0 },
4997   /* Following two are likewise never called.  */
4998   { "dn",          s_dn,          0 },
4999   { "qn",          s_qn,          0 },
5000   { "unreq",       s_unreq,       0 },
5001   { "bss",         s_bss,         0 },
5002   { "align",       s_align_ptwo,  2 },
5003   { "arm",         s_arm,         0 },
5004   { "thumb",       s_thumb,       0 },
5005   { "code",        s_code,        0 },
5006   { "force_thumb", s_force_thumb, 0 },
5007   { "thumb_func",  s_thumb_func,  0 },
5008   { "thumb_set",   s_thumb_set,   0 },
5009   { "even",        s_even,        0 },
5010   { "ltorg",       s_ltorg,       0 },
5011   { "pool",        s_ltorg,       0 },
5012   { "syntax",      s_syntax,      0 },
5013   { "cpu",         s_arm_cpu,     0 },
5014   { "arch",        s_arm_arch,    0 },
5015   { "object_arch", s_arm_object_arch,   0 },
5016   { "fpu",         s_arm_fpu,     0 },
5017   { "arch_extension", s_arm_arch_extension, 0 },
5018 #ifdef OBJ_ELF
5019   { "word",             s_arm_elf_cons, 4 },
5020   { "long",             s_arm_elf_cons, 4 },
5021   { "inst.n",           s_arm_elf_inst, 2 },
5022   { "inst.w",           s_arm_elf_inst, 4 },
5023   { "inst",             s_arm_elf_inst, 0 },
5024   { "rel31",            s_arm_rel31,      0 },
5025   { "fnstart",          s_arm_unwind_fnstart,   0 },
5026   { "fnend",            s_arm_unwind_fnend,     0 },
5027   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
5028   { "personality",      s_arm_unwind_personality, 0 },
5029   { "personalityindex", s_arm_unwind_personalityindex, 0 },
5030   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
5031   { "save",             s_arm_unwind_save,      0 },
5032   { "vsave",            s_arm_unwind_save,      1 },
5033   { "movsp",            s_arm_unwind_movsp,     0 },
5034   { "pad",              s_arm_unwind_pad,       0 },
5035   { "setfp",            s_arm_unwind_setfp,     0 },
5036   { "unwind_raw",       s_arm_unwind_raw,       0 },
5037   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
5038   { "tlsdescseq",       s_arm_tls_descseq,      0 },
5039 #else
5040   { "word",        cons, 4},
5041
5042   /* These are used for dwarf.  */
5043   {"2byte", cons, 2},
5044   {"4byte", cons, 4},
5045   {"8byte", cons, 8},
5046   /* These are used for dwarf2.  */
5047   { "file", dwarf2_directive_file, 0 },
5048   { "loc",  dwarf2_directive_loc,  0 },
5049   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
5050 #endif
5051   { "extend",      float_cons, 'x' },
5052   { "ldouble",     float_cons, 'x' },
5053   { "packed",      float_cons, 'p' },
5054 #ifdef TE_PE
5055   {"secrel32", pe_directive_secrel, 0},
5056 #endif
5057
5058   /* These are for compatibility with CodeComposer Studio.  */
5059   {"ref",          s_ccs_ref,        0},
5060   {"def",          s_ccs_def,        0},
5061   {"asmfunc",      s_ccs_asmfunc,    0},
5062   {"endasmfunc",   s_ccs_endasmfunc, 0},
5063
5064   {"float16", float_cons, 'h' },
5065   {"float16_format", set_fp16_format, 0 },
5066
5067   { 0, 0, 0 }
5068 };
5069
5070 /* Parser functions used exclusively in instruction operands.  */
5071
5072 /* Generic immediate-value read function for use in insn parsing.
5073    STR points to the beginning of the immediate (the leading #);
5074    VAL receives the value; if the value is outside [MIN, MAX]
5075    issue an error.  PREFIX_OPT is true if the immediate prefix is
5076    optional.  */
5077
5078 static int
5079 parse_immediate (char **str, int *val, int min, int max,
5080                  bfd_boolean prefix_opt)
5081 {
5082   expressionS exp;
5083
5084   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
5085   if (exp.X_op != O_constant)
5086     {
5087       inst.error = _("constant expression required");
5088       return FAIL;
5089     }
5090
5091   if (exp.X_add_number < min || exp.X_add_number > max)
5092     {
5093       inst.error = _("immediate value out of range");
5094       return FAIL;
5095     }
5096
5097   *val = exp.X_add_number;
5098   return SUCCESS;
5099 }
5100
5101 /* Less-generic immediate-value read function with the possibility of loading a
5102    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
5103    instructions. Puts the result directly in inst.operands[i].  */
5104
5105 static int
5106 parse_big_immediate (char **str, int i, expressionS *in_exp,
5107                      bfd_boolean allow_symbol_p)
5108 {
5109   expressionS exp;
5110   expressionS *exp_p = in_exp ? in_exp : &exp;
5111   char *ptr = *str;
5112
5113   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
5114
5115   if (exp_p->X_op == O_constant)
5116     {
5117       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
5118       /* If we're on a 64-bit host, then a 64-bit number can be returned using
5119          O_constant.  We have to be careful not to break compilation for
5120          32-bit X_add_number, though.  */
5121       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
5122         {
5123           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
5124           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
5125                                   & 0xffffffff);
5126           inst.operands[i].regisimm = 1;
5127         }
5128     }
5129   else if (exp_p->X_op == O_big
5130            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
5131     {
5132       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
5133
5134       /* Bignums have their least significant bits in
5135          generic_bignum[0]. Make sure we put 32 bits in imm and
5136          32 bits in reg,  in a (hopefully) portable way.  */
5137       gas_assert (parts != 0);
5138
5139       /* Make sure that the number is not too big.
5140          PR 11972: Bignums can now be sign-extended to the
5141          size of a .octa so check that the out of range bits
5142          are all zero or all one.  */
5143       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
5144         {
5145           LITTLENUM_TYPE m = -1;
5146
5147           if (generic_bignum[parts * 2] != 0
5148               && generic_bignum[parts * 2] != m)
5149             return FAIL;
5150
5151           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
5152             if (generic_bignum[j] != generic_bignum[j-1])
5153               return FAIL;
5154         }
5155
5156       inst.operands[i].imm = 0;
5157       for (j = 0; j < parts; j++, idx++)
5158         inst.operands[i].imm |= generic_bignum[idx]
5159                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5160       inst.operands[i].reg = 0;
5161       for (j = 0; j < parts; j++, idx++)
5162         inst.operands[i].reg |= generic_bignum[idx]
5163                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5164       inst.operands[i].regisimm = 1;
5165     }
5166   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
5167     return FAIL;
5168
5169   *str = ptr;
5170
5171   return SUCCESS;
5172 }
5173
5174 /* Returns the pseudo-register number of an FPA immediate constant,
5175    or FAIL if there isn't a valid constant here.  */
5176
5177 static int
5178 parse_fpa_immediate (char ** str)
5179 {
5180   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5181   char *         save_in;
5182   expressionS    exp;
5183   int            i;
5184   int            j;
5185
5186   /* First try and match exact strings, this is to guarantee
5187      that some formats will work even for cross assembly.  */
5188
5189   for (i = 0; fp_const[i]; i++)
5190     {
5191       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
5192         {
5193           char *start = *str;
5194
5195           *str += strlen (fp_const[i]);
5196           if (is_end_of_line[(unsigned char) **str])
5197             return i + 8;
5198           *str = start;
5199         }
5200     }
5201
5202   /* Just because we didn't get a match doesn't mean that the constant
5203      isn't valid, just that it is in a format that we don't
5204      automatically recognize.  Try parsing it with the standard
5205      expression routines.  */
5206
5207   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
5208
5209   /* Look for a raw floating point number.  */
5210   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
5211       && is_end_of_line[(unsigned char) *save_in])
5212     {
5213       for (i = 0; i < NUM_FLOAT_VALS; i++)
5214         {
5215           for (j = 0; j < MAX_LITTLENUMS; j++)
5216             {
5217               if (words[j] != fp_values[i][j])
5218                 break;
5219             }
5220
5221           if (j == MAX_LITTLENUMS)
5222             {
5223               *str = save_in;
5224               return i + 8;
5225             }
5226         }
5227     }
5228
5229   /* Try and parse a more complex expression, this will probably fail
5230      unless the code uses a floating point prefix (eg "0f").  */
5231   save_in = input_line_pointer;
5232   input_line_pointer = *str;
5233   if (expression (&exp) == absolute_section
5234       && exp.X_op == O_big
5235       && exp.X_add_number < 0)
5236     {
5237       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
5238          Ditto for 15.  */
5239 #define X_PRECISION 5
5240 #define E_PRECISION 15L
5241       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
5242         {
5243           for (i = 0; i < NUM_FLOAT_VALS; i++)
5244             {
5245               for (j = 0; j < MAX_LITTLENUMS; j++)
5246                 {
5247                   if (words[j] != fp_values[i][j])
5248                     break;
5249                 }
5250
5251               if (j == MAX_LITTLENUMS)
5252                 {
5253                   *str = input_line_pointer;
5254                   input_line_pointer = save_in;
5255                   return i + 8;
5256                 }
5257             }
5258         }
5259     }
5260
5261   *str = input_line_pointer;
5262   input_line_pointer = save_in;
5263   inst.error = _("invalid FPA immediate expression");
5264   return FAIL;
5265 }
5266
5267 /* Returns 1 if a number has "quarter-precision" float format
5268    0baBbbbbbc defgh000 00000000 00000000.  */
5269
5270 static int
5271 is_quarter_float (unsigned imm)
5272 {
5273   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
5274   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
5275 }
5276
5277
5278 /* Detect the presence of a floating point or integer zero constant,
5279    i.e. #0.0 or #0.  */
5280
5281 static bfd_boolean
5282 parse_ifimm_zero (char **in)
5283 {
5284   int error_code;
5285
5286   if (!is_immediate_prefix (**in))
5287     {
5288       /* In unified syntax, all prefixes are optional.  */
5289       if (!unified_syntax)
5290         return FALSE;
5291     }
5292   else
5293     ++*in;
5294
5295   /* Accept #0x0 as a synonym for #0.  */
5296   if (strncmp (*in, "0x", 2) == 0)
5297     {
5298       int val;
5299       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
5300         return FALSE;
5301       return TRUE;
5302     }
5303
5304   error_code = atof_generic (in, ".", EXP_CHARS,
5305                              &generic_floating_point_number);
5306
5307   if (!error_code
5308       && generic_floating_point_number.sign == '+'
5309       && (generic_floating_point_number.low
5310           > generic_floating_point_number.leader))
5311     return TRUE;
5312
5313   return FALSE;
5314 }
5315
5316 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5317    0baBbbbbbc defgh000 00000000 00000000.
5318    The zero and minus-zero cases need special handling, since they can't be
5319    encoded in the "quarter-precision" float format, but can nonetheless be
5320    loaded as integer constants.  */
5321
5322 static unsigned
5323 parse_qfloat_immediate (char **ccp, int *immed)
5324 {
5325   char *str = *ccp;
5326   char *fpnum;
5327   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5328   int found_fpchar = 0;
5329
5330   skip_past_char (&str, '#');
5331
5332   /* We must not accidentally parse an integer as a floating-point number. Make
5333      sure that the value we parse is not an integer by checking for special
5334      characters '.' or 'e'.
5335      FIXME: This is a horrible hack, but doing better is tricky because type
5336      information isn't in a very usable state at parse time.  */
5337   fpnum = str;
5338   skip_whitespace (fpnum);
5339
5340   if (strncmp (fpnum, "0x", 2) == 0)
5341     return FAIL;
5342   else
5343     {
5344       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5345         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5346           {
5347             found_fpchar = 1;
5348             break;
5349           }
5350
5351       if (!found_fpchar)
5352         return FAIL;
5353     }
5354
5355   if ((str = atof_ieee (str, 's', words)) != NULL)
5356     {
5357       unsigned fpword = 0;
5358       int i;
5359
5360       /* Our FP word must be 32 bits (single-precision FP).  */
5361       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5362         {
5363           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5364           fpword |= words[i];
5365         }
5366
5367       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5368         *immed = fpword;
5369       else
5370         return FAIL;
5371
5372       *ccp = str;
5373
5374       return SUCCESS;
5375     }
5376
5377   return FAIL;
5378 }
5379
5380 /* Shift operands.  */
5381 enum shift_kind
5382 {
5383   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX, SHIFT_UXTW
5384 };
5385
5386 struct asm_shift_name
5387 {
5388   const char      *name;
5389   enum shift_kind  kind;
5390 };
5391
5392 /* Third argument to parse_shift.  */
5393 enum parse_shift_mode
5394 {
5395   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5396   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5397   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5398   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5399   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5400   SHIFT_UXTW_IMMEDIATE          /* Shift must be UXTW immediate.  */
5401 };
5402
5403 /* Parse a <shift> specifier on an ARM data processing instruction.
5404    This has three forms:
5405
5406      (LSL|LSR|ASL|ASR|ROR) Rs
5407      (LSL|LSR|ASL|ASR|ROR) #imm
5408      RRX
5409
5410    Note that ASL is assimilated to LSL in the instruction encoding, and
5411    RRX to ROR #0 (which cannot be written as such).  */
5412
5413 static int
5414 parse_shift (char **str, int i, enum parse_shift_mode mode)
5415 {
5416   const struct asm_shift_name *shift_name;
5417   enum shift_kind shift;
5418   char *s = *str;
5419   char *p = s;
5420   int reg;
5421
5422   for (p = *str; ISALPHA (*p); p++)
5423     ;
5424
5425   if (p == *str)
5426     {
5427       inst.error = _("shift expression expected");
5428       return FAIL;
5429     }
5430
5431   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5432                                                             p - *str);
5433
5434   if (shift_name == NULL)
5435     {
5436       inst.error = _("shift expression expected");
5437       return FAIL;
5438     }
5439
5440   shift = shift_name->kind;
5441
5442   switch (mode)
5443     {
5444     case NO_SHIFT_RESTRICT:
5445     case SHIFT_IMMEDIATE:
5446       if (shift == SHIFT_UXTW)
5447         {
5448           inst.error = _("'UXTW' not allowed here");
5449           return FAIL;
5450         }
5451       break;
5452
5453     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5454       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5455         {
5456           inst.error = _("'LSL' or 'ASR' required");
5457           return FAIL;
5458         }
5459       break;
5460
5461     case SHIFT_LSL_IMMEDIATE:
5462       if (shift != SHIFT_LSL)
5463         {
5464           inst.error = _("'LSL' required");
5465           return FAIL;
5466         }
5467       break;
5468
5469     case SHIFT_ASR_IMMEDIATE:
5470       if (shift != SHIFT_ASR)
5471         {
5472           inst.error = _("'ASR' required");
5473           return FAIL;
5474         }
5475       break;
5476     case SHIFT_UXTW_IMMEDIATE:
5477       if (shift != SHIFT_UXTW)
5478         {
5479           inst.error = _("'UXTW' required");
5480           return FAIL;
5481         }
5482       break;
5483
5484     default: abort ();
5485     }
5486
5487   if (shift != SHIFT_RRX)
5488     {
5489       /* Whitespace can appear here if the next thing is a bare digit.  */
5490       skip_whitespace (p);
5491
5492       if (mode == NO_SHIFT_RESTRICT
5493           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5494         {
5495           inst.operands[i].imm = reg;
5496           inst.operands[i].immisreg = 1;
5497         }
5498       else if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5499         return FAIL;
5500     }
5501   inst.operands[i].shift_kind = shift;
5502   inst.operands[i].shifted = 1;
5503   *str = p;
5504   return SUCCESS;
5505 }
5506
5507 /* Parse a <shifter_operand> for an ARM data processing instruction:
5508
5509       #<immediate>
5510       #<immediate>, <rotate>
5511       <Rm>
5512       <Rm>, <shift>
5513
5514    where <shift> is defined by parse_shift above, and <rotate> is a
5515    multiple of 2 between 0 and 30.  Validation of immediate operands
5516    is deferred to md_apply_fix.  */
5517
5518 static int
5519 parse_shifter_operand (char **str, int i)
5520 {
5521   int value;
5522   expressionS exp;
5523
5524   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5525     {
5526       inst.operands[i].reg = value;
5527       inst.operands[i].isreg = 1;
5528
5529       /* parse_shift will override this if appropriate */
5530       inst.relocs[0].exp.X_op = O_constant;
5531       inst.relocs[0].exp.X_add_number = 0;
5532
5533       if (skip_past_comma (str) == FAIL)
5534         return SUCCESS;
5535
5536       /* Shift operation on register.  */
5537       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5538     }
5539
5540   if (my_get_expression (&inst.relocs[0].exp, str, GE_IMM_PREFIX))
5541     return FAIL;
5542
5543   if (skip_past_comma (str) == SUCCESS)
5544     {
5545       /* #x, y -- ie explicit rotation by Y.  */
5546       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5547         return FAIL;
5548
5549       if (exp.X_op != O_constant || inst.relocs[0].exp.X_op != O_constant)
5550         {
5551           inst.error = _("constant expression expected");
5552           return FAIL;
5553         }
5554
5555       value = exp.X_add_number;
5556       if (value < 0 || value > 30 || value % 2 != 0)
5557         {
5558           inst.error = _("invalid rotation");
5559           return FAIL;
5560         }
5561       if (inst.relocs[0].exp.X_add_number < 0
5562           || inst.relocs[0].exp.X_add_number > 255)
5563         {
5564           inst.error = _("invalid constant");
5565           return FAIL;
5566         }
5567
5568       /* Encode as specified.  */
5569       inst.operands[i].imm = inst.relocs[0].exp.X_add_number | value << 7;
5570       return SUCCESS;
5571     }
5572
5573   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
5574   inst.relocs[0].pc_rel = 0;
5575   return SUCCESS;
5576 }
5577
5578 /* Group relocation information.  Each entry in the table contains the
5579    textual name of the relocation as may appear in assembler source
5580    and must end with a colon.
5581    Along with this textual name are the relocation codes to be used if
5582    the corresponding instruction is an ALU instruction (ADD or SUB only),
5583    an LDR, an LDRS, or an LDC.  */
5584
5585 struct group_reloc_table_entry
5586 {
5587   const char *name;
5588   int alu_code;
5589   int ldr_code;
5590   int ldrs_code;
5591   int ldc_code;
5592 };
5593
5594 typedef enum
5595 {
5596   /* Varieties of non-ALU group relocation.  */
5597
5598   GROUP_LDR,
5599   GROUP_LDRS,
5600   GROUP_LDC,
5601   GROUP_MVE
5602 } group_reloc_type;
5603
5604 static struct group_reloc_table_entry group_reloc_table[] =
5605   { /* Program counter relative: */
5606     { "pc_g0_nc",
5607       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5608       0,                                /* LDR */
5609       0,                                /* LDRS */
5610       0 },                              /* LDC */
5611     { "pc_g0",
5612       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5613       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5614       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5615       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5616     { "pc_g1_nc",
5617       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5618       0,                                /* LDR */
5619       0,                                /* LDRS */
5620       0 },                              /* LDC */
5621     { "pc_g1",
5622       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5623       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5624       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5625       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5626     { "pc_g2",
5627       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5628       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5629       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5630       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5631     /* Section base relative */
5632     { "sb_g0_nc",
5633       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5634       0,                                /* LDR */
5635       0,                                /* LDRS */
5636       0 },                              /* LDC */
5637     { "sb_g0",
5638       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5639       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5640       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5641       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5642     { "sb_g1_nc",
5643       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5644       0,                                /* LDR */
5645       0,                                /* LDRS */
5646       0 },                              /* LDC */
5647     { "sb_g1",
5648       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5649       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5650       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5651       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5652     { "sb_g2",
5653       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5654       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5655       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5656       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5657     /* Absolute thumb alu relocations.  */
5658     { "lower0_7",
5659       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5660       0,                                /* LDR.  */
5661       0,                                /* LDRS.  */
5662       0 },                              /* LDC.  */
5663     { "lower8_15",
5664       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5665       0,                                /* LDR.  */
5666       0,                                /* LDRS.  */
5667       0 },                              /* LDC.  */
5668     { "upper0_7",
5669       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5670       0,                                /* LDR.  */
5671       0,                                /* LDRS.  */
5672       0 },                              /* LDC.  */
5673     { "upper8_15",
5674       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5675       0,                                /* LDR.  */
5676       0,                                /* LDRS.  */
5677       0 } };                            /* LDC.  */
5678
5679 /* Given the address of a pointer pointing to the textual name of a group
5680    relocation as may appear in assembler source, attempt to find its details
5681    in group_reloc_table.  The pointer will be updated to the character after
5682    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5683    otherwise.  On success, *entry will be updated to point at the relevant
5684    group_reloc_table entry. */
5685
5686 static int
5687 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5688 {
5689   unsigned int i;
5690   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5691     {
5692       int length = strlen (group_reloc_table[i].name);
5693
5694       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5695           && (*str)[length] == ':')
5696         {
5697           *out = &group_reloc_table[i];
5698           *str += (length + 1);
5699           return SUCCESS;
5700         }
5701     }
5702
5703   return FAIL;
5704 }
5705
5706 /* Parse a <shifter_operand> for an ARM data processing instruction
5707    (as for parse_shifter_operand) where group relocations are allowed:
5708
5709       #<immediate>
5710       #<immediate>, <rotate>
5711       #:<group_reloc>:<expression>
5712       <Rm>
5713       <Rm>, <shift>
5714
5715    where <group_reloc> is one of the strings defined in group_reloc_table.
5716    The hashes are optional.
5717
5718    Everything else is as for parse_shifter_operand.  */
5719
5720 static parse_operand_result
5721 parse_shifter_operand_group_reloc (char **str, int i)
5722 {
5723   /* Determine if we have the sequence of characters #: or just :
5724      coming next.  If we do, then we check for a group relocation.
5725      If we don't, punt the whole lot to parse_shifter_operand.  */
5726
5727   if (((*str)[0] == '#' && (*str)[1] == ':')
5728       || (*str)[0] == ':')
5729     {
5730       struct group_reloc_table_entry *entry;
5731
5732       if ((*str)[0] == '#')
5733         (*str) += 2;
5734       else
5735         (*str)++;
5736
5737       /* Try to parse a group relocation.  Anything else is an error.  */
5738       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5739         {
5740           inst.error = _("unknown group relocation");
5741           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5742         }
5743
5744       /* We now have the group relocation table entry corresponding to
5745          the name in the assembler source.  Next, we parse the expression.  */
5746       if (my_get_expression (&inst.relocs[0].exp, str, GE_NO_PREFIX))
5747         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5748
5749       /* Record the relocation type (always the ALU variant here).  */
5750       inst.relocs[0].type = (bfd_reloc_code_real_type) entry->alu_code;
5751       gas_assert (inst.relocs[0].type != 0);
5752
5753       return PARSE_OPERAND_SUCCESS;
5754     }
5755   else
5756     return parse_shifter_operand (str, i) == SUCCESS
5757            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5758
5759   /* Never reached.  */
5760 }
5761
5762 /* Parse a Neon alignment expression.  Information is written to
5763    inst.operands[i].  We assume the initial ':' has been skipped.
5764
5765    align        .imm = align << 8, .immisalign=1, .preind=0  */
5766 static parse_operand_result
5767 parse_neon_alignment (char **str, int i)
5768 {
5769   char *p = *str;
5770   expressionS exp;
5771
5772   my_get_expression (&exp, &p, GE_NO_PREFIX);
5773
5774   if (exp.X_op != O_constant)
5775     {
5776       inst.error = _("alignment must be constant");
5777       return PARSE_OPERAND_FAIL;
5778     }
5779
5780   inst.operands[i].imm = exp.X_add_number << 8;
5781   inst.operands[i].immisalign = 1;
5782   /* Alignments are not pre-indexes.  */
5783   inst.operands[i].preind = 0;
5784
5785   *str = p;
5786   return PARSE_OPERAND_SUCCESS;
5787 }
5788
5789 /* Parse all forms of an ARM address expression.  Information is written
5790    to inst.operands[i] and/or inst.relocs[0].
5791
5792    Preindexed addressing (.preind=1):
5793
5794    [Rn, #offset]       .reg=Rn .relocs[0].exp=offset
5795    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5796    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5797                        .shift_kind=shift .relocs[0].exp=shift_imm
5798
5799    These three may have a trailing ! which causes .writeback to be set also.
5800
5801    Postindexed addressing (.postind=1, .writeback=1):
5802
5803    [Rn], #offset       .reg=Rn .relocs[0].exp=offset
5804    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5805    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5806                        .shift_kind=shift .relocs[0].exp=shift_imm
5807
5808    Unindexed addressing (.preind=0, .postind=0):
5809
5810    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5811
5812    Other:
5813
5814    [Rn]{!}             shorthand for [Rn,#0]{!}
5815    =immediate          .isreg=0 .relocs[0].exp=immediate
5816    label               .reg=PC .relocs[0].pc_rel=1 .relocs[0].exp=label
5817
5818   It is the caller's responsibility to check for addressing modes not
5819   supported by the instruction, and to set inst.relocs[0].type.  */
5820
5821 static parse_operand_result
5822 parse_address_main (char **str, int i, int group_relocations,
5823                     group_reloc_type group_type)
5824 {
5825   char *p = *str;
5826   int reg;
5827
5828   if (skip_past_char (&p, '[') == FAIL)
5829     {
5830       if (skip_past_char (&p, '=') == FAIL)
5831         {
5832           /* Bare address - translate to PC-relative offset.  */
5833           inst.relocs[0].pc_rel = 1;
5834           inst.operands[i].reg = REG_PC;
5835           inst.operands[i].isreg = 1;
5836           inst.operands[i].preind = 1;
5837
5838           if (my_get_expression (&inst.relocs[0].exp, &p, GE_OPT_PREFIX_BIG))
5839             return PARSE_OPERAND_FAIL;
5840         }
5841       else if (parse_big_immediate (&p, i, &inst.relocs[0].exp,
5842                                     /*allow_symbol_p=*/TRUE))
5843         return PARSE_OPERAND_FAIL;
5844
5845       *str = p;
5846       return PARSE_OPERAND_SUCCESS;
5847     }
5848
5849   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5850   skip_whitespace (p);
5851
5852   if (group_type == GROUP_MVE)
5853     {
5854       enum arm_reg_type rtype = REG_TYPE_MQ;
5855       struct neon_type_el et;
5856       if ((reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5857         {
5858           inst.operands[i].isquad = 1;
5859         }
5860       else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5861         {
5862           inst.error = BAD_ADDR_MODE;
5863           return PARSE_OPERAND_FAIL;
5864         }
5865     }
5866   else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5867     {
5868       if (group_type == GROUP_MVE)
5869         inst.error = BAD_ADDR_MODE;
5870       else
5871         inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5872       return PARSE_OPERAND_FAIL;
5873     }
5874   inst.operands[i].reg = reg;
5875   inst.operands[i].isreg = 1;
5876
5877   if (skip_past_comma (&p) == SUCCESS)
5878     {
5879       inst.operands[i].preind = 1;
5880
5881       if (*p == '+') p++;
5882       else if (*p == '-') p++, inst.operands[i].negative = 1;
5883
5884       enum arm_reg_type rtype = REG_TYPE_MQ;
5885       struct neon_type_el et;
5886       if (group_type == GROUP_MVE
5887           && (reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5888         {
5889           inst.operands[i].immisreg = 2;
5890           inst.operands[i].imm = reg;
5891
5892           if (skip_past_comma (&p) == SUCCESS)
5893             {
5894               if (parse_shift (&p, i, SHIFT_UXTW_IMMEDIATE) == SUCCESS)
5895                 {
5896                   inst.operands[i].imm |= inst.relocs[0].exp.X_add_number << 5;
5897                   inst.relocs[0].exp.X_add_number = 0;
5898                 }
5899               else
5900                 return PARSE_OPERAND_FAIL;
5901             }
5902         }
5903       else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5904         {
5905           inst.operands[i].imm = reg;
5906           inst.operands[i].immisreg = 1;
5907
5908           if (skip_past_comma (&p) == SUCCESS)
5909             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5910               return PARSE_OPERAND_FAIL;
5911         }
5912       else if (skip_past_char (&p, ':') == SUCCESS)
5913         {
5914           /* FIXME: '@' should be used here, but it's filtered out by generic
5915              code before we get to see it here. This may be subject to
5916              change.  */
5917           parse_operand_result result = parse_neon_alignment (&p, i);
5918
5919           if (result != PARSE_OPERAND_SUCCESS)
5920             return result;
5921         }
5922       else
5923         {
5924           if (inst.operands[i].negative)
5925             {
5926               inst.operands[i].negative = 0;
5927               p--;
5928             }
5929
5930           if (group_relocations
5931               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5932             {
5933               struct group_reloc_table_entry *entry;
5934
5935               /* Skip over the #: or : sequence.  */
5936               if (*p == '#')
5937                 p += 2;
5938               else
5939                 p++;
5940
5941               /* Try to parse a group relocation.  Anything else is an
5942                  error.  */
5943               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5944                 {
5945                   inst.error = _("unknown group relocation");
5946                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5947                 }
5948
5949               /* We now have the group relocation table entry corresponding to
5950                  the name in the assembler source.  Next, we parse the
5951                  expression.  */
5952               if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
5953                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5954
5955               /* Record the relocation type.  */
5956               switch (group_type)
5957                 {
5958                   case GROUP_LDR:
5959                     inst.relocs[0].type
5960                         = (bfd_reloc_code_real_type) entry->ldr_code;
5961                     break;
5962
5963                   case GROUP_LDRS:
5964                     inst.relocs[0].type
5965                         = (bfd_reloc_code_real_type) entry->ldrs_code;
5966                     break;
5967
5968                   case GROUP_LDC:
5969                     inst.relocs[0].type
5970                         = (bfd_reloc_code_real_type) entry->ldc_code;
5971                     break;
5972
5973                   default:
5974                     gas_assert (0);
5975                 }
5976
5977               if (inst.relocs[0].type == 0)
5978                 {
5979                   inst.error = _("this group relocation is not allowed on this instruction");
5980                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5981                 }
5982             }
5983           else
5984             {
5985               char *q = p;
5986
5987               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5988                 return PARSE_OPERAND_FAIL;
5989               /* If the offset is 0, find out if it's a +0 or -0.  */
5990               if (inst.relocs[0].exp.X_op == O_constant
5991                   && inst.relocs[0].exp.X_add_number == 0)
5992                 {
5993                   skip_whitespace (q);
5994                   if (*q == '#')
5995                     {
5996                       q++;
5997                       skip_whitespace (q);
5998                     }
5999                   if (*q == '-')
6000                     inst.operands[i].negative = 1;
6001                 }
6002             }
6003         }
6004     }
6005   else if (skip_past_char (&p, ':') == SUCCESS)
6006     {
6007       /* FIXME: '@' should be used here, but it's filtered out by generic code
6008          before we get to see it here. This may be subject to change.  */
6009       parse_operand_result result = parse_neon_alignment (&p, i);
6010
6011       if (result != PARSE_OPERAND_SUCCESS)
6012         return result;
6013     }
6014
6015   if (skip_past_char (&p, ']') == FAIL)
6016     {
6017       inst.error = _("']' expected");
6018       return PARSE_OPERAND_FAIL;
6019     }
6020
6021   if (skip_past_char (&p, '!') == SUCCESS)
6022     inst.operands[i].writeback = 1;
6023
6024   else if (skip_past_comma (&p) == SUCCESS)
6025     {
6026       if (skip_past_char (&p, '{') == SUCCESS)
6027         {
6028           /* [Rn], {expr} - unindexed, with option */
6029           if (parse_immediate (&p, &inst.operands[i].imm,
6030                                0, 255, TRUE) == FAIL)
6031             return PARSE_OPERAND_FAIL;
6032
6033           if (skip_past_char (&p, '}') == FAIL)
6034             {
6035               inst.error = _("'}' expected at end of 'option' field");
6036               return PARSE_OPERAND_FAIL;
6037             }
6038           if (inst.operands[i].preind)
6039             {
6040               inst.error = _("cannot combine index with option");
6041               return PARSE_OPERAND_FAIL;
6042             }
6043           *str = p;
6044           return PARSE_OPERAND_SUCCESS;
6045         }
6046       else
6047         {
6048           inst.operands[i].postind = 1;
6049           inst.operands[i].writeback = 1;
6050
6051           if (inst.operands[i].preind)
6052             {
6053               inst.error = _("cannot combine pre- and post-indexing");
6054               return PARSE_OPERAND_FAIL;
6055             }
6056
6057           if (*p == '+') p++;
6058           else if (*p == '-') p++, inst.operands[i].negative = 1;
6059
6060           enum arm_reg_type rtype = REG_TYPE_MQ;
6061           struct neon_type_el et;
6062           if (group_type == GROUP_MVE
6063               && (reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
6064             {
6065               inst.operands[i].immisreg = 2;
6066               inst.operands[i].imm = reg;
6067             }
6068           else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
6069             {
6070               /* We might be using the immediate for alignment already. If we
6071                  are, OR the register number into the low-order bits.  */
6072               if (inst.operands[i].immisalign)
6073                 inst.operands[i].imm |= reg;
6074               else
6075                 inst.operands[i].imm = reg;
6076               inst.operands[i].immisreg = 1;
6077
6078               if (skip_past_comma (&p) == SUCCESS)
6079                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
6080                   return PARSE_OPERAND_FAIL;
6081             }
6082           else
6083             {
6084               char *q = p;
6085
6086               if (inst.operands[i].negative)
6087                 {
6088                   inst.operands[i].negative = 0;
6089                   p--;
6090                 }
6091               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
6092                 return PARSE_OPERAND_FAIL;
6093               /* If the offset is 0, find out if it's a +0 or -0.  */
6094               if (inst.relocs[0].exp.X_op == O_constant
6095                   && inst.relocs[0].exp.X_add_number == 0)
6096                 {
6097                   skip_whitespace (q);
6098                   if (*q == '#')
6099                     {
6100                       q++;
6101                       skip_whitespace (q);
6102                     }
6103                   if (*q == '-')
6104                     inst.operands[i].negative = 1;
6105                 }
6106             }
6107         }
6108     }
6109
6110   /* If at this point neither .preind nor .postind is set, we have a
6111      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
6112   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
6113     {
6114       inst.operands[i].preind = 1;
6115       inst.relocs[0].exp.X_op = O_constant;
6116       inst.relocs[0].exp.X_add_number = 0;
6117     }
6118   *str = p;
6119   return PARSE_OPERAND_SUCCESS;
6120 }
6121
6122 static int
6123 parse_address (char **str, int i)
6124 {
6125   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
6126          ? SUCCESS : FAIL;
6127 }
6128
6129 static parse_operand_result
6130 parse_address_group_reloc (char **str, int i, group_reloc_type type)
6131 {
6132   return parse_address_main (str, i, 1, type);
6133 }
6134
6135 /* Parse an operand for a MOVW or MOVT instruction.  */
6136 static int
6137 parse_half (char **str)
6138 {
6139   char * p;
6140
6141   p = *str;
6142   skip_past_char (&p, '#');
6143   if (strncasecmp (p, ":lower16:", 9) == 0)
6144     inst.relocs[0].type = BFD_RELOC_ARM_MOVW;
6145   else if (strncasecmp (p, ":upper16:", 9) == 0)
6146     inst.relocs[0].type = BFD_RELOC_ARM_MOVT;
6147
6148   if (inst.relocs[0].type != BFD_RELOC_UNUSED)
6149     {
6150       p += 9;
6151       skip_whitespace (p);
6152     }
6153
6154   if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
6155     return FAIL;
6156
6157   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
6158     {
6159       if (inst.relocs[0].exp.X_op != O_constant)
6160         {
6161           inst.error = _("constant expression expected");
6162           return FAIL;
6163         }
6164       if (inst.relocs[0].exp.X_add_number < 0
6165           || inst.relocs[0].exp.X_add_number > 0xffff)
6166         {
6167           inst.error = _("immediate value out of range");
6168           return FAIL;
6169         }
6170     }
6171   *str = p;
6172   return SUCCESS;
6173 }
6174
6175 /* Miscellaneous. */
6176
6177 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
6178    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
6179 static int
6180 parse_psr (char **str, bfd_boolean lhs)
6181 {
6182   char *p;
6183   unsigned long psr_field;
6184   const struct asm_psr *psr;
6185   char *start;
6186   bfd_boolean is_apsr = FALSE;
6187   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
6188
6189   /* PR gas/12698:  If the user has specified -march=all then m_profile will
6190      be TRUE, but we want to ignore it in this case as we are building for any
6191      CPU type, including non-m variants.  */
6192   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
6193     m_profile = FALSE;
6194
6195   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
6196      feature for ease of use and backwards compatibility.  */
6197   p = *str;
6198   if (strncasecmp (p, "SPSR", 4) == 0)
6199     {
6200       if (m_profile)
6201         goto unsupported_psr;
6202
6203       psr_field = SPSR_BIT;
6204     }
6205   else if (strncasecmp (p, "CPSR", 4) == 0)
6206     {
6207       if (m_profile)
6208         goto unsupported_psr;
6209
6210       psr_field = 0;
6211     }
6212   else if (strncasecmp (p, "APSR", 4) == 0)
6213     {
6214       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
6215          and ARMv7-R architecture CPUs.  */
6216       is_apsr = TRUE;
6217       psr_field = 0;
6218     }
6219   else if (m_profile)
6220     {
6221       start = p;
6222       do
6223         p++;
6224       while (ISALNUM (*p) || *p == '_');
6225
6226       if (strncasecmp (start, "iapsr", 5) == 0
6227           || strncasecmp (start, "eapsr", 5) == 0
6228           || strncasecmp (start, "xpsr", 4) == 0
6229           || strncasecmp (start, "psr", 3) == 0)
6230         p = start + strcspn (start, "rR") + 1;
6231
6232       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
6233                                                   p - start);
6234
6235       if (!psr)
6236         return FAIL;
6237
6238       /* If APSR is being written, a bitfield may be specified.  Note that
6239          APSR itself is handled above.  */
6240       if (psr->field <= 3)
6241         {
6242           psr_field = psr->field;
6243           is_apsr = TRUE;
6244           goto check_suffix;
6245         }
6246
6247       *str = p;
6248       /* M-profile MSR instructions have the mask field set to "10", except
6249          *PSR variants which modify APSR, which may use a different mask (and
6250          have been handled already).  Do that by setting the PSR_f field
6251          here.  */
6252       return psr->field | (lhs ? PSR_f : 0);
6253     }
6254   else
6255     goto unsupported_psr;
6256
6257   p += 4;
6258 check_suffix:
6259   if (*p == '_')
6260     {
6261       /* A suffix follows.  */
6262       p++;
6263       start = p;
6264
6265       do
6266         p++;
6267       while (ISALNUM (*p) || *p == '_');
6268
6269       if (is_apsr)
6270         {
6271           /* APSR uses a notation for bits, rather than fields.  */
6272           unsigned int nzcvq_bits = 0;
6273           unsigned int g_bit = 0;
6274           char *bit;
6275
6276           for (bit = start; bit != p; bit++)
6277             {
6278               switch (TOLOWER (*bit))
6279                 {
6280                 case 'n':
6281                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
6282                   break;
6283
6284                 case 'z':
6285                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
6286                   break;
6287
6288                 case 'c':
6289                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
6290                   break;
6291
6292                 case 'v':
6293                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
6294                   break;
6295
6296                 case 'q':
6297                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
6298                   break;
6299
6300                 case 'g':
6301                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
6302                   break;
6303
6304                 default:
6305                   inst.error = _("unexpected bit specified after APSR");
6306                   return FAIL;
6307                 }
6308             }
6309
6310           if (nzcvq_bits == 0x1f)
6311             psr_field |= PSR_f;
6312
6313           if (g_bit == 0x1)
6314             {
6315               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
6316                 {
6317                   inst.error = _("selected processor does not "
6318                                  "support DSP extension");
6319                   return FAIL;
6320                 }
6321
6322               psr_field |= PSR_s;
6323             }
6324
6325           if ((nzcvq_bits & 0x20) != 0
6326               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
6327               || (g_bit & 0x2) != 0)
6328             {
6329               inst.error = _("bad bitmask specified after APSR");
6330               return FAIL;
6331             }
6332         }
6333       else
6334         {
6335           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
6336                                                       p - start);
6337           if (!psr)
6338             goto error;
6339
6340           psr_field |= psr->field;
6341         }
6342     }
6343   else
6344     {
6345       if (ISALNUM (*p))
6346         goto error;    /* Garbage after "[CS]PSR".  */
6347
6348       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
6349          is deprecated, but allow it anyway.  */
6350       if (is_apsr && lhs)
6351         {
6352           psr_field |= PSR_f;
6353           as_tsktsk (_("writing to APSR without specifying a bitmask is "
6354                        "deprecated"));
6355         }
6356       else if (!m_profile)
6357         /* These bits are never right for M-profile devices: don't set them
6358            (only code paths which read/write APSR reach here).  */
6359         psr_field |= (PSR_c | PSR_f);
6360     }
6361   *str = p;
6362   return psr_field;
6363
6364  unsupported_psr:
6365   inst.error = _("selected processor does not support requested special "
6366                  "purpose register");
6367   return FAIL;
6368
6369  error:
6370   inst.error = _("flag for {c}psr instruction expected");
6371   return FAIL;
6372 }
6373
6374 static int
6375 parse_sys_vldr_vstr (char **str)
6376 {
6377   unsigned i;
6378   int val = FAIL;
6379   struct {
6380     const char *name;
6381     int regl;
6382     int regh;
6383   } sysregs[] = {
6384     {"FPSCR",           0x1, 0x0},
6385     {"FPSCR_nzcvqc",    0x2, 0x0},
6386     {"VPR",             0x4, 0x1},
6387     {"P0",              0x5, 0x1},
6388     {"FPCXTNS",         0x6, 0x1},
6389     {"FPCXTS",          0x7, 0x1}
6390   };
6391   char *op_end = strchr (*str, ',');
6392   size_t op_strlen = op_end - *str;
6393
6394   for (i = 0; i < sizeof (sysregs) / sizeof (sysregs[0]); i++)
6395     {
6396       if (!strncmp (*str, sysregs[i].name, op_strlen))
6397         {
6398           val = sysregs[i].regl | (sysregs[i].regh << 3);
6399           *str = op_end;
6400           break;
6401         }
6402     }
6403
6404   return val;
6405 }
6406
6407 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
6408    value suitable for splatting into the AIF field of the instruction.  */
6409
6410 static int
6411 parse_cps_flags (char **str)
6412 {
6413   int val = 0;
6414   int saw_a_flag = 0;
6415   char *s = *str;
6416
6417   for (;;)
6418     switch (*s++)
6419       {
6420       case '\0': case ',':
6421         goto done;
6422
6423       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6424       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6425       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6426
6427       default:
6428         inst.error = _("unrecognized CPS flag");
6429         return FAIL;
6430       }
6431
6432  done:
6433   if (saw_a_flag == 0)
6434     {
6435       inst.error = _("missing CPS flags");
6436       return FAIL;
6437     }
6438
6439   *str = s - 1;
6440   return val;
6441 }
6442
6443 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6444    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6445
6446 static int
6447 parse_endian_specifier (char **str)
6448 {
6449   int little_endian;
6450   char *s = *str;
6451
6452   if (strncasecmp (s, "BE", 2))
6453     little_endian = 0;
6454   else if (strncasecmp (s, "LE", 2))
6455     little_endian = 1;
6456   else
6457     {
6458       inst.error = _("valid endian specifiers are be or le");
6459       return FAIL;
6460     }
6461
6462   if (ISALNUM (s[2]) || s[2] == '_')
6463     {
6464       inst.error = _("valid endian specifiers are be or le");
6465       return FAIL;
6466     }
6467
6468   *str = s + 2;
6469   return little_endian;
6470 }
6471
6472 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6473    value suitable for poking into the rotate field of an sxt or sxta
6474    instruction, or FAIL on error.  */
6475
6476 static int
6477 parse_ror (char **str)
6478 {
6479   int rot;
6480   char *s = *str;
6481
6482   if (strncasecmp (s, "ROR", 3) == 0)
6483     s += 3;
6484   else
6485     {
6486       inst.error = _("missing rotation field after comma");
6487       return FAIL;
6488     }
6489
6490   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6491     return FAIL;
6492
6493   switch (rot)
6494     {
6495     case  0: *str = s; return 0x0;
6496     case  8: *str = s; return 0x1;
6497     case 16: *str = s; return 0x2;
6498     case 24: *str = s; return 0x3;
6499
6500     default:
6501       inst.error = _("rotation can only be 0, 8, 16, or 24");
6502       return FAIL;
6503     }
6504 }
6505
6506 /* Parse a conditional code (from conds[] below).  The value returned is in the
6507    range 0 .. 14, or FAIL.  */
6508 static int
6509 parse_cond (char **str)
6510 {
6511   char *q;
6512   const struct asm_cond *c;
6513   int n;
6514   /* Condition codes are always 2 characters, so matching up to
6515      3 characters is sufficient.  */
6516   char cond[3];
6517
6518   q = *str;
6519   n = 0;
6520   while (ISALPHA (*q) && n < 3)
6521     {
6522       cond[n] = TOLOWER (*q);
6523       q++;
6524       n++;
6525     }
6526
6527   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6528   if (!c)
6529     {
6530       inst.error = _("condition required");
6531       return FAIL;
6532     }
6533
6534   *str = q;
6535   return c->value;
6536 }
6537
6538 /* Parse an option for a barrier instruction.  Returns the encoding for the
6539    option, or FAIL.  */
6540 static int
6541 parse_barrier (char **str)
6542 {
6543   char *p, *q;
6544   const struct asm_barrier_opt *o;
6545
6546   p = q = *str;
6547   while (ISALPHA (*q))
6548     q++;
6549
6550   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6551                                                     q - p);
6552   if (!o)
6553     return FAIL;
6554
6555   if (!mark_feature_used (&o->arch))
6556     return FAIL;
6557
6558   *str = q;
6559   return o->value;
6560 }
6561
6562 /* Parse the operands of a table branch instruction.  Similar to a memory
6563    operand.  */
6564 static int
6565 parse_tb (char **str)
6566 {
6567   char * p = *str;
6568   int reg;
6569
6570   if (skip_past_char (&p, '[') == FAIL)
6571     {
6572       inst.error = _("'[' expected");
6573       return FAIL;
6574     }
6575
6576   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6577     {
6578       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6579       return FAIL;
6580     }
6581   inst.operands[0].reg = reg;
6582
6583   if (skip_past_comma (&p) == FAIL)
6584     {
6585       inst.error = _("',' expected");
6586       return FAIL;
6587     }
6588
6589   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6590     {
6591       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6592       return FAIL;
6593     }
6594   inst.operands[0].imm = reg;
6595
6596   if (skip_past_comma (&p) == SUCCESS)
6597     {
6598       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6599         return FAIL;
6600       if (inst.relocs[0].exp.X_add_number != 1)
6601         {
6602           inst.error = _("invalid shift");
6603           return FAIL;
6604         }
6605       inst.operands[0].shifted = 1;
6606     }
6607
6608   if (skip_past_char (&p, ']') == FAIL)
6609     {
6610       inst.error = _("']' expected");
6611       return FAIL;
6612     }
6613   *str = p;
6614   return SUCCESS;
6615 }
6616
6617 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6618    information on the types the operands can take and how they are encoded.
6619    Up to four operands may be read; this function handles setting the
6620    ".present" field for each read operand itself.
6621    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6622    else returns FAIL.  */
6623
6624 static int
6625 parse_neon_mov (char **str, int *which_operand)
6626 {
6627   int i = *which_operand, val;
6628   enum arm_reg_type rtype;
6629   char *ptr = *str;
6630   struct neon_type_el optype;
6631
6632    if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6633     {
6634       /* Cases 17 or 19.  */
6635       inst.operands[i].reg = val;
6636       inst.operands[i].isvec = 1;
6637       inst.operands[i].isscalar = 2;
6638       inst.operands[i].vectype = optype;
6639       inst.operands[i++].present = 1;
6640
6641       if (skip_past_comma (&ptr) == FAIL)
6642         goto wanted_comma;
6643
6644       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6645         {
6646           /* Case 17: VMOV<c>.<dt> <Qd[idx]>, <Rt>  */
6647           inst.operands[i].reg = val;
6648           inst.operands[i].isreg = 1;
6649           inst.operands[i].present = 1;
6650         }
6651       else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6652         {
6653           /* Case 19: VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>  */
6654           inst.operands[i].reg = val;
6655           inst.operands[i].isvec = 1;
6656           inst.operands[i].isscalar = 2;
6657           inst.operands[i].vectype = optype;
6658           inst.operands[i++].present = 1;
6659
6660           if (skip_past_comma (&ptr) == FAIL)
6661             goto wanted_comma;
6662
6663           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6664             goto wanted_arm;
6665
6666           inst.operands[i].reg = val;
6667           inst.operands[i].isreg = 1;
6668           inst.operands[i++].present = 1;
6669
6670           if (skip_past_comma (&ptr) == FAIL)
6671             goto wanted_comma;
6672
6673           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6674             goto wanted_arm;
6675
6676           inst.operands[i].reg = val;
6677           inst.operands[i].isreg = 1;
6678           inst.operands[i].present = 1;
6679         }
6680       else
6681         {
6682           first_error (_("expected ARM or MVE vector register"));
6683           return FAIL;
6684         }
6685     }
6686    else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_VFD)) != FAIL)
6687     {
6688       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6689       inst.operands[i].reg = val;
6690       inst.operands[i].isscalar = 1;
6691       inst.operands[i].vectype = optype;
6692       inst.operands[i++].present = 1;
6693
6694       if (skip_past_comma (&ptr) == FAIL)
6695         goto wanted_comma;
6696
6697       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6698         goto wanted_arm;
6699
6700       inst.operands[i].reg = val;
6701       inst.operands[i].isreg = 1;
6702       inst.operands[i].present = 1;
6703     }
6704   else if (((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6705             != FAIL)
6706            || ((val = arm_typed_reg_parse (&ptr, REG_TYPE_MQ, &rtype, &optype))
6707                != FAIL))
6708     {
6709       /* Cases 0, 1, 2, 3, 5 (D only).  */
6710       if (skip_past_comma (&ptr) == FAIL)
6711         goto wanted_comma;
6712
6713       inst.operands[i].reg = val;
6714       inst.operands[i].isreg = 1;
6715       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6716       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6717       inst.operands[i].isvec = 1;
6718       inst.operands[i].vectype = optype;
6719       inst.operands[i++].present = 1;
6720
6721       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6722         {
6723           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6724              Case 13: VMOV <Sd>, <Rm>  */
6725           inst.operands[i].reg = val;
6726           inst.operands[i].isreg = 1;
6727           inst.operands[i].present = 1;
6728
6729           if (rtype == REG_TYPE_NQ)
6730             {
6731               first_error (_("can't use Neon quad register here"));
6732               return FAIL;
6733             }
6734           else if (rtype != REG_TYPE_VFS)
6735             {
6736               i++;
6737               if (skip_past_comma (&ptr) == FAIL)
6738                 goto wanted_comma;
6739               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6740                 goto wanted_arm;
6741               inst.operands[i].reg = val;
6742               inst.operands[i].isreg = 1;
6743               inst.operands[i].present = 1;
6744             }
6745         }
6746       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6747                                            &optype)) != FAIL)
6748         {
6749           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6750              Case 1: VMOV<c><q> <Dd>, <Dm>
6751              Case 8: VMOV.F32 <Sd>, <Sm>
6752              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6753
6754           inst.operands[i].reg = val;
6755           inst.operands[i].isreg = 1;
6756           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6757           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6758           inst.operands[i].isvec = 1;
6759           inst.operands[i].vectype = optype;
6760           inst.operands[i].present = 1;
6761
6762           if (skip_past_comma (&ptr) == SUCCESS)
6763             {
6764               /* Case 15.  */
6765               i++;
6766
6767               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6768                 goto wanted_arm;
6769
6770               inst.operands[i].reg = val;
6771               inst.operands[i].isreg = 1;
6772               inst.operands[i++].present = 1;
6773
6774               if (skip_past_comma (&ptr) == FAIL)
6775                 goto wanted_comma;
6776
6777               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6778                 goto wanted_arm;
6779
6780               inst.operands[i].reg = val;
6781               inst.operands[i].isreg = 1;
6782               inst.operands[i].present = 1;
6783             }
6784         }
6785       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6786           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6787              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6788              Case 10: VMOV.F32 <Sd>, #<imm>
6789              Case 11: VMOV.F64 <Dd>, #<imm>  */
6790         inst.operands[i].immisfloat = 1;
6791       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6792                == SUCCESS)
6793           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6794              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6795         ;
6796       else
6797         {
6798           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6799           return FAIL;
6800         }
6801     }
6802   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6803     {
6804       /* Cases 6, 7, 16, 18.  */
6805       inst.operands[i].reg = val;
6806       inst.operands[i].isreg = 1;
6807       inst.operands[i++].present = 1;
6808
6809       if (skip_past_comma (&ptr) == FAIL)
6810         goto wanted_comma;
6811
6812       if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6813         {
6814           /* Case 18: VMOV<c>.<dt> <Rt>, <Qn[idx]>  */
6815           inst.operands[i].reg = val;
6816           inst.operands[i].isscalar = 2;
6817           inst.operands[i].present = 1;
6818           inst.operands[i].vectype = optype;
6819         }
6820       else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_VFD)) != FAIL)
6821         {
6822           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6823           inst.operands[i].reg = val;
6824           inst.operands[i].isscalar = 1;
6825           inst.operands[i].present = 1;
6826           inst.operands[i].vectype = optype;
6827         }
6828       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6829         {
6830           inst.operands[i].reg = val;
6831           inst.operands[i].isreg = 1;
6832           inst.operands[i++].present = 1;
6833
6834           if (skip_past_comma (&ptr) == FAIL)
6835             goto wanted_comma;
6836
6837           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6838               != FAIL)
6839             {
6840               /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6841
6842               inst.operands[i].reg = val;
6843               inst.operands[i].isreg = 1;
6844               inst.operands[i].isvec = 1;
6845               inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6846               inst.operands[i].vectype = optype;
6847               inst.operands[i].present = 1;
6848
6849               if (rtype == REG_TYPE_VFS)
6850                 {
6851                   /* Case 14.  */
6852                   i++;
6853                   if (skip_past_comma (&ptr) == FAIL)
6854                     goto wanted_comma;
6855                   if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6856                                                   &optype)) == FAIL)
6857                     {
6858                       first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6859                       return FAIL;
6860                     }
6861                   inst.operands[i].reg = val;
6862                   inst.operands[i].isreg = 1;
6863                   inst.operands[i].isvec = 1;
6864                   inst.operands[i].issingle = 1;
6865                   inst.operands[i].vectype = optype;
6866                   inst.operands[i].present = 1;
6867                 }
6868             }
6869           else
6870             {
6871               if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ))
6872                        != FAIL)
6873                 {
6874                   /* Case 16: VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>  */
6875                   inst.operands[i].reg = val;
6876                   inst.operands[i].isvec = 1;
6877                   inst.operands[i].isscalar = 2;
6878                   inst.operands[i].vectype = optype;
6879                   inst.operands[i++].present = 1;
6880
6881                   if (skip_past_comma (&ptr) == FAIL)
6882                     goto wanted_comma;
6883
6884                   if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ))
6885                       == FAIL)
6886                     {
6887                       first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
6888                       return FAIL;
6889                     }
6890                   inst.operands[i].reg = val;
6891                   inst.operands[i].isvec = 1;
6892                   inst.operands[i].isscalar = 2;
6893                   inst.operands[i].vectype = optype;
6894                   inst.operands[i].present = 1;
6895                 }
6896               else
6897                 {
6898                   first_error (_("VFP single, double or MVE vector register"
6899                                " expected"));
6900                   return FAIL;
6901                 }
6902             }
6903         }
6904       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6905                != FAIL)
6906         {
6907           /* Case 13.  */
6908           inst.operands[i].reg = val;
6909           inst.operands[i].isreg = 1;
6910           inst.operands[i].isvec = 1;
6911           inst.operands[i].issingle = 1;
6912           inst.operands[i].vectype = optype;
6913           inst.operands[i].present = 1;
6914         }
6915     }
6916   else
6917     {
6918       first_error (_("parse error"));
6919       return FAIL;
6920     }
6921
6922   /* Successfully parsed the operands. Update args.  */
6923   *which_operand = i;
6924   *str = ptr;
6925   return SUCCESS;
6926
6927  wanted_comma:
6928   first_error (_("expected comma"));
6929   return FAIL;
6930
6931  wanted_arm:
6932   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6933   return FAIL;
6934 }
6935
6936 /* Use this macro when the operand constraints are different
6937    for ARM and THUMB (e.g. ldrd).  */
6938 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6939         ((arm_operand) | ((thumb_operand) << 16))
6940
6941 /* Matcher codes for parse_operands.  */
6942 enum operand_parse_code
6943 {
6944   OP_stop,      /* end of line */
6945
6946   OP_RR,        /* ARM register */
6947   OP_RRnpc,     /* ARM register, not r15 */
6948   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6949   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6950   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6951                    optional trailing ! */
6952   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6953   OP_RCP,       /* Coprocessor number */
6954   OP_RCN,       /* Coprocessor register */
6955   OP_RF,        /* FPA register */
6956   OP_RVS,       /* VFP single precision register */
6957   OP_RVD,       /* VFP double precision register (0..15) */
6958   OP_RND,       /* Neon double precision register (0..31) */
6959   OP_RNDMQ,     /* Neon double precision (0..31) or MVE vector register.  */
6960   OP_RNDMQR,    /* Neon double precision (0..31), MVE vector or ARM register.
6961                  */
6962   OP_RNQ,       /* Neon quad precision register */
6963   OP_RNQMQ,     /* Neon quad or MVE vector register.  */
6964   OP_RVSD,      /* VFP single or double precision register */
6965   OP_RVSD_COND, /* VFP single, double precision register or condition code.  */
6966   OP_RVSDMQ,    /* VFP single, double precision or MVE vector register.  */
6967   OP_RNSD,      /* Neon single or double precision register */
6968   OP_RNDQ,      /* Neon double or quad precision register */
6969   OP_RNDQMQ,     /* Neon double, quad or MVE vector register.  */
6970   OP_RNDQMQR,   /* Neon double, quad, MVE vector or ARM register.  */
6971   OP_RNSDQ,     /* Neon single, double or quad precision register */
6972   OP_RNSC,      /* Neon scalar D[X] */
6973   OP_RVC,       /* VFP control register */
6974   OP_RMF,       /* Maverick F register */
6975   OP_RMD,       /* Maverick D register */
6976   OP_RMFX,      /* Maverick FX register */
6977   OP_RMDX,      /* Maverick DX register */
6978   OP_RMAX,      /* Maverick AX register */
6979   OP_RMDS,      /* Maverick DSPSC register */
6980   OP_RIWR,      /* iWMMXt wR register */
6981   OP_RIWC,      /* iWMMXt wC register */
6982   OP_RIWG,      /* iWMMXt wCG register */
6983   OP_RXA,       /* XScale accumulator register */
6984
6985   OP_RNSDQMQ,   /* Neon single, double or quad register or MVE vector register
6986                  */
6987   OP_RNSDQMQR,  /* Neon single, double or quad register, MVE vector register or
6988                    GPR (no SP/SP)  */
6989   OP_RMQ,       /* MVE vector register.  */
6990   OP_RMQRZ,     /* MVE vector or ARM register including ZR.  */
6991   OP_RMQRR,     /* MVE vector or ARM register.  */
6992
6993   /* New operands for Armv8.1-M Mainline.  */
6994   OP_LR,        /* ARM LR register */
6995   OP_RRe,       /* ARM register, only even numbered.  */
6996   OP_RRo,       /* ARM register, only odd numbered, not r13 or r15.  */
6997   OP_RRnpcsp_I32, /* ARM register (no BadReg) or literal 1 .. 32 */
6998   OP_RR_ZR,     /* ARM register or ZR but no PC */
6999
7000   OP_REGLST,    /* ARM register list */
7001   OP_CLRMLST,   /* CLRM register list */
7002   OP_VRSLST,    /* VFP single-precision register list */
7003   OP_VRDLST,    /* VFP double-precision register list */
7004   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
7005   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
7006   OP_NSTRLST,   /* Neon element/structure list */
7007   OP_VRSDVLST,  /* VFP single or double-precision register list and VPR */
7008   OP_MSTRLST2,  /* MVE vector list with two elements.  */
7009   OP_MSTRLST4,  /* MVE vector list with four elements.  */
7010
7011   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
7012   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
7013   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
7014   OP_RSVDMQ_FI0, /* VFP S, D, MVE vector register or floating point immediate
7015                     zero.  */
7016   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
7017   OP_RNSD_RNSC, /* Neon S or D reg, or Neon scalar.  */
7018   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
7019   OP_RNSDQ_RNSC_MQ, /* Vector S, D or Q reg, Neon scalar or MVE vector register.
7020                      */
7021   OP_RNSDQ_RNSC_MQ_RR, /* Vector S, D or Q reg, or MVE vector reg , or Neon
7022                           scalar, or ARM register.  */
7023   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
7024   OP_RNDQ_RNSC_RR, /* Neon D or Q reg, Neon scalar, or ARM register.  */
7025   OP_RNDQMQ_RNSC_RR, /* Neon D or Q reg, Neon scalar, MVE vector or ARM
7026                         register.  */
7027   OP_RNDQMQ_RNSC, /* Neon D, Q or MVE vector reg, or Neon scalar.  */
7028   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
7029   OP_VMOV,      /* Neon VMOV operands.  */
7030   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
7031   /* Neon D, Q or MVE vector register, or big immediate for logic and VMVN.  */
7032   OP_RNDQMQ_Ibig,
7033   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
7034   OP_RNDQMQ_I63b_RR, /* Neon D or Q reg, immediate for shift, MVE vector or
7035                         ARM register.  */
7036   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
7037   OP_VLDR,      /* VLDR operand.  */
7038
7039   OP_I0,        /* immediate zero */
7040   OP_I7,        /* immediate value 0 .. 7 */
7041   OP_I15,       /*                 0 .. 15 */
7042   OP_I16,       /*                 1 .. 16 */
7043   OP_I16z,      /*                 0 .. 16 */
7044   OP_I31,       /*                 0 .. 31 */
7045   OP_I31w,      /*                 0 .. 31, optional trailing ! */
7046   OP_I32,       /*                 1 .. 32 */
7047   OP_I32z,      /*                 0 .. 32 */
7048   OP_I48_I64,   /*                 48 or 64 */
7049   OP_I63,       /*                 0 .. 63 */
7050   OP_I63s,      /*               -64 .. 63 */
7051   OP_I64,       /*                 1 .. 64 */
7052   OP_I64z,      /*                 0 .. 64 */
7053   OP_I255,      /*                 0 .. 255 */
7054
7055   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
7056   OP_I7b,       /*                             0 .. 7 */
7057   OP_I15b,      /*                             0 .. 15 */
7058   OP_I31b,      /*                             0 .. 31 */
7059
7060   OP_SH,        /* shifter operand */
7061   OP_SHG,       /* shifter operand with possible group relocation */
7062   OP_ADDR,      /* Memory address expression (any mode) */
7063   OP_ADDRMVE,   /* Memory address expression for MVE's VSTR/VLDR.  */
7064   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
7065   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
7066   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
7067   OP_EXP,       /* arbitrary expression */
7068   OP_EXPi,      /* same, with optional immediate prefix */
7069   OP_EXPr,      /* same, with optional relocation suffix */
7070   OP_EXPs,      /* same, with optional non-first operand relocation suffix */
7071   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
7072   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
7073   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
7074
7075   OP_CPSF,      /* CPS flags */
7076   OP_ENDI,      /* Endianness specifier */
7077   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
7078   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
7079   OP_COND,      /* conditional code */
7080   OP_TB,        /* Table branch.  */
7081
7082   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
7083
7084   OP_RRnpc_I0,  /* ARM register or literal 0 */
7085   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
7086   OP_RR_EXi,    /* ARM register or expression with imm prefix */
7087   OP_RF_IF,     /* FPA register or immediate */
7088   OP_RIWR_RIWC, /* iWMMXt R or C reg */
7089   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
7090
7091   /* Optional operands.  */
7092   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
7093   OP_oI31b,      /*                             0 .. 31 */
7094   OP_oI32b,      /*                             1 .. 32 */
7095   OP_oI32z,      /*                             0 .. 32 */
7096   OP_oIffffb,    /*                             0 .. 65535 */
7097   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
7098
7099   OP_oRR,        /* ARM register */
7100   OP_oLR,        /* ARM LR register */
7101   OP_oRRnpc,     /* ARM register, not the PC */
7102   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
7103   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
7104   OP_oRND,       /* Optional Neon double precision register */
7105   OP_oRNQ,       /* Optional Neon quad precision register */
7106   OP_oRNDQMQ,     /* Optional Neon double, quad or MVE vector register.  */
7107   OP_oRNDQ,      /* Optional Neon double or quad precision register */
7108   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
7109   OP_oRNSDQMQ,   /* Optional single, double or quad register or MVE vector
7110                     register.  */
7111   OP_oSHll,      /* LSL immediate */
7112   OP_oSHar,      /* ASR immediate */
7113   OP_oSHllar,    /* LSL or ASR immediate */
7114   OP_oROR,       /* ROR 0/8/16/24 */
7115   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
7116
7117   OP_oRMQRZ,    /* optional MVE vector or ARM register including ZR.  */
7118
7119   /* Some pre-defined mixed (ARM/THUMB) operands.  */
7120   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
7121   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
7122   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
7123
7124   OP_FIRST_OPTIONAL = OP_oI7b
7125 };
7126
7127 /* Generic instruction operand parser.  This does no encoding and no
7128    semantic validation; it merely squirrels values away in the inst
7129    structure.  Returns SUCCESS or FAIL depending on whether the
7130    specified grammar matched.  */
7131 static int
7132 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
7133 {
7134   unsigned const int *upat = pattern;
7135   char *backtrack_pos = 0;
7136   const char *backtrack_error = 0;
7137   int i, val = 0, backtrack_index = 0;
7138   enum arm_reg_type rtype;
7139   parse_operand_result result;
7140   unsigned int op_parse_code;
7141   bfd_boolean partial_match;
7142
7143 #define po_char_or_fail(chr)                    \
7144   do                                            \
7145     {                                           \
7146       if (skip_past_char (&str, chr) == FAIL)   \
7147         goto bad_args;                          \
7148     }                                           \
7149   while (0)
7150
7151 #define po_reg_or_fail(regtype)                                 \
7152   do                                                            \
7153     {                                                           \
7154       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7155                                  & inst.operands[i].vectype);   \
7156       if (val == FAIL)                                          \
7157         {                                                       \
7158           first_error (_(reg_expected_msgs[regtype]));          \
7159           goto failure;                                         \
7160         }                                                       \
7161       inst.operands[i].reg = val;                               \
7162       inst.operands[i].isreg = 1;                               \
7163       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7164       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7165       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7166                              || rtype == REG_TYPE_VFD           \
7167                              || rtype == REG_TYPE_NQ);          \
7168       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7169     }                                                           \
7170   while (0)
7171
7172 #define po_reg_or_goto(regtype, label)                          \
7173   do                                                            \
7174     {                                                           \
7175       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7176                                  & inst.operands[i].vectype);   \
7177       if (val == FAIL)                                          \
7178         goto label;                                             \
7179                                                                 \
7180       inst.operands[i].reg = val;                               \
7181       inst.operands[i].isreg = 1;                               \
7182       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7183       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7184       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7185                              || rtype == REG_TYPE_VFD           \
7186                              || rtype == REG_TYPE_NQ);          \
7187       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7188     }                                                           \
7189   while (0)
7190
7191 #define po_imm_or_fail(min, max, popt)                          \
7192   do                                                            \
7193     {                                                           \
7194       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
7195         goto failure;                                           \
7196       inst.operands[i].imm = val;                               \
7197     }                                                           \
7198   while (0)
7199
7200 #define po_imm1_or_imm2_or_fail(imm1, imm2, popt)               \
7201   do                                                            \
7202     {                                                           \
7203       expressionS exp;                                          \
7204       my_get_expression (&exp, &str, popt);                     \
7205       if (exp.X_op != O_constant)                               \
7206         {                                                       \
7207           inst.error = _("constant expression required");       \
7208           goto failure;                                         \
7209         }                                                       \
7210       if (exp.X_add_number != imm1 && exp.X_add_number != imm2) \
7211         {                                                       \
7212           inst.error = _("immediate value 48 or 64 expected");  \
7213           goto failure;                                         \
7214         }                                                       \
7215       inst.operands[i].imm = exp.X_add_number;                  \
7216     }                                                           \
7217   while (0)
7218
7219 #define po_scalar_or_goto(elsz, label, reg_type)                        \
7220   do                                                                    \
7221     {                                                                   \
7222       val = parse_scalar (& str, elsz, & inst.operands[i].vectype,      \
7223                           reg_type);                                    \
7224       if (val == FAIL)                                                  \
7225         goto label;                                                     \
7226       inst.operands[i].reg = val;                                       \
7227       inst.operands[i].isscalar = 1;                                    \
7228     }                                                                   \
7229   while (0)
7230
7231 #define po_misc_or_fail(expr)                   \
7232   do                                            \
7233     {                                           \
7234       if (expr)                                 \
7235         goto failure;                           \
7236     }                                           \
7237   while (0)
7238
7239 #define po_misc_or_fail_no_backtrack(expr)              \
7240   do                                                    \
7241     {                                                   \
7242       result = expr;                                    \
7243       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
7244         backtrack_pos = 0;                              \
7245       if (result != PARSE_OPERAND_SUCCESS)              \
7246         goto failure;                                   \
7247     }                                                   \
7248   while (0)
7249
7250 #define po_barrier_or_imm(str)                             \
7251   do                                                       \
7252     {                                                      \
7253       val = parse_barrier (&str);                          \
7254       if (val == FAIL && ! ISALPHA (*str))                 \
7255         goto immediate;                                    \
7256       if (val == FAIL                                      \
7257           /* ISB can only take SY as an option.  */        \
7258           || ((inst.instruction & 0xf0) == 0x60            \
7259                && val != 0xf))                             \
7260         {                                                  \
7261            inst.error = _("invalid barrier type");         \
7262            backtrack_pos = 0;                              \
7263            goto failure;                                   \
7264         }                                                  \
7265     }                                                      \
7266   while (0)
7267
7268   skip_whitespace (str);
7269
7270   for (i = 0; upat[i] != OP_stop; i++)
7271     {
7272       op_parse_code = upat[i];
7273       if (op_parse_code >= 1<<16)
7274         op_parse_code = thumb ? (op_parse_code >> 16)
7275                                 : (op_parse_code & ((1<<16)-1));
7276
7277       if (op_parse_code >= OP_FIRST_OPTIONAL)
7278         {
7279           /* Remember where we are in case we need to backtrack.  */
7280           backtrack_pos = str;
7281           backtrack_error = inst.error;
7282           backtrack_index = i;
7283         }
7284
7285       if (i > 0 && (i > 1 || inst.operands[0].present))
7286         po_char_or_fail (',');
7287
7288       switch (op_parse_code)
7289         {
7290           /* Registers */
7291         case OP_oRRnpc:
7292         case OP_oRRnpcsp:
7293         case OP_RRnpc:
7294         case OP_RRnpcsp:
7295         case OP_oRR:
7296         case OP_RRe:
7297         case OP_RRo:
7298         case OP_LR:
7299         case OP_oLR:
7300         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
7301         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
7302         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
7303         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
7304         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
7305         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
7306         case OP_oRND:
7307         case OP_RNDMQR:
7308           po_reg_or_goto (REG_TYPE_RN, try_rndmq);
7309           break;
7310         try_rndmq:
7311         case OP_RNDMQ:
7312           po_reg_or_goto (REG_TYPE_MQ, try_rnd);
7313           break;
7314         try_rnd:
7315         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
7316         case OP_RVC:
7317           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
7318           break;
7319           /* Also accept generic coprocessor regs for unknown registers.  */
7320           coproc_reg:
7321           po_reg_or_goto (REG_TYPE_CN, vpr_po);
7322           break;
7323           /* Also accept P0 or p0 for VPR.P0.  Since P0 is already an
7324              existing register with a value of 0, this seems like the
7325              best way to parse P0.  */
7326           vpr_po:
7327           if (strncasecmp (str, "P0", 2) == 0)
7328             {
7329               str += 2;
7330               inst.operands[i].isreg = 1;
7331               inst.operands[i].reg = 13;
7332             }
7333           else
7334             goto failure;
7335           break;
7336         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
7337         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
7338         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
7339         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
7340         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
7341         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
7342         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
7343         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
7344         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
7345         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
7346         case OP_oRNQ:
7347         case OP_RNQMQ:
7348           po_reg_or_goto (REG_TYPE_MQ, try_nq);
7349           break;
7350         try_nq:
7351         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
7352         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
7353         case OP_RNDQMQR:
7354           po_reg_or_goto (REG_TYPE_RN, try_rndqmq);
7355           break;
7356         try_rndqmq:
7357         case OP_oRNDQMQ:
7358         case OP_RNDQMQ:
7359           po_reg_or_goto (REG_TYPE_MQ, try_rndq);
7360           break;
7361         try_rndq:
7362         case OP_oRNDQ:
7363         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
7364         case OP_RVSDMQ:
7365           po_reg_or_goto (REG_TYPE_MQ, try_rvsd);
7366           break;
7367         try_rvsd:
7368         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
7369         case OP_RVSD_COND:
7370           po_reg_or_goto (REG_TYPE_VFSD, try_cond);
7371           break;
7372         case OP_oRNSDQ:
7373         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
7374         case OP_RNSDQMQR:
7375           po_reg_or_goto (REG_TYPE_RN, try_mq);
7376           break;
7377           try_mq:
7378         case OP_oRNSDQMQ:
7379         case OP_RNSDQMQ:
7380           po_reg_or_goto (REG_TYPE_MQ, try_nsdq2);
7381           break;
7382           try_nsdq2:
7383           po_reg_or_fail (REG_TYPE_NSDQ);
7384           inst.error = 0;
7385           break;
7386         case OP_RMQRR:
7387           po_reg_or_goto (REG_TYPE_RN, try_rmq);
7388           break;
7389         try_rmq:
7390         case OP_RMQ:
7391           po_reg_or_fail (REG_TYPE_MQ);
7392           break;
7393         /* Neon scalar. Using an element size of 8 means that some invalid
7394            scalars are accepted here, so deal with those in later code.  */
7395         case OP_RNSC:  po_scalar_or_goto (8, failure, REG_TYPE_VFD);    break;
7396
7397         case OP_RNDQ_I0:
7398           {
7399             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
7400             break;
7401             try_imm0:
7402             po_imm_or_fail (0, 0, TRUE);
7403           }
7404           break;
7405
7406         case OP_RVSD_I0:
7407           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
7408           break;
7409
7410         case OP_RSVDMQ_FI0:
7411           po_reg_or_goto (REG_TYPE_MQ, try_rsvd_fi0);
7412           break;
7413         try_rsvd_fi0:
7414         case OP_RSVD_FI0:
7415           {
7416             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
7417             break;
7418             try_ifimm0:
7419             if (parse_ifimm_zero (&str))
7420               inst.operands[i].imm = 0;
7421             else
7422             {
7423               inst.error
7424                 = _("only floating point zero is allowed as immediate value");
7425               goto failure;
7426             }
7427           }
7428           break;
7429
7430         case OP_RR_RNSC:
7431           {
7432             po_scalar_or_goto (8, try_rr, REG_TYPE_VFD);
7433             break;
7434             try_rr:
7435             po_reg_or_fail (REG_TYPE_RN);
7436           }
7437           break;
7438
7439         case OP_RNSDQ_RNSC_MQ_RR:
7440           po_reg_or_goto (REG_TYPE_RN, try_rnsdq_rnsc_mq);
7441           break;
7442         try_rnsdq_rnsc_mq:
7443         case OP_RNSDQ_RNSC_MQ:
7444           po_reg_or_goto (REG_TYPE_MQ, try_rnsdq_rnsc);
7445           break;
7446         try_rnsdq_rnsc:
7447         case OP_RNSDQ_RNSC:
7448           {
7449             po_scalar_or_goto (8, try_nsdq, REG_TYPE_VFD);
7450             inst.error = 0;
7451             break;
7452             try_nsdq:
7453             po_reg_or_fail (REG_TYPE_NSDQ);
7454             inst.error = 0;
7455           }
7456           break;
7457
7458         case OP_RNSD_RNSC:
7459           {
7460             po_scalar_or_goto (8, try_s_scalar, REG_TYPE_VFD);
7461             break;
7462             try_s_scalar:
7463             po_scalar_or_goto (4, try_nsd, REG_TYPE_VFS);
7464             break;
7465             try_nsd:
7466             po_reg_or_fail (REG_TYPE_NSD);
7467           }
7468           break;
7469
7470         case OP_RNDQMQ_RNSC_RR:
7471           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc_rr);
7472           break;
7473         try_rndq_rnsc_rr:
7474         case OP_RNDQ_RNSC_RR:
7475           po_reg_or_goto (REG_TYPE_RN, try_rndq_rnsc);
7476           break;
7477         case OP_RNDQMQ_RNSC:
7478           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc);
7479           break;
7480         try_rndq_rnsc:
7481         case OP_RNDQ_RNSC:
7482           {
7483             po_scalar_or_goto (8, try_ndq, REG_TYPE_VFD);
7484             break;
7485             try_ndq:
7486             po_reg_or_fail (REG_TYPE_NDQ);
7487           }
7488           break;
7489
7490         case OP_RND_RNSC:
7491           {
7492             po_scalar_or_goto (8, try_vfd, REG_TYPE_VFD);
7493             break;
7494             try_vfd:
7495             po_reg_or_fail (REG_TYPE_VFD);
7496           }
7497           break;
7498
7499         case OP_VMOV:
7500           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
7501              not careful then bad things might happen.  */
7502           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
7503           break;
7504
7505         case OP_RNDQMQ_Ibig:
7506           po_reg_or_goto (REG_TYPE_MQ, try_rndq_ibig);
7507           break;
7508         try_rndq_ibig:
7509         case OP_RNDQ_Ibig:
7510           {
7511             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
7512             break;
7513             try_immbig:
7514             /* There's a possibility of getting a 64-bit immediate here, so
7515                we need special handling.  */
7516             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
7517                 == FAIL)
7518               {
7519                 inst.error = _("immediate value is out of range");
7520                 goto failure;
7521               }
7522           }
7523           break;
7524
7525         case OP_RNDQMQ_I63b_RR:
7526           po_reg_or_goto (REG_TYPE_MQ, try_rndq_i63b_rr);
7527           break;
7528         try_rndq_i63b_rr:
7529           po_reg_or_goto (REG_TYPE_RN, try_rndq_i63b);
7530           break;
7531         try_rndq_i63b:
7532         case OP_RNDQ_I63b:
7533           {
7534             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
7535             break;
7536             try_shimm:
7537             po_imm_or_fail (0, 63, TRUE);
7538           }
7539           break;
7540
7541         case OP_RRnpcb:
7542           po_char_or_fail ('[');
7543           po_reg_or_fail  (REG_TYPE_RN);
7544           po_char_or_fail (']');
7545           break;
7546
7547         case OP_RRnpctw:
7548         case OP_RRw:
7549         case OP_oRRw:
7550           po_reg_or_fail (REG_TYPE_RN);
7551           if (skip_past_char (&str, '!') == SUCCESS)
7552             inst.operands[i].writeback = 1;
7553           break;
7554
7555           /* Immediates */
7556         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
7557         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
7558         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
7559         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
7560         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
7561         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
7562         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
7563         case OP_I48_I64: po_imm1_or_imm2_or_fail (48, 64, FALSE); break;
7564         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
7565         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
7566         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
7567         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
7568         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
7569
7570         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
7571         case OP_oI7b:
7572         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
7573         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
7574         case OP_oI31b:
7575         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
7576         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
7577         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
7578         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
7579
7580           /* Immediate variants */
7581         case OP_oI255c:
7582           po_char_or_fail ('{');
7583           po_imm_or_fail (0, 255, TRUE);
7584           po_char_or_fail ('}');
7585           break;
7586
7587         case OP_I31w:
7588           /* The expression parser chokes on a trailing !, so we have
7589              to find it first and zap it.  */
7590           {
7591             char *s = str;
7592             while (*s && *s != ',')
7593               s++;
7594             if (s[-1] == '!')
7595               {
7596                 s[-1] = '\0';
7597                 inst.operands[i].writeback = 1;
7598               }
7599             po_imm_or_fail (0, 31, TRUE);
7600             if (str == s - 1)
7601               str = s;
7602           }
7603           break;
7604
7605           /* Expressions */
7606         case OP_EXPi:   EXPi:
7607           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7608                                               GE_OPT_PREFIX));
7609           break;
7610
7611         case OP_EXP:
7612           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7613                                               GE_NO_PREFIX));
7614           break;
7615
7616         case OP_EXPr:   EXPr:
7617           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7618                                               GE_NO_PREFIX));
7619           if (inst.relocs[0].exp.X_op == O_symbol)
7620             {
7621               val = parse_reloc (&str);
7622               if (val == -1)
7623                 {
7624                   inst.error = _("unrecognized relocation suffix");
7625                   goto failure;
7626                 }
7627               else if (val != BFD_RELOC_UNUSED)
7628                 {
7629                   inst.operands[i].imm = val;
7630                   inst.operands[i].hasreloc = 1;
7631                 }
7632             }
7633           break;
7634
7635         case OP_EXPs:
7636           po_misc_or_fail (my_get_expression (&inst.relocs[i].exp, &str,
7637                                               GE_NO_PREFIX));
7638           if (inst.relocs[i].exp.X_op == O_symbol)
7639             {
7640               inst.operands[i].hasreloc = 1;
7641             }
7642           else if (inst.relocs[i].exp.X_op == O_constant)
7643             {
7644               inst.operands[i].imm = inst.relocs[i].exp.X_add_number;
7645               inst.operands[i].hasreloc = 0;
7646             }
7647           break;
7648
7649           /* Operand for MOVW or MOVT.  */
7650         case OP_HALF:
7651           po_misc_or_fail (parse_half (&str));
7652           break;
7653
7654           /* Register or expression.  */
7655         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7656         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7657
7658           /* Register or immediate.  */
7659         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7660         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7661
7662         case OP_RRnpcsp_I32: po_reg_or_goto (REG_TYPE_RN, I32); break;
7663         I32:                 po_imm_or_fail (1, 32, FALSE);     break;
7664
7665         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7666         IF:
7667           if (!is_immediate_prefix (*str))
7668             goto bad_args;
7669           str++;
7670           val = parse_fpa_immediate (&str);
7671           if (val == FAIL)
7672             goto failure;
7673           /* FPA immediates are encoded as registers 8-15.
7674              parse_fpa_immediate has already applied the offset.  */
7675           inst.operands[i].reg = val;
7676           inst.operands[i].isreg = 1;
7677           break;
7678
7679         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7680         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7681
7682           /* Two kinds of register.  */
7683         case OP_RIWR_RIWC:
7684           {
7685             struct reg_entry *rege = arm_reg_parse_multi (&str);
7686             if (!rege
7687                 || (rege->type != REG_TYPE_MMXWR
7688                     && rege->type != REG_TYPE_MMXWC
7689                     && rege->type != REG_TYPE_MMXWCG))
7690               {
7691                 inst.error = _("iWMMXt data or control register expected");
7692                 goto failure;
7693               }
7694             inst.operands[i].reg = rege->number;
7695             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7696           }
7697           break;
7698
7699         case OP_RIWC_RIWG:
7700           {
7701             struct reg_entry *rege = arm_reg_parse_multi (&str);
7702             if (!rege
7703                 || (rege->type != REG_TYPE_MMXWC
7704                     && rege->type != REG_TYPE_MMXWCG))
7705               {
7706                 inst.error = _("iWMMXt control register expected");
7707                 goto failure;
7708               }
7709             inst.operands[i].reg = rege->number;
7710             inst.operands[i].isreg = 1;
7711           }
7712           break;
7713
7714           /* Misc */
7715         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7716         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7717         case OP_oROR:    val = parse_ror (&str);                break;
7718         try_cond:
7719         case OP_COND:    val = parse_cond (&str);               break;
7720         case OP_oBARRIER_I15:
7721           po_barrier_or_imm (str); break;
7722           immediate:
7723           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7724             goto failure;
7725           break;
7726
7727         case OP_wPSR:
7728         case OP_rPSR:
7729           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7730           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7731             {
7732               inst.error = _("Banked registers are not available with this "
7733                              "architecture.");
7734               goto failure;
7735             }
7736           break;
7737           try_psr:
7738           val = parse_psr (&str, op_parse_code == OP_wPSR);
7739           break;
7740
7741         case OP_VLDR:
7742           po_reg_or_goto (REG_TYPE_VFSD, try_sysreg);
7743           break;
7744         try_sysreg:
7745           val = parse_sys_vldr_vstr (&str);
7746           break;
7747
7748         case OP_APSR_RR:
7749           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7750           break;
7751           try_apsr:
7752           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7753              instruction).  */
7754           if (strncasecmp (str, "APSR_", 5) == 0)
7755             {
7756               unsigned found = 0;
7757               str += 5;
7758               while (found < 15)
7759                 switch (*str++)
7760                   {
7761                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7762                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7763                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7764                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7765                   default: found = 16;
7766                   }
7767               if (found != 15)
7768                 goto failure;
7769               inst.operands[i].isvec = 1;
7770               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7771               inst.operands[i].reg = REG_PC;
7772             }
7773           else
7774             goto failure;
7775           break;
7776
7777         case OP_TB:
7778           po_misc_or_fail (parse_tb (&str));
7779           break;
7780
7781           /* Register lists.  */
7782         case OP_REGLST:
7783           val = parse_reg_list (&str, REGLIST_RN);
7784           if (*str == '^')
7785             {
7786               inst.operands[i].writeback = 1;
7787               str++;
7788             }
7789           break;
7790
7791         case OP_CLRMLST:
7792           val = parse_reg_list (&str, REGLIST_CLRM);
7793           break;
7794
7795         case OP_VRSLST:
7796           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S,
7797                                     &partial_match);
7798           break;
7799
7800         case OP_VRDLST:
7801           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D,
7802                                     &partial_match);
7803           break;
7804
7805         case OP_VRSDLST:
7806           /* Allow Q registers too.  */
7807           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7808                                     REGLIST_NEON_D, &partial_match);
7809           if (val == FAIL)
7810             {
7811               inst.error = NULL;
7812               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7813                                         REGLIST_VFP_S, &partial_match);
7814               inst.operands[i].issingle = 1;
7815             }
7816           break;
7817
7818         case OP_VRSDVLST:
7819           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7820                                     REGLIST_VFP_D_VPR, &partial_match);
7821           if (val == FAIL && !partial_match)
7822             {
7823               inst.error = NULL;
7824               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7825                                         REGLIST_VFP_S_VPR, &partial_match);
7826               inst.operands[i].issingle = 1;
7827             }
7828           break;
7829
7830         case OP_NRDLST:
7831           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7832                                     REGLIST_NEON_D, &partial_match);
7833           break;
7834
7835         case OP_MSTRLST4:
7836         case OP_MSTRLST2:
7837           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7838                                            1, &inst.operands[i].vectype);
7839           if (val != (((op_parse_code == OP_MSTRLST2) ? 3 : 7) << 5 | 0xe))
7840             goto failure;
7841           break;
7842         case OP_NSTRLST:
7843           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7844                                            0, &inst.operands[i].vectype);
7845           break;
7846
7847           /* Addressing modes */
7848         case OP_ADDRMVE:
7849           po_misc_or_fail (parse_address_group_reloc (&str, i, GROUP_MVE));
7850           break;
7851
7852         case OP_ADDR:
7853           po_misc_or_fail (parse_address (&str, i));
7854           break;
7855
7856         case OP_ADDRGLDR:
7857           po_misc_or_fail_no_backtrack (
7858             parse_address_group_reloc (&str, i, GROUP_LDR));
7859           break;
7860
7861         case OP_ADDRGLDRS:
7862           po_misc_or_fail_no_backtrack (
7863             parse_address_group_reloc (&str, i, GROUP_LDRS));
7864           break;
7865
7866         case OP_ADDRGLDC:
7867           po_misc_or_fail_no_backtrack (
7868             parse_address_group_reloc (&str, i, GROUP_LDC));
7869           break;
7870
7871         case OP_SH:
7872           po_misc_or_fail (parse_shifter_operand (&str, i));
7873           break;
7874
7875         case OP_SHG:
7876           po_misc_or_fail_no_backtrack (
7877             parse_shifter_operand_group_reloc (&str, i));
7878           break;
7879
7880         case OP_oSHll:
7881           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7882           break;
7883
7884         case OP_oSHar:
7885           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7886           break;
7887
7888         case OP_oSHllar:
7889           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7890           break;
7891
7892         case OP_RMQRZ:
7893         case OP_oRMQRZ:
7894           po_reg_or_goto (REG_TYPE_MQ, try_rr_zr);
7895           break;
7896
7897         case OP_RR_ZR:
7898         try_rr_zr:
7899           po_reg_or_goto (REG_TYPE_RN, ZR);
7900           break;
7901         ZR:
7902           po_reg_or_fail (REG_TYPE_ZR);
7903           break;
7904
7905         default:
7906           as_fatal (_("unhandled operand code %d"), op_parse_code);
7907         }
7908
7909       /* Various value-based sanity checks and shared operations.  We
7910          do not signal immediate failures for the register constraints;
7911          this allows a syntax error to take precedence.  */
7912       switch (op_parse_code)
7913         {
7914         case OP_oRRnpc:
7915         case OP_RRnpc:
7916         case OP_RRnpcb:
7917         case OP_RRw:
7918         case OP_oRRw:
7919         case OP_RRnpc_I0:
7920           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7921             inst.error = BAD_PC;
7922           break;
7923
7924         case OP_oRRnpcsp:
7925         case OP_RRnpcsp:
7926         case OP_RRnpcsp_I32:
7927           if (inst.operands[i].isreg)
7928             {
7929               if (inst.operands[i].reg == REG_PC)
7930                 inst.error = BAD_PC;
7931               else if (inst.operands[i].reg == REG_SP
7932                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7933                           relaxed since ARMv8-A.  */
7934                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7935                 {
7936                   gas_assert (thumb);
7937                   inst.error = BAD_SP;
7938                 }
7939             }
7940           break;
7941
7942         case OP_RRnpctw:
7943           if (inst.operands[i].isreg
7944               && inst.operands[i].reg == REG_PC
7945               && (inst.operands[i].writeback || thumb))
7946             inst.error = BAD_PC;
7947           break;
7948
7949         case OP_RVSD_COND:
7950         case OP_VLDR:
7951           if (inst.operands[i].isreg)
7952             break;
7953         /* fall through.  */
7954
7955         case OP_CPSF:
7956         case OP_ENDI:
7957         case OP_oROR:
7958         case OP_wPSR:
7959         case OP_rPSR:
7960         case OP_COND:
7961         case OP_oBARRIER_I15:
7962         case OP_REGLST:
7963         case OP_CLRMLST:
7964         case OP_VRSLST:
7965         case OP_VRDLST:
7966         case OP_VRSDLST:
7967         case OP_VRSDVLST:
7968         case OP_NRDLST:
7969         case OP_NSTRLST:
7970         case OP_MSTRLST2:
7971         case OP_MSTRLST4:
7972           if (val == FAIL)
7973             goto failure;
7974           inst.operands[i].imm = val;
7975           break;
7976
7977         case OP_LR:
7978         case OP_oLR:
7979           if (inst.operands[i].reg != REG_LR)
7980             inst.error = _("operand must be LR register");
7981           break;
7982
7983         case OP_RMQRZ:
7984         case OP_oRMQRZ:
7985         case OP_RR_ZR:
7986           if (!inst.operands[i].iszr && inst.operands[i].reg == REG_PC)
7987             inst.error = BAD_PC;
7988           break;
7989
7990         case OP_RRe:
7991           if (inst.operands[i].isreg
7992               && (inst.operands[i].reg & 0x00000001) != 0)
7993             inst.error = BAD_ODD;
7994           break;
7995
7996         case OP_RRo:
7997           if (inst.operands[i].isreg)
7998             {
7999               if ((inst.operands[i].reg & 0x00000001) != 1)
8000                 inst.error = BAD_EVEN;
8001               else if (inst.operands[i].reg == REG_SP)
8002                 as_tsktsk (MVE_BAD_SP);
8003               else if (inst.operands[i].reg == REG_PC)
8004                 inst.error = BAD_PC;
8005             }
8006           break;
8007
8008         default:
8009           break;
8010         }
8011
8012       /* If we get here, this operand was successfully parsed.  */
8013       inst.operands[i].present = 1;
8014       continue;
8015
8016     bad_args:
8017       inst.error = BAD_ARGS;
8018
8019     failure:
8020       if (!backtrack_pos)
8021         {
8022           /* The parse routine should already have set inst.error, but set a
8023              default here just in case.  */
8024           if (!inst.error)
8025             inst.error = BAD_SYNTAX;
8026           return FAIL;
8027         }
8028
8029       /* Do not backtrack over a trailing optional argument that
8030          absorbed some text.  We will only fail again, with the
8031          'garbage following instruction' error message, which is
8032          probably less helpful than the current one.  */
8033       if (backtrack_index == i && backtrack_pos != str
8034           && upat[i+1] == OP_stop)
8035         {
8036           if (!inst.error)
8037             inst.error = BAD_SYNTAX;
8038           return FAIL;
8039         }
8040
8041       /* Try again, skipping the optional argument at backtrack_pos.  */
8042       str = backtrack_pos;
8043       inst.error = backtrack_error;
8044       inst.operands[backtrack_index].present = 0;
8045       i = backtrack_index;
8046       backtrack_pos = 0;
8047     }
8048
8049   /* Check that we have parsed all the arguments.  */
8050   if (*str != '\0' && !inst.error)
8051     inst.error = _("garbage following instruction");
8052
8053   return inst.error ? FAIL : SUCCESS;
8054 }
8055
8056 #undef po_char_or_fail
8057 #undef po_reg_or_fail
8058 #undef po_reg_or_goto
8059 #undef po_imm_or_fail
8060 #undef po_scalar_or_fail
8061 #undef po_barrier_or_imm
8062
8063 /* Shorthand macro for instruction encoding functions issuing errors.  */
8064 #define constraint(expr, err)                   \
8065   do                                            \
8066     {                                           \
8067       if (expr)                                 \
8068         {                                       \
8069           inst.error = err;                     \
8070           return;                               \
8071         }                                       \
8072     }                                           \
8073   while (0)
8074
8075 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
8076    instructions are unpredictable if these registers are used.  This
8077    is the BadReg predicate in ARM's Thumb-2 documentation.
8078
8079    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
8080    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
8081 #define reject_bad_reg(reg)                                     \
8082   do                                                            \
8083    if (reg == REG_PC)                                           \
8084      {                                                          \
8085        inst.error = BAD_PC;                                     \
8086        return;                                                  \
8087      }                                                          \
8088    else if (reg == REG_SP                                       \
8089             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
8090      {                                                          \
8091        inst.error = BAD_SP;                                     \
8092        return;                                                  \
8093      }                                                          \
8094   while (0)
8095
8096 /* If REG is R13 (the stack pointer), warn that its use is
8097    deprecated.  */
8098 #define warn_deprecated_sp(reg)                 \
8099   do                                            \
8100     if (warn_on_deprecated && reg == REG_SP)    \
8101        as_tsktsk (_("use of r13 is deprecated"));       \
8102   while (0)
8103
8104 /* Functions for operand encoding.  ARM, then Thumb.  */
8105
8106 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
8107
8108 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
8109
8110    The only binary encoding difference is the Coprocessor number.  Coprocessor
8111    9 is used for half-precision calculations or conversions.  The format of the
8112    instruction is the same as the equivalent Coprocessor 10 instruction that
8113    exists for Single-Precision operation.  */
8114
8115 static void
8116 do_scalar_fp16_v82_encode (void)
8117 {
8118   if (inst.cond < COND_ALWAYS)
8119     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
8120                " the behaviour is UNPREDICTABLE"));
8121   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
8122               _(BAD_FP16));
8123
8124   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
8125   mark_feature_used (&arm_ext_fp16);
8126 }
8127
8128 /* If VAL can be encoded in the immediate field of an ARM instruction,
8129    return the encoded form.  Otherwise, return FAIL.  */
8130
8131 static unsigned int
8132 encode_arm_immediate (unsigned int val)
8133 {
8134   unsigned int a, i;
8135
8136   if (val <= 0xff)
8137     return val;
8138
8139   for (i = 2; i < 32; i += 2)
8140     if ((a = rotate_left (val, i)) <= 0xff)
8141       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
8142
8143   return FAIL;
8144 }
8145
8146 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
8147    return the encoded form.  Otherwise, return FAIL.  */
8148 static unsigned int
8149 encode_thumb32_immediate (unsigned int val)
8150 {
8151   unsigned int a, i;
8152
8153   if (val <= 0xff)
8154     return val;
8155
8156   for (i = 1; i <= 24; i++)
8157     {
8158       a = val >> i;
8159       if ((val & ~(0xff << i)) == 0)
8160         return ((val >> i) & 0x7f) | ((32 - i) << 7);
8161     }
8162
8163   a = val & 0xff;
8164   if (val == ((a << 16) | a))
8165     return 0x100 | a;
8166   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
8167     return 0x300 | a;
8168
8169   a = val & 0xff00;
8170   if (val == ((a << 16) | a))
8171     return 0x200 | (a >> 8);
8172
8173   return FAIL;
8174 }
8175 /* Encode a VFP SP or DP register number into inst.instruction.  */
8176
8177 static void
8178 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
8179 {
8180   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
8181       && reg > 15)
8182     {
8183       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
8184         {
8185           if (thumb_mode)
8186             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8187                                     fpu_vfp_ext_d32);
8188           else
8189             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
8190                                     fpu_vfp_ext_d32);
8191         }
8192       else
8193         {
8194           first_error (_("D register out of range for selected VFP version"));
8195           return;
8196         }
8197     }
8198
8199   switch (pos)
8200     {
8201     case VFP_REG_Sd:
8202       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
8203       break;
8204
8205     case VFP_REG_Sn:
8206       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
8207       break;
8208
8209     case VFP_REG_Sm:
8210       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
8211       break;
8212
8213     case VFP_REG_Dd:
8214       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
8215       break;
8216
8217     case VFP_REG_Dn:
8218       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
8219       break;
8220
8221     case VFP_REG_Dm:
8222       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
8223       break;
8224
8225     default:
8226       abort ();
8227     }
8228 }
8229
8230 /* Encode a <shift> in an ARM-format instruction.  The immediate,
8231    if any, is handled by md_apply_fix.   */
8232 static void
8233 encode_arm_shift (int i)
8234 {
8235   /* register-shifted register.  */
8236   if (inst.operands[i].immisreg)
8237     {
8238       int op_index;
8239       for (op_index = 0; op_index <= i; ++op_index)
8240         {
8241           /* Check the operand only when it's presented.  In pre-UAL syntax,
8242              if the destination register is the same as the first operand, two
8243              register form of the instruction can be used.  */
8244           if (inst.operands[op_index].present && inst.operands[op_index].isreg
8245               && inst.operands[op_index].reg == REG_PC)
8246             as_warn (UNPRED_REG ("r15"));
8247         }
8248
8249       if (inst.operands[i].imm == REG_PC)
8250         as_warn (UNPRED_REG ("r15"));
8251     }
8252
8253   if (inst.operands[i].shift_kind == SHIFT_RRX)
8254     inst.instruction |= SHIFT_ROR << 5;
8255   else
8256     {
8257       inst.instruction |= inst.operands[i].shift_kind << 5;
8258       if (inst.operands[i].immisreg)
8259         {
8260           inst.instruction |= SHIFT_BY_REG;
8261           inst.instruction |= inst.operands[i].imm << 8;
8262         }
8263       else
8264         inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8265     }
8266 }
8267
8268 static void
8269 encode_arm_shifter_operand (int i)
8270 {
8271   if (inst.operands[i].isreg)
8272     {
8273       inst.instruction |= inst.operands[i].reg;
8274       encode_arm_shift (i);
8275     }
8276   else
8277     {
8278       inst.instruction |= INST_IMMEDIATE;
8279       if (inst.relocs[0].type != BFD_RELOC_ARM_IMMEDIATE)
8280         inst.instruction |= inst.operands[i].imm;
8281     }
8282 }
8283
8284 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
8285 static void
8286 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
8287 {
8288   /* PR 14260:
8289      Generate an error if the operand is not a register.  */
8290   constraint (!inst.operands[i].isreg,
8291               _("Instruction does not support =N addresses"));
8292
8293   inst.instruction |= inst.operands[i].reg << 16;
8294
8295   if (inst.operands[i].preind)
8296     {
8297       if (is_t)
8298         {
8299           inst.error = _("instruction does not accept preindexed addressing");
8300           return;
8301         }
8302       inst.instruction |= PRE_INDEX;
8303       if (inst.operands[i].writeback)
8304         inst.instruction |= WRITE_BACK;
8305
8306     }
8307   else if (inst.operands[i].postind)
8308     {
8309       gas_assert (inst.operands[i].writeback);
8310       if (is_t)
8311         inst.instruction |= WRITE_BACK;
8312     }
8313   else /* unindexed - only for coprocessor */
8314     {
8315       inst.error = _("instruction does not accept unindexed addressing");
8316       return;
8317     }
8318
8319   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
8320       && (((inst.instruction & 0x000f0000) >> 16)
8321           == ((inst.instruction & 0x0000f000) >> 12)))
8322     as_warn ((inst.instruction & LOAD_BIT)
8323              ? _("destination register same as write-back base")
8324              : _("source register same as write-back base"));
8325 }
8326
8327 /* inst.operands[i] was set up by parse_address.  Encode it into an
8328    ARM-format mode 2 load or store instruction.  If is_t is true,
8329    reject forms that cannot be used with a T instruction (i.e. not
8330    post-indexed).  */
8331 static void
8332 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
8333 {
8334   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
8335
8336   encode_arm_addr_mode_common (i, is_t);
8337
8338   if (inst.operands[i].immisreg)
8339     {
8340       constraint ((inst.operands[i].imm == REG_PC
8341                    || (is_pc && inst.operands[i].writeback)),
8342                   BAD_PC_ADDRESSING);
8343       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
8344       inst.instruction |= inst.operands[i].imm;
8345       if (!inst.operands[i].negative)
8346         inst.instruction |= INDEX_UP;
8347       if (inst.operands[i].shifted)
8348         {
8349           if (inst.operands[i].shift_kind == SHIFT_RRX)
8350             inst.instruction |= SHIFT_ROR << 5;
8351           else
8352             {
8353               inst.instruction |= inst.operands[i].shift_kind << 5;
8354               inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8355             }
8356         }
8357     }
8358   else /* immediate offset in inst.relocs[0] */
8359     {
8360       if (is_pc && !inst.relocs[0].pc_rel)
8361         {
8362           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
8363
8364           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
8365              cannot use PC in addressing.
8366              PC cannot be used in writeback addressing, either.  */
8367           constraint ((is_t || inst.operands[i].writeback),
8368                       BAD_PC_ADDRESSING);
8369
8370           /* Use of PC in str is deprecated for ARMv7.  */
8371           if (warn_on_deprecated
8372               && !is_load
8373               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
8374             as_tsktsk (_("use of PC in this instruction is deprecated"));
8375         }
8376
8377       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8378         {
8379           /* Prefer + for zero encoded value.  */
8380           if (!inst.operands[i].negative)
8381             inst.instruction |= INDEX_UP;
8382           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM;
8383         }
8384     }
8385 }
8386
8387 /* inst.operands[i] was set up by parse_address.  Encode it into an
8388    ARM-format mode 3 load or store instruction.  Reject forms that
8389    cannot be used with such instructions.  If is_t is true, reject
8390    forms that cannot be used with a T instruction (i.e. not
8391    post-indexed).  */
8392 static void
8393 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
8394 {
8395   if (inst.operands[i].immisreg && inst.operands[i].shifted)
8396     {
8397       inst.error = _("instruction does not accept scaled register index");
8398       return;
8399     }
8400
8401   encode_arm_addr_mode_common (i, is_t);
8402
8403   if (inst.operands[i].immisreg)
8404     {
8405       constraint ((inst.operands[i].imm == REG_PC
8406                    || (is_t && inst.operands[i].reg == REG_PC)),
8407                   BAD_PC_ADDRESSING);
8408       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
8409                   BAD_PC_WRITEBACK);
8410       inst.instruction |= inst.operands[i].imm;
8411       if (!inst.operands[i].negative)
8412         inst.instruction |= INDEX_UP;
8413     }
8414   else /* immediate offset in inst.relocs[0] */
8415     {
8416       constraint ((inst.operands[i].reg == REG_PC && !inst.relocs[0].pc_rel
8417                    && inst.operands[i].writeback),
8418                   BAD_PC_WRITEBACK);
8419       inst.instruction |= HWOFFSET_IMM;
8420       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8421         {
8422           /* Prefer + for zero encoded value.  */
8423           if (!inst.operands[i].negative)
8424             inst.instruction |= INDEX_UP;
8425
8426           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM8;
8427         }
8428     }
8429 }
8430
8431 /* Write immediate bits [7:0] to the following locations:
8432
8433   |28/24|23     19|18 16|15                    4|3     0|
8434   |  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|
8435
8436   This function is used by VMOV/VMVN/VORR/VBIC.  */
8437
8438 static void
8439 neon_write_immbits (unsigned immbits)
8440 {
8441   inst.instruction |= immbits & 0xf;
8442   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
8443   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
8444 }
8445
8446 /* Invert low-order SIZE bits of XHI:XLO.  */
8447
8448 static void
8449 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
8450 {
8451   unsigned immlo = xlo ? *xlo : 0;
8452   unsigned immhi = xhi ? *xhi : 0;
8453
8454   switch (size)
8455     {
8456     case 8:
8457       immlo = (~immlo) & 0xff;
8458       break;
8459
8460     case 16:
8461       immlo = (~immlo) & 0xffff;
8462       break;
8463
8464     case 64:
8465       immhi = (~immhi) & 0xffffffff;
8466       /* fall through.  */
8467
8468     case 32:
8469       immlo = (~immlo) & 0xffffffff;
8470       break;
8471
8472     default:
8473       abort ();
8474     }
8475
8476   if (xlo)
8477     *xlo = immlo;
8478
8479   if (xhi)
8480     *xhi = immhi;
8481 }
8482
8483 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
8484    A, B, C, D.  */
8485
8486 static int
8487 neon_bits_same_in_bytes (unsigned imm)
8488 {
8489   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
8490          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
8491          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
8492          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
8493 }
8494
8495 /* For immediate of above form, return 0bABCD.  */
8496
8497 static unsigned
8498 neon_squash_bits (unsigned imm)
8499 {
8500   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
8501          | ((imm & 0x01000000) >> 21);
8502 }
8503
8504 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
8505
8506 static unsigned
8507 neon_qfloat_bits (unsigned imm)
8508 {
8509   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
8510 }
8511
8512 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
8513    the instruction. *OP is passed as the initial value of the op field, and
8514    may be set to a different value depending on the constant (i.e.
8515    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
8516    MVN).  If the immediate looks like a repeated pattern then also
8517    try smaller element sizes.  */
8518
8519 static int
8520 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
8521                          unsigned *immbits, int *op, int size,
8522                          enum neon_el_type type)
8523 {
8524   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
8525      float.  */
8526   if (type == NT_float && !float_p)
8527     return FAIL;
8528
8529   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
8530     {
8531       if (size != 32 || *op == 1)
8532         return FAIL;
8533       *immbits = neon_qfloat_bits (immlo);
8534       return 0xf;
8535     }
8536
8537   if (size == 64)
8538     {
8539       if (neon_bits_same_in_bytes (immhi)
8540           && neon_bits_same_in_bytes (immlo))
8541         {
8542           if (*op == 1)
8543             return FAIL;
8544           *immbits = (neon_squash_bits (immhi) << 4)
8545                      | neon_squash_bits (immlo);
8546           *op = 1;
8547           return 0xe;
8548         }
8549
8550       if (immhi != immlo)
8551         return FAIL;
8552     }
8553
8554   if (size >= 32)
8555     {
8556       if (immlo == (immlo & 0x000000ff))
8557         {
8558           *immbits = immlo;
8559           return 0x0;
8560         }
8561       else if (immlo == (immlo & 0x0000ff00))
8562         {
8563           *immbits = immlo >> 8;
8564           return 0x2;
8565         }
8566       else if (immlo == (immlo & 0x00ff0000))
8567         {
8568           *immbits = immlo >> 16;
8569           return 0x4;
8570         }
8571       else if (immlo == (immlo & 0xff000000))
8572         {
8573           *immbits = immlo >> 24;
8574           return 0x6;
8575         }
8576       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
8577         {
8578           *immbits = (immlo >> 8) & 0xff;
8579           return 0xc;
8580         }
8581       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
8582         {
8583           *immbits = (immlo >> 16) & 0xff;
8584           return 0xd;
8585         }
8586
8587       if ((immlo & 0xffff) != (immlo >> 16))
8588         return FAIL;
8589       immlo &= 0xffff;
8590     }
8591
8592   if (size >= 16)
8593     {
8594       if (immlo == (immlo & 0x000000ff))
8595         {
8596           *immbits = immlo;
8597           return 0x8;
8598         }
8599       else if (immlo == (immlo & 0x0000ff00))
8600         {
8601           *immbits = immlo >> 8;
8602           return 0xa;
8603         }
8604
8605       if ((immlo & 0xff) != (immlo >> 8))
8606         return FAIL;
8607       immlo &= 0xff;
8608     }
8609
8610   if (immlo == (immlo & 0x000000ff))
8611     {
8612       /* Don't allow MVN with 8-bit immediate.  */
8613       if (*op == 1)
8614         return FAIL;
8615       *immbits = immlo;
8616       return 0xe;
8617     }
8618
8619   return FAIL;
8620 }
8621
8622 #if defined BFD_HOST_64_BIT
8623 /* Returns TRUE if double precision value V may be cast
8624    to single precision without loss of accuracy.  */
8625
8626 static bfd_boolean
8627 is_double_a_single (bfd_int64_t v)
8628 {
8629   int exp = (int)((v >> 52) & 0x7FF);
8630   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8631
8632   return (exp == 0 || exp == 0x7FF
8633           || (exp >= 1023 - 126 && exp <= 1023 + 127))
8634     && (mantissa & 0x1FFFFFFFl) == 0;
8635 }
8636
8637 /* Returns a double precision value casted to single precision
8638    (ignoring the least significant bits in exponent and mantissa).  */
8639
8640 static int
8641 double_to_single (bfd_int64_t v)
8642 {
8643   int sign = (int) ((v >> 63) & 1l);
8644   int exp = (int) ((v >> 52) & 0x7FF);
8645   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8646
8647   if (exp == 0x7FF)
8648     exp = 0xFF;
8649   else
8650     {
8651       exp = exp - 1023 + 127;
8652       if (exp >= 0xFF)
8653         {
8654           /* Infinity.  */
8655           exp = 0x7F;
8656           mantissa = 0;
8657         }
8658       else if (exp < 0)
8659         {
8660           /* No denormalized numbers.  */
8661           exp = 0;
8662           mantissa = 0;
8663         }
8664     }
8665   mantissa >>= 29;
8666   return (sign << 31) | (exp << 23) | mantissa;
8667 }
8668 #endif /* BFD_HOST_64_BIT */
8669
8670 enum lit_type
8671 {
8672   CONST_THUMB,
8673   CONST_ARM,
8674   CONST_VEC
8675 };
8676
8677 static void do_vfp_nsyn_opcode (const char *);
8678
8679 /* inst.relocs[0].exp describes an "=expr" load pseudo-operation.
8680    Determine whether it can be performed with a move instruction; if
8681    it can, convert inst.instruction to that move instruction and
8682    return TRUE; if it can't, convert inst.instruction to a literal-pool
8683    load and return FALSE.  If this is not a valid thing to do in the
8684    current context, set inst.error and return TRUE.
8685
8686    inst.operands[i] describes the destination register.  */
8687
8688 static bfd_boolean
8689 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
8690 {
8691   unsigned long tbit;
8692   bfd_boolean thumb_p = (t == CONST_THUMB);
8693   bfd_boolean arm_p   = (t == CONST_ARM);
8694
8695   if (thumb_p)
8696     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
8697   else
8698     tbit = LOAD_BIT;
8699
8700   if ((inst.instruction & tbit) == 0)
8701     {
8702       inst.error = _("invalid pseudo operation");
8703       return TRUE;
8704     }
8705
8706   if (inst.relocs[0].exp.X_op != O_constant
8707       && inst.relocs[0].exp.X_op != O_symbol
8708       && inst.relocs[0].exp.X_op != O_big)
8709     {
8710       inst.error = _("constant expression expected");
8711       return TRUE;
8712     }
8713
8714   if (inst.relocs[0].exp.X_op == O_constant
8715       || inst.relocs[0].exp.X_op == O_big)
8716     {
8717 #if defined BFD_HOST_64_BIT
8718       bfd_int64_t v;
8719 #else
8720       offsetT v;
8721 #endif
8722       if (inst.relocs[0].exp.X_op == O_big)
8723         {
8724           LITTLENUM_TYPE w[X_PRECISION];
8725           LITTLENUM_TYPE * l;
8726
8727           if (inst.relocs[0].exp.X_add_number == -1)
8728             {
8729               gen_to_words (w, X_PRECISION, E_PRECISION);
8730               l = w;
8731               /* FIXME: Should we check words w[2..5] ?  */
8732             }
8733           else
8734             l = generic_bignum;
8735
8736 #if defined BFD_HOST_64_BIT
8737           v =
8738             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8739                   << LITTLENUM_NUMBER_OF_BITS)
8740                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8741                 << LITTLENUM_NUMBER_OF_BITS)
8742                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8743               << LITTLENUM_NUMBER_OF_BITS)
8744              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8745 #else
8746           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8747             |  (l[0] & LITTLENUM_MASK);
8748 #endif
8749         }
8750       else
8751         v = inst.relocs[0].exp.X_add_number;
8752
8753       if (!inst.operands[i].issingle)
8754         {
8755           if (thumb_p)
8756             {
8757               /* LDR should not use lead in a flag-setting instruction being
8758                  chosen so we do not check whether movs can be used.  */
8759
8760               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8761                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8762                   && inst.operands[i].reg != 13
8763                   && inst.operands[i].reg != 15)
8764                 {
8765                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8766                      movw instruction.  */
8767                   unsigned int newimm;
8768                   bfd_boolean isNegated;
8769
8770                   newimm = encode_thumb32_immediate (v);
8771                   if (newimm != (unsigned int) FAIL)
8772                     isNegated = FALSE;
8773                   else
8774                     {
8775                       newimm = encode_thumb32_immediate (~v);
8776                       if (newimm != (unsigned int) FAIL)
8777                         isNegated = TRUE;
8778                     }
8779
8780                   /* The number can be loaded with a mov.w or mvn
8781                      instruction.  */
8782                   if (newimm != (unsigned int) FAIL
8783                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8784                     {
8785                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8786                                           | (inst.operands[i].reg << 8));
8787                       /* Change to MOVN.  */
8788                       inst.instruction |= (isNegated ? 0x200000 : 0);
8789                       inst.instruction |= (newimm & 0x800) << 15;
8790                       inst.instruction |= (newimm & 0x700) << 4;
8791                       inst.instruction |= (newimm & 0x0ff);
8792                       return TRUE;
8793                     }
8794                   /* The number can be loaded with a movw instruction.  */
8795                   else if ((v & ~0xFFFF) == 0
8796                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8797                     {
8798                       int imm = v & 0xFFFF;
8799
8800                       inst.instruction = 0xf2400000;  /* MOVW.  */
8801                       inst.instruction |= (inst.operands[i].reg << 8);
8802                       inst.instruction |= (imm & 0xf000) << 4;
8803                       inst.instruction |= (imm & 0x0800) << 15;
8804                       inst.instruction |= (imm & 0x0700) << 4;
8805                       inst.instruction |= (imm & 0x00ff);
8806                       /*  In case this replacement is being done on Armv8-M
8807                           Baseline we need to make sure to disable the
8808                           instruction size check, as otherwise GAS will reject
8809                           the use of this T32 instruction.  */
8810                       inst.size_req = 0;
8811                       return TRUE;
8812                     }
8813                 }
8814             }
8815           else if (arm_p)
8816             {
8817               int value = encode_arm_immediate (v);
8818
8819               if (value != FAIL)
8820                 {
8821                   /* This can be done with a mov instruction.  */
8822                   inst.instruction &= LITERAL_MASK;
8823                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8824                   inst.instruction |= value & 0xfff;
8825                   return TRUE;
8826                 }
8827
8828               value = encode_arm_immediate (~ v);
8829               if (value != FAIL)
8830                 {
8831                   /* This can be done with a mvn instruction.  */
8832                   inst.instruction &= LITERAL_MASK;
8833                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8834                   inst.instruction |= value & 0xfff;
8835                   return TRUE;
8836                 }
8837             }
8838           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8839             {
8840               int op = 0;
8841               unsigned immbits = 0;
8842               unsigned immlo = inst.operands[1].imm;
8843               unsigned immhi = inst.operands[1].regisimm
8844                 ? inst.operands[1].reg
8845                 : inst.relocs[0].exp.X_unsigned
8846                 ? 0
8847                 : ((bfd_int64_t)((int) immlo)) >> 32;
8848               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8849                                                    &op, 64, NT_invtype);
8850
8851               if (cmode == FAIL)
8852                 {
8853                   neon_invert_size (&immlo, &immhi, 64);
8854                   op = !op;
8855                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8856                                                    &op, 64, NT_invtype);
8857                 }
8858
8859               if (cmode != FAIL)
8860                 {
8861                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8862                     | (1 << 23)
8863                     | (cmode << 8)
8864                     | (op << 5)
8865                     | (1 << 4);
8866
8867                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8868                   if (thumb_mode)
8869                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8870                   else
8871                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8872                   neon_write_immbits (immbits);
8873                   return TRUE;
8874                 }
8875             }
8876         }
8877
8878       if (t == CONST_VEC)
8879         {
8880           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8881           if (inst.operands[i].issingle
8882               && is_quarter_float (inst.operands[1].imm)
8883               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8884             {
8885               inst.operands[1].imm =
8886                 neon_qfloat_bits (v);
8887               do_vfp_nsyn_opcode ("fconsts");
8888               return TRUE;
8889             }
8890
8891           /* If our host does not support a 64-bit type then we cannot perform
8892              the following optimization.  This mean that there will be a
8893              discrepancy between the output produced by an assembler built for
8894              a 32-bit-only host and the output produced from a 64-bit host, but
8895              this cannot be helped.  */
8896 #if defined BFD_HOST_64_BIT
8897           else if (!inst.operands[1].issingle
8898                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8899             {
8900               if (is_double_a_single (v)
8901                   && is_quarter_float (double_to_single (v)))
8902                 {
8903                   inst.operands[1].imm =
8904                     neon_qfloat_bits (double_to_single (v));
8905                   do_vfp_nsyn_opcode ("fconstd");
8906                   return TRUE;
8907                 }
8908             }
8909 #endif
8910         }
8911     }
8912
8913   if (add_to_lit_pool ((!inst.operands[i].isvec
8914                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8915     return TRUE;
8916
8917   inst.operands[1].reg = REG_PC;
8918   inst.operands[1].isreg = 1;
8919   inst.operands[1].preind = 1;
8920   inst.relocs[0].pc_rel = 1;
8921   inst.relocs[0].type = (thumb_p
8922                      ? BFD_RELOC_ARM_THUMB_OFFSET
8923                      : (mode_3
8924                         ? BFD_RELOC_ARM_HWLITERAL
8925                         : BFD_RELOC_ARM_LITERAL));
8926   return FALSE;
8927 }
8928
8929 /* inst.operands[i] was set up by parse_address.  Encode it into an
8930    ARM-format instruction.  Reject all forms which cannot be encoded
8931    into a coprocessor load/store instruction.  If wb_ok is false,
8932    reject use of writeback; if unind_ok is false, reject use of
8933    unindexed addressing.  If reloc_override is not 0, use it instead
8934    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8935    (in which case it is preserved).  */
8936
8937 static int
8938 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8939 {
8940   if (!inst.operands[i].isreg)
8941     {
8942       /* PR 18256 */
8943       if (! inst.operands[0].isvec)
8944         {
8945           inst.error = _("invalid co-processor operand");
8946           return FAIL;
8947         }
8948       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8949         return SUCCESS;
8950     }
8951
8952   inst.instruction |= inst.operands[i].reg << 16;
8953
8954   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8955
8956   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8957     {
8958       gas_assert (!inst.operands[i].writeback);
8959       if (!unind_ok)
8960         {
8961           inst.error = _("instruction does not support unindexed addressing");
8962           return FAIL;
8963         }
8964       inst.instruction |= inst.operands[i].imm;
8965       inst.instruction |= INDEX_UP;
8966       return SUCCESS;
8967     }
8968
8969   if (inst.operands[i].preind)
8970     inst.instruction |= PRE_INDEX;
8971
8972   if (inst.operands[i].writeback)
8973     {
8974       if (inst.operands[i].reg == REG_PC)
8975         {
8976           inst.error = _("pc may not be used with write-back");
8977           return FAIL;
8978         }
8979       if (!wb_ok)
8980         {
8981           inst.error = _("instruction does not support writeback");
8982           return FAIL;
8983         }
8984       inst.instruction |= WRITE_BACK;
8985     }
8986
8987   if (reloc_override)
8988     inst.relocs[0].type = (bfd_reloc_code_real_type) reloc_override;
8989   else if ((inst.relocs[0].type < BFD_RELOC_ARM_ALU_PC_G0_NC
8990             || inst.relocs[0].type > BFD_RELOC_ARM_LDC_SB_G2)
8991            && inst.relocs[0].type != BFD_RELOC_ARM_LDR_PC_G0)
8992     {
8993       if (thumb_mode)
8994         inst.relocs[0].type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8995       else
8996         inst.relocs[0].type = BFD_RELOC_ARM_CP_OFF_IMM;
8997     }
8998
8999   /* Prefer + for zero encoded value.  */
9000   if (!inst.operands[i].negative)
9001     inst.instruction |= INDEX_UP;
9002
9003   return SUCCESS;
9004 }
9005
9006 /* Functions for instruction encoding, sorted by sub-architecture.
9007    First some generics; their names are taken from the conventional
9008    bit positions for register arguments in ARM format instructions.  */
9009
9010 static void
9011 do_noargs (void)
9012 {
9013 }
9014
9015 static void
9016 do_rd (void)
9017 {
9018   inst.instruction |= inst.operands[0].reg << 12;
9019 }
9020
9021 static void
9022 do_rn (void)
9023 {
9024   inst.instruction |= inst.operands[0].reg << 16;
9025 }
9026
9027 static void
9028 do_rd_rm (void)
9029 {
9030   inst.instruction |= inst.operands[0].reg << 12;
9031   inst.instruction |= inst.operands[1].reg;
9032 }
9033
9034 static void
9035 do_rm_rn (void)
9036 {
9037   inst.instruction |= inst.operands[0].reg;
9038   inst.instruction |= inst.operands[1].reg << 16;
9039 }
9040
9041 static void
9042 do_rd_rn (void)
9043 {
9044   inst.instruction |= inst.operands[0].reg << 12;
9045   inst.instruction |= inst.operands[1].reg << 16;
9046 }
9047
9048 static void
9049 do_rn_rd (void)
9050 {
9051   inst.instruction |= inst.operands[0].reg << 16;
9052   inst.instruction |= inst.operands[1].reg << 12;
9053 }
9054
9055 static void
9056 do_tt (void)
9057 {
9058   inst.instruction |= inst.operands[0].reg << 8;
9059   inst.instruction |= inst.operands[1].reg << 16;
9060 }
9061
9062 static bfd_boolean
9063 check_obsolete (const arm_feature_set *feature, const char *msg)
9064 {
9065   if (ARM_CPU_IS_ANY (cpu_variant))
9066     {
9067       as_tsktsk ("%s", msg);
9068       return TRUE;
9069     }
9070   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
9071     {
9072       as_bad ("%s", msg);
9073       return TRUE;
9074     }
9075
9076   return FALSE;
9077 }
9078
9079 static void
9080 do_rd_rm_rn (void)
9081 {
9082   unsigned Rn = inst.operands[2].reg;
9083   /* Enforce restrictions on SWP instruction.  */
9084   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
9085     {
9086       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
9087                   _("Rn must not overlap other operands"));
9088
9089       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
9090        */
9091       if (!check_obsolete (&arm_ext_v8,
9092                            _("swp{b} use is obsoleted for ARMv8 and later"))
9093           && warn_on_deprecated
9094           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
9095         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
9096     }
9097
9098   inst.instruction |= inst.operands[0].reg << 12;
9099   inst.instruction |= inst.operands[1].reg;
9100   inst.instruction |= Rn << 16;
9101 }
9102
9103 static void
9104 do_rd_rn_rm (void)
9105 {
9106   inst.instruction |= inst.operands[0].reg << 12;
9107   inst.instruction |= inst.operands[1].reg << 16;
9108   inst.instruction |= inst.operands[2].reg;
9109 }
9110
9111 static void
9112 do_rm_rd_rn (void)
9113 {
9114   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
9115   constraint (((inst.relocs[0].exp.X_op != O_constant
9116                 && inst.relocs[0].exp.X_op != O_illegal)
9117                || inst.relocs[0].exp.X_add_number != 0),
9118               BAD_ADDR_MODE);
9119   inst.instruction |= inst.operands[0].reg;
9120   inst.instruction |= inst.operands[1].reg << 12;
9121   inst.instruction |= inst.operands[2].reg << 16;
9122 }
9123
9124 static void
9125 do_imm0 (void)
9126 {
9127   inst.instruction |= inst.operands[0].imm;
9128 }
9129
9130 static void
9131 do_rd_cpaddr (void)
9132 {
9133   inst.instruction |= inst.operands[0].reg << 12;
9134   encode_arm_cp_address (1, TRUE, TRUE, 0);
9135 }
9136
9137 /* ARM instructions, in alphabetical order by function name (except
9138    that wrapper functions appear immediately after the function they
9139    wrap).  */
9140
9141 /* This is a pseudo-op of the form "adr rd, label" to be converted
9142    into a relative address of the form "add rd, pc, #label-.-8".  */
9143
9144 static void
9145 do_adr (void)
9146 {
9147   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9148
9149   /* Frag hacking will turn this into a sub instruction if the offset turns
9150      out to be negative.  */
9151   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
9152   inst.relocs[0].pc_rel = 1;
9153   inst.relocs[0].exp.X_add_number -= 8;
9154
9155   if (support_interwork
9156       && inst.relocs[0].exp.X_op == O_symbol
9157       && inst.relocs[0].exp.X_add_symbol != NULL
9158       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9159       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9160     inst.relocs[0].exp.X_add_number |= 1;
9161 }
9162
9163 /* This is a pseudo-op of the form "adrl rd, label" to be converted
9164    into a relative address of the form:
9165    add rd, pc, #low(label-.-8)"
9166    add rd, rd, #high(label-.-8)"  */
9167
9168 static void
9169 do_adrl (void)
9170 {
9171   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9172
9173   /* Frag hacking will turn this into a sub instruction if the offset turns
9174      out to be negative.  */
9175   inst.relocs[0].type          = BFD_RELOC_ARM_ADRL_IMMEDIATE;
9176   inst.relocs[0].pc_rel        = 1;
9177   inst.size                    = INSN_SIZE * 2;
9178   inst.relocs[0].exp.X_add_number -= 8;
9179
9180   if (support_interwork
9181       && inst.relocs[0].exp.X_op == O_symbol
9182       && inst.relocs[0].exp.X_add_symbol != NULL
9183       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9184       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9185     inst.relocs[0].exp.X_add_number |= 1;
9186 }
9187
9188 static void
9189 do_arit (void)
9190 {
9191   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9192               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9193               THUMB1_RELOC_ONLY);
9194   if (!inst.operands[1].present)
9195     inst.operands[1].reg = inst.operands[0].reg;
9196   inst.instruction |= inst.operands[0].reg << 12;
9197   inst.instruction |= inst.operands[1].reg << 16;
9198   encode_arm_shifter_operand (2);
9199 }
9200
9201 static void
9202 do_barrier (void)
9203 {
9204   if (inst.operands[0].present)
9205     inst.instruction |= inst.operands[0].imm;
9206   else
9207     inst.instruction |= 0xf;
9208 }
9209
9210 static void
9211 do_bfc (void)
9212 {
9213   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9214   constraint (msb > 32, _("bit-field extends past end of register"));
9215   /* The instruction encoding stores the LSB and MSB,
9216      not the LSB and width.  */
9217   inst.instruction |= inst.operands[0].reg << 12;
9218   inst.instruction |= inst.operands[1].imm << 7;
9219   inst.instruction |= (msb - 1) << 16;
9220 }
9221
9222 static void
9223 do_bfi (void)
9224 {
9225   unsigned int msb;
9226
9227   /* #0 in second position is alternative syntax for bfc, which is
9228      the same instruction but with REG_PC in the Rm field.  */
9229   if (!inst.operands[1].isreg)
9230     inst.operands[1].reg = REG_PC;
9231
9232   msb = inst.operands[2].imm + inst.operands[3].imm;
9233   constraint (msb > 32, _("bit-field extends past end of register"));
9234   /* The instruction encoding stores the LSB and MSB,
9235      not the LSB and width.  */
9236   inst.instruction |= inst.operands[0].reg << 12;
9237   inst.instruction |= inst.operands[1].reg;
9238   inst.instruction |= inst.operands[2].imm << 7;
9239   inst.instruction |= (msb - 1) << 16;
9240 }
9241
9242 static void
9243 do_bfx (void)
9244 {
9245   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9246               _("bit-field extends past end of register"));
9247   inst.instruction |= inst.operands[0].reg << 12;
9248   inst.instruction |= inst.operands[1].reg;
9249   inst.instruction |= inst.operands[2].imm << 7;
9250   inst.instruction |= (inst.operands[3].imm - 1) << 16;
9251 }
9252
9253 /* ARM V5 breakpoint instruction (argument parse)
9254      BKPT <16 bit unsigned immediate>
9255      Instruction is not conditional.
9256         The bit pattern given in insns[] has the COND_ALWAYS condition,
9257         and it is an error if the caller tried to override that.  */
9258
9259 static void
9260 do_bkpt (void)
9261 {
9262   /* Top 12 of 16 bits to bits 19:8.  */
9263   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
9264
9265   /* Bottom 4 of 16 bits to bits 3:0.  */
9266   inst.instruction |= inst.operands[0].imm & 0xf;
9267 }
9268
9269 static void
9270 encode_branch (int default_reloc)
9271 {
9272   if (inst.operands[0].hasreloc)
9273     {
9274       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
9275                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
9276                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
9277       inst.relocs[0].type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
9278         ? BFD_RELOC_ARM_PLT32
9279         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
9280     }
9281   else
9282     inst.relocs[0].type = (bfd_reloc_code_real_type) default_reloc;
9283   inst.relocs[0].pc_rel = 1;
9284 }
9285
9286 static void
9287 do_branch (void)
9288 {
9289 #ifdef OBJ_ELF
9290   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9291     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9292   else
9293 #endif
9294     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9295 }
9296
9297 static void
9298 do_bl (void)
9299 {
9300 #ifdef OBJ_ELF
9301   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9302     {
9303       if (inst.cond == COND_ALWAYS)
9304         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
9305       else
9306         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9307     }
9308   else
9309 #endif
9310     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9311 }
9312
9313 /* ARM V5 branch-link-exchange instruction (argument parse)
9314      BLX <target_addr>          ie BLX(1)
9315      BLX{<condition>} <Rm>      ie BLX(2)
9316    Unfortunately, there are two different opcodes for this mnemonic.
9317    So, the insns[].value is not used, and the code here zaps values
9318         into inst.instruction.
9319    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
9320
9321 static void
9322 do_blx (void)
9323 {
9324   if (inst.operands[0].isreg)
9325     {
9326       /* Arg is a register; the opcode provided by insns[] is correct.
9327          It is not illegal to do "blx pc", just useless.  */
9328       if (inst.operands[0].reg == REG_PC)
9329         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
9330
9331       inst.instruction |= inst.operands[0].reg;
9332     }
9333   else
9334     {
9335       /* Arg is an address; this instruction cannot be executed
9336          conditionally, and the opcode must be adjusted.
9337          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
9338          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
9339       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9340       inst.instruction = 0xfa000000;
9341       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
9342     }
9343 }
9344
9345 static void
9346 do_bx (void)
9347 {
9348   bfd_boolean want_reloc;
9349
9350   if (inst.operands[0].reg == REG_PC)
9351     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
9352
9353   inst.instruction |= inst.operands[0].reg;
9354   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
9355      it is for ARMv4t or earlier.  */
9356   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
9357   if (!ARM_FEATURE_ZERO (selected_object_arch)
9358       && !ARM_CPU_HAS_FEATURE (selected_object_arch, arm_ext_v5))
9359       want_reloc = TRUE;
9360
9361 #ifdef OBJ_ELF
9362   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
9363 #endif
9364     want_reloc = FALSE;
9365
9366   if (want_reloc)
9367     inst.relocs[0].type = BFD_RELOC_ARM_V4BX;
9368 }
9369
9370
9371 /* ARM v5TEJ.  Jump to Jazelle code.  */
9372
9373 static void
9374 do_bxj (void)
9375 {
9376   if (inst.operands[0].reg == REG_PC)
9377     as_tsktsk (_("use of r15 in bxj is not really useful"));
9378
9379   inst.instruction |= inst.operands[0].reg;
9380 }
9381
9382 /* Co-processor data operation:
9383       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
9384       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
9385 static void
9386 do_cdp (void)
9387 {
9388   inst.instruction |= inst.operands[0].reg << 8;
9389   inst.instruction |= inst.operands[1].imm << 20;
9390   inst.instruction |= inst.operands[2].reg << 12;
9391   inst.instruction |= inst.operands[3].reg << 16;
9392   inst.instruction |= inst.operands[4].reg;
9393   inst.instruction |= inst.operands[5].imm << 5;
9394 }
9395
9396 static void
9397 do_cmp (void)
9398 {
9399   inst.instruction |= inst.operands[0].reg << 16;
9400   encode_arm_shifter_operand (1);
9401 }
9402
9403 /* Transfer between coprocessor and ARM registers.
9404    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
9405    MRC2
9406    MCR{cond}
9407    MCR2
9408
9409    No special properties.  */
9410
9411 struct deprecated_coproc_regs_s
9412 {
9413   unsigned cp;
9414   int opc1;
9415   unsigned crn;
9416   unsigned crm;
9417   int opc2;
9418   arm_feature_set deprecated;
9419   arm_feature_set obsoleted;
9420   const char *dep_msg;
9421   const char *obs_msg;
9422 };
9423
9424 #define DEPR_ACCESS_V8 \
9425   N_("This coprocessor register access is deprecated in ARMv8")
9426
9427 /* Table of all deprecated coprocessor registers.  */
9428 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
9429 {
9430     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
9431      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9432      DEPR_ACCESS_V8, NULL},
9433     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
9434      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9435      DEPR_ACCESS_V8, NULL},
9436     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
9437      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9438      DEPR_ACCESS_V8, NULL},
9439     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
9440      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9441      DEPR_ACCESS_V8, NULL},
9442     {14, 6, 0,  0, 0,                                   /* TEECR.  */
9443      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9444      DEPR_ACCESS_V8, NULL},
9445 };
9446
9447 #undef DEPR_ACCESS_V8
9448
9449 static const size_t deprecated_coproc_reg_count =
9450   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
9451
9452 static void
9453 do_co_reg (void)
9454 {
9455   unsigned Rd;
9456   size_t i;
9457
9458   Rd = inst.operands[2].reg;
9459   if (thumb_mode)
9460     {
9461       if (inst.instruction == 0xee000010
9462           || inst.instruction == 0xfe000010)
9463         /* MCR, MCR2  */
9464         reject_bad_reg (Rd);
9465       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9466         /* MRC, MRC2  */
9467         constraint (Rd == REG_SP, BAD_SP);
9468     }
9469   else
9470     {
9471       /* MCR */
9472       if (inst.instruction == 0xe000010)
9473         constraint (Rd == REG_PC, BAD_PC);
9474     }
9475
9476     for (i = 0; i < deprecated_coproc_reg_count; ++i)
9477       {
9478         const struct deprecated_coproc_regs_s *r =
9479           deprecated_coproc_regs + i;
9480
9481         if (inst.operands[0].reg == r->cp
9482             && inst.operands[1].imm == r->opc1
9483             && inst.operands[3].reg == r->crn
9484             && inst.operands[4].reg == r->crm
9485             && inst.operands[5].imm == r->opc2)
9486           {
9487             if (! ARM_CPU_IS_ANY (cpu_variant)
9488                 && warn_on_deprecated
9489                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
9490               as_tsktsk ("%s", r->dep_msg);
9491           }
9492       }
9493
9494   inst.instruction |= inst.operands[0].reg << 8;
9495   inst.instruction |= inst.operands[1].imm << 21;
9496   inst.instruction |= Rd << 12;
9497   inst.instruction |= inst.operands[3].reg << 16;
9498   inst.instruction |= inst.operands[4].reg;
9499   inst.instruction |= inst.operands[5].imm << 5;
9500 }
9501
9502 /* Transfer between coprocessor register and pair of ARM registers.
9503    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
9504    MCRR2
9505    MRRC{cond}
9506    MRRC2
9507
9508    Two XScale instructions are special cases of these:
9509
9510      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
9511      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
9512
9513    Result unpredictable if Rd or Rn is R15.  */
9514
9515 static void
9516 do_co_reg2c (void)
9517 {
9518   unsigned Rd, Rn;
9519
9520   Rd = inst.operands[2].reg;
9521   Rn = inst.operands[3].reg;
9522
9523   if (thumb_mode)
9524     {
9525       reject_bad_reg (Rd);
9526       reject_bad_reg (Rn);
9527     }
9528   else
9529     {
9530       constraint (Rd == REG_PC, BAD_PC);
9531       constraint (Rn == REG_PC, BAD_PC);
9532     }
9533
9534   /* Only check the MRRC{2} variants.  */
9535   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
9536     {
9537        /* If Rd == Rn, error that the operation is
9538           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
9539        constraint (Rd == Rn, BAD_OVERLAP);
9540     }
9541
9542   inst.instruction |= inst.operands[0].reg << 8;
9543   inst.instruction |= inst.operands[1].imm << 4;
9544   inst.instruction |= Rd << 12;
9545   inst.instruction |= Rn << 16;
9546   inst.instruction |= inst.operands[4].reg;
9547 }
9548
9549 static void
9550 do_cpsi (void)
9551 {
9552   inst.instruction |= inst.operands[0].imm << 6;
9553   if (inst.operands[1].present)
9554     {
9555       inst.instruction |= CPSI_MMOD;
9556       inst.instruction |= inst.operands[1].imm;
9557     }
9558 }
9559
9560 static void
9561 do_dbg (void)
9562 {
9563   inst.instruction |= inst.operands[0].imm;
9564 }
9565
9566 static void
9567 do_div (void)
9568 {
9569   unsigned Rd, Rn, Rm;
9570
9571   Rd = inst.operands[0].reg;
9572   Rn = (inst.operands[1].present
9573         ? inst.operands[1].reg : Rd);
9574   Rm = inst.operands[2].reg;
9575
9576   constraint ((Rd == REG_PC), BAD_PC);
9577   constraint ((Rn == REG_PC), BAD_PC);
9578   constraint ((Rm == REG_PC), BAD_PC);
9579
9580   inst.instruction |= Rd << 16;
9581   inst.instruction |= Rn << 0;
9582   inst.instruction |= Rm << 8;
9583 }
9584
9585 static void
9586 do_it (void)
9587 {
9588   /* There is no IT instruction in ARM mode.  We
9589      process it to do the validation as if in
9590      thumb mode, just in case the code gets
9591      assembled for thumb using the unified syntax.  */
9592
9593   inst.size = 0;
9594   if (unified_syntax)
9595     {
9596       set_pred_insn_type (IT_INSN);
9597       now_pred.mask = (inst.instruction & 0xf) | 0x10;
9598       now_pred.cc = inst.operands[0].imm;
9599     }
9600 }
9601
9602 /* If there is only one register in the register list,
9603    then return its register number.  Otherwise return -1.  */
9604 static int
9605 only_one_reg_in_list (int range)
9606 {
9607   int i = ffs (range) - 1;
9608   return (i > 15 || range != (1 << i)) ? -1 : i;
9609 }
9610
9611 static void
9612 encode_ldmstm(int from_push_pop_mnem)
9613 {
9614   int base_reg = inst.operands[0].reg;
9615   int range = inst.operands[1].imm;
9616   int one_reg;
9617
9618   inst.instruction |= base_reg << 16;
9619   inst.instruction |= range;
9620
9621   if (inst.operands[1].writeback)
9622     inst.instruction |= LDM_TYPE_2_OR_3;
9623
9624   if (inst.operands[0].writeback)
9625     {
9626       inst.instruction |= WRITE_BACK;
9627       /* Check for unpredictable uses of writeback.  */
9628       if (inst.instruction & LOAD_BIT)
9629         {
9630           /* Not allowed in LDM type 2.  */
9631           if ((inst.instruction & LDM_TYPE_2_OR_3)
9632               && ((range & (1 << REG_PC)) == 0))
9633             as_warn (_("writeback of base register is UNPREDICTABLE"));
9634           /* Only allowed if base reg not in list for other types.  */
9635           else if (range & (1 << base_reg))
9636             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
9637         }
9638       else /* STM.  */
9639         {
9640           /* Not allowed for type 2.  */
9641           if (inst.instruction & LDM_TYPE_2_OR_3)
9642             as_warn (_("writeback of base register is UNPREDICTABLE"));
9643           /* Only allowed if base reg not in list, or first in list.  */
9644           else if ((range & (1 << base_reg))
9645                    && (range & ((1 << base_reg) - 1)))
9646             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
9647         }
9648     }
9649
9650   /* If PUSH/POP has only one register, then use the A2 encoding.  */
9651   one_reg = only_one_reg_in_list (range);
9652   if (from_push_pop_mnem && one_reg >= 0)
9653     {
9654       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
9655
9656       if (is_push && one_reg == 13 /* SP */)
9657         /* PR 22483: The A2 encoding cannot be used when
9658            pushing the stack pointer as this is UNPREDICTABLE.  */
9659         return;
9660
9661       inst.instruction &= A_COND_MASK;
9662       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
9663       inst.instruction |= one_reg << 12;
9664     }
9665 }
9666
9667 static void
9668 do_ldmstm (void)
9669 {
9670   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
9671 }
9672
9673 /* ARMv5TE load-consecutive (argument parse)
9674    Mode is like LDRH.
9675
9676      LDRccD R, mode
9677      STRccD R, mode.  */
9678
9679 static void
9680 do_ldrd (void)
9681 {
9682   constraint (inst.operands[0].reg % 2 != 0,
9683               _("first transfer register must be even"));
9684   constraint (inst.operands[1].present
9685               && inst.operands[1].reg != inst.operands[0].reg + 1,
9686               _("can only transfer two consecutive registers"));
9687   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9688   constraint (!inst.operands[2].isreg, _("'[' expected"));
9689
9690   if (!inst.operands[1].present)
9691     inst.operands[1].reg = inst.operands[0].reg + 1;
9692
9693   /* encode_arm_addr_mode_3 will diagnose overlap between the base
9694      register and the first register written; we have to diagnose
9695      overlap between the base and the second register written here.  */
9696
9697   if (inst.operands[2].reg == inst.operands[1].reg
9698       && (inst.operands[2].writeback || inst.operands[2].postind))
9699     as_warn (_("base register written back, and overlaps "
9700                "second transfer register"));
9701
9702   if (!(inst.instruction & V4_STR_BIT))
9703     {
9704       /* For an index-register load, the index register must not overlap the
9705         destination (even if not write-back).  */
9706       if (inst.operands[2].immisreg
9707               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
9708               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
9709         as_warn (_("index register overlaps transfer register"));
9710     }
9711   inst.instruction |= inst.operands[0].reg << 12;
9712   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
9713 }
9714
9715 static void
9716 do_ldrex (void)
9717 {
9718   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
9719               || inst.operands[1].postind || inst.operands[1].writeback
9720               || inst.operands[1].immisreg || inst.operands[1].shifted
9721               || inst.operands[1].negative
9722               /* This can arise if the programmer has written
9723                    strex rN, rM, foo
9724                  or if they have mistakenly used a register name as the last
9725                  operand,  eg:
9726                    strex rN, rM, rX
9727                  It is very difficult to distinguish between these two cases
9728                  because "rX" might actually be a label. ie the register
9729                  name has been occluded by a symbol of the same name. So we
9730                  just generate a general 'bad addressing mode' type error
9731                  message and leave it up to the programmer to discover the
9732                  true cause and fix their mistake.  */
9733               || (inst.operands[1].reg == REG_PC),
9734               BAD_ADDR_MODE);
9735
9736   constraint (inst.relocs[0].exp.X_op != O_constant
9737               || inst.relocs[0].exp.X_add_number != 0,
9738               _("offset must be zero in ARM encoding"));
9739
9740   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
9741
9742   inst.instruction |= inst.operands[0].reg << 12;
9743   inst.instruction |= inst.operands[1].reg << 16;
9744   inst.relocs[0].type = BFD_RELOC_UNUSED;
9745 }
9746
9747 static void
9748 do_ldrexd (void)
9749 {
9750   constraint (inst.operands[0].reg % 2 != 0,
9751               _("even register required"));
9752   constraint (inst.operands[1].present
9753               && inst.operands[1].reg != inst.operands[0].reg + 1,
9754               _("can only load two consecutive registers"));
9755   /* If op 1 were present and equal to PC, this function wouldn't
9756      have been called in the first place.  */
9757   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9758
9759   inst.instruction |= inst.operands[0].reg << 12;
9760   inst.instruction |= inst.operands[2].reg << 16;
9761 }
9762
9763 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9764    which is not a multiple of four is UNPREDICTABLE.  */
9765 static void
9766 check_ldr_r15_aligned (void)
9767 {
9768   constraint (!(inst.operands[1].immisreg)
9769               && (inst.operands[0].reg == REG_PC
9770               && inst.operands[1].reg == REG_PC
9771               && (inst.relocs[0].exp.X_add_number & 0x3)),
9772               _("ldr to register 15 must be 4-byte aligned"));
9773 }
9774
9775 static void
9776 do_ldst (void)
9777 {
9778   inst.instruction |= inst.operands[0].reg << 12;
9779   if (!inst.operands[1].isreg)
9780     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9781       return;
9782   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9783   check_ldr_r15_aligned ();
9784 }
9785
9786 static void
9787 do_ldstt (void)
9788 {
9789   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9790      reject [Rn,...].  */
9791   if (inst.operands[1].preind)
9792     {
9793       constraint (inst.relocs[0].exp.X_op != O_constant
9794                   || inst.relocs[0].exp.X_add_number != 0,
9795                   _("this instruction requires a post-indexed address"));
9796
9797       inst.operands[1].preind = 0;
9798       inst.operands[1].postind = 1;
9799       inst.operands[1].writeback = 1;
9800     }
9801   inst.instruction |= inst.operands[0].reg << 12;
9802   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9803 }
9804
9805 /* Halfword and signed-byte load/store operations.  */
9806
9807 static void
9808 do_ldstv4 (void)
9809 {
9810   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9811   inst.instruction |= inst.operands[0].reg << 12;
9812   if (!inst.operands[1].isreg)
9813     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9814       return;
9815   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9816 }
9817
9818 static void
9819 do_ldsttv4 (void)
9820 {
9821   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9822      reject [Rn,...].  */
9823   if (inst.operands[1].preind)
9824     {
9825       constraint (inst.relocs[0].exp.X_op != O_constant
9826                   || inst.relocs[0].exp.X_add_number != 0,
9827                   _("this instruction requires a post-indexed address"));
9828
9829       inst.operands[1].preind = 0;
9830       inst.operands[1].postind = 1;
9831       inst.operands[1].writeback = 1;
9832     }
9833   inst.instruction |= inst.operands[0].reg << 12;
9834   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9835 }
9836
9837 /* Co-processor register load/store.
9838    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9839 static void
9840 do_lstc (void)
9841 {
9842   inst.instruction |= inst.operands[0].reg << 8;
9843   inst.instruction |= inst.operands[1].reg << 12;
9844   encode_arm_cp_address (2, TRUE, TRUE, 0);
9845 }
9846
9847 static void
9848 do_mlas (void)
9849 {
9850   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9851   if (inst.operands[0].reg == inst.operands[1].reg
9852       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9853       && !(inst.instruction & 0x00400000))
9854     as_tsktsk (_("Rd and Rm should be different in mla"));
9855
9856   inst.instruction |= inst.operands[0].reg << 16;
9857   inst.instruction |= inst.operands[1].reg;
9858   inst.instruction |= inst.operands[2].reg << 8;
9859   inst.instruction |= inst.operands[3].reg << 12;
9860 }
9861
9862 static void
9863 do_mov (void)
9864 {
9865   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9866               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9867               THUMB1_RELOC_ONLY);
9868   inst.instruction |= inst.operands[0].reg << 12;
9869   encode_arm_shifter_operand (1);
9870 }
9871
9872 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9873 static void
9874 do_mov16 (void)
9875 {
9876   bfd_vma imm;
9877   bfd_boolean top;
9878
9879   top = (inst.instruction & 0x00400000) != 0;
9880   constraint (top && inst.relocs[0].type == BFD_RELOC_ARM_MOVW,
9881               _(":lower16: not allowed in this instruction"));
9882   constraint (!top && inst.relocs[0].type == BFD_RELOC_ARM_MOVT,
9883               _(":upper16: not allowed in this instruction"));
9884   inst.instruction |= inst.operands[0].reg << 12;
9885   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
9886     {
9887       imm = inst.relocs[0].exp.X_add_number;
9888       /* The value is in two pieces: 0:11, 16:19.  */
9889       inst.instruction |= (imm & 0x00000fff);
9890       inst.instruction |= (imm & 0x0000f000) << 4;
9891     }
9892 }
9893
9894 static int
9895 do_vfp_nsyn_mrs (void)
9896 {
9897   if (inst.operands[0].isvec)
9898     {
9899       if (inst.operands[1].reg != 1)
9900         first_error (_("operand 1 must be FPSCR"));
9901       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9902       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9903       do_vfp_nsyn_opcode ("fmstat");
9904     }
9905   else if (inst.operands[1].isvec)
9906     do_vfp_nsyn_opcode ("fmrx");
9907   else
9908     return FAIL;
9909
9910   return SUCCESS;
9911 }
9912
9913 static int
9914 do_vfp_nsyn_msr (void)
9915 {
9916   if (inst.operands[0].isvec)
9917     do_vfp_nsyn_opcode ("fmxr");
9918   else
9919     return FAIL;
9920
9921   return SUCCESS;
9922 }
9923
9924 static void
9925 do_vmrs (void)
9926 {
9927   unsigned Rt = inst.operands[0].reg;
9928
9929   if (thumb_mode && Rt == REG_SP)
9930     {
9931       inst.error = BAD_SP;
9932       return;
9933     }
9934
9935   switch (inst.operands[1].reg)
9936     {
9937     /* MVFR2 is only valid for Armv8-A.  */
9938     case 5:
9939       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9940                   _(BAD_FPU));
9941       break;
9942
9943     /* Check for new Armv8.1-M Mainline changes to <spec_reg>.  */
9944     case 1: /* fpscr.  */
9945       constraint (!(ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9946                     || ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9947                   _(BAD_FPU));
9948       break;
9949
9950     case 14: /* fpcxt_ns.  */
9951     case 15: /* fpcxt_s.  */
9952       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main),
9953                   _("selected processor does not support instruction"));
9954       break;
9955
9956     case  2: /* fpscr_nzcvqc.  */
9957     case 12: /* vpr.  */
9958     case 13: /* p0.  */
9959       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main)
9960                   || (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
9961                       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
9962                   _("selected processor does not support instruction"));
9963       if (inst.operands[0].reg != 2
9964           && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
9965         as_warn (_("accessing MVE system register without MVE is UNPREDICTABLE"));
9966       break;
9967
9968     default:
9969       break;
9970     }
9971
9972   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9973   if (!inst.operands[0].isvec && Rt == REG_PC)
9974     {
9975       inst.error = BAD_PC;
9976       return;
9977     }
9978
9979   /* If we get through parsing the register name, we just insert the number
9980      generated into the instruction without further validation.  */
9981   inst.instruction |= (inst.operands[1].reg << 16);
9982   inst.instruction |= (Rt << 12);
9983 }
9984
9985 static void
9986 do_vmsr (void)
9987 {
9988   unsigned Rt = inst.operands[1].reg;
9989
9990   if (thumb_mode)
9991     reject_bad_reg (Rt);
9992   else if (Rt == REG_PC)
9993     {
9994       inst.error = BAD_PC;
9995       return;
9996     }
9997
9998   switch (inst.operands[0].reg)
9999     {
10000     /* MVFR2 is only valid for Armv8-A.  */
10001     case 5:
10002       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
10003                   _(BAD_FPU));
10004       break;
10005
10006     /* Check for new Armv8.1-M Mainline changes to <spec_reg>.  */
10007     case  1: /* fpcr.  */
10008       constraint (!(ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
10009                     || ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
10010                   _(BAD_FPU));
10011       break;
10012
10013     case 14: /* fpcxt_ns.  */
10014     case 15: /* fpcxt_s.  */
10015       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main),
10016                   _("selected processor does not support instruction"));
10017       break;
10018
10019     case  2: /* fpscr_nzcvqc.  */
10020     case 12: /* vpr.  */
10021     case 13: /* p0.  */
10022       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_1m_main)
10023                   || (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
10024                       && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)),
10025                   _("selected processor does not support instruction"));
10026       if (inst.operands[0].reg != 2
10027           && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
10028         as_warn (_("accessing MVE system register without MVE is UNPREDICTABLE"));
10029       break;
10030
10031     default:
10032       break;
10033     }
10034
10035   /* If we get through parsing the register name, we just insert the number
10036      generated into the instruction without further validation.  */
10037   inst.instruction |= (inst.operands[0].reg << 16);
10038   inst.instruction |= (Rt << 12);
10039 }
10040
10041 static void
10042 do_mrs (void)
10043 {
10044   unsigned br;
10045
10046   if (do_vfp_nsyn_mrs () == SUCCESS)
10047     return;
10048
10049   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10050   inst.instruction |= inst.operands[0].reg << 12;
10051
10052   if (inst.operands[1].isreg)
10053     {
10054       br = inst.operands[1].reg;
10055       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
10056         as_bad (_("bad register for mrs"));
10057     }
10058   else
10059     {
10060       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
10061       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
10062                   != (PSR_c|PSR_f),
10063                   _("'APSR', 'CPSR' or 'SPSR' expected"));
10064       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
10065     }
10066
10067   inst.instruction |= br;
10068 }
10069
10070 /* Two possible forms:
10071       "{C|S}PSR_<field>, Rm",
10072       "{C|S}PSR_f, #expression".  */
10073
10074 static void
10075 do_msr (void)
10076 {
10077   if (do_vfp_nsyn_msr () == SUCCESS)
10078     return;
10079
10080   inst.instruction |= inst.operands[0].imm;
10081   if (inst.operands[1].isreg)
10082     inst.instruction |= inst.operands[1].reg;
10083   else
10084     {
10085       inst.instruction |= INST_IMMEDIATE;
10086       inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
10087       inst.relocs[0].pc_rel = 0;
10088     }
10089 }
10090
10091 static void
10092 do_mul (void)
10093 {
10094   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
10095
10096   if (!inst.operands[2].present)
10097     inst.operands[2].reg = inst.operands[0].reg;
10098   inst.instruction |= inst.operands[0].reg << 16;
10099   inst.instruction |= inst.operands[1].reg;
10100   inst.instruction |= inst.operands[2].reg << 8;
10101
10102   if (inst.operands[0].reg == inst.operands[1].reg
10103       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
10104     as_tsktsk (_("Rd and Rm should be different in mul"));
10105 }
10106
10107 /* Long Multiply Parser
10108    UMULL RdLo, RdHi, Rm, Rs
10109    SMULL RdLo, RdHi, Rm, Rs
10110    UMLAL RdLo, RdHi, Rm, Rs
10111    SMLAL RdLo, RdHi, Rm, Rs.  */
10112
10113 static void
10114 do_mull (void)
10115 {
10116   inst.instruction |= inst.operands[0].reg << 12;
10117   inst.instruction |= inst.operands[1].reg << 16;
10118   inst.instruction |= inst.operands[2].reg;
10119   inst.instruction |= inst.operands[3].reg << 8;
10120
10121   /* rdhi and rdlo must be different.  */
10122   if (inst.operands[0].reg == inst.operands[1].reg)
10123     as_tsktsk (_("rdhi and rdlo must be different"));
10124
10125   /* rdhi, rdlo and rm must all be different before armv6.  */
10126   if ((inst.operands[0].reg == inst.operands[2].reg
10127       || inst.operands[1].reg == inst.operands[2].reg)
10128       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
10129     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
10130 }
10131
10132 static void
10133 do_nop (void)
10134 {
10135   if (inst.operands[0].present
10136       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
10137     {
10138       /* Architectural NOP hints are CPSR sets with no bits selected.  */
10139       inst.instruction &= 0xf0000000;
10140       inst.instruction |= 0x0320f000;
10141       if (inst.operands[0].present)
10142         inst.instruction |= inst.operands[0].imm;
10143     }
10144 }
10145
10146 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
10147    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
10148    Condition defaults to COND_ALWAYS.
10149    Error if Rd, Rn or Rm are R15.  */
10150
10151 static void
10152 do_pkhbt (void)
10153 {
10154   inst.instruction |= inst.operands[0].reg << 12;
10155   inst.instruction |= inst.operands[1].reg << 16;
10156   inst.instruction |= inst.operands[2].reg;
10157   if (inst.operands[3].present)
10158     encode_arm_shift (3);
10159 }
10160
10161 /* ARM V6 PKHTB (Argument Parse).  */
10162
10163 static void
10164 do_pkhtb (void)
10165 {
10166   if (!inst.operands[3].present)
10167     {
10168       /* If the shift specifier is omitted, turn the instruction
10169          into pkhbt rd, rm, rn. */
10170       inst.instruction &= 0xfff00010;
10171       inst.instruction |= inst.operands[0].reg << 12;
10172       inst.instruction |= inst.operands[1].reg;
10173       inst.instruction |= inst.operands[2].reg << 16;
10174     }
10175   else
10176     {
10177       inst.instruction |= inst.operands[0].reg << 12;
10178       inst.instruction |= inst.operands[1].reg << 16;
10179       inst.instruction |= inst.operands[2].reg;
10180       encode_arm_shift (3);
10181     }
10182 }
10183
10184 /* ARMv5TE: Preload-Cache
10185    MP Extensions: Preload for write
10186
10187     PLD(W) <addr_mode>
10188
10189   Syntactically, like LDR with B=1, W=0, L=1.  */
10190
10191 static void
10192 do_pld (void)
10193 {
10194   constraint (!inst.operands[0].isreg,
10195               _("'[' expected after PLD mnemonic"));
10196   constraint (inst.operands[0].postind,
10197               _("post-indexed expression used in preload instruction"));
10198   constraint (inst.operands[0].writeback,
10199               _("writeback used in preload instruction"));
10200   constraint (!inst.operands[0].preind,
10201               _("unindexed addressing used in preload instruction"));
10202   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10203 }
10204
10205 /* ARMv7: PLI <addr_mode>  */
10206 static void
10207 do_pli (void)
10208 {
10209   constraint (!inst.operands[0].isreg,
10210               _("'[' expected after PLI mnemonic"));
10211   constraint (inst.operands[0].postind,
10212               _("post-indexed expression used in preload instruction"));
10213   constraint (inst.operands[0].writeback,
10214               _("writeback used in preload instruction"));
10215   constraint (!inst.operands[0].preind,
10216               _("unindexed addressing used in preload instruction"));
10217   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10218   inst.instruction &= ~PRE_INDEX;
10219 }
10220
10221 static void
10222 do_push_pop (void)
10223 {
10224   constraint (inst.operands[0].writeback,
10225               _("push/pop do not support {reglist}^"));
10226   inst.operands[1] = inst.operands[0];
10227   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
10228   inst.operands[0].isreg = 1;
10229   inst.operands[0].writeback = 1;
10230   inst.operands[0].reg = REG_SP;
10231   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
10232 }
10233
10234 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
10235    word at the specified address and the following word
10236    respectively.
10237    Unconditionally executed.
10238    Error if Rn is R15.  */
10239
10240 static void
10241 do_rfe (void)
10242 {
10243   inst.instruction |= inst.operands[0].reg << 16;
10244   if (inst.operands[0].writeback)
10245     inst.instruction |= WRITE_BACK;
10246 }
10247
10248 /* ARM V6 ssat (argument parse).  */
10249
10250 static void
10251 do_ssat (void)
10252 {
10253   inst.instruction |= inst.operands[0].reg << 12;
10254   inst.instruction |= (inst.operands[1].imm - 1) << 16;
10255   inst.instruction |= inst.operands[2].reg;
10256
10257   if (inst.operands[3].present)
10258     encode_arm_shift (3);
10259 }
10260
10261 /* ARM V6 usat (argument parse).  */
10262
10263 static void
10264 do_usat (void)
10265 {
10266   inst.instruction |= inst.operands[0].reg << 12;
10267   inst.instruction |= inst.operands[1].imm << 16;
10268   inst.instruction |= inst.operands[2].reg;
10269
10270   if (inst.operands[3].present)
10271     encode_arm_shift (3);
10272 }
10273
10274 /* ARM V6 ssat16 (argument parse).  */
10275
10276 static void
10277 do_ssat16 (void)
10278 {
10279   inst.instruction |= inst.operands[0].reg << 12;
10280   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
10281   inst.instruction |= inst.operands[2].reg;
10282 }
10283
10284 static void
10285 do_usat16 (void)
10286 {
10287   inst.instruction |= inst.operands[0].reg << 12;
10288   inst.instruction |= inst.operands[1].imm << 16;
10289   inst.instruction |= inst.operands[2].reg;
10290 }
10291
10292 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
10293    preserving the other bits.
10294
10295    setend <endian_specifier>, where <endian_specifier> is either
10296    BE or LE.  */
10297
10298 static void
10299 do_setend (void)
10300 {
10301   if (warn_on_deprecated
10302       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10303       as_tsktsk (_("setend use is deprecated for ARMv8"));
10304
10305   if (inst.operands[0].imm)
10306     inst.instruction |= 0x200;
10307 }
10308
10309 static void
10310 do_shift (void)
10311 {
10312   unsigned int Rm = (inst.operands[1].present
10313                      ? inst.operands[1].reg
10314                      : inst.operands[0].reg);
10315
10316   inst.instruction |= inst.operands[0].reg << 12;
10317   inst.instruction |= Rm;
10318   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
10319     {
10320       inst.instruction |= inst.operands[2].reg << 8;
10321       inst.instruction |= SHIFT_BY_REG;
10322       /* PR 12854: Error on extraneous shifts.  */
10323       constraint (inst.operands[2].shifted,
10324                   _("extraneous shift as part of operand to shift insn"));
10325     }
10326   else
10327     inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
10328 }
10329
10330 static void
10331 do_smc (void)
10332 {
10333   unsigned int value = inst.relocs[0].exp.X_add_number;
10334   constraint (value > 0xf, _("immediate too large (bigger than 0xF)"));
10335
10336   inst.relocs[0].type = BFD_RELOC_ARM_SMC;
10337   inst.relocs[0].pc_rel = 0;
10338 }
10339
10340 static void
10341 do_hvc (void)
10342 {
10343   inst.relocs[0].type = BFD_RELOC_ARM_HVC;
10344   inst.relocs[0].pc_rel = 0;
10345 }
10346
10347 static void
10348 do_swi (void)
10349 {
10350   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
10351   inst.relocs[0].pc_rel = 0;
10352 }
10353
10354 static void
10355 do_setpan (void)
10356 {
10357   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10358               _("selected processor does not support SETPAN instruction"));
10359
10360   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
10361 }
10362
10363 static void
10364 do_t_setpan (void)
10365 {
10366   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10367               _("selected processor does not support SETPAN instruction"));
10368
10369   inst.instruction |= (inst.operands[0].imm << 3);
10370 }
10371
10372 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
10373    SMLAxy{cond} Rd,Rm,Rs,Rn
10374    SMLAWy{cond} Rd,Rm,Rs,Rn
10375    Error if any register is R15.  */
10376
10377 static void
10378 do_smla (void)
10379 {
10380   inst.instruction |= inst.operands[0].reg << 16;
10381   inst.instruction |= inst.operands[1].reg;
10382   inst.instruction |= inst.operands[2].reg << 8;
10383   inst.instruction |= inst.operands[3].reg << 12;
10384 }
10385
10386 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
10387    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
10388    Error if any register is R15.
10389    Warning if Rdlo == Rdhi.  */
10390
10391 static void
10392 do_smlal (void)
10393 {
10394   inst.instruction |= inst.operands[0].reg << 12;
10395   inst.instruction |= inst.operands[1].reg << 16;
10396   inst.instruction |= inst.operands[2].reg;
10397   inst.instruction |= inst.operands[3].reg << 8;
10398
10399   if (inst.operands[0].reg == inst.operands[1].reg)
10400     as_tsktsk (_("rdhi and rdlo must be different"));
10401 }
10402
10403 /* ARM V5E (El Segundo) signed-multiply (argument parse)
10404    SMULxy{cond} Rd,Rm,Rs
10405    Error if any register is R15.  */
10406
10407 static void
10408 do_smul (void)
10409 {
10410   inst.instruction |= inst.operands[0].reg << 16;
10411   inst.instruction |= inst.operands[1].reg;
10412   inst.instruction |= inst.operands[2].reg << 8;
10413 }
10414
10415 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
10416    the same for both ARM and Thumb-2.  */
10417
10418 static void
10419 do_srs (void)
10420 {
10421   int reg;
10422
10423   if (inst.operands[0].present)
10424     {
10425       reg = inst.operands[0].reg;
10426       constraint (reg != REG_SP, _("SRS base register must be r13"));
10427     }
10428   else
10429     reg = REG_SP;
10430
10431   inst.instruction |= reg << 16;
10432   inst.instruction |= inst.operands[1].imm;
10433   if (inst.operands[0].writeback || inst.operands[1].writeback)
10434     inst.instruction |= WRITE_BACK;
10435 }
10436
10437 /* ARM V6 strex (argument parse).  */
10438
10439 static void
10440 do_strex (void)
10441 {
10442   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10443               || inst.operands[2].postind || inst.operands[2].writeback
10444               || inst.operands[2].immisreg || inst.operands[2].shifted
10445               || inst.operands[2].negative
10446               /* See comment in do_ldrex().  */
10447               || (inst.operands[2].reg == REG_PC),
10448               BAD_ADDR_MODE);
10449
10450   constraint (inst.operands[0].reg == inst.operands[1].reg
10451               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10452
10453   constraint (inst.relocs[0].exp.X_op != O_constant
10454               || inst.relocs[0].exp.X_add_number != 0,
10455               _("offset must be zero in ARM encoding"));
10456
10457   inst.instruction |= inst.operands[0].reg << 12;
10458   inst.instruction |= inst.operands[1].reg;
10459   inst.instruction |= inst.operands[2].reg << 16;
10460   inst.relocs[0].type = BFD_RELOC_UNUSED;
10461 }
10462
10463 static void
10464 do_t_strexbh (void)
10465 {
10466   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10467               || inst.operands[2].postind || inst.operands[2].writeback
10468               || inst.operands[2].immisreg || inst.operands[2].shifted
10469               || inst.operands[2].negative,
10470               BAD_ADDR_MODE);
10471
10472   constraint (inst.operands[0].reg == inst.operands[1].reg
10473               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10474
10475   do_rm_rd_rn ();
10476 }
10477
10478 static void
10479 do_strexd (void)
10480 {
10481   constraint (inst.operands[1].reg % 2 != 0,
10482               _("even register required"));
10483   constraint (inst.operands[2].present
10484               && inst.operands[2].reg != inst.operands[1].reg + 1,
10485               _("can only store two consecutive registers"));
10486   /* If op 2 were present and equal to PC, this function wouldn't
10487      have been called in the first place.  */
10488   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
10489
10490   constraint (inst.operands[0].reg == inst.operands[1].reg
10491               || inst.operands[0].reg == inst.operands[1].reg + 1
10492               || inst.operands[0].reg == inst.operands[3].reg,
10493               BAD_OVERLAP);
10494
10495   inst.instruction |= inst.operands[0].reg << 12;
10496   inst.instruction |= inst.operands[1].reg;
10497   inst.instruction |= inst.operands[3].reg << 16;
10498 }
10499
10500 /* ARM V8 STRL.  */
10501 static void
10502 do_stlex (void)
10503 {
10504   constraint (inst.operands[0].reg == inst.operands[1].reg
10505               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10506
10507   do_rd_rm_rn ();
10508 }
10509
10510 static void
10511 do_t_stlex (void)
10512 {
10513   constraint (inst.operands[0].reg == inst.operands[1].reg
10514               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10515
10516   do_rm_rd_rn ();
10517 }
10518
10519 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
10520    extends it to 32-bits, and adds the result to a value in another
10521    register.  You can specify a rotation by 0, 8, 16, or 24 bits
10522    before extracting the 16-bit value.
10523    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
10524    Condition defaults to COND_ALWAYS.
10525    Error if any register uses R15.  */
10526
10527 static void
10528 do_sxtah (void)
10529 {
10530   inst.instruction |= inst.operands[0].reg << 12;
10531   inst.instruction |= inst.operands[1].reg << 16;
10532   inst.instruction |= inst.operands[2].reg;
10533   inst.instruction |= inst.operands[3].imm << 10;
10534 }
10535
10536 /* ARM V6 SXTH.
10537
10538    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
10539    Condition defaults to COND_ALWAYS.
10540    Error if any register uses R15.  */
10541
10542 static void
10543 do_sxth (void)
10544 {
10545   inst.instruction |= inst.operands[0].reg << 12;
10546   inst.instruction |= inst.operands[1].reg;
10547   inst.instruction |= inst.operands[2].imm << 10;
10548 }
10549 \f
10550 /* VFP instructions.  In a logical order: SP variant first, monad
10551    before dyad, arithmetic then move then load/store.  */
10552
10553 static void
10554 do_vfp_sp_monadic (void)
10555 {
10556   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10557               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10558               _(BAD_FPU));
10559
10560   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10561   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10562 }
10563
10564 static void
10565 do_vfp_sp_dyadic (void)
10566 {
10567   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10568   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10569   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10570 }
10571
10572 static void
10573 do_vfp_sp_compare_z (void)
10574 {
10575   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10576 }
10577
10578 static void
10579 do_vfp_dp_sp_cvt (void)
10580 {
10581   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10582   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10583 }
10584
10585 static void
10586 do_vfp_sp_dp_cvt (void)
10587 {
10588   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10589   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10590 }
10591
10592 static void
10593 do_vfp_reg_from_sp (void)
10594 {
10595   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10596              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10597              _(BAD_FPU));
10598
10599   inst.instruction |= inst.operands[0].reg << 12;
10600   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10601 }
10602
10603 static void
10604 do_vfp_reg2_from_sp2 (void)
10605 {
10606   constraint (inst.operands[2].imm != 2,
10607               _("only two consecutive VFP SP registers allowed here"));
10608   inst.instruction |= inst.operands[0].reg << 12;
10609   inst.instruction |= inst.operands[1].reg << 16;
10610   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10611 }
10612
10613 static void
10614 do_vfp_sp_from_reg (void)
10615 {
10616   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10617              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10618              _(BAD_FPU));
10619
10620   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
10621   inst.instruction |= inst.operands[1].reg << 12;
10622 }
10623
10624 static void
10625 do_vfp_sp2_from_reg2 (void)
10626 {
10627   constraint (inst.operands[0].imm != 2,
10628               _("only two consecutive VFP SP registers allowed here"));
10629   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
10630   inst.instruction |= inst.operands[1].reg << 12;
10631   inst.instruction |= inst.operands[2].reg << 16;
10632 }
10633
10634 static void
10635 do_vfp_sp_ldst (void)
10636 {
10637   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10638   encode_arm_cp_address (1, FALSE, TRUE, 0);
10639 }
10640
10641 static void
10642 do_vfp_dp_ldst (void)
10643 {
10644   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10645   encode_arm_cp_address (1, FALSE, TRUE, 0);
10646 }
10647
10648
10649 static void
10650 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
10651 {
10652   if (inst.operands[0].writeback)
10653     inst.instruction |= WRITE_BACK;
10654   else
10655     constraint (ldstm_type != VFP_LDSTMIA,
10656                 _("this addressing mode requires base-register writeback"));
10657   inst.instruction |= inst.operands[0].reg << 16;
10658   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
10659   inst.instruction |= inst.operands[1].imm;
10660 }
10661
10662 static void
10663 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
10664 {
10665   int count;
10666
10667   if (inst.operands[0].writeback)
10668     inst.instruction |= WRITE_BACK;
10669   else
10670     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
10671                 _("this addressing mode requires base-register writeback"));
10672
10673   inst.instruction |= inst.operands[0].reg << 16;
10674   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10675
10676   count = inst.operands[1].imm << 1;
10677   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
10678     count += 1;
10679
10680   inst.instruction |= count;
10681 }
10682
10683 static void
10684 do_vfp_sp_ldstmia (void)
10685 {
10686   vfp_sp_ldstm (VFP_LDSTMIA);
10687 }
10688
10689 static void
10690 do_vfp_sp_ldstmdb (void)
10691 {
10692   vfp_sp_ldstm (VFP_LDSTMDB);
10693 }
10694
10695 static void
10696 do_vfp_dp_ldstmia (void)
10697 {
10698   vfp_dp_ldstm (VFP_LDSTMIA);
10699 }
10700
10701 static void
10702 do_vfp_dp_ldstmdb (void)
10703 {
10704   vfp_dp_ldstm (VFP_LDSTMDB);
10705 }
10706
10707 static void
10708 do_vfp_xp_ldstmia (void)
10709 {
10710   vfp_dp_ldstm (VFP_LDSTMIAX);
10711 }
10712
10713 static void
10714 do_vfp_xp_ldstmdb (void)
10715 {
10716   vfp_dp_ldstm (VFP_LDSTMDBX);
10717 }
10718
10719 static void
10720 do_vfp_dp_rd_rm (void)
10721 {
10722   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
10723               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10724               _(BAD_FPU));
10725
10726   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10727   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10728 }
10729
10730 static void
10731 do_vfp_dp_rn_rd (void)
10732 {
10733   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
10734   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10735 }
10736
10737 static void
10738 do_vfp_dp_rd_rn (void)
10739 {
10740   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10741   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10742 }
10743
10744 static void
10745 do_vfp_dp_rd_rn_rm (void)
10746 {
10747   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10748               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10749               _(BAD_FPU));
10750
10751   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10752   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10753   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
10754 }
10755
10756 static void
10757 do_vfp_dp_rd (void)
10758 {
10759   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10760 }
10761
10762 static void
10763 do_vfp_dp_rm_rd_rn (void)
10764 {
10765   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10766               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10767               _(BAD_FPU));
10768
10769   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
10770   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10771   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
10772 }
10773
10774 /* VFPv3 instructions.  */
10775 static void
10776 do_vfp_sp_const (void)
10777 {
10778   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10779   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10780   inst.instruction |= (inst.operands[1].imm & 0x0f);
10781 }
10782
10783 static void
10784 do_vfp_dp_const (void)
10785 {
10786   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10787   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10788   inst.instruction |= (inst.operands[1].imm & 0x0f);
10789 }
10790
10791 static void
10792 vfp_conv (int srcsize)
10793 {
10794   int immbits = srcsize - inst.operands[1].imm;
10795
10796   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
10797     {
10798       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
10799          i.e. immbits must be in range 0 - 16.  */
10800       inst.error = _("immediate value out of range, expected range [0, 16]");
10801       return;
10802     }
10803   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
10804     {
10805       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
10806          i.e. immbits must be in range 0 - 31.  */
10807       inst.error = _("immediate value out of range, expected range [1, 32]");
10808       return;
10809     }
10810
10811   inst.instruction |= (immbits & 1) << 5;
10812   inst.instruction |= (immbits >> 1);
10813 }
10814
10815 static void
10816 do_vfp_sp_conv_16 (void)
10817 {
10818   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10819   vfp_conv (16);
10820 }
10821
10822 static void
10823 do_vfp_dp_conv_16 (void)
10824 {
10825   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10826   vfp_conv (16);
10827 }
10828
10829 static void
10830 do_vfp_sp_conv_32 (void)
10831 {
10832   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10833   vfp_conv (32);
10834 }
10835
10836 static void
10837 do_vfp_dp_conv_32 (void)
10838 {
10839   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10840   vfp_conv (32);
10841 }
10842 \f
10843 /* FPA instructions.  Also in a logical order.  */
10844
10845 static void
10846 do_fpa_cmp (void)
10847 {
10848   inst.instruction |= inst.operands[0].reg << 16;
10849   inst.instruction |= inst.operands[1].reg;
10850 }
10851
10852 static void
10853 do_fpa_ldmstm (void)
10854 {
10855   inst.instruction |= inst.operands[0].reg << 12;
10856   switch (inst.operands[1].imm)
10857     {
10858     case 1: inst.instruction |= CP_T_X;          break;
10859     case 2: inst.instruction |= CP_T_Y;          break;
10860     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10861     case 4:                                      break;
10862     default: abort ();
10863     }
10864
10865   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10866     {
10867       /* The instruction specified "ea" or "fd", so we can only accept
10868          [Rn]{!}.  The instruction does not really support stacking or
10869          unstacking, so we have to emulate these by setting appropriate
10870          bits and offsets.  */
10871       constraint (inst.relocs[0].exp.X_op != O_constant
10872                   || inst.relocs[0].exp.X_add_number != 0,
10873                   _("this instruction does not support indexing"));
10874
10875       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10876         inst.relocs[0].exp.X_add_number = 12 * inst.operands[1].imm;
10877
10878       if (!(inst.instruction & INDEX_UP))
10879         inst.relocs[0].exp.X_add_number = -inst.relocs[0].exp.X_add_number;
10880
10881       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10882         {
10883           inst.operands[2].preind = 0;
10884           inst.operands[2].postind = 1;
10885         }
10886     }
10887
10888   encode_arm_cp_address (2, TRUE, TRUE, 0);
10889 }
10890 \f
10891 /* iWMMXt instructions: strictly in alphabetical order.  */
10892
10893 static void
10894 do_iwmmxt_tandorc (void)
10895 {
10896   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10897 }
10898
10899 static void
10900 do_iwmmxt_textrc (void)
10901 {
10902   inst.instruction |= inst.operands[0].reg << 12;
10903   inst.instruction |= inst.operands[1].imm;
10904 }
10905
10906 static void
10907 do_iwmmxt_textrm (void)
10908 {
10909   inst.instruction |= inst.operands[0].reg << 12;
10910   inst.instruction |= inst.operands[1].reg << 16;
10911   inst.instruction |= inst.operands[2].imm;
10912 }
10913
10914 static void
10915 do_iwmmxt_tinsr (void)
10916 {
10917   inst.instruction |= inst.operands[0].reg << 16;
10918   inst.instruction |= inst.operands[1].reg << 12;
10919   inst.instruction |= inst.operands[2].imm;
10920 }
10921
10922 static void
10923 do_iwmmxt_tmia (void)
10924 {
10925   inst.instruction |= inst.operands[0].reg << 5;
10926   inst.instruction |= inst.operands[1].reg;
10927   inst.instruction |= inst.operands[2].reg << 12;
10928 }
10929
10930 static void
10931 do_iwmmxt_waligni (void)
10932 {
10933   inst.instruction |= inst.operands[0].reg << 12;
10934   inst.instruction |= inst.operands[1].reg << 16;
10935   inst.instruction |= inst.operands[2].reg;
10936   inst.instruction |= inst.operands[3].imm << 20;
10937 }
10938
10939 static void
10940 do_iwmmxt_wmerge (void)
10941 {
10942   inst.instruction |= inst.operands[0].reg << 12;
10943   inst.instruction |= inst.operands[1].reg << 16;
10944   inst.instruction |= inst.operands[2].reg;
10945   inst.instruction |= inst.operands[3].imm << 21;
10946 }
10947
10948 static void
10949 do_iwmmxt_wmov (void)
10950 {
10951   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10952   inst.instruction |= inst.operands[0].reg << 12;
10953   inst.instruction |= inst.operands[1].reg << 16;
10954   inst.instruction |= inst.operands[1].reg;
10955 }
10956
10957 static void
10958 do_iwmmxt_wldstbh (void)
10959 {
10960   int reloc;
10961   inst.instruction |= inst.operands[0].reg << 12;
10962   if (thumb_mode)
10963     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10964   else
10965     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10966   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10967 }
10968
10969 static void
10970 do_iwmmxt_wldstw (void)
10971 {
10972   /* RIWR_RIWC clears .isreg for a control register.  */
10973   if (!inst.operands[0].isreg)
10974     {
10975       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10976       inst.instruction |= 0xf0000000;
10977     }
10978
10979   inst.instruction |= inst.operands[0].reg << 12;
10980   encode_arm_cp_address (1, TRUE, TRUE, 0);
10981 }
10982
10983 static void
10984 do_iwmmxt_wldstd (void)
10985 {
10986   inst.instruction |= inst.operands[0].reg << 12;
10987   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10988       && inst.operands[1].immisreg)
10989     {
10990       inst.instruction &= ~0x1a000ff;
10991       inst.instruction |= (0xfU << 28);
10992       if (inst.operands[1].preind)
10993         inst.instruction |= PRE_INDEX;
10994       if (!inst.operands[1].negative)
10995         inst.instruction |= INDEX_UP;
10996       if (inst.operands[1].writeback)
10997         inst.instruction |= WRITE_BACK;
10998       inst.instruction |= inst.operands[1].reg << 16;
10999       inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
11000       inst.instruction |= inst.operands[1].imm;
11001     }
11002   else
11003     encode_arm_cp_address (1, TRUE, FALSE, 0);
11004 }
11005
11006 static void
11007 do_iwmmxt_wshufh (void)
11008 {
11009   inst.instruction |= inst.operands[0].reg << 12;
11010   inst.instruction |= inst.operands[1].reg << 16;
11011   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
11012   inst.instruction |= (inst.operands[2].imm & 0x0f);
11013 }
11014
11015 static void
11016 do_iwmmxt_wzero (void)
11017 {
11018   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
11019   inst.instruction |= inst.operands[0].reg;
11020   inst.instruction |= inst.operands[0].reg << 12;
11021   inst.instruction |= inst.operands[0].reg << 16;
11022 }
11023
11024 static void
11025 do_iwmmxt_wrwrwr_or_imm5 (void)
11026 {
11027   if (inst.operands[2].isreg)
11028     do_rd_rn_rm ();
11029   else {
11030     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
11031                 _("immediate operand requires iWMMXt2"));
11032     do_rd_rn ();
11033     if (inst.operands[2].imm == 0)
11034       {
11035         switch ((inst.instruction >> 20) & 0xf)
11036           {
11037           case 4:
11038           case 5:
11039           case 6:
11040           case 7:
11041             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
11042             inst.operands[2].imm = 16;
11043             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
11044             break;
11045           case 8:
11046           case 9:
11047           case 10:
11048           case 11:
11049             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
11050             inst.operands[2].imm = 32;
11051             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
11052             break;
11053           case 12:
11054           case 13:
11055           case 14:
11056           case 15:
11057             {
11058               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
11059               unsigned long wrn;
11060               wrn = (inst.instruction >> 16) & 0xf;
11061               inst.instruction &= 0xff0fff0f;
11062               inst.instruction |= wrn;
11063               /* Bail out here; the instruction is now assembled.  */
11064               return;
11065             }
11066           }
11067       }
11068     /* Map 32 -> 0, etc.  */
11069     inst.operands[2].imm &= 0x1f;
11070     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
11071   }
11072 }
11073 \f
11074 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
11075    operations first, then control, shift, and load/store.  */
11076
11077 /* Insns like "foo X,Y,Z".  */
11078
11079 static void
11080 do_mav_triple (void)
11081 {
11082   inst.instruction |= inst.operands[0].reg << 16;
11083   inst.instruction |= inst.operands[1].reg;
11084   inst.instruction |= inst.operands[2].reg << 12;
11085 }
11086
11087 /* Insns like "foo W,X,Y,Z".
11088     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
11089
11090 static void
11091 do_mav_quad (void)
11092 {
11093   inst.instruction |= inst.operands[0].reg << 5;
11094   inst.instruction |= inst.operands[1].reg << 12;
11095   inst.instruction |= inst.operands[2].reg << 16;
11096   inst.instruction |= inst.operands[3].reg;
11097 }
11098
11099 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
11100 static void
11101 do_mav_dspsc (void)
11102 {
11103   inst.instruction |= inst.operands[1].reg << 12;
11104 }
11105
11106 /* Maverick shift immediate instructions.
11107    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
11108    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
11109
11110 static void
11111 do_mav_shift (void)
11112 {
11113   int imm = inst.operands[2].imm;
11114
11115   inst.instruction |= inst.operands[0].reg << 12;
11116   inst.instruction |= inst.operands[1].reg << 16;
11117
11118   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
11119      Bits 5-7 of the insn should have bits 4-6 of the immediate.
11120      Bit 4 should be 0.  */
11121   imm = (imm & 0xf) | ((imm & 0x70) << 1);
11122
11123   inst.instruction |= imm;
11124 }
11125 \f
11126 /* XScale instructions.  Also sorted arithmetic before move.  */
11127
11128 /* Xscale multiply-accumulate (argument parse)
11129      MIAcc   acc0,Rm,Rs
11130      MIAPHcc acc0,Rm,Rs
11131      MIAxycc acc0,Rm,Rs.  */
11132
11133 static void
11134 do_xsc_mia (void)
11135 {
11136   inst.instruction |= inst.operands[1].reg;
11137   inst.instruction |= inst.operands[2].reg << 12;
11138 }
11139
11140 /* Xscale move-accumulator-register (argument parse)
11141
11142      MARcc   acc0,RdLo,RdHi.  */
11143
11144 static void
11145 do_xsc_mar (void)
11146 {
11147   inst.instruction |= inst.operands[1].reg << 12;
11148   inst.instruction |= inst.operands[2].reg << 16;
11149 }
11150
11151 /* Xscale move-register-accumulator (argument parse)
11152
11153      MRAcc   RdLo,RdHi,acc0.  */
11154
11155 static void
11156 do_xsc_mra (void)
11157 {
11158   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
11159   inst.instruction |= inst.operands[0].reg << 12;
11160   inst.instruction |= inst.operands[1].reg << 16;
11161 }
11162 \f
11163 /* Encoding functions relevant only to Thumb.  */
11164
11165 /* inst.operands[i] is a shifted-register operand; encode
11166    it into inst.instruction in the format used by Thumb32.  */
11167
11168 static void
11169 encode_thumb32_shifted_operand (int i)
11170 {
11171   unsigned int value = inst.relocs[0].exp.X_add_number;
11172   unsigned int shift = inst.operands[i].shift_kind;
11173
11174   constraint (inst.operands[i].immisreg,
11175               _("shift by register not allowed in thumb mode"));
11176   inst.instruction |= inst.operands[i].reg;
11177   if (shift == SHIFT_RRX)
11178     inst.instruction |= SHIFT_ROR << 4;
11179   else
11180     {
11181       constraint (inst.relocs[0].exp.X_op != O_constant,
11182                   _("expression too complex"));
11183
11184       constraint (value > 32
11185                   || (value == 32 && (shift == SHIFT_LSL
11186                                       || shift == SHIFT_ROR)),
11187                   _("shift expression is too large"));
11188
11189       if (value == 0)
11190         shift = SHIFT_LSL;
11191       else if (value == 32)
11192         value = 0;
11193
11194       inst.instruction |= shift << 4;
11195       inst.instruction |= (value & 0x1c) << 10;
11196       inst.instruction |= (value & 0x03) << 6;
11197     }
11198 }
11199
11200
11201 /* inst.operands[i] was set up by parse_address.  Encode it into a
11202    Thumb32 format load or store instruction.  Reject forms that cannot
11203    be used with such instructions.  If is_t is true, reject forms that
11204    cannot be used with a T instruction; if is_d is true, reject forms
11205    that cannot be used with a D instruction.  If it is a store insn,
11206    reject PC in Rn.  */
11207
11208 static void
11209 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
11210 {
11211   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
11212
11213   constraint (!inst.operands[i].isreg,
11214               _("Instruction does not support =N addresses"));
11215
11216   inst.instruction |= inst.operands[i].reg << 16;
11217   if (inst.operands[i].immisreg)
11218     {
11219       constraint (is_pc, BAD_PC_ADDRESSING);
11220       constraint (is_t || is_d, _("cannot use register index with this instruction"));
11221       constraint (inst.operands[i].negative,
11222                   _("Thumb does not support negative register indexing"));
11223       constraint (inst.operands[i].postind,
11224                   _("Thumb does not support register post-indexing"));
11225       constraint (inst.operands[i].writeback,
11226                   _("Thumb does not support register indexing with writeback"));
11227       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
11228                   _("Thumb supports only LSL in shifted register indexing"));
11229
11230       inst.instruction |= inst.operands[i].imm;
11231       if (inst.operands[i].shifted)
11232         {
11233           constraint (inst.relocs[0].exp.X_op != O_constant,
11234                       _("expression too complex"));
11235           constraint (inst.relocs[0].exp.X_add_number < 0
11236                       || inst.relocs[0].exp.X_add_number > 3,
11237                       _("shift out of range"));
11238           inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
11239         }
11240       inst.relocs[0].type = BFD_RELOC_UNUSED;
11241     }
11242   else if (inst.operands[i].preind)
11243     {
11244       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
11245       constraint (is_t && inst.operands[i].writeback,
11246                   _("cannot use writeback with this instruction"));
11247       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
11248                   BAD_PC_ADDRESSING);
11249
11250       if (is_d)
11251         {
11252           inst.instruction |= 0x01000000;
11253           if (inst.operands[i].writeback)
11254             inst.instruction |= 0x00200000;
11255         }
11256       else
11257         {
11258           inst.instruction |= 0x00000c00;
11259           if (inst.operands[i].writeback)
11260             inst.instruction |= 0x00000100;
11261         }
11262       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11263     }
11264   else if (inst.operands[i].postind)
11265     {
11266       gas_assert (inst.operands[i].writeback);
11267       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
11268       constraint (is_t, _("cannot use post-indexing with this instruction"));
11269
11270       if (is_d)
11271         inst.instruction |= 0x00200000;
11272       else
11273         inst.instruction |= 0x00000900;
11274       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11275     }
11276   else /* unindexed - only for coprocessor */
11277     inst.error = _("instruction does not accept unindexed addressing");
11278 }
11279
11280 /* Table of Thumb instructions which exist in 16- and/or 32-bit
11281    encodings (the latter only in post-V6T2 cores).  The index is the
11282    value used in the insns table below.  When there is more than one
11283    possible 16-bit encoding for the instruction, this table always
11284    holds variant (1).
11285    Also contains several pseudo-instructions used during relaxation.  */
11286 #define T16_32_TAB                              \
11287   X(_adc,   4140, eb400000),                    \
11288   X(_adcs,  4140, eb500000),                    \
11289   X(_add,   1c00, eb000000),                    \
11290   X(_adds,  1c00, eb100000),                    \
11291   X(_addi,  0000, f1000000),                    \
11292   X(_addis, 0000, f1100000),                    \
11293   X(_add_pc,000f, f20f0000),                    \
11294   X(_add_sp,000d, f10d0000),                    \
11295   X(_adr,   000f, f20f0000),                    \
11296   X(_and,   4000, ea000000),                    \
11297   X(_ands,  4000, ea100000),                    \
11298   X(_asr,   1000, fa40f000),                    \
11299   X(_asrs,  1000, fa50f000),                    \
11300   X(_b,     e000, f000b000),                    \
11301   X(_bcond, d000, f0008000),                    \
11302   X(_bf,    0000, f040e001),                    \
11303   X(_bfcsel,0000, f000e001),                    \
11304   X(_bfx,   0000, f060e001),                    \
11305   X(_bfl,   0000, f000c001),                    \
11306   X(_bflx,  0000, f070e001),                    \
11307   X(_bic,   4380, ea200000),                    \
11308   X(_bics,  4380, ea300000),                    \
11309   X(_cinc,  0000, ea509000),                    \
11310   X(_cinv,  0000, ea50a000),                    \
11311   X(_cmn,   42c0, eb100f00),                    \
11312   X(_cmp,   2800, ebb00f00),                    \
11313   X(_cneg,  0000, ea50b000),                    \
11314   X(_cpsie, b660, f3af8400),                    \
11315   X(_cpsid, b670, f3af8600),                    \
11316   X(_cpy,   4600, ea4f0000),                    \
11317   X(_csel,  0000, ea508000),                    \
11318   X(_cset,  0000, ea5f900f),                    \
11319   X(_csetm, 0000, ea5fa00f),                    \
11320   X(_csinc, 0000, ea509000),                    \
11321   X(_csinv, 0000, ea50a000),                    \
11322   X(_csneg, 0000, ea50b000),                    \
11323   X(_dec_sp,80dd, f1ad0d00),                    \
11324   X(_dls,   0000, f040e001),                    \
11325   X(_dlstp, 0000, f000e001),                    \
11326   X(_eor,   4040, ea800000),                    \
11327   X(_eors,  4040, ea900000),                    \
11328   X(_inc_sp,00dd, f10d0d00),                    \
11329   X(_lctp,  0000, f00fe001),                    \
11330   X(_ldmia, c800, e8900000),                    \
11331   X(_ldr,   6800, f8500000),                    \
11332   X(_ldrb,  7800, f8100000),                    \
11333   X(_ldrh,  8800, f8300000),                    \
11334   X(_ldrsb, 5600, f9100000),                    \
11335   X(_ldrsh, 5e00, f9300000),                    \
11336   X(_ldr_pc,4800, f85f0000),                    \
11337   X(_ldr_pc2,4800, f85f0000),                   \
11338   X(_ldr_sp,9800, f85d0000),                    \
11339   X(_le,    0000, f00fc001),                    \
11340   X(_letp,  0000, f01fc001),                    \
11341   X(_lsl,   0000, fa00f000),                    \
11342   X(_lsls,  0000, fa10f000),                    \
11343   X(_lsr,   0800, fa20f000),                    \
11344   X(_lsrs,  0800, fa30f000),                    \
11345   X(_mov,   2000, ea4f0000),                    \
11346   X(_movs,  2000, ea5f0000),                    \
11347   X(_mul,   4340, fb00f000),                     \
11348   X(_muls,  4340, ffffffff), /* no 32b muls */  \
11349   X(_mvn,   43c0, ea6f0000),                    \
11350   X(_mvns,  43c0, ea7f0000),                    \
11351   X(_neg,   4240, f1c00000), /* rsb #0 */       \
11352   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
11353   X(_orr,   4300, ea400000),                    \
11354   X(_orrs,  4300, ea500000),                    \
11355   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
11356   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
11357   X(_rev,   ba00, fa90f080),                    \
11358   X(_rev16, ba40, fa90f090),                    \
11359   X(_revsh, bac0, fa90f0b0),                    \
11360   X(_ror,   41c0, fa60f000),                    \
11361   X(_rors,  41c0, fa70f000),                    \
11362   X(_sbc,   4180, eb600000),                    \
11363   X(_sbcs,  4180, eb700000),                    \
11364   X(_stmia, c000, e8800000),                    \
11365   X(_str,   6000, f8400000),                    \
11366   X(_strb,  7000, f8000000),                    \
11367   X(_strh,  8000, f8200000),                    \
11368   X(_str_sp,9000, f84d0000),                    \
11369   X(_sub,   1e00, eba00000),                    \
11370   X(_subs,  1e00, ebb00000),                    \
11371   X(_subi,  8000, f1a00000),                    \
11372   X(_subis, 8000, f1b00000),                    \
11373   X(_sxtb,  b240, fa4ff080),                    \
11374   X(_sxth,  b200, fa0ff080),                    \
11375   X(_tst,   4200, ea100f00),                    \
11376   X(_uxtb,  b2c0, fa5ff080),                    \
11377   X(_uxth,  b280, fa1ff080),                    \
11378   X(_nop,   bf00, f3af8000),                    \
11379   X(_yield, bf10, f3af8001),                    \
11380   X(_wfe,   bf20, f3af8002),                    \
11381   X(_wfi,   bf30, f3af8003),                    \
11382   X(_wls,   0000, f040c001),                    \
11383   X(_wlstp, 0000, f000c001),                    \
11384   X(_sev,   bf40, f3af8004),                    \
11385   X(_sevl,  bf50, f3af8005),                    \
11386   X(_udf,   de00, f7f0a000)
11387
11388 /* To catch errors in encoding functions, the codes are all offset by
11389    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
11390    as 16-bit instructions.  */
11391 #define X(a,b,c) T_MNEM##a
11392 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
11393 #undef X
11394
11395 #define X(a,b,c) 0x##b
11396 static const unsigned short thumb_op16[] = { T16_32_TAB };
11397 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
11398 #undef X
11399
11400 #define X(a,b,c) 0x##c
11401 static const unsigned int thumb_op32[] = { T16_32_TAB };
11402 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
11403 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
11404 #undef X
11405 #undef T16_32_TAB
11406
11407 /* Thumb instruction encoders, in alphabetical order.  */
11408
11409 /* ADDW or SUBW.  */
11410
11411 static void
11412 do_t_add_sub_w (void)
11413 {
11414   int Rd, Rn;
11415
11416   Rd = inst.operands[0].reg;
11417   Rn = inst.operands[1].reg;
11418
11419   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
11420      is the SP-{plus,minus}-immediate form of the instruction.  */
11421   if (Rn == REG_SP)
11422     constraint (Rd == REG_PC, BAD_PC);
11423   else
11424     reject_bad_reg (Rd);
11425
11426   inst.instruction |= (Rn << 16) | (Rd << 8);
11427   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11428 }
11429
11430 /* Parse an add or subtract instruction.  We get here with inst.instruction
11431    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
11432
11433 static void
11434 do_t_add_sub (void)
11435 {
11436   int Rd, Rs, Rn;
11437
11438   Rd = inst.operands[0].reg;
11439   Rs = (inst.operands[1].present
11440         ? inst.operands[1].reg    /* Rd, Rs, foo */
11441         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11442
11443   if (Rd == REG_PC)
11444     set_pred_insn_type_last ();
11445
11446   if (unified_syntax)
11447     {
11448       bfd_boolean flags;
11449       bfd_boolean narrow;
11450       int opcode;
11451
11452       flags = (inst.instruction == T_MNEM_adds
11453                || inst.instruction == T_MNEM_subs);
11454       if (flags)
11455         narrow = !in_pred_block ();
11456       else
11457         narrow = in_pred_block ();
11458       if (!inst.operands[2].isreg)
11459         {
11460           int add;
11461
11462           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11463             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11464
11465           add = (inst.instruction == T_MNEM_add
11466                  || inst.instruction == T_MNEM_adds);
11467           opcode = 0;
11468           if (inst.size_req != 4)
11469             {
11470               /* Attempt to use a narrow opcode, with relaxation if
11471                  appropriate.  */
11472               if (Rd == REG_SP && Rs == REG_SP && !flags)
11473                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
11474               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
11475                 opcode = T_MNEM_add_sp;
11476               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
11477                 opcode = T_MNEM_add_pc;
11478               else if (Rd <= 7 && Rs <= 7 && narrow)
11479                 {
11480                   if (flags)
11481                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
11482                   else
11483                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
11484                 }
11485               if (opcode)
11486                 {
11487                   inst.instruction = THUMB_OP16(opcode);
11488                   inst.instruction |= (Rd << 4) | Rs;
11489                   if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11490                       || (inst.relocs[0].type
11491                           > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC))
11492                   {
11493                     if (inst.size_req == 2)
11494                       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11495                     else
11496                       inst.relax = opcode;
11497                   }
11498                 }
11499               else
11500                 constraint (inst.size_req == 2, BAD_HIREG);
11501             }
11502           if (inst.size_req == 4
11503               || (inst.size_req != 2 && !opcode))
11504             {
11505               constraint ((inst.relocs[0].type
11506                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
11507                           && (inst.relocs[0].type
11508                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
11509                           THUMB1_RELOC_ONLY);
11510               if (Rd == REG_PC)
11511                 {
11512                   constraint (add, BAD_PC);
11513                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
11514                              _("only SUBS PC, LR, #const allowed"));
11515                   constraint (inst.relocs[0].exp.X_op != O_constant,
11516                               _("expression too complex"));
11517                   constraint (inst.relocs[0].exp.X_add_number < 0
11518                               || inst.relocs[0].exp.X_add_number > 0xff,
11519                              _("immediate value out of range"));
11520                   inst.instruction = T2_SUBS_PC_LR
11521                                      | inst.relocs[0].exp.X_add_number;
11522                   inst.relocs[0].type = BFD_RELOC_UNUSED;
11523                   return;
11524                 }
11525               else if (Rs == REG_PC)
11526                 {
11527                   /* Always use addw/subw.  */
11528                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
11529                   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11530                 }
11531               else
11532                 {
11533                   inst.instruction = THUMB_OP32 (inst.instruction);
11534                   inst.instruction = (inst.instruction & 0xe1ffffff)
11535                                      | 0x10000000;
11536                   if (flags)
11537                     inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11538                   else
11539                     inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_IMM;
11540                 }
11541               inst.instruction |= Rd << 8;
11542               inst.instruction |= Rs << 16;
11543             }
11544         }
11545       else
11546         {
11547           unsigned int value = inst.relocs[0].exp.X_add_number;
11548           unsigned int shift = inst.operands[2].shift_kind;
11549
11550           Rn = inst.operands[2].reg;
11551           /* See if we can do this with a 16-bit instruction.  */
11552           if (!inst.operands[2].shifted && inst.size_req != 4)
11553             {
11554               if (Rd > 7 || Rs > 7 || Rn > 7)
11555                 narrow = FALSE;
11556
11557               if (narrow)
11558                 {
11559                   inst.instruction = ((inst.instruction == T_MNEM_adds
11560                                        || inst.instruction == T_MNEM_add)
11561                                       ? T_OPCODE_ADD_R3
11562                                       : T_OPCODE_SUB_R3);
11563                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11564                   return;
11565                 }
11566
11567               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
11568                 {
11569                   /* Thumb-1 cores (except v6-M) require at least one high
11570                      register in a narrow non flag setting add.  */
11571                   if (Rd > 7 || Rn > 7
11572                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
11573                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
11574                     {
11575                       if (Rd == Rn)
11576                         {
11577                           Rn = Rs;
11578                           Rs = Rd;
11579                         }
11580                       inst.instruction = T_OPCODE_ADD_HI;
11581                       inst.instruction |= (Rd & 8) << 4;
11582                       inst.instruction |= (Rd & 7);
11583                       inst.instruction |= Rn << 3;
11584                       return;
11585                     }
11586                 }
11587             }
11588
11589           constraint (Rd == REG_PC, BAD_PC);
11590           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11591             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11592           constraint (Rs == REG_PC, BAD_PC);
11593           reject_bad_reg (Rn);
11594
11595           /* If we get here, it can't be done in 16 bits.  */
11596           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
11597                       _("shift must be constant"));
11598           inst.instruction = THUMB_OP32 (inst.instruction);
11599           inst.instruction |= Rd << 8;
11600           inst.instruction |= Rs << 16;
11601           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
11602                       _("shift value over 3 not allowed in thumb mode"));
11603           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
11604                       _("only LSL shift allowed in thumb mode"));
11605           encode_thumb32_shifted_operand (2);
11606         }
11607     }
11608   else
11609     {
11610       constraint (inst.instruction == T_MNEM_adds
11611                   || inst.instruction == T_MNEM_subs,
11612                   BAD_THUMB32);
11613
11614       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
11615         {
11616           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
11617                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
11618                       BAD_HIREG);
11619
11620           inst.instruction = (inst.instruction == T_MNEM_add
11621                               ? 0x0000 : 0x8000);
11622           inst.instruction |= (Rd << 4) | Rs;
11623           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11624           return;
11625         }
11626
11627       Rn = inst.operands[2].reg;
11628       constraint (inst.operands[2].shifted, _("unshifted register required"));
11629
11630       /* We now have Rd, Rs, and Rn set to registers.  */
11631       if (Rd > 7 || Rs > 7 || Rn > 7)
11632         {
11633           /* Can't do this for SUB.      */
11634           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
11635           inst.instruction = T_OPCODE_ADD_HI;
11636           inst.instruction |= (Rd & 8) << 4;
11637           inst.instruction |= (Rd & 7);
11638           if (Rs == Rd)
11639             inst.instruction |= Rn << 3;
11640           else if (Rn == Rd)
11641             inst.instruction |= Rs << 3;
11642           else
11643             constraint (1, _("dest must overlap one source register"));
11644         }
11645       else
11646         {
11647           inst.instruction = (inst.instruction == T_MNEM_add
11648                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
11649           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11650         }
11651     }
11652 }
11653
11654 static void
11655 do_t_adr (void)
11656 {
11657   unsigned Rd;
11658
11659   Rd = inst.operands[0].reg;
11660   reject_bad_reg (Rd);
11661
11662   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
11663     {
11664       /* Defer to section relaxation.  */
11665       inst.relax = inst.instruction;
11666       inst.instruction = THUMB_OP16 (inst.instruction);
11667       inst.instruction |= Rd << 4;
11668     }
11669   else if (unified_syntax && inst.size_req != 2)
11670     {
11671       /* Generate a 32-bit opcode.  */
11672       inst.instruction = THUMB_OP32 (inst.instruction);
11673       inst.instruction |= Rd << 8;
11674       inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_PC12;
11675       inst.relocs[0].pc_rel = 1;
11676     }
11677   else
11678     {
11679       /* Generate a 16-bit opcode.  */
11680       inst.instruction = THUMB_OP16 (inst.instruction);
11681       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11682       inst.relocs[0].exp.X_add_number -= 4; /* PC relative adjust.  */
11683       inst.relocs[0].pc_rel = 1;
11684       inst.instruction |= Rd << 4;
11685     }
11686
11687   if (inst.relocs[0].exp.X_op == O_symbol
11688       && inst.relocs[0].exp.X_add_symbol != NULL
11689       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11690       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11691     inst.relocs[0].exp.X_add_number += 1;
11692 }
11693
11694 /* Arithmetic instructions for which there is just one 16-bit
11695    instruction encoding, and it allows only two low registers.
11696    For maximal compatibility with ARM syntax, we allow three register
11697    operands even when Thumb-32 instructions are not available, as long
11698    as the first two are identical.  For instance, both "sbc r0,r1" and
11699    "sbc r0,r0,r1" are allowed.  */
11700 static void
11701 do_t_arit3 (void)
11702 {
11703   int Rd, Rs, Rn;
11704
11705   Rd = inst.operands[0].reg;
11706   Rs = (inst.operands[1].present
11707         ? inst.operands[1].reg    /* Rd, Rs, foo */
11708         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11709   Rn = inst.operands[2].reg;
11710
11711   reject_bad_reg (Rd);
11712   reject_bad_reg (Rs);
11713   if (inst.operands[2].isreg)
11714     reject_bad_reg (Rn);
11715
11716   if (unified_syntax)
11717     {
11718       if (!inst.operands[2].isreg)
11719         {
11720           /* For an immediate, we always generate a 32-bit opcode;
11721              section relaxation will shrink it later if possible.  */
11722           inst.instruction = THUMB_OP32 (inst.instruction);
11723           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11724           inst.instruction |= Rd << 8;
11725           inst.instruction |= Rs << 16;
11726           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11727         }
11728       else
11729         {
11730           bfd_boolean narrow;
11731
11732           /* See if we can do this with a 16-bit instruction.  */
11733           if (THUMB_SETS_FLAGS (inst.instruction))
11734             narrow = !in_pred_block ();
11735           else
11736             narrow = in_pred_block ();
11737
11738           if (Rd > 7 || Rn > 7 || Rs > 7)
11739             narrow = FALSE;
11740           if (inst.operands[2].shifted)
11741             narrow = FALSE;
11742           if (inst.size_req == 4)
11743             narrow = FALSE;
11744
11745           if (narrow
11746               && Rd == Rs)
11747             {
11748               inst.instruction = THUMB_OP16 (inst.instruction);
11749               inst.instruction |= Rd;
11750               inst.instruction |= Rn << 3;
11751               return;
11752             }
11753
11754           /* If we get here, it can't be done in 16 bits.  */
11755           constraint (inst.operands[2].shifted
11756                       && inst.operands[2].immisreg,
11757                       _("shift must be constant"));
11758           inst.instruction = THUMB_OP32 (inst.instruction);
11759           inst.instruction |= Rd << 8;
11760           inst.instruction |= Rs << 16;
11761           encode_thumb32_shifted_operand (2);
11762         }
11763     }
11764   else
11765     {
11766       /* On its face this is a lie - the instruction does set the
11767          flags.  However, the only supported mnemonic in this mode
11768          says it doesn't.  */
11769       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11770
11771       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11772                   _("unshifted register required"));
11773       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11774       constraint (Rd != Rs,
11775                   _("dest and source1 must be the same register"));
11776
11777       inst.instruction = THUMB_OP16 (inst.instruction);
11778       inst.instruction |= Rd;
11779       inst.instruction |= Rn << 3;
11780     }
11781 }
11782
11783 /* Similarly, but for instructions where the arithmetic operation is
11784    commutative, so we can allow either of them to be different from
11785    the destination operand in a 16-bit instruction.  For instance, all
11786    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
11787    accepted.  */
11788 static void
11789 do_t_arit3c (void)
11790 {
11791   int Rd, Rs, Rn;
11792
11793   Rd = inst.operands[0].reg;
11794   Rs = (inst.operands[1].present
11795         ? inst.operands[1].reg    /* Rd, Rs, foo */
11796         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11797   Rn = inst.operands[2].reg;
11798
11799   reject_bad_reg (Rd);
11800   reject_bad_reg (Rs);
11801   if (inst.operands[2].isreg)
11802     reject_bad_reg (Rn);
11803
11804   if (unified_syntax)
11805     {
11806       if (!inst.operands[2].isreg)
11807         {
11808           /* For an immediate, we always generate a 32-bit opcode;
11809              section relaxation will shrink it later if possible.  */
11810           inst.instruction = THUMB_OP32 (inst.instruction);
11811           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11812           inst.instruction |= Rd << 8;
11813           inst.instruction |= Rs << 16;
11814           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11815         }
11816       else
11817         {
11818           bfd_boolean narrow;
11819
11820           /* See if we can do this with a 16-bit instruction.  */
11821           if (THUMB_SETS_FLAGS (inst.instruction))
11822             narrow = !in_pred_block ();
11823           else
11824             narrow = in_pred_block ();
11825
11826           if (Rd > 7 || Rn > 7 || Rs > 7)
11827             narrow = FALSE;
11828           if (inst.operands[2].shifted)
11829             narrow = FALSE;
11830           if (inst.size_req == 4)
11831             narrow = FALSE;
11832
11833           if (narrow)
11834             {
11835               if (Rd == Rs)
11836                 {
11837                   inst.instruction = THUMB_OP16 (inst.instruction);
11838                   inst.instruction |= Rd;
11839                   inst.instruction |= Rn << 3;
11840                   return;
11841                 }
11842               if (Rd == Rn)
11843                 {
11844                   inst.instruction = THUMB_OP16 (inst.instruction);
11845                   inst.instruction |= Rd;
11846                   inst.instruction |= Rs << 3;
11847                   return;
11848                 }
11849             }
11850
11851           /* If we get here, it can't be done in 16 bits.  */
11852           constraint (inst.operands[2].shifted
11853                       && inst.operands[2].immisreg,
11854                       _("shift must be constant"));
11855           inst.instruction = THUMB_OP32 (inst.instruction);
11856           inst.instruction |= Rd << 8;
11857           inst.instruction |= Rs << 16;
11858           encode_thumb32_shifted_operand (2);
11859         }
11860     }
11861   else
11862     {
11863       /* On its face this is a lie - the instruction does set the
11864          flags.  However, the only supported mnemonic in this mode
11865          says it doesn't.  */
11866       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11867
11868       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11869                   _("unshifted register required"));
11870       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11871
11872       inst.instruction = THUMB_OP16 (inst.instruction);
11873       inst.instruction |= Rd;
11874
11875       if (Rd == Rs)
11876         inst.instruction |= Rn << 3;
11877       else if (Rd == Rn)
11878         inst.instruction |= Rs << 3;
11879       else
11880         constraint (1, _("dest must overlap one source register"));
11881     }
11882 }
11883
11884 static void
11885 do_t_bfc (void)
11886 {
11887   unsigned Rd;
11888   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11889   constraint (msb > 32, _("bit-field extends past end of register"));
11890   /* The instruction encoding stores the LSB and MSB,
11891      not the LSB and width.  */
11892   Rd = inst.operands[0].reg;
11893   reject_bad_reg (Rd);
11894   inst.instruction |= Rd << 8;
11895   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11896   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11897   inst.instruction |= msb - 1;
11898 }
11899
11900 static void
11901 do_t_bfi (void)
11902 {
11903   int Rd, Rn;
11904   unsigned int msb;
11905
11906   Rd = inst.operands[0].reg;
11907   reject_bad_reg (Rd);
11908
11909   /* #0 in second position is alternative syntax for bfc, which is
11910      the same instruction but with REG_PC in the Rm field.  */
11911   if (!inst.operands[1].isreg)
11912     Rn = REG_PC;
11913   else
11914     {
11915       Rn = inst.operands[1].reg;
11916       reject_bad_reg (Rn);
11917     }
11918
11919   msb = inst.operands[2].imm + inst.operands[3].imm;
11920   constraint (msb > 32, _("bit-field extends past end of register"));
11921   /* The instruction encoding stores the LSB and MSB,
11922      not the LSB and width.  */
11923   inst.instruction |= Rd << 8;
11924   inst.instruction |= Rn << 16;
11925   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11926   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11927   inst.instruction |= msb - 1;
11928 }
11929
11930 static void
11931 do_t_bfx (void)
11932 {
11933   unsigned Rd, Rn;
11934
11935   Rd = inst.operands[0].reg;
11936   Rn = inst.operands[1].reg;
11937
11938   reject_bad_reg (Rd);
11939   reject_bad_reg (Rn);
11940
11941   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11942               _("bit-field extends past end of register"));
11943   inst.instruction |= Rd << 8;
11944   inst.instruction |= Rn << 16;
11945   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11946   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11947   inst.instruction |= inst.operands[3].imm - 1;
11948 }
11949
11950 /* ARM V5 Thumb BLX (argument parse)
11951         BLX <target_addr>       which is BLX(1)
11952         BLX <Rm>                which is BLX(2)
11953    Unfortunately, there are two different opcodes for this mnemonic.
11954    So, the insns[].value is not used, and the code here zaps values
11955         into inst.instruction.
11956
11957    ??? How to take advantage of the additional two bits of displacement
11958    available in Thumb32 mode?  Need new relocation?  */
11959
11960 static void
11961 do_t_blx (void)
11962 {
11963   set_pred_insn_type_last ();
11964
11965   if (inst.operands[0].isreg)
11966     {
11967       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11968       /* We have a register, so this is BLX(2).  */
11969       inst.instruction |= inst.operands[0].reg << 3;
11970     }
11971   else
11972     {
11973       /* No register.  This must be BLX(1).  */
11974       inst.instruction = 0xf000e800;
11975       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11976     }
11977 }
11978
11979 static void
11980 do_t_branch (void)
11981 {
11982   int opcode;
11983   int cond;
11984   bfd_reloc_code_real_type reloc;
11985
11986   cond = inst.cond;
11987   set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);
11988
11989   if (in_pred_block ())
11990     {
11991       /* Conditional branches inside IT blocks are encoded as unconditional
11992          branches.  */
11993       cond = COND_ALWAYS;
11994     }
11995   else
11996     cond = inst.cond;
11997
11998   if (cond != COND_ALWAYS)
11999     opcode = T_MNEM_bcond;
12000   else
12001     opcode = inst.instruction;
12002
12003   if (unified_syntax
12004       && (inst.size_req == 4
12005           || (inst.size_req != 2
12006               && (inst.operands[0].hasreloc
12007                   || inst.relocs[0].exp.X_op == O_constant))))
12008     {
12009       inst.instruction = THUMB_OP32(opcode);
12010       if (cond == COND_ALWAYS)
12011         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
12012       else
12013         {
12014           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
12015                       _("selected architecture does not support "
12016                         "wide conditional branch instruction"));
12017
12018           gas_assert (cond != 0xF);
12019           inst.instruction |= cond << 22;
12020           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
12021         }
12022     }
12023   else
12024     {
12025       inst.instruction = THUMB_OP16(opcode);
12026       if (cond == COND_ALWAYS)
12027         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
12028       else
12029         {
12030           inst.instruction |= cond << 8;
12031           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
12032         }
12033       /* Allow section relaxation.  */
12034       if (unified_syntax && inst.size_req != 2)
12035         inst.relax = opcode;
12036     }
12037   inst.relocs[0].type = reloc;
12038   inst.relocs[0].pc_rel = 1;
12039 }
12040
12041 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
12042    between the two is the maximum immediate allowed - which is passed in
12043    RANGE.  */
12044 static void
12045 do_t_bkpt_hlt1 (int range)
12046 {
12047   constraint (inst.cond != COND_ALWAYS,
12048               _("instruction is always unconditional"));
12049   if (inst.operands[0].present)
12050     {
12051       constraint (inst.operands[0].imm > range,
12052                   _("immediate value out of range"));
12053       inst.instruction |= inst.operands[0].imm;
12054     }
12055
12056   set_pred_insn_type (NEUTRAL_IT_INSN);
12057 }
12058
12059 static void
12060 do_t_hlt (void)
12061 {
12062   do_t_bkpt_hlt1 (63);
12063 }
12064
12065 static void
12066 do_t_bkpt (void)
12067 {
12068   do_t_bkpt_hlt1 (255);
12069 }
12070
12071 static void
12072 do_t_branch23 (void)
12073 {
12074   set_pred_insn_type_last ();
12075   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
12076
12077   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
12078      this file.  We used to simply ignore the PLT reloc type here --
12079      the branch encoding is now needed to deal with TLSCALL relocs.
12080      So if we see a PLT reloc now, put it back to how it used to be to
12081      keep the preexisting behaviour.  */
12082   if (inst.relocs[0].type == BFD_RELOC_ARM_PLT32)
12083     inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH23;
12084
12085 #if defined(OBJ_COFF)
12086   /* If the destination of the branch is a defined symbol which does not have
12087      the THUMB_FUNC attribute, then we must be calling a function which has
12088      the (interfacearm) attribute.  We look for the Thumb entry point to that
12089      function and change the branch to refer to that function instead.  */
12090   if (   inst.relocs[0].exp.X_op == O_symbol
12091       && inst.relocs[0].exp.X_add_symbol != NULL
12092       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
12093       && ! THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
12094     inst.relocs[0].exp.X_add_symbol
12095       = find_real_start (inst.relocs[0].exp.X_add_symbol);
12096 #endif
12097 }
12098
12099 static void
12100 do_t_bx (void)
12101 {
12102   set_pred_insn_type_last ();
12103   inst.instruction |= inst.operands[0].reg << 3;
12104   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
12105      should cause the alignment to be checked once it is known.  This is
12106      because BX PC only works if the instruction is word aligned.  */
12107 }
12108
12109 static void
12110 do_t_bxj (void)
12111 {
12112   int Rm;
12113
12114   set_pred_insn_type_last ();
12115   Rm = inst.operands[0].reg;
12116   reject_bad_reg (Rm);
12117   inst.instruction |= Rm << 16;
12118 }
12119
12120 static void
12121 do_t_clz (void)
12122 {
12123   unsigned Rd;
12124   unsigned Rm;
12125
12126   Rd = inst.operands[0].reg;
12127   Rm = inst.operands[1].reg;
12128
12129   reject_bad_reg (Rd);
12130   reject_bad_reg (Rm);
12131
12132   inst.instruction |= Rd << 8;
12133   inst.instruction |= Rm << 16;
12134   inst.instruction |= Rm;
12135 }
12136
12137 /* For the Armv8.1-M conditional instructions.  */
12138 static void
12139 do_t_cond (void)
12140 {
12141   unsigned Rd, Rn, Rm;
12142   signed int cond;
12143
12144   constraint (inst.cond != COND_ALWAYS, BAD_COND);
12145
12146   Rd = inst.operands[0].reg;
12147   switch (inst.instruction)
12148     {
12149       case T_MNEM_csinc:
12150       case T_MNEM_csinv:
12151       case T_MNEM_csneg:
12152       case T_MNEM_csel:
12153         Rn = inst.operands[1].reg;
12154         Rm = inst.operands[2].reg;
12155         cond = inst.operands[3].imm;
12156         constraint (Rn == REG_SP, BAD_SP);
12157         constraint (Rm == REG_SP, BAD_SP);
12158         break;
12159
12160       case T_MNEM_cinc:
12161       case T_MNEM_cinv:
12162       case T_MNEM_cneg:
12163         Rn = inst.operands[1].reg;
12164         cond = inst.operands[2].imm;
12165         /* Invert the last bit to invert the cond.  */
12166         cond = TOGGLE_BIT (cond, 0);
12167         constraint (Rn == REG_SP, BAD_SP);
12168         Rm = Rn;
12169         break;
12170
12171       case T_MNEM_csetm:
12172       case T_MNEM_cset:
12173         cond = inst.operands[1].imm;
12174         /* Invert the last bit to invert the cond.  */
12175         cond = TOGGLE_BIT (cond, 0);
12176         Rn = REG_PC;
12177         Rm = REG_PC;
12178         break;
12179
12180       default: abort ();
12181     }
12182
12183   set_pred_insn_type (OUTSIDE_PRED_INSN);
12184   inst.instruction = THUMB_OP32 (inst.instruction);
12185   inst.instruction |= Rd << 8;
12186   inst.instruction |= Rn << 16;
12187   inst.instruction |= Rm;
12188   inst.instruction |= cond << 4;
12189 }
12190
12191 static void
12192 do_t_csdb (void)
12193 {
12194   set_pred_insn_type (OUTSIDE_PRED_INSN);
12195 }
12196
12197 static void
12198 do_t_cps (void)
12199 {
12200   set_pred_insn_type (OUTSIDE_PRED_INSN);
12201   inst.instruction |= inst.operands[0].imm;
12202 }
12203
12204 static void
12205 do_t_cpsi (void)
12206 {
12207   set_pred_insn_type (OUTSIDE_PRED_INSN);
12208   if (unified_syntax
12209       && (inst.operands[1].present || inst.size_req == 4)
12210       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
12211     {
12212       unsigned int imod = (inst.instruction & 0x0030) >> 4;
12213       inst.instruction = 0xf3af8000;
12214       inst.instruction |= imod << 9;
12215       inst.instruction |= inst.operands[0].imm << 5;
12216       if (inst.operands[1].present)
12217         inst.instruction |= 0x100 | inst.operands[1].imm;
12218     }
12219   else
12220     {
12221       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
12222                   && (inst.operands[0].imm & 4),
12223                   _("selected processor does not support 'A' form "
12224                     "of this instruction"));
12225       constraint (inst.operands[1].present || inst.size_req == 4,
12226                   _("Thumb does not support the 2-argument "
12227                     "form of this instruction"));
12228       inst.instruction |= inst.operands[0].imm;
12229     }
12230 }
12231
12232 /* THUMB CPY instruction (argument parse).  */
12233
12234 static void
12235 do_t_cpy (void)
12236 {
12237   if (inst.size_req == 4)
12238     {
12239       inst.instruction = THUMB_OP32 (T_MNEM_mov);
12240       inst.instruction |= inst.operands[0].reg << 8;
12241       inst.instruction |= inst.operands[1].reg;
12242     }
12243   else
12244     {
12245       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
12246       inst.instruction |= (inst.operands[0].reg & 0x7);
12247       inst.instruction |= inst.operands[1].reg << 3;
12248     }
12249 }
12250
12251 static void
12252 do_t_cbz (void)
12253 {
12254   set_pred_insn_type (OUTSIDE_PRED_INSN);
12255   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12256   inst.instruction |= inst.operands[0].reg;
12257   inst.relocs[0].pc_rel = 1;
12258   inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH7;
12259 }
12260
12261 static void
12262 do_t_dbg (void)
12263 {
12264   inst.instruction |= inst.operands[0].imm;
12265 }
12266
12267 static void
12268 do_t_div (void)
12269 {
12270   unsigned Rd, Rn, Rm;
12271
12272   Rd = inst.operands[0].reg;
12273   Rn = (inst.operands[1].present
12274         ? inst.operands[1].reg : Rd);
12275   Rm = inst.operands[2].reg;
12276
12277   reject_bad_reg (Rd);
12278   reject_bad_reg (Rn);
12279   reject_bad_reg (Rm);
12280
12281   inst.instruction |= Rd << 8;
12282   inst.instruction |= Rn << 16;
12283   inst.instruction |= Rm;
12284 }
12285
12286 static void
12287 do_t_hint (void)
12288 {
12289   if (unified_syntax && inst.size_req == 4)
12290     inst.instruction = THUMB_OP32 (inst.instruction);
12291   else
12292     inst.instruction = THUMB_OP16 (inst.instruction);
12293 }
12294
12295 static void
12296 do_t_it (void)
12297 {
12298   unsigned int cond = inst.operands[0].imm;
12299
12300   set_pred_insn_type (IT_INSN);
12301   now_pred.mask = (inst.instruction & 0xf) | 0x10;
12302   now_pred.cc = cond;
12303   now_pred.warn_deprecated = FALSE;
12304   now_pred.type = SCALAR_PRED;
12305
12306   /* If the condition is a negative condition, invert the mask.  */
12307   if ((cond & 0x1) == 0x0)
12308     {
12309       unsigned int mask = inst.instruction & 0x000f;
12310
12311       if ((mask & 0x7) == 0)
12312         {
12313           /* No conversion needed.  */
12314           now_pred.block_length = 1;
12315         }
12316       else if ((mask & 0x3) == 0)
12317         {
12318           mask ^= 0x8;
12319           now_pred.block_length = 2;
12320         }
12321       else if ((mask & 0x1) == 0)
12322         {
12323           mask ^= 0xC;
12324           now_pred.block_length = 3;
12325         }
12326       else
12327         {
12328           mask ^= 0xE;
12329           now_pred.block_length = 4;
12330         }
12331
12332       inst.instruction &= 0xfff0;
12333       inst.instruction |= mask;
12334     }
12335
12336   inst.instruction |= cond << 4;
12337 }
12338
12339 /* Helper function used for both push/pop and ldm/stm.  */
12340 static void
12341 encode_thumb2_multi (bfd_boolean do_io, int base, unsigned mask,
12342                      bfd_boolean writeback)
12343 {
12344   bfd_boolean load, store;
12345
12346   gas_assert (base != -1 || !do_io);
12347   load = do_io && ((inst.instruction & (1 << 20)) != 0);
12348   store = do_io && !load;
12349
12350   if (mask & (1 << 13))
12351     inst.error =  _("SP not allowed in register list");
12352
12353   if (do_io && (mask & (1 << base)) != 0
12354       && writeback)
12355     inst.error = _("having the base register in the register list when "
12356                    "using write back is UNPREDICTABLE");
12357
12358   if (load)
12359     {
12360       if (mask & (1 << 15))
12361         {
12362           if (mask & (1 << 14))
12363             inst.error = _("LR and PC should not both be in register list");
12364           else
12365             set_pred_insn_type_last ();
12366         }
12367     }
12368   else if (store)
12369     {
12370       if (mask & (1 << 15))
12371         inst.error = _("PC not allowed in register list");
12372     }
12373
12374   if (do_io && ((mask & (mask - 1)) == 0))
12375     {
12376       /* Single register transfers implemented as str/ldr.  */
12377       if (writeback)
12378         {
12379           if (inst.instruction & (1 << 23))
12380             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
12381           else
12382             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
12383         }
12384       else
12385         {
12386           if (inst.instruction & (1 << 23))
12387             inst.instruction = 0x00800000; /* ia -> [base] */
12388           else
12389             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
12390         }
12391
12392       inst.instruction |= 0xf8400000;
12393       if (load)
12394         inst.instruction |= 0x00100000;
12395
12396       mask = ffs (mask) - 1;
12397       mask <<= 12;
12398     }
12399   else if (writeback)
12400     inst.instruction |= WRITE_BACK;
12401
12402   inst.instruction |= mask;
12403   if (do_io)
12404     inst.instruction |= base << 16;
12405 }
12406
12407 static void
12408 do_t_ldmstm (void)
12409 {
12410   /* This really doesn't seem worth it.  */
12411   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
12412               _("expression too complex"));
12413   constraint (inst.operands[1].writeback,
12414               _("Thumb load/store multiple does not support {reglist}^"));
12415
12416   if (unified_syntax)
12417     {
12418       bfd_boolean narrow;
12419       unsigned mask;
12420
12421       narrow = FALSE;
12422       /* See if we can use a 16-bit instruction.  */
12423       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
12424           && inst.size_req != 4
12425           && !(inst.operands[1].imm & ~0xff))
12426         {
12427           mask = 1 << inst.operands[0].reg;
12428
12429           if (inst.operands[0].reg <= 7)
12430             {
12431               if (inst.instruction == T_MNEM_stmia
12432                   ? inst.operands[0].writeback
12433                   : (inst.operands[0].writeback
12434                      == !(inst.operands[1].imm & mask)))
12435                 {
12436                   if (inst.instruction == T_MNEM_stmia
12437                       && (inst.operands[1].imm & mask)
12438                       && (inst.operands[1].imm & (mask - 1)))
12439                     as_warn (_("value stored for r%d is UNKNOWN"),
12440                              inst.operands[0].reg);
12441
12442                   inst.instruction = THUMB_OP16 (inst.instruction);
12443                   inst.instruction |= inst.operands[0].reg << 8;
12444                   inst.instruction |= inst.operands[1].imm;
12445                   narrow = TRUE;
12446                 }
12447               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12448                 {
12449                   /* This means 1 register in reg list one of 3 situations:
12450                      1. Instruction is stmia, but without writeback.
12451                      2. lmdia without writeback, but with Rn not in
12452                         reglist.
12453                      3. ldmia with writeback, but with Rn in reglist.
12454                      Case 3 is UNPREDICTABLE behaviour, so we handle
12455                      case 1 and 2 which can be converted into a 16-bit
12456                      str or ldr. The SP cases are handled below.  */
12457                   unsigned long opcode;
12458                   /* First, record an error for Case 3.  */
12459                   if (inst.operands[1].imm & mask
12460                       && inst.operands[0].writeback)
12461                     inst.error =
12462                         _("having the base register in the register list when "
12463                           "using write back is UNPREDICTABLE");
12464
12465                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
12466                                                              : T_MNEM_ldr);
12467                   inst.instruction = THUMB_OP16 (opcode);
12468                   inst.instruction |= inst.operands[0].reg << 3;
12469                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
12470                   narrow = TRUE;
12471                 }
12472             }
12473           else if (inst.operands[0] .reg == REG_SP)
12474             {
12475               if (inst.operands[0].writeback)
12476                 {
12477                   inst.instruction =
12478                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12479                                     ? T_MNEM_push : T_MNEM_pop);
12480                   inst.instruction |= inst.operands[1].imm;
12481                   narrow = TRUE;
12482                 }
12483               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12484                 {
12485                   inst.instruction =
12486                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12487                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
12488                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
12489                   narrow = TRUE;
12490                 }
12491             }
12492         }
12493
12494       if (!narrow)
12495         {
12496           if (inst.instruction < 0xffff)
12497             inst.instruction = THUMB_OP32 (inst.instruction);
12498
12499           encode_thumb2_multi (TRUE /* do_io */, inst.operands[0].reg,
12500                                inst.operands[1].imm,
12501                                inst.operands[0].writeback);
12502         }
12503     }
12504   else
12505     {
12506       constraint (inst.operands[0].reg > 7
12507                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
12508       constraint (inst.instruction != T_MNEM_ldmia
12509                   && inst.instruction != T_MNEM_stmia,
12510                   _("Thumb-2 instruction only valid in unified syntax"));
12511       if (inst.instruction == T_MNEM_stmia)
12512         {
12513           if (!inst.operands[0].writeback)
12514             as_warn (_("this instruction will write back the base register"));
12515           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
12516               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
12517             as_warn (_("value stored for r%d is UNKNOWN"),
12518                      inst.operands[0].reg);
12519         }
12520       else
12521         {
12522           if (!inst.operands[0].writeback
12523               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
12524             as_warn (_("this instruction will write back the base register"));
12525           else if (inst.operands[0].writeback
12526                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
12527             as_warn (_("this instruction will not write back the base register"));
12528         }
12529
12530       inst.instruction = THUMB_OP16 (inst.instruction);
12531       inst.instruction |= inst.operands[0].reg << 8;
12532       inst.instruction |= inst.operands[1].imm;
12533     }
12534 }
12535
12536 static void
12537 do_t_ldrex (void)
12538 {
12539   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
12540               || inst.operands[1].postind || inst.operands[1].writeback
12541               || inst.operands[1].immisreg || inst.operands[1].shifted
12542               || inst.operands[1].negative,
12543               BAD_ADDR_MODE);
12544
12545   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
12546
12547   inst.instruction |= inst.operands[0].reg << 12;
12548   inst.instruction |= inst.operands[1].reg << 16;
12549   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
12550 }
12551
12552 static void
12553 do_t_ldrexd (void)
12554 {
12555   if (!inst.operands[1].present)
12556     {
12557       constraint (inst.operands[0].reg == REG_LR,
12558                   _("r14 not allowed as first register "
12559                     "when second register is omitted"));
12560       inst.operands[1].reg = inst.operands[0].reg + 1;
12561     }
12562   constraint (inst.operands[0].reg == inst.operands[1].reg,
12563               BAD_OVERLAP);
12564
12565   inst.instruction |= inst.operands[0].reg << 12;
12566   inst.instruction |= inst.operands[1].reg << 8;
12567   inst.instruction |= inst.operands[2].reg << 16;
12568 }
12569
12570 static void
12571 do_t_ldst (void)
12572 {
12573   unsigned long opcode;
12574   int Rn;
12575
12576   if (inst.operands[0].isreg
12577       && !inst.operands[0].preind
12578       && inst.operands[0].reg == REG_PC)
12579     set_pred_insn_type_last ();
12580
12581   opcode = inst.instruction;
12582   if (unified_syntax)
12583     {
12584       if (!inst.operands[1].isreg)
12585         {
12586           if (opcode <= 0xffff)
12587             inst.instruction = THUMB_OP32 (opcode);
12588           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12589             return;
12590         }
12591       if (inst.operands[1].isreg
12592           && !inst.operands[1].writeback
12593           && !inst.operands[1].shifted && !inst.operands[1].postind
12594           && !inst.operands[1].negative && inst.operands[0].reg <= 7
12595           && opcode <= 0xffff
12596           && inst.size_req != 4)
12597         {
12598           /* Insn may have a 16-bit form.  */
12599           Rn = inst.operands[1].reg;
12600           if (inst.operands[1].immisreg)
12601             {
12602               inst.instruction = THUMB_OP16 (opcode);
12603               /* [Rn, Rik] */
12604               if (Rn <= 7 && inst.operands[1].imm <= 7)
12605                 goto op16;
12606               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
12607                 reject_bad_reg (inst.operands[1].imm);
12608             }
12609           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
12610                     && opcode != T_MNEM_ldrsb)
12611                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
12612                    || (Rn == REG_SP && opcode == T_MNEM_str))
12613             {
12614               /* [Rn, #const] */
12615               if (Rn > 7)
12616                 {
12617                   if (Rn == REG_PC)
12618                     {
12619                       if (inst.relocs[0].pc_rel)
12620                         opcode = T_MNEM_ldr_pc2;
12621                       else
12622                         opcode = T_MNEM_ldr_pc;
12623                     }
12624                   else
12625                     {
12626                       if (opcode == T_MNEM_ldr)
12627                         opcode = T_MNEM_ldr_sp;
12628                       else
12629                         opcode = T_MNEM_str_sp;
12630                     }
12631                   inst.instruction = inst.operands[0].reg << 8;
12632                 }
12633               else
12634                 {
12635                   inst.instruction = inst.operands[0].reg;
12636                   inst.instruction |= inst.operands[1].reg << 3;
12637                 }
12638               inst.instruction |= THUMB_OP16 (opcode);
12639               if (inst.size_req == 2)
12640                 inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12641               else
12642                 inst.relax = opcode;
12643               return;
12644             }
12645         }
12646       /* Definitely a 32-bit variant.  */
12647
12648       /* Warning for Erratum 752419.  */
12649       if (opcode == T_MNEM_ldr
12650           && inst.operands[0].reg == REG_SP
12651           && inst.operands[1].writeback == 1
12652           && !inst.operands[1].immisreg)
12653         {
12654           if (no_cpu_selected ()
12655               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
12656                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
12657                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
12658             as_warn (_("This instruction may be unpredictable "
12659                        "if executed on M-profile cores "
12660                        "with interrupts enabled."));
12661         }
12662
12663       /* Do some validations regarding addressing modes.  */
12664       if (inst.operands[1].immisreg)
12665         reject_bad_reg (inst.operands[1].imm);
12666
12667       constraint (inst.operands[1].writeback == 1
12668                   && inst.operands[0].reg == inst.operands[1].reg,
12669                   BAD_OVERLAP);
12670
12671       inst.instruction = THUMB_OP32 (opcode);
12672       inst.instruction |= inst.operands[0].reg << 12;
12673       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
12674       check_ldr_r15_aligned ();
12675       return;
12676     }
12677
12678   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12679
12680   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
12681     {
12682       /* Only [Rn,Rm] is acceptable.  */
12683       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
12684       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
12685                   || inst.operands[1].postind || inst.operands[1].shifted
12686                   || inst.operands[1].negative,
12687                   _("Thumb does not support this addressing mode"));
12688       inst.instruction = THUMB_OP16 (inst.instruction);
12689       goto op16;
12690     }
12691
12692   inst.instruction = THUMB_OP16 (inst.instruction);
12693   if (!inst.operands[1].isreg)
12694     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12695       return;
12696
12697   constraint (!inst.operands[1].preind
12698               || inst.operands[1].shifted
12699               || inst.operands[1].writeback,
12700               _("Thumb does not support this addressing mode"));
12701   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
12702     {
12703       constraint (inst.instruction & 0x0600,
12704                   _("byte or halfword not valid for base register"));
12705       constraint (inst.operands[1].reg == REG_PC
12706                   && !(inst.instruction & THUMB_LOAD_BIT),
12707                   _("r15 based store not allowed"));
12708       constraint (inst.operands[1].immisreg,
12709                   _("invalid base register for register offset"));
12710
12711       if (inst.operands[1].reg == REG_PC)
12712         inst.instruction = T_OPCODE_LDR_PC;
12713       else if (inst.instruction & THUMB_LOAD_BIT)
12714         inst.instruction = T_OPCODE_LDR_SP;
12715       else
12716         inst.instruction = T_OPCODE_STR_SP;
12717
12718       inst.instruction |= inst.operands[0].reg << 8;
12719       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12720       return;
12721     }
12722
12723   constraint (inst.operands[1].reg > 7, BAD_HIREG);
12724   if (!inst.operands[1].immisreg)
12725     {
12726       /* Immediate offset.  */
12727       inst.instruction |= inst.operands[0].reg;
12728       inst.instruction |= inst.operands[1].reg << 3;
12729       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12730       return;
12731     }
12732
12733   /* Register offset.  */
12734   constraint (inst.operands[1].imm > 7, BAD_HIREG);
12735   constraint (inst.operands[1].negative,
12736               _("Thumb does not support this addressing mode"));
12737
12738  op16:
12739   switch (inst.instruction)
12740     {
12741     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
12742     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
12743     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
12744     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
12745     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
12746     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
12747     case 0x5600 /* ldrsb */:
12748     case 0x5e00 /* ldrsh */: break;
12749     default: abort ();
12750     }
12751
12752   inst.instruction |= inst.operands[0].reg;
12753   inst.instruction |= inst.operands[1].reg << 3;
12754   inst.instruction |= inst.operands[1].imm << 6;
12755 }
12756
12757 static void
12758 do_t_ldstd (void)
12759 {
12760   if (!inst.operands[1].present)
12761     {
12762       inst.operands[1].reg = inst.operands[0].reg + 1;
12763       constraint (inst.operands[0].reg == REG_LR,
12764                   _("r14 not allowed here"));
12765       constraint (inst.operands[0].reg == REG_R12,
12766                   _("r12 not allowed here"));
12767     }
12768
12769   if (inst.operands[2].writeback
12770       && (inst.operands[0].reg == inst.operands[2].reg
12771       || inst.operands[1].reg == inst.operands[2].reg))
12772     as_warn (_("base register written back, and overlaps "
12773                "one of transfer registers"));
12774
12775   inst.instruction |= inst.operands[0].reg << 12;
12776   inst.instruction |= inst.operands[1].reg << 8;
12777   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
12778 }
12779
12780 static void
12781 do_t_ldstt (void)
12782 {
12783   inst.instruction |= inst.operands[0].reg << 12;
12784   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
12785 }
12786
12787 static void
12788 do_t_mla (void)
12789 {
12790   unsigned Rd, Rn, Rm, Ra;
12791
12792   Rd = inst.operands[0].reg;
12793   Rn = inst.operands[1].reg;
12794   Rm = inst.operands[2].reg;
12795   Ra = inst.operands[3].reg;
12796
12797   reject_bad_reg (Rd);
12798   reject_bad_reg (Rn);
12799   reject_bad_reg (Rm);
12800   reject_bad_reg (Ra);
12801
12802   inst.instruction |= Rd << 8;
12803   inst.instruction |= Rn << 16;
12804   inst.instruction |= Rm;
12805   inst.instruction |= Ra << 12;
12806 }
12807
12808 static void
12809 do_t_mlal (void)
12810 {
12811   unsigned RdLo, RdHi, Rn, Rm;
12812
12813   RdLo = inst.operands[0].reg;
12814   RdHi = inst.operands[1].reg;
12815   Rn = inst.operands[2].reg;
12816   Rm = inst.operands[3].reg;
12817
12818   reject_bad_reg (RdLo);
12819   reject_bad_reg (RdHi);
12820   reject_bad_reg (Rn);
12821   reject_bad_reg (Rm);
12822
12823   inst.instruction |= RdLo << 12;
12824   inst.instruction |= RdHi << 8;
12825   inst.instruction |= Rn << 16;
12826   inst.instruction |= Rm;
12827 }
12828
12829 static void
12830 do_t_mov_cmp (void)
12831 {
12832   unsigned Rn, Rm;
12833
12834   Rn = inst.operands[0].reg;
12835   Rm = inst.operands[1].reg;
12836
12837   if (Rn == REG_PC)
12838     set_pred_insn_type_last ();
12839
12840   if (unified_syntax)
12841     {
12842       int r0off = (inst.instruction == T_MNEM_mov
12843                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
12844       unsigned long opcode;
12845       bfd_boolean narrow;
12846       bfd_boolean low_regs;
12847
12848       low_regs = (Rn <= 7 && Rm <= 7);
12849       opcode = inst.instruction;
12850       if (in_pred_block ())
12851         narrow = opcode != T_MNEM_movs;
12852       else
12853         narrow = opcode != T_MNEM_movs || low_regs;
12854       if (inst.size_req == 4
12855           || inst.operands[1].shifted)
12856         narrow = FALSE;
12857
12858       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
12859       if (opcode == T_MNEM_movs && inst.operands[1].isreg
12860           && !inst.operands[1].shifted
12861           && Rn == REG_PC
12862           && Rm == REG_LR)
12863         {
12864           inst.instruction = T2_SUBS_PC_LR;
12865           return;
12866         }
12867
12868       if (opcode == T_MNEM_cmp)
12869         {
12870           constraint (Rn == REG_PC, BAD_PC);
12871           if (narrow)
12872             {
12873               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
12874                  but valid.  */
12875               warn_deprecated_sp (Rm);
12876               /* R15 was documented as a valid choice for Rm in ARMv6,
12877                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
12878                  tools reject R15, so we do too.  */
12879               constraint (Rm == REG_PC, BAD_PC);
12880             }
12881           else
12882             reject_bad_reg (Rm);
12883         }
12884       else if (opcode == T_MNEM_mov
12885                || opcode == T_MNEM_movs)
12886         {
12887           if (inst.operands[1].isreg)
12888             {
12889               if (opcode == T_MNEM_movs)
12890                 {
12891                   reject_bad_reg (Rn);
12892                   reject_bad_reg (Rm);
12893                 }
12894               else if (narrow)
12895                 {
12896                   /* This is mov.n.  */
12897                   if ((Rn == REG_SP || Rn == REG_PC)
12898                       && (Rm == REG_SP || Rm == REG_PC))
12899                     {
12900                       as_tsktsk (_("Use of r%u as a source register is "
12901                                  "deprecated when r%u is the destination "
12902                                  "register."), Rm, Rn);
12903                     }
12904                 }
12905               else
12906                 {
12907                   /* This is mov.w.  */
12908                   constraint (Rn == REG_PC, BAD_PC);
12909                   constraint (Rm == REG_PC, BAD_PC);
12910                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12911                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
12912                 }
12913             }
12914           else
12915             reject_bad_reg (Rn);
12916         }
12917
12918       if (!inst.operands[1].isreg)
12919         {
12920           /* Immediate operand.  */
12921           if (!in_pred_block () && opcode == T_MNEM_mov)
12922             narrow = 0;
12923           if (low_regs && narrow)
12924             {
12925               inst.instruction = THUMB_OP16 (opcode);
12926               inst.instruction |= Rn << 8;
12927               if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12928                   || inst.relocs[0].type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12929                 {
12930                   if (inst.size_req == 2)
12931                     inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12932                   else
12933                     inst.relax = opcode;
12934                 }
12935             }
12936           else
12937             {
12938               constraint ((inst.relocs[0].type
12939                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
12940                           && (inst.relocs[0].type
12941                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
12942                           THUMB1_RELOC_ONLY);
12943
12944               inst.instruction = THUMB_OP32 (inst.instruction);
12945               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12946               inst.instruction |= Rn << r0off;
12947               inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12948             }
12949         }
12950       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12951                && (inst.instruction == T_MNEM_mov
12952                    || inst.instruction == T_MNEM_movs))
12953         {
12954           /* Register shifts are encoded as separate shift instructions.  */
12955           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12956
12957           if (in_pred_block ())
12958             narrow = !flags;
12959           else
12960             narrow = flags;
12961
12962           if (inst.size_req == 4)
12963             narrow = FALSE;
12964
12965           if (!low_regs || inst.operands[1].imm > 7)
12966             narrow = FALSE;
12967
12968           if (Rn != Rm)
12969             narrow = FALSE;
12970
12971           switch (inst.operands[1].shift_kind)
12972             {
12973             case SHIFT_LSL:
12974               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12975               break;
12976             case SHIFT_ASR:
12977               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12978               break;
12979             case SHIFT_LSR:
12980               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12981               break;
12982             case SHIFT_ROR:
12983               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12984               break;
12985             default:
12986               abort ();
12987             }
12988
12989           inst.instruction = opcode;
12990           if (narrow)
12991             {
12992               inst.instruction |= Rn;
12993               inst.instruction |= inst.operands[1].imm << 3;
12994             }
12995           else
12996             {
12997               if (flags)
12998                 inst.instruction |= CONDS_BIT;
12999
13000               inst.instruction |= Rn << 8;
13001               inst.instruction |= Rm << 16;
13002               inst.instruction |= inst.operands[1].imm;
13003             }
13004         }
13005       else if (!narrow)
13006         {
13007           /* Some mov with immediate shift have narrow variants.
13008              Register shifts are handled above.  */
13009           if (low_regs && inst.operands[1].shifted
13010               && (inst.instruction == T_MNEM_mov
13011                   || inst.instruction == T_MNEM_movs))
13012             {
13013               if (in_pred_block ())
13014                 narrow = (inst.instruction == T_MNEM_mov);
13015               else
13016                 narrow = (inst.instruction == T_MNEM_movs);
13017             }
13018
13019           if (narrow)
13020             {
13021               switch (inst.operands[1].shift_kind)
13022                 {
13023                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13024                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13025                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13026                 default: narrow = FALSE; break;
13027                 }
13028             }
13029
13030           if (narrow)
13031             {
13032               inst.instruction |= Rn;
13033               inst.instruction |= Rm << 3;
13034               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13035             }
13036           else
13037             {
13038               inst.instruction = THUMB_OP32 (inst.instruction);
13039               inst.instruction |= Rn << r0off;
13040               encode_thumb32_shifted_operand (1);
13041             }
13042         }
13043       else
13044         switch (inst.instruction)
13045           {
13046           case T_MNEM_mov:
13047             /* In v4t or v5t a move of two lowregs produces unpredictable
13048                results. Don't allow this.  */
13049             if (low_regs)
13050               {
13051                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
13052                             "MOV Rd, Rs with two low registers is not "
13053                             "permitted on this architecture");
13054                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
13055                                         arm_ext_v6);
13056               }
13057
13058             inst.instruction = T_OPCODE_MOV_HR;
13059             inst.instruction |= (Rn & 0x8) << 4;
13060             inst.instruction |= (Rn & 0x7);
13061             inst.instruction |= Rm << 3;
13062             break;
13063
13064           case T_MNEM_movs:
13065             /* We know we have low registers at this point.
13066                Generate LSLS Rd, Rs, #0.  */
13067             inst.instruction = T_OPCODE_LSL_I;
13068             inst.instruction |= Rn;
13069             inst.instruction |= Rm << 3;
13070             break;
13071
13072           case T_MNEM_cmp:
13073             if (low_regs)
13074               {
13075                 inst.instruction = T_OPCODE_CMP_LR;
13076                 inst.instruction |= Rn;
13077                 inst.instruction |= Rm << 3;
13078               }
13079             else
13080               {
13081                 inst.instruction = T_OPCODE_CMP_HR;
13082                 inst.instruction |= (Rn & 0x8) << 4;
13083                 inst.instruction |= (Rn & 0x7);
13084                 inst.instruction |= Rm << 3;
13085               }
13086             break;
13087           }
13088       return;
13089     }
13090
13091   inst.instruction = THUMB_OP16 (inst.instruction);
13092
13093   /* PR 10443: Do not silently ignore shifted operands.  */
13094   constraint (inst.operands[1].shifted,
13095               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
13096
13097   if (inst.operands[1].isreg)
13098     {
13099       if (Rn < 8 && Rm < 8)
13100         {
13101           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
13102              since a MOV instruction produces unpredictable results.  */
13103           if (inst.instruction == T_OPCODE_MOV_I8)
13104             inst.instruction = T_OPCODE_ADD_I3;
13105           else
13106             inst.instruction = T_OPCODE_CMP_LR;
13107
13108           inst.instruction |= Rn;
13109           inst.instruction |= Rm << 3;
13110         }
13111       else
13112         {
13113           if (inst.instruction == T_OPCODE_MOV_I8)
13114             inst.instruction = T_OPCODE_MOV_HR;
13115           else
13116             inst.instruction = T_OPCODE_CMP_HR;
13117           do_t_cpy ();
13118         }
13119     }
13120   else
13121     {
13122       constraint (Rn > 7,
13123                   _("only lo regs allowed with immediate"));
13124       inst.instruction |= Rn << 8;
13125       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
13126     }
13127 }
13128
13129 static void
13130 do_t_mov16 (void)
13131 {
13132   unsigned Rd;
13133   bfd_vma imm;
13134   bfd_boolean top;
13135
13136   top = (inst.instruction & 0x00800000) != 0;
13137   if (inst.relocs[0].type == BFD_RELOC_ARM_MOVW)
13138     {
13139       constraint (top, _(":lower16: not allowed in this instruction"));
13140       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVW;
13141     }
13142   else if (inst.relocs[0].type == BFD_RELOC_ARM_MOVT)
13143     {
13144       constraint (!top, _(":upper16: not allowed in this instruction"));
13145       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVT;
13146     }
13147
13148   Rd = inst.operands[0].reg;
13149   reject_bad_reg (Rd);
13150
13151   inst.instruction |= Rd << 8;
13152   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
13153     {
13154       imm = inst.relocs[0].exp.X_add_number;
13155       inst.instruction |= (imm & 0xf000) << 4;
13156       inst.instruction |= (imm & 0x0800) << 15;
13157       inst.instruction |= (imm & 0x0700) << 4;
13158       inst.instruction |= (imm & 0x00ff);
13159     }
13160 }
13161
13162 static void
13163 do_t_mvn_tst (void)
13164 {
13165   unsigned Rn, Rm;
13166
13167   Rn = inst.operands[0].reg;
13168   Rm = inst.operands[1].reg;
13169
13170   if (inst.instruction == T_MNEM_cmp
13171       || inst.instruction == T_MNEM_cmn)
13172     constraint (Rn == REG_PC, BAD_PC);
13173   else
13174     reject_bad_reg (Rn);
13175   reject_bad_reg (Rm);
13176
13177   if (unified_syntax)
13178     {
13179       int r0off = (inst.instruction == T_MNEM_mvn
13180                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
13181       bfd_boolean narrow;
13182
13183       if (inst.size_req == 4
13184           || inst.instruction > 0xffff
13185           || inst.operands[1].shifted
13186           || Rn > 7 || Rm > 7)
13187         narrow = FALSE;
13188       else if (inst.instruction == T_MNEM_cmn
13189                || inst.instruction == T_MNEM_tst)
13190         narrow = TRUE;
13191       else if (THUMB_SETS_FLAGS (inst.instruction))
13192         narrow = !in_pred_block ();
13193       else
13194         narrow = in_pred_block ();
13195
13196       if (!inst.operands[1].isreg)
13197         {
13198           /* For an immediate, we always generate a 32-bit opcode;
13199              section relaxation will shrink it later if possible.  */
13200           if (inst.instruction < 0xffff)
13201             inst.instruction = THUMB_OP32 (inst.instruction);
13202           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13203           inst.instruction |= Rn << r0off;
13204           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13205         }
13206       else
13207         {
13208           /* See if we can do this with a 16-bit instruction.  */
13209           if (narrow)
13210             {
13211               inst.instruction = THUMB_OP16 (inst.instruction);
13212               inst.instruction |= Rn;
13213               inst.instruction |= Rm << 3;
13214             }
13215           else
13216             {
13217               constraint (inst.operands[1].shifted
13218                           && inst.operands[1].immisreg,
13219                           _("shift must be constant"));
13220               if (inst.instruction < 0xffff)
13221                 inst.instruction = THUMB_OP32 (inst.instruction);
13222               inst.instruction |= Rn << r0off;
13223               encode_thumb32_shifted_operand (1);
13224             }
13225         }
13226     }
13227   else
13228     {
13229       constraint (inst.instruction > 0xffff
13230                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
13231       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
13232                   _("unshifted register required"));
13233       constraint (Rn > 7 || Rm > 7,
13234                   BAD_HIREG);
13235
13236       inst.instruction = THUMB_OP16 (inst.instruction);
13237       inst.instruction |= Rn;
13238       inst.instruction |= Rm << 3;
13239     }
13240 }
13241
13242 static void
13243 do_t_mrs (void)
13244 {
13245   unsigned Rd;
13246
13247   if (do_vfp_nsyn_mrs () == SUCCESS)
13248     return;
13249
13250   Rd = inst.operands[0].reg;
13251   reject_bad_reg (Rd);
13252   inst.instruction |= Rd << 8;
13253
13254   if (inst.operands[1].isreg)
13255     {
13256       unsigned br = inst.operands[1].reg;
13257       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
13258         as_bad (_("bad register for mrs"));
13259
13260       inst.instruction |= br & (0xf << 16);
13261       inst.instruction |= (br & 0x300) >> 4;
13262       inst.instruction |= (br & SPSR_BIT) >> 2;
13263     }
13264   else
13265     {
13266       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13267
13268       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13269         {
13270           /* PR gas/12698:  The constraint is only applied for m_profile.
13271              If the user has specified -march=all, we want to ignore it as
13272              we are building for any CPU type, including non-m variants.  */
13273           bfd_boolean m_profile =
13274             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13275           constraint ((flags != 0) && m_profile, _("selected processor does "
13276                                                    "not support requested special purpose register"));
13277         }
13278       else
13279         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
13280            devices).  */
13281         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
13282                     _("'APSR', 'CPSR' or 'SPSR' expected"));
13283
13284       inst.instruction |= (flags & SPSR_BIT) >> 2;
13285       inst.instruction |= inst.operands[1].imm & 0xff;
13286       inst.instruction |= 0xf0000;
13287     }
13288 }
13289
13290 static void
13291 do_t_msr (void)
13292 {
13293   int flags;
13294   unsigned Rn;
13295
13296   if (do_vfp_nsyn_msr () == SUCCESS)
13297     return;
13298
13299   constraint (!inst.operands[1].isreg,
13300               _("Thumb encoding does not support an immediate here"));
13301
13302   if (inst.operands[0].isreg)
13303     flags = (int)(inst.operands[0].reg);
13304   else
13305     flags = inst.operands[0].imm;
13306
13307   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13308     {
13309       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13310
13311       /* PR gas/12698:  The constraint is only applied for m_profile.
13312          If the user has specified -march=all, we want to ignore it as
13313          we are building for any CPU type, including non-m variants.  */
13314       bfd_boolean m_profile =
13315         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13316       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13317            && (bits & ~(PSR_s | PSR_f)) != 0)
13318           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13319               && bits != PSR_f)) && m_profile,
13320           _("selected processor does not support requested special "
13321             "purpose register"));
13322     }
13323   else
13324      constraint ((flags & 0xff) != 0, _("selected processor does not support "
13325                  "requested special purpose register"));
13326
13327   Rn = inst.operands[1].reg;
13328   reject_bad_reg (Rn);
13329
13330   inst.instruction |= (flags & SPSR_BIT) >> 2;
13331   inst.instruction |= (flags & 0xf0000) >> 8;
13332   inst.instruction |= (flags & 0x300) >> 4;
13333   inst.instruction |= (flags & 0xff);
13334   inst.instruction |= Rn << 16;
13335 }
13336
13337 static void
13338 do_t_mul (void)
13339 {
13340   bfd_boolean narrow;
13341   unsigned Rd, Rn, Rm;
13342
13343   if (!inst.operands[2].present)
13344     inst.operands[2].reg = inst.operands[0].reg;
13345
13346   Rd = inst.operands[0].reg;
13347   Rn = inst.operands[1].reg;
13348   Rm = inst.operands[2].reg;
13349
13350   if (unified_syntax)
13351     {
13352       if (inst.size_req == 4
13353           || (Rd != Rn
13354               && Rd != Rm)
13355           || Rn > 7
13356           || Rm > 7)
13357         narrow = FALSE;
13358       else if (inst.instruction == T_MNEM_muls)
13359         narrow = !in_pred_block ();
13360       else
13361         narrow = in_pred_block ();
13362     }
13363   else
13364     {
13365       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
13366       constraint (Rn > 7 || Rm > 7,
13367                   BAD_HIREG);
13368       narrow = TRUE;
13369     }
13370
13371   if (narrow)
13372     {
13373       /* 16-bit MULS/Conditional MUL.  */
13374       inst.instruction = THUMB_OP16 (inst.instruction);
13375       inst.instruction |= Rd;
13376
13377       if (Rd == Rn)
13378         inst.instruction |= Rm << 3;
13379       else if (Rd == Rm)
13380         inst.instruction |= Rn << 3;
13381       else
13382         constraint (1, _("dest must overlap one source register"));
13383     }
13384   else
13385     {
13386       constraint (inst.instruction != T_MNEM_mul,
13387                   _("Thumb-2 MUL must not set flags"));
13388       /* 32-bit MUL.  */
13389       inst.instruction = THUMB_OP32 (inst.instruction);
13390       inst.instruction |= Rd << 8;
13391       inst.instruction |= Rn << 16;
13392       inst.instruction |= Rm << 0;
13393
13394       reject_bad_reg (Rd);
13395       reject_bad_reg (Rn);
13396       reject_bad_reg (Rm);
13397     }
13398 }
13399
13400 static void
13401 do_t_mull (void)
13402 {
13403   unsigned RdLo, RdHi, Rn, Rm;
13404
13405   RdLo = inst.operands[0].reg;
13406   RdHi = inst.operands[1].reg;
13407   Rn = inst.operands[2].reg;
13408   Rm = inst.operands[3].reg;
13409
13410   reject_bad_reg (RdLo);
13411   reject_bad_reg (RdHi);
13412   reject_bad_reg (Rn);
13413   reject_bad_reg (Rm);
13414
13415   inst.instruction |= RdLo << 12;
13416   inst.instruction |= RdHi << 8;
13417   inst.instruction |= Rn << 16;
13418   inst.instruction |= Rm;
13419
13420  if (RdLo == RdHi)
13421     as_tsktsk (_("rdhi and rdlo must be different"));
13422 }
13423
13424 static void
13425 do_t_nop (void)
13426 {
13427   set_pred_insn_type (NEUTRAL_IT_INSN);
13428
13429   if (unified_syntax)
13430     {
13431       if (inst.size_req == 4 || inst.operands[0].imm > 15)
13432         {
13433           inst.instruction = THUMB_OP32 (inst.instruction);
13434           inst.instruction |= inst.operands[0].imm;
13435         }
13436       else
13437         {
13438           /* PR9722: Check for Thumb2 availability before
13439              generating a thumb2 nop instruction.  */
13440           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
13441             {
13442               inst.instruction = THUMB_OP16 (inst.instruction);
13443               inst.instruction |= inst.operands[0].imm << 4;
13444             }
13445           else
13446             inst.instruction = 0x46c0;
13447         }
13448     }
13449   else
13450     {
13451       constraint (inst.operands[0].present,
13452                   _("Thumb does not support NOP with hints"));
13453       inst.instruction = 0x46c0;
13454     }
13455 }
13456
13457 static void
13458 do_t_neg (void)
13459 {
13460   if (unified_syntax)
13461     {
13462       bfd_boolean narrow;
13463
13464       if (THUMB_SETS_FLAGS (inst.instruction))
13465         narrow = !in_pred_block ();
13466       else
13467         narrow = in_pred_block ();
13468       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13469         narrow = FALSE;
13470       if (inst.size_req == 4)
13471         narrow = FALSE;
13472
13473       if (!narrow)
13474         {
13475           inst.instruction = THUMB_OP32 (inst.instruction);
13476           inst.instruction |= inst.operands[0].reg << 8;
13477           inst.instruction |= inst.operands[1].reg << 16;
13478         }
13479       else
13480         {
13481           inst.instruction = THUMB_OP16 (inst.instruction);
13482           inst.instruction |= inst.operands[0].reg;
13483           inst.instruction |= inst.operands[1].reg << 3;
13484         }
13485     }
13486   else
13487     {
13488       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
13489                   BAD_HIREG);
13490       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13491
13492       inst.instruction = THUMB_OP16 (inst.instruction);
13493       inst.instruction |= inst.operands[0].reg;
13494       inst.instruction |= inst.operands[1].reg << 3;
13495     }
13496 }
13497
13498 static void
13499 do_t_orn (void)
13500 {
13501   unsigned Rd, Rn;
13502
13503   Rd = inst.operands[0].reg;
13504   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
13505
13506   reject_bad_reg (Rd);
13507   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
13508   reject_bad_reg (Rn);
13509
13510   inst.instruction |= Rd << 8;
13511   inst.instruction |= Rn << 16;
13512
13513   if (!inst.operands[2].isreg)
13514     {
13515       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13516       inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13517     }
13518   else
13519     {
13520       unsigned Rm;
13521
13522       Rm = inst.operands[2].reg;
13523       reject_bad_reg (Rm);
13524
13525       constraint (inst.operands[2].shifted
13526                   && inst.operands[2].immisreg,
13527                   _("shift must be constant"));
13528       encode_thumb32_shifted_operand (2);
13529     }
13530 }
13531
13532 static void
13533 do_t_pkhbt (void)
13534 {
13535   unsigned Rd, Rn, Rm;
13536
13537   Rd = inst.operands[0].reg;
13538   Rn = inst.operands[1].reg;
13539   Rm = inst.operands[2].reg;
13540
13541   reject_bad_reg (Rd);
13542   reject_bad_reg (Rn);
13543   reject_bad_reg (Rm);
13544
13545   inst.instruction |= Rd << 8;
13546   inst.instruction |= Rn << 16;
13547   inst.instruction |= Rm;
13548   if (inst.operands[3].present)
13549     {
13550       unsigned int val = inst.relocs[0].exp.X_add_number;
13551       constraint (inst.relocs[0].exp.X_op != O_constant,
13552                   _("expression too complex"));
13553       inst.instruction |= (val & 0x1c) << 10;
13554       inst.instruction |= (val & 0x03) << 6;
13555     }
13556 }
13557
13558 static void
13559 do_t_pkhtb (void)
13560 {
13561   if (!inst.operands[3].present)
13562     {
13563       unsigned Rtmp;
13564
13565       inst.instruction &= ~0x00000020;
13566
13567       /* PR 10168.  Swap the Rm and Rn registers.  */
13568       Rtmp = inst.operands[1].reg;
13569       inst.operands[1].reg = inst.operands[2].reg;
13570       inst.operands[2].reg = Rtmp;
13571     }
13572   do_t_pkhbt ();
13573 }
13574
13575 static void
13576 do_t_pld (void)
13577 {
13578   if (inst.operands[0].immisreg)
13579     reject_bad_reg (inst.operands[0].imm);
13580
13581   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
13582 }
13583
13584 static void
13585 do_t_push_pop (void)
13586 {
13587   unsigned mask;
13588
13589   constraint (inst.operands[0].writeback,
13590               _("push/pop do not support {reglist}^"));
13591   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
13592               _("expression too complex"));
13593
13594   mask = inst.operands[0].imm;
13595   if (inst.size_req != 4 && (mask & ~0xff) == 0)
13596     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
13597   else if (inst.size_req != 4
13598            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
13599                                        ? REG_LR : REG_PC)))
13600     {
13601       inst.instruction = THUMB_OP16 (inst.instruction);
13602       inst.instruction |= THUMB_PP_PC_LR;
13603       inst.instruction |= mask & 0xff;
13604     }
13605   else if (unified_syntax)
13606     {
13607       inst.instruction = THUMB_OP32 (inst.instruction);
13608       encode_thumb2_multi (TRUE /* do_io */, 13, mask, TRUE);
13609     }
13610   else
13611     {
13612       inst.error = _("invalid register list to push/pop instruction");
13613       return;
13614     }
13615 }
13616
13617 static void
13618 do_t_clrm (void)
13619 {
13620   if (unified_syntax)
13621     encode_thumb2_multi (FALSE /* do_io */, -1, inst.operands[0].imm, FALSE);
13622   else
13623     {
13624       inst.error = _("invalid register list to push/pop instruction");
13625       return;
13626     }
13627 }
13628
13629 static void
13630 do_t_vscclrm (void)
13631 {
13632   if (inst.operands[0].issingle)
13633     {
13634       inst.instruction |= (inst.operands[0].reg & 0x1) << 22;
13635       inst.instruction |= (inst.operands[0].reg & 0x1e) << 11;
13636       inst.instruction |= inst.operands[0].imm;
13637     }
13638   else
13639     {
13640       inst.instruction |= (inst.operands[0].reg & 0x10) << 18;
13641       inst.instruction |= (inst.operands[0].reg & 0xf) << 12;
13642       inst.instruction |= 1 << 8;
13643       inst.instruction |= inst.operands[0].imm << 1;
13644     }
13645 }
13646
13647 static void
13648 do_t_rbit (void)
13649 {
13650   unsigned Rd, Rm;
13651
13652   Rd = inst.operands[0].reg;
13653   Rm = inst.operands[1].reg;
13654
13655   reject_bad_reg (Rd);
13656   reject_bad_reg (Rm);
13657
13658   inst.instruction |= Rd << 8;
13659   inst.instruction |= Rm << 16;
13660   inst.instruction |= Rm;
13661 }
13662
13663 static void
13664 do_t_rev (void)
13665 {
13666   unsigned Rd, Rm;
13667
13668   Rd = inst.operands[0].reg;
13669   Rm = inst.operands[1].reg;
13670
13671   reject_bad_reg (Rd);
13672   reject_bad_reg (Rm);
13673
13674   if (Rd <= 7 && Rm <= 7
13675       && inst.size_req != 4)
13676     {
13677       inst.instruction = THUMB_OP16 (inst.instruction);
13678       inst.instruction |= Rd;
13679       inst.instruction |= Rm << 3;
13680     }
13681   else if (unified_syntax)
13682     {
13683       inst.instruction = THUMB_OP32 (inst.instruction);
13684       inst.instruction |= Rd << 8;
13685       inst.instruction |= Rm << 16;
13686       inst.instruction |= Rm;
13687     }
13688   else
13689     inst.error = BAD_HIREG;
13690 }
13691
13692 static void
13693 do_t_rrx (void)
13694 {
13695   unsigned Rd, Rm;
13696
13697   Rd = inst.operands[0].reg;
13698   Rm = inst.operands[1].reg;
13699
13700   reject_bad_reg (Rd);
13701   reject_bad_reg (Rm);
13702
13703   inst.instruction |= Rd << 8;
13704   inst.instruction |= Rm;
13705 }
13706
13707 static void
13708 do_t_rsb (void)
13709 {
13710   unsigned Rd, Rs;
13711
13712   Rd = inst.operands[0].reg;
13713   Rs = (inst.operands[1].present
13714         ? inst.operands[1].reg    /* Rd, Rs, foo */
13715         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
13716
13717   reject_bad_reg (Rd);
13718   reject_bad_reg (Rs);
13719   if (inst.operands[2].isreg)
13720     reject_bad_reg (inst.operands[2].reg);
13721
13722   inst.instruction |= Rd << 8;
13723   inst.instruction |= Rs << 16;
13724   if (!inst.operands[2].isreg)
13725     {
13726       bfd_boolean narrow;
13727
13728       if ((inst.instruction & 0x00100000) != 0)
13729         narrow = !in_pred_block ();
13730       else
13731         narrow = in_pred_block ();
13732
13733       if (Rd > 7 || Rs > 7)
13734         narrow = FALSE;
13735
13736       if (inst.size_req == 4 || !unified_syntax)
13737         narrow = FALSE;
13738
13739       if (inst.relocs[0].exp.X_op != O_constant
13740           || inst.relocs[0].exp.X_add_number != 0)
13741         narrow = FALSE;
13742
13743       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
13744          relaxation, but it doesn't seem worth the hassle.  */
13745       if (narrow)
13746         {
13747           inst.relocs[0].type = BFD_RELOC_UNUSED;
13748           inst.instruction = THUMB_OP16 (T_MNEM_negs);
13749           inst.instruction |= Rs << 3;
13750           inst.instruction |= Rd;
13751         }
13752       else
13753         {
13754           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13755           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13756         }
13757     }
13758   else
13759     encode_thumb32_shifted_operand (2);
13760 }
13761
13762 static void
13763 do_t_setend (void)
13764 {
13765   if (warn_on_deprecated
13766       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13767       as_tsktsk (_("setend use is deprecated for ARMv8"));
13768
13769   set_pred_insn_type (OUTSIDE_PRED_INSN);
13770   if (inst.operands[0].imm)
13771     inst.instruction |= 0x8;
13772 }
13773
13774 static void
13775 do_t_shift (void)
13776 {
13777   if (!inst.operands[1].present)
13778     inst.operands[1].reg = inst.operands[0].reg;
13779
13780   if (unified_syntax)
13781     {
13782       bfd_boolean narrow;
13783       int shift_kind;
13784
13785       switch (inst.instruction)
13786         {
13787         case T_MNEM_asr:
13788         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
13789         case T_MNEM_lsl:
13790         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
13791         case T_MNEM_lsr:
13792         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
13793         case T_MNEM_ror:
13794         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
13795         default: abort ();
13796         }
13797
13798       if (THUMB_SETS_FLAGS (inst.instruction))
13799         narrow = !in_pred_block ();
13800       else
13801         narrow = in_pred_block ();
13802       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13803         narrow = FALSE;
13804       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
13805         narrow = FALSE;
13806       if (inst.operands[2].isreg
13807           && (inst.operands[1].reg != inst.operands[0].reg
13808               || inst.operands[2].reg > 7))
13809         narrow = FALSE;
13810       if (inst.size_req == 4)
13811         narrow = FALSE;
13812
13813       reject_bad_reg (inst.operands[0].reg);
13814       reject_bad_reg (inst.operands[1].reg);
13815
13816       if (!narrow)
13817         {
13818           if (inst.operands[2].isreg)
13819             {
13820               reject_bad_reg (inst.operands[2].reg);
13821               inst.instruction = THUMB_OP32 (inst.instruction);
13822               inst.instruction |= inst.operands[0].reg << 8;
13823               inst.instruction |= inst.operands[1].reg << 16;
13824               inst.instruction |= inst.operands[2].reg;
13825
13826               /* PR 12854: Error on extraneous shifts.  */
13827               constraint (inst.operands[2].shifted,
13828                           _("extraneous shift as part of operand to shift insn"));
13829             }
13830           else
13831             {
13832               inst.operands[1].shifted = 1;
13833               inst.operands[1].shift_kind = shift_kind;
13834               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
13835                                              ? T_MNEM_movs : T_MNEM_mov);
13836               inst.instruction |= inst.operands[0].reg << 8;
13837               encode_thumb32_shifted_operand (1);
13838               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
13839               inst.relocs[0].type = BFD_RELOC_UNUSED;
13840             }
13841         }
13842       else
13843         {
13844           if (inst.operands[2].isreg)
13845             {
13846               switch (shift_kind)
13847                 {
13848                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
13849                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
13850                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
13851                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
13852                 default: abort ();
13853                 }
13854
13855               inst.instruction |= inst.operands[0].reg;
13856               inst.instruction |= inst.operands[2].reg << 3;
13857
13858               /* PR 12854: Error on extraneous shifts.  */
13859               constraint (inst.operands[2].shifted,
13860                           _("extraneous shift as part of operand to shift insn"));
13861             }
13862           else
13863             {
13864               switch (shift_kind)
13865                 {
13866                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13867                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13868                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13869                 default: abort ();
13870                 }
13871               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13872               inst.instruction |= inst.operands[0].reg;
13873               inst.instruction |= inst.operands[1].reg << 3;
13874             }
13875         }
13876     }
13877   else
13878     {
13879       constraint (inst.operands[0].reg > 7
13880                   || inst.operands[1].reg > 7, BAD_HIREG);
13881       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13882
13883       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
13884         {
13885           constraint (inst.operands[2].reg > 7, BAD_HIREG);
13886           constraint (inst.operands[0].reg != inst.operands[1].reg,
13887                       _("source1 and dest must be same register"));
13888
13889           switch (inst.instruction)
13890             {
13891             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
13892             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
13893             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
13894             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
13895             default: abort ();
13896             }
13897
13898           inst.instruction |= inst.operands[0].reg;
13899           inst.instruction |= inst.operands[2].reg << 3;
13900
13901           /* PR 12854: Error on extraneous shifts.  */
13902           constraint (inst.operands[2].shifted,
13903                       _("extraneous shift as part of operand to shift insn"));
13904         }
13905       else
13906         {
13907           switch (inst.instruction)
13908             {
13909             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
13910             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
13911             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
13912             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
13913             default: abort ();
13914             }
13915           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13916           inst.instruction |= inst.operands[0].reg;
13917           inst.instruction |= inst.operands[1].reg << 3;
13918         }
13919     }
13920 }
13921
13922 static void
13923 do_t_simd (void)
13924 {
13925   unsigned Rd, Rn, Rm;
13926
13927   Rd = inst.operands[0].reg;
13928   Rn = inst.operands[1].reg;
13929   Rm = inst.operands[2].reg;
13930
13931   reject_bad_reg (Rd);
13932   reject_bad_reg (Rn);
13933   reject_bad_reg (Rm);
13934
13935   inst.instruction |= Rd << 8;
13936   inst.instruction |= Rn << 16;
13937   inst.instruction |= Rm;
13938 }
13939
13940 static void
13941 do_t_simd2 (void)
13942 {
13943   unsigned Rd, Rn, Rm;
13944
13945   Rd = inst.operands[0].reg;
13946   Rm = inst.operands[1].reg;
13947   Rn = inst.operands[2].reg;
13948
13949   reject_bad_reg (Rd);
13950   reject_bad_reg (Rn);
13951   reject_bad_reg (Rm);
13952
13953   inst.instruction |= Rd << 8;
13954   inst.instruction |= Rn << 16;
13955   inst.instruction |= Rm;
13956 }
13957
13958 static void
13959 do_t_smc (void)
13960 {
13961   unsigned int value = inst.relocs[0].exp.X_add_number;
13962   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13963               _("SMC is not permitted on this architecture"));
13964   constraint (inst.relocs[0].exp.X_op != O_constant,
13965               _("expression too complex"));
13966   constraint (value > 0xf, _("immediate too large (bigger than 0xF)"));
13967
13968   inst.relocs[0].type = BFD_RELOC_UNUSED;
13969   inst.instruction |= (value & 0x000f) << 16;
13970
13971   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13972   set_pred_insn_type_last ();
13973 }
13974
13975 static void
13976 do_t_hvc (void)
13977 {
13978   unsigned int value = inst.relocs[0].exp.X_add_number;
13979
13980   inst.relocs[0].type = BFD_RELOC_UNUSED;
13981   inst.instruction |= (value & 0x0fff);
13982   inst.instruction |= (value & 0xf000) << 4;
13983 }
13984
13985 static void
13986 do_t_ssat_usat (int bias)
13987 {
13988   unsigned Rd, Rn;
13989
13990   Rd = inst.operands[0].reg;
13991   Rn = inst.operands[2].reg;
13992
13993   reject_bad_reg (Rd);
13994   reject_bad_reg (Rn);
13995
13996   inst.instruction |= Rd << 8;
13997   inst.instruction |= inst.operands[1].imm - bias;
13998   inst.instruction |= Rn << 16;
13999
14000   if (inst.operands[3].present)
14001     {
14002       offsetT shift_amount = inst.relocs[0].exp.X_add_number;
14003
14004       inst.relocs[0].type = BFD_RELOC_UNUSED;
14005
14006       constraint (inst.relocs[0].exp.X_op != O_constant,
14007                   _("expression too complex"));
14008
14009       if (shift_amount != 0)
14010         {
14011           constraint (shift_amount > 31,
14012                       _("shift expression is too large"));
14013
14014           if (inst.operands[3].shift_kind == SHIFT_ASR)
14015             inst.instruction |= 0x00200000;  /* sh bit.  */
14016
14017           inst.instruction |= (shift_amount & 0x1c) << 10;
14018           inst.instruction |= (shift_amount & 0x03) << 6;
14019         }
14020     }
14021 }
14022
14023 static void
14024 do_t_ssat (void)
14025 {
14026   do_t_ssat_usat (1);
14027 }
14028
14029 static void
14030 do_t_ssat16 (void)
14031 {
14032   unsigned Rd, Rn;
14033
14034   Rd = inst.operands[0].reg;
14035   Rn = inst.operands[2].reg;
14036
14037   reject_bad_reg (Rd);
14038   reject_bad_reg (Rn);
14039
14040   inst.instruction |= Rd << 8;
14041   inst.instruction |= inst.operands[1].imm - 1;
14042   inst.instruction |= Rn << 16;
14043 }
14044
14045 static void
14046 do_t_strex (void)
14047 {
14048   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
14049               || inst.operands[2].postind || inst.operands[2].writeback
14050               || inst.operands[2].immisreg || inst.operands[2].shifted
14051               || inst.operands[2].negative,
14052               BAD_ADDR_MODE);
14053
14054   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
14055
14056   inst.instruction |= inst.operands[0].reg << 8;
14057   inst.instruction |= inst.operands[1].reg << 12;
14058   inst.instruction |= inst.operands[2].reg << 16;
14059   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
14060 }
14061
14062 static void
14063 do_t_strexd (void)
14064 {
14065   if (!inst.operands[2].present)
14066     inst.operands[2].reg = inst.operands[1].reg + 1;
14067
14068   constraint (inst.operands[0].reg == inst.operands[1].reg
14069               || inst.operands[0].reg == inst.operands[2].reg
14070               || inst.operands[0].reg == inst.operands[3].reg,
14071               BAD_OVERLAP);
14072
14073   inst.instruction |= inst.operands[0].reg;
14074   inst.instruction |= inst.operands[1].reg << 12;
14075   inst.instruction |= inst.operands[2].reg << 8;
14076   inst.instruction |= inst.operands[3].reg << 16;
14077 }
14078
14079 static void
14080 do_t_sxtah (void)
14081 {
14082   unsigned Rd, Rn, Rm;
14083
14084   Rd = inst.operands[0].reg;
14085   Rn = inst.operands[1].reg;
14086   Rm = inst.operands[2].reg;
14087
14088   reject_bad_reg (Rd);
14089   reject_bad_reg (Rn);
14090   reject_bad_reg (Rm);
14091
14092   inst.instruction |= Rd << 8;
14093   inst.instruction |= Rn << 16;
14094   inst.instruction |= Rm;
14095   inst.instruction |= inst.operands[3].imm << 4;
14096 }
14097
14098 static void
14099 do_t_sxth (void)
14100 {
14101   unsigned Rd, Rm;
14102
14103   Rd = inst.operands[0].reg;
14104   Rm = inst.operands[1].reg;
14105
14106   reject_bad_reg (Rd);
14107   reject_bad_reg (Rm);
14108
14109   if (inst.instruction <= 0xffff
14110       && inst.size_req != 4
14111       && Rd <= 7 && Rm <= 7
14112       && (!inst.operands[2].present || inst.operands[2].imm == 0))
14113     {
14114       inst.instruction = THUMB_OP16 (inst.instruction);
14115       inst.instruction |= Rd;
14116       inst.instruction |= Rm << 3;
14117     }
14118   else if (unified_syntax)
14119     {
14120       if (inst.instruction <= 0xffff)
14121         inst.instruction = THUMB_OP32 (inst.instruction);
14122       inst.instruction |= Rd << 8;
14123       inst.instruction |= Rm;
14124       inst.instruction |= inst.operands[2].imm << 4;
14125     }
14126   else
14127     {
14128       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
14129                   _("Thumb encoding does not support rotation"));
14130       constraint (1, BAD_HIREG);
14131     }
14132 }
14133
14134 static void
14135 do_t_swi (void)
14136 {
14137   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
14138 }
14139
14140 static void
14141 do_t_tb (void)
14142 {
14143   unsigned Rn, Rm;
14144   int half;
14145
14146   half = (inst.instruction & 0x10) != 0;
14147   set_pred_insn_type_last ();
14148   constraint (inst.operands[0].immisreg,
14149               _("instruction requires register index"));
14150
14151   Rn = inst.operands[0].reg;
14152   Rm = inst.operands[0].imm;
14153
14154   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
14155     constraint (Rn == REG_SP, BAD_SP);
14156   reject_bad_reg (Rm);
14157
14158   constraint (!half && inst.operands[0].shifted,
14159               _("instruction does not allow shifted index"));
14160   inst.instruction |= (Rn << 16) | Rm;
14161 }
14162
14163 static void
14164 do_t_udf (void)
14165 {
14166   if (!inst.operands[0].present)
14167     inst.operands[0].imm = 0;
14168
14169   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
14170     {
14171       constraint (inst.size_req == 2,
14172                   _("immediate value out of range"));
14173       inst.instruction = THUMB_OP32 (inst.instruction);
14174       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
14175       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
14176     }
14177   else
14178     {
14179       inst.instruction = THUMB_OP16 (inst.instruction);
14180       inst.instruction |= inst.operands[0].imm;
14181     }
14182
14183   set_pred_insn_type (NEUTRAL_IT_INSN);
14184 }
14185
14186
14187 static void
14188 do_t_usat (void)
14189 {
14190   do_t_ssat_usat (0);
14191 }
14192
14193 static void
14194 do_t_usat16 (void)
14195 {
14196   unsigned Rd, Rn;
14197
14198   Rd = inst.operands[0].reg;
14199   Rn = inst.operands[2].reg;
14200
14201   reject_bad_reg (Rd);
14202   reject_bad_reg (Rn);
14203
14204   inst.instruction |= Rd << 8;
14205   inst.instruction |= inst.operands[1].imm;
14206   inst.instruction |= Rn << 16;
14207 }
14208
14209 /* Checking the range of the branch offset (VAL) with NBITS bits
14210    and IS_SIGNED signedness.  Also checks the LSB to be 0.  */
14211 static int
14212 v8_1_branch_value_check (int val, int nbits, int is_signed)
14213 {
14214   gas_assert (nbits > 0 && nbits <= 32);
14215   if (is_signed)
14216     {
14217       int cmp = (1 << (nbits - 1));
14218       if ((val < -cmp) || (val >= cmp) || (val & 0x01))
14219         return FAIL;
14220     }
14221   else
14222     {
14223       if ((val <= 0) || (val >= (1 << nbits)) || (val & 0x1))
14224         return FAIL;
14225     }
14226     return SUCCESS;
14227 }
14228
14229 /* For branches in Armv8.1-M Mainline.  */
14230 static void
14231 do_t_branch_future (void)
14232 {
14233   unsigned long insn = inst.instruction;
14234
14235   inst.instruction = THUMB_OP32 (inst.instruction);
14236   if (inst.operands[0].hasreloc == 0)
14237     {
14238       if (v8_1_branch_value_check (inst.operands[0].imm, 5, FALSE) == FAIL)
14239         as_bad (BAD_BRANCH_OFF);
14240
14241       inst.instruction |= ((inst.operands[0].imm & 0x1f) >> 1) << 23;
14242     }
14243   else
14244     {
14245       inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH5;
14246       inst.relocs[0].pc_rel = 1;
14247     }
14248
14249   switch (insn)
14250     {
14251       case T_MNEM_bf:
14252         if (inst.operands[1].hasreloc == 0)
14253           {
14254             int val = inst.operands[1].imm;
14255             if (v8_1_branch_value_check (inst.operands[1].imm, 17, TRUE) == FAIL)
14256               as_bad (BAD_BRANCH_OFF);
14257
14258             int immA = (val & 0x0001f000) >> 12;
14259             int immB = (val & 0x00000ffc) >> 2;
14260             int immC = (val & 0x00000002) >> 1;
14261             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14262           }
14263         else
14264           {
14265             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF17;
14266             inst.relocs[1].pc_rel = 1;
14267           }
14268         break;
14269
14270       case T_MNEM_bfl:
14271         if (inst.operands[1].hasreloc == 0)
14272           {
14273             int val = inst.operands[1].imm;
14274             if (v8_1_branch_value_check (inst.operands[1].imm, 19, TRUE) == FAIL)
14275               as_bad (BAD_BRANCH_OFF);
14276
14277             int immA = (val & 0x0007f000) >> 12;
14278             int immB = (val & 0x00000ffc) >> 2;
14279             int immC = (val & 0x00000002) >> 1;
14280             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14281           }
14282           else
14283           {
14284             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF19;
14285             inst.relocs[1].pc_rel = 1;
14286           }
14287         break;
14288
14289       case T_MNEM_bfcsel:
14290         /* Operand 1.  */
14291         if (inst.operands[1].hasreloc == 0)
14292           {
14293             int val = inst.operands[1].imm;
14294             int immA = (val & 0x00001000) >> 12;
14295             int immB = (val & 0x00000ffc) >> 2;
14296             int immC = (val & 0x00000002) >> 1;
14297             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14298           }
14299           else
14300           {
14301             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF13;
14302             inst.relocs[1].pc_rel = 1;
14303           }
14304
14305         /* Operand 2.  */
14306         if (inst.operands[2].hasreloc == 0)
14307           {
14308               constraint ((inst.operands[0].hasreloc != 0), BAD_ARGS);
14309               int val2 = inst.operands[2].imm;
14310               int val0 = inst.operands[0].imm & 0x1f;
14311               int diff = val2 - val0;
14312               if (diff == 4)
14313                 inst.instruction |= 1 << 17; /* T bit.  */
14314               else if (diff != 2)
14315                 as_bad (_("out of range label-relative fixup value"));
14316           }
14317         else
14318           {
14319               constraint ((inst.operands[0].hasreloc == 0), BAD_ARGS);
14320               inst.relocs[2].type = BFD_RELOC_THUMB_PCREL_BFCSEL;
14321               inst.relocs[2].pc_rel = 1;
14322           }
14323
14324         /* Operand 3.  */
14325         constraint (inst.cond != COND_ALWAYS, BAD_COND);
14326         inst.instruction |= (inst.operands[3].imm & 0xf) << 18;
14327         break;
14328
14329       case T_MNEM_bfx:
14330       case T_MNEM_bflx:
14331         inst.instruction |= inst.operands[1].reg << 16;
14332         break;
14333
14334       default: abort ();
14335     }
14336 }
14337
14338 /* Helper function for do_t_loloop to handle relocations.  */
14339 static void
14340 v8_1_loop_reloc (int is_le)
14341 {
14342   if (inst.relocs[0].exp.X_op == O_constant)
14343     {
14344       int value = inst.relocs[0].exp.X_add_number;
14345       value = (is_le) ? -value : value;
14346
14347       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
14348         as_bad (BAD_BRANCH_OFF);
14349
14350       int imml, immh;
14351
14352       immh = (value & 0x00000ffc) >> 2;
14353       imml = (value & 0x00000002) >> 1;
14354
14355       inst.instruction |= (imml << 11) | (immh << 1);
14356     }
14357   else
14358     {
14359       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_LOOP12;
14360       inst.relocs[0].pc_rel = 1;
14361     }
14362 }
14363
14364 /* For shifts with four operands in MVE.  */
14365 static void
14366 do_mve_scalar_shift1 (void)
14367 {
14368   unsigned int value = inst.operands[2].imm;
14369
14370   inst.instruction |= inst.operands[0].reg << 16;
14371   inst.instruction |= inst.operands[1].reg << 8;
14372
14373   /* Setting the bit for saturation.  */
14374   inst.instruction |= ((value == 64) ? 0: 1) << 7;
14375
14376   /* Assuming Rm is already checked not to be 11x1.  */
14377   constraint (inst.operands[3].reg == inst.operands[0].reg, BAD_OVERLAP);
14378   constraint (inst.operands[3].reg == inst.operands[1].reg, BAD_OVERLAP);
14379   inst.instruction |= inst.operands[3].reg << 12;
14380 }
14381
14382 /* For shifts in MVE.  */
14383 static void
14384 do_mve_scalar_shift (void)
14385 {
14386   if (!inst.operands[2].present)
14387     {
14388       inst.operands[2] = inst.operands[1];
14389       inst.operands[1].reg = 0xf;
14390     }
14391
14392   inst.instruction |= inst.operands[0].reg << 16;
14393   inst.instruction |= inst.operands[1].reg << 8;
14394
14395   if (inst.operands[2].isreg)
14396     {
14397       /* Assuming Rm is already checked not to be 11x1.  */
14398       constraint (inst.operands[2].reg == inst.operands[0].reg, BAD_OVERLAP);
14399       constraint (inst.operands[2].reg == inst.operands[1].reg, BAD_OVERLAP);
14400       inst.instruction |= inst.operands[2].reg << 12;
14401     }
14402   else
14403     {
14404       /* Assuming imm is already checked as [1,32].  */
14405       unsigned int value = inst.operands[2].imm;
14406       inst.instruction |= (value & 0x1c) << 10;
14407       inst.instruction |= (value & 0x03) << 6;
14408       /* Change last 4 bits from 0xd to 0xf.  */
14409       inst.instruction |= 0x2;
14410     }
14411 }
14412
14413 /* MVE instruction encoder helpers.  */
14414 #define M_MNEM_vabav    0xee800f01
14415 #define M_MNEM_vmladav    0xeef00e00
14416 #define M_MNEM_vmladava   0xeef00e20
14417 #define M_MNEM_vmladavx   0xeef01e00
14418 #define M_MNEM_vmladavax  0xeef01e20
14419 #define M_MNEM_vmlsdav    0xeef00e01
14420 #define M_MNEM_vmlsdava   0xeef00e21
14421 #define M_MNEM_vmlsdavx   0xeef01e01
14422 #define M_MNEM_vmlsdavax  0xeef01e21
14423 #define M_MNEM_vmullt   0xee011e00
14424 #define M_MNEM_vmullb   0xee010e00
14425 #define M_MNEM_vst20    0xfc801e00
14426 #define M_MNEM_vst21    0xfc801e20
14427 #define M_MNEM_vst40    0xfc801e01
14428 #define M_MNEM_vst41    0xfc801e21
14429 #define M_MNEM_vst42    0xfc801e41
14430 #define M_MNEM_vst43    0xfc801e61
14431 #define M_MNEM_vld20    0xfc901e00
14432 #define M_MNEM_vld21    0xfc901e20
14433 #define M_MNEM_vld40    0xfc901e01
14434 #define M_MNEM_vld41    0xfc901e21
14435 #define M_MNEM_vld42    0xfc901e41
14436 #define M_MNEM_vld43    0xfc901e61
14437 #define M_MNEM_vstrb    0xec000e00
14438 #define M_MNEM_vstrh    0xec000e10
14439 #define M_MNEM_vstrw    0xec000e40
14440 #define M_MNEM_vstrd    0xec000e50
14441 #define M_MNEM_vldrb    0xec100e00
14442 #define M_MNEM_vldrh    0xec100e10
14443 #define M_MNEM_vldrw    0xec100e40
14444 #define M_MNEM_vldrd    0xec100e50
14445 #define M_MNEM_vmovlt   0xeea01f40
14446 #define M_MNEM_vmovlb   0xeea00f40
14447 #define M_MNEM_vmovnt   0xfe311e81
14448 #define M_MNEM_vmovnb   0xfe310e81
14449 #define M_MNEM_vadc     0xee300f00
14450 #define M_MNEM_vadci    0xee301f00
14451 #define M_MNEM_vbrsr    0xfe011e60
14452 #define M_MNEM_vaddlv   0xee890f00
14453 #define M_MNEM_vaddlva  0xee890f20
14454 #define M_MNEM_vaddv    0xeef10f00
14455 #define M_MNEM_vaddva   0xeef10f20
14456 #define M_MNEM_vddup    0xee011f6e
14457 #define M_MNEM_vdwdup   0xee011f60
14458 #define M_MNEM_vidup    0xee010f6e
14459 #define M_MNEM_viwdup   0xee010f60
14460 #define M_MNEM_vmaxv    0xeee20f00
14461 #define M_MNEM_vmaxav   0xeee00f00
14462 #define M_MNEM_vminv    0xeee20f80
14463 #define M_MNEM_vminav   0xeee00f80
14464 #define M_MNEM_vmlaldav   0xee800e00
14465 #define M_MNEM_vmlaldava  0xee800e20
14466 #define M_MNEM_vmlaldavx  0xee801e00
14467 #define M_MNEM_vmlaldavax 0xee801e20
14468 #define M_MNEM_vmlsldav   0xee800e01
14469 #define M_MNEM_vmlsldava  0xee800e21
14470 #define M_MNEM_vmlsldavx  0xee801e01
14471 #define M_MNEM_vmlsldavax 0xee801e21
14472 #define M_MNEM_vrmlaldavhx  0xee801f00
14473 #define M_MNEM_vrmlaldavhax 0xee801f20
14474 #define M_MNEM_vrmlsldavh   0xfe800e01
14475 #define M_MNEM_vrmlsldavha  0xfe800e21
14476 #define M_MNEM_vrmlsldavhx  0xfe801e01
14477 #define M_MNEM_vrmlsldavhax 0xfe801e21
14478 #define M_MNEM_vqmovnt    0xee331e01
14479 #define M_MNEM_vqmovnb    0xee330e01
14480 #define M_MNEM_vqmovunt   0xee311e81
14481 #define M_MNEM_vqmovunb   0xee310e81
14482 #define M_MNEM_vshrnt       0xee801fc1
14483 #define M_MNEM_vshrnb       0xee800fc1
14484 #define M_MNEM_vrshrnt      0xfe801fc1
14485 #define M_MNEM_vqshrnt      0xee801f40
14486 #define M_MNEM_vqshrnb      0xee800f40
14487 #define M_MNEM_vqshrunt     0xee801fc0
14488 #define M_MNEM_vqshrunb     0xee800fc0
14489 #define M_MNEM_vrshrnb      0xfe800fc1
14490 #define M_MNEM_vqrshrnt     0xee801f41
14491 #define M_MNEM_vqrshrnb     0xee800f41
14492 #define M_MNEM_vqrshrunt    0xfe801fc0
14493 #define M_MNEM_vqrshrunb    0xfe800fc0
14494
14495 /* Neon instruction encoder helpers.  */
14496
14497 /* Encodings for the different types for various Neon opcodes.  */
14498
14499 /* An "invalid" code for the following tables.  */
14500 #define N_INV -1u
14501
14502 struct neon_tab_entry
14503 {
14504   unsigned integer;
14505   unsigned float_or_poly;
14506   unsigned scalar_or_imm;
14507 };
14508
14509 /* Map overloaded Neon opcodes to their respective encodings.  */
14510 #define NEON_ENC_TAB                                    \
14511   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
14512   X(vabdl,      0x0800700, N_INV,     N_INV),           \
14513   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
14514   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
14515   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
14516   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
14517   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
14518   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
14519   X(vaddl,      0x0800000, N_INV,     N_INV),           \
14520   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
14521   X(vsubl,      0x0800200, N_INV,     N_INV),           \
14522   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
14523   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
14524   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
14525   /* Register variants of the following two instructions are encoded as
14526      vcge / vcgt with the operands reversed.  */        \
14527   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
14528   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
14529   X(vfma,       N_INV, 0x0000c10, N_INV),               \
14530   X(vfms,       N_INV, 0x0200c10, N_INV),               \
14531   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
14532   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
14533   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
14534   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
14535   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
14536   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
14537   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
14538   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
14539   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
14540   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
14541   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
14542   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
14543   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
14544   X(vshl,       0x0000400, N_INV,     0x0800510),       \
14545   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
14546   X(vand,       0x0000110, N_INV,     0x0800030),       \
14547   X(vbic,       0x0100110, N_INV,     0x0800030),       \
14548   X(veor,       0x1000110, N_INV,     N_INV),           \
14549   X(vorn,       0x0300110, N_INV,     0x0800010),       \
14550   X(vorr,       0x0200110, N_INV,     0x0800010),       \
14551   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
14552   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
14553   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
14554   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
14555   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
14556   X(vst1,       0x0000000, 0x0800000, N_INV),           \
14557   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
14558   X(vst2,       0x0000100, 0x0800100, N_INV),           \
14559   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
14560   X(vst3,       0x0000200, 0x0800200, N_INV),           \
14561   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
14562   X(vst4,       0x0000300, 0x0800300, N_INV),           \
14563   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
14564   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
14565   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
14566   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
14567   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
14568   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
14569   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
14570   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
14571   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
14572   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
14573   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
14574   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
14575   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
14576   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
14577   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
14578   X(vselge,     0xe200a00, N_INV,     N_INV),           \
14579   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
14580   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
14581   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
14582   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
14583   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
14584   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
14585   X(aes,        0x3b00300, N_INV,     N_INV),           \
14586   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
14587   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
14588   X(sha2op,     0x3ba0380, N_INV,     N_INV)
14589
14590 enum neon_opc
14591 {
14592 #define X(OPC,I,F,S) N_MNEM_##OPC
14593 NEON_ENC_TAB
14594 #undef X
14595 };
14596
14597 static const struct neon_tab_entry neon_enc_tab[] =
14598 {
14599 #define X(OPC,I,F,S) { (I), (F), (S) }
14600 NEON_ENC_TAB
14601 #undef X
14602 };
14603
14604 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
14605 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14606 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
14607 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14608 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14609 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14610 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14611 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14612 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14613 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14614 #define NEON_ENC_SINGLE_(X) \
14615   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
14616 #define NEON_ENC_DOUBLE_(X) \
14617   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
14618 #define NEON_ENC_FPV8_(X) \
14619   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
14620
14621 #define NEON_ENCODE(type, inst)                                 \
14622   do                                                            \
14623     {                                                           \
14624       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
14625       inst.is_neon = 1;                                         \
14626     }                                                           \
14627   while (0)
14628
14629 #define check_neon_suffixes                                             \
14630   do                                                                    \
14631     {                                                                   \
14632       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
14633         {                                                               \
14634           as_bad (_("invalid neon suffix for non neon instruction"));   \
14635           return;                                                       \
14636         }                                                               \
14637     }                                                                   \
14638   while (0)
14639
14640 /* Define shapes for instruction operands. The following mnemonic characters
14641    are used in this table:
14642
14643      F - VFP S<n> register
14644      D - Neon D<n> register
14645      Q - Neon Q<n> register
14646      I - Immediate
14647      S - Scalar
14648      R - ARM register
14649      L - D<n> register list
14650
14651    This table is used to generate various data:
14652      - enumerations of the form NS_DDR to be used as arguments to
14653        neon_select_shape.
14654      - a table classifying shapes into single, double, quad, mixed.
14655      - a table used to drive neon_select_shape.  */
14656
14657 #define NEON_SHAPE_DEF                  \
14658   X(4, (R, R, Q, Q), QUAD),             \
14659   X(4, (Q, R, R, I), QUAD),             \
14660   X(4, (R, R, S, S), QUAD),             \
14661   X(4, (S, S, R, R), QUAD),             \
14662   X(3, (Q, R, I), QUAD),                \
14663   X(3, (I, Q, Q), QUAD),                \
14664   X(3, (I, Q, R), QUAD),                \
14665   X(3, (R, Q, Q), QUAD),                \
14666   X(3, (D, D, D), DOUBLE),              \
14667   X(3, (Q, Q, Q), QUAD),                \
14668   X(3, (D, D, I), DOUBLE),              \
14669   X(3, (Q, Q, I), QUAD),                \
14670   X(3, (D, D, S), DOUBLE),              \
14671   X(3, (Q, Q, S), QUAD),                \
14672   X(3, (Q, Q, R), QUAD),                \
14673   X(3, (R, R, Q), QUAD),                \
14674   X(2, (R, Q),    QUAD),                \
14675   X(2, (D, D), DOUBLE),                 \
14676   X(2, (Q, Q), QUAD),                   \
14677   X(2, (D, S), DOUBLE),                 \
14678   X(2, (Q, S), QUAD),                   \
14679   X(2, (D, R), DOUBLE),                 \
14680   X(2, (Q, R), QUAD),                   \
14681   X(2, (D, I), DOUBLE),                 \
14682   X(2, (Q, I), QUAD),                   \
14683   X(3, (D, L, D), DOUBLE),              \
14684   X(2, (D, Q), MIXED),                  \
14685   X(2, (Q, D), MIXED),                  \
14686   X(3, (D, Q, I), MIXED),               \
14687   X(3, (Q, D, I), MIXED),               \
14688   X(3, (Q, D, D), MIXED),               \
14689   X(3, (D, Q, Q), MIXED),               \
14690   X(3, (Q, Q, D), MIXED),               \
14691   X(3, (Q, D, S), MIXED),               \
14692   X(3, (D, Q, S), MIXED),               \
14693   X(4, (D, D, D, I), DOUBLE),           \
14694   X(4, (Q, Q, Q, I), QUAD),             \
14695   X(4, (D, D, S, I), DOUBLE),           \
14696   X(4, (Q, Q, S, I), QUAD),             \
14697   X(2, (F, F), SINGLE),                 \
14698   X(3, (F, F, F), SINGLE),              \
14699   X(2, (F, I), SINGLE),                 \
14700   X(2, (F, D), MIXED),                  \
14701   X(2, (D, F), MIXED),                  \
14702   X(3, (F, F, I), MIXED),               \
14703   X(4, (R, R, F, F), SINGLE),           \
14704   X(4, (F, F, R, R), SINGLE),           \
14705   X(3, (D, R, R), DOUBLE),              \
14706   X(3, (R, R, D), DOUBLE),              \
14707   X(2, (S, R), SINGLE),                 \
14708   X(2, (R, S), SINGLE),                 \
14709   X(2, (F, R), SINGLE),                 \
14710   X(2, (R, F), SINGLE),                 \
14711 /* Used for MVE tail predicated loop instructions.  */\
14712   X(2, (R, R), QUAD),                   \
14713 /* Half float shape supported so far.  */\
14714   X (2, (H, D), MIXED),                 \
14715   X (2, (D, H), MIXED),                 \
14716   X (2, (H, F), MIXED),                 \
14717   X (2, (F, H), MIXED),                 \
14718   X (2, (H, H), HALF),                  \
14719   X (2, (H, R), HALF),                  \
14720   X (2, (R, H), HALF),                  \
14721   X (2, (H, I), HALF),                  \
14722   X (3, (H, H, H), HALF),               \
14723   X (3, (H, F, I), MIXED),              \
14724   X (3, (F, H, I), MIXED),              \
14725   X (3, (D, H, H), MIXED),              \
14726   X (3, (D, H, S), MIXED)
14727
14728 #define S2(A,B)         NS_##A##B
14729 #define S3(A,B,C)       NS_##A##B##C
14730 #define S4(A,B,C,D)     NS_##A##B##C##D
14731
14732 #define X(N, L, C) S##N L
14733
14734 enum neon_shape
14735 {
14736   NEON_SHAPE_DEF,
14737   NS_NULL
14738 };
14739
14740 #undef X
14741 #undef S2
14742 #undef S3
14743 #undef S4
14744
14745 enum neon_shape_class
14746 {
14747   SC_HALF,
14748   SC_SINGLE,
14749   SC_DOUBLE,
14750   SC_QUAD,
14751   SC_MIXED
14752 };
14753
14754 #define X(N, L, C) SC_##C
14755
14756 static enum neon_shape_class neon_shape_class[] =
14757 {
14758   NEON_SHAPE_DEF
14759 };
14760
14761 #undef X
14762
14763 enum neon_shape_el
14764 {
14765   SE_H,
14766   SE_F,
14767   SE_D,
14768   SE_Q,
14769   SE_I,
14770   SE_S,
14771   SE_R,
14772   SE_L
14773 };
14774
14775 /* Register widths of above.  */
14776 static unsigned neon_shape_el_size[] =
14777 {
14778   16,
14779   32,
14780   64,
14781   128,
14782   0,
14783   32,
14784   32,
14785   0
14786 };
14787
14788 struct neon_shape_info
14789 {
14790   unsigned els;
14791   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
14792 };
14793
14794 #define S2(A,B)         { SE_##A, SE_##B }
14795 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
14796 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
14797
14798 #define X(N, L, C) { N, S##N L }
14799
14800 static struct neon_shape_info neon_shape_tab[] =
14801 {
14802   NEON_SHAPE_DEF
14803 };
14804
14805 #undef X
14806 #undef S2
14807 #undef S3
14808 #undef S4
14809
14810 /* Bit masks used in type checking given instructions.
14811   'N_EQK' means the type must be the same as (or based on in some way) the key
14812    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
14813    set, various other bits can be set as well in order to modify the meaning of
14814    the type constraint.  */
14815
14816 enum neon_type_mask
14817 {
14818   N_S8   = 0x0000001,
14819   N_S16  = 0x0000002,
14820   N_S32  = 0x0000004,
14821   N_S64  = 0x0000008,
14822   N_U8   = 0x0000010,
14823   N_U16  = 0x0000020,
14824   N_U32  = 0x0000040,
14825   N_U64  = 0x0000080,
14826   N_I8   = 0x0000100,
14827   N_I16  = 0x0000200,
14828   N_I32  = 0x0000400,
14829   N_I64  = 0x0000800,
14830   N_8    = 0x0001000,
14831   N_16   = 0x0002000,
14832   N_32   = 0x0004000,
14833   N_64   = 0x0008000,
14834   N_P8   = 0x0010000,
14835   N_P16  = 0x0020000,
14836   N_F16  = 0x0040000,
14837   N_F32  = 0x0080000,
14838   N_F64  = 0x0100000,
14839   N_P64  = 0x0200000,
14840   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
14841   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
14842   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
14843   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
14844   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
14845   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
14846   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
14847   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
14848   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
14849   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
14850   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
14851   N_UTYP = 0,
14852   N_MAX_NONSPECIAL = N_P64
14853 };
14854
14855 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
14856
14857 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
14858 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14859 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
14860 #define N_S_32     (N_S8 | N_S16 | N_S32)
14861 #define N_F_16_32  (N_F16 | N_F32)
14862 #define N_SUF_32   (N_SU_32 | N_F_16_32)
14863 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
14864 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
14865 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
14866 #define N_I_MVE    (N_I8 | N_I16 | N_I32)
14867 #define N_F_MVE    (N_F16 | N_F32)
14868 #define N_SU_MVE   (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14869
14870 /* Pass this as the first type argument to neon_check_type to ignore types
14871    altogether.  */
14872 #define N_IGNORE_TYPE (N_KEY | N_EQK)
14873
14874 /* Select a "shape" for the current instruction (describing register types or
14875    sizes) from a list of alternatives. Return NS_NULL if the current instruction
14876    doesn't fit. For non-polymorphic shapes, checking is usually done as a
14877    function of operand parsing, so this function doesn't need to be called.
14878    Shapes should be listed in order of decreasing length.  */
14879
14880 static enum neon_shape
14881 neon_select_shape (enum neon_shape shape, ...)
14882 {
14883   va_list ap;
14884   enum neon_shape first_shape = shape;
14885
14886   /* Fix missing optional operands. FIXME: we don't know at this point how
14887      many arguments we should have, so this makes the assumption that we have
14888      > 1. This is true of all current Neon opcodes, I think, but may not be
14889      true in the future.  */
14890   if (!inst.operands[1].present)
14891     inst.operands[1] = inst.operands[0];
14892
14893   va_start (ap, shape);
14894
14895   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
14896     {
14897       unsigned j;
14898       int matches = 1;
14899
14900       for (j = 0; j < neon_shape_tab[shape].els; j++)
14901         {
14902           if (!inst.operands[j].present)
14903             {
14904               matches = 0;
14905               break;
14906             }
14907
14908           switch (neon_shape_tab[shape].el[j])
14909             {
14910               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
14911                  a VFP single precision register operand, it's essentially
14912                  means only half of the register is used.
14913
14914                  If the type specifier is given after the mnemonics, the
14915                  information is stored in inst.vectype.  If the type specifier
14916                  is given after register operand, the information is stored
14917                  in inst.operands[].vectype.
14918
14919                  When there is only one type specifier, and all the register
14920                  operands are the same type of hardware register, the type
14921                  specifier applies to all register operands.
14922
14923                  If no type specifier is given, the shape is inferred from
14924                  operand information.
14925
14926                  for example:
14927                  vadd.f16 s0, s1, s2:           NS_HHH
14928                  vabs.f16 s0, s1:               NS_HH
14929                  vmov.f16 s0, r1:               NS_HR
14930                  vmov.f16 r0, s1:               NS_RH
14931                  vcvt.f16 r0, s1:               NS_RH
14932                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
14933                  vcvt.f16.s32   s2, s2:         NS_HF
14934               */
14935             case SE_H:
14936               if (!(inst.operands[j].isreg
14937                     && inst.operands[j].isvec
14938                     && inst.operands[j].issingle
14939                     && !inst.operands[j].isquad
14940                     && ((inst.vectype.elems == 1
14941                          && inst.vectype.el[0].size == 16)
14942                         || (inst.vectype.elems > 1
14943                             && inst.vectype.el[j].size == 16)
14944                         || (inst.vectype.elems == 0
14945                             && inst.operands[j].vectype.type != NT_invtype
14946                             && inst.operands[j].vectype.size == 16))))
14947                 matches = 0;
14948               break;
14949
14950             case SE_F:
14951               if (!(inst.operands[j].isreg
14952                     && inst.operands[j].isvec
14953                     && inst.operands[j].issingle
14954                     && !inst.operands[j].isquad
14955                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
14956                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
14957                         || (inst.vectype.elems == 0
14958                             && (inst.operands[j].vectype.size == 32
14959                                 || inst.operands[j].vectype.type == NT_invtype)))))
14960                 matches = 0;
14961               break;
14962
14963             case SE_D:
14964               if (!(inst.operands[j].isreg
14965                     && inst.operands[j].isvec
14966                     && !inst.operands[j].isquad
14967                     && !inst.operands[j].issingle))
14968                 matches = 0;
14969               break;
14970
14971             case SE_R:
14972               if (!(inst.operands[j].isreg
14973                     && !inst.operands[j].isvec))
14974                 matches = 0;
14975               break;
14976
14977             case SE_Q:
14978               if (!(inst.operands[j].isreg
14979                     && inst.operands[j].isvec
14980                     && inst.operands[j].isquad
14981                     && !inst.operands[j].issingle))
14982                 matches = 0;
14983               break;
14984
14985             case SE_I:
14986               if (!(!inst.operands[j].isreg
14987                     && !inst.operands[j].isscalar))
14988                 matches = 0;
14989               break;
14990
14991             case SE_S:
14992               if (!(!inst.operands[j].isreg
14993                     && inst.operands[j].isscalar))
14994                 matches = 0;
14995               break;
14996
14997             case SE_L:
14998               break;
14999             }
15000           if (!matches)
15001             break;
15002         }
15003       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
15004         /* We've matched all the entries in the shape table, and we don't
15005            have any left over operands which have not been matched.  */
15006         break;
15007     }
15008
15009   va_end (ap);
15010
15011   if (shape == NS_NULL && first_shape != NS_NULL)
15012     first_error (_("invalid instruction shape"));
15013
15014   return shape;
15015 }
15016
15017 /* True if SHAPE is predominantly a quadword operation (most of the time, this
15018    means the Q bit should be set).  */
15019
15020 static int
15021 neon_quad (enum neon_shape shape)
15022 {
15023   return neon_shape_class[shape] == SC_QUAD;
15024 }
15025
15026 static void
15027 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
15028                        unsigned *g_size)
15029 {
15030   /* Allow modification to be made to types which are constrained to be
15031      based on the key element, based on bits set alongside N_EQK.  */
15032   if ((typebits & N_EQK) != 0)
15033     {
15034       if ((typebits & N_HLF) != 0)
15035         *g_size /= 2;
15036       else if ((typebits & N_DBL) != 0)
15037         *g_size *= 2;
15038       if ((typebits & N_SGN) != 0)
15039         *g_type = NT_signed;
15040       else if ((typebits & N_UNS) != 0)
15041         *g_type = NT_unsigned;
15042       else if ((typebits & N_INT) != 0)
15043         *g_type = NT_integer;
15044       else if ((typebits & N_FLT) != 0)
15045         *g_type = NT_float;
15046       else if ((typebits & N_SIZ) != 0)
15047         *g_type = NT_untyped;
15048     }
15049 }
15050
15051 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
15052    operand type, i.e. the single type specified in a Neon instruction when it
15053    is the only one given.  */
15054
15055 static struct neon_type_el
15056 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
15057 {
15058   struct neon_type_el dest = *key;
15059
15060   gas_assert ((thisarg & N_EQK) != 0);
15061
15062   neon_modify_type_size (thisarg, &dest.type, &dest.size);
15063
15064   return dest;
15065 }
15066
15067 /* Convert Neon type and size into compact bitmask representation.  */
15068
15069 static enum neon_type_mask
15070 type_chk_of_el_type (enum neon_el_type type, unsigned size)
15071 {
15072   switch (type)
15073     {
15074     case NT_untyped:
15075       switch (size)
15076         {
15077         case 8:  return N_8;
15078         case 16: return N_16;
15079         case 32: return N_32;
15080         case 64: return N_64;
15081         default: ;
15082         }
15083       break;
15084
15085     case NT_integer:
15086       switch (size)
15087         {
15088         case 8:  return N_I8;
15089         case 16: return N_I16;
15090         case 32: return N_I32;
15091         case 64: return N_I64;
15092         default: ;
15093         }
15094       break;
15095
15096     case NT_float:
15097       switch (size)
15098         {
15099         case 16: return N_F16;
15100         case 32: return N_F32;
15101         case 64: return N_F64;
15102         default: ;
15103         }
15104       break;
15105
15106     case NT_poly:
15107       switch (size)
15108         {
15109         case 8:  return N_P8;
15110         case 16: return N_P16;
15111         case 64: return N_P64;
15112         default: ;
15113         }
15114       break;
15115
15116     case NT_signed:
15117       switch (size)
15118         {
15119         case 8:  return N_S8;
15120         case 16: return N_S16;
15121         case 32: return N_S32;
15122         case 64: return N_S64;
15123         default: ;
15124         }
15125       break;
15126
15127     case NT_unsigned:
15128       switch (size)
15129         {
15130         case 8:  return N_U8;
15131         case 16: return N_U16;
15132         case 32: return N_U32;
15133         case 64: return N_U64;
15134         default: ;
15135         }
15136       break;
15137
15138     default: ;
15139     }
15140
15141   return N_UTYP;
15142 }
15143
15144 /* Convert compact Neon bitmask type representation to a type and size. Only
15145    handles the case where a single bit is set in the mask.  */
15146
15147 static int
15148 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
15149                      enum neon_type_mask mask)
15150 {
15151   if ((mask & N_EQK) != 0)
15152     return FAIL;
15153
15154   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
15155     *size = 8;
15156   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
15157     *size = 16;
15158   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
15159     *size = 32;
15160   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
15161     *size = 64;
15162   else
15163     return FAIL;
15164
15165   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
15166     *type = NT_signed;
15167   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
15168     *type = NT_unsigned;
15169   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
15170     *type = NT_integer;
15171   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
15172     *type = NT_untyped;
15173   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
15174     *type = NT_poly;
15175   else if ((mask & (N_F_ALL)) != 0)
15176     *type = NT_float;
15177   else
15178     return FAIL;
15179
15180   return SUCCESS;
15181 }
15182
15183 /* Modify a bitmask of allowed types. This is only needed for type
15184    relaxation.  */
15185
15186 static unsigned
15187 modify_types_allowed (unsigned allowed, unsigned mods)
15188 {
15189   unsigned size;
15190   enum neon_el_type type;
15191   unsigned destmask;
15192   int i;
15193
15194   destmask = 0;
15195
15196   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
15197     {
15198       if (el_type_of_type_chk (&type, &size,
15199                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
15200         {
15201           neon_modify_type_size (mods, &type, &size);
15202           destmask |= type_chk_of_el_type (type, size);
15203         }
15204     }
15205
15206   return destmask;
15207 }
15208
15209 /* Check type and return type classification.
15210    The manual states (paraphrase): If one datatype is given, it indicates the
15211    type given in:
15212     - the second operand, if there is one
15213     - the operand, if there is no second operand
15214     - the result, if there are no operands.
15215    This isn't quite good enough though, so we use a concept of a "key" datatype
15216    which is set on a per-instruction basis, which is the one which matters when
15217    only one data type is written.
15218    Note: this function has side-effects (e.g. filling in missing operands). All
15219    Neon instructions should call it before performing bit encoding.  */
15220
15221 static struct neon_type_el
15222 neon_check_type (unsigned els, enum neon_shape ns, ...)
15223 {
15224   va_list ap;
15225   unsigned i, pass, key_el = 0;
15226   unsigned types[NEON_MAX_TYPE_ELS];
15227   enum neon_el_type k_type = NT_invtype;
15228   unsigned k_size = -1u;
15229   struct neon_type_el badtype = {NT_invtype, -1};
15230   unsigned key_allowed = 0;
15231
15232   /* Optional registers in Neon instructions are always (not) in operand 1.
15233      Fill in the missing operand here, if it was omitted.  */
15234   if (els > 1 && !inst.operands[1].present)
15235     inst.operands[1] = inst.operands[0];
15236
15237   /* Suck up all the varargs.  */
15238   va_start (ap, ns);
15239   for (i = 0; i < els; i++)
15240     {
15241       unsigned thisarg = va_arg (ap, unsigned);
15242       if (thisarg == N_IGNORE_TYPE)
15243         {
15244           va_end (ap);
15245           return badtype;
15246         }
15247       types[i] = thisarg;
15248       if ((thisarg & N_KEY) != 0)
15249         key_el = i;
15250     }
15251   va_end (ap);
15252
15253   if (inst.vectype.elems > 0)
15254     for (i = 0; i < els; i++)
15255       if (inst.operands[i].vectype.type != NT_invtype)
15256         {
15257           first_error (_("types specified in both the mnemonic and operands"));
15258           return badtype;
15259         }
15260
15261   /* Duplicate inst.vectype elements here as necessary.
15262      FIXME: No idea if this is exactly the same as the ARM assembler,
15263      particularly when an insn takes one register and one non-register
15264      operand. */
15265   if (inst.vectype.elems == 1 && els > 1)
15266     {
15267       unsigned j;
15268       inst.vectype.elems = els;
15269       inst.vectype.el[key_el] = inst.vectype.el[0];
15270       for (j = 0; j < els; j++)
15271         if (j != key_el)
15272           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
15273                                                   types[j]);
15274     }
15275   else if (inst.vectype.elems == 0 && els > 0)
15276     {
15277       unsigned j;
15278       /* No types were given after the mnemonic, so look for types specified
15279          after each operand. We allow some flexibility here; as long as the
15280          "key" operand has a type, we can infer the others.  */
15281       for (j = 0; j < els; j++)
15282         if (inst.operands[j].vectype.type != NT_invtype)
15283           inst.vectype.el[j] = inst.operands[j].vectype;
15284
15285       if (inst.operands[key_el].vectype.type != NT_invtype)
15286         {
15287           for (j = 0; j < els; j++)
15288             if (inst.operands[j].vectype.type == NT_invtype)
15289               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
15290                                                       types[j]);
15291         }
15292       else
15293         {
15294           first_error (_("operand types can't be inferred"));
15295           return badtype;
15296         }
15297     }
15298   else if (inst.vectype.elems != els)
15299     {
15300       first_error (_("type specifier has the wrong number of parts"));
15301       return badtype;
15302     }
15303
15304   for (pass = 0; pass < 2; pass++)
15305     {
15306       for (i = 0; i < els; i++)
15307         {
15308           unsigned thisarg = types[i];
15309           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
15310             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
15311           enum neon_el_type g_type = inst.vectype.el[i].type;
15312           unsigned g_size = inst.vectype.el[i].size;
15313
15314           /* Decay more-specific signed & unsigned types to sign-insensitive
15315              integer types if sign-specific variants are unavailable.  */
15316           if ((g_type == NT_signed || g_type == NT_unsigned)
15317               && (types_allowed & N_SU_ALL) == 0)
15318             g_type = NT_integer;
15319
15320           /* If only untyped args are allowed, decay any more specific types to
15321              them. Some instructions only care about signs for some element
15322              sizes, so handle that properly.  */
15323           if (((types_allowed & N_UNT) == 0)
15324               && ((g_size == 8 && (types_allowed & N_8) != 0)
15325                   || (g_size == 16 && (types_allowed & N_16) != 0)
15326                   || (g_size == 32 && (types_allowed & N_32) != 0)
15327                   || (g_size == 64 && (types_allowed & N_64) != 0)))
15328             g_type = NT_untyped;
15329
15330           if (pass == 0)
15331             {
15332               if ((thisarg & N_KEY) != 0)
15333                 {
15334                   k_type = g_type;
15335                   k_size = g_size;
15336                   key_allowed = thisarg & ~N_KEY;
15337
15338                   /* Check architecture constraint on FP16 extension.  */
15339                   if (k_size == 16
15340                       && k_type == NT_float
15341                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15342                     {
15343                       inst.error = _(BAD_FP16);
15344                       return badtype;
15345                     }
15346                 }
15347             }
15348           else
15349             {
15350               if ((thisarg & N_VFP) != 0)
15351                 {
15352                   enum neon_shape_el regshape;
15353                   unsigned regwidth, match;
15354
15355                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
15356                   if (ns == NS_NULL)
15357                     {
15358                       first_error (_("invalid instruction shape"));
15359                       return badtype;
15360                     }
15361                   regshape = neon_shape_tab[ns].el[i];
15362                   regwidth = neon_shape_el_size[regshape];
15363
15364                   /* In VFP mode, operands must match register widths. If we
15365                      have a key operand, use its width, else use the width of
15366                      the current operand.  */
15367                   if (k_size != -1u)
15368                     match = k_size;
15369                   else
15370                     match = g_size;
15371
15372                   /* FP16 will use a single precision register.  */
15373                   if (regwidth == 32 && match == 16)
15374                     {
15375                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15376                         match = regwidth;
15377                       else
15378                         {
15379                           inst.error = _(BAD_FP16);
15380                           return badtype;
15381                         }
15382                     }
15383
15384                   if (regwidth != match)
15385                     {
15386                       first_error (_("operand size must match register width"));
15387                       return badtype;
15388                     }
15389                 }
15390
15391               if ((thisarg & N_EQK) == 0)
15392                 {
15393                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
15394
15395                   if ((given_type & types_allowed) == 0)
15396                     {
15397                       first_error (BAD_SIMD_TYPE);
15398                       return badtype;
15399                     }
15400                 }
15401               else
15402                 {
15403                   enum neon_el_type mod_k_type = k_type;
15404                   unsigned mod_k_size = k_size;
15405                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
15406                   if (g_type != mod_k_type || g_size != mod_k_size)
15407                     {
15408                       first_error (_("inconsistent types in Neon instruction"));
15409                       return badtype;
15410                     }
15411                 }
15412             }
15413         }
15414     }
15415
15416   return inst.vectype.el[key_el];
15417 }
15418
15419 /* Neon-style VFP instruction forwarding.  */
15420
15421 /* Thumb VFP instructions have 0xE in the condition field.  */
15422
15423 static void
15424 do_vfp_cond_or_thumb (void)
15425 {
15426   inst.is_neon = 1;
15427
15428   if (thumb_mode)
15429     inst.instruction |= 0xe0000000;
15430   else
15431     inst.instruction |= inst.cond << 28;
15432 }
15433
15434 /* Look up and encode a simple mnemonic, for use as a helper function for the
15435    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
15436    etc.  It is assumed that operand parsing has already been done, and that the
15437    operands are in the form expected by the given opcode (this isn't necessarily
15438    the same as the form in which they were parsed, hence some massaging must
15439    take place before this function is called).
15440    Checks current arch version against that in the looked-up opcode.  */
15441
15442 static void
15443 do_vfp_nsyn_opcode (const char *opname)
15444 {
15445   const struct asm_opcode *opcode;
15446
15447   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
15448
15449   if (!opcode)
15450     abort ();
15451
15452   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
15453                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
15454               _(BAD_FPU));
15455
15456   inst.is_neon = 1;
15457
15458   if (thumb_mode)
15459     {
15460       inst.instruction = opcode->tvalue;
15461       opcode->tencode ();
15462     }
15463   else
15464     {
15465       inst.instruction = (inst.cond << 28) | opcode->avalue;
15466       opcode->aencode ();
15467     }
15468 }
15469
15470 static void
15471 do_vfp_nsyn_add_sub (enum neon_shape rs)
15472 {
15473   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
15474
15475   if (rs == NS_FFF || rs == NS_HHH)
15476     {
15477       if (is_add)
15478         do_vfp_nsyn_opcode ("fadds");
15479       else
15480         do_vfp_nsyn_opcode ("fsubs");
15481
15482       /* ARMv8.2 fp16 instruction.  */
15483       if (rs == NS_HHH)
15484         do_scalar_fp16_v82_encode ();
15485     }
15486   else
15487     {
15488       if (is_add)
15489         do_vfp_nsyn_opcode ("faddd");
15490       else
15491         do_vfp_nsyn_opcode ("fsubd");
15492     }
15493 }
15494
15495 /* Check operand types to see if this is a VFP instruction, and if so call
15496    PFN ().  */
15497
15498 static int
15499 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
15500 {
15501   enum neon_shape rs;
15502   struct neon_type_el et;
15503
15504   switch (args)
15505     {
15506     case 2:
15507       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15508       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15509       break;
15510
15511     case 3:
15512       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15513       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15514                             N_F_ALL | N_KEY | N_VFP);
15515       break;
15516
15517     default:
15518       abort ();
15519     }
15520
15521   if (et.type != NT_invtype)
15522     {
15523       pfn (rs);
15524       return SUCCESS;
15525     }
15526
15527   inst.error = NULL;
15528   return FAIL;
15529 }
15530
15531 static void
15532 do_vfp_nsyn_mla_mls (enum neon_shape rs)
15533 {
15534   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
15535
15536   if (rs == NS_FFF || rs == NS_HHH)
15537     {
15538       if (is_mla)
15539         do_vfp_nsyn_opcode ("fmacs");
15540       else
15541         do_vfp_nsyn_opcode ("fnmacs");
15542
15543       /* ARMv8.2 fp16 instruction.  */
15544       if (rs == NS_HHH)
15545         do_scalar_fp16_v82_encode ();
15546     }
15547   else
15548     {
15549       if (is_mla)
15550         do_vfp_nsyn_opcode ("fmacd");
15551       else
15552         do_vfp_nsyn_opcode ("fnmacd");
15553     }
15554 }
15555
15556 static void
15557 do_vfp_nsyn_fma_fms (enum neon_shape rs)
15558 {
15559   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
15560
15561   if (rs == NS_FFF || rs == NS_HHH)
15562     {
15563       if (is_fma)
15564         do_vfp_nsyn_opcode ("ffmas");
15565       else
15566         do_vfp_nsyn_opcode ("ffnmas");
15567
15568       /* ARMv8.2 fp16 instruction.  */
15569       if (rs == NS_HHH)
15570         do_scalar_fp16_v82_encode ();
15571     }
15572   else
15573     {
15574       if (is_fma)
15575         do_vfp_nsyn_opcode ("ffmad");
15576       else
15577         do_vfp_nsyn_opcode ("ffnmad");
15578     }
15579 }
15580
15581 static void
15582 do_vfp_nsyn_mul (enum neon_shape rs)
15583 {
15584   if (rs == NS_FFF || rs == NS_HHH)
15585     {
15586       do_vfp_nsyn_opcode ("fmuls");
15587
15588       /* ARMv8.2 fp16 instruction.  */
15589       if (rs == NS_HHH)
15590         do_scalar_fp16_v82_encode ();
15591     }
15592   else
15593     do_vfp_nsyn_opcode ("fmuld");
15594 }
15595
15596 static void
15597 do_vfp_nsyn_abs_neg (enum neon_shape rs)
15598 {
15599   int is_neg = (inst.instruction & 0x80) != 0;
15600   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
15601
15602   if (rs == NS_FF || rs == NS_HH)
15603     {
15604       if (is_neg)
15605         do_vfp_nsyn_opcode ("fnegs");
15606       else
15607         do_vfp_nsyn_opcode ("fabss");
15608
15609       /* ARMv8.2 fp16 instruction.  */
15610       if (rs == NS_HH)
15611         do_scalar_fp16_v82_encode ();
15612     }
15613   else
15614     {
15615       if (is_neg)
15616         do_vfp_nsyn_opcode ("fnegd");
15617       else
15618         do_vfp_nsyn_opcode ("fabsd");
15619     }
15620 }
15621
15622 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
15623    insns belong to Neon, and are handled elsewhere.  */
15624
15625 static void
15626 do_vfp_nsyn_ldm_stm (int is_dbmode)
15627 {
15628   int is_ldm = (inst.instruction & (1 << 20)) != 0;
15629   if (is_ldm)
15630     {
15631       if (is_dbmode)
15632         do_vfp_nsyn_opcode ("fldmdbs");
15633       else
15634         do_vfp_nsyn_opcode ("fldmias");
15635     }
15636   else
15637     {
15638       if (is_dbmode)
15639         do_vfp_nsyn_opcode ("fstmdbs");
15640       else
15641         do_vfp_nsyn_opcode ("fstmias");
15642     }
15643 }
15644
15645 static void
15646 do_vfp_nsyn_sqrt (void)
15647 {
15648   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15649   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15650
15651   if (rs == NS_FF || rs == NS_HH)
15652     {
15653       do_vfp_nsyn_opcode ("fsqrts");
15654
15655       /* ARMv8.2 fp16 instruction.  */
15656       if (rs == NS_HH)
15657         do_scalar_fp16_v82_encode ();
15658     }
15659   else
15660     do_vfp_nsyn_opcode ("fsqrtd");
15661 }
15662
15663 static void
15664 do_vfp_nsyn_div (void)
15665 {
15666   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15667   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15668                    N_F_ALL | N_KEY | N_VFP);
15669
15670   if (rs == NS_FFF || rs == NS_HHH)
15671     {
15672       do_vfp_nsyn_opcode ("fdivs");
15673
15674       /* ARMv8.2 fp16 instruction.  */
15675       if (rs == NS_HHH)
15676         do_scalar_fp16_v82_encode ();
15677     }
15678   else
15679     do_vfp_nsyn_opcode ("fdivd");
15680 }
15681
15682 static void
15683 do_vfp_nsyn_nmul (void)
15684 {
15685   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15686   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15687                    N_F_ALL | N_KEY | N_VFP);
15688
15689   if (rs == NS_FFF || rs == NS_HHH)
15690     {
15691       NEON_ENCODE (SINGLE, inst);
15692       do_vfp_sp_dyadic ();
15693
15694       /* ARMv8.2 fp16 instruction.  */
15695       if (rs == NS_HHH)
15696         do_scalar_fp16_v82_encode ();
15697     }
15698   else
15699     {
15700       NEON_ENCODE (DOUBLE, inst);
15701       do_vfp_dp_rd_rn_rm ();
15702     }
15703   do_vfp_cond_or_thumb ();
15704
15705 }
15706
15707 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
15708    (0, 1, 2, 3).  */
15709
15710 static unsigned
15711 neon_logbits (unsigned x)
15712 {
15713   return ffs (x) - 4;
15714 }
15715
15716 #define LOW4(R) ((R) & 0xf)
15717 #define HI1(R) (((R) >> 4) & 1)
15718
15719 static unsigned
15720 mve_get_vcmp_vpt_cond (struct neon_type_el et)
15721 {
15722   switch (et.type)
15723     {
15724     default:
15725       first_error (BAD_EL_TYPE);
15726       return 0;
15727     case NT_float:
15728       switch (inst.operands[0].imm)
15729         {
15730         default:
15731           first_error (_("invalid condition"));
15732           return 0;
15733         case 0x0:
15734           /* eq.  */
15735           return 0;
15736         case 0x1:
15737           /* ne.  */
15738           return 1;
15739         case 0xa:
15740           /* ge/  */
15741           return 4;
15742         case 0xb:
15743           /* lt.  */
15744           return 5;
15745         case 0xc:
15746           /* gt.  */
15747           return 6;
15748         case 0xd:
15749           /* le.  */
15750           return 7;
15751         }
15752     case NT_integer:
15753       /* only accept eq and ne.  */
15754       if (inst.operands[0].imm > 1)
15755         {
15756           first_error (_("invalid condition"));
15757           return 0;
15758         }
15759       return inst.operands[0].imm;
15760     case NT_unsigned:
15761       if (inst.operands[0].imm == 0x2)
15762         return 2;
15763       else if (inst.operands[0].imm == 0x8)
15764         return 3;
15765       else
15766         {
15767           first_error (_("invalid condition"));
15768           return 0;
15769         }
15770     case NT_signed:
15771       switch (inst.operands[0].imm)
15772         {
15773           default:
15774             first_error (_("invalid condition"));
15775             return 0;
15776           case 0xa:
15777             /* ge.  */
15778             return 4;
15779           case 0xb:
15780             /* lt.  */
15781             return 5;
15782           case 0xc:
15783             /* gt.  */
15784             return 6;
15785           case 0xd:
15786             /* le.  */
15787             return 7;
15788         }
15789     }
15790   /* Should be unreachable.  */
15791   abort ();
15792 }
15793
15794 static void
15795 do_mve_vpt (void)
15796 {
15797   /* We are dealing with a vector predicated block.  */
15798   if (inst.operands[0].present)
15799     {
15800       enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15801       struct neon_type_el et
15802         = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15803                            N_EQK);
15804
15805       unsigned fcond = mve_get_vcmp_vpt_cond (et);
15806
15807       constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15808
15809       if (et.type == NT_invtype)
15810         return;
15811
15812       if (et.type == NT_float)
15813         {
15814           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15815                       BAD_FPU);
15816           constraint (et.size != 16 && et.size != 32, BAD_EL_TYPE);
15817           inst.instruction |= (et.size == 16) << 28;
15818           inst.instruction |= 0x3 << 20;
15819         }
15820       else
15821         {
15822           constraint (et.size != 8 && et.size != 16 && et.size != 32,
15823                       BAD_EL_TYPE);
15824           inst.instruction |= 1 << 28;
15825           inst.instruction |= neon_logbits (et.size) << 20;
15826         }
15827
15828       if (inst.operands[2].isquad)
15829         {
15830           inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15831           inst.instruction |= LOW4 (inst.operands[2].reg);
15832           inst.instruction |= (fcond & 0x2) >> 1;
15833         }
15834       else
15835         {
15836           if (inst.operands[2].reg == REG_SP)
15837             as_tsktsk (MVE_BAD_SP);
15838           inst.instruction |= 1 << 6;
15839           inst.instruction |= (fcond & 0x2) << 4;
15840           inst.instruction |= inst.operands[2].reg;
15841         }
15842       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15843       inst.instruction |= (fcond & 0x4) << 10;
15844       inst.instruction |= (fcond & 0x1) << 7;
15845
15846     }
15847     set_pred_insn_type (VPT_INSN);
15848     now_pred.cc = 0;
15849     now_pred.mask = ((inst.instruction & 0x00400000) >> 19)
15850                     | ((inst.instruction & 0xe000) >> 13);
15851     now_pred.warn_deprecated = FALSE;
15852     now_pred.type = VECTOR_PRED;
15853     inst.is_neon = 1;
15854 }
15855
15856 static void
15857 do_mve_vcmp (void)
15858 {
15859   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
15860   if (!inst.operands[1].isreg || !inst.operands[1].isquad)
15861     first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
15862   if (!inst.operands[2].present)
15863     first_error (_("MVE vector or ARM register expected"));
15864   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15865
15866   /* Deal with 'else' conditional MVE's vcmp, it will be parsed as vcmpe.  */
15867   if ((inst.instruction & 0xffffffff) == N_MNEM_vcmpe
15868       && inst.operands[1].isquad)
15869     {
15870       inst.instruction = N_MNEM_vcmp;
15871       inst.cond = 0x10;
15872     }
15873
15874   if (inst.cond > COND_ALWAYS)
15875     inst.pred_insn_type = INSIDE_VPT_INSN;
15876   else
15877     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15878
15879   enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15880   struct neon_type_el et
15881     = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15882                        N_EQK);
15883
15884   constraint (rs == NS_IQR && inst.operands[2].reg == REG_PC
15885               && !inst.operands[2].iszr, BAD_PC);
15886
15887   unsigned fcond = mve_get_vcmp_vpt_cond (et);
15888
15889   inst.instruction = 0xee010f00;
15890   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15891   inst.instruction |= (fcond & 0x4) << 10;
15892   inst.instruction |= (fcond & 0x1) << 7;
15893   if (et.type == NT_float)
15894     {
15895       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15896                   BAD_FPU);
15897       inst.instruction |= (et.size == 16) << 28;
15898       inst.instruction |= 0x3 << 20;
15899     }
15900   else
15901     {
15902       inst.instruction |= 1 << 28;
15903       inst.instruction |= neon_logbits (et.size) << 20;
15904     }
15905   if (inst.operands[2].isquad)
15906     {
15907       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15908       inst.instruction |= (fcond & 0x2) >> 1;
15909       inst.instruction |= LOW4 (inst.operands[2].reg);
15910     }
15911   else
15912     {
15913       if (inst.operands[2].reg == REG_SP)
15914         as_tsktsk (MVE_BAD_SP);
15915       inst.instruction |= 1 << 6;
15916       inst.instruction |= (fcond & 0x2) << 4;
15917       inst.instruction |= inst.operands[2].reg;
15918     }
15919
15920   inst.is_neon = 1;
15921   return;
15922 }
15923
15924 static void
15925 do_mve_vmaxa_vmina (void)
15926 {
15927   if (inst.cond > COND_ALWAYS)
15928     inst.pred_insn_type = INSIDE_VPT_INSN;
15929   else
15930     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15931
15932   enum neon_shape rs = neon_select_shape (NS_QQ, NS_NULL);
15933   struct neon_type_el et
15934     = neon_check_type (2, rs, N_EQK, N_KEY | N_S8 | N_S16 | N_S32);
15935
15936   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15937   inst.instruction |= neon_logbits (et.size) << 18;
15938   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15939   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15940   inst.instruction |= LOW4 (inst.operands[1].reg);
15941   inst.is_neon = 1;
15942 }
15943
15944 static void
15945 do_mve_vfmas (void)
15946 {
15947   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
15948   struct neon_type_el et
15949     = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK, N_EQK);
15950
15951   if (inst.cond > COND_ALWAYS)
15952     inst.pred_insn_type = INSIDE_VPT_INSN;
15953   else
15954     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15955
15956   if (inst.operands[2].reg == REG_SP)
15957     as_tsktsk (MVE_BAD_SP);
15958   else if (inst.operands[2].reg == REG_PC)
15959     as_tsktsk (MVE_BAD_PC);
15960
15961   inst.instruction |= (et.size == 16) << 28;
15962   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15963   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15964   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15965   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15966   inst.instruction |= inst.operands[2].reg;
15967   inst.is_neon = 1;
15968 }
15969
15970 static void
15971 do_mve_viddup (void)
15972 {
15973   if (inst.cond > COND_ALWAYS)
15974     inst.pred_insn_type = INSIDE_VPT_INSN;
15975   else
15976     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15977
15978   unsigned imm = inst.relocs[0].exp.X_add_number;
15979   constraint (imm != 1 && imm != 2 && imm != 4 && imm != 8,
15980               _("immediate must be either 1, 2, 4 or 8"));
15981
15982   enum neon_shape rs;
15983   struct neon_type_el et;
15984   unsigned Rm;
15985   if (inst.instruction == M_MNEM_vddup || inst.instruction == M_MNEM_vidup)
15986     {
15987       rs = neon_select_shape (NS_QRI, NS_NULL);
15988       et = neon_check_type (2, rs, N_KEY | N_U8 | N_U16 | N_U32, N_EQK);
15989       Rm = 7;
15990     }
15991   else
15992     {
15993       constraint ((inst.operands[2].reg % 2) != 1, BAD_EVEN);
15994       if (inst.operands[2].reg == REG_SP)
15995         as_tsktsk (MVE_BAD_SP);
15996       else if (inst.operands[2].reg == REG_PC)
15997         first_error (BAD_PC);
15998
15999       rs = neon_select_shape (NS_QRRI, NS_NULL);
16000       et = neon_check_type (3, rs, N_KEY | N_U8 | N_U16 | N_U32, N_EQK, N_EQK);
16001       Rm = inst.operands[2].reg >> 1;
16002     }
16003   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16004   inst.instruction |= neon_logbits (et.size) << 20;
16005   inst.instruction |= inst.operands[1].reg << 16;
16006   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16007   inst.instruction |= (imm > 2) << 7;
16008   inst.instruction |= Rm << 1;
16009   inst.instruction |= (imm == 2 || imm == 8);
16010   inst.is_neon = 1;
16011 }
16012
16013 static void
16014 do_mve_vmlas (void)
16015 {
16016   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
16017   struct neon_type_el et
16018     = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16019
16020   if (inst.operands[2].reg == REG_PC)
16021     as_tsktsk (MVE_BAD_PC);
16022   else if (inst.operands[2].reg == REG_SP)
16023     as_tsktsk (MVE_BAD_SP);
16024
16025   if (inst.cond > COND_ALWAYS)
16026     inst.pred_insn_type = INSIDE_VPT_INSN;
16027   else
16028     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16029
16030   inst.instruction |= (et.type == NT_unsigned) << 28;
16031   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16032   inst.instruction |= neon_logbits (et.size) << 20;
16033   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16034   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16035   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16036   inst.instruction |= inst.operands[2].reg;
16037   inst.is_neon = 1;
16038 }
16039
16040 static void
16041 do_mve_vshll (void)
16042 {
16043   struct neon_type_el et
16044     = neon_check_type (2, NS_QQI, N_EQK, N_S8 | N_U8 | N_S16 | N_U16 | N_KEY);
16045
16046   if (inst.cond > COND_ALWAYS)
16047     inst.pred_insn_type = INSIDE_VPT_INSN;
16048   else
16049     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16050
16051   int imm = inst.operands[2].imm;
16052   constraint (imm < 1 || (unsigned)imm > et.size,
16053               _("immediate value out of range"));
16054
16055   if ((unsigned)imm == et.size)
16056     {
16057       inst.instruction |= neon_logbits (et.size) << 18;
16058       inst.instruction |= 0x110001;
16059     }
16060   else
16061     {
16062       inst.instruction |= (et.size + imm) << 16;
16063       inst.instruction |= 0x800140;
16064     }
16065
16066   inst.instruction |= (et.type == NT_unsigned) << 28;
16067   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16068   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16069   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16070   inst.instruction |= LOW4 (inst.operands[1].reg);
16071   inst.is_neon = 1;
16072 }
16073
16074 static void
16075 do_mve_vshlc (void)
16076 {
16077   if (inst.cond > COND_ALWAYS)
16078     inst.pred_insn_type = INSIDE_VPT_INSN;
16079   else
16080     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16081
16082   if (inst.operands[1].reg == REG_PC)
16083     as_tsktsk (MVE_BAD_PC);
16084   else if (inst.operands[1].reg == REG_SP)
16085     as_tsktsk (MVE_BAD_SP);
16086
16087   int imm = inst.operands[2].imm;
16088   constraint (imm < 1 || imm > 32, _("immediate value out of range"));
16089
16090   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16091   inst.instruction |= (imm & 0x1f) << 16;
16092   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16093   inst.instruction |= inst.operands[1].reg;
16094   inst.is_neon = 1;
16095 }
16096
16097 static void
16098 do_mve_vshrn (void)
16099 {
16100   unsigned types;
16101   switch (inst.instruction)
16102     {
16103     case M_MNEM_vshrnt:
16104     case M_MNEM_vshrnb:
16105     case M_MNEM_vrshrnt:
16106     case M_MNEM_vrshrnb:
16107       types = N_I16 | N_I32;
16108       break;
16109     case M_MNEM_vqshrnt:
16110     case M_MNEM_vqshrnb:
16111     case M_MNEM_vqrshrnt:
16112     case M_MNEM_vqrshrnb:
16113       types = N_U16 | N_U32 | N_S16 | N_S32;
16114       break;
16115     case M_MNEM_vqshrunt:
16116     case M_MNEM_vqshrunb:
16117     case M_MNEM_vqrshrunt:
16118     case M_MNEM_vqrshrunb:
16119       types = N_S16 | N_S32;
16120       break;
16121     default:
16122       abort ();
16123     }
16124
16125   struct neon_type_el et = neon_check_type (2, NS_QQI, N_EQK, types | N_KEY);
16126
16127   if (inst.cond > COND_ALWAYS)
16128     inst.pred_insn_type = INSIDE_VPT_INSN;
16129   else
16130     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16131
16132   unsigned Qd = inst.operands[0].reg;
16133   unsigned Qm = inst.operands[1].reg;
16134   unsigned imm = inst.operands[2].imm;
16135   constraint (imm < 1 || ((unsigned) imm) > (et.size / 2),
16136               et.size == 16
16137               ? _("immediate operand expected in the range [1,8]")
16138               : _("immediate operand expected in the range [1,16]"));
16139
16140   inst.instruction |= (et.type == NT_unsigned) << 28;
16141   inst.instruction |= HI1 (Qd) << 22;
16142   inst.instruction |= (et.size - imm) << 16;
16143   inst.instruction |= LOW4 (Qd) << 12;
16144   inst.instruction |= HI1 (Qm) << 5;
16145   inst.instruction |= LOW4 (Qm);
16146   inst.is_neon = 1;
16147 }
16148
16149 static void
16150 do_mve_vqmovn (void)
16151 {
16152   struct neon_type_el et;
16153   if (inst.instruction == M_MNEM_vqmovnt
16154      || inst.instruction == M_MNEM_vqmovnb)
16155     et = neon_check_type (2, NS_QQ, N_EQK,
16156                           N_U16 | N_U32 | N_S16 | N_S32 | N_KEY);
16157   else
16158     et = neon_check_type (2, NS_QQ, N_EQK, N_S16 | N_S32 | N_KEY);
16159
16160   if (inst.cond > COND_ALWAYS)
16161     inst.pred_insn_type = INSIDE_VPT_INSN;
16162   else
16163     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16164
16165   inst.instruction |= (et.type == NT_unsigned) << 28;
16166   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16167   inst.instruction |= (et.size == 32) << 18;
16168   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16169   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16170   inst.instruction |= LOW4 (inst.operands[1].reg);
16171   inst.is_neon = 1;
16172 }
16173
16174 static void
16175 do_mve_vpsel (void)
16176 {
16177   neon_select_shape (NS_QQQ, NS_NULL);
16178
16179   if (inst.cond > COND_ALWAYS)
16180     inst.pred_insn_type = INSIDE_VPT_INSN;
16181   else
16182     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16183
16184   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16185   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16186   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16187   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16188   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16189   inst.instruction |= LOW4 (inst.operands[2].reg);
16190   inst.is_neon = 1;
16191 }
16192
16193 static void
16194 do_mve_vpnot (void)
16195 {
16196   if (inst.cond > COND_ALWAYS)
16197     inst.pred_insn_type = INSIDE_VPT_INSN;
16198   else
16199     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16200 }
16201
16202 static void
16203 do_mve_vmaxnma_vminnma (void)
16204 {
16205   enum neon_shape rs = neon_select_shape (NS_QQ, NS_NULL);
16206   struct neon_type_el et
16207     = neon_check_type (2, rs, N_EQK, N_F_MVE | N_KEY);
16208
16209   if (inst.cond > COND_ALWAYS)
16210     inst.pred_insn_type = INSIDE_VPT_INSN;
16211   else
16212     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16213
16214   inst.instruction |= (et.size == 16) << 28;
16215   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16216   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16217   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16218   inst.instruction |= LOW4 (inst.operands[1].reg);
16219   inst.is_neon = 1;
16220 }
16221
16222 static void
16223 do_mve_vcmul (void)
16224 {
16225   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
16226   struct neon_type_el et
16227     = neon_check_type (3, rs, N_EQK, N_EQK, N_F_MVE | N_KEY);
16228
16229   if (inst.cond > COND_ALWAYS)
16230     inst.pred_insn_type = INSIDE_VPT_INSN;
16231   else
16232     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16233
16234   unsigned rot = inst.relocs[0].exp.X_add_number;
16235   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
16236               _("immediate out of range"));
16237
16238   if (et.size == 32 && (inst.operands[0].reg == inst.operands[1].reg
16239                         || inst.operands[0].reg == inst.operands[2].reg))
16240     as_tsktsk (BAD_MVE_SRCDEST);
16241
16242   inst.instruction |= (et.size == 32) << 28;
16243   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16244   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16245   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16246   inst.instruction |= (rot > 90) << 12;
16247   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16248   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16249   inst.instruction |= LOW4 (inst.operands[2].reg);
16250   inst.instruction |= (rot == 90 || rot == 270);
16251   inst.is_neon = 1;
16252 }
16253
16254 /* To handle the Low Overhead Loop instructions
16255    in Armv8.1-M Mainline and MVE.  */
16256 static void
16257 do_t_loloop (void)
16258 {
16259   unsigned long insn = inst.instruction;
16260
16261   inst.instruction = THUMB_OP32 (inst.instruction);
16262
16263   if (insn == T_MNEM_lctp)
16264     return;
16265
16266   set_pred_insn_type (MVE_OUTSIDE_PRED_INSN);
16267
16268   if (insn == T_MNEM_wlstp || insn == T_MNEM_dlstp)
16269     {
16270       struct neon_type_el et
16271        = neon_check_type (2, NS_RR, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16272       inst.instruction |= neon_logbits (et.size) << 20;
16273       inst.is_neon = 1;
16274     }
16275
16276   switch (insn)
16277     {
16278     case T_MNEM_letp:
16279       constraint (!inst.operands[0].present,
16280                   _("expected LR"));
16281       /* fall through.  */
16282     case T_MNEM_le:
16283       /* le <label>.  */
16284       if (!inst.operands[0].present)
16285        inst.instruction |= 1 << 21;
16286
16287       v8_1_loop_reloc (TRUE);
16288       break;
16289
16290     case T_MNEM_wls:
16291     case T_MNEM_wlstp:
16292       v8_1_loop_reloc (FALSE);
16293       /* fall through.  */
16294     case T_MNEM_dlstp:
16295     case T_MNEM_dls:
16296       constraint (inst.operands[1].isreg != 1, BAD_ARGS);
16297
16298       if (insn == T_MNEM_wlstp || insn == T_MNEM_dlstp)
16299        constraint (inst.operands[1].reg == REG_PC, BAD_PC);
16300       else if (inst.operands[1].reg == REG_PC)
16301        as_tsktsk (MVE_BAD_PC);
16302       if (inst.operands[1].reg == REG_SP)
16303        as_tsktsk (MVE_BAD_SP);
16304
16305       inst.instruction |= (inst.operands[1].reg << 16);
16306       break;
16307
16308     default:
16309       abort ();
16310     }
16311 }
16312
16313
16314 static void
16315 do_vfp_nsyn_cmp (void)
16316 {
16317   enum neon_shape rs;
16318   if (!inst.operands[0].isreg)
16319     {
16320       do_mve_vcmp ();
16321       return;
16322     }
16323   else
16324     {
16325       constraint (inst.operands[2].present, BAD_SYNTAX);
16326       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd),
16327                   BAD_FPU);
16328     }
16329
16330   if (inst.operands[1].isreg)
16331     {
16332       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
16333       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
16334
16335       if (rs == NS_FF || rs == NS_HH)
16336         {
16337           NEON_ENCODE (SINGLE, inst);
16338           do_vfp_sp_monadic ();
16339         }
16340       else
16341         {
16342           NEON_ENCODE (DOUBLE, inst);
16343           do_vfp_dp_rd_rm ();
16344         }
16345     }
16346   else
16347     {
16348       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
16349       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
16350
16351       switch (inst.instruction & 0x0fffffff)
16352         {
16353         case N_MNEM_vcmp:
16354           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
16355           break;
16356         case N_MNEM_vcmpe:
16357           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
16358           break;
16359         default:
16360           abort ();
16361         }
16362
16363       if (rs == NS_FI || rs == NS_HI)
16364         {
16365           NEON_ENCODE (SINGLE, inst);
16366           do_vfp_sp_compare_z ();
16367         }
16368       else
16369         {
16370           NEON_ENCODE (DOUBLE, inst);
16371           do_vfp_dp_rd ();
16372         }
16373     }
16374   do_vfp_cond_or_thumb ();
16375
16376   /* ARMv8.2 fp16 instruction.  */
16377   if (rs == NS_HI || rs == NS_HH)
16378     do_scalar_fp16_v82_encode ();
16379 }
16380
16381 static void
16382 nsyn_insert_sp (void)
16383 {
16384   inst.operands[1] = inst.operands[0];
16385   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
16386   inst.operands[0].reg = REG_SP;
16387   inst.operands[0].isreg = 1;
16388   inst.operands[0].writeback = 1;
16389   inst.operands[0].present = 1;
16390 }
16391
16392 static void
16393 do_vfp_nsyn_push (void)
16394 {
16395   nsyn_insert_sp ();
16396
16397   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16398               _("register list must contain at least 1 and at most 16 "
16399                 "registers"));
16400
16401   if (inst.operands[1].issingle)
16402     do_vfp_nsyn_opcode ("fstmdbs");
16403   else
16404     do_vfp_nsyn_opcode ("fstmdbd");
16405 }
16406
16407 static void
16408 do_vfp_nsyn_pop (void)
16409 {
16410   nsyn_insert_sp ();
16411
16412   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16413               _("register list must contain at least 1 and at most 16 "
16414                 "registers"));
16415
16416   if (inst.operands[1].issingle)
16417     do_vfp_nsyn_opcode ("fldmias");
16418   else
16419     do_vfp_nsyn_opcode ("fldmiad");
16420 }
16421
16422 /* Fix up Neon data-processing instructions, ORing in the correct bits for
16423    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
16424
16425 static void
16426 neon_dp_fixup (struct arm_it* insn)
16427 {
16428   unsigned int i = insn->instruction;
16429   insn->is_neon = 1;
16430
16431   if (thumb_mode)
16432     {
16433       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
16434       if (i & (1 << 24))
16435         i |= 1 << 28;
16436
16437       i &= ~(1 << 24);
16438
16439       i |= 0xef000000;
16440     }
16441   else
16442     i |= 0xf2000000;
16443
16444   insn->instruction = i;
16445 }
16446
16447 static void
16448 mve_encode_qqr (int size, int U, int fp)
16449 {
16450   if (inst.operands[2].reg == REG_SP)
16451     as_tsktsk (MVE_BAD_SP);
16452   else if (inst.operands[2].reg == REG_PC)
16453     as_tsktsk (MVE_BAD_PC);
16454
16455   if (fp)
16456     {
16457       /* vadd.  */
16458       if (((unsigned)inst.instruction) == 0xd00)
16459         inst.instruction = 0xee300f40;
16460       /* vsub.  */
16461       else if (((unsigned)inst.instruction) == 0x200d00)
16462         inst.instruction = 0xee301f40;
16463       /* vmul.  */
16464       else if (((unsigned)inst.instruction) == 0x1000d10)
16465         inst.instruction = 0xee310e60;
16466
16467       /* Setting size which is 1 for F16 and 0 for F32.  */
16468       inst.instruction |= (size == 16) << 28;
16469     }
16470   else
16471     {
16472       /* vadd.  */
16473       if (((unsigned)inst.instruction) == 0x800)
16474         inst.instruction = 0xee010f40;
16475       /* vsub.  */
16476       else if (((unsigned)inst.instruction) == 0x1000800)
16477         inst.instruction = 0xee011f40;
16478       /* vhadd.  */
16479       else if (((unsigned)inst.instruction) == 0)
16480         inst.instruction = 0xee000f40;
16481       /* vhsub.  */
16482       else if (((unsigned)inst.instruction) == 0x200)
16483         inst.instruction = 0xee001f40;
16484       /* vmla.  */
16485       else if (((unsigned)inst.instruction) == 0x900)
16486         inst.instruction = 0xee010e40;
16487       /* vmul.  */
16488       else if (((unsigned)inst.instruction) == 0x910)
16489         inst.instruction = 0xee011e60;
16490       /* vqadd.  */
16491       else if (((unsigned)inst.instruction) == 0x10)
16492         inst.instruction = 0xee000f60;
16493       /* vqsub.  */
16494       else if (((unsigned)inst.instruction) == 0x210)
16495         inst.instruction = 0xee001f60;
16496       /* vqrdmlah.  */
16497       else if (((unsigned)inst.instruction) == 0x3000b10)
16498         inst.instruction = 0xee000e40;
16499       /* vqdmulh.  */
16500       else if (((unsigned)inst.instruction) == 0x0000b00)
16501         inst.instruction = 0xee010e60;
16502       /* vqrdmulh.  */
16503       else if (((unsigned)inst.instruction) == 0x1000b00)
16504         inst.instruction = 0xfe010e60;
16505
16506       /* Set U-bit.  */
16507       inst.instruction |= U << 28;
16508
16509       /* Setting bits for size.  */
16510       inst.instruction |= neon_logbits (size) << 20;
16511     }
16512   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16513   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16514   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16515   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16516   inst.instruction |= inst.operands[2].reg;
16517   inst.is_neon = 1;
16518 }
16519
16520 static void
16521 mve_encode_rqq (unsigned bit28, unsigned size)
16522 {
16523   inst.instruction |= bit28 << 28;
16524   inst.instruction |= neon_logbits (size) << 20;
16525   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16526   inst.instruction |= inst.operands[0].reg << 12;
16527   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16528   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16529   inst.instruction |= LOW4 (inst.operands[2].reg);
16530   inst.is_neon = 1;
16531 }
16532
16533 static void
16534 mve_encode_qqq (int ubit, int size)
16535 {
16536
16537   inst.instruction |= (ubit != 0) << 28;
16538   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16539   inst.instruction |= neon_logbits (size) << 20;
16540   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16541   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16542   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16543   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16544   inst.instruction |= LOW4 (inst.operands[2].reg);
16545
16546   inst.is_neon = 1;
16547 }
16548
16549 static void
16550 mve_encode_rq (unsigned bit28, unsigned size)
16551 {
16552   inst.instruction |= bit28 << 28;
16553   inst.instruction |= neon_logbits (size) << 18;
16554   inst.instruction |= inst.operands[0].reg << 12;
16555   inst.instruction |= LOW4 (inst.operands[1].reg);
16556   inst.is_neon = 1;
16557 }
16558
16559 static void
16560 mve_encode_rrqq (unsigned U, unsigned size)
16561 {
16562   constraint (inst.operands[3].reg > 14, MVE_BAD_QREG);
16563
16564   inst.instruction |= U << 28;
16565   inst.instruction |= (inst.operands[1].reg >> 1) << 20;
16566   inst.instruction |= LOW4 (inst.operands[2].reg) << 16;
16567   inst.instruction |= (size == 32) << 16;
16568   inst.instruction |= inst.operands[0].reg << 12;
16569   inst.instruction |= HI1 (inst.operands[2].reg) << 7;
16570   inst.instruction |= inst.operands[3].reg;
16571   inst.is_neon = 1;
16572 }
16573
16574 /* Encode insns with bit pattern:
16575
16576   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16577   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
16578
16579   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
16580   different meaning for some instruction.  */
16581
16582 static void
16583 neon_three_same (int isquad, int ubit, int size)
16584 {
16585   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16586   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16587   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16588   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16589   inst.instruction |= LOW4 (inst.operands[2].reg);
16590   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16591   inst.instruction |= (isquad != 0) << 6;
16592   inst.instruction |= (ubit != 0) << 24;
16593   if (size != -1)
16594     inst.instruction |= neon_logbits (size) << 20;
16595
16596   neon_dp_fixup (&inst);
16597 }
16598
16599 /* Encode instructions of the form:
16600
16601   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
16602   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
16603
16604   Don't write size if SIZE == -1.  */
16605
16606 static void
16607 neon_two_same (int qbit, int ubit, int size)
16608 {
16609   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16610   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16611   inst.instruction |= LOW4 (inst.operands[1].reg);
16612   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16613   inst.instruction |= (qbit != 0) << 6;
16614   inst.instruction |= (ubit != 0) << 24;
16615
16616   if (size != -1)
16617     inst.instruction |= neon_logbits (size) << 18;
16618
16619   neon_dp_fixup (&inst);
16620 }
16621
16622 enum vfp_or_neon_is_neon_bits
16623 {
16624 NEON_CHECK_CC = 1,
16625 NEON_CHECK_ARCH = 2,
16626 NEON_CHECK_ARCH8 = 4
16627 };
16628
16629 /* Call this function if an instruction which may have belonged to the VFP or
16630  Neon instruction sets, but turned out to be a Neon instruction (due to the
16631  operand types involved, etc.). We have to check and/or fix-up a couple of
16632  things:
16633
16634    - Make sure the user hasn't attempted to make a Neon instruction
16635      conditional.
16636    - Alter the value in the condition code field if necessary.
16637    - Make sure that the arch supports Neon instructions.
16638
16639  Which of these operations take place depends on bits from enum
16640  vfp_or_neon_is_neon_bits.
16641
16642  WARNING: This function has side effects! If NEON_CHECK_CC is used and the
16643  current instruction's condition is COND_ALWAYS, the condition field is
16644  changed to inst.uncond_value.  This is necessary because instructions shared
16645  between VFP and Neon may be conditional for the VFP variants only, and the
16646  unconditional Neon version must have, e.g., 0xF in the condition field.  */
16647
16648 static int
16649 vfp_or_neon_is_neon (unsigned check)
16650 {
16651 /* Conditions are always legal in Thumb mode (IT blocks).  */
16652 if (!thumb_mode && (check & NEON_CHECK_CC))
16653   {
16654     if (inst.cond != COND_ALWAYS)
16655       {
16656         first_error (_(BAD_COND));
16657         return FAIL;
16658       }
16659     if (inst.uncond_value != -1)
16660       inst.instruction |= inst.uncond_value << 28;
16661   }
16662
16663
16664   if (((check & NEON_CHECK_ARCH) && !mark_feature_used (&fpu_neon_ext_v1))
16665       || ((check & NEON_CHECK_ARCH8)
16666           && !mark_feature_used (&fpu_neon_ext_armv8)))
16667     {
16668       first_error (_(BAD_FPU));
16669       return FAIL;
16670     }
16671
16672 return SUCCESS;
16673 }
16674
16675
16676 /* Return TRUE if the SIMD instruction is available for the current
16677    cpu_variant.  FP is set to TRUE if this is a SIMD floating-point
16678    instruction.  CHECK contains th.  CHECK contains the set of bits to pass to
16679    vfp_or_neon_is_neon for the NEON specific checks.  */
16680
16681 static bfd_boolean
16682 check_simd_pred_availability (int fp, unsigned check)
16683 {
16684 if (inst.cond > COND_ALWAYS)
16685   {
16686     if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16687       {
16688         inst.error = BAD_FPU;
16689         return FALSE;
16690       }
16691     inst.pred_insn_type = INSIDE_VPT_INSN;
16692   }
16693 else if (inst.cond < COND_ALWAYS)
16694   {
16695     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16696       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16697     else if (vfp_or_neon_is_neon (check) == FAIL)
16698       return FALSE;
16699   }
16700 else
16701   {
16702     if (!ARM_CPU_HAS_FEATURE (cpu_variant, fp ? mve_fp_ext : mve_ext)
16703         && vfp_or_neon_is_neon (check) == FAIL)
16704       return FALSE;
16705
16706     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16707       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16708   }
16709 return TRUE;
16710 }
16711
16712 /* Neon instruction encoders, in approximate order of appearance.  */
16713
16714 static void
16715 do_neon_dyadic_i_su (void)
16716 {
16717   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16718    return;
16719
16720   enum neon_shape rs;
16721   struct neon_type_el et;
16722   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16723     rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16724   else
16725     rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16726
16727   et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_32 | N_KEY);
16728
16729
16730   if (rs != NS_QQR)
16731     neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16732   else
16733     mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
16734 }
16735
16736 static void
16737 do_neon_dyadic_i64_su (void)
16738 {
16739   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
16740     return;
16741   enum neon_shape rs;
16742   struct neon_type_el et;
16743   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16744     {
16745       rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
16746       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16747     }
16748   else
16749     {
16750       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16751       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_ALL | N_KEY);
16752     }
16753   if (rs == NS_QQR)
16754     mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
16755   else
16756     neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16757 }
16758
16759 static void
16760 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
16761                 unsigned immbits)
16762 {
16763   unsigned size = et.size >> 3;
16764   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16765   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16766   inst.instruction |= LOW4 (inst.operands[1].reg);
16767   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16768   inst.instruction |= (isquad != 0) << 6;
16769   inst.instruction |= immbits << 16;
16770   inst.instruction |= (size >> 3) << 7;
16771   inst.instruction |= (size & 0x7) << 19;
16772   if (write_ubit)
16773     inst.instruction |= (uval != 0) << 24;
16774
16775   neon_dp_fixup (&inst);
16776 }
16777
16778 static void
16779 do_neon_shl (void)
16780 {
16781   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16782    return;
16783
16784   if (!inst.operands[2].isreg)
16785     {
16786       enum neon_shape rs;
16787       struct neon_type_el et;
16788       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16789         {
16790           rs = neon_select_shape (NS_QQI, NS_NULL);
16791           et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_MVE);
16792         }
16793       else
16794         {
16795           rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16796           et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
16797         }
16798       int imm = inst.operands[2].imm;
16799
16800       constraint (imm < 0 || (unsigned)imm >= et.size,
16801                   _("immediate out of range for shift"));
16802       NEON_ENCODE (IMMED, inst);
16803       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
16804     }
16805   else
16806     {
16807       enum neon_shape rs;
16808       struct neon_type_el et;
16809       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16810         {
16811           rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16812           et = neon_check_type (3, rs, N_EQK, N_SU_MVE | N_KEY, N_EQK | N_EQK);
16813         }
16814       else
16815         {
16816           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16817           et = neon_check_type (3, rs, N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
16818         }
16819
16820
16821       if (rs == NS_QQR)
16822         {
16823           constraint (inst.operands[0].reg != inst.operands[1].reg,
16824                        _("invalid instruction shape"));
16825           if (inst.operands[2].reg == REG_SP)
16826             as_tsktsk (MVE_BAD_SP);
16827           else if (inst.operands[2].reg == REG_PC)
16828             as_tsktsk (MVE_BAD_PC);
16829
16830           inst.instruction = 0xee311e60;
16831           inst.instruction |= (et.type == NT_unsigned) << 28;
16832           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16833           inst.instruction |= neon_logbits (et.size) << 18;
16834           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16835           inst.instruction |= inst.operands[2].reg;
16836           inst.is_neon = 1;
16837         }
16838       else
16839         {
16840           unsigned int tmp;
16841
16842           /* VSHL/VQSHL 3-register variants have syntax such as:
16843                vshl.xx Dd, Dm, Dn
16844              whereas other 3-register operations encoded by neon_three_same have
16845              syntax like:
16846                vadd.xx Dd, Dn, Dm
16847              (i.e. with Dn & Dm reversed). Swap operands[1].reg and
16848              operands[2].reg here.  */
16849           tmp = inst.operands[2].reg;
16850           inst.operands[2].reg = inst.operands[1].reg;
16851           inst.operands[1].reg = tmp;
16852           NEON_ENCODE (INTEGER, inst);
16853           neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16854         }
16855     }
16856 }
16857
16858 static void
16859 do_neon_qshl (void)
16860 {
16861   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16862    return;
16863
16864   if (!inst.operands[2].isreg)
16865     {
16866       enum neon_shape rs;
16867       struct neon_type_el et;
16868       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16869         {
16870           rs = neon_select_shape (NS_QQI, NS_NULL);
16871           et = neon_check_type (2, rs, N_EQK, N_KEY | N_SU_MVE);
16872         }
16873       else
16874         {
16875           rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16876           et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16877         }
16878       int imm = inst.operands[2].imm;
16879
16880       constraint (imm < 0 || (unsigned)imm >= et.size,
16881                   _("immediate out of range for shift"));
16882       NEON_ENCODE (IMMED, inst);
16883       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
16884     }
16885   else
16886     {
16887       enum neon_shape rs;
16888       struct neon_type_el et;
16889
16890       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16891         {
16892           rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
16893           et = neon_check_type (3, rs, N_EQK, N_SU_MVE | N_KEY, N_EQK | N_EQK);
16894         }
16895       else
16896         {
16897           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16898           et = neon_check_type (3, rs, N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
16899         }
16900
16901       if (rs == NS_QQR)
16902         {
16903           constraint (inst.operands[0].reg != inst.operands[1].reg,
16904                        _("invalid instruction shape"));
16905           if (inst.operands[2].reg == REG_SP)
16906             as_tsktsk (MVE_BAD_SP);
16907           else if (inst.operands[2].reg == REG_PC)
16908             as_tsktsk (MVE_BAD_PC);
16909
16910           inst.instruction = 0xee311ee0;
16911           inst.instruction |= (et.type == NT_unsigned) << 28;
16912           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16913           inst.instruction |= neon_logbits (et.size) << 18;
16914           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16915           inst.instruction |= inst.operands[2].reg;
16916           inst.is_neon = 1;
16917         }
16918       else
16919         {
16920           unsigned int tmp;
16921
16922           /* See note in do_neon_shl.  */
16923           tmp = inst.operands[2].reg;
16924           inst.operands[2].reg = inst.operands[1].reg;
16925           inst.operands[1].reg = tmp;
16926           NEON_ENCODE (INTEGER, inst);
16927           neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16928         }
16929     }
16930 }
16931
16932 static void
16933 do_neon_rshl (void)
16934 {
16935   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
16936    return;
16937
16938   enum neon_shape rs;
16939   struct neon_type_el et;
16940   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16941     {
16942       rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
16943       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
16944     }
16945   else
16946     {
16947       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16948       et = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_ALL | N_KEY);
16949     }
16950
16951   unsigned int tmp;
16952
16953   if (rs == NS_QQR)
16954     {
16955       if (inst.operands[2].reg == REG_PC)
16956         as_tsktsk (MVE_BAD_PC);
16957       else if (inst.operands[2].reg == REG_SP)
16958         as_tsktsk (MVE_BAD_SP);
16959
16960       constraint (inst.operands[0].reg != inst.operands[1].reg,
16961                   _("invalid instruction shape"));
16962
16963       if (inst.instruction == 0x0000510)
16964         /* We are dealing with vqrshl.  */
16965         inst.instruction = 0xee331ee0;
16966       else
16967         /* We are dealing with vrshl.  */
16968         inst.instruction = 0xee331e60;
16969
16970       inst.instruction |= (et.type == NT_unsigned) << 28;
16971       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16972       inst.instruction |= neon_logbits (et.size) << 18;
16973       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16974       inst.instruction |= inst.operands[2].reg;
16975       inst.is_neon = 1;
16976     }
16977   else
16978     {
16979       tmp = inst.operands[2].reg;
16980       inst.operands[2].reg = inst.operands[1].reg;
16981       inst.operands[1].reg = tmp;
16982       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16983     }
16984 }
16985
16986 static int
16987 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
16988 {
16989   /* Handle .I8 pseudo-instructions.  */
16990   if (size == 8)
16991     {
16992       /* Unfortunately, this will make everything apart from zero out-of-range.
16993          FIXME is this the intended semantics? There doesn't seem much point in
16994          accepting .I8 if so.  */
16995       immediate |= immediate << 8;
16996       size = 16;
16997     }
16998
16999   if (size >= 32)
17000     {
17001       if (immediate == (immediate & 0x000000ff))
17002         {
17003           *immbits = immediate;
17004           return 0x1;
17005         }
17006       else if (immediate == (immediate & 0x0000ff00))
17007         {
17008           *immbits = immediate >> 8;
17009           return 0x3;
17010         }
17011       else if (immediate == (immediate & 0x00ff0000))
17012         {
17013           *immbits = immediate >> 16;
17014           return 0x5;
17015         }
17016       else if (immediate == (immediate & 0xff000000))
17017         {
17018           *immbits = immediate >> 24;
17019           return 0x7;
17020         }
17021       if ((immediate & 0xffff) != (immediate >> 16))
17022         goto bad_immediate;
17023       immediate &= 0xffff;
17024     }
17025
17026   if (immediate == (immediate & 0x000000ff))
17027     {
17028       *immbits = immediate;
17029       return 0x9;
17030     }
17031   else if (immediate == (immediate & 0x0000ff00))
17032     {
17033       *immbits = immediate >> 8;
17034       return 0xb;
17035     }
17036
17037   bad_immediate:
17038   first_error (_("immediate value out of range"));
17039   return FAIL;
17040 }
17041
17042 static void
17043 do_neon_logic (void)
17044 {
17045   if (inst.operands[2].present && inst.operands[2].isreg)
17046     {
17047       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17048       if (rs == NS_QQQ
17049           && !check_simd_pred_availability (FALSE,
17050                                             NEON_CHECK_ARCH | NEON_CHECK_CC))
17051         return;
17052       else if (rs != NS_QQQ
17053                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
17054         first_error (BAD_FPU);
17055
17056       neon_check_type (3, rs, N_IGNORE_TYPE);
17057       /* U bit and size field were set as part of the bitmask.  */
17058       NEON_ENCODE (INTEGER, inst);
17059       neon_three_same (neon_quad (rs), 0, -1);
17060     }
17061   else
17062     {
17063       const int three_ops_form = (inst.operands[2].present
17064                                   && !inst.operands[2].isreg);
17065       const int immoperand = (three_ops_form ? 2 : 1);
17066       enum neon_shape rs = (three_ops_form
17067                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
17068                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
17069       /* Because neon_select_shape makes the second operand a copy of the first
17070          if the second operand is not present.  */
17071       if (rs == NS_QQI
17072           && !check_simd_pred_availability (FALSE,
17073                                             NEON_CHECK_ARCH | NEON_CHECK_CC))
17074         return;
17075       else if (rs != NS_QQI
17076                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
17077         first_error (BAD_FPU);
17078
17079       struct neon_type_el et;
17080       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17081         et = neon_check_type (2, rs, N_I32 | N_I16 | N_KEY, N_EQK);
17082       else
17083         et = neon_check_type (2, rs, N_I8 | N_I16 | N_I32 | N_I64 | N_F32
17084                               | N_KEY, N_EQK);
17085
17086       if (et.type == NT_invtype)
17087         return;
17088       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
17089       unsigned immbits;
17090       int cmode;
17091
17092
17093       if (three_ops_form)
17094         constraint (inst.operands[0].reg != inst.operands[1].reg,
17095                     _("first and second operands shall be the same register"));
17096
17097       NEON_ENCODE (IMMED, inst);
17098
17099       immbits = inst.operands[immoperand].imm;
17100       if (et.size == 64)
17101         {
17102           /* .i64 is a pseudo-op, so the immediate must be a repeating
17103              pattern.  */
17104           if (immbits != (inst.operands[immoperand].regisimm ?
17105                           inst.operands[immoperand].reg : 0))
17106             {
17107               /* Set immbits to an invalid constant.  */
17108               immbits = 0xdeadbeef;
17109             }
17110         }
17111
17112       switch (opcode)
17113         {
17114         case N_MNEM_vbic:
17115           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17116           break;
17117
17118         case N_MNEM_vorr:
17119           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17120           break;
17121
17122         case N_MNEM_vand:
17123           /* Pseudo-instruction for VBIC.  */
17124           neon_invert_size (&immbits, 0, et.size);
17125           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17126           break;
17127
17128         case N_MNEM_vorn:
17129           /* Pseudo-instruction for VORR.  */
17130           neon_invert_size (&immbits, 0, et.size);
17131           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
17132           break;
17133
17134         default:
17135           abort ();
17136         }
17137
17138       if (cmode == FAIL)
17139         return;
17140
17141       inst.instruction |= neon_quad (rs) << 6;
17142       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17143       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17144       inst.instruction |= cmode << 8;
17145       neon_write_immbits (immbits);
17146
17147       neon_dp_fixup (&inst);
17148     }
17149 }
17150
17151 static void
17152 do_neon_bitfield (void)
17153 {
17154   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17155   neon_check_type (3, rs, N_IGNORE_TYPE);
17156   neon_three_same (neon_quad (rs), 0, -1);
17157 }
17158
17159 static void
17160 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
17161                   unsigned destbits)
17162 {
17163   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17164   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
17165                                             types | N_KEY);
17166   if (et.type == NT_float)
17167     {
17168       NEON_ENCODE (FLOAT, inst);
17169       if (rs == NS_QQR)
17170         mve_encode_qqr (et.size, 0, 1);
17171       else
17172         neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
17173     }
17174   else
17175     {
17176       NEON_ENCODE (INTEGER, inst);
17177       if (rs == NS_QQR)
17178         mve_encode_qqr (et.size, et.type == ubit_meaning, 0);
17179       else
17180         neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
17181     }
17182 }
17183
17184
17185 static void
17186 do_neon_dyadic_if_su_d (void)
17187 {
17188   /* This version only allow D registers, but that constraint is enforced during
17189      operand parsing so we don't need to do anything extra here.  */
17190   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
17191 }
17192
17193 static void
17194 do_neon_dyadic_if_i_d (void)
17195 {
17196   /* The "untyped" case can't happen. Do this to stop the "U" bit being
17197      affected if we specify unsigned args.  */
17198   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17199 }
17200
17201 static void
17202 do_mve_vstr_vldr_QI (int size, int elsize, int load)
17203 {
17204   constraint (size < 32, BAD_ADDR_MODE);
17205   constraint (size != elsize, BAD_EL_TYPE);
17206   constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17207   constraint (!inst.operands[1].preind, BAD_ADDR_MODE);
17208   constraint (load && inst.operands[0].reg == inst.operands[1].reg,
17209               _("destination register and offset register may not be the"
17210                 " same"));
17211
17212   int imm = inst.relocs[0].exp.X_add_number;
17213   int add = 1;
17214   if (imm < 0)
17215     {
17216       add = 0;
17217       imm = -imm;
17218     }
17219   constraint ((imm % (size / 8) != 0)
17220               || imm > (0x7f << neon_logbits (size)),
17221               (size == 32) ? _("immediate must be a multiple of 4 in the"
17222                                " range of +/-[0,508]")
17223                            : _("immediate must be a multiple of 8 in the"
17224                                " range of +/-[0,1016]"));
17225   inst.instruction |= 0x11 << 24;
17226   inst.instruction |= add << 23;
17227   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17228   inst.instruction |= inst.operands[1].writeback << 21;
17229   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17230   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17231   inst.instruction |= 1 << 12;
17232   inst.instruction |= (size == 64) << 8;
17233   inst.instruction &= 0xffffff00;
17234   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17235   inst.instruction |= imm >> neon_logbits (size);
17236 }
17237
17238 static void
17239 do_mve_vstr_vldr_RQ (int size, int elsize, int load)
17240 {
17241     unsigned os = inst.operands[1].imm >> 5;
17242     constraint (os != 0 && size == 8,
17243                 _("can not shift offsets when accessing less than half-word"));
17244     constraint (os && os != neon_logbits (size),
17245                 _("shift immediate must be 1, 2 or 3 for half-word, word"
17246                   " or double-word accesses respectively"));
17247     if (inst.operands[1].reg == REG_PC)
17248       as_tsktsk (MVE_BAD_PC);
17249
17250     switch (size)
17251       {
17252       case 8:
17253         constraint (elsize >= 64, BAD_EL_TYPE);
17254         break;
17255       case 16:
17256         constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
17257         break;
17258       case 32:
17259       case 64:
17260         constraint (elsize != size, BAD_EL_TYPE);
17261         break;
17262       default:
17263         break;
17264       }
17265     constraint (inst.operands[1].writeback || !inst.operands[1].preind,
17266                 BAD_ADDR_MODE);
17267     if (load)
17268       {
17269         constraint (inst.operands[0].reg == (inst.operands[1].imm & 0x1f),
17270                     _("destination register and offset register may not be"
17271                     " the same"));
17272         constraint (size == elsize && inst.vectype.el[0].type != NT_unsigned,
17273                     BAD_EL_TYPE);
17274         constraint (inst.vectype.el[0].type != NT_unsigned
17275                     && inst.vectype.el[0].type != NT_signed, BAD_EL_TYPE);
17276         inst.instruction |= (inst.vectype.el[0].type == NT_unsigned) << 28;
17277       }
17278     else
17279       {
17280         constraint (inst.vectype.el[0].type != NT_untyped, BAD_EL_TYPE);
17281       }
17282
17283     inst.instruction |= 1 << 23;
17284     inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17285     inst.instruction |= inst.operands[1].reg << 16;
17286     inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17287     inst.instruction |= neon_logbits (elsize) << 7;
17288     inst.instruction |= HI1 (inst.operands[1].imm) << 5;
17289     inst.instruction |= LOW4 (inst.operands[1].imm);
17290     inst.instruction |= !!os;
17291 }
17292
17293 static void
17294 do_mve_vstr_vldr_RI (int size, int elsize, int load)
17295 {
17296   enum neon_el_type type = inst.vectype.el[0].type;
17297
17298   constraint (size >= 64, BAD_ADDR_MODE);
17299   switch (size)
17300     {
17301     case 16:
17302       constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
17303       break;
17304     case 32:
17305       constraint (elsize != size, BAD_EL_TYPE);
17306       break;
17307     default:
17308       break;
17309     }
17310   if (load)
17311     {
17312       constraint (elsize != size && type != NT_unsigned
17313                   && type != NT_signed, BAD_EL_TYPE);
17314     }
17315   else
17316     {
17317       constraint (elsize != size && type != NT_untyped, BAD_EL_TYPE);
17318     }
17319
17320   int imm = inst.relocs[0].exp.X_add_number;
17321   int add = 1;
17322   if (imm < 0)
17323     {
17324       add = 0;
17325       imm = -imm;
17326     }
17327
17328   if ((imm % (size / 8) != 0) || imm > (0x7f << neon_logbits (size)))
17329     {
17330       switch (size)
17331         {
17332         case 8:
17333           constraint (1, _("immediate must be in the range of +/-[0,127]"));
17334           break;
17335         case 16:
17336           constraint (1, _("immediate must be a multiple of 2 in the"
17337                            " range of +/-[0,254]"));
17338           break;
17339         case 32:
17340           constraint (1, _("immediate must be a multiple of 4 in the"
17341                            " range of +/-[0,508]"));
17342           break;
17343         }
17344     }
17345
17346   if (size != elsize)
17347     {
17348       constraint (inst.operands[1].reg > 7, BAD_HIREG);
17349       constraint (inst.operands[0].reg > 14,
17350                   _("MVE vector register in the range [Q0..Q7] expected"));
17351       inst.instruction |= (load && type == NT_unsigned) << 28;
17352       inst.instruction |= (size == 16) << 19;
17353       inst.instruction |= neon_logbits (elsize) << 7;
17354     }
17355   else
17356     {
17357       if (inst.operands[1].reg == REG_PC)
17358         as_tsktsk (MVE_BAD_PC);
17359       else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
17360         as_tsktsk (MVE_BAD_SP);
17361       inst.instruction |= 1 << 12;
17362       inst.instruction |= neon_logbits (size) << 7;
17363     }
17364   inst.instruction |= inst.operands[1].preind << 24;
17365   inst.instruction |= add << 23;
17366   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17367   inst.instruction |= inst.operands[1].writeback << 21;
17368   inst.instruction |= inst.operands[1].reg << 16;
17369   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17370   inst.instruction &= 0xffffff80;
17371   inst.instruction |= imm >> neon_logbits (size);
17372
17373 }
17374
17375 static void
17376 do_mve_vstr_vldr (void)
17377 {
17378   unsigned size;
17379   int load = 0;
17380
17381   if (inst.cond > COND_ALWAYS)
17382     inst.pred_insn_type = INSIDE_VPT_INSN;
17383   else
17384     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17385
17386   switch (inst.instruction)
17387     {
17388     default:
17389       gas_assert (0);
17390       break;
17391     case M_MNEM_vldrb:
17392       load = 1;
17393       /* fall through.  */
17394     case M_MNEM_vstrb:
17395       size = 8;
17396       break;
17397     case M_MNEM_vldrh:
17398       load = 1;
17399       /* fall through.  */
17400     case M_MNEM_vstrh:
17401       size = 16;
17402       break;
17403     case M_MNEM_vldrw:
17404       load = 1;
17405       /* fall through.  */
17406     case M_MNEM_vstrw:
17407       size = 32;
17408       break;
17409     case M_MNEM_vldrd:
17410       load = 1;
17411       /* fall through.  */
17412     case M_MNEM_vstrd:
17413       size = 64;
17414       break;
17415     }
17416   unsigned elsize = inst.vectype.el[0].size;
17417
17418   if (inst.operands[1].isquad)
17419     {
17420       /* We are dealing with [Q, imm]{!} cases.  */
17421       do_mve_vstr_vldr_QI (size, elsize, load);
17422     }
17423   else
17424     {
17425       if (inst.operands[1].immisreg == 2)
17426         {
17427           /* We are dealing with [R, Q, {UXTW #os}] cases.  */
17428           do_mve_vstr_vldr_RQ (size, elsize, load);
17429         }
17430       else if (!inst.operands[1].immisreg)
17431         {
17432           /* We are dealing with [R, Imm]{!}/[R], Imm cases.  */
17433           do_mve_vstr_vldr_RI (size, elsize, load);
17434         }
17435       else
17436         constraint (1, BAD_ADDR_MODE);
17437     }
17438
17439   inst.is_neon = 1;
17440 }
17441
17442 static void
17443 do_mve_vst_vld (void)
17444 {
17445   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17446     return;
17447
17448   constraint (!inst.operands[1].preind || inst.relocs[0].exp.X_add_symbol != 0
17449               || inst.relocs[0].exp.X_add_number != 0
17450               || inst.operands[1].immisreg != 0,
17451               BAD_ADDR_MODE);
17452   constraint (inst.vectype.el[0].size > 32, BAD_EL_TYPE);
17453   if (inst.operands[1].reg == REG_PC)
17454     as_tsktsk (MVE_BAD_PC);
17455   else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
17456     as_tsktsk (MVE_BAD_SP);
17457
17458
17459   /* These instructions are one of the "exceptions" mentioned in
17460      handle_pred_state.  They are MVE instructions that are not VPT compatible
17461      and do not accept a VPT code, thus appending such a code is a syntax
17462      error.  */
17463   if (inst.cond > COND_ALWAYS)
17464     first_error (BAD_SYNTAX);
17465   /* If we append a scalar condition code we can set this to
17466      MVE_OUTSIDE_PRED_INSN as it will also lead to a syntax error.  */
17467   else if (inst.cond < COND_ALWAYS)
17468     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17469   else
17470     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
17471
17472   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17473   inst.instruction |= inst.operands[1].writeback << 21;
17474   inst.instruction |= inst.operands[1].reg << 16;
17475   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17476   inst.instruction |= neon_logbits (inst.vectype.el[0].size) << 7;
17477   inst.is_neon = 1;
17478 }
17479
17480 static void
17481 do_mve_vaddlv (void)
17482 {
17483   enum neon_shape rs = neon_select_shape (NS_RRQ, NS_NULL);
17484   struct neon_type_el et
17485     = neon_check_type (3, rs, N_EQK, N_EQK, N_S32 | N_U32 | N_KEY);
17486
17487   if (et.type == NT_invtype)
17488     first_error (BAD_EL_TYPE);
17489
17490   if (inst.cond > COND_ALWAYS)
17491     inst.pred_insn_type = INSIDE_VPT_INSN;
17492   else
17493     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17494
17495   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
17496
17497   inst.instruction |= (et.type == NT_unsigned) << 28;
17498   inst.instruction |= inst.operands[1].reg << 19;
17499   inst.instruction |= inst.operands[0].reg << 12;
17500   inst.instruction |= inst.operands[2].reg;
17501   inst.is_neon = 1;
17502 }
17503
17504 static void
17505 do_neon_dyadic_if_su (void)
17506 {
17507   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17508   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
17509                                             N_SUF_32 | N_KEY);
17510
17511   constraint ((inst.instruction == ((unsigned) N_MNEM_vmax)
17512                || inst.instruction == ((unsigned) N_MNEM_vmin))
17513               && et.type == NT_float
17514               && !ARM_CPU_HAS_FEATURE (cpu_variant,fpu_neon_ext_v1), BAD_FPU);
17515
17516   if (!check_simd_pred_availability (et.type == NT_float,
17517                                      NEON_CHECK_ARCH | NEON_CHECK_CC))
17518     return;
17519
17520   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
17521 }
17522
17523 static void
17524 do_neon_addsub_if_i (void)
17525 {
17526   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
17527       && try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
17528     return;
17529
17530   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
17531   struct neon_type_el et = neon_check_type (3, rs, N_EQK,
17532                                             N_EQK, N_IF_32 | N_I64 | N_KEY);
17533
17534   constraint (rs == NS_QQR && et.size == 64, BAD_FPU);
17535   /* If we are parsing Q registers and the element types match MVE, which NEON
17536      also supports, then we must check whether this is an instruction that can
17537      be used by both MVE/NEON.  This distinction can be made based on whether
17538      they are predicated or not.  */
17539   if ((rs == NS_QQQ || rs == NS_QQR) && et.size != 64)
17540     {
17541       if (!check_simd_pred_availability (et.type == NT_float,
17542                                          NEON_CHECK_ARCH | NEON_CHECK_CC))
17543         return;
17544     }
17545   else
17546     {
17547       /* If they are either in a D register or are using an unsupported.  */
17548       if (rs != NS_QQR
17549           && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17550         return;
17551     }
17552
17553   /* The "untyped" case can't happen. Do this to stop the "U" bit being
17554      affected if we specify unsigned args.  */
17555   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
17556 }
17557
17558 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
17559    result to be:
17560      V<op> A,B     (A is operand 0, B is operand 2)
17561    to mean:
17562      V<op> A,B,A
17563    not:
17564      V<op> A,B,B
17565    so handle that case specially.  */
17566
17567 static void
17568 neon_exchange_operands (void)
17569 {
17570   if (inst.operands[1].present)
17571     {
17572       void *scratch = xmalloc (sizeof (inst.operands[0]));
17573
17574       /* Swap operands[1] and operands[2].  */
17575       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
17576       inst.operands[1] = inst.operands[2];
17577       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
17578       free (scratch);
17579     }
17580   else
17581     {
17582       inst.operands[1] = inst.operands[2];
17583       inst.operands[2] = inst.operands[0];
17584     }
17585 }
17586
17587 static void
17588 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
17589 {
17590   if (inst.operands[2].isreg)
17591     {
17592       if (invert)
17593         neon_exchange_operands ();
17594       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
17595     }
17596   else
17597     {
17598       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17599       struct neon_type_el et = neon_check_type (2, rs,
17600         N_EQK | N_SIZ, immtypes | N_KEY);
17601
17602       NEON_ENCODE (IMMED, inst);
17603       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17604       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17605       inst.instruction |= LOW4 (inst.operands[1].reg);
17606       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17607       inst.instruction |= neon_quad (rs) << 6;
17608       inst.instruction |= (et.type == NT_float) << 10;
17609       inst.instruction |= neon_logbits (et.size) << 18;
17610
17611       neon_dp_fixup (&inst);
17612     }
17613 }
17614
17615 static void
17616 do_neon_cmp (void)
17617 {
17618   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
17619 }
17620
17621 static void
17622 do_neon_cmp_inv (void)
17623 {
17624   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
17625 }
17626
17627 static void
17628 do_neon_ceq (void)
17629 {
17630   neon_compare (N_IF_32, N_IF_32, FALSE);
17631 }
17632
17633 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
17634    scalars, which are encoded in 5 bits, M : Rm.
17635    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
17636    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
17637    index in M.
17638
17639    Dot Product instructions are similar to multiply instructions except elsize
17640    should always be 32.
17641
17642    This function translates SCALAR, which is GAS's internal encoding of indexed
17643    scalar register, to raw encoding.  There is also register and index range
17644    check based on ELSIZE.  */
17645
17646 static unsigned
17647 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
17648 {
17649   unsigned regno = NEON_SCALAR_REG (scalar);
17650   unsigned elno = NEON_SCALAR_INDEX (scalar);
17651
17652   switch (elsize)
17653     {
17654     case 16:
17655       if (regno > 7 || elno > 3)
17656         goto bad_scalar;
17657       return regno | (elno << 3);
17658
17659     case 32:
17660       if (regno > 15 || elno > 1)
17661         goto bad_scalar;
17662       return regno | (elno << 4);
17663
17664     default:
17665     bad_scalar:
17666       first_error (_("scalar out of range for multiply instruction"));
17667     }
17668
17669   return 0;
17670 }
17671
17672 /* Encode multiply / multiply-accumulate scalar instructions.  */
17673
17674 static void
17675 neon_mul_mac (struct neon_type_el et, int ubit)
17676 {
17677   unsigned scalar;
17678
17679   /* Give a more helpful error message if we have an invalid type.  */
17680   if (et.type == NT_invtype)
17681     return;
17682
17683   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
17684   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17685   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17686   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17687   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17688   inst.instruction |= LOW4 (scalar);
17689   inst.instruction |= HI1 (scalar) << 5;
17690   inst.instruction |= (et.type == NT_float) << 8;
17691   inst.instruction |= neon_logbits (et.size) << 20;
17692   inst.instruction |= (ubit != 0) << 24;
17693
17694   neon_dp_fixup (&inst);
17695 }
17696
17697 static void
17698 do_neon_mac_maybe_scalar (void)
17699 {
17700   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
17701     return;
17702
17703   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17704     return;
17705
17706   if (inst.operands[2].isscalar)
17707     {
17708       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17709       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17710       struct neon_type_el et = neon_check_type (3, rs,
17711         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
17712       NEON_ENCODE (SCALAR, inst);
17713       neon_mul_mac (et, neon_quad (rs));
17714     }
17715   else if (!inst.operands[2].isvec)
17716     {
17717       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17718
17719       enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17720       neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
17721
17722       neon_dyadic_misc (NT_unsigned, N_SU_MVE, 0);
17723     }
17724   else
17725     {
17726       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17727       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
17728          affected if we specify unsigned args.  */
17729       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17730     }
17731 }
17732
17733 static void
17734 do_neon_fmac (void)
17735 {
17736   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_fma)
17737       && try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
17738     return;
17739
17740   if (!check_simd_pred_availability (TRUE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17741     return;
17742
17743   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
17744     {
17745       enum neon_shape rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
17746       struct neon_type_el et = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK,
17747                                                 N_EQK);
17748
17749       if (rs == NS_QQR)
17750         {
17751           if (inst.operands[2].reg == REG_SP)
17752             as_tsktsk (MVE_BAD_SP);
17753           else if (inst.operands[2].reg == REG_PC)
17754             as_tsktsk (MVE_BAD_PC);
17755
17756           inst.instruction = 0xee310e40;
17757           inst.instruction |= (et.size == 16) << 28;
17758           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17759           inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17760           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17761           inst.instruction |= HI1 (inst.operands[1].reg) << 6;
17762           inst.instruction |= inst.operands[2].reg;
17763           inst.is_neon = 1;
17764           return;
17765         }
17766     }
17767   else
17768     {
17769       constraint (!inst.operands[2].isvec, BAD_FPU);
17770     }
17771
17772   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
17773 }
17774
17775 static void
17776 do_neon_tst (void)
17777 {
17778   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17779   struct neon_type_el et = neon_check_type (3, rs,
17780     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
17781   neon_three_same (neon_quad (rs), 0, et.size);
17782 }
17783
17784 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
17785    same types as the MAC equivalents. The polynomial type for this instruction
17786    is encoded the same as the integer type.  */
17787
17788 static void
17789 do_neon_mul (void)
17790 {
17791   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
17792     return;
17793
17794   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
17795     return;
17796
17797   if (inst.operands[2].isscalar)
17798     {
17799       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17800       do_neon_mac_maybe_scalar ();
17801     }
17802   else
17803     {
17804       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17805         {
17806           enum neon_shape rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
17807           struct neon_type_el et
17808             = neon_check_type (3, rs, N_EQK, N_EQK, N_I_MVE | N_F_MVE | N_KEY);
17809           if (et.type == NT_float)
17810             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
17811                         BAD_FPU);
17812
17813           neon_dyadic_misc (NT_float, N_I_MVE | N_F_MVE, 0);
17814         }
17815       else
17816         {
17817           constraint (!inst.operands[2].isvec, BAD_FPU);
17818           neon_dyadic_misc (NT_poly,
17819                             N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
17820         }
17821     }
17822 }
17823
17824 static void
17825 do_neon_qdmulh (void)
17826 {
17827   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
17828    return;
17829
17830   if (inst.operands[2].isscalar)
17831     {
17832       constraint (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
17833       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17834       struct neon_type_el et = neon_check_type (3, rs,
17835         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17836       NEON_ENCODE (SCALAR, inst);
17837       neon_mul_mac (et, neon_quad (rs));
17838     }
17839   else
17840     {
17841       enum neon_shape rs;
17842       struct neon_type_el et;
17843       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17844         {
17845           rs = neon_select_shape (NS_QQR, NS_QQQ, NS_NULL);
17846           et = neon_check_type (3, rs,
17847             N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17848         }
17849       else
17850         {
17851           rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17852           et = neon_check_type (3, rs,
17853             N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17854         }
17855
17856       NEON_ENCODE (INTEGER, inst);
17857       if (rs == NS_QQR)
17858         mve_encode_qqr (et.size, 0, 0);
17859       else
17860         /* The U bit (rounding) comes from bit mask.  */
17861         neon_three_same (neon_quad (rs), 0, et.size);
17862     }
17863 }
17864
17865 static void
17866 do_mve_vaddv (void)
17867 {
17868   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
17869   struct neon_type_el et
17870     = neon_check_type (2, rs, N_EQK,  N_SU_32 | N_KEY);
17871
17872   if (et.type == NT_invtype)
17873     first_error (BAD_EL_TYPE);
17874
17875   if (inst.cond > COND_ALWAYS)
17876     inst.pred_insn_type = INSIDE_VPT_INSN;
17877   else
17878     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17879
17880   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
17881
17882   mve_encode_rq (et.type == NT_unsigned, et.size);
17883 }
17884
17885 static void
17886 do_mve_vhcadd (void)
17887 {
17888   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
17889   struct neon_type_el et
17890     = neon_check_type (3, rs, N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17891
17892   if (inst.cond > COND_ALWAYS)
17893     inst.pred_insn_type = INSIDE_VPT_INSN;
17894   else
17895     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17896
17897   unsigned rot = inst.relocs[0].exp.X_add_number;
17898   constraint (rot != 90 && rot != 270, _("immediate out of range"));
17899
17900   if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
17901     as_tsktsk (_("Warning: 32-bit element size and same first and third "
17902                  "operand makes instruction UNPREDICTABLE"));
17903
17904   mve_encode_qqq (0, et.size);
17905   inst.instruction |= (rot == 270) << 12;
17906   inst.is_neon = 1;
17907 }
17908
17909 static void
17910 do_mve_vqdmull (void)
17911 {
17912   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_QQR, NS_NULL);
17913   struct neon_type_el et
17914     = neon_check_type (3, rs, N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17915
17916   if (et.size == 32
17917       && (inst.operands[0].reg == inst.operands[1].reg
17918           || (rs == NS_QQQ && inst.operands[0].reg == inst.operands[2].reg)))
17919     as_tsktsk (BAD_MVE_SRCDEST);
17920
17921   if (inst.cond > COND_ALWAYS)
17922     inst.pred_insn_type = INSIDE_VPT_INSN;
17923   else
17924     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17925
17926   if (rs == NS_QQQ)
17927     {
17928       mve_encode_qqq (et.size == 32, 64);
17929       inst.instruction |= 1;
17930     }
17931   else
17932     {
17933       mve_encode_qqr (64, et.size == 32, 0);
17934       inst.instruction |= 0x3 << 5;
17935     }
17936 }
17937
17938 static void
17939 do_mve_vadc (void)
17940 {
17941   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17942   struct neon_type_el et
17943     = neon_check_type (3, rs, N_KEY | N_I32, N_EQK, N_EQK);
17944
17945   if (et.type == NT_invtype)
17946     first_error (BAD_EL_TYPE);
17947
17948   if (inst.cond > COND_ALWAYS)
17949     inst.pred_insn_type = INSIDE_VPT_INSN;
17950   else
17951     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17952
17953   mve_encode_qqq (0, 64);
17954 }
17955
17956 static void
17957 do_mve_vbrsr (void)
17958 {
17959   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
17960   struct neon_type_el et
17961     = neon_check_type (3, rs, N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
17962
17963   if (inst.cond > COND_ALWAYS)
17964     inst.pred_insn_type = INSIDE_VPT_INSN;
17965   else
17966     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17967
17968   mve_encode_qqr (et.size, 0, 0);
17969 }
17970
17971 static void
17972 do_mve_vsbc (void)
17973 {
17974   neon_check_type (3, NS_QQQ, N_EQK, N_EQK, N_I32 | N_KEY);
17975
17976   if (inst.cond > COND_ALWAYS)
17977     inst.pred_insn_type = INSIDE_VPT_INSN;
17978   else
17979     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17980
17981   mve_encode_qqq (1, 64);
17982 }
17983
17984 static void
17985 do_mve_vmulh (void)
17986 {
17987   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
17988   struct neon_type_el et
17989     = neon_check_type (3, rs, N_EQK, N_EQK, N_SU_MVE | N_KEY);
17990
17991   if (inst.cond > COND_ALWAYS)
17992     inst.pred_insn_type = INSIDE_VPT_INSN;
17993   else
17994     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17995
17996   mve_encode_qqq (et.type == NT_unsigned, et.size);
17997 }
17998
17999 static void
18000 do_mve_vqdmlah (void)
18001 {
18002   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
18003   struct neon_type_el et
18004     = neon_check_type (3, rs, N_EQK, N_EQK, N_S_32 | N_KEY);
18005
18006   if (inst.cond > COND_ALWAYS)
18007     inst.pred_insn_type = INSIDE_VPT_INSN;
18008   else
18009     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18010
18011   mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
18012 }
18013
18014 static void
18015 do_mve_vqdmladh (void)
18016 {
18017   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
18018   struct neon_type_el et
18019     = neon_check_type (3, rs, N_EQK, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18020
18021   if (inst.cond > COND_ALWAYS)
18022     inst.pred_insn_type = INSIDE_VPT_INSN;
18023   else
18024     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18025
18026   mve_encode_qqq (0, et.size);
18027 }
18028
18029
18030 static void
18031 do_mve_vmull (void)
18032 {
18033
18034   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_DDS,
18035                                           NS_QQS, NS_QQQ, NS_QQR, NS_NULL);
18036   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
18037       && inst.cond == COND_ALWAYS
18038       && ((unsigned)inst.instruction) == M_MNEM_vmullt)
18039     {
18040       if (rs == NS_QQQ)
18041         {
18042
18043           struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
18044                                                     N_SUF_32 | N_F64 | N_P8
18045                                                     | N_P16 | N_I_MVE | N_KEY);
18046           if (((et.type == NT_poly) && et.size == 8
18047                && ARM_CPU_IS_ANY (cpu_variant))
18048               || (et.type == NT_integer) || (et.type == NT_float))
18049             goto neon_vmul;
18050         }
18051       else
18052         goto neon_vmul;
18053     }
18054
18055   constraint (rs != NS_QQQ, BAD_FPU);
18056   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
18057                                             N_SU_32 | N_P8 | N_P16 | N_KEY);
18058
18059   /* We are dealing with MVE's vmullt.  */
18060   if (et.size == 32
18061       && (inst.operands[0].reg == inst.operands[1].reg
18062           || inst.operands[0].reg == inst.operands[2].reg))
18063     as_tsktsk (BAD_MVE_SRCDEST);
18064
18065   if (inst.cond > COND_ALWAYS)
18066     inst.pred_insn_type = INSIDE_VPT_INSN;
18067   else
18068     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18069
18070   if (et.type == NT_poly)
18071     mve_encode_qqq (neon_logbits (et.size), 64);
18072   else
18073     mve_encode_qqq (et.type == NT_unsigned, et.size);
18074
18075   return;
18076
18077 neon_vmul:
18078   inst.instruction = N_MNEM_vmul;
18079   inst.cond = 0xb;
18080   if (thumb_mode)
18081     inst.pred_insn_type = INSIDE_IT_INSN;
18082   do_neon_mul ();
18083 }
18084
18085 static void
18086 do_mve_vabav (void)
18087 {
18088   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
18089
18090   if (rs == NS_NULL)
18091     return;
18092
18093   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18094     return;
18095
18096   struct neon_type_el et = neon_check_type (2, NS_NULL, N_EQK, N_KEY | N_S8
18097                                             | N_S16 | N_S32 | N_U8 | N_U16
18098                                             | N_U32);
18099
18100   if (inst.cond > COND_ALWAYS)
18101     inst.pred_insn_type = INSIDE_VPT_INSN;
18102   else
18103     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18104
18105   mve_encode_rqq (et.type == NT_unsigned, et.size);
18106 }
18107
18108 static void
18109 do_mve_vmladav (void)
18110 {
18111   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
18112   struct neon_type_el et = neon_check_type (3, rs,
18113                                             N_EQK, N_EQK, N_SU_MVE | N_KEY);
18114
18115   if (et.type == NT_unsigned
18116       && (inst.instruction == M_MNEM_vmladavx
18117           || inst.instruction == M_MNEM_vmladavax
18118           || inst.instruction == M_MNEM_vmlsdav
18119           || inst.instruction == M_MNEM_vmlsdava
18120           || inst.instruction == M_MNEM_vmlsdavx
18121           || inst.instruction == M_MNEM_vmlsdavax))
18122     first_error (BAD_SIMD_TYPE);
18123
18124   constraint (inst.operands[2].reg > 14,
18125               _("MVE vector register in the range [Q0..Q7] expected"));
18126
18127   if (inst.cond > COND_ALWAYS)
18128     inst.pred_insn_type = INSIDE_VPT_INSN;
18129   else
18130     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18131
18132   if (inst.instruction == M_MNEM_vmlsdav
18133       || inst.instruction == M_MNEM_vmlsdava
18134       || inst.instruction == M_MNEM_vmlsdavx
18135       || inst.instruction == M_MNEM_vmlsdavax)
18136     inst.instruction |= (et.size == 8) << 28;
18137   else
18138     inst.instruction |= (et.size == 8) << 8;
18139
18140   mve_encode_rqq (et.type == NT_unsigned, 64);
18141   inst.instruction |= (et.size == 32) << 16;
18142 }
18143
18144 static void
18145 do_mve_vmlaldav (void)
18146 {
18147   enum neon_shape rs = neon_select_shape (NS_RRQQ, NS_NULL);
18148   struct neon_type_el et
18149     = neon_check_type (4, rs, N_EQK, N_EQK, N_EQK,
18150                        N_S16 | N_S32 | N_U16 | N_U32 | N_KEY);
18151
18152   if (et.type == NT_unsigned
18153       && (inst.instruction == M_MNEM_vmlsldav
18154           || inst.instruction == M_MNEM_vmlsldava
18155           || inst.instruction == M_MNEM_vmlsldavx
18156           || inst.instruction == M_MNEM_vmlsldavax))
18157     first_error (BAD_SIMD_TYPE);
18158
18159   if (inst.cond > COND_ALWAYS)
18160     inst.pred_insn_type = INSIDE_VPT_INSN;
18161   else
18162     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18163
18164   mve_encode_rrqq (et.type == NT_unsigned, et.size);
18165 }
18166
18167 static void
18168 do_mve_vrmlaldavh (void)
18169 {
18170   struct neon_type_el et;
18171   if (inst.instruction == M_MNEM_vrmlsldavh
18172      || inst.instruction == M_MNEM_vrmlsldavha
18173      || inst.instruction == M_MNEM_vrmlsldavhx
18174      || inst.instruction == M_MNEM_vrmlsldavhax)
18175     {
18176       et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK, N_S32 | N_KEY);
18177       if (inst.operands[1].reg == REG_SP)
18178         as_tsktsk (MVE_BAD_SP);
18179     }
18180   else
18181     {
18182       if (inst.instruction == M_MNEM_vrmlaldavhx
18183           || inst.instruction == M_MNEM_vrmlaldavhax)
18184         et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK, N_S32 | N_KEY);
18185       else
18186         et = neon_check_type (4, NS_RRQQ, N_EQK, N_EQK, N_EQK,
18187                               N_U32 | N_S32 | N_KEY);
18188       /* vrmlaldavh's encoding with SP as the second, odd, GPR operand may alias
18189          with vmax/min instructions, making the use of SP in assembly really
18190          nonsensical, so instead of issuing a warning like we do for other uses
18191          of SP for the odd register operand we error out.  */
18192       constraint (inst.operands[1].reg == REG_SP, BAD_SP);
18193     }
18194
18195   /* Make sure we still check the second operand is an odd one and that PC is
18196      disallowed.  This because we are parsing for any GPR operand, to be able
18197      to distinguish between giving a warning or an error for SP as described
18198      above.  */
18199   constraint ((inst.operands[1].reg % 2) != 1, BAD_EVEN);
18200   constraint (inst.operands[1].reg == REG_PC, BAD_PC);
18201
18202   if (inst.cond > COND_ALWAYS)
18203     inst.pred_insn_type = INSIDE_VPT_INSN;
18204   else
18205     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18206
18207   mve_encode_rrqq (et.type == NT_unsigned, 0);
18208 }
18209
18210
18211 static void
18212 do_mve_vmaxnmv (void)
18213 {
18214   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
18215   struct neon_type_el et
18216     = neon_check_type (2, rs, N_EQK, N_F_MVE | N_KEY);
18217
18218   if (inst.cond > COND_ALWAYS)
18219     inst.pred_insn_type = INSIDE_VPT_INSN;
18220   else
18221     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18222
18223   if (inst.operands[0].reg == REG_SP)
18224     as_tsktsk (MVE_BAD_SP);
18225   else if (inst.operands[0].reg == REG_PC)
18226     as_tsktsk (MVE_BAD_PC);
18227
18228   mve_encode_rq (et.size == 16, 64);
18229 }
18230
18231 static void
18232 do_mve_vmaxv (void)
18233 {
18234   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
18235   struct neon_type_el et;
18236
18237   if (inst.instruction == M_MNEM_vmaxv || inst.instruction == M_MNEM_vminv)
18238     et = neon_check_type (2, rs, N_EQK, N_SU_MVE | N_KEY);
18239   else
18240     et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18241
18242   if (inst.cond > COND_ALWAYS)
18243     inst.pred_insn_type = INSIDE_VPT_INSN;
18244   else
18245     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18246
18247   if (inst.operands[0].reg == REG_SP)
18248     as_tsktsk (MVE_BAD_SP);
18249   else if (inst.operands[0].reg == REG_PC)
18250     as_tsktsk (MVE_BAD_PC);
18251
18252   mve_encode_rq (et.type == NT_unsigned, et.size);
18253 }
18254
18255
18256 static void
18257 do_neon_qrdmlah (void)
18258 {
18259   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18260    return;
18261   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18262     {
18263       /* Check we're on the correct architecture.  */
18264       if (!mark_feature_used (&fpu_neon_ext_armv8))
18265         inst.error
18266           = _("instruction form not available on this architecture.");
18267       else if (!mark_feature_used (&fpu_neon_ext_v8_1))
18268         {
18269           as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
18270           record_feature_use (&fpu_neon_ext_v8_1);
18271         }
18272         if (inst.operands[2].isscalar)
18273           {
18274             enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
18275             struct neon_type_el et = neon_check_type (3, rs,
18276               N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
18277             NEON_ENCODE (SCALAR, inst);
18278             neon_mul_mac (et, neon_quad (rs));
18279           }
18280         else
18281           {
18282             enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18283             struct neon_type_el et = neon_check_type (3, rs,
18284               N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
18285             NEON_ENCODE (INTEGER, inst);
18286             /* The U bit (rounding) comes from bit mask.  */
18287             neon_three_same (neon_quad (rs), 0, et.size);
18288           }
18289     }
18290   else
18291     {
18292       enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
18293       struct neon_type_el et
18294         = neon_check_type (3, rs, N_EQK, N_EQK, N_S_32 | N_KEY);
18295
18296       NEON_ENCODE (INTEGER, inst);
18297       mve_encode_qqr (et.size, et.type == NT_unsigned, 0);
18298     }
18299 }
18300
18301 static void
18302 do_neon_fcmp_absolute (void)
18303 {
18304   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18305   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
18306                                             N_F_16_32 | N_KEY);
18307   /* Size field comes from bit mask.  */
18308   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
18309 }
18310
18311 static void
18312 do_neon_fcmp_absolute_inv (void)
18313 {
18314   neon_exchange_operands ();
18315   do_neon_fcmp_absolute ();
18316 }
18317
18318 static void
18319 do_neon_step (void)
18320 {
18321   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18322   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
18323                                             N_F_16_32 | N_KEY);
18324   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
18325 }
18326
18327 static void
18328 do_neon_abs_neg (void)
18329 {
18330   enum neon_shape rs;
18331   struct neon_type_el et;
18332
18333   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
18334     return;
18335
18336   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18337   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
18338
18339   if (!check_simd_pred_availability (et.type == NT_float,
18340                                      NEON_CHECK_ARCH | NEON_CHECK_CC))
18341     return;
18342
18343   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18344   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18345   inst.instruction |= LOW4 (inst.operands[1].reg);
18346   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18347   inst.instruction |= neon_quad (rs) << 6;
18348   inst.instruction |= (et.type == NT_float) << 10;
18349   inst.instruction |= neon_logbits (et.size) << 18;
18350
18351   neon_dp_fixup (&inst);
18352 }
18353
18354 static void
18355 do_neon_sli (void)
18356 {
18357   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18358     return;
18359
18360   enum neon_shape rs;
18361   struct neon_type_el et;
18362   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18363     {
18364       rs = neon_select_shape (NS_QQI, NS_NULL);
18365       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_KEY);
18366     }
18367   else
18368     {
18369       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18370       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18371     }
18372
18373
18374   int imm = inst.operands[2].imm;
18375   constraint (imm < 0 || (unsigned)imm >= et.size,
18376               _("immediate out of range for insert"));
18377   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
18378 }
18379
18380 static void
18381 do_neon_sri (void)
18382 {
18383   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18384     return;
18385
18386   enum neon_shape rs;
18387   struct neon_type_el et;
18388   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18389     {
18390       rs = neon_select_shape (NS_QQI, NS_NULL);
18391       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_KEY);
18392     }
18393   else
18394     {
18395       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18396       et = neon_check_type (2, rs, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18397     }
18398
18399   int imm = inst.operands[2].imm;
18400   constraint (imm < 1 || (unsigned)imm > et.size,
18401               _("immediate out of range for insert"));
18402   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
18403 }
18404
18405 static void
18406 do_neon_qshlu_imm (void)
18407 {
18408   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
18409     return;
18410
18411   enum neon_shape rs;
18412   struct neon_type_el et;
18413   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18414     {
18415       rs = neon_select_shape (NS_QQI, NS_NULL);
18416       et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
18417     }
18418   else
18419     {
18420       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18421       et = neon_check_type (2, rs, N_EQK | N_UNS,
18422                             N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
18423     }
18424
18425   int imm = inst.operands[2].imm;
18426   constraint (imm < 0 || (unsigned)imm >= et.size,
18427               _("immediate out of range for shift"));
18428   /* Only encodes the 'U present' variant of the instruction.
18429      In this case, signed types have OP (bit 8) set to 0.
18430      Unsigned types have OP set to 1.  */
18431   inst.instruction |= (et.type == NT_unsigned) << 8;
18432   /* The rest of the bits are the same as other immediate shifts.  */
18433   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
18434 }
18435
18436 static void
18437 do_neon_qmovn (void)
18438 {
18439   struct neon_type_el et = neon_check_type (2, NS_DQ,
18440     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
18441   /* Saturating move where operands can be signed or unsigned, and the
18442      destination has the same signedness.  */
18443   NEON_ENCODE (INTEGER, inst);
18444   if (et.type == NT_unsigned)
18445     inst.instruction |= 0xc0;
18446   else
18447     inst.instruction |= 0x80;
18448   neon_two_same (0, 1, et.size / 2);
18449 }
18450
18451 static void
18452 do_neon_qmovun (void)
18453 {
18454   struct neon_type_el et = neon_check_type (2, NS_DQ,
18455     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
18456   /* Saturating move with unsigned results. Operands must be signed.  */
18457   NEON_ENCODE (INTEGER, inst);
18458   neon_two_same (0, 1, et.size / 2);
18459 }
18460
18461 static void
18462 do_neon_rshift_sat_narrow (void)
18463 {
18464   /* FIXME: Types for narrowing. If operands are signed, results can be signed
18465      or unsigned. If operands are unsigned, results must also be unsigned.  */
18466   struct neon_type_el et = neon_check_type (2, NS_DQI,
18467     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
18468   int imm = inst.operands[2].imm;
18469   /* This gets the bounds check, size encoding and immediate bits calculation
18470      right.  */
18471   et.size /= 2;
18472
18473   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
18474      VQMOVN.I<size> <Dd>, <Qm>.  */
18475   if (imm == 0)
18476     {
18477       inst.operands[2].present = 0;
18478       inst.instruction = N_MNEM_vqmovn;
18479       do_neon_qmovn ();
18480       return;
18481     }
18482
18483   constraint (imm < 1 || (unsigned)imm > et.size,
18484               _("immediate out of range"));
18485   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
18486 }
18487
18488 static void
18489 do_neon_rshift_sat_narrow_u (void)
18490 {
18491   /* FIXME: Types for narrowing. If operands are signed, results can be signed
18492      or unsigned. If operands are unsigned, results must also be unsigned.  */
18493   struct neon_type_el et = neon_check_type (2, NS_DQI,
18494     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
18495   int imm = inst.operands[2].imm;
18496   /* This gets the bounds check, size encoding and immediate bits calculation
18497      right.  */
18498   et.size /= 2;
18499
18500   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
18501      VQMOVUN.I<size> <Dd>, <Qm>.  */
18502   if (imm == 0)
18503     {
18504       inst.operands[2].present = 0;
18505       inst.instruction = N_MNEM_vqmovun;
18506       do_neon_qmovun ();
18507       return;
18508     }
18509
18510   constraint (imm < 1 || (unsigned)imm > et.size,
18511               _("immediate out of range"));
18512   /* FIXME: The manual is kind of unclear about what value U should have in
18513      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
18514      must be 1.  */
18515   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
18516 }
18517
18518 static void
18519 do_neon_movn (void)
18520 {
18521   struct neon_type_el et = neon_check_type (2, NS_DQ,
18522     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
18523   NEON_ENCODE (INTEGER, inst);
18524   neon_two_same (0, 1, et.size / 2);
18525 }
18526
18527 static void
18528 do_neon_rshift_narrow (void)
18529 {
18530   struct neon_type_el et = neon_check_type (2, NS_DQI,
18531     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
18532   int imm = inst.operands[2].imm;
18533   /* This gets the bounds check, size encoding and immediate bits calculation
18534      right.  */
18535   et.size /= 2;
18536
18537   /* If immediate is zero then we are a pseudo-instruction for
18538      VMOVN.I<size> <Dd>, <Qm>  */
18539   if (imm == 0)
18540     {
18541       inst.operands[2].present = 0;
18542       inst.instruction = N_MNEM_vmovn;
18543       do_neon_movn ();
18544       return;
18545     }
18546
18547   constraint (imm < 1 || (unsigned)imm > et.size,
18548               _("immediate out of range for narrowing operation"));
18549   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
18550 }
18551
18552 static void
18553 do_neon_shll (void)
18554 {
18555   /* FIXME: Type checking when lengthening.  */
18556   struct neon_type_el et = neon_check_type (2, NS_QDI,
18557     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
18558   unsigned imm = inst.operands[2].imm;
18559
18560   if (imm == et.size)
18561     {
18562       /* Maximum shift variant.  */
18563       NEON_ENCODE (INTEGER, inst);
18564       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18565       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18566       inst.instruction |= LOW4 (inst.operands[1].reg);
18567       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18568       inst.instruction |= neon_logbits (et.size) << 18;
18569
18570       neon_dp_fixup (&inst);
18571     }
18572   else
18573     {
18574       /* A more-specific type check for non-max versions.  */
18575       et = neon_check_type (2, NS_QDI,
18576         N_EQK | N_DBL, N_SU_32 | N_KEY);
18577       NEON_ENCODE (IMMED, inst);
18578       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
18579     }
18580 }
18581
18582 /* Check the various types for the VCVT instruction, and return which version
18583    the current instruction is.  */
18584
18585 #define CVT_FLAVOUR_VAR                                                       \
18586   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
18587   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
18588   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
18589   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
18590   /* Half-precision conversions.  */                                          \
18591   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
18592   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
18593   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
18594   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
18595   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
18596   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
18597   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
18598      Compared with single/double precision variants, only the co-processor    \
18599      field is different, so the encoding flow is reused here.  */             \
18600   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
18601   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
18602   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
18603   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
18604   /* VFP instructions.  */                                                    \
18605   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
18606   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
18607   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
18608   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
18609   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
18610   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
18611   /* VFP instructions with bitshift.  */                                      \
18612   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
18613   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
18614   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
18615   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
18616   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
18617   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
18618   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
18619   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
18620
18621 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
18622   neon_cvt_flavour_##C,
18623
18624 /* The different types of conversions we can do.  */
18625 enum neon_cvt_flavour
18626 {
18627   CVT_FLAVOUR_VAR
18628   neon_cvt_flavour_invalid,
18629   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
18630 };
18631
18632 #undef CVT_VAR
18633
18634 static enum neon_cvt_flavour
18635 get_neon_cvt_flavour (enum neon_shape rs)
18636 {
18637 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
18638   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
18639   if (et.type != NT_invtype)                            \
18640     {                                                   \
18641       inst.error = NULL;                                \
18642       return (neon_cvt_flavour_##C);                    \
18643     }
18644
18645   struct neon_type_el et;
18646   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
18647                         || rs == NS_FF) ? N_VFP : 0;
18648   /* The instruction versions which take an immediate take one register
18649      argument, which is extended to the width of the full register. Thus the
18650      "source" and "destination" registers must have the same width.  Hack that
18651      here by making the size equal to the key (wider, in this case) operand.  */
18652   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
18653
18654   CVT_FLAVOUR_VAR;
18655
18656   return neon_cvt_flavour_invalid;
18657 #undef CVT_VAR
18658 }
18659
18660 enum neon_cvt_mode
18661 {
18662   neon_cvt_mode_a,
18663   neon_cvt_mode_n,
18664   neon_cvt_mode_p,
18665   neon_cvt_mode_m,
18666   neon_cvt_mode_z,
18667   neon_cvt_mode_x,
18668   neon_cvt_mode_r
18669 };
18670
18671 /* Neon-syntax VFP conversions.  */
18672
18673 static void
18674 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
18675 {
18676   const char *opname = 0;
18677
18678   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
18679       || rs == NS_FHI || rs == NS_HFI)
18680     {
18681       /* Conversions with immediate bitshift.  */
18682       const char *enc[] =
18683         {
18684 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
18685           CVT_FLAVOUR_VAR
18686           NULL
18687 #undef CVT_VAR
18688         };
18689
18690       if (flavour < (int) ARRAY_SIZE (enc))
18691         {
18692           opname = enc[flavour];
18693           constraint (inst.operands[0].reg != inst.operands[1].reg,
18694                       _("operands 0 and 1 must be the same register"));
18695           inst.operands[1] = inst.operands[2];
18696           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
18697         }
18698     }
18699   else
18700     {
18701       /* Conversions without bitshift.  */
18702       const char *enc[] =
18703         {
18704 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
18705           CVT_FLAVOUR_VAR
18706           NULL
18707 #undef CVT_VAR
18708         };
18709
18710       if (flavour < (int) ARRAY_SIZE (enc))
18711         opname = enc[flavour];
18712     }
18713
18714   if (opname)
18715     do_vfp_nsyn_opcode (opname);
18716
18717   /* ARMv8.2 fp16 VCVT instruction.  */
18718   if (flavour == neon_cvt_flavour_s32_f16
18719       || flavour == neon_cvt_flavour_u32_f16
18720       || flavour == neon_cvt_flavour_f16_u32
18721       || flavour == neon_cvt_flavour_f16_s32)
18722     do_scalar_fp16_v82_encode ();
18723 }
18724
18725 static void
18726 do_vfp_nsyn_cvtz (void)
18727 {
18728   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
18729   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
18730   const char *enc[] =
18731     {
18732 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
18733       CVT_FLAVOUR_VAR
18734       NULL
18735 #undef CVT_VAR
18736     };
18737
18738   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
18739     do_vfp_nsyn_opcode (enc[flavour]);
18740 }
18741
18742 static void
18743 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
18744                       enum neon_cvt_mode mode)
18745 {
18746   int sz, op;
18747   int rm;
18748
18749   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
18750      D register operands.  */
18751   if (flavour == neon_cvt_flavour_s32_f64
18752       || flavour == neon_cvt_flavour_u32_f64)
18753     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
18754                 _(BAD_FPU));
18755
18756   if (flavour == neon_cvt_flavour_s32_f16
18757       || flavour == neon_cvt_flavour_u32_f16)
18758     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
18759                 _(BAD_FP16));
18760
18761   set_pred_insn_type (OUTSIDE_PRED_INSN);
18762
18763   switch (flavour)
18764     {
18765     case neon_cvt_flavour_s32_f64:
18766       sz = 1;
18767       op = 1;
18768       break;
18769     case neon_cvt_flavour_s32_f32:
18770       sz = 0;
18771       op = 1;
18772       break;
18773     case neon_cvt_flavour_s32_f16:
18774       sz = 0;
18775       op = 1;
18776       break;
18777     case neon_cvt_flavour_u32_f64:
18778       sz = 1;
18779       op = 0;
18780       break;
18781     case neon_cvt_flavour_u32_f32:
18782       sz = 0;
18783       op = 0;
18784       break;
18785     case neon_cvt_flavour_u32_f16:
18786       sz = 0;
18787       op = 0;
18788       break;
18789     default:
18790       first_error (_("invalid instruction shape"));
18791       return;
18792     }
18793
18794   switch (mode)
18795     {
18796     case neon_cvt_mode_a: rm = 0; break;
18797     case neon_cvt_mode_n: rm = 1; break;
18798     case neon_cvt_mode_p: rm = 2; break;
18799     case neon_cvt_mode_m: rm = 3; break;
18800     default: first_error (_("invalid rounding mode")); return;
18801     }
18802
18803   NEON_ENCODE (FPV8, inst);
18804   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
18805   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
18806   inst.instruction |= sz << 8;
18807
18808   /* ARMv8.2 fp16 VCVT instruction.  */
18809   if (flavour == neon_cvt_flavour_s32_f16
18810       ||flavour == neon_cvt_flavour_u32_f16)
18811     do_scalar_fp16_v82_encode ();
18812   inst.instruction |= op << 7;
18813   inst.instruction |= rm << 16;
18814   inst.instruction |= 0xf0000000;
18815   inst.is_neon = TRUE;
18816 }
18817
18818 static void
18819 do_neon_cvt_1 (enum neon_cvt_mode mode)
18820 {
18821   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
18822                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
18823                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
18824                                           NS_NULL);
18825   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
18826
18827   if (flavour == neon_cvt_flavour_invalid)
18828     return;
18829
18830   /* PR11109: Handle round-to-zero for VCVT conversions.  */
18831   if (mode == neon_cvt_mode_z
18832       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
18833       && (flavour == neon_cvt_flavour_s16_f16
18834           || flavour == neon_cvt_flavour_u16_f16
18835           || flavour == neon_cvt_flavour_s32_f32
18836           || flavour == neon_cvt_flavour_u32_f32
18837           || flavour == neon_cvt_flavour_s32_f64
18838           || flavour == neon_cvt_flavour_u32_f64)
18839       && (rs == NS_FD || rs == NS_FF))
18840     {
18841       do_vfp_nsyn_cvtz ();
18842       return;
18843     }
18844
18845   /* ARMv8.2 fp16 VCVT conversions.  */
18846   if (mode == neon_cvt_mode_z
18847       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
18848       && (flavour == neon_cvt_flavour_s32_f16
18849           || flavour == neon_cvt_flavour_u32_f16)
18850       && (rs == NS_FH))
18851     {
18852       do_vfp_nsyn_cvtz ();
18853       do_scalar_fp16_v82_encode ();
18854       return;
18855     }
18856
18857   /* VFP rather than Neon conversions.  */
18858   if (flavour >= neon_cvt_flavour_first_fp)
18859     {
18860       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
18861         do_vfp_nsyn_cvt (rs, flavour);
18862       else
18863         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
18864
18865       return;
18866     }
18867
18868   switch (rs)
18869     {
18870     case NS_QQI:
18871       if (mode == neon_cvt_mode_z
18872           && (flavour == neon_cvt_flavour_f16_s16
18873               || flavour == neon_cvt_flavour_f16_u16
18874               || flavour == neon_cvt_flavour_s16_f16
18875               || flavour == neon_cvt_flavour_u16_f16
18876               || flavour == neon_cvt_flavour_f32_u32
18877               || flavour == neon_cvt_flavour_f32_s32
18878               || flavour == neon_cvt_flavour_s32_f32
18879               || flavour == neon_cvt_flavour_u32_f32))
18880         {
18881           if (!check_simd_pred_availability (TRUE,
18882                                              NEON_CHECK_CC | NEON_CHECK_ARCH))
18883             return;
18884         }
18885       else if (mode == neon_cvt_mode_n)
18886         {
18887           /* We are dealing with vcvt with the 'ne' condition.  */
18888           inst.cond = 0x1;
18889           inst.instruction = N_MNEM_vcvt;
18890           do_neon_cvt_1 (neon_cvt_mode_z);
18891           return;
18892         }
18893       /* fall through.  */
18894     case NS_DDI:
18895       {
18896         unsigned immbits;
18897         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
18898                              0x0000100, 0x1000100, 0x0, 0x1000000};
18899
18900         if ((rs != NS_QQI || !ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18901             && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
18902             return;
18903
18904         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
18905           {
18906             constraint (inst.operands[2].present && inst.operands[2].imm == 0,
18907                         _("immediate value out of range"));
18908             switch (flavour)
18909               {
18910                 case neon_cvt_flavour_f16_s16:
18911                 case neon_cvt_flavour_f16_u16:
18912                 case neon_cvt_flavour_s16_f16:
18913                 case neon_cvt_flavour_u16_f16:
18914                   constraint (inst.operands[2].imm > 16,
18915                               _("immediate value out of range"));
18916                   break;
18917                 case neon_cvt_flavour_f32_u32:
18918                 case neon_cvt_flavour_f32_s32:
18919                 case neon_cvt_flavour_s32_f32:
18920                 case neon_cvt_flavour_u32_f32:
18921                   constraint (inst.operands[2].imm > 32,
18922                               _("immediate value out of range"));
18923                   break;
18924                 default:
18925                   inst.error = BAD_FPU;
18926                   return;
18927               }
18928           }
18929
18930         /* Fixed-point conversion with #0 immediate is encoded as an
18931            integer conversion.  */
18932         if (inst.operands[2].present && inst.operands[2].imm == 0)
18933           goto int_encode;
18934         NEON_ENCODE (IMMED, inst);
18935         if (flavour != neon_cvt_flavour_invalid)
18936           inst.instruction |= enctab[flavour];
18937         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18938         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18939         inst.instruction |= LOW4 (inst.operands[1].reg);
18940         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18941         inst.instruction |= neon_quad (rs) << 6;
18942         inst.instruction |= 1 << 21;
18943         if (flavour < neon_cvt_flavour_s16_f16)
18944           {
18945             inst.instruction |= 1 << 21;
18946             immbits = 32 - inst.operands[2].imm;
18947             inst.instruction |= immbits << 16;
18948           }
18949         else
18950           {
18951             inst.instruction |= 3 << 20;
18952             immbits = 16 - inst.operands[2].imm;
18953             inst.instruction |= immbits << 16;
18954             inst.instruction &= ~(1 << 9);
18955           }
18956
18957         neon_dp_fixup (&inst);
18958       }
18959       break;
18960
18961     case NS_QQ:
18962       if ((mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
18963            || mode == neon_cvt_mode_m || mode == neon_cvt_mode_p)
18964           && (flavour == neon_cvt_flavour_s16_f16
18965               || flavour == neon_cvt_flavour_u16_f16
18966               || flavour == neon_cvt_flavour_s32_f32
18967               || flavour == neon_cvt_flavour_u32_f32))
18968         {
18969           if (!check_simd_pred_availability (TRUE,
18970                                              NEON_CHECK_CC | NEON_CHECK_ARCH8))
18971             return;
18972         }
18973       else if (mode == neon_cvt_mode_z
18974                && (flavour == neon_cvt_flavour_f16_s16
18975                    || flavour == neon_cvt_flavour_f16_u16
18976                    || flavour == neon_cvt_flavour_s16_f16
18977                    || flavour == neon_cvt_flavour_u16_f16
18978                    || flavour == neon_cvt_flavour_f32_u32
18979                    || flavour == neon_cvt_flavour_f32_s32
18980                    || flavour == neon_cvt_flavour_s32_f32
18981                    || flavour == neon_cvt_flavour_u32_f32))
18982         {
18983           if (!check_simd_pred_availability (TRUE,
18984                                              NEON_CHECK_CC | NEON_CHECK_ARCH))
18985             return;
18986         }
18987       /* fall through.  */
18988     case NS_DD:
18989       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
18990         {
18991
18992           NEON_ENCODE (FLOAT, inst);
18993           if (!check_simd_pred_availability (TRUE,
18994                                              NEON_CHECK_CC | NEON_CHECK_ARCH8))
18995             return;
18996
18997           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18998           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18999           inst.instruction |= LOW4 (inst.operands[1].reg);
19000           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19001           inst.instruction |= neon_quad (rs) << 6;
19002           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
19003                                || flavour == neon_cvt_flavour_u32_f32) << 7;
19004           inst.instruction |= mode << 8;
19005           if (flavour == neon_cvt_flavour_u16_f16
19006               || flavour == neon_cvt_flavour_s16_f16)
19007             /* Mask off the original size bits and reencode them.  */
19008             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
19009
19010           if (thumb_mode)
19011             inst.instruction |= 0xfc000000;
19012           else
19013             inst.instruction |= 0xf0000000;
19014         }
19015       else
19016         {
19017     int_encode:
19018           {
19019             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
19020                                   0x100, 0x180, 0x0, 0x080};
19021
19022             NEON_ENCODE (INTEGER, inst);
19023
19024           if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
19025             {
19026               if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
19027                 return;
19028             }
19029
19030             if (flavour != neon_cvt_flavour_invalid)
19031               inst.instruction |= enctab[flavour];
19032
19033             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19034             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19035             inst.instruction |= LOW4 (inst.operands[1].reg);
19036             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19037             inst.instruction |= neon_quad (rs) << 6;
19038             if (flavour >= neon_cvt_flavour_s16_f16
19039                 && flavour <= neon_cvt_flavour_f16_u16)
19040               /* Half precision.  */
19041               inst.instruction |= 1 << 18;
19042             else
19043               inst.instruction |= 2 << 18;
19044
19045             neon_dp_fixup (&inst);
19046           }
19047         }
19048       break;
19049
19050     /* Half-precision conversions for Advanced SIMD -- neon.  */
19051     case NS_QD:
19052     case NS_DQ:
19053       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
19054         return;
19055
19056       if ((rs == NS_DQ)
19057           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
19058           {
19059             as_bad (_("operand size must match register width"));
19060             break;
19061           }
19062
19063       if ((rs == NS_QD)
19064           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
19065           {
19066             as_bad (_("operand size must match register width"));
19067             break;
19068           }
19069
19070       if (rs == NS_DQ)
19071         inst.instruction = 0x3b60600;
19072       else
19073         inst.instruction = 0x3b60700;
19074
19075       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19076       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19077       inst.instruction |= LOW4 (inst.operands[1].reg);
19078       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19079       neon_dp_fixup (&inst);
19080       break;
19081
19082     default:
19083       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
19084       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
19085         do_vfp_nsyn_cvt (rs, flavour);
19086       else
19087         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
19088     }
19089 }
19090
19091 static void
19092 do_neon_cvtr (void)
19093 {
19094   do_neon_cvt_1 (neon_cvt_mode_x);
19095 }
19096
19097 static void
19098 do_neon_cvt (void)
19099 {
19100   do_neon_cvt_1 (neon_cvt_mode_z);
19101 }
19102
19103 static void
19104 do_neon_cvta (void)
19105 {
19106   do_neon_cvt_1 (neon_cvt_mode_a);
19107 }
19108
19109 static void
19110 do_neon_cvtn (void)
19111 {
19112   do_neon_cvt_1 (neon_cvt_mode_n);
19113 }
19114
19115 static void
19116 do_neon_cvtp (void)
19117 {
19118   do_neon_cvt_1 (neon_cvt_mode_p);
19119 }
19120
19121 static void
19122 do_neon_cvtm (void)
19123 {
19124   do_neon_cvt_1 (neon_cvt_mode_m);
19125 }
19126
19127 static void
19128 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
19129 {
19130   if (is_double)
19131     mark_feature_used (&fpu_vfp_ext_armv8);
19132
19133   encode_arm_vfp_reg (inst.operands[0].reg,
19134                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
19135   encode_arm_vfp_reg (inst.operands[1].reg,
19136                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
19137   inst.instruction |= to ? 0x10000 : 0;
19138   inst.instruction |= t ? 0x80 : 0;
19139   inst.instruction |= is_double ? 0x100 : 0;
19140   do_vfp_cond_or_thumb ();
19141 }
19142
19143 static void
19144 do_neon_cvttb_1 (bfd_boolean t)
19145 {
19146   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
19147                                           NS_DF, NS_DH, NS_QQ, NS_QQI, NS_NULL);
19148
19149   if (rs == NS_NULL)
19150     return;
19151   else if (rs == NS_QQ || rs == NS_QQI)
19152     {
19153       int single_to_half = 0;
19154       if (!check_simd_pred_availability (TRUE, NEON_CHECK_ARCH))
19155         return;
19156
19157       enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
19158
19159       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19160           && (flavour ==  neon_cvt_flavour_u16_f16
19161               || flavour ==  neon_cvt_flavour_s16_f16
19162               || flavour ==  neon_cvt_flavour_f16_s16
19163               || flavour ==  neon_cvt_flavour_f16_u16
19164               || flavour ==  neon_cvt_flavour_u32_f32
19165               || flavour ==  neon_cvt_flavour_s32_f32
19166               || flavour ==  neon_cvt_flavour_f32_s32
19167               || flavour ==  neon_cvt_flavour_f32_u32))
19168         {
19169           inst.cond = 0xf;
19170           inst.instruction = N_MNEM_vcvt;
19171           set_pred_insn_type (INSIDE_VPT_INSN);
19172           do_neon_cvt_1 (neon_cvt_mode_z);
19173           return;
19174         }
19175       else if (rs == NS_QQ && flavour == neon_cvt_flavour_f32_f16)
19176         single_to_half = 1;
19177       else if (rs == NS_QQ && flavour != neon_cvt_flavour_f16_f32)
19178         {
19179           first_error (BAD_FPU);
19180           return;
19181         }
19182
19183       inst.instruction = 0xee3f0e01;
19184       inst.instruction |= single_to_half << 28;
19185       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19186       inst.instruction |= LOW4 (inst.operands[0].reg) << 13;
19187       inst.instruction |= t << 12;
19188       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19189       inst.instruction |= LOW4 (inst.operands[1].reg) << 1;
19190       inst.is_neon = 1;
19191     }
19192   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
19193     {
19194       inst.error = NULL;
19195       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
19196     }
19197   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
19198     {
19199       inst.error = NULL;
19200       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
19201     }
19202   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
19203     {
19204       /* The VCVTB and VCVTT instructions with D-register operands
19205          don't work for SP only targets.  */
19206       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19207                   _(BAD_FPU));
19208
19209       inst.error = NULL;
19210       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
19211     }
19212   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
19213     {
19214       /* The VCVTB and VCVTT instructions with D-register operands
19215          don't work for SP only targets.  */
19216       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19217                   _(BAD_FPU));
19218
19219       inst.error = NULL;
19220       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
19221     }
19222   else
19223     return;
19224 }
19225
19226 static void
19227 do_neon_cvtb (void)
19228 {
19229   do_neon_cvttb_1 (FALSE);
19230 }
19231
19232
19233 static void
19234 do_neon_cvtt (void)
19235 {
19236   do_neon_cvttb_1 (TRUE);
19237 }
19238
19239 static void
19240 neon_move_immediate (void)
19241 {
19242   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
19243   struct neon_type_el et = neon_check_type (2, rs,
19244     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
19245   unsigned immlo, immhi = 0, immbits;
19246   int op, cmode, float_p;
19247
19248   constraint (et.type == NT_invtype,
19249               _("operand size must be specified for immediate VMOV"));
19250
19251   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
19252   op = (inst.instruction & (1 << 5)) != 0;
19253
19254   immlo = inst.operands[1].imm;
19255   if (inst.operands[1].regisimm)
19256     immhi = inst.operands[1].reg;
19257
19258   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
19259               _("immediate has bits set outside the operand size"));
19260
19261   float_p = inst.operands[1].immisfloat;
19262
19263   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
19264                                         et.size, et.type)) == FAIL)
19265     {
19266       /* Invert relevant bits only.  */
19267       neon_invert_size (&immlo, &immhi, et.size);
19268       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
19269          with one or the other; those cases are caught by
19270          neon_cmode_for_move_imm.  */
19271       op = !op;
19272       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
19273                                             &op, et.size, et.type)) == FAIL)
19274         {
19275           first_error (_("immediate out of range"));
19276           return;
19277         }
19278     }
19279
19280   inst.instruction &= ~(1 << 5);
19281   inst.instruction |= op << 5;
19282
19283   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19284   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19285   inst.instruction |= neon_quad (rs) << 6;
19286   inst.instruction |= cmode << 8;
19287
19288   neon_write_immbits (immbits);
19289 }
19290
19291 static void
19292 do_neon_mvn (void)
19293 {
19294   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
19295     return;
19296
19297   if (inst.operands[1].isreg)
19298     {
19299       enum neon_shape rs;
19300       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19301         rs = neon_select_shape (NS_QQ, NS_NULL);
19302       else
19303         rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19304
19305       NEON_ENCODE (INTEGER, inst);
19306       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19307       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19308       inst.instruction |= LOW4 (inst.operands[1].reg);
19309       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19310       inst.instruction |= neon_quad (rs) << 6;
19311     }
19312   else
19313     {
19314       NEON_ENCODE (IMMED, inst);
19315       neon_move_immediate ();
19316     }
19317
19318   neon_dp_fixup (&inst);
19319
19320   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19321     {
19322       constraint (!inst.operands[1].isreg && !inst.operands[0].isquad, BAD_FPU);
19323       constraint ((inst.instruction & 0xd00) == 0xd00,
19324                   _("immediate value out of range"));
19325     }
19326 }
19327
19328 /* Encode instructions of form:
19329
19330   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
19331   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
19332
19333 static void
19334 neon_mixed_length (struct neon_type_el et, unsigned size)
19335 {
19336   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19337   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19338   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19339   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19340   inst.instruction |= LOW4 (inst.operands[2].reg);
19341   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19342   inst.instruction |= (et.type == NT_unsigned) << 24;
19343   inst.instruction |= neon_logbits (size) << 20;
19344
19345   neon_dp_fixup (&inst);
19346 }
19347
19348 static void
19349 do_neon_dyadic_long (void)
19350 {
19351   enum neon_shape rs = neon_select_shape (NS_QDD, NS_QQQ, NS_QQR, NS_NULL);
19352   if (rs == NS_QDD)
19353     {
19354       if (vfp_or_neon_is_neon (NEON_CHECK_ARCH | NEON_CHECK_CC) == FAIL)
19355         return;
19356
19357       NEON_ENCODE (INTEGER, inst);
19358       /* FIXME: Type checking for lengthening op.  */
19359       struct neon_type_el et = neon_check_type (3, NS_QDD,
19360         N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
19361       neon_mixed_length (et, et.size);
19362     }
19363   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19364            && (inst.cond == 0xf || inst.cond == 0x10))
19365     {
19366       /* If parsing for MVE, vaddl/vsubl/vabdl{e,t} can only be vadd/vsub/vabd
19367          in an IT block with le/lt conditions.  */
19368
19369       if (inst.cond == 0xf)
19370         inst.cond = 0xb;
19371       else if (inst.cond == 0x10)
19372         inst.cond = 0xd;
19373
19374       inst.pred_insn_type = INSIDE_IT_INSN;
19375
19376       if (inst.instruction == N_MNEM_vaddl)
19377         {
19378           inst.instruction = N_MNEM_vadd;
19379           do_neon_addsub_if_i ();
19380         }
19381       else if (inst.instruction == N_MNEM_vsubl)
19382         {
19383           inst.instruction = N_MNEM_vsub;
19384           do_neon_addsub_if_i ();
19385         }
19386       else if (inst.instruction == N_MNEM_vabdl)
19387         {
19388           inst.instruction = N_MNEM_vabd;
19389           do_neon_dyadic_if_su ();
19390         }
19391     }
19392   else
19393     first_error (BAD_FPU);
19394 }
19395
19396 static void
19397 do_neon_abal (void)
19398 {
19399   struct neon_type_el et = neon_check_type (3, NS_QDD,
19400     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
19401   neon_mixed_length (et, et.size);
19402 }
19403
19404 static void
19405 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
19406 {
19407   if (inst.operands[2].isscalar)
19408     {
19409       struct neon_type_el et = neon_check_type (3, NS_QDS,
19410         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
19411       NEON_ENCODE (SCALAR, inst);
19412       neon_mul_mac (et, et.type == NT_unsigned);
19413     }
19414   else
19415     {
19416       struct neon_type_el et = neon_check_type (3, NS_QDD,
19417         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
19418       NEON_ENCODE (INTEGER, inst);
19419       neon_mixed_length (et, et.size);
19420     }
19421 }
19422
19423 static void
19424 do_neon_mac_maybe_scalar_long (void)
19425 {
19426   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
19427 }
19428
19429 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
19430    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
19431
19432 static unsigned
19433 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
19434 {
19435   unsigned regno = NEON_SCALAR_REG (scalar);
19436   unsigned elno = NEON_SCALAR_INDEX (scalar);
19437
19438   if (quad_p)
19439     {
19440       if (regno > 7 || elno > 3)
19441         goto bad_scalar;
19442
19443       return ((regno & 0x7)
19444               | ((elno & 0x1) << 3)
19445               | (((elno >> 1) & 0x1) << 5));
19446     }
19447   else
19448     {
19449       if (regno > 15 || elno > 1)
19450         goto bad_scalar;
19451
19452       return (((regno & 0x1) << 5)
19453               | ((regno >> 1) & 0x7)
19454               | ((elno & 0x1) << 3));
19455     }
19456
19457 bad_scalar:
19458   first_error (_("scalar out of range for multiply instruction"));
19459   return 0;
19460 }
19461
19462 static void
19463 do_neon_fmac_maybe_scalar_long (int subtype)
19464 {
19465   enum neon_shape rs;
19466   int high8;
19467   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
19468      field (bits[21:20]) has different meaning.  For scalar index variant, it's
19469      used to differentiate add and subtract, otherwise it's with fixed value
19470      0x2.  */
19471   int size = -1;
19472
19473   if (inst.cond != COND_ALWAYS)
19474     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
19475                "behaviour is UNPREDICTABLE"));
19476
19477   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
19478               _(BAD_FP16));
19479
19480   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
19481               _(BAD_FPU));
19482
19483   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
19484      be a scalar index register.  */
19485   if (inst.operands[2].isscalar)
19486     {
19487       high8 = 0xfe000000;
19488       if (subtype)
19489         size = 16;
19490       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
19491     }
19492   else
19493     {
19494       high8 = 0xfc000000;
19495       size = 32;
19496       if (subtype)
19497         inst.instruction |= (0x1 << 23);
19498       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
19499     }
19500
19501   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
19502
19503   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
19504      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
19505      so we simply pass -1 as size.  */
19506   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
19507   neon_three_same (quad_p, 0, size);
19508
19509   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
19510   inst.instruction &= 0x00ffffff;
19511   inst.instruction |= high8;
19512
19513 #define LOW1(R) ((R) & 0x1)
19514 #define HI4(R) (((R) >> 1) & 0xf)
19515   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
19516      whether the instruction is in Q form and whether Vm is a scalar indexed
19517      operand.  */
19518   if (inst.operands[2].isscalar)
19519     {
19520       unsigned rm
19521         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
19522       inst.instruction &= 0xffffffd0;
19523       inst.instruction |= rm;
19524
19525       if (!quad_p)
19526         {
19527           /* Redo Rn as well.  */
19528           inst.instruction &= 0xfff0ff7f;
19529           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
19530           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
19531         }
19532     }
19533   else if (!quad_p)
19534     {
19535       /* Redo Rn and Rm.  */
19536       inst.instruction &= 0xfff0ff50;
19537       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
19538       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
19539       inst.instruction |= HI4 (inst.operands[2].reg);
19540       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
19541     }
19542 }
19543
19544 static void
19545 do_neon_vfmal (void)
19546 {
19547   return do_neon_fmac_maybe_scalar_long (0);
19548 }
19549
19550 static void
19551 do_neon_vfmsl (void)
19552 {
19553   return do_neon_fmac_maybe_scalar_long (1);
19554 }
19555
19556 static void
19557 do_neon_dyadic_wide (void)
19558 {
19559   struct neon_type_el et = neon_check_type (3, NS_QQD,
19560     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
19561   neon_mixed_length (et, et.size);
19562 }
19563
19564 static void
19565 do_neon_dyadic_narrow (void)
19566 {
19567   struct neon_type_el et = neon_check_type (3, NS_QDD,
19568     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
19569   /* Operand sign is unimportant, and the U bit is part of the opcode,
19570      so force the operand type to integer.  */
19571   et.type = NT_integer;
19572   neon_mixed_length (et, et.size / 2);
19573 }
19574
19575 static void
19576 do_neon_mul_sat_scalar_long (void)
19577 {
19578   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
19579 }
19580
19581 static void
19582 do_neon_vmull (void)
19583 {
19584   if (inst.operands[2].isscalar)
19585     do_neon_mac_maybe_scalar_long ();
19586   else
19587     {
19588       struct neon_type_el et = neon_check_type (3, NS_QDD,
19589         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
19590
19591       if (et.type == NT_poly)
19592         NEON_ENCODE (POLY, inst);
19593       else
19594         NEON_ENCODE (INTEGER, inst);
19595
19596       /* For polynomial encoding the U bit must be zero, and the size must
19597          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
19598          obviously, as 0b10).  */
19599       if (et.size == 64)
19600         {
19601           /* Check we're on the correct architecture.  */
19602           if (!mark_feature_used (&fpu_crypto_ext_armv8))
19603             inst.error =
19604               _("Instruction form not available on this architecture.");
19605
19606           et.size = 32;
19607         }
19608
19609       neon_mixed_length (et, et.size);
19610     }
19611 }
19612
19613 static void
19614 do_neon_ext (void)
19615 {
19616   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
19617   struct neon_type_el et = neon_check_type (3, rs,
19618     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
19619   unsigned imm = (inst.operands[3].imm * et.size) / 8;
19620
19621   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
19622               _("shift out of range"));
19623   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19624   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19625   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19626   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19627   inst.instruction |= LOW4 (inst.operands[2].reg);
19628   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19629   inst.instruction |= neon_quad (rs) << 6;
19630   inst.instruction |= imm << 8;
19631
19632   neon_dp_fixup (&inst);
19633 }
19634
19635 static void
19636 do_neon_rev (void)
19637 {
19638   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
19639    return;
19640
19641   enum neon_shape rs;
19642   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19643     rs = neon_select_shape (NS_QQ, NS_NULL);
19644   else
19645     rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19646
19647   struct neon_type_el et = neon_check_type (2, rs,
19648     N_EQK, N_8 | N_16 | N_32 | N_KEY);
19649
19650   unsigned op = (inst.instruction >> 7) & 3;
19651   /* N (width of reversed regions) is encoded as part of the bitmask. We
19652      extract it here to check the elements to be reversed are smaller.
19653      Otherwise we'd get a reserved instruction.  */
19654   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
19655
19656   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext) && elsize == 64
19657       && inst.operands[0].reg == inst.operands[1].reg)
19658     as_tsktsk (_("Warning: 64-bit element size and same destination and source"
19659                  " operands makes instruction UNPREDICTABLE"));
19660
19661   gas_assert (elsize != 0);
19662   constraint (et.size >= elsize,
19663               _("elements must be smaller than reversal region"));
19664   neon_two_same (neon_quad (rs), 1, et.size);
19665 }
19666
19667 static void
19668 do_neon_dup (void)
19669 {
19670   if (inst.operands[1].isscalar)
19671     {
19672       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1),
19673                   BAD_FPU);
19674       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
19675       struct neon_type_el et = neon_check_type (2, rs,
19676         N_EQK, N_8 | N_16 | N_32 | N_KEY);
19677       unsigned sizebits = et.size >> 3;
19678       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
19679       int logsize = neon_logbits (et.size);
19680       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
19681
19682       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
19683         return;
19684
19685       NEON_ENCODE (SCALAR, inst);
19686       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19687       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19688       inst.instruction |= LOW4 (dm);
19689       inst.instruction |= HI1 (dm) << 5;
19690       inst.instruction |= neon_quad (rs) << 6;
19691       inst.instruction |= x << 17;
19692       inst.instruction |= sizebits << 16;
19693
19694       neon_dp_fixup (&inst);
19695     }
19696   else
19697     {
19698       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
19699       struct neon_type_el et = neon_check_type (2, rs,
19700         N_8 | N_16 | N_32 | N_KEY, N_EQK);
19701       if (rs == NS_QR)
19702         {
19703           if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH))
19704             return;
19705         }
19706       else
19707         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1),
19708                     BAD_FPU);
19709
19710       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19711         {
19712           if (inst.operands[1].reg == REG_SP)
19713             as_tsktsk (MVE_BAD_SP);
19714           else if (inst.operands[1].reg == REG_PC)
19715             as_tsktsk (MVE_BAD_PC);
19716         }
19717
19718       /* Duplicate ARM register to lanes of vector.  */
19719       NEON_ENCODE (ARMREG, inst);
19720       switch (et.size)
19721         {
19722         case 8:  inst.instruction |= 0x400000; break;
19723         case 16: inst.instruction |= 0x000020; break;
19724         case 32: inst.instruction |= 0x000000; break;
19725         default: break;
19726         }
19727       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
19728       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
19729       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
19730       inst.instruction |= neon_quad (rs) << 21;
19731       /* The encoding for this instruction is identical for the ARM and Thumb
19732          variants, except for the condition field.  */
19733       do_vfp_cond_or_thumb ();
19734     }
19735 }
19736
19737 static void
19738 do_mve_mov (int toQ)
19739 {
19740   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19741     return;
19742   if (inst.cond > COND_ALWAYS)
19743     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
19744
19745   unsigned Rt = 0, Rt2 = 1, Q0 = 2, Q1 = 3;
19746   if (toQ)
19747     {
19748       Q0 = 0;
19749       Q1 = 1;
19750       Rt = 2;
19751       Rt2 = 3;
19752     }
19753
19754   constraint (inst.operands[Q0].reg != inst.operands[Q1].reg + 2,
19755               _("Index one must be [2,3] and index two must be two less than"
19756                 " index one."));
19757   constraint (inst.operands[Rt].reg == inst.operands[Rt2].reg,
19758               _("General purpose registers may not be the same"));
19759   constraint (inst.operands[Rt].reg == REG_SP
19760               || inst.operands[Rt2].reg == REG_SP,
19761               BAD_SP);
19762   constraint (inst.operands[Rt].reg == REG_PC
19763               || inst.operands[Rt2].reg == REG_PC,
19764               BAD_PC);
19765
19766   inst.instruction = 0xec000f00;
19767   inst.instruction |= HI1 (inst.operands[Q1].reg / 32) << 23;
19768   inst.instruction |= !!toQ << 20;
19769   inst.instruction |= inst.operands[Rt2].reg << 16;
19770   inst.instruction |= LOW4 (inst.operands[Q1].reg / 32) << 13;
19771   inst.instruction |= (inst.operands[Q1].reg % 4) << 4;
19772   inst.instruction |= inst.operands[Rt].reg;
19773 }
19774
19775 static void
19776 do_mve_movn (void)
19777 {
19778   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19779     return;
19780
19781   if (inst.cond > COND_ALWAYS)
19782     inst.pred_insn_type = INSIDE_VPT_INSN;
19783   else
19784     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
19785
19786   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_I16 | N_I32
19787                                             | N_KEY);
19788
19789   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19790   inst.instruction |= (neon_logbits (et.size) - 1) << 18;
19791   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19792   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19793   inst.instruction |= LOW4 (inst.operands[1].reg);
19794   inst.is_neon = 1;
19795
19796 }
19797
19798 /* VMOV has particularly many variations. It can be one of:
19799      0. VMOV<c><q> <Qd>, <Qm>
19800      1. VMOV<c><q> <Dd>, <Dm>
19801    (Register operations, which are VORR with Rm = Rn.)
19802      2. VMOV<c><q>.<dt> <Qd>, #<imm>
19803      3. VMOV<c><q>.<dt> <Dd>, #<imm>
19804    (Immediate loads.)
19805      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
19806    (ARM register to scalar.)
19807      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
19808    (Two ARM registers to vector.)
19809      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
19810    (Scalar to ARM register.)
19811      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
19812    (Vector to two ARM registers.)
19813      8. VMOV.F32 <Sd>, <Sm>
19814      9. VMOV.F64 <Dd>, <Dm>
19815    (VFP register moves.)
19816     10. VMOV.F32 <Sd>, #imm
19817     11. VMOV.F64 <Dd>, #imm
19818    (VFP float immediate load.)
19819     12. VMOV <Rd>, <Sm>
19820    (VFP single to ARM reg.)
19821     13. VMOV <Sd>, <Rm>
19822    (ARM reg to VFP single.)
19823     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
19824    (Two ARM regs to two VFP singles.)
19825     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
19826    (Two VFP singles to two ARM regs.)
19827    16. VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>
19828    17. VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>
19829    18. VMOV<c>.<dt> <Rt>, <Qn[idx]>
19830    19. VMOV<c>.<dt> <Qd[idx]>, <Rt>
19831
19832    These cases can be disambiguated using neon_select_shape, except cases 1/9
19833    and 3/11 which depend on the operand type too.
19834
19835    All the encoded bits are hardcoded by this function.
19836
19837    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
19838    Cases 5, 7 may be used with VFPv2 and above.
19839
19840    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
19841    can specify a type where it doesn't make sense to, and is ignored).  */
19842
19843 static void
19844 do_neon_mov (void)
19845 {
19846   enum neon_shape rs = neon_select_shape (NS_RRSS, NS_SSRR, NS_RRFF, NS_FFRR,
19847                                           NS_DRR, NS_RRD, NS_QQ, NS_DD, NS_QI,
19848                                           NS_DI, NS_SR, NS_RS, NS_FF, NS_FI,
19849                                           NS_RF, NS_FR, NS_HR, NS_RH, NS_HI,
19850                                           NS_NULL);
19851   struct neon_type_el et;
19852   const char *ldconst = 0;
19853
19854   switch (rs)
19855     {
19856     case NS_DD:  /* case 1/9.  */
19857       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
19858       /* It is not an error here if no type is given.  */
19859       inst.error = NULL;
19860       if (et.type == NT_float && et.size == 64)
19861         {
19862           do_vfp_nsyn_opcode ("fcpyd");
19863           break;
19864         }
19865       /* fall through.  */
19866
19867     case NS_QQ:  /* case 0/1.  */
19868       {
19869         if (!check_simd_pred_availability (FALSE,
19870                                            NEON_CHECK_CC | NEON_CHECK_ARCH))
19871           return;
19872         /* The architecture manual I have doesn't explicitly state which
19873            value the U bit should have for register->register moves, but
19874            the equivalent VORR instruction has U = 0, so do that.  */
19875         inst.instruction = 0x0200110;
19876         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19877         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19878         inst.instruction |= LOW4 (inst.operands[1].reg);
19879         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19880         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19881         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19882         inst.instruction |= neon_quad (rs) << 6;
19883
19884         neon_dp_fixup (&inst);
19885       }
19886       break;
19887
19888     case NS_DI:  /* case 3/11.  */
19889       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
19890       inst.error = NULL;
19891       if (et.type == NT_float && et.size == 64)
19892         {
19893           /* case 11 (fconstd).  */
19894           ldconst = "fconstd";
19895           goto encode_fconstd;
19896         }
19897       /* fall through.  */
19898
19899     case NS_QI:  /* case 2/3.  */
19900       if (!check_simd_pred_availability (FALSE,
19901                                          NEON_CHECK_CC | NEON_CHECK_ARCH))
19902         return;
19903       inst.instruction = 0x0800010;
19904       neon_move_immediate ();
19905       neon_dp_fixup (&inst);
19906       break;
19907
19908     case NS_SR:  /* case 4.  */
19909       {
19910         unsigned bcdebits = 0;
19911         int logsize;
19912         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
19913         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
19914
19915         /* .<size> is optional here, defaulting to .32. */
19916         if (inst.vectype.elems == 0
19917             && inst.operands[0].vectype.type == NT_invtype
19918             && inst.operands[1].vectype.type == NT_invtype)
19919           {
19920             inst.vectype.el[0].type = NT_untyped;
19921             inst.vectype.el[0].size = 32;
19922             inst.vectype.elems = 1;
19923           }
19924
19925         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
19926         logsize = neon_logbits (et.size);
19927
19928         if (et.size != 32)
19929           {
19930             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19931                 && vfp_or_neon_is_neon (NEON_CHECK_ARCH) == FAIL)
19932               return;
19933           }
19934         else
19935           {
19936             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
19937                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19938                         _(BAD_FPU));
19939           }
19940
19941         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19942           {
19943             if (inst.operands[1].reg == REG_SP)
19944               as_tsktsk (MVE_BAD_SP);
19945             else if (inst.operands[1].reg == REG_PC)
19946               as_tsktsk (MVE_BAD_PC);
19947           }
19948         unsigned size = inst.operands[0].isscalar == 1 ? 64 : 128;
19949
19950         constraint (et.type == NT_invtype, _("bad type for scalar"));
19951         constraint (x >= size / et.size, _("scalar index out of range"));
19952
19953
19954         switch (et.size)
19955           {
19956           case 8:  bcdebits = 0x8; break;
19957           case 16: bcdebits = 0x1; break;
19958           case 32: bcdebits = 0x0; break;
19959           default: ;
19960           }
19961
19962         bcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
19963
19964         inst.instruction = 0xe000b10;
19965         do_vfp_cond_or_thumb ();
19966         inst.instruction |= LOW4 (dn) << 16;
19967         inst.instruction |= HI1 (dn) << 7;
19968         inst.instruction |= inst.operands[1].reg << 12;
19969         inst.instruction |= (bcdebits & 3) << 5;
19970         inst.instruction |= ((bcdebits >> 2) & 3) << 21;
19971         inst.instruction |= (x >> (3-logsize)) << 16;
19972       }
19973       break;
19974
19975     case NS_DRR:  /* case 5 (fmdrr).  */
19976       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
19977                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
19978                   _(BAD_FPU));
19979
19980       inst.instruction = 0xc400b10;
19981       do_vfp_cond_or_thumb ();
19982       inst.instruction |= LOW4 (inst.operands[0].reg);
19983       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
19984       inst.instruction |= inst.operands[1].reg << 12;
19985       inst.instruction |= inst.operands[2].reg << 16;
19986       break;
19987
19988     case NS_RS:  /* case 6.  */
19989       {
19990         unsigned logsize;
19991         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
19992         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
19993         unsigned abcdebits = 0;
19994
19995         /* .<dt> is optional here, defaulting to .32. */
19996         if (inst.vectype.elems == 0
19997             && inst.operands[0].vectype.type == NT_invtype
19998             && inst.operands[1].vectype.type == NT_invtype)
19999           {
20000             inst.vectype.el[0].type = NT_untyped;
20001             inst.vectype.el[0].size = 32;
20002             inst.vectype.elems = 1;
20003           }
20004
20005         et = neon_check_type (2, NS_NULL,
20006                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
20007         logsize = neon_logbits (et.size);
20008
20009         if (et.size != 32)
20010           {
20011             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
20012                 && vfp_or_neon_is_neon (NEON_CHECK_CC
20013                                         | NEON_CHECK_ARCH) == FAIL)
20014               return;
20015           }
20016         else
20017           {
20018             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
20019                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20020                         _(BAD_FPU));
20021           }
20022
20023         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20024           {
20025             if (inst.operands[0].reg == REG_SP)
20026               as_tsktsk (MVE_BAD_SP);
20027             else if (inst.operands[0].reg == REG_PC)
20028               as_tsktsk (MVE_BAD_PC);
20029           }
20030
20031         unsigned size = inst.operands[1].isscalar == 1 ? 64 : 128;
20032
20033         constraint (et.type == NT_invtype, _("bad type for scalar"));
20034         constraint (x >= size / et.size, _("scalar index out of range"));
20035
20036         switch (et.size)
20037           {
20038           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
20039           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
20040           case 32: abcdebits = 0x00; break;
20041           default: ;
20042           }
20043
20044         abcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
20045         inst.instruction = 0xe100b10;
20046         do_vfp_cond_or_thumb ();
20047         inst.instruction |= LOW4 (dn) << 16;
20048         inst.instruction |= HI1 (dn) << 7;
20049         inst.instruction |= inst.operands[0].reg << 12;
20050         inst.instruction |= (abcdebits & 3) << 5;
20051         inst.instruction |= (abcdebits >> 2) << 21;
20052         inst.instruction |= (x >> (3-logsize)) << 16;
20053       }
20054       break;
20055
20056     case NS_RRD:  /* case 7 (fmrrd).  */
20057       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20058                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20059                   _(BAD_FPU));
20060
20061       inst.instruction = 0xc500b10;
20062       do_vfp_cond_or_thumb ();
20063       inst.instruction |= inst.operands[0].reg << 12;
20064       inst.instruction |= inst.operands[1].reg << 16;
20065       inst.instruction |= LOW4 (inst.operands[2].reg);
20066       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
20067       break;
20068
20069     case NS_FF:  /* case 8 (fcpys).  */
20070       do_vfp_nsyn_opcode ("fcpys");
20071       break;
20072
20073     case NS_HI:
20074     case NS_FI:  /* case 10 (fconsts).  */
20075       ldconst = "fconsts";
20076     encode_fconstd:
20077       if (!inst.operands[1].immisfloat)
20078         {
20079           unsigned new_imm;
20080           /* Immediate has to fit in 8 bits so float is enough.  */
20081           float imm = (float) inst.operands[1].imm;
20082           memcpy (&new_imm, &imm, sizeof (float));
20083           /* But the assembly may have been written to provide an integer
20084              bit pattern that equates to a float, so check that the
20085              conversion has worked.  */
20086           if (is_quarter_float (new_imm))
20087             {
20088               if (is_quarter_float (inst.operands[1].imm))
20089                 as_warn (_("immediate constant is valid both as a bit-pattern and a floating point value (using the fp value)"));
20090
20091               inst.operands[1].imm = new_imm;
20092               inst.operands[1].immisfloat = 1;
20093             }
20094         }
20095
20096       if (is_quarter_float (inst.operands[1].imm))
20097         {
20098           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
20099           do_vfp_nsyn_opcode (ldconst);
20100
20101           /* ARMv8.2 fp16 vmov.f16 instruction.  */
20102           if (rs == NS_HI)
20103             do_scalar_fp16_v82_encode ();
20104         }
20105       else
20106         first_error (_("immediate out of range"));
20107       break;
20108
20109     case NS_RH:
20110     case NS_RF:  /* case 12 (fmrs).  */
20111       do_vfp_nsyn_opcode ("fmrs");
20112       /* ARMv8.2 fp16 vmov.f16 instruction.  */
20113       if (rs == NS_RH)
20114         do_scalar_fp16_v82_encode ();
20115       break;
20116
20117     case NS_HR:
20118     case NS_FR:  /* case 13 (fmsr).  */
20119       do_vfp_nsyn_opcode ("fmsr");
20120       /* ARMv8.2 fp16 vmov.f16 instruction.  */
20121       if (rs == NS_HR)
20122         do_scalar_fp16_v82_encode ();
20123       break;
20124
20125     case NS_RRSS:
20126       do_mve_mov (0);
20127       break;
20128     case NS_SSRR:
20129       do_mve_mov (1);
20130       break;
20131
20132     /* The encoders for the fmrrs and fmsrr instructions expect three operands
20133        (one of which is a list), but we have parsed four.  Do some fiddling to
20134        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
20135        expect.  */
20136     case NS_RRFF:  /* case 14 (fmrrs).  */
20137       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20138                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20139                   _(BAD_FPU));
20140       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
20141                   _("VFP registers must be adjacent"));
20142       inst.operands[2].imm = 2;
20143       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
20144       do_vfp_nsyn_opcode ("fmrrs");
20145       break;
20146
20147     case NS_FFRR:  /* case 15 (fmsrr).  */
20148       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
20149                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
20150                   _(BAD_FPU));
20151       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
20152                   _("VFP registers must be adjacent"));
20153       inst.operands[1] = inst.operands[2];
20154       inst.operands[2] = inst.operands[3];
20155       inst.operands[0].imm = 2;
20156       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
20157       do_vfp_nsyn_opcode ("fmsrr");
20158       break;
20159
20160     case NS_NULL:
20161       /* neon_select_shape has determined that the instruction
20162          shape is wrong and has already set the error message.  */
20163       break;
20164
20165     default:
20166       abort ();
20167     }
20168 }
20169
20170 static void
20171 do_mve_movl (void)
20172 {
20173   if (!(inst.operands[0].present && inst.operands[0].isquad
20174       && inst.operands[1].present && inst.operands[1].isquad
20175       && !inst.operands[2].present))
20176     {
20177       inst.instruction = 0;
20178       inst.cond = 0xb;
20179       if (thumb_mode)
20180         set_pred_insn_type (INSIDE_IT_INSN);
20181       do_neon_mov ();
20182       return;
20183     }
20184
20185   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20186     return;
20187
20188   if (inst.cond != COND_ALWAYS)
20189     inst.pred_insn_type = INSIDE_VPT_INSN;
20190
20191   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_S8 | N_U8
20192                                             | N_S16 | N_U16 | N_KEY);
20193
20194   inst.instruction |= (et.type == NT_unsigned) << 28;
20195   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20196   inst.instruction |= (neon_logbits (et.size) + 1) << 19;
20197   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20198   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
20199   inst.instruction |= LOW4 (inst.operands[1].reg);
20200   inst.is_neon = 1;
20201 }
20202
20203 static void
20204 do_neon_rshift_round_imm (void)
20205 {
20206   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20207    return;
20208
20209   enum neon_shape rs;
20210   struct neon_type_el et;
20211
20212   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20213     {
20214       rs = neon_select_shape (NS_QQI, NS_NULL);
20215       et = neon_check_type (2, rs, N_EQK, N_SU_MVE | N_KEY);
20216     }
20217   else
20218     {
20219       rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
20220       et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
20221     }
20222   int imm = inst.operands[2].imm;
20223
20224   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
20225   if (imm == 0)
20226     {
20227       inst.operands[2].present = 0;
20228       do_neon_mov ();
20229       return;
20230     }
20231
20232   constraint (imm < 1 || (unsigned)imm > et.size,
20233               _("immediate out of range for shift"));
20234   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
20235                   et.size - imm);
20236 }
20237
20238 static void
20239 do_neon_movhf (void)
20240 {
20241   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
20242   constraint (rs != NS_HH, _("invalid suffix"));
20243
20244   if (inst.cond != COND_ALWAYS)
20245     {
20246       if (thumb_mode)
20247         {
20248           as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
20249                      " the behaviour is UNPREDICTABLE"));
20250         }
20251       else
20252         {
20253           inst.error = BAD_COND;
20254           return;
20255         }
20256     }
20257
20258   do_vfp_sp_monadic ();
20259
20260   inst.is_neon = 1;
20261   inst.instruction |= 0xf0000000;
20262 }
20263
20264 static void
20265 do_neon_movl (void)
20266 {
20267   struct neon_type_el et = neon_check_type (2, NS_QD,
20268     N_EQK | N_DBL, N_SU_32 | N_KEY);
20269   unsigned sizebits = et.size >> 3;
20270   inst.instruction |= sizebits << 19;
20271   neon_two_same (0, et.type == NT_unsigned, -1);
20272 }
20273
20274 static void
20275 do_neon_trn (void)
20276 {
20277   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20278   struct neon_type_el et = neon_check_type (2, rs,
20279     N_EQK, N_8 | N_16 | N_32 | N_KEY);
20280   NEON_ENCODE (INTEGER, inst);
20281   neon_two_same (neon_quad (rs), 1, et.size);
20282 }
20283
20284 static void
20285 do_neon_zip_uzp (void)
20286 {
20287   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20288   struct neon_type_el et = neon_check_type (2, rs,
20289     N_EQK, N_8 | N_16 | N_32 | N_KEY);
20290   if (rs == NS_DD && et.size == 32)
20291     {
20292       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
20293       inst.instruction = N_MNEM_vtrn;
20294       do_neon_trn ();
20295       return;
20296     }
20297   neon_two_same (neon_quad (rs), 1, et.size);
20298 }
20299
20300 static void
20301 do_neon_sat_abs_neg (void)
20302 {
20303   if (!check_simd_pred_availability (FALSE, NEON_CHECK_CC | NEON_CHECK_ARCH))
20304     return;
20305
20306   enum neon_shape rs;
20307   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20308     rs = neon_select_shape (NS_QQ, NS_NULL);
20309   else
20310     rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20311   struct neon_type_el et = neon_check_type (2, rs,
20312     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
20313   neon_two_same (neon_quad (rs), 1, et.size);
20314 }
20315
20316 static void
20317 do_neon_pair_long (void)
20318 {
20319   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20320   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
20321   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
20322   inst.instruction |= (et.type == NT_unsigned) << 7;
20323   neon_two_same (neon_quad (rs), 1, et.size);
20324 }
20325
20326 static void
20327 do_neon_recip_est (void)
20328 {
20329   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20330   struct neon_type_el et = neon_check_type (2, rs,
20331     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
20332   inst.instruction |= (et.type == NT_float) << 8;
20333   neon_two_same (neon_quad (rs), 1, et.size);
20334 }
20335
20336 static void
20337 do_neon_cls (void)
20338 {
20339   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20340     return;
20341
20342   enum neon_shape rs;
20343   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20344    rs = neon_select_shape (NS_QQ, NS_NULL);
20345   else
20346    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20347
20348   struct neon_type_el et = neon_check_type (2, rs,
20349     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
20350   neon_two_same (neon_quad (rs), 1, et.size);
20351 }
20352
20353 static void
20354 do_neon_clz (void)
20355 {
20356   if (!check_simd_pred_availability (FALSE, NEON_CHECK_ARCH | NEON_CHECK_CC))
20357     return;
20358
20359   enum neon_shape rs;
20360   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20361    rs = neon_select_shape (NS_QQ, NS_NULL);
20362   else
20363    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20364
20365   struct neon_type_el et = neon_check_type (2, rs,
20366     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
20367   neon_two_same (neon_quad (rs), 1, et.size);
20368 }
20369
20370 static void
20371 do_neon_cnt (void)
20372 {
20373   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20374   struct neon_type_el et = neon_check_type (2, rs,
20375     N_EQK | N_INT, N_8 | N_KEY);
20376   neon_two_same (neon_quad (rs), 1, et.size);
20377 }
20378
20379 static void
20380 do_neon_swp (void)
20381 {
20382   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
20383   neon_two_same (neon_quad (rs), 1, -1);
20384 }
20385
20386 static void
20387 do_neon_tbl_tbx (void)
20388 {
20389   unsigned listlenbits;
20390   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
20391
20392   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
20393     {
20394       first_error (_("bad list length for table lookup"));
20395       return;
20396     }
20397
20398   listlenbits = inst.operands[1].imm - 1;
20399   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20400   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20401   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
20402   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
20403   inst.instruction |= LOW4 (inst.operands[2].reg);
20404   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
20405   inst.instruction |= listlenbits << 8;
20406
20407   neon_dp_fixup (&inst);
20408 }
20409
20410 static void
20411 do_neon_ldm_stm (void)
20412 {
20413   /* P, U and L bits are part of bitmask.  */
20414   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
20415   unsigned offsetbits = inst.operands[1].imm * 2;
20416
20417   if (inst.operands[1].issingle)
20418     {
20419       do_vfp_nsyn_ldm_stm (is_dbmode);
20420       return;
20421     }
20422
20423   constraint (is_dbmode && !inst.operands[0].writeback,
20424               _("writeback (!) must be used for VLDMDB and VSTMDB"));
20425
20426   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
20427               _("register list must contain at least 1 and at most 16 "
20428                 "registers"));
20429
20430   inst.instruction |= inst.operands[0].reg << 16;
20431   inst.instruction |= inst.operands[0].writeback << 21;
20432   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
20433   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
20434
20435   inst.instruction |= offsetbits;
20436
20437   do_vfp_cond_or_thumb ();
20438 }
20439
20440 static void
20441 do_neon_ldr_str (void)
20442 {
20443   int is_ldr = (inst.instruction & (1 << 20)) != 0;
20444
20445   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
20446      And is UNPREDICTABLE in thumb mode.  */
20447   if (!is_ldr
20448       && inst.operands[1].reg == REG_PC
20449       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
20450     {
20451       if (thumb_mode)
20452         inst.error = _("Use of PC here is UNPREDICTABLE");
20453       else if (warn_on_deprecated)
20454         as_tsktsk (_("Use of PC here is deprecated"));
20455     }
20456
20457   if (inst.operands[0].issingle)
20458     {
20459       if (is_ldr)
20460         do_vfp_nsyn_opcode ("flds");
20461       else
20462         do_vfp_nsyn_opcode ("fsts");
20463
20464       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
20465       if (inst.vectype.el[0].size == 16)
20466         do_scalar_fp16_v82_encode ();
20467     }
20468   else
20469     {
20470       if (is_ldr)
20471         do_vfp_nsyn_opcode ("fldd");
20472       else
20473         do_vfp_nsyn_opcode ("fstd");
20474     }
20475 }
20476
20477 static void
20478 do_t_vldr_vstr_sysreg (void)
20479 {
20480   int fp_vldr_bitno = 20, sysreg_vldr_bitno = 20;
20481   bfd_boolean is_vldr = ((inst.instruction & (1 << fp_vldr_bitno)) != 0);
20482
20483   /* Use of PC is UNPREDICTABLE.  */
20484   if (inst.operands[1].reg == REG_PC)
20485     inst.error = _("Use of PC here is UNPREDICTABLE");
20486
20487   if (inst.operands[1].immisreg)
20488     inst.error = _("instruction does not accept register index");
20489
20490   if (!inst.operands[1].isreg)
20491     inst.error = _("instruction does not accept PC-relative addressing");
20492
20493   if (abs (inst.operands[1].imm) >= (1 << 7))
20494     inst.error = _("immediate value out of range");
20495
20496   inst.instruction = 0xec000f80;
20497   if (is_vldr)
20498     inst.instruction |= 1 << sysreg_vldr_bitno;
20499   encode_arm_cp_address (1, TRUE, FALSE, BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM);
20500   inst.instruction |= (inst.operands[0].imm & 0x7) << 13;
20501   inst.instruction |= (inst.operands[0].imm & 0x8) << 19;
20502 }
20503
20504 static void
20505 do_vldr_vstr (void)
20506 {
20507   bfd_boolean sysreg_op = !inst.operands[0].isreg;
20508
20509   /* VLDR/VSTR (System Register).  */
20510   if (sysreg_op)
20511     {
20512       if (!mark_feature_used (&arm_ext_v8_1m_main))
20513         as_bad (_("Instruction not permitted on this architecture"));
20514
20515       do_t_vldr_vstr_sysreg ();
20516     }
20517   /* VLDR/VSTR.  */
20518   else
20519     {
20520       if (!mark_feature_used (&fpu_vfp_ext_v1xd))
20521         as_bad (_("Instruction not permitted on this architecture"));
20522       do_neon_ldr_str ();
20523     }
20524 }
20525
20526 /* "interleave" version also handles non-interleaving register VLD1/VST1
20527    instructions.  */
20528
20529 static void
20530 do_neon_ld_st_interleave (void)
20531 {
20532   struct neon_type_el et = neon_check_type (1, NS_NULL,
20533                                             N_8 | N_16 | N_32 | N_64);
20534   unsigned alignbits = 0;
20535   unsigned idx;
20536   /* The bits in this table go:
20537      0: register stride of one (0) or two (1)
20538      1,2: register list length, minus one (1, 2, 3, 4).
20539      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
20540      We use -1 for invalid entries.  */
20541   const int typetable[] =
20542     {
20543       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
20544        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
20545        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
20546        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
20547     };
20548   int typebits;
20549
20550   if (et.type == NT_invtype)
20551     return;
20552
20553   if (inst.operands[1].immisalign)
20554     switch (inst.operands[1].imm >> 8)
20555       {
20556       case 64: alignbits = 1; break;
20557       case 128:
20558         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
20559             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
20560           goto bad_alignment;
20561         alignbits = 2;
20562         break;
20563       case 256:
20564         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
20565           goto bad_alignment;
20566         alignbits = 3;
20567         break;
20568       default:
20569       bad_alignment:
20570         first_error (_("bad alignment"));
20571         return;
20572       }
20573
20574   inst.instruction |= alignbits << 4;
20575   inst.instruction |= neon_logbits (et.size) << 6;
20576
20577   /* Bits [4:6] of the immediate in a list specifier encode register stride
20578      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
20579      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
20580      up the right value for "type" in a table based on this value and the given
20581      list style, then stick it back.  */
20582   idx = ((inst.operands[0].imm >> 4) & 7)
20583         | (((inst.instruction >> 8) & 3) << 3);
20584
20585   typebits = typetable[idx];
20586
20587   constraint (typebits == -1, _("bad list type for instruction"));
20588   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
20589               BAD_EL_TYPE);
20590
20591   inst.instruction &= ~0xf00;
20592   inst.instruction |= typebits << 8;
20593 }
20594
20595 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
20596    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
20597    otherwise. The variable arguments are a list of pairs of legal (size, align)
20598    values, terminated with -1.  */
20599
20600 static int
20601 neon_alignment_bit (int size, int align, int *do_alignment, ...)
20602 {
20603   va_list ap;
20604   int result = FAIL, thissize, thisalign;
20605
20606   if (!inst.operands[1].immisalign)
20607     {
20608       *do_alignment = 0;
20609       return SUCCESS;
20610     }
20611
20612   va_start (ap, do_alignment);
20613
20614   do
20615     {
20616       thissize = va_arg (ap, int);
20617       if (thissize == -1)
20618         break;
20619       thisalign = va_arg (ap, int);
20620
20621       if (size == thissize && align == thisalign)
20622         result = SUCCESS;
20623     }
20624   while (result != SUCCESS);
20625
20626   va_end (ap);
20627
20628   if (result == SUCCESS)
20629     *do_alignment = 1;
20630   else
20631     first_error (_("unsupported alignment for instruction"));
20632
20633   return result;
20634 }
20635
20636 static void
20637 do_neon_ld_st_lane (void)
20638 {
20639   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
20640   int align_good, do_alignment = 0;
20641   int logsize = neon_logbits (et.size);
20642   int align = inst.operands[1].imm >> 8;
20643   int n = (inst.instruction >> 8) & 3;
20644   int max_el = 64 / et.size;
20645
20646   if (et.type == NT_invtype)
20647     return;
20648
20649   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
20650               _("bad list length"));
20651   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
20652               _("scalar index out of range"));
20653   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
20654               && et.size == 8,
20655               _("stride of 2 unavailable when element size is 8"));
20656
20657   switch (n)
20658     {
20659     case 0:  /* VLD1 / VST1.  */
20660       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
20661                                        32, 32, -1);
20662       if (align_good == FAIL)
20663         return;
20664       if (do_alignment)
20665         {
20666           unsigned alignbits = 0;
20667           switch (et.size)
20668             {
20669             case 16: alignbits = 0x1; break;
20670             case 32: alignbits = 0x3; break;
20671             default: ;
20672             }
20673           inst.instruction |= alignbits << 4;
20674         }
20675       break;
20676
20677     case 1:  /* VLD2 / VST2.  */
20678       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
20679                       16, 32, 32, 64, -1);
20680       if (align_good == FAIL)
20681         return;
20682       if (do_alignment)
20683         inst.instruction |= 1 << 4;
20684       break;
20685
20686     case 2:  /* VLD3 / VST3.  */
20687       constraint (inst.operands[1].immisalign,
20688                   _("can't use alignment with this instruction"));
20689       break;
20690
20691     case 3:  /* VLD4 / VST4.  */
20692       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
20693                                        16, 64, 32, 64, 32, 128, -1);
20694       if (align_good == FAIL)
20695         return;
20696       if (do_alignment)
20697         {
20698           unsigned alignbits = 0;
20699           switch (et.size)
20700             {
20701             case 8:  alignbits = 0x1; break;
20702             case 16: alignbits = 0x1; break;
20703             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
20704             default: ;
20705             }
20706           inst.instruction |= alignbits << 4;
20707         }
20708       break;
20709
20710     default: ;
20711     }
20712
20713   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
20714   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20715     inst.instruction |= 1 << (4 + logsize);
20716
20717   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
20718   inst.instruction |= logsize << 10;
20719 }
20720
20721 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
20722
20723 static void
20724 do_neon_ld_dup (void)
20725 {
20726   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
20727   int align_good, do_alignment = 0;
20728
20729   if (et.type == NT_invtype)
20730     return;
20731
20732   switch ((inst.instruction >> 8) & 3)
20733     {
20734     case 0:  /* VLD1.  */
20735       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
20736       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
20737                                        &do_alignment, 16, 16, 32, 32, -1);
20738       if (align_good == FAIL)
20739         return;
20740       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
20741         {
20742         case 1: break;
20743         case 2: inst.instruction |= 1 << 5; break;
20744         default: first_error (_("bad list length")); return;
20745         }
20746       inst.instruction |= neon_logbits (et.size) << 6;
20747       break;
20748
20749     case 1:  /* VLD2.  */
20750       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
20751                                        &do_alignment, 8, 16, 16, 32, 32, 64,
20752                                        -1);
20753       if (align_good == FAIL)
20754         return;
20755       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
20756                   _("bad list length"));
20757       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20758         inst.instruction |= 1 << 5;
20759       inst.instruction |= neon_logbits (et.size) << 6;
20760       break;
20761
20762     case 2:  /* VLD3.  */
20763       constraint (inst.operands[1].immisalign,
20764                   _("can't use alignment with this instruction"));
20765       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
20766                   _("bad list length"));
20767       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20768         inst.instruction |= 1 << 5;
20769       inst.instruction |= neon_logbits (et.size) << 6;
20770       break;
20771
20772     case 3:  /* VLD4.  */
20773       {
20774         int align = inst.operands[1].imm >> 8;
20775         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
20776                                          16, 64, 32, 64, 32, 128, -1);
20777         if (align_good == FAIL)
20778           return;
20779         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
20780                     _("bad list length"));
20781         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
20782           inst.instruction |= 1 << 5;
20783         if (et.size == 32 && align == 128)
20784           inst.instruction |= 0x3 << 6;
20785         else
20786           inst.instruction |= neon_logbits (et.size) << 6;
20787       }
20788       break;
20789
20790     default: ;
20791     }
20792
20793   inst.instruction |= do_alignment << 4;
20794 }
20795
20796 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
20797    apart from bits [11:4].  */
20798
20799 static void
20800 do_neon_ldx_stx (void)
20801 {
20802   if (inst.operands[1].isreg)
20803     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
20804
20805   switch (NEON_LANE (inst.operands[0].imm))
20806     {
20807     case NEON_INTERLEAVE_LANES:
20808       NEON_ENCODE (INTERLV, inst);
20809       do_neon_ld_st_interleave ();
20810       break;
20811
20812     case NEON_ALL_LANES:
20813       NEON_ENCODE (DUP, inst);
20814       if (inst.instruction == N_INV)
20815         {
20816           first_error ("only loads support such operands");
20817           break;
20818         }
20819       do_neon_ld_dup ();
20820       break;
20821
20822     default:
20823       NEON_ENCODE (LANE, inst);
20824       do_neon_ld_st_lane ();
20825     }
20826
20827   /* L bit comes from bit mask.  */
20828   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20829   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20830   inst.instruction |= inst.operands[1].reg << 16;
20831
20832   if (inst.operands[1].postind)
20833     {
20834       int postreg = inst.operands[1].imm & 0xf;
20835       constraint (!inst.operands[1].immisreg,
20836                   _("post-index must be a register"));
20837       constraint (postreg == 0xd || postreg == 0xf,
20838                   _("bad register for post-index"));
20839       inst.instruction |= postreg;
20840     }
20841   else
20842     {
20843       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
20844       constraint (inst.relocs[0].exp.X_op != O_constant
20845                   || inst.relocs[0].exp.X_add_number != 0,
20846                   BAD_ADDR_MODE);
20847
20848       if (inst.operands[1].writeback)
20849         {
20850           inst.instruction |= 0xd;
20851         }
20852       else
20853         inst.instruction |= 0xf;
20854     }
20855
20856   if (thumb_mode)
20857     inst.instruction |= 0xf9000000;
20858   else
20859     inst.instruction |= 0xf4000000;
20860 }
20861
20862 /* FP v8.  */
20863 static void
20864 do_vfp_nsyn_fpv8 (enum neon_shape rs)
20865 {
20866   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
20867      D register operands.  */
20868   if (neon_shape_class[rs] == SC_DOUBLE)
20869     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20870                 _(BAD_FPU));
20871
20872   NEON_ENCODE (FPV8, inst);
20873
20874   if (rs == NS_FFF || rs == NS_HHH)
20875     {
20876       do_vfp_sp_dyadic ();
20877
20878       /* ARMv8.2 fp16 instruction.  */
20879       if (rs == NS_HHH)
20880         do_scalar_fp16_v82_encode ();
20881     }
20882   else
20883     do_vfp_dp_rd_rn_rm ();
20884
20885   if (rs == NS_DDD)
20886     inst.instruction |= 0x100;
20887
20888   inst.instruction |= 0xf0000000;
20889 }
20890
20891 static void
20892 do_vsel (void)
20893 {
20894   set_pred_insn_type (OUTSIDE_PRED_INSN);
20895
20896   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
20897     first_error (_("invalid instruction shape"));
20898 }
20899
20900 static void
20901 do_vmaxnm (void)
20902 {
20903   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20904     set_pred_insn_type (OUTSIDE_PRED_INSN);
20905
20906   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
20907     return;
20908
20909   if (!check_simd_pred_availability (TRUE, NEON_CHECK_CC | NEON_CHECK_ARCH8))
20910     return;
20911
20912   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
20913 }
20914
20915 static void
20916 do_vrint_1 (enum neon_cvt_mode mode)
20917 {
20918   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
20919   struct neon_type_el et;
20920
20921   if (rs == NS_NULL)
20922     return;
20923
20924   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
20925      D register operands.  */
20926   if (neon_shape_class[rs] == SC_DOUBLE)
20927     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20928                 _(BAD_FPU));
20929
20930   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
20931                         | N_VFP);
20932   if (et.type != NT_invtype)
20933     {
20934       /* VFP encodings.  */
20935       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
20936           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
20937         set_pred_insn_type (OUTSIDE_PRED_INSN);
20938
20939       NEON_ENCODE (FPV8, inst);
20940       if (rs == NS_FF || rs == NS_HH)
20941         do_vfp_sp_monadic ();
20942       else
20943         do_vfp_dp_rd_rm ();
20944
20945       switch (mode)
20946         {
20947         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
20948         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
20949         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
20950         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
20951         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
20952         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
20953         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
20954         default: abort ();
20955         }
20956
20957       inst.instruction |= (rs == NS_DD) << 8;
20958       do_vfp_cond_or_thumb ();
20959
20960       /* ARMv8.2 fp16 vrint instruction.  */
20961       if (rs == NS_HH)
20962       do_scalar_fp16_v82_encode ();
20963     }
20964   else
20965     {
20966       /* Neon encodings (or something broken...).  */
20967       inst.error = NULL;
20968       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
20969
20970       if (et.type == NT_invtype)
20971         return;
20972
20973       if (!check_simd_pred_availability (TRUE,
20974                                          NEON_CHECK_CC | NEON_CHECK_ARCH8))
20975         return;
20976
20977       NEON_ENCODE (FLOAT, inst);
20978
20979       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
20980       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
20981       inst.instruction |= LOW4 (inst.operands[1].reg);
20982       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
20983       inst.instruction |= neon_quad (rs) << 6;
20984       /* Mask off the original size bits and reencode them.  */
20985       inst.instruction = ((inst.instruction & 0xfff3ffff)
20986                           | neon_logbits (et.size) << 18);
20987
20988       switch (mode)
20989         {
20990         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
20991         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
20992         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
20993         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
20994         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
20995         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
20996         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
20997         default: abort ();
20998         }
20999
21000       if (thumb_mode)
21001         inst.instruction |= 0xfc000000;
21002       else
21003         inst.instruction |= 0xf0000000;
21004     }
21005 }
21006
21007 static void
21008 do_vrintx (void)
21009 {
21010   do_vrint_1 (neon_cvt_mode_x);
21011 }
21012
21013 static void
21014 do_vrintz (void)
21015 {
21016   do_vrint_1 (neon_cvt_mode_z);
21017 }
21018
21019 static void
21020 do_vrintr (void)
21021 {
21022   do_vrint_1 (neon_cvt_mode_r);
21023 }
21024
21025 static void
21026 do_vrinta (void)
21027 {
21028   do_vrint_1 (neon_cvt_mode_a);
21029 }
21030
21031 static void
21032 do_vrintn (void)
21033 {
21034   do_vrint_1 (neon_cvt_mode_n);
21035 }
21036
21037 static void
21038 do_vrintp (void)
21039 {
21040   do_vrint_1 (neon_cvt_mode_p);
21041 }
21042
21043 static void
21044 do_vrintm (void)
21045 {
21046   do_vrint_1 (neon_cvt_mode_m);
21047 }
21048
21049 static unsigned
21050 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
21051 {
21052   unsigned regno = NEON_SCALAR_REG (opnd);
21053   unsigned elno = NEON_SCALAR_INDEX (opnd);
21054
21055   if (elsize == 16 && elno < 2 && regno < 16)
21056     return regno | (elno << 4);
21057   else if (elsize == 32 && elno == 0)
21058     return regno;
21059
21060   first_error (_("scalar out of range"));
21061   return 0;
21062 }
21063
21064 static void
21065 do_vcmla (void)
21066 {
21067   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext)
21068               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
21069                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
21070   constraint (inst.relocs[0].exp.X_op != O_constant,
21071               _("expression too complex"));
21072   unsigned rot = inst.relocs[0].exp.X_add_number;
21073   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
21074               _("immediate out of range"));
21075   rot /= 90;
21076
21077   if (!check_simd_pred_availability (TRUE,
21078                                      NEON_CHECK_ARCH8 | NEON_CHECK_CC))
21079     return;
21080
21081   if (inst.operands[2].isscalar)
21082     {
21083       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
21084         first_error (_("invalid instruction shape"));
21085       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
21086       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
21087                                        N_KEY | N_F16 | N_F32).size;
21088       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
21089       inst.is_neon = 1;
21090       inst.instruction = 0xfe000800;
21091       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21092       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21093       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
21094       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
21095       inst.instruction |= LOW4 (m);
21096       inst.instruction |= HI1 (m) << 5;
21097       inst.instruction |= neon_quad (rs) << 6;
21098       inst.instruction |= rot << 20;
21099       inst.instruction |= (size == 32) << 23;
21100     }
21101   else
21102     {
21103       enum neon_shape rs;
21104       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
21105         rs = neon_select_shape (NS_QQQI, NS_NULL);
21106       else
21107         rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
21108
21109       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
21110                                        N_KEY | N_F16 | N_F32).size;
21111       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext) && size == 32
21112           && (inst.operands[0].reg == inst.operands[1].reg
21113               || inst.operands[0].reg == inst.operands[2].reg))
21114         as_tsktsk (BAD_MVE_SRCDEST);
21115
21116       neon_three_same (neon_quad (rs), 0, -1);
21117       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
21118       inst.instruction |= 0xfc200800;
21119       inst.instruction |= rot << 23;
21120       inst.instruction |= (size == 32) << 20;
21121     }
21122 }
21123
21124 static void
21125 do_vcadd (void)
21126 {
21127   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
21128               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
21129                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
21130   constraint (inst.relocs[0].exp.X_op != O_constant,
21131               _("expression too complex"));
21132
21133   unsigned rot = inst.relocs[0].exp.X_add_number;
21134   constraint (rot != 90 && rot != 270, _("immediate out of range"));
21135   enum neon_shape rs;
21136   struct neon_type_el et;
21137   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
21138     {
21139       rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
21140       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32);
21141     }
21142   else
21143     {
21144       rs = neon_select_shape (NS_QQQI, NS_NULL);
21145       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32 | N_I8
21146                             | N_I16 | N_I32);
21147       if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
21148         as_tsktsk (_("Warning: 32-bit element size and same first and third "
21149                      "operand makes instruction UNPREDICTABLE"));
21150     }
21151
21152   if (et.type == NT_invtype)
21153     return;
21154
21155   if (!check_simd_pred_availability (et.type == NT_float,
21156                                      NEON_CHECK_ARCH8 | NEON_CHECK_CC))
21157     return;
21158
21159   if (et.type == NT_float)
21160     {
21161       neon_three_same (neon_quad (rs), 0, -1);
21162       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
21163       inst.instruction |= 0xfc800800;
21164       inst.instruction |= (rot == 270) << 24;
21165       inst.instruction |= (et.size == 32) << 20;
21166     }
21167   else
21168     {
21169       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
21170       inst.instruction = 0xfe000f00;
21171       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21172       inst.instruction |= neon_logbits (et.size) << 20;
21173       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
21174       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21175       inst.instruction |= (rot == 270) << 12;
21176       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
21177       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
21178       inst.instruction |= LOW4 (inst.operands[2].reg);
21179       inst.is_neon = 1;
21180     }
21181 }
21182
21183 /* Dot Product instructions encoding support.  */
21184
21185 static void
21186 do_neon_dotproduct (int unsigned_p)
21187 {
21188   enum neon_shape rs;
21189   unsigned scalar_oprd2 = 0;
21190   int high8;
21191
21192   if (inst.cond != COND_ALWAYS)
21193     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
21194                "is UNPREDICTABLE"));
21195
21196   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
21197               _(BAD_FPU));
21198
21199   /* Dot Product instructions are in three-same D/Q register format or the third
21200      operand can be a scalar index register.  */
21201   if (inst.operands[2].isscalar)
21202     {
21203       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
21204       high8 = 0xfe000000;
21205       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
21206     }
21207   else
21208     {
21209       high8 = 0xfc000000;
21210       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
21211     }
21212
21213   if (unsigned_p)
21214     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
21215   else
21216     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
21217
21218   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
21219      Product instruction, so we pass 0 as the "ubit" parameter.  And the
21220      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
21221   neon_three_same (neon_quad (rs), 0, 32);
21222
21223   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
21224      different NEON three-same encoding.  */
21225   inst.instruction &= 0x00ffffff;
21226   inst.instruction |= high8;
21227   /* Encode 'U' bit which indicates signedness.  */
21228   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
21229   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
21230      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
21231      the instruction encoding.  */
21232   if (inst.operands[2].isscalar)
21233     {
21234       inst.instruction &= 0xffffffd0;
21235       inst.instruction |= LOW4 (scalar_oprd2);
21236       inst.instruction |= HI1 (scalar_oprd2) << 5;
21237     }
21238 }
21239
21240 /* Dot Product instructions for signed integer.  */
21241
21242 static void
21243 do_neon_dotproduct_s (void)
21244 {
21245   return do_neon_dotproduct (0);
21246 }
21247
21248 /* Dot Product instructions for unsigned integer.  */
21249
21250 static void
21251 do_neon_dotproduct_u (void)
21252 {
21253   return do_neon_dotproduct (1);
21254 }
21255
21256 /* Crypto v1 instructions.  */
21257 static void
21258 do_crypto_2op_1 (unsigned elttype, int op)
21259 {
21260   set_pred_insn_type (OUTSIDE_PRED_INSN);
21261
21262   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
21263       == NT_invtype)
21264     return;
21265
21266   inst.error = NULL;
21267
21268   NEON_ENCODE (INTEGER, inst);
21269   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
21270   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
21271   inst.instruction |= LOW4 (inst.operands[1].reg);
21272   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
21273   if (op != -1)
21274     inst.instruction |= op << 6;
21275
21276   if (thumb_mode)
21277     inst.instruction |= 0xfc000000;
21278   else
21279     inst.instruction |= 0xf0000000;
21280 }
21281
21282 static void
21283 do_crypto_3op_1 (int u, int op)
21284 {
21285   set_pred_insn_type (OUTSIDE_PRED_INSN);
21286
21287   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
21288                        N_32 | N_UNT | N_KEY).type == NT_invtype)
21289     return;
21290
21291   inst.error = NULL;
21292
21293   NEON_ENCODE (INTEGER, inst);
21294   neon_three_same (1, u, 8 << op);
21295 }
21296
21297 static void
21298 do_aese (void)
21299 {
21300   do_crypto_2op_1 (N_8, 0);
21301 }
21302
21303 static void
21304 do_aesd (void)
21305 {
21306   do_crypto_2op_1 (N_8, 1);
21307 }
21308
21309 static void
21310 do_aesmc (void)
21311 {
21312   do_crypto_2op_1 (N_8, 2);
21313 }
21314
21315 static void
21316 do_aesimc (void)
21317 {
21318   do_crypto_2op_1 (N_8, 3);
21319 }
21320
21321 static void
21322 do_sha1c (void)
21323 {
21324   do_crypto_3op_1 (0, 0);
21325 }
21326
21327 static void
21328 do_sha1p (void)
21329 {
21330   do_crypto_3op_1 (0, 1);
21331 }
21332
21333 static void
21334 do_sha1m (void)
21335 {
21336   do_crypto_3op_1 (0, 2);
21337 }
21338
21339 static void
21340 do_sha1su0 (void)
21341 {
21342   do_crypto_3op_1 (0, 3);
21343 }
21344
21345 static void
21346 do_sha256h (void)
21347 {
21348   do_crypto_3op_1 (1, 0);
21349 }
21350
21351 static void
21352 do_sha256h2 (void)
21353 {
21354   do_crypto_3op_1 (1, 1);
21355 }
21356
21357 static void
21358 do_sha256su1 (void)
21359 {
21360   do_crypto_3op_1 (1, 2);
21361 }
21362
21363 static void
21364 do_sha1h (void)
21365 {
21366   do_crypto_2op_1 (N_32, -1);
21367 }
21368
21369 static void
21370 do_sha1su1 (void)
21371 {
21372   do_crypto_2op_1 (N_32, 0);
21373 }
21374
21375 static void
21376 do_sha256su0 (void)
21377 {
21378   do_crypto_2op_1 (N_32, 1);
21379 }
21380
21381 static void
21382 do_crc32_1 (unsigned int poly, unsigned int sz)
21383 {
21384   unsigned int Rd = inst.operands[0].reg;
21385   unsigned int Rn = inst.operands[1].reg;
21386   unsigned int Rm = inst.operands[2].reg;
21387
21388   set_pred_insn_type (OUTSIDE_PRED_INSN);
21389   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
21390   inst.instruction |= LOW4 (Rn) << 16;
21391   inst.instruction |= LOW4 (Rm);
21392   inst.instruction |= sz << (thumb_mode ? 4 : 21);
21393   inst.instruction |= poly << (thumb_mode ? 20 : 9);
21394
21395   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
21396     as_warn (UNPRED_REG ("r15"));
21397 }
21398
21399 static void
21400 do_crc32b (void)
21401 {
21402   do_crc32_1 (0, 0);
21403 }
21404
21405 static void
21406 do_crc32h (void)
21407 {
21408   do_crc32_1 (0, 1);
21409 }
21410
21411 static void
21412 do_crc32w (void)
21413 {
21414   do_crc32_1 (0, 2);
21415 }
21416
21417 static void
21418 do_crc32cb (void)
21419 {
21420   do_crc32_1 (1, 0);
21421 }
21422
21423 static void
21424 do_crc32ch (void)
21425 {
21426   do_crc32_1 (1, 1);
21427 }
21428
21429 static void
21430 do_crc32cw (void)
21431 {
21432   do_crc32_1 (1, 2);
21433 }
21434
21435 static void
21436 do_vjcvt (void)
21437 {
21438   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
21439               _(BAD_FPU));
21440   neon_check_type (2, NS_FD, N_S32, N_F64);
21441   do_vfp_sp_dp_cvt ();
21442   do_vfp_cond_or_thumb ();
21443 }
21444
21445 \f
21446 /* Overall per-instruction processing.  */
21447
21448 /* We need to be able to fix up arbitrary expressions in some statements.
21449    This is so that we can handle symbols that are an arbitrary distance from
21450    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
21451    which returns part of an address in a form which will be valid for
21452    a data instruction.  We do this by pushing the expression into a symbol
21453    in the expr_section, and creating a fix for that.  */
21454
21455 static void
21456 fix_new_arm (fragS *       frag,
21457              int           where,
21458              short int     size,
21459              expressionS * exp,
21460              int           pc_rel,
21461              int           reloc)
21462 {
21463   fixS *           new_fix;
21464
21465   switch (exp->X_op)
21466     {
21467     case O_constant:
21468       if (pc_rel)
21469         {
21470           /* Create an absolute valued symbol, so we have something to
21471              refer to in the object file.  Unfortunately for us, gas's
21472              generic expression parsing will already have folded out
21473              any use of .set foo/.type foo %function that may have
21474              been used to set type information of the target location,
21475              that's being specified symbolically.  We have to presume
21476              the user knows what they are doing.  */
21477           char name[16 + 8];
21478           symbolS *symbol;
21479
21480           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
21481
21482           symbol = symbol_find_or_make (name);
21483           S_SET_SEGMENT (symbol, absolute_section);
21484           symbol_set_frag (symbol, &zero_address_frag);
21485           S_SET_VALUE (symbol, exp->X_add_number);
21486           exp->X_op = O_symbol;
21487           exp->X_add_symbol = symbol;
21488           exp->X_add_number = 0;
21489         }
21490       /* FALLTHROUGH */
21491     case O_symbol:
21492     case O_add:
21493     case O_subtract:
21494       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
21495                              (enum bfd_reloc_code_real) reloc);
21496       break;
21497
21498     default:
21499       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
21500                                   pc_rel, (enum bfd_reloc_code_real) reloc);
21501       break;
21502     }
21503
21504   /* Mark whether the fix is to a THUMB instruction, or an ARM
21505      instruction.  */
21506   new_fix->tc_fix_data = thumb_mode;
21507 }
21508
21509 /* Create a frg for an instruction requiring relaxation.  */
21510 static void
21511 output_relax_insn (void)
21512 {
21513   char * to;
21514   symbolS *sym;
21515   int offset;
21516
21517   /* The size of the instruction is unknown, so tie the debug info to the
21518      start of the instruction.  */
21519   dwarf2_emit_insn (0);
21520
21521   switch (inst.relocs[0].exp.X_op)
21522     {
21523     case O_symbol:
21524       sym = inst.relocs[0].exp.X_add_symbol;
21525       offset = inst.relocs[0].exp.X_add_number;
21526       break;
21527     case O_constant:
21528       sym = NULL;
21529       offset = inst.relocs[0].exp.X_add_number;
21530       break;
21531     default:
21532       sym = make_expr_symbol (&inst.relocs[0].exp);
21533       offset = 0;
21534       break;
21535   }
21536   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
21537                  inst.relax, sym, offset, NULL/*offset, opcode*/);
21538   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
21539 }
21540
21541 /* Write a 32-bit thumb instruction to buf.  */
21542 static void
21543 put_thumb32_insn (char * buf, unsigned long insn)
21544 {
21545   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
21546   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
21547 }
21548
21549 static void
21550 output_inst (const char * str)
21551 {
21552   char * to = NULL;
21553
21554   if (inst.error)
21555     {
21556       as_bad ("%s -- `%s'", inst.error, str);
21557       return;
21558     }
21559   if (inst.relax)
21560     {
21561       output_relax_insn ();
21562       return;
21563     }
21564   if (inst.size == 0)
21565     return;
21566
21567   to = frag_more (inst.size);
21568   /* PR 9814: Record the thumb mode into the current frag so that we know
21569      what type of NOP padding to use, if necessary.  We override any previous
21570      setting so that if the mode has changed then the NOPS that we use will
21571      match the encoding of the last instruction in the frag.  */
21572   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21573
21574   if (thumb_mode && (inst.size > THUMB_SIZE))
21575     {
21576       gas_assert (inst.size == (2 * THUMB_SIZE));
21577       put_thumb32_insn (to, inst.instruction);
21578     }
21579   else if (inst.size > INSN_SIZE)
21580     {
21581       gas_assert (inst.size == (2 * INSN_SIZE));
21582       md_number_to_chars (to, inst.instruction, INSN_SIZE);
21583       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
21584     }
21585   else
21586     md_number_to_chars (to, inst.instruction, inst.size);
21587
21588   int r;
21589   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
21590     {
21591       if (inst.relocs[r].type != BFD_RELOC_UNUSED)
21592         fix_new_arm (frag_now, to - frag_now->fr_literal,
21593                      inst.size, & inst.relocs[r].exp, inst.relocs[r].pc_rel,
21594                      inst.relocs[r].type);
21595     }
21596
21597   dwarf2_emit_insn (inst.size);
21598 }
21599
21600 static char *
21601 output_it_inst (int cond, int mask, char * to)
21602 {
21603   unsigned long instruction = 0xbf00;
21604
21605   mask &= 0xf;
21606   instruction |= mask;
21607   instruction |= cond << 4;
21608
21609   if (to == NULL)
21610     {
21611       to = frag_more (2);
21612 #ifdef OBJ_ELF
21613       dwarf2_emit_insn (2);
21614 #endif
21615     }
21616
21617   md_number_to_chars (to, instruction, 2);
21618
21619   return to;
21620 }
21621
21622 /* Tag values used in struct asm_opcode's tag field.  */
21623 enum opcode_tag
21624 {
21625   OT_unconditional,     /* Instruction cannot be conditionalized.
21626                            The ARM condition field is still 0xE.  */
21627   OT_unconditionalF,    /* Instruction cannot be conditionalized
21628                            and carries 0xF in its ARM condition field.  */
21629   OT_csuffix,           /* Instruction takes a conditional suffix.  */
21630   OT_csuffixF,          /* Some forms of the instruction take a scalar
21631                            conditional suffix, others place 0xF where the
21632                            condition field would be, others take a vector
21633                            conditional suffix.  */
21634   OT_cinfix3,           /* Instruction takes a conditional infix,
21635                            beginning at character index 3.  (In
21636                            unified mode, it becomes a suffix.)  */
21637   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
21638                             tsts, cmps, cmns, and teqs. */
21639   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
21640                            character index 3, even in unified mode.  Used for
21641                            legacy instructions where suffix and infix forms
21642                            may be ambiguous.  */
21643   OT_csuf_or_in3,       /* Instruction takes either a conditional
21644                            suffix or an infix at character index 3.  */
21645   OT_odd_infix_unc,     /* This is the unconditional variant of an
21646                            instruction that takes a conditional infix
21647                            at an unusual position.  In unified mode,
21648                            this variant will accept a suffix.  */
21649   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
21650                            are the conditional variants of instructions that
21651                            take conditional infixes in unusual positions.
21652                            The infix appears at character index
21653                            (tag - OT_odd_infix_0).  These are not accepted
21654                            in unified mode.  */
21655 };
21656
21657 /* Subroutine of md_assemble, responsible for looking up the primary
21658    opcode from the mnemonic the user wrote.  STR points to the
21659    beginning of the mnemonic.
21660
21661    This is not simply a hash table lookup, because of conditional
21662    variants.  Most instructions have conditional variants, which are
21663    expressed with a _conditional affix_ to the mnemonic.  If we were
21664    to encode each conditional variant as a literal string in the opcode
21665    table, it would have approximately 20,000 entries.
21666
21667    Most mnemonics take this affix as a suffix, and in unified syntax,
21668    'most' is upgraded to 'all'.  However, in the divided syntax, some
21669    instructions take the affix as an infix, notably the s-variants of
21670    the arithmetic instructions.  Of those instructions, all but six
21671    have the infix appear after the third character of the mnemonic.
21672
21673    Accordingly, the algorithm for looking up primary opcodes given
21674    an identifier is:
21675
21676    1. Look up the identifier in the opcode table.
21677       If we find a match, go to step U.
21678
21679    2. Look up the last two characters of the identifier in the
21680       conditions table.  If we find a match, look up the first N-2
21681       characters of the identifier in the opcode table.  If we
21682       find a match, go to step CE.
21683
21684    3. Look up the fourth and fifth characters of the identifier in
21685       the conditions table.  If we find a match, extract those
21686       characters from the identifier, and look up the remaining
21687       characters in the opcode table.  If we find a match, go
21688       to step CM.
21689
21690    4. Fail.
21691
21692    U. Examine the tag field of the opcode structure, in case this is
21693       one of the six instructions with its conditional infix in an
21694       unusual place.  If it is, the tag tells us where to find the
21695       infix; look it up in the conditions table and set inst.cond
21696       accordingly.  Otherwise, this is an unconditional instruction.
21697       Again set inst.cond accordingly.  Return the opcode structure.
21698
21699   CE. Examine the tag field to make sure this is an instruction that
21700       should receive a conditional suffix.  If it is not, fail.
21701       Otherwise, set inst.cond from the suffix we already looked up,
21702       and return the opcode structure.
21703
21704   CM. Examine the tag field to make sure this is an instruction that
21705       should receive a conditional infix after the third character.
21706       If it is not, fail.  Otherwise, undo the edits to the current
21707       line of input and proceed as for case CE.  */
21708
21709 static const struct asm_opcode *
21710 opcode_lookup (char **str)
21711 {
21712   char *end, *base;
21713   char *affix;
21714   const struct asm_opcode *opcode;
21715   const struct asm_cond *cond;
21716   char save[2];
21717
21718   /* Scan up to the end of the mnemonic, which must end in white space,
21719      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
21720   for (base = end = *str; *end != '\0'; end++)
21721     if (*end == ' ' || *end == '.')
21722       break;
21723
21724   if (end == base)
21725     return NULL;
21726
21727   /* Handle a possible width suffix and/or Neon type suffix.  */
21728   if (end[0] == '.')
21729     {
21730       int offset = 2;
21731
21732       /* The .w and .n suffixes are only valid if the unified syntax is in
21733          use.  */
21734       if (unified_syntax && end[1] == 'w')
21735         inst.size_req = 4;
21736       else if (unified_syntax && end[1] == 'n')
21737         inst.size_req = 2;
21738       else
21739         offset = 0;
21740
21741       inst.vectype.elems = 0;
21742
21743       *str = end + offset;
21744
21745       if (end[offset] == '.')
21746         {
21747           /* See if we have a Neon type suffix (possible in either unified or
21748              non-unified ARM syntax mode).  */
21749           if (parse_neon_type (&inst.vectype, str) == FAIL)
21750             return NULL;
21751         }
21752       else if (end[offset] != '\0' && end[offset] != ' ')
21753         return NULL;
21754     }
21755   else
21756     *str = end;
21757
21758   /* Look for unaffixed or special-case affixed mnemonic.  */
21759   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21760                                                     end - base);
21761   if (opcode)
21762     {
21763       /* step U */
21764       if (opcode->tag < OT_odd_infix_0)
21765         {
21766           inst.cond = COND_ALWAYS;
21767           return opcode;
21768         }
21769
21770       if (warn_on_deprecated && unified_syntax)
21771         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
21772       affix = base + (opcode->tag - OT_odd_infix_0);
21773       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21774       gas_assert (cond);
21775
21776       inst.cond = cond->value;
21777       return opcode;
21778     }
21779  if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
21780    {
21781     /* Cannot have a conditional suffix on a mnemonic of less than a character.
21782      */
21783     if (end - base < 2)
21784       return NULL;
21785      affix = end - 1;
21786      cond = (const struct asm_cond *) hash_find_n (arm_vcond_hsh, affix, 1);
21787      opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21788                                                       affix - base);
21789      /* If this opcode can not be vector predicated then don't accept it with a
21790         vector predication code.  */
21791      if (opcode && !opcode->mayBeVecPred)
21792        opcode = NULL;
21793    }
21794   if (!opcode || !cond)
21795     {
21796       /* Cannot have a conditional suffix on a mnemonic of less than two
21797          characters.  */
21798       if (end - base < 3)
21799         return NULL;
21800
21801       /* Look for suffixed mnemonic.  */
21802       affix = end - 2;
21803       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21804       opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21805                                                         affix - base);
21806     }
21807
21808   if (opcode && cond)
21809     {
21810       /* step CE */
21811       switch (opcode->tag)
21812         {
21813         case OT_cinfix3_legacy:
21814           /* Ignore conditional suffixes matched on infix only mnemonics.  */
21815           break;
21816
21817         case OT_cinfix3:
21818         case OT_cinfix3_deprecated:
21819         case OT_odd_infix_unc:
21820           if (!unified_syntax)
21821             return NULL;
21822           /* Fall through.  */
21823
21824         case OT_csuffix:
21825         case OT_csuffixF:
21826         case OT_csuf_or_in3:
21827           inst.cond = cond->value;
21828           return opcode;
21829
21830         case OT_unconditional:
21831         case OT_unconditionalF:
21832           if (thumb_mode)
21833             inst.cond = cond->value;
21834           else
21835             {
21836               /* Delayed diagnostic.  */
21837               inst.error = BAD_COND;
21838               inst.cond = COND_ALWAYS;
21839             }
21840           return opcode;
21841
21842         default:
21843           return NULL;
21844         }
21845     }
21846
21847   /* Cannot have a usual-position infix on a mnemonic of less than
21848      six characters (five would be a suffix).  */
21849   if (end - base < 6)
21850     return NULL;
21851
21852   /* Look for infixed mnemonic in the usual position.  */
21853   affix = base + 3;
21854   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
21855   if (!cond)
21856     return NULL;
21857
21858   memcpy (save, affix, 2);
21859   memmove (affix, affix + 2, (end - affix) - 2);
21860   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
21861                                                     (end - base) - 2);
21862   memmove (affix + 2, affix, (end - affix) - 2);
21863   memcpy (affix, save, 2);
21864
21865   if (opcode
21866       && (opcode->tag == OT_cinfix3
21867           || opcode->tag == OT_cinfix3_deprecated
21868           || opcode->tag == OT_csuf_or_in3
21869           || opcode->tag == OT_cinfix3_legacy))
21870     {
21871       /* Step CM.  */
21872       if (warn_on_deprecated && unified_syntax
21873           && (opcode->tag == OT_cinfix3
21874               || opcode->tag == OT_cinfix3_deprecated))
21875         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
21876
21877       inst.cond = cond->value;
21878       return opcode;
21879     }
21880
21881   return NULL;
21882 }
21883
21884 /* This function generates an initial IT instruction, leaving its block
21885    virtually open for the new instructions. Eventually,
21886    the mask will be updated by now_pred_add_mask () each time
21887    a new instruction needs to be included in the IT block.
21888    Finally, the block is closed with close_automatic_it_block ().
21889    The block closure can be requested either from md_assemble (),
21890    a tencode (), or due to a label hook.  */
21891
21892 static void
21893 new_automatic_it_block (int cond)
21894 {
21895   now_pred.state = AUTOMATIC_PRED_BLOCK;
21896   now_pred.mask = 0x18;
21897   now_pred.cc = cond;
21898   now_pred.block_length = 1;
21899   mapping_state (MAP_THUMB);
21900   now_pred.insn = output_it_inst (cond, now_pred.mask, NULL);
21901   now_pred.warn_deprecated = FALSE;
21902   now_pred.insn_cond = TRUE;
21903 }
21904
21905 /* Close an automatic IT block.
21906    See comments in new_automatic_it_block ().  */
21907
21908 static void
21909 close_automatic_it_block (void)
21910 {
21911   now_pred.mask = 0x10;
21912   now_pred.block_length = 0;
21913 }
21914
21915 /* Update the mask of the current automatically-generated IT
21916    instruction. See comments in new_automatic_it_block ().  */
21917
21918 static void
21919 now_pred_add_mask (int cond)
21920 {
21921 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
21922 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
21923                                               | ((bitvalue) << (nbit)))
21924   const int resulting_bit = (cond & 1);
21925
21926   now_pred.mask &= 0xf;
21927   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
21928                                    resulting_bit,
21929                                   (5 - now_pred.block_length));
21930   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
21931                                    1,
21932                                    ((5 - now_pred.block_length) - 1));
21933   output_it_inst (now_pred.cc, now_pred.mask, now_pred.insn);
21934
21935 #undef CLEAR_BIT
21936 #undef SET_BIT_VALUE
21937 }
21938
21939 /* The IT blocks handling machinery is accessed through the these functions:
21940      it_fsm_pre_encode ()               from md_assemble ()
21941      set_pred_insn_type ()              optional, from the tencode functions
21942      set_pred_insn_type_last ()         ditto
21943      in_pred_block ()                   ditto
21944      it_fsm_post_encode ()              from md_assemble ()
21945      force_automatic_it_block_close ()  from label handling functions
21946
21947    Rationale:
21948      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
21949         initializing the IT insn type with a generic initial value depending
21950         on the inst.condition.
21951      2) During the tencode function, two things may happen:
21952         a) The tencode function overrides the IT insn type by
21953            calling either set_pred_insn_type (type) or
21954            set_pred_insn_type_last ().
21955         b) The tencode function queries the IT block state by
21956            calling in_pred_block () (i.e. to determine narrow/not narrow mode).
21957
21958         Both set_pred_insn_type and in_pred_block run the internal FSM state
21959         handling function (handle_pred_state), because: a) setting the IT insn
21960         type may incur in an invalid state (exiting the function),
21961         and b) querying the state requires the FSM to be updated.
21962         Specifically we want to avoid creating an IT block for conditional
21963         branches, so it_fsm_pre_encode is actually a guess and we can't
21964         determine whether an IT block is required until the tencode () routine
21965         has decided what type of instruction this actually it.
21966         Because of this, if set_pred_insn_type and in_pred_block have to be
21967         used, set_pred_insn_type has to be called first.
21968
21969         set_pred_insn_type_last () is a wrapper of set_pred_insn_type (type),
21970         that determines the insn IT type depending on the inst.cond code.
21971         When a tencode () routine encodes an instruction that can be
21972         either outside an IT block, or, in the case of being inside, has to be
21973         the last one, set_pred_insn_type_last () will determine the proper
21974         IT instruction type based on the inst.cond code. Otherwise,
21975         set_pred_insn_type can be called for overriding that logic or
21976         for covering other cases.
21977
21978         Calling handle_pred_state () may not transition the IT block state to
21979         OUTSIDE_PRED_BLOCK immediately, since the (current) state could be
21980         still queried. Instead, if the FSM determines that the state should
21981         be transitioned to OUTSIDE_PRED_BLOCK, a flag is marked to be closed
21982         after the tencode () function: that's what it_fsm_post_encode () does.
21983
21984         Since in_pred_block () calls the state handling function to get an
21985         updated state, an error may occur (due to invalid insns combination).
21986         In that case, inst.error is set.
21987         Therefore, inst.error has to be checked after the execution of
21988         the tencode () routine.
21989
21990      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
21991         any pending state change (if any) that didn't take place in
21992         handle_pred_state () as explained above.  */
21993
21994 static void
21995 it_fsm_pre_encode (void)
21996 {
21997   if (inst.cond != COND_ALWAYS)
21998     inst.pred_insn_type =  INSIDE_IT_INSN;
21999   else
22000     inst.pred_insn_type = OUTSIDE_PRED_INSN;
22001
22002   now_pred.state_handled = 0;
22003 }
22004
22005 /* IT state FSM handling function.  */
22006 /* MVE instructions and non-MVE instructions are handled differently because of
22007    the introduction of VPT blocks.
22008    Specifications say that any non-MVE instruction inside a VPT block is
22009    UNPREDICTABLE, with the exception of the BKPT instruction.  Whereas most MVE
22010    instructions are deemed to be UNPREDICTABLE if inside an IT block.  For the
22011    few exceptions we have MVE_UNPREDICABLE_INSN.
22012    The error messages provided depending on the different combinations possible
22013    are described in the cases below:
22014    For 'most' MVE instructions:
22015    1) In an IT block, with an IT code: syntax error
22016    2) In an IT block, with a VPT code: error: must be in a VPT block
22017    3) In an IT block, with no code: warning: UNPREDICTABLE
22018    4) In a VPT block, with an IT code: syntax error
22019    5) In a VPT block, with a VPT code: OK!
22020    6) In a VPT block, with no code: error: missing code
22021    7) Outside a pred block, with an IT code: error: syntax error
22022    8) Outside a pred block, with a VPT code: error: should be in a VPT block
22023    9) Outside a pred block, with no code: OK!
22024    For non-MVE instructions:
22025    10) In an IT block, with an IT code: OK!
22026    11) In an IT block, with a VPT code: syntax error
22027    12) In an IT block, with no code: error: missing code
22028    13) In a VPT block, with an IT code: error: should be in an IT block
22029    14) In a VPT block, with a VPT code: syntax error
22030    15) In a VPT block, with no code: UNPREDICTABLE
22031    16) Outside a pred block, with an IT code: error: should be in an IT block
22032    17) Outside a pred block, with a VPT code: syntax error
22033    18) Outside a pred block, with no code: OK!
22034  */
22035
22036
22037 static int
22038 handle_pred_state (void)
22039 {
22040   now_pred.state_handled = 1;
22041   now_pred.insn_cond = FALSE;
22042
22043   switch (now_pred.state)
22044     {
22045     case OUTSIDE_PRED_BLOCK:
22046       switch (inst.pred_insn_type)
22047         {
22048         case MVE_UNPREDICABLE_INSN:
22049         case MVE_OUTSIDE_PRED_INSN:
22050           if (inst.cond < COND_ALWAYS)
22051             {
22052               /* Case 7: Outside a pred block, with an IT code: error: syntax
22053                  error.  */
22054               inst.error = BAD_SYNTAX;
22055               return FAIL;
22056             }
22057           /* Case 9:  Outside a pred block, with no code: OK!  */
22058           break;
22059         case OUTSIDE_PRED_INSN:
22060           if (inst.cond > COND_ALWAYS)
22061             {
22062               /* Case 17:  Outside a pred block, with a VPT code: syntax error.
22063                */
22064               inst.error = BAD_SYNTAX;
22065               return FAIL;
22066             }
22067           /* Case 18: Outside a pred block, with no code: OK!  */
22068           break;
22069
22070         case INSIDE_VPT_INSN:
22071           /* Case 8: Outside a pred block, with a VPT code: error: should be in
22072              a VPT block.  */
22073           inst.error = BAD_OUT_VPT;
22074           return FAIL;
22075
22076         case INSIDE_IT_INSN:
22077         case INSIDE_IT_LAST_INSN:
22078           if (inst.cond < COND_ALWAYS)
22079             {
22080               /* Case 16: Outside a pred block, with an IT code: error: should
22081                  be in an IT block.  */
22082               if (thumb_mode == 0)
22083                 {
22084                   if (unified_syntax
22085                       && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
22086                     as_tsktsk (_("Warning: conditional outside an IT block"\
22087                                  " for Thumb."));
22088                 }
22089               else
22090                 {
22091                   if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
22092                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
22093                     {
22094                       /* Automatically generate the IT instruction.  */
22095                       new_automatic_it_block (inst.cond);
22096                       if (inst.pred_insn_type == INSIDE_IT_LAST_INSN)
22097                         close_automatic_it_block ();
22098                     }
22099                   else
22100                     {
22101                       inst.error = BAD_OUT_IT;
22102                       return FAIL;
22103                     }
22104                 }
22105               break;
22106             }
22107           else if (inst.cond > COND_ALWAYS)
22108             {
22109               /* Case 17: Outside a pred block, with a VPT code: syntax error.
22110                */
22111               inst.error = BAD_SYNTAX;
22112               return FAIL;
22113             }
22114           else
22115             gas_assert (0);
22116         case IF_INSIDE_IT_LAST_INSN:
22117         case NEUTRAL_IT_INSN:
22118           break;
22119
22120         case VPT_INSN:
22121           if (inst.cond != COND_ALWAYS)
22122             first_error (BAD_SYNTAX);
22123           now_pred.state = MANUAL_PRED_BLOCK;
22124           now_pred.block_length = 0;
22125           now_pred.type = VECTOR_PRED;
22126           now_pred.cc = 0;
22127           break;
22128         case IT_INSN:
22129           now_pred.state = MANUAL_PRED_BLOCK;
22130           now_pred.block_length = 0;
22131           now_pred.type = SCALAR_PRED;
22132           break;
22133         }
22134       break;
22135
22136     case AUTOMATIC_PRED_BLOCK:
22137       /* Three things may happen now:
22138          a) We should increment current it block size;
22139          b) We should close current it block (closing insn or 4 insns);
22140          c) We should close current it block and start a new one (due
22141          to incompatible conditions or
22142          4 insns-length block reached).  */
22143
22144       switch (inst.pred_insn_type)
22145         {
22146         case INSIDE_VPT_INSN:
22147         case VPT_INSN:
22148         case MVE_UNPREDICABLE_INSN:
22149         case MVE_OUTSIDE_PRED_INSN:
22150           gas_assert (0);
22151         case OUTSIDE_PRED_INSN:
22152           /* The closure of the block shall happen immediately,
22153              so any in_pred_block () call reports the block as closed.  */
22154           force_automatic_it_block_close ();
22155           break;
22156
22157         case INSIDE_IT_INSN:
22158         case INSIDE_IT_LAST_INSN:
22159         case IF_INSIDE_IT_LAST_INSN:
22160           now_pred.block_length++;
22161
22162           if (now_pred.block_length > 4
22163               || !now_pred_compatible (inst.cond))
22164             {
22165               force_automatic_it_block_close ();
22166               if (inst.pred_insn_type != IF_INSIDE_IT_LAST_INSN)
22167                 new_automatic_it_block (inst.cond);
22168             }
22169           else
22170             {
22171               now_pred.insn_cond = TRUE;
22172               now_pred_add_mask (inst.cond);
22173             }
22174
22175           if (now_pred.state == AUTOMATIC_PRED_BLOCK
22176               && (inst.pred_insn_type == INSIDE_IT_LAST_INSN
22177                   || inst.pred_insn_type == IF_INSIDE_IT_LAST_INSN))
22178             close_automatic_it_block ();
22179           break;
22180
22181         case NEUTRAL_IT_INSN:
22182           now_pred.block_length++;
22183           now_pred.insn_cond = TRUE;
22184
22185           if (now_pred.block_length > 4)
22186             force_automatic_it_block_close ();
22187           else
22188             now_pred_add_mask (now_pred.cc & 1);
22189           break;
22190
22191         case IT_INSN:
22192           close_automatic_it_block ();
22193           now_pred.state = MANUAL_PRED_BLOCK;
22194           break;
22195         }
22196       break;
22197
22198     case MANUAL_PRED_BLOCK:
22199       {
22200         int cond, is_last;
22201         if (now_pred.type == SCALAR_PRED)
22202           {
22203             /* Check conditional suffixes.  */
22204             cond = now_pred.cc ^ ((now_pred.mask >> 4) & 1) ^ 1;
22205             now_pred.mask <<= 1;
22206             now_pred.mask &= 0x1f;
22207             is_last = (now_pred.mask == 0x10);
22208           }
22209         else
22210           {
22211             now_pred.cc ^= (now_pred.mask >> 4);
22212             cond = now_pred.cc + 0xf;
22213             now_pred.mask <<= 1;
22214             now_pred.mask &= 0x1f;
22215             is_last = now_pred.mask == 0x10;
22216           }
22217         now_pred.insn_cond = TRUE;
22218
22219         switch (inst.pred_insn_type)
22220           {
22221           case OUTSIDE_PRED_INSN:
22222             if (now_pred.type == SCALAR_PRED)
22223               {
22224                 if (inst.cond == COND_ALWAYS)
22225                   {
22226                     /* Case 12: In an IT block, with no code: error: missing
22227                        code.  */
22228                     inst.error = BAD_NOT_IT;
22229                     return FAIL;
22230                   }
22231                 else if (inst.cond > COND_ALWAYS)
22232                   {
22233                     /* Case 11: In an IT block, with a VPT code: syntax error.
22234                      */
22235                     inst.error = BAD_SYNTAX;
22236                     return FAIL;
22237                   }
22238                 else if (thumb_mode)
22239                   {
22240                     /* This is for some special cases where a non-MVE
22241                        instruction is not allowed in an IT block, such as cbz,
22242                        but are put into one with a condition code.
22243                        You could argue this should be a syntax error, but we
22244                        gave the 'not allowed in IT block' diagnostic in the
22245                        past so we will keep doing so.  */
22246                     inst.error = BAD_NOT_IT;
22247                     return FAIL;
22248                   }
22249                 break;
22250               }
22251             else
22252               {
22253                 /* Case 15: In a VPT block, with no code: UNPREDICTABLE.  */
22254                 as_tsktsk (MVE_NOT_VPT);
22255                 return SUCCESS;
22256               }
22257           case MVE_OUTSIDE_PRED_INSN:
22258             if (now_pred.type == SCALAR_PRED)
22259               {
22260                 if (inst.cond == COND_ALWAYS)
22261                   {
22262                     /* Case 3: In an IT block, with no code: warning:
22263                        UNPREDICTABLE.  */
22264                     as_tsktsk (MVE_NOT_IT);
22265                     return SUCCESS;
22266                   }
22267                 else if (inst.cond < COND_ALWAYS)
22268                   {
22269                     /* Case 1: In an IT block, with an IT code: syntax error.
22270                      */
22271                     inst.error = BAD_SYNTAX;
22272                     return FAIL;
22273                   }
22274                 else
22275                   gas_assert (0);
22276               }
22277             else
22278               {
22279                 if (inst.cond < COND_ALWAYS)
22280                   {
22281                     /* Case 4: In a VPT block, with an IT code: syntax error.
22282                      */
22283                     inst.error = BAD_SYNTAX;
22284                     return FAIL;
22285                   }
22286                 else if (inst.cond == COND_ALWAYS)
22287                   {
22288                     /* Case 6: In a VPT block, with no code: error: missing
22289                        code.  */
22290                     inst.error = BAD_NOT_VPT;
22291                     return FAIL;
22292                   }
22293                 else
22294                   {
22295                     gas_assert (0);
22296                   }
22297               }
22298           case MVE_UNPREDICABLE_INSN:
22299             as_tsktsk (now_pred.type == SCALAR_PRED ? MVE_NOT_IT : MVE_NOT_VPT);
22300             return SUCCESS;
22301           case INSIDE_IT_INSN:
22302             if (inst.cond > COND_ALWAYS)
22303               {
22304                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
22305                 /* Case 14: In a VPT block, with a VPT code: syntax error.  */
22306                 inst.error = BAD_SYNTAX;
22307                 return FAIL;
22308               }
22309             else if (now_pred.type == SCALAR_PRED)
22310               {
22311                 /* Case 10: In an IT block, with an IT code: OK!  */
22312                 if (cond != inst.cond)
22313                   {
22314                     inst.error = now_pred.type == SCALAR_PRED ? BAD_IT_COND :
22315                       BAD_VPT_COND;
22316                     return FAIL;
22317                   }
22318               }
22319             else
22320               {
22321                 /* Case 13: In a VPT block, with an IT code: error: should be
22322                    in an IT block.  */
22323                 inst.error = BAD_OUT_IT;
22324                 return FAIL;
22325               }
22326             break;
22327
22328           case INSIDE_VPT_INSN:
22329             if (now_pred.type == SCALAR_PRED)
22330               {
22331                 /* Case 2: In an IT block, with a VPT code: error: must be in a
22332                    VPT block.  */
22333                 inst.error = BAD_OUT_VPT;
22334                 return FAIL;
22335               }
22336             /* Case 5:  In a VPT block, with a VPT code: OK!  */
22337             else if (cond != inst.cond)
22338               {
22339                 inst.error = BAD_VPT_COND;
22340                 return FAIL;
22341               }
22342             break;
22343           case INSIDE_IT_LAST_INSN:
22344           case IF_INSIDE_IT_LAST_INSN:
22345             if (now_pred.type == VECTOR_PRED || inst.cond > COND_ALWAYS)
22346               {
22347                 /* Case 4: In a VPT block, with an IT code: syntax error.  */
22348                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
22349                 inst.error = BAD_SYNTAX;
22350                 return FAIL;
22351               }
22352             else if (cond != inst.cond)
22353               {
22354                 inst.error = BAD_IT_COND;
22355                 return FAIL;
22356               }
22357             if (!is_last)
22358               {
22359                 inst.error = BAD_BRANCH;
22360                 return FAIL;
22361               }
22362             break;
22363
22364           case NEUTRAL_IT_INSN:
22365             /* The BKPT instruction is unconditional even in a IT or VPT
22366                block.  */
22367             break;
22368
22369           case IT_INSN:
22370             if (now_pred.type == SCALAR_PRED)
22371               {
22372                 inst.error = BAD_IT_IT;
22373                 return FAIL;
22374               }
22375             /* fall through.  */
22376           case VPT_INSN:
22377             if (inst.cond == COND_ALWAYS)
22378               {
22379                 /* Executing a VPT/VPST instruction inside an IT block or a
22380                    VPT/VPST/IT instruction inside a VPT block is UNPREDICTABLE.
22381                  */
22382                 if (now_pred.type == SCALAR_PRED)
22383                   as_tsktsk (MVE_NOT_IT);
22384                 else
22385                   as_tsktsk (MVE_NOT_VPT);
22386                 return SUCCESS;
22387               }
22388             else
22389               {
22390                 /* VPT/VPST do not accept condition codes.  */
22391                 inst.error = BAD_SYNTAX;
22392                 return FAIL;
22393               }
22394           }
22395         }
22396       break;
22397     }
22398
22399   return SUCCESS;
22400 }
22401
22402 struct depr_insn_mask
22403 {
22404   unsigned long pattern;
22405   unsigned long mask;
22406   const char* description;
22407 };
22408
22409 /* List of 16-bit instruction patterns deprecated in an IT block in
22410    ARMv8.  */
22411 static const struct depr_insn_mask depr_it_insns[] = {
22412   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
22413   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
22414   { 0xa000, 0xb800, N_("ADR") },
22415   { 0x4800, 0xf800, N_("Literal loads") },
22416   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
22417   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
22418   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
22419      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
22420   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
22421   { 0, 0, NULL }
22422 };
22423
22424 static void
22425 it_fsm_post_encode (void)
22426 {
22427   int is_last;
22428
22429   if (!now_pred.state_handled)
22430     handle_pred_state ();
22431
22432   if (now_pred.insn_cond
22433       && !now_pred.warn_deprecated
22434       && warn_on_deprecated
22435       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8)
22436       && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_m))
22437     {
22438       if (inst.instruction >= 0x10000)
22439         {
22440           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
22441                      "performance deprecated in ARMv8-A and ARMv8-R"));
22442           now_pred.warn_deprecated = TRUE;
22443         }
22444       else
22445         {
22446           const struct depr_insn_mask *p = depr_it_insns;
22447
22448           while (p->mask != 0)
22449             {
22450               if ((inst.instruction & p->mask) == p->pattern)
22451                 {
22452                   as_tsktsk (_("IT blocks containing 16-bit Thumb "
22453                                "instructions of the following class are "
22454                                "performance deprecated in ARMv8-A and "
22455                                "ARMv8-R: %s"), p->description);
22456                   now_pred.warn_deprecated = TRUE;
22457                   break;
22458                 }
22459
22460               ++p;
22461             }
22462         }
22463
22464       if (now_pred.block_length > 1)
22465         {
22466           as_tsktsk (_("IT blocks containing more than one conditional "
22467                      "instruction are performance deprecated in ARMv8-A and "
22468                      "ARMv8-R"));
22469           now_pred.warn_deprecated = TRUE;
22470         }
22471     }
22472
22473     is_last = (now_pred.mask == 0x10);
22474     if (is_last)
22475       {
22476         now_pred.state = OUTSIDE_PRED_BLOCK;
22477         now_pred.mask = 0;
22478       }
22479 }
22480
22481 static void
22482 force_automatic_it_block_close (void)
22483 {
22484   if (now_pred.state == AUTOMATIC_PRED_BLOCK)
22485     {
22486       close_automatic_it_block ();
22487       now_pred.state = OUTSIDE_PRED_BLOCK;
22488       now_pred.mask = 0;
22489     }
22490 }
22491
22492 static int
22493 in_pred_block (void)
22494 {
22495   if (!now_pred.state_handled)
22496     handle_pred_state ();
22497
22498   return now_pred.state != OUTSIDE_PRED_BLOCK;
22499 }
22500
22501 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
22502    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
22503    here, hence the "known" in the function name.  */
22504
22505 static bfd_boolean
22506 known_t32_only_insn (const struct asm_opcode *opcode)
22507 {
22508   /* Original Thumb-1 wide instruction.  */
22509   if (opcode->tencode == do_t_blx
22510       || opcode->tencode == do_t_branch23
22511       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
22512       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
22513     return TRUE;
22514
22515   /* Wide-only instruction added to ARMv8-M Baseline.  */
22516   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
22517       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
22518       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
22519       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
22520     return TRUE;
22521
22522   return FALSE;
22523 }
22524
22525 /* Whether wide instruction variant can be used if available for a valid OPCODE
22526    in ARCH.  */
22527
22528 static bfd_boolean
22529 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
22530 {
22531   if (known_t32_only_insn (opcode))
22532     return TRUE;
22533
22534   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
22535      of variant T3 of B.W is checked in do_t_branch.  */
22536   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
22537       && opcode->tencode == do_t_branch)
22538     return TRUE;
22539
22540   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
22541   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
22542       && opcode->tencode == do_t_mov_cmp
22543       /* Make sure CMP instruction is not affected.  */
22544       && opcode->aencode == do_mov)
22545     return TRUE;
22546
22547   /* Wide instruction variants of all instructions with narrow *and* wide
22548      variants become available with ARMv6t2.  Other opcodes are either
22549      narrow-only or wide-only and are thus available if OPCODE is valid.  */
22550   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
22551     return TRUE;
22552
22553   /* OPCODE with narrow only instruction variant or wide variant not
22554      available.  */
22555   return FALSE;
22556 }
22557
22558 void
22559 md_assemble (char *str)
22560 {
22561   char *p = str;
22562   const struct asm_opcode * opcode;
22563
22564   /* Align the previous label if needed.  */
22565   if (last_label_seen != NULL)
22566     {
22567       symbol_set_frag (last_label_seen, frag_now);
22568       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
22569       S_SET_SEGMENT (last_label_seen, now_seg);
22570     }
22571
22572   memset (&inst, '\0', sizeof (inst));
22573   int r;
22574   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
22575     inst.relocs[r].type = BFD_RELOC_UNUSED;
22576
22577   opcode = opcode_lookup (&p);
22578   if (!opcode)
22579     {
22580       /* It wasn't an instruction, but it might be a register alias of
22581          the form alias .req reg, or a Neon .dn/.qn directive.  */
22582       if (! create_register_alias (str, p)
22583           && ! create_neon_reg_alias (str, p))
22584         as_bad (_("bad instruction `%s'"), str);
22585
22586       return;
22587     }
22588
22589   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
22590     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
22591
22592   /* The value which unconditional instructions should have in place of the
22593      condition field.  */
22594   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
22595
22596   if (thumb_mode)
22597     {
22598       arm_feature_set variant;
22599
22600       variant = cpu_variant;
22601       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
22602       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
22603         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
22604       /* Check that this instruction is supported for this CPU.  */
22605       if (!opcode->tvariant
22606           || (thumb_mode == 1
22607               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
22608         {
22609           if (opcode->tencode == do_t_swi)
22610             as_bad (_("SVC is not permitted on this architecture"));
22611           else
22612             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
22613           return;
22614         }
22615       if (inst.cond != COND_ALWAYS && !unified_syntax
22616           && opcode->tencode != do_t_branch)
22617         {
22618           as_bad (_("Thumb does not support conditional execution"));
22619           return;
22620         }
22621
22622       /* Two things are addressed here:
22623          1) Implicit require narrow instructions on Thumb-1.
22624             This avoids relaxation accidentally introducing Thumb-2
22625             instructions.
22626          2) Reject wide instructions in non Thumb-2 cores.
22627
22628          Only instructions with narrow and wide variants need to be handled
22629          but selecting all non wide-only instructions is easier.  */
22630       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
22631           && !t32_insn_ok (variant, opcode))
22632         {
22633           if (inst.size_req == 0)
22634             inst.size_req = 2;
22635           else if (inst.size_req == 4)
22636             {
22637               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
22638                 as_bad (_("selected processor does not support 32bit wide "
22639                           "variant of instruction `%s'"), str);
22640               else
22641                 as_bad (_("selected processor does not support `%s' in "
22642                           "Thumb-2 mode"), str);
22643               return;
22644             }
22645         }
22646
22647       inst.instruction = opcode->tvalue;
22648
22649       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
22650         {
22651           /* Prepare the pred_insn_type for those encodings that don't set
22652              it.  */
22653           it_fsm_pre_encode ();
22654
22655           opcode->tencode ();
22656
22657           it_fsm_post_encode ();
22658         }
22659
22660       if (!(inst.error || inst.relax))
22661         {
22662           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
22663           inst.size = (inst.instruction > 0xffff ? 4 : 2);
22664           if (inst.size_req && inst.size_req != inst.size)
22665             {
22666               as_bad (_("cannot honor width suffix -- `%s'"), str);
22667               return;
22668             }
22669         }
22670
22671       /* Something has gone badly wrong if we try to relax a fixed size
22672          instruction.  */
22673       gas_assert (inst.size_req == 0 || !inst.relax);
22674
22675       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
22676                               *opcode->tvariant);
22677       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
22678          set those bits when Thumb-2 32-bit instructions are seen.  The impact
22679          of relaxable instructions will be considered later after we finish all
22680          relaxation.  */
22681       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
22682         variant = arm_arch_none;
22683       else
22684         variant = cpu_variant;
22685       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
22686         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
22687                                 arm_ext_v6t2);
22688
22689       check_neon_suffixes;
22690
22691       if (!inst.error)
22692         {
22693           mapping_state (MAP_THUMB);
22694         }
22695     }
22696   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
22697     {
22698       bfd_boolean is_bx;
22699
22700       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
22701       is_bx = (opcode->aencode == do_bx);
22702
22703       /* Check that this instruction is supported for this CPU.  */
22704       if (!(is_bx && fix_v4bx)
22705           && !(opcode->avariant &&
22706                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
22707         {
22708           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
22709           return;
22710         }
22711       if (inst.size_req)
22712         {
22713           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
22714           return;
22715         }
22716
22717       inst.instruction = opcode->avalue;
22718       if (opcode->tag == OT_unconditionalF)
22719         inst.instruction |= 0xFU << 28;
22720       else
22721         inst.instruction |= inst.cond << 28;
22722       inst.size = INSN_SIZE;
22723       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
22724         {
22725           it_fsm_pre_encode ();
22726           opcode->aencode ();
22727           it_fsm_post_encode ();
22728         }
22729       /* Arm mode bx is marked as both v4T and v5 because it's still required
22730          on a hypothetical non-thumb v5 core.  */
22731       if (is_bx)
22732         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
22733       else
22734         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
22735                                 *opcode->avariant);
22736
22737       check_neon_suffixes;
22738
22739       if (!inst.error)
22740         {
22741           mapping_state (MAP_ARM);
22742         }
22743     }
22744   else
22745     {
22746       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
22747                 "-- `%s'"), str);
22748       return;
22749     }
22750   output_inst (str);
22751 }
22752
22753 static void
22754 check_pred_blocks_finished (void)
22755 {
22756 #ifdef OBJ_ELF
22757   asection *sect;
22758
22759   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
22760     if (seg_info (sect)->tc_segment_info_data.current_pred.state
22761         == MANUAL_PRED_BLOCK)
22762       {
22763         if (now_pred.type == SCALAR_PRED)
22764           as_warn (_("section '%s' finished with an open IT block."),
22765                    sect->name);
22766         else
22767           as_warn (_("section '%s' finished with an open VPT/VPST block."),
22768                    sect->name);
22769       }
22770 #else
22771   if (now_pred.state == MANUAL_PRED_BLOCK)
22772     {
22773       if (now_pred.type == SCALAR_PRED)
22774        as_warn (_("file finished with an open IT block."));
22775       else
22776         as_warn (_("file finished with an open VPT/VPST block."));
22777     }
22778 #endif
22779 }
22780
22781 /* Various frobbings of labels and their addresses.  */
22782
22783 void
22784 arm_start_line_hook (void)
22785 {
22786   last_label_seen = NULL;
22787 }
22788
22789 void
22790 arm_frob_label (symbolS * sym)
22791 {
22792   last_label_seen = sym;
22793
22794   ARM_SET_THUMB (sym, thumb_mode);
22795
22796 #if defined OBJ_COFF || defined OBJ_ELF
22797   ARM_SET_INTERWORK (sym, support_interwork);
22798 #endif
22799
22800   force_automatic_it_block_close ();
22801
22802   /* Note - do not allow local symbols (.Lxxx) to be labelled
22803      as Thumb functions.  This is because these labels, whilst
22804      they exist inside Thumb code, are not the entry points for
22805      possible ARM->Thumb calls.  Also, these labels can be used
22806      as part of a computed goto or switch statement.  eg gcc
22807      can generate code that looks like this:
22808
22809                 ldr  r2, [pc, .Laaa]
22810                 lsl  r3, r3, #2
22811                 ldr  r2, [r3, r2]
22812                 mov  pc, r2
22813
22814        .Lbbb:  .word .Lxxx
22815        .Lccc:  .word .Lyyy
22816        ..etc...
22817        .Laaa:   .word Lbbb
22818
22819      The first instruction loads the address of the jump table.
22820      The second instruction converts a table index into a byte offset.
22821      The third instruction gets the jump address out of the table.
22822      The fourth instruction performs the jump.
22823
22824      If the address stored at .Laaa is that of a symbol which has the
22825      Thumb_Func bit set, then the linker will arrange for this address
22826      to have the bottom bit set, which in turn would mean that the
22827      address computation performed by the third instruction would end
22828      up with the bottom bit set.  Since the ARM is capable of unaligned
22829      word loads, the instruction would then load the incorrect address
22830      out of the jump table, and chaos would ensue.  */
22831   if (label_is_thumb_function_name
22832       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
22833       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
22834     {
22835       /* When the address of a Thumb function is taken the bottom
22836          bit of that address should be set.  This will allow
22837          interworking between Arm and Thumb functions to work
22838          correctly.  */
22839
22840       THUMB_SET_FUNC (sym, 1);
22841
22842       label_is_thumb_function_name = FALSE;
22843     }
22844
22845   dwarf2_emit_label (sym);
22846 }
22847
22848 bfd_boolean
22849 arm_data_in_code (void)
22850 {
22851   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
22852     {
22853       *input_line_pointer = '/';
22854       input_line_pointer += 5;
22855       *input_line_pointer = 0;
22856       return TRUE;
22857     }
22858
22859   return FALSE;
22860 }
22861
22862 char *
22863 arm_canonicalize_symbol_name (char * name)
22864 {
22865   int len;
22866
22867   if (thumb_mode && (len = strlen (name)) > 5
22868       && streq (name + len - 5, "/data"))
22869     *(name + len - 5) = 0;
22870
22871   return name;
22872 }
22873 \f
22874 /* Table of all register names defined by default.  The user can
22875    define additional names with .req.  Note that all register names
22876    should appear in both upper and lowercase variants.  Some registers
22877    also have mixed-case names.  */
22878
22879 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
22880 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
22881 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
22882 #define REGSET(p,t) \
22883   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
22884   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
22885   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
22886   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
22887 #define REGSETH(p,t) \
22888   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
22889   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
22890   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
22891   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
22892 #define REGSET2(p,t) \
22893   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
22894   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
22895   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
22896   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
22897 #define SPLRBANK(base,bank,t) \
22898   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
22899   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
22900   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
22901   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
22902   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
22903   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
22904
22905 static const struct reg_entry reg_names[] =
22906 {
22907   /* ARM integer registers.  */
22908   REGSET(r, RN), REGSET(R, RN),
22909
22910   /* ATPCS synonyms.  */
22911   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
22912   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
22913   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
22914
22915   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
22916   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
22917   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
22918
22919   /* Well-known aliases.  */
22920   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
22921   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
22922
22923   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
22924   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
22925
22926   /* Defining the new Zero register from ARMv8.1-M.  */
22927   REGDEF(zr,15,ZR),
22928   REGDEF(ZR,15,ZR),
22929
22930   /* Coprocessor numbers.  */
22931   REGSET(p, CP), REGSET(P, CP),
22932
22933   /* Coprocessor register numbers.  The "cr" variants are for backward
22934      compatibility.  */
22935   REGSET(c,  CN), REGSET(C, CN),
22936   REGSET(cr, CN), REGSET(CR, CN),
22937
22938   /* ARM banked registers.  */
22939   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
22940   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
22941   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
22942   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
22943   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
22944   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
22945   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
22946
22947   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
22948   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
22949   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
22950   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
22951   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
22952   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
22953   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
22954   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
22955
22956   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
22957   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
22958   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
22959   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
22960   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
22961   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
22962   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
22963   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
22964   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
22965
22966   /* FPA registers.  */
22967   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
22968   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
22969
22970   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
22971   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
22972
22973   /* VFP SP registers.  */
22974   REGSET(s,VFS),  REGSET(S,VFS),
22975   REGSETH(s,VFS), REGSETH(S,VFS),
22976
22977   /* VFP DP Registers.  */
22978   REGSET(d,VFD),  REGSET(D,VFD),
22979   /* Extra Neon DP registers.  */
22980   REGSETH(d,VFD), REGSETH(D,VFD),
22981
22982   /* Neon QP registers.  */
22983   REGSET2(q,NQ),  REGSET2(Q,NQ),
22984
22985   /* VFP control registers.  */
22986   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
22987   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
22988   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
22989   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
22990   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
22991   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
22992   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
22993   REGDEF(fpscr_nzcvqc,2,VFC), REGDEF(FPSCR_nzcvqc,2,VFC),
22994   REGDEF(vpr,12,VFC), REGDEF(VPR,12,VFC),
22995   REGDEF(fpcxt_ns,14,VFC), REGDEF(FPCXT_NS,14,VFC),
22996   REGDEF(fpcxt_s,15,VFC), REGDEF(FPCXT_S,15,VFC),
22997
22998   /* Maverick DSP coprocessor registers.  */
22999   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
23000   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
23001
23002   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
23003   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
23004   REGDEF(dspsc,0,DSPSC),
23005
23006   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
23007   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
23008   REGDEF(DSPSC,0,DSPSC),
23009
23010   /* iWMMXt data registers - p0, c0-15.  */
23011   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
23012
23013   /* iWMMXt control registers - p1, c0-3.  */
23014   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
23015   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
23016   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
23017   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
23018
23019   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
23020   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
23021   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
23022   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
23023   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
23024
23025   /* XScale accumulator registers.  */
23026   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
23027 };
23028 #undef REGDEF
23029 #undef REGNUM
23030 #undef REGSET
23031
23032 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
23033    within psr_required_here.  */
23034 static const struct asm_psr psrs[] =
23035 {
23036   /* Backward compatibility notation.  Note that "all" is no longer
23037      truly all possible PSR bits.  */
23038   {"all",  PSR_c | PSR_f},
23039   {"flg",  PSR_f},
23040   {"ctl",  PSR_c},
23041
23042   /* Individual flags.  */
23043   {"f",    PSR_f},
23044   {"c",    PSR_c},
23045   {"x",    PSR_x},
23046   {"s",    PSR_s},
23047
23048   /* Combinations of flags.  */
23049   {"fs",   PSR_f | PSR_s},
23050   {"fx",   PSR_f | PSR_x},
23051   {"fc",   PSR_f | PSR_c},
23052   {"sf",   PSR_s | PSR_f},
23053   {"sx",   PSR_s | PSR_x},
23054   {"sc",   PSR_s | PSR_c},
23055   {"xf",   PSR_x | PSR_f},
23056   {"xs",   PSR_x | PSR_s},
23057   {"xc",   PSR_x | PSR_c},
23058   {"cf",   PSR_c | PSR_f},
23059   {"cs",   PSR_c | PSR_s},
23060   {"cx",   PSR_c | PSR_x},
23061   {"fsx",  PSR_f | PSR_s | PSR_x},
23062   {"fsc",  PSR_f | PSR_s | PSR_c},
23063   {"fxs",  PSR_f | PSR_x | PSR_s},
23064   {"fxc",  PSR_f | PSR_x | PSR_c},
23065   {"fcs",  PSR_f | PSR_c | PSR_s},
23066   {"fcx",  PSR_f | PSR_c | PSR_x},
23067   {"sfx",  PSR_s | PSR_f | PSR_x},
23068   {"sfc",  PSR_s | PSR_f | PSR_c},
23069   {"sxf",  PSR_s | PSR_x | PSR_f},
23070   {"sxc",  PSR_s | PSR_x | PSR_c},
23071   {"scf",  PSR_s | PSR_c | PSR_f},
23072   {"scx",  PSR_s | PSR_c | PSR_x},
23073   {"xfs",  PSR_x | PSR_f | PSR_s},
23074   {"xfc",  PSR_x | PSR_f | PSR_c},
23075   {"xsf",  PSR_x | PSR_s | PSR_f},
23076   {"xsc",  PSR_x | PSR_s | PSR_c},
23077   {"xcf",  PSR_x | PSR_c | PSR_f},
23078   {"xcs",  PSR_x | PSR_c | PSR_s},
23079   {"cfs",  PSR_c | PSR_f | PSR_s},
23080   {"cfx",  PSR_c | PSR_f | PSR_x},
23081   {"csf",  PSR_c | PSR_s | PSR_f},
23082   {"csx",  PSR_c | PSR_s | PSR_x},
23083   {"cxf",  PSR_c | PSR_x | PSR_f},
23084   {"cxs",  PSR_c | PSR_x | PSR_s},
23085   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
23086   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
23087   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
23088   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
23089   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
23090   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
23091   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
23092   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
23093   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
23094   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
23095   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
23096   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
23097   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
23098   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
23099   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
23100   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
23101   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
23102   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
23103   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
23104   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
23105   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
23106   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
23107   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
23108   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
23109 };
23110
23111 /* Table of V7M psr names.  */
23112 static const struct asm_psr v7m_psrs[] =
23113 {
23114   {"apsr",         0x0 }, {"APSR",         0x0 },
23115   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
23116   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
23117   {"psr",          0x3 }, {"PSR",          0x3 },
23118   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
23119   {"ipsr",         0x5 }, {"IPSR",         0x5 },
23120   {"epsr",         0x6 }, {"EPSR",         0x6 },
23121   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
23122   {"msp",          0x8 }, {"MSP",          0x8 },
23123   {"psp",          0x9 }, {"PSP",          0x9 },
23124   {"msplim",       0xa }, {"MSPLIM",       0xa },
23125   {"psplim",       0xb }, {"PSPLIM",       0xb },
23126   {"primask",      0x10}, {"PRIMASK",      0x10},
23127   {"basepri",      0x11}, {"BASEPRI",      0x11},
23128   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
23129   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
23130   {"control",      0x14}, {"CONTROL",      0x14},
23131   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
23132   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
23133   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
23134   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
23135   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
23136   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
23137   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
23138   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
23139   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
23140 };
23141
23142 /* Table of all shift-in-operand names.  */
23143 static const struct asm_shift_name shift_names [] =
23144 {
23145   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
23146   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
23147   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
23148   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
23149   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
23150   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX },
23151   { "uxtw", SHIFT_UXTW}, { "UXTW", SHIFT_UXTW}
23152 };
23153
23154 /* Table of all explicit relocation names.  */
23155 #ifdef OBJ_ELF
23156 static struct reloc_entry reloc_names[] =
23157 {
23158   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
23159   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
23160   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
23161   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
23162   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
23163   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
23164   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
23165   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
23166   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
23167   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
23168   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
23169   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
23170   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
23171         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
23172   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
23173         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
23174   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
23175         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ},
23176   { "gotfuncdesc", BFD_RELOC_ARM_GOTFUNCDESC },
23177         { "GOTFUNCDESC", BFD_RELOC_ARM_GOTFUNCDESC },
23178   { "gotofffuncdesc", BFD_RELOC_ARM_GOTOFFFUNCDESC },
23179         { "GOTOFFFUNCDESC", BFD_RELOC_ARM_GOTOFFFUNCDESC },
23180   { "funcdesc", BFD_RELOC_ARM_FUNCDESC },
23181         { "FUNCDESC", BFD_RELOC_ARM_FUNCDESC },
23182    { "tlsgd_fdpic", BFD_RELOC_ARM_TLS_GD32_FDPIC },      { "TLSGD_FDPIC", BFD_RELOC_ARM_TLS_GD32_FDPIC },
23183    { "tlsldm_fdpic", BFD_RELOC_ARM_TLS_LDM32_FDPIC },    { "TLSLDM_FDPIC", BFD_RELOC_ARM_TLS_LDM32_FDPIC },
23184    { "gottpoff_fdpic", BFD_RELOC_ARM_TLS_IE32_FDPIC },   { "GOTTPOFF_FDIC", BFD_RELOC_ARM_TLS_IE32_FDPIC },
23185 };
23186 #endif
23187
23188 /* Table of all conditional affixes.  */
23189 static const struct asm_cond conds[] =
23190 {
23191   {"eq", 0x0},
23192   {"ne", 0x1},
23193   {"cs", 0x2}, {"hs", 0x2},
23194   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
23195   {"mi", 0x4},
23196   {"pl", 0x5},
23197   {"vs", 0x6},
23198   {"vc", 0x7},
23199   {"hi", 0x8},
23200   {"ls", 0x9},
23201   {"ge", 0xa},
23202   {"lt", 0xb},
23203   {"gt", 0xc},
23204   {"le", 0xd},
23205   {"al", 0xe}
23206 };
23207 static const struct asm_cond vconds[] =
23208 {
23209     {"t", 0xf},
23210     {"e", 0x10}
23211 };
23212
23213 #define UL_BARRIER(L,U,CODE,FEAT) \
23214   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
23215   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
23216
23217 static struct asm_barrier_opt barrier_opt_names[] =
23218 {
23219   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
23220   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
23221   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
23222   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
23223   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
23224   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
23225   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
23226   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
23227   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
23228   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
23229   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
23230   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
23231   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
23232   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
23233   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
23234   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
23235 };
23236
23237 #undef UL_BARRIER
23238
23239 /* Table of ARM-format instructions.    */
23240
23241 /* Macros for gluing together operand strings.  N.B. In all cases
23242    other than OPS0, the trailing OP_stop comes from default
23243    zero-initialization of the unspecified elements of the array.  */
23244 #define OPS0()            { OP_stop, }
23245 #define OPS1(a)           { OP_##a, }
23246 #define OPS2(a,b)         { OP_##a,OP_##b, }
23247 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
23248 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
23249 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
23250 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
23251
23252 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
23253    This is useful when mixing operands for ARM and THUMB, i.e. using the
23254    MIX_ARM_THUMB_OPERANDS macro.
23255    In order to use these macros, prefix the number of operands with _
23256    e.g. _3.  */
23257 #define OPS_1(a)           { a, }
23258 #define OPS_2(a,b)         { a,b, }
23259 #define OPS_3(a,b,c)       { a,b,c, }
23260 #define OPS_4(a,b,c,d)     { a,b,c,d, }
23261 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
23262 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
23263
23264 /* These macros abstract out the exact format of the mnemonic table and
23265    save some repeated characters.  */
23266
23267 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
23268 #define TxCE(mnem, op, top, nops, ops, ae, te) \
23269   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
23270     THUMB_VARIANT, do_##ae, do_##te, 0 }
23271
23272 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
23273    a T_MNEM_xyz enumerator.  */
23274 #define TCE(mnem, aop, top, nops, ops, ae, te) \
23275       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
23276 #define tCE(mnem, aop, top, nops, ops, ae, te) \
23277       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23278
23279 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
23280    infix after the third character.  */
23281 #define TxC3(mnem, op, top, nops, ops, ae, te) \
23282   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
23283     THUMB_VARIANT, do_##ae, do_##te, 0 }
23284 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
23285   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
23286     THUMB_VARIANT, do_##ae, do_##te, 0 }
23287 #define TC3(mnem, aop, top, nops, ops, ae, te) \
23288       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
23289 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
23290       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
23291 #define tC3(mnem, aop, top, nops, ops, ae, te) \
23292       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23293 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
23294       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
23295
23296 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
23297    field is still 0xE.  Many of the Thumb variants can be executed
23298    conditionally, so this is checked separately.  */
23299 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
23300   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
23301     THUMB_VARIANT, do_##ae, do_##te, 0 }
23302
23303 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
23304    Used by mnemonics that have very minimal differences in the encoding for
23305    ARM and Thumb variants and can be handled in a common function.  */
23306 #define TUEc(mnem, op, top, nops, ops, en) \
23307   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
23308     THUMB_VARIANT, do_##en, do_##en, 0 }
23309
23310 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
23311    condition code field.  */
23312 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
23313   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
23314     THUMB_VARIANT, do_##ae, do_##te, 0 }
23315
23316 /* ARM-only variants of all the above.  */
23317 #define CE(mnem,  op, nops, ops, ae)    \
23318   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23319
23320 #define C3(mnem, op, nops, ops, ae)     \
23321   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23322
23323 /* Thumb-only variants of TCE and TUE.  */
23324 #define ToC(mnem, top, nops, ops, te) \
23325   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
23326     do_##te, 0 }
23327
23328 #define ToU(mnem, top, nops, ops, te) \
23329   { mnem, OPS##nops ops, OT_unconditional, 0x0, 0x##top, 0, THUMB_VARIANT, \
23330     NULL, do_##te, 0 }
23331
23332 /* T_MNEM_xyz enumerator variants of ToC.  */
23333 #define toC(mnem, top, nops, ops, te) \
23334   { mnem, OPS##nops ops, OT_csuffix, 0x0, T_MNEM##top, 0, THUMB_VARIANT, NULL, \
23335     do_##te, 0 }
23336
23337 /* T_MNEM_xyz enumerator variants of ToU.  */
23338 #define toU(mnem, top, nops, ops, te) \
23339   { mnem, OPS##nops ops, OT_unconditional, 0x0, T_MNEM##top, 0, THUMB_VARIANT, \
23340     NULL, do_##te, 0 }
23341
23342 /* Legacy mnemonics that always have conditional infix after the third
23343    character.  */
23344 #define CL(mnem, op, nops, ops, ae)     \
23345   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
23346     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23347
23348 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
23349 #define cCE(mnem,  op, nops, ops, ae)   \
23350   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23351
23352 /* mov instructions that are shared between coprocessor and MVE.  */
23353 #define mcCE(mnem,  op, nops, ops, ae)  \
23354   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##ae, 0 }
23355
23356 /* Legacy coprocessor instructions where conditional infix and conditional
23357    suffix are ambiguous.  For consistency this includes all FPA instructions,
23358    not just the potentially ambiguous ones.  */
23359 #define cCL(mnem, op, nops, ops, ae)    \
23360   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
23361     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23362
23363 /* Coprocessor, takes either a suffix or a position-3 infix
23364    (for an FPA corner case). */
23365 #define C3E(mnem, op, nops, ops, ae) \
23366   { mnem, OPS##nops ops, OT_csuf_or_in3, \
23367     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
23368
23369 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
23370   { m1 #m2 m3, OPS##nops ops, \
23371     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
23372     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23373
23374 #define CM(m1, m2, op, nops, ops, ae)   \
23375   xCM_ (m1,   , m2, op, nops, ops, ae), \
23376   xCM_ (m1, eq, m2, op, nops, ops, ae), \
23377   xCM_ (m1, ne, m2, op, nops, ops, ae), \
23378   xCM_ (m1, cs, m2, op, nops, ops, ae), \
23379   xCM_ (m1, hs, m2, op, nops, ops, ae), \
23380   xCM_ (m1, cc, m2, op, nops, ops, ae), \
23381   xCM_ (m1, ul, m2, op, nops, ops, ae), \
23382   xCM_ (m1, lo, m2, op, nops, ops, ae), \
23383   xCM_ (m1, mi, m2, op, nops, ops, ae), \
23384   xCM_ (m1, pl, m2, op, nops, ops, ae), \
23385   xCM_ (m1, vs, m2, op, nops, ops, ae), \
23386   xCM_ (m1, vc, m2, op, nops, ops, ae), \
23387   xCM_ (m1, hi, m2, op, nops, ops, ae), \
23388   xCM_ (m1, ls, m2, op, nops, ops, ae), \
23389   xCM_ (m1, ge, m2, op, nops, ops, ae), \
23390   xCM_ (m1, lt, m2, op, nops, ops, ae), \
23391   xCM_ (m1, gt, m2, op, nops, ops, ae), \
23392   xCM_ (m1, le, m2, op, nops, ops, ae), \
23393   xCM_ (m1, al, m2, op, nops, ops, ae)
23394
23395 #define UE(mnem, op, nops, ops, ae)     \
23396   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23397
23398 #define UF(mnem, op, nops, ops, ae)     \
23399   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
23400
23401 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
23402    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
23403    use the same encoding function for each.  */
23404 #define NUF(mnem, op, nops, ops, enc)                                   \
23405   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
23406     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
23407
23408 /* Neon data processing, version which indirects through neon_enc_tab for
23409    the various overloaded versions of opcodes.  */
23410 #define nUF(mnem, op, nops, ops, enc)                                   \
23411   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
23412     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
23413
23414 /* Neon insn with conditional suffix for the ARM version, non-overloaded
23415    version.  */
23416 #define NCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
23417   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
23418     THUMB_VARIANT, do_##enc, do_##enc, mve_p }
23419
23420 #define NCE(mnem, op, nops, ops, enc)                                   \
23421    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
23422
23423 #define NCEF(mnem, op, nops, ops, enc)                                  \
23424     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
23425
23426 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
23427 #define nCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
23428   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
23429     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, mve_p }
23430
23431 #define nCE(mnem, op, nops, ops, enc)                                   \
23432    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
23433
23434 #define nCEF(mnem, op, nops, ops, enc)                                  \
23435     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
23436
23437 /*   */
23438 #define mCEF(mnem, op, nops, ops, enc)                          \
23439   { #mnem, OPS##nops ops, OT_csuffixF, M_MNEM##op, M_MNEM##op,  \
23440     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23441
23442
23443 /* nCEF but for MVE predicated instructions.  */
23444 #define mnCEF(mnem, op, nops, ops, enc)                                 \
23445     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
23446
23447 /* nCE but for MVE predicated instructions.  */
23448 #define mnCE(mnem, op, nops, ops, enc)                                  \
23449    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
23450
23451 /* NUF but for potentially MVE predicated instructions.  */
23452 #define MNUF(mnem, op, nops, ops, enc)                                  \
23453   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
23454     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 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, N_MNEM##op, N_MNEM##op,    \
23459     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
23460
23461 /* ToC but for potentially MVE predicated instructions.  */
23462 #define mToC(mnem, top, nops, ops, te) \
23463   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
23464     do_##te, 1 }
23465
23466 /* NCE but for MVE predicated instructions.  */
23467 #define MNCE(mnem, op, nops, ops, enc)                                  \
23468    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
23469
23470 /* NCEF but for MVE predicated instructions.  */
23471 #define MNCEF(mnem, op, nops, ops, enc)                                 \
23472     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
23473 #define do_0 0
23474
23475 static const struct asm_opcode insns[] =
23476 {
23477 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
23478 #define THUMB_VARIANT  & arm_ext_v4t
23479  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
23480  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
23481  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
23482  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
23483  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
23484  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
23485  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
23486  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
23487  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
23488  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
23489  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
23490  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
23491  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
23492  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
23493  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
23494  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
23495
23496  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
23497     for setting PSR flag bits.  They are obsolete in V6 and do not
23498     have Thumb equivalents. */
23499  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
23500  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
23501   CL("tstp",    110f000,           2, (RR, SH),      cmp),
23502  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
23503  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
23504   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
23505  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
23506  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
23507   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
23508
23509  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
23510  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
23511  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
23512  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
23513
23514  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
23515  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
23516  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
23517                                                                 OP_RRnpc),
23518                                         OP_ADDRGLDR),ldst, t_ldst),
23519  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
23520
23521  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23522  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23523  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23524  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23525  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23526  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
23527
23528  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
23529  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
23530
23531   /* Pseudo ops.  */
23532  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
23533   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
23534  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
23535  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
23536
23537   /* Thumb-compatibility pseudo ops.  */
23538  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
23539  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
23540  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
23541  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
23542  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
23543  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
23544  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
23545  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
23546  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
23547  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
23548  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
23549  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
23550
23551  /* These may simplify to neg.  */
23552  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
23553  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
23554
23555 #undef THUMB_VARIANT
23556 #define THUMB_VARIANT  & arm_ext_os
23557
23558  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
23559  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
23560
23561 #undef  THUMB_VARIANT
23562 #define THUMB_VARIANT  & arm_ext_v6
23563
23564  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
23565
23566  /* V1 instructions with no Thumb analogue prior to V6T2.  */
23567 #undef  THUMB_VARIANT
23568 #define THUMB_VARIANT  & arm_ext_v6t2
23569
23570  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
23571  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
23572   CL("teqp",    130f000,           2, (RR, SH),      cmp),
23573
23574  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23575  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23576  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
23577  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
23578
23579  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23580  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23581
23582  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23583  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
23584
23585  /* V1 instructions with no Thumb analogue at all.  */
23586   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
23587   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
23588
23589   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
23590   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
23591   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
23592   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
23593   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
23594   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
23595   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
23596   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
23597
23598 #undef  ARM_VARIANT
23599 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
23600 #undef  THUMB_VARIANT
23601 #define THUMB_VARIANT  & arm_ext_v4t
23602
23603  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
23604  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
23605
23606 #undef  THUMB_VARIANT
23607 #define THUMB_VARIANT  & arm_ext_v6t2
23608
23609  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
23610   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
23611
23612   /* Generic coprocessor instructions.  */
23613  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
23614  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23615  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23616  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23617  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23618  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23619  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
23620
23621 #undef  ARM_VARIANT
23622 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
23623
23624   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
23625   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
23626
23627 #undef  ARM_VARIANT
23628 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
23629 #undef  THUMB_VARIANT
23630 #define THUMB_VARIANT  & arm_ext_msr
23631
23632  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
23633  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
23634
23635 #undef  ARM_VARIANT
23636 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
23637 #undef  THUMB_VARIANT
23638 #define THUMB_VARIANT  & arm_ext_v6t2
23639
23640  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23641   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23642  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23643   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23644  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23645   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23646  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
23647   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
23648
23649 #undef  ARM_VARIANT
23650 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
23651 #undef  THUMB_VARIANT
23652 #define THUMB_VARIANT  & arm_ext_v4t
23653
23654  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23655  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23656  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23657  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23658  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23659  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
23660
23661 #undef  ARM_VARIANT
23662 #define ARM_VARIANT  & arm_ext_v4t_5
23663
23664   /* ARM Architecture 4T.  */
23665   /* Note: bx (and blx) are required on V5, even if the processor does
23666      not support Thumb.  */
23667  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
23668
23669 #undef  ARM_VARIANT
23670 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
23671 #undef  THUMB_VARIANT
23672 #define THUMB_VARIANT  & arm_ext_v5t
23673
23674   /* Note: blx has 2 variants; the .value coded here is for
23675      BLX(2).  Only this variant has conditional execution.  */
23676  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
23677  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
23678
23679 #undef  THUMB_VARIANT
23680 #define THUMB_VARIANT  & arm_ext_v6t2
23681
23682  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
23683  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23684  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
23685  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
23686  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
23687  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
23688  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23689  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
23690
23691 #undef  ARM_VARIANT
23692 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
23693 #undef  THUMB_VARIANT
23694 #define THUMB_VARIANT  & arm_ext_v5exp
23695
23696  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23697  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23698  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23699  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23700
23701  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23702  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
23703
23704  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23705  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23706  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23707  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
23708
23709  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23710  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23711  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23712  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23713
23714  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23715  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
23716
23717  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23718  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23719  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23720  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
23721
23722 #undef  ARM_VARIANT
23723 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
23724 #undef  THUMB_VARIANT
23725 #define THUMB_VARIANT  & arm_ext_v6t2
23726
23727  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
23728  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
23729      ldrd, t_ldstd),
23730  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
23731                                        ADDRGLDRS), ldrd, t_ldstd),
23732
23733  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23734  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23735
23736 #undef  ARM_VARIANT
23737 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
23738
23739  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
23740
23741 #undef  ARM_VARIANT
23742 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
23743 #undef  THUMB_VARIANT
23744 #define THUMB_VARIANT  & arm_ext_v6
23745
23746  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
23747  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
23748  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23749  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23750  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
23751  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23752  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23753  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23754  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
23755  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
23756
23757 #undef  THUMB_VARIANT
23758 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23759
23760  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
23761  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23762                                       strex,  t_strex),
23763 #undef  THUMB_VARIANT
23764 #define THUMB_VARIANT  & arm_ext_v6t2
23765
23766  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23767  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
23768
23769  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
23770  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
23771
23772 /*  ARM V6 not included in V7M.  */
23773 #undef  THUMB_VARIANT
23774 #define THUMB_VARIANT  & arm_ext_v6_notm
23775  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23776  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23777   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
23778   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
23779  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
23780  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
23781   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
23782  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
23783   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
23784  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23785  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23786  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
23787   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
23788   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
23789   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
23790   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
23791  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
23792  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
23793  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
23794
23795 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
23796 #undef  THUMB_VARIANT
23797 #define THUMB_VARIANT  & arm_ext_v6_dsp
23798  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
23799  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
23800  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23801  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23802  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23803  /* Old name for QASX.  */
23804  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23805  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23806  /* Old name for QSAX.  */
23807  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23808  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23809  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23810  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23811  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23812  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23813  /* Old name for SASX.  */
23814  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23815  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23816  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23817  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23818  /* Old name for SHASX.  */
23819  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23820  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23821  /* Old name for SHSAX.  */
23822  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23823  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23824  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23825  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23826  /* Old name for SSAX.  */
23827  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23828  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23829  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23830  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23831  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23832  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23833  /* Old name for UASX.  */
23834  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23835  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23836  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23837  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23838  /* Old name for UHASX.  */
23839  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23840  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23841  /* Old name for UHSAX.  */
23842  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23843  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23844  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23845  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23846  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23847  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23848  /* Old name for UQASX.  */
23849  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23850  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23851  /* Old name for UQSAX.  */
23852  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
23853  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23854  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23855  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23856  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23857  /* Old name for USAX.  */
23858  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23859  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23860  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23861  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23862  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23863  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
23864  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23865  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23866  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
23867  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
23868  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
23869  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23870  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23871  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23872  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23873  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23874  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23875  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23876  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
23877  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23878  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23879  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23880  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
23881  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23882  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23883  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23884  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23885  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23886  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
23887  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
23888  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
23889  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
23890  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
23891  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
23892
23893 #undef  ARM_VARIANT
23894 #define ARM_VARIANT   & arm_ext_v6k_v6t2
23895 #undef  THUMB_VARIANT
23896 #define THUMB_VARIANT & arm_ext_v6k_v6t2
23897
23898  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
23899  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
23900  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
23901  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
23902
23903 #undef  THUMB_VARIANT
23904 #define THUMB_VARIANT  & arm_ext_v6_notm
23905  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
23906                                       ldrexd, t_ldrexd),
23907  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
23908                                        RRnpcb), strexd, t_strexd),
23909
23910 #undef  THUMB_VARIANT
23911 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23912  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
23913      rd_rn,  rd_rn),
23914  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
23915      rd_rn,  rd_rn),
23916  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23917      strex, t_strexbh),
23918  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
23919      strex, t_strexbh),
23920  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
23921
23922 #undef  ARM_VARIANT
23923 #define ARM_VARIANT    & arm_ext_sec
23924 #undef  THUMB_VARIANT
23925 #define THUMB_VARIANT  & arm_ext_sec
23926
23927  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
23928
23929 #undef  ARM_VARIANT
23930 #define ARM_VARIANT    & arm_ext_virt
23931 #undef  THUMB_VARIANT
23932 #define THUMB_VARIANT    & arm_ext_virt
23933
23934  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
23935  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
23936
23937 #undef  ARM_VARIANT
23938 #define ARM_VARIANT    & arm_ext_pan
23939 #undef  THUMB_VARIANT
23940 #define THUMB_VARIANT  & arm_ext_pan
23941
23942  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
23943
23944 #undef  ARM_VARIANT
23945 #define ARM_VARIANT    & arm_ext_v6t2
23946 #undef  THUMB_VARIANT
23947 #define THUMB_VARIANT  & arm_ext_v6t2
23948
23949  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
23950  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
23951  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
23952  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
23953
23954  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
23955  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
23956
23957  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23958  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23959  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23960  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
23961
23962 #undef  ARM_VARIANT
23963 #define ARM_VARIANT    & arm_ext_v3
23964 #undef  THUMB_VARIANT
23965 #define THUMB_VARIANT  & arm_ext_v6t2
23966
23967  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
23968  TUF("ssbb",    57ff040, f3bf8f40, 0, (), noargs, t_csdb),
23969  TUF("pssbb",   57ff044, f3bf8f44, 0, (), noargs, t_csdb),
23970
23971 #undef  ARM_VARIANT
23972 #define ARM_VARIANT    & arm_ext_v6t2
23973 #undef  THUMB_VARIANT
23974 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
23975  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
23976  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
23977
23978  /* Thumb-only instructions.  */
23979 #undef  ARM_VARIANT
23980 #define ARM_VARIANT NULL
23981   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
23982   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
23983
23984  /* ARM does not really have an IT instruction, so always allow it.
23985     The opcode is copied from Thumb in order to allow warnings in
23986     -mimplicit-it=[never | arm] modes.  */
23987 #undef  ARM_VARIANT
23988 #define ARM_VARIANT  & arm_ext_v1
23989 #undef  THUMB_VARIANT
23990 #define THUMB_VARIANT  & arm_ext_v6t2
23991
23992  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
23993  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
23994  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
23995  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
23996  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
23997  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
23998  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
23999  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
24000  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
24001  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
24002  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
24003  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
24004  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
24005  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
24006  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
24007  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
24008  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
24009  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
24010
24011  /* Thumb2 only instructions.  */
24012 #undef  ARM_VARIANT
24013 #define ARM_VARIANT  NULL
24014
24015  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
24016  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
24017  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
24018  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
24019  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
24020  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
24021
24022  /* Hardware division instructions.  */
24023 #undef  ARM_VARIANT
24024 #define ARM_VARIANT    & arm_ext_adiv
24025 #undef  THUMB_VARIANT
24026 #define THUMB_VARIANT  & arm_ext_div
24027
24028  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
24029  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
24030
24031  /* ARM V6M/V7 instructions.  */
24032 #undef  ARM_VARIANT
24033 #define ARM_VARIANT    & arm_ext_barrier
24034 #undef  THUMB_VARIANT
24035 #define THUMB_VARIANT  & arm_ext_barrier
24036
24037  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
24038  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
24039  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
24040
24041  /* ARM V7 instructions.  */
24042 #undef  ARM_VARIANT
24043 #define ARM_VARIANT    & arm_ext_v7
24044 #undef  THUMB_VARIANT
24045 #define THUMB_VARIANT  & arm_ext_v7
24046
24047  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
24048  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
24049
24050 #undef  ARM_VARIANT
24051 #define ARM_VARIANT    & arm_ext_mp
24052 #undef  THUMB_VARIANT
24053 #define THUMB_VARIANT  & arm_ext_mp
24054
24055  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
24056
24057  /* AArchv8 instructions.  */
24058 #undef  ARM_VARIANT
24059 #define ARM_VARIANT   & arm_ext_v8
24060
24061 /* Instructions shared between armv8-a and armv8-m.  */
24062 #undef  THUMB_VARIANT
24063 #define THUMB_VARIANT & arm_ext_atomics
24064
24065  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24066  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24067  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24068  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24069  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24070  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
24071  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24072  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
24073  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
24074  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
24075                                                         stlex,  t_stlex),
24076  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
24077                                                         stlex, t_stlex),
24078  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
24079                                                         stlex, t_stlex),
24080 #undef  THUMB_VARIANT
24081 #define THUMB_VARIANT & arm_ext_v8
24082
24083  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
24084  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
24085                                                         ldrexd, t_ldrexd),
24086  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
24087                                                         strexd, t_strexd),
24088
24089 /* Defined in V8 but is in undefined encoding space for earlier
24090    architectures.  However earlier architectures are required to treat
24091    this instuction as a semihosting trap as well.  Hence while not explicitly
24092    defined as such, it is in fact correct to define the instruction for all
24093    architectures.  */
24094 #undef  THUMB_VARIANT
24095 #define THUMB_VARIANT  & arm_ext_v1
24096 #undef  ARM_VARIANT
24097 #define ARM_VARIANT  & arm_ext_v1
24098  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
24099
24100  /* ARMv8 T32 only.  */
24101 #undef  ARM_VARIANT
24102 #define ARM_VARIANT  NULL
24103  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
24104  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
24105  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
24106
24107   /* FP for ARMv8.  */
24108 #undef  ARM_VARIANT
24109 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
24110 #undef  THUMB_VARIANT
24111 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
24112
24113   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
24114   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
24115   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
24116   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
24117   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
24118   mnCE(vrintz, _vrintr, 2, (RNSDQMQ, oRNSDQMQ),         vrintz),
24119   mnCE(vrintx, _vrintr, 2, (RNSDQMQ, oRNSDQMQ),         vrintx),
24120   mnUF(vrinta, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrinta),
24121   mnUF(vrintn, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintn),
24122   mnUF(vrintp, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintp),
24123   mnUF(vrintm, _vrinta, 2, (RNSDQMQ, oRNSDQMQ),         vrintm),
24124
24125   /* Crypto v1 extensions.  */
24126 #undef  ARM_VARIANT
24127 #define ARM_VARIANT & fpu_crypto_ext_armv8
24128 #undef  THUMB_VARIANT
24129 #define THUMB_VARIANT & fpu_crypto_ext_armv8
24130
24131   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
24132   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
24133   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
24134   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
24135   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
24136   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
24137   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
24138   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
24139   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
24140   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
24141   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
24142   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
24143   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
24144   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
24145
24146 #undef  ARM_VARIANT
24147 #define ARM_VARIANT   & crc_ext_armv8
24148 #undef  THUMB_VARIANT
24149 #define THUMB_VARIANT & crc_ext_armv8
24150   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
24151   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
24152   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
24153   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
24154   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
24155   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
24156
24157  /* ARMv8.2 RAS extension.  */
24158 #undef  ARM_VARIANT
24159 #define ARM_VARIANT   & arm_ext_ras
24160 #undef  THUMB_VARIANT
24161 #define THUMB_VARIANT & arm_ext_ras
24162  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
24163
24164 #undef  ARM_VARIANT
24165 #define ARM_VARIANT   & arm_ext_v8_3
24166 #undef  THUMB_VARIANT
24167 #define THUMB_VARIANT & arm_ext_v8_3
24168  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
24169
24170 #undef  ARM_VARIANT
24171 #define ARM_VARIANT   & fpu_neon_ext_dotprod
24172 #undef  THUMB_VARIANT
24173 #define THUMB_VARIANT & fpu_neon_ext_dotprod
24174  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
24175  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
24176
24177 #undef  ARM_VARIANT
24178 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
24179 #undef  THUMB_VARIANT
24180 #define THUMB_VARIANT NULL
24181
24182  cCE("wfs",     e200110, 1, (RR),            rd),
24183  cCE("rfs",     e300110, 1, (RR),            rd),
24184  cCE("wfc",     e400110, 1, (RR),            rd),
24185  cCE("rfc",     e500110, 1, (RR),            rd),
24186
24187  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24188  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24189  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24190  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24191
24192  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24193  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24194  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24195  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
24196
24197  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
24198  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
24199  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
24200  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
24201  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
24202  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
24203  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
24204  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
24205  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
24206  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
24207  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
24208  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
24209
24210  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
24211  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
24212  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
24213  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
24214  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
24215  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
24216  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
24217  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
24218  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
24219  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
24220  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
24221  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
24222
24223  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
24224  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
24225  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
24226  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
24227  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
24228  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
24229  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
24230  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
24231  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
24232  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
24233  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
24234  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
24235
24236  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
24237  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
24238  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
24239  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
24240  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
24241  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
24242  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
24243  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
24244  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
24245  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
24246  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
24247  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
24248
24249  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
24250  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
24251  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
24252  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
24253  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
24254  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
24255  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
24256  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
24257  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
24258  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
24259  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
24260  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
24261
24262  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
24263  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
24264  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
24265  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
24266  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
24267  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
24268  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
24269  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
24270  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
24271  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
24272  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
24273  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
24274
24275  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
24276  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
24277  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
24278  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
24279  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
24280  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
24281  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
24282  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
24283  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
24284  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
24285  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
24286  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
24287
24288  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
24289  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
24290  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
24291  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
24292  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
24293  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
24294  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
24295  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
24296  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
24297  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
24298  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
24299  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
24300
24301  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
24302  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
24303  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
24304  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
24305  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
24306  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
24307  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
24308  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
24309  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
24310  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
24311  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
24312  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
24313
24314  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
24315  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
24316  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
24317  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
24318  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
24319  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
24320  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
24321  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
24322  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
24323  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
24324  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
24325  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
24326
24327  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
24328  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
24329  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
24330  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
24331  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
24332  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
24333  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
24334  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
24335  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
24336  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
24337  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
24338  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
24339
24340  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
24341  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
24342  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
24343  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
24344  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
24345  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
24346  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
24347  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
24348  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
24349  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
24350  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
24351  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
24352
24353  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
24354  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
24355  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
24356  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
24357  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
24358  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
24359  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
24360  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
24361  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
24362  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
24363  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
24364  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
24365
24366  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
24367  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
24368  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
24369  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
24370  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
24371  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
24372  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
24373  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
24374  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
24375  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
24376  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
24377  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
24378
24379  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
24380  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
24381  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
24382  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
24383  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
24384  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
24385  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
24386  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
24387  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
24388  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
24389  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
24390  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
24391
24392  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
24393  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
24394  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
24395  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
24396  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
24397  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
24398  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
24399  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
24400  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
24401  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
24402  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
24403  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
24404
24405  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
24406  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
24407  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
24408  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
24409  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
24410  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24411  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24412  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24413  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
24414  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
24415  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
24416  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
24417
24418  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
24419  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
24420  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
24421  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
24422  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
24423  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24424  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24425  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24426  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
24427  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
24428  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
24429  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
24430
24431  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
24432  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
24433  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
24434  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
24435  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
24436  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24437  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24438  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24439  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
24440  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
24441  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
24442  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
24443
24444  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
24445  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
24446  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
24447  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
24448  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
24449  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24450  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24451  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24452  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
24453  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
24454  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
24455  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
24456
24457  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
24458  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
24459  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
24460  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
24461  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
24462  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24463  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24464  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24465  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
24466  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
24467  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
24468  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
24469
24470  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
24471  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
24472  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
24473  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
24474  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
24475  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24476  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24477  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24478  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
24479  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
24480  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
24481  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
24482
24483  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
24484  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
24485  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
24486  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
24487  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
24488  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24489  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24490  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24491  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
24492  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
24493  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
24494  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
24495
24496  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
24497  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
24498  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
24499  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
24500  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
24501  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24502  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24503  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24504  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
24505  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
24506  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
24507  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
24508
24509  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
24510  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
24511  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
24512  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
24513  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
24514  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24515  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24516  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24517  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
24518  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
24519  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
24520  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
24521
24522  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
24523  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
24524  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
24525  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
24526  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
24527  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24528  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24529  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24530  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
24531  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
24532  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
24533  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
24534
24535  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24536  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24537  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24538  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24539  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24540  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24541  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24542  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24543  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24544  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24545  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24546  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24547
24548  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24549  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24550  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24551  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24552  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24553  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24554  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24555  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24556  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24557  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24558  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24559  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24560
24561  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
24562  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
24563  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
24564  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
24565  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
24566  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
24567  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
24568  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
24569  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
24570  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
24571  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
24572  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
24573
24574  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
24575  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
24576  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
24577  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
24578
24579  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
24580  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
24581  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
24582  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
24583  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
24584  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
24585  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
24586  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
24587  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
24588  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
24589  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
24590  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
24591
24592   /* The implementation of the FIX instruction is broken on some
24593      assemblers, in that it accepts a precision specifier as well as a
24594      rounding specifier, despite the fact that this is meaningless.
24595      To be more compatible, we accept it as well, though of course it
24596      does not set any bits.  */
24597  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
24598  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
24599  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
24600  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
24601  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
24602  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
24603  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
24604  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
24605  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
24606  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
24607  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
24608  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
24609  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
24610
24611   /* Instructions that were new with the real FPA, call them V2.  */
24612 #undef  ARM_VARIANT
24613 #define ARM_VARIANT  & fpu_fpa_ext_v2
24614
24615  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24616  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24617  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24618  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24619  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24620  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
24621
24622 #undef  ARM_VARIANT
24623 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
24624 #undef THUMB_VARIANT
24625 #define THUMB_VARIANT  & arm_ext_v6t2
24626  mcCE(vmrs,     ef00a10, 2, (APSR_RR, RVC),   vmrs),
24627  mcCE(vmsr,     ee00a10, 2, (RVC, RR),        vmsr),
24628 #undef THUMB_VARIANT
24629
24630   /* Moves and type conversions.  */
24631  cCE("fmstat",  ef1fa10, 0, (),               noargs),
24632  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24633  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
24634  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
24635  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24636  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
24637  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24638  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
24639  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
24640
24641   /* Memory operations.  */
24642  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
24643  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
24644  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24645  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24646  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24647  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24648  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24649  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24650  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24651  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24652  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24653  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
24654  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24655  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
24656  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24657  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
24658  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24659  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
24660
24661   /* Monadic operations.  */
24662  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24663  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
24664  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24665
24666   /* Dyadic operations.  */
24667  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24668  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24669  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24670  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24671  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24672  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24673  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24674  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24675  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
24676
24677   /* Comparisons.  */
24678  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
24679  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
24680  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
24681  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
24682
24683  /* Double precision load/store are still present on single precision
24684     implementations.  */
24685  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
24686  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
24687  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24688  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24689  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24690  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24691  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24692  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
24693  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24694  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
24695
24696 #undef  ARM_VARIANT
24697 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
24698
24699   /* Moves and type conversions.  */
24700  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24701  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24702  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
24703  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
24704  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
24705  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
24706  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24707  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
24708  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24709  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24710  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24711  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
24712
24713   /* Monadic operations.  */
24714  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24715  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24716  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24717
24718   /* Dyadic operations.  */
24719  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24720  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24721  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24722  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24723  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24724  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24725  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24726  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24727  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
24728
24729   /* Comparisons.  */
24730  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24731  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
24732  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
24733  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
24734
24735 /* Instructions which may belong to either the Neon or VFP instruction sets.
24736    Individual encoder functions perform additional architecture checks.  */
24737 #undef  ARM_VARIANT
24738 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
24739 #undef  THUMB_VARIANT
24740 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
24741
24742   /* These mnemonics are unique to VFP.  */
24743  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
24744  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
24745  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24746  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24747  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
24748  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
24749  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
24750  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
24751
24752   /* Mnemonics shared by Neon and VFP.  */
24753  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
24754
24755  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24756  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24757  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24758  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24759  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24760  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
24761
24762  mnCEF(vcvt,     _vcvt,   3, (RNSDQMQ, RNSDQMQ, oI32z), neon_cvt),
24763  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
24764  MNCEF(vcvtb,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtb),
24765  MNCEF(vcvtt,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtt),
24766
24767
24768   /* NOTE: All VMOV encoding is special-cased!  */
24769  NCE(vmovq,     0,       1, (VMOV), neon_mov),
24770
24771 #undef  THUMB_VARIANT
24772 /* Could be either VLDR/VSTR or VLDR/VSTR (system register) which are guarded
24773    by different feature bits.  Since we are setting the Thumb guard, we can
24774    require Thumb-1 which makes it a nop guard and set the right feature bit in
24775    do_vldr_vstr ().  */
24776 #define THUMB_VARIANT  & arm_ext_v4t
24777  NCE(vldr,      d100b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
24778  NCE(vstr,      d000b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
24779
24780 #undef  ARM_VARIANT
24781 #define ARM_VARIANT    & arm_ext_fp16
24782 #undef  THUMB_VARIANT
24783 #define THUMB_VARIANT  & arm_ext_fp16
24784  /* New instructions added from v8.2, allowing the extraction and insertion of
24785     the upper 16 bits of a 32-bit vector register.  */
24786  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
24787  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
24788
24789  /* New backported fma/fms instructions optional in v8.2.  */
24790  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
24791  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
24792
24793 #undef  THUMB_VARIANT
24794 #define THUMB_VARIANT  & fpu_neon_ext_v1
24795 #undef  ARM_VARIANT
24796 #define ARM_VARIANT    & fpu_neon_ext_v1
24797
24798   /* Data processing with three registers of the same length.  */
24799   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
24800  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
24801  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
24802  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24803  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24804  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
24805   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
24806  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
24807  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
24808  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
24809  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
24810   /* If not immediate, fall back to neon_dyadic_i64_su.
24811      shl should accept I8 I16 I32 I64,
24812      qshl should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
24813  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl),
24814  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl),
24815   /* Logic ops, types optional & ignored.  */
24816  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24817  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24818  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24819  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
24820  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
24821   /* Bitfield ops, untyped.  */
24822  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24823  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24824  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24825  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24826  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
24827  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
24828   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
24829  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24830  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24831  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
24832   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
24833      back to neon_dyadic_if_su.  */
24834  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
24835  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
24836  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
24837  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
24838  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
24839  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
24840  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
24841  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
24842   /* Comparison. Type I8 I16 I32 F32.  */
24843  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
24844  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
24845   /* As above, D registers only.  */
24846  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
24847  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
24848   /* Int and float variants, signedness unimportant.  */
24849  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
24850  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
24851  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
24852   /* Add/sub take types I8 I16 I32 I64 F32.  */
24853  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
24854  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
24855   /* vtst takes sizes 8, 16, 32.  */
24856  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
24857  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
24858   /* VMUL takes I8 I16 I32 F32 P8.  */
24859  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
24860   /* VQD{R}MULH takes S16 S32.  */
24861  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
24862  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
24863  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
24864  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
24865  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
24866  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
24867  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
24868  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
24869  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
24870  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
24871  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
24872  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
24873  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
24874  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
24875  /* ARM v8.1 extension.  */
24876  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
24877  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
24878  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
24879
24880   /* Two address, int/float. Types S8 S16 S32 F32.  */
24881  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
24882  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
24883
24884   /* Data processing with two registers and a shift amount.  */
24885   /* Right shifts, and variants with rounding.
24886      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
24887  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
24888  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
24889  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
24890  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
24891  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
24892  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
24893   /* Shift and insert. Sizes accepted 8 16 32 64.  */
24894  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
24895  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
24896   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
24897  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
24898   /* Right shift immediate, saturating & narrowing, with rounding variants.
24899      Types accepted S16 S32 S64 U16 U32 U64.  */
24900  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
24901  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
24902   /* As above, unsigned. Types accepted S16 S32 S64.  */
24903  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
24904  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
24905   /* Right shift narrowing. Types accepted I16 I32 I64.  */
24906  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
24907  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
24908   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
24909  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
24910   /* CVT with optional immediate for fixed-point variant.  */
24911  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
24912
24913  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
24914
24915   /* Data processing, three registers of different lengths.  */
24916   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
24917  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
24918   /* If not scalar, fall back to neon_dyadic_long.
24919      Vector types as above, scalar types S16 S32 U16 U32.  */
24920  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
24921  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
24922   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
24923  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
24924  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
24925   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
24926  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24927  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24928  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24929  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
24930   /* Saturating doubling multiplies. Types S16 S32.  */
24931  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24932  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24933  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
24934   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
24935      S16 S32 U16 U32.  */
24936  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
24937
24938   /* Extract. Size 8.  */
24939  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
24940  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
24941
24942   /* Two registers, miscellaneous.  */
24943   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
24944  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
24945  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
24946  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
24947   /* Vector replicate. Sizes 8 16 32.  */
24948  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
24949   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
24950  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
24951   /* VMOVN. Types I16 I32 I64.  */
24952  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
24953   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
24954  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
24955   /* VQMOVUN. Types S16 S32 S64.  */
24956  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
24957   /* VZIP / VUZP. Sizes 8 16 32.  */
24958  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
24959  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
24960  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
24961  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
24962   /* VQABS / VQNEG. Types S8 S16 S32.  */
24963  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
24964  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
24965   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
24966  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
24967  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
24968  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
24969  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
24970   /* Reciprocal estimates.  Types U32 F16 F32.  */
24971  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
24972  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
24973  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
24974  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
24975   /* VCLS. Types S8 S16 S32.  */
24976  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
24977   /* VCLZ. Types I8 I16 I32.  */
24978  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
24979   /* VCNT. Size 8.  */
24980  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
24981  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
24982   /* Two address, untyped.  */
24983  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
24984  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
24985   /* VTRN. Sizes 8 16 32.  */
24986  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
24987  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
24988
24989   /* Table lookup. Size 8.  */
24990  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
24991  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
24992
24993 #undef  THUMB_VARIANT
24994 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
24995 #undef  ARM_VARIANT
24996 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
24997
24998   /* Neon element/structure load/store.  */
24999  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25000  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25001  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25002  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25003  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25004  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25005  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25006  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
25007
25008 #undef  THUMB_VARIANT
25009 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
25010 #undef  ARM_VARIANT
25011 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
25012  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
25013  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25014  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25015  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25016  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25017  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25018  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25019  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
25020  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
25021
25022 #undef  THUMB_VARIANT
25023 #define THUMB_VARIANT  & fpu_vfp_ext_v3
25024 #undef  ARM_VARIANT
25025 #define ARM_VARIANT    & fpu_vfp_ext_v3
25026
25027  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
25028  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25029  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25030  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25031  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25032  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25033  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25034  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
25035  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
25036
25037 #undef  ARM_VARIANT
25038 #define ARM_VARIANT    & fpu_vfp_ext_fma
25039 #undef  THUMB_VARIANT
25040 #define THUMB_VARIANT  & fpu_vfp_ext_fma
25041  /* Mnemonics shared by Neon, VFP and MVE.  These are included in the
25042     VFP FMA variant; NEON and VFP FMA always includes the NEON
25043     FMA instructions.  */
25044  mnCEF(vfma,     _vfma,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR), neon_fmac),
25045  mnCEF(vfms,     _vfms,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ),  neon_fmac),
25046
25047  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
25048     the v form should always be used.  */
25049  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
25050  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
25051  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
25052  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
25053  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
25054  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
25055
25056 #undef THUMB_VARIANT
25057 #undef  ARM_VARIANT
25058 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
25059
25060  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25061  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25062  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25063  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25064  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25065  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
25066  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
25067  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
25068
25069 #undef  ARM_VARIANT
25070 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
25071
25072  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
25073  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
25074  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
25075  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
25076  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
25077  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
25078  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
25079  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
25080  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
25081  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25082  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25083  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25084  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25085  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25086  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
25087  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25088  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25089  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
25090  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
25091  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
25092  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25093  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25094  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25095  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25096  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25097  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
25098  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
25099  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
25100  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
25101  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
25102  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
25103  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
25104  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
25105  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
25106  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
25107  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
25108  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
25109  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25110  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25111  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25112  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25113  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25114  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25115  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25116  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25117  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25118  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
25119  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25120  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25121  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25122  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25123  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25124  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25125  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25126  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25127  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25128  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25129  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25130  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25131  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25132  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25133  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25134  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25135  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25136  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25137  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25138  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25139  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25140  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
25141  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
25142  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25143  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25144  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25145  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25146  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25147  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25148  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25149  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25150  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25151  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25152  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25153  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25154  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25155  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25156  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25157  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25158  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25159  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25160  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
25161  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25162  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25163  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25164  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25165  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25166  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25167  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25168  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25169  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25170  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25171  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25172  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25173  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25174  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25175  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25176  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25177  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25178  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25179  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25180  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25181  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25182  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
25183  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25184  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25185  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25186  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25187  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25188  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25189  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25190  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25191  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25192  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25193  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25194  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25195  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25196  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25197  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25198  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25199  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
25200  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
25201  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25202  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
25203  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
25204  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
25205  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25206  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25207  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25208  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25209  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25210  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25211  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25212  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25213  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25214  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
25215  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
25216  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
25217  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
25218  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
25219  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
25220  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25221  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25222  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25223  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
25224  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
25225  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
25226  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
25227  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
25228  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
25229  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25230  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25231  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
25232  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25233  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
25234
25235 #undef  ARM_VARIANT
25236 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
25237
25238  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
25239  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
25240  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
25241  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
25242  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
25243  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
25244  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25245  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25246  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25247  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25248  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25249  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25250  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25251  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25252  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25253  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25254  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25255  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25256  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25257  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25258  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
25259  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25260  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25261  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25262  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25263  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25264  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25265  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25266  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25267  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25268  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25269  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25270  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25271  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25272  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25273  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25274  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25275  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25276  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25277  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25278  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25279  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25280  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25281  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25282  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25283  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25284  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25285  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25286  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25287  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25288  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25289  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25290  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25291  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25292  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25293  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25294  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
25295
25296 #undef  ARM_VARIANT
25297 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
25298
25299  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
25300  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
25301  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
25302  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
25303  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
25304  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
25305  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
25306  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
25307  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
25308  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
25309  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
25310  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
25311  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
25312  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
25313  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
25314  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
25315  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
25316  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
25317  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
25318  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
25319  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
25320  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
25321  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
25322  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
25323  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
25324  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
25325  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
25326  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
25327  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
25328  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
25329  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
25330  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
25331  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
25332  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
25333  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
25334  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
25335  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
25336  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
25337  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
25338  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
25339  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
25340  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
25341  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
25342  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
25343  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
25344  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
25345  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
25346  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
25347  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
25348  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
25349  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
25350  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
25351  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
25352  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
25353  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
25354  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
25355  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
25356  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
25357  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
25358  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
25359  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
25360  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
25361  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
25362  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
25363  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25364  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25365  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25366  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25367  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25368  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
25369  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25370  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
25371  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
25372  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
25373  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
25374  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
25375
25376  /* ARMv8.5-A instructions.  */
25377 #undef  ARM_VARIANT
25378 #define ARM_VARIANT   & arm_ext_sb
25379 #undef  THUMB_VARIANT
25380 #define THUMB_VARIANT & arm_ext_sb
25381  TUF("sb", 57ff070, f3bf8f70, 0, (), noargs, noargs),
25382
25383 #undef  ARM_VARIANT
25384 #define ARM_VARIANT   & arm_ext_predres
25385 #undef  THUMB_VARIANT
25386 #define THUMB_VARIANT & arm_ext_predres
25387  CE("cfprctx", e070f93, 1, (RRnpc), rd),
25388  CE("dvprctx", e070fb3, 1, (RRnpc), rd),
25389  CE("cpprctx", e070ff3, 1, (RRnpc), rd),
25390
25391  /* ARMv8-M instructions.  */
25392 #undef  ARM_VARIANT
25393 #define ARM_VARIANT NULL
25394 #undef  THUMB_VARIANT
25395 #define THUMB_VARIANT & arm_ext_v8m
25396  ToU("sg",    e97fe97f, 0, (),             noargs),
25397  ToC("blxns", 4784,     1, (RRnpc),        t_blx),
25398  ToC("bxns",  4704,     1, (RRnpc),        t_bx),
25399  ToC("tt",    e840f000, 2, (RRnpc, RRnpc), tt),
25400  ToC("ttt",   e840f040, 2, (RRnpc, RRnpc), tt),
25401  ToC("tta",   e840f080, 2, (RRnpc, RRnpc), tt),
25402  ToC("ttat",  e840f0c0, 2, (RRnpc, RRnpc), tt),
25403
25404  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
25405     instructions behave as nop if no VFP is present.  */
25406 #undef  THUMB_VARIANT
25407 #define THUMB_VARIANT & arm_ext_v8m_main
25408  ToC("vlldm", ec300a00, 1, (RRnpc), rn),
25409  ToC("vlstm", ec200a00, 1, (RRnpc), rn),
25410
25411  /* Armv8.1-M Mainline instructions.  */
25412 #undef  THUMB_VARIANT
25413 #define THUMB_VARIANT & arm_ext_v8_1m_main
25414  toU("cinc",  _cinc,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25415  toU("cinv",  _cinv,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25416  toU("cneg",  _cneg,  3, (RRnpcsp, RR_ZR, COND),        t_cond),
25417  toU("csel",  _csel,  4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25418  toU("csetm", _csetm, 2, (RRnpcsp, COND),               t_cond),
25419  toU("cset",  _cset,  2, (RRnpcsp, COND),               t_cond),
25420  toU("csinc", _csinc, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25421  toU("csinv", _csinv, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25422  toU("csneg", _csneg, 4, (RRnpcsp, RR_ZR, RR_ZR, COND), t_cond),
25423
25424  toC("bf",     _bf,     2, (EXPs, EXPs),             t_branch_future),
25425  toU("bfcsel", _bfcsel, 4, (EXPs, EXPs, EXPs, COND), t_branch_future),
25426  toC("bfx",    _bfx,    2, (EXPs, RRnpcsp),          t_branch_future),
25427  toC("bfl",    _bfl,    2, (EXPs, EXPs),             t_branch_future),
25428  toC("bflx",   _bflx,   2, (EXPs, RRnpcsp),          t_branch_future),
25429
25430  toU("dls", _dls, 2, (LR, RRnpcsp),      t_loloop),
25431  toU("wls", _wls, 3, (LR, RRnpcsp, EXP), t_loloop),
25432  toU("le",  _le,  2, (oLR, EXP),         t_loloop),
25433
25434  ToC("clrm",    e89f0000, 1, (CLRMLST),  t_clrm),
25435  ToC("vscclrm", ec9f0a00, 1, (VRSDVLST), t_vscclrm),
25436
25437 #undef  THUMB_VARIANT
25438 #define THUMB_VARIANT & mve_ext
25439  ToC("lsll",    ea50010d, 3, (RRe, RRo, RRnpcsp_I32), mve_scalar_shift),
25440  ToC("lsrl",    ea50011f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25441  ToC("asrl",    ea50012d, 3, (RRe, RRo, RRnpcsp_I32), mve_scalar_shift),
25442  ToC("uqrshll", ea51010d, 4, (RRe, RRo, I48_I64, RRnpcsp), mve_scalar_shift1),
25443  ToC("sqrshrl", ea51012d, 4, (RRe, RRo, I48_I64, RRnpcsp), mve_scalar_shift1),
25444  ToC("uqshll",  ea51010f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25445  ToC("urshrl",  ea51011f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25446  ToC("srshrl",  ea51012f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25447  ToC("sqshll",  ea51013f, 3, (RRe, RRo, I32),         mve_scalar_shift),
25448  ToC("uqrshl",  ea500f0d, 2, (RRnpcsp, RRnpcsp),      mve_scalar_shift),
25449  ToC("sqrshr",  ea500f2d, 2, (RRnpcsp, RRnpcsp),      mve_scalar_shift),
25450  ToC("uqshl",   ea500f0f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25451  ToC("urshr",   ea500f1f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25452  ToC("srshr",   ea500f2f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25453  ToC("sqshl",   ea500f3f, 2, (RRnpcsp, I32),          mve_scalar_shift),
25454
25455  ToC("vpt",     ee410f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25456  ToC("vptt",    ee018f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25457  ToC("vpte",    ee418f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25458  ToC("vpttt",   ee014f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25459  ToC("vptte",   ee01cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25460  ToC("vptet",   ee41cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25461  ToC("vptee",   ee414f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25462  ToC("vptttt",  ee012f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25463  ToC("vpttte",  ee016f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25464  ToC("vpttet",  ee01ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25465  ToC("vpttee",  ee01af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25466  ToC("vptett",  ee41af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25467  ToC("vptete",  ee41ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25468  ToC("vpteet",  ee416f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25469  ToC("vpteee",  ee412f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
25470
25471  ToC("vpst",    fe710f4d, 0, (), mve_vpt),
25472  ToC("vpstt",   fe318f4d, 0, (), mve_vpt),
25473  ToC("vpste",   fe718f4d, 0, (), mve_vpt),
25474  ToC("vpsttt",  fe314f4d, 0, (), mve_vpt),
25475  ToC("vpstte",  fe31cf4d, 0, (), mve_vpt),
25476  ToC("vpstet",  fe71cf4d, 0, (), mve_vpt),
25477  ToC("vpstee",  fe714f4d, 0, (), mve_vpt),
25478  ToC("vpstttt", fe312f4d, 0, (), mve_vpt),
25479  ToC("vpsttte", fe316f4d, 0, (), mve_vpt),
25480  ToC("vpsttet", fe31ef4d, 0, (), mve_vpt),
25481  ToC("vpsttee", fe31af4d, 0, (), mve_vpt),
25482  ToC("vpstett", fe71af4d, 0, (), mve_vpt),
25483  ToC("vpstete", fe71ef4d, 0, (), mve_vpt),
25484  ToC("vpsteet", fe716f4d, 0, (), mve_vpt),
25485  ToC("vpsteee", fe712f4d, 0, (), mve_vpt),
25486
25487  /* MVE and MVE FP only.  */
25488  mToC("vhcadd", ee000f00,   4, (RMQ, RMQ, RMQ, EXPi),             mve_vhcadd),
25489  mCEF(vadc,     _vadc,      3, (RMQ, RMQ, RMQ),                   mve_vadc),
25490  mCEF(vadci,    _vadci,     3, (RMQ, RMQ, RMQ),                   mve_vadc),
25491  mToC("vsbc",   fe300f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
25492  mToC("vsbci",  fe301f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
25493  mCEF(vmullb,   _vmullb,    3, (RMQ, RMQ, RMQ),                   mve_vmull),
25494  mCEF(vabav,    _vabav,     3, (RRnpcsp, RMQ, RMQ),               mve_vabav),
25495  mCEF(vmladav,    _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25496  mCEF(vmladava,   _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25497  mCEF(vmladavx,   _vmladavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25498  mCEF(vmladavax,  _vmladavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
25499  mCEF(vmlav,      _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25500  mCEF(vmlava,     _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25501  mCEF(vmlsdav,    _vmlsdav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
25502  mCEF(vmlsdava,   _vmlsdava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25503  mCEF(vmlsdavx,   _vmlsdavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
25504  mCEF(vmlsdavax,  _vmlsdavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
25505
25506  mCEF(vst20,    _vst20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25507  mCEF(vst21,    _vst21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25508  mCEF(vst40,    _vst40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25509  mCEF(vst41,    _vst41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25510  mCEF(vst42,    _vst42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25511  mCEF(vst43,    _vst43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25512  mCEF(vld20,    _vld20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25513  mCEF(vld21,    _vld21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
25514  mCEF(vld40,    _vld40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25515  mCEF(vld41,    _vld41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25516  mCEF(vld42,    _vld42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25517  mCEF(vld43,    _vld43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
25518  mCEF(vstrb,    _vstrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25519  mCEF(vstrh,    _vstrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25520  mCEF(vstrw,    _vstrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25521  mCEF(vstrd,    _vstrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25522  mCEF(vldrb,    _vldrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25523  mCEF(vldrh,    _vldrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25524  mCEF(vldrw,    _vldrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25525  mCEF(vldrd,    _vldrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
25526
25527  mCEF(vmovnt,   _vmovnt,    2, (RMQ, RMQ),                        mve_movn),
25528  mCEF(vmovnb,   _vmovnb,    2, (RMQ, RMQ),                        mve_movn),
25529  mCEF(vbrsr,    _vbrsr,     3, (RMQ, RMQ, RR),                    mve_vbrsr),
25530  mCEF(vaddlv,   _vaddlv,    3, (RRe, RRo, RMQ),                   mve_vaddlv),
25531  mCEF(vaddlva,  _vaddlva,   3, (RRe, RRo, RMQ),                   mve_vaddlv),
25532  mCEF(vaddv,    _vaddv,     2, (RRe, RMQ),                        mve_vaddv),
25533  mCEF(vaddva,   _vaddva,    2, (RRe, RMQ),                        mve_vaddv),
25534  mCEF(vddup,    _vddup,     3, (RMQ, RRe, EXPi),                  mve_viddup),
25535  mCEF(vdwdup,   _vdwdup,    4, (RMQ, RRe, RR, EXPi),              mve_viddup),
25536  mCEF(vidup,    _vidup,     3, (RMQ, RRe, EXPi),                  mve_viddup),
25537  mCEF(viwdup,   _viwdup,    4, (RMQ, RRe, RR, EXPi),              mve_viddup),
25538  mToC("vmaxa",  ee330e81,   2, (RMQ, RMQ),                        mve_vmaxa_vmina),
25539  mToC("vmina",  ee331e81,   2, (RMQ, RMQ),                        mve_vmaxa_vmina),
25540  mCEF(vmaxv,    _vmaxv,   2, (RR, RMQ),                           mve_vmaxv),
25541  mCEF(vmaxav,   _vmaxav,  2, (RR, RMQ),                           mve_vmaxv),
25542  mCEF(vminv,    _vminv,   2, (RR, RMQ),                           mve_vmaxv),
25543  mCEF(vminav,   _vminav,  2, (RR, RMQ),                           mve_vmaxv),
25544
25545  mCEF(vmlaldav,   _vmlaldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25546  mCEF(vmlaldava,  _vmlaldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25547  mCEF(vmlaldavx,  _vmlaldavx,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25548  mCEF(vmlaldavax, _vmlaldavax,  4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25549  mCEF(vmlalv,     _vmlaldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25550  mCEF(vmlalva,    _vmlaldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25551  mCEF(vmlsldav,   _vmlsldav,    4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25552  mCEF(vmlsldava,  _vmlsldava,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25553  mCEF(vmlsldavx,  _vmlsldavx,   4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25554  mCEF(vmlsldavax, _vmlsldavax,  4, (RRe, RRo, RMQ, RMQ),        mve_vmlaldav),
25555  mToC("vrmlaldavh", ee800f00,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25556  mToC("vrmlaldavha",ee800f20,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25557  mCEF(vrmlaldavhx,  _vrmlaldavhx,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25558  mCEF(vrmlaldavhax, _vrmlaldavhax, 4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25559  mToC("vrmlalvh",   ee800f00,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25560  mToC("vrmlalvha",  ee800f20,      4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25561  mCEF(vrmlsldavh,   _vrmlsldavh,   4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25562  mCEF(vrmlsldavha,  _vrmlsldavha,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25563  mCEF(vrmlsldavhx,  _vrmlsldavhx,  4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25564  mCEF(vrmlsldavhax, _vrmlsldavhax, 4, (RRe, RR, RMQ, RMQ),  mve_vrmlaldavh),
25565
25566  mToC("vmlas",    ee011e40,     3, (RMQ, RMQ, RR),              mve_vmlas),
25567  mToC("vmulh",    ee010e01,     3, (RMQ, RMQ, RMQ),             mve_vmulh),
25568  mToC("vrmulh",   ee011e01,     3, (RMQ, RMQ, RMQ),             mve_vmulh),
25569  mToC("vpnot",    fe310f4d,     0, (),                          mve_vpnot),
25570  mToC("vpsel",    fe310f01,     3, (RMQ, RMQ, RMQ),             mve_vpsel),
25571
25572  mToC("vqdmladh",  ee000e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25573  mToC("vqdmladhx", ee001e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25574  mToC("vqrdmladh", ee000e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25575  mToC("vqrdmladhx",ee001e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25576  mToC("vqdmlsdh",  fe000e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25577  mToC("vqdmlsdhx", fe001e00,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25578  mToC("vqrdmlsdh", fe000e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25579  mToC("vqrdmlsdhx",fe001e01,    3, (RMQ, RMQ, RMQ),             mve_vqdmladh),
25580  mToC("vqdmlah",   ee000e60,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25581  mToC("vqdmlash",  ee001e60,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25582  mToC("vqrdmlash", ee001e40,    3, (RMQ, RMQ, RR),              mve_vqdmlah),
25583  mToC("vqdmullt",  ee301f00,    3, (RMQ, RMQ, RMQRR),           mve_vqdmull),
25584  mToC("vqdmullb",  ee300f00,    3, (RMQ, RMQ, RMQRR),           mve_vqdmull),
25585  mCEF(vqmovnt,    _vqmovnt,     2, (RMQ, RMQ),                  mve_vqmovn),
25586  mCEF(vqmovnb,    _vqmovnb,     2, (RMQ, RMQ),                  mve_vqmovn),
25587  mCEF(vqmovunt,   _vqmovunt,    2, (RMQ, RMQ),                  mve_vqmovn),
25588  mCEF(vqmovunb,   _vqmovunb,    2, (RMQ, RMQ),                  mve_vqmovn),
25589
25590  mCEF(vshrnt,     _vshrnt,      3, (RMQ, RMQ, I32z),    mve_vshrn),
25591  mCEF(vshrnb,     _vshrnb,      3, (RMQ, RMQ, I32z),    mve_vshrn),
25592  mCEF(vrshrnt,    _vrshrnt,     3, (RMQ, RMQ, I32z),    mve_vshrn),
25593  mCEF(vrshrnb,    _vrshrnb,     3, (RMQ, RMQ, I32z),    mve_vshrn),
25594  mCEF(vqshrnt,    _vqrshrnt,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25595  mCEF(vqshrnb,    _vqrshrnb,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25596  mCEF(vqshrunt,   _vqrshrunt,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25597  mCEF(vqshrunb,   _vqrshrunb,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25598  mCEF(vqrshrnt,   _vqrshrnt,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25599  mCEF(vqrshrnb,   _vqrshrnb,    3, (RMQ, RMQ, I32z),    mve_vshrn),
25600  mCEF(vqrshrunt,  _vqrshrunt,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25601  mCEF(vqrshrunb,  _vqrshrunb,   3, (RMQ, RMQ, I32z),    mve_vshrn),
25602
25603  mToC("vshlc",      eea00fc0,      3, (RMQ, RR, I32z),      mve_vshlc),
25604  mToC("vshllt",     ee201e00,      3, (RMQ, RMQ, I32),      mve_vshll),
25605  mToC("vshllb",     ee200e00,      3, (RMQ, RMQ, I32),      mve_vshll),
25606
25607  toU("dlstp",   _dlstp, 2, (LR, RR),      t_loloop),
25608  toU("wlstp",   _wlstp, 3, (LR, RR, EXP), t_loloop),
25609  toU("letp",    _letp,  2, (LR, EXP),     t_loloop),
25610  toU("lctp",    _lctp,  0, (),            t_loloop),
25611
25612 #undef THUMB_VARIANT
25613 #define THUMB_VARIANT & mve_fp_ext
25614  mToC("vcmul", ee300e00,   4, (RMQ, RMQ, RMQ, EXPi),              mve_vcmul),
25615  mToC("vfmas", ee311e40,   3, (RMQ, RMQ, RR),                     mve_vfmas),
25616  mToC("vmaxnma", ee3f0e81, 2, (RMQ, RMQ),                         mve_vmaxnma_vminnma),
25617  mToC("vminnma", ee3f1e81, 2, (RMQ, RMQ),                         mve_vmaxnma_vminnma),
25618  mToC("vmaxnmv", eeee0f00, 2, (RR, RMQ),                          mve_vmaxnmv),
25619  mToC("vmaxnmav",eeec0f00, 2, (RR, RMQ),                          mve_vmaxnmv),
25620  mToC("vminnmv", eeee0f80, 2, (RR, RMQ),                          mve_vmaxnmv),
25621  mToC("vminnmav",eeec0f80, 2, (RR, RMQ),                          mve_vmaxnmv),
25622
25623 #undef  ARM_VARIANT
25624 #define ARM_VARIANT  & fpu_vfp_ext_v1
25625 #undef  THUMB_VARIANT
25626 #define THUMB_VARIANT  & arm_ext_v6t2
25627  mnCEF(vmla,     _vmla,    3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ_RR), neon_mac_maybe_scalar),
25628  mnCEF(vmul,     _vmul,    3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ_RR), neon_mul),
25629
25630  mcCE(fcpyd,    eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
25631
25632 #undef  ARM_VARIANT
25633 #define ARM_VARIANT  & fpu_vfp_ext_v1xd
25634
25635  MNCE(vmov,   0,        1, (VMOV),            neon_mov),
25636  mcCE(fmrs,     e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
25637  mcCE(fmsr,     e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
25638  mcCE(fcpys,    eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
25639
25640  mCEF(vmullt, _vmullt,  3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ),  mve_vmull),
25641  mnCEF(vadd,  _vadd,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
25642  mnCEF(vsub,  _vsub,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
25643
25644  MNCEF(vabs,  1b10300,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
25645  MNCEF(vneg,  1b10380,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
25646
25647  mCEF(vmovlt, _vmovlt,  1, (VMOV),              mve_movl),
25648  mCEF(vmovlb, _vmovlb,  1, (VMOV),              mve_movl),
25649
25650  mnCE(vcmp,      _vcmp,    3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
25651  mnCE(vcmpe,     _vcmpe,   3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
25652
25653 #undef  ARM_VARIANT
25654 #define ARM_VARIANT  & fpu_vfp_ext_v2
25655
25656  mcCE(fmsrr,    c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
25657  mcCE(fmrrs,    c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
25658  mcCE(fmdrr,    c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
25659  mcCE(fmrrd,    c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
25660
25661 #undef  ARM_VARIANT
25662 #define ARM_VARIANT    & fpu_vfp_ext_armv8xd
25663  mnUF(vcvta,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvta),
25664  mnUF(vcvtp,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtp),
25665  mnUF(vcvtn,  _vcvta,  3, (RNSDQMQ, oRNSDQMQ, oI32z),   neon_cvtn),
25666  mnUF(vcvtm,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtm),
25667  mnUF(vmaxnm, _vmaxnm, 3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ), vmaxnm),
25668  mnUF(vminnm, _vminnm, 3, (RNSDQMQ, oRNSDQMQ, RNSDQMQ), vmaxnm),
25669
25670 #undef  ARM_VARIANT
25671 #define ARM_VARIANT & fpu_neon_ext_v1
25672  mnUF(vabd,      _vabd,           3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25673  mnUF(vabdl,     _vabdl,          3, (RNQMQ, RNDMQ, RNDMQ),   neon_dyadic_long),
25674  mnUF(vaddl,     _vaddl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
25675  mnUF(vsubl,     _vsubl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
25676  mnUF(vand,      _vand,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25677  mnUF(vbic,      _vbic,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25678  mnUF(vorr,      _vorr,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25679  mnUF(vorn,      _vorn,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
25680  mnUF(veor,      _veor,           3, (RNDQMQ, oRNDQMQ, RNDQMQ),      neon_logic),
25681  MNUF(vcls,      1b00400,         2, (RNDQMQ, RNDQMQ),               neon_cls),
25682  MNUF(vclz,      1b00480,         2, (RNDQMQ, RNDQMQ),               neon_clz),
25683  mnCE(vdup,      _vdup,           2, (RNDQMQ, RR_RNSC),              neon_dup),
25684  MNUF(vhadd,     00000000,        3, (RNDQMQ, oRNDQMQ, RNDQMQR),  neon_dyadic_i_su),
25685  MNUF(vrhadd,    00000100,        3, (RNDQMQ, oRNDQMQ, RNDQMQ),   neon_dyadic_i_su),
25686  MNUF(vhsub,     00000200,        3, (RNDQMQ, oRNDQMQ, RNDQMQR),  neon_dyadic_i_su),
25687  mnUF(vmin,      _vmin,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25688  mnUF(vmax,      _vmax,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
25689  MNUF(vqadd,     0000010,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_dyadic_i64_su),
25690  MNUF(vqsub,     0000210,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_dyadic_i64_su),
25691  mnUF(vmvn,      _vmvn,    2, (RNDQMQ, RNDQMQ_Ibig), neon_mvn),
25692  MNUF(vqabs,     1b00700,  2, (RNDQMQ, RNDQMQ),     neon_sat_abs_neg),
25693  MNUF(vqneg,     1b00780,  2, (RNDQMQ, RNDQMQ),     neon_sat_abs_neg),
25694  mnUF(vqrdmlah,  _vqrdmlah,3, (RNDQMQ, oRNDQMQ, RNDQ_RNSC_RR), neon_qrdmlah),
25695  mnUF(vqdmulh,   _vqdmulh, 3, (RNDQMQ, oRNDQMQ, RNDQMQ_RNSC_RR), neon_qdmulh),
25696  mnUF(vqrdmulh,  _vqrdmulh,3, (RNDQMQ, oRNDQMQ, RNDQMQ_RNSC_RR), neon_qdmulh),
25697  MNUF(vqrshl,    0000510,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_rshl),
25698  MNUF(vrshl,     0000500,  3, (RNDQMQ, oRNDQMQ, RNDQMQR), neon_rshl),
25699  MNUF(vshr,      0800010,  3, (RNDQMQ, oRNDQMQ, I64z), neon_rshift_round_imm),
25700  MNUF(vrshr,     0800210,  3, (RNDQMQ, oRNDQMQ, I64z), neon_rshift_round_imm),
25701  MNUF(vsli,      1800510,  3, (RNDQMQ, oRNDQMQ, I63),  neon_sli),
25702  MNUF(vsri,      1800410,  3, (RNDQMQ, oRNDQMQ, I64z), neon_sri),
25703  MNUF(vrev64,    1b00000,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25704  MNUF(vrev32,    1b00080,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25705  MNUF(vrev16,    1b00100,  2, (RNDQMQ, RNDQMQ),     neon_rev),
25706  mnUF(vshl,      _vshl,    3, (RNDQMQ, oRNDQMQ, RNDQMQ_I63b_RR), neon_shl),
25707  mnUF(vqshl,     _vqshl,   3, (RNDQMQ, oRNDQMQ, RNDQMQ_I63b_RR), neon_qshl),
25708  MNUF(vqshlu,    1800610,  3, (RNDQMQ, oRNDQMQ, I63),            neon_qshlu_imm),
25709
25710 #undef  ARM_VARIANT
25711 #define ARM_VARIANT & arm_ext_v8_3
25712 #undef  THUMB_VARIANT
25713 #define THUMB_VARIANT & arm_ext_v6t2_v8m
25714  MNUF (vcadd, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ, EXPi), vcadd),
25715  MNUF (vcmla, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ_RNSC, EXPi), vcmla),
25716 };
25717 #undef ARM_VARIANT
25718 #undef THUMB_VARIANT
25719 #undef TCE
25720 #undef TUE
25721 #undef TUF
25722 #undef TCC
25723 #undef cCE
25724 #undef cCL
25725 #undef C3E
25726 #undef C3
25727 #undef CE
25728 #undef CM
25729 #undef CL
25730 #undef UE
25731 #undef UF
25732 #undef UT
25733 #undef NUF
25734 #undef nUF
25735 #undef NCE
25736 #undef nCE
25737 #undef OPS0
25738 #undef OPS1
25739 #undef OPS2
25740 #undef OPS3
25741 #undef OPS4
25742 #undef OPS5
25743 #undef OPS6
25744 #undef do_0
25745 #undef ToC
25746 #undef toC
25747 #undef ToU
25748 #undef toU
25749 \f
25750 /* MD interface: bits in the object file.  */
25751
25752 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
25753    for use in the a.out file, and stores them in the array pointed to by buf.
25754    This knows about the endian-ness of the target machine and does
25755    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
25756    2 (short) and 4 (long)  Floating numbers are put out as a series of
25757    LITTLENUMS (shorts, here at least).  */
25758
25759 void
25760 md_number_to_chars (char * buf, valueT val, int n)
25761 {
25762   if (target_big_endian)
25763     number_to_chars_bigendian (buf, val, n);
25764   else
25765     number_to_chars_littleendian (buf, val, n);
25766 }
25767
25768 static valueT
25769 md_chars_to_number (char * buf, int n)
25770 {
25771   valueT result = 0;
25772   unsigned char * where = (unsigned char *) buf;
25773
25774   if (target_big_endian)
25775     {
25776       while (n--)
25777         {
25778           result <<= 8;
25779           result |= (*where++ & 255);
25780         }
25781     }
25782   else
25783     {
25784       while (n--)
25785         {
25786           result <<= 8;
25787           result |= (where[n] & 255);
25788         }
25789     }
25790
25791   return result;
25792 }
25793
25794 /* MD interface: Sections.  */
25795
25796 /* Calculate the maximum variable size (i.e., excluding fr_fix)
25797    that an rs_machine_dependent frag may reach.  */
25798
25799 unsigned int
25800 arm_frag_max_var (fragS *fragp)
25801 {
25802   /* We only use rs_machine_dependent for variable-size Thumb instructions,
25803      which are either THUMB_SIZE (2) or INSN_SIZE (4).
25804
25805      Note that we generate relaxable instructions even for cases that don't
25806      really need it, like an immediate that's a trivial constant.  So we're
25807      overestimating the instruction size for some of those cases.  Rather
25808      than putting more intelligence here, it would probably be better to
25809      avoid generating a relaxation frag in the first place when it can be
25810      determined up front that a short instruction will suffice.  */
25811
25812   gas_assert (fragp->fr_type == rs_machine_dependent);
25813   return INSN_SIZE;
25814 }
25815
25816 /* Estimate the size of a frag before relaxing.  Assume everything fits in
25817    2 bytes.  */
25818
25819 int
25820 md_estimate_size_before_relax (fragS * fragp,
25821                                segT    segtype ATTRIBUTE_UNUSED)
25822 {
25823   fragp->fr_var = 2;
25824   return 2;
25825 }
25826
25827 /* Convert a machine dependent frag.  */
25828
25829 void
25830 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
25831 {
25832   unsigned long insn;
25833   unsigned long old_op;
25834   char *buf;
25835   expressionS exp;
25836   fixS *fixp;
25837   int reloc_type;
25838   int pc_rel;
25839   int opcode;
25840
25841   buf = fragp->fr_literal + fragp->fr_fix;
25842
25843   old_op = bfd_get_16(abfd, buf);
25844   if (fragp->fr_symbol)
25845     {
25846       exp.X_op = O_symbol;
25847       exp.X_add_symbol = fragp->fr_symbol;
25848     }
25849   else
25850     {
25851       exp.X_op = O_constant;
25852     }
25853   exp.X_add_number = fragp->fr_offset;
25854   opcode = fragp->fr_subtype;
25855   switch (opcode)
25856     {
25857     case T_MNEM_ldr_pc:
25858     case T_MNEM_ldr_pc2:
25859     case T_MNEM_ldr_sp:
25860     case T_MNEM_str_sp:
25861     case T_MNEM_ldr:
25862     case T_MNEM_ldrb:
25863     case T_MNEM_ldrh:
25864     case T_MNEM_str:
25865     case T_MNEM_strb:
25866     case T_MNEM_strh:
25867       if (fragp->fr_var == 4)
25868         {
25869           insn = THUMB_OP32 (opcode);
25870           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
25871             {
25872               insn |= (old_op & 0x700) << 4;
25873             }
25874           else
25875             {
25876               insn |= (old_op & 7) << 12;
25877               insn |= (old_op & 0x38) << 13;
25878             }
25879           insn |= 0x00000c00;
25880           put_thumb32_insn (buf, insn);
25881           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
25882         }
25883       else
25884         {
25885           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
25886         }
25887       pc_rel = (opcode == T_MNEM_ldr_pc2);
25888       break;
25889     case T_MNEM_adr:
25890       if (fragp->fr_var == 4)
25891         {
25892           insn = THUMB_OP32 (opcode);
25893           insn |= (old_op & 0xf0) << 4;
25894           put_thumb32_insn (buf, insn);
25895           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
25896         }
25897       else
25898         {
25899           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25900           exp.X_add_number -= 4;
25901         }
25902       pc_rel = 1;
25903       break;
25904     case T_MNEM_mov:
25905     case T_MNEM_movs:
25906     case T_MNEM_cmp:
25907     case T_MNEM_cmn:
25908       if (fragp->fr_var == 4)
25909         {
25910           int r0off = (opcode == T_MNEM_mov
25911                        || opcode == T_MNEM_movs) ? 0 : 8;
25912           insn = THUMB_OP32 (opcode);
25913           insn = (insn & 0xe1ffffff) | 0x10000000;
25914           insn |= (old_op & 0x700) << r0off;
25915           put_thumb32_insn (buf, insn);
25916           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
25917         }
25918       else
25919         {
25920           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
25921         }
25922       pc_rel = 0;
25923       break;
25924     case T_MNEM_b:
25925       if (fragp->fr_var == 4)
25926         {
25927           insn = THUMB_OP32(opcode);
25928           put_thumb32_insn (buf, insn);
25929           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
25930         }
25931       else
25932         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
25933       pc_rel = 1;
25934       break;
25935     case T_MNEM_bcond:
25936       if (fragp->fr_var == 4)
25937         {
25938           insn = THUMB_OP32(opcode);
25939           insn |= (old_op & 0xf00) << 14;
25940           put_thumb32_insn (buf, insn);
25941           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
25942         }
25943       else
25944         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
25945       pc_rel = 1;
25946       break;
25947     case T_MNEM_add_sp:
25948     case T_MNEM_add_pc:
25949     case T_MNEM_inc_sp:
25950     case T_MNEM_dec_sp:
25951       if (fragp->fr_var == 4)
25952         {
25953           /* ??? Choose between add and addw.  */
25954           insn = THUMB_OP32 (opcode);
25955           insn |= (old_op & 0xf0) << 4;
25956           put_thumb32_insn (buf, insn);
25957           if (opcode == T_MNEM_add_pc)
25958             reloc_type = BFD_RELOC_ARM_T32_IMM12;
25959           else
25960             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
25961         }
25962       else
25963         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25964       pc_rel = 0;
25965       break;
25966
25967     case T_MNEM_addi:
25968     case T_MNEM_addis:
25969     case T_MNEM_subi:
25970     case T_MNEM_subis:
25971       if (fragp->fr_var == 4)
25972         {
25973           insn = THUMB_OP32 (opcode);
25974           insn |= (old_op & 0xf0) << 4;
25975           insn |= (old_op & 0xf) << 16;
25976           put_thumb32_insn (buf, insn);
25977           if (insn & (1 << 20))
25978             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
25979           else
25980             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
25981         }
25982       else
25983         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
25984       pc_rel = 0;
25985       break;
25986     default:
25987       abort ();
25988     }
25989   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
25990                       (enum bfd_reloc_code_real) reloc_type);
25991   fixp->fx_file = fragp->fr_file;
25992   fixp->fx_line = fragp->fr_line;
25993   fragp->fr_fix += fragp->fr_var;
25994
25995   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
25996   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
25997       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
25998     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
25999 }
26000
26001 /* Return the size of a relaxable immediate operand instruction.
26002    SHIFT and SIZE specify the form of the allowable immediate.  */
26003 static int
26004 relax_immediate (fragS *fragp, int size, int shift)
26005 {
26006   offsetT offset;
26007   offsetT mask;
26008   offsetT low;
26009
26010   /* ??? Should be able to do better than this.  */
26011   if (fragp->fr_symbol)
26012     return 4;
26013
26014   low = (1 << shift) - 1;
26015   mask = (1 << (shift + size)) - (1 << shift);
26016   offset = fragp->fr_offset;
26017   /* Force misaligned offsets to 32-bit variant.  */
26018   if (offset & low)
26019     return 4;
26020   if (offset & ~mask)
26021     return 4;
26022   return 2;
26023 }
26024
26025 /* Get the address of a symbol during relaxation.  */
26026 static addressT
26027 relaxed_symbol_addr (fragS *fragp, long stretch)
26028 {
26029   fragS *sym_frag;
26030   addressT addr;
26031   symbolS *sym;
26032
26033   sym = fragp->fr_symbol;
26034   sym_frag = symbol_get_frag (sym);
26035   know (S_GET_SEGMENT (sym) != absolute_section
26036         || sym_frag == &zero_address_frag);
26037   addr = S_GET_VALUE (sym) + fragp->fr_offset;
26038
26039   /* If frag has yet to be reached on this pass, assume it will
26040      move by STRETCH just as we did.  If this is not so, it will
26041      be because some frag between grows, and that will force
26042      another pass.  */
26043
26044   if (stretch != 0
26045       && sym_frag->relax_marker != fragp->relax_marker)
26046     {
26047       fragS *f;
26048
26049       /* Adjust stretch for any alignment frag.  Note that if have
26050          been expanding the earlier code, the symbol may be
26051          defined in what appears to be an earlier frag.  FIXME:
26052          This doesn't handle the fr_subtype field, which specifies
26053          a maximum number of bytes to skip when doing an
26054          alignment.  */
26055       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
26056         {
26057           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
26058             {
26059               if (stretch < 0)
26060                 stretch = - ((- stretch)
26061                              & ~ ((1 << (int) f->fr_offset) - 1));
26062               else
26063                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
26064               if (stretch == 0)
26065                 break;
26066             }
26067         }
26068       if (f != NULL)
26069         addr += stretch;
26070     }
26071
26072   return addr;
26073 }
26074
26075 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
26076    load.  */
26077 static int
26078 relax_adr (fragS *fragp, asection *sec, long stretch)
26079 {
26080   addressT addr;
26081   offsetT val;
26082
26083   /* Assume worst case for symbols not known to be in the same section.  */
26084   if (fragp->fr_symbol == NULL
26085       || !S_IS_DEFINED (fragp->fr_symbol)
26086       || sec != S_GET_SEGMENT (fragp->fr_symbol)
26087       || S_IS_WEAK (fragp->fr_symbol))
26088     return 4;
26089
26090   val = relaxed_symbol_addr (fragp, stretch);
26091   addr = fragp->fr_address + fragp->fr_fix;
26092   addr = (addr + 4) & ~3;
26093   /* Force misaligned targets to 32-bit variant.  */
26094   if (val & 3)
26095     return 4;
26096   val -= addr;
26097   if (val < 0 || val > 1020)
26098     return 4;
26099   return 2;
26100 }
26101
26102 /* Return the size of a relaxable add/sub immediate instruction.  */
26103 static int
26104 relax_addsub (fragS *fragp, asection *sec)
26105 {
26106   char *buf;
26107   int op;
26108
26109   buf = fragp->fr_literal + fragp->fr_fix;
26110   op = bfd_get_16(sec->owner, buf);
26111   if ((op & 0xf) == ((op >> 4) & 0xf))
26112     return relax_immediate (fragp, 8, 0);
26113   else
26114     return relax_immediate (fragp, 3, 0);
26115 }
26116
26117 /* Return TRUE iff the definition of symbol S could be pre-empted
26118    (overridden) at link or load time.  */
26119 static bfd_boolean
26120 symbol_preemptible (symbolS *s)
26121 {
26122   /* Weak symbols can always be pre-empted.  */
26123   if (S_IS_WEAK (s))
26124     return TRUE;
26125
26126   /* Non-global symbols cannot be pre-empted. */
26127   if (! S_IS_EXTERNAL (s))
26128     return FALSE;
26129
26130 #ifdef OBJ_ELF
26131   /* In ELF, a global symbol can be marked protected, or private.  In that
26132      case it can't be pre-empted (other definitions in the same link unit
26133      would violate the ODR).  */
26134   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
26135     return FALSE;
26136 #endif
26137
26138   /* Other global symbols might be pre-empted.  */
26139   return TRUE;
26140 }
26141
26142 /* Return the size of a relaxable branch instruction.  BITS is the
26143    size of the offset field in the narrow instruction.  */
26144
26145 static int
26146 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
26147 {
26148   addressT addr;
26149   offsetT val;
26150   offsetT limit;
26151
26152   /* Assume worst case for symbols not known to be in the same section.  */
26153   if (!S_IS_DEFINED (fragp->fr_symbol)
26154       || sec != S_GET_SEGMENT (fragp->fr_symbol)
26155       || S_IS_WEAK (fragp->fr_symbol))
26156     return 4;
26157
26158 #ifdef OBJ_ELF
26159   /* A branch to a function in ARM state will require interworking.  */
26160   if (S_IS_DEFINED (fragp->fr_symbol)
26161       && ARM_IS_FUNC (fragp->fr_symbol))
26162       return 4;
26163 #endif
26164
26165   if (symbol_preemptible (fragp->fr_symbol))
26166     return 4;
26167
26168   val = relaxed_symbol_addr (fragp, stretch);
26169   addr = fragp->fr_address + fragp->fr_fix + 4;
26170   val -= addr;
26171
26172   /* Offset is a signed value *2 */
26173   limit = 1 << bits;
26174   if (val >= limit || val < -limit)
26175     return 4;
26176   return 2;
26177 }
26178
26179
26180 /* Relax a machine dependent frag.  This returns the amount by which
26181    the current size of the frag should change.  */
26182
26183 int
26184 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
26185 {
26186   int oldsize;
26187   int newsize;
26188
26189   oldsize = fragp->fr_var;
26190   switch (fragp->fr_subtype)
26191     {
26192     case T_MNEM_ldr_pc2:
26193       newsize = relax_adr (fragp, sec, stretch);
26194       break;
26195     case T_MNEM_ldr_pc:
26196     case T_MNEM_ldr_sp:
26197     case T_MNEM_str_sp:
26198       newsize = relax_immediate (fragp, 8, 2);
26199       break;
26200     case T_MNEM_ldr:
26201     case T_MNEM_str:
26202       newsize = relax_immediate (fragp, 5, 2);
26203       break;
26204     case T_MNEM_ldrh:
26205     case T_MNEM_strh:
26206       newsize = relax_immediate (fragp, 5, 1);
26207       break;
26208     case T_MNEM_ldrb:
26209     case T_MNEM_strb:
26210       newsize = relax_immediate (fragp, 5, 0);
26211       break;
26212     case T_MNEM_adr:
26213       newsize = relax_adr (fragp, sec, stretch);
26214       break;
26215     case T_MNEM_mov:
26216     case T_MNEM_movs:
26217     case T_MNEM_cmp:
26218     case T_MNEM_cmn:
26219       newsize = relax_immediate (fragp, 8, 0);
26220       break;
26221     case T_MNEM_b:
26222       newsize = relax_branch (fragp, sec, 11, stretch);
26223       break;
26224     case T_MNEM_bcond:
26225       newsize = relax_branch (fragp, sec, 8, stretch);
26226       break;
26227     case T_MNEM_add_sp:
26228     case T_MNEM_add_pc:
26229       newsize = relax_immediate (fragp, 8, 2);
26230       break;
26231     case T_MNEM_inc_sp:
26232     case T_MNEM_dec_sp:
26233       newsize = relax_immediate (fragp, 7, 2);
26234       break;
26235     case T_MNEM_addi:
26236     case T_MNEM_addis:
26237     case T_MNEM_subi:
26238     case T_MNEM_subis:
26239       newsize = relax_addsub (fragp, sec);
26240       break;
26241     default:
26242       abort ();
26243     }
26244
26245   fragp->fr_var = newsize;
26246   /* Freeze wide instructions that are at or before the same location as
26247      in the previous pass.  This avoids infinite loops.
26248      Don't freeze them unconditionally because targets may be artificially
26249      misaligned by the expansion of preceding frags.  */
26250   if (stretch <= 0 && newsize > 2)
26251     {
26252       md_convert_frag (sec->owner, sec, fragp);
26253       frag_wane (fragp);
26254     }
26255
26256   return newsize - oldsize;
26257 }
26258
26259 /* Round up a section size to the appropriate boundary.  */
26260
26261 valueT
26262 md_section_align (segT   segment ATTRIBUTE_UNUSED,
26263                   valueT size)
26264 {
26265   return size;
26266 }
26267
26268 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
26269    of an rs_align_code fragment.  */
26270
26271 void
26272 arm_handle_align (fragS * fragP)
26273 {
26274   static unsigned char const arm_noop[2][2][4] =
26275     {
26276       {  /* ARMv1 */
26277         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
26278         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
26279       },
26280       {  /* ARMv6k */
26281         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
26282         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
26283       },
26284     };
26285   static unsigned char const thumb_noop[2][2][2] =
26286     {
26287       {  /* Thumb-1 */
26288         {0xc0, 0x46},  /* LE */
26289         {0x46, 0xc0},  /* BE */
26290       },
26291       {  /* Thumb-2 */
26292         {0x00, 0xbf},  /* LE */
26293         {0xbf, 0x00}   /* BE */
26294       }
26295     };
26296   static unsigned char const wide_thumb_noop[2][4] =
26297     {  /* Wide Thumb-2 */
26298       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
26299       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
26300     };
26301
26302   unsigned bytes, fix, noop_size;
26303   char * p;
26304   const unsigned char * noop;
26305   const unsigned char *narrow_noop = NULL;
26306 #ifdef OBJ_ELF
26307   enum mstate state;
26308 #endif
26309
26310   if (fragP->fr_type != rs_align_code)
26311     return;
26312
26313   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
26314   p = fragP->fr_literal + fragP->fr_fix;
26315   fix = 0;
26316
26317   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
26318     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
26319
26320   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
26321
26322   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
26323     {
26324       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
26325                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
26326         {
26327           narrow_noop = thumb_noop[1][target_big_endian];
26328           noop = wide_thumb_noop[target_big_endian];
26329         }
26330       else
26331         noop = thumb_noop[0][target_big_endian];
26332       noop_size = 2;
26333 #ifdef OBJ_ELF
26334       state = MAP_THUMB;
26335 #endif
26336     }
26337   else
26338     {
26339       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
26340                                            ? selected_cpu : arm_arch_none,
26341                                            arm_ext_v6k) != 0]
26342                      [target_big_endian];
26343       noop_size = 4;
26344 #ifdef OBJ_ELF
26345       state = MAP_ARM;
26346 #endif
26347     }
26348
26349   fragP->fr_var = noop_size;
26350
26351   if (bytes & (noop_size - 1))
26352     {
26353       fix = bytes & (noop_size - 1);
26354 #ifdef OBJ_ELF
26355       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
26356 #endif
26357       memset (p, 0, fix);
26358       p += fix;
26359       bytes -= fix;
26360     }
26361
26362   if (narrow_noop)
26363     {
26364       if (bytes & noop_size)
26365         {
26366           /* Insert a narrow noop.  */
26367           memcpy (p, narrow_noop, noop_size);
26368           p += noop_size;
26369           bytes -= noop_size;
26370           fix += noop_size;
26371         }
26372
26373       /* Use wide noops for the remainder */
26374       noop_size = 4;
26375     }
26376
26377   while (bytes >= noop_size)
26378     {
26379       memcpy (p, noop, noop_size);
26380       p += noop_size;
26381       bytes -= noop_size;
26382       fix += noop_size;
26383     }
26384
26385   fragP->fr_fix += fix;
26386 }
26387
26388 /* Called from md_do_align.  Used to create an alignment
26389    frag in a code section.  */
26390
26391 void
26392 arm_frag_align_code (int n, int max)
26393 {
26394   char * p;
26395
26396   /* We assume that there will never be a requirement
26397      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
26398   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
26399     {
26400       char err_msg[128];
26401
26402       sprintf (err_msg,
26403         _("alignments greater than %d bytes not supported in .text sections."),
26404         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
26405       as_fatal ("%s", err_msg);
26406     }
26407
26408   p = frag_var (rs_align_code,
26409                 MAX_MEM_FOR_RS_ALIGN_CODE,
26410                 1,
26411                 (relax_substateT) max,
26412                 (symbolS *) NULL,
26413                 (offsetT) n,
26414                 (char *) NULL);
26415   *p = 0;
26416 }
26417
26418 /* Perform target specific initialisation of a frag.
26419    Note - despite the name this initialisation is not done when the frag
26420    is created, but only when its type is assigned.  A frag can be created
26421    and used a long time before its type is set, so beware of assuming that
26422    this initialisation is performed first.  */
26423
26424 #ifndef OBJ_ELF
26425 void
26426 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
26427 {
26428   /* Record whether this frag is in an ARM or a THUMB area.  */
26429   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
26430 }
26431
26432 #else /* OBJ_ELF is defined.  */
26433 void
26434 arm_init_frag (fragS * fragP, int max_chars)
26435 {
26436   bfd_boolean frag_thumb_mode;
26437
26438   /* If the current ARM vs THUMB mode has not already
26439      been recorded into this frag then do so now.  */
26440   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
26441     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
26442
26443   /* PR 21809: Do not set a mapping state for debug sections
26444      - it just confuses other tools.  */
26445   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
26446     return;
26447
26448   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
26449
26450   /* Record a mapping symbol for alignment frags.  We will delete this
26451      later if the alignment ends up empty.  */
26452   switch (fragP->fr_type)
26453     {
26454     case rs_align:
26455     case rs_align_test:
26456     case rs_fill:
26457       mapping_state_2 (MAP_DATA, max_chars);
26458       break;
26459     case rs_align_code:
26460       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
26461       break;
26462     default:
26463       break;
26464     }
26465 }
26466
26467 /* When we change sections we need to issue a new mapping symbol.  */
26468
26469 void
26470 arm_elf_change_section (void)
26471 {
26472   /* Link an unlinked unwind index table section to the .text section.  */
26473   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
26474       && elf_linked_to_section (now_seg) == NULL)
26475     elf_linked_to_section (now_seg) = text_section;
26476 }
26477
26478 int
26479 arm_elf_section_type (const char * str, size_t len)
26480 {
26481   if (len == 5 && strncmp (str, "exidx", 5) == 0)
26482     return SHT_ARM_EXIDX;
26483
26484   return -1;
26485 }
26486 \f
26487 /* Code to deal with unwinding tables.  */
26488
26489 static void add_unwind_adjustsp (offsetT);
26490
26491 /* Generate any deferred unwind frame offset.  */
26492
26493 static void
26494 flush_pending_unwind (void)
26495 {
26496   offsetT offset;
26497
26498   offset = unwind.pending_offset;
26499   unwind.pending_offset = 0;
26500   if (offset != 0)
26501     add_unwind_adjustsp (offset);
26502 }
26503
26504 /* Add an opcode to this list for this function.  Two-byte opcodes should
26505    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
26506    order.  */
26507
26508 static void
26509 add_unwind_opcode (valueT op, int length)
26510 {
26511   /* Add any deferred stack adjustment.  */
26512   if (unwind.pending_offset)
26513     flush_pending_unwind ();
26514
26515   unwind.sp_restored = 0;
26516
26517   if (unwind.opcode_count + length > unwind.opcode_alloc)
26518     {
26519       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
26520       if (unwind.opcodes)
26521         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
26522                                      unwind.opcode_alloc);
26523       else
26524         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
26525     }
26526   while (length > 0)
26527     {
26528       length--;
26529       unwind.opcodes[unwind.opcode_count] = op & 0xff;
26530       op >>= 8;
26531       unwind.opcode_count++;
26532     }
26533 }
26534
26535 /* Add unwind opcodes to adjust the stack pointer.  */
26536
26537 static void
26538 add_unwind_adjustsp (offsetT offset)
26539 {
26540   valueT op;
26541
26542   if (offset > 0x200)
26543     {
26544       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
26545       char bytes[5];
26546       int n;
26547       valueT o;
26548
26549       /* Long form: 0xb2, uleb128.  */
26550       /* This might not fit in a word so add the individual bytes,
26551          remembering the list is built in reverse order.  */
26552       o = (valueT) ((offset - 0x204) >> 2);
26553       if (o == 0)
26554         add_unwind_opcode (0, 1);
26555
26556       /* Calculate the uleb128 encoding of the offset.  */
26557       n = 0;
26558       while (o)
26559         {
26560           bytes[n] = o & 0x7f;
26561           o >>= 7;
26562           if (o)
26563             bytes[n] |= 0x80;
26564           n++;
26565         }
26566       /* Add the insn.  */
26567       for (; n; n--)
26568         add_unwind_opcode (bytes[n - 1], 1);
26569       add_unwind_opcode (0xb2, 1);
26570     }
26571   else if (offset > 0x100)
26572     {
26573       /* Two short opcodes.  */
26574       add_unwind_opcode (0x3f, 1);
26575       op = (offset - 0x104) >> 2;
26576       add_unwind_opcode (op, 1);
26577     }
26578   else if (offset > 0)
26579     {
26580       /* Short opcode.  */
26581       op = (offset - 4) >> 2;
26582       add_unwind_opcode (op, 1);
26583     }
26584   else if (offset < 0)
26585     {
26586       offset = -offset;
26587       while (offset > 0x100)
26588         {
26589           add_unwind_opcode (0x7f, 1);
26590           offset -= 0x100;
26591         }
26592       op = ((offset - 4) >> 2) | 0x40;
26593       add_unwind_opcode (op, 1);
26594     }
26595 }
26596
26597 /* Finish the list of unwind opcodes for this function.  */
26598
26599 static void
26600 finish_unwind_opcodes (void)
26601 {
26602   valueT op;
26603
26604   if (unwind.fp_used)
26605     {
26606       /* Adjust sp as necessary.  */
26607       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
26608       flush_pending_unwind ();
26609
26610       /* After restoring sp from the frame pointer.  */
26611       op = 0x90 | unwind.fp_reg;
26612       add_unwind_opcode (op, 1);
26613     }
26614   else
26615     flush_pending_unwind ();
26616 }
26617
26618
26619 /* Start an exception table entry.  If idx is nonzero this is an index table
26620    entry.  */
26621
26622 static void
26623 start_unwind_section (const segT text_seg, int idx)
26624 {
26625   const char * text_name;
26626   const char * prefix;
26627   const char * prefix_once;
26628   const char * group_name;
26629   char * sec_name;
26630   int type;
26631   int flags;
26632   int linkonce;
26633
26634   if (idx)
26635     {
26636       prefix = ELF_STRING_ARM_unwind;
26637       prefix_once = ELF_STRING_ARM_unwind_once;
26638       type = SHT_ARM_EXIDX;
26639     }
26640   else
26641     {
26642       prefix = ELF_STRING_ARM_unwind_info;
26643       prefix_once = ELF_STRING_ARM_unwind_info_once;
26644       type = SHT_PROGBITS;
26645     }
26646
26647   text_name = segment_name (text_seg);
26648   if (streq (text_name, ".text"))
26649     text_name = "";
26650
26651   if (strncmp (text_name, ".gnu.linkonce.t.",
26652                strlen (".gnu.linkonce.t.")) == 0)
26653     {
26654       prefix = prefix_once;
26655       text_name += strlen (".gnu.linkonce.t.");
26656     }
26657
26658   sec_name = concat (prefix, text_name, (char *) NULL);
26659
26660   flags = SHF_ALLOC;
26661   linkonce = 0;
26662   group_name = 0;
26663
26664   /* Handle COMDAT group.  */
26665   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
26666     {
26667       group_name = elf_group_name (text_seg);
26668       if (group_name == NULL)
26669         {
26670           as_bad (_("Group section `%s' has no group signature"),
26671                   segment_name (text_seg));
26672           ignore_rest_of_line ();
26673           return;
26674         }
26675       flags |= SHF_GROUP;
26676       linkonce = 1;
26677     }
26678
26679   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
26680                           linkonce, 0);
26681
26682   /* Set the section link for index tables.  */
26683   if (idx)
26684     elf_linked_to_section (now_seg) = text_seg;
26685 }
26686
26687
26688 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
26689    personality routine data.  Returns zero, or the index table value for
26690    an inline entry.  */
26691
26692 static valueT
26693 create_unwind_entry (int have_data)
26694 {
26695   int size;
26696   addressT where;
26697   char *ptr;
26698   /* The current word of data.  */
26699   valueT data;
26700   /* The number of bytes left in this word.  */
26701   int n;
26702
26703   finish_unwind_opcodes ();
26704
26705   /* Remember the current text section.  */
26706   unwind.saved_seg = now_seg;
26707   unwind.saved_subseg = now_subseg;
26708
26709   start_unwind_section (now_seg, 0);
26710
26711   if (unwind.personality_routine == NULL)
26712     {
26713       if (unwind.personality_index == -2)
26714         {
26715           if (have_data)
26716             as_bad (_("handlerdata in cantunwind frame"));
26717           return 1; /* EXIDX_CANTUNWIND.  */
26718         }
26719
26720       /* Use a default personality routine if none is specified.  */
26721       if (unwind.personality_index == -1)
26722         {
26723           if (unwind.opcode_count > 3)
26724             unwind.personality_index = 1;
26725           else
26726             unwind.personality_index = 0;
26727         }
26728
26729       /* Space for the personality routine entry.  */
26730       if (unwind.personality_index == 0)
26731         {
26732           if (unwind.opcode_count > 3)
26733             as_bad (_("too many unwind opcodes for personality routine 0"));
26734
26735           if (!have_data)
26736             {
26737               /* All the data is inline in the index table.  */
26738               data = 0x80;
26739               n = 3;
26740               while (unwind.opcode_count > 0)
26741                 {
26742                   unwind.opcode_count--;
26743                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
26744                   n--;
26745                 }
26746
26747               /* Pad with "finish" opcodes.  */
26748               while (n--)
26749                 data = (data << 8) | 0xb0;
26750
26751               return data;
26752             }
26753           size = 0;
26754         }
26755       else
26756         /* We get two opcodes "free" in the first word.  */
26757         size = unwind.opcode_count - 2;
26758     }
26759   else
26760     {
26761       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
26762       if (unwind.personality_index != -1)
26763         {
26764           as_bad (_("attempt to recreate an unwind entry"));
26765           return 1;
26766         }
26767
26768       /* An extra byte is required for the opcode count.        */
26769       size = unwind.opcode_count + 1;
26770     }
26771
26772   size = (size + 3) >> 2;
26773   if (size > 0xff)
26774     as_bad (_("too many unwind opcodes"));
26775
26776   frag_align (2, 0, 0);
26777   record_alignment (now_seg, 2);
26778   unwind.table_entry = expr_build_dot ();
26779
26780   /* Allocate the table entry.  */
26781   ptr = frag_more ((size << 2) + 4);
26782   /* PR 13449: Zero the table entries in case some of them are not used.  */
26783   memset (ptr, 0, (size << 2) + 4);
26784   where = frag_now_fix () - ((size << 2) + 4);
26785
26786   switch (unwind.personality_index)
26787     {
26788     case -1:
26789       /* ??? Should this be a PLT generating relocation?  */
26790       /* Custom personality routine.  */
26791       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
26792                BFD_RELOC_ARM_PREL31);
26793
26794       where += 4;
26795       ptr += 4;
26796
26797       /* Set the first byte to the number of additional words.  */
26798       data = size > 0 ? size - 1 : 0;
26799       n = 3;
26800       break;
26801
26802     /* ABI defined personality routines.  */
26803     case 0:
26804       /* Three opcodes bytes are packed into the first word.  */
26805       data = 0x80;
26806       n = 3;
26807       break;
26808
26809     case 1:
26810     case 2:
26811       /* The size and first two opcode bytes go in the first word.  */
26812       data = ((0x80 + unwind.personality_index) << 8) | size;
26813       n = 2;
26814       break;
26815
26816     default:
26817       /* Should never happen.  */
26818       abort ();
26819     }
26820
26821   /* Pack the opcodes into words (MSB first), reversing the list at the same
26822      time.  */
26823   while (unwind.opcode_count > 0)
26824     {
26825       if (n == 0)
26826         {
26827           md_number_to_chars (ptr, data, 4);
26828           ptr += 4;
26829           n = 4;
26830           data = 0;
26831         }
26832       unwind.opcode_count--;
26833       n--;
26834       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
26835     }
26836
26837   /* Finish off the last word.  */
26838   if (n < 4)
26839     {
26840       /* Pad with "finish" opcodes.  */
26841       while (n--)
26842         data = (data << 8) | 0xb0;
26843
26844       md_number_to_chars (ptr, data, 4);
26845     }
26846
26847   if (!have_data)
26848     {
26849       /* Add an empty descriptor if there is no user-specified data.   */
26850       ptr = frag_more (4);
26851       md_number_to_chars (ptr, 0, 4);
26852     }
26853
26854   return 0;
26855 }
26856
26857
26858 /* Initialize the DWARF-2 unwind information for this procedure.  */
26859
26860 void
26861 tc_arm_frame_initial_instructions (void)
26862 {
26863   cfi_add_CFA_def_cfa (REG_SP, 0);
26864 }
26865 #endif /* OBJ_ELF */
26866
26867 /* Convert REGNAME to a DWARF-2 register number.  */
26868
26869 int
26870 tc_arm_regname_to_dw2regnum (char *regname)
26871 {
26872   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
26873   if (reg != FAIL)
26874     return reg;
26875
26876   /* PR 16694: Allow VFP registers as well.  */
26877   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
26878   if (reg != FAIL)
26879     return 64 + reg;
26880
26881   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
26882   if (reg != FAIL)
26883     return reg + 256;
26884
26885   return FAIL;
26886 }
26887
26888 #ifdef TE_PE
26889 void
26890 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
26891 {
26892   expressionS exp;
26893
26894   exp.X_op = O_secrel;
26895   exp.X_add_symbol = symbol;
26896   exp.X_add_number = 0;
26897   emit_expr (&exp, size);
26898 }
26899 #endif
26900
26901 /* MD interface: Symbol and relocation handling.  */
26902
26903 /* Return the address within the segment that a PC-relative fixup is
26904    relative to.  For ARM, PC-relative fixups applied to instructions
26905    are generally relative to the location of the fixup plus 8 bytes.
26906    Thumb branches are offset by 4, and Thumb loads relative to PC
26907    require special handling.  */
26908
26909 long
26910 md_pcrel_from_section (fixS * fixP, segT seg)
26911 {
26912   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
26913
26914   /* If this is pc-relative and we are going to emit a relocation
26915      then we just want to put out any pipeline compensation that the linker
26916      will need.  Otherwise we want to use the calculated base.
26917      For WinCE we skip the bias for externals as well, since this
26918      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
26919   if (fixP->fx_pcrel
26920       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
26921           || (arm_force_relocation (fixP)
26922 #ifdef TE_WINCE
26923               && !S_IS_EXTERNAL (fixP->fx_addsy)
26924 #endif
26925               )))
26926     base = 0;
26927
26928
26929   switch (fixP->fx_r_type)
26930     {
26931       /* PC relative addressing on the Thumb is slightly odd as the
26932          bottom two bits of the PC are forced to zero for the
26933          calculation.  This happens *after* application of the
26934          pipeline offset.  However, Thumb adrl already adjusts for
26935          this, so we need not do it again.  */
26936     case BFD_RELOC_ARM_THUMB_ADD:
26937       return base & ~3;
26938
26939     case BFD_RELOC_ARM_THUMB_OFFSET:
26940     case BFD_RELOC_ARM_T32_OFFSET_IMM:
26941     case BFD_RELOC_ARM_T32_ADD_PC12:
26942     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
26943       return (base + 4) & ~3;
26944
26945       /* Thumb branches are simply offset by +4.  */
26946     case BFD_RELOC_THUMB_PCREL_BRANCH5:
26947     case BFD_RELOC_THUMB_PCREL_BRANCH7:
26948     case BFD_RELOC_THUMB_PCREL_BRANCH9:
26949     case BFD_RELOC_THUMB_PCREL_BRANCH12:
26950     case BFD_RELOC_THUMB_PCREL_BRANCH20:
26951     case BFD_RELOC_THUMB_PCREL_BRANCH25:
26952     case BFD_RELOC_THUMB_PCREL_BFCSEL:
26953     case BFD_RELOC_ARM_THUMB_BF17:
26954     case BFD_RELOC_ARM_THUMB_BF19:
26955     case BFD_RELOC_ARM_THUMB_BF13:
26956     case BFD_RELOC_ARM_THUMB_LOOP12:
26957       return base + 4;
26958
26959     case BFD_RELOC_THUMB_PCREL_BRANCH23:
26960       if (fixP->fx_addsy
26961           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26962           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26963           && ARM_IS_FUNC (fixP->fx_addsy)
26964           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26965         base = fixP->fx_where + fixP->fx_frag->fr_address;
26966        return base + 4;
26967
26968       /* BLX is like branches above, but forces the low two bits of PC to
26969          zero.  */
26970     case BFD_RELOC_THUMB_PCREL_BLX:
26971       if (fixP->fx_addsy
26972           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26973           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26974           && THUMB_IS_FUNC (fixP->fx_addsy)
26975           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26976         base = fixP->fx_where + fixP->fx_frag->fr_address;
26977       return (base + 4) & ~3;
26978
26979       /* ARM mode branches are offset by +8.  However, the Windows CE
26980          loader expects the relocation not to take this into account.  */
26981     case BFD_RELOC_ARM_PCREL_BLX:
26982       if (fixP->fx_addsy
26983           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26984           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26985           && ARM_IS_FUNC (fixP->fx_addsy)
26986           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26987         base = fixP->fx_where + fixP->fx_frag->fr_address;
26988       return base + 8;
26989
26990     case BFD_RELOC_ARM_PCREL_CALL:
26991       if (fixP->fx_addsy
26992           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26993           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26994           && THUMB_IS_FUNC (fixP->fx_addsy)
26995           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26996         base = fixP->fx_where + fixP->fx_frag->fr_address;
26997       return base + 8;
26998
26999     case BFD_RELOC_ARM_PCREL_BRANCH:
27000     case BFD_RELOC_ARM_PCREL_JUMP:
27001     case BFD_RELOC_ARM_PLT32:
27002 #ifdef TE_WINCE
27003       /* When handling fixups immediately, because we have already
27004          discovered the value of a symbol, or the address of the frag involved
27005          we must account for the offset by +8, as the OS loader will never see the reloc.
27006          see fixup_segment() in write.c
27007          The S_IS_EXTERNAL test handles the case of global symbols.
27008          Those need the calculated base, not just the pipe compensation the linker will need.  */
27009       if (fixP->fx_pcrel
27010           && fixP->fx_addsy != NULL
27011           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27012           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
27013         return base + 8;
27014       return base;
27015 #else
27016       return base + 8;
27017 #endif
27018
27019
27020       /* ARM mode loads relative to PC are also offset by +8.  Unlike
27021          branches, the Windows CE loader *does* expect the relocation
27022          to take this into account.  */
27023     case BFD_RELOC_ARM_OFFSET_IMM:
27024     case BFD_RELOC_ARM_OFFSET_IMM8:
27025     case BFD_RELOC_ARM_HWLITERAL:
27026     case BFD_RELOC_ARM_LITERAL:
27027     case BFD_RELOC_ARM_CP_OFF_IMM:
27028       return base + 8;
27029
27030
27031       /* Other PC-relative relocations are un-offset.  */
27032     default:
27033       return base;
27034     }
27035 }
27036
27037 static bfd_boolean flag_warn_syms = TRUE;
27038
27039 bfd_boolean
27040 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
27041 {
27042   /* PR 18347 - Warn if the user attempts to create a symbol with the same
27043      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
27044      does mean that the resulting code might be very confusing to the reader.
27045      Also this warning can be triggered if the user omits an operand before
27046      an immediate address, eg:
27047
27048        LDR =foo
27049
27050      GAS treats this as an assignment of the value of the symbol foo to a
27051      symbol LDR, and so (without this code) it will not issue any kind of
27052      warning or error message.
27053
27054      Note - ARM instructions are case-insensitive but the strings in the hash
27055      table are all stored in lower case, so we must first ensure that name is
27056      lower case too.  */
27057   if (flag_warn_syms && arm_ops_hsh)
27058     {
27059       char * nbuf = strdup (name);
27060       char * p;
27061
27062       for (p = nbuf; *p; p++)
27063         *p = TOLOWER (*p);
27064       if (hash_find (arm_ops_hsh, nbuf) != NULL)
27065         {
27066           static struct hash_control * already_warned = NULL;
27067
27068           if (already_warned == NULL)
27069             already_warned = hash_new ();
27070           /* Only warn about the symbol once.  To keep the code
27071              simple we let hash_insert do the lookup for us.  */
27072           if (hash_insert (already_warned, nbuf, NULL) == NULL)
27073             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
27074         }
27075       else
27076         free (nbuf);
27077     }
27078
27079   return FALSE;
27080 }
27081
27082 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
27083    Otherwise we have no need to default values of symbols.  */
27084
27085 symbolS *
27086 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
27087 {
27088 #ifdef OBJ_ELF
27089   if (name[0] == '_' && name[1] == 'G'
27090       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
27091     {
27092       if (!GOT_symbol)
27093         {
27094           if (symbol_find (name))
27095             as_bad (_("GOT already in the symbol table"));
27096
27097           GOT_symbol = symbol_new (name, undefined_section,
27098                                    (valueT) 0, & zero_address_frag);
27099         }
27100
27101       return GOT_symbol;
27102     }
27103 #endif
27104
27105   return NULL;
27106 }
27107
27108 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
27109    computed as two separate immediate values, added together.  We
27110    already know that this value cannot be computed by just one ARM
27111    instruction.  */
27112
27113 static unsigned int
27114 validate_immediate_twopart (unsigned int   val,
27115                             unsigned int * highpart)
27116 {
27117   unsigned int a;
27118   unsigned int i;
27119
27120   for (i = 0; i < 32; i += 2)
27121     if (((a = rotate_left (val, i)) & 0xff) != 0)
27122       {
27123         if (a & 0xff00)
27124           {
27125             if (a & ~ 0xffff)
27126               continue;
27127             * highpart = (a  >> 8) | ((i + 24) << 7);
27128           }
27129         else if (a & 0xff0000)
27130           {
27131             if (a & 0xff000000)
27132               continue;
27133             * highpart = (a >> 16) | ((i + 16) << 7);
27134           }
27135         else
27136           {
27137             gas_assert (a & 0xff000000);
27138             * highpart = (a >> 24) | ((i + 8) << 7);
27139           }
27140
27141         return (a & 0xff) | (i << 7);
27142       }
27143
27144   return FAIL;
27145 }
27146
27147 static int
27148 validate_offset_imm (unsigned int val, int hwse)
27149 {
27150   if ((hwse && val > 255) || val > 4095)
27151     return FAIL;
27152   return val;
27153 }
27154
27155 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
27156    negative immediate constant by altering the instruction.  A bit of
27157    a hack really.
27158         MOV <-> MVN
27159         AND <-> BIC
27160         ADC <-> SBC
27161         by inverting the second operand, and
27162         ADD <-> SUB
27163         CMP <-> CMN
27164         by negating the second operand.  */
27165
27166 static int
27167 negate_data_op (unsigned long * instruction,
27168                 unsigned long   value)
27169 {
27170   int op, new_inst;
27171   unsigned long negated, inverted;
27172
27173   negated = encode_arm_immediate (-value);
27174   inverted = encode_arm_immediate (~value);
27175
27176   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
27177   switch (op)
27178     {
27179       /* First negates.  */
27180     case OPCODE_SUB:             /* ADD <-> SUB  */
27181       new_inst = OPCODE_ADD;
27182       value = negated;
27183       break;
27184
27185     case OPCODE_ADD:
27186       new_inst = OPCODE_SUB;
27187       value = negated;
27188       break;
27189
27190     case OPCODE_CMP:             /* CMP <-> CMN  */
27191       new_inst = OPCODE_CMN;
27192       value = negated;
27193       break;
27194
27195     case OPCODE_CMN:
27196       new_inst = OPCODE_CMP;
27197       value = negated;
27198       break;
27199
27200       /* Now Inverted ops.  */
27201     case OPCODE_MOV:             /* MOV <-> MVN  */
27202       new_inst = OPCODE_MVN;
27203       value = inverted;
27204       break;
27205
27206     case OPCODE_MVN:
27207       new_inst = OPCODE_MOV;
27208       value = inverted;
27209       break;
27210
27211     case OPCODE_AND:             /* AND <-> BIC  */
27212       new_inst = OPCODE_BIC;
27213       value = inverted;
27214       break;
27215
27216     case OPCODE_BIC:
27217       new_inst = OPCODE_AND;
27218       value = inverted;
27219       break;
27220
27221     case OPCODE_ADC:              /* ADC <-> SBC  */
27222       new_inst = OPCODE_SBC;
27223       value = inverted;
27224       break;
27225
27226     case OPCODE_SBC:
27227       new_inst = OPCODE_ADC;
27228       value = inverted;
27229       break;
27230
27231       /* We cannot do anything.  */
27232     default:
27233       return FAIL;
27234     }
27235
27236   if (value == (unsigned) FAIL)
27237     return FAIL;
27238
27239   *instruction &= OPCODE_MASK;
27240   *instruction |= new_inst << DATA_OP_SHIFT;
27241   return value;
27242 }
27243
27244 /* Like negate_data_op, but for Thumb-2.   */
27245
27246 static unsigned int
27247 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
27248 {
27249   int op, new_inst;
27250   int rd;
27251   unsigned int negated, inverted;
27252
27253   negated = encode_thumb32_immediate (-value);
27254   inverted = encode_thumb32_immediate (~value);
27255
27256   rd = (*instruction >> 8) & 0xf;
27257   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
27258   switch (op)
27259     {
27260       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
27261     case T2_OPCODE_SUB:
27262       new_inst = T2_OPCODE_ADD;
27263       value = negated;
27264       break;
27265
27266     case T2_OPCODE_ADD:
27267       new_inst = T2_OPCODE_SUB;
27268       value = negated;
27269       break;
27270
27271       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
27272     case T2_OPCODE_ORR:
27273       new_inst = T2_OPCODE_ORN;
27274       value = inverted;
27275       break;
27276
27277     case T2_OPCODE_ORN:
27278       new_inst = T2_OPCODE_ORR;
27279       value = inverted;
27280       break;
27281
27282       /* AND <-> BIC.  TST has no inverted equivalent.  */
27283     case T2_OPCODE_AND:
27284       new_inst = T2_OPCODE_BIC;
27285       if (rd == 15)
27286         value = FAIL;
27287       else
27288         value = inverted;
27289       break;
27290
27291     case T2_OPCODE_BIC:
27292       new_inst = T2_OPCODE_AND;
27293       value = inverted;
27294       break;
27295
27296       /* ADC <-> SBC  */
27297     case T2_OPCODE_ADC:
27298       new_inst = T2_OPCODE_SBC;
27299       value = inverted;
27300       break;
27301
27302     case T2_OPCODE_SBC:
27303       new_inst = T2_OPCODE_ADC;
27304       value = inverted;
27305       break;
27306
27307       /* We cannot do anything.  */
27308     default:
27309       return FAIL;
27310     }
27311
27312   if (value == (unsigned int)FAIL)
27313     return FAIL;
27314
27315   *instruction &= T2_OPCODE_MASK;
27316   *instruction |= new_inst << T2_DATA_OP_SHIFT;
27317   return value;
27318 }
27319
27320 /* Read a 32-bit thumb instruction from buf.  */
27321
27322 static unsigned long
27323 get_thumb32_insn (char * buf)
27324 {
27325   unsigned long insn;
27326   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
27327   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27328
27329   return insn;
27330 }
27331
27332 /* We usually want to set the low bit on the address of thumb function
27333    symbols.  In particular .word foo - . should have the low bit set.
27334    Generic code tries to fold the difference of two symbols to
27335    a constant.  Prevent this and force a relocation when the first symbols
27336    is a thumb function.  */
27337
27338 bfd_boolean
27339 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
27340 {
27341   if (op == O_subtract
27342       && l->X_op == O_symbol
27343       && r->X_op == O_symbol
27344       && THUMB_IS_FUNC (l->X_add_symbol))
27345     {
27346       l->X_op = O_subtract;
27347       l->X_op_symbol = r->X_add_symbol;
27348       l->X_add_number -= r->X_add_number;
27349       return TRUE;
27350     }
27351
27352   /* Process as normal.  */
27353   return FALSE;
27354 }
27355
27356 /* Encode Thumb2 unconditional branches and calls. The encoding
27357    for the 2 are identical for the immediate values.  */
27358
27359 static void
27360 encode_thumb2_b_bl_offset (char * buf, offsetT value)
27361 {
27362 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
27363   offsetT newval;
27364   offsetT newval2;
27365   addressT S, I1, I2, lo, hi;
27366
27367   S = (value >> 24) & 0x01;
27368   I1 = (value >> 23) & 0x01;
27369   I2 = (value >> 22) & 0x01;
27370   hi = (value >> 12) & 0x3ff;
27371   lo = (value >> 1) & 0x7ff;
27372   newval   = md_chars_to_number (buf, THUMB_SIZE);
27373   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27374   newval  |= (S << 10) | hi;
27375   newval2 &=  ~T2I1I2MASK;
27376   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
27377   md_number_to_chars (buf, newval, THUMB_SIZE);
27378   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
27379 }
27380
27381 void
27382 md_apply_fix (fixS *    fixP,
27383                valueT * valP,
27384                segT     seg)
27385 {
27386   offsetT        value = * valP;
27387   offsetT        newval;
27388   unsigned int   newimm;
27389   unsigned long  temp;
27390   int            sign;
27391   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
27392
27393   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
27394
27395   /* Note whether this will delete the relocation.  */
27396
27397   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
27398     fixP->fx_done = 1;
27399
27400   /* On a 64-bit host, silently truncate 'value' to 32 bits for
27401      consistency with the behaviour on 32-bit hosts.  Remember value
27402      for emit_reloc.  */
27403   value &= 0xffffffff;
27404   value ^= 0x80000000;
27405   value -= 0x80000000;
27406
27407   *valP = value;
27408   fixP->fx_addnumber = value;
27409
27410   /* Same treatment for fixP->fx_offset.  */
27411   fixP->fx_offset &= 0xffffffff;
27412   fixP->fx_offset ^= 0x80000000;
27413   fixP->fx_offset -= 0x80000000;
27414
27415   switch (fixP->fx_r_type)
27416     {
27417     case BFD_RELOC_NONE:
27418       /* This will need to go in the object file.  */
27419       fixP->fx_done = 0;
27420       break;
27421
27422     case BFD_RELOC_ARM_IMMEDIATE:
27423       /* We claim that this fixup has been processed here,
27424          even if in fact we generate an error because we do
27425          not have a reloc for it, so tc_gen_reloc will reject it.  */
27426       fixP->fx_done = 1;
27427
27428       if (fixP->fx_addsy)
27429         {
27430           const char *msg = 0;
27431
27432           if (! S_IS_DEFINED (fixP->fx_addsy))
27433             msg = _("undefined symbol %s used as an immediate value");
27434           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
27435             msg = _("symbol %s is in a different section");
27436           else if (S_IS_WEAK (fixP->fx_addsy))
27437             msg = _("symbol %s is weak and may be overridden later");
27438
27439           if (msg)
27440             {
27441               as_bad_where (fixP->fx_file, fixP->fx_line,
27442                             msg, S_GET_NAME (fixP->fx_addsy));
27443               break;
27444             }
27445         }
27446
27447       temp = md_chars_to_number (buf, INSN_SIZE);
27448
27449       /* If the offset is negative, we should use encoding A2 for ADR.  */
27450       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
27451         newimm = negate_data_op (&temp, value);
27452       else
27453         {
27454           newimm = encode_arm_immediate (value);
27455
27456           /* If the instruction will fail, see if we can fix things up by
27457              changing the opcode.  */
27458           if (newimm == (unsigned int) FAIL)
27459             newimm = negate_data_op (&temp, value);
27460           /* MOV accepts both ARM modified immediate (A1 encoding) and
27461              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
27462              When disassembling, MOV is preferred when there is no encoding
27463              overlap.  */
27464           if (newimm == (unsigned int) FAIL
27465               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
27466               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
27467               && !((temp >> SBIT_SHIFT) & 0x1)
27468               && value >= 0 && value <= 0xffff)
27469             {
27470               /* Clear bits[23:20] to change encoding from A1 to A2.  */
27471               temp &= 0xff0fffff;
27472               /* Encoding high 4bits imm.  Code below will encode the remaining
27473                  low 12bits.  */
27474               temp |= (value & 0x0000f000) << 4;
27475               newimm = value & 0x00000fff;
27476             }
27477         }
27478
27479       if (newimm == (unsigned int) FAIL)
27480         {
27481           as_bad_where (fixP->fx_file, fixP->fx_line,
27482                         _("invalid constant (%lx) after fixup"),
27483                         (unsigned long) value);
27484           break;
27485         }
27486
27487       newimm |= (temp & 0xfffff000);
27488       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
27489       break;
27490
27491     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
27492       {
27493         unsigned int highpart = 0;
27494         unsigned int newinsn  = 0xe1a00000; /* nop.  */
27495
27496         if (fixP->fx_addsy)
27497           {
27498             const char *msg = 0;
27499
27500             if (! S_IS_DEFINED (fixP->fx_addsy))
27501               msg = _("undefined symbol %s used as an immediate value");
27502             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
27503               msg = _("symbol %s is in a different section");
27504             else if (S_IS_WEAK (fixP->fx_addsy))
27505               msg = _("symbol %s is weak and may be overridden later");
27506
27507             if (msg)
27508               {
27509                 as_bad_where (fixP->fx_file, fixP->fx_line,
27510                               msg, S_GET_NAME (fixP->fx_addsy));
27511                 break;
27512               }
27513           }
27514
27515         newimm = encode_arm_immediate (value);
27516         temp = md_chars_to_number (buf, INSN_SIZE);
27517
27518         /* If the instruction will fail, see if we can fix things up by
27519            changing the opcode.  */
27520         if (newimm == (unsigned int) FAIL
27521             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
27522           {
27523             /* No ?  OK - try using two ADD instructions to generate
27524                the value.  */
27525             newimm = validate_immediate_twopart (value, & highpart);
27526
27527             /* Yes - then make sure that the second instruction is
27528                also an add.  */
27529             if (newimm != (unsigned int) FAIL)
27530               newinsn = temp;
27531             /* Still No ?  Try using a negated value.  */
27532             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
27533               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
27534             /* Otherwise - give up.  */
27535             else
27536               {
27537                 as_bad_where (fixP->fx_file, fixP->fx_line,
27538                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
27539                               (long) value);
27540                 break;
27541               }
27542
27543             /* Replace the first operand in the 2nd instruction (which
27544                is the PC) with the destination register.  We have
27545                already added in the PC in the first instruction and we
27546                do not want to do it again.  */
27547             newinsn &= ~ 0xf0000;
27548             newinsn |= ((newinsn & 0x0f000) << 4);
27549           }
27550
27551         newimm |= (temp & 0xfffff000);
27552         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
27553
27554         highpart |= (newinsn & 0xfffff000);
27555         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
27556       }
27557       break;
27558
27559     case BFD_RELOC_ARM_OFFSET_IMM:
27560       if (!fixP->fx_done && seg->use_rela_p)
27561         value = 0;
27562       /* Fall through.  */
27563
27564     case BFD_RELOC_ARM_LITERAL:
27565       sign = value > 0;
27566
27567       if (value < 0)
27568         value = - value;
27569
27570       if (validate_offset_imm (value, 0) == FAIL)
27571         {
27572           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
27573             as_bad_where (fixP->fx_file, fixP->fx_line,
27574                           _("invalid literal constant: pool needs to be closer"));
27575           else
27576             as_bad_where (fixP->fx_file, fixP->fx_line,
27577                           _("bad immediate value for offset (%ld)"),
27578                           (long) value);
27579           break;
27580         }
27581
27582       newval = md_chars_to_number (buf, INSN_SIZE);
27583       if (value == 0)
27584         newval &= 0xfffff000;
27585       else
27586         {
27587           newval &= 0xff7ff000;
27588           newval |= value | (sign ? INDEX_UP : 0);
27589         }
27590       md_number_to_chars (buf, newval, INSN_SIZE);
27591       break;
27592
27593     case BFD_RELOC_ARM_OFFSET_IMM8:
27594     case BFD_RELOC_ARM_HWLITERAL:
27595       sign = value > 0;
27596
27597       if (value < 0)
27598         value = - value;
27599
27600       if (validate_offset_imm (value, 1) == FAIL)
27601         {
27602           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
27603             as_bad_where (fixP->fx_file, fixP->fx_line,
27604                           _("invalid literal constant: pool needs to be closer"));
27605           else
27606             as_bad_where (fixP->fx_file, fixP->fx_line,
27607                           _("bad immediate value for 8-bit offset (%ld)"),
27608                           (long) value);
27609           break;
27610         }
27611
27612       newval = md_chars_to_number (buf, INSN_SIZE);
27613       if (value == 0)
27614         newval &= 0xfffff0f0;
27615       else
27616         {
27617           newval &= 0xff7ff0f0;
27618           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
27619         }
27620       md_number_to_chars (buf, newval, INSN_SIZE);
27621       break;
27622
27623     case BFD_RELOC_ARM_T32_OFFSET_U8:
27624       if (value < 0 || value > 1020 || value % 4 != 0)
27625         as_bad_where (fixP->fx_file, fixP->fx_line,
27626                       _("bad immediate value for offset (%ld)"), (long) value);
27627       value /= 4;
27628
27629       newval = md_chars_to_number (buf+2, THUMB_SIZE);
27630       newval |= value;
27631       md_number_to_chars (buf+2, newval, THUMB_SIZE);
27632       break;
27633
27634     case BFD_RELOC_ARM_T32_OFFSET_IMM:
27635       /* This is a complicated relocation used for all varieties of Thumb32
27636          load/store instruction with immediate offset:
27637
27638          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
27639                                                    *4, optional writeback(W)
27640                                                    (doubleword load/store)
27641
27642          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
27643          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
27644          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
27645          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
27646          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
27647
27648          Uppercase letters indicate bits that are already encoded at
27649          this point.  Lowercase letters are our problem.  For the
27650          second block of instructions, the secondary opcode nybble
27651          (bits 8..11) is present, and bit 23 is zero, even if this is
27652          a PC-relative operation.  */
27653       newval = md_chars_to_number (buf, THUMB_SIZE);
27654       newval <<= 16;
27655       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
27656
27657       if ((newval & 0xf0000000) == 0xe0000000)
27658         {
27659           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
27660           if (value >= 0)
27661             newval |= (1 << 23);
27662           else
27663             value = -value;
27664           if (value % 4 != 0)
27665             {
27666               as_bad_where (fixP->fx_file, fixP->fx_line,
27667                             _("offset not a multiple of 4"));
27668               break;
27669             }
27670           value /= 4;
27671           if (value > 0xff)
27672             {
27673               as_bad_where (fixP->fx_file, fixP->fx_line,
27674                             _("offset out of range"));
27675               break;
27676             }
27677           newval &= ~0xff;
27678         }
27679       else if ((newval & 0x000f0000) == 0x000f0000)
27680         {
27681           /* PC-relative, 12-bit offset.  */
27682           if (value >= 0)
27683             newval |= (1 << 23);
27684           else
27685             value = -value;
27686           if (value > 0xfff)
27687             {
27688               as_bad_where (fixP->fx_file, fixP->fx_line,
27689                             _("offset out of range"));
27690               break;
27691             }
27692           newval &= ~0xfff;
27693         }
27694       else if ((newval & 0x00000100) == 0x00000100)
27695         {
27696           /* Writeback: 8-bit, +/- offset.  */
27697           if (value >= 0)
27698             newval |= (1 << 9);
27699           else
27700             value = -value;
27701           if (value > 0xff)
27702             {
27703               as_bad_where (fixP->fx_file, fixP->fx_line,
27704                             _("offset out of range"));
27705               break;
27706             }
27707           newval &= ~0xff;
27708         }
27709       else if ((newval & 0x00000f00) == 0x00000e00)
27710         {
27711           /* T-instruction: positive 8-bit offset.  */
27712           if (value < 0 || value > 0xff)
27713             {
27714               as_bad_where (fixP->fx_file, fixP->fx_line,
27715                             _("offset out of range"));
27716               break;
27717             }
27718           newval &= ~0xff;
27719           newval |= value;
27720         }
27721       else
27722         {
27723           /* Positive 12-bit or negative 8-bit offset.  */
27724           int limit;
27725           if (value >= 0)
27726             {
27727               newval |= (1 << 23);
27728               limit = 0xfff;
27729             }
27730           else
27731             {
27732               value = -value;
27733               limit = 0xff;
27734             }
27735           if (value > limit)
27736             {
27737               as_bad_where (fixP->fx_file, fixP->fx_line,
27738                             _("offset out of range"));
27739               break;
27740             }
27741           newval &= ~limit;
27742         }
27743
27744       newval |= value;
27745       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
27746       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
27747       break;
27748
27749     case BFD_RELOC_ARM_SHIFT_IMM:
27750       newval = md_chars_to_number (buf, INSN_SIZE);
27751       if (((unsigned long) value) > 32
27752           || (value == 32
27753               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
27754         {
27755           as_bad_where (fixP->fx_file, fixP->fx_line,
27756                         _("shift expression is too large"));
27757           break;
27758         }
27759
27760       if (value == 0)
27761         /* Shifts of zero must be done as lsl.  */
27762         newval &= ~0x60;
27763       else if (value == 32)
27764         value = 0;
27765       newval &= 0xfffff07f;
27766       newval |= (value & 0x1f) << 7;
27767       md_number_to_chars (buf, newval, INSN_SIZE);
27768       break;
27769
27770     case BFD_RELOC_ARM_T32_IMMEDIATE:
27771     case BFD_RELOC_ARM_T32_ADD_IMM:
27772     case BFD_RELOC_ARM_T32_IMM12:
27773     case BFD_RELOC_ARM_T32_ADD_PC12:
27774       /* We claim that this fixup has been processed here,
27775          even if in fact we generate an error because we do
27776          not have a reloc for it, so tc_gen_reloc will reject it.  */
27777       fixP->fx_done = 1;
27778
27779       if (fixP->fx_addsy
27780           && ! S_IS_DEFINED (fixP->fx_addsy))
27781         {
27782           as_bad_where (fixP->fx_file, fixP->fx_line,
27783                         _("undefined symbol %s used as an immediate value"),
27784                         S_GET_NAME (fixP->fx_addsy));
27785           break;
27786         }
27787
27788       newval = md_chars_to_number (buf, THUMB_SIZE);
27789       newval <<= 16;
27790       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
27791
27792       newimm = FAIL;
27793       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
27794            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
27795               Thumb2 modified immediate encoding (T2).  */
27796            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
27797           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
27798         {
27799           newimm = encode_thumb32_immediate (value);
27800           if (newimm == (unsigned int) FAIL)
27801             newimm = thumb32_negate_data_op (&newval, value);
27802         }
27803       if (newimm == (unsigned int) FAIL)
27804         {
27805           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
27806             {
27807               /* Turn add/sum into addw/subw.  */
27808               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
27809                 newval = (newval & 0xfeffffff) | 0x02000000;
27810               /* No flat 12-bit imm encoding for addsw/subsw.  */
27811               if ((newval & 0x00100000) == 0)
27812                 {
27813                   /* 12 bit immediate for addw/subw.  */
27814                   if (value < 0)
27815                     {
27816                       value = -value;
27817                       newval ^= 0x00a00000;
27818                     }
27819                   if (value > 0xfff)
27820                     newimm = (unsigned int) FAIL;
27821                   else
27822                     newimm = value;
27823                 }
27824             }
27825           else
27826             {
27827               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
27828                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
27829                  disassembling, MOV is preferred when there is no encoding
27830                  overlap.  */
27831               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
27832                   /* NOTE: MOV uses the ORR opcode in Thumb 2 mode
27833                      but with the Rn field [19:16] set to 1111.  */
27834                   && (((newval >> 16) & 0xf) == 0xf)
27835                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
27836                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
27837                   && value >= 0 && value <= 0xffff)
27838                 {
27839                   /* Toggle bit[25] to change encoding from T2 to T3.  */
27840                   newval ^= 1 << 25;
27841                   /* Clear bits[19:16].  */
27842                   newval &= 0xfff0ffff;
27843                   /* Encoding high 4bits imm.  Code below will encode the
27844                      remaining low 12bits.  */
27845                   newval |= (value & 0x0000f000) << 4;
27846                   newimm = value & 0x00000fff;
27847                 }
27848             }
27849         }
27850
27851       if (newimm == (unsigned int)FAIL)
27852         {
27853           as_bad_where (fixP->fx_file, fixP->fx_line,
27854                         _("invalid constant (%lx) after fixup"),
27855                         (unsigned long) value);
27856           break;
27857         }
27858
27859       newval |= (newimm & 0x800) << 15;
27860       newval |= (newimm & 0x700) << 4;
27861       newval |= (newimm & 0x0ff);
27862
27863       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
27864       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
27865       break;
27866
27867     case BFD_RELOC_ARM_SMC:
27868       if (((unsigned long) value) > 0xf)
27869         as_bad_where (fixP->fx_file, fixP->fx_line,
27870                       _("invalid smc expression"));
27871
27872       newval = md_chars_to_number (buf, INSN_SIZE);
27873       newval |= (value & 0xf);
27874       md_number_to_chars (buf, newval, INSN_SIZE);
27875       break;
27876
27877     case BFD_RELOC_ARM_HVC:
27878       if (((unsigned long) value) > 0xffff)
27879         as_bad_where (fixP->fx_file, fixP->fx_line,
27880                       _("invalid hvc expression"));
27881       newval = md_chars_to_number (buf, INSN_SIZE);
27882       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
27883       md_number_to_chars (buf, newval, INSN_SIZE);
27884       break;
27885
27886     case BFD_RELOC_ARM_SWI:
27887       if (fixP->tc_fix_data != 0)
27888         {
27889           if (((unsigned long) value) > 0xff)
27890             as_bad_where (fixP->fx_file, fixP->fx_line,
27891                           _("invalid swi expression"));
27892           newval = md_chars_to_number (buf, THUMB_SIZE);
27893           newval |= value;
27894           md_number_to_chars (buf, newval, THUMB_SIZE);
27895         }
27896       else
27897         {
27898           if (((unsigned long) value) > 0x00ffffff)
27899             as_bad_where (fixP->fx_file, fixP->fx_line,
27900                           _("invalid swi expression"));
27901           newval = md_chars_to_number (buf, INSN_SIZE);
27902           newval |= value;
27903           md_number_to_chars (buf, newval, INSN_SIZE);
27904         }
27905       break;
27906
27907     case BFD_RELOC_ARM_MULTI:
27908       if (((unsigned long) value) > 0xffff)
27909         as_bad_where (fixP->fx_file, fixP->fx_line,
27910                       _("invalid expression in load/store multiple"));
27911       newval = value | md_chars_to_number (buf, INSN_SIZE);
27912       md_number_to_chars (buf, newval, INSN_SIZE);
27913       break;
27914
27915 #ifdef OBJ_ELF
27916     case BFD_RELOC_ARM_PCREL_CALL:
27917
27918       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27919           && fixP->fx_addsy
27920           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27921           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27922           && THUMB_IS_FUNC (fixP->fx_addsy))
27923         /* Flip the bl to blx. This is a simple flip
27924            bit here because we generate PCREL_CALL for
27925            unconditional bls.  */
27926         {
27927           newval = md_chars_to_number (buf, INSN_SIZE);
27928           newval = newval | 0x10000000;
27929           md_number_to_chars (buf, newval, INSN_SIZE);
27930           temp = 1;
27931           fixP->fx_done = 1;
27932         }
27933       else
27934         temp = 3;
27935       goto arm_branch_common;
27936
27937     case BFD_RELOC_ARM_PCREL_JUMP:
27938       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27939           && fixP->fx_addsy
27940           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27941           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27942           && THUMB_IS_FUNC (fixP->fx_addsy))
27943         {
27944           /* This would map to a bl<cond>, b<cond>,
27945              b<always> to a Thumb function. We
27946              need to force a relocation for this particular
27947              case.  */
27948           newval = md_chars_to_number (buf, INSN_SIZE);
27949           fixP->fx_done = 0;
27950         }
27951       /* Fall through.  */
27952
27953     case BFD_RELOC_ARM_PLT32:
27954 #endif
27955     case BFD_RELOC_ARM_PCREL_BRANCH:
27956       temp = 3;
27957       goto arm_branch_common;
27958
27959     case BFD_RELOC_ARM_PCREL_BLX:
27960
27961       temp = 1;
27962       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27963           && fixP->fx_addsy
27964           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27965           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27966           && ARM_IS_FUNC (fixP->fx_addsy))
27967         {
27968           /* Flip the blx to a bl and warn.  */
27969           const char *name = S_GET_NAME (fixP->fx_addsy);
27970           newval = 0xeb000000;
27971           as_warn_where (fixP->fx_file, fixP->fx_line,
27972                          _("blx to '%s' an ARM ISA state function changed to bl"),
27973                           name);
27974           md_number_to_chars (buf, newval, INSN_SIZE);
27975           temp = 3;
27976           fixP->fx_done = 1;
27977         }
27978
27979 #ifdef OBJ_ELF
27980        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
27981          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
27982 #endif
27983
27984     arm_branch_common:
27985       /* We are going to store value (shifted right by two) in the
27986          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
27987          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
27988          also be clear.  */
27989       if (value & temp)
27990         as_bad_where (fixP->fx_file, fixP->fx_line,
27991                       _("misaligned branch destination"));
27992       if ((value & (offsetT)0xfe000000) != (offsetT)0
27993           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
27994         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
27995
27996       if (fixP->fx_done || !seg->use_rela_p)
27997         {
27998           newval = md_chars_to_number (buf, INSN_SIZE);
27999           newval |= (value >> 2) & 0x00ffffff;
28000           /* Set the H bit on BLX instructions.  */
28001           if (temp == 1)
28002             {
28003               if (value & 2)
28004                 newval |= 0x01000000;
28005               else
28006                 newval &= ~0x01000000;
28007             }
28008           md_number_to_chars (buf, newval, INSN_SIZE);
28009         }
28010       break;
28011
28012     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
28013       /* CBZ can only branch forward.  */
28014
28015       /* Attempts to use CBZ to branch to the next instruction
28016          (which, strictly speaking, are prohibited) will be turned into
28017          no-ops.
28018
28019          FIXME: It may be better to remove the instruction completely and
28020          perform relaxation.  */
28021       if (value == -2)
28022         {
28023           newval = md_chars_to_number (buf, THUMB_SIZE);
28024           newval = 0xbf00; /* NOP encoding T1 */
28025           md_number_to_chars (buf, newval, THUMB_SIZE);
28026         }
28027       else
28028         {
28029           if (value & ~0x7e)
28030             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28031
28032           if (fixP->fx_done || !seg->use_rela_p)
28033             {
28034               newval = md_chars_to_number (buf, THUMB_SIZE);
28035               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
28036               md_number_to_chars (buf, newval, THUMB_SIZE);
28037             }
28038         }
28039       break;
28040
28041     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
28042       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
28043         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28044
28045       if (fixP->fx_done || !seg->use_rela_p)
28046         {
28047           newval = md_chars_to_number (buf, THUMB_SIZE);
28048           newval |= (value & 0x1ff) >> 1;
28049           md_number_to_chars (buf, newval, THUMB_SIZE);
28050         }
28051       break;
28052
28053     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
28054       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
28055         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28056
28057       if (fixP->fx_done || !seg->use_rela_p)
28058         {
28059           newval = md_chars_to_number (buf, THUMB_SIZE);
28060           newval |= (value & 0xfff) >> 1;
28061           md_number_to_chars (buf, newval, THUMB_SIZE);
28062         }
28063       break;
28064
28065     case BFD_RELOC_THUMB_PCREL_BRANCH20:
28066       if (fixP->fx_addsy
28067           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28068           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28069           && ARM_IS_FUNC (fixP->fx_addsy)
28070           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
28071         {
28072           /* Force a relocation for a branch 20 bits wide.  */
28073           fixP->fx_done = 0;
28074         }
28075       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
28076         as_bad_where (fixP->fx_file, fixP->fx_line,
28077                       _("conditional branch out of range"));
28078
28079       if (fixP->fx_done || !seg->use_rela_p)
28080         {
28081           offsetT newval2;
28082           addressT S, J1, J2, lo, hi;
28083
28084           S  = (value & 0x00100000) >> 20;
28085           J2 = (value & 0x00080000) >> 19;
28086           J1 = (value & 0x00040000) >> 18;
28087           hi = (value & 0x0003f000) >> 12;
28088           lo = (value & 0x00000ffe) >> 1;
28089
28090           newval   = md_chars_to_number (buf, THUMB_SIZE);
28091           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28092           newval  |= (S << 10) | hi;
28093           newval2 |= (J1 << 13) | (J2 << 11) | lo;
28094           md_number_to_chars (buf, newval, THUMB_SIZE);
28095           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28096         }
28097       break;
28098
28099     case BFD_RELOC_THUMB_PCREL_BLX:
28100       /* If there is a blx from a thumb state function to
28101          another thumb function flip this to a bl and warn
28102          about it.  */
28103
28104       if (fixP->fx_addsy
28105           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28106           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28107           && THUMB_IS_FUNC (fixP->fx_addsy))
28108         {
28109           const char *name = S_GET_NAME (fixP->fx_addsy);
28110           as_warn_where (fixP->fx_file, fixP->fx_line,
28111                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
28112                          name);
28113           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28114           newval = newval | 0x1000;
28115           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
28116           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
28117           fixP->fx_done = 1;
28118         }
28119
28120
28121       goto thumb_bl_common;
28122
28123     case BFD_RELOC_THUMB_PCREL_BRANCH23:
28124       /* A bl from Thumb state ISA to an internal ARM state function
28125          is converted to a blx.  */
28126       if (fixP->fx_addsy
28127           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28128           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28129           && ARM_IS_FUNC (fixP->fx_addsy)
28130           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
28131         {
28132           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28133           newval = newval & ~0x1000;
28134           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
28135           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
28136           fixP->fx_done = 1;
28137         }
28138
28139     thumb_bl_common:
28140
28141       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
28142         /* For a BLX instruction, make sure that the relocation is rounded up
28143            to a word boundary.  This follows the semantics of the instruction
28144            which specifies that bit 1 of the target address will come from bit
28145            1 of the base address.  */
28146         value = (value + 3) & ~ 3;
28147
28148 #ifdef OBJ_ELF
28149        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
28150            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
28151          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
28152 #endif
28153
28154       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
28155         {
28156           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
28157             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28158           else if ((value & ~0x1ffffff)
28159                    && ((value & ~0x1ffffff) != ~0x1ffffff))
28160             as_bad_where (fixP->fx_file, fixP->fx_line,
28161                           _("Thumb2 branch out of range"));
28162         }
28163
28164       if (fixP->fx_done || !seg->use_rela_p)
28165         encode_thumb2_b_bl_offset (buf, value);
28166
28167       break;
28168
28169     case BFD_RELOC_THUMB_PCREL_BRANCH25:
28170       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
28171         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
28172
28173       if (fixP->fx_done || !seg->use_rela_p)
28174           encode_thumb2_b_bl_offset (buf, value);
28175
28176       break;
28177
28178     case BFD_RELOC_8:
28179       if (fixP->fx_done || !seg->use_rela_p)
28180         *buf = value;
28181       break;
28182
28183     case BFD_RELOC_16:
28184       if (fixP->fx_done || !seg->use_rela_p)
28185         md_number_to_chars (buf, value, 2);
28186       break;
28187
28188 #ifdef OBJ_ELF
28189     case BFD_RELOC_ARM_TLS_CALL:
28190     case BFD_RELOC_ARM_THM_TLS_CALL:
28191     case BFD_RELOC_ARM_TLS_DESCSEQ:
28192     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
28193     case BFD_RELOC_ARM_TLS_GOTDESC:
28194     case BFD_RELOC_ARM_TLS_GD32:
28195     case BFD_RELOC_ARM_TLS_LE32:
28196     case BFD_RELOC_ARM_TLS_IE32:
28197     case BFD_RELOC_ARM_TLS_LDM32:
28198     case BFD_RELOC_ARM_TLS_LDO32:
28199       S_SET_THREAD_LOCAL (fixP->fx_addsy);
28200       break;
28201
28202       /* Same handling as above, but with the arm_fdpic guard.  */
28203     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
28204     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
28205     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
28206       if (arm_fdpic)
28207         {
28208           S_SET_THREAD_LOCAL (fixP->fx_addsy);
28209         }
28210       else
28211         {
28212           as_bad_where (fixP->fx_file, fixP->fx_line,
28213                         _("Relocation supported only in FDPIC mode"));
28214         }
28215       break;
28216
28217     case BFD_RELOC_ARM_GOT32:
28218     case BFD_RELOC_ARM_GOTOFF:
28219       break;
28220
28221     case BFD_RELOC_ARM_GOT_PREL:
28222       if (fixP->fx_done || !seg->use_rela_p)
28223         md_number_to_chars (buf, value, 4);
28224       break;
28225
28226     case BFD_RELOC_ARM_TARGET2:
28227       /* TARGET2 is not partial-inplace, so we need to write the
28228          addend here for REL targets, because it won't be written out
28229          during reloc processing later.  */
28230       if (fixP->fx_done || !seg->use_rela_p)
28231         md_number_to_chars (buf, fixP->fx_offset, 4);
28232       break;
28233
28234       /* Relocations for FDPIC.  */
28235     case BFD_RELOC_ARM_GOTFUNCDESC:
28236     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
28237     case BFD_RELOC_ARM_FUNCDESC:
28238       if (arm_fdpic)
28239         {
28240           if (fixP->fx_done || !seg->use_rela_p)
28241             md_number_to_chars (buf, 0, 4);
28242         }
28243       else
28244         {
28245           as_bad_where (fixP->fx_file, fixP->fx_line,
28246                         _("Relocation supported only in FDPIC mode"));
28247       }
28248       break;
28249 #endif
28250
28251     case BFD_RELOC_RVA:
28252     case BFD_RELOC_32:
28253     case BFD_RELOC_ARM_TARGET1:
28254     case BFD_RELOC_ARM_ROSEGREL32:
28255     case BFD_RELOC_ARM_SBREL32:
28256     case BFD_RELOC_32_PCREL:
28257 #ifdef TE_PE
28258     case BFD_RELOC_32_SECREL:
28259 #endif
28260       if (fixP->fx_done || !seg->use_rela_p)
28261 #ifdef TE_WINCE
28262         /* For WinCE we only do this for pcrel fixups.  */
28263         if (fixP->fx_done || fixP->fx_pcrel)
28264 #endif
28265           md_number_to_chars (buf, value, 4);
28266       break;
28267
28268 #ifdef OBJ_ELF
28269     case BFD_RELOC_ARM_PREL31:
28270       if (fixP->fx_done || !seg->use_rela_p)
28271         {
28272           newval = md_chars_to_number (buf, 4) & 0x80000000;
28273           if ((value ^ (value >> 1)) & 0x40000000)
28274             {
28275               as_bad_where (fixP->fx_file, fixP->fx_line,
28276                             _("rel31 relocation overflow"));
28277             }
28278           newval |= value & 0x7fffffff;
28279           md_number_to_chars (buf, newval, 4);
28280         }
28281       break;
28282 #endif
28283
28284     case BFD_RELOC_ARM_CP_OFF_IMM:
28285     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
28286     case BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM:
28287       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
28288         newval = md_chars_to_number (buf, INSN_SIZE);
28289       else
28290         newval = get_thumb32_insn (buf);
28291       if ((newval & 0x0f200f00) == 0x0d000900)
28292         {
28293           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
28294              has permitted values that are multiples of 2, in the range 0
28295              to 510.  */
28296           if (value < -510 || value > 510 || (value & 1))
28297             as_bad_where (fixP->fx_file, fixP->fx_line,
28298                           _("co-processor offset out of range"));
28299         }
28300       else if ((newval & 0xfe001f80) == 0xec000f80)
28301         {
28302           if (value < -511 || value > 512 || (value & 3))
28303             as_bad_where (fixP->fx_file, fixP->fx_line,
28304                           _("co-processor offset out of range"));
28305         }
28306       else if (value < -1023 || value > 1023 || (value & 3))
28307         as_bad_where (fixP->fx_file, fixP->fx_line,
28308                       _("co-processor offset out of range"));
28309     cp_off_common:
28310       sign = value > 0;
28311       if (value < 0)
28312         value = -value;
28313       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28314           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
28315         newval = md_chars_to_number (buf, INSN_SIZE);
28316       else
28317         newval = get_thumb32_insn (buf);
28318       if (value == 0)
28319         {
28320           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
28321             newval &= 0xffffff80;
28322           else
28323             newval &= 0xffffff00;
28324         }
28325       else
28326         {
28327           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
28328             newval &= 0xff7fff80;
28329           else
28330             newval &= 0xff7fff00;
28331           if ((newval & 0x0f200f00) == 0x0d000900)
28332             {
28333               /* This is a fp16 vstr/vldr.
28334
28335                  It requires the immediate offset in the instruction is shifted
28336                  left by 1 to be a half-word offset.
28337
28338                  Here, left shift by 1 first, and later right shift by 2
28339                  should get the right offset.  */
28340               value <<= 1;
28341             }
28342           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
28343         }
28344       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28345           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
28346         md_number_to_chars (buf, newval, INSN_SIZE);
28347       else
28348         put_thumb32_insn (buf, newval);
28349       break;
28350
28351     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
28352     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
28353       if (value < -255 || value > 255)
28354         as_bad_where (fixP->fx_file, fixP->fx_line,
28355                       _("co-processor offset out of range"));
28356       value *= 4;
28357       goto cp_off_common;
28358
28359     case BFD_RELOC_ARM_THUMB_OFFSET:
28360       newval = md_chars_to_number (buf, THUMB_SIZE);
28361       /* Exactly what ranges, and where the offset is inserted depends
28362          on the type of instruction, we can establish this from the
28363          top 4 bits.  */
28364       switch (newval >> 12)
28365         {
28366         case 4: /* PC load.  */
28367           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
28368              forced to zero for these loads; md_pcrel_from has already
28369              compensated for this.  */
28370           if (value & 3)
28371             as_bad_where (fixP->fx_file, fixP->fx_line,
28372                           _("invalid offset, target not word aligned (0x%08lX)"),
28373                           (((unsigned long) fixP->fx_frag->fr_address
28374                             + (unsigned long) fixP->fx_where) & ~3)
28375                           + (unsigned long) value);
28376
28377           if (value & ~0x3fc)
28378             as_bad_where (fixP->fx_file, fixP->fx_line,
28379                           _("invalid offset, value too big (0x%08lX)"),
28380                           (long) value);
28381
28382           newval |= value >> 2;
28383           break;
28384
28385         case 9: /* SP load/store.  */
28386           if (value & ~0x3fc)
28387             as_bad_where (fixP->fx_file, fixP->fx_line,
28388                           _("invalid offset, value too big (0x%08lX)"),
28389                           (long) value);
28390           newval |= value >> 2;
28391           break;
28392
28393         case 6: /* Word load/store.  */
28394           if (value & ~0x7c)
28395             as_bad_where (fixP->fx_file, fixP->fx_line,
28396                           _("invalid offset, value too big (0x%08lX)"),
28397                           (long) value);
28398           newval |= value << 4; /* 6 - 2.  */
28399           break;
28400
28401         case 7: /* Byte load/store.  */
28402           if (value & ~0x1f)
28403             as_bad_where (fixP->fx_file, fixP->fx_line,
28404                           _("invalid offset, value too big (0x%08lX)"),
28405                           (long) value);
28406           newval |= value << 6;
28407           break;
28408
28409         case 8: /* Halfword load/store.  */
28410           if (value & ~0x3e)
28411             as_bad_where (fixP->fx_file, fixP->fx_line,
28412                           _("invalid offset, value too big (0x%08lX)"),
28413                           (long) value);
28414           newval |= value << 5; /* 6 - 1.  */
28415           break;
28416
28417         default:
28418           as_bad_where (fixP->fx_file, fixP->fx_line,
28419                         "Unable to process relocation for thumb opcode: %lx",
28420                         (unsigned long) newval);
28421           break;
28422         }
28423       md_number_to_chars (buf, newval, THUMB_SIZE);
28424       break;
28425
28426     case BFD_RELOC_ARM_THUMB_ADD:
28427       /* This is a complicated relocation, since we use it for all of
28428          the following immediate relocations:
28429
28430             3bit ADD/SUB
28431             8bit ADD/SUB
28432             9bit ADD/SUB SP word-aligned
28433            10bit ADD PC/SP word-aligned
28434
28435          The type of instruction being processed is encoded in the
28436          instruction field:
28437
28438            0x8000  SUB
28439            0x00F0  Rd
28440            0x000F  Rs
28441       */
28442       newval = md_chars_to_number (buf, THUMB_SIZE);
28443       {
28444         int rd = (newval >> 4) & 0xf;
28445         int rs = newval & 0xf;
28446         int subtract = !!(newval & 0x8000);
28447
28448         /* Check for HI regs, only very restricted cases allowed:
28449            Adjusting SP, and using PC or SP to get an address.  */
28450         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
28451             || (rs > 7 && rs != REG_SP && rs != REG_PC))
28452           as_bad_where (fixP->fx_file, fixP->fx_line,
28453                         _("invalid Hi register with immediate"));
28454
28455         /* If value is negative, choose the opposite instruction.  */
28456         if (value < 0)
28457           {
28458             value = -value;
28459             subtract = !subtract;
28460             if (value < 0)
28461               as_bad_where (fixP->fx_file, fixP->fx_line,
28462                             _("immediate value out of range"));
28463           }
28464
28465         if (rd == REG_SP)
28466           {
28467             if (value & ~0x1fc)
28468               as_bad_where (fixP->fx_file, fixP->fx_line,
28469                             _("invalid immediate for stack address calculation"));
28470             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
28471             newval |= value >> 2;
28472           }
28473         else if (rs == REG_PC || rs == REG_SP)
28474           {
28475             /* PR gas/18541.  If the addition is for a defined symbol
28476                within range of an ADR instruction then accept it.  */
28477             if (subtract
28478                 && value == 4
28479                 && fixP->fx_addsy != NULL)
28480               {
28481                 subtract = 0;
28482
28483                 if (! S_IS_DEFINED (fixP->fx_addsy)
28484                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
28485                     || S_IS_WEAK (fixP->fx_addsy))
28486                   {
28487                     as_bad_where (fixP->fx_file, fixP->fx_line,
28488                                   _("address calculation needs a strongly defined nearby symbol"));
28489                   }
28490                 else
28491                   {
28492                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
28493
28494                     /* Round up to the next 4-byte boundary.  */
28495                     if (v & 3)
28496                       v = (v + 3) & ~ 3;
28497                     else
28498                       v += 4;
28499                     v = S_GET_VALUE (fixP->fx_addsy) - v;
28500
28501                     if (v & ~0x3fc)
28502                       {
28503                         as_bad_where (fixP->fx_file, fixP->fx_line,
28504                                       _("symbol too far away"));
28505                       }
28506                     else
28507                       {
28508                         fixP->fx_done = 1;
28509                         value = v;
28510                       }
28511                   }
28512               }
28513
28514             if (subtract || value & ~0x3fc)
28515               as_bad_where (fixP->fx_file, fixP->fx_line,
28516                             _("invalid immediate for address calculation (value = 0x%08lX)"),
28517                             (unsigned long) (subtract ? - value : value));
28518             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
28519             newval |= rd << 8;
28520             newval |= value >> 2;
28521           }
28522         else if (rs == rd)
28523           {
28524             if (value & ~0xff)
28525               as_bad_where (fixP->fx_file, fixP->fx_line,
28526                             _("immediate value out of range"));
28527             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
28528             newval |= (rd << 8) | value;
28529           }
28530         else
28531           {
28532             if (value & ~0x7)
28533               as_bad_where (fixP->fx_file, fixP->fx_line,
28534                             _("immediate value out of range"));
28535             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
28536             newval |= rd | (rs << 3) | (value << 6);
28537           }
28538       }
28539       md_number_to_chars (buf, newval, THUMB_SIZE);
28540       break;
28541
28542     case BFD_RELOC_ARM_THUMB_IMM:
28543       newval = md_chars_to_number (buf, THUMB_SIZE);
28544       if (value < 0 || value > 255)
28545         as_bad_where (fixP->fx_file, fixP->fx_line,
28546                       _("invalid immediate: %ld is out of range"),
28547                       (long) value);
28548       newval |= value;
28549       md_number_to_chars (buf, newval, THUMB_SIZE);
28550       break;
28551
28552     case BFD_RELOC_ARM_THUMB_SHIFT:
28553       /* 5bit shift value (0..32).  LSL cannot take 32.  */
28554       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
28555       temp = newval & 0xf800;
28556       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
28557         as_bad_where (fixP->fx_file, fixP->fx_line,
28558                       _("invalid shift value: %ld"), (long) value);
28559       /* Shifts of zero must be encoded as LSL.  */
28560       if (value == 0)
28561         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
28562       /* Shifts of 32 are encoded as zero.  */
28563       else if (value == 32)
28564         value = 0;
28565       newval |= value << 6;
28566       md_number_to_chars (buf, newval, THUMB_SIZE);
28567       break;
28568
28569     case BFD_RELOC_VTABLE_INHERIT:
28570     case BFD_RELOC_VTABLE_ENTRY:
28571       fixP->fx_done = 0;
28572       return;
28573
28574     case BFD_RELOC_ARM_MOVW:
28575     case BFD_RELOC_ARM_MOVT:
28576     case BFD_RELOC_ARM_THUMB_MOVW:
28577     case BFD_RELOC_ARM_THUMB_MOVT:
28578       if (fixP->fx_done || !seg->use_rela_p)
28579         {
28580           /* REL format relocations are limited to a 16-bit addend.  */
28581           if (!fixP->fx_done)
28582             {
28583               if (value < -0x8000 || value > 0x7fff)
28584                   as_bad_where (fixP->fx_file, fixP->fx_line,
28585                                 _("offset out of range"));
28586             }
28587           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
28588                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
28589             {
28590               value >>= 16;
28591             }
28592
28593           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
28594               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
28595             {
28596               newval = get_thumb32_insn (buf);
28597               newval &= 0xfbf08f00;
28598               newval |= (value & 0xf000) << 4;
28599               newval |= (value & 0x0800) << 15;
28600               newval |= (value & 0x0700) << 4;
28601               newval |= (value & 0x00ff);
28602               put_thumb32_insn (buf, newval);
28603             }
28604           else
28605             {
28606               newval = md_chars_to_number (buf, 4);
28607               newval &= 0xfff0f000;
28608               newval |= value & 0x0fff;
28609               newval |= (value & 0xf000) << 4;
28610               md_number_to_chars (buf, newval, 4);
28611             }
28612         }
28613       return;
28614
28615    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
28616    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
28617    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
28618    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
28619       gas_assert (!fixP->fx_done);
28620       {
28621         bfd_vma insn;
28622         bfd_boolean is_mov;
28623         bfd_vma encoded_addend = value;
28624
28625         /* Check that addend can be encoded in instruction.  */
28626         if (!seg->use_rela_p && (value < 0 || value > 255))
28627           as_bad_where (fixP->fx_file, fixP->fx_line,
28628                         _("the offset 0x%08lX is not representable"),
28629                         (unsigned long) encoded_addend);
28630
28631         /* Extract the instruction.  */
28632         insn = md_chars_to_number (buf, THUMB_SIZE);
28633         is_mov = (insn & 0xf800) == 0x2000;
28634
28635         /* Encode insn.  */
28636         if (is_mov)
28637           {
28638             if (!seg->use_rela_p)
28639               insn |= encoded_addend;
28640           }
28641         else
28642           {
28643             int rd, rs;
28644
28645             /* Extract the instruction.  */
28646              /* Encoding is the following
28647                 0x8000  SUB
28648                 0x00F0  Rd
28649                 0x000F  Rs
28650              */
28651              /* The following conditions must be true :
28652                 - ADD
28653                 - Rd == Rs
28654                 - Rd <= 7
28655              */
28656             rd = (insn >> 4) & 0xf;
28657             rs = insn & 0xf;
28658             if ((insn & 0x8000) || (rd != rs) || rd > 7)
28659               as_bad_where (fixP->fx_file, fixP->fx_line,
28660                         _("Unable to process relocation for thumb opcode: %lx"),
28661                         (unsigned long) insn);
28662
28663             /* Encode as ADD immediate8 thumb 1 code.  */
28664             insn = 0x3000 | (rd << 8);
28665
28666             /* Place the encoded addend into the first 8 bits of the
28667                instruction.  */
28668             if (!seg->use_rela_p)
28669               insn |= encoded_addend;
28670           }
28671
28672         /* Update the instruction.  */
28673         md_number_to_chars (buf, insn, THUMB_SIZE);
28674       }
28675       break;
28676
28677    case BFD_RELOC_ARM_ALU_PC_G0_NC:
28678    case BFD_RELOC_ARM_ALU_PC_G0:
28679    case BFD_RELOC_ARM_ALU_PC_G1_NC:
28680    case BFD_RELOC_ARM_ALU_PC_G1:
28681    case BFD_RELOC_ARM_ALU_PC_G2:
28682    case BFD_RELOC_ARM_ALU_SB_G0_NC:
28683    case BFD_RELOC_ARM_ALU_SB_G0:
28684    case BFD_RELOC_ARM_ALU_SB_G1_NC:
28685    case BFD_RELOC_ARM_ALU_SB_G1:
28686    case BFD_RELOC_ARM_ALU_SB_G2:
28687      gas_assert (!fixP->fx_done);
28688      if (!seg->use_rela_p)
28689        {
28690          bfd_vma insn;
28691          bfd_vma encoded_addend;
28692          bfd_vma addend_abs = llabs (value);
28693
28694          /* Check that the absolute value of the addend can be
28695             expressed as an 8-bit constant plus a rotation.  */
28696          encoded_addend = encode_arm_immediate (addend_abs);
28697          if (encoded_addend == (unsigned int) FAIL)
28698            as_bad_where (fixP->fx_file, fixP->fx_line,
28699                          _("the offset 0x%08lX is not representable"),
28700                          (unsigned long) addend_abs);
28701
28702          /* Extract the instruction.  */
28703          insn = md_chars_to_number (buf, INSN_SIZE);
28704
28705          /* If the addend is positive, use an ADD instruction.
28706             Otherwise use a SUB.  Take care not to destroy the S bit.  */
28707          insn &= 0xff1fffff;
28708          if (value < 0)
28709            insn |= 1 << 22;
28710          else
28711            insn |= 1 << 23;
28712
28713          /* Place the encoded addend into the first 12 bits of the
28714             instruction.  */
28715          insn &= 0xfffff000;
28716          insn |= encoded_addend;
28717
28718          /* Update the instruction.  */
28719          md_number_to_chars (buf, insn, INSN_SIZE);
28720        }
28721      break;
28722
28723     case BFD_RELOC_ARM_LDR_PC_G0:
28724     case BFD_RELOC_ARM_LDR_PC_G1:
28725     case BFD_RELOC_ARM_LDR_PC_G2:
28726     case BFD_RELOC_ARM_LDR_SB_G0:
28727     case BFD_RELOC_ARM_LDR_SB_G1:
28728     case BFD_RELOC_ARM_LDR_SB_G2:
28729       gas_assert (!fixP->fx_done);
28730       if (!seg->use_rela_p)
28731         {
28732           bfd_vma insn;
28733           bfd_vma addend_abs = llabs (value);
28734
28735           /* Check that the absolute value of the addend can be
28736              encoded in 12 bits.  */
28737           if (addend_abs >= 0x1000)
28738             as_bad_where (fixP->fx_file, fixP->fx_line,
28739                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
28740                           (unsigned long) addend_abs);
28741
28742           /* Extract the instruction.  */
28743           insn = md_chars_to_number (buf, INSN_SIZE);
28744
28745           /* If the addend is negative, clear bit 23 of the instruction.
28746              Otherwise set it.  */
28747           if (value < 0)
28748             insn &= ~(1 << 23);
28749           else
28750             insn |= 1 << 23;
28751
28752           /* Place the absolute value of the addend into the first 12 bits
28753              of the instruction.  */
28754           insn &= 0xfffff000;
28755           insn |= addend_abs;
28756
28757           /* Update the instruction.  */
28758           md_number_to_chars (buf, insn, INSN_SIZE);
28759         }
28760       break;
28761
28762     case BFD_RELOC_ARM_LDRS_PC_G0:
28763     case BFD_RELOC_ARM_LDRS_PC_G1:
28764     case BFD_RELOC_ARM_LDRS_PC_G2:
28765     case BFD_RELOC_ARM_LDRS_SB_G0:
28766     case BFD_RELOC_ARM_LDRS_SB_G1:
28767     case BFD_RELOC_ARM_LDRS_SB_G2:
28768       gas_assert (!fixP->fx_done);
28769       if (!seg->use_rela_p)
28770         {
28771           bfd_vma insn;
28772           bfd_vma addend_abs = llabs (value);
28773
28774           /* Check that the absolute value of the addend can be
28775              encoded in 8 bits.  */
28776           if (addend_abs >= 0x100)
28777             as_bad_where (fixP->fx_file, fixP->fx_line,
28778                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
28779                           (unsigned long) addend_abs);
28780
28781           /* Extract the instruction.  */
28782           insn = md_chars_to_number (buf, INSN_SIZE);
28783
28784           /* If the addend is negative, clear bit 23 of the instruction.
28785              Otherwise set it.  */
28786           if (value < 0)
28787             insn &= ~(1 << 23);
28788           else
28789             insn |= 1 << 23;
28790
28791           /* Place the first four bits of the absolute value of the addend
28792              into the first 4 bits of the instruction, and the remaining
28793              four into bits 8 .. 11.  */
28794           insn &= 0xfffff0f0;
28795           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
28796
28797           /* Update the instruction.  */
28798           md_number_to_chars (buf, insn, INSN_SIZE);
28799         }
28800       break;
28801
28802     case BFD_RELOC_ARM_LDC_PC_G0:
28803     case BFD_RELOC_ARM_LDC_PC_G1:
28804     case BFD_RELOC_ARM_LDC_PC_G2:
28805     case BFD_RELOC_ARM_LDC_SB_G0:
28806     case BFD_RELOC_ARM_LDC_SB_G1:
28807     case BFD_RELOC_ARM_LDC_SB_G2:
28808       gas_assert (!fixP->fx_done);
28809       if (!seg->use_rela_p)
28810         {
28811           bfd_vma insn;
28812           bfd_vma addend_abs = llabs (value);
28813
28814           /* Check that the absolute value of the addend is a multiple of
28815              four and, when divided by four, fits in 8 bits.  */
28816           if (addend_abs & 0x3)
28817             as_bad_where (fixP->fx_file, fixP->fx_line,
28818                           _("bad offset 0x%08lX (must be word-aligned)"),
28819                           (unsigned long) addend_abs);
28820
28821           if ((addend_abs >> 2) > 0xff)
28822             as_bad_where (fixP->fx_file, fixP->fx_line,
28823                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
28824                           (unsigned long) addend_abs);
28825
28826           /* Extract the instruction.  */
28827           insn = md_chars_to_number (buf, INSN_SIZE);
28828
28829           /* If the addend is negative, clear bit 23 of the instruction.
28830              Otherwise set it.  */
28831           if (value < 0)
28832             insn &= ~(1 << 23);
28833           else
28834             insn |= 1 << 23;
28835
28836           /* Place the addend (divided by four) into the first eight
28837              bits of the instruction.  */
28838           insn &= 0xfffffff0;
28839           insn |= addend_abs >> 2;
28840
28841           /* Update the instruction.  */
28842           md_number_to_chars (buf, insn, INSN_SIZE);
28843         }
28844       break;
28845
28846     case BFD_RELOC_THUMB_PCREL_BRANCH5:
28847       if (fixP->fx_addsy
28848           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28849           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28850           && ARM_IS_FUNC (fixP->fx_addsy)
28851           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28852         {
28853           /* Force a relocation for a branch 5 bits wide.  */
28854           fixP->fx_done = 0;
28855         }
28856       if (v8_1_branch_value_check (value, 5, FALSE) == FAIL)
28857         as_bad_where (fixP->fx_file, fixP->fx_line,
28858                       BAD_BRANCH_OFF);
28859
28860       if (fixP->fx_done || !seg->use_rela_p)
28861         {
28862           addressT boff = value >> 1;
28863
28864           newval  = md_chars_to_number (buf, THUMB_SIZE);
28865           newval |= (boff << 7);
28866           md_number_to_chars (buf, newval, THUMB_SIZE);
28867         }
28868       break;
28869
28870     case BFD_RELOC_THUMB_PCREL_BFCSEL:
28871       if (fixP->fx_addsy
28872           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28873           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28874           && ARM_IS_FUNC (fixP->fx_addsy)
28875           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28876         {
28877           fixP->fx_done = 0;
28878         }
28879       if ((value & ~0x7f) && ((value & ~0x3f) != ~0x3f))
28880         as_bad_where (fixP->fx_file, fixP->fx_line,
28881                       _("branch out of range"));
28882
28883       if (fixP->fx_done || !seg->use_rela_p)
28884         {
28885           newval  = md_chars_to_number (buf, THUMB_SIZE);
28886
28887           addressT boff = ((newval & 0x0780) >> 7) << 1;
28888           addressT diff = value - boff;
28889
28890           if (diff == 4)
28891             {
28892               newval |= 1 << 1; /* T bit.  */
28893             }
28894           else if (diff != 2)
28895             {
28896               as_bad_where (fixP->fx_file, fixP->fx_line,
28897                             _("out of range label-relative fixup value"));
28898             }
28899           md_number_to_chars (buf, newval, THUMB_SIZE);
28900         }
28901       break;
28902
28903     case BFD_RELOC_ARM_THUMB_BF17:
28904       if (fixP->fx_addsy
28905           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28906           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28907           && ARM_IS_FUNC (fixP->fx_addsy)
28908           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28909         {
28910           /* Force a relocation for a branch 17 bits wide.  */
28911           fixP->fx_done = 0;
28912         }
28913
28914       if (v8_1_branch_value_check (value, 17, TRUE) == FAIL)
28915         as_bad_where (fixP->fx_file, fixP->fx_line,
28916                       BAD_BRANCH_OFF);
28917
28918       if (fixP->fx_done || !seg->use_rela_p)
28919         {
28920           offsetT newval2;
28921           addressT immA, immB, immC;
28922
28923           immA = (value & 0x0001f000) >> 12;
28924           immB = (value & 0x00000ffc) >> 2;
28925           immC = (value & 0x00000002) >> 1;
28926
28927           newval   = md_chars_to_number (buf, THUMB_SIZE);
28928           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28929           newval  |= immA;
28930           newval2 |= (immC << 11) | (immB << 1);
28931           md_number_to_chars (buf, newval, THUMB_SIZE);
28932           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28933         }
28934       break;
28935
28936     case BFD_RELOC_ARM_THUMB_BF19:
28937       if (fixP->fx_addsy
28938           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28939           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28940           && ARM_IS_FUNC (fixP->fx_addsy)
28941           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28942         {
28943           /* Force a relocation for a branch 19 bits wide.  */
28944           fixP->fx_done = 0;
28945         }
28946
28947       if (v8_1_branch_value_check (value, 19, TRUE) == FAIL)
28948         as_bad_where (fixP->fx_file, fixP->fx_line,
28949                       BAD_BRANCH_OFF);
28950
28951       if (fixP->fx_done || !seg->use_rela_p)
28952         {
28953           offsetT newval2;
28954           addressT immA, immB, immC;
28955
28956           immA = (value & 0x0007f000) >> 12;
28957           immB = (value & 0x00000ffc) >> 2;
28958           immC = (value & 0x00000002) >> 1;
28959
28960           newval   = md_chars_to_number (buf, THUMB_SIZE);
28961           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28962           newval  |= immA;
28963           newval2 |= (immC << 11) | (immB << 1);
28964           md_number_to_chars (buf, newval, THUMB_SIZE);
28965           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28966         }
28967       break;
28968
28969     case BFD_RELOC_ARM_THUMB_BF13:
28970       if (fixP->fx_addsy
28971           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
28972           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
28973           && ARM_IS_FUNC (fixP->fx_addsy)
28974           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
28975         {
28976           /* Force a relocation for a branch 13 bits wide.  */
28977           fixP->fx_done = 0;
28978         }
28979
28980       if (v8_1_branch_value_check (value, 13, TRUE) == FAIL)
28981         as_bad_where (fixP->fx_file, fixP->fx_line,
28982                       BAD_BRANCH_OFF);
28983
28984       if (fixP->fx_done || !seg->use_rela_p)
28985         {
28986           offsetT newval2;
28987           addressT immA, immB, immC;
28988
28989           immA = (value & 0x00001000) >> 12;
28990           immB = (value & 0x00000ffc) >> 2;
28991           immC = (value & 0x00000002) >> 1;
28992
28993           newval   = md_chars_to_number (buf, THUMB_SIZE);
28994           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
28995           newval  |= immA;
28996           newval2 |= (immC << 11) | (immB << 1);
28997           md_number_to_chars (buf, newval, THUMB_SIZE);
28998           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
28999         }
29000       break;
29001
29002     case BFD_RELOC_ARM_THUMB_LOOP12:
29003       if (fixP->fx_addsy
29004           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
29005           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
29006           && ARM_IS_FUNC (fixP->fx_addsy)
29007           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
29008         {
29009           /* Force a relocation for a branch 12 bits wide.  */
29010           fixP->fx_done = 0;
29011         }
29012
29013       bfd_vma insn = get_thumb32_insn (buf);
29014       /* le lr, <label>, le <label> or letp lr, <label> */
29015       if (((insn & 0xffffffff) == 0xf00fc001)
29016           || ((insn & 0xffffffff) == 0xf02fc001)
29017           || ((insn & 0xffffffff) == 0xf01fc001))
29018         value = -value;
29019
29020       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
29021         as_bad_where (fixP->fx_file, fixP->fx_line,
29022                       BAD_BRANCH_OFF);
29023       if (fixP->fx_done || !seg->use_rela_p)
29024         {
29025           addressT imml, immh;
29026
29027           immh = (value & 0x00000ffc) >> 2;
29028           imml = (value & 0x00000002) >> 1;
29029
29030           newval  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
29031           newval |= (imml << 11) | (immh << 1);
29032           md_number_to_chars (buf + THUMB_SIZE, newval, THUMB_SIZE);
29033         }
29034       break;
29035
29036     case BFD_RELOC_ARM_V4BX:
29037       /* This will need to go in the object file.  */
29038       fixP->fx_done = 0;
29039       break;
29040
29041     case BFD_RELOC_UNUSED:
29042     default:
29043       as_bad_where (fixP->fx_file, fixP->fx_line,
29044                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
29045     }
29046 }
29047
29048 /* Translate internal representation of relocation info to BFD target
29049    format.  */
29050
29051 arelent *
29052 tc_gen_reloc (asection *section, fixS *fixp)
29053 {
29054   arelent * reloc;
29055   bfd_reloc_code_real_type code;
29056
29057   reloc = XNEW (arelent);
29058
29059   reloc->sym_ptr_ptr = XNEW (asymbol *);
29060   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
29061   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
29062
29063   if (fixp->fx_pcrel)
29064     {
29065       if (section->use_rela_p)
29066         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
29067       else
29068         fixp->fx_offset = reloc->address;
29069     }
29070   reloc->addend = fixp->fx_offset;
29071
29072   switch (fixp->fx_r_type)
29073     {
29074     case BFD_RELOC_8:
29075       if (fixp->fx_pcrel)
29076         {
29077           code = BFD_RELOC_8_PCREL;
29078           break;
29079         }
29080       /* Fall through.  */
29081
29082     case BFD_RELOC_16:
29083       if (fixp->fx_pcrel)
29084         {
29085           code = BFD_RELOC_16_PCREL;
29086           break;
29087         }
29088       /* Fall through.  */
29089
29090     case BFD_RELOC_32:
29091       if (fixp->fx_pcrel)
29092         {
29093           code = BFD_RELOC_32_PCREL;
29094           break;
29095         }
29096       /* Fall through.  */
29097
29098     case BFD_RELOC_ARM_MOVW:
29099       if (fixp->fx_pcrel)
29100         {
29101           code = BFD_RELOC_ARM_MOVW_PCREL;
29102           break;
29103         }
29104       /* Fall through.  */
29105
29106     case BFD_RELOC_ARM_MOVT:
29107       if (fixp->fx_pcrel)
29108         {
29109           code = BFD_RELOC_ARM_MOVT_PCREL;
29110           break;
29111         }
29112       /* Fall through.  */
29113
29114     case BFD_RELOC_ARM_THUMB_MOVW:
29115       if (fixp->fx_pcrel)
29116         {
29117           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
29118           break;
29119         }
29120       /* Fall through.  */
29121
29122     case BFD_RELOC_ARM_THUMB_MOVT:
29123       if (fixp->fx_pcrel)
29124         {
29125           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
29126           break;
29127         }
29128       /* Fall through.  */
29129
29130     case BFD_RELOC_NONE:
29131     case BFD_RELOC_ARM_PCREL_BRANCH:
29132     case BFD_RELOC_ARM_PCREL_BLX:
29133     case BFD_RELOC_RVA:
29134     case BFD_RELOC_THUMB_PCREL_BRANCH7:
29135     case BFD_RELOC_THUMB_PCREL_BRANCH9:
29136     case BFD_RELOC_THUMB_PCREL_BRANCH12:
29137     case BFD_RELOC_THUMB_PCREL_BRANCH20:
29138     case BFD_RELOC_THUMB_PCREL_BRANCH23:
29139     case BFD_RELOC_THUMB_PCREL_BRANCH25:
29140     case BFD_RELOC_VTABLE_ENTRY:
29141     case BFD_RELOC_VTABLE_INHERIT:
29142 #ifdef TE_PE
29143     case BFD_RELOC_32_SECREL:
29144 #endif
29145       code = fixp->fx_r_type;
29146       break;
29147
29148     case BFD_RELOC_THUMB_PCREL_BLX:
29149 #ifdef OBJ_ELF
29150       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
29151         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
29152       else
29153 #endif
29154         code = BFD_RELOC_THUMB_PCREL_BLX;
29155       break;
29156
29157     case BFD_RELOC_ARM_LITERAL:
29158     case BFD_RELOC_ARM_HWLITERAL:
29159       /* If this is called then the a literal has
29160          been referenced across a section boundary.  */
29161       as_bad_where (fixp->fx_file, fixp->fx_line,
29162                     _("literal referenced across section boundary"));
29163       return NULL;
29164
29165 #ifdef OBJ_ELF
29166     case BFD_RELOC_ARM_TLS_CALL:
29167     case BFD_RELOC_ARM_THM_TLS_CALL:
29168     case BFD_RELOC_ARM_TLS_DESCSEQ:
29169     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
29170     case BFD_RELOC_ARM_GOT32:
29171     case BFD_RELOC_ARM_GOTOFF:
29172     case BFD_RELOC_ARM_GOT_PREL:
29173     case BFD_RELOC_ARM_PLT32:
29174     case BFD_RELOC_ARM_TARGET1:
29175     case BFD_RELOC_ARM_ROSEGREL32:
29176     case BFD_RELOC_ARM_SBREL32:
29177     case BFD_RELOC_ARM_PREL31:
29178     case BFD_RELOC_ARM_TARGET2:
29179     case BFD_RELOC_ARM_TLS_LDO32:
29180     case BFD_RELOC_ARM_PCREL_CALL:
29181     case BFD_RELOC_ARM_PCREL_JUMP:
29182     case BFD_RELOC_ARM_ALU_PC_G0_NC:
29183     case BFD_RELOC_ARM_ALU_PC_G0:
29184     case BFD_RELOC_ARM_ALU_PC_G1_NC:
29185     case BFD_RELOC_ARM_ALU_PC_G1:
29186     case BFD_RELOC_ARM_ALU_PC_G2:
29187     case BFD_RELOC_ARM_LDR_PC_G0:
29188     case BFD_RELOC_ARM_LDR_PC_G1:
29189     case BFD_RELOC_ARM_LDR_PC_G2:
29190     case BFD_RELOC_ARM_LDRS_PC_G0:
29191     case BFD_RELOC_ARM_LDRS_PC_G1:
29192     case BFD_RELOC_ARM_LDRS_PC_G2:
29193     case BFD_RELOC_ARM_LDC_PC_G0:
29194     case BFD_RELOC_ARM_LDC_PC_G1:
29195     case BFD_RELOC_ARM_LDC_PC_G2:
29196     case BFD_RELOC_ARM_ALU_SB_G0_NC:
29197     case BFD_RELOC_ARM_ALU_SB_G0:
29198     case BFD_RELOC_ARM_ALU_SB_G1_NC:
29199     case BFD_RELOC_ARM_ALU_SB_G1:
29200     case BFD_RELOC_ARM_ALU_SB_G2:
29201     case BFD_RELOC_ARM_LDR_SB_G0:
29202     case BFD_RELOC_ARM_LDR_SB_G1:
29203     case BFD_RELOC_ARM_LDR_SB_G2:
29204     case BFD_RELOC_ARM_LDRS_SB_G0:
29205     case BFD_RELOC_ARM_LDRS_SB_G1:
29206     case BFD_RELOC_ARM_LDRS_SB_G2:
29207     case BFD_RELOC_ARM_LDC_SB_G0:
29208     case BFD_RELOC_ARM_LDC_SB_G1:
29209     case BFD_RELOC_ARM_LDC_SB_G2:
29210     case BFD_RELOC_ARM_V4BX:
29211     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
29212     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
29213     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
29214     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
29215     case BFD_RELOC_ARM_GOTFUNCDESC:
29216     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
29217     case BFD_RELOC_ARM_FUNCDESC:
29218     case BFD_RELOC_ARM_THUMB_BF17:
29219     case BFD_RELOC_ARM_THUMB_BF19:
29220     case BFD_RELOC_ARM_THUMB_BF13:
29221       code = fixp->fx_r_type;
29222       break;
29223
29224     case BFD_RELOC_ARM_TLS_GOTDESC:
29225     case BFD_RELOC_ARM_TLS_GD32:
29226     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
29227     case BFD_RELOC_ARM_TLS_LE32:
29228     case BFD_RELOC_ARM_TLS_IE32:
29229     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
29230     case BFD_RELOC_ARM_TLS_LDM32:
29231     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
29232       /* BFD will include the symbol's address in the addend.
29233          But we don't want that, so subtract it out again here.  */
29234       if (!S_IS_COMMON (fixp->fx_addsy))
29235         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
29236       code = fixp->fx_r_type;
29237       break;
29238 #endif
29239
29240     case BFD_RELOC_ARM_IMMEDIATE:
29241       as_bad_where (fixp->fx_file, fixp->fx_line,
29242                     _("internal relocation (type: IMMEDIATE) not fixed up"));
29243       return NULL;
29244
29245     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
29246       as_bad_where (fixp->fx_file, fixp->fx_line,
29247                     _("ADRL used for a symbol not defined in the same file"));
29248       return NULL;
29249
29250     case BFD_RELOC_THUMB_PCREL_BRANCH5:
29251     case BFD_RELOC_THUMB_PCREL_BFCSEL:
29252     case BFD_RELOC_ARM_THUMB_LOOP12:
29253       as_bad_where (fixp->fx_file, fixp->fx_line,
29254                     _("%s used for a symbol not defined in the same file"),
29255                     bfd_get_reloc_code_name (fixp->fx_r_type));
29256       return NULL;
29257
29258     case BFD_RELOC_ARM_OFFSET_IMM:
29259       if (section->use_rela_p)
29260         {
29261           code = fixp->fx_r_type;
29262           break;
29263         }
29264
29265       if (fixp->fx_addsy != NULL
29266           && !S_IS_DEFINED (fixp->fx_addsy)
29267           && S_IS_LOCAL (fixp->fx_addsy))
29268         {
29269           as_bad_where (fixp->fx_file, fixp->fx_line,
29270                         _("undefined local label `%s'"),
29271                         S_GET_NAME (fixp->fx_addsy));
29272           return NULL;
29273         }
29274
29275       as_bad_where (fixp->fx_file, fixp->fx_line,
29276                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
29277       return NULL;
29278
29279     default:
29280       {
29281         const char * type;
29282
29283         switch (fixp->fx_r_type)
29284           {
29285           case BFD_RELOC_NONE:             type = "NONE";         break;
29286           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
29287           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
29288           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
29289           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
29290           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
29291           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
29292           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
29293           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
29294           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
29295           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
29296           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
29297           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
29298           default:                         type = _("<unknown>"); break;
29299           }
29300         as_bad_where (fixp->fx_file, fixp->fx_line,
29301                       _("cannot represent %s relocation in this object file format"),
29302                       type);
29303         return NULL;
29304       }
29305     }
29306
29307 #ifdef OBJ_ELF
29308   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
29309       && GOT_symbol
29310       && fixp->fx_addsy == GOT_symbol)
29311     {
29312       code = BFD_RELOC_ARM_GOTPC;
29313       reloc->addend = fixp->fx_offset = reloc->address;
29314     }
29315 #endif
29316
29317   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
29318
29319   if (reloc->howto == NULL)
29320     {
29321       as_bad_where (fixp->fx_file, fixp->fx_line,
29322                     _("cannot represent %s relocation in this object file format"),
29323                     bfd_get_reloc_code_name (code));
29324       return NULL;
29325     }
29326
29327   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
29328      vtable entry to be used in the relocation's section offset.  */
29329   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
29330     reloc->address = fixp->fx_offset;
29331
29332   return reloc;
29333 }
29334
29335 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
29336
29337 void
29338 cons_fix_new_arm (fragS *       frag,
29339                   int           where,
29340                   int           size,
29341                   expressionS * exp,
29342                   bfd_reloc_code_real_type reloc)
29343 {
29344   int pcrel = 0;
29345
29346   /* Pick a reloc.
29347      FIXME: @@ Should look at CPU word size.  */
29348   switch (size)
29349     {
29350     case 1:
29351       reloc = BFD_RELOC_8;
29352       break;
29353     case 2:
29354       reloc = BFD_RELOC_16;
29355       break;
29356     case 4:
29357     default:
29358       reloc = BFD_RELOC_32;
29359       break;
29360     case 8:
29361       reloc = BFD_RELOC_64;
29362       break;
29363     }
29364
29365 #ifdef TE_PE
29366   if (exp->X_op == O_secrel)
29367   {
29368     exp->X_op = O_symbol;
29369     reloc = BFD_RELOC_32_SECREL;
29370   }
29371 #endif
29372
29373   fix_new_exp (frag, where, size, exp, pcrel, reloc);
29374 }
29375
29376 #if defined (OBJ_COFF)
29377 void
29378 arm_validate_fix (fixS * fixP)
29379 {
29380   /* If the destination of the branch is a defined symbol which does not have
29381      the THUMB_FUNC attribute, then we must be calling a function which has
29382      the (interfacearm) attribute.  We look for the Thumb entry point to that
29383      function and change the branch to refer to that function instead.  */
29384   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
29385       && fixP->fx_addsy != NULL
29386       && S_IS_DEFINED (fixP->fx_addsy)
29387       && ! THUMB_IS_FUNC (fixP->fx_addsy))
29388     {
29389       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
29390     }
29391 }
29392 #endif
29393
29394
29395 int
29396 arm_force_relocation (struct fix * fixp)
29397 {
29398 #if defined (OBJ_COFF) && defined (TE_PE)
29399   if (fixp->fx_r_type == BFD_RELOC_RVA)
29400     return 1;
29401 #endif
29402
29403   /* In case we have a call or a branch to a function in ARM ISA mode from
29404      a thumb function or vice-versa force the relocation. These relocations
29405      are cleared off for some cores that might have blx and simple transformations
29406      are possible.  */
29407
29408 #ifdef OBJ_ELF
29409   switch (fixp->fx_r_type)
29410     {
29411     case BFD_RELOC_ARM_PCREL_JUMP:
29412     case BFD_RELOC_ARM_PCREL_CALL:
29413     case BFD_RELOC_THUMB_PCREL_BLX:
29414       if (THUMB_IS_FUNC (fixp->fx_addsy))
29415         return 1;
29416       break;
29417
29418     case BFD_RELOC_ARM_PCREL_BLX:
29419     case BFD_RELOC_THUMB_PCREL_BRANCH25:
29420     case BFD_RELOC_THUMB_PCREL_BRANCH20:
29421     case BFD_RELOC_THUMB_PCREL_BRANCH23:
29422       if (ARM_IS_FUNC (fixp->fx_addsy))
29423         return 1;
29424       break;
29425
29426     default:
29427       break;
29428     }
29429 #endif
29430
29431   /* Resolve these relocations even if the symbol is extern or weak.
29432      Technically this is probably wrong due to symbol preemption.
29433      In practice these relocations do not have enough range to be useful
29434      at dynamic link time, and some code (e.g. in the Linux kernel)
29435      expects these references to be resolved.  */
29436   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
29437       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
29438       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
29439       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
29440       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
29441       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
29442       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
29443       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
29444       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
29445       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
29446       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
29447       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
29448       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
29449       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
29450     return 0;
29451
29452   /* Always leave these relocations for the linker.  */
29453   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
29454        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
29455       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
29456     return 1;
29457
29458   /* Always generate relocations against function symbols.  */
29459   if (fixp->fx_r_type == BFD_RELOC_32
29460       && fixp->fx_addsy
29461       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
29462     return 1;
29463
29464   return generic_force_reloc (fixp);
29465 }
29466
29467 #if defined (OBJ_ELF) || defined (OBJ_COFF)
29468 /* Relocations against function names must be left unadjusted,
29469    so that the linker can use this information to generate interworking
29470    stubs.  The MIPS version of this function
29471    also prevents relocations that are mips-16 specific, but I do not
29472    know why it does this.
29473
29474    FIXME:
29475    There is one other problem that ought to be addressed here, but
29476    which currently is not:  Taking the address of a label (rather
29477    than a function) and then later jumping to that address.  Such
29478    addresses also ought to have their bottom bit set (assuming that
29479    they reside in Thumb code), but at the moment they will not.  */
29480
29481 bfd_boolean
29482 arm_fix_adjustable (fixS * fixP)
29483 {
29484   if (fixP->fx_addsy == NULL)
29485     return 1;
29486
29487   /* Preserve relocations against symbols with function type.  */
29488   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
29489     return FALSE;
29490
29491   if (THUMB_IS_FUNC (fixP->fx_addsy)
29492       && fixP->fx_subsy == NULL)
29493     return FALSE;
29494
29495   /* We need the symbol name for the VTABLE entries.  */
29496   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
29497       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
29498     return FALSE;
29499
29500   /* Don't allow symbols to be discarded on GOT related relocs.  */
29501   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
29502       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
29503       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
29504       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
29505       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32_FDPIC
29506       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
29507       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
29508       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32_FDPIC
29509       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
29510       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32_FDPIC
29511       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
29512       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
29513       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
29514       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
29515       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
29516       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
29517       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
29518     return FALSE;
29519
29520   /* Similarly for group relocations.  */
29521   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
29522        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
29523       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
29524     return FALSE;
29525
29526   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
29527   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
29528       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
29529       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
29530       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
29531       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
29532       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
29533       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
29534       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
29535     return FALSE;
29536
29537   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
29538      offsets, so keep these symbols.  */
29539   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
29540       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
29541     return FALSE;
29542
29543   return TRUE;
29544 }
29545 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
29546
29547 #ifdef OBJ_ELF
29548 const char *
29549 elf32_arm_target_format (void)
29550 {
29551 #ifdef TE_SYMBIAN
29552   return (target_big_endian
29553           ? "elf32-bigarm-symbian"
29554           : "elf32-littlearm-symbian");
29555 #elif defined (TE_VXWORKS)
29556   return (target_big_endian
29557           ? "elf32-bigarm-vxworks"
29558           : "elf32-littlearm-vxworks");
29559 #elif defined (TE_NACL)
29560   return (target_big_endian
29561           ? "elf32-bigarm-nacl"
29562           : "elf32-littlearm-nacl");
29563 #else
29564   if (arm_fdpic)
29565     {
29566       if (target_big_endian)
29567         return "elf32-bigarm-fdpic";
29568       else
29569         return "elf32-littlearm-fdpic";
29570     }
29571   else
29572     {
29573       if (target_big_endian)
29574         return "elf32-bigarm";
29575       else
29576         return "elf32-littlearm";
29577     }
29578 #endif
29579 }
29580
29581 void
29582 armelf_frob_symbol (symbolS * symp,
29583                     int *     puntp)
29584 {
29585   elf_frob_symbol (symp, puntp);
29586 }
29587 #endif
29588
29589 /* MD interface: Finalization.  */
29590
29591 void
29592 arm_cleanup (void)
29593 {
29594   literal_pool * pool;
29595
29596   /* Ensure that all the predication blocks are properly closed.  */
29597   check_pred_blocks_finished ();
29598
29599   for (pool = list_of_pools; pool; pool = pool->next)
29600     {
29601       /* Put it at the end of the relevant section.  */
29602       subseg_set (pool->section, pool->sub_section);
29603 #ifdef OBJ_ELF
29604       arm_elf_change_section ();
29605 #endif
29606       s_ltorg (0);
29607     }
29608 }
29609
29610 #ifdef OBJ_ELF
29611 /* Remove any excess mapping symbols generated for alignment frags in
29612    SEC.  We may have created a mapping symbol before a zero byte
29613    alignment; remove it if there's a mapping symbol after the
29614    alignment.  */
29615 static void
29616 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
29617                        void *dummy ATTRIBUTE_UNUSED)
29618 {
29619   segment_info_type *seginfo = seg_info (sec);
29620   fragS *fragp;
29621
29622   if (seginfo == NULL || seginfo->frchainP == NULL)
29623     return;
29624
29625   for (fragp = seginfo->frchainP->frch_root;
29626        fragp != NULL;
29627        fragp = fragp->fr_next)
29628     {
29629       symbolS *sym = fragp->tc_frag_data.last_map;
29630       fragS *next = fragp->fr_next;
29631
29632       /* Variable-sized frags have been converted to fixed size by
29633          this point.  But if this was variable-sized to start with,
29634          there will be a fixed-size frag after it.  So don't handle
29635          next == NULL.  */
29636       if (sym == NULL || next == NULL)
29637         continue;
29638
29639       if (S_GET_VALUE (sym) < next->fr_address)
29640         /* Not at the end of this frag.  */
29641         continue;
29642       know (S_GET_VALUE (sym) == next->fr_address);
29643
29644       do
29645         {
29646           if (next->tc_frag_data.first_map != NULL)
29647             {
29648               /* Next frag starts with a mapping symbol.  Discard this
29649                  one.  */
29650               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
29651               break;
29652             }
29653
29654           if (next->fr_next == NULL)
29655             {
29656               /* This mapping symbol is at the end of the section.  Discard
29657                  it.  */
29658               know (next->fr_fix == 0 && next->fr_var == 0);
29659               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
29660               break;
29661             }
29662
29663           /* As long as we have empty frags without any mapping symbols,
29664              keep looking.  */
29665           /* If the next frag is non-empty and does not start with a
29666              mapping symbol, then this mapping symbol is required.  */
29667           if (next->fr_address != next->fr_next->fr_address)
29668             break;
29669
29670           next = next->fr_next;
29671         }
29672       while (next != NULL);
29673     }
29674 }
29675 #endif
29676
29677 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
29678    ARM ones.  */
29679
29680 void
29681 arm_adjust_symtab (void)
29682 {
29683 #ifdef OBJ_COFF
29684   symbolS * sym;
29685
29686   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
29687     {
29688       if (ARM_IS_THUMB (sym))
29689         {
29690           if (THUMB_IS_FUNC (sym))
29691             {
29692               /* Mark the symbol as a Thumb function.  */
29693               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
29694                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
29695                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
29696
29697               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
29698                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
29699               else
29700                 as_bad (_("%s: unexpected function type: %d"),
29701                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
29702             }
29703           else switch (S_GET_STORAGE_CLASS (sym))
29704             {
29705             case C_EXT:
29706               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
29707               break;
29708             case C_STAT:
29709               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
29710               break;
29711             case C_LABEL:
29712               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
29713               break;
29714             default:
29715               /* Do nothing.  */
29716               break;
29717             }
29718         }
29719
29720       if (ARM_IS_INTERWORK (sym))
29721         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
29722     }
29723 #endif
29724 #ifdef OBJ_ELF
29725   symbolS * sym;
29726   char      bind;
29727
29728   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
29729     {
29730       if (ARM_IS_THUMB (sym))
29731         {
29732           elf_symbol_type * elf_sym;
29733
29734           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
29735           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
29736
29737           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
29738                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
29739             {
29740               /* If it's a .thumb_func, declare it as so,
29741                  otherwise tag label as .code 16.  */
29742               if (THUMB_IS_FUNC (sym))
29743                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
29744                                          ST_BRANCH_TO_THUMB);
29745               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
29746                 elf_sym->internal_elf_sym.st_info =
29747                   ELF_ST_INFO (bind, STT_ARM_16BIT);
29748             }
29749         }
29750     }
29751
29752   /* Remove any overlapping mapping symbols generated by alignment frags.  */
29753   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
29754   /* Now do generic ELF adjustments.  */
29755   elf_adjust_symtab ();
29756 #endif
29757 }
29758
29759 /* MD interface: Initialization.  */
29760
29761 static void
29762 set_constant_flonums (void)
29763 {
29764   int i;
29765
29766   for (i = 0; i < NUM_FLOAT_VALS; i++)
29767     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
29768       abort ();
29769 }
29770
29771 /* Auto-select Thumb mode if it's the only available instruction set for the
29772    given architecture.  */
29773
29774 static void
29775 autoselect_thumb_from_cpu_variant (void)
29776 {
29777   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
29778     opcode_select (16);
29779 }
29780
29781 void
29782 md_begin (void)
29783 {
29784   unsigned mach;
29785   unsigned int i;
29786
29787   if (   (arm_ops_hsh = hash_new ()) == NULL
29788       || (arm_cond_hsh = hash_new ()) == NULL
29789       || (arm_vcond_hsh = hash_new ()) == NULL
29790       || (arm_shift_hsh = hash_new ()) == NULL
29791       || (arm_psr_hsh = hash_new ()) == NULL
29792       || (arm_v7m_psr_hsh = hash_new ()) == NULL
29793       || (arm_reg_hsh = hash_new ()) == NULL
29794       || (arm_reloc_hsh = hash_new ()) == NULL
29795       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
29796     as_fatal (_("virtual memory exhausted"));
29797
29798   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
29799     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
29800   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
29801     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
29802   for (i = 0; i < sizeof (vconds) / sizeof (struct asm_cond); i++)
29803     hash_insert (arm_vcond_hsh, vconds[i].template_name, (void *) (vconds + i));
29804   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
29805     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
29806   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
29807     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
29808   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
29809     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
29810                  (void *) (v7m_psrs + i));
29811   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
29812     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
29813   for (i = 0;
29814        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
29815        i++)
29816     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
29817                  (void *) (barrier_opt_names + i));
29818 #ifdef OBJ_ELF
29819   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
29820     {
29821       struct reloc_entry * entry = reloc_names + i;
29822
29823       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
29824         /* This makes encode_branch() use the EABI versions of this relocation.  */
29825         entry->reloc = BFD_RELOC_UNUSED;
29826
29827       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
29828     }
29829 #endif
29830
29831   set_constant_flonums ();
29832
29833   /* Set the cpu variant based on the command-line options.  We prefer
29834      -mcpu= over -march= if both are set (as for GCC); and we prefer
29835      -mfpu= over any other way of setting the floating point unit.
29836      Use of legacy options with new options are faulted.  */
29837   if (legacy_cpu)
29838     {
29839       if (mcpu_cpu_opt || march_cpu_opt)
29840         as_bad (_("use of old and new-style options to set CPU type"));
29841
29842       selected_arch = *legacy_cpu;
29843     }
29844   else if (mcpu_cpu_opt)
29845     {
29846       selected_arch = *mcpu_cpu_opt;
29847       selected_ext = *mcpu_ext_opt;
29848     }
29849   else if (march_cpu_opt)
29850     {
29851       selected_arch = *march_cpu_opt;
29852       selected_ext = *march_ext_opt;
29853     }
29854   ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
29855
29856   if (legacy_fpu)
29857     {
29858       if (mfpu_opt)
29859         as_bad (_("use of old and new-style options to set FPU type"));
29860
29861       selected_fpu = *legacy_fpu;
29862     }
29863   else if (mfpu_opt)
29864     selected_fpu = *mfpu_opt;
29865   else
29866     {
29867 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
29868         || defined (TE_NetBSD) || defined (TE_VXWORKS))
29869       /* Some environments specify a default FPU.  If they don't, infer it
29870          from the processor.  */
29871       if (mcpu_fpu_opt)
29872         selected_fpu = *mcpu_fpu_opt;
29873       else if (march_fpu_opt)
29874         selected_fpu = *march_fpu_opt;
29875 #else
29876       selected_fpu = fpu_default;
29877 #endif
29878     }
29879
29880   if (ARM_FEATURE_ZERO (selected_fpu))
29881     {
29882       if (!no_cpu_selected ())
29883         selected_fpu = fpu_default;
29884       else
29885         selected_fpu = fpu_arch_fpa;
29886     }
29887
29888 #ifdef CPU_DEFAULT
29889   if (ARM_FEATURE_ZERO (selected_arch))
29890     {
29891       selected_arch = cpu_default;
29892       selected_cpu = selected_arch;
29893     }
29894   ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29895 #else
29896   /*  Autodection of feature mode: allow all features in cpu_variant but leave
29897       selected_cpu unset.  It will be set in aeabi_set_public_attributes ()
29898       after all instruction have been processed and we can decide what CPU
29899       should be selected.  */
29900   if (ARM_FEATURE_ZERO (selected_arch))
29901     ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
29902   else
29903     ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29904 #endif
29905
29906   autoselect_thumb_from_cpu_variant ();
29907
29908   arm_arch_used = thumb_arch_used = arm_arch_none;
29909
29910 #if defined OBJ_COFF || defined OBJ_ELF
29911   {
29912     unsigned int flags = 0;
29913
29914 #if defined OBJ_ELF
29915     flags = meabi_flags;
29916
29917     switch (meabi_flags)
29918       {
29919       case EF_ARM_EABI_UNKNOWN:
29920 #endif
29921         /* Set the flags in the private structure.  */
29922         if (uses_apcs_26)      flags |= F_APCS26;
29923         if (support_interwork) flags |= F_INTERWORK;
29924         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
29925         if (pic_code)          flags |= F_PIC;
29926         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
29927           flags |= F_SOFT_FLOAT;
29928
29929         switch (mfloat_abi_opt)
29930           {
29931           case ARM_FLOAT_ABI_SOFT:
29932           case ARM_FLOAT_ABI_SOFTFP:
29933             flags |= F_SOFT_FLOAT;
29934             break;
29935
29936           case ARM_FLOAT_ABI_HARD:
29937             if (flags & F_SOFT_FLOAT)
29938               as_bad (_("hard-float conflicts with specified fpu"));
29939             break;
29940           }
29941
29942         /* Using pure-endian doubles (even if soft-float).      */
29943         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
29944           flags |= F_VFP_FLOAT;
29945
29946 #if defined OBJ_ELF
29947         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
29948             flags |= EF_ARM_MAVERICK_FLOAT;
29949         break;
29950
29951       case EF_ARM_EABI_VER4:
29952       case EF_ARM_EABI_VER5:
29953         /* No additional flags to set.  */
29954         break;
29955
29956       default:
29957         abort ();
29958       }
29959 #endif
29960     bfd_set_private_flags (stdoutput, flags);
29961
29962     /* We have run out flags in the COFF header to encode the
29963        status of ATPCS support, so instead we create a dummy,
29964        empty, debug section called .arm.atpcs.  */
29965     if (atpcs)
29966       {
29967         asection * sec;
29968
29969         sec = bfd_make_section (stdoutput, ".arm.atpcs");
29970
29971         if (sec != NULL)
29972           {
29973             bfd_set_section_flags
29974               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
29975             bfd_set_section_size (stdoutput, sec, 0);
29976             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
29977           }
29978       }
29979   }
29980 #endif
29981
29982   /* Record the CPU type as well.  */
29983   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
29984     mach = bfd_mach_arm_iWMMXt2;
29985   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
29986     mach = bfd_mach_arm_iWMMXt;
29987   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
29988     mach = bfd_mach_arm_XScale;
29989   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
29990     mach = bfd_mach_arm_ep9312;
29991   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
29992     mach = bfd_mach_arm_5TE;
29993   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
29994     {
29995       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
29996         mach = bfd_mach_arm_5T;
29997       else
29998         mach = bfd_mach_arm_5;
29999     }
30000   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
30001     {
30002       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
30003         mach = bfd_mach_arm_4T;
30004       else
30005         mach = bfd_mach_arm_4;
30006     }
30007   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
30008     mach = bfd_mach_arm_3M;
30009   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
30010     mach = bfd_mach_arm_3;
30011   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
30012     mach = bfd_mach_arm_2a;
30013   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
30014     mach = bfd_mach_arm_2;
30015   else
30016     mach = bfd_mach_arm_unknown;
30017
30018   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
30019 }
30020
30021 /* Command line processing.  */
30022
30023 /* md_parse_option
30024       Invocation line includes a switch not recognized by the base assembler.
30025       See if it's a processor-specific option.
30026
30027       This routine is somewhat complicated by the need for backwards
30028       compatibility (since older releases of gcc can't be changed).
30029       The new options try to make the interface as compatible as
30030       possible with GCC.
30031
30032       New options (supported) are:
30033
30034               -mcpu=<cpu name>           Assemble for selected processor
30035               -march=<architecture name> Assemble for selected architecture
30036               -mfpu=<fpu architecture>   Assemble for selected FPU.
30037               -EB/-mbig-endian           Big-endian
30038               -EL/-mlittle-endian        Little-endian
30039               -k                         Generate PIC code
30040               -mthumb                    Start in Thumb mode
30041               -mthumb-interwork          Code supports ARM/Thumb interworking
30042
30043               -m[no-]warn-deprecated     Warn about deprecated features
30044               -m[no-]warn-syms           Warn when symbols match instructions
30045
30046       For now we will also provide support for:
30047
30048               -mapcs-32                  32-bit Program counter
30049               -mapcs-26                  26-bit Program counter
30050               -macps-float               Floats passed in FP registers
30051               -mapcs-reentrant           Reentrant code
30052               -matpcs
30053       (sometime these will probably be replaced with -mapcs=<list of options>
30054       and -matpcs=<list of options>)
30055
30056       The remaining options are only supported for back-wards compatibility.
30057       Cpu variants, the arm part is optional:
30058               -m[arm]1                Currently not supported.
30059               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
30060               -m[arm]3                Arm 3 processor
30061               -m[arm]6[xx],           Arm 6 processors
30062               -m[arm]7[xx][t][[d]m]   Arm 7 processors
30063               -m[arm]8[10]            Arm 8 processors
30064               -m[arm]9[20][tdmi]      Arm 9 processors
30065               -mstrongarm[110[0]]     StrongARM processors
30066               -mxscale                XScale processors
30067               -m[arm]v[2345[t[e]]]    Arm architectures
30068               -mall                   All (except the ARM1)
30069       FP variants:
30070               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
30071               -mfpe-old               (No float load/store multiples)
30072               -mvfpxd                 VFP Single precision
30073               -mvfp                   All VFP
30074               -mno-fpu                Disable all floating point instructions
30075
30076       The following CPU names are recognized:
30077               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
30078               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
30079               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
30080               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
30081               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
30082               arm10t arm10e, arm1020t, arm1020e, arm10200e,
30083               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
30084
30085       */
30086
30087 const char * md_shortopts = "m:k";
30088
30089 #ifdef ARM_BI_ENDIAN
30090 #define OPTION_EB (OPTION_MD_BASE + 0)
30091 #define OPTION_EL (OPTION_MD_BASE + 1)
30092 #else
30093 #if TARGET_BYTES_BIG_ENDIAN
30094 #define OPTION_EB (OPTION_MD_BASE + 0)
30095 #else
30096 #define OPTION_EL (OPTION_MD_BASE + 1)
30097 #endif
30098 #endif
30099 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
30100 #define OPTION_FDPIC (OPTION_MD_BASE + 3)
30101
30102 struct option md_longopts[] =
30103 {
30104 #ifdef OPTION_EB
30105   {"EB", no_argument, NULL, OPTION_EB},
30106 #endif
30107 #ifdef OPTION_EL
30108   {"EL", no_argument, NULL, OPTION_EL},
30109 #endif
30110   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
30111 #ifdef OBJ_ELF
30112   {"fdpic", no_argument, NULL, OPTION_FDPIC},
30113 #endif
30114   {NULL, no_argument, NULL, 0}
30115 };
30116
30117 size_t md_longopts_size = sizeof (md_longopts);
30118
30119 struct arm_option_table
30120 {
30121   const char *  option;         /* Option name to match.  */
30122   const char *  help;           /* Help information.  */
30123   int *         var;            /* Variable to change.  */
30124   int           value;          /* What to change it to.  */
30125   const char *  deprecated;     /* If non-null, print this message.  */
30126 };
30127
30128 struct arm_option_table arm_opts[] =
30129 {
30130   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
30131   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
30132   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
30133    &support_interwork, 1, NULL},
30134   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
30135   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
30136   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
30137    1, NULL},
30138   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
30139   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
30140   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
30141   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
30142    NULL},
30143
30144   /* These are recognized by the assembler, but have no affect on code.  */
30145   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
30146   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
30147
30148   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
30149   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
30150    &warn_on_deprecated, 0, NULL},
30151   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
30152   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
30153   {NULL, NULL, NULL, 0, NULL}
30154 };
30155
30156 struct arm_legacy_option_table
30157 {
30158   const char *              option;             /* Option name to match.  */
30159   const arm_feature_set **  var;                /* Variable to change.  */
30160   const arm_feature_set     value;              /* What to change it to.  */
30161   const char *              deprecated;         /* If non-null, print this message.  */
30162 };
30163
30164 const struct arm_legacy_option_table arm_legacy_opts[] =
30165 {
30166   /* DON'T add any new processors to this list -- we want the whole list
30167      to go away...  Add them to the processors table instead.  */
30168   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
30169   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
30170   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
30171   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
30172   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
30173   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
30174   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
30175   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
30176   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
30177   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
30178   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
30179   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
30180   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
30181   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
30182   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
30183   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
30184   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
30185   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
30186   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
30187   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
30188   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
30189   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
30190   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
30191   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
30192   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
30193   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
30194   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
30195   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
30196   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
30197   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
30198   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
30199   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
30200   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
30201   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
30202   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
30203   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
30204   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
30205   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
30206   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
30207   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
30208   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
30209   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
30210   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
30211   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
30212   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
30213   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
30214   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30215   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30216   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30217   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
30218   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
30219   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
30220   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
30221   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
30222   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
30223   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
30224   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
30225   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
30226   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
30227   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
30228   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
30229   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
30230   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
30231   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
30232   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
30233   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
30234   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
30235   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
30236   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
30237   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
30238    N_("use -mcpu=strongarm110")},
30239   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
30240    N_("use -mcpu=strongarm1100")},
30241   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
30242    N_("use -mcpu=strongarm1110")},
30243   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
30244   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
30245   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
30246
30247   /* Architecture variants -- don't add any more to this list either.  */
30248   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
30249   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
30250   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
30251   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
30252   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
30253   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
30254   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
30255   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
30256   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
30257   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
30258   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
30259   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
30260   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
30261   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
30262   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
30263   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
30264   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
30265   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
30266
30267   /* Floating point variants -- don't add any more to this list either.  */
30268   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
30269   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
30270   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
30271   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
30272    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
30273
30274   {NULL, NULL, ARM_ARCH_NONE, NULL}
30275 };
30276
30277 struct arm_cpu_option_table
30278 {
30279   const char *           name;
30280   size_t                 name_len;
30281   const arm_feature_set  value;
30282   const arm_feature_set  ext;
30283   /* For some CPUs we assume an FPU unless the user explicitly sets
30284      -mfpu=...  */
30285   const arm_feature_set  default_fpu;
30286   /* The canonical name of the CPU, or NULL to use NAME converted to upper
30287      case.  */
30288   const char *           canonical_name;
30289 };
30290
30291 /* This list should, at a minimum, contain all the cpu names
30292    recognized by GCC.  */
30293 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
30294
30295 static const struct arm_cpu_option_table arm_cpus[] =
30296 {
30297   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
30298                ARM_ARCH_NONE,
30299                FPU_ARCH_FPA),
30300   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
30301                ARM_ARCH_NONE,
30302                FPU_ARCH_FPA),
30303   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
30304                ARM_ARCH_NONE,
30305                FPU_ARCH_FPA),
30306   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
30307                ARM_ARCH_NONE,
30308                FPU_ARCH_FPA),
30309   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
30310                ARM_ARCH_NONE,
30311                FPU_ARCH_FPA),
30312   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
30313                ARM_ARCH_NONE,
30314                FPU_ARCH_FPA),
30315   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
30316                ARM_ARCH_NONE,
30317                FPU_ARCH_FPA),
30318   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
30319                ARM_ARCH_NONE,
30320                FPU_ARCH_FPA),
30321   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
30322                ARM_ARCH_NONE,
30323                FPU_ARCH_FPA),
30324   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
30325                ARM_ARCH_NONE,
30326                FPU_ARCH_FPA),
30327   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
30328                ARM_ARCH_NONE,
30329                FPU_ARCH_FPA),
30330   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
30331                ARM_ARCH_NONE,
30332                FPU_ARCH_FPA),
30333   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
30334                ARM_ARCH_NONE,
30335                FPU_ARCH_FPA),
30336   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
30337                ARM_ARCH_NONE,
30338                FPU_ARCH_FPA),
30339   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
30340                ARM_ARCH_NONE,
30341                FPU_ARCH_FPA),
30342   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
30343                ARM_ARCH_NONE,
30344                FPU_ARCH_FPA),
30345   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
30346                ARM_ARCH_NONE,
30347                FPU_ARCH_FPA),
30348   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
30349                ARM_ARCH_NONE,
30350                FPU_ARCH_FPA),
30351   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
30352                ARM_ARCH_NONE,
30353                FPU_ARCH_FPA),
30354   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
30355                ARM_ARCH_NONE,
30356                FPU_ARCH_FPA),
30357   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
30358                ARM_ARCH_NONE,
30359                FPU_ARCH_FPA),
30360   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
30361                ARM_ARCH_NONE,
30362                FPU_ARCH_FPA),
30363   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
30364                ARM_ARCH_NONE,
30365                FPU_ARCH_FPA),
30366   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
30367                ARM_ARCH_NONE,
30368                FPU_ARCH_FPA),
30369   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
30370                ARM_ARCH_NONE,
30371                FPU_ARCH_FPA),
30372   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
30373                ARM_ARCH_NONE,
30374                FPU_ARCH_FPA),
30375   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
30376                ARM_ARCH_NONE,
30377                FPU_ARCH_FPA),
30378   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
30379                ARM_ARCH_NONE,
30380                FPU_ARCH_FPA),
30381   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
30382                ARM_ARCH_NONE,
30383                FPU_ARCH_FPA),
30384   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
30385                ARM_ARCH_NONE,
30386                FPU_ARCH_FPA),
30387   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
30388                ARM_ARCH_NONE,
30389                FPU_ARCH_FPA),
30390   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
30391                ARM_ARCH_NONE,
30392                FPU_ARCH_FPA),
30393   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
30394                ARM_ARCH_NONE,
30395                FPU_ARCH_FPA),
30396   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
30397                ARM_ARCH_NONE,
30398                FPU_ARCH_FPA),
30399   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
30400                ARM_ARCH_NONE,
30401                FPU_ARCH_FPA),
30402   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
30403                ARM_ARCH_NONE,
30404                FPU_ARCH_FPA),
30405   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
30406                ARM_ARCH_NONE,
30407                FPU_ARCH_FPA),
30408   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
30409                ARM_ARCH_NONE,
30410                FPU_ARCH_FPA),
30411   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
30412                ARM_ARCH_NONE,
30413                FPU_ARCH_FPA),
30414   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
30415                ARM_ARCH_NONE,
30416                FPU_ARCH_FPA),
30417   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
30418                ARM_ARCH_NONE,
30419                FPU_ARCH_FPA),
30420   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
30421                ARM_ARCH_NONE,
30422                FPU_ARCH_FPA),
30423   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
30424                ARM_ARCH_NONE,
30425                FPU_ARCH_FPA),
30426   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
30427                ARM_ARCH_NONE,
30428                FPU_ARCH_FPA),
30429   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
30430                ARM_ARCH_NONE,
30431                FPU_ARCH_FPA),
30432   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
30433                ARM_ARCH_NONE,
30434                FPU_ARCH_FPA),
30435
30436   /* For V5 or later processors we default to using VFP; but the user
30437      should really set the FPU type explicitly.  */
30438   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
30439                ARM_ARCH_NONE,
30440                FPU_ARCH_VFP_V2),
30441   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
30442                ARM_ARCH_NONE,
30443                FPU_ARCH_VFP_V2),
30444   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
30445                ARM_ARCH_NONE,
30446                FPU_ARCH_VFP_V2),
30447   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
30448                ARM_ARCH_NONE,
30449                FPU_ARCH_VFP_V2),
30450   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
30451                ARM_ARCH_NONE,
30452                FPU_ARCH_VFP_V2),
30453   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
30454                ARM_ARCH_NONE,
30455                FPU_ARCH_VFP_V2),
30456   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
30457                ARM_ARCH_NONE,
30458                FPU_ARCH_VFP_V2),
30459   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
30460                ARM_ARCH_NONE,
30461                FPU_ARCH_VFP_V2),
30462   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
30463                ARM_ARCH_NONE,
30464                FPU_ARCH_VFP_V2),
30465   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
30466                ARM_ARCH_NONE,
30467                FPU_ARCH_VFP_V2),
30468   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
30469                ARM_ARCH_NONE,
30470                FPU_ARCH_VFP_V2),
30471   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
30472                ARM_ARCH_NONE,
30473                FPU_ARCH_VFP_V2),
30474   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
30475                ARM_ARCH_NONE,
30476                FPU_ARCH_VFP_V1),
30477   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
30478                ARM_ARCH_NONE,
30479                FPU_ARCH_VFP_V1),
30480   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
30481                ARM_ARCH_NONE,
30482                FPU_ARCH_VFP_V2),
30483   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
30484                ARM_ARCH_NONE,
30485                FPU_ARCH_VFP_V2),
30486   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
30487                ARM_ARCH_NONE,
30488                FPU_ARCH_VFP_V1),
30489   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
30490                ARM_ARCH_NONE,
30491                FPU_ARCH_VFP_V2),
30492   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
30493                ARM_ARCH_NONE,
30494                FPU_ARCH_VFP_V2),
30495   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
30496                ARM_ARCH_NONE,
30497                FPU_ARCH_VFP_V2),
30498   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
30499                ARM_ARCH_NONE,
30500                FPU_ARCH_VFP_V2),
30501   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
30502                ARM_ARCH_NONE,
30503                FPU_ARCH_VFP_V2),
30504   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
30505                ARM_ARCH_NONE,
30506                FPU_ARCH_VFP_V2),
30507   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
30508                ARM_ARCH_NONE,
30509                FPU_ARCH_VFP_V2),
30510   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
30511                ARM_ARCH_NONE,
30512                FPU_ARCH_VFP_V2),
30513   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
30514                ARM_ARCH_NONE,
30515                FPU_ARCH_VFP_V2),
30516   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
30517                ARM_ARCH_NONE,
30518                FPU_NONE),
30519   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
30520                ARM_ARCH_NONE,
30521                FPU_NONE),
30522   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
30523                ARM_ARCH_NONE,
30524                FPU_ARCH_VFP_V2),
30525   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
30526                ARM_ARCH_NONE,
30527                FPU_ARCH_VFP_V2),
30528   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
30529                ARM_ARCH_NONE,
30530                FPU_ARCH_VFP_V2),
30531   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
30532                ARM_ARCH_NONE,
30533                FPU_NONE),
30534   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
30535                ARM_ARCH_NONE,
30536                FPU_NONE),
30537   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
30538                ARM_ARCH_NONE,
30539                FPU_ARCH_VFP_V2),
30540   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
30541                ARM_ARCH_NONE,
30542                FPU_NONE),
30543   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
30544                ARM_ARCH_NONE,
30545                FPU_ARCH_VFP_V2),
30546   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
30547                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30548                FPU_NONE),
30549   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
30550                ARM_ARCH_NONE,
30551                FPU_ARCH_NEON_VFP_V4),
30552   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
30553                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
30554                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
30555   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
30556                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30557                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
30558   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
30559                ARM_ARCH_NONE,
30560                FPU_ARCH_NEON_VFP_V4),
30561   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
30562                ARM_ARCH_NONE,
30563                FPU_ARCH_NEON_VFP_V4),
30564   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
30565                ARM_ARCH_NONE,
30566                FPU_ARCH_NEON_VFP_V4),
30567   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
30568                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30569                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30570   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
30571                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30572                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30573   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
30574                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30575                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30576   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
30577                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30578                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30579   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
30580                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30581                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30582   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
30583               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30584               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30585   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
30586               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30587               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30588   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
30589                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30590                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30591   ARM_CPU_OPT ("cortex-a76",    "Cortex-A76",          ARM_ARCH_V8_2A,
30592                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30593                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30594   ARM_CPU_OPT ("cortex-a76ae",    "Cortex-A76AE",      ARM_ARCH_V8_2A,
30595                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30596                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30597   ARM_CPU_OPT ("cortex-a77",    "Cortex-A77",          ARM_ARCH_V8_2A,
30598                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30599                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30600   ARM_CPU_OPT ("ares",    "Ares",              ARM_ARCH_V8_2A,
30601                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30602                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30603   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
30604                ARM_ARCH_NONE,
30605                FPU_NONE),
30606   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
30607                ARM_ARCH_NONE,
30608                FPU_ARCH_VFP_V3D16),
30609   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
30610                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30611                FPU_NONE),
30612   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
30613                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30614                FPU_ARCH_VFP_V3D16),
30615   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
30616                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
30617                FPU_ARCH_VFP_V3D16),
30618   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
30619               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30620               FPU_ARCH_NEON_VFP_ARMV8),
30621   ARM_CPU_OPT ("cortex-m35p",     "Cortex-M35P",       ARM_ARCH_V8M_MAIN,
30622                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30623                FPU_NONE),
30624   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
30625                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30626                FPU_NONE),
30627   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
30628                ARM_ARCH_NONE,
30629                FPU_NONE),
30630   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
30631                ARM_ARCH_NONE,
30632                FPU_NONE),
30633   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
30634                ARM_ARCH_NONE,
30635                FPU_NONE),
30636   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
30637                ARM_ARCH_NONE,
30638                FPU_NONE),
30639   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
30640                ARM_ARCH_NONE,
30641                FPU_NONE),
30642   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
30643                ARM_ARCH_NONE,
30644                FPU_NONE),
30645   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
30646                ARM_ARCH_NONE,
30647                FPU_NONE),
30648   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
30649                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30650                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30651   ARM_CPU_OPT ("neoverse-n1",    "Neoverse N1",        ARM_ARCH_V8_2A,
30652                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
30653                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
30654   /* ??? XSCALE is really an architecture.  */
30655   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
30656                ARM_ARCH_NONE,
30657                FPU_ARCH_VFP_V2),
30658
30659   /* ??? iwmmxt is not a processor.  */
30660   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
30661                ARM_ARCH_NONE,
30662                FPU_ARCH_VFP_V2),
30663   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
30664                ARM_ARCH_NONE,
30665                FPU_ARCH_VFP_V2),
30666   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
30667                ARM_ARCH_NONE,
30668                FPU_ARCH_VFP_V2),
30669
30670   /* Maverick.  */
30671   ARM_CPU_OPT ("ep9312",          "ARM920T",
30672                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
30673                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
30674
30675   /* Marvell processors.  */
30676   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
30677                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30678                FPU_ARCH_VFP_V3D16),
30679   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
30680                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
30681                FPU_ARCH_NEON_VFP_V4),
30682
30683   /* APM X-Gene family.  */
30684   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
30685                ARM_ARCH_NONE,
30686                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30687   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
30688                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
30689                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
30690
30691   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
30692 };
30693 #undef ARM_CPU_OPT
30694
30695 struct arm_ext_table
30696 {
30697   const char *            name;
30698   size_t                  name_len;
30699   const arm_feature_set   merge;
30700   const arm_feature_set   clear;
30701 };
30702
30703 struct arm_arch_option_table
30704 {
30705   const char *                  name;
30706   size_t                        name_len;
30707   const arm_feature_set         value;
30708   const arm_feature_set         default_fpu;
30709   const struct arm_ext_table *  ext_table;
30710 };
30711
30712 /* Used to add support for +E and +noE extension.  */
30713 #define ARM_EXT(E, M, C) { E, sizeof (E) - 1, M, C }
30714 /* Used to add support for a +E extension.  */
30715 #define ARM_ADD(E, M) { E, sizeof(E) - 1, M, ARM_ARCH_NONE }
30716 /* Used to add support for a +noE extension.  */
30717 #define ARM_REMOVE(E, C) { E, sizeof(E) -1, ARM_ARCH_NONE, C }
30718
30719 #define ALL_FP ARM_FEATURE (0, ARM_EXT2_FP16_INST | ARM_EXT2_FP16_FML, \
30720                             ~0 & ~FPU_ENDIAN_PURE)
30721
30722 static const struct arm_ext_table armv5te_ext_table[] =
30723 {
30724   ARM_EXT ("fp", FPU_ARCH_VFP_V2, ALL_FP),
30725   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30726 };
30727
30728 static const struct arm_ext_table armv7_ext_table[] =
30729 {
30730   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30731   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30732 };
30733
30734 static const struct arm_ext_table armv7ve_ext_table[] =
30735 {
30736   ARM_EXT ("fp", FPU_ARCH_VFP_V4D16, ALL_FP),
30737   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16),
30738   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
30739   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30740   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
30741   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),  /* Alias for +fp.  */
30742   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
30743
30744   ARM_EXT ("simd", FPU_ARCH_NEON_VFP_V4,
30745            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
30746
30747   /* Aliases for +simd.  */
30748   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
30749
30750   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30751   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30752   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
30753
30754   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30755 };
30756
30757 static const struct arm_ext_table armv7a_ext_table[] =
30758 {
30759   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30760   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
30761   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
30762   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30763   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
30764   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),
30765   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
30766
30767   ARM_EXT ("simd", FPU_ARCH_VFP_V3_PLUS_NEON_V1,
30768            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
30769
30770   /* Aliases for +simd.  */
30771   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30772   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
30773
30774   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
30775   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
30776
30777   ARM_ADD ("mp", ARM_FEATURE_CORE_LOW (ARM_EXT_MP)),
30778   ARM_ADD ("sec", ARM_FEATURE_CORE_LOW (ARM_EXT_SEC)),
30779   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30780 };
30781
30782 static const struct arm_ext_table armv7r_ext_table[] =
30783 {
30784   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V3xD),
30785   ARM_ADD ("vfpv3xd", FPU_ARCH_VFP_V3xD), /* Alias for +fp.sp.  */
30786   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
30787   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
30788   ARM_ADD ("vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16),
30789   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
30790   ARM_EXT ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
30791            ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV)),
30792   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30793 };
30794
30795 static const struct arm_ext_table armv7em_ext_table[] =
30796 {
30797   ARM_EXT ("fp", FPU_ARCH_VFP_V4_SP_D16, ALL_FP),
30798   /* Alias for +fp, used to be known as fpv4-sp-d16.  */
30799   ARM_ADD ("vfpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16),
30800   ARM_ADD ("fpv5", FPU_ARCH_VFP_V5_SP_D16),
30801   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
30802   ARM_ADD ("fpv5-d16", FPU_ARCH_VFP_V5D16),
30803   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30804 };
30805
30806 static const struct arm_ext_table armv8a_ext_table[] =
30807 {
30808   ARM_ADD ("crc", ARCH_CRC_ARMV8),
30809   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
30810   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30811            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30812
30813   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30814      should use the +simd option to turn on FP.  */
30815   ARM_REMOVE ("fp", ALL_FP),
30816   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30817   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30818   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30819 };
30820
30821
30822 static const struct arm_ext_table armv81a_ext_table[] =
30823 {
30824   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
30825   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
30826            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30827
30828   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30829      should use the +simd option to turn on FP.  */
30830   ARM_REMOVE ("fp", ALL_FP),
30831   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30832   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30833   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30834 };
30835
30836 static const struct arm_ext_table armv82a_ext_table[] =
30837 {
30838   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
30839   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_2_FP16),
30840   ARM_ADD ("fp16fml", FPU_ARCH_NEON_VFP_ARMV8_2_FP16FML),
30841   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
30842            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30843   ARM_ADD ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30844
30845   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30846      should use the +simd option to turn on FP.  */
30847   ARM_REMOVE ("fp", ALL_FP),
30848   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30849   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30850   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30851 };
30852
30853 static const struct arm_ext_table armv84a_ext_table[] =
30854 {
30855   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30856   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
30857   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
30858            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30859
30860   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30861      should use the +simd option to turn on FP.  */
30862   ARM_REMOVE ("fp", ALL_FP),
30863   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
30864   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
30865   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30866 };
30867
30868 static const struct arm_ext_table armv85a_ext_table[] =
30869 {
30870   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
30871   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
30872   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
30873            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30874
30875   /* Armv8-a does not allow an FP implementation without SIMD, so the user
30876      should use the +simd option to turn on FP.  */
30877   ARM_REMOVE ("fp", ALL_FP),
30878   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30879 };
30880
30881 static const struct arm_ext_table armv8m_main_ext_table[] =
30882 {
30883   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30884                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
30885   ARM_EXT ("fp", FPU_ARCH_VFP_V5_SP_D16, ALL_FP),
30886   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
30887   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30888 };
30889
30890 static const struct arm_ext_table armv8_1m_main_ext_table[] =
30891 {
30892   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
30893                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
30894   ARM_EXT ("fp",
30895            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30896                         FPU_VFP_V5_SP_D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA),
30897            ALL_FP),
30898   ARM_ADD ("fp.dp",
30899            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30900                         FPU_VFP_V5D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
30901   ARM_EXT ("mve", ARM_FEATURE_COPROC (FPU_MVE),
30902            ARM_FEATURE_COPROC (FPU_MVE | FPU_MVE_FP)),
30903   ARM_ADD ("mve.fp",
30904            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
30905                         FPU_MVE | FPU_MVE_FP | FPU_VFP_V5_SP_D16 |
30906                         FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
30907   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30908 };
30909
30910 static const struct arm_ext_table armv8r_ext_table[] =
30911 {
30912   ARM_ADD ("crc", ARCH_CRC_ARMV8),
30913   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
30914   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
30915            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
30916   ARM_REMOVE ("fp", ALL_FP),
30917   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V5_SP_D16),
30918   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
30919 };
30920
30921 /* This list should, at a minimum, contain all the architecture names
30922    recognized by GCC.  */
30923 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF, NULL }
30924 #define ARM_ARCH_OPT2(N, V, DF, ext) \
30925   { N, sizeof (N) - 1, V, DF, ext##_ext_table }
30926
30927 static const struct arm_arch_option_table arm_archs[] =
30928 {
30929   ARM_ARCH_OPT ("all",            ARM_ANY,              FPU_ARCH_FPA),
30930   ARM_ARCH_OPT ("armv1",          ARM_ARCH_V1,          FPU_ARCH_FPA),
30931   ARM_ARCH_OPT ("armv2",          ARM_ARCH_V2,          FPU_ARCH_FPA),
30932   ARM_ARCH_OPT ("armv2a",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
30933   ARM_ARCH_OPT ("armv2s",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
30934   ARM_ARCH_OPT ("armv3",          ARM_ARCH_V3,          FPU_ARCH_FPA),
30935   ARM_ARCH_OPT ("armv3m",         ARM_ARCH_V3M,         FPU_ARCH_FPA),
30936   ARM_ARCH_OPT ("armv4",          ARM_ARCH_V4,          FPU_ARCH_FPA),
30937   ARM_ARCH_OPT ("armv4xm",        ARM_ARCH_V4xM,        FPU_ARCH_FPA),
30938   ARM_ARCH_OPT ("armv4t",         ARM_ARCH_V4T,         FPU_ARCH_FPA),
30939   ARM_ARCH_OPT ("armv4txm",       ARM_ARCH_V4TxM,       FPU_ARCH_FPA),
30940   ARM_ARCH_OPT ("armv5",          ARM_ARCH_V5,          FPU_ARCH_VFP),
30941   ARM_ARCH_OPT ("armv5t",         ARM_ARCH_V5T,         FPU_ARCH_VFP),
30942   ARM_ARCH_OPT ("armv5txm",       ARM_ARCH_V5TxM,       FPU_ARCH_VFP),
30943   ARM_ARCH_OPT2 ("armv5te",       ARM_ARCH_V5TE,        FPU_ARCH_VFP,   armv5te),
30944   ARM_ARCH_OPT2 ("armv5texp",     ARM_ARCH_V5TExP,      FPU_ARCH_VFP, armv5te),
30945   ARM_ARCH_OPT2 ("armv5tej",      ARM_ARCH_V5TEJ,       FPU_ARCH_VFP,   armv5te),
30946   ARM_ARCH_OPT2 ("armv6",         ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
30947   ARM_ARCH_OPT2 ("armv6j",        ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
30948   ARM_ARCH_OPT2 ("armv6k",        ARM_ARCH_V6K,         FPU_ARCH_VFP,   armv5te),
30949   ARM_ARCH_OPT2 ("armv6z",        ARM_ARCH_V6Z,         FPU_ARCH_VFP,   armv5te),
30950   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
30951      kept to preserve existing behaviour.  */
30952   ARM_ARCH_OPT2 ("armv6kz",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
30953   ARM_ARCH_OPT2 ("armv6zk",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
30954   ARM_ARCH_OPT2 ("armv6t2",       ARM_ARCH_V6T2,        FPU_ARCH_VFP,   armv5te),
30955   ARM_ARCH_OPT2 ("armv6kt2",      ARM_ARCH_V6KT2,       FPU_ARCH_VFP,   armv5te),
30956   ARM_ARCH_OPT2 ("armv6zt2",      ARM_ARCH_V6ZT2,       FPU_ARCH_VFP,   armv5te),
30957   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
30958      kept to preserve existing behaviour.  */
30959   ARM_ARCH_OPT2 ("armv6kzt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
30960   ARM_ARCH_OPT2 ("armv6zkt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
30961   ARM_ARCH_OPT ("armv6-m",        ARM_ARCH_V6M,         FPU_ARCH_VFP),
30962   ARM_ARCH_OPT ("armv6s-m",       ARM_ARCH_V6SM,        FPU_ARCH_VFP),
30963   ARM_ARCH_OPT2 ("armv7",         ARM_ARCH_V7,          FPU_ARCH_VFP, armv7),
30964   /* The official spelling of the ARMv7 profile variants is the dashed form.
30965      Accept the non-dashed form for compatibility with old toolchains.  */
30966   ARM_ARCH_OPT2 ("armv7a",        ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
30967   ARM_ARCH_OPT2 ("armv7ve",       ARM_ARCH_V7VE,        FPU_ARCH_VFP, armv7ve),
30968   ARM_ARCH_OPT2 ("armv7r",        ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
30969   ARM_ARCH_OPT ("armv7m",         ARM_ARCH_V7M,         FPU_ARCH_VFP),
30970   ARM_ARCH_OPT2 ("armv7-a",       ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
30971   ARM_ARCH_OPT2 ("armv7-r",       ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
30972   ARM_ARCH_OPT ("armv7-m",        ARM_ARCH_V7M,         FPU_ARCH_VFP),
30973   ARM_ARCH_OPT2 ("armv7e-m",      ARM_ARCH_V7EM,        FPU_ARCH_VFP, armv7em),
30974   ARM_ARCH_OPT ("armv8-m.base",   ARM_ARCH_V8M_BASE,    FPU_ARCH_VFP),
30975   ARM_ARCH_OPT2 ("armv8-m.main",  ARM_ARCH_V8M_MAIN,    FPU_ARCH_VFP,
30976                  armv8m_main),
30977   ARM_ARCH_OPT2 ("armv8.1-m.main", ARM_ARCH_V8_1M_MAIN, FPU_ARCH_VFP,
30978                  armv8_1m_main),
30979   ARM_ARCH_OPT2 ("armv8-a",       ARM_ARCH_V8A,         FPU_ARCH_VFP, armv8a),
30980   ARM_ARCH_OPT2 ("armv8.1-a",     ARM_ARCH_V8_1A,       FPU_ARCH_VFP, armv81a),
30981   ARM_ARCH_OPT2 ("armv8.2-a",     ARM_ARCH_V8_2A,       FPU_ARCH_VFP, armv82a),
30982   ARM_ARCH_OPT2 ("armv8.3-a",     ARM_ARCH_V8_3A,       FPU_ARCH_VFP, armv82a),
30983   ARM_ARCH_OPT2 ("armv8-r",       ARM_ARCH_V8R,         FPU_ARCH_VFP, armv8r),
30984   ARM_ARCH_OPT2 ("armv8.4-a",     ARM_ARCH_V8_4A,       FPU_ARCH_VFP, armv84a),
30985   ARM_ARCH_OPT2 ("armv8.5-a",     ARM_ARCH_V8_5A,       FPU_ARCH_VFP, armv85a),
30986   ARM_ARCH_OPT ("xscale",         ARM_ARCH_XSCALE,      FPU_ARCH_VFP),
30987   ARM_ARCH_OPT ("iwmmxt",         ARM_ARCH_IWMMXT,      FPU_ARCH_VFP),
30988   ARM_ARCH_OPT ("iwmmxt2",        ARM_ARCH_IWMMXT2,     FPU_ARCH_VFP),
30989   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
30990 };
30991 #undef ARM_ARCH_OPT
30992
30993 /* ISA extensions in the co-processor and main instruction set space.  */
30994
30995 struct arm_option_extension_value_table
30996 {
30997   const char *           name;
30998   size_t                 name_len;
30999   const arm_feature_set  merge_value;
31000   const arm_feature_set  clear_value;
31001   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
31002      indicates that an extension is available for all architectures while
31003      ARM_ANY marks an empty entry.  */
31004   const arm_feature_set  allowed_archs[2];
31005 };
31006
31007 /* The following table must be in alphabetical order with a NULL last entry.  */
31008
31009 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
31010 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
31011
31012 /* DEPRECATED: Refrain from using this table to add any new extensions, instead
31013    use the context sensitive approach using arm_ext_table's.  */
31014 static const struct arm_option_extension_value_table arm_extensions[] =
31015 {
31016   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
31017                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31018   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
31019                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
31020                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31021   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
31022                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
31023                           ARM_ARCH_V8_2A),
31024   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
31025                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
31026                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
31027   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
31028                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31029   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
31030                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
31031                         ARM_ARCH_V8_2A),
31032   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
31033                                                   | ARM_EXT2_FP16_FML),
31034                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
31035                                                   | ARM_EXT2_FP16_FML),
31036                            ARM_ARCH_V8_2A),
31037   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
31038                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
31039                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
31040                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
31041   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
31042      Thumb divide instruction.  Due to this having the same name as the
31043      previous entry, this will be ignored when doing command-line parsing and
31044      only considered by build attribute selection code.  */
31045   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
31046                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
31047                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
31048   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
31049                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
31050   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
31051                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
31052   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
31053                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
31054   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
31055                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
31056                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
31057                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
31058   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
31059                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
31060                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
31061   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
31062                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
31063                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31064   ARM_EXT_OPT ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
31065                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
31066                         ARM_ARCH_V8A),
31067   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
31068                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
31069                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31070   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
31071                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
31072                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
31073   ARM_EXT_OPT ("sb",    ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
31074                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
31075                         ARM_ARCH_V8A),
31076   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
31077                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
31078                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
31079                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
31080   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
31081                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
31082                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
31083   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
31084                                      | ARM_EXT_DIV),
31085                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
31086                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
31087   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
31088                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
31089   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
31090 };
31091 #undef ARM_EXT_OPT
31092
31093 /* ISA floating-point and Advanced SIMD extensions.  */
31094 struct arm_option_fpu_value_table
31095 {
31096   const char *           name;
31097   const arm_feature_set  value;
31098 };
31099
31100 /* This list should, at a minimum, contain all the fpu names
31101    recognized by GCC.  */
31102 static const struct arm_option_fpu_value_table arm_fpus[] =
31103 {
31104   {"softfpa",           FPU_NONE},
31105   {"fpe",               FPU_ARCH_FPE},
31106   {"fpe2",              FPU_ARCH_FPE},
31107   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
31108   {"fpa",               FPU_ARCH_FPA},
31109   {"fpa10",             FPU_ARCH_FPA},
31110   {"fpa11",             FPU_ARCH_FPA},
31111   {"arm7500fe",         FPU_ARCH_FPA},
31112   {"softvfp",           FPU_ARCH_VFP},
31113   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
31114   {"vfp",               FPU_ARCH_VFP_V2},
31115   {"vfp9",              FPU_ARCH_VFP_V2},
31116   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
31117   {"vfp10",             FPU_ARCH_VFP_V2},
31118   {"vfp10-r0",          FPU_ARCH_VFP_V1},
31119   {"vfpxd",             FPU_ARCH_VFP_V1xD},
31120   {"vfpv2",             FPU_ARCH_VFP_V2},
31121   {"vfpv3",             FPU_ARCH_VFP_V3},
31122   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
31123   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
31124   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
31125   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
31126   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
31127   {"arm1020t",          FPU_ARCH_VFP_V1},
31128   {"arm1020e",          FPU_ARCH_VFP_V2},
31129   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
31130   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
31131   {"maverick",          FPU_ARCH_MAVERICK},
31132   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
31133   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
31134   {"neon-fp16",         FPU_ARCH_NEON_FP16},
31135   {"vfpv4",             FPU_ARCH_VFP_V4},
31136   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
31137   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
31138   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
31139   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
31140   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
31141   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
31142   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
31143   {"crypto-neon-fp-armv8",
31144                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
31145   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
31146   {"crypto-neon-fp-armv8.1",
31147                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
31148   {NULL,                ARM_ARCH_NONE}
31149 };
31150
31151 struct arm_option_value_table
31152 {
31153   const char *name;
31154   long value;
31155 };
31156
31157 static const struct arm_option_value_table arm_float_abis[] =
31158 {
31159   {"hard",      ARM_FLOAT_ABI_HARD},
31160   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
31161   {"soft",      ARM_FLOAT_ABI_SOFT},
31162   {NULL,        0}
31163 };
31164
31165 #ifdef OBJ_ELF
31166 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
31167 static const struct arm_option_value_table arm_eabis[] =
31168 {
31169   {"gnu",       EF_ARM_EABI_UNKNOWN},
31170   {"4",         EF_ARM_EABI_VER4},
31171   {"5",         EF_ARM_EABI_VER5},
31172   {NULL,        0}
31173 };
31174 #endif
31175
31176 struct arm_long_option_table
31177 {
31178   const char * option;                  /* Substring to match.  */
31179   const char * help;                    /* Help information.  */
31180   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
31181   const char * deprecated;              /* If non-null, print this message.  */
31182 };
31183
31184 static bfd_boolean
31185 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
31186                      arm_feature_set *ext_set,
31187                      const struct arm_ext_table *ext_table)
31188 {
31189   /* We insist on extensions being specified in alphabetical order, and with
31190      extensions being added before being removed.  We achieve this by having
31191      the global ARM_EXTENSIONS table in alphabetical order, and using the
31192      ADDING_VALUE variable to indicate whether we are adding an extension (1)
31193      or removing it (0) and only allowing it to change in the order
31194      -1 -> 1 -> 0.  */
31195   const struct arm_option_extension_value_table * opt = NULL;
31196   const arm_feature_set arm_any = ARM_ANY;
31197   int adding_value = -1;
31198
31199   while (str != NULL && *str != 0)
31200     {
31201       const char *ext;
31202       size_t len;
31203
31204       if (*str != '+')
31205         {
31206           as_bad (_("invalid architectural extension"));
31207           return FALSE;
31208         }
31209
31210       str++;
31211       ext = strchr (str, '+');
31212
31213       if (ext != NULL)
31214         len = ext - str;
31215       else
31216         len = strlen (str);
31217
31218       if (len >= 2 && strncmp (str, "no", 2) == 0)
31219         {
31220           if (adding_value != 0)
31221             {
31222               adding_value = 0;
31223               opt = arm_extensions;
31224             }
31225
31226           len -= 2;
31227           str += 2;
31228         }
31229       else if (len > 0)
31230         {
31231           if (adding_value == -1)
31232             {
31233               adding_value = 1;
31234               opt = arm_extensions;
31235             }
31236           else if (adding_value != 1)
31237             {
31238               as_bad (_("must specify extensions to add before specifying "
31239                         "those to remove"));
31240               return FALSE;
31241             }
31242         }
31243
31244       if (len == 0)
31245         {
31246           as_bad (_("missing architectural extension"));
31247           return FALSE;
31248         }
31249
31250       gas_assert (adding_value != -1);
31251       gas_assert (opt != NULL);
31252
31253       if (ext_table != NULL)
31254         {
31255           const struct arm_ext_table * ext_opt = ext_table;
31256           bfd_boolean found = FALSE;
31257           for (; ext_opt->name != NULL; ext_opt++)
31258             if (ext_opt->name_len == len
31259                 && strncmp (ext_opt->name, str, len) == 0)
31260               {
31261                 if (adding_value)
31262                   {
31263                     if (ARM_FEATURE_ZERO (ext_opt->merge))
31264                         /* TODO: Option not supported.  When we remove the
31265                            legacy table this case should error out.  */
31266                         continue;
31267
31268                     ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, ext_opt->merge);
31269                   }
31270                 else
31271                   {
31272                     if (ARM_FEATURE_ZERO (ext_opt->clear))
31273                         /* TODO: Option not supported.  When we remove the
31274                            legacy table this case should error out.  */
31275                         continue;
31276                     ARM_CLEAR_FEATURE (*ext_set, *ext_set, ext_opt->clear);
31277                   }
31278                 found = TRUE;
31279                 break;
31280               }
31281           if (found)
31282             {
31283               str = ext;
31284               continue;
31285             }
31286         }
31287
31288       /* Scan over the options table trying to find an exact match. */
31289       for (; opt->name != NULL; opt++)
31290         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31291           {
31292             int i, nb_allowed_archs =
31293               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
31294             /* Check we can apply the extension to this architecture.  */
31295             for (i = 0; i < nb_allowed_archs; i++)
31296               {
31297                 /* Empty entry.  */
31298                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
31299                   continue;
31300                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
31301                   break;
31302               }
31303             if (i == nb_allowed_archs)
31304               {
31305                 as_bad (_("extension does not apply to the base architecture"));
31306                 return FALSE;
31307               }
31308
31309             /* Add or remove the extension.  */
31310             if (adding_value)
31311               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
31312             else
31313               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
31314
31315             /* Allowing Thumb division instructions for ARMv7 in autodetection
31316                rely on this break so that duplicate extensions (extensions
31317                with the same name as a previous extension in the list) are not
31318                considered for command-line parsing.  */
31319             break;
31320           }
31321
31322       if (opt->name == NULL)
31323         {
31324           /* Did we fail to find an extension because it wasn't specified in
31325              alphabetical order, or because it does not exist?  */
31326
31327           for (opt = arm_extensions; opt->name != NULL; opt++)
31328             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31329               break;
31330
31331           if (opt->name == NULL)
31332             as_bad (_("unknown architectural extension `%s'"), str);
31333           else
31334             as_bad (_("architectural extensions must be specified in "
31335                       "alphabetical order"));
31336
31337           return FALSE;
31338         }
31339       else
31340         {
31341           /* We should skip the extension we've just matched the next time
31342              round.  */
31343           opt++;
31344         }
31345
31346       str = ext;
31347     };
31348
31349   return TRUE;
31350 }
31351
31352 static bfd_boolean
31353 arm_parse_fp16_opt (const char *str)
31354 {
31355   if (strcasecmp (str, "ieee") == 0)
31356     fp16_format = ARM_FP16_FORMAT_IEEE;
31357   else if (strcasecmp (str, "alternative") == 0)
31358     fp16_format = ARM_FP16_FORMAT_ALTERNATIVE;
31359   else
31360     {
31361       as_bad (_("unrecognised float16 format \"%s\""), str);
31362       return FALSE;
31363     }
31364
31365   return TRUE;
31366 }
31367
31368 static bfd_boolean
31369 arm_parse_cpu (const char *str)
31370 {
31371   const struct arm_cpu_option_table *opt;
31372   const char *ext = strchr (str, '+');
31373   size_t len;
31374
31375   if (ext != NULL)
31376     len = ext - str;
31377   else
31378     len = strlen (str);
31379
31380   if (len == 0)
31381     {
31382       as_bad (_("missing cpu name `%s'"), str);
31383       return FALSE;
31384     }
31385
31386   for (opt = arm_cpus; opt->name != NULL; opt++)
31387     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31388       {
31389         mcpu_cpu_opt = &opt->value;
31390         if (mcpu_ext_opt == NULL)
31391           mcpu_ext_opt = XNEW (arm_feature_set);
31392         *mcpu_ext_opt = opt->ext;
31393         mcpu_fpu_opt = &opt->default_fpu;
31394         if (opt->canonical_name)
31395           {
31396             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
31397             strcpy (selected_cpu_name, opt->canonical_name);
31398           }
31399         else
31400           {
31401             size_t i;
31402
31403             if (len >= sizeof selected_cpu_name)
31404               len = (sizeof selected_cpu_name) - 1;
31405
31406             for (i = 0; i < len; i++)
31407               selected_cpu_name[i] = TOUPPER (opt->name[i]);
31408             selected_cpu_name[i] = 0;
31409           }
31410
31411         if (ext != NULL)
31412           return arm_parse_extension (ext, mcpu_cpu_opt, mcpu_ext_opt, NULL);
31413
31414         return TRUE;
31415       }
31416
31417   as_bad (_("unknown cpu `%s'"), str);
31418   return FALSE;
31419 }
31420
31421 static bfd_boolean
31422 arm_parse_arch (const char *str)
31423 {
31424   const struct arm_arch_option_table *opt;
31425   const char *ext = strchr (str, '+');
31426   size_t len;
31427
31428   if (ext != NULL)
31429     len = ext - str;
31430   else
31431     len = strlen (str);
31432
31433   if (len == 0)
31434     {
31435       as_bad (_("missing architecture name `%s'"), str);
31436       return FALSE;
31437     }
31438
31439   for (opt = arm_archs; opt->name != NULL; opt++)
31440     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
31441       {
31442         march_cpu_opt = &opt->value;
31443         if (march_ext_opt == NULL)
31444           march_ext_opt = XNEW (arm_feature_set);
31445         *march_ext_opt = arm_arch_none;
31446         march_fpu_opt = &opt->default_fpu;
31447         strcpy (selected_cpu_name, opt->name);
31448
31449         if (ext != NULL)
31450           return arm_parse_extension (ext, march_cpu_opt, march_ext_opt,
31451                                       opt->ext_table);
31452
31453         return TRUE;
31454       }
31455
31456   as_bad (_("unknown architecture `%s'\n"), str);
31457   return FALSE;
31458 }
31459
31460 static bfd_boolean
31461 arm_parse_fpu (const char * str)
31462 {
31463   const struct arm_option_fpu_value_table * opt;
31464
31465   for (opt = arm_fpus; opt->name != NULL; opt++)
31466     if (streq (opt->name, str))
31467       {
31468         mfpu_opt = &opt->value;
31469         return TRUE;
31470       }
31471
31472   as_bad (_("unknown floating point format `%s'\n"), str);
31473   return FALSE;
31474 }
31475
31476 static bfd_boolean
31477 arm_parse_float_abi (const char * str)
31478 {
31479   const struct arm_option_value_table * opt;
31480
31481   for (opt = arm_float_abis; opt->name != NULL; opt++)
31482     if (streq (opt->name, str))
31483       {
31484         mfloat_abi_opt = opt->value;
31485         return TRUE;
31486       }
31487
31488   as_bad (_("unknown floating point abi `%s'\n"), str);
31489   return FALSE;
31490 }
31491
31492 #ifdef OBJ_ELF
31493 static bfd_boolean
31494 arm_parse_eabi (const char * str)
31495 {
31496   const struct arm_option_value_table *opt;
31497
31498   for (opt = arm_eabis; opt->name != NULL; opt++)
31499     if (streq (opt->name, str))
31500       {
31501         meabi_flags = opt->value;
31502         return TRUE;
31503       }
31504   as_bad (_("unknown EABI `%s'\n"), str);
31505   return FALSE;
31506 }
31507 #endif
31508
31509 static bfd_boolean
31510 arm_parse_it_mode (const char * str)
31511 {
31512   bfd_boolean ret = TRUE;
31513
31514   if (streq ("arm", str))
31515     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
31516   else if (streq ("thumb", str))
31517     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
31518   else if (streq ("always", str))
31519     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
31520   else if (streq ("never", str))
31521     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
31522   else
31523     {
31524       as_bad (_("unknown implicit IT mode `%s', should be "\
31525                 "arm, thumb, always, or never."), str);
31526       ret = FALSE;
31527     }
31528
31529   return ret;
31530 }
31531
31532 static bfd_boolean
31533 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
31534 {
31535   codecomposer_syntax = TRUE;
31536   arm_comment_chars[0] = ';';
31537   arm_line_separator_chars[0] = 0;
31538   return TRUE;
31539 }
31540
31541 struct arm_long_option_table arm_long_opts[] =
31542 {
31543   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
31544    arm_parse_cpu, NULL},
31545   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
31546    arm_parse_arch, NULL},
31547   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
31548    arm_parse_fpu, NULL},
31549   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
31550    arm_parse_float_abi, NULL},
31551 #ifdef OBJ_ELF
31552   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
31553    arm_parse_eabi, NULL},
31554 #endif
31555   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
31556    arm_parse_it_mode, NULL},
31557   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
31558    arm_ccs_mode, NULL},
31559   {"mfp16-format=",
31560    N_("[ieee|alternative]\n\
31561                           set the encoding for half precision floating point "
31562                           "numbers to IEEE\n\
31563                           or Arm alternative format."),
31564    arm_parse_fp16_opt, NULL },
31565   {NULL, NULL, 0, NULL}
31566 };
31567
31568 int
31569 md_parse_option (int c, const char * arg)
31570 {
31571   struct arm_option_table *opt;
31572   const struct arm_legacy_option_table *fopt;
31573   struct arm_long_option_table *lopt;
31574
31575   switch (c)
31576     {
31577 #ifdef OPTION_EB
31578     case OPTION_EB:
31579       target_big_endian = 1;
31580       break;
31581 #endif
31582
31583 #ifdef OPTION_EL
31584     case OPTION_EL:
31585       target_big_endian = 0;
31586       break;
31587 #endif
31588
31589     case OPTION_FIX_V4BX:
31590       fix_v4bx = TRUE;
31591       break;
31592
31593 #ifdef OBJ_ELF
31594     case OPTION_FDPIC:
31595       arm_fdpic = TRUE;
31596       break;
31597 #endif /* OBJ_ELF */
31598
31599     case 'a':
31600       /* Listing option.  Just ignore these, we don't support additional
31601          ones.  */
31602       return 0;
31603
31604     default:
31605       for (opt = arm_opts; opt->option != NULL; opt++)
31606         {
31607           if (c == opt->option[0]
31608               && ((arg == NULL && opt->option[1] == 0)
31609                   || streq (arg, opt->option + 1)))
31610             {
31611               /* If the option is deprecated, tell the user.  */
31612               if (warn_on_deprecated && opt->deprecated != NULL)
31613                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
31614                            arg ? arg : "", _(opt->deprecated));
31615
31616               if (opt->var != NULL)
31617                 *opt->var = opt->value;
31618
31619               return 1;
31620             }
31621         }
31622
31623       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
31624         {
31625           if (c == fopt->option[0]
31626               && ((arg == NULL && fopt->option[1] == 0)
31627                   || streq (arg, fopt->option + 1)))
31628             {
31629               /* If the option is deprecated, tell the user.  */
31630               if (warn_on_deprecated && fopt->deprecated != NULL)
31631                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
31632                            arg ? arg : "", _(fopt->deprecated));
31633
31634               if (fopt->var != NULL)
31635                 *fopt->var = &fopt->value;
31636
31637               return 1;
31638             }
31639         }
31640
31641       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
31642         {
31643           /* These options are expected to have an argument.  */
31644           if (c == lopt->option[0]
31645               && arg != NULL
31646               && strncmp (arg, lopt->option + 1,
31647                           strlen (lopt->option + 1)) == 0)
31648             {
31649               /* If the option is deprecated, tell the user.  */
31650               if (warn_on_deprecated && lopt->deprecated != NULL)
31651                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
31652                            _(lopt->deprecated));
31653
31654               /* Call the sup-option parser.  */
31655               return lopt->func (arg + strlen (lopt->option) - 1);
31656             }
31657         }
31658
31659       return 0;
31660     }
31661
31662   return 1;
31663 }
31664
31665 void
31666 md_show_usage (FILE * fp)
31667 {
31668   struct arm_option_table *opt;
31669   struct arm_long_option_table *lopt;
31670
31671   fprintf (fp, _(" ARM-specific assembler options:\n"));
31672
31673   for (opt = arm_opts; opt->option != NULL; opt++)
31674     if (opt->help != NULL)
31675       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
31676
31677   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
31678     if (lopt->help != NULL)
31679       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
31680
31681 #ifdef OPTION_EB
31682   fprintf (fp, _("\
31683   -EB                     assemble code for a big-endian cpu\n"));
31684 #endif
31685
31686 #ifdef OPTION_EL
31687   fprintf (fp, _("\
31688   -EL                     assemble code for a little-endian cpu\n"));
31689 #endif
31690
31691   fprintf (fp, _("\
31692   --fix-v4bx              Allow BX in ARMv4 code\n"));
31693
31694 #ifdef OBJ_ELF
31695   fprintf (fp, _("\
31696   --fdpic                 generate an FDPIC object file\n"));
31697 #endif /* OBJ_ELF */
31698 }
31699
31700 #ifdef OBJ_ELF
31701
31702 typedef struct
31703 {
31704   int val;
31705   arm_feature_set flags;
31706 } cpu_arch_ver_table;
31707
31708 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
31709    chronologically for architectures, with an exception for ARMv6-M and
31710    ARMv6S-M due to legacy reasons.  No new architecture should have a
31711    special case.  This allows for build attribute selection results to be
31712    stable when new architectures are added.  */
31713 static const cpu_arch_ver_table cpu_arch_ver[] =
31714 {
31715     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V1},
31716     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2},
31717     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2S},
31718     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3},
31719     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3M},
31720     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4xM},
31721     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4},
31722     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4TxM},
31723     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4T},
31724     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5xM},
31725     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5},
31726     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5TxM},
31727     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5T},
31728     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TExP},
31729     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TE},
31730     {TAG_CPU_ARCH_V5TEJ,      ARM_ARCH_V5TEJ},
31731     {TAG_CPU_ARCH_V6,         ARM_ARCH_V6},
31732     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6Z},
31733     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6KZ},
31734     {TAG_CPU_ARCH_V6K,        ARM_ARCH_V6K},
31735     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6T2},
31736     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KT2},
31737     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6ZT2},
31738     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KZT2},
31739
31740     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
31741        always selected build attributes to match those of ARMv6-M
31742        (resp. ARMv6S-M).  However, due to these architectures being a strict
31743        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
31744        would be selected when fully respecting chronology of architectures.
31745        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
31746        move them before ARMv7 architectures.  */
31747     {TAG_CPU_ARCH_V6_M,       ARM_ARCH_V6M},
31748     {TAG_CPU_ARCH_V6S_M,      ARM_ARCH_V6SM},
31749
31750     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7},
31751     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7A},
31752     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7R},
31753     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7M},
31754     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7VE},
31755     {TAG_CPU_ARCH_V7E_M,      ARM_ARCH_V7EM},
31756     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8A},
31757     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_1A},
31758     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_2A},
31759     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_3A},
31760     {TAG_CPU_ARCH_V8M_BASE,   ARM_ARCH_V8M_BASE},
31761     {TAG_CPU_ARCH_V8M_MAIN,   ARM_ARCH_V8M_MAIN},
31762     {TAG_CPU_ARCH_V8R,        ARM_ARCH_V8R},
31763     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_4A},
31764     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_5A},
31765     {TAG_CPU_ARCH_V8_1M_MAIN, ARM_ARCH_V8_1M_MAIN},
31766     {-1,                      ARM_ARCH_NONE}
31767 };
31768
31769 /* Set an attribute if it has not already been set by the user.  */
31770
31771 static void
31772 aeabi_set_attribute_int (int tag, int value)
31773 {
31774   if (tag < 1
31775       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
31776       || !attributes_set_explicitly[tag])
31777     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
31778 }
31779
31780 static void
31781 aeabi_set_attribute_string (int tag, const char *value)
31782 {
31783   if (tag < 1
31784       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
31785       || !attributes_set_explicitly[tag])
31786     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
31787 }
31788
31789 /* Return whether features in the *NEEDED feature set are available via
31790    extensions for the architecture whose feature set is *ARCH_FSET.  */
31791
31792 static bfd_boolean
31793 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
31794                             const arm_feature_set *needed)
31795 {
31796   int i, nb_allowed_archs;
31797   arm_feature_set ext_fset;
31798   const struct arm_option_extension_value_table *opt;
31799
31800   ext_fset = arm_arch_none;
31801   for (opt = arm_extensions; opt->name != NULL; opt++)
31802     {
31803       /* Extension does not provide any feature we need.  */
31804       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
31805         continue;
31806
31807       nb_allowed_archs =
31808         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
31809       for (i = 0; i < nb_allowed_archs; i++)
31810         {
31811           /* Empty entry.  */
31812           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
31813             break;
31814
31815           /* Extension is available, add it.  */
31816           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
31817             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
31818         }
31819     }
31820
31821   /* Can we enable all features in *needed?  */
31822   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
31823 }
31824
31825 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
31826    a given architecture feature set *ARCH_EXT_FSET including extension feature
31827    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
31828    - if true, check for an exact match of the architecture modulo extensions;
31829    - otherwise, select build attribute value of the first superset
31830      architecture released so that results remains stable when new architectures
31831      are added.
31832    For -march/-mcpu=all the build attribute value of the most featureful
31833    architecture is returned.  Tag_CPU_arch_profile result is returned in
31834    PROFILE.  */
31835
31836 static int
31837 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
31838                               const arm_feature_set *ext_fset,
31839                               char *profile, int exact_match)
31840 {
31841   arm_feature_set arch_fset;
31842   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
31843
31844   /* Select most featureful architecture with all its extensions if building
31845      for -march=all as the feature sets used to set build attributes.  */
31846   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
31847     {
31848       /* Force revisiting of decision for each new architecture.  */
31849       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8_1M_MAIN);
31850       *profile = 'A';
31851       return TAG_CPU_ARCH_V8;
31852     }
31853
31854   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
31855
31856   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
31857     {
31858       arm_feature_set known_arch_fset;
31859
31860       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
31861       if (exact_match)
31862         {
31863           /* Base architecture match user-specified architecture and
31864              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
31865           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
31866             {
31867               p_ver_ret = p_ver;
31868               goto found;
31869             }
31870           /* Base architecture match user-specified architecture only
31871              (eg. ARMv6-M in the same case as above).  Record it in case we
31872              find a match with above condition.  */
31873           else if (p_ver_ret == NULL
31874                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
31875             p_ver_ret = p_ver;
31876         }
31877       else
31878         {
31879
31880           /* Architecture has all features wanted.  */
31881           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
31882             {
31883               arm_feature_set added_fset;
31884
31885               /* Compute features added by this architecture over the one
31886                  recorded in p_ver_ret.  */
31887               if (p_ver_ret != NULL)
31888                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
31889                                    p_ver_ret->flags);
31890               /* First architecture that match incl. with extensions, or the
31891                  only difference in features over the recorded match is
31892                  features that were optional and are now mandatory.  */
31893               if (p_ver_ret == NULL
31894                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
31895                 {
31896                   p_ver_ret = p_ver;
31897                   goto found;
31898                 }
31899             }
31900           else if (p_ver_ret == NULL)
31901             {
31902               arm_feature_set needed_ext_fset;
31903
31904               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
31905
31906               /* Architecture has all features needed when using some
31907                  extensions.  Record it and continue searching in case there
31908                  exist an architecture providing all needed features without
31909                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
31910                  OS extension).  */
31911               if (have_ext_for_needed_feat_p (&known_arch_fset,
31912                                               &needed_ext_fset))
31913                 p_ver_ret = p_ver;
31914             }
31915         }
31916     }
31917
31918   if (p_ver_ret == NULL)
31919     return -1;
31920
31921 found:
31922   /* Tag_CPU_arch_profile.  */
31923   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
31924       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
31925       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
31926           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
31927     *profile = 'A';
31928   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
31929     *profile = 'R';
31930   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
31931     *profile = 'M';
31932   else
31933     *profile = '\0';
31934   return p_ver_ret->val;
31935 }
31936
31937 /* Set the public EABI object attributes.  */
31938
31939 static void
31940 aeabi_set_public_attributes (void)
31941 {
31942   char profile = '\0';
31943   int arch = -1;
31944   int virt_sec = 0;
31945   int fp16_optional = 0;
31946   int skip_exact_match = 0;
31947   arm_feature_set flags, flags_arch, flags_ext;
31948
31949   /* Autodetection mode, choose the architecture based the instructions
31950      actually used.  */
31951   if (no_cpu_selected ())
31952     {
31953       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
31954
31955       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
31956         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
31957
31958       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
31959         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
31960
31961       /* Code run during relaxation relies on selected_cpu being set.  */
31962       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
31963       flags_ext = arm_arch_none;
31964       ARM_CLEAR_FEATURE (selected_arch, flags_arch, flags_ext);
31965       selected_ext = flags_ext;
31966       selected_cpu = flags;
31967     }
31968   /* Otherwise, choose the architecture based on the capabilities of the
31969      requested cpu.  */
31970   else
31971     {
31972       ARM_MERGE_FEATURE_SETS (flags_arch, selected_arch, selected_ext);
31973       ARM_CLEAR_FEATURE (flags_arch, flags_arch, fpu_any);
31974       flags_ext = selected_ext;
31975       flags = selected_cpu;
31976     }
31977   ARM_MERGE_FEATURE_SETS (flags, flags, selected_fpu);
31978
31979   /* Allow the user to override the reported architecture.  */
31980   if (!ARM_FEATURE_ZERO (selected_object_arch))
31981     {
31982       ARM_CLEAR_FEATURE (flags_arch, selected_object_arch, fpu_any);
31983       flags_ext = arm_arch_none;
31984     }
31985   else
31986     skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
31987
31988   /* When this function is run again after relaxation has happened there is no
31989      way to determine whether an architecture or CPU was specified by the user:
31990      - selected_cpu is set above for relaxation to work;
31991      - march_cpu_opt is not set if only -mcpu or .cpu is used;
31992      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
31993      Therefore, if not in -march=all case we first try an exact match and fall
31994      back to autodetection.  */
31995   if (!skip_exact_match)
31996     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
31997   if (arch == -1)
31998     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
31999   if (arch == -1)
32000     as_bad (_("no architecture contains all the instructions used\n"));
32001
32002   /* Tag_CPU_name.  */
32003   if (selected_cpu_name[0])
32004     {
32005       char *q;
32006
32007       q = selected_cpu_name;
32008       if (strncmp (q, "armv", 4) == 0)
32009         {
32010           int i;
32011
32012           q += 4;
32013           for (i = 0; q[i]; i++)
32014             q[i] = TOUPPER (q[i]);
32015         }
32016       aeabi_set_attribute_string (Tag_CPU_name, q);
32017     }
32018
32019   /* Tag_CPU_arch.  */
32020   aeabi_set_attribute_int (Tag_CPU_arch, arch);
32021
32022   /* Tag_CPU_arch_profile.  */
32023   if (profile != '\0')
32024     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
32025
32026   /* Tag_DSP_extension.  */
32027   if (ARM_CPU_HAS_FEATURE (selected_ext, arm_ext_dsp))
32028     aeabi_set_attribute_int (Tag_DSP_extension, 1);
32029
32030   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
32031   /* Tag_ARM_ISA_use.  */
32032   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
32033       || ARM_FEATURE_ZERO (flags_arch))
32034     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
32035
32036   /* Tag_THUMB_ISA_use.  */
32037   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
32038       || ARM_FEATURE_ZERO (flags_arch))
32039     {
32040       int thumb_isa_use;
32041
32042       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
32043           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
32044         thumb_isa_use = 3;
32045       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
32046         thumb_isa_use = 2;
32047       else
32048         thumb_isa_use = 1;
32049       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
32050     }
32051
32052   /* Tag_VFP_arch.  */
32053   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
32054     aeabi_set_attribute_int (Tag_VFP_arch,
32055                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
32056                              ? 7 : 8);
32057   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
32058     aeabi_set_attribute_int (Tag_VFP_arch,
32059                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
32060                              ? 5 : 6);
32061   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
32062     {
32063       fp16_optional = 1;
32064       aeabi_set_attribute_int (Tag_VFP_arch, 3);
32065     }
32066   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
32067     {
32068       aeabi_set_attribute_int (Tag_VFP_arch, 4);
32069       fp16_optional = 1;
32070     }
32071   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
32072     aeabi_set_attribute_int (Tag_VFP_arch, 2);
32073   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
32074            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
32075     aeabi_set_attribute_int (Tag_VFP_arch, 1);
32076
32077   /* Tag_ABI_HardFP_use.  */
32078   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
32079       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
32080     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
32081
32082   /* Tag_WMMX_arch.  */
32083   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
32084     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
32085   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
32086     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
32087
32088   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
32089   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
32090     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
32091   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
32092     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
32093   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
32094     {
32095       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
32096         {
32097           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
32098         }
32099       else
32100         {
32101           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
32102           fp16_optional = 1;
32103         }
32104     }
32105
32106   if (ARM_CPU_HAS_FEATURE (flags, mve_fp_ext))
32107     aeabi_set_attribute_int (Tag_MVE_arch, 2);
32108   else if (ARM_CPU_HAS_FEATURE (flags, mve_ext))
32109     aeabi_set_attribute_int (Tag_MVE_arch, 1);
32110
32111   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
32112   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
32113     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
32114
32115   /* Tag_DIV_use.
32116
32117      We set Tag_DIV_use to two when integer divide instructions have been used
32118      in ARM state, or when Thumb integer divide instructions have been used,
32119      but we have no architecture profile set, nor have we any ARM instructions.
32120
32121      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
32122      by the base architecture.
32123
32124      For new architectures we will have to check these tests.  */
32125   gas_assert (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
32126   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
32127       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
32128     aeabi_set_attribute_int (Tag_DIV_use, 0);
32129   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
32130            || (profile == '\0'
32131                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
32132                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
32133     aeabi_set_attribute_int (Tag_DIV_use, 2);
32134
32135   /* Tag_MP_extension_use.  */
32136   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
32137     aeabi_set_attribute_int (Tag_MPextension_use, 1);
32138
32139   /* Tag Virtualization_use.  */
32140   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
32141     virt_sec |= 1;
32142   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
32143     virt_sec |= 2;
32144   if (virt_sec != 0)
32145     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
32146
32147   if (fp16_format != ARM_FP16_FORMAT_DEFAULT)
32148     aeabi_set_attribute_int (Tag_ABI_FP_16bit_format, fp16_format);
32149 }
32150
32151 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
32152    finished and free extension feature bits which will not be used anymore.  */
32153
32154 void
32155 arm_md_post_relax (void)
32156 {
32157   aeabi_set_public_attributes ();
32158   XDELETE (mcpu_ext_opt);
32159   mcpu_ext_opt = NULL;
32160   XDELETE (march_ext_opt);
32161   march_ext_opt = NULL;
32162 }
32163
32164 /* Add the default contents for the .ARM.attributes section.  */
32165
32166 void
32167 arm_md_end (void)
32168 {
32169   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
32170     return;
32171
32172   aeabi_set_public_attributes ();
32173 }
32174 #endif /* OBJ_ELF */
32175
32176 /* Parse a .cpu directive.  */
32177
32178 static void
32179 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
32180 {
32181   const struct arm_cpu_option_table *opt;
32182   char *name;
32183   char saved_char;
32184
32185   name = input_line_pointer;
32186   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32187     input_line_pointer++;
32188   saved_char = *input_line_pointer;
32189   *input_line_pointer = 0;
32190
32191   /* Skip the first "all" entry.  */
32192   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
32193     if (streq (opt->name, name))
32194       {
32195         selected_arch = opt->value;
32196         selected_ext = opt->ext;
32197         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
32198         if (opt->canonical_name)
32199           strcpy (selected_cpu_name, opt->canonical_name);
32200         else
32201           {
32202             int i;
32203             for (i = 0; opt->name[i]; i++)
32204               selected_cpu_name[i] = TOUPPER (opt->name[i]);
32205
32206             selected_cpu_name[i] = 0;
32207           }
32208         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32209
32210         *input_line_pointer = saved_char;
32211         demand_empty_rest_of_line ();
32212         return;
32213       }
32214   as_bad (_("unknown cpu `%s'"), name);
32215   *input_line_pointer = saved_char;
32216   ignore_rest_of_line ();
32217 }
32218
32219 /* Parse a .arch directive.  */
32220
32221 static void
32222 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
32223 {
32224   const struct arm_arch_option_table *opt;
32225   char saved_char;
32226   char *name;
32227
32228   name = input_line_pointer;
32229   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32230     input_line_pointer++;
32231   saved_char = *input_line_pointer;
32232   *input_line_pointer = 0;
32233
32234   /* Skip the first "all" entry.  */
32235   for (opt = arm_archs + 1; opt->name != NULL; opt++)
32236     if (streq (opt->name, name))
32237       {
32238         selected_arch = opt->value;
32239         selected_ext = arm_arch_none;
32240         selected_cpu = selected_arch;
32241         strcpy (selected_cpu_name, opt->name);
32242         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32243         *input_line_pointer = saved_char;
32244         demand_empty_rest_of_line ();
32245         return;
32246       }
32247
32248   as_bad (_("unknown architecture `%s'\n"), name);
32249   *input_line_pointer = saved_char;
32250   ignore_rest_of_line ();
32251 }
32252
32253 /* Parse a .object_arch directive.  */
32254
32255 static void
32256 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
32257 {
32258   const struct arm_arch_option_table *opt;
32259   char saved_char;
32260   char *name;
32261
32262   name = input_line_pointer;
32263   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32264     input_line_pointer++;
32265   saved_char = *input_line_pointer;
32266   *input_line_pointer = 0;
32267
32268   /* Skip the first "all" entry.  */
32269   for (opt = arm_archs + 1; opt->name != NULL; opt++)
32270     if (streq (opt->name, name))
32271       {
32272         selected_object_arch = opt->value;
32273         *input_line_pointer = saved_char;
32274         demand_empty_rest_of_line ();
32275         return;
32276       }
32277
32278   as_bad (_("unknown architecture `%s'\n"), name);
32279   *input_line_pointer = saved_char;
32280   ignore_rest_of_line ();
32281 }
32282
32283 /* Parse a .arch_extension directive.  */
32284
32285 static void
32286 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
32287 {
32288   const struct arm_option_extension_value_table *opt;
32289   char saved_char;
32290   char *name;
32291   int adding_value = 1;
32292
32293   name = input_line_pointer;
32294   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32295     input_line_pointer++;
32296   saved_char = *input_line_pointer;
32297   *input_line_pointer = 0;
32298
32299   if (strlen (name) >= 2
32300       && strncmp (name, "no", 2) == 0)
32301     {
32302       adding_value = 0;
32303       name += 2;
32304     }
32305
32306   for (opt = arm_extensions; opt->name != NULL; opt++)
32307     if (streq (opt->name, name))
32308       {
32309         int i, nb_allowed_archs =
32310           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
32311         for (i = 0; i < nb_allowed_archs; i++)
32312           {
32313             /* Empty entry.  */
32314             if (ARM_CPU_IS_ANY (opt->allowed_archs[i]))
32315               continue;
32316             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], selected_arch))
32317               break;
32318           }
32319
32320         if (i == nb_allowed_archs)
32321           {
32322             as_bad (_("architectural extension `%s' is not allowed for the "
32323                       "current base architecture"), name);
32324             break;
32325           }
32326
32327         if (adding_value)
32328           ARM_MERGE_FEATURE_SETS (selected_ext, selected_ext,
32329                                   opt->merge_value);
32330         else
32331           ARM_CLEAR_FEATURE (selected_ext, selected_ext, opt->clear_value);
32332
32333         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
32334         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32335         *input_line_pointer = saved_char;
32336         demand_empty_rest_of_line ();
32337         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
32338            on this return so that duplicate extensions (extensions with the
32339            same name as a previous extension in the list) are not considered
32340            for command-line parsing.  */
32341         return;
32342       }
32343
32344   if (opt->name == NULL)
32345     as_bad (_("unknown architecture extension `%s'\n"), name);
32346
32347   *input_line_pointer = saved_char;
32348   ignore_rest_of_line ();
32349 }
32350
32351 /* Parse a .fpu directive.  */
32352
32353 static void
32354 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
32355 {
32356   const struct arm_option_fpu_value_table *opt;
32357   char saved_char;
32358   char *name;
32359
32360   name = input_line_pointer;
32361   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
32362     input_line_pointer++;
32363   saved_char = *input_line_pointer;
32364   *input_line_pointer = 0;
32365
32366   for (opt = arm_fpus; opt->name != NULL; opt++)
32367     if (streq (opt->name, name))
32368       {
32369         selected_fpu = opt->value;
32370 #ifndef CPU_DEFAULT
32371         if (no_cpu_selected ())
32372           ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
32373         else
32374 #endif
32375           ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
32376         *input_line_pointer = saved_char;
32377         demand_empty_rest_of_line ();
32378         return;
32379       }
32380
32381   as_bad (_("unknown floating point format `%s'\n"), name);
32382   *input_line_pointer = saved_char;
32383   ignore_rest_of_line ();
32384 }
32385
32386 /* Copy symbol information.  */
32387
32388 void
32389 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
32390 {
32391   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
32392 }
32393
32394 #ifdef OBJ_ELF
32395 /* Given a symbolic attribute NAME, return the proper integer value.
32396    Returns -1 if the attribute is not known.  */
32397
32398 int
32399 arm_convert_symbolic_attribute (const char *name)
32400 {
32401   static const struct
32402   {
32403     const char * name;
32404     const int    tag;
32405   }
32406   attribute_table[] =
32407     {
32408       /* When you modify this table you should
32409          also modify the list in doc/c-arm.texi.  */
32410 #define T(tag) {#tag, tag}
32411       T (Tag_CPU_raw_name),
32412       T (Tag_CPU_name),
32413       T (Tag_CPU_arch),
32414       T (Tag_CPU_arch_profile),
32415       T (Tag_ARM_ISA_use),
32416       T (Tag_THUMB_ISA_use),
32417       T (Tag_FP_arch),
32418       T (Tag_VFP_arch),
32419       T (Tag_WMMX_arch),
32420       T (Tag_Advanced_SIMD_arch),
32421       T (Tag_PCS_config),
32422       T (Tag_ABI_PCS_R9_use),
32423       T (Tag_ABI_PCS_RW_data),
32424       T (Tag_ABI_PCS_RO_data),
32425       T (Tag_ABI_PCS_GOT_use),
32426       T (Tag_ABI_PCS_wchar_t),
32427       T (Tag_ABI_FP_rounding),
32428       T (Tag_ABI_FP_denormal),
32429       T (Tag_ABI_FP_exceptions),
32430       T (Tag_ABI_FP_user_exceptions),
32431       T (Tag_ABI_FP_number_model),
32432       T (Tag_ABI_align_needed),
32433       T (Tag_ABI_align8_needed),
32434       T (Tag_ABI_align_preserved),
32435       T (Tag_ABI_align8_preserved),
32436       T (Tag_ABI_enum_size),
32437       T (Tag_ABI_HardFP_use),
32438       T (Tag_ABI_VFP_args),
32439       T (Tag_ABI_WMMX_args),
32440       T (Tag_ABI_optimization_goals),
32441       T (Tag_ABI_FP_optimization_goals),
32442       T (Tag_compatibility),
32443       T (Tag_CPU_unaligned_access),
32444       T (Tag_FP_HP_extension),
32445       T (Tag_VFP_HP_extension),
32446       T (Tag_ABI_FP_16bit_format),
32447       T (Tag_MPextension_use),
32448       T (Tag_DIV_use),
32449       T (Tag_nodefaults),
32450       T (Tag_also_compatible_with),
32451       T (Tag_conformance),
32452       T (Tag_T2EE_use),
32453       T (Tag_Virtualization_use),
32454       T (Tag_DSP_extension),
32455       T (Tag_MVE_arch),
32456       /* We deliberately do not include Tag_MPextension_use_legacy.  */
32457 #undef T
32458     };
32459   unsigned int i;
32460
32461   if (name == NULL)
32462     return -1;
32463
32464   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
32465     if (streq (name, attribute_table[i].name))
32466       return attribute_table[i].tag;
32467
32468   return -1;
32469 }
32470
32471 /* Apply sym value for relocations only in the case that they are for
32472    local symbols in the same segment as the fixup and you have the
32473    respective architectural feature for blx and simple switches.  */
32474
32475 int
32476 arm_apply_sym_value (struct fix * fixP, segT this_seg)
32477 {
32478   if (fixP->fx_addsy
32479       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
32480       /* PR 17444: If the local symbol is in a different section then a reloc
32481          will always be generated for it, so applying the symbol value now
32482          will result in a double offset being stored in the relocation.  */
32483       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
32484       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
32485     {
32486       switch (fixP->fx_r_type)
32487         {
32488         case BFD_RELOC_ARM_PCREL_BLX:
32489         case BFD_RELOC_THUMB_PCREL_BRANCH23:
32490           if (ARM_IS_FUNC (fixP->fx_addsy))
32491             return 1;
32492           break;
32493
32494         case BFD_RELOC_ARM_PCREL_CALL:
32495         case BFD_RELOC_THUMB_PCREL_BLX:
32496           if (THUMB_IS_FUNC (fixP->fx_addsy))
32497             return 1;
32498           break;
32499
32500         default:
32501           break;
32502         }
32503
32504     }
32505   return 0;
32506 }
32507 #endif /* OBJ_ELF */