[PATCH 15/57][Arm][GAS] Add support for MVE instructions: vcls, vclz and vfmas
[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 /* Pure syntax.  */
1013
1014 /* This array holds the chars that always start a comment.  If the
1015    pre-processor is disabled, these aren't very useful.  */
1016 char arm_comment_chars[] = "@";
1017
1018 /* This array holds the chars that only start a comment at the beginning of
1019    a line.  If the line seems to have the form '# 123 filename'
1020    .line and .file directives will appear in the pre-processed output.  */
1021 /* Note that input_file.c hand checks for '#' at the beginning of the
1022    first line of the input file.  This is because the compiler outputs
1023    #NO_APP at the beginning of its output.  */
1024 /* Also note that comments like this one will always work.  */
1025 const char line_comment_chars[] = "#";
1026
1027 char arm_line_separator_chars[] = ";";
1028
1029 /* Chars that can be used to separate mant
1030    from exp in floating point numbers.  */
1031 const char EXP_CHARS[] = "eE";
1032
1033 /* Chars that mean this number is a floating point constant.  */
1034 /* As in 0f12.456  */
1035 /* or    0d1.2345e12  */
1036
1037 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
1038
1039 /* Prefix characters that indicate the start of an immediate
1040    value.  */
1041 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
1042
1043 /* Separator character handling.  */
1044
1045 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
1046
1047 static inline int
1048 skip_past_char (char ** str, char c)
1049 {
1050   /* PR gas/14987: Allow for whitespace before the expected character.  */
1051   skip_whitespace (*str);
1052
1053   if (**str == c)
1054     {
1055       (*str)++;
1056       return SUCCESS;
1057     }
1058   else
1059     return FAIL;
1060 }
1061
1062 #define skip_past_comma(str) skip_past_char (str, ',')
1063
1064 /* Arithmetic expressions (possibly involving symbols).  */
1065
1066 /* Return TRUE if anything in the expression is a bignum.  */
1067
1068 static bfd_boolean
1069 walk_no_bignums (symbolS * sp)
1070 {
1071   if (symbol_get_value_expression (sp)->X_op == O_big)
1072     return TRUE;
1073
1074   if (symbol_get_value_expression (sp)->X_add_symbol)
1075     {
1076       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
1077               || (symbol_get_value_expression (sp)->X_op_symbol
1078                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
1079     }
1080
1081   return FALSE;
1082 }
1083
1084 static bfd_boolean in_my_get_expression = FALSE;
1085
1086 /* Third argument to my_get_expression.  */
1087 #define GE_NO_PREFIX 0
1088 #define GE_IMM_PREFIX 1
1089 #define GE_OPT_PREFIX 2
1090 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
1091    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
1092 #define GE_OPT_PREFIX_BIG 3
1093
1094 static int
1095 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1096 {
1097   char * save_in;
1098
1099   /* In unified syntax, all prefixes are optional.  */
1100   if (unified_syntax)
1101     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1102                   : GE_OPT_PREFIX;
1103
1104   switch (prefix_mode)
1105     {
1106     case GE_NO_PREFIX: break;
1107     case GE_IMM_PREFIX:
1108       if (!is_immediate_prefix (**str))
1109         {
1110           inst.error = _("immediate expression requires a # prefix");
1111           return FAIL;
1112         }
1113       (*str)++;
1114       break;
1115     case GE_OPT_PREFIX:
1116     case GE_OPT_PREFIX_BIG:
1117       if (is_immediate_prefix (**str))
1118         (*str)++;
1119       break;
1120     default:
1121       abort ();
1122     }
1123
1124   memset (ep, 0, sizeof (expressionS));
1125
1126   save_in = input_line_pointer;
1127   input_line_pointer = *str;
1128   in_my_get_expression = TRUE;
1129   expression (ep);
1130   in_my_get_expression = FALSE;
1131
1132   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1133     {
1134       /* We found a bad or missing expression in md_operand().  */
1135       *str = input_line_pointer;
1136       input_line_pointer = save_in;
1137       if (inst.error == NULL)
1138         inst.error = (ep->X_op == O_absent
1139                       ? _("missing expression") :_("bad expression"));
1140       return 1;
1141     }
1142
1143   /* Get rid of any bignums now, so that we don't generate an error for which
1144      we can't establish a line number later on.  Big numbers are never valid
1145      in instructions, which is where this routine is always called.  */
1146   if (prefix_mode != GE_OPT_PREFIX_BIG
1147       && (ep->X_op == O_big
1148           || (ep->X_add_symbol
1149               && (walk_no_bignums (ep->X_add_symbol)
1150                   || (ep->X_op_symbol
1151                       && walk_no_bignums (ep->X_op_symbol))))))
1152     {
1153       inst.error = _("invalid constant");
1154       *str = input_line_pointer;
1155       input_line_pointer = save_in;
1156       return 1;
1157     }
1158
1159   *str = input_line_pointer;
1160   input_line_pointer = save_in;
1161   return SUCCESS;
1162 }
1163
1164 /* Turn a string in input_line_pointer into a floating point constant
1165    of type TYPE, and store the appropriate bytes in *LITP.  The number
1166    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1167    returned, or NULL on OK.
1168
1169    Note that fp constants aren't represent in the normal way on the ARM.
1170    In big endian mode, things are as expected.  However, in little endian
1171    mode fp constants are big-endian word-wise, and little-endian byte-wise
1172    within the words.  For example, (double) 1.1 in big endian mode is
1173    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1174    the byte sequence 99 99 f1 3f 9a 99 99 99.
1175
1176    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1177
1178 const char *
1179 md_atof (int type, char * litP, int * sizeP)
1180 {
1181   int prec;
1182   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1183   char *t;
1184   int i;
1185
1186   switch (type)
1187     {
1188     case 'f':
1189     case 'F':
1190     case 's':
1191     case 'S':
1192       prec = 2;
1193       break;
1194
1195     case 'd':
1196     case 'D':
1197     case 'r':
1198     case 'R':
1199       prec = 4;
1200       break;
1201
1202     case 'x':
1203     case 'X':
1204       prec = 5;
1205       break;
1206
1207     case 'p':
1208     case 'P':
1209       prec = 5;
1210       break;
1211
1212     default:
1213       *sizeP = 0;
1214       return _("Unrecognized or unsupported floating point constant");
1215     }
1216
1217   t = atof_ieee (input_line_pointer, type, words);
1218   if (t)
1219     input_line_pointer = t;
1220   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1221
1222   if (target_big_endian)
1223     {
1224       for (i = 0; i < prec; i++)
1225         {
1226           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1227           litP += sizeof (LITTLENUM_TYPE);
1228         }
1229     }
1230   else
1231     {
1232       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1233         for (i = prec - 1; i >= 0; i--)
1234           {
1235             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1236             litP += sizeof (LITTLENUM_TYPE);
1237           }
1238       else
1239         /* For a 4 byte float the order of elements in `words' is 1 0.
1240            For an 8 byte float the order is 1 0 3 2.  */
1241         for (i = 0; i < prec; i += 2)
1242           {
1243             md_number_to_chars (litP, (valueT) words[i + 1],
1244                                 sizeof (LITTLENUM_TYPE));
1245             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1246                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1247             litP += 2 * sizeof (LITTLENUM_TYPE);
1248           }
1249     }
1250
1251   return NULL;
1252 }
1253
1254 /* We handle all bad expressions here, so that we can report the faulty
1255    instruction in the error message.  */
1256
1257 void
1258 md_operand (expressionS * exp)
1259 {
1260   if (in_my_get_expression)
1261     exp->X_op = O_illegal;
1262 }
1263
1264 /* Immediate values.  */
1265
1266 #ifdef OBJ_ELF
1267 /* Generic immediate-value read function for use in directives.
1268    Accepts anything that 'expression' can fold to a constant.
1269    *val receives the number.  */
1270
1271 static int
1272 immediate_for_directive (int *val)
1273 {
1274   expressionS exp;
1275   exp.X_op = O_illegal;
1276
1277   if (is_immediate_prefix (*input_line_pointer))
1278     {
1279       input_line_pointer++;
1280       expression (&exp);
1281     }
1282
1283   if (exp.X_op != O_constant)
1284     {
1285       as_bad (_("expected #constant"));
1286       ignore_rest_of_line ();
1287       return FAIL;
1288     }
1289   *val = exp.X_add_number;
1290   return SUCCESS;
1291 }
1292 #endif
1293
1294 /* Register parsing.  */
1295
1296 /* Generic register parser.  CCP points to what should be the
1297    beginning of a register name.  If it is indeed a valid register
1298    name, advance CCP over it and return the reg_entry structure;
1299    otherwise return NULL.  Does not issue diagnostics.  */
1300
1301 static struct reg_entry *
1302 arm_reg_parse_multi (char **ccp)
1303 {
1304   char *start = *ccp;
1305   char *p;
1306   struct reg_entry *reg;
1307
1308   skip_whitespace (start);
1309
1310 #ifdef REGISTER_PREFIX
1311   if (*start != REGISTER_PREFIX)
1312     return NULL;
1313   start++;
1314 #endif
1315 #ifdef OPTIONAL_REGISTER_PREFIX
1316   if (*start == OPTIONAL_REGISTER_PREFIX)
1317     start++;
1318 #endif
1319
1320   p = start;
1321   if (!ISALPHA (*p) || !is_name_beginner (*p))
1322     return NULL;
1323
1324   do
1325     p++;
1326   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1327
1328   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1329
1330   if (!reg)
1331     return NULL;
1332
1333   *ccp = p;
1334   return reg;
1335 }
1336
1337 static int
1338 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1339                     enum arm_reg_type type)
1340 {
1341   /* Alternative syntaxes are accepted for a few register classes.  */
1342   switch (type)
1343     {
1344     case REG_TYPE_MVF:
1345     case REG_TYPE_MVD:
1346     case REG_TYPE_MVFX:
1347     case REG_TYPE_MVDX:
1348       /* Generic coprocessor register names are allowed for these.  */
1349       if (reg && reg->type == REG_TYPE_CN)
1350         return reg->number;
1351       break;
1352
1353     case REG_TYPE_CP:
1354       /* For backward compatibility, a bare number is valid here.  */
1355       {
1356         unsigned long processor = strtoul (start, ccp, 10);
1357         if (*ccp != start && processor <= 15)
1358           return processor;
1359       }
1360       /* Fall through.  */
1361
1362     case REG_TYPE_MMXWC:
1363       /* WC includes WCG.  ??? I'm not sure this is true for all
1364          instructions that take WC registers.  */
1365       if (reg && reg->type == REG_TYPE_MMXWCG)
1366         return reg->number;
1367       break;
1368
1369     default:
1370       break;
1371     }
1372
1373   return FAIL;
1374 }
1375
1376 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1377    return value is the register number or FAIL.  */
1378
1379 static int
1380 arm_reg_parse (char **ccp, enum arm_reg_type type)
1381 {
1382   char *start = *ccp;
1383   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1384   int ret;
1385
1386   /* Do not allow a scalar (reg+index) to parse as a register.  */
1387   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1388     return FAIL;
1389
1390   if (reg && reg->type == type)
1391     return reg->number;
1392
1393   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1394     return ret;
1395
1396   *ccp = start;
1397   return FAIL;
1398 }
1399
1400 /* Parse a Neon type specifier. *STR should point at the leading '.'
1401    character. Does no verification at this stage that the type fits the opcode
1402    properly. E.g.,
1403
1404      .i32.i32.s16
1405      .s32.f32
1406      .u16
1407
1408    Can all be legally parsed by this function.
1409
1410    Fills in neon_type struct pointer with parsed information, and updates STR
1411    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1412    type, FAIL if not.  */
1413
1414 static int
1415 parse_neon_type (struct neon_type *type, char **str)
1416 {
1417   char *ptr = *str;
1418
1419   if (type)
1420     type->elems = 0;
1421
1422   while (type->elems < NEON_MAX_TYPE_ELS)
1423     {
1424       enum neon_el_type thistype = NT_untyped;
1425       unsigned thissize = -1u;
1426
1427       if (*ptr != '.')
1428         break;
1429
1430       ptr++;
1431
1432       /* Just a size without an explicit type.  */
1433       if (ISDIGIT (*ptr))
1434         goto parsesize;
1435
1436       switch (TOLOWER (*ptr))
1437         {
1438         case 'i': thistype = NT_integer; break;
1439         case 'f': thistype = NT_float; break;
1440         case 'p': thistype = NT_poly; break;
1441         case 's': thistype = NT_signed; break;
1442         case 'u': thistype = NT_unsigned; break;
1443         case 'd':
1444           thistype = NT_float;
1445           thissize = 64;
1446           ptr++;
1447           goto done;
1448         default:
1449           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1450           return FAIL;
1451         }
1452
1453       ptr++;
1454
1455       /* .f is an abbreviation for .f32.  */
1456       if (thistype == NT_float && !ISDIGIT (*ptr))
1457         thissize = 32;
1458       else
1459         {
1460         parsesize:
1461           thissize = strtoul (ptr, &ptr, 10);
1462
1463           if (thissize != 8 && thissize != 16 && thissize != 32
1464               && thissize != 64)
1465             {
1466               as_bad (_("bad size %d in type specifier"), thissize);
1467               return FAIL;
1468             }
1469         }
1470
1471       done:
1472       if (type)
1473         {
1474           type->el[type->elems].type = thistype;
1475           type->el[type->elems].size = thissize;
1476           type->elems++;
1477         }
1478     }
1479
1480   /* Empty/missing type is not a successful parse.  */
1481   if (type->elems == 0)
1482     return FAIL;
1483
1484   *str = ptr;
1485
1486   return SUCCESS;
1487 }
1488
1489 /* Errors may be set multiple times during parsing or bit encoding
1490    (particularly in the Neon bits), but usually the earliest error which is set
1491    will be the most meaningful. Avoid overwriting it with later (cascading)
1492    errors by calling this function.  */
1493
1494 static void
1495 first_error (const char *err)
1496 {
1497   if (!inst.error)
1498     inst.error = err;
1499 }
1500
1501 /* Parse a single type, e.g. ".s32", leading period included.  */
1502 static int
1503 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1504 {
1505   char *str = *ccp;
1506   struct neon_type optype;
1507
1508   if (*str == '.')
1509     {
1510       if (parse_neon_type (&optype, &str) == SUCCESS)
1511         {
1512           if (optype.elems == 1)
1513             *vectype = optype.el[0];
1514           else
1515             {
1516               first_error (_("only one type should be specified for operand"));
1517               return FAIL;
1518             }
1519         }
1520       else
1521         {
1522           first_error (_("vector type expected"));
1523           return FAIL;
1524         }
1525     }
1526   else
1527     return FAIL;
1528
1529   *ccp = str;
1530
1531   return SUCCESS;
1532 }
1533
1534 /* Special meanings for indices (which have a range of 0-7), which will fit into
1535    a 4-bit integer.  */
1536
1537 #define NEON_ALL_LANES          15
1538 #define NEON_INTERLEAVE_LANES   14
1539
1540 /* Record a use of the given feature.  */
1541 static void
1542 record_feature_use (const arm_feature_set *feature)
1543 {
1544   if (thumb_mode)
1545     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
1546   else
1547     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
1548 }
1549
1550 /* If the given feature available in the selected CPU, mark it as used.
1551    Returns TRUE iff feature is available.  */
1552 static bfd_boolean
1553 mark_feature_used (const arm_feature_set *feature)
1554 {
1555
1556   /* Do not support the use of MVE only instructions when in auto-detection or
1557      -march=all.  */
1558   if (((feature == &mve_ext) || (feature == &mve_fp_ext))
1559       && ARM_CPU_IS_ANY (cpu_variant))
1560     {
1561       first_error (BAD_MVE_AUTO);
1562       return FALSE;
1563     }
1564   /* Ensure the option is valid on the current architecture.  */
1565   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
1566     return FALSE;
1567
1568   /* Add the appropriate architecture feature for the barrier option used.
1569      */
1570   record_feature_use (feature);
1571
1572   return TRUE;
1573 }
1574
1575 /* Parse either a register or a scalar, with an optional type. Return the
1576    register number, and optionally fill in the actual type of the register
1577    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1578    type/index information in *TYPEINFO.  */
1579
1580 static int
1581 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1582                            enum arm_reg_type *rtype,
1583                            struct neon_typed_alias *typeinfo)
1584 {
1585   char *str = *ccp;
1586   struct reg_entry *reg = arm_reg_parse_multi (&str);
1587   struct neon_typed_alias atype;
1588   struct neon_type_el parsetype;
1589
1590   atype.defined = 0;
1591   atype.index = -1;
1592   atype.eltype.type = NT_invtype;
1593   atype.eltype.size = -1;
1594
1595   /* Try alternate syntax for some types of register. Note these are mutually
1596      exclusive with the Neon syntax extensions.  */
1597   if (reg == NULL)
1598     {
1599       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1600       if (altreg != FAIL)
1601         *ccp = str;
1602       if (typeinfo)
1603         *typeinfo = atype;
1604       return altreg;
1605     }
1606
1607   /* Undo polymorphism when a set of register types may be accepted.  */
1608   if ((type == REG_TYPE_NDQ
1609        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1610       || (type == REG_TYPE_VFSD
1611           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1612       || (type == REG_TYPE_NSDQ
1613           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1614               || reg->type == REG_TYPE_NQ))
1615       || (type == REG_TYPE_NSD
1616           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1617       || (type == REG_TYPE_MMXWC
1618           && (reg->type == REG_TYPE_MMXWCG)))
1619     type = (enum arm_reg_type) reg->type;
1620
1621   if (type == REG_TYPE_MQ)
1622     {
1623       if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1624         return FAIL;
1625
1626       if (!reg || reg->type != REG_TYPE_NQ)
1627         return FAIL;
1628
1629       if (reg->number > 14 && !mark_feature_used (&fpu_vfp_ext_d32))
1630         {
1631           first_error (_("expected MVE register [q0..q7]"));
1632           return FAIL;
1633         }
1634       type = REG_TYPE_NQ;
1635     }
1636   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
1637            && (type == REG_TYPE_NQ))
1638     return FAIL;
1639
1640
1641   if (type != reg->type)
1642     return FAIL;
1643
1644   if (reg->neon)
1645     atype = *reg->neon;
1646
1647   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1648     {
1649       if ((atype.defined & NTA_HASTYPE) != 0)
1650         {
1651           first_error (_("can't redefine type for operand"));
1652           return FAIL;
1653         }
1654       atype.defined |= NTA_HASTYPE;
1655       atype.eltype = parsetype;
1656     }
1657
1658   if (skip_past_char (&str, '[') == SUCCESS)
1659     {
1660       if (type != REG_TYPE_VFD
1661           && !(type == REG_TYPE_VFS
1662                && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_2))
1663           && !(type == REG_TYPE_NQ
1664                && ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)))
1665         {
1666           if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1667             first_error (_("only D and Q registers may be indexed"));
1668           else
1669             first_error (_("only D registers may be indexed"));
1670           return FAIL;
1671         }
1672
1673       if ((atype.defined & NTA_HASINDEX) != 0)
1674         {
1675           first_error (_("can't change index for operand"));
1676           return FAIL;
1677         }
1678
1679       atype.defined |= NTA_HASINDEX;
1680
1681       if (skip_past_char (&str, ']') == SUCCESS)
1682         atype.index = NEON_ALL_LANES;
1683       else
1684         {
1685           expressionS exp;
1686
1687           my_get_expression (&exp, &str, GE_NO_PREFIX);
1688
1689           if (exp.X_op != O_constant)
1690             {
1691               first_error (_("constant expression required"));
1692               return FAIL;
1693             }
1694
1695           if (skip_past_char (&str, ']') == FAIL)
1696             return FAIL;
1697
1698           atype.index = exp.X_add_number;
1699         }
1700     }
1701
1702   if (typeinfo)
1703     *typeinfo = atype;
1704
1705   if (rtype)
1706     *rtype = type;
1707
1708   *ccp = str;
1709
1710   return reg->number;
1711 }
1712
1713 /* Like arm_reg_parse, but also allow the following extra features:
1714     - If RTYPE is non-zero, return the (possibly restricted) type of the
1715       register (e.g. Neon double or quad reg when either has been requested).
1716     - If this is a Neon vector type with additional type information, fill
1717       in the struct pointed to by VECTYPE (if non-NULL).
1718    This function will fault on encountering a scalar.  */
1719
1720 static int
1721 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1722                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1723 {
1724   struct neon_typed_alias atype;
1725   char *str = *ccp;
1726   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1727
1728   if (reg == FAIL)
1729     return FAIL;
1730
1731   /* Do not allow regname(... to parse as a register.  */
1732   if (*str == '(')
1733     return FAIL;
1734
1735   /* Do not allow a scalar (reg+index) to parse as a register.  */
1736   if ((atype.defined & NTA_HASINDEX) != 0)
1737     {
1738       first_error (_("register operand expected, but got scalar"));
1739       return FAIL;
1740     }
1741
1742   if (vectype)
1743     *vectype = atype.eltype;
1744
1745   *ccp = str;
1746
1747   return reg;
1748 }
1749
1750 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1751 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1752
1753 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1754    have enough information to be able to do a good job bounds-checking. So, we
1755    just do easy checks here, and do further checks later.  */
1756
1757 static int
1758 parse_scalar (char **ccp, int elsize, struct neon_type_el *type, enum
1759               arm_reg_type reg_type)
1760 {
1761   int reg;
1762   char *str = *ccp;
1763   struct neon_typed_alias atype;
1764   unsigned reg_size;
1765
1766   reg = parse_typed_reg_or_scalar (&str, reg_type, NULL, &atype);
1767
1768   switch (reg_type)
1769     {
1770     case REG_TYPE_VFS:
1771       reg_size = 32;
1772       break;
1773     case REG_TYPE_VFD:
1774       reg_size = 64;
1775       break;
1776     case REG_TYPE_MQ:
1777       reg_size = 128;
1778       break;
1779     default:
1780       gas_assert (0);
1781       return FAIL;
1782     }
1783
1784   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1785     return FAIL;
1786
1787   if (reg_type != REG_TYPE_MQ && atype.index == NEON_ALL_LANES)
1788     {
1789       first_error (_("scalar must have an index"));
1790       return FAIL;
1791     }
1792   else if (atype.index >= reg_size / elsize)
1793     {
1794       first_error (_("scalar index out of range"));
1795       return FAIL;
1796     }
1797
1798   if (type)
1799     *type = atype.eltype;
1800
1801   *ccp = str;
1802
1803   return reg * 16 + atype.index;
1804 }
1805
1806 /* Types of registers in a list.  */
1807
1808 enum reg_list_els
1809 {
1810   REGLIST_RN,
1811   REGLIST_CLRM,
1812   REGLIST_VFP_S,
1813   REGLIST_VFP_S_VPR,
1814   REGLIST_VFP_D,
1815   REGLIST_VFP_D_VPR,
1816   REGLIST_NEON_D
1817 };
1818
1819 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1820
1821 static long
1822 parse_reg_list (char ** strp, enum reg_list_els etype)
1823 {
1824   char *str = *strp;
1825   long range = 0;
1826   int another_range;
1827
1828   gas_assert (etype == REGLIST_RN || etype == REGLIST_CLRM);
1829
1830   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1831   do
1832     {
1833       skip_whitespace (str);
1834
1835       another_range = 0;
1836
1837       if (*str == '{')
1838         {
1839           int in_range = 0;
1840           int cur_reg = -1;
1841
1842           str++;
1843           do
1844             {
1845               int reg;
1846               const char apsr_str[] = "apsr";
1847               int apsr_str_len = strlen (apsr_str);
1848
1849               reg = arm_reg_parse (&str, REGLIST_RN);
1850               if (etype == REGLIST_CLRM)
1851                 {
1852                   if (reg == REG_SP || reg == REG_PC)
1853                     reg = FAIL;
1854                   else if (reg == FAIL
1855                            && !strncasecmp (str, apsr_str, apsr_str_len)
1856                            && !ISALPHA (*(str + apsr_str_len)))
1857                     {
1858                       reg = 15;
1859                       str += apsr_str_len;
1860                     }
1861
1862                   if (reg == FAIL)
1863                     {
1864                       first_error (_("r0-r12, lr or APSR expected"));
1865                       return FAIL;
1866                     }
1867                 }
1868               else /* etype == REGLIST_RN.  */
1869                 {
1870                   if (reg == FAIL)
1871                     {
1872                       first_error (_(reg_expected_msgs[REGLIST_RN]));
1873                       return FAIL;
1874                     }
1875                 }
1876
1877               if (in_range)
1878                 {
1879                   int i;
1880
1881                   if (reg <= cur_reg)
1882                     {
1883                       first_error (_("bad range in register list"));
1884                       return FAIL;
1885                     }
1886
1887                   for (i = cur_reg + 1; i < reg; i++)
1888                     {
1889                       if (range & (1 << i))
1890                         as_tsktsk
1891                           (_("Warning: duplicated register (r%d) in register list"),
1892                            i);
1893                       else
1894                         range |= 1 << i;
1895                     }
1896                   in_range = 0;
1897                 }
1898
1899               if (range & (1 << reg))
1900                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1901                            reg);
1902               else if (reg <= cur_reg)
1903                 as_tsktsk (_("Warning: register range not in ascending order"));
1904
1905               range |= 1 << reg;
1906               cur_reg = reg;
1907             }
1908           while (skip_past_comma (&str) != FAIL
1909                  || (in_range = 1, *str++ == '-'));
1910           str--;
1911
1912           if (skip_past_char (&str, '}') == FAIL)
1913             {
1914               first_error (_("missing `}'"));
1915               return FAIL;
1916             }
1917         }
1918       else if (etype == REGLIST_RN)
1919         {
1920           expressionS exp;
1921
1922           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1923             return FAIL;
1924
1925           if (exp.X_op == O_constant)
1926             {
1927               if (exp.X_add_number
1928                   != (exp.X_add_number & 0x0000ffff))
1929                 {
1930                   inst.error = _("invalid register mask");
1931                   return FAIL;
1932                 }
1933
1934               if ((range & exp.X_add_number) != 0)
1935                 {
1936                   int regno = range & exp.X_add_number;
1937
1938                   regno &= -regno;
1939                   regno = (1 << regno) - 1;
1940                   as_tsktsk
1941                     (_("Warning: duplicated register (r%d) in register list"),
1942                      regno);
1943                 }
1944
1945               range |= exp.X_add_number;
1946             }
1947           else
1948             {
1949               if (inst.relocs[0].type != 0)
1950                 {
1951                   inst.error = _("expression too complex");
1952                   return FAIL;
1953                 }
1954
1955               memcpy (&inst.relocs[0].exp, &exp, sizeof (expressionS));
1956               inst.relocs[0].type = BFD_RELOC_ARM_MULTI;
1957               inst.relocs[0].pc_rel = 0;
1958             }
1959         }
1960
1961       if (*str == '|' || *str == '+')
1962         {
1963           str++;
1964           another_range = 1;
1965         }
1966     }
1967   while (another_range);
1968
1969   *strp = str;
1970   return range;
1971 }
1972
1973 /* Parse a VFP register list.  If the string is invalid return FAIL.
1974    Otherwise return the number of registers, and set PBASE to the first
1975    register.  Parses registers of type ETYPE.
1976    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1977      - Q registers can be used to specify pairs of D registers
1978      - { } can be omitted from around a singleton register list
1979          FIXME: This is not implemented, as it would require backtracking in
1980          some cases, e.g.:
1981            vtbl.8 d3,d4,d5
1982          This could be done (the meaning isn't really ambiguous), but doesn't
1983          fit in well with the current parsing framework.
1984      - 32 D registers may be used (also true for VFPv3).
1985    FIXME: Types are ignored in these register lists, which is probably a
1986    bug.  */
1987
1988 static int
1989 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype,
1990                     bfd_boolean *partial_match)
1991 {
1992   char *str = *ccp;
1993   int base_reg;
1994   int new_base;
1995   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1996   int max_regs = 0;
1997   int count = 0;
1998   int warned = 0;
1999   unsigned long mask = 0;
2000   int i;
2001   bfd_boolean vpr_seen = FALSE;
2002   bfd_boolean expect_vpr =
2003     (etype == REGLIST_VFP_S_VPR) || (etype == REGLIST_VFP_D_VPR);
2004
2005   if (skip_past_char (&str, '{') == FAIL)
2006     {
2007       inst.error = _("expecting {");
2008       return FAIL;
2009     }
2010
2011   switch (etype)
2012     {
2013     case REGLIST_VFP_S:
2014     case REGLIST_VFP_S_VPR:
2015       regtype = REG_TYPE_VFS;
2016       max_regs = 32;
2017       break;
2018
2019     case REGLIST_VFP_D:
2020     case REGLIST_VFP_D_VPR:
2021       regtype = REG_TYPE_VFD;
2022       break;
2023
2024     case REGLIST_NEON_D:
2025       regtype = REG_TYPE_NDQ;
2026       break;
2027
2028     default:
2029       gas_assert (0);
2030     }
2031
2032   if (etype != REGLIST_VFP_S && etype != REGLIST_VFP_S_VPR)
2033     {
2034       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
2035       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
2036         {
2037           max_regs = 32;
2038           if (thumb_mode)
2039             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
2040                                     fpu_vfp_ext_d32);
2041           else
2042             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
2043                                     fpu_vfp_ext_d32);
2044         }
2045       else
2046         max_regs = 16;
2047     }
2048
2049   base_reg = max_regs;
2050   *partial_match = FALSE;
2051
2052   do
2053     {
2054       int setmask = 1, addregs = 1;
2055       const char vpr_str[] = "vpr";
2056       int vpr_str_len = strlen (vpr_str);
2057
2058       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
2059
2060       if (expect_vpr)
2061         {
2062           if (new_base == FAIL
2063               && !strncasecmp (str, vpr_str, vpr_str_len)
2064               && !ISALPHA (*(str + vpr_str_len))
2065               && !vpr_seen)
2066             {
2067               vpr_seen = TRUE;
2068               str += vpr_str_len;
2069               if (count == 0)
2070                 base_reg = 0; /* Canonicalize VPR only on d0 with 0 regs.  */
2071             }
2072           else if (vpr_seen)
2073             {
2074               first_error (_("VPR expected last"));
2075               return FAIL;
2076             }
2077           else if (new_base == FAIL)
2078             {
2079               if (regtype == REG_TYPE_VFS)
2080                 first_error (_("VFP single precision register or VPR "
2081                                "expected"));
2082               else /* regtype == REG_TYPE_VFD.  */
2083                 first_error (_("VFP/Neon double precision register or VPR "
2084                                "expected"));
2085               return FAIL;
2086             }
2087         }
2088       else if (new_base == FAIL)
2089         {
2090           first_error (_(reg_expected_msgs[regtype]));
2091           return FAIL;
2092         }
2093
2094       *partial_match = TRUE;
2095       if (vpr_seen)
2096         continue;
2097
2098       if (new_base >= max_regs)
2099         {
2100           first_error (_("register out of range in list"));
2101           return FAIL;
2102         }
2103
2104       /* Note: a value of 2 * n is returned for the register Q<n>.  */
2105       if (regtype == REG_TYPE_NQ)
2106         {
2107           setmask = 3;
2108           addregs = 2;
2109         }
2110
2111       if (new_base < base_reg)
2112         base_reg = new_base;
2113
2114       if (mask & (setmask << new_base))
2115         {
2116           first_error (_("invalid register list"));
2117           return FAIL;
2118         }
2119
2120       if ((mask >> new_base) != 0 && ! warned && !vpr_seen)
2121         {
2122           as_tsktsk (_("register list not in ascending order"));
2123           warned = 1;
2124         }
2125
2126       mask |= setmask << new_base;
2127       count += addregs;
2128
2129       if (*str == '-') /* We have the start of a range expression */
2130         {
2131           int high_range;
2132
2133           str++;
2134
2135           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
2136               == FAIL)
2137             {
2138               inst.error = gettext (reg_expected_msgs[regtype]);
2139               return FAIL;
2140             }
2141
2142           if (high_range >= max_regs)
2143             {
2144               first_error (_("register out of range in list"));
2145               return FAIL;
2146             }
2147
2148           if (regtype == REG_TYPE_NQ)
2149             high_range = high_range + 1;
2150
2151           if (high_range <= new_base)
2152             {
2153               inst.error = _("register range not in ascending order");
2154               return FAIL;
2155             }
2156
2157           for (new_base += addregs; new_base <= high_range; new_base += addregs)
2158             {
2159               if (mask & (setmask << new_base))
2160                 {
2161                   inst.error = _("invalid register list");
2162                   return FAIL;
2163                 }
2164
2165               mask |= setmask << new_base;
2166               count += addregs;
2167             }
2168         }
2169     }
2170   while (skip_past_comma (&str) != FAIL);
2171
2172   str++;
2173
2174   /* Sanity check -- should have raised a parse error above.  */
2175   if ((!vpr_seen && count == 0) || count > max_regs)
2176     abort ();
2177
2178   *pbase = base_reg;
2179
2180   if (expect_vpr && !vpr_seen)
2181     {
2182       first_error (_("VPR expected last"));
2183       return FAIL;
2184     }
2185
2186   /* Final test -- the registers must be consecutive.  */
2187   mask >>= base_reg;
2188   for (i = 0; i < count; i++)
2189     {
2190       if ((mask & (1u << i)) == 0)
2191         {
2192           inst.error = _("non-contiguous register range");
2193           return FAIL;
2194         }
2195     }
2196
2197   *ccp = str;
2198
2199   return count;
2200 }
2201
2202 /* True if two alias types are the same.  */
2203
2204 static bfd_boolean
2205 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
2206 {
2207   if (!a && !b)
2208     return TRUE;
2209
2210   if (!a || !b)
2211     return FALSE;
2212
2213   if (a->defined != b->defined)
2214     return FALSE;
2215
2216   if ((a->defined & NTA_HASTYPE) != 0
2217       && (a->eltype.type != b->eltype.type
2218           || a->eltype.size != b->eltype.size))
2219     return FALSE;
2220
2221   if ((a->defined & NTA_HASINDEX) != 0
2222       && (a->index != b->index))
2223     return FALSE;
2224
2225   return TRUE;
2226 }
2227
2228 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
2229    The base register is put in *PBASE.
2230    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
2231    the return value.
2232    The register stride (minus one) is put in bit 4 of the return value.
2233    Bits [6:5] encode the list length (minus one).
2234    The type of the list elements is put in *ELTYPE, if non-NULL.  */
2235
2236 #define NEON_LANE(X)            ((X) & 0xf)
2237 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
2238 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2239
2240 static int
2241 parse_neon_el_struct_list (char **str, unsigned *pbase,
2242                            int mve,
2243                            struct neon_type_el *eltype)
2244 {
2245   char *ptr = *str;
2246   int base_reg = -1;
2247   int reg_incr = -1;
2248   int count = 0;
2249   int lane = -1;
2250   int leading_brace = 0;
2251   enum arm_reg_type rtype = REG_TYPE_NDQ;
2252   const char *const incr_error = mve ? _("register stride must be 1") :
2253     _("register stride must be 1 or 2");
2254   const char *const type_error = _("mismatched element/structure types in list");
2255   struct neon_typed_alias firsttype;
2256   firsttype.defined = 0;
2257   firsttype.eltype.type = NT_invtype;
2258   firsttype.eltype.size = -1;
2259   firsttype.index = -1;
2260
2261   if (skip_past_char (&ptr, '{') == SUCCESS)
2262     leading_brace = 1;
2263
2264   do
2265     {
2266       struct neon_typed_alias atype;
2267       if (mve)
2268         rtype = REG_TYPE_MQ;
2269       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2270
2271       if (getreg == FAIL)
2272         {
2273           first_error (_(reg_expected_msgs[rtype]));
2274           return FAIL;
2275         }
2276
2277       if (base_reg == -1)
2278         {
2279           base_reg = getreg;
2280           if (rtype == REG_TYPE_NQ)
2281             {
2282               reg_incr = 1;
2283             }
2284           firsttype = atype;
2285         }
2286       else if (reg_incr == -1)
2287         {
2288           reg_incr = getreg - base_reg;
2289           if (reg_incr < 1 || reg_incr > 2)
2290             {
2291               first_error (_(incr_error));
2292               return FAIL;
2293             }
2294         }
2295       else if (getreg != base_reg + reg_incr * count)
2296         {
2297           first_error (_(incr_error));
2298           return FAIL;
2299         }
2300
2301       if (! neon_alias_types_same (&atype, &firsttype))
2302         {
2303           first_error (_(type_error));
2304           return FAIL;
2305         }
2306
2307       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2308          modes.  */
2309       if (ptr[0] == '-')
2310         {
2311           struct neon_typed_alias htype;
2312           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2313           if (lane == -1)
2314             lane = NEON_INTERLEAVE_LANES;
2315           else if (lane != NEON_INTERLEAVE_LANES)
2316             {
2317               first_error (_(type_error));
2318               return FAIL;
2319             }
2320           if (reg_incr == -1)
2321             reg_incr = 1;
2322           else if (reg_incr != 1)
2323             {
2324               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2325               return FAIL;
2326             }
2327           ptr++;
2328           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2329           if (hireg == FAIL)
2330             {
2331               first_error (_(reg_expected_msgs[rtype]));
2332               return FAIL;
2333             }
2334           if (! neon_alias_types_same (&htype, &firsttype))
2335             {
2336               first_error (_(type_error));
2337               return FAIL;
2338             }
2339           count += hireg + dregs - getreg;
2340           continue;
2341         }
2342
2343       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2344       if (rtype == REG_TYPE_NQ)
2345         {
2346           count += 2;
2347           continue;
2348         }
2349
2350       if ((atype.defined & NTA_HASINDEX) != 0)
2351         {
2352           if (lane == -1)
2353             lane = atype.index;
2354           else if (lane != atype.index)
2355             {
2356               first_error (_(type_error));
2357               return FAIL;
2358             }
2359         }
2360       else if (lane == -1)
2361         lane = NEON_INTERLEAVE_LANES;
2362       else if (lane != NEON_INTERLEAVE_LANES)
2363         {
2364           first_error (_(type_error));
2365           return FAIL;
2366         }
2367       count++;
2368     }
2369   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2370
2371   /* No lane set by [x]. We must be interleaving structures.  */
2372   if (lane == -1)
2373     lane = NEON_INTERLEAVE_LANES;
2374
2375   /* Sanity check.  */
2376   if (lane == -1 || base_reg == -1 || count < 1 || (!mve && count > 4)
2377       || (count > 1 && reg_incr == -1))
2378     {
2379       first_error (_("error parsing element/structure list"));
2380       return FAIL;
2381     }
2382
2383   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2384     {
2385       first_error (_("expected }"));
2386       return FAIL;
2387     }
2388
2389   if (reg_incr == -1)
2390     reg_incr = 1;
2391
2392   if (eltype)
2393     *eltype = firsttype.eltype;
2394
2395   *pbase = base_reg;
2396   *str = ptr;
2397
2398   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2399 }
2400
2401 /* Parse an explicit relocation suffix on an expression.  This is
2402    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2403    arm_reloc_hsh contains no entries, so this function can only
2404    succeed if there is no () after the word.  Returns -1 on error,
2405    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2406
2407 static int
2408 parse_reloc (char **str)
2409 {
2410   struct reloc_entry *r;
2411   char *p, *q;
2412
2413   if (**str != '(')
2414     return BFD_RELOC_UNUSED;
2415
2416   p = *str + 1;
2417   q = p;
2418
2419   while (*q && *q != ')' && *q != ',')
2420     q++;
2421   if (*q != ')')
2422     return -1;
2423
2424   if ((r = (struct reloc_entry *)
2425        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2426     return -1;
2427
2428   *str = q + 1;
2429   return r->reloc;
2430 }
2431
2432 /* Directives: register aliases.  */
2433
2434 static struct reg_entry *
2435 insert_reg_alias (char *str, unsigned number, int type)
2436 {
2437   struct reg_entry *new_reg;
2438   const char *name;
2439
2440   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2441     {
2442       if (new_reg->builtin)
2443         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2444
2445       /* Only warn about a redefinition if it's not defined as the
2446          same register.  */
2447       else if (new_reg->number != number || new_reg->type != type)
2448         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2449
2450       return NULL;
2451     }
2452
2453   name = xstrdup (str);
2454   new_reg = XNEW (struct reg_entry);
2455
2456   new_reg->name = name;
2457   new_reg->number = number;
2458   new_reg->type = type;
2459   new_reg->builtin = FALSE;
2460   new_reg->neon = NULL;
2461
2462   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2463     abort ();
2464
2465   return new_reg;
2466 }
2467
2468 static void
2469 insert_neon_reg_alias (char *str, int number, int type,
2470                        struct neon_typed_alias *atype)
2471 {
2472   struct reg_entry *reg = insert_reg_alias (str, number, type);
2473
2474   if (!reg)
2475     {
2476       first_error (_("attempt to redefine typed alias"));
2477       return;
2478     }
2479
2480   if (atype)
2481     {
2482       reg->neon = XNEW (struct neon_typed_alias);
2483       *reg->neon = *atype;
2484     }
2485 }
2486
2487 /* Look for the .req directive.  This is of the form:
2488
2489         new_register_name .req existing_register_name
2490
2491    If we find one, or if it looks sufficiently like one that we want to
2492    handle any error here, return TRUE.  Otherwise return FALSE.  */
2493
2494 static bfd_boolean
2495 create_register_alias (char * newname, char *p)
2496 {
2497   struct reg_entry *old;
2498   char *oldname, *nbuf;
2499   size_t nlen;
2500
2501   /* The input scrubber ensures that whitespace after the mnemonic is
2502      collapsed to single spaces.  */
2503   oldname = p;
2504   if (strncmp (oldname, " .req ", 6) != 0)
2505     return FALSE;
2506
2507   oldname += 6;
2508   if (*oldname == '\0')
2509     return FALSE;
2510
2511   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2512   if (!old)
2513     {
2514       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2515       return TRUE;
2516     }
2517
2518   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2519      the desired alias name, and p points to its end.  If not, then
2520      the desired alias name is in the global original_case_string.  */
2521 #ifdef TC_CASE_SENSITIVE
2522   nlen = p - newname;
2523 #else
2524   newname = original_case_string;
2525   nlen = strlen (newname);
2526 #endif
2527
2528   nbuf = xmemdup0 (newname, nlen);
2529
2530   /* Create aliases under the new name as stated; an all-lowercase
2531      version of the new name; and an all-uppercase version of the new
2532      name.  */
2533   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2534     {
2535       for (p = nbuf; *p; p++)
2536         *p = TOUPPER (*p);
2537
2538       if (strncmp (nbuf, newname, nlen))
2539         {
2540           /* If this attempt to create an additional alias fails, do not bother
2541              trying to create the all-lower case alias.  We will fail and issue
2542              a second, duplicate error message.  This situation arises when the
2543              programmer does something like:
2544                foo .req r0
2545                Foo .req r1
2546              The second .req creates the "Foo" alias but then fails to create
2547              the artificial FOO alias because it has already been created by the
2548              first .req.  */
2549           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2550             {
2551               free (nbuf);
2552               return TRUE;
2553             }
2554         }
2555
2556       for (p = nbuf; *p; p++)
2557         *p = TOLOWER (*p);
2558
2559       if (strncmp (nbuf, newname, nlen))
2560         insert_reg_alias (nbuf, old->number, old->type);
2561     }
2562
2563   free (nbuf);
2564   return TRUE;
2565 }
2566
2567 /* Create a Neon typed/indexed register alias using directives, e.g.:
2568      X .dn d5.s32[1]
2569      Y .qn 6.s16
2570      Z .dn d7
2571      T .dn Z[0]
2572    These typed registers can be used instead of the types specified after the
2573    Neon mnemonic, so long as all operands given have types. Types can also be
2574    specified directly, e.g.:
2575      vadd d0.s32, d1.s32, d2.s32  */
2576
2577 static bfd_boolean
2578 create_neon_reg_alias (char *newname, char *p)
2579 {
2580   enum arm_reg_type basetype;
2581   struct reg_entry *basereg;
2582   struct reg_entry mybasereg;
2583   struct neon_type ntype;
2584   struct neon_typed_alias typeinfo;
2585   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2586   int namelen;
2587
2588   typeinfo.defined = 0;
2589   typeinfo.eltype.type = NT_invtype;
2590   typeinfo.eltype.size = -1;
2591   typeinfo.index = -1;
2592
2593   nameend = p;
2594
2595   if (strncmp (p, " .dn ", 5) == 0)
2596     basetype = REG_TYPE_VFD;
2597   else if (strncmp (p, " .qn ", 5) == 0)
2598     basetype = REG_TYPE_NQ;
2599   else
2600     return FALSE;
2601
2602   p += 5;
2603
2604   if (*p == '\0')
2605     return FALSE;
2606
2607   basereg = arm_reg_parse_multi (&p);
2608
2609   if (basereg && basereg->type != basetype)
2610     {
2611       as_bad (_("bad type for register"));
2612       return FALSE;
2613     }
2614
2615   if (basereg == NULL)
2616     {
2617       expressionS exp;
2618       /* Try parsing as an integer.  */
2619       my_get_expression (&exp, &p, GE_NO_PREFIX);
2620       if (exp.X_op != O_constant)
2621         {
2622           as_bad (_("expression must be constant"));
2623           return FALSE;
2624         }
2625       basereg = &mybasereg;
2626       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2627                                                   : exp.X_add_number;
2628       basereg->neon = 0;
2629     }
2630
2631   if (basereg->neon)
2632     typeinfo = *basereg->neon;
2633
2634   if (parse_neon_type (&ntype, &p) == SUCCESS)
2635     {
2636       /* We got a type.  */
2637       if (typeinfo.defined & NTA_HASTYPE)
2638         {
2639           as_bad (_("can't redefine the type of a register alias"));
2640           return FALSE;
2641         }
2642
2643       typeinfo.defined |= NTA_HASTYPE;
2644       if (ntype.elems != 1)
2645         {
2646           as_bad (_("you must specify a single type only"));
2647           return FALSE;
2648         }
2649       typeinfo.eltype = ntype.el[0];
2650     }
2651
2652   if (skip_past_char (&p, '[') == SUCCESS)
2653     {
2654       expressionS exp;
2655       /* We got a scalar index.  */
2656
2657       if (typeinfo.defined & NTA_HASINDEX)
2658         {
2659           as_bad (_("can't redefine the index of a scalar alias"));
2660           return FALSE;
2661         }
2662
2663       my_get_expression (&exp, &p, GE_NO_PREFIX);
2664
2665       if (exp.X_op != O_constant)
2666         {
2667           as_bad (_("scalar index must be constant"));
2668           return FALSE;
2669         }
2670
2671       typeinfo.defined |= NTA_HASINDEX;
2672       typeinfo.index = exp.X_add_number;
2673
2674       if (skip_past_char (&p, ']') == FAIL)
2675         {
2676           as_bad (_("expecting ]"));
2677           return FALSE;
2678         }
2679     }
2680
2681   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2682      the desired alias name, and p points to its end.  If not, then
2683      the desired alias name is in the global original_case_string.  */
2684 #ifdef TC_CASE_SENSITIVE
2685   namelen = nameend - newname;
2686 #else
2687   newname = original_case_string;
2688   namelen = strlen (newname);
2689 #endif
2690
2691   namebuf = xmemdup0 (newname, namelen);
2692
2693   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2694                          typeinfo.defined != 0 ? &typeinfo : NULL);
2695
2696   /* Insert name in all uppercase.  */
2697   for (p = namebuf; *p; p++)
2698     *p = TOUPPER (*p);
2699
2700   if (strncmp (namebuf, newname, namelen))
2701     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2702                            typeinfo.defined != 0 ? &typeinfo : NULL);
2703
2704   /* Insert name in all lowercase.  */
2705   for (p = namebuf; *p; p++)
2706     *p = TOLOWER (*p);
2707
2708   if (strncmp (namebuf, newname, namelen))
2709     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2710                            typeinfo.defined != 0 ? &typeinfo : NULL);
2711
2712   free (namebuf);
2713   return TRUE;
2714 }
2715
2716 /* Should never be called, as .req goes between the alias and the
2717    register name, not at the beginning of the line.  */
2718
2719 static void
2720 s_req (int a ATTRIBUTE_UNUSED)
2721 {
2722   as_bad (_("invalid syntax for .req directive"));
2723 }
2724
2725 static void
2726 s_dn (int a ATTRIBUTE_UNUSED)
2727 {
2728   as_bad (_("invalid syntax for .dn directive"));
2729 }
2730
2731 static void
2732 s_qn (int a ATTRIBUTE_UNUSED)
2733 {
2734   as_bad (_("invalid syntax for .qn directive"));
2735 }
2736
2737 /* The .unreq directive deletes an alias which was previously defined
2738    by .req.  For example:
2739
2740        my_alias .req r11
2741        .unreq my_alias    */
2742
2743 static void
2744 s_unreq (int a ATTRIBUTE_UNUSED)
2745 {
2746   char * name;
2747   char saved_char;
2748
2749   name = input_line_pointer;
2750
2751   while (*input_line_pointer != 0
2752          && *input_line_pointer != ' '
2753          && *input_line_pointer != '\n')
2754     ++input_line_pointer;
2755
2756   saved_char = *input_line_pointer;
2757   *input_line_pointer = 0;
2758
2759   if (!*name)
2760     as_bad (_("invalid syntax for .unreq directive"));
2761   else
2762     {
2763       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2764                                                               name);
2765
2766       if (!reg)
2767         as_bad (_("unknown register alias '%s'"), name);
2768       else if (reg->builtin)
2769         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2770                  name);
2771       else
2772         {
2773           char * p;
2774           char * nbuf;
2775
2776           hash_delete (arm_reg_hsh, name, FALSE);
2777           free ((char *) reg->name);
2778           if (reg->neon)
2779             free (reg->neon);
2780           free (reg);
2781
2782           /* Also locate the all upper case and all lower case versions.
2783              Do not complain if we cannot find one or the other as it
2784              was probably deleted above.  */
2785
2786           nbuf = strdup (name);
2787           for (p = nbuf; *p; p++)
2788             *p = TOUPPER (*p);
2789           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2790           if (reg)
2791             {
2792               hash_delete (arm_reg_hsh, nbuf, FALSE);
2793               free ((char *) reg->name);
2794               if (reg->neon)
2795                 free (reg->neon);
2796               free (reg);
2797             }
2798
2799           for (p = nbuf; *p; p++)
2800             *p = TOLOWER (*p);
2801           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2802           if (reg)
2803             {
2804               hash_delete (arm_reg_hsh, nbuf, FALSE);
2805               free ((char *) reg->name);
2806               if (reg->neon)
2807                 free (reg->neon);
2808               free (reg);
2809             }
2810
2811           free (nbuf);
2812         }
2813     }
2814
2815   *input_line_pointer = saved_char;
2816   demand_empty_rest_of_line ();
2817 }
2818
2819 /* Directives: Instruction set selection.  */
2820
2821 #ifdef OBJ_ELF
2822 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2823    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2824    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2825    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2826
2827 /* Create a new mapping symbol for the transition to STATE.  */
2828
2829 static void
2830 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2831 {
2832   symbolS * symbolP;
2833   const char * symname;
2834   int type;
2835
2836   switch (state)
2837     {
2838     case MAP_DATA:
2839       symname = "$d";
2840       type = BSF_NO_FLAGS;
2841       break;
2842     case MAP_ARM:
2843       symname = "$a";
2844       type = BSF_NO_FLAGS;
2845       break;
2846     case MAP_THUMB:
2847       symname = "$t";
2848       type = BSF_NO_FLAGS;
2849       break;
2850     default:
2851       abort ();
2852     }
2853
2854   symbolP = symbol_new (symname, now_seg, value, frag);
2855   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2856
2857   switch (state)
2858     {
2859     case MAP_ARM:
2860       THUMB_SET_FUNC (symbolP, 0);
2861       ARM_SET_THUMB (symbolP, 0);
2862       ARM_SET_INTERWORK (symbolP, support_interwork);
2863       break;
2864
2865     case MAP_THUMB:
2866       THUMB_SET_FUNC (symbolP, 1);
2867       ARM_SET_THUMB (symbolP, 1);
2868       ARM_SET_INTERWORK (symbolP, support_interwork);
2869       break;
2870
2871     case MAP_DATA:
2872     default:
2873       break;
2874     }
2875
2876   /* Save the mapping symbols for future reference.  Also check that
2877      we do not place two mapping symbols at the same offset within a
2878      frag.  We'll handle overlap between frags in
2879      check_mapping_symbols.
2880
2881      If .fill or other data filling directive generates zero sized data,
2882      the mapping symbol for the following code will have the same value
2883      as the one generated for the data filling directive.  In this case,
2884      we replace the old symbol with the new one at the same address.  */
2885   if (value == 0)
2886     {
2887       if (frag->tc_frag_data.first_map != NULL)
2888         {
2889           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2890           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2891         }
2892       frag->tc_frag_data.first_map = symbolP;
2893     }
2894   if (frag->tc_frag_data.last_map != NULL)
2895     {
2896       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2897       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2898         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2899     }
2900   frag->tc_frag_data.last_map = symbolP;
2901 }
2902
2903 /* We must sometimes convert a region marked as code to data during
2904    code alignment, if an odd number of bytes have to be padded.  The
2905    code mapping symbol is pushed to an aligned address.  */
2906
2907 static void
2908 insert_data_mapping_symbol (enum mstate state,
2909                             valueT value, fragS *frag, offsetT bytes)
2910 {
2911   /* If there was already a mapping symbol, remove it.  */
2912   if (frag->tc_frag_data.last_map != NULL
2913       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2914     {
2915       symbolS *symp = frag->tc_frag_data.last_map;
2916
2917       if (value == 0)
2918         {
2919           know (frag->tc_frag_data.first_map == symp);
2920           frag->tc_frag_data.first_map = NULL;
2921         }
2922       frag->tc_frag_data.last_map = NULL;
2923       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2924     }
2925
2926   make_mapping_symbol (MAP_DATA, value, frag);
2927   make_mapping_symbol (state, value + bytes, frag);
2928 }
2929
2930 static void mapping_state_2 (enum mstate state, int max_chars);
2931
2932 /* Set the mapping state to STATE.  Only call this when about to
2933    emit some STATE bytes to the file.  */
2934
2935 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2936 void
2937 mapping_state (enum mstate state)
2938 {
2939   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2940
2941   if (mapstate == state)
2942     /* The mapping symbol has already been emitted.
2943        There is nothing else to do.  */
2944     return;
2945
2946   if (state == MAP_ARM || state == MAP_THUMB)
2947     /*  PR gas/12931
2948         All ARM instructions require 4-byte alignment.
2949         (Almost) all Thumb instructions require 2-byte alignment.
2950
2951         When emitting instructions into any section, mark the section
2952         appropriately.
2953
2954         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2955         but themselves require 2-byte alignment; this applies to some
2956         PC- relative forms.  However, these cases will involve implicit
2957         literal pool generation or an explicit .align >=2, both of
2958         which will cause the section to me marked with sufficient
2959         alignment.  Thus, we don't handle those cases here.  */
2960     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2961
2962   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2963     /* This case will be evaluated later.  */
2964     return;
2965
2966   mapping_state_2 (state, 0);
2967 }
2968
2969 /* Same as mapping_state, but MAX_CHARS bytes have already been
2970    allocated.  Put the mapping symbol that far back.  */
2971
2972 static void
2973 mapping_state_2 (enum mstate state, int max_chars)
2974 {
2975   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2976
2977   if (!SEG_NORMAL (now_seg))
2978     return;
2979
2980   if (mapstate == state)
2981     /* The mapping symbol has already been emitted.
2982        There is nothing else to do.  */
2983     return;
2984
2985   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2986           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2987     {
2988       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2989       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2990
2991       if (add_symbol)
2992         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2993     }
2994
2995   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2996   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2997 }
2998 #undef TRANSITION
2999 #else
3000 #define mapping_state(x) ((void)0)
3001 #define mapping_state_2(x, y) ((void)0)
3002 #endif
3003
3004 /* Find the real, Thumb encoded start of a Thumb function.  */
3005
3006 #ifdef OBJ_COFF
3007 static symbolS *
3008 find_real_start (symbolS * symbolP)
3009 {
3010   char *       real_start;
3011   const char * name = S_GET_NAME (symbolP);
3012   symbolS *    new_target;
3013
3014   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
3015 #define STUB_NAME ".real_start_of"
3016
3017   if (name == NULL)
3018     abort ();
3019
3020   /* The compiler may generate BL instructions to local labels because
3021      it needs to perform a branch to a far away location. These labels
3022      do not have a corresponding ".real_start_of" label.  We check
3023      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
3024      the ".real_start_of" convention for nonlocal branches.  */
3025   if (S_IS_LOCAL (symbolP) || name[0] == '.')
3026     return symbolP;
3027
3028   real_start = concat (STUB_NAME, name, NULL);
3029   new_target = symbol_find (real_start);
3030   free (real_start);
3031
3032   if (new_target == NULL)
3033     {
3034       as_warn (_("Failed to find real start of function: %s\n"), name);
3035       new_target = symbolP;
3036     }
3037
3038   return new_target;
3039 }
3040 #endif
3041
3042 static void
3043 opcode_select (int width)
3044 {
3045   switch (width)
3046     {
3047     case 16:
3048       if (! thumb_mode)
3049         {
3050           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
3051             as_bad (_("selected processor does not support THUMB opcodes"));
3052
3053           thumb_mode = 1;
3054           /* No need to force the alignment, since we will have been
3055              coming from ARM mode, which is word-aligned.  */
3056           record_alignment (now_seg, 1);
3057         }
3058       break;
3059
3060     case 32:
3061       if (thumb_mode)
3062         {
3063           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
3064             as_bad (_("selected processor does not support ARM opcodes"));
3065
3066           thumb_mode = 0;
3067
3068           if (!need_pass_2)
3069             frag_align (2, 0, 0);
3070
3071           record_alignment (now_seg, 1);
3072         }
3073       break;
3074
3075     default:
3076       as_bad (_("invalid instruction size selected (%d)"), width);
3077     }
3078 }
3079
3080 static void
3081 s_arm (int ignore ATTRIBUTE_UNUSED)
3082 {
3083   opcode_select (32);
3084   demand_empty_rest_of_line ();
3085 }
3086
3087 static void
3088 s_thumb (int ignore ATTRIBUTE_UNUSED)
3089 {
3090   opcode_select (16);
3091   demand_empty_rest_of_line ();
3092 }
3093
3094 static void
3095 s_code (int unused ATTRIBUTE_UNUSED)
3096 {
3097   int temp;
3098
3099   temp = get_absolute_expression ();
3100   switch (temp)
3101     {
3102     case 16:
3103     case 32:
3104       opcode_select (temp);
3105       break;
3106
3107     default:
3108       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
3109     }
3110 }
3111
3112 static void
3113 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
3114 {
3115   /* If we are not already in thumb mode go into it, EVEN if
3116      the target processor does not support thumb instructions.
3117      This is used by gcc/config/arm/lib1funcs.asm for example
3118      to compile interworking support functions even if the
3119      target processor should not support interworking.  */
3120   if (! thumb_mode)
3121     {
3122       thumb_mode = 2;
3123       record_alignment (now_seg, 1);
3124     }
3125
3126   demand_empty_rest_of_line ();
3127 }
3128
3129 static void
3130 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
3131 {
3132   s_thumb (0);
3133
3134   /* The following label is the name/address of the start of a Thumb function.
3135      We need to know this for the interworking support.  */
3136   label_is_thumb_function_name = TRUE;
3137 }
3138
3139 /* Perform a .set directive, but also mark the alias as
3140    being a thumb function.  */
3141
3142 static void
3143 s_thumb_set (int equiv)
3144 {
3145   /* XXX the following is a duplicate of the code for s_set() in read.c
3146      We cannot just call that code as we need to get at the symbol that
3147      is created.  */
3148   char *    name;
3149   char      delim;
3150   char *    end_name;
3151   symbolS * symbolP;
3152
3153   /* Especial apologies for the random logic:
3154      This just grew, and could be parsed much more simply!
3155      Dean - in haste.  */
3156   delim     = get_symbol_name (& name);
3157   end_name  = input_line_pointer;
3158   (void) restore_line_pointer (delim);
3159
3160   if (*input_line_pointer != ',')
3161     {
3162       *end_name = 0;
3163       as_bad (_("expected comma after name \"%s\""), name);
3164       *end_name = delim;
3165       ignore_rest_of_line ();
3166       return;
3167     }
3168
3169   input_line_pointer++;
3170   *end_name = 0;
3171
3172   if (name[0] == '.' && name[1] == '\0')
3173     {
3174       /* XXX - this should not happen to .thumb_set.  */
3175       abort ();
3176     }
3177
3178   if ((symbolP = symbol_find (name)) == NULL
3179       && (symbolP = md_undefined_symbol (name)) == NULL)
3180     {
3181 #ifndef NO_LISTING
3182       /* When doing symbol listings, play games with dummy fragments living
3183          outside the normal fragment chain to record the file and line info
3184          for this symbol.  */
3185       if (listing & LISTING_SYMBOLS)
3186         {
3187           extern struct list_info_struct * listing_tail;
3188           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
3189
3190           memset (dummy_frag, 0, sizeof (fragS));
3191           dummy_frag->fr_type = rs_fill;
3192           dummy_frag->line = listing_tail;
3193           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
3194           dummy_frag->fr_symbol = symbolP;
3195         }
3196       else
3197 #endif
3198         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
3199
3200 #ifdef OBJ_COFF
3201       /* "set" symbols are local unless otherwise specified.  */
3202       SF_SET_LOCAL (symbolP);
3203 #endif /* OBJ_COFF  */
3204     }                           /* Make a new symbol.  */
3205
3206   symbol_table_insert (symbolP);
3207
3208   * end_name = delim;
3209
3210   if (equiv
3211       && S_IS_DEFINED (symbolP)
3212       && S_GET_SEGMENT (symbolP) != reg_section)
3213     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
3214
3215   pseudo_set (symbolP);
3216
3217   demand_empty_rest_of_line ();
3218
3219   /* XXX Now we come to the Thumb specific bit of code.  */
3220
3221   THUMB_SET_FUNC (symbolP, 1);
3222   ARM_SET_THUMB (symbolP, 1);
3223 #if defined OBJ_ELF || defined OBJ_COFF
3224   ARM_SET_INTERWORK (symbolP, support_interwork);
3225 #endif
3226 }
3227
3228 /* Directives: Mode selection.  */
3229
3230 /* .syntax [unified|divided] - choose the new unified syntax
3231    (same for Arm and Thumb encoding, modulo slight differences in what
3232    can be represented) or the old divergent syntax for each mode.  */
3233 static void
3234 s_syntax (int unused ATTRIBUTE_UNUSED)
3235 {
3236   char *name, delim;
3237
3238   delim = get_symbol_name (& name);
3239
3240   if (!strcasecmp (name, "unified"))
3241     unified_syntax = TRUE;
3242   else if (!strcasecmp (name, "divided"))
3243     unified_syntax = FALSE;
3244   else
3245     {
3246       as_bad (_("unrecognized syntax mode \"%s\""), name);
3247       return;
3248     }
3249   (void) restore_line_pointer (delim);
3250   demand_empty_rest_of_line ();
3251 }
3252
3253 /* Directives: sectioning and alignment.  */
3254
3255 static void
3256 s_bss (int ignore ATTRIBUTE_UNUSED)
3257 {
3258   /* We don't support putting frags in the BSS segment, we fake it by
3259      marking in_bss, then looking at s_skip for clues.  */
3260   subseg_set (bss_section, 0);
3261   demand_empty_rest_of_line ();
3262
3263 #ifdef md_elf_section_change_hook
3264   md_elf_section_change_hook ();
3265 #endif
3266 }
3267
3268 static void
3269 s_even (int ignore ATTRIBUTE_UNUSED)
3270 {
3271   /* Never make frag if expect extra pass.  */
3272   if (!need_pass_2)
3273     frag_align (1, 0, 0);
3274
3275   record_alignment (now_seg, 1);
3276
3277   demand_empty_rest_of_line ();
3278 }
3279
3280 /* Directives: CodeComposer Studio.  */
3281
3282 /*  .ref  (for CodeComposer Studio syntax only).  */
3283 static void
3284 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3285 {
3286   if (codecomposer_syntax)
3287     ignore_rest_of_line ();
3288   else
3289     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3290 }
3291
3292 /*  If name is not NULL, then it is used for marking the beginning of a
3293     function, whereas if it is NULL then it means the function end.  */
3294 static void
3295 asmfunc_debug (const char * name)
3296 {
3297   static const char * last_name = NULL;
3298
3299   if (name != NULL)
3300     {
3301       gas_assert (last_name == NULL);
3302       last_name = name;
3303
3304       if (debug_type == DEBUG_STABS)
3305          stabs_generate_asm_func (name, name);
3306     }
3307   else
3308     {
3309       gas_assert (last_name != NULL);
3310
3311       if (debug_type == DEBUG_STABS)
3312         stabs_generate_asm_endfunc (last_name, last_name);
3313
3314       last_name = NULL;
3315     }
3316 }
3317
3318 static void
3319 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3320 {
3321   if (codecomposer_syntax)
3322     {
3323       switch (asmfunc_state)
3324         {
3325         case OUTSIDE_ASMFUNC:
3326           asmfunc_state = WAITING_ASMFUNC_NAME;
3327           break;
3328
3329         case WAITING_ASMFUNC_NAME:
3330           as_bad (_(".asmfunc repeated."));
3331           break;
3332
3333         case WAITING_ENDASMFUNC:
3334           as_bad (_(".asmfunc without function."));
3335           break;
3336         }
3337       demand_empty_rest_of_line ();
3338     }
3339   else
3340     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3341 }
3342
3343 static void
3344 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3345 {
3346   if (codecomposer_syntax)
3347     {
3348       switch (asmfunc_state)
3349         {
3350         case OUTSIDE_ASMFUNC:
3351           as_bad (_(".endasmfunc without a .asmfunc."));
3352           break;
3353
3354         case WAITING_ASMFUNC_NAME:
3355           as_bad (_(".endasmfunc without function."));
3356           break;
3357
3358         case WAITING_ENDASMFUNC:
3359           asmfunc_state = OUTSIDE_ASMFUNC;
3360           asmfunc_debug (NULL);
3361           break;
3362         }
3363       demand_empty_rest_of_line ();
3364     }
3365   else
3366     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3367 }
3368
3369 static void
3370 s_ccs_def (int name)
3371 {
3372   if (codecomposer_syntax)
3373     s_globl (name);
3374   else
3375     as_bad (_(".def pseudo-op only available with -mccs flag."));
3376 }
3377
3378 /* Directives: Literal pools.  */
3379
3380 static literal_pool *
3381 find_literal_pool (void)
3382 {
3383   literal_pool * pool;
3384
3385   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3386     {
3387       if (pool->section == now_seg
3388           && pool->sub_section == now_subseg)
3389         break;
3390     }
3391
3392   return pool;
3393 }
3394
3395 static literal_pool *
3396 find_or_make_literal_pool (void)
3397 {
3398   /* Next literal pool ID number.  */
3399   static unsigned int latest_pool_num = 1;
3400   literal_pool *      pool;
3401
3402   pool = find_literal_pool ();
3403
3404   if (pool == NULL)
3405     {
3406       /* Create a new pool.  */
3407       pool = XNEW (literal_pool);
3408       if (! pool)
3409         return NULL;
3410
3411       pool->next_free_entry = 0;
3412       pool->section         = now_seg;
3413       pool->sub_section     = now_subseg;
3414       pool->next            = list_of_pools;
3415       pool->symbol          = NULL;
3416       pool->alignment       = 2;
3417
3418       /* Add it to the list.  */
3419       list_of_pools = pool;
3420     }
3421
3422   /* New pools, and emptied pools, will have a NULL symbol.  */
3423   if (pool->symbol == NULL)
3424     {
3425       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3426                                     (valueT) 0, &zero_address_frag);
3427       pool->id = latest_pool_num ++;
3428     }
3429
3430   /* Done.  */
3431   return pool;
3432 }
3433
3434 /* Add the literal in the global 'inst'
3435    structure to the relevant literal pool.  */
3436
3437 static int
3438 add_to_lit_pool (unsigned int nbytes)
3439 {
3440 #define PADDING_SLOT 0x1
3441 #define LIT_ENTRY_SIZE_MASK 0xFF
3442   literal_pool * pool;
3443   unsigned int entry, pool_size = 0;
3444   bfd_boolean padding_slot_p = FALSE;
3445   unsigned imm1 = 0;
3446   unsigned imm2 = 0;
3447
3448   if (nbytes == 8)
3449     {
3450       imm1 = inst.operands[1].imm;
3451       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3452                : inst.relocs[0].exp.X_unsigned ? 0
3453                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3454       if (target_big_endian)
3455         {
3456           imm1 = imm2;
3457           imm2 = inst.operands[1].imm;
3458         }
3459     }
3460
3461   pool = find_or_make_literal_pool ();
3462
3463   /* Check if this literal value is already in the pool.  */
3464   for (entry = 0; entry < pool->next_free_entry; entry ++)
3465     {
3466       if (nbytes == 4)
3467         {
3468           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3469               && (inst.relocs[0].exp.X_op == O_constant)
3470               && (pool->literals[entry].X_add_number
3471                   == inst.relocs[0].exp.X_add_number)
3472               && (pool->literals[entry].X_md == nbytes)
3473               && (pool->literals[entry].X_unsigned
3474                   == inst.relocs[0].exp.X_unsigned))
3475             break;
3476
3477           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3478               && (inst.relocs[0].exp.X_op == O_symbol)
3479               && (pool->literals[entry].X_add_number
3480                   == inst.relocs[0].exp.X_add_number)
3481               && (pool->literals[entry].X_add_symbol
3482                   == inst.relocs[0].exp.X_add_symbol)
3483               && (pool->literals[entry].X_op_symbol
3484                   == inst.relocs[0].exp.X_op_symbol)
3485               && (pool->literals[entry].X_md == nbytes))
3486             break;
3487         }
3488       else if ((nbytes == 8)
3489                && !(pool_size & 0x7)
3490                && ((entry + 1) != pool->next_free_entry)
3491                && (pool->literals[entry].X_op == O_constant)
3492                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3493                && (pool->literals[entry].X_unsigned
3494                    == inst.relocs[0].exp.X_unsigned)
3495                && (pool->literals[entry + 1].X_op == O_constant)
3496                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3497                && (pool->literals[entry + 1].X_unsigned
3498                    == inst.relocs[0].exp.X_unsigned))
3499         break;
3500
3501       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3502       if (padding_slot_p && (nbytes == 4))
3503         break;
3504
3505       pool_size += 4;
3506     }
3507
3508   /* Do we need to create a new entry?  */
3509   if (entry == pool->next_free_entry)
3510     {
3511       if (entry >= MAX_LITERAL_POOL_SIZE)
3512         {
3513           inst.error = _("literal pool overflow");
3514           return FAIL;
3515         }
3516
3517       if (nbytes == 8)
3518         {
3519           /* For 8-byte entries, we align to an 8-byte boundary,
3520              and split it into two 4-byte entries, because on 32-bit
3521              host, 8-byte constants are treated as big num, thus
3522              saved in "generic_bignum" which will be overwritten
3523              by later assignments.
3524
3525              We also need to make sure there is enough space for
3526              the split.
3527
3528              We also check to make sure the literal operand is a
3529              constant number.  */
3530           if (!(inst.relocs[0].exp.X_op == O_constant
3531                 || inst.relocs[0].exp.X_op == O_big))
3532             {
3533               inst.error = _("invalid type for literal pool");
3534               return FAIL;
3535             }
3536           else if (pool_size & 0x7)
3537             {
3538               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3539                 {
3540                   inst.error = _("literal pool overflow");
3541                   return FAIL;
3542                 }
3543
3544               pool->literals[entry] = inst.relocs[0].exp;
3545               pool->literals[entry].X_op = O_constant;
3546               pool->literals[entry].X_add_number = 0;
3547               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3548               pool->next_free_entry += 1;
3549               pool_size += 4;
3550             }
3551           else if ((entry + 1) >= 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 = imm1;
3560           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3561           pool->literals[entry++].X_md = 4;
3562           pool->literals[entry] = inst.relocs[0].exp;
3563           pool->literals[entry].X_op = O_constant;
3564           pool->literals[entry].X_add_number = imm2;
3565           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3566           pool->literals[entry].X_md = 4;
3567           pool->alignment = 3;
3568           pool->next_free_entry += 1;
3569         }
3570       else
3571         {
3572           pool->literals[entry] = inst.relocs[0].exp;
3573           pool->literals[entry].X_md = 4;
3574         }
3575
3576 #ifdef OBJ_ELF
3577       /* PR ld/12974: Record the location of the first source line to reference
3578          this entry in the literal pool.  If it turns out during linking that the
3579          symbol does not exist we will be able to give an accurate line number for
3580          the (first use of the) missing reference.  */
3581       if (debug_type == DEBUG_DWARF2)
3582         dwarf2_where (pool->locs + entry);
3583 #endif
3584       pool->next_free_entry += 1;
3585     }
3586   else if (padding_slot_p)
3587     {
3588       pool->literals[entry] = inst.relocs[0].exp;
3589       pool->literals[entry].X_md = nbytes;
3590     }
3591
3592   inst.relocs[0].exp.X_op             = O_symbol;
3593   inst.relocs[0].exp.X_add_number = pool_size;
3594   inst.relocs[0].exp.X_add_symbol = pool->symbol;
3595
3596   return SUCCESS;
3597 }
3598
3599 bfd_boolean
3600 tc_start_label_without_colon (void)
3601 {
3602   bfd_boolean ret = TRUE;
3603
3604   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3605     {
3606       const char *label = input_line_pointer;
3607
3608       while (!is_end_of_line[(int) label[-1]])
3609         --label;
3610
3611       if (*label == '.')
3612         {
3613           as_bad (_("Invalid label '%s'"), label);
3614           ret = FALSE;
3615         }
3616
3617       asmfunc_debug (label);
3618
3619       asmfunc_state = WAITING_ENDASMFUNC;
3620     }
3621
3622   return ret;
3623 }
3624
3625 /* Can't use symbol_new here, so have to create a symbol and then at
3626    a later date assign it a value. That's what these functions do.  */
3627
3628 static void
3629 symbol_locate (symbolS *    symbolP,
3630                const char * name,       /* It is copied, the caller can modify.  */
3631                segT         segment,    /* Segment identifier (SEG_<something>).  */
3632                valueT       valu,       /* Symbol value.  */
3633                fragS *      frag)       /* Associated fragment.  */
3634 {
3635   size_t name_length;
3636   char * preserved_copy_of_name;
3637
3638   name_length = strlen (name) + 1;   /* +1 for \0.  */
3639   obstack_grow (&notes, name, name_length);
3640   preserved_copy_of_name = (char *) obstack_finish (&notes);
3641
3642 #ifdef tc_canonicalize_symbol_name
3643   preserved_copy_of_name =
3644     tc_canonicalize_symbol_name (preserved_copy_of_name);
3645 #endif
3646
3647   S_SET_NAME (symbolP, preserved_copy_of_name);
3648
3649   S_SET_SEGMENT (symbolP, segment);
3650   S_SET_VALUE (symbolP, valu);
3651   symbol_clear_list_pointers (symbolP);
3652
3653   symbol_set_frag (symbolP, frag);
3654
3655   /* Link to end of symbol chain.  */
3656   {
3657     extern int symbol_table_frozen;
3658
3659     if (symbol_table_frozen)
3660       abort ();
3661   }
3662
3663   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3664
3665   obj_symbol_new_hook (symbolP);
3666
3667 #ifdef tc_symbol_new_hook
3668   tc_symbol_new_hook (symbolP);
3669 #endif
3670
3671 #ifdef DEBUG_SYMS
3672   verify_symbol_chain (symbol_rootP, symbol_lastP);
3673 #endif /* DEBUG_SYMS  */
3674 }
3675
3676 static void
3677 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3678 {
3679   unsigned int entry;
3680   literal_pool * pool;
3681   char sym_name[20];
3682
3683   pool = find_literal_pool ();
3684   if (pool == NULL
3685       || pool->symbol == NULL
3686       || pool->next_free_entry == 0)
3687     return;
3688
3689   /* Align pool as you have word accesses.
3690      Only make a frag if we have to.  */
3691   if (!need_pass_2)
3692     frag_align (pool->alignment, 0, 0);
3693
3694   record_alignment (now_seg, 2);
3695
3696 #ifdef OBJ_ELF
3697   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3698   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3699 #endif
3700   sprintf (sym_name, "$$lit_\002%x", pool->id);
3701
3702   symbol_locate (pool->symbol, sym_name, now_seg,
3703                  (valueT) frag_now_fix (), frag_now);
3704   symbol_table_insert (pool->symbol);
3705
3706   ARM_SET_THUMB (pool->symbol, thumb_mode);
3707
3708 #if defined OBJ_COFF || defined OBJ_ELF
3709   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3710 #endif
3711
3712   for (entry = 0; entry < pool->next_free_entry; entry ++)
3713     {
3714 #ifdef OBJ_ELF
3715       if (debug_type == DEBUG_DWARF2)
3716         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3717 #endif
3718       /* First output the expression in the instruction to the pool.  */
3719       emit_expr (&(pool->literals[entry]),
3720                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3721     }
3722
3723   /* Mark the pool as empty.  */
3724   pool->next_free_entry = 0;
3725   pool->symbol = NULL;
3726 }
3727
3728 #ifdef OBJ_ELF
3729 /* Forward declarations for functions below, in the MD interface
3730    section.  */
3731 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3732 static valueT create_unwind_entry (int);
3733 static void start_unwind_section (const segT, int);
3734 static void add_unwind_opcode (valueT, int);
3735 static void flush_pending_unwind (void);
3736
3737 /* Directives: Data.  */
3738
3739 static void
3740 s_arm_elf_cons (int nbytes)
3741 {
3742   expressionS exp;
3743
3744 #ifdef md_flush_pending_output
3745   md_flush_pending_output ();
3746 #endif
3747
3748   if (is_it_end_of_statement ())
3749     {
3750       demand_empty_rest_of_line ();
3751       return;
3752     }
3753
3754 #ifdef md_cons_align
3755   md_cons_align (nbytes);
3756 #endif
3757
3758   mapping_state (MAP_DATA);
3759   do
3760     {
3761       int reloc;
3762       char *base = input_line_pointer;
3763
3764       expression (& exp);
3765
3766       if (exp.X_op != O_symbol)
3767         emit_expr (&exp, (unsigned int) nbytes);
3768       else
3769         {
3770           char *before_reloc = input_line_pointer;
3771           reloc = parse_reloc (&input_line_pointer);
3772           if (reloc == -1)
3773             {
3774               as_bad (_("unrecognized relocation suffix"));
3775               ignore_rest_of_line ();
3776               return;
3777             }
3778           else if (reloc == BFD_RELOC_UNUSED)
3779             emit_expr (&exp, (unsigned int) nbytes);
3780           else
3781             {
3782               reloc_howto_type *howto = (reloc_howto_type *)
3783                   bfd_reloc_type_lookup (stdoutput,
3784                                          (bfd_reloc_code_real_type) reloc);
3785               int size = bfd_get_reloc_size (howto);
3786
3787               if (reloc == BFD_RELOC_ARM_PLT32)
3788                 {
3789                   as_bad (_("(plt) is only valid on branch targets"));
3790                   reloc = BFD_RELOC_UNUSED;
3791                   size = 0;
3792                 }
3793
3794               if (size > nbytes)
3795                 as_bad (ngettext ("%s relocations do not fit in %d byte",
3796                                   "%s relocations do not fit in %d bytes",
3797                                   nbytes),
3798                         howto->name, nbytes);
3799               else
3800                 {
3801                   /* We've parsed an expression stopping at O_symbol.
3802                      But there may be more expression left now that we
3803                      have parsed the relocation marker.  Parse it again.
3804                      XXX Surely there is a cleaner way to do this.  */
3805                   char *p = input_line_pointer;
3806                   int offset;
3807                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3808
3809                   memcpy (save_buf, base, input_line_pointer - base);
3810                   memmove (base + (input_line_pointer - before_reloc),
3811                            base, before_reloc - base);
3812
3813                   input_line_pointer = base + (input_line_pointer-before_reloc);
3814                   expression (&exp);
3815                   memcpy (base, save_buf, p - base);
3816
3817                   offset = nbytes - size;
3818                   p = frag_more (nbytes);
3819                   memset (p, 0, nbytes);
3820                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3821                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3822                   free (save_buf);
3823                 }
3824             }
3825         }
3826     }
3827   while (*input_line_pointer++ == ',');
3828
3829   /* Put terminator back into stream.  */
3830   input_line_pointer --;
3831   demand_empty_rest_of_line ();
3832 }
3833
3834 /* Emit an expression containing a 32-bit thumb instruction.
3835    Implementation based on put_thumb32_insn.  */
3836
3837 static void
3838 emit_thumb32_expr (expressionS * exp)
3839 {
3840   expressionS exp_high = *exp;
3841
3842   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3843   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3844   exp->X_add_number &= 0xffff;
3845   emit_expr (exp, (unsigned int) THUMB_SIZE);
3846 }
3847
3848 /*  Guess the instruction size based on the opcode.  */
3849
3850 static int
3851 thumb_insn_size (int opcode)
3852 {
3853   if ((unsigned int) opcode < 0xe800u)
3854     return 2;
3855   else if ((unsigned int) opcode >= 0xe8000000u)
3856     return 4;
3857   else
3858     return 0;
3859 }
3860
3861 static bfd_boolean
3862 emit_insn (expressionS *exp, int nbytes)
3863 {
3864   int size = 0;
3865
3866   if (exp->X_op == O_constant)
3867     {
3868       size = nbytes;
3869
3870       if (size == 0)
3871         size = thumb_insn_size (exp->X_add_number);
3872
3873       if (size != 0)
3874         {
3875           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3876             {
3877               as_bad (_(".inst.n operand too big. "\
3878                         "Use .inst.w instead"));
3879               size = 0;
3880             }
3881           else
3882             {
3883               if (now_pred.state == AUTOMATIC_PRED_BLOCK)
3884                 set_pred_insn_type_nonvoid (OUTSIDE_PRED_INSN, 0);
3885               else
3886                 set_pred_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3887
3888               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3889                 emit_thumb32_expr (exp);
3890               else
3891                 emit_expr (exp, (unsigned int) size);
3892
3893               it_fsm_post_encode ();
3894             }
3895         }
3896       else
3897         as_bad (_("cannot determine Thumb instruction size. "   \
3898                   "Use .inst.n/.inst.w instead"));
3899     }
3900   else
3901     as_bad (_("constant expression required"));
3902
3903   return (size != 0);
3904 }
3905
3906 /* Like s_arm_elf_cons but do not use md_cons_align and
3907    set the mapping state to MAP_ARM/MAP_THUMB.  */
3908
3909 static void
3910 s_arm_elf_inst (int nbytes)
3911 {
3912   if (is_it_end_of_statement ())
3913     {
3914       demand_empty_rest_of_line ();
3915       return;
3916     }
3917
3918   /* Calling mapping_state () here will not change ARM/THUMB,
3919      but will ensure not to be in DATA state.  */
3920
3921   if (thumb_mode)
3922     mapping_state (MAP_THUMB);
3923   else
3924     {
3925       if (nbytes != 0)
3926         {
3927           as_bad (_("width suffixes are invalid in ARM mode"));
3928           ignore_rest_of_line ();
3929           return;
3930         }
3931
3932       nbytes = 4;
3933
3934       mapping_state (MAP_ARM);
3935     }
3936
3937   do
3938     {
3939       expressionS exp;
3940
3941       expression (& exp);
3942
3943       if (! emit_insn (& exp, nbytes))
3944         {
3945           ignore_rest_of_line ();
3946           return;
3947         }
3948     }
3949   while (*input_line_pointer++ == ',');
3950
3951   /* Put terminator back into stream.  */
3952   input_line_pointer --;
3953   demand_empty_rest_of_line ();
3954 }
3955
3956 /* Parse a .rel31 directive.  */
3957
3958 static void
3959 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3960 {
3961   expressionS exp;
3962   char *p;
3963   valueT highbit;
3964
3965   highbit = 0;
3966   if (*input_line_pointer == '1')
3967     highbit = 0x80000000;
3968   else if (*input_line_pointer != '0')
3969     as_bad (_("expected 0 or 1"));
3970
3971   input_line_pointer++;
3972   if (*input_line_pointer != ',')
3973     as_bad (_("missing comma"));
3974   input_line_pointer++;
3975
3976 #ifdef md_flush_pending_output
3977   md_flush_pending_output ();
3978 #endif
3979
3980 #ifdef md_cons_align
3981   md_cons_align (4);
3982 #endif
3983
3984   mapping_state (MAP_DATA);
3985
3986   expression (&exp);
3987
3988   p = frag_more (4);
3989   md_number_to_chars (p, highbit, 4);
3990   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3991                BFD_RELOC_ARM_PREL31);
3992
3993   demand_empty_rest_of_line ();
3994 }
3995
3996 /* Directives: AEABI stack-unwind tables.  */
3997
3998 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3999
4000 static void
4001 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
4002 {
4003   demand_empty_rest_of_line ();
4004   if (unwind.proc_start)
4005     {
4006       as_bad (_("duplicate .fnstart directive"));
4007       return;
4008     }
4009
4010   /* Mark the start of the function.  */
4011   unwind.proc_start = expr_build_dot ();
4012
4013   /* Reset the rest of the unwind info.  */
4014   unwind.opcode_count = 0;
4015   unwind.table_entry = NULL;
4016   unwind.personality_routine = NULL;
4017   unwind.personality_index = -1;
4018   unwind.frame_size = 0;
4019   unwind.fp_offset = 0;
4020   unwind.fp_reg = REG_SP;
4021   unwind.fp_used = 0;
4022   unwind.sp_restored = 0;
4023 }
4024
4025
4026 /* Parse a handlerdata directive.  Creates the exception handling table entry
4027    for the function.  */
4028
4029 static void
4030 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
4031 {
4032   demand_empty_rest_of_line ();
4033   if (!unwind.proc_start)
4034     as_bad (MISSING_FNSTART);
4035
4036   if (unwind.table_entry)
4037     as_bad (_("duplicate .handlerdata directive"));
4038
4039   create_unwind_entry (1);
4040 }
4041
4042 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
4043
4044 static void
4045 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
4046 {
4047   long where;
4048   char *ptr;
4049   valueT val;
4050   unsigned int marked_pr_dependency;
4051
4052   demand_empty_rest_of_line ();
4053
4054   if (!unwind.proc_start)
4055     {
4056       as_bad (_(".fnend directive without .fnstart"));
4057       return;
4058     }
4059
4060   /* Add eh table entry.  */
4061   if (unwind.table_entry == NULL)
4062     val = create_unwind_entry (0);
4063   else
4064     val = 0;
4065
4066   /* Add index table entry.  This is two words.  */
4067   start_unwind_section (unwind.saved_seg, 1);
4068   frag_align (2, 0, 0);
4069   record_alignment (now_seg, 2);
4070
4071   ptr = frag_more (8);
4072   memset (ptr, 0, 8);
4073   where = frag_now_fix () - 8;
4074
4075   /* Self relative offset of the function start.  */
4076   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
4077            BFD_RELOC_ARM_PREL31);
4078
4079   /* Indicate dependency on EHABI-defined personality routines to the
4080      linker, if it hasn't been done already.  */
4081   marked_pr_dependency
4082     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
4083   if (unwind.personality_index >= 0 && unwind.personality_index < 3
4084       && !(marked_pr_dependency & (1 << unwind.personality_index)))
4085     {
4086       static const char *const name[] =
4087         {
4088           "__aeabi_unwind_cpp_pr0",
4089           "__aeabi_unwind_cpp_pr1",
4090           "__aeabi_unwind_cpp_pr2"
4091         };
4092       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
4093       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
4094       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
4095         |= 1 << unwind.personality_index;
4096     }
4097
4098   if (val)
4099     /* Inline exception table entry.  */
4100     md_number_to_chars (ptr + 4, val, 4);
4101   else
4102     /* Self relative offset of the table entry.  */
4103     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
4104              BFD_RELOC_ARM_PREL31);
4105
4106   /* Restore the original section.  */
4107   subseg_set (unwind.saved_seg, unwind.saved_subseg);
4108
4109   unwind.proc_start = NULL;
4110 }
4111
4112
4113 /* Parse an unwind_cantunwind directive.  */
4114
4115 static void
4116 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
4117 {
4118   demand_empty_rest_of_line ();
4119   if (!unwind.proc_start)
4120     as_bad (MISSING_FNSTART);
4121
4122   if (unwind.personality_routine || unwind.personality_index != -1)
4123     as_bad (_("personality routine specified for cantunwind frame"));
4124
4125   unwind.personality_index = -2;
4126 }
4127
4128
4129 /* Parse a personalityindex directive.  */
4130
4131 static void
4132 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
4133 {
4134   expressionS exp;
4135
4136   if (!unwind.proc_start)
4137     as_bad (MISSING_FNSTART);
4138
4139   if (unwind.personality_routine || unwind.personality_index != -1)
4140     as_bad (_("duplicate .personalityindex directive"));
4141
4142   expression (&exp);
4143
4144   if (exp.X_op != O_constant
4145       || exp.X_add_number < 0 || exp.X_add_number > 15)
4146     {
4147       as_bad (_("bad personality routine number"));
4148       ignore_rest_of_line ();
4149       return;
4150     }
4151
4152   unwind.personality_index = exp.X_add_number;
4153
4154   demand_empty_rest_of_line ();
4155 }
4156
4157
4158 /* Parse a personality directive.  */
4159
4160 static void
4161 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
4162 {
4163   char *name, *p, c;
4164
4165   if (!unwind.proc_start)
4166     as_bad (MISSING_FNSTART);
4167
4168   if (unwind.personality_routine || unwind.personality_index != -1)
4169     as_bad (_("duplicate .personality directive"));
4170
4171   c = get_symbol_name (& name);
4172   p = input_line_pointer;
4173   if (c == '"')
4174     ++ input_line_pointer;
4175   unwind.personality_routine = symbol_find_or_make (name);
4176   *p = c;
4177   demand_empty_rest_of_line ();
4178 }
4179
4180
4181 /* Parse a directive saving core registers.  */
4182
4183 static void
4184 s_arm_unwind_save_core (void)
4185 {
4186   valueT op;
4187   long range;
4188   int n;
4189
4190   range = parse_reg_list (&input_line_pointer, REGLIST_RN);
4191   if (range == FAIL)
4192     {
4193       as_bad (_("expected register list"));
4194       ignore_rest_of_line ();
4195       return;
4196     }
4197
4198   demand_empty_rest_of_line ();
4199
4200   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
4201      into .unwind_save {..., sp...}.  We aren't bothered about the value of
4202      ip because it is clobbered by calls.  */
4203   if (unwind.sp_restored && unwind.fp_reg == 12
4204       && (range & 0x3000) == 0x1000)
4205     {
4206       unwind.opcode_count--;
4207       unwind.sp_restored = 0;
4208       range = (range | 0x2000) & ~0x1000;
4209       unwind.pending_offset = 0;
4210     }
4211
4212   /* Pop r4-r15.  */
4213   if (range & 0xfff0)
4214     {
4215       /* See if we can use the short opcodes.  These pop a block of up to 8
4216          registers starting with r4, plus maybe r14.  */
4217       for (n = 0; n < 8; n++)
4218         {
4219           /* Break at the first non-saved register.      */
4220           if ((range & (1 << (n + 4))) == 0)
4221             break;
4222         }
4223       /* See if there are any other bits set.  */
4224       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
4225         {
4226           /* Use the long form.  */
4227           op = 0x8000 | ((range >> 4) & 0xfff);
4228           add_unwind_opcode (op, 2);
4229         }
4230       else
4231         {
4232           /* Use the short form.  */
4233           if (range & 0x4000)
4234             op = 0xa8; /* Pop r14.      */
4235           else
4236             op = 0xa0; /* Do not pop r14.  */
4237           op |= (n - 1);
4238           add_unwind_opcode (op, 1);
4239         }
4240     }
4241
4242   /* Pop r0-r3.  */
4243   if (range & 0xf)
4244     {
4245       op = 0xb100 | (range & 0xf);
4246       add_unwind_opcode (op, 2);
4247     }
4248
4249   /* Record the number of bytes pushed.  */
4250   for (n = 0; n < 16; n++)
4251     {
4252       if (range & (1 << n))
4253         unwind.frame_size += 4;
4254     }
4255 }
4256
4257
4258 /* Parse a directive saving FPA registers.  */
4259
4260 static void
4261 s_arm_unwind_save_fpa (int reg)
4262 {
4263   expressionS exp;
4264   int num_regs;
4265   valueT op;
4266
4267   /* Get Number of registers to transfer.  */
4268   if (skip_past_comma (&input_line_pointer) != FAIL)
4269     expression (&exp);
4270   else
4271     exp.X_op = O_illegal;
4272
4273   if (exp.X_op != O_constant)
4274     {
4275       as_bad (_("expected , <constant>"));
4276       ignore_rest_of_line ();
4277       return;
4278     }
4279
4280   num_regs = exp.X_add_number;
4281
4282   if (num_regs < 1 || num_regs > 4)
4283     {
4284       as_bad (_("number of registers must be in the range [1:4]"));
4285       ignore_rest_of_line ();
4286       return;
4287     }
4288
4289   demand_empty_rest_of_line ();
4290
4291   if (reg == 4)
4292     {
4293       /* Short form.  */
4294       op = 0xb4 | (num_regs - 1);
4295       add_unwind_opcode (op, 1);
4296     }
4297   else
4298     {
4299       /* Long form.  */
4300       op = 0xc800 | (reg << 4) | (num_regs - 1);
4301       add_unwind_opcode (op, 2);
4302     }
4303   unwind.frame_size += num_regs * 12;
4304 }
4305
4306
4307 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4308
4309 static void
4310 s_arm_unwind_save_vfp_armv6 (void)
4311 {
4312   int count;
4313   unsigned int start;
4314   valueT op;
4315   int num_vfpv3_regs = 0;
4316   int num_regs_below_16;
4317   bfd_boolean partial_match;
4318
4319   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D,
4320                               &partial_match);
4321   if (count == FAIL)
4322     {
4323       as_bad (_("expected register list"));
4324       ignore_rest_of_line ();
4325       return;
4326     }
4327
4328   demand_empty_rest_of_line ();
4329
4330   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4331      than FSTMX/FLDMX-style ones).  */
4332
4333   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4334   if (start >= 16)
4335     num_vfpv3_regs = count;
4336   else if (start + count > 16)
4337     num_vfpv3_regs = start + count - 16;
4338
4339   if (num_vfpv3_regs > 0)
4340     {
4341       int start_offset = start > 16 ? start - 16 : 0;
4342       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4343       add_unwind_opcode (op, 2);
4344     }
4345
4346   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4347   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4348   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4349   if (num_regs_below_16 > 0)
4350     {
4351       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4352       add_unwind_opcode (op, 2);
4353     }
4354
4355   unwind.frame_size += count * 8;
4356 }
4357
4358
4359 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4360
4361 static void
4362 s_arm_unwind_save_vfp (void)
4363 {
4364   int count;
4365   unsigned int reg;
4366   valueT op;
4367   bfd_boolean partial_match;
4368
4369   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D,
4370                               &partial_match);
4371   if (count == FAIL)
4372     {
4373       as_bad (_("expected register list"));
4374       ignore_rest_of_line ();
4375       return;
4376     }
4377
4378   demand_empty_rest_of_line ();
4379
4380   if (reg == 8)
4381     {
4382       /* Short form.  */
4383       op = 0xb8 | (count - 1);
4384       add_unwind_opcode (op, 1);
4385     }
4386   else
4387     {
4388       /* Long form.  */
4389       op = 0xb300 | (reg << 4) | (count - 1);
4390       add_unwind_opcode (op, 2);
4391     }
4392   unwind.frame_size += count * 8 + 4;
4393 }
4394
4395
4396 /* Parse a directive saving iWMMXt data registers.  */
4397
4398 static void
4399 s_arm_unwind_save_mmxwr (void)
4400 {
4401   int reg;
4402   int hi_reg;
4403   int i;
4404   unsigned mask = 0;
4405   valueT op;
4406
4407   if (*input_line_pointer == '{')
4408     input_line_pointer++;
4409
4410   do
4411     {
4412       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4413
4414       if (reg == FAIL)
4415         {
4416           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4417           goto error;
4418         }
4419
4420       if (mask >> reg)
4421         as_tsktsk (_("register list not in ascending order"));
4422       mask |= 1 << reg;
4423
4424       if (*input_line_pointer == '-')
4425         {
4426           input_line_pointer++;
4427           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4428           if (hi_reg == FAIL)
4429             {
4430               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4431               goto error;
4432             }
4433           else if (reg >= hi_reg)
4434             {
4435               as_bad (_("bad register range"));
4436               goto error;
4437             }
4438           for (; reg < hi_reg; reg++)
4439             mask |= 1 << reg;
4440         }
4441     }
4442   while (skip_past_comma (&input_line_pointer) != FAIL);
4443
4444   skip_past_char (&input_line_pointer, '}');
4445
4446   demand_empty_rest_of_line ();
4447
4448   /* Generate any deferred opcodes because we're going to be looking at
4449      the list.  */
4450   flush_pending_unwind ();
4451
4452   for (i = 0; i < 16; i++)
4453     {
4454       if (mask & (1 << i))
4455         unwind.frame_size += 8;
4456     }
4457
4458   /* Attempt to combine with a previous opcode.  We do this because gcc
4459      likes to output separate unwind directives for a single block of
4460      registers.  */
4461   if (unwind.opcode_count > 0)
4462     {
4463       i = unwind.opcodes[unwind.opcode_count - 1];
4464       if ((i & 0xf8) == 0xc0)
4465         {
4466           i &= 7;
4467           /* Only merge if the blocks are contiguous.  */
4468           if (i < 6)
4469             {
4470               if ((mask & 0xfe00) == (1 << 9))
4471                 {
4472                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4473                   unwind.opcode_count--;
4474                 }
4475             }
4476           else if (i == 6 && unwind.opcode_count >= 2)
4477             {
4478               i = unwind.opcodes[unwind.opcode_count - 2];
4479               reg = i >> 4;
4480               i &= 0xf;
4481
4482               op = 0xffff << (reg - 1);
4483               if (reg > 0
4484                   && ((mask & op) == (1u << (reg - 1))))
4485                 {
4486                   op = (1 << (reg + i + 1)) - 1;
4487                   op &= ~((1 << reg) - 1);
4488                   mask |= op;
4489                   unwind.opcode_count -= 2;
4490                 }
4491             }
4492         }
4493     }
4494
4495   hi_reg = 15;
4496   /* We want to generate opcodes in the order the registers have been
4497      saved, ie. descending order.  */
4498   for (reg = 15; reg >= -1; reg--)
4499     {
4500       /* Save registers in blocks.  */
4501       if (reg < 0
4502           || !(mask & (1 << reg)))
4503         {
4504           /* We found an unsaved reg.  Generate opcodes to save the
4505              preceding block.   */
4506           if (reg != hi_reg)
4507             {
4508               if (reg == 9)
4509                 {
4510                   /* Short form.  */
4511                   op = 0xc0 | (hi_reg - 10);
4512                   add_unwind_opcode (op, 1);
4513                 }
4514               else
4515                 {
4516                   /* Long form.  */
4517                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4518                   add_unwind_opcode (op, 2);
4519                 }
4520             }
4521           hi_reg = reg - 1;
4522         }
4523     }
4524
4525   return;
4526 error:
4527   ignore_rest_of_line ();
4528 }
4529
4530 static void
4531 s_arm_unwind_save_mmxwcg (void)
4532 {
4533   int reg;
4534   int hi_reg;
4535   unsigned mask = 0;
4536   valueT op;
4537
4538   if (*input_line_pointer == '{')
4539     input_line_pointer++;
4540
4541   skip_whitespace (input_line_pointer);
4542
4543   do
4544     {
4545       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4546
4547       if (reg == FAIL)
4548         {
4549           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4550           goto error;
4551         }
4552
4553       reg -= 8;
4554       if (mask >> reg)
4555         as_tsktsk (_("register list not in ascending order"));
4556       mask |= 1 << reg;
4557
4558       if (*input_line_pointer == '-')
4559         {
4560           input_line_pointer++;
4561           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4562           if (hi_reg == FAIL)
4563             {
4564               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4565               goto error;
4566             }
4567           else if (reg >= hi_reg)
4568             {
4569               as_bad (_("bad register range"));
4570               goto error;
4571             }
4572           for (; reg < hi_reg; reg++)
4573             mask |= 1 << reg;
4574         }
4575     }
4576   while (skip_past_comma (&input_line_pointer) != FAIL);
4577
4578   skip_past_char (&input_line_pointer, '}');
4579
4580   demand_empty_rest_of_line ();
4581
4582   /* Generate any deferred opcodes because we're going to be looking at
4583      the list.  */
4584   flush_pending_unwind ();
4585
4586   for (reg = 0; reg < 16; reg++)
4587     {
4588       if (mask & (1 << reg))
4589         unwind.frame_size += 4;
4590     }
4591   op = 0xc700 | mask;
4592   add_unwind_opcode (op, 2);
4593   return;
4594 error:
4595   ignore_rest_of_line ();
4596 }
4597
4598
4599 /* Parse an unwind_save directive.
4600    If the argument is non-zero, this is a .vsave directive.  */
4601
4602 static void
4603 s_arm_unwind_save (int arch_v6)
4604 {
4605   char *peek;
4606   struct reg_entry *reg;
4607   bfd_boolean had_brace = FALSE;
4608
4609   if (!unwind.proc_start)
4610     as_bad (MISSING_FNSTART);
4611
4612   /* Figure out what sort of save we have.  */
4613   peek = input_line_pointer;
4614
4615   if (*peek == '{')
4616     {
4617       had_brace = TRUE;
4618       peek++;
4619     }
4620
4621   reg = arm_reg_parse_multi (&peek);
4622
4623   if (!reg)
4624     {
4625       as_bad (_("register expected"));
4626       ignore_rest_of_line ();
4627       return;
4628     }
4629
4630   switch (reg->type)
4631     {
4632     case REG_TYPE_FN:
4633       if (had_brace)
4634         {
4635           as_bad (_("FPA .unwind_save does not take a register list"));
4636           ignore_rest_of_line ();
4637           return;
4638         }
4639       input_line_pointer = peek;
4640       s_arm_unwind_save_fpa (reg->number);
4641       return;
4642
4643     case REG_TYPE_RN:
4644       s_arm_unwind_save_core ();
4645       return;
4646
4647     case REG_TYPE_VFD:
4648       if (arch_v6)
4649         s_arm_unwind_save_vfp_armv6 ();
4650       else
4651         s_arm_unwind_save_vfp ();
4652       return;
4653
4654     case REG_TYPE_MMXWR:
4655       s_arm_unwind_save_mmxwr ();
4656       return;
4657
4658     case REG_TYPE_MMXWCG:
4659       s_arm_unwind_save_mmxwcg ();
4660       return;
4661
4662     default:
4663       as_bad (_(".unwind_save does not support this kind of register"));
4664       ignore_rest_of_line ();
4665     }
4666 }
4667
4668
4669 /* Parse an unwind_movsp directive.  */
4670
4671 static void
4672 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4673 {
4674   int reg;
4675   valueT op;
4676   int offset;
4677
4678   if (!unwind.proc_start)
4679     as_bad (MISSING_FNSTART);
4680
4681   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4682   if (reg == FAIL)
4683     {
4684       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4685       ignore_rest_of_line ();
4686       return;
4687     }
4688
4689   /* Optional constant.  */
4690   if (skip_past_comma (&input_line_pointer) != FAIL)
4691     {
4692       if (immediate_for_directive (&offset) == FAIL)
4693         return;
4694     }
4695   else
4696     offset = 0;
4697
4698   demand_empty_rest_of_line ();
4699
4700   if (reg == REG_SP || reg == REG_PC)
4701     {
4702       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4703       return;
4704     }
4705
4706   if (unwind.fp_reg != REG_SP)
4707     as_bad (_("unexpected .unwind_movsp directive"));
4708
4709   /* Generate opcode to restore the value.  */
4710   op = 0x90 | reg;
4711   add_unwind_opcode (op, 1);
4712
4713   /* Record the information for later.  */
4714   unwind.fp_reg = reg;
4715   unwind.fp_offset = unwind.frame_size - offset;
4716   unwind.sp_restored = 1;
4717 }
4718
4719 /* Parse an unwind_pad directive.  */
4720
4721 static void
4722 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4723 {
4724   int offset;
4725
4726   if (!unwind.proc_start)
4727     as_bad (MISSING_FNSTART);
4728
4729   if (immediate_for_directive (&offset) == FAIL)
4730     return;
4731
4732   if (offset & 3)
4733     {
4734       as_bad (_("stack increment must be multiple of 4"));
4735       ignore_rest_of_line ();
4736       return;
4737     }
4738
4739   /* Don't generate any opcodes, just record the details for later.  */
4740   unwind.frame_size += offset;
4741   unwind.pending_offset += offset;
4742
4743   demand_empty_rest_of_line ();
4744 }
4745
4746 /* Parse an unwind_setfp directive.  */
4747
4748 static void
4749 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4750 {
4751   int sp_reg;
4752   int fp_reg;
4753   int offset;
4754
4755   if (!unwind.proc_start)
4756     as_bad (MISSING_FNSTART);
4757
4758   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4759   if (skip_past_comma (&input_line_pointer) == FAIL)
4760     sp_reg = FAIL;
4761   else
4762     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4763
4764   if (fp_reg == FAIL || sp_reg == FAIL)
4765     {
4766       as_bad (_("expected <reg>, <reg>"));
4767       ignore_rest_of_line ();
4768       return;
4769     }
4770
4771   /* Optional constant.  */
4772   if (skip_past_comma (&input_line_pointer) != FAIL)
4773     {
4774       if (immediate_for_directive (&offset) == FAIL)
4775         return;
4776     }
4777   else
4778     offset = 0;
4779
4780   demand_empty_rest_of_line ();
4781
4782   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4783     {
4784       as_bad (_("register must be either sp or set by a previous"
4785                 "unwind_movsp directive"));
4786       return;
4787     }
4788
4789   /* Don't generate any opcodes, just record the information for later.  */
4790   unwind.fp_reg = fp_reg;
4791   unwind.fp_used = 1;
4792   if (sp_reg == REG_SP)
4793     unwind.fp_offset = unwind.frame_size - offset;
4794   else
4795     unwind.fp_offset -= offset;
4796 }
4797
4798 /* Parse an unwind_raw directive.  */
4799
4800 static void
4801 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4802 {
4803   expressionS exp;
4804   /* This is an arbitrary limit.         */
4805   unsigned char op[16];
4806   int count;
4807
4808   if (!unwind.proc_start)
4809     as_bad (MISSING_FNSTART);
4810
4811   expression (&exp);
4812   if (exp.X_op == O_constant
4813       && skip_past_comma (&input_line_pointer) != FAIL)
4814     {
4815       unwind.frame_size += exp.X_add_number;
4816       expression (&exp);
4817     }
4818   else
4819     exp.X_op = O_illegal;
4820
4821   if (exp.X_op != O_constant)
4822     {
4823       as_bad (_("expected <offset>, <opcode>"));
4824       ignore_rest_of_line ();
4825       return;
4826     }
4827
4828   count = 0;
4829
4830   /* Parse the opcode.  */
4831   for (;;)
4832     {
4833       if (count >= 16)
4834         {
4835           as_bad (_("unwind opcode too long"));
4836           ignore_rest_of_line ();
4837         }
4838       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4839         {
4840           as_bad (_("invalid unwind opcode"));
4841           ignore_rest_of_line ();
4842           return;
4843         }
4844       op[count++] = exp.X_add_number;
4845
4846       /* Parse the next byte.  */
4847       if (skip_past_comma (&input_line_pointer) == FAIL)
4848         break;
4849
4850       expression (&exp);
4851     }
4852
4853   /* Add the opcode bytes in reverse order.  */
4854   while (count--)
4855     add_unwind_opcode (op[count], 1);
4856
4857   demand_empty_rest_of_line ();
4858 }
4859
4860
4861 /* Parse a .eabi_attribute directive.  */
4862
4863 static void
4864 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4865 {
4866   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4867
4868   if (tag >= 0 && tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4869     attributes_set_explicitly[tag] = 1;
4870 }
4871
4872 /* Emit a tls fix for the symbol.  */
4873
4874 static void
4875 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4876 {
4877   char *p;
4878   expressionS exp;
4879 #ifdef md_flush_pending_output
4880   md_flush_pending_output ();
4881 #endif
4882
4883 #ifdef md_cons_align
4884   md_cons_align (4);
4885 #endif
4886
4887   /* Since we're just labelling the code, there's no need to define a
4888      mapping symbol.  */
4889   expression (&exp);
4890   p = obstack_next_free (&frchain_now->frch_obstack);
4891   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4892                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4893                : BFD_RELOC_ARM_TLS_DESCSEQ);
4894 }
4895 #endif /* OBJ_ELF */
4896
4897 static void s_arm_arch (int);
4898 static void s_arm_object_arch (int);
4899 static void s_arm_cpu (int);
4900 static void s_arm_fpu (int);
4901 static void s_arm_arch_extension (int);
4902
4903 #ifdef TE_PE
4904
4905 static void
4906 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4907 {
4908   expressionS exp;
4909
4910   do
4911     {
4912       expression (&exp);
4913       if (exp.X_op == O_symbol)
4914         exp.X_op = O_secrel;
4915
4916       emit_expr (&exp, 4);
4917     }
4918   while (*input_line_pointer++ == ',');
4919
4920   input_line_pointer--;
4921   demand_empty_rest_of_line ();
4922 }
4923 #endif /* TE_PE */
4924
4925 /* This table describes all the machine specific pseudo-ops the assembler
4926    has to support.  The fields are:
4927      pseudo-op name without dot
4928      function to call to execute this pseudo-op
4929      Integer arg to pass to the function.  */
4930
4931 const pseudo_typeS md_pseudo_table[] =
4932 {
4933   /* Never called because '.req' does not start a line.  */
4934   { "req",         s_req,         0 },
4935   /* Following two are likewise never called.  */
4936   { "dn",          s_dn,          0 },
4937   { "qn",          s_qn,          0 },
4938   { "unreq",       s_unreq,       0 },
4939   { "bss",         s_bss,         0 },
4940   { "align",       s_align_ptwo,  2 },
4941   { "arm",         s_arm,         0 },
4942   { "thumb",       s_thumb,       0 },
4943   { "code",        s_code,        0 },
4944   { "force_thumb", s_force_thumb, 0 },
4945   { "thumb_func",  s_thumb_func,  0 },
4946   { "thumb_set",   s_thumb_set,   0 },
4947   { "even",        s_even,        0 },
4948   { "ltorg",       s_ltorg,       0 },
4949   { "pool",        s_ltorg,       0 },
4950   { "syntax",      s_syntax,      0 },
4951   { "cpu",         s_arm_cpu,     0 },
4952   { "arch",        s_arm_arch,    0 },
4953   { "object_arch", s_arm_object_arch,   0 },
4954   { "fpu",         s_arm_fpu,     0 },
4955   { "arch_extension", s_arm_arch_extension, 0 },
4956 #ifdef OBJ_ELF
4957   { "word",             s_arm_elf_cons, 4 },
4958   { "long",             s_arm_elf_cons, 4 },
4959   { "inst.n",           s_arm_elf_inst, 2 },
4960   { "inst.w",           s_arm_elf_inst, 4 },
4961   { "inst",             s_arm_elf_inst, 0 },
4962   { "rel31",            s_arm_rel31,      0 },
4963   { "fnstart",          s_arm_unwind_fnstart,   0 },
4964   { "fnend",            s_arm_unwind_fnend,     0 },
4965   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4966   { "personality",      s_arm_unwind_personality, 0 },
4967   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4968   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4969   { "save",             s_arm_unwind_save,      0 },
4970   { "vsave",            s_arm_unwind_save,      1 },
4971   { "movsp",            s_arm_unwind_movsp,     0 },
4972   { "pad",              s_arm_unwind_pad,       0 },
4973   { "setfp",            s_arm_unwind_setfp,     0 },
4974   { "unwind_raw",       s_arm_unwind_raw,       0 },
4975   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4976   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4977 #else
4978   { "word",        cons, 4},
4979
4980   /* These are used for dwarf.  */
4981   {"2byte", cons, 2},
4982   {"4byte", cons, 4},
4983   {"8byte", cons, 8},
4984   /* These are used for dwarf2.  */
4985   { "file", dwarf2_directive_file, 0 },
4986   { "loc",  dwarf2_directive_loc,  0 },
4987   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4988 #endif
4989   { "extend",      float_cons, 'x' },
4990   { "ldouble",     float_cons, 'x' },
4991   { "packed",      float_cons, 'p' },
4992 #ifdef TE_PE
4993   {"secrel32", pe_directive_secrel, 0},
4994 #endif
4995
4996   /* These are for compatibility with CodeComposer Studio.  */
4997   {"ref",          s_ccs_ref,        0},
4998   {"def",          s_ccs_def,        0},
4999   {"asmfunc",      s_ccs_asmfunc,    0},
5000   {"endasmfunc",   s_ccs_endasmfunc, 0},
5001
5002   { 0, 0, 0 }
5003 };
5004 \f
5005 /* Parser functions used exclusively in instruction operands.  */
5006
5007 /* Generic immediate-value read function for use in insn parsing.
5008    STR points to the beginning of the immediate (the leading #);
5009    VAL receives the value; if the value is outside [MIN, MAX]
5010    issue an error.  PREFIX_OPT is true if the immediate prefix is
5011    optional.  */
5012
5013 static int
5014 parse_immediate (char **str, int *val, int min, int max,
5015                  bfd_boolean prefix_opt)
5016 {
5017   expressionS exp;
5018
5019   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
5020   if (exp.X_op != O_constant)
5021     {
5022       inst.error = _("constant expression required");
5023       return FAIL;
5024     }
5025
5026   if (exp.X_add_number < min || exp.X_add_number > max)
5027     {
5028       inst.error = _("immediate value out of range");
5029       return FAIL;
5030     }
5031
5032   *val = exp.X_add_number;
5033   return SUCCESS;
5034 }
5035
5036 /* Less-generic immediate-value read function with the possibility of loading a
5037    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
5038    instructions. Puts the result directly in inst.operands[i].  */
5039
5040 static int
5041 parse_big_immediate (char **str, int i, expressionS *in_exp,
5042                      bfd_boolean allow_symbol_p)
5043 {
5044   expressionS exp;
5045   expressionS *exp_p = in_exp ? in_exp : &exp;
5046   char *ptr = *str;
5047
5048   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
5049
5050   if (exp_p->X_op == O_constant)
5051     {
5052       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
5053       /* If we're on a 64-bit host, then a 64-bit number can be returned using
5054          O_constant.  We have to be careful not to break compilation for
5055          32-bit X_add_number, though.  */
5056       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
5057         {
5058           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
5059           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
5060                                   & 0xffffffff);
5061           inst.operands[i].regisimm = 1;
5062         }
5063     }
5064   else if (exp_p->X_op == O_big
5065            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
5066     {
5067       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
5068
5069       /* Bignums have their least significant bits in
5070          generic_bignum[0]. Make sure we put 32 bits in imm and
5071          32 bits in reg,  in a (hopefully) portable way.  */
5072       gas_assert (parts != 0);
5073
5074       /* Make sure that the number is not too big.
5075          PR 11972: Bignums can now be sign-extended to the
5076          size of a .octa so check that the out of range bits
5077          are all zero or all one.  */
5078       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
5079         {
5080           LITTLENUM_TYPE m = -1;
5081
5082           if (generic_bignum[parts * 2] != 0
5083               && generic_bignum[parts * 2] != m)
5084             return FAIL;
5085
5086           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
5087             if (generic_bignum[j] != generic_bignum[j-1])
5088               return FAIL;
5089         }
5090
5091       inst.operands[i].imm = 0;
5092       for (j = 0; j < parts; j++, idx++)
5093         inst.operands[i].imm |= generic_bignum[idx]
5094                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5095       inst.operands[i].reg = 0;
5096       for (j = 0; j < parts; j++, idx++)
5097         inst.operands[i].reg |= generic_bignum[idx]
5098                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5099       inst.operands[i].regisimm = 1;
5100     }
5101   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
5102     return FAIL;
5103
5104   *str = ptr;
5105
5106   return SUCCESS;
5107 }
5108
5109 /* Returns the pseudo-register number of an FPA immediate constant,
5110    or FAIL if there isn't a valid constant here.  */
5111
5112 static int
5113 parse_fpa_immediate (char ** str)
5114 {
5115   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5116   char *         save_in;
5117   expressionS    exp;
5118   int            i;
5119   int            j;
5120
5121   /* First try and match exact strings, this is to guarantee
5122      that some formats will work even for cross assembly.  */
5123
5124   for (i = 0; fp_const[i]; i++)
5125     {
5126       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
5127         {
5128           char *start = *str;
5129
5130           *str += strlen (fp_const[i]);
5131           if (is_end_of_line[(unsigned char) **str])
5132             return i + 8;
5133           *str = start;
5134         }
5135     }
5136
5137   /* Just because we didn't get a match doesn't mean that the constant
5138      isn't valid, just that it is in a format that we don't
5139      automatically recognize.  Try parsing it with the standard
5140      expression routines.  */
5141
5142   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
5143
5144   /* Look for a raw floating point number.  */
5145   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
5146       && is_end_of_line[(unsigned char) *save_in])
5147     {
5148       for (i = 0; i < NUM_FLOAT_VALS; i++)
5149         {
5150           for (j = 0; j < MAX_LITTLENUMS; j++)
5151             {
5152               if (words[j] != fp_values[i][j])
5153                 break;
5154             }
5155
5156           if (j == MAX_LITTLENUMS)
5157             {
5158               *str = save_in;
5159               return i + 8;
5160             }
5161         }
5162     }
5163
5164   /* Try and parse a more complex expression, this will probably fail
5165      unless the code uses a floating point prefix (eg "0f").  */
5166   save_in = input_line_pointer;
5167   input_line_pointer = *str;
5168   if (expression (&exp) == absolute_section
5169       && exp.X_op == O_big
5170       && exp.X_add_number < 0)
5171     {
5172       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
5173          Ditto for 15.  */
5174 #define X_PRECISION 5
5175 #define E_PRECISION 15L
5176       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
5177         {
5178           for (i = 0; i < NUM_FLOAT_VALS; i++)
5179             {
5180               for (j = 0; j < MAX_LITTLENUMS; j++)
5181                 {
5182                   if (words[j] != fp_values[i][j])
5183                     break;
5184                 }
5185
5186               if (j == MAX_LITTLENUMS)
5187                 {
5188                   *str = input_line_pointer;
5189                   input_line_pointer = save_in;
5190                   return i + 8;
5191                 }
5192             }
5193         }
5194     }
5195
5196   *str = input_line_pointer;
5197   input_line_pointer = save_in;
5198   inst.error = _("invalid FPA immediate expression");
5199   return FAIL;
5200 }
5201
5202 /* Returns 1 if a number has "quarter-precision" float format
5203    0baBbbbbbc defgh000 00000000 00000000.  */
5204
5205 static int
5206 is_quarter_float (unsigned imm)
5207 {
5208   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
5209   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
5210 }
5211
5212
5213 /* Detect the presence of a floating point or integer zero constant,
5214    i.e. #0.0 or #0.  */
5215
5216 static bfd_boolean
5217 parse_ifimm_zero (char **in)
5218 {
5219   int error_code;
5220
5221   if (!is_immediate_prefix (**in))
5222     {
5223       /* In unified syntax, all prefixes are optional.  */
5224       if (!unified_syntax)
5225         return FALSE;
5226     }
5227   else
5228     ++*in;
5229
5230   /* Accept #0x0 as a synonym for #0.  */
5231   if (strncmp (*in, "0x", 2) == 0)
5232     {
5233       int val;
5234       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
5235         return FALSE;
5236       return TRUE;
5237     }
5238
5239   error_code = atof_generic (in, ".", EXP_CHARS,
5240                              &generic_floating_point_number);
5241
5242   if (!error_code
5243       && generic_floating_point_number.sign == '+'
5244       && (generic_floating_point_number.low
5245           > generic_floating_point_number.leader))
5246     return TRUE;
5247
5248   return FALSE;
5249 }
5250
5251 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5252    0baBbbbbbc defgh000 00000000 00000000.
5253    The zero and minus-zero cases need special handling, since they can't be
5254    encoded in the "quarter-precision" float format, but can nonetheless be
5255    loaded as integer constants.  */
5256
5257 static unsigned
5258 parse_qfloat_immediate (char **ccp, int *immed)
5259 {
5260   char *str = *ccp;
5261   char *fpnum;
5262   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5263   int found_fpchar = 0;
5264
5265   skip_past_char (&str, '#');
5266
5267   /* We must not accidentally parse an integer as a floating-point number. Make
5268      sure that the value we parse is not an integer by checking for special
5269      characters '.' or 'e'.
5270      FIXME: This is a horrible hack, but doing better is tricky because type
5271      information isn't in a very usable state at parse time.  */
5272   fpnum = str;
5273   skip_whitespace (fpnum);
5274
5275   if (strncmp (fpnum, "0x", 2) == 0)
5276     return FAIL;
5277   else
5278     {
5279       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5280         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5281           {
5282             found_fpchar = 1;
5283             break;
5284           }
5285
5286       if (!found_fpchar)
5287         return FAIL;
5288     }
5289
5290   if ((str = atof_ieee (str, 's', words)) != NULL)
5291     {
5292       unsigned fpword = 0;
5293       int i;
5294
5295       /* Our FP word must be 32 bits (single-precision FP).  */
5296       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5297         {
5298           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5299           fpword |= words[i];
5300         }
5301
5302       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5303         *immed = fpword;
5304       else
5305         return FAIL;
5306
5307       *ccp = str;
5308
5309       return SUCCESS;
5310     }
5311
5312   return FAIL;
5313 }
5314
5315 /* Shift operands.  */
5316 enum shift_kind
5317 {
5318   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX, SHIFT_UXTW
5319 };
5320
5321 struct asm_shift_name
5322 {
5323   const char      *name;
5324   enum shift_kind  kind;
5325 };
5326
5327 /* Third argument to parse_shift.  */
5328 enum parse_shift_mode
5329 {
5330   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5331   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5332   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5333   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5334   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5335   SHIFT_UXTW_IMMEDIATE          /* Shift must be UXTW immediate.  */
5336 };
5337
5338 /* Parse a <shift> specifier on an ARM data processing instruction.
5339    This has three forms:
5340
5341      (LSL|LSR|ASL|ASR|ROR) Rs
5342      (LSL|LSR|ASL|ASR|ROR) #imm
5343      RRX
5344
5345    Note that ASL is assimilated to LSL in the instruction encoding, and
5346    RRX to ROR #0 (which cannot be written as such).  */
5347
5348 static int
5349 parse_shift (char **str, int i, enum parse_shift_mode mode)
5350 {
5351   const struct asm_shift_name *shift_name;
5352   enum shift_kind shift;
5353   char *s = *str;
5354   char *p = s;
5355   int reg;
5356
5357   for (p = *str; ISALPHA (*p); p++)
5358     ;
5359
5360   if (p == *str)
5361     {
5362       inst.error = _("shift expression expected");
5363       return FAIL;
5364     }
5365
5366   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5367                                                             p - *str);
5368
5369   if (shift_name == NULL)
5370     {
5371       inst.error = _("shift expression expected");
5372       return FAIL;
5373     }
5374
5375   shift = shift_name->kind;
5376
5377   switch (mode)
5378     {
5379     case NO_SHIFT_RESTRICT:
5380     case SHIFT_IMMEDIATE:
5381       if (shift == SHIFT_UXTW)
5382         {
5383           inst.error = _("'UXTW' not allowed here");
5384           return FAIL;
5385         }
5386       break;
5387
5388     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5389       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5390         {
5391           inst.error = _("'LSL' or 'ASR' required");
5392           return FAIL;
5393         }
5394       break;
5395
5396     case SHIFT_LSL_IMMEDIATE:
5397       if (shift != SHIFT_LSL)
5398         {
5399           inst.error = _("'LSL' required");
5400           return FAIL;
5401         }
5402       break;
5403
5404     case SHIFT_ASR_IMMEDIATE:
5405       if (shift != SHIFT_ASR)
5406         {
5407           inst.error = _("'ASR' required");
5408           return FAIL;
5409         }
5410       break;
5411     case SHIFT_UXTW_IMMEDIATE:
5412       if (shift != SHIFT_UXTW)
5413         {
5414           inst.error = _("'UXTW' required");
5415           return FAIL;
5416         }
5417       break;
5418
5419     default: abort ();
5420     }
5421
5422   if (shift != SHIFT_RRX)
5423     {
5424       /* Whitespace can appear here if the next thing is a bare digit.  */
5425       skip_whitespace (p);
5426
5427       if (mode == NO_SHIFT_RESTRICT
5428           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5429         {
5430           inst.operands[i].imm = reg;
5431           inst.operands[i].immisreg = 1;
5432         }
5433       else if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5434         return FAIL;
5435     }
5436   inst.operands[i].shift_kind = shift;
5437   inst.operands[i].shifted = 1;
5438   *str = p;
5439   return SUCCESS;
5440 }
5441
5442 /* Parse a <shifter_operand> for an ARM data processing instruction:
5443
5444       #<immediate>
5445       #<immediate>, <rotate>
5446       <Rm>
5447       <Rm>, <shift>
5448
5449    where <shift> is defined by parse_shift above, and <rotate> is a
5450    multiple of 2 between 0 and 30.  Validation of immediate operands
5451    is deferred to md_apply_fix.  */
5452
5453 static int
5454 parse_shifter_operand (char **str, int i)
5455 {
5456   int value;
5457   expressionS exp;
5458
5459   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5460     {
5461       inst.operands[i].reg = value;
5462       inst.operands[i].isreg = 1;
5463
5464       /* parse_shift will override this if appropriate */
5465       inst.relocs[0].exp.X_op = O_constant;
5466       inst.relocs[0].exp.X_add_number = 0;
5467
5468       if (skip_past_comma (str) == FAIL)
5469         return SUCCESS;
5470
5471       /* Shift operation on register.  */
5472       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5473     }
5474
5475   if (my_get_expression (&inst.relocs[0].exp, str, GE_IMM_PREFIX))
5476     return FAIL;
5477
5478   if (skip_past_comma (str) == SUCCESS)
5479     {
5480       /* #x, y -- ie explicit rotation by Y.  */
5481       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5482         return FAIL;
5483
5484       if (exp.X_op != O_constant || inst.relocs[0].exp.X_op != O_constant)
5485         {
5486           inst.error = _("constant expression expected");
5487           return FAIL;
5488         }
5489
5490       value = exp.X_add_number;
5491       if (value < 0 || value > 30 || value % 2 != 0)
5492         {
5493           inst.error = _("invalid rotation");
5494           return FAIL;
5495         }
5496       if (inst.relocs[0].exp.X_add_number < 0
5497           || inst.relocs[0].exp.X_add_number > 255)
5498         {
5499           inst.error = _("invalid constant");
5500           return FAIL;
5501         }
5502
5503       /* Encode as specified.  */
5504       inst.operands[i].imm = inst.relocs[0].exp.X_add_number | value << 7;
5505       return SUCCESS;
5506     }
5507
5508   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
5509   inst.relocs[0].pc_rel = 0;
5510   return SUCCESS;
5511 }
5512
5513 /* Group relocation information.  Each entry in the table contains the
5514    textual name of the relocation as may appear in assembler source
5515    and must end with a colon.
5516    Along with this textual name are the relocation codes to be used if
5517    the corresponding instruction is an ALU instruction (ADD or SUB only),
5518    an LDR, an LDRS, or an LDC.  */
5519
5520 struct group_reloc_table_entry
5521 {
5522   const char *name;
5523   int alu_code;
5524   int ldr_code;
5525   int ldrs_code;
5526   int ldc_code;
5527 };
5528
5529 typedef enum
5530 {
5531   /* Varieties of non-ALU group relocation.  */
5532
5533   GROUP_LDR,
5534   GROUP_LDRS,
5535   GROUP_LDC,
5536   GROUP_MVE
5537 } group_reloc_type;
5538
5539 static struct group_reloc_table_entry group_reloc_table[] =
5540   { /* Program counter relative: */
5541     { "pc_g0_nc",
5542       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5543       0,                                /* LDR */
5544       0,                                /* LDRS */
5545       0 },                              /* LDC */
5546     { "pc_g0",
5547       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5548       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5549       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5550       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5551     { "pc_g1_nc",
5552       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5553       0,                                /* LDR */
5554       0,                                /* LDRS */
5555       0 },                              /* LDC */
5556     { "pc_g1",
5557       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5558       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5559       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5560       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5561     { "pc_g2",
5562       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5563       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5564       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5565       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5566     /* Section base relative */
5567     { "sb_g0_nc",
5568       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5569       0,                                /* LDR */
5570       0,                                /* LDRS */
5571       0 },                              /* LDC */
5572     { "sb_g0",
5573       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5574       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5575       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5576       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5577     { "sb_g1_nc",
5578       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5579       0,                                /* LDR */
5580       0,                                /* LDRS */
5581       0 },                              /* LDC */
5582     { "sb_g1",
5583       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5584       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5585       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5586       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5587     { "sb_g2",
5588       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5589       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5590       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5591       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5592     /* Absolute thumb alu relocations.  */
5593     { "lower0_7",
5594       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5595       0,                                /* LDR.  */
5596       0,                                /* LDRS.  */
5597       0 },                              /* LDC.  */
5598     { "lower8_15",
5599       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5600       0,                                /* LDR.  */
5601       0,                                /* LDRS.  */
5602       0 },                              /* LDC.  */
5603     { "upper0_7",
5604       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5605       0,                                /* LDR.  */
5606       0,                                /* LDRS.  */
5607       0 },                              /* LDC.  */
5608     { "upper8_15",
5609       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5610       0,                                /* LDR.  */
5611       0,                                /* LDRS.  */
5612       0 } };                            /* LDC.  */
5613
5614 /* Given the address of a pointer pointing to the textual name of a group
5615    relocation as may appear in assembler source, attempt to find its details
5616    in group_reloc_table.  The pointer will be updated to the character after
5617    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5618    otherwise.  On success, *entry will be updated to point at the relevant
5619    group_reloc_table entry. */
5620
5621 static int
5622 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5623 {
5624   unsigned int i;
5625   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5626     {
5627       int length = strlen (group_reloc_table[i].name);
5628
5629       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5630           && (*str)[length] == ':')
5631         {
5632           *out = &group_reloc_table[i];
5633           *str += (length + 1);
5634           return SUCCESS;
5635         }
5636     }
5637
5638   return FAIL;
5639 }
5640
5641 /* Parse a <shifter_operand> for an ARM data processing instruction
5642    (as for parse_shifter_operand) where group relocations are allowed:
5643
5644       #<immediate>
5645       #<immediate>, <rotate>
5646       #:<group_reloc>:<expression>
5647       <Rm>
5648       <Rm>, <shift>
5649
5650    where <group_reloc> is one of the strings defined in group_reloc_table.
5651    The hashes are optional.
5652
5653    Everything else is as for parse_shifter_operand.  */
5654
5655 static parse_operand_result
5656 parse_shifter_operand_group_reloc (char **str, int i)
5657 {
5658   /* Determine if we have the sequence of characters #: or just :
5659      coming next.  If we do, then we check for a group relocation.
5660      If we don't, punt the whole lot to parse_shifter_operand.  */
5661
5662   if (((*str)[0] == '#' && (*str)[1] == ':')
5663       || (*str)[0] == ':')
5664     {
5665       struct group_reloc_table_entry *entry;
5666
5667       if ((*str)[0] == '#')
5668         (*str) += 2;
5669       else
5670         (*str)++;
5671
5672       /* Try to parse a group relocation.  Anything else is an error.  */
5673       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5674         {
5675           inst.error = _("unknown group relocation");
5676           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5677         }
5678
5679       /* We now have the group relocation table entry corresponding to
5680          the name in the assembler source.  Next, we parse the expression.  */
5681       if (my_get_expression (&inst.relocs[0].exp, str, GE_NO_PREFIX))
5682         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5683
5684       /* Record the relocation type (always the ALU variant here).  */
5685       inst.relocs[0].type = (bfd_reloc_code_real_type) entry->alu_code;
5686       gas_assert (inst.relocs[0].type != 0);
5687
5688       return PARSE_OPERAND_SUCCESS;
5689     }
5690   else
5691     return parse_shifter_operand (str, i) == SUCCESS
5692            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5693
5694   /* Never reached.  */
5695 }
5696
5697 /* Parse a Neon alignment expression.  Information is written to
5698    inst.operands[i].  We assume the initial ':' has been skipped.
5699
5700    align        .imm = align << 8, .immisalign=1, .preind=0  */
5701 static parse_operand_result
5702 parse_neon_alignment (char **str, int i)
5703 {
5704   char *p = *str;
5705   expressionS exp;
5706
5707   my_get_expression (&exp, &p, GE_NO_PREFIX);
5708
5709   if (exp.X_op != O_constant)
5710     {
5711       inst.error = _("alignment must be constant");
5712       return PARSE_OPERAND_FAIL;
5713     }
5714
5715   inst.operands[i].imm = exp.X_add_number << 8;
5716   inst.operands[i].immisalign = 1;
5717   /* Alignments are not pre-indexes.  */
5718   inst.operands[i].preind = 0;
5719
5720   *str = p;
5721   return PARSE_OPERAND_SUCCESS;
5722 }
5723
5724 /* Parse all forms of an ARM address expression.  Information is written
5725    to inst.operands[i] and/or inst.relocs[0].
5726
5727    Preindexed addressing (.preind=1):
5728
5729    [Rn, #offset]       .reg=Rn .relocs[0].exp=offset
5730    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5731    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5732                        .shift_kind=shift .relocs[0].exp=shift_imm
5733
5734    These three may have a trailing ! which causes .writeback to be set also.
5735
5736    Postindexed addressing (.postind=1, .writeback=1):
5737
5738    [Rn], #offset       .reg=Rn .relocs[0].exp=offset
5739    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5740    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5741                        .shift_kind=shift .relocs[0].exp=shift_imm
5742
5743    Unindexed addressing (.preind=0, .postind=0):
5744
5745    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5746
5747    Other:
5748
5749    [Rn]{!}             shorthand for [Rn,#0]{!}
5750    =immediate          .isreg=0 .relocs[0].exp=immediate
5751    label               .reg=PC .relocs[0].pc_rel=1 .relocs[0].exp=label
5752
5753   It is the caller's responsibility to check for addressing modes not
5754   supported by the instruction, and to set inst.relocs[0].type.  */
5755
5756 static parse_operand_result
5757 parse_address_main (char **str, int i, int group_relocations,
5758                     group_reloc_type group_type)
5759 {
5760   char *p = *str;
5761   int reg;
5762
5763   if (skip_past_char (&p, '[') == FAIL)
5764     {
5765       if (skip_past_char (&p, '=') == FAIL)
5766         {
5767           /* Bare address - translate to PC-relative offset.  */
5768           inst.relocs[0].pc_rel = 1;
5769           inst.operands[i].reg = REG_PC;
5770           inst.operands[i].isreg = 1;
5771           inst.operands[i].preind = 1;
5772
5773           if (my_get_expression (&inst.relocs[0].exp, &p, GE_OPT_PREFIX_BIG))
5774             return PARSE_OPERAND_FAIL;
5775         }
5776       else if (parse_big_immediate (&p, i, &inst.relocs[0].exp,
5777                                     /*allow_symbol_p=*/TRUE))
5778         return PARSE_OPERAND_FAIL;
5779
5780       *str = p;
5781       return PARSE_OPERAND_SUCCESS;
5782     }
5783
5784   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5785   skip_whitespace (p);
5786
5787   if (group_type == GROUP_MVE)
5788     {
5789       enum arm_reg_type rtype = REG_TYPE_MQ;
5790       struct neon_type_el et;
5791       if ((reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5792         {
5793           inst.operands[i].isquad = 1;
5794         }
5795       else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5796         {
5797           inst.error = BAD_ADDR_MODE;
5798           return PARSE_OPERAND_FAIL;
5799         }
5800     }
5801   else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5802     {
5803       if (group_type == GROUP_MVE)
5804         inst.error = BAD_ADDR_MODE;
5805       else
5806         inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5807       return PARSE_OPERAND_FAIL;
5808     }
5809   inst.operands[i].reg = reg;
5810   inst.operands[i].isreg = 1;
5811
5812   if (skip_past_comma (&p) == SUCCESS)
5813     {
5814       inst.operands[i].preind = 1;
5815
5816       if (*p == '+') p++;
5817       else if (*p == '-') p++, inst.operands[i].negative = 1;
5818
5819       enum arm_reg_type rtype = REG_TYPE_MQ;
5820       struct neon_type_el et;
5821       if (group_type == GROUP_MVE
5822           && (reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5823         {
5824           inst.operands[i].immisreg = 2;
5825           inst.operands[i].imm = reg;
5826
5827           if (skip_past_comma (&p) == SUCCESS)
5828             {
5829               if (parse_shift (&p, i, SHIFT_UXTW_IMMEDIATE) == SUCCESS)
5830                 {
5831                   inst.operands[i].imm |= inst.relocs[0].exp.X_add_number << 5;
5832                   inst.relocs[0].exp.X_add_number = 0;
5833                 }
5834               else
5835                 return PARSE_OPERAND_FAIL;
5836             }
5837         }
5838       else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5839         {
5840           inst.operands[i].imm = reg;
5841           inst.operands[i].immisreg = 1;
5842
5843           if (skip_past_comma (&p) == SUCCESS)
5844             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5845               return PARSE_OPERAND_FAIL;
5846         }
5847       else if (skip_past_char (&p, ':') == SUCCESS)
5848         {
5849           /* FIXME: '@' should be used here, but it's filtered out by generic
5850              code before we get to see it here. This may be subject to
5851              change.  */
5852           parse_operand_result result = parse_neon_alignment (&p, i);
5853
5854           if (result != PARSE_OPERAND_SUCCESS)
5855             return result;
5856         }
5857       else
5858         {
5859           if (inst.operands[i].negative)
5860             {
5861               inst.operands[i].negative = 0;
5862               p--;
5863             }
5864
5865           if (group_relocations
5866               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5867             {
5868               struct group_reloc_table_entry *entry;
5869
5870               /* Skip over the #: or : sequence.  */
5871               if (*p == '#')
5872                 p += 2;
5873               else
5874                 p++;
5875
5876               /* Try to parse a group relocation.  Anything else is an
5877                  error.  */
5878               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5879                 {
5880                   inst.error = _("unknown group relocation");
5881                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5882                 }
5883
5884               /* We now have the group relocation table entry corresponding to
5885                  the name in the assembler source.  Next, we parse the
5886                  expression.  */
5887               if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
5888                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5889
5890               /* Record the relocation type.  */
5891               switch (group_type)
5892                 {
5893                   case GROUP_LDR:
5894                     inst.relocs[0].type
5895                         = (bfd_reloc_code_real_type) entry->ldr_code;
5896                     break;
5897
5898                   case GROUP_LDRS:
5899                     inst.relocs[0].type
5900                         = (bfd_reloc_code_real_type) entry->ldrs_code;
5901                     break;
5902
5903                   case GROUP_LDC:
5904                     inst.relocs[0].type
5905                         = (bfd_reloc_code_real_type) entry->ldc_code;
5906                     break;
5907
5908                   default:
5909                     gas_assert (0);
5910                 }
5911
5912               if (inst.relocs[0].type == 0)
5913                 {
5914                   inst.error = _("this group relocation is not allowed on this instruction");
5915                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5916                 }
5917             }
5918           else
5919             {
5920               char *q = p;
5921
5922               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5923                 return PARSE_OPERAND_FAIL;
5924               /* If the offset is 0, find out if it's a +0 or -0.  */
5925               if (inst.relocs[0].exp.X_op == O_constant
5926                   && inst.relocs[0].exp.X_add_number == 0)
5927                 {
5928                   skip_whitespace (q);
5929                   if (*q == '#')
5930                     {
5931                       q++;
5932                       skip_whitespace (q);
5933                     }
5934                   if (*q == '-')
5935                     inst.operands[i].negative = 1;
5936                 }
5937             }
5938         }
5939     }
5940   else if (skip_past_char (&p, ':') == SUCCESS)
5941     {
5942       /* FIXME: '@' should be used here, but it's filtered out by generic code
5943          before we get to see it here. This may be subject to change.  */
5944       parse_operand_result result = parse_neon_alignment (&p, i);
5945
5946       if (result != PARSE_OPERAND_SUCCESS)
5947         return result;
5948     }
5949
5950   if (skip_past_char (&p, ']') == FAIL)
5951     {
5952       inst.error = _("']' expected");
5953       return PARSE_OPERAND_FAIL;
5954     }
5955
5956   if (skip_past_char (&p, '!') == SUCCESS)
5957     inst.operands[i].writeback = 1;
5958
5959   else if (skip_past_comma (&p) == SUCCESS)
5960     {
5961       if (skip_past_char (&p, '{') == SUCCESS)
5962         {
5963           /* [Rn], {expr} - unindexed, with option */
5964           if (parse_immediate (&p, &inst.operands[i].imm,
5965                                0, 255, TRUE) == FAIL)
5966             return PARSE_OPERAND_FAIL;
5967
5968           if (skip_past_char (&p, '}') == FAIL)
5969             {
5970               inst.error = _("'}' expected at end of 'option' field");
5971               return PARSE_OPERAND_FAIL;
5972             }
5973           if (inst.operands[i].preind)
5974             {
5975               inst.error = _("cannot combine index with option");
5976               return PARSE_OPERAND_FAIL;
5977             }
5978           *str = p;
5979           return PARSE_OPERAND_SUCCESS;
5980         }
5981       else
5982         {
5983           inst.operands[i].postind = 1;
5984           inst.operands[i].writeback = 1;
5985
5986           if (inst.operands[i].preind)
5987             {
5988               inst.error = _("cannot combine pre- and post-indexing");
5989               return PARSE_OPERAND_FAIL;
5990             }
5991
5992           if (*p == '+') p++;
5993           else if (*p == '-') p++, inst.operands[i].negative = 1;
5994
5995           enum arm_reg_type rtype = REG_TYPE_MQ;
5996           struct neon_type_el et;
5997           if (group_type == GROUP_MVE
5998               && (reg = arm_typed_reg_parse (&p, rtype, &rtype, &et)) != FAIL)
5999             {
6000               inst.operands[i].immisreg = 2;
6001               inst.operands[i].imm = reg;
6002             }
6003           else if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
6004             {
6005               /* We might be using the immediate for alignment already. If we
6006                  are, OR the register number into the low-order bits.  */
6007               if (inst.operands[i].immisalign)
6008                 inst.operands[i].imm |= reg;
6009               else
6010                 inst.operands[i].imm = reg;
6011               inst.operands[i].immisreg = 1;
6012
6013               if (skip_past_comma (&p) == SUCCESS)
6014                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
6015                   return PARSE_OPERAND_FAIL;
6016             }
6017           else
6018             {
6019               char *q = p;
6020
6021               if (inst.operands[i].negative)
6022                 {
6023                   inst.operands[i].negative = 0;
6024                   p--;
6025                 }
6026               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
6027                 return PARSE_OPERAND_FAIL;
6028               /* If the offset is 0, find out if it's a +0 or -0.  */
6029               if (inst.relocs[0].exp.X_op == O_constant
6030                   && inst.relocs[0].exp.X_add_number == 0)
6031                 {
6032                   skip_whitespace (q);
6033                   if (*q == '#')
6034                     {
6035                       q++;
6036                       skip_whitespace (q);
6037                     }
6038                   if (*q == '-')
6039                     inst.operands[i].negative = 1;
6040                 }
6041             }
6042         }
6043     }
6044
6045   /* If at this point neither .preind nor .postind is set, we have a
6046      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
6047   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
6048     {
6049       inst.operands[i].preind = 1;
6050       inst.relocs[0].exp.X_op = O_constant;
6051       inst.relocs[0].exp.X_add_number = 0;
6052     }
6053   *str = p;
6054   return PARSE_OPERAND_SUCCESS;
6055 }
6056
6057 static int
6058 parse_address (char **str, int i)
6059 {
6060   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
6061          ? SUCCESS : FAIL;
6062 }
6063
6064 static parse_operand_result
6065 parse_address_group_reloc (char **str, int i, group_reloc_type type)
6066 {
6067   return parse_address_main (str, i, 1, type);
6068 }
6069
6070 /* Parse an operand for a MOVW or MOVT instruction.  */
6071 static int
6072 parse_half (char **str)
6073 {
6074   char * p;
6075
6076   p = *str;
6077   skip_past_char (&p, '#');
6078   if (strncasecmp (p, ":lower16:", 9) == 0)
6079     inst.relocs[0].type = BFD_RELOC_ARM_MOVW;
6080   else if (strncasecmp (p, ":upper16:", 9) == 0)
6081     inst.relocs[0].type = BFD_RELOC_ARM_MOVT;
6082
6083   if (inst.relocs[0].type != BFD_RELOC_UNUSED)
6084     {
6085       p += 9;
6086       skip_whitespace (p);
6087     }
6088
6089   if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
6090     return FAIL;
6091
6092   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
6093     {
6094       if (inst.relocs[0].exp.X_op != O_constant)
6095         {
6096           inst.error = _("constant expression expected");
6097           return FAIL;
6098         }
6099       if (inst.relocs[0].exp.X_add_number < 0
6100           || inst.relocs[0].exp.X_add_number > 0xffff)
6101         {
6102           inst.error = _("immediate value out of range");
6103           return FAIL;
6104         }
6105     }
6106   *str = p;
6107   return SUCCESS;
6108 }
6109
6110 /* Miscellaneous. */
6111
6112 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
6113    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
6114 static int
6115 parse_psr (char **str, bfd_boolean lhs)
6116 {
6117   char *p;
6118   unsigned long psr_field;
6119   const struct asm_psr *psr;
6120   char *start;
6121   bfd_boolean is_apsr = FALSE;
6122   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
6123
6124   /* PR gas/12698:  If the user has specified -march=all then m_profile will
6125      be TRUE, but we want to ignore it in this case as we are building for any
6126      CPU type, including non-m variants.  */
6127   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
6128     m_profile = FALSE;
6129
6130   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
6131      feature for ease of use and backwards compatibility.  */
6132   p = *str;
6133   if (strncasecmp (p, "SPSR", 4) == 0)
6134     {
6135       if (m_profile)
6136         goto unsupported_psr;
6137
6138       psr_field = SPSR_BIT;
6139     }
6140   else if (strncasecmp (p, "CPSR", 4) == 0)
6141     {
6142       if (m_profile)
6143         goto unsupported_psr;
6144
6145       psr_field = 0;
6146     }
6147   else if (strncasecmp (p, "APSR", 4) == 0)
6148     {
6149       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
6150          and ARMv7-R architecture CPUs.  */
6151       is_apsr = TRUE;
6152       psr_field = 0;
6153     }
6154   else if (m_profile)
6155     {
6156       start = p;
6157       do
6158         p++;
6159       while (ISALNUM (*p) || *p == '_');
6160
6161       if (strncasecmp (start, "iapsr", 5) == 0
6162           || strncasecmp (start, "eapsr", 5) == 0
6163           || strncasecmp (start, "xpsr", 4) == 0
6164           || strncasecmp (start, "psr", 3) == 0)
6165         p = start + strcspn (start, "rR") + 1;
6166
6167       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
6168                                                   p - start);
6169
6170       if (!psr)
6171         return FAIL;
6172
6173       /* If APSR is being written, a bitfield may be specified.  Note that
6174          APSR itself is handled above.  */
6175       if (psr->field <= 3)
6176         {
6177           psr_field = psr->field;
6178           is_apsr = TRUE;
6179           goto check_suffix;
6180         }
6181
6182       *str = p;
6183       /* M-profile MSR instructions have the mask field set to "10", except
6184          *PSR variants which modify APSR, which may use a different mask (and
6185          have been handled already).  Do that by setting the PSR_f field
6186          here.  */
6187       return psr->field | (lhs ? PSR_f : 0);
6188     }
6189   else
6190     goto unsupported_psr;
6191
6192   p += 4;
6193 check_suffix:
6194   if (*p == '_')
6195     {
6196       /* A suffix follows.  */
6197       p++;
6198       start = p;
6199
6200       do
6201         p++;
6202       while (ISALNUM (*p) || *p == '_');
6203
6204       if (is_apsr)
6205         {
6206           /* APSR uses a notation for bits, rather than fields.  */
6207           unsigned int nzcvq_bits = 0;
6208           unsigned int g_bit = 0;
6209           char *bit;
6210
6211           for (bit = start; bit != p; bit++)
6212             {
6213               switch (TOLOWER (*bit))
6214                 {
6215                 case 'n':
6216                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
6217                   break;
6218
6219                 case 'z':
6220                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
6221                   break;
6222
6223                 case 'c':
6224                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
6225                   break;
6226
6227                 case 'v':
6228                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
6229                   break;
6230
6231                 case 'q':
6232                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
6233                   break;
6234
6235                 case 'g':
6236                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
6237                   break;
6238
6239                 default:
6240                   inst.error = _("unexpected bit specified after APSR");
6241                   return FAIL;
6242                 }
6243             }
6244
6245           if (nzcvq_bits == 0x1f)
6246             psr_field |= PSR_f;
6247
6248           if (g_bit == 0x1)
6249             {
6250               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
6251                 {
6252                   inst.error = _("selected processor does not "
6253                                  "support DSP extension");
6254                   return FAIL;
6255                 }
6256
6257               psr_field |= PSR_s;
6258             }
6259
6260           if ((nzcvq_bits & 0x20) != 0
6261               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
6262               || (g_bit & 0x2) != 0)
6263             {
6264               inst.error = _("bad bitmask specified after APSR");
6265               return FAIL;
6266             }
6267         }
6268       else
6269         {
6270           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
6271                                                       p - start);
6272           if (!psr)
6273             goto error;
6274
6275           psr_field |= psr->field;
6276         }
6277     }
6278   else
6279     {
6280       if (ISALNUM (*p))
6281         goto error;    /* Garbage after "[CS]PSR".  */
6282
6283       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
6284          is deprecated, but allow it anyway.  */
6285       if (is_apsr && lhs)
6286         {
6287           psr_field |= PSR_f;
6288           as_tsktsk (_("writing to APSR without specifying a bitmask is "
6289                        "deprecated"));
6290         }
6291       else if (!m_profile)
6292         /* These bits are never right for M-profile devices: don't set them
6293            (only code paths which read/write APSR reach here).  */
6294         psr_field |= (PSR_c | PSR_f);
6295     }
6296   *str = p;
6297   return psr_field;
6298
6299  unsupported_psr:
6300   inst.error = _("selected processor does not support requested special "
6301                  "purpose register");
6302   return FAIL;
6303
6304  error:
6305   inst.error = _("flag for {c}psr instruction expected");
6306   return FAIL;
6307 }
6308
6309 static int
6310 parse_sys_vldr_vstr (char **str)
6311 {
6312   unsigned i;
6313   int val = FAIL;
6314   struct {
6315     const char *name;
6316     int regl;
6317     int regh;
6318   } sysregs[] = {
6319     {"FPSCR",           0x1, 0x0},
6320     {"FPSCR_nzcvqc",    0x2, 0x0},
6321     {"VPR",             0x4, 0x1},
6322     {"P0",              0x5, 0x1},
6323     {"FPCXTNS",         0x6, 0x1},
6324     {"FPCXTS",          0x7, 0x1}
6325   };
6326   char *op_end = strchr (*str, ',');
6327   size_t op_strlen = op_end - *str;
6328
6329   for (i = 0; i < sizeof (sysregs) / sizeof (sysregs[0]); i++)
6330     {
6331       if (!strncmp (*str, sysregs[i].name, op_strlen))
6332         {
6333           val = sysregs[i].regl | (sysregs[i].regh << 3);
6334           *str = op_end;
6335           break;
6336         }
6337     }
6338
6339   return val;
6340 }
6341
6342 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
6343    value suitable for splatting into the AIF field of the instruction.  */
6344
6345 static int
6346 parse_cps_flags (char **str)
6347 {
6348   int val = 0;
6349   int saw_a_flag = 0;
6350   char *s = *str;
6351
6352   for (;;)
6353     switch (*s++)
6354       {
6355       case '\0': case ',':
6356         goto done;
6357
6358       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6359       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6360       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6361
6362       default:
6363         inst.error = _("unrecognized CPS flag");
6364         return FAIL;
6365       }
6366
6367  done:
6368   if (saw_a_flag == 0)
6369     {
6370       inst.error = _("missing CPS flags");
6371       return FAIL;
6372     }
6373
6374   *str = s - 1;
6375   return val;
6376 }
6377
6378 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6379    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6380
6381 static int
6382 parse_endian_specifier (char **str)
6383 {
6384   int little_endian;
6385   char *s = *str;
6386
6387   if (strncasecmp (s, "BE", 2))
6388     little_endian = 0;
6389   else if (strncasecmp (s, "LE", 2))
6390     little_endian = 1;
6391   else
6392     {
6393       inst.error = _("valid endian specifiers are be or le");
6394       return FAIL;
6395     }
6396
6397   if (ISALNUM (s[2]) || s[2] == '_')
6398     {
6399       inst.error = _("valid endian specifiers are be or le");
6400       return FAIL;
6401     }
6402
6403   *str = s + 2;
6404   return little_endian;
6405 }
6406
6407 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6408    value suitable for poking into the rotate field of an sxt or sxta
6409    instruction, or FAIL on error.  */
6410
6411 static int
6412 parse_ror (char **str)
6413 {
6414   int rot;
6415   char *s = *str;
6416
6417   if (strncasecmp (s, "ROR", 3) == 0)
6418     s += 3;
6419   else
6420     {
6421       inst.error = _("missing rotation field after comma");
6422       return FAIL;
6423     }
6424
6425   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6426     return FAIL;
6427
6428   switch (rot)
6429     {
6430     case  0: *str = s; return 0x0;
6431     case  8: *str = s; return 0x1;
6432     case 16: *str = s; return 0x2;
6433     case 24: *str = s; return 0x3;
6434
6435     default:
6436       inst.error = _("rotation can only be 0, 8, 16, or 24");
6437       return FAIL;
6438     }
6439 }
6440
6441 /* Parse a conditional code (from conds[] below).  The value returned is in the
6442    range 0 .. 14, or FAIL.  */
6443 static int
6444 parse_cond (char **str)
6445 {
6446   char *q;
6447   const struct asm_cond *c;
6448   int n;
6449   /* Condition codes are always 2 characters, so matching up to
6450      3 characters is sufficient.  */
6451   char cond[3];
6452
6453   q = *str;
6454   n = 0;
6455   while (ISALPHA (*q) && n < 3)
6456     {
6457       cond[n] = TOLOWER (*q);
6458       q++;
6459       n++;
6460     }
6461
6462   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6463   if (!c)
6464     {
6465       inst.error = _("condition required");
6466       return FAIL;
6467     }
6468
6469   *str = q;
6470   return c->value;
6471 }
6472
6473 /* Parse an option for a barrier instruction.  Returns the encoding for the
6474    option, or FAIL.  */
6475 static int
6476 parse_barrier (char **str)
6477 {
6478   char *p, *q;
6479   const struct asm_barrier_opt *o;
6480
6481   p = q = *str;
6482   while (ISALPHA (*q))
6483     q++;
6484
6485   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6486                                                     q - p);
6487   if (!o)
6488     return FAIL;
6489
6490   if (!mark_feature_used (&o->arch))
6491     return FAIL;
6492
6493   *str = q;
6494   return o->value;
6495 }
6496
6497 /* Parse the operands of a table branch instruction.  Similar to a memory
6498    operand.  */
6499 static int
6500 parse_tb (char **str)
6501 {
6502   char * p = *str;
6503   int reg;
6504
6505   if (skip_past_char (&p, '[') == FAIL)
6506     {
6507       inst.error = _("'[' expected");
6508       return FAIL;
6509     }
6510
6511   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6512     {
6513       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6514       return FAIL;
6515     }
6516   inst.operands[0].reg = reg;
6517
6518   if (skip_past_comma (&p) == FAIL)
6519     {
6520       inst.error = _("',' expected");
6521       return FAIL;
6522     }
6523
6524   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6525     {
6526       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6527       return FAIL;
6528     }
6529   inst.operands[0].imm = reg;
6530
6531   if (skip_past_comma (&p) == SUCCESS)
6532     {
6533       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6534         return FAIL;
6535       if (inst.relocs[0].exp.X_add_number != 1)
6536         {
6537           inst.error = _("invalid shift");
6538           return FAIL;
6539         }
6540       inst.operands[0].shifted = 1;
6541     }
6542
6543   if (skip_past_char (&p, ']') == FAIL)
6544     {
6545       inst.error = _("']' expected");
6546       return FAIL;
6547     }
6548   *str = p;
6549   return SUCCESS;
6550 }
6551
6552 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6553    information on the types the operands can take and how they are encoded.
6554    Up to four operands may be read; this function handles setting the
6555    ".present" field for each read operand itself.
6556    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6557    else returns FAIL.  */
6558
6559 static int
6560 parse_neon_mov (char **str, int *which_operand)
6561 {
6562   int i = *which_operand, val;
6563   enum arm_reg_type rtype;
6564   char *ptr = *str;
6565   struct neon_type_el optype;
6566
6567    if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6568     {
6569       /* Cases 17 or 19.  */
6570       inst.operands[i].reg = val;
6571       inst.operands[i].isvec = 1;
6572       inst.operands[i].isscalar = 2;
6573       inst.operands[i].vectype = optype;
6574       inst.operands[i++].present = 1;
6575
6576       if (skip_past_comma (&ptr) == FAIL)
6577         goto wanted_comma;
6578
6579       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6580         {
6581           /* Case 17: VMOV<c>.<dt> <Qd[idx]>, <Rt>  */
6582           inst.operands[i].reg = val;
6583           inst.operands[i].isreg = 1;
6584           inst.operands[i].present = 1;
6585         }
6586       else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6587         {
6588           /* Case 19: VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>  */
6589           inst.operands[i].reg = val;
6590           inst.operands[i].isvec = 1;
6591           inst.operands[i].isscalar = 2;
6592           inst.operands[i].vectype = optype;
6593           inst.operands[i++].present = 1;
6594
6595           if (skip_past_comma (&ptr) == FAIL)
6596             goto wanted_comma;
6597
6598           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6599             goto wanted_arm;
6600
6601           inst.operands[i].reg = val;
6602           inst.operands[i].isreg = 1;
6603           inst.operands[i++].present = 1;
6604
6605           if (skip_past_comma (&ptr) == FAIL)
6606             goto wanted_comma;
6607
6608           if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6609             goto wanted_arm;
6610
6611           inst.operands[i].reg = val;
6612           inst.operands[i].isreg = 1;
6613           inst.operands[i].present = 1;
6614         }
6615       else
6616         {
6617           first_error (_("expected ARM or MVE vector register"));
6618           return FAIL;
6619         }
6620     }
6621    else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_VFD)) != FAIL)
6622     {
6623       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6624       inst.operands[i].reg = val;
6625       inst.operands[i].isscalar = 1;
6626       inst.operands[i].vectype = optype;
6627       inst.operands[i++].present = 1;
6628
6629       if (skip_past_comma (&ptr) == FAIL)
6630         goto wanted_comma;
6631
6632       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6633         goto wanted_arm;
6634
6635       inst.operands[i].reg = val;
6636       inst.operands[i].isreg = 1;
6637       inst.operands[i].present = 1;
6638     }
6639   else if (((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6640             != FAIL)
6641            || ((val = arm_typed_reg_parse (&ptr, REG_TYPE_MQ, &rtype, &optype))
6642                != FAIL))
6643     {
6644       /* Cases 0, 1, 2, 3, 5 (D only).  */
6645       if (skip_past_comma (&ptr) == FAIL)
6646         goto wanted_comma;
6647
6648       inst.operands[i].reg = val;
6649       inst.operands[i].isreg = 1;
6650       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6651       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6652       inst.operands[i].isvec = 1;
6653       inst.operands[i].vectype = optype;
6654       inst.operands[i++].present = 1;
6655
6656       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6657         {
6658           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6659              Case 13: VMOV <Sd>, <Rm>  */
6660           inst.operands[i].reg = val;
6661           inst.operands[i].isreg = 1;
6662           inst.operands[i].present = 1;
6663
6664           if (rtype == REG_TYPE_NQ)
6665             {
6666               first_error (_("can't use Neon quad register here"));
6667               return FAIL;
6668             }
6669           else if (rtype != REG_TYPE_VFS)
6670             {
6671               i++;
6672               if (skip_past_comma (&ptr) == FAIL)
6673                 goto wanted_comma;
6674               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6675                 goto wanted_arm;
6676               inst.operands[i].reg = val;
6677               inst.operands[i].isreg = 1;
6678               inst.operands[i].present = 1;
6679             }
6680         }
6681       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6682                                            &optype)) != FAIL)
6683         {
6684           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6685              Case 1: VMOV<c><q> <Dd>, <Dm>
6686              Case 8: VMOV.F32 <Sd>, <Sm>
6687              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6688
6689           inst.operands[i].reg = val;
6690           inst.operands[i].isreg = 1;
6691           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6692           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6693           inst.operands[i].isvec = 1;
6694           inst.operands[i].vectype = optype;
6695           inst.operands[i].present = 1;
6696
6697           if (skip_past_comma (&ptr) == SUCCESS)
6698             {
6699               /* Case 15.  */
6700               i++;
6701
6702               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6703                 goto wanted_arm;
6704
6705               inst.operands[i].reg = val;
6706               inst.operands[i].isreg = 1;
6707               inst.operands[i++].present = 1;
6708
6709               if (skip_past_comma (&ptr) == FAIL)
6710                 goto wanted_comma;
6711
6712               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6713                 goto wanted_arm;
6714
6715               inst.operands[i].reg = val;
6716               inst.operands[i].isreg = 1;
6717               inst.operands[i].present = 1;
6718             }
6719         }
6720       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6721           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6722              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6723              Case 10: VMOV.F32 <Sd>, #<imm>
6724              Case 11: VMOV.F64 <Dd>, #<imm>  */
6725         inst.operands[i].immisfloat = 1;
6726       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6727                == SUCCESS)
6728           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6729              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6730         ;
6731       else
6732         {
6733           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6734           return FAIL;
6735         }
6736     }
6737   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6738     {
6739       /* Cases 6, 7, 16, 18.  */
6740       inst.operands[i].reg = val;
6741       inst.operands[i].isreg = 1;
6742       inst.operands[i++].present = 1;
6743
6744       if (skip_past_comma (&ptr) == FAIL)
6745         goto wanted_comma;
6746
6747       if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ)) != FAIL)
6748         {
6749           /* Case 18: VMOV<c>.<dt> <Rt>, <Qn[idx]>  */
6750           inst.operands[i].reg = val;
6751           inst.operands[i].isscalar = 2;
6752           inst.operands[i].present = 1;
6753           inst.operands[i].vectype = optype;
6754         }
6755       else if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_VFD)) != FAIL)
6756         {
6757           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6758           inst.operands[i].reg = val;
6759           inst.operands[i].isscalar = 1;
6760           inst.operands[i].present = 1;
6761           inst.operands[i].vectype = optype;
6762         }
6763       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6764         {
6765           inst.operands[i].reg = val;
6766           inst.operands[i].isreg = 1;
6767           inst.operands[i++].present = 1;
6768
6769           if (skip_past_comma (&ptr) == FAIL)
6770             goto wanted_comma;
6771
6772           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6773               != FAIL)
6774             {
6775               /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6776
6777               inst.operands[i].reg = val;
6778               inst.operands[i].isreg = 1;
6779               inst.operands[i].isvec = 1;
6780               inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6781               inst.operands[i].vectype = optype;
6782               inst.operands[i].present = 1;
6783
6784               if (rtype == REG_TYPE_VFS)
6785                 {
6786                   /* Case 14.  */
6787                   i++;
6788                   if (skip_past_comma (&ptr) == FAIL)
6789                     goto wanted_comma;
6790                   if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6791                                                   &optype)) == FAIL)
6792                     {
6793                       first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6794                       return FAIL;
6795                     }
6796                   inst.operands[i].reg = val;
6797                   inst.operands[i].isreg = 1;
6798                   inst.operands[i].isvec = 1;
6799                   inst.operands[i].issingle = 1;
6800                   inst.operands[i].vectype = optype;
6801                   inst.operands[i].present = 1;
6802                 }
6803             }
6804           else
6805             {
6806               if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ))
6807                        != FAIL)
6808                 {
6809                   /* Case 16: VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>  */
6810                   inst.operands[i].reg = val;
6811                   inst.operands[i].isvec = 1;
6812                   inst.operands[i].isscalar = 2;
6813                   inst.operands[i].vectype = optype;
6814                   inst.operands[i++].present = 1;
6815
6816                   if (skip_past_comma (&ptr) == FAIL)
6817                     goto wanted_comma;
6818
6819                   if ((val = parse_scalar (&ptr, 8, &optype, REG_TYPE_MQ))
6820                       == FAIL)
6821                     {
6822                       first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
6823                       return FAIL;
6824                     }
6825                   inst.operands[i].reg = val;
6826                   inst.operands[i].isvec = 1;
6827                   inst.operands[i].isscalar = 2;
6828                   inst.operands[i].vectype = optype;
6829                   inst.operands[i].present = 1;
6830                 }
6831               else
6832                 {
6833                   first_error (_("VFP single, double or MVE vector register"
6834                                " expected"));
6835                   return FAIL;
6836                 }
6837             }
6838         }
6839       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6840                != FAIL)
6841         {
6842           /* Case 13.  */
6843           inst.operands[i].reg = val;
6844           inst.operands[i].isreg = 1;
6845           inst.operands[i].isvec = 1;
6846           inst.operands[i].issingle = 1;
6847           inst.operands[i].vectype = optype;
6848           inst.operands[i].present = 1;
6849         }
6850     }
6851   else
6852     {
6853       first_error (_("parse error"));
6854       return FAIL;
6855     }
6856
6857   /* Successfully parsed the operands. Update args.  */
6858   *which_operand = i;
6859   *str = ptr;
6860   return SUCCESS;
6861
6862  wanted_comma:
6863   first_error (_("expected comma"));
6864   return FAIL;
6865
6866  wanted_arm:
6867   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6868   return FAIL;
6869 }
6870
6871 /* Use this macro when the operand constraints are different
6872    for ARM and THUMB (e.g. ldrd).  */
6873 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6874         ((arm_operand) | ((thumb_operand) << 16))
6875
6876 /* Matcher codes for parse_operands.  */
6877 enum operand_parse_code
6878 {
6879   OP_stop,      /* end of line */
6880
6881   OP_RR,        /* ARM register */
6882   OP_RRnpc,     /* ARM register, not r15 */
6883   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6884   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6885   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6886                    optional trailing ! */
6887   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6888   OP_RCP,       /* Coprocessor number */
6889   OP_RCN,       /* Coprocessor register */
6890   OP_RF,        /* FPA register */
6891   OP_RVS,       /* VFP single precision register */
6892   OP_RVD,       /* VFP double precision register (0..15) */
6893   OP_RND,       /* Neon double precision register (0..31) */
6894   OP_RNDMQ,     /* Neon double precision (0..31) or MVE vector register.  */
6895   OP_RNDMQR,    /* Neon double precision (0..31), MVE vector or ARM register.
6896                  */
6897   OP_RNQ,       /* Neon quad precision register */
6898   OP_RNQMQ,     /* Neon quad or MVE vector register.  */
6899   OP_RVSD,      /* VFP single or double precision register */
6900   OP_RVSD_COND, /* VFP single, double precision register or condition code.  */
6901   OP_RVSDMQ,    /* VFP single, double precision or MVE vector register.  */
6902   OP_RNSD,      /* Neon single or double precision register */
6903   OP_RNDQ,      /* Neon double or quad precision register */
6904   OP_RNDQMQ,     /* Neon double, quad or MVE vector register.  */
6905   OP_RNSDQ,     /* Neon single, double or quad precision register */
6906   OP_RNSC,      /* Neon scalar D[X] */
6907   OP_RVC,       /* VFP control register */
6908   OP_RMF,       /* Maverick F register */
6909   OP_RMD,       /* Maverick D register */
6910   OP_RMFX,      /* Maverick FX register */
6911   OP_RMDX,      /* Maverick DX register */
6912   OP_RMAX,      /* Maverick AX register */
6913   OP_RMDS,      /* Maverick DSPSC register */
6914   OP_RIWR,      /* iWMMXt wR register */
6915   OP_RIWC,      /* iWMMXt wC register */
6916   OP_RIWG,      /* iWMMXt wCG register */
6917   OP_RXA,       /* XScale accumulator register */
6918
6919   OP_RNSDQMQ,   /* Neon single, double or quad register or MVE vector register
6920                  */
6921   OP_RNSDQMQR,  /* Neon single, double or quad register, MVE vector register or
6922                    GPR (no SP/SP)  */
6923   OP_RMQ,       /* MVE vector register.  */
6924   OP_RMQRZ,     /* MVE vector or ARM register including ZR.  */
6925
6926   /* New operands for Armv8.1-M Mainline.  */
6927   OP_LR,        /* ARM LR register */
6928   OP_RRe,       /* ARM register, only even numbered.  */
6929   OP_RRo,       /* ARM register, only odd numbered, not r13 or r15.  */
6930   OP_RRnpcsp_I32, /* ARM register (no BadReg) or literal 1 .. 32 */
6931
6932   OP_REGLST,    /* ARM register list */
6933   OP_CLRMLST,   /* CLRM register list */
6934   OP_VRSLST,    /* VFP single-precision register list */
6935   OP_VRDLST,    /* VFP double-precision register list */
6936   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6937   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6938   OP_NSTRLST,   /* Neon element/structure list */
6939   OP_VRSDVLST,  /* VFP single or double-precision register list and VPR */
6940   OP_MSTRLST2,  /* MVE vector list with two elements.  */
6941   OP_MSTRLST4,  /* MVE vector list with four elements.  */
6942
6943   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6944   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6945   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6946   OP_RSVDMQ_FI0, /* VFP S, D, MVE vector register or floating point immediate
6947                     zero.  */
6948   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6949   OP_RNSD_RNSC, /* Neon S or D reg, or Neon scalar.  */
6950   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6951   OP_RNSDQ_RNSC_MQ, /* Vector S, D or Q reg, Neon scalar or MVE vector register.
6952                      */
6953   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6954   OP_RNDQMQ_RNSC, /* Neon D, Q or MVE vector reg, or Neon scalar.  */
6955   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6956   OP_VMOV,      /* Neon VMOV operands.  */
6957   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6958   /* Neon D, Q or MVE vector register, or big immediate for logic and VMVN.  */
6959   OP_RNDQMQ_Ibig,
6960   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6961   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6962   OP_VLDR,      /* VLDR operand.  */
6963
6964   OP_I0,        /* immediate zero */
6965   OP_I7,        /* immediate value 0 .. 7 */
6966   OP_I15,       /*                 0 .. 15 */
6967   OP_I16,       /*                 1 .. 16 */
6968   OP_I16z,      /*                 0 .. 16 */
6969   OP_I31,       /*                 0 .. 31 */
6970   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6971   OP_I32,       /*                 1 .. 32 */
6972   OP_I32z,      /*                 0 .. 32 */
6973   OP_I63,       /*                 0 .. 63 */
6974   OP_I63s,      /*               -64 .. 63 */
6975   OP_I64,       /*                 1 .. 64 */
6976   OP_I64z,      /*                 0 .. 64 */
6977   OP_I255,      /*                 0 .. 255 */
6978
6979   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6980   OP_I7b,       /*                             0 .. 7 */
6981   OP_I15b,      /*                             0 .. 15 */
6982   OP_I31b,      /*                             0 .. 31 */
6983
6984   OP_SH,        /* shifter operand */
6985   OP_SHG,       /* shifter operand with possible group relocation */
6986   OP_ADDR,      /* Memory address expression (any mode) */
6987   OP_ADDRMVE,   /* Memory address expression for MVE's VSTR/VLDR.  */
6988   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6989   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6990   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6991   OP_EXP,       /* arbitrary expression */
6992   OP_EXPi,      /* same, with optional immediate prefix */
6993   OP_EXPr,      /* same, with optional relocation suffix */
6994   OP_EXPs,      /* same, with optional non-first operand relocation suffix */
6995   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6996   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
6997   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
6998
6999   OP_CPSF,      /* CPS flags */
7000   OP_ENDI,      /* Endianness specifier */
7001   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
7002   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
7003   OP_COND,      /* conditional code */
7004   OP_TB,        /* Table branch.  */
7005
7006   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
7007
7008   OP_RRnpc_I0,  /* ARM register or literal 0 */
7009   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
7010   OP_RR_EXi,    /* ARM register or expression with imm prefix */
7011   OP_RF_IF,     /* FPA register or immediate */
7012   OP_RIWR_RIWC, /* iWMMXt R or C reg */
7013   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
7014
7015   /* Optional operands.  */
7016   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
7017   OP_oI31b,      /*                             0 .. 31 */
7018   OP_oI32b,      /*                             1 .. 32 */
7019   OP_oI32z,      /*                             0 .. 32 */
7020   OP_oIffffb,    /*                             0 .. 65535 */
7021   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
7022
7023   OP_oRR,        /* ARM register */
7024   OP_oLR,        /* ARM LR register */
7025   OP_oRRnpc,     /* ARM register, not the PC */
7026   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
7027   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
7028   OP_oRND,       /* Optional Neon double precision register */
7029   OP_oRNQ,       /* Optional Neon quad precision register */
7030   OP_oRNDQMQ,     /* Optional Neon double, quad or MVE vector register.  */
7031   OP_oRNDQ,      /* Optional Neon double or quad precision register */
7032   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
7033   OP_oRNSDQMQ,   /* Optional single, double or quad register or MVE vector
7034                     register.  */
7035   OP_oSHll,      /* LSL immediate */
7036   OP_oSHar,      /* ASR immediate */
7037   OP_oSHllar,    /* LSL or ASR immediate */
7038   OP_oROR,       /* ROR 0/8/16/24 */
7039   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
7040
7041   OP_oRMQRZ,    /* optional MVE vector or ARM register including ZR.  */
7042
7043   /* Some pre-defined mixed (ARM/THUMB) operands.  */
7044   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
7045   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
7046   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
7047
7048   OP_FIRST_OPTIONAL = OP_oI7b
7049 };
7050
7051 /* Generic instruction operand parser.  This does no encoding and no
7052    semantic validation; it merely squirrels values away in the inst
7053    structure.  Returns SUCCESS or FAIL depending on whether the
7054    specified grammar matched.  */
7055 static int
7056 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
7057 {
7058   unsigned const int *upat = pattern;
7059   char *backtrack_pos = 0;
7060   const char *backtrack_error = 0;
7061   int i, val = 0, backtrack_index = 0;
7062   enum arm_reg_type rtype;
7063   parse_operand_result result;
7064   unsigned int op_parse_code;
7065   bfd_boolean partial_match;
7066
7067 #define po_char_or_fail(chr)                    \
7068   do                                            \
7069     {                                           \
7070       if (skip_past_char (&str, chr) == FAIL)   \
7071         goto bad_args;                          \
7072     }                                           \
7073   while (0)
7074
7075 #define po_reg_or_fail(regtype)                                 \
7076   do                                                            \
7077     {                                                           \
7078       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7079                                  & inst.operands[i].vectype);   \
7080       if (val == FAIL)                                          \
7081         {                                                       \
7082           first_error (_(reg_expected_msgs[regtype]));          \
7083           goto failure;                                         \
7084         }                                                       \
7085       inst.operands[i].reg = val;                               \
7086       inst.operands[i].isreg = 1;                               \
7087       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7088       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7089       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7090                              || rtype == REG_TYPE_VFD           \
7091                              || rtype == REG_TYPE_NQ);          \
7092       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7093     }                                                           \
7094   while (0)
7095
7096 #define po_reg_or_goto(regtype, label)                          \
7097   do                                                            \
7098     {                                                           \
7099       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
7100                                  & inst.operands[i].vectype);   \
7101       if (val == FAIL)                                          \
7102         goto label;                                             \
7103                                                                 \
7104       inst.operands[i].reg = val;                               \
7105       inst.operands[i].isreg = 1;                               \
7106       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
7107       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
7108       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
7109                              || rtype == REG_TYPE_VFD           \
7110                              || rtype == REG_TYPE_NQ);          \
7111       inst.operands[i].iszr = (rtype == REG_TYPE_ZR);           \
7112     }                                                           \
7113   while (0)
7114
7115 #define po_imm_or_fail(min, max, popt)                          \
7116   do                                                            \
7117     {                                                           \
7118       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
7119         goto failure;                                           \
7120       inst.operands[i].imm = val;                               \
7121     }                                                           \
7122   while (0)
7123
7124 #define po_scalar_or_goto(elsz, label, reg_type)                        \
7125   do                                                                    \
7126     {                                                                   \
7127       val = parse_scalar (& str, elsz, & inst.operands[i].vectype,      \
7128                           reg_type);                                    \
7129       if (val == FAIL)                                                  \
7130         goto label;                                                     \
7131       inst.operands[i].reg = val;                                       \
7132       inst.operands[i].isscalar = 1;                                    \
7133     }                                                                   \
7134   while (0)
7135
7136 #define po_misc_or_fail(expr)                   \
7137   do                                            \
7138     {                                           \
7139       if (expr)                                 \
7140         goto failure;                           \
7141     }                                           \
7142   while (0)
7143
7144 #define po_misc_or_fail_no_backtrack(expr)              \
7145   do                                                    \
7146     {                                                   \
7147       result = expr;                                    \
7148       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
7149         backtrack_pos = 0;                              \
7150       if (result != PARSE_OPERAND_SUCCESS)              \
7151         goto failure;                                   \
7152     }                                                   \
7153   while (0)
7154
7155 #define po_barrier_or_imm(str)                             \
7156   do                                                       \
7157     {                                                      \
7158       val = parse_barrier (&str);                          \
7159       if (val == FAIL && ! ISALPHA (*str))                 \
7160         goto immediate;                                    \
7161       if (val == FAIL                                      \
7162           /* ISB can only take SY as an option.  */        \
7163           || ((inst.instruction & 0xf0) == 0x60            \
7164                && val != 0xf))                             \
7165         {                                                  \
7166            inst.error = _("invalid barrier type");         \
7167            backtrack_pos = 0;                              \
7168            goto failure;                                   \
7169         }                                                  \
7170     }                                                      \
7171   while (0)
7172
7173   skip_whitespace (str);
7174
7175   for (i = 0; upat[i] != OP_stop; i++)
7176     {
7177       op_parse_code = upat[i];
7178       if (op_parse_code >= 1<<16)
7179         op_parse_code = thumb ? (op_parse_code >> 16)
7180                                 : (op_parse_code & ((1<<16)-1));
7181
7182       if (op_parse_code >= OP_FIRST_OPTIONAL)
7183         {
7184           /* Remember where we are in case we need to backtrack.  */
7185           backtrack_pos = str;
7186           backtrack_error = inst.error;
7187           backtrack_index = i;
7188         }
7189
7190       if (i > 0 && (i > 1 || inst.operands[0].present))
7191         po_char_or_fail (',');
7192
7193       switch (op_parse_code)
7194         {
7195           /* Registers */
7196         case OP_oRRnpc:
7197         case OP_oRRnpcsp:
7198         case OP_RRnpc:
7199         case OP_RRnpcsp:
7200         case OP_oRR:
7201         case OP_RRe:
7202         case OP_RRo:
7203         case OP_LR:
7204         case OP_oLR:
7205         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
7206         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
7207         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
7208         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
7209         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
7210         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
7211         case OP_oRND:
7212         case OP_RNDMQR:
7213           po_reg_or_goto (REG_TYPE_RN, try_rndmq);
7214           break;
7215         try_rndmq:
7216         case OP_RNDMQ:
7217           po_reg_or_goto (REG_TYPE_MQ, try_rnd);
7218           break;
7219         try_rnd:
7220         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
7221         case OP_RVC:
7222           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
7223           break;
7224           /* Also accept generic coprocessor regs for unknown registers.  */
7225           coproc_reg:
7226           po_reg_or_fail (REG_TYPE_CN);
7227           break;
7228         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
7229         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
7230         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
7231         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
7232         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
7233         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
7234         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
7235         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
7236         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
7237         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
7238         case OP_oRNQ:
7239         case OP_RNQMQ:
7240           po_reg_or_goto (REG_TYPE_MQ, try_nq);
7241           break;
7242         try_nq:
7243         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
7244         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
7245         case OP_oRNDQMQ:
7246         case OP_RNDQMQ:
7247           po_reg_or_goto (REG_TYPE_MQ, try_rndq);
7248           break;
7249         try_rndq:
7250         case OP_oRNDQ:
7251         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
7252         case OP_RVSDMQ:
7253           po_reg_or_goto (REG_TYPE_MQ, try_rvsd);
7254           break;
7255         try_rvsd:
7256         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
7257         case OP_RVSD_COND:
7258           po_reg_or_goto (REG_TYPE_VFSD, try_cond);
7259           break;
7260         case OP_oRNSDQ:
7261         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
7262         case OP_RNSDQMQR:
7263           po_reg_or_goto (REG_TYPE_RN, try_mq);
7264           break;
7265           try_mq:
7266         case OP_oRNSDQMQ:
7267         case OP_RNSDQMQ:
7268           po_reg_or_goto (REG_TYPE_MQ, try_nsdq2);
7269           break;
7270           try_nsdq2:
7271           po_reg_or_fail (REG_TYPE_NSDQ);
7272           inst.error = 0;
7273           break;
7274         case OP_RMQ:
7275           po_reg_or_fail (REG_TYPE_MQ);
7276           break;
7277         /* Neon scalar. Using an element size of 8 means that some invalid
7278            scalars are accepted here, so deal with those in later code.  */
7279         case OP_RNSC:  po_scalar_or_goto (8, failure, REG_TYPE_VFD);    break;
7280
7281         case OP_RNDQ_I0:
7282           {
7283             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
7284             break;
7285             try_imm0:
7286             po_imm_or_fail (0, 0, TRUE);
7287           }
7288           break;
7289
7290         case OP_RVSD_I0:
7291           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
7292           break;
7293
7294         case OP_RSVDMQ_FI0:
7295           po_reg_or_goto (REG_TYPE_MQ, try_rsvd_fi0);
7296           break;
7297         try_rsvd_fi0:
7298         case OP_RSVD_FI0:
7299           {
7300             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
7301             break;
7302             try_ifimm0:
7303             if (parse_ifimm_zero (&str))
7304               inst.operands[i].imm = 0;
7305             else
7306             {
7307               inst.error
7308                 = _("only floating point zero is allowed as immediate value");
7309               goto failure;
7310             }
7311           }
7312           break;
7313
7314         case OP_RR_RNSC:
7315           {
7316             po_scalar_or_goto (8, try_rr, REG_TYPE_VFD);
7317             break;
7318             try_rr:
7319             po_reg_or_fail (REG_TYPE_RN);
7320           }
7321           break;
7322
7323         case OP_RNSDQ_RNSC_MQ:
7324           po_reg_or_goto (REG_TYPE_MQ, try_rnsdq_rnsc);
7325           break;
7326         try_rnsdq_rnsc:
7327         case OP_RNSDQ_RNSC:
7328           {
7329             po_scalar_or_goto (8, try_nsdq, REG_TYPE_VFD);
7330             inst.error = 0;
7331             break;
7332             try_nsdq:
7333             po_reg_or_fail (REG_TYPE_NSDQ);
7334             inst.error = 0;
7335           }
7336           break;
7337
7338         case OP_RNSD_RNSC:
7339           {
7340             po_scalar_or_goto (8, try_s_scalar, REG_TYPE_VFD);
7341             break;
7342             try_s_scalar:
7343             po_scalar_or_goto (4, try_nsd, REG_TYPE_VFS);
7344             break;
7345             try_nsd:
7346             po_reg_or_fail (REG_TYPE_NSD);
7347           }
7348           break;
7349
7350         case OP_RNDQMQ_RNSC:
7351           po_reg_or_goto (REG_TYPE_MQ, try_rndq_rnsc);
7352           break;
7353         try_rndq_rnsc:
7354         case OP_RNDQ_RNSC:
7355           {
7356             po_scalar_or_goto (8, try_ndq, REG_TYPE_VFD);
7357             break;
7358             try_ndq:
7359             po_reg_or_fail (REG_TYPE_NDQ);
7360           }
7361           break;
7362
7363         case OP_RND_RNSC:
7364           {
7365             po_scalar_or_goto (8, try_vfd, REG_TYPE_VFD);
7366             break;
7367             try_vfd:
7368             po_reg_or_fail (REG_TYPE_VFD);
7369           }
7370           break;
7371
7372         case OP_VMOV:
7373           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
7374              not careful then bad things might happen.  */
7375           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
7376           break;
7377
7378         case OP_RNDQMQ_Ibig:
7379           po_reg_or_goto (REG_TYPE_MQ, try_rndq_ibig);
7380           break;
7381         try_rndq_ibig:
7382         case OP_RNDQ_Ibig:
7383           {
7384             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
7385             break;
7386             try_immbig:
7387             /* There's a possibility of getting a 64-bit immediate here, so
7388                we need special handling.  */
7389             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
7390                 == FAIL)
7391               {
7392                 inst.error = _("immediate value is out of range");
7393                 goto failure;
7394               }
7395           }
7396           break;
7397
7398         case OP_RNDQ_I63b:
7399           {
7400             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
7401             break;
7402             try_shimm:
7403             po_imm_or_fail (0, 63, TRUE);
7404           }
7405           break;
7406
7407         case OP_RRnpcb:
7408           po_char_or_fail ('[');
7409           po_reg_or_fail  (REG_TYPE_RN);
7410           po_char_or_fail (']');
7411           break;
7412
7413         case OP_RRnpctw:
7414         case OP_RRw:
7415         case OP_oRRw:
7416           po_reg_or_fail (REG_TYPE_RN);
7417           if (skip_past_char (&str, '!') == SUCCESS)
7418             inst.operands[i].writeback = 1;
7419           break;
7420
7421           /* Immediates */
7422         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
7423         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
7424         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
7425         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
7426         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
7427         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
7428         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
7429         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
7430         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
7431         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
7432         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
7433         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
7434
7435         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
7436         case OP_oI7b:
7437         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
7438         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
7439         case OP_oI31b:
7440         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
7441         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
7442         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
7443         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
7444
7445           /* Immediate variants */
7446         case OP_oI255c:
7447           po_char_or_fail ('{');
7448           po_imm_or_fail (0, 255, TRUE);
7449           po_char_or_fail ('}');
7450           break;
7451
7452         case OP_I31w:
7453           /* The expression parser chokes on a trailing !, so we have
7454              to find it first and zap it.  */
7455           {
7456             char *s = str;
7457             while (*s && *s != ',')
7458               s++;
7459             if (s[-1] == '!')
7460               {
7461                 s[-1] = '\0';
7462                 inst.operands[i].writeback = 1;
7463               }
7464             po_imm_or_fail (0, 31, TRUE);
7465             if (str == s - 1)
7466               str = s;
7467           }
7468           break;
7469
7470           /* Expressions */
7471         case OP_EXPi:   EXPi:
7472           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7473                                               GE_OPT_PREFIX));
7474           break;
7475
7476         case OP_EXP:
7477           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7478                                               GE_NO_PREFIX));
7479           break;
7480
7481         case OP_EXPr:   EXPr:
7482           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7483                                               GE_NO_PREFIX));
7484           if (inst.relocs[0].exp.X_op == O_symbol)
7485             {
7486               val = parse_reloc (&str);
7487               if (val == -1)
7488                 {
7489                   inst.error = _("unrecognized relocation suffix");
7490                   goto failure;
7491                 }
7492               else if (val != BFD_RELOC_UNUSED)
7493                 {
7494                   inst.operands[i].imm = val;
7495                   inst.operands[i].hasreloc = 1;
7496                 }
7497             }
7498           break;
7499
7500         case OP_EXPs:
7501           po_misc_or_fail (my_get_expression (&inst.relocs[i].exp, &str,
7502                                               GE_NO_PREFIX));
7503           if (inst.relocs[i].exp.X_op == O_symbol)
7504             {
7505               inst.operands[i].hasreloc = 1;
7506             }
7507           else if (inst.relocs[i].exp.X_op == O_constant)
7508             {
7509               inst.operands[i].imm = inst.relocs[i].exp.X_add_number;
7510               inst.operands[i].hasreloc = 0;
7511             }
7512           break;
7513
7514           /* Operand for MOVW or MOVT.  */
7515         case OP_HALF:
7516           po_misc_or_fail (parse_half (&str));
7517           break;
7518
7519           /* Register or expression.  */
7520         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7521         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7522
7523           /* Register or immediate.  */
7524         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7525         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7526
7527         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7528         IF:
7529           if (!is_immediate_prefix (*str))
7530             goto bad_args;
7531           str++;
7532           val = parse_fpa_immediate (&str);
7533           if (val == FAIL)
7534             goto failure;
7535           /* FPA immediates are encoded as registers 8-15.
7536              parse_fpa_immediate has already applied the offset.  */
7537           inst.operands[i].reg = val;
7538           inst.operands[i].isreg = 1;
7539           break;
7540
7541         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7542         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7543
7544           /* Two kinds of register.  */
7545         case OP_RIWR_RIWC:
7546           {
7547             struct reg_entry *rege = arm_reg_parse_multi (&str);
7548             if (!rege
7549                 || (rege->type != REG_TYPE_MMXWR
7550                     && rege->type != REG_TYPE_MMXWC
7551                     && rege->type != REG_TYPE_MMXWCG))
7552               {
7553                 inst.error = _("iWMMXt data or control register expected");
7554                 goto failure;
7555               }
7556             inst.operands[i].reg = rege->number;
7557             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7558           }
7559           break;
7560
7561         case OP_RIWC_RIWG:
7562           {
7563             struct reg_entry *rege = arm_reg_parse_multi (&str);
7564             if (!rege
7565                 || (rege->type != REG_TYPE_MMXWC
7566                     && rege->type != REG_TYPE_MMXWCG))
7567               {
7568                 inst.error = _("iWMMXt control register expected");
7569                 goto failure;
7570               }
7571             inst.operands[i].reg = rege->number;
7572             inst.operands[i].isreg = 1;
7573           }
7574           break;
7575
7576           /* Misc */
7577         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7578         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7579         case OP_oROR:    val = parse_ror (&str);                break;
7580         try_cond:
7581         case OP_COND:    val = parse_cond (&str);               break;
7582         case OP_oBARRIER_I15:
7583           po_barrier_or_imm (str); break;
7584           immediate:
7585           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7586             goto failure;
7587           break;
7588
7589         case OP_wPSR:
7590         case OP_rPSR:
7591           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7592           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7593             {
7594               inst.error = _("Banked registers are not available with this "
7595                              "architecture.");
7596               goto failure;
7597             }
7598           break;
7599           try_psr:
7600           val = parse_psr (&str, op_parse_code == OP_wPSR);
7601           break;
7602
7603         case OP_VLDR:
7604           po_reg_or_goto (REG_TYPE_VFSD, try_sysreg);
7605           break;
7606         try_sysreg:
7607           val = parse_sys_vldr_vstr (&str);
7608           break;
7609
7610         case OP_APSR_RR:
7611           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7612           break;
7613           try_apsr:
7614           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7615              instruction).  */
7616           if (strncasecmp (str, "APSR_", 5) == 0)
7617             {
7618               unsigned found = 0;
7619               str += 5;
7620               while (found < 15)
7621                 switch (*str++)
7622                   {
7623                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7624                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7625                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7626                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7627                   default: found = 16;
7628                   }
7629               if (found != 15)
7630                 goto failure;
7631               inst.operands[i].isvec = 1;
7632               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7633               inst.operands[i].reg = REG_PC;
7634             }
7635           else
7636             goto failure;
7637           break;
7638
7639         case OP_TB:
7640           po_misc_or_fail (parse_tb (&str));
7641           break;
7642
7643           /* Register lists.  */
7644         case OP_REGLST:
7645           val = parse_reg_list (&str, REGLIST_RN);
7646           if (*str == '^')
7647             {
7648               inst.operands[i].writeback = 1;
7649               str++;
7650             }
7651           break;
7652
7653         case OP_CLRMLST:
7654           val = parse_reg_list (&str, REGLIST_CLRM);
7655           break;
7656
7657         case OP_VRSLST:
7658           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S,
7659                                     &partial_match);
7660           break;
7661
7662         case OP_VRDLST:
7663           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D,
7664                                     &partial_match);
7665           break;
7666
7667         case OP_VRSDLST:
7668           /* Allow Q registers too.  */
7669           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7670                                     REGLIST_NEON_D, &partial_match);
7671           if (val == FAIL)
7672             {
7673               inst.error = NULL;
7674               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7675                                         REGLIST_VFP_S, &partial_match);
7676               inst.operands[i].issingle = 1;
7677             }
7678           break;
7679
7680         case OP_VRSDVLST:
7681           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7682                                     REGLIST_VFP_D_VPR, &partial_match);
7683           if (val == FAIL && !partial_match)
7684             {
7685               inst.error = NULL;
7686               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7687                                         REGLIST_VFP_S_VPR, &partial_match);
7688               inst.operands[i].issingle = 1;
7689             }
7690           break;
7691
7692         case OP_NRDLST:
7693           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7694                                     REGLIST_NEON_D, &partial_match);
7695           break;
7696
7697         case OP_MSTRLST4:
7698         case OP_MSTRLST2:
7699           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7700                                            1, &inst.operands[i].vectype);
7701           if (val != (((op_parse_code == OP_MSTRLST2) ? 3 : 7) << 5 | 0xe))
7702             goto failure;
7703           break;
7704         case OP_NSTRLST:
7705           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7706                                            0, &inst.operands[i].vectype);
7707           break;
7708
7709           /* Addressing modes */
7710         case OP_ADDRMVE:
7711           po_misc_or_fail (parse_address_group_reloc (&str, i, GROUP_MVE));
7712           break;
7713
7714         case OP_ADDR:
7715           po_misc_or_fail (parse_address (&str, i));
7716           break;
7717
7718         case OP_ADDRGLDR:
7719           po_misc_or_fail_no_backtrack (
7720             parse_address_group_reloc (&str, i, GROUP_LDR));
7721           break;
7722
7723         case OP_ADDRGLDRS:
7724           po_misc_or_fail_no_backtrack (
7725             parse_address_group_reloc (&str, i, GROUP_LDRS));
7726           break;
7727
7728         case OP_ADDRGLDC:
7729           po_misc_or_fail_no_backtrack (
7730             parse_address_group_reloc (&str, i, GROUP_LDC));
7731           break;
7732
7733         case OP_SH:
7734           po_misc_or_fail (parse_shifter_operand (&str, i));
7735           break;
7736
7737         case OP_SHG:
7738           po_misc_or_fail_no_backtrack (
7739             parse_shifter_operand_group_reloc (&str, i));
7740           break;
7741
7742         case OP_oSHll:
7743           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7744           break;
7745
7746         case OP_oSHar:
7747           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7748           break;
7749
7750         case OP_oSHllar:
7751           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7752           break;
7753
7754         case OP_RMQRZ:
7755         case OP_oRMQRZ:
7756           po_reg_or_goto (REG_TYPE_MQ, try_rr_zr);
7757           break;
7758         try_rr_zr:
7759           po_reg_or_goto (REG_TYPE_RN, ZR);
7760           break;
7761         ZR:
7762           po_reg_or_fail (REG_TYPE_ZR);
7763           break;
7764
7765         default:
7766           as_fatal (_("unhandled operand code %d"), op_parse_code);
7767         }
7768
7769       /* Various value-based sanity checks and shared operations.  We
7770          do not signal immediate failures for the register constraints;
7771          this allows a syntax error to take precedence.  */
7772       switch (op_parse_code)
7773         {
7774         case OP_oRRnpc:
7775         case OP_RRnpc:
7776         case OP_RRnpcb:
7777         case OP_RRw:
7778         case OP_oRRw:
7779         case OP_RRnpc_I0:
7780           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7781             inst.error = BAD_PC;
7782           break;
7783
7784         case OP_oRRnpcsp:
7785         case OP_RRnpcsp:
7786           if (inst.operands[i].isreg)
7787             {
7788               if (inst.operands[i].reg == REG_PC)
7789                 inst.error = BAD_PC;
7790               else if (inst.operands[i].reg == REG_SP
7791                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7792                           relaxed since ARMv8-A.  */
7793                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7794                 {
7795                   gas_assert (thumb);
7796                   inst.error = BAD_SP;
7797                 }
7798             }
7799           break;
7800
7801         case OP_RRnpctw:
7802           if (inst.operands[i].isreg
7803               && inst.operands[i].reg == REG_PC
7804               && (inst.operands[i].writeback || thumb))
7805             inst.error = BAD_PC;
7806           break;
7807
7808         case OP_RVSD_COND:
7809         case OP_VLDR:
7810           if (inst.operands[i].isreg)
7811             break;
7812         /* fall through.  */
7813
7814         case OP_CPSF:
7815         case OP_ENDI:
7816         case OP_oROR:
7817         case OP_wPSR:
7818         case OP_rPSR:
7819         case OP_COND:
7820         case OP_oBARRIER_I15:
7821         case OP_REGLST:
7822         case OP_CLRMLST:
7823         case OP_VRSLST:
7824         case OP_VRDLST:
7825         case OP_VRSDLST:
7826         case OP_VRSDVLST:
7827         case OP_NRDLST:
7828         case OP_NSTRLST:
7829         case OP_MSTRLST2:
7830         case OP_MSTRLST4:
7831           if (val == FAIL)
7832             goto failure;
7833           inst.operands[i].imm = val;
7834           break;
7835
7836         case OP_LR:
7837         case OP_oLR:
7838           if (inst.operands[i].reg != REG_LR)
7839             inst.error = _("operand must be LR register");
7840           break;
7841
7842         case OP_RMQRZ:
7843         case OP_oRMQRZ:
7844           if (!inst.operands[i].iszr && inst.operands[i].reg == REG_PC)
7845             inst.error = BAD_PC;
7846           break;
7847
7848         case OP_RRe:
7849           if (inst.operands[i].isreg
7850               && (inst.operands[i].reg & 0x00000001) != 0)
7851             inst.error = BAD_ODD;
7852           break;
7853
7854         case OP_RRo:
7855           if (inst.operands[i].isreg)
7856             {
7857               if ((inst.operands[i].reg & 0x00000001) != 1)
7858                 inst.error = BAD_EVEN;
7859               else if (inst.operands[i].reg == REG_SP)
7860                 as_tsktsk (MVE_BAD_SP);
7861               else if (inst.operands[i].reg == REG_PC)
7862                 inst.error = BAD_PC;
7863             }
7864           break;
7865
7866         default:
7867           break;
7868         }
7869
7870       /* If we get here, this operand was successfully parsed.  */
7871       inst.operands[i].present = 1;
7872       continue;
7873
7874     bad_args:
7875       inst.error = BAD_ARGS;
7876
7877     failure:
7878       if (!backtrack_pos)
7879         {
7880           /* The parse routine should already have set inst.error, but set a
7881              default here just in case.  */
7882           if (!inst.error)
7883             inst.error = BAD_SYNTAX;
7884           return FAIL;
7885         }
7886
7887       /* Do not backtrack over a trailing optional argument that
7888          absorbed some text.  We will only fail again, with the
7889          'garbage following instruction' error message, which is
7890          probably less helpful than the current one.  */
7891       if (backtrack_index == i && backtrack_pos != str
7892           && upat[i+1] == OP_stop)
7893         {
7894           if (!inst.error)
7895             inst.error = BAD_SYNTAX;
7896           return FAIL;
7897         }
7898
7899       /* Try again, skipping the optional argument at backtrack_pos.  */
7900       str = backtrack_pos;
7901       inst.error = backtrack_error;
7902       inst.operands[backtrack_index].present = 0;
7903       i = backtrack_index;
7904       backtrack_pos = 0;
7905     }
7906
7907   /* Check that we have parsed all the arguments.  */
7908   if (*str != '\0' && !inst.error)
7909     inst.error = _("garbage following instruction");
7910
7911   return inst.error ? FAIL : SUCCESS;
7912 }
7913
7914 #undef po_char_or_fail
7915 #undef po_reg_or_fail
7916 #undef po_reg_or_goto
7917 #undef po_imm_or_fail
7918 #undef po_scalar_or_fail
7919 #undef po_barrier_or_imm
7920
7921 /* Shorthand macro for instruction encoding functions issuing errors.  */
7922 #define constraint(expr, err)                   \
7923   do                                            \
7924     {                                           \
7925       if (expr)                                 \
7926         {                                       \
7927           inst.error = err;                     \
7928           return;                               \
7929         }                                       \
7930     }                                           \
7931   while (0)
7932
7933 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7934    instructions are unpredictable if these registers are used.  This
7935    is the BadReg predicate in ARM's Thumb-2 documentation.
7936
7937    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
7938    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
7939 #define reject_bad_reg(reg)                                     \
7940   do                                                            \
7941    if (reg == REG_PC)                                           \
7942      {                                                          \
7943        inst.error = BAD_PC;                                     \
7944        return;                                                  \
7945      }                                                          \
7946    else if (reg == REG_SP                                       \
7947             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
7948      {                                                          \
7949        inst.error = BAD_SP;                                     \
7950        return;                                                  \
7951      }                                                          \
7952   while (0)
7953
7954 /* If REG is R13 (the stack pointer), warn that its use is
7955    deprecated.  */
7956 #define warn_deprecated_sp(reg)                 \
7957   do                                            \
7958     if (warn_on_deprecated && reg == REG_SP)    \
7959        as_tsktsk (_("use of r13 is deprecated"));       \
7960   while (0)
7961
7962 /* Functions for operand encoding.  ARM, then Thumb.  */
7963
7964 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7965
7966 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
7967
7968    The only binary encoding difference is the Coprocessor number.  Coprocessor
7969    9 is used for half-precision calculations or conversions.  The format of the
7970    instruction is the same as the equivalent Coprocessor 10 instruction that
7971    exists for Single-Precision operation.  */
7972
7973 static void
7974 do_scalar_fp16_v82_encode (void)
7975 {
7976   if (inst.cond < COND_ALWAYS)
7977     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
7978                " the behaviour is UNPREDICTABLE"));
7979   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
7980               _(BAD_FP16));
7981
7982   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
7983   mark_feature_used (&arm_ext_fp16);
7984 }
7985
7986 /* If VAL can be encoded in the immediate field of an ARM instruction,
7987    return the encoded form.  Otherwise, return FAIL.  */
7988
7989 static unsigned int
7990 encode_arm_immediate (unsigned int val)
7991 {
7992   unsigned int a, i;
7993
7994   if (val <= 0xff)
7995     return val;
7996
7997   for (i = 2; i < 32; i += 2)
7998     if ((a = rotate_left (val, i)) <= 0xff)
7999       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
8000
8001   return FAIL;
8002 }
8003
8004 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
8005    return the encoded form.  Otherwise, return FAIL.  */
8006 static unsigned int
8007 encode_thumb32_immediate (unsigned int val)
8008 {
8009   unsigned int a, i;
8010
8011   if (val <= 0xff)
8012     return val;
8013
8014   for (i = 1; i <= 24; i++)
8015     {
8016       a = val >> i;
8017       if ((val & ~(0xff << i)) == 0)
8018         return ((val >> i) & 0x7f) | ((32 - i) << 7);
8019     }
8020
8021   a = val & 0xff;
8022   if (val == ((a << 16) | a))
8023     return 0x100 | a;
8024   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
8025     return 0x300 | a;
8026
8027   a = val & 0xff00;
8028   if (val == ((a << 16) | a))
8029     return 0x200 | (a >> 8);
8030
8031   return FAIL;
8032 }
8033 /* Encode a VFP SP or DP register number into inst.instruction.  */
8034
8035 static void
8036 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
8037 {
8038   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
8039       && reg > 15)
8040     {
8041       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
8042         {
8043           if (thumb_mode)
8044             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
8045                                     fpu_vfp_ext_d32);
8046           else
8047             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
8048                                     fpu_vfp_ext_d32);
8049         }
8050       else
8051         {
8052           first_error (_("D register out of range for selected VFP version"));
8053           return;
8054         }
8055     }
8056
8057   switch (pos)
8058     {
8059     case VFP_REG_Sd:
8060       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
8061       break;
8062
8063     case VFP_REG_Sn:
8064       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
8065       break;
8066
8067     case VFP_REG_Sm:
8068       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
8069       break;
8070
8071     case VFP_REG_Dd:
8072       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
8073       break;
8074
8075     case VFP_REG_Dn:
8076       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
8077       break;
8078
8079     case VFP_REG_Dm:
8080       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
8081       break;
8082
8083     default:
8084       abort ();
8085     }
8086 }
8087
8088 /* Encode a <shift> in an ARM-format instruction.  The immediate,
8089    if any, is handled by md_apply_fix.   */
8090 static void
8091 encode_arm_shift (int i)
8092 {
8093   /* register-shifted register.  */
8094   if (inst.operands[i].immisreg)
8095     {
8096       int op_index;
8097       for (op_index = 0; op_index <= i; ++op_index)
8098         {
8099           /* Check the operand only when it's presented.  In pre-UAL syntax,
8100              if the destination register is the same as the first operand, two
8101              register form of the instruction can be used.  */
8102           if (inst.operands[op_index].present && inst.operands[op_index].isreg
8103               && inst.operands[op_index].reg == REG_PC)
8104             as_warn (UNPRED_REG ("r15"));
8105         }
8106
8107       if (inst.operands[i].imm == REG_PC)
8108         as_warn (UNPRED_REG ("r15"));
8109     }
8110
8111   if (inst.operands[i].shift_kind == SHIFT_RRX)
8112     inst.instruction |= SHIFT_ROR << 5;
8113   else
8114     {
8115       inst.instruction |= inst.operands[i].shift_kind << 5;
8116       if (inst.operands[i].immisreg)
8117         {
8118           inst.instruction |= SHIFT_BY_REG;
8119           inst.instruction |= inst.operands[i].imm << 8;
8120         }
8121       else
8122         inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8123     }
8124 }
8125
8126 static void
8127 encode_arm_shifter_operand (int i)
8128 {
8129   if (inst.operands[i].isreg)
8130     {
8131       inst.instruction |= inst.operands[i].reg;
8132       encode_arm_shift (i);
8133     }
8134   else
8135     {
8136       inst.instruction |= INST_IMMEDIATE;
8137       if (inst.relocs[0].type != BFD_RELOC_ARM_IMMEDIATE)
8138         inst.instruction |= inst.operands[i].imm;
8139     }
8140 }
8141
8142 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
8143 static void
8144 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
8145 {
8146   /* PR 14260:
8147      Generate an error if the operand is not a register.  */
8148   constraint (!inst.operands[i].isreg,
8149               _("Instruction does not support =N addresses"));
8150
8151   inst.instruction |= inst.operands[i].reg << 16;
8152
8153   if (inst.operands[i].preind)
8154     {
8155       if (is_t)
8156         {
8157           inst.error = _("instruction does not accept preindexed addressing");
8158           return;
8159         }
8160       inst.instruction |= PRE_INDEX;
8161       if (inst.operands[i].writeback)
8162         inst.instruction |= WRITE_BACK;
8163
8164     }
8165   else if (inst.operands[i].postind)
8166     {
8167       gas_assert (inst.operands[i].writeback);
8168       if (is_t)
8169         inst.instruction |= WRITE_BACK;
8170     }
8171   else /* unindexed - only for coprocessor */
8172     {
8173       inst.error = _("instruction does not accept unindexed addressing");
8174       return;
8175     }
8176
8177   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
8178       && (((inst.instruction & 0x000f0000) >> 16)
8179           == ((inst.instruction & 0x0000f000) >> 12)))
8180     as_warn ((inst.instruction & LOAD_BIT)
8181              ? _("destination register same as write-back base")
8182              : _("source register same as write-back base"));
8183 }
8184
8185 /* inst.operands[i] was set up by parse_address.  Encode it into an
8186    ARM-format mode 2 load or store instruction.  If is_t is true,
8187    reject forms that cannot be used with a T instruction (i.e. not
8188    post-indexed).  */
8189 static void
8190 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
8191 {
8192   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
8193
8194   encode_arm_addr_mode_common (i, is_t);
8195
8196   if (inst.operands[i].immisreg)
8197     {
8198       constraint ((inst.operands[i].imm == REG_PC
8199                    || (is_pc && inst.operands[i].writeback)),
8200                   BAD_PC_ADDRESSING);
8201       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
8202       inst.instruction |= inst.operands[i].imm;
8203       if (!inst.operands[i].negative)
8204         inst.instruction |= INDEX_UP;
8205       if (inst.operands[i].shifted)
8206         {
8207           if (inst.operands[i].shift_kind == SHIFT_RRX)
8208             inst.instruction |= SHIFT_ROR << 5;
8209           else
8210             {
8211               inst.instruction |= inst.operands[i].shift_kind << 5;
8212               inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
8213             }
8214         }
8215     }
8216   else /* immediate offset in inst.relocs[0] */
8217     {
8218       if (is_pc && !inst.relocs[0].pc_rel)
8219         {
8220           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
8221
8222           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
8223              cannot use PC in addressing.
8224              PC cannot be used in writeback addressing, either.  */
8225           constraint ((is_t || inst.operands[i].writeback),
8226                       BAD_PC_ADDRESSING);
8227
8228           /* Use of PC in str is deprecated for ARMv7.  */
8229           if (warn_on_deprecated
8230               && !is_load
8231               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
8232             as_tsktsk (_("use of PC in this instruction is deprecated"));
8233         }
8234
8235       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8236         {
8237           /* Prefer + for zero encoded value.  */
8238           if (!inst.operands[i].negative)
8239             inst.instruction |= INDEX_UP;
8240           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM;
8241         }
8242     }
8243 }
8244
8245 /* inst.operands[i] was set up by parse_address.  Encode it into an
8246    ARM-format mode 3 load or store instruction.  Reject forms that
8247    cannot be used with such instructions.  If is_t is true, reject
8248    forms that cannot be used with a T instruction (i.e. not
8249    post-indexed).  */
8250 static void
8251 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
8252 {
8253   if (inst.operands[i].immisreg && inst.operands[i].shifted)
8254     {
8255       inst.error = _("instruction does not accept scaled register index");
8256       return;
8257     }
8258
8259   encode_arm_addr_mode_common (i, is_t);
8260
8261   if (inst.operands[i].immisreg)
8262     {
8263       constraint ((inst.operands[i].imm == REG_PC
8264                    || (is_t && inst.operands[i].reg == REG_PC)),
8265                   BAD_PC_ADDRESSING);
8266       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
8267                   BAD_PC_WRITEBACK);
8268       inst.instruction |= inst.operands[i].imm;
8269       if (!inst.operands[i].negative)
8270         inst.instruction |= INDEX_UP;
8271     }
8272   else /* immediate offset in inst.relocs[0] */
8273     {
8274       constraint ((inst.operands[i].reg == REG_PC && !inst.relocs[0].pc_rel
8275                    && inst.operands[i].writeback),
8276                   BAD_PC_WRITEBACK);
8277       inst.instruction |= HWOFFSET_IMM;
8278       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
8279         {
8280           /* Prefer + for zero encoded value.  */
8281           if (!inst.operands[i].negative)
8282             inst.instruction |= INDEX_UP;
8283
8284           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM8;
8285         }
8286     }
8287 }
8288
8289 /* Write immediate bits [7:0] to the following locations:
8290
8291   |28/24|23     19|18 16|15                    4|3     0|
8292   |  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|
8293
8294   This function is used by VMOV/VMVN/VORR/VBIC.  */
8295
8296 static void
8297 neon_write_immbits (unsigned immbits)
8298 {
8299   inst.instruction |= immbits & 0xf;
8300   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
8301   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
8302 }
8303
8304 /* Invert low-order SIZE bits of XHI:XLO.  */
8305
8306 static void
8307 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
8308 {
8309   unsigned immlo = xlo ? *xlo : 0;
8310   unsigned immhi = xhi ? *xhi : 0;
8311
8312   switch (size)
8313     {
8314     case 8:
8315       immlo = (~immlo) & 0xff;
8316       break;
8317
8318     case 16:
8319       immlo = (~immlo) & 0xffff;
8320       break;
8321
8322     case 64:
8323       immhi = (~immhi) & 0xffffffff;
8324       /* fall through.  */
8325
8326     case 32:
8327       immlo = (~immlo) & 0xffffffff;
8328       break;
8329
8330     default:
8331       abort ();
8332     }
8333
8334   if (xlo)
8335     *xlo = immlo;
8336
8337   if (xhi)
8338     *xhi = immhi;
8339 }
8340
8341 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
8342    A, B, C, D.  */
8343
8344 static int
8345 neon_bits_same_in_bytes (unsigned imm)
8346 {
8347   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
8348          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
8349          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
8350          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
8351 }
8352
8353 /* For immediate of above form, return 0bABCD.  */
8354
8355 static unsigned
8356 neon_squash_bits (unsigned imm)
8357 {
8358   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
8359          | ((imm & 0x01000000) >> 21);
8360 }
8361
8362 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
8363
8364 static unsigned
8365 neon_qfloat_bits (unsigned imm)
8366 {
8367   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
8368 }
8369
8370 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
8371    the instruction. *OP is passed as the initial value of the op field, and
8372    may be set to a different value depending on the constant (i.e.
8373    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
8374    MVN).  If the immediate looks like a repeated pattern then also
8375    try smaller element sizes.  */
8376
8377 static int
8378 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
8379                          unsigned *immbits, int *op, int size,
8380                          enum neon_el_type type)
8381 {
8382   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
8383      float.  */
8384   if (type == NT_float && !float_p)
8385     return FAIL;
8386
8387   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
8388     {
8389       if (size != 32 || *op == 1)
8390         return FAIL;
8391       *immbits = neon_qfloat_bits (immlo);
8392       return 0xf;
8393     }
8394
8395   if (size == 64)
8396     {
8397       if (neon_bits_same_in_bytes (immhi)
8398           && neon_bits_same_in_bytes (immlo))
8399         {
8400           if (*op == 1)
8401             return FAIL;
8402           *immbits = (neon_squash_bits (immhi) << 4)
8403                      | neon_squash_bits (immlo);
8404           *op = 1;
8405           return 0xe;
8406         }
8407
8408       if (immhi != immlo)
8409         return FAIL;
8410     }
8411
8412   if (size >= 32)
8413     {
8414       if (immlo == (immlo & 0x000000ff))
8415         {
8416           *immbits = immlo;
8417           return 0x0;
8418         }
8419       else if (immlo == (immlo & 0x0000ff00))
8420         {
8421           *immbits = immlo >> 8;
8422           return 0x2;
8423         }
8424       else if (immlo == (immlo & 0x00ff0000))
8425         {
8426           *immbits = immlo >> 16;
8427           return 0x4;
8428         }
8429       else if (immlo == (immlo & 0xff000000))
8430         {
8431           *immbits = immlo >> 24;
8432           return 0x6;
8433         }
8434       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
8435         {
8436           *immbits = (immlo >> 8) & 0xff;
8437           return 0xc;
8438         }
8439       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
8440         {
8441           *immbits = (immlo >> 16) & 0xff;
8442           return 0xd;
8443         }
8444
8445       if ((immlo & 0xffff) != (immlo >> 16))
8446         return FAIL;
8447       immlo &= 0xffff;
8448     }
8449
8450   if (size >= 16)
8451     {
8452       if (immlo == (immlo & 0x000000ff))
8453         {
8454           *immbits = immlo;
8455           return 0x8;
8456         }
8457       else if (immlo == (immlo & 0x0000ff00))
8458         {
8459           *immbits = immlo >> 8;
8460           return 0xa;
8461         }
8462
8463       if ((immlo & 0xff) != (immlo >> 8))
8464         return FAIL;
8465       immlo &= 0xff;
8466     }
8467
8468   if (immlo == (immlo & 0x000000ff))
8469     {
8470       /* Don't allow MVN with 8-bit immediate.  */
8471       if (*op == 1)
8472         return FAIL;
8473       *immbits = immlo;
8474       return 0xe;
8475     }
8476
8477   return FAIL;
8478 }
8479
8480 #if defined BFD_HOST_64_BIT
8481 /* Returns TRUE if double precision value V may be cast
8482    to single precision without loss of accuracy.  */
8483
8484 static bfd_boolean
8485 is_double_a_single (bfd_int64_t v)
8486 {
8487   int exp = (int)((v >> 52) & 0x7FF);
8488   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8489
8490   return (exp == 0 || exp == 0x7FF
8491           || (exp >= 1023 - 126 && exp <= 1023 + 127))
8492     && (mantissa & 0x1FFFFFFFl) == 0;
8493 }
8494
8495 /* Returns a double precision value casted to single precision
8496    (ignoring the least significant bits in exponent and mantissa).  */
8497
8498 static int
8499 double_to_single (bfd_int64_t v)
8500 {
8501   int sign = (int) ((v >> 63) & 1l);
8502   int exp = (int) ((v >> 52) & 0x7FF);
8503   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8504
8505   if (exp == 0x7FF)
8506     exp = 0xFF;
8507   else
8508     {
8509       exp = exp - 1023 + 127;
8510       if (exp >= 0xFF)
8511         {
8512           /* Infinity.  */
8513           exp = 0x7F;
8514           mantissa = 0;
8515         }
8516       else if (exp < 0)
8517         {
8518           /* No denormalized numbers.  */
8519           exp = 0;
8520           mantissa = 0;
8521         }
8522     }
8523   mantissa >>= 29;
8524   return (sign << 31) | (exp << 23) | mantissa;
8525 }
8526 #endif /* BFD_HOST_64_BIT */
8527
8528 enum lit_type
8529 {
8530   CONST_THUMB,
8531   CONST_ARM,
8532   CONST_VEC
8533 };
8534
8535 static void do_vfp_nsyn_opcode (const char *);
8536
8537 /* inst.relocs[0].exp describes an "=expr" load pseudo-operation.
8538    Determine whether it can be performed with a move instruction; if
8539    it can, convert inst.instruction to that move instruction and
8540    return TRUE; if it can't, convert inst.instruction to a literal-pool
8541    load and return FALSE.  If this is not a valid thing to do in the
8542    current context, set inst.error and return TRUE.
8543
8544    inst.operands[i] describes the destination register.  */
8545
8546 static bfd_boolean
8547 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
8548 {
8549   unsigned long tbit;
8550   bfd_boolean thumb_p = (t == CONST_THUMB);
8551   bfd_boolean arm_p   = (t == CONST_ARM);
8552
8553   if (thumb_p)
8554     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
8555   else
8556     tbit = LOAD_BIT;
8557
8558   if ((inst.instruction & tbit) == 0)
8559     {
8560       inst.error = _("invalid pseudo operation");
8561       return TRUE;
8562     }
8563
8564   if (inst.relocs[0].exp.X_op != O_constant
8565       && inst.relocs[0].exp.X_op != O_symbol
8566       && inst.relocs[0].exp.X_op != O_big)
8567     {
8568       inst.error = _("constant expression expected");
8569       return TRUE;
8570     }
8571
8572   if (inst.relocs[0].exp.X_op == O_constant
8573       || inst.relocs[0].exp.X_op == O_big)
8574     {
8575 #if defined BFD_HOST_64_BIT
8576       bfd_int64_t v;
8577 #else
8578       offsetT v;
8579 #endif
8580       if (inst.relocs[0].exp.X_op == O_big)
8581         {
8582           LITTLENUM_TYPE w[X_PRECISION];
8583           LITTLENUM_TYPE * l;
8584
8585           if (inst.relocs[0].exp.X_add_number == -1)
8586             {
8587               gen_to_words (w, X_PRECISION, E_PRECISION);
8588               l = w;
8589               /* FIXME: Should we check words w[2..5] ?  */
8590             }
8591           else
8592             l = generic_bignum;
8593
8594 #if defined BFD_HOST_64_BIT
8595           v =
8596             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8597                   << LITTLENUM_NUMBER_OF_BITS)
8598                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8599                 << LITTLENUM_NUMBER_OF_BITS)
8600                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8601               << LITTLENUM_NUMBER_OF_BITS)
8602              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8603 #else
8604           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8605             |  (l[0] & LITTLENUM_MASK);
8606 #endif
8607         }
8608       else
8609         v = inst.relocs[0].exp.X_add_number;
8610
8611       if (!inst.operands[i].issingle)
8612         {
8613           if (thumb_p)
8614             {
8615               /* LDR should not use lead in a flag-setting instruction being
8616                  chosen so we do not check whether movs can be used.  */
8617
8618               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8619                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8620                   && inst.operands[i].reg != 13
8621                   && inst.operands[i].reg != 15)
8622                 {
8623                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8624                      movw instruction.  */
8625                   unsigned int newimm;
8626                   bfd_boolean isNegated;
8627
8628                   newimm = encode_thumb32_immediate (v);
8629                   if (newimm != (unsigned int) FAIL)
8630                     isNegated = FALSE;
8631                   else
8632                     {
8633                       newimm = encode_thumb32_immediate (~v);
8634                       if (newimm != (unsigned int) FAIL)
8635                         isNegated = TRUE;
8636                     }
8637
8638                   /* The number can be loaded with a mov.w or mvn
8639                      instruction.  */
8640                   if (newimm != (unsigned int) FAIL
8641                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8642                     {
8643                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8644                                           | (inst.operands[i].reg << 8));
8645                       /* Change to MOVN.  */
8646                       inst.instruction |= (isNegated ? 0x200000 : 0);
8647                       inst.instruction |= (newimm & 0x800) << 15;
8648                       inst.instruction |= (newimm & 0x700) << 4;
8649                       inst.instruction |= (newimm & 0x0ff);
8650                       return TRUE;
8651                     }
8652                   /* The number can be loaded with a movw instruction.  */
8653                   else if ((v & ~0xFFFF) == 0
8654                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8655                     {
8656                       int imm = v & 0xFFFF;
8657
8658                       inst.instruction = 0xf2400000;  /* MOVW.  */
8659                       inst.instruction |= (inst.operands[i].reg << 8);
8660                       inst.instruction |= (imm & 0xf000) << 4;
8661                       inst.instruction |= (imm & 0x0800) << 15;
8662                       inst.instruction |= (imm & 0x0700) << 4;
8663                       inst.instruction |= (imm & 0x00ff);
8664                       return TRUE;
8665                     }
8666                 }
8667             }
8668           else if (arm_p)
8669             {
8670               int value = encode_arm_immediate (v);
8671
8672               if (value != FAIL)
8673                 {
8674                   /* This can be done with a mov instruction.  */
8675                   inst.instruction &= LITERAL_MASK;
8676                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8677                   inst.instruction |= value & 0xfff;
8678                   return TRUE;
8679                 }
8680
8681               value = encode_arm_immediate (~ v);
8682               if (value != FAIL)
8683                 {
8684                   /* This can be done with a mvn instruction.  */
8685                   inst.instruction &= LITERAL_MASK;
8686                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8687                   inst.instruction |= value & 0xfff;
8688                   return TRUE;
8689                 }
8690             }
8691           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8692             {
8693               int op = 0;
8694               unsigned immbits = 0;
8695               unsigned immlo = inst.operands[1].imm;
8696               unsigned immhi = inst.operands[1].regisimm
8697                 ? inst.operands[1].reg
8698                 : inst.relocs[0].exp.X_unsigned
8699                 ? 0
8700                 : ((bfd_int64_t)((int) immlo)) >> 32;
8701               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8702                                                    &op, 64, NT_invtype);
8703
8704               if (cmode == FAIL)
8705                 {
8706                   neon_invert_size (&immlo, &immhi, 64);
8707                   op = !op;
8708                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8709                                                    &op, 64, NT_invtype);
8710                 }
8711
8712               if (cmode != FAIL)
8713                 {
8714                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8715                     | (1 << 23)
8716                     | (cmode << 8)
8717                     | (op << 5)
8718                     | (1 << 4);
8719
8720                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8721                   if (thumb_mode)
8722                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8723                   else
8724                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8725                   neon_write_immbits (immbits);
8726                   return TRUE;
8727                 }
8728             }
8729         }
8730
8731       if (t == CONST_VEC)
8732         {
8733           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8734           if (inst.operands[i].issingle
8735               && is_quarter_float (inst.operands[1].imm)
8736               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8737             {
8738               inst.operands[1].imm =
8739                 neon_qfloat_bits (v);
8740               do_vfp_nsyn_opcode ("fconsts");
8741               return TRUE;
8742             }
8743
8744           /* If our host does not support a 64-bit type then we cannot perform
8745              the following optimization.  This mean that there will be a
8746              discrepancy between the output produced by an assembler built for
8747              a 32-bit-only host and the output produced from a 64-bit host, but
8748              this cannot be helped.  */
8749 #if defined BFD_HOST_64_BIT
8750           else if (!inst.operands[1].issingle
8751                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8752             {
8753               if (is_double_a_single (v)
8754                   && is_quarter_float (double_to_single (v)))
8755                 {
8756                   inst.operands[1].imm =
8757                     neon_qfloat_bits (double_to_single (v));
8758                   do_vfp_nsyn_opcode ("fconstd");
8759                   return TRUE;
8760                 }
8761             }
8762 #endif
8763         }
8764     }
8765
8766   if (add_to_lit_pool ((!inst.operands[i].isvec
8767                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8768     return TRUE;
8769
8770   inst.operands[1].reg = REG_PC;
8771   inst.operands[1].isreg = 1;
8772   inst.operands[1].preind = 1;
8773   inst.relocs[0].pc_rel = 1;
8774   inst.relocs[0].type = (thumb_p
8775                      ? BFD_RELOC_ARM_THUMB_OFFSET
8776                      : (mode_3
8777                         ? BFD_RELOC_ARM_HWLITERAL
8778                         : BFD_RELOC_ARM_LITERAL));
8779   return FALSE;
8780 }
8781
8782 /* inst.operands[i] was set up by parse_address.  Encode it into an
8783    ARM-format instruction.  Reject all forms which cannot be encoded
8784    into a coprocessor load/store instruction.  If wb_ok is false,
8785    reject use of writeback; if unind_ok is false, reject use of
8786    unindexed addressing.  If reloc_override is not 0, use it instead
8787    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8788    (in which case it is preserved).  */
8789
8790 static int
8791 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8792 {
8793   if (!inst.operands[i].isreg)
8794     {
8795       /* PR 18256 */
8796       if (! inst.operands[0].isvec)
8797         {
8798           inst.error = _("invalid co-processor operand");
8799           return FAIL;
8800         }
8801       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8802         return SUCCESS;
8803     }
8804
8805   inst.instruction |= inst.operands[i].reg << 16;
8806
8807   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8808
8809   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8810     {
8811       gas_assert (!inst.operands[i].writeback);
8812       if (!unind_ok)
8813         {
8814           inst.error = _("instruction does not support unindexed addressing");
8815           return FAIL;
8816         }
8817       inst.instruction |= inst.operands[i].imm;
8818       inst.instruction |= INDEX_UP;
8819       return SUCCESS;
8820     }
8821
8822   if (inst.operands[i].preind)
8823     inst.instruction |= PRE_INDEX;
8824
8825   if (inst.operands[i].writeback)
8826     {
8827       if (inst.operands[i].reg == REG_PC)
8828         {
8829           inst.error = _("pc may not be used with write-back");
8830           return FAIL;
8831         }
8832       if (!wb_ok)
8833         {
8834           inst.error = _("instruction does not support writeback");
8835           return FAIL;
8836         }
8837       inst.instruction |= WRITE_BACK;
8838     }
8839
8840   if (reloc_override)
8841     inst.relocs[0].type = (bfd_reloc_code_real_type) reloc_override;
8842   else if ((inst.relocs[0].type < BFD_RELOC_ARM_ALU_PC_G0_NC
8843             || inst.relocs[0].type > BFD_RELOC_ARM_LDC_SB_G2)
8844            && inst.relocs[0].type != BFD_RELOC_ARM_LDR_PC_G0)
8845     {
8846       if (thumb_mode)
8847         inst.relocs[0].type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8848       else
8849         inst.relocs[0].type = BFD_RELOC_ARM_CP_OFF_IMM;
8850     }
8851
8852   /* Prefer + for zero encoded value.  */
8853   if (!inst.operands[i].negative)
8854     inst.instruction |= INDEX_UP;
8855
8856   return SUCCESS;
8857 }
8858
8859 /* Functions for instruction encoding, sorted by sub-architecture.
8860    First some generics; their names are taken from the conventional
8861    bit positions for register arguments in ARM format instructions.  */
8862
8863 static void
8864 do_noargs (void)
8865 {
8866 }
8867
8868 static void
8869 do_rd (void)
8870 {
8871   inst.instruction |= inst.operands[0].reg << 12;
8872 }
8873
8874 static void
8875 do_rn (void)
8876 {
8877   inst.instruction |= inst.operands[0].reg << 16;
8878 }
8879
8880 static void
8881 do_rd_rm (void)
8882 {
8883   inst.instruction |= inst.operands[0].reg << 12;
8884   inst.instruction |= inst.operands[1].reg;
8885 }
8886
8887 static void
8888 do_rm_rn (void)
8889 {
8890   inst.instruction |= inst.operands[0].reg;
8891   inst.instruction |= inst.operands[1].reg << 16;
8892 }
8893
8894 static void
8895 do_rd_rn (void)
8896 {
8897   inst.instruction |= inst.operands[0].reg << 12;
8898   inst.instruction |= inst.operands[1].reg << 16;
8899 }
8900
8901 static void
8902 do_rn_rd (void)
8903 {
8904   inst.instruction |= inst.operands[0].reg << 16;
8905   inst.instruction |= inst.operands[1].reg << 12;
8906 }
8907
8908 static void
8909 do_tt (void)
8910 {
8911   inst.instruction |= inst.operands[0].reg << 8;
8912   inst.instruction |= inst.operands[1].reg << 16;
8913 }
8914
8915 static bfd_boolean
8916 check_obsolete (const arm_feature_set *feature, const char *msg)
8917 {
8918   if (ARM_CPU_IS_ANY (cpu_variant))
8919     {
8920       as_tsktsk ("%s", msg);
8921       return TRUE;
8922     }
8923   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8924     {
8925       as_bad ("%s", msg);
8926       return TRUE;
8927     }
8928
8929   return FALSE;
8930 }
8931
8932 static void
8933 do_rd_rm_rn (void)
8934 {
8935   unsigned Rn = inst.operands[2].reg;
8936   /* Enforce restrictions on SWP instruction.  */
8937   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8938     {
8939       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8940                   _("Rn must not overlap other operands"));
8941
8942       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8943        */
8944       if (!check_obsolete (&arm_ext_v8,
8945                            _("swp{b} use is obsoleted for ARMv8 and later"))
8946           && warn_on_deprecated
8947           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8948         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8949     }
8950
8951   inst.instruction |= inst.operands[0].reg << 12;
8952   inst.instruction |= inst.operands[1].reg;
8953   inst.instruction |= Rn << 16;
8954 }
8955
8956 static void
8957 do_rd_rn_rm (void)
8958 {
8959   inst.instruction |= inst.operands[0].reg << 12;
8960   inst.instruction |= inst.operands[1].reg << 16;
8961   inst.instruction |= inst.operands[2].reg;
8962 }
8963
8964 static void
8965 do_rm_rd_rn (void)
8966 {
8967   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8968   constraint (((inst.relocs[0].exp.X_op != O_constant
8969                 && inst.relocs[0].exp.X_op != O_illegal)
8970                || inst.relocs[0].exp.X_add_number != 0),
8971               BAD_ADDR_MODE);
8972   inst.instruction |= inst.operands[0].reg;
8973   inst.instruction |= inst.operands[1].reg << 12;
8974   inst.instruction |= inst.operands[2].reg << 16;
8975 }
8976
8977 static void
8978 do_imm0 (void)
8979 {
8980   inst.instruction |= inst.operands[0].imm;
8981 }
8982
8983 static void
8984 do_rd_cpaddr (void)
8985 {
8986   inst.instruction |= inst.operands[0].reg << 12;
8987   encode_arm_cp_address (1, TRUE, TRUE, 0);
8988 }
8989
8990 /* ARM instructions, in alphabetical order by function name (except
8991    that wrapper functions appear immediately after the function they
8992    wrap).  */
8993
8994 /* This is a pseudo-op of the form "adr rd, label" to be converted
8995    into a relative address of the form "add rd, pc, #label-.-8".  */
8996
8997 static void
8998 do_adr (void)
8999 {
9000   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9001
9002   /* Frag hacking will turn this into a sub instruction if the offset turns
9003      out to be negative.  */
9004   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
9005   inst.relocs[0].pc_rel = 1;
9006   inst.relocs[0].exp.X_add_number -= 8;
9007
9008   if (support_interwork
9009       && inst.relocs[0].exp.X_op == O_symbol
9010       && inst.relocs[0].exp.X_add_symbol != NULL
9011       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9012       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9013     inst.relocs[0].exp.X_add_number |= 1;
9014 }
9015
9016 /* This is a pseudo-op of the form "adrl rd, label" to be converted
9017    into a relative address of the form:
9018    add rd, pc, #low(label-.-8)"
9019    add rd, rd, #high(label-.-8)"  */
9020
9021 static void
9022 do_adrl (void)
9023 {
9024   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
9025
9026   /* Frag hacking will turn this into a sub instruction if the offset turns
9027      out to be negative.  */
9028   inst.relocs[0].type          = BFD_RELOC_ARM_ADRL_IMMEDIATE;
9029   inst.relocs[0].pc_rel        = 1;
9030   inst.size                    = INSN_SIZE * 2;
9031   inst.relocs[0].exp.X_add_number -= 8;
9032
9033   if (support_interwork
9034       && inst.relocs[0].exp.X_op == O_symbol
9035       && inst.relocs[0].exp.X_add_symbol != NULL
9036       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
9037       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
9038     inst.relocs[0].exp.X_add_number |= 1;
9039 }
9040
9041 static void
9042 do_arit (void)
9043 {
9044   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9045               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9046               THUMB1_RELOC_ONLY);
9047   if (!inst.operands[1].present)
9048     inst.operands[1].reg = inst.operands[0].reg;
9049   inst.instruction |= inst.operands[0].reg << 12;
9050   inst.instruction |= inst.operands[1].reg << 16;
9051   encode_arm_shifter_operand (2);
9052 }
9053
9054 static void
9055 do_barrier (void)
9056 {
9057   if (inst.operands[0].present)
9058     inst.instruction |= inst.operands[0].imm;
9059   else
9060     inst.instruction |= 0xf;
9061 }
9062
9063 static void
9064 do_bfc (void)
9065 {
9066   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
9067   constraint (msb > 32, _("bit-field extends past end of register"));
9068   /* The instruction encoding stores the LSB and MSB,
9069      not the LSB and width.  */
9070   inst.instruction |= inst.operands[0].reg << 12;
9071   inst.instruction |= inst.operands[1].imm << 7;
9072   inst.instruction |= (msb - 1) << 16;
9073 }
9074
9075 static void
9076 do_bfi (void)
9077 {
9078   unsigned int msb;
9079
9080   /* #0 in second position is alternative syntax for bfc, which is
9081      the same instruction but with REG_PC in the Rm field.  */
9082   if (!inst.operands[1].isreg)
9083     inst.operands[1].reg = REG_PC;
9084
9085   msb = inst.operands[2].imm + inst.operands[3].imm;
9086   constraint (msb > 32, _("bit-field extends past end of register"));
9087   /* The instruction encoding stores the LSB and MSB,
9088      not the LSB and width.  */
9089   inst.instruction |= inst.operands[0].reg << 12;
9090   inst.instruction |= inst.operands[1].reg;
9091   inst.instruction |= inst.operands[2].imm << 7;
9092   inst.instruction |= (msb - 1) << 16;
9093 }
9094
9095 static void
9096 do_bfx (void)
9097 {
9098   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
9099               _("bit-field extends past end of register"));
9100   inst.instruction |= inst.operands[0].reg << 12;
9101   inst.instruction |= inst.operands[1].reg;
9102   inst.instruction |= inst.operands[2].imm << 7;
9103   inst.instruction |= (inst.operands[3].imm - 1) << 16;
9104 }
9105
9106 /* ARM V5 breakpoint instruction (argument parse)
9107      BKPT <16 bit unsigned immediate>
9108      Instruction is not conditional.
9109         The bit pattern given in insns[] has the COND_ALWAYS condition,
9110         and it is an error if the caller tried to override that.  */
9111
9112 static void
9113 do_bkpt (void)
9114 {
9115   /* Top 12 of 16 bits to bits 19:8.  */
9116   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
9117
9118   /* Bottom 4 of 16 bits to bits 3:0.  */
9119   inst.instruction |= inst.operands[0].imm & 0xf;
9120 }
9121
9122 static void
9123 encode_branch (int default_reloc)
9124 {
9125   if (inst.operands[0].hasreloc)
9126     {
9127       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
9128                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
9129                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
9130       inst.relocs[0].type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
9131         ? BFD_RELOC_ARM_PLT32
9132         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
9133     }
9134   else
9135     inst.relocs[0].type = (bfd_reloc_code_real_type) default_reloc;
9136   inst.relocs[0].pc_rel = 1;
9137 }
9138
9139 static void
9140 do_branch (void)
9141 {
9142 #ifdef OBJ_ELF
9143   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9144     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9145   else
9146 #endif
9147     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9148 }
9149
9150 static void
9151 do_bl (void)
9152 {
9153 #ifdef OBJ_ELF
9154   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
9155     {
9156       if (inst.cond == COND_ALWAYS)
9157         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
9158       else
9159         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
9160     }
9161   else
9162 #endif
9163     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
9164 }
9165
9166 /* ARM V5 branch-link-exchange instruction (argument parse)
9167      BLX <target_addr>          ie BLX(1)
9168      BLX{<condition>} <Rm>      ie BLX(2)
9169    Unfortunately, there are two different opcodes for this mnemonic.
9170    So, the insns[].value is not used, and the code here zaps values
9171         into inst.instruction.
9172    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
9173
9174 static void
9175 do_blx (void)
9176 {
9177   if (inst.operands[0].isreg)
9178     {
9179       /* Arg is a register; the opcode provided by insns[] is correct.
9180          It is not illegal to do "blx pc", just useless.  */
9181       if (inst.operands[0].reg == REG_PC)
9182         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
9183
9184       inst.instruction |= inst.operands[0].reg;
9185     }
9186   else
9187     {
9188       /* Arg is an address; this instruction cannot be executed
9189          conditionally, and the opcode must be adjusted.
9190          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
9191          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
9192       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9193       inst.instruction = 0xfa000000;
9194       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
9195     }
9196 }
9197
9198 static void
9199 do_bx (void)
9200 {
9201   bfd_boolean want_reloc;
9202
9203   if (inst.operands[0].reg == REG_PC)
9204     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
9205
9206   inst.instruction |= inst.operands[0].reg;
9207   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
9208      it is for ARMv4t or earlier.  */
9209   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
9210   if (!ARM_FEATURE_ZERO (selected_object_arch)
9211       && !ARM_CPU_HAS_FEATURE (selected_object_arch, arm_ext_v5))
9212       want_reloc = TRUE;
9213
9214 #ifdef OBJ_ELF
9215   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
9216 #endif
9217     want_reloc = FALSE;
9218
9219   if (want_reloc)
9220     inst.relocs[0].type = BFD_RELOC_ARM_V4BX;
9221 }
9222
9223
9224 /* ARM v5TEJ.  Jump to Jazelle code.  */
9225
9226 static void
9227 do_bxj (void)
9228 {
9229   if (inst.operands[0].reg == REG_PC)
9230     as_tsktsk (_("use of r15 in bxj is not really useful"));
9231
9232   inst.instruction |= inst.operands[0].reg;
9233 }
9234
9235 /* Co-processor data operation:
9236       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
9237       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
9238 static void
9239 do_cdp (void)
9240 {
9241   inst.instruction |= inst.operands[0].reg << 8;
9242   inst.instruction |= inst.operands[1].imm << 20;
9243   inst.instruction |= inst.operands[2].reg << 12;
9244   inst.instruction |= inst.operands[3].reg << 16;
9245   inst.instruction |= inst.operands[4].reg;
9246   inst.instruction |= inst.operands[5].imm << 5;
9247 }
9248
9249 static void
9250 do_cmp (void)
9251 {
9252   inst.instruction |= inst.operands[0].reg << 16;
9253   encode_arm_shifter_operand (1);
9254 }
9255
9256 /* Transfer between coprocessor and ARM registers.
9257    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
9258    MRC2
9259    MCR{cond}
9260    MCR2
9261
9262    No special properties.  */
9263
9264 struct deprecated_coproc_regs_s
9265 {
9266   unsigned cp;
9267   int opc1;
9268   unsigned crn;
9269   unsigned crm;
9270   int opc2;
9271   arm_feature_set deprecated;
9272   arm_feature_set obsoleted;
9273   const char *dep_msg;
9274   const char *obs_msg;
9275 };
9276
9277 #define DEPR_ACCESS_V8 \
9278   N_("This coprocessor register access is deprecated in ARMv8")
9279
9280 /* Table of all deprecated coprocessor registers.  */
9281 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
9282 {
9283     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
9284      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9285      DEPR_ACCESS_V8, NULL},
9286     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
9287      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9288      DEPR_ACCESS_V8, NULL},
9289     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
9290      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9291      DEPR_ACCESS_V8, NULL},
9292     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
9293      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9294      DEPR_ACCESS_V8, NULL},
9295     {14, 6, 0,  0, 0,                                   /* TEECR.  */
9296      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
9297      DEPR_ACCESS_V8, NULL},
9298 };
9299
9300 #undef DEPR_ACCESS_V8
9301
9302 static const size_t deprecated_coproc_reg_count =
9303   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
9304
9305 static void
9306 do_co_reg (void)
9307 {
9308   unsigned Rd;
9309   size_t i;
9310
9311   Rd = inst.operands[2].reg;
9312   if (thumb_mode)
9313     {
9314       if (inst.instruction == 0xee000010
9315           || inst.instruction == 0xfe000010)
9316         /* MCR, MCR2  */
9317         reject_bad_reg (Rd);
9318       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9319         /* MRC, MRC2  */
9320         constraint (Rd == REG_SP, BAD_SP);
9321     }
9322   else
9323     {
9324       /* MCR */
9325       if (inst.instruction == 0xe000010)
9326         constraint (Rd == REG_PC, BAD_PC);
9327     }
9328
9329     for (i = 0; i < deprecated_coproc_reg_count; ++i)
9330       {
9331         const struct deprecated_coproc_regs_s *r =
9332           deprecated_coproc_regs + i;
9333
9334         if (inst.operands[0].reg == r->cp
9335             && inst.operands[1].imm == r->opc1
9336             && inst.operands[3].reg == r->crn
9337             && inst.operands[4].reg == r->crm
9338             && inst.operands[5].imm == r->opc2)
9339           {
9340             if (! ARM_CPU_IS_ANY (cpu_variant)
9341                 && warn_on_deprecated
9342                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
9343               as_tsktsk ("%s", r->dep_msg);
9344           }
9345       }
9346
9347   inst.instruction |= inst.operands[0].reg << 8;
9348   inst.instruction |= inst.operands[1].imm << 21;
9349   inst.instruction |= Rd << 12;
9350   inst.instruction |= inst.operands[3].reg << 16;
9351   inst.instruction |= inst.operands[4].reg;
9352   inst.instruction |= inst.operands[5].imm << 5;
9353 }
9354
9355 /* Transfer between coprocessor register and pair of ARM registers.
9356    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
9357    MCRR2
9358    MRRC{cond}
9359    MRRC2
9360
9361    Two XScale instructions are special cases of these:
9362
9363      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
9364      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
9365
9366    Result unpredictable if Rd or Rn is R15.  */
9367
9368 static void
9369 do_co_reg2c (void)
9370 {
9371   unsigned Rd, Rn;
9372
9373   Rd = inst.operands[2].reg;
9374   Rn = inst.operands[3].reg;
9375
9376   if (thumb_mode)
9377     {
9378       reject_bad_reg (Rd);
9379       reject_bad_reg (Rn);
9380     }
9381   else
9382     {
9383       constraint (Rd == REG_PC, BAD_PC);
9384       constraint (Rn == REG_PC, BAD_PC);
9385     }
9386
9387   /* Only check the MRRC{2} variants.  */
9388   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
9389     {
9390        /* If Rd == Rn, error that the operation is
9391           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
9392        constraint (Rd == Rn, BAD_OVERLAP);
9393     }
9394
9395   inst.instruction |= inst.operands[0].reg << 8;
9396   inst.instruction |= inst.operands[1].imm << 4;
9397   inst.instruction |= Rd << 12;
9398   inst.instruction |= Rn << 16;
9399   inst.instruction |= inst.operands[4].reg;
9400 }
9401
9402 static void
9403 do_cpsi (void)
9404 {
9405   inst.instruction |= inst.operands[0].imm << 6;
9406   if (inst.operands[1].present)
9407     {
9408       inst.instruction |= CPSI_MMOD;
9409       inst.instruction |= inst.operands[1].imm;
9410     }
9411 }
9412
9413 static void
9414 do_dbg (void)
9415 {
9416   inst.instruction |= inst.operands[0].imm;
9417 }
9418
9419 static void
9420 do_div (void)
9421 {
9422   unsigned Rd, Rn, Rm;
9423
9424   Rd = inst.operands[0].reg;
9425   Rn = (inst.operands[1].present
9426         ? inst.operands[1].reg : Rd);
9427   Rm = inst.operands[2].reg;
9428
9429   constraint ((Rd == REG_PC), BAD_PC);
9430   constraint ((Rn == REG_PC), BAD_PC);
9431   constraint ((Rm == REG_PC), BAD_PC);
9432
9433   inst.instruction |= Rd << 16;
9434   inst.instruction |= Rn << 0;
9435   inst.instruction |= Rm << 8;
9436 }
9437
9438 static void
9439 do_it (void)
9440 {
9441   /* There is no IT instruction in ARM mode.  We
9442      process it to do the validation as if in
9443      thumb mode, just in case the code gets
9444      assembled for thumb using the unified syntax.  */
9445
9446   inst.size = 0;
9447   if (unified_syntax)
9448     {
9449       set_pred_insn_type (IT_INSN);
9450       now_pred.mask = (inst.instruction & 0xf) | 0x10;
9451       now_pred.cc = inst.operands[0].imm;
9452     }
9453 }
9454
9455 /* If there is only one register in the register list,
9456    then return its register number.  Otherwise return -1.  */
9457 static int
9458 only_one_reg_in_list (int range)
9459 {
9460   int i = ffs (range) - 1;
9461   return (i > 15 || range != (1 << i)) ? -1 : i;
9462 }
9463
9464 static void
9465 encode_ldmstm(int from_push_pop_mnem)
9466 {
9467   int base_reg = inst.operands[0].reg;
9468   int range = inst.operands[1].imm;
9469   int one_reg;
9470
9471   inst.instruction |= base_reg << 16;
9472   inst.instruction |= range;
9473
9474   if (inst.operands[1].writeback)
9475     inst.instruction |= LDM_TYPE_2_OR_3;
9476
9477   if (inst.operands[0].writeback)
9478     {
9479       inst.instruction |= WRITE_BACK;
9480       /* Check for unpredictable uses of writeback.  */
9481       if (inst.instruction & LOAD_BIT)
9482         {
9483           /* Not allowed in LDM type 2.  */
9484           if ((inst.instruction & LDM_TYPE_2_OR_3)
9485               && ((range & (1 << REG_PC)) == 0))
9486             as_warn (_("writeback of base register is UNPREDICTABLE"));
9487           /* Only allowed if base reg not in list for other types.  */
9488           else if (range & (1 << base_reg))
9489             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
9490         }
9491       else /* STM.  */
9492         {
9493           /* Not allowed for type 2.  */
9494           if (inst.instruction & LDM_TYPE_2_OR_3)
9495             as_warn (_("writeback of base register is UNPREDICTABLE"));
9496           /* Only allowed if base reg not in list, or first in list.  */
9497           else if ((range & (1 << base_reg))
9498                    && (range & ((1 << base_reg) - 1)))
9499             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
9500         }
9501     }
9502
9503   /* If PUSH/POP has only one register, then use the A2 encoding.  */
9504   one_reg = only_one_reg_in_list (range);
9505   if (from_push_pop_mnem && one_reg >= 0)
9506     {
9507       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
9508
9509       if (is_push && one_reg == 13 /* SP */)
9510         /* PR 22483: The A2 encoding cannot be used when
9511            pushing the stack pointer as this is UNPREDICTABLE.  */
9512         return;
9513
9514       inst.instruction &= A_COND_MASK;
9515       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
9516       inst.instruction |= one_reg << 12;
9517     }
9518 }
9519
9520 static void
9521 do_ldmstm (void)
9522 {
9523   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
9524 }
9525
9526 /* ARMv5TE load-consecutive (argument parse)
9527    Mode is like LDRH.
9528
9529      LDRccD R, mode
9530      STRccD R, mode.  */
9531
9532 static void
9533 do_ldrd (void)
9534 {
9535   constraint (inst.operands[0].reg % 2 != 0,
9536               _("first transfer register must be even"));
9537   constraint (inst.operands[1].present
9538               && inst.operands[1].reg != inst.operands[0].reg + 1,
9539               _("can only transfer two consecutive registers"));
9540   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9541   constraint (!inst.operands[2].isreg, _("'[' expected"));
9542
9543   if (!inst.operands[1].present)
9544     inst.operands[1].reg = inst.operands[0].reg + 1;
9545
9546   /* encode_arm_addr_mode_3 will diagnose overlap between the base
9547      register and the first register written; we have to diagnose
9548      overlap between the base and the second register written here.  */
9549
9550   if (inst.operands[2].reg == inst.operands[1].reg
9551       && (inst.operands[2].writeback || inst.operands[2].postind))
9552     as_warn (_("base register written back, and overlaps "
9553                "second transfer register"));
9554
9555   if (!(inst.instruction & V4_STR_BIT))
9556     {
9557       /* For an index-register load, the index register must not overlap the
9558         destination (even if not write-back).  */
9559       if (inst.operands[2].immisreg
9560               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
9561               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
9562         as_warn (_("index register overlaps transfer register"));
9563     }
9564   inst.instruction |= inst.operands[0].reg << 12;
9565   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
9566 }
9567
9568 static void
9569 do_ldrex (void)
9570 {
9571   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
9572               || inst.operands[1].postind || inst.operands[1].writeback
9573               || inst.operands[1].immisreg || inst.operands[1].shifted
9574               || inst.operands[1].negative
9575               /* This can arise if the programmer has written
9576                    strex rN, rM, foo
9577                  or if they have mistakenly used a register name as the last
9578                  operand,  eg:
9579                    strex rN, rM, rX
9580                  It is very difficult to distinguish between these two cases
9581                  because "rX" might actually be a label. ie the register
9582                  name has been occluded by a symbol of the same name. So we
9583                  just generate a general 'bad addressing mode' type error
9584                  message and leave it up to the programmer to discover the
9585                  true cause and fix their mistake.  */
9586               || (inst.operands[1].reg == REG_PC),
9587               BAD_ADDR_MODE);
9588
9589   constraint (inst.relocs[0].exp.X_op != O_constant
9590               || inst.relocs[0].exp.X_add_number != 0,
9591               _("offset must be zero in ARM encoding"));
9592
9593   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
9594
9595   inst.instruction |= inst.operands[0].reg << 12;
9596   inst.instruction |= inst.operands[1].reg << 16;
9597   inst.relocs[0].type = BFD_RELOC_UNUSED;
9598 }
9599
9600 static void
9601 do_ldrexd (void)
9602 {
9603   constraint (inst.operands[0].reg % 2 != 0,
9604               _("even register required"));
9605   constraint (inst.operands[1].present
9606               && inst.operands[1].reg != inst.operands[0].reg + 1,
9607               _("can only load two consecutive registers"));
9608   /* If op 1 were present and equal to PC, this function wouldn't
9609      have been called in the first place.  */
9610   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9611
9612   inst.instruction |= inst.operands[0].reg << 12;
9613   inst.instruction |= inst.operands[2].reg << 16;
9614 }
9615
9616 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9617    which is not a multiple of four is UNPREDICTABLE.  */
9618 static void
9619 check_ldr_r15_aligned (void)
9620 {
9621   constraint (!(inst.operands[1].immisreg)
9622               && (inst.operands[0].reg == REG_PC
9623               && inst.operands[1].reg == REG_PC
9624               && (inst.relocs[0].exp.X_add_number & 0x3)),
9625               _("ldr to register 15 must be 4-byte aligned"));
9626 }
9627
9628 static void
9629 do_ldst (void)
9630 {
9631   inst.instruction |= inst.operands[0].reg << 12;
9632   if (!inst.operands[1].isreg)
9633     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9634       return;
9635   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9636   check_ldr_r15_aligned ();
9637 }
9638
9639 static void
9640 do_ldstt (void)
9641 {
9642   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9643      reject [Rn,...].  */
9644   if (inst.operands[1].preind)
9645     {
9646       constraint (inst.relocs[0].exp.X_op != O_constant
9647                   || inst.relocs[0].exp.X_add_number != 0,
9648                   _("this instruction requires a post-indexed address"));
9649
9650       inst.operands[1].preind = 0;
9651       inst.operands[1].postind = 1;
9652       inst.operands[1].writeback = 1;
9653     }
9654   inst.instruction |= inst.operands[0].reg << 12;
9655   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9656 }
9657
9658 /* Halfword and signed-byte load/store operations.  */
9659
9660 static void
9661 do_ldstv4 (void)
9662 {
9663   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9664   inst.instruction |= inst.operands[0].reg << 12;
9665   if (!inst.operands[1].isreg)
9666     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9667       return;
9668   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9669 }
9670
9671 static void
9672 do_ldsttv4 (void)
9673 {
9674   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9675      reject [Rn,...].  */
9676   if (inst.operands[1].preind)
9677     {
9678       constraint (inst.relocs[0].exp.X_op != O_constant
9679                   || inst.relocs[0].exp.X_add_number != 0,
9680                   _("this instruction requires a post-indexed address"));
9681
9682       inst.operands[1].preind = 0;
9683       inst.operands[1].postind = 1;
9684       inst.operands[1].writeback = 1;
9685     }
9686   inst.instruction |= inst.operands[0].reg << 12;
9687   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9688 }
9689
9690 /* Co-processor register load/store.
9691    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9692 static void
9693 do_lstc (void)
9694 {
9695   inst.instruction |= inst.operands[0].reg << 8;
9696   inst.instruction |= inst.operands[1].reg << 12;
9697   encode_arm_cp_address (2, TRUE, TRUE, 0);
9698 }
9699
9700 static void
9701 do_mlas (void)
9702 {
9703   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9704   if (inst.operands[0].reg == inst.operands[1].reg
9705       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9706       && !(inst.instruction & 0x00400000))
9707     as_tsktsk (_("Rd and Rm should be different in mla"));
9708
9709   inst.instruction |= inst.operands[0].reg << 16;
9710   inst.instruction |= inst.operands[1].reg;
9711   inst.instruction |= inst.operands[2].reg << 8;
9712   inst.instruction |= inst.operands[3].reg << 12;
9713 }
9714
9715 static void
9716 do_mov (void)
9717 {
9718   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9719               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9720               THUMB1_RELOC_ONLY);
9721   inst.instruction |= inst.operands[0].reg << 12;
9722   encode_arm_shifter_operand (1);
9723 }
9724
9725 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9726 static void
9727 do_mov16 (void)
9728 {
9729   bfd_vma imm;
9730   bfd_boolean top;
9731
9732   top = (inst.instruction & 0x00400000) != 0;
9733   constraint (top && inst.relocs[0].type == BFD_RELOC_ARM_MOVW,
9734               _(":lower16: not allowed in this instruction"));
9735   constraint (!top && inst.relocs[0].type == BFD_RELOC_ARM_MOVT,
9736               _(":upper16: not allowed in this instruction"));
9737   inst.instruction |= inst.operands[0].reg << 12;
9738   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
9739     {
9740       imm = inst.relocs[0].exp.X_add_number;
9741       /* The value is in two pieces: 0:11, 16:19.  */
9742       inst.instruction |= (imm & 0x00000fff);
9743       inst.instruction |= (imm & 0x0000f000) << 4;
9744     }
9745 }
9746
9747 static int
9748 do_vfp_nsyn_mrs (void)
9749 {
9750   if (inst.operands[0].isvec)
9751     {
9752       if (inst.operands[1].reg != 1)
9753         first_error (_("operand 1 must be FPSCR"));
9754       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9755       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9756       do_vfp_nsyn_opcode ("fmstat");
9757     }
9758   else if (inst.operands[1].isvec)
9759     do_vfp_nsyn_opcode ("fmrx");
9760   else
9761     return FAIL;
9762
9763   return SUCCESS;
9764 }
9765
9766 static int
9767 do_vfp_nsyn_msr (void)
9768 {
9769   if (inst.operands[0].isvec)
9770     do_vfp_nsyn_opcode ("fmxr");
9771   else
9772     return FAIL;
9773
9774   return SUCCESS;
9775 }
9776
9777 static void
9778 do_vmrs (void)
9779 {
9780   unsigned Rt = inst.operands[0].reg;
9781
9782   if (thumb_mode && Rt == REG_SP)
9783     {
9784       inst.error = BAD_SP;
9785       return;
9786     }
9787
9788   /* MVFR2 is only valid at ARMv8-A.  */
9789   if (inst.operands[1].reg == 5)
9790     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9791                 _(BAD_FPU));
9792
9793   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9794   if (!inst.operands[0].isvec && Rt == REG_PC)
9795     {
9796       inst.error = BAD_PC;
9797       return;
9798     }
9799
9800   /* If we get through parsing the register name, we just insert the number
9801      generated into the instruction without further validation.  */
9802   inst.instruction |= (inst.operands[1].reg << 16);
9803   inst.instruction |= (Rt << 12);
9804 }
9805
9806 static void
9807 do_vmsr (void)
9808 {
9809   unsigned Rt = inst.operands[1].reg;
9810
9811   if (thumb_mode)
9812     reject_bad_reg (Rt);
9813   else if (Rt == REG_PC)
9814     {
9815       inst.error = BAD_PC;
9816       return;
9817     }
9818
9819   /* MVFR2 is only valid for ARMv8-A.  */
9820   if (inst.operands[0].reg == 5)
9821     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9822                 _(BAD_FPU));
9823
9824   /* If we get through parsing the register name, we just insert the number
9825      generated into the instruction without further validation.  */
9826   inst.instruction |= (inst.operands[0].reg << 16);
9827   inst.instruction |= (Rt << 12);
9828 }
9829
9830 static void
9831 do_mrs (void)
9832 {
9833   unsigned br;
9834
9835   if (do_vfp_nsyn_mrs () == SUCCESS)
9836     return;
9837
9838   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9839   inst.instruction |= inst.operands[0].reg << 12;
9840
9841   if (inst.operands[1].isreg)
9842     {
9843       br = inst.operands[1].reg;
9844       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
9845         as_bad (_("bad register for mrs"));
9846     }
9847   else
9848     {
9849       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9850       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9851                   != (PSR_c|PSR_f),
9852                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9853       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9854     }
9855
9856   inst.instruction |= br;
9857 }
9858
9859 /* Two possible forms:
9860       "{C|S}PSR_<field>, Rm",
9861       "{C|S}PSR_f, #expression".  */
9862
9863 static void
9864 do_msr (void)
9865 {
9866   if (do_vfp_nsyn_msr () == SUCCESS)
9867     return;
9868
9869   inst.instruction |= inst.operands[0].imm;
9870   if (inst.operands[1].isreg)
9871     inst.instruction |= inst.operands[1].reg;
9872   else
9873     {
9874       inst.instruction |= INST_IMMEDIATE;
9875       inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
9876       inst.relocs[0].pc_rel = 0;
9877     }
9878 }
9879
9880 static void
9881 do_mul (void)
9882 {
9883   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9884
9885   if (!inst.operands[2].present)
9886     inst.operands[2].reg = inst.operands[0].reg;
9887   inst.instruction |= inst.operands[0].reg << 16;
9888   inst.instruction |= inst.operands[1].reg;
9889   inst.instruction |= inst.operands[2].reg << 8;
9890
9891   if (inst.operands[0].reg == inst.operands[1].reg
9892       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9893     as_tsktsk (_("Rd and Rm should be different in mul"));
9894 }
9895
9896 /* Long Multiply Parser
9897    UMULL RdLo, RdHi, Rm, Rs
9898    SMULL RdLo, RdHi, Rm, Rs
9899    UMLAL RdLo, RdHi, Rm, Rs
9900    SMLAL RdLo, RdHi, Rm, Rs.  */
9901
9902 static void
9903 do_mull (void)
9904 {
9905   inst.instruction |= inst.operands[0].reg << 12;
9906   inst.instruction |= inst.operands[1].reg << 16;
9907   inst.instruction |= inst.operands[2].reg;
9908   inst.instruction |= inst.operands[3].reg << 8;
9909
9910   /* rdhi and rdlo must be different.  */
9911   if (inst.operands[0].reg == inst.operands[1].reg)
9912     as_tsktsk (_("rdhi and rdlo must be different"));
9913
9914   /* rdhi, rdlo and rm must all be different before armv6.  */
9915   if ((inst.operands[0].reg == inst.operands[2].reg
9916       || inst.operands[1].reg == inst.operands[2].reg)
9917       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9918     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9919 }
9920
9921 static void
9922 do_nop (void)
9923 {
9924   if (inst.operands[0].present
9925       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9926     {
9927       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9928       inst.instruction &= 0xf0000000;
9929       inst.instruction |= 0x0320f000;
9930       if (inst.operands[0].present)
9931         inst.instruction |= inst.operands[0].imm;
9932     }
9933 }
9934
9935 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9936    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9937    Condition defaults to COND_ALWAYS.
9938    Error if Rd, Rn or Rm are R15.  */
9939
9940 static void
9941 do_pkhbt (void)
9942 {
9943   inst.instruction |= inst.operands[0].reg << 12;
9944   inst.instruction |= inst.operands[1].reg << 16;
9945   inst.instruction |= inst.operands[2].reg;
9946   if (inst.operands[3].present)
9947     encode_arm_shift (3);
9948 }
9949
9950 /* ARM V6 PKHTB (Argument Parse).  */
9951
9952 static void
9953 do_pkhtb (void)
9954 {
9955   if (!inst.operands[3].present)
9956     {
9957       /* If the shift specifier is omitted, turn the instruction
9958          into pkhbt rd, rm, rn. */
9959       inst.instruction &= 0xfff00010;
9960       inst.instruction |= inst.operands[0].reg << 12;
9961       inst.instruction |= inst.operands[1].reg;
9962       inst.instruction |= inst.operands[2].reg << 16;
9963     }
9964   else
9965     {
9966       inst.instruction |= inst.operands[0].reg << 12;
9967       inst.instruction |= inst.operands[1].reg << 16;
9968       inst.instruction |= inst.operands[2].reg;
9969       encode_arm_shift (3);
9970     }
9971 }
9972
9973 /* ARMv5TE: Preload-Cache
9974    MP Extensions: Preload for write
9975
9976     PLD(W) <addr_mode>
9977
9978   Syntactically, like LDR with B=1, W=0, L=1.  */
9979
9980 static void
9981 do_pld (void)
9982 {
9983   constraint (!inst.operands[0].isreg,
9984               _("'[' expected after PLD mnemonic"));
9985   constraint (inst.operands[0].postind,
9986               _("post-indexed expression used in preload instruction"));
9987   constraint (inst.operands[0].writeback,
9988               _("writeback used in preload instruction"));
9989   constraint (!inst.operands[0].preind,
9990               _("unindexed addressing used in preload instruction"));
9991   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9992 }
9993
9994 /* ARMv7: PLI <addr_mode>  */
9995 static void
9996 do_pli (void)
9997 {
9998   constraint (!inst.operands[0].isreg,
9999               _("'[' expected after PLI mnemonic"));
10000   constraint (inst.operands[0].postind,
10001               _("post-indexed expression used in preload instruction"));
10002   constraint (inst.operands[0].writeback,
10003               _("writeback used in preload instruction"));
10004   constraint (!inst.operands[0].preind,
10005               _("unindexed addressing used in preload instruction"));
10006   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
10007   inst.instruction &= ~PRE_INDEX;
10008 }
10009
10010 static void
10011 do_push_pop (void)
10012 {
10013   constraint (inst.operands[0].writeback,
10014               _("push/pop do not support {reglist}^"));
10015   inst.operands[1] = inst.operands[0];
10016   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
10017   inst.operands[0].isreg = 1;
10018   inst.operands[0].writeback = 1;
10019   inst.operands[0].reg = REG_SP;
10020   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
10021 }
10022
10023 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
10024    word at the specified address and the following word
10025    respectively.
10026    Unconditionally executed.
10027    Error if Rn is R15.  */
10028
10029 static void
10030 do_rfe (void)
10031 {
10032   inst.instruction |= inst.operands[0].reg << 16;
10033   if (inst.operands[0].writeback)
10034     inst.instruction |= WRITE_BACK;
10035 }
10036
10037 /* ARM V6 ssat (argument parse).  */
10038
10039 static void
10040 do_ssat (void)
10041 {
10042   inst.instruction |= inst.operands[0].reg << 12;
10043   inst.instruction |= (inst.operands[1].imm - 1) << 16;
10044   inst.instruction |= inst.operands[2].reg;
10045
10046   if (inst.operands[3].present)
10047     encode_arm_shift (3);
10048 }
10049
10050 /* ARM V6 usat (argument parse).  */
10051
10052 static void
10053 do_usat (void)
10054 {
10055   inst.instruction |= inst.operands[0].reg << 12;
10056   inst.instruction |= inst.operands[1].imm << 16;
10057   inst.instruction |= inst.operands[2].reg;
10058
10059   if (inst.operands[3].present)
10060     encode_arm_shift (3);
10061 }
10062
10063 /* ARM V6 ssat16 (argument parse).  */
10064
10065 static void
10066 do_ssat16 (void)
10067 {
10068   inst.instruction |= inst.operands[0].reg << 12;
10069   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
10070   inst.instruction |= inst.operands[2].reg;
10071 }
10072
10073 static void
10074 do_usat16 (void)
10075 {
10076   inst.instruction |= inst.operands[0].reg << 12;
10077   inst.instruction |= inst.operands[1].imm << 16;
10078   inst.instruction |= inst.operands[2].reg;
10079 }
10080
10081 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
10082    preserving the other bits.
10083
10084    setend <endian_specifier>, where <endian_specifier> is either
10085    BE or LE.  */
10086
10087 static void
10088 do_setend (void)
10089 {
10090   if (warn_on_deprecated
10091       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10092       as_tsktsk (_("setend use is deprecated for ARMv8"));
10093
10094   if (inst.operands[0].imm)
10095     inst.instruction |= 0x200;
10096 }
10097
10098 static void
10099 do_shift (void)
10100 {
10101   unsigned int Rm = (inst.operands[1].present
10102                      ? inst.operands[1].reg
10103                      : inst.operands[0].reg);
10104
10105   inst.instruction |= inst.operands[0].reg << 12;
10106   inst.instruction |= Rm;
10107   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
10108     {
10109       inst.instruction |= inst.operands[2].reg << 8;
10110       inst.instruction |= SHIFT_BY_REG;
10111       /* PR 12854: Error on extraneous shifts.  */
10112       constraint (inst.operands[2].shifted,
10113                   _("extraneous shift as part of operand to shift insn"));
10114     }
10115   else
10116     inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
10117 }
10118
10119 static void
10120 do_smc (void)
10121 {
10122   inst.relocs[0].type = BFD_RELOC_ARM_SMC;
10123   inst.relocs[0].pc_rel = 0;
10124 }
10125
10126 static void
10127 do_hvc (void)
10128 {
10129   inst.relocs[0].type = BFD_RELOC_ARM_HVC;
10130   inst.relocs[0].pc_rel = 0;
10131 }
10132
10133 static void
10134 do_swi (void)
10135 {
10136   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
10137   inst.relocs[0].pc_rel = 0;
10138 }
10139
10140 static void
10141 do_setpan (void)
10142 {
10143   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10144               _("selected processor does not support SETPAN instruction"));
10145
10146   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
10147 }
10148
10149 static void
10150 do_t_setpan (void)
10151 {
10152   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
10153               _("selected processor does not support SETPAN instruction"));
10154
10155   inst.instruction |= (inst.operands[0].imm << 3);
10156 }
10157
10158 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
10159    SMLAxy{cond} Rd,Rm,Rs,Rn
10160    SMLAWy{cond} Rd,Rm,Rs,Rn
10161    Error if any register is R15.  */
10162
10163 static void
10164 do_smla (void)
10165 {
10166   inst.instruction |= inst.operands[0].reg << 16;
10167   inst.instruction |= inst.operands[1].reg;
10168   inst.instruction |= inst.operands[2].reg << 8;
10169   inst.instruction |= inst.operands[3].reg << 12;
10170 }
10171
10172 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
10173    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
10174    Error if any register is R15.
10175    Warning if Rdlo == Rdhi.  */
10176
10177 static void
10178 do_smlal (void)
10179 {
10180   inst.instruction |= inst.operands[0].reg << 12;
10181   inst.instruction |= inst.operands[1].reg << 16;
10182   inst.instruction |= inst.operands[2].reg;
10183   inst.instruction |= inst.operands[3].reg << 8;
10184
10185   if (inst.operands[0].reg == inst.operands[1].reg)
10186     as_tsktsk (_("rdhi and rdlo must be different"));
10187 }
10188
10189 /* ARM V5E (El Segundo) signed-multiply (argument parse)
10190    SMULxy{cond} Rd,Rm,Rs
10191    Error if any register is R15.  */
10192
10193 static void
10194 do_smul (void)
10195 {
10196   inst.instruction |= inst.operands[0].reg << 16;
10197   inst.instruction |= inst.operands[1].reg;
10198   inst.instruction |= inst.operands[2].reg << 8;
10199 }
10200
10201 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
10202    the same for both ARM and Thumb-2.  */
10203
10204 static void
10205 do_srs (void)
10206 {
10207   int reg;
10208
10209   if (inst.operands[0].present)
10210     {
10211       reg = inst.operands[0].reg;
10212       constraint (reg != REG_SP, _("SRS base register must be r13"));
10213     }
10214   else
10215     reg = REG_SP;
10216
10217   inst.instruction |= reg << 16;
10218   inst.instruction |= inst.operands[1].imm;
10219   if (inst.operands[0].writeback || inst.operands[1].writeback)
10220     inst.instruction |= WRITE_BACK;
10221 }
10222
10223 /* ARM V6 strex (argument parse).  */
10224
10225 static void
10226 do_strex (void)
10227 {
10228   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10229               || inst.operands[2].postind || inst.operands[2].writeback
10230               || inst.operands[2].immisreg || inst.operands[2].shifted
10231               || inst.operands[2].negative
10232               /* See comment in do_ldrex().  */
10233               || (inst.operands[2].reg == REG_PC),
10234               BAD_ADDR_MODE);
10235
10236   constraint (inst.operands[0].reg == inst.operands[1].reg
10237               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10238
10239   constraint (inst.relocs[0].exp.X_op != O_constant
10240               || inst.relocs[0].exp.X_add_number != 0,
10241               _("offset must be zero in ARM encoding"));
10242
10243   inst.instruction |= inst.operands[0].reg << 12;
10244   inst.instruction |= inst.operands[1].reg;
10245   inst.instruction |= inst.operands[2].reg << 16;
10246   inst.relocs[0].type = BFD_RELOC_UNUSED;
10247 }
10248
10249 static void
10250 do_t_strexbh (void)
10251 {
10252   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
10253               || inst.operands[2].postind || inst.operands[2].writeback
10254               || inst.operands[2].immisreg || inst.operands[2].shifted
10255               || inst.operands[2].negative,
10256               BAD_ADDR_MODE);
10257
10258   constraint (inst.operands[0].reg == inst.operands[1].reg
10259               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10260
10261   do_rm_rd_rn ();
10262 }
10263
10264 static void
10265 do_strexd (void)
10266 {
10267   constraint (inst.operands[1].reg % 2 != 0,
10268               _("even register required"));
10269   constraint (inst.operands[2].present
10270               && inst.operands[2].reg != inst.operands[1].reg + 1,
10271               _("can only store two consecutive registers"));
10272   /* If op 2 were present and equal to PC, this function wouldn't
10273      have been called in the first place.  */
10274   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
10275
10276   constraint (inst.operands[0].reg == inst.operands[1].reg
10277               || inst.operands[0].reg == inst.operands[1].reg + 1
10278               || inst.operands[0].reg == inst.operands[3].reg,
10279               BAD_OVERLAP);
10280
10281   inst.instruction |= inst.operands[0].reg << 12;
10282   inst.instruction |= inst.operands[1].reg;
10283   inst.instruction |= inst.operands[3].reg << 16;
10284 }
10285
10286 /* ARM V8 STRL.  */
10287 static void
10288 do_stlex (void)
10289 {
10290   constraint (inst.operands[0].reg == inst.operands[1].reg
10291               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10292
10293   do_rd_rm_rn ();
10294 }
10295
10296 static void
10297 do_t_stlex (void)
10298 {
10299   constraint (inst.operands[0].reg == inst.operands[1].reg
10300               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
10301
10302   do_rm_rd_rn ();
10303 }
10304
10305 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
10306    extends it to 32-bits, and adds the result to a value in another
10307    register.  You can specify a rotation by 0, 8, 16, or 24 bits
10308    before extracting the 16-bit value.
10309    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
10310    Condition defaults to COND_ALWAYS.
10311    Error if any register uses R15.  */
10312
10313 static void
10314 do_sxtah (void)
10315 {
10316   inst.instruction |= inst.operands[0].reg << 12;
10317   inst.instruction |= inst.operands[1].reg << 16;
10318   inst.instruction |= inst.operands[2].reg;
10319   inst.instruction |= inst.operands[3].imm << 10;
10320 }
10321
10322 /* ARM V6 SXTH.
10323
10324    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
10325    Condition defaults to COND_ALWAYS.
10326    Error if any register uses R15.  */
10327
10328 static void
10329 do_sxth (void)
10330 {
10331   inst.instruction |= inst.operands[0].reg << 12;
10332   inst.instruction |= inst.operands[1].reg;
10333   inst.instruction |= inst.operands[2].imm << 10;
10334 }
10335 \f
10336 /* VFP instructions.  In a logical order: SP variant first, monad
10337    before dyad, arithmetic then move then load/store.  */
10338
10339 static void
10340 do_vfp_sp_monadic (void)
10341 {
10342   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10343               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10344               _(BAD_FPU));
10345
10346   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10347   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10348 }
10349
10350 static void
10351 do_vfp_sp_dyadic (void)
10352 {
10353   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10354   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10355   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10356 }
10357
10358 static void
10359 do_vfp_sp_compare_z (void)
10360 {
10361   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10362 }
10363
10364 static void
10365 do_vfp_dp_sp_cvt (void)
10366 {
10367   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10368   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10369 }
10370
10371 static void
10372 do_vfp_sp_dp_cvt (void)
10373 {
10374   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10375   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10376 }
10377
10378 static void
10379 do_vfp_reg_from_sp (void)
10380 {
10381   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10382              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10383              _(BAD_FPU));
10384
10385   inst.instruction |= inst.operands[0].reg << 12;
10386   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10387 }
10388
10389 static void
10390 do_vfp_reg2_from_sp2 (void)
10391 {
10392   constraint (inst.operands[2].imm != 2,
10393               _("only two consecutive VFP SP registers allowed here"));
10394   inst.instruction |= inst.operands[0].reg << 12;
10395   inst.instruction |= inst.operands[1].reg << 16;
10396   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10397 }
10398
10399 static void
10400 do_vfp_sp_from_reg (void)
10401 {
10402   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
10403              && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10404              _(BAD_FPU));
10405
10406   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
10407   inst.instruction |= inst.operands[1].reg << 12;
10408 }
10409
10410 static void
10411 do_vfp_sp2_from_reg2 (void)
10412 {
10413   constraint (inst.operands[0].imm != 2,
10414               _("only two consecutive VFP SP registers allowed here"));
10415   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
10416   inst.instruction |= inst.operands[1].reg << 12;
10417   inst.instruction |= inst.operands[2].reg << 16;
10418 }
10419
10420 static void
10421 do_vfp_sp_ldst (void)
10422 {
10423   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10424   encode_arm_cp_address (1, FALSE, TRUE, 0);
10425 }
10426
10427 static void
10428 do_vfp_dp_ldst (void)
10429 {
10430   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10431   encode_arm_cp_address (1, FALSE, TRUE, 0);
10432 }
10433
10434
10435 static void
10436 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
10437 {
10438   if (inst.operands[0].writeback)
10439     inst.instruction |= WRITE_BACK;
10440   else
10441     constraint (ldstm_type != VFP_LDSTMIA,
10442                 _("this addressing mode requires base-register writeback"));
10443   inst.instruction |= inst.operands[0].reg << 16;
10444   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
10445   inst.instruction |= inst.operands[1].imm;
10446 }
10447
10448 static void
10449 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
10450 {
10451   int count;
10452
10453   if (inst.operands[0].writeback)
10454     inst.instruction |= WRITE_BACK;
10455   else
10456     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
10457                 _("this addressing mode requires base-register writeback"));
10458
10459   inst.instruction |= inst.operands[0].reg << 16;
10460   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10461
10462   count = inst.operands[1].imm << 1;
10463   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
10464     count += 1;
10465
10466   inst.instruction |= count;
10467 }
10468
10469 static void
10470 do_vfp_sp_ldstmia (void)
10471 {
10472   vfp_sp_ldstm (VFP_LDSTMIA);
10473 }
10474
10475 static void
10476 do_vfp_sp_ldstmdb (void)
10477 {
10478   vfp_sp_ldstm (VFP_LDSTMDB);
10479 }
10480
10481 static void
10482 do_vfp_dp_ldstmia (void)
10483 {
10484   vfp_dp_ldstm (VFP_LDSTMIA);
10485 }
10486
10487 static void
10488 do_vfp_dp_ldstmdb (void)
10489 {
10490   vfp_dp_ldstm (VFP_LDSTMDB);
10491 }
10492
10493 static void
10494 do_vfp_xp_ldstmia (void)
10495 {
10496   vfp_dp_ldstm (VFP_LDSTMIAX);
10497 }
10498
10499 static void
10500 do_vfp_xp_ldstmdb (void)
10501 {
10502   vfp_dp_ldstm (VFP_LDSTMDBX);
10503 }
10504
10505 static void
10506 do_vfp_dp_rd_rm (void)
10507 {
10508   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
10509               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10510               _(BAD_FPU));
10511
10512   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10513   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10514 }
10515
10516 static void
10517 do_vfp_dp_rn_rd (void)
10518 {
10519   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
10520   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10521 }
10522
10523 static void
10524 do_vfp_dp_rd_rn (void)
10525 {
10526   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10527   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10528 }
10529
10530 static void
10531 do_vfp_dp_rd_rn_rm (void)
10532 {
10533   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10534               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10535               _(BAD_FPU));
10536
10537   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10538   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10539   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
10540 }
10541
10542 static void
10543 do_vfp_dp_rd (void)
10544 {
10545   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10546 }
10547
10548 static void
10549 do_vfp_dp_rm_rd_rn (void)
10550 {
10551   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
10552               && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
10553               _(BAD_FPU));
10554
10555   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
10556   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10557   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
10558 }
10559
10560 /* VFPv3 instructions.  */
10561 static void
10562 do_vfp_sp_const (void)
10563 {
10564   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10565   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10566   inst.instruction |= (inst.operands[1].imm & 0x0f);
10567 }
10568
10569 static void
10570 do_vfp_dp_const (void)
10571 {
10572   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10573   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10574   inst.instruction |= (inst.operands[1].imm & 0x0f);
10575 }
10576
10577 static void
10578 vfp_conv (int srcsize)
10579 {
10580   int immbits = srcsize - inst.operands[1].imm;
10581
10582   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
10583     {
10584       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
10585          i.e. immbits must be in range 0 - 16.  */
10586       inst.error = _("immediate value out of range, expected range [0, 16]");
10587       return;
10588     }
10589   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
10590     {
10591       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
10592          i.e. immbits must be in range 0 - 31.  */
10593       inst.error = _("immediate value out of range, expected range [1, 32]");
10594       return;
10595     }
10596
10597   inst.instruction |= (immbits & 1) << 5;
10598   inst.instruction |= (immbits >> 1);
10599 }
10600
10601 static void
10602 do_vfp_sp_conv_16 (void)
10603 {
10604   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10605   vfp_conv (16);
10606 }
10607
10608 static void
10609 do_vfp_dp_conv_16 (void)
10610 {
10611   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10612   vfp_conv (16);
10613 }
10614
10615 static void
10616 do_vfp_sp_conv_32 (void)
10617 {
10618   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10619   vfp_conv (32);
10620 }
10621
10622 static void
10623 do_vfp_dp_conv_32 (void)
10624 {
10625   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10626   vfp_conv (32);
10627 }
10628 \f
10629 /* FPA instructions.  Also in a logical order.  */
10630
10631 static void
10632 do_fpa_cmp (void)
10633 {
10634   inst.instruction |= inst.operands[0].reg << 16;
10635   inst.instruction |= inst.operands[1].reg;
10636 }
10637
10638 static void
10639 do_fpa_ldmstm (void)
10640 {
10641   inst.instruction |= inst.operands[0].reg << 12;
10642   switch (inst.operands[1].imm)
10643     {
10644     case 1: inst.instruction |= CP_T_X;          break;
10645     case 2: inst.instruction |= CP_T_Y;          break;
10646     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10647     case 4:                                      break;
10648     default: abort ();
10649     }
10650
10651   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10652     {
10653       /* The instruction specified "ea" or "fd", so we can only accept
10654          [Rn]{!}.  The instruction does not really support stacking or
10655          unstacking, so we have to emulate these by setting appropriate
10656          bits and offsets.  */
10657       constraint (inst.relocs[0].exp.X_op != O_constant
10658                   || inst.relocs[0].exp.X_add_number != 0,
10659                   _("this instruction does not support indexing"));
10660
10661       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10662         inst.relocs[0].exp.X_add_number = 12 * inst.operands[1].imm;
10663
10664       if (!(inst.instruction & INDEX_UP))
10665         inst.relocs[0].exp.X_add_number = -inst.relocs[0].exp.X_add_number;
10666
10667       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10668         {
10669           inst.operands[2].preind = 0;
10670           inst.operands[2].postind = 1;
10671         }
10672     }
10673
10674   encode_arm_cp_address (2, TRUE, TRUE, 0);
10675 }
10676 \f
10677 /* iWMMXt instructions: strictly in alphabetical order.  */
10678
10679 static void
10680 do_iwmmxt_tandorc (void)
10681 {
10682   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10683 }
10684
10685 static void
10686 do_iwmmxt_textrc (void)
10687 {
10688   inst.instruction |= inst.operands[0].reg << 12;
10689   inst.instruction |= inst.operands[1].imm;
10690 }
10691
10692 static void
10693 do_iwmmxt_textrm (void)
10694 {
10695   inst.instruction |= inst.operands[0].reg << 12;
10696   inst.instruction |= inst.operands[1].reg << 16;
10697   inst.instruction |= inst.operands[2].imm;
10698 }
10699
10700 static void
10701 do_iwmmxt_tinsr (void)
10702 {
10703   inst.instruction |= inst.operands[0].reg << 16;
10704   inst.instruction |= inst.operands[1].reg << 12;
10705   inst.instruction |= inst.operands[2].imm;
10706 }
10707
10708 static void
10709 do_iwmmxt_tmia (void)
10710 {
10711   inst.instruction |= inst.operands[0].reg << 5;
10712   inst.instruction |= inst.operands[1].reg;
10713   inst.instruction |= inst.operands[2].reg << 12;
10714 }
10715
10716 static void
10717 do_iwmmxt_waligni (void)
10718 {
10719   inst.instruction |= inst.operands[0].reg << 12;
10720   inst.instruction |= inst.operands[1].reg << 16;
10721   inst.instruction |= inst.operands[2].reg;
10722   inst.instruction |= inst.operands[3].imm << 20;
10723 }
10724
10725 static void
10726 do_iwmmxt_wmerge (void)
10727 {
10728   inst.instruction |= inst.operands[0].reg << 12;
10729   inst.instruction |= inst.operands[1].reg << 16;
10730   inst.instruction |= inst.operands[2].reg;
10731   inst.instruction |= inst.operands[3].imm << 21;
10732 }
10733
10734 static void
10735 do_iwmmxt_wmov (void)
10736 {
10737   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10738   inst.instruction |= inst.operands[0].reg << 12;
10739   inst.instruction |= inst.operands[1].reg << 16;
10740   inst.instruction |= inst.operands[1].reg;
10741 }
10742
10743 static void
10744 do_iwmmxt_wldstbh (void)
10745 {
10746   int reloc;
10747   inst.instruction |= inst.operands[0].reg << 12;
10748   if (thumb_mode)
10749     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10750   else
10751     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10752   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10753 }
10754
10755 static void
10756 do_iwmmxt_wldstw (void)
10757 {
10758   /* RIWR_RIWC clears .isreg for a control register.  */
10759   if (!inst.operands[0].isreg)
10760     {
10761       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10762       inst.instruction |= 0xf0000000;
10763     }
10764
10765   inst.instruction |= inst.operands[0].reg << 12;
10766   encode_arm_cp_address (1, TRUE, TRUE, 0);
10767 }
10768
10769 static void
10770 do_iwmmxt_wldstd (void)
10771 {
10772   inst.instruction |= inst.operands[0].reg << 12;
10773   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10774       && inst.operands[1].immisreg)
10775     {
10776       inst.instruction &= ~0x1a000ff;
10777       inst.instruction |= (0xfU << 28);
10778       if (inst.operands[1].preind)
10779         inst.instruction |= PRE_INDEX;
10780       if (!inst.operands[1].negative)
10781         inst.instruction |= INDEX_UP;
10782       if (inst.operands[1].writeback)
10783         inst.instruction |= WRITE_BACK;
10784       inst.instruction |= inst.operands[1].reg << 16;
10785       inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
10786       inst.instruction |= inst.operands[1].imm;
10787     }
10788   else
10789     encode_arm_cp_address (1, TRUE, FALSE, 0);
10790 }
10791
10792 static void
10793 do_iwmmxt_wshufh (void)
10794 {
10795   inst.instruction |= inst.operands[0].reg << 12;
10796   inst.instruction |= inst.operands[1].reg << 16;
10797   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10798   inst.instruction |= (inst.operands[2].imm & 0x0f);
10799 }
10800
10801 static void
10802 do_iwmmxt_wzero (void)
10803 {
10804   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
10805   inst.instruction |= inst.operands[0].reg;
10806   inst.instruction |= inst.operands[0].reg << 12;
10807   inst.instruction |= inst.operands[0].reg << 16;
10808 }
10809
10810 static void
10811 do_iwmmxt_wrwrwr_or_imm5 (void)
10812 {
10813   if (inst.operands[2].isreg)
10814     do_rd_rn_rm ();
10815   else {
10816     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
10817                 _("immediate operand requires iWMMXt2"));
10818     do_rd_rn ();
10819     if (inst.operands[2].imm == 0)
10820       {
10821         switch ((inst.instruction >> 20) & 0xf)
10822           {
10823           case 4:
10824           case 5:
10825           case 6:
10826           case 7:
10827             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
10828             inst.operands[2].imm = 16;
10829             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
10830             break;
10831           case 8:
10832           case 9:
10833           case 10:
10834           case 11:
10835             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10836             inst.operands[2].imm = 32;
10837             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10838             break;
10839           case 12:
10840           case 13:
10841           case 14:
10842           case 15:
10843             {
10844               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10845               unsigned long wrn;
10846               wrn = (inst.instruction >> 16) & 0xf;
10847               inst.instruction &= 0xff0fff0f;
10848               inst.instruction |= wrn;
10849               /* Bail out here; the instruction is now assembled.  */
10850               return;
10851             }
10852           }
10853       }
10854     /* Map 32 -> 0, etc.  */
10855     inst.operands[2].imm &= 0x1f;
10856     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10857   }
10858 }
10859 \f
10860 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10861    operations first, then control, shift, and load/store.  */
10862
10863 /* Insns like "foo X,Y,Z".  */
10864
10865 static void
10866 do_mav_triple (void)
10867 {
10868   inst.instruction |= inst.operands[0].reg << 16;
10869   inst.instruction |= inst.operands[1].reg;
10870   inst.instruction |= inst.operands[2].reg << 12;
10871 }
10872
10873 /* Insns like "foo W,X,Y,Z".
10874     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10875
10876 static void
10877 do_mav_quad (void)
10878 {
10879   inst.instruction |= inst.operands[0].reg << 5;
10880   inst.instruction |= inst.operands[1].reg << 12;
10881   inst.instruction |= inst.operands[2].reg << 16;
10882   inst.instruction |= inst.operands[3].reg;
10883 }
10884
10885 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10886 static void
10887 do_mav_dspsc (void)
10888 {
10889   inst.instruction |= inst.operands[1].reg << 12;
10890 }
10891
10892 /* Maverick shift immediate instructions.
10893    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10894    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10895
10896 static void
10897 do_mav_shift (void)
10898 {
10899   int imm = inst.operands[2].imm;
10900
10901   inst.instruction |= inst.operands[0].reg << 12;
10902   inst.instruction |= inst.operands[1].reg << 16;
10903
10904   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10905      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10906      Bit 4 should be 0.  */
10907   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10908
10909   inst.instruction |= imm;
10910 }
10911 \f
10912 /* XScale instructions.  Also sorted arithmetic before move.  */
10913
10914 /* Xscale multiply-accumulate (argument parse)
10915      MIAcc   acc0,Rm,Rs
10916      MIAPHcc acc0,Rm,Rs
10917      MIAxycc acc0,Rm,Rs.  */
10918
10919 static void
10920 do_xsc_mia (void)
10921 {
10922   inst.instruction |= inst.operands[1].reg;
10923   inst.instruction |= inst.operands[2].reg << 12;
10924 }
10925
10926 /* Xscale move-accumulator-register (argument parse)
10927
10928      MARcc   acc0,RdLo,RdHi.  */
10929
10930 static void
10931 do_xsc_mar (void)
10932 {
10933   inst.instruction |= inst.operands[1].reg << 12;
10934   inst.instruction |= inst.operands[2].reg << 16;
10935 }
10936
10937 /* Xscale move-register-accumulator (argument parse)
10938
10939      MRAcc   RdLo,RdHi,acc0.  */
10940
10941 static void
10942 do_xsc_mra (void)
10943 {
10944   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10945   inst.instruction |= inst.operands[0].reg << 12;
10946   inst.instruction |= inst.operands[1].reg << 16;
10947 }
10948 \f
10949 /* Encoding functions relevant only to Thumb.  */
10950
10951 /* inst.operands[i] is a shifted-register operand; encode
10952    it into inst.instruction in the format used by Thumb32.  */
10953
10954 static void
10955 encode_thumb32_shifted_operand (int i)
10956 {
10957   unsigned int value = inst.relocs[0].exp.X_add_number;
10958   unsigned int shift = inst.operands[i].shift_kind;
10959
10960   constraint (inst.operands[i].immisreg,
10961               _("shift by register not allowed in thumb mode"));
10962   inst.instruction |= inst.operands[i].reg;
10963   if (shift == SHIFT_RRX)
10964     inst.instruction |= SHIFT_ROR << 4;
10965   else
10966     {
10967       constraint (inst.relocs[0].exp.X_op != O_constant,
10968                   _("expression too complex"));
10969
10970       constraint (value > 32
10971                   || (value == 32 && (shift == SHIFT_LSL
10972                                       || shift == SHIFT_ROR)),
10973                   _("shift expression is too large"));
10974
10975       if (value == 0)
10976         shift = SHIFT_LSL;
10977       else if (value == 32)
10978         value = 0;
10979
10980       inst.instruction |= shift << 4;
10981       inst.instruction |= (value & 0x1c) << 10;
10982       inst.instruction |= (value & 0x03) << 6;
10983     }
10984 }
10985
10986
10987 /* inst.operands[i] was set up by parse_address.  Encode it into a
10988    Thumb32 format load or store instruction.  Reject forms that cannot
10989    be used with such instructions.  If is_t is true, reject forms that
10990    cannot be used with a T instruction; if is_d is true, reject forms
10991    that cannot be used with a D instruction.  If it is a store insn,
10992    reject PC in Rn.  */
10993
10994 static void
10995 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10996 {
10997   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10998
10999   constraint (!inst.operands[i].isreg,
11000               _("Instruction does not support =N addresses"));
11001
11002   inst.instruction |= inst.operands[i].reg << 16;
11003   if (inst.operands[i].immisreg)
11004     {
11005       constraint (is_pc, BAD_PC_ADDRESSING);
11006       constraint (is_t || is_d, _("cannot use register index with this instruction"));
11007       constraint (inst.operands[i].negative,
11008                   _("Thumb does not support negative register indexing"));
11009       constraint (inst.operands[i].postind,
11010                   _("Thumb does not support register post-indexing"));
11011       constraint (inst.operands[i].writeback,
11012                   _("Thumb does not support register indexing with writeback"));
11013       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
11014                   _("Thumb supports only LSL in shifted register indexing"));
11015
11016       inst.instruction |= inst.operands[i].imm;
11017       if (inst.operands[i].shifted)
11018         {
11019           constraint (inst.relocs[0].exp.X_op != O_constant,
11020                       _("expression too complex"));
11021           constraint (inst.relocs[0].exp.X_add_number < 0
11022                       || inst.relocs[0].exp.X_add_number > 3,
11023                       _("shift out of range"));
11024           inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
11025         }
11026       inst.relocs[0].type = BFD_RELOC_UNUSED;
11027     }
11028   else if (inst.operands[i].preind)
11029     {
11030       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
11031       constraint (is_t && inst.operands[i].writeback,
11032                   _("cannot use writeback with this instruction"));
11033       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
11034                   BAD_PC_ADDRESSING);
11035
11036       if (is_d)
11037         {
11038           inst.instruction |= 0x01000000;
11039           if (inst.operands[i].writeback)
11040             inst.instruction |= 0x00200000;
11041         }
11042       else
11043         {
11044           inst.instruction |= 0x00000c00;
11045           if (inst.operands[i].writeback)
11046             inst.instruction |= 0x00000100;
11047         }
11048       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11049     }
11050   else if (inst.operands[i].postind)
11051     {
11052       gas_assert (inst.operands[i].writeback);
11053       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
11054       constraint (is_t, _("cannot use post-indexing with this instruction"));
11055
11056       if (is_d)
11057         inst.instruction |= 0x00200000;
11058       else
11059         inst.instruction |= 0x00000900;
11060       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
11061     }
11062   else /* unindexed - only for coprocessor */
11063     inst.error = _("instruction does not accept unindexed addressing");
11064 }
11065
11066 /* Table of Thumb instructions which exist in both 16- and 32-bit
11067    encodings (the latter only in post-V6T2 cores).  The index is the
11068    value used in the insns table below.  When there is more than one
11069    possible 16-bit encoding for the instruction, this table always
11070    holds variant (1).
11071    Also contains several pseudo-instructions used during relaxation.  */
11072 #define T16_32_TAB                              \
11073   X(_adc,   4140, eb400000),                    \
11074   X(_adcs,  4140, eb500000),                    \
11075   X(_add,   1c00, eb000000),                    \
11076   X(_adds,  1c00, eb100000),                    \
11077   X(_addi,  0000, f1000000),                    \
11078   X(_addis, 0000, f1100000),                    \
11079   X(_add_pc,000f, f20f0000),                    \
11080   X(_add_sp,000d, f10d0000),                    \
11081   X(_adr,   000f, f20f0000),                    \
11082   X(_and,   4000, ea000000),                    \
11083   X(_ands,  4000, ea100000),                    \
11084   X(_asr,   1000, fa40f000),                    \
11085   X(_asrs,  1000, fa50f000),                    \
11086   X(_b,     e000, f000b000),                    \
11087   X(_bcond, d000, f0008000),                    \
11088   X(_bf,    0000, f040e001),                    \
11089   X(_bfcsel,0000, f000e001),                    \
11090   X(_bfx,   0000, f060e001),                    \
11091   X(_bfl,   0000, f000c001),                    \
11092   X(_bflx,  0000, f070e001),                    \
11093   X(_bic,   4380, ea200000),                    \
11094   X(_bics,  4380, ea300000),                    \
11095   X(_cmn,   42c0, eb100f00),                    \
11096   X(_cmp,   2800, ebb00f00),                    \
11097   X(_cpsie, b660, f3af8400),                    \
11098   X(_cpsid, b670, f3af8600),                    \
11099   X(_cpy,   4600, ea4f0000),                    \
11100   X(_dec_sp,80dd, f1ad0d00),                    \
11101   X(_dls,   0000, f040e001),                    \
11102   X(_eor,   4040, ea800000),                    \
11103   X(_eors,  4040, ea900000),                    \
11104   X(_inc_sp,00dd, f10d0d00),                    \
11105   X(_ldmia, c800, e8900000),                    \
11106   X(_ldr,   6800, f8500000),                    \
11107   X(_ldrb,  7800, f8100000),                    \
11108   X(_ldrh,  8800, f8300000),                    \
11109   X(_ldrsb, 5600, f9100000),                    \
11110   X(_ldrsh, 5e00, f9300000),                    \
11111   X(_ldr_pc,4800, f85f0000),                    \
11112   X(_ldr_pc2,4800, f85f0000),                   \
11113   X(_ldr_sp,9800, f85d0000),                    \
11114   X(_le,    0000, f00fc001),                    \
11115   X(_lsl,   0000, fa00f000),                    \
11116   X(_lsls,  0000, fa10f000),                    \
11117   X(_lsr,   0800, fa20f000),                    \
11118   X(_lsrs,  0800, fa30f000),                    \
11119   X(_mov,   2000, ea4f0000),                    \
11120   X(_movs,  2000, ea5f0000),                    \
11121   X(_mul,   4340, fb00f000),                     \
11122   X(_muls,  4340, ffffffff), /* no 32b muls */  \
11123   X(_mvn,   43c0, ea6f0000),                    \
11124   X(_mvns,  43c0, ea7f0000),                    \
11125   X(_neg,   4240, f1c00000), /* rsb #0 */       \
11126   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
11127   X(_orr,   4300, ea400000),                    \
11128   X(_orrs,  4300, ea500000),                    \
11129   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
11130   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
11131   X(_rev,   ba00, fa90f080),                    \
11132   X(_rev16, ba40, fa90f090),                    \
11133   X(_revsh, bac0, fa90f0b0),                    \
11134   X(_ror,   41c0, fa60f000),                    \
11135   X(_rors,  41c0, fa70f000),                    \
11136   X(_sbc,   4180, eb600000),                    \
11137   X(_sbcs,  4180, eb700000),                    \
11138   X(_stmia, c000, e8800000),                    \
11139   X(_str,   6000, f8400000),                    \
11140   X(_strb,  7000, f8000000),                    \
11141   X(_strh,  8000, f8200000),                    \
11142   X(_str_sp,9000, f84d0000),                    \
11143   X(_sub,   1e00, eba00000),                    \
11144   X(_subs,  1e00, ebb00000),                    \
11145   X(_subi,  8000, f1a00000),                    \
11146   X(_subis, 8000, f1b00000),                    \
11147   X(_sxtb,  b240, fa4ff080),                    \
11148   X(_sxth,  b200, fa0ff080),                    \
11149   X(_tst,   4200, ea100f00),                    \
11150   X(_uxtb,  b2c0, fa5ff080),                    \
11151   X(_uxth,  b280, fa1ff080),                    \
11152   X(_nop,   bf00, f3af8000),                    \
11153   X(_yield, bf10, f3af8001),                    \
11154   X(_wfe,   bf20, f3af8002),                    \
11155   X(_wfi,   bf30, f3af8003),                    \
11156   X(_wls,   0000, f040c001),                    \
11157   X(_sev,   bf40, f3af8004),                    \
11158   X(_sevl,  bf50, f3af8005),                    \
11159   X(_udf,   de00, f7f0a000)
11160
11161 /* To catch errors in encoding functions, the codes are all offset by
11162    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
11163    as 16-bit instructions.  */
11164 #define X(a,b,c) T_MNEM##a
11165 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
11166 #undef X
11167
11168 #define X(a,b,c) 0x##b
11169 static const unsigned short thumb_op16[] = { T16_32_TAB };
11170 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
11171 #undef X
11172
11173 #define X(a,b,c) 0x##c
11174 static const unsigned int thumb_op32[] = { T16_32_TAB };
11175 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
11176 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
11177 #undef X
11178 #undef T16_32_TAB
11179
11180 /* Thumb instruction encoders, in alphabetical order.  */
11181
11182 /* ADDW or SUBW.  */
11183
11184 static void
11185 do_t_add_sub_w (void)
11186 {
11187   int Rd, Rn;
11188
11189   Rd = inst.operands[0].reg;
11190   Rn = inst.operands[1].reg;
11191
11192   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
11193      is the SP-{plus,minus}-immediate form of the instruction.  */
11194   if (Rn == REG_SP)
11195     constraint (Rd == REG_PC, BAD_PC);
11196   else
11197     reject_bad_reg (Rd);
11198
11199   inst.instruction |= (Rn << 16) | (Rd << 8);
11200   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11201 }
11202
11203 /* Parse an add or subtract instruction.  We get here with inst.instruction
11204    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
11205
11206 static void
11207 do_t_add_sub (void)
11208 {
11209   int Rd, Rs, Rn;
11210
11211   Rd = inst.operands[0].reg;
11212   Rs = (inst.operands[1].present
11213         ? inst.operands[1].reg    /* Rd, Rs, foo */
11214         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11215
11216   if (Rd == REG_PC)
11217     set_pred_insn_type_last ();
11218
11219   if (unified_syntax)
11220     {
11221       bfd_boolean flags;
11222       bfd_boolean narrow;
11223       int opcode;
11224
11225       flags = (inst.instruction == T_MNEM_adds
11226                || inst.instruction == T_MNEM_subs);
11227       if (flags)
11228         narrow = !in_pred_block ();
11229       else
11230         narrow = in_pred_block ();
11231       if (!inst.operands[2].isreg)
11232         {
11233           int add;
11234
11235           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11236             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11237
11238           add = (inst.instruction == T_MNEM_add
11239                  || inst.instruction == T_MNEM_adds);
11240           opcode = 0;
11241           if (inst.size_req != 4)
11242             {
11243               /* Attempt to use a narrow opcode, with relaxation if
11244                  appropriate.  */
11245               if (Rd == REG_SP && Rs == REG_SP && !flags)
11246                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
11247               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
11248                 opcode = T_MNEM_add_sp;
11249               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
11250                 opcode = T_MNEM_add_pc;
11251               else if (Rd <= 7 && Rs <= 7 && narrow)
11252                 {
11253                   if (flags)
11254                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
11255                   else
11256                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
11257                 }
11258               if (opcode)
11259                 {
11260                   inst.instruction = THUMB_OP16(opcode);
11261                   inst.instruction |= (Rd << 4) | Rs;
11262                   if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11263                       || (inst.relocs[0].type
11264                           > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC))
11265                   {
11266                     if (inst.size_req == 2)
11267                       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11268                     else
11269                       inst.relax = opcode;
11270                   }
11271                 }
11272               else
11273                 constraint (inst.size_req == 2, BAD_HIREG);
11274             }
11275           if (inst.size_req == 4
11276               || (inst.size_req != 2 && !opcode))
11277             {
11278               constraint ((inst.relocs[0].type
11279                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
11280                           && (inst.relocs[0].type
11281                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
11282                           THUMB1_RELOC_ONLY);
11283               if (Rd == REG_PC)
11284                 {
11285                   constraint (add, BAD_PC);
11286                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
11287                              _("only SUBS PC, LR, #const allowed"));
11288                   constraint (inst.relocs[0].exp.X_op != O_constant,
11289                               _("expression too complex"));
11290                   constraint (inst.relocs[0].exp.X_add_number < 0
11291                               || inst.relocs[0].exp.X_add_number > 0xff,
11292                              _("immediate value out of range"));
11293                   inst.instruction = T2_SUBS_PC_LR
11294                                      | inst.relocs[0].exp.X_add_number;
11295                   inst.relocs[0].type = BFD_RELOC_UNUSED;
11296                   return;
11297                 }
11298               else if (Rs == REG_PC)
11299                 {
11300                   /* Always use addw/subw.  */
11301                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
11302                   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
11303                 }
11304               else
11305                 {
11306                   inst.instruction = THUMB_OP32 (inst.instruction);
11307                   inst.instruction = (inst.instruction & 0xe1ffffff)
11308                                      | 0x10000000;
11309                   if (flags)
11310                     inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11311                   else
11312                     inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_IMM;
11313                 }
11314               inst.instruction |= Rd << 8;
11315               inst.instruction |= Rs << 16;
11316             }
11317         }
11318       else
11319         {
11320           unsigned int value = inst.relocs[0].exp.X_add_number;
11321           unsigned int shift = inst.operands[2].shift_kind;
11322
11323           Rn = inst.operands[2].reg;
11324           /* See if we can do this with a 16-bit instruction.  */
11325           if (!inst.operands[2].shifted && inst.size_req != 4)
11326             {
11327               if (Rd > 7 || Rs > 7 || Rn > 7)
11328                 narrow = FALSE;
11329
11330               if (narrow)
11331                 {
11332                   inst.instruction = ((inst.instruction == T_MNEM_adds
11333                                        || inst.instruction == T_MNEM_add)
11334                                       ? T_OPCODE_ADD_R3
11335                                       : T_OPCODE_SUB_R3);
11336                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11337                   return;
11338                 }
11339
11340               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
11341                 {
11342                   /* Thumb-1 cores (except v6-M) require at least one high
11343                      register in a narrow non flag setting add.  */
11344                   if (Rd > 7 || Rn > 7
11345                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
11346                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
11347                     {
11348                       if (Rd == Rn)
11349                         {
11350                           Rn = Rs;
11351                           Rs = Rd;
11352                         }
11353                       inst.instruction = T_OPCODE_ADD_HI;
11354                       inst.instruction |= (Rd & 8) << 4;
11355                       inst.instruction |= (Rd & 7);
11356                       inst.instruction |= Rn << 3;
11357                       return;
11358                     }
11359                 }
11360             }
11361
11362           constraint (Rd == REG_PC, BAD_PC);
11363           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11364             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11365           constraint (Rs == REG_PC, BAD_PC);
11366           reject_bad_reg (Rn);
11367
11368           /* If we get here, it can't be done in 16 bits.  */
11369           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
11370                       _("shift must be constant"));
11371           inst.instruction = THUMB_OP32 (inst.instruction);
11372           inst.instruction |= Rd << 8;
11373           inst.instruction |= Rs << 16;
11374           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
11375                       _("shift value over 3 not allowed in thumb mode"));
11376           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
11377                       _("only LSL shift allowed in thumb mode"));
11378           encode_thumb32_shifted_operand (2);
11379         }
11380     }
11381   else
11382     {
11383       constraint (inst.instruction == T_MNEM_adds
11384                   || inst.instruction == T_MNEM_subs,
11385                   BAD_THUMB32);
11386
11387       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
11388         {
11389           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
11390                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
11391                       BAD_HIREG);
11392
11393           inst.instruction = (inst.instruction == T_MNEM_add
11394                               ? 0x0000 : 0x8000);
11395           inst.instruction |= (Rd << 4) | Rs;
11396           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11397           return;
11398         }
11399
11400       Rn = inst.operands[2].reg;
11401       constraint (inst.operands[2].shifted, _("unshifted register required"));
11402
11403       /* We now have Rd, Rs, and Rn set to registers.  */
11404       if (Rd > 7 || Rs > 7 || Rn > 7)
11405         {
11406           /* Can't do this for SUB.      */
11407           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
11408           inst.instruction = T_OPCODE_ADD_HI;
11409           inst.instruction |= (Rd & 8) << 4;
11410           inst.instruction |= (Rd & 7);
11411           if (Rs == Rd)
11412             inst.instruction |= Rn << 3;
11413           else if (Rn == Rd)
11414             inst.instruction |= Rs << 3;
11415           else
11416             constraint (1, _("dest must overlap one source register"));
11417         }
11418       else
11419         {
11420           inst.instruction = (inst.instruction == T_MNEM_add
11421                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
11422           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11423         }
11424     }
11425 }
11426
11427 static void
11428 do_t_adr (void)
11429 {
11430   unsigned Rd;
11431
11432   Rd = inst.operands[0].reg;
11433   reject_bad_reg (Rd);
11434
11435   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
11436     {
11437       /* Defer to section relaxation.  */
11438       inst.relax = inst.instruction;
11439       inst.instruction = THUMB_OP16 (inst.instruction);
11440       inst.instruction |= Rd << 4;
11441     }
11442   else if (unified_syntax && inst.size_req != 2)
11443     {
11444       /* Generate a 32-bit opcode.  */
11445       inst.instruction = THUMB_OP32 (inst.instruction);
11446       inst.instruction |= Rd << 8;
11447       inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_PC12;
11448       inst.relocs[0].pc_rel = 1;
11449     }
11450   else
11451     {
11452       /* Generate a 16-bit opcode.  */
11453       inst.instruction = THUMB_OP16 (inst.instruction);
11454       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11455       inst.relocs[0].exp.X_add_number -= 4; /* PC relative adjust.  */
11456       inst.relocs[0].pc_rel = 1;
11457       inst.instruction |= Rd << 4;
11458     }
11459
11460   if (inst.relocs[0].exp.X_op == O_symbol
11461       && inst.relocs[0].exp.X_add_symbol != NULL
11462       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11463       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11464     inst.relocs[0].exp.X_add_number += 1;
11465 }
11466
11467 /* Arithmetic instructions for which there is just one 16-bit
11468    instruction encoding, and it allows only two low registers.
11469    For maximal compatibility with ARM syntax, we allow three register
11470    operands even when Thumb-32 instructions are not available, as long
11471    as the first two are identical.  For instance, both "sbc r0,r1" and
11472    "sbc r0,r0,r1" are allowed.  */
11473 static void
11474 do_t_arit3 (void)
11475 {
11476   int Rd, Rs, Rn;
11477
11478   Rd = inst.operands[0].reg;
11479   Rs = (inst.operands[1].present
11480         ? inst.operands[1].reg    /* Rd, Rs, foo */
11481         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11482   Rn = inst.operands[2].reg;
11483
11484   reject_bad_reg (Rd);
11485   reject_bad_reg (Rs);
11486   if (inst.operands[2].isreg)
11487     reject_bad_reg (Rn);
11488
11489   if (unified_syntax)
11490     {
11491       if (!inst.operands[2].isreg)
11492         {
11493           /* For an immediate, we always generate a 32-bit opcode;
11494              section relaxation will shrink it later if possible.  */
11495           inst.instruction = THUMB_OP32 (inst.instruction);
11496           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11497           inst.instruction |= Rd << 8;
11498           inst.instruction |= Rs << 16;
11499           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11500         }
11501       else
11502         {
11503           bfd_boolean narrow;
11504
11505           /* See if we can do this with a 16-bit instruction.  */
11506           if (THUMB_SETS_FLAGS (inst.instruction))
11507             narrow = !in_pred_block ();
11508           else
11509             narrow = in_pred_block ();
11510
11511           if (Rd > 7 || Rn > 7 || Rs > 7)
11512             narrow = FALSE;
11513           if (inst.operands[2].shifted)
11514             narrow = FALSE;
11515           if (inst.size_req == 4)
11516             narrow = FALSE;
11517
11518           if (narrow
11519               && Rd == Rs)
11520             {
11521               inst.instruction = THUMB_OP16 (inst.instruction);
11522               inst.instruction |= Rd;
11523               inst.instruction |= Rn << 3;
11524               return;
11525             }
11526
11527           /* If we get here, it can't be done in 16 bits.  */
11528           constraint (inst.operands[2].shifted
11529                       && inst.operands[2].immisreg,
11530                       _("shift must be constant"));
11531           inst.instruction = THUMB_OP32 (inst.instruction);
11532           inst.instruction |= Rd << 8;
11533           inst.instruction |= Rs << 16;
11534           encode_thumb32_shifted_operand (2);
11535         }
11536     }
11537   else
11538     {
11539       /* On its face this is a lie - the instruction does set the
11540          flags.  However, the only supported mnemonic in this mode
11541          says it doesn't.  */
11542       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11543
11544       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11545                   _("unshifted register required"));
11546       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11547       constraint (Rd != Rs,
11548                   _("dest and source1 must be the same register"));
11549
11550       inst.instruction = THUMB_OP16 (inst.instruction);
11551       inst.instruction |= Rd;
11552       inst.instruction |= Rn << 3;
11553     }
11554 }
11555
11556 /* Similarly, but for instructions where the arithmetic operation is
11557    commutative, so we can allow either of them to be different from
11558    the destination operand in a 16-bit instruction.  For instance, all
11559    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
11560    accepted.  */
11561 static void
11562 do_t_arit3c (void)
11563 {
11564   int Rd, Rs, Rn;
11565
11566   Rd = inst.operands[0].reg;
11567   Rs = (inst.operands[1].present
11568         ? inst.operands[1].reg    /* Rd, Rs, foo */
11569         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11570   Rn = inst.operands[2].reg;
11571
11572   reject_bad_reg (Rd);
11573   reject_bad_reg (Rs);
11574   if (inst.operands[2].isreg)
11575     reject_bad_reg (Rn);
11576
11577   if (unified_syntax)
11578     {
11579       if (!inst.operands[2].isreg)
11580         {
11581           /* For an immediate, we always generate a 32-bit opcode;
11582              section relaxation will shrink it later if possible.  */
11583           inst.instruction = THUMB_OP32 (inst.instruction);
11584           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11585           inst.instruction |= Rd << 8;
11586           inst.instruction |= Rs << 16;
11587           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11588         }
11589       else
11590         {
11591           bfd_boolean narrow;
11592
11593           /* See if we can do this with a 16-bit instruction.  */
11594           if (THUMB_SETS_FLAGS (inst.instruction))
11595             narrow = !in_pred_block ();
11596           else
11597             narrow = in_pred_block ();
11598
11599           if (Rd > 7 || Rn > 7 || Rs > 7)
11600             narrow = FALSE;
11601           if (inst.operands[2].shifted)
11602             narrow = FALSE;
11603           if (inst.size_req == 4)
11604             narrow = FALSE;
11605
11606           if (narrow)
11607             {
11608               if (Rd == Rs)
11609                 {
11610                   inst.instruction = THUMB_OP16 (inst.instruction);
11611                   inst.instruction |= Rd;
11612                   inst.instruction |= Rn << 3;
11613                   return;
11614                 }
11615               if (Rd == Rn)
11616                 {
11617                   inst.instruction = THUMB_OP16 (inst.instruction);
11618                   inst.instruction |= Rd;
11619                   inst.instruction |= Rs << 3;
11620                   return;
11621                 }
11622             }
11623
11624           /* If we get here, it can't be done in 16 bits.  */
11625           constraint (inst.operands[2].shifted
11626                       && inst.operands[2].immisreg,
11627                       _("shift must be constant"));
11628           inst.instruction = THUMB_OP32 (inst.instruction);
11629           inst.instruction |= Rd << 8;
11630           inst.instruction |= Rs << 16;
11631           encode_thumb32_shifted_operand (2);
11632         }
11633     }
11634   else
11635     {
11636       /* On its face this is a lie - the instruction does set the
11637          flags.  However, the only supported mnemonic in this mode
11638          says it doesn't.  */
11639       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11640
11641       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11642                   _("unshifted register required"));
11643       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11644
11645       inst.instruction = THUMB_OP16 (inst.instruction);
11646       inst.instruction |= Rd;
11647
11648       if (Rd == Rs)
11649         inst.instruction |= Rn << 3;
11650       else if (Rd == Rn)
11651         inst.instruction |= Rs << 3;
11652       else
11653         constraint (1, _("dest must overlap one source register"));
11654     }
11655 }
11656
11657 static void
11658 do_t_bfc (void)
11659 {
11660   unsigned Rd;
11661   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11662   constraint (msb > 32, _("bit-field extends past end of register"));
11663   /* The instruction encoding stores the LSB and MSB,
11664      not the LSB and width.  */
11665   Rd = inst.operands[0].reg;
11666   reject_bad_reg (Rd);
11667   inst.instruction |= Rd << 8;
11668   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11669   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11670   inst.instruction |= msb - 1;
11671 }
11672
11673 static void
11674 do_t_bfi (void)
11675 {
11676   int Rd, Rn;
11677   unsigned int msb;
11678
11679   Rd = inst.operands[0].reg;
11680   reject_bad_reg (Rd);
11681
11682   /* #0 in second position is alternative syntax for bfc, which is
11683      the same instruction but with REG_PC in the Rm field.  */
11684   if (!inst.operands[1].isreg)
11685     Rn = REG_PC;
11686   else
11687     {
11688       Rn = inst.operands[1].reg;
11689       reject_bad_reg (Rn);
11690     }
11691
11692   msb = inst.operands[2].imm + inst.operands[3].imm;
11693   constraint (msb > 32, _("bit-field extends past end of register"));
11694   /* The instruction encoding stores the LSB and MSB,
11695      not the LSB and width.  */
11696   inst.instruction |= Rd << 8;
11697   inst.instruction |= Rn << 16;
11698   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11699   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11700   inst.instruction |= msb - 1;
11701 }
11702
11703 static void
11704 do_t_bfx (void)
11705 {
11706   unsigned Rd, Rn;
11707
11708   Rd = inst.operands[0].reg;
11709   Rn = inst.operands[1].reg;
11710
11711   reject_bad_reg (Rd);
11712   reject_bad_reg (Rn);
11713
11714   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11715               _("bit-field extends past end of register"));
11716   inst.instruction |= Rd << 8;
11717   inst.instruction |= Rn << 16;
11718   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11719   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11720   inst.instruction |= inst.operands[3].imm - 1;
11721 }
11722
11723 /* ARM V5 Thumb BLX (argument parse)
11724         BLX <target_addr>       which is BLX(1)
11725         BLX <Rm>                which is BLX(2)
11726    Unfortunately, there are two different opcodes for this mnemonic.
11727    So, the insns[].value is not used, and the code here zaps values
11728         into inst.instruction.
11729
11730    ??? How to take advantage of the additional two bits of displacement
11731    available in Thumb32 mode?  Need new relocation?  */
11732
11733 static void
11734 do_t_blx (void)
11735 {
11736   set_pred_insn_type_last ();
11737
11738   if (inst.operands[0].isreg)
11739     {
11740       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11741       /* We have a register, so this is BLX(2).  */
11742       inst.instruction |= inst.operands[0].reg << 3;
11743     }
11744   else
11745     {
11746       /* No register.  This must be BLX(1).  */
11747       inst.instruction = 0xf000e800;
11748       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11749     }
11750 }
11751
11752 static void
11753 do_t_branch (void)
11754 {
11755   int opcode;
11756   int cond;
11757   bfd_reloc_code_real_type reloc;
11758
11759   cond = inst.cond;
11760   set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);
11761
11762   if (in_pred_block ())
11763     {
11764       /* Conditional branches inside IT blocks are encoded as unconditional
11765          branches.  */
11766       cond = COND_ALWAYS;
11767     }
11768   else
11769     cond = inst.cond;
11770
11771   if (cond != COND_ALWAYS)
11772     opcode = T_MNEM_bcond;
11773   else
11774     opcode = inst.instruction;
11775
11776   if (unified_syntax
11777       && (inst.size_req == 4
11778           || (inst.size_req != 2
11779               && (inst.operands[0].hasreloc
11780                   || inst.relocs[0].exp.X_op == O_constant))))
11781     {
11782       inst.instruction = THUMB_OP32(opcode);
11783       if (cond == COND_ALWAYS)
11784         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11785       else
11786         {
11787           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11788                       _("selected architecture does not support "
11789                         "wide conditional branch instruction"));
11790
11791           gas_assert (cond != 0xF);
11792           inst.instruction |= cond << 22;
11793           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
11794         }
11795     }
11796   else
11797     {
11798       inst.instruction = THUMB_OP16(opcode);
11799       if (cond == COND_ALWAYS)
11800         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
11801       else
11802         {
11803           inst.instruction |= cond << 8;
11804           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
11805         }
11806       /* Allow section relaxation.  */
11807       if (unified_syntax && inst.size_req != 2)
11808         inst.relax = opcode;
11809     }
11810   inst.relocs[0].type = reloc;
11811   inst.relocs[0].pc_rel = 1;
11812 }
11813
11814 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
11815    between the two is the maximum immediate allowed - which is passed in
11816    RANGE.  */
11817 static void
11818 do_t_bkpt_hlt1 (int range)
11819 {
11820   constraint (inst.cond != COND_ALWAYS,
11821               _("instruction is always unconditional"));
11822   if (inst.operands[0].present)
11823     {
11824       constraint (inst.operands[0].imm > range,
11825                   _("immediate value out of range"));
11826       inst.instruction |= inst.operands[0].imm;
11827     }
11828
11829   set_pred_insn_type (NEUTRAL_IT_INSN);
11830 }
11831
11832 static void
11833 do_t_hlt (void)
11834 {
11835   do_t_bkpt_hlt1 (63);
11836 }
11837
11838 static void
11839 do_t_bkpt (void)
11840 {
11841   do_t_bkpt_hlt1 (255);
11842 }
11843
11844 static void
11845 do_t_branch23 (void)
11846 {
11847   set_pred_insn_type_last ();
11848   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
11849
11850   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
11851      this file.  We used to simply ignore the PLT reloc type here --
11852      the branch encoding is now needed to deal with TLSCALL relocs.
11853      So if we see a PLT reloc now, put it back to how it used to be to
11854      keep the preexisting behaviour.  */
11855   if (inst.relocs[0].type == BFD_RELOC_ARM_PLT32)
11856     inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH23;
11857
11858 #if defined(OBJ_COFF)
11859   /* If the destination of the branch is a defined symbol which does not have
11860      the THUMB_FUNC attribute, then we must be calling a function which has
11861      the (interfacearm) attribute.  We look for the Thumb entry point to that
11862      function and change the branch to refer to that function instead.  */
11863   if (   inst.relocs[0].exp.X_op == O_symbol
11864       && inst.relocs[0].exp.X_add_symbol != NULL
11865       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11866       && ! THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11867     inst.relocs[0].exp.X_add_symbol
11868       = find_real_start (inst.relocs[0].exp.X_add_symbol);
11869 #endif
11870 }
11871
11872 static void
11873 do_t_bx (void)
11874 {
11875   set_pred_insn_type_last ();
11876   inst.instruction |= inst.operands[0].reg << 3;
11877   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11878      should cause the alignment to be checked once it is known.  This is
11879      because BX PC only works if the instruction is word aligned.  */
11880 }
11881
11882 static void
11883 do_t_bxj (void)
11884 {
11885   int Rm;
11886
11887   set_pred_insn_type_last ();
11888   Rm = inst.operands[0].reg;
11889   reject_bad_reg (Rm);
11890   inst.instruction |= Rm << 16;
11891 }
11892
11893 static void
11894 do_t_clz (void)
11895 {
11896   unsigned Rd;
11897   unsigned Rm;
11898
11899   Rd = inst.operands[0].reg;
11900   Rm = inst.operands[1].reg;
11901
11902   reject_bad_reg (Rd);
11903   reject_bad_reg (Rm);
11904
11905   inst.instruction |= Rd << 8;
11906   inst.instruction |= Rm << 16;
11907   inst.instruction |= Rm;
11908 }
11909
11910 static void
11911 do_t_csdb (void)
11912 {
11913   set_pred_insn_type (OUTSIDE_PRED_INSN);
11914 }
11915
11916 static void
11917 do_t_cps (void)
11918 {
11919   set_pred_insn_type (OUTSIDE_PRED_INSN);
11920   inst.instruction |= inst.operands[0].imm;
11921 }
11922
11923 static void
11924 do_t_cpsi (void)
11925 {
11926   set_pred_insn_type (OUTSIDE_PRED_INSN);
11927   if (unified_syntax
11928       && (inst.operands[1].present || inst.size_req == 4)
11929       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11930     {
11931       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11932       inst.instruction = 0xf3af8000;
11933       inst.instruction |= imod << 9;
11934       inst.instruction |= inst.operands[0].imm << 5;
11935       if (inst.operands[1].present)
11936         inst.instruction |= 0x100 | inst.operands[1].imm;
11937     }
11938   else
11939     {
11940       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11941                   && (inst.operands[0].imm & 4),
11942                   _("selected processor does not support 'A' form "
11943                     "of this instruction"));
11944       constraint (inst.operands[1].present || inst.size_req == 4,
11945                   _("Thumb does not support the 2-argument "
11946                     "form of this instruction"));
11947       inst.instruction |= inst.operands[0].imm;
11948     }
11949 }
11950
11951 /* THUMB CPY instruction (argument parse).  */
11952
11953 static void
11954 do_t_cpy (void)
11955 {
11956   if (inst.size_req == 4)
11957     {
11958       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11959       inst.instruction |= inst.operands[0].reg << 8;
11960       inst.instruction |= inst.operands[1].reg;
11961     }
11962   else
11963     {
11964       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11965       inst.instruction |= (inst.operands[0].reg & 0x7);
11966       inst.instruction |= inst.operands[1].reg << 3;
11967     }
11968 }
11969
11970 static void
11971 do_t_cbz (void)
11972 {
11973   set_pred_insn_type (OUTSIDE_PRED_INSN);
11974   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11975   inst.instruction |= inst.operands[0].reg;
11976   inst.relocs[0].pc_rel = 1;
11977   inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11978 }
11979
11980 static void
11981 do_t_dbg (void)
11982 {
11983   inst.instruction |= inst.operands[0].imm;
11984 }
11985
11986 static void
11987 do_t_div (void)
11988 {
11989   unsigned Rd, Rn, Rm;
11990
11991   Rd = inst.operands[0].reg;
11992   Rn = (inst.operands[1].present
11993         ? inst.operands[1].reg : Rd);
11994   Rm = inst.operands[2].reg;
11995
11996   reject_bad_reg (Rd);
11997   reject_bad_reg (Rn);
11998   reject_bad_reg (Rm);
11999
12000   inst.instruction |= Rd << 8;
12001   inst.instruction |= Rn << 16;
12002   inst.instruction |= Rm;
12003 }
12004
12005 static void
12006 do_t_hint (void)
12007 {
12008   if (unified_syntax && inst.size_req == 4)
12009     inst.instruction = THUMB_OP32 (inst.instruction);
12010   else
12011     inst.instruction = THUMB_OP16 (inst.instruction);
12012 }
12013
12014 static void
12015 do_t_it (void)
12016 {
12017   unsigned int cond = inst.operands[0].imm;
12018
12019   set_pred_insn_type (IT_INSN);
12020   now_pred.mask = (inst.instruction & 0xf) | 0x10;
12021   now_pred.cc = cond;
12022   now_pred.warn_deprecated = FALSE;
12023   now_pred.type = SCALAR_PRED;
12024
12025   /* If the condition is a negative condition, invert the mask.  */
12026   if ((cond & 0x1) == 0x0)
12027     {
12028       unsigned int mask = inst.instruction & 0x000f;
12029
12030       if ((mask & 0x7) == 0)
12031         {
12032           /* No conversion needed.  */
12033           now_pred.block_length = 1;
12034         }
12035       else if ((mask & 0x3) == 0)
12036         {
12037           mask ^= 0x8;
12038           now_pred.block_length = 2;
12039         }
12040       else if ((mask & 0x1) == 0)
12041         {
12042           mask ^= 0xC;
12043           now_pred.block_length = 3;
12044         }
12045       else
12046         {
12047           mask ^= 0xE;
12048           now_pred.block_length = 4;
12049         }
12050
12051       inst.instruction &= 0xfff0;
12052       inst.instruction |= mask;
12053     }
12054
12055   inst.instruction |= cond << 4;
12056 }
12057
12058 /* Helper function used for both push/pop and ldm/stm.  */
12059 static void
12060 encode_thumb2_multi (bfd_boolean do_io, int base, unsigned mask,
12061                      bfd_boolean writeback)
12062 {
12063   bfd_boolean load, store;
12064
12065   gas_assert (base != -1 || !do_io);
12066   load = do_io && ((inst.instruction & (1 << 20)) != 0);
12067   store = do_io && !load;
12068
12069   if (mask & (1 << 13))
12070     inst.error =  _("SP not allowed in register list");
12071
12072   if (do_io && (mask & (1 << base)) != 0
12073       && writeback)
12074     inst.error = _("having the base register in the register list when "
12075                    "using write back is UNPREDICTABLE");
12076
12077   if (load)
12078     {
12079       if (mask & (1 << 15))
12080         {
12081           if (mask & (1 << 14))
12082             inst.error = _("LR and PC should not both be in register list");
12083           else
12084             set_pred_insn_type_last ();
12085         }
12086     }
12087   else if (store)
12088     {
12089       if (mask & (1 << 15))
12090         inst.error = _("PC not allowed in register list");
12091     }
12092
12093   if (do_io && ((mask & (mask - 1)) == 0))
12094     {
12095       /* Single register transfers implemented as str/ldr.  */
12096       if (writeback)
12097         {
12098           if (inst.instruction & (1 << 23))
12099             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
12100           else
12101             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
12102         }
12103       else
12104         {
12105           if (inst.instruction & (1 << 23))
12106             inst.instruction = 0x00800000; /* ia -> [base] */
12107           else
12108             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
12109         }
12110
12111       inst.instruction |= 0xf8400000;
12112       if (load)
12113         inst.instruction |= 0x00100000;
12114
12115       mask = ffs (mask) - 1;
12116       mask <<= 12;
12117     }
12118   else if (writeback)
12119     inst.instruction |= WRITE_BACK;
12120
12121   inst.instruction |= mask;
12122   if (do_io)
12123     inst.instruction |= base << 16;
12124 }
12125
12126 static void
12127 do_t_ldmstm (void)
12128 {
12129   /* This really doesn't seem worth it.  */
12130   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
12131               _("expression too complex"));
12132   constraint (inst.operands[1].writeback,
12133               _("Thumb load/store multiple does not support {reglist}^"));
12134
12135   if (unified_syntax)
12136     {
12137       bfd_boolean narrow;
12138       unsigned mask;
12139
12140       narrow = FALSE;
12141       /* See if we can use a 16-bit instruction.  */
12142       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
12143           && inst.size_req != 4
12144           && !(inst.operands[1].imm & ~0xff))
12145         {
12146           mask = 1 << inst.operands[0].reg;
12147
12148           if (inst.operands[0].reg <= 7)
12149             {
12150               if (inst.instruction == T_MNEM_stmia
12151                   ? inst.operands[0].writeback
12152                   : (inst.operands[0].writeback
12153                      == !(inst.operands[1].imm & mask)))
12154                 {
12155                   if (inst.instruction == T_MNEM_stmia
12156                       && (inst.operands[1].imm & mask)
12157                       && (inst.operands[1].imm & (mask - 1)))
12158                     as_warn (_("value stored for r%d is UNKNOWN"),
12159                              inst.operands[0].reg);
12160
12161                   inst.instruction = THUMB_OP16 (inst.instruction);
12162                   inst.instruction |= inst.operands[0].reg << 8;
12163                   inst.instruction |= inst.operands[1].imm;
12164                   narrow = TRUE;
12165                 }
12166               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12167                 {
12168                   /* This means 1 register in reg list one of 3 situations:
12169                      1. Instruction is stmia, but without writeback.
12170                      2. lmdia without writeback, but with Rn not in
12171                         reglist.
12172                      3. ldmia with writeback, but with Rn in reglist.
12173                      Case 3 is UNPREDICTABLE behaviour, so we handle
12174                      case 1 and 2 which can be converted into a 16-bit
12175                      str or ldr. The SP cases are handled below.  */
12176                   unsigned long opcode;
12177                   /* First, record an error for Case 3.  */
12178                   if (inst.operands[1].imm & mask
12179                       && inst.operands[0].writeback)
12180                     inst.error =
12181                         _("having the base register in the register list when "
12182                           "using write back is UNPREDICTABLE");
12183
12184                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
12185                                                              : T_MNEM_ldr);
12186                   inst.instruction = THUMB_OP16 (opcode);
12187                   inst.instruction |= inst.operands[0].reg << 3;
12188                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
12189                   narrow = TRUE;
12190                 }
12191             }
12192           else if (inst.operands[0] .reg == REG_SP)
12193             {
12194               if (inst.operands[0].writeback)
12195                 {
12196                   inst.instruction =
12197                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12198                                     ? T_MNEM_push : T_MNEM_pop);
12199                   inst.instruction |= inst.operands[1].imm;
12200                   narrow = TRUE;
12201                 }
12202               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
12203                 {
12204                   inst.instruction =
12205                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
12206                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
12207                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
12208                   narrow = TRUE;
12209                 }
12210             }
12211         }
12212
12213       if (!narrow)
12214         {
12215           if (inst.instruction < 0xffff)
12216             inst.instruction = THUMB_OP32 (inst.instruction);
12217
12218           encode_thumb2_multi (TRUE /* do_io */, inst.operands[0].reg,
12219                                inst.operands[1].imm,
12220                                inst.operands[0].writeback);
12221         }
12222     }
12223   else
12224     {
12225       constraint (inst.operands[0].reg > 7
12226                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
12227       constraint (inst.instruction != T_MNEM_ldmia
12228                   && inst.instruction != T_MNEM_stmia,
12229                   _("Thumb-2 instruction only valid in unified syntax"));
12230       if (inst.instruction == T_MNEM_stmia)
12231         {
12232           if (!inst.operands[0].writeback)
12233             as_warn (_("this instruction will write back the base register"));
12234           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
12235               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
12236             as_warn (_("value stored for r%d is UNKNOWN"),
12237                      inst.operands[0].reg);
12238         }
12239       else
12240         {
12241           if (!inst.operands[0].writeback
12242               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
12243             as_warn (_("this instruction will write back the base register"));
12244           else if (inst.operands[0].writeback
12245                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
12246             as_warn (_("this instruction will not write back the base register"));
12247         }
12248
12249       inst.instruction = THUMB_OP16 (inst.instruction);
12250       inst.instruction |= inst.operands[0].reg << 8;
12251       inst.instruction |= inst.operands[1].imm;
12252     }
12253 }
12254
12255 static void
12256 do_t_ldrex (void)
12257 {
12258   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
12259               || inst.operands[1].postind || inst.operands[1].writeback
12260               || inst.operands[1].immisreg || inst.operands[1].shifted
12261               || inst.operands[1].negative,
12262               BAD_ADDR_MODE);
12263
12264   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
12265
12266   inst.instruction |= inst.operands[0].reg << 12;
12267   inst.instruction |= inst.operands[1].reg << 16;
12268   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
12269 }
12270
12271 static void
12272 do_t_ldrexd (void)
12273 {
12274   if (!inst.operands[1].present)
12275     {
12276       constraint (inst.operands[0].reg == REG_LR,
12277                   _("r14 not allowed as first register "
12278                     "when second register is omitted"));
12279       inst.operands[1].reg = inst.operands[0].reg + 1;
12280     }
12281   constraint (inst.operands[0].reg == inst.operands[1].reg,
12282               BAD_OVERLAP);
12283
12284   inst.instruction |= inst.operands[0].reg << 12;
12285   inst.instruction |= inst.operands[1].reg << 8;
12286   inst.instruction |= inst.operands[2].reg << 16;
12287 }
12288
12289 static void
12290 do_t_ldst (void)
12291 {
12292   unsigned long opcode;
12293   int Rn;
12294
12295   if (inst.operands[0].isreg
12296       && !inst.operands[0].preind
12297       && inst.operands[0].reg == REG_PC)
12298     set_pred_insn_type_last ();
12299
12300   opcode = inst.instruction;
12301   if (unified_syntax)
12302     {
12303       if (!inst.operands[1].isreg)
12304         {
12305           if (opcode <= 0xffff)
12306             inst.instruction = THUMB_OP32 (opcode);
12307           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12308             return;
12309         }
12310       if (inst.operands[1].isreg
12311           && !inst.operands[1].writeback
12312           && !inst.operands[1].shifted && !inst.operands[1].postind
12313           && !inst.operands[1].negative && inst.operands[0].reg <= 7
12314           && opcode <= 0xffff
12315           && inst.size_req != 4)
12316         {
12317           /* Insn may have a 16-bit form.  */
12318           Rn = inst.operands[1].reg;
12319           if (inst.operands[1].immisreg)
12320             {
12321               inst.instruction = THUMB_OP16 (opcode);
12322               /* [Rn, Rik] */
12323               if (Rn <= 7 && inst.operands[1].imm <= 7)
12324                 goto op16;
12325               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
12326                 reject_bad_reg (inst.operands[1].imm);
12327             }
12328           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
12329                     && opcode != T_MNEM_ldrsb)
12330                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
12331                    || (Rn == REG_SP && opcode == T_MNEM_str))
12332             {
12333               /* [Rn, #const] */
12334               if (Rn > 7)
12335                 {
12336                   if (Rn == REG_PC)
12337                     {
12338                       if (inst.relocs[0].pc_rel)
12339                         opcode = T_MNEM_ldr_pc2;
12340                       else
12341                         opcode = T_MNEM_ldr_pc;
12342                     }
12343                   else
12344                     {
12345                       if (opcode == T_MNEM_ldr)
12346                         opcode = T_MNEM_ldr_sp;
12347                       else
12348                         opcode = T_MNEM_str_sp;
12349                     }
12350                   inst.instruction = inst.operands[0].reg << 8;
12351                 }
12352               else
12353                 {
12354                   inst.instruction = inst.operands[0].reg;
12355                   inst.instruction |= inst.operands[1].reg << 3;
12356                 }
12357               inst.instruction |= THUMB_OP16 (opcode);
12358               if (inst.size_req == 2)
12359                 inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12360               else
12361                 inst.relax = opcode;
12362               return;
12363             }
12364         }
12365       /* Definitely a 32-bit variant.  */
12366
12367       /* Warning for Erratum 752419.  */
12368       if (opcode == T_MNEM_ldr
12369           && inst.operands[0].reg == REG_SP
12370           && inst.operands[1].writeback == 1
12371           && !inst.operands[1].immisreg)
12372         {
12373           if (no_cpu_selected ()
12374               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
12375                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
12376                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
12377             as_warn (_("This instruction may be unpredictable "
12378                        "if executed on M-profile cores "
12379                        "with interrupts enabled."));
12380         }
12381
12382       /* Do some validations regarding addressing modes.  */
12383       if (inst.operands[1].immisreg)
12384         reject_bad_reg (inst.operands[1].imm);
12385
12386       constraint (inst.operands[1].writeback == 1
12387                   && inst.operands[0].reg == inst.operands[1].reg,
12388                   BAD_OVERLAP);
12389
12390       inst.instruction = THUMB_OP32 (opcode);
12391       inst.instruction |= inst.operands[0].reg << 12;
12392       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
12393       check_ldr_r15_aligned ();
12394       return;
12395     }
12396
12397   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12398
12399   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
12400     {
12401       /* Only [Rn,Rm] is acceptable.  */
12402       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
12403       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
12404                   || inst.operands[1].postind || inst.operands[1].shifted
12405                   || inst.operands[1].negative,
12406                   _("Thumb does not support this addressing mode"));
12407       inst.instruction = THUMB_OP16 (inst.instruction);
12408       goto op16;
12409     }
12410
12411   inst.instruction = THUMB_OP16 (inst.instruction);
12412   if (!inst.operands[1].isreg)
12413     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12414       return;
12415
12416   constraint (!inst.operands[1].preind
12417               || inst.operands[1].shifted
12418               || inst.operands[1].writeback,
12419               _("Thumb does not support this addressing mode"));
12420   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
12421     {
12422       constraint (inst.instruction & 0x0600,
12423                   _("byte or halfword not valid for base register"));
12424       constraint (inst.operands[1].reg == REG_PC
12425                   && !(inst.instruction & THUMB_LOAD_BIT),
12426                   _("r15 based store not allowed"));
12427       constraint (inst.operands[1].immisreg,
12428                   _("invalid base register for register offset"));
12429
12430       if (inst.operands[1].reg == REG_PC)
12431         inst.instruction = T_OPCODE_LDR_PC;
12432       else if (inst.instruction & THUMB_LOAD_BIT)
12433         inst.instruction = T_OPCODE_LDR_SP;
12434       else
12435         inst.instruction = T_OPCODE_STR_SP;
12436
12437       inst.instruction |= inst.operands[0].reg << 8;
12438       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12439       return;
12440     }
12441
12442   constraint (inst.operands[1].reg > 7, BAD_HIREG);
12443   if (!inst.operands[1].immisreg)
12444     {
12445       /* Immediate offset.  */
12446       inst.instruction |= inst.operands[0].reg;
12447       inst.instruction |= inst.operands[1].reg << 3;
12448       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12449       return;
12450     }
12451
12452   /* Register offset.  */
12453   constraint (inst.operands[1].imm > 7, BAD_HIREG);
12454   constraint (inst.operands[1].negative,
12455               _("Thumb does not support this addressing mode"));
12456
12457  op16:
12458   switch (inst.instruction)
12459     {
12460     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
12461     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
12462     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
12463     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
12464     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
12465     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
12466     case 0x5600 /* ldrsb */:
12467     case 0x5e00 /* ldrsh */: break;
12468     default: abort ();
12469     }
12470
12471   inst.instruction |= inst.operands[0].reg;
12472   inst.instruction |= inst.operands[1].reg << 3;
12473   inst.instruction |= inst.operands[1].imm << 6;
12474 }
12475
12476 static void
12477 do_t_ldstd (void)
12478 {
12479   if (!inst.operands[1].present)
12480     {
12481       inst.operands[1].reg = inst.operands[0].reg + 1;
12482       constraint (inst.operands[0].reg == REG_LR,
12483                   _("r14 not allowed here"));
12484       constraint (inst.operands[0].reg == REG_R12,
12485                   _("r12 not allowed here"));
12486     }
12487
12488   if (inst.operands[2].writeback
12489       && (inst.operands[0].reg == inst.operands[2].reg
12490       || inst.operands[1].reg == inst.operands[2].reg))
12491     as_warn (_("base register written back, and overlaps "
12492                "one of transfer registers"));
12493
12494   inst.instruction |= inst.operands[0].reg << 12;
12495   inst.instruction |= inst.operands[1].reg << 8;
12496   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
12497 }
12498
12499 static void
12500 do_t_ldstt (void)
12501 {
12502   inst.instruction |= inst.operands[0].reg << 12;
12503   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
12504 }
12505
12506 static void
12507 do_t_mla (void)
12508 {
12509   unsigned Rd, Rn, Rm, Ra;
12510
12511   Rd = inst.operands[0].reg;
12512   Rn = inst.operands[1].reg;
12513   Rm = inst.operands[2].reg;
12514   Ra = inst.operands[3].reg;
12515
12516   reject_bad_reg (Rd);
12517   reject_bad_reg (Rn);
12518   reject_bad_reg (Rm);
12519   reject_bad_reg (Ra);
12520
12521   inst.instruction |= Rd << 8;
12522   inst.instruction |= Rn << 16;
12523   inst.instruction |= Rm;
12524   inst.instruction |= Ra << 12;
12525 }
12526
12527 static void
12528 do_t_mlal (void)
12529 {
12530   unsigned RdLo, RdHi, Rn, Rm;
12531
12532   RdLo = inst.operands[0].reg;
12533   RdHi = inst.operands[1].reg;
12534   Rn = inst.operands[2].reg;
12535   Rm = inst.operands[3].reg;
12536
12537   reject_bad_reg (RdLo);
12538   reject_bad_reg (RdHi);
12539   reject_bad_reg (Rn);
12540   reject_bad_reg (Rm);
12541
12542   inst.instruction |= RdLo << 12;
12543   inst.instruction |= RdHi << 8;
12544   inst.instruction |= Rn << 16;
12545   inst.instruction |= Rm;
12546 }
12547
12548 static void
12549 do_t_mov_cmp (void)
12550 {
12551   unsigned Rn, Rm;
12552
12553   Rn = inst.operands[0].reg;
12554   Rm = inst.operands[1].reg;
12555
12556   if (Rn == REG_PC)
12557     set_pred_insn_type_last ();
12558
12559   if (unified_syntax)
12560     {
12561       int r0off = (inst.instruction == T_MNEM_mov
12562                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
12563       unsigned long opcode;
12564       bfd_boolean narrow;
12565       bfd_boolean low_regs;
12566
12567       low_regs = (Rn <= 7 && Rm <= 7);
12568       opcode = inst.instruction;
12569       if (in_pred_block ())
12570         narrow = opcode != T_MNEM_movs;
12571       else
12572         narrow = opcode != T_MNEM_movs || low_regs;
12573       if (inst.size_req == 4
12574           || inst.operands[1].shifted)
12575         narrow = FALSE;
12576
12577       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
12578       if (opcode == T_MNEM_movs && inst.operands[1].isreg
12579           && !inst.operands[1].shifted
12580           && Rn == REG_PC
12581           && Rm == REG_LR)
12582         {
12583           inst.instruction = T2_SUBS_PC_LR;
12584           return;
12585         }
12586
12587       if (opcode == T_MNEM_cmp)
12588         {
12589           constraint (Rn == REG_PC, BAD_PC);
12590           if (narrow)
12591             {
12592               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
12593                  but valid.  */
12594               warn_deprecated_sp (Rm);
12595               /* R15 was documented as a valid choice for Rm in ARMv6,
12596                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
12597                  tools reject R15, so we do too.  */
12598               constraint (Rm == REG_PC, BAD_PC);
12599             }
12600           else
12601             reject_bad_reg (Rm);
12602         }
12603       else if (opcode == T_MNEM_mov
12604                || opcode == T_MNEM_movs)
12605         {
12606           if (inst.operands[1].isreg)
12607             {
12608               if (opcode == T_MNEM_movs)
12609                 {
12610                   reject_bad_reg (Rn);
12611                   reject_bad_reg (Rm);
12612                 }
12613               else if (narrow)
12614                 {
12615                   /* This is mov.n.  */
12616                   if ((Rn == REG_SP || Rn == REG_PC)
12617                       && (Rm == REG_SP || Rm == REG_PC))
12618                     {
12619                       as_tsktsk (_("Use of r%u as a source register is "
12620                                  "deprecated when r%u is the destination "
12621                                  "register."), Rm, Rn);
12622                     }
12623                 }
12624               else
12625                 {
12626                   /* This is mov.w.  */
12627                   constraint (Rn == REG_PC, BAD_PC);
12628                   constraint (Rm == REG_PC, BAD_PC);
12629                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12630                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
12631                 }
12632             }
12633           else
12634             reject_bad_reg (Rn);
12635         }
12636
12637       if (!inst.operands[1].isreg)
12638         {
12639           /* Immediate operand.  */
12640           if (!in_pred_block () && opcode == T_MNEM_mov)
12641             narrow = 0;
12642           if (low_regs && narrow)
12643             {
12644               inst.instruction = THUMB_OP16 (opcode);
12645               inst.instruction |= Rn << 8;
12646               if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12647                   || inst.relocs[0].type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12648                 {
12649                   if (inst.size_req == 2)
12650                     inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12651                   else
12652                     inst.relax = opcode;
12653                 }
12654             }
12655           else
12656             {
12657               constraint ((inst.relocs[0].type
12658                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
12659                           && (inst.relocs[0].type
12660                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
12661                           THUMB1_RELOC_ONLY);
12662
12663               inst.instruction = THUMB_OP32 (inst.instruction);
12664               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12665               inst.instruction |= Rn << r0off;
12666               inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12667             }
12668         }
12669       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12670                && (inst.instruction == T_MNEM_mov
12671                    || inst.instruction == T_MNEM_movs))
12672         {
12673           /* Register shifts are encoded as separate shift instructions.  */
12674           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12675
12676           if (in_pred_block ())
12677             narrow = !flags;
12678           else
12679             narrow = flags;
12680
12681           if (inst.size_req == 4)
12682             narrow = FALSE;
12683
12684           if (!low_regs || inst.operands[1].imm > 7)
12685             narrow = FALSE;
12686
12687           if (Rn != Rm)
12688             narrow = FALSE;
12689
12690           switch (inst.operands[1].shift_kind)
12691             {
12692             case SHIFT_LSL:
12693               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12694               break;
12695             case SHIFT_ASR:
12696               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12697               break;
12698             case SHIFT_LSR:
12699               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12700               break;
12701             case SHIFT_ROR:
12702               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12703               break;
12704             default:
12705               abort ();
12706             }
12707
12708           inst.instruction = opcode;
12709           if (narrow)
12710             {
12711               inst.instruction |= Rn;
12712               inst.instruction |= inst.operands[1].imm << 3;
12713             }
12714           else
12715             {
12716               if (flags)
12717                 inst.instruction |= CONDS_BIT;
12718
12719               inst.instruction |= Rn << 8;
12720               inst.instruction |= Rm << 16;
12721               inst.instruction |= inst.operands[1].imm;
12722             }
12723         }
12724       else if (!narrow)
12725         {
12726           /* Some mov with immediate shift have narrow variants.
12727              Register shifts are handled above.  */
12728           if (low_regs && inst.operands[1].shifted
12729               && (inst.instruction == T_MNEM_mov
12730                   || inst.instruction == T_MNEM_movs))
12731             {
12732               if (in_pred_block ())
12733                 narrow = (inst.instruction == T_MNEM_mov);
12734               else
12735                 narrow = (inst.instruction == T_MNEM_movs);
12736             }
12737
12738           if (narrow)
12739             {
12740               switch (inst.operands[1].shift_kind)
12741                 {
12742                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12743                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12744                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12745                 default: narrow = FALSE; break;
12746                 }
12747             }
12748
12749           if (narrow)
12750             {
12751               inst.instruction |= Rn;
12752               inst.instruction |= Rm << 3;
12753               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
12754             }
12755           else
12756             {
12757               inst.instruction = THUMB_OP32 (inst.instruction);
12758               inst.instruction |= Rn << r0off;
12759               encode_thumb32_shifted_operand (1);
12760             }
12761         }
12762       else
12763         switch (inst.instruction)
12764           {
12765           case T_MNEM_mov:
12766             /* In v4t or v5t a move of two lowregs produces unpredictable
12767                results. Don't allow this.  */
12768             if (low_regs)
12769               {
12770                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
12771                             "MOV Rd, Rs with two low registers is not "
12772                             "permitted on this architecture");
12773                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
12774                                         arm_ext_v6);
12775               }
12776
12777             inst.instruction = T_OPCODE_MOV_HR;
12778             inst.instruction |= (Rn & 0x8) << 4;
12779             inst.instruction |= (Rn & 0x7);
12780             inst.instruction |= Rm << 3;
12781             break;
12782
12783           case T_MNEM_movs:
12784             /* We know we have low registers at this point.
12785                Generate LSLS Rd, Rs, #0.  */
12786             inst.instruction = T_OPCODE_LSL_I;
12787             inst.instruction |= Rn;
12788             inst.instruction |= Rm << 3;
12789             break;
12790
12791           case T_MNEM_cmp:
12792             if (low_regs)
12793               {
12794                 inst.instruction = T_OPCODE_CMP_LR;
12795                 inst.instruction |= Rn;
12796                 inst.instruction |= Rm << 3;
12797               }
12798             else
12799               {
12800                 inst.instruction = T_OPCODE_CMP_HR;
12801                 inst.instruction |= (Rn & 0x8) << 4;
12802                 inst.instruction |= (Rn & 0x7);
12803                 inst.instruction |= Rm << 3;
12804               }
12805             break;
12806           }
12807       return;
12808     }
12809
12810   inst.instruction = THUMB_OP16 (inst.instruction);
12811
12812   /* PR 10443: Do not silently ignore shifted operands.  */
12813   constraint (inst.operands[1].shifted,
12814               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
12815
12816   if (inst.operands[1].isreg)
12817     {
12818       if (Rn < 8 && Rm < 8)
12819         {
12820           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
12821              since a MOV instruction produces unpredictable results.  */
12822           if (inst.instruction == T_OPCODE_MOV_I8)
12823             inst.instruction = T_OPCODE_ADD_I3;
12824           else
12825             inst.instruction = T_OPCODE_CMP_LR;
12826
12827           inst.instruction |= Rn;
12828           inst.instruction |= Rm << 3;
12829         }
12830       else
12831         {
12832           if (inst.instruction == T_OPCODE_MOV_I8)
12833             inst.instruction = T_OPCODE_MOV_HR;
12834           else
12835             inst.instruction = T_OPCODE_CMP_HR;
12836           do_t_cpy ();
12837         }
12838     }
12839   else
12840     {
12841       constraint (Rn > 7,
12842                   _("only lo regs allowed with immediate"));
12843       inst.instruction |= Rn << 8;
12844       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12845     }
12846 }
12847
12848 static void
12849 do_t_mov16 (void)
12850 {
12851   unsigned Rd;
12852   bfd_vma imm;
12853   bfd_boolean top;
12854
12855   top = (inst.instruction & 0x00800000) != 0;
12856   if (inst.relocs[0].type == BFD_RELOC_ARM_MOVW)
12857     {
12858       constraint (top, _(":lower16: not allowed in this instruction"));
12859       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVW;
12860     }
12861   else if (inst.relocs[0].type == BFD_RELOC_ARM_MOVT)
12862     {
12863       constraint (!top, _(":upper16: not allowed in this instruction"));
12864       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVT;
12865     }
12866
12867   Rd = inst.operands[0].reg;
12868   reject_bad_reg (Rd);
12869
12870   inst.instruction |= Rd << 8;
12871   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
12872     {
12873       imm = inst.relocs[0].exp.X_add_number;
12874       inst.instruction |= (imm & 0xf000) << 4;
12875       inst.instruction |= (imm & 0x0800) << 15;
12876       inst.instruction |= (imm & 0x0700) << 4;
12877       inst.instruction |= (imm & 0x00ff);
12878     }
12879 }
12880
12881 static void
12882 do_t_mvn_tst (void)
12883 {
12884   unsigned Rn, Rm;
12885
12886   Rn = inst.operands[0].reg;
12887   Rm = inst.operands[1].reg;
12888
12889   if (inst.instruction == T_MNEM_cmp
12890       || inst.instruction == T_MNEM_cmn)
12891     constraint (Rn == REG_PC, BAD_PC);
12892   else
12893     reject_bad_reg (Rn);
12894   reject_bad_reg (Rm);
12895
12896   if (unified_syntax)
12897     {
12898       int r0off = (inst.instruction == T_MNEM_mvn
12899                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12900       bfd_boolean narrow;
12901
12902       if (inst.size_req == 4
12903           || inst.instruction > 0xffff
12904           || inst.operands[1].shifted
12905           || Rn > 7 || Rm > 7)
12906         narrow = FALSE;
12907       else if (inst.instruction == T_MNEM_cmn
12908                || inst.instruction == T_MNEM_tst)
12909         narrow = TRUE;
12910       else if (THUMB_SETS_FLAGS (inst.instruction))
12911         narrow = !in_pred_block ();
12912       else
12913         narrow = in_pred_block ();
12914
12915       if (!inst.operands[1].isreg)
12916         {
12917           /* For an immediate, we always generate a 32-bit opcode;
12918              section relaxation will shrink it later if possible.  */
12919           if (inst.instruction < 0xffff)
12920             inst.instruction = THUMB_OP32 (inst.instruction);
12921           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12922           inst.instruction |= Rn << r0off;
12923           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12924         }
12925       else
12926         {
12927           /* See if we can do this with a 16-bit instruction.  */
12928           if (narrow)
12929             {
12930               inst.instruction = THUMB_OP16 (inst.instruction);
12931               inst.instruction |= Rn;
12932               inst.instruction |= Rm << 3;
12933             }
12934           else
12935             {
12936               constraint (inst.operands[1].shifted
12937                           && inst.operands[1].immisreg,
12938                           _("shift must be constant"));
12939               if (inst.instruction < 0xffff)
12940                 inst.instruction = THUMB_OP32 (inst.instruction);
12941               inst.instruction |= Rn << r0off;
12942               encode_thumb32_shifted_operand (1);
12943             }
12944         }
12945     }
12946   else
12947     {
12948       constraint (inst.instruction > 0xffff
12949                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12950       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12951                   _("unshifted register required"));
12952       constraint (Rn > 7 || Rm > 7,
12953                   BAD_HIREG);
12954
12955       inst.instruction = THUMB_OP16 (inst.instruction);
12956       inst.instruction |= Rn;
12957       inst.instruction |= Rm << 3;
12958     }
12959 }
12960
12961 static void
12962 do_t_mrs (void)
12963 {
12964   unsigned Rd;
12965
12966   if (do_vfp_nsyn_mrs () == SUCCESS)
12967     return;
12968
12969   Rd = inst.operands[0].reg;
12970   reject_bad_reg (Rd);
12971   inst.instruction |= Rd << 8;
12972
12973   if (inst.operands[1].isreg)
12974     {
12975       unsigned br = inst.operands[1].reg;
12976       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12977         as_bad (_("bad register for mrs"));
12978
12979       inst.instruction |= br & (0xf << 16);
12980       inst.instruction |= (br & 0x300) >> 4;
12981       inst.instruction |= (br & SPSR_BIT) >> 2;
12982     }
12983   else
12984     {
12985       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12986
12987       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12988         {
12989           /* PR gas/12698:  The constraint is only applied for m_profile.
12990              If the user has specified -march=all, we want to ignore it as
12991              we are building for any CPU type, including non-m variants.  */
12992           bfd_boolean m_profile =
12993             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12994           constraint ((flags != 0) && m_profile, _("selected processor does "
12995                                                    "not support requested special purpose register"));
12996         }
12997       else
12998         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12999            devices).  */
13000         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
13001                     _("'APSR', 'CPSR' or 'SPSR' expected"));
13002
13003       inst.instruction |= (flags & SPSR_BIT) >> 2;
13004       inst.instruction |= inst.operands[1].imm & 0xff;
13005       inst.instruction |= 0xf0000;
13006     }
13007 }
13008
13009 static void
13010 do_t_msr (void)
13011 {
13012   int flags;
13013   unsigned Rn;
13014
13015   if (do_vfp_nsyn_msr () == SUCCESS)
13016     return;
13017
13018   constraint (!inst.operands[1].isreg,
13019               _("Thumb encoding does not support an immediate here"));
13020
13021   if (inst.operands[0].isreg)
13022     flags = (int)(inst.operands[0].reg);
13023   else
13024     flags = inst.operands[0].imm;
13025
13026   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
13027     {
13028       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
13029
13030       /* PR gas/12698:  The constraint is only applied for m_profile.
13031          If the user has specified -march=all, we want to ignore it as
13032          we are building for any CPU type, including non-m variants.  */
13033       bfd_boolean m_profile =
13034         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
13035       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13036            && (bits & ~(PSR_s | PSR_f)) != 0)
13037           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
13038               && bits != PSR_f)) && m_profile,
13039           _("selected processor does not support requested special "
13040             "purpose register"));
13041     }
13042   else
13043      constraint ((flags & 0xff) != 0, _("selected processor does not support "
13044                  "requested special purpose register"));
13045
13046   Rn = inst.operands[1].reg;
13047   reject_bad_reg (Rn);
13048
13049   inst.instruction |= (flags & SPSR_BIT) >> 2;
13050   inst.instruction |= (flags & 0xf0000) >> 8;
13051   inst.instruction |= (flags & 0x300) >> 4;
13052   inst.instruction |= (flags & 0xff);
13053   inst.instruction |= Rn << 16;
13054 }
13055
13056 static void
13057 do_t_mul (void)
13058 {
13059   bfd_boolean narrow;
13060   unsigned Rd, Rn, Rm;
13061
13062   if (!inst.operands[2].present)
13063     inst.operands[2].reg = inst.operands[0].reg;
13064
13065   Rd = inst.operands[0].reg;
13066   Rn = inst.operands[1].reg;
13067   Rm = inst.operands[2].reg;
13068
13069   if (unified_syntax)
13070     {
13071       if (inst.size_req == 4
13072           || (Rd != Rn
13073               && Rd != Rm)
13074           || Rn > 7
13075           || Rm > 7)
13076         narrow = FALSE;
13077       else if (inst.instruction == T_MNEM_muls)
13078         narrow = !in_pred_block ();
13079       else
13080         narrow = in_pred_block ();
13081     }
13082   else
13083     {
13084       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
13085       constraint (Rn > 7 || Rm > 7,
13086                   BAD_HIREG);
13087       narrow = TRUE;
13088     }
13089
13090   if (narrow)
13091     {
13092       /* 16-bit MULS/Conditional MUL.  */
13093       inst.instruction = THUMB_OP16 (inst.instruction);
13094       inst.instruction |= Rd;
13095
13096       if (Rd == Rn)
13097         inst.instruction |= Rm << 3;
13098       else if (Rd == Rm)
13099         inst.instruction |= Rn << 3;
13100       else
13101         constraint (1, _("dest must overlap one source register"));
13102     }
13103   else
13104     {
13105       constraint (inst.instruction != T_MNEM_mul,
13106                   _("Thumb-2 MUL must not set flags"));
13107       /* 32-bit MUL.  */
13108       inst.instruction = THUMB_OP32 (inst.instruction);
13109       inst.instruction |= Rd << 8;
13110       inst.instruction |= Rn << 16;
13111       inst.instruction |= Rm << 0;
13112
13113       reject_bad_reg (Rd);
13114       reject_bad_reg (Rn);
13115       reject_bad_reg (Rm);
13116     }
13117 }
13118
13119 static void
13120 do_t_mull (void)
13121 {
13122   unsigned RdLo, RdHi, Rn, Rm;
13123
13124   RdLo = inst.operands[0].reg;
13125   RdHi = inst.operands[1].reg;
13126   Rn = inst.operands[2].reg;
13127   Rm = inst.operands[3].reg;
13128
13129   reject_bad_reg (RdLo);
13130   reject_bad_reg (RdHi);
13131   reject_bad_reg (Rn);
13132   reject_bad_reg (Rm);
13133
13134   inst.instruction |= RdLo << 12;
13135   inst.instruction |= RdHi << 8;
13136   inst.instruction |= Rn << 16;
13137   inst.instruction |= Rm;
13138
13139  if (RdLo == RdHi)
13140     as_tsktsk (_("rdhi and rdlo must be different"));
13141 }
13142
13143 static void
13144 do_t_nop (void)
13145 {
13146   set_pred_insn_type (NEUTRAL_IT_INSN);
13147
13148   if (unified_syntax)
13149     {
13150       if (inst.size_req == 4 || inst.operands[0].imm > 15)
13151         {
13152           inst.instruction = THUMB_OP32 (inst.instruction);
13153           inst.instruction |= inst.operands[0].imm;
13154         }
13155       else
13156         {
13157           /* PR9722: Check for Thumb2 availability before
13158              generating a thumb2 nop instruction.  */
13159           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
13160             {
13161               inst.instruction = THUMB_OP16 (inst.instruction);
13162               inst.instruction |= inst.operands[0].imm << 4;
13163             }
13164           else
13165             inst.instruction = 0x46c0;
13166         }
13167     }
13168   else
13169     {
13170       constraint (inst.operands[0].present,
13171                   _("Thumb does not support NOP with hints"));
13172       inst.instruction = 0x46c0;
13173     }
13174 }
13175
13176 static void
13177 do_t_neg (void)
13178 {
13179   if (unified_syntax)
13180     {
13181       bfd_boolean narrow;
13182
13183       if (THUMB_SETS_FLAGS (inst.instruction))
13184         narrow = !in_pred_block ();
13185       else
13186         narrow = in_pred_block ();
13187       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13188         narrow = FALSE;
13189       if (inst.size_req == 4)
13190         narrow = FALSE;
13191
13192       if (!narrow)
13193         {
13194           inst.instruction = THUMB_OP32 (inst.instruction);
13195           inst.instruction |= inst.operands[0].reg << 8;
13196           inst.instruction |= inst.operands[1].reg << 16;
13197         }
13198       else
13199         {
13200           inst.instruction = THUMB_OP16 (inst.instruction);
13201           inst.instruction |= inst.operands[0].reg;
13202           inst.instruction |= inst.operands[1].reg << 3;
13203         }
13204     }
13205   else
13206     {
13207       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
13208                   BAD_HIREG);
13209       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13210
13211       inst.instruction = THUMB_OP16 (inst.instruction);
13212       inst.instruction |= inst.operands[0].reg;
13213       inst.instruction |= inst.operands[1].reg << 3;
13214     }
13215 }
13216
13217 static void
13218 do_t_orn (void)
13219 {
13220   unsigned Rd, Rn;
13221
13222   Rd = inst.operands[0].reg;
13223   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
13224
13225   reject_bad_reg (Rd);
13226   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
13227   reject_bad_reg (Rn);
13228
13229   inst.instruction |= Rd << 8;
13230   inst.instruction |= Rn << 16;
13231
13232   if (!inst.operands[2].isreg)
13233     {
13234       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13235       inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13236     }
13237   else
13238     {
13239       unsigned Rm;
13240
13241       Rm = inst.operands[2].reg;
13242       reject_bad_reg (Rm);
13243
13244       constraint (inst.operands[2].shifted
13245                   && inst.operands[2].immisreg,
13246                   _("shift must be constant"));
13247       encode_thumb32_shifted_operand (2);
13248     }
13249 }
13250
13251 static void
13252 do_t_pkhbt (void)
13253 {
13254   unsigned Rd, Rn, Rm;
13255
13256   Rd = inst.operands[0].reg;
13257   Rn = inst.operands[1].reg;
13258   Rm = inst.operands[2].reg;
13259
13260   reject_bad_reg (Rd);
13261   reject_bad_reg (Rn);
13262   reject_bad_reg (Rm);
13263
13264   inst.instruction |= Rd << 8;
13265   inst.instruction |= Rn << 16;
13266   inst.instruction |= Rm;
13267   if (inst.operands[3].present)
13268     {
13269       unsigned int val = inst.relocs[0].exp.X_add_number;
13270       constraint (inst.relocs[0].exp.X_op != O_constant,
13271                   _("expression too complex"));
13272       inst.instruction |= (val & 0x1c) << 10;
13273       inst.instruction |= (val & 0x03) << 6;
13274     }
13275 }
13276
13277 static void
13278 do_t_pkhtb (void)
13279 {
13280   if (!inst.operands[3].present)
13281     {
13282       unsigned Rtmp;
13283
13284       inst.instruction &= ~0x00000020;
13285
13286       /* PR 10168.  Swap the Rm and Rn registers.  */
13287       Rtmp = inst.operands[1].reg;
13288       inst.operands[1].reg = inst.operands[2].reg;
13289       inst.operands[2].reg = Rtmp;
13290     }
13291   do_t_pkhbt ();
13292 }
13293
13294 static void
13295 do_t_pld (void)
13296 {
13297   if (inst.operands[0].immisreg)
13298     reject_bad_reg (inst.operands[0].imm);
13299
13300   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
13301 }
13302
13303 static void
13304 do_t_push_pop (void)
13305 {
13306   unsigned mask;
13307
13308   constraint (inst.operands[0].writeback,
13309               _("push/pop do not support {reglist}^"));
13310   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
13311               _("expression too complex"));
13312
13313   mask = inst.operands[0].imm;
13314   if (inst.size_req != 4 && (mask & ~0xff) == 0)
13315     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
13316   else if (inst.size_req != 4
13317            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
13318                                        ? REG_LR : REG_PC)))
13319     {
13320       inst.instruction = THUMB_OP16 (inst.instruction);
13321       inst.instruction |= THUMB_PP_PC_LR;
13322       inst.instruction |= mask & 0xff;
13323     }
13324   else if (unified_syntax)
13325     {
13326       inst.instruction = THUMB_OP32 (inst.instruction);
13327       encode_thumb2_multi (TRUE /* do_io */, 13, mask, TRUE);
13328     }
13329   else
13330     {
13331       inst.error = _("invalid register list to push/pop instruction");
13332       return;
13333     }
13334 }
13335
13336 static void
13337 do_t_clrm (void)
13338 {
13339   if (unified_syntax)
13340     encode_thumb2_multi (FALSE /* do_io */, -1, inst.operands[0].imm, FALSE);
13341   else
13342     {
13343       inst.error = _("invalid register list to push/pop instruction");
13344       return;
13345     }
13346 }
13347
13348 static void
13349 do_t_vscclrm (void)
13350 {
13351   if (inst.operands[0].issingle)
13352     {
13353       inst.instruction |= (inst.operands[0].reg & 0x1) << 22;
13354       inst.instruction |= (inst.operands[0].reg & 0x1e) << 11;
13355       inst.instruction |= inst.operands[0].imm;
13356     }
13357   else
13358     {
13359       inst.instruction |= (inst.operands[0].reg & 0x10) << 18;
13360       inst.instruction |= (inst.operands[0].reg & 0xf) << 12;
13361       inst.instruction |= 1 << 8;
13362       inst.instruction |= inst.operands[0].imm << 1;
13363     }
13364 }
13365
13366 static void
13367 do_t_rbit (void)
13368 {
13369   unsigned Rd, Rm;
13370
13371   Rd = inst.operands[0].reg;
13372   Rm = inst.operands[1].reg;
13373
13374   reject_bad_reg (Rd);
13375   reject_bad_reg (Rm);
13376
13377   inst.instruction |= Rd << 8;
13378   inst.instruction |= Rm << 16;
13379   inst.instruction |= Rm;
13380 }
13381
13382 static void
13383 do_t_rev (void)
13384 {
13385   unsigned Rd, Rm;
13386
13387   Rd = inst.operands[0].reg;
13388   Rm = inst.operands[1].reg;
13389
13390   reject_bad_reg (Rd);
13391   reject_bad_reg (Rm);
13392
13393   if (Rd <= 7 && Rm <= 7
13394       && inst.size_req != 4)
13395     {
13396       inst.instruction = THUMB_OP16 (inst.instruction);
13397       inst.instruction |= Rd;
13398       inst.instruction |= Rm << 3;
13399     }
13400   else if (unified_syntax)
13401     {
13402       inst.instruction = THUMB_OP32 (inst.instruction);
13403       inst.instruction |= Rd << 8;
13404       inst.instruction |= Rm << 16;
13405       inst.instruction |= Rm;
13406     }
13407   else
13408     inst.error = BAD_HIREG;
13409 }
13410
13411 static void
13412 do_t_rrx (void)
13413 {
13414   unsigned Rd, Rm;
13415
13416   Rd = inst.operands[0].reg;
13417   Rm = inst.operands[1].reg;
13418
13419   reject_bad_reg (Rd);
13420   reject_bad_reg (Rm);
13421
13422   inst.instruction |= Rd << 8;
13423   inst.instruction |= Rm;
13424 }
13425
13426 static void
13427 do_t_rsb (void)
13428 {
13429   unsigned Rd, Rs;
13430
13431   Rd = inst.operands[0].reg;
13432   Rs = (inst.operands[1].present
13433         ? inst.operands[1].reg    /* Rd, Rs, foo */
13434         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
13435
13436   reject_bad_reg (Rd);
13437   reject_bad_reg (Rs);
13438   if (inst.operands[2].isreg)
13439     reject_bad_reg (inst.operands[2].reg);
13440
13441   inst.instruction |= Rd << 8;
13442   inst.instruction |= Rs << 16;
13443   if (!inst.operands[2].isreg)
13444     {
13445       bfd_boolean narrow;
13446
13447       if ((inst.instruction & 0x00100000) != 0)
13448         narrow = !in_pred_block ();
13449       else
13450         narrow = in_pred_block ();
13451
13452       if (Rd > 7 || Rs > 7)
13453         narrow = FALSE;
13454
13455       if (inst.size_req == 4 || !unified_syntax)
13456         narrow = FALSE;
13457
13458       if (inst.relocs[0].exp.X_op != O_constant
13459           || inst.relocs[0].exp.X_add_number != 0)
13460         narrow = FALSE;
13461
13462       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
13463          relaxation, but it doesn't seem worth the hassle.  */
13464       if (narrow)
13465         {
13466           inst.relocs[0].type = BFD_RELOC_UNUSED;
13467           inst.instruction = THUMB_OP16 (T_MNEM_negs);
13468           inst.instruction |= Rs << 3;
13469           inst.instruction |= Rd;
13470         }
13471       else
13472         {
13473           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13474           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13475         }
13476     }
13477   else
13478     encode_thumb32_shifted_operand (2);
13479 }
13480
13481 static void
13482 do_t_setend (void)
13483 {
13484   if (warn_on_deprecated
13485       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13486       as_tsktsk (_("setend use is deprecated for ARMv8"));
13487
13488   set_pred_insn_type (OUTSIDE_PRED_INSN);
13489   if (inst.operands[0].imm)
13490     inst.instruction |= 0x8;
13491 }
13492
13493 static void
13494 do_t_shift (void)
13495 {
13496   if (!inst.operands[1].present)
13497     inst.operands[1].reg = inst.operands[0].reg;
13498
13499   if (unified_syntax)
13500     {
13501       bfd_boolean narrow;
13502       int shift_kind;
13503
13504       switch (inst.instruction)
13505         {
13506         case T_MNEM_asr:
13507         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
13508         case T_MNEM_lsl:
13509         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
13510         case T_MNEM_lsr:
13511         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
13512         case T_MNEM_ror:
13513         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
13514         default: abort ();
13515         }
13516
13517       if (THUMB_SETS_FLAGS (inst.instruction))
13518         narrow = !in_pred_block ();
13519       else
13520         narrow = in_pred_block ();
13521       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13522         narrow = FALSE;
13523       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
13524         narrow = FALSE;
13525       if (inst.operands[2].isreg
13526           && (inst.operands[1].reg != inst.operands[0].reg
13527               || inst.operands[2].reg > 7))
13528         narrow = FALSE;
13529       if (inst.size_req == 4)
13530         narrow = FALSE;
13531
13532       reject_bad_reg (inst.operands[0].reg);
13533       reject_bad_reg (inst.operands[1].reg);
13534
13535       if (!narrow)
13536         {
13537           if (inst.operands[2].isreg)
13538             {
13539               reject_bad_reg (inst.operands[2].reg);
13540               inst.instruction = THUMB_OP32 (inst.instruction);
13541               inst.instruction |= inst.operands[0].reg << 8;
13542               inst.instruction |= inst.operands[1].reg << 16;
13543               inst.instruction |= inst.operands[2].reg;
13544
13545               /* PR 12854: Error on extraneous shifts.  */
13546               constraint (inst.operands[2].shifted,
13547                           _("extraneous shift as part of operand to shift insn"));
13548             }
13549           else
13550             {
13551               inst.operands[1].shifted = 1;
13552               inst.operands[1].shift_kind = shift_kind;
13553               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
13554                                              ? T_MNEM_movs : T_MNEM_mov);
13555               inst.instruction |= inst.operands[0].reg << 8;
13556               encode_thumb32_shifted_operand (1);
13557               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
13558               inst.relocs[0].type = BFD_RELOC_UNUSED;
13559             }
13560         }
13561       else
13562         {
13563           if (inst.operands[2].isreg)
13564             {
13565               switch (shift_kind)
13566                 {
13567                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
13568                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
13569                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
13570                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
13571                 default: abort ();
13572                 }
13573
13574               inst.instruction |= inst.operands[0].reg;
13575               inst.instruction |= inst.operands[2].reg << 3;
13576
13577               /* PR 12854: Error on extraneous shifts.  */
13578               constraint (inst.operands[2].shifted,
13579                           _("extraneous shift as part of operand to shift insn"));
13580             }
13581           else
13582             {
13583               switch (shift_kind)
13584                 {
13585                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13586                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13587                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13588                 default: abort ();
13589                 }
13590               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13591               inst.instruction |= inst.operands[0].reg;
13592               inst.instruction |= inst.operands[1].reg << 3;
13593             }
13594         }
13595     }
13596   else
13597     {
13598       constraint (inst.operands[0].reg > 7
13599                   || inst.operands[1].reg > 7, BAD_HIREG);
13600       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13601
13602       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
13603         {
13604           constraint (inst.operands[2].reg > 7, BAD_HIREG);
13605           constraint (inst.operands[0].reg != inst.operands[1].reg,
13606                       _("source1 and dest must be same register"));
13607
13608           switch (inst.instruction)
13609             {
13610             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
13611             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
13612             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
13613             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
13614             default: abort ();
13615             }
13616
13617           inst.instruction |= inst.operands[0].reg;
13618           inst.instruction |= inst.operands[2].reg << 3;
13619
13620           /* PR 12854: Error on extraneous shifts.  */
13621           constraint (inst.operands[2].shifted,
13622                       _("extraneous shift as part of operand to shift insn"));
13623         }
13624       else
13625         {
13626           switch (inst.instruction)
13627             {
13628             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
13629             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
13630             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
13631             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
13632             default: abort ();
13633             }
13634           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13635           inst.instruction |= inst.operands[0].reg;
13636           inst.instruction |= inst.operands[1].reg << 3;
13637         }
13638     }
13639 }
13640
13641 static void
13642 do_t_simd (void)
13643 {
13644   unsigned Rd, Rn, Rm;
13645
13646   Rd = inst.operands[0].reg;
13647   Rn = inst.operands[1].reg;
13648   Rm = inst.operands[2].reg;
13649
13650   reject_bad_reg (Rd);
13651   reject_bad_reg (Rn);
13652   reject_bad_reg (Rm);
13653
13654   inst.instruction |= Rd << 8;
13655   inst.instruction |= Rn << 16;
13656   inst.instruction |= Rm;
13657 }
13658
13659 static void
13660 do_t_simd2 (void)
13661 {
13662   unsigned Rd, Rn, Rm;
13663
13664   Rd = inst.operands[0].reg;
13665   Rm = inst.operands[1].reg;
13666   Rn = inst.operands[2].reg;
13667
13668   reject_bad_reg (Rd);
13669   reject_bad_reg (Rn);
13670   reject_bad_reg (Rm);
13671
13672   inst.instruction |= Rd << 8;
13673   inst.instruction |= Rn << 16;
13674   inst.instruction |= Rm;
13675 }
13676
13677 static void
13678 do_t_smc (void)
13679 {
13680   unsigned int value = inst.relocs[0].exp.X_add_number;
13681   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13682               _("SMC is not permitted on this architecture"));
13683   constraint (inst.relocs[0].exp.X_op != O_constant,
13684               _("expression too complex"));
13685   inst.relocs[0].type = BFD_RELOC_UNUSED;
13686   inst.instruction |= (value & 0xf000) >> 12;
13687   inst.instruction |= (value & 0x0ff0);
13688   inst.instruction |= (value & 0x000f) << 16;
13689   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13690   set_pred_insn_type_last ();
13691 }
13692
13693 static void
13694 do_t_hvc (void)
13695 {
13696   unsigned int value = inst.relocs[0].exp.X_add_number;
13697
13698   inst.relocs[0].type = BFD_RELOC_UNUSED;
13699   inst.instruction |= (value & 0x0fff);
13700   inst.instruction |= (value & 0xf000) << 4;
13701 }
13702
13703 static void
13704 do_t_ssat_usat (int bias)
13705 {
13706   unsigned Rd, Rn;
13707
13708   Rd = inst.operands[0].reg;
13709   Rn = inst.operands[2].reg;
13710
13711   reject_bad_reg (Rd);
13712   reject_bad_reg (Rn);
13713
13714   inst.instruction |= Rd << 8;
13715   inst.instruction |= inst.operands[1].imm - bias;
13716   inst.instruction |= Rn << 16;
13717
13718   if (inst.operands[3].present)
13719     {
13720       offsetT shift_amount = inst.relocs[0].exp.X_add_number;
13721
13722       inst.relocs[0].type = BFD_RELOC_UNUSED;
13723
13724       constraint (inst.relocs[0].exp.X_op != O_constant,
13725                   _("expression too complex"));
13726
13727       if (shift_amount != 0)
13728         {
13729           constraint (shift_amount > 31,
13730                       _("shift expression is too large"));
13731
13732           if (inst.operands[3].shift_kind == SHIFT_ASR)
13733             inst.instruction |= 0x00200000;  /* sh bit.  */
13734
13735           inst.instruction |= (shift_amount & 0x1c) << 10;
13736           inst.instruction |= (shift_amount & 0x03) << 6;
13737         }
13738     }
13739 }
13740
13741 static void
13742 do_t_ssat (void)
13743 {
13744   do_t_ssat_usat (1);
13745 }
13746
13747 static void
13748 do_t_ssat16 (void)
13749 {
13750   unsigned Rd, Rn;
13751
13752   Rd = inst.operands[0].reg;
13753   Rn = inst.operands[2].reg;
13754
13755   reject_bad_reg (Rd);
13756   reject_bad_reg (Rn);
13757
13758   inst.instruction |= Rd << 8;
13759   inst.instruction |= inst.operands[1].imm - 1;
13760   inst.instruction |= Rn << 16;
13761 }
13762
13763 static void
13764 do_t_strex (void)
13765 {
13766   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
13767               || inst.operands[2].postind || inst.operands[2].writeback
13768               || inst.operands[2].immisreg || inst.operands[2].shifted
13769               || inst.operands[2].negative,
13770               BAD_ADDR_MODE);
13771
13772   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
13773
13774   inst.instruction |= inst.operands[0].reg << 8;
13775   inst.instruction |= inst.operands[1].reg << 12;
13776   inst.instruction |= inst.operands[2].reg << 16;
13777   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
13778 }
13779
13780 static void
13781 do_t_strexd (void)
13782 {
13783   if (!inst.operands[2].present)
13784     inst.operands[2].reg = inst.operands[1].reg + 1;
13785
13786   constraint (inst.operands[0].reg == inst.operands[1].reg
13787               || inst.operands[0].reg == inst.operands[2].reg
13788               || inst.operands[0].reg == inst.operands[3].reg,
13789               BAD_OVERLAP);
13790
13791   inst.instruction |= inst.operands[0].reg;
13792   inst.instruction |= inst.operands[1].reg << 12;
13793   inst.instruction |= inst.operands[2].reg << 8;
13794   inst.instruction |= inst.operands[3].reg << 16;
13795 }
13796
13797 static void
13798 do_t_sxtah (void)
13799 {
13800   unsigned Rd, Rn, Rm;
13801
13802   Rd = inst.operands[0].reg;
13803   Rn = inst.operands[1].reg;
13804   Rm = inst.operands[2].reg;
13805
13806   reject_bad_reg (Rd);
13807   reject_bad_reg (Rn);
13808   reject_bad_reg (Rm);
13809
13810   inst.instruction |= Rd << 8;
13811   inst.instruction |= Rn << 16;
13812   inst.instruction |= Rm;
13813   inst.instruction |= inst.operands[3].imm << 4;
13814 }
13815
13816 static void
13817 do_t_sxth (void)
13818 {
13819   unsigned Rd, Rm;
13820
13821   Rd = inst.operands[0].reg;
13822   Rm = inst.operands[1].reg;
13823
13824   reject_bad_reg (Rd);
13825   reject_bad_reg (Rm);
13826
13827   if (inst.instruction <= 0xffff
13828       && inst.size_req != 4
13829       && Rd <= 7 && Rm <= 7
13830       && (!inst.operands[2].present || inst.operands[2].imm == 0))
13831     {
13832       inst.instruction = THUMB_OP16 (inst.instruction);
13833       inst.instruction |= Rd;
13834       inst.instruction |= Rm << 3;
13835     }
13836   else if (unified_syntax)
13837     {
13838       if (inst.instruction <= 0xffff)
13839         inst.instruction = THUMB_OP32 (inst.instruction);
13840       inst.instruction |= Rd << 8;
13841       inst.instruction |= Rm;
13842       inst.instruction |= inst.operands[2].imm << 4;
13843     }
13844   else
13845     {
13846       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
13847                   _("Thumb encoding does not support rotation"));
13848       constraint (1, BAD_HIREG);
13849     }
13850 }
13851
13852 static void
13853 do_t_swi (void)
13854 {
13855   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
13856 }
13857
13858 static void
13859 do_t_tb (void)
13860 {
13861   unsigned Rn, Rm;
13862   int half;
13863
13864   half = (inst.instruction & 0x10) != 0;
13865   set_pred_insn_type_last ();
13866   constraint (inst.operands[0].immisreg,
13867               _("instruction requires register index"));
13868
13869   Rn = inst.operands[0].reg;
13870   Rm = inst.operands[0].imm;
13871
13872   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13873     constraint (Rn == REG_SP, BAD_SP);
13874   reject_bad_reg (Rm);
13875
13876   constraint (!half && inst.operands[0].shifted,
13877               _("instruction does not allow shifted index"));
13878   inst.instruction |= (Rn << 16) | Rm;
13879 }
13880
13881 static void
13882 do_t_udf (void)
13883 {
13884   if (!inst.operands[0].present)
13885     inst.operands[0].imm = 0;
13886
13887   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
13888     {
13889       constraint (inst.size_req == 2,
13890                   _("immediate value out of range"));
13891       inst.instruction = THUMB_OP32 (inst.instruction);
13892       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
13893       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
13894     }
13895   else
13896     {
13897       inst.instruction = THUMB_OP16 (inst.instruction);
13898       inst.instruction |= inst.operands[0].imm;
13899     }
13900
13901   set_pred_insn_type (NEUTRAL_IT_INSN);
13902 }
13903
13904
13905 static void
13906 do_t_usat (void)
13907 {
13908   do_t_ssat_usat (0);
13909 }
13910
13911 static void
13912 do_t_usat16 (void)
13913 {
13914   unsigned Rd, Rn;
13915
13916   Rd = inst.operands[0].reg;
13917   Rn = inst.operands[2].reg;
13918
13919   reject_bad_reg (Rd);
13920   reject_bad_reg (Rn);
13921
13922   inst.instruction |= Rd << 8;
13923   inst.instruction |= inst.operands[1].imm;
13924   inst.instruction |= Rn << 16;
13925 }
13926
13927 /* Checking the range of the branch offset (VAL) with NBITS bits
13928    and IS_SIGNED signedness.  Also checks the LSB to be 0.  */
13929 static int
13930 v8_1_branch_value_check (int val, int nbits, int is_signed)
13931 {
13932   gas_assert (nbits > 0 && nbits <= 32);
13933   if (is_signed)
13934     {
13935       int cmp = (1 << (nbits - 1));
13936       if ((val < -cmp) || (val >= cmp) || (val & 0x01))
13937         return FAIL;
13938     }
13939   else
13940     {
13941       if ((val <= 0) || (val >= (1 << nbits)) || (val & 0x1))
13942         return FAIL;
13943     }
13944     return SUCCESS;
13945 }
13946
13947 /* For branches in Armv8.1-M Mainline.  */
13948 static void
13949 do_t_branch_future (void)
13950 {
13951   unsigned long insn = inst.instruction;
13952
13953   inst.instruction = THUMB_OP32 (inst.instruction);
13954   if (inst.operands[0].hasreloc == 0)
13955     {
13956       if (v8_1_branch_value_check (inst.operands[0].imm, 5, FALSE) == FAIL)
13957         as_bad (BAD_BRANCH_OFF);
13958
13959       inst.instruction |= ((inst.operands[0].imm & 0x1f) >> 1) << 23;
13960     }
13961   else
13962     {
13963       inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH5;
13964       inst.relocs[0].pc_rel = 1;
13965     }
13966
13967   switch (insn)
13968     {
13969       case T_MNEM_bf:
13970         if (inst.operands[1].hasreloc == 0)
13971           {
13972             int val = inst.operands[1].imm;
13973             if (v8_1_branch_value_check (inst.operands[1].imm, 17, TRUE) == FAIL)
13974               as_bad (BAD_BRANCH_OFF);
13975
13976             int immA = (val & 0x0001f000) >> 12;
13977             int immB = (val & 0x00000ffc) >> 2;
13978             int immC = (val & 0x00000002) >> 1;
13979             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
13980           }
13981         else
13982           {
13983             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF17;
13984             inst.relocs[1].pc_rel = 1;
13985           }
13986         break;
13987
13988       case T_MNEM_bfl:
13989         if (inst.operands[1].hasreloc == 0)
13990           {
13991             int val = inst.operands[1].imm;
13992             if (v8_1_branch_value_check (inst.operands[1].imm, 19, TRUE) == FAIL)
13993               as_bad (BAD_BRANCH_OFF);
13994
13995             int immA = (val & 0x0007f000) >> 12;
13996             int immB = (val & 0x00000ffc) >> 2;
13997             int immC = (val & 0x00000002) >> 1;
13998             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
13999           }
14000           else
14001           {
14002             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF19;
14003             inst.relocs[1].pc_rel = 1;
14004           }
14005         break;
14006
14007       case T_MNEM_bfcsel:
14008         /* Operand 1.  */
14009         if (inst.operands[1].hasreloc == 0)
14010           {
14011             int val = inst.operands[1].imm;
14012             int immA = (val & 0x00001000) >> 12;
14013             int immB = (val & 0x00000ffc) >> 2;
14014             int immC = (val & 0x00000002) >> 1;
14015             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
14016           }
14017           else
14018           {
14019             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF13;
14020             inst.relocs[1].pc_rel = 1;
14021           }
14022
14023         /* Operand 2.  */
14024         if (inst.operands[2].hasreloc == 0)
14025           {
14026               constraint ((inst.operands[0].hasreloc != 0), BAD_ARGS);
14027               int val2 = inst.operands[2].imm;
14028               int val0 = inst.operands[0].imm & 0x1f;
14029               int diff = val2 - val0;
14030               if (diff == 4)
14031                 inst.instruction |= 1 << 17; /* T bit.  */
14032               else if (diff != 2)
14033                 as_bad (_("out of range label-relative fixup value"));
14034           }
14035         else
14036           {
14037               constraint ((inst.operands[0].hasreloc == 0), BAD_ARGS);
14038               inst.relocs[2].type = BFD_RELOC_THUMB_PCREL_BFCSEL;
14039               inst.relocs[2].pc_rel = 1;
14040           }
14041
14042         /* Operand 3.  */
14043         constraint (inst.cond != COND_ALWAYS, BAD_COND);
14044         inst.instruction |= (inst.operands[3].imm & 0xf) << 18;
14045         break;
14046
14047       case T_MNEM_bfx:
14048       case T_MNEM_bflx:
14049         inst.instruction |= inst.operands[1].reg << 16;
14050         break;
14051
14052       default: abort ();
14053     }
14054 }
14055
14056 /* Helper function for do_t_loloop to handle relocations.  */
14057 static void
14058 v8_1_loop_reloc (int is_le)
14059 {
14060   if (inst.relocs[0].exp.X_op == O_constant)
14061     {
14062       int value = inst.relocs[0].exp.X_add_number;
14063       value = (is_le) ? -value : value;
14064
14065       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
14066         as_bad (BAD_BRANCH_OFF);
14067
14068       int imml, immh;
14069
14070       immh = (value & 0x00000ffc) >> 2;
14071       imml = (value & 0x00000002) >> 1;
14072
14073       inst.instruction |= (imml << 11) | (immh << 1);
14074     }
14075   else
14076     {
14077       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_LOOP12;
14078       inst.relocs[0].pc_rel = 1;
14079     }
14080 }
14081
14082 /* To handle the Scalar Low Overhead Loop instructions
14083    in Armv8.1-M Mainline.  */
14084 static void
14085 do_t_loloop (void)
14086 {
14087   unsigned long insn = inst.instruction;
14088
14089   set_pred_insn_type (OUTSIDE_PRED_INSN);
14090   inst.instruction = THUMB_OP32 (inst.instruction);
14091
14092   switch (insn)
14093     {
14094     case T_MNEM_le:
14095       /* le <label>.  */
14096       if (!inst.operands[0].present)
14097         inst.instruction |= 1 << 21;
14098
14099       v8_1_loop_reloc (TRUE);
14100       break;
14101
14102     case T_MNEM_wls:
14103       v8_1_loop_reloc (FALSE);
14104       /* Fall through.  */
14105     case T_MNEM_dls:
14106       constraint (inst.operands[1].isreg != 1, BAD_ARGS);
14107       inst.instruction |= (inst.operands[1].reg << 16);
14108       break;
14109
14110     default: abort();
14111     }
14112 }
14113
14114 /* MVE instruction encoder helpers.  */
14115 #define M_MNEM_vabav    0xee800f01
14116 #define M_MNEM_vmladav    0xeef00e00
14117 #define M_MNEM_vmladava   0xeef00e20
14118 #define M_MNEM_vmladavx   0xeef01e00
14119 #define M_MNEM_vmladavax  0xeef01e20
14120 #define M_MNEM_vmlsdav    0xeef00e01
14121 #define M_MNEM_vmlsdava   0xeef00e21
14122 #define M_MNEM_vmlsdavx   0xeef01e01
14123 #define M_MNEM_vmlsdavax  0xeef01e21
14124 #define M_MNEM_vmullt   0xee011e00
14125 #define M_MNEM_vmullb   0xee010e00
14126 #define M_MNEM_vst20    0xfc801e00
14127 #define M_MNEM_vst21    0xfc801e20
14128 #define M_MNEM_vst40    0xfc801e01
14129 #define M_MNEM_vst41    0xfc801e21
14130 #define M_MNEM_vst42    0xfc801e41
14131 #define M_MNEM_vst43    0xfc801e61
14132 #define M_MNEM_vld20    0xfc901e00
14133 #define M_MNEM_vld21    0xfc901e20
14134 #define M_MNEM_vld40    0xfc901e01
14135 #define M_MNEM_vld41    0xfc901e21
14136 #define M_MNEM_vld42    0xfc901e41
14137 #define M_MNEM_vld43    0xfc901e61
14138 #define M_MNEM_vstrb    0xec000e00
14139 #define M_MNEM_vstrh    0xec000e10
14140 #define M_MNEM_vstrw    0xec000e40
14141 #define M_MNEM_vstrd    0xec000e50
14142 #define M_MNEM_vldrb    0xec100e00
14143 #define M_MNEM_vldrh    0xec100e10
14144 #define M_MNEM_vldrw    0xec100e40
14145 #define M_MNEM_vldrd    0xec100e50
14146 #define M_MNEM_vmovlt   0xeea01f40
14147 #define M_MNEM_vmovlb   0xeea00f40
14148 #define M_MNEM_vmovnt   0xfe311e81
14149 #define M_MNEM_vmovnb   0xfe310e81
14150 #define M_MNEM_vadc     0xee300f00
14151 #define M_MNEM_vadci    0xee301f00
14152 #define M_MNEM_vbrsr    0xfe011e60
14153 #define M_MNEM_vaddlv   0xee890f00
14154 #define M_MNEM_vaddlva  0xee890f20
14155 #define M_MNEM_vaddv    0xeef10f00
14156 #define M_MNEM_vaddva   0xeef10f20
14157
14158 /* Neon instruction encoder helpers.  */
14159
14160 /* Encodings for the different types for various Neon opcodes.  */
14161
14162 /* An "invalid" code for the following tables.  */
14163 #define N_INV -1u
14164
14165 struct neon_tab_entry
14166 {
14167   unsigned integer;
14168   unsigned float_or_poly;
14169   unsigned scalar_or_imm;
14170 };
14171
14172 /* Map overloaded Neon opcodes to their respective encodings.  */
14173 #define NEON_ENC_TAB                                    \
14174   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
14175   X(vabdl,      0x0800700, N_INV,     N_INV),           \
14176   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
14177   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
14178   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
14179   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
14180   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
14181   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
14182   X(vaddl,      0x0800000, N_INV,     N_INV),           \
14183   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
14184   X(vsubl,      0x0800200, N_INV,     N_INV),           \
14185   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
14186   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
14187   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
14188   /* Register variants of the following two instructions are encoded as
14189      vcge / vcgt with the operands reversed.  */        \
14190   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
14191   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
14192   X(vfma,       N_INV, 0x0000c10, N_INV),               \
14193   X(vfms,       N_INV, 0x0200c10, N_INV),               \
14194   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
14195   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
14196   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
14197   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
14198   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
14199   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
14200   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
14201   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
14202   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
14203   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
14204   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
14205   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
14206   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
14207   X(vshl,       0x0000400, N_INV,     0x0800510),       \
14208   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
14209   X(vand,       0x0000110, N_INV,     0x0800030),       \
14210   X(vbic,       0x0100110, N_INV,     0x0800030),       \
14211   X(veor,       0x1000110, N_INV,     N_INV),           \
14212   X(vorn,       0x0300110, N_INV,     0x0800010),       \
14213   X(vorr,       0x0200110, N_INV,     0x0800010),       \
14214   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
14215   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
14216   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
14217   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
14218   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
14219   X(vst1,       0x0000000, 0x0800000, N_INV),           \
14220   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
14221   X(vst2,       0x0000100, 0x0800100, N_INV),           \
14222   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
14223   X(vst3,       0x0000200, 0x0800200, N_INV),           \
14224   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
14225   X(vst4,       0x0000300, 0x0800300, N_INV),           \
14226   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
14227   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
14228   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
14229   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
14230   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
14231   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
14232   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
14233   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
14234   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
14235   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
14236   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
14237   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
14238   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
14239   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
14240   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
14241   X(vselge,     0xe200a00, N_INV,     N_INV),           \
14242   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
14243   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
14244   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
14245   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
14246   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
14247   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
14248   X(aes,        0x3b00300, N_INV,     N_INV),           \
14249   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
14250   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
14251   X(sha2op,     0x3ba0380, N_INV,     N_INV)
14252
14253 enum neon_opc
14254 {
14255 #define X(OPC,I,F,S) N_MNEM_##OPC
14256 NEON_ENC_TAB
14257 #undef X
14258 };
14259
14260 static const struct neon_tab_entry neon_enc_tab[] =
14261 {
14262 #define X(OPC,I,F,S) { (I), (F), (S) }
14263 NEON_ENC_TAB
14264 #undef X
14265 };
14266
14267 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
14268 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14269 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
14270 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14271 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14272 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14273 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14274 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
14275 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
14276 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
14277 #define NEON_ENC_SINGLE_(X) \
14278   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
14279 #define NEON_ENC_DOUBLE_(X) \
14280   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
14281 #define NEON_ENC_FPV8_(X) \
14282   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
14283
14284 #define NEON_ENCODE(type, inst)                                 \
14285   do                                                            \
14286     {                                                           \
14287       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
14288       inst.is_neon = 1;                                         \
14289     }                                                           \
14290   while (0)
14291
14292 #define check_neon_suffixes                                             \
14293   do                                                                    \
14294     {                                                                   \
14295       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
14296         {                                                               \
14297           as_bad (_("invalid neon suffix for non neon instruction"));   \
14298           return;                                                       \
14299         }                                                               \
14300     }                                                                   \
14301   while (0)
14302
14303 /* Define shapes for instruction operands. The following mnemonic characters
14304    are used in this table:
14305
14306      F - VFP S<n> register
14307      D - Neon D<n> register
14308      Q - Neon Q<n> register
14309      I - Immediate
14310      S - Scalar
14311      R - ARM register
14312      L - D<n> register list
14313
14314    This table is used to generate various data:
14315      - enumerations of the form NS_DDR to be used as arguments to
14316        neon_select_shape.
14317      - a table classifying shapes into single, double, quad, mixed.
14318      - a table used to drive neon_select_shape.  */
14319
14320 #define NEON_SHAPE_DEF                  \
14321   X(4, (R, R, S, S), QUAD),             \
14322   X(4, (S, S, R, R), QUAD),             \
14323   X(3, (I, Q, Q), QUAD),                \
14324   X(3, (I, Q, R), QUAD),                \
14325   X(3, (R, Q, Q), QUAD),                \
14326   X(3, (D, D, D), DOUBLE),              \
14327   X(3, (Q, Q, Q), QUAD),                \
14328   X(3, (D, D, I), DOUBLE),              \
14329   X(3, (Q, Q, I), QUAD),                \
14330   X(3, (D, D, S), DOUBLE),              \
14331   X(3, (Q, Q, S), QUAD),                \
14332   X(3, (Q, Q, R), QUAD),                \
14333   X(3, (R, R, Q), QUAD),                \
14334   X(2, (R, Q),    QUAD),                \
14335   X(2, (D, D), DOUBLE),                 \
14336   X(2, (Q, Q), QUAD),                   \
14337   X(2, (D, S), DOUBLE),                 \
14338   X(2, (Q, S), QUAD),                   \
14339   X(2, (D, R), DOUBLE),                 \
14340   X(2, (Q, R), QUAD),                   \
14341   X(2, (D, I), DOUBLE),                 \
14342   X(2, (Q, I), QUAD),                   \
14343   X(3, (D, L, D), DOUBLE),              \
14344   X(2, (D, Q), MIXED),                  \
14345   X(2, (Q, D), MIXED),                  \
14346   X(3, (D, Q, I), MIXED),               \
14347   X(3, (Q, D, I), MIXED),               \
14348   X(3, (Q, D, D), MIXED),               \
14349   X(3, (D, Q, Q), MIXED),               \
14350   X(3, (Q, Q, D), MIXED),               \
14351   X(3, (Q, D, S), MIXED),               \
14352   X(3, (D, Q, S), MIXED),               \
14353   X(4, (D, D, D, I), DOUBLE),           \
14354   X(4, (Q, Q, Q, I), QUAD),             \
14355   X(4, (D, D, S, I), DOUBLE),           \
14356   X(4, (Q, Q, S, I), QUAD),             \
14357   X(2, (F, F), SINGLE),                 \
14358   X(3, (F, F, F), SINGLE),              \
14359   X(2, (F, I), SINGLE),                 \
14360   X(2, (F, D), MIXED),                  \
14361   X(2, (D, F), MIXED),                  \
14362   X(3, (F, F, I), MIXED),               \
14363   X(4, (R, R, F, F), SINGLE),           \
14364   X(4, (F, F, R, R), SINGLE),           \
14365   X(3, (D, R, R), DOUBLE),              \
14366   X(3, (R, R, D), DOUBLE),              \
14367   X(2, (S, R), SINGLE),                 \
14368   X(2, (R, S), SINGLE),                 \
14369   X(2, (F, R), SINGLE),                 \
14370   X(2, (R, F), SINGLE),                 \
14371 /* Half float shape supported so far.  */\
14372   X (2, (H, D), MIXED),                 \
14373   X (2, (D, H), MIXED),                 \
14374   X (2, (H, F), MIXED),                 \
14375   X (2, (F, H), MIXED),                 \
14376   X (2, (H, H), HALF),                  \
14377   X (2, (H, R), HALF),                  \
14378   X (2, (R, H), HALF),                  \
14379   X (2, (H, I), HALF),                  \
14380   X (3, (H, H, H), HALF),               \
14381   X (3, (H, F, I), MIXED),              \
14382   X (3, (F, H, I), MIXED),              \
14383   X (3, (D, H, H), MIXED),              \
14384   X (3, (D, H, S), MIXED)
14385
14386 #define S2(A,B)         NS_##A##B
14387 #define S3(A,B,C)       NS_##A##B##C
14388 #define S4(A,B,C,D)     NS_##A##B##C##D
14389
14390 #define X(N, L, C) S##N L
14391
14392 enum neon_shape
14393 {
14394   NEON_SHAPE_DEF,
14395   NS_NULL
14396 };
14397
14398 #undef X
14399 #undef S2
14400 #undef S3
14401 #undef S4
14402
14403 enum neon_shape_class
14404 {
14405   SC_HALF,
14406   SC_SINGLE,
14407   SC_DOUBLE,
14408   SC_QUAD,
14409   SC_MIXED
14410 };
14411
14412 #define X(N, L, C) SC_##C
14413
14414 static enum neon_shape_class neon_shape_class[] =
14415 {
14416   NEON_SHAPE_DEF
14417 };
14418
14419 #undef X
14420
14421 enum neon_shape_el
14422 {
14423   SE_H,
14424   SE_F,
14425   SE_D,
14426   SE_Q,
14427   SE_I,
14428   SE_S,
14429   SE_R,
14430   SE_L
14431 };
14432
14433 /* Register widths of above.  */
14434 static unsigned neon_shape_el_size[] =
14435 {
14436   16,
14437   32,
14438   64,
14439   128,
14440   0,
14441   32,
14442   32,
14443   0
14444 };
14445
14446 struct neon_shape_info
14447 {
14448   unsigned els;
14449   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
14450 };
14451
14452 #define S2(A,B)         { SE_##A, SE_##B }
14453 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
14454 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
14455
14456 #define X(N, L, C) { N, S##N L }
14457
14458 static struct neon_shape_info neon_shape_tab[] =
14459 {
14460   NEON_SHAPE_DEF
14461 };
14462
14463 #undef X
14464 #undef S2
14465 #undef S3
14466 #undef S4
14467
14468 /* Bit masks used in type checking given instructions.
14469   'N_EQK' means the type must be the same as (or based on in some way) the key
14470    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
14471    set, various other bits can be set as well in order to modify the meaning of
14472    the type constraint.  */
14473
14474 enum neon_type_mask
14475 {
14476   N_S8   = 0x0000001,
14477   N_S16  = 0x0000002,
14478   N_S32  = 0x0000004,
14479   N_S64  = 0x0000008,
14480   N_U8   = 0x0000010,
14481   N_U16  = 0x0000020,
14482   N_U32  = 0x0000040,
14483   N_U64  = 0x0000080,
14484   N_I8   = 0x0000100,
14485   N_I16  = 0x0000200,
14486   N_I32  = 0x0000400,
14487   N_I64  = 0x0000800,
14488   N_8    = 0x0001000,
14489   N_16   = 0x0002000,
14490   N_32   = 0x0004000,
14491   N_64   = 0x0008000,
14492   N_P8   = 0x0010000,
14493   N_P16  = 0x0020000,
14494   N_F16  = 0x0040000,
14495   N_F32  = 0x0080000,
14496   N_F64  = 0x0100000,
14497   N_P64  = 0x0200000,
14498   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
14499   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
14500   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
14501   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
14502   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
14503   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
14504   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
14505   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
14506   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
14507   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
14508   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
14509   N_UTYP = 0,
14510   N_MAX_NONSPECIAL = N_P64
14511 };
14512
14513 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
14514
14515 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
14516 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14517 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
14518 #define N_S_32     (N_S8 | N_S16 | N_S32)
14519 #define N_F_16_32  (N_F16 | N_F32)
14520 #define N_SUF_32   (N_SU_32 | N_F_16_32)
14521 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
14522 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
14523 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
14524 #define N_I_MVE    (N_I8 | N_I16 | N_I32)
14525 #define N_F_MVE    (N_F16 | N_F32)
14526 #define N_SU_MVE   (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14527
14528 /* Pass this as the first type argument to neon_check_type to ignore types
14529    altogether.  */
14530 #define N_IGNORE_TYPE (N_KEY | N_EQK)
14531
14532 /* Select a "shape" for the current instruction (describing register types or
14533    sizes) from a list of alternatives. Return NS_NULL if the current instruction
14534    doesn't fit. For non-polymorphic shapes, checking is usually done as a
14535    function of operand parsing, so this function doesn't need to be called.
14536    Shapes should be listed in order of decreasing length.  */
14537
14538 static enum neon_shape
14539 neon_select_shape (enum neon_shape shape, ...)
14540 {
14541   va_list ap;
14542   enum neon_shape first_shape = shape;
14543
14544   /* Fix missing optional operands. FIXME: we don't know at this point how
14545      many arguments we should have, so this makes the assumption that we have
14546      > 1. This is true of all current Neon opcodes, I think, but may not be
14547      true in the future.  */
14548   if (!inst.operands[1].present)
14549     inst.operands[1] = inst.operands[0];
14550
14551   va_start (ap, shape);
14552
14553   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
14554     {
14555       unsigned j;
14556       int matches = 1;
14557
14558       for (j = 0; j < neon_shape_tab[shape].els; j++)
14559         {
14560           if (!inst.operands[j].present)
14561             {
14562               matches = 0;
14563               break;
14564             }
14565
14566           switch (neon_shape_tab[shape].el[j])
14567             {
14568               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
14569                  a VFP single precision register operand, it's essentially
14570                  means only half of the register is used.
14571
14572                  If the type specifier is given after the mnemonics, the
14573                  information is stored in inst.vectype.  If the type specifier
14574                  is given after register operand, the information is stored
14575                  in inst.operands[].vectype.
14576
14577                  When there is only one type specifier, and all the register
14578                  operands are the same type of hardware register, the type
14579                  specifier applies to all register operands.
14580
14581                  If no type specifier is given, the shape is inferred from
14582                  operand information.
14583
14584                  for example:
14585                  vadd.f16 s0, s1, s2:           NS_HHH
14586                  vabs.f16 s0, s1:               NS_HH
14587                  vmov.f16 s0, r1:               NS_HR
14588                  vmov.f16 r0, s1:               NS_RH
14589                  vcvt.f16 r0, s1:               NS_RH
14590                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
14591                  vcvt.f16.s32   s2, s2:         NS_HF
14592               */
14593             case SE_H:
14594               if (!(inst.operands[j].isreg
14595                     && inst.operands[j].isvec
14596                     && inst.operands[j].issingle
14597                     && !inst.operands[j].isquad
14598                     && ((inst.vectype.elems == 1
14599                          && inst.vectype.el[0].size == 16)
14600                         || (inst.vectype.elems > 1
14601                             && inst.vectype.el[j].size == 16)
14602                         || (inst.vectype.elems == 0
14603                             && inst.operands[j].vectype.type != NT_invtype
14604                             && inst.operands[j].vectype.size == 16))))
14605                 matches = 0;
14606               break;
14607
14608             case SE_F:
14609               if (!(inst.operands[j].isreg
14610                     && inst.operands[j].isvec
14611                     && inst.operands[j].issingle
14612                     && !inst.operands[j].isquad
14613                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
14614                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
14615                         || (inst.vectype.elems == 0
14616                             && (inst.operands[j].vectype.size == 32
14617                                 || inst.operands[j].vectype.type == NT_invtype)))))
14618                 matches = 0;
14619               break;
14620
14621             case SE_D:
14622               if (!(inst.operands[j].isreg
14623                     && inst.operands[j].isvec
14624                     && !inst.operands[j].isquad
14625                     && !inst.operands[j].issingle))
14626                 matches = 0;
14627               break;
14628
14629             case SE_R:
14630               if (!(inst.operands[j].isreg
14631                     && !inst.operands[j].isvec))
14632                 matches = 0;
14633               break;
14634
14635             case SE_Q:
14636               if (!(inst.operands[j].isreg
14637                     && inst.operands[j].isvec
14638                     && inst.operands[j].isquad
14639                     && !inst.operands[j].issingle))
14640                 matches = 0;
14641               break;
14642
14643             case SE_I:
14644               if (!(!inst.operands[j].isreg
14645                     && !inst.operands[j].isscalar))
14646                 matches = 0;
14647               break;
14648
14649             case SE_S:
14650               if (!(!inst.operands[j].isreg
14651                     && inst.operands[j].isscalar))
14652                 matches = 0;
14653               break;
14654
14655             case SE_L:
14656               break;
14657             }
14658           if (!matches)
14659             break;
14660         }
14661       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
14662         /* We've matched all the entries in the shape table, and we don't
14663            have any left over operands which have not been matched.  */
14664         break;
14665     }
14666
14667   va_end (ap);
14668
14669   if (shape == NS_NULL && first_shape != NS_NULL)
14670     first_error (_("invalid instruction shape"));
14671
14672   return shape;
14673 }
14674
14675 /* True if SHAPE is predominantly a quadword operation (most of the time, this
14676    means the Q bit should be set).  */
14677
14678 static int
14679 neon_quad (enum neon_shape shape)
14680 {
14681   return neon_shape_class[shape] == SC_QUAD;
14682 }
14683
14684 static void
14685 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
14686                        unsigned *g_size)
14687 {
14688   /* Allow modification to be made to types which are constrained to be
14689      based on the key element, based on bits set alongside N_EQK.  */
14690   if ((typebits & N_EQK) != 0)
14691     {
14692       if ((typebits & N_HLF) != 0)
14693         *g_size /= 2;
14694       else if ((typebits & N_DBL) != 0)
14695         *g_size *= 2;
14696       if ((typebits & N_SGN) != 0)
14697         *g_type = NT_signed;
14698       else if ((typebits & N_UNS) != 0)
14699         *g_type = NT_unsigned;
14700       else if ((typebits & N_INT) != 0)
14701         *g_type = NT_integer;
14702       else if ((typebits & N_FLT) != 0)
14703         *g_type = NT_float;
14704       else if ((typebits & N_SIZ) != 0)
14705         *g_type = NT_untyped;
14706     }
14707 }
14708
14709 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
14710    operand type, i.e. the single type specified in a Neon instruction when it
14711    is the only one given.  */
14712
14713 static struct neon_type_el
14714 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
14715 {
14716   struct neon_type_el dest = *key;
14717
14718   gas_assert ((thisarg & N_EQK) != 0);
14719
14720   neon_modify_type_size (thisarg, &dest.type, &dest.size);
14721
14722   return dest;
14723 }
14724
14725 /* Convert Neon type and size into compact bitmask representation.  */
14726
14727 static enum neon_type_mask
14728 type_chk_of_el_type (enum neon_el_type type, unsigned size)
14729 {
14730   switch (type)
14731     {
14732     case NT_untyped:
14733       switch (size)
14734         {
14735         case 8:  return N_8;
14736         case 16: return N_16;
14737         case 32: return N_32;
14738         case 64: return N_64;
14739         default: ;
14740         }
14741       break;
14742
14743     case NT_integer:
14744       switch (size)
14745         {
14746         case 8:  return N_I8;
14747         case 16: return N_I16;
14748         case 32: return N_I32;
14749         case 64: return N_I64;
14750         default: ;
14751         }
14752       break;
14753
14754     case NT_float:
14755       switch (size)
14756         {
14757         case 16: return N_F16;
14758         case 32: return N_F32;
14759         case 64: return N_F64;
14760         default: ;
14761         }
14762       break;
14763
14764     case NT_poly:
14765       switch (size)
14766         {
14767         case 8:  return N_P8;
14768         case 16: return N_P16;
14769         case 64: return N_P64;
14770         default: ;
14771         }
14772       break;
14773
14774     case NT_signed:
14775       switch (size)
14776         {
14777         case 8:  return N_S8;
14778         case 16: return N_S16;
14779         case 32: return N_S32;
14780         case 64: return N_S64;
14781         default: ;
14782         }
14783       break;
14784
14785     case NT_unsigned:
14786       switch (size)
14787         {
14788         case 8:  return N_U8;
14789         case 16: return N_U16;
14790         case 32: return N_U32;
14791         case 64: return N_U64;
14792         default: ;
14793         }
14794       break;
14795
14796     default: ;
14797     }
14798
14799   return N_UTYP;
14800 }
14801
14802 /* Convert compact Neon bitmask type representation to a type and size. Only
14803    handles the case where a single bit is set in the mask.  */
14804
14805 static int
14806 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
14807                      enum neon_type_mask mask)
14808 {
14809   if ((mask & N_EQK) != 0)
14810     return FAIL;
14811
14812   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
14813     *size = 8;
14814   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
14815     *size = 16;
14816   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
14817     *size = 32;
14818   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
14819     *size = 64;
14820   else
14821     return FAIL;
14822
14823   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
14824     *type = NT_signed;
14825   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
14826     *type = NT_unsigned;
14827   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
14828     *type = NT_integer;
14829   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
14830     *type = NT_untyped;
14831   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
14832     *type = NT_poly;
14833   else if ((mask & (N_F_ALL)) != 0)
14834     *type = NT_float;
14835   else
14836     return FAIL;
14837
14838   return SUCCESS;
14839 }
14840
14841 /* Modify a bitmask of allowed types. This is only needed for type
14842    relaxation.  */
14843
14844 static unsigned
14845 modify_types_allowed (unsigned allowed, unsigned mods)
14846 {
14847   unsigned size;
14848   enum neon_el_type type;
14849   unsigned destmask;
14850   int i;
14851
14852   destmask = 0;
14853
14854   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
14855     {
14856       if (el_type_of_type_chk (&type, &size,
14857                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
14858         {
14859           neon_modify_type_size (mods, &type, &size);
14860           destmask |= type_chk_of_el_type (type, size);
14861         }
14862     }
14863
14864   return destmask;
14865 }
14866
14867 /* Check type and return type classification.
14868    The manual states (paraphrase): If one datatype is given, it indicates the
14869    type given in:
14870     - the second operand, if there is one
14871     - the operand, if there is no second operand
14872     - the result, if there are no operands.
14873    This isn't quite good enough though, so we use a concept of a "key" datatype
14874    which is set on a per-instruction basis, which is the one which matters when
14875    only one data type is written.
14876    Note: this function has side-effects (e.g. filling in missing operands). All
14877    Neon instructions should call it before performing bit encoding.  */
14878
14879 static struct neon_type_el
14880 neon_check_type (unsigned els, enum neon_shape ns, ...)
14881 {
14882   va_list ap;
14883   unsigned i, pass, key_el = 0;
14884   unsigned types[NEON_MAX_TYPE_ELS];
14885   enum neon_el_type k_type = NT_invtype;
14886   unsigned k_size = -1u;
14887   struct neon_type_el badtype = {NT_invtype, -1};
14888   unsigned key_allowed = 0;
14889
14890   /* Optional registers in Neon instructions are always (not) in operand 1.
14891      Fill in the missing operand here, if it was omitted.  */
14892   if (els > 1 && !inst.operands[1].present)
14893     inst.operands[1] = inst.operands[0];
14894
14895   /* Suck up all the varargs.  */
14896   va_start (ap, ns);
14897   for (i = 0; i < els; i++)
14898     {
14899       unsigned thisarg = va_arg (ap, unsigned);
14900       if (thisarg == N_IGNORE_TYPE)
14901         {
14902           va_end (ap);
14903           return badtype;
14904         }
14905       types[i] = thisarg;
14906       if ((thisarg & N_KEY) != 0)
14907         key_el = i;
14908     }
14909   va_end (ap);
14910
14911   if (inst.vectype.elems > 0)
14912     for (i = 0; i < els; i++)
14913       if (inst.operands[i].vectype.type != NT_invtype)
14914         {
14915           first_error (_("types specified in both the mnemonic and operands"));
14916           return badtype;
14917         }
14918
14919   /* Duplicate inst.vectype elements here as necessary.
14920      FIXME: No idea if this is exactly the same as the ARM assembler,
14921      particularly when an insn takes one register and one non-register
14922      operand. */
14923   if (inst.vectype.elems == 1 && els > 1)
14924     {
14925       unsigned j;
14926       inst.vectype.elems = els;
14927       inst.vectype.el[key_el] = inst.vectype.el[0];
14928       for (j = 0; j < els; j++)
14929         if (j != key_el)
14930           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14931                                                   types[j]);
14932     }
14933   else if (inst.vectype.elems == 0 && els > 0)
14934     {
14935       unsigned j;
14936       /* No types were given after the mnemonic, so look for types specified
14937          after each operand. We allow some flexibility here; as long as the
14938          "key" operand has a type, we can infer the others.  */
14939       for (j = 0; j < els; j++)
14940         if (inst.operands[j].vectype.type != NT_invtype)
14941           inst.vectype.el[j] = inst.operands[j].vectype;
14942
14943       if (inst.operands[key_el].vectype.type != NT_invtype)
14944         {
14945           for (j = 0; j < els; j++)
14946             if (inst.operands[j].vectype.type == NT_invtype)
14947               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14948                                                       types[j]);
14949         }
14950       else
14951         {
14952           first_error (_("operand types can't be inferred"));
14953           return badtype;
14954         }
14955     }
14956   else if (inst.vectype.elems != els)
14957     {
14958       first_error (_("type specifier has the wrong number of parts"));
14959       return badtype;
14960     }
14961
14962   for (pass = 0; pass < 2; pass++)
14963     {
14964       for (i = 0; i < els; i++)
14965         {
14966           unsigned thisarg = types[i];
14967           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
14968             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
14969           enum neon_el_type g_type = inst.vectype.el[i].type;
14970           unsigned g_size = inst.vectype.el[i].size;
14971
14972           /* Decay more-specific signed & unsigned types to sign-insensitive
14973              integer types if sign-specific variants are unavailable.  */
14974           if ((g_type == NT_signed || g_type == NT_unsigned)
14975               && (types_allowed & N_SU_ALL) == 0)
14976             g_type = NT_integer;
14977
14978           /* If only untyped args are allowed, decay any more specific types to
14979              them. Some instructions only care about signs for some element
14980              sizes, so handle that properly.  */
14981           if (((types_allowed & N_UNT) == 0)
14982               && ((g_size == 8 && (types_allowed & N_8) != 0)
14983                   || (g_size == 16 && (types_allowed & N_16) != 0)
14984                   || (g_size == 32 && (types_allowed & N_32) != 0)
14985                   || (g_size == 64 && (types_allowed & N_64) != 0)))
14986             g_type = NT_untyped;
14987
14988           if (pass == 0)
14989             {
14990               if ((thisarg & N_KEY) != 0)
14991                 {
14992                   k_type = g_type;
14993                   k_size = g_size;
14994                   key_allowed = thisarg & ~N_KEY;
14995
14996                   /* Check architecture constraint on FP16 extension.  */
14997                   if (k_size == 16
14998                       && k_type == NT_float
14999                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15000                     {
15001                       inst.error = _(BAD_FP16);
15002                       return badtype;
15003                     }
15004                 }
15005             }
15006           else
15007             {
15008               if ((thisarg & N_VFP) != 0)
15009                 {
15010                   enum neon_shape_el regshape;
15011                   unsigned regwidth, match;
15012
15013                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
15014                   if (ns == NS_NULL)
15015                     {
15016                       first_error (_("invalid instruction shape"));
15017                       return badtype;
15018                     }
15019                   regshape = neon_shape_tab[ns].el[i];
15020                   regwidth = neon_shape_el_size[regshape];
15021
15022                   /* In VFP mode, operands must match register widths. If we
15023                      have a key operand, use its width, else use the width of
15024                      the current operand.  */
15025                   if (k_size != -1u)
15026                     match = k_size;
15027                   else
15028                     match = g_size;
15029
15030                   /* FP16 will use a single precision register.  */
15031                   if (regwidth == 32 && match == 16)
15032                     {
15033                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
15034                         match = regwidth;
15035                       else
15036                         {
15037                           inst.error = _(BAD_FP16);
15038                           return badtype;
15039                         }
15040                     }
15041
15042                   if (regwidth != match)
15043                     {
15044                       first_error (_("operand size must match register width"));
15045                       return badtype;
15046                     }
15047                 }
15048
15049               if ((thisarg & N_EQK) == 0)
15050                 {
15051                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
15052
15053                   if ((given_type & types_allowed) == 0)
15054                     {
15055                       first_error (BAD_SIMD_TYPE);
15056                       return badtype;
15057                     }
15058                 }
15059               else
15060                 {
15061                   enum neon_el_type mod_k_type = k_type;
15062                   unsigned mod_k_size = k_size;
15063                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
15064                   if (g_type != mod_k_type || g_size != mod_k_size)
15065                     {
15066                       first_error (_("inconsistent types in Neon instruction"));
15067                       return badtype;
15068                     }
15069                 }
15070             }
15071         }
15072     }
15073
15074   return inst.vectype.el[key_el];
15075 }
15076
15077 /* Neon-style VFP instruction forwarding.  */
15078
15079 /* Thumb VFP instructions have 0xE in the condition field.  */
15080
15081 static void
15082 do_vfp_cond_or_thumb (void)
15083 {
15084   inst.is_neon = 1;
15085
15086   if (thumb_mode)
15087     inst.instruction |= 0xe0000000;
15088   else
15089     inst.instruction |= inst.cond << 28;
15090 }
15091
15092 /* Look up and encode a simple mnemonic, for use as a helper function for the
15093    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
15094    etc.  It is assumed that operand parsing has already been done, and that the
15095    operands are in the form expected by the given opcode (this isn't necessarily
15096    the same as the form in which they were parsed, hence some massaging must
15097    take place before this function is called).
15098    Checks current arch version against that in the looked-up opcode.  */
15099
15100 static void
15101 do_vfp_nsyn_opcode (const char *opname)
15102 {
15103   const struct asm_opcode *opcode;
15104
15105   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
15106
15107   if (!opcode)
15108     abort ();
15109
15110   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
15111                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
15112               _(BAD_FPU));
15113
15114   inst.is_neon = 1;
15115
15116   if (thumb_mode)
15117     {
15118       inst.instruction = opcode->tvalue;
15119       opcode->tencode ();
15120     }
15121   else
15122     {
15123       inst.instruction = (inst.cond << 28) | opcode->avalue;
15124       opcode->aencode ();
15125     }
15126 }
15127
15128 static void
15129 do_vfp_nsyn_add_sub (enum neon_shape rs)
15130 {
15131   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
15132
15133   if (rs == NS_FFF || rs == NS_HHH)
15134     {
15135       if (is_add)
15136         do_vfp_nsyn_opcode ("fadds");
15137       else
15138         do_vfp_nsyn_opcode ("fsubs");
15139
15140       /* ARMv8.2 fp16 instruction.  */
15141       if (rs == NS_HHH)
15142         do_scalar_fp16_v82_encode ();
15143     }
15144   else
15145     {
15146       if (is_add)
15147         do_vfp_nsyn_opcode ("faddd");
15148       else
15149         do_vfp_nsyn_opcode ("fsubd");
15150     }
15151 }
15152
15153 /* Check operand types to see if this is a VFP instruction, and if so call
15154    PFN ().  */
15155
15156 static int
15157 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
15158 {
15159   enum neon_shape rs;
15160   struct neon_type_el et;
15161
15162   switch (args)
15163     {
15164     case 2:
15165       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15166       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15167       break;
15168
15169     case 3:
15170       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15171       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15172                             N_F_ALL | N_KEY | N_VFP);
15173       break;
15174
15175     default:
15176       abort ();
15177     }
15178
15179   if (et.type != NT_invtype)
15180     {
15181       pfn (rs);
15182       return SUCCESS;
15183     }
15184
15185   inst.error = NULL;
15186   return FAIL;
15187 }
15188
15189 static void
15190 do_vfp_nsyn_mla_mls (enum neon_shape rs)
15191 {
15192   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
15193
15194   if (rs == NS_FFF || rs == NS_HHH)
15195     {
15196       if (is_mla)
15197         do_vfp_nsyn_opcode ("fmacs");
15198       else
15199         do_vfp_nsyn_opcode ("fnmacs");
15200
15201       /* ARMv8.2 fp16 instruction.  */
15202       if (rs == NS_HHH)
15203         do_scalar_fp16_v82_encode ();
15204     }
15205   else
15206     {
15207       if (is_mla)
15208         do_vfp_nsyn_opcode ("fmacd");
15209       else
15210         do_vfp_nsyn_opcode ("fnmacd");
15211     }
15212 }
15213
15214 static void
15215 do_vfp_nsyn_fma_fms (enum neon_shape rs)
15216 {
15217   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
15218
15219   if (rs == NS_FFF || rs == NS_HHH)
15220     {
15221       if (is_fma)
15222         do_vfp_nsyn_opcode ("ffmas");
15223       else
15224         do_vfp_nsyn_opcode ("ffnmas");
15225
15226       /* ARMv8.2 fp16 instruction.  */
15227       if (rs == NS_HHH)
15228         do_scalar_fp16_v82_encode ();
15229     }
15230   else
15231     {
15232       if (is_fma)
15233         do_vfp_nsyn_opcode ("ffmad");
15234       else
15235         do_vfp_nsyn_opcode ("ffnmad");
15236     }
15237 }
15238
15239 static void
15240 do_vfp_nsyn_mul (enum neon_shape rs)
15241 {
15242   if (rs == NS_FFF || rs == NS_HHH)
15243     {
15244       do_vfp_nsyn_opcode ("fmuls");
15245
15246       /* ARMv8.2 fp16 instruction.  */
15247       if (rs == NS_HHH)
15248         do_scalar_fp16_v82_encode ();
15249     }
15250   else
15251     do_vfp_nsyn_opcode ("fmuld");
15252 }
15253
15254 static void
15255 do_vfp_nsyn_abs_neg (enum neon_shape rs)
15256 {
15257   int is_neg = (inst.instruction & 0x80) != 0;
15258   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
15259
15260   if (rs == NS_FF || rs == NS_HH)
15261     {
15262       if (is_neg)
15263         do_vfp_nsyn_opcode ("fnegs");
15264       else
15265         do_vfp_nsyn_opcode ("fabss");
15266
15267       /* ARMv8.2 fp16 instruction.  */
15268       if (rs == NS_HH)
15269         do_scalar_fp16_v82_encode ();
15270     }
15271   else
15272     {
15273       if (is_neg)
15274         do_vfp_nsyn_opcode ("fnegd");
15275       else
15276         do_vfp_nsyn_opcode ("fabsd");
15277     }
15278 }
15279
15280 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
15281    insns belong to Neon, and are handled elsewhere.  */
15282
15283 static void
15284 do_vfp_nsyn_ldm_stm (int is_dbmode)
15285 {
15286   int is_ldm = (inst.instruction & (1 << 20)) != 0;
15287   if (is_ldm)
15288     {
15289       if (is_dbmode)
15290         do_vfp_nsyn_opcode ("fldmdbs");
15291       else
15292         do_vfp_nsyn_opcode ("fldmias");
15293     }
15294   else
15295     {
15296       if (is_dbmode)
15297         do_vfp_nsyn_opcode ("fstmdbs");
15298       else
15299         do_vfp_nsyn_opcode ("fstmias");
15300     }
15301 }
15302
15303 static void
15304 do_vfp_nsyn_sqrt (void)
15305 {
15306   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15307   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15308
15309   if (rs == NS_FF || rs == NS_HH)
15310     {
15311       do_vfp_nsyn_opcode ("fsqrts");
15312
15313       /* ARMv8.2 fp16 instruction.  */
15314       if (rs == NS_HH)
15315         do_scalar_fp16_v82_encode ();
15316     }
15317   else
15318     do_vfp_nsyn_opcode ("fsqrtd");
15319 }
15320
15321 static void
15322 do_vfp_nsyn_div (void)
15323 {
15324   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15325   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15326                    N_F_ALL | N_KEY | N_VFP);
15327
15328   if (rs == NS_FFF || rs == NS_HHH)
15329     {
15330       do_vfp_nsyn_opcode ("fdivs");
15331
15332       /* ARMv8.2 fp16 instruction.  */
15333       if (rs == NS_HHH)
15334         do_scalar_fp16_v82_encode ();
15335     }
15336   else
15337     do_vfp_nsyn_opcode ("fdivd");
15338 }
15339
15340 static void
15341 do_vfp_nsyn_nmul (void)
15342 {
15343   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
15344   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
15345                    N_F_ALL | N_KEY | N_VFP);
15346
15347   if (rs == NS_FFF || rs == NS_HHH)
15348     {
15349       NEON_ENCODE (SINGLE, inst);
15350       do_vfp_sp_dyadic ();
15351
15352       /* ARMv8.2 fp16 instruction.  */
15353       if (rs == NS_HHH)
15354         do_scalar_fp16_v82_encode ();
15355     }
15356   else
15357     {
15358       NEON_ENCODE (DOUBLE, inst);
15359       do_vfp_dp_rd_rn_rm ();
15360     }
15361   do_vfp_cond_or_thumb ();
15362
15363 }
15364
15365 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
15366    (0, 1, 2, 3).  */
15367
15368 static unsigned
15369 neon_logbits (unsigned x)
15370 {
15371   return ffs (x) - 4;
15372 }
15373
15374 #define LOW4(R) ((R) & 0xf)
15375 #define HI1(R) (((R) >> 4) & 1)
15376
15377 static unsigned
15378 mve_get_vcmp_vpt_cond (struct neon_type_el et)
15379 {
15380   switch (et.type)
15381     {
15382     default:
15383       first_error (BAD_EL_TYPE);
15384       return 0;
15385     case NT_float:
15386       switch (inst.operands[0].imm)
15387         {
15388         default:
15389           first_error (_("invalid condition"));
15390           return 0;
15391         case 0x0:
15392           /* eq.  */
15393           return 0;
15394         case 0x1:
15395           /* ne.  */
15396           return 1;
15397         case 0xa:
15398           /* ge/  */
15399           return 4;
15400         case 0xb:
15401           /* lt.  */
15402           return 5;
15403         case 0xc:
15404           /* gt.  */
15405           return 6;
15406         case 0xd:
15407           /* le.  */
15408           return 7;
15409         }
15410     case NT_integer:
15411       /* only accept eq and ne.  */
15412       if (inst.operands[0].imm > 1)
15413         {
15414           first_error (_("invalid condition"));
15415           return 0;
15416         }
15417       return inst.operands[0].imm;
15418     case NT_unsigned:
15419       if (inst.operands[0].imm == 0x2)
15420         return 2;
15421       else if (inst.operands[0].imm == 0x8)
15422         return 3;
15423       else
15424         {
15425           first_error (_("invalid condition"));
15426           return 0;
15427         }
15428     case NT_signed:
15429       switch (inst.operands[0].imm)
15430         {
15431           default:
15432             first_error (_("invalid condition"));
15433             return 0;
15434           case 0xa:
15435             /* ge.  */
15436             return 4;
15437           case 0xb:
15438             /* lt.  */
15439             return 5;
15440           case 0xc:
15441             /* gt.  */
15442             return 6;
15443           case 0xd:
15444             /* le.  */
15445             return 7;
15446         }
15447     }
15448   /* Should be unreachable.  */
15449   abort ();
15450 }
15451
15452 static void
15453 do_mve_vpt (void)
15454 {
15455   /* We are dealing with a vector predicated block.  */
15456   if (inst.operands[0].present)
15457     {
15458       enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15459       struct neon_type_el et
15460         = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15461                            N_EQK);
15462
15463       unsigned fcond = mve_get_vcmp_vpt_cond (et);
15464
15465       constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15466
15467       if (et.type == NT_invtype)
15468         return;
15469
15470       if (et.type == NT_float)
15471         {
15472           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15473                       BAD_FPU);
15474           constraint (et.size != 16 && et.size != 32, BAD_EL_TYPE);
15475           inst.instruction |= (et.size == 16) << 28;
15476           inst.instruction |= 0x3 << 20;
15477         }
15478       else
15479         {
15480           constraint (et.size != 8 && et.size != 16 && et.size != 32,
15481                       BAD_EL_TYPE);
15482           inst.instruction |= 1 << 28;
15483           inst.instruction |= neon_logbits (et.size) << 20;
15484         }
15485
15486       if (inst.operands[2].isquad)
15487         {
15488           inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15489           inst.instruction |= LOW4 (inst.operands[2].reg);
15490           inst.instruction |= (fcond & 0x2) >> 1;
15491         }
15492       else
15493         {
15494           if (inst.operands[2].reg == REG_SP)
15495             as_tsktsk (MVE_BAD_SP);
15496           inst.instruction |= 1 << 6;
15497           inst.instruction |= (fcond & 0x2) << 4;
15498           inst.instruction |= inst.operands[2].reg;
15499         }
15500       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15501       inst.instruction |= (fcond & 0x4) << 10;
15502       inst.instruction |= (fcond & 0x1) << 7;
15503
15504     }
15505     set_pred_insn_type (VPT_INSN);
15506     now_pred.cc = 0;
15507     now_pred.mask = ((inst.instruction & 0x00400000) >> 19)
15508                     | ((inst.instruction & 0xe000) >> 13);
15509     now_pred.warn_deprecated = FALSE;
15510     now_pred.type = VECTOR_PRED;
15511     inst.is_neon = 1;
15512 }
15513
15514 static void
15515 do_mve_vcmp (void)
15516 {
15517   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
15518   if (!inst.operands[1].isreg || !inst.operands[1].isquad)
15519     first_error (_(reg_expected_msgs[REG_TYPE_MQ]));
15520   if (!inst.operands[2].present)
15521     first_error (_("MVE vector or ARM register expected"));
15522   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
15523
15524   /* Deal with 'else' conditional MVE's vcmp, it will be parsed as vcmpe.  */
15525   if ((inst.instruction & 0xffffffff) == N_MNEM_vcmpe
15526       && inst.operands[1].isquad)
15527     {
15528       inst.instruction = N_MNEM_vcmp;
15529       inst.cond = 0x10;
15530     }
15531
15532   if (inst.cond > COND_ALWAYS)
15533     inst.pred_insn_type = INSIDE_VPT_INSN;
15534   else
15535     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15536
15537   enum neon_shape rs = neon_select_shape (NS_IQQ, NS_IQR, NS_NULL);
15538   struct neon_type_el et
15539     = neon_check_type (3, rs, N_EQK, N_KEY | N_F_MVE | N_I_MVE | N_SU_32,
15540                        N_EQK);
15541
15542   constraint (rs == NS_IQR && inst.operands[2].reg == REG_PC
15543               && !inst.operands[2].iszr, BAD_PC);
15544
15545   unsigned fcond = mve_get_vcmp_vpt_cond (et);
15546
15547   inst.instruction = 0xee010f00;
15548   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15549   inst.instruction |= (fcond & 0x4) << 10;
15550   inst.instruction |= (fcond & 0x1) << 7;
15551   if (et.type == NT_float)
15552     {
15553       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext),
15554                   BAD_FPU);
15555       inst.instruction |= (et.size == 16) << 28;
15556       inst.instruction |= 0x3 << 20;
15557     }
15558   else
15559     {
15560       inst.instruction |= 1 << 28;
15561       inst.instruction |= neon_logbits (et.size) << 20;
15562     }
15563   if (inst.operands[2].isquad)
15564     {
15565       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15566       inst.instruction |= (fcond & 0x2) >> 1;
15567       inst.instruction |= LOW4 (inst.operands[2].reg);
15568     }
15569   else
15570     {
15571       if (inst.operands[2].reg == REG_SP)
15572         as_tsktsk (MVE_BAD_SP);
15573       inst.instruction |= 1 << 6;
15574       inst.instruction |= (fcond & 0x2) << 4;
15575       inst.instruction |= inst.operands[2].reg;
15576     }
15577
15578   inst.is_neon = 1;
15579   return;
15580 }
15581
15582 static void
15583 do_mve_vfmas (void)
15584 {
15585   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
15586   struct neon_type_el et
15587     = neon_check_type (3, rs, N_F_MVE | N_KEY, N_EQK, N_EQK);
15588
15589   if (inst.cond > COND_ALWAYS)
15590     inst.pred_insn_type = INSIDE_VPT_INSN;
15591   else
15592     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15593
15594   if (inst.operands[2].reg == REG_SP)
15595     as_tsktsk (MVE_BAD_SP);
15596   else if (inst.operands[2].reg == REG_PC)
15597     as_tsktsk (MVE_BAD_PC);
15598
15599   inst.instruction |= (et.size == 16) << 28;
15600   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15601   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15602   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15603   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15604   inst.instruction |= inst.operands[2].reg;
15605   inst.is_neon = 1;
15606 }
15607
15608 static void
15609 do_mve_vcmul (void)
15610 {
15611   enum neon_shape rs = neon_select_shape (NS_QQQI, NS_NULL);
15612   struct neon_type_el et
15613     = neon_check_type (3, rs, N_EQK, N_EQK, N_F_MVE | N_KEY);
15614
15615   if (inst.cond > COND_ALWAYS)
15616     inst.pred_insn_type = INSIDE_VPT_INSN;
15617   else
15618     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15619
15620   unsigned rot = inst.relocs[0].exp.X_add_number;
15621   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
15622               _("immediate out of range"));
15623
15624   if (et.size == 32 && (inst.operands[0].reg == inst.operands[1].reg
15625                         || inst.operands[0].reg == inst.operands[2].reg))
15626     as_tsktsk (BAD_MVE_SRCDEST);
15627
15628   inst.instruction |= (et.size == 32) << 28;
15629   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15630   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15631   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15632   inst.instruction |= (rot > 90) << 12;
15633   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15634   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15635   inst.instruction |= LOW4 (inst.operands[2].reg);
15636   inst.instruction |= (rot == 90 || rot == 270);
15637   inst.is_neon = 1;
15638 }
15639
15640 static void
15641 do_vfp_nsyn_cmp (void)
15642 {
15643   enum neon_shape rs;
15644   if (!inst.operands[0].isreg)
15645     {
15646       do_mve_vcmp ();
15647       return;
15648     }
15649   else
15650     {
15651       constraint (inst.operands[2].present, BAD_SYNTAX);
15652       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd),
15653                   BAD_FPU);
15654     }
15655
15656   if (inst.operands[1].isreg)
15657     {
15658       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15659       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15660
15661       if (rs == NS_FF || rs == NS_HH)
15662         {
15663           NEON_ENCODE (SINGLE, inst);
15664           do_vfp_sp_monadic ();
15665         }
15666       else
15667         {
15668           NEON_ENCODE (DOUBLE, inst);
15669           do_vfp_dp_rd_rm ();
15670         }
15671     }
15672   else
15673     {
15674       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
15675       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
15676
15677       switch (inst.instruction & 0x0fffffff)
15678         {
15679         case N_MNEM_vcmp:
15680           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
15681           break;
15682         case N_MNEM_vcmpe:
15683           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
15684           break;
15685         default:
15686           abort ();
15687         }
15688
15689       if (rs == NS_FI || rs == NS_HI)
15690         {
15691           NEON_ENCODE (SINGLE, inst);
15692           do_vfp_sp_compare_z ();
15693         }
15694       else
15695         {
15696           NEON_ENCODE (DOUBLE, inst);
15697           do_vfp_dp_rd ();
15698         }
15699     }
15700   do_vfp_cond_or_thumb ();
15701
15702   /* ARMv8.2 fp16 instruction.  */
15703   if (rs == NS_HI || rs == NS_HH)
15704     do_scalar_fp16_v82_encode ();
15705 }
15706
15707 static void
15708 nsyn_insert_sp (void)
15709 {
15710   inst.operands[1] = inst.operands[0];
15711   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
15712   inst.operands[0].reg = REG_SP;
15713   inst.operands[0].isreg = 1;
15714   inst.operands[0].writeback = 1;
15715   inst.operands[0].present = 1;
15716 }
15717
15718 static void
15719 do_vfp_nsyn_push (void)
15720 {
15721   nsyn_insert_sp ();
15722
15723   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15724               _("register list must contain at least 1 and at most 16 "
15725                 "registers"));
15726
15727   if (inst.operands[1].issingle)
15728     do_vfp_nsyn_opcode ("fstmdbs");
15729   else
15730     do_vfp_nsyn_opcode ("fstmdbd");
15731 }
15732
15733 static void
15734 do_vfp_nsyn_pop (void)
15735 {
15736   nsyn_insert_sp ();
15737
15738   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15739               _("register list must contain at least 1 and at most 16 "
15740                 "registers"));
15741
15742   if (inst.operands[1].issingle)
15743     do_vfp_nsyn_opcode ("fldmias");
15744   else
15745     do_vfp_nsyn_opcode ("fldmiad");
15746 }
15747
15748 /* Fix up Neon data-processing instructions, ORing in the correct bits for
15749    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
15750
15751 static void
15752 neon_dp_fixup (struct arm_it* insn)
15753 {
15754   unsigned int i = insn->instruction;
15755   insn->is_neon = 1;
15756
15757   if (thumb_mode)
15758     {
15759       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
15760       if (i & (1 << 24))
15761         i |= 1 << 28;
15762
15763       i &= ~(1 << 24);
15764
15765       i |= 0xef000000;
15766     }
15767   else
15768     i |= 0xf2000000;
15769
15770   insn->instruction = i;
15771 }
15772
15773 static void
15774 mve_encode_qqr (int size, int fp)
15775 {
15776   if (inst.operands[2].reg == REG_SP)
15777     as_tsktsk (MVE_BAD_SP);
15778   else if (inst.operands[2].reg == REG_PC)
15779     as_tsktsk (MVE_BAD_PC);
15780
15781   if (fp)
15782     {
15783       /* vadd.  */
15784       if (((unsigned)inst.instruction) == 0xd00)
15785         inst.instruction = 0xee300f40;
15786       /* vsub.  */
15787       else if (((unsigned)inst.instruction) == 0x200d00)
15788         inst.instruction = 0xee301f40;
15789
15790       /* Setting size which is 1 for F16 and 0 for F32.  */
15791       inst.instruction |= (size == 16) << 28;
15792     }
15793   else
15794     {
15795       /* vadd.  */
15796       if (((unsigned)inst.instruction) == 0x800)
15797         inst.instruction = 0xee010f40;
15798       /* vsub.  */
15799       else if (((unsigned)inst.instruction) == 0x1000800)
15800         inst.instruction = 0xee011f40;
15801       /* Setting bits for size.  */
15802       inst.instruction |= neon_logbits (size) << 20;
15803     }
15804   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15805   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15806   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15807   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15808   inst.instruction |= inst.operands[2].reg;
15809   inst.is_neon = 1;
15810 }
15811
15812 static void
15813 mve_encode_rqq (unsigned bit28, unsigned size)
15814 {
15815   inst.instruction |= bit28 << 28;
15816   inst.instruction |= neon_logbits (size) << 20;
15817   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15818   inst.instruction |= inst.operands[0].reg << 12;
15819   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15820   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15821   inst.instruction |= LOW4 (inst.operands[2].reg);
15822   inst.is_neon = 1;
15823 }
15824
15825 static void
15826 mve_encode_qqq (int ubit, int size)
15827 {
15828
15829   inst.instruction |= (ubit != 0) << 28;
15830   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15831   inst.instruction |= neon_logbits (size) << 20;
15832   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15833   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15834   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15835   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15836   inst.instruction |= LOW4 (inst.operands[2].reg);
15837
15838   inst.is_neon = 1;
15839 }
15840
15841 static void
15842 mve_encode_rq (unsigned bit28, unsigned size)
15843 {
15844   inst.instruction |= bit28 << 28;
15845   inst.instruction |= neon_logbits (size) << 18;
15846   inst.instruction |= inst.operands[0].reg << 12;
15847   inst.instruction |= LOW4 (inst.operands[1].reg);
15848   inst.is_neon = 1;
15849 }
15850
15851 /* Encode insns with bit pattern:
15852
15853   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
15854   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
15855
15856   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
15857   different meaning for some instruction.  */
15858
15859 static void
15860 neon_three_same (int isquad, int ubit, int size)
15861 {
15862   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15863   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15864   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15865   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15866   inst.instruction |= LOW4 (inst.operands[2].reg);
15867   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15868   inst.instruction |= (isquad != 0) << 6;
15869   inst.instruction |= (ubit != 0) << 24;
15870   if (size != -1)
15871     inst.instruction |= neon_logbits (size) << 20;
15872
15873   neon_dp_fixup (&inst);
15874 }
15875
15876 /* Encode instructions of the form:
15877
15878   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
15879   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
15880
15881   Don't write size if SIZE == -1.  */
15882
15883 static void
15884 neon_two_same (int qbit, int ubit, int size)
15885 {
15886   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15887   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15888   inst.instruction |= LOW4 (inst.operands[1].reg);
15889   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15890   inst.instruction |= (qbit != 0) << 6;
15891   inst.instruction |= (ubit != 0) << 24;
15892
15893   if (size != -1)
15894     inst.instruction |= neon_logbits (size) << 18;
15895
15896   neon_dp_fixup (&inst);
15897 }
15898
15899 /* Neon instruction encoders, in approximate order of appearance.  */
15900
15901 static void
15902 do_neon_dyadic_i_su (void)
15903 {
15904   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15905   struct neon_type_el et = neon_check_type (3, rs,
15906     N_EQK, N_EQK, N_SU_32 | N_KEY);
15907   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15908 }
15909
15910 static void
15911 do_neon_dyadic_i64_su (void)
15912 {
15913   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15914   struct neon_type_el et = neon_check_type (3, rs,
15915     N_EQK, N_EQK, N_SU_ALL | N_KEY);
15916   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15917 }
15918
15919 static void
15920 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
15921                 unsigned immbits)
15922 {
15923   unsigned size = et.size >> 3;
15924   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15925   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15926   inst.instruction |= LOW4 (inst.operands[1].reg);
15927   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15928   inst.instruction |= (isquad != 0) << 6;
15929   inst.instruction |= immbits << 16;
15930   inst.instruction |= (size >> 3) << 7;
15931   inst.instruction |= (size & 0x7) << 19;
15932   if (write_ubit)
15933     inst.instruction |= (uval != 0) << 24;
15934
15935   neon_dp_fixup (&inst);
15936 }
15937
15938 static void
15939 do_neon_shl_imm (void)
15940 {
15941   if (!inst.operands[2].isreg)
15942     {
15943       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15944       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
15945       int imm = inst.operands[2].imm;
15946
15947       constraint (imm < 0 || (unsigned)imm >= et.size,
15948                   _("immediate out of range for shift"));
15949       NEON_ENCODE (IMMED, inst);
15950       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15951     }
15952   else
15953     {
15954       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15955       struct neon_type_el et = neon_check_type (3, rs,
15956         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
15957       unsigned int tmp;
15958
15959       /* VSHL/VQSHL 3-register variants have syntax such as:
15960            vshl.xx Dd, Dm, Dn
15961          whereas other 3-register operations encoded by neon_three_same have
15962          syntax like:
15963            vadd.xx Dd, Dn, Dm
15964          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
15965          here.  */
15966       tmp = inst.operands[2].reg;
15967       inst.operands[2].reg = inst.operands[1].reg;
15968       inst.operands[1].reg = tmp;
15969       NEON_ENCODE (INTEGER, inst);
15970       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15971     }
15972 }
15973
15974 static void
15975 do_neon_qshl_imm (void)
15976 {
15977   if (!inst.operands[2].isreg)
15978     {
15979       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15980       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
15981       int imm = inst.operands[2].imm;
15982
15983       constraint (imm < 0 || (unsigned)imm >= et.size,
15984                   _("immediate out of range for shift"));
15985       NEON_ENCODE (IMMED, inst);
15986       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
15987     }
15988   else
15989     {
15990       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15991       struct neon_type_el et = neon_check_type (3, rs,
15992         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
15993       unsigned int tmp;
15994
15995       /* See note in do_neon_shl_imm.  */
15996       tmp = inst.operands[2].reg;
15997       inst.operands[2].reg = inst.operands[1].reg;
15998       inst.operands[1].reg = tmp;
15999       NEON_ENCODE (INTEGER, inst);
16000       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16001     }
16002 }
16003
16004 static void
16005 do_neon_rshl (void)
16006 {
16007   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16008   struct neon_type_el et = neon_check_type (3, rs,
16009     N_EQK, N_EQK, N_SU_ALL | N_KEY);
16010   unsigned int tmp;
16011
16012   tmp = inst.operands[2].reg;
16013   inst.operands[2].reg = inst.operands[1].reg;
16014   inst.operands[1].reg = tmp;
16015   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
16016 }
16017
16018 static int
16019 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
16020 {
16021   /* Handle .I8 pseudo-instructions.  */
16022   if (size == 8)
16023     {
16024       /* Unfortunately, this will make everything apart from zero out-of-range.
16025          FIXME is this the intended semantics? There doesn't seem much point in
16026          accepting .I8 if so.  */
16027       immediate |= immediate << 8;
16028       size = 16;
16029     }
16030
16031   if (size >= 32)
16032     {
16033       if (immediate == (immediate & 0x000000ff))
16034         {
16035           *immbits = immediate;
16036           return 0x1;
16037         }
16038       else if (immediate == (immediate & 0x0000ff00))
16039         {
16040           *immbits = immediate >> 8;
16041           return 0x3;
16042         }
16043       else if (immediate == (immediate & 0x00ff0000))
16044         {
16045           *immbits = immediate >> 16;
16046           return 0x5;
16047         }
16048       else if (immediate == (immediate & 0xff000000))
16049         {
16050           *immbits = immediate >> 24;
16051           return 0x7;
16052         }
16053       if ((immediate & 0xffff) != (immediate >> 16))
16054         goto bad_immediate;
16055       immediate &= 0xffff;
16056     }
16057
16058   if (immediate == (immediate & 0x000000ff))
16059     {
16060       *immbits = immediate;
16061       return 0x9;
16062     }
16063   else if (immediate == (immediate & 0x0000ff00))
16064     {
16065       *immbits = immediate >> 8;
16066       return 0xb;
16067     }
16068
16069   bad_immediate:
16070   first_error (_("immediate value out of range"));
16071   return FAIL;
16072 }
16073
16074 enum vfp_or_neon_is_neon_bits
16075 {
16076 NEON_CHECK_CC = 1,
16077 NEON_CHECK_ARCH = 2,
16078 NEON_CHECK_ARCH8 = 4
16079 };
16080
16081 /* Call this function if an instruction which may have belonged to the VFP or
16082  Neon instruction sets, but turned out to be a Neon instruction (due to the
16083  operand types involved, etc.). We have to check and/or fix-up a couple of
16084  things:
16085
16086    - Make sure the user hasn't attempted to make a Neon instruction
16087      conditional.
16088    - Alter the value in the condition code field if necessary.
16089    - Make sure that the arch supports Neon instructions.
16090
16091  Which of these operations take place depends on bits from enum
16092  vfp_or_neon_is_neon_bits.
16093
16094  WARNING: This function has side effects! If NEON_CHECK_CC is used and the
16095  current instruction's condition is COND_ALWAYS, the condition field is
16096  changed to inst.uncond_value.  This is necessary because instructions shared
16097  between VFP and Neon may be conditional for the VFP variants only, and the
16098  unconditional Neon version must have, e.g., 0xF in the condition field.  */
16099
16100 static int
16101 vfp_or_neon_is_neon (unsigned check)
16102 {
16103 /* Conditions are always legal in Thumb mode (IT blocks).  */
16104 if (!thumb_mode && (check & NEON_CHECK_CC))
16105   {
16106     if (inst.cond != COND_ALWAYS)
16107       {
16108         first_error (_(BAD_COND));
16109         return FAIL;
16110       }
16111     if (inst.uncond_value != -1)
16112       inst.instruction |= inst.uncond_value << 28;
16113   }
16114
16115
16116   if (((check & NEON_CHECK_ARCH) && !mark_feature_used (&fpu_neon_ext_v1))
16117       || ((check & NEON_CHECK_ARCH8)
16118           && !mark_feature_used (&fpu_neon_ext_armv8)))
16119     {
16120       first_error (_(BAD_FPU));
16121       return FAIL;
16122     }
16123
16124 return SUCCESS;
16125 }
16126
16127 static int
16128 check_simd_pred_availability (int fp, unsigned check)
16129 {
16130 if (inst.cond > COND_ALWAYS)
16131   {
16132     if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16133       {
16134         inst.error = BAD_FPU;
16135         return 1;
16136       }
16137     inst.pred_insn_type = INSIDE_VPT_INSN;
16138   }
16139 else if (inst.cond < COND_ALWAYS)
16140   {
16141     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16142       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16143     else if (vfp_or_neon_is_neon (check) == FAIL)
16144       return 2;
16145   }
16146 else
16147   {
16148     if (!ARM_CPU_HAS_FEATURE (cpu_variant, fp ? mve_fp_ext : mve_ext)
16149         && vfp_or_neon_is_neon (check) == FAIL)
16150       return 3;
16151
16152     if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16153       inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16154   }
16155 return 0;
16156 }
16157
16158 static void
16159 do_neon_logic (void)
16160 {
16161   if (inst.operands[2].present && inst.operands[2].isreg)
16162     {
16163       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16164       if (rs == NS_QQQ
16165           && check_simd_pred_availability (0, NEON_CHECK_ARCH | NEON_CHECK_CC)
16166           == FAIL)
16167         return;
16168       else if (rs != NS_QQQ
16169                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
16170         first_error (BAD_FPU);
16171
16172       neon_check_type (3, rs, N_IGNORE_TYPE);
16173       /* U bit and size field were set as part of the bitmask.  */
16174       NEON_ENCODE (INTEGER, inst);
16175       neon_three_same (neon_quad (rs), 0, -1);
16176     }
16177   else
16178     {
16179       const int three_ops_form = (inst.operands[2].present
16180                                   && !inst.operands[2].isreg);
16181       const int immoperand = (three_ops_form ? 2 : 1);
16182       enum neon_shape rs = (three_ops_form
16183                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
16184                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
16185       /* Because neon_select_shape makes the second operand a copy of the first
16186          if the second operand is not present.  */
16187       if (rs == NS_QQI
16188           && check_simd_pred_availability (0, NEON_CHECK_ARCH | NEON_CHECK_CC)
16189           == FAIL)
16190         return;
16191       else if (rs != NS_QQI
16192                && !ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
16193         first_error (BAD_FPU);
16194
16195       struct neon_type_el et;
16196       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16197         et = neon_check_type (2, rs, N_I32 | N_I16 | N_KEY, N_EQK);
16198       else
16199         et = neon_check_type (2, rs, N_I8 | N_I16 | N_I32 | N_I64 | N_F32
16200                               | N_KEY, N_EQK);
16201
16202       if (et.type == NT_invtype)
16203         return;
16204       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
16205       unsigned immbits;
16206       int cmode;
16207
16208
16209       if (three_ops_form)
16210         constraint (inst.operands[0].reg != inst.operands[1].reg,
16211                     _("first and second operands shall be the same register"));
16212
16213       NEON_ENCODE (IMMED, inst);
16214
16215       immbits = inst.operands[immoperand].imm;
16216       if (et.size == 64)
16217         {
16218           /* .i64 is a pseudo-op, so the immediate must be a repeating
16219              pattern.  */
16220           if (immbits != (inst.operands[immoperand].regisimm ?
16221                           inst.operands[immoperand].reg : 0))
16222             {
16223               /* Set immbits to an invalid constant.  */
16224               immbits = 0xdeadbeef;
16225             }
16226         }
16227
16228       switch (opcode)
16229         {
16230         case N_MNEM_vbic:
16231           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
16232           break;
16233
16234         case N_MNEM_vorr:
16235           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
16236           break;
16237
16238         case N_MNEM_vand:
16239           /* Pseudo-instruction for VBIC.  */
16240           neon_invert_size (&immbits, 0, et.size);
16241           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
16242           break;
16243
16244         case N_MNEM_vorn:
16245           /* Pseudo-instruction for VORR.  */
16246           neon_invert_size (&immbits, 0, et.size);
16247           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
16248           break;
16249
16250         default:
16251           abort ();
16252         }
16253
16254       if (cmode == FAIL)
16255         return;
16256
16257       inst.instruction |= neon_quad (rs) << 6;
16258       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16259       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16260       inst.instruction |= cmode << 8;
16261       neon_write_immbits (immbits);
16262
16263       neon_dp_fixup (&inst);
16264     }
16265 }
16266
16267 static void
16268 do_neon_bitfield (void)
16269 {
16270   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16271   neon_check_type (3, rs, N_IGNORE_TYPE);
16272   neon_three_same (neon_quad (rs), 0, -1);
16273 }
16274
16275 static void
16276 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
16277                   unsigned destbits)
16278 {
16279   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
16280   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
16281                                             types | N_KEY);
16282   if (et.type == NT_float)
16283     {
16284       NEON_ENCODE (FLOAT, inst);
16285       if (rs == NS_QQR)
16286         mve_encode_qqr (et.size, 1);
16287       else
16288         neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
16289     }
16290   else
16291     {
16292       NEON_ENCODE (INTEGER, inst);
16293       if (rs == NS_QQR)
16294         mve_encode_qqr (et.size, 0);
16295       else
16296         neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
16297     }
16298 }
16299
16300
16301 static void
16302 do_neon_dyadic_if_su_d (void)
16303 {
16304   /* This version only allow D registers, but that constraint is enforced during
16305      operand parsing so we don't need to do anything extra here.  */
16306   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
16307 }
16308
16309 static void
16310 do_neon_dyadic_if_i_d (void)
16311 {
16312   /* The "untyped" case can't happen. Do this to stop the "U" bit being
16313      affected if we specify unsigned args.  */
16314   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
16315 }
16316
16317 static void
16318 do_mve_vstr_vldr_QI (int size, int elsize, int load)
16319 {
16320   constraint (size < 32, BAD_ADDR_MODE);
16321   constraint (size != elsize, BAD_EL_TYPE);
16322   constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
16323   constraint (!inst.operands[1].preind, BAD_ADDR_MODE);
16324   constraint (load && inst.operands[0].reg == inst.operands[1].reg,
16325               _("destination register and offset register may not be the"
16326                 " same"));
16327
16328   int imm = inst.relocs[0].exp.X_add_number;
16329   int add = 1;
16330   if (imm < 0)
16331     {
16332       add = 0;
16333       imm = -imm;
16334     }
16335   constraint ((imm % (size / 8) != 0)
16336               || imm > (0x7f << neon_logbits (size)),
16337               (size == 32) ? _("immediate must be a multiple of 4 in the"
16338                                " range of +/-[0,508]")
16339                            : _("immediate must be a multiple of 8 in the"
16340                                " range of +/-[0,1016]"));
16341   inst.instruction |= 0x11 << 24;
16342   inst.instruction |= add << 23;
16343   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16344   inst.instruction |= inst.operands[1].writeback << 21;
16345   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16346   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16347   inst.instruction |= 1 << 12;
16348   inst.instruction |= (size == 64) << 8;
16349   inst.instruction &= 0xffffff00;
16350   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16351   inst.instruction |= imm >> neon_logbits (size);
16352 }
16353
16354 static void
16355 do_mve_vstr_vldr_RQ (int size, int elsize, int load)
16356 {
16357     unsigned os = inst.operands[1].imm >> 5;
16358     constraint (os != 0 && size == 8,
16359                 _("can not shift offsets when accessing less than half-word"));
16360     constraint (os && os != neon_logbits (size),
16361                 _("shift immediate must be 1, 2 or 3 for half-word, word"
16362                   " or double-word accesses respectively"));
16363     if (inst.operands[1].reg == REG_PC)
16364       as_tsktsk (MVE_BAD_PC);
16365
16366     switch (size)
16367       {
16368       case 8:
16369         constraint (elsize >= 64, BAD_EL_TYPE);
16370         break;
16371       case 16:
16372         constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
16373         break;
16374       case 32:
16375       case 64:
16376         constraint (elsize != size, BAD_EL_TYPE);
16377         break;
16378       default:
16379         break;
16380       }
16381     constraint (inst.operands[1].writeback || !inst.operands[1].preind,
16382                 BAD_ADDR_MODE);
16383     if (load)
16384       {
16385         constraint (inst.operands[0].reg == (inst.operands[1].imm & 0x1f),
16386                     _("destination register and offset register may not be"
16387                     " the same"));
16388         constraint (size == elsize && inst.vectype.el[0].type != NT_unsigned,
16389                     BAD_EL_TYPE);
16390         constraint (inst.vectype.el[0].type != NT_unsigned
16391                     && inst.vectype.el[0].type != NT_signed, BAD_EL_TYPE);
16392         inst.instruction |= (inst.vectype.el[0].type == NT_unsigned) << 28;
16393       }
16394     else
16395       {
16396         constraint (inst.vectype.el[0].type != NT_untyped, BAD_EL_TYPE);
16397       }
16398
16399     inst.instruction |= 1 << 23;
16400     inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16401     inst.instruction |= inst.operands[1].reg << 16;
16402     inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16403     inst.instruction |= neon_logbits (elsize) << 7;
16404     inst.instruction |= HI1 (inst.operands[1].imm) << 5;
16405     inst.instruction |= LOW4 (inst.operands[1].imm);
16406     inst.instruction |= !!os;
16407 }
16408
16409 static void
16410 do_mve_vstr_vldr_RI (int size, int elsize, int load)
16411 {
16412   enum neon_el_type type = inst.vectype.el[0].type;
16413
16414   constraint (size >= 64, BAD_ADDR_MODE);
16415   switch (size)
16416     {
16417     case 16:
16418       constraint (elsize < 16 || elsize >= 64, BAD_EL_TYPE);
16419       break;
16420     case 32:
16421       constraint (elsize != size, BAD_EL_TYPE);
16422       break;
16423     default:
16424       break;
16425     }
16426   if (load)
16427     {
16428       constraint (elsize != size && type != NT_unsigned
16429                   && type != NT_signed, BAD_EL_TYPE);
16430     }
16431   else
16432     {
16433       constraint (elsize != size && type != NT_untyped, BAD_EL_TYPE);
16434     }
16435
16436   int imm = inst.relocs[0].exp.X_add_number;
16437   int add = 1;
16438   if (imm < 0)
16439     {
16440       add = 0;
16441       imm = -imm;
16442     }
16443
16444   if ((imm % (size / 8) != 0) || imm > (0x7f << neon_logbits (size)))
16445     {
16446       switch (size)
16447         {
16448         case 8:
16449           constraint (1, _("immediate must be in the range of +/-[0,127]"));
16450           break;
16451         case 16:
16452           constraint (1, _("immediate must be a multiple of 2 in the"
16453                            " range of +/-[0,254]"));
16454           break;
16455         case 32:
16456           constraint (1, _("immediate must be a multiple of 4 in the"
16457                            " range of +/-[0,508]"));
16458           break;
16459         }
16460     }
16461
16462   if (size != elsize)
16463     {
16464       constraint (inst.operands[1].reg > 7, BAD_HIREG);
16465       constraint (inst.operands[0].reg > 14,
16466                   _("MVE vector register in the range [Q0..Q7] expected"));
16467       inst.instruction |= (load && type == NT_unsigned) << 28;
16468       inst.instruction |= (size == 16) << 19;
16469       inst.instruction |= neon_logbits (elsize) << 7;
16470     }
16471   else
16472     {
16473       if (inst.operands[1].reg == REG_PC)
16474         as_tsktsk (MVE_BAD_PC);
16475       else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
16476         as_tsktsk (MVE_BAD_SP);
16477       inst.instruction |= 1 << 12;
16478       inst.instruction |= neon_logbits (size) << 7;
16479     }
16480   inst.instruction |= inst.operands[1].preind << 24;
16481   inst.instruction |= add << 23;
16482   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16483   inst.instruction |= inst.operands[1].writeback << 21;
16484   inst.instruction |= inst.operands[1].reg << 16;
16485   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16486   inst.instruction &= 0xffffff80;
16487   inst.instruction |= imm >> neon_logbits (size);
16488
16489 }
16490
16491 static void
16492 do_mve_vstr_vldr (void)
16493 {
16494   unsigned size;
16495   int load = 0;
16496
16497   if (inst.cond > COND_ALWAYS)
16498     inst.pred_insn_type = INSIDE_VPT_INSN;
16499   else
16500     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16501
16502   switch (inst.instruction)
16503     {
16504     default:
16505       gas_assert (0);
16506       break;
16507     case M_MNEM_vldrb:
16508       load = 1;
16509       /* fall through.  */
16510     case M_MNEM_vstrb:
16511       size = 8;
16512       break;
16513     case M_MNEM_vldrh:
16514       load = 1;
16515       /* fall through.  */
16516     case M_MNEM_vstrh:
16517       size = 16;
16518       break;
16519     case M_MNEM_vldrw:
16520       load = 1;
16521       /* fall through.  */
16522     case M_MNEM_vstrw:
16523       size = 32;
16524       break;
16525     case M_MNEM_vldrd:
16526       load = 1;
16527       /* fall through.  */
16528     case M_MNEM_vstrd:
16529       size = 64;
16530       break;
16531     }
16532   unsigned elsize = inst.vectype.el[0].size;
16533
16534   if (inst.operands[1].isquad)
16535     {
16536       /* We are dealing with [Q, imm]{!} cases.  */
16537       do_mve_vstr_vldr_QI (size, elsize, load);
16538     }
16539   else
16540     {
16541       if (inst.operands[1].immisreg == 2)
16542         {
16543           /* We are dealing with [R, Q, {UXTW #os}] cases.  */
16544           do_mve_vstr_vldr_RQ (size, elsize, load);
16545         }
16546       else if (!inst.operands[1].immisreg)
16547         {
16548           /* We are dealing with [R, Imm]{!}/[R], Imm cases.  */
16549           do_mve_vstr_vldr_RI (size, elsize, load);
16550         }
16551       else
16552         constraint (1, BAD_ADDR_MODE);
16553     }
16554
16555   inst.is_neon = 1;
16556 }
16557
16558 static void
16559 do_mve_vst_vld (void)
16560 {
16561   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
16562     return;
16563
16564   constraint (!inst.operands[1].preind || inst.relocs[0].exp.X_add_symbol != 0
16565               || inst.relocs[0].exp.X_add_number != 0
16566               || inst.operands[1].immisreg != 0,
16567               BAD_ADDR_MODE);
16568   constraint (inst.vectype.el[0].size > 32, BAD_EL_TYPE);
16569   if (inst.operands[1].reg == REG_PC)
16570     as_tsktsk (MVE_BAD_PC);
16571   else if (inst.operands[1].reg == REG_SP && inst.operands[1].writeback)
16572     as_tsktsk (MVE_BAD_SP);
16573
16574
16575   /* These instructions are one of the "exceptions" mentioned in
16576      handle_pred_state.  They are MVE instructions that are not VPT compatible
16577      and do not accept a VPT code, thus appending such a code is a syntax
16578      error.  */
16579   if (inst.cond > COND_ALWAYS)
16580     first_error (BAD_SYNTAX);
16581   /* If we append a scalar condition code we can set this to
16582      MVE_OUTSIDE_PRED_INSN as it will also lead to a syntax error.  */
16583   else if (inst.cond < COND_ALWAYS)
16584     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16585   else
16586     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
16587
16588   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16589   inst.instruction |= inst.operands[1].writeback << 21;
16590   inst.instruction |= inst.operands[1].reg << 16;
16591   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16592   inst.instruction |= neon_logbits (inst.vectype.el[0].size) << 7;
16593   inst.is_neon = 1;
16594 }
16595
16596 static void
16597 do_mve_vaddlv (void)
16598 {
16599   enum neon_shape rs = neon_select_shape (NS_RRQ, NS_NULL);
16600   struct neon_type_el et
16601     = neon_check_type (3, rs, N_EQK, N_EQK, N_S32 | N_U32 | N_KEY);
16602
16603   if (et.type == NT_invtype)
16604     first_error (BAD_EL_TYPE);
16605
16606   if (inst.cond > COND_ALWAYS)
16607     inst.pred_insn_type = INSIDE_VPT_INSN;
16608   else
16609     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16610
16611   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
16612
16613   inst.instruction |= (et.type == NT_unsigned) << 28;
16614   inst.instruction |= inst.operands[1].reg << 19;
16615   inst.instruction |= inst.operands[0].reg << 12;
16616   inst.instruction |= inst.operands[2].reg;
16617   inst.is_neon = 1;
16618 }
16619
16620 static void
16621 do_neon_dyadic_if_su (void)
16622 {
16623   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
16624   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
16625                                             N_SUF_32 | N_KEY);
16626
16627   if (check_simd_pred_availability (et.type == NT_float,
16628                                     NEON_CHECK_ARCH | NEON_CHECK_CC))
16629     return;
16630
16631   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
16632 }
16633
16634 static void
16635 do_neon_addsub_if_i (void)
16636 {
16637   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
16638       && try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
16639     return;
16640
16641   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
16642   struct neon_type_el et = neon_check_type (3, rs, N_EQK,
16643                                             N_EQK, N_IF_32 | N_I64 | N_KEY);
16644
16645   constraint (rs == NS_QQR && et.size == 64, BAD_FPU);
16646   /* If we are parsing Q registers and the element types match MVE, which NEON
16647      also supports, then we must check whether this is an instruction that can
16648      be used by both MVE/NEON.  This distinction can be made based on whether
16649      they are predicated or not.  */
16650   if ((rs == NS_QQQ || rs == NS_QQR) && et.size != 64)
16651     {
16652       if (check_simd_pred_availability (et.type == NT_float,
16653                                         NEON_CHECK_ARCH | NEON_CHECK_CC))
16654         return;
16655     }
16656   else
16657     {
16658       /* If they are either in a D register or are using an unsupported.  */
16659       if (rs != NS_QQR
16660           && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16661         return;
16662     }
16663
16664   /* The "untyped" case can't happen. Do this to stop the "U" bit being
16665      affected if we specify unsigned args.  */
16666   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
16667 }
16668
16669 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
16670    result to be:
16671      V<op> A,B     (A is operand 0, B is operand 2)
16672    to mean:
16673      V<op> A,B,A
16674    not:
16675      V<op> A,B,B
16676    so handle that case specially.  */
16677
16678 static void
16679 neon_exchange_operands (void)
16680 {
16681   if (inst.operands[1].present)
16682     {
16683       void *scratch = xmalloc (sizeof (inst.operands[0]));
16684
16685       /* Swap operands[1] and operands[2].  */
16686       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
16687       inst.operands[1] = inst.operands[2];
16688       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
16689       free (scratch);
16690     }
16691   else
16692     {
16693       inst.operands[1] = inst.operands[2];
16694       inst.operands[2] = inst.operands[0];
16695     }
16696 }
16697
16698 static void
16699 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
16700 {
16701   if (inst.operands[2].isreg)
16702     {
16703       if (invert)
16704         neon_exchange_operands ();
16705       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
16706     }
16707   else
16708     {
16709       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16710       struct neon_type_el et = neon_check_type (2, rs,
16711         N_EQK | N_SIZ, immtypes | N_KEY);
16712
16713       NEON_ENCODE (IMMED, inst);
16714       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16715       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16716       inst.instruction |= LOW4 (inst.operands[1].reg);
16717       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16718       inst.instruction |= neon_quad (rs) << 6;
16719       inst.instruction |= (et.type == NT_float) << 10;
16720       inst.instruction |= neon_logbits (et.size) << 18;
16721
16722       neon_dp_fixup (&inst);
16723     }
16724 }
16725
16726 static void
16727 do_neon_cmp (void)
16728 {
16729   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
16730 }
16731
16732 static void
16733 do_neon_cmp_inv (void)
16734 {
16735   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
16736 }
16737
16738 static void
16739 do_neon_ceq (void)
16740 {
16741   neon_compare (N_IF_32, N_IF_32, FALSE);
16742 }
16743
16744 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
16745    scalars, which are encoded in 5 bits, M : Rm.
16746    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
16747    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
16748    index in M.
16749
16750    Dot Product instructions are similar to multiply instructions except elsize
16751    should always be 32.
16752
16753    This function translates SCALAR, which is GAS's internal encoding of indexed
16754    scalar register, to raw encoding.  There is also register and index range
16755    check based on ELSIZE.  */
16756
16757 static unsigned
16758 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
16759 {
16760   unsigned regno = NEON_SCALAR_REG (scalar);
16761   unsigned elno = NEON_SCALAR_INDEX (scalar);
16762
16763   switch (elsize)
16764     {
16765     case 16:
16766       if (regno > 7 || elno > 3)
16767         goto bad_scalar;
16768       return regno | (elno << 3);
16769
16770     case 32:
16771       if (regno > 15 || elno > 1)
16772         goto bad_scalar;
16773       return regno | (elno << 4);
16774
16775     default:
16776     bad_scalar:
16777       first_error (_("scalar out of range for multiply instruction"));
16778     }
16779
16780   return 0;
16781 }
16782
16783 /* Encode multiply / multiply-accumulate scalar instructions.  */
16784
16785 static void
16786 neon_mul_mac (struct neon_type_el et, int ubit)
16787 {
16788   unsigned scalar;
16789
16790   /* Give a more helpful error message if we have an invalid type.  */
16791   if (et.type == NT_invtype)
16792     return;
16793
16794   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
16795   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16796   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16797   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16798   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16799   inst.instruction |= LOW4 (scalar);
16800   inst.instruction |= HI1 (scalar) << 5;
16801   inst.instruction |= (et.type == NT_float) << 8;
16802   inst.instruction |= neon_logbits (et.size) << 20;
16803   inst.instruction |= (ubit != 0) << 24;
16804
16805   neon_dp_fixup (&inst);
16806 }
16807
16808 static void
16809 do_neon_mac_maybe_scalar (void)
16810 {
16811   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
16812     return;
16813
16814   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16815     return;
16816
16817   if (inst.operands[2].isscalar)
16818     {
16819       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
16820       struct neon_type_el et = neon_check_type (3, rs,
16821         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
16822       NEON_ENCODE (SCALAR, inst);
16823       neon_mul_mac (et, neon_quad (rs));
16824     }
16825   else
16826     {
16827       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
16828          affected if we specify unsigned args.  */
16829       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
16830     }
16831 }
16832
16833 static void
16834 do_neon_fmac (void)
16835 {
16836   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
16837     return;
16838
16839   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16840     return;
16841
16842   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
16843 }
16844
16845 static void
16846 do_neon_tst (void)
16847 {
16848   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16849   struct neon_type_el et = neon_check_type (3, rs,
16850     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
16851   neon_three_same (neon_quad (rs), 0, et.size);
16852 }
16853
16854 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
16855    same types as the MAC equivalents. The polynomial type for this instruction
16856    is encoded the same as the integer type.  */
16857
16858 static void
16859 do_neon_mul (void)
16860 {
16861   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
16862     return;
16863
16864   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16865     return;
16866
16867   if (inst.operands[2].isscalar)
16868     do_neon_mac_maybe_scalar ();
16869   else
16870     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
16871 }
16872
16873 static void
16874 do_neon_qdmulh (void)
16875 {
16876   if (inst.operands[2].isscalar)
16877     {
16878       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
16879       struct neon_type_el et = neon_check_type (3, rs,
16880         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
16881       NEON_ENCODE (SCALAR, inst);
16882       neon_mul_mac (et, neon_quad (rs));
16883     }
16884   else
16885     {
16886       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
16887       struct neon_type_el et = neon_check_type (3, rs,
16888         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
16889       NEON_ENCODE (INTEGER, inst);
16890       /* The U bit (rounding) comes from bit mask.  */
16891       neon_three_same (neon_quad (rs), 0, et.size);
16892     }
16893 }
16894
16895 static void
16896 do_mve_vaddv (void)
16897 {
16898   enum neon_shape rs = neon_select_shape (NS_RQ, NS_NULL);
16899   struct neon_type_el et
16900     = neon_check_type (2, rs, N_EQK,  N_SU_32 | N_KEY);
16901
16902   if (et.type == NT_invtype)
16903     first_error (BAD_EL_TYPE);
16904
16905   if (inst.cond > COND_ALWAYS)
16906     inst.pred_insn_type = INSIDE_VPT_INSN;
16907   else
16908     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16909
16910   constraint (inst.operands[1].reg > 14, MVE_BAD_QREG);
16911
16912   mve_encode_rq (et.type == NT_unsigned, et.size);
16913 }
16914
16915 static void
16916 do_mve_vadc (void)
16917 {
16918   enum neon_shape rs = neon_select_shape (NS_QQQ, NS_NULL);
16919   struct neon_type_el et
16920     = neon_check_type (3, rs, N_KEY | N_I32, N_EQK, N_EQK);
16921
16922   if (et.type == NT_invtype)
16923     first_error (BAD_EL_TYPE);
16924
16925   if (inst.cond > COND_ALWAYS)
16926     inst.pred_insn_type = INSIDE_VPT_INSN;
16927   else
16928     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16929
16930   mve_encode_qqq (0, 64);
16931 }
16932
16933 static void
16934 do_mve_vbrsr (void)
16935 {
16936   enum neon_shape rs = neon_select_shape (NS_QQR, NS_NULL);
16937   struct neon_type_el et
16938     = neon_check_type (3, rs, N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
16939
16940   if (inst.cond > COND_ALWAYS)
16941     inst.pred_insn_type = INSIDE_VPT_INSN;
16942   else
16943     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16944
16945   mve_encode_qqr (et.size, 0);
16946 }
16947
16948 static void
16949 do_mve_vsbc (void)
16950 {
16951   neon_check_type (3, NS_QQQ, N_EQK, N_EQK, N_I32 | N_KEY);
16952
16953   if (inst.cond > COND_ALWAYS)
16954     inst.pred_insn_type = INSIDE_VPT_INSN;
16955   else
16956     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
16957
16958   mve_encode_qqq (1, 64);
16959 }
16960
16961 static void
16962 do_mve_vmull (void)
16963 {
16964
16965   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_DDS,
16966                                           NS_QQS, NS_QQQ, NS_QQR, NS_NULL);
16967   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
16968       && inst.cond == COND_ALWAYS
16969       && ((unsigned)inst.instruction) == M_MNEM_vmullt)
16970     {
16971       if (rs == NS_QQQ)
16972         {
16973
16974           struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
16975                                                     N_SUF_32 | N_F64 | N_P8
16976                                                     | N_P16 | N_I_MVE | N_KEY);
16977           if (((et.type == NT_poly) && et.size == 8
16978                && ARM_CPU_IS_ANY (cpu_variant))
16979               || (et.type == NT_integer) || (et.type == NT_float))
16980             goto neon_vmul;
16981         }
16982       else
16983         goto neon_vmul;
16984     }
16985
16986   constraint (rs != NS_QQQ, BAD_FPU);
16987   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
16988                                             N_SU_32 | N_P8 | N_P16 | N_KEY);
16989
16990   /* We are dealing with MVE's vmullt.  */
16991   if (et.size == 32
16992       && (inst.operands[0].reg == inst.operands[1].reg
16993           || inst.operands[0].reg == inst.operands[2].reg))
16994     as_tsktsk (BAD_MVE_SRCDEST);
16995
16996   if (inst.cond > COND_ALWAYS)
16997     inst.pred_insn_type = INSIDE_VPT_INSN;
16998   else
16999     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17000
17001   if (et.type == NT_poly)
17002     mve_encode_qqq (neon_logbits (et.size), 64);
17003   else
17004     mve_encode_qqq (et.type == NT_unsigned, et.size);
17005
17006   return;
17007
17008 neon_vmul:
17009   inst.instruction = N_MNEM_vmul;
17010   inst.cond = 0xb;
17011   if (thumb_mode)
17012     inst.pred_insn_type = INSIDE_IT_INSN;
17013   do_neon_mul ();
17014 }
17015
17016 static void
17017 do_mve_vabav (void)
17018 {
17019   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
17020
17021   if (rs == NS_NULL)
17022     return;
17023
17024   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
17025     return;
17026
17027   struct neon_type_el et = neon_check_type (2, NS_NULL, N_EQK, N_KEY | N_S8
17028                                             | N_S16 | N_S32 | N_U8 | N_U16
17029                                             | N_U32);
17030
17031   if (inst.cond > COND_ALWAYS)
17032     inst.pred_insn_type = INSIDE_VPT_INSN;
17033   else
17034     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17035
17036   mve_encode_rqq (et.type == NT_unsigned, et.size);
17037 }
17038
17039 static void
17040 do_mve_vmladav (void)
17041 {
17042   enum neon_shape rs = neon_select_shape (NS_RQQ, NS_NULL);
17043   struct neon_type_el et = neon_check_type (3, rs,
17044                                             N_EQK, N_EQK, N_SU_MVE | N_KEY);
17045
17046   if (et.type == NT_unsigned
17047       && (inst.instruction == M_MNEM_vmladavx
17048           || inst.instruction == M_MNEM_vmladavax
17049           || inst.instruction == M_MNEM_vmlsdav
17050           || inst.instruction == M_MNEM_vmlsdava
17051           || inst.instruction == M_MNEM_vmlsdavx
17052           || inst.instruction == M_MNEM_vmlsdavax))
17053     first_error (BAD_SIMD_TYPE);
17054
17055   constraint (inst.operands[2].reg > 14,
17056               _("MVE vector register in the range [Q0..Q7] expected"));
17057
17058   if (inst.cond > COND_ALWAYS)
17059     inst.pred_insn_type = INSIDE_VPT_INSN;
17060   else
17061     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
17062
17063   if (inst.instruction == M_MNEM_vmlsdav
17064       || inst.instruction == M_MNEM_vmlsdava
17065       || inst.instruction == M_MNEM_vmlsdavx
17066       || inst.instruction == M_MNEM_vmlsdavax)
17067     inst.instruction |= (et.size == 8) << 28;
17068   else
17069     inst.instruction |= (et.size == 8) << 8;
17070
17071   mve_encode_rqq (et.type == NT_unsigned, 64);
17072   inst.instruction |= (et.size == 32) << 16;
17073 }
17074
17075 static void
17076 do_neon_qrdmlah (void)
17077 {
17078   /* Check we're on the correct architecture.  */
17079   if (!mark_feature_used (&fpu_neon_ext_armv8))
17080     inst.error =
17081       _("instruction form not available on this architecture.");
17082   else if (!mark_feature_used (&fpu_neon_ext_v8_1))
17083     {
17084       as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
17085       record_feature_use (&fpu_neon_ext_v8_1);
17086     }
17087
17088   if (inst.operands[2].isscalar)
17089     {
17090       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17091       struct neon_type_el et = neon_check_type (3, rs,
17092         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17093       NEON_ENCODE (SCALAR, inst);
17094       neon_mul_mac (et, neon_quad (rs));
17095     }
17096   else
17097     {
17098       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17099       struct neon_type_el et = neon_check_type (3, rs,
17100         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
17101       NEON_ENCODE (INTEGER, inst);
17102       /* The U bit (rounding) comes from bit mask.  */
17103       neon_three_same (neon_quad (rs), 0, et.size);
17104     }
17105 }
17106
17107 static void
17108 do_neon_fcmp_absolute (void)
17109 {
17110   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17111   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
17112                                             N_F_16_32 | N_KEY);
17113   /* Size field comes from bit mask.  */
17114   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
17115 }
17116
17117 static void
17118 do_neon_fcmp_absolute_inv (void)
17119 {
17120   neon_exchange_operands ();
17121   do_neon_fcmp_absolute ();
17122 }
17123
17124 static void
17125 do_neon_step (void)
17126 {
17127   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17128   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
17129                                             N_F_16_32 | N_KEY);
17130   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
17131 }
17132
17133 static void
17134 do_neon_abs_neg (void)
17135 {
17136   enum neon_shape rs;
17137   struct neon_type_el et;
17138
17139   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
17140     return;
17141
17142   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17143   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
17144
17145   if (check_simd_pred_availability (et.type == NT_float,
17146                                     NEON_CHECK_ARCH | NEON_CHECK_CC))
17147     return;
17148
17149   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17150   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17151   inst.instruction |= LOW4 (inst.operands[1].reg);
17152   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17153   inst.instruction |= neon_quad (rs) << 6;
17154   inst.instruction |= (et.type == NT_float) << 10;
17155   inst.instruction |= neon_logbits (et.size) << 18;
17156
17157   neon_dp_fixup (&inst);
17158 }
17159
17160 static void
17161 do_neon_sli (void)
17162 {
17163   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17164   struct neon_type_el et = neon_check_type (2, rs,
17165     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
17166   int imm = inst.operands[2].imm;
17167   constraint (imm < 0 || (unsigned)imm >= et.size,
17168               _("immediate out of range for insert"));
17169   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
17170 }
17171
17172 static void
17173 do_neon_sri (void)
17174 {
17175   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17176   struct neon_type_el et = neon_check_type (2, rs,
17177     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
17178   int imm = inst.operands[2].imm;
17179   constraint (imm < 1 || (unsigned)imm > et.size,
17180               _("immediate out of range for insert"));
17181   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
17182 }
17183
17184 static void
17185 do_neon_qshlu_imm (void)
17186 {
17187   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17188   struct neon_type_el et = neon_check_type (2, rs,
17189     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
17190   int imm = inst.operands[2].imm;
17191   constraint (imm < 0 || (unsigned)imm >= et.size,
17192               _("immediate out of range for shift"));
17193   /* Only encodes the 'U present' variant of the instruction.
17194      In this case, signed types have OP (bit 8) set to 0.
17195      Unsigned types have OP set to 1.  */
17196   inst.instruction |= (et.type == NT_unsigned) << 8;
17197   /* The rest of the bits are the same as other immediate shifts.  */
17198   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
17199 }
17200
17201 static void
17202 do_neon_qmovn (void)
17203 {
17204   struct neon_type_el et = neon_check_type (2, NS_DQ,
17205     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
17206   /* Saturating move where operands can be signed or unsigned, and the
17207      destination has the same signedness.  */
17208   NEON_ENCODE (INTEGER, inst);
17209   if (et.type == NT_unsigned)
17210     inst.instruction |= 0xc0;
17211   else
17212     inst.instruction |= 0x80;
17213   neon_two_same (0, 1, et.size / 2);
17214 }
17215
17216 static void
17217 do_neon_qmovun (void)
17218 {
17219   struct neon_type_el et = neon_check_type (2, NS_DQ,
17220     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
17221   /* Saturating move with unsigned results. Operands must be signed.  */
17222   NEON_ENCODE (INTEGER, inst);
17223   neon_two_same (0, 1, et.size / 2);
17224 }
17225
17226 static void
17227 do_neon_rshift_sat_narrow (void)
17228 {
17229   /* FIXME: Types for narrowing. If operands are signed, results can be signed
17230      or unsigned. If operands are unsigned, results must also be unsigned.  */
17231   struct neon_type_el et = neon_check_type (2, NS_DQI,
17232     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
17233   int imm = inst.operands[2].imm;
17234   /* This gets the bounds check, size encoding and immediate bits calculation
17235      right.  */
17236   et.size /= 2;
17237
17238   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
17239      VQMOVN.I<size> <Dd>, <Qm>.  */
17240   if (imm == 0)
17241     {
17242       inst.operands[2].present = 0;
17243       inst.instruction = N_MNEM_vqmovn;
17244       do_neon_qmovn ();
17245       return;
17246     }
17247
17248   constraint (imm < 1 || (unsigned)imm > et.size,
17249               _("immediate out of range"));
17250   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
17251 }
17252
17253 static void
17254 do_neon_rshift_sat_narrow_u (void)
17255 {
17256   /* FIXME: Types for narrowing. If operands are signed, results can be signed
17257      or unsigned. If operands are unsigned, results must also be unsigned.  */
17258   struct neon_type_el et = neon_check_type (2, NS_DQI,
17259     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
17260   int imm = inst.operands[2].imm;
17261   /* This gets the bounds check, size encoding and immediate bits calculation
17262      right.  */
17263   et.size /= 2;
17264
17265   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
17266      VQMOVUN.I<size> <Dd>, <Qm>.  */
17267   if (imm == 0)
17268     {
17269       inst.operands[2].present = 0;
17270       inst.instruction = N_MNEM_vqmovun;
17271       do_neon_qmovun ();
17272       return;
17273     }
17274
17275   constraint (imm < 1 || (unsigned)imm > et.size,
17276               _("immediate out of range"));
17277   /* FIXME: The manual is kind of unclear about what value U should have in
17278      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
17279      must be 1.  */
17280   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
17281 }
17282
17283 static void
17284 do_neon_movn (void)
17285 {
17286   struct neon_type_el et = neon_check_type (2, NS_DQ,
17287     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
17288   NEON_ENCODE (INTEGER, inst);
17289   neon_two_same (0, 1, et.size / 2);
17290 }
17291
17292 static void
17293 do_neon_rshift_narrow (void)
17294 {
17295   struct neon_type_el et = neon_check_type (2, NS_DQI,
17296     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
17297   int imm = inst.operands[2].imm;
17298   /* This gets the bounds check, size encoding and immediate bits calculation
17299      right.  */
17300   et.size /= 2;
17301
17302   /* If immediate is zero then we are a pseudo-instruction for
17303      VMOVN.I<size> <Dd>, <Qm>  */
17304   if (imm == 0)
17305     {
17306       inst.operands[2].present = 0;
17307       inst.instruction = N_MNEM_vmovn;
17308       do_neon_movn ();
17309       return;
17310     }
17311
17312   constraint (imm < 1 || (unsigned)imm > et.size,
17313               _("immediate out of range for narrowing operation"));
17314   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
17315 }
17316
17317 static void
17318 do_neon_shll (void)
17319 {
17320   /* FIXME: Type checking when lengthening.  */
17321   struct neon_type_el et = neon_check_type (2, NS_QDI,
17322     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
17323   unsigned imm = inst.operands[2].imm;
17324
17325   if (imm == et.size)
17326     {
17327       /* Maximum shift variant.  */
17328       NEON_ENCODE (INTEGER, inst);
17329       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17330       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17331       inst.instruction |= LOW4 (inst.operands[1].reg);
17332       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17333       inst.instruction |= neon_logbits (et.size) << 18;
17334
17335       neon_dp_fixup (&inst);
17336     }
17337   else
17338     {
17339       /* A more-specific type check for non-max versions.  */
17340       et = neon_check_type (2, NS_QDI,
17341         N_EQK | N_DBL, N_SU_32 | N_KEY);
17342       NEON_ENCODE (IMMED, inst);
17343       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
17344     }
17345 }
17346
17347 /* Check the various types for the VCVT instruction, and return which version
17348    the current instruction is.  */
17349
17350 #define CVT_FLAVOUR_VAR                                                       \
17351   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
17352   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
17353   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
17354   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
17355   /* Half-precision conversions.  */                                          \
17356   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
17357   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
17358   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
17359   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
17360   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
17361   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
17362   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
17363      Compared with single/double precision variants, only the co-processor    \
17364      field is different, so the encoding flow is reused here.  */             \
17365   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
17366   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
17367   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
17368   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
17369   /* VFP instructions.  */                                                    \
17370   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
17371   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
17372   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
17373   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
17374   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
17375   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
17376   /* VFP instructions with bitshift.  */                                      \
17377   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
17378   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
17379   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
17380   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
17381   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
17382   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
17383   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
17384   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
17385
17386 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
17387   neon_cvt_flavour_##C,
17388
17389 /* The different types of conversions we can do.  */
17390 enum neon_cvt_flavour
17391 {
17392   CVT_FLAVOUR_VAR
17393   neon_cvt_flavour_invalid,
17394   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
17395 };
17396
17397 #undef CVT_VAR
17398
17399 static enum neon_cvt_flavour
17400 get_neon_cvt_flavour (enum neon_shape rs)
17401 {
17402 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
17403   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
17404   if (et.type != NT_invtype)                            \
17405     {                                                   \
17406       inst.error = NULL;                                \
17407       return (neon_cvt_flavour_##C);                    \
17408     }
17409
17410   struct neon_type_el et;
17411   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
17412                         || rs == NS_FF) ? N_VFP : 0;
17413   /* The instruction versions which take an immediate take one register
17414      argument, which is extended to the width of the full register. Thus the
17415      "source" and "destination" registers must have the same width.  Hack that
17416      here by making the size equal to the key (wider, in this case) operand.  */
17417   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
17418
17419   CVT_FLAVOUR_VAR;
17420
17421   return neon_cvt_flavour_invalid;
17422 #undef CVT_VAR
17423 }
17424
17425 enum neon_cvt_mode
17426 {
17427   neon_cvt_mode_a,
17428   neon_cvt_mode_n,
17429   neon_cvt_mode_p,
17430   neon_cvt_mode_m,
17431   neon_cvt_mode_z,
17432   neon_cvt_mode_x,
17433   neon_cvt_mode_r
17434 };
17435
17436 /* Neon-syntax VFP conversions.  */
17437
17438 static void
17439 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
17440 {
17441   const char *opname = 0;
17442
17443   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
17444       || rs == NS_FHI || rs == NS_HFI)
17445     {
17446       /* Conversions with immediate bitshift.  */
17447       const char *enc[] =
17448         {
17449 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
17450           CVT_FLAVOUR_VAR
17451           NULL
17452 #undef CVT_VAR
17453         };
17454
17455       if (flavour < (int) ARRAY_SIZE (enc))
17456         {
17457           opname = enc[flavour];
17458           constraint (inst.operands[0].reg != inst.operands[1].reg,
17459                       _("operands 0 and 1 must be the same register"));
17460           inst.operands[1] = inst.operands[2];
17461           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
17462         }
17463     }
17464   else
17465     {
17466       /* Conversions without bitshift.  */
17467       const char *enc[] =
17468         {
17469 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
17470           CVT_FLAVOUR_VAR
17471           NULL
17472 #undef CVT_VAR
17473         };
17474
17475       if (flavour < (int) ARRAY_SIZE (enc))
17476         opname = enc[flavour];
17477     }
17478
17479   if (opname)
17480     do_vfp_nsyn_opcode (opname);
17481
17482   /* ARMv8.2 fp16 VCVT instruction.  */
17483   if (flavour == neon_cvt_flavour_s32_f16
17484       || flavour == neon_cvt_flavour_u32_f16
17485       || flavour == neon_cvt_flavour_f16_u32
17486       || flavour == neon_cvt_flavour_f16_s32)
17487     do_scalar_fp16_v82_encode ();
17488 }
17489
17490 static void
17491 do_vfp_nsyn_cvtz (void)
17492 {
17493   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
17494   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
17495   const char *enc[] =
17496     {
17497 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
17498       CVT_FLAVOUR_VAR
17499       NULL
17500 #undef CVT_VAR
17501     };
17502
17503   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
17504     do_vfp_nsyn_opcode (enc[flavour]);
17505 }
17506
17507 static void
17508 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
17509                       enum neon_cvt_mode mode)
17510 {
17511   int sz, op;
17512   int rm;
17513
17514   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17515      D register operands.  */
17516   if (flavour == neon_cvt_flavour_s32_f64
17517       || flavour == neon_cvt_flavour_u32_f64)
17518     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17519                 _(BAD_FPU));
17520
17521   if (flavour == neon_cvt_flavour_s32_f16
17522       || flavour == neon_cvt_flavour_u32_f16)
17523     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
17524                 _(BAD_FP16));
17525
17526   set_pred_insn_type (OUTSIDE_PRED_INSN);
17527
17528   switch (flavour)
17529     {
17530     case neon_cvt_flavour_s32_f64:
17531       sz = 1;
17532       op = 1;
17533       break;
17534     case neon_cvt_flavour_s32_f32:
17535       sz = 0;
17536       op = 1;
17537       break;
17538     case neon_cvt_flavour_s32_f16:
17539       sz = 0;
17540       op = 1;
17541       break;
17542     case neon_cvt_flavour_u32_f64:
17543       sz = 1;
17544       op = 0;
17545       break;
17546     case neon_cvt_flavour_u32_f32:
17547       sz = 0;
17548       op = 0;
17549       break;
17550     case neon_cvt_flavour_u32_f16:
17551       sz = 0;
17552       op = 0;
17553       break;
17554     default:
17555       first_error (_("invalid instruction shape"));
17556       return;
17557     }
17558
17559   switch (mode)
17560     {
17561     case neon_cvt_mode_a: rm = 0; break;
17562     case neon_cvt_mode_n: rm = 1; break;
17563     case neon_cvt_mode_p: rm = 2; break;
17564     case neon_cvt_mode_m: rm = 3; break;
17565     default: first_error (_("invalid rounding mode")); return;
17566     }
17567
17568   NEON_ENCODE (FPV8, inst);
17569   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
17570   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
17571   inst.instruction |= sz << 8;
17572
17573   /* ARMv8.2 fp16 VCVT instruction.  */
17574   if (flavour == neon_cvt_flavour_s32_f16
17575       ||flavour == neon_cvt_flavour_u32_f16)
17576     do_scalar_fp16_v82_encode ();
17577   inst.instruction |= op << 7;
17578   inst.instruction |= rm << 16;
17579   inst.instruction |= 0xf0000000;
17580   inst.is_neon = TRUE;
17581 }
17582
17583 static void
17584 do_neon_cvt_1 (enum neon_cvt_mode mode)
17585 {
17586   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
17587                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
17588                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
17589                                           NS_NULL);
17590   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
17591
17592   if (flavour == neon_cvt_flavour_invalid)
17593     return;
17594
17595   /* PR11109: Handle round-to-zero for VCVT conversions.  */
17596   if (mode == neon_cvt_mode_z
17597       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
17598       && (flavour == neon_cvt_flavour_s16_f16
17599           || flavour == neon_cvt_flavour_u16_f16
17600           || flavour == neon_cvt_flavour_s32_f32
17601           || flavour == neon_cvt_flavour_u32_f32
17602           || flavour == neon_cvt_flavour_s32_f64
17603           || flavour == neon_cvt_flavour_u32_f64)
17604       && (rs == NS_FD || rs == NS_FF))
17605     {
17606       do_vfp_nsyn_cvtz ();
17607       return;
17608     }
17609
17610   /* ARMv8.2 fp16 VCVT conversions.  */
17611   if (mode == neon_cvt_mode_z
17612       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
17613       && (flavour == neon_cvt_flavour_s32_f16
17614           || flavour == neon_cvt_flavour_u32_f16)
17615       && (rs == NS_FH))
17616     {
17617       do_vfp_nsyn_cvtz ();
17618       do_scalar_fp16_v82_encode ();
17619       return;
17620     }
17621
17622   /* VFP rather than Neon conversions.  */
17623   if (flavour >= neon_cvt_flavour_first_fp)
17624     {
17625       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
17626         do_vfp_nsyn_cvt (rs, flavour);
17627       else
17628         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
17629
17630       return;
17631     }
17632
17633   switch (rs)
17634     {
17635     case NS_QQI:
17636       if (mode == neon_cvt_mode_z
17637           && (flavour == neon_cvt_flavour_f16_s16
17638               || flavour == neon_cvt_flavour_f16_u16
17639               || flavour == neon_cvt_flavour_s16_f16
17640               || flavour == neon_cvt_flavour_u16_f16
17641               || flavour == neon_cvt_flavour_f32_u32
17642               || flavour == neon_cvt_flavour_f32_s32
17643               || flavour == neon_cvt_flavour_s32_f32
17644               || flavour == neon_cvt_flavour_u32_f32))
17645         {
17646           if (check_simd_pred_availability (1, NEON_CHECK_CC | NEON_CHECK_ARCH))
17647             return;
17648         }
17649       else if (mode == neon_cvt_mode_n)
17650         {
17651           /* We are dealing with vcvt with the 'ne' condition.  */
17652           inst.cond = 0x1;
17653           inst.instruction = N_MNEM_vcvt;
17654           do_neon_cvt_1 (neon_cvt_mode_z);
17655           return;
17656         }
17657       /* fall through.  */
17658     case NS_DDI:
17659       {
17660         unsigned immbits;
17661         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
17662                              0x0000100, 0x1000100, 0x0, 0x1000000};
17663
17664         if ((rs != NS_QQI || !ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
17665             && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17666             return;
17667
17668         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
17669           {
17670             constraint (inst.operands[2].present && inst.operands[2].imm == 0,
17671                         _("immediate value out of range"));
17672             switch (flavour)
17673               {
17674                 case neon_cvt_flavour_f16_s16:
17675                 case neon_cvt_flavour_f16_u16:
17676                 case neon_cvt_flavour_s16_f16:
17677                 case neon_cvt_flavour_u16_f16:
17678                   constraint (inst.operands[2].imm > 16,
17679                               _("immediate value out of range"));
17680                   break;
17681                 case neon_cvt_flavour_f32_u32:
17682                 case neon_cvt_flavour_f32_s32:
17683                 case neon_cvt_flavour_s32_f32:
17684                 case neon_cvt_flavour_u32_f32:
17685                   constraint (inst.operands[2].imm > 32,
17686                               _("immediate value out of range"));
17687                   break;
17688                 default:
17689                   inst.error = BAD_FPU;
17690                   return;
17691               }
17692           }
17693
17694         /* Fixed-point conversion with #0 immediate is encoded as an
17695            integer conversion.  */
17696         if (inst.operands[2].present && inst.operands[2].imm == 0)
17697           goto int_encode;
17698         NEON_ENCODE (IMMED, inst);
17699         if (flavour != neon_cvt_flavour_invalid)
17700           inst.instruction |= enctab[flavour];
17701         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17702         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17703         inst.instruction |= LOW4 (inst.operands[1].reg);
17704         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17705         inst.instruction |= neon_quad (rs) << 6;
17706         inst.instruction |= 1 << 21;
17707         if (flavour < neon_cvt_flavour_s16_f16)
17708           {
17709             inst.instruction |= 1 << 21;
17710             immbits = 32 - inst.operands[2].imm;
17711             inst.instruction |= immbits << 16;
17712           }
17713         else
17714           {
17715             inst.instruction |= 3 << 20;
17716             immbits = 16 - inst.operands[2].imm;
17717             inst.instruction |= immbits << 16;
17718             inst.instruction &= ~(1 << 9);
17719           }
17720
17721         neon_dp_fixup (&inst);
17722       }
17723       break;
17724
17725     case NS_QQ:
17726       if ((mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
17727            || mode == neon_cvt_mode_m || mode == neon_cvt_mode_p)
17728           && (flavour == neon_cvt_flavour_s16_f16
17729               || flavour == neon_cvt_flavour_u16_f16
17730               || flavour == neon_cvt_flavour_s32_f32
17731               || flavour == neon_cvt_flavour_u32_f32))
17732         {
17733           if (check_simd_pred_availability (1,
17734                                             NEON_CHECK_CC | NEON_CHECK_ARCH8))
17735             return;
17736         }
17737       else if (mode == neon_cvt_mode_z
17738                && (flavour == neon_cvt_flavour_f16_s16
17739                    || flavour == neon_cvt_flavour_f16_u16
17740                    || flavour == neon_cvt_flavour_s16_f16
17741                    || flavour == neon_cvt_flavour_u16_f16
17742                    || flavour == neon_cvt_flavour_f32_u32
17743                    || flavour == neon_cvt_flavour_f32_s32
17744                    || flavour == neon_cvt_flavour_s32_f32
17745                    || flavour == neon_cvt_flavour_u32_f32))
17746         {
17747           if (check_simd_pred_availability (1,
17748                                             NEON_CHECK_CC | NEON_CHECK_ARCH))
17749             return;
17750         }
17751       /* fall through.  */
17752     case NS_DD:
17753       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
17754         {
17755
17756           NEON_ENCODE (FLOAT, inst);
17757           if (check_simd_pred_availability (1,
17758                                             NEON_CHECK_CC | NEON_CHECK_ARCH8))
17759             return;
17760
17761           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17762           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17763           inst.instruction |= LOW4 (inst.operands[1].reg);
17764           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17765           inst.instruction |= neon_quad (rs) << 6;
17766           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
17767                                || flavour == neon_cvt_flavour_u32_f32) << 7;
17768           inst.instruction |= mode << 8;
17769           if (flavour == neon_cvt_flavour_u16_f16
17770               || flavour == neon_cvt_flavour_s16_f16)
17771             /* Mask off the original size bits and reencode them.  */
17772             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
17773
17774           if (thumb_mode)
17775             inst.instruction |= 0xfc000000;
17776           else
17777             inst.instruction |= 0xf0000000;
17778         }
17779       else
17780         {
17781     int_encode:
17782           {
17783             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
17784                                   0x100, 0x180, 0x0, 0x080};
17785
17786             NEON_ENCODE (INTEGER, inst);
17787
17788           if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
17789             {
17790               if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17791                 return;
17792             }
17793
17794             if (flavour != neon_cvt_flavour_invalid)
17795               inst.instruction |= enctab[flavour];
17796
17797             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17798             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17799             inst.instruction |= LOW4 (inst.operands[1].reg);
17800             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17801             inst.instruction |= neon_quad (rs) << 6;
17802             if (flavour >= neon_cvt_flavour_s16_f16
17803                 && flavour <= neon_cvt_flavour_f16_u16)
17804               /* Half precision.  */
17805               inst.instruction |= 1 << 18;
17806             else
17807               inst.instruction |= 2 << 18;
17808
17809             neon_dp_fixup (&inst);
17810           }
17811         }
17812       break;
17813
17814     /* Half-precision conversions for Advanced SIMD -- neon.  */
17815     case NS_QD:
17816     case NS_DQ:
17817       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17818         return;
17819
17820       if ((rs == NS_DQ)
17821           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
17822           {
17823             as_bad (_("operand size must match register width"));
17824             break;
17825           }
17826
17827       if ((rs == NS_QD)
17828           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
17829           {
17830             as_bad (_("operand size must match register width"));
17831             break;
17832           }
17833
17834       if (rs == NS_DQ)
17835         inst.instruction = 0x3b60600;
17836       else
17837         inst.instruction = 0x3b60700;
17838
17839       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17840       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17841       inst.instruction |= LOW4 (inst.operands[1].reg);
17842       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17843       neon_dp_fixup (&inst);
17844       break;
17845
17846     default:
17847       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
17848       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
17849         do_vfp_nsyn_cvt (rs, flavour);
17850       else
17851         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
17852     }
17853 }
17854
17855 static void
17856 do_neon_cvtr (void)
17857 {
17858   do_neon_cvt_1 (neon_cvt_mode_x);
17859 }
17860
17861 static void
17862 do_neon_cvt (void)
17863 {
17864   do_neon_cvt_1 (neon_cvt_mode_z);
17865 }
17866
17867 static void
17868 do_neon_cvta (void)
17869 {
17870   do_neon_cvt_1 (neon_cvt_mode_a);
17871 }
17872
17873 static void
17874 do_neon_cvtn (void)
17875 {
17876   do_neon_cvt_1 (neon_cvt_mode_n);
17877 }
17878
17879 static void
17880 do_neon_cvtp (void)
17881 {
17882   do_neon_cvt_1 (neon_cvt_mode_p);
17883 }
17884
17885 static void
17886 do_neon_cvtm (void)
17887 {
17888   do_neon_cvt_1 (neon_cvt_mode_m);
17889 }
17890
17891 static void
17892 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
17893 {
17894   if (is_double)
17895     mark_feature_used (&fpu_vfp_ext_armv8);
17896
17897   encode_arm_vfp_reg (inst.operands[0].reg,
17898                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
17899   encode_arm_vfp_reg (inst.operands[1].reg,
17900                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
17901   inst.instruction |= to ? 0x10000 : 0;
17902   inst.instruction |= t ? 0x80 : 0;
17903   inst.instruction |= is_double ? 0x100 : 0;
17904   do_vfp_cond_or_thumb ();
17905 }
17906
17907 static void
17908 do_neon_cvttb_1 (bfd_boolean t)
17909 {
17910   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
17911                                           NS_DF, NS_DH, NS_QQ, NS_QQI, NS_NULL);
17912
17913   if (rs == NS_NULL)
17914     return;
17915   else if (rs == NS_QQ || rs == NS_QQI)
17916     {
17917       int single_to_half = 0;
17918       if (check_simd_pred_availability (1, NEON_CHECK_ARCH))
17919         return;
17920
17921       enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
17922
17923       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
17924           && (flavour ==  neon_cvt_flavour_u16_f16
17925               || flavour ==  neon_cvt_flavour_s16_f16
17926               || flavour ==  neon_cvt_flavour_f16_s16
17927               || flavour ==  neon_cvt_flavour_f16_u16
17928               || flavour ==  neon_cvt_flavour_u32_f32
17929               || flavour ==  neon_cvt_flavour_s32_f32
17930               || flavour ==  neon_cvt_flavour_f32_s32
17931               || flavour ==  neon_cvt_flavour_f32_u32))
17932         {
17933           inst.cond = 0xf;
17934           inst.instruction = N_MNEM_vcvt;
17935           set_pred_insn_type (INSIDE_VPT_INSN);
17936           do_neon_cvt_1 (neon_cvt_mode_z);
17937           return;
17938         }
17939       else if (rs == NS_QQ && flavour == neon_cvt_flavour_f32_f16)
17940         single_to_half = 1;
17941       else if (rs == NS_QQ && flavour != neon_cvt_flavour_f16_f32)
17942         {
17943           first_error (BAD_FPU);
17944           return;
17945         }
17946
17947       inst.instruction = 0xee3f0e01;
17948       inst.instruction |= single_to_half << 28;
17949       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17950       inst.instruction |= LOW4 (inst.operands[0].reg) << 13;
17951       inst.instruction |= t << 12;
17952       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17953       inst.instruction |= LOW4 (inst.operands[1].reg) << 1;
17954       inst.is_neon = 1;
17955     }
17956   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
17957     {
17958       inst.error = NULL;
17959       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
17960     }
17961   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
17962     {
17963       inst.error = NULL;
17964       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
17965     }
17966   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
17967     {
17968       /* The VCVTB and VCVTT instructions with D-register operands
17969          don't work for SP only targets.  */
17970       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17971                   _(BAD_FPU));
17972
17973       inst.error = NULL;
17974       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
17975     }
17976   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
17977     {
17978       /* The VCVTB and VCVTT instructions with D-register operands
17979          don't work for SP only targets.  */
17980       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17981                   _(BAD_FPU));
17982
17983       inst.error = NULL;
17984       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
17985     }
17986   else
17987     return;
17988 }
17989
17990 static void
17991 do_neon_cvtb (void)
17992 {
17993   do_neon_cvttb_1 (FALSE);
17994 }
17995
17996
17997 static void
17998 do_neon_cvtt (void)
17999 {
18000   do_neon_cvttb_1 (TRUE);
18001 }
18002
18003 static void
18004 neon_move_immediate (void)
18005 {
18006   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
18007   struct neon_type_el et = neon_check_type (2, rs,
18008     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
18009   unsigned immlo, immhi = 0, immbits;
18010   int op, cmode, float_p;
18011
18012   constraint (et.type == NT_invtype,
18013               _("operand size must be specified for immediate VMOV"));
18014
18015   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
18016   op = (inst.instruction & (1 << 5)) != 0;
18017
18018   immlo = inst.operands[1].imm;
18019   if (inst.operands[1].regisimm)
18020     immhi = inst.operands[1].reg;
18021
18022   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
18023               _("immediate has bits set outside the operand size"));
18024
18025   float_p = inst.operands[1].immisfloat;
18026
18027   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
18028                                         et.size, et.type)) == FAIL)
18029     {
18030       /* Invert relevant bits only.  */
18031       neon_invert_size (&immlo, &immhi, et.size);
18032       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
18033          with one or the other; those cases are caught by
18034          neon_cmode_for_move_imm.  */
18035       op = !op;
18036       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
18037                                             &op, et.size, et.type)) == FAIL)
18038         {
18039           first_error (_("immediate out of range"));
18040           return;
18041         }
18042     }
18043
18044   inst.instruction &= ~(1 << 5);
18045   inst.instruction |= op << 5;
18046
18047   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18048   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18049   inst.instruction |= neon_quad (rs) << 6;
18050   inst.instruction |= cmode << 8;
18051
18052   neon_write_immbits (immbits);
18053 }
18054
18055 static void
18056 do_neon_mvn (void)
18057 {
18058   if (inst.operands[1].isreg)
18059     {
18060       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18061
18062       NEON_ENCODE (INTEGER, inst);
18063       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18064       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18065       inst.instruction |= LOW4 (inst.operands[1].reg);
18066       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18067       inst.instruction |= neon_quad (rs) << 6;
18068     }
18069   else
18070     {
18071       NEON_ENCODE (IMMED, inst);
18072       neon_move_immediate ();
18073     }
18074
18075   neon_dp_fixup (&inst);
18076 }
18077
18078 /* Encode instructions of form:
18079
18080   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
18081   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
18082
18083 static void
18084 neon_mixed_length (struct neon_type_el et, unsigned size)
18085 {
18086   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18087   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18088   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
18089   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
18090   inst.instruction |= LOW4 (inst.operands[2].reg);
18091   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
18092   inst.instruction |= (et.type == NT_unsigned) << 24;
18093   inst.instruction |= neon_logbits (size) << 20;
18094
18095   neon_dp_fixup (&inst);
18096 }
18097
18098 static void
18099 do_neon_dyadic_long (void)
18100 {
18101   enum neon_shape rs = neon_select_shape (NS_QDD, NS_QQQ, NS_QQR, NS_NULL);
18102   if (rs == NS_QDD)
18103     {
18104       if (vfp_or_neon_is_neon (NEON_CHECK_ARCH | NEON_CHECK_CC) == FAIL)
18105         return;
18106
18107       NEON_ENCODE (INTEGER, inst);
18108       /* FIXME: Type checking for lengthening op.  */
18109       struct neon_type_el et = neon_check_type (3, NS_QDD,
18110         N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
18111       neon_mixed_length (et, et.size);
18112     }
18113   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
18114            && (inst.cond == 0xf || inst.cond == 0x10))
18115     {
18116       /* If parsing for MVE, vaddl/vsubl/vabdl{e,t} can only be vadd/vsub/vabd
18117          in an IT block with le/lt conditions.  */
18118
18119       if (inst.cond == 0xf)
18120         inst.cond = 0xb;
18121       else if (inst.cond == 0x10)
18122         inst.cond = 0xd;
18123
18124       inst.pred_insn_type = INSIDE_IT_INSN;
18125
18126       if (inst.instruction == N_MNEM_vaddl)
18127         {
18128           inst.instruction = N_MNEM_vadd;
18129           do_neon_addsub_if_i ();
18130         }
18131       else if (inst.instruction == N_MNEM_vsubl)
18132         {
18133           inst.instruction = N_MNEM_vsub;
18134           do_neon_addsub_if_i ();
18135         }
18136       else if (inst.instruction == N_MNEM_vabdl)
18137         {
18138           inst.instruction = N_MNEM_vabd;
18139           do_neon_dyadic_if_su ();
18140         }
18141     }
18142   else
18143     first_error (BAD_FPU);
18144 }
18145
18146 static void
18147 do_neon_abal (void)
18148 {
18149   struct neon_type_el et = neon_check_type (3, NS_QDD,
18150     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
18151   neon_mixed_length (et, et.size);
18152 }
18153
18154 static void
18155 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
18156 {
18157   if (inst.operands[2].isscalar)
18158     {
18159       struct neon_type_el et = neon_check_type (3, NS_QDS,
18160         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
18161       NEON_ENCODE (SCALAR, inst);
18162       neon_mul_mac (et, et.type == NT_unsigned);
18163     }
18164   else
18165     {
18166       struct neon_type_el et = neon_check_type (3, NS_QDD,
18167         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
18168       NEON_ENCODE (INTEGER, inst);
18169       neon_mixed_length (et, et.size);
18170     }
18171 }
18172
18173 static void
18174 do_neon_mac_maybe_scalar_long (void)
18175 {
18176   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
18177 }
18178
18179 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
18180    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
18181
18182 static unsigned
18183 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
18184 {
18185   unsigned regno = NEON_SCALAR_REG (scalar);
18186   unsigned elno = NEON_SCALAR_INDEX (scalar);
18187
18188   if (quad_p)
18189     {
18190       if (regno > 7 || elno > 3)
18191         goto bad_scalar;
18192
18193       return ((regno & 0x7)
18194               | ((elno & 0x1) << 3)
18195               | (((elno >> 1) & 0x1) << 5));
18196     }
18197   else
18198     {
18199       if (regno > 15 || elno > 1)
18200         goto bad_scalar;
18201
18202       return (((regno & 0x1) << 5)
18203               | ((regno >> 1) & 0x7)
18204               | ((elno & 0x1) << 3));
18205     }
18206
18207 bad_scalar:
18208   first_error (_("scalar out of range for multiply instruction"));
18209   return 0;
18210 }
18211
18212 static void
18213 do_neon_fmac_maybe_scalar_long (int subtype)
18214 {
18215   enum neon_shape rs;
18216   int high8;
18217   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
18218      field (bits[21:20]) has different meaning.  For scalar index variant, it's
18219      used to differentiate add and subtract, otherwise it's with fixed value
18220      0x2.  */
18221   int size = -1;
18222
18223   if (inst.cond != COND_ALWAYS)
18224     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
18225                "behaviour is UNPREDICTABLE"));
18226
18227   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
18228               _(BAD_FP16));
18229
18230   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
18231               _(BAD_FPU));
18232
18233   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
18234      be a scalar index register.  */
18235   if (inst.operands[2].isscalar)
18236     {
18237       high8 = 0xfe000000;
18238       if (subtype)
18239         size = 16;
18240       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
18241     }
18242   else
18243     {
18244       high8 = 0xfc000000;
18245       size = 32;
18246       if (subtype)
18247         inst.instruction |= (0x1 << 23);
18248       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
18249     }
18250
18251   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
18252
18253   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
18254      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
18255      so we simply pass -1 as size.  */
18256   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
18257   neon_three_same (quad_p, 0, size);
18258
18259   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
18260   inst.instruction &= 0x00ffffff;
18261   inst.instruction |= high8;
18262
18263 #define LOW1(R) ((R) & 0x1)
18264 #define HI4(R) (((R) >> 1) & 0xf)
18265   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
18266      whether the instruction is in Q form and whether Vm is a scalar indexed
18267      operand.  */
18268   if (inst.operands[2].isscalar)
18269     {
18270       unsigned rm
18271         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
18272       inst.instruction &= 0xffffffd0;
18273       inst.instruction |= rm;
18274
18275       if (!quad_p)
18276         {
18277           /* Redo Rn as well.  */
18278           inst.instruction &= 0xfff0ff7f;
18279           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
18280           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
18281         }
18282     }
18283   else if (!quad_p)
18284     {
18285       /* Redo Rn and Rm.  */
18286       inst.instruction &= 0xfff0ff50;
18287       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
18288       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
18289       inst.instruction |= HI4 (inst.operands[2].reg);
18290       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
18291     }
18292 }
18293
18294 static void
18295 do_neon_vfmal (void)
18296 {
18297   return do_neon_fmac_maybe_scalar_long (0);
18298 }
18299
18300 static void
18301 do_neon_vfmsl (void)
18302 {
18303   return do_neon_fmac_maybe_scalar_long (1);
18304 }
18305
18306 static void
18307 do_neon_dyadic_wide (void)
18308 {
18309   struct neon_type_el et = neon_check_type (3, NS_QQD,
18310     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
18311   neon_mixed_length (et, et.size);
18312 }
18313
18314 static void
18315 do_neon_dyadic_narrow (void)
18316 {
18317   struct neon_type_el et = neon_check_type (3, NS_QDD,
18318     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
18319   /* Operand sign is unimportant, and the U bit is part of the opcode,
18320      so force the operand type to integer.  */
18321   et.type = NT_integer;
18322   neon_mixed_length (et, et.size / 2);
18323 }
18324
18325 static void
18326 do_neon_mul_sat_scalar_long (void)
18327 {
18328   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
18329 }
18330
18331 static void
18332 do_neon_vmull (void)
18333 {
18334   if (inst.operands[2].isscalar)
18335     do_neon_mac_maybe_scalar_long ();
18336   else
18337     {
18338       struct neon_type_el et = neon_check_type (3, NS_QDD,
18339         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
18340
18341       if (et.type == NT_poly)
18342         NEON_ENCODE (POLY, inst);
18343       else
18344         NEON_ENCODE (INTEGER, inst);
18345
18346       /* For polynomial encoding the U bit must be zero, and the size must
18347          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
18348          obviously, as 0b10).  */
18349       if (et.size == 64)
18350         {
18351           /* Check we're on the correct architecture.  */
18352           if (!mark_feature_used (&fpu_crypto_ext_armv8))
18353             inst.error =
18354               _("Instruction form not available on this architecture.");
18355
18356           et.size = 32;
18357         }
18358
18359       neon_mixed_length (et, et.size);
18360     }
18361 }
18362
18363 static void
18364 do_neon_ext (void)
18365 {
18366   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
18367   struct neon_type_el et = neon_check_type (3, rs,
18368     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
18369   unsigned imm = (inst.operands[3].imm * et.size) / 8;
18370
18371   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
18372               _("shift out of range"));
18373   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18374   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18375   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
18376   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
18377   inst.instruction |= LOW4 (inst.operands[2].reg);
18378   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
18379   inst.instruction |= neon_quad (rs) << 6;
18380   inst.instruction |= imm << 8;
18381
18382   neon_dp_fixup (&inst);
18383 }
18384
18385 static void
18386 do_neon_rev (void)
18387 {
18388   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18389   struct neon_type_el et = neon_check_type (2, rs,
18390     N_EQK, N_8 | N_16 | N_32 | N_KEY);
18391   unsigned op = (inst.instruction >> 7) & 3;
18392   /* N (width of reversed regions) is encoded as part of the bitmask. We
18393      extract it here to check the elements to be reversed are smaller.
18394      Otherwise we'd get a reserved instruction.  */
18395   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
18396   gas_assert (elsize != 0);
18397   constraint (et.size >= elsize,
18398               _("elements must be smaller than reversal region"));
18399   neon_two_same (neon_quad (rs), 1, et.size);
18400 }
18401
18402 static void
18403 do_neon_dup (void)
18404 {
18405   if (inst.operands[1].isscalar)
18406     {
18407       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
18408       struct neon_type_el et = neon_check_type (2, rs,
18409         N_EQK, N_8 | N_16 | N_32 | N_KEY);
18410       unsigned sizebits = et.size >> 3;
18411       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
18412       int logsize = neon_logbits (et.size);
18413       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
18414
18415       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
18416         return;
18417
18418       NEON_ENCODE (SCALAR, inst);
18419       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18420       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18421       inst.instruction |= LOW4 (dm);
18422       inst.instruction |= HI1 (dm) << 5;
18423       inst.instruction |= neon_quad (rs) << 6;
18424       inst.instruction |= x << 17;
18425       inst.instruction |= sizebits << 16;
18426
18427       neon_dp_fixup (&inst);
18428     }
18429   else
18430     {
18431       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
18432       struct neon_type_el et = neon_check_type (2, rs,
18433         N_8 | N_16 | N_32 | N_KEY, N_EQK);
18434       /* Duplicate ARM register to lanes of vector.  */
18435       NEON_ENCODE (ARMREG, inst);
18436       switch (et.size)
18437         {
18438         case 8:  inst.instruction |= 0x400000; break;
18439         case 16: inst.instruction |= 0x000020; break;
18440         case 32: inst.instruction |= 0x000000; break;
18441         default: break;
18442         }
18443       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
18444       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
18445       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
18446       inst.instruction |= neon_quad (rs) << 21;
18447       /* The encoding for this instruction is identical for the ARM and Thumb
18448          variants, except for the condition field.  */
18449       do_vfp_cond_or_thumb ();
18450     }
18451 }
18452
18453 static void
18454 do_mve_mov (int toQ)
18455 {
18456   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18457     return;
18458   if (inst.cond > COND_ALWAYS)
18459     inst.pred_insn_type = MVE_UNPREDICABLE_INSN;
18460
18461   unsigned Rt = 0, Rt2 = 1, Q0 = 2, Q1 = 3;
18462   if (toQ)
18463     {
18464       Q0 = 0;
18465       Q1 = 1;
18466       Rt = 2;
18467       Rt2 = 3;
18468     }
18469
18470   constraint (inst.operands[Q0].reg != inst.operands[Q1].reg + 2,
18471               _("Index one must be [2,3] and index two must be two less than"
18472                 " index one."));
18473   constraint (inst.operands[Rt].reg == inst.operands[Rt2].reg,
18474               _("General purpose registers may not be the same"));
18475   constraint (inst.operands[Rt].reg == REG_SP
18476               || inst.operands[Rt2].reg == REG_SP,
18477               BAD_SP);
18478   constraint (inst.operands[Rt].reg == REG_PC
18479               || inst.operands[Rt2].reg == REG_PC,
18480               BAD_PC);
18481
18482   inst.instruction = 0xec000f00;
18483   inst.instruction |= HI1 (inst.operands[Q1].reg / 32) << 23;
18484   inst.instruction |= !!toQ << 20;
18485   inst.instruction |= inst.operands[Rt2].reg << 16;
18486   inst.instruction |= LOW4 (inst.operands[Q1].reg / 32) << 13;
18487   inst.instruction |= (inst.operands[Q1].reg % 4) << 4;
18488   inst.instruction |= inst.operands[Rt].reg;
18489 }
18490
18491 static void
18492 do_mve_movn (void)
18493 {
18494   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18495     return;
18496
18497   if (inst.cond > COND_ALWAYS)
18498     inst.pred_insn_type = INSIDE_VPT_INSN;
18499   else
18500     inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
18501
18502   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_I16 | N_I32
18503                                             | N_KEY);
18504
18505   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18506   inst.instruction |= (neon_logbits (et.size) - 1) << 18;
18507   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18508   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18509   inst.instruction |= LOW4 (inst.operands[1].reg);
18510   inst.is_neon = 1;
18511
18512 }
18513
18514 /* VMOV has particularly many variations. It can be one of:
18515      0. VMOV<c><q> <Qd>, <Qm>
18516      1. VMOV<c><q> <Dd>, <Dm>
18517    (Register operations, which are VORR with Rm = Rn.)
18518      2. VMOV<c><q>.<dt> <Qd>, #<imm>
18519      3. VMOV<c><q>.<dt> <Dd>, #<imm>
18520    (Immediate loads.)
18521      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
18522    (ARM register to scalar.)
18523      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
18524    (Two ARM registers to vector.)
18525      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
18526    (Scalar to ARM register.)
18527      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
18528    (Vector to two ARM registers.)
18529      8. VMOV.F32 <Sd>, <Sm>
18530      9. VMOV.F64 <Dd>, <Dm>
18531    (VFP register moves.)
18532     10. VMOV.F32 <Sd>, #imm
18533     11. VMOV.F64 <Dd>, #imm
18534    (VFP float immediate load.)
18535     12. VMOV <Rd>, <Sm>
18536    (VFP single to ARM reg.)
18537     13. VMOV <Sd>, <Rm>
18538    (ARM reg to VFP single.)
18539     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
18540    (Two ARM regs to two VFP singles.)
18541     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
18542    (Two VFP singles to two ARM regs.)
18543    16. VMOV<c> <Rt>, <Rt2>, <Qd[idx]>, <Qd[idx2]>
18544    17. VMOV<c> <Qd[idx]>, <Qd[idx2]>, <Rt>, <Rt2>
18545    18. VMOV<c>.<dt> <Rt>, <Qn[idx]>
18546    19. VMOV<c>.<dt> <Qd[idx]>, <Rt>
18547
18548    These cases can be disambiguated using neon_select_shape, except cases 1/9
18549    and 3/11 which depend on the operand type too.
18550
18551    All the encoded bits are hardcoded by this function.
18552
18553    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
18554    Cases 5, 7 may be used with VFPv2 and above.
18555
18556    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
18557    can specify a type where it doesn't make sense to, and is ignored).  */
18558
18559 static void
18560 do_neon_mov (void)
18561 {
18562   enum neon_shape rs = neon_select_shape (NS_RRSS, NS_SSRR, NS_RRFF, NS_FFRR,
18563                                           NS_DRR, NS_RRD, NS_QQ, NS_DD, NS_QI,
18564                                           NS_DI, NS_SR, NS_RS, NS_FF, NS_FI,
18565                                           NS_RF, NS_FR, NS_HR, NS_RH, NS_HI,
18566                                           NS_NULL);
18567   struct neon_type_el et;
18568   const char *ldconst = 0;
18569
18570   switch (rs)
18571     {
18572     case NS_DD:  /* case 1/9.  */
18573       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
18574       /* It is not an error here if no type is given.  */
18575       inst.error = NULL;
18576       if (et.type == NT_float && et.size == 64)
18577         {
18578           do_vfp_nsyn_opcode ("fcpyd");
18579           break;
18580         }
18581       /* fall through.  */
18582
18583     case NS_QQ:  /* case 0/1.  */
18584       {
18585         if (check_simd_pred_availability (0, NEON_CHECK_CC | NEON_CHECK_ARCH))
18586           return;
18587         /* The architecture manual I have doesn't explicitly state which
18588            value the U bit should have for register->register moves, but
18589            the equivalent VORR instruction has U = 0, so do that.  */
18590         inst.instruction = 0x0200110;
18591         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18592         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18593         inst.instruction |= LOW4 (inst.operands[1].reg);
18594         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18595         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
18596         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
18597         inst.instruction |= neon_quad (rs) << 6;
18598
18599         neon_dp_fixup (&inst);
18600       }
18601       break;
18602
18603     case NS_DI:  /* case 3/11.  */
18604       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
18605       inst.error = NULL;
18606       if (et.type == NT_float && et.size == 64)
18607         {
18608           /* case 11 (fconstd).  */
18609           ldconst = "fconstd";
18610           goto encode_fconstd;
18611         }
18612       /* fall through.  */
18613
18614     case NS_QI:  /* case 2/3.  */
18615       if (check_simd_pred_availability (0, NEON_CHECK_CC | NEON_CHECK_ARCH))
18616         return;
18617       inst.instruction = 0x0800010;
18618       neon_move_immediate ();
18619       neon_dp_fixup (&inst);
18620       break;
18621
18622     case NS_SR:  /* case 4.  */
18623       {
18624         unsigned bcdebits = 0;
18625         int logsize;
18626         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
18627         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
18628
18629         /* .<size> is optional here, defaulting to .32. */
18630         if (inst.vectype.elems == 0
18631             && inst.operands[0].vectype.type == NT_invtype
18632             && inst.operands[1].vectype.type == NT_invtype)
18633           {
18634             inst.vectype.el[0].type = NT_untyped;
18635             inst.vectype.el[0].size = 32;
18636             inst.vectype.elems = 1;
18637           }
18638
18639         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
18640         logsize = neon_logbits (et.size);
18641
18642         if (et.size != 32)
18643           {
18644             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
18645                 && vfp_or_neon_is_neon (NEON_CHECK_ARCH) == FAIL)
18646               return;
18647           }
18648         else
18649           {
18650             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
18651                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
18652                         _(BAD_FPU));
18653           }
18654
18655         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18656           {
18657             if (inst.operands[1].reg == REG_SP)
18658               as_tsktsk (MVE_BAD_SP);
18659             else if (inst.operands[1].reg == REG_PC)
18660               as_tsktsk (MVE_BAD_PC);
18661           }
18662         unsigned size = inst.operands[0].isscalar == 1 ? 64 : 128;
18663
18664         constraint (et.type == NT_invtype, _("bad type for scalar"));
18665         constraint (x >= size / et.size, _("scalar index out of range"));
18666
18667
18668         switch (et.size)
18669           {
18670           case 8:  bcdebits = 0x8; break;
18671           case 16: bcdebits = 0x1; break;
18672           case 32: bcdebits = 0x0; break;
18673           default: ;
18674           }
18675
18676         bcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
18677
18678         inst.instruction = 0xe000b10;
18679         do_vfp_cond_or_thumb ();
18680         inst.instruction |= LOW4 (dn) << 16;
18681         inst.instruction |= HI1 (dn) << 7;
18682         inst.instruction |= inst.operands[1].reg << 12;
18683         inst.instruction |= (bcdebits & 3) << 5;
18684         inst.instruction |= ((bcdebits >> 2) & 3) << 21;
18685         inst.instruction |= (x >> (3-logsize)) << 16;
18686       }
18687       break;
18688
18689     case NS_DRR:  /* case 5 (fmdrr).  */
18690       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
18691                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
18692                   _(BAD_FPU));
18693
18694       inst.instruction = 0xc400b10;
18695       do_vfp_cond_or_thumb ();
18696       inst.instruction |= LOW4 (inst.operands[0].reg);
18697       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
18698       inst.instruction |= inst.operands[1].reg << 12;
18699       inst.instruction |= inst.operands[2].reg << 16;
18700       break;
18701
18702     case NS_RS:  /* case 6.  */
18703       {
18704         unsigned logsize;
18705         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
18706         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
18707         unsigned abcdebits = 0;
18708
18709         /* .<dt> is optional here, defaulting to .32. */
18710         if (inst.vectype.elems == 0
18711             && inst.operands[0].vectype.type == NT_invtype
18712             && inst.operands[1].vectype.type == NT_invtype)
18713           {
18714             inst.vectype.el[0].type = NT_untyped;
18715             inst.vectype.el[0].size = 32;
18716             inst.vectype.elems = 1;
18717           }
18718
18719         et = neon_check_type (2, NS_NULL,
18720                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
18721         logsize = neon_logbits (et.size);
18722
18723         if (et.size != 32)
18724           {
18725             if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
18726                 && vfp_or_neon_is_neon (NEON_CHECK_CC
18727                                         | NEON_CHECK_ARCH) == FAIL)
18728               return;
18729           }
18730         else
18731           {
18732             constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1)
18733                         && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
18734                         _(BAD_FPU));
18735           }
18736
18737         if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18738           {
18739             if (inst.operands[0].reg == REG_SP)
18740               as_tsktsk (MVE_BAD_SP);
18741             else if (inst.operands[0].reg == REG_PC)
18742               as_tsktsk (MVE_BAD_PC);
18743           }
18744
18745         unsigned size = inst.operands[1].isscalar == 1 ? 64 : 128;
18746
18747         constraint (et.type == NT_invtype, _("bad type for scalar"));
18748         constraint (x >= size / et.size, _("scalar index out of range"));
18749
18750         switch (et.size)
18751           {
18752           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
18753           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
18754           case 32: abcdebits = 0x00; break;
18755           default: ;
18756           }
18757
18758         abcdebits |= (x & ((1 << (3-logsize)) - 1)) << logsize;
18759         inst.instruction = 0xe100b10;
18760         do_vfp_cond_or_thumb ();
18761         inst.instruction |= LOW4 (dn) << 16;
18762         inst.instruction |= HI1 (dn) << 7;
18763         inst.instruction |= inst.operands[0].reg << 12;
18764         inst.instruction |= (abcdebits & 3) << 5;
18765         inst.instruction |= (abcdebits >> 2) << 21;
18766         inst.instruction |= (x >> (3-logsize)) << 16;
18767       }
18768       break;
18769
18770     case NS_RRD:  /* case 7 (fmrrd).  */
18771       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
18772                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
18773                   _(BAD_FPU));
18774
18775       inst.instruction = 0xc500b10;
18776       do_vfp_cond_or_thumb ();
18777       inst.instruction |= inst.operands[0].reg << 12;
18778       inst.instruction |= inst.operands[1].reg << 16;
18779       inst.instruction |= LOW4 (inst.operands[2].reg);
18780       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
18781       break;
18782
18783     case NS_FF:  /* case 8 (fcpys).  */
18784       do_vfp_nsyn_opcode ("fcpys");
18785       break;
18786
18787     case NS_HI:
18788     case NS_FI:  /* case 10 (fconsts).  */
18789       ldconst = "fconsts";
18790     encode_fconstd:
18791       if (!inst.operands[1].immisfloat)
18792         {
18793           unsigned new_imm;
18794           /* Immediate has to fit in 8 bits so float is enough.  */
18795           float imm = (float) inst.operands[1].imm;
18796           memcpy (&new_imm, &imm, sizeof (float));
18797           /* But the assembly may have been written to provide an integer
18798              bit pattern that equates to a float, so check that the
18799              conversion has worked.  */
18800           if (is_quarter_float (new_imm))
18801             {
18802               if (is_quarter_float (inst.operands[1].imm))
18803                 as_warn (_("immediate constant is valid both as a bit-pattern and a floating point value (using the fp value)"));
18804
18805               inst.operands[1].imm = new_imm;
18806               inst.operands[1].immisfloat = 1;
18807             }
18808         }
18809
18810       if (is_quarter_float (inst.operands[1].imm))
18811         {
18812           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
18813           do_vfp_nsyn_opcode (ldconst);
18814
18815           /* ARMv8.2 fp16 vmov.f16 instruction.  */
18816           if (rs == NS_HI)
18817             do_scalar_fp16_v82_encode ();
18818         }
18819       else
18820         first_error (_("immediate out of range"));
18821       break;
18822
18823     case NS_RH:
18824     case NS_RF:  /* case 12 (fmrs).  */
18825       do_vfp_nsyn_opcode ("fmrs");
18826       /* ARMv8.2 fp16 vmov.f16 instruction.  */
18827       if (rs == NS_RH)
18828         do_scalar_fp16_v82_encode ();
18829       break;
18830
18831     case NS_HR:
18832     case NS_FR:  /* case 13 (fmsr).  */
18833       do_vfp_nsyn_opcode ("fmsr");
18834       /* ARMv8.2 fp16 vmov.f16 instruction.  */
18835       if (rs == NS_HR)
18836         do_scalar_fp16_v82_encode ();
18837       break;
18838
18839     case NS_RRSS:
18840       do_mve_mov (0);
18841       break;
18842     case NS_SSRR:
18843       do_mve_mov (1);
18844       break;
18845
18846     /* The encoders for the fmrrs and fmsrr instructions expect three operands
18847        (one of which is a list), but we have parsed four.  Do some fiddling to
18848        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
18849        expect.  */
18850     case NS_RRFF:  /* case 14 (fmrrs).  */
18851       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
18852                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
18853                   _(BAD_FPU));
18854       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
18855                   _("VFP registers must be adjacent"));
18856       inst.operands[2].imm = 2;
18857       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
18858       do_vfp_nsyn_opcode ("fmrrs");
18859       break;
18860
18861     case NS_FFRR:  /* case 15 (fmsrr).  */
18862       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2)
18863                   && !ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext),
18864                   _(BAD_FPU));
18865       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
18866                   _("VFP registers must be adjacent"));
18867       inst.operands[1] = inst.operands[2];
18868       inst.operands[2] = inst.operands[3];
18869       inst.operands[0].imm = 2;
18870       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
18871       do_vfp_nsyn_opcode ("fmsrr");
18872       break;
18873
18874     case NS_NULL:
18875       /* neon_select_shape has determined that the instruction
18876          shape is wrong and has already set the error message.  */
18877       break;
18878
18879     default:
18880       abort ();
18881     }
18882 }
18883
18884 static void
18885 do_mve_movl (void)
18886 {
18887   if (!(inst.operands[0].present && inst.operands[0].isquad
18888       && inst.operands[1].present && inst.operands[1].isquad
18889       && !inst.operands[2].present))
18890     {
18891       inst.instruction = 0;
18892       inst.cond = 0xb;
18893       if (thumb_mode)
18894         set_pred_insn_type (INSIDE_IT_INSN);
18895       do_neon_mov ();
18896       return;
18897     }
18898
18899   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18900     return;
18901
18902   if (inst.cond != COND_ALWAYS)
18903     inst.pred_insn_type = INSIDE_VPT_INSN;
18904
18905   struct neon_type_el et = neon_check_type (2, NS_QQ, N_EQK, N_S8 | N_U8
18906                                             | N_S16 | N_U16 | N_KEY);
18907
18908   inst.instruction |= (et.type == NT_unsigned) << 28;
18909   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18910   inst.instruction |= (neon_logbits (et.size) + 1) << 19;
18911   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18912   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18913   inst.instruction |= LOW4 (inst.operands[1].reg);
18914   inst.is_neon = 1;
18915 }
18916
18917 static void
18918 do_neon_rshift_round_imm (void)
18919 {
18920   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
18921   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
18922   int imm = inst.operands[2].imm;
18923
18924   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
18925   if (imm == 0)
18926     {
18927       inst.operands[2].present = 0;
18928       do_neon_mov ();
18929       return;
18930     }
18931
18932   constraint (imm < 1 || (unsigned)imm > et.size,
18933               _("immediate out of range for shift"));
18934   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
18935                   et.size - imm);
18936 }
18937
18938 static void
18939 do_neon_movhf (void)
18940 {
18941   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
18942   constraint (rs != NS_HH, _("invalid suffix"));
18943
18944   if (inst.cond != COND_ALWAYS)
18945     {
18946       if (thumb_mode)
18947         {
18948           as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
18949                      " the behaviour is UNPREDICTABLE"));
18950         }
18951       else
18952         {
18953           inst.error = BAD_COND;
18954           return;
18955         }
18956     }
18957
18958   do_vfp_sp_monadic ();
18959
18960   inst.is_neon = 1;
18961   inst.instruction |= 0xf0000000;
18962 }
18963
18964 static void
18965 do_neon_movl (void)
18966 {
18967   struct neon_type_el et = neon_check_type (2, NS_QD,
18968     N_EQK | N_DBL, N_SU_32 | N_KEY);
18969   unsigned sizebits = et.size >> 3;
18970   inst.instruction |= sizebits << 19;
18971   neon_two_same (0, et.type == NT_unsigned, -1);
18972 }
18973
18974 static void
18975 do_neon_trn (void)
18976 {
18977   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18978   struct neon_type_el et = neon_check_type (2, rs,
18979     N_EQK, N_8 | N_16 | N_32 | N_KEY);
18980   NEON_ENCODE (INTEGER, inst);
18981   neon_two_same (neon_quad (rs), 1, et.size);
18982 }
18983
18984 static void
18985 do_neon_zip_uzp (void)
18986 {
18987   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
18988   struct neon_type_el et = neon_check_type (2, rs,
18989     N_EQK, N_8 | N_16 | N_32 | N_KEY);
18990   if (rs == NS_DD && et.size == 32)
18991     {
18992       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
18993       inst.instruction = N_MNEM_vtrn;
18994       do_neon_trn ();
18995       return;
18996     }
18997   neon_two_same (neon_quad (rs), 1, et.size);
18998 }
18999
19000 static void
19001 do_neon_sat_abs_neg (void)
19002 {
19003   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19004   struct neon_type_el et = neon_check_type (2, rs,
19005     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
19006   neon_two_same (neon_quad (rs), 1, et.size);
19007 }
19008
19009 static void
19010 do_neon_pair_long (void)
19011 {
19012   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19013   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
19014   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
19015   inst.instruction |= (et.type == NT_unsigned) << 7;
19016   neon_two_same (neon_quad (rs), 1, et.size);
19017 }
19018
19019 static void
19020 do_neon_recip_est (void)
19021 {
19022   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19023   struct neon_type_el et = neon_check_type (2, rs,
19024     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
19025   inst.instruction |= (et.type == NT_float) << 8;
19026   neon_two_same (neon_quad (rs), 1, et.size);
19027 }
19028
19029 static void
19030 do_neon_cls (void)
19031 {
19032   if (check_simd_pred_availability (0, NEON_CHECK_ARCH | NEON_CHECK_CC))
19033     return;
19034
19035   enum neon_shape rs;
19036   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19037    rs = neon_select_shape (NS_QQ, NS_NULL);
19038   else
19039    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19040
19041   struct neon_type_el et = neon_check_type (2, rs,
19042     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
19043   neon_two_same (neon_quad (rs), 1, et.size);
19044 }
19045
19046 static void
19047 do_neon_clz (void)
19048 {
19049   if (check_simd_pred_availability (0, NEON_CHECK_ARCH | NEON_CHECK_CC))
19050     return;
19051
19052   enum neon_shape rs;
19053   if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19054    rs = neon_select_shape (NS_QQ, NS_NULL);
19055   else
19056    rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19057
19058   struct neon_type_el et = neon_check_type (2, rs,
19059     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
19060   neon_two_same (neon_quad (rs), 1, et.size);
19061 }
19062
19063 static void
19064 do_neon_cnt (void)
19065 {
19066   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19067   struct neon_type_el et = neon_check_type (2, rs,
19068     N_EQK | N_INT, N_8 | N_KEY);
19069   neon_two_same (neon_quad (rs), 1, et.size);
19070 }
19071
19072 static void
19073 do_neon_swp (void)
19074 {
19075   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
19076   neon_two_same (neon_quad (rs), 1, -1);
19077 }
19078
19079 static void
19080 do_neon_tbl_tbx (void)
19081 {
19082   unsigned listlenbits;
19083   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
19084
19085   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
19086     {
19087       first_error (_("bad list length for table lookup"));
19088       return;
19089     }
19090
19091   listlenbits = inst.operands[1].imm - 1;
19092   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19093   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19094   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19095   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19096   inst.instruction |= LOW4 (inst.operands[2].reg);
19097   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19098   inst.instruction |= listlenbits << 8;
19099
19100   neon_dp_fixup (&inst);
19101 }
19102
19103 static void
19104 do_neon_ldm_stm (void)
19105 {
19106   /* P, U and L bits are part of bitmask.  */
19107   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
19108   unsigned offsetbits = inst.operands[1].imm * 2;
19109
19110   if (inst.operands[1].issingle)
19111     {
19112       do_vfp_nsyn_ldm_stm (is_dbmode);
19113       return;
19114     }
19115
19116   constraint (is_dbmode && !inst.operands[0].writeback,
19117               _("writeback (!) must be used for VLDMDB and VSTMDB"));
19118
19119   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
19120               _("register list must contain at least 1 and at most 16 "
19121                 "registers"));
19122
19123   inst.instruction |= inst.operands[0].reg << 16;
19124   inst.instruction |= inst.operands[0].writeback << 21;
19125   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
19126   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
19127
19128   inst.instruction |= offsetbits;
19129
19130   do_vfp_cond_or_thumb ();
19131 }
19132
19133 static void
19134 do_neon_ldr_str (void)
19135 {
19136   int is_ldr = (inst.instruction & (1 << 20)) != 0;
19137
19138   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
19139      And is UNPREDICTABLE in thumb mode.  */
19140   if (!is_ldr
19141       && inst.operands[1].reg == REG_PC
19142       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
19143     {
19144       if (thumb_mode)
19145         inst.error = _("Use of PC here is UNPREDICTABLE");
19146       else if (warn_on_deprecated)
19147         as_tsktsk (_("Use of PC here is deprecated"));
19148     }
19149
19150   if (inst.operands[0].issingle)
19151     {
19152       if (is_ldr)
19153         do_vfp_nsyn_opcode ("flds");
19154       else
19155         do_vfp_nsyn_opcode ("fsts");
19156
19157       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
19158       if (inst.vectype.el[0].size == 16)
19159         do_scalar_fp16_v82_encode ();
19160     }
19161   else
19162     {
19163       if (is_ldr)
19164         do_vfp_nsyn_opcode ("fldd");
19165       else
19166         do_vfp_nsyn_opcode ("fstd");
19167     }
19168 }
19169
19170 static void
19171 do_t_vldr_vstr_sysreg (void)
19172 {
19173   int fp_vldr_bitno = 20, sysreg_vldr_bitno = 20;
19174   bfd_boolean is_vldr = ((inst.instruction & (1 << fp_vldr_bitno)) != 0);
19175
19176   /* Use of PC is UNPREDICTABLE.  */
19177   if (inst.operands[1].reg == REG_PC)
19178     inst.error = _("Use of PC here is UNPREDICTABLE");
19179
19180   if (inst.operands[1].immisreg)
19181     inst.error = _("instruction does not accept register index");
19182
19183   if (!inst.operands[1].isreg)
19184     inst.error = _("instruction does not accept PC-relative addressing");
19185
19186   if (abs (inst.operands[1].imm) >= (1 << 7))
19187     inst.error = _("immediate value out of range");
19188
19189   inst.instruction = 0xec000f80;
19190   if (is_vldr)
19191     inst.instruction |= 1 << sysreg_vldr_bitno;
19192   encode_arm_cp_address (1, TRUE, FALSE, BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM);
19193   inst.instruction |= (inst.operands[0].imm & 0x7) << 13;
19194   inst.instruction |= (inst.operands[0].imm & 0x8) << 19;
19195 }
19196
19197 static void
19198 do_vldr_vstr (void)
19199 {
19200   bfd_boolean sysreg_op = !inst.operands[0].isreg;
19201
19202   /* VLDR/VSTR (System Register).  */
19203   if (sysreg_op)
19204     {
19205       if (!mark_feature_used (&arm_ext_v8_1m_main))
19206         as_bad (_("Instruction not permitted on this architecture"));
19207
19208       do_t_vldr_vstr_sysreg ();
19209     }
19210   /* VLDR/VSTR.  */
19211   else
19212     {
19213       if (!mark_feature_used (&fpu_vfp_ext_v1xd))
19214         as_bad (_("Instruction not permitted on this architecture"));
19215       do_neon_ldr_str ();
19216     }
19217 }
19218
19219 /* "interleave" version also handles non-interleaving register VLD1/VST1
19220    instructions.  */
19221
19222 static void
19223 do_neon_ld_st_interleave (void)
19224 {
19225   struct neon_type_el et = neon_check_type (1, NS_NULL,
19226                                             N_8 | N_16 | N_32 | N_64);
19227   unsigned alignbits = 0;
19228   unsigned idx;
19229   /* The bits in this table go:
19230      0: register stride of one (0) or two (1)
19231      1,2: register list length, minus one (1, 2, 3, 4).
19232      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
19233      We use -1 for invalid entries.  */
19234   const int typetable[] =
19235     {
19236       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
19237        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
19238        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
19239        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
19240     };
19241   int typebits;
19242
19243   if (et.type == NT_invtype)
19244     return;
19245
19246   if (inst.operands[1].immisalign)
19247     switch (inst.operands[1].imm >> 8)
19248       {
19249       case 64: alignbits = 1; break;
19250       case 128:
19251         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
19252             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
19253           goto bad_alignment;
19254         alignbits = 2;
19255         break;
19256       case 256:
19257         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
19258           goto bad_alignment;
19259         alignbits = 3;
19260         break;
19261       default:
19262       bad_alignment:
19263         first_error (_("bad alignment"));
19264         return;
19265       }
19266
19267   inst.instruction |= alignbits << 4;
19268   inst.instruction |= neon_logbits (et.size) << 6;
19269
19270   /* Bits [4:6] of the immediate in a list specifier encode register stride
19271      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
19272      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
19273      up the right value for "type" in a table based on this value and the given
19274      list style, then stick it back.  */
19275   idx = ((inst.operands[0].imm >> 4) & 7)
19276         | (((inst.instruction >> 8) & 3) << 3);
19277
19278   typebits = typetable[idx];
19279
19280   constraint (typebits == -1, _("bad list type for instruction"));
19281   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
19282               BAD_EL_TYPE);
19283
19284   inst.instruction &= ~0xf00;
19285   inst.instruction |= typebits << 8;
19286 }
19287
19288 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
19289    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
19290    otherwise. The variable arguments are a list of pairs of legal (size, align)
19291    values, terminated with -1.  */
19292
19293 static int
19294 neon_alignment_bit (int size, int align, int *do_alignment, ...)
19295 {
19296   va_list ap;
19297   int result = FAIL, thissize, thisalign;
19298
19299   if (!inst.operands[1].immisalign)
19300     {
19301       *do_alignment = 0;
19302       return SUCCESS;
19303     }
19304
19305   va_start (ap, do_alignment);
19306
19307   do
19308     {
19309       thissize = va_arg (ap, int);
19310       if (thissize == -1)
19311         break;
19312       thisalign = va_arg (ap, int);
19313
19314       if (size == thissize && align == thisalign)
19315         result = SUCCESS;
19316     }
19317   while (result != SUCCESS);
19318
19319   va_end (ap);
19320
19321   if (result == SUCCESS)
19322     *do_alignment = 1;
19323   else
19324     first_error (_("unsupported alignment for instruction"));
19325
19326   return result;
19327 }
19328
19329 static void
19330 do_neon_ld_st_lane (void)
19331 {
19332   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
19333   int align_good, do_alignment = 0;
19334   int logsize = neon_logbits (et.size);
19335   int align = inst.operands[1].imm >> 8;
19336   int n = (inst.instruction >> 8) & 3;
19337   int max_el = 64 / et.size;
19338
19339   if (et.type == NT_invtype)
19340     return;
19341
19342   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
19343               _("bad list length"));
19344   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
19345               _("scalar index out of range"));
19346   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
19347               && et.size == 8,
19348               _("stride of 2 unavailable when element size is 8"));
19349
19350   switch (n)
19351     {
19352     case 0:  /* VLD1 / VST1.  */
19353       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
19354                                        32, 32, -1);
19355       if (align_good == FAIL)
19356         return;
19357       if (do_alignment)
19358         {
19359           unsigned alignbits = 0;
19360           switch (et.size)
19361             {
19362             case 16: alignbits = 0x1; break;
19363             case 32: alignbits = 0x3; break;
19364             default: ;
19365             }
19366           inst.instruction |= alignbits << 4;
19367         }
19368       break;
19369
19370     case 1:  /* VLD2 / VST2.  */
19371       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
19372                       16, 32, 32, 64, -1);
19373       if (align_good == FAIL)
19374         return;
19375       if (do_alignment)
19376         inst.instruction |= 1 << 4;
19377       break;
19378
19379     case 2:  /* VLD3 / VST3.  */
19380       constraint (inst.operands[1].immisalign,
19381                   _("can't use alignment with this instruction"));
19382       break;
19383
19384     case 3:  /* VLD4 / VST4.  */
19385       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
19386                                        16, 64, 32, 64, 32, 128, -1);
19387       if (align_good == FAIL)
19388         return;
19389       if (do_alignment)
19390         {
19391           unsigned alignbits = 0;
19392           switch (et.size)
19393             {
19394             case 8:  alignbits = 0x1; break;
19395             case 16: alignbits = 0x1; break;
19396             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
19397             default: ;
19398             }
19399           inst.instruction |= alignbits << 4;
19400         }
19401       break;
19402
19403     default: ;
19404     }
19405
19406   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
19407   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
19408     inst.instruction |= 1 << (4 + logsize);
19409
19410   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
19411   inst.instruction |= logsize << 10;
19412 }
19413
19414 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
19415
19416 static void
19417 do_neon_ld_dup (void)
19418 {
19419   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
19420   int align_good, do_alignment = 0;
19421
19422   if (et.type == NT_invtype)
19423     return;
19424
19425   switch ((inst.instruction >> 8) & 3)
19426     {
19427     case 0:  /* VLD1.  */
19428       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
19429       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
19430                                        &do_alignment, 16, 16, 32, 32, -1);
19431       if (align_good == FAIL)
19432         return;
19433       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
19434         {
19435         case 1: break;
19436         case 2: inst.instruction |= 1 << 5; break;
19437         default: first_error (_("bad list length")); return;
19438         }
19439       inst.instruction |= neon_logbits (et.size) << 6;
19440       break;
19441
19442     case 1:  /* VLD2.  */
19443       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
19444                                        &do_alignment, 8, 16, 16, 32, 32, 64,
19445                                        -1);
19446       if (align_good == FAIL)
19447         return;
19448       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
19449                   _("bad list length"));
19450       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
19451         inst.instruction |= 1 << 5;
19452       inst.instruction |= neon_logbits (et.size) << 6;
19453       break;
19454
19455     case 2:  /* VLD3.  */
19456       constraint (inst.operands[1].immisalign,
19457                   _("can't use alignment with this instruction"));
19458       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
19459                   _("bad list length"));
19460       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
19461         inst.instruction |= 1 << 5;
19462       inst.instruction |= neon_logbits (et.size) << 6;
19463       break;
19464
19465     case 3:  /* VLD4.  */
19466       {
19467         int align = inst.operands[1].imm >> 8;
19468         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
19469                                          16, 64, 32, 64, 32, 128, -1);
19470         if (align_good == FAIL)
19471           return;
19472         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
19473                     _("bad list length"));
19474         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
19475           inst.instruction |= 1 << 5;
19476         if (et.size == 32 && align == 128)
19477           inst.instruction |= 0x3 << 6;
19478         else
19479           inst.instruction |= neon_logbits (et.size) << 6;
19480       }
19481       break;
19482
19483     default: ;
19484     }
19485
19486   inst.instruction |= do_alignment << 4;
19487 }
19488
19489 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
19490    apart from bits [11:4].  */
19491
19492 static void
19493 do_neon_ldx_stx (void)
19494 {
19495   if (inst.operands[1].isreg)
19496     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
19497
19498   switch (NEON_LANE (inst.operands[0].imm))
19499     {
19500     case NEON_INTERLEAVE_LANES:
19501       NEON_ENCODE (INTERLV, inst);
19502       do_neon_ld_st_interleave ();
19503       break;
19504
19505     case NEON_ALL_LANES:
19506       NEON_ENCODE (DUP, inst);
19507       if (inst.instruction == N_INV)
19508         {
19509           first_error ("only loads support such operands");
19510           break;
19511         }
19512       do_neon_ld_dup ();
19513       break;
19514
19515     default:
19516       NEON_ENCODE (LANE, inst);
19517       do_neon_ld_st_lane ();
19518     }
19519
19520   /* L bit comes from bit mask.  */
19521   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19522   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19523   inst.instruction |= inst.operands[1].reg << 16;
19524
19525   if (inst.operands[1].postind)
19526     {
19527       int postreg = inst.operands[1].imm & 0xf;
19528       constraint (!inst.operands[1].immisreg,
19529                   _("post-index must be a register"));
19530       constraint (postreg == 0xd || postreg == 0xf,
19531                   _("bad register for post-index"));
19532       inst.instruction |= postreg;
19533     }
19534   else
19535     {
19536       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
19537       constraint (inst.relocs[0].exp.X_op != O_constant
19538                   || inst.relocs[0].exp.X_add_number != 0,
19539                   BAD_ADDR_MODE);
19540
19541       if (inst.operands[1].writeback)
19542         {
19543           inst.instruction |= 0xd;
19544         }
19545       else
19546         inst.instruction |= 0xf;
19547     }
19548
19549   if (thumb_mode)
19550     inst.instruction |= 0xf9000000;
19551   else
19552     inst.instruction |= 0xf4000000;
19553 }
19554
19555 /* FP v8.  */
19556 static void
19557 do_vfp_nsyn_fpv8 (enum neon_shape rs)
19558 {
19559   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
19560      D register operands.  */
19561   if (neon_shape_class[rs] == SC_DOUBLE)
19562     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19563                 _(BAD_FPU));
19564
19565   NEON_ENCODE (FPV8, inst);
19566
19567   if (rs == NS_FFF || rs == NS_HHH)
19568     {
19569       do_vfp_sp_dyadic ();
19570
19571       /* ARMv8.2 fp16 instruction.  */
19572       if (rs == NS_HHH)
19573         do_scalar_fp16_v82_encode ();
19574     }
19575   else
19576     do_vfp_dp_rd_rn_rm ();
19577
19578   if (rs == NS_DDD)
19579     inst.instruction |= 0x100;
19580
19581   inst.instruction |= 0xf0000000;
19582 }
19583
19584 static void
19585 do_vsel (void)
19586 {
19587   set_pred_insn_type (OUTSIDE_PRED_INSN);
19588
19589   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
19590     first_error (_("invalid instruction shape"));
19591 }
19592
19593 static void
19594 do_vmaxnm (void)
19595 {
19596   set_pred_insn_type (OUTSIDE_PRED_INSN);
19597
19598   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
19599     return;
19600
19601   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
19602     return;
19603
19604   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
19605 }
19606
19607 static void
19608 do_vrint_1 (enum neon_cvt_mode mode)
19609 {
19610   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
19611   struct neon_type_el et;
19612
19613   if (rs == NS_NULL)
19614     return;
19615
19616   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
19617      D register operands.  */
19618   if (neon_shape_class[rs] == SC_DOUBLE)
19619     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
19620                 _(BAD_FPU));
19621
19622   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
19623                         | N_VFP);
19624   if (et.type != NT_invtype)
19625     {
19626       /* VFP encodings.  */
19627       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
19628           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
19629         set_pred_insn_type (OUTSIDE_PRED_INSN);
19630
19631       NEON_ENCODE (FPV8, inst);
19632       if (rs == NS_FF || rs == NS_HH)
19633         do_vfp_sp_monadic ();
19634       else
19635         do_vfp_dp_rd_rm ();
19636
19637       switch (mode)
19638         {
19639         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
19640         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
19641         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
19642         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
19643         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
19644         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
19645         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
19646         default: abort ();
19647         }
19648
19649       inst.instruction |= (rs == NS_DD) << 8;
19650       do_vfp_cond_or_thumb ();
19651
19652       /* ARMv8.2 fp16 vrint instruction.  */
19653       if (rs == NS_HH)
19654       do_scalar_fp16_v82_encode ();
19655     }
19656   else
19657     {
19658       /* Neon encodings (or something broken...).  */
19659       inst.error = NULL;
19660       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
19661
19662       if (et.type == NT_invtype)
19663         return;
19664
19665       set_pred_insn_type (OUTSIDE_PRED_INSN);
19666       NEON_ENCODE (FLOAT, inst);
19667
19668       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
19669         return;
19670
19671       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19672       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19673       inst.instruction |= LOW4 (inst.operands[1].reg);
19674       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19675       inst.instruction |= neon_quad (rs) << 6;
19676       /* Mask off the original size bits and reencode them.  */
19677       inst.instruction = ((inst.instruction & 0xfff3ffff)
19678                           | neon_logbits (et.size) << 18);
19679
19680       switch (mode)
19681         {
19682         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
19683         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
19684         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
19685         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
19686         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
19687         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
19688         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
19689         default: abort ();
19690         }
19691
19692       if (thumb_mode)
19693         inst.instruction |= 0xfc000000;
19694       else
19695         inst.instruction |= 0xf0000000;
19696     }
19697 }
19698
19699 static void
19700 do_vrintx (void)
19701 {
19702   do_vrint_1 (neon_cvt_mode_x);
19703 }
19704
19705 static void
19706 do_vrintz (void)
19707 {
19708   do_vrint_1 (neon_cvt_mode_z);
19709 }
19710
19711 static void
19712 do_vrintr (void)
19713 {
19714   do_vrint_1 (neon_cvt_mode_r);
19715 }
19716
19717 static void
19718 do_vrinta (void)
19719 {
19720   do_vrint_1 (neon_cvt_mode_a);
19721 }
19722
19723 static void
19724 do_vrintn (void)
19725 {
19726   do_vrint_1 (neon_cvt_mode_n);
19727 }
19728
19729 static void
19730 do_vrintp (void)
19731 {
19732   do_vrint_1 (neon_cvt_mode_p);
19733 }
19734
19735 static void
19736 do_vrintm (void)
19737 {
19738   do_vrint_1 (neon_cvt_mode_m);
19739 }
19740
19741 static unsigned
19742 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
19743 {
19744   unsigned regno = NEON_SCALAR_REG (opnd);
19745   unsigned elno = NEON_SCALAR_INDEX (opnd);
19746
19747   if (elsize == 16 && elno < 2 && regno < 16)
19748     return regno | (elno << 4);
19749   else if (elsize == 32 && elno == 0)
19750     return regno;
19751
19752   first_error (_("scalar out of range"));
19753   return 0;
19754 }
19755
19756 static void
19757 do_vcmla (void)
19758 {
19759   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext)
19760               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
19761                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
19762   constraint (inst.relocs[0].exp.X_op != O_constant,
19763               _("expression too complex"));
19764   unsigned rot = inst.relocs[0].exp.X_add_number;
19765   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
19766               _("immediate out of range"));
19767   rot /= 90;
19768
19769   if (check_simd_pred_availability (1, NEON_CHECK_ARCH8 | NEON_CHECK_CC))
19770     return;
19771
19772   if (inst.operands[2].isscalar)
19773     {
19774       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
19775         first_error (_("invalid instruction shape"));
19776       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
19777       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
19778                                        N_KEY | N_F16 | N_F32).size;
19779       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
19780       inst.is_neon = 1;
19781       inst.instruction = 0xfe000800;
19782       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19783       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19784       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19785       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19786       inst.instruction |= LOW4 (m);
19787       inst.instruction |= HI1 (m) << 5;
19788       inst.instruction |= neon_quad (rs) << 6;
19789       inst.instruction |= rot << 20;
19790       inst.instruction |= (size == 32) << 23;
19791     }
19792   else
19793     {
19794       enum neon_shape rs;
19795       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext))
19796         rs = neon_select_shape (NS_QQQI, NS_NULL);
19797       else
19798         rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
19799
19800       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
19801                                        N_KEY | N_F16 | N_F32).size;
19802       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_fp_ext) && size == 32
19803           && (inst.operands[0].reg == inst.operands[1].reg
19804               || inst.operands[0].reg == inst.operands[2].reg))
19805         as_tsktsk (BAD_MVE_SRCDEST);
19806
19807       neon_three_same (neon_quad (rs), 0, -1);
19808       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
19809       inst.instruction |= 0xfc200800;
19810       inst.instruction |= rot << 23;
19811       inst.instruction |= (size == 32) << 20;
19812     }
19813 }
19814
19815 static void
19816 do_vcadd (void)
19817 {
19818   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
19819               && (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8)
19820                   || !mark_feature_used (&arm_ext_v8_3)), (BAD_FPU));
19821   constraint (inst.relocs[0].exp.X_op != O_constant,
19822               _("expression too complex"));
19823
19824   unsigned rot = inst.relocs[0].exp.X_add_number;
19825   constraint (rot != 90 && rot != 270, _("immediate out of range"));
19826   enum neon_shape rs;
19827   struct neon_type_el et;
19828   if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
19829     {
19830       rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
19831       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32);
19832     }
19833   else
19834     {
19835       rs = neon_select_shape (NS_QQQI, NS_NULL);
19836       et = neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16 | N_F32 | N_I8
19837                             | N_I16 | N_I32);
19838       if (et.size == 32 && inst.operands[0].reg == inst.operands[2].reg)
19839         as_tsktsk (_("Warning: 32-bit element size and same first and third "
19840                      "operand makes instruction UNPREDICTABLE"));
19841     }
19842
19843   if (et.type == NT_invtype)
19844     return;
19845
19846   if (check_simd_pred_availability (et.type == NT_float, NEON_CHECK_ARCH8
19847                                     | NEON_CHECK_CC))
19848     return;
19849
19850   if (et.type == NT_float)
19851     {
19852       neon_three_same (neon_quad (rs), 0, -1);
19853       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
19854       inst.instruction |= 0xfc800800;
19855       inst.instruction |= (rot == 270) << 24;
19856       inst.instruction |= (et.size == 32) << 20;
19857     }
19858   else
19859     {
19860       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext), BAD_FPU);
19861       inst.instruction = 0xfe000f00;
19862       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19863       inst.instruction |= neon_logbits (et.size) << 20;
19864       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
19865       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19866       inst.instruction |= (rot == 270) << 12;
19867       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
19868       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
19869       inst.instruction |= LOW4 (inst.operands[2].reg);
19870       inst.is_neon = 1;
19871     }
19872 }
19873
19874 /* Dot Product instructions encoding support.  */
19875
19876 static void
19877 do_neon_dotproduct (int unsigned_p)
19878 {
19879   enum neon_shape rs;
19880   unsigned scalar_oprd2 = 0;
19881   int high8;
19882
19883   if (inst.cond != COND_ALWAYS)
19884     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
19885                "is UNPREDICTABLE"));
19886
19887   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
19888               _(BAD_FPU));
19889
19890   /* Dot Product instructions are in three-same D/Q register format or the third
19891      operand can be a scalar index register.  */
19892   if (inst.operands[2].isscalar)
19893     {
19894       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
19895       high8 = 0xfe000000;
19896       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
19897     }
19898   else
19899     {
19900       high8 = 0xfc000000;
19901       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
19902     }
19903
19904   if (unsigned_p)
19905     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
19906   else
19907     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
19908
19909   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
19910      Product instruction, so we pass 0 as the "ubit" parameter.  And the
19911      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
19912   neon_three_same (neon_quad (rs), 0, 32);
19913
19914   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
19915      different NEON three-same encoding.  */
19916   inst.instruction &= 0x00ffffff;
19917   inst.instruction |= high8;
19918   /* Encode 'U' bit which indicates signedness.  */
19919   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
19920   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
19921      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
19922      the instruction encoding.  */
19923   if (inst.operands[2].isscalar)
19924     {
19925       inst.instruction &= 0xffffffd0;
19926       inst.instruction |= LOW4 (scalar_oprd2);
19927       inst.instruction |= HI1 (scalar_oprd2) << 5;
19928     }
19929 }
19930
19931 /* Dot Product instructions for signed integer.  */
19932
19933 static void
19934 do_neon_dotproduct_s (void)
19935 {
19936   return do_neon_dotproduct (0);
19937 }
19938
19939 /* Dot Product instructions for unsigned integer.  */
19940
19941 static void
19942 do_neon_dotproduct_u (void)
19943 {
19944   return do_neon_dotproduct (1);
19945 }
19946
19947 /* Crypto v1 instructions.  */
19948 static void
19949 do_crypto_2op_1 (unsigned elttype, int op)
19950 {
19951   set_pred_insn_type (OUTSIDE_PRED_INSN);
19952
19953   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
19954       == NT_invtype)
19955     return;
19956
19957   inst.error = NULL;
19958
19959   NEON_ENCODE (INTEGER, inst);
19960   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
19961   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
19962   inst.instruction |= LOW4 (inst.operands[1].reg);
19963   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
19964   if (op != -1)
19965     inst.instruction |= op << 6;
19966
19967   if (thumb_mode)
19968     inst.instruction |= 0xfc000000;
19969   else
19970     inst.instruction |= 0xf0000000;
19971 }
19972
19973 static void
19974 do_crypto_3op_1 (int u, int op)
19975 {
19976   set_pred_insn_type (OUTSIDE_PRED_INSN);
19977
19978   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
19979                        N_32 | N_UNT | N_KEY).type == NT_invtype)
19980     return;
19981
19982   inst.error = NULL;
19983
19984   NEON_ENCODE (INTEGER, inst);
19985   neon_three_same (1, u, 8 << op);
19986 }
19987
19988 static void
19989 do_aese (void)
19990 {
19991   do_crypto_2op_1 (N_8, 0);
19992 }
19993
19994 static void
19995 do_aesd (void)
19996 {
19997   do_crypto_2op_1 (N_8, 1);
19998 }
19999
20000 static void
20001 do_aesmc (void)
20002 {
20003   do_crypto_2op_1 (N_8, 2);
20004 }
20005
20006 static void
20007 do_aesimc (void)
20008 {
20009   do_crypto_2op_1 (N_8, 3);
20010 }
20011
20012 static void
20013 do_sha1c (void)
20014 {
20015   do_crypto_3op_1 (0, 0);
20016 }
20017
20018 static void
20019 do_sha1p (void)
20020 {
20021   do_crypto_3op_1 (0, 1);
20022 }
20023
20024 static void
20025 do_sha1m (void)
20026 {
20027   do_crypto_3op_1 (0, 2);
20028 }
20029
20030 static void
20031 do_sha1su0 (void)
20032 {
20033   do_crypto_3op_1 (0, 3);
20034 }
20035
20036 static void
20037 do_sha256h (void)
20038 {
20039   do_crypto_3op_1 (1, 0);
20040 }
20041
20042 static void
20043 do_sha256h2 (void)
20044 {
20045   do_crypto_3op_1 (1, 1);
20046 }
20047
20048 static void
20049 do_sha256su1 (void)
20050 {
20051   do_crypto_3op_1 (1, 2);
20052 }
20053
20054 static void
20055 do_sha1h (void)
20056 {
20057   do_crypto_2op_1 (N_32, -1);
20058 }
20059
20060 static void
20061 do_sha1su1 (void)
20062 {
20063   do_crypto_2op_1 (N_32, 0);
20064 }
20065
20066 static void
20067 do_sha256su0 (void)
20068 {
20069   do_crypto_2op_1 (N_32, 1);
20070 }
20071
20072 static void
20073 do_crc32_1 (unsigned int poly, unsigned int sz)
20074 {
20075   unsigned int Rd = inst.operands[0].reg;
20076   unsigned int Rn = inst.operands[1].reg;
20077   unsigned int Rm = inst.operands[2].reg;
20078
20079   set_pred_insn_type (OUTSIDE_PRED_INSN);
20080   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
20081   inst.instruction |= LOW4 (Rn) << 16;
20082   inst.instruction |= LOW4 (Rm);
20083   inst.instruction |= sz << (thumb_mode ? 4 : 21);
20084   inst.instruction |= poly << (thumb_mode ? 20 : 9);
20085
20086   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
20087     as_warn (UNPRED_REG ("r15"));
20088 }
20089
20090 static void
20091 do_crc32b (void)
20092 {
20093   do_crc32_1 (0, 0);
20094 }
20095
20096 static void
20097 do_crc32h (void)
20098 {
20099   do_crc32_1 (0, 1);
20100 }
20101
20102 static void
20103 do_crc32w (void)
20104 {
20105   do_crc32_1 (0, 2);
20106 }
20107
20108 static void
20109 do_crc32cb (void)
20110 {
20111   do_crc32_1 (1, 0);
20112 }
20113
20114 static void
20115 do_crc32ch (void)
20116 {
20117   do_crc32_1 (1, 1);
20118 }
20119
20120 static void
20121 do_crc32cw (void)
20122 {
20123   do_crc32_1 (1, 2);
20124 }
20125
20126 static void
20127 do_vjcvt (void)
20128 {
20129   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
20130               _(BAD_FPU));
20131   neon_check_type (2, NS_FD, N_S32, N_F64);
20132   do_vfp_sp_dp_cvt ();
20133   do_vfp_cond_or_thumb ();
20134 }
20135
20136 \f
20137 /* Overall per-instruction processing.  */
20138
20139 /* We need to be able to fix up arbitrary expressions in some statements.
20140    This is so that we can handle symbols that are an arbitrary distance from
20141    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
20142    which returns part of an address in a form which will be valid for
20143    a data instruction.  We do this by pushing the expression into a symbol
20144    in the expr_section, and creating a fix for that.  */
20145
20146 static void
20147 fix_new_arm (fragS *       frag,
20148              int           where,
20149              short int     size,
20150              expressionS * exp,
20151              int           pc_rel,
20152              int           reloc)
20153 {
20154   fixS *           new_fix;
20155
20156   switch (exp->X_op)
20157     {
20158     case O_constant:
20159       if (pc_rel)
20160         {
20161           /* Create an absolute valued symbol, so we have something to
20162              refer to in the object file.  Unfortunately for us, gas's
20163              generic expression parsing will already have folded out
20164              any use of .set foo/.type foo %function that may have
20165              been used to set type information of the target location,
20166              that's being specified symbolically.  We have to presume
20167              the user knows what they are doing.  */
20168           char name[16 + 8];
20169           symbolS *symbol;
20170
20171           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
20172
20173           symbol = symbol_find_or_make (name);
20174           S_SET_SEGMENT (symbol, absolute_section);
20175           symbol_set_frag (symbol, &zero_address_frag);
20176           S_SET_VALUE (symbol, exp->X_add_number);
20177           exp->X_op = O_symbol;
20178           exp->X_add_symbol = symbol;
20179           exp->X_add_number = 0;
20180         }
20181       /* FALLTHROUGH */
20182     case O_symbol:
20183     case O_add:
20184     case O_subtract:
20185       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
20186                              (enum bfd_reloc_code_real) reloc);
20187       break;
20188
20189     default:
20190       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
20191                                   pc_rel, (enum bfd_reloc_code_real) reloc);
20192       break;
20193     }
20194
20195   /* Mark whether the fix is to a THUMB instruction, or an ARM
20196      instruction.  */
20197   new_fix->tc_fix_data = thumb_mode;
20198 }
20199
20200 /* Create a frg for an instruction requiring relaxation.  */
20201 static void
20202 output_relax_insn (void)
20203 {
20204   char * to;
20205   symbolS *sym;
20206   int offset;
20207
20208   /* The size of the instruction is unknown, so tie the debug info to the
20209      start of the instruction.  */
20210   dwarf2_emit_insn (0);
20211
20212   switch (inst.relocs[0].exp.X_op)
20213     {
20214     case O_symbol:
20215       sym = inst.relocs[0].exp.X_add_symbol;
20216       offset = inst.relocs[0].exp.X_add_number;
20217       break;
20218     case O_constant:
20219       sym = NULL;
20220       offset = inst.relocs[0].exp.X_add_number;
20221       break;
20222     default:
20223       sym = make_expr_symbol (&inst.relocs[0].exp);
20224       offset = 0;
20225       break;
20226   }
20227   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
20228                  inst.relax, sym, offset, NULL/*offset, opcode*/);
20229   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
20230 }
20231
20232 /* Write a 32-bit thumb instruction to buf.  */
20233 static void
20234 put_thumb32_insn (char * buf, unsigned long insn)
20235 {
20236   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
20237   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
20238 }
20239
20240 static void
20241 output_inst (const char * str)
20242 {
20243   char * to = NULL;
20244
20245   if (inst.error)
20246     {
20247       as_bad ("%s -- `%s'", inst.error, str);
20248       return;
20249     }
20250   if (inst.relax)
20251     {
20252       output_relax_insn ();
20253       return;
20254     }
20255   if (inst.size == 0)
20256     return;
20257
20258   to = frag_more (inst.size);
20259   /* PR 9814: Record the thumb mode into the current frag so that we know
20260      what type of NOP padding to use, if necessary.  We override any previous
20261      setting so that if the mode has changed then the NOPS that we use will
20262      match the encoding of the last instruction in the frag.  */
20263   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
20264
20265   if (thumb_mode && (inst.size > THUMB_SIZE))
20266     {
20267       gas_assert (inst.size == (2 * THUMB_SIZE));
20268       put_thumb32_insn (to, inst.instruction);
20269     }
20270   else if (inst.size > INSN_SIZE)
20271     {
20272       gas_assert (inst.size == (2 * INSN_SIZE));
20273       md_number_to_chars (to, inst.instruction, INSN_SIZE);
20274       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
20275     }
20276   else
20277     md_number_to_chars (to, inst.instruction, inst.size);
20278
20279   int r;
20280   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
20281     {
20282       if (inst.relocs[r].type != BFD_RELOC_UNUSED)
20283         fix_new_arm (frag_now, to - frag_now->fr_literal,
20284                      inst.size, & inst.relocs[r].exp, inst.relocs[r].pc_rel,
20285                      inst.relocs[r].type);
20286     }
20287
20288   dwarf2_emit_insn (inst.size);
20289 }
20290
20291 static char *
20292 output_it_inst (int cond, int mask, char * to)
20293 {
20294   unsigned long instruction = 0xbf00;
20295
20296   mask &= 0xf;
20297   instruction |= mask;
20298   instruction |= cond << 4;
20299
20300   if (to == NULL)
20301     {
20302       to = frag_more (2);
20303 #ifdef OBJ_ELF
20304       dwarf2_emit_insn (2);
20305 #endif
20306     }
20307
20308   md_number_to_chars (to, instruction, 2);
20309
20310   return to;
20311 }
20312
20313 /* Tag values used in struct asm_opcode's tag field.  */
20314 enum opcode_tag
20315 {
20316   OT_unconditional,     /* Instruction cannot be conditionalized.
20317                            The ARM condition field is still 0xE.  */
20318   OT_unconditionalF,    /* Instruction cannot be conditionalized
20319                            and carries 0xF in its ARM condition field.  */
20320   OT_csuffix,           /* Instruction takes a conditional suffix.  */
20321   OT_csuffixF,          /* Some forms of the instruction take a scalar
20322                            conditional suffix, others place 0xF where the
20323                            condition field would be, others take a vector
20324                            conditional suffix.  */
20325   OT_cinfix3,           /* Instruction takes a conditional infix,
20326                            beginning at character index 3.  (In
20327                            unified mode, it becomes a suffix.)  */
20328   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
20329                             tsts, cmps, cmns, and teqs. */
20330   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
20331                            character index 3, even in unified mode.  Used for
20332                            legacy instructions where suffix and infix forms
20333                            may be ambiguous.  */
20334   OT_csuf_or_in3,       /* Instruction takes either a conditional
20335                            suffix or an infix at character index 3.  */
20336   OT_odd_infix_unc,     /* This is the unconditional variant of an
20337                            instruction that takes a conditional infix
20338                            at an unusual position.  In unified mode,
20339                            this variant will accept a suffix.  */
20340   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
20341                            are the conditional variants of instructions that
20342                            take conditional infixes in unusual positions.
20343                            The infix appears at character index
20344                            (tag - OT_odd_infix_0).  These are not accepted
20345                            in unified mode.  */
20346 };
20347
20348 /* Subroutine of md_assemble, responsible for looking up the primary
20349    opcode from the mnemonic the user wrote.  STR points to the
20350    beginning of the mnemonic.
20351
20352    This is not simply a hash table lookup, because of conditional
20353    variants.  Most instructions have conditional variants, which are
20354    expressed with a _conditional affix_ to the mnemonic.  If we were
20355    to encode each conditional variant as a literal string in the opcode
20356    table, it would have approximately 20,000 entries.
20357
20358    Most mnemonics take this affix as a suffix, and in unified syntax,
20359    'most' is upgraded to 'all'.  However, in the divided syntax, some
20360    instructions take the affix as an infix, notably the s-variants of
20361    the arithmetic instructions.  Of those instructions, all but six
20362    have the infix appear after the third character of the mnemonic.
20363
20364    Accordingly, the algorithm for looking up primary opcodes given
20365    an identifier is:
20366
20367    1. Look up the identifier in the opcode table.
20368       If we find a match, go to step U.
20369
20370    2. Look up the last two characters of the identifier in the
20371       conditions table.  If we find a match, look up the first N-2
20372       characters of the identifier in the opcode table.  If we
20373       find a match, go to step CE.
20374
20375    3. Look up the fourth and fifth characters of the identifier in
20376       the conditions table.  If we find a match, extract those
20377       characters from the identifier, and look up the remaining
20378       characters in the opcode table.  If we find a match, go
20379       to step CM.
20380
20381    4. Fail.
20382
20383    U. Examine the tag field of the opcode structure, in case this is
20384       one of the six instructions with its conditional infix in an
20385       unusual place.  If it is, the tag tells us where to find the
20386       infix; look it up in the conditions table and set inst.cond
20387       accordingly.  Otherwise, this is an unconditional instruction.
20388       Again set inst.cond accordingly.  Return the opcode structure.
20389
20390   CE. Examine the tag field to make sure this is an instruction that
20391       should receive a conditional suffix.  If it is not, fail.
20392       Otherwise, set inst.cond from the suffix we already looked up,
20393       and return the opcode structure.
20394
20395   CM. Examine the tag field to make sure this is an instruction that
20396       should receive a conditional infix after the third character.
20397       If it is not, fail.  Otherwise, undo the edits to the current
20398       line of input and proceed as for case CE.  */
20399
20400 static const struct asm_opcode *
20401 opcode_lookup (char **str)
20402 {
20403   char *end, *base;
20404   char *affix;
20405   const struct asm_opcode *opcode;
20406   const struct asm_cond *cond;
20407   char save[2];
20408
20409   /* Scan up to the end of the mnemonic, which must end in white space,
20410      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
20411   for (base = end = *str; *end != '\0'; end++)
20412     if (*end == ' ' || *end == '.')
20413       break;
20414
20415   if (end == base)
20416     return NULL;
20417
20418   /* Handle a possible width suffix and/or Neon type suffix.  */
20419   if (end[0] == '.')
20420     {
20421       int offset = 2;
20422
20423       /* The .w and .n suffixes are only valid if the unified syntax is in
20424          use.  */
20425       if (unified_syntax && end[1] == 'w')
20426         inst.size_req = 4;
20427       else if (unified_syntax && end[1] == 'n')
20428         inst.size_req = 2;
20429       else
20430         offset = 0;
20431
20432       inst.vectype.elems = 0;
20433
20434       *str = end + offset;
20435
20436       if (end[offset] == '.')
20437         {
20438           /* See if we have a Neon type suffix (possible in either unified or
20439              non-unified ARM syntax mode).  */
20440           if (parse_neon_type (&inst.vectype, str) == FAIL)
20441             return NULL;
20442         }
20443       else if (end[offset] != '\0' && end[offset] != ' ')
20444         return NULL;
20445     }
20446   else
20447     *str = end;
20448
20449   /* Look for unaffixed or special-case affixed mnemonic.  */
20450   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
20451                                                     end - base);
20452   if (opcode)
20453     {
20454       /* step U */
20455       if (opcode->tag < OT_odd_infix_0)
20456         {
20457           inst.cond = COND_ALWAYS;
20458           return opcode;
20459         }
20460
20461       if (warn_on_deprecated && unified_syntax)
20462         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
20463       affix = base + (opcode->tag - OT_odd_infix_0);
20464       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
20465       gas_assert (cond);
20466
20467       inst.cond = cond->value;
20468       return opcode;
20469     }
20470  if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
20471    {
20472     /* Cannot have a conditional suffix on a mnemonic of less than a character.
20473      */
20474     if (end - base < 2)
20475       return NULL;
20476      affix = end - 1;
20477      cond = (const struct asm_cond *) hash_find_n (arm_vcond_hsh, affix, 1);
20478      opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
20479                                                       affix - base);
20480      /* If this opcode can not be vector predicated then don't accept it with a
20481         vector predication code.  */
20482      if (opcode && !opcode->mayBeVecPred)
20483        opcode = NULL;
20484    }
20485   if (!opcode || !cond)
20486     {
20487       /* Cannot have a conditional suffix on a mnemonic of less than two
20488          characters.  */
20489       if (end - base < 3)
20490         return NULL;
20491
20492       /* Look for suffixed mnemonic.  */
20493       affix = end - 2;
20494       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
20495       opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
20496                                                         affix - base);
20497     }
20498
20499   if (opcode && cond)
20500     {
20501       /* step CE */
20502       switch (opcode->tag)
20503         {
20504         case OT_cinfix3_legacy:
20505           /* Ignore conditional suffixes matched on infix only mnemonics.  */
20506           break;
20507
20508         case OT_cinfix3:
20509         case OT_cinfix3_deprecated:
20510         case OT_odd_infix_unc:
20511           if (!unified_syntax)
20512             return NULL;
20513           /* Fall through.  */
20514
20515         case OT_csuffix:
20516         case OT_csuffixF:
20517         case OT_csuf_or_in3:
20518           inst.cond = cond->value;
20519           return opcode;
20520
20521         case OT_unconditional:
20522         case OT_unconditionalF:
20523           if (thumb_mode)
20524             inst.cond = cond->value;
20525           else
20526             {
20527               /* Delayed diagnostic.  */
20528               inst.error = BAD_COND;
20529               inst.cond = COND_ALWAYS;
20530             }
20531           return opcode;
20532
20533         default:
20534           return NULL;
20535         }
20536     }
20537
20538   /* Cannot have a usual-position infix on a mnemonic of less than
20539      six characters (five would be a suffix).  */
20540   if (end - base < 6)
20541     return NULL;
20542
20543   /* Look for infixed mnemonic in the usual position.  */
20544   affix = base + 3;
20545   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
20546   if (!cond)
20547     return NULL;
20548
20549   memcpy (save, affix, 2);
20550   memmove (affix, affix + 2, (end - affix) - 2);
20551   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
20552                                                     (end - base) - 2);
20553   memmove (affix + 2, affix, (end - affix) - 2);
20554   memcpy (affix, save, 2);
20555
20556   if (opcode
20557       && (opcode->tag == OT_cinfix3
20558           || opcode->tag == OT_cinfix3_deprecated
20559           || opcode->tag == OT_csuf_or_in3
20560           || opcode->tag == OT_cinfix3_legacy))
20561     {
20562       /* Step CM.  */
20563       if (warn_on_deprecated && unified_syntax
20564           && (opcode->tag == OT_cinfix3
20565               || opcode->tag == OT_cinfix3_deprecated))
20566         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
20567
20568       inst.cond = cond->value;
20569       return opcode;
20570     }
20571
20572   return NULL;
20573 }
20574
20575 /* This function generates an initial IT instruction, leaving its block
20576    virtually open for the new instructions. Eventually,
20577    the mask will be updated by now_pred_add_mask () each time
20578    a new instruction needs to be included in the IT block.
20579    Finally, the block is closed with close_automatic_it_block ().
20580    The block closure can be requested either from md_assemble (),
20581    a tencode (), or due to a label hook.  */
20582
20583 static void
20584 new_automatic_it_block (int cond)
20585 {
20586   now_pred.state = AUTOMATIC_PRED_BLOCK;
20587   now_pred.mask = 0x18;
20588   now_pred.cc = cond;
20589   now_pred.block_length = 1;
20590   mapping_state (MAP_THUMB);
20591   now_pred.insn = output_it_inst (cond, now_pred.mask, NULL);
20592   now_pred.warn_deprecated = FALSE;
20593   now_pred.insn_cond = TRUE;
20594 }
20595
20596 /* Close an automatic IT block.
20597    See comments in new_automatic_it_block ().  */
20598
20599 static void
20600 close_automatic_it_block (void)
20601 {
20602   now_pred.mask = 0x10;
20603   now_pred.block_length = 0;
20604 }
20605
20606 /* Update the mask of the current automatically-generated IT
20607    instruction. See comments in new_automatic_it_block ().  */
20608
20609 static void
20610 now_pred_add_mask (int cond)
20611 {
20612 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
20613 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
20614                                               | ((bitvalue) << (nbit)))
20615   const int resulting_bit = (cond & 1);
20616
20617   now_pred.mask &= 0xf;
20618   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
20619                                    resulting_bit,
20620                                   (5 - now_pred.block_length));
20621   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
20622                                    1,
20623                                    ((5 - now_pred.block_length) - 1));
20624   output_it_inst (now_pred.cc, now_pred.mask, now_pred.insn);
20625
20626 #undef CLEAR_BIT
20627 #undef SET_BIT_VALUE
20628 }
20629
20630 /* The IT blocks handling machinery is accessed through the these functions:
20631      it_fsm_pre_encode ()               from md_assemble ()
20632      set_pred_insn_type ()              optional, from the tencode functions
20633      set_pred_insn_type_last ()         ditto
20634      in_pred_block ()                   ditto
20635      it_fsm_post_encode ()              from md_assemble ()
20636      force_automatic_it_block_close ()  from label handling functions
20637
20638    Rationale:
20639      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
20640         initializing the IT insn type with a generic initial value depending
20641         on the inst.condition.
20642      2) During the tencode function, two things may happen:
20643         a) The tencode function overrides the IT insn type by
20644            calling either set_pred_insn_type (type) or
20645            set_pred_insn_type_last ().
20646         b) The tencode function queries the IT block state by
20647            calling in_pred_block () (i.e. to determine narrow/not narrow mode).
20648
20649         Both set_pred_insn_type and in_pred_block run the internal FSM state
20650         handling function (handle_pred_state), because: a) setting the IT insn
20651         type may incur in an invalid state (exiting the function),
20652         and b) querying the state requires the FSM to be updated.
20653         Specifically we want to avoid creating an IT block for conditional
20654         branches, so it_fsm_pre_encode is actually a guess and we can't
20655         determine whether an IT block is required until the tencode () routine
20656         has decided what type of instruction this actually it.
20657         Because of this, if set_pred_insn_type and in_pred_block have to be
20658         used, set_pred_insn_type has to be called first.
20659
20660         set_pred_insn_type_last () is a wrapper of set_pred_insn_type (type),
20661         that determines the insn IT type depending on the inst.cond code.
20662         When a tencode () routine encodes an instruction that can be
20663         either outside an IT block, or, in the case of being inside, has to be
20664         the last one, set_pred_insn_type_last () will determine the proper
20665         IT instruction type based on the inst.cond code. Otherwise,
20666         set_pred_insn_type can be called for overriding that logic or
20667         for covering other cases.
20668
20669         Calling handle_pred_state () may not transition the IT block state to
20670         OUTSIDE_PRED_BLOCK immediately, since the (current) state could be
20671         still queried. Instead, if the FSM determines that the state should
20672         be transitioned to OUTSIDE_PRED_BLOCK, a flag is marked to be closed
20673         after the tencode () function: that's what it_fsm_post_encode () does.
20674
20675         Since in_pred_block () calls the state handling function to get an
20676         updated state, an error may occur (due to invalid insns combination).
20677         In that case, inst.error is set.
20678         Therefore, inst.error has to be checked after the execution of
20679         the tencode () routine.
20680
20681      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
20682         any pending state change (if any) that didn't take place in
20683         handle_pred_state () as explained above.  */
20684
20685 static void
20686 it_fsm_pre_encode (void)
20687 {
20688   if (inst.cond != COND_ALWAYS)
20689     inst.pred_insn_type =  INSIDE_IT_INSN;
20690   else
20691     inst.pred_insn_type = OUTSIDE_PRED_INSN;
20692
20693   now_pred.state_handled = 0;
20694 }
20695
20696 /* IT state FSM handling function.  */
20697 /* MVE instructions and non-MVE instructions are handled differently because of
20698    the introduction of VPT blocks.
20699    Specifications say that any non-MVE instruction inside a VPT block is
20700    UNPREDICTABLE, with the exception of the BKPT instruction.  Whereas most MVE
20701    instructions are deemed to be UNPREDICTABLE if inside an IT block.  For the
20702    few exceptions we have MVE_UNPREDICABLE_INSN.
20703    The error messages provided depending on the different combinations possible
20704    are described in the cases below:
20705    For 'most' MVE instructions:
20706    1) In an IT block, with an IT code: syntax error
20707    2) In an IT block, with a VPT code: error: must be in a VPT block
20708    3) In an IT block, with no code: warning: UNPREDICTABLE
20709    4) In a VPT block, with an IT code: syntax error
20710    5) In a VPT block, with a VPT code: OK!
20711    6) In a VPT block, with no code: error: missing code
20712    7) Outside a pred block, with an IT code: error: syntax error
20713    8) Outside a pred block, with a VPT code: error: should be in a VPT block
20714    9) Outside a pred block, with no code: OK!
20715    For non-MVE instructions:
20716    10) In an IT block, with an IT code: OK!
20717    11) In an IT block, with a VPT code: syntax error
20718    12) In an IT block, with no code: error: missing code
20719    13) In a VPT block, with an IT code: error: should be in an IT block
20720    14) In a VPT block, with a VPT code: syntax error
20721    15) In a VPT block, with no code: UNPREDICTABLE
20722    16) Outside a pred block, with an IT code: error: should be in an IT block
20723    17) Outside a pred block, with a VPT code: syntax error
20724    18) Outside a pred block, with no code: OK!
20725  */
20726
20727
20728 static int
20729 handle_pred_state (void)
20730 {
20731   now_pred.state_handled = 1;
20732   now_pred.insn_cond = FALSE;
20733
20734   switch (now_pred.state)
20735     {
20736     case OUTSIDE_PRED_BLOCK:
20737       switch (inst.pred_insn_type)
20738         {
20739         case MVE_UNPREDICABLE_INSN:
20740         case MVE_OUTSIDE_PRED_INSN:
20741           if (inst.cond < COND_ALWAYS)
20742             {
20743               /* Case 7: Outside a pred block, with an IT code: error: syntax
20744                  error.  */
20745               inst.error = BAD_SYNTAX;
20746               return FAIL;
20747             }
20748           /* Case 9:  Outside a pred block, with no code: OK!  */
20749           break;
20750         case OUTSIDE_PRED_INSN:
20751           if (inst.cond > COND_ALWAYS)
20752             {
20753               /* Case 17:  Outside a pred block, with a VPT code: syntax error.
20754                */
20755               inst.error = BAD_SYNTAX;
20756               return FAIL;
20757             }
20758           /* Case 18: Outside a pred block, with no code: OK!  */
20759           break;
20760
20761         case INSIDE_VPT_INSN:
20762           /* Case 8: Outside a pred block, with a VPT code: error: should be in
20763              a VPT block.  */
20764           inst.error = BAD_OUT_VPT;
20765           return FAIL;
20766
20767         case INSIDE_IT_INSN:
20768         case INSIDE_IT_LAST_INSN:
20769           if (inst.cond < COND_ALWAYS)
20770             {
20771               /* Case 16: Outside a pred block, with an IT code: error: should
20772                  be in an IT block.  */
20773               if (thumb_mode == 0)
20774                 {
20775                   if (unified_syntax
20776                       && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
20777                     as_tsktsk (_("Warning: conditional outside an IT block"\
20778                                  " for Thumb."));
20779                 }
20780               else
20781                 {
20782                   if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
20783                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
20784                     {
20785                       /* Automatically generate the IT instruction.  */
20786                       new_automatic_it_block (inst.cond);
20787                       if (inst.pred_insn_type == INSIDE_IT_LAST_INSN)
20788                         close_automatic_it_block ();
20789                     }
20790                   else
20791                     {
20792                       inst.error = BAD_OUT_IT;
20793                       return FAIL;
20794                     }
20795                 }
20796               break;
20797             }
20798           else if (inst.cond > COND_ALWAYS)
20799             {
20800               /* Case 17: Outside a pred block, with a VPT code: syntax error.
20801                */
20802               inst.error = BAD_SYNTAX;
20803               return FAIL;
20804             }
20805           else
20806             gas_assert (0);
20807         case IF_INSIDE_IT_LAST_INSN:
20808         case NEUTRAL_IT_INSN:
20809           break;
20810
20811         case VPT_INSN:
20812           if (inst.cond != COND_ALWAYS)
20813             first_error (BAD_SYNTAX);
20814           now_pred.state = MANUAL_PRED_BLOCK;
20815           now_pred.block_length = 0;
20816           now_pred.type = VECTOR_PRED;
20817           now_pred.cc = 0;
20818           break;
20819         case IT_INSN:
20820           now_pred.state = MANUAL_PRED_BLOCK;
20821           now_pred.block_length = 0;
20822           now_pred.type = SCALAR_PRED;
20823           break;
20824         }
20825       break;
20826
20827     case AUTOMATIC_PRED_BLOCK:
20828       /* Three things may happen now:
20829          a) We should increment current it block size;
20830          b) We should close current it block (closing insn or 4 insns);
20831          c) We should close current it block and start a new one (due
20832          to incompatible conditions or
20833          4 insns-length block reached).  */
20834
20835       switch (inst.pred_insn_type)
20836         {
20837         case INSIDE_VPT_INSN:
20838         case VPT_INSN:
20839         case MVE_UNPREDICABLE_INSN:
20840         case MVE_OUTSIDE_PRED_INSN:
20841           gas_assert (0);
20842         case OUTSIDE_PRED_INSN:
20843           /* The closure of the block shall happen immediately,
20844              so any in_pred_block () call reports the block as closed.  */
20845           force_automatic_it_block_close ();
20846           break;
20847
20848         case INSIDE_IT_INSN:
20849         case INSIDE_IT_LAST_INSN:
20850         case IF_INSIDE_IT_LAST_INSN:
20851           now_pred.block_length++;
20852
20853           if (now_pred.block_length > 4
20854               || !now_pred_compatible (inst.cond))
20855             {
20856               force_automatic_it_block_close ();
20857               if (inst.pred_insn_type != IF_INSIDE_IT_LAST_INSN)
20858                 new_automatic_it_block (inst.cond);
20859             }
20860           else
20861             {
20862               now_pred.insn_cond = TRUE;
20863               now_pred_add_mask (inst.cond);
20864             }
20865
20866           if (now_pred.state == AUTOMATIC_PRED_BLOCK
20867               && (inst.pred_insn_type == INSIDE_IT_LAST_INSN
20868                   || inst.pred_insn_type == IF_INSIDE_IT_LAST_INSN))
20869             close_automatic_it_block ();
20870           break;
20871
20872         case NEUTRAL_IT_INSN:
20873           now_pred.block_length++;
20874           now_pred.insn_cond = TRUE;
20875
20876           if (now_pred.block_length > 4)
20877             force_automatic_it_block_close ();
20878           else
20879             now_pred_add_mask (now_pred.cc & 1);
20880           break;
20881
20882         case IT_INSN:
20883           close_automatic_it_block ();
20884           now_pred.state = MANUAL_PRED_BLOCK;
20885           break;
20886         }
20887       break;
20888
20889     case MANUAL_PRED_BLOCK:
20890       {
20891         int cond, is_last;
20892         if (now_pred.type == SCALAR_PRED)
20893           {
20894             /* Check conditional suffixes.  */
20895             cond = now_pred.cc ^ ((now_pred.mask >> 4) & 1) ^ 1;
20896             now_pred.mask <<= 1;
20897             now_pred.mask &= 0x1f;
20898             is_last = (now_pred.mask == 0x10);
20899           }
20900         else
20901           {
20902             now_pred.cc ^= (now_pred.mask >> 4);
20903             cond = now_pred.cc + 0xf;
20904             now_pred.mask <<= 1;
20905             now_pred.mask &= 0x1f;
20906             is_last = now_pred.mask == 0x10;
20907           }
20908         now_pred.insn_cond = TRUE;
20909
20910         switch (inst.pred_insn_type)
20911           {
20912           case OUTSIDE_PRED_INSN:
20913             if (now_pred.type == SCALAR_PRED)
20914               {
20915                 if (inst.cond == COND_ALWAYS)
20916                   {
20917                     /* Case 12: In an IT block, with no code: error: missing
20918                        code.  */
20919                     inst.error = BAD_NOT_IT;
20920                     return FAIL;
20921                   }
20922                 else if (inst.cond > COND_ALWAYS)
20923                   {
20924                     /* Case 11: In an IT block, with a VPT code: syntax error.
20925                      */
20926                     inst.error = BAD_SYNTAX;
20927                     return FAIL;
20928                   }
20929                 else if (thumb_mode)
20930                   {
20931                     /* This is for some special cases where a non-MVE
20932                        instruction is not allowed in an IT block, such as cbz,
20933                        but are put into one with a condition code.
20934                        You could argue this should be a syntax error, but we
20935                        gave the 'not allowed in IT block' diagnostic in the
20936                        past so we will keep doing so.  */
20937                     inst.error = BAD_NOT_IT;
20938                     return FAIL;
20939                   }
20940                 break;
20941               }
20942             else
20943               {
20944                 /* Case 15: In a VPT block, with no code: UNPREDICTABLE.  */
20945                 as_tsktsk (MVE_NOT_VPT);
20946                 return SUCCESS;
20947               }
20948           case MVE_OUTSIDE_PRED_INSN:
20949             if (now_pred.type == SCALAR_PRED)
20950               {
20951                 if (inst.cond == COND_ALWAYS)
20952                   {
20953                     /* Case 3: In an IT block, with no code: warning:
20954                        UNPREDICTABLE.  */
20955                     as_tsktsk (MVE_NOT_IT);
20956                     return SUCCESS;
20957                   }
20958                 else if (inst.cond < COND_ALWAYS)
20959                   {
20960                     /* Case 1: In an IT block, with an IT code: syntax error.
20961                      */
20962                     inst.error = BAD_SYNTAX;
20963                     return FAIL;
20964                   }
20965                 else
20966                   gas_assert (0);
20967               }
20968             else
20969               {
20970                 if (inst.cond < COND_ALWAYS)
20971                   {
20972                     /* Case 4: In a VPT block, with an IT code: syntax error.
20973                      */
20974                     inst.error = BAD_SYNTAX;
20975                     return FAIL;
20976                   }
20977                 else if (inst.cond == COND_ALWAYS)
20978                   {
20979                     /* Case 6: In a VPT block, with no code: error: missing
20980                        code.  */
20981                     inst.error = BAD_NOT_VPT;
20982                     return FAIL;
20983                   }
20984                 else
20985                   {
20986                     gas_assert (0);
20987                   }
20988               }
20989           case MVE_UNPREDICABLE_INSN:
20990             as_tsktsk (now_pred.type == SCALAR_PRED ? MVE_NOT_IT : MVE_NOT_VPT);
20991             return SUCCESS;
20992           case INSIDE_IT_INSN:
20993             if (inst.cond > COND_ALWAYS)
20994               {
20995                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
20996                 /* Case 14: In a VPT block, with a VPT code: syntax error.  */
20997                 inst.error = BAD_SYNTAX;
20998                 return FAIL;
20999               }
21000             else if (now_pred.type == SCALAR_PRED)
21001               {
21002                 /* Case 10: In an IT block, with an IT code: OK!  */
21003                 if (cond != inst.cond)
21004                   {
21005                     inst.error = now_pred.type == SCALAR_PRED ? BAD_IT_COND :
21006                       BAD_VPT_COND;
21007                     return FAIL;
21008                   }
21009               }
21010             else
21011               {
21012                 /* Case 13: In a VPT block, with an IT code: error: should be
21013                    in an IT block.  */
21014                 inst.error = BAD_OUT_IT;
21015                 return FAIL;
21016               }
21017             break;
21018
21019           case INSIDE_VPT_INSN:
21020             if (now_pred.type == SCALAR_PRED)
21021               {
21022                 /* Case 2: In an IT block, with a VPT code: error: must be in a
21023                    VPT block.  */
21024                 inst.error = BAD_OUT_VPT;
21025                 return FAIL;
21026               }
21027             /* Case 5:  In a VPT block, with a VPT code: OK!  */
21028             else if (cond != inst.cond)
21029               {
21030                 inst.error = BAD_VPT_COND;
21031                 return FAIL;
21032               }
21033             break;
21034           case INSIDE_IT_LAST_INSN:
21035           case IF_INSIDE_IT_LAST_INSN:
21036             if (now_pred.type == VECTOR_PRED || inst.cond > COND_ALWAYS)
21037               {
21038                 /* Case 4: In a VPT block, with an IT code: syntax error.  */
21039                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
21040                 inst.error = BAD_SYNTAX;
21041                 return FAIL;
21042               }
21043             else if (cond != inst.cond)
21044               {
21045                 inst.error = BAD_IT_COND;
21046                 return FAIL;
21047               }
21048             if (!is_last)
21049               {
21050                 inst.error = BAD_BRANCH;
21051                 return FAIL;
21052               }
21053             break;
21054
21055           case NEUTRAL_IT_INSN:
21056             /* The BKPT instruction is unconditional even in a IT or VPT
21057                block.  */
21058             break;
21059
21060           case IT_INSN:
21061             if (now_pred.type == SCALAR_PRED)
21062               {
21063                 inst.error = BAD_IT_IT;
21064                 return FAIL;
21065               }
21066             /* fall through.  */
21067           case VPT_INSN:
21068             if (inst.cond == COND_ALWAYS)
21069               {
21070                 /* Executing a VPT/VPST instruction inside an IT block or a
21071                    VPT/VPST/IT instruction inside a VPT block is UNPREDICTABLE.
21072                  */
21073                 if (now_pred.type == SCALAR_PRED)
21074                   as_tsktsk (MVE_NOT_IT);
21075                 else
21076                   as_tsktsk (MVE_NOT_VPT);
21077                 return SUCCESS;
21078               }
21079             else
21080               {
21081                 /* VPT/VPST do not accept condition codes.  */
21082                 inst.error = BAD_SYNTAX;
21083                 return FAIL;
21084               }
21085           }
21086         }
21087       break;
21088     }
21089
21090   return SUCCESS;
21091 }
21092
21093 struct depr_insn_mask
21094 {
21095   unsigned long pattern;
21096   unsigned long mask;
21097   const char* description;
21098 };
21099
21100 /* List of 16-bit instruction patterns deprecated in an IT block in
21101    ARMv8.  */
21102 static const struct depr_insn_mask depr_it_insns[] = {
21103   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
21104   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
21105   { 0xa000, 0xb800, N_("ADR") },
21106   { 0x4800, 0xf800, N_("Literal loads") },
21107   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
21108   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
21109   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
21110      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
21111   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
21112   { 0, 0, NULL }
21113 };
21114
21115 static void
21116 it_fsm_post_encode (void)
21117 {
21118   int is_last;
21119
21120   if (!now_pred.state_handled)
21121     handle_pred_state ();
21122
21123   if (now_pred.insn_cond
21124       && !now_pred.warn_deprecated
21125       && warn_on_deprecated
21126       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8)
21127       && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_m))
21128     {
21129       if (inst.instruction >= 0x10000)
21130         {
21131           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
21132                      "performance deprecated in ARMv8-A and ARMv8-R"));
21133           now_pred.warn_deprecated = TRUE;
21134         }
21135       else
21136         {
21137           const struct depr_insn_mask *p = depr_it_insns;
21138
21139           while (p->mask != 0)
21140             {
21141               if ((inst.instruction & p->mask) == p->pattern)
21142                 {
21143                   as_tsktsk (_("IT blocks containing 16-bit Thumb "
21144                                "instructions of the following class are "
21145                                "performance deprecated in ARMv8-A and "
21146                                "ARMv8-R: %s"), p->description);
21147                   now_pred.warn_deprecated = TRUE;
21148                   break;
21149                 }
21150
21151               ++p;
21152             }
21153         }
21154
21155       if (now_pred.block_length > 1)
21156         {
21157           as_tsktsk (_("IT blocks containing more than one conditional "
21158                      "instruction are performance deprecated in ARMv8-A and "
21159                      "ARMv8-R"));
21160           now_pred.warn_deprecated = TRUE;
21161         }
21162     }
21163
21164     is_last = (now_pred.mask == 0x10);
21165     if (is_last)
21166       {
21167         now_pred.state = OUTSIDE_PRED_BLOCK;
21168         now_pred.mask = 0;
21169       }
21170 }
21171
21172 static void
21173 force_automatic_it_block_close (void)
21174 {
21175   if (now_pred.state == AUTOMATIC_PRED_BLOCK)
21176     {
21177       close_automatic_it_block ();
21178       now_pred.state = OUTSIDE_PRED_BLOCK;
21179       now_pred.mask = 0;
21180     }
21181 }
21182
21183 static int
21184 in_pred_block (void)
21185 {
21186   if (!now_pred.state_handled)
21187     handle_pred_state ();
21188
21189   return now_pred.state != OUTSIDE_PRED_BLOCK;
21190 }
21191
21192 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
21193    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
21194    here, hence the "known" in the function name.  */
21195
21196 static bfd_boolean
21197 known_t32_only_insn (const struct asm_opcode *opcode)
21198 {
21199   /* Original Thumb-1 wide instruction.  */
21200   if (opcode->tencode == do_t_blx
21201       || opcode->tencode == do_t_branch23
21202       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
21203       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
21204     return TRUE;
21205
21206   /* Wide-only instruction added to ARMv8-M Baseline.  */
21207   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
21208       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
21209       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
21210       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
21211     return TRUE;
21212
21213   return FALSE;
21214 }
21215
21216 /* Whether wide instruction variant can be used if available for a valid OPCODE
21217    in ARCH.  */
21218
21219 static bfd_boolean
21220 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
21221 {
21222   if (known_t32_only_insn (opcode))
21223     return TRUE;
21224
21225   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
21226      of variant T3 of B.W is checked in do_t_branch.  */
21227   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
21228       && opcode->tencode == do_t_branch)
21229     return TRUE;
21230
21231   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
21232   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
21233       && opcode->tencode == do_t_mov_cmp
21234       /* Make sure CMP instruction is not affected.  */
21235       && opcode->aencode == do_mov)
21236     return TRUE;
21237
21238   /* Wide instruction variants of all instructions with narrow *and* wide
21239      variants become available with ARMv6t2.  Other opcodes are either
21240      narrow-only or wide-only and are thus available if OPCODE is valid.  */
21241   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
21242     return TRUE;
21243
21244   /* OPCODE with narrow only instruction variant or wide variant not
21245      available.  */
21246   return FALSE;
21247 }
21248
21249 void
21250 md_assemble (char *str)
21251 {
21252   char *p = str;
21253   const struct asm_opcode * opcode;
21254
21255   /* Align the previous label if needed.  */
21256   if (last_label_seen != NULL)
21257     {
21258       symbol_set_frag (last_label_seen, frag_now);
21259       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
21260       S_SET_SEGMENT (last_label_seen, now_seg);
21261     }
21262
21263   memset (&inst, '\0', sizeof (inst));
21264   int r;
21265   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
21266     inst.relocs[r].type = BFD_RELOC_UNUSED;
21267
21268   opcode = opcode_lookup (&p);
21269   if (!opcode)
21270     {
21271       /* It wasn't an instruction, but it might be a register alias of
21272          the form alias .req reg, or a Neon .dn/.qn directive.  */
21273       if (! create_register_alias (str, p)
21274           && ! create_neon_reg_alias (str, p))
21275         as_bad (_("bad instruction `%s'"), str);
21276
21277       return;
21278     }
21279
21280   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
21281     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
21282
21283   /* The value which unconditional instructions should have in place of the
21284      condition field.  */
21285   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
21286
21287   if (thumb_mode)
21288     {
21289       arm_feature_set variant;
21290
21291       variant = cpu_variant;
21292       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
21293       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
21294         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
21295       /* Check that this instruction is supported for this CPU.  */
21296       if (!opcode->tvariant
21297           || (thumb_mode == 1
21298               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
21299         {
21300           if (opcode->tencode == do_t_swi)
21301             as_bad (_("SVC is not permitted on this architecture"));
21302           else
21303             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
21304           return;
21305         }
21306       if (inst.cond != COND_ALWAYS && !unified_syntax
21307           && opcode->tencode != do_t_branch)
21308         {
21309           as_bad (_("Thumb does not support conditional execution"));
21310           return;
21311         }
21312
21313       /* Two things are addressed here:
21314          1) Implicit require narrow instructions on Thumb-1.
21315             This avoids relaxation accidentally introducing Thumb-2
21316             instructions.
21317          2) Reject wide instructions in non Thumb-2 cores.
21318
21319          Only instructions with narrow and wide variants need to be handled
21320          but selecting all non wide-only instructions is easier.  */
21321       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
21322           && !t32_insn_ok (variant, opcode))
21323         {
21324           if (inst.size_req == 0)
21325             inst.size_req = 2;
21326           else if (inst.size_req == 4)
21327             {
21328               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
21329                 as_bad (_("selected processor does not support 32bit wide "
21330                           "variant of instruction `%s'"), str);
21331               else
21332                 as_bad (_("selected processor does not support `%s' in "
21333                           "Thumb-2 mode"), str);
21334               return;
21335             }
21336         }
21337
21338       inst.instruction = opcode->tvalue;
21339
21340       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
21341         {
21342           /* Prepare the pred_insn_type for those encodings that don't set
21343              it.  */
21344           it_fsm_pre_encode ();
21345
21346           opcode->tencode ();
21347
21348           it_fsm_post_encode ();
21349         }
21350
21351       if (!(inst.error || inst.relax))
21352         {
21353           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
21354           inst.size = (inst.instruction > 0xffff ? 4 : 2);
21355           if (inst.size_req && inst.size_req != inst.size)
21356             {
21357               as_bad (_("cannot honor width suffix -- `%s'"), str);
21358               return;
21359             }
21360         }
21361
21362       /* Something has gone badly wrong if we try to relax a fixed size
21363          instruction.  */
21364       gas_assert (inst.size_req == 0 || !inst.relax);
21365
21366       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
21367                               *opcode->tvariant);
21368       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
21369          set those bits when Thumb-2 32-bit instructions are seen.  The impact
21370          of relaxable instructions will be considered later after we finish all
21371          relaxation.  */
21372       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
21373         variant = arm_arch_none;
21374       else
21375         variant = cpu_variant;
21376       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
21377         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
21378                                 arm_ext_v6t2);
21379
21380       check_neon_suffixes;
21381
21382       if (!inst.error)
21383         {
21384           mapping_state (MAP_THUMB);
21385         }
21386     }
21387   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
21388     {
21389       bfd_boolean is_bx;
21390
21391       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
21392       is_bx = (opcode->aencode == do_bx);
21393
21394       /* Check that this instruction is supported for this CPU.  */
21395       if (!(is_bx && fix_v4bx)
21396           && !(opcode->avariant &&
21397                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
21398         {
21399           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
21400           return;
21401         }
21402       if (inst.size_req)
21403         {
21404           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
21405           return;
21406         }
21407
21408       inst.instruction = opcode->avalue;
21409       if (opcode->tag == OT_unconditionalF)
21410         inst.instruction |= 0xFU << 28;
21411       else
21412         inst.instruction |= inst.cond << 28;
21413       inst.size = INSN_SIZE;
21414       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
21415         {
21416           it_fsm_pre_encode ();
21417           opcode->aencode ();
21418           it_fsm_post_encode ();
21419         }
21420       /* Arm mode bx is marked as both v4T and v5 because it's still required
21421          on a hypothetical non-thumb v5 core.  */
21422       if (is_bx)
21423         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
21424       else
21425         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
21426                                 *opcode->avariant);
21427
21428       check_neon_suffixes;
21429
21430       if (!inst.error)
21431         {
21432           mapping_state (MAP_ARM);
21433         }
21434     }
21435   else
21436     {
21437       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
21438                 "-- `%s'"), str);
21439       return;
21440     }
21441   output_inst (str);
21442 }
21443
21444 static void
21445 check_pred_blocks_finished (void)
21446 {
21447 #ifdef OBJ_ELF
21448   asection *sect;
21449
21450   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
21451     if (seg_info (sect)->tc_segment_info_data.current_pred.state
21452         == MANUAL_PRED_BLOCK)
21453       {
21454         if (now_pred.type == SCALAR_PRED)
21455           as_warn (_("section '%s' finished with an open IT block."),
21456                    sect->name);
21457         else
21458           as_warn (_("section '%s' finished with an open VPT/VPST block."),
21459                    sect->name);
21460       }
21461 #else
21462   if (now_pred.state == MANUAL_PRED_BLOCK)
21463     {
21464       if (now_pred.type == SCALAR_PRED)
21465        as_warn (_("file finished with an open IT block."));
21466       else
21467         as_warn (_("file finished with an open VPT/VPST block."));
21468     }
21469 #endif
21470 }
21471
21472 /* Various frobbings of labels and their addresses.  */
21473
21474 void
21475 arm_start_line_hook (void)
21476 {
21477   last_label_seen = NULL;
21478 }
21479
21480 void
21481 arm_frob_label (symbolS * sym)
21482 {
21483   last_label_seen = sym;
21484
21485   ARM_SET_THUMB (sym, thumb_mode);
21486
21487 #if defined OBJ_COFF || defined OBJ_ELF
21488   ARM_SET_INTERWORK (sym, support_interwork);
21489 #endif
21490
21491   force_automatic_it_block_close ();
21492
21493   /* Note - do not allow local symbols (.Lxxx) to be labelled
21494      as Thumb functions.  This is because these labels, whilst
21495      they exist inside Thumb code, are not the entry points for
21496      possible ARM->Thumb calls.  Also, these labels can be used
21497      as part of a computed goto or switch statement.  eg gcc
21498      can generate code that looks like this:
21499
21500                 ldr  r2, [pc, .Laaa]
21501                 lsl  r3, r3, #2
21502                 ldr  r2, [r3, r2]
21503                 mov  pc, r2
21504
21505        .Lbbb:  .word .Lxxx
21506        .Lccc:  .word .Lyyy
21507        ..etc...
21508        .Laaa:   .word Lbbb
21509
21510      The first instruction loads the address of the jump table.
21511      The second instruction converts a table index into a byte offset.
21512      The third instruction gets the jump address out of the table.
21513      The fourth instruction performs the jump.
21514
21515      If the address stored at .Laaa is that of a symbol which has the
21516      Thumb_Func bit set, then the linker will arrange for this address
21517      to have the bottom bit set, which in turn would mean that the
21518      address computation performed by the third instruction would end
21519      up with the bottom bit set.  Since the ARM is capable of unaligned
21520      word loads, the instruction would then load the incorrect address
21521      out of the jump table, and chaos would ensue.  */
21522   if (label_is_thumb_function_name
21523       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
21524       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
21525     {
21526       /* When the address of a Thumb function is taken the bottom
21527          bit of that address should be set.  This will allow
21528          interworking between Arm and Thumb functions to work
21529          correctly.  */
21530
21531       THUMB_SET_FUNC (sym, 1);
21532
21533       label_is_thumb_function_name = FALSE;
21534     }
21535
21536   dwarf2_emit_label (sym);
21537 }
21538
21539 bfd_boolean
21540 arm_data_in_code (void)
21541 {
21542   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
21543     {
21544       *input_line_pointer = '/';
21545       input_line_pointer += 5;
21546       *input_line_pointer = 0;
21547       return TRUE;
21548     }
21549
21550   return FALSE;
21551 }
21552
21553 char *
21554 arm_canonicalize_symbol_name (char * name)
21555 {
21556   int len;
21557
21558   if (thumb_mode && (len = strlen (name)) > 5
21559       && streq (name + len - 5, "/data"))
21560     *(name + len - 5) = 0;
21561
21562   return name;
21563 }
21564 \f
21565 /* Table of all register names defined by default.  The user can
21566    define additional names with .req.  Note that all register names
21567    should appear in both upper and lowercase variants.  Some registers
21568    also have mixed-case names.  */
21569
21570 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
21571 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
21572 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
21573 #define REGSET(p,t) \
21574   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
21575   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
21576   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
21577   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
21578 #define REGSETH(p,t) \
21579   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
21580   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
21581   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
21582   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
21583 #define REGSET2(p,t) \
21584   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
21585   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
21586   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
21587   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
21588 #define SPLRBANK(base,bank,t) \
21589   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
21590   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
21591   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
21592   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
21593   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
21594   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
21595
21596 static const struct reg_entry reg_names[] =
21597 {
21598   /* ARM integer registers.  */
21599   REGSET(r, RN), REGSET(R, RN),
21600
21601   /* ATPCS synonyms.  */
21602   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
21603   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
21604   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
21605
21606   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
21607   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
21608   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
21609
21610   /* Well-known aliases.  */
21611   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
21612   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
21613
21614   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
21615   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
21616
21617   /* Defining the new Zero register from ARMv8.1-M.  */
21618   REGDEF(zr,15,ZR),
21619   REGDEF(ZR,15,ZR),
21620
21621   /* Coprocessor numbers.  */
21622   REGSET(p, CP), REGSET(P, CP),
21623
21624   /* Coprocessor register numbers.  The "cr" variants are for backward
21625      compatibility.  */
21626   REGSET(c,  CN), REGSET(C, CN),
21627   REGSET(cr, CN), REGSET(CR, CN),
21628
21629   /* ARM banked registers.  */
21630   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
21631   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
21632   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
21633   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
21634   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
21635   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
21636   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
21637
21638   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
21639   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
21640   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
21641   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
21642   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
21643   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
21644   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
21645   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
21646
21647   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
21648   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
21649   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
21650   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
21651   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
21652   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
21653   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
21654   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
21655   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
21656
21657   /* FPA registers.  */
21658   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
21659   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
21660
21661   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
21662   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
21663
21664   /* VFP SP registers.  */
21665   REGSET(s,VFS),  REGSET(S,VFS),
21666   REGSETH(s,VFS), REGSETH(S,VFS),
21667
21668   /* VFP DP Registers.  */
21669   REGSET(d,VFD),  REGSET(D,VFD),
21670   /* Extra Neon DP registers.  */
21671   REGSETH(d,VFD), REGSETH(D,VFD),
21672
21673   /* Neon QP registers.  */
21674   REGSET2(q,NQ),  REGSET2(Q,NQ),
21675
21676   /* VFP control registers.  */
21677   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
21678   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
21679   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
21680   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
21681   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
21682   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
21683   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
21684
21685   /* Maverick DSP coprocessor registers.  */
21686   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
21687   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
21688
21689   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
21690   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
21691   REGDEF(dspsc,0,DSPSC),
21692
21693   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
21694   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
21695   REGDEF(DSPSC,0,DSPSC),
21696
21697   /* iWMMXt data registers - p0, c0-15.  */
21698   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
21699
21700   /* iWMMXt control registers - p1, c0-3.  */
21701   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
21702   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
21703   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
21704   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
21705
21706   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
21707   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
21708   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
21709   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
21710   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
21711
21712   /* XScale accumulator registers.  */
21713   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
21714 };
21715 #undef REGDEF
21716 #undef REGNUM
21717 #undef REGSET
21718
21719 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
21720    within psr_required_here.  */
21721 static const struct asm_psr psrs[] =
21722 {
21723   /* Backward compatibility notation.  Note that "all" is no longer
21724      truly all possible PSR bits.  */
21725   {"all",  PSR_c | PSR_f},
21726   {"flg",  PSR_f},
21727   {"ctl",  PSR_c},
21728
21729   /* Individual flags.  */
21730   {"f",    PSR_f},
21731   {"c",    PSR_c},
21732   {"x",    PSR_x},
21733   {"s",    PSR_s},
21734
21735   /* Combinations of flags.  */
21736   {"fs",   PSR_f | PSR_s},
21737   {"fx",   PSR_f | PSR_x},
21738   {"fc",   PSR_f | PSR_c},
21739   {"sf",   PSR_s | PSR_f},
21740   {"sx",   PSR_s | PSR_x},
21741   {"sc",   PSR_s | PSR_c},
21742   {"xf",   PSR_x | PSR_f},
21743   {"xs",   PSR_x | PSR_s},
21744   {"xc",   PSR_x | PSR_c},
21745   {"cf",   PSR_c | PSR_f},
21746   {"cs",   PSR_c | PSR_s},
21747   {"cx",   PSR_c | PSR_x},
21748   {"fsx",  PSR_f | PSR_s | PSR_x},
21749   {"fsc",  PSR_f | PSR_s | PSR_c},
21750   {"fxs",  PSR_f | PSR_x | PSR_s},
21751   {"fxc",  PSR_f | PSR_x | PSR_c},
21752   {"fcs",  PSR_f | PSR_c | PSR_s},
21753   {"fcx",  PSR_f | PSR_c | PSR_x},
21754   {"sfx",  PSR_s | PSR_f | PSR_x},
21755   {"sfc",  PSR_s | PSR_f | PSR_c},
21756   {"sxf",  PSR_s | PSR_x | PSR_f},
21757   {"sxc",  PSR_s | PSR_x | PSR_c},
21758   {"scf",  PSR_s | PSR_c | PSR_f},
21759   {"scx",  PSR_s | PSR_c | PSR_x},
21760   {"xfs",  PSR_x | PSR_f | PSR_s},
21761   {"xfc",  PSR_x | PSR_f | PSR_c},
21762   {"xsf",  PSR_x | PSR_s | PSR_f},
21763   {"xsc",  PSR_x | PSR_s | PSR_c},
21764   {"xcf",  PSR_x | PSR_c | PSR_f},
21765   {"xcs",  PSR_x | PSR_c | PSR_s},
21766   {"cfs",  PSR_c | PSR_f | PSR_s},
21767   {"cfx",  PSR_c | PSR_f | PSR_x},
21768   {"csf",  PSR_c | PSR_s | PSR_f},
21769   {"csx",  PSR_c | PSR_s | PSR_x},
21770   {"cxf",  PSR_c | PSR_x | PSR_f},
21771   {"cxs",  PSR_c | PSR_x | PSR_s},
21772   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
21773   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
21774   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
21775   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
21776   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
21777   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
21778   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
21779   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
21780   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
21781   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
21782   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
21783   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
21784   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
21785   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
21786   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
21787   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
21788   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
21789   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
21790   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
21791   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
21792   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
21793   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
21794   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
21795   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
21796 };
21797
21798 /* Table of V7M psr names.  */
21799 static const struct asm_psr v7m_psrs[] =
21800 {
21801   {"apsr",         0x0 }, {"APSR",         0x0 },
21802   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
21803   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
21804   {"psr",          0x3 }, {"PSR",          0x3 },
21805   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
21806   {"ipsr",         0x5 }, {"IPSR",         0x5 },
21807   {"epsr",         0x6 }, {"EPSR",         0x6 },
21808   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
21809   {"msp",          0x8 }, {"MSP",          0x8 },
21810   {"psp",          0x9 }, {"PSP",          0x9 },
21811   {"msplim",       0xa }, {"MSPLIM",       0xa },
21812   {"psplim",       0xb }, {"PSPLIM",       0xb },
21813   {"primask",      0x10}, {"PRIMASK",      0x10},
21814   {"basepri",      0x11}, {"BASEPRI",      0x11},
21815   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
21816   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
21817   {"control",      0x14}, {"CONTROL",      0x14},
21818   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
21819   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
21820   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
21821   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
21822   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
21823   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
21824   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
21825   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
21826   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
21827 };
21828
21829 /* Table of all shift-in-operand names.  */
21830 static const struct asm_shift_name shift_names [] =
21831 {
21832   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
21833   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
21834   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
21835   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
21836   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
21837   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX },
21838   { "uxtw", SHIFT_UXTW}, { "UXTW", SHIFT_UXTW}
21839 };
21840
21841 /* Table of all explicit relocation names.  */
21842 #ifdef OBJ_ELF
21843 static struct reloc_entry reloc_names[] =
21844 {
21845   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
21846   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
21847   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
21848   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
21849   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
21850   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
21851   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
21852   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
21853   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
21854   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
21855   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
21856   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
21857   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
21858         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
21859   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
21860         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
21861   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
21862         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ},
21863   { "gotfuncdesc", BFD_RELOC_ARM_GOTFUNCDESC },
21864         { "GOTFUNCDESC", BFD_RELOC_ARM_GOTFUNCDESC },
21865   { "gotofffuncdesc", BFD_RELOC_ARM_GOTOFFFUNCDESC },
21866         { "GOTOFFFUNCDESC", BFD_RELOC_ARM_GOTOFFFUNCDESC },
21867   { "funcdesc", BFD_RELOC_ARM_FUNCDESC },
21868         { "FUNCDESC", BFD_RELOC_ARM_FUNCDESC },
21869    { "tlsgd_fdpic", BFD_RELOC_ARM_TLS_GD32_FDPIC },      { "TLSGD_FDPIC", BFD_RELOC_ARM_TLS_GD32_FDPIC },
21870    { "tlsldm_fdpic", BFD_RELOC_ARM_TLS_LDM32_FDPIC },    { "TLSLDM_FDPIC", BFD_RELOC_ARM_TLS_LDM32_FDPIC },
21871    { "gottpoff_fdpic", BFD_RELOC_ARM_TLS_IE32_FDPIC },   { "GOTTPOFF_FDIC", BFD_RELOC_ARM_TLS_IE32_FDPIC },
21872 };
21873 #endif
21874
21875 /* Table of all conditional affixes.  */
21876 static const struct asm_cond conds[] =
21877 {
21878   {"eq", 0x0},
21879   {"ne", 0x1},
21880   {"cs", 0x2}, {"hs", 0x2},
21881   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
21882   {"mi", 0x4},
21883   {"pl", 0x5},
21884   {"vs", 0x6},
21885   {"vc", 0x7},
21886   {"hi", 0x8},
21887   {"ls", 0x9},
21888   {"ge", 0xa},
21889   {"lt", 0xb},
21890   {"gt", 0xc},
21891   {"le", 0xd},
21892   {"al", 0xe}
21893 };
21894 static const struct asm_cond vconds[] =
21895 {
21896     {"t", 0xf},
21897     {"e", 0x10}
21898 };
21899
21900 #define UL_BARRIER(L,U,CODE,FEAT) \
21901   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
21902   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
21903
21904 static struct asm_barrier_opt barrier_opt_names[] =
21905 {
21906   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
21907   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
21908   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
21909   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
21910   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
21911   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
21912   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
21913   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
21914   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
21915   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
21916   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
21917   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
21918   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
21919   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
21920   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
21921   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
21922 };
21923
21924 #undef UL_BARRIER
21925
21926 /* Table of ARM-format instructions.    */
21927
21928 /* Macros for gluing together operand strings.  N.B. In all cases
21929    other than OPS0, the trailing OP_stop comes from default
21930    zero-initialization of the unspecified elements of the array.  */
21931 #define OPS0()            { OP_stop, }
21932 #define OPS1(a)           { OP_##a, }
21933 #define OPS2(a,b)         { OP_##a,OP_##b, }
21934 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
21935 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
21936 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
21937 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
21938
21939 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
21940    This is useful when mixing operands for ARM and THUMB, i.e. using the
21941    MIX_ARM_THUMB_OPERANDS macro.
21942    In order to use these macros, prefix the number of operands with _
21943    e.g. _3.  */
21944 #define OPS_1(a)           { a, }
21945 #define OPS_2(a,b)         { a,b, }
21946 #define OPS_3(a,b,c)       { a,b,c, }
21947 #define OPS_4(a,b,c,d)     { a,b,c,d, }
21948 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
21949 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
21950
21951 /* These macros abstract out the exact format of the mnemonic table and
21952    save some repeated characters.  */
21953
21954 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
21955 #define TxCE(mnem, op, top, nops, ops, ae, te) \
21956   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
21957     THUMB_VARIANT, do_##ae, do_##te, 0 }
21958
21959 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
21960    a T_MNEM_xyz enumerator.  */
21961 #define TCE(mnem, aop, top, nops, ops, ae, te) \
21962       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
21963 #define tCE(mnem, aop, top, nops, ops, ae, te) \
21964       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
21965
21966 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
21967    infix after the third character.  */
21968 #define TxC3(mnem, op, top, nops, ops, ae, te) \
21969   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
21970     THUMB_VARIANT, do_##ae, do_##te, 0 }
21971 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
21972   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
21973     THUMB_VARIANT, do_##ae, do_##te, 0 }
21974 #define TC3(mnem, aop, top, nops, ops, ae, te) \
21975       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
21976 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
21977       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
21978 #define tC3(mnem, aop, top, nops, ops, ae, te) \
21979       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
21980 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
21981       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
21982
21983 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
21984    field is still 0xE.  Many of the Thumb variants can be executed
21985    conditionally, so this is checked separately.  */
21986 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
21987   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
21988     THUMB_VARIANT, do_##ae, do_##te, 0 }
21989
21990 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
21991    Used by mnemonics that have very minimal differences in the encoding for
21992    ARM and Thumb variants and can be handled in a common function.  */
21993 #define TUEc(mnem, op, top, nops, ops, en) \
21994   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
21995     THUMB_VARIANT, do_##en, do_##en, 0 }
21996
21997 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
21998    condition code field.  */
21999 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
22000   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
22001     THUMB_VARIANT, do_##ae, do_##te, 0 }
22002
22003 /* ARM-only variants of all the above.  */
22004 #define CE(mnem,  op, nops, ops, ae)    \
22005   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
22006
22007 #define C3(mnem, op, nops, ops, ae)     \
22008   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
22009
22010 /* Thumb-only variants of TCE and TUE.  */
22011 #define ToC(mnem, top, nops, ops, te) \
22012   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
22013     do_##te, 0 }
22014
22015 #define ToU(mnem, top, nops, ops, te) \
22016   { mnem, OPS##nops ops, OT_unconditional, 0x0, 0x##top, 0, THUMB_VARIANT, \
22017     NULL, do_##te, 0 }
22018
22019 /* T_MNEM_xyz enumerator variants of ToC.  */
22020 #define toC(mnem, top, nops, ops, te) \
22021   { mnem, OPS##nops ops, OT_csuffix, 0x0, T_MNEM##top, 0, THUMB_VARIANT, NULL, \
22022     do_##te, 0 }
22023
22024 /* T_MNEM_xyz enumerator variants of ToU.  */
22025 #define toU(mnem, top, nops, ops, te) \
22026   { mnem, OPS##nops ops, OT_unconditional, 0x0, T_MNEM##top, 0, THUMB_VARIANT, \
22027     NULL, do_##te, 0 }
22028
22029 /* Legacy mnemonics that always have conditional infix after the third
22030    character.  */
22031 #define CL(mnem, op, nops, ops, ae)     \
22032   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
22033     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
22034
22035 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
22036 #define cCE(mnem,  op, nops, ops, ae)   \
22037   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
22038
22039 /* mov instructions that are shared between coprocessor and MVE.  */
22040 #define mcCE(mnem,  op, nops, ops, ae)  \
22041   { #mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, THUMB_VARIANT, do_##ae, do_##ae, 0 }
22042
22043 /* Legacy coprocessor instructions where conditional infix and conditional
22044    suffix are ambiguous.  For consistency this includes all FPA instructions,
22045    not just the potentially ambiguous ones.  */
22046 #define cCL(mnem, op, nops, ops, ae)    \
22047   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
22048     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
22049
22050 /* Coprocessor, takes either a suffix or a position-3 infix
22051    (for an FPA corner case). */
22052 #define C3E(mnem, op, nops, ops, ae) \
22053   { mnem, OPS##nops ops, OT_csuf_or_in3, \
22054     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
22055
22056 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
22057   { m1 #m2 m3, OPS##nops ops, \
22058     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
22059     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
22060
22061 #define CM(m1, m2, op, nops, ops, ae)   \
22062   xCM_ (m1,   , m2, op, nops, ops, ae), \
22063   xCM_ (m1, eq, m2, op, nops, ops, ae), \
22064   xCM_ (m1, ne, m2, op, nops, ops, ae), \
22065   xCM_ (m1, cs, m2, op, nops, ops, ae), \
22066   xCM_ (m1, hs, m2, op, nops, ops, ae), \
22067   xCM_ (m1, cc, m2, op, nops, ops, ae), \
22068   xCM_ (m1, ul, m2, op, nops, ops, ae), \
22069   xCM_ (m1, lo, m2, op, nops, ops, ae), \
22070   xCM_ (m1, mi, m2, op, nops, ops, ae), \
22071   xCM_ (m1, pl, m2, op, nops, ops, ae), \
22072   xCM_ (m1, vs, m2, op, nops, ops, ae), \
22073   xCM_ (m1, vc, m2, op, nops, ops, ae), \
22074   xCM_ (m1, hi, m2, op, nops, ops, ae), \
22075   xCM_ (m1, ls, m2, op, nops, ops, ae), \
22076   xCM_ (m1, ge, m2, op, nops, ops, ae), \
22077   xCM_ (m1, lt, m2, op, nops, ops, ae), \
22078   xCM_ (m1, gt, m2, op, nops, ops, ae), \
22079   xCM_ (m1, le, m2, op, nops, ops, ae), \
22080   xCM_ (m1, al, m2, op, nops, ops, ae)
22081
22082 #define UE(mnem, op, nops, ops, ae)     \
22083   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
22084
22085 #define UF(mnem, op, nops, ops, ae)     \
22086   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
22087
22088 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
22089    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
22090    use the same encoding function for each.  */
22091 #define NUF(mnem, op, nops, ops, enc)                                   \
22092   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
22093     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
22094
22095 /* Neon data processing, version which indirects through neon_enc_tab for
22096    the various overloaded versions of opcodes.  */
22097 #define nUF(mnem, op, nops, ops, enc)                                   \
22098   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
22099     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
22100
22101 /* Neon insn with conditional suffix for the ARM version, non-overloaded
22102    version.  */
22103 #define NCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
22104   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
22105     THUMB_VARIANT, do_##enc, do_##enc, mve_p }
22106
22107 #define NCE(mnem, op, nops, ops, enc)                                   \
22108    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
22109
22110 #define NCEF(mnem, op, nops, ops, enc)                                  \
22111     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
22112
22113 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
22114 #define nCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
22115   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
22116     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, mve_p }
22117
22118 #define nCE(mnem, op, nops, ops, enc)                                   \
22119    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
22120
22121 #define nCEF(mnem, op, nops, ops, enc)                                  \
22122     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
22123
22124 /*   */
22125 #define mCEF(mnem, op, nops, ops, enc)                          \
22126   { #mnem, OPS##nops ops, OT_csuffixF, M_MNEM##op, M_MNEM##op,  \
22127     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
22128
22129
22130 /* nCEF but for MVE predicated instructions.  */
22131 #define mnCEF(mnem, op, nops, ops, enc)                                 \
22132     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
22133
22134 /* nCE but for MVE predicated instructions.  */
22135 #define mnCE(mnem, op, nops, ops, enc)                                  \
22136    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
22137
22138 /* NUF but for potentially MVE predicated instructions.  */
22139 #define MNUF(mnem, op, nops, ops, enc)                                  \
22140   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
22141     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
22142
22143 /* nUF but for potentially MVE predicated instructions.  */
22144 #define mnUF(mnem, op, nops, ops, enc)                                  \
22145   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
22146     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
22147
22148 /* ToC but for potentially MVE predicated instructions.  */
22149 #define mToC(mnem, top, nops, ops, te) \
22150   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
22151     do_##te, 1 }
22152
22153 /* NCE but for MVE predicated instructions.  */
22154 #define MNCE(mnem, op, nops, ops, enc)                                  \
22155    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
22156
22157 /* NCEF but for MVE predicated instructions.  */
22158 #define MNCEF(mnem, op, nops, ops, enc)                                 \
22159     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
22160 #define do_0 0
22161
22162 static const struct asm_opcode insns[] =
22163 {
22164 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
22165 #define THUMB_VARIANT  & arm_ext_v4t
22166  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
22167  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
22168  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
22169  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
22170  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
22171  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
22172  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
22173  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
22174  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
22175  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
22176  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
22177  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
22178  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
22179  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
22180  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
22181  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
22182
22183  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
22184     for setting PSR flag bits.  They are obsolete in V6 and do not
22185     have Thumb equivalents. */
22186  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
22187  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
22188   CL("tstp",    110f000,           2, (RR, SH),      cmp),
22189  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
22190  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
22191   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
22192  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
22193  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
22194   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
22195
22196  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
22197  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
22198  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
22199  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
22200
22201  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
22202  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
22203  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
22204                                                                 OP_RRnpc),
22205                                         OP_ADDRGLDR),ldst, t_ldst),
22206  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
22207
22208  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
22209  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
22210  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
22211  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
22212  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
22213  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
22214
22215  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
22216  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
22217
22218   /* Pseudo ops.  */
22219  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
22220   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
22221  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
22222  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
22223
22224   /* Thumb-compatibility pseudo ops.  */
22225  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
22226  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
22227  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
22228  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
22229  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
22230  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
22231  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
22232  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
22233  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
22234  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
22235  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
22236  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
22237
22238  /* These may simplify to neg.  */
22239  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
22240  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
22241
22242 #undef THUMB_VARIANT
22243 #define THUMB_VARIANT  & arm_ext_os
22244
22245  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
22246  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
22247
22248 #undef  THUMB_VARIANT
22249 #define THUMB_VARIANT  & arm_ext_v6
22250
22251  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
22252
22253  /* V1 instructions with no Thumb analogue prior to V6T2.  */
22254 #undef  THUMB_VARIANT
22255 #define THUMB_VARIANT  & arm_ext_v6t2
22256
22257  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
22258  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
22259   CL("teqp",    130f000,           2, (RR, SH),      cmp),
22260
22261  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
22262  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
22263  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
22264  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
22265
22266  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
22267  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
22268
22269  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
22270  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
22271
22272  /* V1 instructions with no Thumb analogue at all.  */
22273   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
22274   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
22275
22276   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
22277   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
22278   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
22279   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
22280   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
22281   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
22282   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
22283   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
22284
22285 #undef  ARM_VARIANT
22286 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
22287 #undef  THUMB_VARIANT
22288 #define THUMB_VARIANT  & arm_ext_v4t
22289
22290  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
22291  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
22292
22293 #undef  THUMB_VARIANT
22294 #define THUMB_VARIANT  & arm_ext_v6t2
22295
22296  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
22297   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
22298
22299   /* Generic coprocessor instructions.  */
22300  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
22301  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
22302  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
22303  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
22304  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
22305  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
22306  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
22307
22308 #undef  ARM_VARIANT
22309 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
22310
22311   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
22312   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
22313
22314 #undef  ARM_VARIANT
22315 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
22316 #undef  THUMB_VARIANT
22317 #define THUMB_VARIANT  & arm_ext_msr
22318
22319  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
22320  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
22321
22322 #undef  ARM_VARIANT
22323 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
22324 #undef  THUMB_VARIANT
22325 #define THUMB_VARIANT  & arm_ext_v6t2
22326
22327  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
22328   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
22329  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
22330   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
22331  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
22332   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
22333  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
22334   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
22335
22336 #undef  ARM_VARIANT
22337 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
22338 #undef  THUMB_VARIANT
22339 #define THUMB_VARIANT  & arm_ext_v4t
22340
22341  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
22342  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
22343  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
22344  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
22345  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
22346  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
22347
22348 #undef  ARM_VARIANT
22349 #define ARM_VARIANT  & arm_ext_v4t_5
22350
22351   /* ARM Architecture 4T.  */
22352   /* Note: bx (and blx) are required on V5, even if the processor does
22353      not support Thumb.  */
22354  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
22355
22356 #undef  ARM_VARIANT
22357 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
22358 #undef  THUMB_VARIANT
22359 #define THUMB_VARIANT  & arm_ext_v5t
22360
22361   /* Note: blx has 2 variants; the .value coded here is for
22362      BLX(2).  Only this variant has conditional execution.  */
22363  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
22364  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
22365
22366 #undef  THUMB_VARIANT
22367 #define THUMB_VARIANT  & arm_ext_v6t2
22368
22369  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
22370  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
22371  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
22372  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
22373  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
22374  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
22375  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
22376  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
22377
22378 #undef  ARM_VARIANT
22379 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
22380 #undef  THUMB_VARIANT
22381 #define THUMB_VARIANT  & arm_ext_v5exp
22382
22383  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
22384  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
22385  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
22386  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
22387
22388  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
22389  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
22390
22391  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
22392  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
22393  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
22394  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
22395
22396  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
22397  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
22398  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
22399  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
22400
22401  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
22402  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
22403
22404  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
22405  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
22406  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
22407  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
22408
22409 #undef  ARM_VARIANT
22410 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
22411 #undef  THUMB_VARIANT
22412 #define THUMB_VARIANT  & arm_ext_v6t2
22413
22414  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
22415  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
22416      ldrd, t_ldstd),
22417  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
22418                                        ADDRGLDRS), ldrd, t_ldstd),
22419
22420  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
22421  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
22422
22423 #undef  ARM_VARIANT
22424 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
22425
22426  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
22427
22428 #undef  ARM_VARIANT
22429 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
22430 #undef  THUMB_VARIANT
22431 #define THUMB_VARIANT  & arm_ext_v6
22432
22433  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
22434  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
22435  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
22436  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
22437  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
22438  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
22439  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
22440  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
22441  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
22442  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
22443
22444 #undef  THUMB_VARIANT
22445 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
22446
22447  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
22448  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
22449                                       strex,  t_strex),
22450 #undef  THUMB_VARIANT
22451 #define THUMB_VARIANT  & arm_ext_v6t2
22452
22453  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
22454  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
22455
22456  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
22457  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
22458
22459 /*  ARM V6 not included in V7M.  */
22460 #undef  THUMB_VARIANT
22461 #define THUMB_VARIANT  & arm_ext_v6_notm
22462  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
22463  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
22464   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
22465   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
22466  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
22467  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
22468   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
22469  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
22470   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
22471  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
22472  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
22473  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
22474   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
22475   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
22476   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
22477   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
22478  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
22479  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
22480  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
22481
22482 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
22483 #undef  THUMB_VARIANT
22484 #define THUMB_VARIANT  & arm_ext_v6_dsp
22485  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
22486  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
22487  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22488  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22489  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22490  /* Old name for QASX.  */
22491  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22492  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22493  /* Old name for QSAX.  */
22494  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22495  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22496  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22497  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22498  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22499  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22500  /* Old name for SASX.  */
22501  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22502  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22503  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22504  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22505  /* Old name for SHASX.  */
22506  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22507  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22508  /* Old name for SHSAX.  */
22509  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22510  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22511  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22512  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22513  /* Old name for SSAX.  */
22514  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22515  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22516  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22517  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22518  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22519  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22520  /* Old name for UASX.  */
22521  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22522  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22523  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22524  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22525  /* Old name for UHASX.  */
22526  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22527  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22528  /* Old name for UHSAX.  */
22529  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22530  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22531  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22532  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22533  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22534  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22535  /* Old name for UQASX.  */
22536  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22537  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22538  /* Old name for UQSAX.  */
22539  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
22540  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22541  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22542  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22543  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22544  /* Old name for USAX.  */
22545  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22546  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22547  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
22548  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
22549  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
22550  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
22551  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
22552  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
22553  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
22554  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
22555  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
22556  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22557  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22558  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
22559  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
22560  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22561  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22562  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
22563  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
22564  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22565  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22566  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22567  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
22568  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
22569  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
22570  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
22571  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
22572  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
22573  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
22574  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
22575  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
22576  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
22577  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
22578  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
22579
22580 #undef  ARM_VARIANT
22581 #define ARM_VARIANT   & arm_ext_v6k_v6t2
22582 #undef  THUMB_VARIANT
22583 #define THUMB_VARIANT & arm_ext_v6k_v6t2
22584
22585  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
22586  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
22587  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
22588  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
22589
22590 #undef  THUMB_VARIANT
22591 #define THUMB_VARIANT  & arm_ext_v6_notm
22592  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
22593                                       ldrexd, t_ldrexd),
22594  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
22595                                        RRnpcb), strexd, t_strexd),
22596
22597 #undef  THUMB_VARIANT
22598 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
22599  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
22600      rd_rn,  rd_rn),
22601  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
22602      rd_rn,  rd_rn),
22603  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
22604      strex, t_strexbh),
22605  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
22606      strex, t_strexbh),
22607  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
22608
22609 #undef  ARM_VARIANT
22610 #define ARM_VARIANT    & arm_ext_sec
22611 #undef  THUMB_VARIANT
22612 #define THUMB_VARIANT  & arm_ext_sec
22613
22614  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
22615
22616 #undef  ARM_VARIANT
22617 #define ARM_VARIANT    & arm_ext_virt
22618 #undef  THUMB_VARIANT
22619 #define THUMB_VARIANT    & arm_ext_virt
22620
22621  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
22622  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
22623
22624 #undef  ARM_VARIANT
22625 #define ARM_VARIANT    & arm_ext_pan
22626 #undef  THUMB_VARIANT
22627 #define THUMB_VARIANT  & arm_ext_pan
22628
22629  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
22630
22631 #undef  ARM_VARIANT
22632 #define ARM_VARIANT    & arm_ext_v6t2
22633 #undef  THUMB_VARIANT
22634 #define THUMB_VARIANT  & arm_ext_v6t2
22635
22636  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
22637  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
22638  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
22639  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
22640
22641  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
22642  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
22643
22644  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
22645  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
22646  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
22647  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
22648
22649 #undef  ARM_VARIANT
22650 #define ARM_VARIANT    & arm_ext_v3
22651 #undef  THUMB_VARIANT
22652 #define THUMB_VARIANT  & arm_ext_v6t2
22653
22654  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
22655  TUF("ssbb",    57ff040, f3bf8f40, 0, (), noargs, t_csdb),
22656  TUF("pssbb",   57ff044, f3bf8f44, 0, (), noargs, t_csdb),
22657
22658 #undef  ARM_VARIANT
22659 #define ARM_VARIANT    & arm_ext_v6t2
22660 #undef  THUMB_VARIANT
22661 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
22662  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
22663  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
22664
22665  /* Thumb-only instructions.  */
22666 #undef  ARM_VARIANT
22667 #define ARM_VARIANT NULL
22668   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
22669   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
22670
22671  /* ARM does not really have an IT instruction, so always allow it.
22672     The opcode is copied from Thumb in order to allow warnings in
22673     -mimplicit-it=[never | arm] modes.  */
22674 #undef  ARM_VARIANT
22675 #define ARM_VARIANT  & arm_ext_v1
22676 #undef  THUMB_VARIANT
22677 #define THUMB_VARIANT  & arm_ext_v6t2
22678
22679  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
22680  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
22681  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
22682  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
22683  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
22684  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
22685  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
22686  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
22687  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
22688  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
22689  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
22690  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
22691  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
22692  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
22693  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
22694  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
22695  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
22696  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
22697
22698  /* Thumb2 only instructions.  */
22699 #undef  ARM_VARIANT
22700 #define ARM_VARIANT  NULL
22701
22702  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
22703  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
22704  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
22705  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
22706  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
22707  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
22708
22709  /* Hardware division instructions.  */
22710 #undef  ARM_VARIANT
22711 #define ARM_VARIANT    & arm_ext_adiv
22712 #undef  THUMB_VARIANT
22713 #define THUMB_VARIANT  & arm_ext_div
22714
22715  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
22716  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
22717
22718  /* ARM V6M/V7 instructions.  */
22719 #undef  ARM_VARIANT
22720 #define ARM_VARIANT    & arm_ext_barrier
22721 #undef  THUMB_VARIANT
22722 #define THUMB_VARIANT  & arm_ext_barrier
22723
22724  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
22725  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
22726  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
22727
22728  /* ARM V7 instructions.  */
22729 #undef  ARM_VARIANT
22730 #define ARM_VARIANT    & arm_ext_v7
22731 #undef  THUMB_VARIANT
22732 #define THUMB_VARIANT  & arm_ext_v7
22733
22734  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
22735  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
22736
22737 #undef  ARM_VARIANT
22738 #define ARM_VARIANT    & arm_ext_mp
22739 #undef  THUMB_VARIANT
22740 #define THUMB_VARIANT  & arm_ext_mp
22741
22742  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
22743
22744  /* AArchv8 instructions.  */
22745 #undef  ARM_VARIANT
22746 #define ARM_VARIANT   & arm_ext_v8
22747
22748 /* Instructions shared between armv8-a and armv8-m.  */
22749 #undef  THUMB_VARIANT
22750 #define THUMB_VARIANT & arm_ext_atomics
22751
22752  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
22753  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
22754  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
22755  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
22756  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
22757  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
22758  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
22759  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
22760  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
22761  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
22762                                                         stlex,  t_stlex),
22763  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
22764                                                         stlex, t_stlex),
22765  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
22766                                                         stlex, t_stlex),
22767 #undef  THUMB_VARIANT
22768 #define THUMB_VARIANT & arm_ext_v8
22769
22770  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
22771  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
22772                                                         ldrexd, t_ldrexd),
22773  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
22774                                                         strexd, t_strexd),
22775
22776 /* Defined in V8 but is in undefined encoding space for earlier
22777    architectures.  However earlier architectures are required to treat
22778    this instuction as a semihosting trap as well.  Hence while not explicitly
22779    defined as such, it is in fact correct to define the instruction for all
22780    architectures.  */
22781 #undef  THUMB_VARIANT
22782 #define THUMB_VARIANT  & arm_ext_v1
22783 #undef  ARM_VARIANT
22784 #define ARM_VARIANT  & arm_ext_v1
22785  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
22786
22787  /* ARMv8 T32 only.  */
22788 #undef  ARM_VARIANT
22789 #define ARM_VARIANT  NULL
22790  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
22791  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
22792  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
22793
22794   /* FP for ARMv8.  */
22795 #undef  ARM_VARIANT
22796 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
22797 #undef  THUMB_VARIANT
22798 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
22799
22800   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
22801   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
22802   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
22803   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
22804   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
22805   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
22806   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
22807   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
22808   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
22809   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
22810   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
22811   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
22812   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
22813
22814   /* Crypto v1 extensions.  */
22815 #undef  ARM_VARIANT
22816 #define ARM_VARIANT & fpu_crypto_ext_armv8
22817 #undef  THUMB_VARIANT
22818 #define THUMB_VARIANT & fpu_crypto_ext_armv8
22819
22820   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
22821   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
22822   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
22823   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
22824   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
22825   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
22826   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
22827   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
22828   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
22829   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
22830   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
22831   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
22832   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
22833   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
22834
22835 #undef  ARM_VARIANT
22836 #define ARM_VARIANT   & crc_ext_armv8
22837 #undef  THUMB_VARIANT
22838 #define THUMB_VARIANT & crc_ext_armv8
22839   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
22840   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
22841   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
22842   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
22843   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
22844   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
22845
22846  /* ARMv8.2 RAS extension.  */
22847 #undef  ARM_VARIANT
22848 #define ARM_VARIANT   & arm_ext_ras
22849 #undef  THUMB_VARIANT
22850 #define THUMB_VARIANT & arm_ext_ras
22851  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
22852
22853 #undef  ARM_VARIANT
22854 #define ARM_VARIANT   & arm_ext_v8_3
22855 #undef  THUMB_VARIANT
22856 #define THUMB_VARIANT & arm_ext_v8_3
22857  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
22858
22859 #undef  ARM_VARIANT
22860 #define ARM_VARIANT   & fpu_neon_ext_dotprod
22861 #undef  THUMB_VARIANT
22862 #define THUMB_VARIANT & fpu_neon_ext_dotprod
22863  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
22864  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
22865
22866 #undef  ARM_VARIANT
22867 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
22868 #undef  THUMB_VARIANT
22869 #define THUMB_VARIANT NULL
22870
22871  cCE("wfs",     e200110, 1, (RR),            rd),
22872  cCE("rfs",     e300110, 1, (RR),            rd),
22873  cCE("wfc",     e400110, 1, (RR),            rd),
22874  cCE("rfc",     e500110, 1, (RR),            rd),
22875
22876  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22877  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22878  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22879  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22880
22881  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22882  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22883  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22884  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
22885
22886  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
22887  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
22888  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
22889  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
22890  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
22891  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
22892  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
22893  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
22894  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
22895  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
22896  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
22897  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
22898
22899  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
22900  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
22901  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
22902  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
22903  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
22904  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
22905  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
22906  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
22907  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
22908  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
22909  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
22910  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
22911
22912  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
22913  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
22914  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
22915  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
22916  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
22917  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
22918  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
22919  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
22920  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
22921  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
22922  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
22923  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
22924
22925  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
22926  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
22927  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
22928  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
22929  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
22930  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
22931  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
22932  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
22933  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
22934  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
22935  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
22936  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
22937
22938  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
22939  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
22940  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
22941  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
22942  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
22943  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
22944  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
22945  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
22946  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
22947  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
22948  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
22949  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
22950
22951  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
22952  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
22953  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
22954  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
22955  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
22956  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
22957  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
22958  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
22959  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
22960  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
22961  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
22962  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
22963
22964  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
22965  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
22966  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
22967  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
22968  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
22969  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
22970  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
22971  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
22972  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
22973  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
22974  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
22975  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
22976
22977  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
22978  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
22979  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
22980  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
22981  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
22982  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
22983  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
22984  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
22985  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
22986  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
22987  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
22988  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
22989
22990  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
22991  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
22992  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
22993  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
22994  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
22995  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
22996  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
22997  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
22998  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
22999  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
23000  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
23001  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
23002
23003  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
23004  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
23005  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
23006  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
23007  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
23008  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
23009  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
23010  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
23011  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
23012  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
23013  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
23014  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
23015
23016  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
23017  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
23018  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
23019  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
23020  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
23021  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
23022  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
23023  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
23024  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
23025  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
23026  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
23027  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
23028
23029  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
23030  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
23031  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
23032  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
23033  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
23034  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
23035  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
23036  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
23037  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
23038  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
23039  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
23040  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
23041
23042  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
23043  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
23044  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
23045  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
23046  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
23047  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
23048  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
23049  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
23050  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
23051  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
23052  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
23053  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
23054
23055  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
23056  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
23057  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
23058  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
23059  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
23060  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
23061  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
23062  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
23063  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
23064  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
23065  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
23066  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
23067
23068  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
23069  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
23070  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
23071  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
23072  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
23073  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
23074  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
23075  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
23076  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
23077  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
23078  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
23079  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
23080
23081  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
23082  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
23083  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
23084  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
23085  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
23086  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
23087  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
23088  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
23089  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
23090  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
23091  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
23092  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
23093
23094  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
23095  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
23096  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
23097  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
23098  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
23099  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23100  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23101  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23102  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
23103  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
23104  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
23105  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
23106
23107  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
23108  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
23109  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
23110  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
23111  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
23112  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23113  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23114  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23115  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
23116  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
23117  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
23118  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
23119
23120  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
23121  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
23122  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
23123  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
23124  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
23125  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23126  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23127  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23128  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
23129  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
23130  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
23131  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
23132
23133  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
23134  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
23135  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
23136  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
23137  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
23138  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23139  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23140  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23141  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
23142  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
23143  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
23144  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
23145
23146  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
23147  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
23148  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
23149  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
23150  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
23151  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23152  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23153  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23154  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
23155  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
23156  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
23157  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
23158
23159  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
23160  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
23161  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
23162  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
23163  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
23164  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23165  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23166  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23167  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
23168  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
23169  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
23170  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
23171
23172  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
23173  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
23174  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
23175  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
23176  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
23177  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23178  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23179  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23180  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
23181  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
23182  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
23183  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
23184
23185  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
23186  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
23187  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
23188  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
23189  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
23190  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23191  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23192  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23193  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
23194  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
23195  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
23196  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
23197
23198  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
23199  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
23200  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
23201  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
23202  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
23203  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23204  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23205  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23206  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
23207  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
23208  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
23209  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
23210
23211  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
23212  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
23213  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
23214  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
23215  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
23216  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23217  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23218  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23219  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
23220  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
23221  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
23222  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
23223
23224  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
23225  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
23226  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
23227  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
23228  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
23229  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23230  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23231  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23232  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
23233  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
23234  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
23235  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
23236
23237  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
23238  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
23239  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
23240  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
23241  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
23242  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23243  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23244  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23245  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
23246  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
23247  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
23248  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
23249
23250  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
23251  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
23252  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
23253  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
23254  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
23255  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
23256  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
23257  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
23258  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
23259  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
23260  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
23261  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
23262
23263  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
23264  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
23265  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
23266  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
23267
23268  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
23269  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
23270  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
23271  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
23272  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
23273  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
23274  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
23275  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
23276  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
23277  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
23278  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
23279  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
23280
23281   /* The implementation of the FIX instruction is broken on some
23282      assemblers, in that it accepts a precision specifier as well as a
23283      rounding specifier, despite the fact that this is meaningless.
23284      To be more compatible, we accept it as well, though of course it
23285      does not set any bits.  */
23286  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
23287  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
23288  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
23289  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
23290  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
23291  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
23292  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
23293  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
23294  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
23295  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
23296  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
23297  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
23298  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
23299
23300   /* Instructions that were new with the real FPA, call them V2.  */
23301 #undef  ARM_VARIANT
23302 #define ARM_VARIANT  & fpu_fpa_ext_v2
23303
23304  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
23305  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
23306  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
23307  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
23308  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
23309  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
23310
23311 #undef  ARM_VARIANT
23312 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
23313
23314   /* Moves and type conversions.  */
23315  cCE("fmstat",  ef1fa10, 0, (),               noargs),
23316  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
23317  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
23318  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
23319  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
23320  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
23321  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
23322  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
23323  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
23324  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
23325  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
23326
23327   /* Memory operations.  */
23328  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
23329  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
23330  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
23331  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
23332  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
23333  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
23334  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
23335  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
23336  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
23337  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
23338  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
23339  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
23340  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
23341  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
23342  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
23343  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
23344  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
23345  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
23346
23347   /* Monadic operations.  */
23348  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
23349  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
23350  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
23351
23352   /* Dyadic operations.  */
23353  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23354  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23355  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23356  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23357  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23358  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23359  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23360  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23361  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23362
23363   /* Comparisons.  */
23364  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
23365  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
23366  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
23367  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
23368
23369  /* Double precision load/store are still present on single precision
23370     implementations.  */
23371  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
23372  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
23373  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
23374  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
23375  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
23376  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
23377  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
23378  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
23379  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
23380  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
23381
23382 #undef  ARM_VARIANT
23383 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
23384
23385   /* Moves and type conversions.  */
23386  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
23387  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
23388  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
23389  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
23390  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
23391  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
23392  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
23393  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
23394  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
23395  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
23396  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
23397  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
23398
23399   /* Monadic operations.  */
23400  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
23401  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
23402  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
23403
23404   /* Dyadic operations.  */
23405  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23406  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23407  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23408  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23409  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23410  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23411  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23412  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23413  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23414
23415   /* Comparisons.  */
23416  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
23417  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
23418  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
23419  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
23420
23421 /* Instructions which may belong to either the Neon or VFP instruction sets.
23422    Individual encoder functions perform additional architecture checks.  */
23423 #undef  ARM_VARIANT
23424 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
23425 #undef  THUMB_VARIANT
23426 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
23427
23428   /* These mnemonics are unique to VFP.  */
23429  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
23430  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
23431  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
23432  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
23433  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
23434  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
23435  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
23436  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
23437
23438   /* Mnemonics shared by Neon and VFP.  */
23439  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
23440  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
23441  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
23442
23443  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
23444  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
23445  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
23446  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
23447  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
23448  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
23449
23450  mnCEF(vcvt,     _vcvt,   3, (RNSDQMQ, RNSDQMQ, oI32z), neon_cvt),
23451  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
23452  MNCEF(vcvtb,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtb),
23453  MNCEF(vcvtt,   eb20a40, 3, (RVSDMQ, RVSDMQ, oI32b), neon_cvtt),
23454
23455
23456   /* NOTE: All VMOV encoding is special-cased!  */
23457  NCE(vmovq,     0,       1, (VMOV), neon_mov),
23458
23459 #undef  THUMB_VARIANT
23460 /* Could be either VLDR/VSTR or VLDR/VSTR (system register) which are guarded
23461    by different feature bits.  Since we are setting the Thumb guard, we can
23462    require Thumb-1 which makes it a nop guard and set the right feature bit in
23463    do_vldr_vstr ().  */
23464 #define THUMB_VARIANT  & arm_ext_v4t
23465  NCE(vldr,      d100b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
23466  NCE(vstr,      d000b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
23467
23468 #undef  ARM_VARIANT
23469 #define ARM_VARIANT    & arm_ext_fp16
23470 #undef  THUMB_VARIANT
23471 #define THUMB_VARIANT  & arm_ext_fp16
23472  /* New instructions added from v8.2, allowing the extraction and insertion of
23473     the upper 16 bits of a 32-bit vector register.  */
23474  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
23475  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
23476
23477  /* New backported fma/fms instructions optional in v8.2.  */
23478  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
23479  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
23480
23481 #undef  THUMB_VARIANT
23482 #define THUMB_VARIANT  & fpu_neon_ext_v1
23483 #undef  ARM_VARIANT
23484 #define ARM_VARIANT    & fpu_neon_ext_v1
23485
23486   /* Data processing with three registers of the same length.  */
23487   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
23488  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
23489  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
23490  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
23491  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
23492  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
23493  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
23494  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
23495  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
23496   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
23497  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
23498  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
23499  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
23500  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
23501  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
23502  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
23503  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
23504  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
23505   /* If not immediate, fall back to neon_dyadic_i64_su.
23506      shl_imm should accept I8 I16 I32 I64,
23507      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
23508  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
23509  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
23510  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
23511  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
23512   /* Logic ops, types optional & ignored.  */
23513  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
23514  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
23515  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
23516  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
23517  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
23518   /* Bitfield ops, untyped.  */
23519  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
23520  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
23521  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
23522  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
23523  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
23524  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
23525   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
23526  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
23527  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
23528  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
23529  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
23530  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
23531   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
23532      back to neon_dyadic_if_su.  */
23533  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
23534  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
23535  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
23536  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
23537  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
23538  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
23539  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
23540  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
23541   /* Comparison. Type I8 I16 I32 F32.  */
23542  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
23543  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
23544   /* As above, D registers only.  */
23545  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
23546  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
23547   /* Int and float variants, signedness unimportant.  */
23548  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
23549  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
23550  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
23551   /* Add/sub take types I8 I16 I32 I64 F32.  */
23552  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
23553  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
23554   /* vtst takes sizes 8, 16, 32.  */
23555  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
23556  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
23557   /* VMUL takes I8 I16 I32 F32 P8.  */
23558  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
23559   /* VQD{R}MULH takes S16 S32.  */
23560  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
23561  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
23562  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
23563  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
23564  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
23565  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
23566  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
23567  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
23568  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
23569  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
23570  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
23571  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
23572  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
23573  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
23574  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
23575  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
23576  /* ARM v8.1 extension.  */
23577  nUF (vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
23578  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
23579  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
23580  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
23581
23582   /* Two address, int/float. Types S8 S16 S32 F32.  */
23583  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
23584  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
23585
23586   /* Data processing with two registers and a shift amount.  */
23587   /* Right shifts, and variants with rounding.
23588      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
23589  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
23590  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
23591  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
23592  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
23593  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
23594  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
23595  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
23596  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
23597   /* Shift and insert. Sizes accepted 8 16 32 64.  */
23598  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
23599  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
23600  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
23601  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
23602   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
23603  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
23604  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
23605   /* Right shift immediate, saturating & narrowing, with rounding variants.
23606      Types accepted S16 S32 S64 U16 U32 U64.  */
23607  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
23608  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
23609   /* As above, unsigned. Types accepted S16 S32 S64.  */
23610  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
23611  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
23612   /* Right shift narrowing. Types accepted I16 I32 I64.  */
23613  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
23614  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
23615   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
23616  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
23617   /* CVT with optional immediate for fixed-point variant.  */
23618  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
23619
23620  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
23621  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
23622
23623   /* Data processing, three registers of different lengths.  */
23624   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
23625  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
23626   /* If not scalar, fall back to neon_dyadic_long.
23627      Vector types as above, scalar types S16 S32 U16 U32.  */
23628  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
23629  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
23630   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
23631  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
23632  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
23633   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
23634  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
23635  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
23636  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
23637  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
23638   /* Saturating doubling multiplies. Types S16 S32.  */
23639  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
23640  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
23641  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
23642   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
23643      S16 S32 U16 U32.  */
23644  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
23645
23646   /* Extract. Size 8.  */
23647  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
23648  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
23649
23650   /* Two registers, miscellaneous.  */
23651   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
23652  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
23653  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
23654  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
23655  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
23656  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
23657  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
23658   /* Vector replicate. Sizes 8 16 32.  */
23659  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
23660  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
23661   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
23662  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
23663   /* VMOVN. Types I16 I32 I64.  */
23664  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
23665   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
23666  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
23667   /* VQMOVUN. Types S16 S32 S64.  */
23668  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
23669   /* VZIP / VUZP. Sizes 8 16 32.  */
23670  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
23671  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
23672  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
23673  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
23674   /* VQABS / VQNEG. Types S8 S16 S32.  */
23675  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
23676  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
23677  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
23678  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
23679   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
23680  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
23681  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
23682  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
23683  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
23684   /* Reciprocal estimates.  Types U32 F16 F32.  */
23685  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
23686  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
23687  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
23688  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
23689   /* VCLS. Types S8 S16 S32.  */
23690  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
23691   /* VCLZ. Types I8 I16 I32.  */
23692  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
23693   /* VCNT. Size 8.  */
23694  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
23695  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
23696   /* Two address, untyped.  */
23697  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
23698  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
23699   /* VTRN. Sizes 8 16 32.  */
23700  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
23701  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
23702
23703   /* Table lookup. Size 8.  */
23704  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
23705  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
23706
23707 #undef  THUMB_VARIANT
23708 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
23709 #undef  ARM_VARIANT
23710 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
23711
23712   /* Neon element/structure load/store.  */
23713  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23714  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23715  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23716  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23717  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23718  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23719  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23720  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
23721
23722 #undef  THUMB_VARIANT
23723 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
23724 #undef  ARM_VARIANT
23725 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
23726  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
23727  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
23728  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
23729  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
23730  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
23731  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
23732  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
23733  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
23734  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
23735
23736 #undef  THUMB_VARIANT
23737 #define THUMB_VARIANT  & fpu_vfp_ext_v3
23738 #undef  ARM_VARIANT
23739 #define ARM_VARIANT    & fpu_vfp_ext_v3
23740
23741  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
23742  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
23743  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
23744  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
23745  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
23746  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
23747  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
23748  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
23749  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
23750
23751 #undef  ARM_VARIANT
23752 #define ARM_VARIANT    & fpu_vfp_ext_fma
23753 #undef  THUMB_VARIANT
23754 #define THUMB_VARIANT  & fpu_vfp_ext_fma
23755  /* Mnemonics shared by Neon and VFP.  These are included in the
23756     VFP FMA variant; NEON and VFP FMA always includes the NEON
23757     FMA instructions.  */
23758  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
23759  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
23760  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
23761     the v form should always be used.  */
23762  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23763  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
23764  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23765  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
23766  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
23767  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
23768
23769 #undef THUMB_VARIANT
23770 #undef  ARM_VARIANT
23771 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
23772
23773  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
23774  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
23775  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
23776  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
23777  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
23778  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
23779  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
23780  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
23781
23782 #undef  ARM_VARIANT
23783 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
23784
23785  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
23786  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
23787  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
23788  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
23789  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
23790  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
23791  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
23792  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
23793  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
23794  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
23795  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
23796  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
23797  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
23798  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
23799  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
23800  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
23801  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
23802  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
23803  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
23804  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
23805  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
23806  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
23807  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
23808  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
23809  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
23810  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
23811  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
23812  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
23813  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
23814  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
23815  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
23816  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
23817  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
23818  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
23819  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
23820  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
23821  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
23822  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23823  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23824  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23825  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23826  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23827  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23828  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23829  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23830  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23831  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
23832  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23833  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23834  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23835  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23836  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23837  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23838  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23839  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23840  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23841  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23842  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23843  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23844  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23845  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23846  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23847  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23848  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23849  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23850  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23851  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
23852  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
23853  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
23854  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
23855  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23856  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23857  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23858  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23859  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23860  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23861  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23862  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23863  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23864  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23865  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23866  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23867  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23868  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23869  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23870  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23871  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23872  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23873  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
23874  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23875  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23876  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23877  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23878  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23879  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23880  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23881  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23882  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23883  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23884  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23885  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23886  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23887  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23888  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23889  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23890  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23891  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23892  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23893  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23894  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23895  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
23896  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23897  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23898  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23899  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23900  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23901  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23902  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23903  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23904  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23905  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23906  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23907  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23908  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23909  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23910  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23911  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23912  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
23913  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
23914  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
23915  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
23916  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
23917  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
23918  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23919  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23920  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23921  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23922  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23923  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23924  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23925  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23926  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23927  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
23928  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
23929  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
23930  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
23931  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
23932  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
23933  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
23934  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
23935  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
23936  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
23937  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
23938  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
23939  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
23940  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
23941  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
23942  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
23943  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
23944  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
23945  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23946  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
23947
23948 #undef  ARM_VARIANT
23949 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
23950
23951  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
23952  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
23953  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
23954  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
23955  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
23956  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
23957  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23958  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23959  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23960  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23961  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23962  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23963  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23964  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23965  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23966  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23967  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23968  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23969  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23970  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23971  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
23972  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23973  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23974  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23975  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23976  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23977  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23978  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23979  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23980  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23981  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23982  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23983  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23984  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23985  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23986  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23987  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23988  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23989  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23990  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23991  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23992  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23993  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23994  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23995  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23996  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23997  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23998  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
23999  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24000  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24001  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24002  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24003  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24004  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24005  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24006  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24007  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
24008
24009 #undef  ARM_VARIANT
24010 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
24011
24012  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
24013  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
24014  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
24015  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
24016  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
24017  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
24018  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
24019  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
24020  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
24021  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
24022  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
24023  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
24024  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
24025  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
24026  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
24027  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
24028  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
24029  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
24030  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
24031  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
24032  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
24033  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
24034  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
24035  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
24036  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
24037  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
24038  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
24039  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
24040  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
24041  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
24042  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
24043  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
24044  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
24045  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
24046  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
24047  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
24048  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
24049  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
24050  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
24051  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
24052  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
24053  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
24054  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
24055  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
24056  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
24057  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
24058  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
24059  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
24060  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
24061  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
24062  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
24063  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
24064  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
24065  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
24066  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
24067  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
24068  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
24069  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
24070  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
24071  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
24072  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
24073  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
24074  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
24075  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
24076  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
24077  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
24078  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
24079  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
24080  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
24081  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
24082  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
24083  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
24084  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
24085  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
24086  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
24087  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
24088
24089  /* ARMv8.5-A instructions.  */
24090 #undef  ARM_VARIANT
24091 #define ARM_VARIANT   & arm_ext_sb
24092 #undef  THUMB_VARIANT
24093 #define THUMB_VARIANT & arm_ext_sb
24094  TUF("sb", 57ff070, f3bf8f70, 0, (), noargs, noargs),
24095
24096 #undef  ARM_VARIANT
24097 #define ARM_VARIANT   & arm_ext_predres
24098 #undef  THUMB_VARIANT
24099 #define THUMB_VARIANT & arm_ext_predres
24100  CE("cfprctx", e070f93, 1, (RRnpc), rd),
24101  CE("dvprctx", e070fb3, 1, (RRnpc), rd),
24102  CE("cpprctx", e070ff3, 1, (RRnpc), rd),
24103
24104  /* ARMv8-M instructions.  */
24105 #undef  ARM_VARIANT
24106 #define ARM_VARIANT NULL
24107 #undef  THUMB_VARIANT
24108 #define THUMB_VARIANT & arm_ext_v8m
24109  ToU("sg",    e97fe97f, 0, (),             noargs),
24110  ToC("blxns", 4784,     1, (RRnpc),        t_blx),
24111  ToC("bxns",  4704,     1, (RRnpc),        t_bx),
24112  ToC("tt",    e840f000, 2, (RRnpc, RRnpc), tt),
24113  ToC("ttt",   e840f040, 2, (RRnpc, RRnpc), tt),
24114  ToC("tta",   e840f080, 2, (RRnpc, RRnpc), tt),
24115  ToC("ttat",  e840f0c0, 2, (RRnpc, RRnpc), tt),
24116
24117  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
24118     instructions behave as nop if no VFP is present.  */
24119 #undef  THUMB_VARIANT
24120 #define THUMB_VARIANT & arm_ext_v8m_main
24121  ToC("vlldm", ec300a00, 1, (RRnpc), rn),
24122  ToC("vlstm", ec200a00, 1, (RRnpc), rn),
24123
24124  /* Armv8.1-M Mainline instructions.  */
24125 #undef  THUMB_VARIANT
24126 #define THUMB_VARIANT & arm_ext_v8_1m_main
24127  toC("bf",     _bf,     2, (EXPs, EXPs),             t_branch_future),
24128  toU("bfcsel", _bfcsel, 4, (EXPs, EXPs, EXPs, COND), t_branch_future),
24129  toC("bfx",    _bfx,    2, (EXPs, RRnpcsp),          t_branch_future),
24130  toC("bfl",    _bfl,    2, (EXPs, EXPs),             t_branch_future),
24131  toC("bflx",   _bflx,   2, (EXPs, RRnpcsp),          t_branch_future),
24132
24133  toU("dls", _dls, 2, (LR, RRnpcsp),      t_loloop),
24134  toU("wls", _wls, 3, (LR, RRnpcsp, EXP), t_loloop),
24135  toU("le",  _le,  2, (oLR, EXP),         t_loloop),
24136
24137  ToC("clrm",    e89f0000, 1, (CLRMLST),  t_clrm),
24138  ToC("vscclrm", ec9f0a00, 1, (VRSDVLST), t_vscclrm),
24139
24140 #undef  THUMB_VARIANT
24141 #define THUMB_VARIANT & mve_ext
24142
24143  ToC("vpt",     ee410f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24144  ToC("vptt",    ee018f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24145  ToC("vpte",    ee418f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24146  ToC("vpttt",   ee014f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24147  ToC("vptte",   ee01cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24148  ToC("vptet",   ee41cf00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24149  ToC("vptee",   ee414f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24150  ToC("vptttt",  ee012f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24151  ToC("vpttte",  ee016f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24152  ToC("vpttet",  ee01ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24153  ToC("vpttee",  ee01af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24154  ToC("vptett",  ee41af00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24155  ToC("vptete",  ee41ef00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24156  ToC("vpteet",  ee416f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24157  ToC("vpteee",  ee412f00, 3, (COND, RMQ, RMQRZ), mve_vpt),
24158
24159  ToC("vpst",    fe710f4d, 0, (), mve_vpt),
24160  ToC("vpstt",   fe318f4d, 0, (), mve_vpt),
24161  ToC("vpste",   fe718f4d, 0, (), mve_vpt),
24162  ToC("vpsttt",  fe314f4d, 0, (), mve_vpt),
24163  ToC("vpstte",  fe31cf4d, 0, (), mve_vpt),
24164  ToC("vpstet",  fe71cf4d, 0, (), mve_vpt),
24165  ToC("vpstee",  fe714f4d, 0, (), mve_vpt),
24166  ToC("vpstttt", fe312f4d, 0, (), mve_vpt),
24167  ToC("vpsttte", fe316f4d, 0, (), mve_vpt),
24168  ToC("vpsttet", fe31ef4d, 0, (), mve_vpt),
24169  ToC("vpsttee", fe31af4d, 0, (), mve_vpt),
24170  ToC("vpstett", fe71af4d, 0, (), mve_vpt),
24171  ToC("vpstete", fe71ef4d, 0, (), mve_vpt),
24172  ToC("vpsteet", fe716f4d, 0, (), mve_vpt),
24173  ToC("vpsteee", fe712f4d, 0, (), mve_vpt),
24174
24175  /* MVE and MVE FP only.  */
24176  mCEF(vadc,     _vadc,      3, (RMQ, RMQ, RMQ),                   mve_vadc),
24177  mCEF(vadci,    _vadci,     3, (RMQ, RMQ, RMQ),                   mve_vadc),
24178  mToC("vsbc",   fe300f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
24179  mToC("vsbci",  fe301f00,   3, (RMQ, RMQ, RMQ),                   mve_vsbc),
24180  mCEF(vmullb,   _vmullb,    3, (RMQ, RMQ, RMQ),                   mve_vmull),
24181  mCEF(vabav,    _vabav,     3, (RRnpcsp, RMQ, RMQ),               mve_vabav),
24182  mCEF(vmladav,    _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
24183  mCEF(vmladava,   _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
24184  mCEF(vmladavx,   _vmladavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
24185  mCEF(vmladavax,  _vmladavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
24186  mCEF(vmlav,      _vmladav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
24187  mCEF(vmlava,     _vmladava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
24188  mCEF(vmlsdav,    _vmlsdav,     3, (RRe, RMQ, RMQ),             mve_vmladav),
24189  mCEF(vmlsdava,   _vmlsdava,    3, (RRe, RMQ, RMQ),             mve_vmladav),
24190  mCEF(vmlsdavx,   _vmlsdavx,    3, (RRe, RMQ, RMQ),             mve_vmladav),
24191  mCEF(vmlsdavax,  _vmlsdavax,   3, (RRe, RMQ, RMQ),             mve_vmladav),
24192
24193  mCEF(vst20,    _vst20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
24194  mCEF(vst21,    _vst21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
24195  mCEF(vst40,    _vst40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24196  mCEF(vst41,    _vst41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24197  mCEF(vst42,    _vst42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24198  mCEF(vst43,    _vst43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24199  mCEF(vld20,    _vld20,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
24200  mCEF(vld21,    _vld21,     2, (MSTRLST2, ADDRMVE),             mve_vst_vld),
24201  mCEF(vld40,    _vld40,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24202  mCEF(vld41,    _vld41,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24203  mCEF(vld42,    _vld42,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24204  mCEF(vld43,    _vld43,     2, (MSTRLST4, ADDRMVE),             mve_vst_vld),
24205  mCEF(vstrb,    _vstrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24206  mCEF(vstrh,    _vstrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24207  mCEF(vstrw,    _vstrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24208  mCEF(vstrd,    _vstrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24209  mCEF(vldrb,    _vldrb,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24210  mCEF(vldrh,    _vldrh,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24211  mCEF(vldrw,    _vldrw,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24212  mCEF(vldrd,    _vldrd,     2, (RMQ, ADDRMVE),                  mve_vstr_vldr),
24213
24214  mCEF(vmovnt,   _vmovnt,    2, (RMQ, RMQ),                        mve_movn),
24215  mCEF(vmovnb,   _vmovnb,    2, (RMQ, RMQ),                        mve_movn),
24216  mCEF(vbrsr,    _vbrsr,     3, (RMQ, RMQ, RR),                    mve_vbrsr),
24217  mCEF(vaddlv,   _vaddlv,    3, (RRe, RRo, RMQ),                   mve_vaddlv),
24218  mCEF(vaddlva,  _vaddlva,   3, (RRe, RRo, RMQ),                   mve_vaddlv),
24219  mCEF(vaddv,    _vaddv,     2, (RRe, RMQ),                        mve_vaddv),
24220  mCEF(vaddva,   _vaddva,    2, (RRe, RMQ),                        mve_vaddv),
24221
24222 #undef THUMB_VARIANT
24223 #define THUMB_VARIANT & mve_fp_ext
24224  mToC("vcmul", ee300e00,   4, (RMQ, RMQ, RMQ, EXPi),              mve_vcmul),
24225  mToC("vfmas", ee311e40,   3, (RMQ, RMQ, RR),                     mve_vfmas),
24226
24227 #undef  ARM_VARIANT
24228 #define ARM_VARIANT  & fpu_vfp_ext_v1
24229 #undef  THUMB_VARIANT
24230 #define THUMB_VARIANT  & arm_ext_v6t2
24231
24232  mcCE(fcpyd,    eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
24233
24234 #undef  ARM_VARIANT
24235 #define ARM_VARIANT  & fpu_vfp_ext_v1xd
24236
24237  MNCE(vmov,   0,        1, (VMOV),            neon_mov),
24238  mcCE(fmrs,     e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
24239  mcCE(fmsr,     e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
24240  mcCE(fcpys,    eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
24241
24242  mCEF(vmullt, _vmullt,  3, (RNSDQMQ, oRNSDQMQ, RNSDQ_RNSC_MQ),  mve_vmull),
24243  mnCEF(vadd,  _vadd,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
24244  mnCEF(vsub,  _vsub,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR),       neon_addsub_if_i),
24245
24246  MNCEF(vabs,  1b10300,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
24247  MNCEF(vneg,  1b10380,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
24248
24249  mCEF(vmovlt, _vmovlt,  1, (VMOV),              mve_movl),
24250  mCEF(vmovlb, _vmovlb,  1, (VMOV),              mve_movl),
24251
24252  mnCE(vcmp,      _vcmp,    3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
24253  mnCE(vcmpe,     _vcmpe,   3, (RVSD_COND, RSVDMQ_FI0, oRMQRZ),    vfp_nsyn_cmp),
24254
24255 #undef  ARM_VARIANT
24256 #define ARM_VARIANT  & fpu_vfp_ext_v2
24257
24258  mcCE(fmsrr,    c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
24259  mcCE(fmrrs,    c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
24260  mcCE(fmdrr,    c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
24261  mcCE(fmrrd,    c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
24262
24263 #undef  ARM_VARIANT
24264 #define ARM_VARIANT    & fpu_vfp_ext_armv8xd
24265  mnUF(vcvta,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvta),
24266  mnUF(vcvtp,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtp),
24267  mnUF(vcvtn,  _vcvta,  3, (RNSDQMQ, oRNSDQMQ, oI32z),   neon_cvtn),
24268  mnUF(vcvtm,  _vcvta,  2, (RNSDQMQ, oRNSDQMQ),          neon_cvtm),
24269
24270 #undef  ARM_VARIANT
24271 #define ARM_VARIANT & fpu_neon_ext_v1
24272  mnUF(vabd,      _vabd,           3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
24273  mnUF(vabdl,     _vabdl,          3, (RNQMQ, RNDMQ, RNDMQ),   neon_dyadic_long),
24274  mnUF(vaddl,     _vaddl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
24275  mnUF(vsubl,     _vsubl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
24276  mnUF(vand,      _vand,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
24277  mnUF(vbic,      _vbic,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
24278  mnUF(vorr,      _vorr,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
24279  mnUF(vorn,      _vorn,           3, (RNDQMQ, oRNDQMQ, RNDQMQ_Ibig), neon_logic),
24280  mnUF(veor,      _veor,           3, (RNDQMQ, oRNDQMQ, RNDQMQ),      neon_logic),
24281  MNUF(vcls,      1b00400,         2, (RNDQMQ, RNDQMQ),               neon_cls),
24282  MNUF(vclz,      1b00480,         2, (RNDQMQ, RNDQMQ),               neon_clz),
24283
24284 #undef  ARM_VARIANT
24285 #define ARM_VARIANT & arm_ext_v8_3
24286 #undef  THUMB_VARIANT
24287 #define THUMB_VARIANT & arm_ext_v6t2_v8m
24288  MNUF (vcadd, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ, EXPi), vcadd),
24289  MNUF (vcmla, 0, 4, (RNDQMQ, RNDQMQ, RNDQMQ_RNSC, EXPi), vcmla),
24290 };
24291 #undef ARM_VARIANT
24292 #undef THUMB_VARIANT
24293 #undef TCE
24294 #undef TUE
24295 #undef TUF
24296 #undef TCC
24297 #undef cCE
24298 #undef cCL
24299 #undef C3E
24300 #undef C3
24301 #undef CE
24302 #undef CM
24303 #undef CL
24304 #undef UE
24305 #undef UF
24306 #undef UT
24307 #undef NUF
24308 #undef nUF
24309 #undef NCE
24310 #undef nCE
24311 #undef OPS0
24312 #undef OPS1
24313 #undef OPS2
24314 #undef OPS3
24315 #undef OPS4
24316 #undef OPS5
24317 #undef OPS6
24318 #undef do_0
24319 #undef ToC
24320 #undef toC
24321 #undef ToU
24322 #undef toU
24323 \f
24324 /* MD interface: bits in the object file.  */
24325
24326 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
24327    for use in the a.out file, and stores them in the array pointed to by buf.
24328    This knows about the endian-ness of the target machine and does
24329    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
24330    2 (short) and 4 (long)  Floating numbers are put out as a series of
24331    LITTLENUMS (shorts, here at least).  */
24332
24333 void
24334 md_number_to_chars (char * buf, valueT val, int n)
24335 {
24336   if (target_big_endian)
24337     number_to_chars_bigendian (buf, val, n);
24338   else
24339     number_to_chars_littleendian (buf, val, n);
24340 }
24341
24342 static valueT
24343 md_chars_to_number (char * buf, int n)
24344 {
24345   valueT result = 0;
24346   unsigned char * where = (unsigned char *) buf;
24347
24348   if (target_big_endian)
24349     {
24350       while (n--)
24351         {
24352           result <<= 8;
24353           result |= (*where++ & 255);
24354         }
24355     }
24356   else
24357     {
24358       while (n--)
24359         {
24360           result <<= 8;
24361           result |= (where[n] & 255);
24362         }
24363     }
24364
24365   return result;
24366 }
24367
24368 /* MD interface: Sections.  */
24369
24370 /* Calculate the maximum variable size (i.e., excluding fr_fix)
24371    that an rs_machine_dependent frag may reach.  */
24372
24373 unsigned int
24374 arm_frag_max_var (fragS *fragp)
24375 {
24376   /* We only use rs_machine_dependent for variable-size Thumb instructions,
24377      which are either THUMB_SIZE (2) or INSN_SIZE (4).
24378
24379      Note that we generate relaxable instructions even for cases that don't
24380      really need it, like an immediate that's a trivial constant.  So we're
24381      overestimating the instruction size for some of those cases.  Rather
24382      than putting more intelligence here, it would probably be better to
24383      avoid generating a relaxation frag in the first place when it can be
24384      determined up front that a short instruction will suffice.  */
24385
24386   gas_assert (fragp->fr_type == rs_machine_dependent);
24387   return INSN_SIZE;
24388 }
24389
24390 /* Estimate the size of a frag before relaxing.  Assume everything fits in
24391    2 bytes.  */
24392
24393 int
24394 md_estimate_size_before_relax (fragS * fragp,
24395                                segT    segtype ATTRIBUTE_UNUSED)
24396 {
24397   fragp->fr_var = 2;
24398   return 2;
24399 }
24400
24401 /* Convert a machine dependent frag.  */
24402
24403 void
24404 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
24405 {
24406   unsigned long insn;
24407   unsigned long old_op;
24408   char *buf;
24409   expressionS exp;
24410   fixS *fixp;
24411   int reloc_type;
24412   int pc_rel;
24413   int opcode;
24414
24415   buf = fragp->fr_literal + fragp->fr_fix;
24416
24417   old_op = bfd_get_16(abfd, buf);
24418   if (fragp->fr_symbol)
24419     {
24420       exp.X_op = O_symbol;
24421       exp.X_add_symbol = fragp->fr_symbol;
24422     }
24423   else
24424     {
24425       exp.X_op = O_constant;
24426     }
24427   exp.X_add_number = fragp->fr_offset;
24428   opcode = fragp->fr_subtype;
24429   switch (opcode)
24430     {
24431     case T_MNEM_ldr_pc:
24432     case T_MNEM_ldr_pc2:
24433     case T_MNEM_ldr_sp:
24434     case T_MNEM_str_sp:
24435     case T_MNEM_ldr:
24436     case T_MNEM_ldrb:
24437     case T_MNEM_ldrh:
24438     case T_MNEM_str:
24439     case T_MNEM_strb:
24440     case T_MNEM_strh:
24441       if (fragp->fr_var == 4)
24442         {
24443           insn = THUMB_OP32 (opcode);
24444           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
24445             {
24446               insn |= (old_op & 0x700) << 4;
24447             }
24448           else
24449             {
24450               insn |= (old_op & 7) << 12;
24451               insn |= (old_op & 0x38) << 13;
24452             }
24453           insn |= 0x00000c00;
24454           put_thumb32_insn (buf, insn);
24455           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
24456         }
24457       else
24458         {
24459           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
24460         }
24461       pc_rel = (opcode == T_MNEM_ldr_pc2);
24462       break;
24463     case T_MNEM_adr:
24464       if (fragp->fr_var == 4)
24465         {
24466           insn = THUMB_OP32 (opcode);
24467           insn |= (old_op & 0xf0) << 4;
24468           put_thumb32_insn (buf, insn);
24469           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
24470         }
24471       else
24472         {
24473           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
24474           exp.X_add_number -= 4;
24475         }
24476       pc_rel = 1;
24477       break;
24478     case T_MNEM_mov:
24479     case T_MNEM_movs:
24480     case T_MNEM_cmp:
24481     case T_MNEM_cmn:
24482       if (fragp->fr_var == 4)
24483         {
24484           int r0off = (opcode == T_MNEM_mov
24485                        || opcode == T_MNEM_movs) ? 0 : 8;
24486           insn = THUMB_OP32 (opcode);
24487           insn = (insn & 0xe1ffffff) | 0x10000000;
24488           insn |= (old_op & 0x700) << r0off;
24489           put_thumb32_insn (buf, insn);
24490           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
24491         }
24492       else
24493         {
24494           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
24495         }
24496       pc_rel = 0;
24497       break;
24498     case T_MNEM_b:
24499       if (fragp->fr_var == 4)
24500         {
24501           insn = THUMB_OP32(opcode);
24502           put_thumb32_insn (buf, insn);
24503           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
24504         }
24505       else
24506         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
24507       pc_rel = 1;
24508       break;
24509     case T_MNEM_bcond:
24510       if (fragp->fr_var == 4)
24511         {
24512           insn = THUMB_OP32(opcode);
24513           insn |= (old_op & 0xf00) << 14;
24514           put_thumb32_insn (buf, insn);
24515           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
24516         }
24517       else
24518         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
24519       pc_rel = 1;
24520       break;
24521     case T_MNEM_add_sp:
24522     case T_MNEM_add_pc:
24523     case T_MNEM_inc_sp:
24524     case T_MNEM_dec_sp:
24525       if (fragp->fr_var == 4)
24526         {
24527           /* ??? Choose between add and addw.  */
24528           insn = THUMB_OP32 (opcode);
24529           insn |= (old_op & 0xf0) << 4;
24530           put_thumb32_insn (buf, insn);
24531           if (opcode == T_MNEM_add_pc)
24532             reloc_type = BFD_RELOC_ARM_T32_IMM12;
24533           else
24534             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
24535         }
24536       else
24537         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
24538       pc_rel = 0;
24539       break;
24540
24541     case T_MNEM_addi:
24542     case T_MNEM_addis:
24543     case T_MNEM_subi:
24544     case T_MNEM_subis:
24545       if (fragp->fr_var == 4)
24546         {
24547           insn = THUMB_OP32 (opcode);
24548           insn |= (old_op & 0xf0) << 4;
24549           insn |= (old_op & 0xf) << 16;
24550           put_thumb32_insn (buf, insn);
24551           if (insn & (1 << 20))
24552             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
24553           else
24554             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
24555         }
24556       else
24557         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
24558       pc_rel = 0;
24559       break;
24560     default:
24561       abort ();
24562     }
24563   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
24564                       (enum bfd_reloc_code_real) reloc_type);
24565   fixp->fx_file = fragp->fr_file;
24566   fixp->fx_line = fragp->fr_line;
24567   fragp->fr_fix += fragp->fr_var;
24568
24569   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
24570   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
24571       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
24572     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
24573 }
24574
24575 /* Return the size of a relaxable immediate operand instruction.
24576    SHIFT and SIZE specify the form of the allowable immediate.  */
24577 static int
24578 relax_immediate (fragS *fragp, int size, int shift)
24579 {
24580   offsetT offset;
24581   offsetT mask;
24582   offsetT low;
24583
24584   /* ??? Should be able to do better than this.  */
24585   if (fragp->fr_symbol)
24586     return 4;
24587
24588   low = (1 << shift) - 1;
24589   mask = (1 << (shift + size)) - (1 << shift);
24590   offset = fragp->fr_offset;
24591   /* Force misaligned offsets to 32-bit variant.  */
24592   if (offset & low)
24593     return 4;
24594   if (offset & ~mask)
24595     return 4;
24596   return 2;
24597 }
24598
24599 /* Get the address of a symbol during relaxation.  */
24600 static addressT
24601 relaxed_symbol_addr (fragS *fragp, long stretch)
24602 {
24603   fragS *sym_frag;
24604   addressT addr;
24605   symbolS *sym;
24606
24607   sym = fragp->fr_symbol;
24608   sym_frag = symbol_get_frag (sym);
24609   know (S_GET_SEGMENT (sym) != absolute_section
24610         || sym_frag == &zero_address_frag);
24611   addr = S_GET_VALUE (sym) + fragp->fr_offset;
24612
24613   /* If frag has yet to be reached on this pass, assume it will
24614      move by STRETCH just as we did.  If this is not so, it will
24615      be because some frag between grows, and that will force
24616      another pass.  */
24617
24618   if (stretch != 0
24619       && sym_frag->relax_marker != fragp->relax_marker)
24620     {
24621       fragS *f;
24622
24623       /* Adjust stretch for any alignment frag.  Note that if have
24624          been expanding the earlier code, the symbol may be
24625          defined in what appears to be an earlier frag.  FIXME:
24626          This doesn't handle the fr_subtype field, which specifies
24627          a maximum number of bytes to skip when doing an
24628          alignment.  */
24629       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
24630         {
24631           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
24632             {
24633               if (stretch < 0)
24634                 stretch = - ((- stretch)
24635                              & ~ ((1 << (int) f->fr_offset) - 1));
24636               else
24637                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
24638               if (stretch == 0)
24639                 break;
24640             }
24641         }
24642       if (f != NULL)
24643         addr += stretch;
24644     }
24645
24646   return addr;
24647 }
24648
24649 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
24650    load.  */
24651 static int
24652 relax_adr (fragS *fragp, asection *sec, long stretch)
24653 {
24654   addressT addr;
24655   offsetT val;
24656
24657   /* Assume worst case for symbols not known to be in the same section.  */
24658   if (fragp->fr_symbol == NULL
24659       || !S_IS_DEFINED (fragp->fr_symbol)
24660       || sec != S_GET_SEGMENT (fragp->fr_symbol)
24661       || S_IS_WEAK (fragp->fr_symbol))
24662     return 4;
24663
24664   val = relaxed_symbol_addr (fragp, stretch);
24665   addr = fragp->fr_address + fragp->fr_fix;
24666   addr = (addr + 4) & ~3;
24667   /* Force misaligned targets to 32-bit variant.  */
24668   if (val & 3)
24669     return 4;
24670   val -= addr;
24671   if (val < 0 || val > 1020)
24672     return 4;
24673   return 2;
24674 }
24675
24676 /* Return the size of a relaxable add/sub immediate instruction.  */
24677 static int
24678 relax_addsub (fragS *fragp, asection *sec)
24679 {
24680   char *buf;
24681   int op;
24682
24683   buf = fragp->fr_literal + fragp->fr_fix;
24684   op = bfd_get_16(sec->owner, buf);
24685   if ((op & 0xf) == ((op >> 4) & 0xf))
24686     return relax_immediate (fragp, 8, 0);
24687   else
24688     return relax_immediate (fragp, 3, 0);
24689 }
24690
24691 /* Return TRUE iff the definition of symbol S could be pre-empted
24692    (overridden) at link or load time.  */
24693 static bfd_boolean
24694 symbol_preemptible (symbolS *s)
24695 {
24696   /* Weak symbols can always be pre-empted.  */
24697   if (S_IS_WEAK (s))
24698     return TRUE;
24699
24700   /* Non-global symbols cannot be pre-empted. */
24701   if (! S_IS_EXTERNAL (s))
24702     return FALSE;
24703
24704 #ifdef OBJ_ELF
24705   /* In ELF, a global symbol can be marked protected, or private.  In that
24706      case it can't be pre-empted (other definitions in the same link unit
24707      would violate the ODR).  */
24708   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
24709     return FALSE;
24710 #endif
24711
24712   /* Other global symbols might be pre-empted.  */
24713   return TRUE;
24714 }
24715
24716 /* Return the size of a relaxable branch instruction.  BITS is the
24717    size of the offset field in the narrow instruction.  */
24718
24719 static int
24720 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
24721 {
24722   addressT addr;
24723   offsetT val;
24724   offsetT limit;
24725
24726   /* Assume worst case for symbols not known to be in the same section.  */
24727   if (!S_IS_DEFINED (fragp->fr_symbol)
24728       || sec != S_GET_SEGMENT (fragp->fr_symbol)
24729       || S_IS_WEAK (fragp->fr_symbol))
24730     return 4;
24731
24732 #ifdef OBJ_ELF
24733   /* A branch to a function in ARM state will require interworking.  */
24734   if (S_IS_DEFINED (fragp->fr_symbol)
24735       && ARM_IS_FUNC (fragp->fr_symbol))
24736       return 4;
24737 #endif
24738
24739   if (symbol_preemptible (fragp->fr_symbol))
24740     return 4;
24741
24742   val = relaxed_symbol_addr (fragp, stretch);
24743   addr = fragp->fr_address + fragp->fr_fix + 4;
24744   val -= addr;
24745
24746   /* Offset is a signed value *2 */
24747   limit = 1 << bits;
24748   if (val >= limit || val < -limit)
24749     return 4;
24750   return 2;
24751 }
24752
24753
24754 /* Relax a machine dependent frag.  This returns the amount by which
24755    the current size of the frag should change.  */
24756
24757 int
24758 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
24759 {
24760   int oldsize;
24761   int newsize;
24762
24763   oldsize = fragp->fr_var;
24764   switch (fragp->fr_subtype)
24765     {
24766     case T_MNEM_ldr_pc2:
24767       newsize = relax_adr (fragp, sec, stretch);
24768       break;
24769     case T_MNEM_ldr_pc:
24770     case T_MNEM_ldr_sp:
24771     case T_MNEM_str_sp:
24772       newsize = relax_immediate (fragp, 8, 2);
24773       break;
24774     case T_MNEM_ldr:
24775     case T_MNEM_str:
24776       newsize = relax_immediate (fragp, 5, 2);
24777       break;
24778     case T_MNEM_ldrh:
24779     case T_MNEM_strh:
24780       newsize = relax_immediate (fragp, 5, 1);
24781       break;
24782     case T_MNEM_ldrb:
24783     case T_MNEM_strb:
24784       newsize = relax_immediate (fragp, 5, 0);
24785       break;
24786     case T_MNEM_adr:
24787       newsize = relax_adr (fragp, sec, stretch);
24788       break;
24789     case T_MNEM_mov:
24790     case T_MNEM_movs:
24791     case T_MNEM_cmp:
24792     case T_MNEM_cmn:
24793       newsize = relax_immediate (fragp, 8, 0);
24794       break;
24795     case T_MNEM_b:
24796       newsize = relax_branch (fragp, sec, 11, stretch);
24797       break;
24798     case T_MNEM_bcond:
24799       newsize = relax_branch (fragp, sec, 8, stretch);
24800       break;
24801     case T_MNEM_add_sp:
24802     case T_MNEM_add_pc:
24803       newsize = relax_immediate (fragp, 8, 2);
24804       break;
24805     case T_MNEM_inc_sp:
24806     case T_MNEM_dec_sp:
24807       newsize = relax_immediate (fragp, 7, 2);
24808       break;
24809     case T_MNEM_addi:
24810     case T_MNEM_addis:
24811     case T_MNEM_subi:
24812     case T_MNEM_subis:
24813       newsize = relax_addsub (fragp, sec);
24814       break;
24815     default:
24816       abort ();
24817     }
24818
24819   fragp->fr_var = newsize;
24820   /* Freeze wide instructions that are at or before the same location as
24821      in the previous pass.  This avoids infinite loops.
24822      Don't freeze them unconditionally because targets may be artificially
24823      misaligned by the expansion of preceding frags.  */
24824   if (stretch <= 0 && newsize > 2)
24825     {
24826       md_convert_frag (sec->owner, sec, fragp);
24827       frag_wane (fragp);
24828     }
24829
24830   return newsize - oldsize;
24831 }
24832
24833 /* Round up a section size to the appropriate boundary.  */
24834
24835 valueT
24836 md_section_align (segT   segment ATTRIBUTE_UNUSED,
24837                   valueT size)
24838 {
24839   return size;
24840 }
24841
24842 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
24843    of an rs_align_code fragment.  */
24844
24845 void
24846 arm_handle_align (fragS * fragP)
24847 {
24848   static unsigned char const arm_noop[2][2][4] =
24849     {
24850       {  /* ARMv1 */
24851         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
24852         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
24853       },
24854       {  /* ARMv6k */
24855         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
24856         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
24857       },
24858     };
24859   static unsigned char const thumb_noop[2][2][2] =
24860     {
24861       {  /* Thumb-1 */
24862         {0xc0, 0x46},  /* LE */
24863         {0x46, 0xc0},  /* BE */
24864       },
24865       {  /* Thumb-2 */
24866         {0x00, 0xbf},  /* LE */
24867         {0xbf, 0x00}   /* BE */
24868       }
24869     };
24870   static unsigned char const wide_thumb_noop[2][4] =
24871     {  /* Wide Thumb-2 */
24872       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
24873       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
24874     };
24875
24876   unsigned bytes, fix, noop_size;
24877   char * p;
24878   const unsigned char * noop;
24879   const unsigned char *narrow_noop = NULL;
24880 #ifdef OBJ_ELF
24881   enum mstate state;
24882 #endif
24883
24884   if (fragP->fr_type != rs_align_code)
24885     return;
24886
24887   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
24888   p = fragP->fr_literal + fragP->fr_fix;
24889   fix = 0;
24890
24891   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
24892     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
24893
24894   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
24895
24896   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
24897     {
24898       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
24899                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
24900         {
24901           narrow_noop = thumb_noop[1][target_big_endian];
24902           noop = wide_thumb_noop[target_big_endian];
24903         }
24904       else
24905         noop = thumb_noop[0][target_big_endian];
24906       noop_size = 2;
24907 #ifdef OBJ_ELF
24908       state = MAP_THUMB;
24909 #endif
24910     }
24911   else
24912     {
24913       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
24914                                            ? selected_cpu : arm_arch_none,
24915                                            arm_ext_v6k) != 0]
24916                      [target_big_endian];
24917       noop_size = 4;
24918 #ifdef OBJ_ELF
24919       state = MAP_ARM;
24920 #endif
24921     }
24922
24923   fragP->fr_var = noop_size;
24924
24925   if (bytes & (noop_size - 1))
24926     {
24927       fix = bytes & (noop_size - 1);
24928 #ifdef OBJ_ELF
24929       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
24930 #endif
24931       memset (p, 0, fix);
24932       p += fix;
24933       bytes -= fix;
24934     }
24935
24936   if (narrow_noop)
24937     {
24938       if (bytes & noop_size)
24939         {
24940           /* Insert a narrow noop.  */
24941           memcpy (p, narrow_noop, noop_size);
24942           p += noop_size;
24943           bytes -= noop_size;
24944           fix += noop_size;
24945         }
24946
24947       /* Use wide noops for the remainder */
24948       noop_size = 4;
24949     }
24950
24951   while (bytes >= noop_size)
24952     {
24953       memcpy (p, noop, noop_size);
24954       p += noop_size;
24955       bytes -= noop_size;
24956       fix += noop_size;
24957     }
24958
24959   fragP->fr_fix += fix;
24960 }
24961
24962 /* Called from md_do_align.  Used to create an alignment
24963    frag in a code section.  */
24964
24965 void
24966 arm_frag_align_code (int n, int max)
24967 {
24968   char * p;
24969
24970   /* We assume that there will never be a requirement
24971      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
24972   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
24973     {
24974       char err_msg[128];
24975
24976       sprintf (err_msg,
24977         _("alignments greater than %d bytes not supported in .text sections."),
24978         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
24979       as_fatal ("%s", err_msg);
24980     }
24981
24982   p = frag_var (rs_align_code,
24983                 MAX_MEM_FOR_RS_ALIGN_CODE,
24984                 1,
24985                 (relax_substateT) max,
24986                 (symbolS *) NULL,
24987                 (offsetT) n,
24988                 (char *) NULL);
24989   *p = 0;
24990 }
24991
24992 /* Perform target specific initialisation of a frag.
24993    Note - despite the name this initialisation is not done when the frag
24994    is created, but only when its type is assigned.  A frag can be created
24995    and used a long time before its type is set, so beware of assuming that
24996    this initialisation is performed first.  */
24997
24998 #ifndef OBJ_ELF
24999 void
25000 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
25001 {
25002   /* Record whether this frag is in an ARM or a THUMB area.  */
25003   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
25004 }
25005
25006 #else /* OBJ_ELF is defined.  */
25007 void
25008 arm_init_frag (fragS * fragP, int max_chars)
25009 {
25010   bfd_boolean frag_thumb_mode;
25011
25012   /* If the current ARM vs THUMB mode has not already
25013      been recorded into this frag then do so now.  */
25014   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
25015     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
25016
25017   /* PR 21809: Do not set a mapping state for debug sections
25018      - it just confuses other tools.  */
25019   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
25020     return;
25021
25022   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
25023
25024   /* Record a mapping symbol for alignment frags.  We will delete this
25025      later if the alignment ends up empty.  */
25026   switch (fragP->fr_type)
25027     {
25028     case rs_align:
25029     case rs_align_test:
25030     case rs_fill:
25031       mapping_state_2 (MAP_DATA, max_chars);
25032       break;
25033     case rs_align_code:
25034       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
25035       break;
25036     default:
25037       break;
25038     }
25039 }
25040
25041 /* When we change sections we need to issue a new mapping symbol.  */
25042
25043 void
25044 arm_elf_change_section (void)
25045 {
25046   /* Link an unlinked unwind index table section to the .text section.  */
25047   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
25048       && elf_linked_to_section (now_seg) == NULL)
25049     elf_linked_to_section (now_seg) = text_section;
25050 }
25051
25052 int
25053 arm_elf_section_type (const char * str, size_t len)
25054 {
25055   if (len == 5 && strncmp (str, "exidx", 5) == 0)
25056     return SHT_ARM_EXIDX;
25057
25058   return -1;
25059 }
25060 \f
25061 /* Code to deal with unwinding tables.  */
25062
25063 static void add_unwind_adjustsp (offsetT);
25064
25065 /* Generate any deferred unwind frame offset.  */
25066
25067 static void
25068 flush_pending_unwind (void)
25069 {
25070   offsetT offset;
25071
25072   offset = unwind.pending_offset;
25073   unwind.pending_offset = 0;
25074   if (offset != 0)
25075     add_unwind_adjustsp (offset);
25076 }
25077
25078 /* Add an opcode to this list for this function.  Two-byte opcodes should
25079    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
25080    order.  */
25081
25082 static void
25083 add_unwind_opcode (valueT op, int length)
25084 {
25085   /* Add any deferred stack adjustment.  */
25086   if (unwind.pending_offset)
25087     flush_pending_unwind ();
25088
25089   unwind.sp_restored = 0;
25090
25091   if (unwind.opcode_count + length > unwind.opcode_alloc)
25092     {
25093       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
25094       if (unwind.opcodes)
25095         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
25096                                      unwind.opcode_alloc);
25097       else
25098         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
25099     }
25100   while (length > 0)
25101     {
25102       length--;
25103       unwind.opcodes[unwind.opcode_count] = op & 0xff;
25104       op >>= 8;
25105       unwind.opcode_count++;
25106     }
25107 }
25108
25109 /* Add unwind opcodes to adjust the stack pointer.  */
25110
25111 static void
25112 add_unwind_adjustsp (offsetT offset)
25113 {
25114   valueT op;
25115
25116   if (offset > 0x200)
25117     {
25118       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
25119       char bytes[5];
25120       int n;
25121       valueT o;
25122
25123       /* Long form: 0xb2, uleb128.  */
25124       /* This might not fit in a word so add the individual bytes,
25125          remembering the list is built in reverse order.  */
25126       o = (valueT) ((offset - 0x204) >> 2);
25127       if (o == 0)
25128         add_unwind_opcode (0, 1);
25129
25130       /* Calculate the uleb128 encoding of the offset.  */
25131       n = 0;
25132       while (o)
25133         {
25134           bytes[n] = o & 0x7f;
25135           o >>= 7;
25136           if (o)
25137             bytes[n] |= 0x80;
25138           n++;
25139         }
25140       /* Add the insn.  */
25141       for (; n; n--)
25142         add_unwind_opcode (bytes[n - 1], 1);
25143       add_unwind_opcode (0xb2, 1);
25144     }
25145   else if (offset > 0x100)
25146     {
25147       /* Two short opcodes.  */
25148       add_unwind_opcode (0x3f, 1);
25149       op = (offset - 0x104) >> 2;
25150       add_unwind_opcode (op, 1);
25151     }
25152   else if (offset > 0)
25153     {
25154       /* Short opcode.  */
25155       op = (offset - 4) >> 2;
25156       add_unwind_opcode (op, 1);
25157     }
25158   else if (offset < 0)
25159     {
25160       offset = -offset;
25161       while (offset > 0x100)
25162         {
25163           add_unwind_opcode (0x7f, 1);
25164           offset -= 0x100;
25165         }
25166       op = ((offset - 4) >> 2) | 0x40;
25167       add_unwind_opcode (op, 1);
25168     }
25169 }
25170
25171 /* Finish the list of unwind opcodes for this function.  */
25172
25173 static void
25174 finish_unwind_opcodes (void)
25175 {
25176   valueT op;
25177
25178   if (unwind.fp_used)
25179     {
25180       /* Adjust sp as necessary.  */
25181       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
25182       flush_pending_unwind ();
25183
25184       /* After restoring sp from the frame pointer.  */
25185       op = 0x90 | unwind.fp_reg;
25186       add_unwind_opcode (op, 1);
25187     }
25188   else
25189     flush_pending_unwind ();
25190 }
25191
25192
25193 /* Start an exception table entry.  If idx is nonzero this is an index table
25194    entry.  */
25195
25196 static void
25197 start_unwind_section (const segT text_seg, int idx)
25198 {
25199   const char * text_name;
25200   const char * prefix;
25201   const char * prefix_once;
25202   const char * group_name;
25203   char * sec_name;
25204   int type;
25205   int flags;
25206   int linkonce;
25207
25208   if (idx)
25209     {
25210       prefix = ELF_STRING_ARM_unwind;
25211       prefix_once = ELF_STRING_ARM_unwind_once;
25212       type = SHT_ARM_EXIDX;
25213     }
25214   else
25215     {
25216       prefix = ELF_STRING_ARM_unwind_info;
25217       prefix_once = ELF_STRING_ARM_unwind_info_once;
25218       type = SHT_PROGBITS;
25219     }
25220
25221   text_name = segment_name (text_seg);
25222   if (streq (text_name, ".text"))
25223     text_name = "";
25224
25225   if (strncmp (text_name, ".gnu.linkonce.t.",
25226                strlen (".gnu.linkonce.t.")) == 0)
25227     {
25228       prefix = prefix_once;
25229       text_name += strlen (".gnu.linkonce.t.");
25230     }
25231
25232   sec_name = concat (prefix, text_name, (char *) NULL);
25233
25234   flags = SHF_ALLOC;
25235   linkonce = 0;
25236   group_name = 0;
25237
25238   /* Handle COMDAT group.  */
25239   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
25240     {
25241       group_name = elf_group_name (text_seg);
25242       if (group_name == NULL)
25243         {
25244           as_bad (_("Group section `%s' has no group signature"),
25245                   segment_name (text_seg));
25246           ignore_rest_of_line ();
25247           return;
25248         }
25249       flags |= SHF_GROUP;
25250       linkonce = 1;
25251     }
25252
25253   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
25254                           linkonce, 0);
25255
25256   /* Set the section link for index tables.  */
25257   if (idx)
25258     elf_linked_to_section (now_seg) = text_seg;
25259 }
25260
25261
25262 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
25263    personality routine data.  Returns zero, or the index table value for
25264    an inline entry.  */
25265
25266 static valueT
25267 create_unwind_entry (int have_data)
25268 {
25269   int size;
25270   addressT where;
25271   char *ptr;
25272   /* The current word of data.  */
25273   valueT data;
25274   /* The number of bytes left in this word.  */
25275   int n;
25276
25277   finish_unwind_opcodes ();
25278
25279   /* Remember the current text section.  */
25280   unwind.saved_seg = now_seg;
25281   unwind.saved_subseg = now_subseg;
25282
25283   start_unwind_section (now_seg, 0);
25284
25285   if (unwind.personality_routine == NULL)
25286     {
25287       if (unwind.personality_index == -2)
25288         {
25289           if (have_data)
25290             as_bad (_("handlerdata in cantunwind frame"));
25291           return 1; /* EXIDX_CANTUNWIND.  */
25292         }
25293
25294       /* Use a default personality routine if none is specified.  */
25295       if (unwind.personality_index == -1)
25296         {
25297           if (unwind.opcode_count > 3)
25298             unwind.personality_index = 1;
25299           else
25300             unwind.personality_index = 0;
25301         }
25302
25303       /* Space for the personality routine entry.  */
25304       if (unwind.personality_index == 0)
25305         {
25306           if (unwind.opcode_count > 3)
25307             as_bad (_("too many unwind opcodes for personality routine 0"));
25308
25309           if (!have_data)
25310             {
25311               /* All the data is inline in the index table.  */
25312               data = 0x80;
25313               n = 3;
25314               while (unwind.opcode_count > 0)
25315                 {
25316                   unwind.opcode_count--;
25317                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
25318                   n--;
25319                 }
25320
25321               /* Pad with "finish" opcodes.  */
25322               while (n--)
25323                 data = (data << 8) | 0xb0;
25324
25325               return data;
25326             }
25327           size = 0;
25328         }
25329       else
25330         /* We get two opcodes "free" in the first word.  */
25331         size = unwind.opcode_count - 2;
25332     }
25333   else
25334     {
25335       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
25336       if (unwind.personality_index != -1)
25337         {
25338           as_bad (_("attempt to recreate an unwind entry"));
25339           return 1;
25340         }
25341
25342       /* An extra byte is required for the opcode count.        */
25343       size = unwind.opcode_count + 1;
25344     }
25345
25346   size = (size + 3) >> 2;
25347   if (size > 0xff)
25348     as_bad (_("too many unwind opcodes"));
25349
25350   frag_align (2, 0, 0);
25351   record_alignment (now_seg, 2);
25352   unwind.table_entry = expr_build_dot ();
25353
25354   /* Allocate the table entry.  */
25355   ptr = frag_more ((size << 2) + 4);
25356   /* PR 13449: Zero the table entries in case some of them are not used.  */
25357   memset (ptr, 0, (size << 2) + 4);
25358   where = frag_now_fix () - ((size << 2) + 4);
25359
25360   switch (unwind.personality_index)
25361     {
25362     case -1:
25363       /* ??? Should this be a PLT generating relocation?  */
25364       /* Custom personality routine.  */
25365       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
25366                BFD_RELOC_ARM_PREL31);
25367
25368       where += 4;
25369       ptr += 4;
25370
25371       /* Set the first byte to the number of additional words.  */
25372       data = size > 0 ? size - 1 : 0;
25373       n = 3;
25374       break;
25375
25376     /* ABI defined personality routines.  */
25377     case 0:
25378       /* Three opcodes bytes are packed into the first word.  */
25379       data = 0x80;
25380       n = 3;
25381       break;
25382
25383     case 1:
25384     case 2:
25385       /* The size and first two opcode bytes go in the first word.  */
25386       data = ((0x80 + unwind.personality_index) << 8) | size;
25387       n = 2;
25388       break;
25389
25390     default:
25391       /* Should never happen.  */
25392       abort ();
25393     }
25394
25395   /* Pack the opcodes into words (MSB first), reversing the list at the same
25396      time.  */
25397   while (unwind.opcode_count > 0)
25398     {
25399       if (n == 0)
25400         {
25401           md_number_to_chars (ptr, data, 4);
25402           ptr += 4;
25403           n = 4;
25404           data = 0;
25405         }
25406       unwind.opcode_count--;
25407       n--;
25408       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
25409     }
25410
25411   /* Finish off the last word.  */
25412   if (n < 4)
25413     {
25414       /* Pad with "finish" opcodes.  */
25415       while (n--)
25416         data = (data << 8) | 0xb0;
25417
25418       md_number_to_chars (ptr, data, 4);
25419     }
25420
25421   if (!have_data)
25422     {
25423       /* Add an empty descriptor if there is no user-specified data.   */
25424       ptr = frag_more (4);
25425       md_number_to_chars (ptr, 0, 4);
25426     }
25427
25428   return 0;
25429 }
25430
25431
25432 /* Initialize the DWARF-2 unwind information for this procedure.  */
25433
25434 void
25435 tc_arm_frame_initial_instructions (void)
25436 {
25437   cfi_add_CFA_def_cfa (REG_SP, 0);
25438 }
25439 #endif /* OBJ_ELF */
25440
25441 /* Convert REGNAME to a DWARF-2 register number.  */
25442
25443 int
25444 tc_arm_regname_to_dw2regnum (char *regname)
25445 {
25446   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
25447   if (reg != FAIL)
25448     return reg;
25449
25450   /* PR 16694: Allow VFP registers as well.  */
25451   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
25452   if (reg != FAIL)
25453     return 64 + reg;
25454
25455   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
25456   if (reg != FAIL)
25457     return reg + 256;
25458
25459   return FAIL;
25460 }
25461
25462 #ifdef TE_PE
25463 void
25464 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
25465 {
25466   expressionS exp;
25467
25468   exp.X_op = O_secrel;
25469   exp.X_add_symbol = symbol;
25470   exp.X_add_number = 0;
25471   emit_expr (&exp, size);
25472 }
25473 #endif
25474
25475 /* MD interface: Symbol and relocation handling.  */
25476
25477 /* Return the address within the segment that a PC-relative fixup is
25478    relative to.  For ARM, PC-relative fixups applied to instructions
25479    are generally relative to the location of the fixup plus 8 bytes.
25480    Thumb branches are offset by 4, and Thumb loads relative to PC
25481    require special handling.  */
25482
25483 long
25484 md_pcrel_from_section (fixS * fixP, segT seg)
25485 {
25486   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
25487
25488   /* If this is pc-relative and we are going to emit a relocation
25489      then we just want to put out any pipeline compensation that the linker
25490      will need.  Otherwise we want to use the calculated base.
25491      For WinCE we skip the bias for externals as well, since this
25492      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
25493   if (fixP->fx_pcrel
25494       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
25495           || (arm_force_relocation (fixP)
25496 #ifdef TE_WINCE
25497               && !S_IS_EXTERNAL (fixP->fx_addsy)
25498 #endif
25499               )))
25500     base = 0;
25501
25502
25503   switch (fixP->fx_r_type)
25504     {
25505       /* PC relative addressing on the Thumb is slightly odd as the
25506          bottom two bits of the PC are forced to zero for the
25507          calculation.  This happens *after* application of the
25508          pipeline offset.  However, Thumb adrl already adjusts for
25509          this, so we need not do it again.  */
25510     case BFD_RELOC_ARM_THUMB_ADD:
25511       return base & ~3;
25512
25513     case BFD_RELOC_ARM_THUMB_OFFSET:
25514     case BFD_RELOC_ARM_T32_OFFSET_IMM:
25515     case BFD_RELOC_ARM_T32_ADD_PC12:
25516     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
25517       return (base + 4) & ~3;
25518
25519       /* Thumb branches are simply offset by +4.  */
25520     case BFD_RELOC_THUMB_PCREL_BRANCH5:
25521     case BFD_RELOC_THUMB_PCREL_BRANCH7:
25522     case BFD_RELOC_THUMB_PCREL_BRANCH9:
25523     case BFD_RELOC_THUMB_PCREL_BRANCH12:
25524     case BFD_RELOC_THUMB_PCREL_BRANCH20:
25525     case BFD_RELOC_THUMB_PCREL_BRANCH25:
25526     case BFD_RELOC_THUMB_PCREL_BFCSEL:
25527     case BFD_RELOC_ARM_THUMB_BF17:
25528     case BFD_RELOC_ARM_THUMB_BF19:
25529     case BFD_RELOC_ARM_THUMB_BF13:
25530     case BFD_RELOC_ARM_THUMB_LOOP12:
25531       return base + 4;
25532
25533     case BFD_RELOC_THUMB_PCREL_BRANCH23:
25534       if (fixP->fx_addsy
25535           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25536           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25537           && ARM_IS_FUNC (fixP->fx_addsy)
25538           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
25539         base = fixP->fx_where + fixP->fx_frag->fr_address;
25540        return base + 4;
25541
25542       /* BLX is like branches above, but forces the low two bits of PC to
25543          zero.  */
25544     case BFD_RELOC_THUMB_PCREL_BLX:
25545       if (fixP->fx_addsy
25546           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25547           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25548           && THUMB_IS_FUNC (fixP->fx_addsy)
25549           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
25550         base = fixP->fx_where + fixP->fx_frag->fr_address;
25551       return (base + 4) & ~3;
25552
25553       /* ARM mode branches are offset by +8.  However, the Windows CE
25554          loader expects the relocation not to take this into account.  */
25555     case BFD_RELOC_ARM_PCREL_BLX:
25556       if (fixP->fx_addsy
25557           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25558           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25559           && ARM_IS_FUNC (fixP->fx_addsy)
25560           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
25561         base = fixP->fx_where + fixP->fx_frag->fr_address;
25562       return base + 8;
25563
25564     case BFD_RELOC_ARM_PCREL_CALL:
25565       if (fixP->fx_addsy
25566           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25567           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25568           && THUMB_IS_FUNC (fixP->fx_addsy)
25569           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
25570         base = fixP->fx_where + fixP->fx_frag->fr_address;
25571       return base + 8;
25572
25573     case BFD_RELOC_ARM_PCREL_BRANCH:
25574     case BFD_RELOC_ARM_PCREL_JUMP:
25575     case BFD_RELOC_ARM_PLT32:
25576 #ifdef TE_WINCE
25577       /* When handling fixups immediately, because we have already
25578          discovered the value of a symbol, or the address of the frag involved
25579          we must account for the offset by +8, as the OS loader will never see the reloc.
25580          see fixup_segment() in write.c
25581          The S_IS_EXTERNAL test handles the case of global symbols.
25582          Those need the calculated base, not just the pipe compensation the linker will need.  */
25583       if (fixP->fx_pcrel
25584           && fixP->fx_addsy != NULL
25585           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25586           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
25587         return base + 8;
25588       return base;
25589 #else
25590       return base + 8;
25591 #endif
25592
25593
25594       /* ARM mode loads relative to PC are also offset by +8.  Unlike
25595          branches, the Windows CE loader *does* expect the relocation
25596          to take this into account.  */
25597     case BFD_RELOC_ARM_OFFSET_IMM:
25598     case BFD_RELOC_ARM_OFFSET_IMM8:
25599     case BFD_RELOC_ARM_HWLITERAL:
25600     case BFD_RELOC_ARM_LITERAL:
25601     case BFD_RELOC_ARM_CP_OFF_IMM:
25602       return base + 8;
25603
25604
25605       /* Other PC-relative relocations are un-offset.  */
25606     default:
25607       return base;
25608     }
25609 }
25610
25611 static bfd_boolean flag_warn_syms = TRUE;
25612
25613 bfd_boolean
25614 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
25615 {
25616   /* PR 18347 - Warn if the user attempts to create a symbol with the same
25617      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
25618      does mean that the resulting code might be very confusing to the reader.
25619      Also this warning can be triggered if the user omits an operand before
25620      an immediate address, eg:
25621
25622        LDR =foo
25623
25624      GAS treats this as an assignment of the value of the symbol foo to a
25625      symbol LDR, and so (without this code) it will not issue any kind of
25626      warning or error message.
25627
25628      Note - ARM instructions are case-insensitive but the strings in the hash
25629      table are all stored in lower case, so we must first ensure that name is
25630      lower case too.  */
25631   if (flag_warn_syms && arm_ops_hsh)
25632     {
25633       char * nbuf = strdup (name);
25634       char * p;
25635
25636       for (p = nbuf; *p; p++)
25637         *p = TOLOWER (*p);
25638       if (hash_find (arm_ops_hsh, nbuf) != NULL)
25639         {
25640           static struct hash_control * already_warned = NULL;
25641
25642           if (already_warned == NULL)
25643             already_warned = hash_new ();
25644           /* Only warn about the symbol once.  To keep the code
25645              simple we let hash_insert do the lookup for us.  */
25646           if (hash_insert (already_warned, nbuf, NULL) == NULL)
25647             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
25648         }
25649       else
25650         free (nbuf);
25651     }
25652
25653   return FALSE;
25654 }
25655
25656 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
25657    Otherwise we have no need to default values of symbols.  */
25658
25659 symbolS *
25660 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
25661 {
25662 #ifdef OBJ_ELF
25663   if (name[0] == '_' && name[1] == 'G'
25664       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
25665     {
25666       if (!GOT_symbol)
25667         {
25668           if (symbol_find (name))
25669             as_bad (_("GOT already in the symbol table"));
25670
25671           GOT_symbol = symbol_new (name, undefined_section,
25672                                    (valueT) 0, & zero_address_frag);
25673         }
25674
25675       return GOT_symbol;
25676     }
25677 #endif
25678
25679   return NULL;
25680 }
25681
25682 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
25683    computed as two separate immediate values, added together.  We
25684    already know that this value cannot be computed by just one ARM
25685    instruction.  */
25686
25687 static unsigned int
25688 validate_immediate_twopart (unsigned int   val,
25689                             unsigned int * highpart)
25690 {
25691   unsigned int a;
25692   unsigned int i;
25693
25694   for (i = 0; i < 32; i += 2)
25695     if (((a = rotate_left (val, i)) & 0xff) != 0)
25696       {
25697         if (a & 0xff00)
25698           {
25699             if (a & ~ 0xffff)
25700               continue;
25701             * highpart = (a  >> 8) | ((i + 24) << 7);
25702           }
25703         else if (a & 0xff0000)
25704           {
25705             if (a & 0xff000000)
25706               continue;
25707             * highpart = (a >> 16) | ((i + 16) << 7);
25708           }
25709         else
25710           {
25711             gas_assert (a & 0xff000000);
25712             * highpart = (a >> 24) | ((i + 8) << 7);
25713           }
25714
25715         return (a & 0xff) | (i << 7);
25716       }
25717
25718   return FAIL;
25719 }
25720
25721 static int
25722 validate_offset_imm (unsigned int val, int hwse)
25723 {
25724   if ((hwse && val > 255) || val > 4095)
25725     return FAIL;
25726   return val;
25727 }
25728
25729 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
25730    negative immediate constant by altering the instruction.  A bit of
25731    a hack really.
25732         MOV <-> MVN
25733         AND <-> BIC
25734         ADC <-> SBC
25735         by inverting the second operand, and
25736         ADD <-> SUB
25737         CMP <-> CMN
25738         by negating the second operand.  */
25739
25740 static int
25741 negate_data_op (unsigned long * instruction,
25742                 unsigned long   value)
25743 {
25744   int op, new_inst;
25745   unsigned long negated, inverted;
25746
25747   negated = encode_arm_immediate (-value);
25748   inverted = encode_arm_immediate (~value);
25749
25750   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
25751   switch (op)
25752     {
25753       /* First negates.  */
25754     case OPCODE_SUB:             /* ADD <-> SUB  */
25755       new_inst = OPCODE_ADD;
25756       value = negated;
25757       break;
25758
25759     case OPCODE_ADD:
25760       new_inst = OPCODE_SUB;
25761       value = negated;
25762       break;
25763
25764     case OPCODE_CMP:             /* CMP <-> CMN  */
25765       new_inst = OPCODE_CMN;
25766       value = negated;
25767       break;
25768
25769     case OPCODE_CMN:
25770       new_inst = OPCODE_CMP;
25771       value = negated;
25772       break;
25773
25774       /* Now Inverted ops.  */
25775     case OPCODE_MOV:             /* MOV <-> MVN  */
25776       new_inst = OPCODE_MVN;
25777       value = inverted;
25778       break;
25779
25780     case OPCODE_MVN:
25781       new_inst = OPCODE_MOV;
25782       value = inverted;
25783       break;
25784
25785     case OPCODE_AND:             /* AND <-> BIC  */
25786       new_inst = OPCODE_BIC;
25787       value = inverted;
25788       break;
25789
25790     case OPCODE_BIC:
25791       new_inst = OPCODE_AND;
25792       value = inverted;
25793       break;
25794
25795     case OPCODE_ADC:              /* ADC <-> SBC  */
25796       new_inst = OPCODE_SBC;
25797       value = inverted;
25798       break;
25799
25800     case OPCODE_SBC:
25801       new_inst = OPCODE_ADC;
25802       value = inverted;
25803       break;
25804
25805       /* We cannot do anything.  */
25806     default:
25807       return FAIL;
25808     }
25809
25810   if (value == (unsigned) FAIL)
25811     return FAIL;
25812
25813   *instruction &= OPCODE_MASK;
25814   *instruction |= new_inst << DATA_OP_SHIFT;
25815   return value;
25816 }
25817
25818 /* Like negate_data_op, but for Thumb-2.   */
25819
25820 static unsigned int
25821 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
25822 {
25823   int op, new_inst;
25824   int rd;
25825   unsigned int negated, inverted;
25826
25827   negated = encode_thumb32_immediate (-value);
25828   inverted = encode_thumb32_immediate (~value);
25829
25830   rd = (*instruction >> 8) & 0xf;
25831   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
25832   switch (op)
25833     {
25834       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
25835     case T2_OPCODE_SUB:
25836       new_inst = T2_OPCODE_ADD;
25837       value = negated;
25838       break;
25839
25840     case T2_OPCODE_ADD:
25841       new_inst = T2_OPCODE_SUB;
25842       value = negated;
25843       break;
25844
25845       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
25846     case T2_OPCODE_ORR:
25847       new_inst = T2_OPCODE_ORN;
25848       value = inverted;
25849       break;
25850
25851     case T2_OPCODE_ORN:
25852       new_inst = T2_OPCODE_ORR;
25853       value = inverted;
25854       break;
25855
25856       /* AND <-> BIC.  TST has no inverted equivalent.  */
25857     case T2_OPCODE_AND:
25858       new_inst = T2_OPCODE_BIC;
25859       if (rd == 15)
25860         value = FAIL;
25861       else
25862         value = inverted;
25863       break;
25864
25865     case T2_OPCODE_BIC:
25866       new_inst = T2_OPCODE_AND;
25867       value = inverted;
25868       break;
25869
25870       /* ADC <-> SBC  */
25871     case T2_OPCODE_ADC:
25872       new_inst = T2_OPCODE_SBC;
25873       value = inverted;
25874       break;
25875
25876     case T2_OPCODE_SBC:
25877       new_inst = T2_OPCODE_ADC;
25878       value = inverted;
25879       break;
25880
25881       /* We cannot do anything.  */
25882     default:
25883       return FAIL;
25884     }
25885
25886   if (value == (unsigned int)FAIL)
25887     return FAIL;
25888
25889   *instruction &= T2_OPCODE_MASK;
25890   *instruction |= new_inst << T2_DATA_OP_SHIFT;
25891   return value;
25892 }
25893
25894 /* Read a 32-bit thumb instruction from buf.  */
25895
25896 static unsigned long
25897 get_thumb32_insn (char * buf)
25898 {
25899   unsigned long insn;
25900   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
25901   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25902
25903   return insn;
25904 }
25905
25906 /* We usually want to set the low bit on the address of thumb function
25907    symbols.  In particular .word foo - . should have the low bit set.
25908    Generic code tries to fold the difference of two symbols to
25909    a constant.  Prevent this and force a relocation when the first symbols
25910    is a thumb function.  */
25911
25912 bfd_boolean
25913 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
25914 {
25915   if (op == O_subtract
25916       && l->X_op == O_symbol
25917       && r->X_op == O_symbol
25918       && THUMB_IS_FUNC (l->X_add_symbol))
25919     {
25920       l->X_op = O_subtract;
25921       l->X_op_symbol = r->X_add_symbol;
25922       l->X_add_number -= r->X_add_number;
25923       return TRUE;
25924     }
25925
25926   /* Process as normal.  */
25927   return FALSE;
25928 }
25929
25930 /* Encode Thumb2 unconditional branches and calls. The encoding
25931    for the 2 are identical for the immediate values.  */
25932
25933 static void
25934 encode_thumb2_b_bl_offset (char * buf, offsetT value)
25935 {
25936 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
25937   offsetT newval;
25938   offsetT newval2;
25939   addressT S, I1, I2, lo, hi;
25940
25941   S = (value >> 24) & 0x01;
25942   I1 = (value >> 23) & 0x01;
25943   I2 = (value >> 22) & 0x01;
25944   hi = (value >> 12) & 0x3ff;
25945   lo = (value >> 1) & 0x7ff;
25946   newval   = md_chars_to_number (buf, THUMB_SIZE);
25947   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25948   newval  |= (S << 10) | hi;
25949   newval2 &=  ~T2I1I2MASK;
25950   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
25951   md_number_to_chars (buf, newval, THUMB_SIZE);
25952   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
25953 }
25954
25955 void
25956 md_apply_fix (fixS *    fixP,
25957                valueT * valP,
25958                segT     seg)
25959 {
25960   offsetT        value = * valP;
25961   offsetT        newval;
25962   unsigned int   newimm;
25963   unsigned long  temp;
25964   int            sign;
25965   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
25966
25967   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
25968
25969   /* Note whether this will delete the relocation.  */
25970
25971   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
25972     fixP->fx_done = 1;
25973
25974   /* On a 64-bit host, silently truncate 'value' to 32 bits for
25975      consistency with the behaviour on 32-bit hosts.  Remember value
25976      for emit_reloc.  */
25977   value &= 0xffffffff;
25978   value ^= 0x80000000;
25979   value -= 0x80000000;
25980
25981   *valP = value;
25982   fixP->fx_addnumber = value;
25983
25984   /* Same treatment for fixP->fx_offset.  */
25985   fixP->fx_offset &= 0xffffffff;
25986   fixP->fx_offset ^= 0x80000000;
25987   fixP->fx_offset -= 0x80000000;
25988
25989   switch (fixP->fx_r_type)
25990     {
25991     case BFD_RELOC_NONE:
25992       /* This will need to go in the object file.  */
25993       fixP->fx_done = 0;
25994       break;
25995
25996     case BFD_RELOC_ARM_IMMEDIATE:
25997       /* We claim that this fixup has been processed here,
25998          even if in fact we generate an error because we do
25999          not have a reloc for it, so tc_gen_reloc will reject it.  */
26000       fixP->fx_done = 1;
26001
26002       if (fixP->fx_addsy)
26003         {
26004           const char *msg = 0;
26005
26006           if (! S_IS_DEFINED (fixP->fx_addsy))
26007             msg = _("undefined symbol %s used as an immediate value");
26008           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
26009             msg = _("symbol %s is in a different section");
26010           else if (S_IS_WEAK (fixP->fx_addsy))
26011             msg = _("symbol %s is weak and may be overridden later");
26012
26013           if (msg)
26014             {
26015               as_bad_where (fixP->fx_file, fixP->fx_line,
26016                             msg, S_GET_NAME (fixP->fx_addsy));
26017               break;
26018             }
26019         }
26020
26021       temp = md_chars_to_number (buf, INSN_SIZE);
26022
26023       /* If the offset is negative, we should use encoding A2 for ADR.  */
26024       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
26025         newimm = negate_data_op (&temp, value);
26026       else
26027         {
26028           newimm = encode_arm_immediate (value);
26029
26030           /* If the instruction will fail, see if we can fix things up by
26031              changing the opcode.  */
26032           if (newimm == (unsigned int) FAIL)
26033             newimm = negate_data_op (&temp, value);
26034           /* MOV accepts both ARM modified immediate (A1 encoding) and
26035              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
26036              When disassembling, MOV is preferred when there is no encoding
26037              overlap.  */
26038           if (newimm == (unsigned int) FAIL
26039               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
26040               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
26041               && !((temp >> SBIT_SHIFT) & 0x1)
26042               && value >= 0 && value <= 0xffff)
26043             {
26044               /* Clear bits[23:20] to change encoding from A1 to A2.  */
26045               temp &= 0xff0fffff;
26046               /* Encoding high 4bits imm.  Code below will encode the remaining
26047                  low 12bits.  */
26048               temp |= (value & 0x0000f000) << 4;
26049               newimm = value & 0x00000fff;
26050             }
26051         }
26052
26053       if (newimm == (unsigned int) FAIL)
26054         {
26055           as_bad_where (fixP->fx_file, fixP->fx_line,
26056                         _("invalid constant (%lx) after fixup"),
26057                         (unsigned long) value);
26058           break;
26059         }
26060
26061       newimm |= (temp & 0xfffff000);
26062       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
26063       break;
26064
26065     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
26066       {
26067         unsigned int highpart = 0;
26068         unsigned int newinsn  = 0xe1a00000; /* nop.  */
26069
26070         if (fixP->fx_addsy)
26071           {
26072             const char *msg = 0;
26073
26074             if (! S_IS_DEFINED (fixP->fx_addsy))
26075               msg = _("undefined symbol %s used as an immediate value");
26076             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
26077               msg = _("symbol %s is in a different section");
26078             else if (S_IS_WEAK (fixP->fx_addsy))
26079               msg = _("symbol %s is weak and may be overridden later");
26080
26081             if (msg)
26082               {
26083                 as_bad_where (fixP->fx_file, fixP->fx_line,
26084                               msg, S_GET_NAME (fixP->fx_addsy));
26085                 break;
26086               }
26087           }
26088
26089         newimm = encode_arm_immediate (value);
26090         temp = md_chars_to_number (buf, INSN_SIZE);
26091
26092         /* If the instruction will fail, see if we can fix things up by
26093            changing the opcode.  */
26094         if (newimm == (unsigned int) FAIL
26095             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
26096           {
26097             /* No ?  OK - try using two ADD instructions to generate
26098                the value.  */
26099             newimm = validate_immediate_twopart (value, & highpart);
26100
26101             /* Yes - then make sure that the second instruction is
26102                also an add.  */
26103             if (newimm != (unsigned int) FAIL)
26104               newinsn = temp;
26105             /* Still No ?  Try using a negated value.  */
26106             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
26107               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
26108             /* Otherwise - give up.  */
26109             else
26110               {
26111                 as_bad_where (fixP->fx_file, fixP->fx_line,
26112                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
26113                               (long) value);
26114                 break;
26115               }
26116
26117             /* Replace the first operand in the 2nd instruction (which
26118                is the PC) with the destination register.  We have
26119                already added in the PC in the first instruction and we
26120                do not want to do it again.  */
26121             newinsn &= ~ 0xf0000;
26122             newinsn |= ((newinsn & 0x0f000) << 4);
26123           }
26124
26125         newimm |= (temp & 0xfffff000);
26126         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
26127
26128         highpart |= (newinsn & 0xfffff000);
26129         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
26130       }
26131       break;
26132
26133     case BFD_RELOC_ARM_OFFSET_IMM:
26134       if (!fixP->fx_done && seg->use_rela_p)
26135         value = 0;
26136       /* Fall through.  */
26137
26138     case BFD_RELOC_ARM_LITERAL:
26139       sign = value > 0;
26140
26141       if (value < 0)
26142         value = - value;
26143
26144       if (validate_offset_imm (value, 0) == FAIL)
26145         {
26146           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
26147             as_bad_where (fixP->fx_file, fixP->fx_line,
26148                           _("invalid literal constant: pool needs to be closer"));
26149           else
26150             as_bad_where (fixP->fx_file, fixP->fx_line,
26151                           _("bad immediate value for offset (%ld)"),
26152                           (long) value);
26153           break;
26154         }
26155
26156       newval = md_chars_to_number (buf, INSN_SIZE);
26157       if (value == 0)
26158         newval &= 0xfffff000;
26159       else
26160         {
26161           newval &= 0xff7ff000;
26162           newval |= value | (sign ? INDEX_UP : 0);
26163         }
26164       md_number_to_chars (buf, newval, INSN_SIZE);
26165       break;
26166
26167     case BFD_RELOC_ARM_OFFSET_IMM8:
26168     case BFD_RELOC_ARM_HWLITERAL:
26169       sign = value > 0;
26170
26171       if (value < 0)
26172         value = - value;
26173
26174       if (validate_offset_imm (value, 1) == FAIL)
26175         {
26176           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
26177             as_bad_where (fixP->fx_file, fixP->fx_line,
26178                           _("invalid literal constant: pool needs to be closer"));
26179           else
26180             as_bad_where (fixP->fx_file, fixP->fx_line,
26181                           _("bad immediate value for 8-bit offset (%ld)"),
26182                           (long) value);
26183           break;
26184         }
26185
26186       newval = md_chars_to_number (buf, INSN_SIZE);
26187       if (value == 0)
26188         newval &= 0xfffff0f0;
26189       else
26190         {
26191           newval &= 0xff7ff0f0;
26192           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
26193         }
26194       md_number_to_chars (buf, newval, INSN_SIZE);
26195       break;
26196
26197     case BFD_RELOC_ARM_T32_OFFSET_U8:
26198       if (value < 0 || value > 1020 || value % 4 != 0)
26199         as_bad_where (fixP->fx_file, fixP->fx_line,
26200                       _("bad immediate value for offset (%ld)"), (long) value);
26201       value /= 4;
26202
26203       newval = md_chars_to_number (buf+2, THUMB_SIZE);
26204       newval |= value;
26205       md_number_to_chars (buf+2, newval, THUMB_SIZE);
26206       break;
26207
26208     case BFD_RELOC_ARM_T32_OFFSET_IMM:
26209       /* This is a complicated relocation used for all varieties of Thumb32
26210          load/store instruction with immediate offset:
26211
26212          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
26213                                                    *4, optional writeback(W)
26214                                                    (doubleword load/store)
26215
26216          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
26217          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
26218          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
26219          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
26220          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
26221
26222          Uppercase letters indicate bits that are already encoded at
26223          this point.  Lowercase letters are our problem.  For the
26224          second block of instructions, the secondary opcode nybble
26225          (bits 8..11) is present, and bit 23 is zero, even if this is
26226          a PC-relative operation.  */
26227       newval = md_chars_to_number (buf, THUMB_SIZE);
26228       newval <<= 16;
26229       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
26230
26231       if ((newval & 0xf0000000) == 0xe0000000)
26232         {
26233           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
26234           if (value >= 0)
26235             newval |= (1 << 23);
26236           else
26237             value = -value;
26238           if (value % 4 != 0)
26239             {
26240               as_bad_where (fixP->fx_file, fixP->fx_line,
26241                             _("offset not a multiple of 4"));
26242               break;
26243             }
26244           value /= 4;
26245           if (value > 0xff)
26246             {
26247               as_bad_where (fixP->fx_file, fixP->fx_line,
26248                             _("offset out of range"));
26249               break;
26250             }
26251           newval &= ~0xff;
26252         }
26253       else if ((newval & 0x000f0000) == 0x000f0000)
26254         {
26255           /* PC-relative, 12-bit offset.  */
26256           if (value >= 0)
26257             newval |= (1 << 23);
26258           else
26259             value = -value;
26260           if (value > 0xfff)
26261             {
26262               as_bad_where (fixP->fx_file, fixP->fx_line,
26263                             _("offset out of range"));
26264               break;
26265             }
26266           newval &= ~0xfff;
26267         }
26268       else if ((newval & 0x00000100) == 0x00000100)
26269         {
26270           /* Writeback: 8-bit, +/- offset.  */
26271           if (value >= 0)
26272             newval |= (1 << 9);
26273           else
26274             value = -value;
26275           if (value > 0xff)
26276             {
26277               as_bad_where (fixP->fx_file, fixP->fx_line,
26278                             _("offset out of range"));
26279               break;
26280             }
26281           newval &= ~0xff;
26282         }
26283       else if ((newval & 0x00000f00) == 0x00000e00)
26284         {
26285           /* T-instruction: positive 8-bit offset.  */
26286           if (value < 0 || value > 0xff)
26287             {
26288               as_bad_where (fixP->fx_file, fixP->fx_line,
26289                             _("offset out of range"));
26290               break;
26291             }
26292           newval &= ~0xff;
26293           newval |= value;
26294         }
26295       else
26296         {
26297           /* Positive 12-bit or negative 8-bit offset.  */
26298           int limit;
26299           if (value >= 0)
26300             {
26301               newval |= (1 << 23);
26302               limit = 0xfff;
26303             }
26304           else
26305             {
26306               value = -value;
26307               limit = 0xff;
26308             }
26309           if (value > limit)
26310             {
26311               as_bad_where (fixP->fx_file, fixP->fx_line,
26312                             _("offset out of range"));
26313               break;
26314             }
26315           newval &= ~limit;
26316         }
26317
26318       newval |= value;
26319       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
26320       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
26321       break;
26322
26323     case BFD_RELOC_ARM_SHIFT_IMM:
26324       newval = md_chars_to_number (buf, INSN_SIZE);
26325       if (((unsigned long) value) > 32
26326           || (value == 32
26327               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
26328         {
26329           as_bad_where (fixP->fx_file, fixP->fx_line,
26330                         _("shift expression is too large"));
26331           break;
26332         }
26333
26334       if (value == 0)
26335         /* Shifts of zero must be done as lsl.  */
26336         newval &= ~0x60;
26337       else if (value == 32)
26338         value = 0;
26339       newval &= 0xfffff07f;
26340       newval |= (value & 0x1f) << 7;
26341       md_number_to_chars (buf, newval, INSN_SIZE);
26342       break;
26343
26344     case BFD_RELOC_ARM_T32_IMMEDIATE:
26345     case BFD_RELOC_ARM_T32_ADD_IMM:
26346     case BFD_RELOC_ARM_T32_IMM12:
26347     case BFD_RELOC_ARM_T32_ADD_PC12:
26348       /* We claim that this fixup has been processed here,
26349          even if in fact we generate an error because we do
26350          not have a reloc for it, so tc_gen_reloc will reject it.  */
26351       fixP->fx_done = 1;
26352
26353       if (fixP->fx_addsy
26354           && ! S_IS_DEFINED (fixP->fx_addsy))
26355         {
26356           as_bad_where (fixP->fx_file, fixP->fx_line,
26357                         _("undefined symbol %s used as an immediate value"),
26358                         S_GET_NAME (fixP->fx_addsy));
26359           break;
26360         }
26361
26362       newval = md_chars_to_number (buf, THUMB_SIZE);
26363       newval <<= 16;
26364       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
26365
26366       newimm = FAIL;
26367       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
26368            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
26369               Thumb2 modified immediate encoding (T2).  */
26370            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
26371           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
26372         {
26373           newimm = encode_thumb32_immediate (value);
26374           if (newimm == (unsigned int) FAIL)
26375             newimm = thumb32_negate_data_op (&newval, value);
26376         }
26377       if (newimm == (unsigned int) FAIL)
26378         {
26379           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
26380             {
26381               /* Turn add/sum into addw/subw.  */
26382               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
26383                 newval = (newval & 0xfeffffff) | 0x02000000;
26384               /* No flat 12-bit imm encoding for addsw/subsw.  */
26385               if ((newval & 0x00100000) == 0)
26386                 {
26387                   /* 12 bit immediate for addw/subw.  */
26388                   if (value < 0)
26389                     {
26390                       value = -value;
26391                       newval ^= 0x00a00000;
26392                     }
26393                   if (value > 0xfff)
26394                     newimm = (unsigned int) FAIL;
26395                   else
26396                     newimm = value;
26397                 }
26398             }
26399           else
26400             {
26401               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
26402                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
26403                  disassembling, MOV is preferred when there is no encoding
26404                  overlap.  */
26405               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
26406                   /* NOTE: MOV uses the ORR opcode in Thumb 2 mode
26407                      but with the Rn field [19:16] set to 1111.  */
26408                   && (((newval >> 16) & 0xf) == 0xf)
26409                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
26410                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
26411                   && value >= 0 && value <= 0xffff)
26412                 {
26413                   /* Toggle bit[25] to change encoding from T2 to T3.  */
26414                   newval ^= 1 << 25;
26415                   /* Clear bits[19:16].  */
26416                   newval &= 0xfff0ffff;
26417                   /* Encoding high 4bits imm.  Code below will encode the
26418                      remaining low 12bits.  */
26419                   newval |= (value & 0x0000f000) << 4;
26420                   newimm = value & 0x00000fff;
26421                 }
26422             }
26423         }
26424
26425       if (newimm == (unsigned int)FAIL)
26426         {
26427           as_bad_where (fixP->fx_file, fixP->fx_line,
26428                         _("invalid constant (%lx) after fixup"),
26429                         (unsigned long) value);
26430           break;
26431         }
26432
26433       newval |= (newimm & 0x800) << 15;
26434       newval |= (newimm & 0x700) << 4;
26435       newval |= (newimm & 0x0ff);
26436
26437       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
26438       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
26439       break;
26440
26441     case BFD_RELOC_ARM_SMC:
26442       if (((unsigned long) value) > 0xffff)
26443         as_bad_where (fixP->fx_file, fixP->fx_line,
26444                       _("invalid smc expression"));
26445       newval = md_chars_to_number (buf, INSN_SIZE);
26446       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
26447       md_number_to_chars (buf, newval, INSN_SIZE);
26448       break;
26449
26450     case BFD_RELOC_ARM_HVC:
26451       if (((unsigned long) value) > 0xffff)
26452         as_bad_where (fixP->fx_file, fixP->fx_line,
26453                       _("invalid hvc expression"));
26454       newval = md_chars_to_number (buf, INSN_SIZE);
26455       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
26456       md_number_to_chars (buf, newval, INSN_SIZE);
26457       break;
26458
26459     case BFD_RELOC_ARM_SWI:
26460       if (fixP->tc_fix_data != 0)
26461         {
26462           if (((unsigned long) value) > 0xff)
26463             as_bad_where (fixP->fx_file, fixP->fx_line,
26464                           _("invalid swi expression"));
26465           newval = md_chars_to_number (buf, THUMB_SIZE);
26466           newval |= value;
26467           md_number_to_chars (buf, newval, THUMB_SIZE);
26468         }
26469       else
26470         {
26471           if (((unsigned long) value) > 0x00ffffff)
26472             as_bad_where (fixP->fx_file, fixP->fx_line,
26473                           _("invalid swi expression"));
26474           newval = md_chars_to_number (buf, INSN_SIZE);
26475           newval |= value;
26476           md_number_to_chars (buf, newval, INSN_SIZE);
26477         }
26478       break;
26479
26480     case BFD_RELOC_ARM_MULTI:
26481       if (((unsigned long) value) > 0xffff)
26482         as_bad_where (fixP->fx_file, fixP->fx_line,
26483                       _("invalid expression in load/store multiple"));
26484       newval = value | md_chars_to_number (buf, INSN_SIZE);
26485       md_number_to_chars (buf, newval, INSN_SIZE);
26486       break;
26487
26488 #ifdef OBJ_ELF
26489     case BFD_RELOC_ARM_PCREL_CALL:
26490
26491       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
26492           && fixP->fx_addsy
26493           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26494           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26495           && THUMB_IS_FUNC (fixP->fx_addsy))
26496         /* Flip the bl to blx. This is a simple flip
26497            bit here because we generate PCREL_CALL for
26498            unconditional bls.  */
26499         {
26500           newval = md_chars_to_number (buf, INSN_SIZE);
26501           newval = newval | 0x10000000;
26502           md_number_to_chars (buf, newval, INSN_SIZE);
26503           temp = 1;
26504           fixP->fx_done = 1;
26505         }
26506       else
26507         temp = 3;
26508       goto arm_branch_common;
26509
26510     case BFD_RELOC_ARM_PCREL_JUMP:
26511       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
26512           && fixP->fx_addsy
26513           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26514           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26515           && THUMB_IS_FUNC (fixP->fx_addsy))
26516         {
26517           /* This would map to a bl<cond>, b<cond>,
26518              b<always> to a Thumb function. We
26519              need to force a relocation for this particular
26520              case.  */
26521           newval = md_chars_to_number (buf, INSN_SIZE);
26522           fixP->fx_done = 0;
26523         }
26524       /* Fall through.  */
26525
26526     case BFD_RELOC_ARM_PLT32:
26527 #endif
26528     case BFD_RELOC_ARM_PCREL_BRANCH:
26529       temp = 3;
26530       goto arm_branch_common;
26531
26532     case BFD_RELOC_ARM_PCREL_BLX:
26533
26534       temp = 1;
26535       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
26536           && fixP->fx_addsy
26537           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26538           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26539           && ARM_IS_FUNC (fixP->fx_addsy))
26540         {
26541           /* Flip the blx to a bl and warn.  */
26542           const char *name = S_GET_NAME (fixP->fx_addsy);
26543           newval = 0xeb000000;
26544           as_warn_where (fixP->fx_file, fixP->fx_line,
26545                          _("blx to '%s' an ARM ISA state function changed to bl"),
26546                           name);
26547           md_number_to_chars (buf, newval, INSN_SIZE);
26548           temp = 3;
26549           fixP->fx_done = 1;
26550         }
26551
26552 #ifdef OBJ_ELF
26553        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
26554          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
26555 #endif
26556
26557     arm_branch_common:
26558       /* We are going to store value (shifted right by two) in the
26559          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
26560          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
26561          also be clear.  */
26562       if (value & temp)
26563         as_bad_where (fixP->fx_file, fixP->fx_line,
26564                       _("misaligned branch destination"));
26565       if ((value & (offsetT)0xfe000000) != (offsetT)0
26566           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
26567         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
26568
26569       if (fixP->fx_done || !seg->use_rela_p)
26570         {
26571           newval = md_chars_to_number (buf, INSN_SIZE);
26572           newval |= (value >> 2) & 0x00ffffff;
26573           /* Set the H bit on BLX instructions.  */
26574           if (temp == 1)
26575             {
26576               if (value & 2)
26577                 newval |= 0x01000000;
26578               else
26579                 newval &= ~0x01000000;
26580             }
26581           md_number_to_chars (buf, newval, INSN_SIZE);
26582         }
26583       break;
26584
26585     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
26586       /* CBZ can only branch forward.  */
26587
26588       /* Attempts to use CBZ to branch to the next instruction
26589          (which, strictly speaking, are prohibited) will be turned into
26590          no-ops.
26591
26592          FIXME: It may be better to remove the instruction completely and
26593          perform relaxation.  */
26594       if (value == -2)
26595         {
26596           newval = md_chars_to_number (buf, THUMB_SIZE);
26597           newval = 0xbf00; /* NOP encoding T1 */
26598           md_number_to_chars (buf, newval, THUMB_SIZE);
26599         }
26600       else
26601         {
26602           if (value & ~0x7e)
26603             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
26604
26605           if (fixP->fx_done || !seg->use_rela_p)
26606             {
26607               newval = md_chars_to_number (buf, THUMB_SIZE);
26608               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
26609               md_number_to_chars (buf, newval, THUMB_SIZE);
26610             }
26611         }
26612       break;
26613
26614     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
26615       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
26616         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
26617
26618       if (fixP->fx_done || !seg->use_rela_p)
26619         {
26620           newval = md_chars_to_number (buf, THUMB_SIZE);
26621           newval |= (value & 0x1ff) >> 1;
26622           md_number_to_chars (buf, newval, THUMB_SIZE);
26623         }
26624       break;
26625
26626     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
26627       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
26628         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
26629
26630       if (fixP->fx_done || !seg->use_rela_p)
26631         {
26632           newval = md_chars_to_number (buf, THUMB_SIZE);
26633           newval |= (value & 0xfff) >> 1;
26634           md_number_to_chars (buf, newval, THUMB_SIZE);
26635         }
26636       break;
26637
26638     case BFD_RELOC_THUMB_PCREL_BRANCH20:
26639       if (fixP->fx_addsy
26640           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26641           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26642           && ARM_IS_FUNC (fixP->fx_addsy)
26643           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26644         {
26645           /* Force a relocation for a branch 20 bits wide.  */
26646           fixP->fx_done = 0;
26647         }
26648       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
26649         as_bad_where (fixP->fx_file, fixP->fx_line,
26650                       _("conditional branch out of range"));
26651
26652       if (fixP->fx_done || !seg->use_rela_p)
26653         {
26654           offsetT newval2;
26655           addressT S, J1, J2, lo, hi;
26656
26657           S  = (value & 0x00100000) >> 20;
26658           J2 = (value & 0x00080000) >> 19;
26659           J1 = (value & 0x00040000) >> 18;
26660           hi = (value & 0x0003f000) >> 12;
26661           lo = (value & 0x00000ffe) >> 1;
26662
26663           newval   = md_chars_to_number (buf, THUMB_SIZE);
26664           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
26665           newval  |= (S << 10) | hi;
26666           newval2 |= (J1 << 13) | (J2 << 11) | lo;
26667           md_number_to_chars (buf, newval, THUMB_SIZE);
26668           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
26669         }
26670       break;
26671
26672     case BFD_RELOC_THUMB_PCREL_BLX:
26673       /* If there is a blx from a thumb state function to
26674          another thumb function flip this to a bl and warn
26675          about it.  */
26676
26677       if (fixP->fx_addsy
26678           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26679           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26680           && THUMB_IS_FUNC (fixP->fx_addsy))
26681         {
26682           const char *name = S_GET_NAME (fixP->fx_addsy);
26683           as_warn_where (fixP->fx_file, fixP->fx_line,
26684                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
26685                          name);
26686           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
26687           newval = newval | 0x1000;
26688           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
26689           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
26690           fixP->fx_done = 1;
26691         }
26692
26693
26694       goto thumb_bl_common;
26695
26696     case BFD_RELOC_THUMB_PCREL_BRANCH23:
26697       /* A bl from Thumb state ISA to an internal ARM state function
26698          is converted to a blx.  */
26699       if (fixP->fx_addsy
26700           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
26701           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
26702           && ARM_IS_FUNC (fixP->fx_addsy)
26703           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
26704         {
26705           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
26706           newval = newval & ~0x1000;
26707           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
26708           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
26709           fixP->fx_done = 1;
26710         }
26711
26712     thumb_bl_common:
26713
26714       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
26715         /* For a BLX instruction, make sure that the relocation is rounded up
26716            to a word boundary.  This follows the semantics of the instruction
26717            which specifies that bit 1 of the target address will come from bit
26718            1 of the base address.  */
26719         value = (value + 3) & ~ 3;
26720
26721 #ifdef OBJ_ELF
26722        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
26723            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
26724          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
26725 #endif
26726
26727       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
26728         {
26729           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
26730             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
26731           else if ((value & ~0x1ffffff)
26732                    && ((value & ~0x1ffffff) != ~0x1ffffff))
26733             as_bad_where (fixP->fx_file, fixP->fx_line,
26734                           _("Thumb2 branch out of range"));
26735         }
26736
26737       if (fixP->fx_done || !seg->use_rela_p)
26738         encode_thumb2_b_bl_offset (buf, value);
26739
26740       break;
26741
26742     case BFD_RELOC_THUMB_PCREL_BRANCH25:
26743       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
26744         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
26745
26746       if (fixP->fx_done || !seg->use_rela_p)
26747           encode_thumb2_b_bl_offset (buf, value);
26748
26749       break;
26750
26751     case BFD_RELOC_8:
26752       if (fixP->fx_done || !seg->use_rela_p)
26753         *buf = value;
26754       break;
26755
26756     case BFD_RELOC_16:
26757       if (fixP->fx_done || !seg->use_rela_p)
26758         md_number_to_chars (buf, value, 2);
26759       break;
26760
26761 #ifdef OBJ_ELF
26762     case BFD_RELOC_ARM_TLS_CALL:
26763     case BFD_RELOC_ARM_THM_TLS_CALL:
26764     case BFD_RELOC_ARM_TLS_DESCSEQ:
26765     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
26766     case BFD_RELOC_ARM_TLS_GOTDESC:
26767     case BFD_RELOC_ARM_TLS_GD32:
26768     case BFD_RELOC_ARM_TLS_LE32:
26769     case BFD_RELOC_ARM_TLS_IE32:
26770     case BFD_RELOC_ARM_TLS_LDM32:
26771     case BFD_RELOC_ARM_TLS_LDO32:
26772       S_SET_THREAD_LOCAL (fixP->fx_addsy);
26773       break;
26774
26775       /* Same handling as above, but with the arm_fdpic guard.  */
26776     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
26777     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
26778     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
26779       if (arm_fdpic)
26780         {
26781           S_SET_THREAD_LOCAL (fixP->fx_addsy);
26782         }
26783       else
26784         {
26785           as_bad_where (fixP->fx_file, fixP->fx_line,
26786                         _("Relocation supported only in FDPIC mode"));
26787         }
26788       break;
26789
26790     case BFD_RELOC_ARM_GOT32:
26791     case BFD_RELOC_ARM_GOTOFF:
26792       break;
26793
26794     case BFD_RELOC_ARM_GOT_PREL:
26795       if (fixP->fx_done || !seg->use_rela_p)
26796         md_number_to_chars (buf, value, 4);
26797       break;
26798
26799     case BFD_RELOC_ARM_TARGET2:
26800       /* TARGET2 is not partial-inplace, so we need to write the
26801          addend here for REL targets, because it won't be written out
26802          during reloc processing later.  */
26803       if (fixP->fx_done || !seg->use_rela_p)
26804         md_number_to_chars (buf, fixP->fx_offset, 4);
26805       break;
26806
26807       /* Relocations for FDPIC.  */
26808     case BFD_RELOC_ARM_GOTFUNCDESC:
26809     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
26810     case BFD_RELOC_ARM_FUNCDESC:
26811       if (arm_fdpic)
26812         {
26813           if (fixP->fx_done || !seg->use_rela_p)
26814             md_number_to_chars (buf, 0, 4);
26815         }
26816       else
26817         {
26818           as_bad_where (fixP->fx_file, fixP->fx_line,
26819                         _("Relocation supported only in FDPIC mode"));
26820       }
26821       break;
26822 #endif
26823
26824     case BFD_RELOC_RVA:
26825     case BFD_RELOC_32:
26826     case BFD_RELOC_ARM_TARGET1:
26827     case BFD_RELOC_ARM_ROSEGREL32:
26828     case BFD_RELOC_ARM_SBREL32:
26829     case BFD_RELOC_32_PCREL:
26830 #ifdef TE_PE
26831     case BFD_RELOC_32_SECREL:
26832 #endif
26833       if (fixP->fx_done || !seg->use_rela_p)
26834 #ifdef TE_WINCE
26835         /* For WinCE we only do this for pcrel fixups.  */
26836         if (fixP->fx_done || fixP->fx_pcrel)
26837 #endif
26838           md_number_to_chars (buf, value, 4);
26839       break;
26840
26841 #ifdef OBJ_ELF
26842     case BFD_RELOC_ARM_PREL31:
26843       if (fixP->fx_done || !seg->use_rela_p)
26844         {
26845           newval = md_chars_to_number (buf, 4) & 0x80000000;
26846           if ((value ^ (value >> 1)) & 0x40000000)
26847             {
26848               as_bad_where (fixP->fx_file, fixP->fx_line,
26849                             _("rel31 relocation overflow"));
26850             }
26851           newval |= value & 0x7fffffff;
26852           md_number_to_chars (buf, newval, 4);
26853         }
26854       break;
26855 #endif
26856
26857     case BFD_RELOC_ARM_CP_OFF_IMM:
26858     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
26859     case BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM:
26860       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
26861         newval = md_chars_to_number (buf, INSN_SIZE);
26862       else
26863         newval = get_thumb32_insn (buf);
26864       if ((newval & 0x0f200f00) == 0x0d000900)
26865         {
26866           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
26867              has permitted values that are multiples of 2, in the range 0
26868              to 510.  */
26869           if (value < -510 || value > 510 || (value & 1))
26870             as_bad_where (fixP->fx_file, fixP->fx_line,
26871                           _("co-processor offset out of range"));
26872         }
26873       else if ((newval & 0xfe001f80) == 0xec000f80)
26874         {
26875           if (value < -511 || value > 512 || (value & 3))
26876             as_bad_where (fixP->fx_file, fixP->fx_line,
26877                           _("co-processor offset out of range"));
26878         }
26879       else if (value < -1023 || value > 1023 || (value & 3))
26880         as_bad_where (fixP->fx_file, fixP->fx_line,
26881                       _("co-processor offset out of range"));
26882     cp_off_common:
26883       sign = value > 0;
26884       if (value < 0)
26885         value = -value;
26886       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
26887           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
26888         newval = md_chars_to_number (buf, INSN_SIZE);
26889       else
26890         newval = get_thumb32_insn (buf);
26891       if (value == 0)
26892         {
26893           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
26894             newval &= 0xffffff80;
26895           else
26896             newval &= 0xffffff00;
26897         }
26898       else
26899         {
26900           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
26901             newval &= 0xff7fff80;
26902           else
26903             newval &= 0xff7fff00;
26904           if ((newval & 0x0f200f00) == 0x0d000900)
26905             {
26906               /* This is a fp16 vstr/vldr.
26907
26908                  It requires the immediate offset in the instruction is shifted
26909                  left by 1 to be a half-word offset.
26910
26911                  Here, left shift by 1 first, and later right shift by 2
26912                  should get the right offset.  */
26913               value <<= 1;
26914             }
26915           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
26916         }
26917       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
26918           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
26919         md_number_to_chars (buf, newval, INSN_SIZE);
26920       else
26921         put_thumb32_insn (buf, newval);
26922       break;
26923
26924     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
26925     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
26926       if (value < -255 || value > 255)
26927         as_bad_where (fixP->fx_file, fixP->fx_line,
26928                       _("co-processor offset out of range"));
26929       value *= 4;
26930       goto cp_off_common;
26931
26932     case BFD_RELOC_ARM_THUMB_OFFSET:
26933       newval = md_chars_to_number (buf, THUMB_SIZE);
26934       /* Exactly what ranges, and where the offset is inserted depends
26935          on the type of instruction, we can establish this from the
26936          top 4 bits.  */
26937       switch (newval >> 12)
26938         {
26939         case 4: /* PC load.  */
26940           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
26941              forced to zero for these loads; md_pcrel_from has already
26942              compensated for this.  */
26943           if (value & 3)
26944             as_bad_where (fixP->fx_file, fixP->fx_line,
26945                           _("invalid offset, target not word aligned (0x%08lX)"),
26946                           (((unsigned long) fixP->fx_frag->fr_address
26947                             + (unsigned long) fixP->fx_where) & ~3)
26948                           + (unsigned long) value);
26949
26950           if (value & ~0x3fc)
26951             as_bad_where (fixP->fx_file, fixP->fx_line,
26952                           _("invalid offset, value too big (0x%08lX)"),
26953                           (long) value);
26954
26955           newval |= value >> 2;
26956           break;
26957
26958         case 9: /* SP load/store.  */
26959           if (value & ~0x3fc)
26960             as_bad_where (fixP->fx_file, fixP->fx_line,
26961                           _("invalid offset, value too big (0x%08lX)"),
26962                           (long) value);
26963           newval |= value >> 2;
26964           break;
26965
26966         case 6: /* Word load/store.  */
26967           if (value & ~0x7c)
26968             as_bad_where (fixP->fx_file, fixP->fx_line,
26969                           _("invalid offset, value too big (0x%08lX)"),
26970                           (long) value);
26971           newval |= value << 4; /* 6 - 2.  */
26972           break;
26973
26974         case 7: /* Byte load/store.  */
26975           if (value & ~0x1f)
26976             as_bad_where (fixP->fx_file, fixP->fx_line,
26977                           _("invalid offset, value too big (0x%08lX)"),
26978                           (long) value);
26979           newval |= value << 6;
26980           break;
26981
26982         case 8: /* Halfword load/store.  */
26983           if (value & ~0x3e)
26984             as_bad_where (fixP->fx_file, fixP->fx_line,
26985                           _("invalid offset, value too big (0x%08lX)"),
26986                           (long) value);
26987           newval |= value << 5; /* 6 - 1.  */
26988           break;
26989
26990         default:
26991           as_bad_where (fixP->fx_file, fixP->fx_line,
26992                         "Unable to process relocation for thumb opcode: %lx",
26993                         (unsigned long) newval);
26994           break;
26995         }
26996       md_number_to_chars (buf, newval, THUMB_SIZE);
26997       break;
26998
26999     case BFD_RELOC_ARM_THUMB_ADD:
27000       /* This is a complicated relocation, since we use it for all of
27001          the following immediate relocations:
27002
27003             3bit ADD/SUB
27004             8bit ADD/SUB
27005             9bit ADD/SUB SP word-aligned
27006            10bit ADD PC/SP word-aligned
27007
27008          The type of instruction being processed is encoded in the
27009          instruction field:
27010
27011            0x8000  SUB
27012            0x00F0  Rd
27013            0x000F  Rs
27014       */
27015       newval = md_chars_to_number (buf, THUMB_SIZE);
27016       {
27017         int rd = (newval >> 4) & 0xf;
27018         int rs = newval & 0xf;
27019         int subtract = !!(newval & 0x8000);
27020
27021         /* Check for HI regs, only very restricted cases allowed:
27022            Adjusting SP, and using PC or SP to get an address.  */
27023         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
27024             || (rs > 7 && rs != REG_SP && rs != REG_PC))
27025           as_bad_where (fixP->fx_file, fixP->fx_line,
27026                         _("invalid Hi register with immediate"));
27027
27028         /* If value is negative, choose the opposite instruction.  */
27029         if (value < 0)
27030           {
27031             value = -value;
27032             subtract = !subtract;
27033             if (value < 0)
27034               as_bad_where (fixP->fx_file, fixP->fx_line,
27035                             _("immediate value out of range"));
27036           }
27037
27038         if (rd == REG_SP)
27039           {
27040             if (value & ~0x1fc)
27041               as_bad_where (fixP->fx_file, fixP->fx_line,
27042                             _("invalid immediate for stack address calculation"));
27043             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
27044             newval |= value >> 2;
27045           }
27046         else if (rs == REG_PC || rs == REG_SP)
27047           {
27048             /* PR gas/18541.  If the addition is for a defined symbol
27049                within range of an ADR instruction then accept it.  */
27050             if (subtract
27051                 && value == 4
27052                 && fixP->fx_addsy != NULL)
27053               {
27054                 subtract = 0;
27055
27056                 if (! S_IS_DEFINED (fixP->fx_addsy)
27057                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
27058                     || S_IS_WEAK (fixP->fx_addsy))
27059                   {
27060                     as_bad_where (fixP->fx_file, fixP->fx_line,
27061                                   _("address calculation needs a strongly defined nearby symbol"));
27062                   }
27063                 else
27064                   {
27065                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
27066
27067                     /* Round up to the next 4-byte boundary.  */
27068                     if (v & 3)
27069                       v = (v + 3) & ~ 3;
27070                     else
27071                       v += 4;
27072                     v = S_GET_VALUE (fixP->fx_addsy) - v;
27073
27074                     if (v & ~0x3fc)
27075                       {
27076                         as_bad_where (fixP->fx_file, fixP->fx_line,
27077                                       _("symbol too far away"));
27078                       }
27079                     else
27080                       {
27081                         fixP->fx_done = 1;
27082                         value = v;
27083                       }
27084                   }
27085               }
27086
27087             if (subtract || value & ~0x3fc)
27088               as_bad_where (fixP->fx_file, fixP->fx_line,
27089                             _("invalid immediate for address calculation (value = 0x%08lX)"),
27090                             (unsigned long) (subtract ? - value : value));
27091             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
27092             newval |= rd << 8;
27093             newval |= value >> 2;
27094           }
27095         else if (rs == rd)
27096           {
27097             if (value & ~0xff)
27098               as_bad_where (fixP->fx_file, fixP->fx_line,
27099                             _("immediate value out of range"));
27100             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
27101             newval |= (rd << 8) | value;
27102           }
27103         else
27104           {
27105             if (value & ~0x7)
27106               as_bad_where (fixP->fx_file, fixP->fx_line,
27107                             _("immediate value out of range"));
27108             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
27109             newval |= rd | (rs << 3) | (value << 6);
27110           }
27111       }
27112       md_number_to_chars (buf, newval, THUMB_SIZE);
27113       break;
27114
27115     case BFD_RELOC_ARM_THUMB_IMM:
27116       newval = md_chars_to_number (buf, THUMB_SIZE);
27117       if (value < 0 || value > 255)
27118         as_bad_where (fixP->fx_file, fixP->fx_line,
27119                       _("invalid immediate: %ld is out of range"),
27120                       (long) value);
27121       newval |= value;
27122       md_number_to_chars (buf, newval, THUMB_SIZE);
27123       break;
27124
27125     case BFD_RELOC_ARM_THUMB_SHIFT:
27126       /* 5bit shift value (0..32).  LSL cannot take 32.  */
27127       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
27128       temp = newval & 0xf800;
27129       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
27130         as_bad_where (fixP->fx_file, fixP->fx_line,
27131                       _("invalid shift value: %ld"), (long) value);
27132       /* Shifts of zero must be encoded as LSL.  */
27133       if (value == 0)
27134         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
27135       /* Shifts of 32 are encoded as zero.  */
27136       else if (value == 32)
27137         value = 0;
27138       newval |= value << 6;
27139       md_number_to_chars (buf, newval, THUMB_SIZE);
27140       break;
27141
27142     case BFD_RELOC_VTABLE_INHERIT:
27143     case BFD_RELOC_VTABLE_ENTRY:
27144       fixP->fx_done = 0;
27145       return;
27146
27147     case BFD_RELOC_ARM_MOVW:
27148     case BFD_RELOC_ARM_MOVT:
27149     case BFD_RELOC_ARM_THUMB_MOVW:
27150     case BFD_RELOC_ARM_THUMB_MOVT:
27151       if (fixP->fx_done || !seg->use_rela_p)
27152         {
27153           /* REL format relocations are limited to a 16-bit addend.  */
27154           if (!fixP->fx_done)
27155             {
27156               if (value < -0x8000 || value > 0x7fff)
27157                   as_bad_where (fixP->fx_file, fixP->fx_line,
27158                                 _("offset out of range"));
27159             }
27160           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
27161                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
27162             {
27163               value >>= 16;
27164             }
27165
27166           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
27167               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
27168             {
27169               newval = get_thumb32_insn (buf);
27170               newval &= 0xfbf08f00;
27171               newval |= (value & 0xf000) << 4;
27172               newval |= (value & 0x0800) << 15;
27173               newval |= (value & 0x0700) << 4;
27174               newval |= (value & 0x00ff);
27175               put_thumb32_insn (buf, newval);
27176             }
27177           else
27178             {
27179               newval = md_chars_to_number (buf, 4);
27180               newval &= 0xfff0f000;
27181               newval |= value & 0x0fff;
27182               newval |= (value & 0xf000) << 4;
27183               md_number_to_chars (buf, newval, 4);
27184             }
27185         }
27186       return;
27187
27188    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
27189    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
27190    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
27191    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
27192       gas_assert (!fixP->fx_done);
27193       {
27194         bfd_vma insn;
27195         bfd_boolean is_mov;
27196         bfd_vma encoded_addend = value;
27197
27198         /* Check that addend can be encoded in instruction.  */
27199         if (!seg->use_rela_p && (value < 0 || value > 255))
27200           as_bad_where (fixP->fx_file, fixP->fx_line,
27201                         _("the offset 0x%08lX is not representable"),
27202                         (unsigned long) encoded_addend);
27203
27204         /* Extract the instruction.  */
27205         insn = md_chars_to_number (buf, THUMB_SIZE);
27206         is_mov = (insn & 0xf800) == 0x2000;
27207
27208         /* Encode insn.  */
27209         if (is_mov)
27210           {
27211             if (!seg->use_rela_p)
27212               insn |= encoded_addend;
27213           }
27214         else
27215           {
27216             int rd, rs;
27217
27218             /* Extract the instruction.  */
27219              /* Encoding is the following
27220                 0x8000  SUB
27221                 0x00F0  Rd
27222                 0x000F  Rs
27223              */
27224              /* The following conditions must be true :
27225                 - ADD
27226                 - Rd == Rs
27227                 - Rd <= 7
27228              */
27229             rd = (insn >> 4) & 0xf;
27230             rs = insn & 0xf;
27231             if ((insn & 0x8000) || (rd != rs) || rd > 7)
27232               as_bad_where (fixP->fx_file, fixP->fx_line,
27233                         _("Unable to process relocation for thumb opcode: %lx"),
27234                         (unsigned long) insn);
27235
27236             /* Encode as ADD immediate8 thumb 1 code.  */
27237             insn = 0x3000 | (rd << 8);
27238
27239             /* Place the encoded addend into the first 8 bits of the
27240                instruction.  */
27241             if (!seg->use_rela_p)
27242               insn |= encoded_addend;
27243           }
27244
27245         /* Update the instruction.  */
27246         md_number_to_chars (buf, insn, THUMB_SIZE);
27247       }
27248       break;
27249
27250    case BFD_RELOC_ARM_ALU_PC_G0_NC:
27251    case BFD_RELOC_ARM_ALU_PC_G0:
27252    case BFD_RELOC_ARM_ALU_PC_G1_NC:
27253    case BFD_RELOC_ARM_ALU_PC_G1:
27254    case BFD_RELOC_ARM_ALU_PC_G2:
27255    case BFD_RELOC_ARM_ALU_SB_G0_NC:
27256    case BFD_RELOC_ARM_ALU_SB_G0:
27257    case BFD_RELOC_ARM_ALU_SB_G1_NC:
27258    case BFD_RELOC_ARM_ALU_SB_G1:
27259    case BFD_RELOC_ARM_ALU_SB_G2:
27260      gas_assert (!fixP->fx_done);
27261      if (!seg->use_rela_p)
27262        {
27263          bfd_vma insn;
27264          bfd_vma encoded_addend;
27265          bfd_vma addend_abs = llabs (value);
27266
27267          /* Check that the absolute value of the addend can be
27268             expressed as an 8-bit constant plus a rotation.  */
27269          encoded_addend = encode_arm_immediate (addend_abs);
27270          if (encoded_addend == (unsigned int) FAIL)
27271            as_bad_where (fixP->fx_file, fixP->fx_line,
27272                          _("the offset 0x%08lX is not representable"),
27273                          (unsigned long) addend_abs);
27274
27275          /* Extract the instruction.  */
27276          insn = md_chars_to_number (buf, INSN_SIZE);
27277
27278          /* If the addend is positive, use an ADD instruction.
27279             Otherwise use a SUB.  Take care not to destroy the S bit.  */
27280          insn &= 0xff1fffff;
27281          if (value < 0)
27282            insn |= 1 << 22;
27283          else
27284            insn |= 1 << 23;
27285
27286          /* Place the encoded addend into the first 12 bits of the
27287             instruction.  */
27288          insn &= 0xfffff000;
27289          insn |= encoded_addend;
27290
27291          /* Update the instruction.  */
27292          md_number_to_chars (buf, insn, INSN_SIZE);
27293        }
27294      break;
27295
27296     case BFD_RELOC_ARM_LDR_PC_G0:
27297     case BFD_RELOC_ARM_LDR_PC_G1:
27298     case BFD_RELOC_ARM_LDR_PC_G2:
27299     case BFD_RELOC_ARM_LDR_SB_G0:
27300     case BFD_RELOC_ARM_LDR_SB_G1:
27301     case BFD_RELOC_ARM_LDR_SB_G2:
27302       gas_assert (!fixP->fx_done);
27303       if (!seg->use_rela_p)
27304         {
27305           bfd_vma insn;
27306           bfd_vma addend_abs = llabs (value);
27307
27308           /* Check that the absolute value of the addend can be
27309              encoded in 12 bits.  */
27310           if (addend_abs >= 0x1000)
27311             as_bad_where (fixP->fx_file, fixP->fx_line,
27312                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
27313                           (unsigned long) addend_abs);
27314
27315           /* Extract the instruction.  */
27316           insn = md_chars_to_number (buf, INSN_SIZE);
27317
27318           /* If the addend is negative, clear bit 23 of the instruction.
27319              Otherwise set it.  */
27320           if (value < 0)
27321             insn &= ~(1 << 23);
27322           else
27323             insn |= 1 << 23;
27324
27325           /* Place the absolute value of the addend into the first 12 bits
27326              of the instruction.  */
27327           insn &= 0xfffff000;
27328           insn |= addend_abs;
27329
27330           /* Update the instruction.  */
27331           md_number_to_chars (buf, insn, INSN_SIZE);
27332         }
27333       break;
27334
27335     case BFD_RELOC_ARM_LDRS_PC_G0:
27336     case BFD_RELOC_ARM_LDRS_PC_G1:
27337     case BFD_RELOC_ARM_LDRS_PC_G2:
27338     case BFD_RELOC_ARM_LDRS_SB_G0:
27339     case BFD_RELOC_ARM_LDRS_SB_G1:
27340     case BFD_RELOC_ARM_LDRS_SB_G2:
27341       gas_assert (!fixP->fx_done);
27342       if (!seg->use_rela_p)
27343         {
27344           bfd_vma insn;
27345           bfd_vma addend_abs = llabs (value);
27346
27347           /* Check that the absolute value of the addend can be
27348              encoded in 8 bits.  */
27349           if (addend_abs >= 0x100)
27350             as_bad_where (fixP->fx_file, fixP->fx_line,
27351                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
27352                           (unsigned long) addend_abs);
27353
27354           /* Extract the instruction.  */
27355           insn = md_chars_to_number (buf, INSN_SIZE);
27356
27357           /* If the addend is negative, clear bit 23 of the instruction.
27358              Otherwise set it.  */
27359           if (value < 0)
27360             insn &= ~(1 << 23);
27361           else
27362             insn |= 1 << 23;
27363
27364           /* Place the first four bits of the absolute value of the addend
27365              into the first 4 bits of the instruction, and the remaining
27366              four into bits 8 .. 11.  */
27367           insn &= 0xfffff0f0;
27368           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
27369
27370           /* Update the instruction.  */
27371           md_number_to_chars (buf, insn, INSN_SIZE);
27372         }
27373       break;
27374
27375     case BFD_RELOC_ARM_LDC_PC_G0:
27376     case BFD_RELOC_ARM_LDC_PC_G1:
27377     case BFD_RELOC_ARM_LDC_PC_G2:
27378     case BFD_RELOC_ARM_LDC_SB_G0:
27379     case BFD_RELOC_ARM_LDC_SB_G1:
27380     case BFD_RELOC_ARM_LDC_SB_G2:
27381       gas_assert (!fixP->fx_done);
27382       if (!seg->use_rela_p)
27383         {
27384           bfd_vma insn;
27385           bfd_vma addend_abs = llabs (value);
27386
27387           /* Check that the absolute value of the addend is a multiple of
27388              four and, when divided by four, fits in 8 bits.  */
27389           if (addend_abs & 0x3)
27390             as_bad_where (fixP->fx_file, fixP->fx_line,
27391                           _("bad offset 0x%08lX (must be word-aligned)"),
27392                           (unsigned long) addend_abs);
27393
27394           if ((addend_abs >> 2) > 0xff)
27395             as_bad_where (fixP->fx_file, fixP->fx_line,
27396                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
27397                           (unsigned long) addend_abs);
27398
27399           /* Extract the instruction.  */
27400           insn = md_chars_to_number (buf, INSN_SIZE);
27401
27402           /* If the addend is negative, clear bit 23 of the instruction.
27403              Otherwise set it.  */
27404           if (value < 0)
27405             insn &= ~(1 << 23);
27406           else
27407             insn |= 1 << 23;
27408
27409           /* Place the addend (divided by four) into the first eight
27410              bits of the instruction.  */
27411           insn &= 0xfffffff0;
27412           insn |= addend_abs >> 2;
27413
27414           /* Update the instruction.  */
27415           md_number_to_chars (buf, insn, INSN_SIZE);
27416         }
27417       break;
27418
27419     case BFD_RELOC_THUMB_PCREL_BRANCH5:
27420       if (fixP->fx_addsy
27421           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27422           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27423           && ARM_IS_FUNC (fixP->fx_addsy)
27424           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
27425         {
27426           /* Force a relocation for a branch 5 bits wide.  */
27427           fixP->fx_done = 0;
27428         }
27429       if (v8_1_branch_value_check (value, 5, FALSE) == FAIL)
27430         as_bad_where (fixP->fx_file, fixP->fx_line,
27431                       BAD_BRANCH_OFF);
27432
27433       if (fixP->fx_done || !seg->use_rela_p)
27434         {
27435           addressT boff = value >> 1;
27436
27437           newval  = md_chars_to_number (buf, THUMB_SIZE);
27438           newval |= (boff << 7);
27439           md_number_to_chars (buf, newval, THUMB_SIZE);
27440         }
27441       break;
27442
27443     case BFD_RELOC_THUMB_PCREL_BFCSEL:
27444       if (fixP->fx_addsy
27445           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27446           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27447           && ARM_IS_FUNC (fixP->fx_addsy)
27448           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
27449         {
27450           fixP->fx_done = 0;
27451         }
27452       if ((value & ~0x7f) && ((value & ~0x3f) != ~0x3f))
27453         as_bad_where (fixP->fx_file, fixP->fx_line,
27454                       _("branch out of range"));
27455
27456       if (fixP->fx_done || !seg->use_rela_p)
27457         {
27458           newval  = md_chars_to_number (buf, THUMB_SIZE);
27459
27460           addressT boff = ((newval & 0x0780) >> 7) << 1;
27461           addressT diff = value - boff;
27462
27463           if (diff == 4)
27464             {
27465               newval |= 1 << 1; /* T bit.  */
27466             }
27467           else if (diff != 2)
27468             {
27469               as_bad_where (fixP->fx_file, fixP->fx_line,
27470                             _("out of range label-relative fixup value"));
27471             }
27472           md_number_to_chars (buf, newval, THUMB_SIZE);
27473         }
27474       break;
27475
27476     case BFD_RELOC_ARM_THUMB_BF17:
27477       if (fixP->fx_addsy
27478           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27479           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27480           && ARM_IS_FUNC (fixP->fx_addsy)
27481           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
27482         {
27483           /* Force a relocation for a branch 17 bits wide.  */
27484           fixP->fx_done = 0;
27485         }
27486
27487       if (v8_1_branch_value_check (value, 17, TRUE) == FAIL)
27488         as_bad_where (fixP->fx_file, fixP->fx_line,
27489                       BAD_BRANCH_OFF);
27490
27491       if (fixP->fx_done || !seg->use_rela_p)
27492         {
27493           offsetT newval2;
27494           addressT immA, immB, immC;
27495
27496           immA = (value & 0x0001f000) >> 12;
27497           immB = (value & 0x00000ffc) >> 2;
27498           immC = (value & 0x00000002) >> 1;
27499
27500           newval   = md_chars_to_number (buf, THUMB_SIZE);
27501           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27502           newval  |= immA;
27503           newval2 |= (immC << 11) | (immB << 1);
27504           md_number_to_chars (buf, newval, THUMB_SIZE);
27505           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
27506         }
27507       break;
27508
27509     case BFD_RELOC_ARM_THUMB_BF19:
27510       if (fixP->fx_addsy
27511           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27512           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27513           && ARM_IS_FUNC (fixP->fx_addsy)
27514           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
27515         {
27516           /* Force a relocation for a branch 19 bits wide.  */
27517           fixP->fx_done = 0;
27518         }
27519
27520       if (v8_1_branch_value_check (value, 19, TRUE) == FAIL)
27521         as_bad_where (fixP->fx_file, fixP->fx_line,
27522                       BAD_BRANCH_OFF);
27523
27524       if (fixP->fx_done || !seg->use_rela_p)
27525         {
27526           offsetT newval2;
27527           addressT immA, immB, immC;
27528
27529           immA = (value & 0x0007f000) >> 12;
27530           immB = (value & 0x00000ffc) >> 2;
27531           immC = (value & 0x00000002) >> 1;
27532
27533           newval   = md_chars_to_number (buf, THUMB_SIZE);
27534           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27535           newval  |= immA;
27536           newval2 |= (immC << 11) | (immB << 1);
27537           md_number_to_chars (buf, newval, THUMB_SIZE);
27538           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
27539         }
27540       break;
27541
27542     case BFD_RELOC_ARM_THUMB_BF13:
27543       if (fixP->fx_addsy
27544           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27545           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27546           && ARM_IS_FUNC (fixP->fx_addsy)
27547           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
27548         {
27549           /* Force a relocation for a branch 13 bits wide.  */
27550           fixP->fx_done = 0;
27551         }
27552
27553       if (v8_1_branch_value_check (value, 13, TRUE) == FAIL)
27554         as_bad_where (fixP->fx_file, fixP->fx_line,
27555                       BAD_BRANCH_OFF);
27556
27557       if (fixP->fx_done || !seg->use_rela_p)
27558         {
27559           offsetT newval2;
27560           addressT immA, immB, immC;
27561
27562           immA = (value & 0x00001000) >> 12;
27563           immB = (value & 0x00000ffc) >> 2;
27564           immC = (value & 0x00000002) >> 1;
27565
27566           newval   = md_chars_to_number (buf, THUMB_SIZE);
27567           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27568           newval  |= immA;
27569           newval2 |= (immC << 11) | (immB << 1);
27570           md_number_to_chars (buf, newval, THUMB_SIZE);
27571           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
27572         }
27573       break;
27574
27575     case BFD_RELOC_ARM_THUMB_LOOP12:
27576       if (fixP->fx_addsy
27577           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
27578           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
27579           && ARM_IS_FUNC (fixP->fx_addsy)
27580           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
27581         {
27582           /* Force a relocation for a branch 12 bits wide.  */
27583           fixP->fx_done = 0;
27584         }
27585
27586       bfd_vma insn = get_thumb32_insn (buf);
27587       /* le lr, <label> or le <label> */
27588       if (((insn & 0xffffffff) == 0xf00fc001)
27589           || ((insn & 0xffffffff) == 0xf02fc001))
27590         value = -value;
27591
27592       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
27593         as_bad_where (fixP->fx_file, fixP->fx_line,
27594                       BAD_BRANCH_OFF);
27595       if (fixP->fx_done || !seg->use_rela_p)
27596         {
27597           addressT imml, immh;
27598
27599           immh = (value & 0x00000ffc) >> 2;
27600           imml = (value & 0x00000002) >> 1;
27601
27602           newval  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
27603           newval |= (imml << 11) | (immh << 1);
27604           md_number_to_chars (buf + THUMB_SIZE, newval, THUMB_SIZE);
27605         }
27606       break;
27607
27608     case BFD_RELOC_ARM_V4BX:
27609       /* This will need to go in the object file.  */
27610       fixP->fx_done = 0;
27611       break;
27612
27613     case BFD_RELOC_UNUSED:
27614     default:
27615       as_bad_where (fixP->fx_file, fixP->fx_line,
27616                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
27617     }
27618 }
27619
27620 /* Translate internal representation of relocation info to BFD target
27621    format.  */
27622
27623 arelent *
27624 tc_gen_reloc (asection *section, fixS *fixp)
27625 {
27626   arelent * reloc;
27627   bfd_reloc_code_real_type code;
27628
27629   reloc = XNEW (arelent);
27630
27631   reloc->sym_ptr_ptr = XNEW (asymbol *);
27632   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
27633   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
27634
27635   if (fixp->fx_pcrel)
27636     {
27637       if (section->use_rela_p)
27638         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
27639       else
27640         fixp->fx_offset = reloc->address;
27641     }
27642   reloc->addend = fixp->fx_offset;
27643
27644   switch (fixp->fx_r_type)
27645     {
27646     case BFD_RELOC_8:
27647       if (fixp->fx_pcrel)
27648         {
27649           code = BFD_RELOC_8_PCREL;
27650           break;
27651         }
27652       /* Fall through.  */
27653
27654     case BFD_RELOC_16:
27655       if (fixp->fx_pcrel)
27656         {
27657           code = BFD_RELOC_16_PCREL;
27658           break;
27659         }
27660       /* Fall through.  */
27661
27662     case BFD_RELOC_32:
27663       if (fixp->fx_pcrel)
27664         {
27665           code = BFD_RELOC_32_PCREL;
27666           break;
27667         }
27668       /* Fall through.  */
27669
27670     case BFD_RELOC_ARM_MOVW:
27671       if (fixp->fx_pcrel)
27672         {
27673           code = BFD_RELOC_ARM_MOVW_PCREL;
27674           break;
27675         }
27676       /* Fall through.  */
27677
27678     case BFD_RELOC_ARM_MOVT:
27679       if (fixp->fx_pcrel)
27680         {
27681           code = BFD_RELOC_ARM_MOVT_PCREL;
27682           break;
27683         }
27684       /* Fall through.  */
27685
27686     case BFD_RELOC_ARM_THUMB_MOVW:
27687       if (fixp->fx_pcrel)
27688         {
27689           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
27690           break;
27691         }
27692       /* Fall through.  */
27693
27694     case BFD_RELOC_ARM_THUMB_MOVT:
27695       if (fixp->fx_pcrel)
27696         {
27697           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
27698           break;
27699         }
27700       /* Fall through.  */
27701
27702     case BFD_RELOC_NONE:
27703     case BFD_RELOC_ARM_PCREL_BRANCH:
27704     case BFD_RELOC_ARM_PCREL_BLX:
27705     case BFD_RELOC_RVA:
27706     case BFD_RELOC_THUMB_PCREL_BRANCH7:
27707     case BFD_RELOC_THUMB_PCREL_BRANCH9:
27708     case BFD_RELOC_THUMB_PCREL_BRANCH12:
27709     case BFD_RELOC_THUMB_PCREL_BRANCH20:
27710     case BFD_RELOC_THUMB_PCREL_BRANCH23:
27711     case BFD_RELOC_THUMB_PCREL_BRANCH25:
27712     case BFD_RELOC_VTABLE_ENTRY:
27713     case BFD_RELOC_VTABLE_INHERIT:
27714 #ifdef TE_PE
27715     case BFD_RELOC_32_SECREL:
27716 #endif
27717       code = fixp->fx_r_type;
27718       break;
27719
27720     case BFD_RELOC_THUMB_PCREL_BLX:
27721 #ifdef OBJ_ELF
27722       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
27723         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
27724       else
27725 #endif
27726         code = BFD_RELOC_THUMB_PCREL_BLX;
27727       break;
27728
27729     case BFD_RELOC_ARM_LITERAL:
27730     case BFD_RELOC_ARM_HWLITERAL:
27731       /* If this is called then the a literal has
27732          been referenced across a section boundary.  */
27733       as_bad_where (fixp->fx_file, fixp->fx_line,
27734                     _("literal referenced across section boundary"));
27735       return NULL;
27736
27737 #ifdef OBJ_ELF
27738     case BFD_RELOC_ARM_TLS_CALL:
27739     case BFD_RELOC_ARM_THM_TLS_CALL:
27740     case BFD_RELOC_ARM_TLS_DESCSEQ:
27741     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
27742     case BFD_RELOC_ARM_GOT32:
27743     case BFD_RELOC_ARM_GOTOFF:
27744     case BFD_RELOC_ARM_GOT_PREL:
27745     case BFD_RELOC_ARM_PLT32:
27746     case BFD_RELOC_ARM_TARGET1:
27747     case BFD_RELOC_ARM_ROSEGREL32:
27748     case BFD_RELOC_ARM_SBREL32:
27749     case BFD_RELOC_ARM_PREL31:
27750     case BFD_RELOC_ARM_TARGET2:
27751     case BFD_RELOC_ARM_TLS_LDO32:
27752     case BFD_RELOC_ARM_PCREL_CALL:
27753     case BFD_RELOC_ARM_PCREL_JUMP:
27754     case BFD_RELOC_ARM_ALU_PC_G0_NC:
27755     case BFD_RELOC_ARM_ALU_PC_G0:
27756     case BFD_RELOC_ARM_ALU_PC_G1_NC:
27757     case BFD_RELOC_ARM_ALU_PC_G1:
27758     case BFD_RELOC_ARM_ALU_PC_G2:
27759     case BFD_RELOC_ARM_LDR_PC_G0:
27760     case BFD_RELOC_ARM_LDR_PC_G1:
27761     case BFD_RELOC_ARM_LDR_PC_G2:
27762     case BFD_RELOC_ARM_LDRS_PC_G0:
27763     case BFD_RELOC_ARM_LDRS_PC_G1:
27764     case BFD_RELOC_ARM_LDRS_PC_G2:
27765     case BFD_RELOC_ARM_LDC_PC_G0:
27766     case BFD_RELOC_ARM_LDC_PC_G1:
27767     case BFD_RELOC_ARM_LDC_PC_G2:
27768     case BFD_RELOC_ARM_ALU_SB_G0_NC:
27769     case BFD_RELOC_ARM_ALU_SB_G0:
27770     case BFD_RELOC_ARM_ALU_SB_G1_NC:
27771     case BFD_RELOC_ARM_ALU_SB_G1:
27772     case BFD_RELOC_ARM_ALU_SB_G2:
27773     case BFD_RELOC_ARM_LDR_SB_G0:
27774     case BFD_RELOC_ARM_LDR_SB_G1:
27775     case BFD_RELOC_ARM_LDR_SB_G2:
27776     case BFD_RELOC_ARM_LDRS_SB_G0:
27777     case BFD_RELOC_ARM_LDRS_SB_G1:
27778     case BFD_RELOC_ARM_LDRS_SB_G2:
27779     case BFD_RELOC_ARM_LDC_SB_G0:
27780     case BFD_RELOC_ARM_LDC_SB_G1:
27781     case BFD_RELOC_ARM_LDC_SB_G2:
27782     case BFD_RELOC_ARM_V4BX:
27783     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
27784     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
27785     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
27786     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
27787     case BFD_RELOC_ARM_GOTFUNCDESC:
27788     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
27789     case BFD_RELOC_ARM_FUNCDESC:
27790     case BFD_RELOC_ARM_THUMB_BF17:
27791     case BFD_RELOC_ARM_THUMB_BF19:
27792     case BFD_RELOC_ARM_THUMB_BF13:
27793       code = fixp->fx_r_type;
27794       break;
27795
27796     case BFD_RELOC_ARM_TLS_GOTDESC:
27797     case BFD_RELOC_ARM_TLS_GD32:
27798     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
27799     case BFD_RELOC_ARM_TLS_LE32:
27800     case BFD_RELOC_ARM_TLS_IE32:
27801     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
27802     case BFD_RELOC_ARM_TLS_LDM32:
27803     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
27804       /* BFD will include the symbol's address in the addend.
27805          But we don't want that, so subtract it out again here.  */
27806       if (!S_IS_COMMON (fixp->fx_addsy))
27807         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
27808       code = fixp->fx_r_type;
27809       break;
27810 #endif
27811
27812     case BFD_RELOC_ARM_IMMEDIATE:
27813       as_bad_where (fixp->fx_file, fixp->fx_line,
27814                     _("internal relocation (type: IMMEDIATE) not fixed up"));
27815       return NULL;
27816
27817     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
27818       as_bad_where (fixp->fx_file, fixp->fx_line,
27819                     _("ADRL used for a symbol not defined in the same file"));
27820       return NULL;
27821
27822     case BFD_RELOC_THUMB_PCREL_BRANCH5:
27823     case BFD_RELOC_THUMB_PCREL_BFCSEL:
27824     case BFD_RELOC_ARM_THUMB_LOOP12:
27825       as_bad_where (fixp->fx_file, fixp->fx_line,
27826                     _("%s used for a symbol not defined in the same file"),
27827                     bfd_get_reloc_code_name (fixp->fx_r_type));
27828       return NULL;
27829
27830     case BFD_RELOC_ARM_OFFSET_IMM:
27831       if (section->use_rela_p)
27832         {
27833           code = fixp->fx_r_type;
27834           break;
27835         }
27836
27837       if (fixp->fx_addsy != NULL
27838           && !S_IS_DEFINED (fixp->fx_addsy)
27839           && S_IS_LOCAL (fixp->fx_addsy))
27840         {
27841           as_bad_where (fixp->fx_file, fixp->fx_line,
27842                         _("undefined local label `%s'"),
27843                         S_GET_NAME (fixp->fx_addsy));
27844           return NULL;
27845         }
27846
27847       as_bad_where (fixp->fx_file, fixp->fx_line,
27848                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
27849       return NULL;
27850
27851     default:
27852       {
27853         const char * type;
27854
27855         switch (fixp->fx_r_type)
27856           {
27857           case BFD_RELOC_NONE:             type = "NONE";         break;
27858           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
27859           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
27860           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
27861           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
27862           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
27863           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
27864           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
27865           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
27866           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
27867           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
27868           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
27869           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
27870           default:                         type = _("<unknown>"); break;
27871           }
27872         as_bad_where (fixp->fx_file, fixp->fx_line,
27873                       _("cannot represent %s relocation in this object file format"),
27874                       type);
27875         return NULL;
27876       }
27877     }
27878
27879 #ifdef OBJ_ELF
27880   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
27881       && GOT_symbol
27882       && fixp->fx_addsy == GOT_symbol)
27883     {
27884       code = BFD_RELOC_ARM_GOTPC;
27885       reloc->addend = fixp->fx_offset = reloc->address;
27886     }
27887 #endif
27888
27889   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
27890
27891   if (reloc->howto == NULL)
27892     {
27893       as_bad_where (fixp->fx_file, fixp->fx_line,
27894                     _("cannot represent %s relocation in this object file format"),
27895                     bfd_get_reloc_code_name (code));
27896       return NULL;
27897     }
27898
27899   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
27900      vtable entry to be used in the relocation's section offset.  */
27901   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
27902     reloc->address = fixp->fx_offset;
27903
27904   return reloc;
27905 }
27906
27907 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
27908
27909 void
27910 cons_fix_new_arm (fragS *       frag,
27911                   int           where,
27912                   int           size,
27913                   expressionS * exp,
27914                   bfd_reloc_code_real_type reloc)
27915 {
27916   int pcrel = 0;
27917
27918   /* Pick a reloc.
27919      FIXME: @@ Should look at CPU word size.  */
27920   switch (size)
27921     {
27922     case 1:
27923       reloc = BFD_RELOC_8;
27924       break;
27925     case 2:
27926       reloc = BFD_RELOC_16;
27927       break;
27928     case 4:
27929     default:
27930       reloc = BFD_RELOC_32;
27931       break;
27932     case 8:
27933       reloc = BFD_RELOC_64;
27934       break;
27935     }
27936
27937 #ifdef TE_PE
27938   if (exp->X_op == O_secrel)
27939   {
27940     exp->X_op = O_symbol;
27941     reloc = BFD_RELOC_32_SECREL;
27942   }
27943 #endif
27944
27945   fix_new_exp (frag, where, size, exp, pcrel, reloc);
27946 }
27947
27948 #if defined (OBJ_COFF)
27949 void
27950 arm_validate_fix (fixS * fixP)
27951 {
27952   /* If the destination of the branch is a defined symbol which does not have
27953      the THUMB_FUNC attribute, then we must be calling a function which has
27954      the (interfacearm) attribute.  We look for the Thumb entry point to that
27955      function and change the branch to refer to that function instead.  */
27956   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
27957       && fixP->fx_addsy != NULL
27958       && S_IS_DEFINED (fixP->fx_addsy)
27959       && ! THUMB_IS_FUNC (fixP->fx_addsy))
27960     {
27961       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
27962     }
27963 }
27964 #endif
27965
27966
27967 int
27968 arm_force_relocation (struct fix * fixp)
27969 {
27970 #if defined (OBJ_COFF) && defined (TE_PE)
27971   if (fixp->fx_r_type == BFD_RELOC_RVA)
27972     return 1;
27973 #endif
27974
27975   /* In case we have a call or a branch to a function in ARM ISA mode from
27976      a thumb function or vice-versa force the relocation. These relocations
27977      are cleared off for some cores that might have blx and simple transformations
27978      are possible.  */
27979
27980 #ifdef OBJ_ELF
27981   switch (fixp->fx_r_type)
27982     {
27983     case BFD_RELOC_ARM_PCREL_JUMP:
27984     case BFD_RELOC_ARM_PCREL_CALL:
27985     case BFD_RELOC_THUMB_PCREL_BLX:
27986       if (THUMB_IS_FUNC (fixp->fx_addsy))
27987         return 1;
27988       break;
27989
27990     case BFD_RELOC_ARM_PCREL_BLX:
27991     case BFD_RELOC_THUMB_PCREL_BRANCH25:
27992     case BFD_RELOC_THUMB_PCREL_BRANCH20:
27993     case BFD_RELOC_THUMB_PCREL_BRANCH23:
27994       if (ARM_IS_FUNC (fixp->fx_addsy))
27995         return 1;
27996       break;
27997
27998     default:
27999       break;
28000     }
28001 #endif
28002
28003   /* Resolve these relocations even if the symbol is extern or weak.
28004      Technically this is probably wrong due to symbol preemption.
28005      In practice these relocations do not have enough range to be useful
28006      at dynamic link time, and some code (e.g. in the Linux kernel)
28007      expects these references to be resolved.  */
28008   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
28009       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
28010       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
28011       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
28012       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
28013       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
28014       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
28015       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
28016       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
28017       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
28018       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
28019       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
28020       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
28021       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
28022     return 0;
28023
28024   /* Always leave these relocations for the linker.  */
28025   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
28026        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
28027       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
28028     return 1;
28029
28030   /* Always generate relocations against function symbols.  */
28031   if (fixp->fx_r_type == BFD_RELOC_32
28032       && fixp->fx_addsy
28033       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
28034     return 1;
28035
28036   return generic_force_reloc (fixp);
28037 }
28038
28039 #if defined (OBJ_ELF) || defined (OBJ_COFF)
28040 /* Relocations against function names must be left unadjusted,
28041    so that the linker can use this information to generate interworking
28042    stubs.  The MIPS version of this function
28043    also prevents relocations that are mips-16 specific, but I do not
28044    know why it does this.
28045
28046    FIXME:
28047    There is one other problem that ought to be addressed here, but
28048    which currently is not:  Taking the address of a label (rather
28049    than a function) and then later jumping to that address.  Such
28050    addresses also ought to have their bottom bit set (assuming that
28051    they reside in Thumb code), but at the moment they will not.  */
28052
28053 bfd_boolean
28054 arm_fix_adjustable (fixS * fixP)
28055 {
28056   if (fixP->fx_addsy == NULL)
28057     return 1;
28058
28059   /* Preserve relocations against symbols with function type.  */
28060   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
28061     return FALSE;
28062
28063   if (THUMB_IS_FUNC (fixP->fx_addsy)
28064       && fixP->fx_subsy == NULL)
28065     return FALSE;
28066
28067   /* We need the symbol name for the VTABLE entries.  */
28068   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
28069       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
28070     return FALSE;
28071
28072   /* Don't allow symbols to be discarded on GOT related relocs.  */
28073   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
28074       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
28075       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
28076       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
28077       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32_FDPIC
28078       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
28079       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
28080       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32_FDPIC
28081       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
28082       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32_FDPIC
28083       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
28084       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
28085       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
28086       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
28087       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
28088       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
28089       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
28090     return FALSE;
28091
28092   /* Similarly for group relocations.  */
28093   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
28094        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
28095       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
28096     return FALSE;
28097
28098   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
28099   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
28100       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
28101       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
28102       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
28103       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
28104       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
28105       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
28106       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
28107     return FALSE;
28108
28109   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
28110      offsets, so keep these symbols.  */
28111   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
28112       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
28113     return FALSE;
28114
28115   return TRUE;
28116 }
28117 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
28118
28119 #ifdef OBJ_ELF
28120 const char *
28121 elf32_arm_target_format (void)
28122 {
28123 #ifdef TE_SYMBIAN
28124   return (target_big_endian
28125           ? "elf32-bigarm-symbian"
28126           : "elf32-littlearm-symbian");
28127 #elif defined (TE_VXWORKS)
28128   return (target_big_endian
28129           ? "elf32-bigarm-vxworks"
28130           : "elf32-littlearm-vxworks");
28131 #elif defined (TE_NACL)
28132   return (target_big_endian
28133           ? "elf32-bigarm-nacl"
28134           : "elf32-littlearm-nacl");
28135 #else
28136   if (arm_fdpic)
28137     {
28138       if (target_big_endian)
28139         return "elf32-bigarm-fdpic";
28140       else
28141         return "elf32-littlearm-fdpic";
28142     }
28143   else
28144     {
28145       if (target_big_endian)
28146         return "elf32-bigarm";
28147       else
28148         return "elf32-littlearm";
28149     }
28150 #endif
28151 }
28152
28153 void
28154 armelf_frob_symbol (symbolS * symp,
28155                     int *     puntp)
28156 {
28157   elf_frob_symbol (symp, puntp);
28158 }
28159 #endif
28160
28161 /* MD interface: Finalization.  */
28162
28163 void
28164 arm_cleanup (void)
28165 {
28166   literal_pool * pool;
28167
28168   /* Ensure that all the predication blocks are properly closed.  */
28169   check_pred_blocks_finished ();
28170
28171   for (pool = list_of_pools; pool; pool = pool->next)
28172     {
28173       /* Put it at the end of the relevant section.  */
28174       subseg_set (pool->section, pool->sub_section);
28175 #ifdef OBJ_ELF
28176       arm_elf_change_section ();
28177 #endif
28178       s_ltorg (0);
28179     }
28180 }
28181
28182 #ifdef OBJ_ELF
28183 /* Remove any excess mapping symbols generated for alignment frags in
28184    SEC.  We may have created a mapping symbol before a zero byte
28185    alignment; remove it if there's a mapping symbol after the
28186    alignment.  */
28187 static void
28188 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
28189                        void *dummy ATTRIBUTE_UNUSED)
28190 {
28191   segment_info_type *seginfo = seg_info (sec);
28192   fragS *fragp;
28193
28194   if (seginfo == NULL || seginfo->frchainP == NULL)
28195     return;
28196
28197   for (fragp = seginfo->frchainP->frch_root;
28198        fragp != NULL;
28199        fragp = fragp->fr_next)
28200     {
28201       symbolS *sym = fragp->tc_frag_data.last_map;
28202       fragS *next = fragp->fr_next;
28203
28204       /* Variable-sized frags have been converted to fixed size by
28205          this point.  But if this was variable-sized to start with,
28206          there will be a fixed-size frag after it.  So don't handle
28207          next == NULL.  */
28208       if (sym == NULL || next == NULL)
28209         continue;
28210
28211       if (S_GET_VALUE (sym) < next->fr_address)
28212         /* Not at the end of this frag.  */
28213         continue;
28214       know (S_GET_VALUE (sym) == next->fr_address);
28215
28216       do
28217         {
28218           if (next->tc_frag_data.first_map != NULL)
28219             {
28220               /* Next frag starts with a mapping symbol.  Discard this
28221                  one.  */
28222               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
28223               break;
28224             }
28225
28226           if (next->fr_next == NULL)
28227             {
28228               /* This mapping symbol is at the end of the section.  Discard
28229                  it.  */
28230               know (next->fr_fix == 0 && next->fr_var == 0);
28231               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
28232               break;
28233             }
28234
28235           /* As long as we have empty frags without any mapping symbols,
28236              keep looking.  */
28237           /* If the next frag is non-empty and does not start with a
28238              mapping symbol, then this mapping symbol is required.  */
28239           if (next->fr_address != next->fr_next->fr_address)
28240             break;
28241
28242           next = next->fr_next;
28243         }
28244       while (next != NULL);
28245     }
28246 }
28247 #endif
28248
28249 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
28250    ARM ones.  */
28251
28252 void
28253 arm_adjust_symtab (void)
28254 {
28255 #ifdef OBJ_COFF
28256   symbolS * sym;
28257
28258   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
28259     {
28260       if (ARM_IS_THUMB (sym))
28261         {
28262           if (THUMB_IS_FUNC (sym))
28263             {
28264               /* Mark the symbol as a Thumb function.  */
28265               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
28266                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
28267                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
28268
28269               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
28270                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
28271               else
28272                 as_bad (_("%s: unexpected function type: %d"),
28273                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
28274             }
28275           else switch (S_GET_STORAGE_CLASS (sym))
28276             {
28277             case C_EXT:
28278               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
28279               break;
28280             case C_STAT:
28281               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
28282               break;
28283             case C_LABEL:
28284               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
28285               break;
28286             default:
28287               /* Do nothing.  */
28288               break;
28289             }
28290         }
28291
28292       if (ARM_IS_INTERWORK (sym))
28293         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
28294     }
28295 #endif
28296 #ifdef OBJ_ELF
28297   symbolS * sym;
28298   char      bind;
28299
28300   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
28301     {
28302       if (ARM_IS_THUMB (sym))
28303         {
28304           elf_symbol_type * elf_sym;
28305
28306           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
28307           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
28308
28309           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
28310                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
28311             {
28312               /* If it's a .thumb_func, declare it as so,
28313                  otherwise tag label as .code 16.  */
28314               if (THUMB_IS_FUNC (sym))
28315                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
28316                                          ST_BRANCH_TO_THUMB);
28317               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
28318                 elf_sym->internal_elf_sym.st_info =
28319                   ELF_ST_INFO (bind, STT_ARM_16BIT);
28320             }
28321         }
28322     }
28323
28324   /* Remove any overlapping mapping symbols generated by alignment frags.  */
28325   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
28326   /* Now do generic ELF adjustments.  */
28327   elf_adjust_symtab ();
28328 #endif
28329 }
28330
28331 /* MD interface: Initialization.  */
28332
28333 static void
28334 set_constant_flonums (void)
28335 {
28336   int i;
28337
28338   for (i = 0; i < NUM_FLOAT_VALS; i++)
28339     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
28340       abort ();
28341 }
28342
28343 /* Auto-select Thumb mode if it's the only available instruction set for the
28344    given architecture.  */
28345
28346 static void
28347 autoselect_thumb_from_cpu_variant (void)
28348 {
28349   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
28350     opcode_select (16);
28351 }
28352
28353 void
28354 md_begin (void)
28355 {
28356   unsigned mach;
28357   unsigned int i;
28358
28359   if (   (arm_ops_hsh = hash_new ()) == NULL
28360       || (arm_cond_hsh = hash_new ()) == NULL
28361       || (arm_vcond_hsh = hash_new ()) == NULL
28362       || (arm_shift_hsh = hash_new ()) == NULL
28363       || (arm_psr_hsh = hash_new ()) == NULL
28364       || (arm_v7m_psr_hsh = hash_new ()) == NULL
28365       || (arm_reg_hsh = hash_new ()) == NULL
28366       || (arm_reloc_hsh = hash_new ()) == NULL
28367       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
28368     as_fatal (_("virtual memory exhausted"));
28369
28370   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
28371     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
28372   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
28373     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
28374   for (i = 0; i < sizeof (vconds) / sizeof (struct asm_cond); i++)
28375     hash_insert (arm_vcond_hsh, vconds[i].template_name, (void *) (vconds + i));
28376   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
28377     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
28378   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
28379     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
28380   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
28381     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
28382                  (void *) (v7m_psrs + i));
28383   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
28384     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
28385   for (i = 0;
28386        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
28387        i++)
28388     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
28389                  (void *) (barrier_opt_names + i));
28390 #ifdef OBJ_ELF
28391   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
28392     {
28393       struct reloc_entry * entry = reloc_names + i;
28394
28395       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
28396         /* This makes encode_branch() use the EABI versions of this relocation.  */
28397         entry->reloc = BFD_RELOC_UNUSED;
28398
28399       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
28400     }
28401 #endif
28402
28403   set_constant_flonums ();
28404
28405   /* Set the cpu variant based on the command-line options.  We prefer
28406      -mcpu= over -march= if both are set (as for GCC); and we prefer
28407      -mfpu= over any other way of setting the floating point unit.
28408      Use of legacy options with new options are faulted.  */
28409   if (legacy_cpu)
28410     {
28411       if (mcpu_cpu_opt || march_cpu_opt)
28412         as_bad (_("use of old and new-style options to set CPU type"));
28413
28414       selected_arch = *legacy_cpu;
28415     }
28416   else if (mcpu_cpu_opt)
28417     {
28418       selected_arch = *mcpu_cpu_opt;
28419       selected_ext = *mcpu_ext_opt;
28420     }
28421   else if (march_cpu_opt)
28422     {
28423       selected_arch = *march_cpu_opt;
28424       selected_ext = *march_ext_opt;
28425     }
28426   ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
28427
28428   if (legacy_fpu)
28429     {
28430       if (mfpu_opt)
28431         as_bad (_("use of old and new-style options to set FPU type"));
28432
28433       selected_fpu = *legacy_fpu;
28434     }
28435   else if (mfpu_opt)
28436     selected_fpu = *mfpu_opt;
28437   else
28438     {
28439 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
28440         || defined (TE_NetBSD) || defined (TE_VXWORKS))
28441       /* Some environments specify a default FPU.  If they don't, infer it
28442          from the processor.  */
28443       if (mcpu_fpu_opt)
28444         selected_fpu = *mcpu_fpu_opt;
28445       else if (march_fpu_opt)
28446         selected_fpu = *march_fpu_opt;
28447 #else
28448       selected_fpu = fpu_default;
28449 #endif
28450     }
28451
28452   if (ARM_FEATURE_ZERO (selected_fpu))
28453     {
28454       if (!no_cpu_selected ())
28455         selected_fpu = fpu_default;
28456       else
28457         selected_fpu = fpu_arch_fpa;
28458     }
28459
28460 #ifdef CPU_DEFAULT
28461   if (ARM_FEATURE_ZERO (selected_arch))
28462     {
28463       selected_arch = cpu_default;
28464       selected_cpu = selected_arch;
28465     }
28466   ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
28467 #else
28468   /*  Autodection of feature mode: allow all features in cpu_variant but leave
28469       selected_cpu unset.  It will be set in aeabi_set_public_attributes ()
28470       after all instruction have been processed and we can decide what CPU
28471       should be selected.  */
28472   if (ARM_FEATURE_ZERO (selected_arch))
28473     ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
28474   else
28475     ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
28476 #endif
28477
28478   autoselect_thumb_from_cpu_variant ();
28479
28480   arm_arch_used = thumb_arch_used = arm_arch_none;
28481
28482 #if defined OBJ_COFF || defined OBJ_ELF
28483   {
28484     unsigned int flags = 0;
28485
28486 #if defined OBJ_ELF
28487     flags = meabi_flags;
28488
28489     switch (meabi_flags)
28490       {
28491       case EF_ARM_EABI_UNKNOWN:
28492 #endif
28493         /* Set the flags in the private structure.  */
28494         if (uses_apcs_26)      flags |= F_APCS26;
28495         if (support_interwork) flags |= F_INTERWORK;
28496         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
28497         if (pic_code)          flags |= F_PIC;
28498         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
28499           flags |= F_SOFT_FLOAT;
28500
28501         switch (mfloat_abi_opt)
28502           {
28503           case ARM_FLOAT_ABI_SOFT:
28504           case ARM_FLOAT_ABI_SOFTFP:
28505             flags |= F_SOFT_FLOAT;
28506             break;
28507
28508           case ARM_FLOAT_ABI_HARD:
28509             if (flags & F_SOFT_FLOAT)
28510               as_bad (_("hard-float conflicts with specified fpu"));
28511             break;
28512           }
28513
28514         /* Using pure-endian doubles (even if soft-float).      */
28515         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
28516           flags |= F_VFP_FLOAT;
28517
28518 #if defined OBJ_ELF
28519         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
28520             flags |= EF_ARM_MAVERICK_FLOAT;
28521         break;
28522
28523       case EF_ARM_EABI_VER4:
28524       case EF_ARM_EABI_VER5:
28525         /* No additional flags to set.  */
28526         break;
28527
28528       default:
28529         abort ();
28530       }
28531 #endif
28532     bfd_set_private_flags (stdoutput, flags);
28533
28534     /* We have run out flags in the COFF header to encode the
28535        status of ATPCS support, so instead we create a dummy,
28536        empty, debug section called .arm.atpcs.  */
28537     if (atpcs)
28538       {
28539         asection * sec;
28540
28541         sec = bfd_make_section (stdoutput, ".arm.atpcs");
28542
28543         if (sec != NULL)
28544           {
28545             bfd_set_section_flags
28546               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
28547             bfd_set_section_size (stdoutput, sec, 0);
28548             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
28549           }
28550       }
28551   }
28552 #endif
28553
28554   /* Record the CPU type as well.  */
28555   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
28556     mach = bfd_mach_arm_iWMMXt2;
28557   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
28558     mach = bfd_mach_arm_iWMMXt;
28559   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
28560     mach = bfd_mach_arm_XScale;
28561   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
28562     mach = bfd_mach_arm_ep9312;
28563   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
28564     mach = bfd_mach_arm_5TE;
28565   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
28566     {
28567       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
28568         mach = bfd_mach_arm_5T;
28569       else
28570         mach = bfd_mach_arm_5;
28571     }
28572   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
28573     {
28574       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
28575         mach = bfd_mach_arm_4T;
28576       else
28577         mach = bfd_mach_arm_4;
28578     }
28579   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
28580     mach = bfd_mach_arm_3M;
28581   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
28582     mach = bfd_mach_arm_3;
28583   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
28584     mach = bfd_mach_arm_2a;
28585   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
28586     mach = bfd_mach_arm_2;
28587   else
28588     mach = bfd_mach_arm_unknown;
28589
28590   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
28591 }
28592
28593 /* Command line processing.  */
28594
28595 /* md_parse_option
28596       Invocation line includes a switch not recognized by the base assembler.
28597       See if it's a processor-specific option.
28598
28599       This routine is somewhat complicated by the need for backwards
28600       compatibility (since older releases of gcc can't be changed).
28601       The new options try to make the interface as compatible as
28602       possible with GCC.
28603
28604       New options (supported) are:
28605
28606               -mcpu=<cpu name>           Assemble for selected processor
28607               -march=<architecture name> Assemble for selected architecture
28608               -mfpu=<fpu architecture>   Assemble for selected FPU.
28609               -EB/-mbig-endian           Big-endian
28610               -EL/-mlittle-endian        Little-endian
28611               -k                         Generate PIC code
28612               -mthumb                    Start in Thumb mode
28613               -mthumb-interwork          Code supports ARM/Thumb interworking
28614
28615               -m[no-]warn-deprecated     Warn about deprecated features
28616               -m[no-]warn-syms           Warn when symbols match instructions
28617
28618       For now we will also provide support for:
28619
28620               -mapcs-32                  32-bit Program counter
28621               -mapcs-26                  26-bit Program counter
28622               -macps-float               Floats passed in FP registers
28623               -mapcs-reentrant           Reentrant code
28624               -matpcs
28625       (sometime these will probably be replaced with -mapcs=<list of options>
28626       and -matpcs=<list of options>)
28627
28628       The remaining options are only supported for back-wards compatibility.
28629       Cpu variants, the arm part is optional:
28630               -m[arm]1                Currently not supported.
28631               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
28632               -m[arm]3                Arm 3 processor
28633               -m[arm]6[xx],           Arm 6 processors
28634               -m[arm]7[xx][t][[d]m]   Arm 7 processors
28635               -m[arm]8[10]            Arm 8 processors
28636               -m[arm]9[20][tdmi]      Arm 9 processors
28637               -mstrongarm[110[0]]     StrongARM processors
28638               -mxscale                XScale processors
28639               -m[arm]v[2345[t[e]]]    Arm architectures
28640               -mall                   All (except the ARM1)
28641       FP variants:
28642               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
28643               -mfpe-old               (No float load/store multiples)
28644               -mvfpxd                 VFP Single precision
28645               -mvfp                   All VFP
28646               -mno-fpu                Disable all floating point instructions
28647
28648       The following CPU names are recognized:
28649               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
28650               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
28651               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
28652               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
28653               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
28654               arm10t arm10e, arm1020t, arm1020e, arm10200e,
28655               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
28656
28657       */
28658
28659 const char * md_shortopts = "m:k";
28660
28661 #ifdef ARM_BI_ENDIAN
28662 #define OPTION_EB (OPTION_MD_BASE + 0)
28663 #define OPTION_EL (OPTION_MD_BASE + 1)
28664 #else
28665 #if TARGET_BYTES_BIG_ENDIAN
28666 #define OPTION_EB (OPTION_MD_BASE + 0)
28667 #else
28668 #define OPTION_EL (OPTION_MD_BASE + 1)
28669 #endif
28670 #endif
28671 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
28672 #define OPTION_FDPIC (OPTION_MD_BASE + 3)
28673
28674 struct option md_longopts[] =
28675 {
28676 #ifdef OPTION_EB
28677   {"EB", no_argument, NULL, OPTION_EB},
28678 #endif
28679 #ifdef OPTION_EL
28680   {"EL", no_argument, NULL, OPTION_EL},
28681 #endif
28682   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
28683 #ifdef OBJ_ELF
28684   {"fdpic", no_argument, NULL, OPTION_FDPIC},
28685 #endif
28686   {NULL, no_argument, NULL, 0}
28687 };
28688
28689 size_t md_longopts_size = sizeof (md_longopts);
28690
28691 struct arm_option_table
28692 {
28693   const char *  option;         /* Option name to match.  */
28694   const char *  help;           /* Help information.  */
28695   int *         var;            /* Variable to change.  */
28696   int           value;          /* What to change it to.  */
28697   const char *  deprecated;     /* If non-null, print this message.  */
28698 };
28699
28700 struct arm_option_table arm_opts[] =
28701 {
28702   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
28703   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
28704   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
28705    &support_interwork, 1, NULL},
28706   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
28707   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
28708   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
28709    1, NULL},
28710   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
28711   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
28712   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
28713   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
28714    NULL},
28715
28716   /* These are recognized by the assembler, but have no affect on code.  */
28717   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
28718   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
28719
28720   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
28721   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
28722    &warn_on_deprecated, 0, NULL},
28723   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
28724   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
28725   {NULL, NULL, NULL, 0, NULL}
28726 };
28727
28728 struct arm_legacy_option_table
28729 {
28730   const char *              option;             /* Option name to match.  */
28731   const arm_feature_set **  var;                /* Variable to change.  */
28732   const arm_feature_set     value;              /* What to change it to.  */
28733   const char *              deprecated;         /* If non-null, print this message.  */
28734 };
28735
28736 const struct arm_legacy_option_table arm_legacy_opts[] =
28737 {
28738   /* DON'T add any new processors to this list -- we want the whole list
28739      to go away...  Add them to the processors table instead.  */
28740   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
28741   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
28742   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
28743   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
28744   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
28745   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
28746   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
28747   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
28748   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
28749   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
28750   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
28751   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
28752   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
28753   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
28754   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
28755   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
28756   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
28757   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
28758   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
28759   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
28760   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
28761   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
28762   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
28763   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
28764   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
28765   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
28766   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
28767   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
28768   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
28769   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
28770   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
28771   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
28772   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
28773   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
28774   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
28775   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
28776   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
28777   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
28778   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
28779   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
28780   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
28781   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
28782   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
28783   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
28784   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
28785   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
28786   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
28787   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
28788   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
28789   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
28790   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
28791   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
28792   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
28793   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
28794   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
28795   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
28796   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
28797   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
28798   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
28799   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
28800   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
28801   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
28802   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
28803   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
28804   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
28805   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
28806   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
28807   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
28808   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
28809   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
28810    N_("use -mcpu=strongarm110")},
28811   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
28812    N_("use -mcpu=strongarm1100")},
28813   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
28814    N_("use -mcpu=strongarm1110")},
28815   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
28816   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
28817   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
28818
28819   /* Architecture variants -- don't add any more to this list either.  */
28820   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
28821   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
28822   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
28823   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
28824   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
28825   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
28826   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
28827   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
28828   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
28829   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
28830   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
28831   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
28832   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
28833   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
28834   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
28835   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
28836   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
28837   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
28838
28839   /* Floating point variants -- don't add any more to this list either.  */
28840   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
28841   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
28842   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
28843   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
28844    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
28845
28846   {NULL, NULL, ARM_ARCH_NONE, NULL}
28847 };
28848
28849 struct arm_cpu_option_table
28850 {
28851   const char *           name;
28852   size_t                 name_len;
28853   const arm_feature_set  value;
28854   const arm_feature_set  ext;
28855   /* For some CPUs we assume an FPU unless the user explicitly sets
28856      -mfpu=...  */
28857   const arm_feature_set  default_fpu;
28858   /* The canonical name of the CPU, or NULL to use NAME converted to upper
28859      case.  */
28860   const char *           canonical_name;
28861 };
28862
28863 /* This list should, at a minimum, contain all the cpu names
28864    recognized by GCC.  */
28865 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
28866
28867 static const struct arm_cpu_option_table arm_cpus[] =
28868 {
28869   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
28870                ARM_ARCH_NONE,
28871                FPU_ARCH_FPA),
28872   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
28873                ARM_ARCH_NONE,
28874                FPU_ARCH_FPA),
28875   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
28876                ARM_ARCH_NONE,
28877                FPU_ARCH_FPA),
28878   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
28879                ARM_ARCH_NONE,
28880                FPU_ARCH_FPA),
28881   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
28882                ARM_ARCH_NONE,
28883                FPU_ARCH_FPA),
28884   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
28885                ARM_ARCH_NONE,
28886                FPU_ARCH_FPA),
28887   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
28888                ARM_ARCH_NONE,
28889                FPU_ARCH_FPA),
28890   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
28891                ARM_ARCH_NONE,
28892                FPU_ARCH_FPA),
28893   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
28894                ARM_ARCH_NONE,
28895                FPU_ARCH_FPA),
28896   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
28897                ARM_ARCH_NONE,
28898                FPU_ARCH_FPA),
28899   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
28900                ARM_ARCH_NONE,
28901                FPU_ARCH_FPA),
28902   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
28903                ARM_ARCH_NONE,
28904                FPU_ARCH_FPA),
28905   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
28906                ARM_ARCH_NONE,
28907                FPU_ARCH_FPA),
28908   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
28909                ARM_ARCH_NONE,
28910                FPU_ARCH_FPA),
28911   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
28912                ARM_ARCH_NONE,
28913                FPU_ARCH_FPA),
28914   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
28915                ARM_ARCH_NONE,
28916                FPU_ARCH_FPA),
28917   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
28918                ARM_ARCH_NONE,
28919                FPU_ARCH_FPA),
28920   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
28921                ARM_ARCH_NONE,
28922                FPU_ARCH_FPA),
28923   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
28924                ARM_ARCH_NONE,
28925                FPU_ARCH_FPA),
28926   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
28927                ARM_ARCH_NONE,
28928                FPU_ARCH_FPA),
28929   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
28930                ARM_ARCH_NONE,
28931                FPU_ARCH_FPA),
28932   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
28933                ARM_ARCH_NONE,
28934                FPU_ARCH_FPA),
28935   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
28936                ARM_ARCH_NONE,
28937                FPU_ARCH_FPA),
28938   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
28939                ARM_ARCH_NONE,
28940                FPU_ARCH_FPA),
28941   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
28942                ARM_ARCH_NONE,
28943                FPU_ARCH_FPA),
28944   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
28945                ARM_ARCH_NONE,
28946                FPU_ARCH_FPA),
28947   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
28948                ARM_ARCH_NONE,
28949                FPU_ARCH_FPA),
28950   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
28951                ARM_ARCH_NONE,
28952                FPU_ARCH_FPA),
28953   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
28954                ARM_ARCH_NONE,
28955                FPU_ARCH_FPA),
28956   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
28957                ARM_ARCH_NONE,
28958                FPU_ARCH_FPA),
28959   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
28960                ARM_ARCH_NONE,
28961                FPU_ARCH_FPA),
28962   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
28963                ARM_ARCH_NONE,
28964                FPU_ARCH_FPA),
28965   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
28966                ARM_ARCH_NONE,
28967                FPU_ARCH_FPA),
28968   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
28969                ARM_ARCH_NONE,
28970                FPU_ARCH_FPA),
28971   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
28972                ARM_ARCH_NONE,
28973                FPU_ARCH_FPA),
28974   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
28975                ARM_ARCH_NONE,
28976                FPU_ARCH_FPA),
28977   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
28978                ARM_ARCH_NONE,
28979                FPU_ARCH_FPA),
28980   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
28981                ARM_ARCH_NONE,
28982                FPU_ARCH_FPA),
28983   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
28984                ARM_ARCH_NONE,
28985                FPU_ARCH_FPA),
28986   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
28987                ARM_ARCH_NONE,
28988                FPU_ARCH_FPA),
28989   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
28990                ARM_ARCH_NONE,
28991                FPU_ARCH_FPA),
28992   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
28993                ARM_ARCH_NONE,
28994                FPU_ARCH_FPA),
28995   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
28996                ARM_ARCH_NONE,
28997                FPU_ARCH_FPA),
28998   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
28999                ARM_ARCH_NONE,
29000                FPU_ARCH_FPA),
29001   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
29002                ARM_ARCH_NONE,
29003                FPU_ARCH_FPA),
29004   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
29005                ARM_ARCH_NONE,
29006                FPU_ARCH_FPA),
29007
29008   /* For V5 or later processors we default to using VFP; but the user
29009      should really set the FPU type explicitly.  */
29010   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
29011                ARM_ARCH_NONE,
29012                FPU_ARCH_VFP_V2),
29013   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
29014                ARM_ARCH_NONE,
29015                FPU_ARCH_VFP_V2),
29016   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
29017                ARM_ARCH_NONE,
29018                FPU_ARCH_VFP_V2),
29019   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
29020                ARM_ARCH_NONE,
29021                FPU_ARCH_VFP_V2),
29022   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
29023                ARM_ARCH_NONE,
29024                FPU_ARCH_VFP_V2),
29025   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
29026                ARM_ARCH_NONE,
29027                FPU_ARCH_VFP_V2),
29028   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
29029                ARM_ARCH_NONE,
29030                FPU_ARCH_VFP_V2),
29031   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
29032                ARM_ARCH_NONE,
29033                FPU_ARCH_VFP_V2),
29034   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
29035                ARM_ARCH_NONE,
29036                FPU_ARCH_VFP_V2),
29037   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
29038                ARM_ARCH_NONE,
29039                FPU_ARCH_VFP_V2),
29040   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
29041                ARM_ARCH_NONE,
29042                FPU_ARCH_VFP_V2),
29043   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
29044                ARM_ARCH_NONE,
29045                FPU_ARCH_VFP_V2),
29046   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
29047                ARM_ARCH_NONE,
29048                FPU_ARCH_VFP_V1),
29049   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
29050                ARM_ARCH_NONE,
29051                FPU_ARCH_VFP_V1),
29052   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
29053                ARM_ARCH_NONE,
29054                FPU_ARCH_VFP_V2),
29055   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
29056                ARM_ARCH_NONE,
29057                FPU_ARCH_VFP_V2),
29058   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
29059                ARM_ARCH_NONE,
29060                FPU_ARCH_VFP_V1),
29061   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
29062                ARM_ARCH_NONE,
29063                FPU_ARCH_VFP_V2),
29064   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
29065                ARM_ARCH_NONE,
29066                FPU_ARCH_VFP_V2),
29067   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
29068                ARM_ARCH_NONE,
29069                FPU_ARCH_VFP_V2),
29070   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
29071                ARM_ARCH_NONE,
29072                FPU_ARCH_VFP_V2),
29073   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
29074                ARM_ARCH_NONE,
29075                FPU_ARCH_VFP_V2),
29076   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
29077                ARM_ARCH_NONE,
29078                FPU_ARCH_VFP_V2),
29079   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
29080                ARM_ARCH_NONE,
29081                FPU_ARCH_VFP_V2),
29082   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
29083                ARM_ARCH_NONE,
29084                FPU_ARCH_VFP_V2),
29085   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
29086                ARM_ARCH_NONE,
29087                FPU_ARCH_VFP_V2),
29088   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
29089                ARM_ARCH_NONE,
29090                FPU_NONE),
29091   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
29092                ARM_ARCH_NONE,
29093                FPU_NONE),
29094   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
29095                ARM_ARCH_NONE,
29096                FPU_ARCH_VFP_V2),
29097   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
29098                ARM_ARCH_NONE,
29099                FPU_ARCH_VFP_V2),
29100   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
29101                ARM_ARCH_NONE,
29102                FPU_ARCH_VFP_V2),
29103   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
29104                ARM_ARCH_NONE,
29105                FPU_NONE),
29106   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
29107                ARM_ARCH_NONE,
29108                FPU_NONE),
29109   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
29110                ARM_ARCH_NONE,
29111                FPU_ARCH_VFP_V2),
29112   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
29113                ARM_ARCH_NONE,
29114                FPU_NONE),
29115   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
29116                ARM_ARCH_NONE,
29117                FPU_ARCH_VFP_V2),
29118   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
29119                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
29120                FPU_NONE),
29121   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
29122                ARM_ARCH_NONE,
29123                FPU_ARCH_NEON_VFP_V4),
29124   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
29125                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
29126                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
29127   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
29128                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
29129                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
29130   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
29131                ARM_ARCH_NONE,
29132                FPU_ARCH_NEON_VFP_V4),
29133   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
29134                ARM_ARCH_NONE,
29135                FPU_ARCH_NEON_VFP_V4),
29136   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
29137                ARM_ARCH_NONE,
29138                FPU_ARCH_NEON_VFP_V4),
29139   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
29140                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29141                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29142   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
29143                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29144                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29145   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
29146                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29147                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29148   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
29149                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29150                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
29151   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
29152                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29153                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29154   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
29155               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29156               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29157   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
29158               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29159               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29160   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
29161                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29162                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
29163   ARM_CPU_OPT ("cortex-a76",    "Cortex-A76",          ARM_ARCH_V8_2A,
29164                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29165                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
29166   ARM_CPU_OPT ("ares",    "Ares",              ARM_ARCH_V8_2A,
29167                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29168                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
29169   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
29170                ARM_ARCH_NONE,
29171                FPU_NONE),
29172   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
29173                ARM_ARCH_NONE,
29174                FPU_ARCH_VFP_V3D16),
29175   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
29176                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
29177                FPU_NONE),
29178   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
29179                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
29180                FPU_ARCH_VFP_V3D16),
29181   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
29182                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
29183                FPU_ARCH_VFP_V3D16),
29184   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
29185               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29186               FPU_ARCH_NEON_VFP_ARMV8),
29187   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
29188                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
29189                FPU_NONE),
29190   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
29191                ARM_ARCH_NONE,
29192                FPU_NONE),
29193   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
29194                ARM_ARCH_NONE,
29195                FPU_NONE),
29196   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
29197                ARM_ARCH_NONE,
29198                FPU_NONE),
29199   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
29200                ARM_ARCH_NONE,
29201                FPU_NONE),
29202   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
29203                ARM_ARCH_NONE,
29204                FPU_NONE),
29205   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
29206                ARM_ARCH_NONE,
29207                FPU_NONE),
29208   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
29209                ARM_ARCH_NONE,
29210                FPU_NONE),
29211   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
29212                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29213                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29214   ARM_CPU_OPT ("neoverse-n1",    "Neoverse N1",        ARM_ARCH_V8_2A,
29215                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29216                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
29217   /* ??? XSCALE is really an architecture.  */
29218   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
29219                ARM_ARCH_NONE,
29220                FPU_ARCH_VFP_V2),
29221
29222   /* ??? iwmmxt is not a processor.  */
29223   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
29224                ARM_ARCH_NONE,
29225                FPU_ARCH_VFP_V2),
29226   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
29227                ARM_ARCH_NONE,
29228                FPU_ARCH_VFP_V2),
29229   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
29230                ARM_ARCH_NONE,
29231                FPU_ARCH_VFP_V2),
29232
29233   /* Maverick.  */
29234   ARM_CPU_OPT ("ep9312",          "ARM920T",
29235                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
29236                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
29237
29238   /* Marvell processors.  */
29239   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
29240                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
29241                FPU_ARCH_VFP_V3D16),
29242   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
29243                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
29244                FPU_ARCH_NEON_VFP_V4),
29245
29246   /* APM X-Gene family.  */
29247   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
29248                ARM_ARCH_NONE,
29249                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29250   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
29251                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29252                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
29253
29254   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
29255 };
29256 #undef ARM_CPU_OPT
29257
29258 struct arm_ext_table
29259 {
29260   const char *            name;
29261   size_t                  name_len;
29262   const arm_feature_set   merge;
29263   const arm_feature_set   clear;
29264 };
29265
29266 struct arm_arch_option_table
29267 {
29268   const char *                  name;
29269   size_t                        name_len;
29270   const arm_feature_set         value;
29271   const arm_feature_set         default_fpu;
29272   const struct arm_ext_table *  ext_table;
29273 };
29274
29275 /* Used to add support for +E and +noE extension.  */
29276 #define ARM_EXT(E, M, C) { E, sizeof (E) - 1, M, C }
29277 /* Used to add support for a +E extension.  */
29278 #define ARM_ADD(E, M) { E, sizeof(E) - 1, M, ARM_ARCH_NONE }
29279 /* Used to add support for a +noE extension.  */
29280 #define ARM_REMOVE(E, C) { E, sizeof(E) -1, ARM_ARCH_NONE, C }
29281
29282 #define ALL_FP ARM_FEATURE (0, ARM_EXT2_FP16_INST | ARM_EXT2_FP16_FML, \
29283                             ~0 & ~FPU_ENDIAN_PURE)
29284
29285 static const struct arm_ext_table armv5te_ext_table[] =
29286 {
29287   ARM_EXT ("fp", FPU_ARCH_VFP_V2, ALL_FP),
29288   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29289 };
29290
29291 static const struct arm_ext_table armv7_ext_table[] =
29292 {
29293   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
29294   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29295 };
29296
29297 static const struct arm_ext_table armv7ve_ext_table[] =
29298 {
29299   ARM_EXT ("fp", FPU_ARCH_VFP_V4D16, ALL_FP),
29300   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16),
29301   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
29302   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
29303   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
29304   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),  /* Alias for +fp.  */
29305   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
29306
29307   ARM_EXT ("simd", FPU_ARCH_NEON_VFP_V4,
29308            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
29309
29310   /* Aliases for +simd.  */
29311   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
29312
29313   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
29314   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
29315   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
29316
29317   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29318 };
29319
29320 static const struct arm_ext_table armv7a_ext_table[] =
29321 {
29322   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
29323   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
29324   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
29325   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
29326   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
29327   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),
29328   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
29329
29330   ARM_EXT ("simd", FPU_ARCH_VFP_V3_PLUS_NEON_V1,
29331            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
29332
29333   /* Aliases for +simd.  */
29334   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
29335   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
29336
29337   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
29338   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
29339
29340   ARM_ADD ("mp", ARM_FEATURE_CORE_LOW (ARM_EXT_MP)),
29341   ARM_ADD ("sec", ARM_FEATURE_CORE_LOW (ARM_EXT_SEC)),
29342   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29343 };
29344
29345 static const struct arm_ext_table armv7r_ext_table[] =
29346 {
29347   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V3xD),
29348   ARM_ADD ("vfpv3xd", FPU_ARCH_VFP_V3xD), /* Alias for +fp.sp.  */
29349   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
29350   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
29351   ARM_ADD ("vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16),
29352   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
29353   ARM_EXT ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
29354            ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV)),
29355   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29356 };
29357
29358 static const struct arm_ext_table armv7em_ext_table[] =
29359 {
29360   ARM_EXT ("fp", FPU_ARCH_VFP_V4_SP_D16, ALL_FP),
29361   /* Alias for +fp, used to be known as fpv4-sp-d16.  */
29362   ARM_ADD ("vfpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16),
29363   ARM_ADD ("fpv5", FPU_ARCH_VFP_V5_SP_D16),
29364   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
29365   ARM_ADD ("fpv5-d16", FPU_ARCH_VFP_V5D16),
29366   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29367 };
29368
29369 static const struct arm_ext_table armv8a_ext_table[] =
29370 {
29371   ARM_ADD ("crc", ARCH_CRC_ARMV8),
29372   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
29373   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
29374            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
29375
29376   /* Armv8-a does not allow an FP implementation without SIMD, so the user
29377      should use the +simd option to turn on FP.  */
29378   ARM_REMOVE ("fp", ALL_FP),
29379   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
29380   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
29381   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29382 };
29383
29384
29385 static const struct arm_ext_table armv81a_ext_table[] =
29386 {
29387   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
29388   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
29389            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
29390
29391   /* Armv8-a does not allow an FP implementation without SIMD, so the user
29392      should use the +simd option to turn on FP.  */
29393   ARM_REMOVE ("fp", ALL_FP),
29394   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
29395   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
29396   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29397 };
29398
29399 static const struct arm_ext_table armv82a_ext_table[] =
29400 {
29401   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
29402   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_2_FP16),
29403   ARM_ADD ("fp16fml", FPU_ARCH_NEON_VFP_ARMV8_2_FP16FML),
29404   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
29405            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
29406   ARM_ADD ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
29407
29408   /* Armv8-a does not allow an FP implementation without SIMD, so the user
29409      should use the +simd option to turn on FP.  */
29410   ARM_REMOVE ("fp", ALL_FP),
29411   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
29412   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
29413   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29414 };
29415
29416 static const struct arm_ext_table armv84a_ext_table[] =
29417 {
29418   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
29419   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
29420   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
29421            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
29422
29423   /* Armv8-a does not allow an FP implementation without SIMD, so the user
29424      should use the +simd option to turn on FP.  */
29425   ARM_REMOVE ("fp", ALL_FP),
29426   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
29427   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
29428   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29429 };
29430
29431 static const struct arm_ext_table armv85a_ext_table[] =
29432 {
29433   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
29434   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
29435   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
29436            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
29437
29438   /* Armv8-a does not allow an FP implementation without SIMD, so the user
29439      should use the +simd option to turn on FP.  */
29440   ARM_REMOVE ("fp", ALL_FP),
29441   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29442 };
29443
29444 static const struct arm_ext_table armv8m_main_ext_table[] =
29445 {
29446   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
29447                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
29448   ARM_EXT ("fp", FPU_ARCH_VFP_V5_SP_D16, ALL_FP),
29449   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
29450   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29451 };
29452
29453 static const struct arm_ext_table armv8_1m_main_ext_table[] =
29454 {
29455   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
29456                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
29457   ARM_EXT ("fp",
29458            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
29459                         FPU_VFP_V5_SP_D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA),
29460            ALL_FP),
29461   ARM_ADD ("fp.dp",
29462            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
29463                         FPU_VFP_V5D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
29464   ARM_EXT ("mve", ARM_FEATURE_COPROC (FPU_MVE),
29465            ARM_FEATURE_COPROC (FPU_MVE | FPU_MVE_FP)),
29466   ARM_ADD ("mve.fp",
29467            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
29468                         FPU_MVE | FPU_MVE_FP | FPU_VFP_V5_SP_D16 |
29469                         FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
29470   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29471 };
29472
29473 static const struct arm_ext_table armv8r_ext_table[] =
29474 {
29475   ARM_ADD ("crc", ARCH_CRC_ARMV8),
29476   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
29477   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
29478            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
29479   ARM_REMOVE ("fp", ALL_FP),
29480   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V5_SP_D16),
29481   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
29482 };
29483
29484 /* This list should, at a minimum, contain all the architecture names
29485    recognized by GCC.  */
29486 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF, NULL }
29487 #define ARM_ARCH_OPT2(N, V, DF, ext) \
29488   { N, sizeof (N) - 1, V, DF, ext##_ext_table }
29489
29490 static const struct arm_arch_option_table arm_archs[] =
29491 {
29492   ARM_ARCH_OPT ("all",            ARM_ANY,              FPU_ARCH_FPA),
29493   ARM_ARCH_OPT ("armv1",          ARM_ARCH_V1,          FPU_ARCH_FPA),
29494   ARM_ARCH_OPT ("armv2",          ARM_ARCH_V2,          FPU_ARCH_FPA),
29495   ARM_ARCH_OPT ("armv2a",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
29496   ARM_ARCH_OPT ("armv2s",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
29497   ARM_ARCH_OPT ("armv3",          ARM_ARCH_V3,          FPU_ARCH_FPA),
29498   ARM_ARCH_OPT ("armv3m",         ARM_ARCH_V3M,         FPU_ARCH_FPA),
29499   ARM_ARCH_OPT ("armv4",          ARM_ARCH_V4,          FPU_ARCH_FPA),
29500   ARM_ARCH_OPT ("armv4xm",        ARM_ARCH_V4xM,        FPU_ARCH_FPA),
29501   ARM_ARCH_OPT ("armv4t",         ARM_ARCH_V4T,         FPU_ARCH_FPA),
29502   ARM_ARCH_OPT ("armv4txm",       ARM_ARCH_V4TxM,       FPU_ARCH_FPA),
29503   ARM_ARCH_OPT ("armv5",          ARM_ARCH_V5,          FPU_ARCH_VFP),
29504   ARM_ARCH_OPT ("armv5t",         ARM_ARCH_V5T,         FPU_ARCH_VFP),
29505   ARM_ARCH_OPT ("armv5txm",       ARM_ARCH_V5TxM,       FPU_ARCH_VFP),
29506   ARM_ARCH_OPT2 ("armv5te",       ARM_ARCH_V5TE,        FPU_ARCH_VFP,   armv5te),
29507   ARM_ARCH_OPT2 ("armv5texp",     ARM_ARCH_V5TExP,      FPU_ARCH_VFP, armv5te),
29508   ARM_ARCH_OPT2 ("armv5tej",      ARM_ARCH_V5TEJ,       FPU_ARCH_VFP,   armv5te),
29509   ARM_ARCH_OPT2 ("armv6",         ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
29510   ARM_ARCH_OPT2 ("armv6j",        ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
29511   ARM_ARCH_OPT2 ("armv6k",        ARM_ARCH_V6K,         FPU_ARCH_VFP,   armv5te),
29512   ARM_ARCH_OPT2 ("armv6z",        ARM_ARCH_V6Z,         FPU_ARCH_VFP,   armv5te),
29513   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
29514      kept to preserve existing behaviour.  */
29515   ARM_ARCH_OPT2 ("armv6kz",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
29516   ARM_ARCH_OPT2 ("armv6zk",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
29517   ARM_ARCH_OPT2 ("armv6t2",       ARM_ARCH_V6T2,        FPU_ARCH_VFP,   armv5te),
29518   ARM_ARCH_OPT2 ("armv6kt2",      ARM_ARCH_V6KT2,       FPU_ARCH_VFP,   armv5te),
29519   ARM_ARCH_OPT2 ("armv6zt2",      ARM_ARCH_V6ZT2,       FPU_ARCH_VFP,   armv5te),
29520   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
29521      kept to preserve existing behaviour.  */
29522   ARM_ARCH_OPT2 ("armv6kzt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
29523   ARM_ARCH_OPT2 ("armv6zkt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
29524   ARM_ARCH_OPT ("armv6-m",        ARM_ARCH_V6M,         FPU_ARCH_VFP),
29525   ARM_ARCH_OPT ("armv6s-m",       ARM_ARCH_V6SM,        FPU_ARCH_VFP),
29526   ARM_ARCH_OPT2 ("armv7",         ARM_ARCH_V7,          FPU_ARCH_VFP, armv7),
29527   /* The official spelling of the ARMv7 profile variants is the dashed form.
29528      Accept the non-dashed form for compatibility with old toolchains.  */
29529   ARM_ARCH_OPT2 ("armv7a",        ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
29530   ARM_ARCH_OPT2 ("armv7ve",       ARM_ARCH_V7VE,        FPU_ARCH_VFP, armv7ve),
29531   ARM_ARCH_OPT2 ("armv7r",        ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
29532   ARM_ARCH_OPT ("armv7m",         ARM_ARCH_V7M,         FPU_ARCH_VFP),
29533   ARM_ARCH_OPT2 ("armv7-a",       ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
29534   ARM_ARCH_OPT2 ("armv7-r",       ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
29535   ARM_ARCH_OPT ("armv7-m",        ARM_ARCH_V7M,         FPU_ARCH_VFP),
29536   ARM_ARCH_OPT2 ("armv7e-m",      ARM_ARCH_V7EM,        FPU_ARCH_VFP, armv7em),
29537   ARM_ARCH_OPT ("armv8-m.base",   ARM_ARCH_V8M_BASE,    FPU_ARCH_VFP),
29538   ARM_ARCH_OPT2 ("armv8-m.main",  ARM_ARCH_V8M_MAIN,    FPU_ARCH_VFP,
29539                  armv8m_main),
29540   ARM_ARCH_OPT2 ("armv8.1-m.main", ARM_ARCH_V8_1M_MAIN, FPU_ARCH_VFP,
29541                  armv8_1m_main),
29542   ARM_ARCH_OPT2 ("armv8-a",       ARM_ARCH_V8A,         FPU_ARCH_VFP, armv8a),
29543   ARM_ARCH_OPT2 ("armv8.1-a",     ARM_ARCH_V8_1A,       FPU_ARCH_VFP, armv81a),
29544   ARM_ARCH_OPT2 ("armv8.2-a",     ARM_ARCH_V8_2A,       FPU_ARCH_VFP, armv82a),
29545   ARM_ARCH_OPT2 ("armv8.3-a",     ARM_ARCH_V8_3A,       FPU_ARCH_VFP, armv82a),
29546   ARM_ARCH_OPT2 ("armv8-r",       ARM_ARCH_V8R,         FPU_ARCH_VFP, armv8r),
29547   ARM_ARCH_OPT2 ("armv8.4-a",     ARM_ARCH_V8_4A,       FPU_ARCH_VFP, armv84a),
29548   ARM_ARCH_OPT2 ("armv8.5-a",     ARM_ARCH_V8_5A,       FPU_ARCH_VFP, armv85a),
29549   ARM_ARCH_OPT ("xscale",         ARM_ARCH_XSCALE,      FPU_ARCH_VFP),
29550   ARM_ARCH_OPT ("iwmmxt",         ARM_ARCH_IWMMXT,      FPU_ARCH_VFP),
29551   ARM_ARCH_OPT ("iwmmxt2",        ARM_ARCH_IWMMXT2,     FPU_ARCH_VFP),
29552   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
29553 };
29554 #undef ARM_ARCH_OPT
29555
29556 /* ISA extensions in the co-processor and main instruction set space.  */
29557
29558 struct arm_option_extension_value_table
29559 {
29560   const char *           name;
29561   size_t                 name_len;
29562   const arm_feature_set  merge_value;
29563   const arm_feature_set  clear_value;
29564   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
29565      indicates that an extension is available for all architectures while
29566      ARM_ANY marks an empty entry.  */
29567   const arm_feature_set  allowed_archs[2];
29568 };
29569
29570 /* The following table must be in alphabetical order with a NULL last entry.  */
29571
29572 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
29573 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
29574
29575 /* DEPRECATED: Refrain from using this table to add any new extensions, instead
29576    use the context sensitive approach using arm_ext_table's.  */
29577 static const struct arm_option_extension_value_table arm_extensions[] =
29578 {
29579   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
29580                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
29581   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
29582                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
29583                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
29584   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
29585                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
29586                           ARM_ARCH_V8_2A),
29587   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
29588                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
29589                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
29590   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
29591                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
29592   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29593                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
29594                         ARM_ARCH_V8_2A),
29595   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
29596                                                   | ARM_EXT2_FP16_FML),
29597                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
29598                                                   | ARM_EXT2_FP16_FML),
29599                            ARM_ARCH_V8_2A),
29600   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
29601                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
29602                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
29603                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
29604   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
29605      Thumb divide instruction.  Due to this having the same name as the
29606      previous entry, this will be ignored when doing command-line parsing and
29607      only considered by build attribute selection code.  */
29608   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
29609                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
29610                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
29611   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
29612                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
29613   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
29614                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
29615   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
29616                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
29617   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
29618                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
29619                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
29620                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
29621   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
29622                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
29623                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
29624   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
29625                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
29626                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
29627   ARM_EXT_OPT ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
29628                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
29629                         ARM_ARCH_V8A),
29630   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
29631                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
29632                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
29633   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
29634                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
29635                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
29636   ARM_EXT_OPT ("sb",    ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
29637                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
29638                         ARM_ARCH_V8A),
29639   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
29640                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
29641                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
29642                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
29643   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
29644                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
29645                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
29646   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
29647                                      | ARM_EXT_DIV),
29648                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
29649                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
29650   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
29651                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
29652   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
29653 };
29654 #undef ARM_EXT_OPT
29655
29656 /* ISA floating-point and Advanced SIMD extensions.  */
29657 struct arm_option_fpu_value_table
29658 {
29659   const char *           name;
29660   const arm_feature_set  value;
29661 };
29662
29663 /* This list should, at a minimum, contain all the fpu names
29664    recognized by GCC.  */
29665 static const struct arm_option_fpu_value_table arm_fpus[] =
29666 {
29667   {"softfpa",           FPU_NONE},
29668   {"fpe",               FPU_ARCH_FPE},
29669   {"fpe2",              FPU_ARCH_FPE},
29670   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
29671   {"fpa",               FPU_ARCH_FPA},
29672   {"fpa10",             FPU_ARCH_FPA},
29673   {"fpa11",             FPU_ARCH_FPA},
29674   {"arm7500fe",         FPU_ARCH_FPA},
29675   {"softvfp",           FPU_ARCH_VFP},
29676   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
29677   {"vfp",               FPU_ARCH_VFP_V2},
29678   {"vfp9",              FPU_ARCH_VFP_V2},
29679   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
29680   {"vfp10",             FPU_ARCH_VFP_V2},
29681   {"vfp10-r0",          FPU_ARCH_VFP_V1},
29682   {"vfpxd",             FPU_ARCH_VFP_V1xD},
29683   {"vfpv2",             FPU_ARCH_VFP_V2},
29684   {"vfpv3",             FPU_ARCH_VFP_V3},
29685   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
29686   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
29687   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
29688   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
29689   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
29690   {"arm1020t",          FPU_ARCH_VFP_V1},
29691   {"arm1020e",          FPU_ARCH_VFP_V2},
29692   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
29693   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
29694   {"maverick",          FPU_ARCH_MAVERICK},
29695   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
29696   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
29697   {"neon-fp16",         FPU_ARCH_NEON_FP16},
29698   {"vfpv4",             FPU_ARCH_VFP_V4},
29699   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
29700   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
29701   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
29702   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
29703   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
29704   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
29705   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
29706   {"crypto-neon-fp-armv8",
29707                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
29708   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
29709   {"crypto-neon-fp-armv8.1",
29710                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
29711   {NULL,                ARM_ARCH_NONE}
29712 };
29713
29714 struct arm_option_value_table
29715 {
29716   const char *name;
29717   long value;
29718 };
29719
29720 static const struct arm_option_value_table arm_float_abis[] =
29721 {
29722   {"hard",      ARM_FLOAT_ABI_HARD},
29723   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
29724   {"soft",      ARM_FLOAT_ABI_SOFT},
29725   {NULL,        0}
29726 };
29727
29728 #ifdef OBJ_ELF
29729 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
29730 static const struct arm_option_value_table arm_eabis[] =
29731 {
29732   {"gnu",       EF_ARM_EABI_UNKNOWN},
29733   {"4",         EF_ARM_EABI_VER4},
29734   {"5",         EF_ARM_EABI_VER5},
29735   {NULL,        0}
29736 };
29737 #endif
29738
29739 struct arm_long_option_table
29740 {
29741   const char * option;                  /* Substring to match.  */
29742   const char * help;                    /* Help information.  */
29743   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
29744   const char * deprecated;              /* If non-null, print this message.  */
29745 };
29746
29747 static bfd_boolean
29748 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
29749                      arm_feature_set *ext_set,
29750                      const struct arm_ext_table *ext_table)
29751 {
29752   /* We insist on extensions being specified in alphabetical order, and with
29753      extensions being added before being removed.  We achieve this by having
29754      the global ARM_EXTENSIONS table in alphabetical order, and using the
29755      ADDING_VALUE variable to indicate whether we are adding an extension (1)
29756      or removing it (0) and only allowing it to change in the order
29757      -1 -> 1 -> 0.  */
29758   const struct arm_option_extension_value_table * opt = NULL;
29759   const arm_feature_set arm_any = ARM_ANY;
29760   int adding_value = -1;
29761
29762   while (str != NULL && *str != 0)
29763     {
29764       const char *ext;
29765       size_t len;
29766
29767       if (*str != '+')
29768         {
29769           as_bad (_("invalid architectural extension"));
29770           return FALSE;
29771         }
29772
29773       str++;
29774       ext = strchr (str, '+');
29775
29776       if (ext != NULL)
29777         len = ext - str;
29778       else
29779         len = strlen (str);
29780
29781       if (len >= 2 && strncmp (str, "no", 2) == 0)
29782         {
29783           if (adding_value != 0)
29784             {
29785               adding_value = 0;
29786               opt = arm_extensions;
29787             }
29788
29789           len -= 2;
29790           str += 2;
29791         }
29792       else if (len > 0)
29793         {
29794           if (adding_value == -1)
29795             {
29796               adding_value = 1;
29797               opt = arm_extensions;
29798             }
29799           else if (adding_value != 1)
29800             {
29801               as_bad (_("must specify extensions to add before specifying "
29802                         "those to remove"));
29803               return FALSE;
29804             }
29805         }
29806
29807       if (len == 0)
29808         {
29809           as_bad (_("missing architectural extension"));
29810           return FALSE;
29811         }
29812
29813       gas_assert (adding_value != -1);
29814       gas_assert (opt != NULL);
29815
29816       if (ext_table != NULL)
29817         {
29818           const struct arm_ext_table * ext_opt = ext_table;
29819           bfd_boolean found = FALSE;
29820           for (; ext_opt->name != NULL; ext_opt++)
29821             if (ext_opt->name_len == len
29822                 && strncmp (ext_opt->name, str, len) == 0)
29823               {
29824                 if (adding_value)
29825                   {
29826                     if (ARM_FEATURE_ZERO (ext_opt->merge))
29827                         /* TODO: Option not supported.  When we remove the
29828                            legacy table this case should error out.  */
29829                         continue;
29830
29831                     ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, ext_opt->merge);
29832                   }
29833                 else
29834                   {
29835                     if (ARM_FEATURE_ZERO (ext_opt->clear))
29836                         /* TODO: Option not supported.  When we remove the
29837                            legacy table this case should error out.  */
29838                         continue;
29839                     ARM_CLEAR_FEATURE (*ext_set, *ext_set, ext_opt->clear);
29840                   }
29841                 found = TRUE;
29842                 break;
29843               }
29844           if (found)
29845             {
29846               str = ext;
29847               continue;
29848             }
29849         }
29850
29851       /* Scan over the options table trying to find an exact match. */
29852       for (; opt->name != NULL; opt++)
29853         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
29854           {
29855             int i, nb_allowed_archs =
29856               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
29857             /* Check we can apply the extension to this architecture.  */
29858             for (i = 0; i < nb_allowed_archs; i++)
29859               {
29860                 /* Empty entry.  */
29861                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
29862                   continue;
29863                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
29864                   break;
29865               }
29866             if (i == nb_allowed_archs)
29867               {
29868                 as_bad (_("extension does not apply to the base architecture"));
29869                 return FALSE;
29870               }
29871
29872             /* Add or remove the extension.  */
29873             if (adding_value)
29874               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
29875             else
29876               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
29877
29878             /* Allowing Thumb division instructions for ARMv7 in autodetection
29879                rely on this break so that duplicate extensions (extensions
29880                with the same name as a previous extension in the list) are not
29881                considered for command-line parsing.  */
29882             break;
29883           }
29884
29885       if (opt->name == NULL)
29886         {
29887           /* Did we fail to find an extension because it wasn't specified in
29888              alphabetical order, or because it does not exist?  */
29889
29890           for (opt = arm_extensions; opt->name != NULL; opt++)
29891             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
29892               break;
29893
29894           if (opt->name == NULL)
29895             as_bad (_("unknown architectural extension `%s'"), str);
29896           else
29897             as_bad (_("architectural extensions must be specified in "
29898                       "alphabetical order"));
29899
29900           return FALSE;
29901         }
29902       else
29903         {
29904           /* We should skip the extension we've just matched the next time
29905              round.  */
29906           opt++;
29907         }
29908
29909       str = ext;
29910     };
29911
29912   return TRUE;
29913 }
29914
29915 static bfd_boolean
29916 arm_parse_cpu (const char *str)
29917 {
29918   const struct arm_cpu_option_table *opt;
29919   const char *ext = strchr (str, '+');
29920   size_t len;
29921
29922   if (ext != NULL)
29923     len = ext - str;
29924   else
29925     len = strlen (str);
29926
29927   if (len == 0)
29928     {
29929       as_bad (_("missing cpu name `%s'"), str);
29930       return FALSE;
29931     }
29932
29933   for (opt = arm_cpus; opt->name != NULL; opt++)
29934     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
29935       {
29936         mcpu_cpu_opt = &opt->value;
29937         if (mcpu_ext_opt == NULL)
29938           mcpu_ext_opt = XNEW (arm_feature_set);
29939         *mcpu_ext_opt = opt->ext;
29940         mcpu_fpu_opt = &opt->default_fpu;
29941         if (opt->canonical_name)
29942           {
29943             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
29944             strcpy (selected_cpu_name, opt->canonical_name);
29945           }
29946         else
29947           {
29948             size_t i;
29949
29950             if (len >= sizeof selected_cpu_name)
29951               len = (sizeof selected_cpu_name) - 1;
29952
29953             for (i = 0; i < len; i++)
29954               selected_cpu_name[i] = TOUPPER (opt->name[i]);
29955             selected_cpu_name[i] = 0;
29956           }
29957
29958         if (ext != NULL)
29959           return arm_parse_extension (ext, mcpu_cpu_opt, mcpu_ext_opt, NULL);
29960
29961         return TRUE;
29962       }
29963
29964   as_bad (_("unknown cpu `%s'"), str);
29965   return FALSE;
29966 }
29967
29968 static bfd_boolean
29969 arm_parse_arch (const char *str)
29970 {
29971   const struct arm_arch_option_table *opt;
29972   const char *ext = strchr (str, '+');
29973   size_t len;
29974
29975   if (ext != NULL)
29976     len = ext - str;
29977   else
29978     len = strlen (str);
29979
29980   if (len == 0)
29981     {
29982       as_bad (_("missing architecture name `%s'"), str);
29983       return FALSE;
29984     }
29985
29986   for (opt = arm_archs; opt->name != NULL; opt++)
29987     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
29988       {
29989         march_cpu_opt = &opt->value;
29990         if (march_ext_opt == NULL)
29991           march_ext_opt = XNEW (arm_feature_set);
29992         *march_ext_opt = arm_arch_none;
29993         march_fpu_opt = &opt->default_fpu;
29994         strcpy (selected_cpu_name, opt->name);
29995
29996         if (ext != NULL)
29997           return arm_parse_extension (ext, march_cpu_opt, march_ext_opt,
29998                                       opt->ext_table);
29999
30000         return TRUE;
30001       }
30002
30003   as_bad (_("unknown architecture `%s'\n"), str);
30004   return FALSE;
30005 }
30006
30007 static bfd_boolean
30008 arm_parse_fpu (const char * str)
30009 {
30010   const struct arm_option_fpu_value_table * opt;
30011
30012   for (opt = arm_fpus; opt->name != NULL; opt++)
30013     if (streq (opt->name, str))
30014       {
30015         mfpu_opt = &opt->value;
30016         return TRUE;
30017       }
30018
30019   as_bad (_("unknown floating point format `%s'\n"), str);
30020   return FALSE;
30021 }
30022
30023 static bfd_boolean
30024 arm_parse_float_abi (const char * str)
30025 {
30026   const struct arm_option_value_table * opt;
30027
30028   for (opt = arm_float_abis; opt->name != NULL; opt++)
30029     if (streq (opt->name, str))
30030       {
30031         mfloat_abi_opt = opt->value;
30032         return TRUE;
30033       }
30034
30035   as_bad (_("unknown floating point abi `%s'\n"), str);
30036   return FALSE;
30037 }
30038
30039 #ifdef OBJ_ELF
30040 static bfd_boolean
30041 arm_parse_eabi (const char * str)
30042 {
30043   const struct arm_option_value_table *opt;
30044
30045   for (opt = arm_eabis; opt->name != NULL; opt++)
30046     if (streq (opt->name, str))
30047       {
30048         meabi_flags = opt->value;
30049         return TRUE;
30050       }
30051   as_bad (_("unknown EABI `%s'\n"), str);
30052   return FALSE;
30053 }
30054 #endif
30055
30056 static bfd_boolean
30057 arm_parse_it_mode (const char * str)
30058 {
30059   bfd_boolean ret = TRUE;
30060
30061   if (streq ("arm", str))
30062     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
30063   else if (streq ("thumb", str))
30064     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
30065   else if (streq ("always", str))
30066     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
30067   else if (streq ("never", str))
30068     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
30069   else
30070     {
30071       as_bad (_("unknown implicit IT mode `%s', should be "\
30072                 "arm, thumb, always, or never."), str);
30073       ret = FALSE;
30074     }
30075
30076   return ret;
30077 }
30078
30079 static bfd_boolean
30080 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
30081 {
30082   codecomposer_syntax = TRUE;
30083   arm_comment_chars[0] = ';';
30084   arm_line_separator_chars[0] = 0;
30085   return TRUE;
30086 }
30087
30088 struct arm_long_option_table arm_long_opts[] =
30089 {
30090   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
30091    arm_parse_cpu, NULL},
30092   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
30093    arm_parse_arch, NULL},
30094   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
30095    arm_parse_fpu, NULL},
30096   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
30097    arm_parse_float_abi, NULL},
30098 #ifdef OBJ_ELF
30099   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
30100    arm_parse_eabi, NULL},
30101 #endif
30102   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
30103    arm_parse_it_mode, NULL},
30104   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
30105    arm_ccs_mode, NULL},
30106   {NULL, NULL, 0, NULL}
30107 };
30108
30109 int
30110 md_parse_option (int c, const char * arg)
30111 {
30112   struct arm_option_table *opt;
30113   const struct arm_legacy_option_table *fopt;
30114   struct arm_long_option_table *lopt;
30115
30116   switch (c)
30117     {
30118 #ifdef OPTION_EB
30119     case OPTION_EB:
30120       target_big_endian = 1;
30121       break;
30122 #endif
30123
30124 #ifdef OPTION_EL
30125     case OPTION_EL:
30126       target_big_endian = 0;
30127       break;
30128 #endif
30129
30130     case OPTION_FIX_V4BX:
30131       fix_v4bx = TRUE;
30132       break;
30133
30134 #ifdef OBJ_ELF
30135     case OPTION_FDPIC:
30136       arm_fdpic = TRUE;
30137       break;
30138 #endif /* OBJ_ELF */
30139
30140     case 'a':
30141       /* Listing option.  Just ignore these, we don't support additional
30142          ones.  */
30143       return 0;
30144
30145     default:
30146       for (opt = arm_opts; opt->option != NULL; opt++)
30147         {
30148           if (c == opt->option[0]
30149               && ((arg == NULL && opt->option[1] == 0)
30150                   || streq (arg, opt->option + 1)))
30151             {
30152               /* If the option is deprecated, tell the user.  */
30153               if (warn_on_deprecated && opt->deprecated != NULL)
30154                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
30155                            arg ? arg : "", _(opt->deprecated));
30156
30157               if (opt->var != NULL)
30158                 *opt->var = opt->value;
30159
30160               return 1;
30161             }
30162         }
30163
30164       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
30165         {
30166           if (c == fopt->option[0]
30167               && ((arg == NULL && fopt->option[1] == 0)
30168                   || streq (arg, fopt->option + 1)))
30169             {
30170               /* If the option is deprecated, tell the user.  */
30171               if (warn_on_deprecated && fopt->deprecated != NULL)
30172                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
30173                            arg ? arg : "", _(fopt->deprecated));
30174
30175               if (fopt->var != NULL)
30176                 *fopt->var = &fopt->value;
30177
30178               return 1;
30179             }
30180         }
30181
30182       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
30183         {
30184           /* These options are expected to have an argument.  */
30185           if (c == lopt->option[0]
30186               && arg != NULL
30187               && strncmp (arg, lopt->option + 1,
30188                           strlen (lopt->option + 1)) == 0)
30189             {
30190               /* If the option is deprecated, tell the user.  */
30191               if (warn_on_deprecated && lopt->deprecated != NULL)
30192                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
30193                            _(lopt->deprecated));
30194
30195               /* Call the sup-option parser.  */
30196               return lopt->func (arg + strlen (lopt->option) - 1);
30197             }
30198         }
30199
30200       return 0;
30201     }
30202
30203   return 1;
30204 }
30205
30206 void
30207 md_show_usage (FILE * fp)
30208 {
30209   struct arm_option_table *opt;
30210   struct arm_long_option_table *lopt;
30211
30212   fprintf (fp, _(" ARM-specific assembler options:\n"));
30213
30214   for (opt = arm_opts; opt->option != NULL; opt++)
30215     if (opt->help != NULL)
30216       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
30217
30218   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
30219     if (lopt->help != NULL)
30220       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
30221
30222 #ifdef OPTION_EB
30223   fprintf (fp, _("\
30224   -EB                     assemble code for a big-endian cpu\n"));
30225 #endif
30226
30227 #ifdef OPTION_EL
30228   fprintf (fp, _("\
30229   -EL                     assemble code for a little-endian cpu\n"));
30230 #endif
30231
30232   fprintf (fp, _("\
30233   --fix-v4bx              Allow BX in ARMv4 code\n"));
30234
30235 #ifdef OBJ_ELF
30236   fprintf (fp, _("\
30237   --fdpic                 generate an FDPIC object file\n"));
30238 #endif /* OBJ_ELF */
30239 }
30240
30241 #ifdef OBJ_ELF
30242
30243 typedef struct
30244 {
30245   int val;
30246   arm_feature_set flags;
30247 } cpu_arch_ver_table;
30248
30249 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
30250    chronologically for architectures, with an exception for ARMv6-M and
30251    ARMv6S-M due to legacy reasons.  No new architecture should have a
30252    special case.  This allows for build attribute selection results to be
30253    stable when new architectures are added.  */
30254 static const cpu_arch_ver_table cpu_arch_ver[] =
30255 {
30256     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V1},
30257     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2},
30258     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2S},
30259     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3},
30260     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3M},
30261     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4xM},
30262     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4},
30263     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4TxM},
30264     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4T},
30265     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5xM},
30266     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5},
30267     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5TxM},
30268     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5T},
30269     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TExP},
30270     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TE},
30271     {TAG_CPU_ARCH_V5TEJ,      ARM_ARCH_V5TEJ},
30272     {TAG_CPU_ARCH_V6,         ARM_ARCH_V6},
30273     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6Z},
30274     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6KZ},
30275     {TAG_CPU_ARCH_V6K,        ARM_ARCH_V6K},
30276     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6T2},
30277     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KT2},
30278     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6ZT2},
30279     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KZT2},
30280
30281     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
30282        always selected build attributes to match those of ARMv6-M
30283        (resp. ARMv6S-M).  However, due to these architectures being a strict
30284        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
30285        would be selected when fully respecting chronology of architectures.
30286        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
30287        move them before ARMv7 architectures.  */
30288     {TAG_CPU_ARCH_V6_M,       ARM_ARCH_V6M},
30289     {TAG_CPU_ARCH_V6S_M,      ARM_ARCH_V6SM},
30290
30291     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7},
30292     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7A},
30293     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7R},
30294     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7M},
30295     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7VE},
30296     {TAG_CPU_ARCH_V7E_M,      ARM_ARCH_V7EM},
30297     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8A},
30298     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_1A},
30299     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_2A},
30300     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_3A},
30301     {TAG_CPU_ARCH_V8M_BASE,   ARM_ARCH_V8M_BASE},
30302     {TAG_CPU_ARCH_V8M_MAIN,   ARM_ARCH_V8M_MAIN},
30303     {TAG_CPU_ARCH_V8R,        ARM_ARCH_V8R},
30304     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_4A},
30305     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_5A},
30306     {TAG_CPU_ARCH_V8_1M_MAIN, ARM_ARCH_V8_1M_MAIN},
30307     {-1,                      ARM_ARCH_NONE}
30308 };
30309
30310 /* Set an attribute if it has not already been set by the user.  */
30311
30312 static void
30313 aeabi_set_attribute_int (int tag, int value)
30314 {
30315   if (tag < 1
30316       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
30317       || !attributes_set_explicitly[tag])
30318     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
30319 }
30320
30321 static void
30322 aeabi_set_attribute_string (int tag, const char *value)
30323 {
30324   if (tag < 1
30325       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
30326       || !attributes_set_explicitly[tag])
30327     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
30328 }
30329
30330 /* Return whether features in the *NEEDED feature set are available via
30331    extensions for the architecture whose feature set is *ARCH_FSET.  */
30332
30333 static bfd_boolean
30334 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
30335                             const arm_feature_set *needed)
30336 {
30337   int i, nb_allowed_archs;
30338   arm_feature_set ext_fset;
30339   const struct arm_option_extension_value_table *opt;
30340
30341   ext_fset = arm_arch_none;
30342   for (opt = arm_extensions; opt->name != NULL; opt++)
30343     {
30344       /* Extension does not provide any feature we need.  */
30345       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
30346         continue;
30347
30348       nb_allowed_archs =
30349         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
30350       for (i = 0; i < nb_allowed_archs; i++)
30351         {
30352           /* Empty entry.  */
30353           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
30354             break;
30355
30356           /* Extension is available, add it.  */
30357           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
30358             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
30359         }
30360     }
30361
30362   /* Can we enable all features in *needed?  */
30363   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
30364 }
30365
30366 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
30367    a given architecture feature set *ARCH_EXT_FSET including extension feature
30368    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
30369    - if true, check for an exact match of the architecture modulo extensions;
30370    - otherwise, select build attribute value of the first superset
30371      architecture released so that results remains stable when new architectures
30372      are added.
30373    For -march/-mcpu=all the build attribute value of the most featureful
30374    architecture is returned.  Tag_CPU_arch_profile result is returned in
30375    PROFILE.  */
30376
30377 static int
30378 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
30379                               const arm_feature_set *ext_fset,
30380                               char *profile, int exact_match)
30381 {
30382   arm_feature_set arch_fset;
30383   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
30384
30385   /* Select most featureful architecture with all its extensions if building
30386      for -march=all as the feature sets used to set build attributes.  */
30387   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
30388     {
30389       /* Force revisiting of decision for each new architecture.  */
30390       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8_1M_MAIN);
30391       *profile = 'A';
30392       return TAG_CPU_ARCH_V8;
30393     }
30394
30395   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
30396
30397   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
30398     {
30399       arm_feature_set known_arch_fset;
30400
30401       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
30402       if (exact_match)
30403         {
30404           /* Base architecture match user-specified architecture and
30405              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
30406           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
30407             {
30408               p_ver_ret = p_ver;
30409               goto found;
30410             }
30411           /* Base architecture match user-specified architecture only
30412              (eg. ARMv6-M in the same case as above).  Record it in case we
30413              find a match with above condition.  */
30414           else if (p_ver_ret == NULL
30415                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
30416             p_ver_ret = p_ver;
30417         }
30418       else
30419         {
30420
30421           /* Architecture has all features wanted.  */
30422           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
30423             {
30424               arm_feature_set added_fset;
30425
30426               /* Compute features added by this architecture over the one
30427                  recorded in p_ver_ret.  */
30428               if (p_ver_ret != NULL)
30429                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
30430                                    p_ver_ret->flags);
30431               /* First architecture that match incl. with extensions, or the
30432                  only difference in features over the recorded match is
30433                  features that were optional and are now mandatory.  */
30434               if (p_ver_ret == NULL
30435                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
30436                 {
30437                   p_ver_ret = p_ver;
30438                   goto found;
30439                 }
30440             }
30441           else if (p_ver_ret == NULL)
30442             {
30443               arm_feature_set needed_ext_fset;
30444
30445               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
30446
30447               /* Architecture has all features needed when using some
30448                  extensions.  Record it and continue searching in case there
30449                  exist an architecture providing all needed features without
30450                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
30451                  OS extension).  */
30452               if (have_ext_for_needed_feat_p (&known_arch_fset,
30453                                               &needed_ext_fset))
30454                 p_ver_ret = p_ver;
30455             }
30456         }
30457     }
30458
30459   if (p_ver_ret == NULL)
30460     return -1;
30461
30462 found:
30463   /* Tag_CPU_arch_profile.  */
30464   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
30465       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
30466       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
30467           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
30468     *profile = 'A';
30469   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
30470     *profile = 'R';
30471   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
30472     *profile = 'M';
30473   else
30474     *profile = '\0';
30475   return p_ver_ret->val;
30476 }
30477
30478 /* Set the public EABI object attributes.  */
30479
30480 static void
30481 aeabi_set_public_attributes (void)
30482 {
30483   char profile = '\0';
30484   int arch = -1;
30485   int virt_sec = 0;
30486   int fp16_optional = 0;
30487   int skip_exact_match = 0;
30488   arm_feature_set flags, flags_arch, flags_ext;
30489
30490   /* Autodetection mode, choose the architecture based the instructions
30491      actually used.  */
30492   if (no_cpu_selected ())
30493     {
30494       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
30495
30496       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
30497         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
30498
30499       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
30500         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
30501
30502       /* Code run during relaxation relies on selected_cpu being set.  */
30503       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
30504       flags_ext = arm_arch_none;
30505       ARM_CLEAR_FEATURE (selected_arch, flags_arch, flags_ext);
30506       selected_ext = flags_ext;
30507       selected_cpu = flags;
30508     }
30509   /* Otherwise, choose the architecture based on the capabilities of the
30510      requested cpu.  */
30511   else
30512     {
30513       ARM_MERGE_FEATURE_SETS (flags_arch, selected_arch, selected_ext);
30514       ARM_CLEAR_FEATURE (flags_arch, flags_arch, fpu_any);
30515       flags_ext = selected_ext;
30516       flags = selected_cpu;
30517     }
30518   ARM_MERGE_FEATURE_SETS (flags, flags, selected_fpu);
30519
30520   /* Allow the user to override the reported architecture.  */
30521   if (!ARM_FEATURE_ZERO (selected_object_arch))
30522     {
30523       ARM_CLEAR_FEATURE (flags_arch, selected_object_arch, fpu_any);
30524       flags_ext = arm_arch_none;
30525     }
30526   else
30527     skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
30528
30529   /* When this function is run again after relaxation has happened there is no
30530      way to determine whether an architecture or CPU was specified by the user:
30531      - selected_cpu is set above for relaxation to work;
30532      - march_cpu_opt is not set if only -mcpu or .cpu is used;
30533      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
30534      Therefore, if not in -march=all case we first try an exact match and fall
30535      back to autodetection.  */
30536   if (!skip_exact_match)
30537     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
30538   if (arch == -1)
30539     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
30540   if (arch == -1)
30541     as_bad (_("no architecture contains all the instructions used\n"));
30542
30543   /* Tag_CPU_name.  */
30544   if (selected_cpu_name[0])
30545     {
30546       char *q;
30547
30548       q = selected_cpu_name;
30549       if (strncmp (q, "armv", 4) == 0)
30550         {
30551           int i;
30552
30553           q += 4;
30554           for (i = 0; q[i]; i++)
30555             q[i] = TOUPPER (q[i]);
30556         }
30557       aeabi_set_attribute_string (Tag_CPU_name, q);
30558     }
30559
30560   /* Tag_CPU_arch.  */
30561   aeabi_set_attribute_int (Tag_CPU_arch, arch);
30562
30563   /* Tag_CPU_arch_profile.  */
30564   if (profile != '\0')
30565     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
30566
30567   /* Tag_DSP_extension.  */
30568   if (ARM_CPU_HAS_FEATURE (selected_ext, arm_ext_dsp))
30569     aeabi_set_attribute_int (Tag_DSP_extension, 1);
30570
30571   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
30572   /* Tag_ARM_ISA_use.  */
30573   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
30574       || ARM_FEATURE_ZERO (flags_arch))
30575     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
30576
30577   /* Tag_THUMB_ISA_use.  */
30578   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
30579       || ARM_FEATURE_ZERO (flags_arch))
30580     {
30581       int thumb_isa_use;
30582
30583       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
30584           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
30585         thumb_isa_use = 3;
30586       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
30587         thumb_isa_use = 2;
30588       else
30589         thumb_isa_use = 1;
30590       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
30591     }
30592
30593   /* Tag_VFP_arch.  */
30594   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
30595     aeabi_set_attribute_int (Tag_VFP_arch,
30596                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
30597                              ? 7 : 8);
30598   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
30599     aeabi_set_attribute_int (Tag_VFP_arch,
30600                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
30601                              ? 5 : 6);
30602   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
30603     {
30604       fp16_optional = 1;
30605       aeabi_set_attribute_int (Tag_VFP_arch, 3);
30606     }
30607   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
30608     {
30609       aeabi_set_attribute_int (Tag_VFP_arch, 4);
30610       fp16_optional = 1;
30611     }
30612   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
30613     aeabi_set_attribute_int (Tag_VFP_arch, 2);
30614   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
30615            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
30616     aeabi_set_attribute_int (Tag_VFP_arch, 1);
30617
30618   /* Tag_ABI_HardFP_use.  */
30619   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
30620       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
30621     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
30622
30623   /* Tag_WMMX_arch.  */
30624   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
30625     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
30626   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
30627     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
30628
30629   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
30630   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
30631     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
30632   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
30633     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
30634   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
30635     {
30636       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
30637         {
30638           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
30639         }
30640       else
30641         {
30642           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
30643           fp16_optional = 1;
30644         }
30645     }
30646
30647   if (ARM_CPU_HAS_FEATURE (flags, mve_fp_ext))
30648     aeabi_set_attribute_int (Tag_MVE_arch, 2);
30649   else if (ARM_CPU_HAS_FEATURE (flags, mve_ext))
30650     aeabi_set_attribute_int (Tag_MVE_arch, 1);
30651
30652   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
30653   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
30654     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
30655
30656   /* Tag_DIV_use.
30657
30658      We set Tag_DIV_use to two when integer divide instructions have been used
30659      in ARM state, or when Thumb integer divide instructions have been used,
30660      but we have no architecture profile set, nor have we any ARM instructions.
30661
30662      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
30663      by the base architecture.
30664
30665      For new architectures we will have to check these tests.  */
30666   gas_assert (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
30667   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
30668       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
30669     aeabi_set_attribute_int (Tag_DIV_use, 0);
30670   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
30671            || (profile == '\0'
30672                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
30673                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
30674     aeabi_set_attribute_int (Tag_DIV_use, 2);
30675
30676   /* Tag_MP_extension_use.  */
30677   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
30678     aeabi_set_attribute_int (Tag_MPextension_use, 1);
30679
30680   /* Tag Virtualization_use.  */
30681   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
30682     virt_sec |= 1;
30683   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
30684     virt_sec |= 2;
30685   if (virt_sec != 0)
30686     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
30687 }
30688
30689 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
30690    finished and free extension feature bits which will not be used anymore.  */
30691
30692 void
30693 arm_md_post_relax (void)
30694 {
30695   aeabi_set_public_attributes ();
30696   XDELETE (mcpu_ext_opt);
30697   mcpu_ext_opt = NULL;
30698   XDELETE (march_ext_opt);
30699   march_ext_opt = NULL;
30700 }
30701
30702 /* Add the default contents for the .ARM.attributes section.  */
30703
30704 void
30705 arm_md_end (void)
30706 {
30707   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
30708     return;
30709
30710   aeabi_set_public_attributes ();
30711 }
30712 #endif /* OBJ_ELF */
30713
30714 /* Parse a .cpu directive.  */
30715
30716 static void
30717 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
30718 {
30719   const struct arm_cpu_option_table *opt;
30720   char *name;
30721   char saved_char;
30722
30723   name = input_line_pointer;
30724   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
30725     input_line_pointer++;
30726   saved_char = *input_line_pointer;
30727   *input_line_pointer = 0;
30728
30729   /* Skip the first "all" entry.  */
30730   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
30731     if (streq (opt->name, name))
30732       {
30733         selected_arch = opt->value;
30734         selected_ext = opt->ext;
30735         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
30736         if (opt->canonical_name)
30737           strcpy (selected_cpu_name, opt->canonical_name);
30738         else
30739           {
30740             int i;
30741             for (i = 0; opt->name[i]; i++)
30742               selected_cpu_name[i] = TOUPPER (opt->name[i]);
30743
30744             selected_cpu_name[i] = 0;
30745           }
30746         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
30747
30748         *input_line_pointer = saved_char;
30749         demand_empty_rest_of_line ();
30750         return;
30751       }
30752   as_bad (_("unknown cpu `%s'"), name);
30753   *input_line_pointer = saved_char;
30754   ignore_rest_of_line ();
30755 }
30756
30757 /* Parse a .arch directive.  */
30758
30759 static void
30760 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
30761 {
30762   const struct arm_arch_option_table *opt;
30763   char saved_char;
30764   char *name;
30765
30766   name = input_line_pointer;
30767   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
30768     input_line_pointer++;
30769   saved_char = *input_line_pointer;
30770   *input_line_pointer = 0;
30771
30772   /* Skip the first "all" entry.  */
30773   for (opt = arm_archs + 1; opt->name != NULL; opt++)
30774     if (streq (opt->name, name))
30775       {
30776         selected_arch = opt->value;
30777         selected_ext = arm_arch_none;
30778         selected_cpu = selected_arch;
30779         strcpy (selected_cpu_name, opt->name);
30780         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
30781         *input_line_pointer = saved_char;
30782         demand_empty_rest_of_line ();
30783         return;
30784       }
30785
30786   as_bad (_("unknown architecture `%s'\n"), name);
30787   *input_line_pointer = saved_char;
30788   ignore_rest_of_line ();
30789 }
30790
30791 /* Parse a .object_arch directive.  */
30792
30793 static void
30794 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
30795 {
30796   const struct arm_arch_option_table *opt;
30797   char saved_char;
30798   char *name;
30799
30800   name = input_line_pointer;
30801   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
30802     input_line_pointer++;
30803   saved_char = *input_line_pointer;
30804   *input_line_pointer = 0;
30805
30806   /* Skip the first "all" entry.  */
30807   for (opt = arm_archs + 1; opt->name != NULL; opt++)
30808     if (streq (opt->name, name))
30809       {
30810         selected_object_arch = opt->value;
30811         *input_line_pointer = saved_char;
30812         demand_empty_rest_of_line ();
30813         return;
30814       }
30815
30816   as_bad (_("unknown architecture `%s'\n"), name);
30817   *input_line_pointer = saved_char;
30818   ignore_rest_of_line ();
30819 }
30820
30821 /* Parse a .arch_extension directive.  */
30822
30823 static void
30824 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
30825 {
30826   const struct arm_option_extension_value_table *opt;
30827   char saved_char;
30828   char *name;
30829   int adding_value = 1;
30830
30831   name = input_line_pointer;
30832   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
30833     input_line_pointer++;
30834   saved_char = *input_line_pointer;
30835   *input_line_pointer = 0;
30836
30837   if (strlen (name) >= 2
30838       && strncmp (name, "no", 2) == 0)
30839     {
30840       adding_value = 0;
30841       name += 2;
30842     }
30843
30844   for (opt = arm_extensions; opt->name != NULL; opt++)
30845     if (streq (opt->name, name))
30846       {
30847         int i, nb_allowed_archs =
30848           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
30849         for (i = 0; i < nb_allowed_archs; i++)
30850           {
30851             /* Empty entry.  */
30852             if (ARM_CPU_IS_ANY (opt->allowed_archs[i]))
30853               continue;
30854             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], selected_arch))
30855               break;
30856           }
30857
30858         if (i == nb_allowed_archs)
30859           {
30860             as_bad (_("architectural extension `%s' is not allowed for the "
30861                       "current base architecture"), name);
30862             break;
30863           }
30864
30865         if (adding_value)
30866           ARM_MERGE_FEATURE_SETS (selected_ext, selected_ext,
30867                                   opt->merge_value);
30868         else
30869           ARM_CLEAR_FEATURE (selected_ext, selected_ext, opt->clear_value);
30870
30871         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
30872         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
30873         *input_line_pointer = saved_char;
30874         demand_empty_rest_of_line ();
30875         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
30876            on this return so that duplicate extensions (extensions with the
30877            same name as a previous extension in the list) are not considered
30878            for command-line parsing.  */
30879         return;
30880       }
30881
30882   if (opt->name == NULL)
30883     as_bad (_("unknown architecture extension `%s'\n"), name);
30884
30885   *input_line_pointer = saved_char;
30886   ignore_rest_of_line ();
30887 }
30888
30889 /* Parse a .fpu directive.  */
30890
30891 static void
30892 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
30893 {
30894   const struct arm_option_fpu_value_table *opt;
30895   char saved_char;
30896   char *name;
30897
30898   name = input_line_pointer;
30899   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
30900     input_line_pointer++;
30901   saved_char = *input_line_pointer;
30902   *input_line_pointer = 0;
30903
30904   for (opt = arm_fpus; opt->name != NULL; opt++)
30905     if (streq (opt->name, name))
30906       {
30907         selected_fpu = opt->value;
30908 #ifndef CPU_DEFAULT
30909         if (no_cpu_selected ())
30910           ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
30911         else
30912 #endif
30913           ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
30914         *input_line_pointer = saved_char;
30915         demand_empty_rest_of_line ();
30916         return;
30917       }
30918
30919   as_bad (_("unknown floating point format `%s'\n"), name);
30920   *input_line_pointer = saved_char;
30921   ignore_rest_of_line ();
30922 }
30923
30924 /* Copy symbol information.  */
30925
30926 void
30927 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
30928 {
30929   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
30930 }
30931
30932 #ifdef OBJ_ELF
30933 /* Given a symbolic attribute NAME, return the proper integer value.
30934    Returns -1 if the attribute is not known.  */
30935
30936 int
30937 arm_convert_symbolic_attribute (const char *name)
30938 {
30939   static const struct
30940   {
30941     const char * name;
30942     const int    tag;
30943   }
30944   attribute_table[] =
30945     {
30946       /* When you modify this table you should
30947          also modify the list in doc/c-arm.texi.  */
30948 #define T(tag) {#tag, tag}
30949       T (Tag_CPU_raw_name),
30950       T (Tag_CPU_name),
30951       T (Tag_CPU_arch),
30952       T (Tag_CPU_arch_profile),
30953       T (Tag_ARM_ISA_use),
30954       T (Tag_THUMB_ISA_use),
30955       T (Tag_FP_arch),
30956       T (Tag_VFP_arch),
30957       T (Tag_WMMX_arch),
30958       T (Tag_Advanced_SIMD_arch),
30959       T (Tag_PCS_config),
30960       T (Tag_ABI_PCS_R9_use),
30961       T (Tag_ABI_PCS_RW_data),
30962       T (Tag_ABI_PCS_RO_data),
30963       T (Tag_ABI_PCS_GOT_use),
30964       T (Tag_ABI_PCS_wchar_t),
30965       T (Tag_ABI_FP_rounding),
30966       T (Tag_ABI_FP_denormal),
30967       T (Tag_ABI_FP_exceptions),
30968       T (Tag_ABI_FP_user_exceptions),
30969       T (Tag_ABI_FP_number_model),
30970       T (Tag_ABI_align_needed),
30971       T (Tag_ABI_align8_needed),
30972       T (Tag_ABI_align_preserved),
30973       T (Tag_ABI_align8_preserved),
30974       T (Tag_ABI_enum_size),
30975       T (Tag_ABI_HardFP_use),
30976       T (Tag_ABI_VFP_args),
30977       T (Tag_ABI_WMMX_args),
30978       T (Tag_ABI_optimization_goals),
30979       T (Tag_ABI_FP_optimization_goals),
30980       T (Tag_compatibility),
30981       T (Tag_CPU_unaligned_access),
30982       T (Tag_FP_HP_extension),
30983       T (Tag_VFP_HP_extension),
30984       T (Tag_ABI_FP_16bit_format),
30985       T (Tag_MPextension_use),
30986       T (Tag_DIV_use),
30987       T (Tag_nodefaults),
30988       T (Tag_also_compatible_with),
30989       T (Tag_conformance),
30990       T (Tag_T2EE_use),
30991       T (Tag_Virtualization_use),
30992       T (Tag_DSP_extension),
30993       T (Tag_MVE_arch),
30994       /* We deliberately do not include Tag_MPextension_use_legacy.  */
30995 #undef T
30996     };
30997   unsigned int i;
30998
30999   if (name == NULL)
31000     return -1;
31001
31002   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
31003     if (streq (name, attribute_table[i].name))
31004       return attribute_table[i].tag;
31005
31006   return -1;
31007 }
31008
31009 /* Apply sym value for relocations only in the case that they are for
31010    local symbols in the same segment as the fixup and you have the
31011    respective architectural feature for blx and simple switches.  */
31012
31013 int
31014 arm_apply_sym_value (struct fix * fixP, segT this_seg)
31015 {
31016   if (fixP->fx_addsy
31017       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
31018       /* PR 17444: If the local symbol is in a different section then a reloc
31019          will always be generated for it, so applying the symbol value now
31020          will result in a double offset being stored in the relocation.  */
31021       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
31022       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
31023     {
31024       switch (fixP->fx_r_type)
31025         {
31026         case BFD_RELOC_ARM_PCREL_BLX:
31027         case BFD_RELOC_THUMB_PCREL_BRANCH23:
31028           if (ARM_IS_FUNC (fixP->fx_addsy))
31029             return 1;
31030           break;
31031
31032         case BFD_RELOC_ARM_PCREL_CALL:
31033         case BFD_RELOC_THUMB_PCREL_BLX:
31034           if (THUMB_IS_FUNC (fixP->fx_addsy))
31035             return 1;
31036           break;
31037
31038         default:
31039           break;
31040         }
31041
31042     }
31043   return 0;
31044 }
31045 #endif /* OBJ_ELF */